drm_bufs.c 44 KB

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