Skip to contentSkip to conversation

vfio/cdx: add support for CDX bus

PATCH Superseded Nipun Gupta 2 participants 3 messages Apr 17, 2023 – Apr 18, 2023

Subsystems (5)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Nipun Gupta

author maintainer
#0 lore raw

PATCH v3 1/1 vfio/cdx: add support for CDX bus

vfio-cdx driver enables IOCTLs for user space to query MMIO regions for CDX devices and mmap them. This change also adds support for reset of CDX devices.

This change adds the VFIO CDX driver and enables the following ioctls for CDX devices:
- VFIO_DEVICE_GET_INFO:
- VFIO_DEVICE_GET_REGION_INFO
- VFIO_DEVICE_RESET

---

Changes v2->v3:
- removed redundant init and release functions
- removed redundant dev and cdx_dev from vfio_cdx_device
- added support for iommufd
- added VFIO_DEVICE_FLAGS_CDX
- removed unrequried WARN_ON
- removed unused ioaddr

Changes v1->v2:
- Updated file2alias to support vfio_cdx
- removed some un-necessary checks in mmap
- removed vfio reset wrapper API
- converted complex macros to static APIs
- used pgprot_device and io_remap_pfn_range

 MAINTAINERS                         |   7 +
 drivers/vfio/Kconfig                |   1 +
 drivers/vfio/Makefile               |   1 +
 drivers/vfio/cdx/Kconfig            |  17 ++
 drivers/vfio/cdx/Makefile           |   8 +
 drivers/vfio/cdx/vfio_cdx.c         | 271 ++++++++++++++++++++++++++++
 drivers/vfio/cdx/vfio_cdx_private.h |  28 +++
 include/linux/cdx/cdx_bus.h         |   1 -
 include/linux/mod_devicetable.h     |   6 +
 include/uapi/linux/vfio.h           |   1 +
 scripts/mod/devicetable-offsets.c   |   1 +
 scripts/mod/file2alias.c            |  17 +-
 12 files changed, 357 insertions(+), 2 deletions(-)
 create mode 100644 drivers/vfio/cdx/Kconfig
 create mode 100644 drivers/vfio/cdx/Makefile
 create mode 100644 drivers/vfio/cdx/vfio_cdx.c
 create mode 100644 drivers/vfio/cdx/vfio_cdx_private.h
MAINTAINERS +7 -0
index 7f74d8571ac9..c4fd42ba8f46 100644
@@ -22064,6 +22064,13 @@ F:	Documentation/filesystems/vfat.rst
 F:	fs/fat/
 F:	tools/testing/selftests/filesystems/fat/
 
+VFIO CDX DRIVER
+M:	Nipun Gupta <[email protected]>
+M:	Nikhil Agarwal <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/cdx/*
+
 VFIO DRIVER
 M:	Alex Williamson <[email protected]>
 L:	[email protected]
drivers/vfio/Kconfig +1 -0
index 89e06c981e43..aba36f5be4ec 100644
@@ -57,6 +57,7 @@ source "drivers/vfio/pci/Kconfig"
 source "drivers/vfio/platform/Kconfig"
 source "drivers/vfio/mdev/Kconfig"
 source "drivers/vfio/fsl-mc/Kconfig"
+source "drivers/vfio/cdx/Kconfig"
 endif
 
 source "virt/lib/Kconfig"
drivers/vfio/Makefile +1 -0
index 70e7dcb302ef..1a27b2516612 100644
@@ -14,3 +14,4 @@ obj-$(CONFIG_VFIO_PCI) += pci/
 obj-$(CONFIG_VFIO_PLATFORM) += platform/
 obj-$(CONFIG_VFIO_MDEV) += mdev/
 obj-$(CONFIG_VFIO_FSL_MC) += fsl-mc/
+obj-$(CONFIG_VFIO_CDX) += cdx/
drivers/vfio/cdx/Kconfig new file +17 -0
new file mode 100644
index 000000000000..e6de0a0caa32
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# VFIO CDX configuration
+#
+# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+#
+
+config VFIO_CDX
+	tristate "VFIO support for CDX bus devices"
+	depends on CDX_BUS
+	select EVENTFD
+	help
+	  Driver to enable VFIO support for the devices on CDX bus.
+	  This is required to make use of CDX devices present in
+	  the system using the VFIO framework.
+
+	  If you don't know what to do here, say N.
drivers/vfio/cdx/Makefile new file +8 -0
new file mode 100644
index 000000000000..82e4ef412c0f
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+#
+
+obj-$(CONFIG_VFIO_CDX) += vfio-cdx.o
+
+vfio-cdx-objs := vfio_cdx.o
drivers/vfio/cdx/vfio_cdx.c new file +271 -0

271 lines not on this page · raw message

drivers/vfio/cdx/vfio_cdx_private.h new file +28 -0
new file mode 100644
index 000000000000..8bdc117ea88e
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+ */
+
+#ifndef VFIO_CDX_PRIVATE_H
+#define VFIO_CDX_PRIVATE_H
+
+#define VFIO_CDX_OFFSET_SHIFT    40
+
+static inline u64 vfio_cdx_index_to_offset(u32 index)
+{
+	return ((u64)(index) << VFIO_CDX_OFFSET_SHIFT);
+}
+
+struct vfio_cdx_region {
+	u32			flags;
+	u32			type;
+	u64			addr;
+	resource_size_t		size;
+};
+
+struct vfio_cdx_device {
+	struct vfio_device	vdev;
+	struct vfio_cdx_region	*regions;
+};
+
+#endif /* VFIO_CDX_PRIVATE_H */
include/linux/cdx/cdx_bus.h +0 -1
index 35ef41d8a61a..bead71b7bc73 100644
@@ -14,7 +14,6 @@
 #include <linux/mod_devicetable.h>
 
 #define MAX_CDX_DEV_RESOURCES	4
-#define CDX_ANY_ID (0xFFFF)
 #define CDX_CONTROLLER_ID_SHIFT 4
 #define CDX_BUS_NUM_MASK 0xF
 
include/linux/mod_devicetable.h +6 -0
index ccaaeda792c0..ccf017353bb6 100644
@@ -912,6 +912,12 @@ struct ishtp_device_id {
 	kernel_ulong_t driver_data;
 };
 
+#define CDX_ANY_ID (0xFFFF)
+
+enum {
+	CDX_ID_F_VFIO_DRIVER_OVERRIDE = 1,
+};
+
 /**
  * struct cdx_device_id - CDX device identifier
  * @vendor: Vendor ID
include/uapi/linux/vfio.h +1 -0
index 0552e8dcf0cb..8e91aaf973e7 100644
@@ -213,6 +213,7 @@ struct vfio_device_info {
 #define VFIO_DEVICE_FLAGS_AP	(1 << 5)	/* vfio-ap device */
 #define VFIO_DEVICE_FLAGS_FSL_MC (1 << 6)	/* vfio-fsl-mc device */
 #define VFIO_DEVICE_FLAGS_CAPS	(1 << 7)	/* Info supports caps */
+#define VFIO_DEVICE_FLAGS_CDX	(1 << 8)	/* vfio-cdx device */
 	__u32	num_regions;	/* Max region index + 1 */
 	__u32	num_irqs;	/* Max IRQ index + 1 */
 	__u32   cap_offset;	/* Offset within info struct of first cap */
scripts/mod/devicetable-offsets.c +1 -0
index 62dc988df84d..abe65f8968dd 100644
@@ -265,6 +265,7 @@ int main(void)
 	DEVID(cdx_device_id);
 	DEVID_FIELD(cdx_device_id, vendor);
 	DEVID_FIELD(cdx_device_id, device);
+	DEVID_FIELD(cdx_device_id, override_only);
 
 	return 0;
 }
scripts/mod/file2alias.c +16 -1
index 28da34ba4359..38120f932b0d 100644
@@ -1458,8 +1458,23 @@ static int do_cdx_entry(const char *filename, void *symval,
 {
 	DEF_FIELD(symval, cdx_device_id, vendor);
 	DEF_FIELD(symval, cdx_device_id, device);
+	DEF_FIELD(symval, cdx_device_id, override_only);
 
-	sprintf(alias, "cdx:v%08Xd%08Xd", vendor, device);
+	switch (override_only) {
+	case 0:
+		strcpy(alias, "cdx:");
+		break;
+	case CDX_ID_F_VFIO_DRIVER_OVERRIDE:
+		strcpy(alias, "vfio_cdx:");
+		break;
+	default:
+		warn("Unknown CDX driver_override alias %08X\n",
+		     override_only);
+		return 0;
+	}
+
+	ADD(alias, "v", vendor != CDX_ANY_ID, vendor);
+	ADD(alias, "d", device != CDX_ANY_ID, device);
 	return 1;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 13 lines · On Mon, 17 Apr 2023 14:07:25 +0530 Nipun Gupta <[email protected]> wrote:
drivers/vfio/cdx/vfio_cdx.c new file +13 -0
+static long vfio_cdx_ioctl(struct vfio_device *core_vdev,
+			   unsigned int cmd, unsigned long arg)
+{
+	struct vfio_cdx_device *vdev =
+		container_of(core_vdev, struct vfio_cdx_device, vdev);
+	struct cdx_device *cdx_dev = to_cdx_device(core_vdev->dev);
+	unsigned long minsz;
+
+	switch (cmd) {
+	case VFIO_DEVICE_GET_INFO:
+	{
+		struct vfio_device_info info;
+

drivers/vfio/cdx/vfio_cdx.c line 103

Quoted 10 lines
drivers/vfio/cdx/vfio_cdx.c new file +10 -0
+		minsz = offsetofend(struct vfio_device_info, num_irqs);
+
+		if (copy_from_user(&info, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (info.argsz < minsz)
+			return -EINVAL;
+
+		info.flags = VFIO_DEVICE_FLAGS_CDX;
+		info.flags = VFIO_DEVICE_FLAGS_RESET;

Whoops, I think you mean |= for the latter one. Thanks,

Alex

author maintainer
#2 lore raw

[AMD Official Use Only - General]

Quoted 36 lines

-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Monday, April 17, 2023 7:51 PM
To: Gupta, Nipun <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; git (AMD-Xilinx) <[email protected]>; Anand, Harpreet <[email protected]>; Jansen Van Vuuren, Pieter <pieter.jansen- [email protected]>; Agarwal, Nikhil <[email protected]>; Simek, Michal <[email protected]>
Subject: Re: [PATCH v3] vfio/cdx: add support for CDX bus

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 17 lines · On Mon, 17 Apr 2023 14:07:25 +0530 Nipun Gupta <[email protected]> wrote:
drivers/vfio/cdx/vfio_cdx.c new file +17 -0
+static long vfio_cdx_ioctl(struct vfio_device *core_vdev,
+                        unsigned int cmd, unsigned long arg)
+{
+     struct vfio_cdx_device *vdev =
+             container_of(core_vdev, struct vfio_cdx_device, vdev);
+     struct cdx_device *cdx_dev = to_cdx_device(core_vdev->dev);
+     unsigned long minsz;
+
+     switch (cmd) {
+     case VFIO_DEVICE_GET_INFO:
+     {
+             struct vfio_device_info info;
+
+             minsz = offsetofend(struct vfio_device_info, num_irqs);
+
+             if (copy_from_user(&info, (void __user *)arg, minsz))
+                     return -EFAULT;
Quoted 8 lines

drivers/vfio/cdx/vfio_cdx.c line 103

Quoted 6 lines
drivers/vfio/cdx/vfio_cdx.c new file +6 -0
+
+             if (info.argsz < minsz)
+                     return -EINVAL;
+
+             info.flags = VFIO_DEVICE_FLAGS_CDX;
+             info.flags = VFIO_DEVICE_FLAGS_RESET;

Whoops, I think you mean |= for the latter one. Thanks,

Oh!! Will send a quick v4 fixing this.

Thanks,
Nipun

Quoted 2 lines

Alex