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" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 26 | #include "cx18-queue.h" |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 27 | #include "cx18-streams.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 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 | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 45 | struct cx18_queue *_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 | { |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 48 | /* clear the buffer if it is not to be enqueued to the full queue */ |
| 49 | if (q != &s->q_full) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 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 | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 55 | |
Andy Walls | 0ef0289 | 2008-12-14 18:52:12 -0300 | [diff] [blame] | 56 | /* q_busy is restricted to a max buffer count imposed by firmware */ |
| 57 | if (q == &s->q_busy && |
| 58 | atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM) |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 59 | q = &s->q_free; |
| 60 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 61 | spin_lock(&q->lock); |
| 62 | |
Andy Walls | b80e107 | 2008-11-28 00:04:21 -0300 | [diff] [blame] | 63 | if (to_front) |
| 64 | list_add(&buf->list, &q->list); /* LIFO */ |
| 65 | else |
| 66 | list_add_tail(&buf->list, &q->list); /* FIFO */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 67 | q->bytesused += buf->bytesused - buf->readpos; |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 68 | atomic_inc(&q->buffers); |
| 69 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 70 | spin_unlock(&q->lock); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 71 | return q; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) |
| 75 | { |
| 76 | struct cx18_buffer *buf = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 77 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 78 | spin_lock(&q->lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 79 | if (!list_empty(&q->list)) { |
Andy Walls | f6b181a | 2008-12-14 21:05:36 -0300 | [diff] [blame] | 80 | buf = list_first_entry(&q->list, struct cx18_buffer, list); |
| 81 | list_del_init(&buf->list); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 82 | q->bytesused -= buf->bytesused - buf->readpos; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 83 | buf->skipped = 0; |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 84 | atomic_dec(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 85 | } |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 86 | spin_unlock(&q->lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 87 | return buf; |
| 88 | } |
| 89 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 90 | 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] | 91 | u32 bytesused) |
| 92 | { |
| 93 | struct cx18 *cx = s->cx; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 94 | struct cx18_buffer *buf; |
Andy Walls | f6b181a | 2008-12-14 21:05:36 -0300 | [diff] [blame] | 95 | struct cx18_buffer *tmp; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 96 | struct cx18_buffer *ret = NULL; |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 97 | LIST_HEAD(sweep_up); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 98 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 99 | /* |
| 100 | * We don't have to acquire multiple q locks here, because we are |
| 101 | * serialized by the single threaded work handler. |
| 102 | * Buffers from the firmware will thus remain in order as |
| 103 | * they are moved from q_busy to q_full or to the dvb ring buffer. |
| 104 | */ |
| 105 | spin_lock(&s->q_busy.lock); |
Andy Walls | f6b181a | 2008-12-14 21:05:36 -0300 | [diff] [blame] | 106 | list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) { |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 107 | /* |
| 108 | * We should find what the firmware told us is done, |
| 109 | * right at the front of the queue. If we don't, we likely have |
| 110 | * missed a buffer done message from the firmware. |
| 111 | * Once we skip a buffer repeatedly, relative to the size of |
| 112 | * q_busy, we have high confidence we've missed it. |
| 113 | */ |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 114 | if (buf->id != id) { |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 115 | buf->skipped++; |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 116 | if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) { |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 117 | /* buffer must have fallen out of rotation */ |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 118 | CX18_WARN("Skipped %s, buffer %d, %d " |
| 119 | "times - it must have dropped out of " |
| 120 | "rotation\n", s->name, buf->id, |
| 121 | buf->skipped); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 122 | /* Sweep it up to put it back into rotation */ |
| 123 | list_move_tail(&buf->list, &sweep_up); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 124 | atomic_dec(&s->q_busy.buffers); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 125 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 126 | continue; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 127 | } |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 128 | /* |
| 129 | * We pull the desired buffer off of the queue here. Something |
| 130 | * will have to put it back on a queue later. |
| 131 | */ |
| 132 | list_del_init(&buf->list); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 133 | atomic_dec(&s->q_busy.buffers); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 134 | ret = buf; |
| 135 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 136 | } |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 137 | spin_unlock(&s->q_busy.lock); |
| 138 | |
| 139 | /* |
| 140 | * We found the buffer for which we were looking. Get it ready for |
| 141 | * the caller to put on q_full or in the dvb ring buffer. |
| 142 | */ |
| 143 | if (ret != NULL) { |
| 144 | ret->bytesused = bytesused; |
| 145 | ret->skipped = 0; |
| 146 | /* readpos and b_flags were 0'ed when the buf went on q_busy */ |
| 147 | cx18_buf_sync_for_cpu(s, ret); |
| 148 | if (s->type != CX18_ENC_STREAM_TYPE_TS) |
| 149 | set_bit(CX18_F_B_NEED_BUF_SWAP, &ret->b_flags); |
| 150 | } |
| 151 | |
| 152 | /* Put any buffers the firmware is ignoring back into normal rotation */ |
| 153 | list_for_each_entry_safe(buf, tmp, &sweep_up, list) { |
| 154 | list_del_init(&buf->list); |
| 155 | cx18_enqueue(s, buf, &s->q_free); |
| 156 | } |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 157 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 158 | } |
| 159 | |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 160 | /* Move all buffers of a queue to q_free, while flushing the buffers */ |
| 161 | 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] | 162 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 163 | struct cx18_buffer *buf; |
| 164 | |
| 165 | if (q == &s->q_free) |
| 166 | return; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 167 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 168 | spin_lock(&q->lock); |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 169 | while (!list_empty(&q->list)) { |
Andy Walls | f6b181a | 2008-12-14 21:05:36 -0300 | [diff] [blame] | 170 | buf = list_first_entry(&q->list, struct cx18_buffer, list); |
| 171 | list_move_tail(&buf->list, &s->q_free.list); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 172 | buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0; |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 173 | atomic_inc(&s->q_free.buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 174 | } |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 175 | cx18_queue_init(q); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 176 | spin_unlock(&q->lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void cx18_flush_queues(struct cx18_stream *s) |
| 180 | { |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 181 | cx18_queue_flush(s, &s->q_busy); |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 182 | cx18_queue_flush(s, &s->q_full); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int cx18_stream_alloc(struct cx18_stream *s) |
| 186 | { |
| 187 | struct cx18 *cx = s->cx; |
| 188 | int i; |
| 189 | |
| 190 | if (s->buffers == 0) |
| 191 | return 0; |
| 192 | |
| 193 | CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n", |
| 194 | s->name, s->buffers, s->buf_size, |
| 195 | s->buffers * s->buf_size / 1024); |
| 196 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 197 | if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] - |
| 198 | (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) { |
| 199 | unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE - |
| 200 | ((char __iomem *)cx->scb->cpu_mdl)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 201 | |
| 202 | CX18_ERR("Too many buffers, cannot fit in SCB area\n"); |
| 203 | CX18_ERR("Max buffers = %zd\n", |
| 204 | bufsz / sizeof(struct cx18_mdl)); |
| 205 | return -ENOMEM; |
| 206 | } |
| 207 | |
| 208 | s->mdl_offset = cx->mdl_offset; |
| 209 | |
| 210 | /* allocate stream buffers. Initially all buffers are in q_free. */ |
| 211 | for (i = 0; i < s->buffers; i++) { |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 212 | struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer), |
| 213 | GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 214 | |
| 215 | if (buf == NULL) |
| 216 | break; |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 217 | buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 218 | if (buf->buf == NULL) { |
| 219 | kfree(buf); |
| 220 | break; |
| 221 | } |
| 222 | buf->id = cx->buffer_id++; |
| 223 | INIT_LIST_HEAD(&buf->list); |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 224 | buf->dma_handle = pci_map_single(s->cx->pci_dev, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 225 | buf->buf, s->buf_size, s->dma); |
| 226 | cx18_buf_sync_for_cpu(s, buf); |
| 227 | cx18_enqueue(s, buf, &s->q_free); |
| 228 | } |
| 229 | if (i == s->buffers) { |
| 230 | cx->mdl_offset += s->buffers; |
| 231 | return 0; |
| 232 | } |
| 233 | CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name); |
| 234 | cx18_stream_free(s); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | |
| 238 | void cx18_stream_free(struct cx18_stream *s) |
| 239 | { |
| 240 | struct cx18_buffer *buf; |
| 241 | |
| 242 | /* move all buffers to q_free */ |
| 243 | cx18_flush_queues(s); |
| 244 | |
| 245 | /* empty q_free */ |
| 246 | while ((buf = cx18_dequeue(s, &s->q_free))) { |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 247 | pci_unmap_single(s->cx->pci_dev, buf->dma_handle, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 248 | s->buf_size, s->dma); |
| 249 | kfree(buf->buf); |
| 250 | kfree(buf); |
| 251 | } |
| 252 | } |