acpi_pcib.c revision 78993
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.c 78993 2001-06-29 20:32:29Z msmith $
28 */
29#include "opt_acpi.h"
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/bus.h>
33
34#include "acpi.h"
35
36#include <dev/acpica/acpivar.h>
37
38#include <machine/pci_cfgreg.h>
39#include <pci/pcivar.h>
40#include "pcib_if.h"
41
42/*
43 * Hooks for the ACPI CA debugging infrastructure
44 */
45#define _COMPONENT	ACPI_BUS
46MODULE_NAME("PCI")
47
48struct acpi_pcib_softc {
49    device_t		ap_dev;
50    ACPI_HANDLE		ap_handle;
51
52    int			ap_segment;	/* analagous to Alpha 'hose' */
53    int			ap_bus;		/* bios-assigned bus number */
54};
55
56static int		acpi_pcib_probe(device_t bus);
57static int		acpi_pcib_attach(device_t bus);
58static int		acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result);
59static int		acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value);
60static int		acpi_pcib_maxslots(device_t dev);
61static u_int32_t	acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes);
62static void		acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
63					       u_int32_t data, int bytes);
64static int		acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin);
65
66static device_method_t acpi_pcib_methods[] = {
67    /* Device interface */
68    DEVMETHOD(device_probe,		acpi_pcib_probe),
69    DEVMETHOD(device_attach,		acpi_pcib_attach),
70    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
71    DEVMETHOD(device_suspend,		bus_generic_suspend),
72    DEVMETHOD(device_resume,		bus_generic_resume),
73
74    /* Bus interface */
75    DEVMETHOD(bus_print_child,		bus_generic_print_child),
76    DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
77    DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
78    DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
79    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
80    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
81    DEVMETHOD(bus_deactivate_resource, 	bus_generic_deactivate_resource),
82    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
83    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
84
85    /* pcib interface */
86    DEVMETHOD(pcib_maxslots,		acpi_pcib_maxslots),
87    DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
88    DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
89    DEVMETHOD(pcib_route_interrupt,	acpi_pcib_route_interrupt),
90
91    {0, 0}
92};
93
94static driver_t acpi_pcib_driver = {
95    "acpi_pcib",
96    acpi_pcib_methods,
97    sizeof(struct acpi_pcib_softc),
98};
99
100devclass_t acpi_pcib_devclass;
101DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_driver, acpi_pcib_devclass, 0, 0);
102
103static int
104acpi_pcib_probe(device_t dev)
105{
106
107    if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
108	!acpi_disabled("pci") &&
109	acpi_MatchHid(dev, "PNP0A03")) {
110
111	/*
112	 * Set device description
113	 */
114	device_set_desc(dev, "Host-PCI bridge");
115	return(0);
116    }
117    return(ENXIO);
118}
119
120static int
121acpi_pcib_attach(device_t dev)
122{
123    struct acpi_pcib_softc	*sc;
124    device_t			child;
125    ACPI_STATUS			status;
126    int				result;
127
128    FUNCTION_TRACE(__func__);
129
130    sc = device_get_softc(dev);
131    sc->ap_dev = dev;
132    sc->ap_handle = acpi_get_handle(dev);
133
134    /*
135     * Don't attach if we're not really there.
136     *
137     * XXX this isn't entirely correct, since we may be a PCI bus
138     * on a hot-plug docking station, etc.
139     */
140    if (!acpi_DeviceIsPresent(dev))
141	return_VALUE(ENXIO);
142
143    /*
144     * Get our segment number by evaluating _SEG
145     * It's OK for this to not exist.
146     */
147    if ((status = acpi_EvaluateInteger(sc->ap_handle, "_SEG", &sc->ap_segment)) != AE_OK) {
148	if (status != AE_NOT_FOUND) {
149	    device_printf(dev, "could not evaluate _SEG - %s\n", acpi_strerror(status));
150	    return_VALUE(ENXIO);
151	}
152	/* if it's not found, assume 0 */
153	sc->ap_segment = 0;
154    }
155
156    /*
157     * Get our base bus number by evaluating _BBN
158     * If this doesn't exist, we assume we're bus number 0.
159     *
160     * XXX note that it may also not exist in the case where we are
161     *     meant to use a private configuration space mechanism for this bus,
162     *     so we should dig out our resources and check to see if we have
163     *     anything like that.  How do we do this?
164     * XXX If we have the requisite information, and if we don't think the
165     *     default PCI configuration space handlers can deal with this bus,
166     *     we should attach our own handler.
167     * XXX invoke _REG on this for the PCI config space address space?
168     */
169    if ((status = acpi_EvaluateInteger(sc->ap_handle, "_BBN", &sc->ap_bus)) != AE_OK) {
170	if (status != AE_NOT_FOUND) {
171	    device_printf(dev, "could not evaluate _BBN - %s\n", acpi_strerror(status));
172	    return_VALUE(ENXIO);
173	}
174	/* if it's not found, assume 0 */
175	sc->ap_bus = 0;
176    }
177
178    /*
179     * Make sure that this bus hasn't already been found.  If it has, return silently
180     * (should we complain here?).
181     */
182    if (devclass_get_device(devclass_find("pci"), sc->ap_bus) != NULL)
183	return_VALUE(0);
184
185    /*
186     * Attach the PCI bus proper.
187     */
188    if ((child = device_add_child(dev, "pci", sc->ap_bus)) == NULL) {
189	device_printf(device_get_parent(dev), "couldn't attach pci bus");
190	return_VALUE(ENXIO);
191    }
192
193    /*
194     * Now go scan the bus.
195     *
196     * XXX It would be nice to defer this and count on the nexus getting it
197     * after the first pass, but this does not seem to be reliable.
198     */
199    result = bus_generic_attach(dev);
200    return_VALUE(result);
201}
202
203static int
204acpi_pcib_maxslots(device_t dev)
205{
206    return(31);
207}
208
209static int
210acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
211{
212    struct acpi_pcib_softc	*sc = device_get_softc(dev);
213
214    switch (which) {
215    case  PCIB_IVAR_BUS:
216	*result = sc->ap_bus;
217	return(0);
218    }
219    return(ENOENT);
220}
221
222static int
223acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
224{
225    struct acpi_pcib_softc	*sc = device_get_softc(dev);
226
227    switch (which) {
228    case  PCIB_IVAR_BUS:
229	sc->ap_bus = value;
230	return(0);
231    }
232    return(ENOENT);
233}
234
235static u_int32_t
236acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes)
237{
238    return(pci_cfgregread(bus, slot, func, reg, bytes));
239}
240
241static void
242acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, u_int32_t data, int bytes)
243{
244    pci_cfgregwrite(bus, slot, func, reg, data, bytes);
245}
246
247static int
248acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
249{
250    /* XXX this is not the right way to do this! */
251    pci_cfgregopen();
252    return(pci_cfgintr(pci_get_bus(dev), pci_get_slot(dev), pin));
253}
254