GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / media / platform / qcom / venus / core.h
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 /**
61  * struct venus_core - holds core parameters valid for all instances
62  *
63  * @base:       IO memory base address
64  * @irq:                Venus irq
65  * @clks:       an array of struct clk pointers
66  * @core0_clk:  a struct clk pointer for core0
67  * @core1_clk:  a struct clk pointer for core1
68  * @vdev_dec:   a reference to video device structure for decoder instances
69  * @vdev_enc:   a reference to video device structure for encoder instances
70  * @v4l2_dev:   a holder for v4l2 device structure
71  * @res:                a reference to venus resources structure
72  * @dev:                convenience struct device pointer
73  * @dev_dec:    convenience struct device pointer for decoder device
74  * @dev_enc:    convenience struct device pointer for encoder device
75  * @lock:       a lock for this strucure
76  * @instances:  a list_head of all instances
77  * @insts_count:        num of instances
78  * @state:      the state of the venus core
79  * @done:       a completion for sync HFI operations
80  * @error:      an error returned during last HFI sync operations
81  * @sys_error:  an error flag that signal system error event
82  * @core_ops:   the core operations
83  * @enc_codecs: encoders supported by this core
84  * @dec_codecs: decoders supported by this core
85  * @max_sessions_supported:     holds the maximum number of sessions
86  * @core_caps:  core capabilities
87  * @priv:       a private filed for HFI operations
88  * @ops:                the core HFI operations
89  * @work:       a delayed work for handling system fatal error
90  */
91 struct venus_core {
92         void __iomem *base;
93         int irq;
94         struct clk *clks[VIDC_CLKS_NUM_MAX];
95         struct clk *core0_clk;
96         struct clk *core1_clk;
97         struct video_device *vdev_dec;
98         struct video_device *vdev_enc;
99         struct v4l2_device v4l2_dev;
100         const struct venus_resources *res;
101         struct device *dev;
102         struct device *dev_dec;
103         struct device *dev_enc;
104         struct mutex lock;
105         struct list_head instances;
106         atomic_t insts_count;
107         unsigned int state;
108         struct completion done;
109         unsigned int error;
110         bool sys_error;
111         const struct hfi_core_ops *core_ops;
112         u32 enc_codecs;
113         u32 dec_codecs;
114         unsigned int max_sessions_supported;
115 #define ENC_ROTATION_CAPABILITY         0x1
116 #define ENC_SCALING_CAPABILITY          0x2
117 #define ENC_DEINTERLACE_CAPABILITY      0x4
118 #define DEC_MULTI_STREAM_CAPABILITY     0x8
119         unsigned int core_caps;
120         void *priv;
121         const struct hfi_ops *ops;
122         struct delayed_work work;
123 };
124
125 struct vdec_controls {
126         u32 post_loop_deb_mode;
127         u32 profile;
128         u32 level;
129 };
130
131 struct venc_controls {
132         u16 gop_size;
133         u32 num_p_frames;
134         u32 num_b_frames;
135         u32 bitrate_mode;
136         u32 bitrate;
137         u32 bitrate_peak;
138
139         u32 h264_i_period;
140         u32 h264_entropy_mode;
141         u32 h264_i_qp;
142         u32 h264_p_qp;
143         u32 h264_b_qp;
144         u32 h264_min_qp;
145         u32 h264_max_qp;
146         u32 h264_loop_filter_mode;
147         u32 h264_loop_filter_alpha;
148         u32 h264_loop_filter_beta;
149
150         u32 vp8_min_qp;
151         u32 vp8_max_qp;
152
153         u32 multi_slice_mode;
154         u32 multi_slice_max_bytes;
155         u32 multi_slice_max_mb;
156
157         u32 header_mode;
158
159         struct {
160                 u32 mpeg4;
161                 u32 h264;
162                 u32 vpx;
163         } profile;
164         struct {
165                 u32 mpeg4;
166                 u32 h264;
167         } level;
168 };
169
170 struct venus_buffer {
171         struct vb2_v4l2_buffer vb;
172         struct list_head list;
173         dma_addr_t dma_addr;
174         u32 size;
175         struct list_head reg_list;
176         u32 flags;
177         struct list_head ref_list;
178 };
179
180 #define to_venus_buffer(ptr)    container_of(ptr, struct venus_buffer, vb)
181
182 /**
183  * struct venus_inst - holds per instance paramerters
184  *
185  * @list:       used for attach an instance to the core
186  * @lock:       instance lock
187  * @core:       a reference to the core struct
188  * @internalbufs:       a list of internal bufferes
189  * @registeredbufs:     a list of registered capture bufferes
190  * @delayed_process     a list of delayed buffers
191  * @delayed_process_work:       a work_struct for process delayed buffers
192  * @ctrl_handler:       v4l control handler
193  * @controls:   a union of decoder and encoder control parameters
194  * @fh:  a holder of v4l file handle structure
195  * @streamon_cap: stream on flag for capture queue
196  * @streamon_out: stream on flag for output queue
197  * @width:      current capture width
198  * @height:     current capture height
199  * @out_width:  current output width
200  * @out_height: current output height
201  * @colorspace: current color space
202  * @quantization:       current quantization
203  * @xfer_func:  current xfer function
204  * @fps:                holds current FPS
205  * @timeperframe:       holds current time per frame structure
206  * @fmt_out:    a reference to output format structure
207  * @fmt_cap:    a reference to capture format structure
208  * @num_input_bufs:     holds number of input buffers
209  * @num_output_bufs:    holds number of output buffers
210  * @input_buf_size      holds input buffer size
211  * @output_buf_size:    holds output buffer size
212  * @reconfig:   a flag raised by decoder when the stream resolution changed
213  * @reconfig_width:     holds the new width
214  * @reconfig_height:    holds the new height
215  * @sequence_cap:       a sequence counter for capture queue
216  * @sequence_out:       a sequence counter for output queue
217  * @m2m_dev:    a reference to m2m device structure
218  * @m2m_ctx:    a reference to m2m context structure
219  * @state:      current state of the instance
220  * @done:       a completion for sync HFI operation
221  * @error:      an error returned during last HFI sync operation
222  * @session_error:      a flag rised by HFI interface in case of session error
223  * @ops:                HFI operations
224  * @priv:       a private for HFI operations callbacks
225  * @session_type:       the type of the session (decoder or encoder)
226  * @hprop:      a union used as a holder by get property
227  * @cap_width:  width capability
228  * @cap_height: height capability
229  * @cap_mbs_per_frame:  macroblocks per frame capability
230  * @cap_mbs_per_sec:    macroblocks per second capability
231  * @cap_framerate:      framerate capability
232  * @cap_scale_x:                horizontal scaling capability
233  * @cap_scale_y:                vertical scaling capability
234  * @cap_bitrate:                bitrate capability
235  * @cap_hier_p:         hier capability
236  * @cap_ltr_count:      LTR count capability
237  * @cap_secure_output2_threshold: secure OUTPUT2 threshold capability
238  * @cap_bufs_mode_static:       buffers allocation mode capability
239  * @cap_bufs_mode_dynamic:      buffers allocation mode capability
240  * @pl_count:   count of supported profiles/levels
241  * @pl:         supported profiles/levels
242  * @bufreq:     holds buffer requirements
243  */
244 struct venus_inst {
245         struct list_head list;
246         struct mutex lock;
247         struct venus_core *core;
248         struct list_head internalbufs;
249         struct list_head registeredbufs;
250         struct list_head delayed_process;
251         struct work_struct delayed_process_work;
252
253         struct v4l2_ctrl_handler ctrl_handler;
254         union {
255                 struct vdec_controls dec;
256                 struct venc_controls enc;
257         } controls;
258         struct v4l2_fh fh;
259         unsigned int streamon_cap, streamon_out;
260         u32 width;
261         u32 height;
262         u32 out_width;
263         u32 out_height;
264         u32 colorspace;
265         u8 ycbcr_enc;
266         u8 quantization;
267         u8 xfer_func;
268         u64 fps;
269         struct v4l2_fract timeperframe;
270         const struct venus_format *fmt_out;
271         const struct venus_format *fmt_cap;
272         unsigned int num_input_bufs;
273         unsigned int num_output_bufs;
274         unsigned int input_buf_size;
275         unsigned int output_buf_size;
276         bool reconfig;
277         u32 reconfig_width;
278         u32 reconfig_height;
279         u32 sequence_cap;
280         u32 sequence_out;
281         struct v4l2_m2m_dev *m2m_dev;
282         struct v4l2_m2m_ctx *m2m_ctx;
283         unsigned int state;
284         struct completion done;
285         unsigned int error;
286         bool session_error;
287         const struct hfi_inst_ops *ops;
288         u32 session_type;
289         union hfi_get_property hprop;
290         struct hfi_capability cap_width;
291         struct hfi_capability cap_height;
292         struct hfi_capability cap_mbs_per_frame;
293         struct hfi_capability cap_mbs_per_sec;
294         struct hfi_capability cap_framerate;
295         struct hfi_capability cap_scale_x;
296         struct hfi_capability cap_scale_y;
297         struct hfi_capability cap_bitrate;
298         struct hfi_capability cap_hier_p;
299         struct hfi_capability cap_ltr_count;
300         struct hfi_capability cap_secure_output2_threshold;
301         bool cap_bufs_mode_static;
302         bool cap_bufs_mode_dynamic;
303         unsigned int pl_count;
304         struct hfi_profile_level pl[HFI_MAX_PROFILE_COUNT];
305         struct hfi_buffer_requirements bufreq[HFI_BUFFER_TYPE_MAX];
306 };
307
308 #define ctrl_to_inst(ctrl)      \
309         container_of((ctrl)->handler, struct venus_inst, ctrl_handler)
310
311 static inline struct venus_inst *to_inst(struct file *filp)
312 {
313         return container_of(filp->private_data, struct venus_inst, fh);
314 }
315
316 static inline void *to_hfi_priv(struct venus_core *core)
317 {
318         return core->priv;
319 }
320
321 #endif