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