aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/cx18/cx18-queue.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/cx18/cx18-queue.c')
-rw-r--r--drivers/media/video/cx18/cx18-queue.c346
1 files changed, 267 insertions, 79 deletions
diff --git a/drivers/media/video/cx18/cx18-queue.c b/drivers/media/video/cx18/cx18-queue.c
index fa1ed7897d97..63304823cef5 100644
--- a/drivers/media/video/cx18/cx18-queue.c
+++ b/drivers/media/video/cx18/cx18-queue.c
@@ -26,6 +26,7 @@
26#include "cx18-queue.h" 26#include "cx18-queue.h"
27#include "cx18-streams.h" 27#include "cx18-streams.h"
28#include "cx18-scb.h" 28#include "cx18-scb.h"
29#include "cx18-io.h"
29 30
30void cx18_buf_swap(struct cx18_buffer *buf) 31void cx18_buf_swap(struct cx18_buffer *buf)
31{ 32{
@@ -35,151 +36,312 @@ void cx18_buf_swap(struct cx18_buffer *buf)
35 swab32s((u32 *)(buf->buf + i)); 36 swab32s((u32 *)(buf->buf + i));
36} 37}
37 38
39void _cx18_mdl_swap(struct cx18_mdl *mdl)
40{
41 struct cx18_buffer *buf;
42
43 list_for_each_entry(buf, &mdl->buf_list, list) {
44 if (buf->bytesused == 0)
45 break;
46 cx18_buf_swap(buf);
47 }
48}
49
38void cx18_queue_init(struct cx18_queue *q) 50void cx18_queue_init(struct cx18_queue *q)
39{ 51{
40 INIT_LIST_HEAD(&q->list); 52 INIT_LIST_HEAD(&q->list);
41 atomic_set(&q->buffers, 0); 53 atomic_set(&q->depth, 0);
42 q->bytesused = 0; 54 q->bytesused = 0;
43} 55}
44 56
45struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf, 57struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
46 struct cx18_queue *q, int to_front) 58 struct cx18_queue *q, int to_front)
47{ 59{
48 /* clear the buffer if it is not to be enqueued to the full queue */ 60 /* clear the mdl if it is not to be enqueued to the full queue */
49 if (q != &s->q_full) { 61 if (q != &s->q_full) {
50 buf->bytesused = 0; 62 mdl->bytesused = 0;
51 buf->readpos = 0; 63 mdl->readpos = 0;
52 buf->b_flags = 0; 64 mdl->m_flags = 0;
53 buf->skipped = 0; 65 mdl->skipped = 0;
66 mdl->curr_buf = NULL;
54 } 67 }
55 68
56 /* q_busy is restricted to a max buffer count imposed by firmware */ 69 /* q_busy is restricted to a max buffer count imposed by firmware */
57 if (q == &s->q_busy && 70 if (q == &s->q_busy &&
58 atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM) 71 atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
59 q = &s->q_free; 72 q = &s->q_free;
60 73
61 spin_lock(&q->lock); 74 spin_lock(&q->lock);
62 75
63 if (to_front) 76 if (to_front)
64 list_add(&buf->list, &q->list); /* LIFO */ 77 list_add(&mdl->list, &q->list); /* LIFO */
65 else 78 else
66 list_add_tail(&buf->list, &q->list); /* FIFO */ 79 list_add_tail(&mdl->list, &q->list); /* FIFO */
67 q->bytesused += buf->bytesused - buf->readpos; 80 q->bytesused += mdl->bytesused - mdl->readpos;
68 atomic_inc(&q->buffers); 81 atomic_inc(&q->depth);
69 82
70 spin_unlock(&q->lock); 83 spin_unlock(&q->lock);
71 return q; 84 return q;
72} 85}
73 86
74struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) 87struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
75{ 88{
76 struct cx18_buffer *buf = NULL; 89 struct cx18_mdl *mdl = NULL;
77 90
78 spin_lock(&q->lock); 91 spin_lock(&q->lock);
79 if (!list_empty(&q->list)) { 92 if (!list_empty(&q->list)) {
80 buf = list_first_entry(&q->list, struct cx18_buffer, list); 93 mdl = list_first_entry(&q->list, struct cx18_mdl, list);
81 list_del_init(&buf->list); 94 list_del_init(&mdl->list);
82 q->bytesused -= buf->bytesused - buf->readpos; 95 q->bytesused -= mdl->bytesused - mdl->readpos;
83 buf->skipped = 0; 96 mdl->skipped = 0;
84 atomic_dec(&q->buffers); 97 atomic_dec(&q->depth);
85 } 98 }
86 spin_unlock(&q->lock); 99 spin_unlock(&q->lock);
87 return buf; 100 return mdl;
101}
102
103static void _cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
104 struct cx18_mdl *mdl)
105{
106 struct cx18_buffer *buf;
107 u32 buf_size = s->buf_size;
108 u32 bytesused = mdl->bytesused;
109
110 list_for_each_entry(buf, &mdl->buf_list, list) {
111 buf->readpos = 0;
112 if (bytesused >= buf_size) {
113 buf->bytesused = buf_size;
114 bytesused -= buf_size;
115 } else {
116 buf->bytesused = bytesused;
117 bytesused = 0;
118 }
119 cx18_buf_sync_for_cpu(s, buf);
120 }
121}
122
123static inline void cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
124 struct cx18_mdl *mdl)
125{
126 struct cx18_buffer *buf;
127
128 if (list_is_singular(&mdl->buf_list)) {
129 buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
130 list);
131 buf->bytesused = mdl->bytesused;
132 buf->readpos = 0;
133 cx18_buf_sync_for_cpu(s, buf);
134 } else {
135 _cx18_mdl_update_bufs_for_cpu(s, mdl);
136 }
88} 137}
89 138
90struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id, 139struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
91 u32 bytesused) 140 u32 bytesused)
92{ 141{
93 struct cx18 *cx = s->cx; 142 struct cx18 *cx = s->cx;
94 struct cx18_buffer *buf; 143 struct cx18_mdl *mdl;
95 struct cx18_buffer *tmp; 144 struct cx18_mdl *tmp;
96 struct cx18_buffer *ret = NULL; 145 struct cx18_mdl *ret = NULL;
97 LIST_HEAD(sweep_up); 146 LIST_HEAD(sweep_up);
98 147
99 /* 148 /*
100 * We don't have to acquire multiple q locks here, because we are 149 * We don't have to acquire multiple q locks here, because we are
101 * serialized by the single threaded work handler. 150 * serialized by the single threaded work handler.
102 * Buffers from the firmware will thus remain in order as 151 * MDLs from the firmware will thus remain in order as
103 * they are moved from q_busy to q_full or to the dvb ring buffer. 152 * they are moved from q_busy to q_full or to the dvb ring buffer.
104 */ 153 */
105 spin_lock(&s->q_busy.lock); 154 spin_lock(&s->q_busy.lock);
106 list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) { 155 list_for_each_entry_safe(mdl, tmp, &s->q_busy.list, list) {
107 /* 156 /*
108 * We should find what the firmware told us is done, 157 * We should find what the firmware told us is done,
109 * right at the front of the queue. If we don't, we likely have 158 * right at the front of the queue. If we don't, we likely have
110 * missed a buffer done message from the firmware. 159 * missed an mdl done message from the firmware.
111 * Once we skip a buffer repeatedly, relative to the size of 160 * Once we skip an mdl repeatedly, relative to the size of
112 * q_busy, we have high confidence we've missed it. 161 * q_busy, we have high confidence we've missed it.
113 */ 162 */
114 if (buf->id != id) { 163 if (mdl->id != id) {
115 buf->skipped++; 164 mdl->skipped++;
116 if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) { 165 if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) {
117 /* buffer must have fallen out of rotation */ 166 /* mdl must have fallen out of rotation */
118 CX18_WARN("Skipped %s, buffer %d, %d " 167 CX18_WARN("Skipped %s, MDL %d, %d "
119 "times - it must have dropped out of " 168 "times - it must have dropped out of "
120 "rotation\n", s->name, buf->id, 169 "rotation\n", s->name, mdl->id,
121 buf->skipped); 170 mdl->skipped);
122 /* Sweep it up to put it back into rotation */ 171 /* Sweep it up to put it back into rotation */
123 list_move_tail(&buf->list, &sweep_up); 172 list_move_tail(&mdl->list, &sweep_up);
124 atomic_dec(&s->q_busy.buffers); 173 atomic_dec(&s->q_busy.depth);
125 } 174 }
126 continue; 175 continue;
127 } 176 }
128 /* 177 /*
129 * We pull the desired buffer off of the queue here. Something 178 * We pull the desired mdl off of the queue here. Something
130 * will have to put it back on a queue later. 179 * will have to put it back on a queue later.
131 */ 180 */
132 list_del_init(&buf->list); 181 list_del_init(&mdl->list);
133 atomic_dec(&s->q_busy.buffers); 182 atomic_dec(&s->q_busy.depth);
134 ret = buf; 183 ret = mdl;
135 break; 184 break;
136 } 185 }
137 spin_unlock(&s->q_busy.lock); 186 spin_unlock(&s->q_busy.lock);
138 187
139 /* 188 /*
140 * We found the buffer for which we were looking. Get it ready for 189 * We found the mdl for which we were looking. Get it ready for
141 * the caller to put on q_full or in the dvb ring buffer. 190 * the caller to put on q_full or in the dvb ring buffer.
142 */ 191 */
143 if (ret != NULL) { 192 if (ret != NULL) {
144 ret->bytesused = bytesused; 193 ret->bytesused = bytesused;
145 ret->skipped = 0; 194 ret->skipped = 0;
146 /* readpos and b_flags were 0'ed when the buf went on q_busy */ 195 /* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */
147 cx18_buf_sync_for_cpu(s, ret); 196 cx18_mdl_update_bufs_for_cpu(s, ret);
148 if (s->type != CX18_ENC_STREAM_TYPE_TS) 197 if (s->type != CX18_ENC_STREAM_TYPE_TS)
149 set_bit(CX18_F_B_NEED_BUF_SWAP, &ret->b_flags); 198 set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags);
150 } 199 }
151 200
152 /* Put any buffers the firmware is ignoring back into normal rotation */ 201 /* Put any mdls the firmware is ignoring back into normal rotation */
153 list_for_each_entry_safe(buf, tmp, &sweep_up, list) { 202 list_for_each_entry_safe(mdl, tmp, &sweep_up, list) {
154 list_del_init(&buf->list); 203 list_del_init(&mdl->list);
155 cx18_enqueue(s, buf, &s->q_free); 204 cx18_enqueue(s, mdl, &s->q_free);
156 } 205 }
157 return ret; 206 return ret;
158} 207}
159 208
160/* Move all buffers of a queue to q_free, while flushing the buffers */ 209/* Move all mdls of a queue, while flushing the mdl */
161static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q) 210static void cx18_queue_flush(struct cx18_stream *s,
211 struct cx18_queue *q_src, struct cx18_queue *q_dst)
162{ 212{
163 struct cx18_buffer *buf; 213 struct cx18_mdl *mdl;
164 214
165 if (q == &s->q_free) 215 /* It only makes sense to flush to q_free or q_idle */
216 if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy)
166 return; 217 return;
167 218
168 spin_lock(&q->lock); 219 spin_lock(&q_src->lock);
169 while (!list_empty(&q->list)) { 220 spin_lock(&q_dst->lock);
170 buf = list_first_entry(&q->list, struct cx18_buffer, list); 221 while (!list_empty(&q_src->list)) {
171 list_move_tail(&buf->list, &s->q_free.list); 222 mdl = list_first_entry(&q_src->list, struct cx18_mdl, list);
172 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0; 223 list_move_tail(&mdl->list, &q_dst->list);
173 atomic_inc(&s->q_free.buffers); 224 mdl->bytesused = 0;
225 mdl->readpos = 0;
226 mdl->m_flags = 0;
227 mdl->skipped = 0;
228 mdl->curr_buf = NULL;
229 atomic_inc(&q_dst->depth);
174 } 230 }
175 cx18_queue_init(q); 231 cx18_queue_init(q_src);
176 spin_unlock(&q->lock); 232 spin_unlock(&q_src->lock);
233 spin_unlock(&q_dst->lock);
177} 234}
178 235
179void cx18_flush_queues(struct cx18_stream *s) 236void cx18_flush_queues(struct cx18_stream *s)
180{ 237{
181 cx18_queue_flush(s, &s->q_busy); 238 cx18_queue_flush(s, &s->q_busy, &s->q_free);
182 cx18_queue_flush(s, &s->q_full); 239 cx18_queue_flush(s, &s->q_full, &s->q_free);
240}
241
242/*
243 * Note, s->buf_pool is not protected by a lock,
244 * the stream better not have *anything* going on when calling this
245 */
246void cx18_unload_queues(struct cx18_stream *s)
247{
248 struct cx18_queue *q_idle = &s->q_idle;
249 struct cx18_mdl *mdl;
250 struct cx18_buffer *buf;
251
252 /* Move all MDLS to q_idle */
253 cx18_queue_flush(s, &s->q_busy, q_idle);
254 cx18_queue_flush(s, &s->q_full, q_idle);
255 cx18_queue_flush(s, &s->q_free, q_idle);
256
257 /* Reset MDL id's and move all buffers back to the stream's buf_pool */
258 spin_lock(&q_idle->lock);
259 list_for_each_entry(mdl, &q_idle->list, list) {
260 while (!list_empty(&mdl->buf_list)) {
261 buf = list_first_entry(&mdl->buf_list,
262 struct cx18_buffer, list);
263 list_move_tail(&buf->list, &s->buf_pool);
264 buf->bytesused = 0;
265 buf->readpos = 0;
266 }
267 mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */
268 /* all other mdl fields were cleared by cx18_queue_flush() */
269 }
270 spin_unlock(&q_idle->lock);
271}
272
273/*
274 * Note, s->buf_pool is not protected by a lock,
275 * the stream better not have *anything* going on when calling this
276 */
277void cx18_load_queues(struct cx18_stream *s)
278{
279 struct cx18 *cx = s->cx;
280 struct cx18_mdl *mdl;
281 struct cx18_buffer *buf;
282 int mdl_id;
283 int i;
284 u32 partial_buf_size;
285
286 /*
287 * Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free
288 * Excess MDLs are left on q_idle
289 * Excess buffers are left in buf_pool and/or on an MDL in q_idle
290 */
291 mdl_id = s->mdl_base_idx;
292 for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl;
293 mdl != NULL && i == s->bufs_per_mdl;
294 mdl = cx18_dequeue(s, &s->q_idle)) {
295
296 mdl->id = mdl_id;
297
298 for (i = 0; i < s->bufs_per_mdl; i++) {
299 if (list_empty(&s->buf_pool))
300 break;
301
302 buf = list_first_entry(&s->buf_pool, struct cx18_buffer,
303 list);
304 list_move_tail(&buf->list, &mdl->buf_list);
305
306 /* update the firmware's MDL array with this buffer */
307 cx18_writel(cx, buf->dma_handle,
308 &cx->scb->cpu_mdl[mdl_id + i].paddr);
309 cx18_writel(cx, s->buf_size,
310 &cx->scb->cpu_mdl[mdl_id + i].length);
311 }
312
313 if (i == s->bufs_per_mdl) {
314 /*
315 * The encoder doesn't honor s->mdl_size. So in the
316 * case of a non-integral number of buffers to meet
317 * mdl_size, we lie about the size of the last buffer
318 * in the MDL to get the encoder to really only send
319 * us mdl_size bytes per MDL transfer.
320 */
321 partial_buf_size = s->mdl_size % s->buf_size;
322 if (partial_buf_size) {
323 cx18_writel(cx, partial_buf_size,
324 &cx->scb->cpu_mdl[mdl_id + i - 1].length);
325 }
326 cx18_enqueue(s, mdl, &s->q_free);
327 } else {
328 /* Not enough buffers for this MDL; we won't use it */
329 cx18_push(s, mdl, &s->q_idle);
330 }
331 mdl_id += i;
332 }
333}
334
335void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl)
336{
337 int dma = s->dma;
338 u32 buf_size = s->buf_size;
339 struct pci_dev *pci_dev = s->cx->pci_dev;
340 struct cx18_buffer *buf;
341
342 list_for_each_entry(buf, &mdl->buf_list, list)
343 pci_dma_sync_single_for_device(pci_dev, buf->dma_handle,
344 buf_size, dma);
183} 345}
184 346
185int cx18_stream_alloc(struct cx18_stream *s) 347int cx18_stream_alloc(struct cx18_stream *s)
@@ -190,44 +352,62 @@ int cx18_stream_alloc(struct cx18_stream *s)
190 if (s->buffers == 0) 352 if (s->buffers == 0)
191 return 0; 353 return 0;
192 354
193 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n", 355 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers "
356 "(%d.%02d kB total)\n",
194 s->name, s->buffers, s->buf_size, 357 s->name, s->buffers, s->buf_size,
195 s->buffers * s->buf_size / 1024); 358 s->buffers * s->buf_size / 1024,
359 (s->buffers * s->buf_size * 100 / 1024) % 100);
196 360
197 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] - 361 if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] -
198 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) { 362 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
199 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE - 363 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
200 ((char __iomem *)cx->scb->cpu_mdl)); 364 ((char __iomem *)cx->scb->cpu_mdl));
201 365
202 CX18_ERR("Too many buffers, cannot fit in SCB area\n"); 366 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
203 CX18_ERR("Max buffers = %zd\n", 367 CX18_ERR("Max buffers = %zd\n",
204 bufsz / sizeof(struct cx18_mdl)); 368 bufsz / sizeof(struct cx18_mdl_ent));
205 return -ENOMEM; 369 return -ENOMEM;
206 } 370 }
207 371
208 s->mdl_offset = cx->mdl_offset; 372 s->mdl_base_idx = cx->free_mdl_idx;
209 373
210 /* allocate stream buffers. Initially all buffers are in q_free. */ 374 /* allocate stream buffers and MDLs */
211 for (i = 0; i < s->buffers; i++) { 375 for (i = 0; i < s->buffers; i++) {
212 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer), 376 struct cx18_mdl *mdl;
213 GFP_KERNEL|__GFP_NOWARN); 377 struct cx18_buffer *buf;
214 378
215 if (buf == NULL) 379 /* 1 MDL per buffer to handle the worst & also default case */
380 mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN);
381 if (mdl == NULL)
216 break; 382 break;
383
384 buf = kzalloc(sizeof(struct cx18_buffer),
385 GFP_KERNEL|__GFP_NOWARN);
386 if (buf == NULL) {
387 kfree(mdl);
388 break;
389 }
390
217 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN); 391 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
218 if (buf->buf == NULL) { 392 if (buf->buf == NULL) {
393 kfree(mdl);
219 kfree(buf); 394 kfree(buf);
220 break; 395 break;
221 } 396 }
222 buf->id = cx->buffer_id++; 397
398 INIT_LIST_HEAD(&mdl->list);
399 INIT_LIST_HEAD(&mdl->buf_list);
400 mdl->id = s->mdl_base_idx; /* a somewhat safe value */
401 cx18_enqueue(s, mdl, &s->q_idle);
402
223 INIT_LIST_HEAD(&buf->list); 403 INIT_LIST_HEAD(&buf->list);
224 buf->dma_handle = pci_map_single(s->cx->pci_dev, 404 buf->dma_handle = pci_map_single(s->cx->pci_dev,
225 buf->buf, s->buf_size, s->dma); 405 buf->buf, s->buf_size, s->dma);
226 cx18_buf_sync_for_cpu(s, buf); 406 cx18_buf_sync_for_cpu(s, buf);
227 cx18_enqueue(s, buf, &s->q_free); 407 list_add_tail(&buf->list, &s->buf_pool);
228 } 408 }
229 if (i == s->buffers) { 409 if (i == s->buffers) {
230 cx->mdl_offset += s->buffers; 410 cx->free_mdl_idx += s->buffers;
231 return 0; 411 return 0;
232 } 412 }
233 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name); 413 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
@@ -237,13 +417,21 @@ int cx18_stream_alloc(struct cx18_stream *s)
237 417
238void cx18_stream_free(struct cx18_stream *s) 418void cx18_stream_free(struct cx18_stream *s)
239{ 419{
420 struct cx18_mdl *mdl;
240 struct cx18_buffer *buf; 421 struct cx18_buffer *buf;
241 422
242 /* move all buffers to q_free */ 423 /* move all buffers to buf_pool and all MDLs to q_idle */
243 cx18_flush_queues(s); 424 cx18_unload_queues(s);
425
426 /* empty q_idle */
427 while ((mdl = cx18_dequeue(s, &s->q_idle)))
428 kfree(mdl);
429
430 /* empty buf_pool */
431 while (!list_empty(&s->buf_pool)) {
432 buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list);
433 list_del_init(&buf->list);
244 434
245 /* empty q_free */
246 while ((buf = cx18_dequeue(s, &s->q_free))) {
247 pci_unmap_single(s->cx->pci_dev, buf->dma_handle, 435 pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
248 s->buf_size, s->dma); 436 s->buf_size, s->dma);
249 kfree(buf->buf); 437 kfree(buf->buf);