aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/media/hantro/hantro_g2_vp9_dec.c
blob: 91c21b634fab1ec0cb11ee3401c4946211914080 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
// SPDX-License-Identifier: GPL-2.0
/*
 * Hantro VP9 codec driver
 *
 * Copyright (C) 2021 Collabora Ltd.
 */
#include "media/videobuf2-core.h"
#include "media/videobuf2-dma-contig.h"
#include "media/videobuf2-v4l2.h"
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <media/v4l2-mem2mem.h>
#include <media/v4l2-vp9.h>

#include "hantro.h"
#include "hantro_vp9.h"
#include "hantro_g2_regs.h"

#define G2_ALIGN 16

enum hantro_ref_frames {
	INTRA_FRAME = 0,
	LAST_FRAME = 1,
	GOLDEN_FRAME = 2,
	ALTREF_FRAME = 3,
	MAX_REF_FRAMES = 4
};

static int start_prepare_run(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame **dec_params)
{
	const struct v4l2_ctrl_vp9_compressed_hdr *prob_updates;
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	struct v4l2_ctrl *ctrl;
	unsigned int fctx_idx;

	/* v4l2-specific stuff */
	hantro_start_prepare_run(ctx);

	ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, V4L2_CID_STATELESS_VP9_FRAME);
	if (WARN_ON(!ctrl))
		return -EINVAL;
	*dec_params = ctrl->p_cur.p;

	ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, V4L2_CID_STATELESS_VP9_COMPRESSED_HDR);
	if (WARN_ON(!ctrl))
		return -EINVAL;
	prob_updates = ctrl->p_cur.p;
	vp9_ctx->cur.tx_mode = prob_updates->tx_mode;

	/*
	 * vp9 stuff
	 *
	 * by this point the userspace has done all parts of 6.2 uncompressed_header()
	 * except this fragment:
	 * if ( FrameIsIntra || error_resilient_mode ) {
	 *	setup_past_independence ( )
	 *	if ( frame_type == KEY_FRAME || error_resilient_mode == 1 ||
	 *	     reset_frame_context == 3 ) {
	 *		for ( i = 0; i < 4; i ++ ) {
	 *			save_probs( i )
	 *		}
	 *	} else if ( reset_frame_context == 2 ) {
	 *		save_probs( frame_context_idx )
	 *	}
	 *	frame_context_idx = 0
	 * }
	 */
	fctx_idx = v4l2_vp9_reset_frame_ctx(*dec_params, vp9_ctx->frame_context);
	vp9_ctx->cur.frame_context_idx = fctx_idx;

	/* 6.1 frame(sz): load_probs() and load_probs2() */
	vp9_ctx->probability_tables = vp9_ctx->frame_context[fctx_idx];

	/*
	 * The userspace has also performed 6.3 compressed_header(), but handling the
	 * probs in a special way. All probs which need updating, except MV-related,
	 * have been read from the bitstream and translated through inv_map_table[],
	 * but no 6.3.6 inv_recenter_nonneg(v, m) has been performed. The values passed
	 * by userspace are either translated values (there are no 0 values in
	 * inv_map_table[]), or zero to indicate no update. All MV-related probs which need
	 * updating have been read from the bitstream and (mv_prob << 1) | 1 has been
	 * performed. The values passed by userspace are either new values
	 * to replace old ones (the above mentioned shift and bitwise or never result in
	 * a zero) or zero to indicate no update.
	 * fw_update_probs() performs actual probs updates or leaves probs as-is
	 * for values for which a zero was passed from userspace.
	 */
	v4l2_vp9_fw_update_probs(&vp9_ctx->probability_tables, prob_updates, *dec_params);

	return 0;
}

static size_t chroma_offset(const struct hantro_ctx *ctx,
			    const struct v4l2_ctrl_vp9_frame *dec_params)
{
	int bytes_per_pixel = dec_params->bit_depth == 8 ? 1 : 2;

	return ctx->src_fmt.width * ctx->src_fmt.height * bytes_per_pixel;
}

static size_t mv_offset(const struct hantro_ctx *ctx,
			const struct v4l2_ctrl_vp9_frame *dec_params)
{
	size_t cr_offset = chroma_offset(ctx, dec_params);

	return ALIGN((cr_offset * 3) / 2, G2_ALIGN);
}

static struct hantro_decoded_buffer *
get_ref_buf(struct hantro_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp)
{
	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
	struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
	int buf_idx;

	/*
	 * If a ref is unused or invalid, address of current destination
	 * buffer is returned.
	 */
	buf_idx = vb2_find_timestamp(cap_q, timestamp, 0);
	if (buf_idx < 0)
		return vb2_to_hantro_decoded_buf(&dst->vb2_buf);

	return vb2_to_hantro_decoded_buf(vb2_get_buffer(cap_q, buf_idx));
}

static void update_dec_buf_info(struct hantro_decoded_buffer *buf,
				const struct v4l2_ctrl_vp9_frame *dec_params)
{
	buf->vp9.width = dec_params->frame_width_minus_1 + 1;
	buf->vp9.height = dec_params->frame_height_minus_1 + 1;
	buf->vp9.bit_depth = dec_params->bit_depth;
}

static void 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)
{
	vp9_ctx->cur.valid = true;
	vp9_ctx->cur.reference_mode = dec_params->reference_mode;
	vp9_ctx->cur.interpolation_filter = dec_params->interpolation_filter;
	vp9_ctx->cur.flags = dec_params->flags;
	vp9_ctx->cur.timestamp = buf->base.vb.vb2_buf.timestamp;
}

static void config_output(struct hantro_ctx *ctx,
			  struct hantro_decoded_buffer *dst,
			  const struct v4l2_ctrl_vp9_frame *dec_params)
{
	dma_addr_t luma_addr, chroma_addr, mv_addr;

	hantro_reg_write(ctx->dev, &g2_out_dis, 0);
	if (!ctx->dev->variant->legacy_regs)
		hantro_reg_write(ctx->dev, &g2_output_format, 0);

	luma_addr = hantro_get_dec_buf_addr(ctx, &dst->base.vb.vb2_buf);
	hantro_write_addr(ctx->dev, G2_OUT_LUMA_ADDR, luma_addr);

	chroma_addr = luma_addr + chroma_offset(ctx, dec_params);
	hantro_write_addr(ctx->dev, G2_OUT_CHROMA_ADDR, chroma_addr);

	mv_addr = luma_addr + mv_offset(ctx, dec_params);
	hantro_write_addr(ctx->dev, G2_OUT_MV_ADDR, mv_addr);
}

struct hantro_vp9_ref_reg {
	const struct hantro_reg width;
	const struct hantro_reg height;
	const struct hantro_reg hor_scale;
	const struct hantro_reg ver_scale;
	u32 y_base;
	u32 c_base;
};

static void 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)
{
	struct hantro_decoded_buffer *buf;
	dma_addr_t luma_addr, chroma_addr;
	u32 refw, refh;

	buf = get_ref_buf(ctx, &dst->base.vb, ref_ts);
	refw = buf->vp9.width;
	refh = buf->vp9.height;

	hantro_reg_write(ctx->dev, &ref_reg->width, refw);
	hantro_reg_write(ctx->dev, &ref_reg->height, refh);

	hantro_reg_write(ctx->dev, &ref_reg->hor_scale, (refw << 14) / dst->vp9.width);
	hantro_reg_write(ctx->dev, &ref_reg->ver_scale, (refh << 14) / dst->vp9.height);

	luma_addr = hantro_get_dec_buf_addr(ctx, &buf->base.vb.vb2_buf);
	hantro_write_addr(ctx->dev, ref_reg->y_base, luma_addr);

	chroma_addr = luma_addr + chroma_offset(ctx, dec_params);
	hantro_write_addr(ctx->dev, ref_reg->c_base, chroma_addr);
}

static void 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)
{
	static const struct hantro_vp9_ref_reg ref_regs[] = {
		{
			/* Last */
			.width = vp9_lref_width,
			.height = vp9_lref_height,
			.hor_scale = vp9_lref_hor_scale,
			.ver_scale = vp9_lref_ver_scale,
			.y_base = G2_REF_LUMA_ADDR(0),
			.c_base = G2_REF_CHROMA_ADDR(0),
		}, {
			/* Golden */
			.width = vp9_gref_width,
			.height = vp9_gref_height,
			.hor_scale = vp9_gref_hor_scale,
			.ver_scale = vp9_gref_ver_scale,
			.y_base = G2_REF_LUMA_ADDR(4),
			.c_base = G2_REF_CHROMA_ADDR(4),
		}, {
			/* Altref */
			.width = vp9_aref_width,
			.height = vp9_aref_height,
			.hor_scale = vp9_aref_hor_scale,
			.ver_scale = vp9_aref_ver_scale,
			.y_base = G2_REF_LUMA_ADDR(5),
			.c_base = G2_REF_CHROMA_ADDR(5),
		},
	};
	dma_addr_t mv_addr;

	config_ref(ctx, dst, &ref_regs[0], dec_params, dec_params->last_frame_ts);
	config_ref(ctx, dst, &ref_regs[1], dec_params, dec_params->golden_frame_ts);
	config_ref(ctx, dst, &ref_regs[2], dec_params, dec_params->alt_frame_ts);

	mv_addr = hantro_get_dec_buf_addr(ctx, &mv_ref->base.vb.vb2_buf) +
		  mv_offset(ctx, dec_params);
	hantro_write_addr(ctx->dev, G2_REF_MV_ADDR(0), mv_addr);

	hantro_reg_write(ctx->dev, &vp9_last_sign_bias,
			 dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_LAST ? 1 : 0);

	hantro_reg_write(ctx->dev, &vp9_gref_sign_bias,
			 dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_GOLDEN ? 1 : 0);

	hantro_reg_write(ctx->dev, &vp9_aref_sign_bias,
			 dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_ALT ? 1 : 0);
}

static void recompute_tile_info(unsigned short *tile_info, unsigned int tiles, unsigned int sbs)
{
	int i;
	unsigned int accumulated = 0;
	unsigned int next_accumulated;

	for (i = 1; i <= tiles; ++i) {
		next_accumulated = i * sbs / tiles;
		*tile_info++ = next_accumulated - accumulated;
		accumulated = next_accumulated;
	}
}

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)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;

	recompute_tile_info(vp9_ctx->tile_r_info, tile_r, sbs_r);
	recompute_tile_info(vp9_ctx->tile_c_info, tile_c, sbs_c);

	vp9_ctx->last_tile_r = tile_r;
	vp9_ctx->last_tile_c = tile_c;
	vp9_ctx->last_sbs_r = sbs_r;
	vp9_ctx->last_sbs_c = sbs_c;
}

static inline unsigned int first_tile_row(unsigned int tile_r, unsigned int sbs_r)
{
	if (tile_r == sbs_r + 1)
		return 1;

	if (tile_r == sbs_r + 2)
		return 2;

	return 0;
}

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)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	unsigned int i, j;
	bool first = true;

	for (i = first_tile_row(tile_r, sbs_r); i < tile_r; ++i) {
		unsigned short r_info = vp9_ctx->tile_r_info[i];

		if (first) {
			if (i > 0)
				r_info += vp9_ctx->tile_r_info[0];
			if (i == 2)
				r_info += vp9_ctx->tile_r_info[1];
			first = false;
		}
		for (j = 0; j < tile_c; ++j) {
			*tile_mem++ = vp9_ctx->tile_c_info[j];
			*tile_mem++ = r_info;
		}
	}
}

static void
config_tiles(struct hantro_ctx *ctx,
	     const struct v4l2_ctrl_vp9_frame *dec_params,
	     struct hantro_decoded_buffer *dst)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	struct hantro_aux_buf *misc = &vp9_ctx->misc;
	struct hantro_aux_buf *tile_edge = &vp9_ctx->tile_edge;
	dma_addr_t addr;
	unsigned short *tile_mem;
	unsigned int rows, cols;

	addr = misc->dma + vp9_ctx->tile_info_offset;
	hantro_write_addr(ctx->dev, G2_TILE_SIZES_ADDR, addr);

	tile_mem = misc->cpu + vp9_ctx->tile_info_offset;
	if (dec_params->tile_cols_log2 || dec_params->tile_rows_log2) {
		unsigned int tile_r = (1 << dec_params->tile_rows_log2);
		unsigned int tile_c = (1 << dec_params->tile_cols_log2);
		unsigned int sbs_r = hantro_vp9_num_sbs(dst->vp9.height);
		unsigned int sbs_c = hantro_vp9_num_sbs(dst->vp9.width);

		if (tile_r != vp9_ctx->last_tile_r || tile_c != vp9_ctx->last_tile_c ||
		    sbs_r != vp9_ctx->last_sbs_r || sbs_c != vp9_ctx->last_sbs_c)
			recompute_tile_rc_info(ctx, tile_r, tile_c, sbs_r, sbs_c);

		fill_tile_info(ctx, tile_r, tile_c, sbs_r, sbs_c, tile_mem);

		cols = tile_c;
		rows = tile_r;
		hantro_reg_write(ctx->dev, &g2_tile_e, 1);
	} else {
		tile_mem[0] = hantro_vp9_num_sbs(dst->vp9.width);
		tile_mem[1] = hantro_vp9_num_sbs(dst->vp9.height);

		cols = 1;
		rows = 1;
		hantro_reg_write(ctx->dev, &g2_tile_e, 0);
	}

	if (ctx->dev->variant->legacy_regs) {
		hantro_reg_write(ctx->dev, &g2_num_tile_cols_old, cols);
		hantro_reg_write(ctx->dev, &g2_num_tile_rows_old, rows);
	} else {
		hantro_reg_write(ctx->dev, &g2_num_tile_cols, cols);
		hantro_reg_write(ctx->dev, &g2_num_tile_rows, rows);
	}

	/* provide aux buffers even if no tiles are used */
	addr = tile_edge->dma;
	hantro_write_addr(ctx->dev, G2_TILE_FILTER_ADDR, addr);

	addr = tile_edge->dma + vp9_ctx->bsd_ctrl_offset;
	hantro_write_addr(ctx->dev, G2_TILE_BSD_ADDR, addr);
}

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)
{
	u8 mask = V4L2_VP9_SEGMENT_FEATURE_ENABLED(feature);

	vp9_ctx->feature_data[segid][feature] = seg->feature_data[segid][feature];
	vp9_ctx->feature_enabled[segid] &= ~mask;
	vp9_ctx->feature_enabled[segid] |= (seg->feature_enabled[segid] & mask);
}

static inline s16 clip3(s16 x, s16 y, s16 z)
{
	return (z < x) ? x : (z > y) ? y : z;
}

static s16 feat_val_clip3(s16 feat_val, s16 feature_data, bool absolute, u8 clip)
{
	if (absolute)
		return feature_data;

	return clip3(0, 255, feat_val + feature_data);
}

static void config_segment(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	const struct v4l2_vp9_segmentation *seg;
	s16 feat_val;
	unsigned char feat_id;
	unsigned int segid;
	bool segment_enabled, absolute, update_data;

	static const struct hantro_reg seg_regs[8][V4L2_VP9_SEG_LVL_MAX] = {
		{ vp9_quant_seg0, vp9_filt_level_seg0, vp9_refpic_seg0, vp9_skip_seg0 },
		{ vp9_quant_seg1, vp9_filt_level_seg1, vp9_refpic_seg1, vp9_skip_seg1 },
		{ vp9_quant_seg2, vp9_filt_level_seg2, vp9_refpic_seg2, vp9_skip_seg2 },
		{ vp9_quant_seg3, vp9_filt_level_seg3, vp9_refpic_seg3, vp9_skip_seg3 },
		{ vp9_quant_seg4, vp9_filt_level_seg4, vp9_refpic_seg4, vp9_skip_seg4 },
		{ vp9_quant_seg5, vp9_filt_level_seg5, vp9_refpic_seg5, vp9_skip_seg5 },
		{ vp9_quant_seg6, vp9_filt_level_seg6, vp9_refpic_seg6, vp9_skip_seg6 },
		{ vp9_quant_seg7, vp9_filt_level_seg7, vp9_refpic_seg7, vp9_skip_seg7 },
	};

	segment_enabled = !!(dec_params->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED);
	hantro_reg_write(ctx->dev, &vp9_segment_e, segment_enabled);
	hantro_reg_write(ctx->dev, &vp9_segment_upd_e,
			 !!(dec_params->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP));
	hantro_reg_write(ctx->dev, &vp9_segment_temp_upd_e,
			 !!(dec_params->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE));

	seg = &dec_params->seg;
	absolute = !!(seg->flags & V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE);
	update_data = !!(seg->flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA);

	for (segid = 0; segid < 8; ++segid) {
		/* Quantizer segment feature */
		feat_id = V4L2_VP9_SEG_LVL_ALT_Q;
		feat_val = dec_params->quant.base_q_idx;
		if (segment_enabled) {
			if (update_data)
				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
			if (v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled, feat_id, segid))
				feat_val = feat_val_clip3(feat_val,
							  vp9_ctx->feature_data[segid][feat_id],
							  absolute, 255);
		}
		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);

		/* Loop filter segment feature */
		feat_id = V4L2_VP9_SEG_LVL_ALT_L;
		feat_val = dec_params->lf.level;
		if (segment_enabled) {
			if (update_data)
				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
			if (v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled, feat_id, segid))
				feat_val = feat_val_clip3(feat_val,
							  vp9_ctx->feature_data[segid][feat_id],
							  absolute, 63);
		}
		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);

		/* Reference frame segment feature */
		feat_id = V4L2_VP9_SEG_LVL_REF_FRAME;
		feat_val = 0;
		if (segment_enabled) {
			if (update_data)
				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
			if (!(dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) &&
			    v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled, feat_id, segid))
				feat_val = vp9_ctx->feature_data[segid][feat_id] + 1;
		}
		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);

		/* Skip segment feature */
		feat_id = V4L2_VP9_SEG_LVL_SKIP;
		feat_val = 0;
		if (segment_enabled) {
			if (update_data)
				update_feat_and_flag(vp9_ctx, seg, feat_id, segid);
			feat_val = v4l2_vp9_seg_feat_enabled(vp9_ctx->feature_enabled,
							     feat_id, segid) ? 1 : 0;
		}
		hantro_reg_write(ctx->dev, &seg_regs[segid][feat_id], feat_val);
	}
}

static void config_loop_filter(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
{
	bool d = dec_params->lf.flags & V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED;

	hantro_reg_write(ctx->dev, &vp9_filt_level, dec_params->lf.level);
	hantro_reg_write(ctx->dev, &g2_out_filtering_dis, dec_params->lf.level == 0);
	hantro_reg_write(ctx->dev, &vp9_filt_sharpness, dec_params->lf.sharpness);

	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_0, d ? dec_params->lf.ref_deltas[0] : 0);
	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_1, d ? dec_params->lf.ref_deltas[1] : 0);
	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_2, d ? dec_params->lf.ref_deltas[2] : 0);
	hantro_reg_write(ctx->dev, &vp9_filt_ref_adj_3, d ? dec_params->lf.ref_deltas[3] : 0);
	hantro_reg_write(ctx->dev, &vp9_filt_mb_adj_0, d ? dec_params->lf.mode_deltas[0] : 0);
	hantro_reg_write(ctx->dev, &vp9_filt_mb_adj_1, d ? dec_params->lf.mode_deltas[1] : 0);
}

static void config_picture_dimensions(struct hantro_ctx *ctx, struct hantro_decoded_buffer *dst)
{
	u32 pic_w_4x4, pic_h_4x4;

	hantro_reg_write(ctx->dev, &g2_pic_width_in_cbs, (dst->vp9.width + 7) / 8);
	hantro_reg_write(ctx->dev, &g2_pic_height_in_cbs, (dst->vp9.height + 7) / 8);
	pic_w_4x4 = roundup(dst->vp9.width, 8) >> 2;
	pic_h_4x4 = roundup(dst->vp9.height, 8) >> 2;
	hantro_reg_write(ctx->dev, &g2_pic_width_4x4, pic_w_4x4);
	hantro_reg_write(ctx->dev, &g2_pic_height_4x4, pic_h_4x4);
}

static void
config_bit_depth(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
{
	if (ctx->dev->variant->legacy_regs) {
		u8 pp_shift = 0;

		hantro_reg_write(ctx->dev, &g2_bit_depth_y, dec_params->bit_depth);
		hantro_reg_write(ctx->dev, &g2_bit_depth_c, dec_params->bit_depth);
		hantro_reg_write(ctx->dev, &g2_rs_out_bit_depth, dec_params->bit_depth);

		if (dec_params->bit_depth > 8)
			pp_shift = 16 - dec_params->bit_depth;

		hantro_reg_write(ctx->dev, &g2_pp_pix_shift, pp_shift);
		hantro_reg_write(ctx->dev, &g2_pix_shift, 0);
	} else {
		hantro_reg_write(ctx->dev, &g2_bit_depth_y_minus8, dec_params->bit_depth - 8);
		hantro_reg_write(ctx->dev, &g2_bit_depth_c_minus8, dec_params->bit_depth - 8);
	}
}

static inline bool is_lossless(const struct v4l2_vp9_quantization *quant)
{
	return quant->base_q_idx == 0 && quant->delta_q_uv_ac == 0 &&
	       quant->delta_q_uv_dc == 0 && quant->delta_q_y_dc == 0;
}

static void
config_quant(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
{
	hantro_reg_write(ctx->dev, &vp9_qp_delta_y_dc, dec_params->quant.delta_q_y_dc);
	hantro_reg_write(ctx->dev, &vp9_qp_delta_ch_dc, dec_params->quant.delta_q_uv_dc);
	hantro_reg_write(ctx->dev, &vp9_qp_delta_ch_ac, dec_params->quant.delta_q_uv_ac);
	hantro_reg_write(ctx->dev, &vp9_lossless_e, is_lossless(&dec_params->quant));
}

static u32
hantro_interp_filter_from_v4l2(unsigned int interpolation_filter)
{
	switch (interpolation_filter) {
	case V4L2_VP9_INTERP_FILTER_EIGHTTAP:
		return 0x1;
	case V4L2_VP9_INTERP_FILTER_EIGHTTAP_SMOOTH:
		return 0;
	case V4L2_VP9_INTERP_FILTER_EIGHTTAP_SHARP:
		return 0x2;
	case V4L2_VP9_INTERP_FILTER_BILINEAR:
		return 0x3;
	case V4L2_VP9_INTERP_FILTER_SWITCHABLE:
		return 0x4;
	}

	return 0;
}

static void
config_others(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params,
	      bool intra_only, bool resolution_change)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;

	hantro_reg_write(ctx->dev, &g2_idr_pic_e, intra_only);

	hantro_reg_write(ctx->dev, &vp9_transform_mode, vp9_ctx->cur.tx_mode);

	hantro_reg_write(ctx->dev, &vp9_mcomp_filt_type, intra_only ?
		0 : hantro_interp_filter_from_v4l2(dec_params->interpolation_filter));

	hantro_reg_write(ctx->dev, &vp9_high_prec_mv_e,
			 !!(dec_params->flags & V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV));

	hantro_reg_write(ctx->dev, &vp9_comp_pred_mode, dec_params->reference_mode);

	hantro_reg_write(ctx->dev, &g2_tempor_mvp_e,
			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) &&
			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) &&
			 !(vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) &&
			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_INTRA_ONLY) &&
			 !resolution_change &&
			 vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_SHOW_FRAME
	);

	hantro_reg_write(ctx->dev, &g2_write_mvs_e,
			 !(dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME));
}

static void
config_compound_reference(struct hantro_ctx *ctx,
			  const struct v4l2_ctrl_vp9_frame *dec_params)
{
	u32 comp_fixed_ref, comp_var_ref[2];
	bool last_ref_frame_sign_bias;
	bool golden_ref_frame_sign_bias;
	bool alt_ref_frame_sign_bias;
	bool comp_ref_allowed = 0;

	comp_fixed_ref = 0;
	comp_var_ref[0] = 0;
	comp_var_ref[1] = 0;

	last_ref_frame_sign_bias = dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_LAST;
	golden_ref_frame_sign_bias = dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_GOLDEN;
	alt_ref_frame_sign_bias = dec_params->ref_frame_sign_bias & V4L2_VP9_SIGN_BIAS_ALT;

	/* 6.3.12 Frame reference mode syntax */
	comp_ref_allowed |= golden_ref_frame_sign_bias != last_ref_frame_sign_bias;
	comp_ref_allowed |= alt_ref_frame_sign_bias != last_ref_frame_sign_bias;

	if (comp_ref_allowed) {
		if (last_ref_frame_sign_bias ==
		    golden_ref_frame_sign_bias) {
			comp_fixed_ref = ALTREF_FRAME;
			comp_var_ref[0] = LAST_FRAME;
			comp_var_ref[1] = GOLDEN_FRAME;
		} else if (last_ref_frame_sign_bias ==
			   alt_ref_frame_sign_bias) {
			comp_fixed_ref = GOLDEN_FRAME;
			comp_var_ref[0] = LAST_FRAME;
			comp_var_ref[1] = ALTREF_FRAME;
		} else {
			comp_fixed_ref = LAST_FRAME;
			comp_var_ref[0] = GOLDEN_FRAME;
			comp_var_ref[1] = ALTREF_FRAME;
		}
	}

	hantro_reg_write(ctx->dev, &vp9_comp_pred_fixed_ref, comp_fixed_ref);
	hantro_reg_write(ctx->dev, &vp9_comp_pred_var_ref0, comp_var_ref[0]);
	hantro_reg_write(ctx->dev, &vp9_comp_pred_var_ref1, comp_var_ref[1]);
}

#define INNER_LOOP \
do {									\
	for (m = 0; m < ARRAY_SIZE(adaptive->coef[0][0][0][0]); ++m) {	\
		memcpy(adaptive->coef[i][j][k][l][m],			\
		       probs->coef[i][j][k][l][m],			\
		       sizeof(probs->coef[i][j][k][l][m]));		\
									\
		adaptive->coef[i][j][k][l][m][3] = 0;			\
	}								\
} while (0)

static void config_probs(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	struct hantro_aux_buf *misc = &vp9_ctx->misc;
	struct hantro_g2_all_probs *all_probs = misc->cpu;
	struct hantro_g2_probs *adaptive;
	struct hantro_g2_mv_probs *mv;
	const struct v4l2_vp9_segmentation *seg = &dec_params->seg;
	const struct v4l2_vp9_frame_context *probs = &vp9_ctx->probability_tables;
	int i, j, k, l, m;

	for (i = 0; i < ARRAY_SIZE(all_probs->kf_y_mode_prob); ++i)
		for (j = 0; j < ARRAY_SIZE(all_probs->kf_y_mode_prob[0]); ++j) {
			memcpy(all_probs->kf_y_mode_prob[i][j],
			       v4l2_vp9_kf_y_mode_prob[i][j],
			       ARRAY_SIZE(all_probs->kf_y_mode_prob[i][j]));

			all_probs->kf_y_mode_prob_tail[i][j][0] =
				v4l2_vp9_kf_y_mode_prob[i][j][8];
		}

	memcpy(all_probs->mb_segment_tree_probs, seg->tree_probs,
	       sizeof(all_probs->mb_segment_tree_probs));

	memcpy(all_probs->segment_pred_probs, seg->pred_probs,
	       sizeof(all_probs->segment_pred_probs));

	for (i = 0; i < ARRAY_SIZE(all_probs->kf_uv_mode_prob); ++i) {
		memcpy(all_probs->kf_uv_mode_prob[i], v4l2_vp9_kf_uv_mode_prob[i],
		       ARRAY_SIZE(all_probs->kf_uv_mode_prob[i]));

		all_probs->kf_uv_mode_prob_tail[i][0] = v4l2_vp9_kf_uv_mode_prob[i][8];
	}

	adaptive = &all_probs->probs;

	for (i = 0; i < ARRAY_SIZE(adaptive->inter_mode); ++i) {
		memcpy(adaptive->inter_mode[i], probs->inter_mode[i],
		       ARRAY_SIZE(probs->inter_mode[i]));

		adaptive->inter_mode[i][3] = 0;
	}

	memcpy(adaptive->is_inter, probs->is_inter, sizeof(adaptive->is_inter));

	for (i = 0; i < ARRAY_SIZE(adaptive->uv_mode); ++i) {
		memcpy(adaptive->uv_mode[i], probs->uv_mode[i],
		       sizeof(adaptive->uv_mode[i]));
		adaptive->uv_mode_tail[i][0] = probs->uv_mode[i][8];
	}

	memcpy(adaptive->tx8, probs->tx8, sizeof(adaptive->tx8));
	memcpy(adaptive->tx16, probs->tx16, sizeof(adaptive->tx16));
	memcpy(adaptive->tx32, probs->tx32, sizeof(adaptive->tx32));

	for (i = 0; i < ARRAY_SIZE(adaptive->y_mode); ++i) {
		memcpy(adaptive->y_mode[i], probs->y_mode[i],
		       ARRAY_SIZE(adaptive->y_mode[i]));

		adaptive->y_mode_tail[i][0] = probs->y_mode[i][8];
	}

	for (i = 0; i < ARRAY_SIZE(adaptive->partition[0]); ++i) {
		memcpy(adaptive->partition[0][i], v4l2_vp9_kf_partition_probs[i],
		       sizeof(v4l2_vp9_kf_partition_probs[i]));

		adaptive->partition[0][i][3] = 0;
	}

	for (i = 0; i < ARRAY_SIZE(adaptive->partition[1]); ++i) {
		memcpy(adaptive->partition[1][i], probs->partition[i],
		       sizeof(probs->partition[i]));

		adaptive->partition[1][i][3] = 0;
	}

	memcpy(adaptive->interp_filter, probs->interp_filter,
	       sizeof(adaptive->interp_filter));

	memcpy(adaptive->comp_mode, probs->comp_mode, sizeof(adaptive->comp_mode));

	memcpy(adaptive->skip, probs->skip, sizeof(adaptive->skip));

	mv = &adaptive->mv;

	memcpy(mv->joint, probs->mv.joint, sizeof(mv->joint));
	memcpy(mv->sign, probs->mv.sign, sizeof(mv->sign));
	memcpy(mv->class0_bit, probs->mv.class0_bit, sizeof(mv->class0_bit));
	memcpy(mv->fr, probs->mv.fr, sizeof(mv->fr));
	memcpy(mv->class0_hp, probs->mv.class0_hp, sizeof(mv->class0_hp));
	memcpy(mv->hp, probs->mv.hp, sizeof(mv->hp));
	memcpy(mv->classes, probs->mv.classes, sizeof(mv->classes));
	memcpy(mv->class0_fr, probs->mv.class0_fr, sizeof(mv->class0_fr));
	memcpy(mv->bits, probs->mv.bits, sizeof(mv->bits));

	memcpy(adaptive->single_ref, probs->single_ref, sizeof(adaptive->single_ref));

	memcpy(adaptive->comp_ref, probs->comp_ref, sizeof(adaptive->comp_ref));

	for (i = 0; i < ARRAY_SIZE(adaptive->coef); ++i)
		for (j = 0; j < ARRAY_SIZE(adaptive->coef[0]); ++j)
			for (k = 0; k < ARRAY_SIZE(adaptive->coef[0][0]); ++k)
				for (l = 0; l < ARRAY_SIZE(adaptive->coef[0][0][0]); ++l)
					INNER_LOOP;

	hantro_write_addr(ctx->dev, G2_VP9_PROBS_ADDR, misc->dma);
}

static void config_counts(struct hantro_ctx *ctx)
{
	struct hantro_vp9_dec_hw_ctx *vp9_dec = &ctx->vp9_dec;
	struct hantro_aux_buf *misc = &vp9_dec->misc;
	dma_addr_t addr = misc->dma + vp9_dec->ctx_counters_offset;

	hantro_write_addr(ctx->dev, G2_VP9_CTX_COUNT_ADDR, addr);
}

static void config_seg_map(struct hantro_ctx *ctx,
			   const struct v4l2_ctrl_vp9_frame *dec_params,
			   bool intra_only, bool update_map)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	struct hantro_aux_buf *segment_map = &vp9_ctx->segment_map;
	dma_addr_t addr;

	if (intra_only ||
	    (dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT)) {
		memset(segment_map->cpu, 0, segment_map->size);
		memset(vp9_ctx->feature_data, 0, sizeof(vp9_ctx->feature_data));
		memset(vp9_ctx->feature_enabled, 0, sizeof(vp9_ctx->feature_enabled));
	}

	addr = segment_map->dma + vp9_ctx->active_segment * vp9_ctx->segment_map_size;
	hantro_write_addr(ctx->dev, G2_VP9_SEGMENT_READ_ADDR, addr);

	addr = segment_map->dma + (1 - vp9_ctx->active_segment) * vp9_ctx->segment_map_size;
	hantro_write_addr(ctx->dev, G2_VP9_SEGMENT_WRITE_ADDR, addr);

	if (update_map)
		vp9_ctx->active_segment = 1 - vp9_ctx->active_segment;
}

static void
config_source(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params,
	      struct vb2_v4l2_buffer *vb2_src)
{
	dma_addr_t stream_base, tmp_addr;
	unsigned int headres_size;
	u32 src_len, start_bit, src_buf_len;

	headres_size = dec_params->uncompressed_header_size
		     + dec_params->compressed_header_size;

	stream_base = vb2_dma_contig_plane_dma_addr(&vb2_src->vb2_buf, 0);

	tmp_addr = stream_base + headres_size;
	if (ctx->dev->variant->legacy_regs)
		hantro_write_addr(ctx->dev, G2_STREAM_ADDR, (tmp_addr & ~0xf));
	else
		hantro_write_addr(ctx->dev, G2_STREAM_ADDR, stream_base);

	start_bit = (tmp_addr & 0xf) * 8;
	hantro_reg_write(ctx->dev, &g2_start_bit, start_bit);

	src_len = vb2_get_plane_payload(&vb2_src->vb2_buf, 0);
	src_len += start_bit / 8 - headres_size;
	hantro_reg_write(ctx->dev, &g2_stream_len, src_len);

	if (!ctx->dev->variant->legacy_regs) {
		tmp_addr &= ~0xf;
		hantro_reg_write(ctx->dev, &g2_strm_start_offset, tmp_addr - stream_base);
		src_buf_len = vb2_plane_size(&vb2_src->vb2_buf, 0);
		hantro_reg_write(ctx->dev, &g2_strm_buffer_len, src_buf_len);
	}
}

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)
{
	struct hantro_decoded_buffer *dst, *last, *mv_ref;
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	const struct v4l2_vp9_segmentation *seg;
	bool intra_only, resolution_change;

	/* vp9 stuff */
	dst = vb2_to_hantro_decoded_buf(&vb2_dst->vb2_buf);

	if (vp9_ctx->last.valid)
		last = get_ref_buf(ctx, &dst->base.vb, vp9_ctx->last.timestamp);
	else
		last = dst;

	update_dec_buf_info(dst, dec_params);
	update_ctx_cur_info(vp9_ctx, dst, dec_params);
	seg = &dec_params->seg;

	intra_only = !!(dec_params->flags &
			(V4L2_VP9_FRAME_FLAG_KEY_FRAME |
			V4L2_VP9_FRAME_FLAG_INTRA_ONLY));

	if (!intra_only &&
	    !(dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) &&
	    vp9_ctx->last.valid)
		mv_ref = last;
	else
		mv_ref = dst;

	resolution_change = dst->vp9.width != last->vp9.width ||
			    dst->vp9.height != last->vp9.height;

	/* configure basic registers */
	hantro_reg_write(ctx->dev, &g2_mode, VP9_DEC_MODE);
	if (!ctx->dev->variant->legacy_regs) {
		hantro_reg_write(ctx->dev, &g2_strm_swap, 0xf);
		hantro_reg_write(ctx->dev, &g2_dirmv_swap, 0xf);
		hantro_reg_write(ctx->dev, &g2_compress_swap, 0xf);
		hantro_reg_write(ctx->dev, &g2_ref_compress_bypass, 1);
	} else {
		hantro_reg_write(ctx->dev, &g2_strm_swap_old, 0x1f);
		hantro_reg_write(ctx->dev, &g2_pic_swap, 0x10);
		hantro_reg_write(ctx->dev, &g2_dirmv_swap_old, 0x10);
		hantro_reg_write(ctx->dev, &g2_tab0_swap_old, 0x10);
		hantro_reg_write(ctx->dev, &g2_tab1_swap_old, 0x10);
		hantro_reg_write(ctx->dev, &g2_tab2_swap_old, 0x10);
		hantro_reg_write(ctx->dev, &g2_tab3_swap_old, 0x10);
		hantro_reg_write(ctx->dev, &g2_rscan_swap, 0x10);
	}
	hantro_reg_write(ctx->dev, &g2_buswidth, BUS_WIDTH_128);
	hantro_reg_write(ctx->dev, &g2_max_burst, 16);
	hantro_reg_write(ctx->dev, &g2_apf_threshold, 8);
	hantro_reg_write(ctx->dev, &g2_clk_gate_e, 1);
	hantro_reg_write(ctx->dev, &g2_max_cb_size, 6);
	hantro_reg_write(ctx->dev, &g2_min_cb_size, 3);
	if (ctx->dev->variant->double_buffer)
		hantro_reg_write(ctx->dev, &g2_double_buffer_e, 1);

	config_output(ctx, dst, dec_params);

	if (!intra_only)
		config_ref_registers(ctx, dec_params, dst, mv_ref);

	config_tiles(ctx, dec_params, dst);
	config_segment(ctx, dec_params);
	config_loop_filter(ctx, dec_params);
	config_picture_dimensions(ctx, dst);
	config_bit_depth(ctx, dec_params);
	config_quant(ctx, dec_params);
	config_others(ctx, dec_params, intra_only, resolution_change);
	config_compound_reference(ctx, dec_params);
	config_probs(ctx, dec_params);
	config_counts(ctx);
	config_seg_map(ctx, dec_params, intra_only,
		       seg->flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP);
	config_source(ctx, dec_params, vb2_src);
}

int hantro_g2_vp9_dec_run(struct hantro_ctx *ctx)
{
	const struct v4l2_ctrl_vp9_frame *decode_params;
	struct vb2_v4l2_buffer *src;
	struct vb2_v4l2_buffer *dst;
	int ret;

	hantro_g2_check_idle(ctx->dev);

	ret = start_prepare_run(ctx, &decode_params);
	if (ret) {
		hantro_end_prepare_run(ctx);
		return ret;
	}

	src = hantro_get_src_buf(ctx);
	dst = hantro_get_dst_buf(ctx);

	config_registers(ctx, decode_params, src, dst);

	hantro_end_prepare_run(ctx);

	vdpu_write(ctx->dev, G2_REG_INTERRUPT_DEC_E, G2_REG_INTERRUPT);

	return 0;
}

#define copy_tx_and_skip(p1, p2)				\
do {								\
	memcpy((p1)->tx8, (p2)->tx8, sizeof((p1)->tx8));	\
	memcpy((p1)->tx16, (p2)->tx16, sizeof((p1)->tx16));	\
	memcpy((p1)->tx32, (p2)->tx32, sizeof((p1)->tx32));	\
	memcpy((p1)->skip, (p2)->skip, sizeof((p1)->skip));	\
} while (0)

void hantro_g2_vp9_dec_done(struct hantro_ctx *ctx)
{
	struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec;
	unsigned int fctx_idx;

	if (!(vp9_ctx->cur.flags & V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX))
		goto out_update_last;

	fctx_idx = vp9_ctx->cur.frame_context_idx;

	if (!(vp9_ctx->cur.flags & V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE)) {
		/* error_resilient_mode == 0 && frame_parallel_decoding_mode == 0 */
		struct v4l2_vp9_frame_context *probs = &vp9_ctx->probability_tables;
		bool frame_is_intra = vp9_ctx->cur.flags &
		    (V4L2_VP9_FRAME_FLAG_KEY_FRAME | V4L2_VP9_FRAME_FLAG_INTRA_ONLY);
		struct tx_and_skip {
			u8 tx8[2][1];
			u8 tx16[2][2];
			u8 tx32[2][3];
			u8 skip[3];
		} _tx_skip, *tx_skip = &_tx_skip;
		struct v4l2_vp9_frame_symbol_counts *counts;
		struct symbol_counts *hantro_cnts;
		u32 tx16p[2][4];
		int i;

		/* buffer the forward-updated TX and skip probs */
		if (frame_is_intra)
			copy_tx_and_skip(tx_skip, probs);

		/* 6.1.2 refresh_probs(): load_probs() and load_probs2() */
		*probs = vp9_ctx->frame_context[fctx_idx];

		/* if FrameIsIntra then undo the effect of load_probs2() */
		if (frame_is_intra)
			copy_tx_and_skip(probs, tx_skip);

		counts = &vp9_ctx->cnts;
		hantro_cnts = vp9_ctx->misc.cpu + vp9_ctx->ctx_counters_offset;
		for (i = 0; i < ARRAY_SIZE(tx16p); ++i) {
			memcpy(tx16p[i],
			       hantro_cnts->tx16x16_count[i],
			       sizeof(hantro_cnts->tx16x16_count[0]));
			tx16p[i][3] = 0;
		}
		counts->tx16p = &tx16p;

		v4l2_vp9_adapt_coef_probs(probs, counts,
					  !vp9_ctx->last.valid ||
					  vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME,
					  frame_is_intra);

		if (!frame_is_intra) {
			/* load_probs2() already done */
			u32 mv_mode[7][4];

			for (i = 0; i < ARRAY_SIZE(mv_mode); ++i) {
				mv_mode[i][0] = hantro_cnts->inter_mode_counts[i][1][0];
				mv_mode[i][1] = hantro_cnts->inter_mode_counts[i][2][0];
				mv_mode[i][2] = hantro_cnts->inter_mode_counts[i][0][0];
				mv_mode[i][3] = hantro_cnts->inter_mode_counts[i][2][1];
			}
			counts->mv_mode = &mv_mode;
			v4l2_vp9_adapt_noncoef_probs(&vp9_ctx->probability_tables, counts,
						     vp9_ctx->cur.reference_mode,
						     vp9_ctx->cur.interpolation_filter,
						     vp9_ctx->cur.tx_mode, vp9_ctx->cur.flags);
		}
	}

	vp9_ctx->frame_context[fctx_idx] = vp9_ctx->probability_tables;

out_update_last:
	vp9_ctx->last = vp9_ctx->cur;
}