All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sudan Landge <sudanl@amazon.com>
To: <tytso@mit.edu>, <Jason@zx2c4.com>, <robh+dt@kernel.org>,
	<krzysztof.kozlowski+dt@linaro.org>, <conor+dt@kernel.org>,
	<sudanl@amazon.com>, <sathyanarayanan.kuppuswamy@linux.intel.com>,
	<thomas.lendacky@amd.com>, <dan.j.williams@intel.com>,
	<devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <graf@amazon.de>, <dwmw@amazon.co.uk>, <bchalios@amazon.es>,
	<xmarcalx@amazon.co.uk>
Subject: [PATCH v1 4/4] virt: vmgenid: add support for devicetree bindings
Date: Tue, 19 Mar 2024 14:32:53 +0000	[thread overview]
Message-ID: <20240319143253.22317-5-sudanl@amazon.com> (raw)
In-Reply-To: <20240319143253.22317-1-sudanl@amazon.com>

Extend the vmgenid platform driver to support devicetree bindings.
With this support, hypervisors can send vmgenid notifications to
the virtual machine without the need to enable ACPI.

The bindings are located at:
- Documentation/devicetree/bindings/vmgenid/vmgenid.yaml

Signed-off-by: Sudan Landge <sudanl@amazon.com>
---
 drivers/virt/Kconfig   |  2 +-
 drivers/virt/vmgenid.c | 90 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 86 insertions(+), 6 deletions(-)

diff --git a/drivers/virt/Kconfig b/drivers/virt/Kconfig
index 40129b6f0eca..4f33ee2f0372 100644
--- a/drivers/virt/Kconfig
+++ b/drivers/virt/Kconfig
@@ -16,7 +16,7 @@ if VIRT_DRIVERS
 config VMGENID
 	tristate "Virtual Machine Generation ID driver"
 	default y
-	depends on ACPI
+	depends on (ACPI || OF)
 	help
 	  Say Y here to use the hypervisor-provided Virtual Machine Generation ID
 	  to reseed the RNG when the VM is cloned. This is highly recommended if
diff --git a/drivers/virt/vmgenid.c b/drivers/virt/vmgenid.c
index e82b344a9d61..96a3ff8ec05a 100644
--- a/drivers/virt/vmgenid.c
+++ b/drivers/virt/vmgenid.c
@@ -2,7 +2,7 @@
 /*
  * Copyright (C) 2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  *
- * The "Virtual Machine Generation ID" is exposed via ACPI and changes when a
+ * The "Virtual Machine Generation ID" is exposed via ACPI or DT and changes when a
  * virtual machine forks or is cloned. This driver exists for shepherding that
  * information to random.c.
  */
@@ -12,6 +12,12 @@
 #include <linux/acpi.h>
 #include <linux/random.h>
 #include <acpi/actypes.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
 #include <linux/platform_device.h>
 
 ACPI_MODULE_NAME("vmgenid");
@@ -21,6 +27,7 @@ enum { VMGENID_SIZE = 16 };
 struct vmgenid_state {
 	u8 *next_id;
 	u8 this_id[VMGENID_SIZE];
+	unsigned int irq;
 };
 
 static void vmgenid_notify(struct device *device)
@@ -42,6 +49,13 @@ static void vmgenid_acpi_handler(acpi_handle handle, u32 event, void *dev)
 	vmgenid_notify(dev);
 }
 
+static irqreturn_t vmgenid_of_irq_handler(int irq, void *dev)
+{
+	vmgenid_notify(dev);
+
+	return IRQ_HANDLED;
+}
+
 static int setup_vmgenid_state(struct vmgenid_state *state, u8 *next_id)
 {
 	if (IS_ERR(next_id))
@@ -98,6 +112,43 @@ static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state)
 	return ret;
 }
 
+static int vmgenid_add_of(struct device *dev, struct vmgenid_state *state)
+{
+	struct resource res;
+	int ret = 0;
+
+	if (of_address_to_resource(dev->of_node, 0, &res)) {
+		dev_err(dev, "Failed to get resources from device tree");
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (!__request_mem_region(res.start, resource_size(&res),
+				  "vmgenid", IORESOURCE_EXCLUSIVE)) {
+		dev_err(dev, "Failed to request mem region");
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = setup_vmgenid_state(state, of_iomap(dev->of_node, 0));
+	if (ret)
+		goto out;
+
+	state->irq = irq_of_parse_and_map(dev->of_node, 0);
+	dev->driver_data = state;
+
+	if (request_irq(state->irq, vmgenid_of_irq_handler,
+			IRQF_SHARED, "vmgenid", dev) < 0) {
+		dev_err(dev, "request_irq failed");
+		dev->driver_data = NULL;
+		ret = -EINVAL;
+		goto out;
+	}
+
+out:
+	return ret;
+}
+
 static int vmgenid_add(struct platform_device *pdev)
 {
 	struct vmgenid_state *state;
@@ -108,7 +159,10 @@ static int vmgenid_add(struct platform_device *pdev)
 	if (!state)
 		return -ENOMEM;
 
-	ret = vmgenid_add_acpi(dev, state);
+	if (dev->of_node)
+		ret = vmgenid_add_of(dev, state);
+	else
+		ret = vmgenid_add_acpi(dev, state);
 
 	if (ret)
 		devm_kfree(dev, state);
@@ -116,7 +170,30 @@ static int vmgenid_add(struct platform_device *pdev)
 	return ret;
 }
 
-static const struct acpi_device_id vmgenid_ids[] = {
+static int vmgenid_remove(struct platform_device *pdev)
+{
+	struct vmgenid_state *state = NULL;
+
+	if (!pdev)
+		return -EINVAL;
+
+	state = pdev->dev.driver_data;
+
+	if (state && pdev->dev.of_node)
+		free_irq(state->irq, &pdev->dev);
+
+	if (state)
+		devm_kfree(&pdev->dev, state);
+
+	return 0;
+}
+
+static const struct of_device_id vmgenid_of_ids[] = {
+	{ .compatible = "linux,vmgenctr", },
+	{},
+};
+
+static const struct acpi_device_id vmgenid_acpi_ids[] = {
 	{ "VMGENCTR", 0 },
 	{ "VM_GEN_COUNTER", 0 },
 	{ }
@@ -124,9 +201,11 @@ static const struct acpi_device_id vmgenid_ids[] = {
 
 static struct platform_driver vmgenid_plaform_driver = {
 	.probe      = vmgenid_add,
+	.remove     = vmgenid_remove,
 	.driver     = {
 		.name   = "vmgenid",
-		.acpi_match_table = ACPI_PTR(vmgenid_ids),
+		.acpi_match_table = ACPI_PTR(vmgenid_acpi_ids),
+		.of_match_table = vmgenid_of_ids,
 		.owner = THIS_MODULE,
 	},
 };
@@ -144,7 +223,8 @@ static void vmgenid_platform_device_exit(void)
 module_init(vmgenid_platform_device_init)
 module_exit(vmgenid_platform_device_exit)
 
-MODULE_DEVICE_TABLE(acpi, vmgenid_ids);
+MODULE_DEVICE_TABLE(acpi, vmgenid_acpi_ids);
+MODULE_DEVICE_TABLE(of, vmgenid_of_ids);
 MODULE_DESCRIPTION("Virtual Machine Generation ID");
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
-- 
2.40.1



  parent reply	other threads:[~2024-03-19 14:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-19 14:32 [PATCH v1 0/4] virt: vmgenid: Add devicetree bindings support Sudan Landge
2024-03-19 14:32 ` [PATCH v1 1/4] virt: vmgenid: rearrange code to make review easier Sudan Landge
2024-03-19 14:32 ` [PATCH v1 2/4] virt: vmgenid: change implementation to use a platform driver Sudan Landge
2024-03-19 14:32 ` [PATCH v1 3/4] dt-bindings: Add bindings for vmgenid Sudan Landge
2024-03-19 15:28   ` Krzysztof Kozlowski
     [not found]     ` <f221da06-2a7c-4db3-a0de-870156865631@amazon.co.uk>
2024-03-20 10:24       ` Krzysztof Kozlowski
2024-03-20 12:16         ` Landge, Sudan
2024-03-19 14:32 ` Sudan Landge [this message]
2024-03-19 15:30   ` [PATCH v1 4/4] virt: vmgenid: add support for devicetree bindings Krzysztof Kozlowski
2024-03-20  8:14   ` kernel test robot
2024-03-20 13:35   ` kernel test robot
2024-03-20 16:54   ` kernel test robot
2024-03-21  1:10   ` kernel test robot
2024-03-19 15:24 ` [PATCH v1 0/4] virt: vmgenid: Add devicetree bindings support Krzysztof Kozlowski
2024-03-20 13:50   ` David Woodhouse
2024-03-20 16:15     ` Rob Herring
2024-03-20 16:55       ` David Woodhouse
2024-03-21 13:32         ` Rob Herring
2024-03-21 17:39           ` Landge, Sudan
2024-03-22  5:40             ` Krzysztof Kozlowski
2024-03-22  8:21               ` David Woodhouse
2024-03-22 13:22                 ` Rob Herring
2024-03-22 14:27                   ` David Woodhouse
2024-03-22 16:39                     ` Landge, Sudan
2024-03-19 15:32 ` Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240319143253.22317-5-sudanl@amazon.com \
    --to=sudanl@amazon.com \
    --cc=Jason@zx2c4.com \
    --cc=bchalios@amazon.es \
    --cc=conor+dt@kernel.org \
    --cc=dan.j.williams@intel.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dwmw@amazon.co.uk \
    --cc=graf@amazon.de \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=sathyanarayanan.kuppuswamy@linux.intel.com \
    --cc=thomas.lendacky@amd.com \
    --cc=tytso@mit.edu \
    --cc=xmarcalx@amazon.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.