drm_bufs.c 43 KB

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