* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / char / drm / lists.c
blob062631f98ea22f93e20eb563427d15c0379f1d12
1 /* lists.c -- Buffer list handling routines -*- linux-c -*-
2 * Created: Mon Apr 19 20:54:22 1999 by faith@precisioninsight.com
3 * Revised: Fri Aug 20 09:27:01 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
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
17 * Software.
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 * PRECISION INSIGHT 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 OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/generic/lists.c,v 1.3 1999/08/20 15:07:02 faith Exp $
28 * $XFree86$
32 #define __NO_VERSION__
33 #include "drmP.h"
35 int drm_waitlist_create(drm_waitlist_t *bl, int count)
37 DRM_DEBUG("%d\n", count);
38 if (bl->count) return -EINVAL;
40 bl->count = count;
41 bl->bufs = drm_alloc((bl->count + 2) * sizeof(*bl->bufs),
42 DRM_MEM_BUFLISTS);
43 bl->rp = bl->bufs;
44 bl->wp = bl->bufs;
45 bl->end = &bl->bufs[bl->count+1];
46 bl->write_lock = SPIN_LOCK_UNLOCKED;
47 bl->read_lock = SPIN_LOCK_UNLOCKED;
48 return 0;
51 int drm_waitlist_destroy(drm_waitlist_t *bl)
53 DRM_DEBUG("\n");
54 if (bl->rp != bl->wp) return -EINVAL;
55 if (bl->bufs) drm_free(bl->bufs,
56 (bl->count + 2) * sizeof(*bl->bufs),
57 DRM_MEM_BUFLISTS);
58 bl->count = 0;
59 bl->bufs = NULL;
60 bl->rp = NULL;
61 bl->wp = NULL;
62 bl->end = NULL;
63 return 0;
66 int drm_waitlist_put(drm_waitlist_t *bl, drm_buf_t *buf)
68 int left;
69 unsigned long flags;
71 left = DRM_LEFTCOUNT(bl);
72 DRM_DEBUG("put %d (%d left, rp = %p, wp = %p)\n",
73 buf->idx, left, bl->rp, bl->wp);
74 if (!left) {
75 DRM_ERROR("Overflow while adding buffer %d from pid %d\n",
76 buf->idx, buf->pid);
77 return -EINVAL;
79 #if DRM_DMA_HISTOGRAM
80 buf->time_queued = get_cycles();
81 #endif
82 buf->list = DRM_LIST_WAIT;
84 spin_lock_irqsave(&bl->write_lock, flags);
85 *bl->wp = buf;
86 if (++bl->wp >= bl->end) bl->wp = bl->bufs;
87 spin_unlock_irqrestore(&bl->write_lock, flags);
89 return 0;
92 drm_buf_t *drm_waitlist_get(drm_waitlist_t *bl)
94 drm_buf_t *buf;
95 unsigned long flags;
97 spin_lock_irqsave(&bl->read_lock, flags);
98 buf = *bl->rp;
99 if (bl->rp == bl->wp) {
100 spin_unlock_irqrestore(&bl->read_lock, flags);
101 return NULL;
103 if (++bl->rp >= bl->end) bl->rp = bl->bufs;
104 spin_unlock_irqrestore(&bl->read_lock, flags);
106 DRM_DEBUG("get %d\n", buf->idx);
107 return buf;
110 int drm_freelist_create(drm_freelist_t *bl, int count)
112 DRM_DEBUG("\n");
113 atomic_set(&bl->count, 0);
114 bl->next = NULL;
115 init_waitqueue_head(&bl->waiting);
116 bl->low_mark = 0;
117 bl->high_mark = 0;
118 atomic_set(&bl->wfh, 0);
119 ++bl->initialized;
120 return 0;
123 int drm_freelist_destroy(drm_freelist_t *bl)
125 DRM_DEBUG("\n");
126 atomic_set(&bl->count, 0);
127 bl->next = NULL;
128 return 0;
131 int drm_freelist_put(drm_device_t *dev, drm_freelist_t *bl, drm_buf_t *buf)
133 unsigned int old;
134 unsigned int new;
135 char failed;
136 int count = 0;
137 drm_device_dma_t *dma = dev->dma;
139 if (!dma) {
140 DRM_ERROR("No DMA support\n");
141 return 1;
144 if (buf->waiting || buf->pending || buf->list == DRM_LIST_FREE) {
145 DRM_ERROR("Freed buffer %d: w%d, p%d, l%d\n",
146 buf->idx, buf->waiting, buf->pending, buf->list);
148 DRM_DEBUG("%d, count = %d, wfh = %d, w%d, p%d\n",
149 buf->idx, atomic_read(&bl->count), atomic_read(&bl->wfh),
150 buf->waiting, buf->pending);
151 if (!bl) return 1;
152 #if DRM_DMA_HISTOGRAM
153 buf->time_freed = get_cycles();
154 drm_histogram_compute(dev, buf);
155 #endif
156 buf->list = DRM_LIST_FREE;
157 do {
158 old = (unsigned long)bl->next;
159 buf->next = (void *)old;
160 new = (unsigned long)buf;
161 _DRM_CAS(&bl->next, old, new, failed);
162 if (++count > DRM_LOOPING_LIMIT) {
163 DRM_ERROR("Looping\n");
164 return 1;
166 } while (failed);
167 atomic_inc(&bl->count);
168 if (atomic_read(&bl->count) > dma->buf_count) {
169 DRM_ERROR("%d of %d buffers free after addition of %d\n",
170 atomic_read(&bl->count), dma->buf_count, buf->idx);
171 return 1;
173 /* Check for high water mark */
174 if (atomic_read(&bl->wfh) && atomic_read(&bl->count)>=bl->high_mark) {
175 atomic_set(&bl->wfh, 0);
176 wake_up_interruptible(&bl->waiting);
178 return 0;
181 static drm_buf_t *drm_freelist_try(drm_freelist_t *bl)
183 unsigned int old;
184 unsigned int new;
185 char failed;
186 drm_buf_t *buf;
187 int count = 0;
189 if (!bl) return NULL;
191 /* Get buffer */
192 do {
193 old = (unsigned int)bl->next;
194 if (!old) {
195 return NULL;
197 new = (unsigned long)bl->next->next;
198 _DRM_CAS(&bl->next, old, new, failed);
199 if (++count > DRM_LOOPING_LIMIT) {
200 DRM_ERROR("Looping\n");
201 return NULL;
203 } while (failed);
204 atomic_dec(&bl->count);
206 buf = (drm_buf_t *)old;
207 buf->next = NULL;
208 buf->list = DRM_LIST_NONE;
209 DRM_DEBUG("%d, count = %d, wfh = %d, w%d, p%d\n",
210 buf->idx, atomic_read(&bl->count), atomic_read(&bl->wfh),
211 buf->waiting, buf->pending);
212 if (buf->waiting || buf->pending) {
213 DRM_ERROR("Free buffer %d: w%d, p%d, l%d\n",
214 buf->idx, buf->waiting, buf->pending, buf->list);
217 return buf;
220 drm_buf_t *drm_freelist_get(drm_freelist_t *bl, int block)
222 drm_buf_t *buf = NULL;
223 DECLARE_WAITQUEUE(entry, current);
225 if (!bl || !bl->initialized) return NULL;
227 /* Check for low water mark */
228 if (atomic_read(&bl->count) <= bl->low_mark) /* Became low */
229 atomic_set(&bl->wfh, 1);
230 if (atomic_read(&bl->wfh)) {
231 DRM_DEBUG("Block = %d, count = %d, wfh = %d\n",
232 block, atomic_read(&bl->count),
233 atomic_read(&bl->wfh));
234 if (block) {
235 add_wait_queue(&bl->waiting, &entry);
236 current->state = TASK_INTERRUPTIBLE;
237 for (;;) {
238 if (!atomic_read(&bl->wfh)
239 && (buf = drm_freelist_try(bl))) break;
240 schedule();
241 if (signal_pending(current)) break;
243 current->state = TASK_RUNNING;
244 remove_wait_queue(&bl->waiting, &entry);
246 return buf;
249 DRM_DEBUG("Count = %d, wfh = %d\n",
250 atomic_read(&bl->count), atomic_read(&bl->wfh));
251 return drm_freelist_try(bl);