drm_bufs.c 40 KB

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