Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3#include <linux/init.h>
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/pci.h>
7#include <linux/device.h>
8#include <linux/sched/task.h>
9#include <linux/intel-svm.h>
10#include <linux/io-64-nonatomic-lo-hi.h>
11#include <linux/cdev.h>
12#include <linux/fs.h>
13#include <linux/poll.h>
14#include <linux/iommu.h>
15#include <uapi/linux/idxd.h>
16#include "registers.h"
17#include "idxd.h"
18
19struct idxd_cdev_context {
20 const char *name;
21 dev_t devt;
22 struct ida minor_ida;
23};
24
25/*
26 * ictx is an array based off of accelerator types. enum idxd_type
27 * is used as index
28 */
29static struct idxd_cdev_context ictx[IDXD_TYPE_MAX] = {
30 { .name = "dsa" },
31 { .name = "iax" }
32};
33
34struct idxd_user_context {
35 struct idxd_wq *wq;
36 struct task_struct *task;
37 unsigned int pasid;
38 unsigned int flags;
39 struct iommu_sva *sva;
40};
41
42static void idxd_cdev_dev_release(struct device *dev)
43{
44 struct idxd_cdev *idxd_cdev = container_of(dev, struct idxd_cdev, dev);
45 struct idxd_cdev_context *cdev_ctx;
46 struct idxd_wq *wq = idxd_cdev->wq;
47
48 cdev_ctx = &ictx[wq->idxd->data->type];
49 ida_simple_remove(&cdev_ctx->minor_ida, idxd_cdev->minor);
50 kfree(idxd_cdev);
51}
52
53static struct device_type idxd_cdev_device_type = {
54 .name = "idxd_cdev",
55 .release = idxd_cdev_dev_release,
56};
57
58static inline struct idxd_cdev *inode_idxd_cdev(struct inode *inode)
59{
60 struct cdev *cdev = inode->i_cdev;
61
62 return container_of(cdev, struct idxd_cdev, cdev);
63}
64
65static inline struct idxd_wq *inode_wq(struct inode *inode)
66{
67 struct idxd_cdev *idxd_cdev = inode_idxd_cdev(inode);
68
69 return idxd_cdev->wq;
70}
71
72static int idxd_cdev_open(struct inode *inode, struct file *filp)
73{
74 struct idxd_user_context *ctx;
75 struct idxd_device *idxd;
76 struct idxd_wq *wq;
77 struct device *dev;
78 int rc = 0;
79 struct iommu_sva *sva;
80 unsigned int pasid;
81
82 wq = inode_wq(inode);
83 idxd = wq->idxd;
84 dev = &idxd->pdev->dev;
85
86 dev_dbg(dev, "%s called: %d\n", __func__, idxd_wq_refcount(wq));
87
88 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
89 if (!ctx)
90 return -ENOMEM;
91
92 mutex_lock(&wq->wq_lock);
93
94 if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq)) {
95 rc = -EBUSY;
96 goto failed;
97 }
98
99 ctx->wq = wq;
100 filp->private_data = ctx;
101
102 if (device_pasid_enabled(idxd)) {
103 sva = iommu_sva_bind_device(dev, current->mm, NULL);
104 if (IS_ERR(sva)) {
105 rc = PTR_ERR(sva);
106 dev_err(dev, "pasid allocation failed: %d\n", rc);
107 goto failed;
108 }
109
110 pasid = iommu_sva_get_pasid(sva);
111 if (pasid == IOMMU_PASID_INVALID) {
112 iommu_sva_unbind_device(sva);
113 goto failed;
114 }
115
116 ctx->sva = sva;
117 ctx->pasid = pasid;
118
119 if (wq_dedicated(wq)) {
120 rc = idxd_wq_set_pasid(wq, pasid);
121 if (rc < 0) {
122 iommu_sva_unbind_device(sva);
123 dev_err(dev, "wq set pasid failed: %d\n", rc);
124 goto failed;
125 }
126 }
127 }
128
129 idxd_wq_get(wq);
130 mutex_unlock(&wq->wq_lock);
131 return 0;
132
133 failed:
134 mutex_unlock(&wq->wq_lock);
135 kfree(ctx);
136 return rc;
137}
138
139static int idxd_cdev_release(struct inode *node, struct file *filep)
140{
141 struct idxd_user_context *ctx = filep->private_data;
142 struct idxd_wq *wq = ctx->wq;
143 struct idxd_device *idxd = wq->idxd;
144 struct device *dev = &idxd->pdev->dev;
145 int rc;
146
147 dev_dbg(dev, "%s called\n", __func__);
148 filep->private_data = NULL;
149
150 /* Wait for in-flight operations to complete. */
151 if (wq_shared(wq)) {
152 idxd_device_drain_pasid(idxd, ctx->pasid);
153 } else {
154 if (device_pasid_enabled(idxd)) {
155 /* The wq disable in the disable pasid function will drain the wq */
156 rc = idxd_wq_disable_pasid(wq);
157 if (rc < 0)
158 dev_err(dev, "wq disable pasid failed.\n");
159 } else {
160 idxd_wq_drain(wq);
161 }
162 }
163
164 if (ctx->sva)
165 iommu_sva_unbind_device(ctx->sva);
166 kfree(ctx);
167 mutex_lock(&wq->wq_lock);
168 idxd_wq_put(wq);
169 mutex_unlock(&wq->wq_lock);
170 return 0;
171}
172
173static int check_vma(struct idxd_wq *wq, struct vm_area_struct *vma,
174 const char *func)
175{
176 struct device *dev = &wq->idxd->pdev->dev;
177
178 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
179 dev_info_ratelimited(dev,
180 "%s: %s: mapping too large: %lu\n",
181 current->comm, func,
182 vma->vm_end - vma->vm_start);
183 return -EINVAL;
184 }
185
186 return 0;
187}
188
189static int idxd_cdev_mmap(struct file *filp, struct vm_area_struct *vma)
190{
191 struct idxd_user_context *ctx = filp->private_data;
192 struct idxd_wq *wq = ctx->wq;
193 struct idxd_device *idxd = wq->idxd;
194 struct pci_dev *pdev = idxd->pdev;
195 phys_addr_t base = pci_resource_start(pdev, IDXD_WQ_BAR);
196 unsigned long pfn;
197 int rc;
198
199 dev_dbg(&pdev->dev, "%s called\n", __func__);
200 rc = check_vma(wq, vma, __func__);
201 if (rc < 0)
202 return rc;
203
204 vma->vm_flags |= VM_DONTCOPY;
205 pfn = (base + idxd_get_wq_portal_full_offset(wq->id,
206 IDXD_PORTAL_LIMITED)) >> PAGE_SHIFT;
207 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
208 vma->vm_private_data = ctx;
209
210 return io_remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
211 vma->vm_page_prot);
212}
213
214static __poll_t idxd_cdev_poll(struct file *filp,
215 struct poll_table_struct *wait)
216{
217 struct idxd_user_context *ctx = filp->private_data;
218 struct idxd_wq *wq = ctx->wq;
219 struct idxd_device *idxd = wq->idxd;
220 unsigned long flags;
221 __poll_t out = 0;
222
223 poll_wait(filp, &wq->err_queue, wait);
224 spin_lock_irqsave(&idxd->dev_lock, flags);
225 if (idxd->sw_err.valid)
226 out = EPOLLIN | EPOLLRDNORM;
227 spin_unlock_irqrestore(&idxd->dev_lock, flags);
228
229 return out;
230}
231
232static const struct file_operations idxd_cdev_fops = {
233 .owner = THIS_MODULE,
234 .open = idxd_cdev_open,
235 .release = idxd_cdev_release,
236 .mmap = idxd_cdev_mmap,
237 .poll = idxd_cdev_poll,
238};
239
240int idxd_cdev_get_major(struct idxd_device *idxd)
241{
242 return MAJOR(ictx[idxd->data->type].devt);
243}
244
245int idxd_wq_add_cdev(struct idxd_wq *wq)
246{
247 struct idxd_device *idxd = wq->idxd;
248 struct idxd_cdev *idxd_cdev;
249 struct cdev *cdev;
250 struct device *dev;
251 struct idxd_cdev_context *cdev_ctx;
252 int rc, minor;
253
254 idxd_cdev = kzalloc(sizeof(*idxd_cdev), GFP_KERNEL);
255 if (!idxd_cdev)
256 return -ENOMEM;
257
258 idxd_cdev->wq = wq;
259 cdev = &idxd_cdev->cdev;
260 dev = &idxd_cdev->dev;
261 cdev_ctx = &ictx[wq->idxd->data->type];
262 minor = ida_simple_get(&cdev_ctx->minor_ida, 0, MINORMASK, GFP_KERNEL);
263 if (minor < 0) {
264 kfree(idxd_cdev);
265 return minor;
266 }
267 idxd_cdev->minor = minor;
268
269 device_initialize(dev);
270 dev->parent = &wq->conf_dev;
271 dev->bus = &dsa_bus_type;
272 dev->type = &idxd_cdev_device_type;
273 dev->devt = MKDEV(MAJOR(cdev_ctx->devt), minor);
274
275 rc = dev_set_name(dev, "%s/wq%u.%u", idxd->data->name_prefix, idxd->id, wq->id);
276 if (rc < 0)
277 goto err;
278
279 wq->idxd_cdev = idxd_cdev;
280 cdev_init(cdev, &idxd_cdev_fops);
281 rc = cdev_device_add(cdev, dev);
282 if (rc) {
283 dev_dbg(&wq->idxd->pdev->dev, "cdev_add failed: %d\n", rc);
284 goto err;
285 }
286
287 return 0;
288
289 err:
290 put_device(dev);
291 wq->idxd_cdev = NULL;
292 return rc;
293}
294
295void idxd_wq_del_cdev(struct idxd_wq *wq)
296{
297 struct idxd_cdev *idxd_cdev;
298 struct idxd_cdev_context *cdev_ctx;
299
300 cdev_ctx = &ictx[wq->idxd->data->type];
301 idxd_cdev = wq->idxd_cdev;
302 wq->idxd_cdev = NULL;
303 cdev_device_del(&idxd_cdev->cdev, &idxd_cdev->dev);
304 put_device(&idxd_cdev->dev);
305}
306
307int idxd_cdev_register(void)
308{
309 int rc, i;
310
311 for (i = 0; i < IDXD_TYPE_MAX; i++) {
312 ida_init(&ictx[i].minor_ida);
313 rc = alloc_chrdev_region(&ictx[i].devt, 0, MINORMASK,
314 ictx[i].name);
315 if (rc)
316 return rc;
317 }
318
319 return 0;
320}
321
322void idxd_cdev_remove(void)
323{
324 int i;
325
326 for (i = 0; i < IDXD_TYPE_MAX; i++) {
327 unregister_chrdev_region(ictx[i].devt, MINORMASK);
328 ida_destroy(&ictx[i].minor_ida);
329 }
330}