linux/drivers/staging/media/hantro/hantro_hw.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/*
   3 * Hantro VPU codec driver
   4 *
   5 * Copyright 2018 Google LLC.
   6 *      Tomasz Figa <tfiga@chromium.org>
   7 */
   8
   9#ifndef HANTRO_HW_H_
  10#define HANTRO_HW_H_
  11
  12#include <linux/interrupt.h>
  13#include <linux/v4l2-controls.h>
  14#include <media/v4l2-ctrls.h>
  15#include <media/videobuf2-core.h>
  16
  17#define DEC_8190_ALIGN_MASK     0x07U
  18
  19#define MB_DIM                  16
  20#define MB_WIDTH(w)             DIV_ROUND_UP(w, MB_DIM)
  21#define MB_HEIGHT(h)            DIV_ROUND_UP(h, MB_DIM)
  22
  23#define NUM_REF_PICTURES        (V4L2_HEVC_DPB_ENTRIES_NUM_MAX + 1)
  24
  25struct hantro_dev;
  26struct hantro_ctx;
  27struct hantro_buf;
  28struct hantro_variant;
  29
  30/**
  31 * struct hantro_aux_buf - auxiliary DMA buffer for hardware data
  32 *
  33 * @cpu:        CPU pointer to the buffer.
  34 * @dma:        DMA address of the buffer.
  35 * @size:       Size of the buffer.
  36 * @attrs:      Attributes of the DMA mapping.
  37 */
  38struct hantro_aux_buf {
  39        void *cpu;
  40        dma_addr_t dma;
  41        size_t size;
  42        unsigned long attrs;
  43};
  44
  45/**
  46 * struct hantro_jpeg_enc_hw_ctx
  47 *
  48 * @bounce_buffer:      Bounce buffer
  49 */
  50struct hantro_jpeg_enc_hw_ctx {
  51        struct hantro_aux_buf bounce_buffer;
  52};
  53
  54/* Max. number of entries in the DPB (HW limitation). */
  55#define HANTRO_H264_DPB_SIZE            16
  56
  57/**
  58 * struct hantro_h264_dec_ctrls
  59 *
  60 * @decode:     Decode params
  61 * @scaling:    Scaling info
  62 * @sps:        SPS info
  63 * @pps:        PPS info
  64 */
  65struct hantro_h264_dec_ctrls {
  66        const struct v4l2_ctrl_h264_decode_params *decode;
  67        const struct v4l2_ctrl_h264_scaling_matrix *scaling;
  68        const struct v4l2_ctrl_h264_sps *sps;
  69        const struct v4l2_ctrl_h264_pps *pps;
  70};
  71
  72/**
  73 * struct hantro_h264_dec_reflists
  74 *
  75 * @p:          P reflist
  76 * @b0:         B0 reflist
  77 * @b1:         B1 reflist
  78 */
  79struct hantro_h264_dec_reflists {
  80        u8 p[HANTRO_H264_DPB_SIZE];
  81        u8 b0[HANTRO_H264_DPB_SIZE];
  82        u8 b1[HANTRO_H264_DPB_SIZE];
  83};
  84
  85/**
  86 * struct hantro_h264_dec_hw_ctx
  87 *
  88 * @priv:       Private auxiliary buffer for hardware.
  89 * @dpb:        DPB
  90 * @reflists:   P/B0/B1 reflists
  91 * @ctrls:      V4L2 controls attached to a run
  92 * @dpb_longterm: DPB long-term
  93 * @dpb_valid:    DPB valid
  94 */
  95struct hantro_h264_dec_hw_ctx {
  96        struct hantro_aux_buf priv;
  97        struct v4l2_h264_dpb_entry dpb[HANTRO_H264_DPB_SIZE];
  98        struct hantro_h264_dec_reflists reflists;
  99        struct hantro_h264_dec_ctrls ctrls;
 100        u32 dpb_longterm;
 101        u32 dpb_valid;
 102};
 103
 104/**
 105 * struct hantro_hevc_dec_ctrls
 106 * @decode_params: Decode params
 107 * @sps:        SPS info
 108 * @pps:        PPS info
 109 * @hevc_hdr_skip_length: the number of data (in bits) to skip in the
 110 *                        slice segment header syntax after 'slice type'
 111 *                        token
 112 */
 113struct hantro_hevc_dec_ctrls {
 114        const struct v4l2_ctrl_hevc_decode_params *decode_params;
 115        const struct v4l2_ctrl_hevc_sps *sps;
 116        const struct v4l2_ctrl_hevc_pps *pps;
 117        u32 hevc_hdr_skip_length;
 118};
 119
 120/**
 121 * struct hantro_hevc_dec_hw_ctx
 122 * @tile_sizes:         Tile sizes buffer
 123 * @tile_filter:        Tile vertical filter buffer
 124 * @tile_sao:           Tile SAO buffer
 125 * @tile_bsd:           Tile BSD control buffer
 126 * @ref_bufs:           Internal reference buffers
 127 * @ref_bufs_poc:       Internal reference buffers picture order count
 128 * @ref_bufs_used:      Bitfield of used reference buffers
 129 * @ctrls:              V4L2 controls attached to a run
 130 * @num_tile_cols_allocated: number of allocated tiles
 131 */
 132struct hantro_hevc_dec_hw_ctx {
 133        struct hantro_aux_buf tile_sizes;
 134        struct hantro_aux_buf tile_filter;
 135        struct hantro_aux_buf tile_sao;
 136        struct hantro_aux_buf tile_bsd;
 137        struct hantro_aux_buf ref_bufs[NUM_REF_PICTURES];
 138        int ref_bufs_poc[NUM_REF_PICTURES];
 139        u32 ref_bufs_used;
 140        struct hantro_hevc_dec_ctrls ctrls;
 141        unsigned int num_tile_cols_allocated;
 142};
 143
 144/**
 145 * struct hantro_mpeg2_dec_hw_ctx
 146 *
 147 * @qtable:             Quantization table
 148 */
 149struct hantro_mpeg2_dec_hw_ctx {
 150        struct hantro_aux_buf qtable;
 151};
 152
 153/**
 154 * struct hantro_vp8_dec_hw_ctx
 155 *
 156 * @segment_map:        Segment map buffer.
 157 * @prob_tbl:           Probability table buffer.
 158 */
 159struct hantro_vp8_dec_hw_ctx {
 160        struct hantro_aux_buf segment_map;
 161        struct hantro_aux_buf prob_tbl;
 162};
 163
 164/**
 165 * struct hantro_postproc_ctx
 166 *
 167 * @dec_q:              References buffers, in decoder format.
 168 */
 169struct hantro_postproc_ctx {
 170        struct hantro_aux_buf dec_q[VB2_MAX_FRAME];
 171};
 172
 173/**
 174 * struct hantro_codec_ops - codec mode specific operations
 175 *
 176 * @init:       If needed, can be used for initialization.
 177 *              Optional and called from process context.
 178 * @exit:       If needed, can be used to undo the .init phase.
 179 *              Optional and called from process context.
 180 * @run:        Start single {en,de)coding job. Called from atomic context
 181 *              to indicate that a pair of buffers is ready and the hardware
 182 *              should be programmed and started. Returns zero if OK, a
 183 *              negative value in error cases.
 184 * @done:       Read back processing results and additional data from hardware.
 185 * @reset:      Reset the hardware in case of a timeout.
 186 */
 187struct hantro_codec_ops {
 188        int (*init)(struct hantro_ctx *ctx);
 189        void (*exit)(struct hantro_ctx *ctx);
 190        int (*run)(struct hantro_ctx *ctx);
 191        void (*done)(struct hantro_ctx *ctx);
 192        void (*reset)(struct hantro_ctx *ctx);
 193};
 194
 195/**
 196 * enum hantro_enc_fmt - source format ID for hardware registers.
 197 *
 198 * @ROCKCHIP_VPU_ENC_FMT_YUV420P: Y/CbCr 4:2:0 planar format
 199 * @ROCKCHIP_VPU_ENC_FMT_YUV420SP: Y/CbCr 4:2:0 semi-planar format
 200 * @ROCKCHIP_VPU_ENC_FMT_YUYV422: YUV 4:2:2 packed format (YUYV)
 201 * @ROCKCHIP_VPU_ENC_FMT_UYVY422: YUV 4:2:2 packed format (UYVY)
 202 */
 203enum hantro_enc_fmt {
 204        ROCKCHIP_VPU_ENC_FMT_YUV420P = 0,
 205        ROCKCHIP_VPU_ENC_FMT_YUV420SP = 1,
 206        ROCKCHIP_VPU_ENC_FMT_YUYV422 = 2,
 207        ROCKCHIP_VPU_ENC_FMT_UYVY422 = 3,
 208};
 209
 210extern const struct hantro_variant imx8mq_vpu_g2_variant;
 211extern const struct hantro_variant imx8mq_vpu_variant;
 212extern const struct hantro_variant px30_vpu_variant;
 213extern const struct hantro_variant rk3036_vpu_variant;
 214extern const struct hantro_variant rk3066_vpu_variant;
 215extern const struct hantro_variant rk3288_vpu_variant;
 216extern const struct hantro_variant rk3328_vpu_variant;
 217extern const struct hantro_variant rk3399_vpu_variant;
 218extern const struct hantro_variant sama5d4_vdec_variant;
 219
 220extern const struct hantro_postproc_regs hantro_g1_postproc_regs;
 221
 222extern const u32 hantro_vp8_dec_mc_filter[8][6];
 223
 224void hantro_watchdog(struct work_struct *work);
 225void hantro_run(struct hantro_ctx *ctx);
 226void hantro_irq_done(struct hantro_dev *vpu,
 227                     enum vb2_buffer_state result);
 228void hantro_start_prepare_run(struct hantro_ctx *ctx);
 229void hantro_end_prepare_run(struct hantro_ctx *ctx);
 230
 231irqreturn_t hantro_g1_irq(int irq, void *dev_id);
 232void hantro_g1_reset(struct hantro_ctx *ctx);
 233
 234int hantro_h1_jpeg_enc_run(struct hantro_ctx *ctx);
 235int rockchip_vpu2_jpeg_enc_run(struct hantro_ctx *ctx);
 236int hantro_jpeg_enc_init(struct hantro_ctx *ctx);
 237void hantro_jpeg_enc_exit(struct hantro_ctx *ctx);
 238void hantro_jpeg_enc_done(struct hantro_ctx *ctx);
 239
 240dma_addr_t hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
 241                                   unsigned int dpb_idx);
 242u16 hantro_h264_get_ref_nbr(struct hantro_ctx *ctx,
 243                            unsigned int dpb_idx);
 244int hantro_h264_dec_prepare_run(struct hantro_ctx *ctx);
 245int rockchip_vpu2_h264_dec_run(struct hantro_ctx *ctx);
 246int hantro_g1_h264_dec_run(struct hantro_ctx *ctx);
 247int hantro_h264_dec_init(struct hantro_ctx *ctx);
 248void hantro_h264_dec_exit(struct hantro_ctx *ctx);
 249
 250int hantro_hevc_dec_init(struct hantro_ctx *ctx);
 251void hantro_hevc_dec_exit(struct hantro_ctx *ctx);
 252int hantro_g2_hevc_dec_run(struct hantro_ctx *ctx);
 253int hantro_hevc_dec_prepare_run(struct hantro_ctx *ctx);
 254dma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx, int poc);
 255void hantro_hevc_ref_remove_unused(struct hantro_ctx *ctx);
 256size_t hantro_hevc_chroma_offset(const struct v4l2_ctrl_hevc_sps *sps);
 257size_t hantro_hevc_motion_vectors_offset(const struct v4l2_ctrl_hevc_sps *sps);
 258
 259static inline size_t
 260hantro_h264_mv_size(unsigned int width, unsigned int height)
 261{
 262        /*
 263         * A decoded 8-bit 4:2:0 NV12 frame may need memory for up to
 264         * 448 bytes per macroblock with additional 32 bytes on
 265         * multi-core variants.
 266         *
 267         * The H264 decoder needs extra space on the output buffers
 268         * to store motion vectors. This is needed for reference
 269         * frames and only if the format is non-post-processed NV12.
 270         *
 271         * Memory layout is as follow:
 272         *
 273         * +---------------------------+
 274         * | Y-plane   256 bytes x MBs |
 275         * +---------------------------+
 276         * | UV-plane  128 bytes x MBs |
 277         * +---------------------------+
 278         * | MV buffer  64 bytes x MBs |
 279         * +---------------------------+
 280         * | MC sync          32 bytes |
 281         * +---------------------------+
 282         */
 283        return 64 * MB_WIDTH(width) * MB_WIDTH(height) + 32;
 284}
 285
 286int hantro_g1_mpeg2_dec_run(struct hantro_ctx *ctx);
 287int rockchip_vpu2_mpeg2_dec_run(struct hantro_ctx *ctx);
 288void hantro_mpeg2_dec_copy_qtable(u8 *qtable,
 289                                  const struct v4l2_ctrl_mpeg2_quantisation *ctrl);
 290int hantro_mpeg2_dec_init(struct hantro_ctx *ctx);
 291void hantro_mpeg2_dec_exit(struct hantro_ctx *ctx);
 292
 293int hantro_g1_vp8_dec_run(struct hantro_ctx *ctx);
 294int rockchip_vpu2_vp8_dec_run(struct hantro_ctx *ctx);
 295int hantro_vp8_dec_init(struct hantro_ctx *ctx);
 296void hantro_vp8_dec_exit(struct hantro_ctx *ctx);
 297void hantro_vp8_prob_update(struct hantro_ctx *ctx,
 298                            const struct v4l2_ctrl_vp8_frame *hdr);
 299
 300#endif /* HANTRO_HW_H_ */
 301