Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cx18 buffer queues |
| 3 | * |
| 4 | * Derived from ivtv-queue.c |
| 5 | * |
| 6 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
Andy Walls | 1ed9dcc | 2008-11-22 01:37:34 -0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Andy Walls <awalls@radix.net> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 8 | * |
| 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 Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 30 | void 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 | |
| 38 | void cx18_queue_init(struct cx18_queue *q) |
| 39 | { |
| 40 | INIT_LIST_HEAD(&q->list); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 41 | atomic_set(&q->buffers, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 42 | q->bytesused = 0; |
| 43 | } |
| 44 | |
Andy Walls | b80e107 | 2008-11-28 00:04:21 -0300 | [diff] [blame^] | 45 | void _cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf, |
| 46 | struct cx18_queue *q, int to_front) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 47 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 48 | /* 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 Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 53 | buf->skipped = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 54 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 55 | mutex_lock(&s->qlock); |
Andy Walls | b80e107 | 2008-11-28 00:04:21 -0300 | [diff] [blame^] | 56 | if (to_front) |
| 57 | list_add(&buf->list, &q->list); /* LIFO */ |
| 58 | else |
| 59 | list_add_tail(&buf->list, &q->list); /* FIFO */ |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 60 | atomic_inc(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 61 | q->bytesused += buf->bytesused - buf->readpos; |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 62 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) |
| 66 | { |
| 67 | struct cx18_buffer *buf = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 68 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 69 | mutex_lock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 70 | if (!list_empty(&q->list)) { |
| 71 | buf = list_entry(q->list.next, struct cx18_buffer, list); |
| 72 | list_del_init(q->list.next); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 73 | atomic_dec(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 74 | q->bytesused -= buf->bytesused - buf->readpos; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 75 | buf->skipped = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 76 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 77 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 78 | return buf; |
| 79 | } |
| 80 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 81 | struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 82 | u32 bytesused) |
| 83 | { |
| 84 | struct cx18 *cx = s->cx; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 85 | struct cx18_buffer *buf; |
| 86 | struct cx18_buffer *ret = NULL; |
| 87 | struct list_head *p, *t; |
| 88 | LIST_HEAD(r); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 89 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 90 | mutex_lock(&s->qlock); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 91 | list_for_each_safe(p, t, &s->q_free.list) { |
| 92 | buf = list_entry(p, struct cx18_buffer, list); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 93 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 94 | if (buf->id != id) { |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 95 | buf->skipped++; |
| 96 | if (buf->skipped >= atomic_read(&s->q_free.buffers)-1) { |
| 97 | /* buffer must have fallen out of rotation */ |
| 98 | atomic_dec(&s->q_free.buffers); |
| 99 | list_move_tail(&buf->list, &r); |
| 100 | CX18_WARN("Skipped %s, buffer %d, %d " |
| 101 | "times - it must have dropped out of " |
| 102 | "rotation\n", s->name, buf->id, |
| 103 | buf->skipped); |
| 104 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 105 | continue; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 106 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 107 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 108 | buf->bytesused = bytesused; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 109 | atomic_dec(&s->q_free.buffers); |
| 110 | if (s->type == CX18_ENC_STREAM_TYPE_TS) { |
| 111 | /* |
| 112 | * TS doesn't use q_full, but for sweeping up lost |
| 113 | * buffers, we want the TS to requeue the buffer just |
| 114 | * before sending the MDL back to the firmware, so we |
| 115 | * pull it off the list here. |
| 116 | */ |
| 117 | list_del_init(&buf->list); |
| 118 | } else { |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 119 | atomic_inc(&s->q_full.buffers); |
| 120 | s->q_full.bytesused += buf->bytesused; |
| 121 | list_move_tail(&buf->list, &s->q_full.list); |
| 122 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 123 | |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 124 | ret = buf; |
| 125 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 126 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 127 | mutex_unlock(&s->qlock); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 128 | |
| 129 | /* Put lost buffers back into firmware transfer rotation */ |
| 130 | while (!list_empty(&r)) { |
| 131 | buf = list_entry(r.next, struct cx18_buffer, list); |
| 132 | list_del_init(r.next); |
| 133 | cx18_enqueue(s, buf, &s->q_free); |
| 134 | cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle, |
| 135 | (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem, |
| 136 | 1, buf->id, s->buf_size); |
| 137 | CX18_INFO("Returning %s, buffer %d back to transfer rotation\n", |
| 138 | s->name, buf->id); |
| 139 | /* and there was much rejoicing... */ |
| 140 | } |
| 141 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 142 | } |
| 143 | |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 144 | /* Move all buffers of a queue to q_free, while flushing the buffers */ |
| 145 | static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 146 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 147 | struct cx18_buffer *buf; |
| 148 | |
| 149 | if (q == &s->q_free) |
| 150 | return; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 151 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 152 | mutex_lock(&s->qlock); |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 153 | 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 Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 156 | buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0; |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 157 | atomic_inc(&s->q_free.buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 158 | } |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 159 | cx18_queue_init(q); |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 160 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void cx18_flush_queues(struct cx18_stream *s) |
| 164 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 165 | cx18_queue_flush(s, &s->q_full); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | int 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 Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 180 | 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 Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 184 | |
| 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 Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 195 | struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer), |
| 196 | GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 197 | |
| 198 | if (buf == NULL) |
| 199 | break; |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 200 | buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 201 | if (buf->buf == NULL) { |
| 202 | kfree(buf); |
| 203 | break; |
| 204 | } |
| 205 | buf->id = cx->buffer_id++; |
| 206 | INIT_LIST_HEAD(&buf->list); |
| 207 | buf->dma_handle = pci_map_single(s->cx->dev, |
| 208 | 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 | |
| 221 | void 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))) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 230 | pci_unmap_single(s->cx->dev, buf->dma_handle, |
| 231 | s->buf_size, s->dma); |
| 232 | kfree(buf->buf); |
| 233 | kfree(buf); |
| 234 | } |
| 235 | } |