drm_bufs.c 42 KB

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