Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp200771lqp; Wed, 20 Mar 2024 19:52:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWKAhkG4qlcXzHuN80wGW7pPQb2IpPpKYTtpGyLBpUPbySiekCXIO/wjopnlcMP/oZmWNSryJyX7e8zUsBeRDlQtMdiKfC7iT02CeLI4g== X-Google-Smtp-Source: AGHT+IESld0EpoYYQvJEiq1zUzrZoI3R7wFglYHb3HtB54MDXxrg78ptjui1EYP6e2OY0iSlCThF X-Received: by 2002:a17:907:9944:b0:a46:1e:d199 with SMTP id kl4-20020a170907994400b00a46001ed199mr4800436ejc.39.1710989554109; Wed, 20 Mar 2024 19:52:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710989554; cv=pass; d=google.com; s=arc-20160816; b=bE76cm+4IdXp3S0AxJPLJqEBNggr1u1uoDbjJquyWsYBKzya1IH02GWUakCDm2K5IQ XOLhi3rmxLJ7z3fj4PI6a5jr1acVPlybwKUGjLghhS8Onp53LtQ4P634Vdwt5wZcHkYq d/ju4/154Nk/T8dJEny3XCIJyPrk06H+7O1MDoyOjxRquRC0gU+l2pO3LvGvMoQbH6HW aMCaxbLBqnxY9+QcuflEGTBAyFwLM3dcTkMILFqK8Nmdycn0AmAnHOGqF/8dTfo5ITLH pPbpOcecwc4VvjF9jMTXIaroHw0YCidG7rzsrYvL/OXyUll/o+gV0P7xOiBslHlg40ue NiEw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=w7lsFF7Jt7Wx4QuN10hvG9q2YtU3jwzHcLdTM8DXudE=; fh=eulCrYuY4zK7Gxr6B+4yAHyThWGi9/x0Te9WMho9aNk=; b=iqSObOnbDj9NZv47uLtNS1iAUMysX8GPOnePAQtikN+dmxf2E/YALpgMh015VKiyi6 NQYSo3/0gP8XkIFqv8AlWOCRt7zvKTeUsm8I8YTsFgMcAOhn/HjMHfd6Xc3lfiMjM6RU toZwMJWZ/YLFnKlJwwxaSzK5/RgtVMtAMdmhpzi7MnxPYlmfDcM8BGVijsm3iQiIdsJe AgjpZqgTxzrPgI4tnWCq2Tss4ZMiYlCxuNgqqWNe+8tbKKssWouc+EtKcSQh1rAtkQQx uRSIEpTpv4A/WHdxv2eHp3XPEsWE5lPkmMeCweJXq2Jk2hwi+0vPUquJFHKXhYDC8iDL k8aA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="kaowWn/i"; arc=pass (i=1 spf=pass spfdomain=amazon.co.uk dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-109605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id he15-20020a1709073d8f00b00a46ee3c4a03si1514693ejc.988.2024.03.20.19.52.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 19:52:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-109605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="kaowWn/i"; arc=pass (i=1 spf=pass spfdomain=amazon.co.uk dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-109605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 970FD1F21784 for ; Thu, 21 Mar 2024 02:52:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DFFFA79CF; Thu, 21 Mar 2024 02:51:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="kaowWn/i" Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44D897462; Thu, 21 Mar 2024 02:51:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.171.190.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710989507; cv=none; b=sWDFxshraz8PJC72DkI5/863GnDmMVPPxoJaqnkCUq8bXXnuHiQ/YraYmRRgsbSHWYQJhKG+vY6fz2WTLgC5lRbmP5BKRxKjzmutrMfYIN5uRtHUQZ+CF3cmeS2LzErW0u6en4Yd38/9CawJdxVzpVgcAJVTHyyi8aCgrytRhpI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710989507; c=relaxed/simple; bh=Ne9uSAiMJaLyo2UxKCXKDjH6mIO6I4ya4u4Kl+S6IOg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=F9huebblMhub1Hg2inry5hZ/ROJTGc/9CJiJvzSMlWpggP0JqSUJH6OrLxI0nY57ReE+Gw5HLzDiOxhHBDoQk5ukklgIqGeDwgX0HeKMlj87NPPSdl2zdAc7gT7piLiBm62RAh6gEuzfDtYmYSy54FvQri2Gfg8Se2g9JYxeInM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=kaowWn/i; arc=none smtp.client-ip=207.171.190.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1710989507; x=1742525507; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=w7lsFF7Jt7Wx4QuN10hvG9q2YtU3jwzHcLdTM8DXudE=; b=kaowWn/iq2QpxcsuPiuvOBwjlB1KO28ODAnz+3jOlhgoGnZFd1esyOa3 6fzZDvVrCAKuyMB+KFwK0q4kltOBh23I+FCQQFVbwjFJVbpg3Z5cxe5vs JvcZVrrZwLQZeMpQ2fe+ZOwE/5dPvkBUCBkrVRhpF0i+yzQUc+oB76xMF w=; X-IronPort-AV: E=Sophos;i="6.07,141,1708387200"; d="scan'208";a="334143291" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2024 02:51:39 +0000 Received: from EX19MTAEUC001.ant.amazon.com [10.0.17.79:18408] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.26.123:2525] with esmtp (Farcaster) id 927bfbc6-a8a5-4bb7-a8da-9035a6125689; Thu, 21 Mar 2024 02:51:37 +0000 (UTC) X-Farcaster-Flow-ID: 927bfbc6-a8a5-4bb7-a8da-9035a6125689 Received: from EX19D036EUC002.ant.amazon.com (10.252.61.191) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Thu, 21 Mar 2024 02:51:37 +0000 Received: from bcd074994f7f.amazon.com (10.106.82.23) by EX19D036EUC002.ant.amazon.com (10.252.61.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Thu, 21 Mar 2024 02:51:32 +0000 From: Sudan Landge To: , , , , , , , , , , CC: , , , Subject: [PATCH v2 4/4] virt: vmgenid: add support for devicetree bindings Date: Thu, 21 Mar 2024 02:51:05 +0000 Message-ID: <20240321025105.53210-5-sudanl@amazon.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240321025105.53210-1-sudanl@amazon.com> References: <20240321025105.53210-1-sudanl@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D043UWA002.ant.amazon.com (10.13.139.53) To EX19D036EUC002.ant.amazon.com (10.252.61.191) - 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/rng/vmgenid.yaml - Use proper FLAGS to compile with and without ACPI and/or devicetree. Signed-off-by: Sudan Landge --- drivers/virt/Kconfig | 2 +- drivers/virt/vmgenid.c | 106 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 101 insertions(+), 7 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 d5394c706bd9..ec378c37a2a2 100644 --- a/drivers/virt/vmgenid.c +++ b/drivers/virt/vmgenid.c @@ -2,7 +2,7 @@ /* * Copyright (C) 2022 Jason A. Donenfeld . 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. */ @@ -13,14 +13,27 @@ #include #include #include - +#ifdef CONFIG_OF +#include +#include +#include +#include +#include +#include +#endif + +#ifdef CONFIG_ACPI ACPI_MODULE_NAME("vmgenid"); +#endif enum { VMGENID_SIZE = 16 }; struct vmgenid_state { u8 *next_id; u8 this_id[VMGENID_SIZE]; +#ifdef CONFIG_OF + unsigned int irq; +#endif }; static void vmgenid_notify(struct device *device) @@ -37,10 +50,24 @@ static void vmgenid_notify(struct device *device) kobject_uevent_env(&device->kobj, KOBJ_CHANGE, envp); } +#ifdef CONFIG_ACPI static void vmgenid_acpi_handler(acpi_handle handle, u32 event, void *dev) { + (void)handle; + (void)event; vmgenid_notify(dev); } +#endif + +#ifdef CONFIG_OF +static irqreturn_t vmgenid_of_irq_handler(int irq, void *dev) +{ + (void)irq; + vmgenid_notify(dev); + + return IRQ_HANDLED; +} +#endif static int setup_vmgenid_state(struct vmgenid_state *state, u8 *next_id) { @@ -55,6 +82,7 @@ static int setup_vmgenid_state(struct vmgenid_state *state, u8 *next_id) static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state) { +#ifdef CONFIG_ACPI struct acpi_device *device = ACPI_COMPANION(dev); struct acpi_buffer parsed = { ACPI_ALLOCATE_BUFFER }; union acpi_object *obj; @@ -96,6 +124,54 @@ static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state) out: ACPI_FREE(parsed.pointer); return ret; +#else + (void)dev; + (void)state; + return -EINVAL; +#endif +} + +static int vmgenid_add_of(struct device *dev, struct vmgenid_state *state) +{ +#ifdef CONFIG_OF + 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, (u8 *)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; +#else + (void)dev; + (void)state; + return -EINVAL; +#endif } static int vmgenid_add(struct platform_device *pdev) @@ -108,7 +184,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,18 +195,33 @@ static int vmgenid_add(struct platform_device *pdev) return ret; } -static const struct acpi_device_id vmgenid_ids[] = { +#ifdef CONFIG_OF +static const struct of_device_id vmgenid_of_ids[] = { + { .compatible = "linux,vmgenctr", }, + {}, +}; +MODULE_DEVICE_TABLE(of, vmgenid_of_ids); +#endif + +#ifdef CONFIG_ACPI +static const struct acpi_device_id vmgenid_acpi_ids[] = { { "VMGENCTR", 0 }, { "VM_GEN_COUNTER", 0 }, { } }; -MODULE_DEVICE_TABLE(acpi, vmgenid_ids); +MODULE_DEVICE_TABLE(acpi, vmgenid_acpi_ids); +#endif static struct platform_driver vmgenid_plaform_driver = { .probe = vmgenid_add, .driver = { .name = "vmgenid", - .acpi_match_table = ACPI_PTR(vmgenid_ids), +#ifdef CONFIG_ACPI + .acpi_match_table = ACPI_PTR(vmgenid_acpi_ids), +#endif +#ifdef CONFIG_OF + .of_match_table = vmgenid_of_ids, +#endif .owner = THIS_MODULE, }, }; -- 2.40.1