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