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