drm_bufs.c 44 KB

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