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>
34 #define CREATE_TRACE_POINTS
37 #define CODA_PARA_BUF_SIZE (10 * 1024)
38 #define CODA7_PS_BUF_SIZE 0x28000
39 #define CODA9_PS_SAVE_SIZE (512 * 1024)
41 #define CODA_DEFAULT_GAMMA 4096
42 #define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
44 static void coda_free_bitstream_buffer(struct coda_ctx *ctx);
46 static inline int coda_is_initialized(struct coda_dev *dev)
48 return coda_read(dev, CODA_REG_BIT_CUR_PC) != 0;
51 static inline unsigned long coda_isbusy(struct coda_dev *dev)
53 return coda_read(dev, CODA_REG_BIT_BUSY);
56 static int coda_wait_timeout(struct coda_dev *dev)
58 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
60 while (coda_isbusy(dev)) {
61 if (time_after(jiffies, timeout))
67 static void coda_command_async(struct coda_ctx *ctx, int cmd)
69 struct coda_dev *dev = ctx->dev;
71 if (dev->devtype->product == CODA_960 ||
72 dev->devtype->product == CODA_7541) {
73 /* Restore context related registers to CODA */
74 coda_write(dev, ctx->bit_stream_param,
75 CODA_REG_BIT_BIT_STREAM_PARAM);
76 coda_write(dev, ctx->frm_dis_flg,
77 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
78 coda_write(dev, ctx->frame_mem_ctrl,
79 CODA_REG_BIT_FRAME_MEM_CTRL);
80 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
83 if (dev->devtype->product == CODA_960) {
84 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
85 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
88 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
90 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
91 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
92 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
94 trace_coda_bit_run(ctx, cmd);
96 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
99 static int coda_command_sync(struct coda_ctx *ctx, int cmd)
101 struct coda_dev *dev = ctx->dev;
104 coda_command_async(ctx, cmd);
105 ret = coda_wait_timeout(dev);
106 trace_coda_bit_done(ctx);
111 int coda_hw_reset(struct coda_ctx *ctx)
113 struct coda_dev *dev = ctx->dev;
114 unsigned long timeout;
121 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
123 if (dev->devtype->product == CODA_960) {
124 timeout = jiffies + msecs_to_jiffies(100);
125 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
126 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
127 if (time_after(jiffies, timeout))
133 ret = reset_control_reset(dev->rstc);
137 if (dev->devtype->product == CODA_960)
138 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
139 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
140 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
141 ret = coda_wait_timeout(dev);
142 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
147 static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
149 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
150 struct coda_dev *dev = ctx->dev;
153 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
154 kfifo->out = (kfifo->in & ~kfifo->mask) |
155 (rd_ptr - ctx->bitstream.paddr);
156 if (kfifo->out > kfifo->in)
157 kfifo->out -= kfifo->mask + 1;
160 static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
162 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
163 struct coda_dev *dev = ctx->dev;
166 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
167 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
168 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
169 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
172 static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
174 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
175 struct coda_dev *dev = ctx->dev;
178 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
179 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
182 static int coda_bitstream_pad(struct coda_ctx *ctx, u32 size)
190 buf = kmalloc(size, GFP_KERNEL);
194 coda_h264_filler_nal(size, buf);
195 n = kfifo_in(&ctx->bitstream_fifo, buf, size);
198 return (n < size) ? -ENOSPC : 0;
201 static int coda_bitstream_queue(struct coda_ctx *ctx,
202 struct vb2_v4l2_buffer *src_buf)
204 u32 src_size = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
207 n = kfifo_in(&ctx->bitstream_fifo,
208 vb2_plane_vaddr(&src_buf->vb2_buf, 0), src_size);
212 src_buf->sequence = ctx->qsequence++;
217 static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
218 struct vb2_v4l2_buffer *src_buf)
220 unsigned long payload = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
223 if (coda_get_bitstream_payload(ctx) + payload + 512 >=
227 if (vb2_plane_vaddr(&src_buf->vb2_buf, 0) == NULL) {
228 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
232 /* Add zero padding before the first H.264 buffer, if it is too small */
233 if (ctx->qsequence == 0 && payload < 512 &&
234 ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
235 coda_bitstream_pad(ctx, 512 - payload);
237 ret = coda_bitstream_queue(ctx, src_buf);
239 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
242 /* Sync read pointer to device */
243 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
244 coda_kfifo_sync_to_device_write(ctx);
251 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list)
253 struct vb2_v4l2_buffer *src_buf;
254 struct coda_buffer_meta *meta;
258 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG)
261 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
263 * Only queue a single JPEG into the bitstream buffer, except
264 * to increase payload over 512 bytes or if in hold state.
266 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
267 (coda_get_bitstream_payload(ctx) >= 512) && !ctx->hold)
270 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
272 /* Drop frames that do not start/end with a SOI/EOI markers */
273 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
274 !coda_jpeg_check_buffer(ctx, &src_buf->vb2_buf)) {
275 v4l2_err(&ctx->dev->v4l2_dev,
276 "dropping invalid JPEG frame %d\n",
278 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
280 struct v4l2_m2m_buffer *m2m_buf;
282 m2m_buf = container_of(src_buf,
283 struct v4l2_m2m_buffer,
285 list_add_tail(&m2m_buf->list, buffer_list);
287 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
292 /* Dump empty buffers */
293 if (!vb2_get_plane_payload(&src_buf->vb2_buf, 0)) {
294 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
295 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
299 /* Buffer start position */
300 start = ctx->bitstream_fifo.kfifo.in &
301 ctx->bitstream_fifo.kfifo.mask;
303 if (coda_bitstream_try_queue(ctx, src_buf)) {
305 * Source buffer is queued in the bitstream ringbuffer;
306 * queue the timestamp and mark source buffer as done
308 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
310 meta = kmalloc(sizeof(*meta), GFP_KERNEL);
312 meta->sequence = src_buf->sequence;
313 meta->timecode = src_buf->timecode;
314 meta->timestamp = src_buf->vb2_buf.timestamp;
316 meta->end = ctx->bitstream_fifo.kfifo.in &
317 ctx->bitstream_fifo.kfifo.mask;
318 spin_lock_irqsave(&ctx->buffer_meta_lock,
320 list_add_tail(&meta->list,
321 &ctx->buffer_meta_list);
323 spin_unlock_irqrestore(&ctx->buffer_meta_lock,
326 trace_coda_bit_queue(ctx, src_buf, meta);
330 struct v4l2_m2m_buffer *m2m_buf;
332 m2m_buf = container_of(src_buf,
333 struct v4l2_m2m_buffer,
335 list_add_tail(&m2m_buf->list, buffer_list);
337 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
345 void coda_bit_stream_end_flag(struct coda_ctx *ctx)
347 struct coda_dev *dev = ctx->dev;
349 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
351 /* If this context is currently running, update the hardware flag */
352 if ((dev->devtype->product == CODA_960) &&
354 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
355 coda_write(dev, ctx->bit_stream_param,
356 CODA_REG_BIT_BIT_STREAM_PARAM);
360 static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
362 struct coda_dev *dev = ctx->dev;
363 u32 *p = ctx->parabuf.vaddr;
365 if (dev->devtype->product == CODA_DX6)
368 p[index ^ 1] = value;
371 static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
372 struct coda_aux_buf *buf, size_t size,
375 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
379 static void coda_free_framebuffers(struct coda_ctx *ctx)
383 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
384 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
387 static int coda_alloc_framebuffers(struct coda_ctx *ctx,
388 struct coda_q_data *q_data, u32 fourcc)
390 struct coda_dev *dev = ctx->dev;
396 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
397 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
398 width = round_up(q_data->width, 16);
399 height = round_up(q_data->height, 16);
401 width = round_up(q_data->width, 8);
402 height = q_data->height;
404 ysize = width * height;
406 /* Allocate frame buffers */
407 for (i = 0; i < ctx->num_internal_frames; i++) {
411 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
412 size = round_up(ysize, 4096) + ysize / 2;
414 size = ysize + ysize / 2;
415 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
416 dev->devtype->product != CODA_DX6)
418 name = kasprintf(GFP_KERNEL, "fb%d", i);
419 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
423 coda_free_framebuffers(ctx);
428 /* Register frame buffers in the parameter buffer */
429 for (i = 0; i < ctx->num_internal_frames; i++) {
432 /* Start addresses of Y, Cb, Cr planes */
433 y = ctx->internal_frames[i].paddr;
435 cr = y + ysize + ysize/4;
436 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) {
437 cb = round_up(cb, 4096);
439 /* Packed 20-bit MSB of base addresses */
440 /* YYYYYCCC, CCyyyyyc, cccc.... */
441 y = (y & 0xfffff000) | cb >> 20;
442 cb = (cb & 0x000ff000) << 12;
444 coda_parabuf_write(ctx, i * 3 + 0, y);
445 coda_parabuf_write(ctx, i * 3 + 1, cb);
446 coda_parabuf_write(ctx, i * 3 + 2, cr);
448 /* mvcol buffer for h.264 */
449 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
450 dev->devtype->product != CODA_DX6)
451 coda_parabuf_write(ctx, 96 + i,
452 ctx->internal_frames[i].paddr +
453 ysize + ysize/4 + ysize/4);
456 /* mvcol buffer for mpeg4 */
457 if ((dev->devtype->product != CODA_DX6) &&
458 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
459 coda_parabuf_write(ctx, 97, ctx->internal_frames[0].paddr +
460 ysize + ysize/4 + ysize/4);
465 static void coda_free_context_buffers(struct coda_ctx *ctx)
467 struct coda_dev *dev = ctx->dev;
469 coda_free_aux_buf(dev, &ctx->slicebuf);
470 coda_free_aux_buf(dev, &ctx->psbuf);
471 if (dev->devtype->product != CODA_DX6)
472 coda_free_aux_buf(dev, &ctx->workbuf);
473 coda_free_aux_buf(dev, &ctx->parabuf);
476 static int coda_alloc_context_buffers(struct coda_ctx *ctx,
477 struct coda_q_data *q_data)
479 struct coda_dev *dev = ctx->dev;
483 if (!ctx->parabuf.vaddr) {
484 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
485 CODA_PARA_BUF_SIZE, "parabuf");
490 if (dev->devtype->product == CODA_DX6)
493 if (!ctx->slicebuf.vaddr && q_data->fourcc == V4L2_PIX_FMT_H264) {
494 /* worst case slice size */
495 size = (DIV_ROUND_UP(q_data->width, 16) *
496 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
497 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size,
503 if (!ctx->psbuf.vaddr && dev->devtype->product == CODA_7541) {
504 ret = coda_alloc_context_buf(ctx, &ctx->psbuf,
505 CODA7_PS_BUF_SIZE, "psbuf");
510 if (!ctx->workbuf.vaddr) {
511 size = dev->devtype->workbuf_size;
512 if (dev->devtype->product == CODA_960 &&
513 q_data->fourcc == V4L2_PIX_FMT_H264)
514 size += CODA9_PS_SAVE_SIZE;
515 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size,
524 coda_free_context_buffers(ctx);
528 static int coda_encode_header(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
529 int header_code, u8 *header, int *size)
531 struct vb2_buffer *vb = &buf->vb2_buf;
532 struct coda_dev *dev = ctx->dev;
537 if (dev->devtype->product == CODA_960)
538 memset(vb2_plane_vaddr(vb, 0), 0, 64);
540 coda_write(dev, vb2_dma_contig_plane_dma_addr(vb, 0),
541 CODA_CMD_ENC_HEADER_BB_START);
542 bufsize = vb2_plane_size(vb, 0);
543 if (dev->devtype->product == CODA_960)
545 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
546 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
547 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
549 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
553 if (dev->devtype->product == CODA_960) {
554 for (i = 63; i > 0; i--)
555 if (((char *)vb2_plane_vaddr(vb, 0))[i] != 0)
559 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
560 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
562 memcpy(header, vb2_plane_vaddr(vb, 0), *size);
567 static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
571 size = round_up(size, 1024);
572 if (size > iram->remaining)
574 iram->remaining -= size;
576 ret = iram->next_paddr;
577 iram->next_paddr += size;
582 static void coda_setup_iram(struct coda_ctx *ctx)
584 struct coda_iram_info *iram_info = &ctx->iram_info;
585 struct coda_dev *dev = ctx->dev;
592 memset(iram_info, 0, sizeof(*iram_info));
593 iram_info->next_paddr = dev->iram.paddr;
594 iram_info->remaining = dev->iram.size;
596 if (!dev->iram.vaddr)
599 switch (dev->devtype->product) {
601 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
602 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
603 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
606 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
607 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
608 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
610 default: /* CODA_DX6 */
614 if (ctx->inst_type == CODA_INST_ENCODER) {
615 struct coda_q_data *q_data_src;
617 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
618 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
619 w128 = mb_width * 128;
622 /* Prioritize in case IRAM is too small for everything */
623 if (dev->devtype->product == CODA_7541) {
624 iram_info->search_ram_size = round_up(mb_width * 16 *
626 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
627 iram_info->search_ram_size);
628 if (!iram_info->search_ram_paddr) {
629 pr_err("IRAM is smaller than the search ram size\n");
632 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
636 /* Only H.264BP and H.263P3 are considered */
637 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64);
638 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64);
639 if (!iram_info->buf_dbk_c_use)
641 iram_info->axi_sram_use |= dbk_bits;
643 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
644 if (!iram_info->buf_bit_use)
646 iram_info->axi_sram_use |= bit_bits;
648 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
649 if (!iram_info->buf_ip_ac_dc_use)
651 iram_info->axi_sram_use |= ip_bits;
653 /* OVL and BTP disabled for encoder */
654 } else if (ctx->inst_type == CODA_INST_DECODER) {
655 struct coda_q_data *q_data_dst;
657 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
658 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
659 w128 = mb_width * 128;
661 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128);
662 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128);
663 if (!iram_info->buf_dbk_c_use)
665 iram_info->axi_sram_use |= dbk_bits;
667 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
668 if (!iram_info->buf_bit_use)
670 iram_info->axi_sram_use |= bit_bits;
672 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
673 if (!iram_info->buf_ip_ac_dc_use)
675 iram_info->axi_sram_use |= ip_bits;
677 /* OVL and BTP unused as there is no VC1 support yet */
681 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
682 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
683 "IRAM smaller than needed\n");
685 if (dev->devtype->product == CODA_7541) {
686 /* TODO - Enabling these causes picture errors on CODA7541 */
687 if (ctx->inst_type == CODA_INST_DECODER) {
689 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
690 CODA7_USE_IP_ENABLE);
693 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
694 CODA7_USE_HOST_DBK_ENABLE |
695 CODA7_USE_IP_ENABLE |
696 CODA7_USE_DBK_ENABLE);
701 static u32 coda_supported_firmwares[] = {
702 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
703 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
704 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
707 static bool coda_firmware_supported(u32 vernum)
711 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
712 if (vernum == coda_supported_firmwares[i])
717 int coda_check_firmware(struct coda_dev *dev)
719 u16 product, major, minor, release;
723 ret = clk_prepare_enable(dev->clk_per);
727 ret = clk_prepare_enable(dev->clk_ahb);
731 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
732 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
733 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
734 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
735 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
736 if (coda_wait_timeout(dev)) {
737 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
742 if (dev->devtype->product == CODA_960) {
743 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
744 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
748 /* Check we are compatible with the loaded firmware */
749 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
750 product = CODA_FIRMWARE_PRODUCT(data);
751 major = CODA_FIRMWARE_MAJOR(data);
752 minor = CODA_FIRMWARE_MINOR(data);
753 release = CODA_FIRMWARE_RELEASE(data);
755 clk_disable_unprepare(dev->clk_per);
756 clk_disable_unprepare(dev->clk_ahb);
758 if (product != dev->devtype->product) {
759 v4l2_err(&dev->v4l2_dev,
760 "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n",
761 coda_product_name(dev->devtype->product),
762 coda_product_name(product), major, minor, release);
766 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
767 coda_product_name(product));
769 if (coda_firmware_supported(data)) {
770 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
771 major, minor, release);
773 v4l2_warn(&dev->v4l2_dev,
774 "Unsupported firmware version: %u.%u.%u\n",
775 major, minor, release);
781 clk_disable_unprepare(dev->clk_ahb);
783 clk_disable_unprepare(dev->clk_per);
788 static void coda9_set_frame_cache(struct coda_ctx *ctx, u32 fourcc)
790 u32 cache_size, cache_config;
792 if (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) {
793 /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */
794 cache_size = 0x20262024;
795 cache_config = 2 << CODA9_CACHE_PAGEMERGE_OFFSET;
797 /* Luma 0x2 page, 4x4 cache, chroma 0x2 page, 4x3 cache size */
798 cache_size = 0x02440243;
799 cache_config = 1 << CODA9_CACHE_PAGEMERGE_OFFSET;
801 coda_write(ctx->dev, cache_size, CODA9_CMD_SET_FRAME_CACHE_SIZE);
802 if (fourcc == V4L2_PIX_FMT_NV12 || fourcc == V4L2_PIX_FMT_YUYV) {
803 cache_config |= 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
804 16 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET |
805 0 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET;
807 cache_config |= 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
808 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET |
809 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET;
811 coda_write(ctx->dev, cache_config, CODA9_CMD_SET_FRAME_CACHE_CONFIG);
815 * Encoder context operations
818 static int coda_encoder_reqbufs(struct coda_ctx *ctx,
819 struct v4l2_requestbuffers *rb)
821 struct coda_q_data *q_data_src;
824 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
828 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
829 ret = coda_alloc_context_buffers(ctx, q_data_src);
833 coda_free_context_buffers(ctx);
839 static int coda_start_encoding(struct coda_ctx *ctx)
841 struct coda_dev *dev = ctx->dev;
842 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
843 struct coda_q_data *q_data_src, *q_data_dst;
844 u32 bitstream_buf, bitstream_size;
845 struct vb2_v4l2_buffer *buf;
846 int gamma, ret, value;
851 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
852 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
853 dst_fourcc = q_data_dst->fourcc;
855 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
856 bitstream_buf = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
857 bitstream_size = q_data_dst->sizeimage;
859 if (!coda_is_initialized(dev)) {
860 v4l2_err(v4l2_dev, "coda is not initialized.\n");
864 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
865 if (!ctx->params.jpeg_qmat_tab[0])
866 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
867 if (!ctx->params.jpeg_qmat_tab[1])
868 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
869 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
872 mutex_lock(&dev->coda_mutex);
874 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
875 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
876 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
877 switch (dev->devtype->product) {
879 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
880 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
883 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
886 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
887 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
891 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
892 CODA9_FRAME_TILED2LINEAR);
893 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12)
894 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
895 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
896 ctx->frame_mem_ctrl |= (0x3 << 9) | CODA9_FRAME_TILED2LINEAR;
897 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
899 if (dev->devtype->product == CODA_DX6) {
900 /* Configure the coda */
901 coda_write(dev, dev->iram.paddr,
902 CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
905 /* Could set rotation here if needed */
907 switch (dev->devtype->product) {
909 value = (q_data_src->width & CODADX6_PICWIDTH_MASK)
910 << CODADX6_PICWIDTH_OFFSET;
911 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK)
912 << CODA_PICHEIGHT_OFFSET;
915 if (dst_fourcc == V4L2_PIX_FMT_H264) {
916 value = (round_up(q_data_src->width, 16) &
917 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
918 value |= (round_up(q_data_src->height, 16) &
919 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
924 value = (q_data_src->width & CODA7_PICWIDTH_MASK)
925 << CODA7_PICWIDTH_OFFSET;
926 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK)
927 << CODA_PICHEIGHT_OFFSET;
929 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
930 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
931 ctx->params.framerate = 0;
932 coda_write(dev, ctx->params.framerate,
933 CODA_CMD_ENC_SEQ_SRC_F_RATE);
935 ctx->params.codec_mode = ctx->codec->mode;
936 switch (dst_fourcc) {
937 case V4L2_PIX_FMT_MPEG4:
938 if (dev->devtype->product == CODA_960)
939 coda_write(dev, CODA9_STD_MPEG4,
940 CODA_CMD_ENC_SEQ_COD_STD);
942 coda_write(dev, CODA_STD_MPEG4,
943 CODA_CMD_ENC_SEQ_COD_STD);
944 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
946 case V4L2_PIX_FMT_H264:
947 if (dev->devtype->product == CODA_960)
948 coda_write(dev, CODA9_STD_H264,
949 CODA_CMD_ENC_SEQ_COD_STD);
951 coda_write(dev, CODA_STD_H264,
952 CODA_CMD_ENC_SEQ_COD_STD);
953 if (ctx->params.h264_deblk_enabled) {
954 value = ((ctx->params.h264_deblk_alpha &
955 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
956 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
957 ((ctx->params.h264_deblk_beta &
958 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
959 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
961 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
963 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
965 case V4L2_PIX_FMT_JPEG:
966 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
967 coda_write(dev, ctx->params.jpeg_restart_interval,
968 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
969 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
970 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
971 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
973 coda_jpeg_write_tables(ctx);
977 "dst format (0x%08x) invalid.\n", dst_fourcc);
983 * slice mode and GOP size registers are used for thumb size/offset
986 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
987 switch (ctx->params.slice_mode) {
988 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
991 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
992 value = (ctx->params.slice_max_mb &
993 CODA_SLICING_SIZE_MASK)
994 << CODA_SLICING_SIZE_OFFSET;
995 value |= (1 & CODA_SLICING_UNIT_MASK)
996 << CODA_SLICING_UNIT_OFFSET;
997 value |= 1 & CODA_SLICING_MODE_MASK;
999 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1000 value = (ctx->params.slice_max_bits &
1001 CODA_SLICING_SIZE_MASK)
1002 << CODA_SLICING_SIZE_OFFSET;
1003 value |= (0 & CODA_SLICING_UNIT_MASK)
1004 << CODA_SLICING_UNIT_OFFSET;
1005 value |= 1 & CODA_SLICING_MODE_MASK;
1008 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
1009 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
1010 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1013 if (ctx->params.bitrate) {
1014 /* Rate control enabled */
1015 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK)
1016 << CODA_RATECONTROL_BITRATE_OFFSET;
1017 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1018 value |= (ctx->params.vbv_delay &
1019 CODA_RATECONTROL_INITIALDELAY_MASK)
1020 << CODA_RATECONTROL_INITIALDELAY_OFFSET;
1021 if (dev->devtype->product == CODA_960)
1022 value |= BIT(31); /* disable autoskip */
1026 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1028 coda_write(dev, ctx->params.vbv_size, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1029 coda_write(dev, ctx->params.intra_refresh,
1030 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1032 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1033 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1037 if (dev->devtype->product == CODA_960)
1038 gamma = CODA9_DEFAULT_GAMMA;
1040 gamma = CODA_DEFAULT_GAMMA;
1042 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
1043 CODA_CMD_ENC_SEQ_RC_GAMMA);
1046 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
1048 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
1049 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
1050 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
1052 if (dev->devtype->product == CODA_960) {
1053 if (ctx->params.h264_max_qp)
1054 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
1055 if (CODA_DEFAULT_GAMMA > 0)
1056 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
1058 if (CODA_DEFAULT_GAMMA > 0) {
1059 if (dev->devtype->product == CODA_DX6)
1060 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
1062 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
1064 if (ctx->params.h264_min_qp)
1065 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
1066 if (ctx->params.h264_max_qp)
1067 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
1069 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1071 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
1073 coda_setup_iram(ctx);
1075 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1076 switch (dev->devtype->product) {
1078 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
1079 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1082 coda_write(dev, ctx->iram_info.search_ram_paddr,
1083 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1084 coda_write(dev, ctx->iram_info.search_ram_size,
1085 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1088 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
1089 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
1093 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1095 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1099 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1100 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1104 ctx->initialized = 1;
1106 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
1107 if (dev->devtype->product == CODA_960)
1108 ctx->num_internal_frames = 4;
1110 ctx->num_internal_frames = 2;
1111 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1113 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1117 stride = q_data_src->bytesperline;
1119 ctx->num_internal_frames = 0;
1123 coda_write(dev, num_fb, CODA_CMD_SET_FRAME_BUF_NUM);
1124 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
1126 if (dev->devtype->product == CODA_7541) {
1127 coda_write(dev, q_data_src->bytesperline,
1128 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1130 if (dev->devtype->product != CODA_DX6) {
1131 coda_write(dev, ctx->iram_info.buf_bit_use,
1132 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1133 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1134 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1135 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1136 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1137 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1138 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1139 coda_write(dev, ctx->iram_info.buf_ovl_use,
1140 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1141 if (dev->devtype->product == CODA_960) {
1142 coda_write(dev, ctx->iram_info.buf_btp_use,
1143 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1145 coda9_set_frame_cache(ctx, q_data_src->fourcc);
1148 coda_write(dev, ctx->internal_frames[2].paddr,
1149 CODA9_CMD_SET_FRAME_SUBSAMP_A);
1150 coda_write(dev, ctx->internal_frames[3].paddr,
1151 CODA9_CMD_SET_FRAME_SUBSAMP_B);
1155 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1157 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1161 /* Save stream headers */
1162 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1163 switch (dst_fourcc) {
1164 case V4L2_PIX_FMT_H264:
1166 * Get SPS in the first frame and copy it to an
1167 * intermediate buffer.
1169 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1170 &ctx->vpu_header[0][0],
1171 &ctx->vpu_header_size[0]);
1176 * Get PPS in the first frame and copy it to an
1177 * intermediate buffer.
1179 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1180 &ctx->vpu_header[1][0],
1181 &ctx->vpu_header_size[1]);
1186 * Length of H.264 headers is variable and thus it might not be
1187 * aligned for the coda to append the encoded frame. In that is
1188 * the case a filler NAL must be added to header 2.
1190 ctx->vpu_header_size[2] = coda_h264_padding(
1191 (ctx->vpu_header_size[0] +
1192 ctx->vpu_header_size[1]),
1193 ctx->vpu_header[2]);
1195 case V4L2_PIX_FMT_MPEG4:
1197 * Get VOS in the first frame and copy it to an
1198 * intermediate buffer
1200 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1201 &ctx->vpu_header[0][0],
1202 &ctx->vpu_header_size[0]);
1206 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1207 &ctx->vpu_header[1][0],
1208 &ctx->vpu_header_size[1]);
1212 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1213 &ctx->vpu_header[2][0],
1214 &ctx->vpu_header_size[2]);
1219 /* No more formats need to save headers at the moment */
1224 mutex_unlock(&dev->coda_mutex);
1228 static int coda_prepare_encode(struct coda_ctx *ctx)
1230 struct coda_q_data *q_data_src, *q_data_dst;
1231 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1232 struct coda_dev *dev = ctx->dev;
1234 int quant_param = 0;
1235 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1240 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1241 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1242 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1243 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1244 dst_fourcc = q_data_dst->fourcc;
1246 src_buf->sequence = ctx->osequence;
1247 dst_buf->sequence = ctx->osequence;
1251 * Workaround coda firmware BUG that only marks the first
1252 * frame as IDR. This is a problem for some decoders that can't
1253 * recover when a frame is lost.
1255 if (src_buf->sequence % ctx->params.gop_size) {
1256 src_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1257 src_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1259 src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1260 src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
1263 if (dev->devtype->product == CODA_960)
1264 coda_set_gdi_regs(ctx);
1267 * Copy headers at the beginning of the first frame for H.264 only.
1268 * In MPEG4 they are already copied by the coda.
1270 if (src_buf->sequence == 0) {
1271 pic_stream_buffer_addr =
1272 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0) +
1273 ctx->vpu_header_size[0] +
1274 ctx->vpu_header_size[1] +
1275 ctx->vpu_header_size[2];
1276 pic_stream_buffer_size = q_data_dst->sizeimage -
1277 ctx->vpu_header_size[0] -
1278 ctx->vpu_header_size[1] -
1279 ctx->vpu_header_size[2];
1280 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0),
1281 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1282 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0)
1283 + ctx->vpu_header_size[0], &ctx->vpu_header[1][0],
1284 ctx->vpu_header_size[1]);
1285 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0)
1286 + ctx->vpu_header_size[0] + ctx->vpu_header_size[1],
1287 &ctx->vpu_header[2][0], ctx->vpu_header_size[2]);
1289 pic_stream_buffer_addr =
1290 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1291 pic_stream_buffer_size = q_data_dst->sizeimage;
1294 if (src_buf->flags & V4L2_BUF_FLAG_KEYFRAME) {
1296 switch (dst_fourcc) {
1297 case V4L2_PIX_FMT_H264:
1298 quant_param = ctx->params.h264_intra_qp;
1300 case V4L2_PIX_FMT_MPEG4:
1301 quant_param = ctx->params.mpeg4_intra_qp;
1303 case V4L2_PIX_FMT_JPEG:
1307 v4l2_warn(&ctx->dev->v4l2_dev,
1308 "cannot set intra qp, fmt not supported\n");
1313 switch (dst_fourcc) {
1314 case V4L2_PIX_FMT_H264:
1315 quant_param = ctx->params.h264_inter_qp;
1317 case V4L2_PIX_FMT_MPEG4:
1318 quant_param = ctx->params.mpeg4_inter_qp;
1321 v4l2_warn(&ctx->dev->v4l2_dev,
1322 "cannot set inter qp, fmt not supported\n");
1328 if (ctx->params.rot_mode)
1329 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1330 coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
1331 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1333 if (dev->devtype->product == CODA_960) {
1334 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1335 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1336 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1338 reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y;
1340 reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y;
1342 coda_write_base(ctx, q_data_src, src_buf, reg);
1344 coda_write(dev, force_ipicture << 1 & 0x2,
1345 CODA_CMD_ENC_PIC_OPTION);
1347 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1348 coda_write(dev, pic_stream_buffer_size / 1024,
1349 CODA_CMD_ENC_PIC_BB_SIZE);
1351 if (!ctx->streamon_out) {
1352 /* After streamoff on the output side, set stream end flag */
1353 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1354 coda_write(dev, ctx->bit_stream_param,
1355 CODA_REG_BIT_BIT_STREAM_PARAM);
1358 if (dev->devtype->product != CODA_DX6)
1359 coda_write(dev, ctx->iram_info.axi_sram_use,
1360 CODA7_REG_BIT_AXI_SRAM_USE);
1362 trace_coda_enc_pic_run(ctx, src_buf);
1364 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1369 static void coda_finish_encode(struct coda_ctx *ctx)
1371 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1372 struct coda_dev *dev = ctx->dev;
1373 u32 wr_ptr, start_ptr;
1375 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1376 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1378 trace_coda_enc_pic_done(ctx, dst_buf);
1380 /* Get results from the coda */
1381 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1382 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1384 /* Calculate bytesused field */
1385 if (dst_buf->sequence == 0) {
1386 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr +
1387 ctx->vpu_header_size[0] +
1388 ctx->vpu_header_size[1] +
1389 ctx->vpu_header_size[2]);
1391 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr);
1394 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1395 wr_ptr - start_ptr);
1397 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1398 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1400 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
1401 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1402 dst_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
1404 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1405 dst_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1408 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1409 dst_buf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1411 src_buf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1412 dst_buf->timecode = src_buf->timecode;
1414 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1416 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1417 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE);
1420 if (ctx->gopcounter < 0)
1421 ctx->gopcounter = ctx->params.gop_size - 1;
1423 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1424 "job finished: encoding frame (%d) (%s)\n",
1426 (dst_buf->flags & V4L2_BUF_FLAG_KEYFRAME) ?
1427 "KEYFRAME" : "PFRAME");
1430 static void coda_seq_end_work(struct work_struct *work)
1432 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1433 struct coda_dev *dev = ctx->dev;
1435 mutex_lock(&ctx->buffer_mutex);
1436 mutex_lock(&dev->coda_mutex);
1438 if (ctx->initialized == 0)
1441 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1442 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx,
1444 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1445 v4l2_err(&dev->v4l2_dev,
1446 "CODA_COMMAND_SEQ_END failed\n");
1450 * FIXME: Sometimes h.264 encoding fails with 8-byte sequences missing
1451 * from the output stream after the h.264 decoder has run. Resetting the
1452 * hardware after the decoder has finished seems to help.
1454 if (dev->devtype->product == CODA_960)
1457 kfifo_init(&ctx->bitstream_fifo,
1458 ctx->bitstream.vaddr, ctx->bitstream.size);
1460 coda_free_framebuffers(ctx);
1462 ctx->initialized = 0;
1465 mutex_unlock(&dev->coda_mutex);
1466 mutex_unlock(&ctx->buffer_mutex);
1469 static void coda_bit_release(struct coda_ctx *ctx)
1471 mutex_lock(&ctx->buffer_mutex);
1472 coda_free_framebuffers(ctx);
1473 coda_free_context_buffers(ctx);
1474 coda_free_bitstream_buffer(ctx);
1475 mutex_unlock(&ctx->buffer_mutex);
1478 const struct coda_context_ops coda_bit_encode_ops = {
1479 .queue_init = coda_encoder_queue_init,
1480 .reqbufs = coda_encoder_reqbufs,
1481 .start_streaming = coda_start_encoding,
1482 .prepare_run = coda_prepare_encode,
1483 .finish_run = coda_finish_encode,
1484 .seq_end_work = coda_seq_end_work,
1485 .release = coda_bit_release,
1489 * Decoder context operations
1492 static int coda_alloc_bitstream_buffer(struct coda_ctx *ctx,
1493 struct coda_q_data *q_data)
1495 if (ctx->bitstream.vaddr)
1498 ctx->bitstream.size = roundup_pow_of_two(q_data->sizeimage * 2);
1499 ctx->bitstream.vaddr = dma_alloc_wc(&ctx->dev->plat_dev->dev,
1500 ctx->bitstream.size,
1501 &ctx->bitstream.paddr, GFP_KERNEL);
1502 if (!ctx->bitstream.vaddr) {
1503 v4l2_err(&ctx->dev->v4l2_dev,
1504 "failed to allocate bitstream ringbuffer");
1507 kfifo_init(&ctx->bitstream_fifo,
1508 ctx->bitstream.vaddr, ctx->bitstream.size);
1513 static void coda_free_bitstream_buffer(struct coda_ctx *ctx)
1515 if (ctx->bitstream.vaddr == NULL)
1518 dma_free_wc(&ctx->dev->plat_dev->dev, ctx->bitstream.size,
1519 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1520 ctx->bitstream.vaddr = NULL;
1521 kfifo_init(&ctx->bitstream_fifo, NULL, 0);
1524 static int coda_decoder_reqbufs(struct coda_ctx *ctx,
1525 struct v4l2_requestbuffers *rb)
1527 struct coda_q_data *q_data_src;
1530 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1534 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1535 ret = coda_alloc_context_buffers(ctx, q_data_src);
1538 ret = coda_alloc_bitstream_buffer(ctx, q_data_src);
1540 coda_free_context_buffers(ctx);
1544 coda_free_bitstream_buffer(ctx);
1545 coda_free_context_buffers(ctx);
1551 static bool coda_reorder_enable(struct coda_ctx *ctx)
1553 const char * const *profile_names;
1554 const char * const *level_names;
1555 struct coda_dev *dev = ctx->dev;
1558 if (dev->devtype->product != CODA_7541 &&
1559 dev->devtype->product != CODA_960)
1562 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1565 if (ctx->codec->src_fourcc != V4L2_PIX_FMT_H264)
1568 profile = coda_h264_profile(ctx->params.h264_profile_idc);
1570 v4l2_warn(&dev->v4l2_dev, "Invalid H264 Profile: %d\n",
1571 ctx->params.h264_profile_idc);
1575 level = coda_h264_level(ctx->params.h264_level_idc);
1577 v4l2_warn(&dev->v4l2_dev, "Invalid H264 Level: %d\n",
1578 ctx->params.h264_level_idc);
1582 profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE);
1583 level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL);
1585 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "H264 Profile/Level: %s L%s\n",
1586 profile_names[profile], level_names[level]);
1588 /* Baseline profile does not support reordering */
1589 return profile > V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
1592 static int __coda_start_decoding(struct coda_ctx *ctx)
1594 struct coda_q_data *q_data_src, *q_data_dst;
1595 u32 bitstream_buf, bitstream_size;
1596 struct coda_dev *dev = ctx->dev;
1598 u32 src_fourcc, dst_fourcc;
1602 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1603 "Video Data Order Adapter: %s\n",
1604 ctx->use_vdoa ? "Enabled" : "Disabled");
1606 /* Start decoding */
1607 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1608 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1609 bitstream_buf = ctx->bitstream.paddr;
1610 bitstream_size = ctx->bitstream.size;
1611 src_fourcc = q_data_src->fourcc;
1612 dst_fourcc = q_data_dst->fourcc;
1614 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1616 /* Update coda bitstream read and write pointers from kfifo */
1617 coda_kfifo_sync_to_device_full(ctx);
1619 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) |
1620 CODA9_FRAME_TILED2LINEAR);
1621 if (dst_fourcc == V4L2_PIX_FMT_NV12 || dst_fourcc == V4L2_PIX_FMT_YUYV)
1622 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
1623 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
1624 ctx->frame_mem_ctrl |= (0x3 << 9) |
1625 ((ctx->use_vdoa) ? 0 : CODA9_FRAME_TILED2LINEAR);
1626 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1628 ctx->display_idx = -1;
1629 ctx->frm_dis_flg = 0;
1630 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1632 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1633 CODA_REG_BIT_BIT_STREAM_PARAM);
1635 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1636 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1638 if (coda_reorder_enable(ctx))
1639 val |= CODA_REORDER_ENABLE;
1640 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1641 val |= CODA_NO_INT_ENABLE;
1642 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1644 ctx->params.codec_mode = ctx->codec->mode;
1645 if (dev->devtype->product == CODA_960 &&
1646 src_fourcc == V4L2_PIX_FMT_MPEG4)
1647 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1649 ctx->params.codec_mode_aux = 0;
1650 if (src_fourcc == V4L2_PIX_FMT_H264) {
1651 if (dev->devtype->product == CODA_7541) {
1652 coda_write(dev, ctx->psbuf.paddr,
1653 CODA_CMD_DEC_SEQ_PS_BB_START);
1654 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1655 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1657 if (dev->devtype->product == CODA_960) {
1658 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1659 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1662 if (dev->devtype->product != CODA_960)
1663 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
1665 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1666 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1667 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1670 ctx->initialized = 1;
1672 /* Update kfifo out pointer from coda bitstream read pointer */
1673 coda_kfifo_sync_from_device(ctx);
1675 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1677 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1678 v4l2_err(&dev->v4l2_dev,
1679 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1680 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1684 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1685 if (dev->devtype->product == CODA_DX6) {
1686 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1687 height = val & CODADX6_PICHEIGHT_MASK;
1689 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1690 height = val & CODA7_PICHEIGHT_MASK;
1693 if (width > q_data_dst->bytesperline || height > q_data_dst->height) {
1694 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1695 width, height, q_data_dst->bytesperline,
1696 q_data_dst->height);
1700 width = round_up(width, 16);
1701 height = round_up(height, 16);
1703 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1704 __func__, ctx->idx, width, height);
1706 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
1708 * If the VDOA is used, the decoder needs one additional frame,
1709 * because the frames are freed when the next frame is decoded.
1710 * Otherwise there are visible errors in the decoded frames (green
1711 * regions in displayed frames) and a broken order of frames (earlier
1712 * frames are sporadically displayed after later frames).
1715 ctx->num_internal_frames += 1;
1716 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1717 v4l2_err(&dev->v4l2_dev,
1718 "not enough framebuffers to decode (%d < %d)\n",
1719 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1723 if (src_fourcc == V4L2_PIX_FMT_H264) {
1727 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1728 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1730 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1731 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1732 q_data_dst->rect.width = width - q_data_dst->rect.left -
1733 (left_right & 0x3ff);
1734 q_data_dst->rect.height = height - q_data_dst->rect.top -
1735 (top_bottom & 0x3ff);
1738 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1740 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n");
1744 /* Tell the decoder how many frame buffers we allocated. */
1745 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1746 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1748 if (dev->devtype->product != CODA_DX6) {
1749 /* Set secondary AXI IRAM */
1750 coda_setup_iram(ctx);
1752 coda_write(dev, ctx->iram_info.buf_bit_use,
1753 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1754 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1755 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1756 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1757 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1758 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1759 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1760 coda_write(dev, ctx->iram_info.buf_ovl_use,
1761 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1762 if (dev->devtype->product == CODA_960) {
1763 coda_write(dev, ctx->iram_info.buf_btp_use,
1764 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1766 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
1767 coda9_set_frame_cache(ctx, dst_fourcc);
1771 if (src_fourcc == V4L2_PIX_FMT_H264) {
1772 coda_write(dev, ctx->slicebuf.paddr,
1773 CODA_CMD_SET_FRAME_SLICE_BB_START);
1774 coda_write(dev, ctx->slicebuf.size / 1024,
1775 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1778 if (dev->devtype->product == CODA_7541) {
1779 int max_mb_x = 1920 / 16;
1780 int max_mb_y = 1088 / 16;
1781 int max_mb_num = max_mb_x * max_mb_y;
1783 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1784 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1785 } else if (dev->devtype->product == CODA_960) {
1786 int max_mb_x = 1920 / 16;
1787 int max_mb_y = 1088 / 16;
1788 int max_mb_num = max_mb_x * max_mb_y;
1790 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1791 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
1794 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1795 v4l2_err(&ctx->dev->v4l2_dev,
1796 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1803 static int coda_start_decoding(struct coda_ctx *ctx)
1805 struct coda_dev *dev = ctx->dev;
1808 mutex_lock(&dev->coda_mutex);
1809 ret = __coda_start_decoding(ctx);
1810 mutex_unlock(&dev->coda_mutex);
1815 static int coda_prepare_decode(struct coda_ctx *ctx)
1817 struct vb2_v4l2_buffer *dst_buf;
1818 struct coda_dev *dev = ctx->dev;
1819 struct coda_q_data *q_data_dst;
1820 struct coda_buffer_meta *meta;
1821 unsigned long flags;
1823 u32 reg_addr, reg_stride;
1825 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1826 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1828 /* Try to copy source buffer contents into the bitstream ringbuffer */
1829 mutex_lock(&ctx->bitstream_mutex);
1830 coda_fill_bitstream(ctx, NULL);
1831 mutex_unlock(&ctx->bitstream_mutex);
1833 if (coda_get_bitstream_payload(ctx) < 512 &&
1834 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1835 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1836 "bitstream payload: %d, skipping\n",
1837 coda_get_bitstream_payload(ctx));
1838 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1842 /* Run coda_start_decoding (again) if not yet initialized */
1843 if (!ctx->initialized) {
1844 int ret = __coda_start_decoding(ctx);
1847 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1848 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1851 ctx->initialized = 1;
1855 if (dev->devtype->product == CODA_960)
1856 coda_set_gdi_regs(ctx);
1858 if (ctx->use_vdoa &&
1859 ctx->display_idx >= 0 &&
1860 ctx->display_idx < ctx->num_internal_frames) {
1861 vdoa_device_run(ctx->vdoa,
1862 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0),
1863 ctx->internal_frames[ctx->display_idx].paddr);
1865 if (dev->devtype->product == CODA_960) {
1867 * The CODA960 seems to have an internal list of
1868 * buffers with 64 entries that includes the
1869 * registered frame buffers as well as the rotator
1872 * ROT_INDEX needs to be < 0x40, but >
1873 * ctx->num_internal_frames.
1876 CODA_MAX_FRAMEBUFFERS + dst_buf->vb2_buf.index,
1877 CODA9_CMD_DEC_PIC_ROT_INDEX);
1879 reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y;
1880 reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE;
1882 reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y;
1883 reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE;
1885 coda_write_base(ctx, q_data_dst, dst_buf, reg_addr);
1886 coda_write(dev, q_data_dst->bytesperline, reg_stride);
1888 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1891 coda_write(dev, rot_mode, CODA_CMD_DEC_PIC_ROT_MODE);
1893 switch (dev->devtype->product) {
1897 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1900 /* 'hardcode to use interrupt disable mode'? */
1901 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION);
1905 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1907 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1908 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1910 if (dev->devtype->product != CODA_DX6)
1911 coda_write(dev, ctx->iram_info.axi_sram_use,
1912 CODA7_REG_BIT_AXI_SRAM_USE);
1914 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1915 meta = list_first_entry_or_null(&ctx->buffer_meta_list,
1916 struct coda_buffer_meta, list);
1918 if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) {
1920 /* If this is the last buffer in the bitstream, add padding */
1921 if (meta->end == (ctx->bitstream_fifo.kfifo.in &
1922 ctx->bitstream_fifo.kfifo.mask)) {
1923 static unsigned char buf[512];
1926 /* Pad to multiple of 256 and then add 256 more */
1927 pad = ((0 - meta->end) & 0xff) + 256;
1929 memset(buf, 0xff, sizeof(buf));
1931 kfifo_in(&ctx->bitstream_fifo, buf, pad);
1934 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1936 coda_kfifo_sync_to_device_full(ctx);
1938 /* Clear decode success flag */
1939 coda_write(dev, 0, CODA_RET_DEC_PIC_SUCCESS);
1941 trace_coda_dec_pic_run(ctx, meta);
1943 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1948 static void coda_finish_decode(struct coda_ctx *ctx)
1950 struct coda_dev *dev = ctx->dev;
1951 struct coda_q_data *q_data_src;
1952 struct coda_q_data *q_data_dst;
1953 struct vb2_v4l2_buffer *dst_buf;
1954 struct coda_buffer_meta *meta;
1955 unsigned long payload;
1956 unsigned long flags;
1966 /* Update kfifo out pointer from coda bitstream read pointer */
1967 coda_kfifo_sync_from_device(ctx);
1970 * in stream-end mode, the read pointer can overshoot the write pointer
1971 * by up to 512 bytes
1973 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
1974 if (coda_get_bitstream_payload(ctx) >= ctx->bitstream.size - 512)
1975 kfifo_init(&ctx->bitstream_fifo,
1976 ctx->bitstream.vaddr, ctx->bitstream.size);
1979 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1980 src_fourcc = q_data_src->fourcc;
1982 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
1984 pr_err("DEC_PIC_SUCCESS = %d\n", val);
1986 success = val & 0x1;
1988 v4l2_err(&dev->v4l2_dev, "decode failed\n");
1990 if (src_fourcc == V4L2_PIX_FMT_H264) {
1992 v4l2_err(&dev->v4l2_dev,
1993 "insufficient PS buffer space (%d bytes)\n",
1996 v4l2_err(&dev->v4l2_dev,
1997 "insufficient slice buffer space (%d bytes)\n",
1998 ctx->slicebuf.size);
2001 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2002 width = (val >> 16) & 0xffff;
2003 height = val & 0xffff;
2005 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2007 /* frame crop information */
2008 if (src_fourcc == V4L2_PIX_FMT_H264) {
2012 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
2013 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
2015 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
2016 /* Keep current crop information */
2018 struct v4l2_rect *rect = &q_data_dst->rect;
2020 rect->left = left_right >> 16 & 0xffff;
2021 rect->top = top_bottom >> 16 & 0xffff;
2022 rect->width = width - rect->left -
2023 (left_right & 0xffff);
2024 rect->height = height - rect->top -
2025 (top_bottom & 0xffff);
2031 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2033 v4l2_err(&dev->v4l2_dev,
2034 "errors in %d macroblocks\n", err_mb);
2036 if (dev->devtype->product == CODA_7541) {
2037 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2039 /* not enough bitstream data */
2040 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2041 "prescan failed: %d\n", val);
2047 /* Wait until the VDOA finished writing the previous display frame */
2048 if (ctx->use_vdoa &&
2049 ctx->display_idx >= 0 &&
2050 ctx->display_idx < ctx->num_internal_frames) {
2051 err_vdoa = vdoa_wait_for_completion(ctx->vdoa);
2054 ctx->frm_dis_flg = coda_read(dev,
2055 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2057 /* The previous display frame was copied out and can be overwritten */
2058 if (ctx->display_idx >= 0 &&
2059 ctx->display_idx < ctx->num_internal_frames) {
2060 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
2061 coda_write(dev, ctx->frm_dis_flg,
2062 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2066 * The index of the last decoded frame, not necessarily in
2067 * display order, and the index of the next display frame.
2068 * The latter could have been decoded in a previous run.
2070 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
2071 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
2073 if (decoded_idx == -1) {
2074 /* no frame was decoded, but we might have a display frame */
2075 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
2076 ctx->sequence_offset++;
2077 else if (ctx->display_idx < 0)
2079 } else if (decoded_idx == -2) {
2080 /* no frame was decoded, we still return remaining buffers */
2081 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
2082 v4l2_err(&dev->v4l2_dev,
2083 "decoded frame index out of range: %d\n", decoded_idx);
2085 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
2086 val -= ctx->sequence_offset;
2087 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
2088 if (!list_empty(&ctx->buffer_meta_list)) {
2089 meta = list_first_entry(&ctx->buffer_meta_list,
2090 struct coda_buffer_meta, list);
2091 list_del(&meta->list);
2093 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
2095 * Clamp counters to 16 bits for comparison, as the HW
2096 * counter rolls over at this point for h.264. This
2097 * may be different for other formats, but using 16 bits
2098 * should be enough to detect most errors and saves us
2099 * from doing different things based on the format.
2101 if ((val & 0xffff) != (meta->sequence & 0xffff)) {
2102 v4l2_err(&dev->v4l2_dev,
2103 "sequence number mismatch (%d(%d) != %d)\n",
2104 val, ctx->sequence_offset,
2107 ctx->frame_metas[decoded_idx] = *meta;
2110 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
2111 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n");
2112 memset(&ctx->frame_metas[decoded_idx], 0,
2113 sizeof(struct coda_buffer_meta));
2114 ctx->frame_metas[decoded_idx].sequence = val;
2115 ctx->sequence_offset++;
2118 trace_coda_dec_pic_done(ctx, &ctx->frame_metas[decoded_idx]);
2120 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
2122 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
2124 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
2126 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
2128 ctx->frame_errors[decoded_idx] = err_mb;
2131 if (display_idx == -1) {
2133 * no more frames to be decoded, but there could still
2134 * be rotator output to dequeue
2137 } else if (display_idx == -3) {
2138 /* possibly prescan failure */
2139 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
2140 v4l2_err(&dev->v4l2_dev,
2141 "presentation frame index out of range: %d\n",
2145 /* If a frame was copied out, return it */
2146 if (ctx->display_idx >= 0 &&
2147 ctx->display_idx < ctx->num_internal_frames) {
2148 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
2149 dst_buf->sequence = ctx->osequence++;
2151 dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
2152 V4L2_BUF_FLAG_PFRAME |
2153 V4L2_BUF_FLAG_BFRAME);
2154 dst_buf->flags |= ctx->frame_types[ctx->display_idx];
2155 meta = &ctx->frame_metas[ctx->display_idx];
2156 dst_buf->timecode = meta->timecode;
2157 dst_buf->vb2_buf.timestamp = meta->timestamp;
2159 trace_coda_dec_rot_done(ctx, dst_buf, meta);
2161 switch (q_data_dst->fourcc) {
2162 case V4L2_PIX_FMT_YUYV:
2163 payload = width * height * 2;
2165 case V4L2_PIX_FMT_YUV420:
2166 case V4L2_PIX_FMT_YVU420:
2167 case V4L2_PIX_FMT_NV12:
2169 payload = width * height * 3 / 2;
2171 case V4L2_PIX_FMT_YUV422P:
2172 payload = width * height * 2;
2175 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
2177 if (ctx->frame_errors[ctx->display_idx] || err_vdoa)
2178 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_ERROR);
2180 coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE);
2182 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2183 "job finished: decoding frame (%d) (%s)\n",
2185 (dst_buf->flags & V4L2_BUF_FLAG_KEYFRAME) ?
2186 "KEYFRAME" : "PFRAME");
2188 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2189 "job finished: no frame decoded\n");
2192 /* The rotator will copy the current display frame next time */
2193 ctx->display_idx = display_idx;
2196 const struct coda_context_ops coda_bit_decode_ops = {
2197 .queue_init = coda_decoder_queue_init,
2198 .reqbufs = coda_decoder_reqbufs,
2199 .start_streaming = coda_start_decoding,
2200 .prepare_run = coda_prepare_decode,
2201 .finish_run = coda_finish_decode,
2202 .seq_end_work = coda_seq_end_work,
2203 .release = coda_bit_release,
2206 irqreturn_t coda_irq_handler(int irq, void *data)
2208 struct coda_dev *dev = data;
2209 struct coda_ctx *ctx;
2211 /* read status register to attend the IRQ */
2212 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2213 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2214 CODA_REG_BIT_INT_CLEAR);
2216 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2218 v4l2_err(&dev->v4l2_dev,
2219 "Instance released before the end of transaction\n");
2220 mutex_unlock(&dev->coda_mutex);
2224 trace_coda_bit_done(ctx);
2226 if (ctx->aborting) {
2227 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2228 "task has been aborted\n");
2231 if (coda_isbusy(ctx->dev)) {
2232 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2233 "coda is still busy!!!!\n");
2237 complete(&ctx->completion);