162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hantro VPU codec driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2018 Google LLC.
662306a36Sopenharmony_ci *	Tomasz Figa <tfiga@chromium.org>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef HANTRO_HW_H_
1062306a36Sopenharmony_ci#define HANTRO_HW_H_
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/interrupt.h>
1362306a36Sopenharmony_ci#include <linux/v4l2-controls.h>
1462306a36Sopenharmony_ci#include <media/v4l2-ctrls.h>
1562306a36Sopenharmony_ci#include <media/v4l2-vp9.h>
1662306a36Sopenharmony_ci#include <media/videobuf2-core.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include "rockchip_av1_entropymode.h"
1962306a36Sopenharmony_ci#include "rockchip_av1_filmgrain.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define DEC_8190_ALIGN_MASK	0x07U
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define MB_DIM			16
2462306a36Sopenharmony_ci#define TILE_MB_DIM		4
2562306a36Sopenharmony_ci#define MB_WIDTH(w)		DIV_ROUND_UP(w, MB_DIM)
2662306a36Sopenharmony_ci#define MB_HEIGHT(h)		DIV_ROUND_UP(h, MB_DIM)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define FMT_MIN_WIDTH		48
2962306a36Sopenharmony_ci#define FMT_MIN_HEIGHT		48
3062306a36Sopenharmony_ci#define FMT_HD_WIDTH		1280
3162306a36Sopenharmony_ci#define FMT_HD_HEIGHT		720
3262306a36Sopenharmony_ci#define FMT_FHD_WIDTH		1920
3362306a36Sopenharmony_ci#define FMT_FHD_HEIGHT		1088
3462306a36Sopenharmony_ci#define FMT_UHD_WIDTH		3840
3562306a36Sopenharmony_ci#define FMT_UHD_HEIGHT		2160
3662306a36Sopenharmony_ci#define FMT_4K_WIDTH		4096
3762306a36Sopenharmony_ci#define FMT_4K_HEIGHT		2304
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#define NUM_REF_PICTURES	(V4L2_HEVC_DPB_ENTRIES_NUM_MAX + 1)
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define AV1_MAX_FRAME_BUF_COUNT	(V4L2_AV1_TOTAL_REFS_PER_FRAME + 1)
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct hantro_dev;
4462306a36Sopenharmony_cistruct hantro_ctx;
4562306a36Sopenharmony_cistruct hantro_buf;
4662306a36Sopenharmony_cistruct hantro_variant;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/**
4962306a36Sopenharmony_ci * struct hantro_aux_buf - auxiliary DMA buffer for hardware data
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * @cpu:	CPU pointer to the buffer.
5262306a36Sopenharmony_ci * @dma:	DMA address of the buffer.
5362306a36Sopenharmony_ci * @size:	Size of the buffer.
5462306a36Sopenharmony_ci * @attrs:	Attributes of the DMA mapping.
5562306a36Sopenharmony_ci */
5662306a36Sopenharmony_cistruct hantro_aux_buf {
5762306a36Sopenharmony_ci	void *cpu;
5862306a36Sopenharmony_ci	dma_addr_t dma;
5962306a36Sopenharmony_ci	size_t size;
6062306a36Sopenharmony_ci	unsigned long attrs;
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* Max. number of entries in the DPB (HW limitation). */
6462306a36Sopenharmony_ci#define HANTRO_H264_DPB_SIZE		16
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * struct hantro_h264_dec_ctrls
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * @decode:	Decode params
7062306a36Sopenharmony_ci * @scaling:	Scaling info
7162306a36Sopenharmony_ci * @sps:	SPS info
7262306a36Sopenharmony_ci * @pps:	PPS info
7362306a36Sopenharmony_ci */
7462306a36Sopenharmony_cistruct hantro_h264_dec_ctrls {
7562306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_decode_params *decode;
7662306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_scaling_matrix *scaling;
7762306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_sps *sps;
7862306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_pps *pps;
7962306a36Sopenharmony_ci};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/**
8262306a36Sopenharmony_ci * struct hantro_h264_dec_reflists
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * @p:		P reflist
8562306a36Sopenharmony_ci * @b0:		B0 reflist
8662306a36Sopenharmony_ci * @b1:		B1 reflist
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_cistruct hantro_h264_dec_reflists {
8962306a36Sopenharmony_ci	struct v4l2_h264_reference p[V4L2_H264_REF_LIST_LEN];
9062306a36Sopenharmony_ci	struct v4l2_h264_reference b0[V4L2_H264_REF_LIST_LEN];
9162306a36Sopenharmony_ci	struct v4l2_h264_reference b1[V4L2_H264_REF_LIST_LEN];
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/**
9562306a36Sopenharmony_ci * struct hantro_h264_dec_hw_ctx
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * @priv:	Private auxiliary buffer for hardware.
9862306a36Sopenharmony_ci * @dpb:	DPB
9962306a36Sopenharmony_ci * @reflists:	P/B0/B1 reflists
10062306a36Sopenharmony_ci * @ctrls:	V4L2 controls attached to a run
10162306a36Sopenharmony_ci * @dpb_longterm: DPB long-term
10262306a36Sopenharmony_ci * @dpb_valid:	  DPB valid
10362306a36Sopenharmony_ci * @cur_poc:	Current picture order count
10462306a36Sopenharmony_ci */
10562306a36Sopenharmony_cistruct hantro_h264_dec_hw_ctx {
10662306a36Sopenharmony_ci	struct hantro_aux_buf priv;
10762306a36Sopenharmony_ci	struct v4l2_h264_dpb_entry dpb[HANTRO_H264_DPB_SIZE];
10862306a36Sopenharmony_ci	struct hantro_h264_dec_reflists reflists;
10962306a36Sopenharmony_ci	struct hantro_h264_dec_ctrls ctrls;
11062306a36Sopenharmony_ci	u32 dpb_longterm;
11162306a36Sopenharmony_ci	u32 dpb_valid;
11262306a36Sopenharmony_ci	s32 cur_poc;
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/**
11662306a36Sopenharmony_ci * struct hantro_hevc_dec_ctrls
11762306a36Sopenharmony_ci * @decode_params: Decode params
11862306a36Sopenharmony_ci * @scaling:	Scaling matrix
11962306a36Sopenharmony_ci * @sps:	SPS info
12062306a36Sopenharmony_ci * @pps:	PPS info
12162306a36Sopenharmony_ci * @hevc_hdr_skip_length: the number of data (in bits) to skip in the
12262306a36Sopenharmony_ci *			  slice segment header syntax after 'slice type'
12362306a36Sopenharmony_ci *			  token
12462306a36Sopenharmony_ci */
12562306a36Sopenharmony_cistruct hantro_hevc_dec_ctrls {
12662306a36Sopenharmony_ci	const struct v4l2_ctrl_hevc_decode_params *decode_params;
12762306a36Sopenharmony_ci	const struct v4l2_ctrl_hevc_scaling_matrix *scaling;
12862306a36Sopenharmony_ci	const struct v4l2_ctrl_hevc_sps *sps;
12962306a36Sopenharmony_ci	const struct v4l2_ctrl_hevc_pps *pps;
13062306a36Sopenharmony_ci	u32 hevc_hdr_skip_length;
13162306a36Sopenharmony_ci};
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci/**
13462306a36Sopenharmony_ci * struct hantro_hevc_dec_hw_ctx
13562306a36Sopenharmony_ci * @tile_sizes:		Tile sizes buffer
13662306a36Sopenharmony_ci * @tile_filter:	Tile vertical filter buffer
13762306a36Sopenharmony_ci * @tile_sao:		Tile SAO buffer
13862306a36Sopenharmony_ci * @tile_bsd:		Tile BSD control buffer
13962306a36Sopenharmony_ci * @ref_bufs:		Internal reference buffers
14062306a36Sopenharmony_ci * @scaling_lists:	Scaling lists buffer
14162306a36Sopenharmony_ci * @ref_bufs_poc:	Internal reference buffers picture order count
14262306a36Sopenharmony_ci * @ref_bufs_used:	Bitfield of used reference buffers
14362306a36Sopenharmony_ci * @ctrls:		V4L2 controls attached to a run
14462306a36Sopenharmony_ci * @num_tile_cols_allocated: number of allocated tiles
14562306a36Sopenharmony_ci */
14662306a36Sopenharmony_cistruct hantro_hevc_dec_hw_ctx {
14762306a36Sopenharmony_ci	struct hantro_aux_buf tile_sizes;
14862306a36Sopenharmony_ci	struct hantro_aux_buf tile_filter;
14962306a36Sopenharmony_ci	struct hantro_aux_buf tile_sao;
15062306a36Sopenharmony_ci	struct hantro_aux_buf tile_bsd;
15162306a36Sopenharmony_ci	struct hantro_aux_buf ref_bufs[NUM_REF_PICTURES];
15262306a36Sopenharmony_ci	struct hantro_aux_buf scaling_lists;
15362306a36Sopenharmony_ci	s32 ref_bufs_poc[NUM_REF_PICTURES];
15462306a36Sopenharmony_ci	u32 ref_bufs_used;
15562306a36Sopenharmony_ci	struct hantro_hevc_dec_ctrls ctrls;
15662306a36Sopenharmony_ci	unsigned int num_tile_cols_allocated;
15762306a36Sopenharmony_ci};
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci/**
16062306a36Sopenharmony_ci * struct hantro_mpeg2_dec_hw_ctx
16162306a36Sopenharmony_ci *
16262306a36Sopenharmony_ci * @qtable:		Quantization table
16362306a36Sopenharmony_ci */
16462306a36Sopenharmony_cistruct hantro_mpeg2_dec_hw_ctx {
16562306a36Sopenharmony_ci	struct hantro_aux_buf qtable;
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci/**
16962306a36Sopenharmony_ci * struct hantro_vp8_dec_hw_ctx
17062306a36Sopenharmony_ci *
17162306a36Sopenharmony_ci * @segment_map:	Segment map buffer.
17262306a36Sopenharmony_ci * @prob_tbl:		Probability table buffer.
17362306a36Sopenharmony_ci */
17462306a36Sopenharmony_cistruct hantro_vp8_dec_hw_ctx {
17562306a36Sopenharmony_ci	struct hantro_aux_buf segment_map;
17662306a36Sopenharmony_ci	struct hantro_aux_buf prob_tbl;
17762306a36Sopenharmony_ci};
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/**
18062306a36Sopenharmony_ci * struct hantro_vp9_frame_info
18162306a36Sopenharmony_ci *
18262306a36Sopenharmony_ci * @valid: frame info valid flag
18362306a36Sopenharmony_ci * @frame_context_idx: index of frame context
18462306a36Sopenharmony_ci * @reference_mode: inter prediction type
18562306a36Sopenharmony_ci * @tx_mode: transform mode
18662306a36Sopenharmony_ci * @interpolation_filter: filter selection for inter prediction
18762306a36Sopenharmony_ci * @flags: frame flags
18862306a36Sopenharmony_ci * @timestamp: frame timestamp
18962306a36Sopenharmony_ci */
19062306a36Sopenharmony_cistruct hantro_vp9_frame_info {
19162306a36Sopenharmony_ci	u32 valid : 1;
19262306a36Sopenharmony_ci	u32 frame_context_idx : 2;
19362306a36Sopenharmony_ci	u32 reference_mode : 2;
19462306a36Sopenharmony_ci	u32 tx_mode : 3;
19562306a36Sopenharmony_ci	u32 interpolation_filter : 3;
19662306a36Sopenharmony_ci	u32 flags;
19762306a36Sopenharmony_ci	u64 timestamp;
19862306a36Sopenharmony_ci};
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci#define MAX_SB_COLS	64
20162306a36Sopenharmony_ci#define MAX_SB_ROWS	34
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci/**
20462306a36Sopenharmony_ci * struct hantro_vp9_dec_hw_ctx
20562306a36Sopenharmony_ci *
20662306a36Sopenharmony_ci * @tile_edge: auxiliary DMA buffer for tile edge processing
20762306a36Sopenharmony_ci * @segment_map: auxiliary DMA buffer for segment map
20862306a36Sopenharmony_ci * @misc: auxiliary DMA buffer for tile info, probabilities and hw counters
20962306a36Sopenharmony_ci * @cnts: vp9 library struct for abstracting hw counters access
21062306a36Sopenharmony_ci * @probability_tables: VP9 probability tables implied by the spec
21162306a36Sopenharmony_ci * @frame_context: VP9 frame contexts
21262306a36Sopenharmony_ci * @cur: current frame information
21362306a36Sopenharmony_ci * @last: last frame information
21462306a36Sopenharmony_ci * @bsd_ctrl_offset: bsd offset into tile_edge
21562306a36Sopenharmony_ci * @segment_map_size: size of segment map
21662306a36Sopenharmony_ci * @ctx_counters_offset: hw counters offset into misc
21762306a36Sopenharmony_ci * @tile_info_offset: tile info offset into misc
21862306a36Sopenharmony_ci * @tile_r_info: per-tile information array
21962306a36Sopenharmony_ci * @tile_c_info: per-tile information array
22062306a36Sopenharmony_ci * @last_tile_r: last number of tile rows
22162306a36Sopenharmony_ci * @last_tile_c: last number of tile cols
22262306a36Sopenharmony_ci * @last_sbs_r: last number of superblock rows
22362306a36Sopenharmony_ci * @last_sbs_c: last number of superblock cols
22462306a36Sopenharmony_ci * @active_segment: number of active segment (alternating between 0 and 1)
22562306a36Sopenharmony_ci * @feature_enabled: segmentation feature enabled flags
22662306a36Sopenharmony_ci * @feature_data: segmentation feature data
22762306a36Sopenharmony_ci */
22862306a36Sopenharmony_cistruct hantro_vp9_dec_hw_ctx {
22962306a36Sopenharmony_ci	struct hantro_aux_buf tile_edge;
23062306a36Sopenharmony_ci	struct hantro_aux_buf segment_map;
23162306a36Sopenharmony_ci	struct hantro_aux_buf misc;
23262306a36Sopenharmony_ci	struct v4l2_vp9_frame_symbol_counts cnts;
23362306a36Sopenharmony_ci	struct v4l2_vp9_frame_context probability_tables;
23462306a36Sopenharmony_ci	struct v4l2_vp9_frame_context frame_context[4];
23562306a36Sopenharmony_ci	struct hantro_vp9_frame_info cur;
23662306a36Sopenharmony_ci	struct hantro_vp9_frame_info last;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	unsigned int bsd_ctrl_offset;
23962306a36Sopenharmony_ci	unsigned int segment_map_size;
24062306a36Sopenharmony_ci	unsigned int ctx_counters_offset;
24162306a36Sopenharmony_ci	unsigned int tile_info_offset;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	unsigned short tile_r_info[MAX_SB_ROWS];
24462306a36Sopenharmony_ci	unsigned short tile_c_info[MAX_SB_COLS];
24562306a36Sopenharmony_ci	unsigned int last_tile_r;
24662306a36Sopenharmony_ci	unsigned int last_tile_c;
24762306a36Sopenharmony_ci	unsigned int last_sbs_r;
24862306a36Sopenharmony_ci	unsigned int last_sbs_c;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	unsigned int active_segment;
25162306a36Sopenharmony_ci	u8 feature_enabled[8];
25262306a36Sopenharmony_ci	s16 feature_data[8][4];
25362306a36Sopenharmony_ci};
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci/**
25662306a36Sopenharmony_ci * struct hantro_av1_dec_ctrls
25762306a36Sopenharmony_ci * @sequence:		AV1 Sequence
25862306a36Sopenharmony_ci * @tile_group_entry:	AV1 Tile Group entry
25962306a36Sopenharmony_ci * @frame:		AV1 Frame Header OBU
26062306a36Sopenharmony_ci * @film_grain:		AV1 Film Grain
26162306a36Sopenharmony_ci */
26262306a36Sopenharmony_cistruct hantro_av1_dec_ctrls {
26362306a36Sopenharmony_ci	const struct v4l2_ctrl_av1_sequence *sequence;
26462306a36Sopenharmony_ci	const struct v4l2_ctrl_av1_tile_group_entry *tile_group_entry;
26562306a36Sopenharmony_ci	const struct v4l2_ctrl_av1_frame *frame;
26662306a36Sopenharmony_ci	const struct v4l2_ctrl_av1_film_grain *film_grain;
26762306a36Sopenharmony_ci};
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistruct hantro_av1_frame_ref {
27062306a36Sopenharmony_ci	int width;
27162306a36Sopenharmony_ci	int height;
27262306a36Sopenharmony_ci	int mi_cols;
27362306a36Sopenharmony_ci	int mi_rows;
27462306a36Sopenharmony_ci	u64 timestamp;
27562306a36Sopenharmony_ci	enum v4l2_av1_frame_type frame_type;
27662306a36Sopenharmony_ci	bool used;
27762306a36Sopenharmony_ci	u32 order_hint;
27862306a36Sopenharmony_ci	u32 order_hints[V4L2_AV1_TOTAL_REFS_PER_FRAME];
27962306a36Sopenharmony_ci	struct vb2_v4l2_buffer *vb2_ref;
28062306a36Sopenharmony_ci};
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci/**
28362306a36Sopenharmony_ci * struct hantro_av1_dec_hw_ctx
28462306a36Sopenharmony_ci * @db_data_col:	db tile col data buffer
28562306a36Sopenharmony_ci * @db_ctrl_col:	db tile col ctrl buffer
28662306a36Sopenharmony_ci * @cdef_col:		cdef tile col buffer
28762306a36Sopenharmony_ci * @sr_col:		sr tile col buffer
28862306a36Sopenharmony_ci * @lr_col:		lr tile col buffer
28962306a36Sopenharmony_ci * @global_model:	global model buffer
29062306a36Sopenharmony_ci * @tile_info:		tile info buffer
29162306a36Sopenharmony_ci * @segment:		segmentation info buffer
29262306a36Sopenharmony_ci * @film_grain:		film grain buffer
29362306a36Sopenharmony_ci * @prob_tbl:		probability table
29462306a36Sopenharmony_ci * @prob_tbl_out:	probability table output
29562306a36Sopenharmony_ci * @tile_buf:		tile buffer
29662306a36Sopenharmony_ci * @ctrls:		V4L2 controls attached to a run
29762306a36Sopenharmony_ci * @frame_refs:		reference frames info slots
29862306a36Sopenharmony_ci * @ref_frame_sign_bias: array of sign bias
29962306a36Sopenharmony_ci * @num_tile_cols_allocated: number of allocated tiles
30062306a36Sopenharmony_ci * @cdfs:		current probabilities structure
30162306a36Sopenharmony_ci * @cdfs_ndvc:		current mv probabilities structure
30262306a36Sopenharmony_ci * @default_cdfs:	default probabilities structure
30362306a36Sopenharmony_ci * @default_cdfs_ndvc:	default mv probabilties structure
30462306a36Sopenharmony_ci * @cdfs_last:		stored probabilities structures
30562306a36Sopenharmony_ci * @cdfs_last_ndvc:	stored mv probabilities structures
30662306a36Sopenharmony_ci * @current_frame_index: index of the current in frame_refs array
30762306a36Sopenharmony_ci */
30862306a36Sopenharmony_cistruct hantro_av1_dec_hw_ctx {
30962306a36Sopenharmony_ci	struct hantro_aux_buf db_data_col;
31062306a36Sopenharmony_ci	struct hantro_aux_buf db_ctrl_col;
31162306a36Sopenharmony_ci	struct hantro_aux_buf cdef_col;
31262306a36Sopenharmony_ci	struct hantro_aux_buf sr_col;
31362306a36Sopenharmony_ci	struct hantro_aux_buf lr_col;
31462306a36Sopenharmony_ci	struct hantro_aux_buf global_model;
31562306a36Sopenharmony_ci	struct hantro_aux_buf tile_info;
31662306a36Sopenharmony_ci	struct hantro_aux_buf segment;
31762306a36Sopenharmony_ci	struct hantro_aux_buf film_grain;
31862306a36Sopenharmony_ci	struct hantro_aux_buf prob_tbl;
31962306a36Sopenharmony_ci	struct hantro_aux_buf prob_tbl_out;
32062306a36Sopenharmony_ci	struct hantro_aux_buf tile_buf;
32162306a36Sopenharmony_ci	struct hantro_av1_dec_ctrls ctrls;
32262306a36Sopenharmony_ci	struct hantro_av1_frame_ref frame_refs[AV1_MAX_FRAME_BUF_COUNT];
32362306a36Sopenharmony_ci	u32 ref_frame_sign_bias[V4L2_AV1_TOTAL_REFS_PER_FRAME];
32462306a36Sopenharmony_ci	unsigned int num_tile_cols_allocated;
32562306a36Sopenharmony_ci	struct av1cdfs *cdfs;
32662306a36Sopenharmony_ci	struct mvcdfs  *cdfs_ndvc;
32762306a36Sopenharmony_ci	struct av1cdfs default_cdfs;
32862306a36Sopenharmony_ci	struct mvcdfs  default_cdfs_ndvc;
32962306a36Sopenharmony_ci	struct av1cdfs cdfs_last[NUM_REF_FRAMES];
33062306a36Sopenharmony_ci	struct mvcdfs  cdfs_last_ndvc[NUM_REF_FRAMES];
33162306a36Sopenharmony_ci	int current_frame_index;
33262306a36Sopenharmony_ci};
33362306a36Sopenharmony_ci/**
33462306a36Sopenharmony_ci * struct hantro_postproc_ctx
33562306a36Sopenharmony_ci *
33662306a36Sopenharmony_ci * @dec_q:		References buffers, in decoder format.
33762306a36Sopenharmony_ci */
33862306a36Sopenharmony_cistruct hantro_postproc_ctx {
33962306a36Sopenharmony_ci	struct hantro_aux_buf dec_q[VB2_MAX_FRAME];
34062306a36Sopenharmony_ci};
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci/**
34362306a36Sopenharmony_ci * struct hantro_postproc_ops - post-processor operations
34462306a36Sopenharmony_ci *
34562306a36Sopenharmony_ci * @enable:		Enable the post-processor block. Optional.
34662306a36Sopenharmony_ci * @disable:		Disable the post-processor block. Optional.
34762306a36Sopenharmony_ci * @enum_framesizes:	Enumerate possible scaled output formats.
34862306a36Sopenharmony_ci *			Returns zero if OK, a negative value in error cases.
34962306a36Sopenharmony_ci *			Optional.
35062306a36Sopenharmony_ci */
35162306a36Sopenharmony_cistruct hantro_postproc_ops {
35262306a36Sopenharmony_ci	void (*enable)(struct hantro_ctx *ctx);
35362306a36Sopenharmony_ci	void (*disable)(struct hantro_ctx *ctx);
35462306a36Sopenharmony_ci	int (*enum_framesizes)(struct hantro_ctx *ctx, struct v4l2_frmsizeenum *fsize);
35562306a36Sopenharmony_ci};
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci/**
35862306a36Sopenharmony_ci * struct hantro_codec_ops - codec mode specific operations
35962306a36Sopenharmony_ci *
36062306a36Sopenharmony_ci * @init:	If needed, can be used for initialization.
36162306a36Sopenharmony_ci *		Optional and called from process context.
36262306a36Sopenharmony_ci * @exit:	If needed, can be used to undo the .init phase.
36362306a36Sopenharmony_ci *		Optional and called from process context.
36462306a36Sopenharmony_ci * @run:	Start single {en,de)coding job. Called from atomic context
36562306a36Sopenharmony_ci *		to indicate that a pair of buffers is ready and the hardware
36662306a36Sopenharmony_ci *		should be programmed and started. Returns zero if OK, a
36762306a36Sopenharmony_ci *		negative value in error cases.
36862306a36Sopenharmony_ci * @done:	Read back processing results and additional data from hardware.
36962306a36Sopenharmony_ci * @reset:	Reset the hardware in case of a timeout.
37062306a36Sopenharmony_ci */
37162306a36Sopenharmony_cistruct hantro_codec_ops {
37262306a36Sopenharmony_ci	int (*init)(struct hantro_ctx *ctx);
37362306a36Sopenharmony_ci	void (*exit)(struct hantro_ctx *ctx);
37462306a36Sopenharmony_ci	int (*run)(struct hantro_ctx *ctx);
37562306a36Sopenharmony_ci	void (*done)(struct hantro_ctx *ctx);
37662306a36Sopenharmony_ci	void (*reset)(struct hantro_ctx *ctx);
37762306a36Sopenharmony_ci};
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci/**
38062306a36Sopenharmony_ci * enum hantro_enc_fmt - source format ID for hardware registers.
38162306a36Sopenharmony_ci *
38262306a36Sopenharmony_ci * @ROCKCHIP_VPU_ENC_FMT_YUV420P: Y/CbCr 4:2:0 planar format
38362306a36Sopenharmony_ci * @ROCKCHIP_VPU_ENC_FMT_YUV420SP: Y/CbCr 4:2:0 semi-planar format
38462306a36Sopenharmony_ci * @ROCKCHIP_VPU_ENC_FMT_YUYV422: YUV 4:2:2 packed format (YUYV)
38562306a36Sopenharmony_ci * @ROCKCHIP_VPU_ENC_FMT_UYVY422: YUV 4:2:2 packed format (UYVY)
38662306a36Sopenharmony_ci */
38762306a36Sopenharmony_cienum hantro_enc_fmt {
38862306a36Sopenharmony_ci	ROCKCHIP_VPU_ENC_FMT_YUV420P = 0,
38962306a36Sopenharmony_ci	ROCKCHIP_VPU_ENC_FMT_YUV420SP = 1,
39062306a36Sopenharmony_ci	ROCKCHIP_VPU_ENC_FMT_YUYV422 = 2,
39162306a36Sopenharmony_ci	ROCKCHIP_VPU_ENC_FMT_UYVY422 = 3,
39262306a36Sopenharmony_ci};
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ciextern const struct hantro_variant imx8mm_vpu_g1_variant;
39562306a36Sopenharmony_ciextern const struct hantro_variant imx8mq_vpu_g1_variant;
39662306a36Sopenharmony_ciextern const struct hantro_variant imx8mq_vpu_g2_variant;
39762306a36Sopenharmony_ciextern const struct hantro_variant imx8mq_vpu_variant;
39862306a36Sopenharmony_ciextern const struct hantro_variant px30_vpu_variant;
39962306a36Sopenharmony_ciextern const struct hantro_variant rk3036_vpu_variant;
40062306a36Sopenharmony_ciextern const struct hantro_variant rk3066_vpu_variant;
40162306a36Sopenharmony_ciextern const struct hantro_variant rk3288_vpu_variant;
40262306a36Sopenharmony_ciextern const struct hantro_variant rk3328_vpu_variant;
40362306a36Sopenharmony_ciextern const struct hantro_variant rk3399_vpu_variant;
40462306a36Sopenharmony_ciextern const struct hantro_variant rk3568_vepu_variant;
40562306a36Sopenharmony_ciextern const struct hantro_variant rk3568_vpu_variant;
40662306a36Sopenharmony_ciextern const struct hantro_variant rk3588_vpu981_variant;
40762306a36Sopenharmony_ciextern const struct hantro_variant sama5d4_vdec_variant;
40862306a36Sopenharmony_ciextern const struct hantro_variant sunxi_vpu_variant;
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ciextern const struct hantro_postproc_ops hantro_g1_postproc_ops;
41162306a36Sopenharmony_ciextern const struct hantro_postproc_ops hantro_g2_postproc_ops;
41262306a36Sopenharmony_ciextern const struct hantro_postproc_ops rockchip_vpu981_postproc_ops;
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ciextern const u32 hantro_vp8_dec_mc_filter[8][6];
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_civoid hantro_watchdog(struct work_struct *work);
41762306a36Sopenharmony_civoid hantro_run(struct hantro_ctx *ctx);
41862306a36Sopenharmony_civoid hantro_irq_done(struct hantro_dev *vpu,
41962306a36Sopenharmony_ci		     enum vb2_buffer_state result);
42062306a36Sopenharmony_civoid hantro_start_prepare_run(struct hantro_ctx *ctx);
42162306a36Sopenharmony_civoid hantro_end_prepare_run(struct hantro_ctx *ctx);
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ciirqreturn_t hantro_g1_irq(int irq, void *dev_id);
42462306a36Sopenharmony_civoid hantro_g1_reset(struct hantro_ctx *ctx);
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ciint hantro_h1_jpeg_enc_run(struct hantro_ctx *ctx);
42762306a36Sopenharmony_ciint rockchip_vpu2_jpeg_enc_run(struct hantro_ctx *ctx);
42862306a36Sopenharmony_civoid hantro_h1_jpeg_enc_done(struct hantro_ctx *ctx);
42962306a36Sopenharmony_civoid rockchip_vpu2_jpeg_enc_done(struct hantro_ctx *ctx);
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cidma_addr_t hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
43262306a36Sopenharmony_ci				   unsigned int dpb_idx);
43362306a36Sopenharmony_ciu16 hantro_h264_get_ref_nbr(struct hantro_ctx *ctx,
43462306a36Sopenharmony_ci			    unsigned int dpb_idx);
43562306a36Sopenharmony_ciint hantro_h264_dec_prepare_run(struct hantro_ctx *ctx);
43662306a36Sopenharmony_ciint rockchip_vpu2_h264_dec_run(struct hantro_ctx *ctx);
43762306a36Sopenharmony_ciint hantro_g1_h264_dec_run(struct hantro_ctx *ctx);
43862306a36Sopenharmony_ciint hantro_h264_dec_init(struct hantro_ctx *ctx);
43962306a36Sopenharmony_civoid hantro_h264_dec_exit(struct hantro_ctx *ctx);
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ciint hantro_hevc_dec_init(struct hantro_ctx *ctx);
44262306a36Sopenharmony_civoid hantro_hevc_dec_exit(struct hantro_ctx *ctx);
44362306a36Sopenharmony_ciint hantro_g2_hevc_dec_run(struct hantro_ctx *ctx);
44462306a36Sopenharmony_ciint hantro_hevc_dec_prepare_run(struct hantro_ctx *ctx);
44562306a36Sopenharmony_civoid hantro_hevc_ref_init(struct hantro_ctx *ctx);
44662306a36Sopenharmony_cidma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx, s32 poc);
44762306a36Sopenharmony_ciint hantro_hevc_add_ref_buf(struct hantro_ctx *ctx, int poc, dma_addr_t addr);
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ciint rockchip_vpu981_av1_dec_init(struct hantro_ctx *ctx);
45062306a36Sopenharmony_civoid rockchip_vpu981_av1_dec_exit(struct hantro_ctx *ctx);
45162306a36Sopenharmony_ciint rockchip_vpu981_av1_dec_run(struct hantro_ctx *ctx);
45262306a36Sopenharmony_civoid rockchip_vpu981_av1_dec_done(struct hantro_ctx *ctx);
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_cistatic inline unsigned short hantro_vp9_num_sbs(unsigned short dimension)
45562306a36Sopenharmony_ci{
45662306a36Sopenharmony_ci	return (dimension + 63) / 64;
45762306a36Sopenharmony_ci}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_cistatic inline size_t
46062306a36Sopenharmony_cihantro_vp9_mv_size(unsigned int width, unsigned int height)
46162306a36Sopenharmony_ci{
46262306a36Sopenharmony_ci	int num_ctbs;
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	/*
46562306a36Sopenharmony_ci	 * There can be up to (CTBs x 64) number of blocks,
46662306a36Sopenharmony_ci	 * and the motion vector for each block needs 16 bytes.
46762306a36Sopenharmony_ci	 */
46862306a36Sopenharmony_ci	num_ctbs = hantro_vp9_num_sbs(width) * hantro_vp9_num_sbs(height);
46962306a36Sopenharmony_ci	return (num_ctbs * 64) * 16;
47062306a36Sopenharmony_ci}
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_cistatic inline size_t
47362306a36Sopenharmony_cihantro_h264_mv_size(unsigned int width, unsigned int height)
47462306a36Sopenharmony_ci{
47562306a36Sopenharmony_ci	/*
47662306a36Sopenharmony_ci	 * A decoded 8-bit 4:2:0 NV12 frame may need memory for up to
47762306a36Sopenharmony_ci	 * 448 bytes per macroblock with additional 32 bytes on
47862306a36Sopenharmony_ci	 * multi-core variants.
47962306a36Sopenharmony_ci	 *
48062306a36Sopenharmony_ci	 * The H264 decoder needs extra space on the output buffers
48162306a36Sopenharmony_ci	 * to store motion vectors. This is needed for reference
48262306a36Sopenharmony_ci	 * frames and only if the format is non-post-processed NV12.
48362306a36Sopenharmony_ci	 *
48462306a36Sopenharmony_ci	 * Memory layout is as follow:
48562306a36Sopenharmony_ci	 *
48662306a36Sopenharmony_ci	 * +---------------------------+
48762306a36Sopenharmony_ci	 * | Y-plane   256 bytes x MBs |
48862306a36Sopenharmony_ci	 * +---------------------------+
48962306a36Sopenharmony_ci	 * | UV-plane  128 bytes x MBs |
49062306a36Sopenharmony_ci	 * +---------------------------+
49162306a36Sopenharmony_ci	 * | MV buffer  64 bytes x MBs |
49262306a36Sopenharmony_ci	 * +---------------------------+
49362306a36Sopenharmony_ci	 * | MC sync          32 bytes |
49462306a36Sopenharmony_ci	 * +---------------------------+
49562306a36Sopenharmony_ci	 */
49662306a36Sopenharmony_ci	return 64 * MB_WIDTH(width) * MB_WIDTH(height) + 32;
49762306a36Sopenharmony_ci}
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_cistatic inline size_t
50062306a36Sopenharmony_cihantro_hevc_mv_size(unsigned int width, unsigned int height)
50162306a36Sopenharmony_ci{
50262306a36Sopenharmony_ci	/*
50362306a36Sopenharmony_ci	 * A CTB can be 64x64, 32x32 or 16x16.
50462306a36Sopenharmony_ci	 * Allocated memory for the "worse" case: 16x16
50562306a36Sopenharmony_ci	 */
50662306a36Sopenharmony_ci	return width * height / 16;
50762306a36Sopenharmony_ci}
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_cistatic inline unsigned short hantro_av1_num_sbs(unsigned short dimension)
51062306a36Sopenharmony_ci{
51162306a36Sopenharmony_ci	return DIV_ROUND_UP(dimension, 64);
51262306a36Sopenharmony_ci}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_cistatic inline size_t
51562306a36Sopenharmony_cihantro_av1_mv_size(unsigned int width, unsigned int height)
51662306a36Sopenharmony_ci{
51762306a36Sopenharmony_ci	size_t num_sbs = hantro_av1_num_sbs(width) * hantro_av1_num_sbs(height);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	return ALIGN(num_sbs * 384, 16) * 2 + 512;
52062306a36Sopenharmony_ci}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ciint hantro_g1_mpeg2_dec_run(struct hantro_ctx *ctx);
52362306a36Sopenharmony_ciint rockchip_vpu2_mpeg2_dec_run(struct hantro_ctx *ctx);
52462306a36Sopenharmony_civoid hantro_mpeg2_dec_copy_qtable(u8 *qtable,
52562306a36Sopenharmony_ci				  const struct v4l2_ctrl_mpeg2_quantisation *ctrl);
52662306a36Sopenharmony_ciint hantro_mpeg2_dec_init(struct hantro_ctx *ctx);
52762306a36Sopenharmony_civoid hantro_mpeg2_dec_exit(struct hantro_ctx *ctx);
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ciint hantro_g1_vp8_dec_run(struct hantro_ctx *ctx);
53062306a36Sopenharmony_ciint rockchip_vpu2_vp8_dec_run(struct hantro_ctx *ctx);
53162306a36Sopenharmony_ciint hantro_vp8_dec_init(struct hantro_ctx *ctx);
53262306a36Sopenharmony_civoid hantro_vp8_dec_exit(struct hantro_ctx *ctx);
53362306a36Sopenharmony_civoid hantro_vp8_prob_update(struct hantro_ctx *ctx,
53462306a36Sopenharmony_ci			    const struct v4l2_ctrl_vp8_frame *hdr);
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ciint hantro_g2_vp9_dec_run(struct hantro_ctx *ctx);
53762306a36Sopenharmony_civoid hantro_g2_vp9_dec_done(struct hantro_ctx *ctx);
53862306a36Sopenharmony_ciint hantro_vp9_dec_init(struct hantro_ctx *ctx);
53962306a36Sopenharmony_civoid hantro_vp9_dec_exit(struct hantro_ctx *ctx);
54062306a36Sopenharmony_civoid hantro_g2_check_idle(struct hantro_dev *vpu);
54162306a36Sopenharmony_ciirqreturn_t hantro_g2_irq(int irq, void *dev_id);
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci#endif /* HANTRO_HW_H_ */
544