drm_bufs.c 38 KB

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