xref: /openbmc/linux/include/linux/uacce.h (revision df1b056d)
1015d239aSKenneth Lee /* SPDX-License-Identifier: GPL-2.0-or-later */
2015d239aSKenneth Lee #ifndef _LINUX_UACCE_H
3015d239aSKenneth Lee #define _LINUX_UACCE_H
4015d239aSKenneth Lee 
5015d239aSKenneth Lee #include <linux/cdev.h>
6015d239aSKenneth Lee #include <uapi/misc/uacce/uacce.h>
7015d239aSKenneth Lee 
8015d239aSKenneth Lee #define UACCE_NAME		"uacce"
9015d239aSKenneth Lee #define UACCE_MAX_REGION	2
10015d239aSKenneth Lee #define UACCE_MAX_NAME_SIZE	64
11e3e289fbSKai Ye #define UACCE_MAX_ERR_THRESHOLD	65535
12015d239aSKenneth Lee 
13015d239aSKenneth Lee struct uacce_queue;
14015d239aSKenneth Lee struct uacce_device;
15015d239aSKenneth Lee 
16015d239aSKenneth Lee /**
17015d239aSKenneth Lee  * struct uacce_qfile_region - structure of queue file region
18015d239aSKenneth Lee  * @type: type of the region
19015d239aSKenneth Lee  */
20015d239aSKenneth Lee struct uacce_qfile_region {
21015d239aSKenneth Lee 	enum uacce_qfrt type;
22015d239aSKenneth Lee };
23015d239aSKenneth Lee 
24015d239aSKenneth Lee /**
25015d239aSKenneth Lee  * struct uacce_ops - uacce device operations
26015d239aSKenneth Lee  * @get_available_instances:  get available instances left of the device
27015d239aSKenneth Lee  * @get_queue: get a queue from the device
28015d239aSKenneth Lee  * @put_queue: free a queue to the device
29015d239aSKenneth Lee  * @start_queue: make the queue start work after get_queue
30015d239aSKenneth Lee  * @stop_queue: make the queue stop work before put_queue
31015d239aSKenneth Lee  * @is_q_updated: check whether the task is finished
32015d239aSKenneth Lee  * @mmap: mmap addresses of queue to user space
33015d239aSKenneth Lee  * @ioctl: ioctl for user space users of the queue
34e3e289fbSKai Ye  * @get_isolate_state: get the device state after set the isolate strategy
35e3e289fbSKai Ye  * @isolate_err_threshold_write: stored the isolate error threshold to the device
36e3e289fbSKai Ye  * @isolate_err_threshold_read: read the isolate error threshold value from the device
37015d239aSKenneth Lee  */
38015d239aSKenneth Lee struct uacce_ops {
39015d239aSKenneth Lee 	int (*get_available_instances)(struct uacce_device *uacce);
40015d239aSKenneth Lee 	int (*get_queue)(struct uacce_device *uacce, unsigned long arg,
41015d239aSKenneth Lee 			 struct uacce_queue *q);
42015d239aSKenneth Lee 	void (*put_queue)(struct uacce_queue *q);
43015d239aSKenneth Lee 	int (*start_queue)(struct uacce_queue *q);
44015d239aSKenneth Lee 	void (*stop_queue)(struct uacce_queue *q);
45015d239aSKenneth Lee 	int (*is_q_updated)(struct uacce_queue *q);
46015d239aSKenneth Lee 	int (*mmap)(struct uacce_queue *q, struct vm_area_struct *vma,
47015d239aSKenneth Lee 		    struct uacce_qfile_region *qfr);
48015d239aSKenneth Lee 	long (*ioctl)(struct uacce_queue *q, unsigned int cmd,
49015d239aSKenneth Lee 		      unsigned long arg);
50e3e289fbSKai Ye 	enum uacce_dev_state (*get_isolate_state)(struct uacce_device *uacce);
51e3e289fbSKai Ye 	int (*isolate_err_threshold_write)(struct uacce_device *uacce, u32 num);
52e3e289fbSKai Ye 	u32 (*isolate_err_threshold_read)(struct uacce_device *uacce);
53015d239aSKenneth Lee };
54015d239aSKenneth Lee 
55015d239aSKenneth Lee /**
56015d239aSKenneth Lee  * struct uacce_interface - interface required for uacce_register()
57015d239aSKenneth Lee  * @name: the uacce device name.  Will show up in sysfs
58015d239aSKenneth Lee  * @flags: uacce device attributes
59015d239aSKenneth Lee  * @ops: pointer to the struct uacce_ops
60015d239aSKenneth Lee  */
61015d239aSKenneth Lee struct uacce_interface {
62015d239aSKenneth Lee 	char name[UACCE_MAX_NAME_SIZE];
63015d239aSKenneth Lee 	unsigned int flags;
64015d239aSKenneth Lee 	const struct uacce_ops *ops;
65015d239aSKenneth Lee };
66015d239aSKenneth Lee 
67e3e289fbSKai Ye enum uacce_dev_state {
68e3e289fbSKai Ye 	UACCE_DEV_NORMAL,
69e3e289fbSKai Ye 	UACCE_DEV_ISOLATE,
70e3e289fbSKai Ye };
71e3e289fbSKai Ye 
72015d239aSKenneth Lee enum uacce_q_state {
73015d239aSKenneth Lee 	UACCE_Q_ZOMBIE = 0,
74015d239aSKenneth Lee 	UACCE_Q_INIT,
75015d239aSKenneth Lee 	UACCE_Q_STARTED,
76015d239aSKenneth Lee };
77015d239aSKenneth Lee 
78015d239aSKenneth Lee /**
79015d239aSKenneth Lee  * struct uacce_queue
80015d239aSKenneth Lee  * @uacce: pointer to uacce
81015d239aSKenneth Lee  * @priv: private pointer
82015d239aSKenneth Lee  * @wait: wait queue head
83fb01562eSJean-Philippe Brucker  * @list: index into uacce queues list
84015d239aSKenneth Lee  * @qfrs: pointer of qfr regions
8580fc671bSJean-Philippe Brucker  * @mutex: protects queue state
86015d239aSKenneth Lee  * @state: queue state machine
87fb01562eSJean-Philippe Brucker  * @pasid: pasid associated to the mm
88fb01562eSJean-Philippe Brucker  * @handle: iommu_sva handle returned by iommu_sva_bind_device()
89*df1b056dSZhangfei Gao  * @mapping: user space mapping of the queue
90015d239aSKenneth Lee  */
91015d239aSKenneth Lee struct uacce_queue {
92015d239aSKenneth Lee 	struct uacce_device *uacce;
93015d239aSKenneth Lee 	void *priv;
94015d239aSKenneth Lee 	wait_queue_head_t wait;
95015d239aSKenneth Lee 	struct list_head list;
96015d239aSKenneth Lee 	struct uacce_qfile_region *qfrs[UACCE_MAX_REGION];
9780fc671bSJean-Philippe Brucker 	struct mutex mutex;
98015d239aSKenneth Lee 	enum uacce_q_state state;
99c7b6bac9SFenghua Yu 	u32 pasid;
100fb01562eSJean-Philippe Brucker 	struct iommu_sva *handle;
101*df1b056dSZhangfei Gao 	struct address_space *mapping;
102015d239aSKenneth Lee };
103015d239aSKenneth Lee 
104015d239aSKenneth Lee /**
105015d239aSKenneth Lee  * struct uacce_device
106015d239aSKenneth Lee  * @algs: supported algorithms
107015d239aSKenneth Lee  * @api_ver: api version
108015d239aSKenneth Lee  * @ops: pointer to the struct uacce_ops
109015d239aSKenneth Lee  * @qf_pg_num: page numbers of the queue file regions
110015d239aSKenneth Lee  * @parent: pointer to the parent device
111015d239aSKenneth Lee  * @is_vf: whether virtual function
112015d239aSKenneth Lee  * @flags: uacce attributes
113015d239aSKenneth Lee  * @dev_id: id of the uacce device
114015d239aSKenneth Lee  * @cdev: cdev of the uacce
115015d239aSKenneth Lee  * @dev: dev of the uacce
11680fc671bSJean-Philippe Brucker  * @mutex: protects uacce operation
117015d239aSKenneth Lee  * @priv: private pointer of the uacce
118fb01562eSJean-Philippe Brucker  * @queues: list of queues
119015d239aSKenneth Lee  */
120015d239aSKenneth Lee struct uacce_device {
121015d239aSKenneth Lee 	const char *algs;
122015d239aSKenneth Lee 	const char *api_ver;
123015d239aSKenneth Lee 	const struct uacce_ops *ops;
124015d239aSKenneth Lee 	unsigned long qf_pg_num[UACCE_MAX_REGION];
125015d239aSKenneth Lee 	struct device *parent;
126015d239aSKenneth Lee 	bool is_vf;
127015d239aSKenneth Lee 	u32 flags;
128015d239aSKenneth Lee 	u32 dev_id;
129015d239aSKenneth Lee 	struct cdev *cdev;
130015d239aSKenneth Lee 	struct device dev;
13180fc671bSJean-Philippe Brucker 	struct mutex mutex;
132015d239aSKenneth Lee 	void *priv;
133015d239aSKenneth Lee 	struct list_head queues;
134015d239aSKenneth Lee };
135015d239aSKenneth Lee 
136015d239aSKenneth Lee #if IS_ENABLED(CONFIG_UACCE)
137015d239aSKenneth Lee 
138015d239aSKenneth Lee struct uacce_device *uacce_alloc(struct device *parent,
139015d239aSKenneth Lee 				 struct uacce_interface *interface);
140015d239aSKenneth Lee int uacce_register(struct uacce_device *uacce);
141015d239aSKenneth Lee void uacce_remove(struct uacce_device *uacce);
142015d239aSKenneth Lee 
143015d239aSKenneth Lee #else /* CONFIG_UACCE */
144015d239aSKenneth Lee 
145015d239aSKenneth Lee static inline
146015d239aSKenneth Lee struct uacce_device *uacce_alloc(struct device *parent,
147015d239aSKenneth Lee 				 struct uacce_interface *interface)
148015d239aSKenneth Lee {
149015d239aSKenneth Lee 	return ERR_PTR(-ENODEV);
150015d239aSKenneth Lee }
151015d239aSKenneth Lee 
152015d239aSKenneth Lee static inline int uacce_register(struct uacce_device *uacce)
153015d239aSKenneth Lee {
154015d239aSKenneth Lee 	return -EINVAL;
155015d239aSKenneth Lee }
156015d239aSKenneth Lee 
157015d239aSKenneth Lee static inline void uacce_remove(struct uacce_device *uacce) {}
158015d239aSKenneth Lee 
159015d239aSKenneth Lee #endif /* CONFIG_UACCE */
160015d239aSKenneth Lee 
161015d239aSKenneth Lee #endif /* _LINUX_UACCE_H */
162