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.
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 * 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 $
32 #define __NO_VERSION__
35 int drm_waitlist_create(drm_waitlist_t
*bl
, int count
)
37 DRM_DEBUG("%d\n", count
);
38 if (bl
->count
) return -EINVAL
;
41 bl
->bufs
= drm_alloc((bl
->count
+ 2) * sizeof(*bl
->bufs
),
45 bl
->end
= &bl
->bufs
[bl
->count
+1];
46 bl
->write_lock
= SPIN_LOCK_UNLOCKED
;
47 bl
->read_lock
= SPIN_LOCK_UNLOCKED
;
51 int drm_waitlist_destroy(drm_waitlist_t
*bl
)
54 if (bl
->rp
!= bl
->wp
) return -EINVAL
;
55 if (bl
->bufs
) drm_free(bl
->bufs
,
56 (bl
->count
+ 2) * sizeof(*bl
->bufs
),
66 int drm_waitlist_put(drm_waitlist_t
*bl
, drm_buf_t
*buf
)
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
);
75 DRM_ERROR("Overflow while adding buffer %d from pid %d\n",
80 buf
->time_queued
= get_cycles();
82 buf
->list
= DRM_LIST_WAIT
;
84 spin_lock_irqsave(&bl
->write_lock
, flags
);
86 if (++bl
->wp
>= bl
->end
) bl
->wp
= bl
->bufs
;
87 spin_unlock_irqrestore(&bl
->write_lock
, flags
);
92 drm_buf_t
*drm_waitlist_get(drm_waitlist_t
*bl
)
97 spin_lock_irqsave(&bl
->read_lock
, flags
);
99 if (bl
->rp
== bl
->wp
) {
100 spin_unlock_irqrestore(&bl
->read_lock
, flags
);
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
);
110 int drm_freelist_create(drm_freelist_t
*bl
, int count
)
113 atomic_set(&bl
->count
, 0);
115 init_waitqueue_head(&bl
->waiting
);
118 atomic_set(&bl
->wfh
, 0);
123 int drm_freelist_destroy(drm_freelist_t
*bl
)
126 atomic_set(&bl
->count
, 0);
131 int drm_freelist_put(drm_device_t
*dev
, drm_freelist_t
*bl
, drm_buf_t
*buf
)
137 drm_device_dma_t
*dma
= dev
->dma
;
140 DRM_ERROR("No DMA support\n");
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
);
152 #if DRM_DMA_HISTOGRAM
153 buf
->time_freed
= get_cycles();
154 drm_histogram_compute(dev
, buf
);
156 buf
->list
= DRM_LIST_FREE
;
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");
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
);
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
);
181 static drm_buf_t
*drm_freelist_try(drm_freelist_t
*bl
)
189 if (!bl
) return NULL
;
193 old
= (unsigned int)bl
->next
;
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");
204 atomic_dec(&bl
->count
);
206 buf
= (drm_buf_t
*)old
;
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
);
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
));
235 add_wait_queue(&bl
->waiting
, &entry
);
236 current
->state
= TASK_INTERRUPTIBLE
;
238 if (!atomic_read(&bl
->wfh
)
239 && (buf
= drm_freelist_try(bl
))) break;
241 if (signal_pending(current
)) break;
243 current
->state
= TASK_RUNNING
;
244 remove_wait_queue(&bl
->waiting
, &entry
);
249 DRM_DEBUG("Count = %d, wfh = %d\n",
250 atomic_read(&bl
->count
), atomic_read(&bl
->wfh
));
251 return drm_freelist_try(bl
);