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