drm_bufs.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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->primary->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->primary->master->lock.hw_lock != NULL) {
  229. vfree(map->handle);
  230. kfree(map);
  231. return -EBUSY;
  232. }
  233. dev->sigdata.lock = dev->primary->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->primary->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. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  367. map->flags, &maplist);
  368. if (err)
  369. return err;
  370. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  371. map->handle = (void *)(unsigned long)maplist->user_token;
  372. /*
  373. * It appears that there are no users of this value whatsoever --
  374. * drmAddMap just discards it. Let's not encourage its use.
  375. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  376. * it's not a real mtrr index anymore.)
  377. */
  378. map->mtrr = -1;
  379. return 0;
  380. }
  381. /**
  382. * Remove a map private from list and deallocate resources if the mapping
  383. * isn't in use.
  384. *
  385. * Searches the map on drm_device::maplist, removes it from the list, see if
  386. * its being used, and free any associate resource (such as MTRR's) if it's not
  387. * being on use.
  388. *
  389. * \sa drm_legacy_addmap
  390. */
  391. int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  392. {
  393. struct drm_map_list *r_list = NULL, *list_t;
  394. drm_dma_handle_t dmah;
  395. int found = 0;
  396. struct drm_master *master;
  397. /* Find the list entry for the map and remove it */
  398. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  399. if (r_list->map == map) {
  400. master = r_list->master;
  401. list_del(&r_list->head);
  402. drm_ht_remove_key(&dev->map_hash,
  403. r_list->user_token >> PAGE_SHIFT);
  404. kfree(r_list);
  405. found = 1;
  406. break;
  407. }
  408. }
  409. if (!found)
  410. return -EINVAL;
  411. switch (map->type) {
  412. case _DRM_REGISTERS:
  413. iounmap(map->handle);
  414. /* FALLTHROUGH */
  415. case _DRM_FRAME_BUFFER:
  416. arch_phys_wc_del(map->mtrr);
  417. break;
  418. case _DRM_SHM:
  419. vfree(map->handle);
  420. if (master) {
  421. if (dev->sigdata.lock == master->lock.hw_lock)
  422. dev->sigdata.lock = NULL;
  423. master->lock.hw_lock = NULL; /* SHM removed */
  424. master->lock.file_priv = NULL;
  425. wake_up_interruptible_all(&master->lock.lock_queue);
  426. }
  427. break;
  428. case _DRM_AGP:
  429. case _DRM_SCATTER_GATHER:
  430. break;
  431. case _DRM_CONSISTENT:
  432. dmah.vaddr = map->handle;
  433. dmah.busaddr = map->offset;
  434. dmah.size = map->size;
  435. __drm_legacy_pci_free(dev, &dmah);
  436. break;
  437. }
  438. kfree(map);
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(drm_legacy_rmmap_locked);
  442. int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
  443. {
  444. int ret;
  445. mutex_lock(&dev->struct_mutex);
  446. ret = drm_legacy_rmmap_locked(dev, map);
  447. mutex_unlock(&dev->struct_mutex);
  448. return ret;
  449. }
  450. EXPORT_SYMBOL(drm_legacy_rmmap);
  451. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  452. * the last close of the device, and this is necessary for cleanup when things
  453. * exit uncleanly. Therefore, having userland manually remove mappings seems
  454. * like a pointless exercise since they're going away anyway.
  455. *
  456. * One use case might be after addmap is allowed for normal users for SHM and
  457. * gets used by drivers that the server doesn't need to care about. This seems
  458. * unlikely.
  459. *
  460. * \param inode device inode.
  461. * \param file_priv DRM file private.
  462. * \param cmd command.
  463. * \param arg pointer to a struct drm_map structure.
  464. * \return zero on success or a negative value on error.
  465. */
  466. int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
  467. struct drm_file *file_priv)
  468. {
  469. struct drm_map *request = data;
  470. struct drm_local_map *map = NULL;
  471. struct drm_map_list *r_list;
  472. int ret;
  473. mutex_lock(&dev->struct_mutex);
  474. list_for_each_entry(r_list, &dev->maplist, head) {
  475. if (r_list->map &&
  476. r_list->user_token == (unsigned long)request->handle &&
  477. r_list->map->flags & _DRM_REMOVABLE) {
  478. map = r_list->map;
  479. break;
  480. }
  481. }
  482. /* List has wrapped around to the head pointer, or its empty we didn't
  483. * find anything.
  484. */
  485. if (list_empty(&dev->maplist) || !map) {
  486. mutex_unlock(&dev->struct_mutex);
  487. return -EINVAL;
  488. }
  489. /* Register and framebuffer maps are permanent */
  490. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  491. mutex_unlock(&dev->struct_mutex);
  492. return 0;
  493. }
  494. ret = drm_legacy_rmmap_locked(dev, map);
  495. mutex_unlock(&dev->struct_mutex);
  496. return ret;
  497. }
  498. /**
  499. * Cleanup after an error on one of the addbufs() functions.
  500. *
  501. * \param dev DRM device.
  502. * \param entry buffer entry where the error occurred.
  503. *
  504. * Frees any pages and buffers associated with the given entry.
  505. */
  506. static void drm_cleanup_buf_error(struct drm_device * dev,
  507. struct drm_buf_entry * entry)
  508. {
  509. int i;
  510. if (entry->seg_count) {
  511. for (i = 0; i < entry->seg_count; i++) {
  512. if (entry->seglist[i]) {
  513. drm_pci_free(dev, entry->seglist[i]);
  514. }
  515. }
  516. kfree(entry->seglist);
  517. entry->seg_count = 0;
  518. }
  519. if (entry->buf_count) {
  520. for (i = 0; i < entry->buf_count; i++) {
  521. kfree(entry->buflist[i].dev_private);
  522. }
  523. kfree(entry->buflist);
  524. entry->buf_count = 0;
  525. }
  526. }
  527. #if __OS_HAS_AGP
  528. /**
  529. * Add AGP buffers for DMA transfers.
  530. *
  531. * \param dev struct drm_device to which the buffers are to be added.
  532. * \param request pointer to a struct drm_buf_desc describing the request.
  533. * \return zero on success or a negative number on failure.
  534. *
  535. * After some sanity checks creates a drm_buf structure for each buffer and
  536. * reallocates the buffer list of the same size order to accommodate the new
  537. * buffers.
  538. */
  539. int drm_legacy_addbufs_agp(struct drm_device *dev,
  540. struct drm_buf_desc *request)
  541. {
  542. struct drm_device_dma *dma = dev->dma;
  543. struct drm_buf_entry *entry;
  544. struct drm_agp_mem *agp_entry;
  545. struct drm_buf *buf;
  546. unsigned long offset;
  547. unsigned long agp_offset;
  548. int count;
  549. int order;
  550. int size;
  551. int alignment;
  552. int page_order;
  553. int total;
  554. int byte_count;
  555. int i, valid;
  556. struct drm_buf **temp_buflist;
  557. if (!dma)
  558. return -EINVAL;
  559. count = request->count;
  560. order = order_base_2(request->size);
  561. size = 1 << order;
  562. alignment = (request->flags & _DRM_PAGE_ALIGN)
  563. ? PAGE_ALIGN(size) : size;
  564. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  565. total = PAGE_SIZE << page_order;
  566. byte_count = 0;
  567. agp_offset = dev->agp->base + request->agp_start;
  568. DRM_DEBUG("count: %d\n", count);
  569. DRM_DEBUG("order: %d\n", order);
  570. DRM_DEBUG("size: %d\n", size);
  571. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  572. DRM_DEBUG("alignment: %d\n", alignment);
  573. DRM_DEBUG("page_order: %d\n", page_order);
  574. DRM_DEBUG("total: %d\n", total);
  575. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  576. return -EINVAL;
  577. /* Make sure buffers are located in AGP memory that we own */
  578. valid = 0;
  579. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  580. if ((agp_offset >= agp_entry->bound) &&
  581. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  582. valid = 1;
  583. break;
  584. }
  585. }
  586. if (!list_empty(&dev->agp->memory) && !valid) {
  587. DRM_DEBUG("zone invalid\n");
  588. return -EINVAL;
  589. }
  590. spin_lock(&dev->buf_lock);
  591. if (dev->buf_use) {
  592. spin_unlock(&dev->buf_lock);
  593. return -EBUSY;
  594. }
  595. atomic_inc(&dev->buf_alloc);
  596. spin_unlock(&dev->buf_lock);
  597. mutex_lock(&dev->struct_mutex);
  598. entry = &dma->bufs[order];
  599. if (entry->buf_count) {
  600. mutex_unlock(&dev->struct_mutex);
  601. atomic_dec(&dev->buf_alloc);
  602. return -ENOMEM; /* May only call once for each order */
  603. }
  604. if (count < 0 || count > 4096) {
  605. mutex_unlock(&dev->struct_mutex);
  606. atomic_dec(&dev->buf_alloc);
  607. return -EINVAL;
  608. }
  609. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  610. if (!entry->buflist) {
  611. mutex_unlock(&dev->struct_mutex);
  612. atomic_dec(&dev->buf_alloc);
  613. return -ENOMEM;
  614. }
  615. entry->buf_size = size;
  616. entry->page_order = page_order;
  617. offset = 0;
  618. while (entry->buf_count < count) {
  619. buf = &entry->buflist[entry->buf_count];
  620. buf->idx = dma->buf_count + entry->buf_count;
  621. buf->total = alignment;
  622. buf->order = order;
  623. buf->used = 0;
  624. buf->offset = (dma->byte_count + offset);
  625. buf->bus_address = agp_offset + offset;
  626. buf->address = (void *)(agp_offset + offset);
  627. buf->next = NULL;
  628. buf->waiting = 0;
  629. buf->pending = 0;
  630. buf->file_priv = NULL;
  631. buf->dev_priv_size = dev->driver->dev_priv_size;
  632. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  633. if (!buf->dev_private) {
  634. /* Set count correctly so we free the proper amount. */
  635. entry->buf_count = count;
  636. drm_cleanup_buf_error(dev, entry);
  637. mutex_unlock(&dev->struct_mutex);
  638. atomic_dec(&dev->buf_alloc);
  639. return -ENOMEM;
  640. }
  641. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  642. offset += alignment;
  643. entry->buf_count++;
  644. byte_count += PAGE_SIZE << page_order;
  645. }
  646. DRM_DEBUG("byte_count: %d\n", byte_count);
  647. temp_buflist = krealloc(dma->buflist,
  648. (dma->buf_count + entry->buf_count) *
  649. sizeof(*dma->buflist), GFP_KERNEL);
  650. if (!temp_buflist) {
  651. /* Free the entry because it isn't valid */
  652. drm_cleanup_buf_error(dev, entry);
  653. mutex_unlock(&dev->struct_mutex);
  654. atomic_dec(&dev->buf_alloc);
  655. return -ENOMEM;
  656. }
  657. dma->buflist = temp_buflist;
  658. for (i = 0; i < entry->buf_count; i++) {
  659. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  660. }
  661. dma->buf_count += entry->buf_count;
  662. dma->seg_count += entry->seg_count;
  663. dma->page_count += byte_count >> PAGE_SHIFT;
  664. dma->byte_count += byte_count;
  665. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  666. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  667. mutex_unlock(&dev->struct_mutex);
  668. request->count = entry->buf_count;
  669. request->size = size;
  670. dma->flags = _DRM_DMA_USE_AGP;
  671. atomic_dec(&dev->buf_alloc);
  672. return 0;
  673. }
  674. EXPORT_SYMBOL(drm_legacy_addbufs_agp);
  675. #endif /* __OS_HAS_AGP */
  676. int drm_legacy_addbufs_pci(struct drm_device *dev,
  677. struct drm_buf_desc *request)
  678. {
  679. struct drm_device_dma *dma = dev->dma;
  680. int count;
  681. int order;
  682. int size;
  683. int total;
  684. int page_order;
  685. struct drm_buf_entry *entry;
  686. drm_dma_handle_t *dmah;
  687. struct drm_buf *buf;
  688. int alignment;
  689. unsigned long offset;
  690. int i;
  691. int byte_count;
  692. int page_count;
  693. unsigned long *temp_pagelist;
  694. struct drm_buf **temp_buflist;
  695. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  696. return -EINVAL;
  697. if (!dma)
  698. return -EINVAL;
  699. if (!capable(CAP_SYS_ADMIN))
  700. return -EPERM;
  701. count = request->count;
  702. order = order_base_2(request->size);
  703. size = 1 << order;
  704. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  705. request->count, request->size, size, order);
  706. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  707. return -EINVAL;
  708. alignment = (request->flags & _DRM_PAGE_ALIGN)
  709. ? PAGE_ALIGN(size) : size;
  710. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  711. total = PAGE_SIZE << page_order;
  712. spin_lock(&dev->buf_lock);
  713. if (dev->buf_use) {
  714. spin_unlock(&dev->buf_lock);
  715. return -EBUSY;
  716. }
  717. atomic_inc(&dev->buf_alloc);
  718. spin_unlock(&dev->buf_lock);
  719. mutex_lock(&dev->struct_mutex);
  720. entry = &dma->bufs[order];
  721. if (entry->buf_count) {
  722. mutex_unlock(&dev->struct_mutex);
  723. atomic_dec(&dev->buf_alloc);
  724. return -ENOMEM; /* May only call once for each order */
  725. }
  726. if (count < 0 || count > 4096) {
  727. mutex_unlock(&dev->struct_mutex);
  728. atomic_dec(&dev->buf_alloc);
  729. return -EINVAL;
  730. }
  731. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  732. if (!entry->buflist) {
  733. mutex_unlock(&dev->struct_mutex);
  734. atomic_dec(&dev->buf_alloc);
  735. return -ENOMEM;
  736. }
  737. entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
  738. if (!entry->seglist) {
  739. kfree(entry->buflist);
  740. mutex_unlock(&dev->struct_mutex);
  741. atomic_dec(&dev->buf_alloc);
  742. return -ENOMEM;
  743. }
  744. /* Keep the original pagelist until we know all the allocations
  745. * have succeeded
  746. */
  747. temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
  748. sizeof(*dma->pagelist), GFP_KERNEL);
  749. if (!temp_pagelist) {
  750. kfree(entry->buflist);
  751. kfree(entry->seglist);
  752. mutex_unlock(&dev->struct_mutex);
  753. atomic_dec(&dev->buf_alloc);
  754. return -ENOMEM;
  755. }
  756. memcpy(temp_pagelist,
  757. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  758. DRM_DEBUG("pagelist: %d entries\n",
  759. dma->page_count + (count << page_order));
  760. entry->buf_size = size;
  761. entry->page_order = page_order;
  762. byte_count = 0;
  763. page_count = 0;
  764. while (entry->buf_count < count) {
  765. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  766. if (!dmah) {
  767. /* Set count correctly so we free the proper amount. */
  768. entry->buf_count = count;
  769. entry->seg_count = count;
  770. drm_cleanup_buf_error(dev, entry);
  771. kfree(temp_pagelist);
  772. mutex_unlock(&dev->struct_mutex);
  773. atomic_dec(&dev->buf_alloc);
  774. return -ENOMEM;
  775. }
  776. entry->seglist[entry->seg_count++] = dmah;
  777. for (i = 0; i < (1 << page_order); i++) {
  778. DRM_DEBUG("page %d @ 0x%08lx\n",
  779. dma->page_count + page_count,
  780. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  781. temp_pagelist[dma->page_count + page_count++]
  782. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  783. }
  784. for (offset = 0;
  785. offset + size <= total && entry->buf_count < count;
  786. offset += alignment, ++entry->buf_count) {
  787. buf = &entry->buflist[entry->buf_count];
  788. buf->idx = dma->buf_count + entry->buf_count;
  789. buf->total = alignment;
  790. buf->order = order;
  791. buf->used = 0;
  792. buf->offset = (dma->byte_count + byte_count + offset);
  793. buf->address = (void *)(dmah->vaddr + offset);
  794. buf->bus_address = dmah->busaddr + offset;
  795. buf->next = NULL;
  796. buf->waiting = 0;
  797. buf->pending = 0;
  798. buf->file_priv = NULL;
  799. buf->dev_priv_size = dev->driver->dev_priv_size;
  800. buf->dev_private = kzalloc(buf->dev_priv_size,
  801. GFP_KERNEL);
  802. if (!buf->dev_private) {
  803. /* Set count correctly so we free the proper amount. */
  804. entry->buf_count = count;
  805. entry->seg_count = count;
  806. drm_cleanup_buf_error(dev, entry);
  807. kfree(temp_pagelist);
  808. mutex_unlock(&dev->struct_mutex);
  809. atomic_dec(&dev->buf_alloc);
  810. return -ENOMEM;
  811. }
  812. DRM_DEBUG("buffer %d @ %p\n",
  813. entry->buf_count, buf->address);
  814. }
  815. byte_count += PAGE_SIZE << page_order;
  816. }
  817. temp_buflist = krealloc(dma->buflist,
  818. (dma->buf_count + entry->buf_count) *
  819. sizeof(*dma->buflist), GFP_KERNEL);
  820. if (!temp_buflist) {
  821. /* Free the entry because it isn't valid */
  822. drm_cleanup_buf_error(dev, entry);
  823. kfree(temp_pagelist);
  824. mutex_unlock(&dev->struct_mutex);
  825. atomic_dec(&dev->buf_alloc);
  826. return -ENOMEM;
  827. }
  828. dma->buflist = temp_buflist;
  829. for (i = 0; i < entry->buf_count; i++) {
  830. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  831. }
  832. /* No allocations failed, so now we can replace the original pagelist
  833. * with the new one.
  834. */
  835. if (dma->page_count) {
  836. kfree(dma->pagelist);
  837. }
  838. dma->pagelist = temp_pagelist;
  839. dma->buf_count += entry->buf_count;
  840. dma->seg_count += entry->seg_count;
  841. dma->page_count += entry->seg_count << page_order;
  842. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  843. mutex_unlock(&dev->struct_mutex);
  844. request->count = entry->buf_count;
  845. request->size = size;
  846. if (request->flags & _DRM_PCI_BUFFER_RO)
  847. dma->flags = _DRM_DMA_USE_PCI_RO;
  848. atomic_dec(&dev->buf_alloc);
  849. return 0;
  850. }
  851. EXPORT_SYMBOL(drm_legacy_addbufs_pci);
  852. static int drm_legacy_addbufs_sg(struct drm_device *dev,
  853. struct drm_buf_desc *request)
  854. {
  855. struct drm_device_dma *dma = dev->dma;
  856. struct drm_buf_entry *entry;
  857. struct drm_buf *buf;
  858. unsigned long offset;
  859. unsigned long agp_offset;
  860. int count;
  861. int order;
  862. int size;
  863. int alignment;
  864. int page_order;
  865. int total;
  866. int byte_count;
  867. int i;
  868. struct drm_buf **temp_buflist;
  869. if (!drm_core_check_feature(dev, DRIVER_SG))
  870. return -EINVAL;
  871. if (!dma)
  872. return -EINVAL;
  873. if (!capable(CAP_SYS_ADMIN))
  874. return -EPERM;
  875. count = request->count;
  876. order = order_base_2(request->size);
  877. size = 1 << order;
  878. alignment = (request->flags & _DRM_PAGE_ALIGN)
  879. ? PAGE_ALIGN(size) : size;
  880. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  881. total = PAGE_SIZE << page_order;
  882. byte_count = 0;
  883. agp_offset = request->agp_start;
  884. DRM_DEBUG("count: %d\n", count);
  885. DRM_DEBUG("order: %d\n", order);
  886. DRM_DEBUG("size: %d\n", size);
  887. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  888. DRM_DEBUG("alignment: %d\n", alignment);
  889. DRM_DEBUG("page_order: %d\n", page_order);
  890. DRM_DEBUG("total: %d\n", total);
  891. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  892. return -EINVAL;
  893. spin_lock(&dev->buf_lock);
  894. if (dev->buf_use) {
  895. spin_unlock(&dev->buf_lock);
  896. return -EBUSY;
  897. }
  898. atomic_inc(&dev->buf_alloc);
  899. spin_unlock(&dev->buf_lock);
  900. mutex_lock(&dev->struct_mutex);
  901. entry = &dma->bufs[order];
  902. if (entry->buf_count) {
  903. mutex_unlock(&dev->struct_mutex);
  904. atomic_dec(&dev->buf_alloc);
  905. return -ENOMEM; /* May only call once for each order */
  906. }
  907. if (count < 0 || count > 4096) {
  908. mutex_unlock(&dev->struct_mutex);
  909. atomic_dec(&dev->buf_alloc);
  910. return -EINVAL;
  911. }
  912. entry->buflist = kzalloc(count * sizeof(*entry->buflist),
  913. GFP_KERNEL);
  914. if (!entry->buflist) {
  915. mutex_unlock(&dev->struct_mutex);
  916. atomic_dec(&dev->buf_alloc);
  917. return -ENOMEM;
  918. }
  919. entry->buf_size = size;
  920. entry->page_order = page_order;
  921. offset = 0;
  922. while (entry->buf_count < count) {
  923. buf = &entry->buflist[entry->buf_count];
  924. buf->idx = dma->buf_count + entry->buf_count;
  925. buf->total = alignment;
  926. buf->order = order;
  927. buf->used = 0;
  928. buf->offset = (dma->byte_count + offset);
  929. buf->bus_address = agp_offset + offset;
  930. buf->address = (void *)(agp_offset + offset
  931. + (unsigned long)dev->sg->virtual);
  932. buf->next = NULL;
  933. buf->waiting = 0;
  934. buf->pending = 0;
  935. buf->file_priv = NULL;
  936. buf->dev_priv_size = dev->driver->dev_priv_size;
  937. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  938. if (!buf->dev_private) {
  939. /* Set count correctly so we free the proper amount. */
  940. entry->buf_count = count;
  941. drm_cleanup_buf_error(dev, entry);
  942. mutex_unlock(&dev->struct_mutex);
  943. atomic_dec(&dev->buf_alloc);
  944. return -ENOMEM;
  945. }
  946. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  947. offset += alignment;
  948. entry->buf_count++;
  949. byte_count += PAGE_SIZE << page_order;
  950. }
  951. DRM_DEBUG("byte_count: %d\n", byte_count);
  952. temp_buflist = krealloc(dma->buflist,
  953. (dma->buf_count + entry->buf_count) *
  954. sizeof(*dma->buflist), GFP_KERNEL);
  955. if (!temp_buflist) {
  956. /* Free the entry because it isn't valid */
  957. drm_cleanup_buf_error(dev, entry);
  958. mutex_unlock(&dev->struct_mutex);
  959. atomic_dec(&dev->buf_alloc);
  960. return -ENOMEM;
  961. }
  962. dma->buflist = temp_buflist;
  963. for (i = 0; i < entry->buf_count; i++) {
  964. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  965. }
  966. dma->buf_count += entry->buf_count;
  967. dma->seg_count += entry->seg_count;
  968. dma->page_count += byte_count >> PAGE_SHIFT;
  969. dma->byte_count += byte_count;
  970. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  971. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  972. mutex_unlock(&dev->struct_mutex);
  973. request->count = entry->buf_count;
  974. request->size = size;
  975. dma->flags = _DRM_DMA_USE_SG;
  976. atomic_dec(&dev->buf_alloc);
  977. return 0;
  978. }
  979. /**
  980. * Add buffers for DMA transfers (ioctl).
  981. *
  982. * \param inode device inode.
  983. * \param file_priv DRM file private.
  984. * \param cmd command.
  985. * \param arg pointer to a struct drm_buf_desc request.
  986. * \return zero on success or a negative number on failure.
  987. *
  988. * According with the memory type specified in drm_buf_desc::flags and the
  989. * build options, it dispatches the call either to addbufs_agp(),
  990. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  991. * PCI memory respectively.
  992. */
  993. int drm_legacy_addbufs(struct drm_device *dev, void *data,
  994. struct drm_file *file_priv)
  995. {
  996. struct drm_buf_desc *request = data;
  997. int ret;
  998. if (drm_core_check_feature(dev, DRIVER_MODESET))
  999. return -EINVAL;
  1000. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1001. return -EINVAL;
  1002. #if __OS_HAS_AGP
  1003. if (request->flags & _DRM_AGP_BUFFER)
  1004. ret = drm_legacy_addbufs_agp(dev, request);
  1005. else
  1006. #endif
  1007. if (request->flags & _DRM_SG_BUFFER)
  1008. ret = drm_legacy_addbufs_sg(dev, request);
  1009. else if (request->flags & _DRM_FB_BUFFER)
  1010. ret = -EINVAL;
  1011. else
  1012. ret = drm_legacy_addbufs_pci(dev, request);
  1013. return ret;
  1014. }
  1015. /**
  1016. * Get information about the buffer mappings.
  1017. *
  1018. * This was originally mean for debugging purposes, or by a sophisticated
  1019. * client library to determine how best to use the available buffers (e.g.,
  1020. * large buffers can be used for image transfer).
  1021. *
  1022. * \param inode device inode.
  1023. * \param file_priv DRM file private.
  1024. * \param cmd command.
  1025. * \param arg pointer to a drm_buf_info structure.
  1026. * \return zero on success or a negative number on failure.
  1027. *
  1028. * Increments drm_device::buf_use while holding the drm_device::buf_lock
  1029. * lock, preventing of allocating more buffers after this call. Information
  1030. * about each requested buffer is then copied into user space.
  1031. */
  1032. int drm_legacy_infobufs(struct drm_device *dev, void *data,
  1033. struct drm_file *file_priv)
  1034. {
  1035. struct drm_device_dma *dma = dev->dma;
  1036. struct drm_buf_info *request = data;
  1037. int i;
  1038. int count;
  1039. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1040. return -EINVAL;
  1041. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1042. return -EINVAL;
  1043. if (!dma)
  1044. return -EINVAL;
  1045. spin_lock(&dev->buf_lock);
  1046. if (atomic_read(&dev->buf_alloc)) {
  1047. spin_unlock(&dev->buf_lock);
  1048. return -EBUSY;
  1049. }
  1050. ++dev->buf_use; /* Can't allocate more after this call */
  1051. spin_unlock(&dev->buf_lock);
  1052. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1053. if (dma->bufs[i].buf_count)
  1054. ++count;
  1055. }
  1056. DRM_DEBUG("count = %d\n", count);
  1057. if (request->count >= count) {
  1058. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1059. if (dma->bufs[i].buf_count) {
  1060. struct drm_buf_desc __user *to =
  1061. &request->list[count];
  1062. struct drm_buf_entry *from = &dma->bufs[i];
  1063. if (copy_to_user(&to->count,
  1064. &from->buf_count,
  1065. sizeof(from->buf_count)) ||
  1066. copy_to_user(&to->size,
  1067. &from->buf_size,
  1068. sizeof(from->buf_size)) ||
  1069. copy_to_user(&to->low_mark,
  1070. &from->low_mark,
  1071. sizeof(from->low_mark)) ||
  1072. copy_to_user(&to->high_mark,
  1073. &from->high_mark,
  1074. sizeof(from->high_mark)))
  1075. return -EFAULT;
  1076. DRM_DEBUG("%d %d %d %d %d\n",
  1077. i,
  1078. dma->bufs[i].buf_count,
  1079. dma->bufs[i].buf_size,
  1080. dma->bufs[i].low_mark,
  1081. dma->bufs[i].high_mark);
  1082. ++count;
  1083. }
  1084. }
  1085. }
  1086. request->count = count;
  1087. return 0;
  1088. }
  1089. /**
  1090. * Specifies a low and high water mark for buffer allocation
  1091. *
  1092. * \param inode device inode.
  1093. * \param file_priv DRM file private.
  1094. * \param cmd command.
  1095. * \param arg a pointer to a drm_buf_desc structure.
  1096. * \return zero on success or a negative number on failure.
  1097. *
  1098. * Verifies that the size order is bounded between the admissible orders and
  1099. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1100. *
  1101. * \note This ioctl is deprecated and mostly never used.
  1102. */
  1103. int drm_legacy_markbufs(struct drm_device *dev, void *data,
  1104. struct drm_file *file_priv)
  1105. {
  1106. struct drm_device_dma *dma = dev->dma;
  1107. struct drm_buf_desc *request = data;
  1108. int order;
  1109. struct drm_buf_entry *entry;
  1110. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1111. return -EINVAL;
  1112. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1113. return -EINVAL;
  1114. if (!dma)
  1115. return -EINVAL;
  1116. DRM_DEBUG("%d, %d, %d\n",
  1117. request->size, request->low_mark, request->high_mark);
  1118. order = order_base_2(request->size);
  1119. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1120. return -EINVAL;
  1121. entry = &dma->bufs[order];
  1122. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1123. return -EINVAL;
  1124. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1125. return -EINVAL;
  1126. entry->low_mark = request->low_mark;
  1127. entry->high_mark = request->high_mark;
  1128. return 0;
  1129. }
  1130. /**
  1131. * Unreserve the buffers in list, previously reserved using drmDMA.
  1132. *
  1133. * \param inode device inode.
  1134. * \param file_priv DRM file private.
  1135. * \param cmd command.
  1136. * \param arg pointer to a drm_buf_free structure.
  1137. * \return zero on success or a negative number on failure.
  1138. *
  1139. * Calls free_buffer() for each used buffer.
  1140. * This function is primarily used for debugging.
  1141. */
  1142. int drm_legacy_freebufs(struct drm_device *dev, void *data,
  1143. struct drm_file *file_priv)
  1144. {
  1145. struct drm_device_dma *dma = dev->dma;
  1146. struct drm_buf_free *request = data;
  1147. int i;
  1148. int idx;
  1149. struct drm_buf *buf;
  1150. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1151. return -EINVAL;
  1152. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1153. return -EINVAL;
  1154. if (!dma)
  1155. return -EINVAL;
  1156. DRM_DEBUG("%d\n", request->count);
  1157. for (i = 0; i < request->count; i++) {
  1158. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1159. return -EFAULT;
  1160. if (idx < 0 || idx >= dma->buf_count) {
  1161. DRM_ERROR("Index %d (of %d max)\n",
  1162. idx, dma->buf_count - 1);
  1163. return -EINVAL;
  1164. }
  1165. buf = dma->buflist[idx];
  1166. if (buf->file_priv != file_priv) {
  1167. DRM_ERROR("Process %d freeing buffer not owned\n",
  1168. task_pid_nr(current));
  1169. return -EINVAL;
  1170. }
  1171. drm_legacy_free_buffer(dev, buf);
  1172. }
  1173. return 0;
  1174. }
  1175. /**
  1176. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1177. *
  1178. * \param inode device inode.
  1179. * \param file_priv DRM file private.
  1180. * \param cmd command.
  1181. * \param arg pointer to a drm_buf_map structure.
  1182. * \return zero on success or a negative number on failure.
  1183. *
  1184. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1185. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1186. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1187. * drm_mmap_dma().
  1188. */
  1189. int drm_legacy_mapbufs(struct drm_device *dev, void *data,
  1190. struct drm_file *file_priv)
  1191. {
  1192. struct drm_device_dma *dma = dev->dma;
  1193. int retcode = 0;
  1194. const int zero = 0;
  1195. unsigned long virtual;
  1196. unsigned long address;
  1197. struct drm_buf_map *request = data;
  1198. int i;
  1199. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1200. return -EINVAL;
  1201. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1202. return -EINVAL;
  1203. if (!dma)
  1204. return -EINVAL;
  1205. spin_lock(&dev->buf_lock);
  1206. if (atomic_read(&dev->buf_alloc)) {
  1207. spin_unlock(&dev->buf_lock);
  1208. return -EBUSY;
  1209. }
  1210. dev->buf_use++; /* Can't allocate more after this call */
  1211. spin_unlock(&dev->buf_lock);
  1212. if (request->count >= dma->buf_count) {
  1213. if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
  1214. || (drm_core_check_feature(dev, DRIVER_SG)
  1215. && (dma->flags & _DRM_DMA_USE_SG))) {
  1216. struct drm_local_map *map = dev->agp_buffer_map;
  1217. unsigned long token = dev->agp_buffer_token;
  1218. if (!map) {
  1219. retcode = -EINVAL;
  1220. goto done;
  1221. }
  1222. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1223. PROT_READ | PROT_WRITE,
  1224. MAP_SHARED,
  1225. token);
  1226. } else {
  1227. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1228. PROT_READ | PROT_WRITE,
  1229. MAP_SHARED, 0);
  1230. }
  1231. if (virtual > -1024UL) {
  1232. /* Real error */
  1233. retcode = (signed long)virtual;
  1234. goto done;
  1235. }
  1236. request->virtual = (void __user *)virtual;
  1237. for (i = 0; i < dma->buf_count; i++) {
  1238. if (copy_to_user(&request->list[i].idx,
  1239. &dma->buflist[i]->idx,
  1240. sizeof(request->list[0].idx))) {
  1241. retcode = -EFAULT;
  1242. goto done;
  1243. }
  1244. if (copy_to_user(&request->list[i].total,
  1245. &dma->buflist[i]->total,
  1246. sizeof(request->list[0].total))) {
  1247. retcode = -EFAULT;
  1248. goto done;
  1249. }
  1250. if (copy_to_user(&request->list[i].used,
  1251. &zero, sizeof(zero))) {
  1252. retcode = -EFAULT;
  1253. goto done;
  1254. }
  1255. address = virtual + dma->buflist[i]->offset; /* *** */
  1256. if (copy_to_user(&request->list[i].address,
  1257. &address, sizeof(address))) {
  1258. retcode = -EFAULT;
  1259. goto done;
  1260. }
  1261. }
  1262. }
  1263. done:
  1264. request->count = dma->buf_count;
  1265. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1266. return retcode;
  1267. }
  1268. int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
  1269. struct drm_file *file_priv)
  1270. {
  1271. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1272. return -EINVAL;
  1273. if (dev->driver->dma_ioctl)
  1274. return dev->driver->dma_ioctl(dev, data, file_priv);
  1275. else
  1276. return -EINVAL;
  1277. }
  1278. struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
  1279. {
  1280. struct drm_map_list *entry;
  1281. list_for_each_entry(entry, &dev->maplist, head) {
  1282. if (entry->map && entry->map->type == _DRM_SHM &&
  1283. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1284. return entry->map;
  1285. }
  1286. }
  1287. return NULL;
  1288. }
  1289. EXPORT_SYMBOL(drm_legacy_getsarea);