Sync ACPI with FreeBSD 7.2
[dragonfly.git] / sys / dev / acpica5 / acpi_pcib_acpi.c
bloba7b707b76f70c411625709b894d176ec092890e7
1 /*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 * __FBSDID("$FreeBSD: src/sys/dev/acpica/acpi_pcib_acpi.c,v 1.55.8.1 2009/04/15 03:14:26 kensmith Exp $");
29 #include <sys/cdefs.h>
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
39 #include "acpi.h"
40 #include <dev/acpica5/acpivar.h>
42 #include <bus/pci/i386/pci_cfgreg.h>
43 #include <bus/pci/pcivar.h>
44 #include <bus/pci/pcib_private.h>
45 #include "pcib_if.h"
47 #include <dev/acpica5/acpi_pcibvar.h>
49 /* Hooks for the ACPI CA debugging infrastructure. */
50 #define _COMPONENT ACPI_BUS
51 ACPI_MODULE_NAME("PCI_ACPI")
53 struct acpi_hpcib_softc {
54 device_t ap_dev;
55 ACPI_HANDLE ap_handle;
56 int ap_flags;
58 int ap_segment; /* analagous to Alpha 'hose' */
59 int ap_bus; /* bios-assigned bus number */
61 ACPI_BUFFER ap_prt; /* interrupt routing table */
64 static int acpi_pcib_acpi_probe(device_t bus);
65 static int acpi_pcib_acpi_attach(device_t bus);
66 static int acpi_pcib_acpi_resume(device_t bus);
67 static int acpi_pcib_read_ivar(device_t dev, device_t child,
68 int which, uintptr_t *result);
69 static int acpi_pcib_write_ivar(device_t dev, device_t child,
70 int which, uintptr_t value);
71 static uint32_t acpi_pcib_read_config(device_t dev, int bus, int slot,
72 int func, int reg, int bytes);
73 static void acpi_pcib_write_config(device_t dev, int bus, int slot,
74 int func, int reg, uint32_t data, int bytes);
75 static int acpi_pcib_acpi_route_interrupt(device_t pcib,
76 device_t dev, int pin);
77 #ifdef MSI
78 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev,
79 int count, int maxcount, int *irqs);
80 static int acpi_pcib_map_msi(device_t pcib, device_t dev,
81 int irq, uint64_t *addr, uint32_t *data);
82 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev,
83 int *irq);
84 #endif
85 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
86 device_t child, int type, int *rid,
87 u_long start, u_long end, u_long count,
88 u_int flags);
90 static device_method_t acpi_pcib_acpi_methods[] = {
91 /* Device interface */
92 DEVMETHOD(device_probe, acpi_pcib_acpi_probe),
93 DEVMETHOD(device_attach, acpi_pcib_acpi_attach),
94 DEVMETHOD(device_shutdown, bus_generic_shutdown),
95 DEVMETHOD(device_suspend, bus_generic_suspend),
96 DEVMETHOD(device_resume, acpi_pcib_acpi_resume),
98 /* Bus interface */
99 DEVMETHOD(bus_print_child, bus_generic_print_child),
100 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
101 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar),
102 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource),
103 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
104 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
105 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
106 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
107 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
109 /* pcib interface */
110 DEVMETHOD(pcib_maxslots, pcib_maxslots),
111 DEVMETHOD(pcib_read_config, acpi_pcib_read_config),
112 DEVMETHOD(pcib_write_config, acpi_pcib_write_config),
113 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt),
114 #ifdef MSI
115 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi),
116 DEVMETHOD(pcib_release_msi, pcib_release_msi),
117 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix),
118 DEVMETHOD(pcib_release_msix, pcib_release_msix),
119 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi),
120 #endif
121 {0, 0}
124 static devclass_t pcib_devclass;
126 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
127 sizeof(struct acpi_hpcib_softc));
128 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
129 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
131 static int
132 acpi_pcib_acpi_probe(device_t dev)
134 static char *pcib_ids[] = { "PNP0A03", NULL };
136 if (acpi_disabled("pcib") ||
137 ACPI_ID_PROBE(device_get_parent(dev), dev, pcib_ids) == NULL)
138 return (ENXIO);
140 if (pci_cfgregopen() == 0)
141 return (ENXIO);
142 device_set_desc(dev, "ACPI Host-PCI bridge");
143 return (0);
146 static int
147 acpi_pcib_acpi_attach(device_t dev)
149 struct acpi_hpcib_softc *sc;
150 ACPI_STATUS status;
151 u_int addr, slot, func, busok;
152 uint8_t busno;
154 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
156 sc = device_get_softc(dev);
157 sc->ap_dev = dev;
158 sc->ap_handle = acpi_get_handle(dev);
161 * Get our base bus number by evaluating _BBN.
162 * If this doesn't work, we assume we're bus number 0.
164 * XXX note that it may also not exist in the case where we are
165 * meant to use a private configuration space mechanism for this bus,
166 * so we should dig out our resources and check to see if we have
167 * anything like that. How do we do this?
168 * XXX If we have the requisite information, and if we don't think the
169 * default PCI configuration space handlers can deal with this bus,
170 * we should attach our own handler.
171 * XXX invoke _REG on this for the PCI config space address space?
172 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
173 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
174 * if _BBN is zero and pcib0 already exists, we try to read our
175 * bus number from the configuration registers at address _ADR.
177 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
178 if (ACPI_FAILURE(status)) {
179 if (status != AE_NOT_FOUND) {
180 device_printf(dev, "could not evaluate _BBN - %s\n",
181 AcpiFormatException(status));
182 return_VALUE (ENXIO);
183 } else {
184 /* If it's not found, assume 0. */
185 sc->ap_bus = 0;
190 * If the bus is zero and pcib0 already exists, read the bus number
191 * via PCI config space.
193 busok = 1;
194 if (sc->ap_bus == 0 && devclass_get_device(pcib_devclass, 0) != dev) {
195 busok = 0;
196 status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr);
197 if (ACPI_FAILURE(status)) {
198 if (status != AE_NOT_FOUND) {
199 device_printf(dev, "could not evaluate _ADR - %s\n",
200 AcpiFormatException(status));
201 return_VALUE (ENXIO);
202 } else
203 device_printf(dev, "couldn't find _ADR\n");
204 } else {
205 /* XXX: We assume bus 0. */
206 slot = ACPI_ADR_PCI_SLOT(addr);
207 func = ACPI_ADR_PCI_FUNC(addr);
208 if (bootverbose)
209 device_printf(dev, "reading config registers from 0:%d:%d\n",
210 slot, func);
211 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
212 device_printf(dev, "couldn't read bus number from cfg space\n");
213 else {
214 sc->ap_bus = busno;
215 busok = 1;
221 * If nothing else worked, hope that ACPI at least lays out the
222 * host-PCI bridges in order and that as a result our unit number
223 * is actually our bus number. There are several reasons this
224 * might not be true.
226 if (busok == 0) {
227 sc->ap_bus = device_get_unit(dev);
228 device_printf(dev, "trying bus number %d\n", sc->ap_bus);
232 * Get our segment number by evaluating _SEG
233 * It's OK for this to not exist.
235 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
236 if (ACPI_FAILURE(status)) {
237 if (status != AE_NOT_FOUND) {
238 device_printf(dev, "could not evaluate _SEG - %s\n",
239 AcpiFormatException(status));
240 return_VALUE (ENXIO);
242 /* If it's not found, assume 0. */
243 sc->ap_segment = 0;
245 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
248 static int
249 acpi_pcib_acpi_resume(device_t dev)
252 return (acpi_pcib_resume(dev));
256 * Support for standard PCI bridge ivars.
258 static int
259 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
261 struct acpi_hpcib_softc *sc = device_get_softc(dev);
263 switch (which) {
264 case PCIB_IVAR_DOMAIN:
265 *result = 0;
266 return (0);
267 case PCIB_IVAR_BUS:
268 *result = sc->ap_bus;
269 return (0);
270 case ACPI_IVAR_HANDLE:
271 *result = (uintptr_t)sc->ap_handle;
272 return (0);
273 case ACPI_IVAR_FLAGS:
274 *result = (uintptr_t)sc->ap_flags;
275 return (0);
277 return (ENOENT);
280 static int
281 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
283 struct acpi_hpcib_softc *sc = device_get_softc(dev);
285 switch (which) {
286 case PCIB_IVAR_DOMAIN:
287 return (EINVAL);
288 case PCIB_IVAR_BUS:
289 sc->ap_bus = value;
290 return (0);
291 case ACPI_IVAR_HANDLE:
292 sc->ap_handle = (ACPI_HANDLE)value;
293 return (0);
294 case ACPI_IVAR_FLAGS:
295 sc->ap_flags = (int)value;
296 return (0);
298 return (ENOENT);
301 static uint32_t
302 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg,
303 int bytes)
305 return (pci_cfgregread(bus, slot, func, reg, bytes));
308 static void
309 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
310 uint32_t data, int bytes)
312 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
315 static int
316 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
318 struct acpi_hpcib_softc *sc = device_get_softc(pcib);
320 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
322 #ifdef MSI
323 static int
324 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
325 int *irqs)
327 device_t bus;
329 bus = device_get_parent(pcib);
330 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
331 irqs));
334 static int
335 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
337 device_t bus;
339 bus = device_get_parent(pcib);
340 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
343 static int
344 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
345 uint32_t *data)
347 device_t bus;
349 bus = device_get_parent(pcib);
350 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
352 #endif
353 static u_long acpi_host_mem_start = 0x80000000;
354 TUNABLE_INT("hw.acpi.host_mem_start", &acpi_host_mem_start);
356 struct resource *
357 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
358 u_long start, u_long end, u_long count, u_int flags)
361 * If no memory preference is given, use upper 32MB slot most
362 * bioses use for their memory window. Typically other bridges
363 * before us get in the way to assert their preferences on memory.
364 * Hardcoding like this sucks, so a more MD/MI way needs to be
365 * found to do it. This is typically only used on older laptops
366 * that don't have pci busses behind pci bridge, so assuming > 32MB
367 * is liekly OK.
369 if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
370 start = acpi_host_mem_start;
371 if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
372 start = 0x1000;
373 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
374 count, flags));