drm_bufs.c 42 KB

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