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