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