1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Hantro G1 post-processor support
4 *
5 * Copyright (C) 2019 Collabora, Ltd.
6 */
7
8 #include <linux/dma-mapping.h>
9 #include <linux/types.h>
10
11 #include "hantro.h"
12 #include "hantro_hw.h"
13 #include "hantro_g1_regs.h"
14
15 #define HANTRO_PP_REG_WRITE(vpu, reg_name, val) \
16 { \
17 hantro_reg_write(vpu, \
18 &(vpu)->variant->postproc_regs->reg_name, \
19 val); \
20 }
21
22 #define HANTRO_PP_REG_WRITE_S(vpu, reg_name, val) \
23 { \
24 hantro_reg_write_s(vpu, \
25 &(vpu)->variant->postproc_regs->reg_name, \
26 val); \
27 }
28
29 #define VPU_PP_IN_YUYV 0x0
30 #define VPU_PP_IN_NV12 0x1
31 #define VPU_PP_IN_YUV420 0x2
32 #define VPU_PP_IN_YUV240_TILED 0x5
33 #define VPU_PP_OUT_RGB 0x0
34 #define VPU_PP_OUT_YUYV 0x3
35
36 const struct hantro_postproc_regs hantro_g1_postproc_regs = {
37 .pipeline_en = {G1_REG_PP_INTERRUPT, 1, 0x1},
38 .max_burst = {G1_REG_PP_DEV_CONFIG, 0, 0x1f},
39 .clk_gate = {G1_REG_PP_DEV_CONFIG, 1, 0x1},
40 .out_swap32 = {G1_REG_PP_DEV_CONFIG, 5, 0x1},
41 .out_endian = {G1_REG_PP_DEV_CONFIG, 6, 0x1},
42 .out_luma_base = {G1_REG_PP_OUT_LUMA_BASE, 0, 0xffffffff},
43 .input_width = {G1_REG_PP_INPUT_SIZE, 0, 0x1ff},
44 .input_height = {G1_REG_PP_INPUT_SIZE, 9, 0x1ff},
45 .output_width = {G1_REG_PP_CONTROL, 4, 0x7ff},
46 .output_height = {G1_REG_PP_CONTROL, 15, 0x7ff},
47 .input_fmt = {G1_REG_PP_CONTROL, 29, 0x7},
48 .output_fmt = {G1_REG_PP_CONTROL, 26, 0x7},
49 .orig_width = {G1_REG_PP_MASK1_ORIG_WIDTH, 23, 0x1ff},
50 .display_width = {G1_REG_PP_DISPLAY_WIDTH, 0, 0xfff},
51 };
52
hantro_needs_postproc(const struct hantro_ctx * ctx,const struct hantro_fmt * fmt)53 bool hantro_needs_postproc(const struct hantro_ctx *ctx,
54 const struct hantro_fmt *fmt)
55 {
56 if (ctx->is_encoder)
57 return false;
58 return fmt->postprocessed;
59 }
60
hantro_postproc_enable(struct hantro_ctx * ctx)61 void hantro_postproc_enable(struct hantro_ctx *ctx)
62 {
63 struct hantro_dev *vpu = ctx->dev;
64 struct vb2_v4l2_buffer *dst_buf;
65 u32 src_pp_fmt, dst_pp_fmt;
66 dma_addr_t dst_dma;
67
68 if (!vpu->variant->postproc_regs)
69 return;
70
71 /* Turn on pipeline mode. Must be done first. */
72 HANTRO_PP_REG_WRITE_S(vpu, pipeline_en, 0x1);
73
74 src_pp_fmt = VPU_PP_IN_NV12;
75
76 switch (ctx->vpu_dst_fmt->fourcc) {
77 case V4L2_PIX_FMT_YUYV:
78 dst_pp_fmt = VPU_PP_OUT_YUYV;
79 break;
80 default:
81 WARN(1, "output format %d not supported by the post-processor, this wasn't expected.",
82 ctx->vpu_dst_fmt->fourcc);
83 dst_pp_fmt = 0;
84 break;
85 }
86
87 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
88 dst_dma = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
89
90 HANTRO_PP_REG_WRITE(vpu, clk_gate, 0x1);
91 HANTRO_PP_REG_WRITE(vpu, out_endian, 0x1);
92 HANTRO_PP_REG_WRITE(vpu, out_swap32, 0x1);
93 HANTRO_PP_REG_WRITE(vpu, max_burst, 16);
94 HANTRO_PP_REG_WRITE(vpu, out_luma_base, dst_dma);
95 HANTRO_PP_REG_WRITE(vpu, input_width, MB_WIDTH(ctx->dst_fmt.width));
96 HANTRO_PP_REG_WRITE(vpu, input_height, MB_HEIGHT(ctx->dst_fmt.height));
97 HANTRO_PP_REG_WRITE(vpu, input_fmt, src_pp_fmt);
98 HANTRO_PP_REG_WRITE(vpu, output_fmt, dst_pp_fmt);
99 HANTRO_PP_REG_WRITE(vpu, output_width, ctx->dst_fmt.width);
100 HANTRO_PP_REG_WRITE(vpu, output_height, ctx->dst_fmt.height);
101 HANTRO_PP_REG_WRITE(vpu, orig_width, MB_WIDTH(ctx->dst_fmt.width));
102 HANTRO_PP_REG_WRITE(vpu, display_width, ctx->dst_fmt.width);
103 }
104
hantro_postproc_free(struct hantro_ctx * ctx)105 void hantro_postproc_free(struct hantro_ctx *ctx)
106 {
107 struct hantro_dev *vpu = ctx->dev;
108 unsigned int i;
109
110 for (i = 0; i < VB2_MAX_FRAME; ++i) {
111 struct hantro_aux_buf *priv = &ctx->postproc.dec_q[i];
112
113 if (priv->cpu) {
114 dma_free_attrs(vpu->dev, priv->size, priv->cpu,
115 priv->dma, priv->attrs);
116 priv->cpu = NULL;
117 }
118 }
119 }
120
hantro_postproc_alloc(struct hantro_ctx * ctx)121 int hantro_postproc_alloc(struct hantro_ctx *ctx)
122 {
123 struct hantro_dev *vpu = ctx->dev;
124 struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
125 struct vb2_queue *cap_queue = &m2m_ctx->cap_q_ctx.q;
126 unsigned int num_buffers = cap_queue->num_buffers;
127 unsigned int i, buf_size;
128
129 buf_size = ctx->dst_fmt.plane_fmt[0].sizeimage;
130 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE)
131 buf_size += hantro_h264_mv_size(ctx->dst_fmt.width,
132 ctx->dst_fmt.height);
133
134 for (i = 0; i < num_buffers; ++i) {
135 struct hantro_aux_buf *priv = &ctx->postproc.dec_q[i];
136
137 /*
138 * The buffers on this queue are meant as intermediate
139 * buffers for the decoder, so no mapping is needed.
140 */
141 priv->attrs = DMA_ATTR_NO_KERNEL_MAPPING;
142 priv->cpu = dma_alloc_attrs(vpu->dev, buf_size, &priv->dma,
143 GFP_KERNEL, priv->attrs);
144 if (!priv->cpu)
145 return -ENOMEM;
146 priv->size = buf_size;
147 }
148 return 0;
149 }
150
hantro_postproc_disable(struct hantro_ctx * ctx)151 void hantro_postproc_disable(struct hantro_ctx *ctx)
152 {
153 struct hantro_dev *vpu = ctx->dev;
154
155 if (!vpu->variant->postproc_regs)
156 return;
157
158 HANTRO_PP_REG_WRITE_S(vpu, pipeline_en, 0x0);
159 }
160