Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Coda multi-standard codec IP
0004  *
0005  * Copyright (C) 2012 Vista Silicon S.L.
0006  *    Javier Martin, <javier.martin@vista-silicon.com>
0007  *    Xavier Duret
0008  * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
0009  */
0010 
0011 #ifndef __CODA_H__
0012 #define __CODA_H__
0013 
0014 #include <linux/debugfs.h>
0015 #include <linux/idr.h>
0016 #include <linux/irqreturn.h>
0017 #include <linux/mutex.h>
0018 #include <linux/kfifo.h>
0019 #include <linux/videodev2.h>
0020 #include <linux/ratelimit.h>
0021 
0022 #include <media/v4l2-ctrls.h>
0023 #include <media/v4l2-device.h>
0024 #include <media/v4l2-fh.h>
0025 #include <media/videobuf2-v4l2.h>
0026 
0027 #include "coda_regs.h"
0028 
0029 #define CODA_MAX_FRAMEBUFFERS   19
0030 #define FMO_SLICE_SAVE_BUF_SIZE (32)
0031 
0032 /*
0033  * This control allows applications to read the per-stream
0034  * (i.e. per-context) Macroblocks Error Count. This value
0035  * is CODA specific.
0036  */
0037 #define V4L2_CID_CODA_MB_ERR_CNT (V4L2_CID_USER_CODA_BASE + 0)
0038 
0039 enum {
0040     V4L2_M2M_SRC = 0,
0041     V4L2_M2M_DST = 1,
0042 };
0043 
0044 enum coda_inst_type {
0045     CODA_INST_ENCODER,
0046     CODA_INST_DECODER,
0047 };
0048 
0049 enum coda_product {
0050     CODA_DX6 = 0xf001,
0051     CODA_HX4 = 0xf00a,
0052     CODA_7541 = 0xf012,
0053     CODA_960 = 0xf020,
0054 };
0055 
0056 struct coda_video_device;
0057 
0058 struct coda_devtype {
0059     char            *firmware[3];
0060     enum coda_product   product;
0061     const struct coda_codec *codecs;
0062     unsigned int        num_codecs;
0063     const struct coda_video_device **vdevs;
0064     unsigned int        num_vdevs;
0065     size_t          workbuf_size;
0066     size_t          tempbuf_size;
0067     size_t          iram_size;
0068 };
0069 
0070 struct coda_aux_buf {
0071     void            *vaddr;
0072     dma_addr_t      paddr;
0073     u32         size;
0074     struct debugfs_blob_wrapper blob;
0075     struct dentry       *dentry;
0076 };
0077 
0078 struct coda_dev {
0079     struct v4l2_device  v4l2_dev;
0080     struct video_device vfd[6];
0081     struct device       *dev;
0082     const struct coda_devtype *devtype;
0083     int         firmware;
0084     struct vdoa_data    *vdoa;
0085 
0086     void __iomem        *regs_base;
0087     struct clk      *clk_per;
0088     struct clk      *clk_ahb;
0089     struct reset_control    *rstc;
0090 
0091     struct coda_aux_buf codebuf;
0092     struct coda_aux_buf tempbuf;
0093     struct coda_aux_buf workbuf;
0094     struct gen_pool     *iram_pool;
0095     struct coda_aux_buf iram;
0096 
0097     struct mutex        dev_mutex;
0098     struct mutex        coda_mutex;
0099     struct workqueue_struct *workqueue;
0100     struct v4l2_m2m_dev *m2m_dev;
0101     struct ida      ida;
0102     struct dentry       *debugfs_root;
0103     struct ratelimit_state  mb_err_rs;
0104 };
0105 
0106 struct coda_codec {
0107     u32 mode;
0108     u32 src_fourcc;
0109     u32 dst_fourcc;
0110     u32 max_w;
0111     u32 max_h;
0112 };
0113 
0114 struct coda_huff_tab;
0115 
0116 struct coda_params {
0117     u8          rot_mode;
0118     u8          h264_intra_qp;
0119     u8          h264_inter_qp;
0120     u8          h264_min_qp;
0121     u8          h264_max_qp;
0122     u8          h264_disable_deblocking_filter_idc;
0123     s8          h264_slice_alpha_c0_offset_div2;
0124     s8          h264_slice_beta_offset_div2;
0125     bool            h264_constrained_intra_pred_flag;
0126     s8          h264_chroma_qp_index_offset;
0127     u8          h264_profile_idc;
0128     u8          h264_level_idc;
0129     u8          mpeg2_profile_idc;
0130     u8          mpeg2_level_idc;
0131     u8          mpeg4_intra_qp;
0132     u8          mpeg4_inter_qp;
0133     u8          gop_size;
0134     int         intra_refresh;
0135     enum v4l2_jpeg_chroma_subsampling jpeg_chroma_subsampling;
0136     u8          jpeg_quality;
0137     u8          jpeg_restart_interval;
0138     u8          *jpeg_qmat_tab[3];
0139     int         jpeg_qmat_index[3];
0140     int         jpeg_huff_dc_index[3];
0141     int         jpeg_huff_ac_index[3];
0142     u32         *jpeg_huff_data;
0143     struct coda_huff_tab    *jpeg_huff_tab;
0144     int         codec_mode;
0145     int         codec_mode_aux;
0146     enum v4l2_mpeg_video_multi_slice_mode slice_mode;
0147     u32         framerate;
0148     u16         bitrate;
0149     u16         vbv_delay;
0150     u32         vbv_size;
0151     u32         slice_max_bits;
0152     u32         slice_max_mb;
0153     bool            force_ipicture;
0154     bool            gop_size_changed;
0155     bool            bitrate_changed;
0156     bool            framerate_changed;
0157     bool            h264_intra_qp_changed;
0158     bool            intra_refresh_changed;
0159     bool            slice_mode_changed;
0160     bool            frame_rc_enable;
0161     bool            mb_rc_enable;
0162 };
0163 
0164 struct coda_buffer_meta {
0165     struct list_head    list;
0166     u32         sequence;
0167     struct v4l2_timecode    timecode;
0168     u64         timestamp;
0169     unsigned int        start;
0170     unsigned int        end;
0171     bool            last;
0172 };
0173 
0174 /* Per-queue, driver-specific private data */
0175 struct coda_q_data {
0176     unsigned int        width;
0177     unsigned int        height;
0178     unsigned int        bytesperline;
0179     unsigned int        sizeimage;
0180     unsigned int        fourcc;
0181     struct v4l2_rect    rect;
0182 };
0183 
0184 struct coda_iram_info {
0185     u32     axi_sram_use;
0186     phys_addr_t buf_bit_use;
0187     phys_addr_t buf_ip_ac_dc_use;
0188     phys_addr_t buf_dbk_y_use;
0189     phys_addr_t buf_dbk_c_use;
0190     phys_addr_t buf_ovl_use;
0191     phys_addr_t buf_btp_use;
0192     phys_addr_t search_ram_paddr;
0193     int     search_ram_size;
0194     int     remaining;
0195     phys_addr_t next_paddr;
0196 };
0197 
0198 #define GDI_LINEAR_FRAME_MAP 0
0199 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
0200 
0201 struct coda_ctx;
0202 
0203 struct coda_context_ops {
0204     int (*queue_init)(void *priv, struct vb2_queue *src_vq,
0205               struct vb2_queue *dst_vq);
0206     int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
0207     int (*start_streaming)(struct coda_ctx *ctx);
0208     int (*prepare_run)(struct coda_ctx *ctx);
0209     void (*finish_run)(struct coda_ctx *ctx);
0210     void (*run_timeout)(struct coda_ctx *ctx);
0211     void (*seq_init_work)(struct work_struct *work);
0212     void (*seq_end_work)(struct work_struct *work);
0213     void (*release)(struct coda_ctx *ctx);
0214 };
0215 
0216 struct coda_internal_frame {
0217     struct coda_aux_buf     buf;
0218     struct coda_buffer_meta     meta;
0219     u32             type;
0220     u32             error;
0221 };
0222 
0223 struct coda_ctx {
0224     struct coda_dev         *dev;
0225     struct mutex            buffer_mutex;
0226     struct work_struct      pic_run_work;
0227     struct work_struct      seq_init_work;
0228     struct work_struct      seq_end_work;
0229     struct completion       completion;
0230     const struct coda_video_device  *cvd;
0231     const struct coda_context_ops   *ops;
0232     int             aborting;
0233     int             initialized;
0234     int             streamon_out;
0235     int             streamon_cap;
0236     u32             qsequence;
0237     u32             osequence;
0238     u32             sequence_offset;
0239     struct coda_q_data      q_data[2];
0240     enum coda_inst_type     inst_type;
0241     const struct coda_codec     *codec;
0242     enum v4l2_colorspace        colorspace;
0243     enum v4l2_xfer_func     xfer_func;
0244     enum v4l2_ycbcr_encoding    ycbcr_enc;
0245     enum v4l2_quantization      quantization;
0246     struct coda_params      params;
0247     struct v4l2_ctrl_handler    ctrls;
0248     struct v4l2_ctrl        *h264_profile_ctrl;
0249     struct v4l2_ctrl        *h264_level_ctrl;
0250     struct v4l2_ctrl        *mpeg2_profile_ctrl;
0251     struct v4l2_ctrl        *mpeg2_level_ctrl;
0252     struct v4l2_ctrl        *mpeg4_profile_ctrl;
0253     struct v4l2_ctrl        *mpeg4_level_ctrl;
0254     struct v4l2_ctrl        *mb_err_cnt_ctrl;
0255     struct v4l2_fh          fh;
0256     int             gopcounter;
0257     int             runcounter;
0258     int             jpeg_ecs_offset;
0259     char                vpu_header[3][64];
0260     int             vpu_header_size[3];
0261     struct kfifo            bitstream_fifo;
0262     struct mutex            bitstream_mutex;
0263     struct coda_aux_buf     bitstream;
0264     bool                hold;
0265     struct coda_aux_buf     parabuf;
0266     struct coda_aux_buf     psbuf;
0267     struct coda_aux_buf     slicebuf;
0268     struct coda_internal_frame  internal_frames[CODA_MAX_FRAMEBUFFERS];
0269     struct list_head        buffer_meta_list;
0270     spinlock_t          buffer_meta_lock;
0271     int             num_metas;
0272     unsigned int            first_frame_sequence;
0273     struct coda_aux_buf     workbuf;
0274     int             num_internal_frames;
0275     int             idx;
0276     int             reg_idx;
0277     struct coda_iram_info       iram_info;
0278     int             tiled_map_type;
0279     u32             bit_stream_param;
0280     u32             frm_dis_flg;
0281     u32             frame_mem_ctrl;
0282     u32             para_change;
0283     int             display_idx;
0284     struct dentry           *debugfs_entry;
0285     bool                use_bit;
0286     bool                use_vdoa;
0287     struct vdoa_ctx         *vdoa;
0288     /*
0289      * wakeup mutex used to serialize encoder stop command and finish_run,
0290      * ensures that finish_run always either flags the last returned buffer
0291      * or wakes up the capture queue to signal EOS afterwards.
0292      */
0293     struct mutex            wakeup_mutex;
0294 };
0295 
0296 extern int coda_debug;
0297 
0298 #define coda_dbg(level, ctx, fmt, arg...)               \
0299     do {                                \
0300         if (coda_debug >= (level))              \
0301             v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
0302              "%u: " fmt, (ctx)->idx, ##arg);        \
0303     } while (0)
0304 
0305 void coda_write(struct coda_dev *dev, u32 data, u32 reg);
0306 unsigned int coda_read(struct coda_dev *dev, u32 reg);
0307 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
0308              struct vb2_v4l2_buffer *buf, unsigned int reg_y);
0309 
0310 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
0311                size_t size, const char *name, struct dentry *parent);
0312 void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
0313 
0314 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
0315                 struct vb2_queue *dst_vq);
0316 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
0317                 struct vb2_queue *dst_vq);
0318 
0319 int coda_hw_reset(struct coda_ctx *ctx);
0320 
0321 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
0322 
0323 void coda_set_gdi_regs(struct coda_ctx *ctx);
0324 
0325 static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
0326                          enum v4l2_buf_type type)
0327 {
0328     switch (type) {
0329     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
0330         return &(ctx->q_data[V4L2_M2M_SRC]);
0331     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
0332         return &(ctx->q_data[V4L2_M2M_DST]);
0333     default:
0334         return NULL;
0335     }
0336 }
0337 
0338 const char *coda_product_name(int product);
0339 
0340 int coda_check_firmware(struct coda_dev *dev);
0341 
0342 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
0343 {
0344     return kfifo_len(&ctx->bitstream_fifo);
0345 }
0346 
0347 /*
0348  * The bitstream prefetcher needs to read at least 2 256 byte periods past
0349  * the desired bitstream position for all data to reach the decoder.
0350  */
0351 static inline bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx,
0352                          unsigned int pos)
0353 {
0354     return (int)(ctx->bitstream_fifo.kfifo.in - ALIGN(pos, 256)) > 512;
0355 }
0356 
0357 bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx, unsigned int pos);
0358 int coda_bitstream_flush(struct coda_ctx *ctx);
0359 
0360 void coda_bit_stream_end_flag(struct coda_ctx *ctx);
0361 
0362 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
0363                enum vb2_buffer_state state);
0364 
0365 int coda_h264_filler_nal(int size, char *p);
0366 int coda_h264_padding(int size, char *p);
0367 int coda_h264_profile(int profile_idc);
0368 int coda_h264_level(int level_idc);
0369 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
0370 int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
0371             int *size, int max_size);
0372 
0373 int coda_mpeg2_profile(int profile_idc);
0374 int coda_mpeg2_level(int level_idc);
0375 u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
0376 int coda_mpeg4_profile(int profile_idc);
0377 int coda_mpeg4_level(int level_idc);
0378 u32 coda_mpeg4_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
0379 
0380 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
0381                      u8 level_idc);
0382 
0383 static inline int coda_jpeg_scale(int src, int dst)
0384 {
0385     return (dst <= src / 8) ? 3 :
0386            (dst <= src / 4) ? 2 :
0387            (dst <= src / 2) ? 1 : 0;
0388 }
0389 
0390 bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
0391 int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb);
0392 int coda_jpeg_write_tables(struct coda_ctx *ctx);
0393 void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
0394 
0395 extern const struct coda_context_ops coda_bit_encode_ops;
0396 extern const struct coda_context_ops coda_bit_decode_ops;
0397 extern const struct coda_context_ops coda9_jpeg_encode_ops;
0398 extern const struct coda_context_ops coda9_jpeg_decode_ops;
0399 
0400 irqreturn_t coda_irq_handler(int irq, void *data);
0401 irqreturn_t coda9_jpeg_irq_handler(int irq, void *data);
0402 
0403 #endif /* __CODA_H__ */