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