2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
27 * $FreeBSD: src/sys/dev/acpica/acpi_pcib_acpi.c,v 1.29 2003/08/22 06:06:16 imp Exp $
28 * $DragonFly: src/sys/dev/acpica5/acpi_pcib_acpi.c,v 1.1 2004/02/21 06:48:08 dillon Exp $
31 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
40 #include <machine/pci_cfgreg.h>
41 #include <bus/pci/pcivar.h>
42 #include <bus/pci/pcib_private.h>
45 #include "acpi_pcibvar.h"
48 * Hooks for the ACPI CA debugging infrastructure
50 #define _COMPONENT ACPI_BUS
51 ACPI_MODULE_NAME("PCI_ACPI")
53 struct acpi_hpcib_softc {
55 ACPI_HANDLE ap_handle;
57 int ap_segment; /* analagous to Alpha 'hose' */
58 int ap_bus; /* bios-assigned bus number */
60 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, int which, uintptr_t *result);
68 static int acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value);
69 static u_int32_t acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes);
70 static void acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
71 u_int32_t data, int bytes);
72 static int acpi_pcib_acpi_route_interrupt(device_t pcib,
73 device_t dev, int pin);
75 static device_method_t acpi_pcib_acpi_methods[] = {
76 /* Device interface */
77 DEVMETHOD(device_probe, acpi_pcib_acpi_probe),
78 DEVMETHOD(device_attach, acpi_pcib_acpi_attach),
79 DEVMETHOD(device_shutdown, bus_generic_shutdown),
80 DEVMETHOD(device_suspend, bus_generic_suspend),
81 DEVMETHOD(device_resume, acpi_pcib_acpi_resume),
84 DEVMETHOD(bus_print_child, bus_generic_print_child),
85 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
86 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar),
87 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
88 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
89 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
90 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
91 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
92 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
95 DEVMETHOD(pcib_maxslots, pcib_maxslots),
96 DEVMETHOD(pcib_read_config, acpi_pcib_read_config),
97 DEVMETHOD(pcib_write_config, acpi_pcib_write_config),
98 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt),
103 static driver_t acpi_pcib_acpi_driver = {
105 acpi_pcib_acpi_methods,
106 sizeof(struct acpi_hpcib_softc),
109 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
112 acpi_pcib_acpi_probe(device_t dev)
115 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
116 !acpi_disabled("pci") &&
117 acpi_MatchHid(dev, "PNP0A03")) {
119 if (!pci_cfgregopen())
123 * Set device description
125 device_set_desc(dev, "ACPI Host-PCI bridge");
132 acpi_pcib_acpi_attach(device_t dev)
134 struct acpi_hpcib_softc *sc;
136 u_int addr, slot, func, busok;
139 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
141 sc = device_get_softc(dev);
143 sc->ap_handle = acpi_get_handle(dev);
146 * Get our base bus number by evaluating _BBN.
147 * If this doesn't work, we assume we're bus number 0.
149 * XXX note that it may also not exist in the case where we are
150 * meant to use a private configuration space mechanism for this bus,
151 * so we should dig out our resources and check to see if we have
152 * anything like that. How do we do this?
153 * XXX If we have the requisite information, and if we don't think the
154 * default PCI configuration space handlers can deal with this bus,
155 * we should attach our own handler.
156 * XXX invoke _REG on this for the PCI config space address space?
157 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
158 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
159 * if _BBN is zero and pcib0 already exists, we try to read our
160 * bus number from the configuration registers at address _ADR.
162 status = acpi_EvaluateInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
163 if (ACPI_FAILURE(status)) {
164 if (status != AE_NOT_FOUND) {
165 device_printf(dev, "could not evaluate _BBN - %s\n",
166 AcpiFormatException(status));
169 /* if it's not found, assume 0 */
175 * If the bus is zero and pcib0 already exists, read the bus number
176 * via PCI config space.
179 if (sc->ap_bus == 0 && devclass_get_device(pcib_devclass, 0) != dev) {
181 status = acpi_EvaluateInteger(sc->ap_handle, "_ADR", &addr);
182 if (ACPI_FAILURE(status)) {
183 if (status != AE_NOT_FOUND) {
184 device_printf(dev, "could not evaluate _ADR - %s\n",
185 AcpiFormatException(status));
188 device_printf(dev, "could not determine config space address\n");
190 /* XXX: We assume bus 0. */
192 func = addr & 0xffff;
194 device_printf(dev, "reading config registers from 0:%d:%d\n",
196 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
197 device_printf(dev, "could not read bus number from config space\n");
206 * If nothing else worked, hope that ACPI at least lays out the
207 * host-PCI bridges in order and that as a result our unit number
208 * is actually our bus number. There are several reasons this
212 sc->ap_bus = device_get_unit(dev);
213 device_printf(dev, "trying bus number %d\n", sc->ap_bus);
217 * Get our segment number by evaluating _SEG
218 * It's OK for this to not exist.
220 if (ACPI_FAILURE(status = acpi_EvaluateInteger(sc->ap_handle, "_SEG", &sc->ap_segment))) {
221 if (status != AE_NOT_FOUND) {
222 device_printf(dev, "could not evaluate _SEG - %s\n", AcpiFormatException(status));
225 /* if it's not found, assume 0 */
229 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
233 acpi_pcib_acpi_resume(device_t dev)
235 struct acpi_hpcib_softc *sc = device_get_softc(dev);
237 return (acpi_pcib_resume(dev, &sc->ap_prt, sc->ap_bus));
241 * Support for standard PCI bridge ivars.
244 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
246 struct acpi_hpcib_softc *sc = device_get_softc(dev);
250 *result = sc->ap_bus;
252 case ACPI_IVAR_HANDLE:
253 *result = (uintptr_t)sc->ap_handle;
260 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
262 struct acpi_hpcib_softc *sc = device_get_softc(dev);
273 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes)
275 return(pci_cfgregread(bus, slot, func, reg, bytes));
279 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, u_int32_t data, int bytes)
281 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
285 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
287 struct acpi_hpcib_softc *sc;
289 /* find the bridge softc */
290 sc = device_get_softc(pcib);
291 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));