Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 2 | /* |
| 3 | * cx18 buffer queues |
| 4 | * |
| 5 | * Derived from ivtv-queue.c |
| 6 | * |
| 7 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
Andy Walls | 6afdeaf | 2010-05-23 18:53:35 -0300 | [diff] [blame] | 8 | * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "cx18-driver.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 12 | #include "cx18-queue.h" |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 13 | #include "cx18-streams.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 14 | #include "cx18-scb.h" |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 15 | #include "cx18-io.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 16 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 17 | void cx18_buf_swap(struct cx18_buffer *buf) |
| 18 | { |
| 19 | int i; |
| 20 | |
| 21 | for (i = 0; i < buf->bytesused; i += 4) |
| 22 | swab32s((u32 *)(buf->buf + i)); |
| 23 | } |
| 24 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 25 | void _cx18_mdl_swap(struct cx18_mdl *mdl) |
| 26 | { |
| 27 | struct cx18_buffer *buf; |
| 28 | |
| 29 | list_for_each_entry(buf, &mdl->buf_list, list) { |
| 30 | if (buf->bytesused == 0) |
| 31 | break; |
| 32 | cx18_buf_swap(buf); |
| 33 | } |
| 34 | } |
| 35 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 36 | void cx18_queue_init(struct cx18_queue *q) |
| 37 | { |
| 38 | INIT_LIST_HEAD(&q->list); |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 39 | atomic_set(&q->depth, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 40 | q->bytesused = 0; |
| 41 | } |
| 42 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 43 | struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl, |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 44 | struct cx18_queue *q, int to_front) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 45 | { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 46 | /* clear the mdl if it is not to be enqueued to the full queue */ |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 47 | if (q != &s->q_full) { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 48 | mdl->bytesused = 0; |
| 49 | mdl->readpos = 0; |
| 50 | mdl->m_flags = 0; |
| 51 | mdl->skipped = 0; |
| 52 | mdl->curr_buf = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 53 | } |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 54 | |
Andy Walls | 0ef0289 | 2008-12-14 18:52:12 -0300 | [diff] [blame] | 55 | /* q_busy is restricted to a max buffer count imposed by firmware */ |
| 56 | if (q == &s->q_busy && |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 57 | atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM) |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 58 | q = &s->q_free; |
| 59 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 60 | spin_lock(&q->lock); |
| 61 | |
Andy Walls | b80e107 | 2008-11-28 00:04:21 -0300 | [diff] [blame] | 62 | if (to_front) |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 63 | list_add(&mdl->list, &q->list); /* LIFO */ |
Andy Walls | b80e107 | 2008-11-28 00:04:21 -0300 | [diff] [blame] | 64 | else |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 65 | list_add_tail(&mdl->list, &q->list); /* FIFO */ |
| 66 | q->bytesused += mdl->bytesused - mdl->readpos; |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 67 | atomic_inc(&q->depth); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 68 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 69 | spin_unlock(&q->lock); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 70 | return q; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 71 | } |
| 72 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 73 | struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 74 | { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 75 | struct cx18_mdl *mdl = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 76 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 77 | spin_lock(&q->lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 78 | if (!list_empty(&q->list)) { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 79 | mdl = list_first_entry(&q->list, struct cx18_mdl, list); |
| 80 | list_del_init(&mdl->list); |
| 81 | q->bytesused -= mdl->bytesused - mdl->readpos; |
| 82 | mdl->skipped = 0; |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 83 | atomic_dec(&q->depth); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 84 | } |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 85 | spin_unlock(&q->lock); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 86 | return mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 87 | } |
| 88 | |
Andy Walls | ad689d5 | 2009-11-11 00:57:16 -0300 | [diff] [blame] | 89 | static void _cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s, |
| 90 | struct cx18_mdl *mdl) |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 91 | { |
| 92 | struct cx18_buffer *buf; |
| 93 | u32 buf_size = s->buf_size; |
| 94 | u32 bytesused = mdl->bytesused; |
| 95 | |
| 96 | list_for_each_entry(buf, &mdl->buf_list, list) { |
| 97 | buf->readpos = 0; |
| 98 | if (bytesused >= buf_size) { |
| 99 | buf->bytesused = buf_size; |
| 100 | bytesused -= buf_size; |
| 101 | } else { |
| 102 | buf->bytesused = bytesused; |
| 103 | bytesused = 0; |
| 104 | } |
Andy Walls | ad689d5 | 2009-11-11 00:57:16 -0300 | [diff] [blame] | 105 | cx18_buf_sync_for_cpu(s, buf); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Andy Walls | ad689d5 | 2009-11-11 00:57:16 -0300 | [diff] [blame] | 109 | static inline void cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s, |
| 110 | struct cx18_mdl *mdl) |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 111 | { |
| 112 | struct cx18_buffer *buf; |
| 113 | |
| 114 | if (list_is_singular(&mdl->buf_list)) { |
| 115 | buf = list_first_entry(&mdl->buf_list, struct cx18_buffer, |
| 116 | list); |
| 117 | buf->bytesused = mdl->bytesused; |
| 118 | buf->readpos = 0; |
Andy Walls | ad689d5 | 2009-11-11 00:57:16 -0300 | [diff] [blame] | 119 | cx18_buf_sync_for_cpu(s, buf); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 120 | } else { |
Andy Walls | ad689d5 | 2009-11-11 00:57:16 -0300 | [diff] [blame] | 121 | _cx18_mdl_update_bufs_for_cpu(s, mdl); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 126 | u32 bytesused) |
| 127 | { |
| 128 | struct cx18 *cx = s->cx; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 129 | struct cx18_mdl *mdl; |
| 130 | struct cx18_mdl *tmp; |
| 131 | struct cx18_mdl *ret = NULL; |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 132 | LIST_HEAD(sweep_up); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 133 | |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 134 | /* |
| 135 | * We don't have to acquire multiple q locks here, because we are |
| 136 | * serialized by the single threaded work handler. |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 137 | * MDLs from the firmware will thus remain in order as |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 138 | * they are moved from q_busy to q_full or to the dvb ring buffer. |
| 139 | */ |
| 140 | spin_lock(&s->q_busy.lock); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 141 | list_for_each_entry_safe(mdl, tmp, &s->q_busy.list, list) { |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 142 | /* |
| 143 | * We should find what the firmware told us is done, |
| 144 | * right at the front of the queue. If we don't, we likely have |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 145 | * missed an mdl done message from the firmware. |
| 146 | * Once we skip an mdl repeatedly, relative to the size of |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 147 | * q_busy, we have high confidence we've missed it. |
| 148 | */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 149 | if (mdl->id != id) { |
| 150 | mdl->skipped++; |
| 151 | if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) { |
| 152 | /* mdl must have fallen out of rotation */ |
Mauro Carvalho Chehab | 6beb138 | 2016-10-18 17:44:03 -0200 | [diff] [blame] | 153 | CX18_WARN("Skipped %s, MDL %d, %d times - it must have dropped out of rotation\n", |
| 154 | s->name, mdl->id, |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 155 | mdl->skipped); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 156 | /* Sweep it up to put it back into rotation */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 157 | list_move_tail(&mdl->list, &sweep_up); |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 158 | atomic_dec(&s->q_busy.depth); |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 159 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 160 | continue; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 161 | } |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 162 | /* |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 163 | * We pull the desired mdl off of the queue here. Something |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 164 | * will have to put it back on a queue later. |
| 165 | */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 166 | list_del_init(&mdl->list); |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 167 | atomic_dec(&s->q_busy.depth); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 168 | ret = mdl; |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 169 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 170 | } |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 171 | spin_unlock(&s->q_busy.lock); |
| 172 | |
| 173 | /* |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 174 | * We found the mdl for which we were looking. Get it ready for |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 175 | * the caller to put on q_full or in the dvb ring buffer. |
| 176 | */ |
| 177 | if (ret != NULL) { |
| 178 | ret->bytesused = bytesused; |
| 179 | ret->skipped = 0; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 180 | /* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */ |
Andy Walls | ad689d5 | 2009-11-11 00:57:16 -0300 | [diff] [blame] | 181 | cx18_mdl_update_bufs_for_cpu(s, ret); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 182 | if (s->type != CX18_ENC_STREAM_TYPE_TS) |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 183 | set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 184 | } |
| 185 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 186 | /* Put any mdls the firmware is ignoring back into normal rotation */ |
| 187 | list_for_each_entry_safe(mdl, tmp, &sweep_up, list) { |
| 188 | list_del_init(&mdl->list); |
| 189 | cx18_enqueue(s, mdl, &s->q_free); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 190 | } |
Andy Walls | bca11a5 | 2008-11-19 01:24:33 -0300 | [diff] [blame] | 191 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 192 | } |
| 193 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 194 | /* Move all mdls of a queue, while flushing the mdl */ |
| 195 | static void cx18_queue_flush(struct cx18_stream *s, |
| 196 | struct cx18_queue *q_src, struct cx18_queue *q_dst) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 197 | { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 198 | struct cx18_mdl *mdl; |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 199 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 200 | /* It only makes sense to flush to q_free or q_idle */ |
| 201 | if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy) |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 202 | return; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 203 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 204 | spin_lock(&q_src->lock); |
| 205 | spin_lock(&q_dst->lock); |
| 206 | while (!list_empty(&q_src->list)) { |
| 207 | mdl = list_first_entry(&q_src->list, struct cx18_mdl, list); |
| 208 | list_move_tail(&mdl->list, &q_dst->list); |
| 209 | mdl->bytesused = 0; |
| 210 | mdl->readpos = 0; |
| 211 | mdl->m_flags = 0; |
| 212 | mdl->skipped = 0; |
| 213 | mdl->curr_buf = NULL; |
| 214 | atomic_inc(&q_dst->depth); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 215 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 216 | cx18_queue_init(q_src); |
| 217 | spin_unlock(&q_src->lock); |
| 218 | spin_unlock(&q_dst->lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void cx18_flush_queues(struct cx18_stream *s) |
| 222 | { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 223 | cx18_queue_flush(s, &s->q_busy, &s->q_free); |
| 224 | cx18_queue_flush(s, &s->q_full, &s->q_free); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Note, s->buf_pool is not protected by a lock, |
| 229 | * the stream better not have *anything* going on when calling this |
| 230 | */ |
| 231 | void cx18_unload_queues(struct cx18_stream *s) |
| 232 | { |
| 233 | struct cx18_queue *q_idle = &s->q_idle; |
| 234 | struct cx18_mdl *mdl; |
| 235 | struct cx18_buffer *buf; |
| 236 | |
| 237 | /* Move all MDLS to q_idle */ |
| 238 | cx18_queue_flush(s, &s->q_busy, q_idle); |
| 239 | cx18_queue_flush(s, &s->q_full, q_idle); |
| 240 | cx18_queue_flush(s, &s->q_free, q_idle); |
| 241 | |
| 242 | /* Reset MDL id's and move all buffers back to the stream's buf_pool */ |
| 243 | spin_lock(&q_idle->lock); |
| 244 | list_for_each_entry(mdl, &q_idle->list, list) { |
| 245 | while (!list_empty(&mdl->buf_list)) { |
| 246 | buf = list_first_entry(&mdl->buf_list, |
| 247 | struct cx18_buffer, list); |
| 248 | list_move_tail(&buf->list, &s->buf_pool); |
| 249 | buf->bytesused = 0; |
| 250 | buf->readpos = 0; |
| 251 | } |
| 252 | mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */ |
| 253 | /* all other mdl fields were cleared by cx18_queue_flush() */ |
| 254 | } |
| 255 | spin_unlock(&q_idle->lock); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Note, s->buf_pool is not protected by a lock, |
| 260 | * the stream better not have *anything* going on when calling this |
| 261 | */ |
| 262 | void cx18_load_queues(struct cx18_stream *s) |
| 263 | { |
| 264 | struct cx18 *cx = s->cx; |
| 265 | struct cx18_mdl *mdl; |
| 266 | struct cx18_buffer *buf; |
| 267 | int mdl_id; |
| 268 | int i; |
Andy Walls | 1047a83 | 2009-11-10 23:28:30 -0300 | [diff] [blame] | 269 | u32 partial_buf_size; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free |
| 273 | * Excess MDLs are left on q_idle |
| 274 | * Excess buffers are left in buf_pool and/or on an MDL in q_idle |
| 275 | */ |
| 276 | mdl_id = s->mdl_base_idx; |
| 277 | for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl; |
| 278 | mdl != NULL && i == s->bufs_per_mdl; |
| 279 | mdl = cx18_dequeue(s, &s->q_idle)) { |
| 280 | |
| 281 | mdl->id = mdl_id; |
| 282 | |
| 283 | for (i = 0; i < s->bufs_per_mdl; i++) { |
| 284 | if (list_empty(&s->buf_pool)) |
| 285 | break; |
| 286 | |
| 287 | buf = list_first_entry(&s->buf_pool, struct cx18_buffer, |
| 288 | list); |
| 289 | list_move_tail(&buf->list, &mdl->buf_list); |
| 290 | |
| 291 | /* update the firmware's MDL array with this buffer */ |
| 292 | cx18_writel(cx, buf->dma_handle, |
| 293 | &cx->scb->cpu_mdl[mdl_id + i].paddr); |
| 294 | cx18_writel(cx, s->buf_size, |
| 295 | &cx->scb->cpu_mdl[mdl_id + i].length); |
| 296 | } |
| 297 | |
Andy Walls | 1047a83 | 2009-11-10 23:28:30 -0300 | [diff] [blame] | 298 | if (i == s->bufs_per_mdl) { |
| 299 | /* |
| 300 | * The encoder doesn't honor s->mdl_size. So in the |
| 301 | * case of a non-integral number of buffers to meet |
| 302 | * mdl_size, we lie about the size of the last buffer |
| 303 | * in the MDL to get the encoder to really only send |
| 304 | * us mdl_size bytes per MDL transfer. |
| 305 | */ |
| 306 | partial_buf_size = s->mdl_size % s->buf_size; |
| 307 | if (partial_buf_size) { |
| 308 | cx18_writel(cx, partial_buf_size, |
| 309 | &cx->scb->cpu_mdl[mdl_id + i - 1].length); |
| 310 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 311 | cx18_enqueue(s, mdl, &s->q_free); |
Andy Walls | 1047a83 | 2009-11-10 23:28:30 -0300 | [diff] [blame] | 312 | } else { |
| 313 | /* Not enough buffers for this MDL; we won't use it */ |
| 314 | cx18_push(s, mdl, &s->q_idle); |
| 315 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 316 | mdl_id += i; |
| 317 | } |
| 318 | } |
| 319 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 320 | void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl) |
| 321 | { |
| 322 | int dma = s->dma; |
| 323 | u32 buf_size = s->buf_size; |
| 324 | struct pci_dev *pci_dev = s->cx->pci_dev; |
| 325 | struct cx18_buffer *buf; |
| 326 | |
| 327 | list_for_each_entry(buf, &mdl->buf_list, list) |
| 328 | pci_dma_sync_single_for_device(pci_dev, buf->dma_handle, |
| 329 | buf_size, dma); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | int cx18_stream_alloc(struct cx18_stream *s) |
| 333 | { |
| 334 | struct cx18 *cx = s->cx; |
| 335 | int i; |
| 336 | |
| 337 | if (s->buffers == 0) |
| 338 | return 0; |
| 339 | |
Mauro Carvalho Chehab | 6beb138 | 2016-10-18 17:44:03 -0200 | [diff] [blame] | 340 | CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%d.%02d kB total)\n", |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 341 | s->name, s->buffers, s->buf_size, |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 342 | s->buffers * s->buf_size / 1024, |
| 343 | (s->buffers * s->buf_size * 100 / 1024) % 100); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 344 | |
Andy Walls | fa655dd | 2009-11-05 21:51:24 -0300 | [diff] [blame] | 345 | if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] - |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 346 | (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) { |
| 347 | unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE - |
| 348 | ((char __iomem *)cx->scb->cpu_mdl)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 349 | |
| 350 | CX18_ERR("Too many buffers, cannot fit in SCB area\n"); |
Mauro Carvalho Chehab | 339f06c | 2014-09-24 20:35:48 -0300 | [diff] [blame] | 351 | CX18_ERR("Max buffers = %zu\n", |
Andy Walls | f0076e6 | 2009-11-04 22:33:33 -0300 | [diff] [blame] | 352 | bufsz / sizeof(struct cx18_mdl_ent)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 353 | return -ENOMEM; |
| 354 | } |
| 355 | |
Andy Walls | fa655dd | 2009-11-05 21:51:24 -0300 | [diff] [blame] | 356 | s->mdl_base_idx = cx->free_mdl_idx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 357 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 358 | /* allocate stream buffers and MDLs */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 359 | for (i = 0; i < s->buffers; i++) { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 360 | struct cx18_mdl *mdl; |
| 361 | struct cx18_buffer *buf; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 362 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 363 | /* 1 MDL per buffer to handle the worst & also default case */ |
| 364 | mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN); |
| 365 | if (mdl == NULL) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 366 | break; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 367 | |
| 368 | buf = kzalloc(sizeof(struct cx18_buffer), |
| 369 | GFP_KERNEL|__GFP_NOWARN); |
| 370 | if (buf == NULL) { |
| 371 | kfree(mdl); |
| 372 | break; |
| 373 | } |
| 374 | |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 375 | buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 376 | if (buf->buf == NULL) { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 377 | kfree(mdl); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 378 | kfree(buf); |
| 379 | break; |
| 380 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 381 | |
| 382 | INIT_LIST_HEAD(&mdl->list); |
| 383 | INIT_LIST_HEAD(&mdl->buf_list); |
| 384 | mdl->id = s->mdl_base_idx; /* a somewhat safe value */ |
| 385 | cx18_enqueue(s, mdl, &s->q_idle); |
| 386 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 387 | INIT_LIST_HEAD(&buf->list); |
Andy Walls | 3d05913d | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 388 | buf->dma_handle = pci_map_single(s->cx->pci_dev, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 389 | buf->buf, s->buf_size, s->dma); |
| 390 | cx18_buf_sync_for_cpu(s, buf); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 391 | list_add_tail(&buf->list, &s->buf_pool); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 392 | } |
| 393 | if (i == s->buffers) { |
Andy Walls | fa655dd | 2009-11-05 21:51:24 -0300 | [diff] [blame] | 394 | cx->free_mdl_idx += s->buffers; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name); |
| 398 | cx18_stream_free(s); |
| 399 | return -ENOMEM; |
| 400 | } |
| 401 | |
| 402 | void cx18_stream_free(struct cx18_stream *s) |
| 403 | { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 404 | struct cx18_mdl *mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 405 | struct cx18_buffer *buf; |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 406 | struct cx18 *cx = s->cx; |
| 407 | |
| 408 | CX18_DEBUG_INFO("Deallocating buffers for %s stream\n", s->name); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 409 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 410 | /* move all buffers to buf_pool and all MDLs to q_idle */ |
| 411 | cx18_unload_queues(s); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 412 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 413 | /* empty q_idle */ |
| 414 | while ((mdl = cx18_dequeue(s, &s->q_idle))) |
| 415 | kfree(mdl); |
| 416 | |
| 417 | /* empty buf_pool */ |
| 418 | while (!list_empty(&s->buf_pool)) { |
| 419 | buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list); |
| 420 | list_del_init(&buf->list); |
| 421 | |
Andy Walls | 3d05913d | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 422 | pci_unmap_single(s->cx->pci_dev, buf->dma_handle, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 423 | s->buf_size, s->dma); |
| 424 | kfree(buf->buf); |
| 425 | kfree(buf); |
| 426 | } |
| 427 | } |