drm_bufs.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /**
  2. * \file drm_bufs.h
  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. /**
  37. * Compute size order. Returns the exponent of the smaller power of two which
  38. * is greater or equal to given number.
  39. *
  40. * \param size size.
  41. * \return order.
  42. *
  43. * \todo Can be made faster.
  44. */
  45. int drm_order( unsigned long size )
  46. {
  47. int order;
  48. unsigned long tmp;
  49. for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++)
  50. ;
  51. if (size & (size - 1))
  52. ++order;
  53. return order;
  54. }
  55. EXPORT_SYMBOL(drm_order);
  56. #ifdef CONFIG_COMPAT
  57. /*
  58. * Used to allocate 32-bit handles for _DRM_SHM regions
  59. * The 0x10000000 value is chosen to be out of the way of
  60. * FB/register and GART physical addresses.
  61. */
  62. static unsigned int map32_handle = 0x10000000;
  63. #endif
  64. /**
  65. * Ioctl to specify a range of memory that is available for mapping by a non-root process.
  66. *
  67. * \param inode device inode.
  68. * \param filp file pointer.
  69. * \param cmd command.
  70. * \param arg pointer to a drm_map structure.
  71. * \return zero on success or a negative value on error.
  72. *
  73. * Adjusts the memory offset to its absolute value according to the mapping
  74. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  75. * applicable and if supported by the kernel.
  76. */
  77. int drm_addmap( struct inode *inode, struct file *filp,
  78. unsigned int cmd, unsigned long arg )
  79. {
  80. drm_file_t *priv = filp->private_data;
  81. drm_device_t *dev = priv->head->dev;
  82. drm_map_t *map;
  83. drm_map_t __user *argp = (void __user *)arg;
  84. drm_map_list_t *list;
  85. if ( !(filp->f_mode & 3) ) return -EACCES; /* Require read/write */
  86. map = drm_alloc( sizeof(*map), DRM_MEM_MAPS );
  87. if ( !map )
  88. return -ENOMEM;
  89. if ( copy_from_user( map, argp, sizeof(*map) ) ) {
  90. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  91. return -EFAULT;
  92. }
  93. /* Only allow shared memory to be removable since we only keep enough
  94. * book keeping information about shared memory to allow for removal
  95. * when processes fork.
  96. */
  97. if ( (map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM ) {
  98. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  99. return -EINVAL;
  100. }
  101. DRM_DEBUG( "offset = 0x%08lx, size = 0x%08lx, type = %d\n",
  102. map->offset, map->size, map->type );
  103. if ( (map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK)) ) {
  104. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  105. return -EINVAL;
  106. }
  107. map->mtrr = -1;
  108. map->handle = NULL;
  109. switch ( map->type ) {
  110. case _DRM_REGISTERS:
  111. case _DRM_FRAME_BUFFER:
  112. #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__)
  113. if ( map->offset + map->size < map->offset ||
  114. map->offset < virt_to_phys(high_memory) ) {
  115. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  116. return -EINVAL;
  117. }
  118. #endif
  119. #ifdef __alpha__
  120. map->offset += dev->hose->mem_space->start;
  121. #endif
  122. if (drm_core_has_MTRR(dev)) {
  123. if ( map->type == _DRM_FRAME_BUFFER ||
  124. (map->flags & _DRM_WRITE_COMBINING) ) {
  125. map->mtrr = mtrr_add( map->offset, map->size,
  126. MTRR_TYPE_WRCOMB, 1 );
  127. }
  128. }
  129. if (map->type == _DRM_REGISTERS)
  130. map->handle = drm_ioremap( map->offset, map->size,
  131. dev );
  132. break;
  133. case _DRM_SHM:
  134. map->handle = vmalloc_32(map->size);
  135. DRM_DEBUG( "%lu %d %p\n",
  136. map->size, drm_order( map->size ), map->handle );
  137. if ( !map->handle ) {
  138. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  139. return -ENOMEM;
  140. }
  141. map->offset = (unsigned long)map->handle;
  142. if ( map->flags & _DRM_CONTAINS_LOCK ) {
  143. /* Prevent a 2nd X Server from creating a 2nd lock */
  144. if (dev->lock.hw_lock != NULL) {
  145. vfree( map->handle );
  146. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  147. return -EBUSY;
  148. }
  149. dev->sigdata.lock =
  150. dev->lock.hw_lock = map->handle; /* Pointer to lock */
  151. }
  152. break;
  153. case _DRM_AGP:
  154. if (drm_core_has_AGP(dev)) {
  155. #ifdef __alpha__
  156. map->offset += dev->hose->mem_space->start;
  157. #endif
  158. map->offset += dev->agp->base;
  159. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  160. }
  161. break;
  162. case _DRM_SCATTER_GATHER:
  163. if (!dev->sg) {
  164. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  165. return -EINVAL;
  166. }
  167. map->offset += dev->sg->handle;
  168. break;
  169. default:
  170. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  171. return -EINVAL;
  172. }
  173. list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
  174. if(!list) {
  175. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  176. return -EINVAL;
  177. }
  178. memset(list, 0, sizeof(*list));
  179. list->map = map;
  180. down(&dev->struct_sem);
  181. list_add(&list->head, &dev->maplist->head);
  182. #ifdef CONFIG_COMPAT
  183. /* Assign a 32-bit handle for _DRM_SHM mappings */
  184. /* We do it here so that dev->struct_sem protects the increment */
  185. if (map->type == _DRM_SHM)
  186. map->offset = map32_handle += PAGE_SIZE;
  187. #endif
  188. up(&dev->struct_sem);
  189. if ( copy_to_user( argp, map, sizeof(*map) ) )
  190. return -EFAULT;
  191. if (copy_to_user(&argp->handle, &map->offset, sizeof(map->offset)))
  192. return -EFAULT;
  193. return 0;
  194. }
  195. /**
  196. * Remove a map private from list and deallocate resources if the mapping
  197. * isn't in use.
  198. *
  199. * \param inode device inode.
  200. * \param filp file pointer.
  201. * \param cmd command.
  202. * \param arg pointer to a drm_map_t structure.
  203. * \return zero on success or a negative value on error.
  204. *
  205. * Searches the map on drm_device::maplist, removes it from the list, see if
  206. * its being used, and free any associate resource (such as MTRR's) if it's not
  207. * being on use.
  208. *
  209. * \sa addmap().
  210. */
  211. int drm_rmmap(struct inode *inode, struct file *filp,
  212. unsigned int cmd, unsigned long arg)
  213. {
  214. drm_file_t *priv = filp->private_data;
  215. drm_device_t *dev = priv->head->dev;
  216. struct list_head *list;
  217. drm_map_list_t *r_list = NULL;
  218. drm_vma_entry_t *pt, *prev;
  219. drm_map_t *map;
  220. drm_map_t request;
  221. int found_maps = 0;
  222. if (copy_from_user(&request, (drm_map_t __user *)arg,
  223. sizeof(request))) {
  224. return -EFAULT;
  225. }
  226. down(&dev->struct_sem);
  227. list = &dev->maplist->head;
  228. list_for_each(list, &dev->maplist->head) {
  229. r_list = list_entry(list, drm_map_list_t, head);
  230. if(r_list->map &&
  231. r_list->map->offset == (unsigned long) request.handle &&
  232. r_list->map->flags & _DRM_REMOVABLE) break;
  233. }
  234. /* List has wrapped around to the head pointer, or its empty we didn't
  235. * find anything.
  236. */
  237. if(list == (&dev->maplist->head)) {
  238. up(&dev->struct_sem);
  239. return -EINVAL;
  240. }
  241. map = r_list->map;
  242. list_del(list);
  243. drm_free(list, sizeof(*list), DRM_MEM_MAPS);
  244. for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
  245. if (pt->vma->vm_private_data == map) found_maps++;
  246. }
  247. if(!found_maps) {
  248. switch (map->type) {
  249. case _DRM_REGISTERS:
  250. case _DRM_FRAME_BUFFER:
  251. if (drm_core_has_MTRR(dev)) {
  252. if (map->mtrr >= 0) {
  253. int retcode;
  254. retcode = mtrr_del(map->mtrr,
  255. map->offset,
  256. map->size);
  257. DRM_DEBUG("mtrr_del = %d\n", retcode);
  258. }
  259. }
  260. drm_ioremapfree(map->handle, map->size, dev);
  261. break;
  262. case _DRM_SHM:
  263. vfree(map->handle);
  264. break;
  265. case _DRM_AGP:
  266. case _DRM_SCATTER_GATHER:
  267. break;
  268. }
  269. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  270. }
  271. up(&dev->struct_sem);
  272. return 0;
  273. }
  274. /**
  275. * Cleanup after an error on one of the addbufs() functions.
  276. *
  277. * \param entry buffer entry where the error occurred.
  278. *
  279. * Frees any pages and buffers associated with the given entry.
  280. */
  281. static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
  282. {
  283. int i;
  284. if (entry->seg_count) {
  285. for (i = 0; i < entry->seg_count; i++) {
  286. if (entry->seglist[i]) {
  287. drm_free_pages(entry->seglist[i],
  288. entry->page_order,
  289. DRM_MEM_DMA);
  290. }
  291. }
  292. drm_free(entry->seglist,
  293. entry->seg_count *
  294. sizeof(*entry->seglist),
  295. DRM_MEM_SEGS);
  296. entry->seg_count = 0;
  297. }
  298. if (entry->buf_count) {
  299. for (i = 0; i < entry->buf_count; i++) {
  300. if (entry->buflist[i].dev_private) {
  301. drm_free(entry->buflist[i].dev_private,
  302. entry->buflist[i].dev_priv_size,
  303. DRM_MEM_BUFS);
  304. }
  305. }
  306. drm_free(entry->buflist,
  307. entry->buf_count *
  308. sizeof(*entry->buflist),
  309. DRM_MEM_BUFS);
  310. entry->buf_count = 0;
  311. }
  312. }
  313. #if __OS_HAS_AGP
  314. /**
  315. * Add AGP buffers for DMA transfers (ioctl).
  316. *
  317. * \param inode device inode.
  318. * \param filp file pointer.
  319. * \param cmd command.
  320. * \param arg pointer to a drm_buf_desc_t request.
  321. * \return zero on success or a negative number on failure.
  322. *
  323. * After some sanity checks creates a drm_buf structure for each buffer and
  324. * reallocates the buffer list of the same size order to accommodate the new
  325. * buffers.
  326. */
  327. int drm_addbufs_agp( struct inode *inode, struct file *filp,
  328. unsigned int cmd, unsigned long arg )
  329. {
  330. drm_file_t *priv = filp->private_data;
  331. drm_device_t *dev = priv->head->dev;
  332. drm_device_dma_t *dma = dev->dma;
  333. drm_buf_desc_t request;
  334. drm_buf_entry_t *entry;
  335. drm_buf_t *buf;
  336. unsigned long offset;
  337. unsigned long agp_offset;
  338. int count;
  339. int order;
  340. int size;
  341. int alignment;
  342. int page_order;
  343. int total;
  344. int byte_count;
  345. int i;
  346. drm_buf_t **temp_buflist;
  347. drm_buf_desc_t __user *argp = (void __user *)arg;
  348. if ( !dma ) return -EINVAL;
  349. if ( copy_from_user( &request, argp,
  350. sizeof(request) ) )
  351. return -EFAULT;
  352. count = request.count;
  353. order = drm_order( request.size );
  354. size = 1 << order;
  355. alignment = (request.flags & _DRM_PAGE_ALIGN)
  356. ? PAGE_ALIGN(size) : size;
  357. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  358. total = PAGE_SIZE << page_order;
  359. byte_count = 0;
  360. agp_offset = dev->agp->base + request.agp_start;
  361. DRM_DEBUG( "count: %d\n", count );
  362. DRM_DEBUG( "order: %d\n", order );
  363. DRM_DEBUG( "size: %d\n", size );
  364. DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
  365. DRM_DEBUG( "alignment: %d\n", alignment );
  366. DRM_DEBUG( "page_order: %d\n", page_order );
  367. DRM_DEBUG( "total: %d\n", total );
  368. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  369. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  370. spin_lock( &dev->count_lock );
  371. if ( dev->buf_use ) {
  372. spin_unlock( &dev->count_lock );
  373. return -EBUSY;
  374. }
  375. atomic_inc( &dev->buf_alloc );
  376. spin_unlock( &dev->count_lock );
  377. down( &dev->struct_sem );
  378. entry = &dma->bufs[order];
  379. if ( entry->buf_count ) {
  380. up( &dev->struct_sem );
  381. atomic_dec( &dev->buf_alloc );
  382. return -ENOMEM; /* May only call once for each order */
  383. }
  384. if (count < 0 || count > 4096) {
  385. up( &dev->struct_sem );
  386. atomic_dec( &dev->buf_alloc );
  387. return -EINVAL;
  388. }
  389. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  390. DRM_MEM_BUFS );
  391. if ( !entry->buflist ) {
  392. up( &dev->struct_sem );
  393. atomic_dec( &dev->buf_alloc );
  394. return -ENOMEM;
  395. }
  396. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  397. entry->buf_size = size;
  398. entry->page_order = page_order;
  399. offset = 0;
  400. while ( entry->buf_count < count ) {
  401. buf = &entry->buflist[entry->buf_count];
  402. buf->idx = dma->buf_count + entry->buf_count;
  403. buf->total = alignment;
  404. buf->order = order;
  405. buf->used = 0;
  406. buf->offset = (dma->byte_count + offset);
  407. buf->bus_address = agp_offset + offset;
  408. buf->address = (void *)(agp_offset + offset);
  409. buf->next = NULL;
  410. buf->waiting = 0;
  411. buf->pending = 0;
  412. init_waitqueue_head( &buf->dma_wait );
  413. buf->filp = NULL;
  414. buf->dev_priv_size = dev->driver->dev_priv_size;
  415. buf->dev_private = drm_alloc( buf->dev_priv_size,
  416. DRM_MEM_BUFS );
  417. if(!buf->dev_private) {
  418. /* Set count correctly so we free the proper amount. */
  419. entry->buf_count = count;
  420. drm_cleanup_buf_error(dev,entry);
  421. up( &dev->struct_sem );
  422. atomic_dec( &dev->buf_alloc );
  423. return -ENOMEM;
  424. }
  425. memset( buf->dev_private, 0, buf->dev_priv_size );
  426. DRM_DEBUG( "buffer %d @ %p\n",
  427. entry->buf_count, buf->address );
  428. offset += alignment;
  429. entry->buf_count++;
  430. byte_count += PAGE_SIZE << page_order;
  431. }
  432. DRM_DEBUG( "byte_count: %d\n", byte_count );
  433. temp_buflist = drm_realloc( dma->buflist,
  434. dma->buf_count * sizeof(*dma->buflist),
  435. (dma->buf_count + entry->buf_count)
  436. * sizeof(*dma->buflist),
  437. DRM_MEM_BUFS );
  438. if(!temp_buflist) {
  439. /* Free the entry because it isn't valid */
  440. drm_cleanup_buf_error(dev,entry);
  441. up( &dev->struct_sem );
  442. atomic_dec( &dev->buf_alloc );
  443. return -ENOMEM;
  444. }
  445. dma->buflist = temp_buflist;
  446. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  447. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  448. }
  449. dma->buf_count += entry->buf_count;
  450. dma->byte_count += byte_count;
  451. DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
  452. DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
  453. up( &dev->struct_sem );
  454. request.count = entry->buf_count;
  455. request.size = size;
  456. if ( copy_to_user( argp, &request, sizeof(request) ) )
  457. return -EFAULT;
  458. dma->flags = _DRM_DMA_USE_AGP;
  459. atomic_dec( &dev->buf_alloc );
  460. return 0;
  461. }
  462. #endif /* __OS_HAS_AGP */
  463. int drm_addbufs_pci( struct inode *inode, struct file *filp,
  464. unsigned int cmd, unsigned long arg )
  465. {
  466. drm_file_t *priv = filp->private_data;
  467. drm_device_t *dev = priv->head->dev;
  468. drm_device_dma_t *dma = dev->dma;
  469. drm_buf_desc_t request;
  470. int count;
  471. int order;
  472. int size;
  473. int total;
  474. int page_order;
  475. drm_buf_entry_t *entry;
  476. unsigned long page;
  477. drm_buf_t *buf;
  478. int alignment;
  479. unsigned long offset;
  480. int i;
  481. int byte_count;
  482. int page_count;
  483. unsigned long *temp_pagelist;
  484. drm_buf_t **temp_buflist;
  485. drm_buf_desc_t __user *argp = (void __user *)arg;
  486. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) return -EINVAL;
  487. if ( !dma ) return -EINVAL;
  488. if ( copy_from_user( &request, argp, sizeof(request) ) )
  489. return -EFAULT;
  490. count = request.count;
  491. order = drm_order( request.size );
  492. size = 1 << order;
  493. DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  494. request.count, request.size, size,
  495. order, dev->queue_count );
  496. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  497. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  498. alignment = (request.flags & _DRM_PAGE_ALIGN)
  499. ? PAGE_ALIGN(size) : size;
  500. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  501. total = PAGE_SIZE << page_order;
  502. spin_lock( &dev->count_lock );
  503. if ( dev->buf_use ) {
  504. spin_unlock( &dev->count_lock );
  505. return -EBUSY;
  506. }
  507. atomic_inc( &dev->buf_alloc );
  508. spin_unlock( &dev->count_lock );
  509. down( &dev->struct_sem );
  510. entry = &dma->bufs[order];
  511. if ( entry->buf_count ) {
  512. up( &dev->struct_sem );
  513. atomic_dec( &dev->buf_alloc );
  514. return -ENOMEM; /* May only call once for each order */
  515. }
  516. if (count < 0 || count > 4096) {
  517. up( &dev->struct_sem );
  518. atomic_dec( &dev->buf_alloc );
  519. return -EINVAL;
  520. }
  521. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  522. DRM_MEM_BUFS );
  523. if ( !entry->buflist ) {
  524. up( &dev->struct_sem );
  525. atomic_dec( &dev->buf_alloc );
  526. return -ENOMEM;
  527. }
  528. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  529. entry->seglist = drm_alloc( count * sizeof(*entry->seglist),
  530. DRM_MEM_SEGS );
  531. if ( !entry->seglist ) {
  532. drm_free( entry->buflist,
  533. count * sizeof(*entry->buflist),
  534. DRM_MEM_BUFS );
  535. up( &dev->struct_sem );
  536. atomic_dec( &dev->buf_alloc );
  537. return -ENOMEM;
  538. }
  539. memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
  540. /* Keep the original pagelist until we know all the allocations
  541. * have succeeded
  542. */
  543. temp_pagelist = drm_alloc( (dma->page_count + (count << page_order))
  544. * sizeof(*dma->pagelist),
  545. DRM_MEM_PAGES );
  546. if (!temp_pagelist) {
  547. drm_free( entry->buflist,
  548. count * sizeof(*entry->buflist),
  549. DRM_MEM_BUFS );
  550. drm_free( entry->seglist,
  551. count * sizeof(*entry->seglist),
  552. DRM_MEM_SEGS );
  553. up( &dev->struct_sem );
  554. atomic_dec( &dev->buf_alloc );
  555. return -ENOMEM;
  556. }
  557. memcpy(temp_pagelist,
  558. dma->pagelist,
  559. dma->page_count * sizeof(*dma->pagelist));
  560. DRM_DEBUG( "pagelist: %d entries\n",
  561. dma->page_count + (count << page_order) );
  562. entry->buf_size = size;
  563. entry->page_order = page_order;
  564. byte_count = 0;
  565. page_count = 0;
  566. while ( entry->buf_count < count ) {
  567. page = drm_alloc_pages( page_order, DRM_MEM_DMA );
  568. if ( !page ) {
  569. /* Set count correctly so we free the proper amount. */
  570. entry->buf_count = count;
  571. entry->seg_count = count;
  572. drm_cleanup_buf_error(dev, entry);
  573. drm_free( temp_pagelist,
  574. (dma->page_count + (count << page_order))
  575. * sizeof(*dma->pagelist),
  576. DRM_MEM_PAGES );
  577. up( &dev->struct_sem );
  578. atomic_dec( &dev->buf_alloc );
  579. return -ENOMEM;
  580. }
  581. entry->seglist[entry->seg_count++] = page;
  582. for ( i = 0 ; i < (1 << page_order) ; i++ ) {
  583. DRM_DEBUG( "page %d @ 0x%08lx\n",
  584. dma->page_count + page_count,
  585. page + PAGE_SIZE * i );
  586. temp_pagelist[dma->page_count + page_count++]
  587. = page + PAGE_SIZE * i;
  588. }
  589. for ( offset = 0 ;
  590. offset + size <= total && entry->buf_count < count ;
  591. offset += alignment, ++entry->buf_count ) {
  592. buf = &entry->buflist[entry->buf_count];
  593. buf->idx = dma->buf_count + entry->buf_count;
  594. buf->total = alignment;
  595. buf->order = order;
  596. buf->used = 0;
  597. buf->offset = (dma->byte_count + byte_count + offset);
  598. buf->address = (void *)(page + offset);
  599. buf->next = NULL;
  600. buf->waiting = 0;
  601. buf->pending = 0;
  602. init_waitqueue_head( &buf->dma_wait );
  603. buf->filp = NULL;
  604. buf->dev_priv_size = dev->driver->dev_priv_size;
  605. buf->dev_private = drm_alloc( buf->dev_priv_size,
  606. DRM_MEM_BUFS );
  607. if(!buf->dev_private) {
  608. /* Set count correctly so we free the proper amount. */
  609. entry->buf_count = count;
  610. entry->seg_count = count;
  611. drm_cleanup_buf_error(dev,entry);
  612. drm_free( temp_pagelist,
  613. (dma->page_count + (count << page_order))
  614. * sizeof(*dma->pagelist),
  615. DRM_MEM_PAGES );
  616. up( &dev->struct_sem );
  617. atomic_dec( &dev->buf_alloc );
  618. return -ENOMEM;
  619. }
  620. memset( buf->dev_private, 0, buf->dev_priv_size );
  621. DRM_DEBUG( "buffer %d @ %p\n",
  622. entry->buf_count, buf->address );
  623. }
  624. byte_count += PAGE_SIZE << page_order;
  625. }
  626. temp_buflist = drm_realloc( dma->buflist,
  627. dma->buf_count * sizeof(*dma->buflist),
  628. (dma->buf_count + entry->buf_count)
  629. * sizeof(*dma->buflist),
  630. DRM_MEM_BUFS );
  631. if (!temp_buflist) {
  632. /* Free the entry because it isn't valid */
  633. drm_cleanup_buf_error(dev,entry);
  634. drm_free( temp_pagelist,
  635. (dma->page_count + (count << page_order))
  636. * sizeof(*dma->pagelist),
  637. DRM_MEM_PAGES );
  638. up( &dev->struct_sem );
  639. atomic_dec( &dev->buf_alloc );
  640. return -ENOMEM;
  641. }
  642. dma->buflist = temp_buflist;
  643. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  644. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  645. }
  646. /* No allocations failed, so now we can replace the orginal pagelist
  647. * with the new one.
  648. */
  649. if (dma->page_count) {
  650. drm_free(dma->pagelist,
  651. dma->page_count * sizeof(*dma->pagelist),
  652. DRM_MEM_PAGES);
  653. }
  654. dma->pagelist = temp_pagelist;
  655. dma->buf_count += entry->buf_count;
  656. dma->seg_count += entry->seg_count;
  657. dma->page_count += entry->seg_count << page_order;
  658. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  659. up( &dev->struct_sem );
  660. request.count = entry->buf_count;
  661. request.size = size;
  662. if ( copy_to_user( argp, &request, sizeof(request) ) )
  663. return -EFAULT;
  664. atomic_dec( &dev->buf_alloc );
  665. return 0;
  666. }
  667. int drm_addbufs_sg( struct inode *inode, struct file *filp,
  668. unsigned int cmd, unsigned long arg )
  669. {
  670. drm_file_t *priv = filp->private_data;
  671. drm_device_t *dev = priv->head->dev;
  672. drm_device_dma_t *dma = dev->dma;
  673. drm_buf_desc_t __user *argp = (void __user *)arg;
  674. drm_buf_desc_t request;
  675. drm_buf_entry_t *entry;
  676. drm_buf_t *buf;
  677. unsigned long offset;
  678. unsigned long agp_offset;
  679. int count;
  680. int order;
  681. int size;
  682. int alignment;
  683. int page_order;
  684. int total;
  685. int byte_count;
  686. int i;
  687. drm_buf_t **temp_buflist;
  688. if (!drm_core_check_feature(dev, DRIVER_SG)) return -EINVAL;
  689. if ( !dma ) return -EINVAL;
  690. if ( copy_from_user( &request, argp, sizeof(request) ) )
  691. return -EFAULT;
  692. count = request.count;
  693. order = drm_order( request.size );
  694. size = 1 << order;
  695. alignment = (request.flags & _DRM_PAGE_ALIGN)
  696. ? PAGE_ALIGN(size) : size;
  697. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  698. total = PAGE_SIZE << page_order;
  699. byte_count = 0;
  700. agp_offset = request.agp_start;
  701. DRM_DEBUG( "count: %d\n", count );
  702. DRM_DEBUG( "order: %d\n", order );
  703. DRM_DEBUG( "size: %d\n", size );
  704. DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
  705. DRM_DEBUG( "alignment: %d\n", alignment );
  706. DRM_DEBUG( "page_order: %d\n", page_order );
  707. DRM_DEBUG( "total: %d\n", total );
  708. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  709. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  710. spin_lock( &dev->count_lock );
  711. if ( dev->buf_use ) {
  712. spin_unlock( &dev->count_lock );
  713. return -EBUSY;
  714. }
  715. atomic_inc( &dev->buf_alloc );
  716. spin_unlock( &dev->count_lock );
  717. down( &dev->struct_sem );
  718. entry = &dma->bufs[order];
  719. if ( entry->buf_count ) {
  720. up( &dev->struct_sem );
  721. atomic_dec( &dev->buf_alloc );
  722. return -ENOMEM; /* May only call once for each order */
  723. }
  724. if (count < 0 || count > 4096) {
  725. up( &dev->struct_sem );
  726. atomic_dec( &dev->buf_alloc );
  727. return -EINVAL;
  728. }
  729. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  730. DRM_MEM_BUFS );
  731. if ( !entry->buflist ) {
  732. up( &dev->struct_sem );
  733. atomic_dec( &dev->buf_alloc );
  734. return -ENOMEM;
  735. }
  736. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  737. entry->buf_size = size;
  738. entry->page_order = page_order;
  739. offset = 0;
  740. while ( entry->buf_count < count ) {
  741. buf = &entry->buflist[entry->buf_count];
  742. buf->idx = dma->buf_count + entry->buf_count;
  743. buf->total = alignment;
  744. buf->order = order;
  745. buf->used = 0;
  746. buf->offset = (dma->byte_count + offset);
  747. buf->bus_address = agp_offset + offset;
  748. buf->address = (void *)(agp_offset + offset + dev->sg->handle);
  749. buf->next = NULL;
  750. buf->waiting = 0;
  751. buf->pending = 0;
  752. init_waitqueue_head( &buf->dma_wait );
  753. buf->filp = NULL;
  754. buf->dev_priv_size = dev->driver->dev_priv_size;
  755. buf->dev_private = drm_alloc( buf->dev_priv_size,
  756. DRM_MEM_BUFS );
  757. if(!buf->dev_private) {
  758. /* Set count correctly so we free the proper amount. */
  759. entry->buf_count = count;
  760. drm_cleanup_buf_error(dev,entry);
  761. up( &dev->struct_sem );
  762. atomic_dec( &dev->buf_alloc );
  763. return -ENOMEM;
  764. }
  765. memset( buf->dev_private, 0, buf->dev_priv_size );
  766. DRM_DEBUG( "buffer %d @ %p\n",
  767. entry->buf_count, buf->address );
  768. offset += alignment;
  769. entry->buf_count++;
  770. byte_count += PAGE_SIZE << page_order;
  771. }
  772. DRM_DEBUG( "byte_count: %d\n", byte_count );
  773. temp_buflist = drm_realloc( dma->buflist,
  774. dma->buf_count * sizeof(*dma->buflist),
  775. (dma->buf_count + entry->buf_count)
  776. * sizeof(*dma->buflist),
  777. DRM_MEM_BUFS );
  778. if(!temp_buflist) {
  779. /* Free the entry because it isn't valid */
  780. drm_cleanup_buf_error(dev,entry);
  781. up( &dev->struct_sem );
  782. atomic_dec( &dev->buf_alloc );
  783. return -ENOMEM;
  784. }
  785. dma->buflist = temp_buflist;
  786. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  787. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  788. }
  789. dma->buf_count += entry->buf_count;
  790. dma->byte_count += byte_count;
  791. DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
  792. DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
  793. up( &dev->struct_sem );
  794. request.count = entry->buf_count;
  795. request.size = size;
  796. if ( copy_to_user( argp, &request, sizeof(request) ) )
  797. return -EFAULT;
  798. dma->flags = _DRM_DMA_USE_SG;
  799. atomic_dec( &dev->buf_alloc );
  800. return 0;
  801. }
  802. /**
  803. * Add buffers for DMA transfers (ioctl).
  804. *
  805. * \param inode device inode.
  806. * \param filp file pointer.
  807. * \param cmd command.
  808. * \param arg pointer to a drm_buf_desc_t request.
  809. * \return zero on success or a negative number on failure.
  810. *
  811. * According with the memory type specified in drm_buf_desc::flags and the
  812. * build options, it dispatches the call either to addbufs_agp(),
  813. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  814. * PCI memory respectively.
  815. */
  816. int drm_addbufs( struct inode *inode, struct file *filp,
  817. unsigned int cmd, unsigned long arg )
  818. {
  819. drm_buf_desc_t request;
  820. drm_file_t *priv = filp->private_data;
  821. drm_device_t *dev = priv->head->dev;
  822. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  823. return -EINVAL;
  824. if ( copy_from_user( &request, (drm_buf_desc_t __user *)arg,
  825. sizeof(request) ) )
  826. return -EFAULT;
  827. #if __OS_HAS_AGP
  828. if ( request.flags & _DRM_AGP_BUFFER )
  829. return drm_addbufs_agp( inode, filp, cmd, arg );
  830. else
  831. #endif
  832. if ( request.flags & _DRM_SG_BUFFER )
  833. return drm_addbufs_sg( inode, filp, cmd, arg );
  834. else
  835. return drm_addbufs_pci( inode, filp, cmd, arg );
  836. }
  837. /**
  838. * Get information about the buffer mappings.
  839. *
  840. * This was originally mean for debugging purposes, or by a sophisticated
  841. * client library to determine how best to use the available buffers (e.g.,
  842. * large buffers can be used for image transfer).
  843. *
  844. * \param inode device inode.
  845. * \param filp file pointer.
  846. * \param cmd command.
  847. * \param arg pointer to a drm_buf_info structure.
  848. * \return zero on success or a negative number on failure.
  849. *
  850. * Increments drm_device::buf_use while holding the drm_device::count_lock
  851. * lock, preventing of allocating more buffers after this call. Information
  852. * about each requested buffer is then copied into user space.
  853. */
  854. int drm_infobufs( struct inode *inode, struct file *filp,
  855. unsigned int cmd, unsigned long arg )
  856. {
  857. drm_file_t *priv = filp->private_data;
  858. drm_device_t *dev = priv->head->dev;
  859. drm_device_dma_t *dma = dev->dma;
  860. drm_buf_info_t request;
  861. drm_buf_info_t __user *argp = (void __user *)arg;
  862. int i;
  863. int count;
  864. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  865. return -EINVAL;
  866. if ( !dma ) return -EINVAL;
  867. spin_lock( &dev->count_lock );
  868. if ( atomic_read( &dev->buf_alloc ) ) {
  869. spin_unlock( &dev->count_lock );
  870. return -EBUSY;
  871. }
  872. ++dev->buf_use; /* Can't allocate more after this call */
  873. spin_unlock( &dev->count_lock );
  874. if ( copy_from_user( &request, argp, sizeof(request) ) )
  875. return -EFAULT;
  876. for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
  877. if ( dma->bufs[i].buf_count ) ++count;
  878. }
  879. DRM_DEBUG( "count = %d\n", count );
  880. if ( request.count >= count ) {
  881. for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
  882. if ( dma->bufs[i].buf_count ) {
  883. drm_buf_desc_t __user *to = &request.list[count];
  884. drm_buf_entry_t *from = &dma->bufs[i];
  885. drm_freelist_t *list = &dma->bufs[i].freelist;
  886. if ( copy_to_user( &to->count,
  887. &from->buf_count,
  888. sizeof(from->buf_count) ) ||
  889. copy_to_user( &to->size,
  890. &from->buf_size,
  891. sizeof(from->buf_size) ) ||
  892. copy_to_user( &to->low_mark,
  893. &list->low_mark,
  894. sizeof(list->low_mark) ) ||
  895. copy_to_user( &to->high_mark,
  896. &list->high_mark,
  897. sizeof(list->high_mark) ) )
  898. return -EFAULT;
  899. DRM_DEBUG( "%d %d %d %d %d\n",
  900. i,
  901. dma->bufs[i].buf_count,
  902. dma->bufs[i].buf_size,
  903. dma->bufs[i].freelist.low_mark,
  904. dma->bufs[i].freelist.high_mark );
  905. ++count;
  906. }
  907. }
  908. }
  909. request.count = count;
  910. if ( copy_to_user( argp, &request, sizeof(request) ) )
  911. return -EFAULT;
  912. return 0;
  913. }
  914. /**
  915. * Specifies a low and high water mark for buffer allocation
  916. *
  917. * \param inode device inode.
  918. * \param filp file pointer.
  919. * \param cmd command.
  920. * \param arg a pointer to a drm_buf_desc structure.
  921. * \return zero on success or a negative number on failure.
  922. *
  923. * Verifies that the size order is bounded between the admissible orders and
  924. * updates the respective drm_device_dma::bufs entry low and high water mark.
  925. *
  926. * \note This ioctl is deprecated and mostly never used.
  927. */
  928. int drm_markbufs( struct inode *inode, struct file *filp,
  929. unsigned int cmd, unsigned long arg )
  930. {
  931. drm_file_t *priv = filp->private_data;
  932. drm_device_t *dev = priv->head->dev;
  933. drm_device_dma_t *dma = dev->dma;
  934. drm_buf_desc_t request;
  935. int order;
  936. drm_buf_entry_t *entry;
  937. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  938. return -EINVAL;
  939. if ( !dma ) return -EINVAL;
  940. if ( copy_from_user( &request,
  941. (drm_buf_desc_t __user *)arg,
  942. sizeof(request) ) )
  943. return -EFAULT;
  944. DRM_DEBUG( "%d, %d, %d\n",
  945. request.size, request.low_mark, request.high_mark );
  946. order = drm_order( request.size );
  947. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  948. entry = &dma->bufs[order];
  949. if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
  950. return -EINVAL;
  951. if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
  952. return -EINVAL;
  953. entry->freelist.low_mark = request.low_mark;
  954. entry->freelist.high_mark = request.high_mark;
  955. return 0;
  956. }
  957. /**
  958. * Unreserve the buffers in list, previously reserved using drmDMA.
  959. *
  960. * \param inode device inode.
  961. * \param filp file pointer.
  962. * \param cmd command.
  963. * \param arg pointer to a drm_buf_free structure.
  964. * \return zero on success or a negative number on failure.
  965. *
  966. * Calls free_buffer() for each used buffer.
  967. * This function is primarily used for debugging.
  968. */
  969. int drm_freebufs( struct inode *inode, struct file *filp,
  970. unsigned int cmd, unsigned long arg )
  971. {
  972. drm_file_t *priv = filp->private_data;
  973. drm_device_t *dev = priv->head->dev;
  974. drm_device_dma_t *dma = dev->dma;
  975. drm_buf_free_t request;
  976. int i;
  977. int idx;
  978. drm_buf_t *buf;
  979. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  980. return -EINVAL;
  981. if ( !dma ) return -EINVAL;
  982. if ( copy_from_user( &request,
  983. (drm_buf_free_t __user *)arg,
  984. sizeof(request) ) )
  985. return -EFAULT;
  986. DRM_DEBUG( "%d\n", request.count );
  987. for ( i = 0 ; i < request.count ; i++ ) {
  988. if ( copy_from_user( &idx,
  989. &request.list[i],
  990. sizeof(idx) ) )
  991. return -EFAULT;
  992. if ( idx < 0 || idx >= dma->buf_count ) {
  993. DRM_ERROR( "Index %d (of %d max)\n",
  994. idx, dma->buf_count - 1 );
  995. return -EINVAL;
  996. }
  997. buf = dma->buflist[idx];
  998. if ( buf->filp != filp ) {
  999. DRM_ERROR( "Process %d freeing buffer not owned\n",
  1000. current->pid );
  1001. return -EINVAL;
  1002. }
  1003. drm_free_buffer( dev, buf );
  1004. }
  1005. return 0;
  1006. }
  1007. /**
  1008. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1009. *
  1010. * \param inode device inode.
  1011. * \param filp file pointer.
  1012. * \param cmd command.
  1013. * \param arg pointer to a drm_buf_map structure.
  1014. * \return zero on success or a negative number on failure.
  1015. *
  1016. * Maps the AGP or SG buffer region with do_mmap(), and copies information
  1017. * about each buffer into user space. The PCI buffers are already mapped on the
  1018. * addbufs_pci() call.
  1019. */
  1020. int drm_mapbufs( struct inode *inode, struct file *filp,
  1021. unsigned int cmd, unsigned long arg )
  1022. {
  1023. drm_file_t *priv = filp->private_data;
  1024. drm_device_t *dev = priv->head->dev;
  1025. drm_device_dma_t *dma = dev->dma;
  1026. drm_buf_map_t __user *argp = (void __user *)arg;
  1027. int retcode = 0;
  1028. const int zero = 0;
  1029. unsigned long virtual;
  1030. unsigned long address;
  1031. drm_buf_map_t request;
  1032. int i;
  1033. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1034. return -EINVAL;
  1035. if ( !dma ) return -EINVAL;
  1036. spin_lock( &dev->count_lock );
  1037. if ( atomic_read( &dev->buf_alloc ) ) {
  1038. spin_unlock( &dev->count_lock );
  1039. return -EBUSY;
  1040. }
  1041. dev->buf_use++; /* Can't allocate more after this call */
  1042. spin_unlock( &dev->count_lock );
  1043. if ( copy_from_user( &request, argp, sizeof(request) ) )
  1044. return -EFAULT;
  1045. if ( request.count >= dma->buf_count ) {
  1046. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
  1047. (drm_core_check_feature(dev, DRIVER_SG) && (dma->flags & _DRM_DMA_USE_SG)) ) {
  1048. drm_map_t *map = dev->agp_buffer_map;
  1049. if ( !map ) {
  1050. retcode = -EINVAL;
  1051. goto done;
  1052. }
  1053. #if LINUX_VERSION_CODE <= 0x020402
  1054. down( &current->mm->mmap_sem );
  1055. #else
  1056. down_write( &current->mm->mmap_sem );
  1057. #endif
  1058. virtual = do_mmap( filp, 0, map->size,
  1059. PROT_READ | PROT_WRITE,
  1060. MAP_SHARED,
  1061. (unsigned long)map->offset );
  1062. #if LINUX_VERSION_CODE <= 0x020402
  1063. up( &current->mm->mmap_sem );
  1064. #else
  1065. up_write( &current->mm->mmap_sem );
  1066. #endif
  1067. } else {
  1068. #if LINUX_VERSION_CODE <= 0x020402
  1069. down( &current->mm->mmap_sem );
  1070. #else
  1071. down_write( &current->mm->mmap_sem );
  1072. #endif
  1073. virtual = do_mmap( filp, 0, dma->byte_count,
  1074. PROT_READ | PROT_WRITE,
  1075. MAP_SHARED, 0 );
  1076. #if LINUX_VERSION_CODE <= 0x020402
  1077. up( &current->mm->mmap_sem );
  1078. #else
  1079. up_write( &current->mm->mmap_sem );
  1080. #endif
  1081. }
  1082. if ( virtual > -1024UL ) {
  1083. /* Real error */
  1084. retcode = (signed long)virtual;
  1085. goto done;
  1086. }
  1087. request.virtual = (void __user *)virtual;
  1088. for ( i = 0 ; i < dma->buf_count ; i++ ) {
  1089. if ( copy_to_user( &request.list[i].idx,
  1090. &dma->buflist[i]->idx,
  1091. sizeof(request.list[0].idx) ) ) {
  1092. retcode = -EFAULT;
  1093. goto done;
  1094. }
  1095. if ( copy_to_user( &request.list[i].total,
  1096. &dma->buflist[i]->total,
  1097. sizeof(request.list[0].total) ) ) {
  1098. retcode = -EFAULT;
  1099. goto done;
  1100. }
  1101. if ( copy_to_user( &request.list[i].used,
  1102. &zero,
  1103. sizeof(zero) ) ) {
  1104. retcode = -EFAULT;
  1105. goto done;
  1106. }
  1107. address = virtual + dma->buflist[i]->offset; /* *** */
  1108. if ( copy_to_user( &request.list[i].address,
  1109. &address,
  1110. sizeof(address) ) ) {
  1111. retcode = -EFAULT;
  1112. goto done;
  1113. }
  1114. }
  1115. }
  1116. done:
  1117. request.count = dma->buf_count;
  1118. DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
  1119. if ( copy_to_user( argp, &request, sizeof(request) ) )
  1120. return -EFAULT;
  1121. return retcode;
  1122. }