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