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 
18 #define VDBGL	"VenusLow : "
19 #define VDBGM	"VenusMed : "
20 #define VDBGH	"VenusHigh: "
21 #define VDBGFW	"VenusFW  : "
22 
23 #define VIDC_CLKS_NUM_MAX		4
24 #define VIDC_VCODEC_CLKS_NUM_MAX	2
25 #define VIDC_PMDOMAINS_NUM_MAX		3
26 
27 extern int venus_fw_debug;
28 
29 struct freq_tbl {
30 	unsigned int load;
31 	unsigned long freq;
32 };
33 
34 struct reg_val {
35 	u32 reg;
36 	u32 value;
37 };
38 
39 struct codec_freq_data {
40 	u32 pixfmt;
41 	u32 session_type;
42 	unsigned long vpp_freq;
43 	unsigned long vsp_freq;
44 };
45 
46 struct bw_tbl {
47 	u32 mbs_per_sec;
48 	u32 avg;
49 	u32 peak;
50 	u32 avg_10bit;
51 	u32 peak_10bit;
52 };
53 
54 struct venus_resources {
55 	u64 dma_mask;
56 	const struct freq_tbl *freq_tbl;
57 	unsigned int freq_tbl_size;
58 	const struct bw_tbl *bw_tbl_enc;
59 	unsigned int bw_tbl_enc_size;
60 	const struct bw_tbl *bw_tbl_dec;
61 	unsigned int bw_tbl_dec_size;
62 	const struct reg_val *reg_tbl;
63 	unsigned int reg_tbl_size;
64 	const struct codec_freq_data *codec_freq_data;
65 	unsigned int codec_freq_data_size;
66 	const char * const clks[VIDC_CLKS_NUM_MAX];
67 	unsigned int clks_num;
68 	const char * const vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
69 	const char * const vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
70 	unsigned int vcodec_clks_num;
71 	const char * const vcodec_pmdomains[VIDC_PMDOMAINS_NUM_MAX];
72 	unsigned int vcodec_pmdomains_num;
73 	const char **opp_pmdomain;
74 	unsigned int vcodec_num;
75 	enum hfi_version hfi_version;
76 	u32 max_load;
77 	unsigned int vmem_id;
78 	u32 vmem_size;
79 	u32 vmem_addr;
80 	u32 cp_start;
81 	u32 cp_size;
82 	u32 cp_nonpixel_start;
83 	u32 cp_nonpixel_size;
84 	const char *fwname;
85 };
86 
87 struct venus_format {
88 	u32 pixfmt;
89 	unsigned int num_planes;
90 	u32 type;
91 	u32 flags;
92 };
93 
94 #define MAX_PLANES		4
95 #define MAX_FMT_ENTRIES		32
96 #define MAX_CAP_ENTRIES		32
97 #define MAX_ALLOC_MODE_ENTRIES	16
98 #define MAX_CODEC_NUM		32
99 
100 struct raw_formats {
101 	u32 buftype;
102 	u32 fmt;
103 };
104 
105 struct venus_caps {
106 	u32 codec;
107 	u32 domain;
108 	bool cap_bufs_mode_dynamic;
109 	unsigned int num_caps;
110 	struct hfi_capability caps[MAX_CAP_ENTRIES];
111 	unsigned int num_pl;
112 	struct hfi_profile_level pl[HFI_MAX_PROFILE_COUNT];
113 	unsigned int num_fmts;
114 	struct raw_formats fmts[MAX_FMT_ENTRIES];
115 	bool valid;	/* used only for Venus v1xx */
116 };
117 
118 /**
119  * struct venus_core - holds core parameters valid for all instances
120  *
121  * @base:	IO memory base address
122  * @irq:		Venus irq
123  * @clks:	an array of struct clk pointers
124  * @vcodec0_clks: an array of vcodec0 struct clk pointers
125  * @vcodec1_clks: an array of vcodec1 struct clk pointers
126  * @pd_dl_venus: pmdomain device-link for venus domain
127  * @pmdomains:	an array of pmdomains struct device pointers
128  * @vdev_dec:	a reference to video device structure for decoder instances
129  * @vdev_enc:	a reference to video device structure for encoder instances
130  * @v4l2_dev:	a holder for v4l2 device structure
131  * @res:		a reference to venus resources structure
132  * @dev:		convenience struct device pointer
133  * @dev_dec:	convenience struct device pointer for decoder device
134  * @dev_enc:	convenience struct device pointer for encoder device
135  * @use_tz:	a flag that suggests presence of trustzone
136  * @lock:	a lock for this strucure
137  * @instances:	a list_head of all instances
138  * @insts_count:	num of instances
139  * @state:	the state of the venus core
140  * @done:	a completion for sync HFI operations
141  * @error:	an error returned during last HFI sync operations
142  * @sys_error:	an error flag that signal system error event
143  * @core_ops:	the core operations
144  * @pm_lock:	a lock for PM operations
145  * @enc_codecs:	encoders supported by this core
146  * @dec_codecs:	decoders supported by this core
147  * @max_sessions_supported:	holds the maximum number of sessions
148  * @core_caps:	core capabilities
149  * @priv:	a private filed for HFI operations
150  * @ops:		the core HFI operations
151  * @work:	a delayed work for handling system fatal error
152  * @root:	debugfs root directory
153  */
154 struct venus_core {
155 	void __iomem *base;
156 	int irq;
157 	struct clk *clks[VIDC_CLKS_NUM_MAX];
158 	struct clk *vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
159 	struct clk *vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
160 	struct icc_path *video_path;
161 	struct icc_path *cpucfg_path;
162 	struct opp_table *opp_table;
163 	bool has_opp_table;
164 	struct device_link *pd_dl_venus;
165 	struct device *pmdomains[VIDC_PMDOMAINS_NUM_MAX];
166 	struct device_link *opp_dl_venus;
167 	struct device *opp_pmdomain;
168 	struct video_device *vdev_dec;
169 	struct video_device *vdev_enc;
170 	struct v4l2_device v4l2_dev;
171 	const struct venus_resources *res;
172 	struct device *dev;
173 	struct device *dev_dec;
174 	struct device *dev_enc;
175 	unsigned int use_tz;
176 	struct video_firmware {
177 		struct device *dev;
178 		struct iommu_domain *iommu_domain;
179 		size_t mapped_mem_size;
180 	} fw;
181 	struct mutex lock;
182 	struct list_head instances;
183 	atomic_t insts_count;
184 	unsigned int state;
185 	struct completion done;
186 	unsigned int error;
187 	bool sys_error;
188 	const struct hfi_core_ops *core_ops;
189 	const struct venus_pm_ops *pm_ops;
190 	struct mutex pm_lock;
191 	unsigned long enc_codecs;
192 	unsigned long dec_codecs;
193 	unsigned int max_sessions_supported;
194 #define ENC_ROTATION_CAPABILITY		0x1
195 #define ENC_SCALING_CAPABILITY		0x2
196 #define ENC_DEINTERLACE_CAPABILITY	0x4
197 #define DEC_MULTI_STREAM_CAPABILITY	0x8
198 	unsigned int core_caps;
199 	void *priv;
200 	const struct hfi_ops *ops;
201 	struct delayed_work work;
202 	struct venus_caps caps[MAX_CODEC_NUM];
203 	unsigned int codecs_count;
204 	unsigned int core0_usage_count;
205 	unsigned int core1_usage_count;
206 	struct dentry *root;
207 };
208 
209 struct vdec_controls {
210 	u32 post_loop_deb_mode;
211 	u32 profile;
212 	u32 level;
213 };
214 
215 struct venc_controls {
216 	u16 gop_size;
217 	u32 num_p_frames;
218 	u32 num_b_frames;
219 	u32 bitrate_mode;
220 	u32 bitrate;
221 	u32 bitrate_peak;
222 	u32 rc_enable;
223 	u32 const_quality;
224 	u32 frame_skip_mode;
225 
226 	u32 h264_i_period;
227 	u32 h264_entropy_mode;
228 	u32 h264_i_qp;
229 	u32 h264_p_qp;
230 	u32 h264_b_qp;
231 	u32 h264_min_qp;
232 	u32 h264_max_qp;
233 	u32 h264_loop_filter_mode;
234 	s32 h264_loop_filter_alpha;
235 	s32 h264_loop_filter_beta;
236 
237 	u32 vp8_min_qp;
238 	u32 vp8_max_qp;
239 
240 	u32 multi_slice_mode;
241 	u32 multi_slice_max_bytes;
242 	u32 multi_slice_max_mb;
243 
244 	u32 header_mode;
245 
246 	struct {
247 		u32 h264;
248 		u32 mpeg4;
249 		u32 hevc;
250 		u32 vp8;
251 		u32 vp9;
252 	} profile;
253 	struct {
254 		u32 h264;
255 		u32 mpeg4;
256 		u32 hevc;
257 		u32 vp9;
258 	} level;
259 };
260 
261 struct venus_buffer {
262 	struct vb2_v4l2_buffer vb;
263 	struct list_head list;
264 	dma_addr_t dma_addr;
265 	u32 size;
266 	struct list_head reg_list;
267 	u32 flags;
268 	struct list_head ref_list;
269 };
270 
271 struct clock_data {
272 	u32 core_id;
273 	unsigned long freq;
274 	const struct codec_freq_data *codec_freq_data;
275 };
276 
277 #define to_venus_buffer(ptr)	container_of(ptr, struct venus_buffer, vb)
278 
279 enum venus_dec_state {
280 	VENUS_DEC_STATE_DEINIT		= 0,
281 	VENUS_DEC_STATE_INIT		= 1,
282 	VENUS_DEC_STATE_CAPTURE_SETUP	= 2,
283 	VENUS_DEC_STATE_STOPPED		= 3,
284 	VENUS_DEC_STATE_SEEK		= 4,
285 	VENUS_DEC_STATE_DRAIN		= 5,
286 	VENUS_DEC_STATE_DECODING	= 6,
287 	VENUS_DEC_STATE_DRC		= 7,
288 	VENUS_DEC_STATE_DRC_FLUSH_DONE	= 8,
289 };
290 
291 struct venus_ts_metadata {
292 	bool used;
293 	u64 ts_ns;
294 	u64 ts_us;
295 	u32 flags;
296 	struct v4l2_timecode tc;
297 };
298 
299 /**
300  * struct venus_inst - holds per instance parameters
301  *
302  * @list:	used for attach an instance to the core
303  * @lock:	instance lock
304  * @core:	a reference to the core struct
305  * @dpbbufs:	a list of decoded picture buffers
306  * @internalbufs:	a list of internal bufferes
307  * @registeredbufs:	a list of registered capture bufferes
308  * @delayed_process	a list of delayed buffers
309  * @delayed_process_work:	a work_struct for process delayed buffers
310  * @ctrl_handler:	v4l control handler
311  * @controls:	a union of decoder and encoder control parameters
312  * @fh:	 a holder of v4l file handle structure
313  * @streamon_cap: stream on flag for capture queue
314  * @streamon_out: stream on flag for output queue
315  * @width:	current capture width
316  * @height:	current capture height
317  * @out_width:	current output width
318  * @out_height:	current output height
319  * @colorspace:	current color space
320  * @quantization:	current quantization
321  * @xfer_func:	current xfer function
322  * @codec_state:	current codec API state (see DEC/ENC_STATE_)
323  * @reconf_wait:	wait queue for resolution change event
324  * @subscriptions:	used to hold current events subscriptions
325  * @buf_count:		used to count number of buffers (reqbuf(0))
326  * @fps:		holds current FPS
327  * @timeperframe:	holds current time per frame structure
328  * @fmt_out:	a reference to output format structure
329  * @fmt_cap:	a reference to capture format structure
330  * @num_input_bufs:	holds number of input buffers
331  * @num_output_bufs:	holds number of output buffers
332  * @input_buf_size	holds input buffer size
333  * @output_buf_size:	holds output buffer size
334  * @output2_buf_size:	holds secondary decoder output buffer size
335  * @dpb_buftype:	decoded picture buffer type
336  * @dpb_fmt:		decoded picture buffer raw format
337  * @opb_buftype:	output picture buffer type
338  * @opb_fmt:		output picture buffer raw format
339  * @reconfig:	a flag raised by decoder when the stream resolution changed
340  * @hfi_codec:		current codec for this instance in HFI space
341  * @sequence_cap:	a sequence counter for capture queue
342  * @sequence_out:	a sequence counter for output queue
343  * @m2m_dev:	a reference to m2m device structure
344  * @m2m_ctx:	a reference to m2m context structure
345  * @state:	current state of the instance
346  * @done:	a completion for sync HFI operation
347  * @error:	an error returned during last HFI sync operation
348  * @session_error:	a flag rised by HFI interface in case of session error
349  * @ops:		HFI operations
350  * @priv:	a private for HFI operations callbacks
351  * @session_type:	the type of the session (decoder or encoder)
352  * @hprop:	a union used as a holder by get property
353  * @last_buf:	last capture buffer for dynamic-resoluton-change
354  */
355 struct venus_inst {
356 	struct list_head list;
357 	struct mutex lock;
358 	struct venus_core *core;
359 	struct clock_data clk_data;
360 	struct list_head dpbbufs;
361 	struct list_head internalbufs;
362 	struct list_head registeredbufs;
363 	struct list_head delayed_process;
364 	struct work_struct delayed_process_work;
365 
366 	struct v4l2_ctrl_handler ctrl_handler;
367 	union {
368 		struct vdec_controls dec;
369 		struct venc_controls enc;
370 	} controls;
371 	struct v4l2_fh fh;
372 	unsigned int streamon_cap, streamon_out;
373 	u32 width;
374 	u32 height;
375 	struct v4l2_rect crop;
376 	u32 out_width;
377 	u32 out_height;
378 	u32 colorspace;
379 	u8 ycbcr_enc;
380 	u8 quantization;
381 	u8 xfer_func;
382 	enum venus_dec_state codec_state;
383 	wait_queue_head_t reconf_wait;
384 	unsigned int subscriptions;
385 	int buf_count;
386 	struct venus_ts_metadata tss[VIDEO_MAX_FRAME];
387 	unsigned long payloads[VIDEO_MAX_FRAME];
388 	u64 fps;
389 	struct v4l2_fract timeperframe;
390 	const struct venus_format *fmt_out;
391 	const struct venus_format *fmt_cap;
392 	unsigned int num_input_bufs;
393 	unsigned int num_output_bufs;
394 	unsigned int input_buf_size;
395 	unsigned int output_buf_size;
396 	unsigned int output2_buf_size;
397 	u32 dpb_buftype;
398 	u32 dpb_fmt;
399 	u32 opb_buftype;
400 	u32 opb_fmt;
401 	bool reconfig;
402 	u32 hfi_codec;
403 	u32 sequence_cap;
404 	u32 sequence_out;
405 	struct v4l2_m2m_dev *m2m_dev;
406 	struct v4l2_m2m_ctx *m2m_ctx;
407 	unsigned int state;
408 	struct completion done;
409 	unsigned int error;
410 	bool session_error;
411 	const struct hfi_inst_ops *ops;
412 	u32 session_type;
413 	union hfi_get_property hprop;
414 	unsigned int core_acquired: 1;
415 	unsigned int bit_depth;
416 	struct vb2_buffer *last_buf;
417 };
418 
419 #define IS_V1(core)	((core)->res->hfi_version == HFI_VERSION_1XX)
420 #define IS_V3(core)	((core)->res->hfi_version == HFI_VERSION_3XX)
421 #define IS_V4(core)	((core)->res->hfi_version == HFI_VERSION_4XX)
422 
423 #define ctrl_to_inst(ctrl)	\
424 	container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
425 
426 static inline struct venus_inst *to_inst(struct file *filp)
427 {
428 	return container_of(filp->private_data, struct venus_inst, fh);
429 }
430 
431 static inline void *to_hfi_priv(struct venus_core *core)
432 {
433 	return core->priv;
434 }
435 
436 static inline struct venus_caps *
437 venus_caps_by_codec(struct venus_core *core, u32 codec, u32 domain)
438 {
439 	unsigned int c;
440 
441 	for (c = 0; c < core->codecs_count; c++) {
442 		if (core->caps[c].codec == codec &&
443 		    core->caps[c].domain == domain)
444 			return &core->caps[c];
445 	}
446 
447 	return NULL;
448 }
449 
450 #endif
451