1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2017 Linaro Ltd.
5  */
6 
7 #ifndef __VENUS_CORE_H_
8 #define __VENUS_CORE_H_
9 
10 #include <linux/list.h>
11 #include <media/videobuf2-v4l2.h>
12 #include <media/v4l2-ctrls.h>
13 #include <media/v4l2-device.h>
14 
15 #include "hfi.h"
16 
17 #define VIDC_CLKS_NUM_MAX		4
18 #define VIDC_VCODEC_CLKS_NUM_MAX	2
19 #define VIDC_PMDOMAINS_NUM_MAX		3
20 
21 struct freq_tbl {
22 	unsigned int load;
23 	unsigned long freq;
24 };
25 
26 struct reg_val {
27 	u32 reg;
28 	u32 value;
29 };
30 
31 struct codec_freq_data {
32 	u32 pixfmt;
33 	u32 session_type;
34 	unsigned long vpp_freq;
35 	unsigned long vsp_freq;
36 };
37 
38 struct bw_tbl {
39 	u32 mbs_per_sec;
40 	u32 avg;
41 	u32 peak;
42 	u32 avg_10bit;
43 	u32 peak_10bit;
44 };
45 
46 struct venus_resources {
47 	u64 dma_mask;
48 	const struct freq_tbl *freq_tbl;
49 	unsigned int freq_tbl_size;
50 	const struct bw_tbl *bw_tbl_enc;
51 	unsigned int bw_tbl_enc_size;
52 	const struct bw_tbl *bw_tbl_dec;
53 	unsigned int bw_tbl_dec_size;
54 	const struct reg_val *reg_tbl;
55 	unsigned int reg_tbl_size;
56 	const struct codec_freq_data *codec_freq_data;
57 	unsigned int codec_freq_data_size;
58 	const char * const clks[VIDC_CLKS_NUM_MAX];
59 	unsigned int clks_num;
60 	const char * const vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
61 	const char * const vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
62 	unsigned int vcodec_clks_num;
63 	const char * const vcodec_pmdomains[VIDC_PMDOMAINS_NUM_MAX];
64 	unsigned int vcodec_pmdomains_num;
65 	unsigned int vcodec_num;
66 	enum hfi_version hfi_version;
67 	u32 max_load;
68 	unsigned int vmem_id;
69 	u32 vmem_size;
70 	u32 vmem_addr;
71 	const char *fwname;
72 };
73 
74 struct venus_format {
75 	u32 pixfmt;
76 	unsigned int num_planes;
77 	u32 type;
78 	u32 flags;
79 };
80 
81 #define MAX_PLANES		4
82 #define MAX_FMT_ENTRIES		32
83 #define MAX_CAP_ENTRIES		32
84 #define MAX_ALLOC_MODE_ENTRIES	16
85 #define MAX_CODEC_NUM		32
86 
87 struct raw_formats {
88 	u32 buftype;
89 	u32 fmt;
90 };
91 
92 struct venus_caps {
93 	u32 codec;
94 	u32 domain;
95 	bool cap_bufs_mode_dynamic;
96 	unsigned int num_caps;
97 	struct hfi_capability caps[MAX_CAP_ENTRIES];
98 	unsigned int num_pl;
99 	struct hfi_profile_level pl[HFI_MAX_PROFILE_COUNT];
100 	unsigned int num_fmts;
101 	struct raw_formats fmts[MAX_FMT_ENTRIES];
102 	bool valid;	/* used only for Venus v1xx */
103 };
104 
105 /**
106  * struct venus_core - holds core parameters valid for all instances
107  *
108  * @base:	IO memory base address
109  * @irq:		Venus irq
110  * @clks:	an array of struct clk pointers
111  * @vcodec0_clks: an array of vcodec0 struct clk pointers
112  * @vcodec1_clks: an array of vcodec1 struct clk pointers
113  * @pd_dl_venus: pmdomain device-link for venus domain
114  * @pmdomains:	an array of pmdomains struct device pointers
115  * @vdev_dec:	a reference to video device structure for decoder instances
116  * @vdev_enc:	a reference to video device structure for encoder instances
117  * @v4l2_dev:	a holder for v4l2 device structure
118  * @res:		a reference to venus resources structure
119  * @dev:		convenience struct device pointer
120  * @dev_dec:	convenience struct device pointer for decoder device
121  * @dev_enc:	convenience struct device pointer for encoder device
122  * @use_tz:	a flag that suggests presence of trustzone
123  * @lock:	a lock for this strucure
124  * @instances:	a list_head of all instances
125  * @insts_count:	num of instances
126  * @state:	the state of the venus core
127  * @done:	a completion for sync HFI operations
128  * @error:	an error returned during last HFI sync operations
129  * @sys_error:	an error flag that signal system error event
130  * @core_ops:	the core operations
131  * @enc_codecs:	encoders supported by this core
132  * @dec_codecs:	decoders supported by this core
133  * @max_sessions_supported:	holds the maximum number of sessions
134  * @core_caps:	core capabilities
135  * @priv:	a private filed for HFI operations
136  * @ops:		the core HFI operations
137  * @work:	a delayed work for handling system fatal error
138  */
139 struct venus_core {
140 	void __iomem *base;
141 	int irq;
142 	struct clk *clks[VIDC_CLKS_NUM_MAX];
143 	struct clk *vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
144 	struct clk *vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
145 	struct icc_path *video_path;
146 	struct icc_path *cpucfg_path;
147 	struct device_link *pd_dl_venus;
148 	struct device *pmdomains[VIDC_PMDOMAINS_NUM_MAX];
149 	struct video_device *vdev_dec;
150 	struct video_device *vdev_enc;
151 	struct v4l2_device v4l2_dev;
152 	const struct venus_resources *res;
153 	struct device *dev;
154 	struct device *dev_dec;
155 	struct device *dev_enc;
156 	unsigned int use_tz;
157 	struct video_firmware {
158 		struct device *dev;
159 		struct iommu_domain *iommu_domain;
160 		size_t mapped_mem_size;
161 	} fw;
162 	struct mutex lock;
163 	struct list_head instances;
164 	atomic_t insts_count;
165 	unsigned int state;
166 	struct completion done;
167 	unsigned int error;
168 	bool sys_error;
169 	const struct hfi_core_ops *core_ops;
170 	const struct venus_pm_ops *pm_ops;
171 	unsigned long enc_codecs;
172 	unsigned long dec_codecs;
173 	unsigned int max_sessions_supported;
174 #define ENC_ROTATION_CAPABILITY		0x1
175 #define ENC_SCALING_CAPABILITY		0x2
176 #define ENC_DEINTERLACE_CAPABILITY	0x4
177 #define DEC_MULTI_STREAM_CAPABILITY	0x8
178 	unsigned int core_caps;
179 	void *priv;
180 	const struct hfi_ops *ops;
181 	struct delayed_work work;
182 	struct venus_caps caps[MAX_CODEC_NUM];
183 	unsigned int codecs_count;
184 	unsigned int core0_usage_count;
185 	unsigned int core1_usage_count;
186 };
187 
188 struct vdec_controls {
189 	u32 post_loop_deb_mode;
190 	u32 profile;
191 	u32 level;
192 };
193 
194 struct venc_controls {
195 	u16 gop_size;
196 	u32 num_p_frames;
197 	u32 num_b_frames;
198 	u32 bitrate_mode;
199 	u32 bitrate;
200 	u32 bitrate_peak;
201 	u32 rc_enable;
202 
203 	u32 h264_i_period;
204 	u32 h264_entropy_mode;
205 	u32 h264_i_qp;
206 	u32 h264_p_qp;
207 	u32 h264_b_qp;
208 	u32 h264_min_qp;
209 	u32 h264_max_qp;
210 	u32 h264_loop_filter_mode;
211 	s32 h264_loop_filter_alpha;
212 	s32 h264_loop_filter_beta;
213 
214 	u32 vp8_min_qp;
215 	u32 vp8_max_qp;
216 
217 	u32 multi_slice_mode;
218 	u32 multi_slice_max_bytes;
219 	u32 multi_slice_max_mb;
220 
221 	u32 header_mode;
222 
223 	struct {
224 		u32 mpeg4;
225 		u32 h264;
226 		u32 vpx;
227 		u32 hevc;
228 	} profile;
229 	struct {
230 		u32 mpeg4;
231 		u32 h264;
232 		u32 hevc;
233 	} level;
234 };
235 
236 struct venus_buffer {
237 	struct vb2_v4l2_buffer vb;
238 	struct list_head list;
239 	dma_addr_t dma_addr;
240 	u32 size;
241 	struct list_head reg_list;
242 	u32 flags;
243 	struct list_head ref_list;
244 };
245 
246 struct clock_data {
247 	u32 core_id;
248 	unsigned long freq;
249 	const struct codec_freq_data *codec_freq_data;
250 };
251 
252 #define to_venus_buffer(ptr)	container_of(ptr, struct venus_buffer, vb)
253 
254 enum venus_dec_state {
255 	VENUS_DEC_STATE_DEINIT		= 0,
256 	VENUS_DEC_STATE_INIT		= 1,
257 	VENUS_DEC_STATE_CAPTURE_SETUP	= 2,
258 	VENUS_DEC_STATE_STOPPED		= 3,
259 	VENUS_DEC_STATE_SEEK		= 4,
260 	VENUS_DEC_STATE_DRAIN		= 5,
261 	VENUS_DEC_STATE_DECODING	= 6,
262 	VENUS_DEC_STATE_DRC		= 7
263 };
264 
265 struct venus_ts_metadata {
266 	bool used;
267 	u64 ts_ns;
268 	u64 ts_us;
269 	u32 flags;
270 	struct v4l2_timecode tc;
271 };
272 
273 /**
274  * struct venus_inst - holds per instance parameters
275  *
276  * @list:	used for attach an instance to the core
277  * @lock:	instance lock
278  * @core:	a reference to the core struct
279  * @dpbbufs:	a list of decoded picture buffers
280  * @internalbufs:	a list of internal bufferes
281  * @registeredbufs:	a list of registered capture bufferes
282  * @delayed_process	a list of delayed buffers
283  * @delayed_process_work:	a work_struct for process delayed buffers
284  * @ctrl_handler:	v4l control handler
285  * @controls:	a union of decoder and encoder control parameters
286  * @fh:	 a holder of v4l file handle structure
287  * @streamon_cap: stream on flag for capture queue
288  * @streamon_out: stream on flag for output queue
289  * @width:	current capture width
290  * @height:	current capture height
291  * @out_width:	current output width
292  * @out_height:	current output height
293  * @colorspace:	current color space
294  * @quantization:	current quantization
295  * @xfer_func:	current xfer function
296  * @codec_state:	current codec API state (see DEC/ENC_STATE_)
297  * @reconf_wait:	wait queue for resolution change event
298  * @subscriptions:	used to hold current events subscriptions
299  * @buf_count:		used to count number of buffers (reqbuf(0))
300  * @fps:		holds current FPS
301  * @timeperframe:	holds current time per frame structure
302  * @fmt_out:	a reference to output format structure
303  * @fmt_cap:	a reference to capture format structure
304  * @num_input_bufs:	holds number of input buffers
305  * @num_output_bufs:	holds number of output buffers
306  * @input_buf_size	holds input buffer size
307  * @output_buf_size:	holds output buffer size
308  * @output2_buf_size:	holds secondary decoder output buffer size
309  * @dpb_buftype:	decoded picture buffer type
310  * @dpb_fmt:		decoded picture buffer raw format
311  * @opb_buftype:	output picture buffer type
312  * @opb_fmt:		output picture buffer raw format
313  * @reconfig:	a flag raised by decoder when the stream resolution changed
314  * @hfi_codec:		current codec for this instance in HFI space
315  * @sequence_cap:	a sequence counter for capture queue
316  * @sequence_out:	a sequence counter for output queue
317  * @m2m_dev:	a reference to m2m device structure
318  * @m2m_ctx:	a reference to m2m context structure
319  * @state:	current state of the instance
320  * @done:	a completion for sync HFI operation
321  * @error:	an error returned during last HFI sync operation
322  * @session_error:	a flag rised by HFI interface in case of session error
323  * @ops:		HFI operations
324  * @priv:	a private for HFI operations callbacks
325  * @session_type:	the type of the session (decoder or encoder)
326  * @hprop:	a union used as a holder by get property
327  */
328 struct venus_inst {
329 	struct list_head list;
330 	struct mutex lock;
331 	struct venus_core *core;
332 	struct clock_data clk_data;
333 	struct list_head dpbbufs;
334 	struct list_head internalbufs;
335 	struct list_head registeredbufs;
336 	struct list_head delayed_process;
337 	struct work_struct delayed_process_work;
338 
339 	struct v4l2_ctrl_handler ctrl_handler;
340 	union {
341 		struct vdec_controls dec;
342 		struct venc_controls enc;
343 	} controls;
344 	struct v4l2_fh fh;
345 	unsigned int streamon_cap, streamon_out;
346 	u32 width;
347 	u32 height;
348 	u32 out_width;
349 	u32 out_height;
350 	u32 colorspace;
351 	u8 ycbcr_enc;
352 	u8 quantization;
353 	u8 xfer_func;
354 	enum venus_dec_state codec_state;
355 	wait_queue_head_t reconf_wait;
356 	unsigned int subscriptions;
357 	int buf_count;
358 	struct venus_ts_metadata tss[VIDEO_MAX_FRAME];
359 	unsigned long payloads[VIDEO_MAX_FRAME];
360 	u64 fps;
361 	struct v4l2_fract timeperframe;
362 	const struct venus_format *fmt_out;
363 	const struct venus_format *fmt_cap;
364 	unsigned int num_input_bufs;
365 	unsigned int num_output_bufs;
366 	unsigned int input_buf_size;
367 	unsigned int output_buf_size;
368 	unsigned int output2_buf_size;
369 	u32 dpb_buftype;
370 	u32 dpb_fmt;
371 	u32 opb_buftype;
372 	u32 opb_fmt;
373 	bool reconfig;
374 	u32 hfi_codec;
375 	u32 sequence_cap;
376 	u32 sequence_out;
377 	struct v4l2_m2m_dev *m2m_dev;
378 	struct v4l2_m2m_ctx *m2m_ctx;
379 	unsigned int state;
380 	struct completion done;
381 	unsigned int error;
382 	bool session_error;
383 	const struct hfi_inst_ops *ops;
384 	u32 session_type;
385 	union hfi_get_property hprop;
386 	unsigned int core_acquired: 1;
387 	unsigned int bit_depth;
388 };
389 
390 #define IS_V1(core)	((core)->res->hfi_version == HFI_VERSION_1XX)
391 #define IS_V3(core)	((core)->res->hfi_version == HFI_VERSION_3XX)
392 #define IS_V4(core)	((core)->res->hfi_version == HFI_VERSION_4XX)
393 
394 #define ctrl_to_inst(ctrl)	\
395 	container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
396 
397 static inline struct venus_inst *to_inst(struct file *filp)
398 {
399 	return container_of(filp->private_data, struct venus_inst, fh);
400 }
401 
402 static inline void *to_hfi_priv(struct venus_core *core)
403 {
404 	return core->priv;
405 }
406 
407 static inline struct venus_caps *
408 venus_caps_by_codec(struct venus_core *core, u32 codec, u32 domain)
409 {
410 	unsigned int c;
411 
412 	for (c = 0; c < core->codecs_count; c++) {
413 		if (core->caps[c].codec == codec &&
414 		    core->caps[c].domain == domain)
415 			return &core->caps[c];
416 	}
417 
418 	return NULL;
419 }
420 
421 #endif
422