1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Coda multi-standard codec IP - BIT processor functions
4 *
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
7 * Xavier Duret
8 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
9 */
10
11 #include <linux/clk.h>
12 #include <linux/irqreturn.h>
13 #include <linux/kernel.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/ratelimit.h>
17 #include <linux/reset.h>
18 #include <linux/slab.h>
19 #include <linux/videodev2.h>
20
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-mem2mem.h>
25 #include <media/videobuf2-v4l2.h>
26 #include <media/videobuf2-dma-contig.h>
27 #include <media/videobuf2-vmalloc.h>
28
29 #include "coda.h"
30 #include "imx-vdoa.h"
31 #define CREATE_TRACE_POINTS
32 #include "trace.h"
33
34 #define CODA_PARA_BUF_SIZE (10 * 1024)
35 #define CODA7_PS_BUF_SIZE 0x28000
36 #define CODA9_PS_SAVE_SIZE (512 * 1024)
37
38 #define CODA_DEFAULT_GAMMA 4096
39 #define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
40
41 static void coda_free_bitstream_buffer(struct coda_ctx *ctx);
42
coda_is_initialized(struct coda_dev * dev)43 static inline int coda_is_initialized(struct coda_dev *dev)
44 {
45 return coda_read(dev, CODA_REG_BIT_CUR_PC) != 0;
46 }
47
coda_isbusy(struct coda_dev * dev)48 static inline unsigned long coda_isbusy(struct coda_dev *dev)
49 {
50 return coda_read(dev, CODA_REG_BIT_BUSY);
51 }
52
coda_wait_timeout(struct coda_dev * dev)53 static int coda_wait_timeout(struct coda_dev *dev)
54 {
55 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
56
57 while (coda_isbusy(dev)) {
58 if (time_after(jiffies, timeout))
59 return -ETIMEDOUT;
60 }
61 return 0;
62 }
63
coda_command_async(struct coda_ctx * ctx,int cmd)64 static void coda_command_async(struct coda_ctx *ctx, int cmd)
65 {
66 struct coda_dev *dev = ctx->dev;
67
68 if (dev->devtype->product == CODA_HX4 ||
69 dev->devtype->product == CODA_7541 ||
70 dev->devtype->product == CODA_960) {
71 /* Restore context related registers to CODA */
72 coda_write(dev, ctx->bit_stream_param,
73 CODA_REG_BIT_BIT_STREAM_PARAM);
74 coda_write(dev, ctx->frm_dis_flg,
75 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
76 coda_write(dev, ctx->frame_mem_ctrl,
77 CODA_REG_BIT_FRAME_MEM_CTRL);
78 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
79 }
80
81 if (dev->devtype->product == CODA_960) {
82 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
83 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
84 }
85
86 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
87
88 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
89 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
90 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
91
92 trace_coda_bit_run(ctx, cmd);
93
94 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
95 }
96
coda_command_sync(struct coda_ctx * ctx,int cmd)97 static int coda_command_sync(struct coda_ctx *ctx, int cmd)
98 {
99 struct coda_dev *dev = ctx->dev;
100 int ret;
101
102 lockdep_assert_held(&dev->coda_mutex);
103
104 coda_command_async(ctx, cmd);
105 ret = coda_wait_timeout(dev);
106 trace_coda_bit_done(ctx);
107
108 return ret;
109 }
110
coda_hw_reset(struct coda_ctx * ctx)111 int coda_hw_reset(struct coda_ctx *ctx)
112 {
113 struct coda_dev *dev = ctx->dev;
114 unsigned long timeout;
115 unsigned int idx;
116 int ret;
117
118 lockdep_assert_held(&dev->coda_mutex);
119
120 if (!dev->rstc)
121 return -ENOENT;
122
123 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
124
125 if (dev->devtype->product == CODA_960) {
126 timeout = jiffies + msecs_to_jiffies(100);
127 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
128 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
129 if (time_after(jiffies, timeout))
130 return -ETIME;
131 cpu_relax();
132 }
133 }
134
135 ret = reset_control_reset(dev->rstc);
136 if (ret < 0)
137 return ret;
138
139 if (dev->devtype->product == CODA_960)
140 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
141 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
142 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
143 ret = coda_wait_timeout(dev);
144 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
145
146 return ret;
147 }
148
coda_kfifo_sync_from_device(struct coda_ctx * ctx)149 static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
150 {
151 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
152 struct coda_dev *dev = ctx->dev;
153 u32 rd_ptr;
154
155 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
156 kfifo->out = (kfifo->in & ~kfifo->mask) |
157 (rd_ptr - ctx->bitstream.paddr);
158 if (kfifo->out > kfifo->in)
159 kfifo->out -= kfifo->mask + 1;
160 }
161
coda_kfifo_sync_to_device_full(struct coda_ctx * ctx)162 static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
163 {
164 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
165 struct coda_dev *dev = ctx->dev;
166 u32 rd_ptr, wr_ptr;
167
168 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
169 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
170 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
171 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
172 }
173
coda_kfifo_sync_to_device_write(struct coda_ctx * ctx)174 static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
175 {
176 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
177 struct coda_dev *dev = ctx->dev;
178 u32 wr_ptr;
179
180 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
181 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
182 }
183
coda_h264_bitstream_pad(struct coda_ctx * ctx,u32 size)184 static int coda_h264_bitstream_pad(struct coda_ctx *ctx, u32 size)
185 {
186 unsigned char *buf;
187 u32 n;
188
189 if (size < 6)
190 size = 6;
191
192 buf = kmalloc(size, GFP_KERNEL);
193 if (!buf)
194 return -ENOMEM;
195
196 coda_h264_filler_nal(size, buf);
197 n = kfifo_in(&ctx->bitstream_fifo, buf, size);
198 kfree(buf);
199
200 return (n < size) ? -ENOSPC : 0;
201 }
202
coda_bitstream_flush(struct coda_ctx * ctx)203 int coda_bitstream_flush(struct coda_ctx *ctx)
204 {
205 int ret;
206
207 if (ctx->inst_type != CODA_INST_DECODER || !ctx->use_bit)
208 return 0;
209
210 ret = coda_command_sync(ctx, CODA_COMMAND_DEC_BUF_FLUSH);
211 if (ret < 0) {
212 v4l2_err(&ctx->dev->v4l2_dev, "failed to flush bitstream\n");
213 return ret;
214 }
215
216 kfifo_init(&ctx->bitstream_fifo, ctx->bitstream.vaddr,
217 ctx->bitstream.size);
218 coda_kfifo_sync_to_device_full(ctx);
219
220 return 0;
221 }
222
coda_bitstream_queue(struct coda_ctx * ctx,const u8 * buf,u32 size)223 static int coda_bitstream_queue(struct coda_ctx *ctx, const u8 *buf, u32 size)
224 {
225 u32 n = kfifo_in(&ctx->bitstream_fifo, buf, size);
226
227 return (n < size) ? -ENOSPC : 0;
228 }
229
coda_buffer_parse_headers(struct coda_ctx * ctx,struct vb2_v4l2_buffer * src_buf,u32 payload)230 static u32 coda_buffer_parse_headers(struct coda_ctx *ctx,
231 struct vb2_v4l2_buffer *src_buf,
232 u32 payload)
233 {
234 u8 *vaddr = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
235 u32 size = 0;
236
237 switch (ctx->codec->src_fourcc) {
238 case V4L2_PIX_FMT_MPEG2:
239 size = coda_mpeg2_parse_headers(ctx, vaddr, payload);
240 break;
241 case V4L2_PIX_FMT_MPEG4:
242 size = coda_mpeg4_parse_headers(ctx, vaddr, payload);
243 break;
244 default:
245 break;
246 }
247
248 return size;
249 }
250
coda_bitstream_try_queue(struct coda_ctx * ctx,struct vb2_v4l2_buffer * src_buf)251 static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
252 struct vb2_v4l2_buffer *src_buf)
253 {
254 unsigned long payload = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
255 u8 *vaddr = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
256 int ret;
257 int i;
258
259 if (coda_get_bitstream_payload(ctx) + payload + 512 >=
260 ctx->bitstream.size)
261 return false;
262
263 if (!vaddr) {
264 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
265 return true;
266 }
267
268 if (ctx->qsequence == 0 && payload < 512) {
269 /*
270 * Add padding after the first buffer, if it is too small to be
271 * fetched by the CODA, by repeating the headers. Without
272 * repeated headers, or the first frame already queued, decoder
273 * sequence initialization fails with error code 0x2000 on i.MX6
274 * or error code 0x1 on i.MX51.
275 */
276 u32 header_size = coda_buffer_parse_headers(ctx, src_buf,
277 payload);
278
279 if (header_size) {
280 coda_dbg(1, ctx, "pad with %u-byte header\n",
281 header_size);
282 for (i = payload; i < 512; i += header_size) {
283 ret = coda_bitstream_queue(ctx, vaddr,
284 header_size);
285 if (ret < 0) {
286 v4l2_err(&ctx->dev->v4l2_dev,
287 "bitstream buffer overflow\n");
288 return false;
289 }
290 if (ctx->dev->devtype->product == CODA_960)
291 break;
292 }
293 } else {
294 coda_dbg(1, ctx,
295 "could not parse header, sequence initialization might fail\n");
296 }
297
298 /* Add padding before the first buffer, if it is too small */
299 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
300 coda_h264_bitstream_pad(ctx, 512 - payload);
301 }
302
303 ret = coda_bitstream_queue(ctx, vaddr, payload);
304 if (ret < 0) {
305 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
306 return false;
307 }
308
309 src_buf->sequence = ctx->qsequence++;
310
311 /* Sync read pointer to device */
312 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
313 coda_kfifo_sync_to_device_write(ctx);
314
315 /* Set the stream-end flag after the last buffer is queued */
316 if (src_buf->flags & V4L2_BUF_FLAG_LAST)
317 coda_bit_stream_end_flag(ctx);
318 ctx->hold = false;
319
320 return true;
321 }
322
coda_fill_bitstream(struct coda_ctx * ctx,struct list_head * buffer_list)323 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list)
324 {
325 struct vb2_v4l2_buffer *src_buf;
326 struct coda_buffer_meta *meta;
327 u32 start;
328
329 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG)
330 return;
331
332 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
333 /*
334 * Only queue two JPEGs into the bitstream buffer to keep
335 * latency low. We need at least one complete buffer and the
336 * header of another buffer (for prescan) in the bitstream.
337 */
338 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
339 ctx->num_metas > 1)
340 break;
341
342 if (ctx->num_internal_frames &&
343 ctx->num_metas >= ctx->num_internal_frames) {
344 meta = list_first_entry(&ctx->buffer_meta_list,
345 struct coda_buffer_meta, list);
346
347 /*
348 * If we managed to fill in at least a full reorder
349 * window of buffers (num_internal_frames is a
350 * conservative estimate for this) and the bitstream
351 * prefetcher has at least 2 256 bytes periods beyond
352 * the first buffer to fetch, we can safely stop queuing
353 * in order to limit the decoder drain latency.
354 */
355 if (coda_bitstream_can_fetch_past(ctx, meta->end))
356 break;
357 }
358
359 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
360
361 /* Drop frames that do not start/end with a SOI/EOI markers */
362 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
363 !coda_jpeg_check_buffer(ctx, &src_buf->vb2_buf)) {
364 v4l2_err(&ctx->dev->v4l2_dev,
365 "dropping invalid JPEG frame %d\n",
366 ctx->qsequence);
367 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
368 if (buffer_list) {
369 struct v4l2_m2m_buffer *m2m_buf;
370
371 m2m_buf = container_of(src_buf,
372 struct v4l2_m2m_buffer,
373 vb);
374 list_add_tail(&m2m_buf->list, buffer_list);
375 } else {
376 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
377 }
378 continue;
379 }
380
381 /* Dump empty buffers */
382 if (!vb2_get_plane_payload(&src_buf->vb2_buf, 0)) {
383 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
384 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
385 continue;
386 }
387
388 /* Buffer start position */
389 start = ctx->bitstream_fifo.kfifo.in;
390
391 if (coda_bitstream_try_queue(ctx, src_buf)) {
392 /*
393 * Source buffer is queued in the bitstream ringbuffer;
394 * queue the timestamp and mark source buffer as done
395 */
396 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
397
398 meta = kmalloc(sizeof(*meta), GFP_KERNEL);
399 if (meta) {
400 meta->sequence = src_buf->sequence;
401 meta->timecode = src_buf->timecode;
402 meta->timestamp = src_buf->vb2_buf.timestamp;
403 meta->start = start;
404 meta->end = ctx->bitstream_fifo.kfifo.in;
405 meta->last = src_buf->flags & V4L2_BUF_FLAG_LAST;
406 if (meta->last)
407 coda_dbg(1, ctx, "marking last meta");
408 spin_lock(&ctx->buffer_meta_lock);
409 list_add_tail(&meta->list,
410 &ctx->buffer_meta_list);
411 ctx->num_metas++;
412 spin_unlock(&ctx->buffer_meta_lock);
413
414 trace_coda_bit_queue(ctx, src_buf, meta);
415 }
416
417 if (buffer_list) {
418 struct v4l2_m2m_buffer *m2m_buf;
419
420 m2m_buf = container_of(src_buf,
421 struct v4l2_m2m_buffer,
422 vb);
423 list_add_tail(&m2m_buf->list, buffer_list);
424 } else {
425 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
426 }
427 } else {
428 break;
429 }
430 }
431 }
432
coda_bit_stream_end_flag(struct coda_ctx * ctx)433 void coda_bit_stream_end_flag(struct coda_ctx *ctx)
434 {
435 struct coda_dev *dev = ctx->dev;
436
437 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
438
439 /* If this context is currently running, update the hardware flag */
440 if ((dev->devtype->product == CODA_960) &&
441 coda_isbusy(dev) &&
442 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
443 coda_write(dev, ctx->bit_stream_param,
444 CODA_REG_BIT_BIT_STREAM_PARAM);
445 }
446 }
447
coda_parabuf_write(struct coda_ctx * ctx,int index,u32 value)448 static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
449 {
450 struct coda_dev *dev = ctx->dev;
451 u32 *p = ctx->parabuf.vaddr;
452
453 if (dev->devtype->product == CODA_DX6)
454 p[index] = value;
455 else
456 p[index ^ 1] = value;
457 }
458
coda_alloc_context_buf(struct coda_ctx * ctx,struct coda_aux_buf * buf,size_t size,const char * name)459 static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
460 struct coda_aux_buf *buf, size_t size,
461 const char *name)
462 {
463 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
464 }
465
466
coda_free_framebuffers(struct coda_ctx * ctx)467 static void coda_free_framebuffers(struct coda_ctx *ctx)
468 {
469 int i;
470
471 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
472 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i].buf);
473 }
474
coda_alloc_framebuffers(struct coda_ctx * ctx,struct coda_q_data * q_data,u32 fourcc)475 static int coda_alloc_framebuffers(struct coda_ctx *ctx,
476 struct coda_q_data *q_data, u32 fourcc)
477 {
478 struct coda_dev *dev = ctx->dev;
479 unsigned int ysize, ycbcr_size;
480 int ret;
481 int i;
482
483 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
484 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264 ||
485 ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4 ||
486 ctx->codec->dst_fourcc == V4L2_PIX_FMT_MPEG4)
487 ysize = round_up(q_data->rect.width, 16) *
488 round_up(q_data->rect.height, 16);
489 else
490 ysize = round_up(q_data->rect.width, 8) * q_data->rect.height;
491
492 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
493 ycbcr_size = round_up(ysize, 4096) + ysize / 2;
494 else
495 ycbcr_size = ysize + ysize / 2;
496
497 /* Allocate frame buffers */
498 for (i = 0; i < ctx->num_internal_frames; i++) {
499 size_t size = ycbcr_size;
500 char *name;
501
502 /* Add space for mvcol buffers */
503 if (dev->devtype->product != CODA_DX6 &&
504 (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
505 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4 && i == 0)))
506 size += ysize / 4;
507 name = kasprintf(GFP_KERNEL, "fb%d", i);
508 if (!name) {
509 coda_free_framebuffers(ctx);
510 return -ENOMEM;
511 }
512 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i].buf,
513 size, name);
514 kfree(name);
515 if (ret < 0) {
516 coda_free_framebuffers(ctx);
517 return ret;
518 }
519 }
520
521 /* Register frame buffers in the parameter buffer */
522 for (i = 0; i < ctx->num_internal_frames; i++) {
523 u32 y, cb, cr, mvcol;
524
525 /* Start addresses of Y, Cb, Cr planes */
526 y = ctx->internal_frames[i].buf.paddr;
527 cb = y + ysize;
528 cr = y + ysize + ysize/4;
529 mvcol = y + ysize + ysize/4 + ysize/4;
530 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) {
531 cb = round_up(cb, 4096);
532 mvcol = cb + ysize/2;
533 cr = 0;
534 /* Packed 20-bit MSB of base addresses */
535 /* YYYYYCCC, CCyyyyyc, cccc.... */
536 y = (y & 0xfffff000) | cb >> 20;
537 cb = (cb & 0x000ff000) << 12;
538 }
539 coda_parabuf_write(ctx, i * 3 + 0, y);
540 coda_parabuf_write(ctx, i * 3 + 1, cb);
541 coda_parabuf_write(ctx, i * 3 + 2, cr);
542
543 if (dev->devtype->product == CODA_DX6)
544 continue;
545
546 /* mvcol buffer for h.264 and mpeg4 */
547 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
548 coda_parabuf_write(ctx, 96 + i, mvcol);
549 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4 && i == 0)
550 coda_parabuf_write(ctx, 97, mvcol);
551 }
552
553 return 0;
554 }
555
coda_free_context_buffers(struct coda_ctx * ctx)556 static void coda_free_context_buffers(struct coda_ctx *ctx)
557 {
558 struct coda_dev *dev = ctx->dev;
559
560 coda_free_aux_buf(dev, &ctx->slicebuf);
561 coda_free_aux_buf(dev, &ctx->psbuf);
562 if (dev->devtype->product != CODA_DX6)
563 coda_free_aux_buf(dev, &ctx->workbuf);
564 coda_free_aux_buf(dev, &ctx->parabuf);
565 }
566
coda_alloc_context_buffers(struct coda_ctx * ctx,struct coda_q_data * q_data)567 static int coda_alloc_context_buffers(struct coda_ctx *ctx,
568 struct coda_q_data *q_data)
569 {
570 struct coda_dev *dev = ctx->dev;
571 size_t size;
572 int ret;
573
574 if (!ctx->parabuf.vaddr) {
575 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
576 CODA_PARA_BUF_SIZE, "parabuf");
577 if (ret < 0)
578 return ret;
579 }
580
581 if (dev->devtype->product == CODA_DX6)
582 return 0;
583
584 if (!ctx->slicebuf.vaddr && q_data->fourcc == V4L2_PIX_FMT_H264) {
585 /* worst case slice size */
586 size = (DIV_ROUND_UP(q_data->rect.width, 16) *
587 DIV_ROUND_UP(q_data->rect.height, 16)) * 3200 / 8 + 512;
588 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size,
589 "slicebuf");
590 if (ret < 0)
591 goto err;
592 }
593
594 if (!ctx->psbuf.vaddr && (dev->devtype->product == CODA_HX4 ||
595 dev->devtype->product == CODA_7541)) {
596 ret = coda_alloc_context_buf(ctx, &ctx->psbuf,
597 CODA7_PS_BUF_SIZE, "psbuf");
598 if (ret < 0)
599 goto err;
600 }
601
602 if (!ctx->workbuf.vaddr) {
603 size = dev->devtype->workbuf_size;
604 if (dev->devtype->product == CODA_960 &&
605 q_data->fourcc == V4L2_PIX_FMT_H264)
606 size += CODA9_PS_SAVE_SIZE;
607 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size,
608 "workbuf");
609 if (ret < 0)
610 goto err;
611 }
612
613 return 0;
614
615 err:
616 coda_free_context_buffers(ctx);
617 return ret;
618 }
619
coda_encode_header(struct coda_ctx * ctx,struct vb2_v4l2_buffer * buf,int header_code,u8 * header,int * size)620 static int coda_encode_header(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
621 int header_code, u8 *header, int *size)
622 {
623 struct vb2_buffer *vb = &buf->vb2_buf;
624 struct coda_dev *dev = ctx->dev;
625 struct coda_q_data *q_data_src;
626 struct v4l2_rect *r;
627 size_t bufsize;
628 int ret;
629 int i;
630
631 if (dev->devtype->product == CODA_960)
632 memset(vb2_plane_vaddr(vb, 0), 0, 64);
633
634 coda_write(dev, vb2_dma_contig_plane_dma_addr(vb, 0),
635 CODA_CMD_ENC_HEADER_BB_START);
636 bufsize = vb2_plane_size(vb, 0);
637 if (dev->devtype->product == CODA_960)
638 bufsize /= 1024;
639 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
640 if (dev->devtype->product == CODA_960 &&
641 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264 &&
642 header_code == CODA_HEADER_H264_SPS) {
643 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
644 r = &q_data_src->rect;
645
646 if (r->width % 16 || r->height % 16) {
647 u32 crop_right = round_up(r->width, 16) - r->width;
648 u32 crop_bottom = round_up(r->height, 16) - r->height;
649
650 coda_write(dev, crop_right,
651 CODA9_CMD_ENC_HEADER_FRAME_CROP_H);
652 coda_write(dev, crop_bottom,
653 CODA9_CMD_ENC_HEADER_FRAME_CROP_V);
654 header_code |= CODA9_HEADER_FRAME_CROP;
655 }
656 }
657 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
658 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
659 if (ret < 0) {
660 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
661 return ret;
662 }
663
664 if (dev->devtype->product == CODA_960) {
665 for (i = 63; i > 0; i--)
666 if (((char *)vb2_plane_vaddr(vb, 0))[i] != 0)
667 break;
668 *size = i + 1;
669 } else {
670 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
671 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
672 }
673 memcpy(header, vb2_plane_vaddr(vb, 0), *size);
674
675 return 0;
676 }
677
coda_slice_mode(struct coda_ctx * ctx)678 static u32 coda_slice_mode(struct coda_ctx *ctx)
679 {
680 int size, unit;
681
682 switch (ctx->params.slice_mode) {
683 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
684 default:
685 return 0;
686 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_MB:
687 size = ctx->params.slice_max_mb;
688 unit = 1;
689 break;
690 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES:
691 size = ctx->params.slice_max_bits;
692 unit = 0;
693 break;
694 }
695
696 return ((size & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET) |
697 ((unit & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET) |
698 ((1 & CODA_SLICING_MODE_MASK) << CODA_SLICING_MODE_OFFSET);
699 }
700
coda_enc_param_change(struct coda_ctx * ctx)701 static int coda_enc_param_change(struct coda_ctx *ctx)
702 {
703 struct coda_dev *dev = ctx->dev;
704 u32 change_enable = 0;
705 u32 success;
706 int ret;
707
708 if (ctx->params.gop_size_changed) {
709 change_enable |= CODA_PARAM_CHANGE_RC_GOP;
710 coda_write(dev, ctx->params.gop_size,
711 CODA_CMD_ENC_PARAM_RC_GOP);
712 ctx->gopcounter = ctx->params.gop_size - 1;
713 ctx->params.gop_size_changed = false;
714 }
715 if (ctx->params.h264_intra_qp_changed) {
716 coda_dbg(1, ctx, "parameter change: intra Qp %u\n",
717 ctx->params.h264_intra_qp);
718
719 if (ctx->params.bitrate) {
720 change_enable |= CODA_PARAM_CHANGE_RC_INTRA_QP;
721 coda_write(dev, ctx->params.h264_intra_qp,
722 CODA_CMD_ENC_PARAM_RC_INTRA_QP);
723 }
724 ctx->params.h264_intra_qp_changed = false;
725 }
726 if (ctx->params.bitrate_changed) {
727 coda_dbg(1, ctx, "parameter change: bitrate %u kbit/s\n",
728 ctx->params.bitrate);
729 change_enable |= CODA_PARAM_CHANGE_RC_BITRATE;
730 coda_write(dev, ctx->params.bitrate,
731 CODA_CMD_ENC_PARAM_RC_BITRATE);
732 ctx->params.bitrate_changed = false;
733 }
734 if (ctx->params.framerate_changed) {
735 coda_dbg(1, ctx, "parameter change: frame rate %u/%u Hz\n",
736 ctx->params.framerate & 0xffff,
737 (ctx->params.framerate >> 16) + 1);
738 change_enable |= CODA_PARAM_CHANGE_RC_FRAME_RATE;
739 coda_write(dev, ctx->params.framerate,
740 CODA_CMD_ENC_PARAM_RC_FRAME_RATE);
741 ctx->params.framerate_changed = false;
742 }
743 if (ctx->params.intra_refresh_changed) {
744 coda_dbg(1, ctx, "parameter change: intra refresh MBs %u\n",
745 ctx->params.intra_refresh);
746 change_enable |= CODA_PARAM_CHANGE_INTRA_MB_NUM;
747 coda_write(dev, ctx->params.intra_refresh,
748 CODA_CMD_ENC_PARAM_INTRA_MB_NUM);
749 ctx->params.intra_refresh_changed = false;
750 }
751 if (ctx->params.slice_mode_changed) {
752 change_enable |= CODA_PARAM_CHANGE_SLICE_MODE;
753 coda_write(dev, coda_slice_mode(ctx),
754 CODA_CMD_ENC_PARAM_SLICE_MODE);
755 ctx->params.slice_mode_changed = false;
756 }
757
758 if (!change_enable)
759 return 0;
760
761 coda_write(dev, change_enable, CODA_CMD_ENC_PARAM_CHANGE_ENABLE);
762
763 ret = coda_command_sync(ctx, CODA_COMMAND_RC_CHANGE_PARAMETER);
764 if (ret < 0)
765 return ret;
766
767 success = coda_read(dev, CODA_RET_ENC_PARAM_CHANGE_SUCCESS);
768 if (success != 1)
769 coda_dbg(1, ctx, "parameter change failed: %u\n", success);
770
771 return 0;
772 }
773
coda_iram_alloc(struct coda_iram_info * iram,size_t size)774 static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
775 {
776 phys_addr_t ret;
777
778 size = round_up(size, 1024);
779 if (size > iram->remaining)
780 return 0;
781 iram->remaining -= size;
782
783 ret = iram->next_paddr;
784 iram->next_paddr += size;
785
786 return ret;
787 }
788
coda_setup_iram(struct coda_ctx * ctx)789 static void coda_setup_iram(struct coda_ctx *ctx)
790 {
791 struct coda_iram_info *iram_info = &ctx->iram_info;
792 struct coda_dev *dev = ctx->dev;
793 int w64, w128;
794 int mb_width;
795 int dbk_bits;
796 int bit_bits;
797 int ip_bits;
798 int me_bits;
799
800 memset(iram_info, 0, sizeof(*iram_info));
801 iram_info->next_paddr = dev->iram.paddr;
802 iram_info->remaining = dev->iram.size;
803
804 if (!dev->iram.vaddr)
805 return;
806
807 switch (dev->devtype->product) {
808 case CODA_HX4:
809 dbk_bits = CODA7_USE_HOST_DBK_ENABLE;
810 bit_bits = CODA7_USE_HOST_BIT_ENABLE;
811 ip_bits = CODA7_USE_HOST_IP_ENABLE;
812 me_bits = CODA7_USE_HOST_ME_ENABLE;
813 break;
814 case CODA_7541:
815 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
816 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
817 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
818 me_bits = CODA7_USE_HOST_ME_ENABLE | CODA7_USE_ME_ENABLE;
819 break;
820 case CODA_960:
821 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
822 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
823 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
824 me_bits = 0;
825 break;
826 default: /* CODA_DX6 */
827 return;
828 }
829
830 if (ctx->inst_type == CODA_INST_ENCODER) {
831 struct coda_q_data *q_data_src;
832
833 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
834 mb_width = DIV_ROUND_UP(q_data_src->rect.width, 16);
835 w128 = mb_width * 128;
836 w64 = mb_width * 64;
837
838 /* Prioritize in case IRAM is too small for everything */
839 if (dev->devtype->product == CODA_HX4 ||
840 dev->devtype->product == CODA_7541) {
841 iram_info->search_ram_size = round_up(mb_width * 16 *
842 36 + 2048, 1024);
843 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
844 iram_info->search_ram_size);
845 if (!iram_info->search_ram_paddr) {
846 pr_err("IRAM is smaller than the search ram size\n");
847 goto out;
848 }
849 iram_info->axi_sram_use |= me_bits;
850 }
851
852 /* Only H.264BP and H.263P3 are considered */
853 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64);
854 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64);
855 if (!iram_info->buf_dbk_y_use || !iram_info->buf_dbk_c_use)
856 goto out;
857 iram_info->axi_sram_use |= dbk_bits;
858
859 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
860 if (!iram_info->buf_bit_use)
861 goto out;
862 iram_info->axi_sram_use |= bit_bits;
863
864 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
865 if (!iram_info->buf_ip_ac_dc_use)
866 goto out;
867 iram_info->axi_sram_use |= ip_bits;
868
869 /* OVL and BTP disabled for encoder */
870 } else if (ctx->inst_type == CODA_INST_DECODER) {
871 struct coda_q_data *q_data_dst;
872
873 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
874 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
875 w128 = mb_width * 128;
876
877 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128);
878 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128);
879 if (!iram_info->buf_dbk_y_use || !iram_info->buf_dbk_c_use)
880 goto out;
881 iram_info->axi_sram_use |= dbk_bits;
882
883 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
884 if (!iram_info->buf_bit_use)
885 goto out;
886 iram_info->axi_sram_use |= bit_bits;
887
888 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
889 if (!iram_info->buf_ip_ac_dc_use)
890 goto out;
891 iram_info->axi_sram_use |= ip_bits;
892
893 /* OVL and BTP unused as there is no VC1 support yet */
894 }
895
896 out:
897 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
898 coda_dbg(1, ctx, "IRAM smaller than needed\n");
899
900 if (dev->devtype->product == CODA_HX4 ||
901 dev->devtype->product == CODA_7541) {
902 /* TODO - Enabling these causes picture errors on CODA7541 */
903 if (ctx->inst_type == CODA_INST_DECODER) {
904 /* fw 1.4.50 */
905 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
906 CODA7_USE_IP_ENABLE);
907 } else {
908 /* fw 13.4.29 */
909 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
910 CODA7_USE_HOST_DBK_ENABLE |
911 CODA7_USE_IP_ENABLE |
912 CODA7_USE_DBK_ENABLE);
913 }
914 }
915 }
916
917 static u32 coda_supported_firmwares[] = {
918 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
919 CODA_FIRMWARE_VERNUM(CODA_HX4, 1, 4, 50),
920 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
921 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
922 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 9),
923 CODA_FIRMWARE_VERNUM(CODA_960, 2, 3, 10),
924 CODA_FIRMWARE_VERNUM(CODA_960, 3, 1, 1),
925 };
926
coda_firmware_supported(u32 vernum)927 static bool coda_firmware_supported(u32 vernum)
928 {
929 int i;
930
931 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
932 if (vernum == coda_supported_firmwares[i])
933 return true;
934 return false;
935 }
936
coda_check_firmware(struct coda_dev * dev)937 int coda_check_firmware(struct coda_dev *dev)
938 {
939 u16 product, major, minor, release;
940 u32 data;
941 int ret;
942
943 ret = clk_prepare_enable(dev->clk_per);
944 if (ret)
945 goto err_clk_per;
946
947 ret = clk_prepare_enable(dev->clk_ahb);
948 if (ret)
949 goto err_clk_ahb;
950
951 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
952 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
953 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
954 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
955 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
956 if (coda_wait_timeout(dev)) {
957 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
958 ret = -EIO;
959 goto err_run_cmd;
960 }
961
962 if (dev->devtype->product == CODA_960) {
963 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
964 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
965 data);
966 }
967
968 /* Check we are compatible with the loaded firmware */
969 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
970 product = CODA_FIRMWARE_PRODUCT(data);
971 major = CODA_FIRMWARE_MAJOR(data);
972 minor = CODA_FIRMWARE_MINOR(data);
973 release = CODA_FIRMWARE_RELEASE(data);
974
975 clk_disable_unprepare(dev->clk_per);
976 clk_disable_unprepare(dev->clk_ahb);
977
978 if (product != dev->devtype->product) {
979 v4l2_err(&dev->v4l2_dev,
980 "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n",
981 coda_product_name(dev->devtype->product),
982 coda_product_name(product), major, minor, release);
983 return -EINVAL;
984 }
985
986 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
987 coda_product_name(product));
988
989 if (coda_firmware_supported(data)) {
990 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
991 major, minor, release);
992 } else {
993 v4l2_warn(&dev->v4l2_dev,
994 "Unsupported firmware version: %u.%u.%u\n",
995 major, minor, release);
996 }
997
998 return 0;
999
1000 err_run_cmd:
1001 clk_disable_unprepare(dev->clk_ahb);
1002 err_clk_ahb:
1003 clk_disable_unprepare(dev->clk_per);
1004 err_clk_per:
1005 return ret;
1006 }
1007
coda9_set_frame_cache(struct coda_ctx * ctx,u32 fourcc)1008 static void coda9_set_frame_cache(struct coda_ctx *ctx, u32 fourcc)
1009 {
1010 u32 cache_size, cache_config;
1011
1012 if (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1013 /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */
1014 cache_size = 0x20262024;
1015 cache_config = 2 << CODA9_CACHE_PAGEMERGE_OFFSET;
1016 } else {
1017 /* Luma 0x2 page, 4x4 cache, chroma 0x2 page, 4x3 cache size */
1018 cache_size = 0x02440243;
1019 cache_config = 1 << CODA9_CACHE_PAGEMERGE_OFFSET;
1020 }
1021 coda_write(ctx->dev, cache_size, CODA9_CMD_SET_FRAME_CACHE_SIZE);
1022 if (fourcc == V4L2_PIX_FMT_NV12 || fourcc == V4L2_PIX_FMT_YUYV) {
1023 cache_config |= 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
1024 16 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET |
1025 0 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET;
1026 } else {
1027 cache_config |= 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
1028 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET |
1029 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET;
1030 }
1031 coda_write(ctx->dev, cache_config, CODA9_CMD_SET_FRAME_CACHE_CONFIG);
1032 }
1033
1034 /*
1035 * Encoder context operations
1036 */
1037
coda_encoder_reqbufs(struct coda_ctx * ctx,struct v4l2_requestbuffers * rb)1038 static int coda_encoder_reqbufs(struct coda_ctx *ctx,
1039 struct v4l2_requestbuffers *rb)
1040 {
1041 struct coda_q_data *q_data_src;
1042 int ret;
1043
1044 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1045 return 0;
1046
1047 if (rb->count) {
1048 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1049 ret = coda_alloc_context_buffers(ctx, q_data_src);
1050 if (ret < 0)
1051 return ret;
1052 } else {
1053 coda_free_context_buffers(ctx);
1054 }
1055
1056 return 0;
1057 }
1058
coda_start_encoding(struct coda_ctx * ctx)1059 static int coda_start_encoding(struct coda_ctx *ctx)
1060 {
1061 struct coda_dev *dev = ctx->dev;
1062 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
1063 struct coda_q_data *q_data_src, *q_data_dst;
1064 u32 bitstream_buf, bitstream_size;
1065 struct vb2_v4l2_buffer *buf;
1066 int gamma, ret, value;
1067 u32 dst_fourcc;
1068 int num_fb;
1069 u32 stride;
1070
1071 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1072 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1073 dst_fourcc = q_data_dst->fourcc;
1074
1075 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1076 bitstream_buf = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
1077 bitstream_size = q_data_dst->sizeimage;
1078
1079 if (!coda_is_initialized(dev)) {
1080 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1081 return -EFAULT;
1082 }
1083
1084 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
1085 if (!ctx->params.jpeg_qmat_tab[0]) {
1086 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
1087 if (!ctx->params.jpeg_qmat_tab[0])
1088 return -ENOMEM;
1089 }
1090 if (!ctx->params.jpeg_qmat_tab[1]) {
1091 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
1092 if (!ctx->params.jpeg_qmat_tab[1])
1093 return -ENOMEM;
1094 }
1095 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
1096 }
1097
1098 mutex_lock(&dev->coda_mutex);
1099
1100 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1101 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1102 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1103 switch (dev->devtype->product) {
1104 case CODA_DX6:
1105 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1106 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1107 break;
1108 case CODA_960:
1109 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
1110 fallthrough;
1111 case CODA_HX4:
1112 case CODA_7541:
1113 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1114 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1115 break;
1116 }
1117
1118 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
1119 CODA9_FRAME_TILED2LINEAR);
1120 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12)
1121 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
1122 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
1123 ctx->frame_mem_ctrl |= (0x3 << 9) | CODA9_FRAME_TILED2LINEAR;
1124 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1125
1126 if (dev->devtype->product == CODA_DX6) {
1127 /* Configure the coda */
1128 coda_write(dev, dev->iram.paddr,
1129 CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1130 }
1131
1132 /* Could set rotation here if needed */
1133 value = 0;
1134 switch (dev->devtype->product) {
1135 case CODA_DX6:
1136 value = (q_data_src->rect.width & CODADX6_PICWIDTH_MASK)
1137 << CODADX6_PICWIDTH_OFFSET;
1138 value |= (q_data_src->rect.height & CODADX6_PICHEIGHT_MASK)
1139 << CODA_PICHEIGHT_OFFSET;
1140 break;
1141 case CODA_HX4:
1142 case CODA_7541:
1143 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1144 value = (round_up(q_data_src->rect.width, 16) &
1145 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
1146 value |= (round_up(q_data_src->rect.height, 16) &
1147 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
1148 break;
1149 }
1150 fallthrough;
1151 case CODA_960:
1152 value = (q_data_src->rect.width & CODA7_PICWIDTH_MASK)
1153 << CODA7_PICWIDTH_OFFSET;
1154 value |= (q_data_src->rect.height & CODA7_PICHEIGHT_MASK)
1155 << CODA_PICHEIGHT_OFFSET;
1156 }
1157 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1158 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
1159 ctx->params.framerate = 0;
1160 coda_write(dev, ctx->params.framerate,
1161 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1162
1163 ctx->params.codec_mode = ctx->codec->mode;
1164 switch (dst_fourcc) {
1165 case V4L2_PIX_FMT_MPEG4:
1166 if (dev->devtype->product == CODA_960)
1167 coda_write(dev, CODA9_STD_MPEG4,
1168 CODA_CMD_ENC_SEQ_COD_STD);
1169 else
1170 coda_write(dev, CODA_STD_MPEG4,
1171 CODA_CMD_ENC_SEQ_COD_STD);
1172 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1173 break;
1174 case V4L2_PIX_FMT_H264:
1175 if (dev->devtype->product == CODA_960)
1176 coda_write(dev, CODA9_STD_H264,
1177 CODA_CMD_ENC_SEQ_COD_STD);
1178 else
1179 coda_write(dev, CODA_STD_H264,
1180 CODA_CMD_ENC_SEQ_COD_STD);
1181 value = ((ctx->params.h264_disable_deblocking_filter_idc &
1182 CODA_264PARAM_DISABLEDEBLK_MASK) <<
1183 CODA_264PARAM_DISABLEDEBLK_OFFSET) |
1184 ((ctx->params.h264_slice_alpha_c0_offset_div2 &
1185 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
1186 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
1187 ((ctx->params.h264_slice_beta_offset_div2 &
1188 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
1189 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET) |
1190 (ctx->params.h264_constrained_intra_pred_flag <<
1191 CODA_264PARAM_CONSTRAINEDINTRAPREDFLAG_OFFSET) |
1192 (ctx->params.h264_chroma_qp_index_offset &
1193 CODA_264PARAM_CHROMAQPOFFSET_MASK);
1194 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
1195 break;
1196 case V4L2_PIX_FMT_JPEG:
1197 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
1198 coda_write(dev, ctx->params.jpeg_restart_interval,
1199 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
1200 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
1201 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
1202 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
1203
1204 coda_jpeg_write_tables(ctx);
1205 break;
1206 default:
1207 v4l2_err(v4l2_dev,
1208 "dst format (0x%08x) invalid.\n", dst_fourcc);
1209 ret = -EINVAL;
1210 goto out;
1211 }
1212
1213 /*
1214 * slice mode and GOP size registers are used for thumb size/offset
1215 * in JPEG mode
1216 */
1217 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
1218 value = coda_slice_mode(ctx);
1219 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
1220 value = ctx->params.gop_size;
1221 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1222 }
1223
1224 if (ctx->params.bitrate && (ctx->params.frame_rc_enable ||
1225 ctx->params.mb_rc_enable)) {
1226 ctx->params.bitrate_changed = false;
1227 ctx->params.h264_intra_qp_changed = false;
1228
1229 /* Rate control enabled */
1230 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK)
1231 << CODA_RATECONTROL_BITRATE_OFFSET;
1232 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1233 value |= (ctx->params.vbv_delay &
1234 CODA_RATECONTROL_INITIALDELAY_MASK)
1235 << CODA_RATECONTROL_INITIALDELAY_OFFSET;
1236 if (dev->devtype->product == CODA_960)
1237 value |= BIT(31); /* disable autoskip */
1238 } else {
1239 value = 0;
1240 }
1241 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1242
1243 coda_write(dev, ctx->params.vbv_size, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1244 coda_write(dev, ctx->params.intra_refresh,
1245 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1246
1247 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1248 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1249
1250
1251 value = 0;
1252 if (dev->devtype->product == CODA_960)
1253 gamma = CODA9_DEFAULT_GAMMA;
1254 else
1255 gamma = CODA_DEFAULT_GAMMA;
1256 if (gamma > 0) {
1257 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
1258 CODA_CMD_ENC_SEQ_RC_GAMMA);
1259 }
1260
1261 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
1262 coda_write(dev,
1263 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
1264 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
1265 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
1266 }
1267 if (dev->devtype->product == CODA_960) {
1268 if (ctx->params.h264_max_qp)
1269 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
1270 if (CODA_DEFAULT_GAMMA > 0)
1271 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
1272 } else {
1273 if (CODA_DEFAULT_GAMMA > 0) {
1274 if (dev->devtype->product == CODA_DX6)
1275 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
1276 else
1277 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
1278 }
1279 if (ctx->params.h264_min_qp)
1280 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
1281 if (ctx->params.h264_max_qp)
1282 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
1283 }
1284 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1285
1286 if (ctx->params.frame_rc_enable && !ctx->params.mb_rc_enable)
1287 value = 1;
1288 else
1289 value = 0;
1290 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
1291
1292 coda_setup_iram(ctx);
1293
1294 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1295 switch (dev->devtype->product) {
1296 case CODA_DX6:
1297 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
1298 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1299 break;
1300 case CODA_HX4:
1301 case CODA_7541:
1302 coda_write(dev, ctx->iram_info.search_ram_paddr,
1303 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1304 coda_write(dev, ctx->iram_info.search_ram_size,
1305 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1306 break;
1307 case CODA_960:
1308 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
1309 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
1310 }
1311 }
1312
1313 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1314 if (ret < 0) {
1315 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1316 goto out;
1317 }
1318
1319 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1320 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1321 ret = -EFAULT;
1322 goto out;
1323 }
1324 ctx->initialized = 1;
1325
1326 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
1327 if (dev->devtype->product == CODA_960)
1328 ctx->num_internal_frames = 4;
1329 else
1330 ctx->num_internal_frames = 2;
1331 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1332 if (ret < 0) {
1333 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1334 goto out;
1335 }
1336 num_fb = 2;
1337 stride = q_data_src->bytesperline;
1338 } else {
1339 ctx->num_internal_frames = 0;
1340 num_fb = 0;
1341 stride = 0;
1342 }
1343 coda_write(dev, num_fb, CODA_CMD_SET_FRAME_BUF_NUM);
1344 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
1345
1346 if (dev->devtype->product == CODA_HX4 ||
1347 dev->devtype->product == CODA_7541) {
1348 coda_write(dev, q_data_src->bytesperline,
1349 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1350 }
1351 if (dev->devtype->product != CODA_DX6) {
1352 coda_write(dev, ctx->iram_info.buf_bit_use,
1353 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1354 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1355 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1356 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1357 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1358 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1359 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1360 coda_write(dev, ctx->iram_info.buf_ovl_use,
1361 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1362 if (dev->devtype->product == CODA_960) {
1363 coda_write(dev, ctx->iram_info.buf_btp_use,
1364 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1365
1366 coda9_set_frame_cache(ctx, q_data_src->fourcc);
1367
1368 /* FIXME */
1369 coda_write(dev, ctx->internal_frames[2].buf.paddr,
1370 CODA9_CMD_SET_FRAME_SUBSAMP_A);
1371 coda_write(dev, ctx->internal_frames[3].buf.paddr,
1372 CODA9_CMD_SET_FRAME_SUBSAMP_B);
1373 }
1374 }
1375
1376 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1377 if (ret < 0) {
1378 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1379 goto out;
1380 }
1381
1382 coda_dbg(1, ctx, "start encoding %dx%d %4.4s->%4.4s @ %d/%d Hz\n",
1383 q_data_src->rect.width, q_data_src->rect.height,
1384 (char *)&ctx->codec->src_fourcc, (char *)&dst_fourcc,
1385 ctx->params.framerate & 0xffff,
1386 (ctx->params.framerate >> 16) + 1);
1387
1388 /* Save stream headers */
1389 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1390 switch (dst_fourcc) {
1391 case V4L2_PIX_FMT_H264:
1392 /*
1393 * Get SPS in the first frame and copy it to an
1394 * intermediate buffer.
1395 */
1396 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1397 &ctx->vpu_header[0][0],
1398 &ctx->vpu_header_size[0]);
1399 if (ret < 0)
1400 goto out;
1401
1402 /*
1403 * If visible width or height are not aligned to macroblock
1404 * size, the crop_right and crop_bottom SPS fields must be set
1405 * to the difference between visible and coded size. This is
1406 * only supported by CODA960 firmware. All others do not allow
1407 * writing frame cropping parameters, so we have to manually
1408 * fix up the SPS RBSP (Sequence Parameter Set Raw Byte
1409 * Sequence Payload) ourselves.
1410 */
1411 if (ctx->dev->devtype->product != CODA_960 &&
1412 ((q_data_src->rect.width % 16) ||
1413 (q_data_src->rect.height % 16))) {
1414 ret = coda_h264_sps_fixup(ctx, q_data_src->rect.width,
1415 q_data_src->rect.height,
1416 &ctx->vpu_header[0][0],
1417 &ctx->vpu_header_size[0],
1418 sizeof(ctx->vpu_header[0]));
1419 if (ret < 0)
1420 goto out;
1421 }
1422
1423 /*
1424 * Get PPS in the first frame and copy it to an
1425 * intermediate buffer.
1426 */
1427 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1428 &ctx->vpu_header[1][0],
1429 &ctx->vpu_header_size[1]);
1430 if (ret < 0)
1431 goto out;
1432
1433 /*
1434 * Length of H.264 headers is variable and thus it might not be
1435 * aligned for the coda to append the encoded frame. In that is
1436 * the case a filler NAL must be added to header 2.
1437 */
1438 ctx->vpu_header_size[2] = coda_h264_padding(
1439 (ctx->vpu_header_size[0] +
1440 ctx->vpu_header_size[1]),
1441 ctx->vpu_header[2]);
1442 break;
1443 case V4L2_PIX_FMT_MPEG4:
1444 /*
1445 * Get VOS in the first frame and copy it to an
1446 * intermediate buffer
1447 */
1448 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1449 &ctx->vpu_header[0][0],
1450 &ctx->vpu_header_size[0]);
1451 if (ret < 0)
1452 goto out;
1453
1454 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1455 &ctx->vpu_header[1][0],
1456 &ctx->vpu_header_size[1]);
1457 if (ret < 0)
1458 goto out;
1459
1460 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1461 &ctx->vpu_header[2][0],
1462 &ctx->vpu_header_size[2]);
1463 if (ret < 0)
1464 goto out;
1465 break;
1466 default:
1467 /* No more formats need to save headers at the moment */
1468 break;
1469 }
1470
1471 out:
1472 mutex_unlock(&dev->coda_mutex);
1473 return ret;
1474 }
1475
coda_prepare_encode(struct coda_ctx * ctx)1476 static int coda_prepare_encode(struct coda_ctx *ctx)
1477 {
1478 struct coda_q_data *q_data_src, *q_data_dst;
1479 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1480 struct coda_dev *dev = ctx->dev;
1481 int force_ipicture;
1482 int quant_param = 0;
1483 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1484 u32 rot_mode = 0;
1485 u32 dst_fourcc;
1486 u32 reg;
1487 int ret;
1488
1489 ret = coda_enc_param_change(ctx);
1490 if (ret < 0) {
1491 v4l2_warn(&ctx->dev->v4l2_dev, "parameter change failed: %d\n",
1492 ret);
1493 }
1494
1495 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1496 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1497 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1498 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1499 dst_fourcc = q_data_dst->fourcc;
1500
1501 src_buf->sequence = ctx->osequence;
1502 dst_buf->sequence = ctx->osequence;
1503 ctx->osequence++;
1504
1505 force_ipicture = ctx->params.force_ipicture;
1506 if (force_ipicture)
1507 ctx->params.force_ipicture = false;
1508 else if (ctx->params.gop_size != 0 &&
1509 (src_buf->sequence % ctx->params.gop_size) == 0)
1510 force_ipicture = 1;
1511
1512 /*
1513 * Workaround coda firmware BUG that only marks the first
1514 * frame as IDR. This is a problem for some decoders that can't
1515 * recover when a frame is lost.
1516 */
1517 if (!force_ipicture) {
1518 src_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1519 src_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1520 } else {
1521 src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1522 src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
1523 }
1524
1525 if (dev->devtype->product == CODA_960)
1526 coda_set_gdi_regs(ctx);
1527
1528 /*
1529 * Copy headers in front of the first frame and forced I frames for
1530 * H.264 only. In MPEG4 they are already copied by the CODA.
1531 */
1532 if (src_buf->sequence == 0 || force_ipicture) {
1533 pic_stream_buffer_addr =
1534 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0) +
1535 ctx->vpu_header_size[0] +
1536 ctx->vpu_header_size[1] +
1537 ctx->vpu_header_size[2];
1538 pic_stream_buffer_size = q_data_dst->sizeimage -
1539 ctx->vpu_header_size[0] -
1540 ctx->vpu_header_size[1] -
1541 ctx->vpu_header_size[2];
1542 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0),
1543 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1544 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0)
1545 + ctx->vpu_header_size[0], &ctx->vpu_header[1][0],
1546 ctx->vpu_header_size[1]);
1547 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0)
1548 + ctx->vpu_header_size[0] + ctx->vpu_header_size[1],
1549 &ctx->vpu_header[2][0], ctx->vpu_header_size[2]);
1550 } else {
1551 pic_stream_buffer_addr =
1552 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1553 pic_stream_buffer_size = q_data_dst->sizeimage;
1554 }
1555
1556 if (force_ipicture) {
1557 switch (dst_fourcc) {
1558 case V4L2_PIX_FMT_H264:
1559 quant_param = ctx->params.h264_intra_qp;
1560 break;
1561 case V4L2_PIX_FMT_MPEG4:
1562 quant_param = ctx->params.mpeg4_intra_qp;
1563 break;
1564 case V4L2_PIX_FMT_JPEG:
1565 quant_param = 30;
1566 break;
1567 default:
1568 v4l2_warn(&ctx->dev->v4l2_dev,
1569 "cannot set intra qp, fmt not supported\n");
1570 break;
1571 }
1572 } else {
1573 switch (dst_fourcc) {
1574 case V4L2_PIX_FMT_H264:
1575 quant_param = ctx->params.h264_inter_qp;
1576 break;
1577 case V4L2_PIX_FMT_MPEG4:
1578 quant_param = ctx->params.mpeg4_inter_qp;
1579 break;
1580 default:
1581 v4l2_warn(&ctx->dev->v4l2_dev,
1582 "cannot set inter qp, fmt not supported\n");
1583 break;
1584 }
1585 }
1586
1587 /* submit */
1588 if (ctx->params.rot_mode)
1589 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1590 coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
1591 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1592
1593 if (dev->devtype->product == CODA_960) {
1594 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1595 coda_write(dev, q_data_src->bytesperline,
1596 CODA9_CMD_ENC_PIC_SRC_STRIDE);
1597 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1598
1599 reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y;
1600 } else {
1601 reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y;
1602 }
1603 coda_write_base(ctx, q_data_src, src_buf, reg);
1604
1605 coda_write(dev, force_ipicture << 1 & 0x2,
1606 CODA_CMD_ENC_PIC_OPTION);
1607
1608 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1609 coda_write(dev, pic_stream_buffer_size / 1024,
1610 CODA_CMD_ENC_PIC_BB_SIZE);
1611
1612 if (!ctx->streamon_out) {
1613 /* After streamoff on the output side, set stream end flag */
1614 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1615 coda_write(dev, ctx->bit_stream_param,
1616 CODA_REG_BIT_BIT_STREAM_PARAM);
1617 }
1618
1619 if (dev->devtype->product != CODA_DX6)
1620 coda_write(dev, ctx->iram_info.axi_sram_use,
1621 CODA7_REG_BIT_AXI_SRAM_USE);
1622
1623 trace_coda_enc_pic_run(ctx, src_buf);
1624
1625 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1626
1627 return 0;
1628 }
1629
coda_frame_type_char(u32 flags)1630 static char coda_frame_type_char(u32 flags)
1631 {
1632 return (flags & V4L2_BUF_FLAG_KEYFRAME) ? 'I' :
1633 (flags & V4L2_BUF_FLAG_PFRAME) ? 'P' :
1634 (flags & V4L2_BUF_FLAG_BFRAME) ? 'B' : '?';
1635 }
1636
coda_finish_encode(struct coda_ctx * ctx)1637 static void coda_finish_encode(struct coda_ctx *ctx)
1638 {
1639 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1640 struct coda_dev *dev = ctx->dev;
1641 u32 wr_ptr, start_ptr;
1642
1643 if (ctx->aborting)
1644 return;
1645
1646 /*
1647 * Lock to make sure that an encoder stop command running in parallel
1648 * will either already have marked src_buf as last, or it will wake up
1649 * the capture queue after the buffers are returned.
1650 */
1651 mutex_lock(&ctx->wakeup_mutex);
1652 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1653 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1654
1655 trace_coda_enc_pic_done(ctx, dst_buf);
1656
1657 /* Get results from the coda */
1658 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1659 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1660
1661 /* Calculate bytesused field */
1662 if (dst_buf->sequence == 0 ||
1663 src_buf->flags & V4L2_BUF_FLAG_KEYFRAME) {
1664 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr +
1665 ctx->vpu_header_size[0] +
1666 ctx->vpu_header_size[1] +
1667 ctx->vpu_header_size[2]);
1668 } else {
1669 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr);
1670 }
1671
1672 coda_dbg(1, ctx, "frame size = %u\n", wr_ptr - start_ptr);
1673
1674 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1675 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1676
1677 dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
1678 V4L2_BUF_FLAG_PFRAME |
1679 V4L2_BUF_FLAG_LAST);
1680 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0)
1681 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1682 else
1683 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1684 dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
1685
1686 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
1687
1688 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1689
1690 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1691 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE);
1692 mutex_unlock(&ctx->wakeup_mutex);
1693
1694 ctx->gopcounter--;
1695 if (ctx->gopcounter < 0)
1696 ctx->gopcounter = ctx->params.gop_size - 1;
1697
1698 coda_dbg(1, ctx, "job finished: encoded %c frame (%d)%s\n",
1699 coda_frame_type_char(dst_buf->flags), dst_buf->sequence,
1700 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "");
1701 }
1702
coda_seq_end_work(struct work_struct * work)1703 static void coda_seq_end_work(struct work_struct *work)
1704 {
1705 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1706 struct coda_dev *dev = ctx->dev;
1707
1708 mutex_lock(&ctx->buffer_mutex);
1709 mutex_lock(&dev->coda_mutex);
1710
1711 if (ctx->initialized == 0)
1712 goto out;
1713
1714 coda_dbg(1, ctx, "%s: sent command 'SEQ_END' to coda\n", __func__);
1715 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1716 v4l2_err(&dev->v4l2_dev,
1717 "CODA_COMMAND_SEQ_END failed\n");
1718 }
1719
1720 /*
1721 * FIXME: Sometimes h.264 encoding fails with 8-byte sequences missing
1722 * from the output stream after the h.264 decoder has run. Resetting the
1723 * hardware after the decoder has finished seems to help.
1724 */
1725 if (dev->devtype->product == CODA_960)
1726 coda_hw_reset(ctx);
1727
1728 kfifo_init(&ctx->bitstream_fifo,
1729 ctx->bitstream.vaddr, ctx->bitstream.size);
1730
1731 coda_free_framebuffers(ctx);
1732
1733 ctx->initialized = 0;
1734
1735 out:
1736 mutex_unlock(&dev->coda_mutex);
1737 mutex_unlock(&ctx->buffer_mutex);
1738 }
1739
coda_bit_release(struct coda_ctx * ctx)1740 static void coda_bit_release(struct coda_ctx *ctx)
1741 {
1742 mutex_lock(&ctx->buffer_mutex);
1743 coda_free_framebuffers(ctx);
1744 coda_free_context_buffers(ctx);
1745 coda_free_bitstream_buffer(ctx);
1746 mutex_unlock(&ctx->buffer_mutex);
1747 }
1748
1749 const struct coda_context_ops coda_bit_encode_ops = {
1750 .queue_init = coda_encoder_queue_init,
1751 .reqbufs = coda_encoder_reqbufs,
1752 .start_streaming = coda_start_encoding,
1753 .prepare_run = coda_prepare_encode,
1754 .finish_run = coda_finish_encode,
1755 .seq_end_work = coda_seq_end_work,
1756 .release = coda_bit_release,
1757 };
1758
1759 /*
1760 * Decoder context operations
1761 */
1762
coda_alloc_bitstream_buffer(struct coda_ctx * ctx,struct coda_q_data * q_data)1763 static int coda_alloc_bitstream_buffer(struct coda_ctx *ctx,
1764 struct coda_q_data *q_data)
1765 {
1766 if (ctx->bitstream.vaddr)
1767 return 0;
1768
1769 ctx->bitstream.size = roundup_pow_of_two(q_data->sizeimage * 2);
1770 ctx->bitstream.vaddr = dma_alloc_wc(ctx->dev->dev, ctx->bitstream.size,
1771 &ctx->bitstream.paddr, GFP_KERNEL);
1772 if (!ctx->bitstream.vaddr) {
1773 v4l2_err(&ctx->dev->v4l2_dev,
1774 "failed to allocate bitstream ringbuffer");
1775 return -ENOMEM;
1776 }
1777 kfifo_init(&ctx->bitstream_fifo,
1778 ctx->bitstream.vaddr, ctx->bitstream.size);
1779
1780 return 0;
1781 }
1782
coda_free_bitstream_buffer(struct coda_ctx * ctx)1783 static void coda_free_bitstream_buffer(struct coda_ctx *ctx)
1784 {
1785 if (ctx->bitstream.vaddr == NULL)
1786 return;
1787
1788 dma_free_wc(ctx->dev->dev, ctx->bitstream.size, ctx->bitstream.vaddr,
1789 ctx->bitstream.paddr);
1790 ctx->bitstream.vaddr = NULL;
1791 kfifo_init(&ctx->bitstream_fifo, NULL, 0);
1792 }
1793
coda_decoder_reqbufs(struct coda_ctx * ctx,struct v4l2_requestbuffers * rb)1794 static int coda_decoder_reqbufs(struct coda_ctx *ctx,
1795 struct v4l2_requestbuffers *rb)
1796 {
1797 struct coda_q_data *q_data_src;
1798 int ret;
1799
1800 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1801 return 0;
1802
1803 if (rb->count) {
1804 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1805 ret = coda_alloc_context_buffers(ctx, q_data_src);
1806 if (ret < 0)
1807 return ret;
1808 ret = coda_alloc_bitstream_buffer(ctx, q_data_src);
1809 if (ret < 0) {
1810 coda_free_context_buffers(ctx);
1811 return ret;
1812 }
1813 } else {
1814 coda_free_bitstream_buffer(ctx);
1815 coda_free_context_buffers(ctx);
1816 }
1817
1818 return 0;
1819 }
1820
coda_reorder_enable(struct coda_ctx * ctx)1821 static bool coda_reorder_enable(struct coda_ctx *ctx)
1822 {
1823 struct coda_dev *dev = ctx->dev;
1824 int profile;
1825
1826 if (dev->devtype->product != CODA_HX4 &&
1827 dev->devtype->product != CODA_7541 &&
1828 dev->devtype->product != CODA_960)
1829 return false;
1830
1831 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1832 return false;
1833
1834 if (ctx->codec->src_fourcc != V4L2_PIX_FMT_H264)
1835 return true;
1836
1837 profile = coda_h264_profile(ctx->params.h264_profile_idc);
1838 if (profile < 0)
1839 v4l2_warn(&dev->v4l2_dev, "Unknown H264 Profile: %u\n",
1840 ctx->params.h264_profile_idc);
1841
1842 /* Baseline profile does not support reordering */
1843 return profile > V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
1844 }
1845
coda_decoder_drop_used_metas(struct coda_ctx * ctx)1846 static void coda_decoder_drop_used_metas(struct coda_ctx *ctx)
1847 {
1848 struct coda_buffer_meta *meta, *tmp;
1849
1850 /*
1851 * All metas that end at or before the RD pointer (fifo out),
1852 * are now consumed by the VPU and should be released.
1853 */
1854 spin_lock(&ctx->buffer_meta_lock);
1855 list_for_each_entry_safe(meta, tmp, &ctx->buffer_meta_list, list) {
1856 if (ctx->bitstream_fifo.kfifo.out >= meta->end) {
1857 coda_dbg(2, ctx, "releasing meta: seq=%d start=%d end=%d\n",
1858 meta->sequence, meta->start, meta->end);
1859
1860 list_del(&meta->list);
1861 ctx->num_metas--;
1862 ctx->first_frame_sequence++;
1863 kfree(meta);
1864 }
1865 }
1866 spin_unlock(&ctx->buffer_meta_lock);
1867 }
1868
__coda_decoder_seq_init(struct coda_ctx * ctx)1869 static int __coda_decoder_seq_init(struct coda_ctx *ctx)
1870 {
1871 struct coda_q_data *q_data_src, *q_data_dst;
1872 u32 bitstream_buf, bitstream_size;
1873 struct coda_dev *dev = ctx->dev;
1874 int width, height;
1875 u32 src_fourcc, dst_fourcc;
1876 u32 val;
1877 int ret;
1878
1879 lockdep_assert_held(&dev->coda_mutex);
1880
1881 coda_dbg(1, ctx, "Video Data Order Adapter: %s\n",
1882 ctx->use_vdoa ? "Enabled" : "Disabled");
1883
1884 /* Start decoding */
1885 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1886 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1887 bitstream_buf = ctx->bitstream.paddr;
1888 bitstream_size = ctx->bitstream.size;
1889 src_fourcc = q_data_src->fourcc;
1890 dst_fourcc = q_data_dst->fourcc;
1891
1892 /* Update coda bitstream read and write pointers from kfifo */
1893 coda_kfifo_sync_to_device_full(ctx);
1894
1895 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
1896 CODA9_FRAME_TILED2LINEAR);
1897 if (dst_fourcc == V4L2_PIX_FMT_NV12 || dst_fourcc == V4L2_PIX_FMT_YUYV)
1898 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
1899 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
1900 ctx->frame_mem_ctrl |= (0x3 << 9) |
1901 ((ctx->use_vdoa) ? 0 : CODA9_FRAME_TILED2LINEAR);
1902 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1903
1904 ctx->display_idx = -1;
1905 ctx->frm_dis_flg = 0;
1906 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1907
1908 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1909 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1910 val = 0;
1911 if (coda_reorder_enable(ctx))
1912 val |= CODA_REORDER_ENABLE;
1913 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1914 val |= CODA_NO_INT_ENABLE;
1915 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1916
1917 ctx->params.codec_mode = ctx->codec->mode;
1918 if (dev->devtype->product == CODA_960 &&
1919 src_fourcc == V4L2_PIX_FMT_MPEG4)
1920 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1921 else
1922 ctx->params.codec_mode_aux = 0;
1923 if (src_fourcc == V4L2_PIX_FMT_MPEG4) {
1924 coda_write(dev, CODA_MP4_CLASS_MPEG4,
1925 CODA_CMD_DEC_SEQ_MP4_ASP_CLASS);
1926 }
1927 if (src_fourcc == V4L2_PIX_FMT_H264) {
1928 if (dev->devtype->product == CODA_HX4 ||
1929 dev->devtype->product == CODA_7541) {
1930 coda_write(dev, ctx->psbuf.paddr,
1931 CODA_CMD_DEC_SEQ_PS_BB_START);
1932 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1933 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1934 }
1935 if (dev->devtype->product == CODA_960) {
1936 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1937 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1938 }
1939 }
1940 if (src_fourcc == V4L2_PIX_FMT_JPEG)
1941 coda_write(dev, 0, CODA_CMD_DEC_SEQ_JPG_THUMB_EN);
1942 if (dev->devtype->product != CODA_960)
1943 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
1944
1945 ctx->bit_stream_param = CODA_BIT_DEC_SEQ_INIT_ESCAPE;
1946 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1947 ctx->bit_stream_param = 0;
1948 if (ret) {
1949 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1950 return ret;
1951 }
1952 ctx->sequence_offset = ~0U;
1953 ctx->initialized = 1;
1954 ctx->first_frame_sequence = 0;
1955
1956 /* Update kfifo out pointer from coda bitstream read pointer */
1957 coda_kfifo_sync_from_device(ctx);
1958
1959 /*
1960 * After updating the read pointer, we need to check if
1961 * any metas are consumed and should be released.
1962 */
1963 coda_decoder_drop_used_metas(ctx);
1964
1965 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1966 v4l2_err(&dev->v4l2_dev,
1967 "CODA_COMMAND_SEQ_INIT failed, error code = 0x%x\n",
1968 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1969 return -EAGAIN;
1970 }
1971
1972 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1973 if (dev->devtype->product == CODA_DX6) {
1974 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1975 height = val & CODADX6_PICHEIGHT_MASK;
1976 } else {
1977 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1978 height = val & CODA7_PICHEIGHT_MASK;
1979 }
1980
1981 if (width > q_data_dst->bytesperline || height > q_data_dst->height) {
1982 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1983 width, height, q_data_dst->bytesperline,
1984 q_data_dst->height);
1985 return -EINVAL;
1986 }
1987
1988 width = round_up(width, 16);
1989 height = round_up(height, 16);
1990
1991 coda_dbg(1, ctx, "start decoding: %dx%d\n", width, height);
1992
1993 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
1994 /*
1995 * If the VDOA is used, the decoder needs one additional frame,
1996 * because the frames are freed when the next frame is decoded.
1997 * Otherwise there are visible errors in the decoded frames (green
1998 * regions in displayed frames) and a broken order of frames (earlier
1999 * frames are sporadically displayed after later frames).
2000 */
2001 if (ctx->use_vdoa)
2002 ctx->num_internal_frames += 1;
2003 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2004 v4l2_err(&dev->v4l2_dev,
2005 "not enough framebuffers to decode (%d < %d)\n",
2006 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2007 return -EINVAL;
2008 }
2009
2010 if (src_fourcc == V4L2_PIX_FMT_H264) {
2011 u32 left_right;
2012 u32 top_bottom;
2013
2014 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2015 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2016
2017 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2018 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2019 q_data_dst->rect.width = width - q_data_dst->rect.left -
2020 (left_right & 0x3ff);
2021 q_data_dst->rect.height = height - q_data_dst->rect.top -
2022 (top_bottom & 0x3ff);
2023 }
2024
2025 if (dev->devtype->product != CODA_DX6) {
2026 u8 profile, level;
2027
2028 val = coda_read(dev, CODA7_RET_DEC_SEQ_HEADER_REPORT);
2029 profile = val & 0xff;
2030 level = (val >> 8) & 0x7f;
2031
2032 if (profile || level)
2033 coda_update_profile_level_ctrls(ctx, profile, level);
2034 }
2035
2036 return 0;
2037 }
2038
coda_dec_seq_init_work(struct work_struct * work)2039 static void coda_dec_seq_init_work(struct work_struct *work)
2040 {
2041 struct coda_ctx *ctx = container_of(work,
2042 struct coda_ctx, seq_init_work);
2043 struct coda_dev *dev = ctx->dev;
2044
2045 mutex_lock(&ctx->buffer_mutex);
2046 mutex_lock(&dev->coda_mutex);
2047
2048 if (!ctx->initialized)
2049 __coda_decoder_seq_init(ctx);
2050
2051 mutex_unlock(&dev->coda_mutex);
2052 mutex_unlock(&ctx->buffer_mutex);
2053 }
2054
__coda_start_decoding(struct coda_ctx * ctx)2055 static int __coda_start_decoding(struct coda_ctx *ctx)
2056 {
2057 struct coda_q_data *q_data_src, *q_data_dst;
2058 struct coda_dev *dev = ctx->dev;
2059 u32 src_fourcc, dst_fourcc;
2060 int ret;
2061
2062 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2063 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2064 src_fourcc = q_data_src->fourcc;
2065 dst_fourcc = q_data_dst->fourcc;
2066
2067 if (!ctx->initialized) {
2068 ret = __coda_decoder_seq_init(ctx);
2069 if (ret < 0)
2070 return ret;
2071 } else {
2072 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
2073 CODA9_FRAME_TILED2LINEAR);
2074 if (dst_fourcc == V4L2_PIX_FMT_NV12 || dst_fourcc == V4L2_PIX_FMT_YUYV)
2075 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
2076 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
2077 ctx->frame_mem_ctrl |= (0x3 << 9) |
2078 ((ctx->use_vdoa) ? 0 : CODA9_FRAME_TILED2LINEAR);
2079 }
2080
2081 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2082
2083 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2084 if (ret < 0) {
2085 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n");
2086 return ret;
2087 }
2088
2089 /* Tell the decoder how many frame buffers we allocated. */
2090 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2091 coda_write(dev, round_up(q_data_dst->rect.width, 16),
2092 CODA_CMD_SET_FRAME_BUF_STRIDE);
2093
2094 if (dev->devtype->product != CODA_DX6) {
2095 /* Set secondary AXI IRAM */
2096 coda_setup_iram(ctx);
2097
2098 coda_write(dev, ctx->iram_info.buf_bit_use,
2099 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2100 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2101 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2102 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2103 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2104 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2105 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2106 coda_write(dev, ctx->iram_info.buf_ovl_use,
2107 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
2108 if (dev->devtype->product == CODA_960) {
2109 coda_write(dev, ctx->iram_info.buf_btp_use,
2110 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2111
2112 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2113 coda9_set_frame_cache(ctx, dst_fourcc);
2114 }
2115 }
2116
2117 if (src_fourcc == V4L2_PIX_FMT_H264) {
2118 coda_write(dev, ctx->slicebuf.paddr,
2119 CODA_CMD_SET_FRAME_SLICE_BB_START);
2120 coda_write(dev, ctx->slicebuf.size / 1024,
2121 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2122 }
2123
2124 if (dev->devtype->product == CODA_HX4 ||
2125 dev->devtype->product == CODA_7541) {
2126 int max_mb_x = 1920 / 16;
2127 int max_mb_y = 1088 / 16;
2128 int max_mb_num = max_mb_x * max_mb_y;
2129
2130 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2131 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
2132 } else if (dev->devtype->product == CODA_960) {
2133 int max_mb_x = 1920 / 16;
2134 int max_mb_y = 1088 / 16;
2135 int max_mb_num = max_mb_x * max_mb_y;
2136
2137 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2138 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
2139 }
2140
2141 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2142 v4l2_err(&ctx->dev->v4l2_dev,
2143 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2144 return -ETIMEDOUT;
2145 }
2146
2147 return 0;
2148 }
2149
coda_start_decoding(struct coda_ctx * ctx)2150 static int coda_start_decoding(struct coda_ctx *ctx)
2151 {
2152 struct coda_dev *dev = ctx->dev;
2153 int ret;
2154
2155 mutex_lock(&dev->coda_mutex);
2156 ret = __coda_start_decoding(ctx);
2157 mutex_unlock(&dev->coda_mutex);
2158
2159 return ret;
2160 }
2161
coda_prepare_decode(struct coda_ctx * ctx)2162 static int coda_prepare_decode(struct coda_ctx *ctx)
2163 {
2164 struct vb2_v4l2_buffer *dst_buf;
2165 struct coda_dev *dev = ctx->dev;
2166 struct coda_q_data *q_data_dst;
2167 struct coda_buffer_meta *meta;
2168 u32 rot_mode = 0;
2169 u32 reg_addr, reg_stride;
2170
2171 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
2172 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2173
2174 /* Try to copy source buffer contents into the bitstream ringbuffer */
2175 mutex_lock(&ctx->bitstream_mutex);
2176 coda_fill_bitstream(ctx, NULL);
2177 mutex_unlock(&ctx->bitstream_mutex);
2178
2179 if (coda_get_bitstream_payload(ctx) < 512 &&
2180 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
2181 coda_dbg(1, ctx, "bitstream payload: %d, skipping\n",
2182 coda_get_bitstream_payload(ctx));
2183 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
2184 return -EAGAIN;
2185 }
2186
2187 /* Run coda_start_decoding (again) if not yet initialized */
2188 if (!ctx->initialized) {
2189 int ret = __coda_start_decoding(ctx);
2190
2191 if (ret < 0) {
2192 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
2193 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
2194 return -EAGAIN;
2195 } else {
2196 ctx->initialized = 1;
2197 }
2198 }
2199
2200 if (dev->devtype->product == CODA_960)
2201 coda_set_gdi_regs(ctx);
2202
2203 if (ctx->use_vdoa &&
2204 ctx->display_idx >= 0 &&
2205 ctx->display_idx < ctx->num_internal_frames) {
2206 vdoa_device_run(ctx->vdoa,
2207 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0),
2208 ctx->internal_frames[ctx->display_idx].buf.paddr);
2209 } else {
2210 if (dev->devtype->product == CODA_960) {
2211 /*
2212 * It was previously assumed that the CODA960 has an
2213 * internal list of 64 buffer entries that contains
2214 * both the registered internal frame buffers as well
2215 * as the rotator buffer output, and that the ROT_INDEX
2216 * register must be set to a value between the last
2217 * internal frame buffers' index and 64.
2218 * At least on firmware version 3.1.1 it turns out that
2219 * setting ROT_INDEX to any value >= 32 causes CODA
2220 * hangups that it can not recover from with the SRC VPU
2221 * reset.
2222 * It does appear to work however, to just set it to a
2223 * fixed value in the [ctx->num_internal_frames, 31]
2224 * range, for example CODA_MAX_FRAMEBUFFERS.
2225 */
2226 coda_write(dev, CODA_MAX_FRAMEBUFFERS,
2227 CODA9_CMD_DEC_PIC_ROT_INDEX);
2228
2229 reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y;
2230 reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE;
2231 } else {
2232 reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y;
2233 reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE;
2234 }
2235 coda_write_base(ctx, q_data_dst, dst_buf, reg_addr);
2236 coda_write(dev, q_data_dst->bytesperline, reg_stride);
2237
2238 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
2239 }
2240
2241 coda_write(dev, rot_mode, CODA_CMD_DEC_PIC_ROT_MODE);
2242
2243 switch (dev->devtype->product) {
2244 case CODA_DX6:
2245 /* TBD */
2246 case CODA_HX4:
2247 case CODA_7541:
2248 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
2249 break;
2250 case CODA_960:
2251 /* 'hardcode to use interrupt disable mode'? */
2252 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION);
2253 break;
2254 }
2255
2256 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
2257
2258 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
2259 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
2260
2261 if (dev->devtype->product != CODA_DX6)
2262 coda_write(dev, ctx->iram_info.axi_sram_use,
2263 CODA7_REG_BIT_AXI_SRAM_USE);
2264
2265 spin_lock(&ctx->buffer_meta_lock);
2266 meta = list_first_entry_or_null(&ctx->buffer_meta_list,
2267 struct coda_buffer_meta, list);
2268
2269 if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) {
2270
2271 /* If this is the last buffer in the bitstream, add padding */
2272 if (meta->end == ctx->bitstream_fifo.kfifo.in) {
2273 static unsigned char buf[512];
2274 unsigned int pad;
2275
2276 /* Pad to multiple of 256 and then add 256 more */
2277 pad = ((0 - meta->end) & 0xff) + 256;
2278
2279 memset(buf, 0xff, sizeof(buf));
2280
2281 kfifo_in(&ctx->bitstream_fifo, buf, pad);
2282 }
2283 }
2284 spin_unlock(&ctx->buffer_meta_lock);
2285
2286 coda_kfifo_sync_to_device_full(ctx);
2287
2288 /* Clear decode success flag */
2289 coda_write(dev, 0, CODA_RET_DEC_PIC_SUCCESS);
2290
2291 /* Clear error return value */
2292 coda_write(dev, 0, CODA_RET_DEC_PIC_ERR_MB);
2293
2294 trace_coda_dec_pic_run(ctx, meta);
2295
2296 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
2297
2298 return 0;
2299 }
2300
coda_finish_decode(struct coda_ctx * ctx)2301 static void coda_finish_decode(struct coda_ctx *ctx)
2302 {
2303 struct coda_dev *dev = ctx->dev;
2304 struct coda_q_data *q_data_src;
2305 struct coda_q_data *q_data_dst;
2306 struct vb2_v4l2_buffer *dst_buf;
2307 struct coda_buffer_meta *meta;
2308 int width, height;
2309 int decoded_idx;
2310 int display_idx;
2311 struct coda_internal_frame *decoded_frame = NULL;
2312 u32 src_fourcc;
2313 int success;
2314 u32 err_mb;
2315 int err_vdoa = 0;
2316 u32 val;
2317
2318 if (ctx->aborting)
2319 return;
2320
2321 /* Update kfifo out pointer from coda bitstream read pointer */
2322 coda_kfifo_sync_from_device(ctx);
2323
2324 /*
2325 * in stream-end mode, the read pointer can overshoot the write pointer
2326 * by up to 512 bytes
2327 */
2328 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2329 if (coda_get_bitstream_payload(ctx) >= ctx->bitstream.size - 512)
2330 kfifo_init(&ctx->bitstream_fifo,
2331 ctx->bitstream.vaddr, ctx->bitstream.size);
2332 }
2333
2334 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2335 src_fourcc = q_data_src->fourcc;
2336
2337 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2338 if (val != 1)
2339 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2340
2341 success = val & 0x1;
2342 if (!success)
2343 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2344
2345 if (src_fourcc == V4L2_PIX_FMT_H264) {
2346 if (val & (1 << 3))
2347 v4l2_err(&dev->v4l2_dev,
2348 "insufficient PS buffer space (%d bytes)\n",
2349 ctx->psbuf.size);
2350 if (val & (1 << 2))
2351 v4l2_err(&dev->v4l2_dev,
2352 "insufficient slice buffer space (%d bytes)\n",
2353 ctx->slicebuf.size);
2354 }
2355
2356 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2357 width = (val >> 16) & 0xffff;
2358 height = val & 0xffff;
2359
2360 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2361
2362 /* frame crop information */
2363 if (src_fourcc == V4L2_PIX_FMT_H264) {
2364 u32 left_right;
2365 u32 top_bottom;
2366
2367 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
2368 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
2369
2370 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
2371 /* Keep current crop information */
2372 } else {
2373 struct v4l2_rect *rect = &q_data_dst->rect;
2374
2375 rect->left = left_right >> 16 & 0xffff;
2376 rect->top = top_bottom >> 16 & 0xffff;
2377 rect->width = width - rect->left -
2378 (left_right & 0xffff);
2379 rect->height = height - rect->top -
2380 (top_bottom & 0xffff);
2381 }
2382 } else {
2383 /* no cropping */
2384 }
2385
2386 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2387 if (err_mb > 0) {
2388 if (__ratelimit(&dev->mb_err_rs))
2389 coda_dbg(1, ctx, "errors in %d macroblocks\n", err_mb);
2390 v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl,
2391 v4l2_ctrl_g_ctrl(ctx->mb_err_cnt_ctrl) + err_mb);
2392 }
2393
2394 if (dev->devtype->product == CODA_HX4 ||
2395 dev->devtype->product == CODA_7541) {
2396 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2397 if (val == 0) {
2398 /* not enough bitstream data */
2399 coda_dbg(1, ctx, "prescan failed: %d\n", val);
2400 ctx->hold = true;
2401 return;
2402 }
2403 }
2404
2405 /* Wait until the VDOA finished writing the previous display frame */
2406 if (ctx->use_vdoa &&
2407 ctx->display_idx >= 0 &&
2408 ctx->display_idx < ctx->num_internal_frames) {
2409 err_vdoa = vdoa_wait_for_completion(ctx->vdoa);
2410 }
2411
2412 ctx->frm_dis_flg = coda_read(dev,
2413 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2414
2415 /* The previous display frame was copied out and can be overwritten */
2416 if (ctx->display_idx >= 0 &&
2417 ctx->display_idx < ctx->num_internal_frames) {
2418 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
2419 coda_write(dev, ctx->frm_dis_flg,
2420 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2421 }
2422
2423 /*
2424 * The index of the last decoded frame, not necessarily in
2425 * display order, and the index of the next display frame.
2426 * The latter could have been decoded in a previous run.
2427 */
2428 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
2429 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
2430
2431 if (decoded_idx == -1) {
2432 /* no frame was decoded, but we might have a display frame */
2433 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
2434 ctx->sequence_offset++;
2435 else if (ctx->display_idx < 0)
2436 ctx->hold = true;
2437 } else if (decoded_idx == -2) {
2438 if (ctx->display_idx >= 0 &&
2439 ctx->display_idx < ctx->num_internal_frames)
2440 ctx->sequence_offset++;
2441 /* no frame was decoded, we still return remaining buffers */
2442 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
2443 v4l2_err(&dev->v4l2_dev,
2444 "decoded frame index out of range: %d\n", decoded_idx);
2445 } else {
2446 int sequence;
2447
2448 decoded_frame = &ctx->internal_frames[decoded_idx];
2449
2450 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM);
2451 if (ctx->sequence_offset == -1)
2452 ctx->sequence_offset = val;
2453
2454 sequence = val + ctx->first_frame_sequence
2455 - ctx->sequence_offset;
2456 spin_lock(&ctx->buffer_meta_lock);
2457 if (!list_empty(&ctx->buffer_meta_list)) {
2458 meta = list_first_entry(&ctx->buffer_meta_list,
2459 struct coda_buffer_meta, list);
2460 list_del(&meta->list);
2461 ctx->num_metas--;
2462 spin_unlock(&ctx->buffer_meta_lock);
2463 /*
2464 * Clamp counters to 16 bits for comparison, as the HW
2465 * counter rolls over at this point for h.264. This
2466 * may be different for other formats, but using 16 bits
2467 * should be enough to detect most errors and saves us
2468 * from doing different things based on the format.
2469 */
2470 if ((sequence & 0xffff) != (meta->sequence & 0xffff)) {
2471 v4l2_err(&dev->v4l2_dev,
2472 "sequence number mismatch (%d(%d) != %d)\n",
2473 sequence, ctx->sequence_offset,
2474 meta->sequence);
2475 }
2476 decoded_frame->meta = *meta;
2477 kfree(meta);
2478 } else {
2479 spin_unlock(&ctx->buffer_meta_lock);
2480 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n");
2481 memset(&decoded_frame->meta, 0,
2482 sizeof(struct coda_buffer_meta));
2483 decoded_frame->meta.sequence = sequence;
2484 decoded_frame->meta.last = false;
2485 ctx->sequence_offset++;
2486 }
2487
2488 trace_coda_dec_pic_done(ctx, &decoded_frame->meta);
2489
2490 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
2491 decoded_frame->type = (val == 0) ? V4L2_BUF_FLAG_KEYFRAME :
2492 (val == 1) ? V4L2_BUF_FLAG_PFRAME :
2493 V4L2_BUF_FLAG_BFRAME;
2494
2495 decoded_frame->error = err_mb;
2496 }
2497
2498 if (display_idx == -1) {
2499 /*
2500 * no more frames to be decoded, but there could still
2501 * be rotator output to dequeue
2502 */
2503 ctx->hold = true;
2504 } else if (display_idx == -3) {
2505 /* possibly prescan failure */
2506 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
2507 v4l2_err(&dev->v4l2_dev,
2508 "presentation frame index out of range: %d\n",
2509 display_idx);
2510 }
2511
2512 /* If a frame was copied out, return it */
2513 if (ctx->display_idx >= 0 &&
2514 ctx->display_idx < ctx->num_internal_frames) {
2515 struct coda_internal_frame *ready_frame;
2516
2517 ready_frame = &ctx->internal_frames[ctx->display_idx];
2518
2519 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
2520 dst_buf->sequence = ctx->osequence++;
2521
2522 dst_buf->field = V4L2_FIELD_NONE;
2523 dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
2524 V4L2_BUF_FLAG_PFRAME |
2525 V4L2_BUF_FLAG_BFRAME);
2526 dst_buf->flags |= ready_frame->type;
2527 meta = &ready_frame->meta;
2528 if (meta->last && !coda_reorder_enable(ctx)) {
2529 /*
2530 * If this was the last decoded frame, and reordering
2531 * is disabled, this will be the last display frame.
2532 */
2533 coda_dbg(1, ctx, "last meta, marking as last frame\n");
2534 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
2535 } else if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG &&
2536 display_idx == -1) {
2537 /*
2538 * If there is no designated presentation frame anymore,
2539 * this frame has to be the last one.
2540 */
2541 coda_dbg(1, ctx,
2542 "no more frames to return, marking as last frame\n");
2543 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
2544 }
2545 dst_buf->timecode = meta->timecode;
2546 dst_buf->vb2_buf.timestamp = meta->timestamp;
2547
2548 trace_coda_dec_rot_done(ctx, dst_buf, meta);
2549
2550 vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
2551 q_data_dst->sizeimage);
2552
2553 if (ready_frame->error || err_vdoa)
2554 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_ERROR);
2555 else
2556 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE);
2557
2558 if (decoded_frame) {
2559 coda_dbg(1, ctx, "job finished: decoded %c frame %u, returned %c frame %u (%u/%u)%s\n",
2560 coda_frame_type_char(decoded_frame->type),
2561 decoded_frame->meta.sequence,
2562 coda_frame_type_char(dst_buf->flags),
2563 ready_frame->meta.sequence,
2564 dst_buf->sequence, ctx->qsequence,
2565 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ?
2566 " (last)" : "");
2567 } else {
2568 coda_dbg(1, ctx, "job finished: no frame decoded (%d), returned %c frame %u (%u/%u)%s\n",
2569 decoded_idx,
2570 coda_frame_type_char(dst_buf->flags),
2571 ready_frame->meta.sequence,
2572 dst_buf->sequence, ctx->qsequence,
2573 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ?
2574 " (last)" : "");
2575 }
2576 } else {
2577 if (decoded_frame) {
2578 coda_dbg(1, ctx, "job finished: decoded %c frame %u, no frame returned (%d)\n",
2579 coda_frame_type_char(decoded_frame->type),
2580 decoded_frame->meta.sequence,
2581 ctx->display_idx);
2582 } else {
2583 coda_dbg(1, ctx, "job finished: no frame decoded (%d) or returned (%d)\n",
2584 decoded_idx, ctx->display_idx);
2585 }
2586 }
2587
2588 /* The rotator will copy the current display frame next time */
2589 ctx->display_idx = display_idx;
2590
2591 /*
2592 * The current decode run might have brought the bitstream fill level
2593 * below the size where we can start the next decode run. As userspace
2594 * might have filled the output queue completely and might thus be
2595 * blocked, we can't rely on the next qbuf to trigger the bitstream
2596 * refill. Check if we have data to refill the bitstream now.
2597 */
2598 mutex_lock(&ctx->bitstream_mutex);
2599 coda_fill_bitstream(ctx, NULL);
2600 mutex_unlock(&ctx->bitstream_mutex);
2601 }
2602
coda_decode_timeout(struct coda_ctx * ctx)2603 static void coda_decode_timeout(struct coda_ctx *ctx)
2604 {
2605 struct vb2_v4l2_buffer *dst_buf;
2606
2607 /*
2608 * For now this only handles the case where we would deadlock with
2609 * userspace, i.e. userspace issued DEC_CMD_STOP and waits for EOS,
2610 * but after a failed decode run we would hold the context and wait for
2611 * userspace to queue more buffers.
2612 */
2613 if (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))
2614 return;
2615
2616 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
2617 dst_buf->sequence = ctx->qsequence - 1;
2618
2619 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_ERROR);
2620 }
2621
2622 const struct coda_context_ops coda_bit_decode_ops = {
2623 .queue_init = coda_decoder_queue_init,
2624 .reqbufs = coda_decoder_reqbufs,
2625 .start_streaming = coda_start_decoding,
2626 .prepare_run = coda_prepare_decode,
2627 .finish_run = coda_finish_decode,
2628 .run_timeout = coda_decode_timeout,
2629 .seq_init_work = coda_dec_seq_init_work,
2630 .seq_end_work = coda_seq_end_work,
2631 .release = coda_bit_release,
2632 };
2633
coda_irq_handler(int irq,void * data)2634 irqreturn_t coda_irq_handler(int irq, void *data)
2635 {
2636 struct coda_dev *dev = data;
2637 struct coda_ctx *ctx;
2638
2639 /* read status register to attend the IRQ */
2640 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2641 coda_write(dev, 0, CODA_REG_BIT_INT_REASON);
2642 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2643 CODA_REG_BIT_INT_CLEAR);
2644
2645 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2646 if (ctx == NULL) {
2647 v4l2_err(&dev->v4l2_dev,
2648 "Instance released before the end of transaction\n");
2649 return IRQ_HANDLED;
2650 }
2651
2652 trace_coda_bit_done(ctx);
2653
2654 if (ctx->aborting) {
2655 coda_dbg(1, ctx, "task has been aborted\n");
2656 }
2657
2658 if (coda_isbusy(ctx->dev)) {
2659 coda_dbg(1, ctx, "coda is still busy!!!!\n");
2660 return IRQ_NONE;
2661 }
2662
2663 complete(&ctx->completion);
2664
2665 return IRQ_HANDLED;
2666 }
2667