Skip to contentSkip to conversation

VFIO support for platform devices on ARM

RFC PATCH Superseded Antonios Motakis 9 participants 55 messages Jun 5, 2014 – Sep 11, 2014 1 message not in the archive

Subsystems (5)
KVM Driver core IOMMU VFIO driver +1 more subsystems

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v6 cover VFIO support for platform devices on ARM

This patch series aims to implement VFIO support for platform devices that reside behind an IOMMU. Examples of such devices are devices behind an ARM SMMU, or behind a Samsung Exynos System MMU.

The effort on VFIO_PLATFORM has been partially conducted under the SAVE FP7 project.
http://www.virtualopensystems.com/en/research/innovation-projects/fp7-save/

This version of the VFIO_PLATFORM patch series includes a number of fixes, and introduces more complete support for eventfds. An eventfd can now be set by the user, that can be used to mask and unmask a given interrupt.

The API used is based on the existing VFIO API that is also used with PCI devices. Only devices that include a basic set of IRQs and memory regions are targeted; devices with complex relationships with other devices on a device tree are not taken into account at this stage.

Using the VFIO_IOMMU_TYPE1 driver with the VFIO_PLATFORM driver introduced here, the standard VFIO ioctls have been found to be working on FastModels with an ARM SMMU (MMU400). Testing was based on the ARM PL330 DMA Controller featured on those models. Reading/writing to and MMAPing device memory regions is also supported via the device file descriptor.

Kim Phillip's driver_override patch for platform bus devices is included in the patch series.

A copy with all the dependencies applied can be cloned from branch vfio-platform-v6 at [email protected]:virtualopensystems/linux-kvm-arm.git

For those who want to apply manually, these patches are based on Linux version 3.15-rc8.

Changes since v5:
- Full eventfd support for IRQ masking and unmasking.
- Changed IOMMU_EXEC to IOMMU_NOEXEC, along with related flags in VFIO.
- Other fixes based on reviewer comments.
Changes since v4:
- Use static offsets for each region in the VFIO device fd
- Include patch in the series for the ARM SMMU to expose IOMMU_EXEC availability via IOMMU_CAP_DMA_EXEC
- Rebased on VFIO multi domain support:
- IOMMU_EXEC is now available if at least one IOMMU in the container supports it
- Expose IOMMU_EXEC if available via the capability VFIO_IOMMU_PROT_EXEC
- Some bug fixes
Changes since v3:
- Use Kim Phillips' driver_probe_device()
Changes since v2:
- Fixed Read/Write and MMAP on device regions
- Removed dependency on Device Tree
- Interrupts support
- Interrupt masking/unmasking
- Automask level sensitive interrupts
- Introduced VFIO_DMA_MAP_FLAG_EXEC
- Code clean ups

Antonios Motakis (19):
iommu/arm-smmu: change IOMMU_EXEC to IOMMU_NOEXEC
iommu: add capability IOMMU_CAP_NOEXEC
iommu/arm-smmu: add IOMMU_CAP_NOEXEC to the ARM SMMU driver iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP vfio/iommu_type1: support for platform bus devices on ARM
vfio: introduce the VFIO_DMA_MAP_FLAG_NOEXEC flag vfio/iommu_type1: implement the VFIO_DMA_MAP_FLAG_NOEXEC flag vfio/platform: initial skeleton of VFIO support for platform devices vfio/platform: return info for device and its memory mapped IO regions vfio/platform: read and write support for the device fd vfio/platform: support MMAP of MMIO regions
vfio/platform: return IRQ info
vfio/platform: initial interrupts support
vfio/platform: support for maskable and automasked interrupts
vfio: move eventfd support code for VFIO_PCI to a sepparate file
vfio: add local lock in virqfd instead of depending on VFIO PCI
vfio: pass an opaque pointer on virqfd initialization
vfio: initialize the virqfd workqueue in VFIO generic code vfio/platform: implement IRQ masking/unmasking via an eventfd

Kim Phillips (1):
driver core: platform: add device binding path 'driver_override'

 Documentation/ABI/testing/sysfs-bus-platform  |  20 ++
 drivers/base/platform.c                       |  47 +++
 drivers/iommu/arm-smmu.c                      |   4 +-
 drivers/vfio/Kconfig                          |   3 +-
 drivers/vfio/Makefile                         |   3 +-
 drivers/vfio/pci/vfio_pci.c                   |   8 -
 drivers/vfio/pci/vfio_pci_intrs.c             | 234 +-------------
 drivers/vfio/pci/vfio_pci_private.h           |   3 -
 drivers/vfio/platform/Kconfig                 |   9 +
 drivers/vfio/platform/Makefile                |   4 +
 drivers/vfio/platform/vfio_platform.c         | 445 ++++++++++++++++++++++++++
 drivers/vfio/platform/vfio_platform_irq.c     | 343 ++++++++++++++++++++
 drivers/vfio/platform/vfio_platform_private.h |  62 ++++
 drivers/vfio/vfio.c                           |   8 +
 drivers/vfio/vfio_iommu_type1.c               |  30 +-
 drivers/vfio/virqfd.c                         | 213 ++++++++++++
 include/linux/iommu.h                         |   7 +-
 include/linux/platform_device.h               |   1 +
 include/linux/vfio.h                          |  26 ++
 include/uapi/linux/vfio.h                     |   3 +
 20 files changed, 1233 insertions(+), 240 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-platform
 create mode 100644 drivers/vfio/platform/Kconfig
 create mode 100644 drivers/vfio/platform/Makefile
 create mode 100644 drivers/vfio/platform/vfio_platform.c
 create mode 100644 drivers/vfio/platform/vfio_platform_irq.c
 create mode 100644 drivers/vfio/platform/vfio_platform_private.h
 create mode 100644 drivers/vfio/virqfd.c
Signature · 1 line
1.8.3.2
#1 lore raw

RFC PATCH v6 1/20 iommu/arm-smmu: change IOMMU_EXEC to IOMMU_NOEXEC

Exposing the XN flag of the SMMU driver as IOMMU_NOEXEC instead of IOMMU_EXEC makes it enforceable, since for IOMMUs that don't support the XN flag pages will always be executable.

---
 drivers/iommu/arm-smmu.c | 2 +-
 include/linux/iommu.h    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/arm-smmu.c +1 -1
index 647c3c7..d5a2200 100644
@@ -1294,7 +1294,7 @@ static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
 	}
 
 	/* If no access, create a faulting entry to avoid TLB fills */
-	if (prot & IOMMU_EXEC)
+	if (!(prot & IOMMU_NOEXEC))
 		pteval &= ~ARM_SMMU_PTE_XN;
 	else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
 		pteval &= ~ARM_SMMU_PTE_PAGE;
include/linux/iommu.h +1 -1
index b96a5b2..fc464d2 100644
@@ -27,7 +27,7 @@
 #define IOMMU_READ	(1 << 0)
 #define IOMMU_WRITE	(1 << 1)
 #define IOMMU_CACHE	(1 << 2) /* DMA cache coherency */
-#define IOMMU_EXEC	(1 << 3)
+#define IOMMU_NOEXEC	(1 << 3)
 
 struct iommu_ops;
 struct iommu_group;
Signature · 1 line
1.8.3.2
#3 lore raw

RFC PATCH v6 2/20 iommu: add capability IOMMU_CAP_NOEXEC

In reply to Antonios Motakis

Some IOMMUs accept an IOMMU_NOEXEC protection flag in addition to IOMMU_READ and IOMMU_WRITE. Expose this as an IOMMU capability.

---
 include/linux/iommu.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/iommu.h +3 -2
index fc464d2..7e152fb 100644
@@ -57,8 +57,9 @@ struct iommu_domain {
 	struct iommu_domain_geometry geometry;
 };
 
-#define IOMMU_CAP_CACHE_COHERENCY	0x1
-#define IOMMU_CAP_INTR_REMAP		0x2	/* isolates device intrs */
+#define IOMMU_CAP_CACHE_COHERENCY	(1 << 0)
+#define IOMMU_CAP_INTR_REMAP		(1 << 1) /* isolates device intrs */
+#define IOMMU_CAP_NOEXEC		(1 << 2) /* IOMMU_NOEXEC flag */
 
 /*
  * Following constraints are specifc to FSL_PAMUV1:
Signature · 1 line
1.8.3.2
#7 lore raw

RFC PATCH v6 3/20 iommu/arm-smmu: add IOMMU_CAP_NOEXEC to the ARM SMMU driver

In reply to Antonios Motakis

The ARM SMMU supports the IOMMU_NOEXEC protection flag. Add the corresponding IOMMU capability.

---
 drivers/iommu/arm-smmu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/arm-smmu.c +2 -0
index d5a2200..15ab2af 100644
@@ -1544,6 +1544,8 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
+	caps |= IOMMU_CAP_NOEXEC;
+
 	return !!(cap & caps);
 }
 
Signature · 1 line
1.8.3.2
#11 lore raw

RFC PATCH v6 4/20 iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

In reply to Antonios Motakis

With an ARM SMMU, interrupt remapping should always be safe from the SMMU's point of view, as it is properly handled by the GIC.

---
 drivers/iommu/arm-smmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm-smmu.c +1 -1
index 15ab2af..ff29402 100644
@@ -1544,7 +1544,7 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
-	caps |= IOMMU_CAP_NOEXEC;
+	caps |= IOMMU_CAP_NOEXEC | IOMMU_CAP_INTR_REMAP;
 
 	return !!(cap & caps);
 }
Signature · 1 line
1.8.3.2
#29 lore raw

RFC PATCH v6 5/20 vfio/iommu_type1: support for platform bus devices on ARM

In reply to Antonios Motakis

This allows to make use of the VFIO_IOMMU_TYPE1 driver with platform devices on ARM. The driver can then be used with an Exynos SMMU, or ARM SMMU driver.

---
 drivers/vfio/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/Kconfig +1 -1
index af7b204..3a598ed 100644
@@ -11,7 +11,7 @@ config VFIO_IOMMU_SPAPR_TCE
 menuconfig VFIO
 	tristate "VFIO Non-Privileged userspace driver framework"
 	depends on IOMMU_API
-	select VFIO_IOMMU_TYPE1 if X86
+	select VFIO_IOMMU_TYPE1 if X86 || ARM
 	select VFIO_IOMMU_SPAPR_TCE if (PPC_POWERNV || PPC_PSERIES)
 	select ANON_INODES
 	help
Signature · 1 line
1.8.3.2
#30 lore raw

RFC PATCH v6 6/20 vfio: introduce the VFIO_DMA_MAP_FLAG_NOEXEC flag

In reply to Antonios Motakis

We introduce the VFIO_DMA_MAP_FLAG_NOEXEC flag to the VFIO dma map call, and expose its availability via the capability VFIO_IOMMU_PROT_NOEXEC. This way the user can control whether the XN flag will be set on the requested mappings. The IOMMU_NOEXEC flag needs to be available for all the IOMMUs of the container used.

---
 include/uapi/linux/vfio.h | 2 ++
 1 file changed, 2 insertions(+)
include/uapi/linux/vfio.h +2 -0
index cb9023d..29d234c 100644
@@ -29,6 +29,7 @@
  * capability is subject to change as groups are added or removed.
  */
 #define VFIO_DMA_CC_IOMMU		4
+#define VFIO_IOMMU_PROT_NOEXEC		5
 
 /*
  * The IOCTL interface is designed for extensibility by embedding the
@@ -398,6 +399,7 @@ struct vfio_iommu_type1_dma_map {
 	__u32	flags;
 #define VFIO_DMA_MAP_FLAG_READ (1 << 0)		/* readable from device */
 #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)	/* writable from device */
+#define VFIO_DMA_MAP_FLAG_NOEXEC (1 << 2)	/* not executable from device */
 	__u64	vaddr;				/* Process virtual address */
 	__u64	iova;				/* IO virtual address */
 	__u64	size;				/* Size of mapping (bytes) */
Signature · 1 line
1.8.3.2
#31 lore raw

RFC PATCH v6 7/20 vfio/iommu_type1: implement the VFIO_DMA_MAP_FLAG_NOEXEC flag

In reply to Antonios Motakis

Some IOMMU drivers, such as the ARM SMMU driver, make available the IOMMU_NOEXEC flag, to set the page tables for a device as XN (execute never). This affects devices such as the ARM PL330 DMA Controller, which respects this flag and will refuse to fetch DMA instructions from memory where the XN flag has been set.

---
 drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +29 -1
index 6673e7b..e2566fd 100644
@@ -80,6 +80,24 @@ struct vfio_group {
 	struct list_head	next;
 };
 
+static int vfio_domains_have_cap_noexec(struct vfio_iommu *iommu)
+{
+	struct vfio_domain *d;
+	int ret = 1;
+
+	mutex_lock(&iommu->lock);
+	list_for_each_entry(d, &iommu->domain_list, next) {
+		if (!iommu_domain_has_cap(d->domain, IOMMU_CAP_NOEXEC)) {
+			ret = 0;
+			break;
+		}
+	}
+	mutex_unlock(&iommu->lock);
+
+	return ret;
+}
+
+
 /*
  * This code handles mapping and unmapping of user data buffers
  * into DMA'ble space using the IOMMU
@@ -542,6 +560,11 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 		prot |= IOMMU_WRITE;
 	if (map->flags & VFIO_DMA_MAP_FLAG_READ)
 		prot |= IOMMU_READ;
+	if (map->flags & VFIO_DMA_MAP_FLAG_NOEXEC) {
+		if (!vfio_domains_have_cap_noexec(iommu))
+			return -EINVAL;
+		prot |= IOMMU_NOEXEC;
+	}
 
 	if (!prot)
 		return -EINVAL; /* No READ/WRITE? */
@@ -899,6 +922,10 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
 			if (!iommu)
 				return 0;
 			return vfio_domains_have_iommu_cache(iommu);
+		case VFIO_IOMMU_PROT_NOEXEC:
+			if (!iommu)
+				return 0;
+			return vfio_domains_have_cap_noexec(iommu);
 		default:
 			return 0;
 		}
@@ -922,7 +949,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
 	} else if (cmd == VFIO_IOMMU_MAP_DMA) {
 		struct vfio_iommu_type1_dma_map map;
 		uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
-				VFIO_DMA_MAP_FLAG_WRITE;
+				VFIO_DMA_MAP_FLAG_WRITE |
+				VFIO_DMA_MAP_FLAG_NOEXEC;
 
 		minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
Signature · 1 line
1.8.3.2
#33 lore raw

RFC PATCH v6 8/20 driver core: platform: add device binding path 'driver_override'

In reply to Antonios Motakis

From: Kim Phillips <[email protected]>

Needed by platform device drivers, such as the upcoming vfio-platform driver, in order to bypass the existing OF, ACPI, id_table and name string matches, and successfully be able to be bound to any device, like so:

echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override echo fff51000.ethernet > /sys/bus/platform/devices/fff51000.ethernet/driver/unbind echo fff51000.ethernet > /sys/bus/platform/drivers_probe

This mimics "PCI: Introduce new device binding path using pci_dev.driver_override", which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.

---
 Documentation/ABI/testing/sysfs-bus-platform | 20 ++++++++++++
 drivers/base/platform.c                      | 47 ++++++++++++++++++++++++++++
 include/linux/platform_device.h              |  1 +
 3 files changed, 68 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-platform
Documentation/ABI/testing/sysfs-bus-platform new file +20 -0
new file mode 100644
index 0000000..5172a61
@@ -0,0 +1,20 @@
+What:		/sys/bus/platform/devices/.../driver_override
+Date:		April 2014
+Contact:	Kim Phillips <[email protected]>
+Description:
+		This file allows the driver for a device to be specified which
+		will override standard OF, ACPI, ID table, and name matching.
+		When specified, only a driver with a name matching the value
+		written to driver_override will have an opportunity to bind
+		to the device.  The override is specified by writing a string
+		to the driver_override file (echo vfio-platform > \
+		driver_override) and may be cleared with an empty string
+		(echo > driver_override).  This returns the device to standard
+		matching rules binding.  Writing to driver_override does not
+		automatically unbind the device from its current driver or make
+		any attempt to automatically load the specified driver.  If no
+		driver with a matching name is currently loaded in the kernel,
+		the device will not bind to any driver.  This also allows
+		devices to opt-out of driver binding using a driver_override
+		name such as "none".  Only a single driver may be specified in
+		the override, there is no support for parsing delimiters.
drivers/base/platform.c +47 -0

73 lines not on this page · raw message

include/linux/platform_device.h +1 -0
index 16f6654..153d303 100644
@@ -28,6 +28,7 @@ struct platform_device {
 	struct resource	*resource;
 
 	const struct platform_device_id	*id_entry;
+	char *driver_override; /* Driver name to force a match */
 
 	/* MFD cell pointer */
 	struct mfd_cell *mfd_cell;
Signature · 1 line
1.8.3.2
#34 lore raw

RFC PATCH v6 9/20 vfio/platform: initial skeleton of VFIO support for platform devices

In reply to Antonios Motakis

This patch forms the skeleton for platform devices support with VFIO.

---
 drivers/vfio/Kconfig                          |   1 +
 drivers/vfio/Makefile                         |   1 +
 drivers/vfio/platform/Kconfig                 |   9 ++
 drivers/vfio/platform/Makefile                |   4 +
 drivers/vfio/platform/vfio_platform.c         | 172 ++++++++++++++++++++++++++
 drivers/vfio/platform/vfio_platform_private.h |  22 ++++
 include/uapi/linux/vfio.h                     |   1 +
 7 files changed, 210 insertions(+)
 create mode 100644 drivers/vfio/platform/Kconfig
 create mode 100644 drivers/vfio/platform/Makefile
 create mode 100644 drivers/vfio/platform/vfio_platform.c
 create mode 100644 drivers/vfio/platform/vfio_platform_private.h
drivers/vfio/Kconfig +1 -0
index 3a598ed..a484d72 100644
@@ -21,3 +21,4 @@ menuconfig VFIO
 	  If you don't know what to do here, say N.
 
 source "drivers/vfio/pci/Kconfig"
+source "drivers/vfio/platform/Kconfig"
drivers/vfio/Makefile +1 -0
index 72bfabc..b5e4a33 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_VFIO) += vfio.o
 obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
 obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
 obj-$(CONFIG_VFIO_PCI) += pci/
+obj-$(CONFIG_VFIO_PLATFORM) += platform/
drivers/vfio/platform/Kconfig new file +9 -0
new file mode 100644
index 0000000..c51af17
@@ -0,0 +1,9 @@
+config VFIO_PLATFORM
+	tristate "VFIO support for platform devices"
+	depends on VFIO && EVENTFD && ARM
+	help
+	  Support for platform devices with VFIO. This is required to make
+	  use of platform devices present on the system using the VFIO
+	  framework.
+
+	  If you don't know what to do here, say N.
drivers/vfio/platform/Makefile new file +4 -0
new file mode 100644
index 0000000..df3a014
@@ -0,0 +1,4 @@
+
+vfio-platform-y := vfio_platform.o
+
+obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.o
drivers/vfio/platform/vfio_platform.c new file +172 -0

172 lines not on this page · raw message

drivers/vfio/platform/vfio_platform_private.h new file +22 -0
new file mode 100644
index 0000000..4ae88f8
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2013 - Virtual Open Systems
+ * Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef VFIO_PLATFORM_PRIVATE_H
+#define VFIO_PLATFORM_PRIVATE_H
+
+struct vfio_platform_device {
+	struct platform_device		*pdev;
+};
+
+#endif /* VFIO_PLATFORM_PRIVATE_H */
include/uapi/linux/vfio.h +1 -0
index 29d234c..d381107 100644
@@ -155,6 +155,7 @@ struct vfio_device_info {
 	__u32	flags;
 #define VFIO_DEVICE_FLAGS_RESET	(1 << 0)	/* Device supports reset */
 #define VFIO_DEVICE_FLAGS_PCI	(1 << 1)	/* vfio-pci device */
+#define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)	/* vfio-platform device */
 	__u32	num_regions;	/* Max region index + 1 */
 	__u32	num_irqs;	/* Max IRQ index + 1 */
 };
Signature · 1 line
1.8.3.2
#35 lore raw

RFC PATCH v6 10/20 vfio/platform: return info for device and its memory mapped IO regions

In reply to Antonios Motakis

A VFIO userspace driver will start by opening the VFIO device that corresponds to an IOMMU group, and will use the ioctl interface to get the basic device info, such as number of memory regions and interrupts, and their properties.

This patch enables the IOCTLs:
- VFIO_DEVICE_GET_INFO
- VFIO_DEVICE_GET_REGION_INFO

IRQ info is provided by one of the latter patches.

---
 drivers/vfio/platform/vfio_platform.c         | 79 +++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h | 17 ++++++
 2 files changed, 90 insertions(+), 6 deletions(-)
drivers/vfio/platform/vfio_platform.c +73 -6
index 1df76d8..eeaebc4 100644
@@ -34,17 +34,66 @@
 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>"
 #define DRIVER_DESC     "VFIO for platform devices - User Level meta-driver"
 
+static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
+{
+	int cnt = 0, i;
+
+	while (platform_get_resource(vdev->pdev, IORESOURCE_MEM, cnt))
+		cnt++;
+
+	vdev->region = kzalloc(sizeof(struct vfio_platform_region) * cnt,
+				GFP_KERNEL);
+	if (!vdev->region)
+		return -ENOMEM;
+
+	for (i = 0; i < cnt;  i++) {
+		struct resource *res =
+			platform_get_resource(vdev->pdev, IORESOURCE_MEM, i);
+
+		if (!res)
+			goto err;
+
+		vdev->region[i].addr = res->start;
+		vdev->region[i].size = resource_size(res);
+		vdev->region[i].flags = 0;
+	}
+
+	vdev->num_regions = cnt;
+
+	return 0;
+err:
+	kfree(vdev->region);
+	return -EINVAL;
+}
+
+static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
+{
+	vdev->num_regions = 0;
+	kfree(vdev->region);
+}
+
 static void vfio_platform_release(void *device_data)
 {
+	struct vfio_platform_device *vdev = device_data;
+
+	vfio_platform_regions_cleanup(vdev);
+
 	module_put(THIS_MODULE);
 }
 
 static int vfio_platform_open(void *device_data)
 {
+	struct vfio_platform_device *vdev = device_data;
+	int ret;
+
 	if (!try_module_get(THIS_MODULE))
 		return -ENODEV;
 
-	return 0;
+	ret = vfio_platform_regions_init(vdev);
+	if (ret)
+		module_put(THIS_MODULE);
+
+	return ret;
 }
 
 static long vfio_platform_ioctl(void *device_data,
@@ -65,18 +114,36 @@ static long vfio_platform_ioctl(void *device_data,
 			return -EINVAL;
 
 		info.flags = VFIO_DEVICE_FLAGS_PLATFORM;
-		info.num_regions = 0;
+		info.num_regions = vdev->num_regions;
 		info.num_irqs = 0;
 
 		return copy_to_user((void __user *)arg, &info, minsz);
 
-	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO)
-		return -EINVAL;
+	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
+		struct vfio_region_info info;
+
+		minsz = offsetofend(struct vfio_region_info, offset);
+
+		if (copy_from_user(&info, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (info.argsz < minsz)
+			return -EINVAL;
+
+		if (info.index >= vdev->num_regions)
+			return -EINVAL;
+
+		/* map offset to the physical address  */
+		info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
+		info.size = vdev->region[info.index].size;
+		info.flags = vdev->region[info.index].flags;
+
+		return copy_to_user((void __user *)arg, &info, minsz);
 
-	else if (cmd == VFIO_DEVICE_GET_IRQ_INFO)
+	} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
 		return -EINVAL;
 
-	else if (cmd == VFIO_DEVICE_SET_IRQS)
+	} else if (cmd == VFIO_DEVICE_SET_IRQS)
 		return -EINVAL;
 
 	else if (cmd == VFIO_DEVICE_RESET)
drivers/vfio/platform/vfio_platform_private.h +17 -0
index 4ae88f8..3448f918 100644
@@ -15,8 +15,25 @@
 #ifndef VFIO_PLATFORM_PRIVATE_H
 #define VFIO_PLATFORM_PRIVATE_H
 
+#define VFIO_PLATFORM_OFFSET_SHIFT   40
+#define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1)
+
+#define VFIO_PLATFORM_OFFSET_TO_INDEX(off)	\
+	(off >> VFIO_PLATFORM_OFFSET_SHIFT)
+
+#define VFIO_PLATFORM_INDEX_TO_OFFSET(index)	\
+	((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)
+
+struct vfio_platform_region {
+	u64			addr;
+	resource_size_t		size;
+	u32			flags;
+};
+
 struct vfio_platform_device {
 	struct platform_device		*pdev;
+	struct vfio_platform_region	*region;
+	u32				num_regions;
 };
 
 #endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 1 line
1.8.3.2
#38 lore raw

RFC PATCH v6 11/20 vfio/platform: read and write support for the device fd

In reply to Antonios Motakis

VFIO returns a file descriptor which we can use to manipulate the memory regions of the device. Usually, the user will mmap memory regions that are addressable on page boundaries, however for memory regions where this is not the case we cannot provide mmap functionality due to security concerns. For this reason we also need allow to read and write to the memory regions via the file descriptor.

---
 drivers/vfio/platform/vfio_platform.c         | 116 +++++++++++++++++++++++++-
 drivers/vfio/platform/vfio_platform_private.h |   1 +
 2 files changed, 114 insertions(+), 3 deletions(-)
drivers/vfio/platform/vfio_platform.c +113 -3
index eeaebc4..c66afbc 100644
@@ -55,7 +55,8 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
 
 		vdev->region[i].addr = res->start;
 		vdev->region[i].size = resource_size(res);
-		vdev->region[i].flags = 0;
+		vdev->region[i].flags = VFIO_REGION_INFO_FLAG_READ
+					| VFIO_REGION_INFO_FLAG_WRITE;
 	}
 
 	vdev->num_regions = cnt;
@@ -155,13 +156,122 @@ static long vfio_platform_ioctl(void *device_data,
 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
 			     size_t count, loff_t *ppos)
 {
-	return -EINVAL;
+	struct vfio_platform_device *vdev = device_data;
+	unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
+	loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
+	unsigned int done = 0;
+
+	if (index >= vdev->num_regions)
+		return -EINVAL;
+
+	if (!vdev->region[index].ioaddr) {
+		vdev->region[index].ioaddr =
+			ioremap_nocache(vdev->region[index].addr,
+					vdev->region[index].size);
+
+		if (!vdev->region[index].ioaddr)
+			return -ENOMEM;
+	}
+
+	while (count) {
+		size_t filled;
+
+		if (count >= 4 && !(off % 4)) {
+			u32 val;
+
+			val = ioread32(vdev->region[index].ioaddr + off);
+			if (copy_to_user(buf, &val, 4))
+				goto err;
+
+			filled = 4;
+		} else if (count >= 2 && !(off % 2)) {
+			u16 val;
+
+			val = ioread16(vdev->region[index].ioaddr + off);
+			if (copy_to_user(buf, &val, 2))
+				goto err;
+
+			filled = 2;
+		} else {
+			u8 val;
+
+			val = ioread8(vdev->region[index].ioaddr + off);
+			if (copy_to_user(buf, &val, 1))
+				goto err;
+
+			filled = 1;
+		}
+
+
+		count -= filled;
+		done += filled;
+		off += filled;
+		buf += filled;
+	}
+
+	return done;
+err:
+	return -EFAULT;
 }
 
 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
 			      size_t count, loff_t *ppos)
 {
-	return -EINVAL;
+	struct vfio_platform_device *vdev = device_data;
+	unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
+	loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
+	unsigned int done = 0;
+
+	if (index >= vdev->num_regions)
+		return -EINVAL;
+
+	if (!vdev->region[index].ioaddr) {
+		vdev->region[index].ioaddr =
+			ioremap_nocache(vdev->region[index].addr,
+					vdev->region[index].size);
+
+		if (!vdev->region[index].ioaddr)
+			return -ENOMEM;
+	}
+
+	while (count) {
+		size_t filled;
+
+		if (count >= 4 && !(off % 4)) {
+			u32 val;
+
+			if (copy_from_user(&val, buf, 4))
+				goto err;
+			iowrite32(val, vdev->region[index].ioaddr + off);
+
+			filled = 4;
+		} else if (count >= 2 && !(off % 2)) {
+			u16 val;
+
+			if (copy_from_user(&val, buf, 2))
+				goto err;
+			iowrite16(val, vdev->region[index].ioaddr + off);
+
+			filled = 2;
+		} else {
+			u8 val;
+
+			if (copy_from_user(&val, buf, 1))
+				goto err;
+			iowrite8(val, vdev->region[index].ioaddr + off);
+
+			filled = 1;
+		}
+
+		count -= filled;
+		done += filled;
+		off += filled;
+		buf += filled;
+	}
+
+	return done;
+err:
+	return -EFAULT;
 }
 
 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
drivers/vfio/platform/vfio_platform_private.h +1 -0
index 3448f918..c56f4b6 100644
@@ -28,6 +28,7 @@ struct vfio_platform_region {
 	u64			addr;
 	resource_size_t		size;
 	u32			flags;
+	void __iomem		*ioaddr;
 };
 
 struct vfio_platform_device {
Signature · 1 line
1.8.3.2
#39 lore raw

RFC PATCH v6 12/20 vfio/platform: support MMAP of MMIO regions

In reply to Antonios Motakis

Allow to memory map the MMIO regions of the device so userspace can directly access them.

---
 drivers/vfio/platform/vfio_platform.c | 40 ++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
drivers/vfio/platform/vfio_platform.c +39 -1
index c66afbc..1515175 100644
@@ -57,6 +57,11 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
 		vdev->region[i].size = resource_size(res);
 		vdev->region[i].flags = VFIO_REGION_INFO_FLAG_READ
 					| VFIO_REGION_INFO_FLAG_WRITE;
+		/* Only regions addressed with PAGE granularity may be MMAPed
+		 * securely. */
+		if (!(vdev->region[i].addr & ~PAGE_MASK)
+				&& !(vdev->region[i].size & ~PAGE_MASK))
+			vdev->region[i].flags |= VFIO_REGION_INFO_FLAG_MMAP;
 	}
 
 	vdev->num_regions = cnt;
@@ -276,7 +281,40 @@ err:
 
 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
 {
-	return -EINVAL;
+	struct vfio_platform_device *vdev = device_data;
+	unsigned int index;
+	u64 req_len, pgoff, req_start;
+	struct vfio_platform_region region;
+
+	index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
+
+	if (vma->vm_end < vma->vm_start)
+		return -EINVAL;
+	if ((vma->vm_flags & VM_SHARED) == 0)
+		return -EINVAL;
+	if (index >= vdev->num_regions)
+		return -EINVAL;
+	if (vma->vm_start & ~PAGE_MASK)
+		return -EINVAL;
+	if (vma->vm_end & ~PAGE_MASK)
+		return -EINVAL;
+
+	region = vdev->region[index];
+
+	req_len = vma->vm_end - vma->vm_start;
+	pgoff = vma->vm_pgoff &
+		((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
+	req_start = pgoff << PAGE_SHIFT;
+
+	if (region.size < PAGE_SIZE || req_start + req_len > region.size)
+		return -EINVAL;
+
+	vma->vm_private_data = vdev;
+	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+	vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
+
+	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+			       req_len, vma->vm_page_prot);
 }
 
 static const struct vfio_device_ops vfio_platform_ops = {
Signature · 1 line
1.8.3.2
#40 lore raw

RFC PATCH v6 13/20 vfio/platform: return IRQ info

In reply to Antonios Motakis

Return information for the interrupts exposed by the device. This patch extends VFIO_DEVICE_GET_INFO with the number of IRQs and enables VFIO_DEVICE_GET_IRQ_INFO

---
 drivers/vfio/platform/Makefile                |  2 +-
 drivers/vfio/platform/vfio_platform.c         | 34 +++++++++++++--
 drivers/vfio/platform/vfio_platform_irq.c     | 59 +++++++++++++++++++++++++++
 drivers/vfio/platform/vfio_platform_private.h | 11 +++++
 4 files changed, 102 insertions(+), 4 deletions(-)
 create mode 100644 drivers/vfio/platform/vfio_platform_irq.c
drivers/vfio/platform/Makefile +1 -1
index df3a014..2c53327 100644
@@ -1,4 +1,4 @@
 
-vfio-platform-y := vfio_platform.o
+vfio-platform-y := vfio_platform.o vfio_platform_irq.o
 
 obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.o
drivers/vfio/platform/vfio_platform.c +31 -3
index 1515175..192291c 100644
@@ -83,6 +83,7 @@ static void vfio_platform_release(void *device_data)
 	struct vfio_platform_device *vdev = device_data;
 
 	vfio_platform_regions_cleanup(vdev);
+	vfio_platform_irq_cleanup(vdev);
 
 	module_put(THIS_MODULE);
 }
@@ -97,7 +98,18 @@ static int vfio_platform_open(void *device_data)
 
 	ret = vfio_platform_regions_init(vdev);
 	if (ret)
-		module_put(THIS_MODULE);
+		goto err_reg;
+
+	ret = vfio_platform_irq_init(vdev);
+	if (ret)
+		goto err_irq;
+
+	return 0;
+
+err_irq:
+	vfio_platform_regions_cleanup(vdev);
+err_reg:
+	module_put(THIS_MODULE);
 
 	return ret;
 }
@@ -121,7 +133,7 @@ static long vfio_platform_ioctl(void *device_data,
 
 		info.flags = VFIO_DEVICE_FLAGS_PLATFORM;
 		info.num_regions = vdev->num_regions;
-		info.num_irqs = 0;
+		info.num_irqs = vdev->num_irqs;
 
 		return copy_to_user((void __user *)arg, &info, minsz);
 
@@ -147,7 +159,23 @@ static long vfio_platform_ioctl(void *device_data,
 		return copy_to_user((void __user *)arg, &info, minsz);
 
 	} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
-		return -EINVAL;
+		struct vfio_irq_info info;
+
+		minsz = offsetofend(struct vfio_irq_info, count);
+
+		if (copy_from_user(&info, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (info.argsz < minsz)
+			return -EINVAL;
+
+		if (info.index >= vdev->num_irqs)
+			return -EINVAL;
+
+		info.flags = vdev->irq[info.index].flags;
+		info.count = vdev->irq[info.index].count;
+
+		return copy_to_user((void __user *)arg, &info, minsz);
 
 	} else if (cmd == VFIO_DEVICE_SET_IRQS)
 		return -EINVAL;
drivers/vfio/platform/vfio_platform_irq.c new file +59 -0

59 lines not on this page · raw message

drivers/vfio/platform/vfio_platform_private.h +11 -0
index c56f4b6..632a294 100644
@@ -24,6 +24,11 @@
 #define VFIO_PLATFORM_INDEX_TO_OFFSET(index)	\
 	((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)
 
+struct vfio_platform_irq {
+	u32			flags;
+	u32			count;
+};
+
 struct vfio_platform_region {
 	u64			addr;
 	resource_size_t		size;
@@ -35,6 +40,12 @@ struct vfio_platform_device {
 	struct platform_device		*pdev;
 	struct vfio_platform_region	*region;
 	u32				num_regions;
+	struct vfio_platform_irq	*irq;
+	u32				num_irqs;
 };
 
+extern int vfio_platform_irq_init(struct vfio_platform_device *vdev);
+
+extern void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev);
+
 #endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 1 line
1.8.3.2
#41 lore raw

RFC PATCH v6 14/20 vfio/platform: initial interrupts support

In reply to Antonios Motakis

This patch allows to set an eventfd for a patform device's interrupt, and also to trigger the interrupt eventfd from userspace for testing.

---
 drivers/vfio/platform/vfio_platform.c         |  36 ++++++-
 drivers/vfio/platform/vfio_platform_irq.c     | 130 +++++++++++++++++++++++++-
 drivers/vfio/platform/vfio_platform_private.h |   7 ++
 3 files changed, 169 insertions(+), 4 deletions(-)
drivers/vfio/platform/vfio_platform.c +33 -3
index 192291c..f4c06c6 100644
@@ -177,10 +177,40 @@ static long vfio_platform_ioctl(void *device_data,
 
 		return copy_to_user((void __user *)arg, &info, minsz);
 
-	} else if (cmd == VFIO_DEVICE_SET_IRQS)
-		return -EINVAL;
+	} else if (cmd == VFIO_DEVICE_SET_IRQS) {
+		struct vfio_irq_set hdr;
+		int ret = 0;
+
+		minsz = offsetofend(struct vfio_irq_set, count);
+
+		if (copy_from_user(&hdr, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (hdr.argsz < minsz)
+			return -EINVAL;
+
+		if (hdr.index >= vdev->num_irqs)
+			return -EINVAL;
+
+		if (hdr.start != 0 || hdr.count > 1)
+			return -EINVAL;
+
+		if (hdr.count == 0 &&
+			(!(hdr.flags & VFIO_IRQ_SET_DATA_NONE) ||
+			 !(hdr.flags & VFIO_IRQ_SET_ACTION_TRIGGER)))
+			return -EINVAL;
+
+		if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
+				  VFIO_IRQ_SET_ACTION_TYPE_MASK))
+			return -EINVAL;
+
+		ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
+						   hdr.start, hdr.count,
+						   (void *)arg+minsz);
+
+		return ret;
 
-	else if (cmd == VFIO_DEVICE_RESET)
+	} else if (cmd == VFIO_DEVICE_RESET)
 		return -EINVAL;
 
 	return -ENOTTY;
drivers/vfio/platform/vfio_platform_irq.c +129 -1

152 lines not on this page · raw message

drivers/vfio/platform/vfio_platform_private.h +7 -0
index 632a294..d6200df 100644
@@ -25,8 +25,11 @@
 	((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)
 
 struct vfio_platform_irq {
+	struct eventfd_ctx	*trigger;
 	u32			flags;
 	u32			count;
+	int			hwirq;
+	char			*name;
 };
 
 struct vfio_platform_region {
@@ -48,4 +51,8 @@ extern int vfio_platform_irq_init(struct vfio_platform_device *vdev);
 
 extern void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev);
 
+extern int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
+			uint32_t flags, unsigned index, unsigned start,
+			unsigned count, void *data);
+
 #endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 1 line
1.8.3.2
#44 lore raw

RFC PATCH v6 15/20 vfio/platform: support for maskable and automasked interrupts

In reply to Antonios Motakis

Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.

---
 drivers/vfio/platform/vfio_platform_irq.c     | 112 ++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h |   2 +
 2 files changed, 109 insertions(+), 5 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +107 -5
index d79f5af..10dfbf0 100644
@@ -51,9 +51,17 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
 		if (hwirq < 0)
 			goto err;
 
-		vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
+		spin_lock_init(&vdev->irq[i].lock);
+
+		vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD
+					| VFIO_IRQ_INFO_MASKABLE;
+
+		if (irq_get_trigger_type(hwirq) & IRQ_TYPE_LEVEL_MASK)
+			vdev->irq[i].flags |= VFIO_IRQ_INFO_AUTOMASKED;
+
 		vdev->irq[i].count = 1;
 		vdev->irq[i].hwirq = hwirq;
+		vdev->irq[i].masked = false;
 	}
 
 	vdev->num_irqs = cnt;
@@ -77,11 +85,27 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 
 static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
 {
-	struct eventfd_ctx *trigger = dev_id;
+	struct vfio_platform_irq *irq_ctx = dev_id;
+	unsigned long flags;
+	int ret = IRQ_NONE;
+
+	spin_lock_irqsave(&irq_ctx->lock, flags);
+
+	if (!irq_ctx->masked) {
+		ret = IRQ_HANDLED;
+
+		if (irq_ctx->flags & VFIO_IRQ_INFO_AUTOMASKED) {
+			disable_irq_nosync(irq_ctx->hwirq);
+			irq_ctx->masked = true;
+		}
+	}
 
-	eventfd_signal(trigger, 1);
+	spin_unlock_irqrestore(&irq_ctx->lock, flags);
 
-	return IRQ_HANDLED;
+	if (ret == IRQ_HANDLED)
+		eventfd_signal(irq_ctx->trigger, 1);
+
+	return ret;
 }
 
 static int vfio_set_trigger(struct vfio_platform_device *vdev,
@@ -162,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,
 	return -EFAULT;
 }
 
+static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
+				    unsigned index, unsigned start,
+				    unsigned count, uint32_t flags, void *data)
+{
+	uint8_t arr;
+
+	if (start != 0 || count != 1)
+		return -EINVAL;
+
+	switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+	case VFIO_IRQ_SET_DATA_BOOL:
+		if (copy_from_user(&arr, data, sizeof(uint8_t)))
+			return -EFAULT;
+
+		if (arr != 0x1)
+			return -EINVAL;
+
+	case VFIO_IRQ_SET_DATA_NONE:
+
+		spin_lock_irq(&vdev->irq[index].lock);
+
+		if (vdev->irq[index].masked) {
+			enable_irq(vdev->irq[index].hwirq);
+			vdev->irq[index].masked = false;
+		}
+
+		spin_unlock_irq(&vdev->irq[index].lock);
+
+		return 0;
+
+	case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
+	default:
+		return -ENOTTY;
+	}
+
+	return 0;
+}
+
+static int vfio_platform_set_irq_mask(struct vfio_platform_device *vdev,
+				    unsigned index, unsigned start,
+				    unsigned count, uint32_t flags, void *data)
+{
+	uint8_t arr;
+
+	if (start != 0 || count != 1)
+		return -EINVAL;
+
+	switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+	case VFIO_IRQ_SET_DATA_BOOL:
+		if (copy_from_user(&arr, data, sizeof(uint8_t)))
+			return -EFAULT;
+
+		if (arr != 0x1)
+			return -EINVAL;
+
+	case VFIO_IRQ_SET_DATA_NONE:
+
+		spin_lock_irq(&vdev->irq[index].lock);
+
+		if (!vdev->irq[index].masked) {
+			disable_irq(vdev->irq[index].hwirq);
+			vdev->irq[index].masked = true;
+		}
+
+		spin_unlock_irq(&vdev->irq[index].lock);
+
+		return 0;
+
+	case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
+	default:
+		return -ENOTTY;
+	}
+
+	return 0;
+}
+
 int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 				 uint32_t flags, unsigned index, unsigned start,
 				 unsigned count, void *data)
@@ -172,8 +272,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 
 	switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
 	case VFIO_IRQ_SET_ACTION_MASK:
+		func = vfio_platform_set_irq_mask;
+		break;
 	case VFIO_IRQ_SET_ACTION_UNMASK:
-		/* XXX not implemented */
+		func = vfio_platform_set_irq_unmask;
 		break;
 	case VFIO_IRQ_SET_ACTION_TRIGGER:
 		func = vfio_platform_set_irq_trigger;
drivers/vfio/platform/vfio_platform_private.h +2 -0
index d6200df..4d887fd 100644
@@ -30,6 +30,8 @@ struct vfio_platform_irq {
 	u32			count;
 	int			hwirq;
 	char			*name;
+	bool			masked;
+	spinlock_t		lock;
 };
 
 struct vfio_platform_region {
Signature · 1 line
1.8.3.2
#49 lore raw

RFC PATCH v6 16/20 vfio: move eventfd support code for VFIO_PCI to a sepparate file

In reply to Antonios Motakis

The virqfd functionality that is used by VFIO_PCI to implement interrupt masking and unmasking via an eventfd, is generic enough and can be reused by another driver. Move it to a separate file in order to allow the code to be shared.

---
 drivers/vfio/Makefile               |   2 +-
 drivers/vfio/pci/vfio_pci_intrs.c   | 213 ------------------------------------
 drivers/vfio/pci/vfio_pci_private.h |   3 -
 drivers/vfio/virqfd.c               | 212 +++++++++++++++++++++++++++++++++++
 include/linux/vfio.h                |  27 +++++
 5 files changed, 240 insertions(+), 217 deletions(-)
 create mode 100644 drivers/vfio/virqfd.c
drivers/vfio/Makefile +1 -1
index b5e4a33..0d8f48b 100644
@@ -1,4 +1,4 @@
-obj-$(CONFIG_VFIO) += vfio.o
+obj-$(CONFIG_VFIO) += vfio.o virqfd.o
 obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
 obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
 obj-$(CONFIG_VFIO_PCI) += pci/
drivers/vfio/pci/vfio_pci_intrs.c +0 -213

226 lines not on this page · raw message

drivers/vfio/pci/vfio_pci_private.h +0 -3
index 9c6d5d0..72b86c7 100644
@@ -85,9 +85,6 @@ extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
 extern int vfio_pci_init_perm_bits(void);
 extern void vfio_pci_uninit_perm_bits(void);
 
-extern int vfio_pci_virqfd_init(void);
-extern void vfio_pci_virqfd_exit(void);
-
 extern int vfio_config_init(struct vfio_pci_device *vdev);
 extern void vfio_config_free(struct vfio_pci_device *vdev);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/virqfd.c new file +212 -0

212 lines not on this page · raw message

include/linux/vfio.h +27 -0
index 81022a52..43968e8 100644
@@ -14,6 +14,8 @@
 
 #include <linux/iommu.h>
 #include <linux/mm.h>
+#include <linux/workqueue.h>
+#include <linux/poll.h>
 #include <uapi/linux/vfio.h>
 
 /**
@@ -99,4 +101,29 @@ extern int vfio_external_user_iommu_id(struct vfio_group *group);
 extern long vfio_external_check_extension(struct vfio_group *group,
 					  unsigned long arg);
 
+/*
+ * IRQFD support
+ */
+struct virqfd {
+	struct vfio_pci_device	*vdev;
+	struct eventfd_ctx	*eventfd;
+	int			(*handler)(struct vfio_pci_device *, void *);
+	void			(*thread)(struct vfio_pci_device *, void *);
+	void			*data;
+	struct work_struct	inject;
+	wait_queue_t		wait;
+	poll_table		pt;
+	struct work_struct	shutdown;
+	struct virqfd		**pvirqfd;
+};
+
+extern int vfio_pci_virqfd_init(void);
+extern void vfio_pci_virqfd_exit(void);
+extern int virqfd_enable(struct vfio_pci_device *vdev,
+			 int (*handler)(struct vfio_pci_device *, void *),
+			 void (*thread)(struct vfio_pci_device *, void *),
+			 void *data, struct virqfd **pvirqfd, int fd);
+extern void virqfd_disable(struct vfio_pci_device *vdev,
+			   struct virqfd **pvirqfd);
+
 #endif /* VFIO_H */
Signature · 1 line
1.8.3.2
#50 lore raw

RFC PATCH v6 17/20 vfio: add local lock in virqfd instead of depending on VFIO PCI

In reply to Antonios Motakis

Sharing the same spinlock with the VFIO bus driver is not necessary for the virqfd code, so remove that dependency.

---
 drivers/vfio/pci/vfio_pci_intrs.c | 10 +++++-----
 drivers/vfio/virqfd.c             | 24 +++++++++++++-----------
 include/linux/vfio.h              |  3 +--
 3 files changed, 19 insertions(+), 18 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +5 -5
index 3f909bb..e56c814 100644
@@ -226,8 +226,8 @@ static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
 static void vfio_intx_disable(struct vfio_pci_device *vdev)
 {
 	vfio_intx_set_signal(vdev, -1);
-	virqfd_disable(vdev, &vdev->ctx[0].unmask);
-	virqfd_disable(vdev, &vdev->ctx[0].mask);
+	virqfd_disable(&vdev->ctx[0].unmask);
+	virqfd_disable(&vdev->ctx[0].mask);
 	vdev->irq_type = VFIO_PCI_NUM_IRQS;
 	vdev->num_ctx = 0;
 	kfree(vdev->ctx);
@@ -377,8 +377,8 @@ static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
 	vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
 
 	for (i = 0; i < vdev->num_ctx; i++) {
-		virqfd_disable(vdev, &vdev->ctx[i].unmask);
-		virqfd_disable(vdev, &vdev->ctx[i].mask);
+		virqfd_disable(&vdev->ctx[i].unmask);
+		virqfd_disable(&vdev->ctx[i].mask);
 	}
 
 	if (msix) {
@@ -415,7 +415,7 @@ static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
 					     vfio_send_intx_eventfd, NULL,
 					     &vdev->ctx[0].unmask, fd);
 
-		virqfd_disable(vdev, &vdev->ctx[0].unmask);
+		virqfd_disable(&vdev->ctx[0].unmask);
 	}
 
 	return 0;
drivers/vfio/virqfd.c +13 -11
index 9cf2842..4528450 100644
@@ -17,6 +17,7 @@
 #include "pci/vfio_pci_private.h"
 
 static struct workqueue_struct *vfio_irqfd_cleanup_wq;
+static spinlock_t lock;
 
 int __init vfio_pci_virqfd_init(void)
 {
@@ -25,6 +26,8 @@ int __init vfio_pci_virqfd_init(void)
 	if (!vfio_irqfd_cleanup_wq)
 		return -ENOMEM;
 
+	spin_lock_init(&lock);
+
 	return 0;
 }
 
@@ -53,21 +56,21 @@ static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
 
 	if (flags & POLLHUP) {
 		unsigned long flags;
-		spin_lock_irqsave(&virqfd->vdev->irqlock, flags);
+		spin_lock_irqsave(&lock, flags);
 
 		/*
 		 * The eventfd is closing, if the virqfd has not yet been
 		 * queued for release, as determined by testing whether the
-		 * vdev pointer to it is still valid, queue it now.  As
+		 * virqfd pointer to it is still valid, queue it now.  As
 		 * with kvm irqfds, we know we won't race against the virqfd
-		 * going away because we hold wqh->lock to get here.
+		 * going away because we hold the lock to get here.
 		 */
 		if (*(virqfd->pvirqfd) == virqfd) {
 			*(virqfd->pvirqfd) = NULL;
 			virqfd_deactivate(virqfd);
 		}
 
-		spin_unlock_irqrestore(&virqfd->vdev->irqlock, flags);
+		spin_unlock_irqrestore(&lock, flags);
 	}
 
 	return 0;
@@ -143,16 +146,16 @@ int virqfd_enable(struct vfio_pci_device *vdev,
 	 * we update the pointer to the virqfd under lock to avoid
 	 * pushing multiple jobs to release the same virqfd.
 	 */
-	spin_lock_irq(&vdev->irqlock);
+	spin_lock_irq(&lock);
 
 	if (*pvirqfd) {
-		spin_unlock_irq(&vdev->irqlock);
+		spin_unlock_irq(&lock);
 		ret = -EBUSY;
 		goto err_busy;
 	}
 	*pvirqfd = virqfd;
 
-	spin_unlock_irq(&vdev->irqlock);
+	spin_unlock_irq(&lock);
 
 	/*
 	 * Install our own custom wake-up handling so we are notified via
@@ -189,19 +192,18 @@ err_fd:
 	return ret;
 }
 
-void virqfd_disable(struct vfio_pci_device *vdev,
-			   struct virqfd **pvirqfd)
+void virqfd_disable(struct virqfd **pvirqfd)
 {
 	unsigned long flags;
 
-	spin_lock_irqsave(&vdev->irqlock, flags);
+	spin_lock_irqsave(&lock, flags);
 
 	if (*pvirqfd) {
 		virqfd_deactivate(*pvirqfd);
 		*pvirqfd = NULL;
 	}
 
-	spin_unlock_irqrestore(&vdev->irqlock, flags);
+	spin_unlock_irqrestore(&lock, flags);
 
 	/*
 	 * Block until we know all outstanding shutdown jobs have completed.
include/linux/vfio.h +1 -2
index 43968e8..44c9808 100644
@@ -123,7 +123,6 @@ extern int virqfd_enable(struct vfio_pci_device *vdev,
 			 int (*handler)(struct vfio_pci_device *, void *),
 			 void (*thread)(struct vfio_pci_device *, void *),
 			 void *data, struct virqfd **pvirqfd, int fd);
-extern void virqfd_disable(struct vfio_pci_device *vdev,
-			   struct virqfd **pvirqfd);
+extern void virqfd_disable(struct virqfd **pvirqfd);
 
 #endif /* VFIO_H */
Signature · 1 line
1.8.3.2
#53 lore raw

RFC PATCH v6 18/20 vfio: pass an opaque pointer on virqfd initialization

In reply to Antonios Motakis

VFIO_PCI passes the VFIO device structure *vdev via eventfd to the handler that implements masking/unmasking of IRQs via an eventfd. We can replace it in the virqfd infrastructure with an opaque type so we can make use of the mechanism from other VFIO bus drivers.

---
 drivers/vfio/pci/vfio_pci_intrs.c | 11 +++++++----
 drivers/vfio/virqfd.c             | 17 ++++++++---------
 include/linux/vfio.h              | 12 ++++++------
 3 files changed, 21 insertions(+), 19 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +7 -4
index e56c814..6ca22a8 100644
@@ -27,8 +27,10 @@
 /*
  * INTx
  */
-static void vfio_send_intx_eventfd(struct vfio_pci_device *vdev, void *unused)
+static void vfio_send_intx_eventfd(void *opaque, void *unused)
 {
+	struct vfio_pci_device *vdev = opaque;
+
 	if (likely(is_intx(vdev) && !vdev->virq_disabled))
 		eventfd_signal(vdev->ctx[0].trigger, 1);
 }
@@ -71,9 +73,9 @@ void vfio_pci_intx_mask(struct vfio_pci_device *vdev)
  * a signal is necessary, which can then be handled via a work queue
  * or directly depending on the caller.
  */
-static int vfio_pci_intx_unmask_handler(struct vfio_pci_device *vdev,
-					void *unused)
+static int vfio_pci_intx_unmask_handler(void *opaque, void *unused)
 {
+	struct vfio_pci_device *vdev = opaque;
 	struct pci_dev *pdev = vdev->pdev;
 	unsigned long flags;
 	int ret = 0;
@@ -411,7 +413,8 @@ static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
 	} else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
 		int32_t fd = *(int32_t *)data;
 		if (fd >= 0)
-			return virqfd_enable(vdev, vfio_pci_intx_unmask_handler,
+			return virqfd_enable((void *) vdev,
+					     vfio_pci_intx_unmask_handler,
 					     vfio_send_intx_eventfd, NULL,
 					     &vdev->ctx[0].unmask, fd);
 
drivers/vfio/virqfd.c +8 -9
index 4528450..8c97e2e 100644
@@ -14,7 +14,6 @@
 #include <linux/eventfd.h>
 #include <linux/file.h>
 #include <linux/slab.h>
-#include "pci/vfio_pci_private.h"
 
 static struct workqueue_struct *vfio_irqfd_cleanup_wq;
 static spinlock_t lock;
@@ -49,7 +48,7 @@ static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
 	if (flags & POLLIN) {
 		/* An event has been signaled, call function */
 		if ((!virqfd->handler ||
-		     virqfd->handler(virqfd->vdev, virqfd->data)) &&
+		     virqfd->handler(virqfd->opaque, virqfd->data)) &&
 		    virqfd->thread)
 			schedule_work(&virqfd->inject);
 	}
@@ -99,13 +98,13 @@ static void virqfd_inject(struct work_struct *work)
 {
 	struct virqfd *virqfd = container_of(work, struct virqfd, inject);
 	if (virqfd->thread)
-		virqfd->thread(virqfd->vdev, virqfd->data);
+		virqfd->thread(virqfd->opaque, virqfd->data);
 }
 
-int virqfd_enable(struct vfio_pci_device *vdev,
-			 int (*handler)(struct vfio_pci_device *, void *),
-			 void (*thread)(struct vfio_pci_device *, void *),
-			 void *data, struct virqfd **pvirqfd, int fd)
+int virqfd_enable(void *opaque,
+		  int (*handler)(void *, void *),
+		  void (*thread)(void *, void *),
+		  void *data, struct virqfd **pvirqfd, int fd)
 {
 	struct fd irqfd;
 	struct eventfd_ctx *ctx;
@@ -118,7 +117,7 @@ int virqfd_enable(struct vfio_pci_device *vdev,
 		return -ENOMEM;
 
 	virqfd->pvirqfd = pvirqfd;
-	virqfd->vdev = vdev;
+	virqfd->opaque = opaque;
 	virqfd->handler = handler;
 	virqfd->thread = thread;
 	virqfd->data = data;
@@ -171,7 +170,7 @@ int virqfd_enable(struct vfio_pci_device *vdev,
 	 * before we registered and trigger it as if we didn't miss it.
 	 */
 	if (events & POLLIN) {
-		if ((!handler || handler(vdev, data)) && thread)
+		if ((!handler || handler(opaque, data)) && thread)
 			schedule_work(&virqfd->inject);
 	}
 
include/linux/vfio.h +6 -6
index 44c9808..e4f7de8 100644
@@ -105,10 +105,10 @@ extern long vfio_external_check_extension(struct vfio_group *group,
  * IRQFD support
  */
 struct virqfd {
-	struct vfio_pci_device	*vdev;
+	void			*opaque;
 	struct eventfd_ctx	*eventfd;
-	int			(*handler)(struct vfio_pci_device *, void *);
-	void			(*thread)(struct vfio_pci_device *, void *);
+	int			(*handler)(void *, void *);
+	void			(*thread)(void *, void *);
 	void			*data;
 	struct work_struct	inject;
 	wait_queue_t		wait;
@@ -119,9 +119,9 @@ struct virqfd {
 
 extern int vfio_pci_virqfd_init(void);
 extern void vfio_pci_virqfd_exit(void);
-extern int virqfd_enable(struct vfio_pci_device *vdev,
-			 int (*handler)(struct vfio_pci_device *, void *),
-			 void (*thread)(struct vfio_pci_device *, void *),
+extern int virqfd_enable(void *opaque,
+			 int (*handler)(void *, void *),
+			 void (*thread)(void *, void *),
 			 void *data, struct virqfd **pvirqfd, int fd);
 extern void virqfd_disable(struct virqfd **pvirqfd);
 
Signature · 1 line
1.8.3.2
#54 lore raw

RFC PATCH v6 19/20 vfio: initialize the virqfd workqueue in VFIO generic code

In reply to Antonios Motakis

Now we have finally completely decoupled virqfd from VFIO_PCI. We can initialize it from the VFIO generic code, in order to safely use it from many different VFIO bus drivers.

---
 drivers/vfio/pci/vfio_pci.c | 8 --------
 drivers/vfio/vfio.c         | 8 ++++++++
 drivers/vfio/virqfd.c       | 4 ++--
 include/linux/vfio.h        | 4 ++--
 4 files changed, 12 insertions(+), 12 deletions(-)
drivers/vfio/pci/vfio_pci.c +0 -8
index 7ba0424..9969eaf 100644
@@ -899,7 +899,6 @@ static struct pci_driver vfio_pci_driver = {
 static void __exit vfio_pci_cleanup(void)
 {
 	pci_unregister_driver(&vfio_pci_driver);
-	vfio_pci_virqfd_exit();
 	vfio_pci_uninit_perm_bits();
 }
 
@@ -912,11 +911,6 @@ static int __init vfio_pci_init(void)
 	if (ret)
 		return ret;
 
-	/* Start the virqfd cleanup handler */
-	ret = vfio_pci_virqfd_init();
-	if (ret)
-		goto out_virqfd;
-
 	/* Register and scan for devices */
 	ret = pci_register_driver(&vfio_pci_driver);
 	if (ret)
@@ -925,8 +919,6 @@ static int __init vfio_pci_init(void)
 	return 0;
 
 out_driver:
-	vfio_pci_virqfd_exit();
-out_virqfd:
 	vfio_pci_uninit_perm_bits();
 	return ret;
 }
drivers/vfio/vfio.c +8 -0
index 512f479..4f95a17 100644
@@ -1470,6 +1470,11 @@ static int __init vfio_init(void)
 	if (ret)
 		goto err_cdev_add;
 
+	/* Start the virqfd cleanup handler used by some VFIO bus drivers */
+	ret = vfio_virqfd_init();
+	if (ret)
+		goto err_virqfd;
+
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
 
 	/*
@@ -1482,6 +1487,8 @@ static int __init vfio_init(void)
 
 	return 0;
 
+err_virqfd:
+	cdev_del(&vfio.group_cdev);
 err_cdev_add:
 	unregister_chrdev_region(vfio.group_devt, MINORMASK);
 err_alloc_chrdev:
@@ -1496,6 +1503,7 @@ static void __exit vfio_cleanup(void)
 {
 	WARN_ON(!list_empty(&vfio.group_list));
 
+	vfio_virqfd_exit();
 	idr_destroy(&vfio.group_idr);
 	cdev_del(&vfio.group_cdev);
 	unregister_chrdev_region(vfio.group_devt, MINORMASK);
drivers/vfio/virqfd.c +2 -2
index 8c97e2e..8d286ca 100644
@@ -18,7 +18,7 @@
 static struct workqueue_struct *vfio_irqfd_cleanup_wq;
 static spinlock_t lock;
 
-int __init vfio_pci_virqfd_init(void)
+int __init vfio_virqfd_init(void)
 {
 	vfio_irqfd_cleanup_wq =
 		create_singlethread_workqueue("vfio-irqfd-cleanup");
@@ -30,7 +30,7 @@ int __init vfio_pci_virqfd_init(void)
 	return 0;
 }
 
-void vfio_pci_virqfd_exit(void)
+void vfio_virqfd_exit(void)
 {
 	destroy_workqueue(vfio_irqfd_cleanup_wq);
 }
include/linux/vfio.h +2 -2
index e4f7de8..132e61f 100644
@@ -117,8 +117,8 @@ struct virqfd {
 	struct virqfd		**pvirqfd;
 };
 
-extern int vfio_pci_virqfd_init(void);
-extern void vfio_pci_virqfd_exit(void);
+extern int vfio_virqfd_init(void);
+extern void vfio_virqfd_exit(void);
 extern int virqfd_enable(void *opaque,
 			 int (*handler)(void *, void *),
 			 void (*thread)(void *, void *),
Signature · 1 line
1.8.3.2
#55 lore raw

RFC PATCH v6 20/20 vfio/platform: implement IRQ masking/unmasking via an eventfd

In reply to Antonios Motakis

With this patch the VFIO user will be able to set an eventfd that can be used in order to mask and unmask IRQs of platform devices.

---
 drivers/vfio/platform/vfio_platform_irq.c     | 86 ++++++++++++++++++++++-----
 drivers/vfio/platform/vfio_platform_private.h |  2 +
 2 files changed, 72 insertions(+), 16 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +70 -16
index 10dfbf0..6768508 100644
@@ -108,6 +108,52 @@ static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
 	return ret;
 }
 
+static void vfio_platform_unmask(struct vfio_platform_irq *irq_ctx)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&irq_ctx->lock, flags);
+
+	if (irq_ctx->masked) {
+		enable_irq(irq_ctx->hwirq);
+		irq_ctx->masked = false;
+	}
+
+	spin_unlock_irqrestore(&irq_ctx->lock, flags);
+}
+
+static void vfio_platform_mask(struct vfio_platform_irq *irq_ctx)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&irq_ctx->lock, flags);
+
+	if (!irq_ctx->masked) {
+		disable_irq(irq_ctx->hwirq);
+		irq_ctx->masked = true;
+	}
+
+	spin_unlock_irqrestore(&irq_ctx->lock, flags);
+}
+
+static int vfio_platform_unmask_handler(void *opaque, void *unused)
+{
+	struct vfio_platform_irq *irq_ctx = opaque;
+
+	vfio_platform_unmask(irq_ctx);
+
+	return 0;
+}
+
+static int vfio_platform_mask_handler(void *opaque, void *unused)
+{
+	struct vfio_platform_irq *irq_ctx = opaque;
+
+	vfio_platform_mask(irq_ctx);
+
+	return 0;
+}
+
 static int vfio_set_trigger(struct vfio_platform_device *vdev,
 			    int index, int fd)
 {
@@ -191,6 +237,7 @@ static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
 				    unsigned count, uint32_t flags, void *data)
 {
 	uint8_t arr;
+	int32_t fd;
 
 	if (start != 0 || count != 1)
 		return -EINVAL;
@@ -204,19 +251,22 @@ static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
 			return -EINVAL;
 
 	case VFIO_IRQ_SET_DATA_NONE:
+		vfio_platform_unmask(&vdev->irq[index]);
+		return 0;
 
-		spin_lock_irq(&vdev->irq[index].lock);
-
-		if (vdev->irq[index].masked) {
-			enable_irq(vdev->irq[index].hwirq);
-			vdev->irq[index].masked = false;
-		}
+	case VFIO_IRQ_SET_DATA_EVENTFD:
+		if (copy_from_user(&fd, data, sizeof(int32_t)))
+			return -EFAULT;
 
-		spin_unlock_irq(&vdev->irq[index].lock);
+		if (fd >= 0)
+			return virqfd_enable((void *) &vdev->irq[index],
+					     vfio_platform_unmask_handler,
+					     NULL, NULL,
+					     &vdev->irq[index].unmask, fd);
 
+		virqfd_disable(&vdev->irq[index].unmask);
 		return 0;
 
-	case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
 	default:
 		return -ENOTTY;
 	}
@@ -229,6 +279,7 @@ static int vfio_platform_set_irq_mask(struct vfio_platform_device *vdev,
 				    unsigned count, uint32_t flags, void *data)
 {
 	uint8_t arr;
+	int32_t fd;
 
 	if (start != 0 || count != 1)
 		return -EINVAL;
@@ -242,19 +293,22 @@ static int vfio_platform_set_irq_mask(struct vfio_platform_device *vdev,
 			return -EINVAL;
 
 	case VFIO_IRQ_SET_DATA_NONE:
+		vfio_platform_mask(&vdev->irq[index]);
+		return 0;
 
-		spin_lock_irq(&vdev->irq[index].lock);
-
-		if (!vdev->irq[index].masked) {
-			disable_irq(vdev->irq[index].hwirq);
-			vdev->irq[index].masked = true;
-		}
+	case VFIO_IRQ_SET_DATA_EVENTFD:
+		if (copy_from_user(&fd, data, sizeof(int32_t)))
+			return -EFAULT;
 
-		spin_unlock_irq(&vdev->irq[index].lock);
+		if (fd >= 0)
+			return virqfd_enable((void *) &vdev->irq[index],
+					     vfio_platform_mask_handler,
+					     NULL, NULL,
+					     &vdev->irq[index].mask, fd);
 
+		virqfd_disable(&vdev->irq[index].mask);
 		return 0;
 
-	case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
 	default:
 		return -ENOTTY;
 	}
drivers/vfio/platform/vfio_platform_private.h +2 -0
index 4d887fd..86a9201 100644
@@ -32,6 +32,8 @@ struct vfio_platform_irq {
 	char			*name;
 	bool			masked;
 	spinlock_t		lock;
+	struct virqfd		*unmask;
+	struct virqfd		*mask;
 };
 
 struct vfio_platform_region {
Signature · 1 line
1.8.3.2
#12 lore raw

RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

In reply to Antonios Motakis
Quoted 36 lines

-----Original Message-----
From: iommu-bounces-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org [mailto:iommu- bounces-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org] On Behalf Of Antonios Motakis
Sent: Thursday, June 05, 2014 10:33 PM
To: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
Cc: [email protected]; [email protected]; open list; [email protected]; a.rigo-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; Yoder Stuart-B08248; moderated list:ARM SMMU DRIVER; Antonios Motakis; tech-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; [email protected]
Subject: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability
IOMMU_CAP_INTR_REMAP

With an ARM SMMU, interrupt remapping should always be safe from the SMMU's point of view, as it is properly handled by the GIC.

---
 drivers/iommu/arm-smmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm-smmu.c index renamed from drivers/iommu/arm-smmu.c +0 -0

15ab2af..ff29402 100644

drivers/iommu/arm-smmu.c +1 -1
@@ -1544,7 +1544,7 @@ static int arm_smmu_domain_has_cap(struct
 iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features &
 ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
-	caps |= IOMMU_CAP_NOEXEC;
+	caps |= IOMMU_CAP_NOEXEC | IOMMU_CAP_INTR_REMAP;
 
Quoted 2 lines

return !!(cap & caps);
}

Why mention this as an IOMMU capability when IOMMU doesn't support it? Also, wouldn't this depend on the GIC IP version? What are we gaining by adding this IOMMU capability list?

-Varun

#4 lore raw

Re: [RFC PATCH v6 02/20] iommu: add capability IOMMU_CAP_NOEXEC

In reply to Antonios Motakis
Quoted 14 lines · Antonios Motakis

Some IOMMUs accept an IOMMU_NOEXEC protection flag in addition to IOMMU_READ and IOMMU_WRITE. Expose this as an IOMMU capability.

---
 include/linux/iommu.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/iommu.h +0 -0
index fc464d2..7e152fb 100644
@@ -57,8 +57,9 @@ struct iommu_domain {
 	struct iommu_domain_geometry geometry;

include/linux/iommu.h struct iommu_domain { · line 65

Quoted 10 lines
include/linux/iommu.h +3 -2
 };
 
-#define IOMMU_CAP_CACHE_COHERENCY	0x1
-#define IOMMU_CAP_INTR_REMAP		0x2	/* isolates device intrs */
+#define IOMMU_CAP_CACHE_COHERENCY	(1 << 0)
+#define IOMMU_CAP_INTR_REMAP		(1 << 1) /* isolates device intrs */
+#define IOMMU_CAP_NOEXEC		(1 << 2) /* IOMMU_NOEXEC flag */
 
 /*
  * Following constraints are specifc to FSL_PAMUV1:

Hmm, was this really intended to be a bitmap? Why? Thanks,

Alex

#32 lore raw

Re: [RFC PATCH v6 07/20] vfio/iommu_type1: implement the VFIO_DMA_MAP_FLAG_NOEXEC flag

In reply to Antonios Motakis
Quoted 64 lines · Antonios Motakis

Some IOMMU drivers, such as the ARM SMMU driver, make available the IOMMU_NOEXEC flag, to set the page tables for a device as XN (execute never). This affects devices such as the ARM PL330 DMA Controller, which respects this flag and will refuse to fetch DMA instructions from memory where the XN flag has been set.

---
 drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +27 -0
index 6673e7b..e2566fd 100644
@@ -80,6 +80,24 @@ struct vfio_group {
 	struct list_head	next;
 };
 
+static int vfio_domains_have_cap_noexec(struct vfio_iommu *iommu)
+{
+	struct vfio_domain *d;
+	int ret = 1;
+
+	mutex_lock(&iommu->lock);
+	list_for_each_entry(d, &iommu->domain_list, next) {
+		if (!iommu_domain_has_cap(d->domain, IOMMU_CAP_NOEXEC)) {
+			ret = 0;
+			break;
+		}
+	}
+	mutex_unlock(&iommu->lock);
+
+	return ret;
+}
+
+
 /*
  * This code handles mapping and unmapping of user data buffers
  * into DMA'ble space using the IOMMU
@@ -542,6 +560,11 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 		prot |= IOMMU_WRITE;
 	if (map->flags & VFIO_DMA_MAP_FLAG_READ)
 		prot |= IOMMU_READ;
+	if (map->flags & VFIO_DMA_MAP_FLAG_NOEXEC) {
+		if (!vfio_domains_have_cap_noexec(iommu))
+			return -EINVAL;
+		prot |= IOMMU_NOEXEC;
+	}
 
 	if (!prot)
 		return -EINVAL; /* No READ/WRITE? */
@@ -899,6 +922,10 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
 			if (!iommu)
 				return 0;
 			return vfio_domains_have_iommu_cache(iommu);
+		case VFIO_IOMMU_PROT_NOEXEC:
+			if (!iommu)
+				return 0;
+			return vfio_domains_have_cap_noexec(iommu);
 		default:
 			return 0;
 		}

drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 956

Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +2 -1
@@ -922,7 +949,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
 	} else if (cmd == VFIO_IOMMU_MAP_DMA) {
 		struct vfio_iommu_type1_dma_map map;
 		uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
-				VFIO_DMA_MAP_FLAG_WRITE;
+				VFIO_DMA_MAP_FLAG_WRITE |
+				VFIO_DMA_MAP_FLAG_NOEXEC;
 
 		minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 

This doesn't look complete. What happens if we support NOEXEC, then we add a group behind an IOMMU that doesn't support NOEXEC? In one case we attempt to use the same domain, which might imply the same IOMMU page tables, but is the hardware going to support that? In the other case we create a new domain, then try to replay the mappings, including the NOEXEC bit... what's that going to do? Can we add a non-NOEXEC domain to a container that was previously NOEXEC? The point of changing the flag from EXEC to NOEXEC was to make it enforceable, but doesn't that mean we need to prevent that happening if we have NOEXEC mappings?

Something needs to happen in vfio_iommu_type1_attach_group() to figure out what's allowed. Also, vfio_domains_have_cap_noexec() should be named vfio_domains_have_iommu_nexec() to match _iommu_cache() and possibly they should both be wrappers to a common function if they end up sharing similar implementations. Thanks,

Alex

#36 lore raw

Re: [RFC PATCH v6 10/20] vfio/platform: return info for device and its memory mapped IO regions

In reply to Antonios Motakis
Quoted 76 lines · Antonios Motakis

A VFIO userspace driver will start by opening the VFIO device that corresponds to an IOMMU group, and will use the ioctl interface to get the basic device info, such as number of memory regions and interrupts, and their properties.

This patch enables the IOCTLs:
- VFIO_DEVICE_GET_INFO
- VFIO_DEVICE_GET_REGION_INFO

IRQ info is provided by one of the latter patches.

---
 drivers/vfio/platform/vfio_platform.c         | 79 +++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h | 17 ++++++
 2 files changed, 90 insertions(+), 6 deletions(-)
drivers/vfio/platform/vfio_platform.c +44 -0
index 1df76d8..eeaebc4 100644
@@ -34,17 +34,66 @@
 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>"
 #define DRIVER_DESC     "VFIO for platform devices - User Level meta-driver"
 
+static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
+{
+	int cnt = 0, i;
+
+	while (platform_get_resource(vdev->pdev, IORESOURCE_MEM, cnt))
+		cnt++;
+
+	vdev->region = kzalloc(sizeof(struct vfio_platform_region) * cnt,
+				GFP_KERNEL);
+	if (!vdev->region)
+		return -ENOMEM;
+
+	for (i = 0; i < cnt;  i++) {
+		struct resource *res =
+			platform_get_resource(vdev->pdev, IORESOURCE_MEM, i);
+
+		if (!res)
+			goto err;
+
+		vdev->region[i].addr = res->start;
+		vdev->region[i].size = resource_size(res);
+		vdev->region[i].flags = 0;
+	}
+
+	vdev->num_regions = cnt;
+
+	return 0;
+err:
+	kfree(vdev->region);
+	return -EINVAL;
+}
+
+static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
+{
+	vdev->num_regions = 0;
+	kfree(vdev->region);
+}
+
 static void vfio_platform_release(void *device_data)
 {
+	struct vfio_platform_device *vdev = device_data;
+
+	vfio_platform_regions_cleanup(vdev);
+
 	module_put(THIS_MODULE);
 }
 
 static int vfio_platform_open(void *device_data)
 {
+	struct vfio_platform_device *vdev = device_data;
+	int ret;

drivers/vfio/platform/vfio_platform.c line 96

Quoted 10 lines
drivers/vfio/platform/vfio_platform.c +6 -1
+
 	if (!try_module_get(THIS_MODULE))
 		return -ENODEV;
 
-	return 0;
+	ret = vfio_platform_regions_init(vdev);
+	if (ret)
+		module_put(THIS_MODULE);
+
+	return ret;

The user can call VFIO_GROUP_GET_DEVICE_FD for a single device more than once, vfio_pci maintains a reference count to make sure we only allocate and initialize on the first open and cleanup on the last close.

Quoted 61 lines
drivers/vfio/platform/vfio_platform.c +23 -5
 }
 
 static long vfio_platform_ioctl(void *device_data,
@@ -65,18 +114,36 @@ static long vfio_platform_ioctl(void *device_data,
 			return -EINVAL;
 
 		info.flags = VFIO_DEVICE_FLAGS_PLATFORM;
-		info.num_regions = 0;
+		info.num_regions = vdev->num_regions;
 		info.num_irqs = 0;
 
 		return copy_to_user((void __user *)arg, &info, minsz);
 
-	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO)
-		return -EINVAL;
+	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
+		struct vfio_region_info info;
+
+		minsz = offsetofend(struct vfio_region_info, offset);
+
+		if (copy_from_user(&info, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (info.argsz < minsz)
+			return -EINVAL;
+
+		if (info.index >= vdev->num_regions)
+			return -EINVAL;
+
+		/* map offset to the physical address  */
+		info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
+		info.size = vdev->region[info.index].size;
+		info.flags = vdev->region[info.index].flags;
+
+		return copy_to_user((void __user *)arg, &info, minsz);
 
-	else if (cmd == VFIO_DEVICE_GET_IRQ_INFO)
+	} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
 		return -EINVAL;
 
-	else if (cmd == VFIO_DEVICE_SET_IRQS)
+	} else if (cmd == VFIO_DEVICE_SET_IRQS)
 		return -EINVAL;
 
 	else if (cmd == VFIO_DEVICE_RESET)
drivers/vfio/platform/vfio_platform_private.h +8 -0
index 4ae88f8..3448f918 100644
@@ -15,8 +15,25 @@
 #ifndef VFIO_PLATFORM_PRIVATE_H
 #define VFIO_PLATFORM_PRIVATE_H
 
+#define VFIO_PLATFORM_OFFSET_SHIFT   40
+#define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1)
+
+#define VFIO_PLATFORM_OFFSET_TO_INDEX(off)	\
+	(off >> VFIO_PLATFORM_OFFSET_SHIFT)
+
+#define VFIO_PLATFORM_INDEX_TO_OFFSET(index)	\
+	((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)

drivers/vfio/platform/vfio_platform_private.h line 35

Quoted 10 lines
drivers/vfio/platform/vfio_platform_private.h +8 -0
+
+struct vfio_platform_region {
+	u64			addr;
+	resource_size_t		size;
+	u32			flags;
+};
+
 struct vfio_platform_device {
 	struct platform_device		*pdev;
+	struct vfio_platform_region	*region;

"regions" (plural) has slightly better consistency. Thanks,

Alex

Quoted 4 lines
drivers/vfio/platform/vfio_platform_private.h +1 -0
+	u32				num_regions;
 };
 
 #endif /* VFIO_PLATFORM_PRIVATE_H */
#51 lore raw

Re: [RFC PATCH v6 17/20] vfio: add local lock in virqfd instead of depending on VFIO PCI

In reply to Antonios Motakis
Quoted 2 lines · Antonios Motakis

Sharing the same spinlock with the VFIO bus driver is not necessary for the virqfd code, so remove that dependency.

I like the idea of consolidating this code, but I need more justification for why the use of the lock here is independent of the bus driver and why it's ok to move from a per-device lock to a global lock. Thanks,

Alex

Quoted 146 lines
---
 drivers/vfio/pci/vfio_pci_intrs.c | 10 +++++-----
 drivers/vfio/virqfd.c             | 24 +++++++++++++-----------
 include/linux/vfio.h              |  3 +--
 3 files changed, 19 insertions(+), 18 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +5 -5
index 3f909bb..e56c814 100644
@@ -226,8 +226,8 @@ static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
 static void vfio_intx_disable(struct vfio_pci_device *vdev)
 {
 	vfio_intx_set_signal(vdev, -1);
-	virqfd_disable(vdev, &vdev->ctx[0].unmask);
-	virqfd_disable(vdev, &vdev->ctx[0].mask);
+	virqfd_disable(&vdev->ctx[0].unmask);
+	virqfd_disable(&vdev->ctx[0].mask);
 	vdev->irq_type = VFIO_PCI_NUM_IRQS;
 	vdev->num_ctx = 0;
 	kfree(vdev->ctx);
@@ -377,8 +377,8 @@ static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
 	vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
 
 	for (i = 0; i < vdev->num_ctx; i++) {
-		virqfd_disable(vdev, &vdev->ctx[i].unmask);
-		virqfd_disable(vdev, &vdev->ctx[i].mask);
+		virqfd_disable(&vdev->ctx[i].unmask);
+		virqfd_disable(&vdev->ctx[i].mask);
 	}
 
 	if (msix) {
@@ -415,7 +415,7 @@ static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
 					     vfio_send_intx_eventfd, NULL,
 					     &vdev->ctx[0].unmask, fd);
 
-		virqfd_disable(vdev, &vdev->ctx[0].unmask);
+		virqfd_disable(&vdev->ctx[0].unmask);
 	}
 
 	return 0;
drivers/vfio/virqfd.c +13 -11
index 9cf2842..4528450 100644
@@ -17,6 +17,7 @@
 #include "pci/vfio_pci_private.h"
 
 static struct workqueue_struct *vfio_irqfd_cleanup_wq;
+static spinlock_t lock;
 
 int __init vfio_pci_virqfd_init(void)
 {
@@ -25,6 +26,8 @@ int __init vfio_pci_virqfd_init(void)
 	if (!vfio_irqfd_cleanup_wq)
 		return -ENOMEM;
 
+	spin_lock_init(&lock);
+
 	return 0;
 }
 
@@ -53,21 +56,21 @@ static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
 
 	if (flags & POLLHUP) {
 		unsigned long flags;
-		spin_lock_irqsave(&virqfd->vdev->irqlock, flags);
+		spin_lock_irqsave(&lock, flags);
 
 		/*
 		 * The eventfd is closing, if the virqfd has not yet been
 		 * queued for release, as determined by testing whether the
-		 * vdev pointer to it is still valid, queue it now.  As
+		 * virqfd pointer to it is still valid, queue it now.  As
 		 * with kvm irqfds, we know we won't race against the virqfd
-		 * going away because we hold wqh->lock to get here.
+		 * going away because we hold the lock to get here.
 		 */
 		if (*(virqfd->pvirqfd) == virqfd) {
 			*(virqfd->pvirqfd) = NULL;
 			virqfd_deactivate(virqfd);
 		}
 
-		spin_unlock_irqrestore(&virqfd->vdev->irqlock, flags);
+		spin_unlock_irqrestore(&lock, flags);
 	}
 
 	return 0;
@@ -143,16 +146,16 @@ int virqfd_enable(struct vfio_pci_device *vdev,
 	 * we update the pointer to the virqfd under lock to avoid
 	 * pushing multiple jobs to release the same virqfd.
 	 */
-	spin_lock_irq(&vdev->irqlock);
+	spin_lock_irq(&lock);
 
 	if (*pvirqfd) {
-		spin_unlock_irq(&vdev->irqlock);
+		spin_unlock_irq(&lock);
 		ret = -EBUSY;
 		goto err_busy;
 	}
 	*pvirqfd = virqfd;
 
-	spin_unlock_irq(&vdev->irqlock);
+	spin_unlock_irq(&lock);
 
 	/*
 	 * Install our own custom wake-up handling so we are notified via
@@ -189,19 +192,18 @@ err_fd:
 	return ret;
 }
 
-void virqfd_disable(struct vfio_pci_device *vdev,
-			   struct virqfd **pvirqfd)
+void virqfd_disable(struct virqfd **pvirqfd)
 {
 	unsigned long flags;
 
-	spin_lock_irqsave(&vdev->irqlock, flags);
+	spin_lock_irqsave(&lock, flags);
 
 	if (*pvirqfd) {
 		virqfd_deactivate(*pvirqfd);
 		*pvirqfd = NULL;
 	}
 
-	spin_unlock_irqrestore(&vdev->irqlock, flags);
+	spin_unlock_irqrestore(&lock, flags);
 
 	/*
 	 * Block until we know all outstanding shutdown jobs have completed.
include/linux/vfio.h +1 -2
index 43968e8..44c9808 100644
@@ -123,7 +123,6 @@ extern int virqfd_enable(struct vfio_pci_device *vdev,
 			 int (*handler)(struct vfio_pci_device *, void *),
 			 void (*thread)(struct vfio_pci_device *, void *),
 			 void *data, struct virqfd **pvirqfd, int fd);
-extern void virqfd_disable(struct vfio_pci_device *vdev,
-			   struct virqfd **pvirqfd);
+extern void virqfd_disable(struct virqfd **pvirqfd);
 
 #endif /* VFIO_H */
#6 lore raw

Re: [RFC PATCH v6 02/20] iommu: add capability IOMMU_CAP_NOEXEC

In reply to a message not in the archive

On Thu, Jun 5, 2014 at 10:03 PM, Alex Williamson <[email protected]

Quoted 24 lines

wrote:

Quoted 13 lines · Antonios Motakis

Some IOMMUs accept an IOMMU_NOEXEC protection flag in addition to IOMMU_READ and IOMMU_WRITE. Expose this as an IOMMU capability.

---
 include/linux/iommu.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/iommu.h +0 -0
index fc464d2..7e152fb 100644

include/linux/iommu.h struct iommu_domain { · line 61

Quoted 7 lines
include/linux/iommu.h +2 -2
@@ -57,8 +57,9 @@ struct iommu_domain {
      struct iommu_domain_geometry geometry;
 };
 
-#define IOMMU_CAP_CACHE_COHERENCY    0x1
-#define IOMMU_CAP_INTR_REMAP         0x2     /* isolates device intrs */
+#define IOMMU_CAP_CACHE_COHERENCY    (1 << 0)
+#define IOMMU_CAP_INTR_REMAP         (1 << 1) /* isolates device intrs

*/

Quoted 7 lines

include/linux/iommu.h struct iommu_domain { · line 65

Quoted 4 lines
include/linux/iommu.h +1 -0
+#define IOMMU_CAP_NOEXEC             (1 << 2) /* IOMMU_NOEXEC flag */
 
 /*
  * Following constraints are specifc to FSL_PAMUV1:

Hmm, was this really intended to be a bitmap? Why? Thanks,

Oops, not really :) will change that

Quoted 4 lines

Alex

Signature · 2 lines
Antonios Motakis
Virtual Open Systems
#37 lore raw

Re: [RFC PATCH v6 10/20] vfio/platform: return info for device and its memory mapped IO regions

In reply to Alex Williamson
Quoted 81 lines · Alex Williamson
Quoted 80 lines · Antonios Motakis

A VFIO userspace driver will start by opening the VFIO device that corresponds to an IOMMU group, and will use the ioctl interface to get the basic device info, such as number of memory regions and interrupts, and their properties.

This patch enables the IOCTLs:
- VFIO_DEVICE_GET_INFO
- VFIO_DEVICE_GET_REGION_INFO

IRQ info is provided by one of the latter patches.

---
 drivers/vfio/platform/vfio_platform.c         | 79 +++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h | 17 ++++++
 2 files changed, 90 insertions(+), 6 deletions(-)
drivers/vfio/platform/vfio_platform.c +45 -0
index 1df76d8..eeaebc4 100644
@@ -34,17 +34,66 @@
 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>"
 #define DRIVER_DESC     "VFIO for platform devices - User Level meta-driver"
 
+static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
+{
+     int cnt = 0, i;
+
+     while (platform_get_resource(vdev->pdev, IORESOURCE_MEM, cnt))
+             cnt++;
+
+     vdev->region = kzalloc(sizeof(struct vfio_platform_region) * cnt,
+                             GFP_KERNEL);
+     if (!vdev->region)
+             return -ENOMEM;
+
+     for (i = 0; i < cnt;  i++) {
+             struct resource *res =
+                     platform_get_resource(vdev->pdev, IORESOURCE_MEM, i);
+
+             if (!res)
+                     goto err;
+
+             vdev->region[i].addr = res->start;
+             vdev->region[i].size = resource_size(res);
+             vdev->region[i].flags = 0;
+     }
+
+     vdev->num_regions = cnt;
+
+     return 0;
+err:
+     kfree(vdev->region);
+     return -EINVAL;
+}
+
+static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
+{
+     vdev->num_regions = 0;
+     kfree(vdev->region);
+}
+
 static void vfio_platform_release(void *device_data)
 {
+     struct vfio_platform_device *vdev = device_data;
+
+     vfio_platform_regions_cleanup(vdev);
+
      module_put(THIS_MODULE);
 }
 
 static int vfio_platform_open(void *device_data)
 {
+     struct vfio_platform_device *vdev = device_data;
+     int ret;
+
      if (!try_module_get(THIS_MODULE))
              return -ENODEV;
 
Quoted 11 lines

drivers/vfio/platform/vfio_platform.c line 96

Quoted 6 lines
drivers/vfio/platform/vfio_platform.c +5 -1
-     return 0;
+     ret = vfio_platform_regions_init(vdev);
+     if (ret)
+             module_put(THIS_MODULE);
+
+     return ret;

The user can call VFIO_GROUP_GET_DEVICE_FD for a single device more than once, vfio_pci maintains a reference count to make sure we only allocate and initialize on the first open and cleanup on the last close.

Ack

Quoted 65 lines
Quoted 65 lines
drivers/vfio/platform/vfio_platform.c +23 -5
 }
 
 static long vfio_platform_ioctl(void *device_data,
@@ -65,18 +114,36 @@ static long vfio_platform_ioctl(void *device_data,
                      return -EINVAL;
 
              info.flags = VFIO_DEVICE_FLAGS_PLATFORM;
-             info.num_regions = 0;
+             info.num_regions = vdev->num_regions;
              info.num_irqs = 0;
 
              return copy_to_user((void __user *)arg, &info, minsz);
 
-     } else if (cmd == VFIO_DEVICE_GET_REGION_INFO)
-             return -EINVAL;
+     } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
+             struct vfio_region_info info;
+
+             minsz = offsetofend(struct vfio_region_info, offset);
+
+             if (copy_from_user(&info, (void __user *)arg, minsz))
+                     return -EFAULT;
+
+             if (info.argsz < minsz)
+                     return -EINVAL;
+
+             if (info.index >= vdev->num_regions)
+                     return -EINVAL;
+
+             /* map offset to the physical address  */
+             info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
+             info.size = vdev->region[info.index].size;
+             info.flags = vdev->region[info.index].flags;
+
+             return copy_to_user((void __user *)arg, &info, minsz);
 
-     else if (cmd == VFIO_DEVICE_GET_IRQ_INFO)
+     } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
              return -EINVAL;
 
-     else if (cmd == VFIO_DEVICE_SET_IRQS)
+     } else if (cmd == VFIO_DEVICE_SET_IRQS)
              return -EINVAL;
 
      else if (cmd == VFIO_DEVICE_RESET)
drivers/vfio/platform/vfio_platform_private.h +12 -0
index 4ae88f8..3448f918 100644
@@ -15,8 +15,25 @@
 #ifndef VFIO_PLATFORM_PRIVATE_H
 #define VFIO_PLATFORM_PRIVATE_H
 
+#define VFIO_PLATFORM_OFFSET_SHIFT   40
+#define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1)
+
+#define VFIO_PLATFORM_OFFSET_TO_INDEX(off)   \
+     (off >> VFIO_PLATFORM_OFFSET_SHIFT)
+
+#define VFIO_PLATFORM_INDEX_TO_OFFSET(index) \
+     ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)
+
+struct vfio_platform_region {
+     u64                     addr;
+     resource_size_t         size;
Quoted 9 lines

drivers/vfio/platform/vfio_platform_private.h line 35

Quoted 6 lines
drivers/vfio/platform/vfio_platform_private.h +4 -0
+     u32                     flags;
+};
+
 struct vfio_platform_device {
      struct platform_device          *pdev;
+     struct vfio_platform_region     *region;

"regions" (plural) has slightly better consistency. Thanks,

Ack as well.

Signature · 2 lines
Antonios Motakis
Virtual Open Systems
#52 lore raw

Re: [RFC PATCH v6 17/20] vfio: add local lock in virqfd instead of depending on VFIO PCI

In reply to Alex Williamson
Quoted 8 lines · Alex Williamson
Quoted 2 lines · Antonios Motakis

Sharing the same spinlock with the VFIO bus driver is not necessary for the virqfd code, so remove that dependency.

I like the idea of consolidating this code, but I need more justification for why the use of the lock here is independent of the bus driver and why it's ok to move from a per-device lock to a global lock. Thanks,

In the bus driver we use the lock when working with IRQs (for example when masking/unmasking). VFIO_PCI will use a global lock for all IRQs of a device, VFIO_PLATFORM will use a different lock for each IRQ.

The virqfd code, as decoupled here from the VFIO_PCI code will not touch this directly, but a lock is used to keep any *virqfds variables consistent. In fact, virqfd is not married to IRQs in particular, in theory you could register a handler for any eventfd. So the lock here is used only when creating and destroying eventfds.

I believe the virqfd code should not be particularly aware of what is behind an eventfd, and what the right lock(s) to use would be - if any. Any sensitive variables will be accessed only from he handler registered from the bus driver, who can still hold the spinlock to do it safely.

Virqfd just needs to keep accesses to any struct *virqfd safe, but this comes into play only when creating or destroying eventfds, so the global lock should not be a bottleneck.

Quoted 152 lines

Alex

Quoted 146 lines
---
 drivers/vfio/pci/vfio_pci_intrs.c | 10 +++++-----
 drivers/vfio/virqfd.c             | 24 +++++++++++++-----------
 include/linux/vfio.h              |  3 +--
 3 files changed, 19 insertions(+), 18 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +5 -5
index 3f909bb..e56c814 100644
@@ -226,8 +226,8 @@ static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
 static void vfio_intx_disable(struct vfio_pci_device *vdev)
 {
      vfio_intx_set_signal(vdev, -1);
-     virqfd_disable(vdev, &vdev->ctx[0].unmask);
-     virqfd_disable(vdev, &vdev->ctx[0].mask);
+     virqfd_disable(&vdev->ctx[0].unmask);
+     virqfd_disable(&vdev->ctx[0].mask);
      vdev->irq_type = VFIO_PCI_NUM_IRQS;
      vdev->num_ctx = 0;
      kfree(vdev->ctx);
@@ -377,8 +377,8 @@ static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
      vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
 
      for (i = 0; i < vdev->num_ctx; i++) {
-             virqfd_disable(vdev, &vdev->ctx[i].unmask);
-             virqfd_disable(vdev, &vdev->ctx[i].mask);
+             virqfd_disable(&vdev->ctx[i].unmask);
+             virqfd_disable(&vdev->ctx[i].mask);
      }
 
      if (msix) {
@@ -415,7 +415,7 @@ static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
                                           vfio_send_intx_eventfd, NULL,
                                           &vdev->ctx[0].unmask, fd);
 
-             virqfd_disable(vdev, &vdev->ctx[0].unmask);
+             virqfd_disable(&vdev->ctx[0].unmask);
      }
 
      return 0;
drivers/vfio/virqfd.c +13 -11
index 9cf2842..4528450 100644
@@ -17,6 +17,7 @@
 #include "pci/vfio_pci_private.h"
 
 static struct workqueue_struct *vfio_irqfd_cleanup_wq;
+static spinlock_t lock;
 
 int __init vfio_pci_virqfd_init(void)
 {
@@ -25,6 +26,8 @@ int __init vfio_pci_virqfd_init(void)
      if (!vfio_irqfd_cleanup_wq)
              return -ENOMEM;
 
+     spin_lock_init(&lock);
+
      return 0;
 }
 
@@ -53,21 +56,21 @@ static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
 
      if (flags & POLLHUP) {
              unsigned long flags;
-             spin_lock_irqsave(&virqfd->vdev->irqlock, flags);
+             spin_lock_irqsave(&lock, flags);
 
              /*
               * The eventfd is closing, if the virqfd has not yet been
               * queued for release, as determined by testing whether the
-              * vdev pointer to it is still valid, queue it now.  As
+              * virqfd pointer to it is still valid, queue it now.  As
               * with kvm irqfds, we know we won't race against the virqfd
-              * going away because we hold wqh->lock to get here.
+              * going away because we hold the lock to get here.
               */
              if (*(virqfd->pvirqfd) == virqfd) {
                      *(virqfd->pvirqfd) = NULL;
                      virqfd_deactivate(virqfd);
              }
 
-             spin_unlock_irqrestore(&virqfd->vdev->irqlock, flags);
+             spin_unlock_irqrestore(&lock, flags);
      }
 
      return 0;
@@ -143,16 +146,16 @@ int virqfd_enable(struct vfio_pci_device *vdev,
       * we update the pointer to the virqfd under lock to avoid
       * pushing multiple jobs to release the same virqfd.
       */
-     spin_lock_irq(&vdev->irqlock);
+     spin_lock_irq(&lock);
 
      if (*pvirqfd) {
-             spin_unlock_irq(&vdev->irqlock);
+             spin_unlock_irq(&lock);
              ret = -EBUSY;
              goto err_busy;
      }
      *pvirqfd = virqfd;
 
-     spin_unlock_irq(&vdev->irqlock);
+     spin_unlock_irq(&lock);
 
      /*
       * Install our own custom wake-up handling so we are notified via
@@ -189,19 +192,18 @@ err_fd:
      return ret;
 }
 
-void virqfd_disable(struct vfio_pci_device *vdev,
-                        struct virqfd **pvirqfd)
+void virqfd_disable(struct virqfd **pvirqfd)
 {
      unsigned long flags;
 
-     spin_lock_irqsave(&vdev->irqlock, flags);
+     spin_lock_irqsave(&lock, flags);
 
      if (*pvirqfd) {
              virqfd_deactivate(*pvirqfd);
              *pvirqfd = NULL;
      }
 
-     spin_unlock_irqrestore(&vdev->irqlock, flags);
+     spin_unlock_irqrestore(&lock, flags);
 
      /*
       * Block until we know all outstanding shutdown jobs have completed.
include/linux/vfio.h +1 -2
index 43968e8..44c9808 100644
@@ -123,7 +123,6 @@ extern int virqfd_enable(struct vfio_pci_device *vdev,
                       int (*handler)(struct vfio_pci_device *, void *),
                       void (*thread)(struct vfio_pci_device *, void *),
                       void *data, struct virqfd **pvirqfd, int fd);
-extern void virqfd_disable(struct vfio_pci_device *vdev,
-                        struct virqfd **pvirqfd);
+extern void virqfd_disable(struct virqfd **pvirqfd);
 
 #endif /* VFIO_H */
Signature · 2 lines
Antonios Motakis
Virtual Open Systems
#42 lore raw

Re: [RFC PATCH v6 14/20] vfio/platform: initial interrupts support

In reply to Antonios Motakis
Quoted 118 lines · Antonios Motakis

This patch allows to set an eventfd for a patform device's interrupt, and also to trigger the interrupt eventfd from userspace for testing.

---
 drivers/vfio/platform/vfio_platform.c         |  36 ++++++-
 drivers/vfio/platform/vfio_platform_irq.c     | 130 +++++++++++++++++++++++++-
 drivers/vfio/platform/vfio_platform_private.h |   7 ++
 3 files changed, 169 insertions(+), 4 deletions(-)
drivers/vfio/platform/vfio_platform.c +33 -3
index 192291c..f4c06c6 100644
@@ -177,10 +177,40 @@ static long vfio_platform_ioctl(void *device_data,
 
 		return copy_to_user((void __user *)arg, &info, minsz);
 
-	} else if (cmd == VFIO_DEVICE_SET_IRQS)
-		return -EINVAL;
+	} else if (cmd == VFIO_DEVICE_SET_IRQS) {
+		struct vfio_irq_set hdr;
+		int ret = 0;
+
+		minsz = offsetofend(struct vfio_irq_set, count);
+
+		if (copy_from_user(&hdr, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (hdr.argsz < minsz)
+			return -EINVAL;
+
+		if (hdr.index >= vdev->num_irqs)
+			return -EINVAL;
+
+		if (hdr.start != 0 || hdr.count > 1)
+			return -EINVAL;
+
+		if (hdr.count == 0 &&
+			(!(hdr.flags & VFIO_IRQ_SET_DATA_NONE) ||
+			 !(hdr.flags & VFIO_IRQ_SET_ACTION_TRIGGER)))
+			return -EINVAL;
+
+		if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
+				  VFIO_IRQ_SET_ACTION_TYPE_MASK))
+			return -EINVAL;
+
+		ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
+						   hdr.start, hdr.count,
+						   (void *)arg+minsz);
+
+		return ret;
 
-	else if (cmd == VFIO_DEVICE_RESET)
+	} else if (cmd == VFIO_DEVICE_RESET)
 		return -EINVAL;
 
 	return -ENOTTY;
drivers/vfio/platform/vfio_platform_irq.c +31 -1
index 22c214f..d79f5af 100644
@@ -31,6 +31,9 @@
 
 #include "vfio_platform_private.h"
 
+static int vfio_set_trigger(struct vfio_platform_device *vdev,
+			    int index, int fd);
+
 int vfio_platform_irq_init(struct vfio_platform_device *vdev)
 {
 	int cnt = 0, i;
@@ -43,17 +46,142 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
 		return -ENOMEM;
 
 	for (i = 0; i < cnt; i++) {
-		vdev->irq[i].flags = 0;
+		int hwirq = platform_get_irq(vdev->pdev, i);
+
+		if (hwirq < 0)
+			goto err;
+
+		vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
 		vdev->irq[i].count = 1;
+		vdev->irq[i].hwirq = hwirq;
 	}
 
 	vdev->num_irqs = cnt;
 
 	return 0;
+err:
+	kfree(vdev->irq);
+	return -EINVAL;
 }
 
 void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 {
+	int i;
+
+	for (i = 0; i < vdev->num_irqs; i++)
+		vfio_set_trigger(vdev, i, -1);
+
 	vdev->num_irqs = 0;
 	kfree(vdev->irq);
 }
+
+static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
+{
+	struct eventfd_ctx *trigger = dev_id;
+
+	eventfd_signal(trigger, 1);
+
+	return IRQ_HANDLED;
+}
+
+static int vfio_set_trigger(struct vfio_platform_device *vdev,
+			    int index, int fd)
+{

drivers/vfio/platform/vfio_platform_irq.c int vfio_platform_irq_init(struct vfio_platform_device *vdev) · line 99

Quoted 10 lines
drivers/vfio/platform/vfio_platform_irq.c +10 -0
+	struct vfio_platform_irq *irq = &vdev->irq[index];
+	struct eventfd_ctx *trigger;
+	int ret;
+
+	if (irq->trigger) {
+		free_irq(irq->hwirq, irq);
+		kfree(irq->name);
+		eventfd_ctx_put(irq->trigger);
+		irq->trigger = NULL;
+	}

this feels incredibly racy, is some lock protecting this access?

-Christoffer

#45 lore raw

Re: [RFC PATCH v6 15/20] vfio/platform: support for maskable and automasked interrupts

In reply to Antonios Motakis
Quoted 16 lines · Antonios Motakis

Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.

---
 drivers/vfio/platform/vfio_platform_irq.c     | 112 ++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h |   2 +
 2 files changed, 109 insertions(+), 5 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +0 -0
index d79f5af..10dfbf0 100644
@@ -51,9 +51,17 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
 		if (hwirq < 0)

drivers/vfio/platform/vfio_platform_irq.c int vfio_platform_irq_init(struct vfio_platform_device *vdev) · line 60

Quoted 10 lines
drivers/vfio/platform/vfio_platform_irq.c +7 -1
 			goto err;
 
-		vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
+		spin_lock_init(&vdev->irq[i].lock);
+
+		vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD
+					| VFIO_IRQ_INFO_MASKABLE;
+
+		if (irq_get_trigger_type(hwirq) & IRQ_TYPE_LEVEL_MASK)
+			vdev->irq[i].flags |= VFIO_IRQ_INFO_AUTOMASKED;

This seems to rely on the fact that you had actually loaded a driver for your device to set the right type. Is this assumption always correct?

It seems to me that this configuration bit should now be up to your user space drive who is the best candidate to know details about your device at this point?

Quoted 39 lines
drivers/vfio/platform/vfio_platform_irq.c +21 -3
+
 		vdev->irq[i].count = 1;
 		vdev->irq[i].hwirq = hwirq;
+		vdev->irq[i].masked = false;
 	}
 
 	vdev->num_irqs = cnt;
@@ -77,11 +85,27 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 
 static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
 {
-	struct eventfd_ctx *trigger = dev_id;
+	struct vfio_platform_irq *irq_ctx = dev_id;
+	unsigned long flags;
+	int ret = IRQ_NONE;
+
+	spin_lock_irqsave(&irq_ctx->lock, flags);
+
+	if (!irq_ctx->masked) {
+		ret = IRQ_HANDLED;
+
+		if (irq_ctx->flags & VFIO_IRQ_INFO_AUTOMASKED) {
+			disable_irq_nosync(irq_ctx->hwirq);
+			irq_ctx->masked = true;
+		}
+	}
 
-	eventfd_signal(trigger, 1);
+	spin_unlock_irqrestore(&irq_ctx->lock, flags);
 
-	return IRQ_HANDLED;
+	if (ret == IRQ_HANDLED)
+		eventfd_signal(irq_ctx->trigger, 1);
+
+	return ret;
 }
 
 static int vfio_set_trigger(struct vfio_platform_device *vdev,

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 193

Quoted 8 lines
drivers/vfio/platform/vfio_platform_irq.c +5 -0
@@ -162,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,
 	return -EFAULT;
 }
 
+static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
+				    unsigned index, unsigned start,
+				    unsigned count, uint32_t flags, void *data)
+{
+	uint8_t arr;

arr?

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 204

Quoted 11 lines
drivers/vfio/platform/vfio_platform_irq.c +11 -0
+
+	if (start != 0 || count != 1)
+		return -EINVAL;
+
+	switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+	case VFIO_IRQ_SET_DATA_BOOL:
+		if (copy_from_user(&arr, data, sizeof(uint8_t)))
+			return -EFAULT;
+
+		if (arr != 0x1)
+			return -EINVAL;

why the fallthrough, what's this about?

Quoted 91 lines
drivers/vfio/platform/vfio_platform_irq.c +63 -1
+
+	case VFIO_IRQ_SET_DATA_NONE:
+
+		spin_lock_irq(&vdev->irq[index].lock);
+
+		if (vdev->irq[index].masked) {
+			enable_irq(vdev->irq[index].hwirq);
+			vdev->irq[index].masked = false;
+		}
+
+		spin_unlock_irq(&vdev->irq[index].lock);
+
+		return 0;
+
+	case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
+	default:
+		return -ENOTTY;
+	}
+
+	return 0;
+}
+
+static int vfio_platform_set_irq_mask(struct vfio_platform_device *vdev,
+				    unsigned index, unsigned start,
+				    unsigned count, uint32_t flags, void *data)
+{
+	uint8_t arr;
+
+	if (start != 0 || count != 1)
+		return -EINVAL;
+
+	switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+	case VFIO_IRQ_SET_DATA_BOOL:
+		if (copy_from_user(&arr, data, sizeof(uint8_t)))
+			return -EFAULT;
+
+		if (arr != 0x1)
+			return -EINVAL;
+
+	case VFIO_IRQ_SET_DATA_NONE:
+
+		spin_lock_irq(&vdev->irq[index].lock);
+
+		if (!vdev->irq[index].masked) {
+			disable_irq(vdev->irq[index].hwirq);
+			vdev->irq[index].masked = true;
+		}
+
+		spin_unlock_irq(&vdev->irq[index].lock);
+
+		return 0;
+
+	case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
+	default:
+		return -ENOTTY;
+	}
+
+	return 0;
+}
+
 int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 				 uint32_t flags, unsigned index, unsigned start,
 				 unsigned count, void *data)
@@ -172,8 +272,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 
 	switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
 	case VFIO_IRQ_SET_ACTION_MASK:
+		func = vfio_platform_set_irq_mask;
+		break;
 	case VFIO_IRQ_SET_ACTION_UNMASK:
-		/* XXX not implemented */
+		func = vfio_platform_set_irq_unmask;
 		break;
 	case VFIO_IRQ_SET_ACTION_TRIGGER:
 		func = vfio_platform_set_irq_trigger;
drivers/vfio/platform/vfio_platform_private.h +2 -0
index d6200df..4d887fd 100644
@@ -30,6 +30,8 @@ struct vfio_platform_irq {
 	u32			count;
 	int			hwirq;
 	char			*name;
+	bool			masked;
+	spinlock_t		lock;
 };
 
 struct vfio_platform_region {
Signature · 1 line
1.8.3.2
#13 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

In reply to Antonios Motakis
Quoted 24 lines · Antonios Motakis

With an ARM SMMU, interrupt remapping should always be safe from the SMMU's point of view, as it is properly handled by the GIC.

---
 drivers/iommu/arm-smmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm-smmu.c +1 -1
index 15ab2af..ff29402 100644
@@ -1544,7 +1544,7 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
-	caps |= IOMMU_CAP_NOEXEC;
+	caps |= IOMMU_CAP_NOEXEC | IOMMU_CAP_INTR_REMAP;
 
 	return !!(cap & caps);
 }
Signature · 1 line
1.8.3.2

What does IOMMU_CAP_INTR_REMAP signify exactly? Is there docs/examples somewhere I can look at? (A quick scan of the Linux souce code doesn't reveal much, and I'm not sure if this is purely MSI related or what...)

Thanks,
-Christoffer

maintainer
#14 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 20 lines · Christoffer Dall
Quoted 19 lines · Antonios Motakis

With an ARM SMMU, interrupt remapping should always be safe from the SMMU's point of view, as it is properly handled by the GIC.

---
 drivers/iommu/arm-smmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm-smmu.c +1 -1
index 15ab2af..ff29402 100644
@@ -1544,7 +1544,7 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
-	caps |= IOMMU_CAP_NOEXEC;
+	caps |= IOMMU_CAP_NOEXEC | IOMMU_CAP_INTR_REMAP;
 
Quoted 8 lines
Quoted 5 lines
drivers/iommu/arm-smmu.c +0 -0
 	return !!(cap & caps);
 }
Signature · 1 line
1.8.3.2

What does IOMMU_CAP_INTR_REMAP signify exactly? Is there docs/examples somewhere I can look at? (A quick scan of the Linux souce code doesn't reveal much, and I'm not sure if this is purely MSI related or what...)

The flag was introduced for x86 IOMMUs to detect whether an IOMMU in the system has and enables interrupt remapping to allow safe device assignment to KVM guests. Without interrupt remapping a malicious guest could attack the host with MSIs from the attached device.

How are PCI MSIs implemented on ARM with SMMU and GIC enabled. MSIs are only memory dma transactions in the end, is it guaranteed on ARM that a device only sends MSI transactions it is allowed to?

Joerg

#2 lore raw

Re: [RFC PATCH v6 01/20] iommu/arm-smmu: change IOMMU_EXEC to IOMMU_NOEXEC

In reply to Antonios Motakis
Quoted 15 lines · Antonios Motakis

Exposing the XN flag of the SMMU driver as IOMMU_NOEXEC instead of IOMMU_EXEC makes it enforceable, since for IOMMUs that don't support the XN flag pages will always be executable.

---
 drivers/iommu/arm-smmu.c | 2 +-
 include/linux/iommu.h    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/arm-smmu.c +0 -0
index 647c3c7..d5a2200 100644

drivers/iommu/arm-smmu.c static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd, · line 1298

Quoted 6 lines
drivers/iommu/arm-smmu.c +1 -1
@@ -1294,7 +1294,7 @@ static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
 	}
 
 	/* If no access, create a faulting entry to avoid TLB fills */
-	if (prot & IOMMU_EXEC)
+	if (!(prot & IOMMU_NOEXEC))
 		pteval &= ~ARM_SMMU_PTE_XN;

It's probably simpler to change the logic so that we initialise pteval without XN set, then set it if IOMMU_NOEXEC is set (rather than set it by default, then clear it if NOEXEC is not set).

Will

#8 lore raw

Re: [RFC PATCH v6 03/20] iommu/arm-smmu: add IOMMU_CAP_NOEXEC to the ARM SMMU driver

In reply to Antonios Motakis
Quoted 13 lines · Antonios Motakis

The ARM SMMU supports the IOMMU_NOEXEC protection flag. Add the corresponding IOMMU capability.

---
 drivers/iommu/arm-smmu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/arm-smmu.c +0 -0
index d5a2200..15ab2af 100644

drivers/iommu/arm-smmu.c static int arm_smmu_domain_has_cap(struct iommu_domain *domain, · line 1548

Quoted 5 lines
drivers/iommu/arm-smmu.c +2 -0
@@ -1544,6 +1544,8 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
+	caps |= IOMMU_CAP_NOEXEC;
+

Just initialise caps to IOMMU_CAP_NOEXEC.

Will

#15 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

In reply to Joerg Roedel
Quoted 30 lines · Joerg Roedel
Quoted 20 lines · Christoffer Dall
Quoted 19 lines · Antonios Motakis

With an ARM SMMU, interrupt remapping should always be safe from the SMMU's point of view, as it is properly handled by the GIC.

---
 drivers/iommu/arm-smmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm-smmu.c +1 -1
index 15ab2af..ff29402 100644
@@ -1544,7 +1544,7 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
-	caps |= IOMMU_CAP_NOEXEC;
+	caps |= IOMMU_CAP_NOEXEC | IOMMU_CAP_INTR_REMAP;
 
Quoted 8 lines
Quoted 5 lines
drivers/iommu/arm-smmu.c +0 -0
 	return !!(cap & caps);
 }
Signature · 1 line
1.8.3.2

What does IOMMU_CAP_INTR_REMAP signify exactly? Is there docs/examples somewhere I can look at? (A quick scan of the Linux souce code doesn't reveal much, and I'm not sure if this is purely MSI related or what...)

Quoted 8 lines

The flag was introduced for x86 IOMMUs to detect whether an IOMMU in the system has and enables interrupt remapping to allow safe device assignment to KVM guests. Without interrupt remapping a malicious guest could attack the host with MSIs from the attached device.

How are PCI MSIs implemented on ARM with SMMU and GIC enabled. MSIs are only memory dma transactions in the end, is it guaranteed on ARM that a device only sends MSI transactions it is allowed to?

MSIs look just like memory accesses made by the device, so the SMMU will translate them to point at the GIC ITS (doorbell). The ITS then has tables to work out how to route the MSI.

So, if IOMMU_CAP_INTR_REMAP is simply supposed to indicate that the SMMU can translate MSIs to point somewhere else, then the ARM SMMU can always do it. If it's supposed to indicate that the actual MSI payload can be filtered/routed, then that requires the GIC ITS.

The part I'm unsure of is how VFIO knows where to map the MSIs to. That requires knowledge of the physical and virtual doorbell pages -- is that discoverable in the API?

Will

maintainer
#16 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

MSIs look just like memory accesses made by the device, so the SMMU will translate them to point at the GIC ITS (doorbell). The ITS then has tables to work out how to route the MSI.

Quoted 8 lines

So, if IOMMU_CAP_INTR_REMAP is simply supposed to indicate that the SMMU can translate MSIs to point somewhere else, then the ARM SMMU can always do it. If it's supposed to indicate that the actual MSI payload can be filtered/routed, then that requires the GIC ITS.

The part I'm unsure of is how VFIO knows where to map the MSIs to. That requires knowledge of the physical and virtual doorbell pages -- is that discoverable in the API?

VFIO does not care about the actual routing, it only cares that the device can not send interrupts it is not allowed to send (e.g. interrupts to vectors used by other devices or, on x86, exception vectors). If that is guaranteed by the SMMU or the GIC ITS hardware and driver then it is fine to set this flag.

Joerg

#17 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 14 lines · Joerg Roedel
Quoted 4 lines · Will Deacon

MSIs look just like memory accesses made by the device, so the SMMU will translate them to point at the GIC ITS (doorbell). The ITS then has tables to work out how to route the MSI.

Quoted 8 lines

So, if IOMMU_CAP_INTR_REMAP is simply supposed to indicate that the SMMU can translate MSIs to point somewhere else, then the ARM SMMU can always do it. If it's supposed to indicate that the actual MSI payload can be filtered/routed, then that requires the GIC ITS.

The part I'm unsure of is how VFIO knows where to map the MSIs to. That requires knowledge of the physical and virtual doorbell pages -- is that discoverable in the API?

Quoted 5 lines

VFIO does not care about the actual routing, it only cares that the device can not send interrupts it is not allowed to send (e.g. interrupts to vectors used by other devices or, on x86, exception vectors). If that is guaranteed by the SMMU or the GIC ITS hardware and driver then it is fine to set this flag.

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Will

#9 lore raw

Re: [RFC PATCH v6 03/20] iommu/arm-smmu: add IOMMU_CAP_NOEXEC to the ARM SMMU driver

In reply to Will Deacon
Quoted 17 lines · Will Deacon
Quoted 16 lines · Antonios Motakis

The ARM SMMU supports the IOMMU_NOEXEC protection flag. Add the corresponding IOMMU capability.

---
 drivers/iommu/arm-smmu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/arm-smmu.c +0 -0
index d5a2200..15ab2af 100644
@@ -1544,6 +1544,8 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
Quoted 4 lines

drivers/iommu/arm-smmu.c static int arm_smmu_domain_has_cap(struct iommu_domain *domain, · line 1548

Quoted 2 lines
drivers/iommu/arm-smmu.c +2 -0
+	caps |= IOMMU_CAP_NOEXEC;
+

Just initialise caps to IOMMU_CAP_NOEXEC.

No, it shouldn't be a bitmap in the first place. Thanks,

Alex

#10 lore raw

Re: [RFC PATCH v6 03/20] iommu/arm-smmu: add IOMMU_CAP_NOEXEC to the ARM SMMU driver

Quoted 18 lines · Alex Williamson
Quoted 17 lines · Will Deacon
Quoted 16 lines · Antonios Motakis

The ARM SMMU supports the IOMMU_NOEXEC protection flag. Add the corresponding IOMMU capability.

---
 drivers/iommu/arm-smmu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/arm-smmu.c +0 -0
index d5a2200..15ab2af 100644
@@ -1544,6 +1544,8 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
 	if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
 		caps |= IOMMU_CAP_CACHE_COHERENCY;
 
Quoted 6 lines
Quoted 4 lines

drivers/iommu/arm-smmu.c static int arm_smmu_domain_has_cap(struct iommu_domain *domain, · line 1548

Quoted 2 lines
drivers/iommu/arm-smmu.c +2 -0
+	caps |= IOMMU_CAP_NOEXEC;
+

Just initialise caps to IOMMU_CAP_NOEXEC.

No, it shouldn't be a bitmap in the first place. Thanks,

Ah, ok. I probably led Antonios down the garden path with the ARM SMMU driver then. Apologies!

Will

#28 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

In reply to Joerg Roedel
Quoted 14 lines · Joerg Roedel
Quoted 4 lines · Will Deacon

MSIs look just like memory accesses made by the device, so the SMMU will translate them to point at the GIC ITS (doorbell). The ITS then has tables to work out how to route the MSI.

Quoted 8 lines

So, if IOMMU_CAP_INTR_REMAP is simply supposed to indicate that the SMMU can translate MSIs to point somewhere else, then the ARM SMMU can always do it. If it's supposed to indicate that the actual MSI payload can be filtered/routed, then that requires the GIC ITS.

The part I'm unsure of is how VFIO knows where to map the MSIs to. That requires knowledge of the physical and virtual doorbell pages -- is that discoverable in the API?

Quoted 5 lines

VFIO does not care about the actual routing, it only cares that the device can not send interrupts it is not allowed to send (e.g. interrupts to vectors used by other devices or, on x86, exception vectors). If that is guaranteed by the SMMU or the GIC ITS hardware and driver then it is fine to set this flag.

Yep, I agree. Thanks,

Alex

maintainer
#18 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

In reply to Will Deacon
Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

#19 lore raw

RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Forgive me if this discussion is not relative here, but I thought it is.

How is VFIO restricting devices from writing to MSI/MSI-X,
Is all the vector area is mapped by VFIO to trap the accesses. I am asking this because we might need to emulate ITS somewhere either in KVM or VFIO to provide direct access to devices.
And I don't see any mentions on that. I think this flag needs to be set by ITS emulation.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Joerg Roedel
Sent: Monday, June 16, 2014 8:39 AM
To: Will Deacon
Cc: [email protected]; [email protected]; open list; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]; [email protected]; Christoffer Dall
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#20 lore raw

RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

When I say emulating ITS, I mean translating guest ITS commands to physical ITS commands and placing them in physical queue.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:08 AM
To: Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Forgive me if this discussion is not relative here, but I thought it is.

How is VFIO restricting devices from writing to MSI/MSI-X, Is all the vector area is mapped by VFIO to trap the accesses. I am asking this because we might need to emulate ITS somewhere either in KVM or VFIO to provide direct access to devices.
And I don't see any mentions on that. I think this flag needs to be set by ITS emulation.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Joerg Roedel
Sent: Monday, June 16, 2014 8:39 AM
To: Will Deacon
Cc: [email protected]; [email protected]; open list; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]; [email protected]; Christoffer Dall
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#21 lore raw

RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Sorry there was a type,

The question is:

How is VFIO restricting software from writing to MSI/MSI-X vectors of the device.

-----Original Message-----
From: Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:16 AM
To: Chalamarla, Tirumalesh; Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

When I say emulating ITS, I mean translating guest ITS commands to physical ITS commands and placing them in physical queue.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:08 AM
To: Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Forgive me if this discussion is not relative here, but I thought it is.

How is VFIO restricting devices from writing to MSI/MSI-X, Is all the vector area is mapped by VFIO to trap the accesses. I am asking this because we might need to emulate ITS somewhere either in KVM or VFIO to provide direct access to devices.
And I don't see any mentions on that. I think this flag needs to be set by ITS emulation.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Joerg Roedel
Sent: Monday, June 16, 2014 8:39 AM
To: Will Deacon
Cc: [email protected]; [email protected]; open list; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]; [email protected]; Christoffer Dall
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#22 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 5 lines · Tirumalesh

Sorry there was a type,

The question is:

How is VFIO restricting software from writing to MSI/MSI-X vectors of the device.

All interrupts are configured via ioctl, not MSI config space or the MSI-X vector table in MMIO space. VFIO protects the MSI config area by virtualizing it (you can't actually write the physical enable bit or address/data through VFIO). The MSI-X vector table is protected by preventing read, write, or mmap access to it. QEMU provides further virtualization above the basics provided by VFIO. We really can't guarantee that devices don't have backdoors to configure these though. See the realtek quirk in QEMU for an example of a device that has such a backdoor. That's why we require interrupt remapping, so that a device that does this can only hurt the guest, and require the user to opt-out if they feel they have a sufficiently trusted guest. Thanks,

Alex

Quoted 55 lines

-----Original Message-----
From: Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:16 AM
To: Chalamarla, Tirumalesh; Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; tech-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

When I say emulating ITS, I mean translating guest ITS commands to physical ITS commands and placing them in physical queue.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:08 AM
To: Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; tech-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Forgive me if this discussion is not relative here, but I thought it is.

How is VFIO restricting devices from writing to MSI/MSI-X, Is all the vector area is mapped by VFIO to trap the accesses. I am asking this because we might need to emulate ITS somewhere either in KVM or VFIO to provide direct access to devices.
And I don't see any mentions on that. I think this flag needs to be set by ITS emulation.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Joerg Roedel
Sent: Monday, June 16, 2014 8:39 AM
To: Will Deacon
Cc: [email protected]; [email protected]; open list; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]; moderated list:ARM SMMU DRIVER; tech-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; [email protected]; Christoffer Dall
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

_______________________________________________
kvmarm mailing list [email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
_______________________________________________
kvmarm mailing list [email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#23 lore raw

RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Thanks for the clarification Alex, That’s exactly my point, why are we relying on QEMU or something else to emulate the MSI space when we can directly give access to devices using ITS (of course with a small emulation code). This way we are also benefited from all ITS services like VCPU migration etc.

What about non QEMU VFIO users, for example, if I wanted to use VFIO to assign a device to a user process I don't need to depend on QEMU. I thought this is one of the main goals of vfio to make it independent of hypervisors.

Thanks,
Tirumalesh.

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, June 26, 2014 12:00 PM
To: Chalamarla, Tirumalesh
Cc: Joerg Roedel; Will Deacon; [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 5 lines · Tirumalesh

Sorry there was a type,

The question is:

How is VFIO restricting software from writing to MSI/MSI-X vectors of the device.

All interrupts are configured via ioctl, not MSI config space or the MSI-X vector table in MMIO space. VFIO protects the MSI config area by virtualizing it (you can't actually write the physical enable bit or address/data through VFIO). The MSI-X vector table is protected by preventing read, write, or mmap access to it. QEMU provides further virtualization above the basics provided by VFIO. We really can't guarantee that devices don't have backdoors to configure these though. See the realtek quirk in QEMU for an example of a device that has such a backdoor. That's why we require interrupt remapping, so that a device that does this can only hurt the guest, and require the user to opt-out if they feel they have a sufficiently trusted guest. Thanks,

Alex

Quoted 71 lines

-----Original Message-----
From: Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:16 AM
To: Chalamarla, Tirumalesh; Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

When I say emulating ITS, I mean translating guest ITS commands to physical ITS commands and placing them in physical queue.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:08 AM
To: Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Forgive me if this discussion is not relative here, but I thought it is.

How is VFIO restricting devices from writing to MSI/MSI-X, Is all the vector area is mapped by VFIO to trap the accesses. I am asking this because we might need to emulate ITS somewhere either in KVM or VFIO to provide direct access to devices.
And I don't see any mentions on that. I think this flag needs to be set by ITS emulation.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Joerg Roedel
Sent: Monday, June 16, 2014 8:39 AM
To: Will Deacon
Cc: [email protected]; [email protected]; open list; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]; [email protected]; Christoffer Dall
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#24 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 2 lines · Tirumalesh

Thanks for the clarification Alex, That’s exactly my point, why are we relying on QEMU or something else to emulate the MSI space when we can directly give access to devices using ITS (of course with a small emulation code). This way we are also benefited from all ITS services like VCPU migration etc.

I have no idea what ITS is.

Quoted 1 line

What about non QEMU VFIO users, for example, if I wanted to use VFIO to assign a device to a user process I don't need to depend on QEMU. I thought this is one of the main goals of vfio to make it independent of hypervisors.

Where did QEMU become a requirement? Maybe I'm missing something in the ARM part of the conversation that got chopped off, but this is exactly why we have the VFIO/QEMU split that we do. VFIO provides basic virtualization for config space and restricts access to other areas that users shouldn't be allowed to change. QEMU is just one example of a userspace VFIO driver. QEMU takes the decomposed device exposed through the VFIO ABI and re-creates a PCI device out of it. VFIO itself has no dependency on QEMU. Thanks,

Alex

Quoted 10 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, June 26, 2014 12:00 PM
To: Chalamarla, Tirumalesh
Cc: Joerg Roedel; Will Deacon; [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 2 lines · Tirumalesh

Sorry there was a type,

Quoted 83 lines
Quoted 3 lines

The question is:

How is VFIO restricting software from writing to MSI/MSI-X vectors of the device.

All interrupts are configured via ioctl, not MSI config space or the MSI-X vector table in MMIO space. VFIO protects the MSI config area by virtualizing it (you can't actually write the physical enable bit or address/data through VFIO). The MSI-X vector table is protected by preventing read, write, or mmap access to it. QEMU provides further virtualization above the basics provided by VFIO. We really can't guarantee that devices don't have backdoors to configure these though. See the realtek quirk in QEMU for an example of a device that has such a backdoor. That's why we require interrupt remapping, so that a device that does this can only hurt the guest, and require the user to opt-out if they feel they have a sufficiently trusted guest. Thanks,

Alex

Quoted 71 lines

-----Original Message-----
From: Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:16 AM
To: Chalamarla, Tirumalesh; Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

When I say emulating ITS, I mean translating guest ITS commands to physical ITS commands and placing them in physical queue.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Chalamarla, Tirumalesh
Sent: Thursday, June 26, 2014 11:08 AM
To: Joerg Roedel; Will Deacon
Cc: [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER
Subject: RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Forgive me if this discussion is not relative here, but I thought it is.

How is VFIO restricting devices from writing to MSI/MSI-X, Is all the vector area is mapped by VFIO to trap the accesses. I am asking this because we might need to emulate ITS somewhere either in KVM or VFIO to provide direct access to devices.
And I don't see any mentions on that. I think this flag needs to be set by ITS emulation.

Regards,
Tirumalesh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Joerg Roedel
Sent: Monday, June 16, 2014 8:39 AM
To: Will Deacon
Cc: [email protected]; [email protected]; open list; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]; [email protected]; Christoffer Dall
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Will Deacon

Ok, thanks. In which case, I think this is really a combined property of the SMMU and the interrupt controller, so we might need some extra code so that the SMMU can check that the interrupt controller for the device is also capable of interrupt remapping.

Right, that this is part of IOMMU code has more or less historic reasons on x86. Interrupt remapping is purely implemented in the IOMMU there, so on ARM some clue-code between interrupt controler and smmu is needed.

Joerg

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#25 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 8 lines · Alex Williamson
Quoted 5 lines · Tirumalesh

Thanks for the clarification Alex, That’s exactly my point, why are we relying on QEMU or something else to emulate the MSI space when we can directly give access to devices using ITS (of course with a small emulation code). This way we are also benefited from all ITS services like VCPU migration etc.

I have no idea what ITS is.

ITS is the MSI doorbell for GICv3 (ARM's latest interrupt controller).

I agree that we will need an ITS emulation if we want to use MSIs in the guest, and I believe that Marc (CC'd) had already started thinking about that.

Quoted 5 lines
Quoted 4 lines

What about non QEMU VFIO users, for example, if I wanted to use VFIO to
assign a device to a user process I don't need to depend on QEMU. I
thought this is one of the main goals of vfio to make it independent of hypervisors.

Quoted 8 lines

Where did QEMU become a requirement? Maybe I'm missing something in the ARM part of the conversation that got chopped off, but this is exactly why we have the VFIO/QEMU split that we do. VFIO provides basic virtualization for config space and restricts access to other areas that users shouldn't be allowed to change. QEMU is just one example of a userspace VFIO driver. QEMU takes the decomposed device exposed through the VFIO ABI and re-creates a PCI device out of it. VFIO itself has no dependency on QEMU. Thanks,

I also don't understand the QEMU part here. The MSI emulation would be in the kernel, just like the GICv2 emulation that we already have. For userspace drivers, wouldn't you just use eventfd rather than bother with emulating MSIs?

Finally, the interrupt remapping part is about the SMMU preventing MSI writes to arbitrary portions of the host address space. The ITS is about routing interrupts to CPUs.

Will
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#26 lore raw

RE: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Marc,

What is your opinion on ITS emulation . is it should be part of KVM or VFIO.
Also this code needs to depend on ITS host driver a lot, Host ITS driver needs to have an interface for this code to use.

Thanks,
Tirumalesh

-----Original Message-----
From: Will Deacon [mailto:[email protected]]
Sent: Friday, June 27, 2014 1:47 AM
To: Alex Williamson
Cc: Chalamarla, Tirumalesh; Joerg Roedel; [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 8 lines · Alex Williamson
Quoted 5 lines · Tirumalesh

Thanks for the clarification Alex, That’s exactly my point, why are we relying on QEMU or something else to emulate the MSI space when we can directly give access to devices using ITS (of course with a small emulation code). This way we are also benefited from all ITS services like VCPU migration etc.

I have no idea what ITS is.

ITS is the MSI doorbell for GICv3 (ARM's latest interrupt controller).

I agree that we will need an ITS emulation if we want to use MSIs in the guest, and I believe that Marc (CC'd) had already started thinking about that.

Quoted 5 lines
Quoted 4 lines

What about non QEMU VFIO users, for example, if I wanted to use VFIO to
assign a device to a user process I don't need to depend on QEMU. I
thought this is one of the main goals of vfio to make it independent of hypervisors.

Quoted 8 lines

Where did QEMU become a requirement? Maybe I'm missing something in the ARM part of the conversation that got chopped off, but this is exactly why we have the VFIO/QEMU split that we do. VFIO provides basic virtualization for config space and restricts access to other areas that users shouldn't be allowed to change. QEMU is just one example of a userspace VFIO driver. QEMU takes the decomposed device exposed through the VFIO ABI and re-creates a PCI device out of it. VFIO itself has no dependency on QEMU. Thanks,

I also don't understand the QEMU part here. The MSI emulation would be in the kernel, just like the GICv2 emulation that we already have. For userspace drivers, wouldn't you just use eventfd rather than bother with emulating MSIs?

Finally, the interrupt remapping part is about the SMMU preventing MSI writes to arbitrary portions of the host address space. The ITS is about routing interrupts to CPUs.

Will

#27 lore raw

Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 4 lines · Tirumalesh

Marc,

What is your opinion on ITS emulation . is it should be part of KVM or VFIO.

Making any sort of emulation part of VFIO sounds quite wrong. That's not what VFIO is about, at all. Emulation belongs to the hypervisor, and nowhere else.

Quoted 2 lines

Also this code needs to depend on ITS host driver a lot, Host ITS driver needs to have an interface for this code to use.

You can share the command interface as some form of library, but that's about it. There is no more relationship between the ITS driver and the ITS emulation as there is between the GIC driver and its emulation counterpart.

	M.
Quoted 51 lines

Thanks,
Tirumalesh

-----Original Message-----
From: Will Deacon [mailto:[email protected]]
Sent: Friday, June 27, 2014 1:47 AM
To: Alex Williamson
Cc: Chalamarla, Tirumalesh; Joerg Roedel; [email protected]; open list; [email protected]; [email protected]; [email protected]; [email protected]; moderated list:ARM SMMU DRIVER; [email protected]
Subject: Re: [RFC PATCH v6 04/20] iommu/arm-smmu: add capability IOMMU_CAP_INTR_REMAP

Quoted 8 lines · Alex Williamson
Quoted 5 lines · Tirumalesh

Thanks for the clarification Alex, That’s exactly my point, why are we relying on QEMU or something else to emulate the MSI space when we can directly give access to devices using ITS (of course with a small emulation code). This way we are also benefited from all ITS services like VCPU migration etc.

I have no idea what ITS is.

ITS is the MSI doorbell for GICv3 (ARM's latest interrupt controller).

I agree that we will need an ITS emulation if we want to use MSIs in

Quoted 9 lines

the guest, and I believe that Marc (CC'd) had already started thinking about that.

Quoted 4 lines

What about non QEMU VFIO users, for example, if I wanted to use VFIO to
assign a device to a user process I don't need to depend on QEMU. I
thought this is one of the main goals of vfio to make it independent of hypervisors.

Quoted 8 lines

Where did QEMU become a requirement? Maybe I'm missing something in the ARM part of the conversation that got chopped off, but this is exactly why we have the VFIO/QEMU split that we do. VFIO provides basic virtualization for config space and restricts access to other areas that users shouldn't be allowed to change. QEMU is just one example of a userspace VFIO driver. QEMU takes the decomposed device exposed through the VFIO ABI and re-creates a PCI device out of it. VFIO itself has no dependency on QEMU. Thanks,

I also don't understand the QEMU part here. The MSI emulation would be

Quoted 3 lines

in the kernel, just like the GICv2 emulation that we already have. For userspace drivers, wouldn't you just use eventfd rather than bother with emulating MSIs?

Finally, the interrupt remapping part is about the SMMU preventing MSI

Quoted 2 lines

writes to arbitrary portions of the host address space. The ITS is about routing interrupts to CPUs.

Will

Signature · 5 lines
Jazz is not dead. It just smells funny.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#46 lore raw

Re: [RFC PATCH v6 15/20] vfio/platform: support for maskable and automasked interrupts

In reply to Christoffer Dall
Quoted 31 lines · Christoffer Dall
Quoted 16 lines · Antonios Motakis

Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.

---
 drivers/vfio/platform/vfio_platform_irq.c     | 112 ++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h |   2 +
 2 files changed, 109 insertions(+), 5 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +0 -0
index d79f5af..10dfbf0 100644
@@ -51,9 +51,17 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
              if (hwirq < 0)

drivers/vfio/platform/vfio_platform_irq.c int vfio_platform_irq_init(struct vfio_platform_device *vdev) · line 60

Quoted 10 lines
drivers/vfio/platform/vfio_platform_irq.c +7 -1
                      goto err;
 
-             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
+             spin_lock_init(&vdev->irq[i].lock);
+
+             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD
+                                     | VFIO_IRQ_INFO_MASKABLE;
+
+             if (irq_get_trigger_type(hwirq) & IRQ_TYPE_LEVEL_MASK)
+                     vdev->irq[i].flags |= VFIO_IRQ_INFO_AUTOMASKED;

This seems to rely on the fact that you had actually loaded a driver for your device to set the right type. Is this assumption always correct?

Quoted 4 lines

It seems to me that this configuration bit should now be up to your user space drive who is the best candidate to know details about your device at this point?

Hm, I see this type being set usually either in a device tree source, or in the support code for a specific platform. Are there any situations where this is actually set by the driver? If I understand right this is not the case for the PL330, but if it is possible that it is the case for another device then I need to rethink this. Though as far as I understand this should not be the case.

Quoted 42 lines
Quoted 42 lines
drivers/vfio/platform/vfio_platform_irq.c +21 -3
+
              vdev->irq[i].count = 1;
              vdev->irq[i].hwirq = hwirq;
+             vdev->irq[i].masked = false;
      }
 
      vdev->num_irqs = cnt;
@@ -77,11 +85,27 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 
 static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
 {
-     struct eventfd_ctx *trigger = dev_id;
+     struct vfio_platform_irq *irq_ctx = dev_id;
+     unsigned long flags;
+     int ret = IRQ_NONE;
+
+     spin_lock_irqsave(&irq_ctx->lock, flags);
+
+     if (!irq_ctx->masked) {
+             ret = IRQ_HANDLED;
+
+             if (irq_ctx->flags & VFIO_IRQ_INFO_AUTOMASKED) {
+                     disable_irq_nosync(irq_ctx->hwirq);
+                     irq_ctx->masked = true;
+             }
+     }
 
-     eventfd_signal(trigger, 1);
+     spin_unlock_irqrestore(&irq_ctx->lock, flags);
 
-     return IRQ_HANDLED;
+     if (ret == IRQ_HANDLED)
+             eventfd_signal(irq_ctx->trigger, 1);
+
+     return ret;
 }
 
 static int vfio_set_trigger(struct vfio_platform_device *vdev,
@@ -162,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,
      return -EFAULT;
 }
 
Quoted 8 lines

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 193

Quoted 5 lines
drivers/vfio/platform/vfio_platform_irq.c +5 -0
+static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
+                                 unsigned index, unsigned start,
+                                 unsigned count, uint32_t flags, void *data)
+{
+     uint8_t arr;

arr?

arr for array! As in, the VFIO API allows an array of IRQs. However for platform devices we don't use this, each IRQ is exposed independently as an array of 1 IRQ.

Quoted 14 lines

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 204

Quoted 12 lines
drivers/vfio/platform/vfio_platform_irq.c +11 -0
+
+     if (start != 0 || count != 1)
+             return -EINVAL;
+
+     switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+     case VFIO_IRQ_SET_DATA_BOOL:
+             if (copy_from_user(&arr, data, sizeof(uint8_t)))
+                     return -EFAULT;
+
+             if (arr != 0x1)
+                     return -EINVAL;

why the fallthrough, what's this about?

The VFIO API allows to unmask/mask an array of IRQs, however with platform devices we only have arrays of 1 IRQ (so not really arrays).

So if the user uses VFIO_IRQ_SET_DATA_BOOL, we need to check that arr == 0x1. When that is the case, a fallthrough to the same code for VFIO_IRQ_SET_DATA_NONE is safe.

If that is not readable enough, then I can add a comment or duplicate the code that does the unmasking. I realize that if you don't know the VFIO API well, then this can look confusing.

Quoted 92 lines
Quoted 92 lines
drivers/vfio/platform/vfio_platform_irq.c +63 -1
+
+     case VFIO_IRQ_SET_DATA_NONE:
+
+             spin_lock_irq(&vdev->irq[index].lock);
+
+             if (vdev->irq[index].masked) {
+                     enable_irq(vdev->irq[index].hwirq);
+                     vdev->irq[index].masked = false;
+             }
+
+             spin_unlock_irq(&vdev->irq[index].lock);
+
+             return 0;
+
+     case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
+     default:
+             return -ENOTTY;
+     }
+
+     return 0;
+}
+
+static int vfio_platform_set_irq_mask(struct vfio_platform_device *vdev,
+                                 unsigned index, unsigned start,
+                                 unsigned count, uint32_t flags, void *data)
+{
+     uint8_t arr;
+
+     if (start != 0 || count != 1)
+             return -EINVAL;
+
+     switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+     case VFIO_IRQ_SET_DATA_BOOL:
+             if (copy_from_user(&arr, data, sizeof(uint8_t)))
+                     return -EFAULT;
+
+             if (arr != 0x1)
+                     return -EINVAL;
+
+     case VFIO_IRQ_SET_DATA_NONE:
+
+             spin_lock_irq(&vdev->irq[index].lock);
+
+             if (!vdev->irq[index].masked) {
+                     disable_irq(vdev->irq[index].hwirq);
+                     vdev->irq[index].masked = true;
+             }
+
+             spin_unlock_irq(&vdev->irq[index].lock);
+
+             return 0;
+
+     case VFIO_IRQ_SET_DATA_EVENTFD: /* XXX not implemented yet */
+     default:
+             return -ENOTTY;
+     }
+
+     return 0;
+}
+
 int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
                               uint32_t flags, unsigned index, unsigned start,
                               unsigned count, void *data)
@@ -172,8 +272,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 
      switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
      case VFIO_IRQ_SET_ACTION_MASK:
+             func = vfio_platform_set_irq_mask;
+             break;
      case VFIO_IRQ_SET_ACTION_UNMASK:
-             /* XXX not implemented */
+             func = vfio_platform_set_irq_unmask;
              break;
      case VFIO_IRQ_SET_ACTION_TRIGGER:
              func = vfio_platform_set_irq_trigger;
drivers/vfio/platform/vfio_platform_private.h +2 -1
index d6200df..4d887fd 100644
@@ -30,6 +30,8 @@ struct vfio_platform_irq {
      u32                     count;
      int                     hwirq;
      char                    *name;
+     bool                    masked;
+     spinlock_t              lock;
 };
 
 struct vfio_platform_region {
--

1.8.3.2

Signature · 2 lines
Antonios Motakis
Virtual Open Systems
#43 lore raw

Re: [RFC PATCH v6 14/20] vfio/platform: initial interrupts support

In reply to Christoffer Dall
Quoted 124 lines · Christoffer Dall
Quoted 122 lines · Antonios Motakis

This patch allows to set an eventfd for a patform device's interrupt, and also to trigger the interrupt eventfd from userspace for testing.

---
 drivers/vfio/platform/vfio_platform.c         |  36 ++++++-
 drivers/vfio/platform/vfio_platform_irq.c     | 130 +++++++++++++++++++++++++-
 drivers/vfio/platform/vfio_platform_private.h |   7 ++
 3 files changed, 169 insertions(+), 4 deletions(-)
drivers/vfio/platform/vfio_platform.c +33 -3
index 192291c..f4c06c6 100644
@@ -177,10 +177,40 @@ static long vfio_platform_ioctl(void *device_data,
 
              return copy_to_user((void __user *)arg, &info, minsz);
 
-     } else if (cmd == VFIO_DEVICE_SET_IRQS)
-             return -EINVAL;
+     } else if (cmd == VFIO_DEVICE_SET_IRQS) {
+             struct vfio_irq_set hdr;
+             int ret = 0;
+
+             minsz = offsetofend(struct vfio_irq_set, count);
+
+             if (copy_from_user(&hdr, (void __user *)arg, minsz))
+                     return -EFAULT;
+
+             if (hdr.argsz < minsz)
+                     return -EINVAL;
+
+             if (hdr.index >= vdev->num_irqs)
+                     return -EINVAL;
+
+             if (hdr.start != 0 || hdr.count > 1)
+                     return -EINVAL;
+
+             if (hdr.count == 0 &&
+                     (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE) ||
+                      !(hdr.flags & VFIO_IRQ_SET_ACTION_TRIGGER)))
+                     return -EINVAL;
+
+             if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
+                               VFIO_IRQ_SET_ACTION_TYPE_MASK))
+                     return -EINVAL;
+
+             ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
+                                                hdr.start, hdr.count,
+                                                (void *)arg+minsz);
+
+             return ret;
 
-     else if (cmd == VFIO_DEVICE_RESET)
+     } else if (cmd == VFIO_DEVICE_RESET)
              return -EINVAL;
 
      return -ENOTTY;
drivers/vfio/platform/vfio_platform_irq.c +35 -1
index 22c214f..d79f5af 100644
@@ -31,6 +31,9 @@
 
 #include "vfio_platform_private.h"
 
+static int vfio_set_trigger(struct vfio_platform_device *vdev,
+                         int index, int fd);
+
 int vfio_platform_irq_init(struct vfio_platform_device *vdev)
 {
      int cnt = 0, i;
@@ -43,17 +46,142 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
              return -ENOMEM;
 
      for (i = 0; i < cnt; i++) {
-             vdev->irq[i].flags = 0;
+             int hwirq = platform_get_irq(vdev->pdev, i);
+
+             if (hwirq < 0)
+                     goto err;
+
+             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
              vdev->irq[i].count = 1;
+             vdev->irq[i].hwirq = hwirq;
      }
 
      vdev->num_irqs = cnt;
 
      return 0;
+err:
+     kfree(vdev->irq);
+     return -EINVAL;
 }
 
 void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 {
+     int i;
+
+     for (i = 0; i < vdev->num_irqs; i++)
+             vfio_set_trigger(vdev, i, -1);
+
      vdev->num_irqs = 0;
      kfree(vdev->irq);
 }
+
+static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
+{
+     struct eventfd_ctx *trigger = dev_id;
+
+     eventfd_signal(trigger, 1);
+
+     return IRQ_HANDLED;
+}
+
+static int vfio_set_trigger(struct vfio_platform_device *vdev,
+                         int index, int fd)
+{
+     struct vfio_platform_irq *irq = &vdev->irq[index];
+     struct eventfd_ctx *trigger;
+     int ret;
+
Quoted 9 lines

drivers/vfio/platform/vfio_platform_irq.c int vfio_platform_irq_init(struct vfio_platform_device *vdev) · line 99

Quoted 6 lines
drivers/vfio/platform/vfio_platform_irq.c +6 -0
+     if (irq->trigger) {
+             free_irq(irq->hwirq, irq);
+             kfree(irq->name);
+             eventfd_ctx_put(irq->trigger);
+             irq->trigger = NULL;
+     }

this feels incredibly racy, is some lock protecting this access?

Good catch; there should have been a mutex earlier protecting it, but it is missing. Thanks.

Quoted 1 line

-Christoffer

Signature · 2 lines
Antonios Motakis
Virtual Open Systems
#47 lore raw

Re: [RFC PATCH v6 15/20] vfio/platform: support for maskable and automasked interrupts

In reply to Antonios Motakis
Quoted 38 lines · Antonios Motakis
Quoted 31 lines · Christoffer Dall
Quoted 16 lines · Antonios Motakis

Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.

---
 drivers/vfio/platform/vfio_platform_irq.c     | 112 ++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h |   2 +
 2 files changed, 109 insertions(+), 5 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +0 -0
index d79f5af..10dfbf0 100644
@@ -51,9 +51,17 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
              if (hwirq < 0)

drivers/vfio/platform/vfio_platform_irq.c int vfio_platform_irq_init(struct vfio_platform_device *vdev) · line 60

Quoted 10 lines
drivers/vfio/platform/vfio_platform_irq.c +7 -1
                      goto err;
 
-             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
+             spin_lock_init(&vdev->irq[i].lock);
+
+             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD
+                                     | VFIO_IRQ_INFO_MASKABLE;
+
+             if (irq_get_trigger_type(hwirq) & IRQ_TYPE_LEVEL_MASK)
+                     vdev->irq[i].flags |= VFIO_IRQ_INFO_AUTOMASKED;

This seems to rely on the fact that you had actually loaded a driver for your device to set the right type. Is this assumption always correct?

Quoted 4 lines

It seems to me that this configuration bit should now be up to your user space drive who is the best candidate to know details about your device at this point?

Quoted 7 lines

Hm, I see this type being set usually either in a device tree source, or in the support code for a specific platform. Are there any situations where this is actually set by the driver? If I understand right this is not the case for the PL330, but if it is possible that it is the case for another device then I need to rethink this. Though as far as I understand this should not be the case.

Wow, this has been incredibly long time since I looked at this code, so not sure if I remember my original reasoning anymore, however,

while device properties are set in the DT, they would only be available to this code if you actually loaded a device driver for that device, right? I'm just not sure that assumption always holds for devices used by VFIO, but I'm really not sure anymore. Maybe I'm rambling.

Quoted 51 lines
Quoted 42 lines
Quoted 42 lines
drivers/vfio/platform/vfio_platform_irq.c +21 -3
+
              vdev->irq[i].count = 1;
              vdev->irq[i].hwirq = hwirq;
+             vdev->irq[i].masked = false;
      }
 
      vdev->num_irqs = cnt;
@@ -77,11 +85,27 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 
 static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
 {
-     struct eventfd_ctx *trigger = dev_id;
+     struct vfio_platform_irq *irq_ctx = dev_id;
+     unsigned long flags;
+     int ret = IRQ_NONE;
+
+     spin_lock_irqsave(&irq_ctx->lock, flags);
+
+     if (!irq_ctx->masked) {
+             ret = IRQ_HANDLED;
+
+             if (irq_ctx->flags & VFIO_IRQ_INFO_AUTOMASKED) {
+                     disable_irq_nosync(irq_ctx->hwirq);
+                     irq_ctx->masked = true;
+             }
+     }
 
-     eventfd_signal(trigger, 1);
+     spin_unlock_irqrestore(&irq_ctx->lock, flags);
 
-     return IRQ_HANDLED;
+     if (ret == IRQ_HANDLED)
+             eventfd_signal(irq_ctx->trigger, 1);
+
+     return ret;
 }
 
 static int vfio_set_trigger(struct vfio_platform_device *vdev,
@@ -162,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,
      return -EFAULT;
 }
 
Quoted 8 lines

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 193

Quoted 5 lines
drivers/vfio/platform/vfio_platform_irq.c +5 -0
+static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
+                                 unsigned index, unsigned start,
+                                 unsigned count, uint32_t flags, void *data)
+{
+     uint8_t arr;

arr?

Quoted 4 lines

arr for array! As in, the VFIO API allows an array of IRQs. However for platform devices we don't use this, each IRQ is exposed independently as an array of 1 IRQ.

but it's not an array. If it contains IRQs, call it irqs. Unless this is referring specifically to a field *named* array, I don't remember the API at current, but reading the code along it didn't make sense to me to have a uint8_t called arr, and code should make as much sense independenly as possible.

This reminds me of people writing code like:

String str;

yuck.

Quoted 18 lines
Quoted 14 lines

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 204

Quoted 12 lines
drivers/vfio/platform/vfio_platform_irq.c +11 -0
+
+     if (start != 0 || count != 1)
+             return -EINVAL;
+
+     switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+     case VFIO_IRQ_SET_DATA_BOOL:
+             if (copy_from_user(&arr, data, sizeof(uint8_t)))
+                     return -EFAULT;
+
+             if (arr != 0x1)
+                     return -EINVAL;

why the fallthrough, what's this about?

The VFIO API allows to unmask/mask an array of IRQs, however with platform devices we only have arrays of 1 IRQ (so not really arrays).

Quoted 8 lines

So if the user uses VFIO_IRQ_SET_DATA_BOOL, we need to check that arr == 0x1. When that is the case, a fallthrough to the same code for VFIO_IRQ_SET_DATA_NONE is safe.

If that is not readable enough, then I can add a comment or duplicate the code that does the unmasking. I realize that if you don't know the VFIO API well, then this can look confusing.

yeah, please put a big fat comment explaining the fallthrough.

-Christoffer

#48 lore raw

Re: [RFC PATCH v6 15/20] vfio/platform: support for maskable and automasked interrupts

Quoted 47 lines · Christoffer Dall
Quoted 38 lines · Antonios Motakis
Quoted 31 lines · Christoffer Dall
Quoted 16 lines · Antonios Motakis

Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.

---
 drivers/vfio/platform/vfio_platform_irq.c     | 112 ++++++++++++++++++++++++--
 drivers/vfio/platform/vfio_platform_private.h |   2 +
 2 files changed, 109 insertions(+), 5 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +0 -0
index d79f5af..10dfbf0 100644
@@ -51,9 +51,17 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)
              if (hwirq < 0)

drivers/vfio/platform/vfio_platform_irq.c int vfio_platform_irq_init(struct vfio_platform_device *vdev) · line 60

Quoted 10 lines
drivers/vfio/platform/vfio_platform_irq.c +7 -1
                      goto err;
 
-             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD;
+             spin_lock_init(&vdev->irq[i].lock);
+
+             vdev->irq[i].flags = VFIO_IRQ_INFO_EVENTFD
+                                     | VFIO_IRQ_INFO_MASKABLE;
+
+             if (irq_get_trigger_type(hwirq) & IRQ_TYPE_LEVEL_MASK)
+                     vdev->irq[i].flags |= VFIO_IRQ_INFO_AUTOMASKED;

This seems to rely on the fact that you had actually loaded a driver for your device to set the right type. Is this assumption always correct?

Quoted 4 lines

It seems to me that this configuration bit should now be up to your user space drive who is the best candidate to know details about your device at this point?

Quoted 7 lines

Hm, I see this type being set usually either in a device tree source, or in the support code for a specific platform. Are there any situations where this is actually set by the driver? If I understand right this is not the case for the PL330, but if it is possible that it is the case for another device then I need to rethink this. Though as far as I understand this should not be the case.

Quoted 7 lines

Wow, this has been incredibly long time since I looked at this code, so not sure if I remember my original reasoning anymore, however,

while device properties are set in the DT, they would only be available to this code if you actually loaded a device driver for that device, right? I'm just not sure that assumption always holds for devices used by VFIO, but I'm really not sure anymore. Maybe I'm rambling.

The device I'm testing with, the PL330 DMAC, is one of the devices that exposes level sensitive interrupts, and therefore for it to properly work VFIO needs to be able to expose it as automasked.

I just tested the code on a kernel that doesn't include the native PL330 DMA driver. It seems that even so, the unmasked property is properly detected and exposed by VFIO. So for this scenario at least the assumptions are true...

I'm afraid I have to admit that if there are any edge cases where this might not be true, I don't know which they are :(

Quoted 63 lines
Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/vfio/platform/vfio_platform_irq.c +21 -3
+
              vdev->irq[i].count = 1;
              vdev->irq[i].hwirq = hwirq;
+             vdev->irq[i].masked = false;
      }
 
      vdev->num_irqs = cnt;
@@ -77,11 +85,27 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev)
 
 static irqreturn_t vfio_irq_handler(int irq, void *dev_id)
 {
-     struct eventfd_ctx *trigger = dev_id;
+     struct vfio_platform_irq *irq_ctx = dev_id;
+     unsigned long flags;
+     int ret = IRQ_NONE;
+
+     spin_lock_irqsave(&irq_ctx->lock, flags);
+
+     if (!irq_ctx->masked) {
+             ret = IRQ_HANDLED;
+
+             if (irq_ctx->flags & VFIO_IRQ_INFO_AUTOMASKED) {
+                     disable_irq_nosync(irq_ctx->hwirq);
+                     irq_ctx->masked = true;
+             }
+     }
 
-     eventfd_signal(trigger, 1);
+     spin_unlock_irqrestore(&irq_ctx->lock, flags);
 
-     return IRQ_HANDLED;
+     if (ret == IRQ_HANDLED)
+             eventfd_signal(irq_ctx->trigger, 1);
+
+     return ret;
 }
 
 static int vfio_set_trigger(struct vfio_platform_device *vdev,
@@ -162,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,
      return -EFAULT;
 }
 
Quoted 8 lines

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 193

Quoted 5 lines
drivers/vfio/platform/vfio_platform_irq.c +5 -0
+static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev,
+                                 unsigned index, unsigned start,
+                                 unsigned count, uint32_t flags, void *data)
+{
+     uint8_t arr;

arr?

Quoted 4 lines

arr for array! As in, the VFIO API allows an array of IRQs. However for platform devices we don't use this, each IRQ is exposed independently as an array of 1 IRQ.

but it's not an array. If it contains IRQs, call it irqs. Unless this is referring specifically to a field *named* array, I don't remember the API at current, but reading the code along it didn't make sense to me to have a uint8_t called arr, and code should make as much sense independenly as possible.

Quoted 5 lines

This reminds me of people writing code like:

String str;

yuck.

You are completely right, the naming is unfortunate. I will change it to something clearer, (e.g. irq_bitmap?)

Quoted 23 lines
Quoted 23 lines
Quoted 15 lines

drivers/vfio/platform/vfio_platform_irq.c static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, · line 204

Quoted 13 lines
drivers/vfio/platform/vfio_platform_irq.c +11 -0
+
+     if (start != 0 || count != 1)
+             return -EINVAL;
+
+     switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+     case VFIO_IRQ_SET_DATA_BOOL:
+             if (copy_from_user(&arr, data, sizeof(uint8_t)))
+                     return -EFAULT;
+
+             if (arr != 0x1)
+                     return -EINVAL;

why the fallthrough, what's this about?

The VFIO API allows to unmask/mask an array of IRQs, however with platform devices we only have arrays of 1 IRQ (so not really arrays).

So if the user uses VFIO_IRQ_SET_DATA_BOOL, we need to check that arr == 0x1. When that is the case, a fallthrough to the same code for VFIO_IRQ_SET_DATA_NONE is safe.

Quoted 6 lines
Quoted 4 lines

If that is not readable enough, then I can add a comment or duplicate the code that does the unmasking. I realize that if you don't know the VFIO API well, then this can look confusing.

yeah, please put a big fat comment explaining the fallthrough.

Ack.

Quoted 2 lines

-Christoffer

Signature · 2 lines
Antonios Motakis
Virtual Open Systems