From nobody Thu Oct 30 22:49:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1752737524; cv=none; d=zohomail.com; s=zohoarc; b=hUmk6b3yZ0pu6/nc5Aq8TvauvOOeaDBnSryVaF1Qyj94gH22q0+PgDKqQQYli4LSDdWeQs4udb57CS9FRKWt4KZHb4LTGmwcKk6E6b1dLwhgQU7Jqi9b8FfKCURX9n6z1eXewjscdp98FMznHLtc9Xw1pd0Q9Fhc/AEFJoKCxqM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1752737524; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=/mG97VI6++S19ioTb3ZrpPqDS8XFyY6c2suBq0gR0Yw=; b=mEFyYZDzt62L+e8tjnYiglLf1LBuNqY6Tal2LVv4FGd2l43Bel6Ohdul1aO+zHb8kmgO67otErMTMeFcQkj2ivKisICfhbsW1R/vGRSYbtrKwF9EsrYwTMWPFxuoOg8vJ2NDVaGVodSyjxRiYqBmfHhQS46ksrhFUlZsxgRfNpI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1752737524435531.4923475065528; Thu, 17 Jul 2025 00:32:04 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1046273.1416587 (Exim 4.92) (envelope-from ) id 1ucJ5s-00048T-02; Thu, 17 Jul 2025 07:31:52 +0000 Received: by outflank-mailman (output) from mailman id 1046273.1416587; Thu, 17 Jul 2025 07:31:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ucJ5r-00048I-T7; Thu, 17 Jul 2025 07:31:51 +0000 Received: by outflank-mailman (input) for mailman id 1046273; Thu, 17 Jul 2025 07:31:49 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ucJ5p-0003tS-PU for xen-devel@lists.xenproject.org; Thu, 17 Jul 2025 07:31:49 +0000 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [2a00:1450:4864:20::329]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 197afc6a-62e0-11f0-a319-13f23c93f187; Thu, 17 Jul 2025 09:31:49 +0200 (CEST) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-4538bc52a8dso4613245e9.2 for ; Thu, 17 Jul 2025 00:31:48 -0700 (PDT) Received: from pc (cpc92320-cmbg19-2-0-cust1786.5-4.cable.virginm.net. [82.13.70.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b5e8dc201asm19792460f8f.22.2025.07.17.00.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jul 2025 00:31:46 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 197afc6a-62e0-11f0-a319-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752737508; x=1753342308; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/mG97VI6++S19ioTb3ZrpPqDS8XFyY6c2suBq0gR0Yw=; b=B0gx7HfnURiGWxuoM/LYKNmNtRFKFiDzPsGPUuhw7a84XpvEyV5mqlLNwUj1JwJwVj BO1ozJKKznfA17ShhmMUbqNqNQqe9HX4ixUlVSZZ97+g7OaVTG7s9/9w5Oqbrvh7Bnj5 Io6osd1Ii6ICu0FYZUBr9QccSY+dwYPGyo93VTLpafgOP21Ah5/XFrf9Y3Giduj2beuf DtRzPcyWN6/Iv5NpsvOm8J1orT7GlFxYn8LZAYc0mDu5NMA9+kfjWKeV8IP1dvXLgPQ6 ScgCL+JGjRF+BU9iAbcAo03SzC0cBVNcF+xMSgiNfOTGd+Go4IrOQNQxqn24kmtlVjUU J2QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752737508; x=1753342308; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/mG97VI6++S19ioTb3ZrpPqDS8XFyY6c2suBq0gR0Yw=; b=LTA4JqPCug+a7PtD4lC15FJnPUsKHGYYnG99JY8vKzaljMMt04FFyXMcFOewjncaM9 uRDDq15xxK70dovvA4nkqT21h+4WjdX50+HuNGecMiFgKFm9xUA3mizzKKqFHMoiRIKJ ik4C1z2aiXeW4GZCbUbmtn5CTroCfuW7VfYLZOHA9hJc1sqVXv9RLfqBDG68TCQ3v8U8 oIWsYIcz0q/ASlmZZZ3dLv8f15PQmRmFbaa56U64BRsH2aJxZZrYa3+hBEA+y7MADOhA jFQln1ZMLECqukM6z/bl2k+X4oi7knjp5ieG/9jDMKVYUGcMlf2QvYqoc5yetyI2OnPY qH2Q== X-Gm-Message-State: AOJu0Yz0r98F+Rn13HIN/rv3uyCmz5rnWkUXyput5ul6vBvYAuE05oa1 ZTpDzcWSkMR/v1qIDD/M/TCEOuuDGedFQ3dptawRiqdXjusNLRVBqo8lqDWh6zLu X-Gm-Gg: ASbGnctCMeEwyzV1gYf6GAw/pMegGBiIe6KYeWJcxjSgtU7dzD6xuoNhKXisjLevLYM repznGaNqwSJjDC83TQV+PgX/7m9IWqzbR/oQkEyj5F8GyU/54oVvXAwkTX+NT+ERIsPAcYPsxp ogsOa2rSI0TCEBX6vNSgENE9ahs3l0rgXrU80hBl6n/EN2hWAcexzqf96l+Mgc85QwwLzTR1BGb yj9yR2RsMxEpr4FLDx6e4qifXwnnLHT0A2rdoaFXJCRBRZSsjELXMHnUBOnJNBVuVXizLNhczpK 92arO2ytvqmo3c/Wl6+3qeuLe2MaCE9eyOAUUFTsyCaYclWgwFbiPWaCB8lii1fBpaSkpPcHwqp Uu9oJlMlxdr949SurZjJ2KMUx6q7OnWP+tMTxuwasmtgXBmTfVvK5/drFiYIClMGcqDWsHR0jJ6 v/oCaXlcE= X-Google-Smtp-Source: AGHT+IHqPVaztcsnHWztyQwVaWKtREQuOj10qkOavLQZm7vpDFqjqCJTQBM6pg4PU5dGPOVrAHmH7A== X-Received: by 2002:a05:600c:8b70:b0:43d:46de:b0eb with SMTP id 5b1f17b1804b1-4562e047122mr60841435e9.12.1752737507456; Thu, 17 Jul 2025 00:31:47 -0700 (PDT) From: Andrii Sultanov To: xen-devel@lists.xenproject.org Cc: Andrii Sultanov , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v5 1/3] drivers: Change amd_iommu struct to contain pci_sbdf_t, simplify code Date: Thu, 17 Jul 2025 08:31:25 +0100 Message-ID: <464e6ef4bf46ea962f1b4438fbb5be4d302a0d3d.1752736989.git.andriy.sultanov@vates.tech> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1752737525101116600 Content-Type: text/plain; charset="utf-8" Following on from 250d87dc3ff9 ("x86/msi: Change __msi_set_enable() to take pci_sbdf_t"), make struct amd_iommu contain pci_sbdf_t directly instead of specifying seg+bdf separately and regenerating sbdf_t from them, which simplifies code. Bloat-o-meter reports: add/remove: 0/0 grow/shrink: 4/13 up/down: 121/-377 (-256) Function old new delta _einittext 22028 22092 +64 amd_iommu_prepare 853 897 +44 __mon_lengths 2928 2936 +8 _invalidate_all_devices 133 138 +5 _hvm_dpci_msi_eoi 157 155 -2 build_info 752 744 -8 amd_iommu_add_device 856 844 -12 amd_iommu_msi_enable 33 20 -13 update_intremap_entry_from_msi_msg 879 859 -20 amd_iommu_msi_msg_update_ire 472 448 -24 send_iommu_command 251 224 -27 amd_iommu_get_supported_ivhd_type 86 54 -32 amd_iommu_detect_one_acpi 918 886 -32 iterate_ivrs_mappings 169 129 -40 flush_command_buffer 460 417 -43 set_iommu_interrupt_handler 421 377 -44 enable_iommu 1745 1665 -80 Resolves: https://gitlab.com/xen-project/xen/-/issues/198 Reported-by: Andrew Cooper Signed-off-by: Andrii Sultanov Acked-by: Jan Beulich Reviewed-by: Denis Mukhin =20 --- Changes in V5: * Simplified a comparison in find_iommu_from_bdf_cap * Changed get_iommu_*capabilities to take pci_sbdf_t Changes in V4: * Dropped references to the order of seg/bdf in the commit message * Dropped unnecessary detail from the commit message * Reverted to a macro usage in one case where it was mistakenly dropped * Folded several separate seg+bdf comparisons into a single one between sbdf_t, folded separate assignments with a macro. Changes in V3: * Dropped the union with seg+bdf/pci_sbdf_t to avoid aliasing, renamed all users appropriately Changes in V2: * Split single commit into several patches * Added the commit title of the referenced patch * Dropped brackets around &(iommu->sbdf) and &(sbdf) --- xen/drivers/passthrough/amd/iommu.h | 4 +-- xen/drivers/passthrough/amd/iommu_acpi.c | 16 +++++----- xen/drivers/passthrough/amd/iommu_cmd.c | 8 ++--- xen/drivers/passthrough/amd/iommu_detect.c | 27 ++++++++-------- xen/drivers/passthrough/amd/iommu_init.c | 35 ++++++++++----------- xen/drivers/passthrough/amd/iommu_intr.c | 29 ++++++++--------- xen/drivers/passthrough/amd/iommu_map.c | 4 +-- xen/drivers/passthrough/amd/pci_amd_iommu.c | 22 ++++++------- 8 files changed, 71 insertions(+), 74 deletions(-) diff --git a/xen/drivers/passthrough/amd/iommu.h b/xen/drivers/passthrough/= amd/iommu.h index 00e81b4b2a..ba541f7943 100644 --- a/xen/drivers/passthrough/amd/iommu.h +++ b/xen/drivers/passthrough/amd/iommu.h @@ -77,8 +77,8 @@ struct amd_iommu { struct list_head list; spinlock_t lock; /* protect iommu */ =20 - u16 seg; - u16 bdf; + pci_sbdf_t sbdf; + struct msi_desc msi; =20 u16 cap_offset; diff --git a/xen/drivers/passthrough/amd/iommu_acpi.c b/xen/drivers/passthr= ough/amd/iommu_acpi.c index 5bdbfb5ba8..04d6e365cb 100644 --- a/xen/drivers/passthrough/amd/iommu_acpi.c +++ b/xen/drivers/passthrough/amd/iommu_acpi.c @@ -58,7 +58,7 @@ static void __init add_ivrs_mapping_entry( uint16_t bdf, uint16_t alias_id, uint8_t flags, unsigned int ext_flags, bool alloc_irt, struct amd_iommu *iommu) { - struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(iommu->seg); + struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(iommu->sbdf.= seg); =20 ASSERT( ivrs_mappings !=3D NULL ); =20 @@ -70,7 +70,7 @@ static void __init add_ivrs_mapping_entry( ivrs_mappings[bdf].device_flags =3D flags; =20 /* Don't map an IOMMU by itself. */ - if ( iommu->bdf =3D=3D bdf ) + if ( iommu->sbdf.bdf =3D=3D bdf ) return; =20 /* Allocate interrupt remapping table if needed. */ @@ -96,7 +96,7 @@ static void __init add_ivrs_mapping_entry( =20 if ( !ivrs_mappings[alias_id].intremap_table ) panic("No memory for %pp's IRT\n", - &PCI_SBDF(iommu->seg, alias_id)); + &PCI_SBDF(iommu->sbdf.seg, alias_id)); } } =20 @@ -112,7 +112,7 @@ static struct amd_iommu * __init find_iommu_from_bdf_ca= p( struct amd_iommu *iommu; =20 for_each_amd_iommu ( iommu ) - if ( (iommu->seg =3D=3D seg) && (iommu->bdf =3D=3D bdf) && + if ( (iommu->sbdf.sbdf =3D=3D PCI_SBDF(seg, bdf).sbdf) && (iommu->cap_offset =3D=3D cap_offset) ) return iommu; =20 @@ -297,13 +297,13 @@ static int __init register_range_for_iommu_devices( /* reserve unity-mapped page entries for devices */ for ( bdf =3D rc =3D 0; !rc && bdf < ivrs_bdf_entries; bdf++ ) { - if ( iommu !=3D find_iommu_for_device(iommu->seg, bdf) ) + if ( iommu !=3D find_iommu_for_device(iommu->sbdf.seg, bdf) ) continue; =20 - req =3D get_ivrs_mappings(iommu->seg)[bdf].dte_requestor_id; - rc =3D reserve_unity_map_for_device(iommu->seg, bdf, base, length, + req =3D get_ivrs_mappings(iommu->sbdf.seg)[bdf].dte_requestor_id; + rc =3D reserve_unity_map_for_device(iommu->sbdf.seg, bdf, base, le= ngth, iw, ir, false) ?: - reserve_unity_map_for_device(iommu->seg, req, base, length, + reserve_unity_map_for_device(iommu->sbdf.seg, req, base, leng= th, iw, ir, false); } =20 diff --git a/xen/drivers/passthrough/amd/iommu_cmd.c b/xen/drivers/passthro= ugh/amd/iommu_cmd.c index 83c525b84f..eefd626161 100644 --- a/xen/drivers/passthrough/amd/iommu_cmd.c +++ b/xen/drivers/passthrough/amd/iommu_cmd.c @@ -40,7 +40,7 @@ static void send_iommu_command(struct amd_iommu *iommu, IOMMU_RING_BUFFER_PTR_MASK) ) { printk_once(XENLOG_ERR "AMD IOMMU %pp: no cmd slot available\n", - &PCI_SBDF(iommu->seg, iommu->bdf)); + &iommu->sbdf); cpu_relax(); } =20 @@ -85,7 +85,7 @@ static void flush_command_buffer(struct amd_iommu *iommu, threshold |=3D threshold << 1; printk(XENLOG_WARNING "AMD IOMMU %pp: %scompletion wait taking too long\n", - &PCI_SBDF(iommu->seg, iommu->bdf), + &iommu->sbdf, timeout_base ? "iotlb " : ""); timeout =3D 0; } @@ -95,7 +95,7 @@ static void flush_command_buffer(struct amd_iommu *iommu, if ( !timeout ) printk(XENLOG_WARNING "AMD IOMMU %pp: %scompletion wait took %lums\n", - &PCI_SBDF(iommu->seg, iommu->bdf), + &iommu->sbdf, timeout_base ? "iotlb " : "", (NOW() - start) / 10000000); } @@ -300,7 +300,7 @@ void amd_iommu_flush_iotlb(u8 devfn, const struct pci_d= ev *pdev, if ( !iommu_has_cap(iommu, PCI_CAP_IOTLB_SHIFT) ) return; =20 - req_id =3D get_dma_requestor_id(iommu->seg, PCI_BDF(pdev->bus, devfn)); + req_id =3D get_dma_requestor_id(iommu->sbdf.seg, PCI_BDF(pdev->bus, de= vfn)); queueid =3D req_id; maxpend =3D pdev->ats.queue_depth & 0xff; =20 diff --git a/xen/drivers/passthrough/amd/iommu_detect.c b/xen/drivers/passt= hrough/amd/iommu_detect.c index cede44e651..94ee297424 100644 --- a/xen/drivers/passthrough/amd/iommu_detect.c +++ b/xen/drivers/passthrough/amd/iommu_detect.c @@ -23,11 +23,11 @@ #include "iommu.h" =20 static int __init get_iommu_msi_capabilities( - u16 seg, u8 bus, u8 dev, u8 func, struct amd_iommu *iommu) + pci_sbdf_t sbdf, struct amd_iommu *iommu) { int pos; =20 - pos =3D pci_find_cap_offset(PCI_SBDF(seg, bus, dev, func), PCI_CAP_ID_= MSI); + pos =3D pci_find_cap_offset(sbdf, PCI_CAP_ID_MSI); =20 if ( !pos ) return -ENODEV; @@ -41,11 +41,11 @@ static int __init get_iommu_msi_capabilities( } =20 static int __init get_iommu_capabilities( - u16 seg, u8 bus, u8 dev, u8 func, u16 cap_ptr, struct amd_iommu *iommu) + pci_sbdf_t sbdf, u16 cap_ptr, struct amd_iommu *iommu) { u8 type; =20 - iommu->cap.header =3D pci_conf_read32(PCI_SBDF(seg, bus, dev, func), c= ap_ptr); + iommu->cap.header =3D pci_conf_read32(sbdf, cap_ptr); type =3D get_field_from_reg_u32(iommu->cap.header, PCI_CAP_TYPE_MASK, PCI_CAP_TYPE_SHIFT); =20 @@ -162,8 +162,8 @@ int __init amd_iommu_detect_one_acpi( spin_lock_init(&iommu->lock); INIT_LIST_HEAD(&iommu->ats_devices); =20 - iommu->seg =3D ivhd_block->pci_segment_group; - iommu->bdf =3D ivhd_block->header.device_id; + iommu->sbdf =3D PCI_SBDF(ivhd_block->pci_segment_group, + ivhd_block->header.device_id); iommu->cap_offset =3D ivhd_block->capability_offset; iommu->mmio_base_phys =3D ivhd_block->base_address; =20 @@ -210,16 +210,15 @@ int __init amd_iommu_detect_one_acpi( /* override IOMMU HT flags */ iommu->ht_flags =3D ivhd_block->header.flags; =20 - bus =3D PCI_BUS(iommu->bdf); - dev =3D PCI_SLOT(iommu->bdf); - func =3D PCI_FUNC(iommu->bdf); + bus =3D PCI_BUS(iommu->sbdf.bdf); + dev =3D PCI_SLOT(iommu->sbdf.bdf); + func =3D PCI_FUNC(iommu->sbdf.bdf); =20 - rt =3D get_iommu_capabilities(iommu->seg, bus, dev, func, - iommu->cap_offset, iommu); + rt =3D get_iommu_capabilities(iommu->sbdf, iommu->cap_offset, iommu); if ( rt ) goto out; =20 - rt =3D get_iommu_msi_capabilities(iommu->seg, bus, dev, func, iommu); + rt =3D get_iommu_msi_capabilities(iommu->sbdf, iommu); if ( rt ) goto out; =20 @@ -228,10 +227,10 @@ int __init amd_iommu_detect_one_acpi( if ( !iommu->domid_map ) goto out; =20 - rt =3D pci_ro_device(iommu->seg, bus, PCI_DEVFN(dev, func)); + rt =3D pci_ro_device(iommu->sbdf.seg, bus, PCI_DEVFN(dev, func)); if ( rt ) printk(XENLOG_ERR "Could not mark config space of %pp read-only (%= d)\n", - &PCI_SBDF(iommu->seg, iommu->bdf), rt); + &iommu->sbdf, rt); =20 list_add_tail(&iommu->list, &amd_iommu_head); rt =3D 0; diff --git a/xen/drivers/passthrough/amd/iommu_init.c b/xen/drivers/passthr= ough/amd/iommu_init.c index bb25b55c85..58d657060a 100644 --- a/xen/drivers/passthrough/amd/iommu_init.c +++ b/xen/drivers/passthrough/amd/iommu_init.c @@ -409,9 +409,7 @@ static void iommu_reset_log(struct amd_iommu *iommu, =20 static void amd_iommu_msi_enable(struct amd_iommu *iommu, int flag) { - pci_sbdf_t sbdf =3D { .seg =3D iommu->seg, .bdf =3D iommu->bdf }; - - __msi_set_enable(sbdf, iommu->msi.msi_attrib.pos, flag); + __msi_set_enable(iommu->sbdf, iommu->msi.msi_attrib.pos, flag); } =20 static void cf_check iommu_msi_unmask(struct irq_desc *desc) @@ -566,7 +564,7 @@ static void cf_check parse_event_log_entry(struct amd_i= ommu *iommu, u32 entry[]) =20 printk(XENLOG_ERR "AMD-Vi: %s: %pp d%u addr %016"PRIx64 " flags %#x%s%s%s%s%s%s%s%s%s%s\n", - code_str, &PCI_SBDF(iommu->seg, device_id), + code_str, &PCI_SBDF(iommu->sbdf.seg, device_id), domain_id, addr, flags, (flags & 0xe00) ? " ??" : "", (flags & 0x100) ? " TR" : "", @@ -583,8 +581,8 @@ static void cf_check parse_event_log_entry(struct amd_i= ommu *iommu, u32 entry[]) amd_iommu_print_entries(iommu, device_id, daddr_to_dfn(addr)); =20 for ( bdf =3D 0; bdf < ivrs_bdf_entries; bdf++ ) - if ( get_dma_requestor_id(iommu->seg, bdf) =3D=3D device_id ) - pci_check_disable_device(iommu->seg, PCI_BUS(bdf), + if ( get_dma_requestor_id(iommu->sbdf.seg, bdf) =3D=3D device_= id ) + pci_check_disable_device(iommu->sbdf.seg, PCI_BUS(bdf), PCI_DEVFN(bdf)); } else @@ -643,7 +641,7 @@ static void cf_check parse_ppr_log_entry(struct amd_iom= mu *iommu, u32 entry[]) struct pci_dev *pdev; =20 pcidevs_lock(); - pdev =3D pci_get_real_pdev(PCI_SBDF(iommu->seg, device_id)); + pdev =3D pci_get_real_pdev(PCI_SBDF(iommu->sbdf.seg, device_id)); pcidevs_unlock(); =20 if ( pdev ) @@ -752,12 +750,11 @@ static bool __init set_iommu_interrupt_handler(struct= amd_iommu *iommu) } =20 pcidevs_lock(); - iommu->msi.dev =3D pci_get_pdev(NULL, PCI_SBDF(iommu->seg, iommu->bdf)= ); + iommu->msi.dev =3D pci_get_pdev(NULL, iommu->sbdf); pcidevs_unlock(); if ( !iommu->msi.dev ) { - AMD_IOMMU_WARN("no pdev for %pp\n", - &PCI_SBDF(iommu->seg, iommu->bdf)); + AMD_IOMMU_WARN("no pdev for %pp\n", &iommu->sbdf); return 0; } =20 @@ -779,7 +776,7 @@ static bool __init set_iommu_interrupt_handler(struct a= md_iommu *iommu) hw_irq_controller *handler; u16 control; =20 - control =3D pci_conf_read16(PCI_SBDF(iommu->seg, iommu->bdf), + control =3D pci_conf_read16(iommu->sbdf, iommu->msi.msi_attrib.pos + PCI_MSI_FLAG= S); =20 iommu->msi.msi.nvec =3D 1; @@ -843,22 +840,22 @@ static void amd_iommu_erratum_746_workaround(struct a= md_iommu *iommu) (boot_cpu_data.x86_model > 0x1f) ) return; =20 - pci_conf_write32(PCI_SBDF(iommu->seg, iommu->bdf), 0xf0, 0x90); - value =3D pci_conf_read32(PCI_SBDF(iommu->seg, iommu->bdf), 0xf4); + pci_conf_write32(iommu->sbdf, 0xf0, 0x90); + value =3D pci_conf_read32(iommu->sbdf, 0xf4); =20 if ( value & (1 << 2) ) return; =20 /* Select NB indirect register 0x90 and enable writing */ - pci_conf_write32(PCI_SBDF(iommu->seg, iommu->bdf), 0xf0, 0x90 | (1 << = 8)); + pci_conf_write32(iommu->sbdf, 0xf0, 0x90 | (1 << 8)); =20 - pci_conf_write32(PCI_SBDF(iommu->seg, iommu->bdf), 0xf4, value | (1 <<= 2)); + pci_conf_write32(iommu->sbdf, 0xf4, value | (1 << 2)); printk(XENLOG_INFO "AMD-Vi: Applying erratum 746 workaround for IOMMU at %pp\n", - &PCI_SBDF(iommu->seg, iommu->bdf)); + &iommu->sbdf); =20 /* Clear the enable writing bit */ - pci_conf_write32(PCI_SBDF(iommu->seg, iommu->bdf), 0xf0, 0x90); + pci_conf_write32(iommu->sbdf, 0xf0, 0x90); } =20 static void enable_iommu(struct amd_iommu *iommu) @@ -1095,7 +1092,7 @@ static int __init amd_iommu_init_one(struct amd_iommu= *iommu, bool intr) goto error_out; =20 /* Make sure that the device table has been successfully allocated. */ - ivrs_mappings =3D get_ivrs_mappings(iommu->seg); + ivrs_mappings =3D get_ivrs_mappings(iommu->sbdf.seg); if ( !IVRS_MAPPINGS_DEVTAB(ivrs_mappings) ) goto error_out; =20 @@ -1363,7 +1360,7 @@ static bool __init amd_sp5100_erratum28(void) =20 static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) { - int rc =3D alloc_ivrs_mappings(iommu->seg); + int rc =3D alloc_ivrs_mappings(iommu->sbdf.seg); =20 if ( !rc ) rc =3D map_iommu_mmio_region(iommu); diff --git a/xen/drivers/passthrough/amd/iommu_intr.c b/xen/drivers/passthr= ough/amd/iommu_intr.c index 08766122b4..7ac07ba77e 100644 --- a/xen/drivers/passthrough/amd/iommu_intr.c +++ b/xen/drivers/passthrough/amd/iommu_intr.c @@ -132,7 +132,7 @@ static int get_intremap_requestor_id(int seg, int bdf) static unsigned int alloc_intremap_entry(const struct amd_iommu *iommu, unsigned int bdf, unsigned int nr) { - const struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(iommu-= >seg); + const struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(iommu-= >sbdf.seg); unsigned long *inuse =3D ivrs_mappings[bdf].intremap_inuse; unsigned int nr_ents =3D intremap_table_entries(ivrs_mappings[bdf].intremap_table, iommu); @@ -167,7 +167,7 @@ static union irte_ptr get_intremap_entry(const struct a= md_iommu *iommu, unsigned int bdf, unsigned int in= dex) { union irte_ptr table =3D { - .ptr =3D get_ivrs_mappings(iommu->seg)[bdf].intremap_table + .ptr =3D get_ivrs_mappings(iommu->sbdf.seg)[bdf].intremap_table }; =20 ASSERT(table.ptr && (index < intremap_table_entries(table.ptr, iommu))= ); @@ -184,7 +184,7 @@ static void free_intremap_entry(const struct amd_iommu = *iommu, unsigned int bdf, unsigned int index) { union irte_ptr entry =3D get_intremap_entry(iommu, bdf, index); - struct ivrs_mappings *ivrs =3D get_ivrs_mappings(iommu->seg); + struct ivrs_mappings *ivrs =3D get_ivrs_mappings(iommu->sbdf.seg); =20 if ( iommu->ctrl.ga_en ) { @@ -281,8 +281,8 @@ static int update_intremap_entry_from_ioapic( unsigned int dest, offset; bool fresh =3D false; =20 - req_id =3D get_intremap_requestor_id(iommu->seg, bdf); - lock =3D get_intremap_lock(iommu->seg, req_id); + req_id =3D get_intremap_requestor_id(iommu->sbdf.seg, bdf); + lock =3D get_intremap_lock(iommu->sbdf.seg, req_id); =20 delivery_mode =3D rte->delivery_mode; vector =3D rte->vector; @@ -419,10 +419,10 @@ static int update_intremap_entry_from_msi_msg( unsigned int dest, offset, i; bool fresh =3D false; =20 - req_id =3D get_dma_requestor_id(iommu->seg, bdf); - alias_id =3D get_intremap_requestor_id(iommu->seg, bdf); + req_id =3D get_dma_requestor_id(iommu->sbdf.seg, bdf); + alias_id =3D get_intremap_requestor_id(iommu->sbdf.seg, bdf); =20 - lock =3D get_intremap_lock(iommu->seg, req_id); + lock =3D get_intremap_lock(iommu->sbdf.seg, req_id); spin_lock_irqsave(lock, flags); =20 if ( msg =3D=3D NULL ) @@ -486,10 +486,10 @@ static int update_intremap_entry_from_msi_msg( */ =20 if ( ( req_id !=3D alias_id ) && - get_ivrs_mappings(iommu->seg)[alias_id].intremap_table !=3D NULL ) + get_ivrs_mappings(iommu->sbdf.seg)[alias_id].intremap_table !=3D = NULL ) { - BUG_ON(get_ivrs_mappings(iommu->seg)[req_id].intremap_table !=3D - get_ivrs_mappings(iommu->seg)[alias_id].intremap_table); + BUG_ON(get_ivrs_mappings(iommu->sbdf.seg)[req_id].intremap_table != =3D + get_ivrs_mappings(iommu->sbdf.seg)[alias_id].intremap_table= ); } =20 return fresh; @@ -498,16 +498,17 @@ static int update_intremap_entry_from_msi_msg( static struct amd_iommu *_find_iommu_for_device(int seg, int bdf) { struct amd_iommu *iommu; + pci_sbdf_t sbdf =3D PCI_SBDF(seg, bdf); =20 for_each_amd_iommu ( iommu ) - if ( iommu->seg =3D=3D seg && iommu->bdf =3D=3D bdf ) + if ( iommu->sbdf.sbdf =3D=3D sbdf.sbdf ) return NULL; =20 iommu =3D find_iommu_for_device(seg, bdf); if ( iommu ) return iommu; =20 - AMD_IOMMU_DEBUG("No IOMMU for MSI dev =3D %pp\n", &PCI_SBDF(seg, bdf)); + AMD_IOMMU_DEBUG("No IOMMU for MSI dev =3D %pp\n", &sbdf); return ERR_PTR(-EINVAL); } =20 @@ -730,7 +731,7 @@ static void dump_intremap_table(const struct amd_iommu = *iommu, if ( ivrs_mapping ) { printk(" %pp:\n", - &PCI_SBDF(iommu->seg, ivrs_mapping->dte_requestor_id)); + &PCI_SBDF(iommu->sbdf.seg, ivrs_mapping->dte_requestor_= id)); ivrs_mapping =3D NULL; } =20 diff --git a/xen/drivers/passthrough/amd/iommu_map.c b/xen/drivers/passthro= ugh/amd/iommu_map.c index dde393645a..d28c475650 100644 --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -558,14 +558,14 @@ void amd_iommu_print_entries(const struct amd_iommu *= iommu, unsigned int dev_id, =20 if ( !dt[dev_id].tv ) { - printk("%pp: no root\n", &PCI_SBDF(iommu->seg, dev_id)); + printk("%pp: no root\n", &PCI_SBDF(iommu->sbdf.seg, dev_id)); return; } =20 pt_mfn =3D _mfn(dt[dev_id].pt_root); level =3D dt[dev_id].paging_mode; printk("%pp root @ %"PRI_mfn" (%u levels) dfn=3D%"PRI_dfn"\n", - &PCI_SBDF(iommu->seg, dev_id), mfn_x(pt_mfn), level, dfn_x(dfn)= ); + &PCI_SBDF(iommu->sbdf.seg, dev_id), mfn_x(pt_mfn), level, dfn_x= (dfn)); =20 while ( level ) { diff --git a/xen/drivers/passthrough/amd/pci_amd_iommu.c b/xen/drivers/pass= through/amd/pci_amd_iommu.c index d00697edb3..6b58e3380b 100644 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -43,7 +43,7 @@ struct amd_iommu *find_iommu_for_device(int seg, int bdf) { unsigned int bd0 =3D bdf & ~PCI_FUNC(~0); =20 - if ( ivrs_mappings[bd0].iommu && ivrs_mappings[bd0].iommu->bdf != =3D bdf ) + if ( ivrs_mappings[bd0].iommu && ivrs_mappings[bd0].iommu->sbdf.bd= f !=3D bdf ) { struct ivrs_mappings tmp =3D ivrs_mappings[bd0]; =20 @@ -121,7 +121,7 @@ static bool use_ats( { return !ivrs_dev->block_ats && iommu_has_cap(iommu, PCI_CAP_IOTLB_SHIFT) && - pci_ats_device(iommu->seg, pdev->bus, pdev->devfn); + pci_ats_device(iommu->sbdf.seg, pdev->bus, pdev->devfn); } =20 static int __must_check amd_iommu_setup_domain_device( @@ -147,17 +147,17 @@ static int __must_check amd_iommu_setup_domain_device( if ( rc ) return rc; =20 - req_id =3D get_dma_requestor_id(iommu->seg, pdev->sbdf.bdf); - ivrs_dev =3D &get_ivrs_mappings(iommu->seg)[req_id]; + req_id =3D get_dma_requestor_id(iommu->sbdf.seg, pdev->sbdf.bdf); + ivrs_dev =3D &get_ivrs_mappings(iommu->sbdf.seg)[req_id]; sr_flags =3D (iommu_hwdom_passthrough && is_hardware_domain(domain) ? 0 : SET_ROOT_VALID) | (ivrs_dev->unity_map ? SET_ROOT_WITH_UNITY_MAP : 0); =20 /* get device-table entry */ - req_id =3D get_dma_requestor_id(iommu->seg, PCI_BDF(bus, devfn)); + req_id =3D get_dma_requestor_id(iommu->sbdf.seg, PCI_BDF(bus, devfn)); table =3D iommu->dev_table.buffer; dte =3D &table[req_id]; - ivrs_dev =3D &get_ivrs_mappings(iommu->seg)[req_id]; + ivrs_dev =3D &get_ivrs_mappings(iommu->sbdf.seg)[req_id]; =20 if ( domain !=3D dom_io ) { @@ -275,7 +275,7 @@ static int __must_check amd_iommu_setup_domain_device( ASSERT(pcidevs_locked()); =20 if ( use_ats(pdev, iommu, ivrs_dev) && - !pci_ats_enabled(iommu->seg, bus, pdev->devfn) ) + !pci_ats_enabled(iommu->sbdf.seg, bus, pdev->devfn) ) { if ( devfn =3D=3D pdev->devfn ) enable_ats_device(pdev, &iommu->ats_devices); @@ -418,12 +418,12 @@ static void amd_iommu_disable_domain_device(const str= uct domain *domain, =20 ASSERT(pcidevs_locked()); =20 - if ( pci_ats_device(iommu->seg, bus, pdev->devfn) && - pci_ats_enabled(iommu->seg, bus, pdev->devfn) ) + if ( pci_ats_device(iommu->sbdf.seg, bus, pdev->devfn) && + pci_ats_enabled(iommu->sbdf.seg, bus, pdev->devfn) ) disable_ats_device(pdev); =20 BUG_ON ( iommu->dev_table.buffer =3D=3D NULL ); - req_id =3D get_dma_requestor_id(iommu->seg, PCI_BDF(bus, devfn)); + req_id =3D get_dma_requestor_id(iommu->sbdf.seg, PCI_BDF(bus, devfn)); table =3D iommu->dev_table.buffer; dte =3D &table[req_id]; =20 @@ -578,7 +578,7 @@ static int cf_check amd_iommu_add_device(u8 devfn, stru= ct pci_dev *pdev) return -EINVAL; =20 for_each_amd_iommu(iommu) - if ( pdev->seg =3D=3D iommu->seg && pdev->sbdf.bdf =3D=3D iommu->b= df ) + if ( pdev->sbdf.sbdf =3D=3D iommu->sbdf.sbdf ) return is_hardware_domain(pdev->domain) ? 0 : -ENODEV; =20 iommu =3D find_iommu_for_device(pdev->seg, pdev->sbdf.bdf); --=20 2.49.0 From nobody Thu Oct 30 22:49:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1752737530; cv=none; d=zohomail.com; s=zohoarc; b=FCmVrCAacSOP6LlNpGs3Tw+OCsq3A/XYkRwlySojzferzdSByr3t4u3NPn9LJt5JoWTPsI+pJ9LMrqZtS5h8uN0+ZUkE+sYKNgeDKb55o6arBeEsAjKJmDwuBZbuZ611/qWVcnlVwj+vd+i5hAwZSPfgAKHPBDrcJFYhsCx8Tko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1752737530; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fcfoiUYIVjl/D0Ywq9KbeNKGyLzp30Ote+u9ohhrmSU=; b=kyTAzefbb9Ro99bA5M8AzHgkMyamiz//77kT4zPpKDtIYcc/xUdTtcQIgT1MBIneYEaVM+nWc3IauX1Jv6T1olUROsBRszSFEb5hm6KZKtAEtIamiLocLuTjG7ZOnu5S2qytDnF27JetUjc/UZGTz5tKyLqXlG3ySTDVEXmwCoc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1752737530209426.6640577848632; Thu, 17 Jul 2025 00:32:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1046274.1416597 (Exim 4.92) (envelope-from ) id 1ucJ5v-0004Oq-Al; Thu, 17 Jul 2025 07:31:55 +0000 Received: by outflank-mailman (output) from mailman id 1046274.1416597; Thu, 17 Jul 2025 07:31:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ucJ5v-0004Oh-7j; Thu, 17 Jul 2025 07:31:55 +0000 Received: by outflank-mailman (input) for mailman id 1046274; Thu, 17 Jul 2025 07:31:53 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ucJ5t-0004NF-PZ for xen-devel@lists.xenproject.org; Thu, 17 Jul 2025 07:31:53 +0000 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [2a00:1450:4864:20::331]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 1afde8ba-62e0-11f0-b894-0df219b8e170; Thu, 17 Jul 2025 09:31:51 +0200 (CEST) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-45619d70c72so14397205e9.0 for ; Thu, 17 Jul 2025 00:31:51 -0700 (PDT) Received: from pc (cpc92320-cmbg19-2-0-cust1786.5-4.cable.virginm.net. [82.13.70.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b5e8dc201asm19792460f8f.22.2025.07.17.00.31.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jul 2025 00:31:49 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 1afde8ba-62e0-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752737511; x=1753342311; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fcfoiUYIVjl/D0Ywq9KbeNKGyLzp30Ote+u9ohhrmSU=; b=E+vA807oRtyFKU834ZuPIe+suj7w935NdWOSFresbm9secVe0E+KB4xawElTsIbn6g FRGHrU5YBCbSbUdZpONG16P+9BZLO1Ly5OWrMu7Uc+sCwDgrXh7Wk8tqX3rXvyGKIeIK hAukzdQxoEQMNLvQGbCZc5W6nEHE4l+/ScbahH7Z9sFSOL9KCI+qXQhHOTXbbQFWpTrd +Oy42rk5IWkXY1z5L/TQMw+vHrEbwgrva/CYGM7sjSS7zMQbd33gXrcQtIKE5bBRPaIX 0LXXy6ztrSCRWie64Nm3Y1T+O+ytV45NFScoUIfKCvNGs4ugO/JUUKxnHNmeppWNTaaC LWYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752737511; x=1753342311; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fcfoiUYIVjl/D0Ywq9KbeNKGyLzp30Ote+u9ohhrmSU=; b=l4I0g5fGfJa/AjrwIeTyJ/aLDlMonC3+Xu80Y78bULdT0B7j2zg9gCGMhyGc7ioIqg wQM4mYciQ/SjDxS+G79L/ImX/oJFTOZGCVRCOJ37GPQFVNNKHLw/Y76DPrT/mQ05hm78 IcXCnPqFhTAYvQStIhGImGXDdlL711dUnMfFEMI1IBJgkpfLAXl1GPa8Zo/7qT2lURg7 CxItnF41HaMMEjkOWgDUsdFFXHeQyn11w6GmiohZnP3lw8hyFCH+nRA5+H+uykRTXUND XfAHGsCHsaT2gERY6X3UBIaXSJhUIyd1pkfmol59Q+KkBYihYXumWxVU9efVcHpZD/H1 NBTg== X-Gm-Message-State: AOJu0YxbpU6XWUYmYXvoKZboHkq8hdpCUyO/568UmlbKdjTAWo3hZniv oEb8mSb1VRdd83vMYBqmXwEDzoIRw1VZ2rZTLSC0VOrsUWsYsKgRjGxPSKaHdkgk X-Gm-Gg: ASbGnctuMH2xSNZI3oG9bXIuuIKzWuCjZTTatHkf+E/GrvJ4EuP/7Gq/cZVBAYpIttI dZzlZFZ4/tR3Ugh01lQ9YdmilNm+1GS/Ap7o8NfxxmWTQpl7Ph79vp/QKXnm6yYELMskJXW4OX3 lSJObp7M1ed1dOZ/6s/i67mz7VM9ry4Wz3TBMDakK8wEDEo4q9Kul3CZCKQDPGW1v8nilyrK8w0 2yLNCE0R3akZNUpPZ+QbuiDNdrY0vfAchPAj2Ujj0NTiO7Ih9NA87SReDI1E3czDgN23oyKuGg+ 7MUOyRy/GXHE0aeEpsxyREon5fw+PaALQsnDcheD1nK3+j2c7iMfAoX7+XQMeNmgjdj35C+eKDb khDqRuGucURDtodUnZAsQv/wdH9he1H9IRk9a4omp1XjDbVjx0q1kehCSbn19dgbEbyLr0dIk X-Google-Smtp-Source: AGHT+IF3hlqeQXuWHBqiWGN3+xcZxUMYETb8myDtKsSXMDI+JdEAQVruwFYaqQ1uPpLUkAW1uMBpEw== X-Received: by 2002:a5d:64c4:0:b0:3a4:e231:8632 with SMTP id ffacd0b85a97d-3b613abe2c2mr1896604f8f.12.1752737510249; Thu, 17 Jul 2025 00:31:50 -0700 (PDT) From: Andrii Sultanov To: xen-devel@lists.xenproject.org Cc: Andrii Sultanov , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Denis Mukhin Subject: [PATCH v5 2/3] drivers: Change find_iommu_for_device function to take pci_sbdf_t, simplify code Date: Thu, 17 Jul 2025 08:31:26 +0100 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1752737531250116600 Content-Type: text/plain; charset="utf-8" Following a similar change to amd_iommu struct, change the find_iommu_for_device function to take pci_sbdf_t as a single parameter. This removes conversions in the majority of cases. Bloat-o-meter reports (on top of the first patch in the series): add/remove: 0/0 grow/shrink: 12/11 up/down: 95/-95 (0) Function old new delta amd_iommu_get_supported_ivhd_type 54 86 +32 parse_ivrs_table 3955 3966 +11 amd_iommu_assign_device 271 282 +11 __mon_lengths 2928 2936 +8 update_intremap_entry_from_msi_msg 859 864 +5 iov_supports_xt 270 275 +5 amd_setup_hpet_msi 232 237 +5 amd_iommu_domain_destroy 46 51 +5 _hvm_dpci_msi_eoi 155 160 +5 find_iommu_for_device 242 246 +4 amd_iommu_ioapic_update_ire 572 575 +3 allocate_domain_resources 82 83 +1 amd_iommu_read_ioapic_from_ire 347 344 -3 reassign_device 843 838 -5 amd_iommu_remove_device 352 347 -5 amd_iommu_get_reserved_device_memory 521 516 -5 amd_iommu_flush_iotlb 359 354 -5 amd_iommu_add_device 844 839 -5 amd_iommu_setup_domain_device 1478 1472 -6 build_info 752 744 -8 amd_iommu_detect_one_acpi 886 876 -10 register_range_for_device 297 281 -16 parse_ivmd_block 1339 1312 -27 Signed-off-by: Andrii Sultanov Acked-by: Jan Beulich Reviewed-by: Denis Mukhin --- Changes in V5: * Added a Reviewed-by tag Changes in V4: * After amendments to the previous commit which increased improvements there, this commit now does not improve code size anymore (but still simplifies code), so I've updated the bloat-o-meter report. Changes in V3: * Amended commit message * As the previous patch dropped the aliasing of seg and bdf, renamed users = of amd_iommu as appropriate. Changes in V2: * Split single commit into several patches * Dropped brackets around &(iommu->sbdf) and &(sbdf) * Dropped most of the hunk in _invalidate_all_devices - it was bloat-equivalent to the existing code - just convert with PCI_SBDF instead * Dropped the hunk in get_intremap_requestor_id (iommu_intr.c) and amd_iommu_get_reserved_device_memory (iommu_map.c) as they were only increasing the code size. * Kept "/* XXX */" where appropriate * Fixed a slip-up in register_range_for_iommu_devices where iommu->sbdf replaced the usage of *different* seg and bdf. --- xen/drivers/passthrough/amd/iommu.h | 2 +- xen/drivers/passthrough/amd/iommu_acpi.c | 14 +++++----- xen/drivers/passthrough/amd/iommu_cmd.c | 2 +- xen/drivers/passthrough/amd/iommu_init.c | 4 +-- xen/drivers/passthrough/amd/iommu_intr.c | 17 ++++++------ xen/drivers/passthrough/amd/iommu_map.c | 2 +- xen/drivers/passthrough/amd/pci_amd_iommu.c | 30 ++++++++++----------- 7 files changed, 35 insertions(+), 36 deletions(-) diff --git a/xen/drivers/passthrough/amd/iommu.h b/xen/drivers/passthrough/= amd/iommu.h index ba541f7943..2599800e6a 100644 --- a/xen/drivers/passthrough/amd/iommu.h +++ b/xen/drivers/passthrough/amd/iommu.h @@ -240,7 +240,7 @@ void amd_iommu_flush_intremap(struct amd_iommu *iommu, = uint16_t bdf); void amd_iommu_flush_all_caches(struct amd_iommu *iommu); =20 /* find iommu for bdf */ -struct amd_iommu *find_iommu_for_device(int seg, int bdf); +struct amd_iommu *find_iommu_for_device(pci_sbdf_t sbdf); =20 /* interrupt remapping */ bool cf_check iov_supports_xt(void); diff --git a/xen/drivers/passthrough/amd/iommu_acpi.c b/xen/drivers/passthr= ough/amd/iommu_acpi.c index 04d6e365cb..a9c5432e86 100644 --- a/xen/drivers/passthrough/amd/iommu_acpi.c +++ b/xen/drivers/passthrough/amd/iommu_acpi.c @@ -239,17 +239,17 @@ static int __init register_range_for_device( unsigned int bdf, paddr_t base, paddr_t limit, bool iw, bool ir, bool exclusion) { - int seg =3D 0; /* XXX */ - struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(seg); + pci_sbdf_t sbdf =3D { .seg =3D 0 /* XXX */, .bdf =3D bdf }; + struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(sbdf.seg); struct amd_iommu *iommu; u16 req; int rc =3D 0; =20 - iommu =3D find_iommu_for_device(seg, bdf); + iommu =3D find_iommu_for_device(sbdf); if ( !iommu ) { AMD_IOMMU_WARN("IVMD: no IOMMU for device %pp - ignoring constrain= \n", - &PCI_SBDF(seg, bdf)); + &sbdf); return 0; } req =3D ivrs_mappings[bdf].dte_requestor_id; @@ -263,9 +263,9 @@ static int __init register_range_for_device( paddr_t length =3D limit + PAGE_SIZE - base; =20 /* reserve unity-mapped page entries for device */ - rc =3D reserve_unity_map_for_device(seg, bdf, base, length, iw, ir, + rc =3D reserve_unity_map_for_device(sbdf.seg, bdf, base, length, i= w, ir, false) ?: - reserve_unity_map_for_device(seg, req, base, length, iw, ir, + reserve_unity_map_for_device(sbdf.seg, req, base, length, iw,= ir, false); } else @@ -297,7 +297,7 @@ static int __init register_range_for_iommu_devices( /* reserve unity-mapped page entries for devices */ for ( bdf =3D rc =3D 0; !rc && bdf < ivrs_bdf_entries; bdf++ ) { - if ( iommu !=3D find_iommu_for_device(iommu->sbdf.seg, bdf) ) + if ( iommu !=3D find_iommu_for_device(PCI_SBDF(iommu->sbdf.seg, bd= f)) ) continue; =20 req =3D get_ivrs_mappings(iommu->sbdf.seg)[bdf].dte_requestor_id; diff --git a/xen/drivers/passthrough/amd/iommu_cmd.c b/xen/drivers/passthro= ugh/amd/iommu_cmd.c index eefd626161..6b80c57f44 100644 --- a/xen/drivers/passthrough/amd/iommu_cmd.c +++ b/xen/drivers/passthrough/amd/iommu_cmd.c @@ -288,7 +288,7 @@ void amd_iommu_flush_iotlb(u8 devfn, const struct pci_d= ev *pdev, if ( !pci_ats_enabled(pdev->seg, pdev->bus, pdev->devfn) ) return; =20 - iommu =3D find_iommu_for_device(pdev->seg, pdev->sbdf.bdf); + iommu =3D find_iommu_for_device(pdev->sbdf); =20 if ( !iommu ) { diff --git a/xen/drivers/passthrough/amd/iommu_init.c b/xen/drivers/passthr= ough/amd/iommu_init.c index 58d657060a..3f6d2f5db5 100644 --- a/xen/drivers/passthrough/amd/iommu_init.c +++ b/xen/drivers/passthrough/amd/iommu_init.c @@ -1540,13 +1540,13 @@ static void invalidate_all_domain_pages(void) static int cf_check _invalidate_all_devices( u16 seg, struct ivrs_mappings *ivrs_mappings) { - unsigned int bdf;=20 + unsigned int bdf; u16 req_id; struct amd_iommu *iommu; =20 for ( bdf =3D 0; bdf < ivrs_bdf_entries; bdf++ ) { - iommu =3D find_iommu_for_device(seg, bdf); + iommu =3D find_iommu_for_device(PCI_SBDF(seg, bdf)); req_id =3D ivrs_mappings[bdf].dte_requestor_id; if ( iommu ) { diff --git a/xen/drivers/passthrough/amd/iommu_intr.c b/xen/drivers/passthr= ough/amd/iommu_intr.c index 7ac07ba77e..25bf25904e 100644 --- a/xen/drivers/passthrough/amd/iommu_intr.c +++ b/xen/drivers/passthrough/amd/iommu_intr.c @@ -337,7 +337,7 @@ void cf_check amd_iommu_ioapic_update_ire( /* get device id of ioapic devices */ bdf =3D ioapic_sbdf[idx].bdf; seg =3D ioapic_sbdf[idx].seg; - iommu =3D find_iommu_for_device(seg, bdf); + iommu =3D find_iommu_for_device(PCI_SBDF(seg, bdf)); if ( !iommu ) { AMD_IOMMU_WARN("failed to find IOMMU for IO-APIC @ %04x:%04x\n", @@ -383,7 +383,7 @@ unsigned int cf_check amd_iommu_read_ioapic_from_ire( =20 seg =3D ioapic_sbdf[idx].seg; bdf =3D ioapic_sbdf[idx].bdf; - iommu =3D find_iommu_for_device(seg, bdf); + iommu =3D find_iommu_for_device(PCI_SBDF(seg, bdf)); if ( !iommu ) return val; req_id =3D get_intremap_requestor_id(seg, bdf); @@ -495,16 +495,15 @@ static int update_intremap_entry_from_msi_msg( return fresh; } =20 -static struct amd_iommu *_find_iommu_for_device(int seg, int bdf) +static struct amd_iommu *_find_iommu_for_device(pci_sbdf_t sbdf) { struct amd_iommu *iommu; - pci_sbdf_t sbdf =3D PCI_SBDF(seg, bdf); =20 for_each_amd_iommu ( iommu ) if ( iommu->sbdf.sbdf =3D=3D sbdf.sbdf ) return NULL; =20 - iommu =3D find_iommu_for_device(seg, bdf); + iommu =3D find_iommu_for_device(sbdf); if ( iommu ) return iommu; =20 @@ -524,7 +523,7 @@ int cf_check amd_iommu_msi_msg_update_ire( bdf =3D pdev ? pdev->sbdf.bdf : hpet_sbdf.bdf; seg =3D pdev ? pdev->seg : hpet_sbdf.seg; =20 - iommu =3D _find_iommu_for_device(seg, bdf); + iommu =3D _find_iommu_for_device(PCI_SBDF(seg, bdf)); if ( IS_ERR_OR_NULL(iommu) ) return PTR_ERR(iommu); =20 @@ -661,8 +660,8 @@ bool __init cf_check iov_supports_xt(void) if ( idx =3D=3D MAX_IO_APICS ) return false; =20 - if ( !find_iommu_for_device(ioapic_sbdf[idx].seg, - ioapic_sbdf[idx].bdf) ) + if ( !find_iommu_for_device(PCI_SBDF(ioapic_sbdf[idx].seg, + ioapic_sbdf[idx].bdf)) ) { AMD_IOMMU_WARN("no IOMMU for IO-APIC %#x (ID %x)\n", apic, IO_APIC_ID(apic)); @@ -691,7 +690,7 @@ int __init cf_check amd_setup_hpet_msi(struct msi_desc = *msi_desc) return -ENODEV; } =20 - iommu =3D find_iommu_for_device(hpet_sbdf.seg, hpet_sbdf.bdf); + iommu =3D find_iommu_for_device(PCI_SBDF(hpet_sbdf.seg, hpet_sbdf.bdf)= ); if ( !iommu ) return -ENXIO; =20 diff --git a/xen/drivers/passthrough/amd/iommu_map.c b/xen/drivers/passthro= ugh/amd/iommu_map.c index d28c475650..320a2dc64c 100644 --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -717,7 +717,7 @@ int cf_check amd_iommu_get_reserved_device_memory( pcidevs_unlock(); =20 if ( pdev ) - iommu =3D find_iommu_for_device(seg, bdf); + iommu =3D find_iommu_for_device(sbdf); if ( !iommu ) continue; } diff --git a/xen/drivers/passthrough/amd/pci_amd_iommu.c b/xen/drivers/pass= through/amd/pci_amd_iommu.c index 6b58e3380b..3a14770855 100644 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -32,35 +32,35 @@ static bool __read_mostly init_done; =20 static const struct iommu_init_ops _iommu_init_ops; =20 -struct amd_iommu *find_iommu_for_device(int seg, int bdf) +struct amd_iommu *find_iommu_for_device(pci_sbdf_t sbdf) { - struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(seg); + struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(sbdf.seg); =20 - if ( !ivrs_mappings || bdf >=3D ivrs_bdf_entries ) + if ( !ivrs_mappings || sbdf.bdf >=3D ivrs_bdf_entries ) return NULL; =20 - if ( unlikely(!ivrs_mappings[bdf].iommu) && likely(init_done) ) + if ( unlikely(!ivrs_mappings[sbdf.bdf].iommu) && likely(init_done) ) { - unsigned int bd0 =3D bdf & ~PCI_FUNC(~0); + unsigned int bd0 =3D sbdf.bdf & ~PCI_FUNC(~0); =20 - if ( ivrs_mappings[bd0].iommu && ivrs_mappings[bd0].iommu->sbdf.bd= f !=3D bdf ) + if ( ivrs_mappings[bd0].iommu && ivrs_mappings[bd0].iommu->sbdf.bd= f !=3D sbdf.bdf ) { struct ivrs_mappings tmp =3D ivrs_mappings[bd0]; =20 tmp.iommu =3D NULL; if ( tmp.dte_requestor_id =3D=3D bd0 ) - tmp.dte_requestor_id =3D bdf; - ivrs_mappings[bdf] =3D tmp; + tmp.dte_requestor_id =3D sbdf.bdf; + ivrs_mappings[sbdf.bdf] =3D tmp; =20 printk(XENLOG_WARNING "%pp not found in ACPI tables;" - " using same IOMMU as function 0\n", &PCI_SBDF(seg, bdf= )); + " using same IOMMU as function 0\n", &sbdf); =20 /* write iommu field last */ - ivrs_mappings[bdf].iommu =3D ivrs_mappings[bd0].iommu; + ivrs_mappings[sbdf.bdf].iommu =3D ivrs_mappings[bd0].iommu; } } =20 - return ivrs_mappings[bdf].iommu; + return ivrs_mappings[sbdf.bdf].iommu; } =20 /* @@ -107,7 +107,7 @@ static bool any_pdev_behind_iommu(const struct domain *= d, if ( pdev =3D=3D exclude ) continue; =20 - if ( find_iommu_for_device(pdev->seg, pdev->sbdf.bdf) =3D=3D iommu= ) + if ( find_iommu_for_device(pdev->sbdf) =3D=3D iommu ) return true; } =20 @@ -468,7 +468,7 @@ static int cf_check reassign_device( struct amd_iommu *iommu; int rc; =20 - iommu =3D find_iommu_for_device(pdev->seg, pdev->sbdf.bdf); + iommu =3D find_iommu_for_device(pdev->sbdf); if ( !iommu ) { AMD_IOMMU_WARN("failed to find IOMMU: %pp cannot be assigned to %p= d\n", @@ -581,7 +581,7 @@ static int cf_check amd_iommu_add_device(u8 devfn, stru= ct pci_dev *pdev) if ( pdev->sbdf.sbdf =3D=3D iommu->sbdf.sbdf ) return is_hardware_domain(pdev->domain) ? 0 : -ENODEV; =20 - iommu =3D find_iommu_for_device(pdev->seg, pdev->sbdf.bdf); + iommu =3D find_iommu_for_device(pdev->sbdf); if ( unlikely(!iommu) ) { /* Filter bridge devices. */ @@ -666,7 +666,7 @@ static int cf_check amd_iommu_remove_device(u8 devfn, s= truct pci_dev *pdev) if ( !pdev->domain ) return -EINVAL; =20 - iommu =3D find_iommu_for_device(pdev->seg, pdev->sbdf.bdf); + iommu =3D find_iommu_for_device(pdev->sbdf); if ( !iommu ) { AMD_IOMMU_WARN("failed to find IOMMU: %pp cannot be removed from %= pd\n", --=20 2.49.0 From nobody Thu Oct 30 22:49:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1752737532; cv=none; d=zohomail.com; s=zohoarc; b=R+EUk6JfeizUWkXYiySqSC4J1M/yU9xXTpzTGR5td9EOcel7SWHmED6Vtbo1XCRi9G5OS+RltlIgUpmJCQUknknkHb1mERjoLGWJNvDktt4nQkobkkCsKjPscy1U2XxI5uXMhsP3EXfpVqrXcklYinMe63b9H62RBBndGg0Lm28= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1752737532; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Qmq/Nsl0M7ErOrqCpTVnTzy11DB2G6jqpravhzisJJo=; b=B08L+w9wtlDTQpkRf18babVbG6K+Yg1uxHhJzv7K+Kbx50FL5olkGRyDBcGrcTawX8u9k5nh4wE9g2Pr3AoPrTrwGy+1EGNYJEAgQBoLt35uiGa4DlpIoxsgyCQZViQdEjz6stJPYERDBoB1WFUSrEMzfPMJaa0R1uDzpYw2J/I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1752737532129224.26297508774678; Thu, 17 Jul 2025 00:32:12 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1046276.1416607 (Exim 4.92) (envelope-from ) id 1ucJ5y-0004gS-IS; Thu, 17 Jul 2025 07:31:58 +0000 Received: by outflank-mailman (output) from mailman id 1046276.1416607; Thu, 17 Jul 2025 07:31:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ucJ5y-0004gJ-F5; Thu, 17 Jul 2025 07:31:58 +0000 Received: by outflank-mailman (input) for mailman id 1046276; Thu, 17 Jul 2025 07:31:57 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ucJ5x-0004NF-3A for xen-devel@lists.xenproject.org; Thu, 17 Jul 2025 07:31:57 +0000 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [2a00:1450:4864:20::336]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 1d10fa46-62e0-11f0-b894-0df219b8e170; Thu, 17 Jul 2025 09:31:55 +0200 (CEST) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-451dbe494d6so6247745e9.1 for ; Thu, 17 Jul 2025 00:31:55 -0700 (PDT) Received: from pc (cpc92320-cmbg19-2-0-cust1786.5-4.cable.virginm.net. [82.13.70.251]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b5e8dc201asm19792460f8f.22.2025.07.17.00.31.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jul 2025 00:31:52 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 1d10fa46-62e0-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752737514; x=1753342314; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Qmq/Nsl0M7ErOrqCpTVnTzy11DB2G6jqpravhzisJJo=; b=UjQXXaISBGBtxv5Dzfl1m0GZ3dR/nz8BO4SScOsQYv9MKQaRdjd9DUs3Ecvzjm9Ait OzYUPlnBgodr+mowq8KHySLo9zXbrKHzjzqZ7JSIcu7+hdKkDIDCpnea0tkatZviENXr SxAwFlxkj3IneESnWqdULurM1fLne64N9DKDf80cRYnXfo6mQk3V1gU2gK1Q3P0gdUhm 4sVUvpL05Ecrks27iK4qkZL8aw50MFW+EJWbhBiePjfoHqXk2WQjed/cNMlgqTHkuHLL dFrtaJPno3c19v5x0JGxA/lxdPNP9aKc5q1W7DIvVw7N73wAxVg2IV4j/8RFoJG3rqY9 4qnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752737514; x=1753342314; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qmq/Nsl0M7ErOrqCpTVnTzy11DB2G6jqpravhzisJJo=; b=G9GHpb/xa3y9ik41ke2TyvmlSAMjwZqC2amM654Lf8F9FMlBIFIzqdbLTLEYB+oBNo MSAAqrBLguIhQ8EPzX1+5HnxXWoSnYaLu88d4DwjVoEpgS9aOc7ddl6ZdxZEbLfb52iy EkjHkSCO+ori5hjVoYqW5uTf+U8wa/y62aV5DqRErzxAx1httqrcwD0Ourp8+l5Fq3ZN MiE3JGV2dtmNHAjVBC+iFmgv/DL/tH4Wqk5C5fhD83wVhFqTkiUbT3AuCOoJ9QXvr/L7 K4/8umqkp0wx/DhCowFXw6fniZEkIbNEcXG9IJbs+db9c3W6r1p96f+vJ/JtPOcGVRg+ 7Swg== X-Gm-Message-State: AOJu0YxbmfHTYQ8yJWCSOuPD4jGKSnC2v3QDdh5K/y8tu3NgCiE6edFy BTQlJSRk14720384J1TUCvRRoH6630fmGumBc2Izvig6UmTqZQnDZRTpSdLa/oYr X-Gm-Gg: ASbGncsLnl31kDww9xgu+EiBKyu1uUibXCs0c0IKiX2hH72kY8KIfrrMDKqsmzmeoJ5 yXVxagtXGpHRHwidA/vRsMOd7cgM9e1DZk/a+/8b5wHjPTjNn7Jj0od4DPPyxb0gwpDZaM2Cd9X 1Rev+cM7RLgMOSFpNqsuTS48ZRpjzk7/IJKzeFRxV4VlwNJENqz5r5gb3H1AEvMkhmsPjS0UduE CgggN7R7QCxznK2W+sddS33Cr3FAPqJT9MDT2tyMFbnLCY1XqAZTpcb9F4s3NHlk2nh0UDUhyDc 49VmXmtX86ZWmWeSEND9X1n0Com7AQ3pGzHD3FEphqCOfYP6uVarKvC5hF1UD+wBV9+lmlxVGCw Vpqb2ldj2QCux1x4sH+EqhrP41Xw7pSfCEje3vnhkGx7qi2XBRkmWEntP4thtrjYXBWKAZY5X X-Google-Smtp-Source: AGHT+IFerTBt+a2FRLEgXUDPZ4Z7PoLIzaMrvURSIR9xXAlQjAMwHEqbeJYQRDO6gcgdpvfg/zPmtQ== X-Received: by 2002:a05:600c:19cb:b0:43c:ee3f:2c3 with SMTP id 5b1f17b1804b1-4562e37a0ecmr42772215e9.7.1752737513849; Thu, 17 Jul 2025 00:31:53 -0700 (PDT) From: Andrii Sultanov To: xen-devel@lists.xenproject.org Cc: Andrii Sultanov , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v5 3/3] drivers: Make ioapic_sbdf and hpet_sbdf contain pci_sbdf_t Date: Thu, 17 Jul 2025 08:31:27 +0100 Message-ID: <94d8b7537021337a6b54d8950294702f5bbd2590.1752736989.git.andriy.sultanov@vates.tech> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1752737533000116600 Content-Type: text/plain; charset="utf-8" Following a similar change to amd_iommu struct, make two more structs take pci_sbdf_t directly instead of seg and bdf separately. This lets us drop several conversions from the latter to the former and simplifies several comparisons and assignments. Bloat-o-meter reports: add/remove: 0/0 grow/shrink: 1/10 up/down: 256/-320 (-64) Function old new delta _einittext 22092 22348 +256 parse_ivrs_hpet 248 245 -3 amd_iommu_detect_one_acpi 876 868 -8 iov_supports_xt 275 264 -11 amd_iommu_read_ioapic_from_ire 344 332 -12 amd_setup_hpet_msi 237 224 -13 amd_iommu_ioapic_update_ire 575 555 -20 reserve_unity_map_for_device 453 424 -29 _hvm_dpci_msi_eoi 160 128 -32 amd_iommu_get_supported_ivhd_type 86 30 -56 parse_ivrs_table 3966 3830 -136 Signed-off-by: Andrii Sultanov Reviewed-by: Jan Beulich --- Changes in V5: * Dropped PCI_BDF usage inside PCI_SBDF macros * Joined separate seg and bdf comparisons into a single sbdf one in parse_ivrs_table * Dropped unnecessary bdf in parse_ivhd_device_special, using sbdf.bdf instead * Reverted print formatting change in amd_iommu_ioapic_update_ire Changes in V4: * Folded several separate seg+bdf comparisons and assignments into one with sbdf_t * With reshuffling in the prior commits, this commit is no longer neutral in terms of code size Changes in V3: * Dropped aliasing of seg and bdf, renamed users. Changes in V2: * Split single commit into several patches * Change the format specifier to %pp in amd_iommu_ioapic_update_ire --- xen/drivers/passthrough/amd/iommu.h | 5 +-- xen/drivers/passthrough/amd/iommu_acpi.c | 40 ++++++++++------------ xen/drivers/passthrough/amd/iommu_intr.c | 43 ++++++++++++------------ 3 files changed, 41 insertions(+), 47 deletions(-) diff --git a/xen/drivers/passthrough/amd/iommu.h b/xen/drivers/passthrough/= amd/iommu.h index 2599800e6a..52f748310b 100644 --- a/xen/drivers/passthrough/amd/iommu.h +++ b/xen/drivers/passthrough/amd/iommu.h @@ -262,7 +262,7 @@ int cf_check amd_setup_hpet_msi(struct msi_desc *msi_de= sc); void cf_check amd_iommu_dump_intremap_tables(unsigned char key); =20 extern struct ioapic_sbdf { - u16 bdf, seg; + pci_sbdf_t sbdf; u8 id; bool cmdline; u16 *pin_2_idx; @@ -273,7 +273,8 @@ unsigned int ioapic_id_to_index(unsigned int apic_id); unsigned int get_next_ioapic_sbdf_index(void); =20 extern struct hpet_sbdf { - u16 bdf, seg, id; + pci_sbdf_t sbdf; + uint16_t id; enum { HPET_NONE, HPET_CMDL, diff --git a/xen/drivers/passthrough/amd/iommu_acpi.c b/xen/drivers/passthr= ough/amd/iommu_acpi.c index a9c5432e86..c007a427f2 100644 --- a/xen/drivers/passthrough/amd/iommu_acpi.c +++ b/xen/drivers/passthrough/amd/iommu_acpi.c @@ -707,8 +707,7 @@ static int __init cf_check parse_ivrs_ioapic(const char= *str) } } =20 - ioapic_sbdf[idx].bdf =3D PCI_BDF(bus, dev, func); - ioapic_sbdf[idx].seg =3D seg; + ioapic_sbdf[idx].sbdf =3D PCI_SBDF(seg, bus, dev, func); ioapic_sbdf[idx].id =3D id; ioapic_sbdf[idx].cmdline =3D true; =20 @@ -734,8 +733,7 @@ static int __init cf_check parse_ivrs_hpet(const char *= str) return -EINVAL; =20 hpet_sbdf.id =3D id; - hpet_sbdf.bdf =3D PCI_BDF(bus, dev, func); - hpet_sbdf.seg =3D seg; + hpet_sbdf.sbdf =3D PCI_SBDF(seg, bus, dev, func); hpet_sbdf.init =3D HPET_CMDL; =20 return 0; @@ -746,8 +744,9 @@ static u16 __init parse_ivhd_device_special( const struct acpi_ivrs_device8c *special, u16 seg, u16 header_length, u16 block_length, struct amd_iommu *iommu) { - u16 dev_length, bdf; + u16 dev_length; unsigned int apic, idx; + pci_sbdf_t sbdf; =20 dev_length =3D sizeof(*special); if ( header_length < (block_length + dev_length) ) @@ -756,16 +755,16 @@ static u16 __init parse_ivhd_device_special( return 0; } =20 - bdf =3D special->used_id; - if ( bdf >=3D ivrs_bdf_entries ) + sbdf =3D PCI_SBDF(seg, special->used_id); + if ( sbdf.bdf >=3D ivrs_bdf_entries ) { - AMD_IOMMU_ERROR("IVHD: invalid Device_Entry Dev_Id %#x\n", bdf); + AMD_IOMMU_ERROR("IVHD: invalid Device_Entry Dev_Id %#x\n", sbdf.bd= f); return 0; } =20 AMD_IOMMU_DEBUG("IVHD Special: %pp variety %#x handle %#x\n", - &PCI_SBDF(seg, bdf), special->variety, special->handle= ); - add_ivrs_mapping_entry(bdf, bdf, special->header.data_setting, 0, true, + &sbdf, special->variety, special->handle); + add_ivrs_mapping_entry(sbdf.bdf, sbdf.bdf, special->header.data_settin= g, 0, true, iommu); =20 switch ( special->variety ) @@ -780,8 +779,7 @@ static u16 __init parse_ivhd_device_special( */ for ( idx =3D 0; idx < nr_ioapic_sbdf; idx++ ) { - if ( ioapic_sbdf[idx].bdf =3D=3D bdf && - ioapic_sbdf[idx].seg =3D=3D seg && + if ( ioapic_sbdf[idx].sbdf.sbdf =3D=3D sbdf.sbdf && ioapic_sbdf[idx].cmdline ) break; } @@ -790,7 +788,7 @@ static u16 __init parse_ivhd_device_special( AMD_IOMMU_DEBUG("IVHD: Command line override present for IO-AP= IC %#x" "(IVRS: %#x devID %pp)\n", ioapic_sbdf[idx].id, special->handle, - &PCI_SBDF(seg, bdf)); + &sbdf); break; } =20 @@ -805,8 +803,7 @@ static u16 __init parse_ivhd_device_special( special->handle); else if ( idx !=3D MAX_IO_APICS && ioapic_sbdf[idx].pin_2_idx ) { - if ( ioapic_sbdf[idx].bdf =3D=3D bdf && - ioapic_sbdf[idx].seg =3D=3D seg ) + if ( ioapic_sbdf[idx].sbdf.sbdf =3D=3D sbdf.sbdf ) AMD_IOMMU_WARN("IVHD: duplicate IO-APIC %#x entries\n", special->handle); else @@ -827,8 +824,7 @@ static u16 __init parse_ivhd_device_special( } =20 /* set device id of ioapic */ - ioapic_sbdf[idx].bdf =3D bdf; - ioapic_sbdf[idx].seg =3D seg; + ioapic_sbdf[idx].sbdf =3D sbdf; ioapic_sbdf[idx].id =3D special->handle; =20 ioapic_sbdf[idx].pin_2_idx =3D xmalloc_array( @@ -862,13 +858,12 @@ static u16 __init parse_ivhd_device_special( AMD_IOMMU_DEBUG("IVHD: Command line override present for HPET = %#x " "(IVRS: %#x devID %pp)\n", hpet_sbdf.id, special->handle, - &PCI_SBDF(seg, bdf)); + &sbdf); break; case HPET_NONE: /* set device id of hpet */ hpet_sbdf.id =3D special->handle; - hpet_sbdf.bdf =3D bdf; - hpet_sbdf.seg =3D seg; + hpet_sbdf.sbdf =3D sbdf; hpet_sbdf.init =3D HPET_IVHD; break; default: @@ -1139,9 +1134,8 @@ static int __init cf_check parse_ivrs_table(struct ac= pi_table_header *table) return -ENXIO; } =20 - if ( !ioapic_sbdf[idx].seg && - /* SB IO-APIC is always on this device in AMD systems. */ - ioapic_sbdf[idx].bdf =3D=3D PCI_BDF(0, 0x14, 0) ) + /* SB IO-APIC is always on this device in AMD systems. */ + if ( ioapic_sbdf[idx].sbdf.sbdf =3D=3D PCI_SBDF(0, 0, 0x14, 0).sbd= f ) sb_ioapic =3D 1; =20 if ( ioapic_sbdf[idx].pin_2_idx ) diff --git a/xen/drivers/passthrough/amd/iommu_intr.c b/xen/drivers/passthr= ough/amd/iommu_intr.c index 25bf25904e..7dae89bcc0 100644 --- a/xen/drivers/passthrough/amd/iommu_intr.c +++ b/xen/drivers/passthrough/amd/iommu_intr.c @@ -323,7 +323,8 @@ void cf_check amd_iommu_ioapic_update_ire( unsigned int apic, unsigned int pin, uint64_t rte) { struct IO_APIC_route_entry new_rte; - int seg, bdf, rc; + pci_sbdf_t sbdf; + int rc; struct amd_iommu *iommu; unsigned int idx; =20 @@ -335,20 +336,19 @@ void cf_check amd_iommu_ioapic_update_ire( new_rte.raw =3D rte; =20 /* get device id of ioapic devices */ - bdf =3D ioapic_sbdf[idx].bdf; - seg =3D ioapic_sbdf[idx].seg; - iommu =3D find_iommu_for_device(PCI_SBDF(seg, bdf)); + sbdf =3D ioapic_sbdf[idx].sbdf; + iommu =3D find_iommu_for_device(sbdf); if ( !iommu ) { AMD_IOMMU_WARN("failed to find IOMMU for IO-APIC @ %04x:%04x\n", - seg, bdf); + sbdf.seg, sbdf.bdf); __ioapic_write_entry(apic, pin, true, new_rte); return; } =20 /* Update interrupt remapping entry */ rc =3D update_intremap_entry_from_ioapic( - bdf, iommu, &new_rte, + sbdf.bdf, iommu, &new_rte, &ioapic_sbdf[idx].pin_2_idx[pin]); =20 if ( rc ) @@ -369,7 +369,8 @@ unsigned int cf_check amd_iommu_read_ioapic_from_ire( unsigned int offset; unsigned int val =3D __io_apic_read(apic, reg); unsigned int pin =3D (reg - 0x10) / 2; - uint16_t seg, bdf, req_id; + pci_sbdf_t sbdf; + uint16_t req_id; const struct amd_iommu *iommu; union irte_ptr entry; =20 @@ -381,12 +382,11 @@ unsigned int cf_check amd_iommu_read_ioapic_from_ire( if ( offset >=3D INTREMAP_MAX_ENTRIES ) return val; =20 - seg =3D ioapic_sbdf[idx].seg; - bdf =3D ioapic_sbdf[idx].bdf; - iommu =3D find_iommu_for_device(PCI_SBDF(seg, bdf)); + sbdf =3D ioapic_sbdf[idx].sbdf; + iommu =3D find_iommu_for_device(sbdf); if ( !iommu ) return val; - req_id =3D get_intremap_requestor_id(seg, bdf); + req_id =3D get_intremap_requestor_id(sbdf.seg, sbdf.bdf); entry =3D get_intremap_entry(iommu, req_id, offset); =20 if ( !(reg & 1) ) @@ -515,15 +515,15 @@ int cf_check amd_iommu_msi_msg_update_ire( struct msi_desc *msi_desc, struct msi_msg *msg) { struct pci_dev *pdev =3D msi_desc->dev; - int bdf, seg, rc; + pci_sbdf_t sbdf; + int rc; struct amd_iommu *iommu; unsigned int i, nr =3D 1; u32 data; =20 - bdf =3D pdev ? pdev->sbdf.bdf : hpet_sbdf.bdf; - seg =3D pdev ? pdev->seg : hpet_sbdf.seg; + sbdf =3D pdev ? pdev->sbdf : hpet_sbdf.sbdf; =20 - iommu =3D _find_iommu_for_device(PCI_SBDF(seg, bdf)); + iommu =3D _find_iommu_for_device(sbdf); if ( IS_ERR_OR_NULL(iommu) ) return PTR_ERR(iommu); =20 @@ -532,7 +532,7 @@ int cf_check amd_iommu_msi_msg_update_ire( =20 if ( msi_desc->remap_index >=3D 0 && !msg ) { - update_intremap_entry_from_msi_msg(iommu, bdf, nr, + update_intremap_entry_from_msi_msg(iommu, sbdf.bdf, nr, &msi_desc->remap_index, NULL, NULL); =20 @@ -543,7 +543,7 @@ int cf_check amd_iommu_msi_msg_update_ire( if ( !msg ) return 0; =20 - rc =3D update_intremap_entry_from_msi_msg(iommu, bdf, nr, + rc =3D update_intremap_entry_from_msi_msg(iommu, sbdf.bdf, nr, &msi_desc->remap_index, msg, &data); if ( rc >=3D 0 ) @@ -660,8 +660,7 @@ bool __init cf_check iov_supports_xt(void) if ( idx =3D=3D MAX_IO_APICS ) return false; =20 - if ( !find_iommu_for_device(PCI_SBDF(ioapic_sbdf[idx].seg, - ioapic_sbdf[idx].bdf)) ) + if ( !find_iommu_for_device(ioapic_sbdf[idx].sbdf) ) { AMD_IOMMU_WARN("no IOMMU for IO-APIC %#x (ID %x)\n", apic, IO_APIC_ID(apic)); @@ -690,14 +689,14 @@ int __init cf_check amd_setup_hpet_msi(struct msi_des= c *msi_desc) return -ENODEV; } =20 - iommu =3D find_iommu_for_device(PCI_SBDF(hpet_sbdf.seg, hpet_sbdf.bdf)= ); + iommu =3D find_iommu_for_device(hpet_sbdf.sbdf); if ( !iommu ) return -ENXIO; =20 - lock =3D get_intremap_lock(hpet_sbdf.seg, hpet_sbdf.bdf); + lock =3D get_intremap_lock(hpet_sbdf.sbdf.seg, hpet_sbdf.sbdf.bdf); spin_lock_irqsave(lock, flags); =20 - msi_desc->remap_index =3D alloc_intremap_entry(iommu, hpet_sbdf.bdf, 1= ); + msi_desc->remap_index =3D alloc_intremap_entry(iommu, hpet_sbdf.sbdf.b= df, 1); if ( msi_desc->remap_index >=3D INTREMAP_MAX_ENTRIES ) { msi_desc->remap_index =3D -1; --=20 2.49.0