acpi_pci.c revision 1.7 1 1.7 jruoho /* $NetBSD: acpi_pci.c,v 1.7 2010/04/22 15:14:24 jruoho 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 /*
32 1.1 cegger * ACPI PCI Bus
33 1.1 cegger */
34 1.1 cegger
35 1.1 cegger #include <sys/cdefs.h>
36 1.7 jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.7 2010/04/22 15:14:24 jruoho Exp $");
37 1.1 cegger
38 1.1 cegger #include <sys/param.h>
39 1.1 cegger #include <sys/device.h>
40 1.1 cegger #include <sys/kmem.h>
41 1.3 jruoho #include <sys/systm.h>
42 1.1 cegger
43 1.6 jruoho #include <dev/pci/pcireg.h>
44 1.6 jruoho #include <dev/pci/pcidevs.h>
45 1.6 jruoho #include <dev/pci/ppbreg.h>
46 1.6 jruoho
47 1.1 cegger #include <dev/acpi/acpireg.h>
48 1.1 cegger #include <dev/acpi/acpivar.h>
49 1.1 cegger #include <dev/acpi/acpi_pci.h>
50 1.1 cegger
51 1.7 jruoho #define _COMPONENT ACPI_BUS_COMPONENT
52 1.7 jruoho ACPI_MODULE_NAME ("acpi_pci")
53 1.6 jruoho
54 1.7 jruoho #define ACPI_HILODWORD(x) ACPI_HIWORD(ACPI_LODWORD((x)))
55 1.7 jruoho #define ACPI_LOLODWORD(x) ACPI_LOWORD(ACPI_LODWORD((x)))
56 1.7 jruoho
57 1.7 jruoho static ACPI_STATUS acpi_pcidev_pciroot_bus(ACPI_HANDLE, uint16_t *);
58 1.7 jruoho static ACPI_STATUS acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *,
59 1.7 jruoho void *);
60 1.7 jruoho static ACPI_STATUS acpi_pcidev_scan_rec(struct acpi_devnode *);
61 1.1 cegger
62 1.1 cegger
63 1.6 jruoho /*
64 1.6 jruoho * Regarding PCI Segment Groups, the ACPI spec says (cf. ACPI 4.0, p. 277):
65 1.6 jruoho *
66 1.6 jruoho * "The optional _SEG object is located under a PCI host bridge and
67 1.6 jruoho * evaluates to an integer that describes the PCI Segment Group (see PCI
68 1.6 jruoho * Firmware Specification v3.0)."
69 1.6 jruoho *
70 1.6 jruoho * "PCI Segment Group supports more than 256 buses in a system by allowing
71 1.6 jruoho * the reuse of the PCI bus numbers. Within each PCI Segment Group, the bus
72 1.6 jruoho * numbers for the PCI buses must be unique. PCI buses in different PCI
73 1.6 jruoho * Segment Group are permitted to have the same bus number."
74 1.6 jruoho *
75 1.6 jruoho * "If _SEG does not exist, OSPM assumes that all PCI bus segments are in
76 1.6 jruoho * PCI Segment Group 0."
77 1.6 jruoho *
78 1.6 jruoho * "The lower 16 bits of _SEG returned integer is the PCI Segment Group
79 1.6 jruoho * number. Other bits are reserved."
80 1.6 jruoho */
81 1.1 cegger
82 1.6 jruoho /*
83 1.6 jruoho * Regarding PCI Base Bus Numbers, the ACPI spec says (cf. ACPI 4.0, p. 277):
84 1.6 jruoho *
85 1.6 jruoho * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
86 1.6 jruoho * number that the BIOS assigns. This is needed to access a PCI_Config
87 1.6 jruoho * operation region for the specified bus. The _BBN object is located under
88 1.6 jruoho * a PCI host bridge and must be unique for every host bridge within a
89 1.6 jruoho * segment since it is the PCI bus number."
90 1.6 jruoho *
91 1.6 jruoho * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
92 1.6 jruoho *
93 1.6 jruoho * "For a multiple root bus machine, _BBN is required for each bus. _BBN
94 1.6 jruoho * should provide the bus number assigned to this bus by the BIOS at boot
95 1.6 jruoho * time."
96 1.6 jruoho */
97 1.6 jruoho
98 1.6 jruoho
99 1.6 jruoho /*
100 1.6 jruoho * acpi_pcidev_pciroot_bus:
101 1.6 jruoho *
102 1.6 jruoho * Derive the PCI bus number of a PCI root bridge from its resources.
103 1.6 jruoho * If successful, return AE_OK and fill *busp. Otherwise, return an
104 1.6 jruoho * exception code and leave *busp unchanged.
105 1.6 jruoho *
106 1.6 jruoho * XXX Use ACPI resource parsing functions (acpi_resource.c) once bus number
107 1.6 jruoho * ranges are implemented there.
108 1.6 jruoho */
109 1.6 jruoho static ACPI_STATUS
110 1.6 jruoho acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
111 1.1 cegger {
112 1.1 cegger ACPI_STATUS rv;
113 1.6 jruoho int32_t bus;
114 1.6 jruoho
115 1.6 jruoho bus = -1;
116 1.6 jruoho rv = AcpiWalkResources(handle, METHOD_NAME__CRS,
117 1.6 jruoho acpi_pcidev_pciroot_bus_callback, &bus);
118 1.1 cegger
119 1.1 cegger if (ACPI_FAILURE(rv))
120 1.6 jruoho return rv;
121 1.6 jruoho
122 1.6 jruoho if (bus < 0 || bus > 0xFFFF)
123 1.6 jruoho return AE_NOT_EXIST;
124 1.6 jruoho
125 1.6 jruoho *busp = (uint16_t)bus;
126 1.6 jruoho return rv;
127 1.6 jruoho }
128 1.6 jruoho
129 1.6 jruoho static ACPI_STATUS
130 1.6 jruoho acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
131 1.6 jruoho {
132 1.6 jruoho int32_t *bus = context;
133 1.6 jruoho ACPI_RESOURCE_ADDRESS64 addr64;
134 1.6 jruoho
135 1.6 jruoho if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
136 1.6 jruoho (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
137 1.6 jruoho (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
138 1.6 jruoho return AE_OK; /* continue the walk */
139 1.6 jruoho
140 1.6 jruoho if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
141 1.6 jruoho return AE_OK; /* continue the walk */
142 1.6 jruoho
143 1.6 jruoho if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
144 1.6 jruoho return AE_OK; /* continue the walk */
145 1.6 jruoho
146 1.6 jruoho if (*bus != -1)
147 1.6 jruoho return AE_ALREADY_EXISTS;
148 1.6 jruoho
149 1.6 jruoho *bus = addr64.Minimum;
150 1.6 jruoho return AE_OK; /* continue the walk */
151 1.6 jruoho }
152 1.6 jruoho
153 1.6 jruoho /*
154 1.6 jruoho * acpi_pcidev_scan_rec:
155 1.6 jruoho *
156 1.6 jruoho * Scan the ACPI device tree for PCI devices. A node is detected as a
157 1.6 jruoho * PCI device if it has an ancestor that is a PCI root bridge and such
158 1.6 jruoho * that all intermediate nodes are PCI-to-PCI bridges. Depth-first
159 1.6 jruoho * recursive implementation.
160 1.6 jruoho */
161 1.6 jruoho static ACPI_STATUS
162 1.6 jruoho acpi_pcidev_scan_rec(struct acpi_devnode *ad)
163 1.6 jruoho {
164 1.6 jruoho struct acpi_devnode *child;
165 1.6 jruoho struct acpi_pci_info *ap;
166 1.6 jruoho ACPI_INTEGER val;
167 1.6 jruoho ACPI_STATUS rv;
168 1.6 jruoho
169 1.6 jruoho if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
170 1.6 jruoho !(ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
171 1.6 jruoho ad->ad_pciinfo = NULL;
172 1.6 jruoho goto rec;
173 1.6 jruoho }
174 1.6 jruoho
175 1.6 jruoho if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
176 1.6 jruoho ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
177 1.6 jruoho if (ap == NULL)
178 1.6 jruoho return AE_NO_MEMORY;
179 1.6 jruoho
180 1.6 jruoho rv = acpi_eval_integer(ad->ad_handle, METHOD_NAME__SEG, &val);
181 1.6 jruoho if (ACPI_SUCCESS(rv))
182 1.6 jruoho ap->ap_segment = ACPI_LOWORD(val);
183 1.6 jruoho else
184 1.6 jruoho ap->ap_segment = 0;
185 1.6 jruoho
186 1.6 jruoho /* try to get bus number using _CRS first */
187 1.6 jruoho rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_bus);
188 1.6 jruoho if (ACPI_FAILURE(rv)) {
189 1.6 jruoho rv = acpi_eval_integer(ad->ad_handle, METHOD_NAME__BBN, &val);
190 1.6 jruoho if (ACPI_SUCCESS(rv))
191 1.6 jruoho ap->ap_bus = ACPI_LOWORD(val);
192 1.6 jruoho else
193 1.6 jruoho ap->ap_bus = 0;
194 1.6 jruoho }
195 1.6 jruoho
196 1.7 jruoho ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
197 1.7 jruoho ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
198 1.6 jruoho
199 1.6 jruoho ap->ap_bridge = true;
200 1.6 jruoho ap->ap_downbus = ap->ap_bus;
201 1.6 jruoho
202 1.6 jruoho ad->ad_pciinfo = ap;
203 1.6 jruoho goto rec;
204 1.6 jruoho }
205 1.6 jruoho
206 1.6 jruoho if ((ad->ad_parent != NULL) &&
207 1.6 jruoho (ad->ad_parent->ad_pciinfo != NULL) &&
208 1.6 jruoho (ad->ad_parent->ad_pciinfo->ap_bridge)) {
209 1.6 jruoho /*
210 1.6 jruoho * Our parent is a PCI root bridge or a PCI-to-PCI bridge. We
211 1.6 jruoho * have the same PCI segment#, and our bus# is its downstream
212 1.6 jruoho * bus number.
213 1.6 jruoho */
214 1.6 jruoho ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
215 1.6 jruoho if (ap == NULL)
216 1.6 jruoho return AE_NO_MEMORY;
217 1.6 jruoho
218 1.6 jruoho ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
219 1.6 jruoho ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
220 1.7 jruoho
221 1.7 jruoho ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
222 1.7 jruoho ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
223 1.1 cegger
224 1.1 cegger /*
225 1.6 jruoho * Check whether this device is a PCI-to-PCI bridge and get its
226 1.6 jruoho * secondary bus#.
227 1.1 cegger */
228 1.6 jruoho rv = acpi_pcidev_ppb_downbus(ap->ap_segment, ap->ap_bus,
229 1.6 jruoho ap->ap_device, ap->ap_function, &ap->ap_downbus);
230 1.6 jruoho if (ACPI_SUCCESS(rv))
231 1.6 jruoho ap->ap_bridge = true;
232 1.6 jruoho else
233 1.6 jruoho ap->ap_bridge = false;
234 1.6 jruoho
235 1.6 jruoho ad->ad_pciinfo = ap;
236 1.6 jruoho goto rec;
237 1.6 jruoho }
238 1.6 jruoho rec:
239 1.6 jruoho SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
240 1.6 jruoho rv = acpi_pcidev_scan_rec(child);
241 1.6 jruoho if (ACPI_FAILURE(rv))
242 1.6 jruoho return rv;
243 1.1 cegger }
244 1.1 cegger
245 1.6 jruoho return AE_OK;
246 1.6 jruoho }
247 1.6 jruoho
248 1.6 jruoho /*
249 1.6 jruoho * acpi_pcidev_ppb_downbus:
250 1.6 jruoho *
251 1.6 jruoho * Retrieve the secondary bus number of the PCI-to-PCI bridge having the
252 1.6 jruoho * given PCI id. If successful, return AE_OK and fill *busp. Otherwise,
253 1.6 jruoho * return an exception code and leave *busp unchanged.
254 1.6 jruoho *
255 1.6 jruoho * XXX Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
256 1.6 jruoho */
257 1.6 jruoho ACPI_STATUS
258 1.6 jruoho acpi_pcidev_ppb_downbus(uint16_t segment, uint16_t bus, uint16_t device,
259 1.6 jruoho uint16_t function, uint16_t *downbus)
260 1.6 jruoho {
261 1.6 jruoho struct acpi_softc *sc = acpi_softc;
262 1.6 jruoho pci_chipset_tag_t pc;
263 1.6 jruoho pcitag_t tag;
264 1.6 jruoho pcireg_t val;
265 1.6 jruoho
266 1.6 jruoho if (bus > 255 || device > 31 || function > 7)
267 1.6 jruoho return AE_BAD_PARAMETER;
268 1.1 cegger
269 1.6 jruoho if (sc == NULL)
270 1.6 jruoho pc = NULL;
271 1.1 cegger else
272 1.6 jruoho pc = sc->sc_pc;
273 1.1 cegger
274 1.6 jruoho tag = pci_make_tag(pc, bus, device, function);
275 1.1 cegger
276 1.6 jruoho /* Check that this device exists. */
277 1.6 jruoho val = pci_conf_read(pc, tag, PCI_ID_REG);
278 1.6 jruoho if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
279 1.6 jruoho PCI_VENDOR(val) == 0)
280 1.6 jruoho return AE_NOT_EXIST;
281 1.6 jruoho
282 1.6 jruoho /* Check that this device is a PCI-to-PCI bridge. */
283 1.6 jruoho val = pci_conf_read(pc, tag, PCI_BHLC_REG);
284 1.6 jruoho if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
285 1.6 jruoho return AE_TYPE;
286 1.6 jruoho
287 1.6 jruoho /* This is a PCI-to-PCI bridge. Get its secondary bus#. */
288 1.6 jruoho val = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
289 1.6 jruoho *downbus = PPB_BUSINFO_SECONDARY(val);
290 1.6 jruoho return AE_OK;
291 1.1 cegger }
292 1.1 cegger
293 1.1 cegger static void
294 1.6 jruoho acpi_pcidev_print(struct acpi_devnode *ad)
295 1.1 cegger {
296 1.6 jruoho aprint_debug(" ");
297 1.6 jruoho if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE)
298 1.6 jruoho aprint_debug("*");
299 1.6 jruoho aprint_debug("%s@%"PRIx16":%"PRIx16":%"PRIx16":%"PRIx16,
300 1.6 jruoho ad->ad_name,
301 1.6 jruoho ad->ad_pciinfo->ap_segment,
302 1.6 jruoho ad->ad_pciinfo->ap_bus,
303 1.6 jruoho ad->ad_pciinfo->ap_device,
304 1.6 jruoho ad->ad_pciinfo->ap_function);
305 1.6 jruoho if (ad->ad_pciinfo->ap_bridge)
306 1.6 jruoho aprint_debug(">%"PRIx16, ad->ad_pciinfo->ap_downbus);
307 1.1 cegger }
308 1.1 cegger
309 1.6 jruoho void
310 1.1 cegger acpi_pcidev_scan(struct acpi_softc *sc)
311 1.1 cegger {
312 1.1 cegger struct acpi_devnode *ad;
313 1.1 cegger
314 1.6 jruoho acpi_pcidev_scan_rec(sc->sc_root);
315 1.6 jruoho aprint_debug_dev(sc->sc_dev, "pci devices:");
316 1.5 jruoho SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
317 1.6 jruoho if (ad->ad_pciinfo != NULL)
318 1.6 jruoho acpi_pcidev_print(ad);
319 1.1 cegger }
320 1.1 cegger aprint_debug("\n");
321 1.1 cegger }
322 1.1 cegger
323 1.1 cegger /*
324 1.1 cegger * acpi_pcidev_find:
325 1.1 cegger *
326 1.1 cegger * Finds a PCI device in the ACPI name space.
327 1.1 cegger * The return status is either:
328 1.1 cegger * - AE_NOT_FOUND if no such device was found.
329 1.1 cegger * - AE_OK if one and only one such device was found.
330 1.1 cegger */
331 1.1 cegger ACPI_STATUS
332 1.6 jruoho acpi_pcidev_find(uint16_t segment, uint16_t bus, uint16_t device,
333 1.6 jruoho uint16_t function, struct acpi_devnode **devnodep)
334 1.1 cegger {
335 1.6 jruoho struct acpi_softc *sc = acpi_softc;
336 1.6 jruoho struct acpi_devnode *ad;
337 1.1 cegger
338 1.6 jruoho if (sc == NULL)
339 1.6 jruoho return AE_NOT_FOUND;
340 1.1 cegger
341 1.6 jruoho SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
342 1.6 jruoho if ((ad->ad_pciinfo != NULL) &&
343 1.6 jruoho (ad->ad_pciinfo->ap_segment == segment) &&
344 1.6 jruoho (ad->ad_pciinfo->ap_bus == bus) &&
345 1.6 jruoho (ad->ad_pciinfo->ap_device == device) &&
346 1.6 jruoho (ad->ad_pciinfo->ap_function == function)) {
347 1.6 jruoho *devnodep = ad;
348 1.6 jruoho return AE_OK;
349 1.6 jruoho }
350 1.1 cegger }
351 1.6 jruoho return AE_NOT_FOUND;
352 1.1 cegger }
353