blob: 7b09c9a3ee8fb5a7882975f502fc29eb222ef28f [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
45void cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
46 struct cx18_queue *q)
47{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030048 /* clear the buffer if it is going to be enqueued to the free queue */
49 if (q == &s->q_free) {
50 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 Wallsf576cee2008-11-16 20:18:05 -030055 mutex_lock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030056 list_add_tail(&buf->list, &q->list);
Andy Wallsb04bce42008-08-22 21:03:11 -030057 atomic_inc(&q->buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030058 q->bytesused += buf->bytesused - buf->readpos;
Andy Wallsf576cee2008-11-16 20:18:05 -030059 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030060}
61
62struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
63{
64 struct cx18_buffer *buf = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030065
Andy Wallsf576cee2008-11-16 20:18:05 -030066 mutex_lock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030067 if (!list_empty(&q->list)) {
68 buf = list_entry(q->list.next, struct cx18_buffer, list);
69 list_del_init(q->list.next);
Andy Wallsb04bce42008-08-22 21:03:11 -030070 atomic_dec(&q->buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030071 q->bytesused -= buf->bytesused - buf->readpos;
Andy Wallsbca11a52008-11-19 01:24:33 -030072 buf->skipped = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030073 }
Andy Wallsf576cee2008-11-16 20:18:05 -030074 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030075 return buf;
76}
77
Andy Wallsee2d64f2008-11-16 01:38:19 -030078struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030079 u32 bytesused)
80{
81 struct cx18 *cx = s->cx;
Andy Wallsbca11a52008-11-19 01:24:33 -030082 struct cx18_buffer *buf;
83 struct cx18_buffer *ret = NULL;
84 struct list_head *p, *t;
85 LIST_HEAD(r);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030086
Andy Wallsf576cee2008-11-16 20:18:05 -030087 mutex_lock(&s->qlock);
Andy Wallsbca11a52008-11-19 01:24:33 -030088 list_for_each_safe(p, t, &s->q_free.list) {
89 buf = list_entry(p, struct cx18_buffer, list);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030090
Andy Wallsee2d64f2008-11-16 01:38:19 -030091 if (buf->id != id) {
Andy Wallsbca11a52008-11-19 01:24:33 -030092 buf->skipped++;
93 if (buf->skipped >= atomic_read(&s->q_free.buffers)-1) {
94 /* buffer must have fallen out of rotation */
95 atomic_dec(&s->q_free.buffers);
96 list_move_tail(&buf->list, &r);
97 CX18_WARN("Skipped %s, buffer %d, %d "
98 "times - it must have dropped out of "
99 "rotation\n", s->name, buf->id,
100 buf->skipped);
101 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300102 continue;
Andy Wallsee2d64f2008-11-16 01:38:19 -0300103 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300104
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300105 buf->bytesused = bytesused;
Andy Wallsbca11a52008-11-19 01:24:33 -0300106 atomic_dec(&s->q_free.buffers);
107 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
108 /*
109 * TS doesn't use q_full, but for sweeping up lost
110 * buffers, we want the TS to requeue the buffer just
111 * before sending the MDL back to the firmware, so we
112 * pull it off the list here.
113 */
114 list_del_init(&buf->list);
115 } else {
Andy Wallsee2d64f2008-11-16 01:38:19 -0300116 atomic_inc(&s->q_full.buffers);
117 s->q_full.bytesused += buf->bytesused;
118 list_move_tail(&buf->list, &s->q_full.list);
119 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300120
Andy Wallsbca11a52008-11-19 01:24:33 -0300121 ret = buf;
122 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300123 }
Andy Wallsf576cee2008-11-16 20:18:05 -0300124 mutex_unlock(&s->qlock);
Andy Wallsbca11a52008-11-19 01:24:33 -0300125
126 /* Put lost buffers back into firmware transfer rotation */
127 while (!list_empty(&r)) {
128 buf = list_entry(r.next, struct cx18_buffer, list);
129 list_del_init(r.next);
130 cx18_enqueue(s, buf, &s->q_free);
131 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
132 (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
133 1, buf->id, s->buf_size);
134 CX18_INFO("Returning %s, buffer %d back to transfer rotation\n",
135 s->name, buf->id);
136 /* and there was much rejoicing... */
137 }
138 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300139}
140
Andy Walls6c9de522008-09-03 17:11:54 -0300141/* Move all buffers of a queue to q_free, while flushing the buffers */
142static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300143{
Andy Walls6c9de522008-09-03 17:11:54 -0300144 struct cx18_buffer *buf;
145
146 if (q == &s->q_free)
147 return;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300148
Andy Wallsf576cee2008-11-16 20:18:05 -0300149 mutex_lock(&s->qlock);
Andy Walls6c9de522008-09-03 17:11:54 -0300150 while (!list_empty(&q->list)) {
151 buf = list_entry(q->list.next, struct cx18_buffer, list);
152 list_move_tail(q->list.next, &s->q_free.list);
Andy Wallsbca11a52008-11-19 01:24:33 -0300153 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
Andy Wallsb04bce42008-08-22 21:03:11 -0300154 atomic_inc(&s->q_free.buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300155 }
Andy Walls6c9de522008-09-03 17:11:54 -0300156 cx18_queue_init(q);
Andy Wallsf576cee2008-11-16 20:18:05 -0300157 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300158}
159
160void cx18_flush_queues(struct cx18_stream *s)
161{
Andy Walls6c9de522008-09-03 17:11:54 -0300162 cx18_queue_flush(s, &s->q_io);
163 cx18_queue_flush(s, &s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300164}
165
166int cx18_stream_alloc(struct cx18_stream *s)
167{
168 struct cx18 *cx = s->cx;
169 int i;
170
171 if (s->buffers == 0)
172 return 0;
173
174 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
175 s->name, s->buffers, s->buf_size,
176 s->buffers * s->buf_size / 1024);
177
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -0300178 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
179 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
180 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
181 ((char __iomem *)cx->scb->cpu_mdl));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300182
183 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
184 CX18_ERR("Max buffers = %zd\n",
185 bufsz / sizeof(struct cx18_mdl));
186 return -ENOMEM;
187 }
188
189 s->mdl_offset = cx->mdl_offset;
190
191 /* allocate stream buffers. Initially all buffers are in q_free. */
192 for (i = 0; i < s->buffers; i++) {
Hans Verkuil3f983872008-05-01 10:31:12 -0300193 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
194 GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300195
196 if (buf == NULL)
197 break;
Hans Verkuil3f983872008-05-01 10:31:12 -0300198 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300199 if (buf->buf == NULL) {
200 kfree(buf);
201 break;
202 }
203 buf->id = cx->buffer_id++;
204 INIT_LIST_HEAD(&buf->list);
205 buf->dma_handle = pci_map_single(s->cx->dev,
206 buf->buf, s->buf_size, s->dma);
207 cx18_buf_sync_for_cpu(s, buf);
208 cx18_enqueue(s, buf, &s->q_free);
209 }
210 if (i == s->buffers) {
211 cx->mdl_offset += s->buffers;
212 return 0;
213 }
214 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
215 cx18_stream_free(s);
216 return -ENOMEM;
217}
218
219void cx18_stream_free(struct cx18_stream *s)
220{
221 struct cx18_buffer *buf;
222
223 /* move all buffers to q_free */
224 cx18_flush_queues(s);
225
226 /* empty q_free */
227 while ((buf = cx18_dequeue(s, &s->q_free))) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228 pci_unmap_single(s->cx->dev, buf->dma_handle,
229 s->buf_size, s->dma);
230 kfree(buf->buf);
231 kfree(buf);
232 }
233}