drm_bufs.c 42 KB

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