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