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