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