From patchwork Fri Jan 26 18:15:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533239 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 639BDC47422 for ; Fri, 26 Jan 2024 19:20: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=ViGx5YDfrJOMOG7FQ+7BSNoDoE2vGzoWfR+ECVN8q3A=; b=3bpYkm9OX73ykL qTOtahF0x74eMoBm9C2JrQkaPR0guK1wMaE4K9fqoUYgws3FmPruEUyvPFYeuh7CVVtgQiGj38BL/ GFtboFpoeFO+hKvJjz2GMQijNDUhDbPS4bAR/knqjYXiyaZC3iXS4IUPNpuRVZ5EuP9QXWoKSV0Od ABI14wVVHF0nvPplXA5YSIJWAbnF5JQGZpN8OeVLBxWr0xVKq63K+L3so/FWPWjQMkrljNAZ9ymOb XSl2lE17+M18wJIvrxGDMWWNuLJ6dXE4/SuMrZF7eNCSO1aiRR+J3d+RQp6mCF55tlJE7TuY3avOJ MwR6bvqBTbQcJltb6bKw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRk8-00000005Bzl-2x5c; Fri, 26 Jan 2024 19:20:00 +0000 Received: from mail-bn7nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2009::601] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk6-000000051UQ-32jt for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UOwuUZkaaRrQYgaqaucKNhs8NhLguMeBSdgt6sztEvwPWG/o9wCakh6pklsm3JxvbzsET8WOFfXm1qgKDDaK6zX0ixhJA+tAa+lB2ovNaJyTX8Z8FYKztd1a2VR439xvejIGOdQNRCcypRyTu8UUN/g2C0X4jqMaeXTe/YVUErfpkhbu70eMVP3GbAZHBAF1cxfy163midzxEpHPXymuwPXqh1hGiV2kuNrwF7gx4qOJts8V/ABl+4xfLbWsPAqfCKf4efUzNCUfLwmCyrpoC/eqSY8JfjkWjxV39KwxAE9QC1SQcjhA1T9zRY6trNm9ZqDa0jU0tKvoY1XdpdtTPw== 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=RG+E+V9Cw7653vTWJJ8EHj5QXr8LZWLPCvwRN0xt32c=; b=ESTrA7i4H5bZGKYtUgvaxe+JuGqdix1ue3Ux6LE5Lq07P2qQUo3tRyTm5XTYCnM2PR4WijfgE8ZuSZ1nLv8pTm5dvHMm78sb0X5k7lsq9hu4LB+f4NzfShoJQK65xsGoo7jsBWyNuZBDhHiBg+EkKohQxu7hNcwppuoIywhwnqONdbRu6R/9/wwYELVaWstRu3ndOCR2elNTkT/RcjKLkelzqVZ5IUOvZRNT5bfUXEVW2ZFajzlbTtwxGh8kxqVodNtem0CzjQVnUb1mdwoajYF3YHrfPykilG6N/zgoJobgjEgAB7fq4hOU2ilM4BUjTuwRtmcR3QQilZHBp6P+9A== 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=RG+E+V9Cw7653vTWJJ8EHj5QXr8LZWLPCvwRN0xt32c=; b=gx3eAx+anld4uWd5udgHNvAiLYsNZuZhSmh6wMHmfJny06QtDRhQ5tAzZDrpv2QYDEtlK1c80gqky1G0NHiKkohiPJBjGMd21lTDgKgDctVXt46AZRlhhh74Qvi0DKSEdFGQZdlifJ1b/HqWXhalFq81j1K/6kjXnEwHm0MNGw1y2Lsaez9vTNyQImI4TSZ2GNOwJubvMjM7x5SHPSIGSJaXeHfuLDcOG1q4QAMDp/sJYvssQR5fSePOrkBrm2rBRea7/pNPTl/1JO2IWOst5LzMEJy9gEi+rHdPFriaZGHB7eVPH6MmjBtK119tlc1xAhZlokvSOwZxU1wOL2OqLg== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:38 +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:38 +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 01/27] iommu/arm-smmu-v3: Check that the RID domain is S1 in SVA Date: Fri, 26 Jan 2024 14:15:03 -0400 Message-ID: <1-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: SA1PR03CA0008.namprd03.prod.outlook.com (2603:10b6:806:2d3::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: 5d4b8b71-d714-4598-0545-08dc1e9ac92f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CdmfPJX1h86R7SfLO5gtY/+njwZG4llxsk1ndGxelNb049SiyuVs+P1JtvwqSPdMP1uyO++/3IwRnqn2F4BDUZoZop+MJE+tt2gWEmjTqkJcHo8Yz/9kkapiZK291+dPU6FHCTwzOlGSIDS9DTKVKOCPNLu23s2kmVB81Tgkqa1F2XUFqcBHz2NPldkhQv7PUn7XKyor9Rn3/g7B6hmZbOmaHhIiUNwgqWjThRUMZ+d1x+ZbA+a+E1SZNmi4kZ7EoIn2b5ufPZ8/j+WOuUtady9959b46FodX0ARATUkkbHgAmkHePfrjaKTILcFHJKejD81nZZflLoW1eT9rrdVOqhqc/aLzLL1nFLBwx9AzlzqLk+WIcmbbbzGAcMyrEnnCLRGGW0Edke08gJGV1y/bavw2zsdLPlceT54Cx2/N2RxGlzvrVGW7tIbVMReSe96sqqQARtt8IkNb9ioIf/xQJQW4t5+XybJAgmfECP7HiPwly17z0QeX+NY2Jt93PCXKnZy10MQyKu2aISh001N7kWYvSD3D+vmWuecEqYQm7Z5EyjTZobYD8EeewBkwISneV3OigJN7gTA6YQKRX1EWMzHl0hYI9Ui93cAA050tD4= 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TeN8skbQo9kuBMP8ZqxxFXTV80EEmmqVqVI2nOrw4+Yoq+i7cAVvLi0kshNiB/OguK1+4ZkT0sEGmbKxmKQWYXNjMc4f6yVU+sMMWcJHv7dHmRbF1RgNLJjbQmj/w0y0jb2A9GTFUwwojeqYfw/4JRx3MHI51OKl67++SlKHJ1m+Y3HAHtuD8b/0bM+B+2TojRpcQ07zUzMp+YRivsDUUuGUGgO5sVUYuyUt/1BUJXTMwI9xWdSBqgG6zZ48nr7EumZcR4Y6ZRDt79TE449rGyuATnJc7tsGd9N82ZP4OTlMdVOxO9if8k9qybt4nuRNeUs8Yv8WyCjNWHpNrjfk9wWfy5fi73YjeYcx9C4qRAAYGCbVc0AY5zzqKXj2toCkGzBsufpllX8IO+Xa9YAswLK8dvz5NiLbuoDgtFwTfUJWsKtyhm0V1cxHqqrZQsJsjt70F4RA3wGhYJ8hQw5kb2bC7rD1Tpwx0vIH18RbMNadyNvXc8aftDC2riQeuQjZtO6DfoZ6uRjeJyJmbht8CLxAHzPW9iAuj++yqLVLepvaJPiYFCezZqBUuYiXzosykmZYes3FsTxXmmxZ1Qv+igsbqqMPYL/KFl/Xaw6rhZ7IRQ+O5YpkWE+gmzn2uvNwpTH1ppB9Tkr6hh4266vgObb1g7Ra4uD93fJIsydQxkGP5aXPTUvDNu0JzrajHLHqzNGXSTYiDem28Dg74quQTyKI4FRcOzAcFSht148Zw3C/ISaVVGtrsyvYsQDI9kQZzfsYRUKaotNRjcAPog6kTKbhQ/0yrK8eXkE/Wr9T5Hy9RAa2Sjg14NLHY2yGgFSLDiqhZZ2eo+I0Y6VvNjsYNidbbZ0JT/CK888+g+ZFCdShp/hBclLAPjwoYfuVQ3W1EEEfmwGRqLkLVpX0bYGvB9Uc7MDJ9Q9SRGmaUXHFmmE9ahbv/05m8gaNzCBs/yU2CByQT1dT+Isv26bhOhlMVL9YrpLBDvpKqLsuk9AIxsOzWfHW4qDhLwIfpXcfOKlPOJTcAp8mbCAoqIVdY1XfejS/MVPMkEuO07tqlRn0dM0CihiVdcT8XNZq2TZHdu4xH8gMmJK9VhfXbjUqNpDnEh86J4yCy58NYamY3C1PucEvKTbNx7kAIvNAt+Lf3ssnYCQ2lekJSZVr69hWpl7iDd7//DDXLtyqm7SStv6PbTJmTEg4AO5sd64aWEWMCBDTA9rQuPuQ9P4BKU/mqSsu4jvFWzC1kCv0Oe7Za7380R5RPbmkj9iOsp56+pTXw7AGBzGIFC4lVDyIMruW37sbV9rIGSPMblh8FGgyVfc/k+m6UewQBmXBQC2xdLUUuugtUw6bo7SNNg0NW60DpZScte5MOd1NEwKlS2cha5twd1emUXUh52D8EuLTa3vDkUorBk+adQ7DPOGMZu66/Zb9oJXWuU6M0fKg/LxTrpe+bB4lQHO29M4keq9kp7fC4HoFlxSU22zxhP9Mt2Tl1Uo0b7XEbtMre93JUyqUuZgo605yV0jSX6uHK7VYFej+DnadjWFk8oc+CZLjSB/jg5hpYD0DFJD935GX8dxW0m3KFf7dFghdKayRd4nPbRhNgp5C X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5d4b8b71-d714-4598-0545-08dc1e9ac92f 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:32.9584 (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: ixlxdMhezFAFN/RKcBWtOqezBnD13AMIwm9YDlS5Qf8WADB4OyIkNELfXPV2wVUc X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101554_890785_09290854 X-CRM114-Status: GOOD ( 10.39 ) 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 This code only works if the RID domain is a S1 domain and has already installed the cdtable. Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 05722121f00e70..b4549d698f3569 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 @@ -387,7 +387,13 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) 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); - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_domain *smmu_domain; + + if (!(domain->type & __IOMMU_DOMAIN_PAGING)) + return -ENODEV; + smmu_domain = to_smmu_domain(iommu_get_domain_for_dev(dev)); + if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + return -ENODEV; if (!master || !master->sva_enabled) return -ENODEV; From patchwork Fri Jan 26 18:15:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533241 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 7CCA7C47DDF for ; Fri, 26 Jan 2024 19:20:13 +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=Ew+tFn8CD6EpryQg0yMmIbpeyL4OCw9CJ8mcRhpwi7Q=; b=H8aFDa3NzbQxnK iM+NOKiSsf1uDsvEKZtRqTjf6UW2IW/VErpGHlWHVG50z14NUhsFWPsOA0EbVdzaqaeBWczODqIsH pRkE9pF71KVgar6F9BhEZsJu8XnjauaCSFLNsP1h4DX0alpaLeK789Y9fn42u+4jG9EsncN7CHrT6 cH6B35sr7PYK/OdgIb/5g6JcuYJkOoJryIuPrcHqH2+wyP4qHxQT6nZg9EeMuCrCC/REwMbmmfvQs iPVrIL5+kVs0hCSuJwEZ6uvm94jt1jIRIXZlcmTgDiu42G3ZRudiHYqFJuU/Q9pptYqlVZoLwcVyA BwbQD4mH+D1TD8AzQ7hw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkA-00000005C0g-0h2m; Fri, 26 Jan 2024 19:20:02 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk8-000000051Vt-22k7 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZR3gHSj1U0I12n3XB2bOCEfGvLuLE42AlVRxe4jOr5+ABRh6aMlox8SkQKPWKsEZpcTPN9BCv6JU7ZZTVtiKDzPw/SPK3QPimrE5gnIrRR/IOlBJk4c7l/hIUdEG0pWPMNzpIsaKzPKApbRov9X7XnVrpAxYYwv9Wo4bSKuS35mK4iSVwLg6Iu5SmeCd/Vo+m7vHyMFZVZOpiOBXAEpHzEIN0Xn6MnxdxVJGgB+Kps4WAQSB7Qu/G/nRMc4Z21Y4Y53YDMwmiYfSNpFrAHJaWXaenfKatmylwWv6NSbE7DOoE0VK5ROWOOlmgjLe0UMZKFTbrcVldHjUI3qOQvsqYA== 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=N971Jefw225IV23nvD2d5FRzMDc+zPfI8NVYuP8XODg=; b=BMmz+HqSpwzSePrMuSZbEsOUiW7OVRkS32GMJ542tQ9o6zo3U50NNgE9L5AKrQdYQ/ZtQklCux4yn9MuNpTMzGGjRqP0K3rxbJTTc0Ik7U+rMvUXVZCXVP0NLVMHZSgYbIX1BVAzkCZnXO6PK7P41S6nrzDexGLmHdeOoQckSLxntbk3GOj8F7RSRmhpbPwmEV0KGlLrZcBynYBty1vM7MAvX2Hgq7zjBrxTY+Ng7qvUN2K+fuMlBEzC8bXUwsMA10SG/psBvmS5FT51TVyqgO5e7pgRW2GxIjB/JvchVdZiNsqmgAecN94wqPh5KoBN0pZHvrBBlykkHtUMPLfAhw== 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=N971Jefw225IV23nvD2d5FRzMDc+zPfI8NVYuP8XODg=; b=bO3m1Zt3LnfCpir7dH6ssb2lKUaMPmc6PN6BRd05tvx4h9V/rjQcA6XsmOCU07uG9GjVgT/2LdDPuxkWpXaSbPZEGyzKCBxlBlu6IUjRg4Hq9b5ORMa1nW6QDqbw9HIHUgHuaOhqbhP3KbneLylzfiUy7/hHFgCvbj0h40UVAuR53WMK6qN19JP1/ASKZ+b0/wh+MRM9o3dYBHcfhgTudncClwXodv35B+vrKzJrP5w+GIfu31K3daWLY1zClfoKiA/jX00x3+nvKeTTVnsq9pPsptqxmEbhstCM7KcN8j9w90u+pTR7AKYmt1QMVlZwuN+e9e6Xam7Q6dHSSqkuBg== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:33 +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:33 +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 02/27] iommu/arm-smmu-v3: Do not allow a SVA domain to be set on the wrong PASID Date: Fri, 26 Jan 2024 14:15:04 -0400 Message-ID: <2-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: SN6PR16CA0055.namprd16.prod.outlook.com (2603:10b6:805:ca::32) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 086d4662-fbe9-4907-ae03-08dc1e9ac83c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cfj9qhPQGDwx4vO/AkIe6mnoXBQYIm+lFx7I7cZfvYnVYB0Hat2odtOUwwjtUbXWUCpCGjzE05ZyeVZjS7w2zE7iO6YQHzLKthgsTyEcK27XSM3uckCaNKnJ5bcMgxCPTqPZuoXMQu55PvanNPWr7zwZERlWR159gGZKHttJlT+7syJigRYdF8dxScQmU1G6sU0GapZPd8Z8kQZAwMgDGDnpL1R0rZLfuz3BL75koq0WRRUwWfum5pjO0D0E+jgmfaK4Qsd1kmpvLDLd0UDHLuFrOVEpIkZ3fP6DC/LmY7DFHW/oZGj4zsl/mGnUL4tHrHP+Bvg5gqg7aNhihFTOYsBtlk9xgzSUEWw+VnHX35A6TwTEjqHMkWTVm3PqI0ha+RtM5WNeqhi74LA/R4iNKRRDD24EEll7Y9JcV22z+axluuYmKDc9/JPz1V+6zivPvwvVGfQazvduE1kDyD8x943RxwZyNMsqVUa47nda5DHEO/0ZgX7hmyAj2h0WRVSgwtnrMM2HJ41UZW5E8Qmf9ouJytCiEx2eJCtHFop0UUFx/pfWi1rTXUCJkGoeDMDxzmQXhtuTgt3Tw7Os0I+589DZwksbHjbxxOYIpiUN51E= 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(4744005)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: FY6rVjUZd/50hBd0ndvgFQOGvHQtWw5TaK16B9SBJevcMb53p2JjJlaLlF+TTyJMZB/Y6GsHSXPbq7fK1O2VMP/Cfybj8Gbd7xkLLCNq+AArD0Zo2HpW5+XdJZh8oqsi6F76l+ljUrVwra95HsafvqFCJilq9SScLv/LqDyLfewhlNUUkf7Hd5DSS7FwGe4zrVOKVeL1OzaHay3hO9qGvWvCK6SH8SaKchcRUw6X6LDYkBbV9atjw6bKK4sidY86gqrbMonhsTI7UE2okfPHUkxHz/QEWfF6CnQLoNNDJMKwdKJcYdnJZnkWdp67CnPpvPt584Sm83yQVoSb00V/craxR1uEuLcPVRiEZLEfFjLL3BB3FZ8v4980lYyV7mG4Yzso33RMzII1rDBzzLcLUGErBUaGbmQEccBWrLLbAZtahH9SBGucVBWd6rs082+E/y2Zwi4J8uB2fAo8u8bj1fAHDnUcoBYfmbk97oAvy2Uyl2mf7M5B7lfMAiX4mefNPOzyT6LbaPlsxGDiCJaYD8K0hEJ19CW+PHw3Y3DpOqxxv85dlfQxZpo2INANJt/6iJBNs655NR7cX73ZQPjrPUl0Bzp5iN6meGgrlt1WBOjtngIyGqCJ5KqozvRVXWxMLCx/mKoKCixolcqedpDGsgs8BMGr3ucnXeg+aHz7jCVNvP8qVFB3n6tZgGPxGGtvX6+5/GNdUgs6UUovSFdnU+OkUs/778lrgIaebUy6gVucxITfClUoX5z+pvQeb1vNOw3eN7fkWUlhlX/b4X48zoJFSQTl8t3LYUMCAH65qX3LUbFwSL5aBi7+DOB32Y8ArquNe/q+MrSNTZOcCCF0EWZj/NsSuHFB7V9RU+i0IXg5TNLGfahz0jstcpRICFDN1nu30z/ksleyTH07HDdaLUnYw6vd7JWIs7DAUXXM+QF6JmpzmTRpm/PjWaLAYCNm94E4lTaqGQkoTJ6iVKsYHhj1oyUwDKP4mdezuU7AlOOk30ES+pxU6Av7Yq1fzY5G1dbkmaR4FZ+jhoHUEL6kuub0gOPImOnnlQ3LUj89GdHKGWXGqIGjqS+JfLgYj1GGULprJD+GovWhdlVaCJXleBAhcA8Mumlvuffu6Ak8SYgpeSzSWOWbMlxp4VnkShYW7QS9o0kYoZt5SQ9iGCATYOTyMedH34xe5jNKBQKkUAlqmZoj8aHCJO+A7vym6QU4ZP2MevPjXC9laaaWl3XJGC8A4Iy66FmZ9Ac6C+I6qULqzvQX0CFaGdZKB0Ur5+KPQkt/YuKQ+f4NWqCNzjPSbebGsQ0khXmL6qqtRbafwWC+omo9uYv5NnYdoX9ueEQvCj8aoaKizrZrtl2nJrSy97ntBiXi6+amvkruAbVs6uHhKoedaIiuKW/OJJSwKr3ZlGUEuK+9VWatGe3e/rqx4ZCytH/IJcGM0vzdG27wy+e6H+l3I7VLwFPXQdl/z+zg9U6Hwc52UnZYunQV+MkDbowvp0mjtdDCyY5zUPDpokI5kHZZhRlioxOskIe93paHMex9yTI+0cBXAfKZEvb0C+tgVTvK9LGnDm238pTnYR7EIvJhkang1qiTJML0TFbj X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 086d4662-fbe9-4907-ae03-08dc1e9ac83c 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:31.3830 (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: QtF88tV7crRBV5WFHclrNbuDphNpQeMrebIBgueJIFA2yAoonUbfN2zjH88Tkh+7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101556_687128_94CBF6A4 X-CRM114-Status: GOOD ( 10.31 ) 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 The SVA code is wired to assume that the SVA is programmed onto the mm->pasid. The current core code always does this, so it is fine. Add a check for clarity. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 +++ 1 file changed, 3 insertions(+) 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 b4549d698f3569..556aa832c63f15 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 @@ -595,6 +595,9 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, int ret = 0; struct mm_struct *mm = domain->mm; + if (mm_get_enqcmd_pasid(mm) != id) + return -EINVAL; + mutex_lock(&sva_lock); ret = __arm_smmu_sva_bind(dev, mm); mutex_unlock(&sva_lock); From patchwork Fri Jan 26 18:15:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533105 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 96601C47422 for ; Fri, 26 Jan 2024 18:16:22 +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=Tv+nl3V/NxslMOArYaqdrklgKA9XoQsn2tp5AFGNaZc=; b=1WDNT6IyjcowBu 6TRr+HHGT2kiFL50FARKsqqpn+3nfXSNYHZzDuvAdocnLBfisbA4Gv/5P2YN+Vry8YMIUKhxgyTqm 0O8vbhz6InjID1EZ9bMssU6EnYPoQtsOp3NrWei9orknoAmSWoLqY67G8iaktot4Zech0vmGPMgXL GCNw8HbHo2cSxbrI47JFfeMIDWJ/eVtExXLaLNWjarhiatmifNpAcjeJriqRtW2vKjEFQBl9NN1HL Q/muwboAdQVLAgxKKdlQElSIUT/3li0hpPzBiPRan8z6OUorT4IScJTpvmifOHAQzfUqIrL4bnKli V8Tw/Xb+Rb8WLyQxeW0w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkP-000000051sY-3gxQ; Fri, 26 Jan 2024 18:16:13 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQjz-000000051Ud-1wEi for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=koScWp7Si3684QH01G7oao7DYiQ1QQaExhWiCZBbT/+MY6FJh/mO8JvrKlxgA966smaDC83oXSbAWuzJmicsoi+Uk5UNZeOfBGgR9H7kHQEYb/8gPnVd+KRxJZtBgMkRYhzo9H21WwsO3SQtnsE/54Vyom4nEJU/hBHyI1MzjobddH6frSMn6pzihcSEATxfygFxEEvF6XAL6H6W2XJwR8bd+mv6DhAhqu2WBERmH+aVgQgg/5tYCxjligC4rn2ZocNyoDJhr8YUZs2Aj+g2soArLTye1UWK8QqaUDa0RdfXJIP7JDzf78eaxc+TBaSUVP/XvEh3lZ2ITCvuGokCXw== 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=5QmwQZ4cuOWhCzdZDns8VH3h4yjbgfnMJjHw/Bn9WvI=; b=BhS7aZuht4C0olQvArCghXRqNKv3eD9T2rMwPG0VFWsyluLQu7MVEGPj7UMivkGBeOdLVowK2y5q2FyJ5LEwYbu3iXC2tGbFWa1YdCrG7NkLxfYe+hCeuclku1C8TGarwMEFjMMmPMq4LjThsFdoLOeBkT5q8kNyvtcq9QOhBiS8crjxyHY+rC6218oX82pmsCVpaXrW96MAXCJ8qetNRefviK3H+0rij3t+bwxSGs+VUsZX1xvuMGgZvw1y/LR3dxXT4lE2EBqz55jLoG6o3jDQHj8Iw3TWwzsiklOD26rEpzdEwcvAqm/sf0E7tDfwfb3kiYdJ+zb4Uto3hN/Dtg== 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=5QmwQZ4cuOWhCzdZDns8VH3h4yjbgfnMJjHw/Bn9WvI=; b=l/41OOUQDsFZOFL1wA6cT/7Icbcn3yQovov5yEt0qiHOg1+eehMhVrtdoXC9jmcRVk7iljYvDis2pBkEwNo6YoQrp2M6ueF3LmTRQJHbnPkfRD1NDI8tTVTkHyBR5bfGwZvhKec77li1MaZ8CV6qK2NlQSifFQMV3xekyOBKFBkGd+vLgCbYnUD6m0aWPsDccOP59/Xqcc5lKqy2JxMKk3os5TiBIHw3GBsSuEHhiPZPzcARyQeaqFfWj587J+kK85PcJAT/EzfmdWMw+9mbkW/3qjFZuICt5yNtW0ydjSZyfNSf/WnAvbnxE5RTma3TMbIVnwABNx0X/9HlxLDfDg== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:32 +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:32 +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 03/27] iommu/arm-smmu-v3: Do not ATC invalidate the entire domain Date: Fri, 26 Jan 2024 14:15:05 -0400 Message-ID: <3-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: SN4PR0501CA0101.namprd05.prod.outlook.com (2603:10b6:803:42::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 491eda78-db73-4e93-61df-08dc1e9ac832 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: htPiFTztq4V3xaPEw8dym4GleqdYKF2svE2/s2NG9X63cJCQ6Akgpe11/QMyEsryDNZDxIBK0yTWXmkpKxg0Aeqz8DbBv0Hhpzd+oNlveGeWpdC7whWWUIqQXjflZNQ+AsPfuk38QXMbJ52xd3yt5oYOFIXrG879DashC8Dc9mzTZioPKZ8kmgdAhPuno67PztopDRrnIaSRNC+oDzk6PGBwgXXTML0L2019vO16rKQBvCtJyVTuk5gXzwTFiIa/wt0edV5erpcKFH2kt0JzQmT6zkf+qD61n2DbByEVeh4PLdE1dlwQTMU3n1OjvG3aDr2ZNQJYTuPU+eED65GAjKJ5ocTrr5LSHfmOdiAg53DLi62+M5UmNiXhUzgH8kTvQ79RP3Cy4+4YaNFZzLzBERWmi/9fvEu9IBJzXEqfksrmLUVz3tQqOsQygLkAeLmzjmdm07huLIFmmOZOzmCJ6wToAyMAXOb0sEcMPSfDnKeZldNew4b59QiTZ9AZjAjwUwPzpYYuFULEEyc0HbCgAc2pvMNCexT19hNpQ+SzdrOyv7sAaN9NuV/gJx/+kLkaO+dw/6DjKhhVtbkztUFD3HKPpZf9uYbDMzKW52NGjxA= 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VUU0NodfsEYZy2+i+A7vInt9aE82Ki5p5KjbgND/ftpFOLEqxtgXllNrSsCU+IJkdO+DCBr7R9ieToGnd57+IEHp8klsf6TmdJkielmaq1czJ76jwkWKzoYSTU45OaZXSIJ35D8sKfyaB7XilF+DNeuIUMqd3aHOmiZsLxsz/ZVNJVNVSS7s+0iJl/Gf4AqUldC/L5hJu5vuYVkcgVkG8uvlBVidndgCox/j31LW8LyUQr8Ia77W3oyqN9v9EyoU0tvTTZvzppKB3jXCijxoGoMSTNQSdQQT+IOG6UsSLyBjXsErCphL63ajefi15Jb2ZuAREFy6hSDv7etM1KbKQMZErHXlHpsQOW/DyskxiVNt/VZ1P/eAixKCrhqA1Q7Mub8lROEKU8mOq1MLxBd85SrMwMLe9KbKjupOCWqYGQ6N8pIutl0daq37W7qx5KThi3datax9Gw+oVB6QiY3vTkPextgXmCKCog++OkHWC7bMzqLeK3jGpDIZWBdC1gjyie8wga92fPALGa755dO0mNB5kTl4PWxorC3DqHvUCKX/cTiKyZyh8F4NX4nEOvj0p5A1Lx4qGwBOAFAmymQ/oPAbpb2ZWOf36SSAD3pul/QLgn8X4+gJGPz8i4fh3lQiI7TVOQQYJf+ElmoqZyGN3hIkXNgXxISIYHGs4hBi37WGkDRPKiBWAuTLZVgaZt4tRpuhhnAFsfCPWg+hU1xYCPy6QW3Z0atgutI+rCKxhnuk1fRjuJ8zIUpULOA8D4FD/lyzTYP0d/Pph+qZfnd72YKe3ICzld8QD42HO5ZUOeJKRODffrVZ3Lp8RnjvRHfUciIOvV/qzMoXSAnFeLp6d1tzXA+P6FeFOhfTrXXyNysZlec2UKzBk36UeVeQ1k7d6HWnYDJR5rSs9DrH8mqUuWq7OEK1+gj4ss9lfZjWp4GiXaaCV6V8V7ZoKVWGmUkSKlt+Sh8jZpswz1Dpr4zaieS/QxGwRHKaBrN20zPsqzoz4XRH+Ux3R+S3xmLJA+XJkAxbyo4TjGGks6wA9e7VJ4ezq486sCg6zoVEfIAxxdwbFZiJGsyuwA3peT210vRclHkErA5ca5Jpiee0RW0SHE+tHxQQ5pIMN0N4AgDqLqcCcvOjmwf9Z6NCQqt7Ovc500HGwTqmFilIZIzgRZJze4Tx/lkddVey8C4/9njSIK/Ei3jd3iXOcXfJh/Tc8YibO8ibAZvHQe58QcyBdEGPPECIKV3ar0jbha4qQppZoau8LpaEl+A613FSUOAK7G9rmz3Y4rR9lwuDIPXeNBCIALWy4mhrpLsR+yPOc09J8aZRUd/Af29yZH/q2Euqy3ECGs+wD0Lh2jswJ+5ZAaoy10p9uhWLgyLmp9wtGz62wV9G9pAi0TFF8N0LwgZVY22iHx4LsLvjdqbVJFwsfwZb2F9nXBAkJUL5pkfyx5V/N+lV3Xbx0tm4ryqyMnGsenwkLhzGAqUMiXltRcy9Zo/OPHrITaDC4+AxMCAB35VHF86c0IM2rI5T+Wiki1dCjL6fSa3p0Vg1sTIakbFneJz7Ykw7OU4EcsRMJBN7h487QdAtEdIT/tG1lu3WXJKdZIYO X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 491eda78-db73-4e93-61df-08dc1e9ac832 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:31.3065 (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: 3y2PLV2VL2dhWGo6X3jpi5Q8As0j6yBpjm31IHIhG87IrZmuRrxUweFIhTPK9N3k X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101547_619740_0ED23BEF X-CRM114-Status: GOOD ( 11.13 ) 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 At this point we know which master we are going to change the PCI config on, this is the only device we need to invalidate. Switch arm_smmu_atc_inv_domain() for arm_smmu_atc_inv_master(). Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 f890abe95e57f7..710016ec41af13 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2471,7 +2471,10 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master, pdev = to_pci_dev(master->dev); atomic_inc(&smmu_domain->nr_ats_masters); - arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, 0, 0); + /* + * ATC invalidation of PASID 0 causes the entire ATC to be flushed. + */ + arm_smmu_atc_inv_master(master); if (pci_enable_ats(pdev, stu)) dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); } From patchwork Fri Jan 26 18:15: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: 13533242 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 ADCC3C48285 for ; Fri, 26 Jan 2024 19:20:13 +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=rwkLbEuwN6IplRdLBYpemTGljTymnDvZuP9gsG3H1+k=; b=sgEsikYemN6Gqn gMob/s5TmNWhA2OxvdZ/c5iiXFJ7buqKSoBXpeD0mUnLGZdFuZ5XZIkvvE14U5c6a9cXD6r/jMIDS bLwEqF9kIK37yRWIb9OeJIKk04Zg6lGMr6nhhPp+Rg2jlnEcPAUyFLjG+53rhds1pgX7izoHoX9LL kruSJODBz7EaNXuasGZnN/ayQK8NctM5MRl62aeUIKPeHb2hSbLGKM89NSGnXYsFvHwGS9U2Lwsdx XGrGS++zJ91dU2PqWMIJXYfcdUDFmT0Yn3YNmXXDkh/NT23ukOylLGslPQeuGHnLQQozCU+W1rWQk gAqpxz8io5gQ/YdjGPvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkB-00000005C1J-1nyM; Fri, 26 Jan 2024 19:20:03 +0000 Received: from mail-dm6nam10on20605.outbound.protection.outlook.com ([2a01:111:f400:7e88::605] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkA-000000051Vs-19A1 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aOe0K8pSoDd2zDTXyvwXXLC4q7PxdIcn++/a+/UTIqccbCKQS5fqOmcfGrtLXZnw1lWjkh+Euk9MfMK1vgalKNq5GN420Z9su9XTK0a5K1NTdI9dcL1duOb6Bi9ZOD+TOdXwemWAtb/C9KkNoka0fIP/K187e+jGC0b6reh3BRczEv2PK2+oqtt7COBUjrMPP3245osIvvYfjUh0OubVCD1EErPjMgu+zUIiYdn3EIJbmRAape3ziQEYkFS4WlDYxzGqp/qgSTVVsDIi82CRagmY0P6qBXfQAQlHnxM/bDFBdV0a50gWMZlCqluzTKS1L1KxRRDPljnEliJg+kBTNw== 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=k0zbDOBIoFk6JE6MgIKy9eXE0pi/4oPD9NUSlezPX9s=; b=ndWH/50wsYEv4W/ptqAEll3DrzJb88ZD4HQyh9hhlmt8nfo/+79VHXMKbwOHvMrW+PkCJTDUWGNN/50VpEQ0a7iGaCqEy/PGgn91sKm5Ohi19r+z5BR0Hk/9KECsOhAB2q9zlZDvCRdbU+y2s0AoFQKWOzqD0AlSXE6sOrHc/Z16QNCRBXJcM/AfVs5ssAEMwm3zG69kF/i5GHGgK+eJD+lM9mEpJXFh0uDEzXx+C+WV4fqWaSUuAtXM+7OLjPZQIDSobyxPJDKG52lqAFlWxONh6sR9BgHFgekiWMXAefFKng1xePZcDFSWGILTlwtSpOoTaWoqL7FcuDipcroIKA== 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=k0zbDOBIoFk6JE6MgIKy9eXE0pi/4oPD9NUSlezPX9s=; b=QaRF+8lcIwdrchE1bSmQIYd7mgZ8lsXGRIST/lyIel4GYvzJVNtVq7uTPkK3wsHLM41ALB+k08jY6uviEDIWFbaSAufcbBltc/njfwFR/U+qTFIWQLVfOuVFcYpildpfbSNkkfwEVddUAKdGg4nnkDVmnCF6Iy0kX/Lw2BzcAABZHAaHL0p5zVfevC1QwLz03MuvE/zZLxmwlwX4zeOMGAq16ijsBiSixo/W5uPeV3fYDuN8QSGI/q+DEg/KHS/Vz+Ku0NYLoSj8zdEO6z/CEBQwhHncXDe3+dxV4bOquN2AkhuoKiAM/Bo7tnM/KxrPRMTvpk/ODlZPwXqkcN0TmA== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:40 +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:40 +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 04/27] iommu/arm-smmu-v3: Add a type for the CD entry Date: Fri, 26 Jan 2024 14:15:06 -0400 Message-ID: <4-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: SN6PR16CA0041.namprd16.prod.outlook.com (2603:10b6:805:ca::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: 8a3a7bc3-c980-4667-d6a7-08dc1e9ac947 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hRbHwqy7BIMMyXDpLUicY9zvLW99nAZqiC4UGjifw3zUYUfqFg77oeIGqEB/wpOPg4KMDirDvzyQiUyt5Zz/i0pjwhcjtkZYndfvSgRjC7/TbQ2DmIvNBfL59/p60JE8Fdxnm5h4efgd3cfNY4bKpWFzag0MGGVpYwkzEPBwgZoUK7qWscCvyrf71qKeGLmA6ldXDzTgsVXXpxYmHHeNlVNJJqicX9OAOSRGD0cPdomHSQRg6UGLnvHIffXuZcMirdfnkS4ELY+TxvIX2k+FUJTtZUXf4HUpTr4dbdWyt+AOSO+ejg6oyPcrdlnatFuR3ArgNKE96V954Gn6KycM9SSFGxjeahgn09ZYvzxG62kqM14n0HnD3Tyhx0yeGq8CfP9+cO5Ct5UXXvhBd6xsazNJHt6DuzAQqtX/jAYcuMHTzXtwovn7yeeNWleMA5ubv0KT0r0eGMNPABaGtUzFQWzZRqtfn1vwGxw2yDgegJzcn79STTbLI4g/UkrPUUvBezjb+ZLiJex6rU4zV9vrwtGq02UKwIDOq/CQCAp8W28AuUJcTbqZRx3aaxwBjqGW+Lyrs99PtzDVV7yzOe5QANsLYGbbDMkkF9dggnAbX7PKODJJrg9Di7AMyjZs5qc57xDbJUGhhaOX6n7ZDs4eig== 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001)(4216001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KNodjc2femEDmITVCrQlhnS3+Ke9Y4rEoLbWaDAKzl57lhdg/3XtEV9fPxD188zwbie5Iw0pz3QU6IdlLRfrFXitD/WKQOanaJD8G+vIOFx7hjF9cUInq7qdHAoM3CwNhCP1V8FcdUMm6HzctIAeKCGFNNfDXWlV/d3tPEc7JPQbCqv/2bWxnuxbCMh+YPPyOo7+jacjZ3UXy3l0k7Zec3rH2MLVIPAJIinnqb4L9U2ZO8bBshZy1kWHtI5e0tmhE9MVtazCFPVfqm7oyZhemL1Qwou+zqOyizBfALC6WiqBM3FxDYN0ewLM+6xLEyS/wOMfX5hvyRVjSRVDPmSLKK1OqkKMyRwQ8OeJ38wu0/BuiQT3M7BNN7Tx3X991ln7MzFFVAfrkiJ+JZ2T9UA3a0ib90sOETkMZC8x82dHh8fSs62eA3Yt7aSQpkT7FaUXOxOFmOhGzWOJPp47TxGhp4iu6sqNrO9rZzjhNFV3xltdM+pFKjgp86X8ZiqmtSMuGfdtkAZr/k+3O5zI53v1hiBOjobH+jjdKD7OJsaUheI7ULIrX1UI23FTWUSTBP8/UgKZRBI6O2LWbySEwPwE0JxHSF0JjQDvXMp9rS5y55AVy8qyHXNwZOERPdHzhGMKpXyVnOlxQ8GENMjCy3y9BflH0957OEPEsfnITfxO+W9/hgwz+yVjUI0AG8QQsIiYLyLQP+uQT4sn6f1Z++JBY8VwMKEFzWo2P7rG1zrZtdY4Ldt0jsJ4UbcA8Vpwjv/zfQniPQkdJmC57vw6HL225GEO8y0vHJ+D+a4KydeF3yo1ryyfzZ2a7j8a+6ex8F6ds/wBTnOG3vk4VbbOAFlkiWYwTANWTsquyBJY4ULLvCx4ra1hI4p1Yq3znz4AsvBVwaNXDJxAEIXlCruEGzkbB2XSojRrxxjDtviBx21IQvhiYFMz3jzR61WrpvhxWeZDCDkgq489Y+ZVjd50oUdP8QpfcNjZxhQ6EHWCpK6j31tw83LA7av9jnTyjb1V2ugdsszqicXA7Jl5bZ62NZu4IQ7/10bVliz86JeQkv6d9KC0t4m6Xrbre0KWU9k7BeNEzTH2JnUhD5ZjyLY1TAtvOIxWSl0XRYLSbKhB5alUqSdEmDhpyhk4hhMAvG5rtS8NQOSWTV7/JWKUCLmRHjfVgWPLT5/yR8Ss8vMTs4aB5h4096EQBf8ycCcKbVDUEY4rGiTZPoOf70hrzjmicZa9vPeMHnmza+j1VoIj0aZRgbF9+kvu0n9H0H21rBeg8PteydvmKHGrB91s1UdrhabNKSYFjxKSBxKrr9cLwQBEG4iv6U435dLHGo9LfbZolHbPDsycnT7vzu6XMFyQzwLJIXbojGQjPYcNILzGXcvJS59CFkpmxhlym6IWFXt4Okde6hL1Yglh7b1QdC8En4F4/Lshzolz45YQVAfOA14GdzZV8s1KH8m0DQu36jOQrF6+cc+V1J2cmidHJP6CeqTgT9NuclRMbyY7eaTeg6uC4X5hlBe3P5vOicPoM/bv5JwtwCPMuNURYKM45U78cQQ2I8WImHEZcOZmDVP//s9W/+aYgFO2TxHp+wFz2ZNS6+yg X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8a3a7bc3-c980-4667-d6a7-08dc1e9ac947 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:33.1533 (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: hFCRT6FvBW/nUe7UYeem31TppW8s+3NRS+crJxLrFmRNNIWjvHgsbsS3R2k0UZWE X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101558_513769_1F1F1E5C X-CRM114-Status: GOOD ( 13.38 ) 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 Instead of passing a naked __le16 * around to represent a CD table entry wrap it in a "struct arm_smmu_cd" with an array of the correct size. This makes it much clearer which functions will comprise the "CD API". Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 +++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 ++++++- 2 files changed, 17 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 710016ec41af13..7798a53e14124f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1173,7 +1173,8 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, WRITE_ONCE(*dst, cpu_to_le64(val)); } -static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) +static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, + u32 ssid) { __le64 *l1ptr; unsigned int idx; @@ -1182,7 +1183,8 @@ static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) - return cd_table->cdtab + ssid * CTXDESC_CD_DWORDS; + return (struct arm_smmu_cd *)(cd_table->cdtab + + ssid * CTXDESC_CD_DWORDS); idx = ssid >> CTXDESC_SPLIT; l1_desc = &cd_table->l1_desc[idx]; @@ -1196,7 +1198,7 @@ static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) arm_smmu_sync_cd(master, ssid, false); } idx = ssid & (CTXDESC_L2_ENTRIES - 1); - return l1_desc->l2ptr + idx * CTXDESC_CD_DWORDS; + return &l1_desc->l2ptr[idx]; } int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, @@ -1215,7 +1217,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, */ u64 val; bool cd_live; - __le64 *cdptr; + struct arm_smmu_cd *cdptr; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; struct arm_smmu_device *smmu = master->smmu; @@ -1226,7 +1228,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (!cdptr) return -ENOMEM; - val = le64_to_cpu(cdptr[0]); + val = le64_to_cpu(cdptr->data[0]); cd_live = !!(val & CTXDESC_CD_0_V); if (!cd) { /* (5) */ @@ -1243,9 +1245,9 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, * this substream's traffic */ } else { /* (1) and (2) */ - cdptr[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); - cdptr[2] = 0; - cdptr[3] = cpu_to_le64(cd->mair); + 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); /* * STE may be live, and the SMMU might read dwords of this CD in any @@ -1277,7 +1279,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, * field within an aligned 64-bit span of a structure can be altered * without first making the structure invalid. */ - WRITE_ONCE(cdptr[0], cpu_to_le64(val)); + WRITE_ONCE(cdptr->data[0], cpu_to_le64(val)); arm_smmu_sync_cd(master, ssid, true); 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 23baf117e7e4b5..7078ed569fd4d3 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -282,6 +282,11 @@ struct arm_smmu_ste { #define CTXDESC_L1_DESC_L2PTR_MASK GENMASK_ULL(51, 12) #define CTXDESC_CD_DWORDS 8 + +struct arm_smmu_cd { + __le64 data[CTXDESC_CD_DWORDS]; +}; + #define CTXDESC_CD_0_TCR_T0SZ GENMASK_ULL(5, 0) #define CTXDESC_CD_0_TCR_TG0 GENMASK_ULL(7, 6) #define CTXDESC_CD_0_TCR_IRGN0 GENMASK_ULL(9, 8) @@ -591,7 +596,7 @@ struct arm_smmu_ctx_desc { }; struct arm_smmu_l1_ctx_desc { - __le64 *l2ptr; + struct arm_smmu_cd *l2ptr; dma_addr_t l2ptr_dma; }; From patchwork Fri Jan 26 18:15: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: 13533107 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 5A10BC47422 for ; Fri, 26 Jan 2024 18:16:28 +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=bIC4ggB7fkVtzxxl5k7waDv0fc1XOjFwRzfqd49Aa14=; b=tChD1W8ekEAq/o sjeGG906W/+xnBeyPSewZaN0D7Ocs+3LZ8fzw69HmMB+bIB9tbxmZ78nJqpF1SzL3MqIyMkrOn9a1 L4E4MZpWtbYTceSjCNOcYjVtSre6P9UoP2oTC6qhwLfBrmComy7TIqC2u8ebiiLjGDKzCHXxwwOdc CJ3QqKuVVMxkAXGtxxkiFZNFojA8pbHlnGyZ26fAniIK5B0Ud76S86M/wvNkIJqQuVutv7ICjWy80 FOmBE7tDkNAIXKkBHDL+R80lr7SeH4azWcMIl0d6FIlswCjMN/B+jgqNkGkyMXawFdP1qh/Aa4mzY dNjM9RKotbmzVGLN62Og==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkQ-000000051sy-3xG6; Fri, 26 Jan 2024 18:16:14 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQjy-000000051Vt-1beT for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GbB+nwmiRJk6cOyolxh/4Mkjo7Cmaxozz35ke6nh/n9R670nx4HthrA2YTOk6K8RRWAvhzMNc27c2gvj3lkuK1OuJNkMjWWJQl4jSb/t3y632TaQfInXhiueyKhvIC09GwEzlqpEGoDd9izvDn5dJIxcIFHqHX07qTLMu75w3W2m+wjbRXeujv0FkqN2avxKxeVH3VnfJUE/8l9HyL/G1JkIihjIk3i+TsUxe8FRDDz4iC86/O4CwGiNMkV65QAF6yYP5p/wKAm5ir7gx/iZsRF3639Zzpfb54QOhuyWvgu73Ob7QMSx5rG8U7QSQQ3n7Z3odOWQBGMkHS4FpawhRA== 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=8ybsX7wNoMXvzNRhE2TPKWkiCMQRM+vcy2IroGqTkis=; b=XXTTvQt2fWO7o+qwMktpLn1aHypJO+6Q7hfiExZoHwahZJSNfBsTxM5hJZgncEQ6Atg1F+l8ce/7nVJv7KzDDm2CdW5g3EHbPSHhULZfP9h67vWAOPFGJlfLTht0JmmTRz/2lHqpmpmTLz4h+gh/9etbWJDrt4hC4kq4zfskoazPBNjC+qDqH5/vTG7VMykgJpbHEJWA+52lPf+2S6rUUCctJVh7eLrOgyFwjokcCeisZAFhxZgHHfC4kRPiFPXLTqbF0cPjCbt9seDR5fsYErrjg6NKVc3wCgf+LzDpSlXjJVdG16HfCwTIIRKMfm+O/sEQfoOPCfejC3T9D7oZ9w== 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=8ybsX7wNoMXvzNRhE2TPKWkiCMQRM+vcy2IroGqTkis=; b=nOq8TtD150obOeg34RbZqBTSRlJaQU+mXKEMAQEfGNIqbwjXzggeVkKrVWCvELdKwMYCCnNthb9e/oYUU4mLRaKOsX1znk3Vwi7405QAP7UnLurj7WgPUppsye35kNytSvybgp8csr12t5BpHSCGTs7fCwuxdiJ83HYXDTxGsJeWpPIK3rbl4zW5QbQR+TERrvQe9lNilNZRbJi6FhLeOWvR5z0G+f9qVgUA6H9Sy9fns38Btsjm1Zm2yszAdLUVYEkIY0FVHrhCRxxx6MlrauwOYc16VuLHQkMTtM70Khvax29CDdfucXIXIaYDIyJhf2KlPvN8R1Q+kFA0u/Xsbg== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:31 +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:31 +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 05/27] iommu/arm-smmu-v3: Make CD programming use arm_smmu_write_entry() Date: Fri, 26 Jan 2024 14:15:07 -0400 Message-ID: <5-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: SN4PR0501CA0127.namprd05.prod.outlook.com (2603:10b6:803:42::44) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 65d7b5b4-f142-441f-e2d8-08dc1e9ac811 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uQ0yGpslr9/siVdFKite5/FNTFgjOmJQsJw9Q4gdRNm3ZMw4ybJgDGymsD/RAfeGMsRPsPPM4y4TVNMaVYgR/h5iYzx552+d0zD6vHEAW5nIdRhI1qVPoYX4E2HlKGqMBFpluWJjbGDwi6Y40juPYgS7NDNTbF0t/yuIyofqWViaSFSN1yaR2S+DKbeKjDpW6QBJf3iN4auGEmtJN7EjX7AKG/FOgtkiO/1dAoVHIsoPh7geR92KETcvgmRzwydF32oqJRlcBUTZ2Fxt0Ww7j7G2eC1GVBDnUgr2SIgS1UoeS7Wr/KelJZLdCtuxOi+4xCW5ZXS/hWBQMzbo1nF1vx9pt9SuPEMhoO4mIA3wc96GkxPLGXwsJ5w8dFeoxXvnfNvM03LqhtLgl18gQSL0hTaWNWjXPzSJi8ZPmIPZZLbaWfSQ+hHOYDQZeD2Flfk7sPFcTEHy18ZQlr8MCWPcM7qqF50xAPM+twJY0AyEFFFofXSPa1TpbCqjiNjInLAX0MpNfEGqQ50gfPe9ob5uXB4q+C82/RCM5/DhvUTpjUnbqI4776+Q3cjWboKHbNR1CZtoSbvk1LS/xlpKJhG9wvn7C6Us0BioSNTPcblm5Kw= 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kvo3CylqvSSP71KeILCtBBEEwGHQWP/xChS/SAi5TquQ4RaQuIPlCdtR5XcLoPUUzp1tVBRnQzqVuTSiC+mWOYKoIlV47UJvXInoRYbfih9Cxje/nnQALR2PnwvgZY2jQqzmjxJiA4MEmwtVwPdxbYchkOJPskFTKyDvFeLe1LqgXEHF1iFwLApGeReFuoXISfr7Z6pbI+9viQEdeTi2XVKdHuW/MyYesUpS/u3OC+rIHkLR0zwggXCg+ODL76QiK5jcZNx7q19dKyDrPn8B3cxI55XkXRnjCRnrhKkKtm1NwVC2P5J9PU9PAasI7TmA9yIv6LqGk9qf3H2tYDD4u43G1X1+uPjUi7TFuFuKtR8cRhHc0Suv8C4JKMSPP+PKTTYFydUEa1kPmuka23ST400DCFi+ComhN1eBMOps/cD/SkiJFofejCwqcHzIopBkhpwnyzpJh68lJSyoXZBzXDQlnXSJwR9mYOQU8XvZR3bJlyZkkezuy5nS+m3K1x8UuYW5/qZKiUCtMiDO7rWw+okuLojkPkqTq6aybD0xWFdQeChIotYGGUrpRP1ohiQWdJ4GLuJ53SNNMuuJzTYbyty+3w5stWR5zmnWXe1BTaI6ZhC8ov1Pr7BQX53c+cZNCWyTDoKrMXI0dyw7Js9aQEXu8zb8K5zoATlC260jrIuRATWhXrF5uAuhZ/eHviFFYNq7JByTNf3T0KIED7RUJp8rWjMbsv5yaYpEP5ejOMVWcKZi9Ad4El3wHAuBUkjvlsGKm7O42cUd1rO+SeKLMHaXBin9YP2RDpIGn+6HfJh7IPNtSPxEBHhWMsOS/A+bSWkQaljj9eCL07roZkwOIJdgigXulbPaAlSueE0R3ey0lkV3s6DbxTgmVo9OUrXXnxecdJc+i4xoSBzL8EUfamuNT9QBOOVzymEju0vr21qc9zKcTDFDwZFHAN0ByR2wXCqIrAsx6mOuXJ/4/A4udoHvJvUxIrfQSUvtETwY4bLjaQ5W2wFDBEkMh0AoJPs9q2pJXba2/zTr0JlLGIdOGZaOmwdMdBulH5v72q97qfOVyiO4I+PVPyyCPGkdmRI+Li2gk/FK6PWbrTyWLvnb5Yd2RL+/T9Wtn1RiOW6Z9Tp5pfx6HEIhGBNjO8d2FUuYUSztUuNjkdiU2soqlsuQ2PlE6ABd+mphyLE2KpX1Pzcsif4/4LEl4OVE5ogHmoApBJYpEBPWdMm8+DksEQnaDhiqTW42vCTGewGZQxNh/AB/37A6wFk/f9hrterWH0fno/EVQGn0IxwnonxK70a2VDPogSk+n6GoXGcukFedDbj60KzUyQVOTvWaG9oILpfYWuBpc4mcJ/j8eEGd0UaLEw8MMIQQL0wSYlWpAYwst/an0UUZZlABXL35PmjVdOPLyuGYbwqocYTD08iUAwbjWl+FAhpPjvotCxTgJjho/2ClpxLENo2lszrJsf/t0k2NJqoJ7deKVBIWl8CG1hc7cCAaeShTHt5lBKR9FwVXfWcPui76FLxHxvE+B3/x8t056vuY5YyfQGQZEwHDqXq+W8fF/dNc9eN914BoRPX2j7bFW0vITApweX93Z0FEovjc X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 65d7b5b4-f142-441f-e2d8-08dc1e9ac811 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:31.1209 (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: BZUTCuzlKmtvMDzvDRo8cZRALFnwvdc6WMdHvrCb3EADz1fjiXdvJxvSFUs32SrZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101546_562628_77EE6B76 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 CD table entries and STE's have the same essential programming sequence, just with different types and sizes. Have arm_smmu_write_ctx_desc() generate a target CD and call arm_smmu_write_entry() to do the programming. Due to the way the target CD is generated by modifying the existing CD this alone is not enough for the CD callers to be freed of the ordering requirements. The following patches will make the rest of the CD flow mirror the STE flow with precise CD contents generated in all cases. Currently the logic can't ensure that the CD always conforms to the used requirements until all the CD generation is moved to the new method. Add a temporary no_used_check to disable the assertions. Signed-off-by: Michael Shavit Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 102 ++++++++++++++------ 1 file changed, 75 insertions(+), 27 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 7798a53e14124f..faa60827d72726 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -57,12 +57,15 @@ struct arm_smmu_entry_writer { struct arm_smmu_entry_writer_ops { unsigned int num_entry_qwords; __le64 v_bit; + bool no_used_check; void (*get_used)(struct arm_smmu_entry_writer *writer, const __le64 *entry, __le64 *used); void (*sync)(struct arm_smmu_entry_writer *writer); }; -#define NUM_ENTRY_QWORDS (sizeof(struct arm_smmu_ste) / sizeof(u64)) +#define NUM_ENTRY_QWORDS \ + (max(sizeof(struct arm_smmu_ste), sizeof(struct arm_smmu_cd)) / \ + sizeof(u64)) static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = { [EVTQ_MSI_INDEX] = { @@ -1015,7 +1018,8 @@ static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, * allowed to set a bit to 1 if the used function doesn't say it * is used. */ - WARN_ON_ONCE(target[i] & ~target_used[i]); + if (!writer->ops->no_used_check) + WARN_ON_ONCE(target[i] & ~target_used[i]); /* Bits can change because they are not currently being used */ unused_update[i] = (entry[i] & cur_used[i]) | @@ -1024,7 +1028,8 @@ static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, * Each bit indicates that a used bit in a qword needs to be * changed after unused_update is applied. */ - if ((unused_update[i] & target_used[i]) != target[i]) + if ((unused_update[i] & target_used[i]) != + (target[i] & target_used[i])) used_qword_diff |= 1 << i; } return used_qword_diff; @@ -1120,8 +1125,11 @@ static void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, * in the entry. The target was already sanity checked by * compute_qword_diff(). */ - WARN_ON_ONCE( - entry_set(writer, entry, target, 0, num_entry_qwords)); + if (writer->ops->no_used_check) + entry_set(writer, entry, target, 0, num_entry_qwords); + else + WARN_ON_ONCE(entry_set(writer, entry, target, 0, + num_entry_qwords)); } } @@ -1201,6 +1209,60 @@ 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(struct arm_smmu_entry_writer *writer, + 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)); + + /* EPD0 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, + .v_bit = cpu_to_le64(CTXDESC_CD_0_V), + .no_used_check = true, + .num_entry_qwords = sizeof(struct arm_smmu_cd) / sizeof(u64), +}; + +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); +} + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, struct arm_smmu_ctx_desc *cd) { @@ -1217,17 +1279,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); @@ -1249,13 +1314,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 | @@ -1269,18 +1327,8 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (cd_table->stall_enabled) val |= CTXDESC_CD_0_S; } - - /* - * 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. - */ - WRITE_ONCE(cdptr->data[0], cpu_to_le64(val)); - arm_smmu_sync_cd(master, ssid, true); + cdptr->data[0] = cpu_to_le64(val); + arm_smmu_write_cd_entry(master, ssid, cd_table_entry, &target); return 0; } From patchwork Fri Jan 26 18:15: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: 13533103 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 28B7DC47422 for ; Fri, 26 Jan 2024 18:16:00 +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=ClMF/c+Bb2KposhZYGbyvwdIMpmUOP1Li3j6CfIjDkg=; b=cWf915lSwCwzLR YQt4ox0QRlmskqrTIHODr/o22aggkUUuWvGEqaIXmkkH/EG8jE4xMHp78hB6cOFhFTZidW2GHphp9 XbV4B1NU5s2/fwRz6owRhMeqFXg6gqhXKU3Pqgcrt0EsqeNr0eoq5WzjmJG1jZWPta4ZsDHKFlSxW G8LTzrTMoElOOqdzY/mOzRW5K5YiTjKXQfzd2sK8cwdRc82ZiAvftMSksOrDlujfI3yLSZ5rweV00 3pDUtFwkHXSDI4A+Un/PXWZsDebc8x0Vfs+OYRxioFCRABXM0FumotuBB3FiSV7sprvlu7BOpk34x vSleYpigdlbDkm4bD8KQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk0-000000051Ys-3rh5; Fri, 26 Jan 2024 18:15:48 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQjx-000000051Ud-1wDB for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YWHSTHR7/KCKvd3rZ59xpcFmIfh82oqyHFG7pl/pbYWVqFVW8KjryVuKXK8jtjktgdgiHSVBL5/6RUNUwtc/OjXpymgoaqPPxWPQh5W8r2xp7qh2ti0bvYNFGQkN53lsYeoUZBhbG7fyuGtunW/SYcV3uHSqWPuFdOBoKtYy7gswFRCj11xUqkR3HNm3RWcb/X4HL6Arpeu00VIVA2+ZurkhFyGNfmG99yd3k3XZwchMRH6AqPfq9Js58AQM8+aQa8gcWLqHNxn7MP5nkCeLgi69ao9JYrKwmffBzmOypJiOJGJRjb2TGxXrlrs5zpE8BYO3zjl2jZVeTIXdbm9dqw== 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=2DsLic7tyWvi7ro5BGBFjvUVtIfg4r8Z306lbCoOIqg=; b=ISQlFxwu5p6WW+3l+QpIGNR1/zK5ST9lmQ0gB1xeHMN9rfjwxmfAv7Cu0BSbhCvvIXb6NNjB73ztOVuznZxomz8dtBS4n4XcULEIvkk+hylQGs9nmoMiaEAJxqvjyO0O6K3RMPTjB+/w73qtRBHIJ7hMYBfwXOfdy/7rh9+fkfOENcd19e8tjQuncZYosaqgZXd04iSfNE3ZFjZjL5BFiIfllBKsCWxbMwU0/Ia6+M9xHnWZMUqQJS0VbMzZmvFJVBZKCh88Tym3P0t9MrN2YUL67jzrMeSINaALZvekhJk9vWlIJJ0rOkfu8uiYjbzTrEcVoJuKKfO0pojDQF2lsw== 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=2DsLic7tyWvi7ro5BGBFjvUVtIfg4r8Z306lbCoOIqg=; b=CdJWLLD2DBMmoz8SPx3/51jCPVnw00tGdQEwBjRtRN3Dd5wZHO0DTqLs07jJ3WT4kXSNlHsm0jlzzz2lUOJTjN7lyz5G9dnvkIZs7P0FMy1ZdeEyhK5G4K4gpzfQzwzH3u1tfAGnuHW72gXymLU+0yV8PqXP7u+EFGBcR28WEE2+OeUwVzeR95RyyodF2pZ0B6rShm+HsSJbXhTmYyo5JAWfiREcJlPZzTKra0y+4NbMvjvuv/3UYz79mQy2si5my/o0pSDwbUi8TOzUKpLggEqiabi3jTgutxqfHwXDdhG+rUaGM75VPgPE9fy7pD/HjBMAqj69OEQ8PuTWggyp0A== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:31 +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:31 +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 06/27] iommu/arm-smmu-v3: Consolidate clearing a CD table entry Date: Fri, 26 Jan 2024 14:15:08 -0400 Message-ID: <6-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: SN6PR16CA0053.namprd16.prod.outlook.com (2603:10b6:805:ca::30) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 4c8df419-2315-4bac-1623-08dc1e9ac815 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cBuPAQ5gBkgzr+gSeMwnQ7+MkwSHhr+ZNsPtQcGdXPjB4GdVK/j+s2Ep15zvxF1wMbcedhgoJREWniMVaAdIW0fdo8qSAumkDiITLN6kSRFZgS9F2bM2miAnP8RiqQibRPQVarsShxj2/6HHrKVSsIONkaOa7nqmf41KsoQGsalyjAAIKasx2h5JDW7WfZLnx6nBr5+i0tOd0NIhs23zYKyzEIfb99EN8Ig/HhewQozsXS2rqB0USWg1uWPg5E/VZN+rF3uSaMiSBGZ4jgW1teUW1gXCmhdtGxhWlcg9LqJzcpI+6MnjMVySZ/MWX0Rl0qdh/RdZkgKSLmc1ue4pABFE+XkZwPqksnR7BEhivIyRwUVAGVxs2Jssb9YV5AZG7nzwDeBS9hzJMZ+D8QqUjZus0C2+5fRwndXvwtzY0Zja0faf4XWc06UDA2VJWE7LlIjBRIj9yfegCpuDkRgSsEOF1/SwdcGqEwMYttVWMg8uZ0YUqOFxtNjpPbqtEJb5T0GGqaSJ6J+Id8CJfMz79y6Qd7XFZ2YRWLKa4nOHVlW+L5W/iFAn1Pa8cVSpQp3XvIZIrc5S1a/AiOz00tTVx4ZxCfpzVtFgDmlg33Hn4Gg= 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iGQnARit2RXem3m9NcDaSt3xBlK4FO+d+2GecKN1xkKCku6sUA3nxvvj9V/a6HcYhOClKBiAd5UfaoAT3/oN/hF1tTmAniIBWb8NuAKhUi/ewMMoBpiOmNJR6WPosGJYajwqYYkQzm/R4UAXiMDVoQqvddUazTmQHpcnehA+no55HZWeruVGnj2Z6rxb0X+Zl7qn3krki0ueF670GDi2CmZZfiYN00g00vwnuNrxZJfwPKAxAaeQhBiU6f85O+2DoydGqjDzl6MTYIJzv6B40hzNhxHuTrpJKiS8DIAtazpEWlDM1ka3IKl+Ljw+tn5xZ99xw39PuKr2mlpSJj0vHAVp0FfzjTabCOYJEqJJ+aMMSq1HDT1dDw2kDo/gYlabkr57H70N3t/Tf8X7FiQpu5p+UBR1CQS6pDobpEmemaorOyo8Qx5k2G41A5NsRcJuVd2kYUmy8vzlIzIfQc+WeB4WGhsX4iw/CSZWVc5aC0bHxSJyVhNBJO9mdix1o68xcxbOZiCfEPJ6gX1P2d8vEgjEBXfOXj8IdlCAAr2XLniyVFnEfmICMpfN12XvEA/PYynlledoBfFLoXlWUxwh8GP1G0OAgp+rvzDqQ8sxkbas2Fok8FsLFkwoAp5A8HU58BVhWowaRY2okfRK9APQVVQq2jWYiLtrLhK2TeJ1vnqRDoOEjACSqr3iFm8EYGbteuPx/8o+C9mWALeGgR1kzJfXIYXFkXDlwwaDS9t7e4jlBAS5s0EYdaaaLAdwXVIEkdIvLfD+5tZPBMddIt+gdAC7bbJX21k4zQZF0CdNOCCKlaUjadb8JUtPKGs9q7qLF3hNv+sJNIGaAoJ1mWTFVIRHpAUGQ+fL1Hgz5NsSPB+R5VGP69gNVTJRPT00BO+AMFE3joJ3g0s7EiofMkvrXRM2nEIzTQ2tIBpTw5LHGar4BFgV0xDYn+fpWteEv1V1vLgOKKB7d8DWfy4ZmWdWB/YdOibbcrTnFzb439+WL2A9mXFmao5tw4DQ17ZKpF0yti8P43i3LwzJxX7yGjaO7vvk0/C7ph7slpihv7dn+c2K1LYRYlzic090OkfyylF1u2YJ9/LiRYGl3iRw1bE85D7AUc9qloBWoXeVIJWWNhLxK1splpbIG9cMfhYEbOU6ztxMZixBVQCZgutagb93FGSAZ6gSCoT8lnPog1Ss7DmgJf3rCamVcsT2JeGOGmeq3ub3BUoIkz9WiLyYcezt3/FlwONyO0nshtMOTFhEvPdGpk9rDZunxfesE8ZFeMKM444xHWmTK4bE1Yt4+wwhikJIrOiV6IQ46VGJ6fyI44J4S0vJJw/t7CoP45g+iOfDb9bPgygy3u5XQCuE9LDQ3g7vqee+n8DGhMUwEAG/38cD7ol65HIvX3/rZ9JBNl8CxVR5goBGxJ+I+ieOWIiF/hVCfoKfA1pViEs2qLQbO5Z/P8X2JVo9Yw+uM1QRRVqpbm5u1BtvokjSmGVcq8r4vBIGWgCraDSGBLkpPWI3HtKUUDoamdfZja3KZTmxJm4s6yxvINlI4ntmqtg5fuRrUdhZWlD+L6qK+qOlGQc0w6bleNy9Jld5LGXRu1/xQolQ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4c8df419-2315-4bac-1623-08dc1e9ac815 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:31.1511 (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: ZgW2wtJ2QgmMw87SAawjSNqFA74QLQZXLvN+aaPf/KS3U/Rsug9noq32AtvcUs6k X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101545_545969_97900DA6 X-CRM114-Status: GOOD ( 13.00 ) 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. Move the two SVA flows that clear the CD to this interface. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 +++++++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 ++++++++++++++----- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 ++ 3 files changed, 25 insertions(+), 9 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 556aa832c63f15..3eb32b9d4a72a3 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 @@ -332,8 +332,8 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, if (ret) { list_for_each_entry_from_reverse( master, &smmu_domain->devices, domain_head) - arm_smmu_write_ctx_desc( - master, mm_get_enqcmd_pasid(mm), NULL); + arm_smmu_clear_cd(master, + mm_get_enqcmd_pasid(mm)); break; } } @@ -357,14 +357,18 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) struct mm_struct *mm = smmu_mn->mn.mm; struct arm_smmu_ctx_desc *cd = smmu_mn->cd; struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_master *master; + unsigned long flags; if (!refcount_dec_and_test(&smmu_mn->refs)) return; list_del(&smmu_mn->list); - arm_smmu_update_ctx_desc_devices(smmu_domain, mm_get_enqcmd_pasid(mm), - NULL); + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + list_for_each_entry(master, &smmu_domain->devices, domain_head) + arm_smmu_clear_cd(master, mm_get_enqcmd_pasid(mm)); + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); /* * If we went through clear(), we've already invalidated, and no 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 faa60827d72726..ada4e85b1917fa 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1263,6 +1263,19 @@ 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_clear_cd(struct arm_smmu_master *master, int 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); +} + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, struct arm_smmu_ctx_desc *cd) { @@ -2691,9 +2704,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; } @@ -2741,8 +2752,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 7078ed569fd4d3..1b4104e9cec3d6 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -749,6 +749,8 @@ 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, int ssid); + 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 Fri Jan 26 18:15: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: 13533249 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 DD4E8C47DDF for ; Fri, 26 Jan 2024 19:20:20 +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=ukOWFkraaFHdJkbY7wNrXxnvKPUd9s33P4f96KCr0M8=; b=NQ+vkYA2L/POk2 C/zz6R6KHQTq+FcMTUGSTSN5N2Fr55KqD0FeWFBJKl/rRtR9/XdiK4xlzIb0+7hGfXamcmyTtxRxh 467bDWnBbiDQsIfm6niGabY4U4z5tvHTSoGNDgwbjzdi5zos6iDnvDOEyr6lRpM95YJihXoffaMgi 2Mjz1ETDgBHYUtfa/7+9TFrL9SmvPwZODJwxXLlqS+odGbagPmc9V5EP+IY3Ji9WxaI30UGeCUItd ndqaHsn38UwqjgyHcju6plaNc2qZOjE8Zeu0tUmB4lr3mLK/aPsa6RjfbhX2atHvYgiGLBVFzxBRm PMmdefZjFC4gSxg5LXoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkG-00000005C58-1vBn; Fri, 26 Jan 2024 19:20:08 +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 1rTQkG-000000051ef-2Rpm for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V2QNb0nGSZTHgvw1RUWnx6cF2VKDrOxNBMGVJlExzUcwWNyLNDzLSMtFujT530/oRcVJ9E3VXRReXy5Pc5N+J5g50q/ASxc25N+6qWZsHImWH6fXuRzlQXRJ2T8LVDfhWBk9Ji6yOr4rHt9pXJ6Pk/2thLrlLpLDodmHjs/JLME3WcFNcnpfcS4COuCd1TlFgM+bWdkwe71GByGICL2NULrWaXmpnNud/jc9jBnQP9XcFZyhSRkn+W1YWMIdv3uLnhCgeSqFYPWvc4loXyUI35AgfD6MLdAdiqiMU62HyMz/aoOEdEOjaKNnVKfr1BOwolYmvS2yje4558S4PcnYAQ== 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=65M9v5P1VRdzLfzdUpvtgeegfolb9V8k6j+NugzubhA=; b=LG+UjxGs11+FRNp57VBBMkW8eqyoORohk0HKn/EzWQ4ssbUnYtkEjyb4Dpx+OPLqaWVVuE7Z48zwBrnDDs00auSAoW5fliqtQGq+ezSrKZn+p9ikGeRzSitaB1wIL2Or0ZgYNUy39folvVQLaC0CsscuCMAQl6/glzJLicdEXeIfoyXiyfkKhoPbTNE3JfTxOq1Q6yyNFMr9hIRNkuTyrE7wCFzyREj0DBOinh/XI5Cv3wwS94OnwXwM29ILwGDF5+/OhHQKnGKF9OAGbw0jc8YaJ3hOWS1H/RLCrdRSnjoqgi05GqUs3DabyQ9nCI+z3AAktyC0li7kDOIpALZIhw== 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=65M9v5P1VRdzLfzdUpvtgeegfolb9V8k6j+NugzubhA=; b=qb5d3q8DHFqJlkdAx8Qn8yczTJGvp0vFnRsbzFHnLqQi7MoRZTA0Kn1uIeB0vg+DA8wo10KNEALCM07wkvaeBsfBs0zRD6Q2VJoh4fewfxlD8StfFCyT/4/sAEy+6krh2Hd9ticSRlUXLP6xzgVSSQMlICDCNAsZHWtZGt3MteWUa20nzhOTKJCjqzlmhScBTQc6FQQYV8CutCVUNDSB4974xTW1+5O2980XFjuxtXIpzRYzPP57N0DIRLcJaeNHuF6nU6c5k5uXkFSFkRLUak0LPNgEPNSOQ4seJmlVc4IfYRylDRX3WKMxsYbuW+v9o8bo8OpXfM5ojzXvTfumVQ== 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:43 +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:43 +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 07/27] iommu/arm-smmu-v3: Move the CD generation for S1 domains into a function Date: Fri, 26 Jan 2024 14:15:09 -0400 Message-ID: <7-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: SA9PR11CA0019.namprd11.prod.outlook.com (2603:10b6:806:6e::24) 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: 9e78e482-7dc2-467f-2de1-08dc1e9aca29 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ThkCFLFtSbV6qPV+0y1Pw5Ger/4Kc/TjL2rgOpewGCiuZjyvYd1BWppbcNfWxBeBuE6xYsNXuOw71g474EzBqrAavQixlOliPTU/Ff+GrHEjaHuYRbcnZPd8Du/+NKE2uQoTROcueeaatuGejOwYzemCKGmJeww92XZyEpsHJRm/XOD30LMM/tX56L8lUFG7IT9s2xd5b/Nu+bH6CP3p3FAtXH9ghDLlRuU3sNw1a/+3fbu1KYiFtEkTkopHCRUYI+mvJbXFx5zWYAC9XK3EhjBxijL/WVo42aFshRK26V3LS3oVsZpIf1vOWe5Aaiqf1H7ghDvwVEsEvyGMZkyiFC3S0FZO14xPLL1phkErUqSt9v3c/kt+DjDPhStXKJd8iiGMD6QtO/8i54Ie/vYhMHAz77Fa8+Mh4q+fa+sVeVFtv9ycPWfGT3P1ovKyN6jDcPpRN9WupmTbqoyJFN30g7akOgrTcNawE6fNxd1/IxcYib4YBrTOOWjSPx8KLLtBi7a5a6kPIepVhDBrtr/wASUoEEVBhVFLLroCA8WSghu4G0k8DudeB6tegNorVMI23rm4mY2qIgXcbRY9/jbz9qOVAmrifY5LmSIgPk6QHbVCfJbOpGtbZX2C9dAG6dj3dHWUmITIJGhzMQ08qQ85Ow== 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001)(4216001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2mwG3RXu6LVoW40fYeo5jOVSDZ5q0nI742XOFEJEfeSUYVamPU3Ux1kkyURxHjjbICLghGKmtAbny9s3ltDPl8cflslMMJK7MwpKynx77SU1dpR5zWbfUGpMiq0iHAsL1WhF71Z3DXKYzQFJ5E8qMN8Z/xmoM4ge64hj8SHiB2W+5uA535Ue00+asO6AHaFcgbQrA/a16Fs7MS1LWqtMXasvRfOrTchwiqpUjg0sdFK4PBVnb9aw4q2PEPBoXah4Sjj+dAzgJ99Mfn0L6LR2GIWUUKOQ+8ODEYCydvAjyu+v3DevFvoldUBW/rXazI29FUXz8hg/LwOZmOiXmUXjHgieDQid0krSbtF4haUl1Uds0qPT6qtWGrDWVGE71Z8ELAt3Xs19p0JbBR43TqtOYD6maw5vLEsODqdeL0y2/c3E8mBNjAgJL7tgeEAYc6f0jv+hK2BqH5BYLULJ8qOL9I4sVfmd4Q9tfJ8g5p+xxWmoVrcHejyuspGFj2qSX3MeogsL5p0vF3QSuXLdXvxnykHUvJ7Xicc0NubPksK70O6zDBJ45AeTy4j9q/aWh4gVV23J4xMswEzL62h1H+lKehHgyoCId6gRCoF/CCFBmBcmBwvZPp1qFsK//59ZjfbznpBT0q5HwEier4Oi/UUEh5gk28rahtxtdwpTvn69Thj19FfuRP+OKYh07+eqsWulbxxj57YeTswVfGrQM05PG2ZxSZSRIu+f3mj4gKrlEs523oSGKxWmSN//lmgFHnH0DUGlk768phU7aWINJwW/3R1hdrzEDIm4xxJIhF/VHiTxLN+ZkAbXHQTnoAzWQLtlcysTZfH5HofDalkhRHn7ue3mKDjtD3v0a9IvIFKXFzSXcS6UcmVy8LKoJSixQBFUmWkFNYr8eS6Ga3+hghG2PEVJspfau8/msoBcCFXv2yCji2b8HIP3caqcA+Exp7RQPi308WrJs2+HnboBLZYmy49w7l1J283sayL5NAm5GFXHF+wFtj3k08igEqJXL6O+2S+yrJSzjd6mfYVkxhduSbb4648mkB/J0xBDfVrI+IiBsKWyJo9AgvQHUUagXFbq0ByRPV6qiqebf1yM53y8w0+F6UIySfVoUfigu1sol8F/2Uy5Q/qkgWqTbBsoYfk+GCn0vQzoUi4ShospRjpuTkBzVSfqhg571asl3Pj+cgXrVpjLihQ0mgEzYdS1YdxRuuL4cJ4J0dbgitVvfyj2t/jFKJ9ocph6NTIcwsgj0X14apQH/OfNzcn8VQ+gjstEC00kR7Hdo4QfXhaYCfzC6mo2EyjWg1B/JqIf6IFZzkxY0hmYZRfizHJI2kI05QCY9utOZZ4A/cMs8UkRaqpqFQSLb70kQowwwuUDlvi5TDXk+TYY0Hdt/zIIadzSzQlsj2iny7etXZzCZgTBNk/8Kci0jA7l7SxV2mzqa+thqaJHS6dGiIjAdzP7PEX8w4WP7MVbR1prdFyCAUdr+WUe8SPevSeu3bm/XaJVwKojnVa8CGnvsXVZSjQ4YG0ZBNrV57ZMVX6GwvgS0wRg8eas3ntY5YXdDBX6l30TSoDaI+hpeSa86YvzgVu3qRPtVleS X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9e78e482-7dc2-467f-2de1-08dc1e9aca29 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.6718 (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: eCIYfNt1xtBfbeldpkmK27ND+SL/lE7F4w5ugLZQPyZnsCvI+Khl2yfHWpxGFMg7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101604_795532_C4D7013E X-CRM114-Status: GOOD ( 19.74 ) 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 the forced clear of the CD during S1 domain attach, arm_smmu_write_cd_entry() will do this automatically if necessary. 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 | 60 +++++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 8 +++ 3 files changed, 75 insertions(+), 18 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 3eb32b9d4a72a3..00d5f1aecc9585 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 @@ -54,6 +54,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. @@ -97,7 +120,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 ada4e85b1917fa..e4e62f598a0859 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1181,8 +1181,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; @@ -1248,9 +1248,9 @@ static const struct arm_smmu_entry_writer_ops arm_smmu_cd_writer_ops = { .num_entry_qwords = sizeof(struct arm_smmu_cd) / sizeof(u64), }; -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 = { @@ -1263,6 +1263,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); +} + void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid) { struct arm_smmu_cd target = {}; @@ -2678,29 +2704,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, &master->cd_table); 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 1b4104e9cec3d6..ec7c906098ec1f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -750,6 +750,14 @@ 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, int 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, + 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); From patchwork Fri Jan 26 18:15: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: 13533246 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 2F269C48285 for ; Fri, 26 Jan 2024 19:20: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=nuzd4AOqZvSJWB0L8y5Hlb/YT8Ni2m5Im7xL/GL9F2k=; b=uvE+9hRZHT/pVs PwmRkcIWXAdsbUgcse8OE344u7KTUo5lfMzUF6ANPnRdv8/ZkoSM1u8wnkrIuWLUrqtYgAjshY61q QL+eeIAgsm6wXb7CowmhhJ4UICOFVhIfWR+0I/L5N+WqtFeqpr40spEg9HX5iiIN36rjAm2tQb4Oe Dji+qViKQw/oNsNpjZ5ASmUvkdtiIOPg0WccXgFUBUf4q0WwAzUUOihlzBkg9OvVs/V9pQzjClRkA g3vWaIsKGhAq0ofD+mzK8CN0RIfdXF4ZamnKkz0OUIa9Jo0w1YF4x4Evpdrlr0PChL3oswILpKC8U duYohQZg1hvDWxpr3Kuw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkD-00000005C2y-2ucK; Fri, 26 Jan 2024 19:20:05 +0000 Received: from mail-bn1nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2407::601] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkE-000000051Wx-0NfV for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:05 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MYsPThVj8DSamUc+2Mp0qTYMWn2orbpyeuM0UKVhWryLWQgmALUiuuXDcCkZ3xtt9V4j7Yit8gzjct9e4K2WfrCA9v9kn7pLTTE21l6LcbfCcL2K7o06GD3ShEjWbA39dFjTSaAA8CxYZOBDZguqjx/+W3yHvakg/3qMLxN9w7BdXSFqK8bWxC/dwKOv+kPrYuzqeZ9CrVtoLCIOhfDVso9HQyBcAAfbjdlHic6s0SL8N+Qv8SNIdqfYvYqXKyCNY/MIfzUxjl+JoaJj1iSFC9qf/rs/v5F92T6hFxqV4hIg3oqorsfvHrlp8VrP/ELvVn7utEdHELMsZFfS0se8Lw== 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=rraAjs0FYBWA7Tc2rKNs+wC2hlKlBTvC3fi9lCb3Nqc=; b=D/0bAR/1Bf1WGWjuBygeJc5ibWjRS8IDnwQ5BC7JQTzV/VDwdNM9tD3Zb/CplWlg3iqrHblW6WhJ8kKwj05k6Hqx0qN2CYECsBi3Pu7rmLVnWGzO8NLDLck3AQg0XQPuqTj3tlgmBPiCBoUe1L+zALGBtsv+LyMgyqM+A7nWIyybVuHZgjDTANZoRbxs6k2isJBH610r+k+AqIj1PPskrTQoO36JqK+0EiTg1fo51w1XymHjp7OkFhWTDxVs1KNqVJcHWuiG1eYpLP2+BS37cI2SlMY8xyuOKeqU53UpV+i8XA6dymlZIRCw0GR6nQ/t1hmgrR6Ibl4wq2JdZbbAeg== 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=rraAjs0FYBWA7Tc2rKNs+wC2hlKlBTvC3fi9lCb3Nqc=; b=gBOTcVj42xfJa/pJSS3tMaZOmM97EsIOFAzjVvYhLHu/P2N72RWCrSIMdWwZM6J8c1fnlv/WT0lmK4laqinMNGDXfJDtxq9tXwO+vwAhgOFAfx0SWBRzhECm4BDyqM8Up0HwtacLTjCNW2Q/ajqoMq+qrEVxDfnjsHL1AKUVuLRJPpuAlDpcMyYeXLKOTdfVtRaVH3Fgcj7Ev5b9TwBqNR2nybtTLTCrftol9QULm1VDV6WxTYzeZjyJCKpRJ2nyc1MRUYiLSOm8HqzMHAflH67SeoEZ3NFuYddPFCS2k9QRDIFw2OB93vxIIZgfniM16/ZQCdDruiDNkZ/05gtipw== 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:43 +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:43 +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 08/27] iommu/arm-smmu-v3: Move allocation of the cdtable into arm_smmu_get_cd_ptr() Date: Fri, 26 Jan 2024 14:15:10 -0400 Message-ID: <8-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: SN7PR18CA0001.namprd18.prod.outlook.com (2603:10b6:806:f3::15) 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: 279659c1-f9b6-4646-8d16-08dc1e9aca16 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ik9tzu26bTr6oj6XMcUW2kz5pgqmAD/R0OuMXCmo9k0Dq5Tj7eo9zO8i1e8LgPyfJJnmte+qA6BZTtxz+/QUgKSMeRDErsLsGiFoDo7YeNQ2eSOuVUVOAof6FHwl457PRcUs5o9EAKRHhXpO2sdAjcmoPx6k/enNnvSaG7ZO07J/AkIRKjZ6j9NLiQzfPtoHLM6Yl4D+2e5mU57mC2OoMvgPV3F8fK/D0LGruljNUkhNlEENDC9ct/ujSxsxasSBMW0wlG80Zkhi2+iGut0hidD3KeFAWSgEdwzpPcZGP3r2FhGcKdYd5ihI08oXZOSvzD64XTKjN9dXtQHifdozmEIuki4B49dzhW+0p+Ajw0VBMNpHcWmdGpkg+aNFz8BVFQhTOE6vruhlW3dJmftst+upln2IksXnXXd0Np+4tN38qPIIgEMGwqnqFeCVg6bl+cDSJOICUNq03IofUMjiWlazq2JXPUKFPDY1L0/vf7amFd1YHmi3MISeodGiYoiFEzxO4hZfWkXZDGicl7COERvnNqTm6BxgHs1l0qf2fKJ1dwq13ymIEqBveOna8Gxb1tv/IjzTCqsriwmo/b6xzevo5KQ7T2e85u8UGwlHRy2o8rrebAztagub3YTtff8fAJxQtBpQa8LHV2wFqgO+0Q== 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001)(4216001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5DVaoa/uBSJuwl7vMyspNu13YrcN/2KphqyhOcuuZbRA056HiFBXqKI0MQy1AXwmr53Eku/Rj827Vudub2V3DiPzsYTZUlLQriEth1OHA2Y2Wzl4pghwi3TnDWp97dMTgEu2d6Hr8ATY52FT28F5RBS+Kq2dnfVfveFzA4sG6GD3n8z/Y+0zSQwn1cL+5HW/ApmJw0aO6O175RxLaZyFB9Q/mHzks/IyOTyELjMlWnhm1ngtLN+/57gmvXWQk5oQywsz+Py4hau/ATV8XRU5uEkew4n5wGQMH1Xr+nDjLS1twVMLmjwYgK9J+tI3ilxBXqM3v00Q0/r3b5DUKd1EB5rCXOh8jaJIPequTiqtSs8N9rDS/wN4eWsxq9Yl03ufDSkgQHsAEGIJhBKaC9iHPqOT0ASp3dD44YldRA+kmDECO3J0eZFyTwRWJzQRy3dALEBemTGeeUQpXNxVs5aue+QSPCb50bjKSXO2D62/UIDqNBUmmae+Vzr7tCsZ3sWt1VGBoJu40CQYbrd/4ic8GV1lSJx2j4r/fwjjjGVIHqxOSCP5+FJcgdXuBV92hVKyZczsUSy1XBqDwAv2Uhyt4W1QGej8zfaoh1U5CM/8rFbkXAd7L8DFKJINley9dmSdgLy/a5O9QyvDvaA8RA4IRwlHx42gtRDP+upbPm85yQJUXqAh5DO792/bzaYIi2KMAJlVQiYoUSiYLb1P4XmlruAkgcItY3IFVatkRuchoZM5MENZSSvGGIBLoHFZC1QHS0W9OiDG+5fWG5HCZfCm8ZMgpFSCKe2h2bX1X3uB+A5H9GiazbUp1FD/8VUeadTesGhJPsL+tEEFvLJcmANKZqgZa9h2QOq/2Pm/fq1w7hUNzRsWoF1glppueUM88ShGn4oLlMQUEpnQ9UZnXAGnjJe7n+MCsNji1w+IOJlRuUaQFSqm5Ss67DzU8xTRTlsSajdhm5aZHhZt2ucc09tMuyNH4R4oXutnDKjigM1dFUeMFKrk+205oUsu4uI+VSORtZVSRjGxHJO/5bLDhcprdBHpEIjZZjCWACu4OUWEHLRIyS+8VTWa5nt0hrSCrYOGwIttnRQ44I918r0cYmfb+pgn0XLSHbxCzAepSmyhOSRe5XL92nBwngmYSS7zPyaEVasUcEDc+/TBAjltz0fWjxqo7v+Oh1lmFKBFWJbxKldIBa+FCpMvbn407ijPeUDCbi8Wnox1fozFXT4ZoHo/gIZ6h5HSywEN9nYSt3bs/I3ljevSRjeCuRlQN8NWMUjKvIkjCMAn7fbMzw6QZiO4grG0KChdAYY6cQzMNIVMq0q1ffQBXeXxhV0RferZUqS3hnGVIy16F8lX+/3EsmwT9rY21NyOzH6MJj7dNqM3Ck9BgXO7R+BNTWhQcb+mxm7V8Cyu7EEubHPMdIWxPt5axaGrZEUSfKcG9OOiBTYyCJd1qxqpoLUQEx/zXj7uRptDg90I8G9WdEIOfw3NoGWHGawuIPHI0W4tTVIoAFGYkpKv4H4zAy6ZOI5knG2vaDkRjbOAtvJ38F9A25TiPHdidL4Iu4cZaxjHYqeuvq8lTidJnjHEO564QcAVGHreYNY8 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 279659c1-f9b6-4646-8d16-08dc1e9aca16 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.5186 (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: gu3RKLLT4plMtARigqj3GwVbnh0BlxQxgTn5O2Tl2I/+c7fY/bEGA4H0f2er+ver X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101602_261884_D96DBE7F X-CRM114-Status: GOOD ( 12.27 ) 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 No reason to force callers to do two steps. Make arm_smmu_get_cd_ptr() able to return an entry in all cases except OOM. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 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 e4e62f598a0859..7562d7577f3940 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -108,6 +108,7 @@ static struct arm_smmu_option_prop arm_smmu_options[] = { static void arm_smmu_rmr_install_bypass_ste(struct arm_smmu_device *smmu); 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) { @@ -1190,6 +1191,11 @@ struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; + if (!master->cd_table.cdtab) { + if (arm_smmu_alloc_cd_tables(master)) + return NULL; + } + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) return (struct arm_smmu_cd *)(cd_table->cdtab + ssid * CTXDESC_CD_DWORDS); @@ -2708,12 +2714,6 @@ 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); if (!cdptr) { ret = -ENOMEM; From patchwork Fri Jan 26 18:15: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: 13533109 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 C7E02C47DDF for ; Fri, 26 Jan 2024 18:16:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: 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=lqXJwUnoDOx3eamrpk09Qbyz2My9AQza5R2gXkRe1mQ=; b=2stWLn5q4r/Hlg 5eNb9JiJ3vYpy3vraBiP2Q9ZjOl0Osl2UJ1J2/5R+kRszn5JQsQtGPdXr9Dp0gqMpSTwzB5jwHB0u W7FUi4u5+FIOMl08jOWABdB6+1J1vT443rfq5gbWVsGGuufUF5V/1ecTaWtid+VcxFPGQKI+AvQsJ UGK2SggClt5PJbc9RYmSeZEf3ggCAcTx4dhz8WkgW1vOyukxww2+Xejy3deIeWkqbw7RQeiyLdjwl egolqhxsZxtutSGXoBOd3Ufik0gocYTw4BMH9TUAHhvVBPCL/pPejT+6OKsAW0my+Qfq7rAfz2qEZ dPBBIg+yVMCMXiXa23SA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkT-000000051u9-03ah; Fri, 26 Jan 2024 18:16:17 +0000 Received: from mail-bn1nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2407::601] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk0-000000051Wx-148K for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iUER1WAgnNclg933HEkL9ND7TeoXx8r6SYGoN2cNrXEwVb2dH7mxJdJecltQJJpnFtNqO+oBZy3CBSv/n3UqqIwpz6iTgLm5B5Sz7J92DPG5AHFb1bwZAZbjdme6ML3fU1b6MKgFSikGVXoSSVINUM3q/8Wi1UWPkkAaI90+hMTmUp0S8c4TRzNVGVByKBf/Qiu08lB38eH4tGpOHBmjVI0t40t+bj7zoAi8ZkLsByap9CEPqV8e43FGodL9mbuZq07AQCNBfM6yoWfIf3qDngwR6mSNw4r+MGaZyMDsWFTrzyQD+ilJBPZeqK5JFcOtZisJdJSYg7RmbYYPpfpAzw== 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=RLQBkKl46ZXDARLc2F56yqEqk2Pe6CvmAAh+XAZwv+8=; b=Hm6Z0E6inNe4vWn9AsSwX0STqA2r3X3ThQkRo/n3qIRwqHURFshemMlwu5upEJObHK8DsdeA0ynWxaKPBtQAg06ahS+PHrkYGRXNS2aEU96bySxdQho7vyyVkkbR7uqAKidoGiDfDZejJi88TGemL1WgC4DGEnLkwD6jXMqXQswJBhafqNFfYDe5zo+rTLxwLZK+HGNnekXBx3UnjuyB01TAj06C1Iaq9s0SG6YItHw6v65XewRy2h1JrYvdmmLhSB/mnMdMEUOUcX8XpzLukM65zOh5Z6MB65KK6jgB+ZgCFzNtG1yl1qlWi+W0Wwl8dHq7N55/iZkkzLRAZm6E7A== 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=RLQBkKl46ZXDARLc2F56yqEqk2Pe6CvmAAh+XAZwv+8=; b=e08WORlO7ht/olTtUMR/BN+t34OgaZTgjhn7lBydUyukemmxebeqki4ge4S/mlqM6MbUPr7lcGeV5C/ch7s9bYzQ80aHiDsIbVtWReZcVbaI81pUJZ5ydt0TuBXJXXIm6pJFETe7oQpltfk5DMYtCx572zAgOvSSNhtmkYIY/3w8eLKS5e0pCee4L0FmYhcQ4+p9pHuw6N+YIhiDlOaNiqnE2FJSAPGfN7jje9v7OZLvh9psSjgUdXaTdBaUzUqxIOQagRp+brmOdujNU8h8LJa7ysNXvIwOTdqY72wQ1Lky42FyolOktJlXTsJ9ZrEiHjpSzRj0r6NLa8lsTo2gzw== 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:41 +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:40 +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 09/27] iommu/arm-smmu-v3: Allocate the CD table entry in advance Date: Fri, 26 Jan 2024 14:15:11 -0400 Message-ID: <9-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: SN7PR04CA0231.namprd04.prod.outlook.com (2603:10b6:806:127::26) 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: e00e0a14-24ae-465f-1a6a-08dc1e9ac94d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qDa65HyG8KwKpwMpch5H/17LkZ8+JxDvA1bMHmiwKk9pjDkSjfkL/A4KdJ3uXd/RNSJWtU6hpjDxR8F2+GWSMQl2GGGwD/D5ZCMf7hRT6ZIhP3dSXOg9iO86lBCC70n4Mfi8WcnyIxlRB90MZOj9JrPcUiKw448zPVvWDRbItMQmCq5cyNRONNFeD9XlLycoeeihzU2RGeo576oyJTVBaiiOp76B56EJM8O8fVXSMhHr+0A99v3yEwqqfPc2DnC5siZGhuJIWsI1JBwkTHgRociZI7ReVCJCcX4PfR1tNM+ufO/Tj2EGa7MeutIXDrFbwRRdFgEozqjKZL45+s/LEntyW9Hszh7YdsOFXvEaicft4HKuRXrvt9KZmg4WbfU3mAOMJ626g35/3PkYnV2U+a18vZbxW7XRHgOZ4TsCianzZejZWYAPaO1ewxxAfOj+GLDlFi6+/CGwKmASl8rgFBOBpGcbhRb4mICLy7LKcZeSDJiZq5bW8YA8/Q4mBkz/Pdw5PADi1vQY3OHyfYOfGDi6v5FYRZ0fEwH36BcrBZ0PFWLFsT2sgvl+Bu0iexBtbY25DpEyk+RRQWsr9dF4/yWsYez1YLTYXjOoL+Cw/M8= 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fFj3ob7TG3Q+so92MCCcruv21B3QTDM5TCLNbQkbGho+SyQ83ZPwxXkk3qAv/R4Ivj230lkVUC67NXDZZGDXzLgViHY2Vh80PJBg4V6g0IV23LG+f1GS5VUHc4vC1GpWNl+QDYR0SNs2kgL71tdu/+x4N7ZtPJ2i/Nhiyvwt2rFPiZ6xG9x7KoYw7mTCm93tkwlszacJz+4u1kMHB3Vg7gSZw5ItuOeORLauldY615bb/t2DXFhnPppnAnfoV62eM1azN+uZfdYN3pmpZVxVr3pECAIkhp8b6tTNQI+FnGjkNkmlXy2t6noNNmpTepwBmsW9RmhvmWKFFZtDCG7F8UXXmGA2rF9xb11GJp9V7uMTZ1ly8NzmKoQC1l65JMCBHAYfYjg3jCj+c2WM5OsUNoxVXRrULchc1VYaBhw+KLJMZPq/dMhgrecOZHv+Wsg0vl8HFzaKcupyUiOwn+ccbrxftZ7N8indQzJdmS/NbDoMaQJoto3eEwKfLHok/jBn1MvxHBwmUsDQkdg1AfMeC6h4zuIFRgLbb5mvOW2/2aI7r3guwhb+VsrwgT9Jkv7LEEMlScpfwalR2RbqODdTSyDReqNpyY3qS1DNgMQvnom/8UXXr3poF/r4J75OZlQ3IlBy8Ml8eXqyL54ejNBWOZSu3erz2d0qBDNgAykvd5Mp2xCu4CeI5bkOSsgmSSiKHdw0yt0xCg0pkls0QLBJIROMCoD3uAVGE9YjkHGZJ5Zpc2yFflL8S/FQrEkR9C4jYDuhFgrw2mPaFD/70CJLa/vW8IVu7wQf2zNdoxmfVrUQzR0/pVuSRgmAhodUNygjiWVFF8QEXqTXO1jU74dtcxIegsVXKjfWqTLBKw12xkdulicSWZEGD5eyGt5BgHkGBoeopBDzhN5Mt1jNfCOggQPMKmynd4yNiM/UgHAfD5L5i9mTQs69JI0U5N1/Yinla6TYGAk5nyMrMj7CSq9cXNgYkDljwqdaimmobqumAV9coBz90gCoGdDvNp8sTAS6tYoAl4daH1JjRBReg72vXuY6+VJ+aHWQYI4CfMO0PVjkvnd5SEFDRGHmRTKVVKyv7LTL7KeOiuyy9k82rfOhzd8F7GfrA5DLCXr4unGIKKMAiISXXAAI99W+/GJaLr3D2qzgG45ZdV+riyufrQbul1NW6giz6vutyLvvR/Jwx5sq+l4R2M9Y4LZ7JDdRakDzR5q8lipwDGzCknFE3gQT59x2Teqjx3pKB6SRG6vzD1tvk+bqI+lpwKJL+d9jH/eUhPmJu3GVkEH6NvwXwIBksrvY0FHxHUwH7iLvMErNsXmcS1GH8yJgD+dwXmBh5ickxk/PzW8RoRX3LGZuDt5PTAl9NbJYfW70qU9QUSFw8zVMz1Y1lRrsL2melTvJy+zWF13oJw0m3+bqIQ9aJ4wGYP+WmwZf9u24WW2RTuT75jfSWYbxkdwMsXfdgcjkvFVOUG+qyJeqHdM+yxLQ4crvUvdGuBi7UW0oTzx3fik1dmFz766dt0mRH0r9iHZmxPBPpZS5hyWUUzKpMk5smZMMJDOIAuhg4ozIpBFjD38HSkc7R1tbuZMKZuCewuY384mA X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e00e0a14-24ae-465f-1a6a-08dc1e9ac94d 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:33.1959 (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: Q83rBDFbpO4WlcknuYdw2+mtxKzs6xiPJfCZj/lcl6pbP4TLFGlMmz4EGJxNEJ1C X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101548_376911_472FC111 X-CRM114-Status: GOOD ( 16.06 ) 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. 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 7562d7577f3940..79d80de4c0a60f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2665,6 +2665,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; @@ -2693,6 +2694,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_get_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. @@ -2712,13 +2719,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_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, @@ -2735,16 +2735,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 Fri Jan 26 18:15: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: 13533253 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 90AC2C47DDB for ; Fri, 26 Jan 2024 19:20:39 +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=knHfQWTFBDc5j9IM8SPUHFrLpddIN0dRILxAVmx9yLU=; b=CzK6PoIkY6IlQf 9gDJ5w/oO47N4oqGwlkj/aPPHg0ZvHn6k4us3n0m1wqPNDGWvxI1qK1FQ80H/jrrEmJNCraKNK9SZ hVOPKFkthC+OxkAyYfGpHBdvdUWBcLAyetOtKAbP6eCRXtGGgzYh/Iyg883W+Rp7BoBWczWdv97lO z0U1pdtAkLJCI6DblwEdsLy9AF0si6vw3eGlKa9+haNqWf60cB55jS0vo+g8BSDmwzE/VZTDqHW36 9Fg2ns7h/k6DL2VRpJR7CzWqaw4gqOaH17LnWxHOmTHiQOC9IXCyQH135Rru7z9vk8AlBvebQ84g3 2zwH5Jxt/PnQ1nz90npg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkW-00000005CIt-3i1R; Fri, 26 Jan 2024 19:20:24 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkI-000000051Vt-2ZgT for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:09 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eUXGKYp36Uwqf5+uZh1zGrUQJEqqM0s7pxuNgv9VPrURisG8JIgVjSi2s4skG1o861DAVf1et6ENhIs9NJC3jJujcIr3Rpl67CAMBFJaPeUzKiDN5hl08id3WF3mWMxZQBCeRH7Kt2J8IdjgFUAdxOCndmAnKT7V/7JvbKpEPUagk+1Fcxq402QYrolyO3bWQDKxRqkeUij3uYGhGGWhFXcYix2D6aeZphNQ+TvJIaoX4RdpsU9PR9VBW0vYyOBeVhhOGsHyxnPAGEij9dQohZa8s0Z8xCKfxQFsgBGfxnwjNCjP3I+iHcuep8JXTB7gvA209gHkVLy8cPfcu0PDRw== 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=F22HzHoGbsw/9Y0xhGEpVBMyJpgrUsKt2FCyuH14i3I=; b=QZk3FsWtP7jxU/MKrBAWG2bjF7twu1kETTIcz/nNVmz5DZkPJLBI5YQQqcnKiYjzaKB8Mf1cMEYU82LgnWt3MT35iFpPwRMJMDRtdyKBc0/yMHEzaJeRapxIJM7TudpwFJy4QKId121Y+EA7XOiJIoJ0k56d06B5bgVP2fSKV1lBGopXbrWY1j+6hkZnAi4xNEd52q5AleT5zqG5Ca9xPCg8fdmEVzbhj+CMDtgemUEo1P2M5kWsbXjbIoq3oQxA7Z4SsQDbcLhk6WKdGhM0kgWhjckA1YKb2hg6SttiCb3bOkx6Jgb9pX6Pl2ml0aIxq++r/HDVvq+RPchL7u+t2Q== 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=F22HzHoGbsw/9Y0xhGEpVBMyJpgrUsKt2FCyuH14i3I=; b=TPvo2X/PBmB8KOeEMUOhGEDhtUUU4QUzEHpdgMW8p5+/SUB20c/pwzp9spyFytogYfnIS997G8c+9/nKd/vB7KCfqWwLI8JxhPmFkVSS/CWcirKxz91EN/HCvC/RB9P4XNgNCXEHW5SJScuv+qwAwrQQB+Dp0usLAZKRAXg/ItwlXjjMQ6LwLac0nuZQYyM13jywHl2yWw+tSP+fVnWbBeYEyzNNX2JhLzBSzjUJEZIv/WkHIYim1M6P92ZvrRA2S7ggtmh9H3WNWB+eZrfWROeBVuWH2otdIwWzKQeVo2hcwIY9r9El0wL7mu7oFNgJPoyR/KaYw7Ls01qFWG1CQQ== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:34 +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:34 +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 10/27] iommu/arm-smmu-v3: Move the CD generation for SVA into a function Date: Fri, 26 Jan 2024 14:15:12 -0400 Message-ID: <10-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: SA9PR11CA0030.namprd11.prod.outlook.com (2603:10b6:806:6e::35) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 9131f5f7-c5c5-445f-34c0-08dc1e9ac848 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Tf4lzaF902mYgufP6bBlkbJBSmnUPVKXqtaSVoOBIAofZCn0a+X+urQ9XPszZqfAC8QcOqhNfmj0J5jBC0+diy3IGw8K9SDM05f7MJkMMwESHdU2qTsivd/p21HSBansBYwkAtziBIB6cmXGsdCq9XbKr/iTCbJak3+ICG2Id7jp2hy0lvU58orryJoO1CRLB+MaDL2ALDUyUBohKEoa/PR+cg7Q5T8UERVn6XLUibrp+BT3epMDq+KAf3dujLf0GJ7y+k8Ba/TuPuok7jQBCV2E+Bgt4NTc7YRrMHjgyGclvhC8jQGC3Dj37PH35eaw6pjFps1pMiyZSFNEatR0atslN9AeSbFlWTOrwJguc5o1k1hUsssifgipE64QJc8vdJoO+xPngjtS2shPp7fzO1NhsFkZe/IQwJymhCYK+6F8mertVQInNpjE/bZBsIvT7069PJpmMYRLKKLW/zw4BPbtok2N683s662saAgqvITAnw393ZB4pl/1ASKmzRBiMNZBKiZ0sC6hmdK13TfexzQn5GYUgpLyoe+JKnRryX8NBPaqTEcYO7sxtro1G34UC0oBErR/WPYvXjrF7mL8Qp1mJK3R5pIfS5uUz3fqugtqE50zdnEOHP9M3/KViXoFGtgp19Bd7RK42vCZxzJ+ZQ== 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(30864003)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001)(414714003)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +jIvnHVRsJ1o/3x7esrBkE2FdFMMgUPH4PMYMT/XELqjLttLuXrCA90GKQGSG5932EGIiy/zdyod0iHQRfe3b3zHFafyLXtxDwPmBpEtcSvy25e5RkBO+Iu3hBjri+gDMhPlGbWxYgGyuwF+otgvrllJXdRlrGNya11HTZlVhP1VvCv8jfLs0Bu+4j8eiqRWJQ4OHO13oYCljXU3Haqv0wSKmmkEPfE5hdbwh1AWGxmnLNF8yrEodd6hIbm/fD1ChIX+V10heFGKTgwAgbF+RK5nHXugI1R24Wxwakv/YyEHP/kLIf9pfOyQoDTCuZjNTLWaw7jSInkEwtgvzTb5kR9pjed/61fWX/h4HSUdLNKptldhZTsXeN94EFfL5IfiLTRwjS6QDwAbYk5VFgyzivvk3/MjnaTP6ZM5IaYbzeaAm9gGrT4P2ZdPdtE/avAsXs5Jbn40ghbvhJAht+gmff/XgAgPgfQvRA+JQz8mNtqUSxwcIVORuTuobdJsBhKDdLgErUmVa+E7ZO/vAAJESY3aSJeOANYMjnv+fkT2j6fE0n4QxYz4sWgJ7QLjJsYv7SWRDeKJS8E1Wceo5mV4uWOIQjp15/7nCvRtbBOppp54X6vRRMJvTSvAZMu6P3Y1Bqawv1rdEQ+Alzo6Cbr/+YA4nuQ5xExUcpwr//4eY2Y5Sv90ASsqZ5rzOTGMrXHh9h8ZKHSYJ+bvhYqNtK9iYCnyGpCIY6po6Z+UJsGPR1eCGlq5r7r/KVppHpNTVfa/mfwEz75f+b+i1/jgKVvyaNEzndVbRvWKuFWdRS4mIFVlNiOEu327BfyzVeQDJ2zrBGpoP4qghxZY4Sj+J9WL5R53cVM3uK4QGOYZQl0HIy7dnuySro0JWo1bmYzFr2d0ozXJGICyviZSmCA0MESg/VNpysn6X6F0/SzaIksODZGUy9DjSyjIodwZwyQFFAJH1rrtHZSirFE4NAUEgmzmShgdZx8953+d2fq8JQo2sAaBTKVmqsWNbvrBxG2ctAA89E3M3CrhBWIfknWCTX1DC8Qi4NrtXOfG68CJeul8xG2t5nT/RF6Ku6mQ/Nz/KD8a2Es9aM3kVw2ubhLorkQP9iN99gyru4kCGhtXlZoWaKoLgJCPiNCBy1gt4UPGZWlkep4d3YYfPd/jjnPhLrEIbpKaRGdSZDgYVBowyQQX66N1vyjgiIgZmnbFILxaFN3qGifE9DlM+67EJ0oYbQBMaCmZV6I6XkdhSPNPzUb6nTmvFZ/r5pXmam4CHdvGJCGUD0rY4sMwR/wWHx8UWlES3LV/Mdm6DvpmQQBuRUSSwCfLakUTIZ+yz0GAZ6Ey1eaeiK0Y9Q3YzrpPfTYlMZY5z+L3JtIIjFyhHSFnmsh68/beqjUth8VZSnFPhaZ5KzjUaOYEp7+TyAY6+MVo1vWBv5S2EqgrljREMdcL+Wz0CeK2rCwoFD0crFVy4hnvLKjKJImHyHD/S9TFnU4vmWFk9xSGzhASGXynow8n+tXcM2XixPZZsnhhkDIvrNif1/zIHoEbL176XXu/nXyWi8BytAG3qJ+W6HH7vP9//+DMNoHr4DLNxKjfXvX8pwHi7EsB X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9131f5f7-c5c5-445f-34c0-08dc1e9ac848 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:31.6668 (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: BIoMHGJgICo9qEelzfaHD6eIZnIt5jjp51oHMQAy9GwwtbP81yFkg03Lk45+CmII X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101606_772966_470F4A38 X-CRM114-Status: GOOD ( 25.08 ) 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. 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. Remove no_used_check since this was the flow that could result in an used bit inconsistent CD. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 159 +++++++++++------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 95 +---------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 - 3 files changed, 107 insertions(+), 152 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 00d5f1aecc9585..7039620c4b2e55 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 @@ -35,25 +35,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) { @@ -129,11 +110,86 @@ 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->stall_enabled && + !(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; @@ -167,39 +223,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; @@ -277,6 +300,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) { @@ -288,8 +313,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); @@ -350,15 +386,22 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_for_each_entry(master, &smmu_domain->devices, domain_head) { - ret = arm_smmu_write_ctx_desc(master, mm_get_enqcmd_pasid(mm), - cd); - if (ret) { + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr; + + cdptr = arm_smmu_get_cd_ptr(master, mm_get_enqcmd_pasid(mm)); + if (!cdptr) { + ret = -ENOMEM; list_for_each_entry_from_reverse( master, &smmu_domain->devices, domain_head) arm_smmu_clear_cd(master, mm_get_enqcmd_pasid(mm)); break; } + + arm_smmu_make_sva_cd(&target, master, mm, cd->asid); + arm_smmu_write_cd_entry(master, mm_get_enqcmd_pasid(mm), cdptr, + &target); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); if (ret) 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 79d80de4c0a60f..8bd072ab0f258f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -57,7 +57,6 @@ struct arm_smmu_entry_writer { struct arm_smmu_entry_writer_ops { unsigned int num_entry_qwords; __le64 v_bit; - bool no_used_check; void (*get_used)(struct arm_smmu_entry_writer *writer, const __le64 *entry, __le64 *used); void (*sync)(struct arm_smmu_entry_writer *writer); @@ -93,12 +92,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"}, @@ -1019,8 +1012,7 @@ static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, * allowed to set a bit to 1 if the used function doesn't say it * is used. */ - if (!writer->ops->no_used_check) - WARN_ON_ONCE(target[i] & ~target_used[i]); + WARN_ON_ONCE(target[i] & ~target_used[i]); /* Bits can change because they are not currently being used */ unused_update[i] = (entry[i] & cur_used[i]) | @@ -1029,8 +1021,7 @@ static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, * Each bit indicates that a used bit in a qword needs to be * changed after unused_update is applied. */ - if ((unused_update[i] & target_used[i]) != - (target[i] & target_used[i])) + if ((unused_update[i] & target_used[i]) != target[i]) used_qword_diff |= 1 << i; } return used_qword_diff; @@ -1126,11 +1117,8 @@ static void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, * in the entry. The target was already sanity checked by * compute_qword_diff(). */ - if (writer->ops->no_used_check) - entry_set(writer, entry, target, 0, num_entry_qwords); - else - WARN_ON_ONCE(entry_set(writer, entry, target, 0, - num_entry_qwords)); + WARN_ON_ONCE( + entry_set(writer, entry, target, 0, num_entry_qwords)); } } @@ -1178,7 +1166,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)); } @@ -1250,7 +1238,6 @@ 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, .v_bit = cpu_to_le64(CTXDESC_CD_0_V), - .no_used_check = true, .num_entry_qwords = sizeof(struct arm_smmu_cd) / sizeof(u64), }; @@ -1308,75 +1295,6 @@ void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid) arm_smmu_write_cd_entry(master, ssid, cdptr, &target); } -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. - * (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; - 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_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); - - if (!cd) { /* (5) */ - val = 0; - } else 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 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; - 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); - 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; @@ -1385,7 +1303,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; @@ -1483,7 +1400,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 ec7c906098ec1f..1f52f861281eb6 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 { @@ -747,7 +745,6 @@ 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, int ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, @@ -759,8 +756,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 Fri Jan 26 18:15: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: 13533248 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 35BB0C47DDB for ; Fri, 26 Jan 2024 19:20:20 +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=RGZV0Dxjf0lUX2Sp1UEpccuS308j1srx1KOFOKW1lz8=; b=Yu7mh9TL2U8BMX cen7/pvByVNInzUksf8Xtrd0tuBGJRlhGPGccgkH8zsoRPVFnrFfmGOH8JQ8+X0y/fZhqgVCkNmuR EUkDpS6H/R9DJC4iWQV1Q2ag34uEZAEprl6fZV5P9vOnkJiKR+N6zF45dydxxocFqFoy8prOBSaJY 3twbM+MibtdjzOhOukoDGGs0dt+dqRD10ufUhV1gFwp1ljc48kXgzYkaHx3ssknHhgGUXHUQFeggf ozsNPOZ2GMUxyjP+WLeJU9Jx8/j0ukx/WshBNbpGs0KLvJfcLmrokHCXwRclKiqXMu79sHJ5dEnfl UjIrjedtImTN5ljdbXSw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkF-00000005C4X-2lQt; Fri, 26 Jan 2024 19:20:07 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkE-000000051Vt-1fh6 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LSgIxMPApVzFXG2baIOzSPljwHvXwqx7hiKzFQoXiAfHCfCsqG9OMM6oGCu6Kslct/kUFvPxyLvmLSe1cPDrYjBDq2J821iByNWCW5BgmXQIJYugiVr/d2xvy9bn8xjqBPNAfbeQixDyA1o3eet3MDf1wsjrXH8E5p61zwONy+ZXP+VAVx+xULOTOkmYpSjkTqwBP8OGO2ME9CHlyl0oZpL3OJgaXes30eiife0/ZncsPbxWso34tO9z4wZ3bUAWCMPgFWbNFYiVzbPB+DgYrlEolQ/E+zwRg46gaQRyoKLtIDzKpg8FhWVAc1wkFMYFwl9UIrPbVrfNjeyNJIP+Eg== 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=8LipFwfAeB4jWPyQ7Kt765a/uZfB9pr1mvXt1sw3p40=; b=kE718JeWLubJWWei5O0UVgTSYNXYa6rW9IaVOF7bH4R/BhTIAbab494auU/5vUxapYojfro2veEfTGGTi1gDk9E8Z1eEWdubKjS8y/2P7UEgPSfxTdqT/cFfe21Nf6wuvLuOiW4gqFcvDOTEj9/wDoxPkQMKEE52+wY8VexWmypzGKHqyxe0onYdWARBvnywTOSi+egPup8jM7cTQR/x2gaVbTDgvwraiG4jiMMXCO2nLe5+7Bx3IgYqOdYp1JQ1xGtD8mga4suOFIXnr/QqqFQAG9gaMjUAe7B9sET2c4ffKgZ+QZi1fYM0qT/7Iz1sz546FKwaNHsRVZ5zmPm+8w== 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=8LipFwfAeB4jWPyQ7Kt765a/uZfB9pr1mvXt1sw3p40=; b=BMLTx61WxV/cFe6tjFtlNg99Kj1+8E9ZQZ14tgpufuu3JBc0GUfA6x+y8VBZwTCWbbGI4xNoYPSLLGKQOOpuTa/vyVcoWCKoZAZzL7vCDZUNHbHVVKd6abNjUozdoXigaXUoA3Jiffos2mSJeRyWhRdLmihry2ejSaYSlMOb/MAvId7TegGRYyerHIIZ3dQxTXaMJj0zj79KxOT5XYQWrAFvrMsy3ZGUoZyXCB/6zWAIHPST+LQyXH+/k4qRGWabdyVz3G+uv4dO+qmUK33svaXo4xjr6a5axBxCwqGZxmoAiZKohCs1gQGFiGX5/+3A3/ecYUzG6GlGW/iq8XkmDg== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:33 +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:33 +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 11/27] iommu/arm-smmu-v3: Lift CD programming out of the SVA notifier code Date: Fri, 26 Jan 2024 14:15:13 -0400 Message-ID: <11-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: SN7PR04CA0228.namprd04.prod.outlook.com (2603:10b6:806:127::23) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 45692d0c-aa96-4cbe-b890-08dc1e9ac840 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QzLHwBpCx/oN7edZ0i4DqOuW/H5o3YDWVeoby2S0c45mtygrvsEwfSFx/AKeSZtTKqNAcMpCvdatnU+Kcnt0Q3KFdmK/ymxJ7r7KPpplTglX+Yggt8ZwIXg5wyHNdfBDH2sJZPoiRRQV7sxM4+Hqopws2matD/eE2ZZIsLQ30EE2L1wtzOQ8KcpaZwj+RfGmw/j2G4rUA+cUye9FTRLsLlcD/hESAbeH+m1o9jTf+5hbabMlUrHws7P/2mS3yCSTSoeBA2BerOGnBoErSGYTUIPwBVXY7pQO4Rtxe+FBmOh4XehxMghnUuBsGJjgA/eRwwAYaVQtMH6NXo9S1mDwrt5VI+Ty9RnVRDYqk2rTmeXi9XtebJG5gnOwRalXmcyjO0UnUZJGVhRzW2vvBBtZOL/eSFMMhI8H69Qffm0AlyiDBpUY/cIehsSmIyuS+dsJx667aakZFUHxk+//9Ahdhk3Ffe8dJuJs4xPQTptsRIOAR6Nw+3Z6ZUMVVhkXCwtClLqlVAbU57RUFOnLZUWVpCAiKCn6LCifR9z/jWcV95JNlsA6cPzrvNo96CsuLyUD 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LuL+OB5aLd1RVkd46oqZai3b1++HfIRN52lnfq40Q0xiPEzHlFGnGBxVpn8sYswIU3VjEHmWecYhnaRPOI1z7HcnnKn/P0cr6kpmeuYuSUVYffMrcbD7VvxOyVuRILoL6ySyamxKx7lvBC7uhgXYADt06jnPGUgpkrlusp/J6aWlz7MdiDKhE3mIV9z5Z3QmRGQ6cPZC3mVJ5pRoOEH288T4Z3lIbGNJNsZ0v2OIsSopur0LQfF/7e/diTuDC1huJKaB9dZJdPvctzrYRnyINeBJEtauu/zIPnxwsSwfoLXYk8NbZ4By8UJqVQMwHCAMp27gDIhNeoG/EmS0/QSdE4pixJGHfWFg5zCOdLyXlH5rBiD3o4bnCHpCmD07T/jbDrxvBIlHg28WQQhy+wEvTcqgWuJqgpGBVyhwG1NLbOFkewREIB3u8EFEF60FsUHURN0xFG3WHH5WBSmolX6ZMChsSAs1z1OJn+nWTQTDpx5V1quMX0kDqE2rYmwcsDol5xHGdqV/MBEQ4BGk76p7X7UMUFykpzi/DZsjxqxkz6/EooVrZFh9yD3F5Bz8IH7BwAwP2zmT+foAAQiMlFqriEjOptfRAdjeCulVwdwvyptcCZ4pvu02ejUC1IFgiwp1xqDOCSfh+6d0oLJHg8xp70QjtJsBrCYL+M2ieLuPPw3jCX3QCTg73YHmFi4ZgjscPLRvOdc6MfxBrKkRWQjV8Ig4pYIhLba1lbxya49hv9t//gF3d/Y+9JxH+tTbgJpq0VSlJRqHgAfLdKjjeDXe9ZosI/GEhMBn2h5o53rAaGGt0kadiMwOn0nmhA0IezzqCSkJkxf/3sLWxhBvcHOwgg1eRjFuSzhRc9l/QcfCmrOk9fikak0dXjsihBWrWyFd8TD65nEJN+0frqbYQMrsPooE92UIdiKPtPuscqx9DjFh8JAFsZRNu1YPmYItxMBnVBxi9Pb0D8STWy4eUXew6yVLP3i84p439Iu1is8w6NCLrpWGMn7GMup2h8CluTwXjKi2LCjWmrOH4z3mCY/FJsRYt7XKWaHvVs8BjfdoICumI0OTFeEwNexNUXQgQ+flNlqzGBoi63dA+NzWEsbQ0mNG1X6y/MtPZIfSdY9EuhIBVwfgm/vp/la+2Hvk4v6bYIq4p5LKmaTlpdtlJcCjUH06Z64MpLrEeuiL1ruvWTW62Gs9PF5EBiUlS50raLxrFZI8tu1tgX+5176XaHi4peK/SnNSxD3I75gy9k3HyJxHKx0qamKXSsQLGRhcUTfCnceyo/+fGVh0ZTunaD53BquExK9jHJyMF1bNefZg0lKjlzMu60hwd2fij/pUlJ5a014SYOlbh1MDDntFkf0bW1cXkNnkbfl7N+Yl4Gm+/AM4hxhRWjD5+9hwGHg5BttmOZRCKgsGQnDiHHrHRErD88aasMpj28cVOq1wACt2PZH0lW/xzFgxj7LXuYnIDg8ZkDkRMzR0POntczof0Q9kvrbZNBVrxDMZv/T9PYH8UXmU3SLAeckAaFDcNR7P4ugNB5Q5LYEJF8iiUpqUZO/8NOs3XgYWh0APOt2X/H1WwveKnDM5iE631QGaimUpt3EZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 45692d0c-aa96-4cbe-b890-08dc1e9ac840 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:31.4927 (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: jMYyhPik0GFVBOQTC1hHSfA9WLHyQEd9vEYSsz7PRalNw0iifPM97fijMG6+6X7y X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101602_637187_680F7A30 X-CRM114-Status: GOOD ( 17.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org ops->set_dev_pasid()/ops->remove_dev_pasid() should work on a single CD table entry, the one that was actually passed in to the function. The current iterating over the master's list is a hold over from the prior design where the CD table was part of the S1 domain. Lift this code up and out so that we setup the CD only once for the correct thing. The SVA code "works" under a single configuration: - The RID domain is a S1 domain - The programmed PASID is the mm->pasid - Nothing changes while SVA is running (sva_enable) Invalidation will still iterate over the S1 domain's master list. That remains OK after this change, we may do harmless extra ATS invalidations for PASIDs that don't need it. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 58 ++++++------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 29 ++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 ++ 3 files changed, 53 insertions(+), 40 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 7039620c4b2e55..973a5219c519cd 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 @@ -351,10 +351,8 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, struct mm_struct *mm) { int ret; - unsigned long flags; struct arm_smmu_ctx_desc *cd; struct arm_smmu_mmu_notifier *smmu_mn; - struct arm_smmu_master *master; list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) { if (smmu_mn->mn.mm == mm) { @@ -384,35 +382,9 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, goto err_free_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 (!cdptr) { - ret = -ENOMEM; - list_for_each_entry_from_reverse( - master, &smmu_domain->devices, domain_head) - arm_smmu_clear_cd(master, - mm_get_enqcmd_pasid(mm)); - break; - } - - arm_smmu_make_sva_cd(&target, master, mm, cd->asid); - arm_smmu_write_cd_entry(master, mm_get_enqcmd_pasid(mm), cdptr, - &target); - } - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - if (ret) - goto err_put_notifier; - list_add(&smmu_mn->list, &smmu_domain->mmu_notifiers); return smmu_mn; -err_put_notifier: - /* Frees smmu_mn */ - mmu_notifier_put(&smmu_mn->mn); err_free_cd: arm_smmu_free_shared_cd(cd); return ERR_PTR(ret); @@ -423,19 +395,12 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) struct mm_struct *mm = smmu_mn->mn.mm; struct arm_smmu_ctx_desc *cd = smmu_mn->cd; struct arm_smmu_domain *smmu_domain = smmu_mn->domain; - struct arm_smmu_master *master; - unsigned long flags; if (!refcount_dec_and_test(&smmu_mn->refs)) return; list_del(&smmu_mn->list); - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) - arm_smmu_clear_cd(master, mm_get_enqcmd_pasid(mm)); - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - /* * If we went through clear(), we've already invalidated, and no * new TLB entry can have been formed. @@ -451,7 +416,8 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) arm_smmu_free_shared_cd(cd); } -static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) +static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, + struct arm_smmu_cd *target) { int ret; struct arm_smmu_bond *bond; @@ -481,6 +447,7 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) } list_add(&bond->list, &master->bonds); + arm_smmu_make_sva_cd(target, master, mm, bond->smmu_mn->cd->asid); return 0; err_free_bond: @@ -643,6 +610,8 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct arm_smmu_bond *bond = NULL, *t; struct arm_smmu_master *master = dev_iommu_priv_get(dev); + arm_smmu_remove_pasid(master, to_smmu_domain(domain), id); + mutex_lock(&sva_lock); list_for_each_entry(t, &master->bonds, list) { if (t->mm == mm) { @@ -662,17 +631,26 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id) { + struct arm_smmu_master *master = dev_iommu_priv_get(dev); int ret = 0; struct mm_struct *mm = domain->mm; + struct arm_smmu_cd target; if (mm_get_enqcmd_pasid(mm) != id) return -EINVAL; - mutex_lock(&sva_lock); - ret = __arm_smmu_sva_bind(dev, mm); - mutex_unlock(&sva_lock); + if (!arm_smmu_get_cd_ptr(master, id)) + return -ENOMEM; - return ret; + mutex_lock(&sva_lock); + ret = __arm_smmu_sva_bind(dev, mm, &target); + mutex_unlock(&sva_lock); + if (ret) + return ret; + + /* This cannot fail since we preallocated the cdptr */ + arm_smmu_set_pasid(master, to_smmu_domain(domain), id, &target); + return 0; } static void arm_smmu_sva_domain_free(struct iommu_domain *domain) 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 8bd072ab0f258f..712795232fcfa9 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2656,6 +2656,35 @@ 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) +{ + if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) + return false; + return to_smmu_domain(domain)->stage == ARM_SMMU_DOMAIN_S1; +} + +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 arm_smmu_cd *cdptr; + + if (!arm_smmu_is_s1_domain(iommu_get_domain_for_dev(master->dev))) + return -ENODEV; + + cdptr = arm_smmu_get_cd_ptr(master, pasid); + if (!cdptr) + return -ENOMEM; + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); + return 0; +} + +void arm_smmu_remove_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid) +{ + arm_smmu_clear_cd(master, pasid); +} + static int arm_smmu_attach_dev_ste(struct device *dev, struct arm_smmu_ste *ste) { 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 1f52f861281eb6..bc6fa08cc788a9 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -756,6 +756,12 @@ 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_set_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid, + const struct arm_smmu_cd *cd); +void arm_smmu_remove_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid); + 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 Fri Jan 26 18:15: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: 13533245 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 EBC9DC47DDF for ; Fri, 26 Jan 2024 19:20:16 +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=jARXDuQe2GSpvMcw/rMbJtOfeIVU/pKCcPPykUfcs/0=; b=0Xd4NZerBR301L g+Tjkuse3CRWUcboi30Ryn3u2SY8S36ZPflth7qPLfY1e2jBEDILlqnju52ewwuyHl06AIni1Fh+G y5lzATOMwYYCgC8ovJzi+En6OekEPLhHVlauvoGABcZXaJm6jguoOXkpHXh4K4n+gBK72MGoElU24 uaSqzCpxU0yT9ytSbNkLAIc+2CRyczvlGx106Anb5RVJKO9Fx8ys93Loo6FR5RMU5UwCL9gPr25Qg HeeLDTkC3KpfII6zOYTPDyVLnLeoTQ2VY5vsuedcIJIw7YPxns1xwTdrWWHhSBgUnlA/8XEA5WuQx n1wELSQjsY6qPCz9lQlw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkC-00000005C2R-3EZt; Fri, 26 Jan 2024 19:20:04 +0000 Received: from mail-bn7nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2009::601] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkD-000000051UQ-0iWW for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hgzZ/sIzDw5lGwzkJSk9EvqAFg42+/fI29thxtI3+emJqu1p8ezhazZsblobkyZRqIWLiTzTY7uXWz+J7Iw9eMnB9ZObU726Xpo91YcTbdTPmzco8JecVlP6G4fr5gwpx/I0oBO0BWXrBeImuuBKKszx4j3rv94yN0s5cr9UroH7fTJkYmQ0prL1FdU4otWBU2HH43Sp3DeOr7cGMliYS+xKGz/cvduQp0RrfOji3dQY2VIpWwHhKvS0k+LdDHzgGpslr+hw6i6pMWmT6OsdadiuwP9xE+QL1aRmK57h6MZiju0hmGPzjB4rDxa1ig2Nc7rtV4pMHJncrLTWUruqIg== 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=E1XnujJJDhRFthHtzz9WxxCn2XFq2wqVuwj2WMBztP4=; b=LDP1wp1LtroMvxebEKXb0yIW1+jNqVte3fi52kYEWMUfBb39d3wiG8nNy/4XS1fh7b+fzxdL3HNC2dYXJlEGxMvOmyOagM/5zQhSTNTcrXEQN3L+uuW7WDr5m7VeAVaTJGtQu+LCsAlEocu+j98bOETfzjIOFZ/oL8TCLHVjpYS/18NLzj8j6mFmPYCMyW2mCGf32msEYruomMEjk1Fk4f1enYNLD3r6w5wQqsQIh+VGsZysIP535KXgbxlivlFcgkBY75eHeCGQ2X29JeDPjEsG7M4OzFTRattBIleWJR2uQC26DG1+netDuR+8GcOerkar+dv8MB7evYrdaJvoqw== 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=E1XnujJJDhRFthHtzz9WxxCn2XFq2wqVuwj2WMBztP4=; b=oy4xjMw9oEiiI6+5SoD5s4JLo5Q5wsp4wSBFyn/wFhqi0L3DUup7/85vT6wP5dSyEqmVJ2zKnbl80wwztwiUu6fT+GWLHCO1N6BWI/vbP4WpabdYLxA1CF9nC0V1BRhuMjoOOOnr63FSZQSsNQqLwKDvOzLXmrOzwaJp+DL1ZWJlVVe9iicgvdYvHXM6biYmlKc0II/O1TCthfIJ+zWlmuYdIk9o8F19hp0lilCYJEfjLl6W82pzROkYvfzuFaAjsw9OShdgytecr8RmMmhlDj0zBmbXVM13y1/okq+gRkV915NKf4gecp4cdF9gwwOoWfDrDi7oOH29vLp7GDT68w== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:39 +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:39 +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 12/27] iommu/arm-smmu-v3: Build the whole CD in arm_smmu_make_s1_cd() Date: Fri, 26 Jan 2024 14:15:14 -0400 Message-ID: <12-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: SA9PR13CA0148.namprd13.prod.outlook.com (2603:10b6:806:27::33) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: 995dcfe4-ac8e-4bc0-6bdc-08dc1e9ac936 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Lx/Jyum+aEum4b/6x5YFsZ70P8dKfAGShWDlrhX5lRx4MpYQKDRenS2s5Wow22iXDTEWST4Uh4xODHpnCGE3q5xIhji1z/bwJM1C0PUqZ/UG+fp0pQa4oL/jn3cbYo2godQHwglnjms7IpLJ59pbyoAw5yH9ENIkqOPovnjqjl3DxD3RziCNr8pA/LyKhXx9mHqZenSGW6iwFr3jTUHiFdhNE703ov0VSc3cee6MMOrwHB4t+oreoy9IwlT+DRnDVkfzOkOCozrmQ7t9ps4+PGgtlpVeCRCeGeFJDa7RdB+xIqfdoXdBR5lxHf0c0xS8NrNE5jG7D1CAgQl8C7cMvNf67+Rbc2zeXpMAINBbhmiPPSEA5h24LtU+6HFtf3L64BGFz7VoTIoOa2S+csq3acn8t92VCbNrYlMQZ0nNVbLnmykRlzDIisrLyLH7zbc+nkrbZ7YvUDru17EUJHg34Yh/L8RWdFrFSa8e0p1r4+WJZ0rUEOaAflC4oJ/rPaxePgIzf8tnC6s4SR7CQp6PwsC51f468W5eK44gZpchtVI+RjUPuxqWYbnecx6aEtEn 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4FM/3QES1cYJL3unC+8H4czUDeuAAw673ob8ySGnNAguo75IEbAleGIMRHmS6QpXcmRySiqj5brzyPSVC6jaUf8cai3Q7tJYu/qBcHTgSOX7ezc4/iQOmvIQBdl9j1TyefBZXF7RBEn4x+HphF5y4UXslNhgwgGwK7/e3gb9VArEbSX2Bs1j0f9smn/SYGcdCgzhisCntMYF+wx8jO4xJi/OhAsYjxftSmGFD2kwg/UH1QQ3KK/GZQzqPgXmqKgdHi3D+7GkM+4Zs5zzwwjBRkh566zY/NDoUwI0zUwWpZHOuyejpZBa8oCPYyKKgmsRhVz0wEEz5TRrTzp/Ye/khfJzhZvG4LkKreh38zLv6r6yKUXcVjuQ5QM8DEKHuCjBuwoZh95B7DMjm4RMKriS8u8IlT6QBLRn3ZMLf7go+Ku4uI11X/uo+TurCCWG5B1XYtJIrkrmsvg+iQM7rFhW9mn/hJF4g40YC2Sr51ud0mufrI9h4OnYNgmrKdiPxsOBhJiD1OvkuGBySBam4BZAgGnfKKAELOQQkK+SHYnN94/wNTsvsLvcqxZRi5dlWHS6UthzHDCnbpXE8chIGar2QDB0Fh7IWbizNrf0Qxe8KZK4dPZqqxA/3gh9Z5aBLPvrzDpVEMvbLXPnqbDds0Bpcr8sdsIaw54oPH3Z4aaNxikMLiH0DnNtv+HSBs2zVN6lcZslZELCbsqfjsHUhpzfOglP89jodcZVQybuwifwPkS0T+yc0bZnLPUsz9SLWYPOrBMXTdBX83hUUNEK2F3X9O/QnvlTL52X7GZGdinNsY65XfrkRDjfCJ19y610bk3u7oOhJjlytT451kBQamTzudCwzZ93ZwW/hBVCybDyZE38rpRe1R3BCPHYdNlPWWZkYo9v9iULfMEwsQ5dlst68H9uOnyh8mh8lR39C0KMtB55Vsyxj1uOG9++dZ1fvZmGH+39UAsGn6dORCxQSW43UYISCaJhF0U6V/naW+zydcxvOF892irw74RaxDy/T3Wag71vft1R4b+9zqUvXHbP5xiBzXRQrJJf5G2mIsB6yNiWV/7vYid8Njs6F6iqJnjS0TtkDGkAhkM8g5BqnT+QBrXVDUqOSwqJaO8+6oRLo1JG6mw2fujRQg7jw8J3LJHOry4lby4lBoq0ar0Hgpv6foKuxHZzzGBxTDx74M7jV8ZvOxoSb2qpGw6hr0FygbimYbKGIR8c3eAqxgM7vD9uTSngdTrI8gkWPcRbsfSPoyXriqetHcU02ERg6/gBrDQu5hNCr1UBQL89eljo8hSTJDyUBMBs9IPu2aaAI5rJOiRs7BYjE+lTkFcuJe4FJag+3OFwQ/gNfJYkyo3qGqFqSe4T+ZOkQoNzhTgPBAxlEPAiYDRh9RFr18aOaz2bqj5tymXaHPPxYmfAhy7yu9Vlhzt1t+M2MBNwPDO4EJwpGy3szfeCn7PihAg9/kyXCIVD0Muj0jw+Lyl3VDeiYopJWsR5bl+GH1rffkBqAaNCSui2NHEDYL4HFGesqPqh3fVFt5fqjp02/KLV5kDUj2zH+JNwNP2nQSpzDMGcDpCrbSz6bg7faA1+LccKouhzteTk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 995dcfe4-ac8e-4bc0-6bdc-08dc1e9ac936 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:33.0117 (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: GR4TifDwWFNC8WUrpmRaUA8e75DgZOGSFE4eKkU6CxHM/pld3e/SREW9N3M7PRqc X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101601_356416_0553B90C X-CRM114-Status: GOOD ( 13.80 ) 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. 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 712795232fcfa9..7e338664d7fea1 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1261,15 +1261,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) | + CTXDESC_CD_0_TCR_EPD1 | #ifdef __BIG_ENDIAN CTXDESC_CD_0_ENDI | #endif 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 | @@ -1277,9 +1287,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, int ssid) @@ -2294,13 +2304,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); @@ -2308,31 +2316,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; @@ -2356,8 +2346,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)) @@ -2400,7 +2389,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 bc6fa08cc788a9..a3ea078bba92ff 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 Fri Jan 26 18:15:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533115 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 BC2B3C47DDB for ; Fri, 26 Jan 2024 18:16:49 +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=lcbkUTYWzJBEa+XeOwV7MQzERICZQtM8MGYfI/jAARQ=; b=yqpkpHEGSt5y40 nldRnMyPP7+EcSBAkQGVlxXC5s0oYaEz2qu7uRUyoMgVe/B66l5we0uIQvScGCAeCLNPYjnx6/d8r 61PDc8Yb/Ho93OZfV+5HgTO6d2ggJGktoeKNTcwBDtO+1VFElNS8wDnP3wyF8Lxo7fFaxvHMjeRwl Tx6ouYqXTQEhM9cvTRrjvC95v6pPmDwETq92iQcg/nzFfN8QIQowiQDw+LKqQ4GEhTDa0o6MGn91p kZ6AP4EYTjQv1ccFr1QAAX+CYNQStQ7Be6rVz4onFCwpGdFIkX7HvKu8Lj+CTj+MtP7qfvOUSf47V 4UmdXpfKWnEZiS1oKAHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkn-000000052BO-2qDT; Fri, 26 Jan 2024 18:16:37 +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 1rTQk6-000000051ef-1woK for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UEM59A2WL3Zi8tFrpkYYA8ZJumrPbbu0TvjF/baHazt2UUYC2aYtfVbXeE8wIxH02kf3cTVa/V9s5Y0yEdStR0gSjjbUMFwfLIdqE8W4JUAvNtcei5AQDcGIoRdkgM/uE6gFn4h0e1J6w+3bcPg8ledzXidzMNrsB75L1U+7tmShH4gv+XwEmEG1pNGwmNLQ4XqB5aLv8lKwH/Wu5Ya4ODYcPybiujjz8qqa/B9mxCkACOlqoiJHqUIbuiD9FsEKnHL1Lz3QZHpdqjHp+ZqlzbuGrUTZaWSPCuHRqFTyvSCx+Ihn9PHADeY6BgAQjr7S6ak76xrIE3+SFP2M1sDdIA== 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=ZG8Z7xsZtWncmKTxyKkhc0a9fod1+LGP7jbslFJirno=; b=O24xPZwHul8N4I5i2y+hOAANs5AdwMCaXGXZVM1XpBj5qaJb2GYdvKKT0HrVpKN004Ue5Mq86wcvLm2C26WPRL5VUADqjxdm5yR9iBxmvV2JKadfY4p0SeTL36RTsdZ/5e3RmjmzijSBPME7Obcb5LL+9TgIUhRmHmoZ/e3c0BVh5gxC3LbXKeGz44tDVYRzxq+AMRIKdAqBKiXtReOtBHHId5asl2ZTbBP8Yy0QTZ3QX8YrDdQXvWFHaW9Ge3XmpSmy7Lod7HBDFWtVZ0o5hlfCA2gcIXP748bEJO0E/Lb4rHrhUEuE8ThUexPEAdbFUbZ/bYsjJNn2FeVE1e80zw== 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=ZG8Z7xsZtWncmKTxyKkhc0a9fod1+LGP7jbslFJirno=; b=XmSHzDMyy6lNg7AEs+4EcK2ARnFqV4MQz1yw78OGJtxLDZS6DYTgfo0Bzd+ghIU4WKpVBwcmzT7QreWsUwDltUXb4laThEFPyY7EPUZIP4S2IXPiB6ar9RNT5NIQSPszgE5kMNUozRaS9EGkExu+BXEuBb06Pv7qgc+VwmXIAPgDUk182JRRIYGAJjVgckLsGqClKAz26hhsR/75WTeVcyzN2W+qUswR2uG8Op5geRyvE8VOYwmekoU5ZgQJE5G4fH8xt9KVHiem1aTV6GWByL3XCyvl6+of8qQiv/73UjkmanAF11erOJe5tESeaVfY9uQ8zOLQT3FUa6ogRq8bvQ== 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:41 +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:41 +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 13/27] iommu/arm-smmu-v3: Make smmu_domain->devices into an allocated list Date: Fri, 26 Jan 2024 14:15:15 -0400 Message-ID: <13-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: SN7PR18CA0029.namprd18.prod.outlook.com (2603:10b6:806:f3::28) 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: b3f3c3e5-66c3-4557-6c5d-08dc1e9ac945 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: v+jsNjIQT6Aet2D/OorKR/m3IYYv7vpM4D+WDQ/cISHWr2OtsTK/dkbPF4jM4cKfIm7bPLMSvaCiE3mWrxyC/+1sRICL/oCKWPBzeVUaCFpN9HMUPT6V5kMzqi0vebzxFvEKOy9ZqvqTngqUdNG8IsOBuL/poDhKUIPuekttbG9SLu5QrPn4EZhbHuzAlKBcFlsLFOZXPM73OScKvsQ2up6J+qhKHx3RFFOxUjzX1FRKucxBK1yjDqBm28ZS9MQPKGMD8qlMcfhwnupcBr2zuW3IdGKeq/OHUzsrdVanIihyI1E6RLR33msmsWzPFHzk8sobSfrCc87HquuloO3Q9AnT7+ECcVEaNc86wYd7JEuIpES4PZGE4xArxouK6aPhB7pa6mA/Xp5r/WT+kbQlwjabgyd1SNPQItaxdasywDCl7VMMHlsW+GNdnZDuSAjKSXfZFe3yoL0aCk7NG4/IkMIE/6zPpCGWl1LA82TRuVxIshakraTN/6u/eFtLE6FPzPg6pobfg+eXuQr/w+x4sN/5omElLz7bgk1CLemkRYncykuBkT0enbmcpGdsWW4b 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xTdnKPKxKZ4gGlVj8V+KZll9VmEKVGpx03b+rE4x2jv3payxtFFtKY9rA9G/97VYCxsSgxwRsmaIuTcZbew8fbDzrMYOWAeKs8BK5Kgg0vGezpOrdb4yDNyU71hFfBpLdBnTq69KahlkrPb+L6F32NXE0/Pz92aXpGQ0bhnDqfu2bszqAizZq7cKE2+EFukFgjp6JpdBUBXLZbjlGV0CnT4uoPdk3CBTyPvXsv5IYhHSAEIxE7b5birmmzVTmyD1Oq1LnxDlGDutO7WwOLOj30gBkYgEgVWmgi6YC9KHX2yWVUhZ1cbfHL3lb5Pk6TbkI/DfuBNF6t1ty/LMngZ/56SrLlFOGMD+jnzm8qi+UC40XhnacHPJflPZh10o7wMOFTjIaL8oZj7xWxgPvcJj6uTx9IiDJTThl5vdq8/fISV1JzdBIeBoeJ2CYI5of7tCSCp3+hJmcZFq4r2qFoBv+DTMi1myDiDeS+aNd+p+oYXN+3E7VU41XBKa8c50AgOgfSk0M+kPkdg7IkvxLDQK4B/UzKdV5bGrcPqa88EfIwHO3u1htRsbuSVWkegahAK6Z1skLr0RUnBwUbHDEPY282JG8BnFSDR7YN9zXDIc7lcTYk6VamfP6tf2M6hfrloNgRMVMP2qdO8jO9ZgREm5rrUZreDFbJIuOTwW480akNLBQTiikIpxMCWRawI93cZA7OL254vVj7nMEfqrP0Tul8M5u33APj5fxEEtkP+YGRvIxUfhiu5u6oJxCL6oVUauAqTx115EU+0CqfKOT/yGUoChVWKEpkUDcokxT0aU1TTtD4Pg0/qmjqmSNPE0UoSFJVCl7U1dbXes8HDFWLgl058J+T9CQixGGUAy5Q86K4MgOu2+LiPh81ix1BKcc3Qu17bu9OL51cyVtMgYoD6HU3dpQQCwtrisU7FfWbIVaKfJNZnemM/Eqs0mesa7ApHaEhaJz5s8WQurq+KNpua/v4tSv0Yvj4UeILd/FGpriebViDvIvyYpet1Smw0aWO1PJmEupUV9DQUbQD5y7a99+OMR6NDlOc9ysWtOImJah/qyh5HzHgip5GCI2I8y6hfWaPp0N0liTVfR3T5IbXmHjlOeSgrO+QGLX0CO5tadekHmbw4s4DhE3TjzG99LlJmv4JxzTcS6/b8qn2weIEYLSPbdrCdIqai15WbVjaTMV1RmWneyUVt0yiGmh4WgEb6C4C1/+0lszAtvmze2qX2gIxIMgGrqAw9Wpa5Gp+/6eKukr1MI9lx4176GGlXzR1epMILj+5cNUsGCDafkNsBYDk1lg8NnpmaU1qu7qaQGc3PJ43ldppjMeRBkgMTbFjfF72aMGDCX5Y2MCEomxR2BUUpEF8KCxfREvsNpSl/eu1vZkcJvwSzhzdRB2cmpkILU2OUx/UtdIK0HvsxPmibGQpQz11iiw/gWJ0qZfCKhJsAAMv0UeRC+byLzKD2WnuYnBZRSx/weOHzcXJs+UpEIWbSPw8vmsUuQJO+jkEB/kyFfM1OH8BkxEtN6An1TD2DVPymmDL5gGK+w0tKTliFqLhSUpxigMkIHjtkepWsY0G/vdyjgdd+WbnNgYJJJaX8c X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b3f3c3e5-66c3-4557-6c5d-08dc1e9ac945 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:33.2273 (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: F7YaMH6IlDro0rC72kAbTOr7FO7QhzbRZbBE6JyA62aoNp6uPDASZaN2W7tZUA5e X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101554_719044_9DE3D03D X-CRM114-Status: GOOD ( 16.55 ) 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 The next patch will need to store the same master twice (with different SSIDs), so allocate memory for each list element. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 11 ++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 39 ++++++++++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 +++- 3 files changed, 47 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 973a5219c519cd..5c50254be4a373 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 @@ -38,12 +38,13 @@ static DEFINE_MUTEX(sva_lock); static void arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) { - struct arm_smmu_master *master; + struct arm_smmu_master_domain *master_domain; 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) { + list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { + struct arm_smmu_master *master = master_domain->master; struct arm_smmu_cd *cdptr; /* S1 domains only support RID attachment right now */ @@ -300,7 +301,7 @@ 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; + struct arm_smmu_master_domain *master_domain; unsigned long flags; mutex_lock(&sva_lock); @@ -314,7 +315,9 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) * but disable translation. */ spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { + list_for_each_entry(master_domain, &smmu_domain->devices, + devices_elm) { + struct arm_smmu_master *master = master_domain->master; struct arm_smmu_cd target; struct arm_smmu_cd *cdptr; 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 7e338664d7fea1..fcacf30a4698d7 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2020,10 +2020,10 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master) int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, unsigned long iova, size_t size) { + struct arm_smmu_master_domain *master_domain; int i; unsigned long flags; struct arm_smmu_cmdq_ent cmd; - struct arm_smmu_master *master; struct arm_smmu_cmdq_batch cmds; if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS)) @@ -2051,7 +2051,10 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, cmds.num = 0; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { + list_for_each_entry(master_domain, &smmu_domain->devices, + devices_elm) { + struct arm_smmu_master *master = master_domain->master; + if (!master->ats_enabled) continue; @@ -2543,9 +2546,26 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) pci_disable_pasid(pdev); } +static struct arm_smmu_master_domain * +arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, + struct arm_smmu_master *master) +{ + struct arm_smmu_master_domain *master_domain; + + lockdep_assert_held(&smmu_domain->devices_lock); + + list_for_each_entry(master_domain, &smmu_domain->devices, + devices_elm) { + if (master_domain->master == master) + return master_domain; + } + return NULL; +} + static void arm_smmu_detach_dev(struct arm_smmu_master *master) { struct iommu_domain *domain = iommu_get_domain_for_dev(master->dev); + struct arm_smmu_master_domain *master_domain; struct arm_smmu_domain *smmu_domain; unsigned long flags; @@ -2556,7 +2576,11 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master) arm_smmu_disable_ats(master, smmu_domain); spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_del_init(&master->domain_head); + master_domain = arm_smmu_find_master_domain(smmu_domain, master); + if (master_domain) { + list_del(&master_domain->devices_elm); + kfree(master_domain); + } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); master->ats_enabled = false; @@ -2570,6 +2594,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_master_domain *master_domain; struct arm_smmu_master *master; struct arm_smmu_cd *cdptr; @@ -2606,6 +2631,11 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return -ENOMEM; } + master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); + if (!master_domain) + return -ENOMEM; + master_domain->master = master; + /* * Prevent arm_smmu_share_asid() from trying to change the ASID * of either the old or new domain while we are working on it. @@ -2619,7 +2649,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) master->ats_enabled = arm_smmu_ats_supported(master); spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_add(&master->domain_head, &smmu_domain->devices); + list_add(&master_domain->devices_elm, &smmu_domain->devices); spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); switch (smmu_domain->stage) { @@ -2938,7 +2968,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->dev = dev; master->smmu = smmu; INIT_LIST_HEAD(&master->bonds); - INIT_LIST_HEAD(&master->domain_head); dev_iommu_priv_set(dev, master); ret = arm_smmu_insert_master(smmu, master); 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 a3ea078bba92ff..742f05df5e8b86 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -695,7 +695,6 @@ struct arm_smmu_stream { struct arm_smmu_master { struct arm_smmu_device *smmu; struct device *dev; - struct list_head domain_head; struct arm_smmu_stream *streams; /* Locked by the iommu core using the group mutex */ struct arm_smmu_ctx_desc_cfg cd_table; @@ -729,12 +728,18 @@ struct arm_smmu_domain { struct iommu_domain domain; + /* List of struct arm_smmu_master_domain */ struct list_head devices; spinlock_t devices_lock; struct list_head mmu_notifiers; }; +struct arm_smmu_master_domain { + struct list_head devices_elm; + struct arm_smmu_master *master; +}; + static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) { return container_of(dom, struct arm_smmu_domain, domain); From patchwork Fri Jan 26 18:15:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533111 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 5A290C47DDB for ; Fri, 26 Jan 2024 18:16:35 +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=I+Qak8Yvip23nM+CJJ8s89h36LXhiuliDOoTzfa6d4Q=; b=cdPIGbMgJ3TZJY jCiGRYgjImX3w+Z2j77HUNuJidMp+ypaIqbRZJxjn/BkUyzyc7mL6gi8a6J89tz1IqvpdAa2B422i KElfBRzDS4kDhR++j/TOPVj8VVQT2VGh7g0yh4xM9FOMdqxgKMnN3xi3oo5LLQ+HLBvn0XC3DM0x5 ZrATYU8JqTGx7PqFcgBIP5vTYsAFHuJjiFDxHmAphLnEkDPmpjyXdfgpPgWW/ugdrOrNrEK+wEBwn pEEgFwLY9UpyBrAsGUNRJWIxohO+Twc8IIDb00E7fqjvkJ4W2/9fGns64yk8ufw6tpt5SksW3TeVI eJ3xxyJWdV/cP7RZeanA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkV-000000051vn-2nHl; Fri, 26 Jan 2024 18:16:19 +0000 Received: from mail-bn7nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2009::601] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk2-000000051UQ-12oS for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SmgZo2FVEjTPFXxpK4MoDLy/PzRfmyuJLFj+VYpVVaecbnRCcnC0t5D/kqxGDQCDrCg+VPpoenU/qkC5bC1BNu9ArX6ExETrqbHwgJdOCWhEP8SfBP/w6oN1bkYNyfPRVpvk3tPSKgcvVHbzGqdIn9bCImOTchZ0GfzcRhP9MOU6rOPphTHNUHsqx/dZvhfnqsrJSBWpPQKRsMWZ7t+f6p3jo8WXmiFJwDE7LYCRouC0FCFIvOVi9EvuhO2z0JJkGCqsYsor4p5URpWjjJOkQDin9ofwUOsgMRHFIuNksSwfg3K5iJ2Bf+CYPL1wInXDcLQBCE1J0PxGYcUq3UInxg== 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=TAV1s2s4/J+XqdCH/nYbzEbFrx1DSlWHZmhbv0SZMB0=; b=KeFVdRlgZ1xBtsCJ+DjI9extSIuwLmdxXn8kXHUiNbn9ks3tS/Oq8d6Nxy3P0/GPFNF2++TPlHkP/s3xg+be17Xl3fByt2RmMNm4WJ4gjWPc1xNFsI78sySZwHsXHMoURherkg9Xgaw6Oi2XiESlNE7KcwTujNjUEYGHtSz4Vkgf6BVgWzNBZuO6jZAVd+iPuNR1dMrt9za5ewwJwxOLzOAYKmUbcDmAIPW3oXcLd4W25FUobG0uStNmxC12IYy+9jJHnFpNStW/cr3+o65G3hUOiUgLzdcklx6XDX07t53Ru8QSA8fXJtEhss+m7N4/Hcm2yKo7CND2L9yu+OhZUg== 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=TAV1s2s4/J+XqdCH/nYbzEbFrx1DSlWHZmhbv0SZMB0=; b=McxkKI+a45XQtReVMO0bj1zZHHgRO4tkyVmgzS2zit/cLpYcAY3FIG1t5uvpd4FEiXbAB5/WZgl2knLv5BCjUfeMHog68xYPaG+L/sOXewP9CBwDyIWb34+NDZz92ZmvdRkOWBsaNWKwOa4gRWWnc97q4vnvcL46MRGn0YsT313eALT0Al++sJfxT6xJKu6oi05g9jmS8o37ci9056NtwLG813m4FSzzA6euux6YshUncBZrAL7snQ7itkaHjzTkEq/ZTS7fyuoOFTJF3YUcA2SoUDhQ2/vq6pA5Tus2RBKEY6FdQ9VXEOKqQ8selUO9v+pAwjplCtReIrIWwIpzEQ== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:36 +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:36 +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 14/27] iommu/arm-smmu-v3: Make changing domains be hitless for ATS Date: Fri, 26 Jan 2024 14:15:16 -0400 Message-ID: <14-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: SA9PR11CA0017.namprd11.prod.outlook.com (2603:10b6:806:6e::22) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: b380d35a-1643-44f0-8ed3-08dc1e9ac8fd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HDVm/VgyE/1ki9SiCc7YLPq8ENouHsZ9j/XjQ1eznL7d0ztSk3e9SN7EzYghGH3ogUnVtsVPLyirdr17CgkCSrQjV/AD+z+AIbpaZ8JMHSa5JnIuvtyQIqRJ7rApNRf5ZOWtU8gNSN3B13PG924ClWmmsrCkls5pm39Y11ht/JfjpUQ4R20HDm581XgtoSesN2WxpF2ffOb2zmX4SDqJCLQ+L96KVyjyk1XyQgalSqCw87sQJrLa5unfv7u5xvsd9G4XkZytpEIOeyyjvFc4mNN6Y34O/c2A48irfqh7LGW0ydP27TVJmvtoNPSF0+gfKTbSIj5EQAnWfOd7KAmSsMvjy8yZ/RnWk2BMwc1oeTZW6X5HgUCTpquny/HgqlZbMPcHMK3gJqtAF6dam0AZYe0yS90w8J7pM7YAPfm4Hfrt2DkxvI2q2VNaydQPYA3MdFSUzDJOSCQQVeiPfyuT5+uF9JEbOgQPXa99LCM5ZUNGvJhzLyDwy7bI0gA52nr3SbwE5sQWlG8y6tl/Hm6nNjgbw9rWC2o/Vey/vfcQKWhgiPaSiVis9oop+yae6QTL 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(30864003)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PbEQrvVAUgi3+uy1FEvu4GBeaZ+aJ4639w9KQkZcssFrG7Iy82ubUu2oIPOQwGzJ1nHK7eHs+hm2ueewPJepfR+dgPSasqLtPsmeO6REU52YwqQla9hVkFyk2evxEwHLJHp5/pT1CaIlqsRv2zpEacy9VqEyh4GOf3wqeqwhlhzmXfhGeLMjPLwOojuHBk9/sejonrps7bboyXEZugeVY2axWRA8fKtXXki7z2a3yZRMYI56M+CZBfroN7OTud8GFvfyUf+3i/zfTCsv1ZcuIbDc0a9B9xq0M0sdpqCfskw3iwqUmWESvmYp3jVnvmnxNTZZFaJF/2+FM7u2AdYnAJ+RmZ+X24c3NqrFb94U1gKr+XcYPiexQyqRgI8Uw0qwp1kxKaJUmspbCgmN23gM4rkdizM/X/szRcqXvwWEUOkUILRIKyIKVxyV2/hVtDAkBQHmliSwnHZnl/V3U85LmNcpbRFlWFnJP9lKNn8XcmyyLNvs7qH8ntle8Ony7z+ZodYzifpF1UkbvkiFJ2V0XiQ7MyjILGulYLvw/AuI/uZHs4ftPCRzirsRWhOCxjfSPnjpq1DuJcHfWDJgON6aykWfoglILn0tUXTJWmbFyJ1NsgeG1/Fj/6iV2ZxY273W7Od/8qmqEbDPRB2QUVcF6hs1JyNXgBN6Q3Dm8jy9NTiPxBdw0H/58LhqEM1Ukb1N/BnDjsojL3FSohFh86jV58TNwR/7fZg5LOtbZC4FOXzvQgi+t5QfTk0oMKejPZzlAaB6JvVchozF41aXcfbI9AxxxRm5Xp/Fo6UopdI7WlFE/wB9Bata/19axWAg+YFH9xeXYuL2mhadcQ07cYmW1oPo8KInrw0YwZSBI756SmKExhMcFBviEk/SiTSlbFMCjrt//iAzHBr92jICu0G3Hun0Ix1bPVHC9IGz6uiyupUvOpJttjKCGv4dT74ngIybavZaTgOhq6nLzhf0dDM60MDp1v/8JSTwP19hVyCs6CSF6OvDnlBT4fWQMCUmuBSw+RB1Jps1bTKOiCVeyqhlAFcEyLLuVMq6I923iVvW+fbNiQ4ru154Ag3dON/i/grPb3kRg7+AryHQQosy1W9Pfo9TlJ8kvaFf55w5DL3QIwKfXc97sbMwwdqv558BKn/ax+QM6vQ+3WtOp/Qw5kOecAwQE3K16KMkCTUP/pois4zRkL7GJLrm+C3yydwxtDRYdrK5SYEz1r7VrqahFY8bpdcwEb5FS4wJUQcfGDg0dPwFTrc5Ko6Cl800/uSul/K0IhZmBW8h/oW+4WZ4jP0BYHfDLuBxUYv9abfZ5oC2h74VVnTQNPNaKwsH55IuHuMh3zhXnq4PiIb6Udidq+jO47eY3dGYYibpjBKsZEWKbrEM3gRA/unMsWcOmkEXFGu13CKsdqO9zYXoFbd7/fJ0Nkn7z/Rm9ch8qQQrc98cLnfchIgUzJTMX2vdTBuL+ziaUQCCbOTYnmsWU2H7ON+IwH2cjStytmflGoG1CNeVwVScLQle0O1zi2LD4aN89PZ1o1DlDxIiTnzJW+dvSnOw98sRAn9NuOarDmgfQ10xLpLG0Qrq3mPmZhPNdKwfmBBE X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b380d35a-1643-44f0-8ed3-08dc1e9ac8fd 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:32.7236 (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: NVfVbLWsdN5IvcFRFLXEoLND3IAOTQJnAll/POiUnR6tEaeEbd4h+UoUQ6HTAlop X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101550_499561_1761384E X-CRM114-Status: GOOD ( 29.48 ) 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 The core code allows the domain to be changed on the fly without a forced stop in BLOCKED/IDENTITY. In this flow the driver should just continually maintain the ATS with no change while the STE is updated. ATS relies on a linked list smmu_domain->devices to keep track of which masters have the domain programmed, but this list is also used by arm_smmu_share_asid(), unrelated to ats. Create two new functions to encapsulate this combined logic: arm_smmu_attach_prepare() arm_smmu_attach_commit() The two functions can sequence both enabling ATS and disabling across the STE store. Have every update of the STE use this sequence. Installing a S1/S2 domain always enables the ATS if the PCIe device supports it. The enable flow is now ordered differently to allow it to be hitless: 1) Add the master to the new smmu_domain->devices list 2) Program the STE 3) Enable ATS at PCIe 4) Remove the master from the old smmu_domain This flow ensures that invalidations to either domain will generate an ATC invalidation to the device while the STE is being switched. Thus we don't need to turn off the ATS anymore for correctness. The disable flow is the reverse: 1) Disable ATS at PCIe 2) Program the STE 3) Invalidate the ATC 4) Remove the master from the old smmu_domain Move the nr_ats_masters adjustments to be close to the list manipulations. It is a count of the number of ATS enabled masters currently in the list. This is stricly before and after the STE/CD are revised, and done under the list's spin_lock. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 199 ++++++++++++++------ 1 file changed, 141 insertions(+), 58 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 fcacf30a4698d7..b50e56cc3d9fe2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1542,7 +1542,8 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_ste *target) static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, struct arm_smmu_master *master, - struct arm_smmu_ctx_desc_cfg *cd_table) + struct arm_smmu_ctx_desc_cfg *cd_table, + bool ats_enabled) { struct arm_smmu_device *smmu = master->smmu; @@ -1564,7 +1565,7 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, STRTAB_STE_1_S1STALLD : 0) | FIELD_PREP(STRTAB_STE_1_EATS, - master->ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0) | + ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0) | FIELD_PREP(STRTAB_STE_1_STRW, (smmu->features & ARM_SMMU_FEAT_E2H) ? STRTAB_STE_1_STRW_EL2 : @@ -1573,7 +1574,8 @@ 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) + struct arm_smmu_domain *smmu_domain, + bool ats_enabled) { struct arm_smmu_s2_cfg *s2_cfg = &smmu_domain->s2_cfg; const struct io_pgtable_cfg *pgtbl_cfg = @@ -1589,7 +1591,7 @@ static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, target->data[1] = cpu_to_le64( FIELD_PREP(STRTAB_STE_1_EATS, - master->ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0)); + ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0)); vtcr_val = FIELD_PREP(STRTAB_STE_2_VTCR_S2T0SZ, vtcr->tsz) | FIELD_PREP(STRTAB_STE_2_VTCR_S2SL0, vtcr->sl) | @@ -2459,22 +2461,16 @@ static bool arm_smmu_ats_supported(struct arm_smmu_master *master) return dev_is_pci(dev) && pci_ats_supported(to_pci_dev(dev)); } -static void arm_smmu_enable_ats(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) +static void arm_smmu_enable_ats(struct arm_smmu_master *master) { size_t stu; struct pci_dev *pdev; struct arm_smmu_device *smmu = master->smmu; - /* Don't enable ATS at the endpoint if it's not enabled in the STE */ - if (!master->ats_enabled) - return; - /* Smallest Translation Unit: log2 of the smallest supported granule */ stu = __ffs(smmu->pgsize_bitmap); pdev = to_pci_dev(master->dev); - atomic_inc(&smmu_domain->nr_ats_masters); /* * ATC invalidation of PASID 0 causes the entire ATC to be flushed. */ @@ -2483,22 +2479,6 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master, dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); } -static void arm_smmu_disable_ats(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) -{ - if (!master->ats_enabled) - return; - - pci_disable_ats(to_pci_dev(master->dev)); - /* - * Ensure ATS is disabled at the endpoint before we issue the - * ATC invalidation via the SMMU. - */ - wmb(); - arm_smmu_atc_inv_master(master); - atomic_dec(&smmu_domain->nr_ats_masters); -} - static int arm_smmu_enable_pasid(struct arm_smmu_master *master) { int ret; @@ -2562,39 +2542,145 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, return NULL; } -static void arm_smmu_detach_dev(struct arm_smmu_master *master) +/* + * If the domain uses the smmu_domain->devices list return the arm_smmu_domain + * structure, otherwise NULL. These domains track attached devices so they can + * issue invalidations. + */ +static struct arm_smmu_domain * +to_smmu_domain_devices(struct iommu_domain *domain) { - struct iommu_domain *domain = iommu_get_domain_for_dev(master->dev); + /* The domain can be NULL only when processing the first attach */ + if (!domain) + return NULL; + if (domain->type & __IOMMU_DOMAIN_PAGING) + return to_smmu_domain(domain); + return NULL; +} + +static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, + struct iommu_domain *domain) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(domain); struct arm_smmu_master_domain *master_domain; - struct arm_smmu_domain *smmu_domain; unsigned long flags; - if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) + if (!smmu_domain) return; - smmu_domain = to_smmu_domain(domain); - arm_smmu_disable_ats(master, smmu_domain); - spin_lock_irqsave(&smmu_domain->devices_lock, flags); master_domain = arm_smmu_find_master_domain(smmu_domain, master); if (master_domain) { list_del(&master_domain->devices_elm); kfree(master_domain); + if (master->ats_enabled) + atomic_dec(&smmu_domain->nr_ats_masters); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); +} - master->ats_enabled = false; +struct attach_state { + bool want_ats; + bool disable_ats; +}; + +/* + * Prepare to attach a domain to a master. If disable_ats is not set this will + * turn on ATS if supported. smmu_domain can be NULL if the domain being + * attached does not have a page table and does not require invalidation + * tracking. + */ +static int arm_smmu_attach_prepare(struct arm_smmu_master *master, + struct iommu_domain *domain, + struct attach_state *state) +{ + struct arm_smmu_domain *smmu_domain = + to_smmu_domain_devices(domain); + struct arm_smmu_master_domain *master_domain; + unsigned long flags; + + /* + * arm_smmu_share_asid() must not see two domains pointing to the same + * arm_smmu_master_domain contents otherwise it could randomly write one + * or the other to the CD. + */ + lockdep_assert_held(&arm_smmu_asid_lock); + + state->want_ats = !state->disable_ats && arm_smmu_ats_supported(master); + + if (smmu_domain) { + master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); + if (!master_domain) + return -ENOMEM; + master_domain->master = master; + + /* + * During prepare we want the current smmu_domain and new + * smmu_domain to be in the devices list before we change any + * HW. This ensures that both domains will send ATS + * invalidations to the master until we are done. + * + * It is tempting to make this list only track masters that are + * using ATS, but arm_smmu_share_asid() also uses this to change + * the ASID of a domain, unrelated to ATS. + * + * Notice if we are re-attaching the same domain then the list + * will have two identical entries and commit will remove only + * one of them. + */ + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + if (state->want_ats) + atomic_inc(&smmu_domain->nr_ats_masters); + list_add(&master_domain->devices_elm, &smmu_domain->devices); + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + } + + if (!state->want_ats && master->ats_enabled) { + pci_disable_ats(to_pci_dev(master->dev)); + /* + * This is probably overkill, but the config write for disabling + * ATS should complete before the STE is configured to generate + * UR to avoid AER noise. + */ + wmb(); + } + return 0; +} + +/* + * Commit is done after the STE/CD are configured with the EATS setting. It + * completes synchronizing the PCI device's ATC and finishes manipulating the + * smmu_domain->devices list. + */ +static void arm_smmu_attach_commit(struct arm_smmu_master *master, + struct attach_state *state) +{ + lockdep_assert_held(&arm_smmu_asid_lock); + + if (state->want_ats && !master->ats_enabled) { + arm_smmu_enable_ats(master); + } else if (master->ats_enabled) { + /* + * The translation has changed, flush the ATC. At this point the + * SMMU is translating for the new domain and both the old&new + * domain will issue invalidations. + */ + arm_smmu_atc_inv_master(master); + } + master->ats_enabled = state->want_ats; + + arm_smmu_remove_master_domain(master, + iommu_get_domain_for_dev(master->dev)); } static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) { int ret = 0; - unsigned long flags; struct arm_smmu_ste target; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - struct arm_smmu_master_domain *master_domain; + struct attach_state state = {}; struct arm_smmu_master *master; struct arm_smmu_cd *cdptr; @@ -2631,11 +2717,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return -ENOMEM; } - master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); - if (!master_domain) - return -ENOMEM; - master_domain->master = master; - /* * Prevent arm_smmu_share_asid() from trying to change the ASID * of either the old or new domain while we are working on it. @@ -2644,13 +2725,11 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) */ mutex_lock(&arm_smmu_asid_lock); - arm_smmu_detach_dev(master); - - master->ats_enabled = arm_smmu_ats_supported(master); - - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_add(&master_domain->devices_elm, &smmu_domain->devices); - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + ret = arm_smmu_attach_prepare(master, domain, &state); + if (ret) { + mutex_unlock(&arm_smmu_asid_lock); + return ret; + } switch (smmu_domain->stage) { case ARM_SMMU_DOMAIN_S1: { @@ -2659,18 +2738,20 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) 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, &master->cd_table); + arm_smmu_make_cdtable_ste(&target, master, &master->cd_table, + state.want_ats); 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_make_s2_domain_ste(&target, master, smmu_domain, + state.want_ats); arm_smmu_install_ste_for_dev(master, &target); arm_smmu_clear_cd(master, IOMMU_NO_PASID); break; } - arm_smmu_enable_ats(master, smmu_domain); + arm_smmu_attach_commit(master, &state); mutex_unlock(&arm_smmu_asid_lock); return 0; } @@ -2704,10 +2785,11 @@ void arm_smmu_remove_pasid(struct arm_smmu_master *master, arm_smmu_clear_cd(master, pasid); } -static int arm_smmu_attach_dev_ste(struct device *dev, - struct arm_smmu_ste *ste) +static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, + struct device *dev, struct arm_smmu_ste *ste) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct attach_state state = {}; if (arm_smmu_master_sva_enabled(master)) return -EBUSY; @@ -2725,9 +2807,10 @@ static int arm_smmu_attach_dev_ste(struct device *dev, * the stream (STE.EATS == 0b00), causing F_BAD_ATS_TREQ and * F_TRANSL_FORBIDDEN events (IHI0070Ea 5.2 Stream Table Entry). */ - arm_smmu_detach_dev(master); - + state.disable_ats = true; + arm_smmu_attach_prepare(master, domain, &state); arm_smmu_install_ste_for_dev(master, ste); + arm_smmu_attach_commit(master, &state); mutex_unlock(&arm_smmu_asid_lock); /* @@ -2745,7 +2828,7 @@ static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, struct arm_smmu_ste ste; arm_smmu_make_bypass_ste(&ste); - return arm_smmu_attach_dev_ste(dev, &ste); + return arm_smmu_attach_dev_ste(domain, dev, &ste); } static const struct iommu_domain_ops arm_smmu_identity_ops = { @@ -2763,7 +2846,7 @@ static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, struct arm_smmu_ste ste; arm_smmu_make_abort_ste(&ste); - return arm_smmu_attach_dev_ste(dev, &ste); + return arm_smmu_attach_dev_ste(domain, dev, &ste); } static const struct iommu_domain_ops arm_smmu_blocked_ops = { From patchwork Fri Jan 26 18:15:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533251 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 10307C47DDB for ; Fri, 26 Jan 2024 19:20:28 +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=ECoPy2osRgtV6oQI0a9FfwDbHei8bHyh3IhxS3ndsXo=; b=qIMOMZmQKQgNB1 AtjtYeiv6toa4vstnbtK/d3hKrKlIt4GObMBGWiTT1nfW6svOWyzz4hw4+HEoMztXx6nzLWneqlN2 QyHsnsiBdKp4BN+7nvFZDq91GOYsfoCUwUC9kxVTpYOjRoVK56r+xXsi85T9xImnN1S39JU+e4BtD 8LyqoL3sTQ1q5iPAKrskh9gvjKantqM3T7q8EMhDHcB6Zn049EMfSlDw+GdEoyzbdY3T6jrroUBsA nDpAQ8xVeXAotw14VTcjCWfupXtLm06jS7YnehtdfsofGilupkcqa2u+CcCq+aH0BpA0NoaBWIS4r 8KcWHCcnUzP/LaoD03zQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkL-00000005C8Y-0Yu5; Fri, 26 Jan 2024 19:20:13 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkI-000000051Ud-1n6n 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=SdqvONfJYYEgvIrHEjaLgen6bI563OlNF9U6mVHT3RSTmb2vZUvFFm1UHnTXcwcAwN0c1nytbEKARQweBdmo4zq2fzdQ3DYxldmll8p5/qaiyAPm/DdK6E4fNAJgtm0IHMNlkUuzkYB09kExmth/YEHfH7l1BF7np1QyLTFRwP4hmHWyJxC7hyFkpSs/0vvIelkBxb2ocp5rH0rlX4jIW0KZBnMZZ4no2/8uGEPKJ5jepFRtL7aw8Ne1Q9ZlsrLeCwALhn7J5BWkSXdC1l+n0qRvZ8K+uMljAH851aTHGJUp6NtGJQeau8+MhyvJe/z4cCwOxbRkdXq9K40sDYPE9A== 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=fXjA1CW8fRYb6lttpaMymfcynk4BrEasRmagUKmReFI=; b=lcDZ/iiU94Qggdv7ZmOiK9A4zb3Tdl/LKRJ60YrX8kpW9Iphj4iuADAs6MtDe5RDGo/zZwpO1305ow0AefvuDlof5Nb3YZ3gfVMAROl5kQ/f3peUKt9wvfLqSe0A/oqToPbA/m21Hng5WincQXK4pqyEilTD8uuKB4pdYqUya6CZ407dzGeYZxS0NA6s1PPMn6VSkyPjKDeu01nmdyUSnWu/4Z1Iuan65CoQRxPHSNyKmWEG/0YJagrGoi7Cg0lTeb0UQLI9L8XAWLRPyea3Z9LlNDLTc1IvZqdeM2SA6Tx3xIlX3skDNxhMe7uq8g0ZTwmA9gVe/U45POJRUd5Ztw== 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=fXjA1CW8fRYb6lttpaMymfcynk4BrEasRmagUKmReFI=; b=rCIpHcT5wUD2rkUWcvYxHKYLDu99xfm3j4Qtz1NlBAcCT6YwuUqO30ZxH13wcTzCJgVVttJ8ocm0xR2vceWkNalJBiN09Oy1mW2nmwz9EnZOlERK7I3VDGO9563WkVqft95BQWj0YD0eFXbAbHiQDHuOBh+/cw2UanZsL8qMbacxVDzF+cmNzcHcBTzVG2y4TkrbXElzYkSX+k4DOmOFi+O/RkkRMXz8WzfkMvksEftaZBe1XNXZYIeIJPQCfeT4Um/UdGghnhtIlgfkoCJkFwC5SaVQ8RQgBCTukpmhoZKGtKf3Ct2Dv97QKZh1AP6E9yMoAxdpvyva9gGNrwG11Q== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:34 +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:34 +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 15/27] iommu/arm-smmu-v3: Add ssid to struct arm_smmu_master_domain Date: Fri, 26 Jan 2024 14:15:17 -0400 Message-ID: <15-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: SN4PR0501CA0106.namprd05.prod.outlook.com (2603:10b6:803:42::23) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 06557c16-e1a3-4bd3-aa70-08dc1e9ac85c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A4ZvEnWQ7xOe0ARI0eOe5bRhkliRlH29dM90799T3MZLHCbnN1yAixkFpqpSZfNE06/zYsoVPwoWgWVl4TK+ZffuEJ/0wVbo94fv+/W543DmdAbYEOp7y9l6jNZnKHDmNusa6Gfa3HdfhiJon3vJ87LB7AxbIzL8RWsyA9iMybwj779rghPHDxTfrdXCbrOvauNt6TNMpGdb5BYuQLJipRjQN045NN6i7aw77xn2NGxCSRbn9GLBe2DwdvuNBC2nWvQpKtZnonr6PLTYowuJBXS+AMmXUgDJUeBUs/jFGrqj88Uhsekane56iPJrIl9k6iEddq1KABbqj5IAy/pna82snmb7iHnsixoEjb5O0Yt2Ll6A1h/ovy5MQ8yoEGfqtkKbzwrNMbM5bhmjC+4lT08M4v+LbP7urRSWtimF6+XL5YayxMdr58HtgEFogx5o8Q/5CT83BGdYMaFrq0iLeUKklUAeunTyKZL4iFnvtcoIPTq3oHSvYBk7NxNeXL2wEvnor5+/lolLNT6MQvMTfyk8D/6kRkBNYAETXDO9f2Gntvcghy9F6TmiETIiNGd/ 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VCDd03VzZXeWj5GarLoxpqHrqIi/fz0NgLE/X1LcNKnr4NxmWBvU8TOS/StZerHYZu7J1iKufGf6Ja4U/gDYL5n6U8UWwqeh1S9//SGnyisGN6CB6771brzuh5vvEGI/qsQlOY0q5fMh5PVHAaXbgBTPtoIMzltICvwprwv17leIQow2B8JNMV+KuUBZACSndILlsMKUR09nvEM70QJ4+QmdUZ32eYR5Z5KSGB5xPr3xQaQO6twzWGxnj/uVroBsN5ux32ltFxCnCz1z2LirwFUShORavba1jlTEi3lxBAOGYBOy5lwiSFPLfTqiWfYSvhVHCK6eONr/fHM+F4a4YdCHq6SmKMpxFGSvv16eUt8Yp45s+xrIJfX/dzJeH82pm6dGqXF2iV141lZ2qPXo12o6gRyOHW1d65Lp5Fl4h8Jn5G6UthK96WJEUtEyi9Pjg/Wl/yUuKbUFrNgKEgsM98GU/j/IrPvPTHvkI9b4rbJwIdRZ/Kq1t1uqtk38KeYjAq8WZFsBrbAWOaSfSowXkED2h/TiC3joOsuer4Fk/97dRIicfKyg8tJY3KHF+LZCNopFLcz3NaMuYJaIDcEPTZLd7g23VdD0ZH2c67tGtF0aZK6qU0VBXxhqz4ZAKbtaHz1hvBnnzhR4G0QxvaQ32EKVifNLic2eIfvk38zgojCf4USP7inNsPvDXVpA0lxO9tUCDkGiPH9NBwh4203pDKkuH7MmluHV8iWNsdKKwLKnk53KAQZ3ZupJBrhqxCaJY0lrjSBH4bEpNGQg7+0oQSuydHsSIG/4CskO+QOqEcpWXJdvaVBPZwMh142sTccRaZEp/4mnB7bHsUxr6OS1v8lKDKaey9G7BuDIpzwnhv4KgsJgfV3VjLFwAVr83cEYlOfwncxwj6S/J2XldL0gBX0jPclvipMgCUwsqI84jbZ+vIqRc/E7QHm93BVaabCO7cCc7mbgtSr4pXT7yBYjTH+C999lP+Z79xvhD00rkBfj8g82s8WpnBFlBIGBvBin9fGJX/FCwnKG8p2XYsClPvXkEfhr5G6r2t9g81M9lBG/QrlfsnkAc7taW/EBDq/8XFucTL13M5DMGbUWYYGNmnJjYsIqCtc4NOF4NoU9edjjGpix0BQFiCXceW/Ol7Dv5xZu4hA9FtldkXAgqnnh/W8RUgMexXqSucSw3WKS92HQLJxE8jSWxs+jDsknaRxReCnCQSDd2QRavIDdq5mzN2oaoS0+E/Vi9WMIeKvhlIee/lNOZ+ZaxA/JSPn5uUknowVlHlmzvnhHJXTcWclPITRjDd2qcCodSZpK2+9goI4cq3XhIVLqJdL/dFg2CpIvfvpzStL8Oy9kcZ2f/MSGNNr2T98X/8wAh0JxMfm9vMs70pQq2+q5HBO5TyC5zPCBRK5ih6dvRGuZzMh6yPJ9q3eRK5cpa7cudkThg3qR3azQIW0wR95KhfdNnTKNi9e1iXo9hIyBgQuCezG/V2yol/WYJIe+rb6HXfCmvrkemyHLjPNP9CysA06EXxM7iGCUdXM1PWyB5GdtmygyfBZeIYeIkvLZlryNUeI17O/hEa8KJkCPK3rp/4BbZbwS4nh+ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 06557c16-e1a3-4bd3-aa70-08dc1e9ac85c 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:31.7121 (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: d+Bc3GLLb/RvEp62iho3gPhjaqEl7aBAPKcxx4jrDebV2LuRYno7L8ANf4tcSIJV X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101606_587882_5B739CA6 X-CRM114-Status: GOOD ( 16.91 ) 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 allow a S1 domain to be attached to a PASID as well. Keep track of the SSID the domain is using on each master in the arm_smmu_master_domain. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 15 ++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 42 +++++++++++++++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 ++- 3 files changed, 43 insertions(+), 19 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 5c50254be4a373..cd0a8c721d9fba 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 @@ -47,13 +47,12 @@ arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) struct arm_smmu_master *master = master_domain->master; struct arm_smmu_cd *cdptr; - /* S1 domains only support RID attachment right now */ - cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid); 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, + arm_smmu_write_cd_entry(master, master_domain->ssid, cdptr, &target_cd); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); @@ -293,8 +292,8 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, smmu_domain); } - arm_smmu_atc_inv_domain(smmu_domain, mm_get_enqcmd_pasid(mm), start, - size); + arm_smmu_atc_inv_domain_sva(smmu_domain, mm_get_enqcmd_pasid(mm), start, + size); } static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) @@ -331,7 +330,7 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) 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); + arm_smmu_atc_inv_domain_sva(smmu_domain, mm_get_enqcmd_pasid(mm), 0, 0); smmu_mn->cleared = true; mutex_unlock(&sva_lock); @@ -410,8 +409,8 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) */ if (!smmu_mn->cleared) { arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid); - arm_smmu_atc_inv_domain(smmu_domain, mm_get_enqcmd_pasid(mm), 0, - 0); + arm_smmu_atc_inv_domain_sva(smmu_domain, + mm_get_enqcmd_pasid(mm), 0, 0); } /* Frees smmu_mn */ 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 b50e56cc3d9fe2..53b2d2b35a440b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2019,8 +2019,8 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master) return arm_smmu_cmdq_batch_submit(master->smmu, &cmds); } -int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, - unsigned long iova, size_t size) +static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + ioasid_t ssid, unsigned long iova, size_t size) { struct arm_smmu_master_domain *master_domain; int i; @@ -2048,8 +2048,6 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, if (!atomic_read(&smmu_domain->nr_ats_masters)) return 0; - arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd); - cmds.num = 0; spin_lock_irqsave(&smmu_domain->devices_lock, flags); @@ -2060,6 +2058,16 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, if (!master->ats_enabled) continue; + /* + * Non-zero ssid means SVA is co-opting the S1 domain to issue + * invalidations for SVA PASIDs. + */ + if (ssid != IOMMU_NO_PASID) + arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd); + else + arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, + &cmd); + for (i = 0; i < master->num_streams; i++) { cmd.atc.sid = master->streams[i].id; arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd); @@ -2070,6 +2078,19 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds); } +static int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size) +{ + return __arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, + size); +} + +int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, + ioasid_t ssid, unsigned long iova, size_t size) +{ + return __arm_smmu_atc_inv_domain(smmu_domain, ssid, iova, size); +} + /* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context(void *cookie) { @@ -2091,7 +2112,7 @@ static void arm_smmu_tlb_inv_context(void *cookie) cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); } - arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, 0, 0); + arm_smmu_atc_inv_domain(smmu_domain, 0, 0); } static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, @@ -2189,7 +2210,7 @@ static void arm_smmu_tlb_inv_range_domain(unsigned long iova, size_t size, * Unfortunately, this can't be leaf-only since we may have * zapped an entire table. */ - arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, size); + arm_smmu_atc_inv_domain(smmu_domain, iova, size); } void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, @@ -2528,7 +2549,8 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) static struct arm_smmu_master_domain * arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, - struct arm_smmu_master *master) + struct arm_smmu_master *master, + ioasid_t ssid) { struct arm_smmu_master_domain *master_domain; @@ -2536,7 +2558,8 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { - if (master_domain->master == master) + if (master_domain->master == master && + master_domain->ssid == ssid) return master_domain; } return NULL; @@ -2569,7 +2592,8 @@ static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, return; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - master_domain = arm_smmu_find_master_domain(smmu_domain, master); + master_domain = arm_smmu_find_master_domain(smmu_domain, master, + IOMMU_NO_PASID); if (master_domain) { list_del(&master_domain->devices_elm); kfree(master_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 742f05df5e8b86..174e9f080066cc 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -738,6 +738,7 @@ struct arm_smmu_domain { struct arm_smmu_master_domain { struct list_head devices_elm; struct arm_smmu_master *master; + u16 ssid; }; static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) @@ -769,8 +770,8 @@ void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, size_t granule, bool leaf, struct arm_smmu_domain *smmu_domain); bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd); -int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, - unsigned long iova, size_t size); +int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, + ioasid_t ssid, unsigned long iova, size_t size); #ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported(struct arm_smmu_device *smmu); From patchwork Fri Jan 26 18:15:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533106 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 2A4CEC47422 for ; Fri, 26 Jan 2024 18:16: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=yVRcu2ZjMMbWcVZvIwAaXLtDghcu6Hcx+xb0lebRdJc=; b=nYmdGPQMxv4srb Z/b75kau0AtcZ2hJFC1QJ0/XcDxwr82GJ/7izCn5xxiCyyOATrXQ/tB6ylYNqLWujMqhhHN/l/MMm HkrQUdyELcLpq1UyYrRtbMv+Uzd5ccHApsMAdtw1z/d+6wWfh/s+PZ109aU6ams3+7Fgm/sP1YedG 8fDDOeQCUyPJxh5j3YOJf1p2DuNNHd9d+mTQoaRS/GqLI2/zoCY9HPPuNwXRW+w8wqqAn+vVGI7Dq dfwfsSBv//V/iKAxN2CJKtumq/MYBTveK2yv4tcY5JId6DksAH0kwupEmfKecXkASBtq2SgvusAcJ 1bd83TcO91esp533o8zQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkR-000000051tO-46Zx; Fri, 26 Jan 2024 18:16:16 +0000 Received: from mail-bn7nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2009::601] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQjz-000000051UQ-2JBw for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QbcSx4a+IniydqltHPZwf7skJRfVtt3MwtYY2Ii6sJnvRoCdCHsUQ3zH7dnRZRXc6UAd8Nang/AaqSxv4IWfYvGHjNx3xuhZ3+JoB2pr1X5cTXlJwz0zQ9JDSFHS7Py0EVlsppO9r/EZ8S0LxsLQoE5Kn9azIJlokPewKVo7kCZuJplFXhGBpQPe+iGOJpxbswfc29OkfAPLC62eQlqli1YG8HBNLb3blqOaLoaCFNpCdoCYENFWz3B5GzSWf/TSfBuZGYH+xPmHOPqQdLj0z0lIdEZofW0TxMNozm5Wrc2nhh9MMeTbAc7i9X0gdFvbBc4khsDFYHpOH4Cc8DTiEQ== 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=Wrm2JbyZjdm8aedWVNAJCmnFDn6ouiS3bj+AajKavEs=; b=HzyQLDb2lX0900PLZ21ENxjFZ72tAS8zd/Ljx3vD0g5ClFdCJqXE8WBf+hAt+/0zVrivOf3uXUXMx9BPLUPe07o9Fn1e6+UqnncrqbKoTLDRYbha3Lke/+PsfUDHlcWxtDtA00zNYMG/X2QZqlEC3lRlcuRVRzJgQm67GLACZTbdGB5bbqQw9yc1ayCi2JxI/Vx/UPmX1CiX41VRcm3blE7PCkO+IRrjeQupSCJl9Rr2QBwURp3RUZ+OKkGvCBntWhmL4U3HYVXm2gf5koGWn2Doq0FxI3sik9GvEdQtwkTTFZf2nn5FXKGo1elBljQgKF3o4z9p0JNe8Yf5Mlv4pw== 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=Wrm2JbyZjdm8aedWVNAJCmnFDn6ouiS3bj+AajKavEs=; b=hFfZqh6WwqwgBoL/vt4Gb9USxOo1pb1yFnltXsuT8B9zVBqXu3VXMJPyaCnWMzs75JqzFzasz/HBT7FwYYizuZI2R5Cr4Y51AhXF6yauWV57nK4BjVSQGmHZ4tCh5KsXW2G6xc6kIIha3b+j1F2VYWvKpD1EWXyowWrrsIsYXOAiFUoZ5Gw5Au2UDZnMmy2C2+MDonKc5ZPEO889FG5+j74Og11owl1bjtDMvG4pMc1wkWMClHD2BXT5iSHQgA5wtu4e04r3XzcBfqtfcAPcNnIzHypDQsXaYnzwayl3ACz0CzKagty5uEctGeVKo8r4X3pQa28HhusQOUv2Ivpsew== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:37 +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:37 +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 16/27] iommu/arm-smmu-v3: Keep track of valid CD entries in the cd_table Date: Fri, 26 Jan 2024 14:15:18 -0400 Message-ID: <16-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: SN7PR04CA0229.namprd04.prod.outlook.com (2603:10b6:806:127::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: a27fed98-b192-46d9-9f52-08dc1e9ac90d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SgiQXfLYZ1Zn5hj9aZtPzN0ypsFEsTgqyN/2LPxaYeL+DUP8Cg/oAw8WCatAlwhYZHq5sEsqjySB3lIcE3eWNmA960uipjHvrpMVx5gHaAHoNnl+LNMtTkTX8iAPT1jj4Hs+yE7RV7BCbofyLymm2WjnI8B+XTV5UEZcK6sLG/0GBm8iOHCNLdUbzqp1ILxl2m9ayVdgnlSMbjb2m9l/0IO7BTHPnm1TbriC2Oj4XsqiGXI/KitwpxZKhKkmiLpEUAG4Oz8UdQjg74SKznWk267Kx3NqJ0ptLQW3IxZ12MV0z3ZvvjN4WQh1+AwMUOBlUfhs5P8f6wc2zpgyPCMRa/b4uQNRoXxHgnV1t2jf/wAwP1LhIyMg30cJ3B+C+i+u6AWPfDry1+qaJKNOJ7dgW9CHGyUftI0fwqKWLnrmP3e267WzZXQj1SM1oc/UH0Q1iFvSm7974Rlm7792s4HV9keWumsIY+tHN0T4+KLrek50YONIWiRaPP5Fi4VLH/Uq1bUI4imzIoBFjV36+VRUWHxB1BMpVKv56ItzGwgR4FT931ZtShTz41gsFAfr/kyBpo1QiTUjQeTysqN+8xjQQ71zivGXSfOmQ9jaunUY3fH3VNAtgO12wFPLR95VQUZCtgHEurZuvV+1ovPDdh8K/g== 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rhRtxSn6SYUSQ4Ow+WkojAPUbvqBAMucOpUFFcb6yUG5Ef3oNspDN8z3fNhxAhXJEe1HpU5sGkOPPlfyQOpk5nFw8EU1Gd6QKCMrddKu/jjdSmikTq0z1fGH7CYgjPwuD0GK1VgscpFFltAKFtiXu0xVEuM62iLVfNa9ibkzdXwPvb9txSsDkkdpU7vKpYCT8iTP5q82sxvCNf0F7bsfETYg04lkGha4lJcG2FfF8k719LzHlbRqZ5xmIndTetO7gYNmHfqjl9gxuJNBfx9HQUXBDeRHhNLWk/t3iR5jgmKa9hc7iAlJyIf6ArdGg4eF7SfWDVv+B4VEfpmL8W5okgg9F+/pCQsyAPg67XPE4/gGjv7Q/ueQoOkpc3K0nbB6ZCrV+nC+yf/lXvfqpjR0y5oiBjjmz5tqyTVVBuuSuuFmQb9EojFdAEk4l195MW7b2d/qPLmgdS/HsmgCAOGYZUAhFg8dggKYYGeWzFwL/qL/uvddfjVPGzh1eKvDEtmSKdSSiGd/6cbQQFGJR2PpgbJ10uqilOZzipbKq+9ZG6Gjn1+IHw8wQMjydA/ZG81BOcIDjLb4qxU6lzZXeKUKR+fUAYoPT83jCPHgDUwqjk+UuovzkX+5GS45tjFtdmni3HsVDUqkA7lSQ06nBbuDtPz6hhb3FktOTTfoGLLapGakwbwqhveHdh23ldfNFTD5yVzEe+fuEumBa5k7ZarT77rVUS1RfkZ83c7p5HNImsr3xBR95yr6LFpDGNPe5rqQWCJ3hZ/mcdqOpcdBCDMjIvRcrJXCmFRt9BqPRYAcUoO5O1NZV7dUhSsn6dri5EG4ctjzmhynQT4C9O9G4vNJNzb7keRJsH4XiwqbGQ7kFPQCzH8PlgxObvDO6s2mGVPJ0CJPKKp3T+6McWKqwBCJeEbrl6NvKIaYvgf6ObBmmsjUFfmguFqYC7+IC05GaB5dMoygm1visKzjoNm+DhlvuYDpA1UxK544hxpGGugbs8my8Cegyuke/d+vSvw0OfcKBg0DCEwoe5OeZi6TsbxV9tSj4GHGoQe6uVLX+8U7wKtjoUIJUk2G4K3J9DP6s+hRUp4qsgBxwVkYJPHeG3YnuNAElJMWyv+050reZFsdeCcK+kHMTtFar0vAOomg7O184ChsX/drJMszFIwZZKJ4+KIgnuebvWRMYJYHE+zVwjBe4NRA+ooP+JaEqlUywwM5HXJnm2TEEF86IjN2QKTf5Ccuo6kdhiieL/77lRA/6ID10r/78Wr7AIUIqnfq1c++X7gxXn2K8g90a4TxiGwsTiZ17jzBC5lusIc5Xm1jHZvrKC/2A7QTb+zSL/KgB5QaxkzNM/O5wz4tYW4bTK7JYMV4vxJrwjNs2FK7AzCSeUpkopn3KG44+zRfb6P6fqjbWp8s5PRC2OiUIwx4MCmw7T1gjNGx31UmKttuan6cK7RMjQ/0PF7fH4ZJ/31Tb9tf+hRjrKmMci9czvbRR0ovYDvd5DRWGMfbc2MBiXBA7nfMk644LkyunUA5VzREHfTYuMvrpvgaW7zGHxi8cs2T2TMNxr+qbImKDU0WtMovjPUlVuAofSBRQaP4PsHTTUvT X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a27fed98-b192-46d9-9f52-08dc1e9ac90d 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:32.7768 (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: on/gjXXAKK2nVrySuYA6QXXBllXQhycp9cJO0Ub5EVLT0yt8KZz8vMDHzMPU8Vh+ X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101547_706595_A32331D9 X-CRM114-Status: GOOD ( 18.82 ) 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 We no longer need a master->sva_enable to control what attaches are allowed. Instead keep track inside the cd_table how many valid CD entries exist, and if the RID has a valid entry. Replace all the attach focused master->sva_enabled tests with a check if the CD has valid entries (or not). If there are any valid entries then the CD table must be currently programmed to the STE. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 5 +--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 26 ++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 +++++++ 3 files changed, 25 insertions(+), 16 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 cd0a8c721d9fba..6d54a6105ceadd 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 @@ -433,9 +433,6 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) return -ENODEV; - if (!master || !master->sva_enabled) - return -ENODEV; - bond = kzalloc(sizeof(*bond), GFP_KERNEL); if (!bond) return -ENOMEM; @@ -638,7 +635,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct mm_struct *mm = domain->mm; struct arm_smmu_cd target; - if (mm_get_enqcmd_pasid(mm) != id) + if (mm_get_enqcmd_pasid(mm) != id || !master->cd_table.used_sid) return -EINVAL; if (!arm_smmu_get_cd_ptr(master, id)) 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 53b2d2b35a440b..0c134d535401e5 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1245,6 +1245,8 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, struct arm_smmu_cd *cdptr, const struct arm_smmu_cd *target) { + bool target_valid = target->data[0] & cpu_to_le64(CTXDESC_CD_0_V); + bool cur_valid = cdptr->data[0] & cpu_to_le64(CTXDESC_CD_0_V); struct arm_smmu_cd_writer cd_writer = { .writer = { .ops = &arm_smmu_cd_writer_ops, @@ -1253,6 +1255,15 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, .ssid = ssid, }; + if (cur_valid != target_valid) { + if (cur_valid) + master->cd_table.used_ssids--; + else + master->cd_table.used_ssids++; + } + if (ssid == IOMMU_NO_PASID) + master->cd_table.used_sid = target_valid; + arm_smmu_write_entry(&cd_writer.writer, cdptr->data, target->data); } @@ -2714,16 +2725,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) master = dev_iommu_priv_get(dev); smmu = master->smmu; - /* - * Checking that SVA is disabled ensures that this device isn't bound to - * any mm, and can be safely detached from its old domain. Bonds cannot - * be removed concurrently since we're holding the group mutex. - */ - if (arm_smmu_master_sva_enabled(master)) { - dev_err(dev, "cannot attach - SVA enabled\n"); - return -EBUSY; - } - mutex_lock(&smmu_domain->init_mutex); if (!smmu_domain->smmu) { @@ -2739,7 +2740,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); if (!cdptr) return -ENOMEM; - } + } else if (arm_smmu_ssids_in_use(&master->cd_table)) + return -EBUSY; /* * Prevent arm_smmu_share_asid() from trying to change the ASID @@ -2815,7 +2817,7 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct attach_state state = {}; - if (arm_smmu_master_sva_enabled(master)) + if (arm_smmu_ssids_in_use(&master->cd_table)) return -EBUSY; /* 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 174e9f080066cc..69351386dc1441 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -602,11 +602,21 @@ struct arm_smmu_ctx_desc_cfg { dma_addr_t cdtab_dma; struct arm_smmu_l1_ctx_desc *l1_desc; unsigned int num_l1_ents; + unsigned int used_ssids; + bool used_sid; u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; }; +/* True if the cd table has SSIDS > 0 in use. */ +static inline bool arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg *cd_table) +{ + if (cd_table->used_sid) + return cd_table->used_ssids > 1; + return cd_table->used_ssids; +} + struct arm_smmu_s2_cfg { u16 vmid; }; From patchwork Fri Jan 26 18:15:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533243 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 A2717C47422 for ; Fri, 26 Jan 2024 19:20:15 +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=uUWA51tjWVLAXkyFXFDfAFVvFo+bMrcQJbWoTt0qpMU=; b=u2AtJf/pTPUSDn QW4yq6HwLNueFLxqSPM1LwHg8kHv6CGrxv2UxAXtR0vrTj/LrQ4sWTNhBTaG8I1pXbGB0EKwupKzP Je9keYtB7bee3N9og89IdkZZXX7mYgwzyQpljFFPhI6qHtMjKhP7k3x2bQ/jH6a7t0qnhlzcJi8OA KQZfBACyJCWzFnapc8hWpJxtwsm654VDSTUUwpHUcn45wCRKTDyXRWrbk8pN5v8XQGi2fPxs1wYcu VAPPTyMZQ5OKtg5ClNiT1TV7+9Z9qnQ2IKKZUa/NzePzd3kpjnz9+vfyht4LsJIhre94h+cTB1PMR fJWDPqyziizQq5UCYW/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkC-00000005C1i-0F8n; Fri, 26 Jan 2024 19:20:04 +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 1rTQkC-000000051ef-22M7 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LuayLAXZ7zmaFNjxKOyQ9fWSo8Skgwwc0NXiKDL1vFAu8J+6yX12sURGfbpDXb1jtz+J91iZKJblZ5xw52NmOj/mPwqC4RCFTIHYWbg0kAEufoXy5ASJopsH/I4eGdrUgDtJjhELnK+p4GjE5UI2/FpIdFUsa8xdTDcMV0yWbbwb3gRABFbtTX8KyjcLXfSN1SZhiR5zcCWuFIk6f00GHGCEt/ailSODmEOyiGRzNLIXYfIm5ZW72ksd3c+5co6cTg5kM66R7K04GIx4MVAREtwhPWEl4+n1j06na5Q3141uMSh/S5QnMGzl93OGZBBEObOPM/kn0fzvU0Kjr/5Ovw== 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=EAI10d1ZqPf6FpJhcLv8dUhoBM6CfFKGFC/Kx3f70lc=; b=hZCSB0ckQJaV7SrHijqN44gHX6Xlo7Txy1NsAFKdV0Bow6ggow1OZFMyFiyvXRKN14CglfGppThIsX31Vjq6jHSP+tkdj33ApITkRWQLWbWmbIW18L1RM6/Yv4M4j8KluakLo0hhk5zRnkum8vBsAIaEGtuw62udWfekJmbp0Q6zGEXXt/QtA6mRo3MeDUx64njwlvwVbB06PZqSIk5VR03HFIJ+8Rwnpf1WyoJG/dNGxFHijVWD0+p083K5CCDmobWuQCshIJSAszT2fiRDHooJ1fPRVKB0Ec8knvfAjD2LKz2fWYQEoze02EOvTqQ/+CTvDcF90IA7VQgB6ARi8g== 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=EAI10d1ZqPf6FpJhcLv8dUhoBM6CfFKGFC/Kx3f70lc=; b=e7FELPIeCMMksX5FbHUOGlBjqBps1xzyvTH8eNFqhOQpt6laLB2D4/o6khNeW0TQKKsHYD2FPQSAlxwUs3XJr8sISnUxyD/Eh11mJWP5VFKRFx+6ItmOpzOWS+0GFN94ldcuSFM2PiXw5tGw1vc6IwPhzorzykEsKo1kCFwrzjfkfRbGRqQiofrfWw9cQuURDGawN8rcm/c/MxpsUCHdO9Nv6HupM5kMddvJrpmXPmObIMNIE2McqrsWRR28z7jlggZOTM0Efe0EqFEw2wvtGTlX+gCzAzkPP1NjjvSJHkVXT0mCMJ8ognQTkX55JFBt0dqbTTixJ3Jy9yAJAhULUg== 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:42 +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:42 +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 17/27] iommu/arm-smmu-v3: Thread SSID through the arm_smmu_attach_*() interface Date: Fri, 26 Jan 2024 14:15:19 -0400 Message-ID: <17-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: SN7PR18CA0028.namprd18.prod.outlook.com (2603:10b6:806:f3::11) 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: f3c7c6b8-6867-4c6b-e847-08dc1e9aca0a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z5Jpqq94yXBEFshrR30jSjTL3Vug4nikrZYmANMoMxFOF07qtIA2zUX6oBN5LCaGdXBwTtEiYFVE/X/Q8p8Oqc6IN2DBMiqNDv5CzJb3hjx36PYw8u39IQrtiPLnDinvyApN2CjkQPJId02r8zDPFgUsE1eNej0KLQoK9Nur6bL87aAHhEDDeNdJi2QAtt19xCcHVSF5UO/s7+YRLNXmGDWoCx/zLd62LNQN4h5PQYdiEAVuzZL6SNrtu8wckJYD9NJ452fzkrflLmkVeD0f9VckU+C5ftxWi1ZWOl9d/r9Oqgo9QswGg+BeqD4bg5XolkZw0/PJpn3Zwn1axF0PWnMuZ7q/y+qt/qGPtj28wfVUY8QwR9hi0bOB8aAKYDs/2MecsqGZonorfumumc693BJR4DwRSKnJ6U9y++T68jihYkZEQi41qJ3y/hwqtrKJYtE1GqfW6cJ1V7NQmUct3dbFvDbrQAYWtRT+1e5AGBZVXaXks0rS2P0KurgOYrU09L8LIst0mVOJbXOJRXlBrwLm6UWkj5YcP7qV4yS7ZHeBog1kLzlEm+Q3yCDmpQ3Y 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gbbJG9P3LpR+e2SNP2nmCeN9U9mzHxszWo/aenckQXnNGjFHyxk4QGAR/dA2RC5cQlE+UfvRQ7yuZ1qvn576T+0D+wxIKV/p78kZagmSJ+ymEe5hVxes8/tG1hAotfDq9MpQVgktjeb4/jsrf9rGH6jAXvrv63gvlSypNj7g6D7bl3b0HAQWQGSDxpqOgkwyCeZS9v1rCmuJ6pqbtfyVro+IRrgMF9Ie/oCEwF6HbhgfCTXVwNvUoGSzsuINCzRly5/x6tI4gQngFIvozq5dsN4maVtPtYnEGvY7M6N44O3OQGbdEdO4ztt6gfL4QT00OT3UZaitYDF6PaTgkaRpn/dfV4OV356KQgvnVB26yEkK4S3PSL897E1swYfaLZwK8qoYdmYK0fskx2TQpXfRcZV6+vXfCqCOYexwtQ+dp0lIpinH+Vgc98Af93OTbqHN0QvW+M2LEPMt/LC0GpzxPC+5FOKesN60bci/zMz7xXrVDQ2VEOGt4sKN7naV7KySnUEMohm0UghGiB3UnP6UxFCCXIJuQL7/HhtlIrkzAvNE77sK80oOebr6i0J2032QniCUvxQ+wwpubP/jADXGUUOdCPx2bg3AUg29dPnpSmWKBEqesjs3FZyIfQxbUYp6sGmHBHyQ2xrsWi6Qwr7nzqlAxiU8dbVC9VbGz6xrGRK8nvdSkqm1D+wmg+Owa3+9XUSKl+cu39e30bs/OBrA/co/iIayYeScoc32zXv+XUUG4Y29t+t5IsCk5B67PwTYO8kfw4JHSp/HkadJUsSvIqZ/+/nIKXKfbx5AMtT2i3IcjGk0GkNqGX2tXcmmL/vrwgi0yd5998amLffZd1AFFyiRpGvmaCUgQoOQgI/iA7oxSMjoGZHb9eAK4trgId/o5WPDCbh/0ILiKOLEBJ7pXTgBFR7dF1+p9+Hs+lUp+JHeC8tMbTsiKrhTLBc1z72JIp+KoirPFxfIk/rH89fJMdGHxCIfB+/BM4p2WmLZIwPdpb1qRR8Lvy2o7GeqTSg2cH+HCZWvMniSOJKF/+3NuIo1vcILqy/ZKFpgVsuO/ne6Zz+IOH94wFIEuTMGmy4E/iVYBVf5vZ7IPfW8tEPlZXlIs4gYMgSNgy2g4hYNjhsJVb5WetVmdswdirAvOusjwjtLpwH4rF/5TCRU8jfzdc37rRY8cOk1Yix7roJOwYRwGLgc2Vtg7ngP8b7AKRa3TCK2RYCLAj1HWdsyr7wJcc6hDtnisQPp21n4AX2FD7shcasaUmHiMAeLPa6xK16Xaw6hEsmtBjqclVLdKyun1KtGpisfRm+FmgA+AZfEq2E9vzrChb/VdbQDvzXOjED8m0Ygx769Ba0vv7bA6Q7+vaeDh5LU+X5kxrLYH093DDx5oe7++1l/ByOWUGUw3+2L6VDn3l8TSa8eRsaTZVq6fKyy4mSb3Pxtlk51zMuxZtI9XyhGEHEfmbZvN0+n3f6OxbRdrZukniBMje8JG4cAsI0vDkrNvz54J9ZcpUtT/Deqge+DKAu/iyEln5iK224jiKONJ+p7FcmXFSY/q3A8FWwWbRtFrF4X3BjxLV5rWFc+I3y3audHRAX9dBOBNLSR X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f3c7c6b8-6867-4c6b-e847-08dc1e9aca0a 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.4350 (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: jltUHbsdPCFSHmUT6GXjct4dlbRC/I/mxLTppTjeBBpb3WhAUOC8pSvYYLgAB23n X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101600_659111_0991CD81 X-CRM114-Status: GOOD ( 14.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 Allow creating and managing arm_smmu_mater_domain's with a non-zero SSID through the arm_smmu_attach_*() family of functions. This triggers ATC invalidation for the correct SSID in PASID cases and tracks the per-attachment SSID in the struct arm_smmu_master_domain. Generalize arm_smmu_attach_remove() to be able to remove SSID's as well by ensuring the ATC for the PASID is flushed properly. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 28 +++++++++++++-------- 1 file changed, 17 insertions(+), 11 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 0c134d535401e5..21b0ab9c2ba763 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2013,13 +2013,14 @@ arm_smmu_atc_inv_to_cmd(int ssid, unsigned long iova, size_t size, cmd->atc.size = log2_span; } -static int arm_smmu_atc_inv_master(struct arm_smmu_master *master) +static int arm_smmu_atc_inv_master(struct arm_smmu_master *master, + ioasid_t ssid) { int i; struct arm_smmu_cmdq_ent cmd; struct arm_smmu_cmdq_batch cmds; - arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd); + arm_smmu_atc_inv_to_cmd(ssid, 0, 0, &cmd); cmds.num = 0; for (i = 0; i < master->num_streams; i++) { @@ -2506,7 +2507,7 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master) /* * ATC invalidation of PASID 0 causes the entire ATC to be flushed. */ - arm_smmu_atc_inv_master(master); + arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); if (pci_enable_ats(pdev, stu)) dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); } @@ -2593,7 +2594,8 @@ to_smmu_domain_devices(struct iommu_domain *domain) } static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, - struct iommu_domain *domain) + struct iommu_domain *domain, + ioasid_t ssid) { struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(domain); struct arm_smmu_master_domain *master_domain; @@ -2603,8 +2605,7 @@ static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, return; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - master_domain = arm_smmu_find_master_domain(smmu_domain, master, - IOMMU_NO_PASID); + master_domain = arm_smmu_find_master_domain(smmu_domain, master, ssid); if (master_domain) { list_del(&master_domain->devices_elm); kfree(master_domain); @@ -2617,6 +2618,7 @@ static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, struct attach_state { bool want_ats; bool disable_ats; + ioasid_t ssid; }; /* @@ -2648,6 +2650,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, if (!master_domain) return -ENOMEM; master_domain->master = master; + master_domain->ssid = state->ssid; /* * During prepare we want the current smmu_domain and new @@ -2700,12 +2703,15 @@ static void arm_smmu_attach_commit(struct arm_smmu_master *master, * SMMU is translating for the new domain and both the old&new * domain will issue invalidations. */ - arm_smmu_atc_inv_master(master); + if (state->want_ats) + arm_smmu_atc_inv_master(master, state->ssid); + else + arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); } master->ats_enabled = state->want_ats; - arm_smmu_remove_master_domain(master, - iommu_get_domain_for_dev(master->dev)); + arm_smmu_remove_master_domain( + master, iommu_get_domain_for_dev(master->dev), state->ssid); } static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) @@ -2715,7 +2721,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - struct attach_state state = {}; + struct attach_state state = {.ssid = IOMMU_NO_PASID}; struct arm_smmu_master *master; struct arm_smmu_cd *cdptr; @@ -2815,7 +2821,7 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, struct device *dev, struct arm_smmu_ste *ste) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); - struct attach_state state = {}; + struct attach_state state = {.ssid = IOMMU_NO_PASID}; if (arm_smmu_ssids_in_use(&master->cd_table)) return -EBUSY; From patchwork Fri Jan 26 18:15:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533247 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 69EB6C47422 for ; Fri, 26 Jan 2024 19:20:19 +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=37tXa9KZBySv8xOWrR5uW2+vxqL5gQN/roxNjaeyMBE=; b=wnUNKrQZJLMeJW TPHqudwxCTqlQOkCQ1XtaZldv3w4X0BZczN/TEB/HSpthcX/o4V71JjmLKGP3dIGoCOeH5dIJMBIJ 5EnuWNAqifg4f7Z08O7KIRGUEyokzYhXX3hlwiz55mms/Dt8LZtBQgrXtSKzEgdxd38aR+Ck9yJfD R3yYpV5XfKht9g0qU3+M6Fdm4yHe1GrEdX/LVDYcHzkIplTCfy9YiMHG1qnT+Az8X4A1xGPB/Vd1Q R7pCTDDblT4lyJdOdWapk29fMB1Vw8n34b1EeCGytpIlRlO8+pZIL2eJwonBP4DNoofw+Jk5o5lqN d/EWR4bIjZg3nd+F3YJw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkE-00000005C3k-2VHL; Fri, 26 Jan 2024 19:20:06 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkE-000000051Ud-3VWO for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T9n3mBRWxtn+QMD2GVALBJxusn85IgeH7Re8cRB4rK1/iKqF7ZUZE8rQZnprMYgT+AGKUL7jKKKXGHGc14cuPCglC0b5oYeH0BEcgR0nBCB1folTdEL5+hvPN0UvkiD1IjrNoqHnqVnoESG44eaPppHkLpxcjt7a/r9pJH+A1ZNvW4+ma6eGIjpLsOH87+Zlzfd1RBZ5Y8pfw9prFKrkfzVmm7F0k0W7RpYpmQ+OgkHBHhVyt6G/IK66ZxrA1KBjMC7SX0Cky8jCHl9HYgZJvObcjEmM/amgg58OEVfbyxGoF2Y1D4ByMAKSKU0xlV/UkPn0mXdZ7e47/f/R/RLdEg== 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=roQg2tDJboKURfA9TrqNkPNL7aeVUAVLO8hVw983YzU=; b=VplN/6FDrKdnkZupiYs22O9nKEkeX7sIyIGsrHaKcq+pxeNjSI/IqFqrAyBaxnlWs3uL/Z5AXLKB4F73kiTceUrzzBNMcUDH/0E92aEzbTf25H9BmAK+6Hd2ZxRSkKgr2tJVtP/ia5aFg7n9p03Kxar+NAOdI4Y83tQypwB0twDGia1OKTdITs9mhrlSOs4WXwfunH15xjitd5gQsX496d1qZDcs+j1Cpau0QyvsdwyqiGNzxZRfqIgztvwjnriWPN8z7EDmgdGJrIpU5piVssDnWM/optmAsNTiGHsa9vjNwU276EK43922jVcVKG11s419zngN4j4lHsl4BLKbMg== 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=roQg2tDJboKURfA9TrqNkPNL7aeVUAVLO8hVw983YzU=; b=QEpwZ/CEhqYNzRakMH22cUW+parab5zXSVckyQESDDiJ5neMt1qeW2O1oAOuKACGBdT4FBGkWEI4mmJeKaEgMS6sk3QOsfXRdVOVOTaKiCjOMFG66//EdsODYweDJ3fjjSuTG+SqcZ8ud832ql+FXnYgi/XayZfEP5tzdDwKDdnFmiYiNwItaISGg0asa3F2ZAdFi/J/X0VnollhoZK3vsQFJJeaSIRxkKI/mdcuht01gjX3ce5gF9PqyrOAIUXKL5B6Bhq+ziO1LjD9rFroRMW+NunJThg1U01u7EwMS+aCc0ZsxRLyNwLAFFe2zYAtPkEnC6Vptn3jLie0hyJPzg== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:34 +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:33 +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 18/27] iommu/arm-smmu-v3: Make SVA allocate a normal arm_smmu_domain Date: Fri, 26 Jan 2024 14:15:20 -0400 Message-ID: <18-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: SN6PR16CA0070.namprd16.prod.outlook.com (2603:10b6:805:ca::47) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: c4d97cda-2e6e-4947-ce02-08dc1e9ac858 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zH+cpbs5IjbPCWtnEbJ2g0mDZCj197s4JJu0duRq/cMuW5AniU8G/0nCYuMWACE+A2HRaelWfh3TuXzos3FA/AiH4xMKWSVaKqke5YpDAnDYaUXaVtZKLZwwZoNQTfFKxUUahuqMUplbyBVJekvRi3uSLtpXnNrBGA/XXlDUDA1y/kMtFizq/4fsa2xRMk3pqblzrL04W6CSv1ddFzRd62gBlH+iCElRU0z5nsCELxPiy6fJHZ85IghxCLSOw3YQgtzrrSqKNfhL8OXPRNoJDdIDVmj4qFhexxAXz3OlyzbTl/M3ZcrKbfgLdgpxqsgLt2zNvnxPTjEJGJ+pepBTQ7aZ0BYcjRcj4+eQk46hZ95jsPsZ19if7atlS+gYthZ4fqyC8e6zfm3AF2XS424D9UN6xw2ffKTVAygqhu6T7ytgj1rKVFggoM+iIXGiShHlS2KgLl4YFF73x4VnohPY/YuLU/5zsX4hcLrTwG+7sqHjry4ZxTM4AMWS0D/xhOH1BVrRj89SMXqR+WctQGpbqMIH12CNoCRCOoX4Zk7B8wyiOYVmuXoEmWW/tnIhr5t3 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qGfCGRWjmzg3wF64MtY+QaS2HBNuyMabLS6VqVCFxiQn8WazYEYC74YmoP9KrTTRD792x3MaC+Si02YSk7+WXiSOD+FdO7XyNb+/G4s7UApGChjwxuNajwjTQAfiCJxxbXjYeRNK9VLMkmmkXWtuwmGdKIBDc6yvgSmNtR/jT1Ufx0fvTXjDd9ZGQGce1qDKiPC2g/0a74aSeHs3bEs47r+TNy9rokQyuUV5rQ6xVY/t60OcN/2tmxYZgPiP8b4km0TbxO1t1hWODPS1r8o0MnP3/tlxFAYFA5/QQx+1m+zLb6+d6YfiomTxxSBwFFiJ0IEq+TdEFfMZrC0o33xjxyjMO1aytwiElfpKQOw+D2/rdzfn2I0J03oD9CKAW/YaPi7AqGwAaXC4EtKojBVVGZgApumq6yJHt4yCR4U+X7LkdN9yaYUwxOHVOVEMJpd/OK0DhN5znF8J+NWuJRzuS2lHpdbzgIeYekEtZcOWj3RWS5hbIAGYqDW9n6yK6+/R47svJezSeTgdzK7AhsVIpLV1rJ5GLOWzBqm0ibgqxYsSqIosXWPrMG9L0XEAfuOsuqHE2OhGhxofEMRYpRrMBaY3ZGZs3NbstaKEK+8RNytQ0Mn1xgCO+h7OBWSA8lrCUJSsdwXU4IYwv2Hid4vLqzJxe5+Ry6jANqBJwQYYU4hte/ZOl7v8GSV4NX5Usf2DN6+vuO/hpmE8aic3yRRiV+kdIPxHXdFKQcjnCbGoRt+/J6EXdY326lEVhPYsyz85C1uH6rdNpcC5gIV2zHUOZ39XNN8Q5fSMSPTZ40d8yPgqqRSgiB3B3O2TAx3x1RVn85C7r+0EWbAr2ZXfV6mrez8Gzotfluu3LPUuNYGTGbpDYUj3tB46Mtb6WfbDXdF9eQKQR2l7Pz/HCbhSpNRRYJiEnZG9TvDUhmxgQWb28HtkLNk2WXoak3s6zn//bQFdY/MOy4cUMQ0ilNNwyVyKk0Ln9lnCsW3zkOkCu5JOddSgRDASXAduJ0kOkKEZ8JRcS8/e7MQqxpk08Vftr8C9OrBh51rd+cY3/FyAsZsTbmAr0UXowG64inaYcCPOvkSCMPaw/NwPJceMDJt/vZWiEI/7o8lwlo69hA3e/TQIw2GirQxXyCDkfIhPmGPPNmtsWAtDvJFnVkrNO5tTyxL6ieSVJOujJ32A/0m5QmA1GaOmIPYu39d5r8+SqaHPFrqpV43Hs8wcICq3RG652GPVUx2UTD92LK7YLXhShAUCyR9VseH4xhhJajnEICieUpIfUagj0aw7ARJgLT0P8tIvv9OXrcRkpqy33I6mwxGWifVa0mg+Ic7Qm1/w9De0Ps9SReCIToND8HevqRyWjK+QEBZB7VIJqN6Bysxm9aHrD3Jw8+pwojY6NAVeQlWvfWs7deYb48Pzx6fNCg3J5l5CK/r/7NMeGrj+dtVZaVVD3JCnHm9kBOrvANbP626qUzXer8qFIPD3wFdREJXtmOOSCezuja9HZEdgEDTfxAkQojkNhSmPaoSDJqqDTPnayxwhmwm2M5QJV6eX3X8oOfIaKB5pxHatPD3V5k3h7F0a7g9Uvz9PIXUICzh1Emfn4G9H X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c4d97cda-2e6e-4947-ce02-08dc1e9ac858 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:31.5814 (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: qmYPq2tmaeguVovHl5fd5kqm0daEDWqn5iMFnN8/gGHv31YRvnmNE9JOkt8sLMEQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101603_018744_E1A1A10E X-CRM114-Status: GOOD ( 15.72 ) 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 Currently the SVA domain is a naked struct iommu_domain, allocate a struct arm_smmu_domain instead. This is necessary to be able to use the struct arm_master_domain mechanism. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 17 ++++++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 34 +++++++++++-------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 4 ++- 3 files changed, 32 insertions(+), 23 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 6d54a6105ceadd..1163c7d70359f4 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 @@ -662,14 +662,17 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; -struct iommu_domain *arm_smmu_sva_domain_alloc(void) +struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned type) { - struct iommu_domain *domain; + struct arm_smmu_domain *smmu_domain; - domain = kzalloc(sizeof(*domain), GFP_KERNEL); - if (!domain) - return NULL; - domain->ops = &arm_smmu_sva_domain_ops; + if (type != IOMMU_DOMAIN_SVA) + return ERR_PTR(-EOPNOTSUPP); - return domain; + smmu_domain = arm_smmu_domain_alloc(); + if (IS_ERR(smmu_domain)) + return ERR_CAST(smmu_domain); + smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; + + return &smmu_domain->domain; } 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 21b0ab9c2ba763..a8e9276990b006 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2280,23 +2280,10 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) } } -static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) -{ - - if (type == IOMMU_DOMAIN_SVA) - return arm_smmu_sva_domain_alloc(); - return ERR_PTR(-EOPNOTSUPP); -} - -static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) +struct arm_smmu_domain *arm_smmu_domain_alloc(void) { struct arm_smmu_domain *smmu_domain; - /* - * Allocate the domain and initialise some of its data structures. - * We can't really do anything meaningful until we've added a - * master. - */ smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL); if (!smmu_domain) return ERR_PTR(-ENOMEM); @@ -2306,6 +2293,23 @@ static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) spin_lock_init(&smmu_domain->devices_lock); INIT_LIST_HEAD(&smmu_domain->mmu_notifiers); + return smmu_domain; +} + +static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) +{ + struct arm_smmu_domain *smmu_domain; + + smmu_domain = arm_smmu_domain_alloc(); + if (IS_ERR(smmu_domain)) + return ERR_CAST(smmu_domain); + + /* + * Allocate the domain and initialise some of its data structures. + * We can't really do anything meaningful until we've added a + * master. + */ + if (dev) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); int ret; @@ -3278,7 +3282,7 @@ static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_domain_alloc, + .domain_alloc = arm_smmu_sva_domain_alloc, .domain_alloc_paging = arm_smmu_domain_alloc_paging, .probe_device = arm_smmu_probe_device, .release_device = arm_smmu_release_device, 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 69351386dc1441..cb08b9305e0208 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -759,6 +759,8 @@ 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; +struct arm_smmu_domain *arm_smmu_domain_alloc(void); + void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid); @@ -791,7 +793,7 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master); int arm_smmu_master_disable_sva(struct arm_smmu_master *master); bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); -struct iommu_domain *arm_smmu_sva_domain_alloc(void); +struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned int type); void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ From patchwork Fri Jan 26 18:15:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533114 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 BB35EC47422 for ; Fri, 26 Jan 2024 18:16:45 +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=wpZUvR3lXmXSRsgfYqk/Wk2Q4raJ3nh6SFexGBZRTA4=; b=pWMYnNDjSDD5/I PxQFpYsGbu/xggVbp79ryerEQcLoKytMGCPlHL9IMkmGERx0iIVdpZ0uy2ixQuk7vZSwM+GOapdAf e1aqRPW2FXsrTHnD9D6iTOxYIxr3aFJM1+EdL3b/3JeqIGuH2kaSLzVNWCPWVDZZrEt03w3capitj FF4jkHlnurIHK1a+uRbSnuwD1mlPmKlgMbgs0lxDlNMW9Y3I+MkF3vy3Eg79vso7F5xkbEQigYmD+ cVcqIl1cmcjBf/lUDCTpi50pU3aKO0h3mevbp20SP4Prl/oRezZeTkJ6CD8YKucZlIS3fFsWVAj6z M5HAz33fviB3xK2krhkA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkg-0000000523k-1O5V; Fri, 26 Jan 2024 18:16:30 +0000 Received: from mail-dm6nam10on20605.outbound.protection.outlook.com ([2a01:111:f400:7e88::605] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk4-000000051Vs-17ak for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:58 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RuFwmBn2n8zXVmWgvKNJ52CH+SCSkrSr5ileNVPqRrTFeMSOG57OLg29NMGOJH6t7E1NyAHgUUnuEgP5bP87qwNrzoH63O6S4ZrQK2pY2Ixb8o0+30ECjx7ulqTLqKgih44pzfJDSJ4M7oqI9EzohqAkxkGCAuzZiAiJtYc1Y7aLiaei/kxE+4xI0h/MOg64GwYW/pTtqSTvFjxyPbxvH9Eutw7N+xeBkgw4IqbWS0l1NBRkYxj/B1haXPBF0wR371DUz/gx9uaaCtpGS8tppjIYfut4xDXhb3pKWmGpBljwnb4CdPecsL1RteXnLQZV9h7ODME8r9xu2EFAvRm2JQ== 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=OjupnVJ2T0we0nVfAmgNqoQcbNhw/8HuxqhGeas1gDk=; b=BYQc8OQFkHH3f1dsDtwNokpcCS+UAROSzk/9fjdQQcopVW2gAv3TuUmTq8bK2l7Sh+BRp4pYUKOMN08uiS4hDshXJCuIP7yEv9dt0J3MtUhK2sk6JKarOs8uRoNipQjIMbNqvUzLEFC5g4RlQO15Flfxkv4UCRGOFD3pkmsslF7AkXj8GRzCzRtFLE6ak1aORZJ8Nu1HlVPQNq9RF3Gkapxd36raWmOrwsA72vkCb6W+L2hl9uQSO70Er2Tojes/DSQM32hkZYKAgMFDBRea4gTWOe/+AOr9E9HObxCvF1eDZ/KBSjGSWyOLJC38S+wcIT8A20qDAAst9O4vI28JXw== 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=OjupnVJ2T0we0nVfAmgNqoQcbNhw/8HuxqhGeas1gDk=; b=Zd3ltFGad2OH5IoZbG7I4q5WvlXOmKza5M7aVZrR+epwXEt6yAqFa/1lHCc1kjOndCBoC7Mkf3j/eiWa2acEhZ9jDY4DNtolDuf5nftdH2wKJ0weg9QaaJksaPeL/4dITjxgk5+DAwH8PZmFQXmk5Emlz5XbmnRabOBujbJzm0LPVgTp0W9pKEYz7RfaKFQt/XOAyLSCb0mLRhht0I1nT7b7Pf1bQ2+IxLoBdwHyCI7B7GseRRxxmOd5xZB7Fvd2ujK/+5cvb/miV6QUXJmp+kvhXhcXNWeqZcHv5GyROi4bbQr4f+/yQjet5wwA+ynXvCHWGtViA2jvJvbgpH6IKQ== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:37 +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:37 +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 19/27] iommu/arm-smmu-v3: Keep track of arm_smmu_master_domain for SVA Date: Fri, 26 Jan 2024 14:15:21 -0400 Message-ID: <19-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: SN6PR16CA0040.namprd16.prod.outlook.com (2603:10b6:805:ca::17) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: 84361178-3f99-4e6c-3842-08dc1e9ac90f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7uwIusB7BChwD19YiRzf1iQsN0XsI//KMUPlaVYU+nX4xcF2ZJjjeNfMZyQQKvBTajkDPvC+q0l9Np9SR515k79Sqirw/bsaNfZ9toSng+ys1orNCOZbSFgO0q+g8w+EAFdGmkiHBGLxFDDRA7MSx1M/WDUjhIj1Z4gXAdamVs0x1KtistSbyOQQ1MsMR5Be0Bhu7OgoNOc2j/ARili0uht2egU58SdL4pAfJKuCofGeMggUV8wdK7MreMiZ06l6X0LwLqh7XfX8AIJrQozw3ArHgYWUmPWpWVRvXiNFH5FWpwSQEOsRsp3C6aaZND8lcupz8IVKvkVfeqeXHYnicN0jkijGq/kQaHpjJguDfpHLxITgb2jric1ufS6YhZP89Y7oFcXp5ppyZe+jycJHmlqUoxL5XXtOiG/c/wwzDNE1GRYD6ViRmyriGMPw4jFVP5eO+grtq5v+tjvay39i8m0PLynC8MyHSj3gG3cgjqHplo5xlXzBr7gidpjv1n0ECUaZELv6uz0NfSDY7f2L+LrkJj+UOCxpRdhTtHzLcWIKcoljQxOvU9jsDKkp42B6 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2Y3MHfiTez0GkSf+zBMccv6Fz/YTkxALL5c4EwK85gIVfcnwhPxG+r4juFRKaU8+697EWIhwBXs+q4S7AKUCShEue8+EYB5/PhDi+hdJeuQAcVp8IBiYfIMrCAlkHWl4a+knkmiB4/kEzB5U1XBDTcRft5JW+GjpMNV8UXIAV2nW6dBlkFbuqO+sGYSoSvN2mUeQUUskiCzcsx8uqlIS0prmnAC7PFWKTgRg3k29MnEiySdykdhyDEPyIzh6ey8plaFmvxS75yCGHNGH368+h69gPaYxcFUKs+qLuq4Ws8FW7QDlI6V71HWl0zM7jQ3uEd2iCs7g9Wp4j4TwEIXkTPf94jRkXbYdTn5HwQ3lxWrnGV/Pk6vpRYitmzkUB9ku9tdglu7KKuCsTMWzQ0Ax+i+Cu2OrSKYfSe1MXZM5kGgkcSoi1cht1vaZA/SHydIrZmkhUFSaNESW/NPeSu0puyHHAgr9OL51WptPm7IacKYgEbYRYG66sNX2jfqnz5lI7Y5CpXp8ZKbBaG1rIZVQ3sAa8wC8lPuLKiX/kQUypQVbvDEnc9eF8kfVdEREQbh+IdPG26SfVkHw68O5inhTi7bcYUVoTc6O5t8zLBw4RZPxMwpg7MTTcGGj9bMS++USolP91X8KGzJJ7hItMfArv0D2t9hB57XOO3mrk7CoG/rxrn2OE1itnHFezW/tNbyynJL3sYlDpk0+UzPEDG4VMv8ls19CsM3IAKedx/pBzGa2HOjFejGuSvVePMR92kLyEYZOVUysafBxcCARGQmruKQqQgPex6aQFQANHuMTfoRBuyakVmZgg0b2uqlPQsWt4WdaNDzqTmAYNuDfpyq7M9uS8QR+Pqtyf0Pa4o56TKcMqSdxLbsKHrbVr0UbCBnoCPO8F1W794HXT6wROHgSk/T10q2ZgCyPWPyeGsbHkrgbk4EKa40qfQF8dgu6V2bGEzBDC/vNbJ588P5WG4GIf6NOBD5gfkTKOoM2YRtBU5oOt0nribp/6Alus3SRhVQhf5wqS7n30teEeGBndOU0uHcnh3XxNgQrZ5I4IddT7L6XhOv5aiDXf/ZgOz3fdEUhlughzaDX+5pLyoOdT965UFsGczoggCx3HMthLBMo5ZZokKZlA+Pu7gXxivisLD8GM8uPlhDQBNEIfIPfSG7yx69tpUmHP/QAePa1VvcvoiJZi4AspPGhXuEOh7cX+Ikz99ivVsGRkBW5xFxF17dyrkQ7Z481wD3iai8rOz8wYP4DmXm7bH+nMJnq+yw6fdv2+y3CYSVRGrCIXZHEZbuJ2ejoEVwQVjfg2lr1SvGwHxzGA1hJx2tx0GlCWeCl44UGmdIc4zXPHxIPyhjg2Uw3Ij2rPBuUT9o8Q4cgHc9EEMOCoF+tADEQ4D09igHT1g/B4ILT35LE09oNPo1nkqXskJd+bHnobyLEjLfcgOwJlZ9LJCGHMJutk9upRJlUk/SezUHPITu4211EZ6RKpIDoukG8bYkqL23/e3qnfCYmgV6D1gkC22Z5fodTkQgn8Z5uCRcHBn4o/CyY6MJZyFEw7DnHLTsKP4JsKGhVRqA/fLlC05GfH3N8lAShcXgLUyUA X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 84361178-3f99-4e6c-3842-08dc1e9ac90f 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:32.7869 (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: ZZ2TQc+3hWys0v2EKvLhi6kwrQn43ji4cbehgTR844VIUh8f4YEC7dV6XRb2EjGD X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101552_445463_F3276E0C X-CRM114-Status: GOOD ( 11.95 ) 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 Currently the smmu_domain->devices list is unused for SVA domains. Fill it in with the SSID and master of every arm_smmu_set_pasid() using the same logic as the RID attach. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 23 +++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 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 a8e9276990b006..961000f308b9bd 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2592,7 +2592,8 @@ to_smmu_domain_devices(struct iommu_domain *domain) /* The domain can be NULL only when processing the first attach */ if (!domain) return NULL; - if (domain->type & __IOMMU_DOMAIN_PAGING) + if ((domain->type & __IOMMU_DOMAIN_PAGING) || + domain->type == IOMMU_DOMAIN_SVA) return to_smmu_domain(domain); return NULL; } @@ -2803,7 +2804,9 @@ 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 attach_state state = {.ssid = pasid}; struct arm_smmu_cd *cdptr; + int ret; if (!arm_smmu_is_s1_domain(iommu_get_domain_for_dev(master->dev))) return -ENODEV; @@ -2811,14 +2814,30 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, cdptr = arm_smmu_get_cd_ptr(master, pasid); if (!cdptr) return -ENOMEM; + + mutex_lock(&arm_smmu_asid_lock); + ret = arm_smmu_attach_prepare(master, &smmu_domain->domain, &state); + if (ret) + goto out_unlock; + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); - return 0; + + arm_smmu_attach_commit(master, &state); + +out_unlock: + mutex_unlock(&arm_smmu_asid_lock); + return ret; } void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid) { + mutex_lock(&arm_smmu_asid_lock); arm_smmu_clear_cd(master, pasid); + if (master->ats_enabled) + arm_smmu_atc_inv_master(master, pasid); + arm_smmu_remove_master_domain(master, &smmu_domain->domain, pasid); + mutex_unlock(&arm_smmu_asid_lock); } static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, From patchwork Fri Jan 26 18:15:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533112 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 3B985C47DDF for ; Fri, 26 Jan 2024 18:16:38 +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=v7PC8MXjLMDdSY86ripmsP51a09KEu7HHQqdxsXu1UQ=; b=HcHoBbSROKPWHi zTe5sE59i3SDQjwBaRS0SOoI/OQqUDSHGXaOeSz/b2qL9bOLnKqchCQSmONNcOd5iUMVywYQFgf4+ n2SSa4+Mm7yB0KVK+66EunEvTzQxONjKJoToQ+cs1cN4Mdp+hDCG40c6YMkuCi8IxquQOJxiPu4eV AeEweEmFkUsDOLCetAC5pA+Rg5/Mr88ziQ1bYhjyUc9FICD7s1AZqb/mEAGexNrtu7qDBzJPCH/yq YmbCK4KY3rg/xr8xbofFoHYd7hIQBXEdPasSpsw+bF7z3kH/gOsD8xZffASiMyG2gEuffRzdIL3Wm MaXw9wEEdrlFvfhFoy/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkc-00000005213-1xvi; Fri, 26 Jan 2024 18:16:26 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk3-000000051Ud-40On for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:56 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eKkyeAv9zhlUzeZ9fKKwLLpDokUk4p8fgnAz02pjpl32uhk3zHGGrKh75O/IqSCiXC5jLUI1Sve0h+P0rydrDc1KfalnnlxuYAfQAgYjv1QMGszacl6PZkWH7vbcobEOiaz3OfubqKXI9itjxjWg7tzCpjO+LMUJqnp9EHMrsd4E9dAQ32Mzsvf+rNucBbK/2eR5lsN18sv9XxTDJp1dZfUFB49bGeIKpm0S8sfVKO9bVGP+jFXB6U+guIEl51Hkd+DZ+OSgl6rISJSVvMtvm3CUwPmBF6eqeRh4wIMdeAcFEs7yaYVvvdAkGZdtNY4yIr/oPbMnHsrnKgNVrQBxZQ== 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=YLgRVqMu61EII0cVhYEFcw0CHxLI0ne9la1jQmieQfU=; b=VjB8aJG3UVYD2FQ7kkaXYCepDi8ntTh2jrGUZkErr2J3y7VYjnszFhYkQU5XBEidEJdJIUzs3+3eEfFIk7R05cTYUgf1YDbPSm4+ffpCa5NOFx25x6SX4WC2xuOeU3W8x+hPeV7Tpn6Jkr94CsxZ0UQMp22agk2AgLtHMUZLwn0Cl1dvrl8gVbNLiN5T9AkHxocdM6w1PmaU2/nzCjSAsvIi6ZPUtBFJMSM4Q3bbSRgQsQy3gAKCigeHSBuMvO21QOyPixy4Q68kgJFu+vZ79u6Dm/QE9hHcvYnj4gC2w00t/5DjGPnpg8aGXIht2W1HNyRO3XdLUofJGJHTWcgxpA== 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=YLgRVqMu61EII0cVhYEFcw0CHxLI0ne9la1jQmieQfU=; b=EPJBFI+sZ7b2nUDtaoeGchlGH7SdLmzk9vC7F2LJ8w3DG2ChdgWuC9v73phKJFa9uaAQMAhiF3CdFfsQtJAtfcQlpPiFaLenY3WXL2tQIM5UlsvCje43osoBbpfa0TeAmA2D/hzer5SK59Tdhw4ibvO+P0+Dx+ENVKx/uc1Cpw3tG/R7z4E37eKD3hrv+DS6Jgck29nzU9AyVgOAFm0nbvP9Z6709aiXSTX/BD9Lsxe0i7li1XGg23kOWUcUFEeo1F0GiY1LkEbDnWcUngmjkWsIldgiNYSGzOLPS4kwfi9W7SP7cKwpG2LGW1PiIfnPKkRArOPvGHz+j3UMf1fr/w== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:32 +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:32 +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 20/27] iommu: Add ops->domain_alloc_sva() Date: Fri, 26 Jan 2024 14:15:22 -0400 Message-ID: <20-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: SA1PR03CA0016.namprd03.prod.outlook.com (2603:10b6:806:2d3::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: bd06d7e0-29d5-4772-3aeb-08dc1e9ac838 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F//9s3c4MMHK7PoKX+24feWkdM/4BMBwl72BxoJbibpn1wU66+EtGPgzoP6TY1iyibQ6weUm2u2/RkoCDL5z4e6q7SHlkN899BYrTv9vhbTYQl/VljX2gwaQ2jEEAK/tQOCzthsQYiT0/lNP+b3jfpYeP6EskWr+/2Ibfu8Lr+gv0EeMZiIvkm3hEusoGx6dgtEQxXOQyHMhN72Dem10gZ/DegehErjh/3IREJyWhuzeXAzf1gOPVIgu18lbhIKZBtbes2sswc+bd0KHpGvqH3OlPTPqA+G+bsXPai+CNOWxhNhy7XX75RK1H2UTYZOGfieSw8vfFApAavLCj+mMkNgRa3viXMfdYpQxn5xn5PBwy2nc6CqM/NcEoNF1ONheIdCKh356N/YPGJdBVWpMmCrXtN+PK/9URucKJw/zM3rB3XVLpl/ZyFnCEw3ZQOGe/dKh/3voCTDiv6t/Fk2ylk/DFfjLE2WrbFfv2qqaSXlPFZV6kCQ76hi+Ndnpwu6ECDVqKUr66A9s0J5YB+4sCCneR9b59SFKFHcTU74OAxpOf/G1PKcpweRp4mh/DZaF 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jkEQSDGfysIgE0gxoBDsDiJMS1GEfnUjvKN3RJ019m6k0Z9Ul+hM1f++AXTp/tukPSs20vKAe+eTznEoUs9f841bV9mjpgE15uh6ibLFW2i44znqnoxmEEp5FEdhibRDvQQ8g3BhLY76NmF3wV7Dbtkqz77UK2A0f+8VvtFagYpZTZWaXP0zGPU6IdcqhN9XAjb+aQUmzvOIUv2t5ttSIQNm9IzyWC6OeJflcKVZcj3KcztmL4VAWvzYtL6i3mhfndwizH76tyojrvvUtYh1/cPZGg0t7HozAapLOb/XKbHVx2DZd4EFAy34gIOM1TiX2A+9pqGL7uPuwJoQtpk8v+z9Mbfx6KQBNcZTqH8ygAvR/UdVpXu+Pujs9aLdMs1RRkc7PmNozgqZNlJ0ifedqnRbucdGLKbMj51lyYaY5SmxWoYLK7S4GbJOEjuFbAaKCkqUEIVlF3N3Ht8FuhEaT4YG4MRdyEA59XDkXNF9VA9bAWRvA0l/tVxwMNShyQKo6kOaCcX2jl1Xs8esQhewtwtCkjLm6Tlgojm89IZNq7bfWlBih72QcqIkHGVEKQ+OqUuuKFXNRnxkRRNZMK1tegzei/Q19kPd4Vp83yWxypOESIgUp6f3BXOMas0kb6ErSkXlMFZOqpZykhv0eMP2uG2VzM0FtNv8V+nLKrxpVboM1hhwW9XuiJx35tKeR2Erdu3ChTaoiSpd+SAJq+eprP6hkggwQ9++OS8aY6pOO2AZyLQlj+MfFvi5vVpX7Yef/yWNweDBu1L7Ndk7/R4uQcf4u2nrzveSCJOzBTBSHYYMkIA9CCr81oiyGsjNTWaOwChZn2rb11whXFZIrSiWw8KrhtGzVqFFv5vlxxCalYvwwJ0Lcwq2WIAyDHoIr01DQaRwKnBqbxH4i76EKJVUNWYHlJOn3XjjBmuQhW8KADzZ4fvX7OLINaSIX5gs0y/t7m3pSmXNAHalx9rgb9UpWQa2QNWjePRbm0HOVzwBW4W0tdgQ0WW+Xy0lQDPyp8s3I5lzX81XI5HpFdbDp8qjXZO8xbocLA8vY/1oogxLssJivIvXe0KkQMLsyL2ySYRbTJeOquXYYwcxPINHhmHGgIpHYVGhnII1bUodo8n4PuaertSFFQmJ7PsGrWo6+dPf+rUTzoJqF7DBXnl/F4mP6tYenzr719X3NX4YC1DhCn+aCA2V02zbFmY6m5dTvevUbRTtTdQSE63/PyGELTQIiVrq0AiFS/ZaQPu3sR9HLL4ZlsNsHSAKlkjNvpiCOuKeCZNwsAeV/YwdhXNLreyQYn//vTcSxK9UKW/A5cxLDUqzr4Dza87wdAwM+7qU+LeIOPy17UMLeZoBiPuG27+TC1Ho18PdjfKt0Y7r0wKsHOnLgF/7Gg1To51Ctcfz04oICPyj1BQmHLIJbAOIKkgtrXajryYfR56RAvFlWCIMLXtLvniUs84LAQxMewCiceqwZXjXepcYFta+c4nKatI2rUsjcJdbsSVa5JFSZQZH8U//0AgLIddV0lfEbXrULAh+6WIOiCd8H7zMbsYVQJ92obeaO9Z9esFtNFQi5sZFMyCEiZW0QidQG3NJti6NJLl3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bd06d7e0-29d5-4772-3aeb-08dc1e9ac838 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:31.3413 (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: 8rOULesmujCSppJG7N8dCDgM9k5El0AvFfBK2jfCD7YQrw5MS+LXZkBixesyHE9c X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101552_187829_1F64069A X-CRM114-Status: GOOD ( 21.54 ) 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 Make a new op that receives the device and the mm_struct that the SVA domain should be created for. Unlike domain_alloc_paging() the dev argument is never NULL here. This allows drivers to fully initialize the SVA domain and allocate the mmu_notifier during allocation. It allows the notifier lifetime to follow the lifetime of the iommu_domain. Since we have only one call site, upgrade the new op to return ERR_PTR instead of NULL. Change SMMUv3 to use the new op. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 13 ++++++++----- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 +++++- drivers/iommu/iommu-sva.c | 4 ++-- drivers/iommu/iommu.c | 12 +++++++++--- include/linux/iommu.h | 3 +++ 6 files changed, 28 insertions(+), 12 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 1163c7d70359f4..163e9acd51b204 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 @@ -654,7 +654,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { - kfree(domain); + kfree(to_smmu_domain(domain)); } static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { @@ -662,17 +662,20 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; -struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned type) +struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, + struct mm_struct *mm) { + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; - if (type != IOMMU_DOMAIN_SVA) - return ERR_PTR(-EOPNOTSUPP); - smmu_domain = arm_smmu_domain_alloc(); if (IS_ERR(smmu_domain)) return ERR_CAST(smmu_domain); + + smmu_domain->domain.type = IOMMU_DOMAIN_SVA; smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; + smmu_domain->smmu = smmu; return &smmu_domain->domain; } 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 961000f308b9bd..849365a5ff550d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3301,8 +3301,8 @@ static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_sva_domain_alloc, .domain_alloc_paging = arm_smmu_domain_alloc_paging, + .domain_alloc_sva = arm_smmu_sva_domain_alloc, .probe_device = arm_smmu_probe_device, .release_device = arm_smmu_release_device, .device_group = arm_smmu_device_group, 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 cb08b9305e0208..66a6a44f4895e9 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -793,7 +793,8 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master); int arm_smmu_master_disable_sva(struct arm_smmu_master *master); bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); -struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned int type); +struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, + struct mm_struct *mm); void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ @@ -839,5 +840,8 @@ static inline void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, ioasid_t id) { } + +#define arm_smmu_sva_domain_alloc NULL + #endif /* CONFIG_ARM_SMMU_V3_SVA */ #endif /* _ARM_SMMU_V3_H */ diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index c3fc9201d0be97..446ce5a0cae868 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -99,8 +99,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm /* Allocate a new domain and set it on device pasid. */ domain = iommu_sva_domain_alloc(dev, mm); - if (!domain) { - ret = -ENOMEM; + if (IS_ERR(domain)) { + ret = PTR_ERR(domain); goto out_free_handle; } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 68e648b5576706..bc96090cb477dc 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3621,9 +3621,15 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; - domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); - if (!domain) - return NULL; + if (ops->domain_alloc_sva) { + domain = ops->domain_alloc_sva(dev, mm); + if (IS_ERR(domain)) + return domain; + } else { + domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); + if (!domain) + return ERR_PTR(-ENOMEM); + } domain->type = IOMMU_DOMAIN_SVA; mmgrab(mm); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 1ea2a820e1eb03..be5b51fc3af4cc 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -419,6 +419,7 @@ static inline int __iommu_copy_struct_from_user_array( * Upon failure, ERR_PTR must be returned. * @domain_alloc_paging: Allocate an iommu_domain that can be used for * UNMANAGED, DMA, and DMA_FQ domain types. + * @domain_alloc_sva: Allocate an iommu_domain for Shared Virtual Addressing. * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling * @probe_finalize: Do final setup work after the device is added to an IOMMU @@ -459,6 +460,8 @@ struct iommu_ops { struct device *dev, u32 flags, struct iommu_domain *parent, const struct iommu_user_data *user_data); struct iommu_domain *(*domain_alloc_paging)(struct device *dev); + struct iommu_domain *(*domain_alloc_sva)(struct device *dev, + struct mm_struct *mm); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev); From patchwork Fri Jan 26 18:15:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533108 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 30BC9C47DDB for ; Fri, 26 Jan 2024 18:16:30 +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=kFgNrd1D29Z+R6T/Tu/YkZkP0dHWyxkjPK2+MHWCPbk=; b=4zZtDf4nWnrmeB fbOrfGOPoVoGaiNkgwcCGukjAatdXDXjgnUkyLaWRPCi4BQvNas3ureq6+sGN+z6q3+ZvBqu/2p6i /tY2CRAIaGwvQ3RX/JNR0oiEeSU6+AK3se1wd8u/3u2tvW1BujDDGjVIJ7zNh9ccLdU39GINGDVLK 2wxVYvbYbp7HbKYjTHqkQ9Ddm5xLjREDAZfmpJ00ptFKWXklGf3Z7TO/4AmxagtfrbNj4gVfeRC/b zOdu+NqVt16cOqdaMaEZFZ0BeomJC+BbMIhRA/pc1f8OV6D5/pW90fSUw02CIgslWZZLNw81Pc+Wc 3Xf5J5IPFblyU06R/vmQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkU-000000051ut-0bwG; Fri, 26 Jan 2024 18:16:18 +0000 Received: from mail-dm6nam10on20605.outbound.protection.outlook.com ([2a01:111:f400:7e88::605] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQjy-000000051Vs-3Ujz for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZHWHyPtH/sB/IxKGrtxCjH04fO7j7ov95bn/VCUYxxENhPTqFh8ztZe57Mp8qIOO5tV34ENZUhKs+LnUFd9m9GMUmhos9TE9hCi6zU98y3LGH56CuuRSxrEFeNOL78r5e6AvRkYNZxVlDFr4Jb9qYwUggN+BXSzq7pG+xBS74Qm0BtcpdBIhMibduIT+GieVp3kr4Jj/C8CtOeXwWTBLtVlTg9GRS4b4hLRQ68hhCFzoVQKw8B4q6sqVz2szqY/2A+XveqqTVKpoqYo75D3GS8vNMXNF67nW7hPZQ7QueMkurtzwv5CRW8L8VkvHLjfSIoIoi+RC8DgOm+UDjoPHOg== 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=VikqWDayINgkNvq46QvfwkTtQZDljYcLG4ml/NN+I0I=; b=EuARnW87ICan+hHJOnTvIGjwJBnZRs3Icpk8rTg3PrTLY4bra8Ugvpa7m6ml9bhxyhCtcDbTyFQ1tA5F544//fddVZ3IUbgrUJwp+/2brDMFDEkpirIj+vrfjJSPwBuhHXiUn2BdguVymFqLs2aXunUwIEedQT+o0CjuE3X+0P9dVzWl8C/H5fCT5SRIKCpgMK2LK1A60P3T4DM9mnRWiN38Vx1oGhWQEa6nRnzktLQgjNnNNV4Dg0VSXMuRSNKrz89qoH5+R9iuyisKZ03o8Rkty6sJI9C9128sH3hrcW4fAgkJ5R+FHY6So6E7d9+S3TLDcqakq/b1Sg5XFgyUhw== 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=VikqWDayINgkNvq46QvfwkTtQZDljYcLG4ml/NN+I0I=; b=nD7+0LKLajYdDnyJfkxXabt8TIBoaz3PhILcMGkiQqJaw54NWIXeAls5idkuWadCNsbDI68zfxBxtSllvPjYkAyImYR2mH2nIPoKjTPGOn5oww5BXFiQ7GXNxF3nut4LYf/DoMofYvGioLjfnGFClF0ex46h4mvXbOkOldkvcryw4PVwQxgnsoRe3ot2uBSATxJJX0YJopNigr++EJK1D/GHfREfz8xsxjJQcsdBlJcux1fvUXPe965wx6VM5o12CXj/fDKocsefWCxfvLmbkScUN7podTf5hVOuKIwNnKDTR3/Q+GPzD2YJxHYOA+IKE84tfiFfFMopFYRPcQb8yA== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:35 +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:34 +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 21/27] iommu/arm-smmu-v3: Put the SVA mmu notifier in the smmu_domain Date: Fri, 26 Jan 2024 14:15:23 -0400 Message-ID: <21-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: SA9PR13CA0147.namprd13.prod.outlook.com (2603:10b6:806:27::32) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: fe70f325-453d-414d-3698-08dc1e9ac866 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GfNgrdZk6ueIt2UFZJKqueTwNv5OElRUkuNBuOmvNQJ1xy1b5nEN8/s4GzkxThGyHGUR0fISBXHBU9ptLwUWffsnMjUrd1U7cIuQTlDmcXTtns7z0R8PfSRRgP+lNiAE6ufPtT4DdPCKdea+6KehlBj6Py9rQjyrgDr/MghVr6bIG1TNRdkX1GwWwjrcC6wlnNdKICBcq8rpKsjVvkobyMbiEIX1uAS/BhGor4z3SLp+VhcpiNiiZsn1DfBOjvA7zwGl++1lOPNB79RGBYmK1+yN9oon5a+NzTlp/mVZCjfjySUAHIQd3UfYdae6ZfjP3nI0efBOyWySigLv3ys/u3PatLqmhjXpKTPIq/eDeoldyyoLOrGUwaiEp+gr37/NjFnlXwq9HnRYFEgonAUkqDTWsY965wrZOntSXZPhGyjyEFMePnb+WwFiLdkFfaG9fsvFBUwfrQ+MFmo7ull5MYD7O/GrlXZM/BeB/rtQBY/ErBtkhf8GDFJl1hzKXl+y8yu0suk9afjeUVKfF6d0V5J0SWX6FUzeQXgtH7SKkEb8U01Gd+U87yJCb3E8YAMT 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(30864003)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BFOOMM8pjvflNO1sJDRiz3xesYlKhwypxXsPvW8KFaRoL49YDvC4unQ9DwQ6PsoVPXnzLHe/VCRL9cGHPPvPUlhyHgYwDwMszftOcw8YNombeHsvs6famyaSbWwC13D2QQoFk1z7K2m1u+9qIRDx+4BnFp9NC2Y1j2LZgUCsx+/nuJ45ACKO6gtNH8eNSTUkNz9eEuNxZpJtDcD4CT+JUtdXFuzFagqknWPu9n1OwZc8V07ux0Hl1C7yim3gKImP8JQdJLGcejULesQ57z25zBIoZOO9KPpS2N61buW67UTQtl/TDiqpIt5tGZRS5gMtoILKGxnoBqTEFKDp1cTTlIdo3DxyGZlnDzBg0nqU/n6a8DQsVrYvQ/9m6x6I/z7HOGVzmuBfUen+vRug4NBgSpFeS1xxzSPHF8fArqWse+HsBTkpmKkB/KWMMQvKRORHpJTueDFSTheR+H/h4QnI12U+paZZRZnPvRCcdl9CiJbU/+NpHQL7bF9MzsYfgcBgXcR2WUmxoQuoEOPraDK/VaLMOt1gGODN+3z4iI2dr23htInLzQ5U4JOAuEki8UBGkQ1F9TUDXO+LeEVlY5vHVUKFa6wz1GqF5GsueeyAGeDmzmCT990svBRSq884A0qN7WlTzT9dC7e8zHOp6k6qozriYOApw5/Cug8EnS4LgYmkX4+2ZOiWOJP5h4Ca+jxeDM30RuDNDOTGX+0tZa5azjbt7UdSWgRK5qdp7+Gy3BlQeQsw/7qyZZXPSMNncr3cA+Vsq07dmTyvlSZNvu2A7ExGlXRA4RGrFIAup3FEl/QPWblig57S5ZRrWMAmWskKRWheeqel8EVP+ax3tskEa8gGGs5LA4+IY/mh0IyrR5GYpr/7Qf1drLtyRJCeQxsN2HYauQyN2mDBhOVMAYjRXBp7UptQRvTqRkvzTimfXhAVpRYDZNXskPvegPdJH0Fj4N3vMnsbeeMKd8WxiOMR48bLlfnn04G/rtnyCEUrHBMrSW7hworyeu8aIqlUyJDvjzA/EQdGgySpCAuhP4m82Nvcj9F6GrDiIFwbPYTKs3Vzhbygz41sEYOx+DrPyKaHBPx7/xfrwqtPytBCucN3lK5XNxFK4nawUrV0T71CeOeYMX30sqHCvBh70JTSUtgkJuBxpzuV5vVAa5Ma433fNjyPaNVylpQLboVM6qSRpR7K4mv/f0l3Q0pFMok7DNUKOLsjvr1m0F+UujHaqUBJh+/V7w4eFTf3jqiAoW0u6U6lH2MCnvaY6R+dK6Ihu7NkGf7cgJn79e3Pvangwx7B7Gynn5NhkOURRypK8tOPvbfgRfH1jci7sVXlEExXsx+WvzXW5Mj44mjDUuz+LGqWmwd14kMajU0TKWa32T/29KGfstbk6xSS96JeGtNnkJuDvRUHrS7WSHAotGluggvu8yGIcW0AiGDFaVbE4eFjhgyiFEb6ev6Lr+oq6Amd1dltyTaO96kbEk3fjDNqz15FHjivUZvG+J7/bXuets+BAr2IX1cx43JlCNqbRqTJJSh8GTnWFVNDPCyjMfvxB1Ds1fgC+s+/Du+hD9jg7ctTK3HKxHPIB+sorl7q329ohYFE X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe70f325-453d-414d-3698-08dc1e9ac866 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:31.8291 (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: ijC8aY3WyT4++5OYd+5+dUTP+d8pIiykIfAHnxGHOlbmlTRCNeCVeyy5PJdohUnH X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101547_037238_9C63523A X-CRM114-Status: GOOD ( 21.04 ) 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 This removes all the notifier de-duplication logic in the driver and relies on the core code to de-duplicate and allocate only one SVA domain per mm per smmu instance. This naturally gives a 1:1 relationship between SVA domain and mmu notifier. Remove all of the previous mmu_notifier, bond, shared cd, and cd refcount logic entirely. For the purpose of organizing patches lightly remove BTM support. The next patches will add it back in. BTM is a performance optimization so this is bisection friendly functionally invisible change. Note that the current code never even enables ARM_SMMU_FEAT_BTM so none of this code is ever even used. The bond/shared_cd/btm/asid allocator are tightly wound together and changing them all at once would make this patch too big. The core issue is that having a single SVA domain per-smmu instance conflicts with the design of having a global ASID table that BTM currently needs, as we would end up having to assign multiple SVA domains to the same ASID. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 389 ++++-------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 81 +--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 +- 3 files changed, 101 insertions(+), 383 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 163e9acd51b204..3b35e61cf1bb49 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 @@ -13,29 +13,9 @@ #include "../../iommu-sva.h" #include "../../io-pgtable-arm.h" -struct arm_smmu_mmu_notifier { - struct mmu_notifier mn; - struct arm_smmu_ctx_desc *cd; - bool cleared; - refcount_t refs; - struct list_head list; - struct arm_smmu_domain *domain; -}; - -#define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn) - -struct arm_smmu_bond { - struct mm_struct *mm; - struct arm_smmu_mmu_notifier *smmu_mn; - struct list_head list; -}; - -#define sva_to_bond(handle) \ - container_of(handle, struct arm_smmu_bond, sva) - static DEFINE_MUTEX(sva_lock); -static void +static void __maybe_unused arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) { struct arm_smmu_master_domain *master_domain; @@ -58,58 +38,6 @@ arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) 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. - */ -static struct arm_smmu_ctx_desc * -arm_smmu_share_asid(struct mm_struct *mm, u16 asid) -{ - int ret; - u32 new_asid; - struct arm_smmu_ctx_desc *cd; - struct arm_smmu_device *smmu; - struct arm_smmu_domain *smmu_domain; - - cd = xa_load(&arm_smmu_asid_xa, asid); - if (!cd) - return NULL; - - if (cd->mm) { - if (WARN_ON(cd->mm != mm)) - return ERR_PTR(-EINVAL); - /* All devices bound to this mm use the same cd struct. */ - refcount_inc(&cd->refs); - return cd; - } - - smmu_domain = container_of(cd, struct arm_smmu_domain, cd); - smmu = smmu_domain->smmu; - - ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd, - XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); - if (ret) - return ERR_PTR(-ENOSPC); - /* - * Race with unmap: TLB invalidations will start targeting the new ASID, - * which isn't assigned yet. We'll do an invalidate-all on the old ASID - * later, so it doesn't matter. - */ - cd->asid = new_asid; - /* - * Update ASID and invalidate CD in all associated masters. There will - * be some overlap between use of both ASIDs, until we invalidate the - * TLB. - */ - arm_smmu_update_s1_domain_cd_entry(smmu_domain); - - /* Invalidate TLB entries previously associated with that context */ - arm_smmu_tlb_inv_asid(smmu, asid); - - xa_erase(&arm_smmu_asid_xa, asid); - return NULL; -} - static u64 page_size_to_cd(void) { static_assert(PAGE_SIZE == SZ_4K || PAGE_SIZE == SZ_16K || @@ -123,7 +51,8 @@ static u64 page_size_to_cd(void) static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, struct arm_smmu_master *master, - struct mm_struct *mm, u16 asid) + struct mm_struct *mm, u16 asid, + bool btm_invalidation) { u64 par; @@ -144,7 +73,7 @@ static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, (master->stall_enabled ? CTXDESC_CD_0_S : 0) | CTXDESC_CD_0_R | CTXDESC_CD_0_A | - CTXDESC_CD_0_ASET | + (btm_invalidation ? 0 : CTXDESC_CD_0_ASET) | FIELD_PREP(CTXDESC_CD_0_ASID, asid)); /* @@ -186,69 +115,6 @@ static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, 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; - struct arm_smmu_ctx_desc *cd; - struct arm_smmu_ctx_desc *ret = NULL; - - /* Don't free the mm until we release the ASID */ - mmgrab(mm); - - asid = arm64_mm_context_get(mm); - if (!asid) { - err = -ESRCH; - goto out_drop_mm; - } - - cd = kzalloc(sizeof(*cd), GFP_KERNEL); - if (!cd) { - err = -ENOMEM; - goto out_put_context; - } - - refcount_set(&cd->refs, 1); - - mutex_lock(&arm_smmu_asid_lock); - ret = arm_smmu_share_asid(mm, asid); - if (ret) { - mutex_unlock(&arm_smmu_asid_lock); - goto out_free_cd; - } - - err = xa_insert(&arm_smmu_asid_xa, asid, cd, GFP_KERNEL); - mutex_unlock(&arm_smmu_asid_lock); - - if (err) - goto out_free_asid; - - cd->asid = asid; - cd->mm = mm; - - return cd; - -out_free_asid: - arm_smmu_free_asid(cd); -out_free_cd: - kfree(cd); -out_put_context: - arm64_mm_context_put(mm); -out_drop_mm: - mmdrop(mm); - return err < 0 ? ERR_PTR(err) : ret; -} - -static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd) -{ - if (arm_smmu_free_asid(cd)) { - /* Unpin ASID */ - arm64_mm_context_put(cd->mm); - mmdrop(cd->mm); - kfree(cd); - } -} - /* * Cloned from the MAX_TLBI_OPS in arch/arm64/include/asm/tlbflush.h, this * is used as a threshold to replace per-page TLBI commands to issue in the @@ -263,8 +129,8 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, unsigned long start, unsigned long end) { - struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn); - struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_domain *smmu_domain = + container_of(mn, struct arm_smmu_domain, mmu_notifier); size_t size; /* @@ -281,34 +147,27 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, size = 0; } - if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM)) { + if (!smmu_domain->btm_invalidation) { if (!size) arm_smmu_tlb_inv_asid(smmu_domain->smmu, - smmu_mn->cd->asid); + smmu_domain->cd.asid); else arm_smmu_tlb_inv_range_asid(start, size, - smmu_mn->cd->asid, + smmu_domain->cd.asid, PAGE_SIZE, false, smmu_domain); } - arm_smmu_atc_inv_domain_sva(smmu_domain, mm_get_enqcmd_pasid(mm), start, - size); + arm_smmu_atc_inv_domain(smmu_domain, start, size); } 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_domain *smmu_domain = + container_of(mn, struct arm_smmu_domain, mmu_notifier); struct arm_smmu_master_domain *master_domain; unsigned long flags; - mutex_lock(&sva_lock); - if (smmu_mn->cleared) { - mutex_unlock(&sva_lock); - return; - } - /* * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events, * but disable translation. @@ -320,25 +179,27 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) struct arm_smmu_cd target; struct arm_smmu_cd *cdptr; - cdptr = arm_smmu_get_cd_ptr(master, mm_get_enqcmd_pasid(mm)); + cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid); 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, + arm_smmu_make_sva_cd(&target, master, NULL, + smmu_domain->cd.asid, + smmu_domain->btm_invalidation); + arm_smmu_write_cd_entry(master, master_domain->ssid, 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_sva(smmu_domain, mm_get_enqcmd_pasid(mm), 0, 0); - - smmu_mn->cleared = true; - mutex_unlock(&sva_lock); + arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); + arm_smmu_atc_inv_domain(smmu_domain, 0, 0); } static void arm_smmu_mmu_notifier_free(struct mmu_notifier *mn) { - kfree(mn_to_smmu(mn)); + struct arm_smmu_domain *smmu_domain = + container_of(mn, struct arm_smmu_domain, mmu_notifier); + + kfree(smmu_domain); } static const struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = { @@ -347,113 +208,6 @@ static const struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = { .free_notifier = arm_smmu_mmu_notifier_free, }; -/* Allocate or get existing MMU notifier for this {domain, mm} pair */ -static struct arm_smmu_mmu_notifier * -arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, - struct mm_struct *mm) -{ - int ret; - struct arm_smmu_ctx_desc *cd; - struct arm_smmu_mmu_notifier *smmu_mn; - - list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) { - if (smmu_mn->mn.mm == mm) { - refcount_inc(&smmu_mn->refs); - return smmu_mn; - } - } - - cd = arm_smmu_alloc_shared_cd(mm); - if (IS_ERR(cd)) - return ERR_CAST(cd); - - smmu_mn = kzalloc(sizeof(*smmu_mn), GFP_KERNEL); - if (!smmu_mn) { - ret = -ENOMEM; - goto err_free_cd; - } - - refcount_set(&smmu_mn->refs, 1); - smmu_mn->cd = cd; - smmu_mn->domain = smmu_domain; - smmu_mn->mn.ops = &arm_smmu_mmu_notifier_ops; - - ret = mmu_notifier_register(&smmu_mn->mn, mm); - if (ret) { - kfree(smmu_mn); - goto err_free_cd; - } - - list_add(&smmu_mn->list, &smmu_domain->mmu_notifiers); - return smmu_mn; - -err_free_cd: - arm_smmu_free_shared_cd(cd); - return ERR_PTR(ret); -} - -static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) -{ - struct mm_struct *mm = smmu_mn->mn.mm; - struct arm_smmu_ctx_desc *cd = smmu_mn->cd; - struct arm_smmu_domain *smmu_domain = smmu_mn->domain; - - if (!refcount_dec_and_test(&smmu_mn->refs)) - return; - - list_del(&smmu_mn->list); - - /* - * If we went through clear(), we've already invalidated, and no - * new TLB entry can have been formed. - */ - if (!smmu_mn->cleared) { - arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid); - arm_smmu_atc_inv_domain_sva(smmu_domain, - mm_get_enqcmd_pasid(mm), 0, 0); - } - - /* Frees smmu_mn */ - mmu_notifier_put(&smmu_mn->mn); - arm_smmu_free_shared_cd(cd); -} - -static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, - struct arm_smmu_cd *target) -{ - int ret; - 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); - struct arm_smmu_domain *smmu_domain; - - if (!(domain->type & __IOMMU_DOMAIN_PAGING)) - return -ENODEV; - smmu_domain = to_smmu_domain(iommu_get_domain_for_dev(dev)); - if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) - return -ENODEV; - - bond = kzalloc(sizeof(*bond), GFP_KERNEL); - if (!bond) - return -ENOMEM; - - bond->mm = mm; - - bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm); - if (IS_ERR(bond->smmu_mn)) { - ret = PTR_ERR(bond->smmu_mn); - goto err_free_bond; - } - - list_add(&bond->list, &master->bonds); - arm_smmu_make_sva_cd(target, master, mm, bond->smmu_mn->cd->asid); - return 0; - -err_free_bond: - kfree(bond); - return ret; -} - bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) { unsigned long reg, fld; @@ -581,11 +335,6 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master) int arm_smmu_master_disable_sva(struct arm_smmu_master *master) { mutex_lock(&sva_lock); - if (!list_empty(&master->bonds)) { - dev_err(master->dev, "cannot disable SVA, device is bound\n"); - mutex_unlock(&sva_lock); - return -EBUSY; - } arm_smmu_master_sva_disable_iopf(master); master->sva_enabled = false; mutex_unlock(&sva_lock); @@ -602,59 +351,54 @@ void arm_smmu_sva_notifier_synchronize(void) mmu_notifier_synchronize(); } -void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t id) -{ - struct mm_struct *mm = domain->mm; - struct arm_smmu_bond *bond = NULL, *t; - struct arm_smmu_master *master = dev_iommu_priv_get(dev); - - arm_smmu_remove_pasid(master, to_smmu_domain(domain), id); - - mutex_lock(&sva_lock); - list_for_each_entry(t, &master->bonds, list) { - if (t->mm == mm) { - bond = t; - break; - } - } - - if (!WARN_ON(!bond)) { - list_del(&bond->list); - arm_smmu_mmu_notifier_put(bond->smmu_mn); - kfree(bond); - } - mutex_unlock(&sva_lock); -} - static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id) { + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_master *master = dev_iommu_priv_get(dev); - int ret = 0; - struct mm_struct *mm = domain->mm; struct arm_smmu_cd target; + int ret; - if (mm_get_enqcmd_pasid(mm) != id || !master->cd_table.used_sid) + /* Prevent arm_smmu_mm_release from being called while we are attaching */ + if (!mmget_not_zero(domain->mm)) return -EINVAL; - if (!arm_smmu_get_cd_ptr(master, id)) - return -ENOMEM; + /* + * This does not need the arm_smmu_asid_lock because SVA domains never + * get reassigned + */ + arm_smmu_make_sva_cd(&target, master, smmu_domain->domain.mm, + smmu_domain->cd.asid, + smmu_domain->btm_invalidation); - mutex_lock(&sva_lock); - ret = __arm_smmu_sva_bind(dev, mm, &target); - mutex_unlock(&sva_lock); - if (ret) - return ret; + ret = arm_smmu_set_pasid(master, to_smmu_domain(domain), id, &target); - /* This cannot fail since we preallocated the cdptr */ - arm_smmu_set_pasid(master, to_smmu_domain(domain), id, &target); - return 0; + mmput(domain->mm); + return ret; } static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { - kfree(to_smmu_domain(domain)); + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + + /* + * Ensure the ASID is empty in the iommu cache before allowing reuse. + */ + arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); + + /* + * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can + * still be called/running at this point. We allow the ASID to be + * reused, and if there is a race then it just suffers harmless + * unnecessary invalidation. + */ + xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + + /* + * Actual free is defered to the SRCU callback + * arm_smmu_mmu_notifier_free() + */ + mmu_notifier_put(&smmu_domain->mmu_notifier); } static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { @@ -668,6 +412,8 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; + u32 asid; + int ret; smmu_domain = arm_smmu_domain_alloc(); if (IS_ERR(smmu_domain)) @@ -677,5 +423,22 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; smmu_domain->smmu = smmu; + ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); + if (ret) + goto err_free; + + smmu_domain->cd.asid = asid; + smmu_domain->mmu_notifier.ops = &arm_smmu_mmu_notifier_ops; + ret = mmu_notifier_register(&smmu_domain->mmu_notifier, mm); + if (ret) + goto err_asid; + return &smmu_domain->domain; + +err_asid: + xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); +err_free: + kfree(smmu_domain); + return ERR_PTR(ret); } 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 849365a5ff550d..58b4dcb6f56f99 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1396,22 +1396,6 @@ static void arm_smmu_free_cd_tables(struct arm_smmu_master *master) cd_table->cdtab = NULL; } -bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) -{ - bool free; - struct arm_smmu_ctx_desc *old_cd; - - if (!cd->asid) - return false; - - free = refcount_dec_and_test(&cd->refs); - if (free) { - old_cd = xa_erase(&arm_smmu_asid_xa, cd->asid); - WARN_ON(old_cd != cd); - } - return free; -} - /* Stream table manipulation functions */ static void arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc) @@ -2031,8 +2015,8 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master, return arm_smmu_cmdq_batch_submit(master->smmu, &cmds); } -static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, - ioasid_t ssid, unsigned long iova, size_t size) +int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size) { struct arm_smmu_master_domain *master_domain; int i; @@ -2070,15 +2054,7 @@ static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, if (!master->ats_enabled) continue; - /* - * Non-zero ssid means SVA is co-opting the S1 domain to issue - * invalidations for SVA PASIDs. - */ - if (ssid != IOMMU_NO_PASID) - arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd); - else - arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, - &cmd); + arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, &cmd); for (i = 0; i < master->num_streams; i++) { cmd.atc.sid = master->streams[i].id; @@ -2090,19 +2066,6 @@ static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds); } -static int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, - unsigned long iova, size_t size) -{ - return __arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, - size); -} - -int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, - ioasid_t ssid, unsigned long iova, size_t size) -{ - return __arm_smmu_atc_inv_domain(smmu_domain, ssid, iova, size); -} - /* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context(void *cookie) { @@ -2291,7 +2254,6 @@ struct arm_smmu_domain *arm_smmu_domain_alloc(void) mutex_init(&smmu_domain->init_mutex); INIT_LIST_HEAD(&smmu_domain->devices); spin_lock_init(&smmu_domain->devices_lock); - INIT_LIST_HEAD(&smmu_domain->mmu_notifiers); return smmu_domain; } @@ -2334,7 +2296,7 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { /* Prevent SVA from touching the CD while we're freeing it */ mutex_lock(&arm_smmu_asid_lock); - arm_smmu_free_asid(&smmu_domain->cd); + xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); mutex_unlock(&arm_smmu_asid_lock); } else { struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; @@ -2352,11 +2314,9 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, u32 asid; struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; - refcount_set(&cd->refs, 1); - /* Prevent SVA from modifying the ASID until it is written to the CD */ mutex_lock(&arm_smmu_asid_lock); - ret = xa_alloc(&arm_smmu_asid_xa, &asid, cd, + ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); cd->asid = (u16)asid; mutex_unlock(&arm_smmu_asid_lock); @@ -2808,7 +2768,11 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_cd *cdptr; int ret; - if (!arm_smmu_is_s1_domain(iommu_get_domain_for_dev(master->dev))) + if (smmu_domain->smmu != master->smmu) + return -EINVAL; + + if (!arm_smmu_is_s1_domain(iommu_get_domain_for_dev(master->dev)) || + !master->cd_table.used_sid) return -ENODEV; cdptr = arm_smmu_get_cd_ptr(master, pasid); @@ -2829,9 +2793,18 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, return ret; } -void arm_smmu_remove_pasid(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain, ioasid_t pasid) +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; + + domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); + if (WARN_ON(IS_ERR(domain)) || !domain) + return; + + smmu_domain = to_smmu_domain(domain); + mutex_lock(&arm_smmu_asid_lock); arm_smmu_clear_cd(master, pasid); if (master->ats_enabled) @@ -3105,7 +3078,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->dev = dev; master->smmu = smmu; - INIT_LIST_HEAD(&master->bonds); dev_iommu_priv_set(dev, master); ret = arm_smmu_insert_master(smmu, master); @@ -3286,17 +3258,6 @@ static int arm_smmu_def_domain_type(struct device *dev) return 0; } -static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) -{ - struct iommu_domain *domain; - - domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); - if (WARN_ON(IS_ERR(domain)) || !domain) - return; - - arm_smmu_sva_remove_dev_pasid(domain, dev, pasid); -} - static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_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 66a6a44f4895e9..2bc04802ab01bf 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; - - refcount_t refs; - struct mm_struct *mm; }; struct arm_smmu_l1_ctx_desc { @@ -713,7 +710,6 @@ struct arm_smmu_master { bool stall_enabled; bool sva_enabled; bool iopf_enabled; - struct list_head bonds; unsigned int ssid_bits; }; @@ -742,7 +738,8 @@ struct arm_smmu_domain { struct list_head devices; spinlock_t devices_lock; - struct list_head mmu_notifiers; + struct mmu_notifier mmu_notifier; + bool btm_invalidation; }; struct arm_smmu_master_domain { @@ -781,9 +778,8 @@ 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, struct arm_smmu_domain *smmu_domain); -bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd); -int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, - ioasid_t ssid, unsigned long iova, size_t size); +int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size); #ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported(struct arm_smmu_device *smmu); @@ -795,8 +791,6 @@ bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, struct mm_struct *mm); -void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) { From patchwork Fri Jan 26 18:15:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533110 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 5A3DCC47422 for ; Fri, 26 Jan 2024 18:16:33 +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=qEVEOmJ2CPcs3VIT6GySrVS3fvjq0ZTKDky2imhqDXI=; b=44y+NCQH/3aaGv dlXJgRKfBEPcK2r8LnA1iZabB7EyrpFKJNr/XXys57dyh8xCgRaMoC5Mcqr6u4S/ejLGz6EXiH+XO K6J8zy+JtOc93vi0nQlXleL6e7ohpuAlBTG/2SJ52gjXmRUh2fM431kV1AquuoGjh1eVQ7LIz8v79 8Hf85AnWELX6q9SrlZaHfIErcp5PZdy2xIsl9M7Jv4LGBAIk1Qhc/IRs4ogqBAVEij3ToDMuQ9WhL 22GsdOwRPMJwwR/dM6TPNoVgvZ7pzUENm6L7NuP84wiKULwtXPwiP/JSwcybotf39Z0YmjSVA3/p7 4/0oEc/4qKow30PjSJpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkX-000000051xe-3sZK; Fri, 26 Jan 2024 18:16:21 +0000 Received: from mail-bn1nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2407::601] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk3-000000051Wx-29Il for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ax1yT/XEekVEXOKz8WDkkcEKBaISvoD6e/hqyVxTS1oUZHscDF1zmFxm+pOevf2PUbWi/NH+cdIM/el31pZ8vRcQmWMlLip5NyYHHvyQ2cMUY08de21ufYg4Pyib1zi7ltlzIbSjN7qorwTODp1PRkpiDK9f0y2LUNp+5WFJzNVP3uHNrxTCOnthVq0e0qX1PpnBSrja19Oj9lWqXhMBHIy4jVOzVZQ19sLnYPCNlDHRtjlB2RDBrIl8C1Hon9fy4D2QU28g0RXoIPbwnIpXzYDA8sSBL8L1jbPJ/KQe2UwEJq467V9tpEk+9GtlSnMNIV3EbCSIqfJOxTVuU09P9A== 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=RjwMmjSh6EyZ6674r65qF/Zz8ySPkRFe2Gm9+tR6FxI=; b=dYukgGZbDpGJN+iMOjx+lIcuelwaHD/NTDwZAK02SRky6g+RVxQ+ON/JDeO93AhCkQXOu76chw6QDjZr+1GGedbXcaFI+Zwb7RZonOfSPJtrD3pXcyc3N+0G0YtKE43uXiu33C6xB6Sr7/cXpK4EhCc3iaK6GH4JMM657lqkVtKGl8ALcWmOBn42Kw5ZesgVJPnErCyB8YSO3eXfILl7ePwWykq25KPPhlVvfncE1uKKbxJHpaEeK4fF7YKu5jY7XGIs/cxW/32F38O9XcY74uqzmPDNU7BNaKsDdye64jegeiHofIBldZRoFD88SzJAHwMkF3OChDq/cG/J/aexow== 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=RjwMmjSh6EyZ6674r65qF/Zz8ySPkRFe2Gm9+tR6FxI=; b=SnHrH9bmBbnhCtcsKuiBntsxvI24wRtXeE5JB5VF+CT2sBz1J5i6PgVyeUwaya2tX2/fiPVCCuPNVtrOKqTLSDmT0VWvaeteXvndT9tta4HoDhwXKT7i8iquIOZv4VeOTwzxfRL9Pi/GuNJMjIHVqaWowvElkSCp7W27hwpH6W0EkrsNT9XCpSfF2UWG0uoqswKAXZBAqLn7EEwg9rh+Ig0l082zHUInMomTJ1dKy/0WwphuPV0G+9mUQPv8mT9tnBV4rVuR7CvqGG+C43hGL/48bqSwddWQD7r740ZE6ZWYRQ0qaJkkIvWv4oTARB5PzOjPu4wTRybH0FhkK3vCPA== 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:41 +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:41 +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 22/27] iommu/arm-smmu-v3: Consolidate freeing the ASID/VMID Date: Fri, 26 Jan 2024 14:15:24 -0400 Message-ID: <22-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: SN7PR04CA0232.namprd04.prod.outlook.com (2603:10b6:806:127::27) 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: 4303618e-1c15-44fc-da03-08dc1e9ac972 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WNHlJEQ4asoi3iVlz3sTfr40THuBNOYGiT6W2e8HP6iFh8T4NE0qz30yF7lQ/AKyrrIQYD0nN0W5JT761N5UiIajQ95ngLrd/TpoGSUIiItKkQWupQwrgG8DT00qrOMQGFG2uf+fwxhJnwLv4Vr1Ooxb1jyqAj8JNyYi//aM8WLjmki9k0CZXOnqX/b5GKPlRfAgQoedDkw67O8cqkp2hJ5dJU4L+GqIsbckXK93zBXIDIyObWaY/royf+VZG/MOTMtKerL66cC/ZRWFcCwvXxmupl9UMz5ipUeHOJzaa8OyzroFB2aFT+Kplf5BGPR0iYs+Z34mLbxVtmNM6tfXenSXmkyMJHm8cpu2cNZonsszw0pR7u5vuo7oreQxdh98FLbHM+rqAe0WZPSJKD2DsnuSAu45HjBHHZ7s7I8tBOIxv3mHtZI7X9MGKv16D0eDT6475bqvLal1kBH1e2quAjfYjg79pbhmiSEAxY81GK5X6+sBQB6pLqSXwc5sQKbDwIzJgxdOi8/17mScr5xVvh4wYEE8gZJvcnF3daIBFKtalMak0/lMGEtA0tXOXryu 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0RTKavtN9w1VXBPXAr9xeGsWYOi/DAtNnVDBqBh9pPSJb9rKLjNkxdCUv696tpJ/07vorT1oomDERmtnHE4tv9fBLYxNn/SI2V1OJBW0GaNyv8Hl6CX4IPOZG0thoJtdEWqUcxLKwyCmQCfkchl+5OY5F/F0HPqHZSQUSbqAkkC19ZFyQ0MTsMIbw0nCpmQ5C4vts90+/LbJ2MG7bSEb9/jpJcbSywEbxuM6/OcGzJraC11oIfb5XvQ94pV9CEBLsImKZPGrxEeFOdJT6eRmvFRS2EgTgMxvQiaRy3GzQNx/ZX2UCufrpFmC29Wmpr4H959wtENDtqCG4RiwHdWm8wDuj53D69j9BDDjle5XENI86CEtpx48rGAaUGZF+0oqfirIxTBoXfekh4CqTL1E7is7tkMbI2OR/LpGDOwoum2iy/7EuBC+cZTzcI6FQUAjZIjNPHFLUqA5Az3/dUW9q+uzPSE15l2YjrErNwbWdvr4t1g+k2yQqWrKYXWwQLSMtJNGZKb7ezUJM4pPexolGrLzn5J85NYxfDBPK+Kj/DA6kw3TV9M/2aLtwLQRJGuWkmuR620wWoXILjUwF4s2gHtkZ7xXiOX8BbKHm6Rx74t177rDYq2q2kMtU2AkLfONeKo7wXfvtMFM71j33YuQsVKCzu6VWT5Ik2ZmPSNzcfy5FcdXJvf4+sSCUFuBV0rpBjcmJ2SsN5HlAJZrGolMqTcHR8wrcp1SbyyWQCZD0cdkwFyeHnoVNBYdhFjRmfX7w/O5+NG0W/Sc2BBfmeHilHnZD1VlruHns9Hjjfgi6Pb/2WM+2IU6VfVAT/c25AINOYXcGQnVd8iGS2JZ5UaOz8kAJyNIctbDnfFUKQrXcoI4FC2QauVD6Xjj/+b6grfkkhshY8be0KsNuiQaKrZ+qbIFIILnMN5lPKV/zKsjVywwWpLw73fx9JgxwvTyKlf3K6FyB0bma7vyZgumKO1tREUf56HTzmIvyjOsCx187fo2FHFppvRG62iJt/lPYVuyxdCHI27svo31SHlvvXa1VWGPdpso2IQtsHNysV9jbjF+3jXZbVfHI2jqRWV92Igp+ZNYh1LPJlApZ8K9ssaxhmp2YlAEL+qfTzhQ/2FCQb7ypHY34u7O4N5wlQfBlkgJ8+v6ov0hvGuNP6//8EvLaAWZCkqvfa2kTgEKjAC+hdengMqdKT+kY61xChr4+t33+X6z1/MWxo/OAA++0qpeK3ODyoXQSCFh7/1iEIzrJ+bB1tLgYO3UwzNqoJf3C3dV+Y3p7Z9g2XX4c0T6pkx1STIhKSb/xnLn8sSKFDWbX9ji8he8EMTQkBMxxm6EhvNgOoC1M5ZIJMpJLKFuaYtx6pX59n6oS2TdLXbkM0n3N7C45WDOIhOEtJdrY/6sAjh/HnNX/yTAimHFwJldUSgI7G9mu1hu38B1ndUmKo4uV3xae2V6EUECKpwdEKD2Gz4hch2fCeCQINZ+ymbufJtUbKLVhUZbGYtsQISfp4bCxrZYewOt88Vgv4VKGisQeeATf7vsDVHNh/Egjjn6+SK82Pj5yirKHIDAefD7Dr+JSQijGGnx99QYRX+fJjfzU0Hy X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4303618e-1c15-44fc-da03-08dc1e9ac972 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:33.4082 (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: ndKjpC2nctDV0hSqYZqbIFylOR+DCTOX7UKXKGYz1XhD8aF5lsJFtx2rm3DlHKYT X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101551_777146_4146BF0B X-CRM114-Status: GOOD ( 20.46 ) 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 The SMMUv3 IOTLB is tagged with a VMID/ASID cache tag. Any time the underlying translation is changed these need to be invalidated. At boot time the IOTLB starts out empty and all cache tags are available for allocation. When a tag is taken out of the allocator the code assumes the IOTLB doesn't reference it, and immediately programs it into a STE/CD. If the cache is referencing the tag then it will have stale data and IOMMU will become incoherent. Thus, whenever an ASID/VMID is freed back to the allocator we need to know that the IOTLB doesn't have any references to it. The SVA code correctly had an invalidation here, but the paging code does not. Consolidate freeing the VMID/ASID to one place and consistently flush both ID types before returning to their allocators. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 9 ++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 36 +++++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + 3 files changed, 29 insertions(+), 17 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 3b35e61cf1bb49..46b4b7626a7c2d 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 @@ -381,18 +381,13 @@ static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - /* - * Ensure the ASID is empty in the iommu cache before allowing reuse. - */ - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); - /* * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can * still be called/running at this point. We allow the ASID to be * reused, and if there is a race then it just suffers harmless * unnecessary invalidation. */ - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + arm_smmu_domain_free_id(smmu_domain); /* * Actual free is defered to the SRCU callback @@ -437,7 +432,7 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, return &smmu_domain->domain; err_asid: - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + arm_smmu_domain_free_id(smmu_domain); err_free: kfree(smmu_domain); return ERR_PTR(ret); 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 58b4dcb6f56f99..fc7824c8decb50 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2285,25 +2285,41 @@ static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) return &smmu_domain->domain; } -static void arm_smmu_domain_free(struct iommu_domain *domain) +/* + * Return the domain's ASID or VMID back to the allocator. All IDs in the + * allocator do not have an IOTLB entries referencing them. + */ +void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain) { - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_device *smmu = smmu_domain->smmu; - free_io_pgtable_ops(smmu_domain->pgtbl_ops); + if ((smmu_domain->stage == ARM_SMMU_DOMAIN_S1 || + smmu_domain->domain.type == IOMMU_DOMAIN_SVA) && + smmu_domain->cd.asid) { + arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); - /* Free the ASID or VMID */ - if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { /* Prevent SVA from touching the CD while we're freeing it */ mutex_lock(&arm_smmu_asid_lock); xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); mutex_unlock(&arm_smmu_asid_lock); - } else { - struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; - if (cfg->vmid) - ida_free(&smmu->vmid_map, cfg->vmid); - } + } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 && + smmu_domain->s2_cfg.vmid) { + struct arm_smmu_cmdq_ent cmd = { + .opcode = CMDQ_OP_TLBI_S12_VMALL, + .tlbi.vmid = smmu_domain->s2_cfg.vmid + }; + arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); + ida_free(&smmu->vmid_map, smmu_domain->s2_cfg.vmid); + } +} + +static void arm_smmu_domain_free(struct iommu_domain *domain) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + + free_io_pgtable_ops(smmu_domain->pgtbl_ops); + arm_smmu_domain_free_id(smmu_domain); kfree(smmu_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 2bc04802ab01bf..4e8806862ebeeb 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -774,6 +774,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid); +void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain); 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 Fri Jan 26 18:15:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533104 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 83EE1C47422 for ; Fri, 26 Jan 2024 18:16:04 +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=d9lEaYxKDsFv0zamBlLd1VVeq9dmLPHZkHDGotW2RCw=; b=4USYTLAcQ73kBY E5nN3OFb3ObKa+/Cy5BNqsCc2pw2bwp3ZMmK0e/WixMf4Ov48bBu/wgxuGFjpHh5r0PqcoVO9KQeY 4zTjZy7wWl8BgDL1i3CZ1cGrVFPbz8hzEESDkQj/+PhZgXXnb7Aht/yTxK+BbTjua4wrQgWUqGL6n UPHQPqJBGFZPTfEu3EIyjk7ap1nqyc2al/ka3PCI62cT01gFLWciLgGER+7R9w7qDRAUifIDqxcQ0 JvDUogoViP5fz/dGhDiWQj3ntn3IGsQq9tNJZKG+vFm5n5HJriW9zfTHqqmbysAyuslnkGEWYhDJ/ jslhHJ9tYaoAW3xaWssQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk2-000000051bF-3V2B; Fri, 26 Jan 2024 18:15:50 +0000 Received: from mail-bn7nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2009::601] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQjx-000000051UQ-199L for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:15:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EKlXhVsXKgM83O0AhSlSHQ2SBsrx7N//PozAB0aeIVzy9H0CkLotR1Z9BZszlTaoQmLx9zwmQtLq5RZIBNXL0rWkk5XNeT9WN815+IHpFX8Hr2xupcE9Xfiq53XKXQBpl+I2r8TVyFHl/yHhx3SlW9AUuYDlTz71oHw7cCamI92GxYiXy3cgSZIVpOLfWwnpHtsbZ4QPZpjwTB/tzfRaz+StKe6ls5x4fMC2ucSzM3htdH1Wd5D8Y2bwLuTAp39UPwKy7B+YTHMP8vGZQBRn046T3UzUX74YFitPQtIsXWpWjBA/Vw0XYFhH00kPT4OVsIOyILllahpWUdXIOsdUHQ== 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=o9BTvMUZH5hSgv/bpXNVHGWCN+MUXvdNLoc8gNQOuRY=; b=Eww4s+3zkF83sfJhpqGu7REx7INBo+GyHzuh0ThjdGPey7iQRFGxyYZUztXjD3Kxpt9J8mt5NI79+3wqJMG0G9LoF4pBKXISBbzN56QocqLBhsaivZs2UYYqJvW7xUk7t/V9R48yjIa8pD/NjdY/akPBjnGMs2Buubc1KcBNJ1yNSNLtenV5TTGoOfTfeaVXPBbzXweMiztF+XkPPIVn4uF4EfdZFCt34moGd/QZa12RG3rhuAKLQO3cB0G8e7RsMay5CyT1fmiV3m2wKCOC5uyjY364LQUQs5Ww5dqHE8XcDCtx5BctH058tawMcFeKRr9ps6RvBMGhIGw6weKceg== 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=o9BTvMUZH5hSgv/bpXNVHGWCN+MUXvdNLoc8gNQOuRY=; b=T3fWv1lmbqn1OSgC84oTHR1rqj29XiRNja5/iuFK0sxjK1LwlIjgOfOLwC5AaxLglDsfVFN8iWdXsFOzB5J6PZlbWEobFyau9jvoOIX9hT9uMaVMVH6w5MhdYOZ5zhEmT/CW4BPFmduSfGUZNujNnEJnxuNVf+NfYcqocSxEKiLBtXCpHVvr6LuzW0DYVEdzTPDtsMGGur4G1RPeRTXA+KRFc+NmJA3XH1ZD08Ljt4nh6jagGVCUJ2r9Z77NLUww7beNrHATY3GRfaATZP7VVYEsErwD255I0veErH1+T2Qj3jXywYCazqQgpCU8askS0PmUiExzMqBmoxVWeQvFTg== 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 PH7PR12MB7185.namprd12.prod.outlook.com (2603:10b6:510:201::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.29; Fri, 26 Jan 2024 18:15:36 +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:36 +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 23/27] iommu/arm-smmu-v3: Move the arm_smmu_asid_xa to per-smmu like vmid Date: Fri, 26 Jan 2024 14:15:25 -0400 Message-ID: <23-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: SN7PR18CA0018.namprd18.prod.outlook.com (2603:10b6:806:f3::10) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB7185:EE_ X-MS-Office365-Filtering-Correlation-Id: 855a518a-02b1-47cc-eba3-08dc1e9ac875 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z3/zntsMh1i/MWtqwx5iQh04k7XlazYH+gMi5fgKIulhV/t6uKsLcki+cFGC9JwQBwUm221hTkthq4wfey955n032HEpdMDhg9YLXf4wh18zh576x1w6wjUtQIRnzHBywiBpFFKCYEGgUkLSYuQ9PPqIgxcrqFnbhOtiSVMiUOm8EBUEmrZhWrs/Shsb+5UryYCiG12vbLzwwVF2UQh3EtSZmMmipRHrk5Dco56qSVjjecSYxDk5csop+u8fGL3I70pdkmVPLwExv0Ks6YzgbiM8vh59+pR3tjuO/QjmwzflnReKewLfaaL4f4AxZmOqZwFlycuxZLRh48rtx8KX9FAnhDCqleDo9ZMvyX5zm2No3lZ7lBhE/BBs9rSN2EjyoQKEX85ubRm+9S6RLgkkAy4PQBodKU5yYoCzKsTn9KHqahZkDB2SPFwpRTPAhCROE6TevwYsuLfdPB7WN0KPUlGcVjkEHlZq86KcQMdJTxZB51f+nKNBnmwrT/jMT906WEfxqJ7czbPRgWQWZpYEUhGkW7M3il+lbqW6IcHqXKlKzOiruiL8HLlsSzX7LBDw 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)(346002)(396003)(39860400002)(376002)(136003)(366004)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(8676002)(5660300002)(66946007)(316002)(66476007)(478600001)(110136005)(54906003)(26005)(6486002)(6512007)(6666004)(2906002)(8936002)(7416002)(2616005)(66556008)(4326008)(6506007)(36756003)(38100700002)(83380400001)(41300700001)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tgCrSyTT5EG/Fux3Kf+4bwSgGY/fdoQsvmYsZkN6MdrTv5CHzINwu7pig1GswwEv1UvJJLXeAI1SVMW0GjeMfDdr1SfIg/EGcJEWiDTd2Rm2y7Y4edLLNjEk8iqICtIT5MNRmjAbarRcNY3HCququXIGqbHpwSE63m+CVNX4bsBCr3mGdhqi1mIMOqur7w0xmtgMRQW4+xb2FqPSeTHg47mh89qXSbaPPcqR3tXTSPty5pU1ywpcaP50xR+OoXANFoe19MOfTUiTnskIGclyVFzhpJ4YW1LfiyOM4vX1mM7s1A8dSWC0CULcacssdznpwmSuLsPyVKrWSQpnEjvPjXAyICdzxmBqioxACvnuaWdHpLwi80JHchuhggQFMGBOCpzZMwX1j4rweqEUYycdqYscZdMx8+zBH1kPfAk+YsvW9FZ+iJXAS1i8DiIhdtyCErY98oC6R5QyjG/B3H5w8qL5xW8a1GaDpL+pT30XQnZ67cc5La6W9iirkVAPWLZkiE+zH6dGLBPigglSPy2ygTmvEtdlbw61Bg7ro9EBKV6aQCEVQEPaWmtZcMSM/GHu1++mq2CytPaslqJeNKvu5iGwq39lipnnP5Exr4R0/SQGMCZHdK8cRaghgLMMZnwPhfA/eF7nLyVJ5VaZAcYiuwtngV7saAMPwyhJft5j8DlW82P9SsOFDmeUpQcEJUw1TA1uz7wZ0PU9C8dnKH0Oa7koAc0yTyXCqV04Q4bjE5T/gh0AYDYXWzD7zA9yvi4tuxhw/gZMzFvBeAsafb+ml3F6NwYmFysKPIgDQEvUHPa4ZVnar48ITRN4850iwJCbMpNcYhqwIlNMiq2ldff1j+3zCot+XUXzz8WPhpzJVB2uuE3d5lfILUKsjjW+HbKSlAcku3RCcj0EvUnEzZux0sCYZyCqEVOwDkhtQYOU+BPHlnD9LCYqeCZ/mWeJIoUZmG6OOFVKwpnuwzSvX/9JHaG/aqGzgMaKOXPpdMRJRYx3GHfem5TgrkVA776snyYicvyE3FEcbjePioj62ThfrYjMKd0N5wgGf/70Vr6os5sIhs+e+kgc+xkSUdR4LjZWkCsI7zEOjUWOjiIkTS4BnfvxHxlf6Kh+QM9ztWjJkrmk++aNat77BkYuEpFd2TnA+VVHuSnPsVWNxLvsHkyt+P4v6ERBAiLUcmk2MSfTsJtgsw5uzZcDlOVmfGjIirKdVcLDCzhgWlBEPrMpt4c3w2UXI+mpYJMeQ4Ey5dbTfzVgPFrgfXlpJ7CFj4LThbcSVzNukRcMyuEuLY5i1R4z+t/CaOXAlbtpdd/sDGv7v3SQTNBuwDv6wS8Ujwt5wkUuPA37OILoJsr1EmuSw965wpuVbNZIsm0GA3J/YjVkvcIKZVicrwOMQF1Sz/En8rVtd3nX+BLDqox3bxU8PYbwzzApzbvPWhv09eEKOPE9F+HH6+5Zb85QZnClYVC04JmSmgrCrAjTL6cE07EjhJlIl/ow51D3hm9KxCS7hkSVoWvUwUkrOS3DKhjbQLRGgczluUPaeZvPb7mS5VL1wsqVkeA2LPz2Eg98/LMt+g7WzfMxSst0q+wQIZhC9onaII2s X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 855a518a-02b1-47cc-eba3-08dc1e9ac875 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:31.8789 (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: DzphO6KTqVkotlEhJtSjgqSLvfDoBm18YMPN1iwntTdt+/1xNV+jERqPlzcLIHsc X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7185 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101545_429105_EB56D5BA X-CRM114-Status: GOOD ( 18.68 ) 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 The SVA BTM and shared cd code was the only thing keeping this as a global array. Now that is out of the way we can move it to per-smmu. 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 | 39 +++++++++---------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +-- 3 files changed, 22 insertions(+), 24 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 46b4b7626a7c2d..080b84a3b78ce4 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 @@ -418,7 +418,7 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; smmu_domain->smmu = smmu; - ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, + ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); if (ret) goto err_free; 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 fc7824c8decb50..be95f29e80df6a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -89,9 +89,6 @@ struct arm_smmu_option_prop { const char *prop; }; -DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa); -DEFINE_MUTEX(arm_smmu_asid_lock); - 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"}, @@ -2299,9 +2296,9 @@ void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain) arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); /* Prevent SVA from touching the CD while we're freeing it */ - mutex_lock(&arm_smmu_asid_lock); - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); - mutex_unlock(&arm_smmu_asid_lock); + mutex_lock(&smmu->asid_lock); + xa_erase(&smmu->asid_map, smmu_domain->cd.asid); + mutex_unlock(&smmu->asid_lock); } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 && smmu_domain->s2_cfg.vmid) { struct arm_smmu_cmdq_ent cmd = { @@ -2331,11 +2328,11 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; /* Prevent SVA from modifying the ASID until it is written to the CD */ - mutex_lock(&arm_smmu_asid_lock); - ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, + mutex_lock(&smmu->asid_lock); + ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); cd->asid = (u16)asid; - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&smmu->asid_lock); return ret; } @@ -2622,7 +2619,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, * arm_smmu_master_domain contents otherwise it could randomly write one * or the other to the CD. */ - lockdep_assert_held(&arm_smmu_asid_lock); + lockdep_assert_held(&master->smmu->asid_lock); state->want_ats = !state->disable_ats && arm_smmu_ats_supported(master); @@ -2674,7 +2671,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, static void arm_smmu_attach_commit(struct arm_smmu_master *master, struct attach_state *state) { - lockdep_assert_held(&arm_smmu_asid_lock); + lockdep_assert_held(&master->smmu->asid_lock); if (state->want_ats && !master->ats_enabled) { arm_smmu_enable_ats(master); @@ -2736,11 +2733,11 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) * This allows the STE and the smmu_domain->devices list to * be inconsistent during this routine. */ - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&smmu->asid_lock); ret = arm_smmu_attach_prepare(master, domain, &state); if (ret) { - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&smmu->asid_lock); return ret; } @@ -2765,7 +2762,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } arm_smmu_attach_commit(master, &state); - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&smmu->asid_lock); return 0; } @@ -2795,7 +2792,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, if (!cdptr) return -ENOMEM; - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&master->smmu->asid_lock); ret = arm_smmu_attach_prepare(master, &smmu_domain->domain, &state); if (ret) goto out_unlock; @@ -2805,7 +2802,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, arm_smmu_attach_commit(master, &state); out_unlock: - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&master->smmu->asid_lock); return ret; } @@ -2821,12 +2818,12 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) smmu_domain = to_smmu_domain(domain); - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&master->smmu->asid_lock); arm_smmu_clear_cd(master, pasid); if (master->ats_enabled) arm_smmu_atc_inv_master(master, pasid); arm_smmu_remove_master_domain(master, &smmu_domain->domain, pasid); - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&master->smmu->asid_lock); } static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, @@ -2842,7 +2839,7 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, * Do not allow any ASID to be changed while are working on the STE, * otherwise we could miss invalidations. */ - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&master->smmu->asid_lock); /* * The SMMU does not support enabling ATS with bypass/abort. When the @@ -2855,7 +2852,7 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, arm_smmu_attach_prepare(master, domain, &state); arm_smmu_install_ste_for_dev(master, ste); arm_smmu_attach_commit(master, &state); - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&master->smmu->asid_lock); /* * This has to be done after removing the master from the @@ -3509,6 +3506,8 @@ static int arm_smmu_init_strtab(struct arm_smmu_device *smmu) smmu->strtab_cfg.strtab_base = reg; ida_init(&smmu->vmid_map); + xa_init_flags(&smmu->asid_map, XA_FLAGS_ALLOC1); + mutex_init(&smmu->asid_lock); 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 4e8806862ebeeb..e8aa78fc0b8a0e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -675,6 +675,8 @@ struct arm_smmu_device { #define ARM_SMMU_MAX_ASIDS (1 << 16) unsigned int asid_bits; + struct xarray asid_map; + struct mutex asid_lock; #define ARM_SMMU_MAX_VMIDS (1 << 16) unsigned int vmid_bits; @@ -753,9 +755,6 @@ static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) return container_of(dom, struct arm_smmu_domain, domain); } -extern struct xarray arm_smmu_asid_xa; -extern struct mutex arm_smmu_asid_lock; - struct arm_smmu_domain *arm_smmu_domain_alloc(void); void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); From patchwork Fri Jan 26 18:15:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533250 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 ABAD6C47422 for ; Fri, 26 Jan 2024 19:20:23 +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=AzY67Jk17qXV7FTI04/yB2aXxGt3f/37/JUXrbMJwXg=; b=TTnHPlAUF1A5hJ OvPltCJbDeNDy8e+GNfFdA4WCgFMkepGaetFbRVh0HE587d8OpZssU/a5ifAV4iz2ZWHADVksyrrZ OQs+FSWCIoEgGigFMfzUJDL//A7XjCJvCFB+z6aOptFPnWN9r2GCXTVRm2P8Hu+waWpsOk4X9qNTj biv/2I0//TP+iwfc8Hjc+IoLtWvYVOHJUkR6XJybpMICA5rS05A3bh5PK4DnIpK4e5Xhc2EQRSx00 q3NP7zAh5qgU26tt/ibjmSmtfw3I9obXNtkdFizE1x+ljC85J6yid5Dun2+mFA/2z55OPUlDKrYC5 TMZGVV5gH/EdMEATIcBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkH-00000005C64-3J7H; Fri, 26 Jan 2024 19:20:09 +0000 Received: from mail-bn1nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2407::601] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQkH-000000051Wx-3j1o 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=GviknjVz4N/UqKGG3GIclxaXF9Fn4W5sHbdf/AwP1EWzdS/iVm7tlZXSs1xG+0ydIGfziBsEBgTtLivSwCjFwW7VIaGOdL7uHNqxfadHiExzDAP5dXmr2fdAjQ3CvHroSg9OwlQy4UKu1jTiNr54G5IuReQSM3ivhoC5bSu/x1eqDp0RjjUDPq7EAvqZt7eFl+i32FAUzuUdviCyqrY4aE8EM+Map1/Ec7sMRshnT40QwA74nWKQ+zwOoIDtjSGIbTiB5jK+Hi/6pKne1v1nHwqcgl1JZpStnnb63MJY0O2Jwwcib8VU45t+Txbq3ttwie4DLnNm7B18sXE0ukvdIw== 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=jIr/tl2WpEb5tV/5ORenYHK8ctS8SE+NHCt2ai6LxKM=; b=INbn17FI8HVHjtihT6fi6kcY+tdDMq46X4TpIB2+hHyhQ/IDyupSNuv6m9ZMv+ZZHc/gFStM5blKg36FyTqBERca5ahCRv4GVzgoez7M7gSsI1W0ixryklg9wfcQl9skANUTcRAqhQjcOfe9olXoUmF40I6EuyTvpHszOlmdkN/GV9shzPjpqAycM1MQz0uRFTPNHYQ5uBek/bXQ8MAYqSct6mJdiQhwcQ49ILUix8PhI8tVDL/4ig++TNAuZ/xdb2wWjKzdAC+VvGQ7JvynOH9dlqeY6XM6LbAV6og/nNo7Oc6OMx24RyUBhmg6ccFkVMtdcD8udfNmv0I0Ut1LDA== 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=jIr/tl2WpEb5tV/5ORenYHK8ctS8SE+NHCt2ai6LxKM=; b=swHzlekXwQ1WwsnD8umRtLjp5Kgif2bfA8R6eF/HyK6R0x5Vgg6wQEVWraSjsAnm0xM5nvJq6+nBo0b9t5KrXakcUrs2MbItTF2qrMzSqZW8pqCD1/VdDyYBoM2lQYvunPblIgqq/D3vayASTaoj1kYh8EGA9blT8HfweKoscxTM56+Y2qQ1yShmZHxrppZFVD9klQivEAydfLq+Dn9w2E0BoAv9mjsPPPOBXNzzrJTh+Zck802zs+5zY62OuAapsNgwMQw37ly/zvxshYAee87cmV4JDZuXxwF5UC9lWlhlrWphiF3l4Y6ZXTsfElZEn0zACsmVLg9w5kZbTZpifw== 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:43 +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:43 +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 24/27] iommu/arm-smmu-v3: Bring back SVA BTM support Date: Fri, 26 Jan 2024 14:15:26 -0400 Message-ID: <24-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: SA9PR11CA0019.namprd11.prod.outlook.com (2603:10b6:806:6e::24) 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: d05c028d-37e9-4cce-cec3-08dc1e9aca43 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cVE47faRPXLtHtM7ha0l88B6ytGPnlo30EOj8uVpUb2GMdBBvFS/Y/AsUKh3ex9NIgSGKNTG28dOvtmHz2gkEcVeeZLdPKsxc5ve9m6Hh3UsYTsRwp6b582mO4jcZBcSXYHS/j3e+leLH4QnvdU6iWVqK0MhoYnlX2MK2TTHuHwDiUqI8VGHdufLXgWJcTQk1IGrMx09zDKNPkMGF4YDqtxvsy0gj5DvYlisLRfjlot25qSYgophurvjw3Sy+Iiy5rVh0LTm2C2ALMECsueQVyAsoAT8dKms0Abyu6Xwcf1w7TdENwVtn3Qe9VFpL5uRTxKArheO35EPjoIyuFe5zrPrI6EpnT6tD051nQ4Ukqk5/LBud6Jq7yc616b9e5ZexYdNYd2Db3dnmQwUFOcNuvZ2F+cbnV08E5aKePD6aPjHdgTad2LHslpLfv0ctSjR5rP7Y74DcFqfMp9/CAaNJSEbRXlf/gK8wBoVHaAUfk/C9SSkvmrFRHrM6xYnDSJnSi5h7w0GtT1P1G2TGhQKQSRzb1uESidhRYbMDehU7R4+FnBzC90h8IEuPlSpoJlF5rHNejdIZPvFuVg0DhNGxSzSXBZJX757WogAsqiJ1vzNd2w7Eeiwj+zo1YMMv8xt 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)(230273577357003)(230173577357003)(186009)(64100799003)(451199024)(1800799012)(83380400001)(36756003)(86362001)(8936002)(8676002)(4326008)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(30864003)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Ul5D8lo7DDhMZpL95mgOKnNjsImXBWvlasz8EX9NLmm0VwxqEBceFuKcXD7jTQDqaLro6XKIUSUr/lkJcJ4QKJjhXvTua7Q/Z1Q53XksUTUOhiDpGr/sxcfyNLGfbYEfZ98tgv8/ICU1WFX4o+UoeTjxthGOx0BUeKJ0ZcXSoPK0Te50Azba8hvxIlDNAWL+ZtEwDaNC/Q9WJPTZNqeaaQLyCJvb7KGYfs3JcQLYLfd19pHwkLaGkxcYBMSt/cFVY0aUyoUgppcOz12FARQqZHnpyGBnXhV/sCGMHKTAA54ndegSPrbIf5yDxYfjQKWeeGbuuaWxhGksS6+fAHZpIN2hwVXezHjg/o7cq7r88+kWyQ584PsB/Rix34wVUgFSATf3Vq73sB87AIdyqog72+w6Ch7ReuSoud7jxU7LEKkSe6FLuA5CGVC2P6m2Skbd1cv2Vbu5+zMXFQdIxxt/U2Zv7IDYsU6DCMxBVAzEsE0uGUZlaYmDeBXsc+p3FdwU13HBbiJpDbaUFpt/nRqeHZXQSBsejtYpHJZdwvFvacPfBaM/wgq4MFJTV6qaCCwjN84AgWx3U7i6QMXnkmKeLM36wDtJV3NSTj8WvLFGhslbEsdBgfQB3+pa9ECRASh18BoZD2/2VxyuQy/N4sELCNMjimbEtfFluT6rRvf+ZldMyjmpdVSE4H+4OVDOqXDcdJtsfsH2byzS/wZPzr3mITzOXN6yEgawEJj4/DztSMRCNyulVEco8cyrkIM7pfJEuzUkxgkRUlAB6IbkbI0CYjCBAC6jPRAjCmTHlnOAU1oryxykyeby9Z9Gj32PWP/JpRWVoA7plyOHy/til7cLMKGO+FzwFUKUanFOIRGXlHB/dLG06qP7mg9L0rgtvWoFxgPk2OOEyVwHQCSQC89HViwKDkkIttcSvSr4LzRsidNJF3DFSi/iLViFi+By4ErzAIwjsSBw9F/ibo+nYJfqKN7GaetYqcu1AxTktJjAcCtqNkjnNSQlSNo50dnHDcGfWYjD7OTHU8uKbDmDwAy0f8aJsK4z6MRLdb6cVA7t8n4cC6WP5x2oziuLAweXRHElamd6QrTbuq06DjacRBIq5c5XvZt6Ov0fMC4/SoIc6ljeDELvCO2Y03a5utjfqfmzH/r60b0RnnhY+Ayoj2gl71R8eOEkGKMIFw+VYDuSqHqQvzgX/SiTUHEPn1N+GyclTVs1lD+ewH/nQCDTwwvO0wugiMdBeTQtNaH+9IUD91WQLFuPaw0O7JN6mUAY3OvYmBd45Ez2UYp+l9/fb5heSRO9lgkIXU8Z3TgotvLWFl10a0Ix1GNDfuX1QCb7McZ1vBUKAyWI0ZmnirykYhLbO/4CaZRai2KI4hHdUGSy8Q1wopkF7AoK8iPYXoDcstEIWVTC9ijzZrvLYdlrIgWWjAeC3sSMDWff4Id9sH3Z8w6DY9j6RUpp5KIU+UtbIpk+D4p+fiNVcbZLJ1QCiBLhZEsXaULwzHA+QE/q8hPyQ0k9wXAG2jfB6Ee5z9Bl9fddtcRZXi+Sd29ZzBXZ2C6tniFNgDqXeVobIRKoReQca6J4CpCUvg6VG9RImau5MzhK X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d05c028d-37e9-4cce-cec3-08dc1e9aca43 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.8684 (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: SHpN3uGLhEVV5F2nVIhrYdtoY7TcGm7IZxIquK9ggy2xyNjpXBOOFr2JBuwU2a7x X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101606_021661_C1A5DE53 X-CRM114-Status: GOOD ( 29.85 ) 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 BTM support is a feature where the CPU TLB invalidation can be forwarded to the IOMMU and also invalidate the IOTLB. For this to work the CPU and IOMMU ASID must be the same. Retain the prior SVA design here of keeping the ASID allocator for the IOMMU private to SMMU and force SVA domains to set an ASID that matches the CPU ASID. This requires changing the ASID assigned to a S1 domain if it happens to be overlapping with the required CPU ASID. We hold on to the CPU ASID so long as the SVA iommu_domain exists, so SVA domain conflict is not possible. With the asid per-smmu we no longer have a problem that two per-smmu iommu_domain's would need to share a CPU ASID entry in the IOMMU's xarray. Use the same ASID move algorithm for the S1 domains as before with some streamlining around how the xarray is being used. Do not synchronize the ASID's if BTM mode is not supported. Just leave BTM features off everywhere. Audit all the places that touch cd->asid and think carefully about how the locking works with the change to the cd->asid by the move algorithm. Use xarray internal locking during xa_alloc() instead of double locking. Add a note that concurrent S1 invalidation doesn't fully work. Note that this is all still dead code, ARM_SMMU_FEAT_BTM is never set. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 133 ++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 15 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 3 files changed, 129 insertions(+), 21 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 080b84a3b78ce4..12c7290e3fc995 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 @@ -15,12 +15,33 @@ static DEFINE_MUTEX(sva_lock); -static void __maybe_unused -arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) +static int arm_smmu_realloc_s1_domain_asid(struct arm_smmu_device *smmu, + struct arm_smmu_domain *smmu_domain) { struct arm_smmu_master_domain *master_domain; + u32 old_asid = smmu_domain->cd.asid; struct arm_smmu_cd target_cd; unsigned long flags; + int ret; + + lockdep_assert_held(&smmu->asid_lock); + + /* + * FIXME: The unmap and invalidation path doesn't take any locks but + * this is not fully safe. Since updating the CD tables is not atomic + * there is always a hole where invalidating only one ASID of two active + * ASIDs during unmap will cause the IOTLB to become stale. + * + * This approach is to hopefully shift the racing CPUs to the new ASID + * before we start programming the HW. This increases the chance that + * racing IOPTE changes will pick up an invalidation for the new ASID + * and we achieve eventual consistency. For the brief period where the + * old ASID is still in the CD entries it will become incoherent. + */ + ret = xa_alloc(&smmu->asid_map, &smmu_domain->cd.asid, smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); + if (ret) + return ret; spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { @@ -36,6 +57,10 @@ arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) &target_cd); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + + /* Clean the ASID we are about to assign to a new translation */ + arm_smmu_tlb_inv_asid(smmu, old_asid); + return 0; } static u64 page_size_to_cd(void) @@ -148,12 +173,12 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, } if (!smmu_domain->btm_invalidation) { + ioasid_t asid = READ_ONCE(smmu_domain->cd.asid); + if (!size) - arm_smmu_tlb_inv_asid(smmu_domain->smmu, - smmu_domain->cd.asid); + arm_smmu_tlb_inv_asid(smmu_domain->smmu, asid); else - arm_smmu_tlb_inv_range_asid(start, size, - smmu_domain->cd.asid, + arm_smmu_tlb_inv_range_asid(start, size, asid, PAGE_SIZE, false, smmu_domain); } @@ -182,6 +207,8 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid); if (WARN_ON(!cdptr)) continue; + + /* An SVA ASID never changes, no asid_lock required */ arm_smmu_make_sva_cd(&target, master, NULL, smmu_domain->cd.asid, smmu_domain->btm_invalidation); @@ -388,6 +415,8 @@ static void arm_smmu_sva_domain_free(struct iommu_domain *domain) * unnecessary invalidation. */ arm_smmu_domain_free_id(smmu_domain); + if (smmu_domain->btm_invalidation) + arm64_mm_context_put(domain->mm); /* * Actual free is defered to the SRCU callback @@ -401,13 +430,97 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; +static int arm_smmu_share_asid(struct arm_smmu_device *smmu, + struct arm_smmu_domain *smmu_domain, + struct mm_struct *mm) +{ + struct arm_smmu_domain *old_s1_domain; + int ret; + + /* + * Notice that BTM is never currently enabled, this is all dead code. + * The specification cautions: + * + * Note: Arm expects that SMMU stage 2 address spaces are generally + * shared with their respective PE virtual machine stage 2 + * configuration. If broadcast invalidation is required to be avoided + * for a particular SMMU stage 2 address space, Arm recommends that a + * hypervisor configures the STE with a VMID that is not allocated for + * virtual machine use on the PEs + * + * However, in Linux, both KVM and SMMU think they own the VMID pool. + * Unfortunately the ARM design is problematic for Linux as we do not + * currently share the S2 table with KVM. This creates a situation where + * the S2 needs to have the same VMID as KVM in order to allow the guest + * to use BTM, however we must still invalidate the S2 directly since it + * is a different radix tree. What Linux would like is something like + * ASET for the STE to disable BTM only for the S2. + * + * Arguably in a system with BTM the driver should prefer to use a S1 + * table in all cases execpt when explicitly asked to create a nesting + * parent. Then it should use the VMID of KVM to enable BTM in the + * guest. We cannot optimize away the resulting double invalidation of + * the S2 :( Or we simply ignore BTM entirely as we are doing now. + */ + if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM)) + return xa_alloc(&smmu->asid_map, &smmu_domain->cd.asid, + smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), + GFP_KERNEL); + + /* At this point the caller ensures we have a mmget() */ + smmu_domain->cd.asid = arm64_mm_context_get(mm); + + mutex_lock(&smmu->asid_lock); + old_s1_domain = xa_store(&smmu->asid_map, smmu_domain->cd.asid, + smmu_domain, GFP_KERNEL); + if (xa_err(old_s1_domain)) { + ret = xa_err(old_s1_domain); + goto out_put_asid; + } + + /* + * In BTM mode the CPU ASID and the IOMMU ASID have to be the same. + * Unfortunately we run separate allocators for this and the IOMMU + * ASID can already have been assigned to a S1 domain. SVA domains + * always align to their CPU ASIDs. In this case we change + * the S1 domain's ASID, update the CD entry and flush the caches. + * + * This is a bit tricky, all the places writing to a S1 CD, reading the + * S1 ASID, or doing xa_erase must hold the asid_lock or xa_lock to + * avoid IOTLB incoherence. + */ + if (old_s1_domain) { + if (WARN_ON(old_s1_domain->domain.type == IOMMU_DOMAIN_SVA)) { + ret = -EINVAL; + goto out_restore_s1; + } + ret = arm_smmu_realloc_s1_domain_asid(smmu, old_s1_domain); + if (ret) + goto out_restore_s1; + } + + smmu_domain->btm_invalidation = true; + + ret = 0; + goto out_unlock; + +out_restore_s1: + xa_store(&smmu->asid_map, smmu_domain->cd.asid, old_s1_domain, + GFP_KERNEL); +out_put_asid: + arm64_mm_context_put(mm); +out_unlock: + mutex_unlock(&smmu->asid_lock); + return ret; +} + struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, struct mm_struct *mm) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; - u32 asid; int ret; smmu_domain = arm_smmu_domain_alloc(); @@ -418,12 +531,10 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; smmu_domain->smmu = smmu; - ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, - XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); + ret = arm_smmu_share_asid(smmu, smmu_domain, mm); if (ret) goto err_free; - smmu_domain->cd.asid = asid; smmu_domain->mmu_notifier.ops = &arm_smmu_mmu_notifier_ops; ret = mmu_notifier_register(&smmu_domain->mmu_notifier, mm); if (ret) @@ -433,6 +544,8 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, err_asid: arm_smmu_domain_free_id(smmu_domain); + if (smmu_domain->btm_invalidation) + arm64_mm_context_put(mm); err_free: kfree(smmu_domain); return ERR_PTR(ret); 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 be95f29e80df6a..dd49da9e533b66 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1274,6 +1274,8 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; + lockdep_assert_held(&master->smmu->asid_lock); + memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( @@ -2078,7 +2080,7 @@ static void arm_smmu_tlb_inv_context(void *cookie) * careful, 007. */ if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { - arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); + arm_smmu_tlb_inv_asid(smmu, READ_ONCE(smmu_domain->cd.asid)); } else { cmd.opcode = CMDQ_OP_TLBI_S12_VMALL; cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; @@ -2323,17 +2325,10 @@ 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) { - int ret; - u32 asid; struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; - /* Prevent SVA from modifying the ASID until it is written to the CD */ - mutex_lock(&smmu->asid_lock); - ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, - XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); - cd->asid = (u16)asid; - mutex_unlock(&smmu->asid_lock); - return ret; + return xa_alloc(&smmu->asid_map, &cd->asid, smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); } static int arm_smmu_domain_finalise_s2(struct arm_smmu_device *smmu, 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 e8aa78fc0b8a0e..ed747e25f2367d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -586,7 +586,7 @@ struct arm_smmu_strtab_l1_desc { }; struct arm_smmu_ctx_desc { - u16 asid; + u32 asid; }; struct arm_smmu_l1_ctx_desc { From patchwork Fri Jan 26 18:15:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533244 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 8AB0DC47DDB for ; Fri, 26 Jan 2024 19:20:15 +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=vYbHmJXz5DL01gRWsUo7Km6UQ6Ozzjfi98pJSLCZ1Ag=; b=OnbHj6spZpftwM /DV7xL4Hdii4ClzaAkkAv80xJW/5X5bPhuVwi+qh8EIApF6JXeH1GWqeLLt5Sp2nKq9gH9GzhJBtF cq2SrleLxiFz1DML7CKZUj0lWfzu0o+R162OjbZb+Glx0vLABvSwC0nmiTYz6c4p90FY+NO8CyblI AeAdDOJNrIlrWFmFcPSa9Vr5Kp+xAGitVUXqhcBgR5y3UV0IiA4KUbQ8+p0hyaGYicZZY2EZMSAfE 3bCnHZSV79mggWsBH5vfATRkMH311vncjxnorSerSxCMw9agD5w5DDt6ynafHTpt4KOggY3zzkdLf XST7Q9Vq/yBNCeld4BFw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRkA-00000005C0z-3OY3; Fri, 26 Jan 2024 19:20:02 +0000 Received: from mail-dm6nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2417::600] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk9-000000051Ud-0QbJ for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G3yNxiV8Lea+UBhTp86amjmM3IPeDxnoIxQhXez4Hk/wVfrvEoMaZ6dfgaYcJRJAEjEKgKnkgq2zJ9s/tG0nuIWhrJY/4vZXvbiqPfGMzDCE9+0q2tipGXLynAdQ1D2kPKwiALAR/DHkEHqFiKNNy1hQN84ETxgeCee61OL69LPRMD0W/cXIBl792l4fUxcqngkT7Xn1jkgEaYcTI89p8w1zcf2NF/hxUx4in1M5OebMMN0W09pg4nJTRuSZi4x7RpzkikAKCX/Ogb7UWkHqSAYB9OK/kEqVnDFSgpow61IT9TiYM/1ESYa+LlDJjlsKdUvHKGmddir9vkQ3TMKyGg== 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=nGSVVZtdzkeaP91xLF+QEFLzkWB+BkCZnFImu3/kV3w=; b=XO37/6jy5Bf6M/LYmj0Fyqlml+9KHZ0UaF6xmpn9Z8ZxZZlQ0PVBTt70GVcx+WjaWDLfiRUCUHgPIRXYcT7QvisqFu/G9kKoxHflS3tN/VE9LzNmHSlhcyWAQOnaWy9k5RImHF6VGVRDABpXf8qTdQjwbeYYdMStLlsSzUIESX81LjmgzGJswys2r7W8T5plMbo5t1Ic6dyjQjpPjtaGW1x8M2GHhgzjR5qYvCLvtm8R9BwCE0dAurTCqXo2prDbifriQlvFAuywRqsh+9cMWCuJT/xqKJ5PocRAgmXAbqeufn4PNsOc20NbkMzV2sDPliQR/A8kYWD9Yig+JoSWpQ== 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=nGSVVZtdzkeaP91xLF+QEFLzkWB+BkCZnFImu3/kV3w=; b=HC/GSgKYYdawGR+JBsHQaodooGkHk1UCRXRWCVj8jrpMcWHnpMc8F6+ZUVvmcoRNdMf2nqTrCwSvlvNVpfPqyLvXbfP+D0uAVpdvJXqp3OJSQGKuQy/XUFVEUh9NPzu9nvz49hrEBqCC2qurdVFiPJW0EICoadRFqY4OncxFVqTO7Y0a6eAvNvvnXoO7j9Id1QjI0QpW8rMgJtODdUkVzVSDSs1UUnAJ32kHkD4IVYHj2xLfCalHBFmFftSlC/AwdZiMXhRsPWmCMYfvFxQMEManBd7iM+yxQDUOhcOZt3LxeBUbDl/LqwBhep/aWV/+ruVbmfrBkGvstlcGio6fIg== 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 IA1PR12MB8079.namprd12.prod.outlook.com (2603:10b6:208:3fb::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 18:15:33 +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:33 +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 25/27] iommu/arm-smmu-v3: Allow IDENTITY/BLOCKED to be set while PASID is used Date: Fri, 26 Jan 2024 14:15:27 -0400 Message-ID: <25-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: SN4PR0501CA0103.namprd05.prod.outlook.com (2603:10b6:803:42::20) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8079:EE_ X-MS-Office365-Filtering-Correlation-Id: 4f5f5de3-c8a5-46f4-ecbe-08dc1e9ac847 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kp1sh8UCUVrQhOOJoAtVrVlO+w2/s3ctEBIvwxBCYdNCtZCNKOFyurSLbv7a9y6qX9DKI/vG2XnrgmYfhepB9K8CwvghTp2/c0X2HowkIaPui6IY7cDJ5jwuO/1rXDpdyl2/nTCdhqDKknmZJ5Cysb4ojPzzLKloq5noueTkR6taJHQy2Qti5ouJHvo5J7uXgRbel0exg2ybXLjUvUSq8BDGSql/UkAJUqGTShoDKCYBAupz7p5Y9w5BQXncNeqe8M06nkBgfxMIY4zLzTOpW6cjsxTL99eS+u5Pn6oTyATH30qubuU/tnSt4RPqp/5aui7j08AOZADhJfIl3mZ6flc3nw14kiYATDysbUZD/+UfTbsd9vTvmQjkjbGjyIn5EbvKAOsBPLB9Bmo6/x+nawLBQGkFdYXy131HXvPsyYuCToHqvW0zkFyrpKsycAgA2B+KOjMYM1xpK5qRmfyF6kq3ulMGw9VuMIbkfStgln/anAHv5h36fG/7ruxgY3dUU0gkudwJUVgl2fPhKNCs2Qm6oBPNALUYy+rSgfrTQPERSJClJxdKB6FsUy8GGoIlDSrArxVJjXE4y1N79gfnxbwDIrqMC4YsVEc6Lgwt0pptAJbpQ6+x68bD65LU+60Z 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)(346002)(136003)(39860400002)(376002)(396003)(366004)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(83380400001)(6486002)(6512007)(38100700002)(54906003)(2616005)(5660300002)(15650500001)(8936002)(478600001)(2906002)(8676002)(4326008)(7416002)(110136005)(6666004)(6506007)(316002)(66476007)(66556008)(66946007)(36756003)(26005)(86362001)(41300700001)(414714003)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uLGXw4cRCDtzad4d0inRXFznUJQb85BV+Wfbl4DRf10mmCoUNCJL5mJHcMMjs+gBaMloQiHzNrIcrO4YZtMS2fOjlAltgeWO5UHwug/9u0GoWcC+fcUPy0BGrFYDRleoV2z1hooKPzkUG58nU1Gty3MG5IUWEMn7mvFJkMkVtD19Qqk91zn4GOEBwsQ1zLiDgNxWcbrbVfZIKzfycMHySbLgcbtBJhJnDWzs/3SkLseH3CcBi+vNnsHh+mMKIDEq4dG36OriXZTK+BXuTs6SS1TVAqc4gOlDKVJCgPpsi6PM+JhTWkLihs8rruBQjnGCYxDks4pt+cevRDdRo/NBXbQeaK2T7L9glxeP850YW9k9ZvOLcd/kbiYRguV0xAmE9+h9xyx32SHFpPe7U4BEc97Bu2aMJCpQHvi1d1KNWXi9u+MTwlS3EB2b70dcolh00gXN7C7l1vBAJfjc1m0/D8yCsrLg7MaztTvhXjqKukKqeQvDOOtfX6wxXzY7lBk+bhyYC0l7LQ4VwfE7pwZgvr6ZlKNpPUNxvAyoZf3bBBrBq2OCx8FmhaifcYZWmvcVMwRMcWZgC198gn0E/JDaPfpDusyBlLJeG+dhJ4dfE4NxprnzZRHTqF0xJiLCnR71Euu2fBh/MaHbs9KOnlHVErDToCtjr7EIqcB8pLECWwMQUbYUBUr0l2Sv3XMbcCLYRtjTiW5EmAJOsnr0nJNV7pZ3A6IZ2TfCjH9P9qUZStb5zczb1Kl1U49uuhZZwjJVjntynDntjbNn4piqVd91RxiSIeVUAc+c8Y69ZQaCwdtDbcIzdwVC0LxB6/2bH0Nn4mbt0xWZAlPhX8AFihCmP18SXjd+5nS618HPsE4wyD67zB9BewFRMybYjj2Oepm8gPdhyYTBq4U+EBgF6QLtaf0bMUk15/KwlSBmd/g09iGhnsx6LNfJzGtpAA6O4O7atjt8Fm76r8R5lmED+DFsU2KU7K9/T05NTa6TwyuRBcaXGibGgjQUVeUOtbB/MhYwsieQ9E/ON/jQ5TVPNhmX14Et8YdJVbIWAJhqt7ClSVGC1sOS++YjApS7lFl1l9mc/O2J29zy/FsRqD5f2C5jheDLpcvLnAOyKr93cLidaBntlnJ6sfN7PE943LPjmf3x4PPZRNdsGXHLHVw1mBA8+EBBnxOePrmCnqOhGLSX2qTcYAqSJp536ng+olI0/Y6GT6Q3jKC7+hf6ZAdEDNgDf7v8lSre3PL0bx5DaTdye/RuS0IyHYPgLtynSqXdLERJOu3g3Y5HODR/j3KVdkJpig5fne2ztquOMOqmswWQSWesKtVkehxP5IMwL2ZDzx+oY7r0igQy8txuUwdn0LhPfT5YwVw0WwEv/VGiaEH5GouJS0XySCoieSxc9rnREnDSZ9VvrOzH3FDx8uK0f0BlWeIWdssPYS+PzUr+zATKD0vzjDQKaTNl73Dut1SDq3Yny6gKqOKiapoEHHTmdu3Wxp2AmSBAxvT5tOyixOFGy1hhllIMQLAQpipOnapXqWb1QAjUkKyyRjXwC286u14QjMtSbaTMlrHs5TvgzIVxPTKxojFM3y8KeX09iZXpU76S X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4f5f5de3-c8a5-46f4-ecbe-08dc1e9ac847 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:31.4422 (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: DRS5Ed3yZrEQGkFtnhXuU8j7U7OvHiSLI4ZaWD5PRQDaq7uACSTx1+ZcgF++yw1C X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8079 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101557_313559_8D41194E X-CRM114-Status: GOOD ( 24.88 ) 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 The HW supports this, use the S1DSS bits to configure the behavior of SSID=0 which is the RID's translation. If SSID's are currently being used in the CD table then just update the S1DSS bits in the STE, remove the master_domain and leave ATS alone. For iommufd the driver design has a small problem that all the unused CD table entries are set with V=0 which will generate an event if VFIO userspace tries to use the CD entry. This patch extends this problem to include the RID as well if PASID is being used. For BLOCKED with used PASIDs the F_STREAM_DISABLED (STRTAB_STE_1_S1DSS_TERMINATE) event is generated on untagged traffic and a substream CD table entry with V=0 (removed pasid) will generate C_BAD_CD. Arguably there is no advantage to using S1DSS over the CD entry 0 with V=0. As we don't yet support PASID in iommufd this is a problem to resolve later, possibly by using EPD0 for unused CD table entries instead of V=0, and not using S1DSS for BLOCKED. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 60 ++++++++++++++------- 1 file changed, 41 insertions(+), 19 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 dd49da9e533b66..87f05820a85923 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1537,7 +1537,7 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_ste *target) static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, struct arm_smmu_master *master, struct arm_smmu_ctx_desc_cfg *cd_table, - bool ats_enabled) + bool ats_enabled, unsigned int s1dss) { struct arm_smmu_device *smmu = master->smmu; @@ -1550,7 +1550,7 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, FIELD_PREP(STRTAB_STE_0_S1CDMAX, cd_table->s1cdmax)); target->data[1] = cpu_to_le64( - FIELD_PREP(STRTAB_STE_1_S1DSS, STRTAB_STE_1_S1DSS_SSID0) | + FIELD_PREP(STRTAB_STE_1_S1DSS, s1dss) | FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) | FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) | FIELD_PREP(STRTAB_STE_1_S1CSH, ARM_SMMU_SH_ISH) | @@ -1563,7 +1563,11 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, FIELD_PREP(STRTAB_STE_1_STRW, (smmu->features & ARM_SMMU_FEAT_E2H) ? STRTAB_STE_1_STRW_EL2 : - STRTAB_STE_1_STRW_NSEL1)); + STRTAB_STE_1_STRW_NSEL1) | + FIELD_PREP(STRTAB_STE_1_SHCFG, + s1dss == STRTAB_STE_1_S1DSS_BYPASS ? + STRTAB_STE_1_SHCFG_INCOMING : + 0)); } static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, @@ -2744,7 +2748,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, &target_cd); arm_smmu_make_cdtable_ste(&target, master, &master->cd_table, - state.want_ats); + state.want_ats, + STRTAB_STE_1_S1DSS_SSID0); arm_smmu_install_ste_for_dev(master, &target); break; } @@ -2821,15 +2826,14 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) mutex_unlock(&master->smmu->asid_lock); } -static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, - struct device *dev, struct arm_smmu_ste *ste) +static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, + struct device *dev, + struct arm_smmu_ste *ste, + unsigned int s1dss) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct attach_state state = {.ssid = IOMMU_NO_PASID}; - if (arm_smmu_ssids_in_use(&master->cd_table)) - return -EBUSY; - /* * Do not allow any ASID to be changed while are working on the STE, * otherwise we could miss invalidations. @@ -2837,14 +2841,30 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, mutex_lock(&master->smmu->asid_lock); /* - * The SMMU does not support enabling ATS with bypass/abort. When the - * STE is in bypass (STE.Config[2:0] == 0b100), ATS Translation Requests - * and Translated transactions are denied as though ATS is disabled for - * the stream (STE.EATS == 0b00), causing F_BAD_ATS_TREQ and - * F_TRANSL_FORBIDDEN events (IHI0070Ea 5.2 Stream Table Entry). + * If the CD table is not in use we can use the provided STE, otherwise + * we use a cdtable STE with the provided S1DSS. */ - state.disable_ats = true; - arm_smmu_attach_prepare(master, domain, &state); + if (arm_smmu_ssids_in_use(&master->cd_table)) { + /* + * If a CD table has to be present then we need to run with ATS + * on even though the RID will fail ATS queries with UR. This is + * because we have no idea what the PASID's need. + */ + arm_smmu_attach_prepare(master, domain, &state); + arm_smmu_make_cdtable_ste(ste, master, &master->cd_table, + state.want_ats, s1dss); + } else { + /* + * The SMMU does not support enabling ATS with bypass/abort. + * When the STE is in bypass (STE.Config[2:0] == 0b100), ATS + * Translation Requests and Translated transactions are denied + * as though ATS is disabled for the stream (STE.EATS == 0b00), + * causing F_BAD_ATS_TREQ and F_TRANSL_FORBIDDEN events + * (IHI0070Ea 5.2 Stream Table Entry). + */ + state.disable_ats = true; + arm_smmu_attach_prepare(master, domain, &state); + } arm_smmu_install_ste_for_dev(master, ste); arm_smmu_attach_commit(master, &state); mutex_unlock(&master->smmu->asid_lock); @@ -2855,7 +2875,6 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, * descriptor from arm_smmu_share_asid(). */ arm_smmu_clear_cd(master, IOMMU_NO_PASID); - return 0; } static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, @@ -2864,7 +2883,8 @@ static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, struct arm_smmu_ste ste; arm_smmu_make_bypass_ste(&ste); - return arm_smmu_attach_dev_ste(domain, dev, &ste); + arm_smmu_attach_dev_ste(domain, dev, &ste, STRTAB_STE_1_S1DSS_BYPASS); + return 0; } static const struct iommu_domain_ops arm_smmu_identity_ops = { @@ -2882,7 +2902,9 @@ static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, struct arm_smmu_ste ste; arm_smmu_make_abort_ste(&ste); - return arm_smmu_attach_dev_ste(domain, dev, &ste); + arm_smmu_attach_dev_ste(domain, dev, &ste, + STRTAB_STE_1_S1DSS_TERMINATE); + return 0; } static const struct iommu_domain_ops arm_smmu_blocked_ops = { 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+gh4cS5xvCVonY2Q08souYmspxaNk1Q5MAqVeeu3JpFEnp7/mSW7G5O5cfVb1rwdgMjxazZsuu/Deb5pUXYomVXwIPJuIptSof4VfTk0qw4NzuK8BQjnLNCZji+bP/+GTxLueDc5jXoE8echJEj13z9xrxtmkrThAwfZqXpInWRR0d4yascKNaJePuj922VL+L3J8K/yqJgSajcbyd29BVN+XQQw9miT5VZbpsPA9SK0KG9JnO/YTja5s25KAYbyfZeLxwlhHqAFkA9uuKjkfl+cGSRSHL1twZn49ZRsIdjwWnO/onaMBJUZDger79ec7ZuCt531B8EO5VUX6kYElJvm+dsI/0VC3lxR54vmWMg1ckysu2LtV/nMG3sR6h0JU7rU7tFaEbnQq2ptoWxOEOWN1D2gaU38bwThTubjSF3sHf2Ro8sNF0YAT8sYez15VpboF0dor3y9ZGLhTMj2EL25RdFzYRZU3vQxP0JagzQbY2hhXu6/IwlkCaVhfqQR2k+ijORcmlChc6PHoTRfPcqYqLhonBa0tqhCUI53l1ZHIvMQmwZE0wVSjZom1CBJBI+1WZhLiSftrdLivoi59mQGhF63efYQNojemtu+m+BFAiZlpIqc5/NkZi9t4l1nyUOfFTx2uCw/RAsYkvmLyfVgtVwS/5iw7FtP6Uk+dArnAguRYL8BLC5wrOBhn+fm6IRbMOWcG6LF+rdSMIGgQdF38QcCR6y2KebwSUDolTcXKOULSDGaVmiMnbQzBrFMVOqyxdR3lB7qzMHoxIHkVbqhVtQR4lgynHqkiUWWJ7pjwFIxqQL3Z/pQ6wE87haW3s3jOVWBwC63knG9XRdzqNZIWhdfmZ3vEghG5uSN52eVuciY7DE8gJ8AJ4Pb3eedMzTcHR0GGEYMVZ5RxDG2jsMrC+hMXpmoQtZsOBzttsyQ5AdEXwFE2O5Fga4rFhMt7knw+dDck3ZEgH4jXFGgrZr0iLNotQt+y0w84vIEZcVtxQaMA/yzF9yChLxM5OJNgTsTkE55dSXyJtaYIqPYeQ2opZqTuQGbfm2rxIp/ncCEDajkqDEQDKRK5xtjdv0iM9P7qnPJZOi13fzmMz4DkQgqIVCC8k6vRuwFPoYHECd7tL9ZnPRMSmNbqsNh7TxTaydFOreKsvKTKfJpaUC/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; From patchwork Fri Jan 26 18:15:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13533240 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 42FD2C47DDB for ; Fri, 26 Jan 2024 19:20:13 +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=5CfpzoyYFv5MuHzXulDHfb9q9zDGGXSmA7VMrFH7zFw=; b=ZDojk2Sgtf2Xxd BluB0mn39kpp4AGNdNRvpzvhdNURFtJgAXPORjBgCCK5TcK7hHv0faI6Qk4X93Yt4b6O79/7Hms5x X9Abo7EYT+aF1KWNR3CGeza1h+l0oYC6N/CYcvIO82JPZDytESXElL337tbXF79svG64a2iiUbBTi 5FNC5PXrteFAA6SU3lVYf+Yt6jPMuVHl74nZtlQVTKCMw2rbfEtgqMc4yZMFIFaS0Y5ml61Q6RVoF g0qXXrs88CIEQJ9GUueJJ3qDYHBxGpkHu44OLxGQoV1c35ys1Td2IKmDqPHafVuqT+TdsTzJbFJ+t +WYOqZWEqp6CL2dCDL4g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTRk9-00000005C09-1Uiv; Fri, 26 Jan 2024 19:20:01 +0000 Received: from mail-bn1nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2407::601] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTQk7-000000051Wx-3Tpu for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2024 18:16:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=amHHDWVvMmWHJHSCG7/tax8avXSj5jy/GwR9zouXODjRjXrrVmwjcU9S5dSHFdhCtaT0mv4n5O+1U3H7mISFPIu3svdFBSDQCtTWRCafwz2SSE56KgoJAt37l4ujROeTYsBsN1YBZX4bLQCVUW5ptiNP9L1nYq0H/jfhJp0fcD86Bg8Y9eYM6yyL59qpBWZ5rNEeAEZ3HffDldLLQUVDOahOh40steWSO7JGdL+vkYy9selAg2yub1VsCW0t4RaSFIfIkobLMCNLo/Qa07+XCggHrErdzqadjPtQb4jlIEHEr1AzoLi9KD3ZmoeJVE89/rfkX6Ed1Rg608SM/R9l9g== 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=tV/ouUpLYAZ/XO/6/oMmXJFpdbP1MVdFztgCouOheKQ=; b=HcsZ6QkyTL1fXpCevoAaXEFsunlMVnVii2KIVQptaEnIMC7T+/AU8L/MskxCluYjVia6enhgFBLVzRXJvvzre3Erg6tHjkVDSHBlMKFjWlKYW90a/sufLyNPDpm6uJEgnUZiAwobZc8lDVwUMoLprrN8xCW/fEZ2Z4NSTYfI/4pB3LaHsuDwEZ6Bj2YeCYL4HRB1U2/WlLrKXPwxKMzZHd26ZVI3TONZU/8M6WJkBufSvvp8IV20lOyesY9bqh01VV1ABnz0ElNN+/FYwLDaB3r5VDn0CUIQzR3X2BUIiTiB+fa7ShAY9O1nOr33BZeZcWIZIVUDvrZYwVV39cKh+w== 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=tV/ouUpLYAZ/XO/6/oMmXJFpdbP1MVdFztgCouOheKQ=; b=Fn34FJ2ygWA4GOVF8lmMpZ9QFo4AOQUQhocAsf5TWE+CsEm2CwfT4vgq5Y0/lRx3dVKtZRTBXN4Rzea8lbYgxu6Xe6YWeEeP9UzDRr+Mi9DJxbEE4Q/d/kkTnn0qfcB6DgbqTDhWkp6gyNrngDCrKNaniik48o3fhL7rpSZkvQehsCjHtxlqWXc7wRhklY0Qar9ZSg+UBzQHoMuUtDglW1eMtfWsV8RbTi2nu8aIeSP0J34lvc7ZKdYHNaTesQlbPjMNU/sqD6inZ5kQibkj/EnMCKWR8CPAysSbb/lX2C6cj34hrao7+qHlqBmEnDnO5HNY5JVF0rQiRBtNINV79w== 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:42 +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:42 +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 27/27] iommu/arm-smmu-v3: Allow setting a S1 domain to a PASID Date: Fri, 26 Jan 2024 14:15:29 -0400 Message-ID: <27-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: SA9PR11CA0003.namprd11.prod.outlook.com (2603:10b6:806:6e::8) 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: d1b42e3b-38bf-46c9-0fea-08dc1e9ac9f1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /WUGH010zlYZaKWAC9I6oIIsJcqXEX1Jcc/DN3RbVGzpMfPj/ZdptfSCncyv9FMzjzlv3Z0N8F18Mst4s8iVEepQZCV5Vfg33TEppVntTjPgyeWgPxSSbm6wbt8sC/5GtzaTdwiv64nEZ9jXEYXr3rJqB+0GoVgSvnlywB/RR1IKux3R6NvrhlyvP3NLJjF6+mqmqICptl6t2L94x5hzV1gDBGB6upWP1L32I2BOL5WjGuOR0Fbcq/bx0/kYWz6g0g9ZiWRIzU2FNNiF0E1/Uu5xVL3OLYIvkvq3mIGWlv3CTpN0IY3GFvEA0MVSPg3GnAzc+Ql0nIcthTM8bHHDKaT4ISf3Ue/WSDhb6wjO96m3fEg22HsOJgpEy07VR7a1JpVWq5XmEmbkKImoofaJndlVbXPictJkRI8VYXKOxQRdunFpi+YE02r6I3pN3w77clhd1AfjMosRRaYOS+kJ4YDiKHPbDKyKb9WMugUsPTwtR3kcQC3J2JS/P28UjzGjFVRd1TjFtwg5amQnSNbm11hMnNUvf9utYJeVaHarkUhh01EvOzyCjN9sOjM/Wscg 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)(5660300002)(26005)(2616005)(38100700002)(316002)(66946007)(54906003)(110136005)(66556008)(2906002)(7416002)(41300700001)(6506007)(66476007)(6512007)(6486002)(6666004)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /210hny3+ug2csHF6rrFIsq87HaeUHQSwURngOhCknrX23ivJwR7FYkWIQ1tbrN0BdCrxOlPFurl+aIKyIy35t2sGYDyZM/EYpO5qnv7woUUtWKeBJqE52raQLGWsYjk2UKwPrafdqAZBnVZulEDtoQj8zQAug0pLE36dFbVf/7Mv0yfHMVpzmAmrAfkZGaPX/PX07gYVgLJ/LVjfd+KvSij8BSrfcSonisg2Dc/AAdzCFxEeXFnLXg9sNWER77Jbf6jDhadVeEaxKDfPV7koTGs+jGrFyG9cSJt4lDt8ypmD0w7ekZBl3coTMktdTWMNto9RXGoXSvhqs6QJAqIWo7FZpJL+GcSf7t9lBD5lTWE8jvh4hdFJkPXmGTnyNGaCkCGnPV802ySa/8Y4gr0avF0wVKYiCMTGufnwuZj/Q32JwSlISQ16/Rvibov+9PopngPOPjHUnlNCzUzGCPW8XvzeOR7jO66MhkG4qLZJzmEfgNA8Nal6JqFAzRsXi1L8OHc0F6O3RTjShRELVn/DGLH9CLuDSToBGj4KK/g0rhp66bhGi0mSYq7LsSqUd/QRybTQDGALp8BJTMzTRpu8Kg6GNbPdWFLbNp4NboaeEBEJ2flxPgStchVvh/IZd3agp47mBGUae2j3c9WRvLBReKLladj4s+SxVW1LzYlXGzL90tMPq3iBPQqKXEJ8GHSZhGZ0DOzUOA3YfN5VFVCwXQG+4rnMm9KQdfuHFdqV4JPkvuboLKq9xNt+Jw7Rk9K7FWO9dSXxwYUc2MGN7X3V4TezH4lW5NInfrundpuAGzRseeugIowFZVd0L2PMibpzwuEvhj2EqyAqVUc6MO5nBBLyzclU/XADrq4sQ648W3Vcuknh6pdH5P9EiihCg2VL2CqbQtgOt+Z6NPq28p0Ffo7LNvsfU2T1kUu0PSKSHRDvKAlXsL39/77g5KEkF5Qh2q1c1OHpQo+INkesJta4CxBn3Tnp5/ImOUxrtRreWcXr12aIGRIvK4KjIPefpNMcTxt2fZ1Qi+xhG9dP32Zm1ka+H5VMWC2FrIH8M7AMI27zeaV96JTNhujaPiLkFPKTEq+jl2L1d1k/GMwV1ZsGyc1NZqGzyNevhAZtrK7NBJwOdnDPkPcz/Mrs8Oa5dI8ZCnMt6/XZV0kpyu4h46/Zzr4pnphzelSNq64zTbtz1L3CCfL460G//7ANozfENbxlJvbsxP04o0inRdrEkiUwiYatBIBG7WboxjNNNpmjEauQK4lVzX91zGqoYNvYfO2M1FWfRm+c5lIaHh2FkAPSBhQ0SOWBWRvExZk0e9aRvk1XIZ50tiDGEBbY5a2IGJqnM7Ce9iJrjht4ip5U7foeX7o6cB+4Xbsiweaxg+TZz+iodmDd0Ihuri/5/5AyhLcNasVS0GMoGORBs7nCyAd8okjq2iInG50hL7Di7eeZIHZeV4cFp3UhESPpITwFdwOsfnZ4v10SoPR2Vs294vLfNZofrTkZVCbbOneqC3lVuFCYtMp7IgBO4xzMW6Ha2+Fr0fwdC5tCRrARHUC6M6oppqtacajv0jZFCMBBAN+A8KV4AULbVvbfkIedP/Kl3tT X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d1b42e3b-38bf-46c9-0fea-08dc1e9ac9f1 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.2320 (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: 5BJU59pZ9rL9uaw8GcC4DMpPpUK5VTo9w7n+dqLPQR4CmGv3FAVwZks7j8qHzFsd X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5186 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240126_101556_033067_49FC9F66 X-CRM114-Status: GOOD ( 17.99 ) 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 The SVA cleanup made the SSID logic entirely general so all we need to do is call it with the correct cd table entry for a S1 domain. This is slightly tricky because of the ASID and how the locking works, the simple fix is to just update the ASID once we get the right locks. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 45 +++++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 2 files changed, 42 insertions(+), 5 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 371d7930c36390..97a1b010b10221 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1274,8 +1274,6 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; - lockdep_assert_held(&master->smmu->asid_lock); - memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( @@ -2773,6 +2771,36 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } +static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t id) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_device *smmu = master->smmu; + struct arm_smmu_cd target_cd; + int ret = 0; + + mutex_lock(&smmu_domain->init_mutex); + if (!smmu_domain->smmu) + ret = arm_smmu_domain_finalise(smmu_domain, smmu); + else if (smmu_domain->smmu != smmu) + ret = -EINVAL; + mutex_unlock(&smmu_domain->init_mutex); + if (ret) + return ret; + + if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + return -EINVAL; + + /* + * We can read cd.asid outside the lock because arm_smmu_set_pasid() + * will fix it + */ + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + return arm_smmu_set_pasid(master, to_smmu_domain(domain), id, + &target_cd); +} + static void arm_smmu_update_ste(struct arm_smmu_master *master, struct iommu_domain *sid_domain, bool want_ats) @@ -2801,7 +2829,7 @@ static void arm_smmu_update_ste(struct arm_smmu_master *master, 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 arm_smmu_cd *cd) { struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev); struct attach_state state = {.ssid = pasid}; @@ -2825,6 +2853,14 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, if (ret) goto out_unlock; + /* + * We don't want to obtain to the asid_lock too early, so fix up the + * caller set ASID under the lock in case it changed. + */ + cd->data[0] &= ~cpu_to_le64(CTXDESC_CD_0_ASID); + cd->data[0] |= cpu_to_le64( + FIELD_PREP(CTXDESC_CD_0_ASID, smmu_domain->cd.asid)); + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); arm_smmu_update_ste(master, sid_domain, state.want_ats); @@ -2842,7 +2878,7 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) 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); + domain = iommu_get_domain_for_dev_pasid(dev, pasid, 0); if (WARN_ON(IS_ERR(domain)) || !domain) return; @@ -3349,6 +3385,7 @@ static struct iommu_ops arm_smmu_ops = { .owner = THIS_MODULE, .default_domain_ops = &(const struct iommu_domain_ops) { .attach_dev = arm_smmu_attach_dev, + .set_dev_pasid = arm_smmu_s1_set_dev_pasid, .map_pages = arm_smmu_map_pages, .unmap_pages = arm_smmu_unmap_pages, .flush_iotlb_all = arm_smmu_flush_iotlb_all, 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 b842414b143c10..cea1a0f8e25475 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -771,7 +771,7 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, 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 arm_smmu_cd *cd); void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid);