drm_bufs.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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. EXPORT_SYMBOL(drm_addbufs_agp);
  500. #endif /* __OS_HAS_AGP */
  501. static int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request)
  502. {
  503. drm_device_dma_t *dma = dev->dma;
  504. int count;
  505. int order;
  506. int size;
  507. int total;
  508. int page_order;
  509. drm_buf_entry_t *entry;
  510. unsigned long page;
  511. drm_buf_t *buf;
  512. int alignment;
  513. unsigned long offset;
  514. int i;
  515. int byte_count;
  516. int page_count;
  517. unsigned long *temp_pagelist;
  518. drm_buf_t **temp_buflist;
  519. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) return -EINVAL;
  520. if ( !dma ) return -EINVAL;
  521. count = request->count;
  522. order = drm_order(request->size);
  523. size = 1 << order;
  524. DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  525. request->count, request->size, size,
  526. order, dev->queue_count );
  527. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  528. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  529. alignment = (request->flags & _DRM_PAGE_ALIGN)
  530. ? PAGE_ALIGN(size) : size;
  531. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  532. total = PAGE_SIZE << page_order;
  533. spin_lock( &dev->count_lock );
  534. if ( dev->buf_use ) {
  535. spin_unlock( &dev->count_lock );
  536. return -EBUSY;
  537. }
  538. atomic_inc( &dev->buf_alloc );
  539. spin_unlock( &dev->count_lock );
  540. down( &dev->struct_sem );
  541. entry = &dma->bufs[order];
  542. if ( entry->buf_count ) {
  543. up( &dev->struct_sem );
  544. atomic_dec( &dev->buf_alloc );
  545. return -ENOMEM; /* May only call once for each order */
  546. }
  547. if (count < 0 || count > 4096) {
  548. up( &dev->struct_sem );
  549. atomic_dec( &dev->buf_alloc );
  550. return -EINVAL;
  551. }
  552. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  553. DRM_MEM_BUFS );
  554. if ( !entry->buflist ) {
  555. up( &dev->struct_sem );
  556. atomic_dec( &dev->buf_alloc );
  557. return -ENOMEM;
  558. }
  559. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  560. entry->seglist = drm_alloc( count * sizeof(*entry->seglist),
  561. DRM_MEM_SEGS );
  562. if ( !entry->seglist ) {
  563. drm_free( entry->buflist,
  564. count * sizeof(*entry->buflist),
  565. DRM_MEM_BUFS );
  566. up( &dev->struct_sem );
  567. atomic_dec( &dev->buf_alloc );
  568. return -ENOMEM;
  569. }
  570. memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
  571. /* Keep the original pagelist until we know all the allocations
  572. * have succeeded
  573. */
  574. temp_pagelist = drm_alloc( (dma->page_count + (count << page_order))
  575. * sizeof(*dma->pagelist),
  576. DRM_MEM_PAGES );
  577. if (!temp_pagelist) {
  578. drm_free( entry->buflist,
  579. count * sizeof(*entry->buflist),
  580. DRM_MEM_BUFS );
  581. drm_free( entry->seglist,
  582. count * sizeof(*entry->seglist),
  583. DRM_MEM_SEGS );
  584. up( &dev->struct_sem );
  585. atomic_dec( &dev->buf_alloc );
  586. return -ENOMEM;
  587. }
  588. memcpy(temp_pagelist,
  589. dma->pagelist,
  590. dma->page_count * sizeof(*dma->pagelist));
  591. DRM_DEBUG( "pagelist: %d entries\n",
  592. dma->page_count + (count << page_order) );
  593. entry->buf_size = size;
  594. entry->page_order = page_order;
  595. byte_count = 0;
  596. page_count = 0;
  597. while ( entry->buf_count < count ) {
  598. page = drm_alloc_pages( page_order, DRM_MEM_DMA );
  599. if ( !page ) {
  600. /* Set count correctly so we free the proper amount. */
  601. entry->buf_count = count;
  602. entry->seg_count = count;
  603. drm_cleanup_buf_error(dev, entry);
  604. drm_free( temp_pagelist,
  605. (dma->page_count + (count << page_order))
  606. * sizeof(*dma->pagelist),
  607. DRM_MEM_PAGES );
  608. up( &dev->struct_sem );
  609. atomic_dec( &dev->buf_alloc );
  610. return -ENOMEM;
  611. }
  612. entry->seglist[entry->seg_count++] = page;
  613. for ( i = 0 ; i < (1 << page_order) ; i++ ) {
  614. DRM_DEBUG( "page %d @ 0x%08lx\n",
  615. dma->page_count + page_count,
  616. page + PAGE_SIZE * i );
  617. temp_pagelist[dma->page_count + page_count++]
  618. = page + PAGE_SIZE * i;
  619. }
  620. for ( offset = 0 ;
  621. offset + size <= total && entry->buf_count < count ;
  622. offset += alignment, ++entry->buf_count ) {
  623. buf = &entry->buflist[entry->buf_count];
  624. buf->idx = dma->buf_count + entry->buf_count;
  625. buf->total = alignment;
  626. buf->order = order;
  627. buf->used = 0;
  628. buf->offset = (dma->byte_count + byte_count + offset);
  629. buf->address = (void *)(page + offset);
  630. buf->next = NULL;
  631. buf->waiting = 0;
  632. buf->pending = 0;
  633. init_waitqueue_head( &buf->dma_wait );
  634. buf->filp = NULL;
  635. buf->dev_priv_size = dev->driver->dev_priv_size;
  636. buf->dev_private = drm_alloc( buf->dev_priv_size,
  637. DRM_MEM_BUFS );
  638. if(!buf->dev_private) {
  639. /* Set count correctly so we free the proper amount. */
  640. entry->buf_count = count;
  641. entry->seg_count = count;
  642. drm_cleanup_buf_error(dev,entry);
  643. drm_free( temp_pagelist,
  644. (dma->page_count + (count << page_order))
  645. * sizeof(*dma->pagelist),
  646. DRM_MEM_PAGES );
  647. up( &dev->struct_sem );
  648. atomic_dec( &dev->buf_alloc );
  649. return -ENOMEM;
  650. }
  651. memset( buf->dev_private, 0, buf->dev_priv_size );
  652. DRM_DEBUG( "buffer %d @ %p\n",
  653. entry->buf_count, buf->address );
  654. }
  655. byte_count += PAGE_SIZE << page_order;
  656. }
  657. temp_buflist = drm_realloc( dma->buflist,
  658. dma->buf_count * sizeof(*dma->buflist),
  659. (dma->buf_count + entry->buf_count)
  660. * sizeof(*dma->buflist),
  661. DRM_MEM_BUFS );
  662. if (!temp_buflist) {
  663. /* Free the entry because it isn't valid */
  664. drm_cleanup_buf_error(dev,entry);
  665. drm_free( temp_pagelist,
  666. (dma->page_count + (count << page_order))
  667. * sizeof(*dma->pagelist),
  668. DRM_MEM_PAGES );
  669. up( &dev->struct_sem );
  670. atomic_dec( &dev->buf_alloc );
  671. return -ENOMEM;
  672. }
  673. dma->buflist = temp_buflist;
  674. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  675. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  676. }
  677. /* No allocations failed, so now we can replace the orginal pagelist
  678. * with the new one.
  679. */
  680. if (dma->page_count) {
  681. drm_free(dma->pagelist,
  682. dma->page_count * sizeof(*dma->pagelist),
  683. DRM_MEM_PAGES);
  684. }
  685. dma->pagelist = temp_pagelist;
  686. dma->buf_count += entry->buf_count;
  687. dma->seg_count += entry->seg_count;
  688. dma->page_count += entry->seg_count << page_order;
  689. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  690. up( &dev->struct_sem );
  691. request->count = entry->buf_count;
  692. request->size = size;
  693. atomic_dec( &dev->buf_alloc );
  694. return 0;
  695. }
  696. EXPORT_SYMBOL(drm_addbufs_pci);
  697. static int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request)
  698. {
  699. drm_device_dma_t *dma = dev->dma;
  700. drm_buf_entry_t *entry;
  701. drm_buf_t *buf;
  702. unsigned long offset;
  703. unsigned long agp_offset;
  704. int count;
  705. int order;
  706. int size;
  707. int alignment;
  708. int page_order;
  709. int total;
  710. int byte_count;
  711. int i;
  712. drm_buf_t **temp_buflist;
  713. if (!drm_core_check_feature(dev, DRIVER_SG)) return -EINVAL;
  714. if ( !dma ) return -EINVAL;
  715. count = request->count;
  716. order = drm_order(request->size);
  717. size = 1 << order;
  718. alignment = (request->flags & _DRM_PAGE_ALIGN)
  719. ? PAGE_ALIGN(size) : size;
  720. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  721. total = PAGE_SIZE << page_order;
  722. byte_count = 0;
  723. agp_offset = request->agp_start;
  724. DRM_DEBUG( "count: %d\n", count );
  725. DRM_DEBUG( "order: %d\n", order );
  726. DRM_DEBUG( "size: %d\n", size );
  727. DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
  728. DRM_DEBUG( "alignment: %d\n", alignment );
  729. DRM_DEBUG( "page_order: %d\n", page_order );
  730. DRM_DEBUG( "total: %d\n", total );
  731. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  732. if ( dev->queue_count ) return -EBUSY; /* Not while in use */
  733. spin_lock( &dev->count_lock );
  734. if ( dev->buf_use ) {
  735. spin_unlock( &dev->count_lock );
  736. return -EBUSY;
  737. }
  738. atomic_inc( &dev->buf_alloc );
  739. spin_unlock( &dev->count_lock );
  740. down( &dev->struct_sem );
  741. entry = &dma->bufs[order];
  742. if ( entry->buf_count ) {
  743. up( &dev->struct_sem );
  744. atomic_dec( &dev->buf_alloc );
  745. return -ENOMEM; /* May only call once for each order */
  746. }
  747. if (count < 0 || count > 4096) {
  748. up( &dev->struct_sem );
  749. atomic_dec( &dev->buf_alloc );
  750. return -EINVAL;
  751. }
  752. entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
  753. DRM_MEM_BUFS );
  754. if ( !entry->buflist ) {
  755. up( &dev->struct_sem );
  756. atomic_dec( &dev->buf_alloc );
  757. return -ENOMEM;
  758. }
  759. memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
  760. entry->buf_size = size;
  761. entry->page_order = page_order;
  762. offset = 0;
  763. while ( entry->buf_count < count ) {
  764. buf = &entry->buflist[entry->buf_count];
  765. buf->idx = dma->buf_count + entry->buf_count;
  766. buf->total = alignment;
  767. buf->order = order;
  768. buf->used = 0;
  769. buf->offset = (dma->byte_count + offset);
  770. buf->bus_address = agp_offset + offset;
  771. buf->address = (void *)(agp_offset + offset + dev->sg->handle);
  772. buf->next = NULL;
  773. buf->waiting = 0;
  774. buf->pending = 0;
  775. init_waitqueue_head( &buf->dma_wait );
  776. buf->filp = NULL;
  777. buf->dev_priv_size = dev->driver->dev_priv_size;
  778. buf->dev_private = drm_alloc( buf->dev_priv_size,
  779. DRM_MEM_BUFS );
  780. if(!buf->dev_private) {
  781. /* Set count correctly so we free the proper amount. */
  782. entry->buf_count = count;
  783. drm_cleanup_buf_error(dev,entry);
  784. up( &dev->struct_sem );
  785. atomic_dec( &dev->buf_alloc );
  786. return -ENOMEM;
  787. }
  788. memset( buf->dev_private, 0, buf->dev_priv_size );
  789. DRM_DEBUG( "buffer %d @ %p\n",
  790. entry->buf_count, buf->address );
  791. offset += alignment;
  792. entry->buf_count++;
  793. byte_count += PAGE_SIZE << page_order;
  794. }
  795. DRM_DEBUG( "byte_count: %d\n", byte_count );
  796. temp_buflist = drm_realloc( dma->buflist,
  797. dma->buf_count * sizeof(*dma->buflist),
  798. (dma->buf_count + entry->buf_count)
  799. * sizeof(*dma->buflist),
  800. DRM_MEM_BUFS );
  801. if(!temp_buflist) {
  802. /* Free the entry because it isn't valid */
  803. drm_cleanup_buf_error(dev,entry);
  804. up( &dev->struct_sem );
  805. atomic_dec( &dev->buf_alloc );
  806. return -ENOMEM;
  807. }
  808. dma->buflist = temp_buflist;
  809. for ( i = 0 ; i < entry->buf_count ; i++ ) {
  810. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  811. }
  812. dma->buf_count += entry->buf_count;
  813. dma->byte_count += byte_count;
  814. DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
  815. DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
  816. up( &dev->struct_sem );
  817. request->count = entry->buf_count;
  818. request->size = size;
  819. dma->flags = _DRM_DMA_USE_SG;
  820. atomic_dec( &dev->buf_alloc );
  821. return 0;
  822. }
  823. int drm_addbufs_fb(drm_device_t *dev, drm_buf_desc_t *request)
  824. {
  825. drm_device_dma_t *dma = dev->dma;
  826. drm_buf_entry_t *entry;
  827. drm_buf_t *buf;
  828. unsigned long offset;
  829. unsigned long agp_offset;
  830. int count;
  831. int order;
  832. int size;
  833. int alignment;
  834. int page_order;
  835. int total;
  836. int byte_count;
  837. int i;
  838. drm_buf_t **temp_buflist;
  839. if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
  840. return -EINVAL;
  841. if (!dma)
  842. return -EINVAL;
  843. count = request->count;
  844. order = drm_order(request->size);
  845. size = 1 << order;
  846. alignment = (request->flags & _DRM_PAGE_ALIGN)
  847. ? PAGE_ALIGN(size) : size;
  848. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  849. total = PAGE_SIZE << page_order;
  850. byte_count = 0;
  851. agp_offset = request->agp_start;
  852. DRM_DEBUG("count: %d\n", count);
  853. DRM_DEBUG("order: %d\n", order);
  854. DRM_DEBUG("size: %d\n", size);
  855. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  856. DRM_DEBUG("alignment: %d\n", alignment);
  857. DRM_DEBUG("page_order: %d\n", page_order);
  858. DRM_DEBUG("total: %d\n", total);
  859. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  860. return -EINVAL;
  861. if (dev->queue_count)
  862. return -EBUSY; /* Not while in use */
  863. spin_lock(&dev->count_lock);
  864. if (dev->buf_use) {
  865. spin_unlock(&dev->count_lock);
  866. return -EBUSY;
  867. }
  868. atomic_inc(&dev->buf_alloc);
  869. spin_unlock(&dev->count_lock);
  870. down(&dev->struct_sem);
  871. entry = &dma->bufs[order];
  872. if (entry->buf_count) {
  873. up(&dev->struct_sem);
  874. atomic_dec(&dev->buf_alloc);
  875. return -ENOMEM; /* May only call once for each order */
  876. }
  877. if (count < 0 || count > 4096) {
  878. up(&dev->struct_sem);
  879. atomic_dec(&dev->buf_alloc);
  880. return -EINVAL;
  881. }
  882. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  883. DRM_MEM_BUFS);
  884. if (!entry->buflist) {
  885. up(&dev->struct_sem);
  886. atomic_dec(&dev->buf_alloc);
  887. return -ENOMEM;
  888. }
  889. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  890. entry->buf_size = size;
  891. entry->page_order = page_order;
  892. offset = 0;
  893. while (entry->buf_count < count) {
  894. buf = &entry->buflist[entry->buf_count];
  895. buf->idx = dma->buf_count + entry->buf_count;
  896. buf->total = alignment;
  897. buf->order = order;
  898. buf->used = 0;
  899. buf->offset = (dma->byte_count + offset);
  900. buf->bus_address = agp_offset + offset;
  901. buf->address = (void *)(agp_offset + offset);
  902. buf->next = NULL;
  903. buf->waiting = 0;
  904. buf->pending = 0;
  905. init_waitqueue_head(&buf->dma_wait);
  906. buf->filp = NULL;
  907. buf->dev_priv_size = dev->driver->dev_priv_size;
  908. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  909. if (!buf->dev_private) {
  910. /* Set count correctly so we free the proper amount. */
  911. entry->buf_count = count;
  912. drm_cleanup_buf_error(dev, entry);
  913. up(&dev->struct_sem);
  914. atomic_dec(&dev->buf_alloc);
  915. return -ENOMEM;
  916. }
  917. memset(buf->dev_private, 0, buf->dev_priv_size);
  918. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  919. offset += alignment;
  920. entry->buf_count++;
  921. byte_count += PAGE_SIZE << page_order;
  922. }
  923. DRM_DEBUG("byte_count: %d\n", byte_count);
  924. temp_buflist = drm_realloc(dma->buflist,
  925. dma->buf_count * sizeof(*dma->buflist),
  926. (dma->buf_count + entry->buf_count)
  927. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  928. if (!temp_buflist) {
  929. /* Free the entry because it isn't valid */
  930. drm_cleanup_buf_error(dev, entry);
  931. up(&dev->struct_sem);
  932. atomic_dec(&dev->buf_alloc);
  933. return -ENOMEM;
  934. }
  935. dma->buflist = temp_buflist;
  936. for (i = 0; i < entry->buf_count; i++) {
  937. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  938. }
  939. dma->buf_count += entry->buf_count;
  940. dma->byte_count += byte_count;
  941. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  942. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  943. up(&dev->struct_sem);
  944. request->count = entry->buf_count;
  945. request->size = size;
  946. dma->flags = _DRM_DMA_USE_FB;
  947. atomic_dec(&dev->buf_alloc);
  948. return 0;
  949. }
  950. /**
  951. * Add buffers for DMA transfers (ioctl).
  952. *
  953. * \param inode device inode.
  954. * \param filp file pointer.
  955. * \param cmd command.
  956. * \param arg pointer to a drm_buf_desc_t request.
  957. * \return zero on success or a negative number on failure.
  958. *
  959. * According with the memory type specified in drm_buf_desc::flags and the
  960. * build options, it dispatches the call either to addbufs_agp(),
  961. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  962. * PCI memory respectively.
  963. */
  964. int drm_addbufs( struct inode *inode, struct file *filp,
  965. unsigned int cmd, unsigned long arg )
  966. {
  967. drm_buf_desc_t request;
  968. drm_file_t *priv = filp->private_data;
  969. drm_device_t *dev = priv->head->dev;
  970. int ret;
  971. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  972. return -EINVAL;
  973. if ( copy_from_user( &request, (drm_buf_desc_t __user *)arg,
  974. sizeof(request) ) )
  975. return -EFAULT;
  976. #if __OS_HAS_AGP
  977. if ( request.flags & _DRM_AGP_BUFFER )
  978. ret=drm_addbufs_agp(dev, &request);
  979. else
  980. #endif
  981. if ( request.flags & _DRM_SG_BUFFER )
  982. ret=drm_addbufs_sg(dev, &request);
  983. else if ( request.flags & _DRM_FB_BUFFER)
  984. ret=drm_addbufs_fb(dev, &request);
  985. else
  986. ret=drm_addbufs_pci(dev, &request);
  987. if (ret==0) {
  988. if (copy_to_user((void __user *)arg, &request,
  989. sizeof(request))) {
  990. ret = -EFAULT;
  991. }
  992. }
  993. return ret;
  994. }
  995. /**
  996. * Get information about the buffer mappings.
  997. *
  998. * This was originally mean for debugging purposes, or by a sophisticated
  999. * client library to determine how best to use the available buffers (e.g.,
  1000. * large buffers can be used for image transfer).
  1001. *
  1002. * \param inode device inode.
  1003. * \param filp file pointer.
  1004. * \param cmd command.
  1005. * \param arg pointer to a drm_buf_info structure.
  1006. * \return zero on success or a negative number on failure.
  1007. *
  1008. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1009. * lock, preventing of allocating more buffers after this call. Information
  1010. * about each requested buffer is then copied into user space.
  1011. */
  1012. int drm_infobufs( struct inode *inode, struct file *filp,
  1013. unsigned int cmd, unsigned long arg )
  1014. {
  1015. drm_file_t *priv = filp->private_data;
  1016. drm_device_t *dev = priv->head->dev;
  1017. drm_device_dma_t *dma = dev->dma;
  1018. drm_buf_info_t request;
  1019. drm_buf_info_t __user *argp = (void __user *)arg;
  1020. int i;
  1021. int count;
  1022. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1023. return -EINVAL;
  1024. if ( !dma ) return -EINVAL;
  1025. spin_lock( &dev->count_lock );
  1026. if ( atomic_read( &dev->buf_alloc ) ) {
  1027. spin_unlock( &dev->count_lock );
  1028. return -EBUSY;
  1029. }
  1030. ++dev->buf_use; /* Can't allocate more after this call */
  1031. spin_unlock( &dev->count_lock );
  1032. if ( copy_from_user( &request, argp, sizeof(request) ) )
  1033. return -EFAULT;
  1034. for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
  1035. if ( dma->bufs[i].buf_count ) ++count;
  1036. }
  1037. DRM_DEBUG( "count = %d\n", count );
  1038. if ( request.count >= count ) {
  1039. for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
  1040. if ( dma->bufs[i].buf_count ) {
  1041. drm_buf_desc_t __user *to = &request.list[count];
  1042. drm_buf_entry_t *from = &dma->bufs[i];
  1043. drm_freelist_t *list = &dma->bufs[i].freelist;
  1044. if ( copy_to_user( &to->count,
  1045. &from->buf_count,
  1046. sizeof(from->buf_count) ) ||
  1047. copy_to_user( &to->size,
  1048. &from->buf_size,
  1049. sizeof(from->buf_size) ) ||
  1050. copy_to_user( &to->low_mark,
  1051. &list->low_mark,
  1052. sizeof(list->low_mark) ) ||
  1053. copy_to_user( &to->high_mark,
  1054. &list->high_mark,
  1055. sizeof(list->high_mark) ) )
  1056. return -EFAULT;
  1057. DRM_DEBUG( "%d %d %d %d %d\n",
  1058. i,
  1059. dma->bufs[i].buf_count,
  1060. dma->bufs[i].buf_size,
  1061. dma->bufs[i].freelist.low_mark,
  1062. dma->bufs[i].freelist.high_mark );
  1063. ++count;
  1064. }
  1065. }
  1066. }
  1067. request.count = count;
  1068. if ( copy_to_user( argp, &request, sizeof(request) ) )
  1069. return -EFAULT;
  1070. return 0;
  1071. }
  1072. /**
  1073. * Specifies a low and high water mark for buffer allocation
  1074. *
  1075. * \param inode device inode.
  1076. * \param filp file pointer.
  1077. * \param cmd command.
  1078. * \param arg a pointer to a drm_buf_desc structure.
  1079. * \return zero on success or a negative number on failure.
  1080. *
  1081. * Verifies that the size order is bounded between the admissible orders and
  1082. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1083. *
  1084. * \note This ioctl is deprecated and mostly never used.
  1085. */
  1086. int drm_markbufs( struct inode *inode, struct file *filp,
  1087. unsigned int cmd, unsigned long arg )
  1088. {
  1089. drm_file_t *priv = filp->private_data;
  1090. drm_device_t *dev = priv->head->dev;
  1091. drm_device_dma_t *dma = dev->dma;
  1092. drm_buf_desc_t request;
  1093. int order;
  1094. drm_buf_entry_t *entry;
  1095. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1096. return -EINVAL;
  1097. if ( !dma ) return -EINVAL;
  1098. if ( copy_from_user( &request,
  1099. (drm_buf_desc_t __user *)arg,
  1100. sizeof(request) ) )
  1101. return -EFAULT;
  1102. DRM_DEBUG( "%d, %d, %d\n",
  1103. request.size, request.low_mark, request.high_mark );
  1104. order = drm_order( request.size );
  1105. if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
  1106. entry = &dma->bufs[order];
  1107. if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
  1108. return -EINVAL;
  1109. if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
  1110. return -EINVAL;
  1111. entry->freelist.low_mark = request.low_mark;
  1112. entry->freelist.high_mark = request.high_mark;
  1113. return 0;
  1114. }
  1115. /**
  1116. * Unreserve the buffers in list, previously reserved using drmDMA.
  1117. *
  1118. * \param inode device inode.
  1119. * \param filp file pointer.
  1120. * \param cmd command.
  1121. * \param arg pointer to a drm_buf_free structure.
  1122. * \return zero on success or a negative number on failure.
  1123. *
  1124. * Calls free_buffer() for each used buffer.
  1125. * This function is primarily used for debugging.
  1126. */
  1127. int drm_freebufs( struct inode *inode, struct file *filp,
  1128. unsigned int cmd, unsigned long arg )
  1129. {
  1130. drm_file_t *priv = filp->private_data;
  1131. drm_device_t *dev = priv->head->dev;
  1132. drm_device_dma_t *dma = dev->dma;
  1133. drm_buf_free_t request;
  1134. int i;
  1135. int idx;
  1136. drm_buf_t *buf;
  1137. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1138. return -EINVAL;
  1139. if ( !dma ) return -EINVAL;
  1140. if ( copy_from_user( &request,
  1141. (drm_buf_free_t __user *)arg,
  1142. sizeof(request) ) )
  1143. return -EFAULT;
  1144. DRM_DEBUG( "%d\n", request.count );
  1145. for ( i = 0 ; i < request.count ; i++ ) {
  1146. if ( copy_from_user( &idx,
  1147. &request.list[i],
  1148. sizeof(idx) ) )
  1149. return -EFAULT;
  1150. if ( idx < 0 || idx >= dma->buf_count ) {
  1151. DRM_ERROR( "Index %d (of %d max)\n",
  1152. idx, dma->buf_count - 1 );
  1153. return -EINVAL;
  1154. }
  1155. buf = dma->buflist[idx];
  1156. if ( buf->filp != filp ) {
  1157. DRM_ERROR( "Process %d freeing buffer not owned\n",
  1158. current->pid );
  1159. return -EINVAL;
  1160. }
  1161. drm_free_buffer( dev, buf );
  1162. }
  1163. return 0;
  1164. }
  1165. /**
  1166. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1167. *
  1168. * \param inode device inode.
  1169. * \param filp file pointer.
  1170. * \param cmd command.
  1171. * \param arg pointer to a drm_buf_map structure.
  1172. * \return zero on success or a negative number on failure.
  1173. *
  1174. * Maps the AGP or SG buffer region with do_mmap(), and copies information
  1175. * about each buffer into user space. The PCI buffers are already mapped on the
  1176. * addbufs_pci() call.
  1177. */
  1178. int drm_mapbufs( struct inode *inode, struct file *filp,
  1179. unsigned int cmd, unsigned long arg )
  1180. {
  1181. drm_file_t *priv = filp->private_data;
  1182. drm_device_t *dev = priv->head->dev;
  1183. drm_device_dma_t *dma = dev->dma;
  1184. drm_buf_map_t __user *argp = (void __user *)arg;
  1185. int retcode = 0;
  1186. const int zero = 0;
  1187. unsigned long virtual;
  1188. unsigned long address;
  1189. drm_buf_map_t request;
  1190. int i;
  1191. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1192. return -EINVAL;
  1193. if ( !dma ) return -EINVAL;
  1194. spin_lock( &dev->count_lock );
  1195. if ( atomic_read( &dev->buf_alloc ) ) {
  1196. spin_unlock( &dev->count_lock );
  1197. return -EBUSY;
  1198. }
  1199. dev->buf_use++; /* Can't allocate more after this call */
  1200. spin_unlock( &dev->count_lock );
  1201. if ( copy_from_user( &request, argp, sizeof(request) ) )
  1202. return -EFAULT;
  1203. if ( request.count >= dma->buf_count ) {
  1204. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1205. || (drm_core_check_feature(dev, DRIVER_SG)
  1206. && (dma->flags & _DRM_DMA_USE_SG))
  1207. || (drm_core_check_feature(dev, DRIVER_FB_DMA)
  1208. && (dma->flags & _DRM_DMA_USE_FB))) {
  1209. drm_map_t *map = dev->agp_buffer_map;
  1210. if ( !map ) {
  1211. retcode = -EINVAL;
  1212. goto done;
  1213. }
  1214. #if LINUX_VERSION_CODE <= 0x020402
  1215. down( &current->mm->mmap_sem );
  1216. #else
  1217. down_write( &current->mm->mmap_sem );
  1218. #endif
  1219. virtual = do_mmap( filp, 0, map->size,
  1220. PROT_READ | PROT_WRITE,
  1221. MAP_SHARED,
  1222. (unsigned long)map->offset );
  1223. #if LINUX_VERSION_CODE <= 0x020402
  1224. up( &current->mm->mmap_sem );
  1225. #else
  1226. up_write( &current->mm->mmap_sem );
  1227. #endif
  1228. } else {
  1229. #if LINUX_VERSION_CODE <= 0x020402
  1230. down( &current->mm->mmap_sem );
  1231. #else
  1232. down_write( &current->mm->mmap_sem );
  1233. #endif
  1234. virtual = do_mmap( filp, 0, dma->byte_count,
  1235. PROT_READ | PROT_WRITE,
  1236. MAP_SHARED, 0 );
  1237. #if LINUX_VERSION_CODE <= 0x020402
  1238. up( &current->mm->mmap_sem );
  1239. #else
  1240. up_write( &current->mm->mmap_sem );
  1241. #endif
  1242. }
  1243. if ( virtual > -1024UL ) {
  1244. /* Real error */
  1245. retcode = (signed long)virtual;
  1246. goto done;
  1247. }
  1248. request.virtual = (void __user *)virtual;
  1249. for ( i = 0 ; i < dma->buf_count ; i++ ) {
  1250. if ( copy_to_user( &request.list[i].idx,
  1251. &dma->buflist[i]->idx,
  1252. sizeof(request.list[0].idx) ) ) {
  1253. retcode = -EFAULT;
  1254. goto done;
  1255. }
  1256. if ( copy_to_user( &request.list[i].total,
  1257. &dma->buflist[i]->total,
  1258. sizeof(request.list[0].total) ) ) {
  1259. retcode = -EFAULT;
  1260. goto done;
  1261. }
  1262. if ( copy_to_user( &request.list[i].used,
  1263. &zero,
  1264. sizeof(zero) ) ) {
  1265. retcode = -EFAULT;
  1266. goto done;
  1267. }
  1268. address = virtual + dma->buflist[i]->offset; /* *** */
  1269. if ( copy_to_user( &request.list[i].address,
  1270. &address,
  1271. sizeof(address) ) ) {
  1272. retcode = -EFAULT;
  1273. goto done;
  1274. }
  1275. }
  1276. }
  1277. done:
  1278. request.count = dma->buf_count;
  1279. DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
  1280. if ( copy_to_user( argp, &request, sizeof(request) ) )
  1281. return -EFAULT;
  1282. return retcode;
  1283. }