drm_bufs.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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(drm_device_t *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(drm_device_t *dev, unsigned int resource)
  42. {
  43. return pci_resource_len(dev->pdev, resource);
  44. }
  45. EXPORT_SYMBOL(drm_get_resource_len);
  46. static drm_map_list_t *drm_find_matching_map(drm_device_t *dev,
  47. drm_local_map_t *map)
  48. {
  49. struct list_head *list;
  50. list_for_each(list, &dev->maplist->head) {
  51. drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
  52. if (entry->map && map->type == entry->map->type &&
  53. ((entry->map->offset == map->offset) ||
  54. (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
  55. return entry;
  56. }
  57. }
  58. return NULL;
  59. }
  60. static int drm_map_handle(drm_device_t *dev, drm_hash_item_t *hash,
  61. unsigned long user_token, int hashed_handle)
  62. {
  63. int use_hashed_handle;
  64. #if (BITS_PER_LONG == 64)
  65. use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
  66. #elif (BITS_PER_LONG == 32)
  67. use_hashed_handle = hashed_handle;
  68. #else
  69. #error Unsupported long size. Neither 64 nor 32 bits.
  70. #endif
  71. if (!use_hashed_handle) {
  72. int ret;
  73. hash->key = user_token >> PAGE_SHIFT;
  74. ret = drm_ht_insert_item(&dev->map_hash, hash);
  75. if (ret != -EINVAL)
  76. return ret;
  77. }
  78. return drm_ht_just_insert_please(&dev->map_hash, hash,
  79. user_token, 32 - PAGE_SHIFT - 3,
  80. 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
  81. }
  82. /**
  83. * Ioctl to specify a range of memory that is available for mapping by a non-root process.
  84. *
  85. * \param inode device inode.
  86. * \param filp file pointer.
  87. * \param cmd command.
  88. * \param arg pointer to a drm_map structure.
  89. * \return zero on success or a negative value on error.
  90. *
  91. * Adjusts the memory offset to its absolute value according to the mapping
  92. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  93. * applicable and if supported by the kernel.
  94. */
  95. static int drm_addmap_core(drm_device_t * dev, unsigned int offset,
  96. unsigned int size, drm_map_type_t type,
  97. drm_map_flags_t flags, drm_map_list_t ** maplist)
  98. {
  99. drm_map_t *map;
  100. drm_map_list_t *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. drm_agp_mem_t *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. for (entry = dev->agp->memory; entry; entry = entry->next) {
  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 (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->head);
  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(drm_device_t * dev, unsigned int offset,
  289. unsigned int size, drm_map_type_t type,
  290. drm_map_flags_t flags, drm_local_map_t ** map_ptr)
  291. {
  292. drm_map_list_t *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. drm_file_t *priv = filp->private_data;
  304. drm_device_t *dev = priv->head->dev;
  305. drm_map_t map;
  306. drm_map_list_t *maplist;
  307. drm_map_t __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(drm_map_t)))
  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 drm_map_t 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(drm_device_t *dev, drm_local_map_t *map)
  344. {
  345. struct list_head *list;
  346. drm_map_list_t *r_list = NULL;
  347. drm_dma_handle_t dmah;
  348. /* Find the list entry for the map and remove it */
  349. list_for_each(list, &dev->maplist->head) {
  350. r_list = list_entry(list, drm_map_list_t, head);
  351. if (r_list->map == map) {
  352. list_del(list);
  353. drm_ht_remove_key(&dev->map_hash,
  354. r_list->user_token >> PAGE_SHIFT);
  355. drm_free(list, sizeof(*list), DRM_MEM_MAPS);
  356. break;
  357. }
  358. }
  359. /* List has wrapped around to the head pointer, or it's empty and we
  360. * didn't find anything.
  361. */
  362. if (list == (&dev->maplist->head)) {
  363. return -EINVAL;
  364. }
  365. switch (map->type) {
  366. case _DRM_REGISTERS:
  367. iounmap(map->handle);
  368. /* FALLTHROUGH */
  369. case _DRM_FRAME_BUFFER:
  370. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  371. int retcode;
  372. retcode = mtrr_del(map->mtrr, map->offset, map->size);
  373. DRM_DEBUG("mtrr_del=%d\n", retcode);
  374. }
  375. break;
  376. case _DRM_SHM:
  377. vfree(map->handle);
  378. break;
  379. case _DRM_AGP:
  380. case _DRM_SCATTER_GATHER:
  381. break;
  382. case _DRM_CONSISTENT:
  383. dmah.vaddr = map->handle;
  384. dmah.busaddr = map->offset;
  385. dmah.size = map->size;
  386. __drm_pci_free(dev, &dmah);
  387. break;
  388. }
  389. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  390. return 0;
  391. }
  392. int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
  393. {
  394. int ret;
  395. mutex_lock(&dev->struct_mutex);
  396. ret = drm_rmmap_locked(dev, map);
  397. mutex_unlock(&dev->struct_mutex);
  398. return ret;
  399. }
  400. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  401. * the last close of the device, and this is necessary for cleanup when things
  402. * exit uncleanly. Therefore, having userland manually remove mappings seems
  403. * like a pointless exercise since they're going away anyway.
  404. *
  405. * One use case might be after addmap is allowed for normal users for SHM and
  406. * gets used by drivers that the server doesn't need to care about. This seems
  407. * unlikely.
  408. */
  409. int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
  410. unsigned int cmd, unsigned long arg)
  411. {
  412. drm_file_t *priv = filp->private_data;
  413. drm_device_t *dev = priv->head->dev;
  414. drm_map_t request;
  415. drm_local_map_t *map = NULL;
  416. struct list_head *list;
  417. int ret;
  418. if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
  419. return -EFAULT;
  420. }
  421. mutex_lock(&dev->struct_mutex);
  422. list_for_each(list, &dev->maplist->head) {
  423. drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
  424. if (r_list->map &&
  425. r_list->user_token == (unsigned long)request.handle &&
  426. r_list->map->flags & _DRM_REMOVABLE) {
  427. map = r_list->map;
  428. break;
  429. }
  430. }
  431. /* List has wrapped around to the head pointer, or its empty we didn't
  432. * find anything.
  433. */
  434. if (list == (&dev->maplist->head)) {
  435. mutex_unlock(&dev->struct_mutex);
  436. return -EINVAL;
  437. }
  438. if (!map) {
  439. mutex_unlock(&dev->struct_mutex);
  440. return -EINVAL;
  441. }
  442. /* Register and framebuffer maps are permanent */
  443. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  444. mutex_unlock(&dev->struct_mutex);
  445. return 0;
  446. }
  447. ret = drm_rmmap_locked(dev, map);
  448. mutex_unlock(&dev->struct_mutex);
  449. return ret;
  450. }
  451. /**
  452. * Cleanup after an error on one of the addbufs() functions.
  453. *
  454. * \param dev DRM device.
  455. * \param entry buffer entry where the error occurred.
  456. *
  457. * Frees any pages and buffers associated with the given entry.
  458. */
  459. static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
  460. {
  461. int i;
  462. if (entry->seg_count) {
  463. for (i = 0; i < entry->seg_count; i++) {
  464. if (entry->seglist[i]) {
  465. drm_pci_free(dev, entry->seglist[i]);
  466. }
  467. }
  468. drm_free(entry->seglist,
  469. entry->seg_count *
  470. sizeof(*entry->seglist), DRM_MEM_SEGS);
  471. entry->seg_count = 0;
  472. }
  473. if (entry->buf_count) {
  474. for (i = 0; i < entry->buf_count; i++) {
  475. if (entry->buflist[i].dev_private) {
  476. drm_free(entry->buflist[i].dev_private,
  477. entry->buflist[i].dev_priv_size,
  478. DRM_MEM_BUFS);
  479. }
  480. }
  481. drm_free(entry->buflist,
  482. entry->buf_count *
  483. sizeof(*entry->buflist), DRM_MEM_BUFS);
  484. entry->buf_count = 0;
  485. }
  486. }
  487. #if __OS_HAS_AGP
  488. /**
  489. * Add AGP buffers for DMA transfers.
  490. *
  491. * \param dev drm_device_t to which the buffers are to be added.
  492. * \param request pointer to a drm_buf_desc_t describing the request.
  493. * \return zero on success or a negative number on failure.
  494. *
  495. * After some sanity checks creates a drm_buf structure for each buffer and
  496. * reallocates the buffer list of the same size order to accommodate the new
  497. * buffers.
  498. */
  499. int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
  500. {
  501. drm_device_dma_t *dma = dev->dma;
  502. drm_buf_entry_t *entry;
  503. drm_agp_mem_t *agp_entry;
  504. drm_buf_t *buf;
  505. unsigned long offset;
  506. unsigned long agp_offset;
  507. int count;
  508. int order;
  509. int size;
  510. int alignment;
  511. int page_order;
  512. int total;
  513. int byte_count;
  514. int i, valid;
  515. drm_buf_t **temp_buflist;
  516. if (!dma)
  517. return -EINVAL;
  518. count = request->count;
  519. order = drm_order(request->size);
  520. size = 1 << order;
  521. alignment = (request->flags & _DRM_PAGE_ALIGN)
  522. ? PAGE_ALIGN(size) : size;
  523. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  524. total = PAGE_SIZE << page_order;
  525. byte_count = 0;
  526. agp_offset = dev->agp->base + request->agp_start;
  527. DRM_DEBUG("count: %d\n", count);
  528. DRM_DEBUG("order: %d\n", order);
  529. DRM_DEBUG("size: %d\n", size);
  530. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  531. DRM_DEBUG("alignment: %d\n", alignment);
  532. DRM_DEBUG("page_order: %d\n", page_order);
  533. DRM_DEBUG("total: %d\n", total);
  534. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  535. return -EINVAL;
  536. if (dev->queue_count)
  537. return -EBUSY; /* Not while in use */
  538. /* Make sure buffers are located in AGP memory that we own */
  539. valid = 0;
  540. for (agp_entry = dev->agp->memory; agp_entry; agp_entry = agp_entry->next) {
  541. if ((agp_offset >= agp_entry->bound) &&
  542. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  543. valid = 1;
  544. break;
  545. }
  546. }
  547. if (dev->agp->memory && !valid) {
  548. DRM_DEBUG("zone invalid\n");
  549. return -EINVAL;
  550. }
  551. spin_lock(&dev->count_lock);
  552. if (dev->buf_use) {
  553. spin_unlock(&dev->count_lock);
  554. return -EBUSY;
  555. }
  556. atomic_inc(&dev->buf_alloc);
  557. spin_unlock(&dev->count_lock);
  558. mutex_lock(&dev->struct_mutex);
  559. entry = &dma->bufs[order];
  560. if (entry->buf_count) {
  561. mutex_unlock(&dev->struct_mutex);
  562. atomic_dec(&dev->buf_alloc);
  563. return -ENOMEM; /* May only call once for each order */
  564. }
  565. if (count < 0 || count > 4096) {
  566. mutex_unlock(&dev->struct_mutex);
  567. atomic_dec(&dev->buf_alloc);
  568. return -EINVAL;
  569. }
  570. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  571. DRM_MEM_BUFS);
  572. if (!entry->buflist) {
  573. mutex_unlock(&dev->struct_mutex);
  574. atomic_dec(&dev->buf_alloc);
  575. return -ENOMEM;
  576. }
  577. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  578. entry->buf_size = size;
  579. entry->page_order = page_order;
  580. offset = 0;
  581. while (entry->buf_count < count) {
  582. buf = &entry->buflist[entry->buf_count];
  583. buf->idx = dma->buf_count + entry->buf_count;
  584. buf->total = alignment;
  585. buf->order = order;
  586. buf->used = 0;
  587. buf->offset = (dma->byte_count + offset);
  588. buf->bus_address = agp_offset + offset;
  589. buf->address = (void *)(agp_offset + offset);
  590. buf->next = NULL;
  591. buf->waiting = 0;
  592. buf->pending = 0;
  593. init_waitqueue_head(&buf->dma_wait);
  594. buf->filp = NULL;
  595. buf->dev_priv_size = dev->driver->dev_priv_size;
  596. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  597. if (!buf->dev_private) {
  598. /* Set count correctly so we free the proper amount. */
  599. entry->buf_count = count;
  600. drm_cleanup_buf_error(dev, entry);
  601. mutex_unlock(&dev->struct_mutex);
  602. atomic_dec(&dev->buf_alloc);
  603. return -ENOMEM;
  604. }
  605. memset(buf->dev_private, 0, buf->dev_priv_size);
  606. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  607. offset += alignment;
  608. entry->buf_count++;
  609. byte_count += PAGE_SIZE << page_order;
  610. }
  611. DRM_DEBUG("byte_count: %d\n", byte_count);
  612. temp_buflist = drm_realloc(dma->buflist,
  613. dma->buf_count * sizeof(*dma->buflist),
  614. (dma->buf_count + entry->buf_count)
  615. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  616. if (!temp_buflist) {
  617. /* Free the entry because it isn't valid */
  618. drm_cleanup_buf_error(dev, entry);
  619. mutex_unlock(&dev->struct_mutex);
  620. atomic_dec(&dev->buf_alloc);
  621. return -ENOMEM;
  622. }
  623. dma->buflist = temp_buflist;
  624. for (i = 0; i < entry->buf_count; i++) {
  625. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  626. }
  627. dma->buf_count += entry->buf_count;
  628. dma->seg_count += entry->seg_count;
  629. dma->page_count += byte_count >> PAGE_SHIFT;
  630. dma->byte_count += byte_count;
  631. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  632. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  633. mutex_unlock(&dev->struct_mutex);
  634. request->count = entry->buf_count;
  635. request->size = size;
  636. dma->flags = _DRM_DMA_USE_AGP;
  637. atomic_dec(&dev->buf_alloc);
  638. return 0;
  639. }
  640. EXPORT_SYMBOL(drm_addbufs_agp);
  641. #endif /* __OS_HAS_AGP */
  642. int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
  643. {
  644. drm_device_dma_t *dma = dev->dma;
  645. int count;
  646. int order;
  647. int size;
  648. int total;
  649. int page_order;
  650. drm_buf_entry_t *entry;
  651. drm_dma_handle_t *dmah;
  652. drm_buf_t *buf;
  653. int alignment;
  654. unsigned long offset;
  655. int i;
  656. int byte_count;
  657. int page_count;
  658. unsigned long *temp_pagelist;
  659. drm_buf_t **temp_buflist;
  660. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  661. return -EINVAL;
  662. if (!dma)
  663. return -EINVAL;
  664. if (!capable(CAP_SYS_ADMIN))
  665. return -EPERM;
  666. count = request->count;
  667. order = drm_order(request->size);
  668. size = 1 << order;
  669. DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  670. request->count, request->size, size, order, dev->queue_count);
  671. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  672. return -EINVAL;
  673. if (dev->queue_count)
  674. return -EBUSY; /* Not while in use */
  675. alignment = (request->flags & _DRM_PAGE_ALIGN)
  676. ? PAGE_ALIGN(size) : size;
  677. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  678. total = PAGE_SIZE << page_order;
  679. spin_lock(&dev->count_lock);
  680. if (dev->buf_use) {
  681. spin_unlock(&dev->count_lock);
  682. return -EBUSY;
  683. }
  684. atomic_inc(&dev->buf_alloc);
  685. spin_unlock(&dev->count_lock);
  686. mutex_lock(&dev->struct_mutex);
  687. entry = &dma->bufs[order];
  688. if (entry->buf_count) {
  689. mutex_unlock(&dev->struct_mutex);
  690. atomic_dec(&dev->buf_alloc);
  691. return -ENOMEM; /* May only call once for each order */
  692. }
  693. if (count < 0 || count > 4096) {
  694. mutex_unlock(&dev->struct_mutex);
  695. atomic_dec(&dev->buf_alloc);
  696. return -EINVAL;
  697. }
  698. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  699. DRM_MEM_BUFS);
  700. if (!entry->buflist) {
  701. mutex_unlock(&dev->struct_mutex);
  702. atomic_dec(&dev->buf_alloc);
  703. return -ENOMEM;
  704. }
  705. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  706. entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
  707. DRM_MEM_SEGS);
  708. if (!entry->seglist) {
  709. drm_free(entry->buflist,
  710. count * sizeof(*entry->buflist), DRM_MEM_BUFS);
  711. mutex_unlock(&dev->struct_mutex);
  712. atomic_dec(&dev->buf_alloc);
  713. return -ENOMEM;
  714. }
  715. memset(entry->seglist, 0, count * sizeof(*entry->seglist));
  716. /* Keep the original pagelist until we know all the allocations
  717. * have succeeded
  718. */
  719. temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
  720. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  721. if (!temp_pagelist) {
  722. drm_free(entry->buflist,
  723. count * sizeof(*entry->buflist), DRM_MEM_BUFS);
  724. drm_free(entry->seglist,
  725. count * sizeof(*entry->seglist), DRM_MEM_SEGS);
  726. mutex_unlock(&dev->struct_mutex);
  727. atomic_dec(&dev->buf_alloc);
  728. return -ENOMEM;
  729. }
  730. memcpy(temp_pagelist,
  731. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  732. DRM_DEBUG("pagelist: %d entries\n",
  733. dma->page_count + (count << page_order));
  734. entry->buf_size = size;
  735. entry->page_order = page_order;
  736. byte_count = 0;
  737. page_count = 0;
  738. while (entry->buf_count < count) {
  739. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
  740. if (!dmah) {
  741. /* Set count correctly so we free the proper amount. */
  742. entry->buf_count = count;
  743. entry->seg_count = count;
  744. drm_cleanup_buf_error(dev, entry);
  745. drm_free(temp_pagelist,
  746. (dma->page_count + (count << page_order))
  747. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  748. mutex_unlock(&dev->struct_mutex);
  749. atomic_dec(&dev->buf_alloc);
  750. return -ENOMEM;
  751. }
  752. entry->seglist[entry->seg_count++] = dmah;
  753. for (i = 0; i < (1 << page_order); i++) {
  754. DRM_DEBUG("page %d @ 0x%08lx\n",
  755. dma->page_count + page_count,
  756. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  757. temp_pagelist[dma->page_count + page_count++]
  758. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  759. }
  760. for (offset = 0;
  761. offset + size <= total && entry->buf_count < count;
  762. offset += alignment, ++entry->buf_count) {
  763. buf = &entry->buflist[entry->buf_count];
  764. buf->idx = dma->buf_count + entry->buf_count;
  765. buf->total = alignment;
  766. buf->order = order;
  767. buf->used = 0;
  768. buf->offset = (dma->byte_count + byte_count + offset);
  769. buf->address = (void *)(dmah->vaddr + offset);
  770. buf->bus_address = dmah->busaddr + offset;
  771. buf->next = NULL;
  772. buf->waiting = 0;
  773. buf->pending = 0;
  774. init_waitqueue_head(&buf->dma_wait);
  775. buf->filp = NULL;
  776. buf->dev_priv_size = dev->driver->dev_priv_size;
  777. buf->dev_private = drm_alloc(buf->dev_priv_size,
  778. DRM_MEM_BUFS);
  779. if (!buf->dev_private) {
  780. /* Set count correctly so we free the proper amount. */
  781. entry->buf_count = count;
  782. entry->seg_count = count;
  783. drm_cleanup_buf_error(dev, entry);
  784. drm_free(temp_pagelist,
  785. (dma->page_count +
  786. (count << page_order))
  787. * sizeof(*dma->pagelist),
  788. DRM_MEM_PAGES);
  789. mutex_unlock(&dev->struct_mutex);
  790. atomic_dec(&dev->buf_alloc);
  791. return -ENOMEM;
  792. }
  793. memset(buf->dev_private, 0, buf->dev_priv_size);
  794. DRM_DEBUG("buffer %d @ %p\n",
  795. entry->buf_count, buf->address);
  796. }
  797. byte_count += PAGE_SIZE << page_order;
  798. }
  799. temp_buflist = drm_realloc(dma->buflist,
  800. dma->buf_count * sizeof(*dma->buflist),
  801. (dma->buf_count + entry->buf_count)
  802. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  803. if (!temp_buflist) {
  804. /* Free the entry because it isn't valid */
  805. drm_cleanup_buf_error(dev, entry);
  806. drm_free(temp_pagelist,
  807. (dma->page_count + (count << page_order))
  808. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  809. mutex_unlock(&dev->struct_mutex);
  810. atomic_dec(&dev->buf_alloc);
  811. return -ENOMEM;
  812. }
  813. dma->buflist = temp_buflist;
  814. for (i = 0; i < entry->buf_count; i++) {
  815. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  816. }
  817. /* No allocations failed, so now we can replace the orginal pagelist
  818. * with the new one.
  819. */
  820. if (dma->page_count) {
  821. drm_free(dma->pagelist,
  822. dma->page_count * sizeof(*dma->pagelist),
  823. DRM_MEM_PAGES);
  824. }
  825. dma->pagelist = temp_pagelist;
  826. dma->buf_count += entry->buf_count;
  827. dma->seg_count += entry->seg_count;
  828. dma->page_count += entry->seg_count << page_order;
  829. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  830. mutex_unlock(&dev->struct_mutex);
  831. request->count = entry->buf_count;
  832. request->size = size;
  833. if (request->flags & _DRM_PCI_BUFFER_RO)
  834. dma->flags = _DRM_DMA_USE_PCI_RO;
  835. atomic_dec(&dev->buf_alloc);
  836. return 0;
  837. }
  838. EXPORT_SYMBOL(drm_addbufs_pci);
  839. static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
  840. {
  841. drm_device_dma_t *dma = dev->dma;
  842. drm_buf_entry_t *entry;
  843. drm_buf_t *buf;
  844. unsigned long offset;
  845. unsigned long agp_offset;
  846. int count;
  847. int order;
  848. int size;
  849. int alignment;
  850. int page_order;
  851. int total;
  852. int byte_count;
  853. int i;
  854. drm_buf_t **temp_buflist;
  855. if (!drm_core_check_feature(dev, DRIVER_SG))
  856. return -EINVAL;
  857. if (!dma)
  858. return -EINVAL;
  859. if (!capable(CAP_SYS_ADMIN))
  860. return -EPERM;
  861. count = request->count;
  862. order = drm_order(request->size);
  863. size = 1 << order;
  864. alignment = (request->flags & _DRM_PAGE_ALIGN)
  865. ? PAGE_ALIGN(size) : size;
  866. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  867. total = PAGE_SIZE << page_order;
  868. byte_count = 0;
  869. agp_offset = request->agp_start;
  870. DRM_DEBUG("count: %d\n", count);
  871. DRM_DEBUG("order: %d\n", order);
  872. DRM_DEBUG("size: %d\n", size);
  873. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  874. DRM_DEBUG("alignment: %d\n", alignment);
  875. DRM_DEBUG("page_order: %d\n", page_order);
  876. DRM_DEBUG("total: %d\n", total);
  877. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  878. return -EINVAL;
  879. if (dev->queue_count)
  880. return -EBUSY; /* Not while in use */
  881. spin_lock(&dev->count_lock);
  882. if (dev->buf_use) {
  883. spin_unlock(&dev->count_lock);
  884. return -EBUSY;
  885. }
  886. atomic_inc(&dev->buf_alloc);
  887. spin_unlock(&dev->count_lock);
  888. mutex_lock(&dev->struct_mutex);
  889. entry = &dma->bufs[order];
  890. if (entry->buf_count) {
  891. mutex_unlock(&dev->struct_mutex);
  892. atomic_dec(&dev->buf_alloc);
  893. return -ENOMEM; /* May only call once for each order */
  894. }
  895. if (count < 0 || count > 4096) {
  896. mutex_unlock(&dev->struct_mutex);
  897. atomic_dec(&dev->buf_alloc);
  898. return -EINVAL;
  899. }
  900. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  901. DRM_MEM_BUFS);
  902. if (!entry->buflist) {
  903. mutex_unlock(&dev->struct_mutex);
  904. atomic_dec(&dev->buf_alloc);
  905. return -ENOMEM;
  906. }
  907. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  908. entry->buf_size = size;
  909. entry->page_order = page_order;
  910. offset = 0;
  911. while (entry->buf_count < count) {
  912. buf = &entry->buflist[entry->buf_count];
  913. buf->idx = dma->buf_count + entry->buf_count;
  914. buf->total = alignment;
  915. buf->order = order;
  916. buf->used = 0;
  917. buf->offset = (dma->byte_count + offset);
  918. buf->bus_address = agp_offset + offset;
  919. buf->address = (void *)(agp_offset + offset
  920. + (unsigned long)dev->sg->virtual);
  921. buf->next = NULL;
  922. buf->waiting = 0;
  923. buf->pending = 0;
  924. init_waitqueue_head(&buf->dma_wait);
  925. buf->filp = NULL;
  926. buf->dev_priv_size = dev->driver->dev_priv_size;
  927. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  928. if (!buf->dev_private) {
  929. /* Set count correctly so we free the proper amount. */
  930. entry->buf_count = count;
  931. drm_cleanup_buf_error(dev, entry);
  932. mutex_unlock(&dev->struct_mutex);
  933. atomic_dec(&dev->buf_alloc);
  934. return -ENOMEM;
  935. }
  936. memset(buf->dev_private, 0, buf->dev_priv_size);
  937. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  938. offset += alignment;
  939. entry->buf_count++;
  940. byte_count += PAGE_SIZE << page_order;
  941. }
  942. DRM_DEBUG("byte_count: %d\n", byte_count);
  943. temp_buflist = drm_realloc(dma->buflist,
  944. dma->buf_count * sizeof(*dma->buflist),
  945. (dma->buf_count + entry->buf_count)
  946. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  947. if (!temp_buflist) {
  948. /* Free the entry because it isn't valid */
  949. drm_cleanup_buf_error(dev, entry);
  950. mutex_unlock(&dev->struct_mutex);
  951. atomic_dec(&dev->buf_alloc);
  952. return -ENOMEM;
  953. }
  954. dma->buflist = temp_buflist;
  955. for (i = 0; i < entry->buf_count; i++) {
  956. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  957. }
  958. dma->buf_count += entry->buf_count;
  959. dma->seg_count += entry->seg_count;
  960. dma->page_count += byte_count >> PAGE_SHIFT;
  961. dma->byte_count += byte_count;
  962. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  963. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  964. mutex_unlock(&dev->struct_mutex);
  965. request->count = entry->buf_count;
  966. request->size = size;
  967. dma->flags = _DRM_DMA_USE_SG;
  968. atomic_dec(&dev->buf_alloc);
  969. return 0;
  970. }
  971. static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
  972. {
  973. drm_device_dma_t *dma = dev->dma;
  974. drm_buf_entry_t *entry;
  975. drm_buf_t *buf;
  976. unsigned long offset;
  977. unsigned long agp_offset;
  978. int count;
  979. int order;
  980. int size;
  981. int alignment;
  982. int page_order;
  983. int total;
  984. int byte_count;
  985. int i;
  986. drm_buf_t **temp_buflist;
  987. if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
  988. return -EINVAL;
  989. if (!dma)
  990. return -EINVAL;
  991. if (!capable(CAP_SYS_ADMIN))
  992. return -EPERM;
  993. count = request->count;
  994. order = drm_order(request->size);
  995. size = 1 << order;
  996. alignment = (request->flags & _DRM_PAGE_ALIGN)
  997. ? PAGE_ALIGN(size) : size;
  998. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  999. total = PAGE_SIZE << page_order;
  1000. byte_count = 0;
  1001. agp_offset = request->agp_start;
  1002. DRM_DEBUG("count: %d\n", count);
  1003. DRM_DEBUG("order: %d\n", order);
  1004. DRM_DEBUG("size: %d\n", size);
  1005. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  1006. DRM_DEBUG("alignment: %d\n", alignment);
  1007. DRM_DEBUG("page_order: %d\n", page_order);
  1008. DRM_DEBUG("total: %d\n", total);
  1009. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1010. return -EINVAL;
  1011. if (dev->queue_count)
  1012. return -EBUSY; /* Not while in use */
  1013. spin_lock(&dev->count_lock);
  1014. if (dev->buf_use) {
  1015. spin_unlock(&dev->count_lock);
  1016. return -EBUSY;
  1017. }
  1018. atomic_inc(&dev->buf_alloc);
  1019. spin_unlock(&dev->count_lock);
  1020. mutex_lock(&dev->struct_mutex);
  1021. entry = &dma->bufs[order];
  1022. if (entry->buf_count) {
  1023. mutex_unlock(&dev->struct_mutex);
  1024. atomic_dec(&dev->buf_alloc);
  1025. return -ENOMEM; /* May only call once for each order */
  1026. }
  1027. if (count < 0 || count > 4096) {
  1028. mutex_unlock(&dev->struct_mutex);
  1029. atomic_dec(&dev->buf_alloc);
  1030. return -EINVAL;
  1031. }
  1032. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  1033. DRM_MEM_BUFS);
  1034. if (!entry->buflist) {
  1035. mutex_unlock(&dev->struct_mutex);
  1036. atomic_dec(&dev->buf_alloc);
  1037. return -ENOMEM;
  1038. }
  1039. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  1040. entry->buf_size = size;
  1041. entry->page_order = page_order;
  1042. offset = 0;
  1043. while (entry->buf_count < count) {
  1044. buf = &entry->buflist[entry->buf_count];
  1045. buf->idx = dma->buf_count + entry->buf_count;
  1046. buf->total = alignment;
  1047. buf->order = order;
  1048. buf->used = 0;
  1049. buf->offset = (dma->byte_count + offset);
  1050. buf->bus_address = agp_offset + offset;
  1051. buf->address = (void *)(agp_offset + offset);
  1052. buf->next = NULL;
  1053. buf->waiting = 0;
  1054. buf->pending = 0;
  1055. init_waitqueue_head(&buf->dma_wait);
  1056. buf->filp = NULL;
  1057. buf->dev_priv_size = dev->driver->dev_priv_size;
  1058. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  1059. if (!buf->dev_private) {
  1060. /* Set count correctly so we free the proper amount. */
  1061. entry->buf_count = count;
  1062. drm_cleanup_buf_error(dev, entry);
  1063. mutex_unlock(&dev->struct_mutex);
  1064. atomic_dec(&dev->buf_alloc);
  1065. return -ENOMEM;
  1066. }
  1067. memset(buf->dev_private, 0, buf->dev_priv_size);
  1068. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  1069. offset += alignment;
  1070. entry->buf_count++;
  1071. byte_count += PAGE_SIZE << page_order;
  1072. }
  1073. DRM_DEBUG("byte_count: %d\n", byte_count);
  1074. temp_buflist = drm_realloc(dma->buflist,
  1075. dma->buf_count * sizeof(*dma->buflist),
  1076. (dma->buf_count + entry->buf_count)
  1077. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  1078. if (!temp_buflist) {
  1079. /* Free the entry because it isn't valid */
  1080. drm_cleanup_buf_error(dev, entry);
  1081. mutex_unlock(&dev->struct_mutex);
  1082. atomic_dec(&dev->buf_alloc);
  1083. return -ENOMEM;
  1084. }
  1085. dma->buflist = temp_buflist;
  1086. for (i = 0; i < entry->buf_count; i++) {
  1087. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  1088. }
  1089. dma->buf_count += entry->buf_count;
  1090. dma->seg_count += entry->seg_count;
  1091. dma->page_count += byte_count >> PAGE_SHIFT;
  1092. dma->byte_count += byte_count;
  1093. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  1094. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  1095. mutex_unlock(&dev->struct_mutex);
  1096. request->count = entry->buf_count;
  1097. request->size = size;
  1098. dma->flags = _DRM_DMA_USE_FB;
  1099. atomic_dec(&dev->buf_alloc);
  1100. return 0;
  1101. }
  1102. /**
  1103. * Add buffers for DMA transfers (ioctl).
  1104. *
  1105. * \param inode device inode.
  1106. * \param filp file pointer.
  1107. * \param cmd command.
  1108. * \param arg pointer to a drm_buf_desc_t request.
  1109. * \return zero on success or a negative number on failure.
  1110. *
  1111. * According with the memory type specified in drm_buf_desc::flags and the
  1112. * build options, it dispatches the call either to addbufs_agp(),
  1113. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1114. * PCI memory respectively.
  1115. */
  1116. int drm_addbufs(struct inode *inode, struct file *filp,
  1117. unsigned int cmd, unsigned long arg)
  1118. {
  1119. drm_buf_desc_t request;
  1120. drm_file_t *priv = filp->private_data;
  1121. drm_device_t *dev = priv->head->dev;
  1122. int ret;
  1123. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1124. return -EINVAL;
  1125. if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
  1126. sizeof(request)))
  1127. return -EFAULT;
  1128. #if __OS_HAS_AGP
  1129. if (request.flags & _DRM_AGP_BUFFER)
  1130. ret = drm_addbufs_agp(dev, &request);
  1131. else
  1132. #endif
  1133. if (request.flags & _DRM_SG_BUFFER)
  1134. ret = drm_addbufs_sg(dev, &request);
  1135. else if (request.flags & _DRM_FB_BUFFER)
  1136. ret = drm_addbufs_fb(dev, &request);
  1137. else
  1138. ret = drm_addbufs_pci(dev, &request);
  1139. if (ret == 0) {
  1140. if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
  1141. ret = -EFAULT;
  1142. }
  1143. }
  1144. return ret;
  1145. }
  1146. /**
  1147. * Get information about the buffer mappings.
  1148. *
  1149. * This was originally mean for debugging purposes, or by a sophisticated
  1150. * client library to determine how best to use the available buffers (e.g.,
  1151. * large buffers can be used for image transfer).
  1152. *
  1153. * \param inode device inode.
  1154. * \param filp file pointer.
  1155. * \param cmd command.
  1156. * \param arg pointer to a drm_buf_info structure.
  1157. * \return zero on success or a negative number on failure.
  1158. *
  1159. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1160. * lock, preventing of allocating more buffers after this call. Information
  1161. * about each requested buffer is then copied into user space.
  1162. */
  1163. int drm_infobufs(struct inode *inode, struct file *filp,
  1164. unsigned int cmd, unsigned long arg)
  1165. {
  1166. drm_file_t *priv = filp->private_data;
  1167. drm_device_t *dev = priv->head->dev;
  1168. drm_device_dma_t *dma = dev->dma;
  1169. drm_buf_info_t request;
  1170. drm_buf_info_t __user *argp = (void __user *)arg;
  1171. int i;
  1172. int count;
  1173. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1174. return -EINVAL;
  1175. if (!dma)
  1176. return -EINVAL;
  1177. spin_lock(&dev->count_lock);
  1178. if (atomic_read(&dev->buf_alloc)) {
  1179. spin_unlock(&dev->count_lock);
  1180. return -EBUSY;
  1181. }
  1182. ++dev->buf_use; /* Can't allocate more after this call */
  1183. spin_unlock(&dev->count_lock);
  1184. if (copy_from_user(&request, argp, sizeof(request)))
  1185. return -EFAULT;
  1186. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1187. if (dma->bufs[i].buf_count)
  1188. ++count;
  1189. }
  1190. DRM_DEBUG("count = %d\n", count);
  1191. if (request.count >= count) {
  1192. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1193. if (dma->bufs[i].buf_count) {
  1194. drm_buf_desc_t __user *to =
  1195. &request.list[count];
  1196. drm_buf_entry_t *from = &dma->bufs[i];
  1197. drm_freelist_t *list = &dma->bufs[i].freelist;
  1198. if (copy_to_user(&to->count,
  1199. &from->buf_count,
  1200. sizeof(from->buf_count)) ||
  1201. copy_to_user(&to->size,
  1202. &from->buf_size,
  1203. sizeof(from->buf_size)) ||
  1204. copy_to_user(&to->low_mark,
  1205. &list->low_mark,
  1206. sizeof(list->low_mark)) ||
  1207. copy_to_user(&to->high_mark,
  1208. &list->high_mark,
  1209. sizeof(list->high_mark)))
  1210. return -EFAULT;
  1211. DRM_DEBUG("%d %d %d %d %d\n",
  1212. i,
  1213. dma->bufs[i].buf_count,
  1214. dma->bufs[i].buf_size,
  1215. dma->bufs[i].freelist.low_mark,
  1216. dma->bufs[i].freelist.high_mark);
  1217. ++count;
  1218. }
  1219. }
  1220. }
  1221. request.count = count;
  1222. if (copy_to_user(argp, &request, sizeof(request)))
  1223. return -EFAULT;
  1224. return 0;
  1225. }
  1226. /**
  1227. * Specifies a low and high water mark for buffer allocation
  1228. *
  1229. * \param inode device inode.
  1230. * \param filp file pointer.
  1231. * \param cmd command.
  1232. * \param arg a pointer to a drm_buf_desc structure.
  1233. * \return zero on success or a negative number on failure.
  1234. *
  1235. * Verifies that the size order is bounded between the admissible orders and
  1236. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1237. *
  1238. * \note This ioctl is deprecated and mostly never used.
  1239. */
  1240. int drm_markbufs(struct inode *inode, struct file *filp,
  1241. unsigned int cmd, unsigned long arg)
  1242. {
  1243. drm_file_t *priv = filp->private_data;
  1244. drm_device_t *dev = priv->head->dev;
  1245. drm_device_dma_t *dma = dev->dma;
  1246. drm_buf_desc_t request;
  1247. int order;
  1248. drm_buf_entry_t *entry;
  1249. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1250. return -EINVAL;
  1251. if (!dma)
  1252. return -EINVAL;
  1253. if (copy_from_user(&request,
  1254. (drm_buf_desc_t __user *) arg, sizeof(request)))
  1255. return -EFAULT;
  1256. DRM_DEBUG("%d, %d, %d\n",
  1257. request.size, request.low_mark, request.high_mark);
  1258. order = drm_order(request.size);
  1259. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1260. return -EINVAL;
  1261. entry = &dma->bufs[order];
  1262. if (request.low_mark < 0 || request.low_mark > entry->buf_count)
  1263. return -EINVAL;
  1264. if (request.high_mark < 0 || request.high_mark > entry->buf_count)
  1265. return -EINVAL;
  1266. entry->freelist.low_mark = request.low_mark;
  1267. entry->freelist.high_mark = request.high_mark;
  1268. return 0;
  1269. }
  1270. /**
  1271. * Unreserve the buffers in list, previously reserved using drmDMA.
  1272. *
  1273. * \param inode device inode.
  1274. * \param filp file pointer.
  1275. * \param cmd command.
  1276. * \param arg pointer to a drm_buf_free structure.
  1277. * \return zero on success or a negative number on failure.
  1278. *
  1279. * Calls free_buffer() for each used buffer.
  1280. * This function is primarily used for debugging.
  1281. */
  1282. int drm_freebufs(struct inode *inode, struct file *filp,
  1283. unsigned int cmd, unsigned long arg)
  1284. {
  1285. drm_file_t *priv = filp->private_data;
  1286. drm_device_t *dev = priv->head->dev;
  1287. drm_device_dma_t *dma = dev->dma;
  1288. drm_buf_free_t request;
  1289. int i;
  1290. int idx;
  1291. drm_buf_t *buf;
  1292. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1293. return -EINVAL;
  1294. if (!dma)
  1295. return -EINVAL;
  1296. if (copy_from_user(&request,
  1297. (drm_buf_free_t __user *) arg, sizeof(request)))
  1298. return -EFAULT;
  1299. DRM_DEBUG("%d\n", request.count);
  1300. for (i = 0; i < request.count; i++) {
  1301. if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
  1302. return -EFAULT;
  1303. if (idx < 0 || idx >= dma->buf_count) {
  1304. DRM_ERROR("Index %d (of %d max)\n",
  1305. idx, dma->buf_count - 1);
  1306. return -EINVAL;
  1307. }
  1308. buf = dma->buflist[idx];
  1309. if (buf->filp != filp) {
  1310. DRM_ERROR("Process %d freeing buffer not owned\n",
  1311. current->pid);
  1312. return -EINVAL;
  1313. }
  1314. drm_free_buffer(dev, buf);
  1315. }
  1316. return 0;
  1317. }
  1318. /**
  1319. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1320. *
  1321. * \param inode device inode.
  1322. * \param filp file pointer.
  1323. * \param cmd command.
  1324. * \param arg pointer to a drm_buf_map structure.
  1325. * \return zero on success or a negative number on failure.
  1326. *
  1327. * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
  1328. * about each buffer into user space. For PCI buffers, it calls do_mmap() with
  1329. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1330. * drm_mmap_dma().
  1331. */
  1332. int drm_mapbufs(struct inode *inode, struct file *filp,
  1333. unsigned int cmd, unsigned long arg)
  1334. {
  1335. drm_file_t *priv = filp->private_data;
  1336. drm_device_t *dev = priv->head->dev;
  1337. drm_device_dma_t *dma = dev->dma;
  1338. drm_buf_map_t __user *argp = (void __user *)arg;
  1339. int retcode = 0;
  1340. const int zero = 0;
  1341. unsigned long virtual;
  1342. unsigned long address;
  1343. drm_buf_map_t request;
  1344. int i;
  1345. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1346. return -EINVAL;
  1347. if (!dma)
  1348. return -EINVAL;
  1349. spin_lock(&dev->count_lock);
  1350. if (atomic_read(&dev->buf_alloc)) {
  1351. spin_unlock(&dev->count_lock);
  1352. return -EBUSY;
  1353. }
  1354. dev->buf_use++; /* Can't allocate more after this call */
  1355. spin_unlock(&dev->count_lock);
  1356. if (copy_from_user(&request, argp, sizeof(request)))
  1357. return -EFAULT;
  1358. if (request.count >= dma->buf_count) {
  1359. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1360. || (drm_core_check_feature(dev, DRIVER_SG)
  1361. && (dma->flags & _DRM_DMA_USE_SG))
  1362. || (drm_core_check_feature(dev, DRIVER_FB_DMA)
  1363. && (dma->flags & _DRM_DMA_USE_FB))) {
  1364. drm_map_t *map = dev->agp_buffer_map;
  1365. unsigned long token = dev->agp_buffer_token;
  1366. if (!map) {
  1367. retcode = -EINVAL;
  1368. goto done;
  1369. }
  1370. down_write(&current->mm->mmap_sem);
  1371. virtual = do_mmap(filp, 0, map->size,
  1372. PROT_READ | PROT_WRITE,
  1373. MAP_SHARED, token);
  1374. up_write(&current->mm->mmap_sem);
  1375. } else {
  1376. down_write(&current->mm->mmap_sem);
  1377. virtual = do_mmap(filp, 0, dma->byte_count,
  1378. PROT_READ | PROT_WRITE,
  1379. MAP_SHARED, 0);
  1380. up_write(&current->mm->mmap_sem);
  1381. }
  1382. if (virtual > -1024UL) {
  1383. /* Real error */
  1384. retcode = (signed long)virtual;
  1385. goto done;
  1386. }
  1387. request.virtual = (void __user *)virtual;
  1388. for (i = 0; i < dma->buf_count; i++) {
  1389. if (copy_to_user(&request.list[i].idx,
  1390. &dma->buflist[i]->idx,
  1391. sizeof(request.list[0].idx))) {
  1392. retcode = -EFAULT;
  1393. goto done;
  1394. }
  1395. if (copy_to_user(&request.list[i].total,
  1396. &dma->buflist[i]->total,
  1397. sizeof(request.list[0].total))) {
  1398. retcode = -EFAULT;
  1399. goto done;
  1400. }
  1401. if (copy_to_user(&request.list[i].used,
  1402. &zero, sizeof(zero))) {
  1403. retcode = -EFAULT;
  1404. goto done;
  1405. }
  1406. address = virtual + dma->buflist[i]->offset; /* *** */
  1407. if (copy_to_user(&request.list[i].address,
  1408. &address, sizeof(address))) {
  1409. retcode = -EFAULT;
  1410. goto done;
  1411. }
  1412. }
  1413. }
  1414. done:
  1415. request.count = dma->buf_count;
  1416. DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
  1417. if (copy_to_user(argp, &request, sizeof(request)))
  1418. return -EFAULT;
  1419. return retcode;
  1420. }
  1421. /**
  1422. * Compute size order. Returns the exponent of the smaller power of two which
  1423. * is greater or equal to given number.
  1424. *
  1425. * \param size size.
  1426. * \return order.
  1427. *
  1428. * \todo Can be made faster.
  1429. */
  1430. int drm_order(unsigned long size)
  1431. {
  1432. int order;
  1433. unsigned long tmp;
  1434. for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
  1435. if (size & (size - 1))
  1436. ++order;
  1437. return order;
  1438. }
  1439. EXPORT_SYMBOL(drm_order);