Skip to contentSkip to conversation

vfio: platform: reset: add support for XHCI reset hook

PATCH In review Gregory CLEMENT 2 participants 3 messages Jul 11, 2019 – Jul 15, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio: platform: reset: add support for XHCI reset hook

The VFIO reset hook is called every time a platform device is passed to a guest or removed from a guest.

When the XHCI device is unbound from the host, the host driver disables the XHCI clocks/phys/regulators so when the device is passed to the guest it becomes dis-functional.

This initial implementation uses the VFIO reset hook to enable the XHCI clocks/phys on behalf of the guest.

Ported from Marvell LSP code originally written by Yehuda Yitschak

---
 drivers/vfio/platform/reset/Kconfig           |  8 +++
 drivers/vfio/platform/reset/Makefile          |  2 +
 .../vfio/platform/reset/vfio_platform_xhci.c  | 60 +++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 drivers/vfio/platform/reset/vfio_platform_xhci.c
drivers/vfio/platform/reset/Kconfig +8 -0
index 392e3c09def0..14f620fd250d 100644
@@ -22,3 +22,11 @@ config VFIO_PLATFORM_BCMFLEXRM_RESET
 	  Enables the VFIO platform driver to handle reset for Broadcom FlexRM
 
 	  If you don't know what to do here, say N.
+
+config VFIO_PLATFORM_XHCI_RESET
+	tristate "VFIO support for USB XHCI reset"
+	depends on VFIO_PLATFORM
+	help
+	  Enables the VFIO platform driver to handle reset for USB XHCI
+
+	  If you don't know what to do here, say N.
drivers/vfio/platform/reset/Makefile +2 -0
index 7294c5ea122e..d84c4d3dc041 100644
@@ -1,7 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 vfio-platform-calxedaxgmac-y := vfio_platform_calxedaxgmac.o
 vfio-platform-amdxgbe-y := vfio_platform_amdxgbe.o
+vfio-platform-xhci-y := vfio_platform_xhci.o
 
 obj-$(CONFIG_VFIO_PLATFORM_CALXEDAXGMAC_RESET) += vfio-platform-calxedaxgmac.o
 obj-$(CONFIG_VFIO_PLATFORM_AMDXGBE_RESET) += vfio-platform-amdxgbe.o
 obj-$(CONFIG_VFIO_PLATFORM_BCMFLEXRM_RESET) += vfio_platform_bcmflexrm.o
+obj-$(CONFIG_VFIO_PLATFORM_XHCI_RESET) += vfio-platform-xhci.o
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#1 lore raw

Hi Gregory,

Quoted 9 lines · Gregory CLEMENT

The VFIO reset hook is called every time a platform device is passed to a guest or removed from a guest.

When the XHCI device is unbound from the host, the host driver disables the XHCI clocks/phys/regulators so when the device is passed to the guest it becomes dis-functional.

This initial implementation uses the VFIO reset hook to enable the XHCI clocks/phys on behalf of the guest.

the platform reset module must also make sure there are no more DMA requests and interrupts that can be sent by the device anymore.

Quoted 50 lines

Ported from Marvell LSP code originally written by Yehuda Yitschak

---
 drivers/vfio/platform/reset/Kconfig           |  8 +++
 drivers/vfio/platform/reset/Makefile          |  2 +
 .../vfio/platform/reset/vfio_platform_xhci.c  | 60 +++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 drivers/vfio/platform/reset/vfio_platform_xhci.c
drivers/vfio/platform/reset/Kconfig +8 -0
index 392e3c09def0..14f620fd250d 100644
@@ -22,3 +22,11 @@ config VFIO_PLATFORM_BCMFLEXRM_RESET
 	  Enables the VFIO platform driver to handle reset for Broadcom FlexRM
 
 	  If you don't know what to do here, say N.
+
+config VFIO_PLATFORM_XHCI_RESET
+	tristate "VFIO support for USB XHCI reset"
+	depends on VFIO_PLATFORM
+	help
+	  Enables the VFIO platform driver to handle reset for USB XHCI
+
+	  If you don't know what to do here, say N.
drivers/vfio/platform/reset/Makefile +2 -0
index 7294c5ea122e..d84c4d3dc041 100644
@@ -1,7 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 vfio-platform-calxedaxgmac-y := vfio_platform_calxedaxgmac.o
 vfio-platform-amdxgbe-y := vfio_platform_amdxgbe.o
+vfio-platform-xhci-y := vfio_platform_xhci.o
 
 obj-$(CONFIG_VFIO_PLATFORM_CALXEDAXGMAC_RESET) += vfio-platform-calxedaxgmac.o
 obj-$(CONFIG_VFIO_PLATFORM_AMDXGBE_RESET) += vfio-platform-amdxgbe.o
 obj-$(CONFIG_VFIO_PLATFORM_BCMFLEXRM_RESET) += vfio_platform_bcmflexrm.o
+obj-$(CONFIG_VFIO_PLATFORM_XHCI_RESET) += vfio-platform-xhci.o
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +3 -0
new file mode 100644
index 000000000000..7b75a04402ee
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * VFIO platform driver specialized for XHCI reset

drivers/vfio/platform/reset/vfio_platform_xhci.c line 13

Quoted 10 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +10 -0
+ *
+ * Copyright 2016 Marvell Semiconductors, Inc.
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/kernel.h>

io, init, kernel should be removable (noticed init and kernel.h also are in other reset modules though)

drivers/vfio/platform/reset/vfio_platform_xhci.c line 21

Quoted 8 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +8 -0
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/phy/phy.h>
+#include <linux/usb/phy.h>
+
+#include "../vfio_platform_private.h"
+
+#define MAX_XHCI_CLOCKS		4

Where does this number come from?

From Documentation/devicetree/bindings/usb/usb-xhci.txt I understand there are max 2 clocks, "core" and "reg" (I don't have any specific knowledge on the device though).

drivers/vfio/platform/reset/vfio_platform_xhci.c line 22

Quoted 1 line
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +1 -0
+#define MAX_XHCI_PHYS		2

not used

Quoted 10 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +10 -0
+
+int vfio_platform_xhci_reset(struct vfio_platform_device *vdev)
+{
+	struct device *dev = vdev->device;
+	struct device_node *np = dev->of_node;
+	struct usb_phy *usb_phy;
+	struct clk *clk;
+	int ret, i;
+
+	/*

drivers/vfio/platform/reset/vfio_platform_xhci.c line 42

Quoted 10 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +10 -0
+	 * Compared to the native driver, no need to handle the
+	 * deferred case, because the resources are already
+	 * there
+	 */
+	for (i = 0; i < MAX_XHCI_CLOCKS; i++) {
+		clk = of_clk_get(np, i);
+		if (!IS_ERR(clk)) {
+			ret = clk_prepare_enable(clk);
+			if (ret)
+				return -ENODEV;

return ret?

drivers/vfio/platform/reset/vfio_platform_xhci.c line 50

Quoted 8 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +8 -0
+		}
+	}
+
+	usb_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
+	if (!IS_ERR(usb_phy)) {
+		ret = usb_phy_init(usb_phy);
+		if (ret)
+			return -ENODEV;

return ret?

drivers/vfio/platform/reset/vfio_platform_xhci.c line 60

Quoted 12 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +10 -0
+	}
 
+
+	return 0;
+}
+
+module_vfio_reset_handler("generic-xhci", vfio_platform_xhci_reset);
+
+MODULE_AUTHOR("Yehuda Yitschak");
+MODULE_DESCRIPTION("Reset support for XHCI vfio platform device");
+MODULE_LICENSE("GPL");

Thanks

Eric

#2 lore raw

Hi Eric,

Quoted 10 lines

Hi Gregory,

Quoted 7 lines · Gregory CLEMENT

The VFIO reset hook is called every time a platform device is passed to a guest or removed from a guest.

When the XHCI device is unbound from the host, the host driver disables the XHCI clocks/phys/regulators so when the device is passed to the guest it becomes dis-functional.

Quoted 5 lines
Quoted 2 lines

This initial implementation uses the VFIO reset hook to enable the XHCI clocks/phys on behalf of the guest.

the platform reset module must also make sure there are no more DMA requests and interrupts that can be sent by the device anymore.

OK I will take care of it too.

Quoted 54 lines
Quoted 54 lines

Ported from Marvell LSP code originally written by Yehuda Yitschak

---
 drivers/vfio/platform/reset/Kconfig           |  8 +++
 drivers/vfio/platform/reset/Makefile          |  2 +
 .../vfio/platform/reset/vfio_platform_xhci.c  | 60 +++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 drivers/vfio/platform/reset/vfio_platform_xhci.c
drivers/vfio/platform/reset/Kconfig +8 -0
index 392e3c09def0..14f620fd250d 100644
@@ -22,3 +22,11 @@ config VFIO_PLATFORM_BCMFLEXRM_RESET
 	  Enables the VFIO platform driver to handle reset for Broadcom FlexRM
 
 	  If you don't know what to do here, say N.
+
+config VFIO_PLATFORM_XHCI_RESET
+	tristate "VFIO support for USB XHCI reset"
+	depends on VFIO_PLATFORM
+	help
+	  Enables the VFIO platform driver to handle reset for USB XHCI
+
+	  If you don't know what to do here, say N.
drivers/vfio/platform/reset/Makefile +2 -0
index 7294c5ea122e..d84c4d3dc041 100644
@@ -1,7 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 vfio-platform-calxedaxgmac-y := vfio_platform_calxedaxgmac.o
 vfio-platform-amdxgbe-y := vfio_platform_amdxgbe.o
+vfio-platform-xhci-y := vfio_platform_xhci.o
 
 obj-$(CONFIG_VFIO_PLATFORM_CALXEDAXGMAC_RESET) += vfio-platform-calxedaxgmac.o
 obj-$(CONFIG_VFIO_PLATFORM_AMDXGBE_RESET) += vfio-platform-amdxgbe.o
 obj-$(CONFIG_VFIO_PLATFORM_BCMFLEXRM_RESET) += vfio_platform_bcmflexrm.o
+obj-$(CONFIG_VFIO_PLATFORM_XHCI_RESET) += vfio-platform-xhci.o
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +7 -0
new file mode 100644
index 000000000000..7b75a04402ee
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * VFIO platform driver specialized for XHCI reset
+ *
+ * Copyright 2016 Marvell Semiconductors, Inc.
+ *
+ */
Quoted 8 lines

drivers/vfio/platform/reset/vfio_platform_xhci.c line 13

Quoted 6 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +6 -0
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/kernel.h>

io, init, kernel should be removable (noticed init and kernel.h also are in other reset modules though)

OK

Quoted 10 lines

drivers/vfio/platform/reset/vfio_platform_xhci.c line 21

Quoted 8 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +8 -0
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/phy/phy.h>
+#include <linux/usb/phy.h>
+
+#include "../vfio_platform_private.h"
+
+#define MAX_XHCI_CLOCKS		4

Where does this number come from?

Quoted 3 lines

From Documentation/devicetree/bindings/usb/usb-xhci.txt I understand there are max 2 clocks, "core" and "reg" (I don't have any specific knowledge on the device though).

Right, I guess the intent was to be future proof if there is more clocks needed, but as we don't know, it's better to use the number of clokck we know.

Quoted 3 lines

drivers/vfio/platform/reset/vfio_platform_xhci.c line 22

Quoted 2 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +1 -0
+#define MAX_XHCI_PHYS		2

not used

Right!

Quoted 13 lines
Quoted 13 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +13 -0
+
+int vfio_platform_xhci_reset(struct vfio_platform_device *vdev)
+{
+	struct device *dev = vdev->device;
+	struct device_node *np = dev->of_node;
+	struct usb_phy *usb_phy;
+	struct clk *clk;
+	int ret, i;
+
+	/*
+	 * Compared to the native driver, no need to handle the
+	 * deferred case, because the resources are already
+	 * there
Quoted 8 lines

drivers/vfio/platform/reset/vfio_platform_xhci.c line 42

Quoted 7 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +7 -0
+	 */
+	for (i = 0; i < MAX_XHCI_CLOCKS; i++) {
+		clk = of_clk_get(np, i);
+		if (!IS_ERR(clk)) {
+			ret = clk_prepare_enable(clk);
+			if (ret)
+				return -ENODEV;

return ret?

OK

Quoted 9 lines

drivers/vfio/platform/reset/vfio_platform_xhci.c line 50

Quoted 8 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +8 -0
+		}
+	}
+
+	usb_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
+	if (!IS_ERR(usb_phy)) {
+		ret = usb_phy_init(usb_phy);
+		if (ret)
+			return -ENODEV;

return ret?

OK

Thanks,

Gregory

Quoted 15 lines

drivers/vfio/platform/reset/vfio_platform_xhci.c line 60

Quoted 12 lines
drivers/vfio/platform/reset/vfio_platform_xhci.c new file +10 -0
+	}
 
+
+	return 0;
+}
+
+module_vfio_reset_handler("generic-xhci", vfio_platform_xhci_reset);
+
+MODULE_AUTHOR("Yehuda Yitschak");
+MODULE_DESCRIPTION("Reset support for XHCI vfio platform device");
+MODULE_LICENSE("GPL");

Thanks

Eric

Signature · 3 lines
Gregory Clement, Bootlin
Embedded Linux and Kernel engineering
http://bootlin.com