drm_bufs.c 43 KB

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