drm_bufs.c 42 KB

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