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 enum idxd_cdev_cleanup
{
47 static void idxd_cdev_dev_release(struct device
*dev
)
49 dev_dbg(dev
, "releasing cdev device\n");
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
*idxd_cdev_wq(struct idxd_cdev
*idxd_cdev
)
67 return container_of(idxd_cdev
, struct idxd_wq
, idxd_cdev
);
70 static inline struct idxd_wq
*inode_wq(struct inode
*inode
)
72 return idxd_cdev_wq(inode_idxd_cdev(inode
));
75 static int idxd_cdev_open(struct inode
*inode
, struct file
*filp
)
77 struct idxd_user_context
*ctx
;
78 struct idxd_device
*idxd
;
82 struct iommu_sva
*sva
;
87 dev
= &idxd
->pdev
->dev
;
89 dev_dbg(dev
, "%s called: %d\n", __func__
, idxd_wq_refcount(wq
));
91 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
95 mutex_lock(&wq
->wq_lock
);
97 if (idxd_wq_refcount(wq
) > 0 && wq_dedicated(wq
)) {
103 filp
->private_data
= ctx
;
105 if (device_pasid_enabled(idxd
)) {
106 sva
= iommu_sva_bind_device(dev
, current
->mm
, NULL
);
109 dev_err(dev
, "pasid allocation failed: %d\n", rc
);
113 pasid
= iommu_sva_get_pasid(sva
);
114 if (pasid
== IOMMU_PASID_INVALID
) {
115 iommu_sva_unbind_device(sva
);
122 if (wq_dedicated(wq
)) {
123 rc
= idxd_wq_set_pasid(wq
, pasid
);
125 iommu_sva_unbind_device(sva
);
126 dev_err(dev
, "wq set pasid failed: %d\n", rc
);
133 mutex_unlock(&wq
->wq_lock
);
137 mutex_unlock(&wq
->wq_lock
);
142 static int idxd_cdev_release(struct inode
*node
, struct file
*filep
)
144 struct idxd_user_context
*ctx
= filep
->private_data
;
145 struct idxd_wq
*wq
= ctx
->wq
;
146 struct idxd_device
*idxd
= wq
->idxd
;
147 struct device
*dev
= &idxd
->pdev
->dev
;
150 dev_dbg(dev
, "%s called\n", __func__
);
151 filep
->private_data
= NULL
;
153 /* Wait for in-flight operations to complete. */
155 idxd_device_drain_pasid(idxd
, ctx
->pasid
);
157 if (device_pasid_enabled(idxd
)) {
158 /* The wq disable in the disable pasid function will drain the wq */
159 rc
= idxd_wq_disable_pasid(wq
);
161 dev_err(dev
, "wq disable pasid failed.\n");
168 iommu_sva_unbind_device(ctx
->sva
);
170 mutex_lock(&wq
->wq_lock
);
172 mutex_unlock(&wq
->wq_lock
);
176 static int check_vma(struct idxd_wq
*wq
, struct vm_area_struct
*vma
,
179 struct device
*dev
= &wq
->idxd
->pdev
->dev
;
181 if ((vma
->vm_end
- vma
->vm_start
) > PAGE_SIZE
) {
182 dev_info_ratelimited(dev
,
183 "%s: %s: mapping too large: %lu\n",
185 vma
->vm_end
- vma
->vm_start
);
192 static int idxd_cdev_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
194 struct idxd_user_context
*ctx
= filp
->private_data
;
195 struct idxd_wq
*wq
= ctx
->wq
;
196 struct idxd_device
*idxd
= wq
->idxd
;
197 struct pci_dev
*pdev
= idxd
->pdev
;
198 phys_addr_t base
= pci_resource_start(pdev
, IDXD_WQ_BAR
);
202 dev_dbg(&pdev
->dev
, "%s called\n", __func__
);
203 rc
= check_vma(wq
, vma
, __func__
);
207 vma
->vm_flags
|= VM_DONTCOPY
;
208 pfn
= (base
+ idxd_get_wq_portal_full_offset(wq
->id
,
209 IDXD_PORTAL_LIMITED
)) >> PAGE_SHIFT
;
210 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
211 vma
->vm_private_data
= ctx
;
213 return io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, PAGE_SIZE
,
217 static __poll_t
idxd_cdev_poll(struct file
*filp
,
218 struct poll_table_struct
*wait
)
220 struct idxd_user_context
*ctx
= filp
->private_data
;
221 struct idxd_wq
*wq
= ctx
->wq
;
222 struct idxd_device
*idxd
= wq
->idxd
;
223 struct idxd_cdev
*idxd_cdev
= &wq
->idxd_cdev
;
227 poll_wait(filp
, &idxd_cdev
->err_queue
, wait
);
228 spin_lock_irqsave(&idxd
->dev_lock
, flags
);
229 if (idxd
->sw_err
.valid
)
230 out
= EPOLLIN
| EPOLLRDNORM
;
231 spin_unlock_irqrestore(&idxd
->dev_lock
, flags
);
236 static const struct file_operations idxd_cdev_fops
= {
237 .owner
= THIS_MODULE
,
238 .open
= idxd_cdev_open
,
239 .release
= idxd_cdev_release
,
240 .mmap
= idxd_cdev_mmap
,
241 .poll
= idxd_cdev_poll
,
244 int idxd_cdev_get_major(struct idxd_device
*idxd
)
246 return MAJOR(ictx
[idxd
->type
].devt
);
249 static int idxd_wq_cdev_dev_setup(struct idxd_wq
*wq
)
251 struct idxd_device
*idxd
= wq
->idxd
;
252 struct idxd_cdev
*idxd_cdev
= &wq
->idxd_cdev
;
253 struct idxd_cdev_context
*cdev_ctx
;
257 idxd_cdev
->dev
= kzalloc(sizeof(*idxd_cdev
->dev
), GFP_KERNEL
);
261 dev
= idxd_cdev
->dev
;
262 dev
->parent
= &idxd
->pdev
->dev
;
263 dev_set_name(dev
, "%s/wq%u.%u", idxd_get_dev_name(idxd
),
265 dev
->bus
= idxd_get_bus_type(idxd
);
267 cdev_ctx
= &ictx
[wq
->idxd
->type
];
268 minor
= ida_simple_get(&cdev_ctx
->minor_ida
, 0, MINORMASK
, GFP_KERNEL
);
275 dev
->devt
= MKDEV(MAJOR(cdev_ctx
->devt
), minor
);
276 dev
->type
= &idxd_cdev_device_type
;
277 rc
= device_register(dev
);
279 dev_err(&idxd
->pdev
->dev
, "device register failed\n");
282 idxd_cdev
->minor
= minor
;
287 ida_simple_remove(&cdev_ctx
->minor_ida
, MINOR(dev
->devt
));
290 idxd_cdev
->dev
= NULL
;
294 static void idxd_wq_cdev_cleanup(struct idxd_wq
*wq
,
295 enum idxd_cdev_cleanup cdev_state
)
297 struct idxd_cdev
*idxd_cdev
= &wq
->idxd_cdev
;
298 struct idxd_cdev_context
*cdev_ctx
;
300 cdev_ctx
= &ictx
[wq
->idxd
->type
];
301 if (cdev_state
== CDEV_NORMAL
)
302 cdev_del(&idxd_cdev
->cdev
);
303 device_unregister(idxd_cdev
->dev
);
305 * The device_type->release() will be called on the device and free
306 * the allocated struct device. We can just forget it.
308 ida_simple_remove(&cdev_ctx
->minor_ida
, idxd_cdev
->minor
);
309 idxd_cdev
->dev
= NULL
;
310 idxd_cdev
->minor
= -1;
313 int idxd_wq_add_cdev(struct idxd_wq
*wq
)
315 struct idxd_cdev
*idxd_cdev
= &wq
->idxd_cdev
;
316 struct cdev
*cdev
= &idxd_cdev
->cdev
;
320 rc
= idxd_wq_cdev_dev_setup(wq
);
324 dev
= idxd_cdev
->dev
;
325 cdev_init(cdev
, &idxd_cdev_fops
);
326 cdev_set_parent(cdev
, &dev
->kobj
);
327 rc
= cdev_add(cdev
, dev
->devt
, 1);
329 dev_dbg(&wq
->idxd
->pdev
->dev
, "cdev_add failed: %d\n", rc
);
330 idxd_wq_cdev_cleanup(wq
, CDEV_FAILED
);
334 init_waitqueue_head(&idxd_cdev
->err_queue
);
338 void idxd_wq_del_cdev(struct idxd_wq
*wq
)
340 idxd_wq_cdev_cleanup(wq
, CDEV_NORMAL
);
343 int idxd_cdev_register(void)
347 for (i
= 0; i
< IDXD_TYPE_MAX
; i
++) {
348 ida_init(&ictx
[i
].minor_ida
);
349 rc
= alloc_chrdev_region(&ictx
[i
].devt
, 0, MINORMASK
,
358 void idxd_cdev_remove(void)
362 for (i
= 0; i
< IDXD_TYPE_MAX
; i
++) {
363 unregister_chrdev_region(ictx
[i
].devt
, MINORMASK
);
364 ida_destroy(&ictx
[i
].minor_ida
);