drm_bufs.c 45 KB

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