blob: 2c58b0c478c0e2861065112a819aef777173e2d8 [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
Andy Walls0ef02892008-12-14 18:52:12 -030058 /* q_busy is restricted to a max buffer count imposed by firmware */
59 if (q == &s->q_busy &&
60 atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
Andy Walls66c2a6b2008-12-08 23:02:45 -030061 q = &s->q_free;
62
Andy Wallsb80e1072008-11-28 00:04:21 -030063 if (to_front)
64 list_add(&buf->list, &q->list); /* LIFO */
65 else
66 list_add_tail(&buf->list, &q->list); /* FIFO */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030067 q->bytesused += buf->bytesused - buf->readpos;
Andy Walls66c2a6b2008-12-08 23:02:45 -030068 atomic_inc(&q->buffers);
69
Andy Wallsf576cee2008-11-16 20:18:05 -030070 mutex_unlock(&s->qlock);
Andy Walls66c2a6b2008-12-08 23:02:45 -030071 return q;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030072}
73
74struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
75{
76 struct cx18_buffer *buf = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030077
Andy Wallsf576cee2008-11-16 20:18:05 -030078 mutex_lock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030079 if (!list_empty(&q->list)) {
80 buf = list_entry(q->list.next, struct cx18_buffer, list);
81 list_del_init(q->list.next);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030082 q->bytesused -= buf->bytesused - buf->readpos;
Andy Wallsbca11a52008-11-19 01:24:33 -030083 buf->skipped = 0;
Andy Walls66c2a6b2008-12-08 23:02:45 -030084 atomic_dec(&q->buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030085 }
Andy Wallsf576cee2008-11-16 20:18:05 -030086 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030087 return buf;
88}
89
Andy Wallsee2d64f2008-11-16 01:38:19 -030090struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030091 u32 bytesused)
92{
93 struct cx18 *cx = s->cx;
Andy Wallsbca11a52008-11-19 01:24:33 -030094 struct cx18_buffer *buf;
95 struct cx18_buffer *ret = NULL;
96 struct list_head *p, *t;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097
Andy Wallsf576cee2008-11-16 20:18:05 -030098 mutex_lock(&s->qlock);
Andy Walls66c2a6b2008-12-08 23:02:45 -030099 list_for_each_safe(p, t, &s->q_busy.list) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300100 buf = list_entry(p, struct cx18_buffer, list);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300101
Andy Wallsee2d64f2008-11-16 01:38:19 -0300102 if (buf->id != id) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300103 buf->skipped++;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300104 if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300105 /* buffer must have fallen out of rotation */
Andy Wallsbca11a52008-11-19 01:24:33 -0300106 CX18_WARN("Skipped %s, buffer %d, %d "
107 "times - it must have dropped out of "
108 "rotation\n", s->name, buf->id,
109 buf->skipped);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300110 /* move it to q_free */
111 list_move_tail(&buf->list, &s->q_free.list);
112 buf->bytesused = buf->readpos = buf->b_flags =
113 buf->skipped = 0;
114 atomic_dec(&s->q_busy.buffers);
115 atomic_inc(&s->q_free.buffers);
Andy Wallsbca11a52008-11-19 01:24:33 -0300116 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300117 continue;
Andy Wallsee2d64f2008-11-16 01:38:19 -0300118 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300119
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300120 buf->bytesused = bytesused;
Andy Wallsabb096d2008-12-12 15:50:27 -0300121 /* Sync the buffer before we release the qlock */
122 cx18_buf_sync_for_cpu(s, buf);
Andy Wallsbca11a52008-11-19 01:24:33 -0300123 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
124 /*
Andy Wallsabb096d2008-12-12 15:50:27 -0300125 * TS doesn't use q_full. As we pull the buffer off of
126 * the queue here, the caller will have to put it back.
Andy Wallsbca11a52008-11-19 01:24:33 -0300127 */
128 list_del_init(&buf->list);
129 } else {
Andy Wallsabb096d2008-12-12 15:50:27 -0300130 /* Move buffer from q_busy to q_full */
Andy Wallsee2d64f2008-11-16 01:38:19 -0300131 list_move_tail(&buf->list, &s->q_full.list);
Andy Wallsabb096d2008-12-12 15:50:27 -0300132 set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300133 s->q_full.bytesused += buf->bytesused;
134 atomic_inc(&s->q_full.buffers);
Andy Wallsee2d64f2008-11-16 01:38:19 -0300135 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300136 atomic_dec(&s->q_busy.buffers);
Andy Walls1d6782b2008-11-05 00:49:14 -0300137
Andy Wallsbca11a52008-11-19 01:24:33 -0300138 ret = buf;
139 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300140 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300141 mutex_unlock(&s->qlock);
Andy Wallsbca11a52008-11-19 01:24:33 -0300142 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300143}
144
Andy Walls6c9de522008-09-03 17:11:54 -0300145/* Move all buffers of a queue to q_free, while flushing the buffers */
146static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300147{
Andy Walls6c9de522008-09-03 17:11:54 -0300148 struct cx18_buffer *buf;
149
150 if (q == &s->q_free)
151 return;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300152
Andy Wallsf576cee2008-11-16 20:18:05 -0300153 mutex_lock(&s->qlock);
Andy Walls6c9de522008-09-03 17:11:54 -0300154 while (!list_empty(&q->list)) {
155 buf = list_entry(q->list.next, struct cx18_buffer, list);
156 list_move_tail(q->list.next, &s->q_free.list);
Andy Wallsbca11a52008-11-19 01:24:33 -0300157 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
Andy Wallsb04bce42008-08-22 21:03:11 -0300158 atomic_inc(&s->q_free.buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300159 }
Andy Walls6c9de522008-09-03 17:11:54 -0300160 cx18_queue_init(q);
Andy Wallsf576cee2008-11-16 20:18:05 -0300161 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162}
163
164void cx18_flush_queues(struct cx18_stream *s)
165{
Andy Walls66c2a6b2008-12-08 23:02:45 -0300166 cx18_queue_flush(s, &s->q_busy);
Andy Walls6c9de522008-09-03 17:11:54 -0300167 cx18_queue_flush(s, &s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300168}
169
170int cx18_stream_alloc(struct cx18_stream *s)
171{
172 struct cx18 *cx = s->cx;
173 int i;
174
175 if (s->buffers == 0)
176 return 0;
177
178 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
179 s->name, s->buffers, s->buf_size,
180 s->buffers * s->buf_size / 1024);
181
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -0300182 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
183 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
184 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
185 ((char __iomem *)cx->scb->cpu_mdl));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300186
187 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
188 CX18_ERR("Max buffers = %zd\n",
189 bufsz / sizeof(struct cx18_mdl));
190 return -ENOMEM;
191 }
192
193 s->mdl_offset = cx->mdl_offset;
194
195 /* allocate stream buffers. Initially all buffers are in q_free. */
196 for (i = 0; i < s->buffers; i++) {
Hans Verkuil3f983872008-05-01 10:31:12 -0300197 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
198 GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300199
200 if (buf == NULL)
201 break;
Hans Verkuil3f983872008-05-01 10:31:12 -0300202 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300203 if (buf->buf == NULL) {
204 kfree(buf);
205 break;
206 }
207 buf->id = cx->buffer_id++;
208 INIT_LIST_HEAD(&buf->list);
209 buf->dma_handle = pci_map_single(s->cx->dev,
210 buf->buf, s->buf_size, s->dma);
211 cx18_buf_sync_for_cpu(s, buf);
212 cx18_enqueue(s, buf, &s->q_free);
213 }
214 if (i == s->buffers) {
215 cx->mdl_offset += s->buffers;
216 return 0;
217 }
218 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
219 cx18_stream_free(s);
220 return -ENOMEM;
221}
222
223void cx18_stream_free(struct cx18_stream *s)
224{
225 struct cx18_buffer *buf;
226
227 /* move all buffers to q_free */
228 cx18_flush_queues(s);
229
230 /* empty q_free */
231 while ((buf = cx18_dequeue(s, &s->q_free))) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300232 pci_unmap_single(s->cx->dev, buf->dma_handle,
233 s->buf_size, s->dma);
234 kfree(buf->buf);
235 kfree(buf);
236 }
237}