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