2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/sysctl.h>
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
44 #include <dev/acpica/acpivar.h>
46 #include <machine/pci_cfgreg.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcib_private.h>
52 #include <dev/acpica/acpi_pcibvar.h>
54 /* Hooks for the ACPI CA debugging infrastructure. */
55 #define _COMPONENT ACPI_BUS
56 ACPI_MODULE_NAME("PCI_ACPI")
58 struct acpi_hpcib_softc {
60 ACPI_HANDLE ap_handle;
63 int ap_segment; /* PCI domain */
64 int ap_bus; /* bios-assigned bus number */
65 int ap_addr; /* device/func of PCI-Host bridge */
67 ACPI_BUFFER ap_prt; /* interrupt routing table */
69 struct pcib_host_resources ap_host_res;
73 static int acpi_pcib_acpi_probe(device_t bus);
74 static int acpi_pcib_acpi_attach(device_t bus);
75 static int acpi_pcib_read_ivar(device_t dev, device_t child,
76 int which, uintptr_t *result);
77 static int acpi_pcib_write_ivar(device_t dev, device_t child,
78 int which, uintptr_t value);
79 static uint32_t acpi_pcib_read_config(device_t dev, u_int bus,
80 u_int slot, u_int func, u_int reg, int bytes);
81 static void acpi_pcib_write_config(device_t dev, u_int bus,
82 u_int slot, u_int func, u_int reg, uint32_t data,
84 static int acpi_pcib_acpi_route_interrupt(device_t pcib,
85 device_t dev, int pin);
86 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev,
87 int count, int maxcount, int *irqs);
88 static int acpi_pcib_map_msi(device_t pcib, device_t dev,
89 int irq, uint64_t *addr, uint32_t *data);
90 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev,
92 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
93 device_t child, int type, int *rid,
94 rman_res_t start, rman_res_t end, rman_res_t count,
97 static int acpi_pcib_acpi_adjust_resource(device_t dev,
98 device_t child, int type, struct resource *r,
99 rman_res_t start, rman_res_t end);
101 static int acpi_pcib_acpi_release_resource(device_t dev,
102 device_t child, int type, int rid,
107 static device_method_t acpi_pcib_acpi_methods[] = {
108 /* Device interface */
109 DEVMETHOD(device_probe, acpi_pcib_acpi_probe),
110 DEVMETHOD(device_attach, acpi_pcib_acpi_attach),
111 DEVMETHOD(device_shutdown, bus_generic_shutdown),
112 DEVMETHOD(device_suspend, bus_generic_suspend),
113 DEVMETHOD(device_resume, bus_generic_resume),
116 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
117 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar),
118 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource),
120 DEVMETHOD(bus_adjust_resource, acpi_pcib_acpi_adjust_resource),
122 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
124 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
125 DEVMETHOD(bus_release_resource, acpi_pcib_acpi_release_resource),
127 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
129 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
130 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
131 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
132 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
135 DEVMETHOD(pcib_maxslots, pcib_maxslots),
136 DEVMETHOD(pcib_read_config, acpi_pcib_read_config),
137 DEVMETHOD(pcib_write_config, acpi_pcib_write_config),
138 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt),
139 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi),
140 DEVMETHOD(pcib_release_msi, pcib_release_msi),
141 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix),
142 DEVMETHOD(pcib_release_msix, pcib_release_msix),
143 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi),
144 DEVMETHOD(pcib_power_for_sleep, acpi_pcib_power_for_sleep),
149 static devclass_t pcib_devclass;
151 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
152 sizeof(struct acpi_hpcib_softc));
153 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
154 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
157 acpi_pcib_acpi_probe(device_t dev)
159 ACPI_DEVICE_INFO *devinfo;
163 if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
164 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
166 root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
168 if (!root || pci_cfgregopen() == 0)
171 device_set_desc(dev, "ACPI Host-PCI bridge");
177 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
179 struct acpi_hpcib_softc *sc;
180 UINT64 length, min, max;
186 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
187 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
188 panic("host bridge has depenedent resources");
189 case ACPI_RESOURCE_TYPE_ADDRESS16:
190 case ACPI_RESOURCE_TYPE_ADDRESS32:
191 case ACPI_RESOURCE_TYPE_ADDRESS64:
192 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
193 if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
196 case ACPI_RESOURCE_TYPE_ADDRESS16:
197 min = res->Data.Address16.Address.Minimum;
198 max = res->Data.Address16.Address.Maximum;
199 length = res->Data.Address16.Address.AddressLength;
201 case ACPI_RESOURCE_TYPE_ADDRESS32:
202 min = res->Data.Address32.Address.Minimum;
203 max = res->Data.Address32.Address.Maximum;
204 length = res->Data.Address32.Address.AddressLength;
206 case ACPI_RESOURCE_TYPE_ADDRESS64:
207 min = res->Data.Address64.Address.Minimum;
208 max = res->Data.Address64.Address.Maximum;
209 length = res->Data.Address64.Address.AddressLength;
213 ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
214 ("should never happen"));
215 min = res->Data.ExtAddress64.Address.Minimum;
216 max = res->Data.ExtAddress64.Address.Maximum;
217 length = res->Data.ExtAddress64.Address.AddressLength;
222 if (min + length - 1 != max &&
223 (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
224 res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
227 switch (res->Data.Address.ResourceType) {
228 case ACPI_MEMORY_RANGE:
229 type = SYS_RES_MEMORY;
230 if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
231 if (res->Data.Address.Info.Mem.Caching ==
232 ACPI_PREFETCHABLE_MEMORY)
233 flags |= RF_PREFETCHABLE;
236 * XXX: Parse prefetch flag out of
242 type = SYS_RES_IOPORT;
245 case ACPI_BUS_NUMBER_RANGE:
253 if (min + length - 1 != max)
254 device_printf(sc->ap_dev,
255 "Length mismatch for %d range: %jx vs %jx\n", type,
256 (uintmax_t)(max - min + 1), (uintmax_t)length);
258 if (min > ULONG_MAX) {
259 device_printf(sc->ap_dev,
260 "Ignoring %d range above 4GB (%#jx-%#jx)\n",
261 type, (uintmax_t)min, (uintmax_t)max);
264 if (max > ULONG_MAX) {
265 device_printf(sc->ap_dev,
266 "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
267 type, (uintmax_t)min, (uintmax_t)max);
271 error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
274 panic("Failed to manage %d range (%#jx-%#jx): %d",
275 type, (uintmax_t)min, (uintmax_t)max, error);
284 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
286 first_decoded_bus(struct acpi_hpcib_softc *sc, rman_res_t *startp)
288 struct resource_list_entry *rle;
290 rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
293 *startp = rle->start;
299 acpi_pcib_acpi_attach(device_t dev)
301 struct acpi_hpcib_softc *sc;
303 static int bus0_seen = 0;
304 u_int slot, func, busok;
305 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
306 struct resource *bus_res;
312 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
314 sc = device_get_softc(dev);
316 sc->ap_handle = acpi_get_handle(dev);
319 * Don't attach if we're not really there.
321 if (!acpi_DeviceIsPresent(dev))
325 * Get our segment number by evaluating _SEG.
326 * It's OK for this to not exist.
328 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
329 if (ACPI_FAILURE(status)) {
330 if (status != AE_NOT_FOUND) {
331 device_printf(dev, "could not evaluate _SEG - %s\n",
332 AcpiFormatException(status));
333 return_VALUE (ENXIO);
335 /* If it's not found, assume 0. */
340 * Get the address (device and function) of the associated
341 * PCI-Host bridge device from _ADR. Assume we don't have one if
344 status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
345 if (ACPI_FAILURE(status)) {
346 device_printf(dev, "could not evaluate _ADR - %s\n",
347 AcpiFormatException(status));
353 * Determine which address ranges this bridge decodes and setup
354 * resource managers for those ranges.
356 if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
357 panic("failed to init hostb resources");
358 if (!acpi_disabled("hostres")) {
359 status = AcpiWalkResources(sc->ap_handle, "_CRS",
360 acpi_pcib_producer_handler, sc);
361 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
362 device_printf(sc->ap_dev, "failed to parse resources: %s\n",
363 AcpiFormatException(status));
368 * Get our base bus number by evaluating _BBN.
369 * If this doesn't work, we assume we're bus number 0.
371 * XXX note that it may also not exist in the case where we are
372 * meant to use a private configuration space mechanism for this bus,
373 * so we should dig out our resources and check to see if we have
374 * anything like that. How do we do this?
375 * XXX If we have the requisite information, and if we don't think the
376 * default PCI configuration space handlers can deal with this bus,
377 * we should attach our own handler.
378 * XXX invoke _REG on this for the PCI config space address space?
379 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
380 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
381 * if _BBN is zero and PCI bus 0 already exists, we try to read our
382 * bus number from the configuration registers at address _ADR.
383 * We only do this for domain/segment 0 in the hopes that this is
384 * only needed for old single-domain machines.
386 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
387 if (ACPI_FAILURE(status)) {
388 if (status != AE_NOT_FOUND) {
389 device_printf(dev, "could not evaluate _BBN - %s\n",
390 AcpiFormatException(status));
393 /* If it's not found, assume 0. */
399 * If this is segment 0, the bus is zero, and PCI bus 0 already
400 * exists, read the bus number via PCI config space.
403 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
405 if (sc->ap_addr != -1) {
406 /* XXX: We assume bus 0. */
407 slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
408 func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
410 device_printf(dev, "reading config registers from 0:%d:%d\n",
412 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
413 device_printf(dev, "couldn't read bus number from cfg space\n");
421 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
423 * If nothing else worked, hope that ACPI at least lays out the
424 * Host-PCI bridges in order and that as a result the next free
425 * bus number is our bus number.
429 * If we have a region of bus numbers, use the first
430 * number for our bus.
432 if (first_decoded_bus(sc, &start) == 0)
436 bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
438 if (bus_res == NULL) {
440 "could not allocate bus number\n");
441 pcib_host_res_free(dev, &sc->ap_host_res);
444 sc->ap_bus = rman_get_start(bus_res);
445 pci_domain_release_bus(sc->ap_segment, dev, rid, bus_res);
449 if (first_decoded_bus(sc, &start) == 0)
450 KASSERT(start == sc->ap_bus, ("bus number mismatch"));
455 * If nothing else worked, hope that ACPI at least lays out the
456 * host-PCI bridges in order and that as a result our unit number
457 * is actually our bus number. There are several reasons this
461 sc->ap_bus = device_get_unit(dev);
462 device_printf(dev, "trying bus number %d\n", sc->ap_bus);
466 /* If this is bus 0 on segment 0, note that it has been seen already. */
467 if (sc->ap_segment == 0 && sc->ap_bus == 0)
470 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
474 * Support for standard PCI bridge ivars.
477 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
479 struct acpi_hpcib_softc *sc = device_get_softc(dev);
482 case PCIB_IVAR_DOMAIN:
483 *result = sc->ap_segment;
486 *result = sc->ap_bus;
488 case ACPI_IVAR_HANDLE:
489 *result = (uintptr_t)sc->ap_handle;
491 case ACPI_IVAR_FLAGS:
492 *result = (uintptr_t)sc->ap_flags;
499 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
501 struct acpi_hpcib_softc *sc = device_get_softc(dev);
504 case PCIB_IVAR_DOMAIN:
509 case ACPI_IVAR_HANDLE:
510 sc->ap_handle = (ACPI_HANDLE)value;
512 case ACPI_IVAR_FLAGS:
513 sc->ap_flags = (int)value;
520 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
521 u_int reg, int bytes)
523 return (pci_cfgregread(bus, slot, func, reg, bytes));
527 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
528 u_int reg, uint32_t data, int bytes)
530 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
534 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
536 struct acpi_hpcib_softc *sc = device_get_softc(pcib);
538 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
542 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
547 bus = device_get_parent(pcib);
548 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
553 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
557 bus = device_get_parent(pcib);
558 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
562 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
565 struct acpi_hpcib_softc *sc;
569 bus = device_get_parent(pcib);
570 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
574 sc = device_get_softc(pcib);
575 if (sc->ap_addr == -1)
577 /* XXX: Assumes all bridges are on bus 0. */
578 hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
579 ACPI_ADR_PCI_FUNC(sc->ap_addr));
581 pci_ht_map_msi(hostb, *addr);
586 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
587 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
590 struct acpi_hpcib_softc *sc;
591 struct resource *res;
594 #if defined(__i386__) || defined(__amd64__)
595 start = hostb_alloc_start(type, start, end, count);
599 sc = device_get_softc(dev);
601 if (type == PCI_RES_BUS)
602 return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
605 res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
609 * XXX: If this is a request for a specific range, assume it is
610 * correct and pass it up to the parent. What we probably want to
611 * do long-term is explicitly trust any firmware-configured
612 * resources during the initial bus scan on boot and then disable
615 if (res == NULL && start + count - 1 == end)
616 res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
620 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
627 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type,
628 struct resource *r, rman_res_t start, rman_res_t end)
630 struct acpi_hpcib_softc *sc;
632 sc = device_get_softc(dev);
634 if (type == PCI_RES_BUS)
635 return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
638 return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start,
644 acpi_pcib_acpi_release_resource(device_t dev, device_t child, int type, int rid,
647 struct acpi_hpcib_softc *sc;
649 sc = device_get_softc(dev);
650 if (type == PCI_RES_BUS)
651 return (pci_domain_release_bus(sc->ap_segment, child, rid, r));
652 return (bus_generic_release_resource(dev, child, type, rid, r));