drm_dma.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* drm_dma.c -- DMA IOCTL and function support -*- linux-c -*-
  2.  * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  3.  *
  4.  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  5.  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6.  * All Rights Reserved.
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person obtaining a
  9.  * copy of this software and associated documentation files (the "Software"),
  10.  * to deal in the Software without restriction, including without limitation
  11.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12.  * and/or sell copies of the Software, and to permit persons to whom the
  13.  * Software is furnished to do so, subject to the following conditions:
  14.  *
  15.  * The above copyright notice and this permission notice (including the next
  16.  * paragraph) shall be included in all copies or substantial portions of the
  17.  * Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25.  * OTHER DEALINGS IN THE SOFTWARE.
  26.  *
  27.  * Authors:
  28.  *    Rickard E. (Rik) Faith <faith@valinux.com>
  29.  *    Gareth Hughes <gareth@valinux.com>
  30.  */
  31. #include "drmP.h"
  32. #include <linux/interrupt.h> /* For task queue support */
  33. #ifndef __HAVE_DMA_WAITQUEUE
  34. #define __HAVE_DMA_WAITQUEUE 0
  35. #endif
  36. #ifndef __HAVE_DMA_RECLAIM
  37. #define __HAVE_DMA_RECLAIM 0
  38. #endif
  39. #ifndef __HAVE_SHARED_IRQ
  40. #define __HAVE_SHARED_IRQ 0
  41. #endif
  42. #if __HAVE_SHARED_IRQ
  43. #define DRM_IRQ_TYPE SA_SHIRQ
  44. #else
  45. #define DRM_IRQ_TYPE 0
  46. #endif
  47. #if __HAVE_DMA
  48. int DRM(dma_setup)( drm_device_t *dev )
  49. {
  50. int i;
  51. dev->dma = DRM(alloc)( sizeof(*dev->dma), DRM_MEM_DRIVER );
  52. if ( !dev->dma )
  53. return -ENOMEM;
  54. memset( dev->dma, 0, sizeof(*dev->dma) );
  55. for ( i = 0 ; i <= DRM_MAX_ORDER ; i++ )
  56. memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));
  57. return 0;
  58. }
  59. void DRM(dma_takedown)(drm_device_t *dev)
  60. {
  61. drm_device_dma_t  *dma = dev->dma;
  62. int   i, j;
  63. if (!dma) return;
  64. /* Clear dma buffers */
  65. for (i = 0; i <= DRM_MAX_ORDER; i++) {
  66. if (dma->bufs[i].seg_count) {
  67. DRM_DEBUG("order %d: buf_count = %d,"
  68.   " seg_count = %dn",
  69.   i,
  70.   dma->bufs[i].buf_count,
  71.   dma->bufs[i].seg_count);
  72. for (j = 0; j < dma->bufs[i].seg_count; j++) {
  73. DRM(free_pages)(dma->bufs[i].seglist[j],
  74. dma->bufs[i].page_order,
  75. DRM_MEM_DMA);
  76. }
  77. DRM(free)(dma->bufs[i].seglist,
  78.   dma->bufs[i].seg_count
  79.   * sizeof(*dma->bufs[0].seglist),
  80.   DRM_MEM_SEGS);
  81. }
  82.     if(dma->bufs[i].buf_count) {
  83.     for(j = 0; j < dma->bufs[i].buf_count; j++) {
  84.    if(dma->bufs[i].buflist[j].dev_private) {
  85.       DRM(free)(dma->bufs[i].buflist[j].dev_private,
  86. dma->bufs[i].buflist[j].dev_priv_size,
  87. DRM_MEM_BUFS);
  88.    }
  89. }
  90.     DRM(free)(dma->bufs[i].buflist,
  91.   dma->bufs[i].buf_count *
  92.   sizeof(*dma->bufs[0].buflist),
  93.   DRM_MEM_BUFS);
  94. #if __HAVE_DMA_FREELIST
  95.     DRM(freelist_destroy)(&dma->bufs[i].freelist);
  96. #endif
  97. }
  98. }
  99. if (dma->buflist) {
  100. DRM(free)(dma->buflist,
  101.   dma->buf_count * sizeof(*dma->buflist),
  102.   DRM_MEM_BUFS);
  103. }
  104. if (dma->pagelist) {
  105. DRM(free)(dma->pagelist,
  106.   dma->page_count * sizeof(*dma->pagelist),
  107.   DRM_MEM_PAGES);
  108. }
  109. DRM(free)(dev->dma, sizeof(*dev->dma), DRM_MEM_DRIVER);
  110. dev->dma = NULL;
  111. }
  112. #if __HAVE_DMA_HISTOGRAM
  113. /* This is slow, but is useful for debugging. */
  114. int DRM(histogram_slot)(unsigned long count)
  115. {
  116. int value = DRM_DMA_HISTOGRAM_INITIAL;
  117. int slot;
  118. for (slot = 0;
  119.      slot < DRM_DMA_HISTOGRAM_SLOTS;
  120.      ++slot, value = DRM_DMA_HISTOGRAM_NEXT(value)) {
  121. if (count < value) return slot;
  122. }
  123. return DRM_DMA_HISTOGRAM_SLOTS - 1;
  124. }
  125. void DRM(histogram_compute)(drm_device_t *dev, drm_buf_t *buf)
  126. {
  127. cycles_t queued_to_dispatched;
  128. cycles_t dispatched_to_completed;
  129. cycles_t completed_to_freed;
  130. int  q2d, d2c, c2f, q2c, q2f;
  131. if (buf->time_queued) {
  132. queued_to_dispatched = (buf->time_dispatched
  133.    - buf->time_queued);
  134. dispatched_to_completed = (buf->time_completed
  135.    - buf->time_dispatched);
  136. completed_to_freed = (buf->time_freed
  137.    - buf->time_completed);
  138. q2d = DRM(histogram_slot)(queued_to_dispatched);
  139. d2c = DRM(histogram_slot)(dispatched_to_completed);
  140. c2f = DRM(histogram_slot)(completed_to_freed);
  141. q2c = DRM(histogram_slot)(queued_to_dispatched
  142.   + dispatched_to_completed);
  143. q2f = DRM(histogram_slot)(queued_to_dispatched
  144.   + dispatched_to_completed
  145.   + completed_to_freed);
  146. atomic_inc(&dev->histo.total);
  147. atomic_inc(&dev->histo.queued_to_dispatched[q2d]);
  148. atomic_inc(&dev->histo.dispatched_to_completed[d2c]);
  149. atomic_inc(&dev->histo.completed_to_freed[c2f]);
  150. atomic_inc(&dev->histo.queued_to_completed[q2c]);
  151. atomic_inc(&dev->histo.queued_to_freed[q2f]);
  152. }
  153. buf->time_queued     = 0;
  154. buf->time_dispatched = 0;
  155. buf->time_completed  = 0;
  156. buf->time_freed      = 0;
  157. }
  158. #endif
  159. void DRM(free_buffer)(drm_device_t *dev, drm_buf_t *buf)
  160. {
  161. if (!buf) return;
  162. buf->waiting  = 0;
  163. buf->pending  = 0;
  164. buf->pid      = 0;
  165. buf->used     = 0;
  166. #if __HAVE_DMA_HISTOGRAM
  167. buf->time_completed = get_cycles();
  168. #endif
  169. if ( __HAVE_DMA_WAITQUEUE && waitqueue_active(&buf->dma_wait)) {
  170. wake_up_interruptible(&buf->dma_wait);
  171. }
  172. #if __HAVE_DMA_FREELIST
  173. else {
  174. drm_device_dma_t *dma = dev->dma;
  175. /* If processes are waiting, the last one
  176.    to wake will put the buffer on the free
  177.    list.  If no processes are waiting, we
  178.    put the buffer on the freelist here. */
  179. DRM(freelist_put)(dev, &dma->bufs[buf->order].freelist, buf);
  180. }
  181. #endif
  182. }
  183. #if !__HAVE_DMA_RECLAIM
  184. void DRM(reclaim_buffers)(drm_device_t *dev, pid_t pid)
  185. {
  186. drm_device_dma_t *dma = dev->dma;
  187. int  i;
  188. if (!dma) return;
  189. for (i = 0; i < dma->buf_count; i++) {
  190. if (dma->buflist[i]->pid == pid) {
  191. switch (dma->buflist[i]->list) {
  192. case DRM_LIST_NONE:
  193. DRM(free_buffer)(dev, dma->buflist[i]);
  194. break;
  195. case DRM_LIST_WAIT:
  196. dma->buflist[i]->list = DRM_LIST_RECLAIM;
  197. break;
  198. default:
  199. /* Buffer already on hardware. */
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. #endif
  206. /* GH: This is a big hack for now...
  207.  */
  208. #if __HAVE_OLD_DMA
  209. void DRM(clear_next_buffer)(drm_device_t *dev)
  210. {
  211. drm_device_dma_t *dma = dev->dma;
  212. dma->next_buffer = NULL;
  213. if (dma->next_queue && !DRM_BUFCOUNT(&dma->next_queue->waitlist)) {
  214. wake_up_interruptible(&dma->next_queue->flush_queue);
  215. }
  216. dma->next_queue  = NULL;
  217. }
  218. int DRM(select_queue)(drm_device_t *dev, void (*wrapper)(unsigned long))
  219. {
  220. int    i;
  221. int    candidate = -1;
  222. int    j      = jiffies;
  223. if (!dev) {
  224. DRM_ERROR("No devicen");
  225. return -1;
  226. }
  227. if (!dev->queuelist || !dev->queuelist[DRM_KERNEL_CONTEXT]) {
  228. /* This only happens between the time the
  229.    interrupt is initialized and the time
  230.    the queues are initialized. */
  231. return -1;
  232. }
  233. /* Doing "while locked" DMA? */
  234. if (DRM_WAITCOUNT(dev, DRM_KERNEL_CONTEXT)) {
  235. return DRM_KERNEL_CONTEXT;
  236. }
  237. /* If there are buffers on the last_context
  238.    queue, and we have not been executing
  239.    this context very long, continue to
  240.    execute this context. */
  241. if (dev->last_switch <= j
  242.     && dev->last_switch + DRM_TIME_SLICE > j
  243.     && DRM_WAITCOUNT(dev, dev->last_context)) {
  244. return dev->last_context;
  245. }
  246. /* Otherwise, find a candidate */
  247. for (i = dev->last_checked + 1; i < dev->queue_count; i++) {
  248. if (DRM_WAITCOUNT(dev, i)) {
  249. candidate = dev->last_checked = i;
  250. break;
  251. }
  252. }
  253. if (candidate < 0) {
  254. for (i = 0; i < dev->queue_count; i++) {
  255. if (DRM_WAITCOUNT(dev, i)) {
  256. candidate = dev->last_checked = i;
  257. break;
  258. }
  259. }
  260. }
  261. if (wrapper
  262.     && candidate >= 0
  263.     && candidate != dev->last_context
  264.     && dev->last_switch <= j
  265.     && dev->last_switch + DRM_TIME_SLICE > j) {
  266. if (dev->timer.expires != dev->last_switch + DRM_TIME_SLICE) {
  267. del_timer(&dev->timer);
  268. dev->timer.function = wrapper;
  269. dev->timer.data     = (unsigned long)dev;
  270. dev->timer.expires  = dev->last_switch+DRM_TIME_SLICE;
  271. add_timer(&dev->timer);
  272. }
  273. return -1;
  274. }
  275. return candidate;
  276. }
  277. int DRM(dma_enqueue)(drm_device_t *dev, drm_dma_t *d)
  278. {
  279. int   i;
  280. drm_queue_t   *q;
  281. drm_buf_t   *buf;
  282. int   idx;
  283. int   while_locked = 0;
  284. drm_device_dma_t  *dma = dev->dma;
  285. DECLARE_WAITQUEUE(entry, current);
  286. DRM_DEBUG("%dn", d->send_count);
  287. if (d->flags & _DRM_DMA_WHILE_LOCKED) {
  288. int context = dev->lock.hw_lock->lock;
  289. if (!_DRM_LOCK_IS_HELD(context)) {
  290. DRM_ERROR("No lock held during "while locked""
  291.   " requestn");
  292. return -EINVAL;
  293. }
  294. if (d->context != _DRM_LOCKING_CONTEXT(context)
  295.     && _DRM_LOCKING_CONTEXT(context) != DRM_KERNEL_CONTEXT) {
  296. DRM_ERROR("Lock held by %d while %d makes"
  297.   " "while locked" requestn",
  298.   _DRM_LOCKING_CONTEXT(context),
  299.   d->context);
  300. return -EINVAL;
  301. }
  302. q = dev->queuelist[DRM_KERNEL_CONTEXT];
  303. while_locked = 1;
  304. } else {
  305. q = dev->queuelist[d->context];
  306. }
  307. atomic_inc(&q->use_count);
  308. if (atomic_read(&q->block_write)) {
  309. add_wait_queue(&q->write_queue, &entry);
  310. atomic_inc(&q->block_count);
  311. for (;;) {
  312. current->state = TASK_INTERRUPTIBLE;
  313. if (!atomic_read(&q->block_write)) break;
  314. schedule();
  315. if (signal_pending(current)) {
  316. atomic_dec(&q->use_count);
  317. remove_wait_queue(&q->write_queue, &entry);
  318. return -EINTR;
  319. }
  320. }
  321. atomic_dec(&q->block_count);
  322. current->state = TASK_RUNNING;
  323. remove_wait_queue(&q->write_queue, &entry);
  324. }
  325. for (i = 0; i < d->send_count; i++) {
  326. idx = d->send_indices[i];
  327. if (idx < 0 || idx >= dma->buf_count) {
  328. atomic_dec(&q->use_count);
  329. DRM_ERROR("Index %d (of %d max)n",
  330.   d->send_indices[i], dma->buf_count - 1);
  331. return -EINVAL;
  332. }
  333. buf = dma->buflist[ idx ];
  334. if (buf->pid != current->pid) {
  335. atomic_dec(&q->use_count);
  336. DRM_ERROR("Process %d using buffer owned by %dn",
  337.   current->pid, buf->pid);
  338. return -EINVAL;
  339. }
  340. if (buf->list != DRM_LIST_NONE) {
  341. atomic_dec(&q->use_count);
  342. DRM_ERROR("Process %d using buffer %d on list %dn",
  343.   current->pid, buf->idx, buf->list);
  344. }
  345. buf->used   = d->send_sizes[i];
  346. buf->while_locked = while_locked;
  347. buf->context   = d->context;
  348. if (!buf->used) {
  349. DRM_ERROR("Queueing 0 length buffern");
  350. }
  351. if (buf->pending) {
  352. atomic_dec(&q->use_count);
  353. DRM_ERROR("Queueing pending buffer:"
  354.   " buffer %d, offset %dn",
  355.   d->send_indices[i], i);
  356. return -EINVAL;
  357. }
  358. if (buf->waiting) {
  359. atomic_dec(&q->use_count);
  360. DRM_ERROR("Queueing waiting buffer:"
  361.   " buffer %d, offset %dn",
  362.   d->send_indices[i], i);
  363. return -EINVAL;
  364. }
  365. buf->waiting = 1;
  366. if (atomic_read(&q->use_count) == 1
  367.     || atomic_read(&q->finalization)) {
  368. DRM(free_buffer)(dev, buf);
  369. } else {
  370. DRM(waitlist_put)(&q->waitlist, buf);
  371. atomic_inc(&q->total_queued);
  372. }
  373. }
  374. atomic_dec(&q->use_count);
  375. return 0;
  376. }
  377. static int DRM(dma_get_buffers_of_order)(drm_device_t *dev, drm_dma_t *d,
  378.  int order)
  379. {
  380. int   i;
  381. drm_buf_t   *buf;
  382. drm_device_dma_t  *dma = dev->dma;
  383. for (i = d->granted_count; i < d->request_count; i++) {
  384. buf = DRM(freelist_get)(&dma->bufs[order].freelist,
  385. d->flags & _DRM_DMA_WAIT);
  386. if (!buf) break;
  387. if (buf->pending || buf->waiting) {
  388. DRM_ERROR("Free buffer %d in use by %d (w%d, p%d)n",
  389.   buf->idx,
  390.   buf->pid,
  391.   buf->waiting,
  392.   buf->pending);
  393. }
  394. buf->pid     = current->pid;
  395. if (copy_to_user(&d->request_indices[i],
  396.  &buf->idx,
  397.  sizeof(buf->idx)))
  398. return -EFAULT;
  399. if (copy_to_user(&d->request_sizes[i],
  400.  &buf->total,
  401.  sizeof(buf->total)))
  402. return -EFAULT;
  403. ++d->granted_count;
  404. }
  405. return 0;
  406. }
  407. int DRM(dma_get_buffers)(drm_device_t *dev, drm_dma_t *dma)
  408. {
  409. int   order;
  410. int   retcode = 0;
  411. int   tmp_order;
  412. order = DRM(order)(dma->request_size);
  413. dma->granted_count = 0;
  414. retcode    = DRM(dma_get_buffers_of_order)(dev, dma, order);
  415. if (dma->granted_count < dma->request_count
  416.     && (dma->flags & _DRM_DMA_SMALLER_OK)) {
  417. for (tmp_order = order - 1;
  418.      !retcode
  419.      && dma->granted_count < dma->request_count
  420.      && tmp_order >= DRM_MIN_ORDER;
  421.      --tmp_order) {
  422. retcode = DRM(dma_get_buffers_of_order)(dev, dma,
  423. tmp_order);
  424. }
  425. }
  426. if (dma->granted_count < dma->request_count
  427.     && (dma->flags & _DRM_DMA_LARGER_OK)) {
  428. for (tmp_order = order + 1;
  429.      !retcode
  430.      && dma->granted_count < dma->request_count
  431.      && tmp_order <= DRM_MAX_ORDER;
  432.      ++tmp_order) {
  433. retcode = DRM(dma_get_buffers_of_order)(dev, dma,
  434. tmp_order);
  435. }
  436. }
  437. return 0;
  438. }
  439. #endif /* __HAVE_OLD_DMA */
  440. #if __HAVE_DMA_IRQ
  441. int DRM(irq_install)( drm_device_t *dev, int irq )
  442. {
  443. int ret;
  444. if ( !irq )
  445. return -EINVAL;
  446. down( &dev->struct_sem );
  447. if ( dev->irq ) {
  448. up( &dev->struct_sem );
  449. return -EBUSY;
  450. }
  451. dev->irq = irq;
  452. up( &dev->struct_sem );
  453. DRM_DEBUG( "%s: irq=%dn", __FUNCTION__, irq );
  454. dev->context_flag = 0;
  455. dev->interrupt_flag = 0;
  456. dev->dma_flag = 0;
  457. dev->dma->next_buffer = NULL;
  458. dev->dma->next_queue = NULL;
  459. dev->dma->this_buffer = NULL;
  460. #if __HAVE_DMA_IRQ_BH
  461. INIT_LIST_HEAD( &dev->tq.list );
  462. dev->tq.sync = 0;
  463. dev->tq.routine = DRM(dma_immediate_bh);
  464. dev->tq.data = dev;
  465. #endif
  466. /* Before installing handler */
  467. DRIVER_PREINSTALL();
  468. /* Install handler */
  469. ret = request_irq( dev->irq, DRM(dma_service),
  470.    DRM_IRQ_TYPE, dev->devname, dev );
  471. if ( ret < 0 ) {
  472. down( &dev->struct_sem );
  473. dev->irq = 0;
  474. up( &dev->struct_sem );
  475. return ret;
  476. }
  477. /* After installing handler */
  478. DRIVER_POSTINSTALL();
  479. return 0;
  480. }
  481. int DRM(irq_uninstall)( drm_device_t *dev )
  482. {
  483. int irq;
  484. down( &dev->struct_sem );
  485. irq = dev->irq;
  486. dev->irq = 0;
  487. up( &dev->struct_sem );
  488. if ( !irq )
  489. return -EINVAL;
  490. DRM_DEBUG( "%s: irq=%dn", __FUNCTION__, irq );
  491. DRIVER_UNINSTALL();
  492. free_irq( irq, dev );
  493. return 0;
  494. }
  495. int DRM(control)( struct inode *inode, struct file *filp,
  496.   unsigned int cmd, unsigned long arg )
  497. {
  498. drm_file_t *priv = filp->private_data;
  499. drm_device_t *dev = priv->dev;
  500. drm_control_t ctl;
  501. if ( copy_from_user( &ctl, (drm_control_t *)arg, sizeof(ctl) ) )
  502. return -EFAULT;
  503. switch ( ctl.func ) {
  504. case DRM_INST_HANDLER:
  505. return DRM(irq_install)( dev, ctl.irq );
  506. case DRM_UNINST_HANDLER:
  507. return DRM(irq_uninstall)( dev );
  508. default:
  509. return -EINVAL;
  510. }
  511. }
  512. #endif /* __HAVE_DMA_IRQ */
  513. #endif /* __HAVE_DMA */