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/bitops.h>
11 #include <linux/list.h>
12 #include <media/videobuf2-v4l2.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 
16 #include "dbgfs.h"
17 #include "hfi.h"
18 #include "hfi_platform.h"
19 #include "hfi_helper.h"
20 
21 #define VDBGL	"VenusLow : "
22 #define VDBGM	"VenusMed : "
23 #define VDBGH	"VenusHigh: "
24 #define VDBGFW	"VenusFW  : "
25 
26 #define VIDC_CLKS_NUM_MAX		4
27 #define VIDC_VCODEC_CLKS_NUM_MAX	2
28 #define VIDC_PMDOMAINS_NUM_MAX		3
29 #define VIDC_RESETS_NUM_MAX		2
30 
31 extern int venus_fw_debug;
32 
33 struct freq_tbl {
34 	unsigned int load;
35 	unsigned long freq;
36 };
37 
38 struct reg_val {
39 	u32 reg;
40 	u32 value;
41 };
42 
43 struct bw_tbl {
44 	u32 mbs_per_sec;
45 	u32 avg;
46 	u32 peak;
47 	u32 avg_10bit;
48 	u32 peak_10bit;
49 };
50 
51 struct venus_resources {
52 	u64 dma_mask;
53 	const struct freq_tbl *freq_tbl;
54 	unsigned int freq_tbl_size;
55 	const struct bw_tbl *bw_tbl_enc;
56 	unsigned int bw_tbl_enc_size;
57 	const struct bw_tbl *bw_tbl_dec;
58 	unsigned int bw_tbl_dec_size;
59 	const struct reg_val *reg_tbl;
60 	unsigned int reg_tbl_size;
61 	const struct hfi_ubwc_config *ubwc_conf;
62 	const char * const clks[VIDC_CLKS_NUM_MAX];
63 	unsigned int clks_num;
64 	const char * const vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
65 	const char * const vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
66 	unsigned int vcodec_clks_num;
67 	const char * const vcodec_pmdomains[VIDC_PMDOMAINS_NUM_MAX];
68 	unsigned int vcodec_pmdomains_num;
69 	const char **opp_pmdomain;
70 	unsigned int vcodec_num;
71 	const char * const resets[VIDC_RESETS_NUM_MAX];
72 	unsigned int resets_num;
73 	enum hfi_version hfi_version;
74 	u8 num_vpp_pipes;
75 	u32 max_load;
76 	unsigned int vmem_id;
77 	u32 vmem_size;
78 	u32 vmem_addr;
79 	u32 cp_start;
80 	u32 cp_size;
81 	u32 cp_nonpixel_start;
82 	u32 cp_nonpixel_size;
83 	const char *fwname;
84 };
85 
86 struct venus_format {
87 	u32 pixfmt;
88 	unsigned int num_planes;
89 	u32 type;
90 	u32 flags;
91 };
92 
93 /**
94  * struct venus_core - holds core parameters valid for all instances
95  *
96  * @base:	IO memory base address
97  * @vbif_base:	IO memory vbif base address
98  * @cpu_base:	IO memory cpu base address
99  * @cpu_cs_base:	IO memory cpu_cs base address
100  * @cpu_ic_base:	IO memory cpu_ic base address
101  * @wrapper_base:	IO memory wrapper base address
102  * @wrapper_tz_base:	IO memory wrapper TZ base address
103  * @aon_base:	AON base address
104  * @irq:		Venus irq
105  * @clks:	an array of struct clk pointers
106  * @vcodec0_clks: an array of vcodec0 struct clk pointers
107  * @vcodec1_clks: an array of vcodec1 struct clk pointers
108  * @video_path: an interconnect handle to video to/from memory path
109  * @cpucfg_path: an interconnect handle to cpu configuration path
110  * @opp_table: an device OPP table handle
111  * @has_opp_table: does OPP table exist
112  * @pmdomains:	an array of pmdomains struct device pointers
113  * @opp_dl_venus: an device-link for device OPP
114  * @opp_pmdomain: an OPP power-domain
115  * @resets: an array of reset signals
116  * @vdev_dec:	a reference to video device structure for decoder instances
117  * @vdev_enc:	a reference to video device structure for encoder instances
118  * @v4l2_dev:	a holder for v4l2 device structure
119  * @res:		a reference to venus resources structure
120  * @dev:		convenience struct device pointer
121  * @dev_dec:	convenience struct device pointer for decoder device
122  * @dev_enc:	convenience struct device pointer for encoder device
123  * @use_tz:	a flag that suggests presence of trustzone
124  * @fw:		structure of firmware parameters
125  * @lock:	a lock for this strucure
126  * @instances:	a list_head of all instances
127  * @insts_count:	num of instances
128  * @state:	the state of the venus core
129  * @done:	a completion for sync HFI operations
130  * @error:	an error returned during last HFI sync operations
131  * @sys_error:	an error flag that signal system error event
132  * @sys_err_done: a waitqueue to wait for system error recovery end
133  * @core_ops:	the core operations
134  * @pm_ops:	a pointer to pm operations
135  * @pm_lock:	a lock for PM operations
136  * @enc_codecs:	encoders supported by this core
137  * @dec_codecs:	decoders supported by this core
138  * @max_sessions_supported:	holds the maximum number of sessions
139  * @priv:	a private filed for HFI operations
140  * @ops:		the core HFI operations
141  * @work:	a delayed work for handling system fatal error
142  * @caps:	an array of supported HFI capabilities
143  * @codecs_count: platform codecs count
144  * @core0_usage_count: usage counter for core0
145  * @core1_usage_count: usage counter for core1
146  * @root:	debugfs root directory
147  */
148 struct venus_core {
149 	void __iomem *base;
150 	void __iomem *vbif_base;
151 	void __iomem *cpu_base;
152 	void __iomem *cpu_cs_base;
153 	void __iomem *cpu_ic_base;
154 	void __iomem *wrapper_base;
155 	void __iomem *wrapper_tz_base;
156 	void __iomem *aon_base;
157 	int irq;
158 	struct clk *clks[VIDC_CLKS_NUM_MAX];
159 	struct clk *vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
160 	struct clk *vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
161 	struct icc_path *video_path;
162 	struct icc_path *cpucfg_path;
163 	bool has_opp_table;
164 	struct device *pmdomains[VIDC_PMDOMAINS_NUM_MAX];
165 	struct device_link *opp_dl_venus;
166 	struct device *opp_pmdomain;
167 	struct reset_control *resets[VIDC_RESETS_NUM_MAX];
168 	struct video_device *vdev_dec;
169 	struct video_device *vdev_enc;
170 	struct v4l2_device v4l2_dev;
171 	const struct venus_resources *res;
172 	struct device *dev;
173 	struct device *dev_dec;
174 	struct device *dev_enc;
175 	unsigned int use_tz;
176 	struct video_firmware {
177 		struct device *dev;
178 		struct iommu_domain *iommu_domain;
179 		size_t mapped_mem_size;
180 		phys_addr_t mem_phys;
181 		size_t mem_size;
182 	} fw;
183 	struct mutex lock;
184 	struct list_head instances;
185 	atomic_t insts_count;
186 	unsigned int state;
187 	struct completion done;
188 	unsigned int error;
189 	unsigned long sys_error;
190 	wait_queue_head_t sys_err_done;
191 	const struct hfi_core_ops *core_ops;
192 	const struct venus_pm_ops *pm_ops;
193 	struct mutex pm_lock;
194 	unsigned long enc_codecs;
195 	unsigned long dec_codecs;
196 	unsigned int max_sessions_supported;
197 	void *priv;
198 	const struct hfi_ops *ops;
199 	struct delayed_work work;
200 	struct hfi_plat_caps caps[MAX_CODEC_NUM];
201 	unsigned int codecs_count;
202 	unsigned int core0_usage_count;
203 	unsigned int core1_usage_count;
204 	struct dentry *root;
205 };
206 
207 struct vdec_controls {
208 	u32 post_loop_deb_mode;
209 	u32 profile;
210 	u32 level;
211 	u32 display_delay;
212 	u32 display_delay_enable;
213 	u64 conceal_color;
214 };
215 
216 struct venc_controls {
217 	u16 gop_size;
218 	u32 num_p_frames;
219 	u32 num_b_frames;
220 	u32 bitrate_mode;
221 	u32 bitrate;
222 	u32 bitrate_peak;
223 	u32 rc_enable;
224 	u32 const_quality;
225 	u32 frame_skip_mode;
226 
227 	u32 h264_i_period;
228 	u32 h264_entropy_mode;
229 	u32 h264_i_qp;
230 	u32 h264_p_qp;
231 	u32 h264_b_qp;
232 	u32 h264_min_qp;
233 	u32 h264_max_qp;
234 	u32 h264_i_min_qp;
235 	u32 h264_i_max_qp;
236 	u32 h264_p_min_qp;
237 	u32 h264_p_max_qp;
238 	u32 h264_b_min_qp;
239 	u32 h264_b_max_qp;
240 	u32 h264_loop_filter_mode;
241 	s32 h264_loop_filter_alpha;
242 	s32 h264_loop_filter_beta;
243 	u32 h264_8x8_transform;
244 
245 	u32 hevc_i_qp;
246 	u32 hevc_p_qp;
247 	u32 hevc_b_qp;
248 	u32 hevc_min_qp;
249 	u32 hevc_max_qp;
250 	u32 hevc_i_min_qp;
251 	u32 hevc_i_max_qp;
252 	u32 hevc_p_min_qp;
253 	u32 hevc_p_max_qp;
254 	u32 hevc_b_min_qp;
255 	u32 hevc_b_max_qp;
256 
257 	u32 vp8_min_qp;
258 	u32 vp8_max_qp;
259 
260 	u32 multi_slice_mode;
261 	u32 multi_slice_max_bytes;
262 	u32 multi_slice_max_mb;
263 
264 	u32 header_mode;
265 	bool aud_enable;
266 	u32 intra_refresh_type;
267 	u32 intra_refresh_period;
268 
269 	struct {
270 		u32 h264;
271 		u32 mpeg4;
272 		u32 hevc;
273 		u32 vp8;
274 		u32 vp9;
275 	} profile;
276 	struct {
277 		u32 h264;
278 		u32 mpeg4;
279 		u32 hevc;
280 		u32 vp9;
281 	} level;
282 
283 	u32 base_priority_id;
284 	u32 ltr_count;
285 	struct v4l2_ctrl_hdr10_cll_info cll;
286 	struct v4l2_ctrl_hdr10_mastering_display mastering;
287 };
288 
289 struct venus_buffer {
290 	struct vb2_v4l2_buffer vb;
291 	struct list_head list;
292 	dma_addr_t dma_addr;
293 	u32 size;
294 	struct list_head reg_list;
295 	u32 flags;
296 	struct list_head ref_list;
297 };
298 
299 struct clock_data {
300 	u32 core_id;
301 	unsigned long freq;
302 	unsigned long vpp_freq;
303 	unsigned long vsp_freq;
304 	unsigned long low_power_freq;
305 };
306 
307 #define to_venus_buffer(ptr)	container_of(ptr, struct venus_buffer, vb)
308 
309 enum venus_dec_state {
310 	VENUS_DEC_STATE_DEINIT		= 0,
311 	VENUS_DEC_STATE_INIT		= 1,
312 	VENUS_DEC_STATE_CAPTURE_SETUP	= 2,
313 	VENUS_DEC_STATE_STOPPED		= 3,
314 	VENUS_DEC_STATE_SEEK		= 4,
315 	VENUS_DEC_STATE_DRAIN		= 5,
316 	VENUS_DEC_STATE_DECODING	= 6,
317 	VENUS_DEC_STATE_DRC		= 7,
318 };
319 
320 struct venus_ts_metadata {
321 	bool used;
322 	u64 ts_ns;
323 	u64 ts_us;
324 	u32 flags;
325 	struct v4l2_timecode tc;
326 };
327 
328 enum venus_inst_modes {
329 	VENUS_LOW_POWER = BIT(0),
330 };
331 
332 /**
333  * struct venus_inst - holds per instance parameters
334  *
335  * @list:	used for attach an instance to the core
336  * @lock:	instance lock
337  * @core:	a reference to the core struct
338  * @clk_data:	clock data per core ID
339  * @dpbbufs:	a list of decoded picture buffers
340  * @internalbufs:	a list of internal bufferes
341  * @registeredbufs:	a list of registered capture bufferes
342  * @delayed_process:	a list of delayed buffers
343  * @delayed_process_work:	a work_struct for process delayed buffers
344  * @nonblock:		nonblocking flag
345  * @ctrl_handler:	v4l control handler
346  * @controls:	a union of decoder and encoder control parameters
347  * @fh:	 a holder of v4l file handle structure
348  * @streamon_cap: stream on flag for capture queue
349  * @streamon_out: stream on flag for output queue
350  * @width:	current capture width
351  * @height:	current capture height
352  * @crop:	current crop rectangle
353  * @fw_min_cnt:	 firmware minimum buffer count
354  * @out_width:	current output width
355  * @out_height:	current output height
356  * @colorspace:	current color space
357  * @ycbcr_enc:	current YCbCr encoding
358  * @quantization:	current quantization
359  * @xfer_func:	current xfer function
360  * @codec_state:	current codec API state (see DEC/ENC_STATE_)
361  * @reconf_wait:	wait queue for resolution change event
362  * @subscriptions:	used to hold current events subscriptions
363  * @buf_count:		used to count number of buffers (reqbuf(0))
364  * @tss:		timestamp metadata
365  * @payloads:		cache plane payload to use it for clock/BW scaling
366  * @fps:		holds current FPS
367  * @timeperframe:	holds current time per frame structure
368  * @fmt_out:	a reference to output format structure
369  * @fmt_cap:	a reference to capture format structure
370  * @num_input_bufs:	holds number of input buffers
371  * @num_output_bufs:	holds number of output buffers
372  * @input_buf_size:	holds input buffer size
373  * @output_buf_size:	holds output buffer size
374  * @output2_buf_size:	holds secondary decoder output buffer size
375  * @dpb_buftype:	decoded picture buffer type
376  * @dpb_fmt:		decoded picture buffer raw format
377  * @opb_buftype:	output picture buffer type
378  * @opb_fmt:		output picture buffer raw format
379  * @reconfig:	a flag raised by decoder when the stream resolution changed
380  * @hfi_codec:		current codec for this instance in HFI space
381  * @sequence_cap:	a sequence counter for capture queue
382  * @sequence_out:	a sequence counter for output queue
383  * @m2m_dev:	a reference to m2m device structure
384  * @m2m_ctx:	a reference to m2m context structure
385  * @state:	current state of the instance
386  * @done:	a completion for sync HFI operation
387  * @error:	an error returned during last HFI sync operation
388  * @session_error:	a flag rised by HFI interface in case of session error
389  * @ops:		HFI operations
390  * @priv:	a private for HFI operations callbacks
391  * @session_type:	the type of the session (decoder or encoder)
392  * @hprop:	a union used as a holder by get property
393  * @core_acquired:	the Core has been acquired
394  * @bit_depth:		current bitstream bit-depth
395  * @pic_struct:		bitstream progressive vs interlaced
396  * @next_buf_last: a flag to mark next queued capture buffer as last
397  * @drain_active:	Drain sequence is in progress
398  * @flags:	bitmask flags describing current instance mode
399  * @dpb_ids:	DPB buffer ID's
400  */
401 struct venus_inst {
402 	struct list_head list;
403 	struct mutex lock;
404 	struct venus_core *core;
405 	struct clock_data clk_data;
406 	struct list_head dpbbufs;
407 	struct list_head internalbufs;
408 	struct list_head registeredbufs;
409 	struct list_head delayed_process;
410 	struct work_struct delayed_process_work;
411 	bool nonblock;
412 
413 	struct v4l2_ctrl_handler ctrl_handler;
414 	union {
415 		struct vdec_controls dec;
416 		struct venc_controls enc;
417 	} controls;
418 	struct v4l2_fh fh;
419 	unsigned int streamon_cap, streamon_out;
420 	u32 width;
421 	u32 height;
422 	struct v4l2_rect crop;
423 	u32 fw_min_cnt;
424 	u32 out_width;
425 	u32 out_height;
426 	u32 colorspace;
427 	u8 ycbcr_enc;
428 	u8 quantization;
429 	u8 xfer_func;
430 	enum venus_dec_state codec_state;
431 	wait_queue_head_t reconf_wait;
432 	unsigned int subscriptions;
433 	int buf_count;
434 	struct venus_ts_metadata tss[VIDEO_MAX_FRAME];
435 	unsigned long payloads[VIDEO_MAX_FRAME];
436 	u64 fps;
437 	struct v4l2_fract timeperframe;
438 	const struct venus_format *fmt_out;
439 	const struct venus_format *fmt_cap;
440 	unsigned int num_input_bufs;
441 	unsigned int num_output_bufs;
442 	unsigned int input_buf_size;
443 	unsigned int output_buf_size;
444 	unsigned int output2_buf_size;
445 	u32 dpb_buftype;
446 	u32 dpb_fmt;
447 	u32 opb_buftype;
448 	u32 opb_fmt;
449 	bool reconfig;
450 	u32 hfi_codec;
451 	u32 sequence_cap;
452 	u32 sequence_out;
453 	struct v4l2_m2m_dev *m2m_dev;
454 	struct v4l2_m2m_ctx *m2m_ctx;
455 	unsigned int state;
456 	struct completion done;
457 	unsigned int error;
458 	bool session_error;
459 	const struct hfi_inst_ops *ops;
460 	u32 session_type;
461 	union hfi_get_property hprop;
462 	unsigned int core_acquired: 1;
463 	unsigned int bit_depth;
464 	unsigned int pic_struct;
465 	bool next_buf_last;
466 	bool drain_active;
467 	enum venus_inst_modes flags;
468 	struct ida dpb_ids;
469 };
470 
471 #define IS_V1(core)	((core)->res->hfi_version == HFI_VERSION_1XX)
472 #define IS_V3(core)	((core)->res->hfi_version == HFI_VERSION_3XX)
473 #define IS_V4(core)	((core)->res->hfi_version == HFI_VERSION_4XX)
474 #define IS_V6(core)	((core)->res->hfi_version == HFI_VERSION_6XX)
475 
476 #define ctrl_to_inst(ctrl)	\
477 	container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
478 
to_inst(struct file * filp)479 static inline struct venus_inst *to_inst(struct file *filp)
480 {
481 	return container_of(filp->private_data, struct venus_inst, fh);
482 }
483 
to_hfi_priv(struct venus_core * core)484 static inline void *to_hfi_priv(struct venus_core *core)
485 {
486 	return core->priv;
487 }
488 
489 static inline struct hfi_plat_caps *
venus_caps_by_codec(struct venus_core * core,u32 codec,u32 domain)490 venus_caps_by_codec(struct venus_core *core, u32 codec, u32 domain)
491 {
492 	unsigned int c;
493 
494 	for (c = 0; c < core->codecs_count; c++) {
495 		if (core->caps[c].codec == codec &&
496 		    core->caps[c].domain == domain)
497 			return &core->caps[c];
498 	}
499 
500 	return NULL;
501 }
502 
503 #endif
504