drm_bufs.c 42 KB

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