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