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