From patchwork Tue Apr 23 13:14:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640072 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 9D997C4345F for ; Tue, 23 Apr 2024 13:15:21 +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=hDl8UrBXcp0CLWaj87MR1uszCiRfPf8EHI0uc6W7HVE=; b=4m5kwMS6lcCcic GqJXgNW1fM04uizsuDWe6XCZZIz85EW8k1XfgNaGQucOw71Y7R1jsfUPR4ip/qCeBxmJimks9OWJP n4RdQsFwpSI7JpgfK/9E5ypuHeD2X4Fcse0KKpYlPhayyf/wHHVlVdMaHMTYEauRVEQzIxYIfkFm5 bAV/N90GYa03e3qkBU7LaQnuTBgx6uI3Hz5KMZ427AH5TflHVDJ2diaOtP6GR1GZqzxhTM0gEJtVc W0nHjd63KTK4F4hFaidsGHnH1kmcsgpqBRi6XTDWmpKOc0YcUMv7738d1R8g5UmFRIAF0wckx+nhW kHdywhYFo+TpNKijS/yQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFzH-000000000pK-4B18; Tue, 23 Apr 2024 13:15:08 +0000 Received: from mail-dm6nam11on20600.outbound.protection.outlook.com ([2a01:111:f403:2415::600] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyu-000000000Uj-3y7e for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AF6Yxx7lo670RQa4ENeeK+vix3pr01zksgLXWiT5u5Xv7OVJDlmDyifGspEsO51YqR8X6gjoPC1KgQQnobmT2DGfyzDWEpomgt+ESqpQ7stUgKhgDGttG+6gMwdgzGaQDFXNQzlETn69VccIatGgsL8W8dWv2r3AVEV4nNvpQeZ5BsgKsSPXL/Ss1VEYcGSCSxauitt9MmFIUl2qgWa37VY9Rec8bpDcKKyUz8rwW0dq1mj8dlaARkDhPHYKr2Rp1kvvACkSeFJPlrFY1Zk7uJ/2rkNmr+oPBzkrDMy4WYXRvaDeqeUEsVzhDGJm2h7EZ484Txic8FFFGvh+3KwmSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=NuOQM+tk1hBYURM7kywp08aMkUjx537isuXThrhYqWA=; b=nJ037gln6FVfnlAQPMuV0nFqaffoJmNZNDQjIbizIYTxfMLBJtvN6SAslEpdSqFgyiypjQO6sOqQN0Epr+juIfYsZFdP/ZFile0EVjK860mMf+t9tBZUjQO6GAC0sZHN8v+av5IaeM88Fka5fpw2+Ywb3hlVsYC8MbI/+VLpSnYnMVAs1eMa3w1G4qfNZUxEIkCs/CSk7tT9orct4snWl0jN0U7XvraCrlfe7ZwCeOPeWb4LIQ/e2Pgf3+mCJNvCMDYBx/KKf1ZegRaAXt6ddBhofFLG8F7Oi1WRZ27K9aNGcONlt4dpmGSyBSh2AA1qZjuO2WrSqqKhiQMHSExUnQ== 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=NuOQM+tk1hBYURM7kywp08aMkUjx537isuXThrhYqWA=; b=LwfVqJMDw4jjUcB3cd8Sn8NK5gQxBPtyLt1hUZ+riq7MsqdhwCnzDNucZTmB5qb8cIjua4kI5DS+/5/BJDfHybvnMdmTUN9liCF896vmtTL9FJvfQRG109YJlocax2T2oEcul1/UDUxxzSF3oUsYfz7XwqjoQdw6X4h4LeNQpzyqeSktxP5XcLT/UB3yoScBNoLSpBCNE7AjTc5f4zRHYqzAmT8bBHbXJWQN+0X1j+Y+xMHZTzHU1CyrGYjNFATQmdwL8nJ2qVoSO40h+MrCoqt4nTy1iOb82hU7nAwNjfirVdI29WoWcXt4w13/v3VYe1KygFMFFpEn3V9d8l5DPA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by SA1PR12MB5672.namprd12.prod.outlook.com (2603:10b6:806:23c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:22 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 1/9] iommu/arm-smmu-v3: Add an ops indirection to the STE code Date: Tue, 23 Apr 2024 10:14:06 -0300 Message-ID: <1-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0365.namprd13.prod.outlook.com (2603:10b6:208:2c0::10) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SA1PR12MB5672:EE_ X-MS-Office365-Filtering-Correlation-Id: b228a9a7-133b-4c12-9e2a-08dc639746bb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mfoFow/P8eQPPxwmvNNS/45SODvJcgTAznBpXwr8gRKCm86/glyo8zEy6O1nC4EsUt2tn6ezTIuz+OX71qvtR+r9p1oCL608R0ghxQlNjvMZ7Hq4ixyT+ZUMkIj42vKQTtD3OLQ6ep7oZEWUUyPVtNSWR5oOKMC3XXaUbgOrBA5283na1/gv2eb/NvvGx0Inz2Zm2YpOLcUO/EHuAGGwO95USm8vtZFzJbFm/qKUv8ee4kcaOIz+DXgOVyv791r8YUUiBkO9h4bnKoMFN0hkcpf+9wAO2Nc7Rj6g/52sUdIZZEWi2JY9GzQX/zUeqnaf2c1FYROZFwj3uUOzBy5x6NawtDVx2u6W23UDxxOOMQ3IjpGyf+J063+hPzwX0OJpAboSj+DgXqKsFiOjf88L5SwTgTfQ9a9DiV0YfgCa/dC7Yr9vLnmim0ZrW1R3sVRvFEypyjVhQJYOhUnbbXnrNUzbITpObpn1EwzQ8mWvpdIV3u799jfpd/5PScG70XS8rkCwUQzaeov8MY7HQzQengEQrjFPBygQZ4ucfQCYa37k2E0E0FnIBOmN/D+m2ggZQiFk9N8mtyF7v/kwrsDP7cymoRP5IizhponFq5Rj0kB1ZNaZuq5KBWm2BGF+puP8lUOkXaxlL1HzJtZM6KaBPciFE1nYPsw2hacuNbqXq/I9VpJs5jP7w44m5nvHevSxuxhhUmnGWTzzAyGUeoyHIlZ04pWwcfR40AgJrm/FzMGV5apC3Z+pZXh99hDFLXc2eXElo0/powq4KBIku/vHr7llHEFeGMBpO3geivizjUOzVXfB+A2kwWoggo7byengQnEJ16qzQfGOkoyOG0tXhPW2QOftswKfqoUfZlfBhj+0v/1W2Cy+2uPRF6JYjJcK03+QKqfHVgUhkanxxuwteDkeuVeKGvPtOTjugsfFvpR+fsQyEi4nZc851kPm3YtkCWUEVgp0Rfv695df+aLWx7bnnvorOqp9zl4XukNgBaO6c9FS8rrVOpxeoE8LKNeZkgEgWwkdEUvGhmRQZgvFZ9FVlsc1e6q6HoVwGhABzTwOC5fOhaP6Rc8+bLqiqiVtoDnDmsz5KZP3CWvSuC1nqYRHQVxiOCs2+Is3BcfEitvBzFYkrVioTj1ZV94ycwDHy669Z+MzKqVhY0fWfvrh0jJmDduJX5nObgggojU/EX1/eHD+kikZ6We5ue97AWpoaqM8rPCy9yjAz0yyMPhKnHkbmIrVwBkwasdISpzTAqEL5pTpTvwGsKzC8F0Pa9ITC9tId6mH9pPuhLTb2pMqKA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CRorQPGZNoPh/J5GKEd1UQw7V5u99vcN5AOtXLqXbOjmN22po+l4P620MmM7owz4TjXxb5maR9K9o2tUnEWhW3nBtt9eYZXpN/mCfhYe2XphVDzNSRLaArTIOAxpq2K20RxVkIqIgiSLGg03LhFqEJVSvjCzH8zH6UBeMHY13PgjdynPuVOr4CPtN1kNUC/mOvreFgdeZf5zIjU5hZ76NmLXqmANiHQdbUNZWvnSOW5WQT+3mFVuEVh4vLiMjmAthwZ1jt0IyN+2LddWaW3rDzkbyH2yCqm+TjG29ZKVlvWmuCnDxPucgrLB4pNXvS78CgcjQQSB/xT/5D8LXRvr/QFnJFQ4FFxAuXZyuRUaCnCcks8+Hvc2pEAFpb1PdhqF3SNVXOZxBZkTudhiE5EYeTb+dnr4j742h3pZdMp1R0oj/hdy4xKK99AxTNVlwBncntfg7lLOjwQ7rX7lyzixhG9zDslw8OL0wJyjIRkNCx96NLYdV6L1/d/DENWpDRiW05l7enCZ4hETM10JNM9gFVAI/XxaVPDIYoG3QMAlZj/MGvMdrbaWnBlQPGMnx+F0axbUjf7F4JcZrfv0EP1Q7YAXuBqBN3C3NTX2YCOMyV+bq6UVPkg1GDwkqfmiNWvN8xAQy8SbKTQc88a4yBfv6JobjgXk+koL8RvauKcv5hst0qNyFpo+aWfyKPvXwTxfvdcU/IpajYqIFr2Srbz2259+9ww5pjt1B8qVMIAYJ+elXaibnbH89UVND38yuEOShS5ViegIB8q1h6lOvymcCSuuzvVxyBMnNj2YseJHoA4ZPS3vOeoq+QMzeUiPHL2NwXxRH0HZIU0NkcB7dXPDfaiBke3Cx6JcsOXLeGfwNV49utl3Fx0diZxUWV0/jOBSPddY2zZ25mRQFHe0IVepFqm+6JolGxCbtu9IhWr1osKUHifBaz4WclXppk0QkxTclrz6LEk6leuiMhEwwQkKlsowOKrJm0NOgB1BmB0YpmKaUXPt0tJjVXUUS4+f4/2lyxOXJetkaw3Xo0weanAMUib3f6IjfSu1FimyB7jRcvTwr0l7afh7TbroxHf44FNODJpaLMysmOylt47uUdVIiekDIEOWkxZIkE+szhuNJ1r9oVSRE+v2AVArj/lj72KTDeDBjm+qdieaOYkYFa2r3IouTB6cXQhC/DRb6Nmqv1yo1zZGrgz1YVFIrjsxQFiZswkdeLm5wubRW1B+16+XHz/CeH6Exv2ikaC6xIXPD/UR7TYgMu3b+M9MdoAqWHQUI0bDSBMeWbvVyuhfjn0MXzm7WytMpjFz8t4Ia/g5oRa3HFTCJzxEUhsaMGlsNlf2DBgb2BVZAYS1AT9NBFD78QqLW64gpzMzELOC2+RRn3GeQcNCr8zXV6hFMMYaVwLCIWMreTg9BqD7hX37fE/WcfutVPrqmiwDKXctb8kTGxMtcOz3inZ9sy993iQVAH1jdSFHtp2SBQsidtJTNM1TRYTvL3QBqOtueaV+RcwioKCGmk45j3WA/LNR/M++K7+ZrBT7/sNUnsXvorLexOfafPRrdYvulkyUspCPcKZoojom1ZMqNZ8KX8dWHs31wuiI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b228a9a7-133b-4c12-9e2a-08dc639746bb X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.9248 (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: LjFpx+mbvRIrWO7594VQG2JPldU15LLP24Jy9FJ4oFkySsZ+7iVlH0xTTJYIi/Eg X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB5672 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061445_107148_8C7B4553 X-CRM114-Status: GOOD ( 20.75 ) 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 Prepare to put the CD code into the same mechanism. Add an ops indirection around all the STE specific code and make the worker functions independent of the entry content being processed. get_used and sync ops are provided to hook the correct code. Signed-off-by: Michael Shavit Reviewed-by: Michael Shavit Reviewed-by: Nicolin Chen Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 176 ++++++++++++-------- 1 file changed, 104 insertions(+), 72 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 79c18e95dd293e..196aeaf280042c 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -42,8 +42,19 @@ enum arm_smmu_msi_index { ARM_SMMU_MAX_MSIS, }; -static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, - ioasid_t sid); +struct arm_smmu_entry_writer_ops; +struct arm_smmu_entry_writer { + const struct arm_smmu_entry_writer_ops *ops; + struct arm_smmu_master *master; +}; + +struct arm_smmu_entry_writer_ops { + void (*get_used)(const __le64 *entry, __le64 *used); + void (*sync)(struct arm_smmu_entry_writer *writer); +}; + +#define NUM_ENTRY_QWORDS 8 +static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64)); static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = { [EVTQ_MSI_INDEX] = { @@ -972,43 +983,42 @@ void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid) * would be nice if this was complete according to the spec, but minimally it * has to capture the bits this driver uses. */ -static void arm_smmu_get_ste_used(const struct arm_smmu_ste *ent, - struct arm_smmu_ste *used_bits) +static void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits) { - unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent->data[0])); + unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0])); - used_bits->data[0] = cpu_to_le64(STRTAB_STE_0_V); - if (!(ent->data[0] & cpu_to_le64(STRTAB_STE_0_V))) + used_bits[0] = cpu_to_le64(STRTAB_STE_0_V); + if (!(ent[0] & cpu_to_le64(STRTAB_STE_0_V))) return; - used_bits->data[0] |= cpu_to_le64(STRTAB_STE_0_CFG); + used_bits[0] |= cpu_to_le64(STRTAB_STE_0_CFG); /* S1 translates */ if (cfg & BIT(0)) { - used_bits->data[0] |= cpu_to_le64(STRTAB_STE_0_S1FMT | - STRTAB_STE_0_S1CTXPTR_MASK | - STRTAB_STE_0_S1CDMAX); - used_bits->data[1] |= + used_bits[0] |= cpu_to_le64(STRTAB_STE_0_S1FMT | + STRTAB_STE_0_S1CTXPTR_MASK | + STRTAB_STE_0_S1CDMAX); + used_bits[1] |= 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_STRW | STRTAB_STE_1_EATS); - used_bits->data[2] |= cpu_to_le64(STRTAB_STE_2_S2VMID); + used_bits[2] |= cpu_to_le64(STRTAB_STE_2_S2VMID); } /* S2 translates */ if (cfg & BIT(1)) { - used_bits->data[1] |= + used_bits[1] |= cpu_to_le64(STRTAB_STE_1_EATS | STRTAB_STE_1_SHCFG); - used_bits->data[2] |= + used_bits[2] |= cpu_to_le64(STRTAB_STE_2_S2VMID | STRTAB_STE_2_VTCR | STRTAB_STE_2_S2AA64 | STRTAB_STE_2_S2ENDI | STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2R); - used_bits->data[3] |= cpu_to_le64(STRTAB_STE_3_S2TTB_MASK); + used_bits[3] |= cpu_to_le64(STRTAB_STE_3_S2TTB_MASK); } if (cfg == STRTAB_STE_0_CFG_BYPASS) - used_bits->data[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG); + used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG); } /* @@ -1017,57 +1027,55 @@ static void arm_smmu_get_ste_used(const struct arm_smmu_ste *ent, * unused_update is an intermediate value of entry that has unused bits set to * their new values. */ -static u8 arm_smmu_entry_qword_diff(const struct arm_smmu_ste *entry, - const struct arm_smmu_ste *target, - struct arm_smmu_ste *unused_update) +static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, + const __le64 *entry, const __le64 *target, + __le64 *unused_update) { - struct arm_smmu_ste target_used = {}; - struct arm_smmu_ste cur_used = {}; + __le64 target_used[NUM_ENTRY_QWORDS] = {}; + __le64 cur_used[NUM_ENTRY_QWORDS] = {}; u8 used_qword_diff = 0; unsigned int i; - arm_smmu_get_ste_used(entry, &cur_used); - arm_smmu_get_ste_used(target, &target_used); + writer->ops->get_used(entry, cur_used); + writer->ops->get_used(target, target_used); - for (i = 0; i != ARRAY_SIZE(target_used.data); i++) { + for (i = 0; i != NUM_ENTRY_QWORDS; i++) { /* * Check that masks are up to date, the make functions are not * allowed to set a bit to 1 if the used function doesn't say it * is used. */ - WARN_ON_ONCE(target->data[i] & ~target_used.data[i]); + WARN_ON_ONCE(target[i] & ~target_used[i]); /* Bits can change because they are not currently being used */ - unused_update->data[i] = (entry->data[i] & cur_used.data[i]) | - (target->data[i] & ~cur_used.data[i]); + unused_update[i] = (entry[i] & cur_used[i]) | + (target[i] & ~cur_used[i]); /* * Each bit indicates that a used bit in a qword needs to be * changed after unused_update is applied. */ - if ((unused_update->data[i] & target_used.data[i]) != - target->data[i]) + if ((unused_update[i] & target_used[i]) != target[i]) used_qword_diff |= 1 << i; } return used_qword_diff; } -static bool entry_set(struct arm_smmu_device *smmu, ioasid_t sid, - struct arm_smmu_ste *entry, - const struct arm_smmu_ste *target, unsigned int start, +static bool entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry, + const __le64 *target, unsigned int start, unsigned int len) { bool changed = false; unsigned int i; for (i = start; len != 0; len--, i++) { - if (entry->data[i] != target->data[i]) { - WRITE_ONCE(entry->data[i], target->data[i]); + if (entry[i] != target[i]) { + WRITE_ONCE(entry[i], target[i]); changed = true; } } if (changed) - arm_smmu_sync_ste_for_sid(smmu, sid); + writer->ops->sync(writer); return changed; } @@ -1097,24 +1105,21 @@ static bool entry_set(struct arm_smmu_device *smmu, ioasid_t sid, * V=0 process. This relies on the IGNORED behavior described in the * specification. */ -static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, - struct arm_smmu_ste *entry, - const struct arm_smmu_ste *target) +static void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, + __le64 *entry, const __le64 *target) { - unsigned int num_entry_qwords = ARRAY_SIZE(target->data); - struct arm_smmu_device *smmu = master->smmu; - struct arm_smmu_ste unused_update; + __le64 unused_update[NUM_ENTRY_QWORDS]; u8 used_qword_diff; used_qword_diff = - arm_smmu_entry_qword_diff(entry, target, &unused_update); + arm_smmu_entry_qword_diff(writer, entry, target, unused_update); if (hweight8(used_qword_diff) == 1) { /* * Only one qword needs its used bits to be changed. This is a - * hitless update, update all bits the current STE is ignoring - * to their new values, then update a single "critical qword" to - * change the STE and finally 0 out any bits that are now unused - * in the target configuration. + * hitless update, update all bits the current STE/CD is + * ignoring to their new values, then update a single "critical + * qword" to change the STE/CD and finally 0 out any bits that + * are now unused in the target configuration. */ unsigned int critical_qword_index = ffs(used_qword_diff) - 1; @@ -1123,22 +1128,21 @@ static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, * writing it in the next step anyways. This can save a sync * when the only change is in that qword. */ - unused_update.data[critical_qword_index] = - entry->data[critical_qword_index]; - entry_set(smmu, sid, entry, &unused_update, 0, num_entry_qwords); - entry_set(smmu, sid, entry, target, critical_qword_index, 1); - entry_set(smmu, sid, entry, target, 0, num_entry_qwords); + unused_update[critical_qword_index] = + entry[critical_qword_index]; + entry_set(writer, entry, unused_update, 0, NUM_ENTRY_QWORDS); + entry_set(writer, entry, target, critical_qword_index, 1); + entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS); } else if (used_qword_diff) { /* * At least two qwords need their inuse bits to be changed. This * requires a breaking update, zero the V bit, write all qwords * but 0, then set qword 0 */ - unused_update.data[0] = entry->data[0] & - cpu_to_le64(~STRTAB_STE_0_V); - entry_set(smmu, sid, entry, &unused_update, 0, 1); - entry_set(smmu, sid, entry, target, 1, num_entry_qwords - 1); - entry_set(smmu, sid, entry, target, 0, 1); + unused_update[0] = 0; + entry_set(writer, entry, unused_update, 0, 1); + entry_set(writer, entry, target, 1, NUM_ENTRY_QWORDS - 1); + entry_set(writer, entry, target, 0, 1); } else { /* * No inuse bit changed. Sanity check that all unused bits are 0 @@ -1146,18 +1150,7 @@ static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, * compute_qword_diff(). */ WARN_ON_ONCE( - entry_set(smmu, sid, entry, target, 0, num_entry_qwords)); - } - - /* It's likely that we'll want to use the new STE soon */ - if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH)) { - struct arm_smmu_cmdq_ent - prefetch_cmd = { .opcode = CMDQ_OP_PREFETCH_CFG, - .prefetch = { - .sid = sid, - } }; - - arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd); + entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS)); } } @@ -1430,17 +1423,56 @@ arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc) WRITE_ONCE(*dst, cpu_to_le64(val)); } -static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid) +struct arm_smmu_ste_writer { + struct arm_smmu_entry_writer writer; + u32 sid; +}; + +static void arm_smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer *writer) { + struct arm_smmu_ste_writer *ste_writer = + container_of(writer, struct arm_smmu_ste_writer, writer); struct arm_smmu_cmdq_ent cmd = { .opcode = CMDQ_OP_CFGI_STE, .cfgi = { - .sid = sid, + .sid = ste_writer->sid, .leaf = true, }, }; - arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); + arm_smmu_cmdq_issue_cmd_with_sync(writer->master->smmu, &cmd); +} + +static const struct arm_smmu_entry_writer_ops arm_smmu_ste_writer_ops = { + .sync = arm_smmu_ste_writer_sync_entry, + .get_used = arm_smmu_get_ste_used, +}; + +static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, + struct arm_smmu_ste *ste, + const struct arm_smmu_ste *target) +{ + struct arm_smmu_device *smmu = master->smmu; + struct arm_smmu_ste_writer ste_writer = { + .writer = { + .ops = &arm_smmu_ste_writer_ops, + .master = master, + }, + .sid = sid, + }; + + arm_smmu_write_entry(&ste_writer.writer, ste->data, target->data); + + /* It's likely that we'll want to use the new STE soon */ + if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH)) { + struct arm_smmu_cmdq_ent + prefetch_cmd = { .opcode = CMDQ_OP_PREFETCH_CFG, + .prefetch = { + .sid = sid, + } }; + + arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd); + } } static void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) From patchwork Tue Apr 23 13:14:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640070 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 700F0C4345F for ; Tue, 23 Apr 2024 13:15:17 +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=EWuy3etjSFxtW5TZgXErXY76+HGTZq9nOOiDN13p/tE=; b=tRsTfIGJSh6rTQ uq9EfbESiglyzGZaaxxNz5G4eCInFd+v1kPn7dAOyMB9yjexKeiaKOofianojJdZo3pVjuRBFky26 qQk1bzvMb96kaI5Pic2eo9HNJBmxNDRN+vjdRWh5TQqXP33jvszDyAlH/se400Er7Zw84hw1Xrsd+ aw60q4RtzTlDHQd/WXgXFZ+jdw5dGD/TOjXlHuTKQFu0Nb8KG2GESu/9FOBgUyAgkCysfAlJnxpHq FtvTWcPkVqnK+DYmOzPZJfB80OGRo1f/5vYPGHoMqlXewLQFa3LBdguee/k+Ms5YatCmgmVw6ThKc ya3GtFQzvUlTuTDJkgYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFzC-000000000lJ-01In; Tue, 23 Apr 2024 13:15:02 +0000 Received: from mail-mw2nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:200a::601] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFys-000000000Vz-2Oip for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CDyA+lh/GSPEdy+DyeWdNgTKumPPYXAJazOR+cLZCl1DnDJS1Vxa0fLRTyFbNIaST8lcKa1t7STcGibQCEfBqLMCEGNUzR59/vsGIU9Eq3XZtYDW9mGQT15aZ70ERa1ePMcoE22zIc0Nzp8fx1xULEekhye/ZFaUrjW+Kp8uc8Uy2HAXbQyM1+YVUXygpoPiuLdT+svpcqaEJAck/zZrlcUQEQvy5iVpe5craDuF/N3sMxKA4ALzT+X8Timv4J1H+jctYZ6fmKTJsO1EJOtj76/6RETCdprbHvDm44otE5ggcsh/zcA7ZX06MmZUgW0POYa1d2E5Uk3vshqUBSO8oQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=+6ebWMsXICkuU38FuAdTDthYKSl5QETMUq6+wiunarQ=; b=Pxsc803PxseswaCvaY8JyIOldq4XqnxOKyW6Y0j0xCfg/F0TPVZ0rpCGLYzotOeHj/s6pgT52m0S7F+boT8xYSIFp8tk6iPNgpI+QdjsJVrU3kM/x1xwEmyMEY8WatClFdRC1pz3ePPDN/u2bvIfhmb5JZ6FqFD0QkO6VtDexW6F7BVnN9LYlZDfMGtyF3u6HHI8ClB5NfRJZ/6mcI/GDB4Glm8OTNnhuUARwZefS3snTb0xzndvocnZ2Qnm5U9bA7tRgmYo7Itr97VArXJFpl6neUHU9d0byJ0khYLnqlrrLDtmUMSgGZRrXJqCc7D+ecKsSGVpaaPK7W4gx51vOg== 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=+6ebWMsXICkuU38FuAdTDthYKSl5QETMUq6+wiunarQ=; b=fnQv8+PvsOmBTL9dp7jRlEEMML6e9TPuB9GZQOfqv1SfNhCMDHFgK5R4tkXnKBPwvXcuGKKtkvuK0PMnvy4zi4rwXnKj8MvBSBcT6ecV80U6q0wJR7qV3+T5iBCV/z/sYSWJyXlODTCmJRot1+jd+HGjjWh/wLDEvITvgwijvsCLZvp3Ihj+zCCl+ZuJ25m0D9BBrurY11C9VI4P8N7tfA/q+5hfK0DpFJqcAJuYXGnFaQ/4l5MGvCCu4SN1twUbUBtjO2nGqJ3j6ONzpZWDNPQcN3MQeu0plLMK5MtZpTVxNiZ8GO2iV7JHmlv0nEa3UNz5uKxO2Yac/6zSdiHehg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BL3PR12MB6593.namprd12.prod.outlook.com (2603:10b6:208:38c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:21 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:20 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 2/9] iommu/arm-smmu-v3: Make CD programming use arm_smmu_write_entry() Date: Tue, 23 Apr 2024 10:14:07 -0300 Message-ID: <2-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0254.namprd13.prod.outlook.com (2603:10b6:208:2ba::19) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BL3PR12MB6593:EE_ X-MS-Office365-Filtering-Correlation-Id: 9ead5d18-da76-4eec-b07a-08dc6397467d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zk2rSGFUtjZgGRWbgqovVZmPrQe51YPHmrI3FAMvN06n91p0ZHjc24nTtfOWGd87LJ/f36i/CDzDGDagB6UrrT1GXF5fQHaxO86g5jF6VPpPuayeuK2ijzDIobxPXZHsSGbpao2Mvlscv66F7ZPlWLAenBCJbJ/+XSLQal7XZM+Tlv1s/c4DzvxsI7Ts06/Ol5Txy2iLhn+JjuLGFwQl5dHWZW+hHgzFx+HXr7TL3wl9HRJfY2xkOtYyidVGyW/x/lu3irf0QxRXSZTNL+l7LoFHPWwkkiBj7GAdZ5S6uERI0f9fBcGPW7wH3BPDG+gvpY2l7wshPuSmBekmnNIP1b8k8cwYtlzId2FPbS0yYtiJGSViXLngqGdzVxP7rHb5jxovW2CyPCbmaLJofXz714cyEDm+STOIPqZ2trcE+i2RyR9uQGb7g+4XKMJo4Gb4ruIQXbxjjTPdCy2j6TvlQQm9vQBaHLKxXe0PqHRLpraAm13jlAF+ellwN83ONUH7yEkY8q85G1VQsheu3dh3TilIvbmYdNoVKF+bxKerPwebDXv7dUmcQRaZ5xzyBA6qZABuThHfeLVbklKRIQ+r+KNeTNX4N+1G1fiSfB3I4xuMcEvw6W5oeCPDSCRI5NVhqct3OU+UwMspTnwAKoYxF0vXufsNeVSaVcue497t0JI0EDwVqtr1J6ornePRd9L9xHoTfu0PdO/a8N1NyFWdKiNqriwDJOrN5HpI8KYnwFmnQIvA6y0axrC+RX/6Zfe3Jy6AwxUVznTgfSMhNgi7jzMXegywWibGE1yDoDODWcygA2mnkyRlEvHkO9RRM79P/79UM88fKCNQJaxhnOZYFCBwaQXBS34AY252hUGxJlIOpB/mB7YJ9PIkSrFT0gY/avxbjo6j1xJ02L0YmcrJPScSIVQUebjYqXo44ZZqOigH5As8+O780Gu+mttenzbPF5FIXKrw37r2SguTqYdtk12UNbNGVoXNoXQZ9xyJ1BfxkOhWsoe2pH0Fxab8qajMVI/1bF12ysakE/GtcAl8B5dMwFUZ1yTQ9LuyuKU+KDUmNJhCQYuQrzwRSS0jgrSg68kqiUd+W9Y6/F4p712pOEsiXVRZ9+ooLxS3k4uadHm0IWAr/KiI4yon1VlpnKRJtG2BTQjqWSeVk3EJswqt4Btck27f9/Hy8siOLcjrOCZqjYu2VxvGQlIMj98+HbPKbvzsLBD1+B8tc8xPw7f8PC1g6lqS9vOvel6/KUZMESXmlM6eHVzi1Nz90m+SYmciaI8X1//NozjmZ+q4E3xbZg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(376005)(7416005)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m4EM1JCCygtrhO28EqmvGJsXndvWqtmUPhLZvPr0BaFNHOCwNwCBL7CvEky0A/8WzNI//DOhohsU4LZ9V7T3FQZwcIJVeDXTXoHkHoy10eO10tNxKmOpC3plb4yu7eFg/cLXqa6AnE8KITkYdLN7tEbqEzogw3IiTlA6V6eETpmB5CbI24R3baTvqk3Xf/+hG3VWUUbckgMHVdLOz4wc4Dy4xJAzsE0QV/xd6BaCz3FPDpK0FNs2lur/NlaNzSBrVyzrxXpZZyQrKquEESGofD18dlWSM4VViI4YY6P0JoiaP8hkKEE6h4aWCjHLgXKomqg9PNDAk+WXrpYwCSgZNpV2K2bgL85eUeBmY+35OQ6qlPCl6dx7mbjyP/+jtoFhKasKJVXEF+Ou0fDszLPZnqj3fEqRCgaAe1FgtSVcCEXjgvwuxZTFiAK4ecnoRYiLpfJg8vzCe/TY1d4vL+BDFLmJ8RXEy5WMiW5Wu/JfnrrEGb94k4w7D4XzLuxl/OsA1f+v1uo/vsaHHoLYlz4CpDF6tUA6KK4cUpdaOFOiFGYM+7+VHNxYzejGHmgIewg+P0BjrN+kz6hjx9XohM3NE6s/QDZz7uLtAD8+r2J1042zNdAR7yDIZ2qsR3SN6m3wn8TvtkN1ZbP2tDTq0N1XqDMSPxwxh3c8EGBqay/q9ZL0yUJZwxgiqI0BuhE5TmKckR4TSN6e3ZBY8OEhWTimrj3ycE8lM7cFWZiCTR6F+uqpNF1C50Kj1eC74ncL8oDwIxOI7QPhxXmDa6Ya5DlLXwPlZXrKEW+KwHVrSbHy1HZjdslUuzwSjDol2cal1SPKbAqPSN68uZoOcg8dqMwj0mxBKB5xSpYTjCNLwFYALystuS2wK+P0jPdkjIFMtCFrfmBfdk/kKxiAAxvWb3bjk02GOjKt00krLgtLQFRrFRHiFPbBN5Pmc2yqz+/wf9fMo7eAp5Yv58GTTsaCxNWz7pdJU+x3TmxMeBfpC77HnlA5JmQlMLwDuUw5wMCRhAtsQ72pKr6zkR+qrKveXvk6JVKIhEd5uC1kNHgpYZy2nUoPuNKVBzCtsbt76BH8VnZdR44AmgcNQ24KXf4WjezHmVWctx/lMJr9PFG9dtgx4wyqDubtAWOKE/lqTTupHewfwgXW6xqegb850pT7AP1lnf7lOji+V4+kQQ5gHyWDR0DSXD4Foafu9JZ9IAKcUDZJeuRbFMBDUvLMPkT93eJwOtV4N5A+i/WSHr+GK7SkcW+55MW9NIX5yzkuDNHqL3UWrDJld+K8UMwWHCcoleArObSc/MITbWLhHbPmkY2N3pYxMZlP5M6wdClmuB+gk5611c34ygnZ5KB/b5uf1Npjse3+L8WYKt3Ub6oYWwc8ChXYnD421VjOIhyn2GVmUPHVJ4uV9rRPbcDDKdQTfYQoceoDNKEKgDzd0bL7aFKH5g/x9Gc0F6kfxZUhep7gVO0IbLyEEorRqGm4iyFP7n4BwKG3vTXYvhv5IqI0/IWZKZHxwTUNkjV0jzZs5yOMwxacXlLx2T9oXhLlxchOHMV67GkQYfxhQhv2rY80m4Axlw+Gi8o1uC9Fjcp5m5FxOzZn X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9ead5d18-da76-4eec-b07a-08dc6397467d X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.4583 (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: t29gfd7zcIyWsHx1ale3FA1BR6psRlJelWSRKNdwbFkM6i9ecVvS7jrM83CinuyG X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6593 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061442_698468_F066D4BD X-CRM114-Status: GOOD ( 22.10 ) 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 CD table entries and STE's have the same essential programming sequence, just with different types. Use the new ops indirection to link CD programming to the common writer. In a few more patches all CD writers will call an appropriate make function and then directly call arm_smmu_write_cd_entry(). arm_smmu_write_ctx_desc() will be removed. Until then lightly tweak arm_smmu_write_ctx_desc() to also use the new programmer by using the same logic as right now to build the target CD on the stack, sanitizing it to meet the used rules, and then using the writer. This is necessary because the writer expects that the currently programmed CD follows the used rules. Next patches add new make functions and new direct calls to arm_smmu_write_cd_entry() which will require this. Signed-off-by: Michael Shavit Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Moritz Fischer Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 96 ++++++++++++++++----- 1 file changed, 76 insertions(+), 20 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 196aeaf280042c..1a61b47eeefe2d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -55,6 +55,7 @@ struct arm_smmu_entry_writer_ops { #define NUM_ENTRY_QWORDS 8 static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64)); +static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64)); static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = { [EVTQ_MSI_INDEX] = { @@ -1230,6 +1231,69 @@ static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, return &l1_desc->l2ptr[idx]; } +struct arm_smmu_cd_writer { + struct arm_smmu_entry_writer writer; + unsigned int ssid; +}; + +static void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits) +{ + used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V); + if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V))) + return; + memset(used_bits, 0xFF, sizeof(struct arm_smmu_cd)); + + /* + * If EPD0 is set by the make function it means + * T0SZ/TG0/IR0/OR0/SH0/TTB0 are IGNORED + */ + if (ent[0] & cpu_to_le64(CTXDESC_CD_0_TCR_EPD0)) { + used_bits[0] &= ~cpu_to_le64( + CTXDESC_CD_0_TCR_T0SZ | CTXDESC_CD_0_TCR_TG0 | + CTXDESC_CD_0_TCR_IRGN0 | CTXDESC_CD_0_TCR_ORGN0 | + CTXDESC_CD_0_TCR_SH0); + used_bits[1] &= ~cpu_to_le64(CTXDESC_CD_1_TTB0_MASK); + } +} + +static void arm_smmu_cd_writer_sync_entry(struct arm_smmu_entry_writer *writer) +{ + struct arm_smmu_cd_writer *cd_writer = + container_of(writer, struct arm_smmu_cd_writer, writer); + + arm_smmu_sync_cd(writer->master, cd_writer->ssid, true); +} + +static const struct arm_smmu_entry_writer_ops arm_smmu_cd_writer_ops = { + .sync = arm_smmu_cd_writer_sync_entry, + .get_used = arm_smmu_get_cd_used, +}; + +static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, + struct arm_smmu_cd *cdptr, + const struct arm_smmu_cd *target) +{ + struct arm_smmu_cd_writer cd_writer = { + .writer = { + .ops = &arm_smmu_cd_writer_ops, + .master = master, + }, + .ssid = ssid, + }; + + arm_smmu_write_entry(&cd_writer.writer, cdptr->data, target->data); +} + +static void arm_smmu_clean_cd_entry(struct arm_smmu_cd *target) +{ + struct arm_smmu_cd used = {}; + int i; + + arm_smmu_get_cd_used(target->data, used.data); + for (i = 0; i != ARRAY_SIZE(target->data); i++) + target->data[i] &= used.data[i]; +} + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, struct arm_smmu_ctx_desc *cd) { @@ -1246,17 +1310,20 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, */ u64 val; bool cd_live; - struct arm_smmu_cd *cdptr; + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr = ⌖ + struct arm_smmu_cd *cd_table_entry; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; struct arm_smmu_device *smmu = master->smmu; if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) return -E2BIG; - cdptr = arm_smmu_get_cd_ptr(master, ssid); - if (!cdptr) + cd_table_entry = arm_smmu_get_cd_ptr(master, ssid); + if (!cd_table_entry) return -ENOMEM; + target = *cd_table_entry; val = le64_to_cpu(cdptr->data[0]); cd_live = !!(val & CTXDESC_CD_0_V); @@ -1278,13 +1345,6 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, cdptr->data[2] = 0; cdptr->data[3] = cpu_to_le64(cd->mair); - /* - * STE may be live, and the SMMU might read dwords of this CD in any - * order. Ensure that it observes valid values before reading - * V=1. - */ - arm_smmu_sync_cd(master, ssid, true); - val = cd->tcr | #ifdef __BIG_ENDIAN CTXDESC_CD_0_ENDI | @@ -1298,18 +1358,14 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (cd_table->stall_enabled) val |= CTXDESC_CD_0_S; } - + cdptr->data[0] = cpu_to_le64(val); /* - * The SMMU accesses 64-bit values atomically. See IHI0070Ca 3.21.3 - * "Configuration structures and configuration invalidation completion" - * - * The size of single-copy atomic reads made by the SMMU is - * IMPLEMENTATION DEFINED but must be at least 64 bits. Any single - * field within an aligned 64-bit span of a structure can be altered - * without first making the structure invalid. + * Since the above is updating the CD entry based on the current value + * without zeroing unused bits it needs fixing before being passed to + * the programming logic. */ - WRITE_ONCE(cdptr->data[0], cpu_to_le64(val)); - arm_smmu_sync_cd(master, ssid, true); + arm_smmu_clean_cd_entry(&target); + arm_smmu_write_cd_entry(master, ssid, cd_table_entry, &target); return 0; } From patchwork Tue Apr 23 13:14:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640067 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 43700C4345F for ; Tue, 23 Apr 2024 13:15:10 +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=ifBogblmcPSyCUoe3Sp8KsfhJ2pmpuOBROolF65yfZc=; b=Lz71OZ2ldgzTX3 s3WmnmaQhGt2JAhCW/ycjo9+dt10srSPCVJ46QuKIG02NrxQKzmKQjihlvPvcd1PwvFVakfsBrF/y /Ftiu7SHfj2FnW7x/sg8luPWddTBHnqqoVIqOQp+5+WWAtIt68T6FhRFEbRU6cd7FdGjTHJJ6wJk0 Hpn8xjZvmpfmswWA6JB+CGX2Hsh7BJXU6bDHqfeiF794nd1+XI5Uvt/HEev2E+Xo6NdQEf5lPdRGc fcRZojkuuDcceXqKqTNGkq/4qw90/PJmlL9dwFiJZ03oB5yMtAQ9JXgI+rk44IIlSYuEBhBHVjfSc SnUHwvIRK18mWP7hZBYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFz8-000000000jR-3wZ1; Tue, 23 Apr 2024 13:14:58 +0000 Received: from mail-dm6nam11on20600.outbound.protection.outlook.com ([2a01:111:f403:2415::600] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyr-000000000Uj-2aNW for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i1JiRTMG9ULnw5Wysy8ZVB2UIhAmb7o+PF1EnGchhr0PMsFIoSyukaakCVB15w1agHv5ESu8YBnXYwmRFBE/TczLo5BuCEoyvaagkCiKf8nakFFS+EmIXww1LD5hYz9HSz0OS7NnbEKs4408yc4hRFYgYveguTNdvl6hpyCB4n/9k5rQ8m5FJqGUw5xOxwL0H7OKpJ7DSgRriEh1oi0tGral3e9nBoEbJb3/CgZGXEKG7a5LX3aYGCI+2JiC+Mhb56XEzaWkYZIxcDxL2d6e78DHVxWk771+AxNHZXpTZ5SUrD8eGxbcb3Z+77rZT7x9TWSYVr+RD8LczEgJBOFVIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=C3Kjb38NvPbvaxls64HEvyCmbmfH7U0ExoyBwEba8nQ=; b=YuygVhXooJjluaMsaObp8b4syCdCult8nolm+USIVenwAfYe/ZWt75dqILrYdHiBeMq/elWgmvp0QbdLbDX3jqhuvgr7khtmZmK2YZKe3MeMV5kQiWU8rU0885xn98mTma7fG7z9KGpBKIcIezTUQ1GQnQEwxKCV7dtuOBsWWta/VVlmFiPTRArgmaTZrnlTYOxTblQT8OBWCjxa1JZmGQS4pgZoA0IMN8cvU7txTWbU/Y0lUmx1ESB7fhhTvd7NzAQuRKf9RTqKpcXdItuVW/f8gw/9P7cr7B/NcCVZh2aTFA4HZOZ73D26y0w7vSiGd4Aimwsx9iJV/SRrro3z3w== 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=C3Kjb38NvPbvaxls64HEvyCmbmfH7U0ExoyBwEba8nQ=; b=BrsW68SmKIMtz4lM8TATbHsF3XRDZgo4N0L8yTEo4Q21uHXisJM4Ogk7sqN+nuyexJcVYsjWWHzZIgBc1jAjLrI/hrowkRQosdkas4dPWiAMi+cXoDIE8e3OLZIz7g+c/5HTeblUkWxWrnrvLruwyPxa3RDgsq9eok1xPzAvEcFPBYvBWmW20ik8QV/X8bkeZ2ei1R8YKt1jAFoann+h24ZzGK4GELv54l6fmo839AphfpowP4vmzbBYGwuJMmMj1lVHM7i7RFYhWhDhag9bzG7rOH7Gfwn8yx4ArMc+Xz5NNCxffKa715wPUs4vumUCYNFAOkVBPzEL1cekpRB4hg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by SA1PR12MB5672.namprd12.prod.outlook.com (2603:10b6:806:23c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:23 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 3/9] iommu/arm-smmu-v3: Move the CD generation for S1 domains into a function Date: Tue, 23 Apr 2024 10:14:08 -0300 Message-ID: <3-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0366.namprd13.prod.outlook.com (2603:10b6:208:2c0::11) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SA1PR12MB5672:EE_ X-MS-Office365-Filtering-Correlation-Id: 3306f4cf-7ef5-413a-9263-08dc639746c3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0GPSG44pNRhgeWKmOmhul8H/+U1wAPpM8BcsDutDkNJ5D0F+QlSVEdf/cwIQnauNmeyVNJMECtHFkHpKGXyIFSCQYaaf1wzOLM1KuMt50fFcfYHISDimx9s59rLp6RAXo1k2XCHaoFv1YweAvFsaf/Gau04SsiYvx550EqJ4bE7Z6dMFTbD9/kujp3QBq3v8bJ6TdClfg+K11N6B11YJ1zgRMJrUz0RouM8tarTzAnd8PMCy8BkxMFsDJQS1OV/UMkeAEIjsgFPMofjxCGhnDVynERa1H3fAj3yvmg7YtI5RZHtTJU5Hg9/FChIw7FEtT1jXYz6U549SOyniZJrfEDx9NzKefYTlpZub/JoJP2KiRXYSysY6Szgm27jDG78XGSvaN9cmYnemCzI4mp0D/JuvbTvhHxOwQr+8hXl4cttyR4aYkWdT0slYVw0hrlnl2IWzSrGrlq4+Y+mrvsiMFj5sp/x+sZ2ipwRDg1O04W4ky5iSxj/L55v41viZmwTmU21Zn9JpzzKlcc08YxNlAL4ah3pKGnyMN4BRyij/0/m3U0kNCl4Fl5Y0aSw2rJrAINyvWRj+PhWywT3Buz60hpmwF5/LYKWNCeDkXOjs3i3R++lPF6hPVp9rL/9uJpIkVQXXIPpO+Pjn65luZxOnKIGn+k4ydNzYfQ6+wEDtQK1/R63BiHFsQmhw8JkzlcSB2Icdk7JPYB/4fa175Hm69ZodLUSDHqCNj85HtbyjjQ0ZuWsHdEYHz0lh3yMaG4Rw1ykRsq5IblGXCApbCa9TUfXQBQMO4VEAwXR4JEHG6CYSl1OC6qylVxiNGM4ER1jno5e8CHHdI2PXNua4XYVbkU9eC+nJXMpZ7ZVzR33EnTLs+SNvKONLJ3MppJsRdsJn+yD479mxGQWArl4kwiZdFU95oaaHL/BCyWd2jodfAB5xlDJCbz/vUOYZsuQDtMa6YfdoScrGMtSm3s9fLfiBJLwfNP9M8AqlUCtkps9/ChoSnQa0GPS1xadHaSVxgjzpGNyuObLchw5Hb1r5nUvC5t0+VVrIXhhp0fjkYjyUMGk/XF0taQz6mKVbECmDLzVoyW4sZFu6M42L7JQUvHP02DA/Tg1jPffvPkIPAMVG/FqU/U1rMc0Skn5gmYTZ2VGo7SWjuZr9v3tyrz0AV3HciqTHnBrRkULPBRCL7X4ORySeHbNcqm8BtELYXG9OBNKdAnljN5pnjQnfX1ASj0XgvJxfSWtYHy5C17Z+xi71Yf+/OnDxqYhI5iHYGIvF3u3xivnG7uVZTBiISIjPbIPDOw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: J5KmM4bT4G6Hj6mMkLq58rJ275YaITkzo0pOMAbH6XJMCJmM0XO4Q3RTGuYj3kFcgN2v2mFj6Mp/3yBpgprSp3jX+3+Sxfj7n1bn622tDhftN0RxzdMiy7Vv1opvg4jpafpfRuiUMeOYlvJvRnYE3YyQa94l007D94GDgIWnuEm7LRHUEQPP9yUZMD9vsorqXpDWlkDTabTfmjQv89Pj+o2/gb9rz+4+quWwwppNWeL170FHfrl33KextIUgwOPVAKhmglO/QYajzY5DgVK7X/FYUENds/EEZ9KbUIiCu2D5ytC+J0mG1hLF/07A2h/bHSOqGxoXXv35R/KBtBB1v2Rt3SqIdvw4admzNK7UuIWScfHQ7Xy0dURh9qvOYWkYcDYkd4UTEmmmQ+NFzVlTGJ9k298JC2mvkGSZsrFEazQG8GYGhikts+Nm2zS/yy29R8ttCpv7UwFXb3UAx65/xCc9ZmkTALm0O/VMSo9DZvZnPdYq23qsunDPRm35kxoCuWV8VQk+eNcjaUarWILM/ldprgyEyXFw9k/Zxeb8dtsjcWU3FR8dF7m2ta9PnYV79N6Ci4Zq5lsdjTaB/eibHYSGOSmi/38zjd5tveoDagOTQI81Ym+5a7woViMMoWAXM400LVfcDsuHf5uwGtkcAp5tTjm/wB+CMj14yzhp0kXgIQnTRmFkF4Va31wwXlJlGF4HAa1GF/hnCC0gzzc7A0DdFdVWQW8JKm0w/nOucJY4uMpc4UO7Jkwn7/jkIteuhYUus/rSvkvimkLUhg4YV3I0wJAPOSS551x2QaZTH1evLi2SzXH+u9OwGJas+XKXlc416jZxHwlV5qhtPRHqd+7J8d8SLYVlWEdm0x5GAbiIblAbvdI67BsfiQY493DAyZPsz9RczF0Y+2N9brGL2or29n0AL+4+O0vIIcz3mm0Bwta4kMuWf+XPAqAUevcarNxvAZ0V1FqYX75/quJjDPOmzDPUyRy5c42goS1mSKUJJHON6yNgn0g8wRQUiljlcqZBPCupXHTy/fGCzwfhbCuXvu/iEdtdfB+Mk0ThcAgLXLKq+sQOff51/8plp5aam9k7lqWds5ZoZJyXsEiH6WvGyoZ0Wl41QYSSZoA2I0d0+0puh+2a4DRQEkSPockYBTI7fJabFGwgh0Ogw0NPAA638DOpq1483Flbn1mGPs9m4yaIT2eY9Wsc4h3kOE6pTe+64hxriojkjmqpfYufjP+A5nloGP11QcfZaxbByduCI6HyA/yxviEfT74gYWKMd3YygCqf7STUYUWInOfOobwWtQ+3x//vUqAmUsSEC+hTpurZCAX6wcJBtxYkmQxpKkVV8T1EXitKIE99H29BZepMD+fUydnTJbr17U3BIzGby6gaDpI3v4mJmUfsF5lwJZnEMeuGYLG538eGr0eYgFd6vvMIZhtf/RnF4Um7mtULCRGu9yqg0Ncua2Q11l0okGqIpxpAmABVWJqbNFeihrdensC3+ooseOJ9odtToY4D2mnO6XeBqvN8wCqJ58OTfqg0rblXBBw3K5NkPx3/VK5a+ME+QARe8CKp5D2rv7yGj6IOWsQ9ZXcJ2PLXpr7n X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3306f4cf-7ef5-413a-9263-08dc639746c3 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:16.0255 (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: mydxMhd7qxdd0M82R1UKTTO61iTVIYuNf90yYS+368Xd3qHxOMfj5FEx0WVrqw3d X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB5672 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061441_733232_51446AE7 X-CRM114-Status: GOOD ( 22.26 ) 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 Introduce arm_smmu_make_s1_cd() to build the CD from the paging S1 domain, and reorganize all the places programming S1 domain CD table entries to call it. Split arm_smmu_update_s1_domain_cd_entry() from arm_smmu_update_ctx_desc_devices() so that the S1 path has its own call chain separate from the unrelated SVA path. arm_smmu_update_s1_domain_cd_entry() only works on S1 domains attached to RIDs and refreshes all their CDs. Remove case (3) from arm_smmu_write_ctx_desc() as it is now handled by directly calling arm_smmu_write_cd_entry(). Remove the forced clear of the CD during S1 domain attach, arm_smmu_write_cd_entry() will do this automatically if necessary. Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Michael Shavit Reviewed-by: Nicolin Chen Reviewed-by: Mostafa Saleh Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 25 ++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 71 +++++++++++-------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 9 +++ 3 files changed, 76 insertions(+), 29 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 41b44baef15e80..d159f60480935e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -53,6 +53,29 @@ static void arm_smmu_update_ctx_desc_devices(struct arm_smmu_domain *smmu_domain spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); } +static void +arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) +{ + struct arm_smmu_master *master; + struct arm_smmu_cd target_cd; + unsigned long flags; + + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + list_for_each_entry(master, &smmu_domain->devices, domain_head) { + struct arm_smmu_cd *cdptr; + + /* S1 domains only support RID attachment right now */ + cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + if (WARN_ON(!cdptr)) + continue; + + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, + &target_cd); + } + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); +} + /* * Check if the CPU ASID is available on the SMMU side. If a private context * descriptor is using it, try to replace it. @@ -96,7 +119,7 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid) * be some overlap between use of both ASIDs, until we invalidate the * TLB. */ - arm_smmu_update_ctx_desc_devices(smmu_domain, IOMMU_NO_PASID, cd); + arm_smmu_update_s1_domain_cd_entry(smmu_domain); /* Invalidate TLB entries previously associated with that context */ arm_smmu_tlb_inv_asid(smmu, asid); 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 1a61b47eeefe2d..0f4d275fff67b0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1203,8 +1203,8 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, WRITE_ONCE(*dst, cpu_to_le64(val)); } -static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, - u32 ssid) +struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, + u32 ssid) { __le64 *l1ptr; unsigned int idx; @@ -1269,9 +1269,9 @@ static const struct arm_smmu_entry_writer_ops arm_smmu_cd_writer_ops = { .get_used = arm_smmu_get_cd_used, }; -static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, - struct arm_smmu_cd *cdptr, - const struct arm_smmu_cd *target) +void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, + struct arm_smmu_cd *cdptr, + const struct arm_smmu_cd *target) { struct arm_smmu_cd_writer cd_writer = { .writer = { @@ -1284,6 +1284,32 @@ static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, arm_smmu_write_entry(&cd_writer.writer, cdptr->data, target->data); } +void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain) +{ + struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; + + memset(target, 0, sizeof(*target)); + + target->data[0] = cpu_to_le64( + cd->tcr | +#ifdef __BIG_ENDIAN + CTXDESC_CD_0_ENDI | +#endif + CTXDESC_CD_0_V | + CTXDESC_CD_0_AA64 | + (master->stall_enabled ? CTXDESC_CD_0_S : 0) | + CTXDESC_CD_0_R | + CTXDESC_CD_0_A | + CTXDESC_CD_0_ASET | + FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) + ); + + target->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); + target->data[3] = cpu_to_le64(cd->mair); +} + static void arm_smmu_clean_cd_entry(struct arm_smmu_cd *target) { struct arm_smmu_cd used = {}; @@ -1302,14 +1328,11 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, * * (1) Install primary CD, for normal DMA traffic (SSID = IOMMU_NO_PASID = 0). * (2) Install a secondary CD, for SID+SSID traffic. - * (3) Update ASID of a CD. Atomically write the first 64 bits of the - * CD, then invalidate the old entry and mappings. * (4) Quiesce the context without clearing the valid bit. Disable * translation, and ignore any translation fault. * (5) Remove a secondary CD. */ u64 val; - bool cd_live; struct arm_smmu_cd target; struct arm_smmu_cd *cdptr = ⌖ struct arm_smmu_cd *cd_table_entry; @@ -1325,7 +1348,6 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, target = *cd_table_entry; val = le64_to_cpu(cdptr->data[0]); - cd_live = !!(val & CTXDESC_CD_0_V); if (!cd) { /* (5) */ val = 0; @@ -1333,13 +1355,6 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (!(smmu->features & ARM_SMMU_FEAT_STALL_FORCE)) val &= ~(CTXDESC_CD_0_S | CTXDESC_CD_0_R); val |= CTXDESC_CD_0_TCR_EPD0; - } else if (cd_live) { /* (3) */ - val &= ~CTXDESC_CD_0_ASID; - val |= FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid); - /* - * Until CD+TLB invalidation, both ASIDs may be used for tagging - * this substream's traffic - */ } else { /* (1) and (2) */ cdptr->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); cdptr->data[2] = 0; @@ -2644,29 +2659,29 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); switch (smmu_domain->stage) { - case ARM_SMMU_DOMAIN_S1: + case ARM_SMMU_DOMAIN_S1: { + struct arm_smmu_cd target_cd; + struct arm_smmu_cd *cdptr; + if (!master->cd_table.cdtab) { ret = arm_smmu_alloc_cd_tables(master); if (ret) goto out_list_del; - } else { - /* - * arm_smmu_write_ctx_desc() relies on the entry being - * invalid to work, clear any existing entry. - */ - ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, - NULL); - if (ret) - goto out_list_del; } - ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, &smmu_domain->cd); - if (ret) + cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + if (!cdptr) { + ret = -ENOMEM; goto out_list_del; + } + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, + &target_cd); arm_smmu_make_cdtable_ste(&target, master); arm_smmu_install_ste_for_dev(master, &target); break; + } case ARM_SMMU_DOMAIN_S2: arm_smmu_make_s2_domain_ste(&target, master, smmu_domain); arm_smmu_install_ste_for_dev(master, &target); 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 4b767e0eeeb682..bb08f087ba39e4 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -751,6 +751,15 @@ extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; extern struct arm_smmu_ctx_desc quiet_cd; +struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, + u32 ssid); +void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain); +void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, + struct arm_smmu_cd *cdptr, + const struct arm_smmu_cd *target); + int arm_smmu_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid, struct arm_smmu_ctx_desc *cd); void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); From patchwork Tue Apr 23 13:14:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640065 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 8449DC4345F for ; Tue, 23 Apr 2024 13:14:56 +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=86mUIxVwl6TlBkbyTNz8oAYdyjkAbkV1EQSQE4tvgTE=; b=pnJG7xOts6uF2G CWxemkFrZ56SWGXg/KW44qjJLPwRoZndSY+BRZfQUQJgDILvunepbyFW2vU1dvI5EDR4gs63WBbF2 i9NlPvp2OlQww0/N3ZkaRu3N58bt9oM0xWEf+Hr6fknyQdNudO542t6fdhDqbnflzY7c/OmImqq28 r81aywy4EEOjKxG5kZ8BP1axmv7kZyLXg+2zz0v0dlw8TNsvX2MQMcjYBWrCkQm4hj+ZurCIxcX/A 99Mu4g1kUEpEWlLdjO6v42X1kfWvd65/br73U6U7tfj8JLkDsXFkvqelKC8QdPNeQlO6zjNr9WK23 nUj9f82EP/E4pmuHPXlA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFys-000000000ZW-2p3b; Tue, 23 Apr 2024 13:14:42 +0000 Received: from mail-mw2nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:200a::601] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyp-000000000Vz-0WE4 for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=b6o+G6WZIme0m+GB3Aw3ugxHIuzizOCUIMYMPgdDwc4zxNpou9Y3M1p4pGGYUp3ukkPXKTeyruuMzqgCIFP+NIfgKCOREzUEoMSCofMQAm0Ttk6ziC596/zXGV6koHiuTzLVWRlLR4lnKrziXIK0eAa7kyC6L27OkSSMCZmps78K/zqBqGIAHmAssQ+pOFDAxFDCqHJO0VQ5c+E9Hw2dssbTlxKnA4Nq3K86O/e+W78H0Utuv9jEyaOsVGGSEC652W0P1VMk27kuWkC2CGqs02uBbwA44aEdpJm+AUa3sinAdD1K6z6Bfdd77ZYkfTcniMT/4dY8hZAR8X6u3xH9Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=g7USwmTvgNseOMZ0U83Nja0Rt/T64eXvuRAGu+bFU60=; b=YuoYcqKSJqTiCEyP49SocLCn6zr+kOBa49T4qOZvfamwYq88jYkmp8drOvYRzESuFCNndd9wbY+xYkDr1mZ4uNgoDzuyU6rmzo4KBlft41bSIdTEW2h/fbZR31zGULELxZrcE0WMOFQPIRuVqZ0eT38X1YQYTfuQHVwh7nW0nkFkv5ocdtD8epJ4UDp2tVQhjqdDDAsUS4uVRbjnLb+KV/c4lw3NglaBJ6T8trgEKoxGNWySIRDYe2OLUkftF+OuQRUy9Ie845JJT6MT65uvrvOEQJbWlQXl0HKJ7U7y+SDiUQr7Gv70tDZ72fjDmoRfGLuTHYtG9TlwDjfHkiTCHw== 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=g7USwmTvgNseOMZ0U83Nja0Rt/T64eXvuRAGu+bFU60=; b=cn2cZnNOVZvTIRcAf/3FrnSHVeuvJQjAETiY+zJf7rKuN93COzbMDMODyz28poOnk2JOZwW7GNJM1TkvQC056GnqyX4/GLCoWNzpyF7RSzFbJ5OWyOAiq+b91djuknLtIb/JMtCPKzak9Z4cWC3uAZcBjG/4z2D/zRA6e3fBaMH7Y3JlvECsH7lHZcjNCG8pHjk5l0vVwyh2X1HRpF4cjGB2FU1Si4VKOgTFq1xSRResJ8moSBzeeRZa1hmdGvKueGXhyPTxNifoF9SWvjW9alwRuI+ned+Gm3NPsiXckS0097uuxnWTR5/dx0MxQnJmi7VWUdqlh+MSTc/OrEdfBg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BL3PR12MB6593.namprd12.prod.outlook.com (2603:10b6:208:38c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:15 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:15 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 4/9] iommu/arm-smmu-v3: Consolidate clearing a CD table entry Date: Tue, 23 Apr 2024 10:14:09 -0300 Message-ID: <4-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0323.namprd13.prod.outlook.com (2603:10b6:208:2c1::28) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BL3PR12MB6593:EE_ X-MS-Office365-Filtering-Correlation-Id: aa76286a-4f46-4d01-4e6f-08dc63974651 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FoppDCxzb7/FFo0j2oplOp82j3Rco6W3P936ejaTc4Y7RLHwIwxGRcKPl0QkGQqdWKgdJo5dqs3s5sfyLAlz+xnyfYpttSlVCKJ48+go1t67OlTFF+mW2WlO4h1OwOJ/jft5s9m/l7W96Siqnof+zukiZXx1cSqWu/FvznSa7hLvOWfiYYgtmk+UsGqQhayxHqZCEQTRNr8RzaBgXMXPtH++UjL+o+zxZvbmjGtnKTtNOe14kifkVtUTEviTHTqfawO7DjT/ShEcjMTXaITRy/x66vDdd+LsnAk1uB6q3UB2sTxE5bufZ7oqyVDSvaViYYDePFdB0ie31Azg9LZx/c0hdda6JwLJ+x9GgXKh86B6yxVJtfMTE3J9so0PWL+Zn+B2mc2McY+gRVHS7nbkviMi0MNFJdZIdhX9Cy91q7skRw0Yz2QyyKARR5Xztmy1avrJPJI5YdnVVcV8dgMnYA4mJmmkzc1mFiH06X87gYsFW//gu26EVrGvNS3x03/kg7q9KMW3AssLyyQRRWbpazqnYi5m0MFJROY3MH9glxstNnuQt/7fJNEPo7sLxztMcKZoVM3CJUcbJx6Ah1QUulwkftN8FgcDehiesEabs5D8Z1yW/qIUd9HyDJXQuz5onhUP6mMC7n0+2K86jzjCyfDsRKPQGBCNWunjiC/Q7JuLKkHyCUF1oWSHGUZaXVZshVOWBIwrq5/zEayBMGf8HCo8b5bBrBEimgp6ODI4wC5plA+VT9xU8uecTf34Ih7hOkzeume0Hi1r9zv/DR1Bp60RSV0gsRu157EQ17Iox4TuWtAT17TsOWiNJK+LdUctrj79dWt9YzhUBK400xc+XfExBrHCoj8lN6tI1QZfGGjEZlIPb1aIfllKVvWQIjUo/pbaS83PZMUTgMocdUTPbSMJKiZDsHov5JPKZnEVz1dPXxQeDpFB9KV4FPARGnG0V/0IX8MHcvL/UgSqZs7PpQIuljSSd3/4AqQCWxvYef5+NKcwO2KBxWfyvmLLXdKv1n6KgYEujHu37Sd+jE4JoD2G7j+QiiRI07+6LWSp/ZJmB9DqZrw+WvF7ZKRwONj38t6CHflmyq4O2SxCoGu8clX2UbgyrgclA+1NPQDCjC/86Kq1TixAVY1t0U9yayE44G24phTBscAOzEYR8rdGBn2jpnHRY32CPy1ssLdhmf1A3INKbCIzlJkbZpbJ/mC9MTpkVtWHAcl+uD9Dy1n7qO3H0ELOJl6JgPZGLv44yOa+tBh3Lwk4YO8HTRf+0eedaU/utSqiXG7j2hefdePrOA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(376005)(7416005)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: JD3QaJoX7XVhk1z0ZjMTPhXdo+7JsO3dQ/LJLmdnpZfNUGhbH4Ko725M8KuQow+QyeOnDcIVVzCVs0OLVJlDGyLFSgFD1DyWAYtb3qAjnTkuu78Sw/OanhtZLw5AdhRZrxcP67D6QGQ+YhO91K3SpyYuM7WP8Y7noiAToEveSl4QdRaTeS5YRtLWLpn0ffrmyULlyUy/3eCdjnZfHDPA8vcnD9mYoblCNgDagBtzcEEFfrGWrVxfEg9Mx7JlYLa2nlOYebSjivPGyPIibLb9mJsD2pMBvNR/h0KWCmmUduiGcFBxxDTIhmgcJ2IowQPE/GzNy8kloF1N/nuRh/FcH6RkFLqhjCWRlaaKQtdJRtaoDv2fU5+i6AEsgX26ZA07zcu/Ut7G5BVaSAwPeFwyg3Q7T0fKtFKYp+BbHrfouXlnYJRvNADbYMNfqHDREyEfy8o/JoYLDMz4aAvSEP+mWFZ8qs+zyGR4OB0D5sZy4IjP/auZfJE6PesCSAhfYaEetBHeKlGgRCSaOsJefZeuBKpXa7jodgCnjBw8tcGiLRKsPz+0VbbY1xMb25KQf6hF1fKyCc11D5FVSBLe1VcBym/8niXa1Cd+OvcL4FentmELGeUn8KA/xX869YqX0d5c+cL1OKNsly/lZe9faDF0XyX4L+XonDY0rrThwU09Oiq4kRFHlUYgEnW0YOr1zcgM3lIWZgIXnix8GatLzH5ui+AUku9/Wo6M3K+IqDhCAkldBabwO3g2WtVpPT9XJsq4uE562N3P4t+khNnE0J8Kv/QoqZMQCWPUMW6bMiXd8bJK0FPK9ceB/BcpEWz2nBvcj9vJNelWBG4oknIZib45M5YtogHOOqP0pjqwjduCSiF4VhYGLzEPV4kzJEZDtz6vjCTVJEzfR+1E34eHKsg6zzLsUaABDsy2blUL7mUSES6rqlLiyU+4IUYZ01/seWxjDdjhC54BzLKHgs9YII3ebFAN8qNoVRTlQBO4Gh37xHpc5cTEu5rwu35PHPxOWbA0O7yFTTpiY99KpZDs1HQyhz4jMF74Ir3TD6ov6031z0uIB1DqH5QwP234nnLA7SZqCrYNUm538Zz52dRVb56djSS6NsgkV1GFxNxLmU0Gz/4XaNk+lOB5cgNBBKvzo0DEpHYYIIim2/fqMJnxQSnE/NZyW37VIuHNOhdIJv/+0Aeb5q/k7h6fVbpO21IES/KSLZfIRSQnvqWUcnE/euF8+obqtEWYafThmmr31uGvhLjfq4H/tlm2QV73dkJz1f1uZ5DqzKwe5EBSaql5C3563Heak14MQx7CT9v3EPq7Pvhmda10EYgU21aiXB1JA9uw838tm+bkssZGpVK9Zn0r6mGQ9r9XeMT/VaD+qjJtnuZHqnQqeoPoFe/3bqluDxB3eS4OnFXR+Eb4O3cbrp6pHGV3kxcgmC591ZtCKi3JF1fele2SGDPRBvZiFAIxGNX41EMHITHkQWQekFhPC9C8TVJdTEUz8QeL7in12cSXfUtWHrElcMhVKt/gJiddZ2+ODiOaFteoRxBfyLaAeCh4F8SLO9JwC5GGs0wTFoOBVrdKwf16u8/2VqBV0PnIork/ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: aa76286a-4f46-4d01-4e6f-08dc63974651 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.2122 (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: bibSZdlP02v9M4h/ZcGvY1s5pG/FiZhgN94sBtrEAzO0+2fUnahpihsDYqzBnzQM X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6593 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061439_247886_F3B0CE7B X-CRM114-Status: GOOD ( 13.70 ) 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 A cleared entry is all 0's. Make arm_smmu_clear_cd() do this sequence. If we are clearing an entry and for some reason it is not already allocated in the CD table then something has gone wrong. Remove case (5) from arm_smmu_write_ctx_desc(). Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Michael Shavit Reviewed-by: Nicolin Chen Reviewed-by: Moritz Fischer Reviewed-by: Mostafa Saleh Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 25 ++++++++++++------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index d159f60480935e..7cf286f7a009fb 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -569,7 +569,7 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, mutex_lock(&sva_lock); - arm_smmu_write_ctx_desc(master, id, NULL); + arm_smmu_clear_cd(master, id); list_for_each_entry(t, &master->bonds, list) { if (t->mm == mm) { 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 0f4d275fff67b0..14beba55c32ff2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1310,6 +1310,19 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, target->data[3] = cpu_to_le64(cd->mair); } +void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid) +{ + struct arm_smmu_cd target = {}; + struct arm_smmu_cd *cdptr; + + if (!master->cd_table.cdtab) + return; + cdptr = arm_smmu_get_cd_ptr(master, ssid); + if (WARN_ON(!cdptr)) + return; + arm_smmu_write_cd_entry(master, ssid, cdptr, &target); +} + static void arm_smmu_clean_cd_entry(struct arm_smmu_cd *target) { struct arm_smmu_cd used = {}; @@ -1330,7 +1343,6 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, * (2) Install a secondary CD, for SID+SSID traffic. * (4) Quiesce the context without clearing the valid bit. Disable * translation, and ignore any translation fault. - * (5) Remove a secondary CD. */ u64 val; struct arm_smmu_cd target; @@ -1349,9 +1361,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, target = *cd_table_entry; val = le64_to_cpu(cdptr->data[0]); - if (!cd) { /* (5) */ - val = 0; - } else if (cd == &quiet_cd) { /* (4) */ + if (cd == &quiet_cd) { /* (4) */ if (!(smmu->features & ARM_SMMU_FEAT_STALL_FORCE)) val &= ~(CTXDESC_CD_0_S | CTXDESC_CD_0_R); val |= CTXDESC_CD_0_TCR_EPD0; @@ -2685,9 +2695,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) case ARM_SMMU_DOMAIN_S2: arm_smmu_make_s2_domain_ste(&target, master, smmu_domain); arm_smmu_install_ste_for_dev(master, &target); - if (master->cd_table.cdtab) - arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, - NULL); + arm_smmu_clear_cd(master, IOMMU_NO_PASID); break; } @@ -2735,8 +2743,7 @@ static int arm_smmu_attach_dev_ste(struct device *dev, * arm_smmu_domain->devices to avoid races updating the same context * descriptor from arm_smmu_share_asid(). */ - if (master->cd_table.cdtab) - arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, NULL); + arm_smmu_clear_cd(master, IOMMU_NO_PASID); return 0; } 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 bb08f087ba39e4..99fd6f24caa818 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -751,6 +751,7 @@ extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; extern struct arm_smmu_ctx_desc quiet_cd; +void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid); void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, From patchwork Tue Apr 23 13:14:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640069 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 B08BAC4345F for ; Tue, 23 Apr 2024 13:15:14 +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=zMbThfZG8R4wn4sWJXROZ0p55WAyAmgZ4PIGB45EPCk=; b=PBXFfOyuVglB4S D0J0j4+GB0SlORgbeq0hlB3OrpY9nv+WcvZWKMEee/W7robhgehM6wYPdJLRQXZWekVhQFQaZlQRE Dv8KirsGmpy7bBQYZugQ0CAhH/p09ACmTaMKUrLs8hwdFnYEm61VhbMZ5u1wsNTy/JZMBe/a3xNt2 XC2XLtJWVRn+N0nlXD85oXexSX/fDxIvRgPWxGGV5ERF+qjmx7lzgk2zzwRwzLetbwsQ/sY/AgIm4 WSJN4d0XqYigRQBjUpO/ZOkuBbjPJnhhxYgCdPL5QSTrEWo4EgP/4qKnr9Q6Bc8fFuv/3xfkKk7Js 6xN4+I6Tz7bbf3nbyFPw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFzD-000000000mh-3xMJ; Tue, 23 Apr 2024 13:15:03 +0000 Received: from mail-dm6nam11on20600.outbound.protection.outlook.com ([2a01:111:f403:2415::600] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyt-000000000Uj-1DWB for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cmnR9NHhVSLsBT75VhqDNwFDXjmBZSyhK8qF+TmwN+vkvJOq0/J1ExWYnjU6K3CzrKJdVT2JFd2Kihn4BToPZrFILLqigXWWgoS1lOtqO5V13t0Spx0furwZEtgLjPMuRFWyadiL0tZIU4spdaxGLZFdJ3g7VrFgJR0eNvAejglYyqhrgBTfxkoV8V5TX2yK9ghiSe4SFNb1y7DhgUx03LiS9FwP4tTTJdUDGtYuhV4bGDs9JWv7dRMR3bJJ2mpjkqTzvoBf6YyPXjVrRoXb02jv8MAA4sQwCX+yLNoxOQp2ZFMYskBN6PES8NlGK7JjbVClZhclucLOAUQsnn09gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=4DFXjXcl6ss7JHBoebzpfIH6ZQzLNJqPhUTOdk2wf0c=; b=ARp3PIsWNM392+aFePkx2fvjszCPm2WtQkuTfRY/HYHI4Wh0W9HaUi51F/F3M5pV0g0zDmwc6GUW3/pc+0oV9ugZYlNKeTxWWQQFx3uQkrSyhGOkwgrzBZh6Sx6oHNoRp2dgG/ygsE5AiMfvRDQ87Btyo6Q464XEyz6XAyZw/l/SNUpc5VWE8802V6ly5R/YxjKJ4kEbBoCcs4FU0zadMfE1p1kX0NGF3SdB5UQ075bW9941JAdL7Rn0GbeE1bx7KTCTmvBf5s2e/KDFrnWIRBZ5+LVE42Eb5M+8YrDlqV274TCcO9M1W9Q8PGMJ5zhU9UDS9QbwOpWSsxCG4iKTig== 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=4DFXjXcl6ss7JHBoebzpfIH6ZQzLNJqPhUTOdk2wf0c=; b=kDc6HCHcIiNUoTagtgvH14L4hPXIsGV7ZRCRRlbPzqjUmeWKe9NCL9BSj/QHgyq5k3aqO0X3iUVFexjEAdvFvkX62vP9mkVDjEf1vqlosoj8xabdi1C+21DUiEb+LmlSECtTFndrWy8dw0I+1DH8GiKy85ZWmrJ+L3oxFMtIriJJoVH6Q8fZa3Ip4twWlDLduWO2eyMjke1hFuoW/Av/YhsAYlFElaSReJdaidmWEkF7eoB57vzJrp5560Gs8dGL8k5hqR4Qx4x/Cuu0SE0uB5ig3TIFkNX5PwumzqSOITycZ9Yb/1RRmtxwPWuxTO+VqKkXVacrML81qq48MgBcBA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by SA1PR12MB5672.namprd12.prod.outlook.com (2603:10b6:806:23c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:22 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameer Kolothum , Mostafa Saleh Subject: [PATCH v8 5/9] iommu/arm-smmu-v3: Make arm_smmu_alloc_cd_ptr() Date: Tue, 23 Apr 2024 10:14:10 -0300 Message-ID: <5-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR14CA0008.namprd14.prod.outlook.com (2603:10b6:208:23e::13) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SA1PR12MB5672:EE_ X-MS-Office365-Filtering-Correlation-Id: d9c075f1-531c-4b6d-57eb-08dc639746b6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7VEjj8lv1Rz0BrqVRdc3PG8MeSdGQhKGb0upfZM3CJLn7n6p4BYlmI7PXmr7E+Lbl724gCDVVZ4kM7jP/eeb9w95eylhU/slXQR0rMRFuZn5+AHNsdqAsRI23xwF7126XeGVB48TzaMYG79RRr/uOfqWdSAWOFkIazKQRfAXtbewN5ewStH5io/s4Q+vllTKREHVhfI7+TsQtuNSyolYGgQSsxhUXyCeoj8Mc50NWZ9NcTkt0XVkf+WkvG/nlRDnLpW9f5/rHmasBvv0zPx7RcJnmgAFF8NLs7z8/BVI301+yiRQipU7PvTgelOdDvUdYfRVskw14Z3L53PtHmo4Fii/ZQuI78C8l56mrZVbJbWfP+U4LJ+jeSe7LAWaYAtOFg9Cine36bKC+Kn+/C/m+EL6WNzPVG+jgzZxoV0zfmhr52jGS15DPCqbYIV7mdPzl5RcHlnKxCcMqUYg37K3tgKejE92ovwqpvoZlXN36exHSntnx85AjbOYXUivOcgmHGUOpY2ElyyE4UQy4y+xYK6mAk7tz496OBWhuKmfHeZa/qBmg2+N/DIw29f3Wwecng1H0D4imVBwEo5+oKLRxE7mWGikC/w0+8nTNzx/0TIolz78XMzt2WbpnpP6gajkcG70NgzmZUEW2SD6cjtjJN0Y6au7FITgfieHahSBDi9lWJT2HbmQWnguB/7WJApw97BRC+jRIcSYekIhcukPUSB/KJCi6rfjCqJIgAbRJRmpEGs9i0rxnA71JmksQYypJj/kbGcGKw6F0ieKOp+xpjBVrTH61LjIt+K1bE7rbaZxGZn8CRlXmSCku86v20Em+MoPLwd8qcTl6+02qORhxxO/4e3yrkhbm7Nn8lnLXCMePmN6i1huvmup5mtW+mgXFqHf7uVIM8qEMOzYKSkUf5E994j8uViKdzaR2jBaNs10ocUL1152IGpCgFL8RzjUMv9blIpDfywXkff825yoFfzH9Zjf47AQ2hl42AFg7UmvnmKsodsoXO0jdumiddiCsYmdRFzH/ON/LfNhfGLzuG9eO1mys++o+Aw9DBHysGtSeXeuFjCEPNXLzG4KEaySZD7PpJpucA8uS2JClGHcBHQVPG76NUGeDLTbChnXZWkVIiFiwBXRrFm9dz9zDEsWDZo7h0KcmjcUXHW2dRJ4xht4f4Va3KtTgbAE51fN7m52l7Uz7wObqJ9e+6/h8AEpsSRM/61tL4eiyo7Iht4Mc9Dj7ZQUd1oIW8LREitskyI6+3V0YAL1869oGOPSQi/kvPi+sUElspU6B+uboVGTQw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Ez5ljKs6QW66ugjjJ7Ash2z/8TtgIeU4d7asC4lvRkgIQPcF1lX2ytjLwAKMlVseCxc5iTVXKx06RBNUbaSwUG1p9ab9xGMfvAy2m+GBHifJGuptL3b1TtlUap55/c3UqxKkMmwdBDVvzGIL62X/ubMwFk/foz0I299t6YqNxNoCI4EM5T58ZfZiv1CfLyC0SCsXsOYWhfkgDWQl7be+XbtOohWeNYK0st1ukR7WBPCxfCaJu/9Q/PjNfJVWWvM9O1sbXTRHtzN0PSkItHtCXsZxzPk20ySpd1zuxjDgWLYNoU/5S2Uac8vlW1oCj/gA7zfIKu0s8PzBjAOFlsvVGVQm4/4KnnGWE3AFS46SqfA1gHo5fdQER1PjVyArqYPDDca6EGS3UBQB0S1tir2J79h2b1eGNvergmQs2DQHrJjlL/PiP+7xSPiOuRwULcY5t7WldQgM3nV9x4jmgK1yTBvhAbbdMQHCvFuOPsEVPffesxBNF54aY/pPoS373z/TYMg/W5jamJnMzjUnn56w6+O2jsQNVHNPSUwNL4qfMDJafJ92JVFw00w36oP3b1QGYe7N8h6aSODoeB9SvD5rSIxedFhsnQ4dOQRDVZBNC91xBkHPiKVUgKHlJfkxWSa4psaofbGa5NfgaF1zTf1brQj8vPlUSBFEH3CMY58kb4RfvbPtAuQUsqu/PnJGEqwDaMGJmhrNBg0Cy9beg13owXguH26YYpVj2q4hWKkVBhMY3Z10kJbMRQAmIDeSXCxR79ZVyxHuKl7a4aju5ZaPB9WUjS0lERNHE6fr7nMB2SYOqkpGzibo116V8L3KhFftoPh/+ffFoAKhZ5L+IfxD7MyshJDJjb0x4bDqqipBZhWPZ94rg2kX8LqKmNI1FsJnyYoYKAoBSCBMohmuDG9HMnVhi4MazMcIzeVYc0AEnuQ2KQT+oBGX6gKpffEa3iRMCQ4kkvs+ppvGZ/RW4/wagBMCtuXlbQF3+S1OTdkJqeNfTl36u3DP7uJB7p5wJOuA7dSIwOCBxUTkp3Q/PPWBblKuS3Y9/nLMLdrurTS42cQhnP4IimLmMqkzeKdK0aGIH0QTqi/DtqY4gudcy4KxVd8GlTgYE7OICfvoPmtLirQZBG5x/XZi6PZkBL7mO7AfudPhrgD2G/NY/qn/fe47jQPpV7+bx3c63wXGcIKqhcSvbMZjSB38UxlJgoIO0mui3qsUgovoqGTFqBK2Te+CPQT9KOjLs5ytKHev3NFgbYp+G5ng6PyeB9ZZZK430iYG/hJ9idW+LVZoyIKdN70LHIQftJIWjcwTNWD+NzjVp/e1zdBZsOV4m9lT7MFE3q0wtMuaOh+lqmhpGoHJ4vAsd/lq8Zu/clQl/Rw+PMxMzdZjuoje1L4Q/fOntHn9aBJXOITo7rHEv62E0VhMnEtYQkcRyOMJyxSrrOLhVeQLHyIoi1PLx06T6vzLO6DfmCNjf3TCMTFEi54JsT9n2Cwn9uZBkmKMF8R8kH2U+rlmLGRMkA+KFSqhbY4KUPlcjrYE3r5BeKglODOeeTEzeExmge0bpXVj4RlZqvwPi6DsCeMjp51K6rwcDAeQpVOEG5Fk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d9c075f1-531c-4b6d-57eb-08dc639746b6 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.8414 (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: SYFcRJXzFx2/TKfeo7jtTstwigzWV32gnSp1b5eUBDolqm74rRKd+92k6fLRiqbG X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB5672 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061443_370255_59E8D68D X-CRM114-Status: GOOD ( 15.63 ) 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 Only the attach callers can perform an allocation for the CD table entry, the other callers must not do so, they do not have the correct locking and they cannot sleep. Split up the functions so this is clear. arm_smmu_get_cd_ptr() will return pointer to a CD table entry without doing any kind of allocation. arm_smmu_alloc_cd_ptr() will allocate the table and any required leaf. A following patch will add lockdep assertions to arm_smmu_alloc_cd_ptr() once the restructuring is completed and arm_smmu_alloc_cd_ptr() is never called in the wrong context. Tested-by: Nicolin Chen Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 61 +++++++++++++-------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 +- 2 files changed, 40 insertions(+), 24 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 14beba55c32ff2..e8da2f7763d102 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -97,6 +97,7 @@ static struct arm_smmu_option_prop arm_smmu_options[] = { static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, struct arm_smmu_device *smmu); +static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master); static void parse_driver_options(struct arm_smmu_device *smmu) { @@ -1206,29 +1207,51 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) { - __le64 *l1ptr; - unsigned int idx; struct arm_smmu_l1_ctx_desc *l1_desc; - struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; + if (!cd_table->cdtab) + return NULL; + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) return (struct arm_smmu_cd *)(cd_table->cdtab + ssid * CTXDESC_CD_DWORDS); - idx = ssid >> CTXDESC_SPLIT; - l1_desc = &cd_table->l1_desc[idx]; - if (!l1_desc->l2ptr) { - if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc)) - return NULL; + l1_desc = &cd_table->l1_desc[ssid / CTXDESC_L2_ENTRIES]; + if (!l1_desc->l2ptr) + return NULL; + return &l1_desc->l2ptr[ssid % CTXDESC_L2_ENTRIES]; +} - l1ptr = cd_table->cdtab + idx * CTXDESC_L1_DESC_DWORDS; - arm_smmu_write_cd_l1_desc(l1ptr, l1_desc); - /* An invalid L1CD can be cached */ - arm_smmu_sync_cd(master, ssid, false); +static struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, + u32 ssid) +{ + struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; + struct arm_smmu_device *smmu = master->smmu; + + if (!cd_table->cdtab) { + if (arm_smmu_alloc_cd_tables(master)) + return NULL; } - idx = ssid & (CTXDESC_L2_ENTRIES - 1); - return &l1_desc->l2ptr[idx]; + + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_64K_L2) { + unsigned int idx = ssid / CTXDESC_L2_ENTRIES; + struct arm_smmu_l1_ctx_desc *l1_desc; + + l1_desc = &cd_table->l1_desc[idx]; + if (!l1_desc->l2ptr) { + __le64 *l1ptr; + + if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc)) + return NULL; + + l1ptr = cd_table->cdtab + idx * CTXDESC_L1_DESC_DWORDS; + arm_smmu_write_cd_l1_desc(l1ptr, l1_desc); + /* An invalid L1CD can be cached */ + arm_smmu_sync_cd(master, ssid, false); + } + } + return arm_smmu_get_cd_ptr(master, ssid); } struct arm_smmu_cd_writer { @@ -1354,7 +1377,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) return -E2BIG; - cd_table_entry = arm_smmu_get_cd_ptr(master, ssid); + cd_table_entry = arm_smmu_alloc_cd_ptr(master, ssid); if (!cd_table_entry) return -ENOMEM; @@ -2673,13 +2696,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_cd target_cd; struct arm_smmu_cd *cdptr; - if (!master->cd_table.cdtab) { - ret = arm_smmu_alloc_cd_tables(master); - if (ret) - goto out_list_del; - } - - cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + cdptr = arm_smmu_alloc_cd_ptr(master, IOMMU_NO_PASID); if (!cdptr) { ret = -ENOMEM; goto out_list_del; 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 99fd6f24caa818..c5c55d3e281865 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -275,8 +275,7 @@ struct arm_smmu_ste { * 2lvl: at most 1024 L1 entries, * 1024 lazy entries per table. */ -#define CTXDESC_SPLIT 10 -#define CTXDESC_L2_ENTRIES (1 << CTXDESC_SPLIT) +#define CTXDESC_L2_ENTRIES 1024 #define CTXDESC_L1_DESC_DWORDS 1 #define CTXDESC_L1_DESC_V (1UL << 0) From patchwork Tue Apr 23 13:14:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640066 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 4870CC4345F for ; Tue, 23 Apr 2024 13:15:07 +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=ntao+1JxCy2Gw2aSm2sThED286Nx75GEdVYJK8pIUZk=; b=NVtYz54IdbLDdr FKGrPtkY+21ymRbQ6cuVqaKLlygURvqBSGfPUoU9cq19VTU/uRDEH+5GxdPCYJUlnFNM3AzxYTBSY SXoQNjbmN1MxE+oEm85bJbRlexR744lfcYco/qPNhBXQZ1+wU4s8fI3bsM6/xW43gMWCU4fSn8WFA OAmtq5JZoJSbG0/wjlK717PZbrKWiALdmD7xEAbVD+/oGc5tCTQsvMV6cJkAEtsAkokZ564BV45iN Sdl2d53m+3IwnbrxZ3ZMFuMhcCMw9KR/4ds1NcaCwUPwinNNlE+8MXH/8MHZ/cPQ27eEU8duVp/AE HRmrRWAy1MhuOder55bw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFz6-000000000gs-0mSe; Tue, 23 Apr 2024 13:14:56 +0000 Received: from mail-dm6nam11on20600.outbound.protection.outlook.com ([2a01:111:f403:2415::600] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyp-000000000Uj-3B4L for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BUsyz8I7pjsnCmuG3qhuOWq1VAEa22CcQ7FR8/YBXjxD0pvXEOhedDEntTnpS8LR7DiKtk48UXioJPNqfch9NzjK99FC/F7i4oWIl/W8mIje1MRkrIYubn3lMN5kX/hawgi0lbihf8b2wvpoIXsMxfkA8pZdFyGZjHPMwk+RMSJ2YLsNC8aAHnnvJJPjoWVZPqTevQA2cF1zgYFGEe3yXjV7QiWh395seuw1hDU7rXWSNasmbHbr5u/k6mmwFKJd8Oe095mfKqAO5cw4IlVRSskjBgsiwB0oxLN7o6dXvYvSUxwYK8+K0nMA+yBlmfi/gBMFR/59d0vVRChauT12Kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=HoxuEuTKO3vt7K3spL90O5PQpKLp97Iq8VUpON58p4c=; b=cU2PVMJAya8OFk3W2Ox/gm8e63KffHv6PXU3x2lptS3pge6o0+4++YWQpxd5Li+Pdj+PZ7rGCLk7fdsPY8HJjAYn2HphSqVib9IRt4tvZ3p4XiFQHWBpFkANfqVYPJZnkFLZlEiG0UbeyvV5f4RjOhlGB1k2DcyYF+QhdxTuds9aZ9MN4nMxpVSw/WygBG9UE066DdUsjVxOan9+s1tDAsREKvKMcGTB9KeugHMTLFjPMGkkbSqslfyd8rc7bF323v5ZvrFE5rfDuCa7/cbsnru/Oj5Xw0PDn+roABTMSa+kjAhOm4s9tol8kfuWmf40CnUcEZWfYAS/rvIhje3XKg== 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=HoxuEuTKO3vt7K3spL90O5PQpKLp97Iq8VUpON58p4c=; b=DRo07vM7J/e3x+nmK5CzPOP7to3feShS4IWwwJRtzIqNwWXganyQajV3kyAJ11In+LxGxB79yePdhm4VlSs/Cq31eMmGIxLZHc1t7geukKpVZknvAEToCnwYhiAvfdPfFzeJG8yWpv5YIgW+xg5I8jZ9AqSgwOFSYZvctUEaTGpReaS8fESXXLavO2qbKBNtIS3T0SxAgRQX3I51dWwBV0jf92E8jgM2VmN/yJHFpTcCSTohEKqKwkK5Ji3FfS8rNiS/DVkew0r7Rhkoao42BYK0DQyU5wo27O3Gn/e+tYytAZSgq+tIf+p9NXoX2VYD9og+7YHMyFVDdeTlDXDWaA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by SA1PR12MB5672.namprd12.prod.outlook.com (2603:10b6:806:23c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:22 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:21 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 6/9] iommu/arm-smmu-v3: Allocate the CD table entry in advance Date: Tue, 23 Apr 2024 10:14:11 -0300 Message-ID: <6-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0381.namprd13.prod.outlook.com (2603:10b6:208:2c0::26) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SA1PR12MB5672:EE_ X-MS-Office365-Filtering-Correlation-Id: d60c8f3d-b827-47ef-694b-08dc639746ab X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: K/M4VwajGzlsq2B0plVqLrcrFt4+7ykzy0dwQz/QI99g1iHnViPcV/lAXLBNAY1eOmFtmMFtjalzj+eP0++BPsFoZgqZrVhejXXdB6miDZYXk5lkX2+CKIX6adzajlHz2I/cjMH3Qm7B0VTZKAN3UUpH8gA7O6LKxLMpYbRYr3vQVN29L/tq1orvfWqI+hOFh97NF4+/6kUx6bwGljBYBJRxKyZnSoOrGupD3rLfFDk2KDodCXt15stvsohtn9aD5AsEWTyGI8snoTrWjyx+HwqdDH3ur+xTPW77G9QV1Ap27hdWo03ybP/l3QxmF0R1iR5l5tJh6ie1ckrplTXUTDvJnNI3BYAKHAzV2J0M03VtXmeq1OyXEo+uRnyXKnmk5J4l9jcm4yd4ouGGb9diUVvSDy7EXaPPbWB8fGZbDljGwMvIUEI3beNgQpq6SR+eFG81JZhbTSHvsf0Z8lrN1OOTcbiNe9/ocprY5WUTyPU2n/R8DlNqWpOC5A5phM6gOyXFvTaXyEOIV/JSqxtDlV6MnRmIm+pdp+46T1yLpT07ZI0Nz5zxUiSRDMjERL3rP5Z3K/UjEvIXH3e35n6J3q3OQSy46ENfDzTSsPr1O3Sp9DYhVkii2I4uDR3UDvW1S8DG+NBi+iCcs221yZLA90rQH7ae2lPbDynGX2JR8Mtbcy40+Gx3z2c7Q54lseuGE2mg5uTUbT8DXYXCV50mJYFohN1+3Hcldof1tif7QaOJ2lZFpLKMYhVNpc9kXs3h2zGFesevaTUAJeuG8kv5fOOpi8ZsimLn1geKqBBmKaHu7LHMW+e7zxtd71/YnAOow8rCv2FaTGsj8YD+xY5f+2XHVyQrAmit6Eu93Jnh0DhPEVUexZ0HyH7kfVZkjb1ZT1TshWD8WoWX+hQLkVUjepipms4TYE2XRtHijp+SDbsCiDUhJQlJHvJ09WY5ULKgrRfjkzU4x5hO+UmV1QVbtx2T1mKClZ2ZbBbvJItAJuULqvvKRyurZatiaQntb4DiANkpMMWBBZWpjueQKXqBckLwm5U/aSDWc+5crueiS37Snz2iSms+9IroCM1HSHRMnrJo+nnYD5cacAIIgCpMT7StrJGWlOQdbxAjO0SThuvS+WZtLcwrVjAIkfHbyB3RN2g7TNxmqHAobdbkpkorGTMhPF9MiMhknQgqaGDitUGKOKumacQqEaWQywaPt68vsWvMXbx9kWTse3DzaEV5ZFEi1Jiqz0XuD8+Ccs1VcWzQdHLrPK9Kqp8QyRh2NjQaZuJJXhaITFQbFtPD7rWuoA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KtOG3xT2/T5n4gsrYxZbpN5fCEkzjPtipya620+CYbDeStQBkXvsXs7+Ffp1qPtlHOIIzw+DM3a8FKTB71y5ApDkD158MiiP6KRXQMX8nT+JxmXo0JJKRgY2o9kq3OnnScCbUVEoGqW1ghnH0cz2FgN1MkJMfYTY2Vszh6aNHbIt+OlZ0xxV9NZcSG3uxYQcPok2/dmeQ0BqmZB7srCJZ84GxQwwkO7buiSshCfcp1+7wkLIqVXHG5jQ7J80DtZ+coOxYBbr4wiAsToY9UI6BttP5Hhce91kTr9yrzbdEngDpOtiZNYG9KPw9nhC7YykJrqTjDVakzQbf4bdIRlxQKWsgiNd4aIgblha8srSwafr/OC370P2FmIOs/2XqaxSxBseRwkBQpSgSaDdoQ7XEpMSi5vnbEnV2dAeIL+5wGE7+d132sLrQBaenoYbMCbDf7APqNegfHhZ9cKsgAptfcdbcAHnzl0o08MyKn8giyh71ikWhNInQPDuZu92Pml1FupG4tp4A5mUbgB6AYHSf4eo3F7FtJfi2ZzZZwa/L9+K1+ZfqgsJxU8MzsoCm8LtPPvQpmF+nIvY1zDoHHPd5U/VO9ab9R2dCVYqILweRegDZIZ8uRiIarzWOBdU8T5auFAPqhffY12aaK+vPu1KjjYxozN25chdUoEuAmUZaLZ2ya+2CZ0chqC5hCcgRem7IqateuP9xGeAbjt3N2blv8tqt7oluO9FODqijCfYTjCkV6B+Kib+f4t/L4qREP8pBnnZ6kqzrj25jFjVrdMxSvkxEXUvGsM9298dxmZr/bxrhFSh06MLIOrPnGhmnIAqjcSwitvHuoYd23A1XJO2HSiIEQMINafEGQXSI4FBq6rRa1XITnWO5xCG1AM77ra/dHQ1gHk9F+usZ+vD9JOAf7mb2hUYb7hDzDJT/Y5vWKJfg6pOteoLN+smq8OlL063sNQBOfZgz46HDsSgF3eM32YvzSi1Pu8fIxUuxWrLtGuVDkaULd5Gz3XQRnqEh/wZJbxtnXRQmGSOVYD0m8WZt4Vfa7TD2OQ9Ij5pne56WvXmiMxUKe1JrAuTHxeSG+NqXpZ0dXvqhSs9vo1QQdIhrlPKUFjUdNJ+MyEPmscj6G3bc65CaGT/dgyNi+rPg4Y6wl7JDMrYZguey611pKHoT4JZrZ7+q43oZbFjK+aXN7SEvSyD8uncehY2Thzg6d2yRUpOR8/RZCuktgt+88uz/hxwmUC1cIWe6+Gp9EM3MxjgwLk7nzeUOtSIyMUkQGWi1hF7LMJMxiTF2gZeYei+jtiOltDAvpRrSGLpgIAR5m23gxII71z6HxbW30y/AkiOv7a1NygHkNteT7HQVs/Zcf68flbcbtnUawqF0dwTTsKOvH90+mxCuDfPItJ9BsWiQOfKh6IoTbAaC9AOpKX9ZVNE0QAGpzW0okYv6KezSUiPUagRR38rch5bNtspsibz1EvlIA9eNdkUxpoH3G0uUNVfUnDGzp7XlH90Rq0iO3D6GkGCc65mMEUuIh3Hhu+oTWAFRddztPBbE0xNZiOSKuORHO7iZnAeZW5lploIcftOkqYUBxs7sZiS/D2Mk6B2 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d60c8f3d-b827-47ef-694b-08dc639746ab X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.7659 (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: jpGIvvevUIuogj2N0CPLQeomNiwd1O58AZ06e8HSENBuPW5b5vrsv69537MQ3VJS X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB5672 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061439_905780_B59280B8 X-CRM114-Status: GOOD ( 15.34 ) 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 Avoid arm_smmu_attach_dev() having to undo the changes to the smmu_domain->devices list, acquire the cdptr earlier so we don't need to handle that error. Now there is a clear break in arm_smmu_attach_dev() where all the prep-work has been done non-disruptively and we commit to making the HW change, which cannot fail. This completes transforming arm_smmu_attach_dev() so that it does not disturb the HW if it fails. Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Michael Shavit Reviewed-by: Nicolin Chen Reviewed-by: Mostafa Saleh Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 24 +++++++-------------- 1 file changed, 8 insertions(+), 16 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 e8da2f7763d102..3039b01e3fbe6b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2647,6 +2647,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_master *master; + struct arm_smmu_cd *cdptr; if (!fwspec) return -ENOENT; @@ -2675,6 +2676,12 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) if (ret) return ret; + if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { + cdptr = arm_smmu_alloc_cd_ptr(master, IOMMU_NO_PASID); + if (!cdptr) + return -ENOMEM; + } + /* * Prevent arm_smmu_share_asid() from trying to change the ASID * of either the old or new domain while we are working on it. @@ -2694,13 +2701,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) switch (smmu_domain->stage) { case ARM_SMMU_DOMAIN_S1: { struct arm_smmu_cd target_cd; - struct arm_smmu_cd *cdptr; - - cdptr = arm_smmu_alloc_cd_ptr(master, IOMMU_NO_PASID); - if (!cdptr) { - ret = -ENOMEM; - goto out_list_del; - } arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, @@ -2717,16 +2717,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } arm_smmu_enable_ats(master, smmu_domain); - goto out_unlock; - -out_list_del: - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_del_init(&master->domain_head); - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - -out_unlock: mutex_unlock(&arm_smmu_asid_lock); - return ret; + return 0; } static int arm_smmu_attach_dev_ste(struct device *dev, From patchwork Tue Apr 23 13:14:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640073 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 92112C4345F for ; Tue, 23 Apr 2024 13:15:26 +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=DTmgfmpN+fxv8wawyxanHw5XsMmM1GdybMesGLhTHZ4=; b=p4T2yMqjliBhpm ItlW7pIdqFIXGGJJ5n9vOOYys9qyYg09AH0y5Pqxp7f/VMhClJQMjfIjtjm4frVFzL/1JEMXLPEVu 2913ucKRU83GXrik9iWrKLl9PPeDDKZXhZRpBgM0cbQfok6rKlYt6YoOYm6LmffhkojhwZemwSBYD XLlcemGtH/uWBUSXzGXWVNGJcTBAJqPpMFWk0Vp09HpNnuGIXgJp3jpW9e5scfijwtkgd9N6EuPoG Y31/6iMfpENc53vRHqSxggy6bNIJzCLjlcMlgknN+LnM/mCfEdsdIYro1uh+AR0eOImzob+PHMl0b V8cwAao23zpZSNaxl/xw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFzJ-000000000qp-3RPE; Tue, 23 Apr 2024 13:15:09 +0000 Received: from mail-mw2nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:200a::601] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyw-000000000Vz-0PxX for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hrbjDmBia3nNFXw8yymbZuAr/d/lwD6agBe+5PWH3WcPt+KrPRqnt/vda84DEU9m6JnE/aMmVS6HoG1AbWmlogM+14TMOCSvk5srVrwaa+DYyEUn/fB0jLujmlGSU72Ofi1nRY7i4iPBWBUjSGbSie+c6f47+kbRfwOrqcgb4tkvo8jJuoLPT2+x17ZpHqzDx8SbNx48DLZt3whgTMMynqcafZoHRi9audAFALSKOzhowLwoNf17fgd6uVGllrW669FArquTkUMKcodDFMeSiaZebxAVtLtzlQM0LiVbn9bYWIT0hiW19lQs08wcjYmJLkLMSZr6W0XGhi/QIxqS+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=kbvGEvS5saPMgjaSj0nAhSHHwE/FjiJmAdshUAmS+I0=; b=FFoWz1WbAJkcHh6GAaNcWtJn+DmIpHJCMCZyfeAcZgLm2dmv12B7f2uk0pDJ5kAPhJ8AIJsezgX2Ol9XgzS9Fs/PCXB3Dn0XcFk0BO7Set2QTPxk+6u9e+830dFTO18zo8WKSTT9CQFmDXhYHvLy03jnE4C4QEXH+Lw8+TJ0UwGEqOxGy6UeZvWiNP6fxQrJ5q/JXjNw6GXFfS8XymjwLo9mhuQqtCAxPFUUqzJf5RFd56LWMXNolt8thLulZ8cV3hQsLHzZTMXrP5MG8fWF3+RVe3ziwcBukkCXt9RiFeJyA6w+rDey3o1UkFCLDbHBUtqDi0qc3w0dJPbNRyxzJA== 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=kbvGEvS5saPMgjaSj0nAhSHHwE/FjiJmAdshUAmS+I0=; b=GFqbgFaaHByvM86lOWy6+Z1ZyEkCYKf00SVE4uFggotMKcRUWZUB6CC9VmqyKNFOekxMdTyddd84WmyuXqKP6QRwPiqNkdDpvGweozZehuWquZVpUnXIEkVgy87WvnpPpa5cBRkznRLcLOqdaOcuO5ec4JPhoCXIq17eUDpKWVvRAwR2oO67BkbBDPbBN8z8jYqjaUI+gbxBdgLck96jSCm0EkV7FadPcwCJnhUUQIkA4WGb/N5elImmNLvcUolFBabDh36txChXXq7LjGXQynSQD9McFEX2Lhue3Iqp/Mni4CANYI/Qx8g6Wb5MXO6eHeUHVbvOJNsSaXtViYsfTw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BL3PR12MB6593.namprd12.prod.outlook.com (2603:10b6:208:38c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:21 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:21 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 7/9] iommu/arm-smmu-v3: Move the CD generation for SVA into a function Date: Tue, 23 Apr 2024 10:14:12 -0300 Message-ID: <7-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0374.namprd13.prod.outlook.com (2603:10b6:208:2c0::19) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BL3PR12MB6593:EE_ X-MS-Office365-Filtering-Correlation-Id: 532be7c5-5633-41a4-a00b-08dc63974682 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3HQftNR9ituwVlSLca4s7jnVS6r9aiki5ylb3fmpN8+slh2+GfBjShbwW57n7Os1FhvbdzFlDai//MbGK4K/mMlorMU2Ss1/2tOVIwy2ntp5mqektFBArzSLRrG/fOK04qIdOBmmr4LPevUf6HlMtuR8KvIN+eqBMBL2FyfH3lZiKB4uR2IRZvsMya/hWhrzFp4yljN4DaC+hPixNGED2dfA/0WhHJwMXJWqiWh1+Koy3I+q3CT02RTE3Y5KCs1R9Jhn0LIhdki3sNzRdSsVk+XOymHqmb3QrwyqkFKfj9vNgU0OO5AY68k/r5hXNmR0XnNrD7t0Xqhk8cEsSSScUDjRUHif2H5By+plpcncMCgT1i3iuaSTHXRmuLB9+6vLgq46x0gTWjktS72YIpFLOI6OfRLPFlZtKXOYXdqVhsLke5iiLWURMXxNtq+Jk2kF7uF1SL+guk+69RAme2p9CuEoHaGVLcKCNwA8f+njWAgNsOZYYk3eU++yE+uCqm8tviSyNcVhUd6pN1jNJwTx17qwHqWtpfzVi7PThZRbpjXunfWuT+O5ugPVZRepmKHipsrb/ecYTgw2HvhxqnUP4ga1sLNtX/wM9WzMki2oSPoi0VwxYJuQfiqruHu8iDKuzazOzU0vInZzeOnvMZcZ5nfGx2KNOC3v0Wsgu0IbfJjuP5ThWN1PeNkW0i4aX1pEgzNqabyHo4Y34UUQh2yAbTZpMpL0F2sGnfp104ZHjL6SBDg3DXe7RySbj72RHUI72h9p6Z1xjyMb+p5KvP2Kv+fKooDHwFsAvhUWncpp2L8PY6ImT/uvr48MkgrD4iwDjVWeZrDWPfJffI0haMmLjkU1hQ03/OPDT5y5wHVNaoBs8z72mXCGvVk3V4rdCG28YSnNUwfE0Fr2tLr41pgX9gcFRKgnZkfBXLZrrnjnqBpI0vbu3irY7RcV9Po3G24UJAh/FDETJbXmR6SgrI3N9k+PWlMYEvD9UbkAVBnQZuYsq19IU5ss1vXaToGiwxiEO2RVcxVRBHeunHbmMV7eovOvF/x5gDNCKCtvTrHnXnwsZBJfsA3lQgrq+6425h12Y6W0FLI/fI18h0iaesC3WsPOfctn9YGU0sxiYWYZX0+BuC8Oj3sk5s8aX/VYHSChB79j5Q+sP+O/WOe5Vs4wt1yt0pNYPusNzWXal2iJ7OSlZd9jJbavN2uiQIw7YRxRXZDNuDOyPsc3pwKthcHFFzhmflHqCD7vLzq/frF74iCnqB1/1QTKyGAX8RYHzE8Q/oOZNPxuyAe2YiW1wKntLw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(376005)(7416005)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LS5Ac9vBhNzCNyn6TLwu/FVBP1GqyNcK8JCmfVpyCcUqJRFzRFJq6QEXwXt32ovQsqBmnAFreckX2HgRtdSlpx5V7KgmY4aeYzRmxYcTh4B5wl8+chIKrrNk4opvfWN4wCJGzwVTk7R2/aPEqxR6dsdXd8E0kmWBX3GNr/wX5YKUeR9i7y3FrtD4zgiqld0I1+iSEhgS2BpONY+4GSD4jkzZpij1I4k/02kmeHJxYTf6WhMJPDgDML2DUcIYNa2rW8vkm5EDNu9BNdkWpaiV4RXm/zNlgOvyW1I6+2mr5vBfG03n9CyrjnhUMb4nzaUvHjby0/Ws4EQbf12cICsZskEqD76cBjYcMVs5E6h2VuFUPYv7js5q8EQy/QdqcdiVhyTE90Nr7O/NxMT4tCVLVG77z9CZJUjSBwULeUQ9gVcnuOqctpqLWnEu4DVHSrs1dGtPwt16XTVSSwWFE4lS3jdhjWT/trgBSRPNPTqgXst/tQzkOEQY0tswDGzXz0BXS2j2sqa+xnzz23pmbywkcjhe+NHShmFYDW1GLfVaz8q4dqWbhG/IEJew4v6j7ORHGISZ0pGWgNxBuLcTuywmcf+31HWUK05G3dBPoUoO7NujFNb8v8ncFukjmwSDZ+LCMwYuZyW8eV3BvMBLRP1WIdu2IxicS8CcPNhoKyNa8LMmni7Aq6fmVemLXB+wriBv4L6lVmu+fbVACdLh3sdCNO1UGS2vdAte4+g7YoBTuNgJQyUpQxtGr5bW7S9Z3cVkUCn3F8V+VePvrTaeyfU3oEPaKkHrlH2YYOeojWYJp8rk+EU9rBJwzgzIMwGzEJf6lZtWCwWWwNW5ieog6d4ifKRN1xhdOVuIM82zwaw+umah6jnjINFIKzIn5xFmslyaluPLVBOLvEd8F8bR/zOrvSKGnV3BNnaDGtWVpLz92AhfRUDTZyx98BSFHdpWj25bicejFrnjB7pCbgR7E4FcnXd2SIuYVOfDsSenyFwY4EaiAaS2j5py19WZ/o0SFoSYtwIfYnm6QC2RGo+Um5cFkE3Y7AYxI4lzYMbA7WY/yJB0ub4Xve7YF6ShhwKcQ9bag4tMVIw1O8QFqBve5gzCPJz9w/tzntKQNQiP+KJLhZMHgSlv3/aLNQ2+NTOpgFd5XdZT5iopN/UD5tzt3oyoRf3i7+LeFzaJjkaAr6WKALvZn/5tpAyR8nvDvQ3GMjeHlKbVdjah+09tfBnorbsy2qkDssB4H1TYZ9/Du7fLUSIwVzqvZQQY0uH0Ql0tJvBPI91sri5zme9Pbryrbyew7VfPFdC5/uSn8+NpnnWJNEGJ25tHnpNiCshv0+3wnBhDdDXI4/Vwqtio6g/qL2B57WrL4WXjXVM4XXovZ1E3ITyIiAPCTcz7cQSs7+3TdgcnMNiHZOA6I0/j3y8l/OXCM7vZv84wLEyF62qRed7kfPyL2GRRJo6mbvY35y0DEd63iv+QAkniN2aq3Y+ZttIrBMxMb6l/Pi1pN88lZsYSZtX0qGhzkZeR3HwlOPoImwhqBL8vMXJQ2ZPjHNrUQoEkri5w6lH+5XclPc5CcSoOukXy9P49AdyAnSjV6YxK1a6L X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 532be7c5-5633-41a4-a00b-08dc63974682 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.5495 (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: QRZ/RHN4hFZECWTBs9JjNEUSH58Gt/xzIBp4NnupQvaHEG+pwwdwU556MQaKt0SJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6593 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061446_255222_B8E2F2A4 X-CRM114-Status: GOOD ( 24.23 ) 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 Pull all the calculations for building the CD table entry for a mmu_struct into arm_smmu_make_sva_cd(). Call it in the two places installing the SVA CD table entry. Open code the last caller of arm_smmu_update_ctx_desc_devices() and remove the function. Remove arm_smmu_write_ctx_desc() since all callers are gone. Add the locking assertions to arm_smmu_alloc_cd_ptr() since arm_smmu_update_ctx_desc_devices() was the last problematic caller. Remove quiet_cd since all users are gone, arm_smmu_make_sva_cd() creates the same value. The behavior of quiet_cd changes slightly, the old implementation edited the CD in place to set CTXDESC_CD_0_TCR_EPD0 assuming it was a SVA CD entry. This version generates a full CD entry with a 0 TTB0 and relies on arm_smmu_write_cd_entry() to install it hitlessly. Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 155 +++++++++++------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 89 +--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 +- 3 files changed, 107 insertions(+), 144 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 7cf286f7a009fb..8730a7043909e3 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -34,25 +34,6 @@ struct arm_smmu_bond { static DEFINE_MUTEX(sva_lock); -/* - * Write the CD to the CD tables for all masters that this domain is attached - * to. Note that this is only used to update existing CD entries in the target - * CD table, for which it's assumed that arm_smmu_write_ctx_desc can't fail. - */ -static void arm_smmu_update_ctx_desc_devices(struct arm_smmu_domain *smmu_domain, - int ssid, - struct arm_smmu_ctx_desc *cd) -{ - struct arm_smmu_master *master; - unsigned long flags; - - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { - arm_smmu_write_ctx_desc(master, ssid, cd); - } - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); -} - static void arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) { @@ -128,11 +109,85 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid) return NULL; } +static u64 page_size_to_cd(void) +{ + static_assert(PAGE_SIZE == SZ_4K || PAGE_SIZE == SZ_16K || + PAGE_SIZE == SZ_64K); + if (PAGE_SIZE == SZ_64K) + return ARM_LPAE_TCR_TG0_64K; + if (PAGE_SIZE == SZ_16K) + return ARM_LPAE_TCR_TG0_16K; + return ARM_LPAE_TCR_TG0_4K; +} + +static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, + struct mm_struct *mm, u16 asid) +{ + u64 par; + + memset(target, 0, sizeof(*target)); + + par = cpuid_feature_extract_unsigned_field( + read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1), + ID_AA64MMFR0_EL1_PARANGE_SHIFT); + + target->data[0] = cpu_to_le64( + CTXDESC_CD_0_TCR_EPD1 | +#ifdef __BIG_ENDIAN + CTXDESC_CD_0_ENDI | +#endif + CTXDESC_CD_0_V | + FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par) | + CTXDESC_CD_0_AA64 | + (master->stall_enabled ? CTXDESC_CD_0_S : 0) | + CTXDESC_CD_0_R | + CTXDESC_CD_0_A | + CTXDESC_CD_0_ASET | + FIELD_PREP(CTXDESC_CD_0_ASID, asid)); + + /* + * If no MM is passed then this creates a SVA entry that faults + * everything. arm_smmu_write_cd_entry() can hitlessly go between these + * two entries types since TTB0 is ignored by HW when EPD0 is set. + */ + if (mm) { + target->data[0] |= cpu_to_le64( + FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, + 64ULL - vabits_actual) | + FIELD_PREP(CTXDESC_CD_0_TCR_TG0, page_size_to_cd()) | + FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, + ARM_LPAE_TCR_RGN_WBWA) | + FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, + ARM_LPAE_TCR_RGN_WBWA) | + FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS)); + + target->data[1] = cpu_to_le64(virt_to_phys(mm->pgd) & + CTXDESC_CD_1_TTB0_MASK); + } else { + target->data[0] |= cpu_to_le64(CTXDESC_CD_0_TCR_EPD0); + + /* + * Disable stall and immediately generate an abort if stall + * disable is permitted. This speeds up cleanup for an unclean + * exit if the device is still doing a lot of DMA. + */ + if (!(master->smmu->features & ARM_SMMU_FEAT_STALL_FORCE)) + target->data[0] &= + cpu_to_le64(~(CTXDESC_CD_0_S | CTXDESC_CD_0_R)); + } + + /* + * MAIR value is pretty much constant and global, so we can just get it + * from the current CPU register + */ + target->data[3] = cpu_to_le64(read_sysreg(mair_el1)); +} + static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) { u16 asid; int err = 0; - u64 tcr, par, reg; struct arm_smmu_ctx_desc *cd; struct arm_smmu_ctx_desc *ret = NULL; @@ -166,39 +221,6 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) if (err) goto out_free_asid; - tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, 64ULL - vabits_actual) | - FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, ARM_LPAE_TCR_RGN_WBWA) | - FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, ARM_LPAE_TCR_RGN_WBWA) | - FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS) | - CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64; - - switch (PAGE_SIZE) { - case SZ_4K: - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_4K); - break; - case SZ_16K: - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_16K); - break; - case SZ_64K: - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_64K); - break; - default: - WARN_ON(1); - err = -EINVAL; - goto out_free_asid; - } - - reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); - par = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_PARANGE_SHIFT); - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par); - - cd->ttbr = virt_to_phys(mm->pgd); - cd->tcr = tcr; - /* - * MAIR value is pretty much constant and global, so we can just get it - * from the current CPU register - */ - cd->mair = read_sysreg(mair_el1); cd->asid = asid; cd->mm = mm; @@ -276,6 +298,8 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) { struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn); struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_master *master; + unsigned long flags; mutex_lock(&sva_lock); if (smmu_mn->cleared) { @@ -287,8 +311,19 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events, * but disable translation. */ - arm_smmu_update_ctx_desc_devices(smmu_domain, mm_get_enqcmd_pasid(mm), - &quiet_cd); + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + list_for_each_entry(master, &smmu_domain->devices, domain_head) { + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr; + + cdptr = arm_smmu_get_cd_ptr(master, mm_get_enqcmd_pasid(mm)); + if (WARN_ON(!cdptr)) + continue; + arm_smmu_make_sva_cd(&target, master, NULL, smmu_mn->cd->asid); + arm_smmu_write_cd_entry(master, mm_get_enqcmd_pasid(mm), cdptr, + &target); + } + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid); arm_smmu_atc_inv_domain(smmu_domain, mm_get_enqcmd_pasid(mm), 0, 0); @@ -383,6 +418,8 @@ static int __arm_smmu_sva_bind(struct device *dev, ioasid_t pasid, struct mm_struct *mm) { int ret; + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr; struct arm_smmu_bond *bond; struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct iommu_domain *domain = iommu_get_domain_for_dev(dev); @@ -409,9 +446,13 @@ static int __arm_smmu_sva_bind(struct device *dev, ioasid_t pasid, goto err_free_bond; } - ret = arm_smmu_write_ctx_desc(master, pasid, bond->smmu_mn->cd); - if (ret) + cdptr = arm_smmu_alloc_cd_ptr(master, mm_get_enqcmd_pasid(mm)); + if (!cdptr) { + ret = -ENOMEM; goto err_put_notifier; + } + arm_smmu_make_sva_cd(&target, master, mm, bond->smmu_mn->cd->asid); + arm_smmu_write_cd_entry(master, pasid, cdptr, &target); list_add(&bond->list, &master->bonds); return 0; 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 3039b01e3fbe6b..f021268dab4763 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -83,12 +83,6 @@ struct arm_smmu_option_prop { DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa); DEFINE_MUTEX(arm_smmu_asid_lock); -/* - * Special value used by SVA when a process dies, to quiesce a CD without - * disabling it. - */ -struct arm_smmu_ctx_desc quiet_cd = { 0 }; - static struct arm_smmu_option_prop arm_smmu_options[] = { { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" }, { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"}, @@ -1200,7 +1194,7 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, u64 val = (l1_desc->l2ptr_dma & CTXDESC_L1_DESC_L2PTR_MASK) | CTXDESC_L1_DESC_V; - /* See comment in arm_smmu_write_ctx_desc() */ + /* The HW has 64 bit atomicity with stores to the L2 CD table */ WRITE_ONCE(*dst, cpu_to_le64(val)); } @@ -1223,12 +1217,15 @@ struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, return &l1_desc->l2ptr[ssid % CTXDESC_L2_ENTRIES]; } -static struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, - u32 ssid) +struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, + u32 ssid) { struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; struct arm_smmu_device *smmu = master->smmu; + might_sleep(); + iommu_group_mutex_assert(master->dev); + if (!cd_table->cdtab) { if (arm_smmu_alloc_cd_tables(master)) return NULL; @@ -1346,77 +1343,6 @@ void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid) arm_smmu_write_cd_entry(master, ssid, cdptr, &target); } -static void arm_smmu_clean_cd_entry(struct arm_smmu_cd *target) -{ - struct arm_smmu_cd used = {}; - int i; - - arm_smmu_get_cd_used(target->data, used.data); - for (i = 0; i != ARRAY_SIZE(target->data); i++) - target->data[i] &= used.data[i]; -} - -int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, - struct arm_smmu_ctx_desc *cd) -{ - /* - * This function handles the following cases: - * - * (1) Install primary CD, for normal DMA traffic (SSID = IOMMU_NO_PASID = 0). - * (2) Install a secondary CD, for SID+SSID traffic. - * (4) Quiesce the context without clearing the valid bit. Disable - * translation, and ignore any translation fault. - */ - u64 val; - struct arm_smmu_cd target; - struct arm_smmu_cd *cdptr = ⌖ - struct arm_smmu_cd *cd_table_entry; - struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; - struct arm_smmu_device *smmu = master->smmu; - - if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) - return -E2BIG; - - cd_table_entry = arm_smmu_alloc_cd_ptr(master, ssid); - if (!cd_table_entry) - return -ENOMEM; - - target = *cd_table_entry; - val = le64_to_cpu(cdptr->data[0]); - - if (cd == &quiet_cd) { /* (4) */ - if (!(smmu->features & ARM_SMMU_FEAT_STALL_FORCE)) - val &= ~(CTXDESC_CD_0_S | CTXDESC_CD_0_R); - val |= CTXDESC_CD_0_TCR_EPD0; - } else { /* (1) and (2) */ - cdptr->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); - cdptr->data[2] = 0; - cdptr->data[3] = cpu_to_le64(cd->mair); - - val = cd->tcr | -#ifdef __BIG_ENDIAN - CTXDESC_CD_0_ENDI | -#endif - CTXDESC_CD_0_R | CTXDESC_CD_0_A | - (cd->mm ? 0 : CTXDESC_CD_0_ASET) | - CTXDESC_CD_0_AA64 | - FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) | - CTXDESC_CD_0_V; - - if (cd_table->stall_enabled) - val |= CTXDESC_CD_0_S; - } - cdptr->data[0] = cpu_to_le64(val); - /* - * Since the above is updating the CD entry based on the current value - * without zeroing unused bits it needs fixing before being passed to - * the programming logic. - */ - arm_smmu_clean_cd_entry(&target); - arm_smmu_write_cd_entry(master, ssid, cd_table_entry, &target); - return 0; -} - static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) { int ret; @@ -1425,7 +1351,6 @@ static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; - cd_table->stall_enabled = master->stall_enabled; cd_table->s1cdmax = master->ssid_bits; max_contexts = 1 << cd_table->s1cdmax; @@ -1523,7 +1448,7 @@ arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc) val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, desc->span); val |= desc->l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK; - /* See comment in arm_smmu_write_ctx_desc() */ + /* The HW has 64 bit atomicity with stores to the L2 STE table */ WRITE_ONCE(*dst, cpu_to_le64(val)); } 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 c5c55d3e281865..5540609069fcd0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -608,8 +608,6 @@ struct arm_smmu_ctx_desc_cfg { u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; - /* Whether CD entries in this table have the stall bit set. */ - u8 stall_enabled:1; }; struct arm_smmu_s2_cfg { @@ -748,11 +746,12 @@ static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; -extern struct arm_smmu_ctx_desc quiet_cd; void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid); +struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, + u32 ssid); void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain); @@ -760,8 +759,6 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, struct arm_smmu_cd *cdptr, const struct arm_smmu_cd *target); -int arm_smmu_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid, - struct arm_smmu_ctx_desc *cd); void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, size_t granule, bool leaf, From patchwork Tue Apr 23 13:14:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13640068 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 27132C10F15 for ; Tue, 23 Apr 2024 13:15:12 +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=L/aedMc0V7/9vG5UeOpx55sOYHwXQQRWo6JbrhSPPGQ=; b=BA0PoeOY7EreOc jXP1z76tR9YDaId1Cu4h9FrhkRcFUaGh3M3aXDc6zbWs8Vvwgyz9GySfUFFKfBKexYfPAL3AJ+pa4 TTi2F65NegrpOLLD1XLdDm9nQsuwFjhxm/2CxPR90s/KLuQtIn+vKjNXttJCL61L2YOLZsgoIEspU PlY+Yjck5EvGtg5Udxfv4Yk1uqWpWwOQtPzGmRstZKePQJvp2Lbgq+IDfNV0HpnDpP+a8PPDpqY8s RS16AZjkZMARHMC7vKLW7pROZgmzdhb+a1Jejb+FtOvVWCwjQy/H/pBwWBcRt1lZcsiJnahFNe7Zq GgZx1OleTTwmF+4znjeg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFz7-000000000ia-2l9p; Tue, 23 Apr 2024 13:14:57 +0000 Received: from mail-mw2nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:200a::601] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyr-000000000Vz-0MvV for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LcWdo6+FYrNTwk2lhKIcZ6DWVXdOqbRyLwfUZwpAPepV/o686NTlQ7mIgS+YIYlpJtWG1ZGSqoyPfKD5MXDlqhKRdCBsESZlMWNIIeynkCU/gGPRA7EEfCMS9SQLTIuDx0onaG0TxwRk5mQueDHvP/BVOlVXaAK258BBs3NzjBdeU3j3I8qiMx0lAu5q3do3GYQJMJN8u/lunCFHHmTtp0tj6YU4P/cr60AccSXJjFwBdUpvpzANAx+9T2fqO7niqpU3JIMRG2S6poT8ZxNDO0iYhcLbn99Pb9OQ+jzOvyIOtk1XaGxVLgcJVcu26VKGB/rSWUw2OzDRgPqylrmnOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=P9Gc0ljTfkZiDfNYJqusXC0QJBdqdT4d4ljYV+FyCtw=; b=HRE14Fzq/zWksYpypY2qKuEWckv6vQZRNXyEXaFSuM3Y3gYYlFWJJvy+DnDh4CfPp7Cuxm0GLOZn6Q7m1aRfeIdYAr02secJV7rrdHGRD+vabew//ZHzwqPCMyiI5Tc+9n6cn88oP5ejXdBWjDwXppkMbGX3/hu8ucZgg0WDXn9fVSPomTLtLqNdSdvY7B/P5OZixM7mz+EJ2Us44uAV2aOrmwddt6cZ10JOKBhAZ6TiTBvE3gRhmL7sgWFI92sXxswR83bA68hgwkl3F/SBVDgdEPefW719KxFdoJjSjX77rg39KHdvCvTDiJrdwjWscEca4mey4RIjAlLAb5T95Q== 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=P9Gc0ljTfkZiDfNYJqusXC0QJBdqdT4d4ljYV+FyCtw=; b=oMnFOqm74eTJ3vMRract2uKTd2nLuQOei6j5eCyouPFcL2TGaabZvh1UQIAV8X7NPFxeutHJ9qbobFXHcnVBEvgS8SUzSrgn++bZJuBv0uB+4dQb2Q9JmTc5o5o4GoVEGEUmrs7aETFYh9CKW8KCebwLR79wCQ75iTyO+gW/g96Opq3ENXQ/sIA6dVOlcP3GqPCim3UuQ4UjkDYYNV3ROQmAnjamgy4ew8y2TIzeTiWxTJ1LdZq2KtTgdCnPpfHyMjWTpa6J2kM+i0ISaD6gMjzs2T92h56BfIzDE+COnjVJE8fh/4XIvVxg5fF5vaQlAOPAUCLd2tPwwYcsAvIkCg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BL3PR12MB6593.namprd12.prod.outlook.com (2603:10b6:208:38c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:19 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:19 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh Subject: [PATCH v8 8/9] iommu/arm-smmu-v3: Build the whole CD in arm_smmu_make_s1_cd() Date: Tue, 23 Apr 2024 10:14:13 -0300 Message-ID: <8-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0380.namprd13.prod.outlook.com (2603:10b6:208:2c0::25) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BL3PR12MB6593:EE_ X-MS-Office365-Filtering-Correlation-Id: 53bd938a-04eb-4d09-5e35-08dc63974668 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fAN1eb2OKhmiddXAPYSyhmriJvgDpC+OkgK1m3V8Ut9JyDDoMlax8F+z6/0zpSsfgRG1nV3WXJPeprhYx7TzsikPDUDSumkDxod1JjBEvG1vD8OtZbVsHUw4zfsRhSyY0B9jDFmGJCCBwtS3DzcNVndGSMCUeEQNsrHryB+Y5LtQL0uVQ/s28vilVTBuazm33s+qe1l80elR5CbZo/+HedCXvVfNAhspFdDCSs9yHh4wvRAMzRcnyLm+PRR9obHSdq3VizwrtaxaFst2ZHdhf60D7vjveiKyZPSkaajwwyQkHwxwFxVwhM4sQoDSKGXTbhsxVvlOZjYoNnj6V0dDwOLBprvZKcZ2+92bk6903fUs/3YemYyzv0QZx/6CrRTF7Pk44cUGUZkcMkj/8uYgUWcoD9s/YMMB4QcsN3Nz23udRkSYZG9o5KhD1NWGhpCQ1M7zDmx16A/2rz1nY4NgCayw6uCG5lKN/Tgb2T1/GOkHHTI2b55bjJOEgiMcLCfku/pP2aMIJEZ/zz1kLfjnWQLcKXN9IA64CEYKx0wSkPwzW+0BI5Dw9uuKsK14XX2ZzbeYU12TXxjy8JNQpjw71EX0o01HEXGZPNUBRcNXtIuyayu+/g3GBQWn+YyGPFbed0KwZ6/O54IdSwDMpcw9vEg+xUC8msh0zDaNyT0dD/6YV5YhUVqZKLRnZ4z+u4zBhrFChsuYEcAJYZmLsFQsGtRQEIiNl3MOwsJ3ks5Iiw8rdZp10xESVxsBzd6ODiu0ugza6Q89yY2hrlJIwRI7lpkRVY2qA9VUpKQPDWRmczjbYbFmnIELAz6D6IFlNL4sNzU/ac/h8j+cIRHmFhycZxk/3XxYRn1MiXD+khAE6d6ZnIn5/NhGyeJcE+6wIjPJIg1c0OVbblPMgHbD7VNYduShuWxMYPH+Nozvg5Eb6PK7fYJrUspYjsXdtV11/EegZsh+1+iM2rmOMpvxppXFr4bRiit6Uf6U9Ai/0ZNbcu51amu0NQk21BrHU5AayVrzDRWfqLH0nc9AlhEo2PYShQO8KoD8uZ8ba/oxfo0JdEDwAtqgfX1T1/N88w8g8HmrX2Bp/EjfZ9Gm7K1D6fzqVETueQguZExaYzsMqcxxhxYLtaYVkpoOMc/kUnmcl8p0l86NW2+a9de4F+7d+Ck9ldyYafN8tapVel2AI0e9Kg88CW61wbr0VKE/BA0dWs1IAPjWuCQ22e/gAnIL3CVJQRSRFWaRi7AJLMOw1eLT2yZ/WcCT/Mdq8mE9tpjZpd5+CfJx+gkrNqf7gcGeBhsPoQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(376005)(7416005)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +4jtrQiL+rKdnjQBjWwGTpIUmA71i4cxuHAg3GHQZ1QaYksOrejyL8im74ByWBL0Qf/ftlDS4RLtLkZDUlKkmVkdMM6bmbNJ5VDT1b+KKv4JrMS+fHHL02ccqGW5VE3w2tUVeh2EKQB7pvx2Xe2oRg4rdDJbM8BY9e5v9iPWA9C0fL1P2Fjxukew/4eoVCB5o1f6P+aGwWJKeM7++02Bpe4xSmoXML5laEi4DDOhyEhHjVa9AyIfFjNj9tvNFJuUWuiEBPDQv2BBBoS4i38cYm4Eed6mYSoQ34wZaHr65PPpX5cnmNW3YtNOE6/UPD2jGFRxGkvllUP4LKFNImsc/rW5jkmHSAnods3h+JHIv4dd16qc68TsDI531Frk70akwGh5vIs2NKEXxdVLLUgVxHWLnkJCj/kaOQISKUrztk8rKjiSp5UVtcr09RFZIlG9e1I5y76j18XMX3oOhleqFnU1MbTAEpsuZ5JCqmG1fNO04XWO/5S2B94sxUjTpGMRJqgUu7SViYK74IDPtlDjTwHL7ytSUYTbNQVJF0pb3MkAgxnfPM8JRD3ZbEwCa89eu/6LS8zCBQkDlWkymiv5Rqju3UHRNbWX/PxJHzLnbDOejE2+74lxfs0m/rIS2j4N6wXsSDiPkFUAGIVAL5SeGYwM8ydNQ1Y/2SqVNRHaR9oUMfTjEsT4J/2cLOh+6W1x71vXl7ezbdhtNavkb0/bb7+Vv4fmyVUCxpz3GYtOlZ5wrcr0tI0foA1IpkcXDUlf1NNjmS8EJnG4GB+zjl01YN5PLm4rmrxduqjyiGOHunelK/RDoT+7ek1c/Wn/Wtz6WXMA+XC66OOSVFLXAWRyLt4lSi132kF04vPjGy9WZ89llkgzTkM+E9LkaU2e13JYRp9G19KXaXFW5nla8TguiagQELy1lyP5B2/eWaC62tk5cy35EIhr0oqdudaWwsxN09qGKnASq5HaddxV+7Cc2ORm8wKPTGGWQB8cOC6UQ6VaP7WCqZczkF3bXXU8oCYkO2od56QTLSKvRsdQUXYAOITMFU1jFdRp+SYW88HStK0H7xh2Ji4oNeMX9y0Bhk2GFDv3jbnRe86MsV1p1mBKIU2UFaIoibZZvU806GGvQcHddvGs1TVMMt7p3yTMu1k4L7QONFI7CYBrn0R86uyp71lBlfJVtnla+cSHEuIkwhhOxCScTxo+YS5H/wPxb5X3IDtTN3HOIHyDaoQNGgNe9rR887UOkWwP84AUr7SO6JnSEMethF3NpUY06ID7CCDUOSR3DVZWVmczhephez8XZp19ueA2uNjp1M5fx5UgZRg/3EdkXIX6cmiIixJ5HY/zWbbor852+h7q51rrRqZOIMw7n4Syi9vPExe3nZzFpnioZcnuif5Vfd++m7S0hu/IhyLX3KPmXfj4paEX6macje2XgwRSIp/kVdpiZwnSbzYM1TZMfxq+c/b9i1khoc9koGi/WrC6TrJgn7z0p834vc6wOGI+xuqGRvXjggrTL/1v/Hssif4011hfe44r+vWuzFbDRJvzYixdwjMSLdcU3dRKQ/rnisk/h7JMiHbxUNAqWznUMdGCK4etG1H6LjWO X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 53bd938a-04eb-4d09-5e35-08dc63974668 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:15.3536 (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: c9k4VVxHX7QNW1RxFpAGPjxD2fHIsqDs5OPaGFKot8gqwp1fT6mzidkA1O+PtCr9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6593 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061441_163283_BD070CD4 X-CRM114-Status: GOOD ( 12.98 ) 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 Half the code was living in arm_smmu_domain_finalise_s1(), just move it here and take the values directly from the pgtbl_ops instead of storing copies. Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Michael Shavit Reviewed-by: Mostafa Saleh Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 47 ++++++++------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 -- 2 files changed, 18 insertions(+), 32 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 f021268dab4763..3817bc99d7e319 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1309,15 +1309,25 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, struct arm_smmu_domain *smmu_domain) { struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; + const struct io_pgtable_cfg *pgtbl_cfg = + &io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops)->cfg; + typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = + &pgtbl_cfg->arm_lpae_s1_cfg.tcr; memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( - cd->tcr | + FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | + FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) | + FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) | + FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) | + FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) | #ifdef __BIG_ENDIAN CTXDESC_CD_0_ENDI | #endif + CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_V | + FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) | CTXDESC_CD_0_AA64 | (master->stall_enabled ? CTXDESC_CD_0_S : 0) | CTXDESC_CD_0_R | @@ -1325,9 +1335,9 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, CTXDESC_CD_0_ASET | FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) ); - - target->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); - target->data[3] = cpu_to_le64(cd->mair); + target->data[1] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.ttbr & + CTXDESC_CD_1_TTB0_MASK); + target->data[3] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.mair); } void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid) @@ -2284,13 +2294,11 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) } static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, - struct arm_smmu_domain *smmu_domain, - struct io_pgtable_cfg *pgtbl_cfg) + struct arm_smmu_domain *smmu_domain) { int ret; u32 asid; struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; - typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; refcount_set(&cd->refs, 1); @@ -2298,31 +2306,13 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, mutex_lock(&arm_smmu_asid_lock); ret = xa_alloc(&arm_smmu_asid_xa, &asid, cd, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); - if (ret) - goto out_unlock; - cd->asid = (u16)asid; - cd->ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr; - cd->tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | - FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) | - FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) | - FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) | - FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) | - FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) | - CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64; - cd->mair = pgtbl_cfg->arm_lpae_s1_cfg.mair; - - mutex_unlock(&arm_smmu_asid_lock); - return 0; - -out_unlock: mutex_unlock(&arm_smmu_asid_lock); return ret; } static int arm_smmu_domain_finalise_s2(struct arm_smmu_device *smmu, - struct arm_smmu_domain *smmu_domain, - struct io_pgtable_cfg *pgtbl_cfg) + struct arm_smmu_domain *smmu_domain) { int vmid; struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; @@ -2346,8 +2336,7 @@ static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, struct io_pgtable_cfg pgtbl_cfg; struct io_pgtable_ops *pgtbl_ops; int (*finalise_stage_fn)(struct arm_smmu_device *smmu, - struct arm_smmu_domain *smmu_domain, - struct io_pgtable_cfg *pgtbl_cfg); + struct arm_smmu_domain *smmu_domain); /* Restrict the stage to what we can actually support */ if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1)) @@ -2390,7 +2379,7 @@ static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, smmu_domain->domain.geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1; smmu_domain->domain.geometry.force_aperture = true; - ret = finalise_stage_fn(smmu, smmu_domain, &pgtbl_cfg); + ret = finalise_stage_fn(smmu, smmu_domain); if (ret < 0) { free_io_pgtable_ops(pgtbl_ops); return ret; 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 5540609069fcd0..392130b840d55b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -587,9 +587,6 @@ struct arm_smmu_strtab_l1_desc { struct arm_smmu_ctx_desc { u16 asid; - u64 ttbr; - u64 tcr; - u64 mair; refcount_t refs; struct mm_struct *mm; From patchwork Tue Apr 23 13:14: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: 13640074 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 62951C04FFE for ; Tue, 23 Apr 2024 13:15:29 +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=yPxp7sZA1B/t46jZZY9CbiaCsDX8hNKNUaQSDI2xyQM=; b=RibwwJg6vbahqB cLTb70gSHlK/No27VDQAzvz3HmjMYm7kgEcUWjWYHqfNuD+dNXWHEtApjGSUfrQ7EI3tGCrxyay1o VcRH3G0Q0kl9y5KblRpusk48NWciLibqNmyN1ttKeX26A6vdJI0ESbomcXPOFqj16RkpBZ8D90+gn 3QkXD82BSwJSWmmEFkz1EimpHCmcst3WodR9xdVZ1XdL6wOGqtQc+tYbZea4lrH9gyGsW3I7PL88b UN/7FFHAKb5SEyLhVPdhn/MCV+zVLVivLJVTvLJHNnOXGkGa0NH17Qvw4njZ+v66a7qfyq/utc1yd qJ9kf9YVr+N5WBj7GVGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFzO-000000000uu-3gOS; Tue, 23 Apr 2024 13:15:14 +0000 Received: from mail-dm6nam11on20600.outbound.protection.outlook.com ([2a01:111:f403:2415::600] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzFyw-000000000Uj-3Inu for linux-arm-kernel@lists.infradead.org; Tue, 23 Apr 2024 13:14:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K0b1UEzZxGJqHPi6dg5rYKX0+d8YVwBqzw+QeUc5KGu9nBIgx/W7Bawk98dM2Ror7BD4fQZUNBHsNRuKphKXdDOQ0OhNrslrnZPUanAG529FsvSDssho7pJ4VvkGyyivGq1rEdaLUFlMUTnX6z0pRdS0Vas9+u5zhlqQU19OuGQsCrksi5U+9Vkv/Mnei3oNvBKF3sIvIDHFUSxr93rKqvOgDdm9DzOMqNU2Iwz7KoIGdJQaF3e9GWt8R1bTSipRos7x2I9cEQvsG+sR8BotHbiqyXO+aRlTiJ5C82Dml4OqcOUNmW558rese/R0RteHKTlYiKGw2Di/3mR2Dl2ifQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=7pTKawxbgzlCZqkYCwcBYfS0qJC+JBhkt0ODlLyj3Ys=; b=iojKvc0ReYg3ACUTb//x0b1WqXwE3F+SuqTKU0vUTU6xpLQ8YsbfFZQzGneTTykv3gHGkdjCzWjCjTK08B/r+ue/XGsaWn7NdVTnpcRucYd/4P481VIYlG9Oa/Az1FPszNAmYPatKMbXsusFtPlQRMLh5CFhV1tdZm0icYdMj2cEcxj8SlsMSLqsNKqE/v6PJRHSDEMr6KgbDkBrDq4fi5TVb7JAvCYzzoVF31DNWHouN+U0Dvv2B77dPFpJR8LeqI6C5H5cvn5kWsxADTGmw5Sfu+epjO4bDnLuNqPrhPkAu470881uiV7qd2pzYWG49JbcTuf+srE+tcuQvyegBg== 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=7pTKawxbgzlCZqkYCwcBYfS0qJC+JBhkt0ODlLyj3Ys=; b=VmBSqJtp1LFC5V4Km5wmoSU7HKzYuWTuLfejihLvPxxy/1OGIadwsf9W6f9wRLCtEEnAc0zarwrGS9os8+s2VwT9RfDl7lPBfvbmArlbL26qJwUcY3EzIJDvWhmYCVI1ce4i0kvHOWH3qq/D79lVj5CJO6KNo2Bxt8bKgKYy7mBszlIGgAvloAmZ/SqGjoQG/P7KPLuZHWdpzXFNnI4YzYVXzzmItTn7XCPELcBPzT+bpJMNzFRzB0fjsGGDDibro8BlV8DtxNTwHIX0XDuvaTqBrMBwNp7X69gIwmK8meP04wSCViGSl0VSMbfil6EgB13evJAZ1V7wktCSrYYbJQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by SA1PR12MB5672.namprd12.prod.outlook.com (2603:10b6:806:23c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Tue, 23 Apr 2024 13:14:23 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.021; Tue, 23 Apr 2024 13:14:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameer Kolothum , Mostafa Saleh Subject: [PATCH v8 9/9] iommu/arm-smmu-v3: Add unit tests for arm_smmu_write_entry Date: Tue, 23 Apr 2024 10:14:14 -0300 Message-ID: <9-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v8-4c4298c63951+13484-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0326.namprd13.prod.outlook.com (2603:10b6:208:2c1::31) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SA1PR12MB5672:EE_ X-MS-Office365-Filtering-Correlation-Id: c31ff9b4-de78-4a67-ff81-08dc63974737 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VybzB/57gS0cryKFuZ6ZIykL3oyGLP7Yvy88h3QD8nd3qAm2YIzxzZYsNqFr+xjvxaSj8NG3WD/s2c6lC+F56zbv8nSSZRS8D93Y8xF1/bDDlFiawgNW8eR1adBr2Rgu3d1GWuzkZuDHtjRcvp+lEzkmuB4qbpsAuFnmbTxigHbb9oMjs2M0T0nrMcMc0fIsLeT850uxYHqFmpi0fCqNWFdyPuh8+7hauif6HjlnVfOQQrI3OlhZli7qcohZ31H6jnzbo2Iag7BpQAS+lwn4LXHY/UEpGZNEHvHoE2fY1Gy9V3sGwUlDPfn2WJU4bDbBiVf5g+S/cn0OR569cHmpFy7mPZ0ZL43ktH0k0FvSh9FRB2en9zAT1h6MAGnLqX/zyvEjD3U5Rxr7yJ6wbuWmZqjavvJMmPq1DrCRMQmmwnWaIRth/EMeVKHHeCSJ1ZSX0GNFuQrolig3fmcWlUOTtgg08nUcooTC3n2d7BxBb48QeVs5h5HWjoPlL0KE6sK4zVOqslBCd2SwcT7QclMoJhL8YcJZsq5SKimgq1QVFv6rtqCwyThfW1m/ZVn4NYwJLkWD3J42xzYY1N1c0V1u3qdfs+uFMjOagPLNNL/RQtg2UPAYR5QxOkZKxfFtUOIN86fbCNcH9OAOD0RFESIuUsC5GPvf2k7tvdWKvRNO0bDvlt4m107FkfAmo8v+PBLxNvMHx2r5Pe9T45OGFnbkcJOlrGkl3lNTOkDDxslwxmCdgKia9hWX9Ds7VGizU1DtLvkQIIGI8wmUE5b2iHwB4UnjQiMqkgcGwTf21gqQRDZcvl+1uTaJ9zR9KIORhWAHlSUozpqzIegM2sQUE2inUjxJf+jrnb7aNtYanmMkq61w1xzkH1XFm9w6DGPvQVdCM/6ZAJNQgHrypFR+adAk5NES8eMJR1NduBJIExS5qdxt4r86Bgt928WL+YXisqlWugltRRp21M93cyNa29A1ho15m1OrWDt8MnZJL95JK0A5MLmGzHooX0U2ALcqXg04Md5XNn397yKbZjyKgD75f4YKC11+F54ky8cMm219RVjndOL6YYPtjSB5USeSozWCPiydhdKk369onRvIiKG6WNMpMqGcs6o1zqpbrYcHDdQhgzmPmdRqsfBssGRD/Ttf3j9d89FaxrvqitLI+c53XUTfQSBJvMxK5XKQ4ObMF5iapueTh7lhrXufnlcJ+rWTVex2X+swrJ84ekIi7Ezh50UJ5ZW4nqLbWxQ4oEHP1yulUkAPjTqWcsl3nKg+asW0NzZyk9g9/05uGPhm0Oj08A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: u/tNvIuuSwYwUiHr/WMNHAB5WY2yCrr0cHuPaeh+rHdnbE3XA7Sev9RGq3MYfVHY7QCfGSQsqXqlp5DROt7HQWw2H+IM/lr5iSlo6lLJqEFnGRoRkyd8gUddOyujI9n8hePTn7JqALMY2xvUElBQNmHNE/rtNgeZ8BqoWO6WGgg2SuO8TzUL+gFo1YX3FTuTGc3xsQHRHsqwQEO2B5Bn6SVrU8fWwwVPDiKcb8jzAR1nBGNQubNpQqtKOyX/mbgB5Iyz0Wi2D7RMrEogCyQvdUKombhWn541dTHSCDMPNRpaPaZ78IGi4QEE8TuB6GijDUcgVeBGNrRoTNNGN5Oxf/ryejfFueSBsbjAxYuKMonokyQ/XJZlUxx7u+aytdF8vYm46JGkeDZR2n4gTwXoK+bFwzAdhEE+Geht1EkkZmCrCYS6yHukdZcBqKRg+MVRS5G5amqeeXQA4aOGuUbL8Mrv7j0glbPBS0Oa0ETYelYt5Buil2lS3xqJ4ZT9cTzgzRw2nNZkOurbP1N5S2qsZbRokBwJWlRFS5fbXmn0YJG1vkiyM3A4yxcTfHgZTrWx/YD34fYHJh2yrDkSY7aV/p08xycD7IGkG1sKpp8uNj6wBt4mMj4cSUJTXL8K3A9WGC91Rbsq77+eKBlly/pnmssEfwm4QMAnX5pya032MB4GUiO0DrXA0UqczUUnnDenG3LXj9xWaLZ9depvHvg5f/ty84gBCG/pOJCS5YCkXx771FHTzMt4ZO1Jz1zuBy6dQsGzp28WPbiqQRTtZSMRkx3tlfR4E2Ry7Eakiu2aZKwwG3+a008eEapJ53f3IPfIHXSf8JTrcafacEN3IQIgDSNY1SLj3I27SM2aWj7Xdd/LnGdBtlmO05k7Ulr452HjOKRZel4htaW6Ps7p8RQva9wJmpchtw7MC2El06vKxztbvhc38qQhAIcseQu6fbydqI6lBIPgvUqqw3X0Jpy9/uiZNdp88bu8/zsvwOCeX/Pb1A57t3b87ALhue/5budXwA8Ltbuy4/3i3++EmYpC3H0cSVIjvb5VpYulBv841r+esJ4jI3W75nDpUG9ymG0jFvmo4R1dhG1ILmBrbSn5lR3WgMThN5xfbrbDRiU6mPEHRpJ3TJ86I34R7pQPjroBbOYeYN243DPJfGI2CUtI61iJ4pLC4KVPl4jxaLMhxZL8OKahlrS24KdKOvlYupmueXriW+WFuGe2XM4A0JMZoknGrerHLkg6C8JeJhIC7XUrf4YhRfbG1H8vEiv4uZ6avguXlUtCcsi1KUiwMWNUEjCQ/bsA3BjMUUYlDQbV/laZuXbUa5DooWYcYcSinkbJ03ffK5dU+ojM2W3fxvc1wK19sBOV3t/JDpJKt+LvayRD5BGH8vXli05sbNWwdYiY9QIegZOrfBkx1hFxbvDgt7CyQfvbXKGGlmZfPTE6/stP6Y0ScIPd1s+k48lxoR/9yQ0IDmPEQBY37u4K4afcWNkxjymlpk89zIjwIBKISEdp5khtbDwiTY6HHjeCQqQ49OU0LB5HuG/xzdtOFX2uxlIVFocG2a00G/r3j+93pHZJL5MPg2T1W97M+2bPsUoc X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c31ff9b4-de78-4a67-ff81-08dc63974737 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Apr 2024 13:14:16.7770 (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: JrCCY6BIzZnshmG1CbN5cY9r0X7bwYoxOu7LM4Zyut2QCclNJbutd9bxs4r8Lv+S X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB5672 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240423_061447_090863_1ABD06C4 X-CRM114-Status: GOOD ( 22.61 ) 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 Add tests for some of the more common STE update operations that we expect to see, as well as some artificial STE updates to test the edges of arm_smmu_write_entry. These also serve as a record of which common operation is expected to be hitless, and how many syncs they require. arm_smmu_write_entry implements a generic algorithm that updates an STE/CD to any other abritrary STE/CD configuration. The update requires a sequence of write+sync operations with some invariants that must be held true after each sync. arm_smmu_write_entry lends itself well to unit-testing since the function's interaction with the STE/CD is already abstracted by input callbacks that we can hook to introspect into the sequence of operations. We can use these hooks to guarantee that invariants are held throughout the entire update operation. Link: https://lore.kernel.org/r/20240106083617.1173871-3-mshavit@google.com Tested-by: Nicolin Chen Signed-off-by: Michael Shavit Signed-off-by: Jason Gunthorpe --- drivers/iommu/Kconfig | 13 +- drivers/iommu/arm/arm-smmu-v3/Makefile | 1 + .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 8 +- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 465 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 43 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 30 ++ 6 files changed, 533 insertions(+), 27 deletions(-) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 0af39bbbe3a30e..f872aeccd82041 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -397,9 +397,9 @@ config ARM_SMMU_V3 Say Y here if your system includes an IOMMU device implementing the ARM SMMUv3 architecture. +if ARM_SMMU_V3 config ARM_SMMU_V3_SVA bool "Shared Virtual Addressing support for the ARM SMMUv3" - depends on ARM_SMMU_V3 select IOMMU_SVA select IOMMU_IOPF select MMU_NOTIFIER @@ -410,6 +410,17 @@ config ARM_SMMU_V3_SVA Say Y here if your system supports SVA extensions such as PCIe PASID and PRI. +config ARM_SMMU_V3_KUNIT_TEST + bool "KUnit tests for arm-smmu-v3 driver" if !KUNIT_ALL_TESTS + depends on KUNIT + depends on ARM_SMMU_V3_SVA + default KUNIT_ALL_TESTS + help + Enable this option to unit-test arm-smmu-v3 driver functions. + + If unsure, say N. +endif + config S390_IOMMU def_bool y if S390 && PCI depends on S390 && PCI diff --git a/drivers/iommu/arm/arm-smmu-v3/Makefile b/drivers/iommu/arm/arm-smmu-v3/Makefile index 54feb1ecccad89..0b97054b3929b7 100644 --- a/drivers/iommu/arm/arm-smmu-v3/Makefile +++ b/drivers/iommu/arm/arm-smmu-v3/Makefile @@ -2,4 +2,5 @@ obj-$(CONFIG_ARM_SMMU_V3) += arm_smmu_v3.o arm_smmu_v3-objs-y += arm-smmu-v3.o arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o +arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_KUNIT_TEST) += arm-smmu-v3-test.o arm_smmu_v3-objs := $(arm_smmu_v3-objs-y) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 8730a7043909e3..34a977a0767d46 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "arm-smmu-v3.h" #include "../../io-pgtable-arm.h" @@ -120,9 +121,10 @@ static u64 page_size_to_cd(void) return ARM_LPAE_TCR_TG0_4K; } -static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, - struct arm_smmu_master *master, - struct mm_struct *mm, u16 asid) +VISIBLE_IF_KUNIT +void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, struct mm_struct *mm, + u16 asid) { u64 par; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c new file mode 100644 index 00000000000000..417804392ff089 --- /dev/null +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c @@ -0,0 +1,465 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2024 Google LLC. + */ +#include +#include + +#include "arm-smmu-v3.h" + +struct arm_smmu_test_writer { + struct arm_smmu_entry_writer writer; + struct kunit *test; + const __le64 *init_entry; + const __le64 *target_entry; + __le64 *entry; + + bool invalid_entry_written; + unsigned int num_syncs; +}; + +#define NUM_ENTRY_QWORDS 8 +#define NUM_EXPECTED_SYNCS(x) x + +static struct arm_smmu_ste bypass_ste; +static struct arm_smmu_ste abort_ste; +static struct arm_smmu_device smmu = { + .features = ARM_SMMU_FEAT_STALLS | ARM_SMMU_FEAT_ATTR_TYPES_OVR +}; +static struct mm_struct sva_mm = { + .pgd = (void *)0xdaedbeefdeadbeefULL, +}; + +static bool arm_smmu_entry_differs_in_used_bits(const __le64 *entry, + const __le64 *used_bits, + const __le64 *target, + unsigned int length) +{ + bool differs = false; + unsigned int i; + + for (i = 0; i < length; i++) { + if ((entry[i] & used_bits[i]) != target[i]) + differs = true; + } + return differs; +} + +static void +arm_smmu_test_writer_record_syncs(struct arm_smmu_entry_writer *writer) +{ + struct arm_smmu_test_writer *test_writer = + container_of(writer, struct arm_smmu_test_writer, writer); + __le64 *entry_used_bits; + + entry_used_bits = kunit_kzalloc( + test_writer->test, sizeof(*entry_used_bits) * NUM_ENTRY_QWORDS, + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test_writer->test, entry_used_bits); + + pr_debug("STE value is now set to: "); + print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 16, 8, + test_writer->entry, + NUM_ENTRY_QWORDS * sizeof(*test_writer->entry), + false); + + test_writer->num_syncs += 1; + if (!test_writer->entry[0]) { + test_writer->invalid_entry_written = true; + } else { + /* + * At any stage in a hitless transition, the entry must be + * equivalent to either the initial entry or the target entry + * when only considering the bits used by the current + * configuration. + */ + writer->ops->get_used(test_writer->entry, entry_used_bits); + KUNIT_EXPECT_FALSE( + test_writer->test, + arm_smmu_entry_differs_in_used_bits( + test_writer->entry, entry_used_bits, + test_writer->init_entry, NUM_ENTRY_QWORDS) && + arm_smmu_entry_differs_in_used_bits( + test_writer->entry, entry_used_bits, + test_writer->target_entry, + NUM_ENTRY_QWORDS)); + } +} + +static void +arm_smmu_v3_test_debug_print_used_bits(struct arm_smmu_entry_writer *writer, + const __le64 *ste) +{ + __le64 used_bits[NUM_ENTRY_QWORDS] = {}; + + arm_smmu_get_ste_used(ste, used_bits); + pr_debug("STE used bits: "); + print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 16, 8, used_bits, + sizeof(used_bits), false); +} + +static const struct arm_smmu_entry_writer_ops test_ste_ops = { + .sync = arm_smmu_test_writer_record_syncs, + .get_used = arm_smmu_get_ste_used, +}; + +static const struct arm_smmu_entry_writer_ops test_cd_ops = { + .sync = arm_smmu_test_writer_record_syncs, + .get_used = arm_smmu_get_cd_used, +}; + +static void arm_smmu_v3_test_ste_expect_transition( + struct kunit *test, const struct arm_smmu_ste *cur, + const struct arm_smmu_ste *target, unsigned int num_syncs_expected, + bool hitless) +{ + struct arm_smmu_ste cur_copy = *cur; + struct arm_smmu_test_writer test_writer = { + .writer = { + .ops = &test_ste_ops, + }, + .test = test, + .init_entry = cur->data, + .target_entry = target->data, + .entry = cur_copy.data, + .num_syncs = 0, + .invalid_entry_written = false, + + }; + + pr_debug("STE initial value: "); + print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 16, 8, cur_copy.data, + sizeof(cur_copy), false); + arm_smmu_v3_test_debug_print_used_bits(&test_writer.writer, cur->data); + pr_debug("STE target value: "); + print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 16, 8, target->data, + sizeof(cur_copy), false); + arm_smmu_v3_test_debug_print_used_bits(&test_writer.writer, + target->data); + + arm_smmu_write_entry(&test_writer.writer, cur_copy.data, target->data); + + KUNIT_EXPECT_EQ(test, test_writer.invalid_entry_written, !hitless); + KUNIT_EXPECT_EQ(test, test_writer.num_syncs, num_syncs_expected); + KUNIT_EXPECT_MEMEQ(test, target->data, cur_copy.data, sizeof(cur_copy)); +} + +static void arm_smmu_v3_test_ste_expect_hitless_transition( + struct kunit *test, const struct arm_smmu_ste *cur, + const struct arm_smmu_ste *target, unsigned int num_syncs_expected) +{ + arm_smmu_v3_test_ste_expect_transition(test, cur, target, + num_syncs_expected, true); +} + +static const dma_addr_t fake_cdtab_dma_addr = 0xF0F0F0F0F0F0; + +static void arm_smmu_test_make_cdtable_ste(struct arm_smmu_ste *ste, + const dma_addr_t dma_addr) +{ + struct arm_smmu_master master = { + .cd_table.cdtab_dma = dma_addr, + .cd_table.s1cdmax = 0xFF, + .cd_table.s1fmt = STRTAB_STE_0_S1FMT_64K_L2, + .smmu = &smmu, + }; + + arm_smmu_make_cdtable_ste(ste, &master); +} + +static void arm_smmu_v3_write_ste_test_bypass_to_abort(struct kunit *test) +{ + /* + * Bypass STEs has used bits in the first two Qwords, while abort STEs + * only have used bits in the first QWord. Transitioning from bypass to + * abort requires two syncs: the first to set the first qword and make + * the STE into an abort, the second to clean up the second qword. + */ + arm_smmu_v3_test_ste_expect_hitless_transition( + test, &bypass_ste, &abort_ste, NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_abort_to_bypass(struct kunit *test) +{ + /* + * Transitioning from abort to bypass also requires two syncs: the first + * to set the second qword data required by the bypass STE, and the + * second to set the first qword and switch to bypass. + */ + arm_smmu_v3_test_ste_expect_hitless_transition( + test, &abort_ste, &bypass_ste, NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_cdtable_to_abort(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_cdtable_ste(&ste, fake_cdtab_dma_addr); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &ste, &abort_ste, + NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_abort_to_cdtable(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_cdtable_ste(&ste, fake_cdtab_dma_addr); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &abort_ste, &ste, + NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_cdtable_to_bypass(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_cdtable_ste(&ste, fake_cdtab_dma_addr); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &ste, &bypass_ste, + NUM_EXPECTED_SYNCS(3)); +} + +static void arm_smmu_v3_write_ste_test_bypass_to_cdtable(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_cdtable_ste(&ste, fake_cdtab_dma_addr); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &bypass_ste, &ste, + NUM_EXPECTED_SYNCS(3)); +} + +static void arm_smmu_test_make_s2_ste(struct arm_smmu_ste *ste, + bool ats_enabled) +{ + struct arm_smmu_master master = { + .smmu = &smmu, + .ats_enabled = ats_enabled, + }; + struct io_pgtable io_pgtable = {}; + struct arm_smmu_domain smmu_domain = { + .pgtbl_ops = &io_pgtable.ops, + }; + + io_pgtable.cfg.arm_lpae_s2_cfg.vttbr = 0xdaedbeefdeadbeefULL; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.ps = 1; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.tg = 2; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.sh = 3; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.orgn = 1; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.irgn = 2; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.sl = 3; + io_pgtable.cfg.arm_lpae_s2_cfg.vtcr.tsz = 4; + + arm_smmu_make_s2_domain_ste(ste, &master, &smmu_domain); +} + +static void arm_smmu_v3_write_ste_test_s2_to_abort(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_s2_ste(&ste, true); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &ste, &abort_ste, + NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_abort_to_s2(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_s2_ste(&ste, true); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &abort_ste, &ste, + NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_s2_to_bypass(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_s2_ste(&ste, true); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &ste, &bypass_ste, + NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_ste_test_bypass_to_s2(struct kunit *test) +{ + struct arm_smmu_ste ste; + + arm_smmu_test_make_s2_ste(&ste, true); + arm_smmu_v3_test_ste_expect_hitless_transition(test, &bypass_ste, &ste, + NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_test_cd_expect_transition( + struct kunit *test, const struct arm_smmu_cd *cur, + const struct arm_smmu_cd *target, unsigned int num_syncs_expected, + bool hitless) +{ + struct arm_smmu_cd cur_copy = *cur; + struct arm_smmu_test_writer test_writer = { + .writer = { + .ops = &test_cd_ops, + }, + .test = test, + .init_entry = cur->data, + .target_entry = target->data, + .entry = cur_copy.data, + .num_syncs = 0, + .invalid_entry_written = false, + + }; + + pr_debug("CD initial value: "); + print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 16, 8, cur_copy.data, + sizeof(cur_copy), false); + arm_smmu_v3_test_debug_print_used_bits(&test_writer.writer, cur->data); + pr_debug("CD target value: "); + print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 16, 8, target->data, + sizeof(cur_copy), false); + arm_smmu_v3_test_debug_print_used_bits(&test_writer.writer, + target->data); + + arm_smmu_write_entry(&test_writer.writer, cur_copy.data, target->data); + + KUNIT_EXPECT_EQ(test, test_writer.invalid_entry_written, !hitless); + KUNIT_EXPECT_EQ(test, test_writer.num_syncs, num_syncs_expected); + KUNIT_EXPECT_MEMEQ(test, target->data, cur_copy.data, sizeof(cur_copy)); +} + +static void arm_smmu_v3_test_cd_expect_non_hitless_transition( + struct kunit *test, const struct arm_smmu_cd *cur, + const struct arm_smmu_cd *target, unsigned int num_syncs_expected) +{ + arm_smmu_v3_test_cd_expect_transition(test, cur, target, + num_syncs_expected, false); +} + +static void arm_smmu_v3_test_cd_expect_hitless_transition( + struct kunit *test, const struct arm_smmu_cd *cur, + const struct arm_smmu_cd *target, unsigned int num_syncs_expected) +{ + arm_smmu_v3_test_cd_expect_transition(test, cur, target, + num_syncs_expected, true); +} + +static void arm_smmu_test_make_s1_cd(struct arm_smmu_cd *cd, unsigned int asid) +{ + struct arm_smmu_master master = { + .smmu = &smmu, + }; + struct io_pgtable io_pgtable = {}; + struct arm_smmu_domain smmu_domain = { + .pgtbl_ops = &io_pgtable.ops, + .cd = { + .asid = asid, + }, + }; + + io_pgtable.cfg.arm_lpae_s1_cfg.ttbr = 0xdaedbeefdeadbeefULL; + io_pgtable.cfg.arm_lpae_s1_cfg.tcr.ips = 1; + io_pgtable.cfg.arm_lpae_s1_cfg.tcr.tg = 2; + io_pgtable.cfg.arm_lpae_s1_cfg.tcr.sh = 3; + io_pgtable.cfg.arm_lpae_s1_cfg.tcr.orgn = 1; + io_pgtable.cfg.arm_lpae_s1_cfg.tcr.irgn = 2; + io_pgtable.cfg.arm_lpae_s1_cfg.tcr.tsz = 4; + io_pgtable.cfg.arm_lpae_s1_cfg.mair = 0xabcdef012345678ULL; + + arm_smmu_make_s1_cd(cd, &master, &smmu_domain); +} + +static void arm_smmu_v3_write_cd_test_s1_clear(struct kunit *test) +{ + struct arm_smmu_cd cd = {}; + struct arm_smmu_cd cd_2; + + arm_smmu_test_make_s1_cd(&cd_2, 1997); + arm_smmu_v3_test_cd_expect_non_hitless_transition( + test, &cd, &cd_2, NUM_EXPECTED_SYNCS(2)); + arm_smmu_v3_test_cd_expect_non_hitless_transition( + test, &cd_2, &cd, NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_cd_test_s1_change_asid(struct kunit *test) +{ + struct arm_smmu_cd cd = {}; + struct arm_smmu_cd cd_2; + + arm_smmu_test_make_s1_cd(&cd, 778); + arm_smmu_test_make_s1_cd(&cd_2, 1997); + arm_smmu_v3_test_cd_expect_hitless_transition(test, &cd, &cd_2, + NUM_EXPECTED_SYNCS(1)); + arm_smmu_v3_test_cd_expect_hitless_transition(test, &cd_2, &cd, + NUM_EXPECTED_SYNCS(1)); +} + +static void arm_smmu_test_make_sva_cd(struct arm_smmu_cd *cd, unsigned int asid) +{ + struct arm_smmu_master master = { + .smmu = &smmu, + }; + + arm_smmu_make_sva_cd(cd, &master, &sva_mm, asid); +} + +static void arm_smmu_test_make_sva_release_cd(struct arm_smmu_cd *cd, + unsigned int asid) +{ + struct arm_smmu_master master = { + .smmu = &smmu, + }; + + arm_smmu_make_sva_cd(cd, &master, NULL, asid); +} + +static void arm_smmu_v3_write_cd_test_sva_clear(struct kunit *test) +{ + struct arm_smmu_cd cd = {}; + struct arm_smmu_cd cd_2; + + arm_smmu_test_make_sva_cd(&cd_2, 1997); + arm_smmu_v3_test_cd_expect_non_hitless_transition( + test, &cd, &cd_2, NUM_EXPECTED_SYNCS(2)); + arm_smmu_v3_test_cd_expect_non_hitless_transition( + test, &cd_2, &cd, NUM_EXPECTED_SYNCS(2)); +} + +static void arm_smmu_v3_write_cd_test_sva_release(struct kunit *test) +{ + struct arm_smmu_cd cd; + struct arm_smmu_cd cd_2; + + arm_smmu_test_make_sva_cd(&cd, 1997); + arm_smmu_test_make_sva_release_cd(&cd_2, 1997); + arm_smmu_v3_test_cd_expect_hitless_transition(test, &cd, &cd_2, + NUM_EXPECTED_SYNCS(2)); + arm_smmu_v3_test_cd_expect_hitless_transition(test, &cd_2, &cd, + NUM_EXPECTED_SYNCS(2)); +} + +static struct kunit_case arm_smmu_v3_test_cases[] = { + KUNIT_CASE(arm_smmu_v3_write_ste_test_bypass_to_abort), + KUNIT_CASE(arm_smmu_v3_write_ste_test_abort_to_bypass), + KUNIT_CASE(arm_smmu_v3_write_ste_test_cdtable_to_abort), + KUNIT_CASE(arm_smmu_v3_write_ste_test_abort_to_cdtable), + KUNIT_CASE(arm_smmu_v3_write_ste_test_cdtable_to_bypass), + KUNIT_CASE(arm_smmu_v3_write_ste_test_bypass_to_cdtable), + KUNIT_CASE(arm_smmu_v3_write_ste_test_s2_to_abort), + KUNIT_CASE(arm_smmu_v3_write_ste_test_abort_to_s2), + KUNIT_CASE(arm_smmu_v3_write_ste_test_s2_to_bypass), + KUNIT_CASE(arm_smmu_v3_write_ste_test_bypass_to_s2), + KUNIT_CASE(arm_smmu_v3_write_cd_test_s1_clear), + KUNIT_CASE(arm_smmu_v3_write_cd_test_s1_change_asid), + KUNIT_CASE(arm_smmu_v3_write_cd_test_sva_clear), + KUNIT_CASE(arm_smmu_v3_write_cd_test_sva_release), + {}, +}; + +static int arm_smmu_v3_test_suite_init(struct kunit_suite *test) +{ + arm_smmu_make_bypass_ste(&smmu, &bypass_ste); + arm_smmu_make_abort_ste(&abort_ste); + return 0; +} + +static struct kunit_suite arm_smmu_v3_test_module = { + .name = "arm-smmu-v3-kunit-test", + .suite_init = arm_smmu_v3_test_suite_init, + .test_cases = arm_smmu_v3_test_cases, +}; +kunit_test_suites(&arm_smmu_v3_test_module); 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 3817bc99d7e319..15bad76cf84a61 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "arm-smmu-v3.h" #include "../../dma-iommu.h" @@ -42,17 +43,6 @@ enum arm_smmu_msi_index { ARM_SMMU_MAX_MSIS, }; -struct arm_smmu_entry_writer_ops; -struct arm_smmu_entry_writer { - const struct arm_smmu_entry_writer_ops *ops; - struct arm_smmu_master *master; -}; - -struct arm_smmu_entry_writer_ops { - void (*get_used)(const __le64 *entry, __le64 *used); - void (*sync)(struct arm_smmu_entry_writer *writer); -}; - #define NUM_ENTRY_QWORDS 8 static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64)); static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64)); @@ -979,7 +969,8 @@ void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid) * would be nice if this was complete according to the spec, but minimally it * has to capture the bits this driver uses. */ -static void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits) +VISIBLE_IF_KUNIT +void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits) { unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0])); @@ -1101,8 +1092,9 @@ static bool entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry, * V=0 process. This relies on the IGNORED behavior described in the * specification. */ -static void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, - __le64 *entry, const __le64 *target) +VISIBLE_IF_KUNIT +void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *entry, + const __le64 *target) { __le64 unused_update[NUM_ENTRY_QWORDS]; u8 used_qword_diff; @@ -1256,7 +1248,8 @@ struct arm_smmu_cd_writer { unsigned int ssid; }; -static void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits) +VISIBLE_IF_KUNIT +void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits) { used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V); if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V))) @@ -1514,7 +1507,8 @@ static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, } } -static void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) +VISIBLE_IF_KUNIT +void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) { memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( @@ -1522,8 +1516,9 @@ static void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT)); } -static void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, - struct arm_smmu_ste *target) +VISIBLE_IF_KUNIT +void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, + struct arm_smmu_ste *target) { memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( @@ -1535,8 +1530,9 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, STRTAB_STE_1_SHCFG_INCOMING)); } -static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, - struct arm_smmu_master *master) +VISIBLE_IF_KUNIT +void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, + struct arm_smmu_master *master) { struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; struct arm_smmu_device *smmu = master->smmu; @@ -1585,9 +1581,10 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, } } -static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, - struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) +VISIBLE_IF_KUNIT +void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, + struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain) { struct arm_smmu_s2_cfg *s2_cfg = &smmu_domain->s2_cfg; const struct io_pgtable_cfg *pgtbl_cfg = 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 392130b840d55b..1242a086c9f948 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -736,6 +736,36 @@ struct arm_smmu_domain { struct list_head mmu_notifiers; }; +/* The following are exposed for testing purposes. */ +struct arm_smmu_entry_writer_ops; +struct arm_smmu_entry_writer { + const struct arm_smmu_entry_writer_ops *ops; + struct arm_smmu_master *master; +}; + +struct arm_smmu_entry_writer_ops { + void (*get_used)(const __le64 *entry, __le64 *used); + void (*sync)(struct arm_smmu_entry_writer *writer); +}; + +#if IS_ENABLED(CONFIG_KUNIT) +void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits); +void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur, + const __le64 *target); +void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits); +void arm_smmu_make_abort_ste(struct arm_smmu_ste *target); +void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, + struct arm_smmu_ste *target); +void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, + struct arm_smmu_master *master); +void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, + struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain); +void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, struct mm_struct *mm, + u16 asid); +#endif + static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) { return container_of(dom, struct arm_smmu_domain, domain);