drm_bufs.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*
  2. * Legacy: Generic DRM Buffer Management
  3. *
  4. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Author: Rickard E. (Rik) Faith <faith@valinux.com>
  9. * Author: Gareth Hughes <gareth@valinux.com>
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the next
  19. * paragraph) shall be included in all copies or substantial portions of the
  20. * Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  26. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  27. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  28. * OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #include <linux/vmalloc.h>
  31. #include <linux/slab.h>
  32. #include <linux/log2.h>
  33. #include <linux/export.h>
  34. #include <asm/shmparam.h>
  35. #include <drm/drmP.h>
  36. #include "drm_legacy.h"
  37. static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
  38. struct drm_local_map *map)
  39. {
  40. struct drm_map_list *entry;
  41. list_for_each_entry(entry, &dev->maplist, head) {
  42. /*
  43. * Because the kernel-userspace ABI is fixed at a 32-bit offset
  44. * while PCI resources may live above that, we only compare the
  45. * lower 32 bits of the map offset for maps of type
  46. * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
  47. * It is assumed that if a driver have more than one resource
  48. * of each type, the lower 32 bits are different.
  49. */
  50. if (!entry->map ||
  51. map->type != entry->map->type ||
  52. entry->master != dev->master)
  53. continue;
  54. switch (map->type) {
  55. case _DRM_SHM:
  56. if (map->flags != _DRM_CONTAINS_LOCK)
  57. break;
  58. return entry;
  59. case _DRM_REGISTERS:
  60. case _DRM_FRAME_BUFFER:
  61. if ((entry->map->offset & 0xffffffff) ==
  62. (map->offset & 0xffffffff))
  63. return entry;
  64. default: /* Make gcc happy */
  65. ;
  66. }
  67. if (entry->map->offset == map->offset)
  68. return entry;
  69. }
  70. return NULL;
  71. }
  72. static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
  73. unsigned long user_token, int hashed_handle, int shm)
  74. {
  75. int use_hashed_handle, shift;
  76. unsigned long add;
  77. #if (BITS_PER_LONG == 64)
  78. use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
  79. #elif (BITS_PER_LONG == 32)
  80. use_hashed_handle = hashed_handle;
  81. #else
  82. #error Unsupported long size. Neither 64 nor 32 bits.
  83. #endif
  84. if (!use_hashed_handle) {
  85. int ret;
  86. hash->key = user_token >> PAGE_SHIFT;
  87. ret = drm_ht_insert_item(&dev->map_hash, hash);
  88. if (ret != -EINVAL)
  89. return ret;
  90. }
  91. shift = 0;
  92. add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
  93. if (shm && (SHMLBA > PAGE_SIZE)) {
  94. int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
  95. /* For shared memory, we have to preserve the SHMLBA
  96. * bits of the eventual vma->vm_pgoff value during
  97. * mmap(). Otherwise we run into cache aliasing problems
  98. * on some platforms. On these platforms, the pgoff of
  99. * a mmap() request is used to pick a suitable virtual
  100. * address for the mmap() region such that it will not
  101. * cause cache aliasing problems.
  102. *
  103. * Therefore, make sure the SHMLBA relevant bits of the
  104. * hash value we use are equal to those in the original
  105. * kernel virtual address.
  106. */
  107. shift = bits;
  108. add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
  109. }
  110. return drm_ht_just_insert_please(&dev->map_hash, hash,
  111. user_token, 32 - PAGE_SHIFT - 3,
  112. shift, add);
  113. }
  114. /**
  115. * Core function to create a range of memory available for mapping by a
  116. * non-root process.
  117. *
  118. * Adjusts the memory offset to its absolute value according to the mapping
  119. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  120. * applicable and if supported by the kernel.
  121. */
  122. static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
  123. unsigned int size, enum drm_map_type type,
  124. enum drm_map_flags flags,
  125. struct drm_map_list ** maplist)
  126. {
  127. struct drm_local_map *map;
  128. struct drm_map_list *list;
  129. drm_dma_handle_t *dmah;
  130. unsigned long user_token;
  131. int ret;
  132. map = kmalloc(sizeof(*map), GFP_KERNEL);
  133. if (!map)
  134. return -ENOMEM;
  135. map->offset = offset;
  136. map->size = size;
  137. map->flags = flags;
  138. map->type = type;
  139. /* Only allow shared memory to be removable since we only keep enough
  140. * book keeping information about shared memory to allow for removal
  141. * when processes fork.
  142. */
  143. if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
  144. kfree(map);
  145. return -EINVAL;
  146. }
  147. DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
  148. (unsigned long long)map->offset, map->size, map->type);
  149. /* page-align _DRM_SHM maps. They are allocated here so there is no security
  150. * hole created by that and it works around various broken drivers that use
  151. * a non-aligned quantity to map the SAREA. --BenH
  152. */
  153. if (map->type == _DRM_SHM)
  154. map->size = PAGE_ALIGN(map->size);
  155. if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
  156. kfree(map);
  157. return -EINVAL;
  158. }
  159. map->mtrr = -1;
  160. map->handle = NULL;
  161. switch (map->type) {
  162. case _DRM_REGISTERS:
  163. case _DRM_FRAME_BUFFER:
  164. #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
  165. if (map->offset + (map->size-1) < map->offset ||
  166. map->offset < virt_to_phys(high_memory)) {
  167. kfree(map);
  168. return -EINVAL;
  169. }
  170. #endif
  171. /* Some drivers preinitialize some maps, without the X Server
  172. * needing to be aware of it. Therefore, we just return success
  173. * when the server tries to create a duplicate map.
  174. */
  175. list = drm_find_matching_map(dev, map);
  176. if (list != NULL) {
  177. if (list->map->size != map->size) {
  178. DRM_DEBUG("Matching maps of type %d with "
  179. "mismatched sizes, (%ld vs %ld)\n",
  180. map->type, map->size,
  181. list->map->size);
  182. list->map->size = map->size;
  183. }
  184. kfree(map);
  185. *maplist = list;
  186. return 0;
  187. }
  188. if (map->type == _DRM_FRAME_BUFFER ||
  189. (map->flags & _DRM_WRITE_COMBINING)) {
  190. map->mtrr =
  191. arch_phys_wc_add(map->offset, map->size);
  192. }
  193. if (map->type == _DRM_REGISTERS) {
  194. if (map->flags & _DRM_WRITE_COMBINING)
  195. map->handle = ioremap_wc(map->offset,
  196. map->size);
  197. else
  198. map->handle = ioremap(map->offset, map->size);
  199. if (!map->handle) {
  200. kfree(map);
  201. return -ENOMEM;
  202. }
  203. }
  204. break;
  205. case _DRM_SHM:
  206. list = drm_find_matching_map(dev, map);
  207. if (list != NULL) {
  208. if(list->map->size != map->size) {
  209. DRM_DEBUG("Matching maps of type %d with "
  210. "mismatched sizes, (%ld vs %ld)\n",
  211. map->type, map->size, list->map->size);
  212. list->map->size = map->size;
  213. }
  214. kfree(map);
  215. *maplist = list;
  216. return 0;
  217. }
  218. map->handle = vmalloc_user(map->size);
  219. DRM_DEBUG("%lu %d %p\n",
  220. map->size, order_base_2(map->size), map->handle);
  221. if (!map->handle) {
  222. kfree(map);
  223. return -ENOMEM;
  224. }
  225. map->offset = (unsigned long)map->handle;
  226. if (map->flags & _DRM_CONTAINS_LOCK) {
  227. /* Prevent a 2nd X Server from creating a 2nd lock */
  228. if (dev->master->lock.hw_lock != NULL) {
  229. vfree(map->handle);
  230. kfree(map);
  231. return -EBUSY;
  232. }
  233. dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
  234. }
  235. break;
  236. case _DRM_AGP: {
  237. struct drm_agp_mem *entry;
  238. int valid = 0;
  239. if (!dev->agp) {
  240. kfree(map);
  241. return -EINVAL;
  242. }
  243. #ifdef __alpha__
  244. map->offset += dev->hose->mem_space->start;
  245. #endif
  246. /* In some cases (i810 driver), user space may have already
  247. * added the AGP base itself, because dev->agp->base previously
  248. * only got set during AGP enable. So, only add the base
  249. * address if the map's offset isn't already within the
  250. * aperture.
  251. */
  252. if (map->offset < dev->agp->base ||
  253. map->offset > dev->agp->base +
  254. dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
  255. map->offset += dev->agp->base;
  256. }
  257. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  258. /* This assumes the DRM is in total control of AGP space.
  259. * It's not always the case as AGP can be in the control
  260. * of user space (i.e. i810 driver). So this loop will get
  261. * skipped and we double check that dev->agp->memory is
  262. * actually set as well as being invalid before EPERM'ing
  263. */
  264. list_for_each_entry(entry, &dev->agp->memory, head) {
  265. if ((map->offset >= entry->bound) &&
  266. (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
  267. valid = 1;
  268. break;
  269. }
  270. }
  271. if (!list_empty(&dev->agp->memory) && !valid) {
  272. kfree(map);
  273. return -EPERM;
  274. }
  275. DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
  276. (unsigned long long)map->offset, map->size);
  277. break;
  278. }
  279. case _DRM_SCATTER_GATHER:
  280. if (!dev->sg) {
  281. kfree(map);
  282. return -EINVAL;
  283. }
  284. map->offset += (unsigned long)dev->sg->virtual;
  285. break;
  286. case _DRM_CONSISTENT:
  287. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  288. * As we're limiting the address to 2^32-1 (or less),
  289. * casting it down to 32 bits is no problem, but we
  290. * need to point to a 64bit variable first. */
  291. dmah = drm_pci_alloc(dev, map->size, map->size);
  292. if (!dmah) {
  293. kfree(map);
  294. return -ENOMEM;
  295. }
  296. map->handle = dmah->vaddr;
  297. map->offset = (unsigned long)dmah->busaddr;
  298. kfree(dmah);
  299. break;
  300. default:
  301. kfree(map);
  302. return -EINVAL;
  303. }
  304. list = kzalloc(sizeof(*list), GFP_KERNEL);
  305. if (!list) {
  306. if (map->type == _DRM_REGISTERS)
  307. iounmap(map->handle);
  308. kfree(map);
  309. return -EINVAL;
  310. }
  311. list->map = map;
  312. mutex_lock(&dev->struct_mutex);
  313. list_add(&list->head, &dev->maplist);
  314. /* Assign a 32-bit handle */
  315. /* We do it here so that dev->struct_mutex protects the increment */
  316. user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
  317. map->offset;
  318. ret = drm_map_handle(dev, &list->hash, user_token, 0,
  319. (map->type == _DRM_SHM));
  320. if (ret) {
  321. if (map->type == _DRM_REGISTERS)
  322. iounmap(map->handle);
  323. kfree(map);
  324. kfree(list);
  325. mutex_unlock(&dev->struct_mutex);
  326. return ret;
  327. }
  328. list->user_token = list->hash.key << PAGE_SHIFT;
  329. mutex_unlock(&dev->struct_mutex);
  330. if (!(map->flags & _DRM_DRIVER))
  331. list->master = dev->master;
  332. *maplist = list;
  333. return 0;
  334. }
  335. int drm_legacy_addmap(struct drm_device * dev, resource_size_t offset,
  336. unsigned int size, enum drm_map_type type,
  337. enum drm_map_flags flags, struct drm_local_map **map_ptr)
  338. {
  339. struct drm_map_list *list;
  340. int rc;
  341. rc = drm_addmap_core(dev, offset, size, type, flags, &list);
  342. if (!rc)
  343. *map_ptr = list->map;
  344. return rc;
  345. }
  346. EXPORT_SYMBOL(drm_legacy_addmap);
  347. /**
  348. * Ioctl to specify a range of memory that is available for mapping by a
  349. * non-root process.
  350. *
  351. * \param inode device inode.
  352. * \param file_priv DRM file private.
  353. * \param cmd command.
  354. * \param arg pointer to a drm_map structure.
  355. * \return zero on success or a negative value on error.
  356. *
  357. */
  358. int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
  359. struct drm_file *file_priv)
  360. {
  361. struct drm_map *map = data;
  362. struct drm_map_list *maplist;
  363. int err;
  364. if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
  365. return -EPERM;
  366. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  367. !drm_core_check_feature(dev, DRIVER_LEGACY))
  368. return -EINVAL;
  369. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  370. map->flags, &maplist);
  371. if (err)
  372. return err;
  373. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  374. map->handle = (void *)(unsigned long)maplist->user_token;
  375. /*
  376. * It appears that there are no users of this value whatsoever --
  377. * drmAddMap just discards it. Let's not encourage its use.
  378. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  379. * it's not a real mtrr index anymore.)
  380. */
  381. map->mtrr = -1;
  382. return 0;
  383. }
  384. /*
  385. * Get a mapping information.
  386. *
  387. * \param inode device inode.
  388. * \param file_priv DRM file private.
  389. * \param cmd command.
  390. * \param arg user argument, pointing to a drm_map structure.
  391. *
  392. * \return zero on success or a negative number on failure.
  393. *
  394. * Searches for the mapping with the specified offset and copies its information
  395. * into userspace
  396. */
  397. int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
  398. struct drm_file *file_priv)
  399. {
  400. struct drm_map *map = data;
  401. struct drm_map_list *r_list = NULL;
  402. struct list_head *list;
  403. int idx;
  404. int i;
  405. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  406. !drm_core_check_feature(dev, DRIVER_LEGACY))
  407. return -EINVAL;
  408. idx = map->offset;
  409. if (idx < 0)
  410. return -EINVAL;
  411. i = 0;
  412. mutex_lock(&dev->struct_mutex);
  413. list_for_each(list, &dev->maplist) {
  414. if (i == idx) {
  415. r_list = list_entry(list, struct drm_map_list, head);
  416. break;
  417. }
  418. i++;
  419. }
  420. if (!r_list || !r_list->map) {
  421. mutex_unlock(&dev->struct_mutex);
  422. return -EINVAL;
  423. }
  424. map->offset = r_list->map->offset;
  425. map->size = r_list->map->size;
  426. map->type = r_list->map->type;
  427. map->flags = r_list->map->flags;
  428. map->handle = (void *)(unsigned long) r_list->user_token;
  429. map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
  430. mutex_unlock(&dev->struct_mutex);
  431. return 0;
  432. }
  433. /**
  434. * Remove a map private from list and deallocate resources if the mapping
  435. * isn't in use.
  436. *
  437. * Searches the map on drm_device::maplist, removes it from the list, see if
  438. * its being used, and free any associate resource (such as MTRR's) if it's not
  439. * being on use.
  440. *
  441. * \sa drm_legacy_addmap
  442. */
  443. int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  444. {
  445. struct drm_map_list *r_list = NULL, *list_t;
  446. drm_dma_handle_t dmah;
  447. int found = 0;
  448. struct drm_master *master;
  449. /* Find the list entry for the map and remove it */
  450. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  451. if (r_list->map == map) {
  452. master = r_list->master;
  453. list_del(&r_list->head);
  454. drm_ht_remove_key(&dev->map_hash,
  455. r_list->user_token >> PAGE_SHIFT);
  456. kfree(r_list);
  457. found = 1;
  458. break;
  459. }
  460. }
  461. if (!found)
  462. return -EINVAL;
  463. switch (map->type) {
  464. case _DRM_REGISTERS:
  465. iounmap(map->handle);
  466. /* FALLTHROUGH */
  467. case _DRM_FRAME_BUFFER:
  468. arch_phys_wc_del(map->mtrr);
  469. break;
  470. case _DRM_SHM:
  471. vfree(map->handle);
  472. if (master) {
  473. if (dev->sigdata.lock == master->lock.hw_lock)
  474. dev->sigdata.lock = NULL;
  475. master->lock.hw_lock = NULL; /* SHM removed */
  476. master->lock.file_priv = NULL;
  477. wake_up_interruptible_all(&master->lock.lock_queue);
  478. }
  479. break;
  480. case _DRM_AGP:
  481. case _DRM_SCATTER_GATHER:
  482. break;
  483. case _DRM_CONSISTENT:
  484. dmah.vaddr = map->handle;
  485. dmah.busaddr = map->offset;
  486. dmah.size = map->size;
  487. __drm_legacy_pci_free(dev, &dmah);
  488. break;
  489. }
  490. kfree(map);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL(drm_legacy_rmmap_locked);
  494. void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
  495. {
  496. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  497. !drm_core_check_feature(dev, DRIVER_LEGACY))
  498. return;
  499. mutex_lock(&dev->struct_mutex);
  500. drm_legacy_rmmap_locked(dev, map);
  501. mutex_unlock(&dev->struct_mutex);
  502. }
  503. EXPORT_SYMBOL(drm_legacy_rmmap);
  504. void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
  505. {
  506. struct drm_map_list *r_list, *list_temp;
  507. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  508. return;
  509. mutex_lock(&dev->struct_mutex);
  510. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  511. if (r_list->master == master) {
  512. drm_legacy_rmmap_locked(dev, r_list->map);
  513. r_list = NULL;
  514. }
  515. }
  516. mutex_unlock(&dev->struct_mutex);
  517. }
  518. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  519. * the last close of the device, and this is necessary for cleanup when things
  520. * exit uncleanly. Therefore, having userland manually remove mappings seems
  521. * like a pointless exercise since they're going away anyway.
  522. *
  523. * One use case might be after addmap is allowed for normal users for SHM and
  524. * gets used by drivers that the server doesn't need to care about. This seems
  525. * unlikely.
  526. *
  527. * \param inode device inode.
  528. * \param file_priv DRM file private.
  529. * \param cmd command.
  530. * \param arg pointer to a struct drm_map structure.
  531. * \return zero on success or a negative value on error.
  532. */
  533. int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
  534. struct drm_file *file_priv)
  535. {
  536. struct drm_map *request = data;
  537. struct drm_local_map *map = NULL;
  538. struct drm_map_list *r_list;
  539. int ret;
  540. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  541. !drm_core_check_feature(dev, DRIVER_LEGACY))
  542. return -EINVAL;
  543. mutex_lock(&dev->struct_mutex);
  544. list_for_each_entry(r_list, &dev->maplist, head) {
  545. if (r_list->map &&
  546. r_list->user_token == (unsigned long)request->handle &&
  547. r_list->map->flags & _DRM_REMOVABLE) {
  548. map = r_list->map;
  549. break;
  550. }
  551. }
  552. /* List has wrapped around to the head pointer, or its empty we didn't
  553. * find anything.
  554. */
  555. if (list_empty(&dev->maplist) || !map) {
  556. mutex_unlock(&dev->struct_mutex);
  557. return -EINVAL;
  558. }
  559. /* Register and framebuffer maps are permanent */
  560. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  561. mutex_unlock(&dev->struct_mutex);
  562. return 0;
  563. }
  564. ret = drm_legacy_rmmap_locked(dev, map);
  565. mutex_unlock(&dev->struct_mutex);
  566. return ret;
  567. }
  568. /**
  569. * Cleanup after an error on one of the addbufs() functions.
  570. *
  571. * \param dev DRM device.
  572. * \param entry buffer entry where the error occurred.
  573. *
  574. * Frees any pages and buffers associated with the given entry.
  575. */
  576. static void drm_cleanup_buf_error(struct drm_device * dev,
  577. struct drm_buf_entry * entry)
  578. {
  579. int i;
  580. if (entry->seg_count) {
  581. for (i = 0; i < entry->seg_count; i++) {
  582. if (entry->seglist[i]) {
  583. drm_pci_free(dev, entry->seglist[i]);
  584. }
  585. }
  586. kfree(entry->seglist);
  587. entry->seg_count = 0;
  588. }
  589. if (entry->buf_count) {
  590. for (i = 0; i < entry->buf_count; i++) {
  591. kfree(entry->buflist[i].dev_private);
  592. }
  593. kfree(entry->buflist);
  594. entry->buf_count = 0;
  595. }
  596. }
  597. #if IS_ENABLED(CONFIG_AGP)
  598. /**
  599. * Add AGP buffers for DMA transfers.
  600. *
  601. * \param dev struct drm_device to which the buffers are to be added.
  602. * \param request pointer to a struct drm_buf_desc describing the request.
  603. * \return zero on success or a negative number on failure.
  604. *
  605. * After some sanity checks creates a drm_buf structure for each buffer and
  606. * reallocates the buffer list of the same size order to accommodate the new
  607. * buffers.
  608. */
  609. int drm_legacy_addbufs_agp(struct drm_device *dev,
  610. struct drm_buf_desc *request)
  611. {
  612. struct drm_device_dma *dma = dev->dma;
  613. struct drm_buf_entry *entry;
  614. struct drm_agp_mem *agp_entry;
  615. struct drm_buf *buf;
  616. unsigned long offset;
  617. unsigned long agp_offset;
  618. int count;
  619. int order;
  620. int size;
  621. int alignment;
  622. int page_order;
  623. int total;
  624. int byte_count;
  625. int i, valid;
  626. struct drm_buf **temp_buflist;
  627. if (!dma)
  628. return -EINVAL;
  629. count = request->count;
  630. order = order_base_2(request->size);
  631. size = 1 << order;
  632. alignment = (request->flags & _DRM_PAGE_ALIGN)
  633. ? PAGE_ALIGN(size) : size;
  634. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  635. total = PAGE_SIZE << page_order;
  636. byte_count = 0;
  637. agp_offset = dev->agp->base + request->agp_start;
  638. DRM_DEBUG("count: %d\n", count);
  639. DRM_DEBUG("order: %d\n", order);
  640. DRM_DEBUG("size: %d\n", size);
  641. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  642. DRM_DEBUG("alignment: %d\n", alignment);
  643. DRM_DEBUG("page_order: %d\n", page_order);
  644. DRM_DEBUG("total: %d\n", total);
  645. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  646. return -EINVAL;
  647. /* Make sure buffers are located in AGP memory that we own */
  648. valid = 0;
  649. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  650. if ((agp_offset >= agp_entry->bound) &&
  651. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  652. valid = 1;
  653. break;
  654. }
  655. }
  656. if (!list_empty(&dev->agp->memory) && !valid) {
  657. DRM_DEBUG("zone invalid\n");
  658. return -EINVAL;
  659. }
  660. spin_lock(&dev->buf_lock);
  661. if (dev->buf_use) {
  662. spin_unlock(&dev->buf_lock);
  663. return -EBUSY;
  664. }
  665. atomic_inc(&dev->buf_alloc);
  666. spin_unlock(&dev->buf_lock);
  667. mutex_lock(&dev->struct_mutex);
  668. entry = &dma->bufs[order];
  669. if (entry->buf_count) {
  670. mutex_unlock(&dev->struct_mutex);
  671. atomic_dec(&dev->buf_alloc);
  672. return -ENOMEM; /* May only call once for each order */
  673. }
  674. if (count < 0 || count > 4096) {
  675. mutex_unlock(&dev->struct_mutex);
  676. atomic_dec(&dev->buf_alloc);
  677. return -EINVAL;
  678. }
  679. entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
  680. if (!entry->buflist) {
  681. mutex_unlock(&dev->struct_mutex);
  682. atomic_dec(&dev->buf_alloc);
  683. return -ENOMEM;
  684. }
  685. entry->buf_size = size;
  686. entry->page_order = page_order;
  687. offset = 0;
  688. while (entry->buf_count < count) {
  689. buf = &entry->buflist[entry->buf_count];
  690. buf->idx = dma->buf_count + entry->buf_count;
  691. buf->total = alignment;
  692. buf->order = order;
  693. buf->used = 0;
  694. buf->offset = (dma->byte_count + offset);
  695. buf->bus_address = agp_offset + offset;
  696. buf->address = (void *)(agp_offset + offset);
  697. buf->next = NULL;
  698. buf->waiting = 0;
  699. buf->pending = 0;
  700. buf->file_priv = NULL;
  701. buf->dev_priv_size = dev->driver->dev_priv_size;
  702. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  703. if (!buf->dev_private) {
  704. /* Set count correctly so we free the proper amount. */
  705. entry->buf_count = count;
  706. drm_cleanup_buf_error(dev, entry);
  707. mutex_unlock(&dev->struct_mutex);
  708. atomic_dec(&dev->buf_alloc);
  709. return -ENOMEM;
  710. }
  711. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  712. offset += alignment;
  713. entry->buf_count++;
  714. byte_count += PAGE_SIZE << page_order;
  715. }
  716. DRM_DEBUG("byte_count: %d\n", byte_count);
  717. temp_buflist = krealloc(dma->buflist,
  718. (dma->buf_count + entry->buf_count) *
  719. sizeof(*dma->buflist), GFP_KERNEL);
  720. if (!temp_buflist) {
  721. /* Free the entry because it isn't valid */
  722. drm_cleanup_buf_error(dev, entry);
  723. mutex_unlock(&dev->struct_mutex);
  724. atomic_dec(&dev->buf_alloc);
  725. return -ENOMEM;
  726. }
  727. dma->buflist = temp_buflist;
  728. for (i = 0; i < entry->buf_count; i++) {
  729. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  730. }
  731. dma->buf_count += entry->buf_count;
  732. dma->seg_count += entry->seg_count;
  733. dma->page_count += byte_count >> PAGE_SHIFT;
  734. dma->byte_count += byte_count;
  735. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  736. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  737. mutex_unlock(&dev->struct_mutex);
  738. request->count = entry->buf_count;
  739. request->size = size;
  740. dma->flags = _DRM_DMA_USE_AGP;
  741. atomic_dec(&dev->buf_alloc);
  742. return 0;
  743. }
  744. EXPORT_SYMBOL(drm_legacy_addbufs_agp);
  745. #endif /* CONFIG_AGP */
  746. int drm_legacy_addbufs_pci(struct drm_device *dev,
  747. struct drm_buf_desc *request)
  748. {
  749. struct drm_device_dma *dma = dev->dma;
  750. int count;
  751. int order;
  752. int size;
  753. int total;
  754. int page_order;
  755. struct drm_buf_entry *entry;
  756. drm_dma_handle_t *dmah;
  757. struct drm_buf *buf;
  758. int alignment;
  759. unsigned long offset;
  760. int i;
  761. int byte_count;
  762. int page_count;
  763. unsigned long *temp_pagelist;
  764. struct drm_buf **temp_buflist;
  765. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  766. return -EINVAL;
  767. if (!dma)
  768. return -EINVAL;
  769. if (!capable(CAP_SYS_ADMIN))
  770. return -EPERM;
  771. count = request->count;
  772. order = order_base_2(request->size);
  773. size = 1 << order;
  774. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  775. request->count, request->size, size, order);
  776. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  777. return -EINVAL;
  778. alignment = (request->flags & _DRM_PAGE_ALIGN)
  779. ? PAGE_ALIGN(size) : size;
  780. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  781. total = PAGE_SIZE << page_order;
  782. spin_lock(&dev->buf_lock);
  783. if (dev->buf_use) {
  784. spin_unlock(&dev->buf_lock);
  785. return -EBUSY;
  786. }
  787. atomic_inc(&dev->buf_alloc);
  788. spin_unlock(&dev->buf_lock);
  789. mutex_lock(&dev->struct_mutex);
  790. entry = &dma->bufs[order];
  791. if (entry->buf_count) {
  792. mutex_unlock(&dev->struct_mutex);
  793. atomic_dec(&dev->buf_alloc);
  794. return -ENOMEM; /* May only call once for each order */
  795. }
  796. if (count < 0 || count > 4096) {
  797. mutex_unlock(&dev->struct_mutex);
  798. atomic_dec(&dev->buf_alloc);
  799. return -EINVAL;
  800. }
  801. entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
  802. if (!entry->buflist) {
  803. mutex_unlock(&dev->struct_mutex);
  804. atomic_dec(&dev->buf_alloc);
  805. return -ENOMEM;
  806. }
  807. entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
  808. if (!entry->seglist) {
  809. kfree(entry->buflist);
  810. mutex_unlock(&dev->struct_mutex);
  811. atomic_dec(&dev->buf_alloc);
  812. return -ENOMEM;
  813. }
  814. /* Keep the original pagelist until we know all the allocations
  815. * have succeeded
  816. */
  817. temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
  818. sizeof(*dma->pagelist),
  819. GFP_KERNEL);
  820. if (!temp_pagelist) {
  821. kfree(entry->buflist);
  822. kfree(entry->seglist);
  823. mutex_unlock(&dev->struct_mutex);
  824. atomic_dec(&dev->buf_alloc);
  825. return -ENOMEM;
  826. }
  827. memcpy(temp_pagelist,
  828. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  829. DRM_DEBUG("pagelist: %d entries\n",
  830. dma->page_count + (count << page_order));
  831. entry->buf_size = size;
  832. entry->page_order = page_order;
  833. byte_count = 0;
  834. page_count = 0;
  835. while (entry->buf_count < count) {
  836. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  837. if (!dmah) {
  838. /* Set count correctly so we free the proper amount. */
  839. entry->buf_count = count;
  840. entry->seg_count = count;
  841. drm_cleanup_buf_error(dev, entry);
  842. kfree(temp_pagelist);
  843. mutex_unlock(&dev->struct_mutex);
  844. atomic_dec(&dev->buf_alloc);
  845. return -ENOMEM;
  846. }
  847. entry->seglist[entry->seg_count++] = dmah;
  848. for (i = 0; i < (1 << page_order); i++) {
  849. DRM_DEBUG("page %d @ 0x%08lx\n",
  850. dma->page_count + page_count,
  851. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  852. temp_pagelist[dma->page_count + page_count++]
  853. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  854. }
  855. for (offset = 0;
  856. offset + size <= total && entry->buf_count < count;
  857. offset += alignment, ++entry->buf_count) {
  858. buf = &entry->buflist[entry->buf_count];
  859. buf->idx = dma->buf_count + entry->buf_count;
  860. buf->total = alignment;
  861. buf->order = order;
  862. buf->used = 0;
  863. buf->offset = (dma->byte_count + byte_count + offset);
  864. buf->address = (void *)(dmah->vaddr + offset);
  865. buf->bus_address = dmah->busaddr + offset;
  866. buf->next = NULL;
  867. buf->waiting = 0;
  868. buf->pending = 0;
  869. buf->file_priv = NULL;
  870. buf->dev_priv_size = dev->driver->dev_priv_size;
  871. buf->dev_private = kzalloc(buf->dev_priv_size,
  872. GFP_KERNEL);
  873. if (!buf->dev_private) {
  874. /* Set count correctly so we free the proper amount. */
  875. entry->buf_count = count;
  876. entry->seg_count = count;
  877. drm_cleanup_buf_error(dev, entry);
  878. kfree(temp_pagelist);
  879. mutex_unlock(&dev->struct_mutex);
  880. atomic_dec(&dev->buf_alloc);
  881. return -ENOMEM;
  882. }
  883. DRM_DEBUG("buffer %d @ %p\n",
  884. entry->buf_count, buf->address);
  885. }
  886. byte_count += PAGE_SIZE << page_order;
  887. }
  888. temp_buflist = krealloc(dma->buflist,
  889. (dma->buf_count + entry->buf_count) *
  890. sizeof(*dma->buflist), GFP_KERNEL);
  891. if (!temp_buflist) {
  892. /* Free the entry because it isn't valid */
  893. drm_cleanup_buf_error(dev, entry);
  894. kfree(temp_pagelist);
  895. mutex_unlock(&dev->struct_mutex);
  896. atomic_dec(&dev->buf_alloc);
  897. return -ENOMEM;
  898. }
  899. dma->buflist = temp_buflist;
  900. for (i = 0; i < entry->buf_count; i++) {
  901. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  902. }
  903. /* No allocations failed, so now we can replace the original pagelist
  904. * with the new one.
  905. */
  906. if (dma->page_count) {
  907. kfree(dma->pagelist);
  908. }
  909. dma->pagelist = temp_pagelist;
  910. dma->buf_count += entry->buf_count;
  911. dma->seg_count += entry->seg_count;
  912. dma->page_count += entry->seg_count << page_order;
  913. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  914. mutex_unlock(&dev->struct_mutex);
  915. request->count = entry->buf_count;
  916. request->size = size;
  917. if (request->flags & _DRM_PCI_BUFFER_RO)
  918. dma->flags = _DRM_DMA_USE_PCI_RO;
  919. atomic_dec(&dev->buf_alloc);
  920. return 0;
  921. }
  922. EXPORT_SYMBOL(drm_legacy_addbufs_pci);
  923. static int drm_legacy_addbufs_sg(struct drm_device *dev,
  924. struct drm_buf_desc *request)
  925. {
  926. struct drm_device_dma *dma = dev->dma;
  927. struct drm_buf_entry *entry;
  928. struct drm_buf *buf;
  929. unsigned long offset;
  930. unsigned long agp_offset;
  931. int count;
  932. int order;
  933. int size;
  934. int alignment;
  935. int page_order;
  936. int total;
  937. int byte_count;
  938. int i;
  939. struct drm_buf **temp_buflist;
  940. if (!drm_core_check_feature(dev, DRIVER_SG))
  941. return -EINVAL;
  942. if (!dma)
  943. return -EINVAL;
  944. if (!capable(CAP_SYS_ADMIN))
  945. return -EPERM;
  946. count = request->count;
  947. order = order_base_2(request->size);
  948. size = 1 << order;
  949. alignment = (request->flags & _DRM_PAGE_ALIGN)
  950. ? PAGE_ALIGN(size) : size;
  951. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  952. total = PAGE_SIZE << page_order;
  953. byte_count = 0;
  954. agp_offset = request->agp_start;
  955. DRM_DEBUG("count: %d\n", count);
  956. DRM_DEBUG("order: %d\n", order);
  957. DRM_DEBUG("size: %d\n", size);
  958. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  959. DRM_DEBUG("alignment: %d\n", alignment);
  960. DRM_DEBUG("page_order: %d\n", page_order);
  961. DRM_DEBUG("total: %d\n", total);
  962. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  963. return -EINVAL;
  964. spin_lock(&dev->buf_lock);
  965. if (dev->buf_use) {
  966. spin_unlock(&dev->buf_lock);
  967. return -EBUSY;
  968. }
  969. atomic_inc(&dev->buf_alloc);
  970. spin_unlock(&dev->buf_lock);
  971. mutex_lock(&dev->struct_mutex);
  972. entry = &dma->bufs[order];
  973. if (entry->buf_count) {
  974. mutex_unlock(&dev->struct_mutex);
  975. atomic_dec(&dev->buf_alloc);
  976. return -ENOMEM; /* May only call once for each order */
  977. }
  978. if (count < 0 || count > 4096) {
  979. mutex_unlock(&dev->struct_mutex);
  980. atomic_dec(&dev->buf_alloc);
  981. return -EINVAL;
  982. }
  983. entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
  984. if (!entry->buflist) {
  985. mutex_unlock(&dev->struct_mutex);
  986. atomic_dec(&dev->buf_alloc);
  987. return -ENOMEM;
  988. }
  989. entry->buf_size = size;
  990. entry->page_order = page_order;
  991. offset = 0;
  992. while (entry->buf_count < count) {
  993. buf = &entry->buflist[entry->buf_count];
  994. buf->idx = dma->buf_count + entry->buf_count;
  995. buf->total = alignment;
  996. buf->order = order;
  997. buf->used = 0;
  998. buf->offset = (dma->byte_count + offset);
  999. buf->bus_address = agp_offset + offset;
  1000. buf->address = (void *)(agp_offset + offset
  1001. + (unsigned long)dev->sg->virtual);
  1002. buf->next = NULL;
  1003. buf->waiting = 0;
  1004. buf->pending = 0;
  1005. buf->file_priv = NULL;
  1006. buf->dev_priv_size = dev->driver->dev_priv_size;
  1007. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  1008. if (!buf->dev_private) {
  1009. /* Set count correctly so we free the proper amount. */
  1010. entry->buf_count = count;
  1011. drm_cleanup_buf_error(dev, entry);
  1012. mutex_unlock(&dev->struct_mutex);
  1013. atomic_dec(&dev->buf_alloc);
  1014. return -ENOMEM;
  1015. }
  1016. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  1017. offset += alignment;
  1018. entry->buf_count++;
  1019. byte_count += PAGE_SIZE << page_order;
  1020. }
  1021. DRM_DEBUG("byte_count: %d\n", byte_count);
  1022. temp_buflist = krealloc(dma->buflist,
  1023. (dma->buf_count + entry->buf_count) *
  1024. sizeof(*dma->buflist), GFP_KERNEL);
  1025. if (!temp_buflist) {
  1026. /* Free the entry because it isn't valid */
  1027. drm_cleanup_buf_error(dev, entry);
  1028. mutex_unlock(&dev->struct_mutex);
  1029. atomic_dec(&dev->buf_alloc);
  1030. return -ENOMEM;
  1031. }
  1032. dma->buflist = temp_buflist;
  1033. for (i = 0; i < entry->buf_count; i++) {
  1034. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  1035. }
  1036. dma->buf_count += entry->buf_count;
  1037. dma->seg_count += entry->seg_count;
  1038. dma->page_count += byte_count >> PAGE_SHIFT;
  1039. dma->byte_count += byte_count;
  1040. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  1041. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  1042. mutex_unlock(&dev->struct_mutex);
  1043. request->count = entry->buf_count;
  1044. request->size = size;
  1045. dma->flags = _DRM_DMA_USE_SG;
  1046. atomic_dec(&dev->buf_alloc);
  1047. return 0;
  1048. }
  1049. /**
  1050. * Add buffers for DMA transfers (ioctl).
  1051. *
  1052. * \param inode device inode.
  1053. * \param file_priv DRM file private.
  1054. * \param cmd command.
  1055. * \param arg pointer to a struct drm_buf_desc request.
  1056. * \return zero on success or a negative number on failure.
  1057. *
  1058. * According with the memory type specified in drm_buf_desc::flags and the
  1059. * build options, it dispatches the call either to addbufs_agp(),
  1060. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1061. * PCI memory respectively.
  1062. */
  1063. int drm_legacy_addbufs(struct drm_device *dev, void *data,
  1064. struct drm_file *file_priv)
  1065. {
  1066. struct drm_buf_desc *request = data;
  1067. int ret;
  1068. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1069. return -EINVAL;
  1070. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1071. return -EINVAL;
  1072. #if IS_ENABLED(CONFIG_AGP)
  1073. if (request->flags & _DRM_AGP_BUFFER)
  1074. ret = drm_legacy_addbufs_agp(dev, request);
  1075. else
  1076. #endif
  1077. if (request->flags & _DRM_SG_BUFFER)
  1078. ret = drm_legacy_addbufs_sg(dev, request);
  1079. else if (request->flags & _DRM_FB_BUFFER)
  1080. ret = -EINVAL;
  1081. else
  1082. ret = drm_legacy_addbufs_pci(dev, request);
  1083. return ret;
  1084. }
  1085. /**
  1086. * Get information about the buffer mappings.
  1087. *
  1088. * This was originally mean for debugging purposes, or by a sophisticated
  1089. * client library to determine how best to use the available buffers (e.g.,
  1090. * large buffers can be used for image transfer).
  1091. *
  1092. * \param inode device inode.
  1093. * \param file_priv DRM file private.
  1094. * \param cmd command.
  1095. * \param arg pointer to a drm_buf_info structure.
  1096. * \return zero on success or a negative number on failure.
  1097. *
  1098. * Increments drm_device::buf_use while holding the drm_device::buf_lock
  1099. * lock, preventing of allocating more buffers after this call. Information
  1100. * about each requested buffer is then copied into user space.
  1101. */
  1102. int drm_legacy_infobufs(struct drm_device *dev, void *data,
  1103. struct drm_file *file_priv)
  1104. {
  1105. struct drm_device_dma *dma = dev->dma;
  1106. struct drm_buf_info *request = data;
  1107. int i;
  1108. int count;
  1109. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1110. return -EINVAL;
  1111. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1112. return -EINVAL;
  1113. if (!dma)
  1114. return -EINVAL;
  1115. spin_lock(&dev->buf_lock);
  1116. if (atomic_read(&dev->buf_alloc)) {
  1117. spin_unlock(&dev->buf_lock);
  1118. return -EBUSY;
  1119. }
  1120. ++dev->buf_use; /* Can't allocate more after this call */
  1121. spin_unlock(&dev->buf_lock);
  1122. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1123. if (dma->bufs[i].buf_count)
  1124. ++count;
  1125. }
  1126. DRM_DEBUG("count = %d\n", count);
  1127. if (request->count >= count) {
  1128. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1129. if (dma->bufs[i].buf_count) {
  1130. struct drm_buf_desc __user *to =
  1131. &request->list[count];
  1132. struct drm_buf_entry *from = &dma->bufs[i];
  1133. if (copy_to_user(&to->count,
  1134. &from->buf_count,
  1135. sizeof(from->buf_count)) ||
  1136. copy_to_user(&to->size,
  1137. &from->buf_size,
  1138. sizeof(from->buf_size)) ||
  1139. copy_to_user(&to->low_mark,
  1140. &from->low_mark,
  1141. sizeof(from->low_mark)) ||
  1142. copy_to_user(&to->high_mark,
  1143. &from->high_mark,
  1144. sizeof(from->high_mark)))
  1145. return -EFAULT;
  1146. DRM_DEBUG("%d %d %d %d %d\n",
  1147. i,
  1148. dma->bufs[i].buf_count,
  1149. dma->bufs[i].buf_size,
  1150. dma->bufs[i].low_mark,
  1151. dma->bufs[i].high_mark);
  1152. ++count;
  1153. }
  1154. }
  1155. }
  1156. request->count = count;
  1157. return 0;
  1158. }
  1159. /**
  1160. * Specifies a low and high water mark for buffer allocation
  1161. *
  1162. * \param inode device inode.
  1163. * \param file_priv DRM file private.
  1164. * \param cmd command.
  1165. * \param arg a pointer to a drm_buf_desc structure.
  1166. * \return zero on success or a negative number on failure.
  1167. *
  1168. * Verifies that the size order is bounded between the admissible orders and
  1169. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1170. *
  1171. * \note This ioctl is deprecated and mostly never used.
  1172. */
  1173. int drm_legacy_markbufs(struct drm_device *dev, void *data,
  1174. struct drm_file *file_priv)
  1175. {
  1176. struct drm_device_dma *dma = dev->dma;
  1177. struct drm_buf_desc *request = data;
  1178. int order;
  1179. struct drm_buf_entry *entry;
  1180. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1181. return -EINVAL;
  1182. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1183. return -EINVAL;
  1184. if (!dma)
  1185. return -EINVAL;
  1186. DRM_DEBUG("%d, %d, %d\n",
  1187. request->size, request->low_mark, request->high_mark);
  1188. order = order_base_2(request->size);
  1189. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1190. return -EINVAL;
  1191. entry = &dma->bufs[order];
  1192. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1193. return -EINVAL;
  1194. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1195. return -EINVAL;
  1196. entry->low_mark = request->low_mark;
  1197. entry->high_mark = request->high_mark;
  1198. return 0;
  1199. }
  1200. /**
  1201. * Unreserve the buffers in list, previously reserved using drmDMA.
  1202. *
  1203. * \param inode device inode.
  1204. * \param file_priv DRM file private.
  1205. * \param cmd command.
  1206. * \param arg pointer to a drm_buf_free structure.
  1207. * \return zero on success or a negative number on failure.
  1208. *
  1209. * Calls free_buffer() for each used buffer.
  1210. * This function is primarily used for debugging.
  1211. */
  1212. int drm_legacy_freebufs(struct drm_device *dev, void *data,
  1213. struct drm_file *file_priv)
  1214. {
  1215. struct drm_device_dma *dma = dev->dma;
  1216. struct drm_buf_free *request = data;
  1217. int i;
  1218. int idx;
  1219. struct drm_buf *buf;
  1220. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1221. return -EINVAL;
  1222. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1223. return -EINVAL;
  1224. if (!dma)
  1225. return -EINVAL;
  1226. DRM_DEBUG("%d\n", request->count);
  1227. for (i = 0; i < request->count; i++) {
  1228. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1229. return -EFAULT;
  1230. if (idx < 0 || idx >= dma->buf_count) {
  1231. DRM_ERROR("Index %d (of %d max)\n",
  1232. idx, dma->buf_count - 1);
  1233. return -EINVAL;
  1234. }
  1235. buf = dma->buflist[idx];
  1236. if (buf->file_priv != file_priv) {
  1237. DRM_ERROR("Process %d freeing buffer not owned\n",
  1238. task_pid_nr(current));
  1239. return -EINVAL;
  1240. }
  1241. drm_legacy_free_buffer(dev, buf);
  1242. }
  1243. return 0;
  1244. }
  1245. /**
  1246. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1247. *
  1248. * \param inode device inode.
  1249. * \param file_priv DRM file private.
  1250. * \param cmd command.
  1251. * \param arg pointer to a drm_buf_map structure.
  1252. * \return zero on success or a negative number on failure.
  1253. *
  1254. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1255. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1256. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1257. * drm_mmap_dma().
  1258. */
  1259. int drm_legacy_mapbufs(struct drm_device *dev, void *data,
  1260. struct drm_file *file_priv)
  1261. {
  1262. struct drm_device_dma *dma = dev->dma;
  1263. int retcode = 0;
  1264. const int zero = 0;
  1265. unsigned long virtual;
  1266. unsigned long address;
  1267. struct drm_buf_map *request = data;
  1268. int i;
  1269. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1270. return -EINVAL;
  1271. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1272. return -EINVAL;
  1273. if (!dma)
  1274. return -EINVAL;
  1275. spin_lock(&dev->buf_lock);
  1276. if (atomic_read(&dev->buf_alloc)) {
  1277. spin_unlock(&dev->buf_lock);
  1278. return -EBUSY;
  1279. }
  1280. dev->buf_use++; /* Can't allocate more after this call */
  1281. spin_unlock(&dev->buf_lock);
  1282. if (request->count >= dma->buf_count) {
  1283. if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
  1284. || (drm_core_check_feature(dev, DRIVER_SG)
  1285. && (dma->flags & _DRM_DMA_USE_SG))) {
  1286. struct drm_local_map *map = dev->agp_buffer_map;
  1287. unsigned long token = dev->agp_buffer_token;
  1288. if (!map) {
  1289. retcode = -EINVAL;
  1290. goto done;
  1291. }
  1292. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1293. PROT_READ | PROT_WRITE,
  1294. MAP_SHARED,
  1295. token);
  1296. } else {
  1297. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1298. PROT_READ | PROT_WRITE,
  1299. MAP_SHARED, 0);
  1300. }
  1301. if (virtual > -1024UL) {
  1302. /* Real error */
  1303. retcode = (signed long)virtual;
  1304. goto done;
  1305. }
  1306. request->virtual = (void __user *)virtual;
  1307. for (i = 0; i < dma->buf_count; i++) {
  1308. if (copy_to_user(&request->list[i].idx,
  1309. &dma->buflist[i]->idx,
  1310. sizeof(request->list[0].idx))) {
  1311. retcode = -EFAULT;
  1312. goto done;
  1313. }
  1314. if (copy_to_user(&request->list[i].total,
  1315. &dma->buflist[i]->total,
  1316. sizeof(request->list[0].total))) {
  1317. retcode = -EFAULT;
  1318. goto done;
  1319. }
  1320. if (copy_to_user(&request->list[i].used,
  1321. &zero, sizeof(zero))) {
  1322. retcode = -EFAULT;
  1323. goto done;
  1324. }
  1325. address = virtual + dma->buflist[i]->offset; /* *** */
  1326. if (copy_to_user(&request->list[i].address,
  1327. &address, sizeof(address))) {
  1328. retcode = -EFAULT;
  1329. goto done;
  1330. }
  1331. }
  1332. }
  1333. done:
  1334. request->count = dma->buf_count;
  1335. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1336. return retcode;
  1337. }
  1338. int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
  1339. struct drm_file *file_priv)
  1340. {
  1341. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1342. return -EINVAL;
  1343. if (dev->driver->dma_ioctl)
  1344. return dev->driver->dma_ioctl(dev, data, file_priv);
  1345. else
  1346. return -EINVAL;
  1347. }
  1348. struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
  1349. {
  1350. struct drm_map_list *entry;
  1351. list_for_each_entry(entry, &dev->maplist, head) {
  1352. if (entry->map && entry->map->type == _DRM_SHM &&
  1353. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1354. return entry->map;
  1355. }
  1356. }
  1357. return NULL;
  1358. }
  1359. EXPORT_SYMBOL(drm_legacy_getsarea);