1 From 8d82d92ea697145c32bb36d9f39afd5bb0927bc2 Mon Sep 17 00:00:00 2001
2 From: Yangbo Lu <yangbo.lu@nxp.com>
3 Date: Wed, 27 Sep 2017 10:34:46 +0800
4 Subject: [PATCH] vfio: support layerscape
6 This is a integrated patch for layerscape vfio support.
8 Signed-off-by: Bharat Bhushan <Bharat.Bhushan@nxp.com>
9 Signed-off-by: Eric Auger <eric.auger@redhat.com>
10 Signed-off-by: Robin Murphy <robin.murphy@arm.com>
11 Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
12 Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
14 drivers/vfio/Kconfig | 1 +
15 drivers/vfio/Makefile | 1 +
16 drivers/vfio/fsl-mc/Kconfig | 9 +
17 drivers/vfio/fsl-mc/Makefile | 2 +
18 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 753 ++++++++++++++++++++++++++++++
19 drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c | 199 ++++++++
20 drivers/vfio/fsl-mc/vfio_fsl_mc_private.h | 55 +++
21 drivers/vfio/vfio_iommu_type1.c | 39 +-
22 include/uapi/linux/vfio.h | 1 +
23 9 files changed, 1058 insertions(+), 2 deletions(-)
24 create mode 100644 drivers/vfio/fsl-mc/Kconfig
25 create mode 100644 drivers/vfio/fsl-mc/Makefile
26 create mode 100644 drivers/vfio/fsl-mc/vfio_fsl_mc.c
27 create mode 100644 drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c
28 create mode 100644 drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
30 diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig
31 index da6e2ce7..8a8a33e0 100644
32 --- a/drivers/vfio/Kconfig
33 +++ b/drivers/vfio/Kconfig
34 @@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU
36 source "drivers/vfio/pci/Kconfig"
37 source "drivers/vfio/platform/Kconfig"
38 +source "drivers/vfio/fsl-mc/Kconfig"
39 source "virt/lib/Kconfig"
40 diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile
41 index 7b8a31f6..560f0c67 100644
42 --- a/drivers/vfio/Makefile
43 +++ b/drivers/vfio/Makefile
44 @@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
45 obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o
46 obj-$(CONFIG_VFIO_PCI) += pci/
47 obj-$(CONFIG_VFIO_PLATFORM) += platform/
48 +obj-$(CONFIG_VFIO_FSL_MC) += fsl-mc/
49 diff --git a/drivers/vfio/fsl-mc/Kconfig b/drivers/vfio/fsl-mc/Kconfig
51 index 00000000..b1a527d6
53 +++ b/drivers/vfio/fsl-mc/Kconfig
56 + tristate "VFIO support for QorIQ DPAA2 fsl-mc bus devices"
57 + depends on VFIO && FSL_MC_BUS && EVENTFD
59 + Driver to enable support for the VFIO QorIQ DPAA2 fsl-mc
60 + (Management Complex) devices. This is required to passthrough
61 + fsl-mc bus devices using the VFIO framework.
63 + If you don't know what to do here, say N.
64 diff --git a/drivers/vfio/fsl-mc/Makefile b/drivers/vfio/fsl-mc/Makefile
66 index 00000000..2aca75af
68 +++ b/drivers/vfio/fsl-mc/Makefile
70 +vfio-fsl_mc-y := vfio_fsl_mc.o
71 +obj-$(CONFIG_VFIO_FSL_MC) += vfio_fsl_mc.o vfio_fsl_mc_intr.o
72 diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
74 index 00000000..9dc32d27
76 +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
79 + * Freescale Management Complex (MC) device passthrough using VFIO
81 + * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
82 + * Copyright 2016-2017 NXP
83 + * Author: Bharat Bhushan <bharat.bhushan@nxp.com>
85 + * This file is licensed under the terms of the GNU General Public
86 + * License version 2. This program is licensed "as is" without any
87 + * warranty of any kind, whether express or implied.
90 +#include <linux/device.h>
91 +#include <linux/iommu.h>
92 +#include <linux/module.h>
93 +#include <linux/mutex.h>
94 +#include <linux/slab.h>
95 +#include <linux/types.h>
96 +#include <linux/vfio.h>
97 +#include <linux/delay.h>
99 +#include "../../staging/fsl-mc/include/mc.h"
100 +#include "../../staging/fsl-mc/include/mc-bus.h"
101 +#include "../../staging/fsl-mc/include/mc-sys.h"
102 +#include "../../staging/fsl-mc/bus/dprc-cmd.h"
104 +#include "vfio_fsl_mc_private.h"
106 +#define DRIVER_VERSION "0.10"
107 +#define DRIVER_AUTHOR "Bharat Bhushan <bharat.bhushan@nxp.com>"
108 +#define DRIVER_DESC "VFIO for FSL-MC devices - User Level meta-driver"
110 +static DEFINE_MUTEX(driver_lock);
112 +/* FSl-MC device regions (address and size) are aligned to 64K.
113 + * While MC firmware reports size less than 64K for some objects (it actually
114 + * reports size which does not include reserved space beyond valid bytes).
115 + * Align the size to PAGE_SIZE for userspace to mmap.
117 +static size_t aligned_region_size(struct fsl_mc_device *mc_dev, int index)
121 + size = resource_size(&mc_dev->regions[index]);
122 + return PAGE_ALIGN(size);
125 +static int vfio_fsl_mc_regions_init(struct vfio_fsl_mc_device *vdev)
127 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
128 + int count = mc_dev->obj_desc.region_count;
131 + vdev->regions = kcalloc(count, sizeof(struct vfio_fsl_mc_region),
133 + if (!vdev->regions)
136 + for (i = 0; i < mc_dev->obj_desc.region_count; i++) {
137 + vdev->regions[i].addr = mc_dev->regions[i].start;
138 + vdev->regions[i].size = aligned_region_size(mc_dev, i);
139 + vdev->regions[i].type = VFIO_FSL_MC_REGION_TYPE_MMIO;
140 + if (mc_dev->regions[i].flags & IORESOURCE_CACHEABLE)
141 + vdev->regions[i].type |=
142 + VFIO_FSL_MC_REGION_TYPE_CACHEABLE;
143 + vdev->regions[i].flags = VFIO_REGION_INFO_FLAG_MMAP;
144 + vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
145 + if (!(mc_dev->regions[i].flags & IORESOURCE_READONLY))
146 + vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_WRITE;
149 + vdev->num_regions = mc_dev->obj_desc.region_count;
153 +static void vfio_fsl_mc_regions_cleanup(struct vfio_fsl_mc_device *vdev)
157 + for (i = 0; i < vdev->num_regions; i++)
158 + iounmap(vdev->regions[i].ioaddr);
160 + vdev->num_regions = 0;
161 + kfree(vdev->regions);
164 +static int vfio_fsl_mc_open(void *device_data)
166 + struct vfio_fsl_mc_device *vdev = device_data;
169 + if (!try_module_get(THIS_MODULE))
172 + mutex_lock(&driver_lock);
173 + if (!vdev->refcnt) {
174 + ret = vfio_fsl_mc_regions_init(vdev);
176 + goto error_region_init;
178 + ret = vfio_fsl_mc_irqs_init(vdev);
180 + goto error_irq_init;
184 + mutex_unlock(&driver_lock);
188 + vfio_fsl_mc_regions_cleanup(vdev);
190 + mutex_unlock(&driver_lock);
192 + module_put(THIS_MODULE);
197 +static void vfio_fsl_mc_release(void *device_data)
199 + struct vfio_fsl_mc_device *vdev = device_data;
200 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
202 + mutex_lock(&driver_lock);
204 + if (!(--vdev->refcnt)) {
205 + vfio_fsl_mc_regions_cleanup(vdev);
206 + vfio_fsl_mc_irqs_cleanup(vdev);
209 + if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
210 + dprc_reset_container(mc_dev->mc_io, 0, mc_dev->mc_handle,
211 + mc_dev->obj_desc.id);
213 + mutex_unlock(&driver_lock);
215 + module_put(THIS_MODULE);
218 +static long vfio_fsl_mc_ioctl(void *device_data, unsigned int cmd,
221 + struct vfio_fsl_mc_device *vdev = device_data;
222 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
223 + unsigned long minsz;
225 + if (WARN_ON(!mc_dev))
229 + case VFIO_DEVICE_GET_INFO:
231 + struct vfio_device_info info;
233 + minsz = offsetofend(struct vfio_device_info, num_irqs);
235 + if (copy_from_user(&info, (void __user *)arg, minsz))
238 + if (info.argsz < minsz)
241 + info.flags = VFIO_DEVICE_FLAGS_FSL_MC;
242 + info.num_regions = mc_dev->obj_desc.region_count;
243 + info.num_irqs = mc_dev->obj_desc.irq_count;
245 + return copy_to_user((void __user *)arg, &info, minsz);
247 + case VFIO_DEVICE_GET_REGION_INFO:
249 + struct vfio_region_info info;
251 + minsz = offsetofend(struct vfio_region_info, offset);
253 + if (copy_from_user(&info, (void __user *)arg, minsz))
256 + if (info.argsz < minsz)
259 + if (info.index >= vdev->num_regions)
262 + /* map offset to the physical address */
263 + info.offset = VFIO_FSL_MC_INDEX_TO_OFFSET(info.index);
264 + info.size = vdev->regions[info.index].size;
265 + info.flags = vdev->regions[info.index].flags;
267 + return copy_to_user((void __user *)arg, &info, minsz);
269 + case VFIO_DEVICE_GET_IRQ_INFO:
271 + struct vfio_irq_info info;
273 + minsz = offsetofend(struct vfio_irq_info, count);
274 + if (copy_from_user(&info, (void __user *)arg, minsz))
277 + if (info.argsz < minsz)
280 + if (info.index >= mc_dev->obj_desc.irq_count)
283 + if (vdev->mc_irqs != NULL) {
284 + info.flags = vdev->mc_irqs[info.index].flags;
285 + info.count = vdev->mc_irqs[info.index].count;
288 + * If IRQs are not initialized then these can not
289 + * be configuted and used by user-space/
295 + return copy_to_user((void __user *)arg, &info, minsz);
297 + case VFIO_DEVICE_SET_IRQS:
299 + struct vfio_irq_set hdr;
303 + minsz = offsetofend(struct vfio_irq_set, count);
305 + if (copy_from_user(&hdr, (void __user *)arg, minsz))
308 + if (hdr.argsz < minsz)
311 + if (hdr.index >= mc_dev->obj_desc.irq_count)
314 + if (hdr.start != 0 || hdr.count > 1)
317 + if (hdr.count == 0 &&
318 + (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE) ||
319 + !(hdr.flags & VFIO_IRQ_SET_ACTION_TRIGGER)))
322 + if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
323 + VFIO_IRQ_SET_ACTION_TYPE_MASK))
326 + if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
329 + if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
330 + size = sizeof(uint8_t);
331 + else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
332 + size = sizeof(int32_t);
336 + if (hdr.argsz - minsz < hdr.count * size)
339 + data = memdup_user((void __user *)(arg + minsz),
342 + return PTR_ERR(data);
345 + ret = vfio_fsl_mc_set_irqs_ioctl(vdev, hdr.flags,
346 + hdr.index, hdr.start,
350 + case VFIO_DEVICE_RESET:
359 +static ssize_t vfio_fsl_mc_read(void *device_data, char __user *buf,
360 + size_t count, loff_t *ppos)
362 + struct vfio_fsl_mc_device *vdev = device_data;
363 + unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
364 + loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
365 + struct vfio_fsl_mc_region *region;
369 + /* Read ioctl supported only for DPRC device */
370 + if (strcmp(vdev->mc_dev->obj_desc.type, "dprc"))
373 + if (index >= vdev->num_regions)
376 + region = &vdev->regions[index];
378 + if (!(region->flags & VFIO_REGION_INFO_FLAG_READ))
381 + if (!region->type & VFIO_FSL_MC_REGION_TYPE_MMIO)
384 + if (!region->ioaddr) {
385 + region->ioaddr = ioremap_nocache(region->addr, region->size);
386 + if (!region->ioaddr)
390 + if (count != 64 || off != 0)
393 + for (i = 7; i >= 0; i--)
394 + data[i] = readq(region->ioaddr + i * sizeof(uint64_t));
396 + if (copy_to_user(buf, data, 64))
402 +#define MC_CMD_COMPLETION_TIMEOUT_MS 5000
403 +#define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
405 +static int vfio_fsl_mc_dprc_wait_for_response(void __iomem *ioaddr)
407 + enum mc_cmd_status status;
408 + unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
412 + struct mc_cmd_header *resp_hdr;
415 + header = readq(ioaddr);
418 + resp_hdr = (struct mc_cmd_header *)&header;
419 + status = (enum mc_cmd_status)resp_hdr->status;
420 + if (status != MC_CMD_STATUS_READY)
423 + udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
424 + timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
425 + if (timeout_usecs == 0)
432 +static int vfio_fsl_mc_send_command(void __iomem *ioaddr, uint64_t *cmd_data)
436 + /* Write at command header in the end */
437 + for (i = 7; i >= 0; i--)
438 + writeq(cmd_data[i], ioaddr + i * sizeof(uint64_t));
440 + /* Wait for response before returning to user-space
441 + * This can be optimized in future to even prepare response
442 + * before returning to user-space and avoid read ioctl.
444 + return vfio_fsl_mc_dprc_wait_for_response(ioaddr);
447 +static int vfio_handle_dprc_commands(void __iomem *ioaddr, uint64_t *cmd_data)
449 + uint64_t cmd_hdr = cmd_data[0];
450 + int cmd = (cmd_hdr >> 52) & 0xfff;
453 + case DPRC_CMDID_OPEN:
455 + return vfio_fsl_mc_send_command(ioaddr, cmd_data);
461 +static ssize_t vfio_fsl_mc_write(void *device_data, const char __user *buf,
462 + size_t count, loff_t *ppos)
464 + struct vfio_fsl_mc_device *vdev = device_data;
465 + unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
466 + loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
467 + struct vfio_fsl_mc_region *region;
471 + /* Write ioctl supported only for DPRC device */
472 + if (strcmp(vdev->mc_dev->obj_desc.type, "dprc"))
475 + if (index >= vdev->num_regions)
478 + region = &vdev->regions[index];
480 + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE))
483 + if (!region->type & VFIO_FSL_MC_REGION_TYPE_MMIO)
486 + if (!region->ioaddr) {
487 + region->ioaddr = ioremap_nocache(region->addr, region->size);
488 + if (!region->ioaddr)
492 + if (count != 64 || off != 0)
495 + if (copy_from_user(&data, buf, 64))
498 + ret = vfio_handle_dprc_commands(region->ioaddr, data);
505 +static int vfio_fsl_mc_mmap_mmio(struct vfio_fsl_mc_region region,
506 + struct vm_area_struct *vma)
508 + u64 size = vma->vm_end - vma->vm_start;
511 + pgoff = vma->vm_pgoff &
512 + ((1U << (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
513 + base = pgoff << PAGE_SHIFT;
515 + if (region.size < PAGE_SIZE || base + size > region.size)
518 + * Set the REGION_TYPE_CACHEABLE (QBman CENA regs) to be the
519 + * cache inhibited area of the portal to avoid coherency issues
520 + * if a user migrates to another core.
522 + if (region.type & VFIO_FSL_MC_REGION_TYPE_CACHEABLE)
523 + vma->vm_page_prot = pgprot_cached_ns(vma->vm_page_prot);
525 + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
527 + vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
529 + return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
530 + size, vma->vm_page_prot);
533 +/* Allows mmaping fsl_mc device regions in assigned DPRC */
534 +static int vfio_fsl_mc_mmap(void *device_data, struct vm_area_struct *vma)
536 + struct vfio_fsl_mc_device *vdev = device_data;
537 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
538 + unsigned long size, addr;
541 + index = vma->vm_pgoff >> (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT);
543 + if (vma->vm_end < vma->vm_start)
545 + if (vma->vm_start & ~PAGE_MASK)
547 + if (vma->vm_end & ~PAGE_MASK)
549 + if (!(vma->vm_flags & VM_SHARED))
551 + if (index >= vdev->num_regions)
554 + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
557 + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
558 + && (vma->vm_flags & VM_READ))
561 + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
562 + && (vma->vm_flags & VM_WRITE))
565 + addr = vdev->regions[index].addr;
566 + size = vdev->regions[index].size;
568 + vma->vm_private_data = mc_dev;
570 + if (vdev->regions[index].type & VFIO_FSL_MC_REGION_TYPE_MMIO)
571 + return vfio_fsl_mc_mmap_mmio(vdev->regions[index], vma);
576 +static const struct vfio_device_ops vfio_fsl_mc_ops = {
577 + .name = "vfio-fsl-mc",
578 + .open = vfio_fsl_mc_open,
579 + .release = vfio_fsl_mc_release,
580 + .ioctl = vfio_fsl_mc_ioctl,
581 + .read = vfio_fsl_mc_read,
582 + .write = vfio_fsl_mc_write,
583 + .mmap = vfio_fsl_mc_mmap,
586 +static int vfio_fsl_mc_initialize_dprc(struct vfio_fsl_mc_device *vdev)
588 + struct device *root_dprc_dev;
589 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
590 + struct device *dev = &mc_dev->dev;
591 + struct fsl_mc_bus *mc_bus;
592 + struct irq_domain *mc_msi_domain;
593 + unsigned int irq_count;
596 + /* device must be DPRC */
597 + if (strcmp(mc_dev->obj_desc.type, "dprc"))
600 + /* mc_io must be un-initialized */
601 + WARN_ON(mc_dev->mc_io);
603 + /* allocate a portal from the root DPRC for vfio use */
604 + fsl_mc_get_root_dprc(dev, &root_dprc_dev);
605 + if (WARN_ON(!root_dprc_dev))
608 + ret = fsl_mc_portal_allocate(to_fsl_mc_device(root_dprc_dev),
609 + FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
612 + goto clean_msi_domain;
614 + /* Reset MCP before move on */
615 + ret = fsl_mc_portal_reset(mc_dev->mc_io);
617 + dev_err(dev, "dprc portal reset failed: error = %d\n", ret);
618 + goto free_mc_portal;
621 + /* MSI domain set up */
622 + ret = fsl_mc_find_msi_domain(root_dprc_dev->parent, &mc_msi_domain);
624 + goto free_mc_portal;
626 + dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
628 + ret = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
629 + &mc_dev->mc_handle);
631 + dev_err(dev, "dprc_open() failed: error = %d\n", ret);
632 + goto free_mc_portal;
635 + /* Initialize resource pool */
636 + fsl_mc_init_all_resource_pools(mc_dev);
638 + mc_bus = to_fsl_mc_bus(mc_dev);
640 + if (!mc_bus->irq_resources) {
641 + irq_count = FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS;
642 + ret = fsl_mc_populate_irq_pool(mc_bus, irq_count);
644 + dev_err(dev, "%s: Failed to init irq-pool\n", __func__);
645 + goto clean_resource_pool;
649 + mutex_init(&mc_bus->scan_mutex);
651 + mutex_lock(&mc_bus->scan_mutex);
652 + ret = dprc_scan_objects(mc_dev, mc_dev->driver_override,
654 + mutex_unlock(&mc_bus->scan_mutex);
656 + dev_err(dev, "dprc_scan_objects() fails (%d)\n", ret);
657 + goto clean_irq_pool;
660 + if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
661 + dev_warn(&mc_dev->dev,
662 + "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
663 + irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
669 + fsl_mc_cleanup_irq_pool(mc_bus);
671 +clean_resource_pool:
672 + fsl_mc_cleanup_all_resource_pools(mc_dev);
673 + dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
676 + fsl_mc_portal_free(mc_dev->mc_io);
679 + dev_set_msi_domain(&mc_dev->dev, NULL);
684 +static int vfio_fsl_mc_device_remove(struct device *dev, void *data)
686 + struct fsl_mc_device *mc_dev;
688 + WARN_ON(dev == NULL);
690 + mc_dev = to_fsl_mc_device(dev);
691 + if (WARN_ON(mc_dev == NULL))
694 + fsl_mc_device_remove(mc_dev);
698 +static void vfio_fsl_mc_cleanup_dprc(struct vfio_fsl_mc_device *vdev)
700 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
701 + struct fsl_mc_bus *mc_bus;
703 + /* device must be DPRC */
704 + if (strcmp(mc_dev->obj_desc.type, "dprc"))
707 + device_for_each_child(&mc_dev->dev, NULL, vfio_fsl_mc_device_remove);
709 + mc_bus = to_fsl_mc_bus(mc_dev);
710 + if (dev_get_msi_domain(&mc_dev->dev))
711 + fsl_mc_cleanup_irq_pool(mc_bus);
713 + dev_set_msi_domain(&mc_dev->dev, NULL);
715 + fsl_mc_cleanup_all_resource_pools(mc_dev);
716 + dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
717 + fsl_mc_portal_free(mc_dev->mc_io);
720 +static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
722 + struct iommu_group *group;
723 + struct vfio_fsl_mc_device *vdev;
724 + struct device *dev = &mc_dev->dev;
727 + group = vfio_iommu_group_get(dev);
729 + dev_err(dev, "%s: VFIO: No IOMMU group\n", __func__);
733 + vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
735 + vfio_iommu_group_put(group, dev);
739 + vdev->mc_dev = mc_dev;
741 + ret = vfio_add_group_dev(dev, &vfio_fsl_mc_ops, vdev);
743 + dev_err(dev, "%s: Failed to add to vfio group\n", __func__);
744 + goto free_vfio_device;
747 + /* DPRC container scanned and it's chilren bound with vfio driver */
748 + if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) {
749 + ret = vfio_fsl_mc_initialize_dprc(vdev);
751 + vfio_del_group_dev(dev);
752 + goto free_vfio_device;
755 + struct fsl_mc_device *mc_bus_dev;
757 + /* Non-dprc devices share mc_io from the parent dprc */
758 + mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
759 + if (mc_bus_dev == NULL) {
760 + vfio_del_group_dev(dev);
761 + goto free_vfio_device;
764 + mc_dev->mc_io = mc_bus_dev->mc_io;
766 + /* Inherit parent MSI domain */
767 + dev_set_msi_domain(&mc_dev->dev,
768 + dev_get_msi_domain(mc_dev->dev.parent));
774 + vfio_iommu_group_put(group, dev);
778 +static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
780 + struct vfio_fsl_mc_device *vdev;
781 + struct device *dev = &mc_dev->dev;
783 + vdev = vfio_del_group_dev(dev);
787 + if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
788 + vfio_fsl_mc_cleanup_dprc(vdev);
790 + dev_set_msi_domain(&mc_dev->dev, NULL);
792 + mc_dev->mc_io = NULL;
794 + vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
801 + * vfio-fsl_mc is a meta-driver, so use driver_override interface to
802 + * bind a fsl_mc container with this driver and match_id_table is NULL.
804 +static struct fsl_mc_driver vfio_fsl_mc_driver = {
805 + .probe = vfio_fsl_mc_probe,
806 + .remove = vfio_fsl_mc_remove,
807 + .match_id_table = NULL,
809 + .name = "vfio-fsl-mc",
810 + .owner = THIS_MODULE,
814 +static int __init vfio_fsl_mc_driver_init(void)
816 + return fsl_mc_driver_register(&vfio_fsl_mc_driver);
819 +static void __exit vfio_fsl_mc_driver_exit(void)
821 + fsl_mc_driver_unregister(&vfio_fsl_mc_driver);
824 +module_init(vfio_fsl_mc_driver_init);
825 +module_exit(vfio_fsl_mc_driver_exit);
827 +MODULE_VERSION(DRIVER_VERSION);
828 +MODULE_LICENSE("GPL v2");
829 +MODULE_AUTHOR(DRIVER_AUTHOR);
830 +MODULE_DESCRIPTION(DRIVER_DESC);
831 diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c b/drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c
833 index 00000000..eb244bb0
835 +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c
838 + * Freescale Management Complex (MC) device passthrough using VFIO
840 + * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
841 + * Author: Bharat Bhushan <bharat.bhushan@nxp.com>
843 + * This file is licensed under the terms of the GNU General Public
844 + * License version 2. This program is licensed "as is" without any
845 + * warranty of any kind, whether express or implied.
848 +#include <linux/vfio.h>
849 +#include <linux/slab.h>
850 +#include <linux/types.h>
851 +#include <linux/eventfd.h>
852 +#include <linux/msi.h>
854 +#include "../../staging/fsl-mc/include/mc.h"
855 +#include "vfio_fsl_mc_private.h"
857 +static irqreturn_t vfio_fsl_mc_irq_handler(int irq_num, void *arg)
859 + struct vfio_fsl_mc_irq *mc_irq = (struct vfio_fsl_mc_irq *)arg;
861 + eventfd_signal(mc_irq->trigger, 1);
862 + return IRQ_HANDLED;
865 +static int vfio_fsl_mc_irq_mask(struct vfio_fsl_mc_device *vdev,
866 + unsigned int index, unsigned int start,
867 + unsigned int count, uint32_t flags,
873 +static int vfio_fsl_mc_irq_unmask(struct vfio_fsl_mc_device *vdev,
874 + unsigned int index, unsigned int start,
875 + unsigned int count, uint32_t flags,
881 +static int vfio_set_trigger(struct vfio_fsl_mc_device *vdev,
884 + struct vfio_fsl_mc_irq *irq = &vdev->mc_irqs[index];
885 + struct eventfd_ctx *trigger;
889 + hwirq = vdev->mc_dev->irqs[index]->msi_desc->irq;
890 + if (irq->trigger) {
891 + free_irq(hwirq, irq);
893 + eventfd_ctx_put(irq->trigger);
894 + irq->trigger = NULL;
897 + if (fd < 0) /* Disable only */
900 + irq->name = kasprintf(GFP_KERNEL, "vfio-irq[%d](%s)",
901 + hwirq, dev_name(&vdev->mc_dev->dev));
905 + trigger = eventfd_ctx_fdget(fd);
906 + if (IS_ERR(trigger)) {
908 + return PTR_ERR(trigger);
911 + irq->trigger = trigger;
913 + ret = request_irq(hwirq, vfio_fsl_mc_irq_handler, 0,
917 + eventfd_ctx_put(trigger);
918 + irq->trigger = NULL;
925 +int vfio_fsl_mc_irqs_init(struct vfio_fsl_mc_device *vdev)
927 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
928 + struct vfio_fsl_mc_irq *mc_irq;
932 + /* Device does not support any interrupt */
933 + if (mc_dev->obj_desc.irq_count == 0)
936 + irq_count = mc_dev->obj_desc.irq_count;
938 + mc_irq = kcalloc(irq_count, sizeof(*mc_irq), GFP_KERNEL);
939 + if (mc_irq == NULL)
942 + /* Allocate IRQs */
943 + ret = fsl_mc_allocate_irqs(mc_dev);
949 + for (i = 0; i < irq_count; i++) {
950 + mc_irq[i].count = 1;
951 + mc_irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
954 + vdev->mc_irqs = mc_irq;
959 +/* Free All IRQs for the given MC object */
960 +void vfio_fsl_mc_irqs_cleanup(struct vfio_fsl_mc_device *vdev)
962 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
963 + int irq_count = mc_dev->obj_desc.irq_count;
966 + /* Device does not support any interrupt */
967 + if (mc_dev->obj_desc.irq_count == 0)
970 + for (i = 0; i < irq_count; i++)
971 + vfio_set_trigger(vdev, i, -1);
973 + fsl_mc_free_irqs(mc_dev);
974 + kfree(vdev->mc_irqs);
977 +static int vfio_fsl_mc_set_irq_trigger(struct vfio_fsl_mc_device *vdev,
978 + unsigned int index, unsigned int start,
979 + unsigned int count, uint32_t flags,
982 + struct vfio_fsl_mc_irq *irq = &vdev->mc_irqs[index];
985 + if (!count && (flags & VFIO_IRQ_SET_DATA_NONE))
986 + return vfio_set_trigger(vdev, index, -1);
988 + if (start != 0 || count != 1)
991 + if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
992 + int32_t fd = *(int32_t *)data;
994 + return vfio_set_trigger(vdev, index, fd);
997 + hwirq = vdev->mc_dev->irqs[index]->msi_desc->irq;
999 + if (flags & VFIO_IRQ_SET_DATA_NONE) {
1000 + vfio_fsl_mc_irq_handler(hwirq, irq);
1002 + } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
1003 + uint8_t trigger = *(uint8_t *)data;
1006 + vfio_fsl_mc_irq_handler(hwirq, irq);
1012 +int vfio_fsl_mc_set_irqs_ioctl(struct vfio_fsl_mc_device *vdev,
1013 + uint32_t flags, unsigned int index,
1014 + unsigned int start, unsigned int count,
1017 + int ret = -ENOTTY;
1019 + switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1020 + case VFIO_IRQ_SET_ACTION_MASK:
1021 + ret = vfio_fsl_mc_irq_mask(vdev, index, start, count,
1024 + case VFIO_IRQ_SET_ACTION_UNMASK:
1025 + ret = vfio_fsl_mc_irq_unmask(vdev, index, start, count,
1028 + case VFIO_IRQ_SET_ACTION_TRIGGER:
1029 + ret = vfio_fsl_mc_set_irq_trigger(vdev, index, start,
1030 + count, flags, data);
1036 diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
1037 new file mode 100644
1038 index 00000000..34e75754
1040 +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
1043 + * Freescale Management Complex VFIO private declarations
1045 + * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
1046 + * Copyright 2016 NXP
1047 + * Author: Bharat Bhushan <bharat.bhushan@nxp.com>
1049 + * This file is licensed under the terms of the GNU General Public
1050 + * License version 2. This program is licensed "as is" without any
1051 + * warranty of any kind, whether express or implied.
1054 +#ifndef VFIO_FSL_MC_PRIVATE_H
1055 +#define VFIO_FSL_MC_PRIVATE_H
1057 +#define VFIO_FSL_MC_OFFSET_SHIFT 40
1058 +#define VFIO_FSL_MC_OFFSET_MASK (((u64)(1) << VFIO_FSL_MC_OFFSET_SHIFT) - 1)
1060 +#define VFIO_FSL_MC_OFFSET_TO_INDEX(off) (off >> VFIO_FSL_MC_OFFSET_SHIFT)
1062 +#define VFIO_FSL_MC_INDEX_TO_OFFSET(index) \
1063 + ((u64)(index) << VFIO_FSL_MC_OFFSET_SHIFT)
1065 +struct vfio_fsl_mc_irq {
1068 + struct eventfd_ctx *trigger;
1072 +struct vfio_fsl_mc_region {
1074 +#define VFIO_FSL_MC_REGION_TYPE_MMIO 1
1075 +#define VFIO_FSL_MC_REGION_TYPE_CACHEABLE 2
1078 + resource_size_t size;
1079 + void __iomem *ioaddr;
1082 +struct vfio_fsl_mc_device {
1083 + struct fsl_mc_device *mc_dev;
1086 + struct vfio_fsl_mc_region *regions;
1087 + struct vfio_fsl_mc_irq *mc_irqs;
1090 +int vfio_fsl_mc_irqs_init(struct vfio_fsl_mc_device *vdev);
1091 +void vfio_fsl_mc_irqs_cleanup(struct vfio_fsl_mc_device *vdev);
1092 +int vfio_fsl_mc_set_irqs_ioctl(struct vfio_fsl_mc_device *vdev,
1093 + uint32_t flags, unsigned int index,
1094 + unsigned int start, unsigned int count,
1096 +#endif /* VFIO_PCI_PRIVATE_H */
1097 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
1098 index 1d48e62f..f0a39331 100644
1099 --- a/drivers/vfio/vfio_iommu_type1.c
1100 +++ b/drivers/vfio/vfio_iommu_type1.c
1102 #include <linux/uaccess.h>
1103 #include <linux/vfio.h>
1104 #include <linux/workqueue.h>
1105 +#include <linux/dma-iommu.h>
1106 +#include <linux/irqdomain.h>
1108 #define DRIVER_VERSION "0.2"
1109 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
1110 @@ -720,6 +722,27 @@ static void vfio_test_domain_fgsp(struct vfio_domain *domain)
1111 __free_pages(pages, order);
1114 +static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base)
1116 + struct list_head group_resv_regions;
1117 + struct iommu_resv_region *region, *next;
1120 + INIT_LIST_HEAD(&group_resv_regions);
1121 + iommu_get_group_resv_regions(group, &group_resv_regions);
1122 + list_for_each_entry(region, &group_resv_regions, list) {
1123 + if (region->type == IOMMU_RESV_SW_MSI) {
1124 + *base = region->start;
1130 + list_for_each_entry_safe(region, next, &group_resv_regions, list)
1135 static int vfio_iommu_type1_attach_group(void *iommu_data,
1136 struct iommu_group *iommu_group)
1138 @@ -728,6 +751,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
1139 struct vfio_domain *domain, *d;
1140 struct bus_type *bus = NULL;
1142 + bool resv_msi, msi_remap;
1143 + phys_addr_t resv_msi_base;
1145 mutex_lock(&iommu->lock);
1147 @@ -774,11 +799,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
1151 + resv_msi = vfio_iommu_has_sw_msi(iommu_group, &resv_msi_base);
1153 INIT_LIST_HEAD(&domain->group_list);
1154 list_add(&group->next, &domain->group_list);
1156 - if (!allow_unsafe_interrupts &&
1157 - !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) {
1158 + msi_remap = resv_msi ? irq_domain_check_msi_remap() :
1159 + iommu_capable(bus, IOMMU_CAP_INTR_REMAP);
1161 + if (!allow_unsafe_interrupts && !msi_remap) {
1162 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
1165 @@ -820,6 +849,12 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
1170 + ret = iommu_get_msi_cookie(domain->domain, resv_msi_base);
1175 list_add(&domain->next, &iommu->domain_list);
1177 mutex_unlock(&iommu->lock);
1178 diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
1179 index 255a2113..1bbaa13d 100644
1180 --- a/include/uapi/linux/vfio.h
1181 +++ b/include/uapi/linux/vfio.h
1182 @@ -198,6 +198,7 @@ struct vfio_device_info {
1183 #define VFIO_DEVICE_FLAGS_PCI (1 << 1) /* vfio-pci device */
1184 #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2) /* vfio-platform device */
1185 #define VFIO_DEVICE_FLAGS_AMBA (1 << 3) /* vfio-amba device */
1186 +#define VFIO_DEVICE_FLAGS_FSL_MC (1 << 5) /* vfio-fsl-mc device */
1187 __u32 num_regions; /* Max region index + 1 */
1188 __u32 num_irqs; /* Max IRQ index + 1 */