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$");
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <sys/sysctl.h>
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
46 #include <dev/acpica/acpivar.h>
48 #include <machine/pci_cfgreg.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcib_private.h>
54 #include <dev/acpica/acpi_pcibvar.h>
56 /* Hooks for the ACPI CA debugging infrastructure. */
57 #define _COMPONENT ACPI_BUS
58 ACPI_MODULE_NAME("PCI_ACPI")
60 struct acpi_hpcib_softc {
62 ACPI_HANDLE ap_handle;
65 int ap_segment; /* PCI domain */
66 int ap_bus; /* bios-assigned bus number */
67 int ap_addr; /* device/func of PCI-Host bridge */
69 ACPI_BUFFER ap_prt; /* interrupt routing table */
71 struct pcib_host_resources ap_host_res;
75 static int acpi_pcib_acpi_probe(device_t bus);
76 static int acpi_pcib_acpi_attach(device_t bus);
77 static int acpi_pcib_read_ivar(device_t dev, device_t child,
78 int which, uintptr_t *result);
79 static int acpi_pcib_write_ivar(device_t dev, device_t child,
80 int which, uintptr_t value);
81 static uint32_t acpi_pcib_read_config(device_t dev, u_int bus,
82 u_int slot, u_int func, u_int reg, int bytes);
83 static void acpi_pcib_write_config(device_t dev, u_int bus,
84 u_int slot, u_int func, u_int reg, uint32_t data,
86 static int acpi_pcib_acpi_route_interrupt(device_t pcib,
87 device_t dev, int pin);
88 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev,
89 int count, int maxcount, int *irqs);
90 static int acpi_pcib_map_msi(device_t pcib, device_t dev,
91 int irq, uint64_t *addr, uint32_t *data);
92 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev,
94 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
95 device_t child, int type, int *rid,
96 rman_res_t start, rman_res_t end, rman_res_t count,
99 static int acpi_pcib_acpi_adjust_resource(device_t dev,
100 device_t child, int type, struct resource *r,
101 rman_res_t start, rman_res_t end);
103 static int acpi_pcib_acpi_release_resource(device_t dev,
104 device_t child, int type, int rid,
109 static device_method_t acpi_pcib_acpi_methods[] = {
110 /* Device interface */
111 DEVMETHOD(device_probe, acpi_pcib_acpi_probe),
112 DEVMETHOD(device_attach, acpi_pcib_acpi_attach),
113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
114 DEVMETHOD(device_suspend, bus_generic_suspend),
115 DEVMETHOD(device_resume, bus_generic_resume),
118 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
119 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar),
120 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource),
122 DEVMETHOD(bus_adjust_resource, acpi_pcib_acpi_adjust_resource),
124 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
126 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
127 DEVMETHOD(bus_release_resource, acpi_pcib_acpi_release_resource),
129 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
131 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
132 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
133 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
134 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
135 DEVMETHOD(bus_get_cpus, acpi_pcib_get_cpus),
138 DEVMETHOD(pcib_maxslots, pcib_maxslots),
139 DEVMETHOD(pcib_read_config, acpi_pcib_read_config),
140 DEVMETHOD(pcib_write_config, acpi_pcib_write_config),
141 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt),
142 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi),
143 DEVMETHOD(pcib_release_msi, pcib_release_msi),
144 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix),
145 DEVMETHOD(pcib_release_msix, pcib_release_msix),
146 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi),
147 DEVMETHOD(pcib_power_for_sleep, acpi_pcib_power_for_sleep),
152 static devclass_t pcib_devclass;
154 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
155 sizeof(struct acpi_hpcib_softc));
156 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
157 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
160 acpi_pcib_acpi_probe(device_t dev)
162 ACPI_DEVICE_INFO *devinfo;
166 if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
167 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
169 root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
171 if (!root || pci_cfgregopen() == 0)
174 device_set_desc(dev, "ACPI Host-PCI bridge");
180 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
182 struct acpi_hpcib_softc *sc;
183 UINT64 length, min, max;
189 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
190 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
191 panic("host bridge has depenedent resources");
192 case ACPI_RESOURCE_TYPE_ADDRESS16:
193 case ACPI_RESOURCE_TYPE_ADDRESS32:
194 case ACPI_RESOURCE_TYPE_ADDRESS64:
195 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
196 if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
199 case ACPI_RESOURCE_TYPE_ADDRESS16:
200 min = res->Data.Address16.Address.Minimum;
201 max = res->Data.Address16.Address.Maximum;
202 length = res->Data.Address16.Address.AddressLength;
204 case ACPI_RESOURCE_TYPE_ADDRESS32:
205 min = res->Data.Address32.Address.Minimum;
206 max = res->Data.Address32.Address.Maximum;
207 length = res->Data.Address32.Address.AddressLength;
209 case ACPI_RESOURCE_TYPE_ADDRESS64:
210 min = res->Data.Address64.Address.Minimum;
211 max = res->Data.Address64.Address.Maximum;
212 length = res->Data.Address64.Address.AddressLength;
216 ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
217 ("should never happen"));
218 min = res->Data.ExtAddress64.Address.Minimum;
219 max = res->Data.ExtAddress64.Address.Maximum;
220 length = res->Data.ExtAddress64.Address.AddressLength;
225 if (min + length - 1 != max &&
226 (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
227 res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
230 switch (res->Data.Address.ResourceType) {
231 case ACPI_MEMORY_RANGE:
232 type = SYS_RES_MEMORY;
233 if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
234 if (res->Data.Address.Info.Mem.Caching ==
235 ACPI_PREFETCHABLE_MEMORY)
236 flags |= RF_PREFETCHABLE;
239 * XXX: Parse prefetch flag out of
245 type = SYS_RES_IOPORT;
248 case ACPI_BUS_NUMBER_RANGE:
256 if (min + length - 1 != max)
257 device_printf(sc->ap_dev,
258 "Length mismatch for %d range: %jx vs %jx\n", type,
259 (uintmax_t)(max - min + 1), (uintmax_t)length);
261 if (min > ULONG_MAX) {
262 device_printf(sc->ap_dev,
263 "Ignoring %d range above 4GB (%#jx-%#jx)\n",
264 type, (uintmax_t)min, (uintmax_t)max);
267 if (max > ULONG_MAX) {
268 device_printf(sc->ap_dev,
269 "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
270 type, (uintmax_t)min, (uintmax_t)max);
274 error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
277 panic("Failed to manage %d range (%#jx-%#jx): %d",
278 type, (uintmax_t)min, (uintmax_t)max, error);
287 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
289 first_decoded_bus(struct acpi_hpcib_softc *sc, rman_res_t *startp)
291 struct resource_list_entry *rle;
293 rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
296 *startp = rle->start;
302 acpi_pcib_osc(struct acpi_hpcib_softc *sc)
307 static uint8_t pci_host_bridge_uuid[ACPI_UUID_LENGTH] = {
308 0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40,
309 0x96, 0x57, 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66
312 /* Support Field: Extended PCI Config Space, MSI */
319 /* Control Field: PCI Express Native Hot Plug */
323 status = acpi_EvaluateOSC(sc->ap_handle, pci_host_bridge_uuid, 1,
324 nitems(cap_set), cap_set, cap_set, false);
325 if (ACPI_FAILURE(status)) {
326 if (status == AE_NOT_FOUND)
328 device_printf(sc->ap_dev, "_OSC failed: %s\n",
329 AcpiFormatException(status));
333 if (cap_set[0] != 0) {
334 device_printf(sc->ap_dev, "_OSC returned error %#x\n",
340 acpi_pcib_acpi_attach(device_t dev)
342 struct acpi_hpcib_softc *sc;
344 static int bus0_seen = 0;
345 u_int slot, func, busok;
346 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
347 struct resource *bus_res;
353 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
355 sc = device_get_softc(dev);
357 sc->ap_handle = acpi_get_handle(dev);
360 * Don't attach if we're not really there.
362 if (!acpi_DeviceIsPresent(dev))
368 * Get our segment number by evaluating _SEG.
369 * It's OK for this to not exist.
371 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
372 if (ACPI_FAILURE(status)) {
373 if (status != AE_NOT_FOUND) {
374 device_printf(dev, "could not evaluate _SEG - %s\n",
375 AcpiFormatException(status));
376 return_VALUE (ENXIO);
378 /* If it's not found, assume 0. */
383 * Get the address (device and function) of the associated
384 * PCI-Host bridge device from _ADR. Assume we don't have one if
387 status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
388 if (ACPI_FAILURE(status)) {
389 device_printf(dev, "could not evaluate _ADR - %s\n",
390 AcpiFormatException(status));
396 * Determine which address ranges this bridge decodes and setup
397 * resource managers for those ranges.
399 if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
400 panic("failed to init hostb resources");
401 if (!acpi_disabled("hostres")) {
402 status = AcpiWalkResources(sc->ap_handle, "_CRS",
403 acpi_pcib_producer_handler, sc);
404 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
405 device_printf(sc->ap_dev, "failed to parse resources: %s\n",
406 AcpiFormatException(status));
411 * Get our base bus number by evaluating _BBN.
412 * If this doesn't work, we assume we're bus number 0.
414 * XXX note that it may also not exist in the case where we are
415 * meant to use a private configuration space mechanism for this bus,
416 * so we should dig out our resources and check to see if we have
417 * anything like that. How do we do this?
418 * XXX If we have the requisite information, and if we don't think the
419 * default PCI configuration space handlers can deal with this bus,
420 * we should attach our own handler.
421 * XXX invoke _REG on this for the PCI config space address space?
422 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
423 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
424 * if _BBN is zero and PCI bus 0 already exists, we try to read our
425 * bus number from the configuration registers at address _ADR.
426 * We only do this for domain/segment 0 in the hopes that this is
427 * only needed for old single-domain machines.
429 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
430 if (ACPI_FAILURE(status)) {
431 if (status != AE_NOT_FOUND) {
432 device_printf(dev, "could not evaluate _BBN - %s\n",
433 AcpiFormatException(status));
436 /* If it's not found, assume 0. */
442 * If this is segment 0, the bus is zero, and PCI bus 0 already
443 * exists, read the bus number via PCI config space.
446 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
448 if (sc->ap_addr != -1) {
449 /* XXX: We assume bus 0. */
450 slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
451 func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
453 device_printf(dev, "reading config registers from 0:%d:%d\n",
455 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
456 device_printf(dev, "couldn't read bus number from cfg space\n");
464 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
466 * If nothing else worked, hope that ACPI at least lays out the
467 * Host-PCI bridges in order and that as a result the next free
468 * bus number is our bus number.
472 * If we have a region of bus numbers, use the first
473 * number for our bus.
475 if (first_decoded_bus(sc, &start) == 0)
479 bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
481 if (bus_res == NULL) {
483 "could not allocate bus number\n");
484 pcib_host_res_free(dev, &sc->ap_host_res);
487 sc->ap_bus = rman_get_start(bus_res);
488 pci_domain_release_bus(sc->ap_segment, dev, rid, bus_res);
492 if (first_decoded_bus(sc, &start) == 0)
493 KASSERT(start == sc->ap_bus, ("bus number mismatch"));
498 * If nothing else worked, hope that ACPI at least lays out the
499 * host-PCI bridges in order and that as a result our unit number
500 * is actually our bus number. There are several reasons this
504 sc->ap_bus = device_get_unit(dev);
505 device_printf(dev, "trying bus number %d\n", sc->ap_bus);
509 /* If this is bus 0 on segment 0, note that it has been seen already. */
510 if (sc->ap_segment == 0 && sc->ap_bus == 0)
513 acpi_pcib_fetch_prt(dev, &sc->ap_prt);
515 if (device_add_child(dev, "pci", -1) == NULL) {
516 device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
519 return (bus_generic_attach(dev));
523 * Support for standard PCI bridge ivars.
526 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
528 struct acpi_hpcib_softc *sc = device_get_softc(dev);
531 case PCIB_IVAR_DOMAIN:
532 *result = sc->ap_segment;
535 *result = sc->ap_bus;
537 case ACPI_IVAR_HANDLE:
538 *result = (uintptr_t)sc->ap_handle;
540 case ACPI_IVAR_FLAGS:
541 *result = (uintptr_t)sc->ap_flags;
548 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
550 struct acpi_hpcib_softc *sc = device_get_softc(dev);
553 case PCIB_IVAR_DOMAIN:
558 case ACPI_IVAR_HANDLE:
559 sc->ap_handle = (ACPI_HANDLE)value;
561 case ACPI_IVAR_FLAGS:
562 sc->ap_flags = (int)value;
569 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
570 u_int reg, int bytes)
572 return (pci_cfgregread(bus, slot, func, reg, bytes));
576 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
577 u_int reg, uint32_t data, int bytes)
579 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
583 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
585 struct acpi_hpcib_softc *sc = device_get_softc(pcib);
587 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
591 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
596 bus = device_get_parent(pcib);
597 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
602 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
606 bus = device_get_parent(pcib);
607 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
611 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
614 struct acpi_hpcib_softc *sc;
618 bus = device_get_parent(pcib);
619 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
623 sc = device_get_softc(pcib);
624 if (sc->ap_addr == -1)
626 /* XXX: Assumes all bridges are on bus 0. */
627 hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
628 ACPI_ADR_PCI_FUNC(sc->ap_addr));
630 pci_ht_map_msi(hostb, *addr);
635 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
636 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
639 struct acpi_hpcib_softc *sc;
640 struct resource *res;
643 #if defined(__i386__) || defined(__amd64__)
644 start = hostb_alloc_start(type, start, end, count);
648 sc = device_get_softc(dev);
650 if (type == PCI_RES_BUS)
651 return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
654 res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
658 * XXX: If this is a request for a specific range, assume it is
659 * correct and pass it up to the parent. What we probably want to
660 * do long-term is explicitly trust any firmware-configured
661 * resources during the initial bus scan on boot and then disable
664 if (res == NULL && start + count - 1 == end)
665 res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
669 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
676 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type,
677 struct resource *r, rman_res_t start, rman_res_t end)
679 struct acpi_hpcib_softc *sc;
681 sc = device_get_softc(dev);
683 if (type == PCI_RES_BUS)
684 return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
687 return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start,
693 acpi_pcib_acpi_release_resource(device_t dev, device_t child, int type, int rid,
696 struct acpi_hpcib_softc *sc;
698 sc = device_get_softc(dev);
699 if (type == PCI_RES_BUS)
700 return (pci_domain_release_bus(sc->ap_segment, child, rid, r));
701 return (bus_generic_release_resource(dev, child, type, rid, r));