drm_bufs.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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-1) < 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. mutex_lock(&dev->struct_mutex);
  236. list_add(&list->head, &dev->maplist->head);
  237. /* Assign a 32-bit handle */
  238. /* We do it here so that dev->struct_mutex protects the increment */
  239. list->user_token = HandleID(map->type == _DRM_SHM
  240. ? (unsigned long)map->handle
  241. : map->offset, dev);
  242. mutex_unlock(&dev->struct_mutex);
  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. if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
  273. return -EPERM;
  274. err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
  275. &maplist);
  276. if (err)
  277. return err;
  278. if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
  279. return -EFAULT;
  280. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  281. if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
  282. return -EFAULT;
  283. return 0;
  284. }
  285. /**
  286. * Remove a map private from list and deallocate resources if the mapping
  287. * isn't in use.
  288. *
  289. * \param inode device inode.
  290. * \param filp file pointer.
  291. * \param cmd command.
  292. * \param arg pointer to a drm_map_t structure.
  293. * \return zero on success or a negative value on error.
  294. *
  295. * Searches the map on drm_device::maplist, removes it from the list, see if
  296. * its being used, and free any associate resource (such as MTRR's) if it's not
  297. * being on use.
  298. *
  299. * \sa drm_addmap
  300. */
  301. int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
  302. {
  303. struct list_head *list;
  304. drm_map_list_t *r_list = NULL;
  305. drm_dma_handle_t dmah;
  306. /* Find the list entry for the map and remove it */
  307. list_for_each(list, &dev->maplist->head) {
  308. r_list = list_entry(list, drm_map_list_t, head);
  309. if (r_list->map == map) {
  310. list_del(list);
  311. drm_free(list, sizeof(*list), DRM_MEM_MAPS);
  312. break;
  313. }
  314. }
  315. /* List has wrapped around to the head pointer, or it's empty and we
  316. * didn't find anything.
  317. */
  318. if (list == (&dev->maplist->head)) {
  319. return -EINVAL;
  320. }
  321. switch (map->type) {
  322. case _DRM_REGISTERS:
  323. drm_ioremapfree(map->handle, map->size, dev);
  324. /* FALLTHROUGH */
  325. case _DRM_FRAME_BUFFER:
  326. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  327. int retcode;
  328. retcode = mtrr_del(map->mtrr, map->offset, map->size);
  329. DRM_DEBUG("mtrr_del=%d\n", retcode);
  330. }
  331. break;
  332. case _DRM_SHM:
  333. vfree(map->handle);
  334. break;
  335. case _DRM_AGP:
  336. case _DRM_SCATTER_GATHER:
  337. break;
  338. case _DRM_CONSISTENT:
  339. dmah.vaddr = map->handle;
  340. dmah.busaddr = map->offset;
  341. dmah.size = map->size;
  342. __drm_pci_free(dev, &dmah);
  343. break;
  344. }
  345. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  346. return 0;
  347. }
  348. int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
  349. {
  350. int ret;
  351. mutex_lock(&dev->struct_mutex);
  352. ret = drm_rmmap_locked(dev, map);
  353. mutex_unlock(&dev->struct_mutex);
  354. return ret;
  355. }
  356. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  357. * the last close of the device, and this is necessary for cleanup when things
  358. * exit uncleanly. Therefore, having userland manually remove mappings seems
  359. * like a pointless exercise since they're going away anyway.
  360. *
  361. * One use case might be after addmap is allowed for normal users for SHM and
  362. * gets used by drivers that the server doesn't need to care about. This seems
  363. * unlikely.
  364. */
  365. int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
  366. unsigned int cmd, unsigned long arg)
  367. {
  368. drm_file_t *priv = filp->private_data;
  369. drm_device_t *dev = priv->head->dev;
  370. drm_map_t request;
  371. drm_local_map_t *map = NULL;
  372. struct list_head *list;
  373. int ret;
  374. if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
  375. return -EFAULT;
  376. }
  377. mutex_lock(&dev->struct_mutex);
  378. list_for_each(list, &dev->maplist->head) {
  379. drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
  380. if (r_list->map &&
  381. r_list->user_token == (unsigned long)request.handle &&
  382. r_list->map->flags & _DRM_REMOVABLE) {
  383. map = r_list->map;
  384. break;
  385. }
  386. }
  387. /* List has wrapped around to the head pointer, or its empty we didn't
  388. * find anything.
  389. */
  390. if (list == (&dev->maplist->head)) {
  391. mutex_unlock(&dev->struct_mutex);
  392. return -EINVAL;
  393. }
  394. if (!map)
  395. return -EINVAL;
  396. /* Register and framebuffer maps are permanent */
  397. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  398. mutex_unlock(&dev->struct_mutex);
  399. return 0;
  400. }
  401. ret = drm_rmmap_locked(dev, map);
  402. mutex_unlock(&dev->struct_mutex);
  403. return ret;
  404. }
  405. /**
  406. * Cleanup after an error on one of the addbufs() functions.
  407. *
  408. * \param dev DRM device.
  409. * \param entry buffer entry where the error occurred.
  410. *
  411. * Frees any pages and buffers associated with the given entry.
  412. */
  413. static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
  414. {
  415. int i;
  416. if (entry->seg_count) {
  417. for (i = 0; i < entry->seg_count; i++) {
  418. if (entry->seglist[i]) {
  419. drm_pci_free(dev, entry->seglist[i]);
  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: %lx\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. mutex_lock(&dev->struct_mutex);
  499. entry = &dma->bufs[order];
  500. if (entry->buf_count) {
  501. mutex_unlock(&dev->struct_mutex);
  502. atomic_dec(&dev->buf_alloc);
  503. return -ENOMEM; /* May only call once for each order */
  504. }
  505. if (count < 0 || count > 4096) {
  506. mutex_unlock(&dev->struct_mutex);
  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. mutex_unlock(&dev->struct_mutex);
  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. mutex_unlock(&dev->struct_mutex);
  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. mutex_unlock(&dev->struct_mutex);
  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->seg_count += entry->seg_count;
  569. dma->page_count += byte_count >> PAGE_SHIFT;
  570. dma->byte_count += byte_count;
  571. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  572. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  573. mutex_unlock(&dev->struct_mutex);
  574. request->count = entry->buf_count;
  575. request->size = size;
  576. dma->flags = _DRM_DMA_USE_AGP;
  577. atomic_dec(&dev->buf_alloc);
  578. return 0;
  579. }
  580. EXPORT_SYMBOL(drm_addbufs_agp);
  581. #endif /* __OS_HAS_AGP */
  582. int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
  583. {
  584. drm_device_dma_t *dma = dev->dma;
  585. int count;
  586. int order;
  587. int size;
  588. int total;
  589. int page_order;
  590. drm_buf_entry_t *entry;
  591. drm_dma_handle_t *dmah;
  592. drm_buf_t *buf;
  593. int alignment;
  594. unsigned long offset;
  595. int i;
  596. int byte_count;
  597. int page_count;
  598. unsigned long *temp_pagelist;
  599. drm_buf_t **temp_buflist;
  600. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  601. return -EINVAL;
  602. if (!dma)
  603. return -EINVAL;
  604. if (!capable(CAP_SYS_ADMIN))
  605. return -EPERM;
  606. count = request->count;
  607. order = drm_order(request->size);
  608. size = 1 << order;
  609. DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  610. request->count, request->size, size, order, dev->queue_count);
  611. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  612. return -EINVAL;
  613. if (dev->queue_count)
  614. return -EBUSY; /* Not while in use */
  615. alignment = (request->flags & _DRM_PAGE_ALIGN)
  616. ? PAGE_ALIGN(size) : size;
  617. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  618. total = PAGE_SIZE << page_order;
  619. spin_lock(&dev->count_lock);
  620. if (dev->buf_use) {
  621. spin_unlock(&dev->count_lock);
  622. return -EBUSY;
  623. }
  624. atomic_inc(&dev->buf_alloc);
  625. spin_unlock(&dev->count_lock);
  626. mutex_lock(&dev->struct_mutex);
  627. entry = &dma->bufs[order];
  628. if (entry->buf_count) {
  629. mutex_unlock(&dev->struct_mutex);
  630. atomic_dec(&dev->buf_alloc);
  631. return -ENOMEM; /* May only call once for each order */
  632. }
  633. if (count < 0 || count > 4096) {
  634. mutex_unlock(&dev->struct_mutex);
  635. atomic_dec(&dev->buf_alloc);
  636. return -EINVAL;
  637. }
  638. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  639. DRM_MEM_BUFS);
  640. if (!entry->buflist) {
  641. mutex_unlock(&dev->struct_mutex);
  642. atomic_dec(&dev->buf_alloc);
  643. return -ENOMEM;
  644. }
  645. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  646. entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
  647. DRM_MEM_SEGS);
  648. if (!entry->seglist) {
  649. drm_free(entry->buflist,
  650. count * sizeof(*entry->buflist), DRM_MEM_BUFS);
  651. mutex_unlock(&dev->struct_mutex);
  652. atomic_dec(&dev->buf_alloc);
  653. return -ENOMEM;
  654. }
  655. memset(entry->seglist, 0, count * sizeof(*entry->seglist));
  656. /* Keep the original pagelist until we know all the allocations
  657. * have succeeded
  658. */
  659. temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
  660. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  661. if (!temp_pagelist) {
  662. drm_free(entry->buflist,
  663. count * sizeof(*entry->buflist), DRM_MEM_BUFS);
  664. drm_free(entry->seglist,
  665. count * sizeof(*entry->seglist), DRM_MEM_SEGS);
  666. mutex_unlock(&dev->struct_mutex);
  667. atomic_dec(&dev->buf_alloc);
  668. return -ENOMEM;
  669. }
  670. memcpy(temp_pagelist,
  671. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  672. DRM_DEBUG("pagelist: %d entries\n",
  673. dma->page_count + (count << page_order));
  674. entry->buf_size = size;
  675. entry->page_order = page_order;
  676. byte_count = 0;
  677. page_count = 0;
  678. while (entry->buf_count < count) {
  679. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
  680. if (!dmah) {
  681. /* Set count correctly so we free the proper amount. */
  682. entry->buf_count = count;
  683. entry->seg_count = count;
  684. drm_cleanup_buf_error(dev, entry);
  685. drm_free(temp_pagelist,
  686. (dma->page_count + (count << page_order))
  687. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  688. mutex_unlock(&dev->struct_mutex);
  689. atomic_dec(&dev->buf_alloc);
  690. return -ENOMEM;
  691. }
  692. entry->seglist[entry->seg_count++] = dmah;
  693. for (i = 0; i < (1 << page_order); i++) {
  694. DRM_DEBUG("page %d @ 0x%08lx\n",
  695. dma->page_count + page_count,
  696. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  697. temp_pagelist[dma->page_count + page_count++]
  698. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  699. }
  700. for (offset = 0;
  701. offset + size <= total && entry->buf_count < count;
  702. offset += alignment, ++entry->buf_count) {
  703. buf = &entry->buflist[entry->buf_count];
  704. buf->idx = dma->buf_count + entry->buf_count;
  705. buf->total = alignment;
  706. buf->order = order;
  707. buf->used = 0;
  708. buf->offset = (dma->byte_count + byte_count + offset);
  709. buf->address = (void *)(dmah->vaddr + offset);
  710. buf->bus_address = dmah->busaddr + offset;
  711. buf->next = NULL;
  712. buf->waiting = 0;
  713. buf->pending = 0;
  714. init_waitqueue_head(&buf->dma_wait);
  715. buf->filp = NULL;
  716. buf->dev_priv_size = dev->driver->dev_priv_size;
  717. buf->dev_private = drm_alloc(buf->dev_priv_size,
  718. DRM_MEM_BUFS);
  719. if (!buf->dev_private) {
  720. /* Set count correctly so we free the proper amount. */
  721. entry->buf_count = count;
  722. entry->seg_count = count;
  723. drm_cleanup_buf_error(dev, entry);
  724. drm_free(temp_pagelist,
  725. (dma->page_count +
  726. (count << page_order))
  727. * sizeof(*dma->pagelist),
  728. DRM_MEM_PAGES);
  729. mutex_unlock(&dev->struct_mutex);
  730. atomic_dec(&dev->buf_alloc);
  731. return -ENOMEM;
  732. }
  733. memset(buf->dev_private, 0, buf->dev_priv_size);
  734. DRM_DEBUG("buffer %d @ %p\n",
  735. entry->buf_count, buf->address);
  736. }
  737. byte_count += PAGE_SIZE << page_order;
  738. }
  739. temp_buflist = drm_realloc(dma->buflist,
  740. dma->buf_count * sizeof(*dma->buflist),
  741. (dma->buf_count + entry->buf_count)
  742. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  743. if (!temp_buflist) {
  744. /* Free the entry because it isn't valid */
  745. drm_cleanup_buf_error(dev, entry);
  746. drm_free(temp_pagelist,
  747. (dma->page_count + (count << page_order))
  748. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  749. mutex_unlock(&dev->struct_mutex);
  750. atomic_dec(&dev->buf_alloc);
  751. return -ENOMEM;
  752. }
  753. dma->buflist = temp_buflist;
  754. for (i = 0; i < entry->buf_count; i++) {
  755. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  756. }
  757. /* No allocations failed, so now we can replace the orginal pagelist
  758. * with the new one.
  759. */
  760. if (dma->page_count) {
  761. drm_free(dma->pagelist,
  762. dma->page_count * sizeof(*dma->pagelist),
  763. DRM_MEM_PAGES);
  764. }
  765. dma->pagelist = temp_pagelist;
  766. dma->buf_count += entry->buf_count;
  767. dma->seg_count += entry->seg_count;
  768. dma->page_count += entry->seg_count << page_order;
  769. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  770. mutex_unlock(&dev->struct_mutex);
  771. request->count = entry->buf_count;
  772. request->size = size;
  773. atomic_dec(&dev->buf_alloc);
  774. return 0;
  775. }
  776. EXPORT_SYMBOL(drm_addbufs_pci);
  777. static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
  778. {
  779. drm_device_dma_t *dma = dev->dma;
  780. drm_buf_entry_t *entry;
  781. drm_buf_t *buf;
  782. unsigned long offset;
  783. unsigned long agp_offset;
  784. int count;
  785. int order;
  786. int size;
  787. int alignment;
  788. int page_order;
  789. int total;
  790. int byte_count;
  791. int i;
  792. drm_buf_t **temp_buflist;
  793. if (!drm_core_check_feature(dev, DRIVER_SG))
  794. return -EINVAL;
  795. if (!dma)
  796. return -EINVAL;
  797. if (!capable(CAP_SYS_ADMIN))
  798. return -EPERM;
  799. count = request->count;
  800. order = drm_order(request->size);
  801. size = 1 << order;
  802. alignment = (request->flags & _DRM_PAGE_ALIGN)
  803. ? PAGE_ALIGN(size) : size;
  804. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  805. total = PAGE_SIZE << page_order;
  806. byte_count = 0;
  807. agp_offset = request->agp_start;
  808. DRM_DEBUG("count: %d\n", count);
  809. DRM_DEBUG("order: %d\n", order);
  810. DRM_DEBUG("size: %d\n", size);
  811. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  812. DRM_DEBUG("alignment: %d\n", alignment);
  813. DRM_DEBUG("page_order: %d\n", page_order);
  814. DRM_DEBUG("total: %d\n", total);
  815. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  816. return -EINVAL;
  817. if (dev->queue_count)
  818. return -EBUSY; /* Not while in use */
  819. spin_lock(&dev->count_lock);
  820. if (dev->buf_use) {
  821. spin_unlock(&dev->count_lock);
  822. return -EBUSY;
  823. }
  824. atomic_inc(&dev->buf_alloc);
  825. spin_unlock(&dev->count_lock);
  826. mutex_lock(&dev->struct_mutex);
  827. entry = &dma->bufs[order];
  828. if (entry->buf_count) {
  829. mutex_unlock(&dev->struct_mutex);
  830. atomic_dec(&dev->buf_alloc);
  831. return -ENOMEM; /* May only call once for each order */
  832. }
  833. if (count < 0 || count > 4096) {
  834. mutex_unlock(&dev->struct_mutex);
  835. atomic_dec(&dev->buf_alloc);
  836. return -EINVAL;
  837. }
  838. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  839. DRM_MEM_BUFS);
  840. if (!entry->buflist) {
  841. mutex_unlock(&dev->struct_mutex);
  842. atomic_dec(&dev->buf_alloc);
  843. return -ENOMEM;
  844. }
  845. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  846. entry->buf_size = size;
  847. entry->page_order = page_order;
  848. offset = 0;
  849. while (entry->buf_count < count) {
  850. buf = &entry->buflist[entry->buf_count];
  851. buf->idx = dma->buf_count + entry->buf_count;
  852. buf->total = alignment;
  853. buf->order = order;
  854. buf->used = 0;
  855. buf->offset = (dma->byte_count + offset);
  856. buf->bus_address = agp_offset + offset;
  857. buf->address = (void *)(agp_offset + offset
  858. + (unsigned long)dev->sg->virtual);
  859. buf->next = NULL;
  860. buf->waiting = 0;
  861. buf->pending = 0;
  862. init_waitqueue_head(&buf->dma_wait);
  863. buf->filp = NULL;
  864. buf->dev_priv_size = dev->driver->dev_priv_size;
  865. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  866. if (!buf->dev_private) {
  867. /* Set count correctly so we free the proper amount. */
  868. entry->buf_count = count;
  869. drm_cleanup_buf_error(dev, entry);
  870. mutex_unlock(&dev->struct_mutex);
  871. atomic_dec(&dev->buf_alloc);
  872. return -ENOMEM;
  873. }
  874. memset(buf->dev_private, 0, buf->dev_priv_size);
  875. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  876. offset += alignment;
  877. entry->buf_count++;
  878. byte_count += PAGE_SIZE << page_order;
  879. }
  880. DRM_DEBUG("byte_count: %d\n", byte_count);
  881. temp_buflist = drm_realloc(dma->buflist,
  882. dma->buf_count * sizeof(*dma->buflist),
  883. (dma->buf_count + entry->buf_count)
  884. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  885. if (!temp_buflist) {
  886. /* Free the entry because it isn't valid */
  887. drm_cleanup_buf_error(dev, entry);
  888. mutex_unlock(&dev->struct_mutex);
  889. atomic_dec(&dev->buf_alloc);
  890. return -ENOMEM;
  891. }
  892. dma->buflist = temp_buflist;
  893. for (i = 0; i < entry->buf_count; i++) {
  894. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  895. }
  896. dma->buf_count += entry->buf_count;
  897. dma->seg_count += entry->seg_count;
  898. dma->page_count += byte_count >> PAGE_SHIFT;
  899. dma->byte_count += byte_count;
  900. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  901. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  902. mutex_unlock(&dev->struct_mutex);
  903. request->count = entry->buf_count;
  904. request->size = size;
  905. dma->flags = _DRM_DMA_USE_SG;
  906. atomic_dec(&dev->buf_alloc);
  907. return 0;
  908. }
  909. static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
  910. {
  911. drm_device_dma_t *dma = dev->dma;
  912. drm_buf_entry_t *entry;
  913. drm_buf_t *buf;
  914. unsigned long offset;
  915. unsigned long agp_offset;
  916. int count;
  917. int order;
  918. int size;
  919. int alignment;
  920. int page_order;
  921. int total;
  922. int byte_count;
  923. int i;
  924. drm_buf_t **temp_buflist;
  925. if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
  926. return -EINVAL;
  927. if (!dma)
  928. return -EINVAL;
  929. if (!capable(CAP_SYS_ADMIN))
  930. return -EPERM;
  931. count = request->count;
  932. order = drm_order(request->size);
  933. size = 1 << order;
  934. alignment = (request->flags & _DRM_PAGE_ALIGN)
  935. ? PAGE_ALIGN(size) : size;
  936. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  937. total = PAGE_SIZE << page_order;
  938. byte_count = 0;
  939. agp_offset = request->agp_start;
  940. DRM_DEBUG("count: %d\n", count);
  941. DRM_DEBUG("order: %d\n", order);
  942. DRM_DEBUG("size: %d\n", size);
  943. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  944. DRM_DEBUG("alignment: %d\n", alignment);
  945. DRM_DEBUG("page_order: %d\n", page_order);
  946. DRM_DEBUG("total: %d\n", total);
  947. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  948. return -EINVAL;
  949. if (dev->queue_count)
  950. return -EBUSY; /* Not while in use */
  951. spin_lock(&dev->count_lock);
  952. if (dev->buf_use) {
  953. spin_unlock(&dev->count_lock);
  954. return -EBUSY;
  955. }
  956. atomic_inc(&dev->buf_alloc);
  957. spin_unlock(&dev->count_lock);
  958. mutex_lock(&dev->struct_mutex);
  959. entry = &dma->bufs[order];
  960. if (entry->buf_count) {
  961. mutex_unlock(&dev->struct_mutex);
  962. atomic_dec(&dev->buf_alloc);
  963. return -ENOMEM; /* May only call once for each order */
  964. }
  965. if (count < 0 || count > 4096) {
  966. mutex_unlock(&dev->struct_mutex);
  967. atomic_dec(&dev->buf_alloc);
  968. return -EINVAL;
  969. }
  970. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  971. DRM_MEM_BUFS);
  972. if (!entry->buflist) {
  973. mutex_unlock(&dev->struct_mutex);
  974. atomic_dec(&dev->buf_alloc);
  975. return -ENOMEM;
  976. }
  977. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  978. entry->buf_size = size;
  979. entry->page_order = page_order;
  980. offset = 0;
  981. while (entry->buf_count < count) {
  982. buf = &entry->buflist[entry->buf_count];
  983. buf->idx = dma->buf_count + entry->buf_count;
  984. buf->total = alignment;
  985. buf->order = order;
  986. buf->used = 0;
  987. buf->offset = (dma->byte_count + offset);
  988. buf->bus_address = agp_offset + offset;
  989. buf->address = (void *)(agp_offset + offset);
  990. buf->next = NULL;
  991. buf->waiting = 0;
  992. buf->pending = 0;
  993. init_waitqueue_head(&buf->dma_wait);
  994. buf->filp = NULL;
  995. buf->dev_priv_size = dev->driver->dev_priv_size;
  996. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  997. if (!buf->dev_private) {
  998. /* Set count correctly so we free the proper amount. */
  999. entry->buf_count = count;
  1000. drm_cleanup_buf_error(dev, entry);
  1001. mutex_unlock(&dev->struct_mutex);
  1002. atomic_dec(&dev->buf_alloc);
  1003. return -ENOMEM;
  1004. }
  1005. memset(buf->dev_private, 0, buf->dev_priv_size);
  1006. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  1007. offset += alignment;
  1008. entry->buf_count++;
  1009. byte_count += PAGE_SIZE << page_order;
  1010. }
  1011. DRM_DEBUG("byte_count: %d\n", byte_count);
  1012. temp_buflist = drm_realloc(dma->buflist,
  1013. dma->buf_count * sizeof(*dma->buflist),
  1014. (dma->buf_count + entry->buf_count)
  1015. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  1016. if (!temp_buflist) {
  1017. /* Free the entry because it isn't valid */
  1018. drm_cleanup_buf_error(dev, entry);
  1019. mutex_unlock(&dev->struct_mutex);
  1020. atomic_dec(&dev->buf_alloc);
  1021. return -ENOMEM;
  1022. }
  1023. dma->buflist = temp_buflist;
  1024. for (i = 0; i < entry->buf_count; i++) {
  1025. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  1026. }
  1027. dma->buf_count += entry->buf_count;
  1028. dma->seg_count += entry->seg_count;
  1029. dma->page_count += byte_count >> PAGE_SHIFT;
  1030. dma->byte_count += byte_count;
  1031. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  1032. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  1033. mutex_unlock(&dev->struct_mutex);
  1034. request->count = entry->buf_count;
  1035. request->size = size;
  1036. dma->flags = _DRM_DMA_USE_FB;
  1037. atomic_dec(&dev->buf_alloc);
  1038. return 0;
  1039. }
  1040. /**
  1041. * Add buffers for DMA transfers (ioctl).
  1042. *
  1043. * \param inode device inode.
  1044. * \param filp file pointer.
  1045. * \param cmd command.
  1046. * \param arg pointer to a drm_buf_desc_t request.
  1047. * \return zero on success or a negative number on failure.
  1048. *
  1049. * According with the memory type specified in drm_buf_desc::flags and the
  1050. * build options, it dispatches the call either to addbufs_agp(),
  1051. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1052. * PCI memory respectively.
  1053. */
  1054. int drm_addbufs(struct inode *inode, struct file *filp,
  1055. unsigned int cmd, unsigned long arg)
  1056. {
  1057. drm_buf_desc_t request;
  1058. drm_file_t *priv = filp->private_data;
  1059. drm_device_t *dev = priv->head->dev;
  1060. int ret;
  1061. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1062. return -EINVAL;
  1063. if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
  1064. sizeof(request)))
  1065. return -EFAULT;
  1066. #if __OS_HAS_AGP
  1067. if (request.flags & _DRM_AGP_BUFFER)
  1068. ret = drm_addbufs_agp(dev, &request);
  1069. else
  1070. #endif
  1071. if (request.flags & _DRM_SG_BUFFER)
  1072. ret = drm_addbufs_sg(dev, &request);
  1073. else if (request.flags & _DRM_FB_BUFFER)
  1074. ret = drm_addbufs_fb(dev, &request);
  1075. else
  1076. ret = drm_addbufs_pci(dev, &request);
  1077. if (ret == 0) {
  1078. if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
  1079. ret = -EFAULT;
  1080. }
  1081. }
  1082. return ret;
  1083. }
  1084. /**
  1085. * Get information about the buffer mappings.
  1086. *
  1087. * This was originally mean for debugging purposes, or by a sophisticated
  1088. * client library to determine how best to use the available buffers (e.g.,
  1089. * large buffers can be used for image transfer).
  1090. *
  1091. * \param inode device inode.
  1092. * \param filp file pointer.
  1093. * \param cmd command.
  1094. * \param arg pointer to a drm_buf_info structure.
  1095. * \return zero on success or a negative number on failure.
  1096. *
  1097. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1098. * lock, preventing of allocating more buffers after this call. Information
  1099. * about each requested buffer is then copied into user space.
  1100. */
  1101. int drm_infobufs(struct inode *inode, struct file *filp,
  1102. unsigned int cmd, unsigned long arg)
  1103. {
  1104. drm_file_t *priv = filp->private_data;
  1105. drm_device_t *dev = priv->head->dev;
  1106. drm_device_dma_t *dma = dev->dma;
  1107. drm_buf_info_t request;
  1108. drm_buf_info_t __user *argp = (void __user *)arg;
  1109. int i;
  1110. int count;
  1111. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1112. return -EINVAL;
  1113. if (!dma)
  1114. return -EINVAL;
  1115. spin_lock(&dev->count_lock);
  1116. if (atomic_read(&dev->buf_alloc)) {
  1117. spin_unlock(&dev->count_lock);
  1118. return -EBUSY;
  1119. }
  1120. ++dev->buf_use; /* Can't allocate more after this call */
  1121. spin_unlock(&dev->count_lock);
  1122. if (copy_from_user(&request, argp, sizeof(request)))
  1123. return -EFAULT;
  1124. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1125. if (dma->bufs[i].buf_count)
  1126. ++count;
  1127. }
  1128. DRM_DEBUG("count = %d\n", count);
  1129. if (request.count >= count) {
  1130. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1131. if (dma->bufs[i].buf_count) {
  1132. drm_buf_desc_t __user *to =
  1133. &request.list[count];
  1134. drm_buf_entry_t *from = &dma->bufs[i];
  1135. drm_freelist_t *list = &dma->bufs[i].freelist;
  1136. if (copy_to_user(&to->count,
  1137. &from->buf_count,
  1138. sizeof(from->buf_count)) ||
  1139. copy_to_user(&to->size,
  1140. &from->buf_size,
  1141. sizeof(from->buf_size)) ||
  1142. copy_to_user(&to->low_mark,
  1143. &list->low_mark,
  1144. sizeof(list->low_mark)) ||
  1145. copy_to_user(&to->high_mark,
  1146. &list->high_mark,
  1147. sizeof(list->high_mark)))
  1148. return -EFAULT;
  1149. DRM_DEBUG("%d %d %d %d %d\n",
  1150. i,
  1151. dma->bufs[i].buf_count,
  1152. dma->bufs[i].buf_size,
  1153. dma->bufs[i].freelist.low_mark,
  1154. dma->bufs[i].freelist.high_mark);
  1155. ++count;
  1156. }
  1157. }
  1158. }
  1159. request.count = count;
  1160. if (copy_to_user(argp, &request, sizeof(request)))
  1161. return -EFAULT;
  1162. return 0;
  1163. }
  1164. /**
  1165. * Specifies a low and high water mark for buffer allocation
  1166. *
  1167. * \param inode device inode.
  1168. * \param filp file pointer.
  1169. * \param cmd command.
  1170. * \param arg a pointer to a drm_buf_desc structure.
  1171. * \return zero on success or a negative number on failure.
  1172. *
  1173. * Verifies that the size order is bounded between the admissible orders and
  1174. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1175. *
  1176. * \note This ioctl is deprecated and mostly never used.
  1177. */
  1178. int drm_markbufs(struct inode *inode, struct file *filp,
  1179. unsigned int cmd, unsigned long arg)
  1180. {
  1181. drm_file_t *priv = filp->private_data;
  1182. drm_device_t *dev = priv->head->dev;
  1183. drm_device_dma_t *dma = dev->dma;
  1184. drm_buf_desc_t request;
  1185. int order;
  1186. drm_buf_entry_t *entry;
  1187. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1188. return -EINVAL;
  1189. if (!dma)
  1190. return -EINVAL;
  1191. if (copy_from_user(&request,
  1192. (drm_buf_desc_t __user *) arg, sizeof(request)))
  1193. return -EFAULT;
  1194. DRM_DEBUG("%d, %d, %d\n",
  1195. request.size, request.low_mark, request.high_mark);
  1196. order = drm_order(request.size);
  1197. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1198. return -EINVAL;
  1199. entry = &dma->bufs[order];
  1200. if (request.low_mark < 0 || request.low_mark > entry->buf_count)
  1201. return -EINVAL;
  1202. if (request.high_mark < 0 || request.high_mark > entry->buf_count)
  1203. return -EINVAL;
  1204. entry->freelist.low_mark = request.low_mark;
  1205. entry->freelist.high_mark = request.high_mark;
  1206. return 0;
  1207. }
  1208. /**
  1209. * Unreserve the buffers in list, previously reserved using drmDMA.
  1210. *
  1211. * \param inode device inode.
  1212. * \param filp file pointer.
  1213. * \param cmd command.
  1214. * \param arg pointer to a drm_buf_free structure.
  1215. * \return zero on success or a negative number on failure.
  1216. *
  1217. * Calls free_buffer() for each used buffer.
  1218. * This function is primarily used for debugging.
  1219. */
  1220. int drm_freebufs(struct inode *inode, struct file *filp,
  1221. unsigned int cmd, unsigned long arg)
  1222. {
  1223. drm_file_t *priv = filp->private_data;
  1224. drm_device_t *dev = priv->head->dev;
  1225. drm_device_dma_t *dma = dev->dma;
  1226. drm_buf_free_t request;
  1227. int i;
  1228. int idx;
  1229. drm_buf_t *buf;
  1230. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1231. return -EINVAL;
  1232. if (!dma)
  1233. return -EINVAL;
  1234. if (copy_from_user(&request,
  1235. (drm_buf_free_t __user *) arg, sizeof(request)))
  1236. return -EFAULT;
  1237. DRM_DEBUG("%d\n", request.count);
  1238. for (i = 0; i < request.count; i++) {
  1239. if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
  1240. return -EFAULT;
  1241. if (idx < 0 || idx >= dma->buf_count) {
  1242. DRM_ERROR("Index %d (of %d max)\n",
  1243. idx, dma->buf_count - 1);
  1244. return -EINVAL;
  1245. }
  1246. buf = dma->buflist[idx];
  1247. if (buf->filp != filp) {
  1248. DRM_ERROR("Process %d freeing buffer not owned\n",
  1249. current->pid);
  1250. return -EINVAL;
  1251. }
  1252. drm_free_buffer(dev, buf);
  1253. }
  1254. return 0;
  1255. }
  1256. /**
  1257. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1258. *
  1259. * \param inode device inode.
  1260. * \param filp file pointer.
  1261. * \param cmd command.
  1262. * \param arg pointer to a drm_buf_map structure.
  1263. * \return zero on success or a negative number on failure.
  1264. *
  1265. * Maps the AGP or SG buffer region with do_mmap(), and copies information
  1266. * about each buffer into user space. The PCI buffers are already mapped on the
  1267. * addbufs_pci() call.
  1268. */
  1269. int drm_mapbufs(struct inode *inode, struct file *filp,
  1270. unsigned int cmd, unsigned long arg)
  1271. {
  1272. drm_file_t *priv = filp->private_data;
  1273. drm_device_t *dev = priv->head->dev;
  1274. drm_device_dma_t *dma = dev->dma;
  1275. drm_buf_map_t __user *argp = (void __user *)arg;
  1276. int retcode = 0;
  1277. const int zero = 0;
  1278. unsigned long virtual;
  1279. unsigned long address;
  1280. drm_buf_map_t request;
  1281. int i;
  1282. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1283. return -EINVAL;
  1284. if (!dma)
  1285. return -EINVAL;
  1286. spin_lock(&dev->count_lock);
  1287. if (atomic_read(&dev->buf_alloc)) {
  1288. spin_unlock(&dev->count_lock);
  1289. return -EBUSY;
  1290. }
  1291. dev->buf_use++; /* Can't allocate more after this call */
  1292. spin_unlock(&dev->count_lock);
  1293. if (copy_from_user(&request, argp, sizeof(request)))
  1294. return -EFAULT;
  1295. if (request.count >= dma->buf_count) {
  1296. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1297. || (drm_core_check_feature(dev, DRIVER_SG)
  1298. && (dma->flags & _DRM_DMA_USE_SG))
  1299. || (drm_core_check_feature(dev, DRIVER_FB_DMA)
  1300. && (dma->flags & _DRM_DMA_USE_FB))) {
  1301. drm_map_t *map = dev->agp_buffer_map;
  1302. unsigned long token = dev->agp_buffer_token;
  1303. if (!map) {
  1304. retcode = -EINVAL;
  1305. goto done;
  1306. }
  1307. down_write(&current->mm->mmap_sem);
  1308. virtual = do_mmap(filp, 0, map->size,
  1309. PROT_READ | PROT_WRITE,
  1310. MAP_SHARED, token);
  1311. up_write(&current->mm->mmap_sem);
  1312. } else {
  1313. down_write(&current->mm->mmap_sem);
  1314. virtual = do_mmap(filp, 0, dma->byte_count,
  1315. PROT_READ | PROT_WRITE,
  1316. MAP_SHARED, 0);
  1317. up_write(&current->mm->mmap_sem);
  1318. }
  1319. if (virtual > -1024UL) {
  1320. /* Real error */
  1321. retcode = (signed long)virtual;
  1322. goto done;
  1323. }
  1324. request.virtual = (void __user *)virtual;
  1325. for (i = 0; i < dma->buf_count; i++) {
  1326. if (copy_to_user(&request.list[i].idx,
  1327. &dma->buflist[i]->idx,
  1328. sizeof(request.list[0].idx))) {
  1329. retcode = -EFAULT;
  1330. goto done;
  1331. }
  1332. if (copy_to_user(&request.list[i].total,
  1333. &dma->buflist[i]->total,
  1334. sizeof(request.list[0].total))) {
  1335. retcode = -EFAULT;
  1336. goto done;
  1337. }
  1338. if (copy_to_user(&request.list[i].used,
  1339. &zero, sizeof(zero))) {
  1340. retcode = -EFAULT;
  1341. goto done;
  1342. }
  1343. address = virtual + dma->buflist[i]->offset; /* *** */
  1344. if (copy_to_user(&request.list[i].address,
  1345. &address, sizeof(address))) {
  1346. retcode = -EFAULT;
  1347. goto done;
  1348. }
  1349. }
  1350. }
  1351. done:
  1352. request.count = dma->buf_count;
  1353. DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
  1354. if (copy_to_user(argp, &request, sizeof(request)))
  1355. return -EFAULT;
  1356. return retcode;
  1357. }
  1358. /**
  1359. * Compute size order. Returns the exponent of the smaller power of two which
  1360. * is greater or equal to given number.
  1361. *
  1362. * \param size size.
  1363. * \return order.
  1364. *
  1365. * \todo Can be made faster.
  1366. */
  1367. int drm_order(unsigned long size)
  1368. {
  1369. int order;
  1370. unsigned long tmp;
  1371. for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
  1372. if (size & (size - 1))
  1373. ++order;
  1374. return order;
  1375. }
  1376. EXPORT_SYMBOL(drm_order);