xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision 28586889c23b86d81983c35c5754f55f0f4b877d)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Michael Smith
315e32d5dSMike Smith  * Copyright (c) 2000 BSDi
415e32d5dSMike Smith  * All rights reserved.
515e32d5dSMike Smith  *
615e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
715e32d5dSMike Smith  * modification, are permitted provided that the following conditions
815e32d5dSMike Smith  * are met:
915e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1015e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1115e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1315e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1415e32d5dSMike Smith  *
1515e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1615e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1715e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1815e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1915e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2015e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2115e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2215e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2315e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2415e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2515e32d5dSMike Smith  * SUCH DAMAGE.
2615e32d5dSMike Smith  */
27dad97feeSDavid E. O'Brien 
28dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
29dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$");
30dad97feeSDavid E. O'Brien 
3115e32d5dSMike Smith #include "opt_acpi.h"
3282cb5c3bSJohn Baldwin #include "opt_pci.h"
3382cb5c3bSJohn Baldwin 
3415e32d5dSMike Smith #include <sys/param.h>
3515e32d5dSMike Smith #include <sys/bus.h>
364fa387b6SMike Smith #include <sys/kernel.h>
3734ff71eeSJohn Baldwin #include <sys/limits.h>
38e3aa81b8SNate Lawson #include <sys/malloc.h>
39fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4083c41143SJohn Baldwin #include <sys/rman.h>
41fd492ee0SWarner Losh #include <sys/sysctl.h>
4215e32d5dSMike Smith 
43129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
44129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
45129d3046SJung-uk Kim 
4615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/pci_cfgreg.h>
494edef187SJohn Baldwin #include <dev/pci/pcireg.h>
50cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
51cace7a2aSWarner Losh #include <dev/pci/pcib_private.h>
5215e32d5dSMike Smith #include "pcib_if.h"
5315e32d5dSMike Smith 
542ccfc932SJohn Baldwin #include <dev/acpica/acpi_pcibvar.h>
552ccfc932SJohn Baldwin 
56e3aa81b8SNate Lawson /* Hooks for the ACPI CA debugging infrastructure. */
57e71b6381SMike Smith #define _COMPONENT	ACPI_BUS
582ccfc932SJohn Baldwin ACPI_MODULE_NAME("PCI_ACPI")
590ae55423SMike Smith 
602ccfc932SJohn Baldwin struct acpi_hpcib_softc {
6115e32d5dSMike Smith     device_t		ap_dev;
6215e32d5dSMike Smith     ACPI_HANDLE		ap_handle;
63d4b9ff91SNate Lawson     int			ap_flags;
64*28586889SWarner Losh     uint32_t		ap_osc_ctl;
6515e32d5dSMike Smith 
660d95597cSJohn Baldwin     int			ap_segment;	/* PCI domain */
6715e32d5dSMike Smith     int			ap_bus;		/* bios-assigned bus number */
680d95597cSJohn Baldwin     int			ap_addr;	/* device/func of PCI-Host bridge */
694fa387b6SMike Smith 
704fa387b6SMike Smith     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
7134ff71eeSJohn Baldwin #ifdef NEW_PCIB
7234ff71eeSJohn Baldwin     struct pcib_host_resources ap_host_res;
7334ff71eeSJohn Baldwin #endif
7415e32d5dSMike Smith };
7515e32d5dSMike Smith 
762ccfc932SJohn Baldwin static int		acpi_pcib_acpi_probe(device_t bus);
772ccfc932SJohn Baldwin static int		acpi_pcib_acpi_attach(device_t bus);
78e3aa81b8SNate Lawson static int		acpi_pcib_read_ivar(device_t dev, device_t child,
79e3aa81b8SNate Lawson 			    int which, uintptr_t *result);
80e3aa81b8SNate Lawson static int		acpi_pcib_write_ivar(device_t dev, device_t child,
81e3aa81b8SNate Lawson 			    int which, uintptr_t value);
821496d4a9SWarner Losh static uint32_t		acpi_pcib_read_config(device_t dev, u_int bus,
831496d4a9SWarner Losh 			    u_int slot, u_int func, u_int reg, int bytes);
841496d4a9SWarner Losh static void		acpi_pcib_write_config(device_t dev, u_int bus,
851496d4a9SWarner Losh 			    u_int slot, u_int func, u_int reg, uint32_t data,
861496d4a9SWarner Losh 			    int bytes);
872ccfc932SJohn Baldwin static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
882ccfc932SJohn Baldwin 			    device_t dev, int pin);
898964299aSJohn Baldwin static int		acpi_pcib_alloc_msi(device_t pcib, device_t dev,
908964299aSJohn Baldwin 			    int count, int maxcount, int *irqs);
91e706f7f0SJohn Baldwin static int		acpi_pcib_map_msi(device_t pcib, device_t dev,
92e706f7f0SJohn Baldwin 			    int irq, uint64_t *addr, uint32_t *data);
938964299aSJohn Baldwin static int		acpi_pcib_alloc_msix(device_t pcib, device_t dev,
94e706f7f0SJohn Baldwin 			    int *irq);
95cd8b53edSWarner Losh static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
96cd8b53edSWarner Losh 			    device_t child, int type, int *rid,
972dd1bdf1SJustin Hibbits 			    rman_res_t start, rman_res_t end, rman_res_t count,
98cd8b53edSWarner Losh 			    u_int flags);
9934ff71eeSJohn Baldwin #ifdef NEW_PCIB
10034ff71eeSJohn Baldwin static int		acpi_pcib_acpi_adjust_resource(device_t dev,
10134ff71eeSJohn Baldwin 			    device_t child, int type, struct resource *r,
1022dd1bdf1SJustin Hibbits 			    rman_res_t start, rman_res_t end);
1034edef187SJohn Baldwin #ifdef PCI_RES_BUS
1044edef187SJohn Baldwin static int		acpi_pcib_acpi_release_resource(device_t dev,
1054edef187SJohn Baldwin 			    device_t child, int type, int rid,
1064edef187SJohn Baldwin 			    struct resource *r);
1074edef187SJohn Baldwin #endif
10834ff71eeSJohn Baldwin #endif
109*28586889SWarner Losh static int		acpi_pcib_request_feature(device_t pcib, device_t dev,
110*28586889SWarner Losh 			    enum pci_feature feature);
11115e32d5dSMike Smith 
1122ccfc932SJohn Baldwin static device_method_t acpi_pcib_acpi_methods[] = {
11315e32d5dSMike Smith     /* Device interface */
1142ccfc932SJohn Baldwin     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
1152ccfc932SJohn Baldwin     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
11615e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
11715e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
1187d23a9b3SJohn Baldwin     DEVMETHOD(device_resume,		bus_generic_resume),
11915e32d5dSMike Smith 
12015e32d5dSMike Smith     /* Bus interface */
12115e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
12215e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
123cd8b53edSWarner Losh     DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
12434ff71eeSJohn Baldwin #ifdef NEW_PCIB
12534ff71eeSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	acpi_pcib_acpi_adjust_resource),
12634ff71eeSJohn Baldwin #else
127d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
12834ff71eeSJohn Baldwin #endif
1294edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
1304edef187SJohn Baldwin     DEVMETHOD(bus_release_resource,	acpi_pcib_acpi_release_resource),
1314edef187SJohn Baldwin #else
13215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
1334edef187SJohn Baldwin #endif
13415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
13515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
13615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
13715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1388d791e5aSJohn Baldwin     DEVMETHOD(bus_get_cpus,		acpi_pcib_get_cpus),
13915e32d5dSMike Smith 
14015e32d5dSMike Smith     /* pcib interface */
1412ccfc932SJohn Baldwin     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
14215e32d5dSMike Smith     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
14315e32d5dSMike Smith     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
1442ccfc932SJohn Baldwin     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
1458964299aSJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
1469bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1478964299aSJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
1489bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
149e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
15062508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
151*28586889SWarner Losh     DEVMETHOD(pcib_request_feature,	acpi_pcib_request_feature),
15215e32d5dSMike Smith 
1534b7ec270SMarius Strobl     DEVMETHOD_END
15415e32d5dSMike Smith };
15515e32d5dSMike Smith 
15604dda605SJohn Baldwin static devclass_t pcib_devclass;
15715e32d5dSMike Smith 
15804dda605SJohn Baldwin DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
15904dda605SJohn Baldwin     sizeof(struct acpi_hpcib_softc));
1602ccfc932SJohn Baldwin DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
16164278df5SNate Lawson MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
16215e32d5dSMike Smith 
16315e32d5dSMike Smith static int
1642ccfc932SJohn Baldwin acpi_pcib_acpi_probe(device_t dev)
16515e32d5dSMike Smith {
16692488a57SJung-uk Kim     ACPI_DEVICE_INFO	*devinfo;
16792488a57SJung-uk Kim     ACPI_HANDLE		h;
16892488a57SJung-uk Kim     int			root;
16915e32d5dSMike Smith 
17092488a57SJung-uk Kim     if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
17192488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
17292488a57SJung-uk Kim 	return (ENXIO);
17392488a57SJung-uk Kim     root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
17492488a57SJung-uk Kim     AcpiOsFree(devinfo);
17592488a57SJung-uk Kim     if (!root || pci_cfgregopen() == 0)
1765fcc8a58SNate Lawson 	return (ENXIO);
17715e32d5dSMike Smith 
1782ccfc932SJohn Baldwin     device_set_desc(dev, "ACPI Host-PCI bridge");
17915e32d5dSMike Smith     return (0);
18015e32d5dSMike Smith }
18115e32d5dSMike Smith 
18234ff71eeSJohn Baldwin #ifdef NEW_PCIB
18334ff71eeSJohn Baldwin static ACPI_STATUS
18434ff71eeSJohn Baldwin acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
18534ff71eeSJohn Baldwin {
18634ff71eeSJohn Baldwin 	struct acpi_hpcib_softc *sc;
18734ff71eeSJohn Baldwin 	UINT64 length, min, max;
18834ff71eeSJohn Baldwin 	u_int flags;
18934ff71eeSJohn Baldwin 	int error, type;
19034ff71eeSJohn Baldwin 
19134ff71eeSJohn Baldwin 	sc = context;
19234ff71eeSJohn Baldwin 	switch (res->Type) {
19334ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
19434ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
19534ff71eeSJohn Baldwin 		panic("host bridge has depenedent resources");
19634ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS16:
19734ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS32:
19834ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS64:
19934ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
20034ff71eeSJohn Baldwin 		if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
20134ff71eeSJohn Baldwin 			break;
20234ff71eeSJohn Baldwin 		switch (res->Type) {
20334ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS16:
2047cf3e94aSJung-uk Kim 			min = res->Data.Address16.Address.Minimum;
2057cf3e94aSJung-uk Kim 			max = res->Data.Address16.Address.Maximum;
2067cf3e94aSJung-uk Kim 			length = res->Data.Address16.Address.AddressLength;
20734ff71eeSJohn Baldwin 			break;
20834ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS32:
2097cf3e94aSJung-uk Kim 			min = res->Data.Address32.Address.Minimum;
2107cf3e94aSJung-uk Kim 			max = res->Data.Address32.Address.Maximum;
2117cf3e94aSJung-uk Kim 			length = res->Data.Address32.Address.AddressLength;
21234ff71eeSJohn Baldwin 			break;
21334ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS64:
2147cf3e94aSJung-uk Kim 			min = res->Data.Address64.Address.Minimum;
2157cf3e94aSJung-uk Kim 			max = res->Data.Address64.Address.Maximum;
2167cf3e94aSJung-uk Kim 			length = res->Data.Address64.Address.AddressLength;
21734ff71eeSJohn Baldwin 			break;
21834ff71eeSJohn Baldwin 		default:
21934ff71eeSJohn Baldwin 			KASSERT(res->Type ==
22034ff71eeSJohn Baldwin 			    ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
22134ff71eeSJohn Baldwin 			    ("should never happen"));
2227cf3e94aSJung-uk Kim 			min = res->Data.ExtAddress64.Address.Minimum;
2237cf3e94aSJung-uk Kim 			max = res->Data.ExtAddress64.Address.Maximum;
2247cf3e94aSJung-uk Kim 			length = res->Data.ExtAddress64.Address.AddressLength;
22534ff71eeSJohn Baldwin 			break;
22634ff71eeSJohn Baldwin 		}
227e9f91b2bSJohn Baldwin 		if (length == 0)
228e9f91b2bSJohn Baldwin 			break;
229e9f91b2bSJohn Baldwin 		if (min + length - 1 != max &&
230e9f91b2bSJohn Baldwin 		    (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
231e9f91b2bSJohn Baldwin 		    res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
23234ff71eeSJohn Baldwin 			break;
23334ff71eeSJohn Baldwin 		flags = 0;
23434ff71eeSJohn Baldwin 		switch (res->Data.Address.ResourceType) {
23534ff71eeSJohn Baldwin 		case ACPI_MEMORY_RANGE:
23634ff71eeSJohn Baldwin 			type = SYS_RES_MEMORY;
23734ff71eeSJohn Baldwin 			if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
23834ff71eeSJohn Baldwin 				if (res->Data.Address.Info.Mem.Caching ==
23934ff71eeSJohn Baldwin 				    ACPI_PREFETCHABLE_MEMORY)
24034ff71eeSJohn Baldwin 					flags |= RF_PREFETCHABLE;
24134ff71eeSJohn Baldwin 			} else {
24234ff71eeSJohn Baldwin 				/*
24334ff71eeSJohn Baldwin 				 * XXX: Parse prefetch flag out of
24434ff71eeSJohn Baldwin 				 * TypeSpecific.
24534ff71eeSJohn Baldwin 				 */
24634ff71eeSJohn Baldwin 			}
24734ff71eeSJohn Baldwin 			break;
24834ff71eeSJohn Baldwin 		case ACPI_IO_RANGE:
24934ff71eeSJohn Baldwin 			type = SYS_RES_IOPORT;
25034ff71eeSJohn Baldwin 			break;
25134ff71eeSJohn Baldwin #ifdef PCI_RES_BUS
25234ff71eeSJohn Baldwin 		case ACPI_BUS_NUMBER_RANGE:
25334ff71eeSJohn Baldwin 			type = PCI_RES_BUS;
25434ff71eeSJohn Baldwin 			break;
25534ff71eeSJohn Baldwin #endif
25634ff71eeSJohn Baldwin 		default:
25734ff71eeSJohn Baldwin 			return (AE_OK);
25834ff71eeSJohn Baldwin 		}
25934ff71eeSJohn Baldwin 
26034ff71eeSJohn Baldwin 		if (min + length - 1 != max)
26134ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
26234ff71eeSJohn Baldwin 			    "Length mismatch for %d range: %jx vs %jx\n", type,
2630c10b85aSJung-uk Kim 			    (uintmax_t)(max - min + 1), (uintmax_t)length);
26434ff71eeSJohn Baldwin #ifdef __i386__
26534ff71eeSJohn Baldwin 		if (min > ULONG_MAX) {
26634ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
26734ff71eeSJohn Baldwin 			    "Ignoring %d range above 4GB (%#jx-%#jx)\n",
26834ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max);
26934ff71eeSJohn Baldwin 			break;
27034ff71eeSJohn Baldwin 		}
27134ff71eeSJohn Baldwin 		if (max > ULONG_MAX) {
27234ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
27334ff71eeSJohn Baldwin        		    "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
27434ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max);
27534ff71eeSJohn Baldwin 			max = ULONG_MAX;
27634ff71eeSJohn Baldwin 		}
27734ff71eeSJohn Baldwin #endif
27834ff71eeSJohn Baldwin 		error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
27934ff71eeSJohn Baldwin 		    flags);
28034ff71eeSJohn Baldwin 		if (error)
28134ff71eeSJohn Baldwin 			panic("Failed to manage %d range (%#jx-%#jx): %d",
28234ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max, error);
28334ff71eeSJohn Baldwin 		break;
28434ff71eeSJohn Baldwin 	default:
28534ff71eeSJohn Baldwin 		break;
28634ff71eeSJohn Baldwin 	}
28734ff71eeSJohn Baldwin 	return (AE_OK);
28834ff71eeSJohn Baldwin }
28934ff71eeSJohn Baldwin #endif
29034ff71eeSJohn Baldwin 
2914edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
2924edef187SJohn Baldwin static int
2932dd1bdf1SJustin Hibbits first_decoded_bus(struct acpi_hpcib_softc *sc, rman_res_t *startp)
2944edef187SJohn Baldwin {
2954edef187SJohn Baldwin 	struct resource_list_entry *rle;
2964edef187SJohn Baldwin 
2974edef187SJohn Baldwin 	rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
2984edef187SJohn Baldwin 	if (rle == NULL)
2994edef187SJohn Baldwin 		return (ENXIO);
3004edef187SJohn Baldwin 	*startp = rle->start;
3014edef187SJohn Baldwin 	return (0);
3024edef187SJohn Baldwin }
3034edef187SJohn Baldwin #endif
3044edef187SJohn Baldwin 
305*28586889SWarner Losh static int
306*28586889SWarner Losh acpi_pcib_osc(struct acpi_hpcib_softc *sc, uint32_t osc_ctl)
307508c21b6SJohn Baldwin {
308508c21b6SJohn Baldwin 	ACPI_STATUS status;
309508c21b6SJohn Baldwin 	uint32_t cap_set[3];
310508c21b6SJohn Baldwin 
311508c21b6SJohn Baldwin 	static uint8_t pci_host_bridge_uuid[ACPI_UUID_LENGTH] = {
312508c21b6SJohn Baldwin 		0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40,
313508c21b6SJohn Baldwin 		0x96, 0x57, 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66
314508c21b6SJohn Baldwin 	};
315508c21b6SJohn Baldwin 
316c21a6664SWarner Losh 	/* Status Field */
317c21a6664SWarner Losh 	cap_set[PCI_OSC_STATUS] = 0;
318c21a6664SWarner Losh 
319508c21b6SJohn Baldwin 	/* Support Field: Extended PCI Config Space, MSI */
320c21a6664SWarner Losh 	cap_set[PCI_OSC_SUPPORT] = PCIM_OSC_SUPPORT_EXT_PCI_CONF |
321c21a6664SWarner Losh 	    PCIM_OSC_SUPPORT_MSI;
322508c21b6SJohn Baldwin 
323508c21b6SJohn Baldwin 	/* Control Field */
324*28586889SWarner Losh 	sc->ap_osc_ctl |= osc_ctl;
325*28586889SWarner Losh 	cap_set[PCI_OSC_CTL] = sc->ap_osc_ctl;
32682cb5c3bSJohn Baldwin 
327508c21b6SJohn Baldwin 	status = acpi_EvaluateOSC(sc->ap_handle, pci_host_bridge_uuid, 1,
3284c26ac69SJohn Baldwin 	    nitems(cap_set), cap_set, cap_set, false);
329508c21b6SJohn Baldwin 	if (ACPI_FAILURE(status)) {
330508c21b6SJohn Baldwin 		if (status == AE_NOT_FOUND)
331*28586889SWarner Losh 			return (0);
332508c21b6SJohn Baldwin 		device_printf(sc->ap_dev, "_OSC failed: %s\n",
333508c21b6SJohn Baldwin 		    AcpiFormatException(status));
334*28586889SWarner Losh 		return (EIO);
335508c21b6SJohn Baldwin 	}
336508c21b6SJohn Baldwin 
337*28586889SWarner Losh 	if (cap_set[PCI_OSC_STATUS] == 0)
338*28586889SWarner Losh 		sc->ap_osc_ctl = cap_set[PCI_OSC_CTL];
339c21a6664SWarner Losh 
340*28586889SWarner Losh 	if (cap_set[PCI_OSC_STATUS] != 0 ||
341*28586889SWarner Losh 	    (cap_set[PCI_OSC_CTL] & osc_ctl) != osc_ctl)
342*28586889SWarner Losh 		return (EIO);
343*28586889SWarner Losh 
344*28586889SWarner Losh 	return (0);
345508c21b6SJohn Baldwin }
346508c21b6SJohn Baldwin 
34715e32d5dSMike Smith static int
3482ccfc932SJohn Baldwin acpi_pcib_acpi_attach(device_t dev)
34915e32d5dSMike Smith {
3502ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc;
35115e32d5dSMike Smith     ACPI_STATUS			status;
3520668724bSJohn Baldwin     static int bus0_seen = 0;
3530d95597cSJohn Baldwin     u_int slot, func, busok;
3544edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
3554edef187SJohn Baldwin     struct resource *bus_res;
3562dd1bdf1SJustin Hibbits     rman_res_t start;
3574edef187SJohn Baldwin     int rid;
3584edef187SJohn Baldwin #endif
3599debb532SJohn Baldwin     uint8_t busno;
3600ae55423SMike Smith 
361b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
36215e32d5dSMike Smith 
36315e32d5dSMike Smith     sc = device_get_softc(dev);
36415e32d5dSMike Smith     sc->ap_dev = dev;
36515e32d5dSMike Smith     sc->ap_handle = acpi_get_handle(dev);
36615e32d5dSMike Smith 
36715e32d5dSMike Smith     /*
3688f4c2e52SJohn Baldwin      * Don't attach if we're not really there.
3698f4c2e52SJohn Baldwin      */
3708f4c2e52SJohn Baldwin     if (!acpi_DeviceIsPresent(dev))
3718f4c2e52SJohn Baldwin 	return (ENXIO);
3728f4c2e52SJohn Baldwin 
373*28586889SWarner Losh     acpi_pcib_osc(sc, 0);
374508c21b6SJohn Baldwin 
3758f4c2e52SJohn Baldwin     /*
3760d95597cSJohn Baldwin      * Get our segment number by evaluating _SEG.
3770668724bSJohn Baldwin      * It's OK for this to not exist.
3780668724bSJohn Baldwin      */
3790668724bSJohn Baldwin     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
3800668724bSJohn Baldwin     if (ACPI_FAILURE(status)) {
3810668724bSJohn Baldwin 	if (status != AE_NOT_FOUND) {
3820668724bSJohn Baldwin 	    device_printf(dev, "could not evaluate _SEG - %s\n",
3830668724bSJohn Baldwin 		AcpiFormatException(status));
3840668724bSJohn Baldwin 	    return_VALUE (ENXIO);
3850668724bSJohn Baldwin 	}
3860668724bSJohn Baldwin 	/* If it's not found, assume 0. */
3870668724bSJohn Baldwin 	sc->ap_segment = 0;
3880668724bSJohn Baldwin     }
3890668724bSJohn Baldwin 
3900d95597cSJohn Baldwin     /*
3910d95597cSJohn Baldwin      * Get the address (device and function) of the associated
3920d95597cSJohn Baldwin      * PCI-Host bridge device from _ADR.  Assume we don't have one if
3930d95597cSJohn Baldwin      * it doesn't exist.
3940d95597cSJohn Baldwin      */
3950d95597cSJohn Baldwin     status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
3960d95597cSJohn Baldwin     if (ACPI_FAILURE(status)) {
3970d95597cSJohn Baldwin 	device_printf(dev, "could not evaluate _ADR - %s\n",
3980d95597cSJohn Baldwin 	    AcpiFormatException(status));
3990d95597cSJohn Baldwin 	sc->ap_addr = -1;
4000d95597cSJohn Baldwin     }
4010d95597cSJohn Baldwin 
40234ff71eeSJohn Baldwin #ifdef NEW_PCIB
40334ff71eeSJohn Baldwin     /*
40434ff71eeSJohn Baldwin      * Determine which address ranges this bridge decodes and setup
40534ff71eeSJohn Baldwin      * resource managers for those ranges.
40634ff71eeSJohn Baldwin      */
40734ff71eeSJohn Baldwin     if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
40834ff71eeSJohn Baldwin 	    panic("failed to init hostb resources");
40934ff71eeSJohn Baldwin     if (!acpi_disabled("hostres")) {
41034ff71eeSJohn Baldwin 	status = AcpiWalkResources(sc->ap_handle, "_CRS",
41134ff71eeSJohn Baldwin 	    acpi_pcib_producer_handler, sc);
41234ff71eeSJohn Baldwin 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
41334ff71eeSJohn Baldwin 	    device_printf(sc->ap_dev, "failed to parse resources: %s\n",
41434ff71eeSJohn Baldwin 		AcpiFormatException(status));
41534ff71eeSJohn Baldwin     }
41634ff71eeSJohn Baldwin #endif
41734ff71eeSJohn Baldwin 
4180668724bSJohn Baldwin     /*
4199debb532SJohn Baldwin      * Get our base bus number by evaluating _BBN.
420c1b1f787SMike Smith      * If this doesn't work, we assume we're bus number 0.
42115e32d5dSMike Smith      *
42215e32d5dSMike Smith      * XXX note that it may also not exist in the case where we are
42315e32d5dSMike Smith      *     meant to use a private configuration space mechanism for this bus,
42415e32d5dSMike Smith      *     so we should dig out our resources and check to see if we have
42515e32d5dSMike Smith      *     anything like that.  How do we do this?
426042283a6SMike Smith      * XXX If we have the requisite information, and if we don't think the
427042283a6SMike Smith      *     default PCI configuration space handlers can deal with this bus,
428042283a6SMike Smith      *     we should attach our own handler.
429042283a6SMike Smith      * XXX invoke _REG on this for the PCI config space address space?
4309debb532SJohn Baldwin      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
4319debb532SJohn Baldwin      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
4320668724bSJohn Baldwin      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
4339debb532SJohn Baldwin      *     bus number from the configuration registers at address _ADR.
4340668724bSJohn Baldwin      *     We only do this for domain/segment 0 in the hopes that this is
4350668724bSJohn Baldwin      *     only needed for old single-domain machines.
43615e32d5dSMike Smith      */
437c310653eSNate Lawson     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
4389debb532SJohn Baldwin     if (ACPI_FAILURE(status)) {
43915e32d5dSMike Smith 	if (status != AE_NOT_FOUND) {
4409debb532SJohn Baldwin 	    device_printf(dev, "could not evaluate _BBN - %s\n",
4419debb532SJohn Baldwin 		AcpiFormatException(status));
4428f4c2e52SJohn Baldwin 	    return (ENXIO);
443c1b1f787SMike Smith 	} else {
444e3aa81b8SNate Lawson 	    /* If it's not found, assume 0. */
44515e32d5dSMike Smith 	    sc->ap_bus = 0;
44615e32d5dSMike Smith 	}
4478745ec57SJohn Baldwin     }
4489debb532SJohn Baldwin 
4499debb532SJohn Baldwin     /*
4500668724bSJohn Baldwin      * If this is segment 0, the bus is zero, and PCI bus 0 already
4510668724bSJohn Baldwin      * exists, read the bus number via PCI config space.
4529debb532SJohn Baldwin      */
4539debb532SJohn Baldwin     busok = 1;
4540668724bSJohn Baldwin     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
4558745ec57SJohn Baldwin 	busok = 0;
4560d95597cSJohn Baldwin 	if (sc->ap_addr != -1) {
4579debb532SJohn Baldwin 	    /* XXX: We assume bus 0. */
4580d95597cSJohn Baldwin 	    slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
4590d95597cSJohn Baldwin 	    func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
4609debb532SJohn Baldwin 	    if (bootverbose)
4619debb532SJohn Baldwin 		device_printf(dev, "reading config registers from 0:%d:%d\n",
4629debb532SJohn Baldwin 		    slot, func);
4638745ec57SJohn Baldwin 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
464e3aa81b8SNate Lawson 		device_printf(dev, "couldn't read bus number from cfg space\n");
4658745ec57SJohn Baldwin 	    else {
4669debb532SJohn Baldwin 		sc->ap_bus = busno;
4678745ec57SJohn Baldwin 		busok = 1;
4689debb532SJohn Baldwin 	    }
4699debb532SJohn Baldwin 	}
4709debb532SJohn Baldwin     }
4719debb532SJohn Baldwin 
4724edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
4734edef187SJohn Baldwin     /*
4744edef187SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
4754edef187SJohn Baldwin      * Host-PCI bridges in order and that as a result the next free
4764edef187SJohn Baldwin      * bus number is our bus number.
4774edef187SJohn Baldwin      */
4784edef187SJohn Baldwin     if (busok == 0) {
4794edef187SJohn Baldwin 	    /*
4804edef187SJohn Baldwin 	     * If we have a region of bus numbers, use the first
4814edef187SJohn Baldwin 	     * number for our bus.
4824edef187SJohn Baldwin 	     */
4834edef187SJohn Baldwin 	    if (first_decoded_bus(sc, &start) == 0)
4844edef187SJohn Baldwin 		    sc->ap_bus = start;
4854edef187SJohn Baldwin 	    else {
4864edef187SJohn Baldwin 		    rid = 0;
4874edef187SJohn Baldwin 		    bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
4884edef187SJohn Baldwin 			PCI_BUSMAX, 1, 0);
4894edef187SJohn Baldwin 		    if (bus_res == NULL) {
4904edef187SJohn Baldwin 			    device_printf(dev,
4914edef187SJohn Baldwin 				"could not allocate bus number\n");
4924edef187SJohn Baldwin 			    pcib_host_res_free(dev, &sc->ap_host_res);
4934edef187SJohn Baldwin 			    return (ENXIO);
4944edef187SJohn Baldwin 		    }
4954edef187SJohn Baldwin 		    sc->ap_bus = rman_get_start(bus_res);
4964edef187SJohn Baldwin 		    pci_domain_release_bus(sc->ap_segment, dev, rid, bus_res);
4974edef187SJohn Baldwin 	    }
4984edef187SJohn Baldwin     } else {
499a1eff92bSJohn Baldwin 	    /*
500a1eff92bSJohn Baldwin 	     * Require the bus number from _BBN to match the start of any
501a1eff92bSJohn Baldwin 	     * decoded range.
502a1eff92bSJohn Baldwin 	     */
503a1eff92bSJohn Baldwin 	    if (first_decoded_bus(sc, &start) == 0 && sc->ap_bus != start) {
504a1eff92bSJohn Baldwin 		    device_printf(dev,
505a1eff92bSJohn Baldwin 		"bus number %d does not match start of decoded range %ju\n",
506a1eff92bSJohn Baldwin 			sc->ap_bus, (uintmax_t)start);
507a1eff92bSJohn Baldwin 		    pcib_host_res_free(dev, &sc->ap_host_res);
508a1eff92bSJohn Baldwin 		    return (ENXIO);
509a1eff92bSJohn Baldwin 	    }
5104edef187SJohn Baldwin     }
5114edef187SJohn Baldwin #else
5129debb532SJohn Baldwin     /*
5139debb532SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
5149debb532SJohn Baldwin      * host-PCI bridges in order and that as a result our unit number
5159debb532SJohn Baldwin      * is actually our bus number.  There are several reasons this
5169debb532SJohn Baldwin      * might not be true.
5179debb532SJohn Baldwin      */
5189debb532SJohn Baldwin     if (busok == 0) {
5199debb532SJohn Baldwin 	sc->ap_bus = device_get_unit(dev);
5209debb532SJohn Baldwin 	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
521c1b1f787SMike Smith     }
5224edef187SJohn Baldwin #endif
52315e32d5dSMike Smith 
5240668724bSJohn Baldwin     /* If this is bus 0 on segment 0, note that it has been seen already. */
5250668724bSJohn Baldwin     if (sc->ap_segment == 0 && sc->ap_bus == 0)
5260668724bSJohn Baldwin 	    bus0_seen = 1;
52715e32d5dSMike Smith 
52867e7d085SJohn Baldwin     acpi_pcib_fetch_prt(dev, &sc->ap_prt);
52967e7d085SJohn Baldwin 
53067e7d085SJohn Baldwin     if (device_add_child(dev, "pci", -1) == NULL) {
53167e7d085SJohn Baldwin 	device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
532a1eff92bSJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
533a1eff92bSJohn Baldwin 	pcib_host_res_free(dev, &sc->ap_host_res);
534a1eff92bSJohn Baldwin #endif
53567e7d085SJohn Baldwin 	return (ENXIO);
53667e7d085SJohn Baldwin     }
53767e7d085SJohn Baldwin     return (bus_generic_attach(dev));
53815e32d5dSMike Smith }
53915e32d5dSMike Smith 
5404fa387b6SMike Smith /*
5414fa387b6SMike Smith  * Support for standard PCI bridge ivars.
5424fa387b6SMike Smith  */
54315e32d5dSMike Smith static int
54415e32d5dSMike Smith acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
54515e32d5dSMike Smith {
5462ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
54715e32d5dSMike Smith 
54815e32d5dSMike Smith     switch (which) {
54955aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
55024c93a6fSJohn Baldwin 	*result = sc->ap_segment;
55155aaf894SMarius Strobl 	return (0);
55215e32d5dSMike Smith     case PCIB_IVAR_BUS:
55315e32d5dSMike Smith 	*result = sc->ap_bus;
55415e32d5dSMike Smith 	return (0);
5552ccfc932SJohn Baldwin     case ACPI_IVAR_HANDLE:
5562ccfc932SJohn Baldwin 	*result = (uintptr_t)sc->ap_handle;
5572ccfc932SJohn Baldwin 	return (0);
558d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
559d4b9ff91SNate Lawson 	*result = (uintptr_t)sc->ap_flags;
560d4b9ff91SNate Lawson 	return (0);
56115e32d5dSMike Smith     }
56215e32d5dSMike Smith     return (ENOENT);
56315e32d5dSMike Smith }
56415e32d5dSMike Smith 
56515e32d5dSMike Smith static int
56615e32d5dSMike Smith acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
56715e32d5dSMike Smith {
5682ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
56915e32d5dSMike Smith 
57015e32d5dSMike Smith     switch (which) {
57155aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
57255aaf894SMarius Strobl 	return (EINVAL);
57315e32d5dSMike Smith     case PCIB_IVAR_BUS:
57415e32d5dSMike Smith 	sc->ap_bus = value;
57515e32d5dSMike Smith 	return (0);
576d4b9ff91SNate Lawson     case ACPI_IVAR_HANDLE:
577d4b9ff91SNate Lawson 	sc->ap_handle = (ACPI_HANDLE)value;
578d4b9ff91SNate Lawson 	return (0);
579d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
580d4b9ff91SNate Lawson 	sc->ap_flags = (int)value;
581d4b9ff91SNate Lawson 	return (0);
58215e32d5dSMike Smith     }
58315e32d5dSMike Smith     return (ENOENT);
58415e32d5dSMike Smith }
58515e32d5dSMike Smith 
586e3aa81b8SNate Lawson static uint32_t
5871496d4a9SWarner Losh acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
5881496d4a9SWarner Losh     u_int reg, int bytes)
58915e32d5dSMike Smith {
59015e32d5dSMike Smith     return (pci_cfgregread(bus, slot, func, reg, bytes));
59115e32d5dSMike Smith }
59215e32d5dSMike Smith 
59315e32d5dSMike Smith static void
5941496d4a9SWarner Losh acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
5951496d4a9SWarner Losh     u_int reg, uint32_t data, int bytes)
59615e32d5dSMike Smith {
59715e32d5dSMike Smith     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
59815e32d5dSMike Smith }
59915e32d5dSMike Smith 
60015e32d5dSMike Smith static int
6012ccfc932SJohn Baldwin acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
60215e32d5dSMike Smith {
6035e1ba6d4SJohn Baldwin     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
6044fa387b6SMike Smith 
6055e1ba6d4SJohn Baldwin     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
60615e32d5dSMike Smith }
607cd8b53edSWarner Losh 
6088964299aSJohn Baldwin static int
6098964299aSJohn Baldwin acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
6108964299aSJohn Baldwin     int *irqs)
6118964299aSJohn Baldwin {
6128964299aSJohn Baldwin 	device_t bus;
6138964299aSJohn Baldwin 
6148964299aSJohn Baldwin 	bus = device_get_parent(pcib);
6158964299aSJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
6168964299aSJohn Baldwin 	    irqs));
6178964299aSJohn Baldwin }
6188964299aSJohn Baldwin 
6198964299aSJohn Baldwin static int
620e706f7f0SJohn Baldwin acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
6218964299aSJohn Baldwin {
6228964299aSJohn Baldwin 	device_t bus;
6238964299aSJohn Baldwin 
6248964299aSJohn Baldwin 	bus = device_get_parent(pcib);
625e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
626e706f7f0SJohn Baldwin }
627e706f7f0SJohn Baldwin 
628e706f7f0SJohn Baldwin static int
629e706f7f0SJohn Baldwin acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
630e706f7f0SJohn Baldwin     uint32_t *data)
631e706f7f0SJohn Baldwin {
6320d95597cSJohn Baldwin 	struct acpi_hpcib_softc *sc;
6330d95597cSJohn Baldwin 	device_t bus, hostb;
6340d95597cSJohn Baldwin 	int error;
635e706f7f0SJohn Baldwin 
636e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
6370d95597cSJohn Baldwin 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
6380d95597cSJohn Baldwin 	if (error)
6390d95597cSJohn Baldwin 		return (error);
6400d95597cSJohn Baldwin 
641af0d1ee9SNeel Natu 	sc = device_get_softc(pcib);
6420d95597cSJohn Baldwin 	if (sc->ap_addr == -1)
6430d95597cSJohn Baldwin 		return (0);
6440d95597cSJohn Baldwin 	/* XXX: Assumes all bridges are on bus 0. */
6450d95597cSJohn Baldwin 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
6460d95597cSJohn Baldwin 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
6470d95597cSJohn Baldwin 	if (hostb != NULL)
6480d95597cSJohn Baldwin 		pci_ht_map_msi(hostb, *addr);
6490d95597cSJohn Baldwin 	return (0);
6508964299aSJohn Baldwin }
6518964299aSJohn Baldwin 
652cd8b53edSWarner Losh struct resource *
653cd8b53edSWarner Losh acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
6542dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
655cd8b53edSWarner Losh {
65634ff71eeSJohn Baldwin #ifdef NEW_PCIB
65734ff71eeSJohn Baldwin     struct acpi_hpcib_softc *sc;
6585d0d779bSJohn Baldwin     struct resource *res;
65934ff71eeSJohn Baldwin #endif
66038d7a61bSJohn Baldwin 
66138d7a61bSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
66238d7a61bSJohn Baldwin     start = hostb_alloc_start(type, start, end, count);
66338d7a61bSJohn Baldwin #endif
66434ff71eeSJohn Baldwin 
66534ff71eeSJohn Baldwin #ifdef NEW_PCIB
66634ff71eeSJohn Baldwin     sc = device_get_softc(dev);
6674edef187SJohn Baldwin #ifdef PCI_RES_BUS
6684edef187SJohn Baldwin     if (type == PCI_RES_BUS)
6694edef187SJohn Baldwin 	return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
6704edef187SJohn Baldwin 	    count, flags));
6714edef187SJohn Baldwin #endif
6725d0d779bSJohn Baldwin     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
6735d0d779bSJohn Baldwin 	count, flags);
674a74d6952SJohn Baldwin 
675a74d6952SJohn Baldwin     /*
676a74d6952SJohn Baldwin      * XXX: If this is a request for a specific range, assume it is
677a74d6952SJohn Baldwin      * correct and pass it up to the parent.  What we probably want to
678a74d6952SJohn Baldwin      * do long-term is explicitly trust any firmware-configured
679a74d6952SJohn Baldwin      * resources during the initial bus scan on boot and then disable
680a74d6952SJohn Baldwin      * this after that.
681a74d6952SJohn Baldwin      */
6825d0d779bSJohn Baldwin     if (res == NULL && start + count - 1 == end)
683a74d6952SJohn Baldwin 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
684a74d6952SJohn Baldwin 	    count, flags);
6855d0d779bSJohn Baldwin     return (res);
68634ff71eeSJohn Baldwin #else
687cd8b53edSWarner Losh     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
688cd8b53edSWarner Losh 	count, flags));
68934ff71eeSJohn Baldwin #endif
690cd8b53edSWarner Losh }
69134ff71eeSJohn Baldwin 
69234ff71eeSJohn Baldwin #ifdef NEW_PCIB
69334ff71eeSJohn Baldwin int
69434ff71eeSJohn Baldwin acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type,
6952dd1bdf1SJustin Hibbits     struct resource *r, rman_res_t start, rman_res_t end)
69634ff71eeSJohn Baldwin {
69734ff71eeSJohn Baldwin 	struct acpi_hpcib_softc *sc;
69834ff71eeSJohn Baldwin 
69934ff71eeSJohn Baldwin 	sc = device_get_softc(dev);
7004edef187SJohn Baldwin #ifdef PCI_RES_BUS
7014edef187SJohn Baldwin 	if (type == PCI_RES_BUS)
7024edef187SJohn Baldwin 		return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
7034edef187SJohn Baldwin 		    end));
7044edef187SJohn Baldwin #endif
70534ff71eeSJohn Baldwin 	return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start,
70634ff71eeSJohn Baldwin 	    end));
70734ff71eeSJohn Baldwin }
7084edef187SJohn Baldwin 
7094edef187SJohn Baldwin #ifdef PCI_RES_BUS
7104edef187SJohn Baldwin int
7114edef187SJohn Baldwin acpi_pcib_acpi_release_resource(device_t dev, device_t child, int type, int rid,
7124edef187SJohn Baldwin     struct resource *r)
7134edef187SJohn Baldwin {
7144edef187SJohn Baldwin 	struct acpi_hpcib_softc *sc;
7154edef187SJohn Baldwin 
7164edef187SJohn Baldwin 	sc = device_get_softc(dev);
7174edef187SJohn Baldwin 	if (type == PCI_RES_BUS)
7184edef187SJohn Baldwin 		return (pci_domain_release_bus(sc->ap_segment, child, rid, r));
7194edef187SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
7204edef187SJohn Baldwin }
7214edef187SJohn Baldwin #endif
72234ff71eeSJohn Baldwin #endif
723*28586889SWarner Losh 
724*28586889SWarner Losh static int
725*28586889SWarner Losh acpi_pcib_request_feature(device_t pcib, device_t dev, enum pci_feature feature)
726*28586889SWarner Losh {
727*28586889SWarner Losh 	uint32_t osc_ctl;
728*28586889SWarner Losh 	struct acpi_hpcib_softc *sc;
729*28586889SWarner Losh 
730*28586889SWarner Losh 	sc = device_get_softc(dev);
731*28586889SWarner Losh 
732*28586889SWarner Losh 	switch (feature) {
733*28586889SWarner Losh 	case PCI_FEATURE_HP:
734*28586889SWarner Losh 		osc_ctl = PCIM_OSC_CTL_PCIE_HP;
735*28586889SWarner Losh 		break;
736*28586889SWarner Losh 	case PCI_FEATURE_AER:
737*28586889SWarner Losh 		osc_ctl = PCIM_OSC_CTL_PCIE_AER;
738*28586889SWarner Losh 		break;
739*28586889SWarner Losh 	default:
740*28586889SWarner Losh 		return (EINVAL);
741*28586889SWarner Losh 	}
742*28586889SWarner Losh 
743*28586889SWarner Losh 	return (acpi_pcib_osc(sc, osc_ctl));
744*28586889SWarner Losh }
745