1*fbb6c848SEzequiel Garcia // SPDX-License-Identifier: GPL-2.0
2*fbb6c848SEzequiel Garcia /*
3*fbb6c848SEzequiel Garcia  * Hantro VP9 codec driver
4*fbb6c848SEzequiel Garcia  *
5*fbb6c848SEzequiel Garcia  * Copyright (C) 2021 Collabora Ltd.
6*fbb6c848SEzequiel Garcia  */
7*fbb6c848SEzequiel Garcia #include "media/videobuf2-core.h"
8*fbb6c848SEzequiel Garcia #include "media/videobuf2-dma-contig.h"
9*fbb6c848SEzequiel Garcia #include "media/videobuf2-v4l2.h"
10*fbb6c848SEzequiel Garcia #include <linux/kernel.h>
11*fbb6c848SEzequiel Garcia #include <linux/vmalloc.h>
12*fbb6c848SEzequiel Garcia #include <media/v4l2-mem2mem.h>
13*fbb6c848SEzequiel Garcia #include <media/v4l2-vp9.h>
14*fbb6c848SEzequiel Garcia 
15*fbb6c848SEzequiel Garcia #include "hantro.h"
16*fbb6c848SEzequiel Garcia #include "hantro_vp9.h"
17*fbb6c848SEzequiel Garcia #include "hantro_g2_regs.h"
18*fbb6c848SEzequiel Garcia 
19*fbb6c848SEzequiel Garcia #define G2_ALIGN 16
20*fbb6c848SEzequiel Garcia 
21*fbb6c848SEzequiel Garcia enum hantro_ref_frames {
22*fbb6c848SEzequiel Garcia 	INTRA_FRAME = 0,
23*fbb6c848SEzequiel Garcia 	LAST_FRAME = 1,
24*fbb6c848SEzequiel Garcia 	GOLDEN_FRAME = 2,
25*fbb6c848SEzequiel Garcia 	ALTREF_FRAME = 3,
26*fbb6c848SEzequiel Garcia 	MAX_REF_FRAMES = 4
27*fbb6c848SEzequiel Garcia };
28*fbb6c848SEzequiel Garcia 
start_prepare_run(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame ** dec_params)29*fbb6c848SEzequiel Garcia static int start_prepare_run(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame **dec_params)
30*fbb6c848SEzequiel Garcia {
31*fbb6c848SEzequiel Garcia 	const struct v4l2_ctrl_vp9_compressed_hdr *prob_updates;
32*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
33*fbb6c848SEzequiel Garcia 	struct v4l2_ctrl *ctrl;
34*fbb6c848SEzequiel Garcia 	unsigned int fctx_idx;
35*fbb6c848SEzequiel Garcia 
36*fbb6c848SEzequiel Garcia 	/* v4l2-specific stuff */
37*fbb6c848SEzequiel Garcia 	hantro_start_prepare_run(ctx);
38*fbb6c848SEzequiel Garcia 
39*fbb6c848SEzequiel Garcia 	ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, V4L2_CID_STATELESS_VP9_FRAME);
40*fbb6c848SEzequiel Garcia 	if (WARN_ON(!ctrl))
41*fbb6c848SEzequiel Garcia 		return -EINVAL;
42*fbb6c848SEzequiel Garcia 	*dec_params = ctrl->p_cur.p;
43*fbb6c848SEzequiel Garcia 
44*fbb6c848SEzequiel Garcia 	ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, V4L2_CID_STATELESS_VP9_COMPRESSED_HDR);
45*fbb6c848SEzequiel Garcia 	if (WARN_ON(!ctrl))
46*fbb6c848SEzequiel Garcia 		return -EINVAL;
47*fbb6c848SEzequiel Garcia 	prob_updates = ctrl->p_cur.p;
48*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.tx_mode = prob_updates->tx_mode;
49*fbb6c848SEzequiel Garcia 
50*fbb6c848SEzequiel Garcia 	/*
51*fbb6c848SEzequiel Garcia 	 * vp9 stuff
52*fbb6c848SEzequiel Garcia 	 *
53*fbb6c848SEzequiel Garcia 	 * by this point the userspace has done all parts of 6.2 uncompressed_header()
54*fbb6c848SEzequiel Garcia 	 * except this fragment:
55*fbb6c848SEzequiel Garcia 	 * if ( FrameIsIntra || error_resilient_mode ) {
56*fbb6c848SEzequiel Garcia 	 *	setup_past_independence ( )
57*fbb6c848SEzequiel Garcia 	 *	if ( frame_type == KEY_FRAME || error_resilient_mode == 1 ||
58*fbb6c848SEzequiel Garcia 	 *	     reset_frame_context == 3 ) {
59*fbb6c848SEzequiel Garcia 	 *		for ( i = 0; i < 4; i ++ ) {
60*fbb6c848SEzequiel Garcia 	 *			save_probs( i )
61*fbb6c848SEzequiel Garcia 	 *		}
62*fbb6c848SEzequiel Garcia 	 *	} else if ( reset_frame_context == 2 ) {
63*fbb6c848SEzequiel Garcia 	 *		save_probs( frame_context_idx )
64*fbb6c848SEzequiel Garcia 	 *	}
65*fbb6c848SEzequiel Garcia 	 *	frame_context_idx = 0
66*fbb6c848SEzequiel Garcia 	 * }
67*fbb6c848SEzequiel Garcia 	 */
68*fbb6c848SEzequiel Garcia 	fctx_idx = v4l2_vp9_reset_frame_ctx(*dec_params, vp9_ctx->frame_context);
69*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.frame_context_idx = fctx_idx;
70*fbb6c848SEzequiel Garcia 
71*fbb6c848SEzequiel Garcia 	/* 6.1 frame(sz): load_probs() and load_probs2() */
72*fbb6c848SEzequiel Garcia 	vp9_ctx->probability_tables = vp9_ctx->frame_context[fctx_idx];
73*fbb6c848SEzequiel Garcia 
74*fbb6c848SEzequiel Garcia 	/*
75*fbb6c848SEzequiel Garcia 	 * The userspace has also performed 6.3 compressed_header(), but handling the
76*fbb6c848SEzequiel Garcia 	 * probs in a special way. All probs which need updating, except MV-related,
77*fbb6c848SEzequiel Garcia 	 * have been read from the bitstream and translated through inv_map_table[],
78*fbb6c848SEzequiel Garcia 	 * but no 6.3.6 inv_recenter_nonneg(v, m) has been performed. The values passed
79*fbb6c848SEzequiel Garcia 	 * by userspace are either translated values (there are no 0 values in
80*fbb6c848SEzequiel Garcia 	 * inv_map_table[]), or zero to indicate no update. All MV-related probs which need
81*fbb6c848SEzequiel Garcia 	 * updating have been read from the bitstream and (mv_prob << 1) | 1 has been
82*fbb6c848SEzequiel Garcia 	 * performed. The values passed by userspace are either new values
83*fbb6c848SEzequiel Garcia 	 * to replace old ones (the above mentioned shift and bitwise or never result in
84*fbb6c848SEzequiel Garcia 	 * a zero) or zero to indicate no update.
85*fbb6c848SEzequiel Garcia 	 * fw_update_probs() performs actual probs updates or leaves probs as-is
86*fbb6c848SEzequiel Garcia 	 * for values for which a zero was passed from userspace.
87*fbb6c848SEzequiel Garcia 	 */
88*fbb6c848SEzequiel Garcia 	v4l2_vp9_fw_update_probs(&vp9_ctx->probability_tables, prob_updates, *dec_params);
89*fbb6c848SEzequiel Garcia 
90*fbb6c848SEzequiel Garcia 	return 0;
91*fbb6c848SEzequiel Garcia }
92*fbb6c848SEzequiel Garcia 
chroma_offset(const struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)93*fbb6c848SEzequiel Garcia static size_t chroma_offset(const struct hantro_ctx *ctx,
94*fbb6c848SEzequiel Garcia 			    const struct v4l2_ctrl_vp9_frame *dec_params)
95*fbb6c848SEzequiel Garcia {
96*fbb6c848SEzequiel Garcia 	int bytes_per_pixel = dec_params->bit_depth == 8 ? 1 : 2;
97*fbb6c848SEzequiel Garcia 
98*fbb6c848SEzequiel Garcia 	return ctx->src_fmt.width * ctx->src_fmt.height * bytes_per_pixel;
99*fbb6c848SEzequiel Garcia }
100*fbb6c848SEzequiel Garcia 
mv_offset(const struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)101*fbb6c848SEzequiel Garcia static size_t mv_offset(const struct hantro_ctx *ctx,
102*fbb6c848SEzequiel Garcia 			const struct v4l2_ctrl_vp9_frame *dec_params)
103*fbb6c848SEzequiel Garcia {
104*fbb6c848SEzequiel Garcia 	size_t cr_offset = chroma_offset(ctx, dec_params);
105*fbb6c848SEzequiel Garcia 
106*fbb6c848SEzequiel Garcia 	return ALIGN((cr_offset * 3) / 2, G2_ALIGN);
107*fbb6c848SEzequiel Garcia }
108*fbb6c848SEzequiel Garcia 
109*fbb6c848SEzequiel Garcia static struct hantro_decoded_buffer *
get_ref_buf(struct hantro_ctx * ctx,struct vb2_v4l2_buffer * dst,u64 timestamp)110*fbb6c848SEzequiel Garcia get_ref_buf(struct hantro_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp)
111*fbb6c848SEzequiel Garcia {
112*fbb6c848SEzequiel Garcia 	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
113*fbb6c848SEzequiel Garcia 	struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
114*fbb6c848SEzequiel Garcia 	struct vb2_buffer *buf;
115*fbb6c848SEzequiel Garcia 
116*fbb6c848SEzequiel Garcia 	/*
117*fbb6c848SEzequiel Garcia 	 * If a ref is unused or invalid, address of current destination
118*fbb6c848SEzequiel Garcia 	 * buffer is returned.
119*fbb6c848SEzequiel Garcia 	 */
120*fbb6c848SEzequiel Garcia 	buf = vb2_find_buffer(cap_q, timestamp);
121*fbb6c848SEzequiel Garcia 	if (!buf)
122*fbb6c848SEzequiel Garcia 		buf = &dst->vb2_buf;
123*fbb6c848SEzequiel Garcia 
124*fbb6c848SEzequiel Garcia 	return vb2_to_hantro_decoded_buf(buf);
125*fbb6c848SEzequiel Garcia }
126*fbb6c848SEzequiel Garcia 
update_dec_buf_info(struct hantro_decoded_buffer * buf,const struct v4l2_ctrl_vp9_frame * dec_params)127*fbb6c848SEzequiel Garcia static void update_dec_buf_info(struct hantro_decoded_buffer *buf,
128*fbb6c848SEzequiel Garcia 				const struct v4l2_ctrl_vp9_frame *dec_params)
129*fbb6c848SEzequiel Garcia {
130*fbb6c848SEzequiel Garcia 	buf->vp9.width = dec_params->frame_width_minus_1 + 1;
131*fbb6c848SEzequiel Garcia 	buf->vp9.height = dec_params->frame_height_minus_1 + 1;
132*fbb6c848SEzequiel Garcia 	buf->vp9.bit_depth = dec_params->bit_depth;
133*fbb6c848SEzequiel Garcia }
134*fbb6c848SEzequiel Garcia 
update_ctx_cur_info(struct hantro_vp9_dec_hw_ctx * vp9_ctx,struct hantro_decoded_buffer * buf,const struct v4l2_ctrl_vp9_frame * dec_params)135*fbb6c848SEzequiel Garcia static void update_ctx_cur_info(struct hantro_vp9_dec_hw_ctx *vp9_ctx,
136*fbb6c848SEzequiel Garcia 				struct hantro_decoded_buffer *buf,
137*fbb6c848SEzequiel Garcia 				const struct v4l2_ctrl_vp9_frame *dec_params)
138*fbb6c848SEzequiel Garcia {
139*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.valid = true;
140*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.reference_mode = dec_params->reference_mode;
141*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.interpolation_filter = dec_params->interpolation_filter;
142*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.flags = dec_params->flags;
143*fbb6c848SEzequiel Garcia 	vp9_ctx->cur.timestamp = buf->base.vb.vb2_buf.timestamp;
144*fbb6c848SEzequiel Garcia }
145*fbb6c848SEzequiel Garcia 
config_output(struct hantro_ctx * ctx,struct hantro_decoded_buffer * dst,const struct v4l2_ctrl_vp9_frame * dec_params)146*fbb6c848SEzequiel Garcia static void config_output(struct hantro_ctx *ctx,
147*fbb6c848SEzequiel Garcia 			  struct hantro_decoded_buffer *dst,
148*fbb6c848SEzequiel Garcia 			  const struct v4l2_ctrl_vp9_frame *dec_params)
149*fbb6c848SEzequiel Garcia {
150*fbb6c848SEzequiel Garcia 	dma_addr_t luma_addr, chroma_addr, mv_addr;
151*fbb6c848SEzequiel Garcia 
152*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_out_dis, 0);
153*fbb6c848SEzequiel Garcia 	if (!ctx->dev->variant->legacy_regs)
154*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_output_format, 0);
155*fbb6c848SEzequiel Garcia 
156*fbb6c848SEzequiel Garcia 	luma_addr = hantro_get_dec_buf_addr(ctx, &dst->base.vb.vb2_buf);
157*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_OUT_LUMA_ADDR, luma_addr);
158*fbb6c848SEzequiel Garcia 
159*fbb6c848SEzequiel Garcia 	chroma_addr = luma_addr + chroma_offset(ctx, dec_params);
160*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_OUT_CHROMA_ADDR, chroma_addr);
161*fbb6c848SEzequiel Garcia 
162*fbb6c848SEzequiel Garcia 	mv_addr = luma_addr + mv_offset(ctx, dec_params);
163*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_OUT_MV_ADDR, mv_addr);
164*fbb6c848SEzequiel Garcia }
165*fbb6c848SEzequiel Garcia 
166*fbb6c848SEzequiel Garcia struct hantro_vp9_ref_reg {
167*fbb6c848SEzequiel Garcia 	const struct hantro_reg width;
168*fbb6c848SEzequiel Garcia 	const struct hantro_reg height;
169*fbb6c848SEzequiel Garcia 	const struct hantro_reg hor_scale;
170*fbb6c848SEzequiel Garcia 	const struct hantro_reg ver_scale;
171*fbb6c848SEzequiel Garcia 	u32 y_base;
172*fbb6c848SEzequiel Garcia 	u32 c_base;
173*fbb6c848SEzequiel Garcia };
174*fbb6c848SEzequiel Garcia 
config_ref(struct hantro_ctx * ctx,struct hantro_decoded_buffer * dst,const struct hantro_vp9_ref_reg * ref_reg,const struct v4l2_ctrl_vp9_frame * dec_params,u64 ref_ts)175*fbb6c848SEzequiel Garcia static void config_ref(struct hantro_ctx *ctx,
176*fbb6c848SEzequiel Garcia 		       struct hantro_decoded_buffer *dst,
177*fbb6c848SEzequiel Garcia 		       const struct hantro_vp9_ref_reg *ref_reg,
178*fbb6c848SEzequiel Garcia 		       const struct v4l2_ctrl_vp9_frame *dec_params,
179*fbb6c848SEzequiel Garcia 		       u64 ref_ts)
180*fbb6c848SEzequiel Garcia {
181*fbb6c848SEzequiel Garcia 	struct hantro_decoded_buffer *buf;
182*fbb6c848SEzequiel Garcia 	dma_addr_t luma_addr, chroma_addr;
183*fbb6c848SEzequiel Garcia 	u32 refw, refh;
184*fbb6c848SEzequiel Garcia 
185*fbb6c848SEzequiel Garcia 	buf = get_ref_buf(ctx, &dst->base.vb, ref_ts);
186*fbb6c848SEzequiel Garcia 	refw = buf->vp9.width;
187*fbb6c848SEzequiel Garcia 	refh = buf->vp9.height;
188*fbb6c848SEzequiel Garcia 
189*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &ref_reg->width, refw);
190*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &ref_reg->height, refh);
191*fbb6c848SEzequiel Garcia 
192*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &ref_reg->hor_scale, (refw << 14) / dst->vp9.width);
193*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &ref_reg->ver_scale, (refh << 14) / dst->vp9.height);
194*fbb6c848SEzequiel Garcia 
195*fbb6c848SEzequiel Garcia 	luma_addr = hantro_get_dec_buf_addr(ctx, &buf->base.vb.vb2_buf);
196*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, ref_reg->y_base, luma_addr);
197*fbb6c848SEzequiel Garcia 
198*fbb6c848SEzequiel Garcia 	chroma_addr = luma_addr + chroma_offset(ctx, dec_params);
199*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, ref_reg->c_base, chroma_addr);
200*fbb6c848SEzequiel Garcia }
201*fbb6c848SEzequiel Garcia 
config_ref_registers(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params,struct hantro_decoded_buffer * dst,struct hantro_decoded_buffer * mv_ref)202*fbb6c848SEzequiel Garcia static void config_ref_registers(struct hantro_ctx *ctx,
203*fbb6c848SEzequiel Garcia 				 const struct v4l2_ctrl_vp9_frame *dec_params,
204*fbb6c848SEzequiel Garcia 				 struct hantro_decoded_buffer *dst,
205*fbb6c848SEzequiel Garcia 				 struct hantro_decoded_buffer *mv_ref)
206*fbb6c848SEzequiel Garcia {
207*fbb6c848SEzequiel Garcia 	static const struct hantro_vp9_ref_reg ref_regs[] = {
208*fbb6c848SEzequiel Garcia 		{
209*fbb6c848SEzequiel Garcia 			/* Last */
210*fbb6c848SEzequiel Garcia 			.width = vp9_lref_width,
211*fbb6c848SEzequiel Garcia 			.height = vp9_lref_height,
212*fbb6c848SEzequiel Garcia 			.hor_scale = vp9_lref_hor_scale,
213*fbb6c848SEzequiel Garcia 			.ver_scale = vp9_lref_ver_scale,
214*fbb6c848SEzequiel Garcia 			.y_base = G2_REF_LUMA_ADDR(0),
215*fbb6c848SEzequiel Garcia 			.c_base = G2_REF_CHROMA_ADDR(0),
216*fbb6c848SEzequiel Garcia 		}, {
217*fbb6c848SEzequiel Garcia 			/* Golden */
218*fbb6c848SEzequiel Garcia 			.width = vp9_gref_width,
219*fbb6c848SEzequiel Garcia 			.height = vp9_gref_height,
220*fbb6c848SEzequiel Garcia 			.hor_scale = vp9_gref_hor_scale,
221*fbb6c848SEzequiel Garcia 			.ver_scale = vp9_gref_ver_scale,
222*fbb6c848SEzequiel Garcia 			.y_base = G2_REF_LUMA_ADDR(4),
223*fbb6c848SEzequiel Garcia 			.c_base = G2_REF_CHROMA_ADDR(4),
224*fbb6c848SEzequiel Garcia 		}, {
225*fbb6c848SEzequiel Garcia 			/* Altref */
226*fbb6c848SEzequiel Garcia 			.width = vp9_aref_width,
227*fbb6c848SEzequiel Garcia 			.height = vp9_aref_height,
228*fbb6c848SEzequiel Garcia 			.hor_scale = vp9_aref_hor_scale,
229*fbb6c848SEzequiel Garcia 			.ver_scale = vp9_aref_ver_scale,
230*fbb6c848SEzequiel Garcia 			.y_base = G2_REF_LUMA_ADDR(5),
231*fbb6c848SEzequiel Garcia 			.c_base = G2_REF_CHROMA_ADDR(5),
232*fbb6c848SEzequiel Garcia 		},
233*fbb6c848SEzequiel Garcia 	};
234*fbb6c848SEzequiel Garcia 	dma_addr_t mv_addr;
235*fbb6c848SEzequiel Garcia 
236*fbb6c848SEzequiel Garcia 	config_ref(ctx, dst, &ref_regs[0], dec_params, dec_params->last_frame_ts);
237*fbb6c848SEzequiel Garcia 	config_ref(ctx, dst, &ref_regs[1], dec_params, dec_params->golden_frame_ts);
238*fbb6c848SEzequiel Garcia 	config_ref(ctx, dst, &ref_regs[2], dec_params, dec_params->alt_frame_ts);
239*fbb6c848SEzequiel Garcia 
240*fbb6c848SEzequiel Garcia 	mv_addr = hantro_get_dec_buf_addr(ctx, &mv_ref->base.vb.vb2_buf) +
241*fbb6c848SEzequiel Garcia 		  mv_offset(ctx, dec_params);
242*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_REF_MV_ADDR(0), mv_addr);
243*fbb6c848SEzequiel Garcia 
244*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_last_sign_bias,
245*fbb6c848SEzequiel Garcia 			 dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_LAST ? 1 : 0);
246*fbb6c848SEzequiel Garcia 
247*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_gref_sign_bias,
248*fbb6c848SEzequiel Garcia 			 dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_GOLDEN ? 1 : 0);
249*fbb6c848SEzequiel Garcia 
250*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_aref_sign_bias,
251*fbb6c848SEzequiel Garcia 			 dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_ALT ? 1 : 0);
252*fbb6c848SEzequiel Garcia }
253*fbb6c848SEzequiel Garcia 
recompute_tile_info(unsigned short * tile_info,unsigned int tiles,unsigned int sbs)254*fbb6c848SEzequiel Garcia static void recompute_tile_info(unsigned short *tile_info, unsigned int tiles, unsigned int sbs)
255*fbb6c848SEzequiel Garcia {
256*fbb6c848SEzequiel Garcia 	int i;
257*fbb6c848SEzequiel Garcia 	unsigned int accumulated = 0;
258*fbb6c848SEzequiel Garcia 	unsigned int next_accumulated;
259*fbb6c848SEzequiel Garcia 
260*fbb6c848SEzequiel Garcia 	for (i = 1; i <= tiles; ++i) {
261*fbb6c848SEzequiel Garcia 		next_accumulated = i * sbs / tiles;
262*fbb6c848SEzequiel Garcia 		*tile_info++ = next_accumulated - accumulated;
263*fbb6c848SEzequiel Garcia 		accumulated = next_accumulated;
264*fbb6c848SEzequiel Garcia 	}
265*fbb6c848SEzequiel Garcia }
266*fbb6c848SEzequiel Garcia 
267*fbb6c848SEzequiel Garcia static void
recompute_tile_rc_info(struct hantro_ctx * ctx,unsigned int tile_r,unsigned int tile_c,unsigned int sbs_r,unsigned int sbs_c)268*fbb6c848SEzequiel Garcia recompute_tile_rc_info(struct hantro_ctx *ctx,
269*fbb6c848SEzequiel Garcia 		       unsigned int tile_r, unsigned int tile_c,
270*fbb6c848SEzequiel Garcia 		       unsigned int sbs_r, unsigned int sbs_c)
271*fbb6c848SEzequiel Garcia {
272*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
273*fbb6c848SEzequiel Garcia 
274*fbb6c848SEzequiel Garcia 	recompute_tile_info(vp9_ctx->tile_r_info, tile_r, sbs_r);
275*fbb6c848SEzequiel Garcia 	recompute_tile_info(vp9_ctx->tile_c_info, tile_c, sbs_c);
276*fbb6c848SEzequiel Garcia 
277*fbb6c848SEzequiel Garcia 	vp9_ctx->last_tile_r = tile_r;
278*fbb6c848SEzequiel Garcia 	vp9_ctx->last_tile_c = tile_c;
279*fbb6c848SEzequiel Garcia 	vp9_ctx->last_sbs_r = sbs_r;
280*fbb6c848SEzequiel Garcia 	vp9_ctx->last_sbs_c = sbs_c;
281*fbb6c848SEzequiel Garcia }
282*fbb6c848SEzequiel Garcia 
first_tile_row(unsigned int tile_r,unsigned int sbs_r)283*fbb6c848SEzequiel Garcia static inline unsigned int first_tile_row(unsigned int tile_r, unsigned int sbs_r)
284*fbb6c848SEzequiel Garcia {
285*fbb6c848SEzequiel Garcia 	if (tile_r == sbs_r + 1)
286*fbb6c848SEzequiel Garcia 		return 1;
287*fbb6c848SEzequiel Garcia 
288*fbb6c848SEzequiel Garcia 	if (tile_r == sbs_r + 2)
289*fbb6c848SEzequiel Garcia 		return 2;
290*fbb6c848SEzequiel Garcia 
291*fbb6c848SEzequiel Garcia 	return 0;
292*fbb6c848SEzequiel Garcia }
293*fbb6c848SEzequiel Garcia 
294*fbb6c848SEzequiel Garcia static void
fill_tile_info(struct hantro_ctx * ctx,unsigned int tile_r,unsigned int tile_c,unsigned int sbs_r,unsigned int sbs_c,unsigned short * tile_mem)295*fbb6c848SEzequiel Garcia fill_tile_info(struct hantro_ctx *ctx,
296*fbb6c848SEzequiel Garcia 	       unsigned int tile_r, unsigned int tile_c,
297*fbb6c848SEzequiel Garcia 	       unsigned int sbs_r, unsigned int sbs_c,
298*fbb6c848SEzequiel Garcia 	       unsigned short *tile_mem)
299*fbb6c848SEzequiel Garcia {
300*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
301*fbb6c848SEzequiel Garcia 	unsigned int i, j;
302*fbb6c848SEzequiel Garcia 	bool first = true;
303*fbb6c848SEzequiel Garcia 
304*fbb6c848SEzequiel Garcia 	for (i = first_tile_row(tile_r, sbs_r); i < tile_r; ++i) {
305*fbb6c848SEzequiel Garcia 		unsigned short r_info = vp9_ctx->tile_r_info[i];
306*fbb6c848SEzequiel Garcia 
307*fbb6c848SEzequiel Garcia 		if (first) {
308*fbb6c848SEzequiel Garcia 			if (i > 0)
309*fbb6c848SEzequiel Garcia 				r_info += vp9_ctx->tile_r_info[0];
310*fbb6c848SEzequiel Garcia 			if (i == 2)
311*fbb6c848SEzequiel Garcia 				r_info += vp9_ctx->tile_r_info[1];
312*fbb6c848SEzequiel Garcia 			first = false;
313*fbb6c848SEzequiel Garcia 		}
314*fbb6c848SEzequiel Garcia 		for (j = 0; j < tile_c; ++j) {
315*fbb6c848SEzequiel Garcia 			*tile_mem++ = vp9_ctx->tile_c_info[j];
316*fbb6c848SEzequiel Garcia 			*tile_mem++ = r_info;
317*fbb6c848SEzequiel Garcia 		}
318*fbb6c848SEzequiel Garcia 	}
319*fbb6c848SEzequiel Garcia }
320*fbb6c848SEzequiel Garcia 
321*fbb6c848SEzequiel Garcia static void
config_tiles(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params,struct hantro_decoded_buffer * dst)322*fbb6c848SEzequiel Garcia config_tiles(struct hantro_ctx *ctx,
323*fbb6c848SEzequiel Garcia 	     const struct v4l2_ctrl_vp9_frame *dec_params,
324*fbb6c848SEzequiel Garcia 	     struct hantro_decoded_buffer *dst)
325*fbb6c848SEzequiel Garcia {
326*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
327*fbb6c848SEzequiel Garcia 	struct hantro_aux_buf *misc = &vp9_ctx->misc;
328*fbb6c848SEzequiel Garcia 	struct hantro_aux_buf *tile_edge = &vp9_ctx->tile_edge;
329*fbb6c848SEzequiel Garcia 	dma_addr_t addr;
330*fbb6c848SEzequiel Garcia 	unsigned short *tile_mem;
331*fbb6c848SEzequiel Garcia 	unsigned int rows, cols;
332*fbb6c848SEzequiel Garcia 
333*fbb6c848SEzequiel Garcia 	addr = misc->dma + vp9_ctx->tile_info_offset;
334*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_TILE_SIZES_ADDR, addr);
335*fbb6c848SEzequiel Garcia 
336*fbb6c848SEzequiel Garcia 	tile_mem = misc->cpu + vp9_ctx->tile_info_offset;
337*fbb6c848SEzequiel Garcia 	if (dec_params->tile_cols_log2 || dec_params->tile_rows_log2) {
338*fbb6c848SEzequiel Garcia 		unsigned int tile_r = (1 << dec_params->tile_rows_log2);
339*fbb6c848SEzequiel Garcia 		unsigned int tile_c = (1 << dec_params->tile_cols_log2);
340*fbb6c848SEzequiel Garcia 		unsigned int sbs_r = hantro_vp9_num_sbs(dst->vp9.height);
341*fbb6c848SEzequiel Garcia 		unsigned int sbs_c = hantro_vp9_num_sbs(dst->vp9.width);
342*fbb6c848SEzequiel Garcia 
343*fbb6c848SEzequiel Garcia 		if (tile_r != vp9_ctx->last_tile_r || tile_c != vp9_ctx->last_tile_c ||
344*fbb6c848SEzequiel Garcia 		    sbs_r != vp9_ctx->last_sbs_r || sbs_c != vp9_ctx->last_sbs_c)
345*fbb6c848SEzequiel Garcia 			recompute_tile_rc_info(ctx, tile_r, tile_c, sbs_r, sbs_c);
346*fbb6c848SEzequiel Garcia 
347*fbb6c848SEzequiel Garcia 		fill_tile_info(ctx, tile_r, tile_c, sbs_r, sbs_c, tile_mem);
348*fbb6c848SEzequiel Garcia 
349*fbb6c848SEzequiel Garcia 		cols = tile_c;
350*fbb6c848SEzequiel Garcia 		rows = tile_r;
351*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_tile_e, 1);
352*fbb6c848SEzequiel Garcia 	} else {
353*fbb6c848SEzequiel Garcia 		tile_mem[0] = hantro_vp9_num_sbs(dst->vp9.width);
354*fbb6c848SEzequiel Garcia 		tile_mem[1] = hantro_vp9_num_sbs(dst->vp9.height);
355*fbb6c848SEzequiel Garcia 
356*fbb6c848SEzequiel Garcia 		cols = 1;
357*fbb6c848SEzequiel Garcia 		rows = 1;
358*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_tile_e, 0);
359*fbb6c848SEzequiel Garcia 	}
360*fbb6c848SEzequiel Garcia 
361*fbb6c848SEzequiel Garcia 	if (ctx->dev->variant->legacy_regs) {
362*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_num_tile_cols_old, cols);
363*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_num_tile_rows_old, rows);
364*fbb6c848SEzequiel Garcia 	} else {
365*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_num_tile_cols, cols);
366*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_num_tile_rows, rows);
367*fbb6c848SEzequiel Garcia 	}
368*fbb6c848SEzequiel Garcia 
369*fbb6c848SEzequiel Garcia 	/* provide aux buffers even if no tiles are used */
370*fbb6c848SEzequiel Garcia 	addr = tile_edge->dma;
371*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_TILE_FILTER_ADDR, addr);
372*fbb6c848SEzequiel Garcia 
373*fbb6c848SEzequiel Garcia 	addr = tile_edge->dma + vp9_ctx->bsd_ctrl_offset;
374*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_TILE_BSD_ADDR, addr);
375*fbb6c848SEzequiel Garcia }
376*fbb6c848SEzequiel Garcia 
377*fbb6c848SEzequiel Garcia static void
update_feat_and_flag(struct hantro_vp9_dec_hw_ctx * vp9_ctx,const struct v4l2_vp9_segmentation * seg,unsigned int feature,unsigned int segid)378*fbb6c848SEzequiel Garcia update_feat_and_flag(struct hantro_vp9_dec_hw_ctx *vp9_ctx,
379*fbb6c848SEzequiel Garcia 		     const struct v4l2_vp9_segmentation *seg,
380*fbb6c848SEzequiel Garcia 		     unsigned int feature,
381*fbb6c848SEzequiel Garcia 		     unsigned int segid)
382*fbb6c848SEzequiel Garcia {
383*fbb6c848SEzequiel Garcia 	u8 mask = V4L2_VP9_SEGMENT_FEATURE_ENABLED(feature);
384*fbb6c848SEzequiel Garcia 
385*fbb6c848SEzequiel Garcia 	vp9_ctx->feature_data[segid][feature] = seg->feature_data[segid][feature];
386*fbb6c848SEzequiel Garcia 	vp9_ctx->feature_enabled[segid] &= ~mask;
387*fbb6c848SEzequiel Garcia 	vp9_ctx->feature_enabled[segid] |= (seg->feature_enabled[segid] & mask);
388*fbb6c848SEzequiel Garcia }
389*fbb6c848SEzequiel Garcia 
clip3(s16 x,s16 y,s16 z)390*fbb6c848SEzequiel Garcia static inline s16 clip3(s16 x, s16 y, s16 z)
391*fbb6c848SEzequiel Garcia {
392*fbb6c848SEzequiel Garcia 	return (z < x) ? x : (z > y) ? y : z;
393*fbb6c848SEzequiel Garcia }
394*fbb6c848SEzequiel Garcia 
feat_val_clip3(s16 feat_val,s16 feature_data,bool absolute,u8 clip)395*fbb6c848SEzequiel Garcia static s16 feat_val_clip3(s16 feat_val, s16 feature_data, bool absolute, u8 clip)
396*fbb6c848SEzequiel Garcia {
397*fbb6c848SEzequiel Garcia 	if (absolute)
398*fbb6c848SEzequiel Garcia 		return feature_data;
399*fbb6c848SEzequiel Garcia 
400*fbb6c848SEzequiel Garcia 	return clip3(0, 255, feat_val + feature_data);
401*fbb6c848SEzequiel Garcia }
402*fbb6c848SEzequiel Garcia 
config_segment(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)403*fbb6c848SEzequiel Garcia static void config_segment(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
404*fbb6c848SEzequiel Garcia {
405*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
406*fbb6c848SEzequiel Garcia 	const struct v4l2_vp9_segmentation *seg;
407*fbb6c848SEzequiel Garcia 	s16 feat_val;
408*fbb6c848SEzequiel Garcia 	unsigned char feat_id;
409*fbb6c848SEzequiel Garcia 	unsigned int segid;
410*fbb6c848SEzequiel Garcia 	bool segment_enabled, absolute, update_data;
411*fbb6c848SEzequiel Garcia 
412*fbb6c848SEzequiel Garcia 	static const struct hantro_reg seg_regs[8][V4L2_VP9_SEG_LVL_MAX] = {
413*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg0, vp9_filt_level_seg0, vp9_refpic_seg0, vp9_skip_seg0 },
414*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg1, vp9_filt_level_seg1, vp9_refpic_seg1, vp9_skip_seg1 },
415*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg2, vp9_filt_level_seg2, vp9_refpic_seg2, vp9_skip_seg2 },
416*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg3, vp9_filt_level_seg3, vp9_refpic_seg3, vp9_skip_seg3 },
417*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg4, vp9_filt_level_seg4, vp9_refpic_seg4, vp9_skip_seg4 },
418*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg5, vp9_filt_level_seg5, vp9_refpic_seg5, vp9_skip_seg5 },
419*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg6, vp9_filt_level_seg6, vp9_refpic_seg6, vp9_skip_seg6 },
420*fbb6c848SEzequiel Garcia 		{ vp9_quant_seg7, vp9_filt_level_seg7, vp9_refpic_seg7, vp9_skip_seg7 },
421*fbb6c848SEzequiel Garcia 	};
422*fbb6c848SEzequiel Garcia 
423*fbb6c848SEzequiel Garcia 	segment_enabled = !!(dec_params->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED);
424*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_segment_e, segment_enabled);
425*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_segment_upd_e,
426*fbb6c848SEzequiel Garcia 			 !!(dec_params->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP));
427*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_segment_temp_upd_e,
428*fbb6c848SEzequiel Garcia 			 !!(dec_params->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE));
429*fbb6c848SEzequiel Garcia 
430*fbb6c848SEzequiel Garcia 	seg = &dec_params->seg;
431*fbb6c848SEzequiel Garcia 	absolute = !!(seg->flags & V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE);
432*fbb6c848SEzequiel Garcia 	update_data = !!(seg->flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA);
433*fbb6c848SEzequiel Garcia 
434*fbb6c848SEzequiel Garcia 	for (segid = 0; segid < 8; ++segid) {
435*fbb6c848SEzequiel Garcia 		/* Quantizer segment feature */
436*fbb6c848SEzequiel Garcia 		feat_id = V4L2_VP9_SEG_LVL_ALT_Q;
437*fbb6c848SEzequiel Garcia 		feat_val = dec_params->quant.base_q_idx;
438*fbb6c848SEzequiel Garcia 		if (segment_enabled) {
439*fbb6c848SEzequiel Garcia 			if (update_data)
440*fbb6c848SEzequiel Garcia 				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
441*fbb6c848SEzequiel Garcia 			if (v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled, feat_id, segid))
442*fbb6c848SEzequiel Garcia 				feat_val = feat_val_clip3(feat_val,
443*fbb6c848SEzequiel Garcia 							  vp9_ctx->feature_data[segid][feat_id],
444*fbb6c848SEzequiel Garcia 							  absolute, 255);
445*fbb6c848SEzequiel Garcia 		}
446*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);
447*fbb6c848SEzequiel Garcia 
448*fbb6c848SEzequiel Garcia 		/* Loop filter segment feature */
449*fbb6c848SEzequiel Garcia 		feat_id = V4L2_VP9_SEG_LVL_ALT_L;
450*fbb6c848SEzequiel Garcia 		feat_val = dec_params->lf.level;
451*fbb6c848SEzequiel Garcia 		if (segment_enabled) {
452*fbb6c848SEzequiel Garcia 			if (update_data)
453*fbb6c848SEzequiel Garcia 				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
454*fbb6c848SEzequiel Garcia 			if (v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled, feat_id, segid))
455*fbb6c848SEzequiel Garcia 				feat_val = feat_val_clip3(feat_val,
456*fbb6c848SEzequiel Garcia 							  vp9_ctx->feature_data[segid][feat_id],
457*fbb6c848SEzequiel Garcia 							  absolute, 63);
458*fbb6c848SEzequiel Garcia 		}
459*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);
460*fbb6c848SEzequiel Garcia 
461*fbb6c848SEzequiel Garcia 		/* Reference frame segment feature */
462*fbb6c848SEzequiel Garcia 		feat_id = V4L2_VP9_SEG_LVL_REF_FRAME;
463*fbb6c848SEzequiel Garcia 		feat_val = 0;
464*fbb6c848SEzequiel Garcia 		if (segment_enabled) {
465*fbb6c848SEzequiel Garcia 			if (update_data)
466*fbb6c848SEzequiel Garcia 				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
467*fbb6c848SEzequiel Garcia 			if (!(dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) &&
468*fbb6c848SEzequiel Garcia 			    v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled, feat_id, segid))
469*fbb6c848SEzequiel Garcia 				feat_val = vp9_ctx->feature_data[segid][feat_id] + 1;
470*fbb6c848SEzequiel Garcia 		}
471*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);
472*fbb6c848SEzequiel Garcia 
473*fbb6c848SEzequiel Garcia 		/* Skip segment feature */
474*fbb6c848SEzequiel Garcia 		feat_id = V4L2_VP9_SEG_LVL_SKIP;
475*fbb6c848SEzequiel Garcia 		feat_val = 0;
476*fbb6c848SEzequiel Garcia 		if (segment_enabled) {
477*fbb6c848SEzequiel Garcia 			if (update_data)
478*fbb6c848SEzequiel Garcia 				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
479*fbb6c848SEzequiel Garcia 			feat_val = v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled,
480*fbb6c848SEzequiel Garcia 							     feat_id, segid) ? 1 : 0;
481*fbb6c848SEzequiel Garcia 		}
482*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);
483*fbb6c848SEzequiel Garcia 	}
484*fbb6c848SEzequiel Garcia }
485*fbb6c848SEzequiel Garcia 
config_loop_filter(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)486*fbb6c848SEzequiel Garcia static void config_loop_filter(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
487*fbb6c848SEzequiel Garcia {
488*fbb6c848SEzequiel Garcia 	bool d = dec_params->lf.flags & V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED;
489*fbb6c848SEzequiel Garcia 
490*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_level, dec_params->lf.level);
491*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_out_filtering_dis, dec_params->lf.level == 0);
492*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_sharpness, dec_params->lf.sharpness);
493*fbb6c848SEzequiel Garcia 
494*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_0, d ? dec_params->lf.ref_deltas[0] : 0);
495*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_1, d ? dec_params->lf.ref_deltas[1] : 0);
496*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_2, d ? dec_params->lf.ref_deltas[2] : 0);
497*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_3, d ? dec_params->lf.ref_deltas[3] : 0);
498*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_mb_adj_0, d ? dec_params->lf.mode_deltas[0] : 0);
499*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_filt_mb_adj_1, d ? dec_params->lf.mode_deltas[1] : 0);
500*fbb6c848SEzequiel Garcia }
501*fbb6c848SEzequiel Garcia 
config_picture_dimensions(struct hantro_ctx * ctx,struct hantro_decoded_buffer * dst)502*fbb6c848SEzequiel Garcia static void config_picture_dimensions(struct hantro_ctx *ctx, struct hantro_decoded_buffer *dst)
503*fbb6c848SEzequiel Garcia {
504*fbb6c848SEzequiel Garcia 	u32 pic_w_4x4, pic_h_4x4;
505*fbb6c848SEzequiel Garcia 
506*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_pic_width_in_cbs, (dst->vp9.width + 7) / 8);
507*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_pic_height_in_cbs, (dst->vp9.height + 7) / 8);
508*fbb6c848SEzequiel Garcia 	pic_w_4x4 = roundup(dst->vp9.width, 8) >> 2;
509*fbb6c848SEzequiel Garcia 	pic_h_4x4 = roundup(dst->vp9.height, 8) >> 2;
510*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_pic_width_4x4, pic_w_4x4);
511*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_pic_height_4x4, pic_h_4x4);
512*fbb6c848SEzequiel Garcia }
513*fbb6c848SEzequiel Garcia 
514*fbb6c848SEzequiel Garcia static void
config_bit_depth(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)515*fbb6c848SEzequiel Garcia config_bit_depth(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
516*fbb6c848SEzequiel Garcia {
517*fbb6c848SEzequiel Garcia 	if (ctx->dev->variant->legacy_regs) {
518*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_bit_depth_y, dec_params->bit_depth);
519*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_bit_depth_c, dec_params->bit_depth);
520*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_pix_shift, 0);
521*fbb6c848SEzequiel Garcia 	} else {
522*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_bit_depth_y_minus8, dec_params->bit_depth - 8);
523*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_bit_depth_c_minus8, dec_params->bit_depth - 8);
524*fbb6c848SEzequiel Garcia 	}
525*fbb6c848SEzequiel Garcia }
526*fbb6c848SEzequiel Garcia 
is_lossless(const struct v4l2_vp9_quantization * quant)527*fbb6c848SEzequiel Garcia static inline bool is_lossless(const struct v4l2_vp9_quantization *quant)
528*fbb6c848SEzequiel Garcia {
529*fbb6c848SEzequiel Garcia 	return quant->base_q_idx == 0 && quant->delta_q_uv_ac == 0 &&
530*fbb6c848SEzequiel Garcia 	       quant->delta_q_uv_dc == 0 && quant->delta_q_y_dc == 0;
531*fbb6c848SEzequiel Garcia }
532*fbb6c848SEzequiel Garcia 
533*fbb6c848SEzequiel Garcia static void
config_quant(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)534*fbb6c848SEzequiel Garcia config_quant(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
535*fbb6c848SEzequiel Garcia {
536*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_qp_delta_y_dc, dec_params->quant.delta_q_y_dc);
537*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_qp_delta_ch_dc, dec_params->quant.delta_q_uv_dc);
538*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_qp_delta_ch_ac, dec_params->quant.delta_q_uv_ac);
539*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_lossless_e, is_lossless(&dec_params->quant));
540*fbb6c848SEzequiel Garcia }
541*fbb6c848SEzequiel Garcia 
542*fbb6c848SEzequiel Garcia static u32
hantro_interp_filter_from_v4l2(unsigned int interpolation_filter)543*fbb6c848SEzequiel Garcia hantro_interp_filter_from_v4l2(unsigned int interpolation_filter)
544*fbb6c848SEzequiel Garcia {
545*fbb6c848SEzequiel Garcia 	switch (interpolation_filter) {
546*fbb6c848SEzequiel Garcia 	case V4L2_VP9_INTERP_FILTER_EIGHTTAP:
547*fbb6c848SEzequiel Garcia 		return 0x1;
548*fbb6c848SEzequiel Garcia 	case V4L2_VP9_INTERP_FILTER_EIGHTTAP_SMOOTH:
549*fbb6c848SEzequiel Garcia 		return 0;
550*fbb6c848SEzequiel Garcia 	case V4L2_VP9_INTERP_FILTER_EIGHTTAP_SHARP:
551*fbb6c848SEzequiel Garcia 		return 0x2;
552*fbb6c848SEzequiel Garcia 	case V4L2_VP9_INTERP_FILTER_BILINEAR:
553*fbb6c848SEzequiel Garcia 		return 0x3;
554*fbb6c848SEzequiel Garcia 	case V4L2_VP9_INTERP_FILTER_SWITCHABLE:
555*fbb6c848SEzequiel Garcia 		return 0x4;
556*fbb6c848SEzequiel Garcia 	}
557*fbb6c848SEzequiel Garcia 
558*fbb6c848SEzequiel Garcia 	return 0;
559*fbb6c848SEzequiel Garcia }
560*fbb6c848SEzequiel Garcia 
561*fbb6c848SEzequiel Garcia static void
config_others(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params,bool intra_only,bool resolution_change)562*fbb6c848SEzequiel Garcia config_others(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params,
563*fbb6c848SEzequiel Garcia 	      bool intra_only, bool resolution_change)
564*fbb6c848SEzequiel Garcia {
565*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
566*fbb6c848SEzequiel Garcia 
567*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_idr_pic_e, intra_only);
568*fbb6c848SEzequiel Garcia 
569*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_transform_mode, vp9_ctx->cur.tx_mode);
570*fbb6c848SEzequiel Garcia 
571*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_mcomp_filt_type, intra_only ?
572*fbb6c848SEzequiel Garcia 		0 : hantro_interp_filter_from_v4l2(dec_params->interpolation_filter));
573*fbb6c848SEzequiel Garcia 
574*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_high_prec_mv_e,
575*fbb6c848SEzequiel Garcia 			 !!(dec_params->flags & V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV));
576*fbb6c848SEzequiel Garcia 
577*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_comp_pred_mode, dec_params->reference_mode);
578*fbb6c848SEzequiel Garcia 
579*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_tempor_mvp_e,
580*fbb6c848SEzequiel Garcia 			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) &&
581*fbb6c848SEzequiel Garcia 			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) &&
582*fbb6c848SEzequiel Garcia 			 !(vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) &&
583*fbb6c848SEzequiel Garcia 			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_INTRA_ONLY) &&
584*fbb6c848SEzequiel Garcia 			 !resolution_change &&
585*fbb6c848SEzequiel Garcia 			 vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_SHOW_FRAME
586*fbb6c848SEzequiel Garcia 	);
587*fbb6c848SEzequiel Garcia 
588*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_write_mvs_e,
589*fbb6c848SEzequiel Garcia 			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME));
590*fbb6c848SEzequiel Garcia }
591*fbb6c848SEzequiel Garcia 
592*fbb6c848SEzequiel Garcia static void
config_compound_reference(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)593*fbb6c848SEzequiel Garcia config_compound_reference(struct hantro_ctx *ctx,
594*fbb6c848SEzequiel Garcia 			  const struct v4l2_ctrl_vp9_frame *dec_params)
595*fbb6c848SEzequiel Garcia {
596*fbb6c848SEzequiel Garcia 	u32 comp_fixed_ref, comp_var_ref[2];
597*fbb6c848SEzequiel Garcia 	bool last_ref_frame_sign_bias;
598*fbb6c848SEzequiel Garcia 	bool golden_ref_frame_sign_bias;
599*fbb6c848SEzequiel Garcia 	bool alt_ref_frame_sign_bias;
600*fbb6c848SEzequiel Garcia 	bool comp_ref_allowed = 0;
601*fbb6c848SEzequiel Garcia 
602*fbb6c848SEzequiel Garcia 	comp_fixed_ref = 0;
603*fbb6c848SEzequiel Garcia 	comp_var_ref[0] = 0;
604*fbb6c848SEzequiel Garcia 	comp_var_ref[1] = 0;
605*fbb6c848SEzequiel Garcia 
606*fbb6c848SEzequiel Garcia 	last_ref_frame_sign_bias = dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_LAST;
607*fbb6c848SEzequiel Garcia 	golden_ref_frame_sign_bias = dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_GOLDEN;
608*fbb6c848SEzequiel Garcia 	alt_ref_frame_sign_bias = dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_ALT;
609*fbb6c848SEzequiel Garcia 
610*fbb6c848SEzequiel Garcia 	/* 6.3.12 Frame reference mode syntax */
611*fbb6c848SEzequiel Garcia 	comp_ref_allowed |= golden_ref_frame_sign_bias != last_ref_frame_sign_bias;
612*fbb6c848SEzequiel Garcia 	comp_ref_allowed |= alt_ref_frame_sign_bias != last_ref_frame_sign_bias;
613*fbb6c848SEzequiel Garcia 
614*fbb6c848SEzequiel Garcia 	if (comp_ref_allowed) {
615*fbb6c848SEzequiel Garcia 		if (last_ref_frame_sign_bias ==
616*fbb6c848SEzequiel Garcia 		    golden_ref_frame_sign_bias) {
617*fbb6c848SEzequiel Garcia 			comp_fixed_ref = ALTREF_FRAME;
618*fbb6c848SEzequiel Garcia 			comp_var_ref[0] = LAST_FRAME;
619*fbb6c848SEzequiel Garcia 			comp_var_ref[1] = GOLDEN_FRAME;
620*fbb6c848SEzequiel Garcia 		} else if (last_ref_frame_sign_bias ==
621*fbb6c848SEzequiel Garcia 			   alt_ref_frame_sign_bias) {
622*fbb6c848SEzequiel Garcia 			comp_fixed_ref = GOLDEN_FRAME;
623*fbb6c848SEzequiel Garcia 			comp_var_ref[0] = LAST_FRAME;
624*fbb6c848SEzequiel Garcia 			comp_var_ref[1] = ALTREF_FRAME;
625*fbb6c848SEzequiel Garcia 		} else {
626*fbb6c848SEzequiel Garcia 			comp_fixed_ref = LAST_FRAME;
627*fbb6c848SEzequiel Garcia 			comp_var_ref[0] = GOLDEN_FRAME;
628*fbb6c848SEzequiel Garcia 			comp_var_ref[1] = ALTREF_FRAME;
629*fbb6c848SEzequiel Garcia 		}
630*fbb6c848SEzequiel Garcia 	}
631*fbb6c848SEzequiel Garcia 
632*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_comp_pred_fixed_ref, comp_fixed_ref);
633*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_comp_pred_var_ref0, comp_var_ref[0]);
634*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &vp9_comp_pred_var_ref1, comp_var_ref[1]);
635*fbb6c848SEzequiel Garcia }
636*fbb6c848SEzequiel Garcia 
637*fbb6c848SEzequiel Garcia #define INNER_LOOP \
638*fbb6c848SEzequiel Garcia do {									\
639*fbb6c848SEzequiel Garcia 	for (m = 0; m < ARRAY_SIZE(adaptive->coef[0][0][0][0]); ++m) {	\
640*fbb6c848SEzequiel Garcia 		memcpy(adaptive->coef[i][j][k][l][m],			\
641*fbb6c848SEzequiel Garcia 		       probs->coef[i][j][k][l][m],			\
642*fbb6c848SEzequiel Garcia 		       sizeof(probs->coef[i][j][k][l][m]));		\
643*fbb6c848SEzequiel Garcia 									\
644*fbb6c848SEzequiel Garcia 		adaptive->coef[i][j][k][l][m][3] = 0;			\
645*fbb6c848SEzequiel Garcia 	}								\
646*fbb6c848SEzequiel Garcia } while (0)
647*fbb6c848SEzequiel Garcia 
config_probs(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params)648*fbb6c848SEzequiel Garcia static void config_probs(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
649*fbb6c848SEzequiel Garcia {
650*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
651*fbb6c848SEzequiel Garcia 	struct hantro_aux_buf *misc = &vp9_ctx->misc;
652*fbb6c848SEzequiel Garcia 	struct hantro_g2_all_probs *all_probs = misc->cpu;
653*fbb6c848SEzequiel Garcia 	struct hantro_g2_probs *adaptive;
654*fbb6c848SEzequiel Garcia 	struct hantro_g2_mv_probs *mv;
655*fbb6c848SEzequiel Garcia 	const struct v4l2_vp9_segmentation *seg = &dec_params->seg;
656*fbb6c848SEzequiel Garcia 	const struct v4l2_vp9_frame_context *probs = &vp9_ctx->probability_tables;
657*fbb6c848SEzequiel Garcia 	int i, j, k, l, m;
658*fbb6c848SEzequiel Garcia 
659*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(all_probs->kf_y_mode_prob); ++i)
660*fbb6c848SEzequiel Garcia 		for (j = 0; j < ARRAY_SIZE(all_probs->kf_y_mode_prob[0]); ++j) {
661*fbb6c848SEzequiel Garcia 			memcpy(all_probs->kf_y_mode_prob[i][j],
662*fbb6c848SEzequiel Garcia 			       v4l2_vp9_kf_y_mode_prob[i][j],
663*fbb6c848SEzequiel Garcia 			       ARRAY_SIZE(all_probs->kf_y_mode_prob[i][j]));
664*fbb6c848SEzequiel Garcia 
665*fbb6c848SEzequiel Garcia 			all_probs->kf_y_mode_prob_tail[i][j][0] =
666*fbb6c848SEzequiel Garcia 				v4l2_vp9_kf_y_mode_prob[i][j][8];
667*fbb6c848SEzequiel Garcia 		}
668*fbb6c848SEzequiel Garcia 
669*fbb6c848SEzequiel Garcia 	memcpy(all_probs->mb_segment_tree_probs, seg->tree_probs,
670*fbb6c848SEzequiel Garcia 	       sizeof(all_probs->mb_segment_tree_probs));
671*fbb6c848SEzequiel Garcia 
672*fbb6c848SEzequiel Garcia 	memcpy(all_probs->segment_pred_probs, seg->pred_probs,
673*fbb6c848SEzequiel Garcia 	       sizeof(all_probs->segment_pred_probs));
674*fbb6c848SEzequiel Garcia 
675*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(all_probs->kf_uv_mode_prob); ++i) {
676*fbb6c848SEzequiel Garcia 		memcpy(all_probs->kf_uv_mode_prob[i], v4l2_vp9_kf_uv_mode_prob[i],
677*fbb6c848SEzequiel Garcia 		       ARRAY_SIZE(all_probs->kf_uv_mode_prob[i]));
678*fbb6c848SEzequiel Garcia 
679*fbb6c848SEzequiel Garcia 		all_probs->kf_uv_mode_prob_tail[i][0] = v4l2_vp9_kf_uv_mode_prob[i][8];
680*fbb6c848SEzequiel Garcia 	}
681*fbb6c848SEzequiel Garcia 
682*fbb6c848SEzequiel Garcia 	adaptive = &all_probs->probs;
683*fbb6c848SEzequiel Garcia 
684*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(adaptive->inter_mode); ++i) {
685*fbb6c848SEzequiel Garcia 		memcpy(adaptive->inter_mode[i], probs->inter_mode[i],
686*fbb6c848SEzequiel Garcia 		       ARRAY_SIZE(probs->inter_mode[i]));
687*fbb6c848SEzequiel Garcia 
688*fbb6c848SEzequiel Garcia 		adaptive->inter_mode[i][3] = 0;
689*fbb6c848SEzequiel Garcia 	}
690*fbb6c848SEzequiel Garcia 
691*fbb6c848SEzequiel Garcia 	memcpy(adaptive->is_inter, probs->is_inter, sizeof(adaptive->is_inter));
692*fbb6c848SEzequiel Garcia 
693*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(adaptive->uv_mode); ++i) {
694*fbb6c848SEzequiel Garcia 		memcpy(adaptive->uv_mode[i], probs->uv_mode[i],
695*fbb6c848SEzequiel Garcia 		       sizeof(adaptive->uv_mode[i]));
696*fbb6c848SEzequiel Garcia 		adaptive->uv_mode_tail[i][0] = probs->uv_mode[i][8];
697*fbb6c848SEzequiel Garcia 	}
698*fbb6c848SEzequiel Garcia 
699*fbb6c848SEzequiel Garcia 	memcpy(adaptive->tx8, probs->tx8, sizeof(adaptive->tx8));
700*fbb6c848SEzequiel Garcia 	memcpy(adaptive->tx16, probs->tx16, sizeof(adaptive->tx16));
701*fbb6c848SEzequiel Garcia 	memcpy(adaptive->tx32, probs->tx32, sizeof(adaptive->tx32));
702*fbb6c848SEzequiel Garcia 
703*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(adaptive->y_mode); ++i) {
704*fbb6c848SEzequiel Garcia 		memcpy(adaptive->y_mode[i], probs->y_mode[i],
705*fbb6c848SEzequiel Garcia 		       ARRAY_SIZE(adaptive->y_mode[i]));
706*fbb6c848SEzequiel Garcia 
707*fbb6c848SEzequiel Garcia 		adaptive->y_mode_tail[i][0] = probs->y_mode[i][8];
708*fbb6c848SEzequiel Garcia 	}
709*fbb6c848SEzequiel Garcia 
710*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(adaptive->partition[0]); ++i) {
711*fbb6c848SEzequiel Garcia 		memcpy(adaptive->partition[0][i], v4l2_vp9_kf_partition_probs[i],
712*fbb6c848SEzequiel Garcia 		       sizeof(v4l2_vp9_kf_partition_probs[i]));
713*fbb6c848SEzequiel Garcia 
714*fbb6c848SEzequiel Garcia 		adaptive->partition[0][i][3] = 0;
715*fbb6c848SEzequiel Garcia 	}
716*fbb6c848SEzequiel Garcia 
717*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(adaptive->partition[1]); ++i) {
718*fbb6c848SEzequiel Garcia 		memcpy(adaptive->partition[1][i], probs->partition[i],
719*fbb6c848SEzequiel Garcia 		       sizeof(probs->partition[i]));
720*fbb6c848SEzequiel Garcia 
721*fbb6c848SEzequiel Garcia 		adaptive->partition[1][i][3] = 0;
722*fbb6c848SEzequiel Garcia 	}
723*fbb6c848SEzequiel Garcia 
724*fbb6c848SEzequiel Garcia 	memcpy(adaptive->interp_filter, probs->interp_filter,
725*fbb6c848SEzequiel Garcia 	       sizeof(adaptive->interp_filter));
726*fbb6c848SEzequiel Garcia 
727*fbb6c848SEzequiel Garcia 	memcpy(adaptive->comp_mode, probs->comp_mode, sizeof(adaptive->comp_mode));
728*fbb6c848SEzequiel Garcia 
729*fbb6c848SEzequiel Garcia 	memcpy(adaptive->skip, probs->skip, sizeof(adaptive->skip));
730*fbb6c848SEzequiel Garcia 
731*fbb6c848SEzequiel Garcia 	mv = &adaptive->mv;
732*fbb6c848SEzequiel Garcia 
733*fbb6c848SEzequiel Garcia 	memcpy(mv->joint, probs->mv.joint, sizeof(mv->joint));
734*fbb6c848SEzequiel Garcia 	memcpy(mv->sign, probs->mv.sign, sizeof(mv->sign));
735*fbb6c848SEzequiel Garcia 	memcpy(mv->class0_bit, probs->mv.class0_bit, sizeof(mv->class0_bit));
736*fbb6c848SEzequiel Garcia 	memcpy(mv->fr, probs->mv.fr, sizeof(mv->fr));
737*fbb6c848SEzequiel Garcia 	memcpy(mv->class0_hp, probs->mv.class0_hp, sizeof(mv->class0_hp));
738*fbb6c848SEzequiel Garcia 	memcpy(mv->hp, probs->mv.hp, sizeof(mv->hp));
739*fbb6c848SEzequiel Garcia 	memcpy(mv->classes, probs->mv.classes, sizeof(mv->classes));
740*fbb6c848SEzequiel Garcia 	memcpy(mv->class0_fr, probs->mv.class0_fr, sizeof(mv->class0_fr));
741*fbb6c848SEzequiel Garcia 	memcpy(mv->bits, probs->mv.bits, sizeof(mv->bits));
742*fbb6c848SEzequiel Garcia 
743*fbb6c848SEzequiel Garcia 	memcpy(adaptive->single_ref, probs->single_ref, sizeof(adaptive->single_ref));
744*fbb6c848SEzequiel Garcia 
745*fbb6c848SEzequiel Garcia 	memcpy(adaptive->comp_ref, probs->comp_ref, sizeof(adaptive->comp_ref));
746*fbb6c848SEzequiel Garcia 
747*fbb6c848SEzequiel Garcia 	for (i = 0; i < ARRAY_SIZE(adaptive->coef); ++i)
748*fbb6c848SEzequiel Garcia 		for (j = 0; j < ARRAY_SIZE(adaptive->coef[0]); ++j)
749*fbb6c848SEzequiel Garcia 			for (k = 0; k < ARRAY_SIZE(adaptive->coef[0][0]); ++k)
750*fbb6c848SEzequiel Garcia 				for (l = 0; l < ARRAY_SIZE(adaptive->coef[0][0][0]); ++l)
751*fbb6c848SEzequiel Garcia 					INNER_LOOP;
752*fbb6c848SEzequiel Garcia 
753*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_VP9_PROBS_ADDR, misc->dma);
754*fbb6c848SEzequiel Garcia }
755*fbb6c848SEzequiel Garcia 
config_counts(struct hantro_ctx * ctx)756*fbb6c848SEzequiel Garcia static void config_counts(struct hantro_ctx *ctx)
757*fbb6c848SEzequiel Garcia {
758*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_dec = &ctx->vp9_dec;
759*fbb6c848SEzequiel Garcia 	struct hantro_aux_buf *misc = &vp9_dec->misc;
760*fbb6c848SEzequiel Garcia 	dma_addr_t addr = misc->dma + vp9_dec->ctx_counters_offset;
761*fbb6c848SEzequiel Garcia 
762*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_VP9_CTX_COUNT_ADDR, addr);
763*fbb6c848SEzequiel Garcia }
764*fbb6c848SEzequiel Garcia 
config_seg_map(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params,bool intra_only,bool update_map)765*fbb6c848SEzequiel Garcia static void config_seg_map(struct hantro_ctx *ctx,
766*fbb6c848SEzequiel Garcia 			   const struct v4l2_ctrl_vp9_frame *dec_params,
767*fbb6c848SEzequiel Garcia 			   bool intra_only, bool update_map)
768*fbb6c848SEzequiel Garcia {
769*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
770*fbb6c848SEzequiel Garcia 	struct hantro_aux_buf *segment_map = &vp9_ctx->segment_map;
771*fbb6c848SEzequiel Garcia 	dma_addr_t addr;
772*fbb6c848SEzequiel Garcia 
773*fbb6c848SEzequiel Garcia 	if (intra_only ||
774*fbb6c848SEzequiel Garcia 	    (dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT)) {
775*fbb6c848SEzequiel Garcia 		memset(segment_map->cpu, 0, segment_map->size);
776*fbb6c848SEzequiel Garcia 		memset(vp9_ctx->feature_data, 0, sizeof(vp9_ctx->feature_data));
777*fbb6c848SEzequiel Garcia 		memset(vp9_ctx->feature_enabled, 0, sizeof(vp9_ctx->feature_enabled));
778*fbb6c848SEzequiel Garcia 	}
779*fbb6c848SEzequiel Garcia 
780*fbb6c848SEzequiel Garcia 	addr = segment_map->dma + vp9_ctx->active_segment * vp9_ctx->segment_map_size;
781*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_VP9_SEGMENT_READ_ADDR, addr);
782*fbb6c848SEzequiel Garcia 
783*fbb6c848SEzequiel Garcia 	addr = segment_map->dma + (1 - vp9_ctx->active_segment) * vp9_ctx->segment_map_size;
784*fbb6c848SEzequiel Garcia 	hantro_write_addr(ctx->dev, G2_VP9_SEGMENT_WRITE_ADDR, addr);
785*fbb6c848SEzequiel Garcia 
786*fbb6c848SEzequiel Garcia 	if (update_map)
787*fbb6c848SEzequiel Garcia 		vp9_ctx->active_segment = 1 - vp9_ctx->active_segment;
788*fbb6c848SEzequiel Garcia }
789*fbb6c848SEzequiel Garcia 
790*fbb6c848SEzequiel Garcia static void
config_source(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params,struct vb2_v4l2_buffer * vb2_src)791*fbb6c848SEzequiel Garcia config_source(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params,
792*fbb6c848SEzequiel Garcia 	      struct vb2_v4l2_buffer *vb2_src)
793*fbb6c848SEzequiel Garcia {
794*fbb6c848SEzequiel Garcia 	dma_addr_t stream_base, tmp_addr;
795*fbb6c848SEzequiel Garcia 	unsigned int headres_size;
796*fbb6c848SEzequiel Garcia 	u32 src_len, start_bit, src_buf_len;
797*fbb6c848SEzequiel Garcia 
798*fbb6c848SEzequiel Garcia 	headres_size = dec_params->uncompressed_header_size
799*fbb6c848SEzequiel Garcia 		     + dec_params->compressed_header_size;
800*fbb6c848SEzequiel Garcia 
801*fbb6c848SEzequiel Garcia 	stream_base = vb2_dma_contig_plane_dma_addr(&vb2_src->vb2_buf, 0);
802*fbb6c848SEzequiel Garcia 
803*fbb6c848SEzequiel Garcia 	tmp_addr = stream_base + headres_size;
804*fbb6c848SEzequiel Garcia 	if (ctx->dev->variant->legacy_regs)
805*fbb6c848SEzequiel Garcia 		hantro_write_addr(ctx->dev, G2_STREAM_ADDR, (tmp_addr & ~0xf));
806*fbb6c848SEzequiel Garcia 	else
807*fbb6c848SEzequiel Garcia 		hantro_write_addr(ctx->dev, G2_STREAM_ADDR, stream_base);
808*fbb6c848SEzequiel Garcia 
809*fbb6c848SEzequiel Garcia 	start_bit = (tmp_addr & 0xf) * 8;
810*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_start_bit, start_bit);
811*fbb6c848SEzequiel Garcia 
812*fbb6c848SEzequiel Garcia 	src_len = vb2_get_plane_payload(&vb2_src->vb2_buf, 0);
813*fbb6c848SEzequiel Garcia 	src_len += start_bit / 8 - headres_size;
814*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_stream_len, src_len);
815*fbb6c848SEzequiel Garcia 
816*fbb6c848SEzequiel Garcia 	if (!ctx->dev->variant->legacy_regs) {
817*fbb6c848SEzequiel Garcia 		tmp_addr &= ~0xf;
818*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_strm_start_offset, tmp_addr - stream_base);
819*fbb6c848SEzequiel Garcia 		src_buf_len = vb2_plane_size(&vb2_src->vb2_buf, 0);
820*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_strm_buffer_len, src_buf_len);
821*fbb6c848SEzequiel Garcia 	}
822*fbb6c848SEzequiel Garcia }
823*fbb6c848SEzequiel Garcia 
824*fbb6c848SEzequiel Garcia static void
config_registers(struct hantro_ctx * ctx,const struct v4l2_ctrl_vp9_frame * dec_params,struct vb2_v4l2_buffer * vb2_src,struct vb2_v4l2_buffer * vb2_dst)825*fbb6c848SEzequiel Garcia config_registers(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params,
826*fbb6c848SEzequiel Garcia 		 struct vb2_v4l2_buffer *vb2_src, struct vb2_v4l2_buffer *vb2_dst)
827*fbb6c848SEzequiel Garcia {
828*fbb6c848SEzequiel Garcia 	struct hantro_decoded_buffer *dst, *last, *mv_ref;
829*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
830*fbb6c848SEzequiel Garcia 	const struct v4l2_vp9_segmentation *seg;
831*fbb6c848SEzequiel Garcia 	bool intra_only, resolution_change;
832*fbb6c848SEzequiel Garcia 
833*fbb6c848SEzequiel Garcia 	/* vp9 stuff */
834*fbb6c848SEzequiel Garcia 	dst = vb2_to_hantro_decoded_buf(&vb2_dst->vb2_buf);
835*fbb6c848SEzequiel Garcia 
836*fbb6c848SEzequiel Garcia 	if (vp9_ctx->last.valid)
837*fbb6c848SEzequiel Garcia 		last = get_ref_buf(ctx, &dst->base.vb, vp9_ctx->last.timestamp);
838*fbb6c848SEzequiel Garcia 	else
839*fbb6c848SEzequiel Garcia 		last = dst;
840*fbb6c848SEzequiel Garcia 
841*fbb6c848SEzequiel Garcia 	update_dec_buf_info(dst, dec_params);
842*fbb6c848SEzequiel Garcia 	update_ctx_cur_info(vp9_ctx, dst, dec_params);
843*fbb6c848SEzequiel Garcia 	seg = &dec_params->seg;
844*fbb6c848SEzequiel Garcia 
845*fbb6c848SEzequiel Garcia 	intra_only = !!(dec_params->flags &
846*fbb6c848SEzequiel Garcia 			(V4L2_VP9_FRAME_FLAG_KEY_FRAME |
847*fbb6c848SEzequiel Garcia 			V4L2_VP9_FRAME_FLAG_INTRA_ONLY));
848*fbb6c848SEzequiel Garcia 
849*fbb6c848SEzequiel Garcia 	if (!intra_only &&
850*fbb6c848SEzequiel Garcia 	    !(dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) &&
851*fbb6c848SEzequiel Garcia 	    vp9_ctx->last.valid)
852*fbb6c848SEzequiel Garcia 		mv_ref = last;
853*fbb6c848SEzequiel Garcia 	else
854*fbb6c848SEzequiel Garcia 		mv_ref = dst;
855*fbb6c848SEzequiel Garcia 
856*fbb6c848SEzequiel Garcia 	resolution_change = dst->vp9.width != last->vp9.width ||
857*fbb6c848SEzequiel Garcia 			    dst->vp9.height != last->vp9.height;
858*fbb6c848SEzequiel Garcia 
859*fbb6c848SEzequiel Garcia 	/* configure basic registers */
860*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_mode, VP9_DEC_MODE);
861*fbb6c848SEzequiel Garcia 	if (!ctx->dev->variant->legacy_regs) {
862*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_strm_swap, 0xf);
863*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_dirmv_swap, 0xf);
864*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_compress_swap, 0xf);
865*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_ref_compress_bypass, 1);
866*fbb6c848SEzequiel Garcia 	} else {
867*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_strm_swap_old, 0x1f);
868*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_pic_swap, 0x10);
869*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_dirmv_swap_old, 0x10);
870*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_tab0_swap_old, 0x10);
871*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_tab1_swap_old, 0x10);
872*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_tab2_swap_old, 0x10);
873*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_tab3_swap_old, 0x10);
874*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_rscan_swap, 0x10);
875*fbb6c848SEzequiel Garcia 	}
876*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_buswidth, BUS_WIDTH_128);
877*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_max_burst, 16);
878*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_apf_threshold, 8);
879*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_clk_gate_e, 1);
880*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_max_cb_size, 6);
881*fbb6c848SEzequiel Garcia 	hantro_reg_write(ctx->dev, &g2_min_cb_size, 3);
882*fbb6c848SEzequiel Garcia 	if (ctx->dev->variant->double_buffer)
883*fbb6c848SEzequiel Garcia 		hantro_reg_write(ctx->dev, &g2_double_buffer_e, 1);
884*fbb6c848SEzequiel Garcia 
885*fbb6c848SEzequiel Garcia 	config_output(ctx, dst, dec_params);
886*fbb6c848SEzequiel Garcia 
887*fbb6c848SEzequiel Garcia 	if (!intra_only)
888*fbb6c848SEzequiel Garcia 		config_ref_registers(ctx, dec_params, dst, mv_ref);
889*fbb6c848SEzequiel Garcia 
890*fbb6c848SEzequiel Garcia 	config_tiles(ctx, dec_params, dst);
891*fbb6c848SEzequiel Garcia 	config_segment(ctx, dec_params);
892*fbb6c848SEzequiel Garcia 	config_loop_filter(ctx, dec_params);
893*fbb6c848SEzequiel Garcia 	config_picture_dimensions(ctx, dst);
894*fbb6c848SEzequiel Garcia 	config_bit_depth(ctx, dec_params);
895*fbb6c848SEzequiel Garcia 	config_quant(ctx, dec_params);
896*fbb6c848SEzequiel Garcia 	config_others(ctx, dec_params, intra_only, resolution_change);
897*fbb6c848SEzequiel Garcia 	config_compound_reference(ctx, dec_params);
898*fbb6c848SEzequiel Garcia 	config_probs(ctx, dec_params);
899*fbb6c848SEzequiel Garcia 	config_counts(ctx);
900*fbb6c848SEzequiel Garcia 	config_seg_map(ctx, dec_params, intra_only,
901*fbb6c848SEzequiel Garcia 		       seg->flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP);
902*fbb6c848SEzequiel Garcia 	config_source(ctx, dec_params, vb2_src);
903*fbb6c848SEzequiel Garcia }
904*fbb6c848SEzequiel Garcia 
hantro_g2_vp9_dec_run(struct hantro_ctx * ctx)905*fbb6c848SEzequiel Garcia int hantro_g2_vp9_dec_run(struct hantro_ctx *ctx)
906*fbb6c848SEzequiel Garcia {
907*fbb6c848SEzequiel Garcia 	const struct v4l2_ctrl_vp9_frame *decode_params;
908*fbb6c848SEzequiel Garcia 	struct vb2_v4l2_buffer *src;
909*fbb6c848SEzequiel Garcia 	struct vb2_v4l2_buffer *dst;
910*fbb6c848SEzequiel Garcia 	int ret;
911*fbb6c848SEzequiel Garcia 
912*fbb6c848SEzequiel Garcia 	hantro_g2_check_idle(ctx->dev);
913*fbb6c848SEzequiel Garcia 
914*fbb6c848SEzequiel Garcia 	ret = start_prepare_run(ctx, &decode_params);
915*fbb6c848SEzequiel Garcia 	if (ret) {
916*fbb6c848SEzequiel Garcia 		hantro_end_prepare_run(ctx);
917*fbb6c848SEzequiel Garcia 		return ret;
918*fbb6c848SEzequiel Garcia 	}
919*fbb6c848SEzequiel Garcia 
920*fbb6c848SEzequiel Garcia 	src = hantro_get_src_buf(ctx);
921*fbb6c848SEzequiel Garcia 	dst = hantro_get_dst_buf(ctx);
922*fbb6c848SEzequiel Garcia 
923*fbb6c848SEzequiel Garcia 	config_registers(ctx, decode_params, src, dst);
924*fbb6c848SEzequiel Garcia 
925*fbb6c848SEzequiel Garcia 	hantro_end_prepare_run(ctx);
926*fbb6c848SEzequiel Garcia 
927*fbb6c848SEzequiel Garcia 	vdpu_write(ctx->dev, G2_REG_INTERRUPT_DEC_E, G2_REG_INTERRUPT);
928*fbb6c848SEzequiel Garcia 
929*fbb6c848SEzequiel Garcia 	return 0;
930*fbb6c848SEzequiel Garcia }
931*fbb6c848SEzequiel Garcia 
932*fbb6c848SEzequiel Garcia #define copy_tx_and_skip(p1, p2)				\
933*fbb6c848SEzequiel Garcia do {								\
934*fbb6c848SEzequiel Garcia 	memcpy((p1)->tx8, (p2)->tx8, sizeof((p1)->tx8));	\
935*fbb6c848SEzequiel Garcia 	memcpy((p1)->tx16, (p2)->tx16, sizeof((p1)->tx16));	\
936*fbb6c848SEzequiel Garcia 	memcpy((p1)->tx32, (p2)->tx32, sizeof((p1)->tx32));	\
937*fbb6c848SEzequiel Garcia 	memcpy((p1)->skip, (p2)->skip, sizeof((p1)->skip));	\
938*fbb6c848SEzequiel Garcia } while (0)
939*fbb6c848SEzequiel Garcia 
hantro_g2_vp9_dec_done(struct hantro_ctx * ctx)940*fbb6c848SEzequiel Garcia void hantro_g2_vp9_dec_done(struct hantro_ctx *ctx)
941*fbb6c848SEzequiel Garcia {
942*fbb6c848SEzequiel Garcia 	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
943*fbb6c848SEzequiel Garcia 	unsigned int fctx_idx;
944*fbb6c848SEzequiel Garcia 
945*fbb6c848SEzequiel Garcia 	if (!(vp9_ctx->cur.flags & V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX))
946*fbb6c848SEzequiel Garcia 		goto out_update_last;
947*fbb6c848SEzequiel Garcia 
948*fbb6c848SEzequiel Garcia 	fctx_idx = vp9_ctx->cur.frame_context_idx;
949*fbb6c848SEzequiel Garcia 
950*fbb6c848SEzequiel Garcia 	if (!(vp9_ctx->cur.flags & V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE)) {
951*fbb6c848SEzequiel Garcia 		/* error_resilient_mode == 0 && frame_parallel_decoding_mode == 0 */
952*fbb6c848SEzequiel Garcia 		struct v4l2_vp9_frame_context *probs = &vp9_ctx->probability_tables;
953*fbb6c848SEzequiel Garcia 		bool frame_is_intra = vp9_ctx->cur.flags &
954*fbb6c848SEzequiel Garcia 		    (V4L2_VP9_FRAME_FLAG_KEY_FRAME | V4L2_VP9_FRAME_FLAG_INTRA_ONLY);
955*fbb6c848SEzequiel Garcia 		struct tx_and_skip {
956*fbb6c848SEzequiel Garcia 			u8 tx8[2][1];
957*fbb6c848SEzequiel Garcia 			u8 tx16[2][2];
958*fbb6c848SEzequiel Garcia 			u8 tx32[2][3];
959*fbb6c848SEzequiel Garcia 			u8 skip[3];
960*fbb6c848SEzequiel Garcia 		} _tx_skip, *tx_skip = &_tx_skip;
961*fbb6c848SEzequiel Garcia 		struct v4l2_vp9_frame_symbol_counts *counts;
962*fbb6c848SEzequiel Garcia 		struct symbol_counts *hantro_cnts;
963*fbb6c848SEzequiel Garcia 		u32 tx16p[2][4];
964*fbb6c848SEzequiel Garcia 		int i;
965*fbb6c848SEzequiel Garcia 
966*fbb6c848SEzequiel Garcia 		/* buffer the forward-updated TX and skip probs */
967*fbb6c848SEzequiel Garcia 		if (frame_is_intra)
968*fbb6c848SEzequiel Garcia 			copy_tx_and_skip(tx_skip, probs);
969*fbb6c848SEzequiel Garcia 
970*fbb6c848SEzequiel Garcia 		/* 6.1.2 refresh_probs(): load_probs() and load_probs2() */
971*fbb6c848SEzequiel Garcia 		*probs = vp9_ctx->frame_context[fctx_idx];
972*fbb6c848SEzequiel Garcia 
973*fbb6c848SEzequiel Garcia 		/* if FrameIsIntra then undo the effect of load_probs2() */
974*fbb6c848SEzequiel Garcia 		if (frame_is_intra)
975*fbb6c848SEzequiel Garcia 			copy_tx_and_skip(probs, tx_skip);
976*fbb6c848SEzequiel Garcia 
977*fbb6c848SEzequiel Garcia 		counts = &vp9_ctx->cnts;
978*fbb6c848SEzequiel Garcia 		hantro_cnts = vp9_ctx->misc.cpu + vp9_ctx->ctx_counters_offset;
979*fbb6c848SEzequiel Garcia 		for (i = 0; i < ARRAY_SIZE(tx16p); ++i) {
980*fbb6c848SEzequiel Garcia 			memcpy(tx16p[i],
981*fbb6c848SEzequiel Garcia 			       hantro_cnts->tx16x16_count[i],
982*fbb6c848SEzequiel Garcia 			       sizeof(hantro_cnts->tx16x16_count[0]));
983*fbb6c848SEzequiel Garcia 			tx16p[i][3] = 0;
984*fbb6c848SEzequiel Garcia 		}
985*fbb6c848SEzequiel Garcia 		counts->tx16p = &tx16p;
986*fbb6c848SEzequiel Garcia 
987*fbb6c848SEzequiel Garcia 		v4l2_vp9_adapt_coef_probs(probs, counts,
988*fbb6c848SEzequiel Garcia 					  !vp9_ctx->last.valid ||
989*fbb6c848SEzequiel Garcia 					  vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME,
990*fbb6c848SEzequiel Garcia 					  frame_is_intra);
991*fbb6c848SEzequiel Garcia 
992*fbb6c848SEzequiel Garcia 		if (!frame_is_intra) {
993*fbb6c848SEzequiel Garcia 			/* load_probs2() already done */
994*fbb6c848SEzequiel Garcia 			u32 mv_mode[7][4];
995*fbb6c848SEzequiel Garcia 
996*fbb6c848SEzequiel Garcia 			for (i = 0; i < ARRAY_SIZE(mv_mode); ++i) {
997*fbb6c848SEzequiel Garcia 				mv_mode[i][0] = hantro_cnts->inter_mode_counts[i][1][0];
998*fbb6c848SEzequiel Garcia 				mv_mode[i][1] = hantro_cnts->inter_mode_counts[i][2][0];
999*fbb6c848SEzequiel Garcia 				mv_mode[i][2] = hantro_cnts->inter_mode_counts[i][0][0];
1000*fbb6c848SEzequiel Garcia 				mv_mode[i][3] = hantro_cnts->inter_mode_counts[i][2][1];
1001*fbb6c848SEzequiel Garcia 			}
1002*fbb6c848SEzequiel Garcia 			counts->mv_mode = &mv_mode;
1003*fbb6c848SEzequiel Garcia 			v4l2_vp9_adapt_noncoef_probs(&vp9_ctx->probability_tables, counts,
1004*fbb6c848SEzequiel Garcia 						     vp9_ctx->cur.reference_mode,
1005*fbb6c848SEzequiel Garcia 						     vp9_ctx->cur.interpolation_filter,
1006*fbb6c848SEzequiel Garcia 						     vp9_ctx->cur.tx_mode, vp9_ctx->cur.flags);
1007*fbb6c848SEzequiel Garcia 		}
1008*fbb6c848SEzequiel Garcia 	}
1009*fbb6c848SEzequiel Garcia 
1010*fbb6c848SEzequiel Garcia 	vp9_ctx->frame_context[fctx_idx] = vp9_ctx->probability_tables;
1011*fbb6c848SEzequiel Garcia 
1012*fbb6c848SEzequiel Garcia out_update_last:
1013*fbb6c848SEzequiel Garcia 	vp9_ctx->last = vp9_ctx->cur;
1014*fbb6c848SEzequiel Garcia }
1015