5bafbc12b943d6955973f1eaa20b9ccd4dc84d64
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / rockchip-vpu / rk3288_vpu_common.h
1 /*
2  * Rockchip RK3288 VPU codec driver
3  *
4  * Copyright (C) 2014 Google, Inc.
5  *      Tomasz Figa <tfiga@chromium.org>
6  *
7  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
8  *
9  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
10  *
11  * This software is licensed under the terms of the GNU General Public
12  * License version 2, as published by the Free Software Foundation, and
13  * may be copied, distributed, and modified under those terms.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20
21 #ifndef RK3288_VPU_COMMON_H_
22 #define RK3288_VPU_COMMON_H_
23
24 /* Enable debugging by default for now. */
25 #define DEBUG
26
27 #include <linux/platform_device.h>
28 #include <linux/videodev2.h>
29 #include <linux/wait.h>
30
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/videobuf2-core.h>
35 #include <media/videobuf2-dma-contig.h>
36
37 #include "rk3288_vpu_hw.h"
38
39 #define RK3288_VPU_NAME                 "rk3288-vpu"
40 #define RK3288_VPU_DEC_NAME             "rk3288-vpu-dec"
41 #define RK3288_VPU_ENC_NAME             "rk3288-vpu-enc"
42
43 #define V4L2_CID_CUSTOM_BASE            (V4L2_CID_USER_BASE | 0x1000)
44
45 #define DST_QUEUE_OFF_BASE              (TASK_SIZE / 2)
46
47 #define RK3288_VPU_MAX_CTRLS            32
48
49 #define MB_DIM                          16
50 #define MB_WIDTH(x_size)                DIV_ROUND_UP(x_size, MB_DIM)
51 #define MB_HEIGHT(y_size)               DIV_ROUND_UP(y_size, MB_DIM)
52
53 struct rk3288_vpu_variant;
54 struct rk3288_vpu_ctx;
55 struct rk3288_vpu_codec_ops;
56
57 /**
58  * enum rk3288_vpu_codec_mode - codec operating mode.
59  * @RK_VPU_CODEC_NONE:  No operating mode. Used for RAW video formats.
60  * @RK_VPU_CODEC_H264D: H264 decoder.
61  * @RK_VPU_CODEC_VP8D:  VP8 decoder.
62  * @RK_VPU_CODEC_H264E: H264 encoder.
63  * @RK_VPU_CODEC_VP8E:  VP8 encoder.
64  */
65 enum rk3288_vpu_codec_mode {
66         RK_VPU_CODEC_NONE = -1,
67         RK_VPU_CODEC_H264D,
68         RK_VPU_CODEC_VP8D,
69         RK_VPU_CODEC_H264E,
70         RK_VPU_CODEC_VP8E
71 };
72
73 /**
74  * enum rk3288_vpu_plane - indices of planes inside a VB2 buffer.
75  * @PLANE_Y:            Plane containing luminance data (also denoted as Y).
76  * @PLANE_CB_CR:        Plane containing interleaved chrominance data (also
77  *                      denoted as CbCr).
78  * @PLANE_CB:           Plane containing CB part of chrominance data.
79  * @PLANE_CR:           Plane containing CR part of chrominance data.
80  */
81 enum rk3288_vpu_plane {
82         PLANE_Y         = 0,
83         PLANE_CB_CR     = 1,
84         PLANE_CB        = 1,
85         PLANE_CR        = 2,
86 };
87
88 /**
89  * struct rk3288_vpu_vp8e_buf_data - mode-specific per-buffer data
90  * @dct_offset:         Offset inside the buffer to DCT partition.
91  * @hdr_size:           Size of header data in the buffer.
92  * @ext_hdr_size:       Size of ext header data in the buffer.
93  * @dct_size:           Size of DCT partition in the buffer.
94  * @header:             Frame header to copy to destination buffer.
95  */
96 struct rk3288_vpu_vp8e_buf_data {
97         size_t dct_offset;
98         size_t hdr_size;
99         size_t ext_hdr_size;
100         size_t dct_size;
101         u8 header[RK3288_HEADER_SIZE];
102 };
103
104 /**
105  * struct rk3288_vpu_buf - Private data related to each VB2 buffer.
106  * @vb:                 Pointer to related VB2 buffer.
107  * @list:               List head for queuing in buffer queue.
108  * @flags:              Buffer state. See enum rk3288_vpu_buf_flags.
109  */
110 struct rk3288_vpu_buf {
111         struct vb2_v4l2_buffer vb;
112         struct list_head list;
113
114         /* Mode-specific data. */
115         union {
116                 struct rk3288_vpu_vp8e_buf_data vp8e;
117         };
118 };
119
120 /**
121  * enum rk3288_vpu_state - bitwise flags indicating hardware state.
122  * @VPU_RUNNING:        The hardware has been programmed for operation
123  *                      and is running at the moment.
124  * @VPU_SUSPENDED:      System is entering sleep state and no more runs
125  *                      should be executed on hardware.
126  */
127 enum rk3288_vpu_state {
128         VPU_RUNNING     = BIT(0),
129         VPU_SUSPENDED   = BIT(1),
130 };
131
132 /**
133  * struct rk3288_vpu_dev - driver data
134  * @v4l2_dev:           V4L2 device to register video devices for.
135  * @vfd_dec:            Video device for decoder.
136  * @vfd_enc:            Video device for encoder.
137  * @pdev:               Pointer to VPU platform device.
138  * @dev:                Pointer to device for convenient logging using
139  *                      dev_ macros.
140  * @alloc_ctx:          VB2 allocator context
141  *                      (for allocations without kernel mapping).
142  * @alloc_ctx_vm:       VB2 allocator context
143  *                      (for allocations with kernel mapping).
144  * @aclk_vcodec:        Handle of ACLK clock.
145  * @hclk_vcodec:        Handle of HCLK clock.
146  * @base:               Mapped address of VPU registers.
147  * @enc_base:           Mapped address of VPU encoder register for convenience.
148  * @dec_base:           Mapped address of VPU decoder register for convenience.
149  * @mapping:            DMA IOMMU mapping.
150  * @vpu_mutex:          Mutex to synchronize V4L2 calls.
151  * @irqlock:            Spinlock to synchronize access to data structures
152  *                      shared with interrupt handlers.
153  * @state:              Device state.
154  * @ready_ctxs:         List of contexts ready to run.
155  * @variant:            Hardware variant-specfic parameters.
156  * @current_ctx:        Context being currently processed by hardware.
157  * @run_wq:             Wait queue to wait for run completion.
158  * @watchdog_work:      Delayed work for hardware timeout handling.
159  * @dummy_encode_ctx:   Context used to run dummy frame encoding to initialize
160  *                      encoder hardware state.
161  * @dummy_encode_src:   Source buffers used for dummy frame encoding.
162  * @dummy_encode_dst:   Desintation buffer used for dummy frame encoding.
163  * @was_decoding:       Indicates whether last run context was a decoder.
164  */
165 struct rk3288_vpu_dev {
166         struct v4l2_device v4l2_dev;
167         struct video_device *vfd_dec;
168         struct video_device *vfd_enc;
169         struct platform_device *pdev;
170         struct device *dev;
171         void *alloc_ctx;
172         void *alloc_ctx_vm;
173         struct clk *aclk_vcodec;
174         struct clk *hclk_vcodec;
175         void __iomem *base;
176         void __iomem *enc_base;
177         void __iomem *dec_base;
178         struct dma_iommu_mapping *mapping;
179
180         struct mutex vpu_mutex; /* video_device lock */
181         spinlock_t irqlock;
182         unsigned long state;
183         struct list_head ready_ctxs;
184         const struct rk3288_vpu_variant *variant;
185         struct rk3288_vpu_ctx *current_ctx;
186         wait_queue_head_t run_wq;
187         struct delayed_work watchdog_work;
188         struct rk3288_vpu_ctx *dummy_encode_ctx;
189         struct rk3288_vpu_aux_buf dummy_encode_src[VIDEO_MAX_PLANES];
190         struct rk3288_vpu_aux_buf dummy_encode_dst;
191         bool was_decoding;
192 };
193
194 /**
195  * struct rk3288_vpu_run_ops - per context operations on run data.
196  * @prepare_run:        Called when the context was selected for running
197  *                      to prepare operating mode specific data.
198  * @run_done:           Called when hardware completed the run to collect
199  *                      operating mode specific data from hardware and
200  *                      finalize the processing.
201  */
202 struct rk3288_vpu_run_ops {
203         void (*prepare_run)(struct rk3288_vpu_ctx *);
204         void (*run_done)(struct rk3288_vpu_ctx *, enum vb2_buffer_state);
205 };
206
207 /**
208  * struct rk3288_vpu_vp8e_run - per-run data specific to VP8 encoding.
209  * @reg_params: Pointer to a buffer containing register values prepared
210  *              by user space.
211  */
212 struct rk3288_vpu_vp8e_run {
213         const struct rk3288_vp8e_reg_params *reg_params;
214 };
215
216 /**
217  * struct rk3288_vpu_vp8d_run - per-run data specific to VP8 decoding.
218  * @frame_hdr: Pointer to a buffer containing per-run frame data which
219  *                      is needed by setting vpu register.
220  */
221 struct rk3288_vpu_vp8d_run {
222         const struct v4l2_ctrl_vp8_frame_hdr *frame_hdr;
223 };
224
225 /**
226  * struct rk3288_vpu_h264d_run - per-run data specific to H264 decoding.
227  * @sps:                Pointer to a buffer containing H264 SPS.
228  * @pps:                Pointer to a buffer containing H264 PPS.
229  * @scaling_matrix:     Pointer to a buffer containing scaling matrix.
230  * @slice_param:        Pointer to a buffer containing slice parameters array.
231  * @decode_param:       Pointer to a buffer containing decode parameters.
232  * @dpb:                Array of DPB entries reordered to keep POC order.
233  * @dpb_map:            Map of indices used in ref_pic_list_* into indices to
234  *                      reordered DPB array.
235  */
236 struct rk3288_vpu_h264d_run {
237         const struct v4l2_ctrl_h264_sps *sps;
238         const struct v4l2_ctrl_h264_pps *pps;
239         const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
240         const struct v4l2_ctrl_h264_slice_param *slice_param;
241         const struct v4l2_ctrl_h264_decode_param *decode_param;
242         struct v4l2_h264_dpb_entry dpb[16];
243         u8 dpb_map[16];
244 };
245
246 /**
247  * struct rk3288_vpu_run - per-run data for hardware code.
248  * @src:                Source buffer to be processed.
249  * @dst:                Destination buffer to be processed.
250  * @priv_src:           Hardware private source buffer.
251  * @priv_dst:           Hardware private destination buffer.
252  */
253 struct rk3288_vpu_run {
254         /* Generic for more than one operating mode. */
255         struct rk3288_vpu_buf *src;
256         struct rk3288_vpu_buf *dst;
257
258         struct rk3288_vpu_aux_buf priv_src;
259         struct rk3288_vpu_aux_buf priv_dst;
260
261         /* Specific for particular operating modes. */
262         union {
263                 struct rk3288_vpu_vp8e_run vp8e;
264                 struct rk3288_vpu_vp8d_run vp8d;
265                 struct rk3288_vpu_h264d_run h264d;
266                 /* Other modes will need different data. */
267         };
268 };
269
270 /**
271  * struct rk3288_vpu_ctx - Context (instance) private data.
272  *
273  * @dev:                VPU driver data to which the context belongs.
274  * @fh:                 V4L2 file handler.
275  *
276  * @vpu_src_fmt:        Descriptor of active source format.
277  * @src_fmt:            V4L2 pixel format of active source format.
278  * @vpu_dst_fmt:        Descriptor of active destination format.
279  * @dst_fmt:            V4L2 pixel format of active destination format.
280  *
281  * @vq_src:             Videobuf2 source queue.
282  * @src_queue:          Internal source buffer queue.
283  * @src_crop:           Configured source crop rectangle (encoder-only).
284  * @vq_dst:             Videobuf2 destination queue
285  * @dst_queue:          Internal destination buffer queue.
286  * @dst_bufs:           Private buffers wrapping VB2 buffers (destination).
287  *
288  * @ctrls:              Array containing pointer to registered controls.
289  * @ctrl_handler:       Control handler used to register controls.
290  * @num_ctrls:          Number of registered controls.
291  *
292  * @list:               List head for queue of ready contexts.
293  *
294  * @run:                Structure containing data about currently scheduled
295  *                      processing run.
296  * @run_ops:            Set of operations related to currently scheduled run.
297  * @hw:                 Structure containing hardware-related context.
298  */
299 struct rk3288_vpu_ctx {
300         struct rk3288_vpu_dev *dev;
301         struct v4l2_fh fh;
302
303         /* Format info */
304         struct rk3288_vpu_fmt *vpu_src_fmt;
305         struct v4l2_pix_format_mplane src_fmt;
306         struct rk3288_vpu_fmt *vpu_dst_fmt;
307         struct v4l2_pix_format_mplane dst_fmt;
308
309         /* VB2 queue data */
310         struct vb2_queue vq_src;
311         struct list_head src_queue;
312         struct v4l2_rect src_crop;
313         struct vb2_queue vq_dst;
314         struct list_head dst_queue;
315         struct vb2_buffer *dst_bufs[VIDEO_MAX_FRAME];
316
317         /* Controls */
318         struct v4l2_ctrl *ctrls[RK3288_VPU_MAX_CTRLS];
319         struct v4l2_ctrl_handler ctrl_handler;
320         unsigned num_ctrls;
321
322         /* Various runtime data */
323         struct list_head list;
324
325         struct rk3288_vpu_run run;
326         const struct rk3288_vpu_run_ops *run_ops;
327         struct rk3288_vpu_hw_ctx hw;
328 };
329
330 /**
331  * struct rk3288_vpu_fmt - information about supported video formats.
332  * @name:       Human readable name of the format.
333  * @fourcc:     FourCC code of the format. See V4L2_PIX_FMT_*.
334  * @codec_mode: Codec mode related to this format. See
335  *              enum rk3288_vpu_codec_mode.
336  * @num_planes: Number of planes used by this format.
337  * @depth:      Depth of each plane in bits per pixel.
338  * @enc_fmt:    Format identifier for encoder registers.
339  */
340 struct rk3288_vpu_fmt {
341         char *name;
342         u32 fourcc;
343         enum rk3288_vpu_codec_mode codec_mode;
344         int num_planes;
345         u8 depth[VIDEO_MAX_PLANES];
346         enum rk3288_vpu_enc_fmt enc_fmt;
347 };
348
349 /**
350  * struct rk3288_vpu_control - information about controls to be registered.
351  * @id:                 Control ID.
352  * @type:               Type of the control.
353  * @name:               Human readable name of the control.
354  * @minimum:            Minimum value of the control.
355  * @maximum:            Maximum value of the control.
356  * @step:               Control value increase step.
357  * @menu_skip_mask:     Mask of invalid menu positions.
358  * @default_value:      Initial value of the control.
359  * @max_stores:         Maximum number of configration stores.
360  * @dims:               Size of each dimension of compound control.
361  * @elem_size:          Size of individual element of compound control.
362  * @is_volatile:        Control is volatile.
363  * @is_read_only:       Control is read-only.
364  * @can_store:          Control uses configuration stores.
365  *
366  * See also struct v4l2_ctrl_config.
367  */
368 struct rk3288_vpu_control {
369         u32 id;
370
371         enum v4l2_ctrl_type type;
372         const char *name;
373         s32 minimum;
374         s32 maximum;
375         s32 step;
376         u32 menu_skip_mask;
377         s32 default_value;
378         s32 max_stores;
379         u32 dims[V4L2_CTRL_MAX_DIMS];
380         u32 elem_size;
381
382         bool is_volatile:1;
383         bool is_read_only:1;
384         bool can_store:1;
385 };
386
387 /* Logging helpers */
388
389 /**
390  * debug - Module parameter to control level of debugging messages.
391  *
392  * Level of debugging messages can be controlled by bits of module parameter
393  * called "debug". Meaning of particular bits is as follows:
394  *
395  * bit 0 - global information: mode, size, init, release
396  * bit 1 - each run start/result information
397  * bit 2 - contents of small controls from userspace
398  * bit 3 - contents of big controls from userspace
399  * bit 4 - detail fmt, ctrl, buffer q/dq information
400  * bit 5 - detail function enter/leave trace information
401  * bit 6 - register write/read information
402  */
403 extern int debug;
404
405 #define vpu_debug(level, fmt, args...)                          \
406         do {                                                    \
407                 if (debug & BIT(level))                         \
408                         pr_debug("%s:%d: " fmt,                 \
409                                  __func__, __LINE__, ##args);   \
410         } while (0)
411
412 #define vpu_debug_enter()       vpu_debug(5, "enter\n")
413 #define vpu_debug_leave()       vpu_debug(5, "leave\n")
414
415 #define vpu_err(fmt, args...)                                   \
416         pr_err("%s:%d: " fmt, __func__, __LINE__, ##args)
417
418 static inline char *fmt2str(u32 fmt, char *str)
419 {
420         char a = fmt & 0xFF;
421         char b = (fmt >> 8) & 0xFF;
422         char c = (fmt >> 16) & 0xFF;
423         char d = (fmt >> 24) & 0xFF;
424
425         sprintf(str, "%c%c%c%c", a, b, c, d);
426
427         return str;
428 }
429
430 /* Structure access helpers. */
431 static inline struct rk3288_vpu_ctx *fh_to_ctx(struct v4l2_fh *fh)
432 {
433         return container_of(fh, struct rk3288_vpu_ctx, fh);
434 }
435
436 static inline struct rk3288_vpu_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
437 {
438         return container_of(ctrl->handler, struct rk3288_vpu_ctx, ctrl_handler);
439 }
440
441 static inline struct rk3288_vpu_buf *vb_to_buf(struct vb2_buffer *vb)
442 {
443         return container_of(to_vb2_v4l2_buffer(vb), struct rk3288_vpu_buf, vb);
444 }
445
446 static inline bool rk3288_vpu_ctx_is_encoder(struct rk3288_vpu_ctx *ctx)
447 {
448         return ctx->vpu_dst_fmt->codec_mode != RK_VPU_CODEC_NONE;
449 }
450
451 static inline bool rk3288_vpu_ctx_is_dummy_encode(struct rk3288_vpu_ctx *ctx)
452 {
453         struct rk3288_vpu_dev *dev = ctx->dev;
454
455         return ctx == dev->dummy_encode_ctx;
456 }
457
458 int rk3288_vpu_ctrls_setup(struct rk3288_vpu_ctx *ctx,
459                            const struct v4l2_ctrl_ops *ctrl_ops,
460                            struct rk3288_vpu_control *controls,
461                            unsigned num_ctrls,
462                            const char *const *(*get_menu)(u32));
463 void rk3288_vpu_ctrls_delete(struct rk3288_vpu_ctx *ctx);
464
465 void rk3288_vpu_try_context(struct rk3288_vpu_dev *dev,
466                             struct rk3288_vpu_ctx *ctx);
467
468 void rk3288_vpu_run_done(struct rk3288_vpu_ctx *ctx,
469                          enum vb2_buffer_state result);
470
471 int rk3288_vpu_aux_buf_alloc(struct rk3288_vpu_dev *vpu,
472                             struct rk3288_vpu_aux_buf *buf, size_t size);
473 void rk3288_vpu_aux_buf_free(struct rk3288_vpu_dev *vpu,
474                              struct rk3288_vpu_aux_buf *buf);
475
476 /* Register accessors. */
477 static inline void vepu_write_relaxed(struct rk3288_vpu_dev *vpu,
478                                        u32 val, u32 reg)
479 {
480         vpu_debug(6, "MARK: set reg[%03d]: %08x\n", reg / 4, val);
481         writel_relaxed(val, vpu->enc_base + reg);
482 }
483
484 static inline void vepu_write(struct rk3288_vpu_dev *vpu, u32 val, u32 reg)
485 {
486         vpu_debug(6, "MARK: set reg[%03d]: %08x\n", reg / 4, val);
487         writel(val, vpu->enc_base + reg);
488 }
489
490 static inline u32 vepu_read(struct rk3288_vpu_dev *vpu, u32 reg)
491 {
492         u32 val = readl(vpu->enc_base + reg);
493
494         vpu_debug(6, "MARK: get reg[%03d]: %08x\n", reg / 4, val);
495         return val;
496 }
497
498 static inline void vdpu_write_relaxed(struct rk3288_vpu_dev *vpu,
499                                        u32 val, u32 reg)
500 {
501         vpu_debug(6, "MARK: set reg[%03d]: %08x\n", reg / 4, val);
502         writel_relaxed(val, vpu->dec_base + reg);
503 }
504
505 static inline void vdpu_write(struct rk3288_vpu_dev *vpu, u32 val, u32 reg)
506 {
507         vpu_debug(6, "MARK: set reg[%03d]: %08x\n", reg / 4, val);
508         writel(val, vpu->dec_base + reg);
509 }
510
511 static inline u32 vdpu_read(struct rk3288_vpu_dev *vpu, u32 reg)
512 {
513         u32 val = readl(vpu->dec_base + reg);
514
515         vpu_debug(6, "MARK: get reg[%03d]: %08x\n", reg / 4, val);
516         return val;
517 }
518
519 #endif /* RK3288_VPU_COMMON_H_ */