drm_bufs.c 43 KB

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