[PATCH 09/16] x86/amd_nb, x86/amd_node: Simplify amd_pci_dev_to_node_id()

Yazen Ghannam posted 16 patches 1 month ago
[PATCH 09/16] x86/amd_nb, x86/amd_node: Simplify amd_pci_dev_to_node_id()
Posted by Yazen Ghannam 1 month ago
From: Mario Limonciello <mario.limonciello@amd.com>

amd_pci_dev_to_node_id() tries to find the AMD node ID of a device by
searching and counting devices.

The AMD node ID of an AMD node device is simply its slot number minus
the AMD node 0 slot number.

Simplify this function and move it to amd_node.h.

[Yazen: Update commit message and simplify function]

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
Co-developed-by: Yazen Ghannam <yazen.ghannam@amd.com>
Signed-off-by: Yazen Ghannam <yazen.ghannam@amd.com>
---
 arch/x86/include/asm/amd_nb.h   | 17 -----------------
 arch/x86/include/asm/amd_node.h |  6 ++++++
 2 files changed, 6 insertions(+), 17 deletions(-)

diff --git a/arch/x86/include/asm/amd_nb.h b/arch/x86/include/asm/amd_nb.h
index a0f2182107b0..b3b42e585655 100644
--- a/arch/x86/include/asm/amd_nb.h
+++ b/arch/x86/include/asm/amd_nb.h
@@ -52,23 +52,6 @@ u16 amd_nb_num(void);
 bool amd_nb_has_feature(unsigned int feature);
 struct amd_northbridge *node_to_amd_nb(int node);
 
-static inline u16 amd_pci_dev_to_node_id(struct pci_dev *pdev)
-{
-	struct pci_dev *misc;
-	int i;
-
-	for (i = 0; i != amd_nb_num(); i++) {
-		misc = node_to_amd_nb(i)->misc;
-
-		if (pci_domain_nr(misc->bus) == pci_domain_nr(pdev->bus) &&
-		    PCI_SLOT(misc->devfn) == PCI_SLOT(pdev->devfn))
-			return i;
-	}
-
-	WARN(1, "Unable to find AMD Northbridge id for %s\n", pci_name(pdev));
-	return 0;
-}
-
 static inline bool amd_gart_present(void)
 {
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
diff --git a/arch/x86/include/asm/amd_node.h b/arch/x86/include/asm/amd_node.h
index 419a0ad13ef2..8e473a293706 100644
--- a/arch/x86/include/asm/amd_node.h
+++ b/arch/x86/include/asm/amd_node.h
@@ -30,4 +30,10 @@ static inline u16 amd_num_nodes(void)
 	return topology_amd_nodes_per_pkg() * topology_max_packages();
 }
 
+/* Caller must ensure the input is an AMD node device. */
+static inline u16 amd_pci_dev_to_node_id(struct pci_dev *pdev)
+{
+	return PCI_SLOT(pdev->devfn) - AMD_NODE0_PCI_SLOT;
+}
+
 #endif /*_ASM_X86_AMD_NODE_H_*/
-- 
2.43.0
Re: [PATCH 09/16] x86/amd_nb, x86/amd_node: Simplify amd_pci_dev_to_node_id()
Posted by Borislav Petkov 3 weeks ago
On Wed, Oct 23, 2024 at 05:21:43PM +0000, Yazen Ghannam wrote:
> diff --git a/arch/x86/include/asm/amd_node.h b/arch/x86/include/asm/amd_node.h
> index 419a0ad13ef2..8e473a293706 100644
> --- a/arch/x86/include/asm/amd_node.h
> +++ b/arch/x86/include/asm/amd_node.h
> @@ -30,4 +30,10 @@ static inline u16 amd_num_nodes(void)
>  	return topology_amd_nodes_per_pkg() * topology_max_packages();
>  }
>  
> +/* Caller must ensure the input is an AMD node device. */

You can ensure that yourself by checking the PCI vendor in the PCI device,
right?

IOW, pdev->vendor...

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH 09/16] x86/amd_nb, x86/amd_node: Simplify amd_pci_dev_to_node_id()
Posted by Yazen Ghannam 2 weeks, 6 days ago
On Mon, Nov 04, 2024 at 03:23:29PM +0100, Borislav Petkov wrote:
> On Wed, Oct 23, 2024 at 05:21:43PM +0000, Yazen Ghannam wrote:
> > diff --git a/arch/x86/include/asm/amd_node.h b/arch/x86/include/asm/amd_node.h
> > index 419a0ad13ef2..8e473a293706 100644
> > --- a/arch/x86/include/asm/amd_node.h
> > +++ b/arch/x86/include/asm/amd_node.h
> > @@ -30,4 +30,10 @@ static inline u16 amd_num_nodes(void)
> >  	return topology_amd_nodes_per_pkg() * topology_max_packages();
> >  }
> >  
> > +/* Caller must ensure the input is an AMD node device. */
> 
> You can ensure that yourself by checking the PCI vendor in the PCI device,
> right?
> 
> IOW, pdev->vendor...
>

Yes, there can be a vendor and/or bus,device check.

I'll add them.

Thanks,
Yazen