]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/acpica/acpi_pcib_acpi.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / acpica / 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
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>
38
39 #include <contrib/dev/acpica/include/acpi.h>
40 #include <contrib/dev/acpica/include/accommon.h>
41
42 #include <dev/acpica/acpivar.h>
43
44 #include <machine/pci_cfgreg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcib_private.h>
47 #include "pcib_if.h"
48
49 #include <dev/acpica/acpi_pcibvar.h>
50
51 /* Hooks for the ACPI CA debugging infrastructure. */
52 #define _COMPONENT      ACPI_BUS
53 ACPI_MODULE_NAME("PCI_ACPI")
54
55 struct acpi_hpcib_softc {
56     device_t            ap_dev;
57     ACPI_HANDLE         ap_handle;
58     int                 ap_flags;
59
60     int                 ap_segment;     /* analagous to Alpha 'hose' */
61     int                 ap_bus;         /* bios-assigned bus number */
62
63     ACPI_BUFFER         ap_prt;         /* interrupt routing table */
64 };
65
66 static int              acpi_pcib_acpi_probe(device_t bus);
67 static int              acpi_pcib_acpi_attach(device_t bus);
68 static int              acpi_pcib_acpi_resume(device_t bus);
69 static int              acpi_pcib_read_ivar(device_t dev, device_t child,
70                             int which, uintptr_t *result);
71 static int              acpi_pcib_write_ivar(device_t dev, device_t child,
72                             int which, uintptr_t value);
73 static uint32_t         acpi_pcib_read_config(device_t dev, u_int bus,
74                             u_int slot, u_int func, u_int reg, int bytes);
75 static void             acpi_pcib_write_config(device_t dev, u_int bus,
76                             u_int slot, u_int func, u_int reg, uint32_t data,
77                             int bytes);
78 static int              acpi_pcib_acpi_route_interrupt(device_t pcib,
79                             device_t dev, int pin);
80 static int              acpi_pcib_alloc_msi(device_t pcib, device_t dev,
81                             int count, int maxcount, int *irqs);
82 static int              acpi_pcib_map_msi(device_t pcib, device_t dev,
83                             int irq, uint64_t *addr, uint32_t *data);
84 static int              acpi_pcib_alloc_msix(device_t pcib, device_t dev,
85                             int *irq);
86 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
87                             device_t child, int type, int *rid,
88                             u_long start, u_long end, u_long count,
89                             u_int flags);
90
91 static device_method_t acpi_pcib_acpi_methods[] = {
92     /* Device interface */
93     DEVMETHOD(device_probe,             acpi_pcib_acpi_probe),
94     DEVMETHOD(device_attach,            acpi_pcib_acpi_attach),
95     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
96     DEVMETHOD(device_suspend,           bus_generic_suspend),
97     DEVMETHOD(device_resume,            acpi_pcib_acpi_resume),
98
99     /* Bus interface */
100     DEVMETHOD(bus_print_child,          bus_generic_print_child),
101     DEVMETHOD(bus_read_ivar,            acpi_pcib_read_ivar),
102     DEVMETHOD(bus_write_ivar,           acpi_pcib_write_ivar),
103     DEVMETHOD(bus_alloc_resource,       acpi_pcib_acpi_alloc_resource),
104     DEVMETHOD(bus_release_resource,     bus_generic_release_resource),
105     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
106     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
107     DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
108     DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
109
110     /* pcib interface */
111     DEVMETHOD(pcib_maxslots,            pcib_maxslots),
112     DEVMETHOD(pcib_read_config,         acpi_pcib_read_config),
113     DEVMETHOD(pcib_write_config,        acpi_pcib_write_config),
114     DEVMETHOD(pcib_route_interrupt,     acpi_pcib_acpi_route_interrupt),
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
121     {0, 0}
122 };
123
124 static devclass_t pcib_devclass;
125
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);
130
131 static int
132 acpi_pcib_acpi_probe(device_t dev)
133 {
134     static char *pcib_ids[] = { "PNP0A03", "PNP0A08", NULL };
135
136     if (acpi_disabled("pcib") ||
137         ACPI_ID_PROBE(device_get_parent(dev), dev, pcib_ids) == NULL)
138         return (ENXIO);
139
140     if (pci_cfgregopen() == 0)
141         return (ENXIO);
142     device_set_desc(dev, "ACPI Host-PCI bridge");
143     return (0);
144 }
145
146 static int
147 acpi_pcib_acpi_attach(device_t dev)
148 {
149     struct acpi_hpcib_softc     *sc;
150     ACPI_STATUS                 status;
151     static int bus0_seen = 0;
152     u_int addr, slot, func, busok;
153     uint8_t busno;
154
155     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
156
157     sc = device_get_softc(dev);
158     sc->ap_dev = dev;
159     sc->ap_handle = acpi_get_handle(dev);
160
161     /*
162      * Get our segment number by evaluating _SEG
163      * It's OK for this to not exist.
164      */
165     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
166     if (ACPI_FAILURE(status)) {
167         if (status != AE_NOT_FOUND) {
168             device_printf(dev, "could not evaluate _SEG - %s\n",
169                 AcpiFormatException(status));
170             return_VALUE (ENXIO);
171         }
172         /* If it's not found, assume 0. */
173         sc->ap_segment = 0;
174     }
175
176     /*
177      * Get our base bus number by evaluating _BBN.
178      * If this doesn't work, we assume we're bus number 0.
179      *
180      * XXX note that it may also not exist in the case where we are
181      *     meant to use a private configuration space mechanism for this bus,
182      *     so we should dig out our resources and check to see if we have
183      *     anything like that.  How do we do this?
184      * XXX If we have the requisite information, and if we don't think the
185      *     default PCI configuration space handlers can deal with this bus,
186      *     we should attach our own handler.
187      * XXX invoke _REG on this for the PCI config space address space?
188      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
189      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
190      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
191      *     bus number from the configuration registers at address _ADR.
192      *     We only do this for domain/segment 0 in the hopes that this is
193      *     only needed for old single-domain machines.
194      */
195     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
196     if (ACPI_FAILURE(status)) {
197         if (status != AE_NOT_FOUND) {
198             device_printf(dev, "could not evaluate _BBN - %s\n",
199                 AcpiFormatException(status));
200             return_VALUE (ENXIO);
201         } else {
202             /* If it's not found, assume 0. */
203             sc->ap_bus = 0;
204         }
205     }
206
207     /*
208      * If this is segment 0, the bus is zero, and PCI bus 0 already
209      * exists, read the bus number via PCI config space.
210      */
211     busok = 1;
212     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
213         busok = 0;
214         status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr);
215         if (ACPI_FAILURE(status)) {
216             if (status != AE_NOT_FOUND) {
217                 device_printf(dev, "could not evaluate _ADR - %s\n",
218                     AcpiFormatException(status));
219                 return_VALUE (ENXIO);
220             } else
221                 device_printf(dev, "couldn't find _ADR\n");
222         } else {
223             /* XXX: We assume bus 0. */
224             slot = ACPI_ADR_PCI_SLOT(addr);
225             func = ACPI_ADR_PCI_FUNC(addr);
226             if (bootverbose)
227                 device_printf(dev, "reading config registers from 0:%d:%d\n",
228                     slot, func);
229             if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
230                 device_printf(dev, "couldn't read bus number from cfg space\n");
231             else {
232                 sc->ap_bus = busno;
233                 busok = 1;
234             }
235         }
236     }
237
238     /*
239      * If nothing else worked, hope that ACPI at least lays out the
240      * host-PCI bridges in order and that as a result our unit number
241      * is actually our bus number.  There are several reasons this
242      * might not be true.
243      */
244     if (busok == 0) {
245         sc->ap_bus = device_get_unit(dev);
246         device_printf(dev, "trying bus number %d\n", sc->ap_bus);
247     }
248
249     /* If this is bus 0 on segment 0, note that it has been seen already. */
250     if (sc->ap_segment == 0 && sc->ap_bus == 0)
251             bus0_seen = 1;
252
253     return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
254 }
255
256 static int
257 acpi_pcib_acpi_resume(device_t dev)
258 {
259
260     return (acpi_pcib_resume(dev));
261 }
262
263 /*
264  * Support for standard PCI bridge ivars.
265  */
266 static int
267 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
268 {
269     struct acpi_hpcib_softc     *sc = device_get_softc(dev);
270
271     switch (which) {
272     case PCIB_IVAR_DOMAIN:
273         *result = 0;
274         return (0);
275     case PCIB_IVAR_BUS:
276         *result = sc->ap_bus;
277         return (0);
278     case ACPI_IVAR_HANDLE:
279         *result = (uintptr_t)sc->ap_handle;
280         return (0);
281     case ACPI_IVAR_FLAGS:
282         *result = (uintptr_t)sc->ap_flags;
283         return (0);
284     }
285     return (ENOENT);
286 }
287
288 static int
289 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
290 {
291     struct acpi_hpcib_softc     *sc = device_get_softc(dev);
292
293     switch (which) {
294     case PCIB_IVAR_DOMAIN:
295         return (EINVAL);
296     case PCIB_IVAR_BUS:
297         sc->ap_bus = value;
298         return (0);
299     case ACPI_IVAR_HANDLE:
300         sc->ap_handle = (ACPI_HANDLE)value;
301         return (0);
302     case ACPI_IVAR_FLAGS:
303         sc->ap_flags = (int)value;
304         return (0);
305     }
306     return (ENOENT);
307 }
308
309 static uint32_t
310 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
311     u_int reg, int bytes)
312 {
313     return (pci_cfgregread(bus, slot, func, reg, bytes));
314 }
315
316 static void
317 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
318     u_int reg, uint32_t data, int bytes)
319 {
320     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
321 }
322
323 static int
324 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
325 {
326     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
327
328     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
329 }
330
331 static int
332 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
333     int *irqs)
334 {
335         device_t bus;
336
337         bus = device_get_parent(pcib);
338         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
339             irqs));
340 }
341
342 static int
343 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
344 {
345         device_t bus;
346
347         bus = device_get_parent(pcib);
348         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
349 }
350
351 static int
352 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
353     uint32_t *data)
354 {
355         device_t bus;
356
357         bus = device_get_parent(pcib);
358         return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
359 }
360
361 static u_long acpi_host_mem_start = 0x80000000;
362 TUNABLE_ULONG("hw.acpi.host_mem_start", &acpi_host_mem_start);
363
364 struct resource *
365 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
366     u_long start, u_long end, u_long count, u_int flags)
367 {
368     /*
369      * If no memory preference is given, use upper 32MB slot most
370      * bioses use for their memory window.  Typically other bridges
371      * before us get in the way to assert their preferences on memory.
372      * Hardcoding like this sucks, so a more MD/MI way needs to be
373      * found to do it.  This is typically only used on older laptops
374      * that don't have pci busses behind pci bridge, so assuming > 32MB
375      * is liekly OK.
376      */
377     if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
378         start = acpi_host_mem_start;
379     if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
380         start = 0x1000;
381     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
382         count, flags));
383 }