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/bitops.h>
11 #include <linux/list.h>
12 #include <media/videobuf2-v4l2.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15
16 #include "dbgfs.h"
17 #include "hfi.h"
18 #include "hfi_platform.h"
19 #include "hfi_helper.h"
20
21 #define VDBGL "VenusLow : "
22 #define VDBGM "VenusMed : "
23 #define VDBGH "VenusHigh: "
24 #define VDBGFW "VenusFW : "
25
26 #define VIDC_CLKS_NUM_MAX 4
27 #define VIDC_VCODEC_CLKS_NUM_MAX 2
28 #define VIDC_RESETS_NUM_MAX 2
29
30 #define VENUS_MAX_FPS 240
31
32 extern int venus_fw_debug;
33
34 struct freq_tbl {
35 unsigned int load;
36 unsigned long freq;
37 };
38
39 struct reg_val {
40 u32 reg;
41 u32 value;
42 };
43
44 struct bw_tbl {
45 u32 mbs_per_sec;
46 u32 avg;
47 u32 peak;
48 u32 avg_10bit;
49 u32 peak_10bit;
50 };
51
52 enum vpu_version {
53 VPU_VERSION_AR50,
54 VPU_VERSION_AR50_LITE,
55 VPU_VERSION_IRIS1,
56 VPU_VERSION_IRIS2,
57 VPU_VERSION_IRIS2_1,
58 };
59
60 struct venus_resources {
61 u64 dma_mask;
62 const struct freq_tbl *freq_tbl;
63 unsigned int freq_tbl_size;
64 const struct bw_tbl *bw_tbl_enc;
65 unsigned int bw_tbl_enc_size;
66 const struct bw_tbl *bw_tbl_dec;
67 unsigned int bw_tbl_dec_size;
68 const struct reg_val *reg_tbl;
69 unsigned int reg_tbl_size;
70 const struct hfi_ubwc_config *ubwc_conf;
71 const char * const clks[VIDC_CLKS_NUM_MAX];
72 unsigned int clks_num;
73 const char * const vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
74 const char * const vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
75 unsigned int vcodec_clks_num;
76 const char **vcodec_pmdomains;
77 unsigned int vcodec_pmdomains_num;
78 const char **opp_pmdomain;
79 unsigned int vcodec_num;
80 const char * const resets[VIDC_RESETS_NUM_MAX];
81 unsigned int resets_num;
82 enum hfi_version hfi_version;
83 enum vpu_version vpu_version;
84 u8 num_vpp_pipes;
85 u32 max_load;
86 unsigned int vmem_id;
87 u32 vmem_size;
88 u32 vmem_addr;
89 u32 cp_start;
90 u32 cp_size;
91 u32 cp_nonpixel_start;
92 u32 cp_nonpixel_size;
93 const char *fwname;
94 };
95
96 enum venus_fmt {
97 VENUS_FMT_NV12 = 0,
98 VENUS_FMT_QC08C = 1,
99 VENUS_FMT_QC10C = 2,
100 VENUS_FMT_P010 = 3,
101 VENUS_FMT_H264 = 4,
102 VENUS_FMT_VP8 = 5,
103 VENUS_FMT_VP9 = 6,
104 VENUS_FMT_HEVC = 7,
105 VENUS_FMT_VC1_ANNEX_G = 8,
106 VENUS_FMT_VC1_ANNEX_L = 9,
107 VENUS_FMT_MPEG4 = 10,
108 VENUS_FMT_MPEG2 = 11,
109 VENUS_FMT_H263 = 12,
110 VENUS_FMT_XVID = 13,
111 };
112
113 struct venus_format {
114 u32 pixfmt;
115 unsigned int num_planes;
116 u32 type;
117 u32 flags;
118 };
119
120 /**
121 * struct venus_core - holds core parameters valid for all instances
122 *
123 * @base: IO memory base address
124 * @vbif_base: IO memory vbif base address
125 * @cpu_base: IO memory cpu base address
126 * @cpu_cs_base: IO memory cpu_cs base address
127 * @cpu_ic_base: IO memory cpu_ic base address
128 * @wrapper_base: IO memory wrapper base address
129 * @wrapper_tz_base: IO memory wrapper TZ base address
130 * @aon_base: AON base address
131 * @irq: Venus irq
132 * @clks: an array of struct clk pointers
133 * @vcodec0_clks: an array of vcodec0 struct clk pointers
134 * @vcodec1_clks: an array of vcodec1 struct clk pointers
135 * @video_path: an interconnect handle to video to/from memory path
136 * @cpucfg_path: an interconnect handle to cpu configuration path
137 * @has_opp_table: does OPP table exist
138 * @pmdomains: a pointer to a list of pmdomains
139 * @opp_dl_venus: an device-link for device OPP
140 * @opp_pmdomain: an OPP power-domain
141 * @resets: an array of reset signals
142 * @vdev_dec: a reference to video device structure for decoder instances
143 * @vdev_enc: a reference to video device structure for encoder instances
144 * @v4l2_dev: a holder for v4l2 device structure
145 * @res: a reference to venus resources structure
146 * @dev: convenience struct device pointer
147 * @dev_dec: convenience struct device pointer for decoder device
148 * @dev_enc: convenience struct device pointer for encoder device
149 * @use_tz: a flag that suggests presence of trustzone
150 * @fw: structure of firmware parameters
151 * @lock: a lock for this strucure
152 * @instances: a list_head of all instances
153 * @insts_count: num of instances
154 * @state: the state of the venus core
155 * @done: a completion for sync HFI operations
156 * @error: an error returned during last HFI sync operations
157 * @sys_error: an error flag that signal system error event
158 * @sys_err_done: a waitqueue to wait for system error recovery end
159 * @core_ops: the core operations
160 * @pm_ops: a pointer to pm operations
161 * @pm_lock: a lock for PM operations
162 * @enc_codecs: encoders supported by this core
163 * @dec_codecs: decoders supported by this core
164 * @max_sessions_supported: holds the maximum number of sessions
165 * @priv: a private filed for HFI operations
166 * @ops: the core HFI operations
167 * @work: a delayed work for handling system fatal error
168 * @caps: an array of supported HFI capabilities
169 * @codecs_count: platform codecs count
170 * @core0_usage_count: usage counter for core0
171 * @core1_usage_count: usage counter for core1
172 * @root: debugfs root directory
173 * @venus_ver: the venus firmware version
174 * @dump_core: a flag indicating that a core dump is required
175 */
176 struct venus_core {
177 void __iomem *base;
178 void __iomem *vbif_base;
179 void __iomem *cpu_base;
180 void __iomem *cpu_cs_base;
181 void __iomem *cpu_ic_base;
182 void __iomem *wrapper_base;
183 void __iomem *wrapper_tz_base;
184 void __iomem *aon_base;
185 int irq;
186 struct clk *clks[VIDC_CLKS_NUM_MAX];
187 struct clk *vcodec0_clks[VIDC_VCODEC_CLKS_NUM_MAX];
188 struct clk *vcodec1_clks[VIDC_VCODEC_CLKS_NUM_MAX];
189 struct icc_path *video_path;
190 struct icc_path *cpucfg_path;
191 bool has_opp_table;
192 struct dev_pm_domain_list *pmdomains;
193 struct device_link *opp_dl_venus;
194 struct device *opp_pmdomain;
195 struct reset_control *resets[VIDC_RESETS_NUM_MAX];
196 struct video_device *vdev_dec;
197 struct video_device *vdev_enc;
198 struct v4l2_device v4l2_dev;
199 const struct venus_resources *res;
200 struct device *dev;
201 struct device *dev_dec;
202 struct device *dev_enc;
203 unsigned int use_tz;
204 struct video_firmware {
205 struct device *dev;
206 struct iommu_domain *iommu_domain;
207 size_t mapped_mem_size;
208 phys_addr_t mem_phys;
209 size_t mem_size;
210 } fw;
211 struct mutex lock;
212 struct list_head instances;
213 atomic_t insts_count;
214 unsigned int state;
215 struct completion done;
216 unsigned int error;
217 unsigned long sys_error;
218 wait_queue_head_t sys_err_done;
219 const struct hfi_core_ops *core_ops;
220 const struct venus_pm_ops *pm_ops;
221 struct mutex pm_lock;
222 unsigned long enc_codecs;
223 unsigned long dec_codecs;
224 unsigned int max_sessions_supported;
225 void *priv;
226 const struct hfi_ops *ops;
227 struct delayed_work work;
228 struct hfi_plat_caps caps[MAX_CODEC_NUM];
229 unsigned int codecs_count;
230 unsigned int core0_usage_count;
231 unsigned int core1_usage_count;
232 struct dentry *root;
233 struct venus_img_version {
234 u32 major;
235 u32 minor;
236 u32 rev;
237 } venus_ver;
238 unsigned long dump_core;
239 };
240
241 struct vdec_controls {
242 u32 post_loop_deb_mode;
243 u32 profile;
244 u32 level;
245 u32 display_delay;
246 u32 display_delay_enable;
247 u64 conceal_color;
248 };
249
250 struct venc_controls {
251 u16 gop_size;
252 u32 num_p_frames;
253 u32 num_b_frames;
254 u32 bitrate_mode;
255 u32 bitrate;
256 u32 bitrate_peak;
257 u32 rc_enable;
258 u32 const_quality;
259 u32 frame_skip_mode;
260
261 u32 h264_i_period;
262 u32 h264_entropy_mode;
263 u32 h264_i_qp;
264 u32 h264_p_qp;
265 u32 h264_b_qp;
266 u32 h264_min_qp;
267 u32 h264_max_qp;
268 u32 h264_i_min_qp;
269 u32 h264_i_max_qp;
270 u32 h264_p_min_qp;
271 u32 h264_p_max_qp;
272 u32 h264_b_min_qp;
273 u32 h264_b_max_qp;
274 u32 h264_loop_filter_mode;
275 s32 h264_loop_filter_alpha;
276 s32 h264_loop_filter_beta;
277 u32 h264_8x8_transform;
278
279 u32 hevc_i_qp;
280 u32 hevc_p_qp;
281 u32 hevc_b_qp;
282 u32 hevc_min_qp;
283 u32 hevc_max_qp;
284 u32 hevc_i_min_qp;
285 u32 hevc_i_max_qp;
286 u32 hevc_p_min_qp;
287 u32 hevc_p_max_qp;
288 u32 hevc_b_min_qp;
289 u32 hevc_b_max_qp;
290
291 u32 vp8_min_qp;
292 u32 vp8_max_qp;
293
294 u32 multi_slice_mode;
295 u32 multi_slice_max_bytes;
296 u32 multi_slice_max_mb;
297
298 u32 header_mode;
299 bool aud_enable;
300 u32 intra_refresh_type;
301 u32 intra_refresh_period;
302
303 struct {
304 u32 h264;
305 u32 mpeg4;
306 u32 hevc;
307 u32 vp8;
308 u32 vp9;
309 } profile;
310 struct {
311 u32 h264;
312 u32 mpeg4;
313 u32 hevc;
314 u32 vp9;
315 } level;
316
317 u32 base_priority_id;
318 u32 ltr_count;
319 struct v4l2_ctrl_hdr10_cll_info cll;
320 struct v4l2_ctrl_hdr10_mastering_display mastering;
321 };
322
323 struct venus_buffer {
324 struct vb2_v4l2_buffer vb;
325 struct list_head list;
326 dma_addr_t dma_addr;
327 u32 size;
328 struct list_head reg_list;
329 u32 flags;
330 struct list_head ref_list;
331 };
332
333 struct clock_data {
334 u32 core_id;
335 unsigned long freq;
336 unsigned long vpp_freq;
337 unsigned long vsp_freq;
338 unsigned long low_power_freq;
339 };
340
341 #define to_venus_buffer(ptr) container_of(ptr, struct venus_buffer, vb)
342
343 enum venus_dec_state {
344 VENUS_DEC_STATE_DEINIT = 0,
345 VENUS_DEC_STATE_INIT = 1,
346 VENUS_DEC_STATE_CAPTURE_SETUP = 2,
347 VENUS_DEC_STATE_STOPPED = 3,
348 VENUS_DEC_STATE_SEEK = 4,
349 VENUS_DEC_STATE_DRAIN = 5,
350 VENUS_DEC_STATE_DECODING = 6,
351 VENUS_DEC_STATE_DRC = 7,
352 };
353
354 enum venus_enc_state {
355 VENUS_ENC_STATE_DEINIT = 0,
356 VENUS_ENC_STATE_INIT = 1,
357 VENUS_ENC_STATE_ENCODING = 2,
358 VENUS_ENC_STATE_STOPPED = 3,
359 VENUS_ENC_STATE_DRAIN = 4,
360 };
361
362 struct venus_ts_metadata {
363 bool used;
364 u64 ts_ns;
365 u64 ts_us;
366 u32 flags;
367 struct v4l2_timecode tc;
368 };
369
370 enum venus_inst_modes {
371 VENUS_LOW_POWER = BIT(0),
372 };
373
374 /**
375 * struct venus_inst - holds per instance parameters
376 *
377 * @list: used for attach an instance to the core
378 * @lock: instance lock
379 * @core: a reference to the core struct
380 * @clk_data: clock data per core ID
381 * @dpbbufs: a list of decoded picture buffers
382 * @internalbufs: a list of internal bufferes
383 * @registeredbufs: a list of registered capture bufferes
384 * @delayed_process: a list of delayed buffers
385 * @delayed_process_work: a work_struct for process delayed buffers
386 * @nonblock: nonblocking flag
387 * @ctrl_handler: v4l control handler
388 * @controls: a union of decoder and encoder control parameters
389 * @fh: a holder of v4l file handle structure
390 * @streamon_cap: stream on flag for capture queue
391 * @streamon_out: stream on flag for output queue
392 * @width: current capture width
393 * @height: current capture height
394 * @crop: current crop rectangle
395 * @fw_min_cnt: firmware minimum buffer count
396 * @out_width: current output width
397 * @out_height: current output height
398 * @colorspace: current color space
399 * @ycbcr_enc: current YCbCr encoding
400 * @quantization: current quantization
401 * @xfer_func: current xfer function
402 * @codec_state: current decoder API state (see DEC_STATE_)
403 * @enc_state: current encoder API state (see ENC_STATE_)
404 * @reconf_wait: wait queue for resolution change event
405 * @subscriptions: used to hold current events subscriptions
406 * @buf_count: used to count number of buffers (reqbuf(0))
407 * @tss: timestamp metadata
408 * @payloads: cache plane payload to use it for clock/BW scaling
409 * @fps: holds current FPS
410 * @timeperframe: holds current time per frame structure
411 * @fmt_out: a reference to output format structure
412 * @fmt_cap: a reference to capture format structure
413 * @num_input_bufs: holds number of input buffers
414 * @num_output_bufs: holds number of output buffers
415 * @input_buf_size: holds input buffer size
416 * @output_buf_size: holds output buffer size
417 * @output2_buf_size: holds secondary decoder output buffer size
418 * @dpb_buftype: decoded picture buffer type
419 * @dpb_fmt: decoded picture buffer raw format
420 * @opb_buftype: output picture buffer type
421 * @opb_fmt: output picture buffer raw format
422 * @reconfig: a flag raised by decoder when the stream resolution changed
423 * @hfi_codec: current codec for this instance in HFI space
424 * @sequence_cap: a sequence counter for capture queue
425 * @sequence_out: a sequence counter for output queue
426 * @m2m_dev: a reference to m2m device structure
427 * @m2m_ctx: a reference to m2m context structure
428 * @ctx_q_lock: a lock to serialize video device ioctl calls
429 * @state: current state of the instance
430 * @done: a completion for sync HFI operation
431 * @error: an error returned during last HFI sync operation
432 * @session_error: a flag rised by HFI interface in case of session error
433 * @ops: HFI operations
434 * @session_type: the type of the session (decoder or encoder)
435 * @hprop: a union used as a holder by get property
436 * @core_acquired: the Core has been acquired
437 * @bit_depth: current bitstream bit-depth
438 * @pic_struct: bitstream progressive vs interlaced
439 * @next_buf_last: a flag to mark next queued capture buffer as last
440 * @drain_active: Drain sequence is in progress
441 * @flags: bitmask flags describing current instance mode
442 * @dpb_ids: DPB buffer ID's
443 */
444 struct venus_inst {
445 struct list_head list;
446 struct mutex lock;
447 struct venus_core *core;
448 struct clock_data clk_data;
449 struct list_head dpbbufs;
450 struct list_head internalbufs;
451 struct list_head registeredbufs;
452 struct list_head delayed_process;
453 struct work_struct delayed_process_work;
454 bool nonblock;
455
456 struct v4l2_ctrl_handler ctrl_handler;
457 union {
458 struct vdec_controls dec;
459 struct venc_controls enc;
460 } controls;
461 struct v4l2_fh fh;
462 unsigned int streamon_cap, streamon_out;
463 u32 width;
464 u32 height;
465 struct v4l2_rect crop;
466 u32 fw_min_cnt;
467 u32 out_width;
468 u32 out_height;
469 u32 colorspace;
470 u8 ycbcr_enc;
471 u8 quantization;
472 u8 xfer_func;
473 enum venus_dec_state codec_state;
474 enum venus_enc_state enc_state;
475 wait_queue_head_t reconf_wait;
476 unsigned int subscriptions;
477 int buf_count;
478 struct venus_ts_metadata tss[VIDEO_MAX_FRAME];
479 unsigned long payloads[VIDEO_MAX_FRAME];
480 u64 fps;
481 struct v4l2_fract timeperframe;
482 const struct venus_format *fmt_out;
483 const struct venus_format *fmt_cap;
484 unsigned int num_input_bufs;
485 unsigned int num_output_bufs;
486 unsigned int input_buf_size;
487 unsigned int output_buf_size;
488 unsigned int output2_buf_size;
489 u32 dpb_buftype;
490 u32 dpb_fmt;
491 u32 opb_buftype;
492 u32 opb_fmt;
493 bool reconfig;
494 u32 hfi_codec;
495 u32 sequence_cap;
496 u32 sequence_out;
497 struct v4l2_m2m_dev *m2m_dev;
498 struct v4l2_m2m_ctx *m2m_ctx;
499 struct mutex ctx_q_lock;
500 unsigned int state;
501 struct completion done;
502 unsigned int error;
503 bool session_error;
504 const struct hfi_inst_ops *ops;
505 u32 session_type;
506 union hfi_get_property hprop;
507 unsigned int core_acquired: 1;
508 unsigned int bit_depth;
509 unsigned int pic_struct;
510 bool next_buf_last;
511 bool drain_active;
512 enum venus_inst_modes flags;
513 struct ida dpb_ids;
514 };
515
516 #define IS_V1(core) ((core)->res->hfi_version == HFI_VERSION_1XX)
517 #define IS_V3(core) ((core)->res->hfi_version == HFI_VERSION_3XX)
518 #define IS_V4(core) ((core)->res->hfi_version == HFI_VERSION_4XX)
519 #define IS_V6(core) ((core)->res->hfi_version == HFI_VERSION_6XX)
520
521 #define IS_AR50(core) ((core)->res->vpu_version == VPU_VERSION_AR50)
522 #define IS_AR50_LITE(core) ((core)->res->vpu_version == VPU_VERSION_AR50_LITE)
523 #define IS_IRIS1(core) ((core)->res->vpu_version == VPU_VERSION_IRIS1)
524 #define IS_IRIS2(core) ((core)->res->vpu_version == VPU_VERSION_IRIS2)
525 #define IS_IRIS2_1(core) ((core)->res->vpu_version == VPU_VERSION_IRIS2_1)
526
527 #define ctrl_to_inst(ctrl) \
528 container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
529
to_inst(struct file * filp)530 static inline struct venus_inst *to_inst(struct file *filp)
531 {
532 return container_of(filp->private_data, struct venus_inst, fh);
533 }
534
to_hfi_priv(struct venus_core * core)535 static inline void *to_hfi_priv(struct venus_core *core)
536 {
537 return core->priv;
538 }
539
540 static inline struct hfi_plat_caps *
venus_caps_by_codec(struct venus_core * core,u32 codec,u32 domain)541 venus_caps_by_codec(struct venus_core *core, u32 codec, u32 domain)
542 {
543 unsigned int c;
544
545 for (c = 0; c < core->codecs_count; c++) {
546 if (core->caps[c].codec == codec &&
547 core->caps[c].domain == domain)
548 return &core->caps[c];
549 }
550
551 return NULL;
552 }
553
554 static inline bool
is_fw_rev_or_newer(struct venus_core * core,u32 vmajor,u32 vminor,u32 vrev)555 is_fw_rev_or_newer(struct venus_core *core, u32 vmajor, u32 vminor, u32 vrev)
556 {
557 return ((core)->venus_ver.major == vmajor &&
558 (core)->venus_ver.minor == vminor &&
559 (core)->venus_ver.rev >= vrev);
560 }
561
562 static inline bool
is_fw_rev_or_older(struct venus_core * core,u32 vmajor,u32 vminor,u32 vrev)563 is_fw_rev_or_older(struct venus_core *core, u32 vmajor, u32 vminor, u32 vrev)
564 {
565 return ((core)->venus_ver.major == vmajor &&
566 (core)->venus_ver.minor == vminor &&
567 (core)->venus_ver.rev <= vrev);
568 }
569 #endif
570