From patchwork Fri Jan 26 18:15:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533252 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 873D3C47DDB for ; Fri, 26 Jan 2024 19:20:34 +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=SPBgJ1TwBMKATM1JHtJmWWA+2XNKYScbXrsIY57eSR0=; b=bL1ovlJ4hjliEs cxOQesRmtGCeAIXtTVa5skz1nQqq5a1UOCHNQ79mT5YFRfKMzLRkqCvvsHtND7H1T4OIISO9qGL1R EHbPocRESCFwo3XALyB/GPm06uXwQvO68ArQNHbk+oJVYOsF4qQ5ULqAunkUyh+XkR1IE7t1GmuTm sXK6QCRWWyxZBFhaak3hmz0t96ffMe5CwJbPe4BFMHezVos3b1QYAv7+xAex12kETdrVUpSnlPtDX Q5i9F/faRLmEMQBQrOWGXC2eWHAkE6XAvBE7BJgAgEOSlD9cGGQULrUyMf8ZjQfLi4RDie8HIM7Mk UtAKZUlCU5CaLT/1VUew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkR-00000005CFE-3G7E; Fri, 26 Jan 2024 19:20:19 +0000 Received: from mail-bn1nam02on20600.outbound.protection.outlook.com ([2a01:111:f403:2407::600] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkJ-000000051ef-027J for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DDOBTkaLrzVP3DxTG3cNkfRuprNMrIszAz5m3AUr2RhYt+Oot/PHNZ8/bD+pUX34AMzZOdCi7nTA/HIRbORPAZVtZbZRN2YgPkcRHYwWIUwtbu+M71kS7SXsg29Rfl020KsLgpOC+4+QtAedBtYp6qOIlJgcMKSYkxYBT04kHBBUwPwehO3qhbqwaPdKzEFvRwB96C1Wu7ja3H6Ijj/RE+wDKuOPSTGRZvq+aPNb0JhJPDo50KTwRZsdyGBBx/Ls++KcVT7c9O5ts8eKplkTewBMcuPcdcXhb47QCmDM/CotIEfyux3cJY6ocCefMlHvV1VMG9PmkkchJiQnDa3Z8w== 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=VV9Yh9P4a4yc4QjCzu13JeOuUpTa3tEya2OAACDUKko=; b=Q3+Tpi/Tyr1PcnBXXFQP7TSKeEKihK3ep/WY5AGGrs2UOsXMNnK+FZabRhsZleuavNbQi17V7v3Js8M6srHv4K9GeZOxatHd5JFzfKiINNFc7V4fAY/CoiEnSWZbiu+FOxLa1VY0Mquv1tBPqfWuUCqox3D7qyp6ehLgM/my45TuWBUit2YoZFdGo0VOM5EL2Efi8thAtiAdQZkQMWzPcDVCEKvN9Iemq5i+orOLmSX9jabSB9wiv9nT3smXjQSyZn22Fc6jVM7MZ2E4XV2C8iGjlaPZhHA3dCgSdQDIK4mscom8vYVHSsQ8uGwkosqUOJN2c5t6lDWwJfHb6xKX7A== 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=VV9Yh9P4a4yc4QjCzu13JeOuUpTa3tEya2OAACDUKko=; b=WwzjwJe07jUxejN5Ojw+2cZoBQ6W4sciZZ69+WkNtLVRqQnE3lxa6uW5NX3ip7kyIVM86dVtzrfgPefvn+HKB8KIKbnvG9N4v+eOUS84fHCaGwBEud3VhQI/7gVEaYEUH+5bf9UjQMnXQUUdUOEeTZGZp9w6mhrHu7dR5c3mvf6KbfHEk2/hqucFy2nlF11u9NNYX2CcAzG8r98nWfSeuQ4hps05Bt318m3pzRvVJprOmExJtYKyU30ep/XV7HkCpvisHP9f/kNPjKYByBMX1n7A3C8E8+5UkbXu09smSkTtOuaLBCuM2XMMi9x9EAPUnVqzW3h8Siwc5N5X/6+kRQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by CH0PR12MB5186.namprd12.prod.outlook.com (2603:10b6:610:b9::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.28; Fri, 26 Jan 2024 18:15:44 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::96dd:1160:6472:9873]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::96dd:1160:6472:9873%6]) with mapi id 15.20.7228.022; Fri, 26 Jan 2024 18:15:44 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v4 26/27] iommu/arm-smmu-v3: Allow a PASID to be set when RID is IDENTITY/BLOCKED Date: Fri, 26 Jan 2024 14:15:28 -0400 Message-ID: <26-v4-e7091cdd9e8d+43b1-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v4-e7091cdd9e8d+43b1-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: SN4PR0501CA0108.namprd05.prod.outlook.com (2603:10b6:803:42::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|CH0PR12MB5186:EE_ X-MS-Office365-Filtering-Correlation-Id: d14776fd-a450-4300-b93b-08dc1e9aca5a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EHSi+tuatpFdKtmI/NStbPSZxmlQ094g/YVUnzfB7e7SjO6LUvT73n0M0IqejaTQmepFufak+8Q6kER1F9ZPsFpfl/xlstY2Y4GtHGBupsjkFqilhEUXxhCUEUIBNgc9Ht71UPXjPGZJ/RLpFvnfqfm+pI+Z5KC4M9BU/ZXSk82Jzan28/B8SNgxAvTy9cENb8cvRdnvGMvGiDJPqc439Lequb2jSvvkA1+vXS54ZZZEaWGkxBEaPi4kUP7Sf1okGbPdpYpD57eWiwR+6z8mibbIJbNIAbYO8w+8iG7JDfNeynIoxaPziGw+pOAPUL2YHdQQ7y7QRWe7opkolv5FZhdPvSSOspNT/7PcL2ykrVSWYzhoujhiAb4s3NCuLksMgV0ZyP5GQyjvhUOKZnm9xS8HAJvRelHPfOU5awX1fF/411vdUvfSGUBZCEbBosgtxGQwDKn6lDe27o+NUhGacr6bE5tFOd0tNXGQIDhmF+EJ8Gni7ipsT+qZ+CSQ4dQvVd3duIfNdiTgpRrASK0tjPo1ge150xHMu+EvSPdYsQ6k/ACmdOcizNxup/zjs6Mf8m4QnyE8xNKSV9MV0gyJRE7OEI8tLNrr/fLW7C1Ji1iGMXl0t0qqinp1Dd3+rhK7aU9ouExXQje91v0g0PHi6g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(39860400002)(136003)(346002)(366004)(396003)(376002)(230922051799003)(186009)(64100799003)(451199024)(1800799012)(83380400001)(36756003)(86362001)(8936002)(8676002)(4326008)(15650500001)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nzpcjW5/QLd/pvnPjzlSxPrOuSAa6bFPotEDzEVPtcu1SP6QeBLcmu4AJusLQWvR/a8Pbge8kTBKPE7gwvG163J9JaxI6yd/mcfM/UeswdWNz7UgrOzgzYR/PwNNXNjgfP/jGVht3Ma+bBDOecIre1cRL+EMc7U04OEKvxh7L7xuFApzh6fZIkdXBQ1Ya/nrblMNWeEpRDwq2OkkDZKCWV0tTomUnWUFzanbWDlxjJ6+IcauNtovPjEnKkhWQU0WAu0R4ktlT2E4v7Tm9pAYNq1rwcW23CgMiZnvu3k0vUKgd8bEwj8UKERywMdEXyw0khfIibAUQ88/pPAyv9QjU4/akiDZ2qxcPv5mTkGZpahVc9ct1Nl3NZcd9QDEbQranjHri+qXCT29CL/3DzLS6Fr8k7zx8XL+gh4cS5xv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ZOi13fzmMz4DkQgqIVCC8k6vRuwFPoYHECd7tL9ZnPRMSmNbqsNh7TxTaydFOreKsvKTKfJpaUC/Pkh3SSK5WYSHe X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d14776fd-a450-4300-b93b-08dc1e9aca5a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 18:15:34.9871 (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: 8JqtVwrCJwAKGt4OqqJSuVsHw8juPN550CUZ5mbzS8xvBMEKkaV1NTMPsm7Hrdk/ X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101607_117783_016058A4 X-CRM114-Status: GOOD ( 21.87 ) 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 If the STE doesn't point to the CD table we can upgrade it by reprogramming the STE with the appropriate S1DSS. We may also need to turn on ATS at the same time. Keep track if the installed STE is pointing at the cd_table and the ATS state to trigger this path. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 57 ++++++++++++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 ++- 2 files changed, 53 insertions(+), 10 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 87f05820a85923..371d7930c36390 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2439,6 +2439,13 @@ static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master, int i, j; struct arm_smmu_device *smmu = master->smmu; + master->cd_table.in_ste = + FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(target->data[0])) == + STRTAB_STE_0_CFG_S1_TRANS; + master->ste_ats_enabled = + FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(target->data[1])) == + STRTAB_STE_1_EATS_TRANS; + for (i = 0; i < master->num_streams; ++i) { u32 sid = master->streams[i].id; struct arm_smmu_ste *step = @@ -2766,27 +2773,48 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } -static bool arm_smmu_is_s1_domain(struct iommu_domain *domain) +static void arm_smmu_update_ste(struct arm_smmu_master *master, + struct iommu_domain *sid_domain, + bool want_ats) { - if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) - return false; - return to_smmu_domain(domain)->stage == ARM_SMMU_DOMAIN_S1; + unsigned int s1dss = STRTAB_STE_1_S1DSS_TERMINATE; + struct arm_smmu_ste ste; + + if (master->cd_table.in_ste && master->ste_ats_enabled == want_ats) + return; + + if (sid_domain->type == IOMMU_DOMAIN_IDENTITY) + s1dss = STRTAB_STE_1_S1DSS_BYPASS; + else + WARN_ON(sid_domain->type != IOMMU_DOMAIN_BLOCKED); + + /* + * Change the STE into a cdtable one with SID IDENTITY/BLOCKED behavior + * using s1dss if necessary. The cd_table is already installed then + * the S1DSS is correct and this will just update the EATS. Otherwise + * it installs the entire thing. This will be hitless. + */ + arm_smmu_make_cdtable_ste(&ste, master, &master->cd_table, want_ats, + s1dss); + arm_smmu_install_ste_for_dev(master, &ste); } int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, const struct arm_smmu_cd *cd) { + struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev); struct attach_state state = {.ssid = pasid}; struct arm_smmu_cd *cdptr; int ret; - if (smmu_domain->smmu != master->smmu) + if (smmu_domain->smmu != master->smmu || pasid == IOMMU_NO_PASID) return -EINVAL; - if (!arm_smmu_is_s1_domain(iommu_get_domain_for_dev(master->dev)) || - !master->cd_table.used_sid) - return -ENODEV; + if (!master->cd_table.in_ste && + sid_domain->type != IOMMU_DOMAIN_IDENTITY && + sid_domain->type != IOMMU_DOMAIN_BLOCKED) + return -EINVAL; cdptr = arm_smmu_get_cd_ptr(master, pasid); if (!cdptr) @@ -2798,6 +2826,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, goto out_unlock; arm_smmu_write_cd_entry(master, pasid, cdptr, cd); + arm_smmu_update_ste(master, sid_domain, state.want_ats); arm_smmu_attach_commit(master, &state); @@ -2811,6 +2840,7 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_domain *smmu_domain; struct iommu_domain *domain; + bool last_ssid = master->cd_table.used_ssids == 1; domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); if (WARN_ON(IS_ERR(domain)) || !domain) @@ -2824,6 +2854,17 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) arm_smmu_atc_inv_master(master, pasid); arm_smmu_remove_master_domain(master, &smmu_domain->domain, pasid); mutex_unlock(&master->smmu->asid_lock); + + /* + * When the last user of the CD table goes away downgrade the STE back + * to a non-cd_table one. + */ + if (last_ssid && !master->cd_table.used_sid) { + struct iommu_domain *sid_domain = + iommu_get_domain_for_dev(master->dev); + + sid_domain->ops->attach_dev(sid_domain, master->dev); + } } static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, 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 ed747e25f2367d..b842414b143c10 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -600,7 +600,8 @@ struct arm_smmu_ctx_desc_cfg { struct arm_smmu_l1_ctx_desc *l1_desc; unsigned int num_l1_ents; unsigned int used_ssids; - bool used_sid; + u8 used_sid; + u8 in_ste; u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; @@ -708,7 +709,8 @@ struct arm_smmu_master { /* Locked by the iommu core using the group mutex */ struct arm_smmu_ctx_desc_cfg cd_table; unsigned int num_streams; - bool ats_enabled; + bool ats_enabled : 1; + bool ste_ats_enabled : 1; bool stall_enabled; bool sva_enabled; bool iopf_enabled;