Contributors: 1
Author Tokens Token Proportion Commits Commit Proportion
Ming Qian 1567 100.00% 7 100.00%
Total 1567 7


/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright 2020-2021 NXP
 */

#ifndef _AMPHION_VPU_H
#define _AMPHION_VPU_H

#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-mem2mem.h>
#include <linux/mailbox_client.h>
#include <linux/mailbox_controller.h>
#include <linux/kfifo.h>

#define VPU_TIMEOUT_WAKEUP	msecs_to_jiffies(200)
#define VPU_TIMEOUT		msecs_to_jiffies(1000)
#define VPU_INST_NULL_ID	(-1L)
#define VPU_MSG_BUFFER_SIZE	(8192)

enum imx_plat_type {
	IMX8QXP = 0,
	IMX8QM  = 1,
	IMX8DM,
	IMX8DX,
	PLAT_TYPE_RESERVED
};

enum vpu_core_type {
	VPU_CORE_TYPE_ENC = 0,
	VPU_CORE_TYPE_DEC = 0x10,
};

struct vpu_dev;
struct vpu_resources {
	enum imx_plat_type plat_type;
	u32 mreg_base;
	int (*setup)(struct vpu_dev *vpu);
	int (*setup_encoder)(struct vpu_dev *vpu);
	int (*setup_decoder)(struct vpu_dev *vpu);
	int (*reset)(struct vpu_dev *vpu);
};

struct vpu_buffer {
	void *virt;
	dma_addr_t phys;
	u32 length;
	u32 bytesused;
	struct device *dev;
};

struct vpu_func {
	struct video_device *vfd;
	struct v4l2_m2m_dev *m2m_dev;
	enum vpu_core_type type;
	int function;
};

struct vpu_dev {
	void __iomem *base;
	struct platform_device *pdev;
	struct device *dev;
	struct mutex lock; /* protect vpu device */
	const struct vpu_resources *res;
	struct list_head cores;

	struct v4l2_device v4l2_dev;
	struct vpu_func encoder;
	struct vpu_func decoder;
	struct media_device mdev;

	struct delayed_work watchdog_work;
	void (*get_vpu)(struct vpu_dev *vpu);
	void (*put_vpu)(struct vpu_dev *vpu);
	void (*get_enc)(struct vpu_dev *vpu);
	void (*put_enc)(struct vpu_dev *vpu);
	void (*get_dec)(struct vpu_dev *vpu);
	void (*put_dec)(struct vpu_dev *vpu);
	atomic_t ref_vpu;
	atomic_t ref_enc;
	atomic_t ref_dec;

	struct dentry *debugfs;
};

struct vpu_format {
	u32 pixfmt;
	u32 mem_planes;
	u32 comp_planes;
	u32 type;
	u32 flags;
	u32 width;
	u32 height;
	u32 sizeimage[VIDEO_MAX_PLANES];
	u32 bytesperline[VIDEO_MAX_PLANES];
	u32 field;
	u32 sibling;
};

struct vpu_core_resources {
	enum vpu_core_type type;
	const char *fwname;
	u32 stride;
	u32 max_width;
	u32 min_width;
	u32 step_width;
	u32 max_height;
	u32 min_height;
	u32 step_height;
	u32 rpc_size;
	u32 fwlog_size;
	u32 act_size;
};

struct vpu_mbox {
	char name[20];
	struct mbox_client cl;
	struct mbox_chan *ch;
	bool block;
};

enum vpu_core_state {
	VPU_CORE_DEINIT = 0,
	VPU_CORE_ACTIVE,
	VPU_CORE_HANG
};

struct vpu_core {
	void __iomem *base;
	struct platform_device *pdev;
	struct device *dev;
	struct device *parent;
	struct device *pd;
	struct device_link *pd_link;
	struct mutex lock;     /* protect vpu core */
	struct mutex cmd_lock; /* Lock vpu command */
	struct list_head list;
	enum vpu_core_type type;
	int id;
	const struct vpu_core_resources *res;
	unsigned long instance_mask;
	u32 supported_instance_count;
	unsigned long hang_mask;
	u32 request_count;
	struct list_head instances;
	enum vpu_core_state state;
	u32 fw_version;

	struct vpu_buffer fw;
	struct vpu_buffer rpc;
	struct vpu_buffer log;
	struct vpu_buffer act;

	struct vpu_mbox tx_type;
	struct vpu_mbox tx_data;
	struct vpu_mbox rx;

	wait_queue_head_t ack_wq;
	struct completion cmp;
	struct workqueue_struct *workqueue;
	struct work_struct msg_work;
	struct delayed_work msg_delayed_work;
	struct kfifo msg_fifo;
	void *msg_buffer;
	unsigned int msg_buffer_size;

	struct vpu_dev *vpu;
	void *iface;

	struct dentry *debugfs;
	struct dentry *debugfs_fwlog;
};

enum vpu_codec_state {
	VPU_CODEC_STATE_DEINIT = 1,
	VPU_CODEC_STATE_CONFIGURED,
	VPU_CODEC_STATE_START,
	VPU_CODEC_STATE_STARTED,
	VPU_CODEC_STATE_ACTIVE,
	VPU_CODEC_STATE_SEEK,
	VPU_CODEC_STATE_STOP,
	VPU_CODEC_STATE_DRAIN,
	VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE,
};

struct vpu_frame_info {
	u32 type;
	u32 id;
	u32 sequence;
	u32 luma;
	u32 chroma_u;
	u32 chroma_v;
	u32 data_offset;
	u32 flags;
	u32 skipped;
	s64 timestamp;
};

struct vpu_inst;
struct vpu_inst_ops {
	int (*ctrl_init)(struct vpu_inst *inst);
	int (*start)(struct vpu_inst *inst, u32 type);
	int (*stop)(struct vpu_inst *inst, u32 type);
	int (*abort)(struct vpu_inst *inst);
	bool (*check_ready)(struct vpu_inst *inst, unsigned int type);
	void (*buf_done)(struct vpu_inst *inst, struct vpu_frame_info *frame);
	void (*event_notify)(struct vpu_inst *inst, u32 event, void *data);
	void (*release)(struct vpu_inst *inst);
	void (*cleanup)(struct vpu_inst *inst);
	void (*mem_request)(struct vpu_inst *inst,
			    u32 enc_frame_size,
			    u32 enc_frame_num,
			    u32 ref_frame_size,
			    u32 ref_frame_num,
			    u32 act_frame_size,
			    u32 act_frame_num);
	void (*input_done)(struct vpu_inst *inst);
	void (*stop_done)(struct vpu_inst *inst);
	int (*process_output)(struct vpu_inst *inst, struct vb2_buffer *vb);
	int (*process_capture)(struct vpu_inst *inst, struct vb2_buffer *vb);
	int (*get_one_frame)(struct vpu_inst *inst, void *info);
	void (*on_queue_empty)(struct vpu_inst *inst, u32 type);
	int (*get_debug_info)(struct vpu_inst *inst, char *str, u32 size, u32 i);
	void (*wait_prepare)(struct vpu_inst *inst);
	void (*wait_finish)(struct vpu_inst *inst);
};

struct vpu_inst {
	struct list_head list;
	struct mutex lock; /* v4l2 and videobuf2 lock */
	struct vpu_dev *vpu;
	struct vpu_core *core;
	struct device *dev;
	int id;

	struct v4l2_fh fh;
	struct v4l2_ctrl_handler ctrl_handler;
	atomic_t ref_count;
	int (*release)(struct vpu_inst *inst);

	enum vpu_codec_state state;
	enum vpu_core_type type;

	struct workqueue_struct *workqueue;
	struct work_struct msg_work;
	struct kfifo msg_fifo;
	u8 msg_buffer[VPU_MSG_BUFFER_SIZE];

	struct vpu_buffer stream_buffer;
	bool use_stream_buffer;
	struct vpu_buffer act;

	struct list_head cmd_q;
	void *pending;
	unsigned long cmd_seq;
	atomic_long_t last_response_cmd;

	struct vpu_inst_ops *ops;
	const struct vpu_format *formats;
	struct vpu_format out_format;
	struct vpu_format cap_format;
	u32 min_buffer_cap;
	u32 min_buffer_out;
	u32 total_input_count;

	struct v4l2_rect crop;
	u32 colorspace;
	u8 ycbcr_enc;
	u8 quantization;
	u8 xfer_func;
	u32 sequence;
	u32 extra_size;

	u32 flows[16];
	u32 flow_idx;

	pid_t pid;
	pid_t tgid;
	struct dentry *debugfs;

	void *priv;
};

#define call_vop(inst, op, args...)					\
	((inst)->ops->op ? (inst)->ops->op(inst, ##args) : 0)		\

#define call_void_vop(inst, op, args...)				\
	do {								\
		if ((inst)->ops->op)					\
			(inst)->ops->op(inst, ##args);				\
	} while (0)

enum {
	VPU_BUF_STATE_IDLE = 0,
	VPU_BUF_STATE_INUSE,
	VPU_BUF_STATE_DECODED,
	VPU_BUF_STATE_READY,
	VPU_BUF_STATE_SKIP,
	VPU_BUF_STATE_ERROR
};

struct vpu_vb2_buffer {
	struct v4l2_m2m_buffer m2m_buf;
	dma_addr_t luma;
	dma_addr_t chroma_u;
	dma_addr_t chroma_v;
	unsigned int state;
	u32 tag;
	u32 average_qp;
};

void vpu_writel(struct vpu_dev *vpu, u32 reg, u32 val);
u32 vpu_readl(struct vpu_dev *vpu, u32 reg);

static inline struct vpu_vb2_buffer *to_vpu_vb2_buffer(struct vb2_v4l2_buffer *vbuf)
{
	struct v4l2_m2m_buffer *m2m_buf = container_of(vbuf, struct v4l2_m2m_buffer, vb);

	return container_of(m2m_buf, struct vpu_vb2_buffer, m2m_buf);
}

static inline const char *vpu_core_type_desc(enum vpu_core_type type)
{
	return type == VPU_CORE_TYPE_ENC ? "encoder" : "decoder";
}

static inline struct vpu_inst *to_inst(struct file *filp)
{
	return container_of(filp->private_data, struct vpu_inst, fh);
}

#define ctrl_to_inst(ctrl)	\
	container_of((ctrl)->handler, struct vpu_inst, ctrl_handler)

const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void);
const struct v4l2_file_operations *venc_get_fops(void);
const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void);
const struct v4l2_file_operations *vdec_get_fops(void);

int vpu_add_func(struct vpu_dev *vpu, struct vpu_func *func);
void vpu_remove_func(struct vpu_func *func);

struct vpu_inst *vpu_inst_get(struct vpu_inst *inst);
void vpu_inst_put(struct vpu_inst *inst);
struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum vpu_core_type type);
void vpu_release_core(struct vpu_core *core);
int vpu_inst_register(struct vpu_inst *inst);
int vpu_inst_unregister(struct vpu_inst *inst);
const struct vpu_core_resources *vpu_get_resource(struct vpu_inst *inst);

int vpu_inst_create_dbgfs_file(struct vpu_inst *inst);
int vpu_inst_remove_dbgfs_file(struct vpu_inst *inst);
int vpu_core_create_dbgfs_file(struct vpu_core *core);
int vpu_core_remove_dbgfs_file(struct vpu_core *core);
void vpu_inst_record_flow(struct vpu_inst *inst, u32 flow);

int vpu_core_driver_init(void);
void vpu_core_driver_exit(void);

const char *vpu_id_name(u32 id);
const char *vpu_codec_state_name(enum vpu_codec_state state);

extern bool debug;
#define vpu_trace(dev, fmt, arg...)					\
	do {								\
		if (debug)						\
			dev_info(dev, "%s: " fmt, __func__, ## arg);	\
	} while (0)

#endif