/* SPDX-License-Identifier: GPL-2.0 */ /* * Hantro VPU codec driver * * Copyright 2018 Google LLC. * Tomasz Figa <[email protected]> * * Based on s5p-mfc driver by Samsung Electronics Co., Ltd. * Copyright (C) 2011 Samsung Electronics Co., Ltd. */ #ifndef HANTRO_H_ #define HANTRO_H_ #include <linux/platform_device.h> #include <linux/videodev2.h> #include <linux/wait.h> #include <linux/clk.h> #include <linux/reset.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-device.h> #include <media/v4l2-ioctl.h> #include <media/v4l2-mem2mem.h> #include <media/videobuf2-core.h> #include <media/videobuf2-dma-contig.h> #include "hantro_hw.h" struct hantro_ctx; struct hantro_codec_ops; struct hantro_postproc_ops; #define HANTRO_JPEG_ENCODER … #define HANTRO_ENCODERS … #define HANTRO_MPEG2_DECODER … #define HANTRO_VP8_DECODER … #define HANTRO_H264_DECODER … #define HANTRO_HEVC_DECODER … #define HANTRO_VP9_DECODER … #define HANTRO_AV1_DECODER … #define HANTRO_DECODERS … /** * struct hantro_irq - irq handler and name * * @name: irq name for device tree lookup * @handler: interrupt handler */ struct hantro_irq { … }; /** * struct hantro_variant - information about VPU hardware variant * * @enc_offset: Offset from VPU base to encoder registers. * @dec_offset: Offset from VPU base to decoder registers. * @enc_fmts: Encoder formats. * @num_enc_fmts: Number of encoder formats. * @dec_fmts: Decoder formats. * @num_dec_fmts: Number of decoder formats. * @postproc_fmts: Post-processor formats. * @num_postproc_fmts: Number of post-processor formats. * @postproc_ops: Post-processor ops. * @codec: Supported codecs * @codec_ops: Codec ops. * @init: Initialize hardware, optional. * @runtime_resume: reenable hardware after power gating, optional. * @irqs: array of irq names and interrupt handlers * @num_irqs: number of irqs in the array * @clk_names: array of clock names * @num_clocks: number of clocks in the array * @reg_names: array of register range names * @num_regs: number of register range names in the array * @double_buffer: core needs double buffering * @legacy_regs: core uses legacy register set * @late_postproc: postproc must be set up at the end of the job */ struct hantro_variant { … }; /** * enum hantro_codec_mode - codec operating mode. * @HANTRO_MODE_NONE: No operating mode. Used for RAW video formats. * @HANTRO_MODE_JPEG_ENC: JPEG encoder. * @HANTRO_MODE_H264_DEC: H264 decoder. * @HANTRO_MODE_MPEG2_DEC: MPEG-2 decoder. * @HANTRO_MODE_VP8_DEC: VP8 decoder. * @HANTRO_MODE_HEVC_DEC: HEVC decoder. * @HANTRO_MODE_VP9_DEC: VP9 decoder. * @HANTRO_MODE_AV1_DEC: AV1 decoder */ enum hantro_codec_mode { … }; /* * struct hantro_ctrl - helper type to declare supported controls * @codec: codec id this control belong to (HANTRO_JPEG_ENCODER, etc.) * @cfg: control configuration */ struct hantro_ctrl { … }; /* * struct hantro_func - Hantro VPU functionality * * @id: processing functionality ID (can be * %MEDIA_ENT_F_PROC_VIDEO_ENCODER or * %MEDIA_ENT_F_PROC_VIDEO_DECODER) * @vdev: &struct video_device that exposes the encoder or * decoder functionality * @source_pad: &struct media_pad with the source pad. * @sink: &struct media_entity pointer with the sink entity * @sink_pad: &struct media_pad with the sink pad. * @proc: &struct media_entity pointer with the M2M device itself. * @proc_pads: &struct media_pad with the @proc pads. * @intf_devnode: &struct media_intf devnode pointer with the interface * with controls the M2M device. * * Contains everything needed to attach the video device to the media device. */ struct hantro_func { … }; static inline struct hantro_func * hantro_vdev_to_func(struct video_device *vdev) { … } /** * struct hantro_dev - driver data * @v4l2_dev: V4L2 device to register video devices for. * @m2m_dev: mem2mem device associated to this device. * @mdev: media device associated to this device. * @encoder: encoder functionality. * @decoder: decoder functionality. * @pdev: Pointer to VPU platform device. * @dev: Pointer to device for convenient logging using * dev_ macros. * @clocks: Array of clock handles. * @resets: Array of reset handles. * @reg_bases: Mapped addresses of VPU registers. * @enc_base: Mapped address of VPU encoder register for convenience. * @dec_base: Mapped address of VPU decoder register for convenience. * @ctrl_base: Mapped address of VPU control block. * @vpu_mutex: Mutex to synchronize V4L2 calls. * @irqlock: Spinlock to synchronize access to data structures * shared with interrupt handlers. * @variant: Hardware variant-specific parameters. * @watchdog_work: Delayed work for hardware timeout handling. */ struct hantro_dev { … }; /** * struct hantro_ctx - Context (instance) private data. * * @dev: VPU driver data to which the context belongs. * @fh: V4L2 file handler. * @is_encoder: Decoder or encoder context? * * @sequence_cap: Sequence counter for capture queue * @sequence_out: Sequence counter for output queue * * @vpu_src_fmt: Descriptor of active source format. * @src_fmt: V4L2 pixel format of active source format. * @vpu_dst_fmt: Descriptor of active destination format. * @dst_fmt: V4L2 pixel format of active destination format. * * @ctrl_handler: Control handler used to register controls. * @jpeg_quality: User-specified JPEG compression quality. * @bit_depth: Bit depth of current frame * @need_postproc: Set to true if the bitstream features require to * use the post-processor. * * @codec_ops: Set of operations related to codec mode. * @postproc: Post-processing context. * @h264_dec: H.264-decoding context. * @mpeg2_dec: MPEG-2-decoding context. * @vp8_dec: VP8-decoding context. * @hevc_dec: HEVC-decoding context. * @vp9_dec: VP9-decoding context. * @av1_dec: AV1-decoding context. */ struct hantro_ctx { … }; /** * struct hantro_fmt - information about supported video formats. * @name: Human readable name of the format. * @fourcc: FourCC code of the format. See V4L2_PIX_FMT_*. * @codec_mode: Codec mode related to this format. See * enum hantro_codec_mode. * @header_size: Optional header size. Currently used by JPEG encoder. * @max_depth: Maximum depth, for bitstream formats * @enc_fmt: Format identifier for encoder registers. * @frmsize: Supported range of frame sizes (only for bitstream formats). * @postprocessed: Indicates if this format needs the post-processor. * @match_depth: Indicates if format bit depth must match video bit depth */ struct hantro_fmt { … }; struct hantro_reg { … }; struct hantro_postproc_regs { … }; struct hantro_vp9_decoded_buffer_info { … }; struct hantro_decoded_buffer { … }; /* Logging helpers */ /** * DOC: hantro_debug: Module parameter to control level of debugging messages. * * Level of debugging messages can be controlled by bits of * module parameter called "debug". Meaning of particular * bits is as follows: * * bit 0 - global information: mode, size, init, release * bit 1 - each run start/result information * bit 2 - contents of small controls from userspace * bit 3 - contents of big controls from userspace * bit 4 - detail fmt, ctrl, buffer q/dq information * bit 5 - detail function enter/leave trace information * bit 6 - register write/read information */ extern int hantro_debug; #define vpu_debug(level, fmt, args...) … #define vpu_err(fmt, args...) … /* Structure access helpers. */ static __always_inline struct hantro_ctx *fh_to_ctx(struct v4l2_fh *fh) { … } /* Register accessors. */ static __always_inline void vepu_write_relaxed(struct hantro_dev *vpu, u32 val, u32 reg) { … } static __always_inline void vepu_write(struct hantro_dev *vpu, u32 val, u32 reg) { … } static __always_inline u32 vepu_read(struct hantro_dev *vpu, u32 reg) { … } static __always_inline void vdpu_write_relaxed(struct hantro_dev *vpu, u32 val, u32 reg) { … } static __always_inline void vdpu_write(struct hantro_dev *vpu, u32 val, u32 reg) { … } static __always_inline void hantro_write_addr(struct hantro_dev *vpu, unsigned long offset, dma_addr_t addr) { … } static __always_inline u32 vdpu_read(struct hantro_dev *vpu, u32 reg) { … } static __always_inline u32 vdpu_read_mask(struct hantro_dev *vpu, const struct hantro_reg *reg, u32 val) { … } static __always_inline void hantro_reg_write(struct hantro_dev *vpu, const struct hantro_reg *reg, u32 val) { … } static __always_inline void hantro_reg_write_relaxed(struct hantro_dev *vpu, const struct hantro_reg *reg, u32 val) { … } void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id); dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts); static inline struct vb2_v4l2_buffer * hantro_get_src_buf(struct hantro_ctx *ctx) { … } static inline struct vb2_v4l2_buffer * hantro_get_dst_buf(struct hantro_ctx *ctx) { … } bool hantro_needs_postproc(const struct hantro_ctx *ctx, const struct hantro_fmt *fmt); dma_addr_t hantro_postproc_get_dec_buf_addr(struct hantro_ctx *ctx, int index); static inline dma_addr_t hantro_get_dec_buf_addr(struct hantro_ctx *ctx, struct vb2_buffer *vb) { … } static inline struct hantro_decoded_buffer * vb2_to_hantro_decoded_buf(struct vb2_buffer *buf) { … } void hantro_postproc_disable(struct hantro_ctx *ctx); void hantro_postproc_enable(struct hantro_ctx *ctx); int hantro_postproc_init(struct hantro_ctx *ctx); void hantro_postproc_free(struct hantro_ctx *ctx); int hanto_postproc_enum_framesizes(struct hantro_ctx *ctx, struct v4l2_frmsizeenum *fsize); #endif /* HANTRO_H_ */