uacce: add uacce driver
[linux-block.git] / include / linux / uacce.h
CommitLineData
015d239a
KL
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
12struct uacce_queue;
13struct uacce_device;
14
15/**
16 * struct uacce_qfile_region - structure of queue file region
17 * @type: type of the region
18 */
19struct uacce_qfile_region {
20 enum uacce_qfrt type;
21};
22
23/**
24 * struct uacce_ops - uacce device operations
25 * @get_available_instances: get available instances left of the device
26 * @get_queue: get a queue from the device
27 * @put_queue: free a queue to the device
28 * @start_queue: make the queue start work after get_queue
29 * @stop_queue: make the queue stop work before put_queue
30 * @is_q_updated: check whether the task is finished
31 * @mmap: mmap addresses of queue to user space
32 * @ioctl: ioctl for user space users of the queue
33 */
34struct uacce_ops {
35 int (*get_available_instances)(struct uacce_device *uacce);
36 int (*get_queue)(struct uacce_device *uacce, unsigned long arg,
37 struct uacce_queue *q);
38 void (*put_queue)(struct uacce_queue *q);
39 int (*start_queue)(struct uacce_queue *q);
40 void (*stop_queue)(struct uacce_queue *q);
41 int (*is_q_updated)(struct uacce_queue *q);
42 int (*mmap)(struct uacce_queue *q, struct vm_area_struct *vma,
43 struct uacce_qfile_region *qfr);
44 long (*ioctl)(struct uacce_queue *q, unsigned int cmd,
45 unsigned long arg);
46};
47
48/**
49 * struct uacce_interface - interface required for uacce_register()
50 * @name: the uacce device name. Will show up in sysfs
51 * @flags: uacce device attributes
52 * @ops: pointer to the struct uacce_ops
53 */
54struct uacce_interface {
55 char name[UACCE_MAX_NAME_SIZE];
56 unsigned int flags;
57 const struct uacce_ops *ops;
58};
59
60enum uacce_q_state {
61 UACCE_Q_ZOMBIE = 0,
62 UACCE_Q_INIT,
63 UACCE_Q_STARTED,
64};
65
66/**
67 * struct uacce_queue
68 * @uacce: pointer to uacce
69 * @priv: private pointer
70 * @wait: wait queue head
71 * @list: index into uacce_mm
72 * @uacce_mm: the corresponding mm
73 * @qfrs: pointer of qfr regions
74 * @state: queue state machine
75 */
76struct uacce_queue {
77 struct uacce_device *uacce;
78 void *priv;
79 wait_queue_head_t wait;
80 struct list_head list;
81 struct uacce_mm *uacce_mm;
82 struct uacce_qfile_region *qfrs[UACCE_MAX_REGION];
83 enum uacce_q_state state;
84};
85
86/**
87 * struct uacce_device
88 * @algs: supported algorithms
89 * @api_ver: api version
90 * @ops: pointer to the struct uacce_ops
91 * @qf_pg_num: page numbers of the queue file regions
92 * @parent: pointer to the parent device
93 * @is_vf: whether virtual function
94 * @flags: uacce attributes
95 * @dev_id: id of the uacce device
96 * @cdev: cdev of the uacce
97 * @dev: dev of the uacce
98 * @priv: private pointer of the uacce
99 * @mm_list: list head of uacce_mm->list
100 * @mm_lock: lock for mm_list
101 */
102struct uacce_device {
103 const char *algs;
104 const char *api_ver;
105 const struct uacce_ops *ops;
106 unsigned long qf_pg_num[UACCE_MAX_REGION];
107 struct device *parent;
108 bool is_vf;
109 u32 flags;
110 u32 dev_id;
111 struct cdev *cdev;
112 struct device dev;
113 void *priv;
114 struct list_head mm_list;
115 struct mutex mm_lock;
116};
117
118/**
119 * struct uacce_mm - keep track of queues bound to a process
120 * @list: index into uacce_device
121 * @queues: list of queues
122 * @mm: the mm struct
123 * @lock: protects the list of queues
124 * @pasid: pasid of the uacce_mm
125 * @handle: iommu_sva handle return from iommu_sva_bind_device
126 */
127struct uacce_mm {
128 struct list_head list;
129 struct list_head queues;
130 struct mm_struct *mm;
131 struct mutex lock;
132 int pasid;
133 struct iommu_sva *handle;
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 */