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