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