vpu.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2020-2021 NXP
  4. */
  5. #ifndef _AMPHION_VPU_H
  6. #define _AMPHION_VPU_H
  7. #include <media/v4l2-device.h>
  8. #include <media/v4l2-ctrls.h>
  9. #include <media/v4l2-mem2mem.h>
  10. #include <linux/mailbox_client.h>
  11. #include <linux/mailbox_controller.h>
  12. #include <linux/kfifo.h>
  13. #define VPU_TIMEOUT_WAKEUP msecs_to_jiffies(200)
  14. #define VPU_TIMEOUT msecs_to_jiffies(1000)
  15. #define VPU_INST_NULL_ID (-1L)
  16. #define VPU_MSG_BUFFER_SIZE (8192)
  17. enum imx_plat_type {
  18. IMX8QXP = 0,
  19. IMX8QM = 1,
  20. IMX8DM,
  21. IMX8DX,
  22. PLAT_TYPE_RESERVED
  23. };
  24. enum vpu_core_type {
  25. VPU_CORE_TYPE_ENC = 0,
  26. VPU_CORE_TYPE_DEC = 0x10,
  27. };
  28. struct vpu_dev;
  29. struct vpu_resources {
  30. enum imx_plat_type plat_type;
  31. u32 mreg_base;
  32. int (*setup)(struct vpu_dev *vpu);
  33. int (*setup_encoder)(struct vpu_dev *vpu);
  34. int (*setup_decoder)(struct vpu_dev *vpu);
  35. int (*reset)(struct vpu_dev *vpu);
  36. };
  37. struct vpu_buffer {
  38. void *virt;
  39. dma_addr_t phys;
  40. u32 length;
  41. u32 bytesused;
  42. struct device *dev;
  43. };
  44. struct vpu_func {
  45. struct video_device *vfd;
  46. struct v4l2_m2m_dev *m2m_dev;
  47. enum vpu_core_type type;
  48. int function;
  49. };
  50. struct vpu_dev {
  51. void __iomem *base;
  52. struct platform_device *pdev;
  53. struct device *dev;
  54. struct mutex lock; /* protect vpu device */
  55. const struct vpu_resources *res;
  56. struct list_head cores;
  57. struct v4l2_device v4l2_dev;
  58. struct vpu_func encoder;
  59. struct vpu_func decoder;
  60. struct media_device mdev;
  61. struct delayed_work watchdog_work;
  62. void (*get_vpu)(struct vpu_dev *vpu);
  63. void (*put_vpu)(struct vpu_dev *vpu);
  64. void (*get_enc)(struct vpu_dev *vpu);
  65. void (*put_enc)(struct vpu_dev *vpu);
  66. void (*get_dec)(struct vpu_dev *vpu);
  67. void (*put_dec)(struct vpu_dev *vpu);
  68. atomic_t ref_vpu;
  69. atomic_t ref_enc;
  70. atomic_t ref_dec;
  71. struct dentry *debugfs;
  72. };
  73. struct vpu_format {
  74. u32 pixfmt;
  75. unsigned int num_planes;
  76. u32 type;
  77. u32 flags;
  78. u32 width;
  79. u32 height;
  80. u32 sizeimage[VIDEO_MAX_PLANES];
  81. u32 bytesperline[VIDEO_MAX_PLANES];
  82. u32 field;
  83. };
  84. struct vpu_core_resources {
  85. enum vpu_core_type type;
  86. const char *fwname;
  87. u32 stride;
  88. u32 max_width;
  89. u32 min_width;
  90. u32 step_width;
  91. u32 max_height;
  92. u32 min_height;
  93. u32 step_height;
  94. u32 rpc_size;
  95. u32 fwlog_size;
  96. u32 act_size;
  97. };
  98. struct vpu_mbox {
  99. char name[20];
  100. struct mbox_client cl;
  101. struct mbox_chan *ch;
  102. bool block;
  103. };
  104. enum vpu_core_state {
  105. VPU_CORE_DEINIT = 0,
  106. VPU_CORE_ACTIVE,
  107. VPU_CORE_HANG
  108. };
  109. struct vpu_core {
  110. void __iomem *base;
  111. struct platform_device *pdev;
  112. struct device *dev;
  113. struct device *parent;
  114. struct device *pd;
  115. struct device_link *pd_link;
  116. struct mutex lock; /* protect vpu core */
  117. struct mutex cmd_lock; /* Lock vpu command */
  118. struct list_head list;
  119. enum vpu_core_type type;
  120. int id;
  121. const struct vpu_core_resources *res;
  122. unsigned long instance_mask;
  123. u32 supported_instance_count;
  124. unsigned long hang_mask;
  125. u32 request_count;
  126. struct list_head instances;
  127. enum vpu_core_state state;
  128. u32 fw_version;
  129. struct vpu_buffer fw;
  130. struct vpu_buffer rpc;
  131. struct vpu_buffer log;
  132. struct vpu_buffer act;
  133. struct vpu_mbox tx_type;
  134. struct vpu_mbox tx_data;
  135. struct vpu_mbox rx;
  136. unsigned long cmd_seq;
  137. wait_queue_head_t ack_wq;
  138. struct completion cmp;
  139. struct workqueue_struct *workqueue;
  140. struct work_struct msg_work;
  141. struct delayed_work msg_delayed_work;
  142. struct kfifo msg_fifo;
  143. void *msg_buffer;
  144. unsigned int msg_buffer_size;
  145. struct vpu_dev *vpu;
  146. void *iface;
  147. struct dentry *debugfs;
  148. struct dentry *debugfs_fwlog;
  149. };
  150. enum vpu_codec_state {
  151. VPU_CODEC_STATE_DEINIT = 1,
  152. VPU_CODEC_STATE_CONFIGURED,
  153. VPU_CODEC_STATE_START,
  154. VPU_CODEC_STATE_STARTED,
  155. VPU_CODEC_STATE_ACTIVE,
  156. VPU_CODEC_STATE_SEEK,
  157. VPU_CODEC_STATE_STOP,
  158. VPU_CODEC_STATE_DRAIN,
  159. VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE,
  160. };
  161. struct vpu_frame_info {
  162. u32 type;
  163. u32 id;
  164. u32 sequence;
  165. u32 luma;
  166. u32 chroma_u;
  167. u32 chroma_v;
  168. u32 data_offset;
  169. u32 flags;
  170. u32 skipped;
  171. s64 timestamp;
  172. };
  173. struct vpu_inst;
  174. struct vpu_inst_ops {
  175. int (*ctrl_init)(struct vpu_inst *inst);
  176. int (*start)(struct vpu_inst *inst, u32 type);
  177. int (*stop)(struct vpu_inst *inst, u32 type);
  178. int (*abort)(struct vpu_inst *inst);
  179. bool (*check_ready)(struct vpu_inst *inst, unsigned int type);
  180. void (*buf_done)(struct vpu_inst *inst, struct vpu_frame_info *frame);
  181. void (*event_notify)(struct vpu_inst *inst, u32 event, void *data);
  182. void (*release)(struct vpu_inst *inst);
  183. void (*cleanup)(struct vpu_inst *inst);
  184. void (*mem_request)(struct vpu_inst *inst,
  185. u32 enc_frame_size,
  186. u32 enc_frame_num,
  187. u32 ref_frame_size,
  188. u32 ref_frame_num,
  189. u32 act_frame_size,
  190. u32 act_frame_num);
  191. void (*input_done)(struct vpu_inst *inst);
  192. void (*stop_done)(struct vpu_inst *inst);
  193. int (*process_output)(struct vpu_inst *inst, struct vb2_buffer *vb);
  194. int (*process_capture)(struct vpu_inst *inst, struct vb2_buffer *vb);
  195. int (*get_one_frame)(struct vpu_inst *inst, void *info);
  196. void (*on_queue_empty)(struct vpu_inst *inst, u32 type);
  197. int (*get_debug_info)(struct vpu_inst *inst, char *str, u32 size, u32 i);
  198. void (*wait_prepare)(struct vpu_inst *inst);
  199. void (*wait_finish)(struct vpu_inst *inst);
  200. };
  201. struct vpu_inst {
  202. struct list_head list;
  203. struct mutex lock; /* v4l2 and videobuf2 lock */
  204. struct vpu_dev *vpu;
  205. struct vpu_core *core;
  206. struct device *dev;
  207. int id;
  208. struct v4l2_fh fh;
  209. struct v4l2_ctrl_handler ctrl_handler;
  210. atomic_t ref_count;
  211. int (*release)(struct vpu_inst *inst);
  212. enum vpu_codec_state state;
  213. enum vpu_core_type type;
  214. struct workqueue_struct *workqueue;
  215. struct work_struct msg_work;
  216. struct kfifo msg_fifo;
  217. u8 msg_buffer[VPU_MSG_BUFFER_SIZE];
  218. struct vpu_buffer stream_buffer;
  219. bool use_stream_buffer;
  220. struct vpu_buffer act;
  221. struct list_head cmd_q;
  222. void *pending;
  223. struct vpu_inst_ops *ops;
  224. const struct vpu_format *formats;
  225. struct vpu_format out_format;
  226. struct vpu_format cap_format;
  227. u32 min_buffer_cap;
  228. u32 min_buffer_out;
  229. u32 total_input_count;
  230. struct v4l2_rect crop;
  231. u32 colorspace;
  232. u8 ycbcr_enc;
  233. u8 quantization;
  234. u8 xfer_func;
  235. u32 sequence;
  236. u32 extra_size;
  237. u32 flows[16];
  238. u32 flow_idx;
  239. pid_t pid;
  240. pid_t tgid;
  241. struct dentry *debugfs;
  242. void *priv;
  243. };
  244. #define call_vop(inst, op, args...) \
  245. ((inst)->ops->op ? (inst)->ops->op(inst, ##args) : 0) \
  246. #define call_void_vop(inst, op, args...) \
  247. do { \
  248. if ((inst)->ops->op) \
  249. (inst)->ops->op(inst, ##args); \
  250. } while (0)
  251. enum {
  252. VPU_BUF_STATE_IDLE = 0,
  253. VPU_BUF_STATE_INUSE,
  254. VPU_BUF_STATE_DECODED,
  255. VPU_BUF_STATE_READY,
  256. VPU_BUF_STATE_SKIP,
  257. VPU_BUF_STATE_ERROR
  258. };
  259. struct vpu_vb2_buffer {
  260. struct v4l2_m2m_buffer m2m_buf;
  261. dma_addr_t luma;
  262. dma_addr_t chroma_u;
  263. dma_addr_t chroma_v;
  264. unsigned int state;
  265. u32 tag;
  266. };
  267. void vpu_writel(struct vpu_dev *vpu, u32 reg, u32 val);
  268. u32 vpu_readl(struct vpu_dev *vpu, u32 reg);
  269. static inline struct vpu_vb2_buffer *to_vpu_vb2_buffer(struct vb2_v4l2_buffer *vbuf)
  270. {
  271. struct v4l2_m2m_buffer *m2m_buf = container_of(vbuf, struct v4l2_m2m_buffer, vb);
  272. return container_of(m2m_buf, struct vpu_vb2_buffer, m2m_buf);
  273. }
  274. static inline const char *vpu_core_type_desc(enum vpu_core_type type)
  275. {
  276. return type == VPU_CORE_TYPE_ENC ? "encoder" : "decoder";
  277. }
  278. static inline struct vpu_inst *to_inst(struct file *filp)
  279. {
  280. return container_of(filp->private_data, struct vpu_inst, fh);
  281. }
  282. #define ctrl_to_inst(ctrl) \
  283. container_of((ctrl)->handler, struct vpu_inst, ctrl_handler)
  284. const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void);
  285. const struct v4l2_file_operations *venc_get_fops(void);
  286. const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void);
  287. const struct v4l2_file_operations *vdec_get_fops(void);
  288. int vpu_add_func(struct vpu_dev *vpu, struct vpu_func *func);
  289. void vpu_remove_func(struct vpu_func *func);
  290. struct vpu_inst *vpu_inst_get(struct vpu_inst *inst);
  291. void vpu_inst_put(struct vpu_inst *inst);
  292. struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum vpu_core_type type);
  293. void vpu_release_core(struct vpu_core *core);
  294. int vpu_inst_register(struct vpu_inst *inst);
  295. int vpu_inst_unregister(struct vpu_inst *inst);
  296. const struct vpu_core_resources *vpu_get_resource(struct vpu_inst *inst);
  297. int vpu_inst_create_dbgfs_file(struct vpu_inst *inst);
  298. int vpu_inst_remove_dbgfs_file(struct vpu_inst *inst);
  299. int vpu_core_create_dbgfs_file(struct vpu_core *core);
  300. int vpu_core_remove_dbgfs_file(struct vpu_core *core);
  301. void vpu_inst_record_flow(struct vpu_inst *inst, u32 flow);
  302. int vpu_core_driver_init(void);
  303. void vpu_core_driver_exit(void);
  304. const char *vpu_id_name(u32 id);
  305. const char *vpu_codec_state_name(enum vpu_codec_state state);
  306. extern bool debug;
  307. #define vpu_trace(dev, fmt, arg...) \
  308. do { \
  309. if (debug) \
  310. dev_info(dev, "%s: " fmt, __func__, ## arg); \
  311. } while (0)
  312. #endif