xref: /openbmc/linux/drivers/media/platform/qcom/venus/core.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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_PMDOMAINS_NUM_MAX		3
29  #define VIDC_RESETS_NUM_MAX		2
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 * const vcodec_pmdomains[VIDC_PMDOMAINS_NUM_MAX];
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  };
94  
95  enum venus_fmt {
96  	VENUS_FMT_NV12			= 0,
97  	VENUS_FMT_QC08C			= 1,
98  	VENUS_FMT_QC10C			= 2,
99  	VENUS_FMT_P010			= 3,
100  	VENUS_FMT_H264			= 4,
101  	VENUS_FMT_VP8			= 5,
102  	VENUS_FMT_VP9			= 6,
103  	VENUS_FMT_HEVC			= 7,
104  	VENUS_FMT_VC1_ANNEX_G		= 8,
105  	VENUS_FMT_VC1_ANNEX_L		= 9,
106  	VENUS_FMT_MPEG4			= 10,
107  	VENUS_FMT_MPEG2			= 11,
108  	VENUS_FMT_H263			= 12,
109  	VENUS_FMT_XVID			= 13,
110  };
111  
112  struct venus_format {
113  	u32 pixfmt;
114  	unsigned int num_planes;
115  	u32 type;
116  	u32 flags;
117  };
118  
119  /**
120   * struct venus_core - holds core parameters valid for all instances
121   *
122   * @base:	IO memory base address
123   * @vbif_base:	IO memory vbif base address
124   * @cpu_base:	IO memory cpu base address
125   * @cpu_cs_base:	IO memory cpu_cs base address
126   * @cpu_ic_base:	IO memory cpu_ic base address
127   * @wrapper_base:	IO memory wrapper base address
128   * @wrapper_tz_base:	IO memory wrapper TZ base address
129   * @aon_base:	AON base address
130   * @irq:		Venus irq
131   * @clks:	an array of struct clk pointers
132   * @vcodec0_clks: an array of vcodec0 struct clk pointers
133   * @vcodec1_clks: an array of vcodec1 struct clk pointers
134   * @video_path: an interconnect handle to video to/from memory path
135   * @cpucfg_path: an interconnect handle to cpu configuration path
136   * @has_opp_table: does OPP table exist
137   * @pmdomains:	an array of pmdomains struct device pointers
138   * @opp_dl_venus: an device-link for device OPP
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   */
174  struct venus_core {
175  	void __iomem *base;
176  	void __iomem *vbif_base;
177  	void __iomem *cpu_base;
178  	void __iomem *cpu_cs_base;
179  	void __iomem *cpu_ic_base;
180  	void __iomem *wrapper_base;
181  	void __iomem *wrapper_tz_base;
182  	void __iomem *aon_base;
183  	int irq;
184  	struct clk *clks[VIDC_CLKS_NUM_MAX];
185  	struct clk *vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
186  	struct clk *vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
187  	struct icc_path *video_path;
188  	struct icc_path *cpucfg_path;
189  	bool has_opp_table;
190  	struct device *pmdomains[VIDC_PMDOMAINS_NUM_MAX];
191  	struct device_link *opp_dl_venus;
192  	struct device *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  };
237  
238  struct vdec_controls {
239  	u32 post_loop_deb_mode;
240  	u32 profile;
241  	u32 level;
242  	u32 display_delay;
243  	u32 display_delay_enable;
244  	u64 conceal_color;
245  };
246  
247  struct venc_controls {
248  	u16 gop_size;
249  	u32 num_p_frames;
250  	u32 num_b_frames;
251  	u32 bitrate_mode;
252  	u32 bitrate;
253  	u32 bitrate_peak;
254  	u32 rc_enable;
255  	u32 const_quality;
256  	u32 frame_skip_mode;
257  
258  	u32 h264_i_period;
259  	u32 h264_entropy_mode;
260  	u32 h264_i_qp;
261  	u32 h264_p_qp;
262  	u32 h264_b_qp;
263  	u32 h264_min_qp;
264  	u32 h264_max_qp;
265  	u32 h264_i_min_qp;
266  	u32 h264_i_max_qp;
267  	u32 h264_p_min_qp;
268  	u32 h264_p_max_qp;
269  	u32 h264_b_min_qp;
270  	u32 h264_b_max_qp;
271  	u32 h264_loop_filter_mode;
272  	s32 h264_loop_filter_alpha;
273  	s32 h264_loop_filter_beta;
274  	u32 h264_8x8_transform;
275  
276  	u32 hevc_i_qp;
277  	u32 hevc_p_qp;
278  	u32 hevc_b_qp;
279  	u32 hevc_min_qp;
280  	u32 hevc_max_qp;
281  	u32 hevc_i_min_qp;
282  	u32 hevc_i_max_qp;
283  	u32 hevc_p_min_qp;
284  	u32 hevc_p_max_qp;
285  	u32 hevc_b_min_qp;
286  	u32 hevc_b_max_qp;
287  
288  	u32 vp8_min_qp;
289  	u32 vp8_max_qp;
290  
291  	u32 multi_slice_mode;
292  	u32 multi_slice_max_bytes;
293  	u32 multi_slice_max_mb;
294  
295  	u32 header_mode;
296  	bool aud_enable;
297  	u32 intra_refresh_type;
298  	u32 intra_refresh_period;
299  
300  	struct {
301  		u32 h264;
302  		u32 mpeg4;
303  		u32 hevc;
304  		u32 vp8;
305  		u32 vp9;
306  	} profile;
307  	struct {
308  		u32 h264;
309  		u32 mpeg4;
310  		u32 hevc;
311  		u32 vp9;
312  	} level;
313  
314  	u32 base_priority_id;
315  	u32 ltr_count;
316  	struct v4l2_ctrl_hdr10_cll_info cll;
317  	struct v4l2_ctrl_hdr10_mastering_display mastering;
318  };
319  
320  struct venus_buffer {
321  	struct vb2_v4l2_buffer vb;
322  	struct list_head list;
323  	dma_addr_t dma_addr;
324  	u32 size;
325  	struct list_head reg_list;
326  	u32 flags;
327  	struct list_head ref_list;
328  };
329  
330  struct clock_data {
331  	u32 core_id;
332  	unsigned long freq;
333  	unsigned long vpp_freq;
334  	unsigned long vsp_freq;
335  	unsigned long low_power_freq;
336  };
337  
338  #define to_venus_buffer(ptr)	container_of(ptr, struct venus_buffer, vb)
339  
340  enum venus_dec_state {
341  	VENUS_DEC_STATE_DEINIT		= 0,
342  	VENUS_DEC_STATE_INIT		= 1,
343  	VENUS_DEC_STATE_CAPTURE_SETUP	= 2,
344  	VENUS_DEC_STATE_STOPPED		= 3,
345  	VENUS_DEC_STATE_SEEK		= 4,
346  	VENUS_DEC_STATE_DRAIN		= 5,
347  	VENUS_DEC_STATE_DECODING	= 6,
348  	VENUS_DEC_STATE_DRC		= 7,
349  };
350  
351  enum venus_enc_state {
352  	VENUS_ENC_STATE_DEINIT		= 0,
353  	VENUS_ENC_STATE_INIT		= 1,
354  	VENUS_ENC_STATE_ENCODING	= 2,
355  	VENUS_ENC_STATE_STOPPED		= 3,
356  	VENUS_ENC_STATE_DRAIN		= 4,
357  };
358  
359  struct venus_ts_metadata {
360  	bool used;
361  	u64 ts_ns;
362  	u64 ts_us;
363  	u32 flags;
364  	struct v4l2_timecode tc;
365  };
366  
367  enum venus_inst_modes {
368  	VENUS_LOW_POWER = BIT(0),
369  };
370  
371  /**
372   * struct venus_inst - holds per instance parameters
373   *
374   * @list:	used for attach an instance to the core
375   * @lock:	instance lock
376   * @core:	a reference to the core struct
377   * @clk_data:	clock data per core ID
378   * @dpbbufs:	a list of decoded picture buffers
379   * @internalbufs:	a list of internal bufferes
380   * @registeredbufs:	a list of registered capture bufferes
381   * @delayed_process:	a list of delayed buffers
382   * @delayed_process_work:	a work_struct for process delayed buffers
383   * @nonblock:		nonblocking flag
384   * @ctrl_handler:	v4l control handler
385   * @controls:	a union of decoder and encoder control parameters
386   * @fh:	 a holder of v4l file handle structure
387   * @streamon_cap: stream on flag for capture queue
388   * @streamon_out: stream on flag for output queue
389   * @width:	current capture width
390   * @height:	current capture height
391   * @crop:	current crop rectangle
392   * @fw_min_cnt:	 firmware minimum buffer count
393   * @out_width:	current output width
394   * @out_height:	current output height
395   * @colorspace:	current color space
396   * @ycbcr_enc:	current YCbCr encoding
397   * @quantization:	current quantization
398   * @xfer_func:	current xfer function
399   * @codec_state:	current decoder API state (see DEC_STATE_)
400   * @enc_state:		current encoder API state (see ENC_STATE_)
401   * @reconf_wait:	wait queue for resolution change event
402   * @subscriptions:	used to hold current events subscriptions
403   * @buf_count:		used to count number of buffers (reqbuf(0))
404   * @tss:		timestamp metadata
405   * @payloads:		cache plane payload to use it for clock/BW scaling
406   * @fps:		holds current FPS
407   * @timeperframe:	holds current time per frame structure
408   * @fmt_out:	a reference to output format structure
409   * @fmt_cap:	a reference to capture format structure
410   * @num_input_bufs:	holds number of input buffers
411   * @num_output_bufs:	holds number of output buffers
412   * @input_buf_size:	holds input buffer size
413   * @output_buf_size:	holds output buffer size
414   * @output2_buf_size:	holds secondary decoder output buffer size
415   * @dpb_buftype:	decoded picture buffer type
416   * @dpb_fmt:		decoded picture buffer raw format
417   * @opb_buftype:	output picture buffer type
418   * @opb_fmt:		output picture buffer raw format
419   * @reconfig:	a flag raised by decoder when the stream resolution changed
420   * @hfi_codec:		current codec for this instance in HFI space
421   * @sequence_cap:	a sequence counter for capture queue
422   * @sequence_out:	a sequence counter for output queue
423   * @m2m_dev:	a reference to m2m device structure
424   * @m2m_ctx:	a reference to m2m context structure
425   * @ctx_q_lock:	a lock to serialize video device ioctl calls
426   * @state:	current state of the instance
427   * @done:	a completion for sync HFI operation
428   * @error:	an error returned during last HFI sync operation
429   * @session_error:	a flag rised by HFI interface in case of session error
430   * @ops:		HFI operations
431   * @priv:	a private for HFI operations callbacks
432   * @session_type:	the type of the session (decoder or encoder)
433   * @hprop:	a union used as a holder by get property
434   * @core_acquired:	the Core has been acquired
435   * @bit_depth:		current bitstream bit-depth
436   * @pic_struct:		bitstream progressive vs interlaced
437   * @next_buf_last: a flag to mark next queued capture buffer as last
438   * @drain_active:	Drain sequence is in progress
439   * @flags:	bitmask flags describing current instance mode
440   * @dpb_ids:	DPB buffer ID's
441   */
442  struct venus_inst {
443  	struct list_head list;
444  	struct mutex lock;
445  	struct venus_core *core;
446  	struct clock_data clk_data;
447  	struct list_head dpbbufs;
448  	struct list_head internalbufs;
449  	struct list_head registeredbufs;
450  	struct list_head delayed_process;
451  	struct work_struct delayed_process_work;
452  	bool nonblock;
453  
454  	struct v4l2_ctrl_handler ctrl_handler;
455  	union {
456  		struct vdec_controls dec;
457  		struct venc_controls enc;
458  	} controls;
459  	struct v4l2_fh fh;
460  	unsigned int streamon_cap, streamon_out;
461  	u32 width;
462  	u32 height;
463  	struct v4l2_rect crop;
464  	u32 fw_min_cnt;
465  	u32 out_width;
466  	u32 out_height;
467  	u32 colorspace;
468  	u8 ycbcr_enc;
469  	u8 quantization;
470  	u8 xfer_func;
471  	enum venus_dec_state codec_state;
472  	enum venus_enc_state enc_state;
473  	wait_queue_head_t reconf_wait;
474  	unsigned int subscriptions;
475  	int buf_count;
476  	struct venus_ts_metadata tss[VIDEO_MAX_FRAME];
477  	unsigned long payloads[VIDEO_MAX_FRAME];
478  	u64 fps;
479  	struct v4l2_fract timeperframe;
480  	const struct venus_format *fmt_out;
481  	const struct venus_format *fmt_cap;
482  	unsigned int num_input_bufs;
483  	unsigned int num_output_bufs;
484  	unsigned int input_buf_size;
485  	unsigned int output_buf_size;
486  	unsigned int output2_buf_size;
487  	u32 dpb_buftype;
488  	u32 dpb_fmt;
489  	u32 opb_buftype;
490  	u32 opb_fmt;
491  	bool reconfig;
492  	u32 hfi_codec;
493  	u32 sequence_cap;
494  	u32 sequence_out;
495  	struct v4l2_m2m_dev *m2m_dev;
496  	struct v4l2_m2m_ctx *m2m_ctx;
497  	struct mutex ctx_q_lock;
498  	unsigned int state;
499  	struct completion done;
500  	unsigned int error;
501  	bool session_error;
502  	const struct hfi_inst_ops *ops;
503  	u32 session_type;
504  	union hfi_get_property hprop;
505  	unsigned int core_acquired: 1;
506  	unsigned int bit_depth;
507  	unsigned int pic_struct;
508  	bool next_buf_last;
509  	bool drain_active;
510  	enum venus_inst_modes flags;
511  	struct ida dpb_ids;
512  };
513  
514  #define IS_V1(core)	((core)->res->hfi_version == HFI_VERSION_1XX)
515  #define IS_V3(core)	((core)->res->hfi_version == HFI_VERSION_3XX)
516  #define IS_V4(core)	((core)->res->hfi_version == HFI_VERSION_4XX)
517  #define IS_V6(core)	((core)->res->hfi_version == HFI_VERSION_6XX)
518  
519  #define IS_AR50(core)		((core)->res->vpu_version == VPU_VERSION_AR50)
520  #define IS_AR50_LITE(core)	((core)->res->vpu_version == VPU_VERSION_AR50_LITE)
521  #define IS_IRIS1(core)		((core)->res->vpu_version == VPU_VERSION_IRIS1)
522  #define IS_IRIS2(core)		((core)->res->vpu_version == VPU_VERSION_IRIS2)
523  #define IS_IRIS2_1(core)	((core)->res->vpu_version == VPU_VERSION_IRIS2_1)
524  
525  #define ctrl_to_inst(ctrl)	\
526  	container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
527  
to_inst(struct file * filp)528  static inline struct venus_inst *to_inst(struct file *filp)
529  {
530  	return container_of(filp->private_data, struct venus_inst, fh);
531  }
532  
to_hfi_priv(struct venus_core * core)533  static inline void *to_hfi_priv(struct venus_core *core)
534  {
535  	return core->priv;
536  }
537  
538  static inline struct hfi_plat_caps *
venus_caps_by_codec(struct venus_core * core,u32 codec,u32 domain)539  venus_caps_by_codec(struct venus_core *core, u32 codec, u32 domain)
540  {
541  	unsigned int c;
542  
543  	for (c = 0; c < core->codecs_count; c++) {
544  		if (core->caps[c].codec == codec &&
545  		    core->caps[c].domain == domain)
546  			return &core->caps[c];
547  	}
548  
549  	return NULL;
550  }
551  
552  static inline bool
is_fw_rev_or_newer(struct venus_core * core,u32 vmajor,u32 vminor,u32 vrev)553  is_fw_rev_or_newer(struct venus_core *core, u32 vmajor, u32 vminor, u32 vrev)
554  {
555  	return ((core)->venus_ver.major == vmajor &&
556  		(core)->venus_ver.minor == vminor &&
557  		(core)->venus_ver.rev >= vrev);
558  }
559  
560  static inline bool
is_fw_rev_or_older(struct venus_core * core,u32 vmajor,u32 vminor,u32 vrev)561  is_fw_rev_or_older(struct venus_core *core, u32 vmajor, u32 vminor, u32 vrev)
562  {
563  	return ((core)->venus_ver.major == vmajor &&
564  		(core)->venus_ver.minor == vminor &&
565  		(core)->venus_ver.rev <= vrev);
566  }
567  #endif
568