1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Copyright 2013-2016 Freescale Semiconductor Inc.
4 * Copyright 2016-2017,2019-2020 NXP
7 #include <linux/device.h>
8 #include <linux/iommu.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/vfio.h>
14 #include <linux/fsl/mc.h>
15 #include <linux/delay.h>
16 #include <linux/io-64-nonatomic-hi-lo.h>
18 #include "vfio_fsl_mc_private.h"
20 static struct fsl_mc_driver vfio_fsl_mc_driver;
22 static int vfio_fsl_mc_open_device(struct vfio_device *core_vdev)
24 struct vfio_fsl_mc_device *vdev =
25 container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
26 struct fsl_mc_device *mc_dev = vdev->mc_dev;
27 int count = mc_dev->obj_desc.region_count;
30 vdev->regions = kcalloc(count, sizeof(struct vfio_fsl_mc_region),
35 for (i = 0; i < count; i++) {
36 struct resource *res = &mc_dev->regions[i];
37 int no_mmap = is_fsl_mc_bus_dprc(mc_dev);
39 vdev->regions[i].addr = res->start;
40 vdev->regions[i].size = resource_size(res);
41 vdev->regions[i].type = mc_dev->regions[i].flags & IORESOURCE_BITS;
43 * Only regions addressed with PAGE granularity may be
46 if (!no_mmap && !(vdev->regions[i].addr & ~PAGE_MASK) &&
47 !(vdev->regions[i].size & ~PAGE_MASK))
48 vdev->regions[i].flags |=
49 VFIO_REGION_INFO_FLAG_MMAP;
50 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
51 if (!(mc_dev->regions[i].flags & IORESOURCE_READONLY))
52 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_WRITE;
58 static void vfio_fsl_mc_regions_cleanup(struct vfio_fsl_mc_device *vdev)
60 struct fsl_mc_device *mc_dev = vdev->mc_dev;
63 for (i = 0; i < mc_dev->obj_desc.region_count; i++)
64 iounmap(vdev->regions[i].ioaddr);
68 static int vfio_fsl_mc_reset_device(struct vfio_fsl_mc_device *vdev)
70 struct fsl_mc_device *mc_dev = vdev->mc_dev;
73 if (is_fsl_mc_bus_dprc(vdev->mc_dev)) {
74 return dprc_reset_container(mc_dev->mc_io, 0,
77 DPRC_RESET_OPTION_NON_RECURSIVE);
81 ret = fsl_mc_obj_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
82 mc_dev->obj_desc.type,
86 ret = fsl_mc_obj_reset(mc_dev->mc_io, 0, token);
88 fsl_mc_obj_close(mc_dev->mc_io, 0, token);
91 ret = fsl_mc_obj_close(mc_dev->mc_io, 0, token);
97 static void vfio_fsl_mc_close_device(struct vfio_device *core_vdev)
99 struct vfio_fsl_mc_device *vdev =
100 container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
101 struct fsl_mc_device *mc_dev = vdev->mc_dev;
102 struct device *cont_dev = fsl_mc_cont_dev(&mc_dev->dev);
103 struct fsl_mc_device *mc_cont = to_fsl_mc_device(cont_dev);
106 vfio_fsl_mc_regions_cleanup(vdev);
108 /* reset the device before cleaning up the interrupts */
109 ret = vfio_fsl_mc_reset_device(vdev);
112 dev_warn(&mc_cont->dev,
113 "VFIO_FLS_MC: reset device has failed (%d)\n", ret);
115 vfio_fsl_mc_irqs_cleanup(vdev);
117 fsl_mc_cleanup_irq_pool(mc_cont);
120 static long vfio_fsl_mc_ioctl(struct vfio_device *core_vdev,
121 unsigned int cmd, unsigned long arg)
124 struct vfio_fsl_mc_device *vdev =
125 container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
126 struct fsl_mc_device *mc_dev = vdev->mc_dev;
129 case VFIO_DEVICE_GET_INFO:
131 struct vfio_device_info info;
133 minsz = offsetofend(struct vfio_device_info, num_irqs);
135 if (copy_from_user(&info, (void __user *)arg, minsz))
138 if (info.argsz < minsz)
141 info.flags = VFIO_DEVICE_FLAGS_FSL_MC;
143 if (is_fsl_mc_bus_dprc(mc_dev))
144 info.flags |= VFIO_DEVICE_FLAGS_RESET;
146 info.num_regions = mc_dev->obj_desc.region_count;
147 info.num_irqs = mc_dev->obj_desc.irq_count;
149 return copy_to_user((void __user *)arg, &info, minsz) ?
152 case VFIO_DEVICE_GET_REGION_INFO:
154 struct vfio_region_info info;
156 minsz = offsetofend(struct vfio_region_info, offset);
158 if (copy_from_user(&info, (void __user *)arg, minsz))
161 if (info.argsz < minsz)
164 if (info.index >= mc_dev->obj_desc.region_count)
167 /* map offset to the physical address */
168 info.offset = VFIO_FSL_MC_INDEX_TO_OFFSET(info.index);
169 info.size = vdev->regions[info.index].size;
170 info.flags = vdev->regions[info.index].flags;
172 if (copy_to_user((void __user *)arg, &info, minsz))
176 case VFIO_DEVICE_GET_IRQ_INFO:
178 struct vfio_irq_info info;
180 minsz = offsetofend(struct vfio_irq_info, count);
181 if (copy_from_user(&info, (void __user *)arg, minsz))
184 if (info.argsz < minsz)
187 if (info.index >= mc_dev->obj_desc.irq_count)
190 info.flags = VFIO_IRQ_INFO_EVENTFD;
193 if (copy_to_user((void __user *)arg, &info, minsz))
197 case VFIO_DEVICE_SET_IRQS:
199 struct vfio_irq_set hdr;
202 size_t data_size = 0;
204 minsz = offsetofend(struct vfio_irq_set, count);
206 if (copy_from_user(&hdr, (void __user *)arg, minsz))
209 ret = vfio_set_irqs_validate_and_prepare(&hdr, mc_dev->obj_desc.irq_count,
210 mc_dev->obj_desc.irq_count, &data_size);
215 data = memdup_user((void __user *)(arg + minsz),
218 return PTR_ERR(data);
221 mutex_lock(&vdev->igate);
222 ret = vfio_fsl_mc_set_irqs_ioctl(vdev, hdr.flags,
223 hdr.index, hdr.start,
225 mutex_unlock(&vdev->igate);
230 case VFIO_DEVICE_RESET:
232 return vfio_fsl_mc_reset_device(vdev);
240 static ssize_t vfio_fsl_mc_read(struct vfio_device *core_vdev, char __user *buf,
241 size_t count, loff_t *ppos)
243 struct vfio_fsl_mc_device *vdev =
244 container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
245 unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
246 loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
247 struct fsl_mc_device *mc_dev = vdev->mc_dev;
248 struct vfio_fsl_mc_region *region;
252 if (index >= mc_dev->obj_desc.region_count)
255 region = &vdev->regions[index];
257 if (!(region->flags & VFIO_REGION_INFO_FLAG_READ))
260 if (!region->ioaddr) {
261 region->ioaddr = ioremap(region->addr, region->size);
266 if (count != 64 || off != 0)
269 for (i = 7; i >= 0; i--)
270 data[i] = readq(region->ioaddr + i * sizeof(uint64_t));
272 if (copy_to_user(buf, data, 64))
278 #define MC_CMD_COMPLETION_TIMEOUT_MS 5000
279 #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
281 static int vfio_fsl_mc_send_command(void __iomem *ioaddr, uint64_t *cmd_data)
284 enum mc_cmd_status status;
285 unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
287 /* Write at command parameter into portal */
288 for (i = 7; i >= 1; i--)
289 writeq_relaxed(cmd_data[i], ioaddr + i * sizeof(uint64_t));
291 /* Write command header in the end */
292 writeq(cmd_data[0], ioaddr);
294 /* Wait for response before returning to user-space
295 * This can be optimized in future to even prepare response
296 * before returning to user-space and avoid read ioctl.
300 struct mc_cmd_header *resp_hdr;
302 header = cpu_to_le64(readq_relaxed(ioaddr));
304 resp_hdr = (struct mc_cmd_header *)&header;
305 status = (enum mc_cmd_status)resp_hdr->status;
306 if (status != MC_CMD_STATUS_READY)
309 udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
310 timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
311 if (timeout_usecs == 0)
318 static ssize_t vfio_fsl_mc_write(struct vfio_device *core_vdev,
319 const char __user *buf, size_t count,
322 struct vfio_fsl_mc_device *vdev =
323 container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
324 unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
325 loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
326 struct fsl_mc_device *mc_dev = vdev->mc_dev;
327 struct vfio_fsl_mc_region *region;
331 if (index >= mc_dev->obj_desc.region_count)
334 region = &vdev->regions[index];
336 if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE))
339 if (!region->ioaddr) {
340 region->ioaddr = ioremap(region->addr, region->size);
345 if (count != 64 || off != 0)
348 if (copy_from_user(&data, buf, 64))
351 ret = vfio_fsl_mc_send_command(region->ioaddr, data);
359 static int vfio_fsl_mc_mmap_mmio(struct vfio_fsl_mc_region region,
360 struct vm_area_struct *vma)
362 u64 size = vma->vm_end - vma->vm_start;
366 pgoff = vma->vm_pgoff &
367 ((1U << (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
368 base = pgoff << PAGE_SHIFT;
370 if (region.size < PAGE_SIZE || base + size > region.size)
373 region_cacheable = (region.type & FSL_MC_REGION_CACHEABLE) &&
374 (region.type & FSL_MC_REGION_SHAREABLE);
375 if (!region_cacheable)
376 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
378 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
380 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
381 size, vma->vm_page_prot);
384 static int vfio_fsl_mc_mmap(struct vfio_device *core_vdev,
385 struct vm_area_struct *vma)
387 struct vfio_fsl_mc_device *vdev =
388 container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
389 struct fsl_mc_device *mc_dev = vdev->mc_dev;
392 index = vma->vm_pgoff >> (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT);
394 if (vma->vm_end < vma->vm_start)
396 if (vma->vm_start & ~PAGE_MASK)
398 if (vma->vm_end & ~PAGE_MASK)
400 if (!(vma->vm_flags & VM_SHARED))
402 if (index >= mc_dev->obj_desc.region_count)
405 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
408 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
409 && (vma->vm_flags & VM_READ))
412 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
413 && (vma->vm_flags & VM_WRITE))
416 vma->vm_private_data = mc_dev;
418 return vfio_fsl_mc_mmap_mmio(vdev->regions[index], vma);
421 static const struct vfio_device_ops vfio_fsl_mc_ops = {
422 .name = "vfio-fsl-mc",
423 .open_device = vfio_fsl_mc_open_device,
424 .close_device = vfio_fsl_mc_close_device,
425 .ioctl = vfio_fsl_mc_ioctl,
426 .read = vfio_fsl_mc_read,
427 .write = vfio_fsl_mc_write,
428 .mmap = vfio_fsl_mc_mmap,
431 static int vfio_fsl_mc_bus_notifier(struct notifier_block *nb,
432 unsigned long action, void *data)
434 struct vfio_fsl_mc_device *vdev = container_of(nb,
435 struct vfio_fsl_mc_device, nb);
436 struct device *dev = data;
437 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
438 struct fsl_mc_device *mc_cont = to_fsl_mc_device(mc_dev->dev.parent);
440 if (action == BUS_NOTIFY_ADD_DEVICE &&
441 vdev->mc_dev == mc_cont) {
442 mc_dev->driver_override = kasprintf(GFP_KERNEL, "%s",
443 vfio_fsl_mc_ops.name);
444 if (!mc_dev->driver_override)
445 dev_warn(dev, "VFIO_FSL_MC: Setting driver override for device in dprc %s failed\n",
446 dev_name(&mc_cont->dev));
448 dev_info(dev, "VFIO_FSL_MC: Setting driver override for device in dprc %s\n",
449 dev_name(&mc_cont->dev));
450 } else if (action == BUS_NOTIFY_BOUND_DRIVER &&
451 vdev->mc_dev == mc_cont) {
452 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
454 if (mc_drv && mc_drv != &vfio_fsl_mc_driver)
455 dev_warn(dev, "VFIO_FSL_MC: Object %s bound to driver %s while DPRC bound to vfio-fsl-mc\n",
456 dev_name(dev), mc_drv->driver.name);
462 static int vfio_fsl_mc_init_device(struct vfio_fsl_mc_device *vdev)
464 struct fsl_mc_device *mc_dev = vdev->mc_dev;
467 /* Non-dprc devices share mc_io from parent */
468 if (!is_fsl_mc_bus_dprc(mc_dev)) {
469 struct fsl_mc_device *mc_cont = to_fsl_mc_device(mc_dev->dev.parent);
471 mc_dev->mc_io = mc_cont->mc_io;
475 vdev->nb.notifier_call = vfio_fsl_mc_bus_notifier;
476 ret = bus_register_notifier(&fsl_mc_bus_type, &vdev->nb);
480 /* open DPRC, allocate a MC portal */
481 ret = dprc_setup(mc_dev);
483 dev_err(&mc_dev->dev, "VFIO_FSL_MC: Failed to setup DPRC (%d)\n", ret);
489 bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
493 static int vfio_fsl_mc_scan_container(struct fsl_mc_device *mc_dev)
497 /* non dprc devices do not scan for other devices */
498 if (!is_fsl_mc_bus_dprc(mc_dev))
500 ret = dprc_scan_container(mc_dev, false);
502 dev_err(&mc_dev->dev,
503 "VFIO_FSL_MC: Container scanning failed (%d)\n", ret);
504 dprc_remove_devices(mc_dev, NULL, 0);
510 static void vfio_fsl_uninit_device(struct vfio_fsl_mc_device *vdev)
512 struct fsl_mc_device *mc_dev = vdev->mc_dev;
514 if (!is_fsl_mc_bus_dprc(mc_dev))
517 dprc_cleanup(mc_dev);
518 bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
521 static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
523 struct vfio_fsl_mc_device *vdev;
524 struct device *dev = &mc_dev->dev;
527 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
531 vfio_init_group_dev(&vdev->vdev, dev, &vfio_fsl_mc_ops);
532 vdev->mc_dev = mc_dev;
533 mutex_init(&vdev->igate);
535 if (is_fsl_mc_bus_dprc(mc_dev))
536 ret = vfio_assign_device_set(&vdev->vdev, &mc_dev->dev);
538 ret = vfio_assign_device_set(&vdev->vdev, mc_dev->dev.parent);
542 ret = vfio_fsl_mc_init_device(vdev);
546 ret = vfio_register_group_dev(&vdev->vdev);
548 dev_err(dev, "VFIO_FSL_MC: Failed to add to vfio group\n");
552 ret = vfio_fsl_mc_scan_container(mc_dev);
555 dev_set_drvdata(dev, vdev);
559 vfio_unregister_group_dev(&vdev->vdev);
561 vfio_fsl_uninit_device(vdev);
563 vfio_uninit_group_dev(&vdev->vdev);
568 static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
570 struct device *dev = &mc_dev->dev;
571 struct vfio_fsl_mc_device *vdev = dev_get_drvdata(dev);
573 vfio_unregister_group_dev(&vdev->vdev);
574 mutex_destroy(&vdev->igate);
576 dprc_remove_devices(mc_dev, NULL, 0);
577 vfio_fsl_uninit_device(vdev);
579 vfio_uninit_group_dev(&vdev->vdev);
584 static struct fsl_mc_driver vfio_fsl_mc_driver = {
585 .probe = vfio_fsl_mc_probe,
586 .remove = vfio_fsl_mc_remove,
588 .name = "vfio-fsl-mc",
589 .owner = THIS_MODULE,
591 .driver_managed_dma = true,
594 static int __init vfio_fsl_mc_driver_init(void)
596 return fsl_mc_driver_register(&vfio_fsl_mc_driver);
599 static void __exit vfio_fsl_mc_driver_exit(void)
601 fsl_mc_driver_unregister(&vfio_fsl_mc_driver);
604 module_init(vfio_fsl_mc_driver_init);
605 module_exit(vfio_fsl_mc_driver_exit);
607 MODULE_LICENSE("Dual BSD/GPL");
608 MODULE_DESCRIPTION("VFIO for FSL-MC devices - User Level meta-driver");