drm_bufs.c 43 KB

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