hantro.h 14 KB

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