acpi_pcib_acpi.c revision 136189
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: head/sys/dev/acpica/acpi_pcib_acpi.c 136189 2004-10-06 07:26:52Z imp $
28 */
29#include "opt_acpi.h"
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/malloc.h>
34#include <sys/module.h>
35
36#include "acpi.h"
37#include <dev/acpica/acpivar.h>
38
39#include <machine/pci_cfgreg.h>
40#include <dev/pci/pcivar.h>
41#include <dev/pci/pcib_private.h>
42#include "pcib_if.h"
43
44#include <dev/acpica/acpi_pcibvar.h>
45
46/* Hooks for the ACPI CA debugging infrastructure. */
47#define _COMPONENT	ACPI_BUS
48ACPI_MODULE_NAME("PCI_ACPI")
49
50struct acpi_hpcib_softc {
51    device_t		ap_dev;
52    ACPI_HANDLE		ap_handle;
53    int			ap_flags;
54
55    int			ap_segment;	/* analagous to Alpha 'hose' */
56    int			ap_bus;		/* bios-assigned bus number */
57
58    ACPI_BUFFER		ap_prt;		/* interrupt routing table */
59};
60
61static int		acpi_pcib_acpi_probe(device_t bus);
62static int		acpi_pcib_acpi_attach(device_t bus);
63static int		acpi_pcib_acpi_resume(device_t bus);
64static int		acpi_pcib_read_ivar(device_t dev, device_t child,
65			    int which, uintptr_t *result);
66static int		acpi_pcib_write_ivar(device_t dev, device_t child,
67			    int which, uintptr_t value);
68static uint32_t		acpi_pcib_read_config(device_t dev, int bus, int slot,
69			    int func, int reg, int bytes);
70static void		acpi_pcib_write_config(device_t dev, int bus, int slot,
71			    int func, int reg, uint32_t data, int bytes);
72static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
73			    device_t dev, int pin);
74static 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
79static 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
107static driver_t acpi_pcib_acpi_driver = {
108    "pcib",
109    acpi_pcib_acpi_methods,
110    sizeof(struct acpi_hpcib_softc),
111};
112
113DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
114MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
115
116static int
117acpi_pcib_acpi_probe(device_t dev)
118{
119    static char *pcib_ids[] = { "PNP0A03", NULL };
120
121    if (acpi_disabled("pcib") ||
122	ACPI_ID_PROBE(device_get_parent(dev), dev, pcib_ids) == NULL)
123	return (ENXIO);
124
125    if (pci_cfgregopen() == 0)
126	return (ENXIO);
127    device_set_desc(dev, "ACPI Host-PCI bridge");
128    return (0);
129}
130
131static int
132acpi_pcib_acpi_attach(device_t dev)
133{
134    struct acpi_hpcib_softc	*sc;
135    ACPI_STATUS			status;
136    u_int addr, slot, func, busok;
137    uint8_t busno;
138
139    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
140
141    sc = device_get_softc(dev);
142    sc->ap_dev = dev;
143    sc->ap_handle = acpi_get_handle(dev);
144
145    /*
146     * Get our base bus number by evaluating _BBN.
147     * If this doesn't work, we assume we're bus number 0.
148     *
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.
161     */
162    status = acpi_GetInteger(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));
167	    return_VALUE (ENXIO);
168	} else {
169	    /* If it's not found, assume 0. */
170	    sc->ap_bus = 0;
171	}
172    }
173
174    /*
175     * If the bus is zero and pcib0 already exists, read the bus number
176     * via PCI config space.
177     */
178    busok = 1;
179    if (sc->ap_bus == 0 && devclass_get_device(pcib_devclass, 0) != dev) {
180	busok = 0;
181	status = acpi_GetInteger(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));
186		return_VALUE (ENXIO);
187	    } else
188		device_printf(dev, "couldn't find _ADR\n");
189	} else {
190	    /* XXX: We assume bus 0. */
191	    slot = ACPI_ADR_PCI_SLOT(addr);
192	    func = ACPI_ADR_PCI_FUNC(addr);
193	    if (bootverbose)
194		device_printf(dev, "reading config registers from 0:%d:%d\n",
195		    slot, func);
196	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
197		device_printf(dev, "couldn't read bus number from cfg space\n");
198	    else {
199		sc->ap_bus = busno;
200		busok = 1;
201	    }
202	}
203    }
204
205    /*
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
209     * might not be true.
210     */
211    if (busok == 0) {
212	sc->ap_bus = device_get_unit(dev);
213	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
214    }
215
216    /*
217     * Get our segment number by evaluating _SEG
218     * It's OK for this to not exist.
219     */
220    status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
221    if (ACPI_FAILURE(status)) {
222	if (status != AE_NOT_FOUND) {
223	    device_printf(dev, "could not evaluate _SEG - %s\n",
224		AcpiFormatException(status));
225	    return_VALUE (ENXIO);
226	}
227	/* If it's not found, assume 0. */
228	sc->ap_segment = 0;
229    }
230
231    return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
232}
233
234static int
235acpi_pcib_acpi_resume(device_t dev)
236{
237
238    return (acpi_pcib_resume(dev));
239}
240
241/*
242 * Support for standard PCI bridge ivars.
243 */
244static int
245acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
246{
247    struct acpi_hpcib_softc	*sc = device_get_softc(dev);
248
249    switch (which) {
250    case PCIB_IVAR_BUS:
251	*result = sc->ap_bus;
252	return (0);
253    case ACPI_IVAR_HANDLE:
254	*result = (uintptr_t)sc->ap_handle;
255	return (0);
256    case ACPI_IVAR_FLAGS:
257	*result = (uintptr_t)sc->ap_flags;
258	return (0);
259    }
260    return (ENOENT);
261}
262
263static int
264acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
265{
266    struct acpi_hpcib_softc 	*sc = device_get_softc(dev);
267
268    switch (which) {
269    case PCIB_IVAR_BUS:
270	sc->ap_bus = value;
271	return (0);
272    case ACPI_IVAR_HANDLE:
273	sc->ap_handle = (ACPI_HANDLE)value;
274	return (0);
275    case ACPI_IVAR_FLAGS:
276	sc->ap_flags = (int)value;
277	return (0);
278    }
279    return (ENOENT);
280}
281
282static uint32_t
283acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg,
284    int bytes)
285{
286    return (pci_cfgregread(bus, slot, func, reg, bytes));
287}
288
289static void
290acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
291    uint32_t data, int bytes)
292{
293    pci_cfgregwrite(bus, slot, func, reg, data, bytes);
294}
295
296static int
297acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
298{
299
300    return (acpi_pcib_route_interrupt(pcib, dev, pin));
301}
302
303struct resource *
304acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
305    u_long start, u_long end, u_long count, u_int flags)
306{
307    /*
308     * If no memory preference is given, use upper 32MB slot most
309     * bioses use for their memory window.  Typically other bridges
310     * before us get in the way to assert their preferences on memory.
311     * Hardcoding like this sucks, so a more MD/MI way needs to be
312     * found to do it.  This is typically only used on older laptops
313     * that don't have pci busses behind pci bridge, so assuming > 32MB
314     * is liekly OK.
315     */
316    if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
317	start = 0xfe000000;
318    return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
319	count, flags));
320}
321