drm_bufs.c 44 KB

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