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