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> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 21 | * 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include "cx18-driver.h" |
| 25 | #include "cx18-streams.h" |
| 26 | #include "cx18-queue.h" |
| 27 | #include "cx18-scb.h" |
| 28 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 29 | void cx18_buf_swap(struct cx18_buffer *buf) |
| 30 | { |
| 31 | int i; |
| 32 | |
| 33 | for (i = 0; i < buf->bytesused; i += 4) |
| 34 | swab32s((u32 *)(buf->buf + i)); |
| 35 | } |
| 36 | |
| 37 | void cx18_queue_init(struct cx18_queue *q) |
| 38 | { |
| 39 | INIT_LIST_HEAD(&q->list); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 40 | atomic_set(&q->buffers, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 41 | q->bytesused = 0; |
| 42 | } |
| 43 | |
| 44 | void cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf, |
| 45 | struct cx18_queue *q) |
| 46 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 47 | /* clear the buffer if it is going to be enqueued to the free queue */ |
| 48 | if (q == &s->q_free) { |
| 49 | buf->bytesused = 0; |
| 50 | buf->readpos = 0; |
| 51 | buf->b_flags = 0; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 52 | buf->skipped = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 53 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 54 | mutex_lock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 55 | list_add_tail(&buf->list, &q->list); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 56 | atomic_inc(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 57 | q->bytesused += buf->bytesused - buf->readpos; |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 58 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) |
| 62 | { |
| 63 | struct cx18_buffer *buf = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 64 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 65 | mutex_lock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 66 | if (!list_empty(&q->list)) { |
| 67 | buf = list_entry(q->list.next, struct cx18_buffer, list); |
| 68 | list_del_init(q->list.next); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 69 | atomic_dec(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 70 | q->bytesused -= buf->bytesused - buf->readpos; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 71 | buf->skipped = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 72 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 73 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 74 | return buf; |
| 75 | } |
| 76 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 77 | 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] | 78 | u32 bytesused) |
| 79 | { |
| 80 | struct cx18 *cx = s->cx; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 81 | struct cx18_buffer *buf; |
| 82 | struct cx18_buffer *ret = NULL; |
| 83 | struct list_head *p, *t; |
| 84 | LIST_HEAD(r); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 85 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 86 | mutex_lock(&s->qlock); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 87 | list_for_each_safe(p, t, &s->q_free.list) { |
| 88 | buf = list_entry(p, struct cx18_buffer, list); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 89 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 90 | if (buf->id != id) { |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 91 | buf->skipped++; |
| 92 | if (buf->skipped >= atomic_read(&s->q_free.buffers)-1) { |
| 93 | /* buffer must have fallen out of rotation */ |
| 94 | atomic_dec(&s->q_free.buffers); |
| 95 | list_move_tail(&buf->list, &r); |
| 96 | CX18_WARN("Skipped %s, buffer %d, %d " |
| 97 | "times - it must have dropped out of " |
| 98 | "rotation\n", s->name, buf->id, |
| 99 | buf->skipped); |
| 100 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 101 | continue; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 102 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 103 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 104 | buf->bytesused = bytesused; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 105 | atomic_dec(&s->q_free.buffers); |
| 106 | if (s->type == CX18_ENC_STREAM_TYPE_TS) { |
| 107 | /* |
| 108 | * TS doesn't use q_full, but for sweeping up lost |
| 109 | * buffers, we want the TS to requeue the buffer just |
| 110 | * before sending the MDL back to the firmware, so we |
| 111 | * pull it off the list here. |
| 112 | */ |
| 113 | list_del_init(&buf->list); |
| 114 | } else { |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 115 | atomic_inc(&s->q_full.buffers); |
| 116 | s->q_full.bytesused += buf->bytesused; |
| 117 | list_move_tail(&buf->list, &s->q_full.list); |
| 118 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 119 | |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 120 | ret = buf; |
| 121 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 122 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 123 | mutex_unlock(&s->qlock); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 124 | |
| 125 | /* Put lost buffers back into firmware transfer rotation */ |
| 126 | while (!list_empty(&r)) { |
| 127 | buf = list_entry(r.next, struct cx18_buffer, list); |
| 128 | list_del_init(r.next); |
| 129 | cx18_enqueue(s, buf, &s->q_free); |
| 130 | cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle, |
| 131 | (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem, |
| 132 | 1, buf->id, s->buf_size); |
| 133 | CX18_INFO("Returning %s, buffer %d back to transfer rotation\n", |
| 134 | s->name, buf->id); |
| 135 | /* and there was much rejoicing... */ |
| 136 | } |
| 137 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 138 | } |
| 139 | |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 140 | /* Move all buffers of a queue to q_free, while flushing the buffers */ |
| 141 | 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] | 142 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 143 | struct cx18_buffer *buf; |
| 144 | |
| 145 | if (q == &s->q_free) |
| 146 | return; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 147 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 148 | mutex_lock(&s->qlock); |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 149 | while (!list_empty(&q->list)) { |
| 150 | buf = list_entry(q->list.next, struct cx18_buffer, list); |
| 151 | list_move_tail(q->list.next, &s->q_free.list); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame^] | 152 | buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0; |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 153 | atomic_inc(&s->q_free.buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 154 | } |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 155 | cx18_queue_init(q); |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 156 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void cx18_flush_queues(struct cx18_stream *s) |
| 160 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 161 | cx18_queue_flush(s, &s->q_io); |
| 162 | cx18_queue_flush(s, &s->q_full); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int cx18_stream_alloc(struct cx18_stream *s) |
| 166 | { |
| 167 | struct cx18 *cx = s->cx; |
| 168 | int i; |
| 169 | |
| 170 | if (s->buffers == 0) |
| 171 | return 0; |
| 172 | |
| 173 | CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n", |
| 174 | s->name, s->buffers, s->buf_size, |
| 175 | s->buffers * s->buf_size / 1024); |
| 176 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 177 | if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] - |
| 178 | (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) { |
| 179 | unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE - |
| 180 | ((char __iomem *)cx->scb->cpu_mdl)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 181 | |
| 182 | CX18_ERR("Too many buffers, cannot fit in SCB area\n"); |
| 183 | CX18_ERR("Max buffers = %zd\n", |
| 184 | bufsz / sizeof(struct cx18_mdl)); |
| 185 | return -ENOMEM; |
| 186 | } |
| 187 | |
| 188 | s->mdl_offset = cx->mdl_offset; |
| 189 | |
| 190 | /* allocate stream buffers. Initially all buffers are in q_free. */ |
| 191 | for (i = 0; i < s->buffers; i++) { |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 192 | struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer), |
| 193 | GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 194 | |
| 195 | if (buf == NULL) |
| 196 | break; |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 197 | buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 198 | if (buf->buf == NULL) { |
| 199 | kfree(buf); |
| 200 | break; |
| 201 | } |
| 202 | buf->id = cx->buffer_id++; |
| 203 | INIT_LIST_HEAD(&buf->list); |
| 204 | buf->dma_handle = pci_map_single(s->cx->dev, |
| 205 | buf->buf, s->buf_size, s->dma); |
| 206 | cx18_buf_sync_for_cpu(s, buf); |
| 207 | cx18_enqueue(s, buf, &s->q_free); |
| 208 | } |
| 209 | if (i == s->buffers) { |
| 210 | cx->mdl_offset += s->buffers; |
| 211 | return 0; |
| 212 | } |
| 213 | CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name); |
| 214 | cx18_stream_free(s); |
| 215 | return -ENOMEM; |
| 216 | } |
| 217 | |
| 218 | void cx18_stream_free(struct cx18_stream *s) |
| 219 | { |
| 220 | struct cx18_buffer *buf; |
| 221 | |
| 222 | /* move all buffers to q_free */ |
| 223 | cx18_flush_queues(s); |
| 224 | |
| 225 | /* empty q_free */ |
| 226 | while ((buf = cx18_dequeue(s, &s->q_free))) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 227 | pci_unmap_single(s->cx->dev, buf->dma_handle, |
| 228 | s->buf_size, s->dma); |
| 229 | kfree(buf->buf); |
| 230 | kfree(buf); |
| 231 | } |
| 232 | } |