hantro.h (14068B)
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 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd. 9 * Copyright (C) 2011 Samsung Electronics Co., Ltd. 10 */ 11 12#ifndef HANTRO_H_ 13#define HANTRO_H_ 14 15#include <linux/platform_device.h> 16#include <linux/videodev2.h> 17#include <linux/wait.h> 18#include <linux/clk.h> 19#include <linux/reset.h> 20 21#include <media/v4l2-ctrls.h> 22#include <media/v4l2-device.h> 23#include <media/v4l2-ioctl.h> 24#include <media/v4l2-mem2mem.h> 25#include <media/videobuf2-core.h> 26#include <media/videobuf2-dma-contig.h> 27 28#include "hantro_hw.h" 29 30struct hantro_ctx; 31struct hantro_codec_ops; 32struct hantro_postproc_ops; 33 34#define HANTRO_JPEG_ENCODER BIT(0) 35#define HANTRO_ENCODERS 0x0000ffff 36#define HANTRO_MPEG2_DECODER BIT(16) 37#define HANTRO_VP8_DECODER BIT(17) 38#define HANTRO_H264_DECODER BIT(18) 39#define HANTRO_HEVC_DECODER BIT(19) 40#define HANTRO_VP9_DECODER BIT(20) 41#define HANTRO_DECODERS 0xffff0000 42 43/** 44 * struct hantro_irq - irq handler and name 45 * 46 * @name: irq name for device tree lookup 47 * @handler: interrupt handler 48 */ 49struct hantro_irq { 50 const char *name; 51 irqreturn_t (*handler)(int irq, void *priv); 52}; 53 54/** 55 * struct hantro_variant - information about VPU hardware variant 56 * 57 * @enc_offset: Offset from VPU base to encoder registers. 58 * @dec_offset: Offset from VPU base to decoder registers. 59 * @enc_fmts: Encoder formats. 60 * @num_enc_fmts: Number of encoder formats. 61 * @dec_fmts: Decoder formats. 62 * @num_dec_fmts: Number of decoder formats. 63 * @postproc_fmts: Post-processor formats. 64 * @num_postproc_fmts: Number of post-processor formats. 65 * @postproc_ops: Post-processor ops. 66 * @codec: Supported codecs 67 * @codec_ops: Codec ops. 68 * @init: Initialize hardware, optional. 69 * @runtime_resume: reenable hardware after power gating, optional. 70 * @irqs: array of irq names and interrupt handlers 71 * @num_irqs: number of irqs in the array 72 * @clk_names: array of clock names 73 * @num_clocks: number of clocks in the array 74 * @reg_names: array of register range names 75 * @num_regs: number of register range names in the array 76 * @double_buffer: core needs double buffering 77 * @legacy_regs: core uses legacy register set 78 * @late_postproc: postproc must be set up at the end of the job 79 */ 80struct hantro_variant { 81 unsigned int enc_offset; 82 unsigned int dec_offset; 83 const struct hantro_fmt *enc_fmts; 84 unsigned int num_enc_fmts; 85 const struct hantro_fmt *dec_fmts; 86 unsigned int num_dec_fmts; 87 const struct hantro_fmt *postproc_fmts; 88 unsigned int num_postproc_fmts; 89 const struct hantro_postproc_ops *postproc_ops; 90 unsigned int codec; 91 const struct hantro_codec_ops *codec_ops; 92 int (*init)(struct hantro_dev *vpu); 93 int (*runtime_resume)(struct hantro_dev *vpu); 94 const struct hantro_irq *irqs; 95 int num_irqs; 96 const char * const *clk_names; 97 int num_clocks; 98 const char * const *reg_names; 99 int num_regs; 100 unsigned int double_buffer : 1; 101 unsigned int legacy_regs : 1; 102 unsigned int late_postproc : 1; 103}; 104 105/** 106 * enum hantro_codec_mode - codec operating mode. 107 * @HANTRO_MODE_NONE: No operating mode. Used for RAW video formats. 108 * @HANTRO_MODE_JPEG_ENC: JPEG encoder. 109 * @HANTRO_MODE_H264_DEC: H264 decoder. 110 * @HANTRO_MODE_MPEG2_DEC: MPEG-2 decoder. 111 * @HANTRO_MODE_VP8_DEC: VP8 decoder. 112 * @HANTRO_MODE_HEVC_DEC: HEVC decoder. 113 * @HANTRO_MODE_VP9_DEC: VP9 decoder. 114 */ 115enum hantro_codec_mode { 116 HANTRO_MODE_NONE = -1, 117 HANTRO_MODE_JPEG_ENC, 118 HANTRO_MODE_H264_DEC, 119 HANTRO_MODE_MPEG2_DEC, 120 HANTRO_MODE_VP8_DEC, 121 HANTRO_MODE_HEVC_DEC, 122 HANTRO_MODE_VP9_DEC, 123}; 124 125/* 126 * struct hantro_ctrl - helper type to declare supported controls 127 * @codec: codec id this control belong to (HANTRO_JPEG_ENCODER, etc.) 128 * @cfg: control configuration 129 */ 130struct hantro_ctrl { 131 unsigned int codec; 132 struct v4l2_ctrl_config cfg; 133}; 134 135/* 136 * struct hantro_func - Hantro VPU functionality 137 * 138 * @id: processing functionality ID (can be 139 * %MEDIA_ENT_F_PROC_VIDEO_ENCODER or 140 * %MEDIA_ENT_F_PROC_VIDEO_DECODER) 141 * @vdev: &struct video_device that exposes the encoder or 142 * decoder functionality 143 * @source_pad: &struct media_pad with the source pad. 144 * @sink: &struct media_entity pointer with the sink entity 145 * @sink_pad: &struct media_pad with the sink pad. 146 * @proc: &struct media_entity pointer with the M2M device itself. 147 * @proc_pads: &struct media_pad with the @proc pads. 148 * @intf_devnode: &struct media_intf devnode pointer with the interface 149 * with controls the M2M device. 150 * 151 * Contains everything needed to attach the video device to the media device. 152 */ 153struct hantro_func { 154 unsigned int id; 155 struct video_device vdev; 156 struct media_pad source_pad; 157 struct media_entity sink; 158 struct media_pad sink_pad; 159 struct media_entity proc; 160 struct media_pad proc_pads[2]; 161 struct media_intf_devnode *intf_devnode; 162}; 163 164static inline struct hantro_func * 165hantro_vdev_to_func(struct video_device *vdev) 166{ 167 return container_of(vdev, struct hantro_func, vdev); 168} 169 170/** 171 * struct hantro_dev - driver data 172 * @v4l2_dev: V4L2 device to register video devices for. 173 * @m2m_dev: mem2mem device associated to this device. 174 * @mdev: media device associated to this device. 175 * @encoder: encoder functionality. 176 * @decoder: decoder functionality. 177 * @pdev: Pointer to VPU platform device. 178 * @dev: Pointer to device for convenient logging using 179 * dev_ macros. 180 * @clocks: Array of clock handles. 181 * @resets: Array of reset handles. 182 * @reg_bases: Mapped addresses of VPU registers. 183 * @enc_base: Mapped address of VPU encoder register for convenience. 184 * @dec_base: Mapped address of VPU decoder register for convenience. 185 * @ctrl_base: Mapped address of VPU control block. 186 * @vpu_mutex: Mutex to synchronize V4L2 calls. 187 * @irqlock: Spinlock to synchronize access to data structures 188 * shared with interrupt handlers. 189 * @variant: Hardware variant-specific parameters. 190 * @watchdog_work: Delayed work for hardware timeout handling. 191 */ 192struct hantro_dev { 193 struct v4l2_device v4l2_dev; 194 struct v4l2_m2m_dev *m2m_dev; 195 struct media_device mdev; 196 struct hantro_func *encoder; 197 struct hantro_func *decoder; 198 struct platform_device *pdev; 199 struct device *dev; 200 struct clk_bulk_data *clocks; 201 struct reset_control *resets; 202 void __iomem **reg_bases; 203 void __iomem *enc_base; 204 void __iomem *dec_base; 205 void __iomem *ctrl_base; 206 207 struct mutex vpu_mutex; /* video_device lock */ 208 spinlock_t irqlock; 209 const struct hantro_variant *variant; 210 struct delayed_work watchdog_work; 211}; 212 213/** 214 * struct hantro_ctx - Context (instance) private data. 215 * 216 * @dev: VPU driver data to which the context belongs. 217 * @fh: V4L2 file handler. 218 * @is_encoder: Decoder or encoder context? 219 * 220 * @sequence_cap: Sequence counter for capture queue 221 * @sequence_out: Sequence counter for output queue 222 * 223 * @vpu_src_fmt: Descriptor of active source format. 224 * @src_fmt: V4L2 pixel format of active source format. 225 * @vpu_dst_fmt: Descriptor of active destination format. 226 * @dst_fmt: V4L2 pixel format of active destination format. 227 * 228 * @ctrl_handler: Control handler used to register controls. 229 * @jpeg_quality: User-specified JPEG compression quality. 230 * 231 * @codec_ops: Set of operations related to codec mode. 232 * @postproc: Post-processing context. 233 * @h264_dec: H.264-decoding context. 234 * @jpeg_enc: JPEG-encoding context. 235 * @mpeg2_dec: MPEG-2-decoding context. 236 * @vp8_dec: VP8-decoding context. 237 * @hevc_dec: HEVC-decoding context. 238 * @vp9_dec: VP9-decoding context. 239 */ 240struct hantro_ctx { 241 struct hantro_dev *dev; 242 struct v4l2_fh fh; 243 bool is_encoder; 244 245 u32 sequence_cap; 246 u32 sequence_out; 247 248 const struct hantro_fmt *vpu_src_fmt; 249 struct v4l2_pix_format_mplane src_fmt; 250 const struct hantro_fmt *vpu_dst_fmt; 251 struct v4l2_pix_format_mplane dst_fmt; 252 253 struct v4l2_ctrl_handler ctrl_handler; 254 int jpeg_quality; 255 256 const struct hantro_codec_ops *codec_ops; 257 struct hantro_postproc_ctx postproc; 258 259 /* Specific for particular codec modes. */ 260 union { 261 struct hantro_h264_dec_hw_ctx h264_dec; 262 struct hantro_mpeg2_dec_hw_ctx mpeg2_dec; 263 struct hantro_vp8_dec_hw_ctx vp8_dec; 264 struct hantro_hevc_dec_hw_ctx hevc_dec; 265 struct hantro_vp9_dec_hw_ctx vp9_dec; 266 }; 267}; 268 269/** 270 * struct hantro_fmt - information about supported video formats. 271 * @name: Human readable name of the format. 272 * @fourcc: FourCC code of the format. See V4L2_PIX_FMT_*. 273 * @codec_mode: Codec mode related to this format. See 274 * enum hantro_codec_mode. 275 * @header_size: Optional header size. Currently used by JPEG encoder. 276 * @max_depth: Maximum depth, for bitstream formats 277 * @enc_fmt: Format identifier for encoder registers. 278 * @frmsize: Supported range of frame sizes (only for bitstream formats). 279 * @postprocessed: Indicates if this format needs the post-processor. 280 */ 281struct hantro_fmt { 282 char *name; 283 u32 fourcc; 284 enum hantro_codec_mode codec_mode; 285 int header_size; 286 int max_depth; 287 enum hantro_enc_fmt enc_fmt; 288 struct v4l2_frmsize_stepwise frmsize; 289 bool postprocessed; 290}; 291 292struct hantro_reg { 293 u32 base; 294 u32 shift; 295 u32 mask; 296}; 297 298struct hantro_postproc_regs { 299 struct hantro_reg pipeline_en; 300 struct hantro_reg max_burst; 301 struct hantro_reg clk_gate; 302 struct hantro_reg out_swap32; 303 struct hantro_reg out_endian; 304 struct hantro_reg out_luma_base; 305 struct hantro_reg input_width; 306 struct hantro_reg input_height; 307 struct hantro_reg output_width; 308 struct hantro_reg output_height; 309 struct hantro_reg input_fmt; 310 struct hantro_reg output_fmt; 311 struct hantro_reg orig_width; 312 struct hantro_reg display_width; 313}; 314 315struct hantro_vp9_decoded_buffer_info { 316 /* Info needed when the decoded frame serves as a reference frame. */ 317 unsigned short width; 318 unsigned short height; 319 u32 bit_depth : 4; 320}; 321 322struct hantro_decoded_buffer { 323 /* Must be the first field in this struct. */ 324 struct v4l2_m2m_buffer base; 325 326 union { 327 struct hantro_vp9_decoded_buffer_info vp9; 328 }; 329}; 330 331/* Logging helpers */ 332 333/** 334 * DOC: hantro_debug: Module parameter to control level of debugging messages. 335 * 336 * Level of debugging messages can be controlled by bits of 337 * module parameter called "debug". Meaning of particular 338 * bits is as follows: 339 * 340 * bit 0 - global information: mode, size, init, release 341 * bit 1 - each run start/result information 342 * bit 2 - contents of small controls from userspace 343 * bit 3 - contents of big controls from userspace 344 * bit 4 - detail fmt, ctrl, buffer q/dq information 345 * bit 5 - detail function enter/leave trace information 346 * bit 6 - register write/read information 347 */ 348extern int hantro_debug; 349 350#define vpu_debug(level, fmt, args...) \ 351 do { \ 352 if (hantro_debug & BIT(level)) \ 353 pr_info("%s:%d: " fmt, \ 354 __func__, __LINE__, ##args); \ 355 } while (0) 356 357#define vpu_err(fmt, args...) \ 358 pr_err("%s:%d: " fmt, __func__, __LINE__, ##args) 359 360/* Structure access helpers. */ 361static inline struct hantro_ctx *fh_to_ctx(struct v4l2_fh *fh) 362{ 363 return container_of(fh, struct hantro_ctx, fh); 364} 365 366/* Register accessors. */ 367static inline void vepu_write_relaxed(struct hantro_dev *vpu, 368 u32 val, u32 reg) 369{ 370 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val); 371 writel_relaxed(val, vpu->enc_base + reg); 372} 373 374static inline void vepu_write(struct hantro_dev *vpu, u32 val, u32 reg) 375{ 376 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val); 377 writel(val, vpu->enc_base + reg); 378} 379 380static inline u32 vepu_read(struct hantro_dev *vpu, u32 reg) 381{ 382 u32 val = readl(vpu->enc_base + reg); 383 384 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val); 385 return val; 386} 387 388static inline void vdpu_write_relaxed(struct hantro_dev *vpu, 389 u32 val, u32 reg) 390{ 391 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val); 392 writel_relaxed(val, vpu->dec_base + reg); 393} 394 395static inline void vdpu_write(struct hantro_dev *vpu, u32 val, u32 reg) 396{ 397 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val); 398 writel(val, vpu->dec_base + reg); 399} 400 401static inline void hantro_write_addr(struct hantro_dev *vpu, 402 unsigned long offset, 403 dma_addr_t addr) 404{ 405 vdpu_write(vpu, addr & 0xffffffff, offset); 406} 407 408static inline u32 vdpu_read(struct hantro_dev *vpu, u32 reg) 409{ 410 u32 val = readl(vpu->dec_base + reg); 411 412 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val); 413 return val; 414} 415 416static inline u32 vdpu_read_mask(struct hantro_dev *vpu, 417 const struct hantro_reg *reg, 418 u32 val) 419{ 420 u32 v; 421 422 v = vdpu_read(vpu, reg->base); 423 v &= ~(reg->mask << reg->shift); 424 v |= ((val & reg->mask) << reg->shift); 425 return v; 426} 427 428static inline void hantro_reg_write(struct hantro_dev *vpu, 429 const struct hantro_reg *reg, 430 u32 val) 431{ 432 vdpu_write_relaxed(vpu, vdpu_read_mask(vpu, reg, val), reg->base); 433} 434 435static inline void hantro_reg_write_s(struct hantro_dev *vpu, 436 const struct hantro_reg *reg, 437 u32 val) 438{ 439 vdpu_write(vpu, vdpu_read_mask(vpu, reg, val), reg->base); 440} 441 442void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id); 443dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts); 444 445static inline struct vb2_v4l2_buffer * 446hantro_get_src_buf(struct hantro_ctx *ctx) 447{ 448 return v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 449} 450 451static inline struct vb2_v4l2_buffer * 452hantro_get_dst_buf(struct hantro_ctx *ctx) 453{ 454 return v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 455} 456 457bool hantro_needs_postproc(const struct hantro_ctx *ctx, 458 const struct hantro_fmt *fmt); 459 460static inline dma_addr_t 461hantro_get_dec_buf_addr(struct hantro_ctx *ctx, struct vb2_buffer *vb) 462{ 463 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) 464 return ctx->postproc.dec_q[vb->index].dma; 465 return vb2_dma_contig_plane_dma_addr(vb, 0); 466} 467 468static inline struct hantro_decoded_buffer * 469vb2_to_hantro_decoded_buf(struct vb2_buffer *buf) 470{ 471 return container_of(buf, struct hantro_decoded_buffer, base.vb.vb2_buf); 472} 473 474void hantro_postproc_disable(struct hantro_ctx *ctx); 475void hantro_postproc_enable(struct hantro_ctx *ctx); 476void hantro_postproc_free(struct hantro_ctx *ctx); 477int hantro_postproc_alloc(struct hantro_ctx *ctx); 478int hanto_postproc_enum_framesizes(struct hantro_ctx *ctx, 479 struct v4l2_frmsizeenum *fsize); 480 481#endif /* HANTRO_H_ */