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