Lines Matching +full:vpu +full:- +full:side

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Coda multi-standard codec IP - BIT processor functions
6 * Javier Martin, <javier.martin@vista-silicon.com>
8 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-mem2mem.h>
25 #include <media/videobuf2-v4l2.h>
26 #include <media/videobuf2-dma-contig.h>
27 #include <media/videobuf2-vmalloc.h>
30 #include "imx-vdoa.h"
59 return -ETIMEDOUT; in coda_wait_timeout()
66 struct coda_dev *dev = ctx->dev; in coda_command_async()
68 if (dev->devtype->product == CODA_HX4 || in coda_command_async()
69 dev->devtype->product == CODA_7541 || in coda_command_async()
70 dev->devtype->product == CODA_960) { in coda_command_async()
72 coda_write(dev, ctx->bit_stream_param, in coda_command_async()
74 coda_write(dev, ctx->frm_dis_flg, in coda_command_async()
75 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); in coda_command_async()
76 coda_write(dev, ctx->frame_mem_ctrl, in coda_command_async()
78 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR); in coda_command_async()
81 if (dev->devtype->product == CODA_960) { in coda_command_async()
88 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX); in coda_command_async()
89 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD); in coda_command_async()
90 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD); in coda_command_async()
99 struct coda_dev *dev = ctx->dev; in coda_command_sync()
102 lockdep_assert_held(&dev->coda_mutex); in coda_command_sync()
113 struct coda_dev *dev = ctx->dev; in coda_hw_reset()
118 lockdep_assert_held(&dev->coda_mutex); in coda_hw_reset()
120 if (!dev->rstc) in coda_hw_reset()
121 return -ENOENT; in coda_hw_reset()
125 if (dev->devtype->product == CODA_960) { in coda_hw_reset()
130 return -ETIME; in coda_hw_reset()
135 ret = reset_control_reset(dev->rstc); in coda_hw_reset()
139 if (dev->devtype->product == CODA_960) in coda_hw_reset()
151 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; in coda_kfifo_sync_from_device()
152 struct coda_dev *dev = ctx->dev; in coda_kfifo_sync_from_device()
155 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); in coda_kfifo_sync_from_device()
156 kfifo->out = (kfifo->in & ~kfifo->mask) | in coda_kfifo_sync_from_device()
157 (rd_ptr - ctx->bitstream.paddr); in coda_kfifo_sync_from_device()
158 if (kfifo->out > kfifo->in) in coda_kfifo_sync_from_device()
159 kfifo->out -= kfifo->mask + 1; in coda_kfifo_sync_from_device()
164 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; in coda_kfifo_sync_to_device_full()
165 struct coda_dev *dev = ctx->dev; in coda_kfifo_sync_to_device_full()
168 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask); in coda_kfifo_sync_to_device_full()
169 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); in coda_kfifo_sync_to_device_full()
170 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask); in coda_kfifo_sync_to_device_full()
171 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); in coda_kfifo_sync_to_device_full()
176 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; in coda_kfifo_sync_to_device_write()
177 struct coda_dev *dev = ctx->dev; in coda_kfifo_sync_to_device_write()
180 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask); in coda_kfifo_sync_to_device_write()
181 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); in coda_kfifo_sync_to_device_write()
194 return -ENOMEM; in coda_h264_bitstream_pad()
197 n = kfifo_in(&ctx->bitstream_fifo, buf, size); in coda_h264_bitstream_pad()
200 return (n < size) ? -ENOSPC : 0; in coda_h264_bitstream_pad()
207 if (ctx->inst_type != CODA_INST_DECODER || !ctx->use_bit) in coda_bitstream_flush()
212 v4l2_err(&ctx->dev->v4l2_dev, "failed to flush bitstream\n"); in coda_bitstream_flush()
216 kfifo_init(&ctx->bitstream_fifo, ctx->bitstream.vaddr, in coda_bitstream_flush()
217 ctx->bitstream.size); in coda_bitstream_flush()
225 u32 n = kfifo_in(&ctx->bitstream_fifo, buf, size); in coda_bitstream_queue()
227 return (n < size) ? -ENOSPC : 0; in coda_bitstream_queue()
234 u8 *vaddr = vb2_plane_vaddr(&src_buf->vb2_buf, 0); in coda_buffer_parse_headers()
237 switch (ctx->codec->src_fourcc) { in coda_buffer_parse_headers()
254 unsigned long payload = vb2_get_plane_payload(&src_buf->vb2_buf, 0); in coda_bitstream_try_queue()
255 u8 *vaddr = vb2_plane_vaddr(&src_buf->vb2_buf, 0); in coda_bitstream_try_queue()
260 ctx->bitstream.size) in coda_bitstream_try_queue()
264 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n"); in coda_bitstream_try_queue()
268 if (ctx->qsequence == 0 && payload < 512) { in coda_bitstream_try_queue()
280 coda_dbg(1, ctx, "pad with %u-byte header\n", in coda_bitstream_try_queue()
286 v4l2_err(&ctx->dev->v4l2_dev, in coda_bitstream_try_queue()
290 if (ctx->dev->devtype->product == CODA_960) in coda_bitstream_try_queue()
299 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264) in coda_bitstream_try_queue()
300 coda_h264_bitstream_pad(ctx, 512 - payload); in coda_bitstream_try_queue()
305 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n"); in coda_bitstream_try_queue()
309 src_buf->sequence = ctx->qsequence++; in coda_bitstream_try_queue()
312 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev)) in coda_bitstream_try_queue()
315 /* Set the stream-end flag after the last buffer is queued */ in coda_bitstream_try_queue()
316 if (src_buf->flags & V4L2_BUF_FLAG_LAST) in coda_bitstream_try_queue()
318 ctx->hold = false; in coda_bitstream_try_queue()
329 lockdep_assert_held(&ctx->bitstream_mutex); in coda_fill_bitstream()
331 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) in coda_fill_bitstream()
334 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) { in coda_fill_bitstream()
340 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG && in coda_fill_bitstream()
341 ctx->num_metas > 1) in coda_fill_bitstream()
344 if (ctx->num_internal_frames && in coda_fill_bitstream()
345 ctx->num_metas >= ctx->num_internal_frames) { in coda_fill_bitstream()
346 meta = list_first_entry(&ctx->buffer_meta_list, in coda_fill_bitstream()
357 if (coda_bitstream_can_fetch_past(ctx, meta->end)) in coda_fill_bitstream()
361 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); in coda_fill_bitstream()
364 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG && in coda_fill_bitstream()
365 !coda_jpeg_check_buffer(ctx, &src_buf->vb2_buf)) { in coda_fill_bitstream()
366 v4l2_err(&ctx->dev->v4l2_dev, in coda_fill_bitstream()
368 ctx->qsequence); in coda_fill_bitstream()
369 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); in coda_fill_bitstream()
376 list_add_tail(&m2m_buf->list, buffer_list); in coda_fill_bitstream()
384 if (!vb2_get_plane_payload(&src_buf->vb2_buf, 0)) { in coda_fill_bitstream()
385 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); in coda_fill_bitstream()
391 start = ctx->bitstream_fifo.kfifo.in; in coda_fill_bitstream()
398 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); in coda_fill_bitstream()
402 meta->sequence = src_buf->sequence; in coda_fill_bitstream()
403 meta->timecode = src_buf->timecode; in coda_fill_bitstream()
404 meta->timestamp = src_buf->vb2_buf.timestamp; in coda_fill_bitstream()
405 meta->start = start; in coda_fill_bitstream()
406 meta->end = ctx->bitstream_fifo.kfifo.in; in coda_fill_bitstream()
407 meta->last = src_buf->flags & V4L2_BUF_FLAG_LAST; in coda_fill_bitstream()
408 if (meta->last) in coda_fill_bitstream()
410 spin_lock(&ctx->buffer_meta_lock); in coda_fill_bitstream()
411 list_add_tail(&meta->list, in coda_fill_bitstream()
412 &ctx->buffer_meta_list); in coda_fill_bitstream()
413 ctx->num_metas++; in coda_fill_bitstream()
414 spin_unlock(&ctx->buffer_meta_lock); in coda_fill_bitstream()
425 list_add_tail(&m2m_buf->list, buffer_list); in coda_fill_bitstream()
437 struct coda_dev *dev = ctx->dev; in coda_bit_stream_end_flag()
439 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; in coda_bit_stream_end_flag()
442 if ((dev->devtype->product == CODA_960) && in coda_bit_stream_end_flag()
444 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) { in coda_bit_stream_end_flag()
445 coda_write(dev, ctx->bit_stream_param, in coda_bit_stream_end_flag()
452 struct coda_dev *dev = ctx->dev; in coda_parabuf_write()
453 u32 *p = ctx->parabuf.vaddr; in coda_parabuf_write()
455 if (dev->devtype->product == CODA_DX6) in coda_parabuf_write()
465 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry); in coda_alloc_context_buf()
474 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i].buf); in coda_free_framebuffers()
480 struct coda_dev *dev = ctx->dev; in coda_alloc_framebuffers()
485 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 || in coda_alloc_framebuffers()
486 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264 || in coda_alloc_framebuffers()
487 ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4 || in coda_alloc_framebuffers()
488 ctx->codec->dst_fourcc == V4L2_PIX_FMT_MPEG4) in coda_alloc_framebuffers()
489 ysize = round_up(q_data->rect.width, 16) * in coda_alloc_framebuffers()
490 round_up(q_data->rect.height, 16); in coda_alloc_framebuffers()
492 ysize = round_up(q_data->rect.width, 8) * q_data->rect.height; in coda_alloc_framebuffers()
494 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) in coda_alloc_framebuffers()
500 for (i = 0; i < ctx->num_internal_frames; i++) { in coda_alloc_framebuffers()
505 if (dev->devtype->product != CODA_DX6 && in coda_alloc_framebuffers()
506 (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 || in coda_alloc_framebuffers()
507 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4 && i == 0))) in coda_alloc_framebuffers()
512 return -ENOMEM; in coda_alloc_framebuffers()
514 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i].buf, in coda_alloc_framebuffers()
524 for (i = 0; i < ctx->num_internal_frames; i++) { in coda_alloc_framebuffers()
528 y = ctx->internal_frames[i].buf.paddr; in coda_alloc_framebuffers()
532 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) { in coda_alloc_framebuffers()
536 /* Packed 20-bit MSB of base addresses */ in coda_alloc_framebuffers()
545 if (dev->devtype->product == CODA_DX6) in coda_alloc_framebuffers()
549 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264) in coda_alloc_framebuffers()
551 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4 && i == 0) in coda_alloc_framebuffers()
560 struct coda_dev *dev = ctx->dev; in coda_free_context_buffers()
562 coda_free_aux_buf(dev, &ctx->slicebuf); in coda_free_context_buffers()
563 coda_free_aux_buf(dev, &ctx->psbuf); in coda_free_context_buffers()
564 if (dev->devtype->product != CODA_DX6) in coda_free_context_buffers()
565 coda_free_aux_buf(dev, &ctx->workbuf); in coda_free_context_buffers()
566 coda_free_aux_buf(dev, &ctx->parabuf); in coda_free_context_buffers()
572 struct coda_dev *dev = ctx->dev; in coda_alloc_context_buffers()
576 if (!ctx->parabuf.vaddr) { in coda_alloc_context_buffers()
577 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, in coda_alloc_context_buffers()
583 if (dev->devtype->product == CODA_DX6) in coda_alloc_context_buffers()
586 if (!ctx->slicebuf.vaddr && q_data->fourcc == V4L2_PIX_FMT_H264) { in coda_alloc_context_buffers()
588 size = (unsigned long)(DIV_ROUND_UP(q_data->rect.width, 16) * in coda_alloc_context_buffers()
589 DIV_ROUND_UP(q_data->rect.height, 16)) * 3200 / 8 + 512; in coda_alloc_context_buffers()
590 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, in coda_alloc_context_buffers()
596 if (!ctx->psbuf.vaddr && (dev->devtype->product == CODA_HX4 || in coda_alloc_context_buffers()
597 dev->devtype->product == CODA_7541)) { in coda_alloc_context_buffers()
598 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, in coda_alloc_context_buffers()
604 if (!ctx->workbuf.vaddr) { in coda_alloc_context_buffers()
605 size = dev->devtype->workbuf_size; in coda_alloc_context_buffers()
606 if (dev->devtype->product == CODA_960 && in coda_alloc_context_buffers()
607 q_data->fourcc == V4L2_PIX_FMT_H264) in coda_alloc_context_buffers()
609 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, in coda_alloc_context_buffers()
625 struct vb2_buffer *vb = &buf->vb2_buf; in coda_encode_header()
626 struct coda_dev *dev = ctx->dev; in coda_encode_header()
633 if (dev->devtype->product == CODA_960) in coda_encode_header()
639 if (dev->devtype->product == CODA_960) in coda_encode_header()
642 if (dev->devtype->product == CODA_960 && in coda_encode_header()
643 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264 && in coda_encode_header()
646 r = &q_data_src->rect; in coda_encode_header()
648 if (r->width % 16 || r->height % 16) { in coda_encode_header()
649 u32 crop_right = round_up(r->width, 16) - r->width; in coda_encode_header()
650 u32 crop_bottom = round_up(r->height, 16) - r->height; in coda_encode_header()
662 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n"); in coda_encode_header()
666 if (dev->devtype->product == CODA_960) { in coda_encode_header()
667 for (i = 63; i > 0; i--) in coda_encode_header()
672 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) - in coda_encode_header()
684 switch (ctx->params.slice_mode) { in coda_slice_mode()
689 size = ctx->params.slice_max_mb; in coda_slice_mode()
693 size = ctx->params.slice_max_bits; in coda_slice_mode()
705 struct coda_dev *dev = ctx->dev; in coda_enc_param_change()
710 if (ctx->params.gop_size_changed) { in coda_enc_param_change()
712 coda_write(dev, ctx->params.gop_size, in coda_enc_param_change()
714 ctx->gopcounter = ctx->params.gop_size - 1; in coda_enc_param_change()
715 ctx->params.gop_size_changed = false; in coda_enc_param_change()
717 if (ctx->params.h264_intra_qp_changed) { in coda_enc_param_change()
719 ctx->params.h264_intra_qp); in coda_enc_param_change()
721 if (ctx->params.bitrate) { in coda_enc_param_change()
723 coda_write(dev, ctx->params.h264_intra_qp, in coda_enc_param_change()
726 ctx->params.h264_intra_qp_changed = false; in coda_enc_param_change()
728 if (ctx->params.bitrate_changed) { in coda_enc_param_change()
730 ctx->params.bitrate); in coda_enc_param_change()
732 coda_write(dev, ctx->params.bitrate, in coda_enc_param_change()
734 ctx->params.bitrate_changed = false; in coda_enc_param_change()
736 if (ctx->params.framerate_changed) { in coda_enc_param_change()
738 ctx->params.framerate & 0xffff, in coda_enc_param_change()
739 (ctx->params.framerate >> 16) + 1); in coda_enc_param_change()
741 coda_write(dev, ctx->params.framerate, in coda_enc_param_change()
743 ctx->params.framerate_changed = false; in coda_enc_param_change()
745 if (ctx->params.intra_refresh_changed) { in coda_enc_param_change()
747 ctx->params.intra_refresh); in coda_enc_param_change()
749 coda_write(dev, ctx->params.intra_refresh, in coda_enc_param_change()
751 ctx->params.intra_refresh_changed = false; in coda_enc_param_change()
753 if (ctx->params.slice_mode_changed) { in coda_enc_param_change()
757 ctx->params.slice_mode_changed = false; in coda_enc_param_change()
781 if (size > iram->remaining) in coda_iram_alloc()
783 iram->remaining -= size; in coda_iram_alloc()
785 ret = iram->next_paddr; in coda_iram_alloc()
786 iram->next_paddr += size; in coda_iram_alloc()
793 struct coda_iram_info *iram_info = &ctx->iram_info; in coda_setup_iram()
794 struct coda_dev *dev = ctx->dev; in coda_setup_iram()
803 iram_info->next_paddr = dev->iram.paddr; in coda_setup_iram()
804 iram_info->remaining = dev->iram.size; in coda_setup_iram()
806 if (!dev->iram.vaddr) in coda_setup_iram()
809 switch (dev->devtype->product) { in coda_setup_iram()
832 if (ctx->inst_type == CODA_INST_ENCODER) { in coda_setup_iram()
836 mb_width = DIV_ROUND_UP(q_data_src->rect.width, 16); in coda_setup_iram()
841 if (dev->devtype->product == CODA_HX4 || in coda_setup_iram()
842 dev->devtype->product == CODA_7541) { in coda_setup_iram()
843 iram_info->search_ram_size = round_up(mb_width * 16 * in coda_setup_iram()
845 iram_info->search_ram_paddr = coda_iram_alloc(iram_info, in coda_setup_iram()
846 iram_info->search_ram_size); in coda_setup_iram()
847 if (!iram_info->search_ram_paddr) { in coda_setup_iram()
851 iram_info->axi_sram_use |= me_bits; in coda_setup_iram()
855 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64); in coda_setup_iram()
856 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64); in coda_setup_iram()
857 if (!iram_info->buf_dbk_y_use || !iram_info->buf_dbk_c_use) in coda_setup_iram()
859 iram_info->axi_sram_use |= dbk_bits; in coda_setup_iram()
861 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128); in coda_setup_iram()
862 if (!iram_info->buf_bit_use) in coda_setup_iram()
864 iram_info->axi_sram_use |= bit_bits; in coda_setup_iram()
866 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128); in coda_setup_iram()
867 if (!iram_info->buf_ip_ac_dc_use) in coda_setup_iram()
869 iram_info->axi_sram_use |= ip_bits; in coda_setup_iram()
872 } else if (ctx->inst_type == CODA_INST_DECODER) { in coda_setup_iram()
876 mb_width = DIV_ROUND_UP(q_data_dst->width, 16); in coda_setup_iram()
879 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128); in coda_setup_iram()
880 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128); in coda_setup_iram()
881 if (!iram_info->buf_dbk_y_use || !iram_info->buf_dbk_c_use) in coda_setup_iram()
883 iram_info->axi_sram_use |= dbk_bits; in coda_setup_iram()
885 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128); in coda_setup_iram()
886 if (!iram_info->buf_bit_use) in coda_setup_iram()
888 iram_info->axi_sram_use |= bit_bits; in coda_setup_iram()
890 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128); in coda_setup_iram()
891 if (!iram_info->buf_ip_ac_dc_use) in coda_setup_iram()
893 iram_info->axi_sram_use |= ip_bits; in coda_setup_iram()
899 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)) in coda_setup_iram()
902 if (dev->devtype->product == CODA_HX4 || in coda_setup_iram()
903 dev->devtype->product == CODA_7541) { in coda_setup_iram()
904 /* TODO - Enabling these causes picture errors on CODA7541 */ in coda_setup_iram()
905 if (ctx->inst_type == CODA_INST_DECODER) { in coda_setup_iram()
907 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE | in coda_setup_iram()
911 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE | in coda_setup_iram()
945 ret = clk_prepare_enable(dev->clk_per); in coda_check_firmware()
949 ret = clk_prepare_enable(dev->clk_ahb); in coda_check_firmware()
959 v4l2_err(&dev->v4l2_dev, "firmware get command error\n"); in coda_check_firmware()
960 ret = -EIO; in coda_check_firmware()
964 if (dev->devtype->product == CODA_960) { in coda_check_firmware()
966 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n", in coda_check_firmware()
977 clk_disable_unprepare(dev->clk_per); in coda_check_firmware()
978 clk_disable_unprepare(dev->clk_ahb); in coda_check_firmware()
980 if (product != dev->devtype->product) { in coda_check_firmware()
981 v4l2_err(&dev->v4l2_dev, in coda_check_firmware()
983 coda_product_name(dev->devtype->product), in coda_check_firmware()
985 return -EINVAL; in coda_check_firmware()
988 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n", in coda_check_firmware()
992 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n", in coda_check_firmware()
995 v4l2_warn(&dev->v4l2_dev, in coda_check_firmware()
1003 clk_disable_unprepare(dev->clk_ahb); in coda_check_firmware()
1005 clk_disable_unprepare(dev->clk_per); in coda_check_firmware()
1014 if (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) { in coda9_set_frame_cache()
1023 coda_write(ctx->dev, cache_size, CODA9_CMD_SET_FRAME_CACHE_SIZE); in coda9_set_frame_cache()
1033 coda_write(ctx->dev, cache_config, CODA9_CMD_SET_FRAME_CACHE_CONFIG); in coda9_set_frame_cache()
1046 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) in coda_encoder_reqbufs()
1049 if (rb->count) { in coda_encoder_reqbufs()
1063 struct coda_dev *dev = ctx->dev; in coda_start_encoding()
1064 struct v4l2_device *v4l2_dev = &dev->v4l2_dev; in coda_start_encoding()
1075 dst_fourcc = q_data_dst->fourcc; in coda_start_encoding()
1077 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); in coda_start_encoding()
1078 bitstream_buf = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0); in coda_start_encoding()
1079 bitstream_size = q_data_dst->sizeimage; in coda_start_encoding()
1083 return -EFAULT; in coda_start_encoding()
1087 if (!ctx->params.jpeg_qmat_tab[0]) { in coda_start_encoding()
1088 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL); in coda_start_encoding()
1089 if (!ctx->params.jpeg_qmat_tab[0]) in coda_start_encoding()
1090 return -ENOMEM; in coda_start_encoding()
1092 if (!ctx->params.jpeg_qmat_tab[1]) { in coda_start_encoding()
1093 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL); in coda_start_encoding()
1094 if (!ctx->params.jpeg_qmat_tab[1]) in coda_start_encoding()
1095 return -ENOMEM; in coda_start_encoding()
1097 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality); in coda_start_encoding()
1100 mutex_lock(&dev->coda_mutex); in coda_start_encoding()
1102 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); in coda_start_encoding()
1103 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); in coda_start_encoding()
1104 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); in coda_start_encoding()
1105 switch (dev->devtype->product) { in coda_start_encoding()
1120 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) | in coda_start_encoding()
1122 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12) in coda_start_encoding()
1123 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE; in coda_start_encoding()
1124 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) in coda_start_encoding()
1125 ctx->frame_mem_ctrl |= (0x3 << 9) | CODA9_FRAME_TILED2LINEAR; in coda_start_encoding()
1126 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL); in coda_start_encoding()
1128 if (dev->devtype->product == CODA_DX6) { in coda_start_encoding()
1130 coda_write(dev, dev->iram.paddr, in coda_start_encoding()
1136 switch (dev->devtype->product) { in coda_start_encoding()
1138 value = (q_data_src->rect.width & CODADX6_PICWIDTH_MASK) in coda_start_encoding()
1140 value |= (q_data_src->rect.height & CODADX6_PICHEIGHT_MASK) in coda_start_encoding()
1146 value = (round_up(q_data_src->rect.width, 16) & in coda_start_encoding()
1148 value |= (round_up(q_data_src->rect.height, 16) & in coda_start_encoding()
1154 value = (q_data_src->rect.width & CODA7_PICWIDTH_MASK) in coda_start_encoding()
1156 value |= (q_data_src->rect.height & CODA7_PICHEIGHT_MASK) in coda_start_encoding()
1161 ctx->params.framerate = 0; in coda_start_encoding()
1162 coda_write(dev, ctx->params.framerate, in coda_start_encoding()
1165 ctx->params.codec_mode = ctx->codec->mode; in coda_start_encoding()
1168 if (dev->devtype->product == CODA_960) in coda_start_encoding()
1177 if (dev->devtype->product == CODA_960) in coda_start_encoding()
1183 value = ((ctx->params.h264_disable_deblocking_filter_idc & in coda_start_encoding()
1186 ((ctx->params.h264_slice_alpha_c0_offset_div2 & in coda_start_encoding()
1189 ((ctx->params.h264_slice_beta_offset_div2 & in coda_start_encoding()
1192 (ctx->params.h264_constrained_intra_pred_flag << in coda_start_encoding()
1194 (ctx->params.h264_chroma_qp_index_offset & in coda_start_encoding()
1200 coda_write(dev, ctx->params.jpeg_restart_interval, in coda_start_encoding()
1211 ret = -EINVAL; in coda_start_encoding()
1222 value = ctx->params.gop_size; in coda_start_encoding()
1226 if (ctx->params.bitrate && (ctx->params.frame_rc_enable || in coda_start_encoding()
1227 ctx->params.mb_rc_enable)) { in coda_start_encoding()
1228 ctx->params.bitrate_changed = false; in coda_start_encoding()
1229 ctx->params.h264_intra_qp_changed = false; in coda_start_encoding()
1232 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) in coda_start_encoding()
1235 value |= (ctx->params.vbv_delay & in coda_start_encoding()
1238 if (dev->devtype->product == CODA_960) in coda_start_encoding()
1245 coda_write(dev, ctx->params.vbv_size, CODA_CMD_ENC_SEQ_RC_BUF_SIZE); in coda_start_encoding()
1246 coda_write(dev, ctx->params.intra_refresh, in coda_start_encoding()
1254 if (dev->devtype->product == CODA_960) in coda_start_encoding()
1263 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) { in coda_start_encoding()
1265 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET | in coda_start_encoding()
1266 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET, in coda_start_encoding()
1269 if (dev->devtype->product == CODA_960) { in coda_start_encoding()
1270 if (ctx->params.h264_max_qp) in coda_start_encoding()
1276 if (dev->devtype->product == CODA_DX6) in coda_start_encoding()
1281 if (ctx->params.h264_min_qp) in coda_start_encoding()
1283 if (ctx->params.h264_max_qp) in coda_start_encoding()
1288 if (ctx->params.frame_rc_enable && !ctx->params.mb_rc_enable) in coda_start_encoding()
1297 switch (dev->devtype->product) { in coda_start_encoding()
1304 coda_write(dev, ctx->iram_info.search_ram_paddr, in coda_start_encoding()
1306 coda_write(dev, ctx->iram_info.search_ram_size, in coda_start_encoding()
1323 ret = -EFAULT; in coda_start_encoding()
1326 ctx->initialized = 1; in coda_start_encoding()
1329 if (dev->devtype->product == CODA_960) in coda_start_encoding()
1330 ctx->num_internal_frames = 4; in coda_start_encoding()
1332 ctx->num_internal_frames = 2; in coda_start_encoding()
1339 stride = q_data_src->bytesperline; in coda_start_encoding()
1341 ctx->num_internal_frames = 0; in coda_start_encoding()
1348 if (dev->devtype->product == CODA_HX4 || in coda_start_encoding()
1349 dev->devtype->product == CODA_7541) { in coda_start_encoding()
1350 coda_write(dev, q_data_src->bytesperline, in coda_start_encoding()
1353 if (dev->devtype->product != CODA_DX6) { in coda_start_encoding()
1354 coda_write(dev, ctx->iram_info.buf_bit_use, in coda_start_encoding()
1356 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use, in coda_start_encoding()
1358 coda_write(dev, ctx->iram_info.buf_dbk_y_use, in coda_start_encoding()
1360 coda_write(dev, ctx->iram_info.buf_dbk_c_use, in coda_start_encoding()
1362 coda_write(dev, ctx->iram_info.buf_ovl_use, in coda_start_encoding()
1364 if (dev->devtype->product == CODA_960) { in coda_start_encoding()
1365 coda_write(dev, ctx->iram_info.buf_btp_use, in coda_start_encoding()
1368 coda9_set_frame_cache(ctx, q_data_src->fourcc); in coda_start_encoding()
1371 coda_write(dev, ctx->internal_frames[2].buf.paddr, in coda_start_encoding()
1373 coda_write(dev, ctx->internal_frames[3].buf.paddr, in coda_start_encoding()
1384 coda_dbg(1, ctx, "start encoding %dx%d %4.4s->%4.4s @ %d/%d Hz\n", in coda_start_encoding()
1385 q_data_src->rect.width, q_data_src->rect.height, in coda_start_encoding()
1386 (char *)&ctx->codec->src_fourcc, (char *)&dst_fourcc, in coda_start_encoding()
1387 ctx->params.framerate & 0xffff, in coda_start_encoding()
1388 (ctx->params.framerate >> 16) + 1); in coda_start_encoding()
1391 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); in coda_start_encoding()
1399 &ctx->vpu_header[0][0], in coda_start_encoding()
1400 &ctx->vpu_header_size[0]); in coda_start_encoding()
1413 if (ctx->dev->devtype->product != CODA_960 && in coda_start_encoding()
1414 ((q_data_src->rect.width % 16) || in coda_start_encoding()
1415 (q_data_src->rect.height % 16))) { in coda_start_encoding()
1416 ret = coda_h264_sps_fixup(ctx, q_data_src->rect.width, in coda_start_encoding()
1417 q_data_src->rect.height, in coda_start_encoding()
1418 &ctx->vpu_header[0][0], in coda_start_encoding()
1419 &ctx->vpu_header_size[0], in coda_start_encoding()
1420 sizeof(ctx->vpu_header[0])); in coda_start_encoding()
1430 &ctx->vpu_header[1][0], in coda_start_encoding()
1431 &ctx->vpu_header_size[1]); in coda_start_encoding()
1440 ctx->vpu_header_size[2] = coda_h264_padding( in coda_start_encoding()
1441 (ctx->vpu_header_size[0] + in coda_start_encoding()
1442 ctx->vpu_header_size[1]), in coda_start_encoding()
1443 ctx->vpu_header[2]); in coda_start_encoding()
1451 &ctx->vpu_header[0][0], in coda_start_encoding()
1452 &ctx->vpu_header_size[0]); in coda_start_encoding()
1457 &ctx->vpu_header[1][0], in coda_start_encoding()
1458 &ctx->vpu_header_size[1]); in coda_start_encoding()
1463 &ctx->vpu_header[2][0], in coda_start_encoding()
1464 &ctx->vpu_header_size[2]); in coda_start_encoding()
1474 mutex_unlock(&dev->coda_mutex); in coda_start_encoding()
1482 struct coda_dev *dev = ctx->dev; in coda_prepare_encode()
1493 v4l2_warn(&ctx->dev->v4l2_dev, "parameter change failed: %d\n", in coda_prepare_encode()
1497 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); in coda_prepare_encode()
1498 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); in coda_prepare_encode()
1501 dst_fourcc = q_data_dst->fourcc; in coda_prepare_encode()
1503 src_buf->sequence = ctx->osequence; in coda_prepare_encode()
1504 dst_buf->sequence = ctx->osequence; in coda_prepare_encode()
1505 ctx->osequence++; in coda_prepare_encode()
1507 force_ipicture = ctx->params.force_ipicture; in coda_prepare_encode()
1509 ctx->params.force_ipicture = false; in coda_prepare_encode()
1510 else if (ctx->params.gop_size != 0 && in coda_prepare_encode()
1511 (src_buf->sequence % ctx->params.gop_size) == 0) in coda_prepare_encode()
1520 src_buf->flags |= V4L2_BUF_FLAG_PFRAME; in coda_prepare_encode()
1521 src_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME; in coda_prepare_encode()
1523 src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME; in coda_prepare_encode()
1524 src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME; in coda_prepare_encode()
1527 if (dev->devtype->product == CODA_960) in coda_prepare_encode()
1534 if (src_buf->sequence == 0 || force_ipicture) { in coda_prepare_encode()
1536 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0) + in coda_prepare_encode()
1537 ctx->vpu_header_size[0] + in coda_prepare_encode()
1538 ctx->vpu_header_size[1] + in coda_prepare_encode()
1539 ctx->vpu_header_size[2]; in coda_prepare_encode()
1540 pic_stream_buffer_size = q_data_dst->sizeimage - in coda_prepare_encode()
1541 ctx->vpu_header_size[0] - in coda_prepare_encode()
1542 ctx->vpu_header_size[1] - in coda_prepare_encode()
1543 ctx->vpu_header_size[2]; in coda_prepare_encode()
1544 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0), in coda_prepare_encode()
1545 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]); in coda_prepare_encode()
1546 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0) in coda_prepare_encode()
1547 + ctx->vpu_header_size[0], &ctx->vpu_header[1][0], in coda_prepare_encode()
1548 ctx->vpu_header_size[1]); in coda_prepare_encode()
1549 memcpy(vb2_plane_vaddr(&dst_buf->vb2_buf, 0) in coda_prepare_encode()
1550 + ctx->vpu_header_size[0] + ctx->vpu_header_size[1], in coda_prepare_encode()
1551 &ctx->vpu_header[2][0], ctx->vpu_header_size[2]); in coda_prepare_encode()
1554 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0); in coda_prepare_encode()
1555 pic_stream_buffer_size = q_data_dst->sizeimage; in coda_prepare_encode()
1561 quant_param = ctx->params.h264_intra_qp; in coda_prepare_encode()
1564 quant_param = ctx->params.mpeg4_intra_qp; in coda_prepare_encode()
1570 v4l2_warn(&ctx->dev->v4l2_dev, in coda_prepare_encode()
1577 quant_param = ctx->params.h264_inter_qp; in coda_prepare_encode()
1580 quant_param = ctx->params.mpeg4_inter_qp; in coda_prepare_encode()
1583 v4l2_warn(&ctx->dev->v4l2_dev, in coda_prepare_encode()
1590 if (ctx->params.rot_mode) in coda_prepare_encode()
1591 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode; in coda_prepare_encode()
1595 if (dev->devtype->product == CODA_960) { in coda_prepare_encode()
1597 coda_write(dev, q_data_src->bytesperline, in coda_prepare_encode()
1614 if (!ctx->streamon_out) { in coda_prepare_encode()
1615 /* After streamoff on the output side, set stream end flag */ in coda_prepare_encode()
1616 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; in coda_prepare_encode()
1617 coda_write(dev, ctx->bit_stream_param, in coda_prepare_encode()
1621 if (dev->devtype->product != CODA_DX6) in coda_prepare_encode()
1622 coda_write(dev, ctx->iram_info.axi_sram_use, in coda_prepare_encode()
1642 struct coda_dev *dev = ctx->dev; in coda_finish_encode()
1645 if (ctx->aborting) in coda_finish_encode()
1653 mutex_lock(&ctx->wakeup_mutex); in coda_finish_encode()
1654 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); in coda_finish_encode()
1655 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); in coda_finish_encode()
1661 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); in coda_finish_encode()
1664 if (dst_buf->sequence == 0 || in coda_finish_encode()
1665 src_buf->flags & V4L2_BUF_FLAG_KEYFRAME) { in coda_finish_encode()
1666 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr + in coda_finish_encode()
1667 ctx->vpu_header_size[0] + in coda_finish_encode()
1668 ctx->vpu_header_size[1] + in coda_finish_encode()
1669 ctx->vpu_header_size[2]); in coda_finish_encode()
1671 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr); in coda_finish_encode()
1674 coda_dbg(1, ctx, "frame size = %u\n", wr_ptr - start_ptr); in coda_finish_encode()
1679 dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | in coda_finish_encode()
1683 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME; in coda_finish_encode()
1685 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME; in coda_finish_encode()
1686 dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST; in coda_finish_encode()
1692 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); in coda_finish_encode()
1694 mutex_unlock(&ctx->wakeup_mutex); in coda_finish_encode()
1696 ctx->gopcounter--; in coda_finish_encode()
1697 if (ctx->gopcounter < 0) in coda_finish_encode()
1698 ctx->gopcounter = ctx->params.gop_size - 1; in coda_finish_encode()
1701 coda_frame_type_char(dst_buf->flags), dst_buf->sequence, in coda_finish_encode()
1702 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : ""); in coda_finish_encode()
1708 struct coda_dev *dev = ctx->dev; in coda_seq_end_work()
1710 mutex_lock(&ctx->buffer_mutex); in coda_seq_end_work()
1711 mutex_lock(&dev->coda_mutex); in coda_seq_end_work()
1713 if (ctx->initialized == 0) in coda_seq_end_work()
1718 v4l2_err(&dev->v4l2_dev, in coda_seq_end_work()
1723 * FIXME: Sometimes h.264 encoding fails with 8-byte sequences missing in coda_seq_end_work()
1727 if (dev->devtype->product == CODA_960) in coda_seq_end_work()
1730 kfifo_init(&ctx->bitstream_fifo, in coda_seq_end_work()
1731 ctx->bitstream.vaddr, ctx->bitstream.size); in coda_seq_end_work()
1735 ctx->initialized = 0; in coda_seq_end_work()
1738 mutex_unlock(&dev->coda_mutex); in coda_seq_end_work()
1739 mutex_unlock(&ctx->buffer_mutex); in coda_seq_end_work()
1744 mutex_lock(&ctx->buffer_mutex); in coda_bit_release()
1748 mutex_unlock(&ctx->buffer_mutex); in coda_bit_release()
1768 if (ctx->bitstream.vaddr) in coda_alloc_bitstream_buffer()
1771 ctx->bitstream.size = roundup_pow_of_two(q_data->sizeimage * 2); in coda_alloc_bitstream_buffer()
1772 ctx->bitstream.vaddr = dma_alloc_wc(ctx->dev->dev, ctx->bitstream.size, in coda_alloc_bitstream_buffer()
1773 &ctx->bitstream.paddr, GFP_KERNEL); in coda_alloc_bitstream_buffer()
1774 if (!ctx->bitstream.vaddr) { in coda_alloc_bitstream_buffer()
1775 v4l2_err(&ctx->dev->v4l2_dev, in coda_alloc_bitstream_buffer()
1777 return -ENOMEM; in coda_alloc_bitstream_buffer()
1779 kfifo_init(&ctx->bitstream_fifo, in coda_alloc_bitstream_buffer()
1780 ctx->bitstream.vaddr, ctx->bitstream.size); in coda_alloc_bitstream_buffer()
1787 if (ctx->bitstream.vaddr == NULL) in coda_free_bitstream_buffer()
1790 dma_free_wc(ctx->dev->dev, ctx->bitstream.size, ctx->bitstream.vaddr, in coda_free_bitstream_buffer()
1791 ctx->bitstream.paddr); in coda_free_bitstream_buffer()
1792 ctx->bitstream.vaddr = NULL; in coda_free_bitstream_buffer()
1793 kfifo_init(&ctx->bitstream_fifo, NULL, 0); in coda_free_bitstream_buffer()
1802 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) in coda_decoder_reqbufs()
1805 if (rb->count) { in coda_decoder_reqbufs()
1825 struct coda_dev *dev = ctx->dev; in coda_reorder_enable()
1828 if (dev->devtype->product != CODA_HX4 && in coda_reorder_enable()
1829 dev->devtype->product != CODA_7541 && in coda_reorder_enable()
1830 dev->devtype->product != CODA_960) in coda_reorder_enable()
1833 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) in coda_reorder_enable()
1836 if (ctx->codec->src_fourcc != V4L2_PIX_FMT_H264) in coda_reorder_enable()
1839 profile = coda_h264_profile(ctx->params.h264_profile_idc); in coda_reorder_enable()
1841 v4l2_warn(&dev->v4l2_dev, "Unknown H264 Profile: %u\n", in coda_reorder_enable()
1842 ctx->params.h264_profile_idc); in coda_reorder_enable()
1854 * are now consumed by the VPU and should be released. in coda_decoder_drop_used_metas()
1856 spin_lock(&ctx->buffer_meta_lock); in coda_decoder_drop_used_metas()
1857 list_for_each_entry_safe(meta, tmp, &ctx->buffer_meta_list, list) { in coda_decoder_drop_used_metas()
1858 if (ctx->bitstream_fifo.kfifo.out >= meta->end) { in coda_decoder_drop_used_metas()
1860 meta->sequence, meta->start, meta->end); in coda_decoder_drop_used_metas()
1862 list_del(&meta->list); in coda_decoder_drop_used_metas()
1863 ctx->num_metas--; in coda_decoder_drop_used_metas()
1864 ctx->first_frame_sequence++; in coda_decoder_drop_used_metas()
1868 spin_unlock(&ctx->buffer_meta_lock); in coda_decoder_drop_used_metas()
1875 struct coda_dev *dev = ctx->dev; in __coda_decoder_seq_init()
1881 lockdep_assert_held(&dev->coda_mutex); in __coda_decoder_seq_init()
1884 ctx->use_vdoa ? "Enabled" : "Disabled"); in __coda_decoder_seq_init()
1889 bitstream_buf = ctx->bitstream.paddr; in __coda_decoder_seq_init()
1890 bitstream_size = ctx->bitstream.size; in __coda_decoder_seq_init()
1891 src_fourcc = q_data_src->fourcc; in __coda_decoder_seq_init()
1892 dst_fourcc = q_data_dst->fourcc; in __coda_decoder_seq_init()
1897 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) | in __coda_decoder_seq_init()
1900 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE; in __coda_decoder_seq_init()
1901 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) in __coda_decoder_seq_init()
1902 ctx->frame_mem_ctrl |= (0x3 << 9) | in __coda_decoder_seq_init()
1903 ((ctx->use_vdoa) ? 0 : CODA9_FRAME_TILED2LINEAR); in __coda_decoder_seq_init()
1904 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL); in __coda_decoder_seq_init()
1906 ctx->display_idx = -1; in __coda_decoder_seq_init()
1907 ctx->frm_dis_flg = 0; in __coda_decoder_seq_init()
1908 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); in __coda_decoder_seq_init()
1915 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) in __coda_decoder_seq_init()
1919 ctx->params.codec_mode = ctx->codec->mode; in __coda_decoder_seq_init()
1920 if (dev->devtype->product == CODA_960 && in __coda_decoder_seq_init()
1922 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4; in __coda_decoder_seq_init()
1924 ctx->params.codec_mode_aux = 0; in __coda_decoder_seq_init()
1930 if (dev->devtype->product == CODA_HX4 || in __coda_decoder_seq_init()
1931 dev->devtype->product == CODA_7541) { in __coda_decoder_seq_init()
1932 coda_write(dev, ctx->psbuf.paddr, in __coda_decoder_seq_init()
1937 if (dev->devtype->product == CODA_960) { in __coda_decoder_seq_init()
1944 if (dev->devtype->product != CODA_960) in __coda_decoder_seq_init()
1947 ctx->bit_stream_param = CODA_BIT_DEC_SEQ_INIT_ESCAPE; in __coda_decoder_seq_init()
1949 ctx->bit_stream_param = 0; in __coda_decoder_seq_init()
1951 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n"); in __coda_decoder_seq_init()
1954 ctx->sequence_offset = ~0U; in __coda_decoder_seq_init()
1955 ctx->initialized = 1; in __coda_decoder_seq_init()
1956 ctx->first_frame_sequence = 0; in __coda_decoder_seq_init()
1968 v4l2_err(&dev->v4l2_dev, in __coda_decoder_seq_init()
1971 return -EAGAIN; in __coda_decoder_seq_init()
1975 if (dev->devtype->product == CODA_DX6) { in __coda_decoder_seq_init()
1983 if (width > q_data_dst->bytesperline || height > q_data_dst->height) { in __coda_decoder_seq_init()
1984 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n", in __coda_decoder_seq_init()
1985 width, height, q_data_dst->bytesperline, in __coda_decoder_seq_init()
1986 q_data_dst->height); in __coda_decoder_seq_init()
1987 return -EINVAL; in __coda_decoder_seq_init()
1995 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED); in __coda_decoder_seq_init()
2003 if (ctx->use_vdoa) in __coda_decoder_seq_init()
2004 ctx->num_internal_frames += 1; in __coda_decoder_seq_init()
2005 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) { in __coda_decoder_seq_init()
2006 v4l2_err(&dev->v4l2_dev, in __coda_decoder_seq_init()
2008 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames); in __coda_decoder_seq_init()
2009 return -EINVAL; in __coda_decoder_seq_init()
2019 q_data_dst->rect.left = (left_right >> 10) & 0x3ff; in __coda_decoder_seq_init()
2020 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff; in __coda_decoder_seq_init()
2021 q_data_dst->rect.width = width - q_data_dst->rect.left - in __coda_decoder_seq_init()
2023 q_data_dst->rect.height = height - q_data_dst->rect.top - in __coda_decoder_seq_init()
2027 if (dev->devtype->product != CODA_DX6) { in __coda_decoder_seq_init()
2045 struct coda_dev *dev = ctx->dev; in coda_dec_seq_init_work()
2047 mutex_lock(&ctx->buffer_mutex); in coda_dec_seq_init_work()
2048 mutex_lock(&dev->coda_mutex); in coda_dec_seq_init_work()
2050 if (!ctx->initialized) in coda_dec_seq_init_work()
2053 mutex_unlock(&dev->coda_mutex); in coda_dec_seq_init_work()
2054 mutex_unlock(&ctx->buffer_mutex); in coda_dec_seq_init_work()
2060 struct coda_dev *dev = ctx->dev; in __coda_start_decoding()
2066 src_fourcc = q_data_src->fourcc; in __coda_start_decoding()
2067 dst_fourcc = q_data_dst->fourcc; in __coda_start_decoding()
2069 if (!ctx->initialized) { in __coda_start_decoding()
2074 ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) | in __coda_start_decoding()
2077 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE; in __coda_start_decoding()
2078 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) in __coda_start_decoding()
2079 ctx->frame_mem_ctrl |= (0x3 << 9) | in __coda_start_decoding()
2080 ((ctx->use_vdoa) ? 0 : CODA9_FRAME_TILED2LINEAR); in __coda_start_decoding()
2083 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); in __coda_start_decoding()
2087 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n"); in __coda_start_decoding()
2092 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM); in __coda_start_decoding()
2093 coda_write(dev, round_up(q_data_dst->rect.width, 16), in __coda_start_decoding()
2096 if (dev->devtype->product != CODA_DX6) { in __coda_start_decoding()
2100 coda_write(dev, ctx->iram_info.buf_bit_use, in __coda_start_decoding()
2102 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use, in __coda_start_decoding()
2104 coda_write(dev, ctx->iram_info.buf_dbk_y_use, in __coda_start_decoding()
2106 coda_write(dev, ctx->iram_info.buf_dbk_c_use, in __coda_start_decoding()
2108 coda_write(dev, ctx->iram_info.buf_ovl_use, in __coda_start_decoding()
2110 if (dev->devtype->product == CODA_960) { in __coda_start_decoding()
2111 coda_write(dev, ctx->iram_info.buf_btp_use, in __coda_start_decoding()
2114 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY); in __coda_start_decoding()
2120 coda_write(dev, ctx->slicebuf.paddr, in __coda_start_decoding()
2122 coda_write(dev, ctx->slicebuf.size / 1024, in __coda_start_decoding()
2126 if (dev->devtype->product == CODA_HX4 || in __coda_start_decoding()
2127 dev->devtype->product == CODA_7541) { in __coda_start_decoding()
2134 } else if (dev->devtype->product == CODA_960) { in __coda_start_decoding()
2144 v4l2_err(&ctx->dev->v4l2_dev, in __coda_start_decoding()
2146 return -ETIMEDOUT; in __coda_start_decoding()
2154 struct coda_dev *dev = ctx->dev; in coda_start_decoding()
2157 mutex_lock(&dev->coda_mutex); in coda_start_decoding()
2159 mutex_unlock(&dev->coda_mutex); in coda_start_decoding()
2167 struct coda_dev *dev = ctx->dev; in coda_prepare_decode()
2173 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); in coda_prepare_decode()
2177 mutex_lock(&ctx->bitstream_mutex); in coda_prepare_decode()
2179 mutex_unlock(&ctx->bitstream_mutex); in coda_prepare_decode()
2182 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) { in coda_prepare_decode()
2185 return -EAGAIN; in coda_prepare_decode()
2189 if (!ctx->initialized) { in coda_prepare_decode()
2193 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n"); in coda_prepare_decode()
2194 return -EAGAIN; in coda_prepare_decode()
2196 ctx->initialized = 1; in coda_prepare_decode()
2200 if (dev->devtype->product == CODA_960) in coda_prepare_decode()
2203 if (ctx->use_vdoa && in coda_prepare_decode()
2204 ctx->display_idx >= 0 && in coda_prepare_decode()
2205 ctx->display_idx < ctx->num_internal_frames) { in coda_prepare_decode()
2206 vdoa_device_run(ctx->vdoa, in coda_prepare_decode()
2207 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0), in coda_prepare_decode()
2208 ctx->internal_frames[ctx->display_idx].buf.paddr); in coda_prepare_decode()
2210 if (dev->devtype->product == CODA_960) { in coda_prepare_decode()
2220 * hangups that it can not recover from with the SRC VPU in coda_prepare_decode()
2223 * fixed value in the [ctx->num_internal_frames, 31] in coda_prepare_decode()
2236 coda_write(dev, q_data_dst->bytesperline, reg_stride); in coda_prepare_decode()
2238 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode; in coda_prepare_decode()
2243 switch (dev->devtype->product) { in coda_prepare_decode()
2261 if (dev->devtype->product != CODA_DX6) in coda_prepare_decode()
2262 coda_write(dev, ctx->iram_info.axi_sram_use, in coda_prepare_decode()
2265 spin_lock(&ctx->buffer_meta_lock); in coda_prepare_decode()
2266 meta = list_first_entry_or_null(&ctx->buffer_meta_list, in coda_prepare_decode()
2269 if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) { in coda_prepare_decode()
2272 if (meta->end == ctx->bitstream_fifo.kfifo.in) { in coda_prepare_decode()
2277 pad = ((0 - meta->end) & 0xff) + 256; in coda_prepare_decode()
2281 kfifo_in(&ctx->bitstream_fifo, buf, pad); in coda_prepare_decode()
2284 spin_unlock(&ctx->buffer_meta_lock); in coda_prepare_decode()
2303 struct coda_dev *dev = ctx->dev; in coda_finish_decode()
2318 if (ctx->aborting) in coda_finish_decode()
2325 * in stream-end mode, the read pointer can overshoot the write pointer in coda_finish_decode()
2328 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) { in coda_finish_decode()
2329 if (coda_get_bitstream_payload(ctx) >= ctx->bitstream.size - 512) in coda_finish_decode()
2330 kfifo_init(&ctx->bitstream_fifo, in coda_finish_decode()
2331 ctx->bitstream.vaddr, ctx->bitstream.size); in coda_finish_decode()
2335 src_fourcc = q_data_src->fourcc; in coda_finish_decode()
2343 v4l2_err(&dev->v4l2_dev, "decode failed\n"); in coda_finish_decode()
2347 v4l2_err(&dev->v4l2_dev, in coda_finish_decode()
2349 ctx->psbuf.size); in coda_finish_decode()
2351 v4l2_err(&dev->v4l2_dev, in coda_finish_decode()
2353 ctx->slicebuf.size); in coda_finish_decode()
2373 struct v4l2_rect *rect = &q_data_dst->rect; in coda_finish_decode()
2375 rect->left = left_right >> 16 & 0xffff; in coda_finish_decode()
2376 rect->top = top_bottom >> 16 & 0xffff; in coda_finish_decode()
2377 rect->width = width - rect->left - in coda_finish_decode()
2379 rect->height = height - rect->top - in coda_finish_decode()
2388 if (__ratelimit(&dev->mb_err_rs)) in coda_finish_decode()
2390 v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, in coda_finish_decode()
2391 v4l2_ctrl_g_ctrl(ctx->mb_err_cnt_ctrl) + err_mb); in coda_finish_decode()
2394 if (dev->devtype->product == CODA_HX4 || in coda_finish_decode()
2395 dev->devtype->product == CODA_7541) { in coda_finish_decode()
2400 ctx->hold = true; in coda_finish_decode()
2406 if (ctx->use_vdoa && in coda_finish_decode()
2407 ctx->display_idx >= 0 && in coda_finish_decode()
2408 ctx->display_idx < ctx->num_internal_frames) { in coda_finish_decode()
2409 err_vdoa = vdoa_wait_for_completion(ctx->vdoa); in coda_finish_decode()
2412 ctx->frm_dis_flg = coda_read(dev, in coda_finish_decode()
2413 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); in coda_finish_decode()
2416 if (ctx->display_idx >= 0 && in coda_finish_decode()
2417 ctx->display_idx < ctx->num_internal_frames) { in coda_finish_decode()
2418 ctx->frm_dis_flg &= ~(1 << ctx->display_idx); in coda_finish_decode()
2419 coda_write(dev, ctx->frm_dis_flg, in coda_finish_decode()
2420 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); in coda_finish_decode()
2431 if (decoded_idx == -1) { in coda_finish_decode()
2433 if (display_idx >= 0 && display_idx < ctx->num_internal_frames) in coda_finish_decode()
2434 ctx->sequence_offset++; in coda_finish_decode()
2435 else if (ctx->display_idx < 0) in coda_finish_decode()
2436 ctx->hold = true; in coda_finish_decode()
2437 } else if (decoded_idx == -2) { in coda_finish_decode()
2438 if (ctx->display_idx >= 0 && in coda_finish_decode()
2439 ctx->display_idx < ctx->num_internal_frames) in coda_finish_decode()
2440 ctx->sequence_offset++; in coda_finish_decode()
2442 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) { in coda_finish_decode()
2443 v4l2_err(&dev->v4l2_dev, in coda_finish_decode()
2448 decoded_frame = &ctx->internal_frames[decoded_idx]; in coda_finish_decode()
2451 if (ctx->sequence_offset == -1) in coda_finish_decode()
2452 ctx->sequence_offset = val; in coda_finish_decode()
2454 sequence = val + ctx->first_frame_sequence in coda_finish_decode()
2455 - ctx->sequence_offset; in coda_finish_decode()
2456 spin_lock(&ctx->buffer_meta_lock); in coda_finish_decode()
2457 if (!list_empty(&ctx->buffer_meta_list)) { in coda_finish_decode()
2458 meta = list_first_entry(&ctx->buffer_meta_list, in coda_finish_decode()
2460 list_del(&meta->list); in coda_finish_decode()
2461 ctx->num_metas--; in coda_finish_decode()
2462 spin_unlock(&ctx->buffer_meta_lock); in coda_finish_decode()
2470 if ((sequence & 0xffff) != (meta->sequence & 0xffff)) { in coda_finish_decode()
2471 v4l2_err(&dev->v4l2_dev, in coda_finish_decode()
2473 sequence, ctx->sequence_offset, in coda_finish_decode()
2474 meta->sequence); in coda_finish_decode()
2476 decoded_frame->meta = *meta; in coda_finish_decode()
2479 spin_unlock(&ctx->buffer_meta_lock); in coda_finish_decode()
2480 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n"); in coda_finish_decode()
2481 memset(&decoded_frame->meta, 0, in coda_finish_decode()
2483 decoded_frame->meta.sequence = sequence; in coda_finish_decode()
2484 decoded_frame->meta.last = false; in coda_finish_decode()
2485 ctx->sequence_offset++; in coda_finish_decode()
2488 trace_coda_dec_pic_done(ctx, &decoded_frame->meta); in coda_finish_decode()
2491 decoded_frame->type = (val == 0) ? V4L2_BUF_FLAG_KEYFRAME : in coda_finish_decode()
2495 decoded_frame->error = err_mb; in coda_finish_decode()
2498 if (display_idx == -1) { in coda_finish_decode()
2503 ctx->hold = true; in coda_finish_decode()
2504 } else if (display_idx == -3) { in coda_finish_decode()
2506 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) { in coda_finish_decode()
2507 v4l2_err(&dev->v4l2_dev, in coda_finish_decode()
2513 if (ctx->display_idx >= 0 && in coda_finish_decode()
2514 ctx->display_idx < ctx->num_internal_frames) { in coda_finish_decode()
2517 ready_frame = &ctx->internal_frames[ctx->display_idx]; in coda_finish_decode()
2519 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); in coda_finish_decode()
2520 dst_buf->sequence = ctx->osequence++; in coda_finish_decode()
2522 dst_buf->field = V4L2_FIELD_NONE; in coda_finish_decode()
2523 dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | in coda_finish_decode()
2526 dst_buf->flags |= ready_frame->type; in coda_finish_decode()
2527 meta = &ready_frame->meta; in coda_finish_decode()
2528 if (meta->last && !coda_reorder_enable(ctx)) { in coda_finish_decode()
2534 dst_buf->flags |= V4L2_BUF_FLAG_LAST; in coda_finish_decode()
2535 } else if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG && in coda_finish_decode()
2536 display_idx == -1) { in coda_finish_decode()
2543 dst_buf->flags |= V4L2_BUF_FLAG_LAST; in coda_finish_decode()
2545 dst_buf->timecode = meta->timecode; in coda_finish_decode()
2546 dst_buf->vb2_buf.timestamp = meta->timestamp; in coda_finish_decode()
2550 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, in coda_finish_decode()
2551 q_data_dst->sizeimage); in coda_finish_decode()
2553 if (ready_frame->error || err_vdoa) in coda_finish_decode()
2560 coda_frame_type_char(decoded_frame->type), in coda_finish_decode()
2561 decoded_frame->meta.sequence, in coda_finish_decode()
2562 coda_frame_type_char(dst_buf->flags), in coda_finish_decode()
2563 ready_frame->meta.sequence, in coda_finish_decode()
2564 dst_buf->sequence, ctx->qsequence, in coda_finish_decode()
2565 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? in coda_finish_decode()
2570 coda_frame_type_char(dst_buf->flags), in coda_finish_decode()
2571 ready_frame->meta.sequence, in coda_finish_decode()
2572 dst_buf->sequence, ctx->qsequence, in coda_finish_decode()
2573 (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? in coda_finish_decode()
2579 coda_frame_type_char(decoded_frame->type), in coda_finish_decode()
2580 decoded_frame->meta.sequence, in coda_finish_decode()
2581 ctx->display_idx); in coda_finish_decode()
2584 decoded_idx, ctx->display_idx); in coda_finish_decode()
2589 ctx->display_idx = display_idx; in coda_finish_decode()
2598 mutex_lock(&ctx->bitstream_mutex); in coda_finish_decode()
2600 mutex_unlock(&ctx->bitstream_mutex); in coda_finish_decode()
2613 if (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG)) in coda_decode_timeout()
2616 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); in coda_decode_timeout()
2617 dst_buf->sequence = ctx->qsequence - 1; in coda_decode_timeout()
2645 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev); in coda_irq_handler()
2647 v4l2_err(&dev->v4l2_dev, in coda_irq_handler()
2654 if (ctx->aborting) { in coda_irq_handler()
2658 if (coda_isbusy(ctx->dev)) { in coda_irq_handler()
2663 complete(&ctx->completion); in coda_irq_handler()