From nobody Thu Nov 28 02:57:09 2024 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2071.outbound.protection.outlook.com [40.107.243.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FF31136E09 for ; Mon, 7 Oct 2024 04:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.71 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728274491; cv=fail; b=nn7BkwmZDXfSSo0fh4V0tGaX5cR7edCBkYxVAOq464FUFITejiKkE/bygdLLx9+0MGxIpvbXzCzgAvHsiWEv6BJjgxK1iq6OAg45d76JgAjrmN1LsM8UoXENQkLy7AXjROHef6DESPT3ri3GWR3rIRIQiPsZFOUb3J8zX3EWa74= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728274491; c=relaxed/simple; bh=BkaK2l6BPIj9YMoWUZuCtgo0Pb/xGgExvoOx0dPPxnw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=omZi211R+WS728I1jIEtoXtnIFC4xYGJY2qUJGZVoY/aJA5yBLPbGiR3W098d8o+tQMcyYgauauQZ3y1FcUKglLVBXTi6suFeS8I03Mcxa9UDYH6SfAA6rSCvABRSsuVzquoesFpicQt0Q/ZCEJHyKQZ5V17iR1qVO3s/9UFCYo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=WnV4ik6X; arc=fail smtp.client-ip=40.107.243.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="WnV4ik6X" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vpQ2pRwfjkeNJnI18cmtYPZRwH0FMrL3FHK6Bf3/houRx+E0miaAF+KDtU7wd0SDAM9OZVglFN6WihwHTp2WXJc4oDCNg2jigJ85u0X5IQGmp60ywb/TIZNTWj5c2N//HSRNvZhr0saLgzwm9clu+BCHCG9OVdKYAq8JiSzdb4LfvbtEIkL92S1/TpdzoH0skq/T4QFaMBf/Jo3Atf+f8mcMTAYl5Z7m4Iyvd0hOKPTb0xwNTjzmwfm6r00Xk3+H1OvP5lwtDrx91KavPXZAB5DZ75b/WgfSL2pg9HjfDnjt1vqsyYuxmSr1cP3RY80UFFWsZDgmIIVoDIlqJZs31Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vP9GYGlo2SZhQ7Sccnv/33eRsH8yXo0gRaTl5X79j28=; b=FxWY3Nj1ZvL8WQ0YiQCJP2dZhzm0KT6I3a21cRnHAMhY8JblEWslCtYH0hhCumFNWJ92BcAHnK9imGSAR0jJITgvyi/AlXk6+1zKmLA/zYIZi6VTdoL2Tm6UKkzn01WmzAJKdHZUmS9r2oqWvqmzvbVPN4cKl5UuwLy2L226GJOGbdY+9lPTa2WAywzPUBvqeZ5/kR1lNmGq5YZp4PahB0bRLywaL79e7Qn8F29BUKnr2PuTCdq2fyH4OQIVCV6Wkgaw6TYZqGHvbY51nnDYgjwJZCiX8EVrDuwjBXA3bVlY7RW41XvRHfoMzkFrpZ2gtV7m4YJfMFRXFZWOrqZX+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vP9GYGlo2SZhQ7Sccnv/33eRsH8yXo0gRaTl5X79j28=; b=WnV4ik6XTx5/UZJcl4ZQWtE6ZaxX4L8HmlxvFYvu/A3bvWg7qm0S1rGP+qDSH7CMatzbnzJaYYgucsxynj9A3pnhAgG4W0lLOLipgGjieOB5dUGQUZr9vmsPA9hShWxLbc/U5eJ7teG3/iLc6zZ298G1mpl3mEjFcyHCSHCreG0= Received: from MW4PR03CA0257.namprd03.prod.outlook.com (2603:10b6:303:b4::22) by MN0PR12MB6002.namprd12.prod.outlook.com (2603:10b6:208:37e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.20; Mon, 7 Oct 2024 04:14:45 +0000 Received: from SJ1PEPF00002311.namprd03.prod.outlook.com (2603:10b6:303:b4:cafe::9b) by MW4PR03CA0257.outlook.office365.com (2603:10b6:303:b4::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.23 via Frontend Transport; Mon, 7 Oct 2024 04:14:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00002311.mail.protection.outlook.com (10.167.242.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8048.13 via Frontend Transport; Mon, 7 Oct 2024 04:14:44 +0000 Received: from purico-ed03host.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Sun, 6 Oct 2024 23:14:40 -0500 From: Suravee Suthikulpanit To: , CC: , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 2/6] iommu/amd: Introduce helper function to update 256-bit DTE Date: Mon, 7 Oct 2024 04:13:49 +0000 Message-ID: <20241007041353.4756-3-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007041353.4756-1-suravee.suthikulpanit@amd.com> References: <20241007041353.4756-1-suravee.suthikulpanit@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00002311:EE_|MN0PR12MB6002:EE_ X-MS-Office365-Filtering-Correlation-Id: 07ddd047-bd1f-42b1-32fa-08dce686934a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?q+APdi9x6rY7M6+wUzD4NOw3q5A7bnKxBtlgb8RfLGa0+DoBf6qZbZiq8EJJ?= =?us-ascii?Q?NsniJRyQuzQAt9V5Y+hC8P0M6T68g1vOjUvVVPWnXj9vrR9jDeswat9xk14k?= =?us-ascii?Q?mQhS7JxDTtc8BTFvKEgHwq+Q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Q?PEd5w9qU7uOI0aWCRKMNQzQEbWIujLktTS++7b6YmvqaVo91N+PD/It+dcF6?= =?us-ascii?Q?WgI6qKeP+lw4y85Wcverj2XuSM2UdRUUS9lRlnYGtxaLZIEmb54yxxUmNcdI?= =?us-ascii?Q?7tZMmqAnTk0Wi1Gt8uOfz72SxJ4FW9Crhg12YUbCb8aaaRCuHn66UtCndVvV?= =?us-ascii?Q?EWlZXYHNEDBSRbAngWuk+IQ28urTNFLEtuuP2S1dO8f4reJZjnRZ9Ge8RF4s?= =?us-ascii?Q?mDOf6pTpm/mKL8hsrLXSVw3X6z1lBaS7KjOSQAzQy6HdRSmpW8DaoOmPQ6Mj?= =?us-ascii?Q?xRVZszBOsivISVfmvt2sbDbchJP4Y5L0lnhlvirUgPsJb73icD7i9rYwgJi8?= =?us-ascii?Q?j7oXfSaKyrjFbN7/tt7PDOzsB2Hn?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Oct 2024 04:14:44.9481 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 07ddd047-bd1f-42b1-32fa-08dce686934a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00002311.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6002 Content-Type: text/plain; charset="utf-8" The current implementation does not follow 128-bit write requirement to update DTE as specified in the AMD I/O Virtualization Techonology (IOMMU) Specification. Therefore, modify the struct dev_table_entry to contain union of u128 data array, and introduce a helper functions update_dte256() to update DTE using two 128-bit cmpxchg operations to update 256-bit DTE with the modified structure, and take into account the DTE[V, GV] bits when programming the DTE to ensure proper order of DTE programming and flushing. In addition, introduce a per-DTE spin_lock struct dev_data.dte_lock to provide synchronization when updating the DTE to prevent cmpxchg128 failure. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 13 +++- drivers/iommu/amd/iommu.c | 114 ++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 601fb4ee6900..91f802be7898 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -425,9 +425,16 @@ #define DTE_GCR3_SHIFT_C 43 =20 #define DTE_GPT_LEVEL_SHIFT 54 +#define DTE_GPT_LEVEL_MASK GENMASK_ULL(55, 54) =20 #define GCR3_VALID 0x01ULL =20 +/* DTE[128:179] | DTE[184:191] */ +#define DTE_DATA2_INTR_MASK ~GENMASK_ULL(55, 52) + +/* DTE[180:181] */ +#define DTE_DATA2_RESV_MASK GENMASK_ULL(53, 52) + #define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL) #define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_PR) #define IOMMU_PTE_DIRTY(pte) ((pte) & IOMMU_PTE_HD) @@ -832,6 +839,7 @@ struct devid_map { struct iommu_dev_data { /*Protect against attach/detach races */ spinlock_t lock; + spinlock_t dte_lock; /* DTE lock for 256-bit access */ =20 struct list_head list; /* For domain->dev_list */ struct llist_node dev_data_list; /* For global dev_data_list */ @@ -882,7 +890,10 @@ extern struct amd_iommu *amd_iommus[MAX_IOMMUS]; * Structure defining one entry in the device table */ struct dev_table_entry { - u64 data[4]; + union { + u64 data[4]; + u128 data128[2]; + }; }; =20 /* diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 8364cd6fa47d..deb19af48a3e 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -77,12 +77,116 @@ static void detach_device(struct device *dev); static void set_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data); =20 +static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid); + /*************************************************************************= *** * * Helper functions * *************************************************************************= ***/ =20 +static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_tab= le_entry *new) +{ + struct dev_table_entry old =3D {}; + + do { + old.data128[1] =3D ptr->data128[1]; + new->data[2] &=3D ~DTE_DATA2_INTR_MASK; + new->data[2] |=3D old.data[2] & (DTE_DATA2_INTR_MASK | DTE_DATA2_RESV_MA= SK); + } while (!try_cmpxchg128(&ptr->data128[1], &old.data128[1], new->data128[= 1])); +} + +static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_tab= le_entry *new) +{ + struct dev_table_entry old =3D {}; + + /* + * Need to preserve DTE[96:106], which can be set by information in IVRS = table. + * See set_dev_entry_from_acpi(). + */ + new->data[1] |=3D ptr->data[1] & DTE_FLAG_MASK; + + do { + old.data128[0] =3D ptr->data128[0]; + } while (!try_cmpxchg128(&ptr->data128[0], &old.data128[0], new->data128[= 0])); +} + +/* + * Note: + * IOMMU reads the entire Device Table entry in a single 256-bit transacti= on + * but the driver is programming DTE using 2 128-bit cmpxchg. So, the driv= er + * need to ensure the following: + * - DTE[V|GV] bit is being written last when setting. + * - DTE[V|GV] bit is being written first when clearing. + * + * This function is used only by code, which updates DMA translation part = of the DTE. + * So, only consider control bits related to DMA when updating the entry. + */ +static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *= dev_data, + struct dev_table_entry *new) +{ + struct dev_table_entry *dev_table =3D get_dev_table(iommu); + struct dev_table_entry *ptr =3D &dev_table[dev_data->devid]; + + spin_lock(&dev_data->dte_lock); + + if (!(ptr->data[0] & DTE_FLAG_V)) { + /* Existing DTE is not valid. */ + write_dte_upper128(ptr, new); + write_dte_lower128(ptr, new); + iommu_flush_dte_sync(iommu, dev_data->devid); + } else if (!(new->data[0] & DTE_FLAG_V)) { + /* Existing DTE is valid. New DTE is not valid. */ + write_dte_lower128(ptr, new); + write_dte_upper128(ptr, new); + iommu_flush_dte_sync(iommu, dev_data->devid); + } else if (!FIELD_GET(DTE_FLAG_GV, ptr->data[0])) { + /* + * Both DTEs are valid. + * Existing DTE has no guest page table. + */ + write_dte_upper128(ptr, new); + write_dte_lower128(ptr, new); + iommu_flush_dte_sync(iommu, dev_data->devid); + } else if (!FIELD_GET(DTE_FLAG_GV, new->data[0])) { + /* + * Both DTEs are valid. + * Existing DTE has guest page table, + * new DTE has no guest page table, + */ + write_dte_lower128(ptr, new); + write_dte_upper128(ptr, new); + iommu_flush_dte_sync(iommu, dev_data->devid); + } else if (FIELD_GET(DTE_GPT_LEVEL_MASK, ptr->data[2]) !=3D + FIELD_GET(DTE_GPT_LEVEL_MASK, new->data[2])) { + /* + * Both DTEs are valid and have guest page table, + * but have different number of levels. So, we need + * to upadte both upper and lower 128-bit value, which + * require disabling and flushing. + */ + struct dev_table_entry clear =3D {}; + + /* First disable DTE */ + write_dte_lower128(ptr, &clear); + iommu_flush_dte_sync(iommu, dev_data->devid); + + /* Then update DTE */ + write_dte_upper128(ptr, new); + write_dte_lower128(ptr, new); + iommu_flush_dte_sync(iommu, dev_data->devid); + } else { + /* + * Both DTEs are valid and have guest page table, + * and same number of levels. We just need to only + * update the lower 128-bit. So no need to disable DTE. + */ + write_dte_lower128(ptr, new); + } + + spin_unlock(&dev_data->dte_lock); +} + static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom) { return (pdom && (pdom->pd_mode =3D=3D PD_MODE_V2)); @@ -203,6 +307,7 @@ static struct iommu_dev_data *alloc_dev_data(struct amd= _iommu *iommu, u16 devid) return NULL; =20 spin_lock_init(&dev_data->lock); + spin_lock_init(&dev_data->dte_lock); dev_data->devid =3D devid; ratelimit_default_init(&dev_data->rs); =20 @@ -1272,6 +1377,15 @@ static int iommu_flush_dte(struct amd_iommu *iommu, = u16 devid) return iommu_queue_command(iommu, &cmd); } =20 +static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid) +{ + int ret; + + ret =3D iommu_flush_dte(iommu, devid); + if (!ret) + iommu_completion_wait(iommu); +} + static void amd_iommu_flush_dte_all(struct amd_iommu *iommu) { u32 devid; --=20 2.34.1