1 // SPDX-License-Identifier: GPL-2.0
3 * Primary to Sideband (P2SB) bridge access support
5 * Copyright (c) 2017, 2021-2022 Intel Corporation.
11 #include <linux/bits.h>
12 #include <linux/export.h>
13 #include <linux/pci.h>
14 #include <linux/platform_data/x86/p2sb.h>
16 #include <asm/cpu_device_id.h>
17 #include <asm/intel-family.h>
20 #define P2SBC_HIDE BIT(8)
22 static const struct x86_cpu_id p2sb_cpu_ids[] = {
23 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, PCI_DEVFN(13, 0)),
24 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, PCI_DEVFN(31, 1)),
25 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_D, PCI_DEVFN(31, 1)),
26 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, PCI_DEVFN(31, 1)),
27 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, PCI_DEVFN(31, 1)),
28 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, PCI_DEVFN(31, 1)),
29 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, PCI_DEVFN(31, 1)),
33 static int p2sb_get_devfn(unsigned int *devfn)
35 const struct x86_cpu_id *id;
37 id = x86_match_cpu(p2sb_cpu_ids);
41 *devfn = (unsigned int)id->driver_data;
45 static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
47 /* Copy resource from the first BAR of the device in question */
48 *mem = pdev->resource[0];
52 static int p2sb_scan_and_read(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
57 pdev = pci_scan_single_device(bus, devfn);
61 ret = p2sb_read_bar0(pdev, mem);
63 pci_stop_and_remove_bus_device(pdev);
68 * p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
69 * @bus: PCI bus to communicate with
70 * @devfn: PCI slot and function to communicate with
71 * @mem: memory resource to be filled in
73 * The BIOS prevents the P2SB device from being enumerated by the PCI
74 * subsystem, so we need to unhide and hide it back to lookup the BAR.
76 * if @bus is NULL, the bus 0 in domain 0 will be used.
77 * If @devfn is 0, it will be replaced by devfn of the P2SB device.
79 * Caller must provide a valid pointer to @mem.
81 * Locking is handled by pci_rescan_remove_lock mutex.
84 * 0 on success or appropriate errno value on error.
86 int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
88 struct pci_dev *pdev_p2sb;
89 unsigned int devfn_p2sb;
90 u32 value = P2SBC_HIDE;
93 /* Get devfn for P2SB device itself */
94 ret = p2sb_get_devfn(&devfn_p2sb);
98 /* if @bus is NULL, use bus 0 in domain 0 */
99 bus = bus ?: pci_find_bus(0, 0);
102 * Prevent concurrent PCI bus scan from seeing the P2SB device and
103 * removing via sysfs while it is temporarily exposed.
105 pci_lock_rescan_remove();
107 /* Unhide the P2SB device, if needed */
108 pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value);
109 if (value & P2SBC_HIDE)
110 pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, 0);
112 pdev_p2sb = pci_scan_single_device(bus, devfn_p2sb);
114 ret = p2sb_scan_and_read(bus, devfn, mem);
116 ret = p2sb_read_bar0(pdev_p2sb, mem);
117 pci_stop_and_remove_bus_device(pdev_p2sb);
119 /* Hide the P2SB device, if it was hidden */
120 if (value & P2SBC_HIDE)
121 pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, P2SBC_HIDE);
123 pci_unlock_rescan_remove();
133 EXPORT_SYMBOL_GPL(p2sb_bar);