Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / dev / acpica5 / acpi_pcib_acpi.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
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.
14  *
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  *
27  * $FreeBSD: src/sys/dev/acpica/acpi_pcib_acpi.c,v 1.32 2004/04/09 18:14:32 njl Exp $
28  * $DragonFly: src/sys/dev/acpica5/acpi_pcib_acpi.c,v 1.2 2004/06/27 08:52:39 dillon Exp $
29  */
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35
36 #include "acpi.h"
37
38 #include "acpivar.h"
39
40 #include <machine/pci_cfgreg.h>
41 #include <bus/pci/pcivar.h>
42 #include <bus/pci/pcib_private.h>
43 #include "pcib_if.h"
44
45 #include "acpi_pcibvar.h"
46
47 /*
48  * Hooks for the ACPI CA debugging infrastructure
49  */
50 #define _COMPONENT      ACPI_BUS
51 ACPI_MODULE_NAME("PCI_ACPI")
52
53 struct acpi_hpcib_softc {
54     device_t            ap_dev;
55     ACPI_HANDLE         ap_handle;
56
57     int                 ap_segment;     /* analagous to Alpha 'hose' */
58     int                 ap_bus;         /* bios-assigned bus number */
59
60     ACPI_BUFFER         ap_prt;         /* interrupt routing table */
61 };
62
63
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);
74 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
75                             device_t child, int type, int *rid,
76                             u_long start, u_long end, u_long count,
77                             u_int flags);
78
79 static device_method_t acpi_pcib_acpi_methods[] = {
80     /* Device interface */
81     DEVMETHOD(device_probe,             acpi_pcib_acpi_probe),
82     DEVMETHOD(device_attach,            acpi_pcib_acpi_attach),
83     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
84     DEVMETHOD(device_suspend,           bus_generic_suspend),
85     DEVMETHOD(device_resume,            acpi_pcib_acpi_resume),
86
87     /* Bus interface */
88     DEVMETHOD(bus_print_child,          bus_generic_print_child),
89     DEVMETHOD(bus_read_ivar,            acpi_pcib_read_ivar),
90     DEVMETHOD(bus_write_ivar,           acpi_pcib_write_ivar),
91     DEVMETHOD(bus_alloc_resource,       acpi_pcib_acpi_alloc_resource),
92     DEVMETHOD(bus_release_resource,     bus_generic_release_resource),
93     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
94     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
95     DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
96     DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
97
98     /* pcib interface */
99     DEVMETHOD(pcib_maxslots,            pcib_maxslots),
100     DEVMETHOD(pcib_read_config,         acpi_pcib_read_config),
101     DEVMETHOD(pcib_write_config,        acpi_pcib_write_config),
102     DEVMETHOD(pcib_route_interrupt,     acpi_pcib_acpi_route_interrupt),
103
104     {0, 0}
105 };
106
107 static driver_t acpi_pcib_acpi_driver = {
108     "pcib",
109     acpi_pcib_acpi_methods,
110     sizeof(struct acpi_hpcib_softc),
111 };
112
113 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
114 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
115
116 static int
117 acpi_pcib_acpi_probe(device_t dev)
118 {
119
120     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
121         !acpi_disabled("pci") &&
122         acpi_MatchHid(dev, "PNP0A03")) {
123
124         if (!pci_cfgregopen())
125                 return(ENXIO);
126
127         /*
128          * Set device description 
129          */
130         device_set_desc(dev, "ACPI Host-PCI bridge");
131         return(0);
132     }
133     return(ENXIO);
134 }
135
136 static int
137 acpi_pcib_acpi_attach(device_t dev)
138 {
139     struct acpi_hpcib_softc     *sc;
140     ACPI_STATUS                 status;
141     u_int addr, slot, func, busok;
142     uint8_t busno;
143
144     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
145
146     sc = device_get_softc(dev);
147     sc->ap_dev = dev;
148     sc->ap_handle = acpi_get_handle(dev);
149
150     /*
151      * Get our base bus number by evaluating _BBN.
152      * If this doesn't work, we assume we're bus number 0.
153      *
154      * XXX note that it may also not exist in the case where we are 
155      *     meant to use a private configuration space mechanism for this bus,
156      *     so we should dig out our resources and check to see if we have
157      *     anything like that.  How do we do this?
158      * XXX If we have the requisite information, and if we don't think the
159      *     default PCI configuration space handlers can deal with this bus,
160      *     we should attach our own handler.
161      * XXX invoke _REG on this for the PCI config space address space?
162      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
163      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
164      *     if _BBN is zero and pcib0 already exists, we try to read our
165      *     bus number from the configuration registers at address _ADR.
166      */
167     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
168     if (ACPI_FAILURE(status)) {
169         if (status != AE_NOT_FOUND) {
170             device_printf(dev, "could not evaluate _BBN - %s\n",
171                 AcpiFormatException(status));
172             return_VALUE(ENXIO);
173         } else {
174             /* if it's not found, assume 0 */
175             sc->ap_bus = 0;
176         }
177     }
178
179     /*
180      * If the bus is zero and pcib0 already exists, read the bus number
181      * via PCI config space.
182      */
183     busok = 1;
184     if (sc->ap_bus == 0 && devclass_get_device(pcib_devclass, 0) != dev) {
185         busok = 0;
186         status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr);
187         if (ACPI_FAILURE(status)) {
188             if (status != AE_NOT_FOUND) {
189                 device_printf(dev, "could not evaluate _ADR - %s\n",
190                     AcpiFormatException(status));
191                 return_VALUE(ENXIO);
192             } else
193                 device_printf(dev, "could not determine config space address\n");
194         } else {
195             /* XXX: We assume bus 0. */
196             slot = addr >> 16;
197             func = addr & 0xffff;
198             if (bootverbose)
199                 device_printf(dev, "reading config registers from 0:%d:%d\n",
200                     slot, func);
201             if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
202                 device_printf(dev, "could not read bus number from config space\n");
203             else {
204                 sc->ap_bus = busno;
205                 busok = 1;
206             }
207         }
208     }
209
210     /*
211      * If nothing else worked, hope that ACPI at least lays out the
212      * host-PCI bridges in order and that as a result our unit number
213      * is actually our bus number.  There are several reasons this
214      * might not be true.
215      */
216     if (busok == 0) {
217         sc->ap_bus = device_get_unit(dev);
218         device_printf(dev, "trying bus number %d\n", sc->ap_bus);
219     }
220
221     /*
222      * Get our segment number by evaluating _SEG
223      * It's OK for this to not exist.
224      */
225     if (ACPI_FAILURE(status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment))) {
226         if (status != AE_NOT_FOUND) {
227             device_printf(dev, "could not evaluate _SEG - %s\n", AcpiFormatException(status));
228             return_VALUE(ENXIO);
229         }
230         /* if it's not found, assume 0 */
231         sc->ap_segment = 0;
232     }
233
234     return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
235 }
236
237 static int
238 acpi_pcib_acpi_resume(device_t dev)
239 {
240     struct acpi_hpcib_softc     *sc = device_get_softc(dev);
241
242     return (acpi_pcib_resume(dev, &sc->ap_prt, sc->ap_bus));
243 }
244
245 /*
246  * Support for standard PCI bridge ivars.
247  */
248 static int
249 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
250 {
251     struct acpi_hpcib_softc     *sc = device_get_softc(dev);
252
253     switch (which) {
254     case  PCIB_IVAR_BUS:
255         *result = sc->ap_bus;
256         return(0);
257     case  ACPI_IVAR_HANDLE:
258         *result = (uintptr_t)sc->ap_handle;
259         return(0);
260     }
261     return(ENOENT);
262 }
263
264 static int
265 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
266 {
267     struct acpi_hpcib_softc     *sc = device_get_softc(dev);
268
269     switch (which) {
270     case  PCIB_IVAR_BUS:
271         sc->ap_bus = value;
272         return(0);
273     }
274     return(ENOENT);
275 }
276
277 static u_int32_t
278 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes)
279 {
280     return(pci_cfgregread(bus, slot, func, reg, bytes));
281 }
282
283 static void
284 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, u_int32_t data, int bytes)
285 {
286     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
287 }
288
289 static int
290 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
291 {
292     struct acpi_hpcib_softc *sc;
293
294     /* find the bridge softc */
295     sc = device_get_softc(pcib);
296     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
297 }
298
299 struct resource *
300 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
301   u_long start, u_long end, u_long count, u_int flags)
302 {
303         /*
304          * If no memory preference is given, use upper 256MB slot most
305          * bioses use for their memory window.  Typically other bridges
306          * before us get in the way to assert their preferences on memory.
307          * Hardcoding like this sucks, so a more MD/MI way needs to be
308          * found to do it.
309          */
310         if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
311                 start = 0xf0000000;
312         return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
313             count, flags));
314 }