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