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/log2.h>
19 #include <linux/platform_device.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-mem2mem.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/videobuf2-dma-contig.h>
30 #include <media/videobuf2-vmalloc.h>
33 #define CREATE_TRACE_POINTS
36 #define CODA_PARA_BUF_SIZE (10 * 1024)
37 #define CODA7_PS_BUF_SIZE 0x28000
38 #define CODA9_PS_SAVE_SIZE (512 * 1024)
40 #define CODA_DEFAULT_GAMMA 4096
41 #define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
43 static void coda_free_bitstream_buffer(struct coda_ctx *ctx);
45 static inline int coda_is_initialized(struct coda_dev *dev)
47 return coda_read(dev, CODA_REG_BIT_CUR_PC) != 0;
50 static inline unsigned long coda_isbusy(struct coda_dev *dev)
52 return coda_read(dev, CODA_REG_BIT_BUSY);
55 static int coda_wait_timeout(struct coda_dev *dev)
57 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
59 while (coda_isbusy(dev)) {
60 if (time_after(jiffies, timeout))
66 static void coda_command_async(struct coda_ctx *ctx, int cmd)
68 struct coda_dev *dev = ctx->dev;
70 if (dev->devtype->product == CODA_960 ||
71 dev->devtype->product == CODA_7541) {
72 /* Restore context related registers to CODA */
73 coda_write(dev, ctx->bit_stream_param,
74 CODA_REG_BIT_BIT_STREAM_PARAM);
75 coda_write(dev, ctx->frm_dis_flg,
76 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
77 coda_write(dev, ctx->frame_mem_ctrl,
78 CODA_REG_BIT_FRAME_MEM_CTRL);
79 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
82 if (dev->devtype->product == CODA_960) {
83 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
84 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
87 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
89 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
90 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
91 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
93 trace_coda_bit_run(ctx, cmd);
95 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
98 static int coda_command_sync(struct coda_ctx *ctx, int cmd)
100 struct coda_dev *dev = ctx->dev;
103 coda_command_async(ctx, cmd);
104 ret = coda_wait_timeout(dev);
105 trace_coda_bit_done(ctx);
110 int coda_hw_reset(struct coda_ctx *ctx)
112 struct coda_dev *dev = ctx->dev;
113 unsigned long timeout;
120 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
122 if (dev->devtype->product == CODA_960) {
123 timeout = jiffies + msecs_to_jiffies(100);
124 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
125 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
126 if (time_after(jiffies, timeout))
132 ret = reset_control_reset(dev->rstc);
136 if (dev->devtype->product == CODA_960)
137 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
138 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
139 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
140 ret = coda_wait_timeout(dev);
141 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
146 static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
148 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
149 struct coda_dev *dev = ctx->dev;
152 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
153 kfifo->out = (kfifo->in & ~kfifo->mask) |
154 (rd_ptr - ctx->bitstream.paddr);
155 if (kfifo->out > kfifo->in)
156 kfifo->out -= kfifo->mask + 1;
159 static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
161 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
162 struct coda_dev *dev = ctx->dev;
165 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
166 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
167 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
168 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
171 static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
173 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
174 struct coda_dev *dev = ctx->dev;
177 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
178 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
181 static int coda_bitstream_queue(struct coda_ctx *ctx,
182 struct vb2_v4l2_buffer *src_buf)
184 u32 src_size = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
187 n = kfifo_in(&ctx->bitstream_fifo,
188 vb2_plane_vaddr(&src_buf->vb2_buf, 0), src_size);
192 src_buf->sequence = ctx->qsequence++;
197 static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
198 struct vb2_v4l2_buffer *src_buf)
202 if (coda_get_bitstream_payload(ctx) +
203 vb2_get_plane_payload(&src_buf->vb2_buf, 0) + 512 >=
207 if (vb2_plane_vaddr(&src_buf->vb2_buf, 0) == NULL) {
208 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
212 ret = coda_bitstream_queue(ctx, src_buf);
214 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
217 /* Sync read pointer to device */
218 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
219 coda_kfifo_sync_to_device_write(ctx);
226 void coda_fill_bitstream(struct coda_ctx *ctx, bool streaming)
228 struct vb2_v4l2_buffer *src_buf;
229 struct coda_buffer_meta *meta;
233 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG)
236 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
238 * Only queue a single JPEG into the bitstream buffer, except
239 * to increase payload over 512 bytes or if in hold state.
241 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
242 (coda_get_bitstream_payload(ctx) >= 512) && !ctx->hold)
245 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
247 /* Drop frames that do not start/end with a SOI/EOI markers */
248 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
249 !coda_jpeg_check_buffer(ctx, &src_buf->vb2_buf)) {
250 v4l2_err(&ctx->dev->v4l2_dev,
251 "dropping invalid JPEG frame %d\n",
253 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
254 v4l2_m2m_buf_done(src_buf, streaming ?
255 VB2_BUF_STATE_ERROR :
256 VB2_BUF_STATE_QUEUED);
260 /* Dump empty buffers */
261 if (!vb2_get_plane_payload(&src_buf->vb2_buf, 0)) {
262 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
263 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
267 /* Buffer start position */
268 start = ctx->bitstream_fifo.kfifo.in &
269 ctx->bitstream_fifo.kfifo.mask;
271 if (coda_bitstream_try_queue(ctx, src_buf)) {
273 * Source buffer is queued in the bitstream ringbuffer;
274 * queue the timestamp and mark source buffer as done
276 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
278 meta = kmalloc(sizeof(*meta), GFP_KERNEL);
280 meta->sequence = src_buf->sequence;
281 meta->timecode = src_buf->timecode;
282 meta->timestamp = src_buf->vb2_buf.timestamp;
284 meta->end = ctx->bitstream_fifo.kfifo.in &
285 ctx->bitstream_fifo.kfifo.mask;
286 spin_lock_irqsave(&ctx->buffer_meta_lock,
288 list_add_tail(&meta->list,
289 &ctx->buffer_meta_list);
291 spin_unlock_irqrestore(&ctx->buffer_meta_lock,
294 trace_coda_bit_queue(ctx, src_buf, meta);
297 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
304 void coda_bit_stream_end_flag(struct coda_ctx *ctx)
306 struct coda_dev *dev = ctx->dev;
308 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
310 /* If this context is currently running, update the hardware flag */
311 if ((dev->devtype->product == CODA_960) &&
313 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
314 coda_write(dev, ctx->bit_stream_param,
315 CODA_REG_BIT_BIT_STREAM_PARAM);
319 static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
321 struct coda_dev *dev = ctx->dev;
322 u32 *p = ctx->parabuf.vaddr;
324 if (dev->devtype->product == CODA_DX6)
327 p[index ^ 1] = value;
330 static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
331 struct coda_aux_buf *buf, size_t size,
334 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
338 static void coda_free_framebuffers(struct coda_ctx *ctx)
342 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
343 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
346 static int coda_alloc_framebuffers(struct coda_ctx *ctx,
347 struct coda_q_data *q_data, u32 fourcc)
349 struct coda_dev *dev = ctx->dev;
355 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
356 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
357 width = round_up(q_data->width, 16);
358 height = round_up(q_data->height, 16);
360 width = round_up(q_data->width, 8);
361 height = q_data->height;
363 ysize = width * height;
365 /* Allocate frame buffers */
366 for (i = 0; i < ctx->num_internal_frames; i++) {
370 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
371 size = round_up(ysize, 4096) + ysize / 2;
373 size = ysize + ysize / 2;
374 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
375 dev->devtype->product != CODA_DX6)
377 name = kasprintf(GFP_KERNEL, "fb%d", i);
378 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
382 coda_free_framebuffers(ctx);
387 /* Register frame buffers in the parameter buffer */
388 for (i = 0; i < ctx->num_internal_frames; i++) {
391 /* Start addresses of Y, Cb, Cr planes */
392 y = ctx->internal_frames[i].paddr;
394 cr = y + ysize + ysize/4;
395 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) {
396 cb = round_up(cb, 4096);
398 /* Packed 20-bit MSB of base addresses */
399 /* YYYYYCCC, CCyyyyyc, cccc.... */
400 y = (y & 0xfffff000) | cb >> 20;
401 cb = (cb & 0x000ff000) << 12;
403 coda_parabuf_write(ctx, i * 3 + 0, y);
404 coda_parabuf_write(ctx, i * 3 + 1, cb);
405 coda_parabuf_write(ctx, i * 3 + 2, cr);
407 /* mvcol buffer for h.264 */
408 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
409 dev->devtype->product != CODA_DX6)
410 coda_parabuf_write(ctx, 96 + i,
411 ctx->internal_frames[i].paddr +
412 ysize + ysize/4 + ysize/4);
415 /* mvcol buffer for mpeg4 */
416 if ((dev->devtype->product != CODA_DX6) &&
417 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
418 coda_parabuf_write(ctx, 97, ctx->internal_frames[0].paddr +
419 ysize + ysize/4 + ysize/4);
424 static void coda_free_context_buffers(struct coda_ctx *ctx)
426 struct coda_dev *dev = ctx->dev;
428 coda_free_aux_buf(dev, &ctx->slicebuf);
429 coda_free_aux_buf(dev, &ctx->psbuf);
430 if (dev->devtype->product != CODA_DX6)
431 coda_free_aux_buf(dev, &ctx->workbuf);
432 coda_free_aux_buf(dev, &ctx->parabuf);
435 static int coda_alloc_context_buffers(struct coda_ctx *ctx,
436 struct coda_q_data *q_data)
438 struct coda_dev *dev = ctx->dev;
442 if (!ctx->parabuf.vaddr) {
443 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
444 CODA_PARA_BUF_SIZE, "parabuf");
449 if (dev->devtype->product == CODA_DX6)
452 if (!ctx->slicebuf.vaddr && q_data->fourcc == V4L2_PIX_FMT_H264) {
453 /* worst case slice size */
454 size = (DIV_ROUND_UP(q_data->width, 16) *
455 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
456 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size,
462 if (!ctx->psbuf.vaddr && dev->devtype->product == CODA_7541) {
463 ret = coda_alloc_context_buf(ctx, &ctx->psbuf,
464 CODA7_PS_BUF_SIZE, "psbuf");
469 if (!ctx->workbuf.vaddr) {
470 size = dev->devtype->workbuf_size;
471 if (dev->devtype->product == CODA_960 &&
472 q_data->fourcc == V4L2_PIX_FMT_H264)
473 size += CODA9_PS_SAVE_SIZE;
474 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size,
483 coda_free_context_buffers(ctx);
487 static int coda_encode_header(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
488 int header_code, u8 *header, int *size)
490 struct vb2_buffer *vb = &buf->vb2_buf;
491 struct coda_dev *dev = ctx->dev;
496 if (dev->devtype->product == CODA_960)
497 memset(vb2_plane_vaddr(vb, 0), 0, 64);
499 coda_write(dev, vb2_dma_contig_plane_dma_addr(vb, 0),
500 CODA_CMD_ENC_HEADER_BB_START);
501 bufsize = vb2_plane_size(vb, 0);
502 if (dev->devtype->product == CODA_960)
504 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
505 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
506 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
508 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
512 if (dev->devtype->product == CODA_960) {
513 for (i = 63; i > 0; i--)
514 if (((char *)vb2_plane_vaddr(vb, 0))[i] != 0)
518 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
519 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
521 memcpy(header, vb2_plane_vaddr(vb, 0), *size);
526 static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
530 size = round_up(size, 1024);
531 if (size > iram->remaining)
533 iram->remaining -= size;
535 ret = iram->next_paddr;
536 iram->next_paddr += size;
541 static void coda_setup_iram(struct coda_ctx *ctx)
543 struct coda_iram_info *iram_info = &ctx->iram_info;
544 struct coda_dev *dev = ctx->dev;
551 memset(iram_info, 0, sizeof(*iram_info));
552 iram_info->next_paddr = dev->iram.paddr;
553 iram_info->remaining = dev->iram.size;
555 if (!dev->iram.vaddr)
558 switch (dev->devtype->product) {
560 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
561 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
562 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
565 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
566 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
567 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
569 default: /* CODA_DX6 */
573 if (ctx->inst_type == CODA_INST_ENCODER) {
574 struct coda_q_data *q_data_src;
576 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
577 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
578 w128 = mb_width * 128;
581 /* Prioritize in case IRAM is too small for everything */
582 if (dev->devtype->product == CODA_7541) {
583 iram_info->search_ram_size = round_up(mb_width * 16 *
585 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
586 iram_info->search_ram_size);
587 if (!iram_info->search_ram_paddr) {
588 pr_err("IRAM is smaller than the search ram size\n");
591 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
595 /* Only H.264BP and H.263P3 are considered */
596 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64);
597 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64);
598 if (!iram_info->buf_dbk_y_use || !iram_info->buf_dbk_c_use)
600 iram_info->axi_sram_use |= dbk_bits;
602 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
603 if (!iram_info->buf_bit_use)
605 iram_info->axi_sram_use |= bit_bits;
607 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
608 if (!iram_info->buf_ip_ac_dc_use)
610 iram_info->axi_sram_use |= ip_bits;
612 /* OVL and BTP disabled for encoder */
613 } else if (ctx->inst_type == CODA_INST_DECODER) {
614 struct coda_q_data *q_data_dst;
616 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
617 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
618 w128 = mb_width * 128;
620 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128);
621 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128);
622 if (!iram_info->buf_dbk_y_use || !iram_info->buf_dbk_c_use)
624 iram_info->axi_sram_use |= dbk_bits;
626 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
627 if (!iram_info->buf_bit_use)
629 iram_info->axi_sram_use |= bit_bits;
631 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
632 if (!iram_info->buf_ip_ac_dc_use)
634 iram_info->axi_sram_use |= ip_bits;
636 /* OVL and BTP unused as there is no VC1 support yet */
640 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
641 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
642 "IRAM smaller than needed\n");
644 if (dev->devtype->product == CODA_7541) {
645 /* TODO - Enabling these causes picture errors on CODA7541 */
646 if (ctx->inst_type == CODA_INST_DECODER) {
648 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
649 CODA7_USE_IP_ENABLE);
652 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
653 CODA7_USE_HOST_DBK_ENABLE |
654 CODA7_USE_IP_ENABLE |
655 CODA7_USE_DBK_ENABLE);
660 static u32 coda_supported_firmwares[] = {
661 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
662 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
663 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
666 static bool coda_firmware_supported(u32 vernum)
670 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
671 if (vernum == coda_supported_firmwares[i])
676 int coda_check_firmware(struct coda_dev *dev)
678 u16 product, major, minor, release;
682 ret = clk_prepare_enable(dev->clk_per);
686 ret = clk_prepare_enable(dev->clk_ahb);
690 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
691 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
692 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
693 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
694 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
695 if (coda_wait_timeout(dev)) {
696 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
701 if (dev->devtype->product == CODA_960) {
702 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
703 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
707 /* Check we are compatible with the loaded firmware */
708 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
709 product = CODA_FIRMWARE_PRODUCT(data);
710 major = CODA_FIRMWARE_MAJOR(data);
711 minor = CODA_FIRMWARE_MINOR(data);
712 release = CODA_FIRMWARE_RELEASE(data);
714 clk_disable_unprepare(dev->clk_per);
715 clk_disable_unprepare(dev->clk_ahb);
717 if (product != dev->devtype->product) {
718 v4l2_err(&dev->v4l2_dev,
719 "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n",
720 coda_product_name(dev->devtype->product),
721 coda_product_name(product), major, minor, release);
725 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
726 coda_product_name(product));
728 if (coda_firmware_supported(data)) {
729 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
730 major, minor, release);
732 v4l2_warn(&dev->v4l2_dev,
733 "Unsupported firmware version: %u.%u.%u\n",
734 major, minor, release);
740 clk_disable_unprepare(dev->clk_ahb);
742 clk_disable_unprepare(dev->clk_per);
747 static void coda9_set_frame_cache(struct coda_ctx *ctx, u32 fourcc)
749 u32 cache_size, cache_config;
751 if (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) {
752 /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */
753 cache_size = 0x20262024;
754 cache_config = 2 << CODA9_CACHE_PAGEMERGE_OFFSET;
756 /* Luma 0x2 page, 4x4 cache, chroma 0x2 page, 4x3 cache size */
757 cache_size = 0x02440243;
758 cache_config = 1 << CODA9_CACHE_PAGEMERGE_OFFSET;
760 coda_write(ctx->dev, cache_size, CODA9_CMD_SET_FRAME_CACHE_SIZE);
761 if (fourcc == V4L2_PIX_FMT_NV12) {
762 cache_config |= 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
763 16 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET |
764 0 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET;
766 cache_config |= 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
767 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET |
768 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET;
770 coda_write(ctx->dev, cache_config, CODA9_CMD_SET_FRAME_CACHE_CONFIG);
774 * Encoder context operations
777 static int coda_encoder_reqbufs(struct coda_ctx *ctx,
778 struct v4l2_requestbuffers *rb)
780 struct coda_q_data *q_data_src;
783 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
787 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
788 ret = coda_alloc_context_buffers(ctx, q_data_src);
792 coda_free_context_buffers(ctx);
798 static int coda_start_encoding(struct coda_ctx *ctx)
800 struct coda_dev *dev = ctx->dev;
801 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
802 struct coda_q_data *q_data_src, *q_data_dst;
803 u32 bitstream_buf, bitstream_size;
804 struct vb2_v4l2_buffer *buf;
805 int gamma, ret, value;
810 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
811 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
812 dst_fourcc = q_data_dst->fourcc;
814 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
815 bitstream_buf = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
816 bitstream_size = q_data_dst->sizeimage;
818 if (!coda_is_initialized(dev)) {
819 v4l2_err(v4l2_dev, "coda is not initialized.\n");
823 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
824 if (!ctx->params.jpeg_qmat_tab[0]) {
825 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
826 if (!ctx->params.jpeg_qmat_tab[0])
829 if (!ctx->params.jpeg_qmat_tab[1]) {
830 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
831 if (!ctx->params.jpeg_qmat_tab[1])
834 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
837 mutex_lock(&dev->coda_mutex);
839 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
840 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
841 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
842 switch (dev->devtype->product) {
844 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
845 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
848 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
851 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
852 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
856 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
857 CODA9_FRAME_TILED2LINEAR);
858 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12)
859 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
860 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
861 ctx->frame_mem_ctrl |= (0x3 << 9) | CODA9_FRAME_TILED2LINEAR;
862 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
864 if (dev->devtype->product == CODA_DX6) {
865 /* Configure the coda */
866 coda_write(dev, dev->iram.paddr,
867 CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
870 /* Could set rotation here if needed */
872 switch (dev->devtype->product) {
874 value = (q_data_src->width & CODADX6_PICWIDTH_MASK)
875 << CODADX6_PICWIDTH_OFFSET;
876 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK)
877 << CODA_PICHEIGHT_OFFSET;
880 if (dst_fourcc == V4L2_PIX_FMT_H264) {
881 value = (round_up(q_data_src->width, 16) &
882 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
883 value |= (round_up(q_data_src->height, 16) &
884 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
889 value = (q_data_src->width & CODA7_PICWIDTH_MASK)
890 << CODA7_PICWIDTH_OFFSET;
891 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK)
892 << CODA_PICHEIGHT_OFFSET;
894 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
895 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
896 ctx->params.framerate = 0;
897 coda_write(dev, ctx->params.framerate,
898 CODA_CMD_ENC_SEQ_SRC_F_RATE);
900 ctx->params.codec_mode = ctx->codec->mode;
901 switch (dst_fourcc) {
902 case V4L2_PIX_FMT_MPEG4:
903 if (dev->devtype->product == CODA_960)
904 coda_write(dev, CODA9_STD_MPEG4,
905 CODA_CMD_ENC_SEQ_COD_STD);
907 coda_write(dev, CODA_STD_MPEG4,
908 CODA_CMD_ENC_SEQ_COD_STD);
909 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
911 case V4L2_PIX_FMT_H264:
912 if (dev->devtype->product == CODA_960)
913 coda_write(dev, CODA9_STD_H264,
914 CODA_CMD_ENC_SEQ_COD_STD);
916 coda_write(dev, CODA_STD_H264,
917 CODA_CMD_ENC_SEQ_COD_STD);
918 if (ctx->params.h264_deblk_enabled) {
919 value = ((ctx->params.h264_deblk_alpha &
920 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
921 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
922 ((ctx->params.h264_deblk_beta &
923 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
924 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
926 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
928 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
930 case V4L2_PIX_FMT_JPEG:
931 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
932 coda_write(dev, ctx->params.jpeg_restart_interval,
933 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
934 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
935 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
936 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
938 coda_jpeg_write_tables(ctx);
942 "dst format (0x%08x) invalid.\n", dst_fourcc);
948 * slice mode and GOP size registers are used for thumb size/offset
951 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
952 switch (ctx->params.slice_mode) {
953 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
956 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
957 value = (ctx->params.slice_max_mb &
958 CODA_SLICING_SIZE_MASK)
959 << CODA_SLICING_SIZE_OFFSET;
960 value |= (1 & CODA_SLICING_UNIT_MASK)
961 << CODA_SLICING_UNIT_OFFSET;
962 value |= 1 & CODA_SLICING_MODE_MASK;
964 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
965 value = (ctx->params.slice_max_bits &
966 CODA_SLICING_SIZE_MASK)
967 << CODA_SLICING_SIZE_OFFSET;
968 value |= (0 & CODA_SLICING_UNIT_MASK)
969 << CODA_SLICING_UNIT_OFFSET;
970 value |= 1 & CODA_SLICING_MODE_MASK;
973 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
974 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
975 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
978 if (ctx->params.bitrate) {
979 /* Rate control enabled */
980 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK)
981 << CODA_RATECONTROL_BITRATE_OFFSET;
982 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
983 value |= (ctx->params.vbv_delay &
984 CODA_RATECONTROL_INITIALDELAY_MASK)
985 << CODA_RATECONTROL_INITIALDELAY_OFFSET;
986 if (dev->devtype->product == CODA_960)
987 value |= BIT(31); /* disable autoskip */
991 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
993 coda_write(dev, ctx->params.vbv_size, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
994 coda_write(dev, ctx->params.intra_refresh,
995 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
997 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
998 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1002 if (dev->devtype->product == CODA_960)
1003 gamma = CODA9_DEFAULT_GAMMA;
1005 gamma = CODA_DEFAULT_GAMMA;
1007 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
1008 CODA_CMD_ENC_SEQ_RC_GAMMA);
1011 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
1013 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
1014 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
1015 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
1017 if (dev->devtype->product == CODA_960) {
1018 if (ctx->params.h264_max_qp)
1019 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
1020 if (CODA_DEFAULT_GAMMA > 0)
1021 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
1023 if (CODA_DEFAULT_GAMMA > 0) {
1024 if (dev->devtype->product == CODA_DX6)
1025 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
1027 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
1029 if (ctx->params.h264_min_qp)
1030 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
1031 if (ctx->params.h264_max_qp)
1032 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
1034 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1036 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
1038 coda_setup_iram(ctx);
1040 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1041 switch (dev->devtype->product) {
1043 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
1044 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1047 coda_write(dev, ctx->iram_info.search_ram_paddr,
1048 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1049 coda_write(dev, ctx->iram_info.search_ram_size,
1050 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1053 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
1054 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
1058 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1060 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1064 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1065 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1069 ctx->initialized = 1;
1071 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
1072 if (dev->devtype->product == CODA_960)
1073 ctx->num_internal_frames = 4;
1075 ctx->num_internal_frames = 2;
1076 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1078 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1082 stride = q_data_src->bytesperline;
1084 ctx->num_internal_frames = 0;
1088 coda_write(dev, num_fb, CODA_CMD_SET_FRAME_BUF_NUM);
1089 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
1091 if (dev->devtype->product == CODA_7541) {
1092 coda_write(dev, q_data_src->bytesperline,
1093 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1095 if (dev->devtype->product != CODA_DX6) {
1096 coda_write(dev, ctx->iram_info.buf_bit_use,
1097 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1098 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1099 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1100 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1101 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1102 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1103 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1104 coda_write(dev, ctx->iram_info.buf_ovl_use,
1105 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1106 if (dev->devtype->product == CODA_960) {
1107 coda_write(dev, ctx->iram_info.buf_btp_use,
1108 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1110 coda9_set_frame_cache(ctx, q_data_src->fourcc);
1113 coda_write(dev, ctx->internal_frames[2].paddr,
1114 CODA9_CMD_SET_FRAME_SUBSAMP_A);
1115 coda_write(dev, ctx->internal_frames[3].paddr,
1116 CODA9_CMD_SET_FRAME_SUBSAMP_B);
1120 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1122 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1126 /* Save stream headers */
1127 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1128 switch (dst_fourcc) {
1129 case V4L2_PIX_FMT_H264:
1131 * Get SPS in the first frame and copy it to an
1132 * intermediate buffer.
1134 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1135 &ctx->vpu_header[0][0],
1136 &ctx->vpu_header_size[0]);
1141 * Get PPS in the first frame and copy it to an
1142 * intermediate buffer.
1144 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1145 &ctx->vpu_header[1][0],
1146 &ctx->vpu_header_size[1]);
1151 * Length of H.264 headers is variable and thus it might not be
1152 * aligned for the coda to append the encoded frame. In that is
1153 * the case a filler NAL must be added to header 2.
1155 ctx->vpu_header_size[2] = coda_h264_padding(
1156 (ctx->vpu_header_size[0] +
1157 ctx->vpu_header_size[1]),
1158 ctx->vpu_header[2]);
1160 case V4L2_PIX_FMT_MPEG4:
1162 * Get VOS in the first frame and copy it to an
1163 * intermediate buffer
1165 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1166 &ctx->vpu_header[0][0],
1167 &ctx->vpu_header_size[0]);
1171 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1172 &ctx->vpu_header[1][0],
1173 &ctx->vpu_header_size[1]);
1177 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1178 &ctx->vpu_header[2][0],
1179 &ctx->vpu_header_size[2]);
1184 /* No more formats need to save headers at the moment */
1189 mutex_unlock(&dev->coda_mutex);
1193 static int coda_prepare_encode(struct coda_ctx *ctx)
1195 struct coda_q_data *q_data_src, *q_data_dst;
1196 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1197 struct coda_dev *dev = ctx->dev;
1199 int quant_param = 0;
1200 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1205 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1206 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1207 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1208 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1209 dst_fourcc = q_data_dst->fourcc;
1211 src_buf->sequence = ctx->osequence;
1212 dst_buf->sequence = ctx->osequence;
1216 * Workaround coda firmware BUG that only marks the first
1217 * frame as IDR. This is a problem for some decoders that can't
1218 * recover when a frame is lost.
1220 if (src_buf->sequence % ctx->params.gop_size) {
1221 src_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1222 src_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1224 src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1225 src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
1228 if (dev->devtype->product == CODA_960)
1229 coda_set_gdi_regs(ctx);
1232 * Copy headers at the beginning of the first frame for H.264 only.
1233 * In MPEG4 they are already copied by the coda.
1235 if (src_buf->sequence == 0) {
1236 pic_stream_buffer_addr =
1237 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0) +
1238 ctx->vpu_header_size[0] +
1239 ctx->vpu_header_size[1] +
1240 ctx->vpu_header_size[2];
1241 pic_stream_buffer_size = q_data_dst->sizeimage -
1242 ctx->vpu_header_size[0] -
1243 ctx->vpu_header_size[1] -
1244 ctx->vpu_header_size[2];
1245 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0),
1246 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1247 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0)
1248 + ctx->vpu_header_size[0], &ctx->vpu_header[1][0],
1249 ctx->vpu_header_size[1]);
1250 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0)
1251 + ctx->vpu_header_size[0] + ctx->vpu_header_size[1],
1252 &ctx->vpu_header[2][0], ctx->vpu_header_size[2]);
1254 pic_stream_buffer_addr =
1255 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1256 pic_stream_buffer_size = q_data_dst->sizeimage;
1259 if (src_buf->flags & V4L2_BUF_FLAG_KEYFRAME) {
1261 switch (dst_fourcc) {
1262 case V4L2_PIX_FMT_H264:
1263 quant_param = ctx->params.h264_intra_qp;
1265 case V4L2_PIX_FMT_MPEG4:
1266 quant_param = ctx->params.mpeg4_intra_qp;
1268 case V4L2_PIX_FMT_JPEG:
1272 v4l2_warn(&ctx->dev->v4l2_dev,
1273 "cannot set intra qp, fmt not supported\n");
1278 switch (dst_fourcc) {
1279 case V4L2_PIX_FMT_H264:
1280 quant_param = ctx->params.h264_inter_qp;
1282 case V4L2_PIX_FMT_MPEG4:
1283 quant_param = ctx->params.mpeg4_inter_qp;
1286 v4l2_warn(&ctx->dev->v4l2_dev,
1287 "cannot set inter qp, fmt not supported\n");
1293 if (ctx->params.rot_mode)
1294 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1295 coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
1296 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1298 if (dev->devtype->product == CODA_960) {
1299 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1300 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1301 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1303 reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y;
1305 reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y;
1307 coda_write_base(ctx, q_data_src, src_buf, reg);
1309 coda_write(dev, force_ipicture << 1 & 0x2,
1310 CODA_CMD_ENC_PIC_OPTION);
1312 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1313 coda_write(dev, pic_stream_buffer_size / 1024,
1314 CODA_CMD_ENC_PIC_BB_SIZE);
1316 if (!ctx->streamon_out) {
1317 /* After streamoff on the output side, set stream end flag */
1318 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1319 coda_write(dev, ctx->bit_stream_param,
1320 CODA_REG_BIT_BIT_STREAM_PARAM);
1323 if (dev->devtype->product != CODA_DX6)
1324 coda_write(dev, ctx->iram_info.axi_sram_use,
1325 CODA7_REG_BIT_AXI_SRAM_USE);
1327 trace_coda_enc_pic_run(ctx, src_buf);
1329 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1334 static void coda_finish_encode(struct coda_ctx *ctx)
1336 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1337 struct coda_dev *dev = ctx->dev;
1338 u32 wr_ptr, start_ptr;
1340 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1341 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1343 trace_coda_enc_pic_done(ctx, dst_buf);
1345 /* Get results from the coda */
1346 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1347 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1349 /* Calculate bytesused field */
1350 if (dst_buf->sequence == 0) {
1351 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr +
1352 ctx->vpu_header_size[0] +
1353 ctx->vpu_header_size[1] +
1354 ctx->vpu_header_size[2]);
1356 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr);
1359 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1360 wr_ptr - start_ptr);
1362 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1363 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1365 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
1366 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1367 dst_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
1369 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1370 dst_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1373 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1374 dst_buf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1376 src_buf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1377 dst_buf->timecode = src_buf->timecode;
1379 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1381 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1382 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE);
1385 if (ctx->gopcounter < 0)
1386 ctx->gopcounter = ctx->params.gop_size - 1;
1388 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1389 "job finished: encoding frame (%d) (%s)\n",
1391 (dst_buf->flags & V4L2_BUF_FLAG_KEYFRAME) ?
1392 "KEYFRAME" : "PFRAME");
1395 static void coda_seq_end_work(struct work_struct *work)
1397 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1398 struct coda_dev *dev = ctx->dev;
1400 mutex_lock(&ctx->buffer_mutex);
1401 mutex_lock(&dev->coda_mutex);
1403 if (ctx->initialized == 0)
1406 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1407 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx,
1409 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1410 v4l2_err(&dev->v4l2_dev,
1411 "CODA_COMMAND_SEQ_END failed\n");
1415 * FIXME: Sometimes h.264 encoding fails with 8-byte sequences missing
1416 * from the output stream after the h.264 decoder has run. Resetting the
1417 * hardware after the decoder has finished seems to help.
1419 if (dev->devtype->product == CODA_960)
1422 kfifo_init(&ctx->bitstream_fifo,
1423 ctx->bitstream.vaddr, ctx->bitstream.size);
1425 coda_free_framebuffers(ctx);
1427 ctx->initialized = 0;
1430 mutex_unlock(&dev->coda_mutex);
1431 mutex_unlock(&ctx->buffer_mutex);
1434 static void coda_bit_release(struct coda_ctx *ctx)
1436 mutex_lock(&ctx->buffer_mutex);
1437 coda_free_framebuffers(ctx);
1438 coda_free_context_buffers(ctx);
1439 coda_free_bitstream_buffer(ctx);
1440 mutex_unlock(&ctx->buffer_mutex);
1443 const struct coda_context_ops coda_bit_encode_ops = {
1444 .queue_init = coda_encoder_queue_init,
1445 .reqbufs = coda_encoder_reqbufs,
1446 .start_streaming = coda_start_encoding,
1447 .prepare_run = coda_prepare_encode,
1448 .finish_run = coda_finish_encode,
1449 .seq_end_work = coda_seq_end_work,
1450 .release = coda_bit_release,
1454 * Decoder context operations
1457 static int coda_alloc_bitstream_buffer(struct coda_ctx *ctx,
1458 struct coda_q_data *q_data)
1460 if (ctx->bitstream.vaddr)
1463 ctx->bitstream.size = roundup_pow_of_two(q_data->sizeimage * 2);
1464 ctx->bitstream.vaddr = dma_alloc_wc(&ctx->dev->plat_dev->dev,
1465 ctx->bitstream.size,
1466 &ctx->bitstream.paddr, GFP_KERNEL);
1467 if (!ctx->bitstream.vaddr) {
1468 v4l2_err(&ctx->dev->v4l2_dev,
1469 "failed to allocate bitstream ringbuffer");
1472 kfifo_init(&ctx->bitstream_fifo,
1473 ctx->bitstream.vaddr, ctx->bitstream.size);
1478 static void coda_free_bitstream_buffer(struct coda_ctx *ctx)
1480 if (ctx->bitstream.vaddr == NULL)
1483 dma_free_wc(&ctx->dev->plat_dev->dev, ctx->bitstream.size,
1484 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1485 ctx->bitstream.vaddr = NULL;
1486 kfifo_init(&ctx->bitstream_fifo, NULL, 0);
1489 static int coda_decoder_reqbufs(struct coda_ctx *ctx,
1490 struct v4l2_requestbuffers *rb)
1492 struct coda_q_data *q_data_src;
1495 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1499 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1500 ret = coda_alloc_context_buffers(ctx, q_data_src);
1503 ret = coda_alloc_bitstream_buffer(ctx, q_data_src);
1505 coda_free_context_buffers(ctx);
1509 coda_free_bitstream_buffer(ctx);
1510 coda_free_context_buffers(ctx);
1516 static int __coda_start_decoding(struct coda_ctx *ctx)
1518 struct coda_q_data *q_data_src, *q_data_dst;
1519 u32 bitstream_buf, bitstream_size;
1520 struct coda_dev *dev = ctx->dev;
1522 u32 src_fourcc, dst_fourcc;
1526 /* Start decoding */
1527 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1528 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1529 bitstream_buf = ctx->bitstream.paddr;
1530 bitstream_size = ctx->bitstream.size;
1531 src_fourcc = q_data_src->fourcc;
1532 dst_fourcc = q_data_dst->fourcc;
1534 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1536 /* Update coda bitstream read and write pointers from kfifo */
1537 coda_kfifo_sync_to_device_full(ctx);
1539 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
1540 CODA9_FRAME_TILED2LINEAR);
1541 if (dst_fourcc == V4L2_PIX_FMT_NV12)
1542 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
1543 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
1544 ctx->frame_mem_ctrl |= (0x3 << 9) | CODA9_FRAME_TILED2LINEAR;
1545 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1547 ctx->display_idx = -1;
1548 ctx->frm_dis_flg = 0;
1549 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1551 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1552 CODA_REG_BIT_BIT_STREAM_PARAM);
1554 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1555 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1557 if ((dev->devtype->product == CODA_7541) ||
1558 (dev->devtype->product == CODA_960))
1559 val |= CODA_REORDER_ENABLE;
1560 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1561 val |= CODA_NO_INT_ENABLE;
1562 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1564 ctx->params.codec_mode = ctx->codec->mode;
1565 if (dev->devtype->product == CODA_960 &&
1566 src_fourcc == V4L2_PIX_FMT_MPEG4)
1567 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1569 ctx->params.codec_mode_aux = 0;
1570 if (src_fourcc == V4L2_PIX_FMT_H264) {
1571 if (dev->devtype->product == CODA_7541) {
1572 coda_write(dev, ctx->psbuf.paddr,
1573 CODA_CMD_DEC_SEQ_PS_BB_START);
1574 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1575 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1577 if (dev->devtype->product == CODA_960) {
1578 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1579 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1582 if (dev->devtype->product != CODA_960)
1583 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
1585 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1586 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1587 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1590 ctx->sequence_offset = ~0U;
1591 ctx->initialized = 1;
1593 /* Update kfifo out pointer from coda bitstream read pointer */
1594 coda_kfifo_sync_from_device(ctx);
1596 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1598 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1599 v4l2_err(&dev->v4l2_dev,
1600 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1601 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1605 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1606 if (dev->devtype->product == CODA_DX6) {
1607 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1608 height = val & CODADX6_PICHEIGHT_MASK;
1610 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1611 height = val & CODA7_PICHEIGHT_MASK;
1614 if (width > q_data_dst->bytesperline || height > q_data_dst->height) {
1615 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1616 width, height, q_data_dst->bytesperline,
1617 q_data_dst->height);
1621 width = round_up(width, 16);
1622 height = round_up(height, 16);
1624 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1625 __func__, ctx->idx, width, height);
1627 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
1628 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1629 v4l2_err(&dev->v4l2_dev,
1630 "not enough framebuffers to decode (%d < %d)\n",
1631 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1635 if (src_fourcc == V4L2_PIX_FMT_H264) {
1639 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1640 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1642 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1643 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1644 q_data_dst->rect.width = width - q_data_dst->rect.left -
1645 (left_right & 0x3ff);
1646 q_data_dst->rect.height = height - q_data_dst->rect.top -
1647 (top_bottom & 0x3ff);
1650 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1652 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n");
1656 /* Tell the decoder how many frame buffers we allocated. */
1657 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1658 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1660 if (dev->devtype->product != CODA_DX6) {
1661 /* Set secondary AXI IRAM */
1662 coda_setup_iram(ctx);
1664 coda_write(dev, ctx->iram_info.buf_bit_use,
1665 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1666 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1667 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1668 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1669 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1670 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1671 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1672 coda_write(dev, ctx->iram_info.buf_ovl_use,
1673 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1674 if (dev->devtype->product == CODA_960) {
1675 coda_write(dev, ctx->iram_info.buf_btp_use,
1676 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1678 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
1679 coda9_set_frame_cache(ctx, dst_fourcc);
1683 if (src_fourcc == V4L2_PIX_FMT_H264) {
1684 coda_write(dev, ctx->slicebuf.paddr,
1685 CODA_CMD_SET_FRAME_SLICE_BB_START);
1686 coda_write(dev, ctx->slicebuf.size / 1024,
1687 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1690 if (dev->devtype->product == CODA_7541) {
1691 int max_mb_x = 1920 / 16;
1692 int max_mb_y = 1088 / 16;
1693 int max_mb_num = max_mb_x * max_mb_y;
1695 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1696 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1697 } else if (dev->devtype->product == CODA_960) {
1698 int max_mb_x = 1920 / 16;
1699 int max_mb_y = 1088 / 16;
1700 int max_mb_num = max_mb_x * max_mb_y;
1702 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1703 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
1706 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1707 v4l2_err(&ctx->dev->v4l2_dev,
1708 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1715 static int coda_start_decoding(struct coda_ctx *ctx)
1717 struct coda_dev *dev = ctx->dev;
1720 mutex_lock(&dev->coda_mutex);
1721 ret = __coda_start_decoding(ctx);
1722 mutex_unlock(&dev->coda_mutex);
1727 static int coda_prepare_decode(struct coda_ctx *ctx)
1729 struct vb2_v4l2_buffer *dst_buf;
1730 struct coda_dev *dev = ctx->dev;
1731 struct coda_q_data *q_data_dst;
1732 struct coda_buffer_meta *meta;
1733 unsigned long flags;
1734 u32 reg_addr, reg_stride;
1736 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1737 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1739 /* Try to copy source buffer contents into the bitstream ringbuffer */
1740 mutex_lock(&ctx->bitstream_mutex);
1741 coda_fill_bitstream(ctx, true);
1742 mutex_unlock(&ctx->bitstream_mutex);
1744 if (coda_get_bitstream_payload(ctx) < 512 &&
1745 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1746 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1747 "bitstream payload: %d, skipping\n",
1748 coda_get_bitstream_payload(ctx));
1749 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1753 /* Run coda_start_decoding (again) if not yet initialized */
1754 if (!ctx->initialized) {
1755 int ret = __coda_start_decoding(ctx);
1758 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1759 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1762 ctx->initialized = 1;
1766 if (dev->devtype->product == CODA_960)
1767 coda_set_gdi_regs(ctx);
1769 if (dev->devtype->product == CODA_960) {
1771 * The CODA960 seems to have an internal list of buffers with
1772 * 64 entries that includes the registered frame buffers as
1773 * well as the rotator buffer output.
1774 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1776 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->vb2_buf.index,
1777 CODA9_CMD_DEC_PIC_ROT_INDEX);
1779 reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y;
1780 reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE;
1782 reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y;
1783 reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE;
1785 coda_write_base(ctx, q_data_dst, dst_buf, reg_addr);
1786 coda_write(dev, q_data_dst->bytesperline, reg_stride);
1788 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1789 CODA_CMD_DEC_PIC_ROT_MODE);
1791 switch (dev->devtype->product) {
1795 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1798 /* 'hardcode to use interrupt disable mode'? */
1799 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION);
1803 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1805 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1806 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1808 if (dev->devtype->product != CODA_DX6)
1809 coda_write(dev, ctx->iram_info.axi_sram_use,
1810 CODA7_REG_BIT_AXI_SRAM_USE);
1812 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1813 meta = list_first_entry_or_null(&ctx->buffer_meta_list,
1814 struct coda_buffer_meta, list);
1816 if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) {
1818 /* If this is the last buffer in the bitstream, add padding */
1819 if (meta->end == (ctx->bitstream_fifo.kfifo.in &
1820 ctx->bitstream_fifo.kfifo.mask)) {
1821 static unsigned char buf[512];
1824 /* Pad to multiple of 256 and then add 256 more */
1825 pad = ((0 - meta->end) & 0xff) + 256;
1827 memset(buf, 0xff, sizeof(buf));
1829 kfifo_in(&ctx->bitstream_fifo, buf, pad);
1832 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1834 coda_kfifo_sync_to_device_full(ctx);
1836 /* Clear decode success flag */
1837 coda_write(dev, 0, CODA_RET_DEC_PIC_SUCCESS);
1839 /* Clear error return value */
1840 coda_write(dev, 0, CODA_RET_DEC_PIC_ERR_MB);
1842 trace_coda_dec_pic_run(ctx, meta);
1844 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1849 static void coda_finish_decode(struct coda_ctx *ctx)
1851 struct coda_dev *dev = ctx->dev;
1852 struct coda_q_data *q_data_src;
1853 struct coda_q_data *q_data_dst;
1854 struct vb2_v4l2_buffer *dst_buf;
1855 struct coda_buffer_meta *meta;
1856 unsigned long payload;
1857 unsigned long flags;
1866 /* Update kfifo out pointer from coda bitstream read pointer */
1867 coda_kfifo_sync_from_device(ctx);
1870 * in stream-end mode, the read pointer can overshoot the write pointer
1871 * by up to 512 bytes
1873 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
1874 if (coda_get_bitstream_payload(ctx) >= ctx->bitstream.size - 512)
1875 kfifo_init(&ctx->bitstream_fifo,
1876 ctx->bitstream.vaddr, ctx->bitstream.size);
1879 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1880 src_fourcc = q_data_src->fourcc;
1882 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
1884 pr_err("DEC_PIC_SUCCESS = %d\n", val);
1886 success = val & 0x1;
1888 v4l2_err(&dev->v4l2_dev, "decode failed\n");
1890 if (src_fourcc == V4L2_PIX_FMT_H264) {
1892 v4l2_err(&dev->v4l2_dev,
1893 "insufficient PS buffer space (%d bytes)\n",
1896 v4l2_err(&dev->v4l2_dev,
1897 "insufficient slice buffer space (%d bytes)\n",
1898 ctx->slicebuf.size);
1901 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
1902 width = (val >> 16) & 0xffff;
1903 height = val & 0xffff;
1905 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1907 /* frame crop information */
1908 if (src_fourcc == V4L2_PIX_FMT_H264) {
1912 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
1913 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
1915 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
1916 /* Keep current crop information */
1918 struct v4l2_rect *rect = &q_data_dst->rect;
1920 rect->left = left_right >> 16 & 0xffff;
1921 rect->top = top_bottom >> 16 & 0xffff;
1922 rect->width = width - rect->left -
1923 (left_right & 0xffff);
1924 rect->height = height - rect->top -
1925 (top_bottom & 0xffff);
1931 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
1933 v4l2_err(&dev->v4l2_dev,
1934 "errors in %d macroblocks\n", err_mb);
1936 if (dev->devtype->product == CODA_7541) {
1937 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
1939 /* not enough bitstream data */
1940 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1941 "prescan failed: %d\n", val);
1947 ctx->frm_dis_flg = coda_read(dev,
1948 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1951 * The previous display frame was copied out by the rotator,
1952 * now it can be overwritten again
1954 if (ctx->display_idx >= 0 &&
1955 ctx->display_idx < ctx->num_internal_frames) {
1956 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
1957 coda_write(dev, ctx->frm_dis_flg,
1958 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1962 * The index of the last decoded frame, not necessarily in
1963 * display order, and the index of the next display frame.
1964 * The latter could have been decoded in a previous run.
1966 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
1967 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
1969 if (decoded_idx == -1) {
1970 /* no frame was decoded, but we might have a display frame */
1971 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
1972 ctx->sequence_offset++;
1973 else if (ctx->display_idx < 0)
1975 } else if (decoded_idx == -2) {
1976 if (ctx->display_idx >= 0 &&
1977 ctx->display_idx < ctx->num_internal_frames)
1978 ctx->sequence_offset++;
1979 /* no frame was decoded, we still return remaining buffers */
1980 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
1981 v4l2_err(&dev->v4l2_dev,
1982 "decoded frame index out of range: %d\n", decoded_idx);
1984 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM);
1985 if (ctx->sequence_offset == -1)
1986 ctx->sequence_offset = val;
1987 val -= ctx->sequence_offset;
1988 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1989 if (!list_empty(&ctx->buffer_meta_list)) {
1990 meta = list_first_entry(&ctx->buffer_meta_list,
1991 struct coda_buffer_meta, list);
1992 list_del(&meta->list);
1994 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1996 * Clamp counters to 16 bits for comparison, as the HW
1997 * counter rolls over at this point for h.264. This
1998 * may be different for other formats, but using 16 bits
1999 * should be enough to detect most errors and saves us
2000 * from doing different things based on the format.
2002 if ((val & 0xffff) != (meta->sequence & 0xffff)) {
2003 v4l2_err(&dev->v4l2_dev,
2004 "sequence number mismatch (%d(%d) != %d)\n",
2005 val, ctx->sequence_offset,
2008 ctx->frame_metas[decoded_idx] = *meta;
2011 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
2012 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n");
2013 memset(&ctx->frame_metas[decoded_idx], 0,
2014 sizeof(struct coda_buffer_meta));
2015 ctx->frame_metas[decoded_idx].sequence = val;
2016 ctx->sequence_offset++;
2019 trace_coda_dec_pic_done(ctx, &ctx->frame_metas[decoded_idx]);
2021 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
2023 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
2025 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
2027 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
2029 ctx->frame_errors[decoded_idx] = err_mb;
2032 if (display_idx == -1) {
2034 * no more frames to be decoded, but there could still
2035 * be rotator output to dequeue
2038 } else if (display_idx == -3) {
2039 /* possibly prescan failure */
2040 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
2041 v4l2_err(&dev->v4l2_dev,
2042 "presentation frame index out of range: %d\n",
2046 /* If a frame was copied out, return it */
2047 if (ctx->display_idx >= 0 &&
2048 ctx->display_idx < ctx->num_internal_frames) {
2049 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
2050 dst_buf->sequence = ctx->osequence++;
2052 dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
2053 V4L2_BUF_FLAG_PFRAME |
2054 V4L2_BUF_FLAG_BFRAME);
2055 dst_buf->flags |= ctx->frame_types[ctx->display_idx];
2056 meta = &ctx->frame_metas[ctx->display_idx];
2057 dst_buf->timecode = meta->timecode;
2058 dst_buf->vb2_buf.timestamp = meta->timestamp;
2060 trace_coda_dec_rot_done(ctx, dst_buf, meta);
2062 switch (q_data_dst->fourcc) {
2063 case V4L2_PIX_FMT_YUV420:
2064 case V4L2_PIX_FMT_YVU420:
2065 case V4L2_PIX_FMT_NV12:
2067 payload = width * height * 3 / 2;
2069 case V4L2_PIX_FMT_YUV422P:
2070 payload = width * height * 2;
2073 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
2075 coda_m2m_buf_done(ctx, dst_buf, ctx->frame_errors[display_idx] ?
2076 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
2078 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2079 "job finished: decoding frame (%d) (%s)\n",
2081 (dst_buf->flags & V4L2_BUF_FLAG_KEYFRAME) ?
2082 "KEYFRAME" : "PFRAME");
2084 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2085 "job finished: no frame decoded\n");
2088 /* The rotator will copy the current display frame next time */
2089 ctx->display_idx = display_idx;
2092 const struct coda_context_ops coda_bit_decode_ops = {
2093 .queue_init = coda_decoder_queue_init,
2094 .reqbufs = coda_decoder_reqbufs,
2095 .start_streaming = coda_start_decoding,
2096 .prepare_run = coda_prepare_decode,
2097 .finish_run = coda_finish_decode,
2098 .seq_end_work = coda_seq_end_work,
2099 .release = coda_bit_release,
2102 irqreturn_t coda_irq_handler(int irq, void *data)
2104 struct coda_dev *dev = data;
2105 struct coda_ctx *ctx;
2107 /* read status register to attend the IRQ */
2108 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2109 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2110 CODA_REG_BIT_INT_CLEAR);
2112 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2114 v4l2_err(&dev->v4l2_dev,
2115 "Instance released before the end of transaction\n");
2119 trace_coda_bit_done(ctx);
2121 if (ctx->aborting) {
2122 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2123 "task has been aborted\n");
2126 if (coda_isbusy(ctx->dev)) {
2127 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2128 "coda is still busy!!!!\n");
2132 complete(&ctx->completion);