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