drm_bufs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. case _DRM_CONSISTENT:
  170. {
  171. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  172. * As we're limit the address to 2^32-1 (or lses),
  173. * casting it down to 32 bits is no problem, but we
  174. * need to point to a 64bit variable first. */
  175. dma_addr_t bus_addr;
  176. map->handle = drm_pci_alloc(dev, map->size, map->size,
  177. 0xffffffffUL, &bus_addr);
  178. map->offset = (unsigned long)bus_addr;
  179. if (!map->handle) {
  180. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  181. return -ENOMEM;
  182. }
  183. break;
  184. }
  185. default:
  186. drm_free( map, sizeof(*map), DRM_MEM_MAPS );
  187. return -EINVAL;
  188. }
  189. list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
  190. if(!list) {
  191. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  192. return -EINVAL;
  193. }
  194. memset(list, 0, sizeof(*list));
  195. list->map = map;
  196. down(&dev->struct_sem);
  197. list_add(&list->head, &dev->maplist->head);
  198. #ifdef CONFIG_COMPAT
  199. /* Assign a 32-bit handle for _DRM_SHM mappings */
  200. /* We do it here so that dev->struct_sem protects the increment */
  201. if (map->type == _DRM_SHM)
  202. map->offset = map32_handle += PAGE_SIZE;
  203. #endif
  204. up(&dev->struct_sem);
  205. if ( copy_to_user( argp, map, sizeof(*map) ) )
  206. return -EFAULT;
  207. if (copy_to_user(&argp->handle, &map->offset, sizeof(map->offset)))
  208. return -EFAULT;
  209. return 0;
  210. }
  211. /**
  212. * Remove a map private from list and deallocate resources if the mapping
  213. * isn't in use.
  214. *
  215. * \param inode device inode.
  216. * \param filp file pointer.
  217. * \param cmd command.
  218. * \param arg pointer to a drm_map_t structure.
  219. * \return zero on success or a negative value on error.
  220. *
  221. * Searches the map on drm_device::maplist, removes it from the list, see if
  222. * its being used, and free any associate resource (such as MTRR's) if it's not
  223. * being on use.
  224. *
  225. * \sa addmap().
  226. */
  227. int drm_rmmap(struct inode *inode, struct file *filp,
  228. unsigned int cmd, unsigned long arg)
  229. {
  230. drm_file_t *priv = filp->private_data;
  231. drm_device_t *dev = priv->head->dev;
  232. struct list_head *list;
  233. drm_map_list_t *r_list = NULL;
  234. drm_vma_entry_t *pt, *prev;
  235. drm_map_t *map;
  236. drm_map_t request;
  237. int found_maps = 0;
  238. if (copy_from_user(&request, (drm_map_t __user *)arg,
  239. sizeof(request))) {
  240. return -EFAULT;
  241. }
  242. down(&dev->struct_sem);
  243. list = &dev->maplist->head;
  244. list_for_each(list, &dev->maplist->head) {
  245. r_list = list_entry(list, drm_map_list_t, head);
  246. if(r_list->map &&
  247. r_list->map->offset == (unsigned long) request.handle &&
  248. r_list->map->flags & _DRM_REMOVABLE) break;
  249. }
  250. /* List has wrapped around to the head pointer, or its empty we didn't
  251. * find anything.
  252. */
  253. if(list == (&dev->maplist->head)) {
  254. up(&dev->struct_sem);
  255. return -EINVAL;
  256. }
  257. map = r_list->map;
  258. list_del(list);
  259. drm_free(list, sizeof(*list), DRM_MEM_MAPS);
  260. for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
  261. if (pt->vma->vm_private_data == map) found_maps++;
  262. }
  263. if(!found_maps) {
  264. switch (map->type) {
  265. case _DRM_REGISTERS:
  266. case _DRM_FRAME_BUFFER:
  267. if (drm_core_has_MTRR(dev)) {
  268. if (map->mtrr >= 0) {
  269. int retcode;
  270. retcode = mtrr_del(map->mtrr,
  271. map->offset,
  272. map->size);
  273. DRM_DEBUG("mtrr_del = %d\n", retcode);
  274. }
  275. }
  276. drm_ioremapfree(map->handle, map->size, dev);
  277. break;
  278. case _DRM_SHM:
  279. vfree(map->handle);
  280. break;
  281. case _DRM_AGP:
  282. case _DRM_SCATTER_GATHER:
  283. break;
  284. case _DRM_CONSISTENT:
  285. drm_pci_free(dev, map->size, map->handle, map->offset);
  286. break;
  287. }
  288. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  289. }
  290. up(&dev->struct_sem);
  291. return 0;
  292. }
  293. /**
  294. * Cleanup after an error on one of the addbufs() functions.
  295. *
  296. * \param entry buffer entry where the error occurred.
  297. *
  298. * Frees any pages and buffers associated with the given entry.
  299. */
  300. static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
  301. {
  302. int i;
  303. if (entry->seg_count) {
  304. for (i = 0; i < entry->seg_count; i++) {
  305. if (entry->seglist[i]) {
  306. drm_free_pages(entry->seglist[i],
  307. entry->page_order,
  308. DRM_MEM_DMA);
  309. }
  310. }
  311. drm_free(entry->seglist,
  312. entry->seg_count *
  313. sizeof(*entry->seglist),
  314. DRM_MEM_SEGS);
  315. entry->seg_count = 0;
  316. }
  317. if (entry->buf_count) {
  318. for (i = 0; i < entry->buf_count; i++) {
  319. if (entry->buflist[i].dev_private) {
  320. drm_free(entry->buflist[i].dev_private,
  321. entry->buflist[i].dev_priv_size,
  322. DRM_MEM_BUFS);
  323. }
  324. }
  325. drm_free(entry->buflist,
  326. entry->buf_count *
  327. sizeof(*entry->buflist),
  328. DRM_MEM_BUFS);
  329. entry->buf_count = 0;
  330. }
  331. }
  332. #if __OS_HAS_AGP
  333. /**
  334. * Add AGP buffers for DMA transfers (ioctl).
  335. *
  336. * \param inode device inode.
  337. * \param filp file pointer.
  338. * \param cmd command.
  339. * \param arg pointer to a drm_buf_desc_t request.
  340. * \return zero on success or a negative number on failure.
  341. *
  342. * After some sanity checks creates a drm_buf structure for each buffer and
  343. * reallocates the buffer list of the same size order to accommodate the new
  344. * buffers.
  345. */
  346. static int drm_addbufs_agp( struct inode *inode, struct file *filp,
  347. unsigned int cmd, unsigned long arg )
  348. {
  349. drm_file_t *priv = filp->private_data;
  350. drm_device_t *dev = priv->head->dev;
  351. drm_device_dma_t *dma = dev->dma;
  352. drm_buf_desc_t request;
  353. drm_buf_entry_t *entry;
  354. drm_buf_t *buf;
  355. unsigned long offset;
  356. unsigned long agp_offset;
  357. int count;
  358. int order;
  359. int size;
  360. int alignment;
  361. int page_order;
  362. int total;
  363. int byte_count;
  364. int i;
  365. drm_buf_t **temp_buflist;
  366. drm_buf_desc_t __user *argp = (void __user *)arg;
  367. if ( !dma ) return -EINVAL;
  368. if ( copy_from_user( &request, argp,
  369. sizeof(request) ) )
  370. return -EFAULT;
  371. count = request.count;
  372. order = drm_order( request.size );
  373. size = 1 << order;
  374. alignment = (request.flags & _DRM_PAGE_ALIGN)
  375. ? PAGE_ALIGN(size) : size;
  376. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  377. total = PAGE_SIZE << page_order;
  378. byte_count = 0;
  379. agp_offset = dev->agp->base + request.agp_start;
  380. DRM_DEBUG( "count: %d\n", count );
  381. DRM_DEBUG( "order: %d\n", order );
  382. DRM_DEBUG( "size: %d\n", size );
  383. DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
  384. DRM_DEBUG( "alignment: %d\n", alignment );
  385. DRM_DEBUG( "page_order: %d\n", page_order );
  386. DRM_DEBUG( "total: %d\n", total );
  387. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  388. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  389. spin_lock( &dev->count_lock );
  390. if ( dev->buf_use ) {
  391. spin_unlock( &dev->count_lock );
  392. return -EBUSY;
  393. }
  394. atomic_inc( &dev->buf_alloc );
  395. spin_unlock( &dev->count_lock );
  396. down( &dev->struct_sem );
  397. entry = &dma->bufs[order];
  398. if ( entry->buf_count ) {
  399. up( &dev->struct_sem );
  400. atomic_dec( &dev->buf_alloc );
  401. return -ENOMEM; /* May only call once for each order */
  402. }
  403. if (count < 0 || count > 4096) {
  404. up( &dev->struct_sem );
  405. atomic_dec( &dev->buf_alloc );
  406. return -EINVAL;
  407. }
  408. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  409. DRM_MEM_BUFS );
  410. if ( !entry->buflist ) {
  411. up( &dev->struct_sem );
  412. atomic_dec( &dev->buf_alloc );
  413. return -ENOMEM;
  414. }
  415. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  416. entry->buf_size = size;
  417. entry->page_order = page_order;
  418. offset = 0;
  419. while ( entry->buf_count < count ) {
  420. buf = &entry->buflist[entry->buf_count];
  421. buf->idx = dma->buf_count + entry->buf_count;
  422. buf->total = alignment;
  423. buf->order = order;
  424. buf->used = 0;
  425. buf->offset = (dma->byte_count + offset);
  426. buf->bus_address = agp_offset + offset;
  427. buf->address = (void *)(agp_offset + offset);
  428. buf->next = NULL;
  429. buf->waiting = 0;
  430. buf->pending = 0;
  431. init_waitqueue_head( &buf->dma_wait );
  432. buf->filp = NULL;
  433. buf->dev_priv_size = dev->driver->dev_priv_size;
  434. buf->dev_private = drm_alloc( buf->dev_priv_size,
  435. DRM_MEM_BUFS );
  436. if(!buf->dev_private) {
  437. /* Set count correctly so we free the proper amount. */
  438. entry->buf_count = count;
  439. drm_cleanup_buf_error(dev,entry);
  440. up( &dev->struct_sem );
  441. atomic_dec( &dev->buf_alloc );
  442. return -ENOMEM;
  443. }
  444. memset( buf->dev_private, 0, buf->dev_priv_size );
  445. DRM_DEBUG( "buffer %d @ %p\n",
  446. entry->buf_count, buf->address );
  447. offset += alignment;
  448. entry->buf_count++;
  449. byte_count += PAGE_SIZE << page_order;
  450. }
  451. DRM_DEBUG( "byte_count: %d\n", byte_count );
  452. temp_buflist = drm_realloc( dma->buflist,
  453. dma->buf_count * sizeof(*dma->buflist),
  454. (dma->buf_count + entry->buf_count)
  455. * sizeof(*dma->buflist),
  456. DRM_MEM_BUFS );
  457. if(!temp_buflist) {
  458. /* Free the entry because it isn't valid */
  459. drm_cleanup_buf_error(dev,entry);
  460. up( &dev->struct_sem );
  461. atomic_dec( &dev->buf_alloc );
  462. return -ENOMEM;
  463. }
  464. dma->buflist = temp_buflist;
  465. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  466. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  467. }
  468. dma->buf_count += entry->buf_count;
  469. dma->byte_count += byte_count;
  470. DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
  471. DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
  472. up( &dev->struct_sem );
  473. request.count = entry->buf_count;
  474. request.size = size;
  475. if ( copy_to_user( argp, &request, sizeof(request) ) )
  476. return -EFAULT;
  477. dma->flags = _DRM_DMA_USE_AGP;
  478. atomic_dec( &dev->buf_alloc );
  479. return 0;
  480. }
  481. #endif /* __OS_HAS_AGP */
  482. static int drm_addbufs_pci( struct inode *inode, struct file *filp,
  483. unsigned int cmd, unsigned long arg )
  484. {
  485. drm_file_t *priv = filp->private_data;
  486. drm_device_t *dev = priv->head->dev;
  487. drm_device_dma_t *dma = dev->dma;
  488. drm_buf_desc_t request;
  489. int count;
  490. int order;
  491. int size;
  492. int total;
  493. int page_order;
  494. drm_buf_entry_t *entry;
  495. unsigned long page;
  496. drm_buf_t *buf;
  497. int alignment;
  498. unsigned long offset;
  499. int i;
  500. int byte_count;
  501. int page_count;
  502. unsigned long *temp_pagelist;
  503. drm_buf_t **temp_buflist;
  504. drm_buf_desc_t __user *argp = (void __user *)arg;
  505. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) return -EINVAL;
  506. if ( !dma ) return -EINVAL;
  507. if ( copy_from_user( &request, argp, sizeof(request) ) )
  508. return -EFAULT;
  509. count = request.count;
  510. order = drm_order( request.size );
  511. size = 1 << order;
  512. DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  513. request.count, request.size, size,
  514. order, dev->queue_count );
  515. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  516. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  517. alignment = (request.flags & _DRM_PAGE_ALIGN)
  518. ? PAGE_ALIGN(size) : size;
  519. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  520. total = PAGE_SIZE << page_order;
  521. spin_lock( &dev->count_lock );
  522. if ( dev->buf_use ) {
  523. spin_unlock( &dev->count_lock );
  524. return -EBUSY;
  525. }
  526. atomic_inc( &dev->buf_alloc );
  527. spin_unlock( &dev->count_lock );
  528. down( &dev->struct_sem );
  529. entry = &dma->bufs[order];
  530. if ( entry->buf_count ) {
  531. up( &dev->struct_sem );
  532. atomic_dec( &dev->buf_alloc );
  533. return -ENOMEM; /* May only call once for each order */
  534. }
  535. if (count < 0 || count > 4096) {
  536. up( &dev->struct_sem );
  537. atomic_dec( &dev->buf_alloc );
  538. return -EINVAL;
  539. }
  540. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  541. DRM_MEM_BUFS );
  542. if ( !entry->buflist ) {
  543. up( &dev->struct_sem );
  544. atomic_dec( &dev->buf_alloc );
  545. return -ENOMEM;
  546. }
  547. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  548. entry->seglist = drm_alloc( count * sizeof(*entry->seglist),
  549. DRM_MEM_SEGS );
  550. if ( !entry->seglist ) {
  551. drm_free( entry->buflist,
  552. count * sizeof(*entry->buflist),
  553. DRM_MEM_BUFS );
  554. up( &dev->struct_sem );
  555. atomic_dec( &dev->buf_alloc );
  556. return -ENOMEM;
  557. }
  558. memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
  559. /* Keep the original pagelist until we know all the allocations
  560. * have succeeded
  561. */
  562. temp_pagelist = drm_alloc( (dma->page_count + (count << page_order))
  563. * sizeof(*dma->pagelist),
  564. DRM_MEM_PAGES );
  565. if (!temp_pagelist) {
  566. drm_free( entry->buflist,
  567. count * sizeof(*entry->buflist),
  568. DRM_MEM_BUFS );
  569. drm_free( entry->seglist,
  570. count * sizeof(*entry->seglist),
  571. DRM_MEM_SEGS );
  572. up( &dev->struct_sem );
  573. atomic_dec( &dev->buf_alloc );
  574. return -ENOMEM;
  575. }
  576. memcpy(temp_pagelist,
  577. dma->pagelist,
  578. dma->page_count * sizeof(*dma->pagelist));
  579. DRM_DEBUG( "pagelist: %d entries\n",
  580. dma->page_count + (count << page_order) );
  581. entry->buf_size = size;
  582. entry->page_order = page_order;
  583. byte_count = 0;
  584. page_count = 0;
  585. while ( entry->buf_count < count ) {
  586. page = drm_alloc_pages( page_order, DRM_MEM_DMA );
  587. if ( !page ) {
  588. /* Set count correctly so we free the proper amount. */
  589. entry->buf_count = count;
  590. entry->seg_count = count;
  591. drm_cleanup_buf_error(dev, entry);
  592. drm_free( temp_pagelist,
  593. (dma->page_count + (count << page_order))
  594. * sizeof(*dma->pagelist),
  595. DRM_MEM_PAGES );
  596. up( &dev->struct_sem );
  597. atomic_dec( &dev->buf_alloc );
  598. return -ENOMEM;
  599. }
  600. entry->seglist[entry->seg_count++] = page;
  601. for ( i = 0 ; i < (1 << page_order) ; i++ ) {
  602. DRM_DEBUG( "page %d @ 0x%08lx\n",
  603. dma->page_count + page_count,
  604. page + PAGE_SIZE * i );
  605. temp_pagelist[dma->page_count + page_count++]
  606. = page + PAGE_SIZE * i;
  607. }
  608. for ( offset = 0 ;
  609. offset + size <= total && entry->buf_count < count ;
  610. offset += alignment, ++entry->buf_count ) {
  611. buf = &entry->buflist[entry->buf_count];
  612. buf->idx = dma->buf_count + entry->buf_count;
  613. buf->total = alignment;
  614. buf->order = order;
  615. buf->used = 0;
  616. buf->offset = (dma->byte_count + byte_count + offset);
  617. buf->address = (void *)(page + offset);
  618. buf->next = NULL;
  619. buf->waiting = 0;
  620. buf->pending = 0;
  621. init_waitqueue_head( &buf->dma_wait );
  622. buf->filp = NULL;
  623. buf->dev_priv_size = dev->driver->dev_priv_size;
  624. buf->dev_private = drm_alloc( buf->dev_priv_size,
  625. DRM_MEM_BUFS );
  626. if(!buf->dev_private) {
  627. /* Set count correctly so we free the proper amount. */
  628. entry->buf_count = count;
  629. entry->seg_count = count;
  630. drm_cleanup_buf_error(dev,entry);
  631. drm_free( temp_pagelist,
  632. (dma->page_count + (count << page_order))
  633. * sizeof(*dma->pagelist),
  634. DRM_MEM_PAGES );
  635. up( &dev->struct_sem );
  636. atomic_dec( &dev->buf_alloc );
  637. return -ENOMEM;
  638. }
  639. memset( buf->dev_private, 0, buf->dev_priv_size );
  640. DRM_DEBUG( "buffer %d @ %p\n",
  641. entry->buf_count, buf->address );
  642. }
  643. byte_count += PAGE_SIZE << page_order;
  644. }
  645. temp_buflist = drm_realloc( dma->buflist,
  646. dma->buf_count * sizeof(*dma->buflist),
  647. (dma->buf_count + entry->buf_count)
  648. * sizeof(*dma->buflist),
  649. DRM_MEM_BUFS );
  650. if (!temp_buflist) {
  651. /* Free the entry because it isn't valid */
  652. drm_cleanup_buf_error(dev,entry);
  653. drm_free( temp_pagelist,
  654. (dma->page_count + (count << page_order))
  655. * sizeof(*dma->pagelist),
  656. DRM_MEM_PAGES );
  657. up( &dev->struct_sem );
  658. atomic_dec( &dev->buf_alloc );
  659. return -ENOMEM;
  660. }
  661. dma->buflist = temp_buflist;
  662. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  663. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  664. }
  665. /* No allocations failed, so now we can replace the orginal pagelist
  666. * with the new one.
  667. */
  668. if (dma->page_count) {
  669. drm_free(dma->pagelist,
  670. dma->page_count * sizeof(*dma->pagelist),
  671. DRM_MEM_PAGES);
  672. }
  673. dma->pagelist = temp_pagelist;
  674. dma->buf_count += entry->buf_count;
  675. dma->seg_count += entry->seg_count;
  676. dma->page_count += entry->seg_count << page_order;
  677. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  678. up( &dev->struct_sem );
  679. request.count = entry->buf_count;
  680. request.size = size;
  681. if ( copy_to_user( argp, &request, sizeof(request) ) )
  682. return -EFAULT;
  683. atomic_dec( &dev->buf_alloc );
  684. return 0;
  685. }
  686. static int drm_addbufs_sg( struct inode *inode, struct file *filp,
  687. unsigned int cmd, unsigned long arg )
  688. {
  689. drm_file_t *priv = filp->private_data;
  690. drm_device_t *dev = priv->head->dev;
  691. drm_device_dma_t *dma = dev->dma;
  692. drm_buf_desc_t __user *argp = (void __user *)arg;
  693. drm_buf_desc_t request;
  694. drm_buf_entry_t *entry;
  695. drm_buf_t *buf;
  696. unsigned long offset;
  697. unsigned long agp_offset;
  698. int count;
  699. int order;
  700. int size;
  701. int alignment;
  702. int page_order;
  703. int total;
  704. int byte_count;
  705. int i;
  706. drm_buf_t **temp_buflist;
  707. if (!drm_core_check_feature(dev, DRIVER_SG)) return -EINVAL;
  708. if ( !dma ) return -EINVAL;
  709. if ( copy_from_user( &request, argp, sizeof(request) ) )
  710. return -EFAULT;
  711. count = request.count;
  712. order = drm_order( request.size );
  713. size = 1 << order;
  714. alignment = (request.flags & _DRM_PAGE_ALIGN)
  715. ? PAGE_ALIGN(size) : size;
  716. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  717. total = PAGE_SIZE << page_order;
  718. byte_count = 0;
  719. agp_offset = request.agp_start;
  720. DRM_DEBUG( "count: %d\n", count );
  721. DRM_DEBUG( "order: %d\n", order );
  722. DRM_DEBUG( "size: %d\n", size );
  723. DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
  724. DRM_DEBUG( "alignment: %d\n", alignment );
  725. DRM_DEBUG( "page_order: %d\n", page_order );
  726. DRM_DEBUG( "total: %d\n", total );
  727. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  728. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  729. spin_lock( &dev->count_lock );
  730. if ( dev->buf_use ) {
  731. spin_unlock( &dev->count_lock );
  732. return -EBUSY;
  733. }
  734. atomic_inc( &dev->buf_alloc );
  735. spin_unlock( &dev->count_lock );
  736. down( &dev->struct_sem );
  737. entry = &dma->bufs[order];
  738. if ( entry->buf_count ) {
  739. up( &dev->struct_sem );
  740. atomic_dec( &dev->buf_alloc );
  741. return -ENOMEM; /* May only call once for each order */
  742. }
  743. if (count < 0 || count > 4096) {
  744. up( &dev->struct_sem );
  745. atomic_dec( &dev->buf_alloc );
  746. return -EINVAL;
  747. }
  748. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  749. DRM_MEM_BUFS );
  750. if ( !entry->buflist ) {
  751. up( &dev->struct_sem );
  752. atomic_dec( &dev->buf_alloc );
  753. return -ENOMEM;
  754. }
  755. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  756. entry->buf_size = size;
  757. entry->page_order = page_order;
  758. offset = 0;
  759. while ( entry->buf_count < count ) {
  760. buf = &entry->buflist[entry->buf_count];
  761. buf->idx = dma->buf_count + entry->buf_count;
  762. buf->total = alignment;
  763. buf->order = order;
  764. buf->used = 0;
  765. buf->offset = (dma->byte_count + offset);
  766. buf->bus_address = agp_offset + offset;
  767. buf->address = (void *)(agp_offset + offset + dev->sg->handle);
  768. buf->next = NULL;
  769. buf->waiting = 0;
  770. buf->pending = 0;
  771. init_waitqueue_head( &buf->dma_wait );
  772. buf->filp = NULL;
  773. buf->dev_priv_size = dev->driver->dev_priv_size;
  774. buf->dev_private = drm_alloc( buf->dev_priv_size,
  775. DRM_MEM_BUFS );
  776. if(!buf->dev_private) {
  777. /* Set count correctly so we free the proper amount. */
  778. entry->buf_count = count;
  779. drm_cleanup_buf_error(dev,entry);
  780. up( &dev->struct_sem );
  781. atomic_dec( &dev->buf_alloc );
  782. return -ENOMEM;
  783. }
  784. memset( buf->dev_private, 0, buf->dev_priv_size );
  785. DRM_DEBUG( "buffer %d @ %p\n",
  786. entry->buf_count, buf->address );
  787. offset += alignment;
  788. entry->buf_count++;
  789. byte_count += PAGE_SIZE << page_order;
  790. }
  791. DRM_DEBUG( "byte_count: %d\n", byte_count );
  792. temp_buflist = drm_realloc( dma->buflist,
  793. dma->buf_count * sizeof(*dma->buflist),
  794. (dma->buf_count + entry->buf_count)
  795. * sizeof(*dma->buflist),
  796. DRM_MEM_BUFS );
  797. if(!temp_buflist) {
  798. /* Free the entry because it isn't valid */
  799. drm_cleanup_buf_error(dev,entry);
  800. up( &dev->struct_sem );
  801. atomic_dec( &dev->buf_alloc );
  802. return -ENOMEM;
  803. }
  804. dma->buflist = temp_buflist;
  805. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  806. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  807. }
  808. dma->buf_count += entry->buf_count;
  809. dma->byte_count += byte_count;
  810. DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
  811. DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
  812. up( &dev->struct_sem );
  813. request.count = entry->buf_count;
  814. request.size = size;
  815. if ( copy_to_user( argp, &request, sizeof(request) ) )
  816. return -EFAULT;
  817. dma->flags = _DRM_DMA_USE_SG;
  818. atomic_dec( &dev->buf_alloc );
  819. return 0;
  820. }
  821. int drm_addbufs_fb(struct inode *inode, struct file *filp,
  822. unsigned int cmd, unsigned long arg)
  823. {
  824. drm_file_t *priv = filp->private_data;
  825. drm_device_t *dev = priv->head->dev;
  826. drm_device_dma_t *dma = dev->dma;
  827. drm_buf_desc_t request;
  828. drm_buf_entry_t *entry;
  829. drm_buf_t *buf;
  830. unsigned long offset;
  831. unsigned long agp_offset;
  832. int count;
  833. int order;
  834. int size;
  835. int alignment;
  836. int page_order;
  837. int total;
  838. int byte_count;
  839. int i;
  840. drm_buf_t **temp_buflist;
  841. drm_buf_desc_t __user *argp = (void __user *)arg;
  842. if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
  843. return -EINVAL;
  844. if (!dma)
  845. return -EINVAL;
  846. if (copy_from_user(&request, argp, sizeof(request)))
  847. return -EFAULT;
  848. count = request.count;
  849. order = drm_order(request.size);
  850. size = 1 << order;
  851. alignment = (request.flags & _DRM_PAGE_ALIGN)
  852. ? PAGE_ALIGN(size) : size;
  853. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  854. total = PAGE_SIZE << page_order;
  855. byte_count = 0;
  856. agp_offset = request.agp_start;
  857. DRM_DEBUG("count: %d\n", count);
  858. DRM_DEBUG("order: %d\n", order);
  859. DRM_DEBUG("size: %d\n", size);
  860. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  861. DRM_DEBUG("alignment: %d\n", alignment);
  862. DRM_DEBUG("page_order: %d\n", page_order);
  863. DRM_DEBUG("total: %d\n", total);
  864. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  865. return -EINVAL;
  866. if (dev->queue_count)
  867. return -EBUSY; /* Not while in use */
  868. spin_lock(&dev->count_lock);
  869. if (dev->buf_use) {
  870. spin_unlock(&dev->count_lock);
  871. return -EBUSY;
  872. }
  873. atomic_inc(&dev->buf_alloc);
  874. spin_unlock(&dev->count_lock);
  875. down(&dev->struct_sem);
  876. entry = &dma->bufs[order];
  877. if (entry->buf_count) {
  878. up(&dev->struct_sem);
  879. atomic_dec(&dev->buf_alloc);
  880. return -ENOMEM; /* May only call once for each order */
  881. }
  882. if (count < 0 || count > 4096) {
  883. up(&dev->struct_sem);
  884. atomic_dec(&dev->buf_alloc);
  885. return -EINVAL;
  886. }
  887. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  888. DRM_MEM_BUFS);
  889. if (!entry->buflist) {
  890. up(&dev->struct_sem);
  891. atomic_dec(&dev->buf_alloc);
  892. return -ENOMEM;
  893. }
  894. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  895. entry->buf_size = size;
  896. entry->page_order = page_order;
  897. offset = 0;
  898. while (entry->buf_count < count) {
  899. buf = &entry->buflist[entry->buf_count];
  900. buf->idx = dma->buf_count + entry->buf_count;
  901. buf->total = alignment;
  902. buf->order = order;
  903. buf->used = 0;
  904. buf->offset = (dma->byte_count + offset);
  905. buf->bus_address = agp_offset + offset;
  906. buf->address = (void *)(agp_offset + offset);
  907. buf->next = NULL;
  908. buf->waiting = 0;
  909. buf->pending = 0;
  910. init_waitqueue_head(&buf->dma_wait);
  911. buf->filp = NULL;
  912. buf->dev_priv_size = dev->driver->dev_priv_size;
  913. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  914. if (!buf->dev_private) {
  915. /* Set count correctly so we free the proper amount. */
  916. entry->buf_count = count;
  917. drm_cleanup_buf_error(dev, entry);
  918. up(&dev->struct_sem);
  919. atomic_dec(&dev->buf_alloc);
  920. return -ENOMEM;
  921. }
  922. memset(buf->dev_private, 0, buf->dev_priv_size);
  923. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  924. offset += alignment;
  925. entry->buf_count++;
  926. byte_count += PAGE_SIZE << page_order;
  927. }
  928. DRM_DEBUG("byte_count: %d\n", byte_count);
  929. temp_buflist = drm_realloc(dma->buflist,
  930. dma->buf_count * sizeof(*dma->buflist),
  931. (dma->buf_count + entry->buf_count)
  932. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  933. if (!temp_buflist) {
  934. /* Free the entry because it isn't valid */
  935. drm_cleanup_buf_error(dev, entry);
  936. up(&dev->struct_sem);
  937. atomic_dec(&dev->buf_alloc);
  938. return -ENOMEM;
  939. }
  940. dma->buflist = temp_buflist;
  941. for (i = 0; i < entry->buf_count; i++) {
  942. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  943. }
  944. dma->buf_count += entry->buf_count;
  945. dma->byte_count += byte_count;
  946. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  947. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  948. up(&dev->struct_sem);
  949. request.count = entry->buf_count;
  950. request.size = size;
  951. if (copy_to_user(argp, &request, sizeof(request)))
  952. return -EFAULT;
  953. dma->flags = _DRM_DMA_USE_FB;
  954. atomic_dec(&dev->buf_alloc);
  955. return 0;
  956. }
  957. /**
  958. * Add buffers for DMA transfers (ioctl).
  959. *
  960. * \param inode device inode.
  961. * \param filp file pointer.
  962. * \param cmd command.
  963. * \param arg pointer to a drm_buf_desc_t request.
  964. * \return zero on success or a negative number on failure.
  965. *
  966. * According with the memory type specified in drm_buf_desc::flags and the
  967. * build options, it dispatches the call either to addbufs_agp(),
  968. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  969. * PCI memory respectively.
  970. */
  971. int drm_addbufs( struct inode *inode, struct file *filp,
  972. unsigned int cmd, unsigned long arg )
  973. {
  974. drm_buf_desc_t request;
  975. drm_file_t *priv = filp->private_data;
  976. drm_device_t *dev = priv->head->dev;
  977. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  978. return -EINVAL;
  979. if ( copy_from_user( &request, (drm_buf_desc_t __user *)arg,
  980. sizeof(request) ) )
  981. return -EFAULT;
  982. #if __OS_HAS_AGP
  983. if ( request.flags & _DRM_AGP_BUFFER )
  984. return drm_addbufs_agp( inode, filp, cmd, arg );
  985. else
  986. #endif
  987. if ( request.flags & _DRM_SG_BUFFER )
  988. return drm_addbufs_sg( inode, filp, cmd, arg );
  989. else if ( request.flags & _DRM_FB_BUFFER)
  990. return drm_addbufs_fb( inode, filp, cmd, arg );
  991. else
  992. return drm_addbufs_pci( inode, filp, cmd, arg );
  993. }
  994. /**
  995. * Get information about the buffer mappings.
  996. *
  997. * This was originally mean for debugging purposes, or by a sophisticated
  998. * client library to determine how best to use the available buffers (e.g.,
  999. * large buffers can be used for image transfer).
  1000. *
  1001. * \param inode device inode.
  1002. * \param filp file pointer.
  1003. * \param cmd command.
  1004. * \param arg pointer to a drm_buf_info structure.
  1005. * \return zero on success or a negative number on failure.
  1006. *
  1007. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1008. * lock, preventing of allocating more buffers after this call. Information
  1009. * about each requested buffer is then copied into user space.
  1010. */
  1011. int drm_infobufs( struct inode *inode, struct file *filp,
  1012. unsigned int cmd, unsigned long arg )
  1013. {
  1014. drm_file_t *priv = filp->private_data;
  1015. drm_device_t *dev = priv->head->dev;
  1016. drm_device_dma_t *dma = dev->dma;
  1017. drm_buf_info_t request;
  1018. drm_buf_info_t __user *argp = (void __user *)arg;
  1019. int i;
  1020. int count;
  1021. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1022. return -EINVAL;
  1023. if ( !dma ) return -EINVAL;
  1024. spin_lock( &dev->count_lock );
  1025. if ( atomic_read( &dev->buf_alloc ) ) {
  1026. spin_unlock( &dev->count_lock );
  1027. return -EBUSY;
  1028. }
  1029. ++dev->buf_use; /* Can't allocate more after this call */
  1030. spin_unlock( &dev->count_lock );
  1031. if ( copy_from_user( &request, argp, sizeof(request) ) )
  1032. return -EFAULT;
  1033. for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
  1034. if ( dma->bufs[i].buf_count ) ++count;
  1035. }
  1036. DRM_DEBUG( "count = %d\n", count );
  1037. if ( request.count >= count ) {
  1038. for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
  1039. if ( dma->bufs[i].buf_count ) {
  1040. drm_buf_desc_t __user *to = &request.list[count];
  1041. drm_buf_entry_t *from = &dma->bufs[i];
  1042. drm_freelist_t *list = &dma->bufs[i].freelist;
  1043. if ( copy_to_user( &to->count,
  1044. &from->buf_count,
  1045. sizeof(from->buf_count) ) ||
  1046. copy_to_user( &to->size,
  1047. &from->buf_size,
  1048. sizeof(from->buf_size) ) ||
  1049. copy_to_user( &to->low_mark,
  1050. &list->low_mark,
  1051. sizeof(list->low_mark) ) ||
  1052. copy_to_user( &to->high_mark,
  1053. &list->high_mark,
  1054. sizeof(list->high_mark) ) )
  1055. return -EFAULT;
  1056. DRM_DEBUG( "%d %d %d %d %d\n",
  1057. i,
  1058. dma->bufs[i].buf_count,
  1059. dma->bufs[i].buf_size,
  1060. dma->bufs[i].freelist.low_mark,
  1061. dma->bufs[i].freelist.high_mark );
  1062. ++count;
  1063. }
  1064. }
  1065. }
  1066. request.count = count;
  1067. if ( copy_to_user( argp, &request, sizeof(request) ) )
  1068. return -EFAULT;
  1069. return 0;
  1070. }
  1071. /**
  1072. * Specifies a low and high water mark for buffer allocation
  1073. *
  1074. * \param inode device inode.
  1075. * \param filp file pointer.
  1076. * \param cmd command.
  1077. * \param arg a pointer to a drm_buf_desc structure.
  1078. * \return zero on success or a negative number on failure.
  1079. *
  1080. * Verifies that the size order is bounded between the admissible orders and
  1081. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1082. *
  1083. * \note This ioctl is deprecated and mostly never used.
  1084. */
  1085. int drm_markbufs( struct inode *inode, struct file *filp,
  1086. unsigned int cmd, unsigned long arg )
  1087. {
  1088. drm_file_t *priv = filp->private_data;
  1089. drm_device_t *dev = priv->head->dev;
  1090. drm_device_dma_t *dma = dev->dma;
  1091. drm_buf_desc_t request;
  1092. int order;
  1093. drm_buf_entry_t *entry;
  1094. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1095. return -EINVAL;
  1096. if ( !dma ) return -EINVAL;
  1097. if ( copy_from_user( &request,
  1098. (drm_buf_desc_t __user *)arg,
  1099. sizeof(request) ) )
  1100. return -EFAULT;
  1101. DRM_DEBUG( "%d, %d, %d\n",
  1102. request.size, request.low_mark, request.high_mark );
  1103. order = drm_order( request.size );
  1104. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  1105. entry = &dma->bufs[order];
  1106. if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
  1107. return -EINVAL;
  1108. if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
  1109. return -EINVAL;
  1110. entry->freelist.low_mark = request.low_mark;
  1111. entry->freelist.high_mark = request.high_mark;
  1112. return 0;
  1113. }
  1114. /**
  1115. * Unreserve the buffers in list, previously reserved using drmDMA.
  1116. *
  1117. * \param inode device inode.
  1118. * \param filp file pointer.
  1119. * \param cmd command.
  1120. * \param arg pointer to a drm_buf_free structure.
  1121. * \return zero on success or a negative number on failure.
  1122. *
  1123. * Calls free_buffer() for each used buffer.
  1124. * This function is primarily used for debugging.
  1125. */
  1126. int drm_freebufs( struct inode *inode, struct file *filp,
  1127. unsigned int cmd, unsigned long arg )
  1128. {
  1129. drm_file_t *priv = filp->private_data;
  1130. drm_device_t *dev = priv->head->dev;
  1131. drm_device_dma_t *dma = dev->dma;
  1132. drm_buf_free_t request;
  1133. int i;
  1134. int idx;
  1135. drm_buf_t *buf;
  1136. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1137. return -EINVAL;
  1138. if ( !dma ) return -EINVAL;
  1139. if ( copy_from_user( &request,
  1140. (drm_buf_free_t __user *)arg,
  1141. sizeof(request) ) )
  1142. return -EFAULT;
  1143. DRM_DEBUG( "%d\n", request.count );
  1144. for ( i = 0 ; i < request.count ; i++ ) {
  1145. if ( copy_from_user( &idx,
  1146. &request.list[i],
  1147. sizeof(idx) ) )
  1148. return -EFAULT;
  1149. if ( idx < 0 || idx >= dma->buf_count ) {
  1150. DRM_ERROR( "Index %d (of %d max)\n",
  1151. idx, dma->buf_count - 1 );
  1152. return -EINVAL;
  1153. }
  1154. buf = dma->buflist[idx];
  1155. if ( buf->filp != filp ) {
  1156. DRM_ERROR( "Process %d freeing buffer not owned\n",
  1157. current->pid );
  1158. return -EINVAL;
  1159. }
  1160. drm_free_buffer( dev, buf );
  1161. }
  1162. return 0;
  1163. }
  1164. /**
  1165. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1166. *
  1167. * \param inode device inode.
  1168. * \param filp file pointer.
  1169. * \param cmd command.
  1170. * \param arg pointer to a drm_buf_map structure.
  1171. * \return zero on success or a negative number on failure.
  1172. *
  1173. * Maps the AGP or SG buffer region with do_mmap(), and copies information
  1174. * about each buffer into user space. The PCI buffers are already mapped on the
  1175. * addbufs_pci() call.
  1176. */
  1177. int drm_mapbufs( struct inode *inode, struct file *filp,
  1178. unsigned int cmd, unsigned long arg )
  1179. {
  1180. drm_file_t *priv = filp->private_data;
  1181. drm_device_t *dev = priv->head->dev;
  1182. drm_device_dma_t *dma = dev->dma;
  1183. drm_buf_map_t __user *argp = (void __user *)arg;
  1184. int retcode = 0;
  1185. const int zero = 0;
  1186. unsigned long virtual;
  1187. unsigned long address;
  1188. drm_buf_map_t request;
  1189. int i;
  1190. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1191. return -EINVAL;
  1192. if ( !dma ) return -EINVAL;
  1193. spin_lock( &dev->count_lock );
  1194. if ( atomic_read( &dev->buf_alloc ) ) {
  1195. spin_unlock( &dev->count_lock );
  1196. return -EBUSY;
  1197. }
  1198. dev->buf_use++; /* Can't allocate more after this call */
  1199. spin_unlock( &dev->count_lock );
  1200. if ( copy_from_user( &request, argp, sizeof(request) ) )
  1201. return -EFAULT;
  1202. if ( request.count >= dma->buf_count ) {
  1203. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1204. || (drm_core_check_feature(dev, DRIVER_SG)
  1205. && (dma->flags & _DRM_DMA_USE_SG))
  1206. || (drm_core_check_feature(dev, DRIVER_FB_DMA)
  1207. && (dma->flags & _DRM_DMA_USE_FB))) {
  1208. drm_map_t *map = dev->agp_buffer_map;
  1209. if ( !map ) {
  1210. retcode = -EINVAL;
  1211. goto done;
  1212. }
  1213. #if LINUX_VERSION_CODE <= 0x020402
  1214. down( &current->mm->mmap_sem );
  1215. #else
  1216. down_write( &current->mm->mmap_sem );
  1217. #endif
  1218. virtual = do_mmap( filp, 0, map->size,
  1219. PROT_READ | PROT_WRITE,
  1220. MAP_SHARED,
  1221. (unsigned long)map->offset );
  1222. #if LINUX_VERSION_CODE <= 0x020402
  1223. up( &current->mm->mmap_sem );
  1224. #else
  1225. up_write( &current->mm->mmap_sem );
  1226. #endif
  1227. } else {
  1228. #if LINUX_VERSION_CODE <= 0x020402
  1229. down( &current->mm->mmap_sem );
  1230. #else
  1231. down_write( &current->mm->mmap_sem );
  1232. #endif
  1233. virtual = do_mmap( filp, 0, dma->byte_count,
  1234. PROT_READ | PROT_WRITE,
  1235. MAP_SHARED, 0 );
  1236. #if LINUX_VERSION_CODE <= 0x020402
  1237. up( &current->mm->mmap_sem );
  1238. #else
  1239. up_write( &current->mm->mmap_sem );
  1240. #endif
  1241. }
  1242. if ( virtual > -1024UL ) {
  1243. /* Real error */
  1244. retcode = (signed long)virtual;
  1245. goto done;
  1246. }
  1247. request.virtual = (void __user *)virtual;
  1248. for ( i = 0 ; i < dma->buf_count ; i++ ) {
  1249. if ( copy_to_user( &request.list[i].idx,
  1250. &dma->buflist[i]->idx,
  1251. sizeof(request.list[0].idx) ) ) {
  1252. retcode = -EFAULT;
  1253. goto done;
  1254. }
  1255. if ( copy_to_user( &request.list[i].total,
  1256. &dma->buflist[i]->total,
  1257. sizeof(request.list[0].total) ) ) {
  1258. retcode = -EFAULT;
  1259. goto done;
  1260. }
  1261. if ( copy_to_user( &request.list[i].used,
  1262. &zero,
  1263. sizeof(zero) ) ) {
  1264. retcode = -EFAULT;
  1265. goto done;
  1266. }
  1267. address = virtual + dma->buflist[i]->offset; /* *** */
  1268. if ( copy_to_user( &request.list[i].address,
  1269. &address,
  1270. sizeof(address) ) ) {
  1271. retcode = -EFAULT;
  1272. goto done;
  1273. }
  1274. }
  1275. }
  1276. done:
  1277. request.count = dma->buf_count;
  1278. DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
  1279. if ( copy_to_user( argp, &request, sizeof(request) ) )
  1280. return -EFAULT;
  1281. return retcode;
  1282. }