blob: 3046b8e74345081d30608af439ff09e17b3a331f [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)) {
Andy Wallsf6b181a2008-12-14 21:05:36 -030080 buf = list_first_entry(&q->list, struct cx18_buffer, list);
81 list_del_init(&buf->list);
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;
Andy Wallsf6b181a2008-12-14 21:05:36 -030095 struct cx18_buffer *tmp;
Andy Wallsbca11a52008-11-19 01:24:33 -030096 struct cx18_buffer *ret = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097
Andy Wallsf576cee2008-11-16 20:18:05 -030098 mutex_lock(&s->qlock);
Andy Wallsf6b181a2008-12-14 21:05:36 -030099 list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) {
Andy Wallsee2d64f2008-11-16 01:38:19 -0300100 if (buf->id != id) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300101 buf->skipped++;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300102 if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
Andy Wallsbca11a52008-11-19 01:24:33 -0300103 /* buffer must have fallen out of rotation */
Andy Wallsbca11a52008-11-19 01:24:33 -0300104 CX18_WARN("Skipped %s, buffer %d, %d "
105 "times - it must have dropped out of "
106 "rotation\n", s->name, buf->id,
107 buf->skipped);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300108 /* move it to q_free */
109 list_move_tail(&buf->list, &s->q_free.list);
110 buf->bytesused = buf->readpos = buf->b_flags =
111 buf->skipped = 0;
112 atomic_dec(&s->q_busy.buffers);
113 atomic_inc(&s->q_free.buffers);
Andy Wallsbca11a52008-11-19 01:24:33 -0300114 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300115 continue;
Andy Wallsee2d64f2008-11-16 01:38:19 -0300116 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300117
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300118 buf->bytesused = bytesused;
Andy Wallsabb096d2008-12-12 15:50:27 -0300119 /* Sync the buffer before we release the qlock */
120 cx18_buf_sync_for_cpu(s, buf);
Andy Wallsbca11a52008-11-19 01:24:33 -0300121 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
122 /*
Andy Wallsabb096d2008-12-12 15:50:27 -0300123 * TS doesn't use q_full. As we pull the buffer off of
124 * the queue here, the caller will have to put it back.
Andy Wallsbca11a52008-11-19 01:24:33 -0300125 */
126 list_del_init(&buf->list);
127 } else {
Andy Wallsabb096d2008-12-12 15:50:27 -0300128 /* Move buffer from q_busy to q_full */
Andy Wallsee2d64f2008-11-16 01:38:19 -0300129 list_move_tail(&buf->list, &s->q_full.list);
Andy Wallsabb096d2008-12-12 15:50:27 -0300130 set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300131 s->q_full.bytesused += buf->bytesused;
132 atomic_inc(&s->q_full.buffers);
Andy Wallsee2d64f2008-11-16 01:38:19 -0300133 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300134 atomic_dec(&s->q_busy.buffers);
Andy Walls1d6782b2008-11-05 00:49:14 -0300135
Andy Wallsbca11a52008-11-19 01:24:33 -0300136 ret = buf;
137 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300138 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300139 mutex_unlock(&s->qlock);
Andy Wallsbca11a52008-11-19 01:24:33 -0300140 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300141}
142
Andy Walls6c9de522008-09-03 17:11:54 -0300143/* Move all buffers of a queue to q_free, while flushing the buffers */
144static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300145{
Andy Walls6c9de522008-09-03 17:11:54 -0300146 struct cx18_buffer *buf;
147
148 if (q == &s->q_free)
149 return;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300150
Andy Wallsf576cee2008-11-16 20:18:05 -0300151 mutex_lock(&s->qlock);
Andy Walls6c9de522008-09-03 17:11:54 -0300152 while (!list_empty(&q->list)) {
Andy Wallsf6b181a2008-12-14 21:05:36 -0300153 buf = list_first_entry(&q->list, struct cx18_buffer, list);
154 list_move_tail(&buf->list, &s->q_free.list);
Andy Wallsbca11a52008-11-19 01:24:33 -0300155 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
Andy Wallsb04bce42008-08-22 21:03:11 -0300156 atomic_inc(&s->q_free.buffers);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300157 }
Andy Walls6c9de522008-09-03 17:11:54 -0300158 cx18_queue_init(q);
Andy Wallsf576cee2008-11-16 20:18:05 -0300159 mutex_unlock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300160}
161
162void cx18_flush_queues(struct cx18_stream *s)
163{
Andy Walls66c2a6b2008-12-08 23:02:45 -0300164 cx18_queue_flush(s, &s->q_busy);
Andy Walls6c9de522008-09-03 17:11:54 -0300165 cx18_queue_flush(s, &s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300166}
167
168int cx18_stream_alloc(struct cx18_stream *s)
169{
170 struct cx18 *cx = s->cx;
171 int i;
172
173 if (s->buffers == 0)
174 return 0;
175
176 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
177 s->name, s->buffers, s->buf_size,
178 s->buffers * s->buf_size / 1024);
179
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -0300180 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
181 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
182 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
183 ((char __iomem *)cx->scb->cpu_mdl));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300184
185 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
186 CX18_ERR("Max buffers = %zd\n",
187 bufsz / sizeof(struct cx18_mdl));
188 return -ENOMEM;
189 }
190
191 s->mdl_offset = cx->mdl_offset;
192
193 /* allocate stream buffers. Initially all buffers are in q_free. */
194 for (i = 0; i < s->buffers; i++) {
Hans Verkuil3f983872008-05-01 10:31:12 -0300195 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
196 GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300197
198 if (buf == NULL)
199 break;
Hans Verkuil3f983872008-05-01 10:31:12 -0300200 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300201 if (buf->buf == NULL) {
202 kfree(buf);
203 break;
204 }
205 buf->id = cx->buffer_id++;
206 INIT_LIST_HEAD(&buf->list);
Andy Walls3d059132009-01-10 21:54:39 -0300207 buf->dma_handle = pci_map_single(s->cx->pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300208 buf->buf, s->buf_size, s->dma);
209 cx18_buf_sync_for_cpu(s, buf);
210 cx18_enqueue(s, buf, &s->q_free);
211 }
212 if (i == s->buffers) {
213 cx->mdl_offset += s->buffers;
214 return 0;
215 }
216 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
217 cx18_stream_free(s);
218 return -ENOMEM;
219}
220
221void cx18_stream_free(struct cx18_stream *s)
222{
223 struct cx18_buffer *buf;
224
225 /* move all buffers to q_free */
226 cx18_flush_queues(s);
227
228 /* empty q_free */
229 while ((buf = cx18_dequeue(s, &s->q_free))) {
Andy Walls3d059132009-01-10 21:54:39 -0300230 pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300231 s->buf_size, s->dma);
232 kfree(buf->buf);
233 kfree(buf);
234 }
235}