xref: /netbsd-src/sys/dev/acpi/acpi_pci.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /* $NetBSD: acpi_pci.c,v 1.24 2018/10/21 11:04:26 jmcneill 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.24 2018/10/21 11:04:26 jmcneill 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  * UUID for _DSM control method, from PCI Firmware Specification.
62  */
63 static UINT8 acpi_pci_dsm_uuid[ACPI_UUID_LENGTH] = {
64 	0xd0, 0x37, 0xc9, 0xe5, 0x53, 0x35, 0x7a, 0x4d,
65 	0x91, 0x17, 0xea, 0x4d, 0x19, 0xc3, 0x43, 0x4d
66 };
67 
68 /*
69  * Regarding PCI Segment Groups (ACPI 4.0, p. 277):
70  *
71  * "The optional _SEG object is located under a PCI host bridge and
72  *  evaluates to an integer that describes the PCI Segment Group (see PCI
73  *  Firmware Specification v3.0)."
74  *
75  * "PCI Segment Group is purely a software concept managed by system
76  *  firmware and used by OSPM. It is a logical collection of PCI buses
77  *  (or bus segments). It is a way to logically group the PCI bus segments
78  *  and PCI Express Hierarchies. _SEG is a level higher than _BBN."
79  *
80  * "PCI Segment Group supports more than 256 buses in a system by allowing
81  *  the reuse of the PCI bus numbers.  Within each PCI Segment Group, the bus
82  *  numbers for the PCI buses must be unique. PCI buses in different PCI
83  *  Segment Group are permitted to have the same bus number."
84  */
85 
86 /*
87  * Regarding PCI Base Bus Numbers (ACPI 4.0, p. 277):
88  *
89  * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
90  *  number that the BIOS assigns.  This is needed to access a PCI_Config
91  *  operation region for the specified bus.  The _BBN object is located under
92  *  a PCI host bridge and must be unique for every host bridge within a
93  *  segment since it is the PCI bus number."
94  *
95  * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
96  *
97  * "For a multiple root bus machine, _BBN is required for each bus.  _BBN
98  *  should provide the bus number assigned to this bus by the BIOS at boot
99  *  time."
100  */
101 
102 /*
103  * acpi_pcidev_pciroot_bus:
104  *
105  *	Derive the PCI bus number of a PCI root bridge from its resources.
106  *	If successful, return AE_OK and fill *busp.  Otherwise, return an
107  *	exception code and leave *busp unchanged.
108  */
109 static ACPI_STATUS
110 acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
111 {
112 	ACPI_STATUS rv;
113 	int32_t bus;
114 
115 	bus = -1;
116 
117 	/*
118 	 * XXX:	Use the ACPI resource parsing functions (acpi_resource.c)
119 	 *	once bus number ranges have been implemented there.
120 	 */
121 	rv = AcpiWalkResources(handle, "_CRS",
122 	    acpi_pcidev_pciroot_bus_callback, &bus);
123 
124 	if (ACPI_FAILURE(rv))
125 		return rv;
126 
127 	if (bus == -1)
128 		return AE_NOT_EXIST;
129 
130 	/* Here it holds that 0 <= bus <= 0xFFFF. */
131 	*busp = (uint16_t)bus;
132 
133 	return rv;
134 }
135 
136 static ACPI_STATUS
137 acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
138 {
139 	ACPI_RESOURCE_ADDRESS64 addr64;
140 	int32_t *bus = context;
141 
142 	/* Always continue the walk by returning AE_OK. */
143 	if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
144 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
145 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
146 		return AE_OK;
147 
148 	if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
149 		return AE_OK;
150 
151 	if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
152 		return AE_OK;
153 
154 	if (*bus != -1)
155 		return AE_ALREADY_EXISTS;
156 
157 	if (addr64.Address.Minimum > 0xFFFF)
158 		return AE_BAD_DATA;
159 
160 	*bus = (int32_t)addr64.Address.Minimum;
161 
162 	return AE_OK;
163 }
164 
165 /*
166  * acpi_pcidev_scan:
167  *
168  *	Scan the ACPI device tree for PCI devices.  A node is detected as a
169  *	PCI device if it has an ancestor that is a PCI root bridge and such
170  *	that all intermediate nodes are PCI-to-PCI bridges.  Depth-first
171  *	recursive implementation.
172  *
173  *	PCI root bridges do not necessarily contain an _ADR, since they already
174  *	contain an _HID (ACPI 4.0a, p. 197).  However we require an _ADR for
175  *	all non-root PCI devices.
176  */
177 ACPI_STATUS
178 acpi_pcidev_scan(struct acpi_devnode *ad)
179 {
180 	struct acpi_devnode *child;
181 	struct acpi_pci_info *ap;
182 	ACPI_INTEGER val;
183 	ACPI_STATUS rv;
184 
185 	ad->ad_pciinfo = NULL;
186 
187 	/*
188 	 * We attach PCI information only to devices that are present,
189 	 * enabled, and functioning properly.
190 	 * Note: there is a possible race condition, because _STA may
191 	 * have changed since ad->ad_devinfo->CurrentStatus was set.
192 	 */
193 	if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
194 		goto rec;
195 
196 	if (!acpi_device_present(ad->ad_handle))
197 		goto rec;
198 
199 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
200 
201 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
202 
203 		/*
204 		 * If no _SEG exist, all PCI bus segments are assumed
205 		 * to be in the PCI segment group 0 (ACPI 4.0, p. 277).
206 		 * The segment group number is conveyed in the lower
207 		 * 16 bits of _SEG (the other bits are all reserved).
208 		 */
209 		rv = acpi_eval_integer(ad->ad_handle, "_SEG", &val);
210 
211 		if (ACPI_SUCCESS(rv))
212 			ap->ap_segment = ACPI_LOWORD(val);
213 
214 		/* Try to get downstream bus number using _CRS first. */
215 		rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_downbus);
216 
217 		if (ACPI_FAILURE(rv)) {
218 			rv = acpi_eval_integer(ad->ad_handle, "_BBN", &val);
219 
220 			if (ACPI_SUCCESS(rv))
221 				ap->ap_downbus = ACPI_LOWORD(val);
222 		}
223 
224 		if (ap->ap_downbus > 255) {
225 			aprint_error_dev(ad->ad_root,
226 			    "invalid PCI downstream bus for %s\n", ad->ad_name);
227 			kmem_free(ap, sizeof(*ap));
228 			goto rec;
229 		}
230 
231 		ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
232 
233 		/*
234 		 * This ACPI node denotes a PCI root bridge, but it may also
235 		 * denote a PCI device on the bridge's downstream bus segment.
236 		 */
237 		if (ad->ad_devinfo->Valid & ACPI_VALID_ADR) {
238 			ap->ap_bus = ap->ap_downbus;
239 			ap->ap_device =
240 			    ACPI_HILODWORD(ad->ad_devinfo->Address);
241 			ap->ap_function =
242 			    ACPI_LOLODWORD(ad->ad_devinfo->Address);
243 
244 			if (ap->ap_device > 31 ||
245 			    (ap->ap_function > 7 && ap->ap_function != 0xFFFF))
246 				aprint_error_dev(ad->ad_root,
247 				    "invalid PCI address for %s\n", ad->ad_name);
248 			else
249 				ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
250 		}
251 
252 		ad->ad_pciinfo = ap;
253 
254 		goto rec;
255 	}
256 
257 	if ((ad->ad_parent != NULL) &&
258 	    (ad->ad_parent->ad_pciinfo != NULL) &&
259 	    (ad->ad_parent->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
260 	    (ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
261 
262 		/*
263 		 * Our parent is a PCI root bridge or a PCI-to-PCI
264 		 * bridge. We have the same PCI segment number, and
265 		 * our bus number is its downstream bus number.
266 		 */
267 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
268 
269 		ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
270 		ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
271 
272 		ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
273 		ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
274 
275 		if (ap->ap_device > 31 ||
276 		    (ap->ap_function > 7 && ap->ap_function != 0xFFFF)) {
277 			aprint_error_dev(ad->ad_root,
278 			    "invalid PCI address for %s\n", ad->ad_name);
279 			kmem_free(ap, sizeof(*ap));
280 			goto rec;
281 		}
282 
283 		ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
284 
285 		if (ap->ap_function == 0xFFFF) {
286 			/*
287 			 * Assume that this device is not a PCI-to-PCI bridge.
288 			 * XXX: Do we need to be smarter?
289 			 */
290 		} else {
291 			/*
292 			 * Check whether this device is a PCI-to-PCI
293 			 * bridge and get its secondary bus number.
294 			 */
295 			rv = acpi_pcidev_ppb_downbus(ap->ap_segment, ap->ap_bus,
296 			    ap->ap_device, ap->ap_function, &ap->ap_downbus);
297 
298 			if (ACPI_SUCCESS(rv))
299 				ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
300 		}
301 
302 		ad->ad_pciinfo = ap;
303 
304 		goto rec;
305 	}
306 
307 rec:
308 	SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
309 		rv = acpi_pcidev_scan(child);
310 
311 		if (ACPI_FAILURE(rv))
312 			return rv;
313 	}
314 
315 	return AE_OK;
316 }
317 
318 /*
319  * acpi_pcidev_ppb_downbus:
320  *
321  *	Retrieve the secondary bus number of the PCI-to-PCI bridge having the
322  *	given PCI id.  If successful, return AE_OK and fill *downbus.
323  *	Otherwise, return an exception code and leave *downbus unchanged.
324  *
325  * XXX	Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
326  */
327 ACPI_STATUS
328 acpi_pcidev_ppb_downbus(uint16_t segment, uint16_t bus, uint16_t device,
329     uint16_t function, uint16_t *downbus)
330 {
331 	struct acpi_softc *sc = acpi_softc;
332 	pci_chipset_tag_t pc;
333 	pcitag_t tag;
334 	pcireg_t val;
335 
336 	if (bus > 255 || device > 31 || function > 7)
337 		return AE_BAD_PARAMETER;
338 
339 	pc = sc->sc_pc;
340 
341 	tag = pci_make_tag(pc, bus, device, function);
342 
343 	/* Check that this device exists. */
344 	val = pci_conf_read(pc, tag, PCI_ID_REG);
345 
346 	if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
347 	    PCI_VENDOR(val) == 0)
348 		return AE_NOT_EXIST;
349 
350 	/* Check that this device is a PCI-to-PCI bridge. */
351 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
352 
353 	if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
354 		return AE_TYPE;
355 
356 	/* This is a PCI-to-PCI bridge.  Get its secondary bus#. */
357 	val = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
358 	*downbus = PPB_BUSINFO_SECONDARY(val);
359 
360 	return AE_OK;
361 }
362 
363 /*
364  * acpi_pcidev_find:
365  *
366  *      Finds a PCI device in the ACPI name space.
367  *
368  *      Returns an ACPI device node on success and NULL on failure.
369  */
370 struct acpi_devnode *
371 acpi_pcidev_find(uint16_t segment, uint16_t bus,
372     uint16_t device, uint16_t function)
373 {
374 	struct acpi_softc *sc = acpi_softc;
375 	struct acpi_devnode *ad;
376 
377 	if (sc == NULL)
378 		return NULL;
379 
380 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
381 
382 		if (ad->ad_pciinfo != NULL &&
383 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_DEVICE) &&
384 		    ad->ad_pciinfo->ap_segment == segment &&
385 		    ad->ad_pciinfo->ap_bus == bus &&
386 		    ad->ad_pciinfo->ap_device == device &&
387 		    ad->ad_pciinfo->ap_function == function)
388 			return ad;
389 	}
390 
391 	return NULL;
392 }
393 
394 /*
395  * acpi_pciroot_find:
396  *
397  *	Finds a PCI root bridge in the ACPI name space.
398  *
399  *	Returns an ACPI device node on success and NULL on failure.
400  */
401 struct acpi_devnode *
402 acpi_pciroot_find(uint16_t segment, uint16_t bus)
403 {
404 	struct acpi_softc *sc = acpi_softc;
405 	struct acpi_devnode *ad;
406 
407 	if (sc == NULL)
408 		return NULL;
409 
410 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
411 
412 		if (ad->ad_pciinfo != NULL &&
413 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
414 		    ad->ad_pciinfo->ap_segment == segment &&
415 		    ad->ad_pciinfo->ap_bus == bus)
416 			return ad;
417 	}
418 
419 	return NULL;
420 }
421 
422 /*
423  * acpi_pcidev_find_dev:
424  *
425  *	Returns the device corresponding to the given PCI info, or NULL
426  *	if it doesn't exist.
427  */
428 device_t
429 acpi_pcidev_find_dev(struct acpi_devnode *ad)
430 {
431 	struct acpi_pci_info *ap;
432 	struct pci_softc *pci;
433 	device_t dv, pr;
434 	deviter_t di;
435 
436 	if (ad == NULL)
437 		return NULL;
438 
439 	if (ad->ad_pciinfo == NULL)
440 		return NULL;
441 
442 	ap = ad->ad_pciinfo;
443 
444 	if (ap->ap_function == 0xFFFF)
445 		return NULL;
446 
447 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
448 	     dv != NULL; dv = deviter_next(&di)) {
449 
450 		pr = device_parent(dv);
451 
452 		if (pr == NULL || device_is_a(pr, "pci") != true)
453 			continue;
454 
455 		if (dv->dv_locators == NULL)	/* This should not happen. */
456 			continue;
457 
458 		pci = device_private(pr);
459 
460 		if (pci->sc_bus == ap->ap_bus &&
461 		    device_locator(dv, PCICF_DEV) == ap->ap_device &&
462 		    device_locator(dv, PCICF_FUNCTION) == ap->ap_function)
463 			break;
464 	}
465 
466 	deviter_release(&di);
467 
468 	return dv;
469 }
470 
471 /*
472  * acpi_pci_ignore_boot_config:
473  *
474  *	Returns 1 if the operating system may ignore the boot configuration
475  *	of PCI resources.
476  */
477 ACPI_INTEGER
478 acpi_pci_ignore_boot_config(ACPI_HANDLE handle)
479 {
480 	ACPI_OBJECT_LIST objs;
481 	ACPI_OBJECT obj[4], *pobj;
482 	ACPI_BUFFER buf;
483 	ACPI_INTEGER ret;
484 
485 	objs.Count = 4;
486 	objs.Pointer = obj;
487 	obj[0].Type = ACPI_TYPE_BUFFER;
488 	obj[0].Buffer.Length = ACPI_UUID_LENGTH;
489 	obj[0].Buffer.Pointer = acpi_pci_dsm_uuid;
490 	obj[1].Type = ACPI_TYPE_INTEGER;
491 	obj[1].Integer.Value = 1;
492 	obj[2].Type = ACPI_TYPE_INTEGER;
493 	obj[2].Integer.Value = 5;
494 	obj[3].Type = ACPI_TYPE_PACKAGE;
495 	obj[3].Package.Count = 0;
496 	obj[3].Package.Elements = NULL;
497 
498 	buf.Pointer = NULL;
499 	buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
500 
501 	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_DSM", &objs, &buf)) || buf.Pointer == NULL)
502 		return 0;
503 
504 	ret = 0;
505 
506 	pobj = buf.Pointer;
507 	switch (pobj->Type) {
508 	case ACPI_TYPE_INTEGER:
509 		ret = pobj->Integer.Value;
510 		break;
511 	case ACPI_TYPE_PACKAGE:
512 		if (pobj->Package.Count == 1 && pobj->Package.Elements[0].Type == ACPI_TYPE_INTEGER)
513 			ret = pobj->Package.Elements[0].Integer.Value;
514 		break;
515 	}
516 
517 	ACPI_FREE(buf.Pointer);
518 
519 	return ret;
520 }
521