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