xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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/bus.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 
35 #include "acpi.h"
36 
37 #include <dev/acpica/acpivar.h>
38 
39 #include <machine/pci_cfgreg.h>
40 #include <pci/pcivar.h>
41 #include <pci/pcib_private.h>
42 #include "pcib_if.h"
43 
44 #include <dev/acpica/acpi_pcibvar.h>
45 
46 /*
47  * Hooks for the ACPI CA debugging infrastructure
48  */
49 #define _COMPONENT	ACPI_BUS
50 ACPI_MODULE_NAME("PCI_ACPI")
51 
52 struct acpi_hpcib_softc {
53     device_t		ap_dev;
54     ACPI_HANDLE		ap_handle;
55 
56     int			ap_segment;	/* analagous to Alpha 'hose' */
57     int			ap_bus;		/* bios-assigned bus number */
58 
59     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
60 };
61 
62 
63 static int		acpi_pcib_acpi_probe(device_t bus);
64 static int		acpi_pcib_acpi_attach(device_t bus);
65 static int		acpi_pcib_acpi_resume(device_t bus);
66 static int		acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result);
67 static int		acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value);
68 static u_int32_t	acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes);
69 static void		acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
70 					       u_int32_t data, int bytes);
71 static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
72     device_t dev, int pin);
73 
74 static device_method_t acpi_pcib_acpi_methods[] = {
75     /* Device interface */
76     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
77     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
78     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
79     DEVMETHOD(device_suspend,		bus_generic_suspend),
80     DEVMETHOD(device_resume,		acpi_pcib_acpi_resume),
81 
82     /* Bus interface */
83     DEVMETHOD(bus_print_child,		bus_generic_print_child),
84     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
85     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
86     DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
87     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
88     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
89     DEVMETHOD(bus_deactivate_resource, 	bus_generic_deactivate_resource),
90     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
91     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
92 
93     /* pcib interface */
94     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
95     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
96     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
97     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
98 
99     {0, 0}
100 };
101 
102 static driver_t acpi_pcib_acpi_driver = {
103     "pcib",
104     acpi_pcib_acpi_methods,
105     sizeof(struct acpi_hpcib_softc),
106 };
107 
108 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
109 
110 static int
111 acpi_pcib_acpi_probe(device_t dev)
112 {
113 
114     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
115 	!acpi_disabled("pci") &&
116 	acpi_MatchHid(dev, "PNP0A03")) {
117 
118 	if (!pci_cfgregopen())
119 		return(ENXIO);
120 
121 	/*
122 	 * Set device description
123 	 */
124 	device_set_desc(dev, "ACPI Host-PCI bridge");
125 	return(0);
126     }
127     return(ENXIO);
128 }
129 
130 static int
131 acpi_pcib_acpi_attach(device_t dev)
132 {
133     struct acpi_hpcib_softc	*sc;
134     ACPI_STATUS			status;
135 
136     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
137 
138     sc = device_get_softc(dev);
139     sc->ap_dev = dev;
140     sc->ap_handle = acpi_get_handle(dev);
141 
142     /*
143      * Get our base bus number by evaluating _BBN.  If that doesn't work, try _ADR.
144      * If this doesn't work, we assume we're bus number 0.
145      *
146      * XXX note that it may also not exist in the case where we are
147      *     meant to use a private configuration space mechanism for this bus,
148      *     so we should dig out our resources and check to see if we have
149      *     anything like that.  How do we do this?
150      * XXX If we have the requisite information, and if we don't think the
151      *     default PCI configuration space handlers can deal with this bus,
152      *     we should attach our own handler.
153      * XXX invoke _REG on this for the PCI config space address space?
154      */
155     if (ACPI_FAILURE(status = acpi_EvaluateInteger(sc->ap_handle, "_BBN", &sc->ap_bus))) {
156 	if (status != AE_NOT_FOUND) {
157 	    device_printf(dev, "could not evaluate _BBN - %s\n", AcpiFormatException(status));
158 	    return_VALUE(ENXIO);
159 	}
160 	if (ACPI_FAILURE(status = acpi_EvaluateInteger(sc->ap_handle, "_ADR", &sc->ap_bus))) {
161 	    if (status != AE_NOT_FOUND) {
162 		device_printf(dev, "could not evaluate _ADR - %s\n", AcpiFormatException(status));
163 		return_VALUE(ENXIO);
164 	    }
165 	} else {
166 	    /* if it's not found, assume 0 */
167 	    sc->ap_bus = 0;
168 	}
169     }
170 
171     /*
172      * Get our segment number by evaluating _SEG
173      * It's OK for this to not exist.
174      */
175     if (ACPI_FAILURE(status = acpi_EvaluateInteger(sc->ap_handle, "_SEG", &sc->ap_segment))) {
176 	if (status != AE_NOT_FOUND) {
177 	    device_printf(dev, "could not evaluate _SEG - %s\n", AcpiFormatException(status));
178 	    return_VALUE(ENXIO);
179 	}
180 	/* if it's not found, assume 0 */
181 	sc->ap_segment = 0;
182     }
183 
184     return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
185 }
186 
187 static int
188 acpi_pcib_acpi_resume(device_t dev)
189 {
190     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
191 
192     return (acpi_pcib_resume(dev, &sc->ap_prt, sc->ap_bus));
193 }
194 
195 /*
196  * Support for standard PCI bridge ivars.
197  */
198 static int
199 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
200 {
201     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
202 
203     switch (which) {
204     case  PCIB_IVAR_BUS:
205 	*result = sc->ap_bus;
206 	return(0);
207     case  ACPI_IVAR_HANDLE:
208 	*result = (uintptr_t)sc->ap_handle;
209 	return(0);
210     }
211     return(ENOENT);
212 }
213 
214 static int
215 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
216 {
217     struct acpi_hpcib_softc 	*sc = device_get_softc(dev);
218 
219     switch (which) {
220     case  PCIB_IVAR_BUS:
221 	sc->ap_bus = value;
222 	return(0);
223     }
224     return(ENOENT);
225 }
226 
227 static u_int32_t
228 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes)
229 {
230     return(pci_cfgregread(bus, slot, func, reg, bytes));
231 }
232 
233 static void
234 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, u_int32_t data, int bytes)
235 {
236     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
237 }
238 
239 static int
240 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
241 {
242     struct acpi_hpcib_softc *sc;
243 
244     /* find the bridge softc */
245     sc = device_get_softc(pcib);
246     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
247 }
248