4 * Derived from ivtv-queue.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include "cx18-driver.h"
26 #include "cx18-queue.h"
27 #include "cx18-streams.h"
31 void cx18_buf_swap(struct cx18_buffer *buf)
35 for (i = 0; i < buf->bytesused; i += 4)
36 swab32s((u32 *)(buf->buf + i));
39 void _cx18_mdl_swap(struct cx18_mdl *mdl)
41 struct cx18_buffer *buf;
43 list_for_each_entry(buf, &mdl->buf_list, list) {
44 if (buf->bytesused == 0)
50 void cx18_queue_init(struct cx18_queue *q)
52 INIT_LIST_HEAD(&q->list);
53 atomic_set(&q->depth, 0);
57 struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
58 struct cx18_queue *q, int to_front)
60 /* clear the mdl if it is not to be enqueued to the full queue */
61 if (q != &s->q_full) {
69 /* q_busy is restricted to a max buffer count imposed by firmware */
70 if (q == &s->q_busy &&
71 atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
77 list_add(&mdl->list, &q->list); /* LIFO */
79 list_add_tail(&mdl->list, &q->list); /* FIFO */
80 q->bytesused += mdl->bytesused - mdl->readpos;
81 atomic_inc(&q->depth);
83 spin_unlock(&q->lock);
87 struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
89 struct cx18_mdl *mdl = NULL;
92 if (!list_empty(&q->list)) {
93 mdl = list_first_entry(&q->list, struct cx18_mdl, list);
94 list_del_init(&mdl->list);
95 q->bytesused -= mdl->bytesused - mdl->readpos;
97 atomic_dec(&q->depth);
99 spin_unlock(&q->lock);
103 static void _cx18_mdl_set_buf_bytesused(struct cx18_stream *s,
104 struct cx18_mdl *mdl)
106 struct cx18_buffer *buf;
107 u32 buf_size = s->buf_size;
108 u32 bytesused = mdl->bytesused;
110 list_for_each_entry(buf, &mdl->buf_list, list) {
112 if (bytesused >= buf_size) {
113 buf->bytesused = buf_size;
114 bytesused -= buf_size;
116 buf->bytesused = bytesused;
122 static inline void cx18_mdl_set_buf_bytesused(struct cx18_stream *s,
123 struct cx18_mdl *mdl)
125 struct cx18_buffer *buf;
127 if (list_is_singular(&mdl->buf_list)) {
128 buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
130 buf->bytesused = mdl->bytesused;
133 _cx18_mdl_set_buf_bytesused(s, mdl);
137 struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
140 struct cx18 *cx = s->cx;
141 struct cx18_mdl *mdl;
142 struct cx18_mdl *tmp;
143 struct cx18_mdl *ret = NULL;
147 * We don't have to acquire multiple q locks here, because we are
148 * serialized by the single threaded work handler.
149 * MDLs from the firmware will thus remain in order as
150 * they are moved from q_busy to q_full or to the dvb ring buffer.
152 spin_lock(&s->q_busy.lock);
153 list_for_each_entry_safe(mdl, tmp, &s->q_busy.list, list) {
155 * We should find what the firmware told us is done,
156 * right at the front of the queue. If we don't, we likely have
157 * missed an mdl done message from the firmware.
158 * Once we skip an mdl repeatedly, relative to the size of
159 * q_busy, we have high confidence we've missed it.
163 if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) {
164 /* mdl must have fallen out of rotation */
165 CX18_WARN("Skipped %s, MDL %d, %d "
166 "times - it must have dropped out of "
167 "rotation\n", s->name, mdl->id,
169 /* Sweep it up to put it back into rotation */
170 list_move_tail(&mdl->list, &sweep_up);
171 atomic_dec(&s->q_busy.depth);
176 * We pull the desired mdl off of the queue here. Something
177 * will have to put it back on a queue later.
179 list_del_init(&mdl->list);
180 atomic_dec(&s->q_busy.depth);
184 spin_unlock(&s->q_busy.lock);
187 * We found the mdl for which we were looking. Get it ready for
188 * the caller to put on q_full or in the dvb ring buffer.
191 ret->bytesused = bytesused;
193 /* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */
194 cx18_mdl_set_buf_bytesused(s, ret);
195 cx18_mdl_sync_for_cpu(s, ret);
196 if (s->type != CX18_ENC_STREAM_TYPE_TS)
197 set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags);
200 /* Put any mdls the firmware is ignoring back into normal rotation */
201 list_for_each_entry_safe(mdl, tmp, &sweep_up, list) {
202 list_del_init(&mdl->list);
203 cx18_enqueue(s, mdl, &s->q_free);
208 /* Move all mdls of a queue, while flushing the mdl */
209 static void cx18_queue_flush(struct cx18_stream *s,
210 struct cx18_queue *q_src, struct cx18_queue *q_dst)
212 struct cx18_mdl *mdl;
214 /* It only makes sense to flush to q_free or q_idle */
215 if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy)
218 spin_lock(&q_src->lock);
219 spin_lock(&q_dst->lock);
220 while (!list_empty(&q_src->list)) {
221 mdl = list_first_entry(&q_src->list, struct cx18_mdl, list);
222 list_move_tail(&mdl->list, &q_dst->list);
227 mdl->curr_buf = NULL;
228 atomic_inc(&q_dst->depth);
230 cx18_queue_init(q_src);
231 spin_unlock(&q_src->lock);
232 spin_unlock(&q_dst->lock);
235 void cx18_flush_queues(struct cx18_stream *s)
237 cx18_queue_flush(s, &s->q_busy, &s->q_free);
238 cx18_queue_flush(s, &s->q_full, &s->q_free);
242 * Note, s->buf_pool is not protected by a lock,
243 * the stream better not have *anything* going on when calling this
245 void cx18_unload_queues(struct cx18_stream *s)
247 struct cx18_queue *q_idle = &s->q_idle;
248 struct cx18_mdl *mdl;
249 struct cx18_buffer *buf;
251 /* Move all MDLS to q_idle */
252 cx18_queue_flush(s, &s->q_busy, q_idle);
253 cx18_queue_flush(s, &s->q_full, q_idle);
254 cx18_queue_flush(s, &s->q_free, q_idle);
256 /* Reset MDL id's and move all buffers back to the stream's buf_pool */
257 spin_lock(&q_idle->lock);
258 list_for_each_entry(mdl, &q_idle->list, list) {
259 while (!list_empty(&mdl->buf_list)) {
260 buf = list_first_entry(&mdl->buf_list,
261 struct cx18_buffer, list);
262 list_move_tail(&buf->list, &s->buf_pool);
266 mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */
267 /* all other mdl fields were cleared by cx18_queue_flush() */
269 spin_unlock(&q_idle->lock);
273 * Note, s->buf_pool is not protected by a lock,
274 * the stream better not have *anything* going on when calling this
276 void cx18_load_queues(struct cx18_stream *s)
278 struct cx18 *cx = s->cx;
279 struct cx18_mdl *mdl;
280 struct cx18_buffer *buf;
285 * Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free
286 * Excess MDLs are left on q_idle
287 * Excess buffers are left in buf_pool and/or on an MDL in q_idle
289 mdl_id = s->mdl_base_idx;
290 for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl;
291 mdl != NULL && i == s->bufs_per_mdl;
292 mdl = cx18_dequeue(s, &s->q_idle)) {
296 for (i = 0; i < s->bufs_per_mdl; i++) {
297 if (list_empty(&s->buf_pool))
300 buf = list_first_entry(&s->buf_pool, struct cx18_buffer,
302 list_move_tail(&buf->list, &mdl->buf_list);
304 /* update the firmware's MDL array with this buffer */
305 cx18_writel(cx, buf->dma_handle,
306 &cx->scb->cpu_mdl[mdl_id + i].paddr);
307 cx18_writel(cx, s->buf_size,
308 &cx->scb->cpu_mdl[mdl_id + i].length);
311 if (i == s->bufs_per_mdl)
312 cx18_enqueue(s, mdl, &s->q_free);
314 cx18_push(s, mdl, &s->q_idle); /* not enough buffers */
319 void _cx18_mdl_sync_for_cpu(struct cx18_stream *s, struct cx18_mdl *mdl)
322 u32 buf_size = s->buf_size;
323 struct pci_dev *pci_dev = s->cx->pci_dev;
324 struct cx18_buffer *buf;
326 list_for_each_entry(buf, &mdl->buf_list, list)
327 pci_dma_sync_single_for_cpu(pci_dev, buf->dma_handle,
331 void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl)
334 u32 buf_size = s->buf_size;
335 struct pci_dev *pci_dev = s->cx->pci_dev;
336 struct cx18_buffer *buf;
338 list_for_each_entry(buf, &mdl->buf_list, list)
339 pci_dma_sync_single_for_device(pci_dev, buf->dma_handle,
343 int cx18_stream_alloc(struct cx18_stream *s)
345 struct cx18 *cx = s->cx;
351 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers "
352 "(%d.%02d kB total)\n",
353 s->name, s->buffers, s->buf_size,
354 s->buffers * s->buf_size / 1024,
355 (s->buffers * s->buf_size * 100 / 1024) % 100);
357 if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] -
358 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
359 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
360 ((char __iomem *)cx->scb->cpu_mdl));
362 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
363 CX18_ERR("Max buffers = %zd\n",
364 bufsz / sizeof(struct cx18_mdl_ent));
368 s->mdl_base_idx = cx->free_mdl_idx;
370 /* allocate stream buffers and MDLs */
371 for (i = 0; i < s->buffers; i++) {
372 struct cx18_mdl *mdl;
373 struct cx18_buffer *buf;
375 /* 1 MDL per buffer to handle the worst & also default case */
376 mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN);
380 buf = kzalloc(sizeof(struct cx18_buffer),
381 GFP_KERNEL|__GFP_NOWARN);
387 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
388 if (buf->buf == NULL) {
394 INIT_LIST_HEAD(&mdl->list);
395 INIT_LIST_HEAD(&mdl->buf_list);
396 mdl->id = s->mdl_base_idx; /* a somewhat safe value */
397 cx18_enqueue(s, mdl, &s->q_idle);
399 INIT_LIST_HEAD(&buf->list);
400 buf->dma_handle = pci_map_single(s->cx->pci_dev,
401 buf->buf, s->buf_size, s->dma);
402 cx18_buf_sync_for_cpu(s, buf);
403 list_add_tail(&buf->list, &s->buf_pool);
405 if (i == s->buffers) {
406 cx->free_mdl_idx += s->buffers;
409 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
414 void cx18_stream_free(struct cx18_stream *s)
416 struct cx18_mdl *mdl;
417 struct cx18_buffer *buf;
419 /* move all buffers to buf_pool and all MDLs to q_idle */
420 cx18_unload_queues(s);
423 while ((mdl = cx18_dequeue(s, &s->q_idle)))
427 while (!list_empty(&s->buf_pool)) {
428 buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list);
429 list_del_init(&buf->list);
431 pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
432 s->buf_size, s->dma);