xref: /openbmc/linux/arch/x86/pci/acpi.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d91482bbSBjorn Helgaas 
3d91482bbSBjorn Helgaas #define pr_fmt(fmt) "PCI: " fmt
4d91482bbSBjorn Helgaas 
5fb9aa6f1SThomas Gleixner #include <linux/pci.h>
6fb9aa6f1SThomas Gleixner #include <linux/acpi.h>
7fb9aa6f1SThomas Gleixner #include <linux/init.h>
8fb9aa6f1SThomas Gleixner #include <linux/irq.h>
9036fff4cSGary Hade #include <linux/dmi.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
114d6b4e69SJiang Liu #include <linux/pci-acpi.h>
12fb9aa6f1SThomas Gleixner #include <asm/numa.h>
1382487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
14fb9aa6f1SThomas Gleixner 
1562f420f8SGary Hade struct pci_root_info {
164d6b4e69SJiang Liu 	struct acpi_pci_root_info common;
1735cb05e5SYinghai Lu 	struct pci_sysdata sd;
18c0fa4078SJiang Liu #ifdef	CONFIG_PCI_MMCONFIG
19c0fa4078SJiang Liu 	bool mcfg_added;
20c0fa4078SJiang Liu 	u8 start_bus;
21c0fa4078SJiang Liu 	u8 end_bus;
22c0fa4078SJiang Liu #endif
2362f420f8SGary Hade };
2462f420f8SGary Hade 
25a2b36ffbSHans de Goede bool pci_use_e820 = true;
267bc5e3f2SBjorn Helgaas static bool pci_use_crs = true;
27346865f0SLongji Guo static bool pci_ignore_seg;
287bc5e3f2SBjorn Helgaas 
set_use_crs(const struct dmi_system_id * id)297bc5e3f2SBjorn Helgaas static int __init set_use_crs(const struct dmi_system_id *id)
307bc5e3f2SBjorn Helgaas {
317bc5e3f2SBjorn Helgaas 	pci_use_crs = true;
327bc5e3f2SBjorn Helgaas 	return 0;
337bc5e3f2SBjorn Helgaas }
347bc5e3f2SBjorn Helgaas 
set_nouse_crs(const struct dmi_system_id * id)3528c3c05dSDave Jones static int __init set_nouse_crs(const struct dmi_system_id *id)
3628c3c05dSDave Jones {
3728c3c05dSDave Jones 	pci_use_crs = false;
3828c3c05dSDave Jones 	return 0;
3928c3c05dSDave Jones }
4028c3c05dSDave Jones 
set_ignore_seg(const struct dmi_system_id * id)411f09b09bSBjorn Helgaas static int __init set_ignore_seg(const struct dmi_system_id *id)
421f09b09bSBjorn Helgaas {
43d91482bbSBjorn Helgaas 	pr_info("%s detected: ignoring ACPI _SEG\n", id->ident);
441f09b09bSBjorn Helgaas 	pci_ignore_seg = true;
451f09b09bSBjorn Helgaas 	return 0;
461f09b09bSBjorn Helgaas }
471f09b09bSBjorn Helgaas 
set_no_e820(const struct dmi_system_id * id)48d341838dSHans de Goede static int __init set_no_e820(const struct dmi_system_id *id)
49d341838dSHans de Goede {
50d91482bbSBjorn Helgaas 	pr_info("%s detected: not clipping E820 regions from _CRS\n",
51d341838dSHans de Goede 	        id->ident);
52d341838dSHans de Goede 	pci_use_e820 = false;
53d341838dSHans de Goede 	return 0;
54d341838dSHans de Goede }
55d341838dSHans de Goede 
561f09b09bSBjorn Helgaas static const struct dmi_system_id pci_crs_quirks[] __initconst = {
577bc5e3f2SBjorn Helgaas 	/* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
587bc5e3f2SBjorn Helgaas 	{
597bc5e3f2SBjorn Helgaas 		.callback = set_use_crs,
607bc5e3f2SBjorn Helgaas 		.ident = "IBM System x3800",
617bc5e3f2SBjorn Helgaas 		.matches = {
627bc5e3f2SBjorn Helgaas 			DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
637bc5e3f2SBjorn Helgaas 			DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
647bc5e3f2SBjorn Helgaas 		},
657bc5e3f2SBjorn Helgaas 	},
662491762cSBjorn Helgaas 	/* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
672491762cSBjorn Helgaas 	/* 2006 AMD HT/VIA system with two host bridges */
682491762cSBjorn Helgaas         {
692491762cSBjorn Helgaas 		.callback = set_use_crs,
702491762cSBjorn Helgaas 		.ident = "ASRock ALiveSATA2-GLAN",
712491762cSBjorn Helgaas 		.matches = {
722491762cSBjorn Helgaas 			DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
732491762cSBjorn Helgaas                 },
742491762cSBjorn Helgaas         },
7529cf7a30SPaul Menzel 	/* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
7629cf7a30SPaul Menzel 	/* 2006 AMD HT/VIA system with two host bridges */
7729cf7a30SPaul Menzel 	{
7829cf7a30SPaul Menzel 		.callback = set_use_crs,
7929cf7a30SPaul Menzel 		.ident = "ASUS M2V-MX SE",
8029cf7a30SPaul Menzel 		.matches = {
8129cf7a30SPaul Menzel 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
8229cf7a30SPaul Menzel 			DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
8329cf7a30SPaul Menzel 			DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
8429cf7a30SPaul Menzel 		},
8529cf7a30SPaul Menzel 	},
8684113717SJonathan Nieder 	/* https://bugzilla.kernel.org/show_bug.cgi?id=42619 */
8784113717SJonathan Nieder 	{
8884113717SJonathan Nieder 		.callback = set_use_crs,
8984113717SJonathan Nieder 		.ident = "MSI MS-7253",
9084113717SJonathan Nieder 		.matches = {
9184113717SJonathan Nieder 			DMI_MATCH(DMI_BOARD_VENDOR, "MICRO-STAR INTERNATIONAL CO., LTD"),
9284113717SJonathan Nieder 			DMI_MATCH(DMI_BOARD_NAME, "MS-7253"),
9384113717SJonathan Nieder 			DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
9484113717SJonathan Nieder 		},
9584113717SJonathan Nieder 	},
961dace011SBjorn Helgaas 	/* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/931368 */
971dace011SBjorn Helgaas 	/* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/1033299 */
981dace011SBjorn Helgaas 	{
991dace011SBjorn Helgaas 		.callback = set_use_crs,
1001dace011SBjorn Helgaas 		.ident = "Foxconn K8M890-8237A",
1011dace011SBjorn Helgaas 		.matches = {
1021dace011SBjorn Helgaas 			DMI_MATCH(DMI_BOARD_VENDOR, "Foxconn"),
1031dace011SBjorn Helgaas 			DMI_MATCH(DMI_BOARD_NAME, "K8M890-8237A"),
1041dace011SBjorn Helgaas 			DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
1051dace011SBjorn Helgaas 		},
1061dace011SBjorn Helgaas 	},
10728c3c05dSDave Jones 
108e702781fSDave Jones 	/* Now for the blacklist.. */
109e702781fSDave Jones 
110e702781fSDave Jones 	/* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
111e702781fSDave Jones 	{
112e702781fSDave Jones 		.callback = set_nouse_crs,
113e702781fSDave Jones 		.ident = "Dell Studio 1557",
114e702781fSDave Jones 		.matches = {
115e702781fSDave Jones 			DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
116e702781fSDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
117e702781fSDave Jones 			DMI_MATCH(DMI_BIOS_VERSION, "A09"),
118e702781fSDave Jones 		},
119e702781fSDave Jones 	},
1208b6a5af9SDave Jones 	/* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
1218b6a5af9SDave Jones 	{
1228b6a5af9SDave Jones 		.callback = set_nouse_crs,
1238b6a5af9SDave Jones 		.ident = "Thinkpad SL510",
1248b6a5af9SDave Jones 		.matches = {
1258b6a5af9SDave Jones 			DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
1268b6a5af9SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "2847DFG"),
1278b6a5af9SDave Jones 			DMI_MATCH(DMI_BIOS_VERSION, "6JET85WW (1.43 )"),
1288b6a5af9SDave Jones 		},
1298b6a5af9SDave Jones 	},
13089e9f7bcSBjorn Helgaas 	/* https://bugzilla.kernel.org/show_bug.cgi?id=42606 */
13189e9f7bcSBjorn Helgaas 	{
13289e9f7bcSBjorn Helgaas 		.callback = set_nouse_crs,
13389e9f7bcSBjorn Helgaas 		.ident = "Supermicro X8DTH",
13489e9f7bcSBjorn Helgaas 		.matches = {
13589e9f7bcSBjorn Helgaas 			DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
13689e9f7bcSBjorn Helgaas 			DMI_MATCH(DMI_PRODUCT_NAME, "X8DTH-i/6/iF/6F"),
13789e9f7bcSBjorn Helgaas 			DMI_MATCH(DMI_BIOS_VERSION, "2.0a"),
13889e9f7bcSBjorn Helgaas 		},
13989e9f7bcSBjorn Helgaas 	},
1401f09b09bSBjorn Helgaas 
1411f09b09bSBjorn Helgaas 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15362 */
1421f09b09bSBjorn Helgaas 	{
1431f09b09bSBjorn Helgaas 		.callback = set_ignore_seg,
1441f09b09bSBjorn Helgaas 		.ident = "HP xw9300",
1451f09b09bSBjorn Helgaas 		.matches = {
1461f09b09bSBjorn Helgaas 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1471f09b09bSBjorn Helgaas 			DMI_MATCH(DMI_PRODUCT_NAME, "HP xw9300 Workstation"),
1481f09b09bSBjorn Helgaas 		},
1491f09b09bSBjorn Helgaas 	},
150d341838dSHans de Goede 
151d341838dSHans de Goede 	/*
152d341838dSHans de Goede 	 * Many Lenovo models with "IIL" in their DMI_PRODUCT_VERSION have
153d341838dSHans de Goede 	 * an E820 reserved region that covers the entire 32-bit host
154d341838dSHans de Goede 	 * bridge memory window from _CRS.  Using the E820 region to clip
155d341838dSHans de Goede 	 * _CRS means no space is available for hot-added or uninitialized
156d341838dSHans de Goede 	 * PCI devices.  This typically breaks I2C controllers for touchpads
157d341838dSHans de Goede 	 * and hot-added Thunderbolt devices.  See the commit log for
158d341838dSHans de Goede 	 * models known to require this quirk and related bug reports.
159d341838dSHans de Goede 	 */
160d341838dSHans de Goede 	{
161d341838dSHans de Goede 		.callback = set_no_e820,
162d341838dSHans de Goede 		.ident = "Lenovo *IIL* product version",
163d341838dSHans de Goede 		.matches = {
164d341838dSHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
165d341838dSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "IIL"),
166d341838dSHans de Goede 		},
167d341838dSHans de Goede 	},
168d341838dSHans de Goede 
169d341838dSHans de Goede 	/*
170d341838dSHans de Goede 	 * The Acer Spin 5 (SP513-54N) has the same E820 reservation covering
171d341838dSHans de Goede 	 * the entire _CRS 32-bit window issue as the Lenovo *IIL* models.
172d341838dSHans de Goede 	 * See https://bugs.launchpad.net/bugs/1884232
173d341838dSHans de Goede 	 */
174d341838dSHans de Goede 	{
175d341838dSHans de Goede 		.callback = set_no_e820,
176d341838dSHans de Goede 		.ident = "Acer Spin 5 (SP513-54N)",
177d341838dSHans de Goede 		.matches = {
178d341838dSHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
179d341838dSHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Spin SP513-54N"),
180d341838dSHans de Goede 		},
181d341838dSHans de Goede 	},
182d341838dSHans de Goede 
183d341838dSHans de Goede 	/*
184d341838dSHans de Goede 	 * Clevo X170KM-G barebones have the same E820 reservation covering
185d341838dSHans de Goede 	 * the entire _CRS 32-bit window issue as the Lenovo *IIL* models.
186d341838dSHans de Goede 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=214259
187d341838dSHans de Goede 	 */
188d341838dSHans de Goede 	{
189d341838dSHans de Goede 		.callback = set_no_e820,
190d341838dSHans de Goede 		.ident = "Clevo X170KM-G Barebone",
191d341838dSHans de Goede 		.matches = {
192d341838dSHans de Goede 			DMI_MATCH(DMI_BOARD_NAME, "X170KM-G"),
193d341838dSHans de Goede 		},
194d341838dSHans de Goede 	},
1957bc5e3f2SBjorn Helgaas 	{}
1967bc5e3f2SBjorn Helgaas };
1977bc5e3f2SBjorn Helgaas 
pci_acpi_crs_quirks(void)1987bc5e3f2SBjorn Helgaas void __init pci_acpi_crs_quirks(void)
1997bc5e3f2SBjorn Helgaas {
20047a9973dSAndy Shevchenko 	int year = dmi_get_bios_year();
20147a9973dSAndy Shevchenko 
20247a9973dSAndy Shevchenko 	if (year >= 0 && year < 2008 && iomem_resource.end <= 0xffffffff)
2037bc5e3f2SBjorn Helgaas 		pci_use_crs = false;
2047bc5e3f2SBjorn Helgaas 
2050ae084d5SHans de Goede 	/*
2060ae084d5SHans de Goede 	 * Some firmware includes unusable space (host bridge registers,
2070ae084d5SHans de Goede 	 * hidden PCI device BARs, etc) in PCI host bridge _CRS.  This is a
2080ae084d5SHans de Goede 	 * firmware defect, and 4dc2287c1805 ("x86: avoid E820 regions when
2090ae084d5SHans de Goede 	 * allocating address space") has clipped out the unusable space in
2100ae084d5SHans de Goede 	 * the past.
2110ae084d5SHans de Goede 	 *
2120ae084d5SHans de Goede 	 * But other firmware supplies E820 reserved regions that cover
2130ae084d5SHans de Goede 	 * entire _CRS windows, so clipping throws away the entire window,
2140ae084d5SHans de Goede 	 * leaving none for hot-added or uninitialized devices.  These E820
2150ae084d5SHans de Goede 	 * entries are probably *not* a firmware defect, so disable the
2160ae084d5SHans de Goede 	 * clipping by default for post-2022 machines.
2170ae084d5SHans de Goede 	 *
2180ae084d5SHans de Goede 	 * We already have quirks to disable clipping for pre-2023
2190ae084d5SHans de Goede 	 * machines, and we'll likely need quirks to *enable* clipping for
2200ae084d5SHans de Goede 	 * post-2022 machines that incorrectly include unusable space in
2210ae084d5SHans de Goede 	 * _CRS.
2220ae084d5SHans de Goede 	 */
2230ae084d5SHans de Goede 	if (year >= 2023)
2240ae084d5SHans de Goede 		pci_use_e820 = false;
2250ae084d5SHans de Goede 
2261f09b09bSBjorn Helgaas 	dmi_check_system(pci_crs_quirks);
2277bc5e3f2SBjorn Helgaas 
2287bc5e3f2SBjorn Helgaas 	/*
2297bc5e3f2SBjorn Helgaas 	 * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
2307bc5e3f2SBjorn Helgaas 	 * takes precedence over anything we figured out above.
2317bc5e3f2SBjorn Helgaas 	 */
2327bc5e3f2SBjorn Helgaas 	if (pci_probe & PCI_ROOT_NO_CRS)
2337bc5e3f2SBjorn Helgaas 		pci_use_crs = false;
2347bc5e3f2SBjorn Helgaas 	else if (pci_probe & PCI_USE__CRS)
2357bc5e3f2SBjorn Helgaas 		pci_use_crs = true;
2367bc5e3f2SBjorn Helgaas 
237d91482bbSBjorn Helgaas 	pr_info("%s host bridge windows from ACPI; if necessary, use \"pci=%s\" and report a bug\n",
2387bc5e3f2SBjorn Helgaas 	        pci_use_crs ? "Using" : "Ignoring",
2397bc5e3f2SBjorn Helgaas 	        pci_use_crs ? "nocrs" : "use_crs");
240fa6dae5dSHans de Goede 
241fa6dae5dSHans de Goede 	/* "pci=use_e820"/"pci=no_e820" on the kernel cmdline takes precedence */
242fa6dae5dSHans de Goede 	if (pci_probe & PCI_NO_E820)
243fa6dae5dSHans de Goede 		pci_use_e820 = false;
244fa6dae5dSHans de Goede 	else if (pci_probe & PCI_USE_E820)
245fa6dae5dSHans de Goede 		pci_use_e820 = true;
246fa6dae5dSHans de Goede 
247d91482bbSBjorn Helgaas 	pr_info("%s E820 reservations for host bridge windows\n",
248fa6dae5dSHans de Goede 	        pci_use_e820 ? "Using" : "Ignoring");
249fa6dae5dSHans de Goede 	if (pci_probe & (PCI_NO_E820 | PCI_USE_E820))
250d91482bbSBjorn Helgaas 		pr_info("Please notify linux-pci@vger.kernel.org so future kernels can do this automatically\n");
2517bc5e3f2SBjorn Helgaas }
2527bc5e3f2SBjorn Helgaas 
253*b824ea2aSEsther Shimanovich /*
254*b824ea2aSEsther Shimanovich  * Check if pdev is part of a PCIe switch that is directly below the
255*b824ea2aSEsther Shimanovich  * specified bridge.
256*b824ea2aSEsther Shimanovich  */
pcie_switch_directly_under(struct pci_dev * bridge,struct pci_dev * pdev)257*b824ea2aSEsther Shimanovich static bool pcie_switch_directly_under(struct pci_dev *bridge,
258*b824ea2aSEsther Shimanovich 				       struct pci_dev *pdev)
259*b824ea2aSEsther Shimanovich {
260*b824ea2aSEsther Shimanovich 	struct pci_dev *parent = pci_upstream_bridge(pdev);
261*b824ea2aSEsther Shimanovich 
262*b824ea2aSEsther Shimanovich 	/* If the device doesn't have a parent, it's not under anything */
263*b824ea2aSEsther Shimanovich 	if (!parent)
264*b824ea2aSEsther Shimanovich 		return false;
265*b824ea2aSEsther Shimanovich 
266*b824ea2aSEsther Shimanovich 	/*
267*b824ea2aSEsther Shimanovich 	 * If the device has a PCIe type, check if it is below the
268*b824ea2aSEsther Shimanovich 	 * corresponding PCIe switch components (if applicable). Then check
269*b824ea2aSEsther Shimanovich 	 * if its upstream port is directly beneath the specified bridge.
270*b824ea2aSEsther Shimanovich 	 */
271*b824ea2aSEsther Shimanovich 	switch (pci_pcie_type(pdev)) {
272*b824ea2aSEsther Shimanovich 	case PCI_EXP_TYPE_UPSTREAM:
273*b824ea2aSEsther Shimanovich 		return parent == bridge;
274*b824ea2aSEsther Shimanovich 
275*b824ea2aSEsther Shimanovich 	case PCI_EXP_TYPE_DOWNSTREAM:
276*b824ea2aSEsther Shimanovich 		if (pci_pcie_type(parent) != PCI_EXP_TYPE_UPSTREAM)
277*b824ea2aSEsther Shimanovich 			return false;
278*b824ea2aSEsther Shimanovich 		parent = pci_upstream_bridge(parent);
279*b824ea2aSEsther Shimanovich 		return parent == bridge;
280*b824ea2aSEsther Shimanovich 
281*b824ea2aSEsther Shimanovich 	case PCI_EXP_TYPE_ENDPOINT:
282*b824ea2aSEsther Shimanovich 		if (pci_pcie_type(parent) != PCI_EXP_TYPE_DOWNSTREAM)
283*b824ea2aSEsther Shimanovich 			return false;
284*b824ea2aSEsther Shimanovich 		parent = pci_upstream_bridge(parent);
285*b824ea2aSEsther Shimanovich 		if (!parent || pci_pcie_type(parent) != PCI_EXP_TYPE_UPSTREAM)
286*b824ea2aSEsther Shimanovich 			return false;
287*b824ea2aSEsther Shimanovich 		parent = pci_upstream_bridge(parent);
288*b824ea2aSEsther Shimanovich 		return parent == bridge;
289*b824ea2aSEsther Shimanovich 	}
290*b824ea2aSEsther Shimanovich 
291*b824ea2aSEsther Shimanovich 	return false;
292*b824ea2aSEsther Shimanovich }
293*b824ea2aSEsther Shimanovich 
pcie_has_usb4_host_interface(struct pci_dev * pdev)294*b824ea2aSEsther Shimanovich static bool pcie_has_usb4_host_interface(struct pci_dev *pdev)
295*b824ea2aSEsther Shimanovich {
296*b824ea2aSEsther Shimanovich 	struct fwnode_handle *fwnode;
297*b824ea2aSEsther Shimanovich 
298*b824ea2aSEsther Shimanovich 	/*
299*b824ea2aSEsther Shimanovich 	 * For USB4, the tunneled PCIe Root or Downstream Ports are marked
300*b824ea2aSEsther Shimanovich 	 * with the "usb4-host-interface" ACPI property, so we look for
301*b824ea2aSEsther Shimanovich 	 * that first. This should cover most cases.
302*b824ea2aSEsther Shimanovich 	 */
303*b824ea2aSEsther Shimanovich 	fwnode = fwnode_find_reference(dev_fwnode(&pdev->dev),
304*b824ea2aSEsther Shimanovich 				       "usb4-host-interface", 0);
305*b824ea2aSEsther Shimanovich 	if (!IS_ERR(fwnode)) {
306*b824ea2aSEsther Shimanovich 		fwnode_handle_put(fwnode);
307*b824ea2aSEsther Shimanovich 		return true;
308*b824ea2aSEsther Shimanovich 	}
309*b824ea2aSEsther Shimanovich 
310*b824ea2aSEsther Shimanovich 	/*
311*b824ea2aSEsther Shimanovich 	 * Any integrated Thunderbolt 3/4 PCIe Root Ports from Intel
312*b824ea2aSEsther Shimanovich 	 * before Alder Lake do not have the "usb4-host-interface"
313*b824ea2aSEsther Shimanovich 	 * property so we use their PCI IDs instead. All these are
314*b824ea2aSEsther Shimanovich 	 * tunneled. This list is not expected to grow.
315*b824ea2aSEsther Shimanovich 	 */
316*b824ea2aSEsther Shimanovich 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
317*b824ea2aSEsther Shimanovich 		switch (pdev->device) {
318*b824ea2aSEsther Shimanovich 		/* Ice Lake Thunderbolt 3 PCIe Root Ports */
319*b824ea2aSEsther Shimanovich 		case 0x8a1d:
320*b824ea2aSEsther Shimanovich 		case 0x8a1f:
321*b824ea2aSEsther Shimanovich 		case 0x8a21:
322*b824ea2aSEsther Shimanovich 		case 0x8a23:
323*b824ea2aSEsther Shimanovich 		/* Tiger Lake-LP Thunderbolt 4 PCIe Root Ports */
324*b824ea2aSEsther Shimanovich 		case 0x9a23:
325*b824ea2aSEsther Shimanovich 		case 0x9a25:
326*b824ea2aSEsther Shimanovich 		case 0x9a27:
327*b824ea2aSEsther Shimanovich 		case 0x9a29:
328*b824ea2aSEsther Shimanovich 		/* Tiger Lake-H Thunderbolt 4 PCIe Root Ports */
329*b824ea2aSEsther Shimanovich 		case 0x9a2b:
330*b824ea2aSEsther Shimanovich 		case 0x9a2d:
331*b824ea2aSEsther Shimanovich 		case 0x9a2f:
332*b824ea2aSEsther Shimanovich 		case 0x9a31:
333*b824ea2aSEsther Shimanovich 			return true;
334*b824ea2aSEsther Shimanovich 		}
335*b824ea2aSEsther Shimanovich 	}
336*b824ea2aSEsther Shimanovich 
337*b824ea2aSEsther Shimanovich 	return false;
338*b824ea2aSEsther Shimanovich }
339*b824ea2aSEsther Shimanovich 
arch_pci_dev_is_removable(struct pci_dev * pdev)340*b824ea2aSEsther Shimanovich bool arch_pci_dev_is_removable(struct pci_dev *pdev)
341*b824ea2aSEsther Shimanovich {
342*b824ea2aSEsther Shimanovich 	struct pci_dev *parent, *root;
343*b824ea2aSEsther Shimanovich 
344*b824ea2aSEsther Shimanovich 	/* pdev without a parent or Root Port is never tunneled */
345*b824ea2aSEsther Shimanovich 	parent = pci_upstream_bridge(pdev);
346*b824ea2aSEsther Shimanovich 	if (!parent)
347*b824ea2aSEsther Shimanovich 		return false;
348*b824ea2aSEsther Shimanovich 	root = pcie_find_root_port(pdev);
349*b824ea2aSEsther Shimanovich 	if (!root)
350*b824ea2aSEsther Shimanovich 		return false;
351*b824ea2aSEsther Shimanovich 
352*b824ea2aSEsther Shimanovich 	/* Internal PCIe devices are not tunneled */
353*b824ea2aSEsther Shimanovich 	if (!root->external_facing)
354*b824ea2aSEsther Shimanovich 		return false;
355*b824ea2aSEsther Shimanovich 
356*b824ea2aSEsther Shimanovich 	/* Anything directly behind a "usb4-host-interface" is tunneled */
357*b824ea2aSEsther Shimanovich 	if (pcie_has_usb4_host_interface(parent))
358*b824ea2aSEsther Shimanovich 		return true;
359*b824ea2aSEsther Shimanovich 
360*b824ea2aSEsther Shimanovich 	/*
361*b824ea2aSEsther Shimanovich 	 * Check if this is a discrete Thunderbolt/USB4 controller that is
362*b824ea2aSEsther Shimanovich 	 * directly behind the non-USB4 PCIe Root Port marked as
363*b824ea2aSEsther Shimanovich 	 * "ExternalFacingPort". Those are not behind a PCIe tunnel.
364*b824ea2aSEsther Shimanovich 	 */
365*b824ea2aSEsther Shimanovich 	if (pcie_switch_directly_under(root, pdev))
366*b824ea2aSEsther Shimanovich 		return false;
367*b824ea2aSEsther Shimanovich 
368*b824ea2aSEsther Shimanovich 	/* PCIe devices after the discrete chip are tunneled */
369*b824ea2aSEsther Shimanovich 	return true;
370*b824ea2aSEsther Shimanovich }
371*b824ea2aSEsther Shimanovich 
372c0fa4078SJiang Liu #ifdef	CONFIG_PCI_MMCONFIG
check_segment(u16 seg,struct device * dev,char * estr)373a18e3690SGreg Kroah-Hartman static int check_segment(u16 seg, struct device *dev, char *estr)
374c0fa4078SJiang Liu {
375c0fa4078SJiang Liu 	if (seg) {
376d91482bbSBjorn Helgaas 		dev_err(dev, "%s can't access configuration space under this host bridge\n",
377c0fa4078SJiang Liu 			estr);
378c0fa4078SJiang Liu 		return -EIO;
379c0fa4078SJiang Liu 	}
380c0fa4078SJiang Liu 
381c0fa4078SJiang Liu 	/*
382c0fa4078SJiang Liu 	 * Failure in adding MMCFG information is not fatal,
383c0fa4078SJiang Liu 	 * just can't access extended configuration space of
384c0fa4078SJiang Liu 	 * devices under this host bridge.
385c0fa4078SJiang Liu 	 */
386d91482bbSBjorn Helgaas 	dev_warn(dev, "%s can't access extended configuration space under this bridge\n",
387c0fa4078SJiang Liu 		 estr);
388c0fa4078SJiang Liu 
389c0fa4078SJiang Liu 	return 0;
390c0fa4078SJiang Liu }
391c0fa4078SJiang Liu 
setup_mcfg_map(struct acpi_pci_root_info * ci)3924d6b4e69SJiang Liu static int setup_mcfg_map(struct acpi_pci_root_info *ci)
393c0fa4078SJiang Liu {
3944d6b4e69SJiang Liu 	int result, seg;
3954d6b4e69SJiang Liu 	struct pci_root_info *info;
3964d6b4e69SJiang Liu 	struct acpi_pci_root *root = ci->root;
3974d6b4e69SJiang Liu 	struct device *dev = &ci->bridge->dev;
398c0fa4078SJiang Liu 
3994d6b4e69SJiang Liu 	info = container_of(ci, struct pci_root_info, common);
4004d6b4e69SJiang Liu 	info->start_bus = (u8)root->secondary.start;
4014d6b4e69SJiang Liu 	info->end_bus = (u8)root->secondary.end;
402c0fa4078SJiang Liu 	info->mcfg_added = false;
4034d6b4e69SJiang Liu 	seg = info->sd.domain;
404c0fa4078SJiang Liu 
405c0fa4078SJiang Liu 	/* return success if MMCFG is not in use */
406c0fa4078SJiang Liu 	if (raw_pci_ext_ops && raw_pci_ext_ops != &pci_mmcfg)
407c0fa4078SJiang Liu 		return 0;
408c0fa4078SJiang Liu 
409c0fa4078SJiang Liu 	if (!(pci_probe & PCI_PROBE_MMCONF))
410c0fa4078SJiang Liu 		return check_segment(seg, dev, "MMCONFIG is disabled,");
411c0fa4078SJiang Liu 
4124d6b4e69SJiang Liu 	result = pci_mmconfig_insert(dev, seg, info->start_bus, info->end_bus,
4134d6b4e69SJiang Liu 				     root->mcfg_addr);
414c0fa4078SJiang Liu 	if (result == 0) {
415c0fa4078SJiang Liu 		/* enable MMCFG if it hasn't been enabled yet */
416c0fa4078SJiang Liu 		if (raw_pci_ext_ops == NULL)
417c0fa4078SJiang Liu 			raw_pci_ext_ops = &pci_mmcfg;
418c0fa4078SJiang Liu 		info->mcfg_added = true;
419c0fa4078SJiang Liu 	} else if (result != -EEXIST)
420c0fa4078SJiang Liu 		return check_segment(seg, dev,
421c0fa4078SJiang Liu 			 "fail to add MMCONFIG information,");
422c0fa4078SJiang Liu 
423c0fa4078SJiang Liu 	return 0;
424c0fa4078SJiang Liu }
425c0fa4078SJiang Liu 
teardown_mcfg_map(struct acpi_pci_root_info * ci)4264d6b4e69SJiang Liu static void teardown_mcfg_map(struct acpi_pci_root_info *ci)
427c0fa4078SJiang Liu {
4284d6b4e69SJiang Liu 	struct pci_root_info *info;
4294d6b4e69SJiang Liu 
4304d6b4e69SJiang Liu 	info = container_of(ci, struct pci_root_info, common);
431c0fa4078SJiang Liu 	if (info->mcfg_added) {
4324d6b4e69SJiang Liu 		pci_mmconfig_delete(info->sd.domain,
4334d6b4e69SJiang Liu 				    info->start_bus, info->end_bus);
434c0fa4078SJiang Liu 		info->mcfg_added = false;
435c0fa4078SJiang Liu 	}
436c0fa4078SJiang Liu }
437c0fa4078SJiang Liu #else
setup_mcfg_map(struct acpi_pci_root_info * ci)4384d6b4e69SJiang Liu static int setup_mcfg_map(struct acpi_pci_root_info *ci)
439c0fa4078SJiang Liu {
440c0fa4078SJiang Liu 	return 0;
441c0fa4078SJiang Liu }
4424d6b4e69SJiang Liu 
teardown_mcfg_map(struct acpi_pci_root_info * ci)4434d6b4e69SJiang Liu static void teardown_mcfg_map(struct acpi_pci_root_info *ci)
444c0fa4078SJiang Liu {
445c0fa4078SJiang Liu }
446c0fa4078SJiang Liu #endif
447c0fa4078SJiang Liu 
pci_acpi_root_get_node(struct acpi_pci_root * root)4484d6b4e69SJiang Liu static int pci_acpi_root_get_node(struct acpi_pci_root *root)
44962f420f8SGary Hade {
4504d6b4e69SJiang Liu 	int busnum = root->secondary.start;
4514d6b4e69SJiang Liu 	struct acpi_device *device = root->device;
4524d6b4e69SJiang Liu 	int node = acpi_get_node(device->handle);
45362f420f8SGary Hade 
4544d6b4e69SJiang Liu 	if (node == NUMA_NO_NODE) {
4554d6b4e69SJiang Liu 		node = x86_pci_root_bus_node(busnum);
4564d6b4e69SJiang Liu 		if (node != 0 && node != NUMA_NO_NODE)
4574d6b4e69SJiang Liu 			dev_info(&device->dev, FW_BUG "no _PXM; falling back to node %d from hardware (may be inconsistent with ACPI node numbers)\n",
4584d6b4e69SJiang Liu 				node);
4594d6b4e69SJiang Liu 	}
4604d6b4e69SJiang Liu 	if (node != NUMA_NO_NODE && !node_online(node))
4614d6b4e69SJiang Liu 		node = NUMA_NO_NODE;
46262f420f8SGary Hade 
4634d6b4e69SJiang Liu 	return node;
464593669c2SJiang Liu }
465593669c2SJiang Liu 
pci_acpi_root_init_info(struct acpi_pci_root_info * ci)4664d6b4e69SJiang Liu static int pci_acpi_root_init_info(struct acpi_pci_root_info *ci)
4674723d0f2SBjorn Helgaas {
4684d6b4e69SJiang Liu 	return setup_mcfg_map(ci);
4694723d0f2SBjorn Helgaas }
4704723d0f2SBjorn Helgaas 
pci_acpi_root_release_info(struct acpi_pci_root_info * ci)4714d6b4e69SJiang Liu static void pci_acpi_root_release_info(struct acpi_pci_root_info *ci)
472fd3b0c1eSYinghai Lu {
4734d6b4e69SJiang Liu 	teardown_mcfg_map(ci);
4744d6b4e69SJiang Liu 	kfree(container_of(ci, struct pci_root_info, common));
475baa495d9SYinghai Lu }
476baa495d9SYinghai Lu 
4772c62e849SJiang Liu /*
4782c62e849SJiang Liu  * An IO port or MMIO resource assigned to a PCI host bridge may be
4792c62e849SJiang Liu  * consumed by the host bridge itself or available to its child
4802c62e849SJiang Liu  * bus/devices. The ACPI specification defines a bit (Producer/Consumer)
4812c62e849SJiang Liu  * to tell whether the resource is consumed by the host bridge itself,
4822c62e849SJiang Liu  * but firmware hasn't used that bit consistently, so we can't rely on it.
4832c62e849SJiang Liu  *
4842c62e849SJiang Liu  * On x86 and IA64 platforms, all IO port and MMIO resources are assumed
4852c62e849SJiang Liu  * to be available to child bus/devices except one special case:
4862c62e849SJiang Liu  *     IO port [0xCF8-0xCFF] is consumed by the host bridge itself
4872c62e849SJiang Liu  *     to access PCI configuration space.
4882c62e849SJiang Liu  *
4892c62e849SJiang Liu  * So explicitly filter out PCI CFG IO ports[0xCF8-0xCFF].
4902c62e849SJiang Liu  */
resource_is_pcicfg_ioport(struct resource * res)4912c62e849SJiang Liu static bool resource_is_pcicfg_ioport(struct resource *res)
4922c62e849SJiang Liu {
4932c62e849SJiang Liu 	return (res->flags & IORESOURCE_IO) &&
4942c62e849SJiang Liu 		res->start == 0xCF8 && res->end == 0xCFF;
4952c62e849SJiang Liu }
4962c62e849SJiang Liu 
pci_acpi_root_prepare_resources(struct acpi_pci_root_info * ci)4974d6b4e69SJiang Liu static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
49862f420f8SGary Hade {
4994d6b4e69SJiang Liu 	struct acpi_device *device = ci->bridge;
5004d6b4e69SJiang Liu 	int busnum = ci->root->secondary.start;
50163f1789eSJiang Liu 	struct resource_entry *entry, *tmp;
5024d6b4e69SJiang Liu 	int status;
50362f420f8SGary Hade 
5044d6b4e69SJiang Liu 	status = acpi_pci_probe_root_resources(ci);
5054c5e242dSBjorn Helgaas 
5064d6b4e69SJiang Liu 	if (pci_use_crs) {
5074d6b4e69SJiang Liu 		resource_list_for_each_entry_safe(entry, tmp, &ci->resources)
5084d6b4e69SJiang Liu 			if (resource_is_pcicfg_ioport(entry->res))
50963f1789eSJiang Liu 				resource_list_destroy_entry(entry);
5104d6b4e69SJiang Liu 		return status;
51162f420f8SGary Hade 	}
5124d6b4e69SJiang Liu 
5134d6b4e69SJiang Liu 	resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
5144d6b4e69SJiang Liu 		dev_printk(KERN_DEBUG, &device->dev,
5154d6b4e69SJiang Liu 			   "host bridge window %pR (ignored)\n", entry->res);
5164d6b4e69SJiang Liu 		resource_list_destroy_entry(entry);
51763f1789eSJiang Liu 	}
5184d6b4e69SJiang Liu 	x86_pci_root_bus_resources(busnum, &ci->resources);
5194d6b4e69SJiang Liu 
5204d6b4e69SJiang Liu 	return 0;
5214d6b4e69SJiang Liu }
5224d6b4e69SJiang Liu 
5234d6b4e69SJiang Liu static struct acpi_pci_root_ops acpi_pci_root_ops = {
5244d6b4e69SJiang Liu 	.pci_ops = &pci_root_ops,
5254d6b4e69SJiang Liu 	.init_info = pci_acpi_root_init_info,
5264d6b4e69SJiang Liu 	.release_info = pci_acpi_root_release_info,
5274d6b4e69SJiang Liu 	.prepare_resources = pci_acpi_root_prepare_resources,
5284d6b4e69SJiang Liu };
52962f420f8SGary Hade 
pci_acpi_scan_root(struct acpi_pci_root * root)530a18e3690SGreg Kroah-Hartman struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
531fb9aa6f1SThomas Gleixner {
53257283776SBjorn Helgaas 	int domain = root->segment;
53357283776SBjorn Helgaas 	int busnum = root->secondary.start;
5344d6b4e69SJiang Liu 	int node = pci_acpi_root_get_node(root);
5358928d5a6SBjorn Helgaas 	struct pci_bus *bus;
536fb9aa6f1SThomas Gleixner 
5371f09b09bSBjorn Helgaas 	if (pci_ignore_seg)
538a3669868SJiang Liu 		root->segment = domain = 0;
5391f09b09bSBjorn Helgaas 
540a79e4198SJeff Garzik 	if (domain && !pci_domains_supported) {
541d91482bbSBjorn Helgaas 		pr_warn("pci_bus %04x:%02x: ignored (multiple domains not supported)\n",
5422a6bed83SBjorn Helgaas 		        domain, busnum);
543a79e4198SJeff Garzik 		return NULL;
544a79e4198SJeff Garzik 	}
545a79e4198SJeff Garzik 
546b87e81e5Syakui.zhao@intel.com 	bus = pci_find_bus(domain, busnum);
547b87e81e5Syakui.zhao@intel.com 	if (bus) {
548b87e81e5Syakui.zhao@intel.com 		/*
549affbda86SBjorn Helgaas 		 * If the desired bus has been scanned already, replace
550affbda86SBjorn Helgaas 		 * its bus->sysdata.
551b87e81e5Syakui.zhao@intel.com 		 */
5524d6b4e69SJiang Liu 		struct pci_sysdata sd = {
5534d6b4e69SJiang Liu 			.domain = domain,
5544d6b4e69SJiang Liu 			.node = node,
5554d6b4e69SJiang Liu 			.companion = root->device
5564d6b4e69SJiang Liu 		};
557593669c2SJiang Liu 
5584d6b4e69SJiang Liu 		memcpy(bus->sysdata, &sd, sizeof(sd));
559593669c2SJiang Liu 	} else {
5604d6b4e69SJiang Liu 		struct pci_root_info *info;
561fd3b0c1eSYinghai Lu 
562d193631bSPunit Agrawal 		info = kzalloc(sizeof(*info), GFP_KERNEL);
5634d6b4e69SJiang Liu 		if (!info)
5644d6b4e69SJiang Liu 			dev_err(&root->device->dev,
5654d6b4e69SJiang Liu 				"pci_bus %04x:%02x: ignored (out of memory)\n",
5664d6b4e69SJiang Liu 				domain, busnum);
5674d6b4e69SJiang Liu 		else {
5684d6b4e69SJiang Liu 			info->sd.domain = domain;
5694d6b4e69SJiang Liu 			info->sd.node = node;
5704d6b4e69SJiang Liu 			info->sd.companion = root->device;
5714d6b4e69SJiang Liu 			bus = acpi_pci_root_create(root, &acpi_pci_root_ops,
5724d6b4e69SJiang Liu 						   &info->common, &info->sd);
573fd3b0c1eSYinghai Lu 		}
574626fdfecSYinghai Lu 	}
575b87e81e5Syakui.zhao@intel.com 
576b03e7495SJon Mason 	/* After the PCI-E bus has been walked and all devices discovered,
577b03e7495SJon Mason 	 * configure any settings of the fabric that might be necessary.
578b03e7495SJon Mason 	 */
579b03e7495SJon Mason 	if (bus) {
580b03e7495SJon Mason 		struct pci_bus *child;
581a58674ffSBjorn Helgaas 		list_for_each_entry(child, &bus->children, node)
582a58674ffSBjorn Helgaas 			pcie_bus_configure_settings(child);
583b03e7495SJon Mason 	}
584b03e7495SJon Mason 
585fb9aa6f1SThomas Gleixner 	return bus;
586fb9aa6f1SThomas Gleixner }
587fb9aa6f1SThomas Gleixner 
pcibios_root_bridge_prepare(struct pci_host_bridge * bridge)5886c0cc950SRafael J. Wysocki int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
5896c0cc950SRafael J. Wysocki {
590dc4fdaf0SRafael J. Wysocki 	/*
591dc4fdaf0SRafael J. Wysocki 	 * We pass NULL as parent to pci_create_root_bus(), so if it is not NULL
592dc4fdaf0SRafael J. Wysocki 	 * here, pci_create_root_bus() has been called by someone else and
593dc4fdaf0SRafael J. Wysocki 	 * sysdata is likely to be different from what we expect.  Let it go in
594dc4fdaf0SRafael J. Wysocki 	 * that case.
595dc4fdaf0SRafael J. Wysocki 	 */
596dc4fdaf0SRafael J. Wysocki 	if (!bridge->dev.parent) {
5976c0cc950SRafael J. Wysocki 		struct pci_sysdata *sd = bridge->bus->sysdata;
5987b199811SRafael J. Wysocki 		ACPI_COMPANION_SET(&bridge->dev, sd->companion);
599dc4fdaf0SRafael J. Wysocki 	}
6006c0cc950SRafael J. Wysocki 	return 0;
6016c0cc950SRafael J. Wysocki }
6026c0cc950SRafael J. Wysocki 
pci_acpi_init(void)6038dd779b1SRobert Richter int __init pci_acpi_init(void)
604fb9aa6f1SThomas Gleixner {
605fb9aa6f1SThomas Gleixner 	struct pci_dev *dev = NULL;
606fb9aa6f1SThomas Gleixner 
607fb9aa6f1SThomas Gleixner 	if (acpi_noirq)
608b72d0db9SThomas Gleixner 		return -ENODEV;
609fb9aa6f1SThomas Gleixner 
610d91482bbSBjorn Helgaas 	pr_info("Using ACPI for IRQ routing\n");
611fb9aa6f1SThomas Gleixner 	acpi_irq_penalty_init();
612fb9aa6f1SThomas Gleixner 	pcibios_enable_irq = acpi_pci_irq_enable;
613fb9aa6f1SThomas Gleixner 	pcibios_disable_irq = acpi_pci_irq_disable;
614ab3b3793SThomas Gleixner 	x86_init.pci.init_irq = x86_init_noop;
615fb9aa6f1SThomas Gleixner 
616fb9aa6f1SThomas Gleixner 	if (pci_routeirq) {
617fb9aa6f1SThomas Gleixner 		/*
618fb9aa6f1SThomas Gleixner 		 * PCI IRQ routing is set up by pci_enable_device(), but we
619fb9aa6f1SThomas Gleixner 		 * also do it here in case there are still broken drivers that
620fb9aa6f1SThomas Gleixner 		 * don't use pci_enable_device().
621fb9aa6f1SThomas Gleixner 		 */
622d91482bbSBjorn Helgaas 		pr_info("Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
623fb9aa6f1SThomas Gleixner 		for_each_pci_dev(dev)
624fb9aa6f1SThomas Gleixner 			acpi_pci_irq_enable(dev);
625657472e9SBjorn Helgaas 	}
626fb9aa6f1SThomas Gleixner 
627fb9aa6f1SThomas Gleixner 	return 0;
628fb9aa6f1SThomas Gleixner }
629