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