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 
238 	u32 hevc_i_qp;
239 	u32 hevc_p_qp;
240 	u32 hevc_b_qp;
241 	u32 hevc_min_qp;
242 	u32 hevc_max_qp;
243 	u32 hevc_i_min_qp;
244 	u32 hevc_i_max_qp;
245 	u32 hevc_p_min_qp;
246 	u32 hevc_p_max_qp;
247 	u32 hevc_b_min_qp;
248 	u32 hevc_b_max_qp;
249 
250 	u32 vp8_min_qp;
251 	u32 vp8_max_qp;
252 
253 	u32 multi_slice_mode;
254 	u32 multi_slice_max_bytes;
255 	u32 multi_slice_max_mb;
256 
257 	u32 header_mode;
258 	bool aud_enable;
259 
260 	struct {
261 		u32 h264;
262 		u32 mpeg4;
263 		u32 hevc;
264 		u32 vp8;
265 		u32 vp9;
266 	} profile;
267 	struct {
268 		u32 h264;
269 		u32 mpeg4;
270 		u32 hevc;
271 		u32 vp9;
272 	} level;
273 
274 	u32 base_priority_id;
275 	u32 ltr_count;
276 	struct v4l2_ctrl_hdr10_cll_info cll;
277 	struct v4l2_ctrl_hdr10_mastering_display mastering;
278 };
279 
280 struct venus_buffer {
281 	struct vb2_v4l2_buffer vb;
282 	struct list_head list;
283 	dma_addr_t dma_addr;
284 	u32 size;
285 	struct list_head reg_list;
286 	u32 flags;
287 	struct list_head ref_list;
288 };
289 
290 struct clock_data {
291 	u32 core_id;
292 	unsigned long freq;
293 	unsigned long vpp_freq;
294 	unsigned long vsp_freq;
295 	unsigned long low_power_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 enum venus_inst_modes {
320 	VENUS_LOW_POWER = BIT(0),
321 };
322 
323 /**
324  * struct venus_inst - holds per instance parameters
325  *
326  * @list:	used for attach an instance to the core
327  * @lock:	instance lock
328  * @core:	a reference to the core struct
329  * @clk_data:	clock data per core ID
330  * @dpbbufs:	a list of decoded picture buffers
331  * @internalbufs:	a list of internal bufferes
332  * @registeredbufs:	a list of registered capture bufferes
333  * @delayed_process:	a list of delayed buffers
334  * @delayed_process_work:	a work_struct for process delayed buffers
335  * @ctrl_handler:	v4l control handler
336  * @controls:	a union of decoder and encoder control parameters
337  * @fh:	 a holder of v4l file handle structure
338  * @streamon_cap: stream on flag for capture queue
339  * @streamon_out: stream on flag for output queue
340  * @width:	current capture width
341  * @height:	current capture height
342  * @crop:	current crop rectangle
343  * @out_width:	current output width
344  * @out_height:	current output height
345  * @colorspace:	current color space
346  * @ycbcr_enc:	current YCbCr encoding
347  * @quantization:	current quantization
348  * @xfer_func:	current xfer function
349  * @codec_state:	current codec API state (see DEC/ENC_STATE_)
350  * @reconf_wait:	wait queue for resolution change event
351  * @subscriptions:	used to hold current events subscriptions
352  * @buf_count:		used to count number of buffers (reqbuf(0))
353  * @tss:		timestamp metadata
354  * @payloads:		cache plane payload to use it for clock/BW scaling
355  * @fps:		holds current FPS
356  * @timeperframe:	holds current time per frame structure
357  * @fmt_out:	a reference to output format structure
358  * @fmt_cap:	a reference to capture format structure
359  * @num_input_bufs:	holds number of input buffers
360  * @num_output_bufs:	holds number of output buffers
361  * @input_buf_size:	holds input buffer size
362  * @output_buf_size:	holds output buffer size
363  * @output2_buf_size:	holds secondary decoder output buffer size
364  * @dpb_buftype:	decoded picture buffer type
365  * @dpb_fmt:		decoded picture buffer raw format
366  * @opb_buftype:	output picture buffer type
367  * @opb_fmt:		output picture buffer raw format
368  * @reconfig:	a flag raised by decoder when the stream resolution changed
369  * @hfi_codec:		current codec for this instance in HFI space
370  * @sequence_cap:	a sequence counter for capture queue
371  * @sequence_out:	a sequence counter for output queue
372  * @m2m_dev:	a reference to m2m device structure
373  * @m2m_ctx:	a reference to m2m context structure
374  * @state:	current state of the instance
375  * @done:	a completion for sync HFI operation
376  * @error:	an error returned during last HFI sync operation
377  * @session_error:	a flag rised by HFI interface in case of session error
378  * @ops:		HFI operations
379  * @priv:	a private for HFI operations callbacks
380  * @session_type:	the type of the session (decoder or encoder)
381  * @hprop:	a union used as a holder by get property
382  * @core_acquired:	the Core has been acquired
383  * @bit_depth:		current bitstream bit-depth
384  * @pic_struct:		bitstream progressive vs interlaced
385  * @next_buf_last: a flag to mark next queued capture buffer as last
386  * @drain_active:	Drain sequence is in progress
387  */
388 struct venus_inst {
389 	struct list_head list;
390 	struct mutex lock;
391 	struct venus_core *core;
392 	struct clock_data clk_data;
393 	struct list_head dpbbufs;
394 	struct list_head internalbufs;
395 	struct list_head registeredbufs;
396 	struct list_head delayed_process;
397 	struct work_struct delayed_process_work;
398 
399 	struct v4l2_ctrl_handler ctrl_handler;
400 	union {
401 		struct vdec_controls dec;
402 		struct venc_controls enc;
403 	} controls;
404 	struct v4l2_fh fh;
405 	unsigned int streamon_cap, streamon_out;
406 	u32 width;
407 	u32 height;
408 	struct v4l2_rect crop;
409 	u32 out_width;
410 	u32 out_height;
411 	u32 colorspace;
412 	u8 ycbcr_enc;
413 	u8 quantization;
414 	u8 xfer_func;
415 	enum venus_dec_state codec_state;
416 	wait_queue_head_t reconf_wait;
417 	unsigned int subscriptions;
418 	int buf_count;
419 	struct venus_ts_metadata tss[VIDEO_MAX_FRAME];
420 	unsigned long payloads[VIDEO_MAX_FRAME];
421 	u64 fps;
422 	struct v4l2_fract timeperframe;
423 	const struct venus_format *fmt_out;
424 	const struct venus_format *fmt_cap;
425 	unsigned int num_input_bufs;
426 	unsigned int num_output_bufs;
427 	unsigned int input_buf_size;
428 	unsigned int output_buf_size;
429 	unsigned int output2_buf_size;
430 	u32 dpb_buftype;
431 	u32 dpb_fmt;
432 	u32 opb_buftype;
433 	u32 opb_fmt;
434 	bool reconfig;
435 	u32 hfi_codec;
436 	u32 sequence_cap;
437 	u32 sequence_out;
438 	struct v4l2_m2m_dev *m2m_dev;
439 	struct v4l2_m2m_ctx *m2m_ctx;
440 	unsigned int state;
441 	struct completion done;
442 	unsigned int error;
443 	bool session_error;
444 	const struct hfi_inst_ops *ops;
445 	u32 session_type;
446 	union hfi_get_property hprop;
447 	unsigned int core_acquired: 1;
448 	unsigned int bit_depth;
449 	unsigned int pic_struct;
450 	bool next_buf_last;
451 	bool drain_active;
452 	enum venus_inst_modes flags;
453 };
454 
455 #define IS_V1(core)	((core)->res->hfi_version == HFI_VERSION_1XX)
456 #define IS_V3(core)	((core)->res->hfi_version == HFI_VERSION_3XX)
457 #define IS_V4(core)	((core)->res->hfi_version == HFI_VERSION_4XX)
458 #define IS_V6(core)	((core)->res->hfi_version == HFI_VERSION_6XX)
459 
460 #define ctrl_to_inst(ctrl)	\
461 	container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
462 
463 static inline struct venus_inst *to_inst(struct file *filp)
464 {
465 	return container_of(filp->private_data, struct venus_inst, fh);
466 }
467 
468 static inline void *to_hfi_priv(struct venus_core *core)
469 {
470 	return core->priv;
471 }
472 
473 static inline struct hfi_plat_caps *
474 venus_caps_by_codec(struct venus_core *core, u32 codec, u32 domain)
475 {
476 	unsigned int c;
477 
478 	for (c = 0; c < core->codecs_count; c++) {
479 		if (core->caps[c].codec == codec &&
480 		    core->caps[c].domain == domain)
481 			return &core->caps[c];
482 	}
483 
484 	return NULL;
485 }
486 
487 #endif
488