2 * Coda multi-standard codec IP - BIT processor functions
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
7 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/clk.h>
16 #include <linux/irqreturn.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/reset.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
29 #include <media/videobuf2-vmalloc.h>
33 #define CODA7_PS_BUF_SIZE 0x28000
34 #define CODA9_PS_SAVE_SIZE (512 * 1024)
36 #define CODA_DEFAULT_GAMMA 4096
37 #define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
39 static inline int coda_is_initialized(struct coda_dev *dev)
41 return coda_read(dev, CODA_REG_BIT_CUR_PC) != 0;
44 static inline unsigned long coda_isbusy(struct coda_dev *dev)
46 return coda_read(dev, CODA_REG_BIT_BUSY);
49 static int coda_wait_timeout(struct coda_dev *dev)
51 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
53 while (coda_isbusy(dev)) {
54 if (time_after(jiffies, timeout))
60 static void coda_command_async(struct coda_ctx *ctx, int cmd)
62 struct coda_dev *dev = ctx->dev;
64 if (dev->devtype->product == CODA_960 ||
65 dev->devtype->product == CODA_7541) {
66 /* Restore context related registers to CODA */
67 coda_write(dev, ctx->bit_stream_param,
68 CODA_REG_BIT_BIT_STREAM_PARAM);
69 coda_write(dev, ctx->frm_dis_flg,
70 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
71 coda_write(dev, ctx->frame_mem_ctrl,
72 CODA_REG_BIT_FRAME_MEM_CTRL);
73 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
76 if (dev->devtype->product == CODA_960) {
77 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
78 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
81 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
83 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
84 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
85 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
87 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
90 static int coda_command_sync(struct coda_ctx *ctx, int cmd)
92 struct coda_dev *dev = ctx->dev;
94 coda_command_async(ctx, cmd);
95 return coda_wait_timeout(dev);
98 int coda_hw_reset(struct coda_ctx *ctx)
100 struct coda_dev *dev = ctx->dev;
101 unsigned long timeout;
108 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
110 if (dev->devtype->product == CODA_960) {
111 timeout = jiffies + msecs_to_jiffies(100);
112 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
113 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
114 if (time_after(jiffies, timeout))
120 ret = reset_control_reset(dev->rstc);
124 if (dev->devtype->product == CODA_960)
125 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
126 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
127 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
128 ret = coda_wait_timeout(dev);
129 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
134 static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
136 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
137 struct coda_dev *dev = ctx->dev;
140 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
141 kfifo->out = (kfifo->in & ~kfifo->mask) |
142 (rd_ptr - ctx->bitstream.paddr);
143 if (kfifo->out > kfifo->in)
144 kfifo->out -= kfifo->mask + 1;
147 static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
149 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
150 struct coda_dev *dev = ctx->dev;
153 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
154 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
155 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
156 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
159 static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
161 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
162 struct coda_dev *dev = ctx->dev;
165 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
166 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
169 static int coda_bitstream_queue(struct coda_ctx *ctx,
170 struct vb2_buffer *src_buf)
172 u32 src_size = vb2_get_plane_payload(src_buf, 0);
175 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0),
180 dma_sync_single_for_device(&ctx->dev->plat_dev->dev,
181 ctx->bitstream.paddr, ctx->bitstream.size,
184 src_buf->v4l2_buf.sequence = ctx->qsequence++;
189 static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
190 struct vb2_buffer *src_buf)
194 if (coda_get_bitstream_payload(ctx) +
195 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
198 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
199 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
203 ret = coda_bitstream_queue(ctx, src_buf);
205 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
208 /* Sync read pointer to device */
209 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
210 coda_kfifo_sync_to_device_write(ctx);
217 void coda_fill_bitstream(struct coda_ctx *ctx)
219 struct vb2_buffer *src_buf;
220 struct coda_buffer_meta *meta;
223 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
225 * Only queue a single JPEG into the bitstream buffer, except
226 * to increase payload over 512 bytes or if in hold state.
228 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
229 (coda_get_bitstream_payload(ctx) >= 512) && !ctx->hold)
232 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
234 /* Drop frames that do not start/end with a SOI/EOI markers */
235 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
236 !coda_jpeg_check_buffer(ctx, src_buf)) {
237 v4l2_err(&ctx->dev->v4l2_dev,
238 "dropping invalid JPEG frame\n");
239 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
240 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
244 /* Buffer start position */
245 start = ctx->bitstream_fifo.kfifo.in &
246 ctx->bitstream_fifo.kfifo.mask;
248 if (coda_bitstream_try_queue(ctx, src_buf)) {
250 * Source buffer is queued in the bitstream ringbuffer;
251 * queue the timestamp and mark source buffer as done
253 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
255 meta = kmalloc(sizeof(*meta), GFP_KERNEL);
257 meta->sequence = src_buf->v4l2_buf.sequence;
258 meta->timecode = src_buf->v4l2_buf.timecode;
259 meta->timestamp = src_buf->v4l2_buf.timestamp;
261 meta->end = ctx->bitstream_fifo.kfifo.in &
262 ctx->bitstream_fifo.kfifo.mask;
263 list_add_tail(&meta->list,
264 &ctx->buffer_meta_list);
267 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
274 void coda_bit_stream_end_flag(struct coda_ctx *ctx)
276 struct coda_dev *dev = ctx->dev;
278 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
280 /* If this context is currently running, update the hardware flag */
281 if ((dev->devtype->product == CODA_960) &&
283 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
284 coda_write(dev, ctx->bit_stream_param,
285 CODA_REG_BIT_BIT_STREAM_PARAM);
289 static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
291 struct coda_dev *dev = ctx->dev;
292 u32 *p = ctx->parabuf.vaddr;
294 if (dev->devtype->product == CODA_DX6)
297 p[index ^ 1] = value;
300 static void coda_free_framebuffers(struct coda_ctx *ctx)
304 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
305 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
308 static int coda_alloc_framebuffers(struct coda_ctx *ctx,
309 struct coda_q_data *q_data, u32 fourcc)
311 struct coda_dev *dev = ctx->dev;
318 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
319 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
320 width = round_up(q_data->width, 16);
321 height = round_up(q_data->height, 16);
323 width = round_up(q_data->width, 8);
324 height = q_data->height;
326 ysize = width * height;
328 /* Allocate frame buffers */
329 for (i = 0; i < ctx->num_internal_frames; i++) {
333 size = ysize + ysize / 2;
334 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
335 dev->devtype->product != CODA_DX6)
337 name = kasprintf(GFP_KERNEL, "fb%d", i);
338 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
342 coda_free_framebuffers(ctx);
347 /* Register frame buffers in the parameter buffer */
348 for (i = 0; i < ctx->num_internal_frames; i++) {
349 paddr = ctx->internal_frames[i].paddr;
350 /* Start addresses of Y, Cb, Cr planes */
351 coda_parabuf_write(ctx, i * 3 + 0, paddr);
352 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize);
353 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize / 4);
355 /* mvcol buffer for h.264 */
356 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
357 dev->devtype->product != CODA_DX6)
358 coda_parabuf_write(ctx, 96 + i,
359 ctx->internal_frames[i].paddr +
360 ysize + ysize/4 + ysize/4);
363 /* mvcol buffer for mpeg4 */
364 if ((dev->devtype->product != CODA_DX6) &&
365 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
366 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
367 ysize + ysize/4 + ysize/4);
372 static void coda_free_context_buffers(struct coda_ctx *ctx)
374 struct coda_dev *dev = ctx->dev;
376 coda_free_aux_buf(dev, &ctx->slicebuf);
377 coda_free_aux_buf(dev, &ctx->psbuf);
378 if (dev->devtype->product != CODA_DX6)
379 coda_free_aux_buf(dev, &ctx->workbuf);
382 static int coda_alloc_context_buffers(struct coda_ctx *ctx,
383 struct coda_q_data *q_data)
385 struct coda_dev *dev = ctx->dev;
389 if (dev->devtype->product == CODA_DX6)
392 if (ctx->psbuf.vaddr) {
393 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
396 if (ctx->slicebuf.vaddr) {
397 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
400 if (ctx->workbuf.vaddr) {
401 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
406 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
407 /* worst case slice size */
408 size = (DIV_ROUND_UP(q_data->width, 16) *
409 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
410 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size,
413 v4l2_err(&dev->v4l2_dev,
414 "failed to allocate %d byte slice buffer",
420 if (dev->devtype->product == CODA_7541) {
421 ret = coda_alloc_context_buf(ctx, &ctx->psbuf,
422 CODA7_PS_BUF_SIZE, "psbuf");
424 v4l2_err(&dev->v4l2_dev,
425 "failed to allocate psmem buffer");
430 size = dev->devtype->workbuf_size;
431 if (dev->devtype->product == CODA_960 &&
432 q_data->fourcc == V4L2_PIX_FMT_H264)
433 size += CODA9_PS_SAVE_SIZE;
434 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
436 v4l2_err(&dev->v4l2_dev,
437 "failed to allocate %d byte context buffer",
445 coda_free_context_buffers(ctx);
449 static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
450 int header_code, u8 *header, int *size)
452 struct coda_dev *dev = ctx->dev;
457 if (dev->devtype->product == CODA_960)
458 memset(vb2_plane_vaddr(buf, 0), 0, 64);
460 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
461 CODA_CMD_ENC_HEADER_BB_START);
462 bufsize = vb2_plane_size(buf, 0);
463 if (dev->devtype->product == CODA_960)
465 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
466 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
467 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
469 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
473 if (dev->devtype->product == CODA_960) {
474 for (i = 63; i > 0; i--)
475 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
479 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
480 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
482 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
487 static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
491 size = round_up(size, 1024);
492 if (size > iram->remaining)
494 iram->remaining -= size;
496 ret = iram->next_paddr;
497 iram->next_paddr += size;
502 static void coda_setup_iram(struct coda_ctx *ctx)
504 struct coda_iram_info *iram_info = &ctx->iram_info;
505 struct coda_dev *dev = ctx->dev;
512 memset(iram_info, 0, sizeof(*iram_info));
513 iram_info->next_paddr = dev->iram.paddr;
514 iram_info->remaining = dev->iram.size;
516 if (!dev->iram.vaddr)
519 switch (dev->devtype->product) {
521 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
522 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
523 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
526 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
527 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
528 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
530 default: /* CODA_DX6 */
534 if (ctx->inst_type == CODA_INST_ENCODER) {
535 struct coda_q_data *q_data_src;
537 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
538 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
539 w128 = mb_width * 128;
542 /* Prioritize in case IRAM is too small for everything */
543 if (dev->devtype->product == CODA_7541) {
544 iram_info->search_ram_size = round_up(mb_width * 16 *
546 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
547 iram_info->search_ram_size);
548 if (!iram_info->search_ram_paddr) {
549 pr_err("IRAM is smaller than the search ram size\n");
552 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
556 /* Only H.264BP and H.263P3 are considered */
557 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64);
558 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64);
559 if (!iram_info->buf_dbk_c_use)
561 iram_info->axi_sram_use |= dbk_bits;
563 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
564 if (!iram_info->buf_bit_use)
566 iram_info->axi_sram_use |= bit_bits;
568 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
569 if (!iram_info->buf_ip_ac_dc_use)
571 iram_info->axi_sram_use |= ip_bits;
573 /* OVL and BTP disabled for encoder */
574 } else if (ctx->inst_type == CODA_INST_DECODER) {
575 struct coda_q_data *q_data_dst;
577 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
578 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
579 w128 = mb_width * 128;
581 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128);
582 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128);
583 if (!iram_info->buf_dbk_c_use)
585 iram_info->axi_sram_use |= dbk_bits;
587 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
588 if (!iram_info->buf_bit_use)
590 iram_info->axi_sram_use |= bit_bits;
592 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
593 if (!iram_info->buf_ip_ac_dc_use)
595 iram_info->axi_sram_use |= ip_bits;
597 /* OVL and BTP unused as there is no VC1 support yet */
601 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
602 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
603 "IRAM smaller than needed\n");
605 if (dev->devtype->product == CODA_7541) {
606 /* TODO - Enabling these causes picture errors on CODA7541 */
607 if (ctx->inst_type == CODA_INST_DECODER) {
609 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
610 CODA7_USE_IP_ENABLE);
613 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
614 CODA7_USE_HOST_DBK_ENABLE |
615 CODA7_USE_IP_ENABLE |
616 CODA7_USE_DBK_ENABLE);
621 static u32 coda_supported_firmwares[] = {
622 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
623 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
624 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
627 static bool coda_firmware_supported(u32 vernum)
631 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
632 if (vernum == coda_supported_firmwares[i])
637 int coda_check_firmware(struct coda_dev *dev)
639 u16 product, major, minor, release;
643 ret = clk_prepare_enable(dev->clk_per);
647 ret = clk_prepare_enable(dev->clk_ahb);
651 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
652 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
653 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
654 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
655 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
656 if (coda_wait_timeout(dev)) {
657 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
662 if (dev->devtype->product == CODA_960) {
663 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
664 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
668 /* Check we are compatible with the loaded firmware */
669 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
670 product = CODA_FIRMWARE_PRODUCT(data);
671 major = CODA_FIRMWARE_MAJOR(data);
672 minor = CODA_FIRMWARE_MINOR(data);
673 release = CODA_FIRMWARE_RELEASE(data);
675 clk_disable_unprepare(dev->clk_per);
676 clk_disable_unprepare(dev->clk_ahb);
678 if (product != dev->devtype->product) {
679 v4l2_err(&dev->v4l2_dev,
680 "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n",
681 coda_product_name(dev->devtype->product),
682 coda_product_name(product), major, minor, release);
686 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
687 coda_product_name(product));
689 if (coda_firmware_supported(data)) {
690 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
691 major, minor, release);
693 v4l2_warn(&dev->v4l2_dev,
694 "Unsupported firmware version: %u.%u.%u\n",
695 major, minor, release);
701 clk_disable_unprepare(dev->clk_ahb);
703 clk_disable_unprepare(dev->clk_per);
709 * Encoder context operations
712 static int coda_start_encoding(struct coda_ctx *ctx)
714 struct coda_dev *dev = ctx->dev;
715 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
716 struct coda_q_data *q_data_src, *q_data_dst;
717 u32 bitstream_buf, bitstream_size;
718 struct vb2_buffer *buf;
719 int gamma, ret, value;
724 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
725 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
726 dst_fourcc = q_data_dst->fourcc;
728 /* Allocate per-instance buffers */
729 ret = coda_alloc_context_buffers(ctx, q_data_src);
733 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
734 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
735 bitstream_size = q_data_dst->sizeimage;
737 if (!coda_is_initialized(dev)) {
738 v4l2_err(v4l2_dev, "coda is not initialized.\n");
742 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
743 if (!ctx->params.jpeg_qmat_tab[0])
744 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
745 if (!ctx->params.jpeg_qmat_tab[1])
746 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
747 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
750 mutex_lock(&dev->coda_mutex);
752 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
753 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
754 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
755 switch (dev->devtype->product) {
757 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
758 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
761 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
764 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
765 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
769 ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE;
770 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12)
771 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
772 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
774 if (dev->devtype->product == CODA_DX6) {
775 /* Configure the coda */
776 coda_write(dev, dev->iram.paddr,
777 CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
780 /* Could set rotation here if needed */
782 switch (dev->devtype->product) {
784 value = (q_data_src->width & CODADX6_PICWIDTH_MASK)
785 << CODADX6_PICWIDTH_OFFSET;
786 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK)
787 << CODA_PICHEIGHT_OFFSET;
790 if (dst_fourcc == V4L2_PIX_FMT_H264) {
791 value = (round_up(q_data_src->width, 16) &
792 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
793 value |= (round_up(q_data_src->height, 16) &
794 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
799 value = (q_data_src->width & CODA7_PICWIDTH_MASK)
800 << CODA7_PICWIDTH_OFFSET;
801 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK)
802 << CODA_PICHEIGHT_OFFSET;
804 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
805 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
806 ctx->params.framerate = 0;
807 coda_write(dev, ctx->params.framerate,
808 CODA_CMD_ENC_SEQ_SRC_F_RATE);
810 ctx->params.codec_mode = ctx->codec->mode;
811 switch (dst_fourcc) {
812 case V4L2_PIX_FMT_MPEG4:
813 if (dev->devtype->product == CODA_960)
814 coda_write(dev, CODA9_STD_MPEG4,
815 CODA_CMD_ENC_SEQ_COD_STD);
817 coda_write(dev, CODA_STD_MPEG4,
818 CODA_CMD_ENC_SEQ_COD_STD);
819 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
821 case V4L2_PIX_FMT_H264:
822 if (dev->devtype->product == CODA_960)
823 coda_write(dev, CODA9_STD_H264,
824 CODA_CMD_ENC_SEQ_COD_STD);
826 coda_write(dev, CODA_STD_H264,
827 CODA_CMD_ENC_SEQ_COD_STD);
828 if (ctx->params.h264_deblk_enabled) {
829 value = ((ctx->params.h264_deblk_alpha &
830 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
831 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
832 ((ctx->params.h264_deblk_beta &
833 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
834 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
836 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
838 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
840 case V4L2_PIX_FMT_JPEG:
841 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
842 coda_write(dev, ctx->params.jpeg_restart_interval,
843 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
844 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
845 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
846 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
848 coda_jpeg_write_tables(ctx);
852 "dst format (0x%08x) invalid.\n", dst_fourcc);
858 * slice mode and GOP size registers are used for thumb size/offset
861 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
862 switch (ctx->params.slice_mode) {
863 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
866 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
867 value = (ctx->params.slice_max_mb &
868 CODA_SLICING_SIZE_MASK)
869 << CODA_SLICING_SIZE_OFFSET;
870 value |= (1 & CODA_SLICING_UNIT_MASK)
871 << CODA_SLICING_UNIT_OFFSET;
872 value |= 1 & CODA_SLICING_MODE_MASK;
874 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
875 value = (ctx->params.slice_max_bits &
876 CODA_SLICING_SIZE_MASK)
877 << CODA_SLICING_SIZE_OFFSET;
878 value |= (0 & CODA_SLICING_UNIT_MASK)
879 << CODA_SLICING_UNIT_OFFSET;
880 value |= 1 & CODA_SLICING_MODE_MASK;
883 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
884 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
885 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
888 if (ctx->params.bitrate) {
889 /* Rate control enabled */
890 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK)
891 << CODA_RATECONTROL_BITRATE_OFFSET;
892 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
893 if (dev->devtype->product == CODA_960)
894 value |= BIT(31); /* disable autoskip */
898 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
900 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
901 coda_write(dev, ctx->params.intra_refresh,
902 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
904 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
905 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
909 if (dev->devtype->product == CODA_960)
910 gamma = CODA9_DEFAULT_GAMMA;
912 gamma = CODA_DEFAULT_GAMMA;
914 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
915 CODA_CMD_ENC_SEQ_RC_GAMMA);
918 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
920 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
921 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
922 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
924 if (dev->devtype->product == CODA_960) {
925 if (ctx->params.h264_max_qp)
926 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
927 if (CODA_DEFAULT_GAMMA > 0)
928 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
930 if (CODA_DEFAULT_GAMMA > 0) {
931 if (dev->devtype->product == CODA_DX6)
932 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
934 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
936 if (ctx->params.h264_min_qp)
937 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
938 if (ctx->params.h264_max_qp)
939 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
941 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
943 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
945 coda_setup_iram(ctx);
947 if (dst_fourcc == V4L2_PIX_FMT_H264) {
948 switch (dev->devtype->product) {
950 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
951 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
954 coda_write(dev, ctx->iram_info.search_ram_paddr,
955 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
956 coda_write(dev, ctx->iram_info.search_ram_size,
957 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
960 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
961 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
965 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
967 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
971 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
972 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
977 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
978 if (dev->devtype->product == CODA_960)
979 ctx->num_internal_frames = 4;
981 ctx->num_internal_frames = 2;
982 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
984 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
988 stride = q_data_src->bytesperline;
990 ctx->num_internal_frames = 0;
994 coda_write(dev, num_fb, CODA_CMD_SET_FRAME_BUF_NUM);
995 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
997 if (dev->devtype->product == CODA_7541) {
998 coda_write(dev, q_data_src->bytesperline,
999 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1001 if (dev->devtype->product != CODA_DX6) {
1002 coda_write(dev, ctx->iram_info.buf_bit_use,
1003 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1004 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1005 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1006 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1007 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1008 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1009 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1010 coda_write(dev, ctx->iram_info.buf_ovl_use,
1011 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1012 if (dev->devtype->product == CODA_960) {
1013 coda_write(dev, ctx->iram_info.buf_btp_use,
1014 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1017 coda_write(dev, ctx->internal_frames[2].paddr,
1018 CODA9_CMD_SET_FRAME_SUBSAMP_A);
1019 coda_write(dev, ctx->internal_frames[3].paddr,
1020 CODA9_CMD_SET_FRAME_SUBSAMP_B);
1024 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1026 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1030 /* Save stream headers */
1031 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1032 switch (dst_fourcc) {
1033 case V4L2_PIX_FMT_H264:
1035 * Get SPS in the first frame and copy it to an
1036 * intermediate buffer.
1038 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1039 &ctx->vpu_header[0][0],
1040 &ctx->vpu_header_size[0]);
1045 * Get PPS in the first frame and copy it to an
1046 * intermediate buffer.
1048 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1049 &ctx->vpu_header[1][0],
1050 &ctx->vpu_header_size[1]);
1055 * Length of H.264 headers is variable and thus it might not be
1056 * aligned for the coda to append the encoded frame. In that is
1057 * the case a filler NAL must be added to header 2.
1059 ctx->vpu_header_size[2] = coda_h264_padding(
1060 (ctx->vpu_header_size[0] +
1061 ctx->vpu_header_size[1]),
1062 ctx->vpu_header[2]);
1064 case V4L2_PIX_FMT_MPEG4:
1066 * Get VOS in the first frame and copy it to an
1067 * intermediate buffer
1069 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1070 &ctx->vpu_header[0][0],
1071 &ctx->vpu_header_size[0]);
1075 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1076 &ctx->vpu_header[1][0],
1077 &ctx->vpu_header_size[1]);
1081 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1082 &ctx->vpu_header[2][0],
1083 &ctx->vpu_header_size[2]);
1088 /* No more formats need to save headers at the moment */
1093 mutex_unlock(&dev->coda_mutex);
1097 static int coda_prepare_encode(struct coda_ctx *ctx)
1099 struct coda_q_data *q_data_src, *q_data_dst;
1100 struct vb2_buffer *src_buf, *dst_buf;
1101 struct coda_dev *dev = ctx->dev;
1103 int quant_param = 0;
1104 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1109 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1110 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1111 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1112 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1113 dst_fourcc = q_data_dst->fourcc;
1115 src_buf->v4l2_buf.sequence = ctx->osequence;
1116 dst_buf->v4l2_buf.sequence = ctx->osequence;
1120 * Workaround coda firmware BUG that only marks the first
1121 * frame as IDR. This is a problem for some decoders that can't
1122 * recover when a frame is lost.
1124 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1125 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1126 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1128 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1129 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1132 if (dev->devtype->product == CODA_960)
1133 coda_set_gdi_regs(ctx);
1136 * Copy headers at the beginning of the first frame for H.264 only.
1137 * In MPEG4 they are already copied by the coda.
1139 if (src_buf->v4l2_buf.sequence == 0) {
1140 pic_stream_buffer_addr =
1141 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1142 ctx->vpu_header_size[0] +
1143 ctx->vpu_header_size[1] +
1144 ctx->vpu_header_size[2];
1145 pic_stream_buffer_size = q_data_dst->sizeimage -
1146 ctx->vpu_header_size[0] -
1147 ctx->vpu_header_size[1] -
1148 ctx->vpu_header_size[2];
1149 memcpy(vb2_plane_vaddr(dst_buf, 0),
1150 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1151 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1152 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1153 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1154 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1155 ctx->vpu_header_size[2]);
1157 pic_stream_buffer_addr =
1158 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1159 pic_stream_buffer_size = q_data_dst->sizeimage;
1162 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1164 switch (dst_fourcc) {
1165 case V4L2_PIX_FMT_H264:
1166 quant_param = ctx->params.h264_intra_qp;
1168 case V4L2_PIX_FMT_MPEG4:
1169 quant_param = ctx->params.mpeg4_intra_qp;
1171 case V4L2_PIX_FMT_JPEG:
1175 v4l2_warn(&ctx->dev->v4l2_dev,
1176 "cannot set intra qp, fmt not supported\n");
1181 switch (dst_fourcc) {
1182 case V4L2_PIX_FMT_H264:
1183 quant_param = ctx->params.h264_inter_qp;
1185 case V4L2_PIX_FMT_MPEG4:
1186 quant_param = ctx->params.mpeg4_inter_qp;
1189 v4l2_warn(&ctx->dev->v4l2_dev,
1190 "cannot set inter qp, fmt not supported\n");
1196 if (ctx->params.rot_mode)
1197 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1198 coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
1199 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1201 if (dev->devtype->product == CODA_960) {
1202 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1203 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1204 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1206 reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y;
1208 reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y;
1210 coda_write_base(ctx, q_data_src, src_buf, reg);
1212 coda_write(dev, force_ipicture << 1 & 0x2,
1213 CODA_CMD_ENC_PIC_OPTION);
1215 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1216 coda_write(dev, pic_stream_buffer_size / 1024,
1217 CODA_CMD_ENC_PIC_BB_SIZE);
1219 if (!ctx->streamon_out) {
1220 /* After streamoff on the output side, set stream end flag */
1221 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1222 coda_write(dev, ctx->bit_stream_param,
1223 CODA_REG_BIT_BIT_STREAM_PARAM);
1226 if (dev->devtype->product != CODA_DX6)
1227 coda_write(dev, ctx->iram_info.axi_sram_use,
1228 CODA7_REG_BIT_AXI_SRAM_USE);
1230 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1235 static void coda_finish_encode(struct coda_ctx *ctx)
1237 struct vb2_buffer *src_buf, *dst_buf;
1238 struct coda_dev *dev = ctx->dev;
1239 u32 wr_ptr, start_ptr;
1241 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1242 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1244 /* Get results from the coda */
1245 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1246 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1248 /* Calculate bytesused field */
1249 if (dst_buf->v4l2_buf.sequence == 0) {
1250 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
1251 ctx->vpu_header_size[0] +
1252 ctx->vpu_header_size[1] +
1253 ctx->vpu_header_size[2]);
1255 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
1258 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1259 wr_ptr - start_ptr);
1261 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1262 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1264 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
1265 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1266 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1268 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1269 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1272 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1273 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1274 dst_buf->v4l2_buf.flags |=
1275 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1276 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1278 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1280 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1281 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1284 if (ctx->gopcounter < 0)
1285 ctx->gopcounter = ctx->params.gop_size - 1;
1287 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1288 "job finished: encoding frame (%d) (%s)\n",
1289 dst_buf->v4l2_buf.sequence,
1290 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1291 "KEYFRAME" : "PFRAME");
1294 static void coda_seq_end_work(struct work_struct *work)
1296 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1297 struct coda_dev *dev = ctx->dev;
1299 mutex_lock(&ctx->buffer_mutex);
1300 mutex_lock(&dev->coda_mutex);
1302 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1303 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx,
1305 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1306 v4l2_err(&dev->v4l2_dev,
1307 "CODA_COMMAND_SEQ_END failed\n");
1310 kfifo_init(&ctx->bitstream_fifo,
1311 ctx->bitstream.vaddr, ctx->bitstream.size);
1313 coda_free_framebuffers(ctx);
1314 coda_free_context_buffers(ctx);
1316 mutex_unlock(&dev->coda_mutex);
1317 mutex_unlock(&ctx->buffer_mutex);
1320 static void coda_bit_release(struct coda_ctx *ctx)
1322 mutex_lock(&ctx->buffer_mutex);
1323 coda_free_framebuffers(ctx);
1324 coda_free_context_buffers(ctx);
1325 mutex_unlock(&ctx->buffer_mutex);
1328 const struct coda_context_ops coda_bit_encode_ops = {
1329 .queue_init = coda_encoder_queue_init,
1330 .start_streaming = coda_start_encoding,
1331 .prepare_run = coda_prepare_encode,
1332 .finish_run = coda_finish_encode,
1333 .seq_end_work = coda_seq_end_work,
1334 .release = coda_bit_release,
1338 * Decoder context operations
1341 static int __coda_start_decoding(struct coda_ctx *ctx)
1343 struct coda_q_data *q_data_src, *q_data_dst;
1344 u32 bitstream_buf, bitstream_size;
1345 struct coda_dev *dev = ctx->dev;
1347 u32 src_fourcc, dst_fourcc;
1351 /* Start decoding */
1352 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1353 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1354 bitstream_buf = ctx->bitstream.paddr;
1355 bitstream_size = ctx->bitstream.size;
1356 src_fourcc = q_data_src->fourcc;
1357 dst_fourcc = q_data_dst->fourcc;
1359 /* Allocate per-instance buffers */
1360 ret = coda_alloc_context_buffers(ctx, q_data_src);
1364 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1366 /* Update coda bitstream read and write pointers from kfifo */
1367 coda_kfifo_sync_to_device_full(ctx);
1369 ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE;
1370 if (dst_fourcc == V4L2_PIX_FMT_NV12)
1371 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
1372 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1374 ctx->display_idx = -1;
1375 ctx->frm_dis_flg = 0;
1376 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1378 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1379 CODA_REG_BIT_BIT_STREAM_PARAM);
1381 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1382 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1384 if ((dev->devtype->product == CODA_7541) ||
1385 (dev->devtype->product == CODA_960))
1386 val |= CODA_REORDER_ENABLE;
1387 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1388 val |= CODA_NO_INT_ENABLE;
1389 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1391 ctx->params.codec_mode = ctx->codec->mode;
1392 if (dev->devtype->product == CODA_960 &&
1393 src_fourcc == V4L2_PIX_FMT_MPEG4)
1394 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1396 ctx->params.codec_mode_aux = 0;
1397 if (src_fourcc == V4L2_PIX_FMT_H264) {
1398 if (dev->devtype->product == CODA_7541) {
1399 coda_write(dev, ctx->psbuf.paddr,
1400 CODA_CMD_DEC_SEQ_PS_BB_START);
1401 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1402 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1404 if (dev->devtype->product == CODA_960) {
1405 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1406 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1409 if (dev->devtype->product != CODA_960)
1410 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
1412 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1413 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1414 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1418 /* Update kfifo out pointer from coda bitstream read pointer */
1419 coda_kfifo_sync_from_device(ctx);
1421 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1423 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1424 v4l2_err(&dev->v4l2_dev,
1425 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1426 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1430 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1431 if (dev->devtype->product == CODA_DX6) {
1432 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1433 height = val & CODADX6_PICHEIGHT_MASK;
1435 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1436 height = val & CODA7_PICHEIGHT_MASK;
1439 if (width > q_data_dst->bytesperline || height > q_data_dst->height) {
1440 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1441 width, height, q_data_dst->bytesperline,
1442 q_data_dst->height);
1446 width = round_up(width, 16);
1447 height = round_up(height, 16);
1449 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1450 __func__, ctx->idx, width, height);
1452 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
1453 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1454 v4l2_err(&dev->v4l2_dev,
1455 "not enough framebuffers to decode (%d < %d)\n",
1456 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1460 if (src_fourcc == V4L2_PIX_FMT_H264) {
1464 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1465 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1467 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1468 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1469 q_data_dst->rect.width = width - q_data_dst->rect.left -
1470 (left_right & 0x3ff);
1471 q_data_dst->rect.height = height - q_data_dst->rect.top -
1472 (top_bottom & 0x3ff);
1475 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1477 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n");
1481 /* Tell the decoder how many frame buffers we allocated. */
1482 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1483 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1485 if (dev->devtype->product != CODA_DX6) {
1486 /* Set secondary AXI IRAM */
1487 coda_setup_iram(ctx);
1489 coda_write(dev, ctx->iram_info.buf_bit_use,
1490 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1491 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1492 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1493 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1494 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1495 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1496 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1497 coda_write(dev, ctx->iram_info.buf_ovl_use,
1498 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1499 if (dev->devtype->product == CODA_960)
1500 coda_write(dev, ctx->iram_info.buf_btp_use,
1501 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1504 if (dev->devtype->product == CODA_960) {
1505 int cbb_size, crb_size;
1507 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
1508 /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */
1509 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
1511 if (dst_fourcc == V4L2_PIX_FMT_NV12) {
1518 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
1519 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
1520 cbb_size << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
1521 crb_size << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
1522 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
1525 if (src_fourcc == V4L2_PIX_FMT_H264) {
1526 coda_write(dev, ctx->slicebuf.paddr,
1527 CODA_CMD_SET_FRAME_SLICE_BB_START);
1528 coda_write(dev, ctx->slicebuf.size / 1024,
1529 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1532 if (dev->devtype->product == CODA_7541) {
1533 int max_mb_x = 1920 / 16;
1534 int max_mb_y = 1088 / 16;
1535 int max_mb_num = max_mb_x * max_mb_y;
1537 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1538 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1539 } else if (dev->devtype->product == CODA_960) {
1540 int max_mb_x = 1920 / 16;
1541 int max_mb_y = 1088 / 16;
1542 int max_mb_num = max_mb_x * max_mb_y;
1544 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1545 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
1548 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1549 v4l2_err(&ctx->dev->v4l2_dev,
1550 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1557 static int coda_start_decoding(struct coda_ctx *ctx)
1559 struct coda_dev *dev = ctx->dev;
1562 mutex_lock(&dev->coda_mutex);
1563 ret = __coda_start_decoding(ctx);
1564 mutex_unlock(&dev->coda_mutex);
1569 static int coda_prepare_decode(struct coda_ctx *ctx)
1571 struct vb2_buffer *dst_buf;
1572 struct coda_dev *dev = ctx->dev;
1573 struct coda_q_data *q_data_dst;
1574 struct coda_buffer_meta *meta;
1575 u32 reg_addr, reg_stride;
1577 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1578 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1580 /* Try to copy source buffer contents into the bitstream ringbuffer */
1581 mutex_lock(&ctx->bitstream_mutex);
1582 coda_fill_bitstream(ctx);
1583 mutex_unlock(&ctx->bitstream_mutex);
1585 if (coda_get_bitstream_payload(ctx) < 512 &&
1586 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1587 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1588 "bitstream payload: %d, skipping\n",
1589 coda_get_bitstream_payload(ctx));
1590 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1594 /* Run coda_start_decoding (again) if not yet initialized */
1595 if (!ctx->initialized) {
1596 int ret = __coda_start_decoding(ctx);
1599 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1600 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1603 ctx->initialized = 1;
1607 if (dev->devtype->product == CODA_960)
1608 coda_set_gdi_regs(ctx);
1610 if (dev->devtype->product == CODA_960) {
1612 * The CODA960 seems to have an internal list of buffers with
1613 * 64 entries that includes the registered frame buffers as
1614 * well as the rotator buffer output.
1615 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1617 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1618 CODA9_CMD_DEC_PIC_ROT_INDEX);
1620 reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y;
1621 reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE;
1623 reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y;
1624 reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE;
1626 coda_write_base(ctx, q_data_dst, dst_buf, reg_addr);
1627 coda_write(dev, q_data_dst->bytesperline, reg_stride);
1629 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1630 CODA_CMD_DEC_PIC_ROT_MODE);
1632 switch (dev->devtype->product) {
1636 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1639 /* 'hardcode to use interrupt disable mode'? */
1640 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION);
1644 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1646 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1647 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1649 if (dev->devtype->product != CODA_DX6)
1650 coda_write(dev, ctx->iram_info.axi_sram_use,
1651 CODA7_REG_BIT_AXI_SRAM_USE);
1653 meta = list_first_entry_or_null(&ctx->buffer_meta_list,
1654 struct coda_buffer_meta, list);
1656 if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) {
1658 /* If this is the last buffer in the bitstream, add padding */
1659 if (meta->end == (ctx->bitstream_fifo.kfifo.in &
1660 ctx->bitstream_fifo.kfifo.mask)) {
1661 static unsigned char buf[512];
1664 /* Pad to multiple of 256 and then add 256 more */
1665 pad = ((0 - meta->end) & 0xff) + 256;
1667 memset(buf, 0xff, sizeof(buf));
1669 kfifo_in(&ctx->bitstream_fifo, buf, pad);
1673 coda_kfifo_sync_to_device_full(ctx);
1675 /* Clear decode success flag */
1676 coda_write(dev, 0, CODA_RET_DEC_PIC_SUCCESS);
1678 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1683 static void coda_finish_decode(struct coda_ctx *ctx)
1685 struct coda_dev *dev = ctx->dev;
1686 struct coda_q_data *q_data_src;
1687 struct coda_q_data *q_data_dst;
1688 struct vb2_buffer *dst_buf;
1689 struct coda_buffer_meta *meta;
1690 unsigned long payload;
1699 /* Update kfifo out pointer from coda bitstream read pointer */
1700 coda_kfifo_sync_from_device(ctx);
1703 * in stream-end mode, the read pointer can overshoot the write pointer
1704 * by up to 512 bytes
1706 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
1707 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
1708 kfifo_init(&ctx->bitstream_fifo,
1709 ctx->bitstream.vaddr, ctx->bitstream.size);
1712 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1713 src_fourcc = q_data_src->fourcc;
1715 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
1717 pr_err("DEC_PIC_SUCCESS = %d\n", val);
1719 success = val & 0x1;
1721 v4l2_err(&dev->v4l2_dev, "decode failed\n");
1723 if (src_fourcc == V4L2_PIX_FMT_H264) {
1725 v4l2_err(&dev->v4l2_dev,
1726 "insufficient PS buffer space (%d bytes)\n",
1729 v4l2_err(&dev->v4l2_dev,
1730 "insufficient slice buffer space (%d bytes)\n",
1731 ctx->slicebuf.size);
1734 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
1735 width = (val >> 16) & 0xffff;
1736 height = val & 0xffff;
1738 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1740 /* frame crop information */
1741 if (src_fourcc == V4L2_PIX_FMT_H264) {
1745 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
1746 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
1748 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
1749 /* Keep current crop information */
1751 struct v4l2_rect *rect = &q_data_dst->rect;
1753 rect->left = left_right >> 16 & 0xffff;
1754 rect->top = top_bottom >> 16 & 0xffff;
1755 rect->width = width - rect->left -
1756 (left_right & 0xffff);
1757 rect->height = height - rect->top -
1758 (top_bottom & 0xffff);
1764 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
1766 v4l2_err(&dev->v4l2_dev,
1767 "errors in %d macroblocks\n", err_mb);
1769 if (dev->devtype->product == CODA_7541) {
1770 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
1772 /* not enough bitstream data */
1773 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1774 "prescan failed: %d\n", val);
1780 ctx->frm_dis_flg = coda_read(dev,
1781 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1784 * The previous display frame was copied out by the rotator,
1785 * now it can be overwritten again
1787 if (ctx->display_idx >= 0 &&
1788 ctx->display_idx < ctx->num_internal_frames) {
1789 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
1790 coda_write(dev, ctx->frm_dis_flg,
1791 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1795 * The index of the last decoded frame, not necessarily in
1796 * display order, and the index of the next display frame.
1797 * The latter could have been decoded in a previous run.
1799 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
1800 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
1802 if (decoded_idx == -1) {
1803 /* no frame was decoded, but we might have a display frame */
1804 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
1805 ctx->sequence_offset++;
1806 else if (ctx->display_idx < 0)
1808 } else if (decoded_idx == -2) {
1809 /* no frame was decoded, we still return remaining buffers */
1810 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
1811 v4l2_err(&dev->v4l2_dev,
1812 "decoded frame index out of range: %d\n", decoded_idx);
1814 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
1815 val -= ctx->sequence_offset;
1816 mutex_lock(&ctx->bitstream_mutex);
1817 if (!list_empty(&ctx->buffer_meta_list)) {
1818 meta = list_first_entry(&ctx->buffer_meta_list,
1819 struct coda_buffer_meta, list);
1820 list_del(&meta->list);
1821 if (val != (meta->sequence & 0xffff)) {
1822 v4l2_err(&dev->v4l2_dev,
1823 "sequence number mismatch (%d(%d) != %d)\n",
1824 val, ctx->sequence_offset,
1827 ctx->frame_metas[decoded_idx] = *meta;
1830 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n");
1831 memset(&ctx->frame_metas[decoded_idx], 0,
1832 sizeof(struct coda_buffer_meta));
1833 ctx->frame_metas[decoded_idx].sequence = val;
1834 ctx->sequence_offset++;
1836 mutex_unlock(&ctx->bitstream_mutex);
1838 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
1840 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
1842 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
1844 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
1846 ctx->frame_errors[decoded_idx] = err_mb;
1849 if (display_idx == -1) {
1851 * no more frames to be decoded, but there could still
1852 * be rotator output to dequeue
1855 } else if (display_idx == -3) {
1856 /* possibly prescan failure */
1857 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
1858 v4l2_err(&dev->v4l2_dev,
1859 "presentation frame index out of range: %d\n",
1863 /* If a frame was copied out, return it */
1864 if (ctx->display_idx >= 0 &&
1865 ctx->display_idx < ctx->num_internal_frames) {
1866 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1867 dst_buf->v4l2_buf.sequence = ctx->osequence++;
1869 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
1870 V4L2_BUF_FLAG_PFRAME |
1871 V4L2_BUF_FLAG_BFRAME);
1872 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
1873 meta = &ctx->frame_metas[ctx->display_idx];
1874 dst_buf->v4l2_buf.timecode = meta->timecode;
1875 dst_buf->v4l2_buf.timestamp = meta->timestamp;
1877 switch (q_data_dst->fourcc) {
1878 case V4L2_PIX_FMT_YUV420:
1879 case V4L2_PIX_FMT_YVU420:
1880 case V4L2_PIX_FMT_NV12:
1882 payload = width * height * 3 / 2;
1884 case V4L2_PIX_FMT_YUV422P:
1885 payload = width * height * 2;
1888 vb2_set_plane_payload(dst_buf, 0, payload);
1890 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
1891 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
1893 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1894 "job finished: decoding frame (%d) (%s)\n",
1895 dst_buf->v4l2_buf.sequence,
1896 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1897 "KEYFRAME" : "PFRAME");
1899 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1900 "job finished: no frame decoded\n");
1903 /* The rotator will copy the current display frame next time */
1904 ctx->display_idx = display_idx;
1907 const struct coda_context_ops coda_bit_decode_ops = {
1908 .queue_init = coda_decoder_queue_init,
1909 .start_streaming = coda_start_decoding,
1910 .prepare_run = coda_prepare_decode,
1911 .finish_run = coda_finish_decode,
1912 .seq_end_work = coda_seq_end_work,
1913 .release = coda_bit_release,
1916 irqreturn_t coda_irq_handler(int irq, void *data)
1918 struct coda_dev *dev = data;
1919 struct coda_ctx *ctx;
1921 /* read status register to attend the IRQ */
1922 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1923 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1924 CODA_REG_BIT_INT_CLEAR);
1926 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1928 v4l2_err(&dev->v4l2_dev,
1929 "Instance released before the end of transaction\n");
1930 mutex_unlock(&dev->coda_mutex);
1934 if (ctx->aborting) {
1935 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1936 "task has been aborted\n");
1939 if (coda_isbusy(ctx->dev)) {
1940 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1941 "coda is still busy!!!!\n");
1945 complete(&ctx->completion);