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 <uapi/linux/idxd.h>
15 #include "registers.h"
18 struct idxd_cdev_context {
25 * ictx is an array based off of accelerator types. enum idxd_type
28 static struct idxd_cdev_context ictx[IDXD_TYPE_MAX] = {
32 struct idxd_user_context {
34 struct task_struct *task;
38 static void idxd_cdev_dev_release(struct device *dev)
40 struct idxd_cdev *idxd_cdev = container_of(dev, struct idxd_cdev, dev);
41 struct idxd_cdev_context *cdev_ctx;
42 struct idxd_wq *wq = idxd_cdev->wq;
44 cdev_ctx = &ictx[wq->idxd->type];
45 ida_simple_remove(&cdev_ctx->minor_ida, idxd_cdev->minor);
49 static struct device_type idxd_cdev_device_type = {
51 .release = idxd_cdev_dev_release,
54 static inline struct idxd_cdev *inode_idxd_cdev(struct inode *inode)
56 struct cdev *cdev = inode->i_cdev;
58 return container_of(cdev, struct idxd_cdev, cdev);
61 static inline struct idxd_wq *inode_wq(struct inode *inode)
63 struct idxd_cdev *idxd_cdev = inode_idxd_cdev(inode);
68 static int idxd_cdev_open(struct inode *inode, struct file *filp)
70 struct idxd_user_context *ctx;
71 struct idxd_device *idxd;
78 dev = &idxd->pdev->dev;
80 dev_dbg(dev, "%s called: %d\n", __func__, idxd_wq_refcount(wq));
82 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
86 mutex_lock(&wq->wq_lock);
88 if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq)) {
94 filp->private_data = ctx;
96 mutex_unlock(&wq->wq_lock);
100 mutex_unlock(&wq->wq_lock);
105 static int idxd_cdev_release(struct inode *node, struct file *filep)
107 struct idxd_user_context *ctx = filep->private_data;
108 struct idxd_wq *wq = ctx->wq;
109 struct idxd_device *idxd = wq->idxd;
110 struct device *dev = &idxd->pdev->dev;
112 dev_dbg(dev, "%s called\n", __func__);
113 filep->private_data = NULL;
115 /* Wait for in-flight operations to complete. */
119 mutex_lock(&wq->wq_lock);
121 mutex_unlock(&wq->wq_lock);
125 static int check_vma(struct idxd_wq *wq, struct vm_area_struct *vma,
128 struct device *dev = &wq->idxd->pdev->dev;
130 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
131 dev_info_ratelimited(dev,
132 "%s: %s: mapping too large: %lu\n",
134 vma->vm_end - vma->vm_start);
141 static int idxd_cdev_mmap(struct file *filp, struct vm_area_struct *vma)
143 struct idxd_user_context *ctx = filp->private_data;
144 struct idxd_wq *wq = ctx->wq;
145 struct idxd_device *idxd = wq->idxd;
146 struct pci_dev *pdev = idxd->pdev;
147 phys_addr_t base = pci_resource_start(pdev, IDXD_WQ_BAR);
151 dev_dbg(&pdev->dev, "%s called\n", __func__);
152 rc = check_vma(wq, vma, __func__);
156 vma->vm_flags |= VM_DONTCOPY;
157 pfn = (base + idxd_get_wq_portal_full_offset(wq->id,
158 IDXD_PORTAL_LIMITED)) >> PAGE_SHIFT;
159 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
160 vma->vm_private_data = ctx;
162 return io_remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
166 static __poll_t idxd_cdev_poll(struct file *filp,
167 struct poll_table_struct *wait)
169 struct idxd_user_context *ctx = filp->private_data;
170 struct idxd_wq *wq = ctx->wq;
171 struct idxd_device *idxd = wq->idxd;
175 poll_wait(filp, &wq->err_queue, wait);
176 spin_lock_irqsave(&idxd->dev_lock, flags);
177 if (idxd->sw_err.valid)
178 out = EPOLLIN | EPOLLRDNORM;
179 spin_unlock_irqrestore(&idxd->dev_lock, flags);
184 static const struct file_operations idxd_cdev_fops = {
185 .owner = THIS_MODULE,
186 .open = idxd_cdev_open,
187 .release = idxd_cdev_release,
188 .mmap = idxd_cdev_mmap,
189 .poll = idxd_cdev_poll,
192 int idxd_cdev_get_major(struct idxd_device *idxd)
194 return MAJOR(ictx[idxd->type].devt);
197 int idxd_wq_add_cdev(struct idxd_wq *wq)
199 struct idxd_device *idxd = wq->idxd;
200 struct idxd_cdev *idxd_cdev;
203 struct idxd_cdev_context *cdev_ctx;
206 idxd_cdev = kzalloc(sizeof(*idxd_cdev), GFP_KERNEL);
211 cdev = &idxd_cdev->cdev;
212 dev = &idxd_cdev->dev;
213 cdev_ctx = &ictx[wq->idxd->type];
214 minor = ida_simple_get(&cdev_ctx->minor_ida, 0, MINORMASK, GFP_KERNEL);
219 idxd_cdev->minor = minor;
221 device_initialize(dev);
222 dev->parent = &wq->conf_dev;
223 dev->bus = idxd_get_bus_type(idxd);
224 dev->type = &idxd_cdev_device_type;
225 dev->devt = MKDEV(MAJOR(cdev_ctx->devt), minor);
227 rc = dev_set_name(dev, "%s/wq%u.%u", idxd_get_dev_name(idxd),
232 wq->idxd_cdev = idxd_cdev;
233 cdev_init(cdev, &idxd_cdev_fops);
234 rc = cdev_device_add(cdev, dev);
236 dev_dbg(&wq->idxd->pdev->dev, "cdev_add failed: %d\n", rc);
244 wq->idxd_cdev = NULL;
248 void idxd_wq_del_cdev(struct idxd_wq *wq)
250 struct idxd_cdev *idxd_cdev;
251 struct idxd_cdev_context *cdev_ctx;
253 cdev_ctx = &ictx[wq->idxd->type];
254 idxd_cdev = wq->idxd_cdev;
255 wq->idxd_cdev = NULL;
256 cdev_device_del(&idxd_cdev->cdev, &idxd_cdev->dev);
257 put_device(&idxd_cdev->dev);
260 int idxd_cdev_register(void)
264 for (i = 0; i < IDXD_TYPE_MAX; i++) {
265 ida_init(&ictx[i].minor_ida);
266 rc = alloc_chrdev_region(&ictx[i].devt, 0, MINORMASK,
269 goto err_free_chrdev_region;
274 err_free_chrdev_region:
275 for (i--; i >= 0; i--)
276 unregister_chrdev_region(ictx[i].devt, MINORMASK);
281 void idxd_cdev_remove(void)
285 for (i = 0; i < IDXD_TYPE_MAX; i++) {
286 unregister_chrdev_region(ictx[i].devt, MINORMASK);
287 ida_destroy(&ictx[i].minor_ida);