acpi_pci.c revision 1.27 1 /* $NetBSD: acpi_pci.c,v 1.27 2020/01/17 17:06:32 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.27 2020/01/17 17:06:32 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 ap->ap_pc = acpi_get_pci_chipset_tag(acpi_softc, ap->ap_segment, ap->ap_downbus);
234
235 /*
236 * This ACPI node denotes a PCI root bridge, but it may also
237 * denote a PCI device on the bridge's downstream bus segment.
238 */
239 if (ad->ad_devinfo->Valid & ACPI_VALID_ADR) {
240 ap->ap_bus = ap->ap_downbus;
241 ap->ap_device =
242 ACPI_HILODWORD(ad->ad_devinfo->Address);
243 ap->ap_function =
244 ACPI_LOLODWORD(ad->ad_devinfo->Address);
245
246 if (ap->ap_device > 31 ||
247 (ap->ap_function > 7 && ap->ap_function != 0xFFFF))
248 aprint_error_dev(ad->ad_root,
249 "invalid PCI address for %s\n", ad->ad_name);
250 else
251 ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
252 }
253
254 ad->ad_pciinfo = ap;
255
256 goto rec;
257 }
258
259 if ((ad->ad_parent != NULL) &&
260 (ad->ad_parent->ad_pciinfo != NULL) &&
261 (ad->ad_parent->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
262 (ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
263
264 /*
265 * Our parent is a PCI root bridge or a PCI-to-PCI
266 * bridge. We have the same PCI segment number, and
267 * our bus number is its downstream bus number.
268 */
269 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
270
271 ap->ap_pc = ad->ad_parent->ad_pciinfo->ap_pc;
272 ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
273 ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
274
275 ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
276 ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
277
278 if (ap->ap_device > 31 ||
279 (ap->ap_function > 7 && ap->ap_function != 0xFFFF)) {
280 aprint_error_dev(ad->ad_root,
281 "invalid PCI address for %s\n", ad->ad_name);
282 kmem_free(ap, sizeof(*ap));
283 goto rec;
284 }
285
286 ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
287
288 if (ap->ap_function == 0xFFFF) {
289 /*
290 * Assume that this device is not a PCI-to-PCI bridge.
291 * XXX: Do we need to be smarter?
292 */
293 } else {
294 /*
295 * Check whether this device is a PCI-to-PCI
296 * bridge and get its secondary bus number.
297 */
298 rv = acpi_pcidev_ppb_downbus(
299 ad->ad_parent->ad_pciinfo->ap_pc,
300 ap->ap_segment, ap->ap_bus, ap->ap_device,
301 ap->ap_function, &ap->ap_downbus);
302
303 if (ACPI_SUCCESS(rv))
304 ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
305 }
306
307 ad->ad_pciinfo = ap;
308
309 goto rec;
310 }
311
312 rec:
313 SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
314 rv = acpi_pcidev_scan(child);
315
316 if (ACPI_FAILURE(rv))
317 return rv;
318 }
319
320 return AE_OK;
321 }
322
323 /*
324 * acpi_pcidev_ppb_downbus:
325 *
326 * Retrieve the secondary bus number of the PCI-to-PCI bridge having the
327 * given PCI id. If successful, return AE_OK and fill *downbus.
328 * Otherwise, return an exception code and leave *downbus unchanged.
329 *
330 * XXX Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
331 */
332 ACPI_STATUS
333 acpi_pcidev_ppb_downbus(pci_chipset_tag_t pc, uint16_t segment, uint16_t bus,
334 uint16_t device, uint16_t function, uint16_t *downbus)
335 {
336 pcitag_t tag;
337 pcireg_t val;
338
339 if (bus > 255 || device > 31 || function > 7)
340 return AE_BAD_PARAMETER;
341
342 tag = pci_make_tag(pc, bus, device, function);
343
344 /* Check that this device exists. */
345 val = pci_conf_read(pc, tag, PCI_ID_REG);
346
347 if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
348 PCI_VENDOR(val) == 0)
349 return AE_NOT_EXIST;
350
351 /* Check that this device is a PCI-to-PCI bridge. */
352 val = pci_conf_read(pc, tag, PCI_BHLC_REG);
353
354 if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
355 return AE_TYPE;
356
357 /* This is a PCI-to-PCI bridge. Get its secondary bus#. */
358 val = pci_conf_read(pc, tag, PCI_BRIDGE_BUS_REG);
359 *downbus = PCI_BRIDGE_BUS_NUM_SECONDARY(val);
360
361 return AE_OK;
362 }
363
364 /*
365 * acpi_pcidev_find:
366 *
367 * Finds a PCI device in the ACPI name space.
368 *
369 * Returns an ACPI device node on success and NULL on failure.
370 */
371 struct acpi_devnode *
372 acpi_pcidev_find(uint16_t segment, uint16_t bus,
373 uint16_t device, uint16_t function)
374 {
375 struct acpi_softc *sc = acpi_softc;
376 struct acpi_devnode *ad;
377
378 if (sc == NULL)
379 return NULL;
380
381 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
382
383 if (ad->ad_pciinfo != NULL &&
384 (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_DEVICE) &&
385 ad->ad_pciinfo->ap_segment == segment &&
386 ad->ad_pciinfo->ap_bus == bus &&
387 ad->ad_pciinfo->ap_device == device &&
388 ad->ad_pciinfo->ap_function == function)
389 return ad;
390 }
391
392 return NULL;
393 }
394
395 /*
396 * acpi_pcidev_get_tag:
397 *
398 * Returns a PCI chipset tag for a PCI device in the ACPI name space.
399 */
400 pci_chipset_tag_t
401 acpi_pcidev_get_tag(uint16_t segment, uint16_t bus,
402 uint16_t device, uint16_t function)
403 {
404 struct acpi_devnode *ad;
405
406 ad = acpi_pcidev_find(segment, bus, device, function);
407 if (ad == NULL || ad->ad_pciinfo == NULL)
408 return NULL;
409
410 return ad->ad_pciinfo->ap_pc;
411 }
412
413 /*
414 * acpi_pciroot_find:
415 *
416 * Finds a PCI root bridge in the ACPI name space.
417 *
418 * Returns an ACPI device node on success and NULL on failure.
419 */
420 struct acpi_devnode *
421 acpi_pciroot_find(uint16_t segment, uint16_t bus)
422 {
423 struct acpi_softc *sc = acpi_softc;
424 struct acpi_devnode *ad;
425
426 if (sc == NULL)
427 return NULL;
428
429 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
430
431 if (ad->ad_pciinfo != NULL &&
432 (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
433 ad->ad_pciinfo->ap_segment == segment &&
434 ad->ad_pciinfo->ap_bus == bus)
435 return ad;
436 }
437
438 return NULL;
439 }
440
441 /*
442 * acpi_pcidev_find_dev:
443 *
444 * Returns the device corresponding to the given PCI info, or NULL
445 * if it doesn't exist.
446 */
447 device_t
448 acpi_pcidev_find_dev(struct acpi_devnode *ad)
449 {
450 struct acpi_pci_info *ap;
451 struct pci_softc *pci;
452 device_t dv, pr;
453 deviter_t di;
454
455 if (ad == NULL)
456 return NULL;
457
458 if (ad->ad_pciinfo == NULL)
459 return NULL;
460
461 ap = ad->ad_pciinfo;
462
463 if (ap->ap_function == 0xFFFF)
464 return NULL;
465
466 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
467 dv != NULL; dv = deviter_next(&di)) {
468
469 pr = device_parent(dv);
470
471 if (pr == NULL || device_is_a(pr, "pci") != true)
472 continue;
473
474 if (dv->dv_locators == NULL) /* This should not happen. */
475 continue;
476
477 pci = device_private(pr);
478
479 if (pci->sc_bus == ap->ap_bus &&
480 device_locator(dv, PCICF_DEV) == ap->ap_device &&
481 device_locator(dv, PCICF_FUNCTION) == ap->ap_function)
482 break;
483 }
484
485 deviter_release(&di);
486
487 return dv;
488 }
489
490 /*
491 * acpi_pci_ignore_boot_config:
492 *
493 * Returns 1 if the operating system may ignore the boot configuration
494 * of PCI resources.
495 */
496 ACPI_INTEGER
497 acpi_pci_ignore_boot_config(ACPI_HANDLE handle)
498 {
499 ACPI_OBJECT_LIST objs;
500 ACPI_OBJECT obj[4], *pobj;
501 ACPI_BUFFER buf;
502 ACPI_INTEGER ret;
503
504 objs.Count = 4;
505 objs.Pointer = obj;
506 obj[0].Type = ACPI_TYPE_BUFFER;
507 obj[0].Buffer.Length = ACPI_UUID_LENGTH;
508 obj[0].Buffer.Pointer = acpi_pci_dsm_uuid;
509 obj[1].Type = ACPI_TYPE_INTEGER;
510 obj[1].Integer.Value = 1;
511 obj[2].Type = ACPI_TYPE_INTEGER;
512 obj[2].Integer.Value = 5;
513 obj[3].Type = ACPI_TYPE_PACKAGE;
514 obj[3].Package.Count = 0;
515 obj[3].Package.Elements = NULL;
516
517 buf.Pointer = NULL;
518 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
519
520 if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_DSM", &objs, &buf)) || buf.Pointer == NULL)
521 return 0;
522
523 ret = 0;
524
525 pobj = buf.Pointer;
526 switch (pobj->Type) {
527 case ACPI_TYPE_INTEGER:
528 ret = pobj->Integer.Value;
529 break;
530 case ACPI_TYPE_PACKAGE:
531 if (pobj->Package.Count == 1 && pobj->Package.Elements[0].Type == ACPI_TYPE_INTEGER)
532 ret = pobj->Package.Elements[0].Integer.Value;
533 break;
534 }
535
536 ACPI_FREE(buf.Pointer);
537
538 return ret;
539 }
540