drm_bufs.c 42 KB

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