From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.52.69 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796033846; b=NtudmYSflvSR0YZeM41yrjHjKiywsO8G54saXYFQPN4DxROZyaoz5wWsLHa3hWKW58G0 MOVW3wh6k0MYSFPbQHqwpoMGUi38dTnHNtKpcpTp1cNXTvAFZ61gAJjwsr5vQNErGtBWm cLFVCWkenLirX+RtHhJGFqiKoFnkk8cK6abz1s0dWzkRGz4veIdsrisLF17xvb0P5tM+g tl6Xdf5r3lm3fnqkAXRk68Bp8nBzXM8BszJsc+jOuYPyXxc4vO8fdQ9EljhKNlLB0VQOp Oo5IWYFDb1iJe7iC95jdfa8DlmwQ37a7xem5CvVqSow6yT/NsjyEJ5SgHwZJt4qVjOw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796033846; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=cdvcFSRj3TONLFFtc35R8x1vCFQUVuDgmPNFIx1Kl1g=; b=nBwCfrqPNsW08AUgN0GNEGtDJcyQ8P40D9PmW0wJyPDSj+zLtDpCLEtlPgOSePrp2GE4 yUzxBpJjQNWy7Xu6JykeYJF+Iu+pL0DZYMlW304l4xfpvGhg2uz8Zo8B99iTzZyvKb2y5 EOesrgDZ66BrhN2YoXpPHI9nOgQE115XD1HCw2W6a/hpHXcEaXKQinwJsnxO06O+x0zSR xAlOw+cWPsRvpnKmgM6/khs5Mn3zdTl3pRSiy5a0RnbEOH2vnuiK5eZxMcyrjDh7LR7m3 Um+F6ZI5o/ztZqjw7/XQM9LxcCrd9BP5FN6L9KuKI27c5VODH9s9tq6Ys+PlLp3+0WA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=nREua19v; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.52.69 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011069.outbound.protection.outlook.com [52.101.52.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4565D606A320 for ; Mon, 30 Nov 2026 10:17:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4565D606A320 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, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=nREua19v ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IL8LpgS/HtZYnjQzl09tJwJOw244Agbe9V6d2VbOGta7hlB0Hmvv7V9e1MrulsP3EutLny/h9RPVucRAQ+u51crvX161lVZnrK5IpEVhGISgBXUb942v5Euf0TzO/HYdComGWKu4cX8OnS4skvsSCJAyCv2PPSi8GZjn/BeyKzAkbMU+OuYqhyb0+Kh61bONLtOXsUO7rTfqmknhhWnvAB6XSEoWIjlBDtQMMnAc7SjNxf3/pVkAMUfsqhD9Cqu6Sd3nlB3xlqmEaUSLRv8jvUmUU84AyiHXyCORFjyocE4FpOZIhAcO95IkDWAIqjF6KddRv/JmyvXUS3ovAGc6oQ== 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=cdvcFSRj3TONLFFtc35R8x1vCFQUVuDgmPNFIx1Kl1g=; b=EYCqECRTyWbvyS5tbxpQ7LmZp4QUl5sYCoXWRtC8bbpUW3zlj8QPfkWCAuAqaHk27Mn3QGOQL4gaFM20YlFG8zrhP0hb9774nCvILk9vmR9DkPMOHAKLtsEf1gTPWwmsGd0PFDf275TIJ33k9LoxEjsrCxwVF5XoNke0HnfApfuKqs4Ggf5Hsv5af4ecPKZQTyfaROezZ76BNnT/Gt2GqZ8lI5TTcc3wL9QEs/hKHETdZx4+rsa8WlJ9FonyM5FK5WQkCORa9sDNWLDVmc3bCg1vreNtvMzk6gdPftk+d8Jv3oxw8Gq3VvtPzs2yOEhUzUhV3MtcFrGo1JX795Cjvg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=bull.com 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=cdvcFSRj3TONLFFtc35R8x1vCFQUVuDgmPNFIx1Kl1g=; b=nREua19vf0VI7o3JvnkWrT+lpYLDo3K0RoT64zS+sydymVUmTFSQ0rCzYHVPNJF5A9axHO/92PSjAXQ3REBLFtywXCM6UeAqiUjEm5IoXNDnSrxQwnLi5oAcmIjAjwmZw8K89mFuCjGMYgmZN5ILjI5DySheubMXl9LTMmCdA7c= Received: from MW4PR04CA0341.namprd04.prod.outlook.com (2603:10b6:303:8a::16) by BL3PR12MB6474.namprd12.prod.outlook.com (2603:10b6:208:3ba::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.12; Mon, 30 Nov 2026 10:17:20 +0000 Received: from SJ1PEPF00002323.namprd03.prod.outlook.com (2603:10b6:303:8a:cafe::36) by MW4PR04CA0341.outlook.office365.com (2603:10b6:303:8a::16) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.13 via Frontend Transport; Mon, 30 Nov 2026 10:17:19 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; 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 SJ1PEPF00002323.mail.protection.outlook.com (10.167.242.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Mon, 30 Nov 2026 10:17:19 +0000 Received: from BLR-L1-SARUNKOD.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Mon, 30 Nov 2026 04:17:14 -0600 From: Sairaj Kodilkar To: =?UTF-8?q?Cl=C3=A9ment=20Mathieu--Drif?= , "Michael S. Tsirkin" , Alejandro Jimenez , Ani Sinha , "Igor Mammedov" , Jason Wang , "Marcelo Tosatti" , Paolo Bonzini , "Richard Henderson" , Sairaj Kodilkar , Yi Liu , , CC: , Subject: [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs Date: Mon, 30 Nov 2026 15:42:35 +0530 Message-ID: <20261130101237.1416-5-sarunkod@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261130101237.1416-1-sarunkod@amd.com> References: <20261130101237.1416-1-sarunkod@amd.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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: SJ1PEPF00002323:EE_|BL3PR12MB6474:EE_ X-MS-Office365-Filtering-Correlation-Id: c6b188ca-f6cc-4269-6160-08df4ecb1dc7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|36860700016|376014|7416014|23010399003|6133799003|18002099003|22082099003|921020|10067099003|11063799006|56012099006|261028144727099003|261102123227099003|261022115727099003|261022113327099003|261022105027099003|261022075727099003|261026121527099003|261026140227099003|261021121427099003|261021123027099003|261009223027099003; X-Microsoft-Antispam-Message-Info: ZNOD7P2/ykd0Qr/gR7pv9lSDttocadazW/l3V1bw56LhewsXPc84sHiU7idJiSop/jdmSf6UG+13XdOIkrBs0MauHCxvl0+4CObXPd9QizsWyRj9ehHRxfRacj2qnAOu4lzVk4qMC8XEWT+co2xJjEPTepOsmxI2oAoPULugS3TYSZAYR/MMrjcNJyrFTxtT44zb4J4U2s+tTK9uBxwqCqGFAuswBAIzvX7g54M5naJtI20xem79sS+5rX1NzEfHe4RmUMNGFB+/5m8nmNPoC6jbsU6h5iAvEUygA1EMCHgRZ+JE/c30xLgqru/hVpmvGmgc1zlkIMckWyRJ3Txx4CIC0c9Y75Gb9V3Fwjaw+uarZm8U29cqkbz4yyLsjcmQczENDpck/oKM8wvrJv4eornaQJOoOZcBBkzhJUMcXPaN9GyYHr7r9+I8zvCMN84+TsfYl0/jDqoVWaMBWae2LFmxiR/D5OCGzaqw3Xv+JsbCtLCmVAw3Utym2Cws8qGUA39dT/9PtHnfSAYh78vLUIAxMxElj5LJ61eSOEUx6VclKVN/2AnV0CjERQUVbkqJcrlpNteBWMESxXMLU/Ul6R4FyscSkn6Q4v1u5/SyAlcBIqCqP1IrJjvxW/1LD1jbAY2QOBf0BF/IvBrDkQNYfIkFYvNBkzaC7puMvhlyKEnpXC0/eLcOXDto9Mex65GhXK63em6ZmUuu5iSP4oJ471Ta17/JAEopxWAg2CdW0JA+NwC5xyWgOG5P9Rl51M0T 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)(36860700016)(376014)(7416014)(23010399003)(6133799003)(18002099003)(22082099003)(921020)(10067099003)(11063799006)(56012099006)(261028144727099003)(261102123227099003)(261022115727099003)(261022113327099003)(261022105027099003)(261022075727099003)(261026121527099003)(261026140227099003)(261021121427099003)(261021123027099003)(261009223027099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WBvjb+E9zbEQv8CNpNfN4cOlVSVXEyxifOG6UMlXmQ46Gw1ruEceds2Sgms/MHvPvQuSmG5s0BrTYE4Zqsc/CFwP3idDXXJDPL12RV3XRpS2wm3AfuAxuI4AgbKWI5pHv+d1TUFPumkJnM+z98ou56NuN+riOY4r9rUIn7+irClS9X1nWeDCldyY4SHV7LUTU0ElKI/qgXegmLVEfV6+hgWM+4zPw4W+PTEuyIoAA4qJxZqBvhsaIztRCSSHjC/np2r9Y2xWxfySS4TBVoi7Z4iqem4c2sZkTKk5JqIqMSD9dMFFLgsw/7EYPqK9Hk14/i054k8f5zb5Rm8bqwUhYljP5r37PUtB9B/fccFS5EhDvUU5IVET6LUK6eLJwibG9o4BbGhRDC40TmNHL8XyqQuLEhPbYi5BoV4zAwXT5O7/9SGBqMclSDunHd3GFBi+ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Nov 2026 10:17:19.3551 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c6b188ca-f6cc-4269-6160-08df4ecb1dc7 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: SJ1PEPF00002323.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6474 Walk the IOMMU list when constructing the IVRS so each IOMMU is added as IVHD. When emitting an IVHD entry, skip a child PCI bus if another IOMMU is already attached to it. That lets a default IOMMU cover pcie.0 while additional IOMMUs isolate selected downstream buses without overlapping IVHD coverage. Assign each IOMMU a distinct MMIO window beginning at 0xfed80000, stepped by the x86 IOMMU instance index, so every device has a unique register base. amd-iommu devices with and without 'pci-id' cannot be mixed, because the vmsd (migratable or not) is shared by the class. Examples, once multiple vIOMMUs are allowed later in the series: Example1, For below PCI configuration AMD IOMMU 1 will serve all the devices under pxb-pci1 and AMD IOMMU 2 will serve all the devices under pxb-pci2. pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1 | +- pcie-root-port2.1 --- Device1 | \- pcie-root-port3.1 --- Device2 \- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2 +- pcie-root-port2.2 --- Device3 \- pcie-root-port2.3 --- Device4 Example2, For below PCI configuration, AMD IOMMU 3 will only serve Device5 and Device6 and devices under pxb-pci buses will be served by IOMMUs of respective buses. pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1 | +- pcie-root-port2.1 --- Device1 | \- pcie-root-port3.1 --- Device2 +- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2 | +- pcie-root-port2.2 --- Device3 | \- pcie-root-port2.3 --- Device4 +--- AMD IOMMU 3 +--- Device5 \--- Device6 Example3, The below PCI configuration is invalid since both IOMMUs share same root bus i.e. pxb-pci1. pcie.0 -+- pxb-pci1 -+- pcie-root-port1 --- AMD IOMMU 1 +- pcie-root-port2 --- AMD IOMMU 2 +- pcie-root-port3 --- Device2 \- pcie-root-port4 --- Device3 Signed-off-by: Sairaj Kodilkar --- hw/i386/acpi-build.c | 196 ++++++++++++++++++++++--------------------- hw/i386/amd_iommu.c | 56 ++++++++++--- hw/i386/amd_iommu.h | 3 + 3 files changed, 148 insertions(+), 107 deletions(-) diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index 0aa31af8de5f..188c97f0e916 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -1845,7 +1845,10 @@ select_devices: pci_for_each_device_under_bus(bus, insert_ivhd, table_data); QLIST_FOREACH(child_bus, &bus->child, sibling) { - add_ivhd_entries_from_bus(child_bus, table_data); + /* In case of multiple IOMMUs, Child bus can have a separate iommu */ + if (!child_bus->iommu_ops) { + add_ivhd_entries_from_bus(child_bus, table_data); + } } } @@ -1885,15 +1888,10 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) { PCMachineState *pcms = PC_MACHINE(qdev_get_machine()); - PCIOMMUEntry *first = QTAILQ_FIRST(&pcms->iommus); - X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(first->dev); - AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu); - GArray *ivhd_blob = g_array_new(false, true, 1); - X86MachineState *x86ms = X86_MACHINE(qdev_get_machine()); + X86MachineState *x86ms = X86_MACHINE(pcms); + PCIOMMUEntry *entry; AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id, .oem_table_id = oem_table_id }; - uint16_t iommu_devid = pci_get_bdf(&s->pci->dev); - uint64_t extended_feature = amdvi_extended_feature_register(s); acpi_table_begin(&table, table_data); /* IVinfo - IO virtualization information common to all @@ -1915,99 +1913,109 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, * blob further below. Fall back to an entry covering all devices, which * is sufficient when no aliases are present. */ - assert(s->root_bus); - add_ivhd_entries_from_bus(s->root_bus, ivhd_blob); - if (!ivhd_blob->len) { - /* - * Type 1 device entry reporting all devices - * These are 4-byte device entries currently reporting the range of - * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) - */ - build_append_int_noprefix(ivhd_blob, 0x0000001, 4); - } + QTAILQ_FOREACH(entry, &pcms->iommus, next) { + X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(entry->dev); + AMDVIState *s = AMD_IOMMU_DEVICE(entry->dev); + GArray *ivhd_blob = g_array_new(false, true, 1); + uint16_t iommu_devid = pci_get_bdf(&s->pci->dev); + uint64_t extended_feature = amdvi_extended_feature_register(s); - /* - * When interrupt remapping is supported, we add a special IVHD device - * for type IO-APIC - * Refer to spec - Table 95: IVHD device entry type codes - * - * Linux IOMMU driver checks for the special IVHD device (type IO-APIC). - * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' - */ - if (x86ms->ioapic_iommu == x86_iommu) { - build_append_int_noprefix(ivhd_blob, - (0x1ull << 56) | /* type IOAPIC */ - (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */ - 0x48, /* special device */ - 8); - } + assert(s->root_bus); + add_ivhd_entries_from_bus(s->root_bus, ivhd_blob); - /* IVHD definition - type 10h */ - build_append_int_noprefix(table_data, 0x10, 1); - /* virtualization flags */ - build_append_int_noprefix(table_data, - (1UL << 0) | /* HtTunEn */ - (1UL << 4) | /* iotblSup */ - (1UL << 6), /* PrefSup */ - 1); - - /* IVHD length */ - build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2); - /* DeviceID */ - build_append_int_noprefix(table_data, iommu_devid, 2); - /* Capability offset */ - build_append_int_noprefix(table_data, s->pci->capab_offset, 2); - /* IOMMU base address */ - build_append_int_noprefix(table_data, s->mr_mmio.addr, 8); - /* PCI Segment Group */ - build_append_int_noprefix(table_data, 0, 2); - /* IOMMU info */ - build_append_int_noprefix(table_data, 0, 2); - /* IOMMU Feature Reporting */ - build_append_int_noprefix(table_data, - get_amd_ivhd_feature_report(extended_feature), - 4); + if (!ivhd_blob->len) { + /* + * Type 1 device entry reporting all devices + * These are 4-byte device entries currently reporting the range + * of Refer to Spec - Table 95:IVHD Device Entry Type Codes + * (4-byte) + */ + build_append_int_noprefix(ivhd_blob, 0x0000001, 4); + } - /* IVHD entries as found above */ - g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len); + /* + * When interrupt remapping is supported, we add a special IVHD device + * for type IO-APIC + * Refer to spec - Table 95: IVHD device entry type codes + * + * Linux IOMMU driver checks for the special IVHD device (type IO-APIC). + * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' + */ + if (x86ms->ioapic_iommu == x86_iommu) { + build_append_int_noprefix(ivhd_blob, + (0x1ull << 56) | /* type IOAPIC */ + (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */ + 0x48, /* special device */ + 8); + } - /* IVHD definition - type 11h */ - build_append_int_noprefix(table_data, 0x11, 1); - /* virtualization flags */ - build_append_int_noprefix(table_data, - (1UL << 0) | /* HtTunEn */ - (1UL << 4), /* iotblSup */ - 1); - - /* IVHD length */ - build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2); - - /* DeviceID */ - build_append_int_noprefix(table_data, iommu_devid, 2); - /* Capability offset */ - build_append_int_noprefix(table_data, s->pci->capab_offset, 2); - /* IOMMU base address */ - build_append_int_noprefix(table_data, s->mr_mmio.addr, 8); - /* PCI Segment Group */ - build_append_int_noprefix(table_data, 0, 2); - /* IOMMU info */ - build_append_int_noprefix(table_data, 0, 2); - /* IOMMU Attributes */ - if (!s->iommu.dma_translation) { - build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4); - } else { - build_append_int_noprefix(table_data, 0, 4); - } - /* EFR Register Image */ - build_append_int_noprefix(table_data, extended_feature, 8); - /* EFR Register Image 2 */ - build_append_int_noprefix(table_data, 0, 8); + /* IVHD definition - type 10h */ + build_append_int_noprefix(table_data, 0x10, 1); + /* virtualization flags */ + build_append_int_noprefix(table_data, + (1UL << 0) | /* HtTunEn */ + (1UL << 4) | /* iotblSup */ + (1UL << 6), /* PrefSup */ + 1); + + /* IVHD length */ + build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2); + /* DeviceID */ + build_append_int_noprefix(table_data, iommu_devid, 2); + /* Capability offset */ + build_append_int_noprefix(table_data, s->pci->capab_offset, 2); + /* IOMMU base address */ + build_append_int_noprefix(table_data, s->mr_mmio.addr, 8); + /* PCI Segment Group */ + build_append_int_noprefix(table_data, 0, 2); + /* IOMMU info */ + build_append_int_noprefix(table_data, 0, 2); + /* IOMMU Feature Reporting */ + build_append_int_noprefix(table_data, + get_amd_ivhd_feature_report(extended_feature), + 4); + + /* IVHD entries as found above */ + g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len); + + /* IVHD definition - type 11h */ + build_append_int_noprefix(table_data, 0x11, 1); + /* virtualization flags */ + build_append_int_noprefix(table_data, + (1UL << 0) | /* HtTunEn */ + (1UL << 4), /* iotblSup */ + 1); + + /* IVHD length */ + build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2); + + /* DeviceID */ + build_append_int_noprefix(table_data, iommu_devid, 2); + /* Capability offset */ + build_append_int_noprefix(table_data, s->pci->capab_offset, 2); + /* IOMMU base address */ + build_append_int_noprefix(table_data, s->mr_mmio.addr, 8); + /* PCI Segment Group */ + build_append_int_noprefix(table_data, 0, 2); + /* IOMMU info */ + build_append_int_noprefix(table_data, 0, 2); + /* IOMMU Attributes */ + if (!s->iommu.dma_translation) { + build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4); + } else { + build_append_int_noprefix(table_data, 0, 4); + } + /* EFR Register Image */ + build_append_int_noprefix(table_data, extended_feature, 8); + /* EFR Register Image 2 */ + build_append_int_noprefix(table_data, 0, 8); - /* IVHD entries as found above */ - g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len); + /* IVHD entries as found above */ + g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len); - g_array_free(ivhd_blob, TRUE); + g_array_free(ivhd_blob, TRUE); + } acpi_table_end(linker, &table); } diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c index 031ecbdb3de5..79d3fcf9fd9b 100644 --- a/hw/i386/amd_iommu.c +++ b/hw/i386/amd_iommu.c @@ -2596,10 +2596,6 @@ static void amdvi_pci_realize(PCIDevice *pdev, Error **errp) /* reset AMDVI specific capabilities, all r/o */ pci_set_long(pdev->config + s->capab_offset, AMDVI_CAPAB_FEATURES); - pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_BAR_LOW, - AMDVI_BASE_ADDR & MAKE_64BIT_MASK(14, 18)); - pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_BAR_HIGH, - AMDVI_BASE_ADDR >> 32); pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_RANGE, 0xff000000); pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_MISC, 0); @@ -2725,6 +2721,11 @@ out: return bus; } +static const VMStateDescription vmstate_amdvi_sysbus = { + .name = "amd-iommu", + .unmigratable = 1 +}; + static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) { DeviceClass *dc = (DeviceClass *) object_get_class(OBJECT(dev)); @@ -2734,8 +2735,22 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) X86MachineState *x86ms = X86_MACHINE(ms); X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); PCIBus *iommu_bus; + uint64_t base_addr; + uint8_t *pci_config; - if (s->pci_id) { + if (x86_iommu->index >= AMDVI_MAX_IOMMUS) { + /* + * Limit the maximum number of IOMMUs to 8 because of limited MMIO + * address. With more than 8 IOMMUs the MMIO address exceeds 0xfeda0000 + * and overlaps with smram-open-high. Keep the MMIO address space in + * range 0xfed80000-0xfed9ffff. + */ + error_setg(errp, "at most %d amd-iommu devices are supported", + AMDVI_MAX_IOMMUS); + return; + } + + if (s->pci_id && dc->vmsd != &vmstate_amdvi_sysbus) { PCIDevice *pdev = NULL; int ret = pci_qdev_find_device(s->pci_id, &pdev); @@ -2752,15 +2767,28 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) iommu_bus = pci_get_bus(pdev); s->pci = AMD_IOMMU_PCI(pdev); dc->vmsd = &vmstate_amdvi_sysbus_migratable; - } else { + } else if (!s->pci_id && dc->vmsd != &vmstate_amdvi_sysbus_migratable) { iommu_bus = pcms->pcibus; s->pci = AMD_IOMMU_PCI(object_new(TYPE_AMD_IOMMU_PCI)); + dc->vmsd = &vmstate_amdvi_sysbus; /* This device should take care of IOMMU PCI properties */ if (!qdev_realize(DEVICE(s->pci), &iommu_bus->qbus, errp)) { return; } + } else { + error_setg(errp, "Cannot mix amd-iommu devices with and without 'pci-id'"); + return; } + base_addr = AMDVI_GET_BASE_ADDR(x86_iommu->index); + + pci_config = s->pci->dev.config + s->pci->capab_offset; + + /* Set up PCI Capability base address */ + pci_set_long(pci_config + AMDVI_CAPAB_BAR_LOW, + base_addr & MAKE_64BIT_MASK(14, 18)); + pci_set_long(pci_config + AMDVI_CAPAB_BAR_HIGH, base_addr >> 32); + s->root_bus = get_root_bus(iommu_bus); s->iotlb = g_hash_table_new_full(amdvi_iotlb_hash, @@ -2772,7 +2800,7 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) /* set up MMIO */ memory_region_init_io(&s->mr_mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio", AMDVI_MMIO_SIZE); - memory_region_add_subregion(get_system_memory(), AMDVI_BASE_ADDR, + memory_region_add_subregion(get_system_memory(), base_addr, &s->mr_mmio); /* Create the share memory regions by all devices */ @@ -2796,6 +2824,14 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) * The linux kernel disables the intremap when it cannot find IOAPIC under * AMD IOMMU IVRS, hence create a ioapic_as with root bus even though AMD * IOMMU is not serving devices attached to root bus. + * + * The IOAPIC is not a PCI device, so it has no natural IOMMU. Bind it to + * the first AMD IOMMU, in command-line order, that has interrupt + * remapping enabled. That IOMMU translates and remaps IOAPIC interrupts + * using the IOAPIC's southbridge device ID (00:14.0), even if it serves a + * root bus other than pcie.0. The IVRS stays consistent, because only the + * IVHD of this IOMMU (x86ms->ioapic_iommu) carries the IOAPIC special + * device entry, and the guest driver follows that entry. */ if (!x86ms->ioapic_iommu && x86_iommu_ir_supported(X86_IOMMU_DEVICE(s))) { x86ms->ioapic_as = amdvi_host_dma_iommu(pcms->pcibus, s, @@ -2826,18 +2862,12 @@ static const Property amdvi_properties[] = { DEFINE_PROP_BOOL("dma-remap", AMDVIState, dma_remap, false), }; -static const VMStateDescription vmstate_amdvi_sysbus = { - .name = "amd-iommu", - .unmigratable = 1 -}; - static void amdvi_sysbus_class_init(ObjectClass *klass, const void *data) { DeviceClass *dc = DEVICE_CLASS(klass); X86IOMMUClass *dc_class = X86_IOMMU_DEVICE_CLASS(klass); device_class_set_legacy_reset(dc, amdvi_sysbus_reset); - dc->vmsd = &vmstate_amdvi_sysbus; dc->hotpluggable = false; dc_class->realize = amdvi_sysbus_realize; dc_class->int_remap = amdvi_int_remap; diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h index 008e882724c1..383605c38e6c 100644 --- a/hw/i386/amd_iommu.h +++ b/hw/i386/amd_iommu.h @@ -64,6 +64,7 @@ #define AMDVI_MMIO_PPR_TAIL 0x2038 #define AMDVI_MMIO_SIZE 0x4000 +#define AMDVI_MAX_IOMMUS 8 #define AMDVI_MMIO_DEVTAB_SIZE_MASK GENMASK64(8, 0) #define AMDVI_MMIO_DEVTAB_BASE_MASK GENMASK64(51, 12) @@ -239,6 +240,8 @@ /* AMDVI default address */ #define AMDVI_BASE_ADDR 0xfed80000ULL +#define AMDVI_GET_BASE_ADDR(index) \ + (AMDVI_BASE_ADDR + ((index) * AMDVI_MMIO_SIZE)) /* page management constants */ #define AMDVI_PAGE_SHIFT 12 -- 2.34.1