1 /* drm_lists.h -- Buffer list handling routines -*- linux-c -*-
2 * Created: Mon Apr 19 20:54:22 1999 by faith@valinux.com
4 * Copyright 1999 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>
34 #define __NO_VERSION__
35 #include "dev/drm/drmP.h"
37 #if __HAVE_DMA_WAITLIST
39 int DRM(waitlist_create)(drm_waitlist_t *bl, int count)
42 return DRM_OS_ERR(EINVAL);
44 bl->bufs = DRM(alloc)((bl->count + 2) * sizeof(*bl->bufs),
47 if(!bl->bufs) return DRM_OS_ERR(ENOMEM);
52 bl->end = &bl->bufs[bl->count+1];
53 DRM_OS_SPININIT( bl->write_lock, "writelock" );
54 DRM_OS_SPININIT( bl->read_lock, "readlock" );
58 int DRM(waitlist_destroy)(drm_waitlist_t *bl)
61 return DRM_OS_ERR(EINVAL);
62 if (bl->bufs) DRM(free)(bl->bufs,
63 (bl->count + 2) * sizeof(*bl->bufs),
73 int DRM(waitlist_put)(drm_waitlist_t *bl, drm_buf_t *buf)
78 #endif /* __linux__ */
81 #endif /* __FreeBSD__ */
82 left = DRM_LEFTCOUNT(bl);
84 DRM_ERROR("Overflow while adding buffer %d from pid %d\n",
86 return DRM_OS_ERR(EINVAL);
88 #if __HAVE_DMA_HISTOGRAM
90 buf->time_queued = get_cycles();
91 #endif /* __linux__ */
93 getnanotime(&buf->time_queued);
94 #endif /* __FreeBSD__ */
96 buf->list = DRM_LIST_WAIT;
99 spin_lock_irqsave(&bl->write_lock, flags);
100 #endif /* __linux__ */
102 DRM_OS_SPINLOCK(&bl->write_lock);
104 #endif /* __FreeBSD__ */
106 if (++bl->wp >= bl->end) bl->wp = bl->bufs;
108 spin_unlock_irqrestore(&bl->write_lock, flags);
109 #endif /* __linux__ */
112 DRM_OS_SPINUNLOCK(&bl->write_lock);
113 #endif /* __FreeBSD__ */
118 drm_buf_t *DRM(waitlist_get)(drm_waitlist_t *bl)
123 #endif /* __linux__ */
126 #endif /* __FreeBSD__ */
129 spin_lock_irqsave(&bl->read_lock, flags);
131 if (bl->rp == bl->wp) {
132 spin_unlock_irqrestore(&bl->read_lock, flags);
135 if (++bl->rp >= bl->end) bl->rp = bl->bufs;
136 spin_unlock_irqrestore(&bl->read_lock, flags);
137 #endif /* __linux__ */
139 DRM_OS_SPINLOCK(&bl->read_lock);
142 if (bl->rp == bl->wp) {
144 DRM_OS_SPINUNLOCK(&bl->read_lock);
147 if (++bl->rp >= bl->end) bl->rp = bl->bufs;
149 DRM_OS_SPINUNLOCK(&bl->read_lock);
150 #endif /* __FreeBSD__ */
155 #endif /* __HAVE_DMA_WAITLIST */
158 #if __HAVE_DMA_FREELIST
160 int DRM(freelist_create)(drm_freelist_t *bl, int count)
162 atomic_set(&bl->count, 0);
165 init_waitqueue_head(&bl->waiting);
166 #endif /* __linux__ */
169 #endif /* __FreeBSD__ */
173 atomic_set(&bl->wfh, 0);
174 DRM_OS_SPININIT( bl->lock, "freelistlock" );
179 int DRM(freelist_destroy)(drm_freelist_t *bl)
181 atomic_set(&bl->count, 0);
186 int DRM(freelist_put)(drm_device_t *dev, drm_freelist_t *bl, drm_buf_t *buf)
188 drm_device_dma_t *dma = dev->dma;
191 DRM_ERROR("No DMA support\n");
195 if (buf->waiting || buf->pending || buf->list == DRM_LIST_FREE) {
196 DRM_ERROR("Freed buffer %d: w%d, p%d, l%d\n",
197 buf->idx, buf->waiting, buf->pending, buf->list);
200 #if __HAVE_DMA_HISTOGRAM
202 buf->time_freed = get_cycles();
203 #endif /* __linux__ */
205 getnanotime(&buf->time_queued);
206 #endif /* __FreeBSD__ */
207 DRM(histogram_compute)(dev, buf);
209 buf->list = DRM_LIST_FREE;
211 DRM_OS_SPINLOCK( &bl->lock );
212 buf->next = bl->next;
214 DRM_OS_SPINUNLOCK( &bl->lock );
216 atomic_inc(&bl->count);
217 if (atomic_read(&bl->count) > dma->buf_count) {
218 DRM_ERROR("%ld of %d buffers free after addition of %d\n",
219 (unsigned long)atomic_read(&bl->count),
220 dma->buf_count, buf->idx);
223 /* Check for high water mark */
224 if (atomic_read(&bl->wfh) && atomic_read(&bl->count)>=bl->high_mark) {
225 atomic_set(&bl->wfh, 0);
226 DRM_OS_WAKEUP_INT(&bl->waiting);
231 static drm_buf_t *DRM(freelist_try)(drm_freelist_t *bl)
235 if (!bl) return NULL;
238 DRM_OS_SPINLOCK(&bl->lock);
240 DRM_OS_SPINUNLOCK(&bl->lock);
244 bl->next = bl->next->next;
245 DRM_OS_SPINUNLOCK(&bl->lock);
247 atomic_dec(&bl->count);
249 buf->list = DRM_LIST_NONE;
250 if (buf->waiting || buf->pending) {
251 DRM_ERROR("Free buffer %d: w%d, p%d, l%d\n",
252 buf->idx, buf->waiting, buf->pending, buf->list);
258 drm_buf_t *DRM(freelist_get)(drm_freelist_t *bl, int block)
260 drm_buf_t *buf = NULL;
262 DECLARE_WAITQUEUE(entry, current);
263 #endif /* __linux__ */
266 #endif /* __FreeBSD__ */
268 if (!bl || !bl->initialized) return NULL;
270 /* Check for low water mark */
271 if (atomic_read(&bl->count) <= bl->low_mark) /* Became low */
272 atomic_set(&bl->wfh, 1);
273 if (atomic_read(&bl->wfh)) {
276 add_wait_queue(&bl->waiting, &entry);
278 current->state = TASK_INTERRUPTIBLE;
279 if (!atomic_read(&bl->wfh)
280 && (buf = DRM(freelist_try)(bl))) break;
282 if (signal_pending(current)) break;
284 current->state = TASK_RUNNING;
285 remove_wait_queue(&bl->waiting, &entry);
286 #endif /* __linux__ */
289 if (!atomic_read(&bl->wfh)
290 && (buf = DRM(freelist_try(bl)))) break;
291 error = tsleep(&bl->waiting, PZERO|PCATCH,
296 #endif /* __FreeBSD__ */
301 return DRM(freelist_try)(bl);
304 #endif /* __HAVE_DMA_FREELIST */