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