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