1 /* drm_lists.h -- Buffer list handling routines -*- linux-c -*-
2  * Created: Mon Apr 19 20:54:22 1999 by faith@valinux.com
3  *
4  * Copyright 1999 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 
32 #include "drmP.h"
33 
34 #if __HAVE_DMA_WAITLIST
35 
DRM(waitlist_create)36 int DRM(waitlist_create)(drm_waitlist_t *bl, int count)
37 {
38 	if (bl->count) return -EINVAL;
39 
40 	bl->bufs       = DRM(alloc)((bl->count + 2) * sizeof(*bl->bufs),
41 				    DRM_MEM_BUFLISTS);
42 
43 	if(!bl->bufs) return -ENOMEM;
44 	memset(bl->bufs, 0, sizeof(*bl->bufs));
45 	bl->count      = count;
46 	bl->rp	       = bl->bufs;
47 	bl->wp	       = bl->bufs;
48 	bl->end	       = &bl->bufs[bl->count+1];
49 	bl->write_lock = SPIN_LOCK_UNLOCKED;
50 	bl->read_lock  = SPIN_LOCK_UNLOCKED;
51 	return 0;
52 }
53 
DRM(waitlist_destroy)54 int DRM(waitlist_destroy)(drm_waitlist_t *bl)
55 {
56 	if (bl->rp != bl->wp) return -EINVAL;
57 	if (bl->bufs) DRM(free)(bl->bufs,
58 				(bl->count + 2) * sizeof(*bl->bufs),
59 				DRM_MEM_BUFLISTS);
60 	bl->count = 0;
61 	bl->bufs  = NULL;
62 	bl->rp	  = NULL;
63 	bl->wp	  = NULL;
64 	bl->end	  = NULL;
65 	return 0;
66 }
67 
DRM(waitlist_put)68 int DRM(waitlist_put)(drm_waitlist_t *bl, drm_buf_t *buf)
69 {
70 	int	      left;
71 	unsigned long flags;
72 
73 	left = DRM_LEFTCOUNT(bl);
74 	if (!left) {
75 		DRM_ERROR("Overflow while adding buffer %d from pid %d\n",
76 			  buf->idx, buf->pid);
77 		return -EINVAL;
78 	}
79 #if __HAVE_DMA_HISTOGRAM
80 	buf->time_queued = get_cycles();
81 #endif
82 	buf->list	 = DRM_LIST_WAIT;
83 
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);
88 
89 	return 0;
90 }
91 
DRM(waitlist_get)92 drm_buf_t *DRM(waitlist_get)(drm_waitlist_t *bl)
93 {
94 	drm_buf_t     *buf;
95 	unsigned long flags;
96 
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;
102 	}
103 	if (++bl->rp >= bl->end) bl->rp = bl->bufs;
104 	spin_unlock_irqrestore(&bl->read_lock, flags);
105 
106 	return buf;
107 }
108 
109 #endif /* __HAVE_DMA_WAITLIST */
110 
111 
112 #if __HAVE_DMA_FREELIST
113 
DRM(freelist_create)114 int DRM(freelist_create)(drm_freelist_t *bl, int count)
115 {
116 	atomic_set(&bl->count, 0);
117 	bl->next      = NULL;
118 	init_waitqueue_head(&bl->waiting);
119 	bl->low_mark  = 0;
120 	bl->high_mark = 0;
121 	atomic_set(&bl->wfh,   0);
122 	bl->lock      = SPIN_LOCK_UNLOCKED;
123 	++bl->initialized;
124 	return 0;
125 }
126 
DRM(freelist_destroy)127 int DRM(freelist_destroy)(drm_freelist_t *bl)
128 {
129 	atomic_set(&bl->count, 0);
130 	bl->next = NULL;
131 	return 0;
132 }
133 
DRM(freelist_put)134 int DRM(freelist_put)(drm_device_t *dev, drm_freelist_t *bl, drm_buf_t *buf)
135 {
136 	drm_device_dma_t *dma  = dev->dma;
137 
138 	if (!dma) {
139 		DRM_ERROR("No DMA support\n");
140 		return 1;
141 	}
142 
143 	if (buf->waiting || buf->pending || buf->list == DRM_LIST_FREE) {
144 		DRM_ERROR("Freed buffer %d: w%d, p%d, l%d\n",
145 			  buf->idx, buf->waiting, buf->pending, buf->list);
146 	}
147 	if (!bl) return 1;
148 #if __HAVE_DMA_HISTOGRAM
149 	buf->time_freed = get_cycles();
150 	DRM(histogram_compute)(dev, buf);
151 #endif
152 	buf->list	= DRM_LIST_FREE;
153 
154 	spin_lock(&bl->lock);
155 	buf->next	= bl->next;
156 	bl->next	= buf;
157 	spin_unlock(&bl->lock);
158 
159 	atomic_inc(&bl->count);
160 	if (atomic_read(&bl->count) > dma->buf_count) {
161 		DRM_ERROR("%d of %d buffers free after addition of %d\n",
162 			  atomic_read(&bl->count), dma->buf_count, buf->idx);
163 		return 1;
164 	}
165 				/* Check for high water mark */
166 	if (atomic_read(&bl->wfh) && atomic_read(&bl->count)>=bl->high_mark) {
167 		atomic_set(&bl->wfh, 0);
168 		wake_up_interruptible(&bl->waiting);
169 	}
170 	return 0;
171 }
172 
DRM(freelist_try)173 static drm_buf_t *DRM(freelist_try)(drm_freelist_t *bl)
174 {
175 	drm_buf_t	  *buf;
176 
177 	if (!bl) return NULL;
178 
179 				/* Get buffer */
180 	spin_lock(&bl->lock);
181 	if (!bl->next) {
182 		spin_unlock(&bl->lock);
183 		return NULL;
184 	}
185 	buf	  = bl->next;
186 	bl->next  = bl->next->next;
187 	spin_unlock(&bl->lock);
188 
189 	atomic_dec(&bl->count);
190 	buf->next = NULL;
191 	buf->list = DRM_LIST_NONE;
192 	if (buf->waiting || buf->pending) {
193 		DRM_ERROR("Free buffer %d: w%d, p%d, l%d\n",
194 			  buf->idx, buf->waiting, buf->pending, buf->list);
195 	}
196 
197 	return buf;
198 }
199 
DRM(freelist_get)200 drm_buf_t *DRM(freelist_get)(drm_freelist_t *bl, int block)
201 {
202 	drm_buf_t	  *buf	= NULL;
203 	DECLARE_WAITQUEUE(entry, current);
204 
205 	if (!bl || !bl->initialized) return NULL;
206 
207 				/* Check for low water mark */
208 	if (atomic_read(&bl->count) <= bl->low_mark) /* Became low */
209 		atomic_set(&bl->wfh, 1);
210 	if (atomic_read(&bl->wfh)) {
211 		if (block) {
212 			add_wait_queue(&bl->waiting, &entry);
213 			for (;;) {
214 				current->state = TASK_INTERRUPTIBLE;
215 				if (!atomic_read(&bl->wfh)
216 				    && (buf = DRM(freelist_try)(bl))) break;
217 				schedule();
218 				if (signal_pending(current)) break;
219 			}
220 			current->state = TASK_RUNNING;
221 			remove_wait_queue(&bl->waiting, &entry);
222 		}
223 		return buf;
224 	}
225 
226 	return DRM(freelist_try)(bl);
227 }
228 
229 #endif /* __HAVE_DMA_FREELIST */
230