1 /* drm_dma.c -- DMA IOCTL and function support -*- linux-c -*-
2 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
33 /* Gamma-specific code pulled from drm_dma.h:
36 void DRM(clear_next_buffer
)(drm_device_t
*dev
)
38 drm_device_dma_t
*dma
= dev
->dma
;
40 dma
->next_buffer
= NULL
;
41 if (dma
->next_queue
&& !DRM_BUFCOUNT(&dma
->next_queue
->waitlist
)) {
42 wake_up_interruptible(&dma
->next_queue
->flush_queue
);
44 dma
->next_queue
= NULL
;
47 int DRM(select_queue
)(drm_device_t
*dev
, void (*wrapper
)(unsigned long))
54 DRM_ERROR("No device\n");
57 if (!dev
->queuelist
|| !dev
->queuelist
[DRM_KERNEL_CONTEXT
]) {
58 /* This only happens between the time the
59 interrupt is initialized and the time
60 the queues are initialized. */
64 /* Doing "while locked" DMA? */
65 if (DRM_WAITCOUNT(dev
, DRM_KERNEL_CONTEXT
)) {
66 return DRM_KERNEL_CONTEXT
;
69 /* If there are buffers on the last_context
70 queue, and we have not been executing
71 this context very long, continue to
72 execute this context. */
73 if (dev
->last_switch
<= j
74 && dev
->last_switch
+ DRM_TIME_SLICE
> j
75 && DRM_WAITCOUNT(dev
, dev
->last_context
)) {
76 return dev
->last_context
;
79 /* Otherwise, find a candidate */
80 for (i
= dev
->last_checked
+ 1; i
< dev
->queue_count
; i
++) {
81 if (DRM_WAITCOUNT(dev
, i
)) {
82 candidate
= dev
->last_checked
= i
;
88 for (i
= 0; i
< dev
->queue_count
; i
++) {
89 if (DRM_WAITCOUNT(dev
, i
)) {
90 candidate
= dev
->last_checked
= i
;
98 && candidate
!= dev
->last_context
99 && dev
->last_switch
<= j
100 && dev
->last_switch
+ DRM_TIME_SLICE
> j
) {
101 if (dev
->timer
.expires
!= dev
->last_switch
+ DRM_TIME_SLICE
) {
102 del_timer(&dev
->timer
);
103 dev
->timer
.function
= wrapper
;
104 dev
->timer
.data
= (unsigned long)dev
;
105 dev
->timer
.expires
= dev
->last_switch
+DRM_TIME_SLICE
;
106 add_timer(&dev
->timer
);
115 int DRM(dma_enqueue
)(struct file
*filp
, drm_dma_t
*d
)
117 drm_file_t
*priv
= filp
->private_data
;
118 drm_device_t
*dev
= priv
->dev
;
123 int while_locked
= 0;
124 drm_device_dma_t
*dma
= dev
->dma
;
127 DECLARE_WAITQUEUE(entry
, current
);
129 DRM_DEBUG("%d\n", d
->send_count
);
131 if (d
->flags
& _DRM_DMA_WHILE_LOCKED
) {
132 int context
= dev
->lock
.hw_lock
->lock
;
134 if (!_DRM_LOCK_IS_HELD(context
)) {
135 DRM_ERROR("No lock held during \"while locked\""
139 if (d
->context
!= _DRM_LOCKING_CONTEXT(context
)
140 && _DRM_LOCKING_CONTEXT(context
) != DRM_KERNEL_CONTEXT
) {
141 DRM_ERROR("Lock held by %d while %d makes"
142 " \"while locked\" request\n",
143 _DRM_LOCKING_CONTEXT(context
),
147 q
= dev
->queuelist
[DRM_KERNEL_CONTEXT
];
150 q
= dev
->queuelist
[d
->context
];
154 atomic_inc(&q
->use_count
);
155 if (atomic_read(&q
->block_write
)) {
156 add_wait_queue(&q
->write_queue
, &entry
);
157 atomic_inc(&q
->block_count
);
159 current
->state
= TASK_INTERRUPTIBLE
;
160 if (!atomic_read(&q
->block_write
)) break;
162 if (signal_pending(current
)) {
163 atomic_dec(&q
->use_count
);
164 remove_wait_queue(&q
->write_queue
, &entry
);
168 atomic_dec(&q
->block_count
);
169 current
->state
= TASK_RUNNING
;
170 remove_wait_queue(&q
->write_queue
, &entry
);
173 ind
= DRM(alloc
)(d
->send_count
* sizeof(int), DRM_MEM_DRIVER
);
177 if (copy_from_user(ind
, d
->send_indices
, d
->send_count
* sizeof(int))) {
183 for (i
= 0; i
< d
->send_count
; i
++) {
185 if (idx
< 0 || idx
>= dma
->buf_count
) {
186 DRM_ERROR("Index %d (of %d max)\n",
187 ind
[i
], dma
->buf_count
- 1);
190 buf
= dma
->buflist
[ idx
];
191 if (buf
->filp
!= filp
) {
192 DRM_ERROR("Process %d using buffer not owned\n",
196 if (buf
->list
!= DRM_LIST_NONE
) {
197 DRM_ERROR("Process %d using buffer %d on list %d\n",
198 current
->pid
, buf
->idx
, buf
->list
);
202 buf
->while_locked
= while_locked
;
203 buf
->context
= d
->context
;
205 DRM_ERROR("Queueing 0 length buffer\n");
208 DRM_ERROR("Queueing pending buffer:"
209 " buffer %d, offset %d\n",
214 DRM_ERROR("Queueing waiting buffer:"
215 " buffer %d, offset %d\n",
220 if (atomic_read(&q
->use_count
) == 1
221 || atomic_read(&q
->finalization
)) {
222 DRM(free_buffer
)(dev
, buf
);
224 DRM(waitlist_put
)(&q
->waitlist
, buf
);
225 atomic_inc(&q
->total_queued
);
228 atomic_dec(&q
->use_count
);
233 DRM(free
)(ind
, d
->send_count
* sizeof(int), DRM_MEM_DRIVER
);
234 atomic_dec(&q
->use_count
);
238 static int DRM(dma_get_buffers_of_order
)(struct file
*filp
, drm_dma_t
*d
,
241 drm_file_t
*priv
= filp
->private_data
;
242 drm_device_t
*dev
= priv
->dev
;
245 drm_device_dma_t
*dma
= dev
->dma
;
247 for (i
= d
->granted_count
; i
< d
->request_count
; i
++) {
248 buf
= DRM(freelist_get
)(&dma
->bufs
[order
].freelist
,
249 d
->flags
& _DRM_DMA_WAIT
);
251 if (buf
->pending
|| buf
->waiting
) {
252 DRM_ERROR("Free buffer %d in use: filp %p (w%d, p%d)\n",
259 if (copy_to_user(&d
->request_indices
[i
],
264 if (copy_to_user(&d
->request_sizes
[i
],
275 int DRM(dma_get_buffers
)(struct file
*filp
, drm_dma_t
*dma
)
281 order
= DRM(order
)(dma
->request_size
);
283 dma
->granted_count
= 0;
284 retcode
= DRM(dma_get_buffers_of_order
)(filp
, dma
, order
);
286 if (dma
->granted_count
< dma
->request_count
287 && (dma
->flags
& _DRM_DMA_SMALLER_OK
)) {
288 for (tmp_order
= order
- 1;
290 && dma
->granted_count
< dma
->request_count
291 && tmp_order
>= DRM_MIN_ORDER
;
294 retcode
= DRM(dma_get_buffers_of_order
)(filp
, dma
,
299 if (dma
->granted_count
< dma
->request_count
300 && (dma
->flags
& _DRM_DMA_LARGER_OK
)) {
301 for (tmp_order
= order
+ 1;
303 && dma
->granted_count
< dma
->request_count
304 && tmp_order
<= DRM_MAX_ORDER
;
307 retcode
= DRM(dma_get_buffers_of_order
)(filp
, dma
,