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