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