GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / media / platform / chips-media / coda.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Coda multi-standard codec IP
4  *
5  * Copyright (C) 2012 Vista Silicon S.L.
6  *    Javier Martin, <javier.martin@vista-silicon.com>
7  *    Xavier Duret
8  * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
9  */
10
11 #ifndef __CODA_H__
12 #define __CODA_H__
13
14 #include <linux/debugfs.h>
15 #include <linux/idr.h>
16 #include <linux/irqreturn.h>
17 #include <linux/mutex.h>
18 #include <linux/kfifo.h>
19 #include <linux/videodev2.h>
20 #include <linux/ratelimit.h>
21
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-fh.h>
25 #include <media/videobuf2-v4l2.h>
26
27 #include "coda_regs.h"
28
29 #define CODA_MAX_FRAMEBUFFERS   19
30 #define FMO_SLICE_SAVE_BUF_SIZE (32)
31
32 /*
33  * This control allows applications to read the per-stream
34  * (i.e. per-context) Macroblocks Error Count. This value
35  * is CODA specific.
36  */
37 #define V4L2_CID_CODA_MB_ERR_CNT (V4L2_CID_USER_CODA_BASE + 0)
38
39 enum {
40         V4L2_M2M_SRC = 0,
41         V4L2_M2M_DST = 1,
42 };
43
44 enum coda_inst_type {
45         CODA_INST_ENCODER,
46         CODA_INST_DECODER,
47 };
48
49 enum coda_product {
50         CODA_DX6 = 0xf001,
51         CODA_HX4 = 0xf00a,
52         CODA_7541 = 0xf012,
53         CODA_960 = 0xf020,
54 };
55
56 struct coda_video_device;
57
58 struct coda_devtype {
59         char                    *firmware[3];
60         enum coda_product       product;
61         const struct coda_codec *codecs;
62         unsigned int            num_codecs;
63         const struct coda_video_device **vdevs;
64         unsigned int            num_vdevs;
65         size_t                  workbuf_size;
66         size_t                  tempbuf_size;
67         size_t                  iram_size;
68 };
69
70 struct coda_aux_buf {
71         void                    *vaddr;
72         dma_addr_t              paddr;
73         u32                     size;
74         struct debugfs_blob_wrapper blob;
75         struct dentry           *dentry;
76 };
77
78 struct coda_dev {
79         struct v4l2_device      v4l2_dev;
80         struct video_device     vfd[6];
81         struct device           *dev;
82         const struct coda_devtype *devtype;
83         int                     firmware;
84         struct vdoa_data        *vdoa;
85
86         void __iomem            *regs_base;
87         struct clk              *clk_per;
88         struct clk              *clk_ahb;
89         struct reset_control    *rstc;
90
91         struct coda_aux_buf     codebuf;
92         struct coda_aux_buf     tempbuf;
93         struct coda_aux_buf     workbuf;
94         struct gen_pool         *iram_pool;
95         struct coda_aux_buf     iram;
96
97         struct mutex            dev_mutex;
98         struct mutex            coda_mutex;
99         struct workqueue_struct *workqueue;
100         struct v4l2_m2m_dev     *m2m_dev;
101         struct ida              ida;
102         struct dentry           *debugfs_root;
103         struct ratelimit_state  mb_err_rs;
104 };
105
106 struct coda_codec {
107         u32 mode;
108         u32 src_fourcc;
109         u32 dst_fourcc;
110         u32 max_w;
111         u32 max_h;
112 };
113
114 struct coda_huff_tab;
115
116 struct coda_params {
117         u8                      rot_mode;
118         u8                      h264_intra_qp;
119         u8                      h264_inter_qp;
120         u8                      h264_min_qp;
121         u8                      h264_max_qp;
122         u8                      h264_disable_deblocking_filter_idc;
123         s8                      h264_slice_alpha_c0_offset_div2;
124         s8                      h264_slice_beta_offset_div2;
125         bool                    h264_constrained_intra_pred_flag;
126         s8                      h264_chroma_qp_index_offset;
127         u8                      h264_profile_idc;
128         u8                      h264_level_idc;
129         u8                      mpeg2_profile_idc;
130         u8                      mpeg2_level_idc;
131         u8                      mpeg4_intra_qp;
132         u8                      mpeg4_inter_qp;
133         u8                      gop_size;
134         int                     intra_refresh;
135         enum v4l2_jpeg_chroma_subsampling jpeg_chroma_subsampling;
136         u8                      jpeg_quality;
137         u8                      jpeg_restart_interval;
138         u8                      *jpeg_qmat_tab[3];
139         int                     jpeg_qmat_index[3];
140         int                     jpeg_huff_dc_index[3];
141         int                     jpeg_huff_ac_index[3];
142         u32                     *jpeg_huff_data;
143         struct coda_huff_tab    *jpeg_huff_tab;
144         int                     codec_mode;
145         int                     codec_mode_aux;
146         enum v4l2_mpeg_video_multi_slice_mode slice_mode;
147         u32                     framerate;
148         u16                     bitrate;
149         u16                     vbv_delay;
150         u32                     vbv_size;
151         u32                     slice_max_bits;
152         u32                     slice_max_mb;
153         bool                    force_ipicture;
154         bool                    gop_size_changed;
155         bool                    bitrate_changed;
156         bool                    framerate_changed;
157         bool                    h264_intra_qp_changed;
158         bool                    intra_refresh_changed;
159         bool                    slice_mode_changed;
160         bool                    frame_rc_enable;
161         bool                    mb_rc_enable;
162 };
163
164 struct coda_buffer_meta {
165         struct list_head        list;
166         u32                     sequence;
167         struct v4l2_timecode    timecode;
168         u64                     timestamp;
169         unsigned int            start;
170         unsigned int            end;
171         bool                    last;
172 };
173
174 /* Per-queue, driver-specific private data */
175 struct coda_q_data {
176         unsigned int            width;
177         unsigned int            height;
178         unsigned int            bytesperline;
179         unsigned int            sizeimage;
180         unsigned int            fourcc;
181         struct v4l2_rect        rect;
182 };
183
184 struct coda_iram_info {
185         u32             axi_sram_use;
186         phys_addr_t     buf_bit_use;
187         phys_addr_t     buf_ip_ac_dc_use;
188         phys_addr_t     buf_dbk_y_use;
189         phys_addr_t     buf_dbk_c_use;
190         phys_addr_t     buf_ovl_use;
191         phys_addr_t     buf_btp_use;
192         phys_addr_t     search_ram_paddr;
193         int             search_ram_size;
194         int             remaining;
195         phys_addr_t     next_paddr;
196 };
197
198 #define GDI_LINEAR_FRAME_MAP 0
199 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
200
201 struct coda_ctx;
202
203 struct coda_context_ops {
204         int (*queue_init)(void *priv, struct vb2_queue *src_vq,
205                           struct vb2_queue *dst_vq);
206         int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
207         int (*start_streaming)(struct coda_ctx *ctx);
208         int (*prepare_run)(struct coda_ctx *ctx);
209         void (*finish_run)(struct coda_ctx *ctx);
210         void (*run_timeout)(struct coda_ctx *ctx);
211         void (*seq_init_work)(struct work_struct *work);
212         void (*seq_end_work)(struct work_struct *work);
213         void (*release)(struct coda_ctx *ctx);
214 };
215
216 struct coda_internal_frame {
217         struct coda_aux_buf             buf;
218         struct coda_buffer_meta         meta;
219         u32                             type;
220         u32                             error;
221 };
222
223 struct coda_ctx {
224         struct coda_dev                 *dev;
225         struct mutex                    buffer_mutex;
226         struct work_struct              pic_run_work;
227         struct work_struct              seq_init_work;
228         struct work_struct              seq_end_work;
229         struct completion               completion;
230         const struct coda_video_device  *cvd;
231         const struct coda_context_ops   *ops;
232         int                             aborting;
233         int                             initialized;
234         int                             streamon_out;
235         int                             streamon_cap;
236         u32                             qsequence;
237         u32                             osequence;
238         u32                             sequence_offset;
239         struct coda_q_data              q_data[2];
240         enum coda_inst_type             inst_type;
241         const struct coda_codec         *codec;
242         enum v4l2_colorspace            colorspace;
243         enum v4l2_xfer_func             xfer_func;
244         enum v4l2_ycbcr_encoding        ycbcr_enc;
245         enum v4l2_quantization          quantization;
246         struct coda_params              params;
247         struct v4l2_ctrl_handler        ctrls;
248         struct v4l2_ctrl                *h264_profile_ctrl;
249         struct v4l2_ctrl                *h264_level_ctrl;
250         struct v4l2_ctrl                *mpeg2_profile_ctrl;
251         struct v4l2_ctrl                *mpeg2_level_ctrl;
252         struct v4l2_ctrl                *mpeg4_profile_ctrl;
253         struct v4l2_ctrl                *mpeg4_level_ctrl;
254         struct v4l2_ctrl                *mb_err_cnt_ctrl;
255         struct v4l2_fh                  fh;
256         int                             gopcounter;
257         int                             runcounter;
258         int                             jpeg_ecs_offset;
259         char                            vpu_header[3][64];
260         int                             vpu_header_size[3];
261         struct kfifo                    bitstream_fifo;
262         struct mutex                    bitstream_mutex;
263         struct coda_aux_buf             bitstream;
264         bool                            hold;
265         struct coda_aux_buf             parabuf;
266         struct coda_aux_buf             psbuf;
267         struct coda_aux_buf             slicebuf;
268         struct coda_internal_frame      internal_frames[CODA_MAX_FRAMEBUFFERS];
269         struct list_head                buffer_meta_list;
270         spinlock_t                      buffer_meta_lock;
271         int                             num_metas;
272         unsigned int                    first_frame_sequence;
273         struct coda_aux_buf             workbuf;
274         int                             num_internal_frames;
275         int                             idx;
276         int                             reg_idx;
277         struct coda_iram_info           iram_info;
278         int                             tiled_map_type;
279         u32                             bit_stream_param;
280         u32                             frm_dis_flg;
281         u32                             frame_mem_ctrl;
282         u32                             para_change;
283         int                             display_idx;
284         struct dentry                   *debugfs_entry;
285         bool                            use_bit;
286         bool                            use_vdoa;
287         struct vdoa_ctx                 *vdoa;
288         /*
289          * wakeup mutex used to serialize encoder stop command and finish_run,
290          * ensures that finish_run always either flags the last returned buffer
291          * or wakes up the capture queue to signal EOS afterwards.
292          */
293         struct mutex                    wakeup_mutex;
294 };
295
296 extern int coda_debug;
297
298 #define coda_dbg(level, ctx, fmt, arg...)                               \
299         do {                                                            \
300                 if (coda_debug >= (level))                              \
301                         v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
302                          "%u: " fmt, (ctx)->idx, ##arg);                \
303         } while (0)
304
305 void coda_write(struct coda_dev *dev, u32 data, u32 reg);
306 unsigned int coda_read(struct coda_dev *dev, u32 reg);
307 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
308                      struct vb2_v4l2_buffer *buf, unsigned int reg_y);
309
310 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
311                        size_t size, const char *name, struct dentry *parent);
312 void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
313
314 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
315                             struct vb2_queue *dst_vq);
316 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
317                             struct vb2_queue *dst_vq);
318
319 int coda_hw_reset(struct coda_ctx *ctx);
320
321 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
322
323 void coda_set_gdi_regs(struct coda_ctx *ctx);
324
325 static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
326                                              enum v4l2_buf_type type)
327 {
328         switch (type) {
329         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
330                 return &(ctx->q_data[V4L2_M2M_SRC]);
331         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
332                 return &(ctx->q_data[V4L2_M2M_DST]);
333         default:
334                 return NULL;
335         }
336 }
337
338 const char *coda_product_name(int product);
339
340 int coda_check_firmware(struct coda_dev *dev);
341
342 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
343 {
344         return kfifo_len(&ctx->bitstream_fifo);
345 }
346
347 /*
348  * The bitstream prefetcher needs to read at least 2 256 byte periods past
349  * the desired bitstream position for all data to reach the decoder.
350  */
351 static inline bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx,
352                                                  unsigned int pos)
353 {
354         return (int)(ctx->bitstream_fifo.kfifo.in - ALIGN(pos, 256)) > 512;
355 }
356
357 bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx, unsigned int pos);
358 int coda_bitstream_flush(struct coda_ctx *ctx);
359
360 void coda_bit_stream_end_flag(struct coda_ctx *ctx);
361
362 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
363                        enum vb2_buffer_state state);
364
365 int coda_h264_filler_nal(int size, char *p);
366 int coda_h264_padding(int size, char *p);
367 int coda_h264_profile(int profile_idc);
368 int coda_h264_level(int level_idc);
369 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
370 int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
371                         int *size, int max_size);
372
373 int coda_mpeg2_profile(int profile_idc);
374 int coda_mpeg2_level(int level_idc);
375 u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
376 int coda_mpeg4_profile(int profile_idc);
377 int coda_mpeg4_level(int level_idc);
378 u32 coda_mpeg4_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
379
380 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
381                                      u8 level_idc);
382
383 static inline int coda_jpeg_scale(int src, int dst)
384 {
385         return (dst <= src / 8) ? 3 :
386                (dst <= src / 4) ? 2 :
387                (dst <= src / 2) ? 1 : 0;
388 }
389
390 bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
391 int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb);
392 int coda_jpeg_write_tables(struct coda_ctx *ctx);
393 void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
394
395 extern const struct coda_context_ops coda_bit_encode_ops;
396 extern const struct coda_context_ops coda_bit_decode_ops;
397 extern const struct coda_context_ops coda9_jpeg_encode_ops;
398 extern const struct coda_context_ops coda9_jpeg_decode_ops;
399
400 irqreturn_t coda_irq_handler(int irq, void *data);
401 irqreturn_t coda9_jpeg_irq_handler(int irq, void *data);
402
403 #endif /* __CODA_H__ */