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>
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>
13 #include <linux/poll.h>
14 #include <linux/iommu.h>
15 #include <uapi/linux/idxd.h>
16 #include "registers.h"
19 struct idxd_cdev_context {
26 * ictx is an array based off of accelerator types. enum idxd_type
29 static struct idxd_cdev_context ictx[IDXD_TYPE_MAX] = {
34 struct idxd_user_context {
36 struct task_struct *task;
39 struct iommu_sva *sva;
42 static void idxd_cdev_dev_release(struct device *dev)
44 struct idxd_cdev *idxd_cdev = dev_to_cdev(dev);
45 struct idxd_cdev_context *cdev_ctx;
46 struct idxd_wq *wq = idxd_cdev->wq;
48 cdev_ctx = &ictx[wq->idxd->data->type];
49 ida_simple_remove(&cdev_ctx->minor_ida, idxd_cdev->minor);
53 static struct device_type idxd_cdev_device_type = {
55 .release = idxd_cdev_dev_release,
58 static inline struct idxd_cdev *inode_idxd_cdev(struct inode *inode)
60 struct cdev *cdev = inode->i_cdev;
62 return container_of(cdev, struct idxd_cdev, cdev);
65 static inline struct idxd_wq *inode_wq(struct inode *inode)
67 struct idxd_cdev *idxd_cdev = inode_idxd_cdev(inode);
72 static int idxd_cdev_open(struct inode *inode, struct file *filp)
74 struct idxd_user_context *ctx;
75 struct idxd_device *idxd;
79 struct iommu_sva *sva;
84 dev = &idxd->pdev->dev;
86 dev_dbg(dev, "%s called: %d\n", __func__, idxd_wq_refcount(wq));
88 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
92 mutex_lock(&wq->wq_lock);
94 if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq)) {
100 filp->private_data = ctx;
102 if (device_user_pasid_enabled(idxd)) {
103 sva = iommu_sva_bind_device(dev, current->mm, NULL);
106 dev_err(dev, "pasid allocation failed: %d\n", rc);
110 pasid = iommu_sva_get_pasid(sva);
111 if (pasid == IOMMU_PASID_INVALID) {
112 iommu_sva_unbind_device(sva);
120 if (wq_dedicated(wq)) {
121 rc = idxd_wq_set_pasid(wq, pasid);
123 iommu_sva_unbind_device(sva);
124 dev_err(dev, "wq set pasid failed: %d\n", rc);
131 mutex_unlock(&wq->wq_lock);
135 mutex_unlock(&wq->wq_lock);
140 static int idxd_cdev_release(struct inode *node, struct file *filep)
142 struct idxd_user_context *ctx = filep->private_data;
143 struct idxd_wq *wq = ctx->wq;
144 struct idxd_device *idxd = wq->idxd;
145 struct device *dev = &idxd->pdev->dev;
148 dev_dbg(dev, "%s called\n", __func__);
149 filep->private_data = NULL;
151 /* Wait for in-flight operations to complete. */
153 idxd_device_drain_pasid(idxd, ctx->pasid);
155 if (device_user_pasid_enabled(idxd)) {
156 /* The wq disable in the disable pasid function will drain the wq */
157 rc = idxd_wq_disable_pasid(wq);
159 dev_err(dev, "wq disable pasid failed.\n");
166 iommu_sva_unbind_device(ctx->sva);
168 mutex_lock(&wq->wq_lock);
170 mutex_unlock(&wq->wq_lock);
174 static int check_vma(struct idxd_wq *wq, struct vm_area_struct *vma,
177 struct device *dev = &wq->idxd->pdev->dev;
179 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
180 dev_info_ratelimited(dev,
181 "%s: %s: mapping too large: %lu\n",
183 vma->vm_end - vma->vm_start);
190 static int idxd_cdev_mmap(struct file *filp, struct vm_area_struct *vma)
192 struct idxd_user_context *ctx = filp->private_data;
193 struct idxd_wq *wq = ctx->wq;
194 struct idxd_device *idxd = wq->idxd;
195 struct pci_dev *pdev = idxd->pdev;
196 phys_addr_t base = pci_resource_start(pdev, IDXD_WQ_BAR);
200 dev_dbg(&pdev->dev, "%s called\n", __func__);
201 rc = check_vma(wq, vma, __func__);
205 vma->vm_flags |= VM_DONTCOPY;
206 pfn = (base + idxd_get_wq_portal_full_offset(wq->id,
207 IDXD_PORTAL_LIMITED)) >> PAGE_SHIFT;
208 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
209 vma->vm_private_data = ctx;
211 return io_remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
215 static __poll_t idxd_cdev_poll(struct file *filp,
216 struct poll_table_struct *wait)
218 struct idxd_user_context *ctx = filp->private_data;
219 struct idxd_wq *wq = ctx->wq;
220 struct idxd_device *idxd = wq->idxd;
223 poll_wait(filp, &wq->err_queue, wait);
224 spin_lock(&idxd->dev_lock);
225 if (idxd->sw_err.valid)
226 out = EPOLLIN | EPOLLRDNORM;
227 spin_unlock(&idxd->dev_lock);
232 static 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,
240 int idxd_cdev_get_major(struct idxd_device *idxd)
242 return MAJOR(ictx[idxd->data->type].devt);
245 int idxd_wq_add_cdev(struct idxd_wq *wq)
247 struct idxd_device *idxd = wq->idxd;
248 struct idxd_cdev *idxd_cdev;
251 struct idxd_cdev_context *cdev_ctx;
254 idxd_cdev = kzalloc(sizeof(*idxd_cdev), GFP_KERNEL);
258 idxd_cdev->idxd_dev.type = IDXD_DEV_CDEV;
260 cdev = &idxd_cdev->cdev;
261 dev = cdev_dev(idxd_cdev);
262 cdev_ctx = &ictx[wq->idxd->data->type];
263 minor = ida_simple_get(&cdev_ctx->minor_ida, 0, MINORMASK, GFP_KERNEL);
268 idxd_cdev->minor = minor;
270 device_initialize(dev);
271 dev->parent = wq_confdev(wq);
272 dev->bus = &dsa_bus_type;
273 dev->type = &idxd_cdev_device_type;
274 dev->devt = MKDEV(MAJOR(cdev_ctx->devt), minor);
276 rc = dev_set_name(dev, "%s/wq%u.%u", idxd->data->name_prefix, idxd->id, wq->id);
280 wq->idxd_cdev = idxd_cdev;
281 cdev_init(cdev, &idxd_cdev_fops);
282 rc = cdev_device_add(cdev, dev);
284 dev_dbg(&wq->idxd->pdev->dev, "cdev_add failed: %d\n", rc);
292 wq->idxd_cdev = NULL;
296 void idxd_wq_del_cdev(struct idxd_wq *wq)
298 struct idxd_cdev *idxd_cdev;
300 idxd_cdev = wq->idxd_cdev;
301 wq->idxd_cdev = NULL;
302 cdev_device_del(&idxd_cdev->cdev, cdev_dev(idxd_cdev));
303 put_device(cdev_dev(idxd_cdev));
306 static int idxd_user_drv_probe(struct idxd_dev *idxd_dev)
308 struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
309 struct idxd_device *idxd = wq->idxd;
312 if (idxd->state != IDXD_DEV_ENABLED)
315 mutex_lock(&wq->wq_lock);
316 wq->type = IDXD_WQT_USER;
317 rc = drv_enable_wq(wq);
321 rc = idxd_wq_add_cdev(wq);
323 idxd->cmd_status = IDXD_SCMD_CDEV_ERR;
327 idxd->cmd_status = 0;
328 mutex_unlock(&wq->wq_lock);
334 wq->type = IDXD_WQT_NONE;
335 mutex_unlock(&wq->wq_lock);
339 static void idxd_user_drv_remove(struct idxd_dev *idxd_dev)
341 struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
343 mutex_lock(&wq->wq_lock);
344 idxd_wq_del_cdev(wq);
346 wq->type = IDXD_WQT_NONE;
347 mutex_unlock(&wq->wq_lock);
350 static enum idxd_dev_type dev_types[] = {
355 struct idxd_device_driver idxd_user_drv = {
356 .probe = idxd_user_drv_probe,
357 .remove = idxd_user_drv_remove,
361 EXPORT_SYMBOL_GPL(idxd_user_drv);
363 int idxd_cdev_register(void)
367 for (i = 0; i < IDXD_TYPE_MAX; i++) {
368 ida_init(&ictx[i].minor_ida);
369 rc = alloc_chrdev_region(&ictx[i].devt, 0, MINORMASK,
372 goto err_free_chrdev_region;
377 err_free_chrdev_region:
378 for (i--; i >= 0; i--)
379 unregister_chrdev_region(ictx[i].devt, MINORMASK);
384 void idxd_cdev_remove(void)
388 for (i = 0; i < IDXD_TYPE_MAX; i++) {
389 unregister_chrdev_region(ictx[i].devt, MINORMASK);
390 ida_destroy(&ictx[i].minor_ida);