xref: /netbsd-src/sys/dev/acpi/acpi_pci.c (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
1 /* $NetBSD: acpi_pci.c,v 1.21 2018/04/07 15:49:52 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christoph Egger and Gregoire Sutre.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.21 2018/04/07 15:49:52 christos Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/device.h>
36 #include <sys/kmem.h>
37 #include <sys/systm.h>
38 
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcidevs.h>
42 #include <dev/pci/ppbreg.h>
43 
44 #include <dev/acpi/acpireg.h>
45 #include <dev/acpi/acpivar.h>
46 #include <dev/acpi/acpi_pci.h>
47 
48 #include "locators.h"
49 
50 #define _COMPONENT	  ACPI_BUS_COMPONENT
51 ACPI_MODULE_NAME	  ("acpi_pci")
52 
53 #define ACPI_HILODWORD(x) ACPI_HIWORD(ACPI_LODWORD((x)))
54 #define ACPI_LOLODWORD(x) ACPI_LOWORD(ACPI_LODWORD((x)))
55 
56 static ACPI_STATUS	  acpi_pcidev_pciroot_bus(ACPI_HANDLE, uint16_t *);
57 static ACPI_STATUS	  acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *,
58 							   void *);
59 
60 /*
61  * Regarding PCI Segment Groups (ACPI 4.0, p. 277):
62  *
63  * "The optional _SEG object is located under a PCI host bridge and
64  *  evaluates to an integer that describes the PCI Segment Group (see PCI
65  *  Firmware Specification v3.0)."
66  *
67  * "PCI Segment Group is purely a software concept managed by system
68  *  firmware and used by OSPM. It is a logical collection of PCI buses
69  *  (or bus segments). It is a way to logically group the PCI bus segments
70  *  and PCI Express Hierarchies. _SEG is a level higher than _BBN."
71  *
72  * "PCI Segment Group supports more than 256 buses in a system by allowing
73  *  the reuse of the PCI bus numbers.  Within each PCI Segment Group, the bus
74  *  numbers for the PCI buses must be unique. PCI buses in different PCI
75  *  Segment Group are permitted to have the same bus number."
76  */
77 
78 /*
79  * Regarding PCI Base Bus Numbers (ACPI 4.0, p. 277):
80  *
81  * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
82  *  number that the BIOS assigns.  This is needed to access a PCI_Config
83  *  operation region for the specified bus.  The _BBN object is located under
84  *  a PCI host bridge and must be unique for every host bridge within a
85  *  segment since it is the PCI bus number."
86  *
87  * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
88  *
89  * "For a multiple root bus machine, _BBN is required for each bus.  _BBN
90  *  should provide the bus number assigned to this bus by the BIOS at boot
91  *  time."
92  */
93 
94 /*
95  * acpi_pcidev_pciroot_bus:
96  *
97  *	Derive the PCI bus number of a PCI root bridge from its resources.
98  *	If successful, return AE_OK and fill *busp.  Otherwise, return an
99  *	exception code and leave *busp unchanged.
100  */
101 static ACPI_STATUS
102 acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
103 {
104 	ACPI_STATUS rv;
105 	int32_t bus;
106 
107 	bus = -1;
108 
109 	/*
110 	 * XXX:	Use the ACPI resource parsing functions (acpi_resource.c)
111 	 *	once bus number ranges have been implemented there.
112 	 */
113 	rv = AcpiWalkResources(handle, "_CRS",
114 	    acpi_pcidev_pciroot_bus_callback, &bus);
115 
116 	if (ACPI_FAILURE(rv))
117 		return rv;
118 
119 	if (bus == -1)
120 		return AE_NOT_EXIST;
121 
122 	/* Here it holds that 0 <= bus <= 0xFFFF. */
123 	*busp = (uint16_t)bus;
124 
125 	return rv;
126 }
127 
128 static ACPI_STATUS
129 acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
130 {
131 	ACPI_RESOURCE_ADDRESS64 addr64;
132 	int32_t *bus = context;
133 
134 	/* Always continue the walk by returning AE_OK. */
135 	if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
136 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
137 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
138 		return AE_OK;
139 
140 	if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
141 		return AE_OK;
142 
143 	if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
144 		return AE_OK;
145 
146 	if (*bus != -1)
147 		return AE_ALREADY_EXISTS;
148 
149 	if (addr64.Address.Minimum > 0xFFFF)
150 		return AE_BAD_DATA;
151 
152 	*bus = (int32_t)addr64.Address.Minimum;
153 
154 	return AE_OK;
155 }
156 
157 /*
158  * acpi_pcidev_scan:
159  *
160  *	Scan the ACPI device tree for PCI devices.  A node is detected as a
161  *	PCI device if it has an ancestor that is a PCI root bridge and such
162  *	that all intermediate nodes are PCI-to-PCI bridges.  Depth-first
163  *	recursive implementation.
164  *
165  *	PCI root bridges do not necessarily contain an _ADR, since they already
166  *	contain an _HID (ACPI 4.0a, p. 197).  However we require an _ADR for
167  *	all non-root PCI devices.
168  */
169 ACPI_STATUS
170 acpi_pcidev_scan(struct acpi_devnode *ad)
171 {
172 	struct acpi_devnode *child;
173 	struct acpi_pci_info *ap;
174 	ACPI_INTEGER val;
175 	ACPI_STATUS rv;
176 
177 	ad->ad_pciinfo = NULL;
178 
179 	/*
180 	 * We attach PCI information only to devices that are present,
181 	 * enabled, and functioning properly.
182 	 * Note: there is a possible race condition, because _STA may
183 	 * have changed since ad->ad_devinfo->CurrentStatus was set.
184 	 */
185 	if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
186 		goto rec;
187 
188 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
189 
190 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
191 
192 		/*
193 		 * If no _SEG exist, all PCI bus segments are assumed
194 		 * to be in the PCI segment group 0 (ACPI 4.0, p. 277).
195 		 * The segment group number is conveyed in the lower
196 		 * 16 bits of _SEG (the other bits are all reserved).
197 		 */
198 		rv = acpi_eval_integer(ad->ad_handle, "_SEG", &val);
199 
200 		if (ACPI_SUCCESS(rv))
201 			ap->ap_segment = ACPI_LOWORD(val);
202 
203 		/* Try to get downstream bus number using _CRS first. */
204 		rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_downbus);
205 
206 		if (ACPI_FAILURE(rv)) {
207 			rv = acpi_eval_integer(ad->ad_handle, "_BBN", &val);
208 
209 			if (ACPI_SUCCESS(rv))
210 				ap->ap_downbus = ACPI_LOWORD(val);
211 		}
212 
213 		if (ap->ap_downbus > 255) {
214 			aprint_error_dev(ad->ad_root,
215 			    "invalid PCI downstream bus for %s\n", ad->ad_name);
216 			kmem_free(ap, sizeof(*ap));
217 			goto rec;
218 		}
219 
220 		ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
221 
222 		/*
223 		 * This ACPI node denotes a PCI root bridge, but it may also
224 		 * denote a PCI device on the bridge's downstream bus segment.
225 		 */
226 		if (ad->ad_devinfo->Valid & ACPI_VALID_ADR) {
227 			ap->ap_bus = ap->ap_downbus;
228 			ap->ap_device =
229 			    ACPI_HILODWORD(ad->ad_devinfo->Address);
230 			ap->ap_function =
231 			    ACPI_LOLODWORD(ad->ad_devinfo->Address);
232 
233 			if (ap->ap_device > 31 ||
234 			    (ap->ap_function > 7 && ap->ap_function != 0xFFFF))
235 				aprint_error_dev(ad->ad_root,
236 				    "invalid PCI address for %s\n", ad->ad_name);
237 			else
238 				ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
239 		}
240 
241 		ad->ad_pciinfo = ap;
242 
243 		goto rec;
244 	}
245 
246 	if ((ad->ad_parent != NULL) &&
247 	    (ad->ad_parent->ad_pciinfo != NULL) &&
248 	    (ad->ad_parent->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
249 	    (ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
250 
251 		/*
252 		 * Our parent is a PCI root bridge or a PCI-to-PCI
253 		 * bridge. We have the same PCI segment number, and
254 		 * our bus number is its downstream bus number.
255 		 */
256 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
257 
258 		ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
259 		ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
260 
261 		ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
262 		ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
263 
264 		if (ap->ap_device > 31 ||
265 		    (ap->ap_function > 7 && ap->ap_function != 0xFFFF)) {
266 			aprint_error_dev(ad->ad_root,
267 			    "invalid PCI address for %s\n", ad->ad_name);
268 			kmem_free(ap, sizeof(*ap));
269 			goto rec;
270 		}
271 
272 		ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
273 
274 		if (ap->ap_function == 0xFFFF) {
275 			/*
276 			 * Assume that this device is not a PCI-to-PCI bridge.
277 			 * XXX: Do we need to be smarter?
278 			 */
279 		} else {
280 			/*
281 			 * Check whether this device is a PCI-to-PCI
282 			 * bridge and get its secondary bus number.
283 			 */
284 			rv = acpi_pcidev_ppb_downbus(ap->ap_segment, ap->ap_bus,
285 			    ap->ap_device, ap->ap_function, &ap->ap_downbus);
286 
287 			if (ACPI_SUCCESS(rv))
288 				ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
289 		}
290 
291 		ad->ad_pciinfo = ap;
292 
293 		goto rec;
294 	}
295 
296 rec:
297 	SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
298 		rv = acpi_pcidev_scan(child);
299 
300 		if (ACPI_FAILURE(rv))
301 			return rv;
302 	}
303 
304 	return AE_OK;
305 }
306 
307 /*
308  * acpi_pcidev_ppb_downbus:
309  *
310  *	Retrieve the secondary bus number of the PCI-to-PCI bridge having the
311  *	given PCI id.  If successful, return AE_OK and fill *downbus.
312  *	Otherwise, return an exception code and leave *downbus unchanged.
313  *
314  * XXX	Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
315  */
316 ACPI_STATUS
317 acpi_pcidev_ppb_downbus(uint16_t segment, uint16_t bus, uint16_t device,
318     uint16_t function, uint16_t *downbus)
319 {
320 	struct acpi_softc *sc = acpi_softc;
321 	pci_chipset_tag_t pc;
322 	pcitag_t tag;
323 	pcireg_t val;
324 
325 	if (bus > 255 || device > 31 || function > 7)
326 		return AE_BAD_PARAMETER;
327 
328 	pc = sc->sc_pc;
329 
330 	tag = pci_make_tag(pc, bus, device, function);
331 
332 	/* Check that this device exists. */
333 	val = pci_conf_read(pc, tag, PCI_ID_REG);
334 
335 	if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
336 	    PCI_VENDOR(val) == 0)
337 		return AE_NOT_EXIST;
338 
339 	/* Check that this device is a PCI-to-PCI bridge. */
340 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
341 
342 	if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
343 		return AE_TYPE;
344 
345 	/* This is a PCI-to-PCI bridge.  Get its secondary bus#. */
346 	val = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
347 	*downbus = PPB_BUSINFO_SECONDARY(val);
348 
349 	return AE_OK;
350 }
351 
352 /*
353  * acpi_pcidev_find:
354  *
355  *      Finds a PCI device in the ACPI name space.
356  *
357  *      Returns an ACPI device node on success and NULL on failure.
358  */
359 struct acpi_devnode *
360 acpi_pcidev_find(uint16_t segment, uint16_t bus,
361     uint16_t device, uint16_t function)
362 {
363 	struct acpi_softc *sc = acpi_softc;
364 	struct acpi_devnode *ad;
365 
366 	if (sc == NULL)
367 		return NULL;
368 
369 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
370 
371 		if (ad->ad_pciinfo != NULL &&
372 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_DEVICE) &&
373 		    ad->ad_pciinfo->ap_segment == segment &&
374 		    ad->ad_pciinfo->ap_bus == bus &&
375 		    ad->ad_pciinfo->ap_device == device &&
376 		    ad->ad_pciinfo->ap_function == function)
377 			return ad;
378 	}
379 
380 	return NULL;
381 }
382 
383 
384 /*
385  * acpi_pcidev_find_dev:
386  *
387  *	Returns the device corresponding to the given PCI info, or NULL
388  *	if it doesn't exist.
389  */
390 device_t
391 acpi_pcidev_find_dev(struct acpi_devnode *ad)
392 {
393 	struct acpi_pci_info *ap;
394 	struct pci_softc *pci;
395 	device_t dv, pr;
396 	deviter_t di;
397 
398 	if (ad == NULL)
399 		return NULL;
400 
401 	if (ad->ad_pciinfo == NULL)
402 		return NULL;
403 
404 	ap = ad->ad_pciinfo;
405 
406 	if (ap->ap_function == 0xFFFF)
407 		return NULL;
408 
409 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
410 	     dv != NULL; dv = deviter_next(&di)) {
411 
412 		pr = device_parent(dv);
413 
414 		if (pr == NULL || device_is_a(pr, "pci") != true)
415 			continue;
416 
417 		if (dv->dv_locators == NULL)	/* This should not happen. */
418 			continue;
419 
420 		pci = device_private(pr);
421 
422 		if (pci->sc_bus == ap->ap_bus &&
423 		    device_locator(dv, PCICF_DEV) == ap->ap_device &&
424 		    device_locator(dv, PCICF_FUNCTION) == ap->ap_function)
425 			break;
426 	}
427 
428 	deviter_release(&di);
429 
430 	return dv;
431 }
432