1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 2020-2021 NXP
9 #include <media/v4l2-device.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-mem2mem.h>
12 #include <linux/mailbox_client.h>
13 #include <linux/mailbox_controller.h>
14 #include <linux/kfifo.h>
16 #define VPU_TIMEOUT_WAKEUP msecs_to_jiffies(200)
17 #define VPU_TIMEOUT msecs_to_jiffies(1000)
18 #define VPU_INST_NULL_ID (-1L)
19 #define VPU_MSG_BUFFER_SIZE (8192)
30 VPU_CORE_TYPE_ENC = 0,
31 VPU_CORE_TYPE_DEC = 0x10,
35 struct vpu_resources {
36 enum imx_plat_type plat_type;
38 int (*setup)(struct vpu_dev *vpu);
39 int (*setup_encoder)(struct vpu_dev *vpu);
40 int (*setup_decoder)(struct vpu_dev *vpu);
41 int (*reset)(struct vpu_dev *vpu);
53 struct video_device *vfd;
54 struct v4l2_m2m_dev *m2m_dev;
55 enum vpu_core_type type;
61 struct platform_device *pdev;
63 struct mutex lock; /* protect vpu device */
64 const struct vpu_resources *res;
65 struct list_head cores;
67 struct v4l2_device v4l2_dev;
68 struct vpu_func encoder;
69 struct vpu_func decoder;
70 struct media_device mdev;
72 struct delayed_work watchdog_work;
73 void (*get_vpu)(struct vpu_dev *vpu);
74 void (*put_vpu)(struct vpu_dev *vpu);
75 void (*get_enc)(struct vpu_dev *vpu);
76 void (*put_enc)(struct vpu_dev *vpu);
77 void (*get_dec)(struct vpu_dev *vpu);
78 void (*put_dec)(struct vpu_dev *vpu);
83 struct dentry *debugfs;
94 u32 sizeimage[VIDEO_MAX_PLANES];
95 u32 bytesperline[VIDEO_MAX_PLANES];
100 struct vpu_core_resources {
101 enum vpu_core_type type;
117 struct mbox_client cl;
118 struct mbox_chan *ch;
122 enum vpu_core_state {
130 struct platform_device *pdev;
132 struct device *parent;
134 struct device_link *pd_link;
135 struct mutex lock; /* protect vpu core */
136 struct mutex cmd_lock; /* Lock vpu command */
137 struct list_head list;
138 enum vpu_core_type type;
140 const struct vpu_core_resources *res;
141 unsigned long instance_mask;
142 u32 supported_instance_count;
143 unsigned long hang_mask;
145 struct list_head instances;
146 enum vpu_core_state state;
149 struct vpu_buffer fw;
150 struct vpu_buffer rpc;
151 struct vpu_buffer log;
152 struct vpu_buffer act;
154 struct vpu_mbox tx_type;
155 struct vpu_mbox tx_data;
158 wait_queue_head_t ack_wq;
159 struct completion cmp;
160 struct workqueue_struct *workqueue;
161 struct work_struct msg_work;
162 struct delayed_work msg_delayed_work;
163 struct kfifo msg_fifo;
165 unsigned int msg_buffer_size;
170 struct dentry *debugfs;
171 struct dentry *debugfs_fwlog;
174 enum vpu_codec_state {
175 VPU_CODEC_STATE_DEINIT = 1,
176 VPU_CODEC_STATE_CONFIGURED,
177 VPU_CODEC_STATE_START,
178 VPU_CODEC_STATE_STARTED,
179 VPU_CODEC_STATE_ACTIVE,
180 VPU_CODEC_STATE_SEEK,
181 VPU_CODEC_STATE_STOP,
182 VPU_CODEC_STATE_DRAIN,
183 VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE,
186 struct vpu_frame_info {
200 struct vpu_inst_ops {
201 int (*ctrl_init)(struct vpu_inst *inst);
202 int (*start)(struct vpu_inst *inst, u32 type);
203 int (*stop)(struct vpu_inst *inst, u32 type);
204 int (*abort)(struct vpu_inst *inst);
205 bool (*check_ready)(struct vpu_inst *inst, unsigned int type);
206 void (*buf_done)(struct vpu_inst *inst, struct vpu_frame_info *frame);
207 void (*event_notify)(struct vpu_inst *inst, u32 event, void *data);
208 void (*release)(struct vpu_inst *inst);
209 void (*cleanup)(struct vpu_inst *inst);
210 void (*mem_request)(struct vpu_inst *inst,
217 void (*input_done)(struct vpu_inst *inst);
218 void (*stop_done)(struct vpu_inst *inst);
219 int (*process_output)(struct vpu_inst *inst, struct vb2_buffer *vb);
220 int (*process_capture)(struct vpu_inst *inst, struct vb2_buffer *vb);
221 int (*get_one_frame)(struct vpu_inst *inst, void *info);
222 void (*on_queue_empty)(struct vpu_inst *inst, u32 type);
223 int (*get_debug_info)(struct vpu_inst *inst, char *str, u32 size, u32 i);
224 void (*wait_prepare)(struct vpu_inst *inst);
225 void (*wait_finish)(struct vpu_inst *inst);
229 struct list_head list;
230 struct mutex lock; /* v4l2 and videobuf2 lock */
232 struct vpu_core *core;
237 struct v4l2_ctrl_handler ctrl_handler;
239 int (*release)(struct vpu_inst *inst);
241 enum vpu_codec_state state;
242 enum vpu_core_type type;
244 struct workqueue_struct *workqueue;
245 struct work_struct msg_work;
246 struct kfifo msg_fifo;
247 u8 msg_buffer[VPU_MSG_BUFFER_SIZE];
249 struct vpu_buffer stream_buffer;
250 bool use_stream_buffer;
251 struct vpu_buffer act;
253 struct list_head cmd_q;
255 unsigned long cmd_seq;
256 atomic_long_t last_response_cmd;
258 struct vpu_inst_ops *ops;
259 const struct vpu_format *formats;
260 struct vpu_format out_format;
261 struct vpu_format cap_format;
264 u32 total_input_count;
266 struct v4l2_rect crop;
279 struct dentry *debugfs;
284 #define call_vop(inst, op, args...) \
285 ((inst)->ops->op ? (inst)->ops->op(inst, ##args) : 0) \
287 #define call_void_vop(inst, op, args...) \
289 if ((inst)->ops->op) \
290 (inst)->ops->op(inst, ##args); \
294 VPU_BUF_STATE_IDLE = 0,
296 VPU_BUF_STATE_DECODED,
302 struct vpu_vb2_buffer {
303 struct v4l2_m2m_buffer m2m_buf;
312 void vpu_writel(struct vpu_dev *vpu, u32 reg, u32 val);
313 u32 vpu_readl(struct vpu_dev *vpu, u32 reg);
315 static inline struct vpu_vb2_buffer *to_vpu_vb2_buffer(struct vb2_v4l2_buffer *vbuf)
317 struct v4l2_m2m_buffer *m2m_buf = container_of(vbuf, struct v4l2_m2m_buffer, vb);
319 return container_of(m2m_buf, struct vpu_vb2_buffer, m2m_buf);
322 static inline const char *vpu_core_type_desc(enum vpu_core_type type)
324 return type == VPU_CORE_TYPE_ENC ? "encoder" : "decoder";
327 static inline struct vpu_inst *to_inst(struct file *filp)
329 return container_of(filp->private_data, struct vpu_inst, fh);
332 #define ctrl_to_inst(ctrl) \
333 container_of((ctrl)->handler, struct vpu_inst, ctrl_handler)
335 const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void);
336 const struct v4l2_file_operations *venc_get_fops(void);
337 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void);
338 const struct v4l2_file_operations *vdec_get_fops(void);
340 int vpu_add_func(struct vpu_dev *vpu, struct vpu_func *func);
341 void vpu_remove_func(struct vpu_func *func);
343 struct vpu_inst *vpu_inst_get(struct vpu_inst *inst);
344 void vpu_inst_put(struct vpu_inst *inst);
345 struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum vpu_core_type type);
346 void vpu_release_core(struct vpu_core *core);
347 int vpu_inst_register(struct vpu_inst *inst);
348 int vpu_inst_unregister(struct vpu_inst *inst);
349 const struct vpu_core_resources *vpu_get_resource(struct vpu_inst *inst);
351 int vpu_inst_create_dbgfs_file(struct vpu_inst *inst);
352 int vpu_inst_remove_dbgfs_file(struct vpu_inst *inst);
353 int vpu_core_create_dbgfs_file(struct vpu_core *core);
354 int vpu_core_remove_dbgfs_file(struct vpu_core *core);
355 void vpu_inst_record_flow(struct vpu_inst *inst, u32 flow);
357 int vpu_core_driver_init(void);
358 void vpu_core_driver_exit(void);
360 const char *vpu_id_name(u32 id);
361 const char *vpu_codec_state_name(enum vpu_codec_state state);
364 #define vpu_trace(dev, fmt, arg...) \
367 dev_info(dev, "%s: " fmt, __func__, ## arg); \