vpu.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. u32 mem_planes;
  76. u32 comp_planes;
  77. u32 type;
  78. u32 flags;
  79. u32 width;
  80. u32 height;
  81. u32 sizeimage[VIDEO_MAX_PLANES];
  82. u32 bytesperline[VIDEO_MAX_PLANES];
  83. u32 field;
  84. u32 sibling;
  85. };
  86. struct vpu_core_resources {
  87. enum vpu_core_type type;
  88. const char *fwname;
  89. u32 stride;
  90. u32 max_width;
  91. u32 min_width;
  92. u32 step_width;
  93. u32 max_height;
  94. u32 min_height;
  95. u32 step_height;
  96. u32 rpc_size;
  97. u32 fwlog_size;
  98. u32 act_size;
  99. };
  100. struct vpu_mbox {
  101. char name[20];
  102. struct mbox_client cl;
  103. struct mbox_chan *ch;
  104. bool block;
  105. };
  106. enum vpu_core_state {
  107. VPU_CORE_DEINIT = 0,
  108. VPU_CORE_ACTIVE,
  109. VPU_CORE_HANG
  110. };
  111. struct vpu_core {
  112. void __iomem *base;
  113. struct platform_device *pdev;
  114. struct device *dev;
  115. struct device *parent;
  116. struct device *pd;
  117. struct device_link *pd_link;
  118. struct mutex lock; /* protect vpu core */
  119. struct mutex cmd_lock; /* Lock vpu command */
  120. struct list_head list;
  121. enum vpu_core_type type;
  122. int id;
  123. const struct vpu_core_resources *res;
  124. unsigned long instance_mask;
  125. u32 supported_instance_count;
  126. unsigned long hang_mask;
  127. u32 request_count;
  128. struct list_head instances;
  129. enum vpu_core_state state;
  130. u32 fw_version;
  131. struct vpu_buffer fw;
  132. struct vpu_buffer rpc;
  133. struct vpu_buffer log;
  134. struct vpu_buffer act;
  135. struct vpu_mbox tx_type;
  136. struct vpu_mbox tx_data;
  137. struct vpu_mbox rx;
  138. wait_queue_head_t ack_wq;
  139. struct completion cmp;
  140. struct workqueue_struct *workqueue;
  141. struct work_struct msg_work;
  142. struct delayed_work msg_delayed_work;
  143. struct kfifo msg_fifo;
  144. void *msg_buffer;
  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. void (*attach_frame_store)(struct vpu_inst *inst, struct vb2_buffer *vb);
  201. void (*reset_frame_store)(struct vpu_inst *inst);
  202. };
  203. struct vpu_inst {
  204. struct list_head list;
  205. struct mutex lock; /* v4l2 and videobuf2 lock */
  206. struct vpu_dev *vpu;
  207. struct vpu_core *core;
  208. struct device *dev;
  209. int id;
  210. struct v4l2_fh fh;
  211. struct v4l2_ctrl_handler ctrl_handler;
  212. atomic_t ref_count;
  213. int (*release)(struct vpu_inst *inst);
  214. enum vpu_codec_state state;
  215. enum vpu_core_type type;
  216. struct workqueue_struct *workqueue;
  217. struct work_struct msg_work;
  218. struct kfifo msg_fifo;
  219. u8 msg_buffer[VPU_MSG_BUFFER_SIZE];
  220. struct vpu_buffer stream_buffer;
  221. bool use_stream_buffer;
  222. struct vpu_buffer act;
  223. struct list_head cmd_q;
  224. void *pending;
  225. unsigned long cmd_seq;
  226. atomic_long_t last_response_cmd;
  227. struct vpu_inst_ops *ops;
  228. const struct vpu_format *formats;
  229. struct vpu_format out_format;
  230. struct vpu_format cap_format;
  231. u32 min_buffer_cap;
  232. u32 min_buffer_out;
  233. u32 total_input_count;
  234. struct v4l2_rect crop;
  235. u32 colorspace;
  236. u8 ycbcr_enc;
  237. u8 quantization;
  238. u8 xfer_func;
  239. u32 sequence;
  240. u32 extra_size;
  241. u32 flows[16];
  242. u32 flow_idx;
  243. pid_t pid;
  244. pid_t tgid;
  245. struct dentry *debugfs;
  246. void *priv;
  247. };
  248. #define call_vop(inst, op, args...) \
  249. ((inst)->ops->op ? (inst)->ops->op(inst, ##args) : 0) \
  250. #define call_void_vop(inst, op, args...) \
  251. do { \
  252. if ((inst)->ops->op) \
  253. (inst)->ops->op(inst, ##args); \
  254. } while (0)
  255. enum {
  256. VPU_BUF_STATE_IDLE = 0,
  257. VPU_BUF_STATE_INUSE,
  258. VPU_BUF_STATE_DECODED,
  259. VPU_BUF_STATE_READY,
  260. VPU_BUF_STATE_SKIP,
  261. VPU_BUF_STATE_ERROR,
  262. VPU_BUF_STATE_CHANGED
  263. };
  264. struct vpu_vb2_buffer {
  265. struct v4l2_m2m_buffer m2m_buf;
  266. dma_addr_t luma;
  267. dma_addr_t chroma_u;
  268. dma_addr_t chroma_v;
  269. unsigned int state;
  270. u32 average_qp;
  271. s32 fs_id;
  272. };
  273. void vpu_writel(struct vpu_dev *vpu, u32 reg, u32 val);
  274. u32 vpu_readl(struct vpu_dev *vpu, u32 reg);
  275. static inline struct vpu_vb2_buffer *to_vpu_vb2_buffer(struct vb2_v4l2_buffer *vbuf)
  276. {
  277. struct v4l2_m2m_buffer *m2m_buf = container_of(vbuf, struct v4l2_m2m_buffer, vb);
  278. return container_of(m2m_buf, struct vpu_vb2_buffer, m2m_buf);
  279. }
  280. static inline const char *vpu_core_type_desc(enum vpu_core_type type)
  281. {
  282. return type == VPU_CORE_TYPE_ENC ? "encoder" : "decoder";
  283. }
  284. static inline struct vpu_inst *to_inst(struct file *filp)
  285. {
  286. return container_of(file_to_v4l2_fh(filp), struct vpu_inst, fh);
  287. }
  288. #define ctrl_to_inst(ctrl) \
  289. container_of((ctrl)->handler, struct vpu_inst, ctrl_handler)
  290. const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void);
  291. const struct v4l2_file_operations *venc_get_fops(void);
  292. const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void);
  293. const struct v4l2_file_operations *vdec_get_fops(void);
  294. int vpu_add_func(struct vpu_dev *vpu, struct vpu_func *func);
  295. void vpu_remove_func(struct vpu_func *func);
  296. struct vpu_inst *vpu_inst_get(struct vpu_inst *inst);
  297. void vpu_inst_put(struct vpu_inst *inst);
  298. struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum vpu_core_type type);
  299. void vpu_release_core(struct vpu_core *core);
  300. int vpu_inst_register(struct vpu_inst *inst);
  301. int vpu_inst_unregister(struct vpu_inst *inst);
  302. const struct vpu_core_resources *vpu_get_resource(struct vpu_inst *inst);
  303. int vpu_inst_create_dbgfs_file(struct vpu_inst *inst);
  304. int vpu_inst_remove_dbgfs_file(struct vpu_inst *inst);
  305. int vpu_core_create_dbgfs_file(struct vpu_core *core);
  306. int vpu_core_remove_dbgfs_file(struct vpu_core *core);
  307. void vpu_inst_record_flow(struct vpu_inst *inst, u32 flow);
  308. int vpu_core_driver_init(void);
  309. void vpu_core_driver_exit(void);
  310. const char *vpu_id_name(u32 id);
  311. const char *vpu_codec_state_name(enum vpu_codec_state state);
  312. extern bool debug;
  313. #define vpu_trace(dev, fmt, arg...) \
  314. do { \
  315. if (debug) \
  316. dev_info(dev, "%s: " fmt, __func__, ## arg); \
  317. } while (0)
  318. #endif