From patchwork Wed Feb 1 12:53:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13124421 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 CEE26C05027 for ; Wed, 1 Feb 2023 14:31:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=od9Q6HLdw4iAmJurfJUmFcwupMiQyWLMyVuDP2nnM2A=; b=O2JPSQRsNOS/JH P1SsmSr+NkEyHYnDVYhrOD2eTiu883Nt99Lzu1rOOBpHbbkfcvK0NKJV1DuUoYQnCVWxH0LbwhFK+ qzA10u0E7Wx5InNKGLQxhStvEDb5Fsxwe1L/OChAX+PN+QJRW4Mt7VJzh8RO968WtcTF+7042NI6Q qd/xFor6gQT0gR+SVjlRpLY6O8WSDiWMseNnI0XDHgNII98TEj41S/lMXRKazcKyjH9MvsjF76Wsm khBoRhzPn9pe5FtvJMKByjanjP6HAEqNSZbyOahfJNKjIDXJ7oOk7jFNFZJJCzVwq8psuhgM6NRZ+ U1CL1wxyvvnTZLqZOSAQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNE8K-00CN1d-4o; Wed, 01 Feb 2023 14:30:44 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNDhn-00CBpS-LB for linux-arm-kernel@bombadil.infradead.org; Wed, 01 Feb 2023 14:03:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=Hw6sl7oFDdNkKrpSvS4DEk1bWbFMgLKs0ATS0rkQZn8=; b=KuzEIpqeXSgGqanL/tXkVC7pYl PtzaJENdEhp8WNA+vq4sCHhE08TkmmHZebJzTlJ1P3pKt+JHkIo1Z2oeLQ+3JdStsWYUL3/oN65yC COueRzwJyZceLM0Yj4SfZSFTtyC2NuF5wZgjY8EiH9Dzz52qooZ2wPlkFTUeg0uHDcNaFW3eVg6Xj zh2MjEWnfjh8/iqnh2DhxpAmm0TdSaiCJCsBo/uO7xnJ8c65CmwZJAr34Wa0HezzAoKsCgCd+4EKq bQSDwyXBl1O67wI9CtpUj8NKoekl9tNd2OhqLXXys7ygmTUg9bht9LbVrYJxK50UU7g6+0DN1bn83 WFfC2afA==; Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pNChl-004m3l-0z for linux-arm-kernel@lists.infradead.org; Wed, 01 Feb 2023 12:59:19 +0000 Received: by mail-wr1-x42b.google.com with SMTP id t18so17257990wro.1 for ; Wed, 01 Feb 2023 04:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Hw6sl7oFDdNkKrpSvS4DEk1bWbFMgLKs0ATS0rkQZn8=; b=CbNaDaiXaXT6GtrdMMULs+E/jbHUR9HhPSILUa/OiUR34pmTwRsBPOIgtXkdjKO84s +tBqu2DpS6al0V7yB61IKnv+b10H2hGIu4mSo23kyjfta6kIjuixhQP0x0rEXX/18oY/ 5a/qIg/aGsRb8qL3tZ5QXMBb/MUc2X2+EhILxj2/ZHfc71FDOYtuWVdYCb75H7I2xGt3 dVv+kvRgOm9tSJFfaFFV6aj2whuG6A6473t88RFMCqgBbAoY2Ueaw7+cCeyrj/MBwuAd 9KMQAhZOpmADWbMrzT36SQbCGLLON1lZWw1KneZXadbfVjbmMX8sVoqte7988sXYtrPm mEqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hw6sl7oFDdNkKrpSvS4DEk1bWbFMgLKs0ATS0rkQZn8=; b=Ud7uvhCSXmg9YSsrKKeuQmuTuzLb5hLM91FWMuDdHAm2y8vcCa76dDiq44n+FsyRA+ +hkJ32OiAtB9ku2Df5NSRDYt8l1dDqHMO2a7xxp94d+lXPGItHoK9NbGkE/6Qz1sF3a9 h9IO3OL2WxhW7TP8V5Fye5lGV77gPYs32r9HGlk5GBe52q94SQYcacs9B4AgFIIGdGqE NJuCeNQravF3dWxxvEksMGxeOFdlYDs/mD9ozkC0BGn7Rlz5LcCHJNuMc7deJPOsmVSo t8Shiqv1Yg/hVAB5p2KlLbyw0G945DAKdyGa/v0BUAsZgdaYTQQQOBrFLLzrMfZeIsa2 /NbQ== X-Gm-Message-State: AO0yUKXdSR2JBXAIcbUp2ImgGstadrduBVzVcay7StgQ11bbTcvUxuXN hu9XRcH0EMXutVdPJ5L/ba45IQ== X-Google-Smtp-Source: AK7set+Pwyjpj/A8PSxsfEiu2UEOKeiweKmB/2zupayUhYBIdKRAMAWRGCPU2Xjlg1Qgye4T4MctNQ== X-Received: by 2002:a5d:6110:0:b0:2bf:b9a4:f688 with SMTP id v16-20020a5d6110000000b002bfb9a4f688mr2377259wrt.23.1675256386278; Wed, 01 Feb 2023 04:59:46 -0800 (PST) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id m15-20020a056000024f00b002bfae16ee2fsm17972811wrz.111.2023.02.01.04.59.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 04:59:45 -0800 (PST) From: Jean-Philippe Brucker To: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, joro@8bytes.org Cc: robin.murphy@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, smostafa@google.com, dbrazdil@google.com, ryan.roberts@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev, Jean-Philippe Brucker Subject: [RFC PATCH 34/45] iommu/arm-smmu-v3: Add host driver for pKVM Date: Wed, 1 Feb 2023 12:53:18 +0000 Message-Id: <20230201125328.2186498-35-jean-philippe@linaro.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230201125328.2186498-1-jean-philippe@linaro.org> References: <20230201125328.2186498-1-jean-philippe@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230201_125913_653363_96A2A894 X-CRM114-Status: GOOD ( 25.35 ) 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 Under protected KVM (pKVM), the host does not have access to guest or hypervisor memory. This means that devices owned by the host must be isolated by the SMMU, and the hypervisor is in charge of the SMMU. Introduce the host component that replaces the normal SMMUv3 driver when pKVM is enabled, and sends configuration and requests to the actual driver running in the hypervisor (EL2). Rather than rely on regular driver probe, pKVM directly calls kvm_arm_smmu_v3_init(), which synchronously finds all SMMUs and hands them to the hypervisor. If the regular driver is enabled, it will not find any free SMMU to drive once it gets probed. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/arm/arm-smmu-v3/Makefile | 5 ++ include/kvm/arm_smmu_v3.h | 14 +++++ arch/arm64/kvm/arm.c | 18 +++++- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 58 +++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c diff --git a/drivers/iommu/arm/arm-smmu-v3/Makefile b/drivers/iommu/arm/arm-smmu-v3/Makefile index c4fcc796213c..a90b97d8bae3 100644 --- a/drivers/iommu/arm/arm-smmu-v3/Makefile +++ b/drivers/iommu/arm/arm-smmu-v3/Makefile @@ -4,3 +4,8 @@ arm_smmu_v3-objs-y += arm-smmu-v3.o arm_smmu_v3-objs-y += arm-smmu-v3-common.o arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o arm_smmu_v3-objs := $(arm_smmu_v3-objs-y) + +obj-$(CONFIG_ARM_SMMU_V3_PKVM) += arm_smmu_v3_kvm.o +arm_smmu_v3_kvm-objs-y += arm-smmu-v3-kvm.o +arm_smmu_v3_kvm-objs-y += arm-smmu-v3-common.o +arm_smmu_v3_kvm-objs := $(arm_smmu_v3_kvm-objs-y) diff --git a/include/kvm/arm_smmu_v3.h b/include/kvm/arm_smmu_v3.h index ed139b0e9612..373b915b6661 100644 --- a/include/kvm/arm_smmu_v3.h +++ b/include/kvm/arm_smmu_v3.h @@ -40,4 +40,18 @@ extern struct hyp_arm_smmu_v3_device *kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_smmus); #endif /* CONFIG_ARM_SMMU_V3_PKVM */ +#ifndef __KVM_NVHE_HYPERVISOR__ +# if IS_ENABLED(CONFIG_ARM_SMMU_V3_PKVM) +int kvm_arm_smmu_v3_init(unsigned int *count); +void kvm_arm_smmu_v3_remove(void); + +# else /* CONFIG_ARM_SMMU_V3_PKVM */ +static inline int kvm_arm_smmu_v3_init(unsigned int *count) +{ + return -ENODEV; +} +static void kvm_arm_smmu_v3_remove(void) {} +# endif /* CONFIG_ARM_SMMU_V3_PKVM */ +#endif /* __KVM_NVHE_HYPERVISOR__ */ + #endif /* __KVM_ARM_SMMU_V3_H */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 31faae76d519..a4cd09fc4abf 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -44,6 +44,7 @@ #include #include #include +#include static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); @@ -1901,11 +1902,26 @@ static bool init_psci_relay(void) static int init_stage2_iommu(void) { - return KVM_IOMMU_DRIVER_NONE; + int ret; + unsigned int smmu_count; + + ret = kvm_arm_smmu_v3_init(&smmu_count); + if (ret) + return ret; + else if (!smmu_count) + return KVM_IOMMU_DRIVER_NONE; + return KVM_IOMMU_DRIVER_SMMUV3; } static void remove_stage2_iommu(enum kvm_iommu_driver iommu) { + switch (iommu) { + case KVM_IOMMU_DRIVER_SMMUV3: + kvm_arm_smmu_v3_remove(); + break; + default: + break; + } } static int init_subsystems(void) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file mode 100644 index 000000000000..4092da8050ef --- /dev/null +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * pKVM host driver for the Arm SMMUv3 + * + * Copyright (C) 2022 Linaro Ltd. + */ +#include + +#include + +#include "arm-smmu-v3.h" + +static int kvm_arm_smmu_probe(struct platform_device *pdev) +{ + return -ENOSYS; +} + +static int kvm_arm_smmu_remove(struct platform_device *pdev) +{ + return 0; +} + +static const struct of_device_id arm_smmu_of_match[] = { + { .compatible = "arm,smmu-v3", }, + { }, +}; + +static struct platform_driver kvm_arm_smmu_driver = { + .driver = { + .name = "kvm-arm-smmu-v3", + .of_match_table = arm_smmu_of_match, + }, + .remove = kvm_arm_smmu_remove, +}; + +/** + * kvm_arm_smmu_v3_init() - Reserve the SMMUv3 for KVM + * @count: on success, number of SMMUs successfully initialized + * + * Return 0 if all present SMMUv3 were probed successfully, or an error. + * If no SMMU was found, return 0, with a count of 0. + */ +int kvm_arm_smmu_v3_init(unsigned int *count) +{ + int ret; + + ret = platform_driver_probe(&kvm_arm_smmu_driver, kvm_arm_smmu_probe); + if (ret) + return ret; + + *count = 0; + return 0; +} + +void kvm_arm_smmu_v3_remove(void) +{ + platform_driver_unregister(&kvm_arm_smmu_driver); +}