xref: /linux/drivers/platform/x86/p2sb.c (revision c6fbb759)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Primary to Sideband (P2SB) bridge access support
4  *
5  * Copyright (c) 2017, 2021-2022 Intel Corporation.
6  *
7  * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8  *	    Jonathan Yong <jonathan.yong@intel.com>
9  */
10 
11 #include <linux/bits.h>
12 #include <linux/export.h>
13 #include <linux/pci.h>
14 #include <linux/platform_data/x86/p2sb.h>
15 
16 #include <asm/cpu_device_id.h>
17 #include <asm/intel-family.h>
18 
19 #define P2SBC			0xe0
20 #define P2SBC_HIDE		BIT(8)
21 
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)),
30 	{}
31 };
32 
33 static int p2sb_get_devfn(unsigned int *devfn)
34 {
35 	const struct x86_cpu_id *id;
36 
37 	id = x86_match_cpu(p2sb_cpu_ids);
38 	if (!id)
39 		return -ENODEV;
40 
41 	*devfn = (unsigned int)id->driver_data;
42 	return 0;
43 }
44 
45 /* Copy resource from the first BAR of the device in question */
46 static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
47 {
48 	struct resource *bar0 = &pdev->resource[0];
49 
50 	/* Make sure we have no dangling pointers in the output */
51 	memset(mem, 0, sizeof(*mem));
52 
53 	/*
54 	 * We copy only selected fields from the original resource.
55 	 * Because a PCI device will be removed soon, we may not use
56 	 * any allocated data, hence we may not copy any pointers.
57 	 */
58 	mem->start = bar0->start;
59 	mem->end = bar0->end;
60 	mem->flags = bar0->flags;
61 	mem->desc = bar0->desc;
62 
63 	return 0;
64 }
65 
66 static int p2sb_scan_and_read(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
67 {
68 	struct pci_dev *pdev;
69 	int ret;
70 
71 	pdev = pci_scan_single_device(bus, devfn);
72 	if (!pdev)
73 		return -ENODEV;
74 
75 	ret = p2sb_read_bar0(pdev, mem);
76 
77 	pci_stop_and_remove_bus_device(pdev);
78 	return ret;
79 }
80 
81 /**
82  * p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
83  * @bus: PCI bus to communicate with
84  * @devfn: PCI slot and function to communicate with
85  * @mem: memory resource to be filled in
86  *
87  * The BIOS prevents the P2SB device from being enumerated by the PCI
88  * subsystem, so we need to unhide and hide it back to lookup the BAR.
89  *
90  * if @bus is NULL, the bus 0 in domain 0 will be used.
91  * If @devfn is 0, it will be replaced by devfn of the P2SB device.
92  *
93  * Caller must provide a valid pointer to @mem.
94  *
95  * Locking is handled by pci_rescan_remove_lock mutex.
96  *
97  * Return:
98  * 0 on success or appropriate errno value on error.
99  */
100 int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
101 {
102 	struct pci_dev *pdev_p2sb;
103 	unsigned int devfn_p2sb;
104 	u32 value = P2SBC_HIDE;
105 	int ret;
106 
107 	/* Get devfn for P2SB device itself */
108 	ret = p2sb_get_devfn(&devfn_p2sb);
109 	if (ret)
110 		return ret;
111 
112 	/* if @bus is NULL, use bus 0 in domain 0 */
113 	bus = bus ?: pci_find_bus(0, 0);
114 
115 	/*
116 	 * Prevent concurrent PCI bus scan from seeing the P2SB device and
117 	 * removing via sysfs while it is temporarily exposed.
118 	 */
119 	pci_lock_rescan_remove();
120 
121 	/* Unhide the P2SB device, if needed */
122 	pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value);
123 	if (value & P2SBC_HIDE)
124 		pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, 0);
125 
126 	pdev_p2sb = pci_scan_single_device(bus, devfn_p2sb);
127 	if (devfn)
128 		ret = p2sb_scan_and_read(bus, devfn, mem);
129 	else
130 		ret = p2sb_read_bar0(pdev_p2sb, mem);
131 	pci_stop_and_remove_bus_device(pdev_p2sb);
132 
133 	/* Hide the P2SB device, if it was hidden */
134 	if (value & P2SBC_HIDE)
135 		pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, P2SBC_HIDE);
136 
137 	pci_unlock_rescan_remove();
138 
139 	if (ret)
140 		return ret;
141 
142 	if (mem->flags == 0)
143 		return -ENODEV;
144 
145 	return 0;
146 }
147 EXPORT_SYMBOL_GPL(p2sb_bar);
148