From nobody Fri Sep 25 09:20:33 2026 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011037.outbound.protection.outlook.com [52.101.52.37]) (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 52471301719 for ; Mon, 14 Sep 2026 18:48:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.52.37 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411709; cv=fail; b=nulO8MQ201Ng5q8bQi7RGqBYegF+7a147KbU8JcJY9bhIav3YMxm86w4eaq7YOu2rK3hSXuJq5w455XuWt6NsqDGQ/DuEsQwfMXo8ZkJr6+kZ4oeZoO1EUNr2Lh3Wi1+4k5jR5apQQ6RyYElhUT5Y386/jQ2GfZPqJVANR96Ks8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411709; c=relaxed/simple; bh=/Y2PQl0vJnjW2tmf54ugY+um95UjPb9FnJ6D/9kW22w=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GnCpg7z0Jsda8ZLdWmLqX9TZS5EfS/GBLR8WpNLgJeYkElzGcBZVCil9Y0CehRoCihSnX+vHNLr5VZnxEQY4fqlk+mXvPCPF61OVSdRHMQ9ur2IT5BX9019WDPOCfkaufc7SoRedwmmflrdMuR0bBYkBJX4YgF1YTo0MQwWCTOA= 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=PNpJpZ5o; arc=fail smtp.client-ip=52.101.52.37 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="PNpJpZ5o" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jlLSR9FOQekoVj+vfi3G8Yp024Y0WVVxopQghaIlT/xf1f/FyDnWpyj1tLMRsuH5apC8sO7krFap0SwbktgDlBaN+nHrU4GJWxHs1KIDl2NGA9CNskJtu29a3bUiU2eU9FYqJS1dF0VctxjbfycVi/PNo29fA6jQy5i6rt5Pt2FFMDMk4Z1lZNWKkVHCWBTNXE0fTcxmnSW/xD6bh+/1mI/Fi/feeQrTE4kWwSVPV2FU+Al8QHL6YEpnu5xc0B5IGzh6JVPHgrmSwfSQ/Xhw6piFZA59N+b5QV1ptdG+BV4XOzlZPtTwBiReHEMlQuHfcdFlBb3y/lLP+6bwg9uYZg== 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=wx89cAN1XzONKzeV3Cn94rs4Y+inKDiYKDwx9ZYCjHI=; b=Sjp+c7FWhnmlk7EQCa/Hq5gG82S1nQeqhidsaiFLj7pN+oLzlJ9PJIMsvyN8S886+giIs6q2JypY0o2RNcvdmoHVKB9fxjgYzTXDy8uYeHiwFVU33FQKbSA1Z8xmO7/neOkWuVO9ikphYiZlqE6nUmJhSszpaHe51xGFPX0HuRpQnx3H/uZBqZ74JbDGhgNSDV6efutLgnEtcd1AtpoOH2TwcmyTwi/DgvpvCS/X1vqcF8zexh61VyNqt7GwIHEyHa8fGn7wSrhnsL+fXHc1jXfFPJmsZkSgOG7NgM8xSJ1wGdCuRv6p8IBnpnL3Z+BpCJ7l/biXCpEHDLTQU6xreQ== 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=wx89cAN1XzONKzeV3Cn94rs4Y+inKDiYKDwx9ZYCjHI=; b=PNpJpZ5oUPnb71qZ1pLeF7pKs2j+3i3S3HN7evVagjle+x34aHsC+njejEivWep4I/0ZnfKqt3H9rCQYB2hqcxbXA1hMENqo7+DA0++helXBkkxAt1BHl11eR6BZRDeHdlKsf+kMigQVlmAIJ7ZWuA8fQabqKRFhTBpnrFzz9Fg= Received: from BN9PR03CA0175.namprd03.prod.outlook.com (2603:10b6:408:f4::30) by CH3PR12MB8850.namprd12.prod.outlook.com (2603:10b6:610:167::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.8; Mon, 14 Sep 2026 18:48:22 +0000 Received: from BL02EPF00021F6E.namprd02.prod.outlook.com (2603:10b6:408:f4:cafe::6f) by BN9PR03CA0175.outlook.office365.com (2603:10b6:408:f4::30) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:22 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6E.mail.protection.outlook.com (10.167.249.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:22 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:16 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 01/24] iommu/amd: Introduce vIOMMU-specific events and event Date: Mon, 14 Sep 2026 18:47:27 +0000 Message-ID: <20260914184750.222939-2-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6E:EE_|CH3PR12MB8850:EE_ X-MS-Office365-Filtering-Correlation-Id: 8fc9c6c3-c0fe-47a5-5dcf-08df1290c08f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|23010399003|36860700016|1800799024|82310400026|10067099003|56012099006|6133799003|11063799006|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: 3Li4A0byHXgbC7GGu/fcLsYRlB+uCb+veQ1C9uFoWK403GpoQKPZhwHCysdC5cFFlojuoxZEauwMxvC4LUhAkMehBuXLjiCNNTqEPsdTm2OB1PxlHJ1n/EuvGPeGDzrMKaN1bK2RhX8//ylBF9IFrYAzyzDfLPPmgY4ue4B0uuRI18hrPk4J6uhDmEDXbpnou4Tn8t7nSqg6tamj5oioYIujlyeD8JBHnCofKwWUyQq8QfZ61l8+DESA/gylOPqQ6PBcu3YQ3uA1PEIsaIHl/ZuXiMmMk7c+RWYLicEz9DiPhlmsPWE/wOTUFNctYaeu+riwYXYOQnJrIBrUmXAwGQ5sGeJvwsaSdkLGtse/y5VJX8BK1Rqid+m9dbNBCPTDrcCHAyG+5SHOGlVt5Sp8vnWrgT3mWZ539gqLVDhWUVMLNLKsMxet5bVIzt+JblwSRXrop2hjxlOgZpkTHZQvE6qaR8EJf6yjVhphfPheFxxOgiUhgWFcUB6uygFrGnzT0gKtl1kpMB8jLKO2Av7u0Eq0Cl+vxEnscZMCJKX3HZvatG7yRo3qNHdNvxQMakI5AqbmC2Zp1Fh/NlNlbyfbX//t2hSh0HW0tR4d3YKSVCP3TEgup1DNpLqiEVMRq9s5jymuCPAhBhhl8BXOnZUUkclsBgqjmb6VMW+HlP6IN5p7CYns+9DYeEvKRzBbULna4SLHljRDP44ycai/28tJIw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(7416014)(23010399003)(36860700016)(1800799024)(82310400026)(10067099003)(56012099006)(6133799003)(11063799006)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: EodRvFRW3E1ZqOQ1MheXiJVjgOL4e5ECUeqjR2auH1+oxNQLcsAYj6o6zMpKqNxR4DPNgErH/2DFL+DksQr0gMVgOaNWeyK/fEjf/Tj5xPRRVtS+iT4bWO1bmNW19PWjjBodYX/CMNqEK4xVPZzv+pcAeyRiNP9oQ47Da9kFkkdS0ZAYJ3kSYxbKlcrNCzqDHwafDRLQ6qLwpJBhDLaaZ4CabXrOEKeXEc0hE6rilV9fKw8eB0/QSLU4cjw0gv2sV59RErb8WHF37hek2N1yYsdw/0Pl1tCUm4IxoGtuwtPBsLUHr2jcVMOB9+MtMn3F1iYopDUb4olHDJLMiVlV41pTIO5qrIPZXLhLWr+yahiF7x1tm2cg0HcBlUOxlTtJfyNrUudgyhE9nVs5qsW84+Q1zxvcaXTgqThq9TI8dDsod6bIneHGbeZp+4Bv03pQ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:22.4682 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8fc9c6c3-c0fe-47a5-5dcf-08df1290c08f 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: BL02EPF00021F6E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8850 Content-Type: text/plain; charset="utf-8" Adding support for new vIOMMU events: * vIOMMU Hardware Error event Also, adding support for the additional vIOMMU related flags in existing events. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 5 ++++ drivers/iommu/amd/iommu.c | 37 +++++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 8241ef922519..f26a9059a356 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -138,6 +138,8 @@ #define EVENT_TYPE_IOTLB_INV_TO 0x7 #define EVENT_TYPE_INV_DEV_REQ 0x8 #define EVENT_TYPE_INV_PPR_REQ 0x9 +#define EVENT_TYPE_VIOMMU_HW_ERR 0xc + #define EVENT_TYPE_RMP_FAULT 0xd #define EVENT_TYPE_RMP_HW_ERR 0xe #define EVENT_DEVID_MASK 0xffff @@ -251,6 +253,9 @@ #define EVTLOG_SIZE_MAX SZ_512K /* 32K entries */ #define EVTLOG_LEN_MASK_MAX (0xFULL << EVTLOG_SIZE_SHIFT) =20 +/* Constants for vIOMMU event fields */ +#define EVENT_VFLAGS_MASK GENMASK_ULL(31, 27) + /* Constants for PPR Log handling */ #define PPRLOG_ENTRY_SIZE 0x10 #define PPRLOG_SIZE_SHIFT 56 diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 56262f6b1f70..43a5690721af 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -859,7 +859,7 @@ static void amd_iommu_report_rmp_fault(struct amd_iommu= *iommu, volatile u32 *ev =20 static void amd_iommu_report_page_fault(struct amd_iommu *iommu, u16 devid, u16 domain_id, - u64 address, int flags) + u64 address, int flags, u8 vflags) { struct iommu_dev_data *dev_data =3D NULL; struct pci_dev *pdev; @@ -894,13 +894,13 @@ static void amd_iommu_report_page_fault(struct amd_io= mmu *iommu, } =20 if (__ratelimit(&dev_data->rs)) { - pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=3D0x%04x address=3D0x= %llx flags=3D0x%04x]\n", - domain_id, address, flags); + pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=3D0x%04x address=3D%#= llx flags=3D0x%04x vflags=3D%#x]\n", + domain_id, address, flags, vflags); } } else { - pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=3D%04x:%02x:%02x.= %x domain=3D0x%04x address=3D0x%llx flags=3D0x%04x]\n", + pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=3D%04x:%02x:%02x.= %x domain=3D0x%04x address=3D%#llx flags=3D0x%04x vflags=3D%#x]\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid= ), - domain_id, address, flags); + domain_id, address, flags, vflags); } =20 out: @@ -950,6 +950,7 @@ static void iommu_print_event(struct amd_iommu *iommu, = void *__evt) int count =3D 0; u64 address, ctrl; u32 pasid; + u8 vflags; =20 retry: type =3D (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; @@ -970,29 +971,30 @@ static void iommu_print_event(struct amd_iommu *iommu= , void *__evt) goto retry; } =20 + vflags =3D FIELD_GET(EVENT_VFLAGS_MASK, event[0]); + if (type =3D=3D EVENT_TYPE_IO_FAULT) { - amd_iommu_report_page_fault(iommu, devid, pasid, address, flags); + amd_iommu_report_page_fault(iommu, devid, pasid, address, flags, vflags); return; } =20 switch (type) { case EVENT_TYPE_ILL_DEV: - dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=3D%04x:%02x:%= 02x.%x pasid=3D0x%05x address=3D0x%llx flags=3D0x%04x]\n", + dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=3D%04x:%02x:%= 02x.%x pasid=3D0x%05x address=3D%#llx flags=3D0x%04x vflags=3D%#x]\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid= ), - pasid, address, flags); + pasid, address, flags, vflags); dev_err(dev, "Control Reg : 0x%llx\n", ctrl); dump_dte_entry(iommu, devid); break; case EVENT_TYPE_DEV_TAB_ERR: - dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=3D%04x:%02x:%0= 2x.%x " - "address=3D0x%llx flags=3D0x%04x]\n", + dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=3D%04x:%02x:%0= 2x.%x address=3D%#llx flags=3D0x%04x vflags=3D%#x]\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid= ), - address, flags); + address, flags, vflags); break; case EVENT_TYPE_PAGE_TAB_ERR: - dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=3D%04x:%02x:%= 02x.%x pasid=3D0x%04x address=3D0x%llx flags=3D0x%04x]\n", + dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=3D%04x:%02x:%= 02x.%x pasid=3D0x%04x address=3D%#llx flags=3D0x%04x vflags=3D%#x]\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid= ), - pasid, address, flags); + pasid, address, flags, vflags); break; case EVENT_TYPE_ILL_CMD: dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=3D0x%llx]\n", = address); @@ -1021,6 +1023,15 @@ static void iommu_print_event(struct amd_iommu *iomm= u, void *__evt) case EVENT_TYPE_INV_PPR_REQ: amd_iommu_report_ppr_err(iommu, event, devid, address, flags); break; + case EVENT_TYPE_VIOMMU_HW_ERR: + { + u16 gid =3D event[0] & 0xFFFF; + u8 src =3D (event[0] >> 16) & 0x3; + + dev_err(dev, "Event logged [VIOMMU_HW_ERR gid=3D%#x address=3D%#llx src= =3D%#x flags=3D0x%04x vflags=3D%#x]\n", + gid, address, src, flags, vflags); + break; + } default: dev_err(dev, "Event logged [UNKNOWN event[0]=3D0x%08x event[1]=3D0x%08x = event[2]=3D0x%08x event[3]=3D0x%08x\n", event[0], event[1], event[2], event[3]); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013071.outbound.protection.outlook.com [40.93.196.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 33D7F381E9C for ; Mon, 14 Sep 2026 18:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.71 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411718; cv=fail; b=kmaXtZMTndZG/1L3wro1vZaYuppoJykeBXJQ3IiT2s4k8j8GuJ5I2t7u5zkBqCgqKBBKdOGah0jUvMoG21gRHJFfyw6+Rt9naOv0WRpANG6L6YvjiVMFsxE62Ecs2efr+UY+g47jwmYlOmjrpgkUWTyHF6PMiytc1pcCIDcfoGQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411718; c=relaxed/simple; bh=Lyalh7EPjq0mjMfaOWPVVEL82Lq8TE7j1Pz3PDay41I=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pdAgilXkvZJPGCv/AmIAXd8BzmxO6ei25EXvOdgwjanaiI/qYlPNDMxYuPQVvcoD+lZX9vip2lGy90SLdcogK1bCMOIUCNa1oexjlp2TViHZwyBJLKb4UTy/UQweoXjUOIQ9F6qpVUd8jV4ahoCgMCjxZwNgvKjIWy8cveBT7WQ= 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=kf0mIQfJ; arc=fail smtp.client-ip=40.93.196.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="kf0mIQfJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vJMHuBjLIewRL0LeoGIKjPE4eHgQVCU+N+zTLaC+YFvsH/xqAZXbR/m72pFaydERDEj89mZe2eiEAso+6T4FpgAoaH72ruxnTu32NjBh+G/CKkXnyTzNWvLB27jSAy8Ft49KNwl/ant8lGnkb3JzE1AtDe59RoDi27hmc+n8NYj/o0VsN8q/SardYvQPq01OUHEeAPHH4B2P2DMImLFF6af6CG+X/J6WeWKPvvQahmRLrf9MMjLztPQaJbVyJ1u4UBnWRj06CE/1iMVPyRXOFh5sfUYrixm80cw7mhnVMkdoj/Ksvc4qmh8bolx+vWknOqoD7df4lGd6po5AV3zOag== 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=3HUCt7salzl06P67dQH+HQ2N7aOz5fVmTNQGZLqzN2M=; b=GCDFxJ7OwjS8V4bFoo+l5D5X4zSZKI0qaFMgsx2dMECd7kTyNpMHvO2tI8sPbW2LuUtCjeD5NQL7G4H0HYG23etLZnAXBqNbslelpT1ooTGzyPHRgIp7VrOqjx/Qqz95ptNkdSGy6LjltY3z6Q5t26WCzDiA+KJi9CP29lXFygmLNG5qJbosUKicOFc29MO1LiSBkAT0nMP0jHjah2YWoCSZG9hh3KOeok3K5brxmLSvdXBhS9CZtorZVt7oUAXlJhpS95oEx6J7mRdD5EZ7MzON1RKkqKesp5wjNdaJUsJiWkQMnuJ6oRgTkSKf3DdS2I1rDUnR2vgt/NFzVWnH/Q== 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=3HUCt7salzl06P67dQH+HQ2N7aOz5fVmTNQGZLqzN2M=; b=kf0mIQfJiEThhmGxTHM7wiZUfWkCyF7be1Hu4nnEqZFu15TBIAzy7GKZHW7otNnAekbZak9Xxi1JWgH+ZpRZg7fxk5y4kDrLTjOh1ZDm3GyU7XYMlF2tqOpGoXgDL73duwMue/28DpC5zvgXUFwZgEOVUeY9lxMDpn02j+5LbQQ= Received: from CH0PR03CA0448.namprd03.prod.outlook.com (2603:10b6:610:10e::11) by SJ2PR12MB7824.namprd12.prod.outlook.com (2603:10b6:a03:4c4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:48:29 +0000 Received: from BL02EPF00021F6C.namprd02.prod.outlook.com (2603:10b6:610:10e:cafe::81) by CH0PR03CA0448.outlook.office365.com (2603:10b6:610:10e::11) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:28 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6C.mail.protection.outlook.com (10.167.249.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:28 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:22 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 02/24] iommu/amd: Introduce EVENT_TYPE_GUEST_EVENT_FAULT Date: Mon, 14 Sep 2026 18:47:28 +0000 Message-ID: <20260914184750.222939-3-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6C:EE_|SJ2PR12MB7824:EE_ X-MS-Office365-Filtering-Correlation-Id: 0a16c1f6-60e0-4f88-bb34-08df1290c442 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|23010399003|7416014|376014|36860700016|10067099003|6133799003|22082099003|18002099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: Zyd2Pe3pGNa+FPZ6bYt34UYHfpAeBgJn+bQ9myPmgplUM44XYOS/1/gED+2V94BXSIB8aF5y2KfVkRxdn1wnYyTQ5SA2Iyj6QupUPnMK515qlcyl0b7Beam+9x9FrHpkYrZ7lp1KL1d7N9grQW2AR1ViDOnpnDt0xoUa9sxZDAYp08WHYhVCiTDRHVRC+GLduOF0Ib029+AJdHOSVzxWrX/xvJUJNkV9EpqhrgaXrWCBJf14F/GnKwMNBif+sIDHi24tIN98gCSFpOcU2OUQaHGQTKGw/DJgRZjb61utuB05bcq4nCo+tkXjlWCQuZVTB5oSp4Agsx+rdnJlfY2jcSbtgQa4vupyl20SV6Ss1W4WOM7cDElmvFlG+M/13L7ayxoxpLTNGTXglFPWAn54kdqv3T5uxTwbieA3w9jwdGW36NML47Z5fn5CFaEyFrQpG8EfEgfL4v/dQSDVHe4bpCYOCbyZKjB871+ZeMhYQoTxnGm4yynW7u2Uo28NnVZqtjSQH7Af81HafT55+C5OJVq3T7kzGdSFHblZ8vlIRt/cprtJfFIDIARwboNPgDSMaSOgG/TywafFtS77saQ2frhHQptrOHFQX+12Elr558zlareWLrRGLYYhCCvEHnAcBbvkk9wyfLm+MmX2L8H1OorwJzUHLNP0kI9RF02zwuqht6xn/UtYUupfmL1adM8eKQZxeVb0frEnD8gW7tALMg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(1800799024)(23010399003)(7416014)(376014)(36860700016)(10067099003)(6133799003)(22082099003)(18002099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fELO5G1H+GV7AR6izNsWPi60WmuvXfx5Bf1VjlyQ0OphUq1mlqsnaxFsFbcKCDI8TxaoOYMvsytlPeWcRMul/Eo43lQgiiM/NwNGRHe9xWjJy3BjXz31lP3RZbRS0zs8iqI0lwd3OkAqezdrSxApRXBjMX2fALstrygxxm6yv6n2nKUNuEsTAfxFg+LpkivdajhbzYtYWo0nNSosDgzTz50o/mNTB6bI3zTccdx9SAo2m3kelbuaj8MOkRzQcoOXffYy1vitZ5ulJZgOU+6FRmMB/i6PpMtSt0jZ8avFLS6lja0VI4gigv2dKWtWh1BGKNp6bUNPjTT1YDvXGcIfbkf8LkDJcoCOZhX69KInrXYIdb0HERhAIOMSjmP+WZfUoWWaWzNSYJ+9PXWLoZaTL9QOeNMz9viahZcLJfHHGDK8zvPmKnRbCRwvN5XAKqWp X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:28.6759 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0a16c1f6-60e0-4f88-bb34-08df1290c442 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: BL02EPF00021F6C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7824 Content-Type: text/plain; charset="utf-8" INSERT_GUEST_EVENT with non-zero reserved bits logs GUEST_EVENT_FAULT followed by the original guest event. Consume both event-log slots, wrapping via evt_buf+(head+EVTLOG_ENTRY_SIZE)%size, and dump the guest payload raw so it is not decoded as a host event. Wait until occupancy is at least two entries, not merely until live tail is not the pair offset. Re-read the event-log tail each poll iteration so a return of 2 cannot advance head past a stale cached tail and walk empty slots with the type-0 erratum retry. Retry a type-0 guest slot up to LOOP_TIMEOUT, the same way erratum 732 waits for the primary slot. Return 2 only when that slot is already in [head, live_tail). Return 1 if the pair never appears in tail. Rate-limit the guest-triggered logs. Log an invalid guest-event when the pair slot is still missing or type 0 after that wait, instead of stalling the event log. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 2 + drivers/iommu/amd/iommu.c | 109 +++++++++++++++++++++++++--- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index f26a9059a356..704b4c8acdc5 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -138,6 +138,7 @@ #define EVENT_TYPE_IOTLB_INV_TO 0x7 #define EVENT_TYPE_INV_DEV_REQ 0x8 #define EVENT_TYPE_INV_PPR_REQ 0x9 +#define EVENT_TYPE_GUEST_EVENT_FAULT 0xb #define EVENT_TYPE_VIOMMU_HW_ERR 0xc =20 #define EVENT_TYPE_RMP_FAULT 0xd @@ -255,6 +256,7 @@ =20 /* Constants for vIOMMU event fields */ #define EVENT_VFLAGS_MASK GENMASK_ULL(31, 27) +#define EVENT_GID_MASK GENMASK(15, 0) =20 /* Constants for PPR Log handling */ #define PPRLOG_ENTRY_SIZE 0x10 diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 43a5690721af..c34a0f5a9540 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -942,8 +942,18 @@ static void amd_iommu_report_ppr_err(struct amd_iommu = *iommu, volatile u32 *even pci_dev_put(pdev); } =20 -static void iommu_print_event(struct amd_iommu *iommu, void *__evt) +static u32 evtlog_used(u32 head, u32 tail) { + return (tail + amd_iommu_evtlog_size - head) % amd_iommu_evtlog_size; +} + +/* + * Decode one event-log slot (or a GUEST_EVENT_FAULT pair). + * Returns the number of slots consumed. + */ +static unsigned int iommu_print_event(struct amd_iommu *iommu, u32 head) +{ + void *__evt =3D iommu->evt_buf + head; struct device *dev =3D iommu->iommu.dev; int type, devid, flags; volatile u32 *event =3D __evt; @@ -965,7 +975,7 @@ static void iommu_print_event(struct amd_iommu *iommu, = void *__evt) /* Did we hit the erratum? */ if (++count =3D=3D LOOP_TIMEOUT) { pr_err("No event written to event log\n"); - return; + return 1; } udelay(1); goto retry; @@ -975,7 +985,7 @@ static void iommu_print_event(struct amd_iommu *iommu, = void *__evt) =20 if (type =3D=3D EVENT_TYPE_IO_FAULT) { amd_iommu_report_page_fault(iommu, devid, pasid, address, flags, vflags); - return; + return 1; } =20 switch (type) { @@ -1023,9 +1033,83 @@ static void iommu_print_event(struct amd_iommu *iomm= u, void *__evt) case EVENT_TYPE_INV_PPR_REQ: amd_iommu_report_ppr_err(iommu, event, devid, address, flags); break; + case EVENT_TYPE_GUEST_EVENT_FAULT: + { + u16 gid =3D FIELD_GET(EVENT_GID_MASK, event[1]); + u32 next =3D (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size; + volatile u32 *guest; + u32 live_tail; + int guest_type, wait; + + dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT gid=3D%#x flag= s=3D0x%04x vflags=3D%#x]\n", + gid, flags, vflags); + + /* + * GUEST_EVENT_FAULT is paired with the original guest + * event in the next log slot (possibly wrapped). The + * poll-time tail can predate that slot. Wait until + * occupancy is at least two entries, then retry a + * type-0 pair slot the same way erratum 732 waits + * for DMA visibility. Return 2 only when that slot + * is already in [head, live_tail). iommu_poll_events() + * re-reads tail each iteration so head cannot skip + * past tail. + */ + wait =3D 0; + for (;;) { + live_tail =3D readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); + if (evtlog_used(head, live_tail) >=3D + 2 * EVTLOG_ENTRY_SIZE) + break; + if (++wait =3D=3D LOOP_TIMEOUT) { + dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT invalid gues= t-event (does not exist)]\n"); + if (!amd_iommu_snp_en) + memset(__evt, 0, EVTLOG_ENTRY_SIZE); + return 1; + } + udelay(1); + } + + guest =3D (volatile u32 *)(iommu->evt_buf + next); + wait =3D 0; + for (;;) { + guest_type =3D (guest[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; + if (guest_type) + break; + if (++wait =3D=3D LOOP_TIMEOUT) { + dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT invalid gues= t-event %08x %08x %08x %08x]\n", + guest[0], guest[1], guest[2], guest[3]); + if (!amd_iommu_snp_en) { + memset(__evt, 0, EVTLOG_ENTRY_SIZE); + memset((void *)guest, 0, EVTLOG_ENTRY_SIZE); + } + /* Occupancy already includes this slot. + * Skip it so a late DMA fill is not + * decoded as a host event. + */ + return 2; + } + udelay(1); + } + + dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT guest-event %0= 8x %08x %08x %08x]\n", + guest[0], guest[1], guest[2], guest[3]); + + /* + * Erratum 732: clear both pair slots so a stale guest + * event is not decoded on the next poll. The second + * slot may wrap to the start of the ring. Skip on SNP; + * that buffer is not writable. + */ + if (!amd_iommu_snp_en) { + memset(__evt, 0, EVTLOG_ENTRY_SIZE); + memset((void *)guest, 0, EVTLOG_ENTRY_SIZE); + } + return 2; + } case EVENT_TYPE_VIOMMU_HW_ERR: { - u16 gid =3D event[0] & 0xFFFF; + u16 gid =3D FIELD_GET(EVENT_GID_MASK, event[0]); u8 src =3D (event[0] >> 16) & 0x3; =20 dev_err(dev, "Event logged [VIOMMU_HW_ERR gid=3D%#x address=3D%#llx src= =3D%#x flags=3D0x%04x vflags=3D%#x]\n", @@ -1044,24 +1128,27 @@ static void iommu_print_event(struct amd_iommu *iom= mu, void *__evt) * SNP enabled system. */ if (!amd_iommu_snp_en) - memset(__evt, 0, 4 * sizeof(u32)); + memset(__evt, 0, EVTLOG_ENTRY_SIZE); + + return 1; } =20 static void iommu_poll_events(struct amd_iommu *iommu) { u32 head, tail; + unsigned int n; =20 head =3D readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); - tail =3D readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); - - while (head !=3D tail) { - iommu_print_event(iommu, iommu->evt_buf + head); + for (;;) { + tail =3D readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); + if (head =3D=3D tail) + break; + n =3D iommu_print_event(iommu, head); =20 /* Update head pointer of hardware ring-buffer */ - head =3D (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size; + head =3D (head + n * EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size; writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); } - } =20 #ifdef CONFIG_IRQ_REMAP --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010034.outbound.protection.outlook.com [52.101.193.34]) (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 5BC17391832 for ; Mon, 14 Sep 2026 18:48:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.193.34 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411726; cv=fail; b=LNK5lbVpB3oYhoI2xe6jNEK9cIZgV6ZXNFDbWMOedSOY3pq7pxWy8DgYTXpXTC7pnwLplw2tLGOs/c/eaBZJpo78/gmFVHn2lID+YbsqA/016dIJfD9SOKBdESKm+GJM7sRQiw0nEZtAolbHonYWJa6A0HC6x1dDeHalflizG60= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411726; c=relaxed/simple; bh=EqX4GfhMnOo/mMFjyzcRI4NdFQhdoxYU0Gj5TtJefu8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EziV1/ufKOgXbCtmJmJpyJ5jzGeskaze8xI16XEaj50ljUCsigGD0j4icll0QjXX+iPIcU6I9UU/+lyBAmLm4bijNadRWtsDikO5HipJwyrGkFLH7rRkAJmw+81mf358PLyWN1QiiKPCGFlNJXntCqSUphYvHnAaotyxhA1KGjA= 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=jHiBmB2R; arc=fail smtp.client-ip=52.101.193.34 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="jHiBmB2R" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=k1ZcW2O14f2JxA4szTJJfu5UdhAJZuIJ0TzBjec0GQ1MFqu7XeWYz8tmqDizDg4IROXVW0Vpc3YWhh861DRo0Yyik3vcWvnxG1b19fLe6RZDMxNwokG4sINR5OAjwDMt/PObdGXN6KRazMlcfAricWdlynbBxKndQPF+vEO2F4kFX7BZ66sPPPVRxr0bbNArGdq2jEIV3LU3mqGD/c92xNTW/tj8lfKBGMv7YoII3lK6B+avitf35VPNt4n2HYapKJL31521kWlRb9aqMfB8iZv3ZaSnG2gxeScYRSSXsJsc4zqHDSahreGuB92BN3pAWDRJWzFOEw0Cfq0h4dtDLw== 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=ofg8I3QnaSD+dcVpiAmmhj8OO59UyfzPjnZxSOXtQjQ=; b=ufyyLZ9QM+b4eWbfC3P2alpxbyYNTd9IZpCr7pQMVQ0qtiDFDqlgaW8fK5soPbSjL+n+a/qFyDEC0jQskLPqJvZtf2bC+9Z5i/gi5fuGEueGkVWpt+uj3SSVx2oiT+XS2tw23I9xebHGrdTAyWbKq+CpALlwFURFrvKkMBT1qv6YDPuw+w0Z5SpqZfGpYLe7JsK+xx5PRoCXQ9jN5qgou1ddrBeytnNwRsbMtNrfmvz2wQrVQWotfsvQfYJel8/SG2HFPLCLrTbIKrben/T8m4PPyXBxbNRpE6ihOV8E+vmGVnI2X0WD3Aq6lz4IMY5j/U2SoHFRCzK7sihquXv11w== 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=ofg8I3QnaSD+dcVpiAmmhj8OO59UyfzPjnZxSOXtQjQ=; b=jHiBmB2RoFtQC8fZFKH0VbWizMXjXzsGh56YLpCmBkSBmKu8F8QFAihmc+qGuUspIL7Y3JU+8MU0tTzr6yuWbVfdBj6FelufRpWnzwGc5DpcGwqqP/zdYIwkmDR/ph3s7mW2lfH7aEt/aDEwi3uLwCOryGvNLFs0GQiCtgUuO8Q= Received: from CH0PR03CA0450.namprd03.prod.outlook.com (2603:10b6:610:10e::28) by CH2PR12MB4216.namprd12.prod.outlook.com (2603:10b6:610:a8::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:48:34 +0000 Received: from BL02EPF00021F6C.namprd02.prod.outlook.com (2603:10b6:610:10e:cafe::90) by CH0PR03CA0450.outlook.office365.com (2603:10b6:610:10e::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:34 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6C.mail.protection.outlook.com (10.167.249.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:34 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:28 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 03/24] iommu/amd: Detect and initialize AMD vIOMMU feature Date: Mon, 14 Sep 2026 18:47:29 +0000 Message-ID: <20260914184750.222939-4-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6C:EE_|CH2PR12MB4216:EE_ X-MS-Office365-Filtering-Correlation-Id: 731106aa-82aa-48a9-e78c-08df1290c7b3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|7416014|23010399003|376014|1800799024|82310400026|56012099006|11063799006|10067099003|18002099003|22082099003|6133799003|13003099007; X-Microsoft-Antispam-Message-Info: auP4jZPYGvoC+MnUQcUkYBnEuL/P2yZhTosgsP520YcPm3zhiUXeO6tR9Cc3gRKChQhnuoMGSaCDvmV4WjX8rNpr1BsVDlV6g+/hfeyBrnAtH9LaruIddH5FFZx+6Xch7eFK3lZlgDGqAxVdzhHx+xLZFBnmU3L1DN/GWW558LOPGJWbp+8o00UDPzIHSAm9KlD7J019THNj/oQJ/prBadqNfccWRLsBxDiIt5yfQUfoP62I8fpvFtZ0AhdJwt208HPFc2rq2c/PNR2LpJXLZWQulFU/dMWxdhjG/tNF6TYAyXEpA6bAEwCUqY2cODMGoxoAjJpRppo6MYmd5JsA8knVjHk3lQc57hNJ/jZb/HN+mzGm4b3nV9dJGa5wDBPKjOugVt5pQh5pZgnuZZf0Ip2yst7nConYl+fhtncBX0PK0mKRBTIWkuxYgS7tqkXFwTUnbUIwSnFFjkEuMKX/F1oLTfAzRbsfnPmw+VZFKE+C20FaXDFMYalVQwMPV+003fgmVtiQAzR36bjuRA2HvrJOoDBuLJzeSArfcnJ075m6QexGlB6SJfYvlHjb+L2jwJWW6dHye5QriYd+xLOvbocWHdLflJFG2+tyj2VLl9L+eGeUhdRT5lVZYX7l2P5jWfHAJJEyJTwK6eZL/owDVjFHlmlINWnOni8dXVsGngHw/4Tlfy7y2XsEdxKFUi+lO7JUP55Q9JH509u2JDMhjA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(7416014)(23010399003)(376014)(1800799024)(82310400026)(56012099006)(11063799006)(10067099003)(18002099003)(22082099003)(6133799003)(13003099007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HxSgniuTFjZ5VGuV2Zd3QKvmCRCY1HKQnmrmC17H8hZcycR90gSiW28y3J+La/5hAbWPa1Pnx4mOHlO052W1fjO1EX0/eQ5oxWniSlF0oAkRSipzsDo7tdccRWNUhMTklfGN5qeccnckjCVSrdGcdDx222aYE269AOwudul8RRMCTDs7hRGY+Lz4Su4ss04w3osGfW5i13sqX3lRflo6E8d/xYFjEEtSOwukMSGAOmbX3fmjl+7GCYg4Oc2Tvg4QgooijihKJVR1KfoMZo2CuFS3cET7KOsVJlTtLs9UIwXOqsZ5QtZv1JTE4OrpCqSwObSocmztmhPukYn78iwH/armZn59N24ESe0Emkxc8m7VJ2Q48qq752aW6u3BuK8LdSGLhWyOVYciZ6hW78T6wP5oUFMphskRnBH4rL+TL6lpu9jqaC+IWc7cGablT7Mm X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:34.4517 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 731106aa-82aa-48a9-e78c-08df1290c7b3 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: BL02EPF00021F6C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4216 Content-Type: text/plain; charset="utf-8" The feature is advertised w/ EFR[VIOMMUSup]. Please see the AMD IOMMU specification[1] for more detail. Gate amd_viommu_init() on this IOMMU's EFR[VIOMMUSup] (iommu->features) at the PCI init call site. Do not use check_feature()/amd_iommu_efr; that global mask is the AND of every IOMMU. Do not fail other IOMMUs when one instance lacks the bit or init fails. Set AMD_IOMMU_FLAG_VIOMMU_EN when amd_viommu_init() succeeds. Log PCI names of IOMMUs that have that flag set. Forward-declare struct amd_iommu in amd_viommu.h so the header is self-contained. When CONFIG_AMD_IOMMU_IOMMUFD is off, the amd_viommu_init() stub returns 0 so a missing build is not logged as an init failure on EFR[VIOMMUSup] hardware. [1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 5 +++++ drivers/iommu/amd/amd_iommu_types.h | 2 ++ drivers/iommu/amd/amd_viommu.h | 29 ++++++++++++++++++++++++ drivers/iommu/amd/init.c | 34 +++++++++++++++++++++++++++++ drivers/iommu/amd/viommu.c | 25 +++++++++++++++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/amd_viommu.h create mode 100644 drivers/iommu/amd/viommu.c diff --git a/drivers/iommu/amd/Makefile b/drivers/iommu/amd/Makefile index 94b8ef2acb18..e1e824b9c7b0 100644 --- a/drivers/iommu/amd/Makefile +++ b/drivers/iommu/amd/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y +=3D iommu.o init.o quirks.o ppr.o pasid.o -obj-$(CONFIG_AMD_IOMMU_IOMMUFD) +=3D iommufd.o nested.o +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) +=3D iommufd.o nested.o viommu.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) +=3D debugfs.o diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 71113e860859..b2ae17e3df34 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -112,6 +112,11 @@ static inline bool check_feature(u64 mask) return (amd_iommu_efr & mask); } =20 +static inline bool amd_iommu_viommu_enabled(struct amd_iommu *iommu) +{ + return iommu && (iommu->flags & AMD_IOMMU_FLAG_VIOMMU_EN); +} + static inline bool check_feature2(u64 mask) { return (amd_iommu_efr2 & mask); diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 704b4c8acdc5..688cd9240790 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -91,6 +91,7 @@ #define FEATURE_HASUP BIT_ULL(49) #define FEATURE_EPHSUP BIT_ULL(50) #define FEATURE_HDSUP BIT_ULL(52) +#define FEATURE_VIOMMU BIT_ULL(55) #define FEATURE_SNP BIT_ULL(63) =20 =20 @@ -465,6 +466,7 @@ struct irq_domain; struct amd_irte_ops; =20 #define AMD_IOMMU_FLAG_TRANS_PRE_ENABLED (1 << 0) +#define AMD_IOMMU_FLAG_VIOMMU_EN (1 << 1) =20 struct gcr3_tbl_info { u64 *gcr3_tbl; /* Guest CR3 table */ diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h new file mode 100644 index 000000000000..864e7b283cff --- /dev/null +++ b/drivers/iommu/amd/amd_viommu.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2026 Advanced Micro Devices, Inc. + */ + +#ifndef AMD_VIOMMU_H +#define AMD_VIOMMU_H + +struct amd_iommu; + +#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD) + +int amd_viommu_init(struct amd_iommu *iommu); + +#else + +/* + * CONFIG_AMD_IOMMU_IOMMUFD is off: host vIOMMU is not compiled in. + * Succeed as a no-op so iommu_init_pci() does not pr_err on + * EFR[VIOMMUSup] hardware. + */ +static inline int amd_viommu_init(struct amd_iommu *iommu) +{ + return 0; +} + +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ + +#endif /* AMD_VIOMMU_H */ diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 8a410d4aa370..c80872ce0b60 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -34,6 +34,7 @@ #include =20 #include "amd_iommu.h" +#include "amd_viommu.h" #include "../irq_remapping.h" #include "../iommu-pages.h" =20 @@ -2196,6 +2197,19 @@ static int __init iommu_init_pci(struct amd_iommu *i= ommu) if (check_feature(FEATURE_PPR) && amd_iommu_alloc_ppr_log(iommu)) return -ENOMEM; =20 + /* + * vIOMMU is advertised in this IOMMU's EFR[VIOMMUSup] + * (iommu->features). Do not use check_feature()/amd_iommu_efr; + * that global mask is the AND of every IOMMU and can hide the + * bit on mixed systems. + */ + if (iommu->features & FEATURE_VIOMMU) { + ret =3D amd_viommu_init(iommu); + if (ret) + pr_err("Failed to initialize vIOMMU on %s\n", + pci_name(iommu->dev)); + } + if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) { pr_info("Using strict mode due to virtualization\n"); iommu_set_dma_strict(); @@ -2264,6 +2278,25 @@ static int __init iommu_init_pci(struct amd_iommu *i= ommu) return pci_enable_device(iommu->dev); } =20 +static void print_viommu_info(void) +{ + struct amd_iommu *iommu; + const char *sep =3D ""; + + for_each_iommu(iommu) { + if (!amd_iommu_viommu_enabled(iommu)) + continue; + if (!sep[0]) + pr_info("vIOMMU enabled: "); + else + pr_cont("%s", sep); + pr_cont("%s", pci_name(iommu->dev)); + sep =3D ", "; + } + if (sep[0]) + pr_cont("\n"); +} + static void print_iommu_info(void) { int i; @@ -2301,6 +2334,7 @@ static void print_iommu_info(void) pr_info("V2 page table enabled (Paging mode : %d level)\n", amd_iommu_gpt_level); } + print_viommu_info(); } =20 static int __init amd_iommu_init_pci(void) diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c new file mode 100644 index 000000000000..53f3302f7cc6 --- /dev/null +++ b/drivers/iommu/amd/viommu.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2026 Advanced Micro Devices, Inc. + */ + +#define pr_fmt(fmt) "AMD-Vi: " fmt +#define dev_fmt(fmt) pr_fmt(fmt) + +#include +#include +#include + +#include +#include + +#include "iommufd.h" +#include "amd_iommu.h" +#include "amd_iommu_types.h" +#include "amd_viommu.h" + +int __init amd_viommu_init(struct amd_iommu *iommu) +{ + iommu->flags |=3D AMD_IOMMU_FLAG_VIOMMU_EN; + return 0; +} --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012066.outbound.protection.outlook.com [40.107.209.66]) (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 EE0E8358D00 for ; Mon, 14 Sep 2026 18:48:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.209.66 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411729; cv=fail; b=TVS70bI4BI4id+KU/zKkCJ5QMoiTWoqByjhoqA4hiIyGI0hUQmdT/yIJ/RZDcKfFN0NQon2J43at/Nxdz5Tus0fP/VPQfxz0qSDz5CDN4dlLAeJhI3yoPjkylA0IH1G7pz69F6QKyiMYp1Tls32qP9MtpQhUSlr5Sy+IY2oNcKQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411729; c=relaxed/simple; bh=aM5YyIs2+bqiLRrppmSnsqCD4aP0AOClDbnE6zYrB5s=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pI1Q9nFPt2H5KJx4M6lHryV8viqYZEsbDcFPNcec3CvKs9pqOOTf3CQwEJ0ggtFL9zaMVVkjg2hQ/u7S6UalEw5TAjOEuv22ftN5dGJWLfu2gcwh9gzpm7rwBm3b+GYNJ5JNY+azYSbXN9JLNtzC6eu38lr/6Jm7Cl/RVFGTZFo= 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=FyULOeez; arc=fail smtp.client-ip=40.107.209.66 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="FyULOeez" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ip3iA9Stu9lMq6ojI/6H+b/FGkETp9RktdY/5KK9AC5KYNABYi3r260F+KnZQtIxdGjGYgzn4o+gT2QL1RCUcjOShPb099GtZu5i4318C9bAZSMRSkrqM/5Gu4JU3WxMSOKP+vZ5bfomhwh5lIJlT5yTzA7RBlQqzjfij6gEub/Wo7fM/vY8SDyAK7Ey2Vojmgzp05GQAC7wzPyvSvuy+L/iHaZICvr7nkIpFPecK8FHJXnQDzSgpQzGe9P2BdoKYH0jA1YlH33E5Yqt0Hj38YlDmO+aaGF89WUSakM0xLlgIyx7SHx5nkDbGb4CFL/8eeMaqkxB7je+2trrucs1ng== 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=19sO4iDheBBltlk/+QgZNz9plcUhwScMBtiKHphznMQ=; b=BbW5iCFbZITsl6aw7QX8/4NuIIxOrsUovw+HBKZLkSvmc4fbQySQ9zWnxwnpayG7Jb7MTvnPvv5qjosQNM7crkWlrGdPEtHMRmY9yUW5k9r6o3pFLDRxh2wdf3IggeAZC2ACPNVxHflW0ZabTfRdlZUsOd5eD3PyT7ZeuSkLI9QUO2OhrTcDIoswxJ2ay0m49U3Bv/KI+PJevQQSD15KThjArP/0x9KjSUC01WJLg0k+oqvcxxuqQpzykjW3j9pVzPea/74FwzzC/TxqvuAJugt4WErz1uRovAmxID3lTz4Il0uMunnRcehTjI6cQvM17oTA+FeDdBL7uQ6pczmfbw== 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=19sO4iDheBBltlk/+QgZNz9plcUhwScMBtiKHphznMQ=; b=FyULOeeztVrcd0cjfks6vLSfN/soqY6ovJGh40YjeSoRcf0+4tGDC83+DQbH3mMRmryQRpk7c44gjAqJf2PlPY6on491EEHih1oIMqx76d+1a3B80Q/tKXN6Cx1nneDQkHbXdD1INU/znQMIyMHsrpR3OMvE+c8hj8WwaP8po40= Received: from BN9P222CA0007.NAMP222.PROD.OUTLOOK.COM (2603:10b6:408:10c::12) by PH0PR12MB7814.namprd12.prod.outlook.com (2603:10b6:510:288::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:48:42 +0000 Received: from BL02EPF00021F6D.namprd02.prod.outlook.com (2603:10b6:408:10c:cafe::f) by BN9P222CA0007.outlook.office365.com (2603:10b6:408:10c::12) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:40 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6D.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:40 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:34 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 04/24] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD Date: Mon, 14 Sep 2026 18:47:30 +0000 Message-ID: <20260914184750.222939-5-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6D:EE_|PH0PR12MB7814:EE_ X-MS-Office365-Filtering-Correlation-Id: 33bb282b-0928-4073-d9bc-08df1290cb4c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|7416014|1800799024|82310400026|36860700016|376014|6133799003|22082099003|18002099003|10067099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: haCv/p4CVrOmXwA/Invw5PhQebNFxsixGuSEYrGjIyg2kstqRmTNP/RLvn1nXb/5NH2uIT6s0xhi02eAU9V3cfAkH9/+F6RVD7jzLdhbss9BJ6MFhhdIaSSp4bYUAYoW9WFxAktUh3SiwmGYhSkmYm8JvcGpXcxdJcqDvMbdd72vkEBTe7xcYk+AhvoLp+7i1cM9O8PVzRCmv+eGVtBug2GxczpQfkU9N35rV8GknkeBw9gx+mnEbhWn4iE8LZzq7RECgXluvU8XmEiTRaXDyVLxN4wyoRNk3aWF+bPjC/zK0ARLRQCrL+irPjlQ0bowFN+VzG1bY6S//sJ1ABKVIYyxcNUNqwj+OpL7y9iI6Vkzjfq4f2L4PdU6jWR4nkzYr7l5gO8zqlBzF5YiYhUvgo+uKKqXG3BY1nopvJGxODSoWMHFCbWef0IoEdZ3lMBjegeM++BtI68aPccsSYMqUA0vffJTAMNL37pNWd2Beui4NUhNb8Q+u+6aYRlxW9doppKqyTpDV6T4jI8KM8JP/+2FdaFPi1bHP7dpzsHMdzHQVVACdyqHS1YXgCKVCPt+c2n+qLg0m7nXxqaQk+sUCkFpLLNJKM9arHXJoOPg+MjNwVmPC1nUeLwxKTV76O5fvi8yut0tE1sBNNX+Cnui/tDPKHpPqnuVYPecXSoaMqkyfoixfF3xJ/H/xOEuNQxHKiJqpQj+6dv3Qx/xv5UUsw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(7416014)(1800799024)(82310400026)(36860700016)(376014)(6133799003)(22082099003)(18002099003)(10067099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uTRJIFyb4Oe8/ZeliCGecQAIJ3m50mTVu70Dw1DGeyg4KgjGbBi/hTbCWzCcvBDBIbhsKc/MsP7taC0KYlRat78GGZeSq3U1QMnZJnPnhtl/ZlUeDqcUVHfPC3uCR5vaybeTZz2A4qq+V3YWjPSTbnlOwPyIBw/KVi1ocmEX2fO/K2vR7H8stKBMkBJQDY3XfjlY14t8xzSwDWWEP+rkHqIanA7YUIVZaLm17JhaWnoVjaArRV5vgUx5/y1G5Pn1IIhiGVW4fnc3OXvF2q7cLK/32Bean8rQdnAIOnHrpstDIF/jKkK/h3X8Wfqnl5K/XI8lX6tTM9DMimskxekDaYZuo1W9mTROY2aINQwNiw6XXzlxFlE9h60CPWvhYGDCh+Yhrh8M7UZQthlqitdPamNowP2XQCgPtdw0UJoOY3+JweIGB9lkwpCtsTwGgixP X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:40.4845 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 33bb282b-0928-4073-d9bc-08df1290cb4c 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: BL02EPF00021F6D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7814 Content-Type: text/plain; charset="utf-8" Introduce a new enum iommu_viommu_type (IOMMU_VIOMMU_TYPE_AMD) for AMD vIOMMU along with the struct iommu_viommu_amd, which is used to initialize IOMMUFD vIOMMU instance when calling struct iommu_ops.viommu_init(). Also, hook up struct iomufd_viomu_ops.alloc_domain_nested to connect nested domain allocation with AMD vIOMMU implementation. Only report a non-zero viommu size when the type is AMD and amd_iommu_viommu_enabled() is true for the device's IOMMU. Additional initialization will be added in subsequent patches. Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/iommufd.c | 10 ++++++++++ include/uapi/linux/iommufd.h | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index 52300b867c1f..e8e7807f2a33 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -34,6 +34,15 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *lengt= h, enum iommu_hw_info_ty =20 size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_t= ype viommu_type) { + struct amd_iommu *iommu; + + if (viommu_type !=3D IOMMU_VIOMMU_TYPE_AMD) + return 0; + + iommu =3D get_amd_iommu_from_dev(dev); + if (!amd_iommu_viommu_enabled(iommu)) + return 0; + return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core); } =20 @@ -73,5 +82,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_vio= mmu *viommu) * struct iommufd_viommu_ops - vIOMMU specific operations */ static const struct iommufd_viommu_ops amd_viommu_ops =3D { + .alloc_domain_nested =3D amd_iommu_alloc_domain_nested, .destroy =3D amd_iommufd_viommu_destroy, }; diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 206fa667c782..e1080b997e8f 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -1095,6 +1095,7 @@ struct iommu_fault_alloc { * @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type * @IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension for= ARM * SMMUv3) enabled ARM SMMUv3 type + * @IOMMU_VIOMMU_TYPE_AMD: AMD HW-vIOMMU type */ enum iommu_viommu_type { IOMMU_VIOMMU_TYPE_DEFAULT =3D 0, @@ -1105,6 +1106,7 @@ enum iommu_viommu_type { * VMM must wire the HYP_OWN bit to 0 in guest VINTF_CONFIG register */ IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV =3D 2, + IOMMU_VIOMMU_TYPE_AMD =3D 3, }; =20 /** @@ -1123,6 +1125,14 @@ struct iommu_viommu_tegra241_cmdqv { __aligned_u64 out_vintf_mmap_length; }; =20 +/** + * struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD) + * @out_vfmmio_mmap_offset: (out) mmap offset for vIOMMU VF-MMIO + */ +struct iommu_viommu_amd { + __aligned_u64 out_vfmmio_mmap_offset; +}; + /** * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) * @size: sizeof(struct iommu_viommu_alloc) --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010045.outbound.protection.outlook.com [52.101.61.45]) (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 B25A839F18A for ; Mon, 14 Sep 2026 18:48:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.45 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411738; cv=fail; b=DQLcr2SjAwyDzBMRWHwbNqZCR2YniXrxbcESDEMwHc4bmq13jXeARsEoUZyrkO345ZcUUZt5j5HS972ZEG/pf8F2JqbZNNJlCb8xGsHPUV0Ir5o2gnleHXgxFPLvwXcBy2v4ov0cbf3hyfTdCYJ4EmaK6EbvWF2xC0rOyEkdwaA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411738; c=relaxed/simple; bh=h9hPEstwnPX73bbaNjUlO+TJPWmhB/fx8KehDfHKVE4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OgKOd5zFyugHr5/Npc3TlMwtSM8TxesJQDG23sz4GW22vdYDq8VTliLKhzltRUhSPvW1jiqYow+0iWk9TgbVpQ0O7NRCWlmj3vBgpGQpNWcaJDGsiw/PHw6ICBluFoyaTeGmNK9Tvmq9uJYTC5IrvOXyqc/gdW1fDTy6PXVKmW0= 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=Wsb5gPFN; arc=fail smtp.client-ip=52.101.61.45 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="Wsb5gPFN" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JzzV8eMa3IdRvR/QjTxP/MvKrIkoG1XdArmVJTpuhCIQ6vevRGwgaI4PpJ2K8RuTSj1jdIluN60qSuOpEBH5UzakCKU0cH5iQR4oH7E4BLxYBjNlkzOcSnnhAtPnxsSE9wFS9fNAwC8DQmOF5pZaD9ZGWLlQVu0EkpQIZhTLlXG/4X5CYBUnixhliL+Gc6PCSMIF9Zm40W7MkqN2JTX5KIcqRAU5EPuErCr6UA1A0ORFLxqPEqTJ4cAyjKSxGA2wl4fyeTKbKhMA5WR2kvgONsr9OJOIo0uQw+Vy+ybg1LpoTzpM3sTy8T3c07r0Ad5PEP822Lg3uA0al9Z/lzrqRw== 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=a7jGhohnvJ5oSUeaMh8uYvPNW9me3d+YV7TXMn79h1Y=; b=Nr3veHpQA0/sEvdiKqf3bNmKAn7F/ClDzLoCiMa8dOCXJe+hGRlHbNx8b1yUH/+pRk/RAzZKESOe2e6yp3+SAfTW1llTMgoCRoJy5jWLalrflW9RjirQcjkA6Do8dbIIr/TNC63VA7rqLaDp8kai3FyvamZWynC0ExY8pT/MYZ9PVyHs21yvlZ8JAwSoRKLy30pMRAsCzclFHUG/DDT8AtbuzctluWldyRjUN5O4evW64fMIkiGuVe/cHGANZGcj6ZAZh/7JgzUYIWdqZs8iUQMYB0R1iXh80jUoSzdPR95gPMpcydk4Qvx1Ou+1YdlO+/HyI3INNlSEBpv0XMEeCA== 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=a7jGhohnvJ5oSUeaMh8uYvPNW9me3d+YV7TXMn79h1Y=; b=Wsb5gPFN5i5kCBBmlyhmYoJ9yTnGB2AJ5VKrgFzZoRN2KeqqexlVwNTUUwk+wqTRGcFtyfbmxPkYHe+bMPMgLWEWiOxxJodtGexoH5nY9+UToHvbc41lF7eI4BD8aST4es/zVB/G00XKDCIOwJP9JYF3MLzfzLcYBVGRoyIPhC0= Received: from BN9P222CA0027.NAMP222.PROD.OUTLOOK.COM (2603:10b6:408:10c::32) by SN7PR12MB8028.namprd12.prod.outlook.com (2603:10b6:806:341::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:48:47 +0000 Received: from BL02EPF00021F6D.namprd02.prod.outlook.com (2603:10b6:408:10c:cafe::d) by BN9P222CA0027.outlook.office365.com (2603:10b6:408:10c::32) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:47 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6D.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:47 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:40 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 05/24] iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances Date: Mon, 14 Sep 2026 18:47:31 +0000 Message-ID: <20260914184750.222939-6-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6D:EE_|SN7PR12MB8028:EE_ X-MS-Office365-Filtering-Correlation-Id: 034e3151-e83a-4e85-cf2c-08df1290cf83 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|7416014|23010399003|82310400026|36860700016|22082099003|18002099003|56012099006|11063799006|5023799004|10067099003; X-Microsoft-Antispam-Message-Info: h+KEyujVKyNDE+LAJNcFo+ZtkZGUPG/YMEQ5S+FwF6GPNfdbIUIn92cJH0282um1XkSrKdkl9ygLOvJnkLnuW9u+NHBwntMXfIOniOtBDOMDLvjpO3TLRzZva2AzCCGH2+W/YQyQ4BIwdb6SM9pl8Pxi7o7YsEIUcw+x1TpMKdIBfIibrukyj254lvRqAILdznZZBSLjXxJoz/ptEvELg3R0S1xGFpYZLNwkuAyEKQiN608k6bwO0d+bt/cObdAEiqm1mXZtOtru0iZ6IENBncZTu7XgF+HanQlPQsmTQm5bZxBMztxmj4TNlqFvYn0n6+TZd9rGHGf1m4dxhqdd0vCdmJwLmnekK65QFkQex8LO7XV8zRNMehozzWaR8SGO2qJL+9aLSjA1IwuPyw7gO5Sb92XSOAR+y/y/5O1rIt1sLEurg6pzhNkIs7qEvRxIorq0gxmQv5UQEwJnjXwuJ6O94Gro/6GiGNU1LHxw7cChmHmdc42vYbTRz6H592zx4EIGeHcrMfHoBMbq4ockYYr56IASNpKomCyEPPtBIWDGFEeRJW6d3WQZ3E1kDYdRwtRenVjTykDDGwX9KSaC5SFKxCkRZP3FFxt3SVu/BfF0m25lOW72edyUknX4mP9Yeisl1pw4Pb6f3odBlDZB7LX6o0Fj67lDqOim82X4++9864s+0IyuuiEWhfxf0wBWwomw9hAqPU5xuMTxaGZ9ng== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(1800799024)(7416014)(23010399003)(82310400026)(36860700016)(22082099003)(18002099003)(56012099006)(11063799006)(5023799004)(10067099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nIZvUh9vryKExmqNURMROMGrLF66rWz/rr8RI9cAtbAfc5DFOBF/RnaMGOhYahshASrYAI19EJXevcw1L5UtY7fozRAKY7qOIttJamkxALXskc+mhsOs6z7dZ3DywzN7NKRFMK0Ywg4ySYL5J8/Yf7w6tPmNxvxAvos4cOW1zJ9w+3wmCRsa3FNsasHFsX/oJP9tMbuDoB5xsxeBQnDGoggHZAw1PYqHyZsRw+NyZO+x/OlBT6kcjfEwneYioiPlMM+H11WZsJLgZj/PNZb+W1E2SLxxz8MVKXXNq+THMQlKo+Gr9uZagpStPq5UHvF+wogo/hQtSFSXthUexcT1okLaLWIG5PgDtxJ94IZmg0r0IDE/oZbu1+Gyp3N5QHf1eGJn+VrcEvT1U5MzAEfuqtdiFGpQ6vPXaGPC+xf4/K9ejGsLNPfFRwzGvt0znFf/ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:47.5562 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 034e3151-e83a-4e85-cf2c-08df1290cf83 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: BL02EPF00021F6D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8028 Content-Type: text/plain; charset="utf-8" Hardware vIOMMU uses a 16-bit Guest ID (GID) per guest IOMMU to index driver and hardware state. Allocate one GID per IOMMUFD vIOMMU from a per-amd_iommu IDA (unique within that IOMMU; a VM behind multiple IOMMUs may hold more than one GID). Add amd_iommu_gid_alloc() and amd_iommu_gid_free(), store the ID in amd_iommu_viommu::gid, and call them from amd_iommufd_viommu_init() and destroy. Initialize gid_ida in init_iommu_one() and destroy it in free_iommu_one() with the IOMMU object. Reject amd_iommufd_viommu_init() with -EOPNOTSUPP when the IOMMU does not have AMD_IOMMU_FLAG_VIOMMU_EN. Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 4 ++++ drivers/iommu/amd/amd_iommu_types.h | 7 +++++++ drivers/iommu/amd/init.c | 2 ++ drivers/iommu/amd/iommu.c | 16 ++++++++++++++++ drivers/iommu/amd/iommufd.c | 14 ++++++++++++++ 5 files changed, 43 insertions(+) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index b2ae17e3df34..f6cf412a9820 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -227,4 +227,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_dat= a, struct dev_table_entry struct iommu_domain * amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags, const struct iommu_user_data *user_data); + +/* Guest ID for vIOMMU */ +int amd_iommu_gid_alloc(struct amd_iommu *iommu); +void amd_iommu_gid_free(struct amd_iommu *iommu, int gid); #endif /* AMD_IOMMU_H */ diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 688cd9240790..8c38775ff5c1 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -21,6 +21,7 @@ #include #include #include +#include =20 #include =20 @@ -404,6 +405,9 @@ =20 #define MAX_DOMAIN_ID 65536 =20 +/* Guest ID bits 0-14; bit 15 is reserved for secure vIOMMU. */ +#define VIOMMU_MAX_GID 0x7FFF + /* Timeout stuff */ #define LOOP_TIMEOUT 100000 #define MMIO_STATUS_TIMEOUT 2000000 @@ -501,6 +505,7 @@ struct amd_iommu_viommu { struct iommufd_viommu core; struct protection_domain *parent; /* nest parent domain for this viommu */ struct list_head pdom_list; /* For protection_domain->viommu_list */ + u16 gid; /* Guest ID for the vIOMMU */ =20 /* * Per-vIOMMU guest domain ID to host domain ID mapping. @@ -760,6 +765,8 @@ struct amd_iommu { /* IOPF support */ struct iopf_queue *iopf_queue; unsigned char iopfq_name[32]; + + struct ida gid_ida; /* guest IDs for this IOMMU */ }; =20 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev) diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index c80872ce0b60..7b4cb473019d 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -1797,6 +1797,7 @@ static void __init free_iommu_one(struct amd_iommu *i= ommu) free_ga_log(iommu); iommu_unmap_mmio_space(iommu); amd_iommu_iopf_uninit(iommu); + ida_destroy(&iommu->gid_ida); } =20 static void __init free_iommu_all(void) @@ -1885,6 +1886,7 @@ static int __init init_iommu_one(struct amd_iommu *io= mmu, struct ivhd_header *h, =20 raw_spin_lock_init(&iommu->lock); iommu->cmd_sem_val =3D 0; + ida_init(&iommu->gid_ida); =20 /* Add IOMMU to internal data structures */ list_add_tail(&iommu->list, &amd_iommu_list); diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index c34a0f5a9540..fa9708af258e 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -257,6 +257,22 @@ static inline bool pdom_is_sva_capable(struct protecti= on_domain *pdom) return pdom_is_v2_pgtbl_mode(pdom) || pdom_is_in_pt_mode(pdom); } =20 +int amd_iommu_gid_alloc(struct amd_iommu *iommu) +{ + int ret =3D ida_alloc_range(&iommu->gid_ida, 1, VIOMMU_MAX_GID, GFP_KERNE= L); + + if (ret >=3D 0) + pr_debug("%s: iommu devid=3D%#x, gid=3D%u\n", __func__, iommu->devid, re= t); + + return ret; +} + +void amd_iommu_gid_free(struct amd_iommu *iommu, int gid) +{ + pr_debug("%s: iommu devid=3D%#x, gid=3D%u\n", __func__, iommu->devid, gid= ); + ida_free(&iommu->gid_ida, gid); +} + static inline int get_acpihid_device_id(struct device *dev, struct acpihid_map_entry **entry) { diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index e8e7807f2a33..69a0e61bf83d 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -49,13 +49,23 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, = enum iommu_viommu_type vi int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_do= main *parent, const struct iommu_user_data *user_data) { + int ret; unsigned long flags; struct protection_domain *pdom =3D to_pdomain(parent); struct amd_iommu_viommu *aviommu =3D container_of(viommu, struct amd_iomm= u_viommu, core); + struct amd_iommu *iommu =3D container_of(viommu->iommu_dev, struct amd_io= mmu, iommu); + + if (!amd_iommu_viommu_enabled(iommu)) + return -EOPNOTSUPP; =20 xa_init_flags(&aviommu->gdomid_array, XA_FLAGS_ALLOC1); aviommu->parent =3D pdom; =20 + ret =3D amd_iommu_gid_alloc(iommu); + if (ret < 0) + return ret; + aviommu->gid =3D ret; + viommu->ops =3D &amd_viommu_ops; =20 spin_lock_irqsave(&pdom->lock, flags); @@ -70,11 +80,15 @@ static void amd_iommufd_viommu_destroy(struct iommufd_v= iommu *viommu) unsigned long flags; struct amd_iommu_viommu *aviommu =3D container_of(viommu, struct amd_iomm= u_viommu, core); struct protection_domain *pdom =3D aviommu->parent; + struct amd_iommu *iommu =3D container_of(viommu->iommu_dev, struct amd_io= mmu, iommu); + + pr_debug("%s: gid=3D%#x, iommu devid=3D%#x\n", __func__, aviommu->gid, io= mmu->devid); =20 spin_lock_irqsave(&pdom->lock, flags); list_del(&aviommu->pdom_list); spin_unlock_irqrestore(&pdom->lock, flags); xa_destroy(&aviommu->gdomid_array); + amd_iommu_gid_free(iommu, aviommu->gid); } =20 /* --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013057.outbound.protection.outlook.com [40.93.201.57]) (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 7D9A338F65D for ; Mon, 14 Sep 2026 18:49:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.201.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411746; cv=fail; b=kkVcwUKoqUKLWE2TqTRL8hWTr9q8VD6/DXamRyKltEjD4KFVYdjb70nvcisJSLe5cH8/9GuxO/QLr4Irkl2NPpMtmypTPqhvxn0Ix8/BDte2kWluh59OUVy36ire3WPoTtQhb5epQSeu4P23lPFQNJ8JeWdIWHVEiXpxgnJwn9A= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411746; c=relaxed/simple; bh=buxR8Sh/dfom31lAgiKQMSyY6K8ANoBr5LA1MvF+a8o=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CMYdUUfEcdX4dbvn4p6hShGlAt7laqdrZsIJy9FuaByUR2Y2tpiVtz6+XGi0r57SYN9XYiZSKDoXuq1WljYLUPZblAhowSUmfol7c9PSDKpbSZVx1tOdM55ZYGPzH/174zSymx2mg5gLmYsEHs68o5MXXeKhc+eAOkI+33lPvjY= 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=Ea1o5tGJ; arc=fail smtp.client-ip=40.93.201.57 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="Ea1o5tGJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=PtjCfAiW3ObllYzeaaTUA2c3X/lJIm3S4vfkwsG78/H8x1N6zMcCnqhZBYNPkfSsSXpbQ2rKRP92qSCwDvk3cHa3nG9wm4/laPxuK4WnYMttcs2hmGx+FDcPhlvzKOnQPUamk9ZmU/QJ4pahoiCfEzJQRJo9ulECcrSL7VRsQHus+geiTNSZPy/UWRwFQPfiCtKNJDuPvVAl8V6bwxK0DExsmZWS4mfTmF6BD143t4aEYKvtUSzQfqX88XkanyShVQmFEDuxeH2JVY77k5aGWHFgsG7WyicEYN3P6Xi60gB25Z179wYjjx0mdOL2bjEJoMC0cl2nSrJwvj2EXVYrCQ== 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=v2/dnfXQ3sPpwBTlBzVy80W9kyioQB+J6+/fxQ44f70=; b=yS11r2iisVLr5q+rDCipt9wdIqisSaSki+JfuQtxwup5IWU8qptBaQ8woGng3kqvSJW8KQ1cajmH1K37qK0vZ6hUz3I1lwKkgbRTEK7ocJEZrqrsli+JdJ7jjt4dYPzqQaDLp8KAzx0c2cOGPV9pKn6ZBbuazlU+KI4pJfs8lEtyFEFB22qkN35gZY7TLOlHGea9GT958RoyxX2vJE5vkmYn7/pPKZA/k2rZBPDA/E2WB3LMaRqFT1UCwhIusFvU/HuXAH2QmGSdaC03jAR9GM0FmfgU1lJaKunjx2/dczTvn4nxeO7qCvZft9cQfWGAuDPFHIZnakUI/GFpPQhZ5Q== 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=v2/dnfXQ3sPpwBTlBzVy80W9kyioQB+J6+/fxQ44f70=; b=Ea1o5tGJ8CUFmnIKaJMgW+x4u2TWygmFvtUE4AtvoKqdgSSSQn4InU+9QaY3Ml7fmo6CXQg2A1THNe6aZLrd1zgaSIUvST6nqSawE9I/hpBz6Mw+l6xvDFyQha+v5X/D4+DWvjSUsUVQKWdR+ZZlSrW7vrFNHi1D0x8BuYrtPX8= Received: from BN9PR03CA0288.namprd03.prod.outlook.com (2603:10b6:408:f5::23) by DM4PR12MB5940.namprd12.prod.outlook.com (2603:10b6:8:6b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:48:53 +0000 Received: from BL02EPF00021F6B.namprd02.prod.outlook.com (2603:10b6:408:f5:cafe::a5) by BN9PR03CA0288.outlook.office365.com (2603:10b6:408:f5::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:52 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6B.mail.protection.outlook.com (10.167.249.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:52 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:46 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs Date: Mon, 14 Sep 2026 18:47:32 +0000 Message-ID: <20260914184750.222939-7-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6B:EE_|DM4PR12MB5940:EE_ X-MS-Office365-Filtering-Correlation-Id: 37fc9b19-e09a-4740-da8a-08df1290d2a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|82310400026|7416014|36860700016|23010399003|6133799003|22082099003|18002099003|11063799006|10067099003|56012099006; X-Microsoft-Antispam-Message-Info: r8H74SQrKN90LZA2sQAnIWTGVHS25il5YJhA4M+iqYDZHcY9ncLK3giPXbWPWRUg3x5buoQrv7oBhbKBq6vzjMlpa+kVe0Fb6ax0KfV61MWls25eNnM0ELeix68Pmw7eMdNJJ1JN6EL5mIF3XT09hssIwqQwMxQsnqMiBUaBAjfddIG0GTIGBlzt8wXwD72L31mVdNdUHqBPT3PnjTD3Il2brwG4tohUwr/T3W9Bjwm11MnbB9w3ktxfpFTI+xvdxSmGDrDOdrGy/hvROXz+6/wp5nch9OotMkojamoIu1r+FWRP6rzNAAIKhV91QDroBOhG1j5S7CZ9YelrvFNPiMg18gcTtugyqCJS4aXcfKkwpPyQG5fGuueEuPTPzmlsy+dh0SfAyUd9ShS1fm5Sofs4lM4g+b066UUPRNNky03L3QhfQJ4qWjSEnm29/47WP+2cDHSxgi/sEhYjCwx07z9WH6vVkNxl9xKeXJOyV84zUQiN4RK28A60X+68Bcqf+PhSGqA+pGbxTpKpRoX82JMDrclyTTikC7RoX+2PS1iA+/Da9oIJOsV2N03rYSVD1xR4An2YzkkFmGP5FwMIuHQ0SDZMXaVJIgUhsOhEyWmWytgI/eOS1kLlYSSKO45MrMIZdGPzHSFBIqz6n5lOIqWE9mDD8t+ygCiRX7jNexKfZRXxXKyaan5T/4SPTefYOUCrAE3evFXDVzFpsGGRBw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(82310400026)(7416014)(36860700016)(23010399003)(6133799003)(22082099003)(18002099003)(11063799006)(10067099003)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ODmGxdFn+4E5cqdSllYZxbupoowuAJXwyuqfTb0OvT+EYHDmiVh4QGK/xCbSltwuM10K+FaiQvjkrzVQoy7kCA0djXK1cOMoQNX1HKJp4hLh/cJk6ayla8I2GaI5wcn9uORw785LgZfuPt6mgFugwlgltq7XbzybfIaWIPejKPYfZpOT4pH8Ia7eH6yPMpwUjiYtDJKSu108V5TgvDcaoFs39NKnH/t/D3fkH/2R110F+X4s63BT2UAKwQicasuR98gtsq8TMPx6UQIHAYyo70I561F0M1tBjCwIE177JiMxOBqutRU+wBBcaUSFDEEpUZ2JV9ZY24GgpveTWCfvnutoOdRVNaFYXcdXliACepyyeAaOnEc2I/ON7+oxo8M+iQHYfV+2KtlTZMic2U+S2ZrLZdtnZv/66FYI5sP7qPi4LBRDUL3TxYAJsGpx2fuF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:52.8314 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 37fc9b19-e09a-4740-da8a-08df1290d2a8 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: BL02EPF00021F6B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5940 Content-Type: text/plain; charset="utf-8" Enable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control). VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch. Reject a VSC VF or VF Control BAR that is disabled or decodes to physical address zero before mapping MMIO. Set AMD_IOMMU_FLAG_VIOMMU_EN after the VF BARs are mapped. Export MMIO map helpers. Call amd_viommu_uninit() from IOMMU teardown before free_iommu_buffers() so MMIO and the command buffer remain. Release the reserved MMIO region if ioremap() fails so VF/VFCTRL map errors do not leak request_mem_region() for the life of the machine. Signed-off-by: Vasant Hegde Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 2 + drivers/iommu/amd/amd_iommu_types.h | 31 +++++++++ drivers/iommu/amd/amd_viommu.h | 6 ++ drivers/iommu/amd/init.c | 14 ++++- drivers/iommu/amd/viommu.c | 98 +++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index f6cf412a9820..51e4364e8564 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -28,6 +28,8 @@ void amd_iommu_set_rlookup_table(struct amd_iommu *iommu,= u16 devid); void iommu_feature_enable(struct amd_iommu *iommu, u8 bit); void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu, gfp_t gfp, size_t size); +u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end); +void __init iommu_unmap_mmio_space(struct amd_iommu *iommu); =20 #ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup(void); diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 8c38775ff5c1..c2257d48e222 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -40,6 +40,12 @@ #define MMIO_RANGE_OFFSET 0x0c #define MMIO_MISC_OFFSET 0x10 =20 +/* vIOMMU Capability offsets (from IOMMU Capability Header) */ +#define MMIO_VSC_VF_BAR_LO_OFFSET 0x08 +#define MMIO_VSC_VF_BAR_HI_OFFSET 0x0c +#define MMIO_VSC_VF_CNTL_BAR_LO_OFFSET 0x10 +#define MMIO_VSC_VF_CNTL_BAR_HI_OFFSET 0x14 + /* Used offsets into the MMIO space */ #define MMIO_DEV_TABLE_OFFSET 0x0000 #define MMIO_CMD_BUF_OFFSET 0x0008 @@ -464,6 +470,20 @@ extern bool amdr_ivrs_remap_support; #define for_each_ivhd_dte_flags(entry) \ list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list) =20 +/* VIOMMU stuff */ +#define VIOMMU_VF_MMIO_ENTRY_SIZE 4096 +#define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE 64 + +/* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */ +#define VIOMMU_VF_MMIO_MAP_SIZE 0x10000000UL +#define VIOMMU_VF_CNTL_MMIO_MAP_SIZE 0x400000UL + +#define VIOMMU_VF_MMIO_BASE(iommu, guestId) \ + (iommu->vf_base + (guestId * VIOMMU_VF_MMIO_ENTRY_SIZE)) + +#define VIOMMU_VFCTRL_MMIO_BASE(iommu, guestId) \ + (iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE)) + struct amd_iommu; struct iommu_domain; struct irq_domain; @@ -678,6 +698,17 @@ struct amd_iommu { */ u16 cap_ptr; =20 + /* + * VF MMIO base physical address. This is needed to calculate/pass + * per guest VF MMIO address (3rd 4K of IOMMU MMIO space) + */ + u64 vf_base_phys; + u64 vf_cntl_phys; + + /* virtual addresses of vIOMMU VF/VF_CNTL BAR */ + u8 __iomem *vf_base; + u8 __iomem *vfctrl_base; + /* pci domain of this IOMMU */ struct amd_iommu_pci_seg *pci_seg; =20 diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index 864e7b283cff..e5fbea666e01 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -12,6 +12,8 @@ struct amd_iommu; =20 int amd_viommu_init(struct amd_iommu *iommu); =20 +void __init amd_viommu_uninit(struct amd_iommu *iommu); + #else =20 /* @@ -24,6 +26,10 @@ static inline int amd_viommu_init(struct amd_iommu *iomm= u) return 0; } =20 +static inline void amd_viommu_uninit(struct amd_iommu *iommu) +{ +} + #endif /* CONFIG_AMD_IOMMU_IOMMUFD */ =20 #endif /* AMD_VIOMMU_H */ diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 7b4cb473019d..79386816807a 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -459,8 +459,10 @@ static void iommu_disable(struct amd_iommu *iommu) * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMM= U in * the system has one. */ -static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end) +u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end) { + u8 __iomem *base; + if (!request_mem_region(address, end, "amd_iommu")) { pr_err("Can not reserve memory region %llx-%llx for mmio\n", address, end); @@ -468,10 +470,14 @@ static u8 __iomem * __init iommu_map_mmio_space(u64 a= ddress, u64 end) return NULL; } =20 - return (u8 __iomem *)ioremap(address, end); + base =3D ioremap(address, end); + if (!base) + release_mem_region(address, end); + + return base; } =20 -static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu) +void __init iommu_unmap_mmio_space(struct amd_iommu *iommu) { if (iommu->mmio_base) iounmap(iommu->mmio_base); @@ -1792,6 +1798,8 @@ static void __init free_sysfs(struct amd_iommu *iommu) static void __init free_iommu_one(struct amd_iommu *iommu) { free_sysfs(iommu); + /* Tear down vIOMMU before IOMMU buffers and MMIO are released. */ + amd_viommu_uninit(iommu); free_iommu_buffers(iommu); amd_iommu_free_ppr_log(iommu); free_ga_log(iommu); diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 53f3302f7cc6..7e6c4412ff20 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -18,8 +18,106 @@ #include "amd_iommu_types.h" #include "amd_viommu.h" =20 +static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) +{ + if (iommu->vfctrl_base) { + iounmap(iommu->vfctrl_base); + iommu->vfctrl_base =3D NULL; + } + if (iommu->vf_cntl_phys) { + release_mem_region(iommu->vf_cntl_phys, VIOMMU_VF_CNTL_MMIO_MAP_SIZE); + iommu->vf_cntl_phys =3D 0; + } + + if (iommu->vf_base) { + iounmap(iommu->vf_base); + iommu->vf_base =3D NULL; + } + if (iommu->vf_base_phys) { + release_mem_region(iommu->vf_base_phys, VIOMMU_VF_MMIO_MAP_SIZE); + iommu->vf_base_phys =3D 0; + } +} + +void __init amd_viommu_uninit(struct amd_iommu *iommu) +{ + iommu->flags &=3D ~AMD_IOMMU_FLAG_VIOMMU_EN; + amd_viommu_vf_vfcntl_unmap(iommu); +} + +static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) +{ + u32 lo, hi; + u16 vsc; + u64 vf_phys, vf_cntl_phys; + + vsc =3D pci_find_capability(iommu->dev, PCI_CAP_ID_VNDR); + if (!vsc) + return -ENODEV; + + pr_debug("device:%s, vsc offset:%04x\n", + pci_name(iommu->dev), vsc); + + /* Setting up VF and VF_CNTL MMIOs */ + pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_BAR_LO_OFFSET, &lo); + pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_BAR_HI_OFFSET, &hi); + vf_phys =3D hi; + vf_phys =3D (vf_phys << 32) | lo; + if (!(vf_phys & 1)) { + pr_err(FW_BUG "vf_phys disabled\n"); + return -EINVAL; + } + vf_phys &=3D ~1ULL; + if (!vf_phys) { + pr_err(FW_BUG "vf_phys address is zero\n"); + return -EINVAL; + } + + pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_CNTL_BAR_LO_OFFSET, &= lo); + pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_CNTL_BAR_HI_OFFSET, &= hi); + vf_cntl_phys =3D hi; + vf_cntl_phys =3D (vf_cntl_phys << 32) | lo; + if (!(vf_cntl_phys & 1)) { + pr_err(FW_BUG "vf_cntl_phys disabled\n"); + return -EINVAL; + } + vf_cntl_phys &=3D ~1ULL; + if (!vf_cntl_phys) { + pr_err(FW_BUG "vf_cntl_phys address is zero\n"); + return -EINVAL; + } + + /* Mapping 256MB of VF and 4MB of VF_CNTL BARs */ + iommu->vf_base =3D iommu_map_mmio_space(vf_phys, VIOMMU_VF_MMIO_MAP_SIZE); + if (!iommu->vf_base) { + pr_err("Can't reserve vf_base\n"); + goto err_out; + } + iommu->vf_base_phys =3D vf_phys; + + iommu->vfctrl_base =3D iommu_map_mmio_space(vf_cntl_phys, VIOMMU_VF_CNTL_= MMIO_MAP_SIZE); + if (!iommu->vfctrl_base) { + pr_err("Can't reserve vfctrl_base\n"); + goto err_out; + } + iommu->vf_cntl_phys =3D vf_cntl_phys; + + pr_debug("%s: IOMMU device:%s, vf_base:%#llx, vfctrl_base:%#llx\n", + __func__, pci_name(iommu->dev), vf_phys, vf_cntl_phys); + return 0; +err_out: + amd_viommu_vf_vfcntl_unmap(iommu); + return -ENOMEM; +} + int __init amd_viommu_init(struct amd_iommu *iommu) { + int ret; + + ret =3D viommu_vf_vfcntl_init(iommu); + if (ret) + return ret; + iommu->flags |=3D AMD_IOMMU_FLAG_VIOMMU_EN; return 0; } --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013056.outbound.protection.outlook.com [40.93.201.56]) (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 81C4D391832 for ; Mon, 14 Sep 2026 18:49:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.201.56 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411748; cv=fail; b=RrGSKu1UlIymhTUo7VZk7ZH3iyGQdd//EfvFZGKIm/LlvhKwRodx2TFdDtYyxmyQ8cXuX7BMhSUDco0pNODJGcFrbGS4VGNZqoU7lTPE1tYur989G1M+Gq3iSLzeZMFGJ/d/QHqpSeNrptfE9F8BcIm4cj4Fstyx44DBQ9fl1Jo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411748; c=relaxed/simple; bh=N+i0zLYYDNQtrz7JB9yYzCOp+nUlxxehdaKL5iOadxo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nxgzj0ZzVLh5+nwWXVV1Ao8ywiBw6MfX4zux6xkIvPeM8Nl13BjkQo3TkkOAVlp1Z15VOSD0VMiDHmrdENvauneLHXPgpCk1prwIFo/zGuZhDbvzGiaTmWsxvcDMSo2+wH8bDBenpZXjV5A/aWYtCCGX2DnY64SnClJk4LIL/08= 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=WNRxbM76; arc=fail smtp.client-ip=40.93.201.56 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="WNRxbM76" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nE/zqg3zp9TWeME0qCElxWrcNq7WqhE44zrIv4x7p5CITPV77DzDQVNxhv27UOvViP8XyZQrkXoEMZpNe8dP9s1zHfgWzvqwI3dyaN4+se8fW7sw7VjxxicDliFrmU61FyP7gKkzrTSdrG6p5rglbuds4giU4OstCawMdcw9TWpfKHCSxUjVDhkIlGbxLg2b1LfiwdsABgN13s4rnm4OZLGm/ADMGNp9d6y70iDn784t+G9GRJD/95O00mfQpOiTpSsYXK6xCjWYIufCETq/Tp+YGfi7Ki4nPo/D6jtvJuG3gDOGF6AsOlMWneiZquTmeYmDFpMcLAb3ibW9HIe0ow== 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=tESWOykCYz+cJPGfENWWIo6x48UBaI74NZ9GWATTCpg=; b=nxw5Xxu+m0rL64JpH3cgOcYkd/LMS9j+D1NCz9dzeW/BGJn43Y/Viy4LR1n/iVJZnmxjMahilVrVh6s7pJ9uS4shChg1cNBkFgvoojodoPSRnl0sLxCZaisDCyNrhxepX7GsPKld+YocPH0naGVgoIyC0e1Q2qHZxumEMqpP5/PFeNfbZQnlWcXqZ3w3umT+piE8w2psWDQWdIUi06gyYhhEUQPmjRZvXhLDeeTPHK3hexVIsaOSWhQg4SqXk33QubbvlPRjzIexWZDQbKs00dnB04O73MpmXFnM9EMuhGptzBJFWkYlf0Fus6yPcBuryzHhmkgpHTiJGuNy6XHb5g== 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=tESWOykCYz+cJPGfENWWIo6x48UBaI74NZ9GWATTCpg=; b=WNRxbM76OwPCKC1VZyeOcAQH22Jqov4xfD93/xAbSbPclVYQCxHXt4oKnI3tykpJ+6ck47xN6CNHFxYJ0LHVlhPCnJQo1z4bRmewYT6BO3mUDJNkUKUMHNcOHCGadz+7B5BMaF4CmBTtOZ01pXWWQYF/1zO4ju4QZJnrDQZbL34= Received: from CH0PR03CA0450.namprd03.prod.outlook.com (2603:10b6:610:10e::28) by PH6PR12MB354726.namprd12.prod.outlook.com (2603:10b6:510:3f7::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:48:59 +0000 Received: from BL02EPF00021F6C.namprd02.prod.outlook.com (2603:10b6:610:10e:cafe::81) by CH0PR03CA0450.outlook.office365.com (2603:10b6:610:10e::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:48:59 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6C.mail.protection.outlook.com (10.167.249.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:48:59 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:52 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit , Vasant Hegde Subject: [PATCH v5 07/24] iommu/amd: Add support for AMD vIOMMU VF MMIO region Date: Mon, 14 Sep 2026 18:47:33 +0000 Message-ID: <20260914184750.222939-8-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6C:EE_|PH6PR12MB354726:EE_ X-MS-Office365-Filtering-Correlation-Id: 3cde5812-e459-4269-ac13-08df1290d669 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|82310400026|23010399003|36860700016|18002099003|22082099003|10067099003|11063799006|56012099006; X-Microsoft-Antispam-Message-Info: JJ+TQSbggjo1L7RdKMj6atNSJAZa/xgAc8ZL7BYrVIlFXWEG5LIBaqVpfMBjwTyF7YFMH/P0ycbiuv7mhbgSyfKok8C6oS1VdsGvttHeZKRiJChC4Ndy9xFrl9ZiVEtRVhSjDJ+DOMyCcF8BTaTjtdFPajjVqpoHndQU2HAlUabPPoIpoiHuejASY/rM/kOvKVv+IMgYTSIh10lv9Y5iwg0wSzJSfBiHcnC4yVF1b8IIpf3YQ14W1DjdyQ0y5WWFPao8AK1swy3sb85rCxQRmHBM/RgjfNdIUGtihzfwkGkVtjwjLq6S6uXRtMTBfp8UAY0KkG9ZGRrXSK4d5Zgc/jJWqg7Jq6ogcU+5HJFrVB5HBH/ERv6ZvXha8VtC2IwiI5q93J98Iazxt6S6wkx+dCeBtJFwypbajULrHVPqUIxgbnPUlsH9Oo26b4NbjSftJKBLwbYkjQ7i7aqaBO1iJ2vuML3FbN8BUrowQtOrqd5U141g1wPwGv6qX44FrmAGcIv5St0s8rJFjTMDSQHYEoqVPr6M2oL/u89MYOJA07c0k9zaMR2ym5p2AhUo+UIfJ51dx//GL18eiSbnJGRKQlLxM4ORG9ynuPp6UaFBylWds9qekWt3Wh3SOyzAbbIBqmyb4B0JVLk/+kcTzY9DkSU6NjZMoR1g+lZavrFmguoNbzPcCSsBns3JMvoP7rbwQsy1LFBgxcjKdgqUXJFwcQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(82310400026)(23010399003)(36860700016)(18002099003)(22082099003)(10067099003)(11063799006)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: twCJDdOfTkHVJZue9HPXHHNu6ywfTzKGTNn+DHRmQBiCxFQ4Po9qBkRHmwV4oQ1EpH+LesMKGNIkoTx18Kr+8CwuN2c/RTtFuPhlSgbwiG8huN3fedQjUgAQPCxjEfAq/IMtv5hnL9ACSRltS5xG8FhC0K2hxmN/iDBwkFAgLBxJHgSGwpC3w1EKsfUQU/v3xXzh5dthzcWMw6AOA+tmS2btqEc7YxtJs+s2yu8ArH104jH/EV4Bj07JqihWe12lab+uFFvGecm4HA4k/nUw2oRWN01kSfA/eZKVIWXVDCXSqLphUrgytSgqITR7fdZ94KrozTAy361e7+Xdm7soVphWtQ6k2brUqh0R2o+r0G4AfvTR8m991QyasqHN8OgrFC8uI+KIif2SobqeJhsfZ8c7iGoMQQFJo7AX7KWrEP/tbcSJRha0IqCULTccqabM X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:48:59.1282 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3cde5812-e459-4269-ac13-08df1290d669 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: BL02EPF00021F6C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH6PR12MB354726 Content-Type: text/plain; charset="utf-8" The AMD vIOMMU virtualizes guest MMIO registers at the 3rd 4K region. This is achieved using the iommufd_viommu_alloc_mmap(). Copy struct iommu_viommu_amd from and to userspace so the VF-MMIO mmap offset is validated and returned on vIOMMU alloc. Return -EINVAL if the VF-MMIO page_base is zero so mmap is not registered at physical address 0. Reviewed-by: Jason Gunthorpe Co-developed-by: Vasant Hegde Signed-off-by: Vasant Hegde Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/amd_viommu.h | 7 +++++ drivers/iommu/amd/iommufd.c | 41 ++++++++++++++++++++++++++++- drivers/iommu/amd/viommu.c | 12 +++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index c2257d48e222..3aaaf69a1e7d 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -532,6 +532,9 @@ struct amd_iommu_viommu { * Indexed by guest domain ID. */ struct xarray gdomid_array; + + /* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mma= p(). */ + unsigned long vfmmio_mmap_offset; }; =20 /* diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index e5fbea666e01..276f2126290f 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -14,6 +14,8 @@ int amd_viommu_init(struct amd_iommu *iommu); =20 void __init amd_viommu_uninit(struct amd_iommu *iommu); =20 +u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid); + #else =20 /* @@ -30,6 +32,11 @@ static inline void amd_viommu_uninit(struct amd_iommu *i= ommu) { } =20 +static inline u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 = gid) +{ + return 0; +} + #endif /* CONFIG_AMD_IOMMU_IOMMUFD */ =20 #endif /* AMD_VIOMMU_H */ diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index 69a0e61bf83d..7f358e57efde 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -7,6 +7,7 @@ =20 #include "iommufd.h" #include "amd_iommu.h" +#include "amd_viommu.h" #include "amd_iommu_types.h" =20 static const struct iommufd_viommu_ops amd_viommu_ops; @@ -51,6 +52,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu= , struct iommu_domain * { int ret; unsigned long flags; + phys_addr_t page_base; + struct iommu_viommu_amd data =3D {}; struct protection_domain *pdom =3D to_pdomain(parent); struct amd_iommu_viommu *aviommu =3D container_of(viommu, struct amd_iomm= u_viommu, core); struct amd_iommu *iommu =3D container_of(viommu->iommu_dev, struct amd_io= mmu, iommu); @@ -58,14 +61,43 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viom= mu, struct iommu_domain * if (!amd_iommu_viommu_enabled(iommu)) return -EOPNOTSUPP; =20 + if (!user_data) + return -EINVAL; + + ret =3D iommu_copy_struct_from_user(&data, user_data, + IOMMU_VIOMMU_TYPE_AMD, + out_vfmmio_mmap_offset); + if (ret) + return ret; + xa_init_flags(&aviommu->gdomid_array, XA_FLAGS_ALLOC1); aviommu->parent =3D pdom; =20 ret =3D amd_iommu_gid_alloc(iommu); if (ret < 0) - return ret; + goto err_gid; aviommu->gid =3D ret; =20 + page_base =3D amd_viommu_get_vfmmio_addr(iommu, aviommu->gid); + if (!page_base) { + ret =3D -EINVAL; + goto err_mmap; + } + + ret =3D iommufd_viommu_alloc_mmap(&aviommu->core, + page_base, SZ_4K, + &aviommu->vfmmio_mmap_offset); + if (ret) + goto err_mmap; + + data.out_vfmmio_mmap_offset =3D aviommu->vfmmio_mmap_offset; + + ret =3D iommu_copy_struct_to_user(user_data, &data, + IOMMU_VIOMMU_TYPE_AMD, + out_vfmmio_mmap_offset); + if (ret) + goto err_init; + viommu->ops =3D &amd_viommu_ops; =20 spin_lock_irqsave(&pdom->lock, flags); @@ -73,6 +105,12 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viom= mu, struct iommu_domain * spin_unlock_irqrestore(&pdom->lock, flags); =20 return 0; +err_init: + iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); +err_mmap: + amd_iommu_gid_free(iommu, aviommu->gid); +err_gid: + return ret; } =20 static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) @@ -88,6 +126,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_vi= ommu *viommu) list_del(&aviommu->pdom_list); spin_unlock_irqrestore(&pdom->lock, flags); xa_destroy(&aviommu->gdomid_array); + iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); amd_iommu_gid_free(iommu, aviommu->gid); } =20 diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 7e6c4412ff20..c9207ed47fb6 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -110,6 +110,18 @@ static int __init viommu_vf_vfcntl_init(struct amd_iom= mu *iommu) return -ENOMEM; } =20 +/* + * Returns VF MMIO BAR offset for the given guest ID which will be + * mapped to guest vIOMMU 3rd 4K MMIO address + */ +u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) +{ + if (WARN_ON(!iommu->vf_base_phys || gid > VIOMMU_MAX_GID)) + return 0; + + return iommu->vf_base_phys + gid * VIOMMU_VF_MMIO_ENTRY_SIZE; +} + int __init amd_viommu_init(struct amd_iommu *iommu) { int ret; --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012022.outbound.protection.outlook.com [52.101.48.22]) (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 C8E7D370ACC for ; Mon, 14 Sep 2026 18:49:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.48.22 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411755; cv=fail; b=cxfNQtkmsqJELdNkBRXR6Sa7fvro97nUdemId+kXjRMKRYCX9oUwzqgy9OPvxLFPrXIYVkJRzYrHrC7xJhNVOEB9wp6ujJbzhgoARCpFVtYENRtBTHYa9jHtEON+uKtMfBcjd7PMNUmYmovHWJAhR4KhKd+ksEuSI8z328hP6wI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411755; c=relaxed/simple; bh=xGc/pmy15scfcGeTjUe85lSl4crWCKCra382F8eE16U=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BZ2HKXRFnEvYV+IOsTXjm26JN3tLDM2YMZ1RKX4N7R7IesTJpNmOaUuNJGpA274nqxqgc4cX7ahT0ffY6ckEzQ/+TVHraDyL/Uc6O7v60/kfLe3Qq2oaGG4IGITdace7UzPAp8t1kun8F3I+ETolD7tQHCsEpR4YYS16l/YpZso= 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=xfP9s2ob; arc=fail smtp.client-ip=52.101.48.22 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="xfP9s2ob" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=dzOtbeJwRYNdHAl097CtquX6QNv6tLp1MNLL+A5ZhDI7oarGaUU0NqkL4EL1thwiucGBHriZ9v202vVJNCm/WaeV0nXkVJJoqyIgl7UmATtMuG0WLfvgdajmRGr7egAj5UaEMV4OiJlxv91VEWBNJVx3s8J5HCf6pEvbFdXorTLDiN5pXM9vcjI8IBGTYrQQb1vYt0lJqQAaAg0ccKSO18UBAAt7NVgxHTeci/Nyn5MsneaDVWCwbHKhIPMbw0xN/NSHNjX5M/royOq6NuNfoDuveV14HYg81a4iu8kxTUto4nasYu+tBAlrne0JZ4b7fWBLKSXrjFxjBeSpRQdOug== 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=7LOt+HRyAlkQG8XL5+GFqVKis4E170MKrppumF39okc=; b=tWXikzNW6cfr/zVSR1A8X+eXSW0pJgbeg7VwmA3nY9Ido0wMZdb/UN/g8QF9cGXccQbL/wPFNdExYtpWpAgmht0MzA3FPaqo3YoTq5zXfUJkCh64ToTgO9rl3QEfv1v/uS8Tmzb/a3vU/wNdKv/YKtw/IJ80tpgxjvR0h/yq9yGt7SSjvrKnubQwg6PzgzWdrnY4kcGpN9tjoe7gEn/2aVqJqfUXYI1Ztwm8gFPICAgefUiwKd5WVndQfDUsmIfVdcc32dpYmIpR1XR6wpmIn06tpxj1UMkgMBuWljQqiBhWKDAcpjOYatF+Zr9vrZfWDz6KinYqZvqkJNhV/Nu1Zw== 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=7LOt+HRyAlkQG8XL5+GFqVKis4E170MKrppumF39okc=; b=xfP9s2ob4TKLAyaXU0Zmdss4+XpXRc76JOBmeOOBwcnWz3ybGO9ZZi22iEdbbc/xGoZQdfg3/iTMY0NjNXTubXRZEnntLvsouvBfM7fye0LLNYQCVpsC9mrGk7soxI3Dj2W6wUYArRvIr7dFL6MZykNigO+md6Qkw0ILhiHyLSE= Received: from BLAPR03CA0128.namprd03.prod.outlook.com (2603:10b6:208:32e::13) by SN7PR12MB7809.namprd12.prod.outlook.com (2603:10b6:806:34e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:49:05 +0000 Received: from BL02EPF00021F69.namprd02.prod.outlook.com (2603:10b6:208:32e:cafe::12) by BLAPR03CA0128.outlook.office365.com (2603:10b6:208:32e::13) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:05 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F69.mail.protection.outlook.com (10.167.249.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:05 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:48:58 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 08/24] iommu/amd: Introduce Reset vMMIO Command Date: Mon, 14 Sep 2026 18:47:34 +0000 Message-ID: <20260914184750.222939-9-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F69:EE_|SN7PR12MB7809:EE_ X-MS-Office365-Filtering-Correlation-Id: 08286678-56d7-4487-9c62-08df1290da04 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|23010399003|36860700016|376014|1800799024|7416014|10067099003|3023799007|22082099003|18002099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: 7bnuyFOZ2wLf9fnp5S9Ju7y0Qbu1Wr8J6FueVMiqAfJ87iuuPCyuEtWuctCD9kn4+ZbBE7bcPIS4OlNpCOB6dzbnGX9bgWZWVaeLsuxtWau70OG80kwz1Rlpqh/y6hIl996nMc2TR00a+P8S01RNu4mfGIVLLmU1lmFo5fUfIfdgFnQ1ZJAR2NM8Hf5uFrrQjUuQrXBynE/BlCCU2AmXuZ3jC+tm8aZW26j/cOREvZV0sAnOFu+KP36ndOlrwBGke68DSEDVQya2akfGDlCo+8PyA1gcWD9J8gK1H/ezg3kGrE3g6FJRtC7X4VJNdZBFaIa8xZUZfBvFuiGw/ams7mmXlSNULcLyi8+czCPkTnfJwHtnjvhEcRsOpT6LEPiAFWyCeGCp/BoRCPyj9upnZChFpfpCExj7vtpdzJ7Q/r+0Mo8Aw+vkrBgbwghWjjeeYPFrwfXx+q2WkzSfIkW45m71H8RLEd2x0+C1z20If0QCQL594WqVSoUFtkMHf6N3joCjs69B580DIdSB4ER9ShoEPg7YNJKtlYDaR/CrqUM3BaaMKBKmhknW3dIw4he3KfCunsUWTYx+GIBCax//aQ8Fyo6ekyqQNfamsn22FoUm+aC2GTIt2VN7HhqUlcQhQhJey+AoDkI+S4i0ML2iOEpuCmVhqV4lz9ttDJCeI67nH0Qo9CvWPoz09MmLM5Tq3V+sbgvpizyBgP+OOJYpnA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(23010399003)(36860700016)(376014)(1800799024)(7416014)(10067099003)(3023799007)(22082099003)(18002099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: W4kBT23Dt9d/1SeUoliWuNa0VOQMmrePLWGgZQ23SVThijz2aPBaj22nPw1mo17yqP7k20o60Pi3Mn6rgFjyys6s8cEm9uxaK99v46Dh9CqPUbTNTn/lL22en5PtYockBSqaAHF9OFwtvcYYyjJzZmDyab+XIuam4YoqZja/NyTexCKW+CeyHJpUs3O/OZc9wzCDbC15Z83eLv+oVdIMEivgZYuBedYwOUZSjCHXYnsUN3cMFZ47R5gNHhJFqpNwl0S/b7ehhJTR6q/66AiPtY2mvsnEWq8Trc0S572FcA8JKNjg79q3YLk4vwLtcSU4j/bskxtomGyhxDFav9/ZcEk9rIT6KwFOOjwO30wOrJGOKWogonJYH6RIKNH5CA6B+6ofd0/YpoSH4G1Y9BNVi1h1hwEyre0zHa93bjwwbfFIPIWfIcW3ndH5otXo0sNs X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:05.1854 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 08286678-56d7-4487-9c62-08df1290da04 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: BL02EPF00021F69.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7809 Content-Type: text/plain; charset="utf-8" Introduce new IOMMU commands for vIOMMU to reset virtualized MMIO registers of a particular guest. Always program RESET_MMIO_ALL_FLAG and RESET_MMIO_VCMD_FLAG. Propagate iommu_completion_wait() errors to the caller. Reviewed-by: Weinan Liu Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/iommu.c | 21 +++++++++++++++++++++ drivers/iommu/amd/iommufd.c | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 51e4364e8564..8c298e090889 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -233,4 +233,5 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *vi= ommu, u32 flags, /* Guest ID for vIOMMU */ int amd_iommu_gid_alloc(struct amd_iommu *iommu); void amd_iommu_gid_free(struct amd_iommu *iommu, int gid); +int amd_iommu_reset_vmmio(struct amd_iommu *iommu, u16 gid); #endif /* AMD_IOMMU_H */ diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 3aaaf69a1e7d..9eb9a0c190de 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -209,6 +209,9 @@ #define CMD_INV_IRT 0x05 #define CMD_COMPLETE_PPR 0x07 #define CMD_INV_ALL 0x08 +#define CMD_RESET_VMMIO 0x0A +#define RESET_MMIO_ALL_FLAG BIT(28) +#define RESET_MMIO_VCMD_FLAG BIT(31) =20 #define CMD_COMPL_WAIT_STORE_MASK 0x01 #define CMD_COMPL_WAIT_INT_MASK 0x02 diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index fa9708af258e..94c59f503662 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -1517,6 +1517,13 @@ static void build_inv_irt(struct iommu_cmd *cmd, u16= devid) CMD_SET_TYPE(cmd, CMD_INV_IRT); } =20 +static void build_reset_vmmio(struct iommu_cmd *cmd, u16 gid) +{ + memset(cmd, 0, sizeof(*cmd)); + cmd->data[0] =3D gid | RESET_MMIO_ALL_FLAG | RESET_MMIO_VCMD_FLAG; + CMD_SET_TYPE(cmd, CMD_RESET_VMMIO); +} + /* * Writes the command to the IOMMUs command buffer and informs the * hardware about the new command. @@ -1770,6 +1777,20 @@ void amd_iommu_flush_all_caches(struct amd_iommu *io= mmu) } } =20 +int amd_iommu_reset_vmmio(struct amd_iommu *iommu, u16 gid) +{ + int ret; + struct iommu_cmd cmd; + + build_reset_vmmio(&cmd, gid); + + ret =3D iommu_queue_command(iommu, &cmd); + if (ret) + return ret; + + return iommu_completion_wait(iommu); +} + /* * Command send function for flushing on-device TLB */ diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index 7f358e57efde..2e34491e3cf7 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -92,6 +92,11 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viomm= u, struct iommu_domain * =20 data.out_vfmmio_mmap_offset =3D aviommu->vfmmio_mmap_offset; =20 + /* Reset vIOMMU MMIOs to initialize the vIOMMU */ + ret =3D amd_iommu_reset_vmmio(iommu, aviommu->gid); + if (ret) + goto err_init; + ret =3D iommu_copy_struct_to_user(user_data, &data, IOMMU_VIOMMU_TYPE_AMD, out_vfmmio_mmap_offset); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011069.outbound.protection.outlook.com [52.101.62.69]) (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 4BBB03A1687 for ; Mon, 14 Sep 2026 18:49:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411761; cv=fail; b=lCERGoOq1OXE/L0ef2MIGR4a8V0RBHX3rcSYYbt+BWPGEKaofRN+muHpdmYurEsm9wjEB05jgvlTxQEMc0Jgv0VNvln0QZSIitFDwgHUvClW5qoftiyq23qLLKz/HDz2gG3+qNhid0DkjHTiGu6dKNtXHRiYM+KLZPpXKT/Ltbw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411761; c=relaxed/simple; bh=5rwEmzkbgZeXNyyWhidTbKY4xDAA/faziLrN2hNRoDk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uslKOi12dfSPgiRWXfcwWeWOKtuNIZMubRX68vHIDX+01WGE8np9d3KSL3saLF1xxdmgL2DSjaF9FrXvrOlbIT43SKW/P+x0tddxJZ/ujLiTGNUDcVVgtV6bZADYaXvvaFmNuSs/aymeAO+Ep+0HpQWqVkdSL2bESAjVi1bwmgY= 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=AbQ40TlV; arc=fail smtp.client-ip=52.101.62.69 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="AbQ40TlV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DC/oTJcrhh14iHR52n8wj5N85abLKyxOCjnaYqVuwrNybckk5kCkE3sDHzOj8shV2SFeMU5krnWvGMSm8cQq10uvBn3yjcbcRm11h3WgXwBAIXkoS4OyZF3bY8IWeWXH0kLFs1v5YReqO3AEFKDFM5GaRE7aGTvxXshrYiF+83mePHfjgFBu6JfDG3OoFudLXpGnKHKfXFBCIke9uWLmrD+8TmjX3I6V7p19lbmSBy3Hp4pZByicgq4S1+kn0LvrSvNl4zoHry41ddewseLWb/A1h0rZqyfQm6ySlJKPmBmzfdAchURyCL9T0Et4gl5m7XZlBrxBrHMBEcxo+sPvDQ== 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=ah62dVIcYBxpIqKVw3l7cVfVI2A33YIndo0S/XTjaro=; b=JmfHLMKPgoOsc7Gfs6Z9VFO2PrDKYELfJz+Mqx2fWsEA0HBXJL5zRiLJgiAQaRLv6QUkiKwkVhIFzEzk5POc3PTrTxaEi6Je5B8rr0yF6bb07OpUFD6zkauj7V5rxpTVIOa/FzNi5ZgZqtMpZvJ4ceka/v5Y+XYxLSPTiFxuTIFuh0fo1AN6MAeUz4JM//KD17EPkoh5f/0J87AXND4aGNCs8GvOtXapnQ/7k+6XhFDvJjfZy6PyCqwB9Km5gXMc3qboJgC2BS6C9wNrEFjJNrBAat868HdYLcziOntESSU9OdWJ+PEqt2bh9kEydQKBVr9HrbKPg93CH7yS0zgpWQ== 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=ah62dVIcYBxpIqKVw3l7cVfVI2A33YIndo0S/XTjaro=; b=AbQ40TlVe66cmrxWQ8ZMo0MQr5Kv9zzO8ZX7m1Px6CoOxkfGjduYvsVNxBxhaQViMOy8/HslgfCsuD86dy2y99A+Z5wZDXvTXQ+VfzjK9Bc96cPHqh3T6tHhJWWBYF4ODGe19z/DHyQtiaCDK7rd1Gxw1aE6UbqKKRkpFrHWBq0= Received: from CH0PR03CA0445.namprd03.prod.outlook.com (2603:10b6:610:10e::31) by SJ1PR12MB6244.namprd12.prod.outlook.com (2603:10b6:a03:455::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:49:11 +0000 Received: from BL02EPF00021F6C.namprd02.prod.outlook.com (2603:10b6:610:10e:cafe::1b) by CH0PR03CA0445.outlook.office365.com (2603:10b6:610:10e::31) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:11 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6C.mail.protection.outlook.com (10.167.249.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:11 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:05 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 09/24] iommu/amd: Introduce and map vIOMMU private IPA region Date: Mon, 14 Sep 2026 18:47:35 +0000 Message-ID: <20260914184750.222939-10-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6C:EE_|SJ1PR12MB6244:EE_ X-MS-Office365-Filtering-Correlation-Id: a70449bb-bb50-4a72-24a2-08df1290ddaf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|36860700016|376014|7416014|82310400026|1800799024|10067099003|11063799006|5023799004|56012099006|13003099007|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: P6GWUsVSb5DDiSRh74+oOxZkBZXZ8J7L5t8Ow513kEqoO4kcgQMXAz11v7eIXCQ/uPeL3Ok+CrVvGAFwi7r3zNGhUrueTqAmD8i55/HpAhX+NqZWl2gSh/DCmI21wSU9L1qjQ+m1Urbym6RYOD5cj7cAwaZQVui+a/6zHZ1eey4eiK1RoamMWMPiKsOH8qCWOIkz3Nf+CHHBudSJ24c4XnEuYQf9bdFETrQP0t2on5b0WoBrcJS4wHSQZw9Q04tPKJz4mfyz5p73gVJ3kqvJ2zlX5KU8UWQ9NTR9F3DjbqlYelDP/buKv61c4Eaosu8/Uccqb0l4LAteE+jclzVi22Njdt6Yz3fGuM1q2LoZeQiiaQ5N7xW1WMpRdz1HdvJwRsxO7SHEu2SmvTuHpSTqrJ53L45xs86bohDpxnVI7Wz0/zmIP4hOLnaXMZN3GwpafxGJdeuyvo8W/k8piCpMZ+vS7wnWGvrwpEBTxRXIh8qDP6nuumMZbetndFSYiTdcC0KNkKLO27PhjWyIhUwz71h8rusX4VqRvQgOyOmoscaf8nkeYzwIUN2TP6XJBEyli2kv2RRLeHweFwXBaTgG0XMkqhhJ+6HCbIlLfTpab0xfVB6IdDXCr/gGuinVvrpCLmg3nWso2soUibUToS+7GCPexvL+qYTSKBueerzmEUac40yxk5LIfv7Y+JC+2uAS7LjxvhBBaxPxFu7W4xXyRQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(36860700016)(376014)(7416014)(82310400026)(1800799024)(10067099003)(11063799006)(5023799004)(56012099006)(13003099007)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kGgOcJjIlxv2KC71Kzg3+i7iFM5B+mJds/tADuq8WvoPzHEwX0QfYYdJGPe4ddjG16E97bQa/SDsWUgFfEki4JNaPZnD3XywftulIECff4G4qDfxc20adNkVHlAxQoEW/ibG1xVNgOsD//80TxB6PMw0cRMnXYpM/f0xL9miRa9aV8iRkGtPufxflPoX8XNZt3FVIZfuVPpIsgNkM+5CoSMW4p/KXMbUMntAXFmo/ZCGvfm0YSGBmoVsbOW8MqERKgwEAdpKrc0nboVopozgC6Ys1mcVgoDO7jP4vC1gPzmyBxZadc6ZOjT6R81ZFx5EU/3zX/yZvKUfWAQpmxsAv114COS7Ad4c2d/G24BqnIZI5tpdRVUwqK5HB9OQW10DF+NgcJE/keYEa9jOKsivorgzePg9+5RzC3Gx9+/eTR3tSyyVsHETPWHhBapPjsrF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:11.3292 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a70449bb-bb50-4a72-24a2-08df1290ddaf 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: BL02EPF00021F6C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6244 Content-Type: text/plain; charset="utf-8" AMD vIOMMU introduces the IOMMU Private Address (IPA) region for guest-side IOMMU virtualization data structures. Introduce a per-IOMMU v1 paging domain in viommu_pdom, allocate 8MB of backing memory as four 2MB subregions, map them into the domain, and add viommu_private_space_init() / viommu_private_space_uninit() as a matched pair. Register the owning IOMMU in viommu_pdom->iommu_array via amd_iommu_pdom_bind_iommu() so unmap uses the standard domain_flush_pages_v1() IOTLB invalidation path. On bare metal iommu_map() does not flush (amd_iommu_iotlb_sync_map() returns early unless amd_iommu_np_cache is set). Call viommu_private_space_uninit() from amd_viommu_uninit() so boot failure and free_iommu_one() release the 8MB IPA mapping. Failed amd_viommu_init() after VF mapping goes through uninit. viommu_priv_alloc_map() takes a gfp for backing pages and iommu_map() page tables. Boot uses GFP_KERNEL. iommu_alloc_pages_node_sz() zeros them. On set_memory_uc() or iommu_map() failure, restore WB or leak rather than returning UC-mapped memory to the allocator. iommu_map() already unmaps any partial IOVA. Skip IOTLB invalidation when CONTROL_CMDBUF_EN is clear so boot failure after disable_iommus() does not wait on a dead command buffer. set_memory_uc() on each 2MB-aligned subregion splits the x86 direct map to 4KB PTEs. set_memory_wb() on free does not re-coalesce them, so the direct map stays fragmented (8MB per vIOMMU-capable IOMMU) for the life of the machine. The private IPA domain skips iommu_domain_init(), so set IOMMU_DOMAIN_UNMANAGED. set_dte_entry() only programs a v1 table when type has __IOMMU_DOMAIN_PAGING; type 0 would WARN and install a cleared DTE when increase_top() rewrites the self DTE. For more info, see section vIOMMU Private Address Space of the IOMMU specification [1]. [1] https://docs.amd.com/v/u/en-US/48882_3.10_PUB Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 5 + drivers/iommu/amd/amd_iommu_types.h | 11 ++ drivers/iommu/amd/init.c | 7 +- drivers/iommu/amd/iommu.c | 13 +- drivers/iommu/amd/viommu.c | 192 ++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 8c298e090889..a0d5d7e34020 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -53,6 +53,8 @@ extern bool amd_iommu_hatdis; /* Protection domain ops */ void amd_iommu_init_identity_domain(void); struct protection_domain *protection_domain_alloc(void); +struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev, + u32 flags); struct iommu_domain *amd_iommu_domain_alloc_sva(struct device *dev, struct mm_struct *mm); void amd_iommu_domain_free(struct iommu_domain *dom); @@ -94,6 +96,9 @@ void amd_iommu_domain_flush_pages(struct protection_domai= n *domain, void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data, ioasid_t pasid, u64 address, u64 last); =20 +int amd_iommu_pdom_bind_iommu(struct amd_iommu *iommu, struct protection_d= omain *pdom); +void amd_iommu_pdom_unbind_iommu(struct amd_iommu *iommu, struct protectio= n_domain *pdom); + #ifdef CONFIG_IRQ_REMAP int amd_iommu_create_irq_domain(struct amd_iommu *iommu); #else diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 9eb9a0c190de..9a90b8fe0fe3 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -417,6 +417,13 @@ /* Guest ID bits 0-14; bit 15 is reserved for secure vIOMMU. */ #define VIOMMU_MAX_GID 0x7FFF =20 +/* + * Total IOMMU private region is 8MB (4 x 2MB-subregion) + */ +#define VIOMMU_PRIV_REGION_BASE (0) +#define VIOMMU_PRIV_SUBREGION_CNT (4) +#define VIOMMU_PRIV_SUBREGION_SIZE (0x200000) /* 2MB */ + /* Timeout stuff */ #define LOOP_TIMEOUT 100000 #define MMIO_STATUS_TIMEOUT 2000000 @@ -804,6 +811,10 @@ struct amd_iommu { unsigned char iopfq_name[32]; =20 struct ida gid_ida; /* guest IDs for this IOMMU */ + + /* HW vIOMMU support */ + struct protection_domain *viommu_pdom; + void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT]; }; =20 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev) diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 79386816807a..17d321412dcc 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -1798,7 +1798,12 @@ static void __init free_sysfs(struct amd_iommu *iomm= u) static void __init free_iommu_one(struct amd_iommu *iommu) { free_sysfs(iommu); - /* Tear down vIOMMU before IOMMU buffers and MMIO are released. */ + /* + * Tear down vIOMMU before buffers and MMIO are released. + * IOTLB invalidation runs only while CONTROL_CMDBUF_EN is + * set (amd_viommu_init() unwind). disable_iommus() clears + * that bit first on the boot-failure path. + */ amd_viommu_uninit(iommu); free_iommu_buffers(iommu); amd_iommu_free_ppr_log(iommu); diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 94c59f503662..1a2ddfabb7cd 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2502,6 +2502,16 @@ static void pdom_detach_iommu(struct amd_iommu *iomm= u, spin_unlock_irqrestore(&pdom->lock, flags); } =20 +int amd_iommu_pdom_bind_iommu(struct amd_iommu *iommu, struct protection_d= omain *pdom) +{ + return pdom_attach_iommu(iommu, pdom); +} + +void amd_iommu_pdom_unbind_iommu(struct amd_iommu *iommu, struct protectio= n_domain *pdom) +{ + pdom_detach_iommu(iommu, pdom); +} + /* * If a device is not yet associated with a domain, this function makes the * device visible in the domain @@ -2851,8 +2861,7 @@ static const struct iommu_dirty_ops amdv1_dirty_ops = =3D { .set_dirty_tracking =3D amd_iommu_set_dirty_tracking, }; =20 -static struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device= *dev, - u32 flags) +struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev, = u32 flags) { struct pt_iommu_amdv1_cfg cfg =3D {}; struct protection_domain *domain; diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index c9207ed47fb6..89d6dc520b2c 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -17,6 +17,7 @@ #include "amd_iommu.h" #include "amd_iommu_types.h" #include "amd_viommu.h" +#include "../iommu-pages.h" =20 static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) { @@ -39,9 +40,12 @@ static void __init amd_viommu_vf_vfcntl_unmap(struct amd= _iommu *iommu) } } =20 +static void viommu_private_space_uninit(struct amd_iommu *iommu); + void __init amd_viommu_uninit(struct amd_iommu *iommu) { iommu->flags &=3D ~AMD_IOMMU_FLAG_VIOMMU_EN; + viommu_private_space_uninit(iommu); amd_viommu_vf_vfcntl_unmap(iommu); } =20 @@ -110,6 +114,187 @@ static int __init viommu_vf_vfcntl_init(struct amd_io= mmu *iommu) return -ENOMEM; } =20 +/* + * Restore WB and free @va. If WB restore fails, leak the pages so + * UC-MINUS direct-map PTEs and the PAT memtype reservation are not + * returned to the buddy allocator. + */ +static void viommu_priv_wb_free(void *va, size_t size) +{ + if (WARN_ON_ONCE(set_memory_wb((unsigned long)va, + size >> PAGE_SHIFT))) + return; + iommu_free_pages(va); +} + +/* + * Allocate backing pages, mark UC, and map at @iova in viommu_pdom. + * *@out_va is NULL on any failure. @gfp is used for the backing + * folio and iommu_map() page tables. + */ +static int viommu_priv_alloc_map(struct amd_iommu *iommu, u64 iova, size_t= size, + gfp_t gfp, void **out_va) +{ + int ret; + void *va; + int nid =3D iommu && iommu->dev ? dev_to_node(&iommu->dev->dev) : NUMA_NO= _NODE; + + *out_va =3D NULL; + + if (!iommu || !iommu->viommu_pdom) + return -EINVAL; + + va =3D iommu_alloc_pages_node_sz(nid, gfp, size); + if (!va) + return -ENOMEM; + + /* + * IOMMU spec: backing storage must be UC. set_memory_uc() + * splits covering 2MB direct-map pages to 4K; set_memory_wb() + * does not re-coalesce them. + */ + ret =3D set_memory_uc((unsigned long)va, size >> PAGE_SHIFT); + if (ret) + goto err_uc; + + ret =3D iommu_map(&iommu->viommu_pdom->domain, iova, + iommu_virt_to_phys(va), size, + IOMMU_READ | IOMMU_WRITE, gfp); + if (ret) + goto err_uc; + + *out_va =3D va; + return 0; + +err_uc: + viommu_priv_wb_free(va, size); + return ret; +} + +static bool viommu_cmdbuf_enabled(struct amd_iommu *iommu) +{ + if (!iommu->mmio_base) + return false; + return readq(iommu->mmio_base + MMIO_CONTROL_OFFSET) & + BIT_ULL(CONTROL_CMDBUF_EN); +} + +/* + * Unmap @iova if the command buffer is still enabled, restore WB, and + * free @cpu_va. Skip IOTLB invalidation after disable_iommus(). + */ +static void viommu_priv_unmap_free(struct amd_iommu *iommu, u64 iova, size= _t size, + void *cpu_va) +{ + size_t unmapped; + + if (!cpu_va) + return; + if (!iommu || !iommu->viommu_pdom) + return; + + if (viommu_cmdbuf_enabled(iommu)) { + unmapped =3D iommu_unmap(&iommu->viommu_pdom->domain, iova, size); + WARN_ON(unmapped !=3D size); + } + + viommu_priv_wb_free(cpu_va, size); +} + +static void *alloc_private_subregion(struct amd_iommu *iommu, u64 base, si= ze_t size) +{ + void *region =3D NULL; + int ret; + + ret =3D viommu_priv_alloc_map(iommu, base, size, GFP_KERNEL, ®ion); + if (ret) + return NULL; + + pr_debug("%s: base=3D%#llx, size=3D%#lx, subregion=3D%#llx(%#llx)\n", + __func__, base, size, (unsigned long long)region, iommu_virt_to_phys(re= gion)); + + return region; +} + +static void viommu_private_space_uninit(struct amd_iommu *iommu) +{ + int i; + u64 base; + struct protection_domain *pdom; + struct iommu_domain *dom; + + pdom =3D iommu->viommu_pdom; + if (!pdom) + return; + + for (i =3D 0; i < VIOMMU_PRIV_SUBREGION_CNT; i++) { + if (!iommu->viommu_priv_region[i]) + continue; + base =3D VIOMMU_PRIV_REGION_BASE + (i * VIOMMU_PRIV_SUBREGION_SIZE); + viommu_priv_unmap_free(iommu, base, VIOMMU_PRIV_SUBREGION_SIZE, + iommu->viommu_priv_region[i]); + iommu->viommu_priv_region[i] =3D NULL; + } + + dom =3D &pdom->domain; + amd_iommu_pdom_unbind_iommu(iommu, pdom); + amd_iommu_domain_free(dom); + iommu->viommu_pdom =3D NULL; +} + +static int viommu_private_space_init(struct amd_iommu *iommu) +{ + int i, ret; + u64 base; + struct iommu_domain *dom; + struct protection_domain *pdom; + struct pt_iommu_amdv1_hw_info pt_info; + + dom =3D amd_iommu_domain_alloc_paging_v1(&iommu->dev->dev, 0); + if (IS_ERR(dom)) { + pr_err("%s: Failed to initialize private space\n", __func__); + return PTR_ERR(dom); + } + + /* + * Skipped iommu_domain_init(). set_dte_entry() only programs + * v1 when type has __IOMMU_DOMAIN_PAGING. + */ + dom->type =3D IOMMU_DOMAIN_UNMANAGED; + + pdom =3D to_pdomain(dom); + iommu->viommu_pdom =3D pdom; + + ret =3D amd_iommu_pdom_bind_iommu(iommu, pdom); + if (ret) { + amd_iommu_domain_free(dom); + iommu->viommu_pdom =3D NULL; + return ret; + } + + /* + * Each private region requires to 8MB of memory to be allocated + * and mapped. Split the region into 4 x 2MB-subregion. + */ + for (i =3D 0; i < VIOMMU_PRIV_SUBREGION_CNT; i++) { + base =3D VIOMMU_PRIV_REGION_BASE + (i * VIOMMU_PRIV_SUBREGION_SIZE); + iommu->viommu_priv_region[i] =3D alloc_private_subregion(iommu, base, + VIOMMU_PRIV_SUBREGION_SIZE); + if (!iommu->viommu_priv_region[i]) { + pr_err("%s: Failed to allocate vIOMMU private subregion %d\n", __func__= , i); + viommu_private_space_uninit(iommu); + return -ENOMEM; + } + } + + pt_iommu_amdv1_hw_info(&pdom->amdv1, &pt_info); + pr_debug("%s: devid=3D%#x, pte_root=3D%#llx\n", + __func__, iommu->devid, + (unsigned long long)pt_info.host_pt_root); + + return 0; +} + /* * Returns VF MMIO BAR offset for the given guest ID which will be * mapped to guest vIOMMU 3rd 4K MMIO address @@ -130,6 +315,13 @@ int __init amd_viommu_init(struct amd_iommu *iommu) if (ret) return ret; =20 + ret =3D viommu_private_space_init(iommu); + if (ret) + goto err; + iommu->flags |=3D AMD_IOMMU_FLAG_VIOMMU_EN; return 0; +err: + amd_viommu_uninit(iommu); + return ret; } --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010025.outbound.protection.outlook.com [52.101.46.25]) (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 C623338736E for ; Mon, 14 Sep 2026 18:49:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.46.25 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411765; cv=fail; b=o+hPdkyCkpWpNmJjvG+Rwqb3aY6L47aKxr/7we+RXEQ216D61Cz6L+CfQQc7Fz7hMWB+giR9Op0wP99HNMk4l/hqB5pSYd6eLU82s43CNuBwe92vua42yv4IeXSPwDX3n3fTlS3F4PfwxqZnaH2+qsY2fOt3G6oUKQmqNpg820g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411765; c=relaxed/simple; bh=5EijOqsg65kUx60vUJckK12Pi7m/7OkciuTmucmKh8Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=G2VNtULUSCFZ6siEbP+ocx+e6ZBDUZWVRpigsvueWdnAMx7DldwU4jO0ZF+gYafyWA4S3wqv+5STnsBwvY4/CxSISQiZS+XBgthdByBeUCYTOL2h2PWnK1XwYHhjLTzThmdhJalTvUfNM1R2f9VMDj7FY12zX+rJA/lSQCfPUMQ= 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=lZx65+OQ; arc=fail smtp.client-ip=52.101.46.25 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="lZx65+OQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DPw69nUl7bCnkwI5Po3LdN5BDAslUXiHYe+Hk61EtfTxxH++tGvQh2HUi0HNeZc8T1ZWcWg3/BzYrSt5vGjzGsQQuV6eHUlx0CLGfd/8DDbfed/fDUd2lOuO5NtX59Isc18BG6RF/5QKIMSmen/TW2yCemtEkWGxBpX3ZEtPkYxGnl4u/i8Q0hzkazu1GonLbJgUxer+6bqom3Eh2FwNnH08KMmBtwqpgTpExr+VonfI3Agk5D8D/ImYHHuiVakm1OXGahymNJOlxRBjiyuoBJWxDDg0zAiM0n/EWJUwA1DAH0q4IDymCgHvl4MXfPag3/6n6TJp+9Viaai+yk1VLw== 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=1TDx6uKWhEghfaALlmUWp8U5PxwQBmlJRdfYvrbErns=; b=VJ5IdY6yWHPxDOpPOloKFtKh+pFSkzWUSw6gQ5J26Eyl3F8KKhgWKMYutMpM904/KzzyybsWzQ0zQlokuz0rkPRM2AdQGsCgT2F6cyzRNYrHpqvVhNI3Rs6DErSSH1Kf4VYR5oykT0VhRz5CWeWgxU5tYN7cQyGIRdvRwap5OhTNRNDBFA7MHVK75AKPIYGEPdeJpVnd2BRFc/dRhZ/pWwb1miVru2vjGyc2vzRvekthjBRDsWwhAi3J64fWhJaN0T6j1f17SnyrmhHHXLkK78RSlSFxeWRXXTfu+lHxFfhOgTj3qgA8/v8CFm6uQzsxMocvFwA/XGCL7ti6z03IEA== 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=1TDx6uKWhEghfaALlmUWp8U5PxwQBmlJRdfYvrbErns=; b=lZx65+OQRJCeTw47mctZ0yPloPhJPzMOy5E9DTTohFwElAl5eNhftNbz1CpASncbYwhn5/JvyZFUaPLh8hV0lOWikJ7TDUoVJVTWuIsnTaSSLtUgzsDGKC2rvFW13pKtqIqechkuZSc9xluv/AVd0cvNVsrBX5zdXxbhmrD9t0w= Received: from BN9PR03CA0272.namprd03.prod.outlook.com (2603:10b6:408:f5::7) by PH7PR12MB5784.namprd12.prod.outlook.com (2603:10b6:510:1d3::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.6; Mon, 14 Sep 2026 18:49:17 +0000 Received: from BL02EPF00021F6B.namprd02.prod.outlook.com (2603:10b6:408:f5:cafe::13) by BN9PR03CA0272.outlook.office365.com (2603:10b6:408:f5::7) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:17 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6B.mail.protection.outlook.com (10.167.249.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:17 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:11 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 10/24] iommu/amd: Pass iommu to device_flush_dte() Date: Mon, 14 Sep 2026 18:47:36 +0000 Message-ID: <20260914184750.222939-11-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6B:EE_|PH7PR12MB5784:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e624c94-effd-4a61-58ce-08df1290e13f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700016|7416014|23010399003|1800799024|82310400026|11063799006|56012099006|10067099003|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: /4EOqLdayXOQtj2EizQIDtRX5Fvs3nc7UM0VqNTVU5t2booXFjCoYptAcq3lUqv9xFzy9ELrW/2NMk571EYDASWmeMBKwpmiTW6KCQpYCV3P0TSzIpZ5H/SYh8WLFdsvMsz3cA+H/xUYKYwrwNbnc3pK0hsudqFHe7b0LK2SZxQwHcX/IDiwsDh9i3Pm+SpJdngHmWdLJW8m68Y1UN6ncXUZk0+CDmp5YJaqG4QWV+JxPE9MSDf+6qssqfIf8nnzJYUrIPbTG3yAHsi+XhcOkSh20g2xxbJhbTEbK+m4xHuYd3D6Xo4C4sHEswvNHRwJ0CqaLrqRomav8u+Z8LAychBGgBvheKQLil1pfgPFKPgfqhOENp6OWtuqasdUB8jk2mpyHG4fIw/uhSWKYFDOIaUze6QGfsRFlOZChEMm/GvSp+tY5Ic482eL9Pg3kJfXySb0ZKAkWnqv7sri5fwFox/n5Mx3yvNxX+it/yFZ0l9BwnAvTSg85THU6/5ftL8wiimpJeOwTdaIGke9bLhczqkXQCYn5J4MiHybseZF7mXFWAn0ZC4hhH3HGFjc5q+iNxvkrv7akabQCTMkds4lT8btgZVRCXGvI6Zt4wAFaGblT7pzYOcB2/OqK/E8s91M6fp+fd9n/cE8hZHnf3ugKtndQlNu4woA4z0YND905DQWInIb+/fzIF8nnirp3Ceb+X+FhtvsqUAH7NvKlMx6Rw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(36860700016)(7416014)(23010399003)(1800799024)(82310400026)(11063799006)(56012099006)(10067099003)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ik1YuhTz8RT4hLLnl2FIp6uSujk8666/RrM5ykJDjouljQEkgSGw1WNhCFdZ4tVEVpSsfB1SR1r4ggT3nQVY4vPUHr6dUCbRy//iYruDS3sunNtxy2onRWJFqpd47+FsQ8OR0ILokRF5fm8/NtOjzxUXncInIIw+EfXgFlVqhIdNIQkFfXE7TK3ZknsSONHW7R5gHfjQ0wxkx401aTbmDtEYK+4C+u3vzMI4smL0VwLpYP2J0qADt1osUWnLlez+jm93JvaUb7+GET5X0ggrdxrb9VyUK4jxwVjKHeJfjeH8dCWxCB94PGVvSzK5zf8ENcnrMPTeDEO5ZlpakI1pf004Vv6ubRjFl+9Fyq2xVzokH8OHCXdtvdEsFobhs7loKuRWlvwVHfinMXCpcdgqLis5PosISYdmnKxKQEQ1AKoPslrZJO7mkl5J4jf93BwE X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:17.3082 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e624c94-effd-4a61-58ce-08df1290e13f 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: BL02EPF00021F6B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5784 Content-Type: text/plain; charset="utf-8" Take struct amd_iommu explicitly so DTE invalidation is issued on the same IOMMU instance used to program the DTE. Also, flush each device in amd_iommu_change_top() immediately after updating its DTE. Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/iommu.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 1a2ddfabb7cd..c7b14842a6c7 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -76,7 +76,7 @@ static void set_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, phys_addr_t top_paddr, unsigned int top_level); =20 -static int device_flush_dte(struct iommu_dev_data *dev_data); +static int device_flush_dte(struct amd_iommu *iommu, struct iommu_dev_data= *dev_data); =20 static void amd_iommu_change_top(struct pt_iommu *iommu_table, phys_addr_t top_paddr, unsigned int top_level); @@ -218,7 +218,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, { update_dte256(iommu, dev_data, new); clone_aliases(iommu, dev_data->dev); - device_flush_dte(dev_data); + device_flush_dte(iommu, dev_data); iommu_completion_wait(iommu); } =20 @@ -1817,9 +1817,8 @@ static int device_flush_dte_alias(struct pci_dev *pde= v, u16 alias, void *data) /* * Command send function for invalidating a device table entry */ -static int device_flush_dte(struct iommu_dev_data *dev_data) +static int device_flush_dte(struct amd_iommu *iommu, struct iommu_dev_data= *dev_data) { - struct amd_iommu *iommu =3D get_amd_iommu_from_dev_data(dev_data); struct pci_dev *pdev =3D NULL; struct amd_iommu_pci_seg *pci_seg; u16 alias; @@ -2788,11 +2787,9 @@ static void amd_iommu_change_top(struct pt_iommu *io= mmu_table, /* Update the HW references with the new level and top ptr */ set_dte_entry(iommu, dev_data, top_paddr, top_level); clone_aliases(iommu, dev_data->dev); + device_flush_dte(iommu, dev_data); } =20 - list_for_each_entry(dev_data, &pdom->dev_list, list) - device_flush_dte(dev_data); - domain_flush_complete(pdom); } =20 @@ -3226,7 +3223,7 @@ static int amd_iommu_set_dirty_tracking(struct iommu_= domain *domain, spin_unlock(&dev_data->dte_lock); =20 /* Flush device DTE */ - device_flush_dte(dev_data); + device_flush_dte(iommu, dev_data); domain_flush =3D true; } =20 --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazon11012020.outbound.protection.outlook.com [40.107.200.20]) (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 45118396D2C for ; Mon, 14 Sep 2026 18:49:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.200.20 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411770; cv=fail; b=p0lmZsykJFjc+jb/sSRpwnSvWUpkNo+KzvjcZ+S9cAcU2tu/mXp7CO+8LmtMExvv6qgNZ8jFht0jeF841ekE7oRZULectRPq9zpz+kpsuwD/Zf45Fj1UOxIUuHRA01eWldEV8E0HTcuEehcq9BRl4+hTqvmBK/2zaGPFYQLlIqU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411770; c=relaxed/simple; bh=L7oaWtICbm7/6chyS170IJAjX7Y5kYeU/29cdE5sLBI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=h9WyKwVmAY+fOGlt5MZzkSVyMzJ2Wk+QZJ8WDnGR3txvkJSW4uxWkON6Yq0/mjkhrwXVlMDIE4ALJkgwgCUWGIBWuIsvsIZnd3LzVjNyLAEY7qvXqUjYH1G3WRoZRc+M3U/mykDPmlzDAEFwtOr564CUM+TIe3pxqbyY7mCS8Vo= 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=SShUv2B1; arc=fail smtp.client-ip=40.107.200.20 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="SShUv2B1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ni6yRajlQc3f3nXGC+XUCcfqzeVjuL7ri+1r6HkSpHc+YIw7pKXjWAtDME0wb4XYc/hvOISUqs03nopp0EtWIzku6oSVTd/3CL+kZ/ZxpftWTcZN4atqJHa82Ru8p9/1r7Mi2Kz8NChMqmKulTjf0VeaL/b+TlfFPSTpCuGmb6lJ/eA0OEj/hhWTznCS+FL0eUsMwzwXR0Jno818lmdBdwsjBJf+Fnf2IcsV3E68uqqQ6aUwhlDjp16l1Cg92RhinMb4tvmNh+fSUl2R3zQwkQwbICsvz2uLd5L0Lqhpn8O4JMqOAUxCuOQf3Yk1azHVkZ2ZDy5HurOKsHz+6ATeIQ== 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=IKr0yRZgqcIAqBuORLtP4ThqnAHxSU6/tq5G0i4Cqgo=; b=dHxnphx6K7qqKGhxr8bPdK5OneXVPDojwurl7uFXwBmMakC7vICFXTxZZUJUIphdHfsn12ncAshd8fdYaooqUe3Pi+qBaDnVodCYsiXKt5vmfVW6A/FMwZj4osshbJUc+WGrxiFm4dxY7YpfWLv8d1JjD2oDvgyc79dJmOin/NkdXW9WkafMVSwMeNYjApPcTj1DYNbFFQ6cC17700czJZ9n/XUhhedkz5j4ybAtG+Egi1ftrrcyu67KsK8ooM7Fkag5788UbaJx7vV5WUatS65Ykvg2lt3O9ErSKU/bppanoxQJvfsse9hwAqxadBd4xRmeZTlF7Sxkf66KIX/mYA== 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=IKr0yRZgqcIAqBuORLtP4ThqnAHxSU6/tq5G0i4Cqgo=; b=SShUv2B1lhQTv90EZLakitn31PDBde+m5oa6x6/clPROaS9BZtf9CoFWnAeqM76aF6v7jS0+H+Y4HgVLrPA4uM8VdGuK/5wpFIrYPBFyFry9jhLLp3YPh4NY5UXUx3DLCkejbPNDEq1d98A1YSkYBUmgvbxoKqF1EFSw/wLzQwY= Received: from BN9PR03CA0184.namprd03.prod.outlook.com (2603:10b6:408:f9::9) by DM4PR12MB6447.namprd12.prod.outlook.com (2603:10b6:8:bf::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.7; Mon, 14 Sep 2026 18:49:23 +0000 Received: from BL02EPF00021F6D.namprd02.prod.outlook.com (2603:10b6:408:f9:cafe::51) by BN9PR03CA0184.outlook.office365.com (2603:10b6:408:f9::9) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:23 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6D.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:23 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:17 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 11/24] iommu/amd: Pass iommu and devid to amd_iommu_make_clear_dte() Date: Mon, 14 Sep 2026 18:47:37 +0000 Message-ID: <20260914184750.222939-12-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6D:EE_|DM4PR12MB6447:EE_ X-MS-Office365-Filtering-Correlation-Id: 90ca1931-66aa-4964-c0a0-08df1290e4e5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|23010399003|36860700016|376014|7416014|1800799024|10067099003|22082099003|18002099003|11063799006|56012099006; X-Microsoft-Antispam-Message-Info: X8gbrPkdFvV2W/ettTH+TYi2UWG+J6xpLYDo0tni+u+hEmhr0H1Ph8dDuHgP9DwtatFiOjBlsmyg1kUA4v6L7UlDsu58e1XBTJOY/tKUzjf4lY+ZzOw94vVCTomTxQ9L89cakwOlKrW6jksA38eUKKaadB/IJsu1tlZLXxsoTKz5u9yIcnJftWoaP8DHhz3t3smd/6zCTKmLbuU/jDWaoRuMC609lOdq+kJW/80eyP5wRF5x8+TaDpY7EC1bJC04mFtuOHSHPUEzJqR/BorWpzpKliXleRoBFQk4c5u+vNqkuYqRJ6QyS3Vz1BA/VOK+Yri1XAt6foztB/6vsdKZoeDhfZjbcJqACgVAnb0+CTgj01yARhRnLxesJPYhQmxQQ/I22Pqlb2gVDoe/rpUYrE2IIfYQipXPMy4bwOt3L4zosAeJFXaHVIlJn+neItxfafQbpDd2LVvCM3F2M+O20Xb1xth9/3dH3yah7dD6NbU8U6UVb7nTXJunL/gs+ww5xD1iu4leGBIvCFUggICWrLN4/1v67bPepTsMKb64OkMLRIMEe6OnGdzOe47FrmLK2CU9P6PT9AwOGnlxp6kdtDmSfi8umLgFbXa3mLGKLSdWNxYJeKMkKWJWdFuLp2JOZonAf/HAZyoaP0F9pfRzVpOZCSp9XT51ZgegbuBDaj0NO4vvrmNlcNVuFcAZoYGsIy07WuUevvoJ26amDy2jvQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(23010399003)(36860700016)(376014)(7416014)(1800799024)(10067099003)(22082099003)(18002099003)(11063799006)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uJfRCqdzFUNTixu1v/Eq4RoHZaEik85iEFqKAhL8e7aMZ5D6IreJ+3dh25GDXWLx+olR3uakA2OPsWr6IqCjeLuBIc0B4h6n9rh17Fb6Z7OGKwwnGEOL6S1JFkYSv4U+auVHHDwSNrnPgAbcjXz0cCD63nuLKDR+NJKNT8O2oG1cbRg+bj8ghBi4Bv6WuCAjBBZSnNKM5921TasaFJxgO3Iyi6uaw4gxBckwIJSskEtouPdrxKi4I2L/qk7ESHYCdCVTv8OQ+J4rCUBM6AKO2NzajLk5CamNfmBLerhNcAaZyAMLtwNunsL53UkopDIOnw/Nqr7+ag6mK7QSxvXHqK21zo3swnEqwOXXgpUH6HXdZgD91z8KqNvep6N5LEO1AzHPWQFU0v7oRXorfrgdtu2oumWAvZUB/kpkHwX7Qk67gSHvzpGEr+ZxBnz4SmLF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:23.4318 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 90ca1931-66aa-4964-c0a0-08df1290e4e5 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: BL02EPF00021F6D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6447 Content-Type: text/plain; charset="utf-8" Take struct amd_iommu and device ID explicitly instead of deriving the IOMMU from struct iommu_dev_data. Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 6 +++--- drivers/iommu/amd/iommu.c | 4 ++-- drivers/iommu/amd/nested.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index a0d5d7e34020..9cc5da745a73 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -210,10 +210,10 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, struct dev_table_entry *new); =20 static inline void -amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table= _entry *new) +amd_iommu_make_clear_dte(struct amd_iommu *iommu, u16 devid, + struct dev_table_entry *new) { struct dev_table_entry *initial_dte; - struct amd_iommu *iommu =3D get_amd_iommu_from_dev(dev_data->dev); =20 /* All existing DTE must have V bit set */ new->data128[0] =3D DTE_FLAG_V; @@ -223,7 +223,7 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_dat= a, struct dev_table_entry * Restore cached persistent DTE bits, which can be set by information * in IVRS table. See set_dev_entry_from_acpi(). */ - initial_dte =3D amd_iommu_get_ivhd_dte_flags(iommu->pci_seg->id, dev_data= ->devid); + initial_dte =3D amd_iommu_get_ivhd_dte_flags(iommu->pci_seg->id, devid); if (initial_dte) { new->data128[0] |=3D initial_dte->data128[0]; new->data128[1] |=3D initial_dte->data128[1]; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index c7b14842a6c7..dd2cb79d506f 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2340,7 +2340,7 @@ static void set_dte_entry(struct amd_iommu *iommu, struct gcr3_tbl_info *gcr3_info =3D &dev_data->gcr3_info; struct dev_table_entry *dte =3D &get_dev_table(iommu)[dev_data->devid]; =20 - amd_iommu_make_clear_dte(dev_data, &new); + amd_iommu_make_clear_dte(iommu, dev_data->devid, &new); =20 old_domid =3D READ_ONCE(dte->data[1]) & DTE_DOMID_MASK; if (gcr3_info->gcr3_tbl) @@ -2372,7 +2372,7 @@ static void clear_dte_entry(struct amd_iommu *iommu, = struct iommu_dev_data *dev_ { struct dev_table_entry new =3D {}; =20 - amd_iommu_make_clear_dte(dev_data, &new); + amd_iommu_make_clear_dte(iommu, dev_data->devid, &new); amd_iommu_update_dte(iommu, dev_data, &new); } =20 diff --git a/drivers/iommu/amd/nested.c b/drivers/iommu/amd/nested.c index f1c7987fc585..8bb0d1596c6c 100644 --- a/drivers/iommu/amd/nested.c +++ b/drivers/iommu/amd/nested.c @@ -205,7 +205,7 @@ static void set_dte_nested(struct amd_iommu *iommu, str= uct iommu_domain *dom, return; =20 parent =3D ndom->viommu->parent; - amd_iommu_make_clear_dte(dev_data, new); + amd_iommu_make_clear_dte(iommu, dev_data->devid, new); =20 /* Retrieve the current pagetable info via the IOMMU PT API. */ pt_iommu_amdv1_hw_info(&parent->amdv1, &pt_info); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013047.outbound.protection.outlook.com [40.93.196.47]) (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 9B1F9392C2C for ; Mon, 14 Sep 2026 18:49:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.47 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411780; cv=fail; b=LPNXkepVxRU2NQbYC1hdKJRL87fUGKPjW3xf5C+SwIz0cdocljfnvYXnS2L4J/gnFTovKe0T8FIKZ+4MPbiNOWV+kGWD5V/RuBw2A1yTZLfELx9OHai5HrnsLZwAmAa7eJuxRVXP5EbZ4tZAiP7U8QOGHJoOhH/W9n/CKwlQHiE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411780; c=relaxed/simple; bh=e5Nlmsl0PLoT0fEBfl8DglwacN3sLpN/2yoINmGM88c=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J9xmMYp0VEfwRGdk/VAglN8ku79M3IZfbkCrHSPWHeMWat/Ho2weS6+Fi1lGzT6yA2Yz4NsX2XBsvqsI4roFaViEMG6y+xHNp3Njf7pyvPxoqNLr/7+19ycsbx7Y7Xvf32F81Kap9rCovufgZ4Gmvb59hhuLTmNfP9xWVNGqvjI= 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=okExYO5i; arc=fail smtp.client-ip=40.93.196.47 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="okExYO5i" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nxc/r0fi31ctjGIDwxD8DS4QgUko4X5BO+Xx7dBhy6HmXLYuh5hq7MdwRcudRhSlUIgE1t0LMLZTbnNDFwRJNxVpj5YasOUHASi6RX3GZnWSIgs5AREEmc3dOv4y3ljyioanW+v6QH/Yyof1u/C9yAKFtqjEjTGFV31Xb1d21DrudaHJDJv9H4ajoO1eYYSmX2fM+U4QEtkfJ6sTwnyAECpnZH99mGdFdW8TL8gpVVraPpy8oCrM0X4hFBJm/9gsA9msBz65VfqyxC20O0lQzAEM2Z0kBkgKmjyKFz5stIuxvAgpwmH/A9K8x7qDbtmAX9y7VvzSATdBBALXFHe1hQ== 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=CmKQoD4fV9QYORxbWFbKh5A/CQe0J8OiT4hJRB/TJT8=; b=d2x/L1TA1Z6d9RrZX7yWfV/KxE8hD2KH/EewhZ+dxWJNuBfzs6XVo1BgwY1EN3pj1NaTxUxlLM94f/Dow5MKHsbZFJdlJhcpIeUOnmNyLzZZFkiB4d6MJyLDnh0ocXewBEmJxfhmMCUhF4cnzR1cHdiaLD9/t8Vp+V837xFVk+7yRP0xS20rfRdowrG1EwGjvzdcSjzwqQ2sfyeobmuTrAqLSfwqbWQmzCGVc42OV9gh7SXOBZZhHlRk9hmLWLwc7eAY8WWYXizvu/LKQINv71OWk+M3f9NvsKrmPgVrXPFVd8QWgpYxLXJU8Y5OhOC9/zRiN/dpGb9vzsM1/Rblag== 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=CmKQoD4fV9QYORxbWFbKh5A/CQe0J8OiT4hJRB/TJT8=; b=okExYO5iqv40WHSHauufwPa+bJue+gmpKdm+3zf98pwitujICPVMz9frwbk9Cmqk3WQox5AzaIc8ewXVAqntXT9nTWAUTXS+DspUeCGT6IomdyvDolu5vo0D+iiFi6PvpgGMyScCFtaPHmqPMsZASBjkgaFhHQHNgyZMAWlQbOw= Received: from BLAPR03CA0144.namprd03.prod.outlook.com (2603:10b6:208:32e::29) by MN2PR12MB4269.namprd12.prod.outlook.com (2603:10b6:208:1d4::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:49:31 +0000 Received: from BL02EPF00021F69.namprd02.prod.outlook.com (2603:10b6:208:32e:cafe::86) by BLAPR03CA0144.outlook.office365.com (2603:10b6:208:32e::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:31 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F69.mail.protection.outlook.com (10.167.249.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:31 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:23 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 12/24] iommu/amd: Store per-segment iommu_dev_data in an xarray Date: Mon, 14 Sep 2026 18:47:38 +0000 Message-ID: <20260914184750.222939-13-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F69:EE_|MN2PR12MB4269:EE_ X-MS-Office365-Filtering-Correlation-Id: 068ac2ae-28db-489a-25c3-08df1290e9eb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700016|7416014|376014|1800799024|23010399003|11063799006|56012099006|10067099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: 9mdTotFSQR6KuBHs22/9MSlfeYBc8BTL0O8gxEwLNtqCTBzZSNehipXBHUlPcA/Qe4yYDCSk1CuwmnW/OT5QlX2K/j/NPA2cZZeHqEGNGnJMUyh3r9g3UdAqt9G9xzw1TW5MD9zFBK2+uvb+BeeWs9xBvk/PWp2rjLTlnQMyrKbSbX7+s6gmc9pwDPzrej2RfD1mjSA2snk0chh/szFERRdFGQrHMR+/ZtltU9+FCYxPkWqt2VxtnK/T+U4+yGsLeMmoWGn2YgtrYxTvhkfiUbFoMKP0GqcephZT3WccJ6+TxXCcDrqGpcjfL4dgwg+BdY+P/wc8rrQQNap/lYF5Sp5waI6+Ho/QHirStRcUlqn3CVI2sN3jJd71Xnt8nsiGr3cEEEZVR5NTabXqzmfj2VO0xBMP2VuRoZgfMmM2T62J1WMzWAShl1CBb0ha/YvHKGM/LN0aCzxC1Nr4cBmfVqNdaG3H9jy1a3Sm/Q76r4RG4PE4pkZThUFJjV6bAywvWjdYYi5HnjfPM5HjcNewNTBXsw3zSggBD8P82f5pz9MJyaUj9HRyHgknmXOU51eX4w0hnPUl5MaY9r15nsY916wGr4sGK5jsZBEZ6BWGHyfZaze6qPygYf0V78CI6/JpFvx72TXW4aGYwqmhjEGWDQxmDxfKF9KB0NAu3tGvBxxHKRYCTtYekHuFqOOU2kv1DCn0zhjfvwqu8Q2VX9we/g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700016)(7416014)(376014)(1800799024)(23010399003)(11063799006)(56012099006)(10067099003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: N6KkGwM1AJfo6brxLcms1mdFRlFq+6yDYI9XAnyt8tZZOLzHedLQCpFIa83ZioamZDmEmphUPbhdsczhEGQ3M7kU1qcV65b6jJk1X1w9u2UUg+WqsMbNBsNM+ZF51d56RuK1o1iqCrg6UaAb5Jtg969LhD2330AwRUjZdph6hQIwNKP7A9AN06NBDjwrPF97zRHSLSk5Xk2boAWkxiquaiHUKKCksuHZfXD6H6uNhgl04eAgejgQ4lfXNI6aOahrQYKmjoK+kWYNG0r+lKI985jhJiS9AXN+M4FvXm2jRsWNC7OEcM7WyMrL+BtiYzx1nvP4uU/0x1VzAEmKcVzwQubkyyIfAREQUOY9aLL8YvlQAh1wo3eKV17RSuSEZWBd/5jZ5PFrdXnYvnJMypf9k8u75nVtq80nNm6qgyO+Nq6SojZcHwbskBD4vDJXYAxr X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:31.8548 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 068ac2ae-28db-489a-25c3-08df1290e9eb 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: BL02EPF00021F69.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4269 Content-Type: text/plain; charset="utf-8" Replace the per-segment llist of iommu_dev_data with an xarray keyed by devid so lookup is O(1). These objects stay immortal for the life of the PCI segment: amd_iommu_release_device() keeps them for replug, and IRQ/DTE paths look them up locklessly via xa_load(). Same-devid intern on xa_cmpxchg() covers replug and racing find_dev_data(). Aliases still get separate keys. Intern remains local to iommu.c; only the lookup table changes. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 6 ++--- drivers/iommu/amd/init.c | 4 +++- drivers/iommu/amd/iommu.c | 34 +++++++++++++++-------------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 9a90b8fe0fe3..d02f49a39a90 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -22,6 +22,7 @@ #include #include #include +#include =20 #include =20 @@ -605,8 +606,8 @@ struct amd_iommu_pci_seg { /* List with all PCI segments in the system */ struct list_head list; =20 - /* List of all available dev_data structures */ - struct llist_head dev_data_list; + /* Immortal iommu_dev_data objects keyed by devid */ + struct xarray dev_data_xa; =20 /* PCI segment number */ u16 id; @@ -861,7 +862,6 @@ struct iommu_dev_data { 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 */ struct protection_domain *domain; /* Domain the device is bound to */ struct gcr3_tbl_info gcr3_info; /* Per-device GCR3 table */ struct device *dev; diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 17d321412dcc..1616fcfd361b 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -1737,7 +1737,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_seg= ment(u16 id, SZ_4K); =20 pci_seg->id =3D id; - init_llist_head(&pci_seg->dev_data_list); + xa_init(&pci_seg->dev_data_xa); INIT_LIST_HEAD(&pci_seg->unity_map); list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list); =20 @@ -1756,6 +1756,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_seg= ment(u16 id, free_dev_table(pci_seg); err_free_pci_seg: list_del(&pci_seg->list); + xa_destroy(&pci_seg->dev_data_xa); kfree(pci_seg); return NULL; } @@ -1779,6 +1780,7 @@ static void __init free_pci_segments(void) =20 for_each_pci_segment_safe(pci_seg, next) { list_del(&pci_seg->list); + xa_destroy(&pci_seg->dev_data_xa); free_irq_lookup_table(pci_seg); free_rlookup_table(pci_seg); free_alias_table(pci_seg); diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index dd2cb79d506f..2b039f38f9ef 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -390,9 +390,14 @@ static struct amd_iommu *rlookup_amd_iommu(struct devi= ce *dev) return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid)); } =20 +/* + * Allocate an immortal per-devid object stored in pci_seg->dev_data_xa. + * These are never erased: amd_iommu_release_device() keeps them for + * replug, and IRQ/DTE paths look them up locklessly via xa_load(). + */ static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 = devid) { - struct iommu_dev_data *dev_data; + struct iommu_dev_data *dev_data, *old; struct amd_iommu_pci_seg *pci_seg =3D iommu->pci_seg; =20 dev_data =3D kzalloc_obj(*dev_data); @@ -404,26 +409,23 @@ static struct iommu_dev_data *alloc_dev_data(struct a= md_iommu *iommu, u16 devid) dev_data->devid =3D devid; ratelimit_default_init(&dev_data->rs); =20 - llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list); + old =3D xa_cmpxchg(&pci_seg->dev_data_xa, devid, NULL, dev_data, + GFP_KERNEL); + if (xa_is_err(old)) { + kfree(dev_data); + return NULL; + } + if (old) { + kfree(dev_data); + return old; + } + return dev_data; } =20 struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid) { - struct iommu_dev_data *dev_data; - struct llist_node *node; - struct amd_iommu_pci_seg *pci_seg =3D iommu->pci_seg; - - if (llist_empty(&pci_seg->dev_data_list)) - return NULL; - - node =3D pci_seg->dev_data_list.first; - llist_for_each_entry(dev_data, node, dev_data_list) { - if (dev_data->devid =3D=3D devid) - return dev_data; - } - - return NULL; + return xa_load(&iommu->pci_seg->dev_data_xa, devid); } =20 static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data) --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012034.outbound.protection.outlook.com [40.107.209.34]) (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 5B2FC394793 for ; Mon, 14 Sep 2026 18:49:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.209.34 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411782; cv=fail; b=GT2SRHGXr0WKCokObXY1ObegAduf2E028KdyTZcGwt2hkjo7gjxMvhfjQptOip8Pfhw9XJrspAFQ+Jy+ACLvM7Mz2SCTnCkLsHqK56QSIS0JguvNLf2qyzBZbNQGB6tBvWDs5plb+eCGL1TSFYJaCZiMScyRCWKJmFn9bK/mfHg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411782; c=relaxed/simple; bh=WFfuKZfi90wyK8+/fvm9RPWlktW5gHwu93+5rBBGZHM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nfLgyk9jOnlahuHwR4KdBsNI74MVWR8rRvGEbLPPy6khVN3y2sEYjtlsnF4pmDTa21t/bY8JxLTU0fntuewe2BmVASKPpMNUST1YL/9l1e2BUF4JyPX9fYx0XHQqWGvhrOUQqsZnVMNi5nUk9SQm+Offn5Aj77GnJbazjqsoAZc= 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=5d6HrNrT; arc=fail smtp.client-ip=40.107.209.34 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="5d6HrNrT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IicpVXmaZAZyKSNEGD7u78aEskyxm34HWQQbKvt/rS4UEjs6x8Yip8hwvI9/ouZaUCaAe5FO8Hk+yUAtkiIKZdqyTsPK9QPTQ6VIe/CfzlQDEpBg9FLbWvXRxN+zFoGBpuLg6DieAG+MGd6bZtvv+UtxdSCCb3wBDOFwOt/mVO0FuztUO1kP9CuliCsY6VrcBu8yfJuhtBWyQZIdVJ3Ax8wJPOANxcnDDdXqe31IMNnBqMuosr/olti+oF/5xwDz2ces369JZJj5FU4jaSPax0ejbWCma6yp2H+Io1qoM5rKKNG3PZ6dAY2jjeYwZUQBudqjNew0KiefSVREed2v7A== 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=V6ea9NZeTrpT5tZEi1cJ/qwgOTWOalwyNdQdILXQEQo=; b=mRFR7XdMrXC/w3gUUwiZWvpH15pw9bVtTUWz62eaGTYYwW+B6bwGdg+CPapt62rk0HCRSvlTsqH51y4bwLJRs/MZCNMIbzSDu2d9BpAukzKYnFcsuoEmWUvEVUNu2LIXWoaNPuVFPWlEDyKd1zdR+afOidrL3Ncjq9JrKUZC/O9BD/4zVVfloWgpjPvapfWpeze0l41SCCRICvLcreRKb18tXS7zFuvBfU4A4A7IreOutH2vC26VGDuVi57//yVWjonpUWT6ctsysm3ANw879ja7dodVFx9CAj7Lba/vTzVrFaNkv3rb0MULfjwLy6rM453juQS8dzCLSrXQZiYz5Q== 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=V6ea9NZeTrpT5tZEi1cJ/qwgOTWOalwyNdQdILXQEQo=; b=5d6HrNrTy6duFOho+bFemwUIFx2qd2SYonaF4hZ8f7Sm/f9+D74IxNJi16goFLYHLquVhN9INzZpr/LvVwV2DzS3YuRTU/jpuxmzcNLO+zgECM9da2FC++PZWXFbIa8TPe/PzEFH5dxtyYBalaubcPDGzCru2+KZufn+yYIVh5k= Received: from BN9PR03CA0204.namprd03.prod.outlook.com (2603:10b6:408:f9::29) by MN2PR12MB4173.namprd12.prod.outlook.com (2603:10b6:208:1d8::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:49:35 +0000 Received: from BL02EPF00021F6D.namprd02.prod.outlook.com (2603:10b6:408:f9:cafe::11) by BN9PR03CA0204.outlook.office365.com (2603:10b6:408:f9::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:35 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6D.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:35 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:29 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 13/24] iommu/amd: Program IOMMU DTE with the private IPA domain Date: Mon, 14 Sep 2026 18:47:39 +0000 Message-ID: <20260914184750.222939-14-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6D:EE_|MN2PR12MB4173:EE_ X-MS-Office365-Filtering-Correlation-Id: dfbe8643-d4ae-4b2a-ed40-08df1290ec25 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|23010399003|1800799024|82310400026|36860700016|6133799003|18002099003|22082099003|10067099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: i+QfI9PrHq+sbvg989uhAoYsrHiwpMLhRiduIDsywvR60ypfB/TIunKegVQWGzJLPYWlEjkv1xMVjoqFXctKgkJJESTiLkhhjLCyOeDIXND2FP4UtpbkFKxpsBH8i1No1xj/OVDE58Q3wh0AMrOiDrNoe2Aj6uy53L9CiDsOSp6Von26HQvu8jdCZfyjhEYYbjmAjOa4QzL9n0yT8LdfHOnhZJl7n3PPeK/AhPR62HrPG3AWSpamlwXr0BP9zGKcLEW3+kNAQxnxjTA1wzbPubRIwEVSdqrLrWrY6eMaNsP21Mkbaqjl9jmRiWU6VwKfUfZVVO9GUDafpw//u3hqNfXb8zjVxE3HtK+TB2dLd6bOmWWRAwHiF0+woRyokc89AHN2E/GjAl0H24JjI+gH4cEYEuZXYsrW8h9lJbaUIFN6TkQgdgWrKFQ/3hAhDsjlbaRb1QMX2H0A+WvIED141ADc4wBGywfnNDW8s1zJXXu8CgKTC1lfJVgHcwzEEB9bTbPafEYUl00kgS+rSxL6EMUzfPzG6M+5QzcYuhZjhXJr7LVbJ+ZQTWuZoyIETp4oDUJCAY+XGSOOmOO+2wZLIdHoUmtGm3mTzXHiIr2kc7keu0cgWIpmKJFEqw5Qri8gvzE5bJNvpCZvX/ttAnbexj+naabFFAYs16MckxZE/RDn6XY2s5e29TfG/X4AgVrRiK1ebzIzoQs7WseghYUNuA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(7416014)(376014)(23010399003)(1800799024)(82310400026)(36860700016)(6133799003)(18002099003)(22082099003)(10067099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wYcpqU6tj+BrjavN1VLG7qnnUPTVBb3ITG93plkWrmJ57f/ciNZXY4dyvFTj0Agfln8Q/z66xljpuISIYLpjvFfsuVvIpePPoebfet+2EkGUtJbnWBsZti7crg+K/N5/tN+6/hcm1Yq8wgdmYsZU4UsQYrqKyZOezrLfF+0WdRGYqiqsz5EnFhItDmbcRicjwHk5oJbtn9SYL54Qki3Td3+bIvwylC22txSv8JvoRpEYvTnrslxarFoJvum39+CV71W2uHjb5QZLUxXLDExED6sJQAJ3nMThG5xZEpTgRrRcvXkmD2k1XSLr0a0g+bKQchiaM+C8saQMd22DzHXKG/8k0ZZaN2h0eUOtGRs2eJOCzHNPC0H5/dtclTY+BbKWrwzLyxOpo+6iqDy8UZg1aKSx/ijPvz8RxfDE/NBEj604G/sX4krbOA7sxyHEn+Ry X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:35.5959 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dfbe8643-d4ae-4b2a-ed40-08df1290ec25 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: BL02EPF00021F6D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4173 Content-Type: text/plain; charset="utf-8" The IOMMU PCI function does not go through the normal device attach path, so it has no iommu_dev_data. Add amd_iommu_alloc_dev_data() and amd_iommu_free_dev_data() for a synthetic object that is not interned in pci_seg->dev_data_xa. Program that DTE with the private IPA domain's v1 page table so the IOMMU can DMA to its own vIOMMU backing store. Leave the synthetic DTE off pdom->dev_list and with no struct device so clone_aliases() is skipped; it is only iommu->devid. Rewrite it from amd_iommu_change_top() when the IPA table grows so later high maps remain reachable. Do this after viommu_private_space_init() so viommu_pdom exists. Clear the DTE and free the object on teardown. Failed self DTE allocation unwinds through amd_viommu_uninit(). Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 4 ++ drivers/iommu/amd/amd_iommu_types.h | 2 + drivers/iommu/amd/iommu.c | 102 +++++++++++++++++++++++++--- drivers/iommu/amd/viommu.c | 48 +++++++++++++ 4 files changed, 148 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 9cc5da745a73..6ac64524208b 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -50,6 +50,10 @@ extern u8 amd_iommu_hpt_vasize; extern unsigned long amd_iommu_pgsize_bitmap; extern bool amd_iommu_hatdis; =20 +struct iommu_dev_data *amd_iommu_alloc_dev_data(u16 devid); +void amd_iommu_free_dev_data(struct amd_iommu *iommu, + struct iommu_dev_data *dev_data); + /* Protection domain ops */ void amd_iommu_init_identity_domain(void); struct protection_domain *protection_domain_alloc(void); diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index d02f49a39a90..4b48ec8bacd6 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -814,6 +814,8 @@ struct amd_iommu { struct ida gid_ida; /* guest IDs for this IOMMU */ =20 /* HW vIOMMU support */ + /* Synthetic IOMMU-self DTE; not in pci_seg->dev_data_xa or pdom->dev_lis= t */ + struct iommu_dev_data *viommu_dev_data; struct protection_domain *viommu_pdom; void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT]; }; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 2b039f38f9ef..bcbd5af75d00 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -217,7 +217,12 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, struct dev_table_entry *new) { update_dte256(iommu, dev_data, new); - clone_aliases(iommu, dev_data->dev); + /* + * Synthetic DTEs (vIOMMU self, translate-device-id) leave + * dev_data->dev NULL so clone_aliases() is skipped. + */ + if (dev_data->dev) + clone_aliases(iommu, dev_data->dev); device_flush_dte(iommu, dev_data); iommu_completion_wait(iommu); } @@ -391,14 +396,13 @@ static struct amd_iommu *rlookup_amd_iommu(struct dev= ice *dev) } =20 /* - * Allocate an immortal per-devid object stored in pci_seg->dev_data_xa. - * These are never erased: amd_iommu_release_device() keeps them for - * replug, and IRQ/DTE paths look them up locklessly via xa_load(). + * Allocate a synthetic DTE object that is not inserted into + * pci_seg->dev_data_xa. Lifetime is owned by the caller (vIOMMU + * self DTE or translate-device-id). */ -static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 = devid) +struct iommu_dev_data *amd_iommu_alloc_dev_data(u16 devid) { - struct iommu_dev_data *dev_data, *old; - struct amd_iommu_pci_seg *pci_seg =3D iommu->pci_seg; + struct iommu_dev_data *dev_data; =20 dev_data =3D kzalloc_obj(*dev_data); if (!dev_data) @@ -408,6 +412,22 @@ static struct iommu_dev_data *alloc_dev_data(struct am= d_iommu *iommu, u16 devid) spin_lock_init(&dev_data->dte_lock); dev_data->devid =3D devid; ratelimit_default_init(&dev_data->rs); + return dev_data; +} + +/* + * Allocate an immortal per-devid object stored in pci_seg->dev_data_xa. + * These are never erased: amd_iommu_release_device() keeps them for + * replug, and IRQ/DTE paths look them up locklessly via xa_load(). + */ +static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 = devid) +{ + struct iommu_dev_data *dev_data, *old; + struct amd_iommu_pci_seg *pci_seg =3D iommu->pci_seg; + + dev_data =3D amd_iommu_alloc_dev_data(devid); + if (!dev_data) + return NULL; =20 old =3D xa_cmpxchg(&pci_seg->dev_data_xa, devid, NULL, dev_data, GFP_KERNEL); @@ -428,6 +448,22 @@ struct iommu_dev_data *search_dev_data(struct amd_iomm= u *iommu, u16 devid) return xa_load(&iommu->pci_seg->dev_data_xa, devid); } =20 +void amd_iommu_free_dev_data(struct amd_iommu *iommu, + struct iommu_dev_data *dev_data) +{ + if (!dev_data) + return; + + /* + * PCI/alias objects in the xarray are immortal. Never kfree + * those; synthetics must not be stored there. + */ + if (WARN_ON_ONCE(search_dev_data(iommu, dev_data->devid) =3D=3D dev_data)) + return; + + kfree(dev_data); +} + static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data) { struct dev_table_entry new; @@ -1826,7 +1862,11 @@ static int device_flush_dte(struct amd_iommu *iommu,= struct iommu_dev_data *dev_ u16 alias; int ret; =20 - if (dev_is_pci(dev_data->dev)) + /* + * Synthetic DTEs leave dev_data->dev NULL; flush iommu->devid + * rather than walking PCI DMA aliases. + */ + if (dev_data->dev && dev_is_pci(dev_data->dev)) pdev =3D to_pci_dev(dev_data->dev); =20 if (pdev) @@ -2770,6 +2810,51 @@ static spinlock_t *amd_iommu_get_top_lock(struct pt_= iommu *iommupt) return &pdom->lock; } =20 +#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD) +/* + * The vIOMMU private IPA domain programs a synthetic DTE for the + * IOMMU's own requester ID so hardware can DMA to backing store. + * That object is not on pdom->dev_list: it is not an IOMMU-API + * attach, and walkers such as rlookup and clone_aliases assume a + * real struct device. + * + * amd_iommu_change_top() therefore misses it. Early maps fit under + * the initial page-table top (PT_FEAT_DYNAMIC_TOP). Later DevID and + * DomID maps at high IPA call increase_top(); the old root stays + * live as a child of the new one, but the self DTE still holds the + * old MODE and would not translate those IOVAs. + * + * Walk iommu_array (from amd_iommu_pdom_bind_iommu()) and rewrite + * iommu->viommu_dev_data when this domain is that IOMMU's private + * IPA table. set_dte_entry() skips clone_aliases() because the + * synthetic DTE has no struct device. + */ +static void update_viommu_self_dte(struct protection_domain *pdom, + phys_addr_t top_paddr, + unsigned int top_level) +{ + struct pdom_iommu_info *pdom_iommu_info; + unsigned long i; + + lockdep_assert_held(&pdom->lock); + + xa_for_each(&pdom->iommu_array, i, pdom_iommu_info) { + struct amd_iommu *iommu =3D pdom_iommu_info->iommu; + + if (iommu->viommu_pdom !=3D pdom || !iommu->viommu_dev_data) + continue; + set_dte_entry(iommu, iommu->viommu_dev_data, top_paddr, + top_level); + } +} +#else +static inline void update_viommu_self_dte(struct protection_domain *pdom, + phys_addr_t top_paddr, + unsigned int top_level) +{ +} +#endif + /* * Update all HW references to the domain with a new pgtable configuration. */ @@ -2792,6 +2877,7 @@ static void amd_iommu_change_top(struct pt_iommu *iom= mu_table, device_flush_dte(iommu, dev_data); } =20 + update_viommu_self_dte(pdom, top_paddr, top_level); domain_flush_complete(pdom); } =20 diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 89d6dc520b2c..3ec907a498c2 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -40,11 +40,55 @@ static void __init amd_viommu_vf_vfcntl_unmap(struct am= d_iommu *iommu) } } =20 + +static void viommu_free_self_dev_data(struct amd_iommu *iommu) +{ + struct iommu_dev_data *dev_data =3D iommu->viommu_dev_data; + struct dev_table_entry new =3D {}; + + if (!dev_data) + return; + + amd_iommu_make_clear_dte(iommu, dev_data->devid, &new); + amd_iommu_update_dte(iommu, dev_data, &new); + amd_iommu_free_dev_data(iommu, dev_data); + iommu->viommu_dev_data =3D NULL; +} + +static int viommu_alloc_self_dev_data(struct amd_iommu *iommu) +{ + struct protection_domain *pdom =3D iommu->viommu_pdom; + struct pt_iommu_amdv1_hw_info pt_info; + struct iommu_dev_data *dev_data; + struct dev_table_entry new =3D {}; + + dev_data =3D amd_iommu_alloc_dev_data(iommu->devid); + if (!dev_data) { + pr_err("%s: Failed to allocate dev_data\n", __func__); + return -ENOMEM; + } + /* + * Synthetic DTE for iommu->devid only: no struct device, so + * amd_iommu_update_dte() skips clone_aliases(), and not on + * pdom->dev_list (not an IOMMU-API attach). + */ + dev_data->dev =3D NULL; + dev_data->domain =3D pdom; + iommu->viommu_dev_data =3D dev_data; + + amd_iommu_make_clear_dte(iommu, iommu->devid, &new); + pt_iommu_amdv1_hw_info(&pdom->amdv1, &pt_info); + amd_iommu_set_dte_v1(dev_data, pdom, pdom->id, &pt_info, &new); + amd_iommu_update_dte(iommu, dev_data, &new); + return 0; +} + static void viommu_private_space_uninit(struct amd_iommu *iommu); =20 void __init amd_viommu_uninit(struct amd_iommu *iommu) { iommu->flags &=3D ~AMD_IOMMU_FLAG_VIOMMU_EN; + viommu_free_self_dev_data(iommu); viommu_private_space_uninit(iommu); amd_viommu_vf_vfcntl_unmap(iommu); } @@ -319,6 +363,10 @@ int __init amd_viommu_init(struct amd_iommu *iommu) if (ret) goto err; =20 + ret =3D viommu_alloc_self_dev_data(iommu); + if (ret) + goto err; + iommu->flags |=3D AMD_IOMMU_FLAG_VIOMMU_EN; return 0; err: --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013017.outbound.protection.outlook.com [40.93.196.17]) (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 5EDC23B9616 for ; Mon, 14 Sep 2026 18:49:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411787; cv=fail; b=PRvS0LO6pbe6M/SXgdfPY3Yr9zkKaQfsRNGa6n3RpoZOQiV5LqqemjCdTeGM/yM4W+xe0JP4KA0qGP4DyGkH7esdV++fBCUieO6k0icbJzxZYW44Pi6PDAMekV47KvoEAFSZ6bw19VxTX9Twcjcg+beJ7vEHMWK4KI3yxZZmcCY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411787; c=relaxed/simple; bh=B9Nc3TyWoBbNX2aoMwxyfOuAk1N0YyXhollmPZE849I=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=E3Xd3gFatFZQ0SicsyH9nkkfo9ZQT7NHrHXlFQsidwRkQkvmwGlbXgSW3thy1R427Gh8cX0t1N+8VHAj1EK5rTR5B57zwElWZYW6osgroGE0UEFllxY7VMsd1HtoIQSxQxg3wmc18Gwbp33lGwJ2NyivQ6vBxbOu+AmkC4WdkUU= 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=lKhwlnME; arc=fail smtp.client-ip=40.93.196.17 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="lKhwlnME" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KdiPayfZLAEQrz4jZXh7acl4PduYYmz6DCgqqjaQbeXYqr99PcW2KXAxaS/o1fprNOH13lpSy2B0C4djjFE1O9RCjvfTTzLOmKau1AauGhJM1fQox8p+dGk7TqmgFO5ZGCVfYqirJ8Ta6gglSZhvC3jIzWDN/kIdFMbRErIIUHteMVi3IUx/DyMvg1zNH2RSKvwGmWuKPjVLmuog4EzdSCgbAhzUAZvGktHyf1Z0UVBGRKzEGmlxHQK49ILgIRUgFMnfIA4csacrAFglxxI4xp3gfwPRLvpANIlSE0PQp2sRI7I5ErrJ5+knQoKaZA89Z0vknOtK2GAASnixo6si+g== 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=yj6wb8CaUbARH7V4pjgJ0IH/JYUi0tTvWgDPWqG5f08=; b=U9NtZ8ZNbTsT8/l1Hi9t9PSpa2crzpxheta3xpGGkptJHi3VUvjmqUeyK4bS+UPyz5MltSU7hpYk1BK3HsJA5Izib+PCO1oLOGr/JgWBvB/Ec8KnH7ir+t3Gq1tPzbuiNbkIvp+8qqjPOSCxtOrRwI7/YuEHaWrQuOkqMkQayQawBZwBOn7D94M7hfkRgm0pLVZOMYUbN749TGhoxl+Z4EsSmsyuit6GjnsJpZo3D43/RbSJ5Uk71ogcss5aqIDaLGTwCovwcQizpz03l5AdCQwYXBDOLIRbHtmQaj9vKYNkA867Du8Jt6k8Anenu3u/R1UMgsu2gvuxGF0W/UbK0A== 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=yj6wb8CaUbARH7V4pjgJ0IH/JYUi0tTvWgDPWqG5f08=; b=lKhwlnMExzZEHfrbKA6tR8L4ifXve4YfHFTyrHCQKHla+RtAN/ZBVVGmEZFlJ3vD71uuwkXLx7mFD/3kmTRIbl/FN49xbNkC0DOR+MqEn8PftFrt4HHz1XS9nt5HR1JHfUGJGmMKUV0t07wpQ3w47ae2UPQHM05hEs7iDmvCb6g= Received: from BN9PR03CA0207.namprd03.prod.outlook.com (2603:10b6:408:f9::32) by PH7PR12MB8778.namprd12.prod.outlook.com (2603:10b6:510:26b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:49:42 +0000 Received: from BL02EPF00021F6D.namprd02.prod.outlook.com (2603:10b6:408:f9:cafe::8) by BN9PR03CA0207.outlook.office365.com (2603:10b6:408:f9::32) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:41 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6D.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:41 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:35 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 14/24] iommu/amd: Add per-VM private IPA alloc/map helpers Date: Mon, 14 Sep 2026 18:47:40 +0000 Message-ID: <20260914184750.222939-15-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6D:EE_|PH7PR12MB8778:EE_ X-MS-Office365-Filtering-Correlation-Id: f6511491-1a50-4106-e63b-08df1290efc0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|36860700016|7416014|376014|82310400026|1800799024|10067099003|11063799006|56012099006|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: fM6gC6m7AB7CMfFSCIHLWmoD37oWlZD5VaMBiH/ackZfgQwB54TlGrn01a1ISbguuFLxlDTKGxHl5Ah8y6xaIQWr5lZHyJKtoF5c+fRWqn/zQdE9r0Z+XdusnpL227AWFcyG4PZqTTy8by7eCe9d8qrJruqttxlD30ILX5una9GIRDiCO6tykELj+NgIaR0fide4blBe1tYZyYRlIjFbzkGIWCwytNv4rONbiIk3l/B6jSzWNBhbk/1t69stE+iZUe+vaIwp+kezfTyUR1LqWYQ1u+OLHiswOXkdS83Sm76CTTOpOd4/unN5Jq6ezuycA/gm+4Sju7NuNWjngn0+XKcIgJSSQHKcss7PyOEypLh7P9vtam7uGBJojj6P3izDggQWvm8ZcKEVmsCdGCOOFMeJSzemfvIqlJFhH3PnDDiFxIG4xJNeS80hxbMpGVJDTVyrxuWFgykbjlq6W2zUZsShOZt6v7LEFaTN9H/43Ao1+OuiY3zfky1hHesDNdIBPObKnxkl579WfN3lcZIwk80oeYxGv8RxskMidiv8k/p3CYfRWDUhuKzlK60cVoiqF2v5Lda0dccL9n9alChN0fR2GF1MGCWfU45BibyA6Qq2hCIOwrgvrx9meVmC8lX8gboGC/ZyG76mKQfbaM/vzNbkzr6QlMNLzhrMtDJeZW6LTjrDsl/hL57zuyImZhFRW61q7qh9bYSKD9GrCsQK/A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(36860700016)(7416014)(376014)(82310400026)(1800799024)(10067099003)(11063799006)(56012099006)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RokPEriA7Yv0+SYGZg1ToW8Mt3zN10qELbUUUvrG8O/f+P/EfMuDMox9s8hCUDepd0PMRnIcas/q987Y3O/MLvzaMmtNOnoHlEZDRTVIRd4wvtogBGT0q+xA9gmgsilBhWCmix+Zqd1/pGN5tMvzj4nYQF6I/r6evQGPY8a4OlsVWheKnqUetMQwielX3iRzCYH9WNJrKjF6+UhmJoiJICuE1qfWgls07Gv4DBTfU1UDQpTI1N2iciipKRCY7lA4gHScTCbLCYTHPKkE+wBCqQA0EVJ82LWYAjjjutM06u4xhbr9ZTS9JiDIFE76faByHaV57bavZyQAYIugh87emHV7LdzvqC/TcigJ+4axaWb8U1FocBinp7MfjEl1wRe71pbRuZTziNcuXSrsTTOXSWlEY7HgsTK3SHxDQL2xTdCMPKR1ZxuI9z+zf9ZRFSeF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:41.6443 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f6511491-1a50-4106-e63b-08df1290efc0 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: BL02EPF00021F6D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8778 Content-Type: text/plain; charset="utf-8" Guest device ID and guest domain ID tables use dedicated slots in the vIOMMU private address (IPA) region, indexed by guest ID (GID). Add alloc_private_vm_region() and free_private_vm_region() to allocate backing pages, map them through viommu_pdom, and tear down with an unmap flush on VM destroy. Charge GFP_KERNEL_ACCOUNT so the DevID/DomID pages count against the caller's memcg. iommufd already charges the vIOMMU object. Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/viommu.c | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 3ec907a498c2..ad5f754f25a4 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -373,3 +373,42 @@ int __init amd_viommu_init(struct amd_iommu *iommu) amd_viommu_uninit(iommu); return ret; } + +static int __maybe_unused alloc_private_vm_region(struct amd_iommu *iommu,= u64 **entry, + u64 base, size_t size, u16 gid) +{ + int ret; + void *va =3D NULL; + u64 addr =3D base + (gid * size); + + /* Charge the caller; boot IPA stays GFP_KERNEL. */ + ret =3D viommu_priv_alloc_map(iommu, addr, size, GFP_KERNEL_ACCOUNT, &va); + if (ret) { + *entry =3D NULL; + return ret; + } + + *entry =3D (u64 *)va; + + pr_debug("%s: entry=3D%#llx(%#llx), addr=3D%#llx, size=3D%#lx\n", __func_= _, + (unsigned long long)*entry, iommu_virt_to_phys(*entry), addr, size); + + return 0; +} + +static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu,= u64 **entry, + u64 base, size_t size, u16 gid) +{ + u64 addr =3D base + (gid * size); + + if (!iommu || !iommu->viommu_pdom || !*entry) + return; + + pr_debug("%s: entry=3D%#llx(%#llx), base=3D%#llx, addr=3D%#llx, size=3D%#= lx\n", + __func__, (unsigned long long)*entry, + iommu_virt_to_phys(*entry), base, addr, size); + + viommu_priv_unmap_free(iommu, addr, size, *entry); + + *entry =3D NULL; +} --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013000.outbound.protection.outlook.com [40.93.196.0]) (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 4C49337F339 for ; Mon, 14 Sep 2026 18:49:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.0 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411796; cv=fail; b=d1Cm+dhbQAXYGa/fCEJHDSmzbLpz9hk7qwUDt0Oge5WJofU/R0qjJXT6daICDwMd2OtFxwG8KgiaEDkZErTLMMdgJ0ZIotObpcFzPDQPnYv7h2dtsysCA6NNCGmzyC8ZFt+xmo6ZXLa/bOaVyvOS+n5JzSF2d233pm4OopCa/uU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411796; c=relaxed/simple; bh=Wep0WN1PQT34DBB3wwnr+fX7MAZzFK8ChvhhrhIV6cw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lHAR1XsPApHy1FV5c6b1K8+fAV+HX9H7lnFdr4XT7np5LvrjWCGy/jjrw3GyPth2l7CpPqPjxDYhtQruhdYlXI2FJ/chPm27R24IpNkMGp7UO1RHu91d0ztFgaBr5LvZAx1SjaZJKDm77dyW+UHFGdCJ5HMEvbNxOTegqBIAmIs= 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=HN76IM7/; arc=fail smtp.client-ip=40.93.196.0 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="HN76IM7/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gfadn+z5oypMyrmPwcbziCSaOG6cDbhGbWaIE7wB93kpI20llsnuh4CS9sSz7J3k7t2fvMRC7a4G2MJLH1PM8m2YS07RPDz2e6kVHxJPpKP4sYl2Flle29aUAzG5exMzPEeE80e+9VU316Bfq3eqvCgBZBxnk35DsCn7td5AOYj9jqsoPhySZsj+SuHMsylvE8PR3Ftm9VQzzOBUt0aLk3wSj8lXs6lMj9X8D6n9w0Q3gigAX1sJM377FHqFUUOi+4G7XMZu/8fK2AwOPIFodgap47f6zdTA3zpruQjMdN0UX+Zhpn4DG2pqO32EfKkdCtplcktrYuOPPbFi5jC8dA== 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=jjdOnBxtL2aQdcodo0iQ5iE1RPP/y9FpkOyZW4aJvak=; b=KYrrRdq2CZU8IMITka8N/72rKo73RiewRpq+TWOjs+QfUdtxC+wvrTromdRbb5N2wb06+y9o9m77lOl/hktIzlfmI9YEJWRyspWiz1e0BabxQqHLQz+KNAli+9AV3bMvakYPiP470/oU+EG5D0SoD7XtY9bAgQ0sOF7kL04TdP/b3x2UoNxvhRtzqxhJLvgv7lA9XzNTL8YjSkzgNdZ5A1C62fsCdTdA9Mo6Mpoi8MrO6Kb+/mqCjdPSeJ32XkFB5tQ3sBcH9/n9q4UiSQs7McmQR+51ajF2YMTxjbnYKunAggKtPNnjAcQaa6T5WAwIV0tF0MTCLOU/DKkWsPR80w== 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=jjdOnBxtL2aQdcodo0iQ5iE1RPP/y9FpkOyZW4aJvak=; b=HN76IM7/1R4QYthtX4+r1aJPadrdK/9++BKUgxjD+KDYi6QaSXuvPy29+zqndQQHZhSvRO96ZH06fJiXDv40tFA+f/mqpqo1JCo1K/HXjMgFms7zKcVYcWTdEMHUp/riI/fm+VPXofNCqvQ8Rpq+e6uy2MId/f4oJ6ws6sASTXs= Received: from BN9PR03CA0946.namprd03.prod.outlook.com (2603:10b6:408:108::21) by PH0PR12MB8006.namprd12.prod.outlook.com (2603:10b6:510:28d::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:49:48 +0000 Received: from BL02EPF00021F6E.namprd02.prod.outlook.com (2603:10b6:408:108:cafe::8) by BN9PR03CA0946.outlook.office365.com (2603:10b6:408:108::21) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:47 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6E.mail.protection.outlook.com (10.167.249.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:47 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:41 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 15/24] iommu/amd: Add helper functions to manage DevID / DomID mapping tables Date: Mon, 14 Sep 2026 18:47:41 +0000 Message-ID: <20260914184750.222939-16-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6E:EE_|PH0PR12MB8006:EE_ X-MS-Office365-Filtering-Correlation-Id: 322acb3e-62d3-4a5e-449c-08df1290f356 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|7416014|23010399003|376014|36860700016|6133799003|18002099003|22082099003|11063799006|10067099003|56012099006; X-Microsoft-Antispam-Message-Info: 3q8A/9k3kYI7c6IGwBEpo1HFLCvC7cFbJlgEZcm8yIEFbmfRtsa5oUR2bbRLSLtYsFvSwO+4UqM4j+tJjy9Eca6kBGe7/buW00XMoDlEZvb51ru4D/q71oL5gZXERBYqi/cTEg2LNjYBaSVYIuAiyzhirylEy3Ht/Nxs0SbfS+OMSizWbhkCbd/c4KcQkw1Phtz+z5n64ZNHTaoGc4xLwgaeNced4D5gM/KRPQK8Y1hRQ00lo/ACJj6jgQZXWlXjlyEMKDjAxMBkTPpYuRNAgTnK6hUB/mRFOwTjRqyH0Q9UZReNRrjkX9MuwdLrmJui4tq/G/5qBnoq+gSkijJ/uHZBMiGRnrCG1dTtXFXGj5w4djMtDt1JdK2kDP6eIZACnxCReQivH15mDchojzwtU9I1Sqp8Qy6MQu0ZqzCwtMUQJBY9wsJyeCbmVHCKuqcD3pgmBfET4rfBwl0eV/Z8ELG8NQRiCO1byPS76E6bwbBEZduX82YoAAq4vb4PsS5Cc+B80bd3Sste9WylGbDiz9LgUQrsmCBTJzPNRYIXksAVTi1HupCBDpfmSmqtJ6H6qkL0XyOgrpQVe54fvyJlGMGJfZn2J/XIR68obdZY1KiZoYkr6MGqZ1s0MM3xxOXNFskUkAAhGmWZnPUR6HgItIawDHfrg58dWO9NxoccUbKR25tehbVvMAyn7EPISJ9h+jXh4BszDI3Y5K0bekIqGg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(7416014)(23010399003)(376014)(36860700016)(6133799003)(18002099003)(22082099003)(11063799006)(10067099003)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kMkUdc2HrvbbzD8ZymGArjZguKIXCbBxtAr6x4zKcDfwBOCreNc0eKYQrs39zgXJ1yzTi28K9ipCzJ/F/ZTHMW5CjoRRTR3Qq5YWpfd1W8pnxoIb9MFei+UlBDzXK5NfbJGqCnJzF8lwqeh9sJ1DN7WQ0eS5jPL2/ewccywxfZmy5/EKORfYhReiQHAQpP9+Wu7ZxlSjoV6C4e61A3vqD0TAYeJnBCQ2XLSyDESrQ+4ZaoD0Se9B3AirUPrcz6YYN3smurZNgJStbP0hoIYAKp+YGAV+gqf2cIVMNg0XID5lkHlr5IufoeFVI4vGZGQ0k3QIIPy0YIlQTYcGD5eZidNqQgHOqwnfI3aOXZ9gtJyALWuR+HGgik7Q6XrQm4kcoNC56KreyjcXGkjGCohM8e1qRCI93FfE/ir3pFzvMqzLgKtq/Tmk/2S2BTa/aRlD X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:47.6571 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 322acb3e-62d3-4a5e-449c-08df1290f356 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: BL02EPF00021F6E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8006 Content-Type: text/plain; charset="utf-8" Introduce amd_viommu_init_one() and amd_viommu_uninit_one(). These functions are called during IOMMUFD vIOMMU initialize and destroy. Currently, it manages the IPA mapping for Device ID and Domain ID mapping tables. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 3 ++ drivers/iommu/amd/amd_viommu.h | 4 ++ drivers/iommu/amd/iommu.c | 1 + drivers/iommu/amd/iommufd.c | 9 +++- drivers/iommu/amd/viommu.c | 64 +++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 4b48ec8bacd6..fc8e1378c7ed 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -544,6 +544,9 @@ struct amd_iommu_viommu { */ struct xarray gdomid_array; =20 + u64 *devid_table; + u64 *domid_table; + /* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mma= p(). */ unsigned long vfmmio_mmap_offset; }; diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index 276f2126290f..580cad0f9f5a 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -16,6 +16,10 @@ void __init amd_viommu_uninit(struct amd_iommu *iommu); =20 u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid); =20 +int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *= viommu); + +void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *viommu); + #else =20 /* diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index bcbd5af75d00..d620586d5734 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -43,6 +43,7 @@ #include =20 #include "amd_iommu.h" +#include "amd_viommu.h" #include "iommufd.h" #include "../irq_remapping.h" #include "../iommu-pages.h" diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index 2e34491e3cf7..97de07d3ad10 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -97,11 +97,15 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viom= mu, struct iommu_domain * if (ret) goto err_init; =20 + ret =3D amd_viommu_init_one(iommu, aviommu); + if (ret) + goto err_init; + ret =3D iommu_copy_struct_to_user(user_data, &data, IOMMU_VIOMMU_TYPE_AMD, out_vfmmio_mmap_offset); if (ret) - goto err_init; + goto err_init_one; =20 viommu->ops =3D &amd_viommu_ops; =20 @@ -110,6 +114,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viom= mu, struct iommu_domain * spin_unlock_irqrestore(&pdom->lock, flags); =20 return 0; +err_init_one: + amd_viommu_uninit_one(iommu, aviommu); err_init: iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); err_mmap: @@ -132,6 +138,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_v= iommu *viommu) spin_unlock_irqrestore(&pdom->lock, flags); xa_destroy(&aviommu->gdomid_array); iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); + amd_viommu_uninit_one(iommu, aviommu); amd_iommu_gid_free(iommu, aviommu->gid); } =20 diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index ad5f754f25a4..06a21f14b2a5 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -19,6 +19,32 @@ #include "amd_viommu.h" #include "../iommu-pages.h" =20 +/* + * Per-vIOMMU DevID / DomID tables share iommu->viommu_pdom: + * IOVA =3D base + gid * entry_size, gid in [1, VIOMMU_MAX_GID]. + * The 64GB gap between these bases is 0x10000 1MB DevID slots. + * VIOMMU_MAX_GID is 0x7FFF, so a max-gid DevID window ends at + * 0x1800000000 and cannot meet VIOMMU_DOMID_MAPPING_BASE. + * + * VIOMMU_MAX_GDEVID / VIOMMU_MAX_GDOMID are 16-bit guest DevID + * and DomID table indices (hardware mapping-table width), not GID. + */ +#define VIOMMU_MAX_GDEVID 0xFFFF +#define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL +#define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20) + +#define VIOMMU_MAX_GDOMID 0xFFFF +#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL +#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) + +static_assert(VIOMMU_DEVID_MAPPING_BASE + + (VIOMMU_MAX_GID + 1ULL) * VIOMMU_DEVID_MAPPING_ENTRY_SIZE <=3D + VIOMMU_DOMID_MAPPING_BASE); +static_assert(VIOMMU_DEVID_MAPPING_ENTRY_SIZE >=3D + (VIOMMU_MAX_GDEVID + 1ULL) * sizeof(u64)); +static_assert(VIOMMU_DOMID_MAPPING_ENTRY_SIZE >=3D + (VIOMMU_MAX_GDOMID + 1ULL) * sizeof(u64)); + static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) { if (iommu->vfctrl_base) { @@ -412,3 +438,41 @@ static void __maybe_unused free_private_vm_region(stru= ct amd_iommu *iommu, u64 * =20 *entry =3D NULL; } + +void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *aviommu) +{ + pr_debug("%s: gid=3D%u\n", __func__, aviommu->gid); + + free_private_vm_region(iommu, &aviommu->devid_table, + VIOMMU_DEVID_MAPPING_BASE, + VIOMMU_DEVID_MAPPING_ENTRY_SIZE, + aviommu->gid); + free_private_vm_region(iommu, &aviommu->domid_table, + VIOMMU_DOMID_MAPPING_BASE, + VIOMMU_DOMID_MAPPING_ENTRY_SIZE, + aviommu->gid); +} + +int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *= viommu) +{ + int ret; + + ret =3D alloc_private_vm_region(iommu, &viommu->devid_table, + VIOMMU_DEVID_MAPPING_BASE, + VIOMMU_DEVID_MAPPING_ENTRY_SIZE, + viommu->gid); + if (ret) + goto err_out; + + ret =3D alloc_private_vm_region(iommu, &viommu->domid_table, + VIOMMU_DOMID_MAPPING_BASE, + VIOMMU_DOMID_MAPPING_ENTRY_SIZE, + viommu->gid); + if (ret) + goto err_out; + + return 0; +err_out: + amd_viommu_uninit_one(iommu, viommu); + return ret; +} --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11010004.outbound.protection.outlook.com [52.101.56.4]) (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 3D97925B092 for ; Mon, 14 Sep 2026 18:50:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.56.4 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411807; cv=fail; b=NoT+BnpdwF+oo8zrUYngnzRDjWwZ6yltMYXf3DiqNWJ2pKfNfTytBKMAJVdlh7NFzjIAOHOlp5vexPOTmOFApDlOVSjD/cuoNb7UT4thNkgRUEL+aXXtzFWEArzTuqSWzZFZRA5KCocWJ7oruej6Ol1RMJNUv/VUTl2RusWeRmg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411807; c=relaxed/simple; bh=w4fBCLy6HOcz8loU9QSBEAJ429e74kKShsfkt6Bbzjk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aQTsvAkzRiniyJXu562snhATi9PDu8YvtWod1EdOrEYkR7zLg0pJ+JvoY/BZ7lznSjwChYQ5xAfPb6yGQWo7vKfzQ8WsgAK8LOPpnj7iy3XM4ZctlByqgHE3vC5If9VZkpXyIAeUYM1lP70ZsfSiv7PNMOd4opQ5XHNvDgc4aWs= 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=crFOuTt0; arc=fail smtp.client-ip=52.101.56.4 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="crFOuTt0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=CuZgwaCCQ0dHJRcxkE44E7LhMoOIozU1XWlVU8I05xrdmWCgTGtIciWInCRjaD00jvt/eUA1mIleF1dDtF/v+tRzsJX7F4svXN+FxrdXvU0kqS9D/Ss6KJwf/rLHnQwE/T4RZw0+goS+HiZeWCHqJjNZSFz8V8wJA9wLh4GW7knfLD0BIomidh8MspT6i6oknrQTGG8qAxGI5RdYN6eVsBX+gZe1x4+bddhInE0JuEm+HzUBXOEZmmqtAJAz5ZDtefpg7N+OAchyb0XZUN5kpnrExwcH/+qcG4acJaLaA5xrCM8b8/hxTiSqx9fJHfurXXxqmHGcMCQ1O+6+ycrZFQ== 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=XhmGqXLZsHfFLPv95rX0UuYHIFEOZ9jT1BBuCw33Ffs=; b=kUAwOtsCTEiDUU9/jiIsw3V97CcFg1Ver33efHrn4E/3nQysRh/iymnm7NMGgjPr+rv25BJgKR9AJb85NJsT1fLBDkyq7R4p1tcGXK6dLP2/iJ+7Rgxh5DwPqX1C0SZ/hv1PyxXwIgchgGQ7KwA8xiysPXffs50ln6zFCWFfBvrJ3NnYj6+qhcbyi4SjvuCai4ZXn0+U6+cudWl+FN0PmfW5ojeod6YtpH0Oa3y4WKpQA+cOIVKz4RJU/UCurCqaGg8P7P0dXllXnIsuCk0nyyu/745UkBb3NXwXeP8HDb9h0Osr+HfUlQ8GZbMId7o1msdcpHjnR7NAR3vbqA06lQ== 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=XhmGqXLZsHfFLPv95rX0UuYHIFEOZ9jT1BBuCw33Ffs=; b=crFOuTt0X1IaGSzt6mA1mCdp2VyX0ulkJJu38MjhmJ7pZQ9HNjZHKwzAsth1eMupkZxwFzbjqKZHg5cTso84UJxDU/nh6EQLzVTlPYzj6mItM/8GsXy+S2UnaPjWTfEXBKBPDQ6y85z14PqOE7JvZY8wf1pzz9qrWsrDth0CqkA= Received: from CH0P220CA0003.NAMP220.PROD.OUTLOOK.COM (2603:10b6:610:ef::31) by SJ0PR12MB5633.namprd12.prod.outlook.com (2603:10b6:a03:428::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.7; Mon, 14 Sep 2026 18:49:54 +0000 Received: from BL02EPF00021F6A.namprd02.prod.outlook.com (2603:10b6:610:ef:cafe::12) by CH0P220CA0003.outlook.office365.com (2603:10b6:610:ef::31) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:53 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6A.mail.protection.outlook.com (10.167.249.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:53 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:47 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 16/24] iommu/amd: Add IOMMUFD vDevice and DevID mapping Date: Mon, 14 Sep 2026 18:47:42 +0000 Message-ID: <20260914184750.222939-17-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6A:EE_|SJ0PR12MB5633:EE_ X-MS-Office365-Filtering-Correlation-Id: a04ad342-da87-4b93-4b24-08df1290f6f8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|7416014|376014|1800799024|23010399003|82310400026|3023799007|56012099006|11063799006|10067099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: sMnLQKxuYbHj96671mLRWdeGEWsGOovfF/4j8y3VUeycHYygQVI2jAG2/mjLaj8EPe9DwSgXMYuq/00NOmhdqmG+xt1OwCRDqbSdfPz7lPd2MV3gzs/V5DACw3Br7jA8XmwCtRkJzGafUxQNhY0dBopo8KlzdUC9uYCjJ4uAKiVEL8VEJQ03MBiqyZT3iZpoy1VKz6ZsySnAxKhcpUK+A8Y40hD1FqRCdbB/slP97pFqIJeYlcxdI0kEtsHMHvqAh149uXUqJcaV2SYCoxX2I9vsf1BGaRsuckNfXIc0zXbOm709mNi+t6JnN0IKuMD2CM3LyaMxwaHMzZlMpWmPSuh9E6+qyZ3eTFFUjF5k6WrgInt7C0MXLOK3bYRK5wKOVHBnosi4xv7zxCF0pmVuVkR7M0T1Z/QgB56pollx/EQuiPCeSCrRE0KBpU1NSwNM2A2Fo9tn2FMCWCm1asAeqeLWA7qdbdnZKUdfc+kN2c2iBtimicJc6KFzw+8Ppzu2f38DLWJei1LRtCjwWJN3FqrtolsMOPcWWSkHKQq+Nv7ugwmqu2fFKpqjxuQUP51O8DZPGhUI5wcffNrSgTXErbxyiPTbOUy99w4ZRbvzsM7/rLMostdYqLpC2KGdvHkflXFX1mPYNkobScpUCzlBzM5HrPhmYG5yi67EwEjSKVzkfPuc0PRENGkE9fENAat+VlmuGz7r1wsZskKj49yBig== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(7416014)(376014)(1800799024)(23010399003)(82310400026)(3023799007)(56012099006)(11063799006)(10067099003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vgK1zmpvvXc5ePaejJxaRpV8Vv0IuUst/BeJUrqfe5Oth2qQva9xMw1LqraX5akNMLQyTeqkRyVX3viGh6spDYNXauvpTY8Kx8vejsM024KNKfsWq/5RLyYnTEk2YzFHvDoJ/3rXAKOizTmAPwyQajJ6zOonzVqnM5SzOK0r1Cdc83TyAOXiQ3kpDDbfDuCJfkBXiBxB2ibcVh4vE0Ja/4PtKGxV1BP/hQ8eTjzqTufX0hpLd0ex3s7njkbe+YIMy4DZANsY7yMI5f5SP369IFOuBWS5Spn0jOkb9ADwkEz1Cv0cR5zBulJm6Rt1HpmAJThivCXIjnA7fPC1IuIWXJunGf3AbRY+cwVKidpO47yFyd6jnfSncaDbpBcBw+1Z4grioZX+tfEXa12Fg/bjNE/0FfKwRVbql5YXtoJotQNWuohJBteY0YQvGndlG69t X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:53.7074 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a04ad342-da87-4b93-4b24-08df1290f6f8 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: BL02EPF00021F6A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5633 Content-Type: text/plain; charset="utf-8" IOMMUFD vDevice allocation supplies the guest device ID (virt_id). The AMD vIOMMU Device ID mapping table translates that gdev_id to the host device ID for virtualised IOMMU commands. Add vdevice_init, which validates a PCI device and virt_id, then programs the mapping through VFCTRL via amd_viommu_set_device_mapping(). Get the host struct device via iommufd_vdevice_to_device(). Set vdev->destroy to restore an idle mapping when the vDevice is removed while the vIOMMU is still alive. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 12 +++++ drivers/iommu/amd/amd_viommu.h | 10 ++++ drivers/iommu/amd/iommufd.c | 52 ++++++++++++++++++++ drivers/iommu/amd/viommu.c | 73 ++++++++++++++++++++++++++++- 4 files changed, 146 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index fc8e1378c7ed..07eaeeb72e38 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -547,6 +547,14 @@ struct amd_iommu_viommu { u64 *devid_table; u64 *domid_table; =20 + /* + * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells + * (DevID and DomID table updates) for this gid. Those registers + * are shared by every vDevice on this vIOMMU; igroup locks are + * not. Do not nest with trans_devid_lock. + */ + struct mutex vfctrl_lock; + /* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mma= p(). */ unsigned long vfmmio_mmap_offset; }; @@ -1113,6 +1121,10 @@ struct amd_irte_ops { void (*clear_allocated)(struct irq_remap_table *, int); }; =20 +struct amd_iommu_vdevice { + struct iommufd_vdevice core; +}; + #ifdef CONFIG_IRQ_REMAP extern struct amd_irte_ops irte_32_ops; extern struct amd_irte_ops irte_128_ops; diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index 580cad0f9f5a..1022ae20c170 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -8,6 +8,9 @@ =20 struct amd_iommu; =20 +/* 16-bit guest DevID index; not VIOMMU_MAX_GID. */ +#define VIOMMU_MAX_GDEVID 0xFFFF + #if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD) =20 int amd_viommu_init(struct amd_iommu *iommu); @@ -20,6 +23,8 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct a= md_iommu_viommu *viommu =20 void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *viommu); =20 +void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu, + u16 hdev_id, u16 gdev_id); #else =20 /* @@ -41,6 +46,11 @@ static inline u64 amd_viommu_get_vfmmio_addr(struct amd_= iommu *iommu, u16 gid) return 0; } =20 +static inline void amd_viommu_set_device_mapping(struct amd_iommu_viommu *= aviommu, + u16 hdev_id, u16 gdev_id) +{ +} + #endif /* CONFIG_AMD_IOMMU_IOMMUFD */ =20 #endif /* AMD_VIOMMU_H */ diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index 97de07d3ad10..5760b183ce62 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -70,6 +70,7 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu= , struct iommu_domain * if (ret) return ret; =20 + mutex_init(&aviommu->vfctrl_lock); xa_init_flags(&aviommu->gdomid_array, XA_FLAGS_ALLOC1); aviommu->parent =3D pdom; =20 @@ -121,6 +122,7 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viom= mu, struct iommu_domain * err_mmap: amd_iommu_gid_free(iommu, aviommu->gid); err_gid: + mutex_destroy(&aviommu->vfctrl_lock); return ret; } =20 @@ -140,6 +142,54 @@ static void amd_iommufd_viommu_destroy(struct iommufd_= viommu *viommu) iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); amd_viommu_uninit_one(iommu, aviommu); amd_iommu_gid_free(iommu, aviommu->gid); + mutex_destroy(&aviommu->vfctrl_lock); +} + +/* + * Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_abort() + * + * Restore an idle DevID map entry for this virt_id. Every DevID mapping + * table slot must have V=3D1 for both active and non-active devices. A + * non-valid entry raises INVALID_DEVICE_REQUEST when the IOMMU processes + * INVALIDATE_IOMMU_ALL. Use host device ID 0: that RID is the IOMMU PCI + * function and does not normally host an endpoint. + */ +static void _amd_viommu_vdevice_destroy(struct iommufd_vdevice *vdev) +{ + struct iommufd_viommu *viommu =3D vdev->viommu; + struct amd_iommu_viommu *aviommu =3D + container_of(viommu, struct amd_iommu_viommu, core); + + amd_viommu_set_device_mapping(aviommu, 0, (u16)vdev->virt_id); +} + +/* + * Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl= () + */ +static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev) +{ + struct device *dev =3D iommufd_vdevice_to_device(vdev); + struct pci_dev *pdev; + struct iommufd_viommu *viommu =3D vdev->viommu; + struct amd_iommu_viommu *aviommu =3D container_of(viommu, struct amd_iomm= u_viommu, core); + + if (!dev_is_pci(dev)) + return -EINVAL; + pdev =3D to_pci_dev(dev); + + if (vdev->virt_id > VIOMMU_MAX_GDEVID) { + pr_err("%s: Invalid virtual ID (virt_id=3D%#llx)\n", __func__, vdev->vir= t_id); + return -EINVAL; + } + + pr_debug("%s: gid=3D%#x, hdev_id=3D%#x, gdev_id=3D%#llx\n", __func__, + aviommu->gid, pci_dev_id(pdev), vdev->virt_id); + + amd_viommu_set_device_mapping(aviommu, pci_dev_id(pdev), + (u16)vdev->virt_id); + vdev->destroy =3D _amd_viommu_vdevice_destroy; + + return 0; } =20 /* @@ -149,4 +199,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_v= iommu *viommu) static const struct iommufd_viommu_ops amd_viommu_ops =3D { .alloc_domain_nested =3D amd_iommu_alloc_domain_nested, .destroy =3D amd_iommufd_viommu_destroy, + .vdevice_size =3D VDEVICE_STRUCT_SIZE(struct amd_iommu_vdevice, core), + .vdevice_init =3D _amd_viommu_vdevice_init, }; diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 06a21f14b2a5..556864eaaa0d 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -6,6 +6,7 @@ #define pr_fmt(fmt) "AMD-Vi: " fmt #define dev_fmt(fmt) pr_fmt(fmt) =20 +#include #include #include #include @@ -29,7 +30,6 @@ * VIOMMU_MAX_GDEVID / VIOMMU_MAX_GDOMID are 16-bit guest DevID * and DomID table indices (hardware mapping-table width), not GID. */ -#define VIOMMU_MAX_GDEVID 0xFFFF #define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL #define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20) =20 @@ -45,6 +45,8 @@ static_assert(VIOMMU_DEVID_MAPPING_ENTRY_SIZE >=3D static_assert(VIOMMU_DOMID_MAPPING_ENTRY_SIZE >=3D (VIOMMU_MAX_GDOMID + 1ULL) * sizeof(u64)); =20 +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET 0x00 + static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) { if (iommu->vfctrl_base) { @@ -439,6 +441,75 @@ static void __maybe_unused free_private_vm_region(stru= ct amd_iommu *iommu, u64 * *entry =3D NULL; } =20 +#define DEVID_ENTRY_GDEVID_MASK GENMASK_ULL(61, 46) +#define DEVID_ENTRY_HDEVID_MASK GENMASK_ULL(29, 14) +#define DEVID_ENTRY_WRITE BIT_ULL(63) +#define DEVID_ENTRY_VALID BIT_ULL(0) + +/* + * CONTROL0/CONTROL1 bit 63 (WRITE) is a doorbell: hardware copies one + * entry into the IPA-backed table and clears WRITE when that DMA write + * retires. This is not a command-buffer operation; + * iommu_completion_wait() does not drain it. A readq() of CONTROL_EN + * is also unrelated. Poll WRITE=3D0 before ringing a new doorbell and + * after each write so the table update is retired before the caller + * proceeds (DTE commit, unmap/free). + */ +static void vfctrl_wait_write_clear(struct amd_iommu *iommu, u16 gid, + unsigned int offset) +{ + u8 __iomem *reg =3D VIOMMU_VFCTRL_MMIO_BASE(iommu, gid) + offset; + int i; + + for (i =3D 0; i < LOOP_TIMEOUT; i++) { + if (!(readq(reg) & DEVID_ENTRY_WRITE)) + return; + udelay(1); + } + pr_err("%s: WRITE timeout gid=3D%#x offset=3D%#x val=3D%#llx\n", + __func__, gid, offset, readq(reg)); +} + +/* + * Guest DevID table update via VFCTRL CONTROL0. + */ +static void set_device_mapping_unlocked(struct amd_iommu *iommu, u16 gid, + u16 hdev_id, u16 gdev_id) +{ + u64 val; + u8 __iomem *vfctrl =3D VIOMMU_VFCTRL_MMIO_BASE(iommu, gid); + + val =3D FIELD_PREP(DEVID_ENTRY_GDEVID_MASK, gdev_id) | + FIELD_PREP(DEVID_ENTRY_HDEVID_MASK, hdev_id) | + DEVID_ENTRY_WRITE | DEVID_ENTRY_VALID; + + vfctrl_wait_write_clear(iommu, gid, + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET); + writeq(val, vfctrl + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET); + vfctrl_wait_write_clear(iommu, gid, + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET); +} + +/* + * Program the DevID via VFCTRL CONTROL0. Waits for WRITE to clear so + * the IPA table DMA has retired before return. Serialized by + * aviommu->vfctrl_lock: CONTROL0 is shared by all vDevices on this gid. + */ +void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu, + u16 hdev_id, u16 gdev_id) +{ + struct amd_iommu *iommu =3D + container_of(aviommu->core.iommu_dev, struct amd_iommu, iommu); + + pr_debug("%s: iommu_devid=3D%#x, gid=3D%#x, hdev_id=3D%#x, gdev_id=3D%#x\= n", + __func__, pci_dev_id(iommu->dev), aviommu->gid, hdev_id, + gdev_id); + + mutex_lock(&aviommu->vfctrl_lock); + set_device_mapping_unlocked(iommu, aviommu->gid, hdev_id, gdev_id); + mutex_unlock(&aviommu->vfctrl_lock); +} + void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *aviommu) { pr_debug("%s: gid=3D%u\n", __func__, aviommu->gid); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012014.outbound.protection.outlook.com [40.93.195.14]) (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 9410738F65D for ; Mon, 14 Sep 2026 18:50:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.195.14 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411813; cv=fail; b=MNWjfDf5D+tispVxeANbzy7bSHcmd0mEfPDLQz+jWJ9iu5gPUxIB4nnTGLODeQIf0mtXsi/WFzEI0+su4q3sVNXF+U91TIDAuYKvY6WbemjBqmjnQ39D2N2WhxgOD27qg5z941+THTsUnsh/zOeQL6rvq39MDiUA2yAgYc+PQuk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411813; c=relaxed/simple; bh=II4bKltEMb32qaz9KbRyO8/kB02mY9iXsN7MX1IJGkI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QIjAaNZ8p/ESeP/7Dtxu3IBRyrZ0uVSBpVma1ep8Zc+W+n21ImVP3JQi1Yph3ZnuRl6SkDXjQDhxc0UD9YNlE25U8snKiN4TqYZh6BNtIEUIZsbhCKUYRxkVN/+Z7vd0nZj71Bpa19IuuovDGrHlLxTBe1yayQp0hW+43Gn4wy0= 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=E5AsFeEe; arc=fail smtp.client-ip=40.93.195.14 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="E5AsFeEe" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WNbnhsewI+7GYHZw/wJXRLBSQVB5mKJ1G/+K3Or0PzEbWX7xuzHlVwGwZ6VPA0nmioMmNCGMRg75u1SUGjmE34Esf+07FSAvw/sW9SAdB++AOJ/imY4uMa8bRvlIOK9TmdYMujob1BbAmFKRH0Upsoh5LAJ1QQAO9S/Hke9ITBsj7a0jKqbxRvUeqaery23B3H4EqTj/ZkT0Mlp89uDS7xNxGEbWhpSrzRH/1TpylR5U+thOgf6cYfbWC1nZaAkLkRJrelbOpXLIbqFmosQ89NzxQoOhH5BQIqCNtF+dOHo9+g5ILALctQF6Em4cYZSB5DicuqFbbVIrSBqGwRcqug== 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=UauVk+6qrlAUumszF9+WTgRGjWQ3MmIgMmKUF2sWUBI=; b=dIr9CkTtVOAtyhY+wl5ywVtyEeOYABFiEMyi5FEn+ybHjCsyJup7H/wuGgh7SdJBKSuCW8RFPo2mYA+d35Z4VVwmYgf0jsFKHc1pR96IGLyF+CFE7M3JqNWBQHiU60vTXh70ta2Lid+DdSkTrNnM3NFlHZ3gpZTvlQTdkWUTkJexJd9n6apWlostbz4xqETEV/u3QPLANYynwAT1rxAvBRBxuKoB1xD7aPU2aitNItwtCUs13TRvLAGUqGEVIdgjzbhQRIypaRIAX6A0iXwGDsHFlt+OwaIgK8cPi3ylhOJEHN1MQr/y8VEUBCGcrYq/Mx2hqqrKA5BC2IfX83MiWw== 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=UauVk+6qrlAUumszF9+WTgRGjWQ3MmIgMmKUF2sWUBI=; b=E5AsFeEexKX5WoRngRiUpmmPpI+jxj0eI/7wkuM1d0fhgP5Rz2Mwn45f+yD3bDsdVYP7MzDtt8P3ezSEv/lTkGR00ubaVcIyx86ZlnEIQvebhDUlzRiEIqQJDlXuRpwtA0znEA50IHbyg6FOWcEaMCdGkgnB2x/kYUclHMUiR2k= Received: from BLAPR03CA0146.namprd03.prod.outlook.com (2603:10b6:208:32e::31) by SN7PR12MB7346.namprd12.prod.outlook.com (2603:10b6:806:299::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:03 +0000 Received: from BL02EPF00021F69.namprd02.prod.outlook.com (2603:10b6:208:32e:cafe::13) by BLAPR03CA0146.outlook.office365.com (2603:10b6:208:32e::31) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:49:59 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F69.mail.protection.outlook.com (10.167.249.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:49:59 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:53 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 17/24] iommu/amd: Program nested DTE and DomID map on attach Date: Mon, 14 Sep 2026 18:47:43 +0000 Message-ID: <20260914184750.222939-18-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F69:EE_|SN7PR12MB7346:EE_ X-MS-Office365-Filtering-Correlation-Id: b5527549-3a0e-4902-af37-08df1290fa91 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|82310400026|7416014|376014|23010399003|1800799024|10067099003|22082099003|18002099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: jE91XyHh30kPwiIFFxNpX63icNBjFwCt74B1Qn43wcARyOcfM2OAkY4eLgRJr1VnK0vFFBW9FZImBCJW66dYEoRdVqBl8WeKU8IwcP8EWb5nHdmpfn92R0l+SZpWUIkolLzQKsE768uXPvecNtG+3uogL7U2nQOFFEp0iYkwOoPtfkPb0DOCDd5XjxiLsUgCK+taQ0odXilxYdhxQJkmF7Ki6N/Y+Ivfm4F3Bu1PzkAmCNA1QpFuDHiPraY/2D4FOOKwlr1hPb7hEEITbQa2MdOKSP3Suq+bzGyrZio+RPUQBCEgWsjEKWwiG8V+gcKytevly1mfxsmiufyjkOJXgWRI5bXlUOiZQafPYtg0WvmrCNR0o80UGrYvlxcJVwv4Qh39yeRUrlk4GtXclJ3Tujsj4hDGGoj5debhYnHIELu/z+gCi9ArcQ4MooneQsPaxwter+Ieopl5RsmfRwO3b+dK9qe4JyShyfM8FI2apwgrCE+cRHVkTseTyu4lgOXkpS1Uc1JGyEa8K/UmkRnKpYtC8gxruC29npJNAoa9nciAeQSyGUh4iHP5BUK10oe66kpbBqsYqo7OZjeBgx6JEelIa5NEUMnFx7U6M6W+QfMfFkPOQ4//qxW6CTgjH1IfdiKFb1/uL/ucrVUPehPcfFQzpsBAoPC4rofmmoZD6jPagF4If/qmFL6iBJ63SZgoM42AVF989I2zEQ8foD1R0Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(82310400026)(7416014)(376014)(23010399003)(1800799024)(10067099003)(22082099003)(18002099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xbGV0k2nRKKcHVNTdrnJdU0Y+TvpYQc1gqfnyqHOZy3pCdZSdfk2lLUPG73hwgGUfC33J225Q0cU4M2KKyEVLbvs+sCt7HBb4cE2C0GuWQWAnSUY3dvYxBBv2Xjmg69VJXOp5vYf2qmXgGKzoOdBbgnhZKHzBaRkfU1DUcG79XEoQrdv3GDkI1YQmlsVmUONpksf1J4hSuRMWXS/4fFmuU8L32yk/jC0PNbHG3aNrB0Y3NLJ+BGWwSRA+KbtUSCykUS4vB4o/L4CliJziPkFQR/6Rz8ME/zhbvhSAgFs4trld8CVjoYrlfpcLI7Flufd/naycrco9CMJz7l14l+BwFqVnkXIus1qkSYLSguBMMgW2AidjJRcbo04+HRAXYCgMiyX2t8AOpOKCyfkheR5u+5GOTviCvIevpUUUHu0n7AQ2HNAOjNe9PxgG+4DIgyk X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:49:59.7903 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b5527549-3a0e-4902-af37-08df1290fa91 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: BL02EPF00021F69.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7346 Content-Type: text/plain; charset="utf-8" On nested attach, the host DTE must enable vIOMMU and carry the guest device and guest IDs from the vDevice. Look up gdev_id with iommufd_viommu_get_vdev_id() and fail the attach if that lookup fails. Program the Domain ID mapping table through VFCTRL so hardware can translate gdom_id to hdom_id. Write CONTROL1 before the DTE is live with DTE_VIOMMU_EN so guest DMA cannot resolve against the idle nest-parent map. DomID CONTROL1 uses the same per-vIOMMU vfctrl_lock as DevID CONTROL0. Hold that lock across CONTROL1 and the DTE commit, and on last-ref nested_domain_free() take it before gdomid_array so idle restore cannot clobber a concurrent alloc+attach. CONTROL1 WRITE uses the same doorbell as CONTROL0. Poll WRITE until it clears; iommu_completion_wait() does not drain the table DMA. On the last nested_domain_free() for a gdom_id, restore the idle DomID map (nest parent, V=3D1) before returning hdom_id to the IDA. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 10 ++++ drivers/iommu/amd/amd_viommu.h | 15 ++++++ drivers/iommu/amd/nested.c | 74 +++++++++++++++++++++++++---- drivers/iommu/amd/viommu.c | 49 +++++++++++++++++++ 4 files changed, 140 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 07eaeeb72e38..b5d1b23791da 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -382,6 +382,11 @@ #define DTE_GPT_LEVEL_SHIFT 54 #define DTE_GPT_LEVEL_MASK GENMASK_ULL(55, 54) =20 +/* vIOMMU bit fields */ +#define DTE_VIOMMU_EN_SHIFT 15 +#define DTE_VIOMMU_GDEVICEID_MASK GENMASK_ULL(31, 16) +#define DTE_VIOMMU_GUESTID_MASK GENMASK_ULL(47, 32) + #define GCR3_VALID 0x01ULL =20 /* DTE[128:179] | DTE[184:191] */ @@ -552,6 +557,11 @@ struct amd_iommu_viommu { * (DevID and DomID table updates) for this gid. Those registers * are shared by every vDevice on this vIOMMU; igroup locks are * not. Do not nest with trans_devid_lock. + * + * nested_domain_free() last-ref takes this before + * gdomid_array's xa_lock so CONTROL1 idle restore cannot race + * a concurrent alloc+attach. Attach holds it across CONTROL1 + * and the DTE commit. */ struct mutex vfctrl_lock; =20 diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index 1022ae20c170..f17ac13da213 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -23,6 +23,11 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct = amd_iommu_viommu *viommu =20 void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *viommu); =20 +void amd_viommu_domain_id_update(struct amd_iommu_viommu *aviommu, + u16 hdom_id, u16 gdom_id); +void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu, + u16 hdom_id, u16 gdom_id); + void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu, u16 hdev_id, u16 gdev_id); #else @@ -51,6 +56,16 @@ static inline void amd_viommu_set_device_mapping(struct = amd_iommu_viommu *aviomm { } =20 +static inline void amd_viommu_domain_id_update(struct amd_iommu_viommu *av= iommu, + u16 hdom_id, u16 gdom_id) +{ +} + +static inline void amd_viommu_domain_id_update_locked(struct amd_iommu_vio= mmu *aviommu, + u16 hdom_id, u16 gdom_id) +{ +} + #endif /* CONFIG_AMD_IOMMU_IOMMUFD */ =20 #endif /* AMD_VIOMMU_H */ diff --git a/drivers/iommu/amd/nested.c b/drivers/iommu/amd/nested.c index 8bb0d1596c6c..ecdcecdc0bb7 100644 --- a/drivers/iommu/amd/nested.c +++ b/drivers/iommu/amd/nested.c @@ -10,6 +10,7 @@ #include =20 #include "amd_iommu.h" +#include "amd_viommu.h" =20 static const struct iommu_domain_ops nested_domain_ops; =20 @@ -188,13 +189,16 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *= viommu, u32 flags, return ERR_PTR(ret); } =20 -static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *d= om, - struct iommu_dev_data *dev_data, struct dev_table_entry *new) +static int set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *do= m, + struct iommu_dev_data *dev_data, struct dev_table_entry *new) { + int ret; + u16 gid; struct protection_domain *parent; struct nested_domain *ndom =3D to_ndomain(dom); struct iommu_hwpt_amd_guest *gdte =3D &ndom->gdte; struct pt_iommu_amdv1_hw_info pt_info; + unsigned long gdev_id; =20 /* * The nest parent domain is attached during the call to the @@ -202,9 +206,15 @@ static void set_dte_nested(struct amd_iommu *iommu, st= ruct iommu_domain *dom, * of the struct amd_iommu_viommu.parent. */ if (WARN_ON(!ndom->viommu || !ndom->viommu->parent)) - return; + return -EINVAL; =20 + gid =3D ndom->viommu->gid; parent =3D ndom->viommu->parent; + + ret =3D iommufd_viommu_get_vdev_id(&ndom->viommu->core, dev_data->dev, &g= dev_id); + if (ret) + return ret; + amd_iommu_make_clear_dte(iommu, dev_data->devid, new); =20 /* Retrieve the current pagetable info via the IOMMU PT API. */ @@ -232,12 +242,19 @@ static void set_dte_nested(struct amd_iommu *iommu, s= truct iommu_domain *dom, =20 /* Guest paging mode */ new->data[2] |=3D gdte->dte[2] & DTE_GPT_LEVEL_MASK; + + new->data[3] |=3D 1ULL << DTE_VIOMMU_EN_SHIFT; + new->data[3] |=3D FIELD_PREP(DTE_VIOMMU_GDEVICEID_MASK, gdev_id); + new->data[3] |=3D FIELD_PREP(DTE_VIOMMU_GUESTID_MASK, gid); + + return 0; } =20 static int nested_attach_device(struct iommu_domain *dom, struct device *d= ev, struct iommu_domain *old) { struct dev_table_entry new =3D {0}; + struct nested_domain *ndom =3D to_ndomain(dom); struct iommu_dev_data *dev_data =3D dev_iommu_priv_get(dev); struct amd_iommu *iommu =3D get_amd_iommu_from_dev_data(dev_data); int ret =3D 0; @@ -251,10 +268,23 @@ static int nested_attach_device(struct iommu_domain *= dom, struct device *dev, =20 mutex_lock(&dev_data->mutex); =20 - set_dte_nested(iommu, dom, dev_data, &new); + ret =3D set_dte_nested(iommu, dom, dev_data, &new); + if (ret) + goto out_err; =20 + /* + * Program gdom_id -> hdom_id before the DTE is live with + * DTE_VIOMMU_EN. Hold vfctrl_lock across both so a concurrent + * last-ref free cannot restore the idle map in between. + */ + mutex_lock(&ndom->viommu->vfctrl_lock); + amd_viommu_domain_id_update_locked(ndom->viommu, + ndom->gdom_info->hdom_id, + ndom->gdom_id); amd_iommu_update_dte(iommu, dev_data, &new); + mutex_unlock(&ndom->viommu->vfctrl_lock); =20 +out_err: mutex_unlock(&dev_data->mutex); =20 return ret; @@ -267,10 +297,18 @@ static void nested_domain_free(struct iommu_domain *d= om) struct nested_domain *ndom __free(kfree) =3D to_ndomain(dom); struct amd_iommu_viommu *aviommu =3D ndom->viommu; =20 + /* + * vfctrl_lock then gdomid_array: keep the slot empty in the + * xarray until CONTROL1 is idle so a concurrent alloc+attach + * for this gdom_id cannot publish H2 and then lose it to this + * restore. Attach takes the same lock around CONTROL1 + DTE. + */ + mutex_lock(&aviommu->vfctrl_lock); xa_lock_irqsave(&aviommu->gdomid_array, irqflags); =20 if (!refcount_dec_and_test(&ndom->gdom_info->users)) { xa_unlock_irqrestore(&aviommu->gdomid_array, irqflags); + mutex_unlock(&aviommu->vfctrl_lock); return; } =20 @@ -282,14 +320,34 @@ static void nested_domain_free(struct iommu_domain *d= om) ndom->gdom_info, NULL, GFP_ATOMIC); =20 xa_unlock_irqrestore(&aviommu->gdomid_array, irqflags); - if (WARN_ON(!curr || xa_err(curr))) + if (WARN_ON(!curr || xa_err(curr))) { + mutex_unlock(&aviommu->vfctrl_lock); return; + } =20 - /* success */ pr_debug("%s: Free gdom_id=3D%#x, hdom_id=3D%#x\n", - __func__, ndom->gdom_id, curr->hdom_id); + __func__, ndom->gdom_id, curr->hdom_id); + + /* + * Restore the idle DomID map before releasing hdom_id. Every + * slot stays V=3D1; unknown mapping raises an event. Idle is the + * nest parent CONTROL1 map, same as vIOMMU init prefill. + * amd_iommu_reset_vmmio() only resets guest MMIO. + * + * Guest INVALIDATE_IOMMU_ALL walks the HW table, not + * gdomid_array. The pdom_ids IDA is global, so a stale V=3D1 + * entry can invalidate a recycled hdom_id that now belongs to + * another domain. + * + * Detach does not clear this slot: hdom_id stays allocated + * until this last free, so a lingering gdom_id -> hdom_id + * map cannot point at a recycled ID. + */ + amd_viommu_domain_id_update_locked(aviommu, aviommu->parent->id, + ndom->gdom_id); + mutex_unlock(&aviommu->vfctrl_lock); =20 - amd_iommu_pdom_id_free(ndom->gdom_info->hdom_id); + amd_iommu_pdom_id_free(curr->hdom_id); kfree(curr); } =20 diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 556864eaaa0d..7521f470e5d4 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -46,6 +46,7 @@ static_assert(VIOMMU_DOMID_MAPPING_ENTRY_SIZE >=3D (VIOMMU_MAX_GDOMID + 1ULL) * sizeof(u64)); =20 #define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET 0x00 +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET 0x08 =20 static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) { @@ -510,6 +511,54 @@ void amd_viommu_set_device_mapping(struct amd_iommu_vi= ommu *aviommu, mutex_unlock(&aviommu->vfctrl_lock); } =20 +#define DOMID_ENTRY_GDOMID_MASK GENMASK_ULL(61, 46) +#define DOMID_ENTRY_HDOMID_MASK GENMASK_ULL(29, 14) +#define DOMID_ENTRY_VALID BIT_ULL(0) +#define DOMID_ENTRY_WRITE BIT_ULL(63) + +/* + * Guest DomID table update via VFCTRL CONTROL1. Same WRITE-bit + * doorbell as CONTROL0. + */ +static void domain_id_update_unlocked(struct amd_iommu *iommu, u16 gid, + u16 hdom_id, u16 gdom_id) +{ + u64 val; + u8 __iomem *vfctrl =3D VIOMMU_VFCTRL_MMIO_BASE(iommu, gid); + + val =3D FIELD_PREP(DOMID_ENTRY_GDOMID_MASK, gdom_id) | + FIELD_PREP(DOMID_ENTRY_HDOMID_MASK, hdom_id) | + DOMID_ENTRY_WRITE | DOMID_ENTRY_VALID; + + vfctrl_wait_write_clear(iommu, gid, + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET); + writeq(val, vfctrl + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET); + vfctrl_wait_write_clear(iommu, gid, + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET); +} + +/* + * Program a gdom_id -> hdom_id entry through VFCTRL CONTROL1. + * Caller must hold aviommu->vfctrl_lock. + */ +void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu, + u16 hdom_id, u16 gdom_id) +{ + struct amd_iommu *iommu =3D + container_of(aviommu->core.iommu_dev, struct amd_iommu, iommu); + + lockdep_assert_held(&aviommu->vfctrl_lock); + domain_id_update_unlocked(iommu, aviommu->gid, hdom_id, gdom_id); +} + +void amd_viommu_domain_id_update(struct amd_iommu_viommu *aviommu, + u16 hdom_id, u16 gdom_id) +{ + mutex_lock(&aviommu->vfctrl_lock); + amd_viommu_domain_id_update_locked(aviommu, hdom_id, gdom_id); + mutex_unlock(&aviommu->vfctrl_lock); +} + void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *aviommu) { pr_debug("%s: gid=3D%u\n", __func__, aviommu->gid); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012039.outbound.protection.outlook.com [52.101.48.39]) (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 CA23F39CD00 for ; Mon, 14 Sep 2026 18:50:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.48.39 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411816; cv=fail; b=Vl63u6jFf5/3uFJGQaoWVxhzXIPJCcIf6UsgZTZ57a4/f4zyQhoSW0V8yrylCP/sDo1GJNizHDYwhW/zXueCBg3FJft2zdsH0utB2lijXupq2wisQblyizF+ZdTtG0f+2LN+JIZe6DaW3YXoluA5SNgUnAzHP/hlbtyb1z/1BnU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411816; c=relaxed/simple; bh=fGiuAUps2tVO78oex3pj+mOkOaC8E9MUlmvuWvtlYos=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=V0851+ILyUzolVOulApWl3H0kjwRz13zsPV0hldXMHHEqdZyJRr8dyTbD3BghAPr8j78ZvDmIVYLz5QyZBIiP2wlzMfdnXYuYNWqY3X11HkMaZ3FyhAJibz7/6EWlFyASUV6eeyNgMNL6Fjm2gJ/qApcaytFtkkI2dQJKFbD8Xs= 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=ppmkx2hm; arc=fail smtp.client-ip=52.101.48.39 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="ppmkx2hm" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=y13lay4Ux4EKN9P8OCe1jHi5nqUQWNIykqI04inGIldsPKr/gst3e9VSwHXl/TDYsW6/XfHYlQmUUfdtNatA3UUtstuIDsaOjS6fylGygJTnwAqiyW7lxcqPxfnnHCSipAXBvvxIUh46+IxJineOpmepHuc/nXVpztkFkUuhBRAMCQKvfXzmeYi92vSZdIJWCxG6QJZjayk1/WOXCZFO8bBX7/QcitjYCKb14dleOngmhxsgLln3ofFlHJYXDc3kD1yfrBqeFGOslLfpqIu2xaNItpyeavcKswJzpTJW698dOjvFPod9yeTQnSo25aYy41SkeDGnnNoHeU5GpDC5eQ== 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=EuqZAJnpsK7cWfCza6+3peaCq2hQp7BovRW9eG6v9u0=; b=KZemtPjIMhvT/Xid0TV1TwJYuYoy8OSqvtuqgHPzP0Ql1sCbsKn6im1dgYnu1+PRJvnDg6ZKzzKzsSbsdpOW/dNK/P1X+d3lajfNcKCab97gpca5uOxz7Sbi1X68FNJW/NZI1YVJotMhFYZoYETFwMHNn9xyURP4z7JS/u2JAwRsHzGX64hgy5JMoXDyzVJ5yET50E0U/IqcASfPYwt8u2O/WAcrOO3IVNMtrnmeioe3Vp71iJXA78qdCTwAzkukZ9sPrgM6okQ4tsa3eh5I8ThFLKMNucvGUztnaQmdz01gl5GII6y6ZFj8cOocLkKQttihHZzOoUBt+TWs1XkrrA== 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=EuqZAJnpsK7cWfCza6+3peaCq2hQp7BovRW9eG6v9u0=; b=ppmkx2hmT1mARRiXg3yLF2hzNSXDYit868YskizHaempNcbAhT5gNnbvJUNRt2fJvxOWXd7ysmrSEOAStIrewBlcJhB5QrBX1nDjpDP7c8l83riwWxhvHEoM7UEYZ93fo/IM7vgvFPfNGQIOLlIUbDsK3wVd18XHZ3VZMvDvPwE= Received: from BN9PR03CA0950.namprd03.prod.outlook.com (2603:10b6:408:108::25) by CY1PR12MB9601.namprd12.prod.outlook.com (2603:10b6:930:107::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:06 +0000 Received: from BL02EPF00021F6E.namprd02.prod.outlook.com (2603:10b6:408:108:cafe::1a) by BN9PR03CA0950.outlook.office365.com (2603:10b6:408:108::25) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:50:06 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6E.mail.protection.outlook.com (10.167.249.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:06 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:49:59 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 18/24] iommu/amd: Init and clear vIOMMU DevID and DomID maps Date: Mon, 14 Sep 2026 18:47:44 +0000 Message-ID: <20260914184750.222939-19-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6E:EE_|CY1PR12MB9601:EE_ X-MS-Office365-Filtering-Correlation-Id: be77823a-aa17-42d1-dcf2-08df1290fe5f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|82310400026|7416014|36860700016|23010399003|6133799003|18002099003|22082099003|11063799006|10067099003|56012099006; X-Microsoft-Antispam-Message-Info: QksHxNL7DYGc9kHIrR73f6ukBPPD+sNwiFP3r6HSOjjYhwq1WLTZTHqO0/uDSBG2o2OC664FAb8IH5Pn6VBEDWTAsItjTOPa3lTH9Q3rWjQWCPN4orGaNar72nhvf0SoCAZSfdkfy5qRo2x+520AVGTNO0CFnbLPSJlgvcDJTcp8TNRvS1uSgiOmxLEyu+icGKKKM4Ds0zveNAljMSPYjo6aQmccrS2/avmYZKVVndtkEQt9DpAdByl9HFqDkjb6e5aiTChoA29lP7x1zbAszj/Q2S3CVeYycufjmEtAeOd0qk+1le7X0M2f4xZGmWk1PnJKRqV/9jLRAWQZtKsXfK4B2jqx0HjrfJ1EWl01EA5gwjamsdNSc3W7zSCF2ztycN9PaR10nXbBBq2qaQqqBtquUq/bvGkvtpSj5UNWu+RYUWSfFQYyqiX0oafrp5Qei2jhoNJ8zblL9uKihPdgkHQSxBCLd3zZAwTd1jg+B+O2LzcTijnX/Kq5xWu7S20loGi1TmRhauQStwPg+Ozllrg6FdEH7XYV7M1bSmqp9Yf6+d/XvPen0KdK31BmwtLPR+LPT2rPKv/0pXe0fTcXCl9CQiGigk+svRt5+JdF1jjIQZZr3cJtL26hbB5mcyV3xe2ciStcktgcc80PeEwYIrk5OXP0b8DzjHUA3AaWskGsN1s5N7ND4LSZ4YhnIOyBMZMkJxb8fp8qhQXhI8N5TA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(82310400026)(7416014)(36860700016)(23010399003)(6133799003)(18002099003)(22082099003)(11063799006)(10067099003)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mZWzl3J4+1Z7Wbg5AA/FNn9btS4PR8JxLzADwIjn+hWy1E8v4X35ahsYzKcn5k+hd6r36prnpCBddJQAYxftwFWHy8CTkiKJeSEvSy2PvF2wNlUTFMnxLOcJ+llKZIdszZSPTRFmVBs8tLeeUfufqrWxn089DSpy3J/We8XgVfmpWY7leUfZLfOKBjZoL7b4JTiGX/kJJLqaEzrDX2zhi8w8+O7C1r6WgqhFP+Ujn5nQguyzP4NY0qQ9iMNgrCT2T7CkQhGI9wMVh82gVNCAOjRD2O2glfgDeqH+MN+j9fXDkAS2oKZ1P8ISBAtg7atnammxX8YVv6rEX5brv8Oj0biOUlx0ExcgVlr5Re1l84iT/NdCSfxC7CQR8pC50wAD0RUOAjT0HuHbs4tCMKKAVOtddx47+UV0lMikmD4FTEQe0YnEystAjzurqfSJ9cy4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:06.1756 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: be77823a-aa17-42d1-dcf2-08df1290fe5f 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: BL02EPF00021F6E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR12MB9601 Content-Type: text/plain; charset="utf-8" Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table. Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one(). On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries. Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/viommu.c | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c index 7521f470e5d4..e0c5e79cf795 100644 --- a/drivers/iommu/amd/viommu.c +++ b/drivers/iommu/amd/viommu.c @@ -7,6 +7,7 @@ #define dev_fmt(fmt) pr_fmt(fmt) =20 #include +#include #include #include #include @@ -447,6 +448,8 @@ static void __maybe_unused free_private_vm_region(struc= t amd_iommu *iommu, u64 * #define DEVID_ENTRY_WRITE BIT_ULL(63) #define DEVID_ENTRY_VALID BIT_ULL(0) =20 +#define VIOMMU_VFCTRL_PREFILL_RESCHED 256 + /* * CONTROL0/CONTROL1 bit 63 (WRITE) is a doorbell: hardware copies one * entry into the IPA-backed table and clears WRITE when that DMA write @@ -559,10 +562,95 @@ void amd_viommu_domain_id_update(struct amd_iommu_vio= mmu *aviommu, mutex_unlock(&aviommu->vfctrl_lock); } =20 +static bool viommu_vfctrl_ready(struct amd_iommu *iommu) +{ + return iommu->vfctrl_base && viommu_cmdbuf_enabled(iommu); +} + +/* + * Drain outstanding CONTROL0/CONTROL1 table DMA before the IPA pages + * are unmapped and freed. Do not rewrite 0..0xFFFF: vDevice destroy + * and nested_domain_free already restored idle entries, and a full + * walk would start 128K new DMA writes into pages about to be freed. + * + * Skip after disable_iommus(): CMDBUF_EN is already clear, matching + * viommu_priv_unmap_free() which skips IOTLB invalidation. + */ +static void viommu_drain_mapping(struct amd_iommu *iommu, + struct amd_iommu_viommu *aviommu) +{ + u16 gid =3D aviommu->gid; + + if (!viommu_vfctrl_ready(iommu)) + return; + + mutex_lock(&aviommu->vfctrl_lock); + vfctrl_wait_write_clear(iommu, gid, + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET); + vfctrl_wait_write_clear(iommu, gid, + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET); + mutex_unlock(&aviommu->vfctrl_lock); +} + +static void viommu_prefill_resched(struct amd_iommu_viommu *aviommu, int i) +{ + lockdep_assert_held(&aviommu->vfctrl_lock); + + if ((i & (VIOMMU_VFCTRL_PREFILL_RESCHED - 1)) !=3D + VIOMMU_VFCTRL_PREFILL_RESCHED - 1) + return; + + mutex_unlock(&aviommu->vfctrl_lock); + cond_resched(); + mutex_lock(&aviommu->vfctrl_lock); +} + +/* + * Prefill every guest ID so hardware never sees an unknown mapping. + * Map all gdom_id to the nest parent domain ID, and all gdev_id with + * hdev_id 0. Cannot be scoped to IDs in use: none exist yet, and + * INVALIDATE_IOMMU_ALL walks the HW table. + * + * Drop vfctrl_lock every VIOMMU_VFCTRL_PREFILL_RESCHED doorbells so + * the 128K MMIO loop can yield. Safe on init: vDevice/nested attach + * cannot run until viommu->ops is published after init_one(). + */ +static void viommu_prefill_mapping(struct amd_iommu *iommu, + struct amd_iommu_viommu *aviommu) +{ + int i; + u16 gid =3D aviommu->gid; + + if (!viommu_vfctrl_ready(iommu)) + return; + + mutex_lock(&aviommu->vfctrl_lock); + if (aviommu->domid_table) { + for (i =3D 0; i <=3D VIOMMU_MAX_GDOMID; i++) { + domain_id_update_unlocked(iommu, gid, + aviommu->parent->id, i); + viommu_prefill_resched(aviommu, i); + } + } + + if (aviommu->devid_table) { + for (i =3D 0; i <=3D VIOMMU_MAX_GDEVID; i++) { + set_device_mapping_unlocked(iommu, gid, 0, i); + viommu_prefill_resched(aviommu, i); + } + } + mutex_unlock(&aviommu->vfctrl_lock); +} + void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viomm= u *aviommu) { pr_debug("%s: gid=3D%u\n", __func__, aviommu->gid); =20 + /* + * Wait until CONTROL0/CONTROL1 table DMA has retired before + * the backing pages are unmapped and freed. + */ + viommu_drain_mapping(iommu, aviommu); free_private_vm_region(iommu, &aviommu->devid_table, VIOMMU_DEVID_MAPPING_BASE, VIOMMU_DEVID_MAPPING_ENTRY_SIZE, @@ -591,6 +679,8 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct= amd_iommu_viommu *viommu if (ret) goto err_out; =20 + viommu_prefill_mapping(iommu, viommu); + return 0; err_out: amd_viommu_uninit_one(iommu, viommu); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from BL0PR03CU003.outbound.protection.outlook.com (mail-eastusazon11012027.outbound.protection.outlook.com [52.101.53.27]) (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 16C073A6412 for ; Mon, 14 Sep 2026 18:50:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.53.27 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411828; cv=fail; b=h+O7KteI9yWvzwCCUVG1kEh7OC/GwiGerPLsyg1CRD3ZLsmPXJbzQL/Hcr8nleOoz4mDkhvQb8DQaZOjr+cVV/a/Eg15YTtFZcoEQYQRmlAH3EI2frS0D191aVxNn8bdjWXrIlII3324fOUPClob7bXVq8KXvpVi9dk5dn850T8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411828; c=relaxed/simple; bh=AZRREDB0SLpAmnJH4ULHtpMjFlIFFAQR/Ff3vi9QnFs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QwvxFarQg53unj8lxnG/OVlbqOYkxjHQBixmeDBmkXja4JUSiTV4c7A3w3YAlt3FkJ0FdCYxtITjpQqLq7gbUGos3sYCOOpi56e0Ce6gFKBNTik3yU0kwHhAFf03ns5S6YUlEtna9rq/DCeGvPdEowGuuAhUaz+d2IK6y8aII7s= 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=DjjJS8Ti; arc=fail smtp.client-ip=52.101.53.27 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="DjjJS8Ti" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NNlTTKmXBOenwnh0zMBnKyKPC/1xhzuFhm+GWoyyK2nUx8UoPXeCcCdQ+ulE1IeqwlOoF4PvTxgHOHm7iehFkH5YhB1nJ6spgA1PTBaFbl+RL6Bjrp/Q2z6C5CO+hCqnMrAKVy2Coexez4U5RcJMsXQkXKu6kLQD0g+ekmah35wrNssrFGyPAawEL3ZRI1v0Fk3ozZw2hYN6GKVodfh43ruJPqZQr5mPamd4R/vCT4jUcu3UrB245KCFkDHrAvnVkfcCNq07OhJtcUMMrtCwb0bo7juZU7gmM93PPKv3yQ8p0JT7xyBQttnd/vT23G8ZXJTGFnBfBV4UtTU9dwBPpg== 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=IT9rfQSSLFZaJNih1naOmCgNsL1b6fIOZ9Mb9NnFspA=; b=YyS6/KmCpSEy8fOeem0/noqUe20YocIWqltZMJHUXkimp+ljDWli5L5p9BWYsWaZMu+TwIlwOhUTieH/l81k9jA4yUgNhJ/vfnRtv8zxpwuLgUeGR65dsH5Yy3KgyFcm+v9NR4P6k+j8t0oh0uyaIzeAzl0dVSTbDjZyppQsX+NJzTQ3scFMFdKHHOCD0TJX2iOv0XvWde54wlwcBpVoyMTJMYC9ZQTmTpAEx6DkCDu7JHjzl4KSLtqCgRa+nJySmFhnoTKpZ7m02XcKMP4uFCXrePOoKlkpdF1jQG6MdzViLAArG3vzZrKgrGRIY7Q3p31z/j84iGhMY17mpp3kHQ== 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=IT9rfQSSLFZaJNih1naOmCgNsL1b6fIOZ9Mb9NnFspA=; b=DjjJS8Ti2CzexFT4LXKiniKHR6GBRH90L5n0dplLpUk28cKg/IVSWbyGQvOIOcYz7JcJxBl6+EbNYXf8QB04vPpb9vjAB8zuR7pj9b0V36k5UbQqFuXk4K6jnm4NupZrOnd3h7d0xJ0xXvT/ZyUBDRCOTN3DyeyX8BOoV2nj7oc= Received: from CH2PR02CA0008.namprd02.prod.outlook.com (2603:10b6:610:4e::18) by IA1PR12MB6356.namprd12.prod.outlook.com (2603:10b6:208:3e0::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:16 +0000 Received: from BL02EPF00021F68.namprd02.prod.outlook.com (2603:10b6:610:4e:cafe::af) by CH2PR02CA0008.outlook.office365.com (2603:10b6:610:4e::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:50:15 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F68.mail.protection.outlook.com (10.167.249.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:15 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:50:05 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 19/24] iommu/amd: Add per-segment translate device ID pool Date: Mon, 14 Sep 2026 18:47:45 +0000 Message-ID: <20260914184750.222939-20-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F68:EE_|IA1PR12MB6356:EE_ X-MS-Office365-Filtering-Correlation-Id: 0457ff2d-c487-452f-224d-08df12910405 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|23010399003|1800799024|82310400026|36860700016|18002099003|22082099003|10067099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: gInHl6CqGzJnZhSWPPjNyc9UHt+D6xkENvBJP4k9BuB8z7YkhhWys8FUQUz20xNAUm48KVDBvzsQc8/OCgM1TYd1yUMcdM7SJ4HTrKlEdcUfTy7Z8p1AcBEEtB7k4gy1iZ7bxRj82sZ4B7Il7G6obx5Zxs2MmukTQWaHKcNBHxNWNJ1lwWtNv6Rv7jlD95nHO3loUM+g1hEyrzz0zjonOKswgPO1kqAjNdft9OMNl8LW13fI99dp1w7zUXgY8XKrRW1OddoV1zOl/RgFnYXmXXN3bwq4hByJm++JDJFFFffv5XPJ/j3UKaYd6mGcwClesOvuINWq5CnA9NgKjW/w9tYCLidaIPCzuNZrpOv9kxt5cNdZSFf1nA9fnJ5K4asj2OisLDRunsqgE9CSfzSiohgZSVmbv0GApHC+TsDiwfxuZ37RtMsZYe93Q41sUmYQo2X8eKppgATfxViuXChkM4dq+nzzFRoONr2LwBMxpz8EKCdY//3lteHllIulCGLS2VXeCne4z9dEwGAmTCIf1JNZa8egW++6KoXWo4YIS5x+e039KOosbbYCKqT6ekvBzIee4WUFU6zsTKb4ZrLLpl3PA/GefKnY8J+JweRsC1hyd+H9vhxPwonLDiuEHy3BP9ZrXyk9gUG2uHc89TVMcok9raTzG8QrWPbr/qnTEGFgo3GAmDAH3yBnbN96EUnr3cLMhOtE6E1Eznz8OuIw6g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(7416014)(376014)(23010399003)(1800799024)(82310400026)(36860700016)(18002099003)(22082099003)(10067099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OyjCroIl/7r5dmx1rVHYHveSXzZ5Y60jtV2XqXtWT7GmCIDi1FJLGG/y7tEvabkSCrFe/N8lelwOZXNbYZ4ab8ZSHCpJrV10FOHQSchLc2ECst8+IWrnQ2okcDI5en7LMRaSWaqScX0SgKoqQ8qifdfAK2k2YbUo7Vs64Zx3Y3ExBuWPvIZcGZP16oV8noQz8d37LafiX/Y5qqi5IwlLK9MleuYohaNDHGLH4MT8VQ0Su2PBQCm7Lyb1IQWsfc9ATRzYpUZoqg1vHrGNux7Aac//tfwhdkiJ27M4UILJJpAKSLSTTsa9L4NIpbbld2i8fJv5xACdXi6Jrmc23UEzJekCV2AUKYpedh++pdMoHWUD5Odu6iyc9JcBtq1Mp2ZsWHBmjE6gCXHrx04izry/rDz5vaBV6rtavAV2IFtcOjhwuo/emkSUVJaRA9jOdDvF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:15.6452 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0457ff2d-c487-452f-224d-08df12910405 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: BL02EPF00021F68.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6356 Content-Type: text/plain; charset="utf-8" A translate device ID (TransDevID) is a host device-table index used by hardware vIOMMU. VFCTRL guest-misc names the slot; that DTE holds the nest-parent v1 table for GPA->SPA walks of guest IOMMU traffic. Reserve PCI requestor IDs in a per-segment pool so they are not used as vIOMMU translation DTEs. The device table is per-segment, so the pool is too. Add trans_devid.c with pci_seg init/fini and amd_iommu_trans_devid_reserve() from probe_device(). Keep reservations for the pci_seg lifetime, including after amd_iommu_release_device(). Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu_types.h | 10 ++++ drivers/iommu/amd/amd_viommu.h | 14 +++++ drivers/iommu/amd/init.c | 3 + drivers/iommu/amd/iommu.c | 19 +++++++ drivers/iommu/amd/trans_devid.c | 87 +++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/trans_devid.c diff --git a/drivers/iommu/amd/Makefile b/drivers/iommu/amd/Makefile index e1e824b9c7b0..12c3fe83e4ce 100644 --- a/drivers/iommu/amd/Makefile +++ b/drivers/iommu/amd/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y +=3D iommu.o init.o quirks.o ppr.o pasid.o -obj-$(CONFIG_AMD_IOMMU_IOMMUFD) +=3D iommufd.o nested.o viommu.o +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) +=3D iommufd.o nested.o viommu.o trans_dev= id.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) +=3D debugfs.o diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index b5d1b23791da..6b3a43952797 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -681,6 +681,16 @@ struct amd_iommu_pci_seg { * parsing time. */ struct list_head unity_map; + +#ifdef CONFIG_AMD_IOMMU_IOMMUFD + /* + * Per-segment translate-device-id pool indexed by id. Entries are: + * absent (FREE), xa value TRANS_DEVID_RESERVED, or a pointer to the + * owning struct amd_iommu_viommu (ALLOCATED). + */ + struct mutex trans_devid_mutex; + struct xarray trans_devid_xa; +#endif }; =20 /* diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index f17ac13da213..b3717a006301 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -30,6 +30,10 @@ void amd_viommu_domain_id_update_locked(struct amd_iommu= _viommu *aviommu, =20 void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu, u16 hdev_id, u16 gdev_id); + +void amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg); +void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg); +int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 i= d); #else =20 /* @@ -66,6 +70,16 @@ static inline void amd_viommu_domain_id_update_locked(st= ruct amd_iommu_viommu *a { } =20 +static inline void +amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg) +{ +} + +static inline void +amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg) +{ +} + #endif /* CONFIG_AMD_IOMMU_IOMMUFD */ =20 #endif /* AMD_VIOMMU_H */ diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 1616fcfd361b..c646cf6bea6e 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -1740,6 +1740,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_seg= ment(u16 id, xa_init(&pci_seg->dev_data_xa); INIT_LIST_HEAD(&pci_seg->unity_map); list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list); + amd_iommu_pci_seg_trans_devid_init(pci_seg); =20 if (alloc_dev_table(pci_seg)) goto err_free_pci_seg; @@ -1756,6 +1757,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_seg= ment(u16 id, free_dev_table(pci_seg); err_free_pci_seg: list_del(&pci_seg->list); + amd_iommu_pci_seg_trans_devid_fini(pci_seg); xa_destroy(&pci_seg->dev_data_xa); kfree(pci_seg); return NULL; @@ -1780,6 +1782,7 @@ static void __init free_pci_segments(void) =20 for_each_pci_segment_safe(pci_seg, next) { list_del(&pci_seg->list); + amd_iommu_pci_seg_trans_devid_fini(pci_seg); xa_destroy(&pci_seg->dev_data_xa); free_irq_lookup_table(pci_seg); free_rlookup_table(pci_seg); diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index d620586d5734..34915e5ff156 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2731,6 +2731,22 @@ static struct iommu_device *amd_iommu_probe_device(s= truct device *dev) if (dev_is_pci(dev)) pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT); =20 +#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD) + /* + * Occupy this RID in the per-segment translate-device-id pool + * so vIOMMU alloc cannot reuse it. Probe is the RID lifetime; + * it is not released on unplug. If reserve fails, the IOMMU + * core frees dev->iommu and a later re-probe retries. + */ + ret =3D amd_iommu_trans_devid_reserve(iommu->pci_seg, dev_data->devid); + if (ret) { + pr_err("%s: Failed to reserve device id %#x\n", __func__, + dev_data->devid); + iommu_dev =3D ERR_PTR(ret); + goto out_err; + } +#endif + out_err: return iommu_dev; } @@ -2744,6 +2760,9 @@ static void amd_iommu_release_device(struct device *d= ev) /* * We keep dev_data around for unplugged devices and reuse it when the * device is re-plugged - not doing so would introduce a ton of races. + * Translate-device-id reservations stay as well: the RID remains + * occupied for the pci_seg lifetime so later alloc cannot reuse a + * DTE index that still belongs to this function. */ } =20 diff --git a/drivers/iommu/amd/trans_devid.c b/drivers/iommu/amd/trans_devi= d.c new file mode 100644 index 000000000000..195551fcfc1a --- /dev/null +++ b/drivers/iommu/amd/trans_devid.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2026 Advanced Micro Devices, Inc. + * + * AMD vIOMMU translate-device-id management. + * + * The pool is per PCI segment because the AMD IOMMU device table is + * per-segment. Each id must be allocated from unused slots in that + * segment. It is used to program the vIOMMU VF Control register to + * specify the DTE used to contain the GPA->SPA mapping (v1 page table). + */ + +#include +#include + +#include "amd_iommu.h" +#include "amd_viommu.h" + +enum trans_devid_state { + TRANS_DEVID_FREE =3D 0, + TRANS_DEVID_RESERVED, +}; + +static inline bool trans_devid_xa_is_reserved(void *entry) +{ + return entry && xa_is_value(entry) && + xa_to_value(entry) =3D=3D TRANS_DEVID_RESERVED; +} + +static inline void *trans_devid_xa_mk_reserved(void) +{ + return xa_mk_value(TRANS_DEVID_RESERVED); +} + +static int trans_devid_xa_install_reserved_locked(struct amd_iommu_pci_seg= *pci_seg, + u16 id) +{ + void *old; + + old =3D xa_store(&pci_seg->trans_devid_xa, id, + trans_devid_xa_mk_reserved(), GFP_KERNEL); + if (xa_is_err(old)) + return xa_err(old); + WARN_ON_ONCE(old); + return 0; +} + +void amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg) +{ + mutex_init(&pci_seg->trans_devid_mutex); + xa_init(&pci_seg->trans_devid_xa); +} + +void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg) +{ + xa_destroy(&pci_seg->trans_devid_xa); + mutex_destroy(&pci_seg->trans_devid_mutex); +} + +/* + * amd_iommu_trans_devid_reserve - occupy @id so it is never returned by a= lloc + * + * Reservation is done when probing the device (see amd_iommu_probe_device= ()). + * It is not released from amd_iommu_release_device(); the slot stays + * reserved for the pci_seg lifetime so replug cannot race later alloc. + * + * Return: 0 on success. A second reserve of an already-reserved @id succ= eeds. + */ +int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 i= d) +{ + void *entry; + int ret =3D 0; + + mutex_lock(&pci_seg->trans_devid_mutex); + entry =3D xa_load(&pci_seg->trans_devid_xa, id); + if (trans_devid_xa_is_reserved(entry)) + goto unlock; + + ret =3D trans_devid_xa_install_reserved_locked(pci_seg, id); +unlock: + mutex_unlock(&pci_seg->trans_devid_mutex); + + if (!ret) + pr_debug("%s: Reserved trans_devid %#x (seg %#x)\n", __func__, id, + pci_seg->id); + return ret; +} --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11010036.outbound.protection.outlook.com [52.101.56.36]) (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 88F1A38DC57 for ; Mon, 14 Sep 2026 18:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.56.36 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411827; cv=fail; b=JMbwBAjw3POqOb0U480fIt0qQ3YPRRP28gz6m27kbwcacsalz1oiF35dTc5b36kRxyKrHm/rCd8M8tDyttEw2ljwPG/A7EOjTnJ5Buz2pHUXaSSX0z/xM3wwkBX6sRHtvO2ALGTH/p1CyzKXx5ihJLkx9M4DaX4lz4zECd/ofB8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411827; c=relaxed/simple; bh=JwxorQLBG0ZrHrMC8fZlTGXaGChrxmT5lHERbzJnI/w=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hA3xCEB361ruFxcmBwubvA85QrdYs+HHwrp/LZDCeeqFN7ABlhefi9G7PgrG9dibFed8xZKLxRMRI/Dv2G/DLvg//33SKHLl6UXN0cvVZrgzmX7h7wajp/MXVdMGmi6v02Ik+4GT3ZHgF7Vl/l7HVnBzowsjyNydknGSFo2Rs78= 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=fFsROjzX; arc=fail smtp.client-ip=52.101.56.36 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="fFsROjzX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=l3sNhLg5TV6I0FrgAb2y6KWPxDPUVX6DuTIRXWwra86nlKz6ZDnXn1ZYzIX3SxidU+esA1zu5H7vYFtbht5ai8dUCyINJ6GSLM9aNBeHBwzx5D1Inb9ukOZZu4NhldXtKlsxY/D9M1qVdPZWJdfqAY3d1+kmuPVqB91maLbRTiLvEOh73//mLhbIUBU8BVwIlSqnWZlrbfqKsM5ufLOLg60KfOeYP3Vuln8gjN0G+PMLqvEU2RyzWScq/BflWeTMdcZhTUjQ8B3nDHdLKEDtGvvJ0u6vpL2aPDvGj3AAfsZ+WHY56PWlLjjVMC0bc1gIERBuAmUnn8v4hWfsfkhZmg== 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=ln1f0dHidx6ac5KgMijDZnnmkQUNwIp/VGcgroP4uyQ=; b=av4kD39qVzv4TBbvdAp9c01Hpj7PlOD2BzmB+ngOqMzXfKpFoiRwzbMGbFRCdoH4mjUKOud8xuITbKgdtYVd7nbdt8R+mT24E++GWBBq+gRflkWoGNMDxYuFOX1m2RpNp8SNXallcg35pqV4yhcoI++HoNtRBd4LJlQ9xzwi4J906CAPHQB+DiDfmHILUJe3Igk8csNKgxFnp/2fs68vRxf26rxOiPsBm/C7RMkOCMux3fcDTb61Gp35U/+0EEkSBtva+4xfD68tYpWWl2IrN2bU9FI4CSVf4476Z3FFxAkz5gCza0XFexwqdx1itvDqHtE30w+mLF74AmXs1Zvy+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=ln1f0dHidx6ac5KgMijDZnnmkQUNwIp/VGcgroP4uyQ=; b=fFsROjzXJROKfpKUDvSonuhu0gn2RZZxb4JXj0v3Bqu6gUTH/hUN+FMBQvffsE+KW3+KG/sN9k5z+aEHDWPgMllyd9h+BZ+ZqG9VMOzQnGczk/o5nYLFg9TBRFuQbT85nL92wPr/YBqnqWFuRNXC18qXdS+tCFgDNOHjrfSg29A= Received: from BN9PR03CA0946.namprd03.prod.outlook.com (2603:10b6:408:108::21) by SN7PR12MB7451.namprd12.prod.outlook.com (2603:10b6:806:29b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:19 +0000 Received: from BL02EPF00021F6E.namprd02.prod.outlook.com (2603:10b6:408:108:cafe::8e) by BN9PR03CA0946.outlook.office365.com (2603:10b6:408:108::21) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:50:18 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6E.mail.protection.outlook.com (10.167.249.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:17 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:50:11 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 20/24] iommu/amd: Reserve translate-device-id for PCI requestor aliases Date: Mon, 14 Sep 2026 18:47:46 +0000 Message-ID: <20260914184750.222939-21-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6E:EE_|SN7PR12MB7451:EE_ X-MS-Office365-Filtering-Correlation-Id: 81e653e6-9311-4846-d296-08df12910568 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|1800799024|82310400026|36860700016|376014|7416014|11063799006|5023799004|6133799003|56012099006|10067099003|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: ApbT7+k3O7KCHhejs593rDIxo4flLNCTiKv/rwFogIYcBFyF6l2VQNT9viSv9hEzDLG/dnTU+ZikDUJm2NyXZRCJGTHfQ/u399ESu7fW57uokRHV4wY9NWxpWseYByMhIQag9qRBqBot8J8WlbZm1uFLBtFp33lqWu7mH/wHnbj/WE/dq55gz25Y5yQjVCcN+acUJqBCCDrkHPSrPYltMnvItEe1lgt/zn5A3GAzTFlMbcPafQ/Zr6KLoOqAADH8hTY+7aOjp5pW8OPg9wOPs/4ojeYwhTtPUYU04IdMx5ebdNCxF2B36qVLKanPhZRrkR8JmOVQo4I+MBeoqsnhzQrPfI3PGHXH+wrxamfvVA8+r7rsmUKrUDtNRiIkox3wQMtD7wYwXlsSd82LXjTxCFkLCJOU6mBOw1bnRaLz/rO30vrX7B2SQdrxzOaABzjxwtnBJxoFUapwVqywpcxkEhdzHIJABOvJJVt3Cg3K/rZ8dQ3LO7m6P1ZxrM+zTQDUaihmSQjcbyoU/Zb+v1UE++EQVvVwiRHvkzFZqVqtfLhaoU1b+MOOw0op2NHnxmcr0ezZhPraHlWeMF9D5E849qe5T6dhggqvotxmI8cqpZ2q+OgZpuF0pcETwEJdSjko3vH0gQKWPMfo7jt+j9X2G77NlkFnDNRCOCWGJHyhig06BmpTcdfViAatgtK79tupEvhEMIM4RF2xOKSP5+7Tzw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(1800799024)(82310400026)(36860700016)(376014)(7416014)(11063799006)(5023799004)(6133799003)(56012099006)(10067099003)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mjTCELuJ+JKyUuENFSoq7Sk07zIhXhL9pStPRYG1lopBeiDB6o7m4d6IqcOsaK5STDAZ/FSV+mUisIIfyFXLs5qatSNWkpYuGBbEHlObfek3T4s4+j8DmY6Mk26Wy0Rpl0twJZzO+lbu1L1bDOyXZV5ncGTboxzv/sPw3eGmQ1y9koA/7+XNXbwV3y9UW1WYSQEJnHsC57L62i2LDFz+f5Y/F5epSvdiQdZzhnmMCh0F5EPK7H07Fnoa7zPIRyqgSUAo8MeyM+LtcQWIUzYqq3pkwcs7Mojy20NhLCJkubDUoWIMoJOxl6U6tdrz/9O3nUn0lNGxxE2LAJKZRIi6Eg9M4MaM9TEswEKbKMk31KlJNDhbr8MoOdZtckt1ReVqQgSB6/WPQUWz7r0RngGUJkNdy01jBald7UHeipGdHZg/Fhu0my6G/NorRCX1AhJS X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:17.9773 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 81e653e6-9311-4846-d296-08df12910568 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: BL02EPF00021F6E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7451 Content-Type: text/plain; charset="utf-8" The per-segment translate-device-id pool tracks which device-table indices are reserved for real PCI functions versus available for dynamic allocation to vIOMMU translation DTEs. amd_iommu_probe_device() already calls amd_iommu_trans_devid_reserve() for the struct device's own PCI BDF so that id cannot later be returned by the allocator. That is not sufficient on its own. The AMD IOMMU driver programs identical DMA translation device-table entries (DTEs) for every requestor ID that can issue DMA on behalf of the same PCI function: the IVRS alias from alias_table[] when it is not covered by the PCI DMA-alias walk (different bus than the device), and every alias visited by pci_for_each_dma_alias(). Those alternate BDFs are not separate struct device probe targets, so they never received a trans_devid reservation and could in principle collide with a dynamically allocated trans_devid. Introduce amd_iommu_trans_devid_reserve_pci_aliases() in trans_devid.c and invoke it from amd_iommu_probe_device() immediately after the primary amd_iommu_trans_devid_reserve() succeeds. For PCI devices the helper reserves the IVRS alias when it differs from the device BDF, then walks pci_for_each_dma_alias() and reserves each alias BDF. Repeated probe and overlap with the primary BDF in the PCI walk are handled by the existing idempotency of amd_iommu_trans_devid_reserve() (a second reserve of an already-reserved id succeeds). Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_viommu.h | 2 ++ drivers/iommu/amd/iommu.c | 8 ++++++ drivers/iommu/amd/trans_devid.c | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index b3717a006301..c91ba854847d 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -34,6 +34,8 @@ void amd_viommu_set_device_mapping(struct amd_iommu_viomm= u *aviommu, void amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg); void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg); int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 i= d); +int amd_iommu_trans_devid_reserve_pci_aliases(struct amd_iommu *iommu, + struct device *dev); #else =20 /* diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 34915e5ff156..fd3d717650fc 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2745,6 +2745,14 @@ static struct iommu_device *amd_iommu_probe_device(s= truct device *dev) iommu_dev =3D ERR_PTR(ret); goto out_err; } + + ret =3D amd_iommu_trans_devid_reserve_pci_aliases(iommu, dev); + if (ret) { + pr_err("%s: Failed to reserve translate devid for alias of %#x (err %d)\= n", + __func__, dev_data->devid, ret); + iommu_dev =3D ERR_PTR(ret); + goto out_err; + } #endif =20 out_err: diff --git a/drivers/iommu/amd/trans_devid.c b/drivers/iommu/amd/trans_devi= d.c index 195551fcfc1a..19a219519c92 100644 --- a/drivers/iommu/amd/trans_devid.c +++ b/drivers/iommu/amd/trans_devid.c @@ -11,6 +11,7 @@ */ =20 #include +#include #include =20 #include "amd_iommu.h" @@ -85,3 +86,47 @@ int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_s= eg *pci_seg, u16 id) pci_seg->id); return ret; } + +static int reserve_trans_devid_each_dma_alias(struct pci_dev *pdev, u16 al= ias, + void *data) +{ + struct amd_iommu_pci_seg *pci_seg =3D data; + + (void)pdev; + return amd_iommu_trans_devid_reserve(pci_seg, alias); +} + +/** + * amd_iommu_trans_devid_reserve_pci_aliases - reserve translate-device-id= s for + * PCI DMA aliases and for the IVRS alias when it is not walked as a PCI D= MA + * alias (different bus). Idempotent for repeated probe; see + * amd_iommu_trans_devid_reserve(). + * + * Return: 0 on success or if @dev is not PCI; otherwise an errno from + * amd_iommu_trans_devid_reserve() or pci_for_each_dma_alias(). + */ +int amd_iommu_trans_devid_reserve_pci_aliases(struct amd_iommu *iommu, + struct device *dev) +{ + struct pci_dev *pdev; + struct amd_iommu_pci_seg *pci_seg; + u16 devid, ivrs_alias; + int ret; + + if (!dev_is_pci(dev)) + return 0; + + pdev =3D to_pci_dev(dev); + pci_seg =3D iommu->pci_seg; + devid =3D pci_dev_id(pdev); + + ivrs_alias =3D pci_seg->alias_table[devid]; + if (ivrs_alias !=3D devid) { + ret =3D amd_iommu_trans_devid_reserve(pci_seg, ivrs_alias); + if (ret) + return ret; + } + + return pci_for_each_dma_alias(pdev, reserve_trans_devid_each_dma_alias, + pci_seg); +} --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011058.outbound.protection.outlook.com [52.101.52.58]) (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 DE8B33B5843 for ; Mon, 14 Sep 2026 18:50:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.52.58 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411832; cv=fail; b=hfUYMKGhrRzFoHb7TaYja1T5Ks1eRAD49pXA/L1LJy0T8R1W5KNVEz/M8m8r+tCPL9wR+fJu7USO1kOt+IdpmeltzKdT281T2+ZyfQQ7G6+3ilH0l57nFmeDnNE9OQiXnvk9Si779at1AF+CHW5kpGYt9JpeSdvxOoHoMWusTuQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411832; c=relaxed/simple; bh=rNWi1MYpmHy4vStNBtthgWvMpRzOKDjOrNctnSYq6mM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=P52KmTDYQJROW+dZB0+8c5OtFX/OMW8xb/fwY28Ys9/RfPig6AudA0Dvvw1DFp5UtkMusYeBCWyFIWdud7go9mS6PBC0ZrrvbuF+Lwi1F/pWXRU6BYDXvQTvid9l6diw08anc4ESecP5Tvoj3u3TjYWSwLToD8/zmMOTYmua1sM= 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=sJ0dnFJD; arc=fail smtp.client-ip=52.101.52.58 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="sJ0dnFJD" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=boG45EZbdvbSvB/aqeiDsCafAxzOkSx2N9i5oVGgOS0M4kiT6mqjRowo78CiT23x2oG3wVcL92wiT8dMSch215R/5j6sL22Jm5YoyNN9knrEAYkkjInh7Eiw0FRCIcSHnQOkrM/xXGMlKCb9UU8cZ9cy4Y8cjZn6/l8G9jb7Qivk+ZWxk0Tw5QkvupL3AT9FMBtrbmUno/EHgmdt8/6QDJqoGBvfxgVLaEzHcqRJ2ugaNBmYiI8TOeq9ZQMH6UsbwaOyjehR719A1MM8vM6BB4+lPXOInKdOrLGcYT64dIpOKRZngn8N/xt0qvh7cKMXINBPax+YWuDM2aVFUs3IHQ== 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=1TJOMgw1GQyJWzLvhE89VZjDK+OSJhe/m8Wiu7BcMvE=; b=UeaZJpQgv8A/oeZjyWr1bZI5GCVUXnv5mwu1YldroNOBtdYQSg5TulxEmOLvXKwzhzHHDjtRZg2pUZ03NYbXZKTsjkO0FEBuz+r3HpdWHzRaN8rGa39Bs3SeJg4y6RbCdn+UTHpJVLsb31ihyaBylOz9pcWThr3phhl4TVtZnhbY4643dayObbXrBueYrwCACMV8lGUWeJwtfe4b/EenSBRENhd0wZZlvrjn7M6i9ERZSFO4NufxUxSW4Nhgu4eTJ6pUBr+fjXKNunJtTJmytRG/lM0aHPwgoljQs/AmuUNvfOcEwWBAOUPpQANmxSUs8IhhoqW3U9bFGwDgqLMREg== 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=1TJOMgw1GQyJWzLvhE89VZjDK+OSJhe/m8Wiu7BcMvE=; b=sJ0dnFJDZTqOp1Uvb1CebXIHJdWDI9kY27/OzsxJ77AIbUoOiRN6Bdq1vH6PFjd+drFzWIMQWwX/DLoIlnGx9nJZFRjmFGVduESppNTR6C4dUeC2iDz4VgYT/amdw3jsWKKVST/lHOm0W3A7vnnPfEQdD1IbTfa2uXXS9kac8ZE= Received: from CH0PR03CA0439.namprd03.prod.outlook.com (2603:10b6:610:10e::8) by SA0PR12MB4415.namprd12.prod.outlook.com (2603:10b6:806:70::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:25 +0000 Received: from BL02EPF00021F6C.namprd02.prod.outlook.com (2603:10b6:610:10e:cafe::26) by CH0PR03CA0439.outlook.office365.com (2603:10b6:610:10e::8) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:50:24 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6C.mail.protection.outlook.com (10.167.249.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:23 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:50:17 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 21/24] iommu/amd: Add translation DTE and VFctrl TransDevID helpers Date: Mon, 14 Sep 2026 18:47:47 +0000 Message-ID: <20260914184750.222939-22-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6C:EE_|SA0PR12MB4415:EE_ X-MS-Office365-Filtering-Correlation-Id: 501948e6-c898-4de5-a079-08df129108ff X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|7416014|376014|1800799024|82310400026|36860700016|22082099003|18002099003|10067099003|56012099006|11063799006|5023799004; X-Microsoft-Antispam-Message-Info: 0XFd/JBtclhJroJU9yDbNBzJ+MR+e5wwwUeUozfXUpqi41MwtBrmOLeaee67RS2XuiU+7fndpdxlw4IGv7HZi13Lr+7Ez4RgCdkuVWCIJ8y9nV+mq8+2uQCZrVlELS3LZeIagAA5dejtZrnqTKmavCOjI7iM5wQKUCAJdcyHOIXg0FiY35iq8eo53Qvao7D48Uc8EbRn1dvU0/wMEP82XxxjBwGdw1Crwiv6qV+uyMFz1OmeDVssJ/vvSpaE+u+/y+EmOJBXD6nf7cF7MnhMy/iK4FRxGGxMkolgS7LpucGJoCXESIUo1RyBtKWTPTEL5FEtmnmnkKqZgIYYxNHJt54Mrjri+r7lPpkfKs3aLE4ApSggha135lqJuZJWdrZbRjgVybquES9lYHSmATUoWHrSscvprYusA82YpylmJKN9bgd8KnncDoytp+1b5V26ppNaBz+ug0DTM3SBRkL8LdtiXPvMajMgpJGWgVq7SCvFucHdKu1poFf7zWPjEXiRtaNYpJ5C32Az2bSs2yQuHCwlfLRLfIsLzZ7/YAi+INRAzd+fE1AYu6SlYOOucPtqRAcOMBPCppsYFU061K8nANLEg8VQGLgEhmQg76+lTpXE2oLHqQDRkRekjHFfizmO+uucABmlfpb7tsdLvJvD7Vs9Ff75vQV4lhxKW1C/gDHDgYRzsW4bGSBiNxRoOiNGpC6LlDpr10BRvzs4m64l/g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(7416014)(376014)(1800799024)(82310400026)(36860700016)(22082099003)(18002099003)(10067099003)(56012099006)(11063799006)(5023799004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dFLTNEnl21OrFBDStlY4KmuqtETGTxHaAFaT8wc4Wztuej119vMq6x3CXAiMQqU4PTuQVOhZyYnVh7IpAzrx/39A8Jd5sJybh5d74cPmQ0+rbF/qyvUDUUqrN4tqWL6+5kvL/OkcryLIC8fJUlmDpaN9dse3YYVLZiomtp4XUTLuob9epdxcxyAJ393Eq+dRMTSNWjhxt75HjCyytjsbNX+E5S0KD7bkRrLnn7P2oNs+6pc9yyDzqjrF7qPOqabXAjy/UdbUsYN6N+zG7uAtpPb2vvrQUzyVOC/1OUWbEsm+LH1VowKUhseTl9leGKO3DBSLIwgJhWFdF5TBYqhV7SlXQdsyPQwcY8XGexWqFm38a4nKb/sCkLWlhFueiMu0pQfISmpEPXm78YdH1G7fegsdPLhE594RS7gGfQZ2aYPYig0ce4rmW3Z2FxnIJ+q2 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:23.9986 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 501948e6-c898-4de5-a079-08df129108ff 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: BL02EPF00021F6C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4415 Content-Type: text/plain; charset="utf-8" vIOMMU GPA->SPA walks use a per-vIOMMU TransDevID: VFCTRL guest-misc names the slot, and that host DTE points at the nest-parent v1 table. Add amd_iommu_set_translate_dte() and amd_iommu_clear_translate_dte() to program that DTE, owned on amd_iommu_viommu.trans_dev_data rather than pci_seg->dev_data_xa. Rewrite it from amd_iommu_change_top() via pdom->viommu_list when the nest-parent table grows. Add amd_iommu_update_vfctrl_mmio_translate_devid() to publish the TransDevID in VFCTRL guest-misc MMIO. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 7 ++ drivers/iommu/amd/amd_iommu_types.h | 4 + drivers/iommu/amd/iommu.c | 153 ++++++++++++++++++++++++---- 3 files changed, 142 insertions(+), 22 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 6ac64524208b..4aede6f6d489 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -213,6 +213,13 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, struct dev_table_entry *new); =20 +int amd_iommu_set_translate_dte(struct iommufd_viommu *viommu); +void amd_iommu_clear_translate_dte(struct amd_iommu *iommu, + struct iommu_dev_data *dev_data, + u16 trans_devid); +void amd_iommu_update_vfctrl_mmio_translate_devid(struct amd_iommu *iommu, + u16 gid, u32 trans_devid); + static inline void amd_iommu_make_clear_dte(struct amd_iommu *iommu, u16 devid, struct dev_table_entry *new) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 6b3a43952797..5f84f25e3f37 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -489,6 +489,7 @@ extern bool amdr_ivrs_remap_support; /* VIOMMU stuff */ #define VIOMMU_VF_MMIO_ENTRY_SIZE 4096 #define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE 64 +#define VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET 0x10 =20 /* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */ #define VIOMMU_VF_MMIO_MAP_SIZE 0x10000000UL @@ -551,6 +552,9 @@ struct amd_iommu_viommu { =20 u64 *devid_table; u64 *domid_table; + u16 trans_devid; + /* Synthetic DTE; not stored in pci_seg->dev_data_xa */ + struct iommu_dev_data *trans_dev_data; =20 /* * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index fd3d717650fc..ba69489aabd3 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -148,11 +148,11 @@ static void write_dte_lower128(struct dev_table_entry= *ptr, struct dev_table_ent * 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) + u16 devid, struct dev_table_entry *new) { unsigned long flags; struct dev_table_entry *dev_table =3D get_dev_table(iommu); - struct dev_table_entry *ptr =3D &dev_table[dev_data->devid]; + struct dev_table_entry *ptr =3D &dev_table[devid]; =20 spin_lock_irqsave(&dev_data->dte_lock, flags); =20 @@ -160,12 +160,12 @@ static void update_dte256(struct amd_iommu *iommu, st= ruct iommu_dev_data *dev_da /* Existing DTE is not valid. */ write_dte_upper128(ptr, new); write_dte_lower128(ptr, new); - iommu_flush_dte_sync(iommu, dev_data->devid); + iommu_flush_dte_sync(iommu, 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); + iommu_flush_dte_sync(iommu, devid); } else if (!FIELD_GET(DTE_FLAG_GV, ptr->data[0])) { /* * Both DTEs are valid. @@ -173,7 +173,7 @@ static void update_dte256(struct amd_iommu *iommu, stru= ct iommu_dev_data *dev_da */ write_dte_upper128(ptr, new); write_dte_lower128(ptr, new); - iommu_flush_dte_sync(iommu, dev_data->devid); + iommu_flush_dte_sync(iommu, devid); } else if (!FIELD_GET(DTE_FLAG_GV, new->data[0])) { /* * Both DTEs are valid. @@ -182,7 +182,7 @@ static void update_dte256(struct amd_iommu *iommu, stru= ct iommu_dev_data *dev_da */ write_dte_lower128(ptr, new); write_dte_upper128(ptr, new); - iommu_flush_dte_sync(iommu, dev_data->devid); + iommu_flush_dte_sync(iommu, devid); } else if (FIELD_GET(DTE_GPT_LEVEL_MASK, ptr->data[2]) !=3D FIELD_GET(DTE_GPT_LEVEL_MASK, new->data[2])) { /* @@ -195,12 +195,12 @@ static void update_dte256(struct amd_iommu *iommu, st= ruct iommu_dev_data *dev_da =20 /* First disable DTE */ write_dte_lower128(ptr, &clear); - iommu_flush_dte_sync(iommu, dev_data->devid); + iommu_flush_dte_sync(iommu, devid); =20 /* Then update DTE */ write_dte_upper128(ptr, new); write_dte_lower128(ptr, new); - iommu_flush_dte_sync(iommu, dev_data->devid); + iommu_flush_dte_sync(iommu, devid); } else { /* * Both DTEs are valid and have guest page table, @@ -217,7 +217,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, struct dev_table_entry *new) { - update_dte256(iommu, dev_data, new); + update_dte256(iommu, dev_data, dev_data->devid, new); /* * Synthetic DTEs (vIOMMU self, translate-device-id) leave * dev_data->dev NULL so clone_aliases() is skipped. @@ -497,7 +497,7 @@ static int clone_alias(struct pci_dev *pdev_origin, u16= alias, void *data) ret =3D -EINVAL; goto out; } - update_dte256(iommu, alias_data, &new); + update_dte256(iommu, alias_data, alias, &new); =20 amd_iommu_set_rlookup_table(iommu, alias); out: @@ -2840,28 +2840,37 @@ static spinlock_t *amd_iommu_get_top_lock(struct pt= _iommu *iommupt) =20 #if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD) /* - * The vIOMMU private IPA domain programs a synthetic DTE for the - * IOMMU's own requester ID so hardware can DMA to backing store. - * That object is not on pdom->dev_list: it is not an IOMMU-API - * attach, and walkers such as rlookup and clone_aliases assume a - * real struct device. + * Synthetic DTEs are not on pdom->dev_list: they are not IOMMU-API + * attaches, and walkers such as rlookup and clone_aliases assume a + * real struct device. amd_iommu_change_top() therefore misses them. + * + * 1. The vIOMMU private IPA domain programs a DTE for the IOMMU's + * own requester ID (iommu->viommu_dev_data) so hardware can DMA + * to backing store. Early maps fit under the initial page-table + * top (PT_FEAT_DYNAMIC_TOP). Later DevID and DomID maps at high + * IPA call increase_top(); the old root stays live as a child of + * the new one, but the self DTE still holds the old MODE and + * would not translate those IOVAs. * - * amd_iommu_change_top() therefore misses it. Early maps fit under - * the initial page-table top (PT_FEAT_DYNAMIC_TOP). Later DevID and - * DomID maps at high IPA call increase_top(); the old root stays - * live as a child of the new one, but the self DTE still holds the - * old MODE and would not translate those IOVAs. + * 2. Each vIOMMU's translate-device-id DTE (aviommu->trans_dev_data) + * points at the nest-parent v1 table for GPA->SPA. That parent is + * also DYNAMIC_TOP. Guest maps of high GPA call increase_top() on + * the nest parent; the translate DTE would otherwise keep the old + * MODE and host_pt_root. * * Walk iommu_array (from amd_iommu_pdom_bind_iommu()) and rewrite * iommu->viommu_dev_data when this domain is that IOMMU's private - * IPA table. set_dte_entry() skips clone_aliases() because the - * synthetic DTE has no struct device. + * IPA table. Walk viommu_list and rewrite each trans_dev_data when + * this domain is a nest parent. set_dte_entry() skips clone_aliases() + * because those objects have no struct device. Use the vIOMMU's + * IOMMU, not rlookup. */ static void update_viommu_self_dte(struct protection_domain *pdom, phys_addr_t top_paddr, unsigned int top_level) { struct pdom_iommu_info *pdom_iommu_info; + struct amd_iommu_viommu *aviommu; unsigned long i; =20 lockdep_assert_held(&pdom->lock); @@ -2874,6 +2883,17 @@ static void update_viommu_self_dte(struct protection= _domain *pdom, set_dte_entry(iommu, iommu->viommu_dev_data, top_paddr, top_level); } + + list_for_each_entry(aviommu, &pdom->viommu_list, pdom_list) { + struct amd_iommu *iommu; + + if (!aviommu->trans_dev_data) + continue; + iommu =3D container_of(aviommu->core.iommu_dev, + struct amd_iommu, iommu); + set_dte_entry(iommu, aviommu->trans_dev_data, top_paddr, + top_level); + } } #else static inline void update_viommu_self_dte(struct protection_domain *pdom, @@ -3453,6 +3473,95 @@ static bool amd_iommu_enforce_cache_coherency(struct= iommu_domain *domain) return true; } =20 +#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD) + +void amd_iommu_update_vfctrl_mmio_translate_devid(struct amd_iommu *iommu, + u16 gid, u32 devid) +{ + writeq((devid & 0xFFFFULL) << 16, + VIOMMU_VFCTRL_MMIO_BASE(iommu, gid) + + VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET); +} + +int amd_iommu_set_translate_dte(struct iommufd_viommu *viommu) +{ + struct amd_iommu_viommu *aviommu =3D + container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu =3D + container_of(viommu->iommu_dev, struct amd_iommu, iommu); + struct protection_domain *pdom =3D aviommu->parent; + u16 gid =3D aviommu->gid; + u16 trans_devid =3D aviommu->trans_devid; + struct iommu_dev_data *trans_dev_data; + unsigned long flags; + + if (!aviommu->trans_dev_data) { + aviommu->trans_dev_data =3D + amd_iommu_alloc_dev_data(trans_devid); + if (!aviommu->trans_dev_data) { + pr_err("%s: Failed to allocate dev_data for translate-device-id %#x\n", + __func__, trans_devid); + return -ENOMEM; + } + } + + trans_dev_data =3D aviommu->trans_dev_data; + trans_dev_data->dev =3D NULL; + trans_dev_data->domain =3D pdom; + /* + * Leave off pdom->dev_list: this is not an IOMMU-API attach. + * Hold pdom->lock so change_top() cannot grow the nest-parent + * top between the sample and the DTE commit. Publish devid + * under that lock; it is the live TransDevID for + * update_viommu_self_dte(). If this vIOMMU is not yet on + * viommu_list, add it before dropping the lock so a concurrent + * increase_top() cannot miss the new DTE. + */ + + spin_lock_irqsave(&pdom->lock, flags); + trans_dev_data->devid =3D trans_devid; + set_dte_entry(iommu, trans_dev_data, 0, 0); + if (list_empty(&aviommu->pdom_list)) + list_add(&aviommu->pdom_list, &pdom->viommu_list); + spin_unlock_irqrestore(&pdom->lock, flags); + + pr_debug("%s: gid=3D%#x, iommu_devid=3D%#x, devid=3D%#x\n", + __func__, gid, iommu->devid, trans_devid); + return 0; +} + +static void amd_iommu_update_dte_slot(struct amd_iommu *iommu, + struct iommu_dev_data *dev_data, + u16 devid, struct dev_table_entry *new) +{ + update_dte256(iommu, dev_data, devid, new); + if (dev_data->dev) + clone_aliases(iommu, dev_data->dev); + iommu_flush_dte(iommu, devid); + iommu_completion_wait(iommu); +} + +void amd_iommu_clear_translate_dte(struct amd_iommu *iommu, + struct iommu_dev_data *dev_data, + u16 trans_devid) +{ + struct dev_table_entry new =3D {}; + + if (!dev_data) + return; + + pr_debug("%s: iommu_devid=3D%#x, trans_devid=3D%#x\n", + __func__, iommu->devid, trans_devid); + + /* + * Clear @trans_devid without swapping the live + * trans_dev_data->devid. change_top() indexes that field. + */ + amd_iommu_make_clear_dte(iommu, trans_devid, &new); + amd_iommu_update_dte_slot(iommu, dev_data, trans_devid, &new); +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ + const struct iommu_ops amd_iommu_ops =3D { .capable =3D amd_iommu_capable, .hw_info =3D amd_iommufd_hw_info, --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013034.outbound.protection.outlook.com [40.107.201.34]) (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 EB2F43A2E28 for ; Mon, 14 Sep 2026 18:50:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.201.34 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411843; cv=fail; b=bBOsj5NShH7IP4flgJ2KRjaJYR2mjgry3TnB0NP0Nzgp9LVZswoq46YAZRY76wGA/hvtgxWw6GB14yzeALX526oIIOSf4Z55K5zbF4P+d3eZUV43JRPjTkMwklCTCNp11v1cyLVVoou87THe2gAy0j5Bfu6NcAupJR5sc0JtuWw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411843; c=relaxed/simple; bh=XuSDAV4UcszjIdocS4pkHSgget01dEzaA1sJWdT33zs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J/julLk20w8cpdWOJncxUMrBd0Rr6HPbucz4oSW+rvs6T4O+Bfd0DCnCOwN9grFyJ2gx+CotCrP7xCWa0EauUqpV58EcVdgtgOhujhGpSe/6aRAeZ61CK1+SE1xdF+njNWVutJYARCAyajgWWOxemJlYL486EU4wLD8hzCMziPo= 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=zlnAzYEP; arc=fail smtp.client-ip=40.107.201.34 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="zlnAzYEP" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Oz0XJT66+6GDs3O+5lbsgglb/D7HT9M/J3lpMOlw6HW7Wq4NHTgy+Ng58ImphAT51sgSzNNkmGTqGQbG7ZIRbON9fR55LIASlv5YsnhCgHP0wSBdkY4NhUe+FfkczWnfNiZDnXLAf9Yv6QB4ToS/n03Uka/PstQ3KYxm9bZVeUs0jkfxQDzIXDtpOgxn/h1QaJVJd+OAL+PXqfypSfs8fWKB1tTu+ZAz9xAlSHuSd5MJUv3voBJOabXQ1ed9cjsmuNp7LCnDrMfiWxNZMUYRfq2X46i+mOdyqEa+YyyIDLAgqpb3NWNsdaH8lx4ZPzqBOKjw8y7dJ++aQDMdhh5q/Q== 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=X55N+BxiDl1uUq94sxRLNZvXyc3mtMUE+PEGklE3uBE=; b=j1WB62fo7zt0ZkZf7qEaWtxSw7vUeRhftY3du+2iQXQzC1JekcP9P3378aAlmi7+pT1Jrggs1T3YAOIldPe3D1d6t6dV8gwrQi57Nhh3JxFwybDiWIhuo5wAMBSUV5eOAjkZPtCy1Uji1iGqyMv7XWD3No+kbL/CE71sj69/a0Cw92Bj8GLUc5LiN2idMjYC1sg/R1A8z6TrPPWIHFgVYqHOOsy6JSVcULAIVJQdzPX7xhB4OcelnYHw1GSfecP2RqHOHehGXWwTiTVIG28tY8J8gPDDf+KGj3GepQGQqcblHChbzrmqw015YOSXfGkiomw+yQ6ECmZlmATKdsqd/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=X55N+BxiDl1uUq94sxRLNZvXyc3mtMUE+PEGklE3uBE=; b=zlnAzYEP1ZVokoKF+s++/h34Fbjz3hgC6pRxQTxi+MsRBmwjLVwtgcuBsl2MbZgPmqqXMr73awKsTyyLxJrC93SZan5LOJhaJsh9rj6mZBr7cCysYyLCjLYLYNL8ImerhXT431lqaOReTiBo3RNrJlhGSX+HiA9rg6VnNwUmS5Q= Received: from CH2PR02CA0026.namprd02.prod.outlook.com (2603:10b6:610:4e::36) by SA1PR12MB7199.namprd12.prod.outlook.com (2603:10b6:806:2bc::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:30 +0000 Received: from BL02EPF00021F68.namprd02.prod.outlook.com (2603:10b6:610:4e:cafe::77) by CH2PR02CA0026.outlook.office365.com (2603:10b6:610:4e::36) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.11 via Frontend Transport; Mon, 14 Sep 2026 18:50:30 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F68.mail.protection.outlook.com (10.167.249.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:30 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:50:23 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 22/24] iommu/amd: Add translate-device-id alloc/free with vIOMMU owner Date: Mon, 14 Sep 2026 18:47:48 +0000 Message-ID: <20260914184750.222939-23-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F68:EE_|SA1PR12MB7199:EE_ X-MS-Office365-Filtering-Correlation-Id: bc316f9c-cdfe-4881-eed3-08df12910c9a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|7416014|376014|23010399003|82310400026|1800799024|3023799007|10067099003|56012099006|11063799006|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: O5PLG+5RVgjMRrILl0g/41iDf2pxorEgo6J1B5DFwCc//AAWRbHJiwqJJCyN2cRR2oFCRtnbsgv7BzccFq76qYNsQPLT/E+S5CKuK8iZD30SodNiT0uedJPTkNX3SxCq0waNPZ8sEvFfTV1gzRlzSLhV9o8Fas391N+lChVQAEFx6chqfS5dfFwAeUQlnadURGP3hp0kqqyxzv29TCQLYNjn2zjpaPgEYn5SdE0omWe1jabWTc9lpT9okZQ0Uw3z/+BfxDZ9kT97o994Fi1lvTJUvyOMT6KpQa9v1v7pfeowOvXK+pKR4aH+5cBALV6VzNIelI8HREfyRdjIPPEu0/+SUkQhjOKjYrssy2v9rTrr5zXpQeF/c4xXkcfMWwz3n+y5oalBEvankoc4cGaRNvanShltB3RlQlaSGCZ3I+l8QUp7wk02pUjtc5XCNO1/dGAg94BubyTkzBeBu5vufESb5+P+QmrN8B1E1xVgxmTgOtwVkKhZNmCjRwhuPMNSZswliHLdrnWweZ16sTvxx57Hy3i82vs2JXHHNtUZ5zyzZGJHXbjgmHH6lRPpcQmViSnQKs6+6Ac5Rl15WEZsCT3mDeovjMMBNqYGyONUbPrTpRgug0GP+wSjQBmN50kixhWySc4cCjYDrOakwv/ITNx6ZuxPadzE+Lkv5yEM/BBSp9iWo6oVKa3bYd5bHy1P1MHDbRL2+pz+wbWC+71Hcg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(7416014)(376014)(23010399003)(82310400026)(1800799024)(3023799007)(10067099003)(56012099006)(11063799006)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /36ARdaqfHhmTv7oEVpQxeABx4U77ywlrzZKlS2T18QVSNRe/QdpuvUG8sW3WUzTg2jVb7c41UHIr401YAtklXGK2zvvC4+9Kjmie8hB5SVuR6ntLdYz1ugf/gcra6Xhpa2ewzlraR4YkEDZrMqfPhRLrkYpKruYZEVIOAk4buf71gmvc1FXacM+Wr0zehtlJpPHD5Jkc0qfUhJZesB1wmZeoz2vlpuFFFqbZ2Pr2wQa4FW5E6Fo23DoxsVz8BGFaLEQ75X0drsqLD61Xdaspxmyd4wEPxRXytZSBl59FkuIhSlsZEwUjfU8SP6g4bLcHHFoJpySTmN1dQnzfuP4hs0T1TzKIfn8vpC7tYmxZjF1s9XxbvfdJCnLD3e/LTjM1HKdxQF2m+7KiwooH/b6C3/5qndtNmTLAiBHLl9FgmiG0UY1GXszp1iLSsHJ52uJ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:30.0484 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bc316f9c-cdfe-4881-eed3-08df12910c9a 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: BL02EPF00021F68.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB7199 Content-Type: text/plain; charset="utf-8" Extend the per-segment translate-device-id pool to record the owning struct amd_iommu_viommu in the xarray. Add amd_iommu_trans_devid_alloc() and amd_iommu_trans_devid_free() to allocate ids from last_bdf down to 0 so each id indexes the device and alias tables, then return them on teardown. Free loads the xarray slot and erases it only when the owner matches @aviommu. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_viommu.h | 4 ++ drivers/iommu/amd/trans_devid.c | 105 ++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h index c91ba854847d..0c77c025f8b5 100644 --- a/drivers/iommu/amd/amd_viommu.h +++ b/drivers/iommu/amd/amd_viommu.h @@ -36,6 +36,10 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu= _pci_seg *pci_seg); int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 i= d); int amd_iommu_trans_devid_reserve_pci_aliases(struct amd_iommu *iommu, struct device *dev); +int amd_iommu_trans_devid_alloc(struct amd_iommu_pci_seg *pci_seg, + struct amd_iommu_viommu *aviommu); +void amd_iommu_trans_devid_free(struct amd_iommu_pci_seg *pci_seg, u16 id, + struct amd_iommu_viommu *aviommu); #else =20 /* diff --git a/drivers/iommu/amd/trans_devid.c b/drivers/iommu/amd/trans_devi= d.c index 19a219519c92..3a4e09fe39d5 100644 --- a/drivers/iommu/amd/trans_devid.c +++ b/drivers/iommu/amd/trans_devid.c @@ -28,24 +28,55 @@ static inline bool trans_devid_xa_is_reserved(void *ent= ry) xa_to_value(entry) =3D=3D TRANS_DEVID_RESERVED; } =20 +static inline struct amd_iommu_viommu *trans_devid_xa_owner(void *entry) +{ + if (!entry || xa_is_value(entry)) + return NULL; + return entry; +} + static inline void *trans_devid_xa_mk_reserved(void) { return xa_mk_value(TRANS_DEVID_RESERVED); } =20 -static int trans_devid_xa_install_reserved_locked(struct amd_iommu_pci_seg= *pci_seg, - u16 id) +static int trans_devid_find_free_locked(struct amd_iommu_pci_seg *pci_seg) +{ + int id; + + for (id =3D pci_seg->last_bdf; id >=3D 0; id--) { + if (!xa_load(&pci_seg->trans_devid_xa, id)) + return id; + } + return -ENOSPC; +} + +static int trans_devid_xa_install_locked(struct amd_iommu_pci_seg *pci_seg, + u16 id, void *entry) { void *old; =20 - old =3D xa_store(&pci_seg->trans_devid_xa, id, - trans_devid_xa_mk_reserved(), GFP_KERNEL); + old =3D xa_store(&pci_seg->trans_devid_xa, id, entry, GFP_KERNEL); if (xa_is_err(old)) return xa_err(old); WARN_ON_ONCE(old); return 0; } =20 +static int trans_devid_xa_install_allocated_locked(struct amd_iommu_pci_se= g *pci_seg, + u16 id, + struct amd_iommu_viommu *aviommu) +{ + return trans_devid_xa_install_locked(pci_seg, id, aviommu); +} + +static int trans_devid_xa_install_reserved_locked(struct amd_iommu_pci_seg= *pci_seg, + u16 id) +{ + return trans_devid_xa_install_locked(pci_seg, id, + trans_devid_xa_mk_reserved()); +} + void amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg) { mutex_init(&pci_seg->trans_devid_mutex); @@ -130,3 +161,69 @@ int amd_iommu_trans_devid_reserve_pci_aliases(struct a= md_iommu *iommu, return pci_for_each_dma_alias(pdev, reserve_trans_devid_each_dma_alias, pci_seg); } + +/** + * amd_iommu_trans_devid_alloc - allocate a translate-device-id for @pci_s= eg + * + * The trans_devid is allocated from pci_seg->last_bdf down to 0 so the + * id is always a valid device-table and alias-table index. PCI devices + * are generally enumerated from the low end of the bus range, so high + * ids are likely unused. + * + * Each vIOMMU receives its own translate-device-id from the per-segment p= ool. + * @aviommu is stored in the xarray as the slot owner. + * + * Return: allocated id on success, negative errno on failure. + */ +int amd_iommu_trans_devid_alloc(struct amd_iommu_pci_seg *pci_seg, + struct amd_iommu_viommu *aviommu) +{ + int id, ret; + + mutex_lock(&pci_seg->trans_devid_mutex); + id =3D trans_devid_find_free_locked(pci_seg); + if (id < 0) { + ret =3D id; + goto unlock; + } + + ret =3D trans_devid_xa_install_allocated_locked(pci_seg, id, aviommu); + if (ret) + goto unlock; + + mutex_unlock(&pci_seg->trans_devid_mutex); + pr_debug("%s: Allocated trans_devid %#x (seg %#x)\n", __func__, id, + pci_seg->id); + return id; + +unlock: + mutex_unlock(&pci_seg->trans_devid_mutex); + if (ret =3D=3D -ENOSPC) + pr_err("%s: No free trans_devid found (seg %#x)\n", __func__, + pci_seg->id); + return ret; +} + +/** + * amd_iommu_trans_devid_free - return @id to the per-segment pool + * + * Erase @id only if the xarray owner is @aviommu. A reserved or foreign + * slot is left unchanged. + * + * Caller must hold @aviommu->trans_devid_lock if racing with relocation. + */ +void amd_iommu_trans_devid_free(struct amd_iommu_pci_seg *pci_seg, u16 id, + struct amd_iommu_viommu *aviommu) +{ + void *entry; + + mutex_lock(&pci_seg->trans_devid_mutex); + entry =3D xa_load(&pci_seg->trans_devid_xa, id); + if (WARN_ON_ONCE(trans_devid_xa_owner(entry) !=3D aviommu)) + goto out; + xa_erase(&pci_seg->trans_devid_xa, id); + pr_debug("%s: Freed trans_devid %#x (seg %#x)\n", __func__, id, + pci_seg->id); +out: + mutex_unlock(&pci_seg->trans_devid_mutex); +} --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11010000.outbound.protection.outlook.com [52.101.56.0]) (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 ABCE0370D54 for ; Mon, 14 Sep 2026 18:50:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.56.0 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411850; cv=fail; b=ilVpjj8RsQuBH7+81DLzxFteS+6Ii2ir1RTGm1HzdPRPyP/dHwvD7mHqizee2eLPXDdtMlA/reQYbDboSSa+AdA6KsuZo4fy/ifkPpaMe2idHjdbY1CYp0myvpClTxgapompy0rqif1YXv4McbtNFes2BG3Xo+tpp1rU/6BkLAs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411850; c=relaxed/simple; bh=8jvnwGKNV/p61VH/lHZgouWmrbjCSF4zNn84dnkqthA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BnHslxZqaZC+6SxgJAyLtD7B3F+S6r5QpJZjaUdOhax44k66P03vVsjee+yRvpVlDeUl5EWD3H1xqEj6roObJt1tIkTjN1YZXZ4czd5mmuaw8OQ1vIVuiN2LaAW+8FRqfWk5GXWDbtBM97Rx4TjmooG99QrnTccaHwBf4qUDUn4= 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=EZOzAy9J; arc=fail smtp.client-ip=52.101.56.0 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="EZOzAy9J" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=dKQHr5VxcqHw8QMFDEwTE9+7YGKCBAbhBvbU9AP4JRd9nDKBpnraasi0noS3VDmWwxCZYDb8Hi49+oygLRqPtplp/SYWu4GyUaG2v9niXCoGpUsf8pvnCi/adtbXd0s7b5oQ4kGMmEwFojFhYyjgience94UCeycBxrAcpq6Z7g89iplxvl6cKA3Ql7/+LGZ38fSLTwRolPd/k5e4TKABLSOP5HakzFZXfvFVvj6wE4vDTYrDxjGmzUS4O/nom2nWFHuaHnTfKCjuBPR32EcHpZ+nIWkqRo/aNTBl+kfm4aZ3wQMszRbgNz4rQkhqdVmk33rJ2FW8AxjcdU2tiub4g== 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=pT7W8fmC5m+hdfdp26XkogyNVJdAqy1HdRuoB0WIrhI=; b=wK1TvjqzV7903pUtdOtuzyzlZOpFJYFWzYjZ7Hr2DNlWt3egpVda+3JYKSJTE5pfYicQ8Ci4iRvxq/X8eZmb5EAKibmY9CSlpNeGtOjODkPbHnHjcinRXb0Pbu3RnaBDKQ7j70QPUryxDeEauJcqENhKCPaeelIQUO6PDN0FWj7YHOUcocY+mv6CFlXJIvv5fVB0kagzkHVFuUs01RJlxZHX7BshanGk0fw9ydMTv/yt4sF/rStTEShPy1fB70DuEmDJtVP8j1dvzoFBDxdoAMIM2JOP15dZD+49mwt9ZfLsOnqfOOJi1xDZfyaN/oK8mVQqNxvRFiHirZu167FOkw== 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=pT7W8fmC5m+hdfdp26XkogyNVJdAqy1HdRuoB0WIrhI=; b=EZOzAy9JI+1LRgbRB5vsoe4rstE8hhlcq2GmzPy+ETsdSUCl/2PxBmwONYzwxLYiX02Z/26s608EcyqRJ596c04IXIYY+/byHEkpZ1bDzkK0/+8IPGKkTX+V8uU+Yq0221MNyvgPdSVIFSORGRkBZV7U4aEd+0gp2HYuKdNptfY= Received: from BN9PR03CA0503.namprd03.prod.outlook.com (2603:10b6:408:130::28) by PH0PR12MB7813.namprd12.prod.outlook.com (2603:10b6:510:286::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:36 +0000 Received: from BL02EPF00021F6F.namprd02.prod.outlook.com (2603:10b6:408:130:cafe::9b) by BN9PR03CA0503.outlook.office365.com (2603:10b6:408:130::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:50:36 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6F.mail.protection.outlook.com (10.167.249.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:36 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:50:29 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 23/24] iommu/amd: Assign per-vIOMMU translate device ID Date: Mon, 14 Sep 2026 18:47:49 +0000 Message-ID: <20260914184750.222939-24-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6F:EE_|PH0PR12MB7813:EE_ X-MS-Office365-Filtering-Correlation-Id: e29994ac-114c-4df1-5115-08df12911030 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|7416014|1800799024|82310400026|376014|36860700016|22082099003|18002099003|10067099003|11063799006|56012099006; X-Microsoft-Antispam-Message-Info: gCf0AJl9oxw++FrkNO+J+EtQF6UFtE6+PwrjoHNNX0jLwXhK4g8VbPUG9Pwkq1LfkpMLcC8HK2ch9xUhWZgn44CQFNfriwxu3PdiwI450NdJ1s7jL3ZK9nqDFhdSTwmsibiXRsAq1l5QYjSwh5p90442wqVSdpS+sdlMcQDeLhMvFV7hqo39nDYiQcJN+WKyPE9zpCKKYF6ZEhHZhdkJCfLEVm4EBlBLLml9d7EPVBGiqQUbRti8Jtj/LtQDtLRcgbNNwDHYD8BPaFk0s5VjolMVA7Fl2VX5oqA0fagnZo10EIYK3by5JSREIV32Ay/hChZ5m48wAP8QWTjwyVlgtP3UkWoh/mQ8a5gHwJw4CZaEoh50Nvma4B7ia52y/+GNpS61In0EmFIze4rNTVfW2m0l8ukCZce2tgf7vBws8EGFDroblcljkrzX53NnbLIqEetDrJwUlJYyiw0VLIsnTlkRxsdet2x78D3/XpKLoaJZgmWksPByw2SQuPIlfNcmey/xrAjn6pe1wy59DQCfMFJGahx3qottUkYHmGzDNyQPgjkT2d89HyqcQ+6coHrb7vHexkXH/59YwUA5SQed4XIFuOknu//ZFPwD2P5hYBMa3nY+33gOc47wa6lgGsRdwD1S0pWj3YnY8eE0fVrmSVAzqjlyOunCYVxLnWUpcf89o1NIsY9aRK8JUJT1bH/7/3bofEKaHLi/0PLRlaCOyg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(7416014)(1800799024)(82310400026)(376014)(36860700016)(22082099003)(18002099003)(10067099003)(11063799006)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OCWJcd/F0d4h9wYiDQ/O+G65uQzEboobR1MxPul55fH7dMh6K4Sp764L/py12qYsXXjQVKvJ0bsaakkcNfhFArlmUjekdrZwfcI/uyqdWWvDxdqO8/bT02NHuNXMh7IMZV/8PkuQBHFxd/ecsU/C2P5gpYexZPg7IIO3OzHCxvPhcnTZHmPhrhllNI0Fmke3voVDVi+lIZuWXvfvY7IzduUrRkxjyin6b7z7Sn4FcU4n23d/z2SFoRbyOsrxE4i0n/3b83jwtkmxWoakwS5ml5SKezmk7/YTccN9j6LovTgosGGFrifbwpjuoyBusVcXrd8vAZYESFoBxvwcu/hxAnd0RYSQ/t7cuV5gN4EgA8BS2B6UTLP67SMVn1VPspLWW21WDkUxfSB3gu4eaRmapvjnKny9duGLO8ArKULGKFOwGv7FpLm4tWI4nfQEYNJi X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:36.0635 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e29994ac-114c-4df1-5115-08df12911030 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: BL02EPF00021F6F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7813 Content-Type: text/plain; charset="utf-8" Allocate one translate-device-id per IOMMUFD vIOMMU instance from the per-segment pool on init. Reset MMIO first, then program translation DTE and VFctrl TransDevID under trans_devid_lock so PCI-probe relocate cannot interleave. Clear both on init error and destroy, and drop the id before amd_viommu_uninit_one(). Initialize pdom_list before set_translate_dte() so list_empty() is valid. Drop the late list_add; set_translate_dte() already publishes the vIOMMU on viommu_list under pdom->lock. list_del_init() on init error if that add ran. Free the synthetic translate-device-id DTE object on init error and destroy; it is not stored in the per-segment xarray. Add per-vIOMMU trans_devid_lock to serialize DTE and VFctrl TransDevID updates on init and teardown. VFctrl TransDevID is not written from amd_viommu_uninit_one(). Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 10 +++++ drivers/iommu/amd/iommufd.c | 66 +++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 5f84f25e3f37..c748bab27509 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -556,6 +556,16 @@ struct amd_iommu_viommu { /* Synthetic DTE; not stored in pci_seg->dev_data_xa */ struct iommu_dev_data *trans_dev_data; =20 + /* + * Serializes this vIOMMU's translation DTE and VFctrl TransDevID + * updates on init and teardown. VFctrl TransDevID is not written + * from amd_viommu_uninit_one(). + * + * Nested lock order: trans_devid_lock, then + * pci_seg->trans_devid_mutex. + */ + struct mutex trans_devid_lock; + /* * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells * (DevID and DomID table updates) for this gid. Those registers diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c index 5760b183ce62..1c29d846beed 100644 --- a/drivers/iommu/amd/iommufd.c +++ b/drivers/iommu/amd/iommufd.c @@ -53,6 +53,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu= , struct iommu_domain * int ret; unsigned long flags; phys_addr_t page_base; + bool trans_devid_allocated =3D false; + bool trans_dte_set =3D false; + bool uninit_on_err =3D false; struct iommu_viommu_amd data =3D {}; struct protection_domain *pdom =3D to_pdomain(parent); struct amd_iommu_viommu *aviommu =3D container_of(viommu, struct amd_iomm= u_viommu, core); @@ -70,7 +73,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu= , struct iommu_domain * if (ret) return ret; =20 + mutex_init(&aviommu->trans_devid_lock); mutex_init(&aviommu->vfctrl_lock); + INIT_LIST_HEAD(&aviommu->pdom_list); xa_init_flags(&aviommu->gdomid_array, XA_FLAGS_ALLOC1); aviommu->parent =3D pdom; =20 @@ -93,31 +98,63 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viom= mu, struct iommu_domain * =20 data.out_vfmmio_mmap_offset =3D aviommu->vfmmio_mmap_offset; =20 - /* Reset vIOMMU MMIOs to initialize the vIOMMU */ + /* Reset vIOMMU MMIOs before the trans_devid is visible in the pool. */ ret =3D amd_iommu_reset_vmmio(iommu, aviommu->gid); if (ret) - goto err_init; + goto err_trans_devid; + + mutex_lock(&aviommu->trans_devid_lock); + ret =3D amd_iommu_trans_devid_alloc(iommu->pci_seg, aviommu); + if (ret < 0) { + mutex_unlock(&aviommu->trans_devid_lock); + goto err_trans_devid; + } + trans_devid_allocated =3D true; + aviommu->trans_devid =3D ret; + + ret =3D amd_iommu_set_translate_dte(viommu); + if (ret) + goto err_init_unlock; + trans_dte_set =3D true; + amd_iommu_update_vfctrl_mmio_translate_devid(iommu, aviommu->gid, + aviommu->trans_devid); + mutex_unlock(&aviommu->trans_devid_lock); =20 ret =3D amd_viommu_init_one(iommu, aviommu); if (ret) goto err_init; + uninit_on_err =3D true; =20 ret =3D iommu_copy_struct_to_user(user_data, &data, IOMMU_VIOMMU_TYPE_AMD, out_vfmmio_mmap_offset); if (ret) - goto err_init_one; + goto err_init; =20 viommu->ops =3D &amd_viommu_ops; =20 - spin_lock_irqsave(&pdom->lock, flags); - list_add(&aviommu->pdom_list, &pdom->viommu_list); - spin_unlock_irqrestore(&pdom->lock, flags); - return 0; -err_init_one: - amd_viommu_uninit_one(iommu, aviommu); err_init: + mutex_lock(&aviommu->trans_devid_lock); +err_init_unlock: + if (trans_dte_set) { + spin_lock_irqsave(&pdom->lock, flags); + if (!list_empty(&aviommu->pdom_list)) + list_del_init(&aviommu->pdom_list); + spin_unlock_irqrestore(&pdom->lock, flags); + amd_iommu_update_vfctrl_mmio_translate_devid(iommu, aviommu->gid, 0); + amd_iommu_clear_translate_dte(iommu, aviommu->trans_dev_data, + aviommu->trans_devid); + amd_iommu_free_dev_data(iommu, aviommu->trans_dev_data); + aviommu->trans_dev_data =3D NULL; + } + if (trans_devid_allocated) + amd_iommu_trans_devid_free(iommu->pci_seg, aviommu->trans_devid, + aviommu); + mutex_unlock(&aviommu->trans_devid_lock); + if (uninit_on_err) + amd_viommu_uninit_one(iommu, aviommu); +err_trans_devid: iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); err_mmap: amd_iommu_gid_free(iommu, aviommu->gid); @@ -140,6 +177,17 @@ static void amd_iommufd_viommu_destroy(struct iommufd_= viommu *viommu) spin_unlock_irqrestore(&pdom->lock, flags); xa_destroy(&aviommu->gdomid_array); iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); + + mutex_lock(&aviommu->trans_devid_lock); + amd_iommu_update_vfctrl_mmio_translate_devid(iommu, aviommu->gid, 0); + amd_iommu_clear_translate_dte(iommu, aviommu->trans_dev_data, + aviommu->trans_devid); + amd_iommu_free_dev_data(iommu, aviommu->trans_dev_data); + aviommu->trans_dev_data =3D NULL; + amd_iommu_trans_devid_free(iommu->pci_seg, aviommu->trans_devid, + aviommu); + mutex_unlock(&aviommu->trans_devid_lock); + amd_viommu_uninit_one(iommu, aviommu); amd_iommu_gid_free(iommu, aviommu->gid); mutex_destroy(&aviommu->vfctrl_lock); --=20 2.34.1 From nobody Fri Sep 25 09:20:33 2026 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012007.outbound.protection.outlook.com [52.101.48.7]) (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 D65F23BBFC0 for ; Mon, 14 Sep 2026 18:50:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.48.7 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411854; cv=fail; b=BrH3zIUDRWtDqBud3xqcjHChv4pQJymaE9vkOtP0wjfY43UsXe9gjRzH8S+Liy+vk58zBBQgAoZqhw7LiSh2f45N6ji/ZvCmB9yAm0CswSZcRdYH05be6O5E5n/IGIfpAUW4J22vHS5rOZqPPnE/hQlbIfTLG0+yc3nHoUsqvaA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789411854; c=relaxed/simple; bh=lX+bNIkEB29tOxrT8AuUvkwVT0h75LHIr20zSMoyisg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qFcqPWxrfA7++4EZzXD5BnyZ205O4jwubjr0AUtwaMOzHbPtrBFeI9SFABZWV+L5Wlk8T81GlOZjaeH3/9LAAtn3yK5eQaEDbsoY7qU9YiZysT0AIYc3CGaxsHuiNUMAdoG4oNjVe3DnmkF15Z/aqGNigttw6AOjTG6oBLa0JqE= 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=KwER0iqb; arc=fail smtp.client-ip=52.101.48.7 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="KwER0iqb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=U04IXM//I56/OI5dotW/JfiiUzP+4SYzghHlc/E0+W8gAySUlumV4SgUhIUooqox2MEsB/5/rydQ87NbMdjnwICM3ET2RpaaebReFRjVXJboy2UPg3/YLvAJ2UC9fq9N04rKQEuz+8BnpykokpPdths0UM2l8iv0cTDlsGtDLIGiO6PsxMnQis34+Yz6i0L0owkIgkCkIqCXY/Y9OaKWwVcEFaKfZbgHtXjkaZFBQ7o8ut4T4UcsspbFWMyxh7URlXbJ81kRqnFwoTlkLEOnqK9y0y9q6E1DtnCOjhIwIbL83TykMHB+/uKDa5QWEexblE0oj6zDZuIAOTGGTXvLXA== 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=PZJ7ftxYGaOG3hsWiojJHAMiTE7pCVEEYmWBxLJS8Tg=; b=T++pnTrC9UBVyEK3/6llDdsaxvbrRWJdQtQIZ6bY3VdJ0uMlZZyCgM6UpuxEeGoASsb0l/bmqIPnYv5RixCxtN0D7p4H+RSlI83mPbRNMlyQXRAHizYkxKD1OFnxIps6t7RSvWiKAYiFVM+SQROljkILqVJE+3IOpAvu8XM1wviomimC3bQ03Cbc0qdY4HrTffDW6cgqJupUUl0xIr9Nw/rcrG2EsvT2nxJrWvSyfrnlsXsk++srX/B1xx3mzBXaYXbQHiVoTfSdF8HDiemvHLlxrCMry+xkXJ4tHdfC+uqHV+P6qTh8gDQBXBBWjWSWcx8A47cVrBgtcOjl0FVdbQ== 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=PZJ7ftxYGaOG3hsWiojJHAMiTE7pCVEEYmWBxLJS8Tg=; b=KwER0iqbzUJCUOBY4uKdwSrQhFgfcv2LS5p7kBQAxB+OoBauUHshVgm2bSlnEXijmP9/C379GqrhHwIFGcIuL92ZYIF7h4h7D4dfdTez9seCd7+gBQO0xMU+ClhfXtsinLoVtz0OYoxR8ahKPOzWSYyWEz0UOTD8MEVg+cLzFbc= Received: from BN9PR03CA0189.namprd03.prod.outlook.com (2603:10b6:408:f9::14) by DM4PR12MB7550.namprd12.prod.outlook.com (2603:10b6:8:10e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.12; Mon, 14 Sep 2026 18:50:46 +0000 Received: from BL02EPF00021F6D.namprd02.prod.outlook.com (2603:10b6:408:f9:cafe::4f) by BN9PR03CA0189.outlook.office365.com (2603:10b6:408:f9::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.12 via Frontend Transport; Mon, 14 Sep 2026 18:50:46 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL02EPF00021F6D.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.428.7 via Frontend Transport; Mon, 14 Sep 2026 18:50:46 +0000 Received: from purico-ed03host.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.49; Mon, 14 Sep 2026 13:50:35 -0500 From: Suravee Suthikulpanit To: , , , CC: , , , , , , , , , , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v5 24/24] iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve Date: Mon, 14 Sep 2026 18:47:50 +0000 Message-ID: <20260914184750.222939-25-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260914184750.222939-1-suravee.suthikulpanit@amd.com> References: <20260914184750.222939-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: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00021F6D:EE_|DM4PR12MB7550:EE_ X-MS-Office365-Filtering-Correlation-Id: 2740c92d-4d7c-471f-6e0f-08df12911623 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|82310400026|1800799024|36860700016|7416014|376014|11063799006|56012099006|10067099003|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: E8VzlNapoUOk4Nvbkfxpwdxwt0X+WCCKPywO2oaqzz6pd53Umt+ZjIPzNBYneRvW3SYFB7f29CCpNyjiPtmzlD0tCGj+LQslV/jYYH+41NH9vGua/F6xcRiZIFZFP9sfcXHLFcABNJNe2vnlXl0Jfn6UFI1diBF/+/IwLDgXBrt+NJOwgiQ/Hiq7sSfXDoW5yLJMgX5rN06Y/Sn17MekxfApzLQK8qpHnOTKtt4PKI2Dxj7inX3GySAlQIakMHOBayi4bmGu13DtK91dHWI3oC5gySIcatgDfhOCCxrkubBrpiN5E4+91irKaF2Sw/gQ51YjwBABE6GFcuvet/L9uk4cpMGruYtHsLJ38vj9o33IrTtVekFDACQjJFweo3c8Hmy3SFdeGhu+H1IvOjVVswxs+366A92pFJ5m5CtFXxogEpFLWk5TPvMRNaM7ASll75oJhMyn0tGwApL6qrpwgavHB+7paD4KUEtgMOmZ8+Q8D/c9ewDZbIYRUv31Pgsi0bjoyEr/KtQH+py5dKwbVeg3l4NCKnYDCRh4vOIjiKsL5o4QoFXjFg2ikF6jDD64Fj9ZQ4CQ3vvE8VpLOo3yvW4xA2tx59gcNIo9T5D30QQJo5ZsTC5lJJ99sulukIotkgefNcctnQzNZ1TWvr+9B0Fhnk5nfATHBpkcWfqlYtgWwm9KQ01h4JM/dz5kdgsa1BID/Cd0UfJr09gDul3nhw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(82310400026)(1800799024)(36860700016)(7416014)(376014)(11063799006)(56012099006)(10067099003)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Ri69uazwq/t8d27yw1GA0W5g38HGq5khyLhgKEgaRpdfDEzLLoLvKqn4MyigELditP8V1MBe35wivpjitie78d7QzouuWIwMBX0aJ02hlWgWEsSBP0ct4w6gzrC9XocGncWRJa60S6Ic7FpXLfFG1hVmygsFyn3aPchh4dkXvMbyYcC2I/8depsTf/BRf2T7eAmkkFwFvaes7k/leyPfVeaJe55a3M5D9Q6TH+gtuXt80La6pZfh2qWYfATyIgAA5Ft+Iz4YYYOG+RNTcxOKQu/f8VqmtChgSGQ+/oAwWFKukBMK8LP6e7coerCmqpNfUVC0uYL1q4iXF1ziTMHiFfJDLfttwJa5tQKkzoN/+JaeD4KL1XvJq03N9hGX+74MEgVBoEmn970qNB4496Tw/swQ6y7yRNxKyxPejLM2m9pBq3RIEnwqsGztCWDV0rPo X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2026 18:50:46.0392 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2740c92d-4d7c-471f-6e0f-08df12911623 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: BL02EPF00021F6D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7550 Content-Type: text/plain; charset="utf-8" When PCI probe reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a newly allocated translate-device-id before marking the BDF reserved for the device. Reserve holds the segment mutex and trylocks trans_devid_lock so the vIOMMU pointer stays valid without reversing the nested lock order. Reprogram translation DTE and VFctrl under trans_devid_lock; roll back the pool on failure. Mark from_id reserved with a raw xa_store so ALLOCATED->RESERVED does not WARN on the non-NULL owner. On MMIO failure, restore from_id the same way (RESERVED->owner) instead of install_allocated. Reuse the vIOMMU's trans_dev_data object. After programming the new slot, clear the old DTE without freeing the object. Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu_types.h | 10 +- drivers/iommu/amd/trans_devid.c | 146 +++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index c748bab27509..347c6954439d 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -558,11 +558,15 @@ struct amd_iommu_viommu { =20 /* * Serializes this vIOMMU's translation DTE and VFctrl TransDevID - * updates on init and teardown. VFctrl TransDevID is not written - * from amd_viommu_uninit_one(). + * updates on init and teardown against PCI-probe relocation, which + * may reprogram the same registers to a newly allocated id. + * VFctrl TransDevID is not written from amd_viommu_uninit_one(). * * Nested lock order: trans_devid_lock, then - * pci_seg->trans_devid_mutex. + * pci_seg->trans_devid_mutex (same as destroy). Relocate drops + * the segment mutex around the MMIO updates. Reserve holds the + * segment mutex and mutex_trylock()s trans_devid_lock so the + * aviommu pointer stays valid without reversing lock order. */ struct mutex trans_devid_lock; =20 diff --git a/drivers/iommu/amd/trans_devid.c b/drivers/iommu/amd/trans_devi= d.c index 3a4e09fe39d5..785f829db719 100644 --- a/drivers/iommu/amd/trans_devid.c +++ b/drivers/iommu/amd/trans_devid.c @@ -12,6 +12,7 @@ =20 #include #include +#include #include =20 #include "amd_iommu.h" @@ -89,6 +90,118 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iomm= u_pci_seg *pci_seg) mutex_destroy(&pci_seg->trans_devid_mutex); } =20 +/** + * trans_devid_do_relocate - move vIOMMU translation DTE from @old_id to @= new_id + * + * Caller holds @aviommu->trans_devid_lock. Pool xarray already records @= new_id + * as allocated to @aviommu and @old_id as reserved. + * + * amd_iommu_set_translate_dte() cannot fail here: trans_dev_data is + * allocated in the same trans_devid_lock section as the xarray owner + * and is only cleared with trans_devid_free(). A later failure after + * the new DTE is programmed must clear that DTE before the pool drops + * @new_id. + */ +static int trans_devid_do_relocate(struct amd_iommu_viommu *aviommu, + u16 old_id, u16 new_id) +{ + struct iommufd_viommu *viommu =3D &aviommu->core; + struct amd_iommu *iommu =3D + container_of(viommu->iommu_dev, struct amd_iommu, iommu); + int ret; + + aviommu->trans_devid =3D new_id; + + ret =3D amd_iommu_set_translate_dte(viommu); + if (ret) + goto err_restore_id; + + amd_iommu_update_vfctrl_mmio_translate_devid(iommu, aviommu->gid, new_id); + + amd_iommu_clear_translate_dte(iommu, aviommu->trans_dev_data, old_id); + + return 0; + +err_restore_id: + aviommu->trans_devid =3D old_id; + return ret; +} + +/** + * trans_devid_relocate - move an allocated id to a new slot and reserve @= from_id + * + * Called when PCI probe needs a BDF that a vIOMMU already owns. Updates = the + * per-segment pool, then reprograms DTE and VFctrl on the owning vIOMMU. + * + * Locking: caller holds @aviommu->trans_devid_lock and + * pci_seg->trans_devid_mutex. Hardware steps run with the viommu lock + * held and the segment mutex dropped. + */ +static int trans_devid_relocate(struct amd_iommu_pci_seg *pci_seg, u16 fro= m_id, + struct amd_iommu_viommu *aviommu) +{ + void *old; + int new_id, ret; + + if (trans_devid_xa_owner(xa_load(&pci_seg->trans_devid_xa, from_id)) !=3D + aviommu) { + ret =3D -ENOENT; + goto unlock_seg; + } + + if (aviommu->trans_devid !=3D from_id) { + ret =3D -EINVAL; + goto unlock_seg; + } + + new_id =3D trans_devid_find_free_locked(pci_seg); + if (new_id < 0) { + ret =3D new_id; + goto unlock_seg; + } + + ret =3D trans_devid_xa_install_allocated_locked(pci_seg, new_id, aviommu); + if (ret) + goto unlock_seg; + + /* + * Replace ALLOCATED with RESERVED in place. install_reserved + * WARNs on a non-NULL old entry; do not erase first or a failed + * store would leave from_id FREE while hardware still uses it. + */ + old =3D xa_store(&pci_seg->trans_devid_xa, from_id, + trans_devid_xa_mk_reserved(), GFP_KERNEL); + if (xa_is_err(old)) { + xa_erase(&pci_seg->trans_devid_xa, new_id); + ret =3D xa_err(old); + goto unlock_seg; + } + WARN_ON_ONCE(trans_devid_xa_owner(old) !=3D aviommu); + + mutex_unlock(&pci_seg->trans_devid_mutex); + + ret =3D trans_devid_do_relocate(aviommu, from_id, new_id); + if (ret) { + mutex_lock(&pci_seg->trans_devid_mutex); + xa_erase(&pci_seg->trans_devid_xa, new_id); + old =3D xa_store(&pci_seg->trans_devid_xa, from_id, aviommu, + GFP_KERNEL); + if (xa_is_err(old)) + ret =3D xa_err(old); + else + WARN_ON_ONCE(!trans_devid_xa_is_reserved(old)); + mutex_unlock(&pci_seg->trans_devid_mutex); + } + + mutex_unlock(&aviommu->trans_devid_lock); + return ret; + +unlock_seg: + mutex_unlock(&pci_seg->trans_devid_mutex); + mutex_unlock(&aviommu->trans_devid_lock); + return ret; +} + /* * amd_iommu_trans_devid_reserve - occupy @id so it is never returned by a= lloc * @@ -96,18 +209,49 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iom= mu_pci_seg *pci_seg) * It is not released from amd_iommu_release_device(); the slot stays * reserved for the pci_seg lifetime so replug cannot race later alloc. * - * Return: 0 on success. A second reserve of an already-reserved @id succ= eeds. + * When @id is allocated to a vIOMMU (e.g. after PCI hot-plug), the driver= relocates + * that vIOMMU to a newly allocated translate-device-id and reserves @id f= or the PCI + * function. + * + * Return: 0 on success, %-ENOSPC if relocation cannot find a free id, or = another + * errno from relocation. A second reserve of an already-reserved @id suc= ceeds. */ int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 i= d) { void *entry; + struct amd_iommu_viommu *aviommu; int ret =3D 0; =20 +retry: mutex_lock(&pci_seg->trans_devid_mutex); entry =3D xa_load(&pci_seg->trans_devid_xa, id); if (trans_devid_xa_is_reserved(entry)) goto unlock; =20 + aviommu =3D trans_devid_xa_owner(entry); + if (aviommu) { + /* + * Pin aviommu with trylock while the mutex still covers + * the xarray pointer. A blocking lock here would invert + * the nested order (ABBA with destroy). Dropping the + * mutex first would UAF if destroy frees aviommu. + * + * No retry bound. cond_resched() avoids a soft lockup; + * progress is this owner dropping trans_devid_lock or + * freeing the slot. + */ + if (!mutex_trylock(&aviommu->trans_devid_lock)) { + mutex_unlock(&pci_seg->trans_devid_mutex); + cond_resched(); + goto retry; + } + ret =3D trans_devid_relocate(pci_seg, id, aviommu); + if (!ret) + pr_debug("%s: Reserved trans_devid %#x after relocation (seg %#x)\n", + __func__, id, pci_seg->id); + return ret; + } + ret =3D trans_devid_xa_install_reserved_locked(pci_seg, id); unlock: mutex_unlock(&pci_seg->trans_devid_mutex); --=20 2.34.1