From nobody Fri Nov 14 21:03:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1760681736; cv=pass; d=zohomail.com; s=zohoarc; b=lB6xSnZL1GE78Q2PgCoVtNA9aPaZlFqdRYTgu5qyj6tqYu/VHRC1B+UUis9BWMoFdxcGPbRrKjt1NavEkfjoT9t1861jQonMlabmg8asUUKqrZtW4c5QBWVO9Ig/+LN2wS6wIT5eAdEwlHs96s9CVatAbH47zIgnAVHMS7M3/IM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760681736; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=u5CaXy3Vq/oMW2meOhC0Nh7UC6+eV9MMpDH3oU1q7VE=; b=MdftI/RxNJwpFOawjmrctFDp9cvIrIMwyt3L7Z2LY2vSqKpZxbxQuPZ427MV6RTnKMN3RY5Kgnof6pUUJR0SZrmAM4TmdnGymfjWVt1eozdTByNrjereZZCW8qQcKon0PStLz/SL+Tmlw3ladyWGZIvtRPffI0zoqw+1tV6SnXo= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1760681736575551.7988650368003; Thu, 16 Oct 2025 23:15:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9dje-0004GV-MJ; Fri, 17 Oct 2025 02:14:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v9djc-0004F9-Fi for qemu-devel@nongnu.org; Fri, 17 Oct 2025 02:14:40 -0400 Received: from mail-westcentralusazlp170130007.outbound.protection.outlook.com ([2a01:111:f403:c112::7] helo=CY3PR05CU001.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v9djX-0008He-GG for qemu-devel@nongnu.org; Fri, 17 Oct 2025 02:14:40 -0400 Received: from CH0P220CA0009.NAMP220.PROD.OUTLOOK.COM (2603:10b6:610:ef::23) by IA1PR12MB6188.namprd12.prod.outlook.com (2603:10b6:208:3e4::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9228.11; Fri, 17 Oct 2025 06:14:25 +0000 Received: from DS3PEPF000099E0.namprd04.prod.outlook.com (2603:10b6:610:ef:cafe::cd) by CH0P220CA0009.outlook.office365.com (2603:10b6:610:ef::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9228.12 via Frontend Transport; Fri, 17 Oct 2025 06:14:24 +0000 Received: from satlexmb07.amd.com (165.204.84.17) by DS3PEPF000099E0.mail.protection.outlook.com (10.167.17.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9253.7 via Frontend Transport; Fri, 17 Oct 2025 06:14:23 +0000 Received: from BLR-L1-SARUNKOD.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Thu, 16 Oct 2025 23:14:19 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZaMyoCPEgDEDliYHculFL/bvHzcs3YLh30e/wqRw6LSFTcsO1fgoUG3zbtIYrhSllrp6gJhYH/AJmzDPTt8YJdtkfLkxvaDVVL6C07rrx9MfI7bNFn8Gn3emOEktbIkT8t/Ac4JGEW+iYmMXiOJXpsy4y91ouzUuCh7zqFqLN/8FxxscHXo41HBHYxdXXEl/JLnOifnP8UakwmotcdeVGkbsGup4YVI5U2aWyzZZvlIMltZBwdYJvp6OgsRMKOvyqseptS7xZK5/4mkRkL/XdmInQ8fPdQCr1Brp336NxclERdArA+10TFwxY7JoPdjuzyFwEtqDxETWx1nvSaqG4g== 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=u5CaXy3Vq/oMW2meOhC0Nh7UC6+eV9MMpDH3oU1q7VE=; b=Td2ODKLgUm8Ans6Amc6PVicv7IdbilpcT03DCxKGccivJcgOLqMuJhtqWk0TAJTEXx4HV5laBK0KjS2++6jeTKnQV/xnFmDm1DX1mo4I4U7JqSKkFPnbkWGlFU8g8sAOHEen8zyHjq3nWIC358lqvO4gCVTozOOf5uMRrTaBYdbk+jNBE8U6MJqj2nvlozTOwtBhhJkVL0ikkuoQfbDsMOpfFbaghtNdd7q8wrJiJg5gWyB+yEsyqKPKtoRVUoAKtr6shyJK7dYkHDPstYRpiELUznpE8m1XM09qmuDZsXuX9GAFl+el6YUNh6OlTOAnSmIsHrue23sTEIrsyd783w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nongnu.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=u5CaXy3Vq/oMW2meOhC0Nh7UC6+eV9MMpDH3oU1q7VE=; b=0iaifBWxbxmA2tu0qFeYhA1MyJlzlXM1x2yt4UtqN4wNMk9VcZ0pVoSQ6wcjVCL8ouMrOLpQvibkodcsifYShBUaCZOVcD4UofVs3djhomFVF6yDy4IEVdAc/ZpQobVKl66PzdFhDT3zkSfHfSHqOyBNAv34546c8OXsF7eqoSo= 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 (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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=satlexmb07.amd.com; pr=C From: Sairaj Kodilkar To: , CC: , , , , , , , , , "Sairaj Kodilkar" Subject: [PATCH v3 2/2] amd_iommu: Support 64 bit address for IOTLB lookup Date: Fri, 17 Oct 2025 11:43:22 +0530 Message-ID: <20251017061322.1584-3-sarunkod@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251017061322.1584-1-sarunkod@amd.com> References: <20251017061322.1584-1-sarunkod@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: satlexmb07.amd.com (10.181.42.216) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099E0:EE_|IA1PR12MB6188:EE_ X-MS-Office365-Filtering-Correlation-Id: 7fc1d75a-eed5-4517-d5d2-08de0d446b29 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?BYEjLBnaoQVrcaVMYmWy1LEHpriKi4Zpb2Ihmb1Y7OHZKvp9pEfTLNwRw58m?= =?us-ascii?Q?x4myMlK+YsvfMwNXs2c1leydIqMq6z13ZASJs0BOXptBuXdLi1eWedKfOsAu?= =?us-ascii?Q?HO+k5gShM3figeyIpIjfTA+3lfUh8W8w8LrJ8RIzs5q5/eZqBn/wFo3K6mv+?= =?us-ascii?Q?cKt40VYiD7sVw+QE0zCKJ59qBWYLn6UtUVeTPeuOfSAZj62iQO+lgZOmYWrv?= =?us-ascii?Q?40EMWTRBmyYbqRgtW2AU7TXDpokvD5L2rfiWMcsZk/zYnTWDeeYrd8a0WbKT?= =?us-ascii?Q?3da8OBU8nEIpYIv353Ic9LX04jDsSpHvGQDoR2/XHOVofYuqimT0mfZ5odf2?= =?us-ascii?Q?Hx/8QlAJyV+BeU3Rx6wMHIwV5kUyy2jgELGghndacfSI6pGFsDrWNazVwquv?= =?us-ascii?Q?VIf3RlzADTHbatCa3k3DrKqokoN0uG6oNj4GV8eTKEQUUlJmsyBll9ON6u3u?= =?us-ascii?Q?nQuldtfs/dEbhz1hFSHyMfG/G/6pr7mmIpDpaC4EDbDaOwkbN6sjqJOAKvv7?= =?us-ascii?Q?TizVIUju7nmJJq+k5a+jKI8usKwHlFX535JvnwBrj3Tt55Aj4LLC4iRqElaA?= =?us-ascii?Q?NhGfsmDCnFUfjobRSU4KNviF1uQyGV3c/xci0khdJR9l9vWCCIlBjT8X1dB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mb9SzsHHli2+e87+QD2guPe4cBgmJ?= =?us-ascii?Q?dmyBW2OKMcyBj5Wt1O8=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:satlexmb07.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230040)(376014)(82310400026)(36860700013)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Oct 2025 06:14:23.9233 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7fc1d75a-eed5-4517-d5d2-08de0d446b29 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=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099E0.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6188 Received-SPF: permerror client-ip=2a01:111:f403:c112::7; envelope-from=Sairaj.ArunKodilkar@amd.com; helo=CY3PR05CU001.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1760681740085158500 Content-Type: text/plain; charset="utf-8" Physical AMD IOMMU supports up to 64 bits of DMA address. When device tries to read or write from a given DMA address, IOMMU translates the address using page table assigned to that device. Since IOMMU uses per device page tables, the emulated IOMMU should use the cache tag of 68 bits (64 bit address - 12 bit page alignment + 16 bit device ID). Current emulated AMD IOMMU uses GLib hash table to create software iotlb and uses 64 bit key to store the IOVA and deviceID, which limits the IOVA to 60 bits. This causes failure while setting up the device when guest is booted with "iommu.forcedac=3D1". To solve this problem, Use 64 bit IOVA and 16 bit devid as key to store entries in IOTLB; Use upper 52 bits of IOVA (GFN) and lower 12 bits of the devid to construct the 64 bit hash key in order avoid the truncation as much as possible (reducing hash collisions). Fixes: d29a09ca6842 ("hw/i386: Introduce AMD IOMMU") Signed-off-by: Sairaj Kodilkar Reviewed-by: Vasant Hegde --- hw/i386/amd_iommu.c | 57 ++++++++++++++++++++++++++++++--------------- hw/i386/amd_iommu.h | 4 ++-- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c index c2cd5213eb1b..5487894aba40 100644 --- a/hw/i386/amd_iommu.c +++ b/hw/i386/amd_iommu.c @@ -106,6 +106,11 @@ typedef struct AMDVIAsKey { uint8_t devfn; } AMDVIAsKey; =20 +typedef struct AMDVIIOTLBKey { + uint64_t gfn; + uint16_t devid; +} AMDVIIOTLBKey; + uint64_t amdvi_extended_feature_register(AMDVIState *s) { uint64_t feature =3D AMDVI_DEFAULT_EXT_FEATURES; @@ -377,16 +382,6 @@ static void amdvi_log_pagetab_error(AMDVIState *s, uin= t16_t devid, PCI_STATUS_SIG_TARGET_ABORT); } =20 -static gboolean amdvi_uint64_equal(gconstpointer v1, gconstpointer v2) -{ - return *((const uint64_t *)v1) =3D=3D *((const uint64_t *)v2); -} - -static guint amdvi_uint64_hash(gconstpointer v) -{ - return (guint)*(const uint64_t *)v; -} - static gboolean amdvi_as_equal(gconstpointer v1, gconstpointer v2) { const AMDVIAsKey *key1 =3D v1; @@ -425,11 +420,30 @@ static AMDVIAddressSpace *amdvi_get_as_by_devid(AMDVI= State *s, uint16_t devid) amdvi_find_as_by_devid, &devid); } =20 +static gboolean amdvi_iotlb_equal(gconstpointer v1, gconstpointer v2) +{ + const AMDVIIOTLBKey *key1 =3D v1; + const AMDVIIOTLBKey *key2 =3D v2; + + return key1->devid =3D=3D key2->devid && key1->gfn =3D=3D key2->gfn; +} + +static guint amdvi_iotlb_hash(gconstpointer v) +{ + const AMDVIIOTLBKey *key =3D v; + /* Use GPA and DEVID to find the bucket */ + return (guint)(key->gfn << AMDVI_PAGE_SHIFT_4K | + (key->devid & ~AMDVI_PAGE_MASK_4K)); +} + + static AMDVIIOTLBEntry *amdvi_iotlb_lookup(AMDVIState *s, hwaddr addr, uint64_t devid) { - uint64_t key =3D (addr >> AMDVI_PAGE_SHIFT_4K) | - ((uint64_t)(devid) << AMDVI_DEVID_SHIFT); + AMDVIIOTLBKey key =3D { + .gfn =3D AMDVI_GET_IOTLB_GFN(addr), + .devid =3D devid, + }; return g_hash_table_lookup(s->iotlb, &key); } =20 @@ -451,8 +465,10 @@ static gboolean amdvi_iotlb_remove_by_devid(gpointer k= ey, gpointer value, static void amdvi_iotlb_remove_page(AMDVIState *s, hwaddr addr, uint64_t devid) { - uint64_t key =3D (addr >> AMDVI_PAGE_SHIFT_4K) | - ((uint64_t)(devid) << AMDVI_DEVID_SHIFT); + AMDVIIOTLBKey key =3D { + .gfn =3D AMDVI_GET_IOTLB_GFN(addr), + .devid =3D devid, + }; g_hash_table_remove(s->iotlb, &key); } =20 @@ -463,8 +479,10 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t= devid, /* don't cache erroneous translations */ if (to_cache.perm !=3D IOMMU_NONE) { AMDVIIOTLBEntry *entry =3D g_new(AMDVIIOTLBEntry, 1); - uint64_t *key =3D g_new(uint64_t, 1); - uint64_t gfn =3D gpa >> AMDVI_PAGE_SHIFT_4K; + AMDVIIOTLBKey *key =3D g_new(AMDVIIOTLBKey, 1); + + key->gfn =3D AMDVI_GET_IOTLB_GFN(gpa); + key->devid =3D devid; =20 trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid= ), PCI_FUNC(devid), gpa, to_cache.translated_addr); @@ -477,7 +495,8 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t = devid, entry->perms =3D to_cache.perm; entry->translated_addr =3D to_cache.translated_addr; entry->page_mask =3D to_cache.addr_mask; - *key =3D gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT); + entry->devid =3D devid; + g_hash_table_replace(s->iotlb, key, entry); } } @@ -2526,8 +2545,8 @@ static void amdvi_sysbus_realize(DeviceState *dev, Er= ror **errp) } } =20 - s->iotlb =3D g_hash_table_new_full(amdvi_uint64_hash, - amdvi_uint64_equal, g_free, g_free); + s->iotlb =3D g_hash_table_new_full(amdvi_iotlb_hash, + amdvi_iotlb_equal, g_free, g_free); =20 s->address_spaces =3D g_hash_table_new_full(amdvi_as_hash, amdvi_as_equal, g_free, g_free); diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h index 38471b95d153..302ccca5121f 100644 --- a/hw/i386/amd_iommu.h +++ b/hw/i386/amd_iommu.h @@ -220,8 +220,8 @@ #define PAGE_SIZE_PTE_COUNT(pgsz) (1ULL << ((ctz64(pgsz) - 12) % 9)) =20 /* IOTLB */ -#define AMDVI_IOTLB_MAX_SIZE 1024 -#define AMDVI_DEVID_SHIFT 36 +#define AMDVI_IOTLB_MAX_SIZE 1024 +#define AMDVI_GET_IOTLB_GFN(addr) (addr >> AMDVI_PAGE_SHIFT_4K) =20 /* default extended feature */ #define AMDVI_DEFAULT_EXT_FEATURES \ --=20 2.34.1