xref: /linux/drivers/platform/x86/p2sb.c (revision 6d9b262afe0ec1d6e0ef99321ca9d6b921310471)
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 #define P2SB_DEVFN_DEFAULT	PCI_DEVFN(31, 1)
23 #define P2SB_DEVFN_GOLDMONT	PCI_DEVFN(13, 0)
24 #define SPI_DEVFN_GOLDMONT	PCI_DEVFN(13, 2)
25 
26 static const struct x86_cpu_id p2sb_cpu_ids[] = {
27 	X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, P2SB_DEVFN_GOLDMONT),
28 	{}
29 };
30 
31 /*
32  * Cache BAR0 of P2SB device functions 0 to 7.
33  * TODO: The constant 8 is the number of functions that PCI specification
34  *       defines. Same definitions exist tree-wide. Unify this definition and
35  *       the other definitions then move to include/uapi/linux/pci.h.
36  */
37 #define NR_P2SB_RES_CACHE 8
38 
39 struct p2sb_res_cache {
40 	u32 bus_dev_id;
41 	struct resource res;
42 };
43 
44 static struct p2sb_res_cache p2sb_resources[NR_P2SB_RES_CACHE];
45 
46 static void p2sb_get_devfn(unsigned int *devfn)
47 {
48 	unsigned int fn = P2SB_DEVFN_DEFAULT;
49 	const struct x86_cpu_id *id;
50 
51 	id = x86_match_cpu(p2sb_cpu_ids);
52 	if (id)
53 		fn = (unsigned int)id->driver_data;
54 
55 	*devfn = fn;
56 }
57 
58 static bool p2sb_valid_resource(struct resource *res)
59 {
60 	if (res->flags)
61 		return true;
62 
63 	return false;
64 }
65 
66 /* Copy resource from the first BAR of the device in question */
67 static void p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
68 {
69 	struct resource *bar0 = pci_resource_n(pdev, 0);
70 
71 	/* Make sure we have no dangling pointers in the output */
72 	memset(mem, 0, sizeof(*mem));
73 
74 	/*
75 	 * We copy only selected fields from the original resource.
76 	 * Because a PCI device will be removed soon, we may not use
77 	 * any allocated data, hence we may not copy any pointers.
78 	 */
79 	mem->start = bar0->start;
80 	mem->end = bar0->end;
81 	mem->flags = bar0->flags;
82 	mem->desc = bar0->desc;
83 }
84 
85 static void p2sb_scan_and_cache_devfn(struct pci_bus *bus, unsigned int devfn)
86 {
87 	struct p2sb_res_cache *cache = &p2sb_resources[PCI_FUNC(devfn)];
88 	struct pci_dev *pdev;
89 
90 	pdev = pci_scan_single_device(bus, devfn);
91 	if (!pdev)
92 		return;
93 
94 	p2sb_read_bar0(pdev, &cache->res);
95 	cache->bus_dev_id = bus->dev.id;
96 
97 	pci_stop_and_remove_bus_device(pdev);
98 }
99 
100 static int p2sb_scan_and_cache(struct pci_bus *bus, unsigned int devfn)
101 {
102 	/* Scan the P2SB device and cache its BAR0 */
103 	p2sb_scan_and_cache_devfn(bus, devfn);
104 
105 	/* On Goldmont p2sb_bar() also gets called for the SPI controller */
106 	if (devfn == P2SB_DEVFN_GOLDMONT)
107 		p2sb_scan_and_cache_devfn(bus, SPI_DEVFN_GOLDMONT);
108 
109 	if (!p2sb_valid_resource(&p2sb_resources[PCI_FUNC(devfn)].res))
110 		return -ENOENT;
111 
112 	return 0;
113 }
114 
115 static struct pci_bus *p2sb_get_bus(struct pci_bus *bus)
116 {
117 	static struct pci_bus *p2sb_bus;
118 
119 	bus = bus ?: p2sb_bus;
120 	if (bus)
121 		return bus;
122 
123 	/* Assume P2SB is on the bus 0 in domain 0 */
124 	p2sb_bus = pci_find_bus(0, 0);
125 	return p2sb_bus;
126 }
127 
128 static int p2sb_cache_resources(void)
129 {
130 	unsigned int devfn_p2sb;
131 	u32 value = P2SBC_HIDE;
132 	struct pci_bus *bus;
133 	u16 class;
134 	int ret;
135 
136 	/* Get devfn for P2SB device itself */
137 	p2sb_get_devfn(&devfn_p2sb);
138 
139 	bus = p2sb_get_bus(NULL);
140 	if (!bus)
141 		return -ENODEV;
142 
143 	/*
144 	 * When a device with same devfn exists and its device class is not
145 	 * PCI_CLASS_MEMORY_OTHER for P2SB, do not touch it.
146 	 */
147 	pci_bus_read_config_word(bus, devfn_p2sb, PCI_CLASS_DEVICE, &class);
148 	if (!PCI_POSSIBLE_ERROR(class) && class != PCI_CLASS_MEMORY_OTHER)
149 		return -ENODEV;
150 
151 	/*
152 	 * Prevent concurrent PCI bus scan from seeing the P2SB device and
153 	 * removing via sysfs while it is temporarily exposed.
154 	 */
155 	pci_lock_rescan_remove();
156 
157 	/*
158 	 * The BIOS prevents the P2SB device from being enumerated by the PCI
159 	 * subsystem, so we need to unhide and hide it back to lookup the BAR.
160 	 * Unhide the P2SB device here, if needed.
161 	 */
162 	pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value);
163 	if (value & P2SBC_HIDE)
164 		pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, 0);
165 
166 	ret = p2sb_scan_and_cache(bus, devfn_p2sb);
167 
168 	/* Hide the P2SB device, if it was hidden */
169 	if (value & P2SBC_HIDE)
170 		pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, P2SBC_HIDE);
171 
172 	pci_unlock_rescan_remove();
173 
174 	return ret;
175 }
176 
177 /**
178  * p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
179  * @bus: PCI bus to communicate with
180  * @devfn: PCI slot and function to communicate with
181  * @mem: memory resource to be filled in
182  *
183  * If @bus is NULL, the bus 0 in domain 0 will be used.
184  * If @devfn is 0, it will be replaced by devfn of the P2SB device.
185  *
186  * Caller must provide a valid pointer to @mem.
187  *
188  * Return:
189  * 0 on success or appropriate errno value on error.
190  */
191 int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
192 {
193 	struct p2sb_res_cache *cache;
194 
195 	bus = p2sb_get_bus(bus);
196 	if (!bus)
197 		return -ENODEV;
198 
199 	if (!devfn)
200 		p2sb_get_devfn(&devfn);
201 
202 	cache = &p2sb_resources[PCI_FUNC(devfn)];
203 	if (cache->bus_dev_id != bus->dev.id)
204 		return -ENODEV;
205 
206 	if (!p2sb_valid_resource(&cache->res))
207 		return -ENOENT;
208 
209 	memcpy(mem, &cache->res, sizeof(*mem));
210 	return 0;
211 }
212 EXPORT_SYMBOL_GPL(p2sb_bar);
213 
214 static int __init p2sb_fs_init(void)
215 {
216 	p2sb_cache_resources();
217 	return 0;
218 }
219 
220 /*
221  * pci_rescan_remove_lock to avoid access to unhidden P2SB devices can
222  * not be locked in sysfs pci bus rescan path because of deadlock. To
223  * avoid the deadlock, access to P2SB devices with the lock at an early
224  * step in kernel initialization and cache required resources. This
225  * should happen after subsys_initcall which initializes PCI subsystem
226  * and before device_initcall which requires P2SB resources.
227  */
228 fs_initcall(p2sb_fs_init);
229