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