blob: a6b0666f0ba3a6f3138ded66a33f358b8d4e900f [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 buffer queues
3 *
4 * Derived from ivtv-queue.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls1ed9dcc2008-11-22 01:37:34 -03007 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
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.
13 *
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.
18 *
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
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-streams.h"
27#include "cx18-queue.h"
28#include "cx18-scb.h"
29
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030030void cx18_buf_swap(struct cx18_buffer *buf)
31{
32 int i;
33
34 for (i = 0; i < buf->bytesused; i += 4)
35 swab32s((u32 *)(buf->buf + i));
36}
37
38void cx18_queue_init(struct cx18_queue *q)
39{
40 INIT_LIST_HEAD(&q->list);
Andy Wallsb04bce42008-08-22 21:03:11 -030041 atomic_set(&q->buffers, 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030042 q->bytesused = 0;
43}
44
Andy Walls66c2a6b2008-12-08 23:02:45 -030045struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
46 struct cx18_queue *q, int to_front)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030047{
Andy Walls66c2a6b2008-12-08 23:02:45 -030048 /* clear the buffer if it is not to be enqueued to the full queue */
49 if (q != &s->q_full) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030050 buf->bytesused = 0;
51 buf->readpos = 0;
52 buf->b_flags = 0;
Andy Wallsbca11a52008-11-19 01:24:33 -030053 buf->skipped = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030054 }
Andy Walls66c2a6b2008-12-08 23:02:45 -030055
Andy Wallsf576cee2008-11-16 20:18:05 -030056 mutex_lock(&s->qlock);
Andy Walls66c2a6b2008-12-08 23:02:45 -030057
58 /* q_busy is restricted to 63 buffers to stay within firmware limits */
59 if (q == &s->q_busy && atomic_read(&q->buffers) >= 63)
60 q = &s->q_free;
61
Andy Wallsb80e1072008-11-28 00:04:21 -030062 if (to_front)
63 list_add(&buf->list, &q->list); /* LIFO */
64 else
65 list_add_tail(&buf->list, &q->list); /* FIFO */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030066 q->bytesused += buf->bytesused - buf->readpos;
Andy Walls66c2a6b2008-12-08 23:02:45 -030067 atomic_inc(&q->buffers);
68
Andy Wallsf576cee2008-11-16 20:18:05 -030069 mutex_unlock(&s->qlock);
Andy Walls66c2a6b2008-12-08 23:02:45 -030070 return q;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030071}
72
73struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
74{
75 struct cx18_buffer *buf = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030076
Andy Wallsf576cee2008-11-16 20:18:05 -030077 mutex_lock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030078 if (!list_empty(&q->list)) {
79 buf = list_entry(q->list.next, struct cx18_buffer, list);
80 list_del_init(q->list.next);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030081 q->bytesused -= buf->bytesused - buf->readpos;
Andy Wallsbca11a52008-11-19 01:24:33 -030082 buf->skipped = 0;
Andy Walls66c2a6b2008-12-08 23:02:45 -030083 atomic_dec(&q->buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030084 }
Andy Wallsf576cee2008-11-16 20:18:05 -030085 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030086 return buf;
87}
88
Andy Wallsee2d64f2008-11-16 01:38:19 -030089struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030090 u32 bytesused)
91{
92 struct cx18 *cx = s->cx;
Andy Wallsbca11a52008-11-19 01:24:33 -030093 struct cx18_buffer *buf;
94 struct cx18_buffer *ret = NULL;
95 struct list_head *p, *t;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030096
Andy Wallsf576cee2008-11-16 20:18:05 -030097 mutex_lock(&s->qlock);
Andy Walls66c2a6b2008-12-08 23:02:45 -030098 list_for_each_safe(p, t, &s->q_busy.list) {
Andy Wallsbca11a52008-11-19 01:24:33 -030099 buf = list_entry(p, struct cx18_buffer, list);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300100
Andy Wallsee2d64f2008-11-16 01:38:19 -0300101 if (buf->id != id) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300102 buf->skipped++;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300103 if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300104 /* buffer must have fallen out of rotation */
Andy Wallsbca11a52008-11-19 01:24:33 -0300105 CX18_WARN("Skipped %s, buffer %d, %d "
106 "times - it must have dropped out of "
107 "rotation\n", s->name, buf->id,
108 buf->skipped);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300109 /* move it to q_free */
110 list_move_tail(&buf->list, &s->q_free.list);
111 buf->bytesused = buf->readpos = buf->b_flags =
112 buf->skipped = 0;
113 atomic_dec(&s->q_busy.buffers);
114 atomic_inc(&s->q_free.buffers);
Andy Wallsbca11a52008-11-19 01:24:33 -0300115 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300116 continue;
Andy Wallsee2d64f2008-11-16 01:38:19 -0300117 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300118
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300119 buf->bytesused = bytesused;
Andy Wallsabb096d2008-12-12 15:50:27 -0300120 /* Sync the buffer before we release the qlock */
121 cx18_buf_sync_for_cpu(s, buf);
Andy Wallsbca11a52008-11-19 01:24:33 -0300122 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
123 /*
Andy Wallsabb096d2008-12-12 15:50:27 -0300124 * TS doesn't use q_full. As we pull the buffer off of
125 * the queue here, the caller will have to put it back.
Andy Wallsbca11a52008-11-19 01:24:33 -0300126 */
127 list_del_init(&buf->list);
128 } else {
Andy Wallsabb096d2008-12-12 15:50:27 -0300129 /* Move buffer from q_busy to q_full */
Andy Wallsee2d64f2008-11-16 01:38:19 -0300130 list_move_tail(&buf->list, &s->q_full.list);
Andy Wallsabb096d2008-12-12 15:50:27 -0300131 set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300132 s->q_full.bytesused += buf->bytesused;
133 atomic_inc(&s->q_full.buffers);
Andy Wallsee2d64f2008-11-16 01:38:19 -0300134 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300135 atomic_dec(&s->q_busy.buffers);
Andy Walls1d6782b2008-11-05 00:49:14 -0300136
Andy Wallsbca11a52008-11-19 01:24:33 -0300137 ret = buf;
138 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300139 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300140 mutex_unlock(&s->qlock);
Andy Wallsbca11a52008-11-19 01:24:33 -0300141 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300142}
143
Andy Walls6c9de522008-09-03 17:11:54 -0300144/* Move all buffers of a queue to q_free, while flushing the buffers */
145static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300146{
Andy Walls6c9de522008-09-03 17:11:54 -0300147 struct cx18_buffer *buf;
148
149 if (q == &s->q_free)
150 return;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300151
Andy Wallsf576cee2008-11-16 20:18:05 -0300152 mutex_lock(&s->qlock);
Andy Walls6c9de522008-09-03 17:11:54 -0300153 while (!list_empty(&q->list)) {
154 buf = list_entry(q->list.next, struct cx18_buffer, list);
155 list_move_tail(q->list.next, &s->q_free.list);
Andy Wallsbca11a52008-11-19 01:24:33 -0300156 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
Andy Wallsb04bce42008-08-22 21:03:11 -0300157 atomic_inc(&s->q_free.buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300158 }
Andy Walls6c9de522008-09-03 17:11:54 -0300159 cx18_queue_init(q);
Andy Wallsf576cee2008-11-16 20:18:05 -0300160 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300161}
162
163void cx18_flush_queues(struct cx18_stream *s)
164{
Andy Walls66c2a6b2008-12-08 23:02:45 -0300165 cx18_queue_flush(s, &s->q_busy);
Andy Walls6c9de522008-09-03 17:11:54 -0300166 cx18_queue_flush(s, &s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300167}
168
169int cx18_stream_alloc(struct cx18_stream *s)
170{
171 struct cx18 *cx = s->cx;
172 int i;
173
174 if (s->buffers == 0)
175 return 0;
176
177 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
178 s->name, s->buffers, s->buf_size,
179 s->buffers * s->buf_size / 1024);
180
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -0300181 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
182 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
183 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
184 ((char __iomem *)cx->scb->cpu_mdl));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300185
186 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
187 CX18_ERR("Max buffers = %zd\n",
188 bufsz / sizeof(struct cx18_mdl));
189 return -ENOMEM;
190 }
191
192 s->mdl_offset = cx->mdl_offset;
193
194 /* allocate stream buffers. Initially all buffers are in q_free. */
195 for (i = 0; i < s->buffers; i++) {
Hans Verkuil3f983872008-05-01 10:31:12 -0300196 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
197 GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300198
199 if (buf == NULL)
200 break;
Hans Verkuil3f983872008-05-01 10:31:12 -0300201 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300202 if (buf->buf == NULL) {
203 kfree(buf);
204 break;
205 }
206 buf->id = cx->buffer_id++;
207 INIT_LIST_HEAD(&buf->list);
208 buf->dma_handle = pci_map_single(s->cx->dev,
209 buf->buf, s->buf_size, s->dma);
210 cx18_buf_sync_for_cpu(s, buf);
211 cx18_enqueue(s, buf, &s->q_free);
212 }
213 if (i == s->buffers) {
214 cx->mdl_offset += s->buffers;
215 return 0;
216 }
217 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
218 cx18_stream_free(s);
219 return -ENOMEM;
220}
221
222void cx18_stream_free(struct cx18_stream *s)
223{
224 struct cx18_buffer *buf;
225
226 /* move all buffers to q_free */
227 cx18_flush_queues(s);
228
229 /* empty q_free */
230 while ((buf = cx18_dequeue(s, &s->q_free))) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300231 pci_unmap_single(s->cx->dev, buf->dma_handle,
232 s->buf_size, s->dma);
233 kfree(buf->buf);
234 kfree(buf);
235 }
236}