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