drm_bufs.c 44 KB

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