mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module
@ 2024-09-17  9:38 Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 1/6] vfio_platform: Introduce vfio_platform_get_region helper Eric Auger
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

We introduce a new vfio platform reset module for the tegra234
Multi-Gigabit Ethernet (MGBE).

This reset driver is more complex than previous ones because some
resources need to be prepared and released (clocks and reset). The
existing infrastructure was too simplistic to do that. So this series
extends the original single reset function to an ops struct enhanced
with optional init/release callbacks.

There the reset and clocks are handled. the callbacks are called
respectively on vfio_platform_init/release_common().

The actual reset toggles the mac reset, disable mac interrupts,
stop DMA requests and do a SW reset.

The reset code is inspired of the native driver:
net/ethernet/stmicro/stmmac/dwxgmac2_dma.c and
net/ethernet/stmicro/stmmac/dwmac-tegra.c

This series can be found at:
https://github.com/eauger/linux/tree/tegra234-mgbe-reset-module-rfc-v2

The qemu series to test with can be found at
https://github.com/eauger/qemu/tree/tegra234-mgbe-rfc

Best Regards

Eric

---

History:
v1 -> v2:
- rename reset_ops into of_reset_ops
- Move region initialization to vfio_platform_init_common
- Use init/release of_reset_ops callbacks instead of open/close


Eric Auger (6):
  vfio_platform: Introduce vfio_platform_get_region helper
  vfio_platform: reset: Prepare for additional reset ops
  vfio-platform: Move region initialization to vfio_platform_init_common
  vfio_platform: reset: Introduce new init and release callbacks
  vfio-platform: Add a new handle to store reset data
  vfio/platform: Add tegra234-mgbe vfio platform reset module

 drivers/vfio/platform/reset/Kconfig           |   7 +
 drivers/vfio/platform/reset/Makefile          |   2 +
 .../platform/reset/vfio_platform_amdxgbe.c    |   7 +-
 .../reset/vfio_platform_calxedaxgmac.c        |   7 +-
 .../reset/vfio_platform_tegra234_mgbe.c       | 234 ++++++++++++++++++
 drivers/vfio/platform/vfio_platform_common.c  |  88 +++++--
 drivers/vfio/platform/vfio_platform_private.h |  43 +++-
 7 files changed, 349 insertions(+), 39 deletions(-)
 create mode 100644 drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c

-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH v2 1/6] vfio_platform: Introduce vfio_platform_get_region helper
  2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
@ 2024-09-17  9:38 ` Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 2/6] vfio_platform: reset: Prepare for additional reset ops Eric Auger
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

Reset modules need to access some specific regions. It may
easier and safer to refer to those regions using their
name instead of relying on their index.

So let's introduce a helper that looks for the
struct vfio_platform_region with a given name.

Signed-off-by: Eric Auger <eric.auger@redhat.com>

---

I don't know if reg names described in binding yaml are guaranteed to
appear in the listed order. I guess no, hence the reg-names.
In my case, for the host tegra234 dt node, regs I even observe regs that
are not documented in the yaml:
mac, xpcs, macsec-base, hypervisor whereas yaml only describes
hypervisor, mac, xpcs
---
 drivers/vfio/platform/vfio_platform_common.c  | 14 ++++++++++++++
 drivers/vfio/platform/vfio_platform_private.h |  5 +++++
 2 files changed, 19 insertions(+)

diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c
index e53757d1d095..6861f977fd5b 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -153,6 +153,7 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
 		vdev->regions[i].addr = res->start;
 		vdev->regions[i].size = resource_size(res);
 		vdev->regions[i].flags = 0;
+		vdev->regions[i].name = res->name;
 
 		switch (resource_type(res)) {
 		case IORESOURCE_MEM:
@@ -188,6 +189,19 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
 	return -EINVAL;
 }
 
+struct vfio_platform_region*
+vfio_platform_get_region(struct vfio_platform_device *vdev, const char *name)
+{
+	int i;
+
+	for (i = 0; i < vdev->num_regions; i++) {
+		if (!strcmp(vdev->regions[i].name, name))
+			return &vdev->regions[i];
+	}
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(vfio_platform_get_region);
+
 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
 {
 	int i;
diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h
index 8d8fab516849..20d67634bc41 100644
--- a/drivers/vfio/platform/vfio_platform_private.h
+++ b/drivers/vfio/platform/vfio_platform_private.h
@@ -37,6 +37,7 @@ struct vfio_platform_region {
 	resource_size_t		size;
 	u32			flags;
 	u32			type;
+	const char		*name;
 #define VFIO_PLATFORM_REGION_TYPE_MMIO	1
 #define VFIO_PLATFORM_REGION_TYPE_PIO	2
 	void __iomem		*ioaddr;
@@ -104,6 +105,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 void __vfio_platform_register_reset(struct vfio_platform_reset_node *n);
 void vfio_platform_unregister_reset(const char *compat,
 				    vfio_platform_reset_fn_t fn);
+
+struct vfio_platform_region *
+vfio_platform_get_region(struct vfio_platform_device *vdev, const char *name);
+
 #define vfio_platform_register_reset(__compat, __reset)		\
 static struct vfio_platform_reset_node __reset ## _node = {	\
 	.owner = THIS_MODULE,					\
-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH v2 2/6] vfio_platform: reset: Prepare for additional reset ops
  2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 1/6] vfio_platform: Introduce vfio_platform_get_region helper Eric Auger
@ 2024-09-17  9:38 ` Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 3/6] vfio-platform: Move region initialization to vfio_platform_init_common Eric Auger
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

Reset modules currently offer a single reset function that gets
called on open, close and VFIO_DEVICE_RESET ioctl.

For more complex devices this infrastructure looks too simplistic.
Indeed some resources may be needed from the init() until the
release(), like clocks, resets. A single function does not allow
that setup.

So let's encapsulate the current reset function into an ops struct
that will be soon extended with new open and close callbacks.

Existing reset modules are adapted.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 .../platform/reset/vfio_platform_amdxgbe.c    |  7 +++-
 .../reset/vfio_platform_calxedaxgmac.c        |  7 +++-
 drivers/vfio/platform/vfio_platform_common.c  | 36 ++++++++++---------
 drivers/vfio/platform/vfio_platform_private.h | 30 ++++++++++------
 4 files changed, 50 insertions(+), 30 deletions(-)

diff --git a/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c b/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c
index abdca900802d..037d6e5ffd92 100644
--- a/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c
+++ b/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c
@@ -109,7 +109,12 @@ static int vfio_platform_amdxgbe_reset(struct vfio_platform_device *vdev)
 	return 0;
 }
 
-module_vfio_reset_handler("amd,xgbe-seattle-v1a", vfio_platform_amdxgbe_reset);
+static const struct vfio_platform_of_reset_ops
+vfio_platform_amdxgbe_of_reset_ops = {
+	.reset = vfio_platform_amdxgbe_reset,
+};
+
+module_vfio_reset_handler("amd,xgbe-seattle-v1a", vfio_platform_amdxgbe_of_reset_ops);
 
 MODULE_VERSION("0.1");
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c b/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c
index 63cc7f0b2e4a..f1d62821aa73 100644
--- a/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c
+++ b/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c
@@ -66,7 +66,12 @@ static int vfio_platform_calxedaxgmac_reset(struct vfio_platform_device *vdev)
 	return 0;
 }
 
-module_vfio_reset_handler("calxeda,hb-xgmac", vfio_platform_calxedaxgmac_reset);
+static const struct vfio_platform_of_reset_ops
+vfio_platform_calxedaxgmac_of_reset_ops = {
+	.reset = vfio_platform_calxedaxgmac_reset,
+};
+
+module_vfio_reset_handler("calxeda,hb-xgmac", vfio_platform_calxedaxgmac_of_reset_ops);
 
 MODULE_VERSION(DRIVER_VERSION);
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c
index 6861f977fd5b..976864d2e2f0 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -28,23 +28,23 @@
 static LIST_HEAD(reset_list);
 static DEFINE_MUTEX(driver_lock);
 
-static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
-					struct module **module)
+static const struct vfio_platform_of_reset_ops *
+vfio_platform_lookup_reset(const char *compat, struct module **module)
 {
+	const struct vfio_platform_of_reset_ops *ops = NULL;
 	struct vfio_platform_reset_node *iter;
-	vfio_platform_reset_fn_t reset_fn = NULL;
 
 	mutex_lock(&driver_lock);
 	list_for_each_entry(iter, &reset_list, link) {
 		if (!strcmp(iter->compat, compat) &&
 			try_module_get(iter->owner)) {
 			*module = iter->owner;
-			reset_fn = iter->of_reset;
+			ops = &iter->ops;
 			break;
 		}
 	}
 	mutex_unlock(&driver_lock);
-	return reset_fn;
+	return ops;
 }
 
 static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
@@ -106,7 +106,7 @@ static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
 	if (VFIO_PLATFORM_IS_ACPI(vdev))
 		return vfio_platform_acpi_has_reset(vdev);
 
-	return vdev->of_reset ? true : false;
+	return vdev->of_reset_ops ? true : false;
 }
 
 static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
@@ -114,15 +114,15 @@ static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
 	if (VFIO_PLATFORM_IS_ACPI(vdev))
 		return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT;
 
-	vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
-						    &vdev->reset_module);
-	if (!vdev->of_reset) {
+	vdev->of_reset_ops = vfio_platform_lookup_reset(vdev->compat,
+						     &vdev->reset_module);
+	if (!vdev->of_reset_ops) {
 		request_module("vfio-reset:%s", vdev->compat);
-		vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
-							&vdev->reset_module);
+		vdev->of_reset_ops = vfio_platform_lookup_reset(vdev->compat,
+								&vdev->reset_module);
 	}
 
-	return vdev->of_reset ? 0 : -ENOENT;
+	return vdev->of_reset_ops ? 0 : -ENOENT;
 }
 
 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
@@ -130,7 +130,7 @@ static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
 	if (VFIO_PLATFORM_IS_ACPI(vdev))
 		return;
 
-	if (vdev->of_reset)
+	if (vdev->of_reset_ops)
 		module_put(vdev->reset_module);
 }
 
@@ -219,9 +219,9 @@ static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
 	if (VFIO_PLATFORM_IS_ACPI(vdev)) {
 		dev_info(vdev->device, "reset\n");
 		return vfio_platform_acpi_call_reset(vdev, extra_dbg);
-	} else if (vdev->of_reset) {
+	} else if (vdev->of_reset_ops && vdev->of_reset_ops->reset) {
 		dev_info(vdev->device, "reset\n");
-		return vdev->of_reset(vdev);
+		return vdev->of_reset_ops->reset(vdev);
 	}
 
 	dev_warn(vdev->device, "no reset function found!\n");
@@ -686,13 +686,15 @@ void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
 
 void vfio_platform_unregister_reset(const char *compat,
-				    vfio_platform_reset_fn_t fn)
+				    struct vfio_platform_of_reset_ops ops)
 {
 	struct vfio_platform_reset_node *iter, *temp;
 
 	mutex_lock(&driver_lock);
 	list_for_each_entry_safe(iter, temp, &reset_list, link) {
-		if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
+		if (!strcmp(iter->compat, compat) &&
+		    !memcmp(&iter->ops, &ops,
+			    sizeof(struct vfio_platform_of_reset_ops))) {
 			list_del(&iter->link);
 			break;
 		}
diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h
index 20d67634bc41..333aefe2a1fc 100644
--- a/drivers/vfio/platform/vfio_platform_private.h
+++ b/drivers/vfio/platform/vfio_platform_private.h
@@ -65,18 +65,26 @@ struct vfio_platform_device {
 	struct resource*
 		(*get_resource)(struct vfio_platform_device *vdev, int i);
 	int	(*get_irq)(struct vfio_platform_device *vdev, int i);
-	int	(*of_reset)(struct vfio_platform_device *vdev);
 
+	const struct vfio_platform_of_reset_ops *of_reset_ops;
 	bool				reset_required;
 };
 
-typedef int (*vfio_platform_reset_fn_t)(struct vfio_platform_device *vdev);
+/**
+ * struct vfio_platform_of_reset_ops - reset ops
+ *
+ * @reset:	reset function (required)
+ */
+struct vfio_platform_of_reset_ops {
+	int (*reset)(struct vfio_platform_device *vdev);
+};
+
 
 struct vfio_platform_reset_node {
 	struct list_head link;
 	char *compat;
 	struct module *owner;
-	vfio_platform_reset_fn_t of_reset;
+	struct vfio_platform_of_reset_ops ops;
 };
 
 int vfio_platform_init_common(struct vfio_platform_device *vdev);
@@ -104,29 +112,29 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 
 void __vfio_platform_register_reset(struct vfio_platform_reset_node *n);
 void vfio_platform_unregister_reset(const char *compat,
-				    vfio_platform_reset_fn_t fn);
+				    struct vfio_platform_of_reset_ops ops);
 
 struct vfio_platform_region *
 vfio_platform_get_region(struct vfio_platform_device *vdev, const char *name);
 
-#define vfio_platform_register_reset(__compat, __reset)		\
-static struct vfio_platform_reset_node __reset ## _node = {	\
+#define vfio_platform_register_reset(__compat, __ops)		\
+static struct vfio_platform_reset_node __ops ## _node = {	\
 	.owner = THIS_MODULE,					\
 	.compat = __compat,					\
-	.of_reset = __reset,					\
+	.ops = __ops,						\
 };								\
-__vfio_platform_register_reset(&__reset ## _node)
+__vfio_platform_register_reset(&__ops ## _node)
 
-#define module_vfio_reset_handler(compat, reset)		\
+#define module_vfio_reset_handler(compat, ops)			\
 MODULE_ALIAS("vfio-reset:" compat);				\
 static int __init reset ## _module_init(void)			\
 {								\
-	vfio_platform_register_reset(compat, reset);		\
+	vfio_platform_register_reset(compat, ops);		\
 	return 0;						\
 };								\
 static void __exit reset ## _module_exit(void)			\
 {								\
-	vfio_platform_unregister_reset(compat, reset);		\
+	vfio_platform_unregister_reset(compat, ops);		\
 };								\
 module_init(reset ## _module_init);				\
 module_exit(reset ## _module_exit)
-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH v2 3/6] vfio-platform: Move region initialization to vfio_platform_init_common
  2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 1/6] vfio_platform: Introduce vfio_platform_get_region helper Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 2/6] vfio_platform: reset: Prepare for additional reset ops Eric Auger
@ 2024-09-17  9:38 ` Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 4/6] vfio_platform: reset: Introduce new init and release callbacks Eric Auger
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

We plan to introduce a new reset module init() callback. This latter
would need to have vdev->regions populated. At the moment this latter
is allocated and populated later on device open.

Call vfio_platform_regions_init() in vfio_platform_init_common()
instead.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 drivers/vfio/platform/vfio_platform_common.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c
index 976864d2e2f0..cd0f2ebff586 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -243,7 +243,6 @@ void vfio_platform_close_device(struct vfio_device *core_vdev)
 			ret, extra_dbg ? extra_dbg : "");
 	}
 	pm_runtime_put(vdev->device);
-	vfio_platform_regions_cleanup(vdev);
 	vfio_platform_irq_cleanup(vdev);
 }
 EXPORT_SYMBOL_GPL(vfio_platform_close_device);
@@ -255,13 +254,9 @@ int vfio_platform_open_device(struct vfio_device *core_vdev)
 	const char *extra_dbg = NULL;
 	int ret;
 
-	ret = vfio_platform_regions_init(vdev);
-	if (ret)
-		return ret;
-
 	ret = vfio_platform_irq_init(vdev);
 	if (ret)
-		goto err_irq;
+		return ret;
 
 	ret = pm_runtime_get_sync(vdev->device);
 	if (ret < 0)
@@ -280,8 +275,6 @@ int vfio_platform_open_device(struct vfio_device *core_vdev)
 err_rst:
 	pm_runtime_put(vdev->device);
 	vfio_platform_irq_cleanup(vdev);
-err_irq:
-	vfio_platform_regions_cleanup(vdev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(vfio_platform_open_device);
@@ -658,12 +651,17 @@ int vfio_platform_init_common(struct vfio_platform_device *vdev)
 		return ret;
 
 	vdev->device = dev;
+	ret = vfio_platform_regions_init(vdev);
+	if (ret)
+		return ret;
+
 	mutex_init(&vdev->igate);
 
 	ret = vfio_platform_get_reset(vdev);
 	if (ret && vdev->reset_required) {
 		dev_err(dev, "No reset function found for device %s\n",
 			vdev->name);
+		vfio_platform_regions_cleanup(vdev);
 		return ret;
 	}
 
@@ -674,6 +672,7 @@ EXPORT_SYMBOL_GPL(vfio_platform_init_common);
 void vfio_platform_release_common(struct vfio_platform_device *vdev)
 {
 	vfio_platform_put_reset(vdev);
+	vfio_platform_regions_cleanup(vdev);
 }
 EXPORT_SYMBOL_GPL(vfio_platform_release_common);
 
-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH v2 4/6] vfio_platform: reset: Introduce new init and release callbacks
  2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
                   ` (2 preceding siblings ...)
  2024-09-17  9:38 ` [RFC PATCH v2 3/6] vfio-platform: Move region initialization to vfio_platform_init_common Eric Auger
@ 2024-09-17  9:38 ` Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 5/6] vfio-platform: Add a new handle to store reset data Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 6/6] vfio/platform: Add tegra234-mgbe vfio platform reset module Eric Auger
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

Some devices may require resources such as clocks and resets
which cannot be handled in the vfio_platform agnostic code. Let's
add 2 new callbacks to handle those resources. Those new callbacks
are optional, as opposed to the reset callback. In case they are
implemented, both need to be.

They are not implemented by the existing reset modules.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 drivers/vfio/platform/vfio_platform_common.c  | 23 ++++++++++++++++++-
 drivers/vfio/platform/vfio_platform_private.h |  6 +++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c
index cd0f2ebff586..8d40ca452bbb 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -228,6 +228,23 @@ static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
 	return -EINVAL;
 }
 
+static void vfio_platform_reset_module_release(struct vfio_platform_device *vpdev)
+{
+	if (VFIO_PLATFORM_IS_ACPI(vpdev))
+		return;
+	if (vpdev->of_reset_ops && vpdev->of_reset_ops->release)
+		vpdev->of_reset_ops->release(vpdev);
+}
+
+static int vfio_platform_reset_module_init(struct vfio_platform_device *vpdev)
+{
+	if (VFIO_PLATFORM_IS_ACPI(vpdev))
+		return 0;
+	if (vpdev->of_reset_ops && vpdev->of_reset_ops->init)
+		return vpdev->of_reset_ops->init(vpdev);
+	return 0;
+}
+
 void vfio_platform_close_device(struct vfio_device *core_vdev)
 {
 	struct vfio_platform_device *vdev =
@@ -665,12 +682,16 @@ int vfio_platform_init_common(struct vfio_platform_device *vdev)
 		return ret;
 	}
 
-	return 0;
+	ret = vfio_platform_reset_module_init(vdev);
+	if (ret)
+		vfio_platform_put_reset(vdev);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(vfio_platform_init_common);
 
 void vfio_platform_release_common(struct vfio_platform_device *vdev)
 {
+	vfio_platform_reset_module_release(vdev);
 	vfio_platform_put_reset(vdev);
 	vfio_platform_regions_cleanup(vdev);
 }
diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h
index 333aefe2a1fc..33183addd235 100644
--- a/drivers/vfio/platform/vfio_platform_private.h
+++ b/drivers/vfio/platform/vfio_platform_private.h
@@ -74,9 +74,13 @@ struct vfio_platform_device {
  * struct vfio_platform_of_reset_ops - reset ops
  *
  * @reset:	reset function (required)
+ * @init:	Called on device attach (optional)
+ * @release:	Called on device detach (optional)
  */
 struct vfio_platform_of_reset_ops {
 	int (*reset)(struct vfio_platform_device *vdev);
+	int (*init)(struct vfio_platform_device *vdev);
+	void (*release)(struct vfio_platform_device *vdev);
 };
 
 
@@ -129,6 +133,8 @@ __vfio_platform_register_reset(&__ops ## _node)
 MODULE_ALIAS("vfio-reset:" compat);				\
 static int __init reset ## _module_init(void)			\
 {								\
+	if (!!ops.init ^ !!ops.release)				\
+		return -EINVAL;					\
 	vfio_platform_register_reset(compat, ops);		\
 	return 0;						\
 };								\
-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH v2 5/6] vfio-platform: Add a new handle to store reset data
  2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
                   ` (3 preceding siblings ...)
  2024-09-17  9:38 ` [RFC PATCH v2 4/6] vfio_platform: reset: Introduce new init and release callbacks Eric Auger
@ 2024-09-17  9:38 ` Eric Auger
  2024-09-17  9:38 ` [RFC PATCH v2 6/6] vfio/platform: Add tegra234-mgbe vfio platform reset module Eric Auger
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

Add a new field to store data used by the reset modules.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 drivers/vfio/platform/vfio_platform_private.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h
index 33183addd235..3e50d48005a2 100644
--- a/drivers/vfio/platform/vfio_platform_private.h
+++ b/drivers/vfio/platform/vfio_platform_private.h
@@ -68,6 +68,8 @@ struct vfio_platform_device {
 
 	const struct vfio_platform_of_reset_ops *of_reset_ops;
 	bool				reset_required;
+	/* This field can be used by reset driver to store some data */
+	void		*reset_opaque;
 };
 
 /**
-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH v2 6/6] vfio/platform: Add tegra234-mgbe vfio platform reset module
  2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
                   ` (4 preceding siblings ...)
  2024-09-17  9:38 ` [RFC PATCH v2 5/6] vfio-platform: Add a new handle to store reset data Eric Auger
@ 2024-09-17  9:38 ` Eric Auger
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Auger @ 2024-09-17  9:38 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, treding, vbhadram, jonathanh,
	mperttunen, linux-kernel, kvm, alex.williamson, clg,
	alexandre.torgue, joabreu
  Cc: msalter

init and release callbacks take care of resources requested by
the reset code, ie. clocks and reset.

The actual reset function toggles the mac reset, disable mac
ihterrupts, stop DMA requests and do a SW reset.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
 drivers/vfio/platform/reset/Kconfig           |   7 +
 drivers/vfio/platform/reset/Makefile          |   2 +
 .../reset/vfio_platform_tegra234_mgbe.c       | 234 ++++++++++++++++++
 3 files changed, 243 insertions(+)
 create mode 100644 drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c

diff --git a/drivers/vfio/platform/reset/Kconfig b/drivers/vfio/platform/reset/Kconfig
index dcc08dc145a5..3113fae21ebf 100644
--- a/drivers/vfio/platform/reset/Kconfig
+++ b/drivers/vfio/platform/reset/Kconfig
@@ -14,6 +14,13 @@ config VFIO_PLATFORM_AMDXGBE_RESET
 
 	  If you don't know what to do here, say N.
 
+config VFIO_PLATFORM_TEGRA234_MGBE_RESET
+	tristate "VFIO support for NVidia tegra234 MGBE reset"
+	help
+	  Enables the VFIO platform driver to handle reset for NVidia tegra234 mgbe
+
+	  If you don't know what to do here, say N.
+
 config VFIO_PLATFORM_BCMFLEXRM_RESET
 	tristate "VFIO support for Broadcom FlexRM reset"
 	depends on ARCH_BCM_IPROC || COMPILE_TEST
diff --git a/drivers/vfio/platform/reset/Makefile b/drivers/vfio/platform/reset/Makefile
index 7294c5ea122e..5ebef71f61a0 100644
--- a/drivers/vfio/platform/reset/Makefile
+++ b/drivers/vfio/platform/reset/Makefile
@@ -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-tegra234-mgbe-y := vfio_platform_tegra234_mgbe.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_TEGRA234_MGBE_RESET) += vfio-platform-tegra234-mgbe.o
 obj-$(CONFIG_VFIO_PLATFORM_BCMFLEXRM_RESET) += vfio_platform_bcmflexrm.o
diff --git a/drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c b/drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c
new file mode 100644
index 000000000000..8e889e5d04f3
--- /dev/null
+++ b/drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c
@@ -0,0 +1,234 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * VFIO platform driver specialized for NVidia tegra234-mgbe reset
+ * Code is inspired from dwxgmac2_dma.c and dwmac-tegra.c code
+ *
+ * Copyright (c) 2024 Red Hat, Inc.  All rights reserved.
+ *     Author: Eric Auger <eric.auger@redhat.com>
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/reset.h>
+#include <linux/iopoll.h>
+#include <linux/clk.h>
+
+#include "../vfio_platform_private.h"
+
+static const char *const mgbe_clks[] = {
+	"rx-pcs", "tx", "tx-pcs", "mac-divider", "mac", "mgbe", "ptp-ref", "mac"
+};
+
+struct tegra_mgbe {
+	struct clk_bulk_data *clks;
+	struct reset_control *mac_rst;
+	void __iomem *mac;
+};
+
+#define XGMAC_TX_CONFIG                 0x00000000
+#define XGMAC_CONFIG_TE                 BIT(0)
+#define XGMAC_RX_CONFIG                 0x00000004
+#define XGMAC_CONFIG_RE                 BIT(0)
+#define XGMAC_DMA_MODE			0x00003000
+#define XGMAC_SWR			BIT(0)
+
+#define XGMAC_DMA_CH_INT_EN(x)		(0x00003138 + (0x80 * (x)))
+#define XGMAC_TIE			BIT(0)
+#define XGMAC_RIE			BIT(6)
+#define XGMAC_RBUE			BIT(7)
+#define XGMAC_DMA_INT_DEFAULT_RX	(XGMAC_RBUE | XGMAC_RIE)
+#define XGMAC_DMA_INT_DEFAULT_TX	(XGMAC_TIE)
+
+#define XGMAC_DMA_CH_STATUS(x)		(0x00003160 + (0x80 * (x)))
+#define XGMAC_DMA_CH_RX_CONTROL(x)      (0x00003108 + (0x80 * (x)))
+#define XGMAC_RXST                      BIT(0)
+#define XGMAC_DMA_CH_TX_CONTROL(x)      (0x00003104 + (0x80 * (x)))
+#define XGMAC_TXST                      BIT(0)
+
+#define XGMAC_INT_STATUS                0x000000b0
+#define XGMAC_INT_EN                    0x000000b4
+
+#define MGBE_WRAP_COMMON_INTR_ENABLE 0x8704
+
+static int
+toggle_reset(struct device *dev, const char *rst_str, struct reset_control *rst)
+{
+	int ret;
+
+	ret = reset_control_assert(rst);
+	if (ret < 0)
+		dev_err(dev, "Failed to assert %s reset %d\n",
+			rst_str, ret);
+	usleep_range(2000, 4000);
+
+	ret = reset_control_deassert(rst);
+	if (ret < 0)
+		dev_err(dev, "Failed to deassert %s reset %d\n", rst_str, ret);
+	usleep_range(2000, 4000);
+	return ret;
+}
+
+static void stop_dma(void __iomem *mac, uint channel)
+{
+	u32 value;
+
+	/* DMA Stop RX */
+	value = readl(mac + XGMAC_DMA_CH_RX_CONTROL(channel));
+	value &= ~XGMAC_RXST;
+	writel(value, mac + XGMAC_DMA_CH_RX_CONTROL(channel));
+
+	value = readl(mac + XGMAC_RX_CONFIG);
+	value &= ~XGMAC_CONFIG_RE;
+	writel(value, mac + XGMAC_RX_CONFIG);
+
+	usleep_range(10, 15);
+
+	/* DMA Stop TX */
+	value = readl(mac + XGMAC_DMA_CH_TX_CONTROL(channel));
+	value &= ~XGMAC_RXST;
+	writel(value, mac + XGMAC_DMA_CH_TX_CONTROL(channel));
+
+	value = readl(mac + XGMAC_TX_CONFIG);
+	value &= ~XGMAC_CONFIG_TE;
+	writel(value, mac + XGMAC_TX_CONFIG);
+
+	usleep_range(10, 15);
+}
+
+static int dma_sw_reset(void __iomem *mac)
+{
+	u32 value;
+
+	value = readl(mac + XGMAC_DMA_MODE);
+	writel(value | XGMAC_SWR, mac + XGMAC_DMA_MODE);
+	return readl_poll_timeout(mac + XGMAC_DMA_MODE, value,
+				  !(value & XGMAC_SWR), 0, 100000);
+}
+
+static void disable_dma_irq(void __iomem *mac, u32 channel)
+{
+	u32 intr_en, intr_status;
+
+	intr_en = readl(mac + XGMAC_DMA_CH_INT_EN(channel));
+
+	intr_en &= ~XGMAC_DMA_INT_DEFAULT_RX;
+	intr_en &= ~XGMAC_DMA_INT_DEFAULT_TX;
+	writel(intr_en, mac + XGMAC_DMA_CH_INT_EN(channel));
+	usleep_range(10, 15);
+
+	intr_status = readl(mac + XGMAC_DMA_CH_STATUS(channel));
+	writel(0, mac + XGMAC_DMA_CH_STATUS(channel));
+}
+
+static int prepare_enable_clocks(struct device *dev, struct clk_bulk_data **clocks)
+{
+	struct clk_bulk_data *clks;
+	int ret;
+
+	clks = kcalloc(ARRAY_SIZE(mgbe_clks), sizeof(*clks), GFP_KERNEL);
+	if (!clks)
+		return -ENOMEM;
+
+	for (int i = 0; i <  ARRAY_SIZE(mgbe_clks); i++)
+		clks[i].id = mgbe_clks[i];
+
+	ret = devm_clk_bulk_get(dev, ARRAY_SIZE(mgbe_clks), clks);
+	if (ret < 0) {
+		dev_err(dev, "Failed to get clocks %d\n", ret);
+		return ret;
+	}
+
+	ret = clk_bulk_prepare_enable(ARRAY_SIZE(mgbe_clks), clks);
+	if (ret < 0) {
+		dev_err(dev, "Failed to prepare_enable clocks %d\n", ret);
+		return ret;
+	}
+	*clocks = clks;
+	return ret;
+}
+
+static int vfio_platform_tegra234_mgbe_init(struct vfio_platform_device *vpdev)
+{
+	struct tegra_mgbe *mgbe;
+	struct vfio_platform_region *mac_regs;
+	struct vfio_device *vdev = &vpdev->vdev;
+	struct device *dev = vdev->dev;
+	int ret = 0;
+
+	mac_regs = vfio_platform_get_region(vpdev, "mac");
+	if (!mac_regs)
+		return -EINVAL;
+
+	mgbe = devm_kmalloc(dev, sizeof(struct tegra_mgbe), GFP_KERNEL);
+	if (!mgbe)
+		return -ENOMEM;
+
+	ret = prepare_enable_clocks(dev, &mgbe->clks);
+	if (ret)
+		return ret;
+
+	mgbe->mac_rst = devm_reset_control_get(dev, "mac");
+	if (IS_ERR(mgbe->mac_rst)) {
+		dev_err(dev, "Failed to get mac reset %ld\n", PTR_ERR(mgbe->mac_rst));
+		ret = PTR_ERR(mgbe->mac_rst);
+		return ret;
+	}
+
+	mac_regs->ioaddr = ioremap(mac_regs->addr, mac_regs->size);
+	if (!mac_regs->ioaddr)
+		return -ENOMEM;
+
+	mgbe->mac = mac_regs->ioaddr;
+	vpdev->reset_opaque = mgbe;
+	return ret;
+}
+
+static void vfio_platform_tegra234_mgbe_release(struct vfio_platform_device *vpdev)
+{
+	struct tegra_mgbe *mgbe = vpdev->reset_opaque;
+
+	/* iounmap is done in vfio_platform_common */
+	clk_bulk_disable_unprepare(ARRAY_SIZE(mgbe_clks), mgbe->clks);
+	vpdev->reset_opaque = NULL;
+}
+
+static int vfio_platform_tegra234_mgbe_reset(struct vfio_platform_device *vpdev)
+{
+	struct tegra_mgbe *mgbe = vpdev->reset_opaque;
+	struct vfio_device *vdev = &vpdev->vdev;
+	struct device *dev = vdev->dev;
+	int ret;
+
+	if (!mgbe)
+		return -ENODEV;
+
+	toggle_reset(dev, "mac", mgbe->mac_rst);
+
+	for (int i = 0; i < 10; i++)
+		disable_dma_irq(mgbe->mac, i);
+
+	writel(0, mgbe->mac + MGBE_WRAP_COMMON_INTR_ENABLE);
+
+	for (int i = 0; i < 10; i++)
+		stop_dma(mgbe->mac, i);
+
+	ret = dma_sw_reset(mgbe->mac);
+	if (ret)
+		dev_err(dev, "Failed to reset the DMA %d\n", ret);
+
+	return ret;
+}
+
+static const struct vfio_platform_of_reset_ops
+vfio_platform_tegra234_mgbe_of_reset_ops = {
+	.reset = vfio_platform_tegra234_mgbe_reset,
+	.init = vfio_platform_tegra234_mgbe_init,
+	.release = vfio_platform_tegra234_mgbe_release,
+};
+
+module_vfio_reset_handler("nvidia,tegra234-mgbe", vfio_platform_tegra234_mgbe_of_reset_ops);
+
+MODULE_VERSION("0.1");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Eric Auger <eric.auger@redhat.com>");
+MODULE_DESCRIPTION("Reset support for NVidia tegra234 mgbe vfio platform device");
-- 
2.41.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-09-17  9:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-17  9:38 [RFC PATCH v2 0/6] vfio: platform: reset: Introduce tegra234 mgbe reset module Eric Auger
2024-09-17  9:38 ` [RFC PATCH v2 1/6] vfio_platform: Introduce vfio_platform_get_region helper Eric Auger
2024-09-17  9:38 ` [RFC PATCH v2 2/6] vfio_platform: reset: Prepare for additional reset ops Eric Auger
2024-09-17  9:38 ` [RFC PATCH v2 3/6] vfio-platform: Move region initialization to vfio_platform_init_common Eric Auger
2024-09-17  9:38 ` [RFC PATCH v2 4/6] vfio_platform: reset: Introduce new init and release callbacks Eric Auger
2024-09-17  9:38 ` [RFC PATCH v2 5/6] vfio-platform: Add a new handle to store reset data Eric Auger
2024-09-17  9:38 ` [RFC PATCH v2 6/6] vfio/platform: Add tegra234-mgbe vfio platform reset module Eric Auger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®