1/* SPDX-License-Identifier: GPL-2.0-or-later */
2#ifndef _LINUX_UACCE_H
3#define _LINUX_UACCE_H
4
5#include <linux/cdev.h>
6#include <uapi/misc/uacce/uacce.h>
7
8#define UACCE_NAME		"uacce"
9#define UACCE_MAX_REGION	2
10#define UACCE_MAX_NAME_SIZE	64
11#define UACCE_MAX_ERR_THRESHOLD	65535
12
13struct uacce_queue;
14struct uacce_device;
15
16/**
17 * struct uacce_qfile_region - structure of queue file region
18 * @type: type of the region
19 */
20struct uacce_qfile_region {
21	enum uacce_qfrt type;
22};
23
24/**
25 * struct uacce_ops - uacce device operations
26 * @get_available_instances:  get available instances left of the device
27 * @get_queue: get a queue from the device
28 * @put_queue: free a queue to the device
29 * @start_queue: make the queue start work after get_queue
30 * @stop_queue: make the queue stop work before put_queue
31 * @is_q_updated: check whether the task is finished
32 * @mmap: mmap addresses of queue to user space
33 * @ioctl: ioctl for user space users of the queue
34 * @get_isolate_state: get the device state after set the isolate strategy
35 * @isolate_err_threshold_write: stored the isolate error threshold to the device
36 * @isolate_err_threshold_read: read the isolate error threshold value from the device
37 */
38struct uacce_ops {
39	int (*get_available_instances)(struct uacce_device *uacce);
40	int (*get_queue)(struct uacce_device *uacce, unsigned long arg,
41			 struct uacce_queue *q);
42	void (*put_queue)(struct uacce_queue *q);
43	int (*start_queue)(struct uacce_queue *q);
44	void (*stop_queue)(struct uacce_queue *q);
45	int (*is_q_updated)(struct uacce_queue *q);
46	int (*mmap)(struct uacce_queue *q, struct vm_area_struct *vma,
47		    struct uacce_qfile_region *qfr);
48	long (*ioctl)(struct uacce_queue *q, unsigned int cmd,
49		      unsigned long arg);
50	enum uacce_dev_state (*get_isolate_state)(struct uacce_device *uacce);
51	int (*isolate_err_threshold_write)(struct uacce_device *uacce, u32 num);
52	u32 (*isolate_err_threshold_read)(struct uacce_device *uacce);
53};
54
55/**
56 * struct uacce_interface - interface required for uacce_register()
57 * @name: the uacce device name.  Will show up in sysfs
58 * @flags: uacce device attributes
59 * @ops: pointer to the struct uacce_ops
60 */
61struct uacce_interface {
62	char name[UACCE_MAX_NAME_SIZE];
63	unsigned int flags;
64	const struct uacce_ops *ops;
65};
66
67enum uacce_dev_state {
68	UACCE_DEV_NORMAL,
69	UACCE_DEV_ISOLATE,
70};
71
72enum uacce_q_state {
73	UACCE_Q_ZOMBIE = 0,
74	UACCE_Q_INIT,
75	UACCE_Q_STARTED,
76};
77
78/**
79 * struct uacce_queue
80 * @uacce: pointer to uacce
81 * @priv: private pointer
82 * @wait: wait queue head
83 * @list: index into uacce queues list
84 * @qfrs: pointer of qfr regions
85 * @mutex: protects queue state
86 * @state: queue state machine
87 * @pasid: pasid associated to the mm
88 * @handle: iommu_sva handle returned by iommu_sva_bind_device()
89 * @mapping: user space mapping of the queue
90 */
91struct uacce_queue {
92	struct uacce_device *uacce;
93	void *priv;
94	wait_queue_head_t wait;
95	struct list_head list;
96	struct uacce_qfile_region *qfrs[UACCE_MAX_REGION];
97	struct mutex mutex;
98	enum uacce_q_state state;
99	u32 pasid;
100	struct iommu_sva *handle;
101	struct address_space *mapping;
102};
103
104/**
105 * struct uacce_device
106 * @algs: supported algorithms
107 * @api_ver: api version
108 * @ops: pointer to the struct uacce_ops
109 * @qf_pg_num: page numbers of the queue file regions
110 * @parent: pointer to the parent device
111 * @is_vf: whether virtual function
112 * @flags: uacce attributes
113 * @dev_id: id of the uacce device
114 * @cdev: cdev of the uacce
115 * @dev: dev of the uacce
116 * @mutex: protects uacce operation
117 * @priv: private pointer of the uacce
118 * @queues: list of queues
119 */
120struct uacce_device {
121	const char *algs;
122	const char *api_ver;
123	const struct uacce_ops *ops;
124	unsigned long qf_pg_num[UACCE_MAX_REGION];
125	struct device *parent;
126	bool is_vf;
127	u32 flags;
128	u32 dev_id;
129	struct cdev *cdev;
130	struct device dev;
131	struct mutex mutex;
132	void *priv;
133	struct list_head queues;
134};
135
136#if IS_ENABLED(CONFIG_UACCE)
137
138struct uacce_device *uacce_alloc(struct device *parent,
139				 struct uacce_interface *interface);
140int uacce_register(struct uacce_device *uacce);
141void uacce_remove(struct uacce_device *uacce);
142
143#else /* CONFIG_UACCE */
144
145static inline
146struct uacce_device *uacce_alloc(struct device *parent,
147				 struct uacce_interface *interface)
148{
149	return ERR_PTR(-ENODEV);
150}
151
152static inline int uacce_register(struct uacce_device *uacce)
153{
154	return -EINVAL;
155}
156
157static inline void uacce_remove(struct uacce_device *uacce) {}
158
159#endif /* CONFIG_UACCE */
160
161#endif /* _LINUX_UACCE_H */
162