From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4DE87C27C75 for ; Wed, 12 Jun 2024 10:40:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ACWLaJj/sO7FE/x9hMeKNNuOBgDiWPG8HyrO4oVecec=; b=PuJJbPnOJTE71jTi5LaQcKqLbh dAV8kHsrBHvY9tn3D9PhSTiSP4GxTaCuvwwmDbHJWrBQNnYMfmIw0/Pgpra/N4oSQYYrGwVjgDU2F xx45w4BGACex0P5Ck2BgSZT4VEZvN9jhaDqg3xtZ5KQc8ESvXuhbVj5Wz5Afrwqib8ViJRVddpISg TjMTVgeA1YB2tyE3RRLNixzIbW4SZNpZ7p/YtFe+duXUET+eB6LmroV5L3nbzeNaiwWmXpq1tZ7HW HfCmwZTsrJoZMwIEq4ZDH77mE6pC7880qr/LomHKtSdODfvmIkXVIzrfum+VFD4JdqVtmYy+aDz+G uG28ClNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sHLOo-0000000CHH2-31uo; Wed, 12 Jun 2024 10:40:14 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sHLOl-0000000CHDo-1k9B for linux-arm-kernel@lists.infradead.org; Wed, 12 Jun 2024 10:40:12 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-35f1c209842so3199117f8f.3 for ; Wed, 12 Jun 2024 03:40:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718188808; x=1718793608; darn=lists.infradead.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ACWLaJj/sO7FE/x9hMeKNNuOBgDiWPG8HyrO4oVecec=; b=lj5Dvlb/ubFsd4h9UFfYWA9ppUazfrt+dft6KTMF1aBqtA8lJru5LCqdNquDq3fFVD SZx6ObtFvIFRZddbhUJNZt8DlDlMmHpNxTfEVFB8u1ikagMJxWiXNhvf8TMS8vwXFgGj Y2iOF6/2EoiI3DjtuxiZmNGLhXU3vBMbfq+iQjqK21ucyNu+A/bY4i7xGBP5gpTYVBmJ Xp1AJd3L5JfSDycnR5J+r8WUN47GCrtKa7Knbtup6cbA9IRhy1rdtSkTW1h6XnjDzCee YqhBWg0dg04S6JsLrjMJwBCJ6W87U/OM/22GQ/CYnAJQP7eB5BReeUfh/yxxF0iuwUU1 4BQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718188808; x=1718793608; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ACWLaJj/sO7FE/x9hMeKNNuOBgDiWPG8HyrO4oVecec=; b=EhgOI6EUrUWUgjyqAKNNHRsblDVZTw/45uWN0W28IzZ1sGEpPHz58szyK9JsYWPIyn GdvZQIaCPEuzT5z5ug17VyCsFojYdKCX64aqGjDyWa25RqD7tMSXUGyAiXonRxbHDQLF xqX+cP4Yy6wqwlbsPFRBhWgQqyGrcXo6KPANzM7AYf5FUUsfYThux1z+bUdNnUKshlt5 nZD6slyEdu9PdXkvhPbMwn7CVRzFZeMuEJ2tiVcow2H+DJYfQnqYE/OCLghSe6PB7XmV E3mtfOaiVG6ZS0m/qt9g0FuuHUiQejgkDDG1EIrPC91Ykrifvn69y2N77DiIiKLcoGSu Js2g== X-Forwarded-Encrypted: i=1; AJvYcCUh2NIQR03ja7doqS+rguVXSJ+Bz4G+9TScQnDELoRNA57OuaNNR7RBgv6UyKFJjz1kqchhMku+LzwIhXtcywPBmJav1mBWuVliZ4AG0ATUAPikKoQ= X-Gm-Message-State: AOJu0Yw+dWvpVX1hbmOye5H+yTmdnf6WBTmub3IEbSSSfxaHLumnzXKr xdQfAWbbcILwbVeNfNK8In5jADFUD3y4xCyMb8DgXo2F3hSa8lT4Y9eXmAQnZSQ= X-Google-Smtp-Source: AGHT+IFajuOTDnrq2pHCCq72kII5joVpB6r6hgB9X0A1ac5/NT6lJbaYlvsP7mzlklqXw70Hx30DVg== X-Received: by 2002:a5d:47a9:0:b0:35f:1f49:f784 with SMTP id ffacd0b85a97d-35fdf779c98mr953464f8f.12.1718188807568; Wed, 12 Jun 2024 03:40:07 -0700 (PDT) Received: from myrica ([2.221.137.100]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f1aa8d4f3sm10874877f8f.99.2024.06.12.03.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 03:40:06 -0700 (PDT) Date: Wed, 12 Jun 2024 11:40:23 +0100 From: Jean-Philippe Brucker To: Steven Price Cc: kvm@vger.kernel.org, kvmarm@lists.linux.dev, Suzuki K Poulose , Catalin Marinas , Marc Zyngier , Will Deacon , James Morse , Oliver Upton , Zenghui Yu , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Joey Gouly , Alexandru Elisei , Christoffer Dall , Fuad Tabba , linux-coco@lists.linux.dev, Ganapatrao Kulkarni Subject: Re: [PATCH v3 02/14] arm64: Detect if in a realm and set RIPAS RAM Message-ID: <20240612104023.GB4602@myrica> References: <20240605093006.145492-1-steven.price@arm.com> <20240605093006.145492-3-steven.price@arm.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="/N5098jZq9Eqa4T2" Content-Disposition: inline In-Reply-To: <20240605093006.145492-3-steven.price@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240612_034011_525866_BA036D31 X-CRM114-Status: GOOD ( 33.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org --/N5098jZq9Eqa4T2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Jun 05, 2024 at 10:29:54AM +0100, Steven Price wrote: > From: Suzuki K Poulose > > Detect that the VM is a realm guest by the presence of the RSI > interface. > > If in a realm then all memory needs to be marked as RIPAS RAM initially, > the loader may or may not have done this for us. To be sure iterate over > all RAM and mark it as such. Any failure is fatal as that implies the > RAM regions passed to Linux are incorrect - which would mean failing > later when attempting to access non-existent RAM. > > Signed-off-by: Suzuki K Poulose > Co-developed-by: Steven Price > Signed-off-by: Steven Price > +static bool rsi_version_matches(void) > +{ > + unsigned long ver_lower, ver_higher; > + unsigned long ret = rsi_request_version(RSI_ABI_VERSION, > + &ver_lower, > + &ver_higher); There is a regression on QEMU TCG (in emulation mode, not running under KVM): qemu-system-aarch64 -M virt -cpu max -kernel Image -nographic This doesn't implement EL3 or EL2, so SMC is UNDEFINED (DDI0487J.a R_HMXQS), and we end up with an undef instruction exception. So this patch would also break hardware that only implements EL1 (I don't know if it exists). The easiest fix is to detect the SMC conduit through the PSCI node in DT. SMCCC helpers already do this, but we can't use them this early in the boot. I tested adding an early probe to the PSCI driver to check this, see attached patches. Note that we do need to test the conduit after finding a PSCI node, because even though it doesn't implement EL2 in this configuration, QEMU still accepts PSCI HVCs in order to support SMP. Thanks, Jean --/N5098jZq9Eqa4T2 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-firmware-psci-Add-psci_early_test_conduit.patch" >From 788bfd45e7ce521666a19dba99277106e4d33c80 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Brucker Date: Tue, 11 Jun 2024 19:15:30 +0100 Subject: [PATCH 1/2] firmware/psci: Add psci_early_test_conduit() Add a function to test early if PSCI is present and what conduit it uses. Because the PSCI conduit corresponds to the SMCCC one, this will let the kernel know whether it can use SMC instructions to discuss with the Realm Management Monitor (RMM), early enough to enable RAM and serial access when running in a Realm. Signed-off-by: Jean-Philippe Brucker --- include/linux/psci.h | 5 +++++ drivers/firmware/psci/psci.c | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/linux/psci.h b/include/linux/psci.h index 4ca0060a3fc4..a1fc1703ba20 100644 --- a/include/linux/psci.h +++ b/include/linux/psci.h @@ -45,8 +45,13 @@ struct psci_0_1_function_ids get_psci_0_1_function_ids(void); #if defined(CONFIG_ARM_PSCI_FW) int __init psci_dt_init(void); +bool __init psci_early_test_conduit(enum arm_smccc_conduit conduit); #else static inline int psci_dt_init(void) { return 0; } +static inline bool psci_early_test_conduit(enum arm_smccc_conduit conduit) +{ + return false; +} #endif #if defined(CONFIG_ARM_PSCI_FW) && defined(CONFIG_ACPI) diff --git a/drivers/firmware/psci/psci.c b/drivers/firmware/psci/psci.c index d9629ff87861..a40dcaf17822 100644 --- a/drivers/firmware/psci/psci.c +++ b/drivers/firmware/psci/psci.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -767,6 +768,30 @@ int __init psci_dt_init(void) return ret; } +/* + * Test early if PSCI is supported, and if its conduit matches @conduit + */ +bool __init psci_early_test_conduit(enum arm_smccc_conduit conduit) +{ + int len; + int psci_node; + const char *method; + unsigned long dt_root; + + /* DT hasn't been unflattened yet, we have to work with the flat blob */ + dt_root = of_get_flat_dt_root(); + psci_node = of_get_flat_dt_subnode_by_name(dt_root, "psci"); + if (psci_node <= 0) + return false; + + method = of_get_flat_dt_prop(psci_node, "method", &len); + if (!method) + return false; + + return (conduit == SMCCC_CONDUIT_SMC && strncmp(method, "smc", len) == 0) || + (conduit == SMCCC_CONDUIT_HVC && strncmp(method, "hvc", len) == 0); +} + #ifdef CONFIG_ACPI /* * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's -- 2.45.2 --/N5098jZq9Eqa4T2 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0002-squash-arm64-Detect-if-in-a-realm-and-set-RIPAS-RAM.patch" >From fcb16e1eb494d2ce21792495955d6d3f26c319c9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Brucker Date: Wed, 12 Jun 2024 09:08:17 +0100 Subject: [PATCH 2/2] squash! arm64: Detect if in a realm and set RIPAS RAM Before issuing an SMC, detect whether SMCCC is available through this conduit. On platforms that do not implement EL3 nor EL2, the SMC instruction is UNDEFINED. SMCCC advises probing the SMCCC availability by first looking for a PSCI node in DT (or in ACPI, but we expect a realm to boot with DT at the moment). Since RMM requires using the SMC conduit for both PSCI and RSI, we rely on the PSCI method property to ensure SMC is available. We could also check that the SMCCC version is at least 1.2 as required by RMM, but no platform requires this extra step at the moment, and we can't use the SMCCC helpers this early in boot. Signed-off-by: Jean-Philippe Brucker --- arch/arm64/kernel/rsi.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/arm64/kernel/rsi.c b/arch/arm64/kernel/rsi.c index 898952d135b0..21fc261a1d26 100644 --- a/arch/arm64/kernel/rsi.c +++ b/arch/arm64/kernel/rsi.c @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -82,6 +83,12 @@ void __init arm64_rsi_setup_memory(void) void __init arm64_rsi_init(void) { + /* + * If PSCI isn't using SMC, RMM isn't present. Don't try to execute an + * SMC as it could be UNDEFINED. + */ + if (!psci_early_test_conduit(SMCCC_CONDUIT_SMC)) + return; if (!rsi_version_matches()) return; if (rsi_get_realm_config(&config)) -- 2.45.2 --/N5098jZq9Eqa4T2--