From patchwork Tue Aug 6 23:41:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 817134 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2078.outbound.protection.outlook.com [40.107.92.78]) (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 6C71A17966D; Tue, 6 Aug 2024 23:41:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987700; cv=fail; b=lTmmJ7ctx76cqiLQxwDm6/T7oS+m1p5mBhS6KWNqiftlQOV1omjeXJQR6Yta0L0LRrQe5PhBuDdVvMwxx/8s9/SjiJaZpUzZGrhi5h1cXcVqaR8Ya9h28TBQ2yM2MLBGNWrBaa4l8I2D7RnAFVJ3hoHS2LS3R6iX7x68Ld1MJvc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987700; c=relaxed/simple; bh=bBZJn5lvUHjg+zR9EfbvHIXFb8wD7ASEalzWJs0FKU0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=PDYoz8BiyBlq2FJ+GDIt5V4mS1x070vf7yP7nYIV1K1ts5PyzgprAaN5UOVYyA0oW/P7GzCAo/Pxqn8Ry6WR6TfhaF5NqkKotfF8h8CTDQKZO9Gtd+vQLJ1/CW4TJUJINafX3ulE4PKcB3P/BQMwDuDPOoWFxFVJIcZd0Kpig5o= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=K8MbFPtg; arc=fail smtp.client-ip=40.107.92.78 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="K8MbFPtg" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hgUkJrUF70bn+lJdMggGWavxbpLJoA1JTGFHVAeCau1nKihHCDOhG96Cb+k6ieiAJiLmkeYCsYDDy5M4QZV5VKc4scV8vKuKN5D19txGpzDXTS1sPctz/yX6r3GUMRe8KaPl7G8KT0xj7vYlzy+tnLkh4i4GZR3eEq2oy0uN3CbTgcnB6V1ODmSgJ1CawAHg+Yl/8nfHCLdukGyt2Xeqqyf1Yfm/PSGHQDK4qCL9vIYX3lIcDGB3Fwt09ztKad3tSQNi/BvmH/KEwXLFuNpAYCdkc7ydApPjFeKIsVna7D2GbRaURy8YYkuFtV5JHYhEeqLJSuv4VQy5iXU/FYxmyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=o1sSWK9laM2BGthFnUvws9LO/P1JFlpBeLAmt4Rlb+A=; b=NggNVZ/7uXP+Y3oas7VN9NEqwOngUeLRGPu/kOVZJzrm+m3H4pP2GuMJWIK9sM6WBGzE8+xfJDhEjKUZ+mtqiJ3093stmFUFPCb7ogmaz9KFpHfcffjGDNh0KDc1g1DlsGdGfcKn8wxXS5uh95xyRAZoHNzUPR/ODJ9JRRLGjbbwmJ6FsvIl7sdXkA5LA5vp+8KOBXrE22oryyodQLFYHjvmatdho2jQ2ZeIEBibx3W0Qqyk3Bb/LhOsxCJl4XtTeSGVcsrsus9yBV5d52yiOuUhyJc++NTltGhgTuhx07TYekb0BeqZUEGH66oY4pP1Qz7KGMW9/EsH8xbmYTtX3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=o1sSWK9laM2BGthFnUvws9LO/P1JFlpBeLAmt4Rlb+A=; b=K8MbFPtge43buJWN2hjLb4pZce9ny1/z1wCKbQdz5JNqse9QgJkK5VNf7zXLqOA36UUesful1fggfFWiVOORpS9q076r+bZZ4X4pNdeAnyie52BVpC6pB8QRYnFRDT3UHV9rqcE4o/bq3zVN++iMaO+kXZAscVFqRbsxoy2sya42PxUrY/f9d5gpTt4lsTJkfQV+BPdqBSQ2HeZvBY17Q0vDUjECcKChJN0qCxJcDigjuwgEqFOjvao6BBp6Fx4pK/z4ovmJyuAy2EbI8b0JaRIdcUY5pfmMrKuyC6L+98DaFd7icP5KjBoItO3nCVB8OHHlglDtD/TJcuj9wLwh7g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) by DS0PR12MB9422.namprd12.prod.outlook.com (2603:10b6:8:1bb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.27; Tue, 6 Aug 2024 23:41:27 +0000 Received: from CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8]) by CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8%3]) with mapi id 15.20.7849.008; Tue, 6 Aug 2024 23:41:27 +0000 From: Jason Gunthorpe To: acpica-devel@lists.linux.dev, Alex Williamson , Hanjun Guo , iommu@lists.linux.dev, Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, Len Brown , linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Lorenzo Pieralisi , "Rafael J. Wysocki" , Robert Moore , Robin Murphy , Sudeep Holla , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH 1/8] vfio: Remove VFIO_TYPE1_NESTING_IOMMU Date: Tue, 6 Aug 2024 20:41:14 -0300 Message-ID: <1-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> In-Reply-To: <0-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> References: X-ClientProxiedBy: BN0PR03CA0049.namprd03.prod.outlook.com (2603:10b6:408:e7::24) To CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB7763:EE_|DS0PR12MB9422:EE_ X-MS-Office365-Filtering-Correlation-Id: 04c50e03-7cf1-41eb-7a3f-08dcb6714948 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|1800799024|366016|376014|921020; X-Microsoft-Antispam-Message-Info: kayzKn46Nl4Mpw6D5EVliY1rR/rKNTU/GcHyDSdGqnUeSVkpLQbFky1cETvwliGCOxo3wqkngYnJSX5PSrK6m/ePJGrTjTVj5g8lz98Qlt9slvv4LEPcnOrgDY0h3Gv9HQPqJa9szPLloXOC5PuHtn8f3Ms6EGBhLKVPXaQOuK7Z1XANogWzrZpuYfW+ucLvB/cDcBrxxdvCuW4dH3zf0xzC9fwlv5lFRnFalhSR7Zp7B4OCsFNx6nripfZnUGb6rIxKg+yZtGu45aqG8Q/jioX5V5IP7brt0NWggZkXjI0nI2u96LdWZesUhA3+nldD6dNbjUrYulVDX7+0dEzijVFKgOxbNlTyrPmdfq7uZabJoaFE9EySp1uwBgGuL4piBtxN+XJF/e+Go6HMWvJm5U14vFhR8Ikufd/d9gbilgUWd7q7nRhjvfVauT5vBfD4z3asLGq8K4JYm0c5Hpt+8hVieF97mPcqvWGjkmrW/H7038ToaCtTCpqJC9dOpf92x2HiE+rcm7k9MpuLu9XJfE5PWF4m1N7J1vTKvs/iUl+JPCiF8HfJxi5VtPtiWpkPi/rU/TJp6sYiozizIIWxBgAatYN9z+IWKTgzZ0xX6Tv8qbNLkswqBG6odRf1MuqXu0/DDfGhOnf7FfEne7CtOnk5E7qaiDtI51JuWRDaAE5kv6t9fQAPBpl2KeeY+Tp/oYEchpghVVefsrNcSWBb1TococAYRnvvSybZhbtE3zxx5XkkmKvaj/QE3BsWdDH1ec9b5JSuVAm88aUH1hL2/TCq6qIuFeNJA9dAX00lmxbwY5X0L1UDJx1ltVweiZKWFSblvkLsTs7z/g9Stj0guMNV1Ipd9pOa5uhZZ8ubnROVNzOPAkNzJCzd1jtH41ewtUtgs/Q7PznrP+CILutvpA8vNIqiYwidFh11jWOORoKzvBA3CW/+AvTJkYOFHbyyEVMi0br/N3X2GIjyUgxgJR0bD7/p0IAIQd4CmUEk8RhHjgE0mYp1z1MKVlawgjyVtL1sJrJ2ippY8pn852SxHqUToIZwpfLC9m59Uo4jK/l1pkFfi3irGD0jbKwr4zVDuhWnzI6jGSRbiXQC7SL1aSfYSbqTwlzgDISK0ceaScCla/GLxVUHuxeu6TwOaHMOwnDLLMV8FetX0v2oFS9NSjGQuXvVE6f2A/XQAJrPt99fOrxab7kbA2UGNIMeVpKfJRlwvDdPgfEvBFya853NTiQNOZ1Z0420esZijkV9k6GybfK+fWMMDqheuSqEdm2p6llGQOuKUbr4A7ne6uXV/cSyGsUlWGmMcfKEEX5lipQcE2fY1Y7CR3axx+CzLZ9bIpX9fJv/hmow8lbe5KbakxXqDSXGVFo6+M3YurHiu9zY4+623xc/N96AlddJOm0gHvd07kuRLsQvi2iIWP/wjw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR12MB7763.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(1800799024)(366016)(376014)(921020); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: NNDr48GkL09AXJ5DCp4f5FJ+mZ+hBdDO3cT7tZDWKjw+Oqr+kHSEfpjkbHJ22ewBqfFB5xBVYtTLyP2SQPlvNbnK/0e1JzLAd9bhErkQtidheOHd1PtPe2dQkC19CQM4JligdaI6NZGDveSYqGrZMzSYf2BGk7u6rZ8oPZn084Oz/ZyIyjQLr3X2ttSfEhzXgdy9H/spMeCttuMU3jIAl41ZdJHYlJJe2vFRHt2pxQ3aH6cEWiwZV7gzQ2DnVvirHotpwTUheRCctMJ37VSPWzwFi4y2uI4G0r/mz45YAHTJN21A++pgMsKEd/mdk4gtXKMugv6HDoazzfwEi6Pxu02kSi08yshnitlE+sov9Jy4Ia4rjLm3lwTTIgbdWGv/4KBVOJ9OTRTbDE+qcKHzSd37gJXJizfmJVzuNXu/Yebmw2HE78WB40Gf1+InDYGfyuIwplhdN0AglgDXdokyE8RFd/gAeUYoEkKDF7rjcbQbkV4lsNQB9XAcwCJzMkjg86EUdjgw50mXxCk/OTwKeXTLjHwdFqGNf/37D9tokI8SXVnhtA40ruR48AKya3lGh7BSbD1XcIJRAtY0zeMVrVv1FO8p9JYG5/KRpvDcHhSsutsshPFlBW+FGn0wcgfIO6LwOmKwpCjxJaXBWM9V0T/tKL7Yumba7dnE0napCVXVwbKq4jwwrbaTDj3yYOC252eGn5FvikXpJqJ6xmd7RrSw8oRnorQbptrqdTk7Jyix3UhBe3X6Ss8vhgiUbHHg5UQaGOzPZ4JWiKrqp6qNfsRlPl7baAUHdG7DPgVitz8ViaSiBGFI0XGuHpV/gSjFPC+OSTzqL2/fiOc2SXc+5PsVuv1CJOl/n3180A7Cvf90MC2So5Ttzywd/kQ+35w/5pniWGB6jHMlbmw3/oHGCKnph/99rV3AWEl4aK6eun7uPvDZBXrH4j28eMX+/wjaSIgz/BbQIXa99uJct3vZ3HbBIqVd0lTuAMQmLgO8aeiMnoPjV8V55h5LiEAKl+0KH7/5/KGNXSdscwX5fbEe6Tt5vY4LaFUxIy2ywezKmlmusD93lCZDI9wXJFI2LCeRPlcjnPa7KJG5LQnbB2DBrK1kczLchzo/Hj5Yg/JFMHg8DFtTRMoA9OwNyKIA/ov3RG82pYqk8VJW0iRNxeo4M0GoWMdYNasfuLLm4Qh3ZzIAu0SqpC3YVbEU8310Nac57yXdX5VIu20wl0HhCZRHlGucfm3x/HSVFQjklU7bg7jHKmZTpDfcWrM16W0r4ygGMLpuhBdbkKzWBEE3oMFcLl2/hBPxvGt7aPi+mGWoJX2G6LXdPbzoyFgDOq0kERLKYrIyic3SXK3u8pKHJk1RAqXhujH/uwRbjFI8igM2dtJH1B+abDluVGyNuYMRJkxEisume0xuPjJJ2FQ+FIVBlWYKYcH0Qb2C7dO1R+9igV9yjkOEx68ygk+wEFaYf38+/OvGAVpuIb4VGHRGMJv1qCbTkcpWmkYtwWNpbGFHmanx3Q4RRR3kTdNiiJooq84CW5os/oxaaE2Wz930xWpDoBkoj7WoTTQPY48DRbi9X9U= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 04c50e03-7cf1-41eb-7a3f-08dcb6714948 X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB7763.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2024 23:41:25.8898 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: dy6oSR9+bUIhoYeHR69ZoDN7Ik7FZscNaVcp242MVpnWjMJg8sGYPhH6mtDektEX X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9422 This control causes the ARM SMMU drivers to choose a stage 2 implementation for the IO pagetable (vs the stage 1 usual default), however this choice has no significant visible impact to the VFIO user. Further qemu never implemented this and no other userspace user is known. The original description in commit f5c9ecebaf2a ("vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type") suggested this was to "provide SMMU translation services to the guest operating system" however the rest of the API to set the guest table pointer for the stage 1 and manage invalidation was never completed, or at least never upstreamed, rendering this part useless dead code. Upstream has now settled on iommufd as the uAPI for controlling nested translation. Choosing the stage 2 implementation should be done by through the IOMMU_HWPT_ALLOC_NEST_PARENT flag during domain allocation. Remove VFIO_TYPE1_NESTING_IOMMU and everything under it including the enable_nesting iommu_domain_op. Just in-case there is some userspace using this continue to treat requesting it as a NOP, but do not advertise support any more. Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Acked-by: Alex Williamson Reviewed-by: Mostafa Saleh --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 16 ---------------- drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 ---------------- drivers/iommu/iommu.c | 10 ---------- drivers/iommu/iommufd/vfio_compat.c | 7 +------ drivers/vfio/vfio_iommu_type1.c | 12 +----------- include/linux/iommu.h | 3 --- include/uapi/linux/vfio.h | 2 +- 7 files changed, 3 insertions(+), 63 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index e5db5325f7eaed..531125f231b662 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3331,21 +3331,6 @@ static struct iommu_group *arm_smmu_device_group(struct device *dev) return group; } -static int arm_smmu_enable_nesting(struct iommu_domain *domain) -{ - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - int ret = 0; - - mutex_lock(&smmu_domain->init_mutex); - if (smmu_domain->smmu) - ret = -EPERM; - else - smmu_domain->stage = ARM_SMMU_DOMAIN_S2; - mutex_unlock(&smmu_domain->init_mutex); - - return ret; -} - static int arm_smmu_of_xlate(struct device *dev, const struct of_phandle_args *args) { @@ -3467,7 +3452,6 @@ static struct iommu_ops arm_smmu_ops = { .flush_iotlb_all = arm_smmu_flush_iotlb_all, .iotlb_sync = arm_smmu_iotlb_sync, .iova_to_phys = arm_smmu_iova_to_phys, - .enable_nesting = arm_smmu_enable_nesting, .free = arm_smmu_domain_free_paging, } }; diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 723273440c2118..38dad1fd53b80a 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -1558,21 +1558,6 @@ static struct iommu_group *arm_smmu_device_group(struct device *dev) return group; } -static int arm_smmu_enable_nesting(struct iommu_domain *domain) -{ - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - int ret = 0; - - mutex_lock(&smmu_domain->init_mutex); - if (smmu_domain->smmu) - ret = -EPERM; - else - smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; - mutex_unlock(&smmu_domain->init_mutex); - - return ret; -} - static int arm_smmu_set_pgtable_quirks(struct iommu_domain *domain, unsigned long quirks) { @@ -1656,7 +1641,6 @@ static struct iommu_ops arm_smmu_ops = { .flush_iotlb_all = arm_smmu_flush_iotlb_all, .iotlb_sync = arm_smmu_iotlb_sync, .iova_to_phys = arm_smmu_iova_to_phys, - .enable_nesting = arm_smmu_enable_nesting, .set_pgtable_quirks = arm_smmu_set_pgtable_quirks, .free = arm_smmu_domain_free, } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index ed6c5cb60c5aee..9da63d57a53cd7 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2723,16 +2723,6 @@ static int __init iommu_init(void) } core_initcall(iommu_init); -int iommu_enable_nesting(struct iommu_domain *domain) -{ - if (domain->type != IOMMU_DOMAIN_UNMANAGED) - return -EINVAL; - if (!domain->ops->enable_nesting) - return -EINVAL; - return domain->ops->enable_nesting(domain); -} -EXPORT_SYMBOL_GPL(iommu_enable_nesting); - int iommu_set_pgtable_quirks(struct iommu_domain *domain, unsigned long quirk) { diff --git a/drivers/iommu/iommufd/vfio_compat.c b/drivers/iommu/iommufd/vfio_compat.c index a3ad5f0b6c59dd..514aacd6400949 100644 --- a/drivers/iommu/iommufd/vfio_compat.c +++ b/drivers/iommu/iommufd/vfio_compat.c @@ -291,12 +291,7 @@ static int iommufd_vfio_check_extension(struct iommufd_ctx *ictx, case VFIO_DMA_CC_IOMMU: return iommufd_vfio_cc_iommu(ictx); - /* - * This is obsolete, and to be removed from VFIO. It was an incomplete - * idea that got merged. - * https://lore.kernel.org/kvm/0-v1-0093c9b0e345+19-vfio_no_nesting_jgg@nvidia.com/ - */ - case VFIO_TYPE1_NESTING_IOMMU: + case __VFIO_RESERVED_TYPE1_NESTING_IOMMU: return 0; /* diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 0960699e75543e..13cf6851cc2718 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -72,7 +72,6 @@ struct vfio_iommu { uint64_t pgsize_bitmap; uint64_t num_non_pinned_groups; bool v2; - bool nesting; bool dirty_page_tracking; struct list_head emulated_iommu_groups; }; @@ -2199,12 +2198,6 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_free_domain; } - if (iommu->nesting) { - ret = iommu_enable_nesting(domain->domain); - if (ret) - goto out_domain; - } - ret = iommu_attach_group(domain->domain, group->iommu_group); if (ret) goto out_domain; @@ -2545,9 +2538,7 @@ static void *vfio_iommu_type1_open(unsigned long arg) switch (arg) { case VFIO_TYPE1_IOMMU: break; - case VFIO_TYPE1_NESTING_IOMMU: - iommu->nesting = true; - fallthrough; + case __VFIO_RESERVED_TYPE1_NESTING_IOMMU: case VFIO_TYPE1v2_IOMMU: iommu->v2 = true; break; @@ -2642,7 +2633,6 @@ static int vfio_iommu_type1_check_extension(struct vfio_iommu *iommu, switch (arg) { case VFIO_TYPE1_IOMMU: case VFIO_TYPE1v2_IOMMU: - case VFIO_TYPE1_NESTING_IOMMU: case VFIO_UNMAP_ALL: return 1; case VFIO_UPDATE_VADDR: diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 4d47f2c3331185..15d7657509f662 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -635,7 +635,6 @@ struct iommu_ops { * @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE, * including no-snoop TLPs on PCIe or other platform * specific mechanisms. - * @enable_nesting: Enable nesting * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. */ @@ -663,7 +662,6 @@ struct iommu_domain_ops { dma_addr_t iova); bool (*enforce_cache_coherency)(struct iommu_domain *domain); - int (*enable_nesting)(struct iommu_domain *domain); int (*set_pgtable_quirks)(struct iommu_domain *domain, unsigned long quirks); @@ -846,7 +844,6 @@ extern void iommu_group_put(struct iommu_group *group); extern int iommu_group_id(struct iommu_group *group); extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *); -int iommu_enable_nesting(struct iommu_domain *domain); int iommu_set_pgtable_quirks(struct iommu_domain *domain, unsigned long quirks); diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 2b68e6cdf1902f..c8dbf8219c4fcb 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -35,7 +35,7 @@ #define VFIO_EEH 5 /* Two-stage IOMMU */ -#define VFIO_TYPE1_NESTING_IOMMU 6 /* Implies v2 */ +#define __VFIO_RESERVED_TYPE1_NESTING_IOMMU 6 /* Implies v2 */ #define VFIO_SPAPR_TCE_v2_IOMMU 7 From patchwork Tue Aug 6 23:41:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 817136 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2078.outbound.protection.outlook.com [40.107.92.78]) (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 3A79B176FDB; Tue, 6 Aug 2024 23:41:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987695; cv=fail; b=EVW/YthW5+40EGgsWW4oSOYOfU7vGJGHVHj5ykF9xSRoFtnGS8m7uwu07ekLL+/lJzIpSH4wU5gyAMrDTk4kBfT362aVuVYfy0la24YicGWUzc4rdZs3e4mnltLd2YqxMIJkk3Zh/UwGrK9+xAxMhpp+j5pQ53QbrTfm4cJw2M0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987695; c=relaxed/simple; bh=26qwsxWB5OYlNcsTkvyqGvjljo0iWzq01q1LuhIsZ9U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=BdYdG4mmcYHCs7XWQjYyznc50LdyUyibNthV3+PLa3gBnQq5Y3aUcWI1jKeQaJq6iASrAL85Teu6U/A5CbLjNtBJ3+KeAuUbRB/JwCncRLE0xhZqc2F5zIv4f4wV/JvgU1Ygp3sYsD7H2eQlvXX2ObIn2K1Ag5uM03T2rA6M1ew= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=t+Q9VHe1; arc=fail smtp.client-ip=40.107.92.78 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="t+Q9VHe1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RZIXMJneVbg038vmtatxcM+jcrJEsG9TH64UBS2IcEGAlgE+Zr+cyszYw87fN9d69rUtlCJ5mzK8hv+8yYjuE3LkP65dx35FIYKFsZCNfgMv3c4qqvWwUJjK7SOzzBhCs+zj3NhtjetTsSng3h09H/lp1vCIdzGCdHPryVISwAYSaWjyMcXI3KkwverObBdlvBvaApksBRfBHx9JyN1YHFq0LVd/eTezr7itHlltCbvEqCKId3Yr8mOqWjHSwFhcLE4yb4ORiyMRQNHMza2gLtA8gQejBRNZe7DCQwSZN1X0SG3XqxCsUjCA4tpQzYsM2xgRowjl+sHf4YkLV0gPGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=i8uELISHKDo8aopEsoXqsfcXZwuzDOSWDjKX+9+WdCY=; b=U3kTjate4F+8piwD7U3XA9H8jYZO/VuFNkeS+cBKVoOvJq6XH3IHf8j9HLpQECAzbQbYqrN4l2TgC/2k5VGPUYua2fZNMogarPqKMHifIe5lhSYrsYBBZT06geGkOYsS3U4zIqCehj7tWmaYEtW81mu/RzyDRr9naC8cWTfW1jsSQmfqrDCDHUGGm751ACTp8uGfg+TSGU/9ktBo+Ch9X89wsGmH14Ps9vYSNkUaPLIC4uPWsA0LonsBj1Au+ADp1F8dKkJUVlkTkQmw1YkHcoBdfPDq+7WqhMKDAD6WEJH+deo9k1HLj8s+plWRjeSVXkYuaSdqALxEz+l1YV6HFA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=i8uELISHKDo8aopEsoXqsfcXZwuzDOSWDjKX+9+WdCY=; b=t+Q9VHe1t5/ujrs0gEsOC0VovOk9ThfFuyCJgslmY/+P7jcPebvdHAicOFgw1FeJV3MByv8bZilDAqc1I/wej168W4SD/UDDLMLtCRXEGeFT0Q73UWjhsJzXHEplQXS2JtPumz8q7hZEP06P+rM8UGM2NRLzlEI7fvm3L1B7437xABmAqcdek3yZaVfqU6O4R+BlRsuvqmEnVoUIp+nK2/qGqBbSxi2WE3IUXedExDV/Sslqr+PmBSCpYv0sgDHG6CbhR8iwhhO0i22jCxo/mIiowMRZFYmFT1NwAzvHTjLPBvKG5303c1wSDyrYppUzIZVIdou+oo1T4CO/famSlQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) by DS0PR12MB9422.namprd12.prod.outlook.com (2603:10b6:8:1bb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.27; Tue, 6 Aug 2024 23:41:25 +0000 Received: from CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8]) by CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8%3]) with mapi id 15.20.7849.008; Tue, 6 Aug 2024 23:41:24 +0000 From: Jason Gunthorpe To: acpica-devel@lists.linux.dev, Alex Williamson , Hanjun Guo , iommu@lists.linux.dev, Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, Len Brown , linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Lorenzo Pieralisi , "Rafael J. Wysocki" , Robert Moore , Robin Murphy , Sudeep Holla , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH 3/8] ACPI/IORT: Support CANWBS memory access flag Date: Tue, 6 Aug 2024 20:41:16 -0300 Message-ID: <3-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> In-Reply-To: <0-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> References: X-ClientProxiedBy: BN9PR03CA0362.namprd03.prod.outlook.com (2603:10b6:408:f7::7) To CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB7763:EE_|DS0PR12MB9422:EE_ X-MS-Office365-Filtering-Correlation-Id: 598044bf-73f0-428f-adf3-08dcb6714809 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|1800799024|366016|376014|921020; X-Microsoft-Antispam-Message-Info: 6TTGCOTZKdwheB17rSPAtTHQapfdDbHh4PBINzchFkEGhv0RtteDzS8TjAzzHqolwqJf7lON+N0ZZaZM0yLA2tdNv9GiLqucSzxb+pUYOkoIY1chlXssDgLciT1akPh7aMb5vlBy/uwiZ0ooUWtWkoNEcgtqkZgzBy9oIHD4RmO3JOBVd7DHsJJ5rY+ct/nQx/XPJJrNlPUoy7eMYlOud0zCWJCQi+YTG4jpNVRo/UKXZZwkDy4OMBN8eA0Yc58ua5A7MgcPJX4ddCtdrzMVKeqlxld0zWX1zS+4/zyuyEl8MkOLcCKibKk0aXPRAwvFUrl13uw9xv/BhwQcF0gCf9zCBZ9gc9Jggt1NjUA4SvxW7Ld6xDmoSiQVWVXeeadxXrRWJDi7/X0Q1c88X9GLOCEcN/HTbUhqhAXeozoiT2eugn+dcnRLWy+USWT8lWUUJZWAzEX2nF5ji+LFGKmGL/0nUI6GhSleccfSq9rZ4ru45aDPBsFZtwxwxA4pFTskJiZoAT3HytWxg7R9fbJApsNejeN461DF/UwejzSbAD9YHZlbmlwl5hC1Dgbr6j/ZfQakvPuR+t/A/KD04t6hULsCgoHgUIzIrLTMmxuaq3oxMVVfsTgz5/QJXsh/zryjTN23T4o6T9vk/+/VhUKiGIljM9ElkpNHJbQb75ObMnn9q/fEe7i4Ewi1DQIsWXucLViYV5c5qI8Xv9mticgCjWYqvBgvfRIoB1jEfijOgkqU7g5P86cwsX9s7NbURW9MAfHaZjBmawVO4ujiUiH5/wDzggO/oiuoURSd7eca5Xf21FSEi5BtU8lxiC1hnCBz+FYQKijTsQOAbpT5I/qJ58O5KCXbgYjjhs8MTbm1nlSAn19qmkkMUuzO49KXmvkW1OZc1ZDmC8GD+/F51wVxbAkx3cV2oTqzGyDJ6l1CUkUW59SmDPvMtr0kKmDAkVRIQJH0uVEw2iB/K0P5ZNtzs3jqco88iT1kdmScCUI0gUdBv3Zf6JWOHbcsEwJvBMEVGYlv+rSfyuqaO6oyIvawcBi1R/jE5NrXMtyZbGRuDo8NGtqF1yNIb0s4DpKt0ynR4WuyGEFfT9iQ4bb9cT2rqtUZcY84S+1Z181jkZtHmqjSUwU2jJ91SMqwZVENSyCPZKdbltxijCQSxuRbEsWaEYi5x0NRgnnR9row4a14GqpvtVDF1HAQSc0AZYmXSyp40SYT67Pi0eOCYlVTZk7mfnOj++SmUaNml60teYhfhMp0kkBsA031SFXy/+AfaS1gtKPo25yy5Z6YfmOkyxfbkW8TFp6xSMdgHUI7VAhT1T5Awo7SiEri8JQiqpZJdmuM1EsUKMRcq/fC5Amt2k9R/5IJgvaLjvMQflELgV/5lqT9Dnpriyqh9veQ6yKxoYhZ X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR12MB7763.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(1800799024)(366016)(376014)(921020); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YOGJyPuKmC10oRUbGYBxjReeiHXJEw/5ZLFykLjQTjiWwwHUCckjsgHCVpY5spAo1KzQMvA1muboAokG/SeqDlhc5n6xGgXgRlQ6hIOEQoKPfmTfi+uJwVlHFcmp45ny02dwK1QUrKk9HfZr0jrRD1L7r9gcRvfrfI1NMbrTmeeqAuX+gg2XlKiMRbbcPZ7sUStlHFya4mDZrQUoidky1gvqK6ns78oeQSZ7UtXIqRroodAu7iDuN0OqE+vtYoOM6NOZT3cnJsGai/oW0lt/MlJuHb4ufuMi9nr+ywYp21S4eaJGzfF3G22DkqF3nQ8xs/XI2BOi8fJ02fkCubuI5wdNk5mOZ+1hYj/GC+Ji4rLysveyujCqehggD1JAGqy6KI1vz4L3nQJIRFG8ky3cwlAlSJfwAsEvlde26Zg29QVV/Meh1YWMQ8FDCHKEnXXoEY5VI/m+HEayFoZK/AKxNGdXcrohKdYCo/ElUlkJb3rqNTbQ7tHAdDu9GZ3cEfCN7oSEfmJoP//UF11JAYabPBnmge6WtL2TPhU7bSwsR4d4pEb79D+ANN32zQPpyQUPcrKm3m1m3hn5KOGm9IfadjuTiZiECtjJ6X3N3ttLHDHHTfUv8JcOgZorXHhMG3mA8Depc+FMMFYHse7Sq1zwBDV3Z9VB2BP1Lg9NT+BwSbvMBLEqSRJCOdVHEDveFXSMKw6rek2gwknLjcxwJhScRHhf5YKYr/x/AITGiA2q44th+mevV6pRvxSCbwqxHFx+xGKqOz4g/TLitJ1Uf2EDfSh+0zsAjQaDOPdKcI13JkxyGXaNRakiT67Lyz9JpTWaT+92RIrFVIgjx6aip7UvWfOX/5WXAV8AaGbKuCBbEt/NQCgVzOlrETcwSxXCUFA5k55AKqJEECSy3JiTO2AuKUOx3DwPkCQzD40WQ5SUPe1UqR2RG/bHo7ksbVSvZws3xsBnL70jTmz+0o/zFfeT7Ld6vWfKsBC4EbFmOjf8/t6xHMowIKgLw1p735w1qIhacPdy3ZguiyR0P8fPDqPGV0yN7OBD/sy2KDe8PY/YY1LaeQGMkAzR0QX+xVtVyAS5Mf2d8vwQyuHFF13ztabw1i9pOSHFWSzEiyvsmxvsniUZLPgypf4mdYiL0mHRqVOuyMqn0okVfuwzzsAx/yVFE+uV3XfZqtTXGpBQqTQ1XMyVG5FsGV42A5cRQQr1bCCcjJISuswrhhrVmOn4TAnps0IvONeV0vqa+2eu6xDSC70mAx2el6ygXjKGEzcC33tzMnCac5x37lqmtBD9zUuwK7YnQWMuiCQpu0CP8X6o0tw8M9ObiQ+ZaxXc8Fl0pZikCkW9NummZxI8Wo4MfYy9F+rK6o9Dpy629uHSSpoxKyAwy/Scg36nxbkJIscwvwwbhO5EPesWgrFiatTSDoqhG8xuxWbZYmGpTYWvb/i6IY9J/8JvUPgpijCdrHi0GqH8hg5NqNk/aDIfmWgVOkk59YhaunVN3pagoNGw9vRCCvWjMavalFMgCfWLosWoHqqRUDRXCC9FSUtBlb5/BfGDrwxok9eyjjf1GNeTi0O/CCs= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 598044bf-73f0-428f-adf3-08dcb6714809 X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB7763.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2024 23:41:23.6812 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 0HsDZqRW5Yr9ist6qP5NAfL4lTJpXVtdNehjcQhDXRkXtBLLDbTeKTiyfNPNfwXJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9422 From: Nicolin Chen The IORT spec, Issue E.f (April 2024), adds a new CANWBS bit to the Memory Access Flag field in the Memory Access Properties table, mainly for a PCI Root Complex. This CANWBS defines the coherency of memory accesses to be not marked IOWB cacheable/shareable. Its value further implies the coherency impact from a pair of mismatched memory attributes (e.g. in a nested translation case): 0x0: Use of mismatched memory attributes for accesses made by this device may lead to a loss of coherency. 0x1: Coherency of accesses made by this device to locations in Conventional memory are ensured as follows, even if the memory attributes for the accesses presented by the device or provided by the SMMU are different from Inner and Outer Write-back cacheable, Shareable. Note that the loss of coherency on a CANWBS-unsupported HW typically could occur to an SMMU that doesn't implement the S2FWB feature where additional cache flush operations would be required to prevent that from happening. Add a new ACPI_IORT_MF_CANWBS flag and set IOMMU_FWSPEC_PCI_RC_CANWBS upon the presence of this new flag. CANWBS and S2FWB are similar features, in that they both guarantee the VM can not violate coherency, however S2FWB can be bypassed by PCI No Snoop TLPs, while CANWBS cannot. Thus CANWBS meets the requirements to set IOMMU_CAP_ENFORCE_CACHE_COHERENCY. Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/acpi/arm64/iort.c | 13 +++++++++++++ include/acpi/actbl2.h | 1 + include/linux/iommu.h | 2 ++ 3 files changed, 16 insertions(+) diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c index 1b39e9ae7ac178..52f5836fa888db 100644 --- a/drivers/acpi/arm64/iort.c +++ b/drivers/acpi/arm64/iort.c @@ -1218,6 +1218,17 @@ static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; } +static bool iort_pci_rc_supports_canwbs(struct acpi_iort_node *node) +{ + struct acpi_iort_memory_access *memory_access; + struct acpi_iort_root_complex *pci_rc; + + pci_rc = (struct acpi_iort_root_complex *)node->node_data; + memory_access = + (struct acpi_iort_memory_access *)&pci_rc->memory_properties; + return memory_access->memory_flags & ACPI_IORT_MF_CANWBS; +} + static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, u32 streamid) { @@ -1335,6 +1346,8 @@ int iort_iommu_configure_id(struct device *dev, const u32 *id_in) fwspec = dev_iommu_fwspec_get(dev); if (fwspec && iort_pci_rc_supports_ats(node)) fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; + if (fwspec && iort_pci_rc_supports_canwbs(node)) + fwspec->flags |= IOMMU_FWSPEC_PCI_RC_CANWBS; } else { node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, iort_match_node_callback, dev); diff --git a/include/acpi/actbl2.h b/include/acpi/actbl2.h index e27958ef82642f..56ce7fc35312c8 100644 --- a/include/acpi/actbl2.h +++ b/include/acpi/actbl2.h @@ -524,6 +524,7 @@ struct acpi_iort_memory_access { #define ACPI_IORT_MF_COHERENCY (1) #define ACPI_IORT_MF_ATTRIBUTES (1<<1) +#define ACPI_IORT_MF_CANWBS (1<<2) /* * IORT node specific subtables diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 15d7657509f662..d1660ec23f263b 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -993,6 +993,8 @@ struct iommu_fwspec { /* ATS is supported */ #define IOMMU_FWSPEC_PCI_RC_ATS (1 << 0) +/* CANWBS is supported */ +#define IOMMU_FWSPEC_PCI_RC_CANWBS (1 << 1) /* * An iommu attach handle represents a relationship between an iommu domain From patchwork Tue Aug 6 23:41:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 817138 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2078.outbound.protection.outlook.com [40.107.92.78]) (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 214F517625A; Tue, 6 Aug 2024 23:41:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987690; cv=fail; b=OlExrsMhxeGS0e+4yWAF87TcZmW1e7fpcCkLodOhLSxDi8CWx366gV9GjIUStb104KLcRhsUjMN/VMC4Qi4w9743twUMyDMCgjHfEJCXCy1oe9gU1mNwh/jPm29KCIP5U3W++25k0QBNyI1TRyWmd5nJnkS6FHzBfa2gOiM1a3E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987690; c=relaxed/simple; bh=fm0eMgXPQbtc8kB0elmkmSeVpo3opgh5k98Uyc2U4zc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=H5Mq1tnEhviqGG22dQIdIfZIhRUeNFwzw5/Q+gnQrVt8TATM9RClZt06QndDf01NP4LXqmCw/7YkaKSCUGwEzPmUJK0WV8rWyI2ho2WPAfTLIGL3YT+rPE3xPTNm5LplrqoqweGsMUanRQEEBlbeI7w5m7FDDYavgXQpO2BWcV8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=dxKRNco8; arc=fail smtp.client-ip=40.107.92.78 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="dxKRNco8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Hchf+4lFLBj6bao8I3DkLUHm6LoVFEYGM+RVn6sAFy0Wb5ZiDRgEyvGLpL6Ie5IlJuSRgAXhHsaM61byl8+BpkMcp8bQvUkfb2MqSR/POnWGBBg50o0hPAVaeBqP3omNd9Zt5nVDyzCEoUwRIf6D+n08Pld2TPl5gac1PhPKSCY5Y3Ubbwgc2zhUB/t4O9HkfejVV6FK1d5mevyLZreAcInG57/kZqw6hsv+dQB/OOl/s9bAer4k1eDr0Wd4mMs4g2yI35ecwj7JrMDcD6ktl9rngYnKjPfqBLR6ypxA4OlXPbiJ7aTnCVLE0ldX9k3AMGRc5Q5Z/f7dzT8x4zZMJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sxJ7bYbfH+piw4qD2+PsYhdNNnsDu2LAPIrP6Tt4Dz4=; b=uTVE4Pd/OKSYXp2wC5wsafm9NBD9SwHycAKjdhF35L2B+M+l1qnYpiiF1dtnauVZH8UKCgm5nEG7er4ToGyi4cpcRym2wv7shMVPMLPi7Tg7xWW1rCQYMFmIhPigBrqgdINCji8UFsMJGXbjPgHGQCUDj2f+D0TuOkGG4CDL2ZwfW4R7/RQeuEN1or1I8ypyXIh8nyApb04He2+ofSZPGkNvdkxHG9crNI8Zr9HdSG+lzRHzkZV3tlEaQAehHJyFwPMQyVg3/52VpTrKqzAzz62GJ/FPgTNhxgxjuj2KqarzHF8sGaGEHMFJAYFQd/LlqwGdVahrXsnFnqUEQLEXWg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=sxJ7bYbfH+piw4qD2+PsYhdNNnsDu2LAPIrP6Tt4Dz4=; b=dxKRNco8drz4p1jMmwkGLSaNsAtK7CllOP1r5PG1KplsBHh6HFR4awyM0XdVqVWIpVDEc1Cif1A2C6iBf4POJWSyqanbz2EtzLx4r8gA6df69EOSKFuLd5eaVYmytKqu3c4vrsSqK+2UMhE8b87fkNLyG8FNdjF0iMr/k7Iz+Y/H+PZC7g2rAa7oCaLSeEn0YfXU+atiqZcljSyHuwIMHyidb28V4+7tjApe4qDIjNAIyWNL6ac4WvTndqBL0Pvppt8o6nlcwgEEK4ccqs6F2W4rL2MNDzczd+AkoUh1g38DDvxcDAjzhw21TTfm734/po4bxXX8bUT4olgWJCdtkA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) by DS0PR12MB9422.namprd12.prod.outlook.com (2603:10b6:8:1bb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.27; Tue, 6 Aug 2024 23:41:23 +0000 Received: from CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8]) by CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8%3]) with mapi id 15.20.7849.008; Tue, 6 Aug 2024 23:41:23 +0000 From: Jason Gunthorpe To: acpica-devel@lists.linux.dev, Alex Williamson , Hanjun Guo , iommu@lists.linux.dev, Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, Len Brown , linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Lorenzo Pieralisi , "Rafael J. Wysocki" , Robert Moore , Robin Murphy , Sudeep Holla , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH 5/8] iommu/arm-smmu-v3: Support IOMMU_GET_HW_INFO via struct arm_smmu_hw_info Date: Tue, 6 Aug 2024 20:41:18 -0300 Message-ID: <5-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> In-Reply-To: <0-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> References: X-ClientProxiedBy: BN9PR03CA0964.namprd03.prod.outlook.com (2603:10b6:408:109::9) To CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB7763:EE_|DS0PR12MB9422:EE_ X-MS-Office365-Filtering-Correlation-Id: c505ee49-181b-49b5-d859-08dcb67147e1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|1800799024|366016|376014|921020; X-Microsoft-Antispam-Message-Info: JVxadVHn+nFm3mZEdyFsih6xNaZgpVv9Y57heZgtK+dBMN8q5FLzQj3m+W/oDW1bLd3tZomjR1NJvDyZZu+7NS8FrdcgHtaP/UxL8vZcrSUm1MRixS7v973lC3yysjyM5oa1ueU6QODmQAkIjC2Z66+EIyxo/5pFA5PNwoIiVzyuE+cb1crXKE50dgSKcgyDBPsoda7Fgg2qM93dDiMvLLI1KZfPZwIG1uBn2v1YGNuwqbq6eKLIZco7z2esLURnPwNxE5SXNNCXbEoT9Pzvj0HsvIPOFa5gKl0uxe71cq4Ui2Hwb9ypIlxxrNn49HEMY+ReApuvxjJanl5t/cC4aowHaSQbUmQPNtYBvxrILUkjZbTZyNv66uPAg0V8X9MLIJeahGjXtuGnlSzFIDnE1pW2pDxy9/i9tdUUMBULBspNHsoA/VJxmVFex7jI9V2Ae1JyrzhH2zlr9s8o7nwKqP0PGNcnAX7HGoAMVWtytB3YMSwU4+jh0Q6WZ2qOxugUxkq6jGFE2AlP5sZkjv9zfW26HviiHe+I05/P8f5Tj+qbCb+DjubCFfdg/9ezTpGTmr/au8xtSMXmHLtUPJ8oyTotc5UU7P3a91hujyn/Gq2HtqkliWRVkefQPKr+my7QBR4Cj5eWhb06EnD+OIGnlFLsqZfy4cHbGsHRKSGigIeGIp0En8pO3883DGWGu7VyUQyethXzVkdJNUvhiXJxYj1g4a0fQh5jPw3nHJXMAoqUNjQ1y7gOeYCVqgudp63EGE83m03PwHVx+c56NsMPxWub4jaPH4/cFeoTM/0g6OLongMSDseU8o20npzlWTKTdZw8+YZptgIpVAZGkgeAKakqmXLhhZk8cUHRUTxwUoxZPVddXvoOrlE35TJ2KwgBufZ8+buVJ/eRiJDs5OIJWuatM195XqU8BWacyH/gIzwITI2whPdPz55wwBfWlI31gDAg0y9T5zAD7gJb/p9+lEFn4+PPZO3fLOX+rKS8E+GJlH0wDIwF4jtxllB4jaKORCLwsU8BBD0jfe0Ra7XPcamXuoAifVNTahrTzJxDOVKSQW1jUw+o6u7Qrv9ExHgadM29xGWn5J/3O1LYDRriSx6C+stkyrQACMmbyAbwU7iRprvrU8l48jhRWLNceobCHreCV941K59k3gavK3mccAkWCf2fuOVB4+S2FE2BpQdhzBvUsHnGUym+tieBu9BhcQoF6Xa0XT52/HRudkvwopPjO0Yli/1soCyjhOZGnh0Kyh8lNYm1ww6gALFXhl5xvsjNNKUyt2WXiVKe6mBUyYa7CjP1UrvdXgvyUdRDvysHZtLb44ENG8v4BrZSgbS4059FbuAZndduJwbYOOjS3JOALN2+9rB/XBbCQnD/Gz62dh6ODNMdCfHE6YU91nHd X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR12MB7763.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(1800799024)(366016)(376014)(921020); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: zM7HJl50qFelmeAMkfr8HOBf+5rmI0fbUcc/SR0I/o+rmVSrcRKbcGtIf8AoADmDQTTybG1OGlvCLd3RkVVBiXehruEgUw/plFeZMjSYRKqbok34VVB+1h/sag+jgIpzc4xGLHm9Vur1ppXmLz52zr3S9jI22q8p+PjQ6AazVpHroLG8lxMeJFDPqDTV7fZ54W6ZkTSwim+hF66uAsOyO+j/4ziTkcJz7myv5bBjrK+GS1wtOgdFJUz5zPHWOlx1q6cSkctC9IfSKaShjkiOg87Ls3H4B58dioQdZgH+AksyIom5QeqaiyU33PdBsbS3qQnjRKF9UrLcMxKrhYzyhidpYD2N8r1Un34VCnLcsk+z43abNUpWsXyGwPZAa/dwNGlo4f1dsJsqsL/9WzpUrbyV3zZbtldmc4KSRW1JkXNbKMed66XpL2r57KhBYNgGzJRZeIMQ5G163tFAKEnouvQ7E/AXZGMhFl0UxKnlCNETR/uPUAOOn0GElkCy+XsA8Cny/xmIi3s75tkHvJYx+ra7B/qdYY5o+N91WjMYWQRsfvsKNxAr0XbfQUokWgMmCh2SI3LOyhj9gPIUBT3H3m87RNtRmZ3kw2Ud65zWOC92Qgi7tuafnFaIIzt8OKcmtttbHaGhkwrlohdHaUHaZpj0p5QEhrEPA9Ao7ycKDWFWPcao5F0ul2Mvjqfb876nOj73WQTserdVBH//yVQx2kMSEMsHAjymLXaEkgtV38G/rysdqbvSmEIsXjfGRLuzbo75BGlmefLuSDAH9DmB9ty8BsQrIJUB8UDFcvnjiokCYgXXG4RTMtNmqCk/+FqVW6zx/JAyV7QPgT043GIyYwfA9r2kGq4s2IYCed9SFHub0fNat9e9KimO5z9RNcCr4YhVbGWyW/rSXxDBPXiVjcY6El+hvQXZRVJS46S5AFvmji8j59Kvq9xow7ntOFJC6GiZvL9ZPxmkqo/vCYvQIaoXSL2RBrJMD37kwmz2K6rc3lCoZUd62IQZL+5ERXs7SVIu0nXf1p0jmu9rweP+aVPGlGV+I2XUlzkG8cUAzfkLCVrGI9+hg/6gpNwV19O/MnnhKKH4tfsysEnkxWQ6tr5TZg1dC8JdjKC1mFQ5eRgH4jCQLQL3ojZnwpPVddSo3IowglI6w/ySebQwS9EVnUBJ+z9s1e2+Zm9gP2SR2cLMYSFCa1RULTaT6W3zudhIiKk7eHSsIn96OVj3sPVI0N+fFcBc9X911lgR50wHKWKJEJtP4q3fCvbzuUo4Yh7zpZMh0g91fkgQX7ZDtVHJymsoEqR+zOYyAg1wQVIiJBcktbVi9/DE8OblzBMC660sLAm0VZDuG/Ei1W1eNgjHB7ANuiUKcOGxCXcI/n/41sj+2ZQa/9DMRQ4jA6+dwuOGxDs0Gs8DO9z0NspKRVVyD4KI/83iO26dyc0cy3Ou4zIyjH78571VqkN853YOuMcccc6t03HFvlYoXD6qGsmgRg4Bz4xyQ0+T5d8QFoSfLTkzfl7BzWrCjfqnRhIrzIKQ5/yflHgtZ+Qc+3scXie2q/0D1oI3fG3qpI6VXVyje9k= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c505ee49-181b-49b5-d859-08dcb67147e1 X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB7763.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2024 23:41:23.4456 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JAZ+5g+KPZ7R5FLS2HzXA0GJOISYIMlA3qvX4/JroDnTKbYEBD9V2uu/nByAqH7j X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9422 From: Nicolin Chen For virtualization cases the IDR/IIDR/AIDR values of the actual SMMU instance need to be available to the VMM so it can construct an appropriate vSMMUv3 that reflects the correct HW capabilities. For userspace page tables these values are required to constrain the valid values within the CD table and the IOPTEs. The kernel does not sanitize these values. If building a VMM then userspace is required to only forward bits into a VM that it knows it can implement. Some bits will also require a VMM to detect if appropriate kernel support is available such as for ATS and BTM. Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 24 ++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 ++ include/uapi/linux/iommufd.h | 35 +++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index 998c01f4b3d2ee..6bbe4aa7b9511c 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2288,6 +2288,29 @@ static bool arm_smmu_enforce_cache_coherency(struct iommu_domain *domain) return ret; } +static void *arm_smmu_hw_info(struct device *dev, u32 *length, u32 *type) +{ + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct iommu_hw_info_arm_smmuv3 *info; + u32 __iomem *base_idr; + unsigned int i; + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return ERR_PTR(-ENOMEM); + + base_idr = master->smmu->base + ARM_SMMU_IDR0; + for (i = 0; i <= 5; i++) + info->idr[i] = readl_relaxed(base_idr + i); + info->iidr = readl_relaxed(master->smmu->base + ARM_SMMU_IIDR); + info->aidr = readl_relaxed(master->smmu->base + ARM_SMMU_AIDR); + + *length = sizeof(*info); + *type = IOMMU_HW_INFO_TYPE_ARM_SMMUV3; + + return info; +} + struct arm_smmu_domain *arm_smmu_domain_alloc(void) { struct arm_smmu_domain *smmu_domain; @@ -3467,6 +3490,7 @@ static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, .capable = arm_smmu_capable, + .hw_info = arm_smmu_hw_info, .domain_alloc_paging = arm_smmu_domain_alloc_paging, .domain_alloc_sva = arm_smmu_sva_domain_alloc, .domain_alloc_user = arm_smmu_domain_alloc_user, diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index 79e1c7a9a218f9..58cd405652e06a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -80,6 +80,8 @@ #define IIDR_REVISION GENMASK(15, 12) #define IIDR_IMPLEMENTER GENMASK(11, 0) +#define ARM_SMMU_AIDR 0x1C + #define ARM_SMMU_CR0 0x20 #define CR0_ATSCHK (1 << 4) #define CR0_CMDQEN (1 << 3) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 4dde745cfb7e29..83b6e1cd338d8f 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -484,15 +484,50 @@ struct iommu_hw_info_vtd { __aligned_u64 ecap_reg; }; +/** + * struct iommu_hw_info_arm_smmuv3 - ARM SMMUv3 hardware information + * (IOMMU_HW_INFO_TYPE_ARM_SMMUV3) + * + * @flags: Must be set to 0 + * @__reserved: Must be 0 + * @idr: Implemented features for ARM SMMU Non-secure programming interface + * @iidr: Information about the implementation and implementer of ARM SMMU, + * and architecture version supported + * @aidr: ARM SMMU architecture version + * + * For the details of @idr, @iidr and @aidr, please refer to the chapters + * from 6.3.1 to 6.3.6 in the SMMUv3 Spec. + * + * User space should read the underlying ARM SMMUv3 hardware information for + * the list of supported features. + * + * Note that these values reflect the raw HW capability, without any insight if + * any required kernel driver support is present. Bits may be set indicating the + * HW has functionality that is lacking kernel software support, such as BTM. If + * a VMM is using this information to construct emulated copies of these + * registers it should only forward bits that it knows it can support. + * + * In future, presence of required kernel support will be indicated in flags. + */ +struct iommu_hw_info_arm_smmuv3 { + __u32 flags; + __u32 __reserved; + __u32 idr[6]; + __u32 iidr; + __u32 aidr; +}; + /** * enum iommu_hw_info_type - IOMMU Hardware Info Types * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not report hardware * info * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type + * @IOMMU_HW_INFO_TYPE_ARM_SMMUV3: ARM SMMUv3 iommu info type */ enum iommu_hw_info_type { IOMMU_HW_INFO_TYPE_NONE = 0, IOMMU_HW_INFO_TYPE_INTEL_VTD = 1, + IOMMU_HW_INFO_TYPE_ARM_SMMUV3 = 2, }; /** From patchwork Tue Aug 6 23:41:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 817135 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2078.outbound.protection.outlook.com [40.107.92.78]) (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 26AFB178380; Tue, 6 Aug 2024 23:41:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987698; cv=fail; b=TjSbxNorAKY1q4EExeooeJwUQ8da1uAW/iLBVhwA6eXAFueUSobJHEBS+bzn2zu4MRubd06bmsywcinTwpQLweBg9xxNc8xDug9BLjiTo/bxFYWAQ8TRcZX6JJgswU2n/CWOtZuOaKsgdMBp+beqJjW6VUHvKaGKHelncK617Hk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722987698; c=relaxed/simple; bh=R0FaVOwdlMrturQCTgl0y46nnQtivF8qLpj1PWj+n2I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=WJxdebYGroWjRdv3tSJbbwgfP6UMiFAx6qvUV2Wvc4KaBE9UpZjt4mj9Vw9h4M0mvx1h3tFDpT74ZPTtL+mje6H1s/DuJcJIWCGXdR26rTPoftodjXDHf/urEU16sVPX+uLOmb3W4O9Ey4t5RwEkICDs812PxOaQ/4vDSTvxRnY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=d1hzAf/1; arc=fail smtp.client-ip=40.107.92.78 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="d1hzAf/1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hnmElfbtV8ye05VHpIaj//ehPbYeWaNIznVfzmHMz9pSqTHO7QUlWFv9rYOQzQz8kO7406APCMaYq8UURB+8vIPI8OSEX/YYxoGu3ASLb0ARoA5Et0fclPldp87VLzQXNMB4u0QjHTA0xLSzKimD39M/T2H6nA2WYilwBYOFy15oV5G7P62GF3Da/MF0m6xEaoyoutI5DBjI24TJQ62Nx2vxsQn/SEdscIx7l/df2V4j0IAX/0lePC+ACFSVKKGtTou6cC9Zbzu58Sl4XUY2CKLzb0osTKzE69v/0BFjjscVS0CxpEAHL3RPCfiFAjO1rzSKzLOCly9ci4trSMUSkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=mo2Q5pdm1DdrPQ+FTWWBO7OGIkGdgEa++hYa2Vljj6w=; b=ZwOlGu9VZJVPcuut6oJyzeRGFl3yFWBuu/nnuWMzelk/hexvqAvtDMDeljHaIDwkw0a+LyT+TrUE7lNQEhqoWr+JIz51moPII1QmT0Z7WV4XtUF7qHfp/vgmt8gKOHCzZELYq+358yzDkx2dRgTu/i8orJ6o+MbmvIJeVCp9Qp+FSfmh40Z7GCYr6JuJ2yreV0H0CHDZL2hcHyJ9DwTlDJpVdBLMuMKbzH1LdLTO01gFB12JWCdlhPLWecqDiLxnGzxJ8R1fQxEtPGPvLJg3JIOf8i4wGw6BdRnQe7B3w3JVqdzxAZ+KSnEL1wovDBkQGb/Syz7IF8mI6zqgJaraxg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mo2Q5pdm1DdrPQ+FTWWBO7OGIkGdgEa++hYa2Vljj6w=; b=d1hzAf/1sc9DEX+4BGJzMglMV/LlnPe9hkOscuYXC0zPTRDljRqSs0pvIDPM1OWpvOn+0/xqr844sZ5kmBwuOALZeRykaxbKO6yRRSRqy9dT7qLgr2216SK49KNJvxoep+fJg/xPjZQuLW9ctQXImvtJf9NWT37t89gZu87Bnf1GbnuSWBwLJKUq+4vmewQ7/TTHS1nDxoXK/fXoC6dnsAdPsEwMbun8q5nnkYRp+QndikAJDefF/ETr/g67BW0/h4YWa8vEVtwMLD6LGvlkp3VlfMIZIT10hyBJ/wy7PbrWUFB95j6NkoWQeyuS9vvupyh+uu+DYQlFm/l6eoQ/FQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) by DS0PR12MB9422.namprd12.prod.outlook.com (2603:10b6:8:1bb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.27; Tue, 6 Aug 2024 23:41:27 +0000 Received: from CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8]) by CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8%3]) with mapi id 15.20.7849.008; Tue, 6 Aug 2024 23:41:27 +0000 From: Jason Gunthorpe To: acpica-devel@lists.linux.dev, Alex Williamson , Hanjun Guo , iommu@lists.linux.dev, Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, Len Brown , linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Lorenzo Pieralisi , "Rafael J. Wysocki" , Robert Moore , Robin Murphy , Sudeep Holla , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH 7/8] iommu/arm-smmu-v3: Support IOMMU_DOMAIN_NESTED Date: Tue, 6 Aug 2024 20:41:20 -0300 Message-ID: <7-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> In-Reply-To: <0-v1-54e734311a7f+14f72-smmuv3_nesting_jgg@nvidia.com> References: X-ClientProxiedBy: BN9PR03CA0987.namprd03.prod.outlook.com (2603:10b6:408:109::32) To CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB7763:EE_|DS0PR12MB9422:EE_ X-MS-Office365-Filtering-Correlation-Id: 400cba6c-8055-40d0-84c8-08dcb6714840 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|1800799024|366016|376014|921020; X-Microsoft-Antispam-Message-Info: tS9ZYbuk8BhU7njM8vP7odz4zP+2lRahef8rFy/+VnZyI/8qDd5G1+84AHShaAGfxbiCTo+PQtLlTT7+AE6JtwLRK1JS3kkGcVVBu7FgUbgQHJ14jxhkU29hrMJHAJzmz77jfpJcvvV/rHRCSUDwJr9gMoOQicXkOeKFzmj/DsaHCXxte0QE19lWD3m07tkCodT5sEvp4bB5dLZKau8C6NRFJg6kWtWFLphfucoCi2Q37uF/TdpJQzUOfBvYBcvUPZos7lbMqTxbM3dhCkY8xYBBU/pyEKClY6kZ9YJm8G2Nwyjx0VEDV7ZFrdvkVIWkksjuzgkvcJ2/5RDzvjiYnYO5slArkpPmRVROHtdCru9mtldyhVvEUODVEFoqwl13ijOqDP4iTlkSIxUWDSXN/CsLRVMzddQFPiz/HVHg8mWuSCqWlpx5CEhl7kHTnfye1ZElTtZqyqsdU2W7vkxE1kbV2c4xhh3CS4YIOu37cKWbG7K5dDtg4mbBj+k1HC+3cTNvcFuw5KTlfw7tZP9pM+RQ17qhSmCDjzP9sAwDFkZZ2DLtsVOQ2yLLwPKH9yKbX2MuEzz0eHn+XQYMf5EH/ryMGh73OfdBCdva/508nZCNAjnT39pYWOyxOlAhiDpUocQZQkef4JyD/SBZ3pCqLvDJ146vQ9AB5ie+LL8T1fR6MWCZlO4IUJah9KFI0DQ1x+fuSuNHMENdoleyNKbmCAtoEoU3OuE4D/53HNCCr8aOJBk1xi5Ic/s0LYGUdmLDQ5QSYDQrvoUMwbCQVAfdukkbrt5z9va5rjjgrctGnTafZ1wsdjYWzhOHEWjsdvw0mZD3ZyaB6KrOytNeWcY2VJa/XKT8FkzODwyypJRu8Olnt9PQsv+RGJb0F/ZXYbgsorXSbM6fyD3ExcOidK4f4Y9HvQ2YXsMVgVwo6jd4qe9MXJRwWtsJHMFVCaPDtjBXuuGTIn7CU9yfLNp9cAJyUl6CgeRwakSZMWz8dKsN3QZVIXHiTZQFQmnMcwaIicxe0PCtkGbnOXDUOtfnX8dx0/1okgVeQKO5D9A0kIV/w+o0Mj0K2rgbAAxibotVpU8ghs4pHryetrp89p0qP+dJLK+7qBJCMqN6ht55sahwMYP5oTka9CJHZNgTBobm8O5ltftVTs0EzuJqEc5I1/g++aBrACSUOvwmyrza/VYrRQ1PgDzAVP0KjE+Kpt8hhlUEPV3MuRl/D160l3xbucDmSTmZ9Qn/bK9Dc6rtGOlvqQsUxFnhZiPhHt61zwpe8EGKMuJLamheitgc6YxJpkEXkiQAfevVjDjyKnJWu/K1MaNnmIgZvCW1BN9xgAh/6bl9YuDbJtRIRv0lIQIx/Vh3m8UU7EePgu6tFwIIZNiPagE6w6pdDAzNn8Mi/Nqnsv9L X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR12MB7763.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(1800799024)(366016)(376014)(921020); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HrGEFV6yAk6YanW9AtbZViZB2HmSgeGVbve9vyGYj3+JnXRVy39i6dhmRjuK4ji+AqVLbY5tSu38fLlxguU2eCszXHipOsnVIe28sQTC8J6X4uF7FGkXU2XP/w8CkWmeRmMxcLNljFoX81o2eYUdApywt3hr3vcAgIbRD9C1ZzgoRRXWKktO7AVoHQS9mn8hF6ala+fkalQE5+jntgni05IkY6aeosJqkPm9bF6wHUBYmLZHaOTr3DJEXuyRtOe1Yz4a1CVl91I3Ck5QODYscO7YvpvN272w1DMr6gHtCZUNdEz+3Mi1VjwKM8urj5sszLO19Z8h9iA4MPIntem1sMFbNyqC8p+OeD0PZzmXGsjJ9U9NN8lj4CkjEOULZaABWikDCXkN4v6qq4A5FUJV4R+AFLHTerrt5H9Fx+M69cc/zlvd+gFkgiYwLpzF8COSxGUBMWSJFUSldLj4hn2FZrOtWblwiv87a7h1oBrUJZFWr+xHlXDfO+MGLKWNh1oaAN1vv+7QBMWfyMu1BhN/6JeaVROPRuR4Isd9oevkEjssFJ/xr2PQpFa/0XHuWLx17Fjl6gdqxP3Gc2I1ecizzSnFCcy82sr7rehihN8gdXGrfQqtPkIbrYm7BqgH3iWb2pcDAOgwmgDwBgd3yTDLxtdPwajRCg+kQRLFbxgZLwnmsAhB6zlQ/Mud8Eh1w28J7zyBjrfesOuDya2VRvQDu36Zck8mpCIMxBoX/JIUfgk+ZD0p5X6iVbkjJArqzu7StVE9rVFGewxnmZodbFvwSDVtOMIY3Y6x1KVDtHp0pXbhTp7hijnOmVXDEo2IF8Viweki2F9+l5kbMDaRKUd2ps9fo01czzba2k9ekJ5djlLEzHXKcPxqW2vflT5wHhLd8+sdWk/QhI+/o68u/TusC2ZnOwSaUaAVTUQhogqocHowV8rZB1W7DfmmKuSYzA4gWi/RyGuz0YoBRas4/N+R6Sv1yMBADx0gYaZ1ey/lRi7MD0gN1DSDyclpfXolftKkXKXtk+0V6hmOfYITevrwhhWM4w4gBMbHHUTC3mdpFkpYgYRtoMD8p4w6/FPnKYRz8pnJbJS418XPBY+55qMthUHUmUheAGOCObnWRvgdFPKxfln2vR4+TQ0EsrI28OoowrMFUdVhRN0HDh/hjN2r+nbZXZKWS17wxPXVJsgqXt3t2QL/16CrLb62NZVAgOPz1bWIZ7WJolljTJmEmGrU+OYTeg1m16S6cvBaySWraawAcMe3kvwpiV8U9ZiflGpec5Pcikb4r2Ha8S+ZwNSlv6cvZ73rPyqAV27fPGV74suWkpLahSUbnVJoFNNY+lJq89Cih7oqX30wrUIaCgFrT+dt5aA9L11vIhfoMu4elnz1jrJ5S7+5FZynF2e5rX5h0lprLpPTzeKrm3CNfFoub/Jh/3VnpcSRJzBg+VDnNt0RqzIyWfvmIoNDVDSqIL5MsAdpEJdIBHxmESidac0OmmYk4Rqxir7nIog0cDrriOfXkv/q2QQclfVeTb4t0VyAi8JsaQd2oicYi8BFq7pqHa2qRgGzZ0k3bKULVfe2JFI= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 400cba6c-8055-40d0-84c8-08dcb6714840 X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB7763.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2024 23:41:24.1804 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kWOkmsdZHp0AE/+4O+p2T3ZcU6JvkwDU51lDMMkYkESaoyK0ECkX6p3OMetbZGQD X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9422 For SMMUv3 a IOMMU_DOMAIN_NESTED is composed of a S2 iommu_domain acting as the parent and a user provided STE fragment that defines the CD table and related data with addresses translated by the S2 iommu_domain. The kernel only permits userspace to control certain allowed bits of the STE that are safe for user/guest control. IOTLB maintenance is a bit subtle here, the S1 implicitly includes the S2 translation, but there is no way of knowing which S1 entries refer to a range of S2. For the IOTLB we follow ARM's guidance and issue a CMDQ_OP_TLBI_NH_ALL to flush all ASIDs from the VMID after flushing the S2 on any change to the S2. Similarly we have to flush the entire ATC if the S2 is changed. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 211 +++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 20 ++ include/uapi/linux/iommufd.h | 20 ++ 3 files changed, 247 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index 5faaccef707ef1..5dbaffd7937747 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -295,6 +295,7 @@ static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) case CMDQ_OP_TLBI_NH_ASID: cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid); fallthrough; + case CMDQ_OP_TLBI_NH_ALL: case CMDQ_OP_TLBI_S12_VMALL: cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid); break; @@ -1640,6 +1641,59 @@ void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, } EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_s2_domain_ste); +static void arm_smmu_make_nested_cd_table_ste( + struct arm_smmu_ste *target, struct arm_smmu_master *master, + struct arm_smmu_nested_domain *nested_domain, bool ats_enabled) +{ + arm_smmu_make_s2_domain_ste(target, master, nested_domain->s2_parent, + ats_enabled); + + target->data[0] = cpu_to_le64(STRTAB_STE_0_V | + FIELD_PREP(STRTAB_STE_0_CFG, + STRTAB_STE_0_CFG_NESTED)) | + (nested_domain->ste[0] & ~STRTAB_STE_0_CFG); + target->data[1] |= nested_domain->ste[1]; +} + +/* + * Create a physical STE from the virtual STE that userspace provided when it + * created the nested domain. Using the vSTE userspace can request: + * - Non-valid STE + * - Abort STE + * - Bypass STE (install the S2, no CD table) + * - CD table STE (install the S2 and the userspace CD table) + */ +static void arm_smmu_make_nested_domain_ste( + struct arm_smmu_ste *target, struct arm_smmu_master *master, + struct arm_smmu_nested_domain *nested_domain, bool ats_enabled) +{ + /* + * Userspace can request a non-valid STE through the nesting interface. + * We relay that into a non-valid physical STE with the intention that + * C_BAD_STE for this SID can be delivered to userspace. + */ + if (!(nested_domain->ste[0] & cpu_to_le64(STRTAB_STE_0_V))) { + memset(target, 0, sizeof(*target)); + return; + } + + switch (FIELD_GET(STRTAB_STE_0_CFG, + le64_to_cpu(nested_domain->ste[0]))) { + case STRTAB_STE_0_CFG_S1_TRANS: + arm_smmu_make_nested_cd_table_ste(target, master, nested_domain, + ats_enabled); + break; + case STRTAB_STE_0_CFG_BYPASS: + arm_smmu_make_s2_domain_ste( + target, master, nested_domain->s2_parent, ats_enabled); + break; + case STRTAB_STE_0_CFG_ABORT: + default: + arm_smmu_make_abort_ste(target); + break; + } +} + /* * This can safely directly manipulate the STE memory without a sync sequence * because the STE table has not been installed in the SMMU yet. @@ -2065,7 +2119,16 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, if (!master->ats_enabled) continue; - arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, &cmd); + if (master_domain->nested_parent) { + /* + * If a S2 used as a nesting parent is changed we have + * no option but to completely flush the ATC. + */ + arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd); + } else { + arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, + &cmd); + } for (i = 0; i < master->num_streams; i++) { cmd.atc.sid = master->streams[i].id; @@ -2192,6 +2255,16 @@ static void arm_smmu_tlb_inv_range_domain(unsigned long iova, size_t size, } __arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain); + if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 && + smmu_domain->nesting_parent) { + /* + * When the S2 domain changes all the nested S1 ASIDs have to be + * flushed too. + */ + cmd.opcode = CMDQ_OP_TLBI_NH_ALL; + arm_smmu_cmdq_issue_cmd_with_sync(smmu_domain->smmu, &cmd); + } + /* * Unfortunately, this can't be leaf-only since we may have * zapped an entire table. @@ -2608,13 +2681,15 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, ioasid_t ssid) { struct arm_smmu_master_domain *master_domain; + bool nested_parent = smmu_domain->domain.type == IOMMU_DOMAIN_NESTED; lockdep_assert_held(&smmu_domain->devices_lock); list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { if (master_domain->master == master && - master_domain->ssid == ssid) + master_domain->ssid == ssid && + master_domain->nested_parent == nested_parent) return master_domain; } return NULL; @@ -2634,6 +2709,9 @@ to_smmu_domain_devices(struct iommu_domain *domain) if ((domain->type & __IOMMU_DOMAIN_PAGING) || domain->type == IOMMU_DOMAIN_SVA) return to_smmu_domain(domain); + if (domain->type == IOMMU_DOMAIN_NESTED) + return container_of(domain, struct arm_smmu_nested_domain, + domain)->s2_parent; return NULL; } @@ -2664,6 +2742,7 @@ struct arm_smmu_attach_state { struct iommu_domain *old_domain; struct arm_smmu_master *master; bool cd_needs_ats; + bool disable_ats; ioasid_t ssid; /* Resulting state */ bool ats_enabled; @@ -2716,7 +2795,8 @@ static int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, * enabled if we have arm_smmu_domain, those always have page * tables. */ - state->ats_enabled = arm_smmu_ats_supported(master); + state->ats_enabled = !state->disable_ats && + arm_smmu_ats_supported(master); } if (smmu_domain) { @@ -2725,6 +2805,8 @@ static int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, return -ENOMEM; master_domain->master = master; master_domain->ssid = state->ssid; + master_domain->nested_parent = new_domain->type == + IOMMU_DOMAIN_NESTED; /* * During prepare we want the current smmu_domain and new @@ -3097,6 +3179,122 @@ static struct iommu_domain arm_smmu_blocked_domain = { .ops = &arm_smmu_blocked_ops, }; +static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, + struct device *dev) +{ + struct arm_smmu_nested_domain *nested_domain = + container_of(domain, struct arm_smmu_nested_domain, domain); + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_attach_state state = { + .master = master, + .old_domain = iommu_get_domain_for_dev(dev), + .ssid = IOMMU_NO_PASID, + /* Currently invalidation of ATC is not supported */ + .disable_ats = true, + }; + struct arm_smmu_ste ste; + int ret; + + if (arm_smmu_ssids_in_use(&master->cd_table) || + nested_domain->s2_parent->smmu != master->smmu) + return -EINVAL; + + mutex_lock(&arm_smmu_asid_lock); + ret = arm_smmu_attach_prepare(&state, domain); + if (ret) { + mutex_unlock(&arm_smmu_asid_lock); + return ret; + } + + arm_smmu_make_nested_domain_ste(&ste, master, nested_domain, + state.ats_enabled); + arm_smmu_install_ste_for_dev(master, &ste); + arm_smmu_attach_commit(&state); + mutex_unlock(&arm_smmu_asid_lock); + return 0; +} + +static void arm_smmu_domain_nested_free(struct iommu_domain *domain) +{ + kfree(container_of(domain, struct arm_smmu_nested_domain, domain)); +} + +static const struct iommu_domain_ops arm_smmu_nested_ops = { + .attach_dev = arm_smmu_attach_dev_nested, + .free = arm_smmu_domain_nested_free, +}; + +static struct iommu_domain * +arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, + struct iommu_domain *parent, + const struct iommu_user_data *user_data) +{ + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); + struct arm_smmu_nested_domain *nested_domain; + struct arm_smmu_domain *smmu_parent; + struct iommu_hwpt_arm_smmuv3 arg; + unsigned int eats; + unsigned int cfg; + int ret; + + if (!(master->smmu->features & ARM_SMMU_FEAT_NESTING)) + return ERR_PTR(-EOPNOTSUPP); + + /* + * Must support some way to prevent the VM from bypassing the cache + * because VFIO currently does not do any cache maintenance. + */ + if (!(fwspec->flags & IOMMU_FWSPEC_PCI_RC_CANWBS) && + !(master->smmu->features & ARM_SMMU_FEAT_S2FWB)) + return ERR_PTR(-EOPNOTSUPP); + + ret = iommu_copy_struct_from_user(&arg, user_data, + IOMMU_HWPT_DATA_ARM_SMMUV3, ste); + if (ret) + return ERR_PTR(ret); + + if (flags || !(master->smmu->features & ARM_SMMU_FEAT_TRANS_S1)) + return ERR_PTR(-EOPNOTSUPP); + + if (!(parent->type & __IOMMU_DOMAIN_PAGING)) + return ERR_PTR(-EINVAL); + + smmu_parent = to_smmu_domain(parent); + if (smmu_parent->stage != ARM_SMMU_DOMAIN_S2 || + smmu_parent->smmu != master->smmu) + return ERR_PTR(-EINVAL); + + /* EIO is reserved for invalid STE data. */ + if ((arg.ste[0] & ~STRTAB_STE_0_NESTING_ALLOWED) || + (arg.ste[1] & ~STRTAB_STE_1_NESTING_ALLOWED)) + return ERR_PTR(-EIO); + + cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(arg.ste[0])); + if (cfg != STRTAB_STE_0_CFG_ABORT && cfg != STRTAB_STE_0_CFG_BYPASS && + cfg != STRTAB_STE_0_CFG_S1_TRANS) + return ERR_PTR(-EIO); + + eats = FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(arg.ste[1])); + if (eats != STRTAB_STE_1_EATS_ABT) + return ERR_PTR(-EIO); + + if (cfg != STRTAB_STE_0_CFG_S1_TRANS) + eats = STRTAB_STE_1_EATS_ABT; + + nested_domain = kzalloc(sizeof(*nested_domain), GFP_KERNEL_ACCOUNT); + if (!nested_domain) + return ERR_PTR(-ENOMEM); + + nested_domain->domain.type = IOMMU_DOMAIN_NESTED; + nested_domain->domain.ops = &arm_smmu_nested_ops; + nested_domain->s2_parent = smmu_parent; + nested_domain->ste[0] = arg.ste[0]; + nested_domain->ste[1] = arg.ste[1] & ~cpu_to_le64(STRTAB_STE_1_EATS); + + return &nested_domain->domain; +} + static struct iommu_domain * arm_smmu_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent, @@ -3108,9 +3306,13 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags, struct arm_smmu_domain *smmu_domain; int ret; + if (parent) + return arm_smmu_domain_alloc_nesting(dev, flags, parent, + user_data); + if (flags & ~PAGING_FLAGS) return ERR_PTR(-EOPNOTSUPP); - if (parent || user_data) + if (user_data) return ERR_PTR(-EOPNOTSUPP); smmu_domain = arm_smmu_domain_alloc(); @@ -3123,6 +3325,7 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags, goto err_free; } smmu_domain->stage = ARM_SMMU_DOMAIN_S2; + smmu_domain->nesting_parent = true; } smmu_domain->domain.type = IOMMU_DOMAIN_UNMANAGED; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index 58cd405652e06a..e149eddb568e7e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -240,6 +240,7 @@ static inline u32 arm_smmu_strtab_l2_idx(u32 sid) #define STRTAB_STE_0_CFG_BYPASS 4 #define STRTAB_STE_0_CFG_S1_TRANS 5 #define STRTAB_STE_0_CFG_S2_TRANS 6 +#define STRTAB_STE_0_CFG_NESTED 7 #define STRTAB_STE_0_S1FMT GENMASK_ULL(5, 4) #define STRTAB_STE_0_S1FMT_LINEAR 0 @@ -291,6 +292,15 @@ static inline u32 arm_smmu_strtab_l2_idx(u32 sid) #define STRTAB_STE_3_S2TTB_MASK GENMASK_ULL(51, 4) +/* These bits can be controlled by userspace for STRTAB_STE_0_CFG_NESTED */ +#define STRTAB_STE_0_NESTING_ALLOWED \ + cpu_to_le64(STRTAB_STE_0_V | STRTAB_STE_0_CFG | STRTAB_STE_0_S1FMT | \ + STRTAB_STE_0_S1CTXPTR_MASK | STRTAB_STE_0_S1CDMAX) +#define STRTAB_STE_1_NESTING_ALLOWED \ + cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR | \ + STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH | \ + STRTAB_STE_1_S1STALLD | STRTAB_STE_1_EATS) + /* * Context descriptors. * @@ -508,6 +518,7 @@ struct arm_smmu_cmdq_ent { }; } cfgi; + #define CMDQ_OP_TLBI_NH_ALL 0x10 #define CMDQ_OP_TLBI_NH_ASID 0x11 #define CMDQ_OP_TLBI_NH_VA 0x12 #define CMDQ_OP_TLBI_EL2_ALL 0x20 @@ -792,6 +803,14 @@ struct arm_smmu_domain { u8 enforce_cache_coherency; struct mmu_notifier mmu_notifier; + bool nesting_parent : 1; +}; + +struct arm_smmu_nested_domain { + struct iommu_domain domain; + struct arm_smmu_domain *s2_parent; + + __le64 ste[2]; }; /* The following are exposed for testing purposes. */ @@ -830,6 +849,7 @@ struct arm_smmu_master_domain { struct list_head devices_elm; struct arm_smmu_master *master; ioasid_t ssid; + u8 nested_parent; }; static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 83b6e1cd338d8f..76e9ad6c9403af 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -394,14 +394,34 @@ struct iommu_hwpt_vtd_s1 { __u32 __reserved; }; +/** + * struct iommu_hwpt_arm_smmuv3 - ARM SMMUv3 Context Descriptor Table info + * (IOMMU_HWPT_DATA_ARM_SMMUV3) + * + * @ste: The first two double words of the user space Stream Table Entry for + * a user stage-1 Context Descriptor Table. Must be little-endian. + * Allowed fields: (Refer to "5.2 Stream Table Entry" in SMMUv3 HW Spec) + * - word-0: V, Cfg, S1Fmt, S1ContextPtr, S1CDMax + * - word-1: S1DSS, S1CIR, S1COR, S1CSH, S1STALLD + * + * -EIO will be returned if @ste is not legal or contains any non-allowed field. + * Cfg can be used to select a S1, Bypass or Abort configuration. A Bypass + * nested domain will translate the same as the nesting parent. + */ +struct iommu_hwpt_arm_smmuv3 { + __aligned_le64 ste[2]; +}; + /** * enum iommu_hwpt_data_type - IOMMU HWPT Data Type * @IOMMU_HWPT_DATA_NONE: no data * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table + * @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table */ enum iommu_hwpt_data_type { IOMMU_HWPT_DATA_NONE = 0, IOMMU_HWPT_DATA_VTD_S1 = 1, + IOMMU_HWPT_DATA_ARM_SMMUV3 = 2, }; /**