xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision 9dbf5b0e6876d8c93890754bcc9c748339de79c0)
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>
2915e32d5dSMike Smith #include "opt_acpi.h"
3082cb5c3bSJohn Baldwin #include "opt_pci.h"
3182cb5c3bSJohn Baldwin 
3215e32d5dSMike Smith #include <sys/param.h>
3315e32d5dSMike Smith #include <sys/bus.h>
344fa387b6SMike Smith #include <sys/kernel.h>
3534ff71eeSJohn Baldwin #include <sys/limits.h>
36e3aa81b8SNate Lawson #include <sys/malloc.h>
37fe12f24bSPoul-Henning Kamp #include <sys/module.h>
3883c41143SJohn Baldwin #include <sys/rman.h>
39fd492ee0SWarner Losh #include <sys/sysctl.h>
4015e32d5dSMike Smith 
41129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
42129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
43129d3046SJung-uk Kim 
4415e32d5dSMike Smith #include <dev/acpica/acpivar.h>
4515e32d5dSMike Smith 
4615e32d5dSMike Smith #include <machine/pci_cfgreg.h>
474edef187SJohn Baldwin #include <dev/pci/pcireg.h>
48cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
49cace7a2aSWarner Losh #include <dev/pci/pcib_private.h>
5015e32d5dSMike Smith #include "pcib_if.h"
5115e32d5dSMike Smith 
522ccfc932SJohn Baldwin #include <dev/acpica/acpi_pcibvar.h>
532ccfc932SJohn Baldwin 
54e3aa81b8SNate Lawson /* Hooks for the ACPI CA debugging infrastructure. */
55e71b6381SMike Smith #define _COMPONENT	ACPI_BUS
562ccfc932SJohn Baldwin ACPI_MODULE_NAME("PCI_ACPI")
570ae55423SMike Smith 
582ccfc932SJohn Baldwin struct acpi_hpcib_softc {
5915e32d5dSMike Smith     device_t		ap_dev;
6015e32d5dSMike Smith     ACPI_HANDLE		ap_handle;
616f4acaf4SJeff Roberson     bus_dma_tag_t	ap_dma_tag;
62d4b9ff91SNate Lawson     int			ap_flags;
6328586889SWarner Losh     uint32_t		ap_osc_ctl;
6415e32d5dSMike Smith 
650d95597cSJohn Baldwin     int			ap_segment;	/* PCI domain */
6615e32d5dSMike Smith     int			ap_bus;		/* bios-assigned bus number */
670d95597cSJohn Baldwin     int			ap_addr;	/* device/func of PCI-Host bridge */
684fa387b6SMike Smith 
694fa387b6SMike Smith     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
7034ff71eeSJohn Baldwin #ifdef NEW_PCIB
7134ff71eeSJohn Baldwin     struct pcib_host_resources ap_host_res;
7234ff71eeSJohn Baldwin #endif
7315e32d5dSMike Smith };
7415e32d5dSMike Smith 
752ccfc932SJohn Baldwin static int		acpi_pcib_acpi_probe(device_t bus);
762ccfc932SJohn Baldwin static int		acpi_pcib_acpi_attach(device_t bus);
77e3aa81b8SNate Lawson static int		acpi_pcib_read_ivar(device_t dev, device_t child,
78e3aa81b8SNate Lawson 			    int which, uintptr_t *result);
79e3aa81b8SNate Lawson static int		acpi_pcib_write_ivar(device_t dev, device_t child,
80e3aa81b8SNate Lawson 			    int which, uintptr_t value);
811496d4a9SWarner Losh static uint32_t		acpi_pcib_read_config(device_t dev, u_int bus,
821496d4a9SWarner Losh 			    u_int slot, u_int func, u_int reg, int bytes);
831496d4a9SWarner Losh static void		acpi_pcib_write_config(device_t dev, u_int bus,
841496d4a9SWarner Losh 			    u_int slot, u_int func, u_int reg, uint32_t data,
851496d4a9SWarner Losh 			    int bytes);
862ccfc932SJohn Baldwin static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
872ccfc932SJohn Baldwin 			    device_t dev, int pin);
888964299aSJohn Baldwin static int		acpi_pcib_alloc_msi(device_t pcib, device_t dev,
898964299aSJohn Baldwin 			    int count, int maxcount, int *irqs);
90e706f7f0SJohn Baldwin static int		acpi_pcib_map_msi(device_t pcib, device_t dev,
91e706f7f0SJohn Baldwin 			    int irq, uint64_t *addr, uint32_t *data);
928964299aSJohn Baldwin static int		acpi_pcib_alloc_msix(device_t pcib, device_t dev,
93e706f7f0SJohn Baldwin 			    int *irq);
94cd8b53edSWarner Losh static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
95cd8b53edSWarner Losh 			    device_t child, int type, int *rid,
962dd1bdf1SJustin Hibbits 			    rman_res_t start, rman_res_t end, rman_res_t count,
97cd8b53edSWarner Losh 			    u_int flags);
9834ff71eeSJohn Baldwin #ifdef NEW_PCIB
9934ff71eeSJohn Baldwin static int		acpi_pcib_acpi_adjust_resource(device_t dev,
100fef01f04SJohn Baldwin 			    device_t child, struct resource *r,
1012dd1bdf1SJustin Hibbits 			    rman_res_t start, rman_res_t end);
1024edef187SJohn Baldwin #ifdef PCI_RES_BUS
1034edef187SJohn Baldwin static int		acpi_pcib_acpi_release_resource(device_t dev,
104*9dbf5b0eSJohn Baldwin 			    device_t child, struct resource *r);
105c2d4fef6SJohn Baldwin static int		acpi_pcib_acpi_activate_resource(device_t dev,
1062baed46eSJohn Baldwin 			    device_t child, struct resource *r);
107c2d4fef6SJohn Baldwin static int		acpi_pcib_acpi_deactivate_resource(device_t dev,
1082baed46eSJohn Baldwin 			    device_t child, struct resource *r);
1094edef187SJohn Baldwin #endif
11034ff71eeSJohn Baldwin #endif
11128586889SWarner Losh static int		acpi_pcib_request_feature(device_t pcib, device_t dev,
11228586889SWarner Losh 			    enum pci_feature feature);
1136f4acaf4SJeff Roberson static bus_dma_tag_t	acpi_pcib_get_dma_tag(device_t bus, device_t child);
11415e32d5dSMike Smith 
1152ccfc932SJohn Baldwin static device_method_t acpi_pcib_acpi_methods[] = {
11615e32d5dSMike Smith     /* Device interface */
1172ccfc932SJohn Baldwin     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
1182ccfc932SJohn Baldwin     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
11915e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
12015e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
1217d23a9b3SJohn Baldwin     DEVMETHOD(device_resume,		bus_generic_resume),
12215e32d5dSMike Smith 
12315e32d5dSMike Smith     /* Bus interface */
12415e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
12515e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
126cd8b53edSWarner Losh     DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
12734ff71eeSJohn Baldwin #ifdef NEW_PCIB
12834ff71eeSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	acpi_pcib_acpi_adjust_resource),
12934ff71eeSJohn Baldwin #else
130d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
13134ff71eeSJohn Baldwin #endif
1324edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
1334edef187SJohn Baldwin     DEVMETHOD(bus_release_resource,	acpi_pcib_acpi_release_resource),
134c2d4fef6SJohn Baldwin     DEVMETHOD(bus_activate_resource,	acpi_pcib_acpi_activate_resource),
135c2d4fef6SJohn Baldwin     DEVMETHOD(bus_deactivate_resource,	acpi_pcib_acpi_deactivate_resource),
1364edef187SJohn Baldwin #else
13715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
13815e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
13915e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
140c2d4fef6SJohn Baldwin #endif
14115e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14215e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1438d791e5aSJohn Baldwin     DEVMETHOD(bus_get_cpus,		acpi_pcib_get_cpus),
1446f4acaf4SJeff Roberson     DEVMETHOD(bus_get_dma_tag,		acpi_pcib_get_dma_tag),
14515e32d5dSMike Smith 
14615e32d5dSMike Smith     /* pcib interface */
1472ccfc932SJohn Baldwin     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
14815e32d5dSMike Smith     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
14915e32d5dSMike Smith     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
1502ccfc932SJohn Baldwin     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
1518964299aSJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
1529bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1538964299aSJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
1549bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
155e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
15662508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
15728586889SWarner Losh     DEVMETHOD(pcib_request_feature,	acpi_pcib_request_feature),
15815e32d5dSMike Smith 
1594b7ec270SMarius Strobl     DEVMETHOD_END
16015e32d5dSMike Smith };
16115e32d5dSMike Smith 
16204dda605SJohn Baldwin DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
16304dda605SJohn Baldwin     sizeof(struct acpi_hpcib_softc));
164916a5d8aSJohn Baldwin DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, 0, 0);
16564278df5SNate Lawson MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
16615e32d5dSMike Smith 
16715e32d5dSMike Smith static int
1682ccfc932SJohn Baldwin acpi_pcib_acpi_probe(device_t dev)
16915e32d5dSMike Smith {
17092488a57SJung-uk Kim     ACPI_DEVICE_INFO	*devinfo;
17192488a57SJung-uk Kim     ACPI_HANDLE		h;
17292488a57SJung-uk Kim     int			root;
17315e32d5dSMike Smith 
17492488a57SJung-uk Kim     if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
17592488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
17692488a57SJung-uk Kim 	return (ENXIO);
17792488a57SJung-uk Kim     root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
17892488a57SJung-uk Kim     AcpiOsFree(devinfo);
17992488a57SJung-uk Kim     if (!root || pci_cfgregopen() == 0)
1805fcc8a58SNate Lawson 	return (ENXIO);
18115e32d5dSMike Smith 
1822ccfc932SJohn Baldwin     device_set_desc(dev, "ACPI Host-PCI bridge");
18315e32d5dSMike Smith     return (0);
18415e32d5dSMike Smith }
18515e32d5dSMike Smith 
18634ff71eeSJohn Baldwin #ifdef NEW_PCIB
18734ff71eeSJohn Baldwin static ACPI_STATUS
18834ff71eeSJohn Baldwin acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
18934ff71eeSJohn Baldwin {
19034ff71eeSJohn Baldwin 	struct acpi_hpcib_softc *sc;
19134ff71eeSJohn Baldwin 	UINT64 length, min, max;
19234ff71eeSJohn Baldwin 	u_int flags;
19334ff71eeSJohn Baldwin 	int error, type;
19434ff71eeSJohn Baldwin 
19534ff71eeSJohn Baldwin 	sc = context;
19634ff71eeSJohn Baldwin 	switch (res->Type) {
19734ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
19834ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
19934ff71eeSJohn Baldwin 		panic("host bridge has depenedent resources");
20034ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS16:
20134ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS32:
20234ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS64:
20334ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
20434ff71eeSJohn Baldwin 		if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
20534ff71eeSJohn Baldwin 			break;
20634ff71eeSJohn Baldwin 		switch (res->Type) {
20734ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS16:
2087cf3e94aSJung-uk Kim 			min = res->Data.Address16.Address.Minimum;
2097cf3e94aSJung-uk Kim 			max = res->Data.Address16.Address.Maximum;
2107cf3e94aSJung-uk Kim 			length = res->Data.Address16.Address.AddressLength;
21134ff71eeSJohn Baldwin 			break;
21234ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS32:
2137cf3e94aSJung-uk Kim 			min = res->Data.Address32.Address.Minimum;
2147cf3e94aSJung-uk Kim 			max = res->Data.Address32.Address.Maximum;
2157cf3e94aSJung-uk Kim 			length = res->Data.Address32.Address.AddressLength;
21634ff71eeSJohn Baldwin 			break;
21734ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS64:
2187cf3e94aSJung-uk Kim 			min = res->Data.Address64.Address.Minimum;
2197cf3e94aSJung-uk Kim 			max = res->Data.Address64.Address.Maximum;
2207cf3e94aSJung-uk Kim 			length = res->Data.Address64.Address.AddressLength;
22134ff71eeSJohn Baldwin 			break;
22234ff71eeSJohn Baldwin 		default:
22334ff71eeSJohn Baldwin 			KASSERT(res->Type ==
22434ff71eeSJohn Baldwin 			    ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
22534ff71eeSJohn Baldwin 			    ("should never happen"));
2267cf3e94aSJung-uk Kim 			min = res->Data.ExtAddress64.Address.Minimum;
2277cf3e94aSJung-uk Kim 			max = res->Data.ExtAddress64.Address.Maximum;
2287cf3e94aSJung-uk Kim 			length = res->Data.ExtAddress64.Address.AddressLength;
22934ff71eeSJohn Baldwin 			break;
23034ff71eeSJohn Baldwin 		}
231e9f91b2bSJohn Baldwin 		if (length == 0)
232e9f91b2bSJohn Baldwin 			break;
233e9f91b2bSJohn Baldwin 		if (min + length - 1 != max &&
234e9f91b2bSJohn Baldwin 		    (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
235e9f91b2bSJohn Baldwin 		    res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
23634ff71eeSJohn Baldwin 			break;
23734ff71eeSJohn Baldwin 		flags = 0;
23834ff71eeSJohn Baldwin 		switch (res->Data.Address.ResourceType) {
23934ff71eeSJohn Baldwin 		case ACPI_MEMORY_RANGE:
24034ff71eeSJohn Baldwin 			type = SYS_RES_MEMORY;
24134ff71eeSJohn Baldwin 			if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
24234ff71eeSJohn Baldwin 				if (res->Data.Address.Info.Mem.Caching ==
24334ff71eeSJohn Baldwin 				    ACPI_PREFETCHABLE_MEMORY)
24434ff71eeSJohn Baldwin 					flags |= RF_PREFETCHABLE;
24534ff71eeSJohn Baldwin 			} else {
24634ff71eeSJohn Baldwin 				/*
24734ff71eeSJohn Baldwin 				 * XXX: Parse prefetch flag out of
24834ff71eeSJohn Baldwin 				 * TypeSpecific.
24934ff71eeSJohn Baldwin 				 */
25034ff71eeSJohn Baldwin 			}
25134ff71eeSJohn Baldwin 			break;
25234ff71eeSJohn Baldwin 		case ACPI_IO_RANGE:
25334ff71eeSJohn Baldwin 			type = SYS_RES_IOPORT;
25434ff71eeSJohn Baldwin 			break;
25534ff71eeSJohn Baldwin #ifdef PCI_RES_BUS
25634ff71eeSJohn Baldwin 		case ACPI_BUS_NUMBER_RANGE:
25734ff71eeSJohn Baldwin 			type = PCI_RES_BUS;
25834ff71eeSJohn Baldwin 			break;
25934ff71eeSJohn Baldwin #endif
26034ff71eeSJohn Baldwin 		default:
26134ff71eeSJohn Baldwin 			return (AE_OK);
26234ff71eeSJohn Baldwin 		}
26334ff71eeSJohn Baldwin 
26434ff71eeSJohn Baldwin 		if (min + length - 1 != max)
26534ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
26634ff71eeSJohn Baldwin 			    "Length mismatch for %d range: %jx vs %jx\n", type,
2670c10b85aSJung-uk Kim 			    (uintmax_t)(max - min + 1), (uintmax_t)length);
26834ff71eeSJohn Baldwin #ifdef __i386__
26934ff71eeSJohn Baldwin 		if (min > ULONG_MAX) {
27034ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
27134ff71eeSJohn Baldwin 			    "Ignoring %d range above 4GB (%#jx-%#jx)\n",
27234ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max);
27334ff71eeSJohn Baldwin 			break;
27434ff71eeSJohn Baldwin 		}
27534ff71eeSJohn Baldwin 		if (max > ULONG_MAX) {
27634ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
27734ff71eeSJohn Baldwin        		    "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
27834ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max);
27934ff71eeSJohn Baldwin 			max = ULONG_MAX;
28034ff71eeSJohn Baldwin 		}
28134ff71eeSJohn Baldwin #endif
28234ff71eeSJohn Baldwin 		error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
28334ff71eeSJohn Baldwin 		    flags);
28434ff71eeSJohn Baldwin 		if (error)
28534ff71eeSJohn Baldwin 			panic("Failed to manage %d range (%#jx-%#jx): %d",
28634ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max, error);
28734ff71eeSJohn Baldwin 		break;
28834ff71eeSJohn Baldwin 	default:
28934ff71eeSJohn Baldwin 		break;
29034ff71eeSJohn Baldwin 	}
29134ff71eeSJohn Baldwin 	return (AE_OK);
29234ff71eeSJohn Baldwin }
29334ff71eeSJohn Baldwin #endif
29434ff71eeSJohn Baldwin 
2954edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
296f6c2774fSJohn Baldwin static bool
297f6c2774fSJohn Baldwin get_decoded_bus_range(struct acpi_hpcib_softc *sc, rman_res_t *startp,
29822a6678bSJohn Baldwin     rman_res_t *endp)
2994edef187SJohn Baldwin {
3004edef187SJohn Baldwin 	struct resource_list_entry *rle;
3014edef187SJohn Baldwin 
3024edef187SJohn Baldwin 	rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
3034edef187SJohn Baldwin 	if (rle == NULL)
304f6c2774fSJohn Baldwin 		return (false);
3054edef187SJohn Baldwin 	*startp = rle->start;
30622a6678bSJohn Baldwin 	*endp = rle->end;
307f6c2774fSJohn Baldwin 	return (true);
3084edef187SJohn Baldwin }
3094edef187SJohn Baldwin #endif
3104edef187SJohn Baldwin 
31128586889SWarner Losh static int
31228586889SWarner Losh acpi_pcib_osc(struct acpi_hpcib_softc *sc, uint32_t osc_ctl)
313508c21b6SJohn Baldwin {
314508c21b6SJohn Baldwin 	ACPI_STATUS status;
315508c21b6SJohn Baldwin 	uint32_t cap_set[3];
316508c21b6SJohn Baldwin 
317508c21b6SJohn Baldwin 	static uint8_t pci_host_bridge_uuid[ACPI_UUID_LENGTH] = {
318508c21b6SJohn Baldwin 		0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40,
319508c21b6SJohn Baldwin 		0x96, 0x57, 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66
320508c21b6SJohn Baldwin 	};
321508c21b6SJohn Baldwin 
3221ffd07bdSJohn Baldwin 	/*
3231ffd07bdSJohn Baldwin 	 * Don't invoke _OSC if a control is already granted.
3241ffd07bdSJohn Baldwin 	 * However, always invoke _OSC during attach when 0 is passed.
3251ffd07bdSJohn Baldwin 	 */
3261ffd07bdSJohn Baldwin 	if (osc_ctl != 0 && (sc->ap_osc_ctl & osc_ctl) == osc_ctl)
3271ffd07bdSJohn Baldwin 		return (0);
328c21a6664SWarner Losh 
3294e38d474SAlexander Motin 	/* Support Field: Extended PCI Config Space, PCI Segment Groups, MSI */
330c21a6664SWarner Losh 	cap_set[PCI_OSC_SUPPORT] = PCIM_OSC_SUPPORT_EXT_PCI_CONF |
3314e38d474SAlexander Motin 	    PCIM_OSC_SUPPORT_SEG_GROUP | PCIM_OSC_SUPPORT_MSI;
3324cee4598SAlexander Motin 	/* Active State Power Management, Clock Power Management Capability */
3334cee4598SAlexander Motin 	if (pci_enable_aspm)
3344cee4598SAlexander Motin 		cap_set[PCI_OSC_SUPPORT] |= PCIM_OSC_SUPPORT_ASPM |
3354cee4598SAlexander Motin 		    PCIM_OSC_SUPPORT_CPMC;
336508c21b6SJohn Baldwin 
337508c21b6SJohn Baldwin 	/* Control Field */
3381ffd07bdSJohn Baldwin 	cap_set[PCI_OSC_CTL] = sc->ap_osc_ctl | osc_ctl;
33982cb5c3bSJohn Baldwin 
340508c21b6SJohn Baldwin 	status = acpi_EvaluateOSC(sc->ap_handle, pci_host_bridge_uuid, 1,
3414c26ac69SJohn Baldwin 	    nitems(cap_set), cap_set, cap_set, false);
342508c21b6SJohn Baldwin 	if (ACPI_FAILURE(status)) {
3431ffd07bdSJohn Baldwin 		if (status == AE_NOT_FOUND) {
3441ffd07bdSJohn Baldwin 			sc->ap_osc_ctl |= osc_ctl;
34528586889SWarner Losh 			return (0);
3461ffd07bdSJohn Baldwin 		}
347508c21b6SJohn Baldwin 		device_printf(sc->ap_dev, "_OSC failed: %s\n",
348508c21b6SJohn Baldwin 		    AcpiFormatException(status));
34928586889SWarner Losh 		return (EIO);
350508c21b6SJohn Baldwin 	}
351508c21b6SJohn Baldwin 
3521ffd07bdSJohn Baldwin 	/*
3531ffd07bdSJohn Baldwin 	 * _OSC may return an error in the status word, but will
3541ffd07bdSJohn Baldwin 	 * update the control mask always.  _OSC should not revoke
3551ffd07bdSJohn Baldwin 	 * previously-granted controls.
3561ffd07bdSJohn Baldwin 	 */
3571ffd07bdSJohn Baldwin 	if ((cap_set[PCI_OSC_CTL] & sc->ap_osc_ctl) != sc->ap_osc_ctl)
3581ffd07bdSJohn Baldwin 		device_printf(sc->ap_dev, "_OSC revoked %#x\n",
3591ffd07bdSJohn Baldwin 		    (cap_set[PCI_OSC_CTL] & sc->ap_osc_ctl) ^ sc->ap_osc_ctl);
36028586889SWarner Losh 	sc->ap_osc_ctl = cap_set[PCI_OSC_CTL];
3611ffd07bdSJohn Baldwin 	if ((sc->ap_osc_ctl & osc_ctl) != osc_ctl)
36228586889SWarner Losh 		return (EIO);
36328586889SWarner Losh 
36428586889SWarner Losh 	return (0);
365508c21b6SJohn Baldwin }
366508c21b6SJohn Baldwin 
36715e32d5dSMike Smith static int
3682ccfc932SJohn Baldwin acpi_pcib_acpi_attach(device_t dev)
36915e32d5dSMike Smith {
3702ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc;
37115e32d5dSMike Smith     ACPI_STATUS			status;
3720668724bSJohn Baldwin     static int bus0_seen = 0;
3730d95597cSJohn Baldwin     u_int slot, func, busok;
3744edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
3754edef187SJohn Baldwin     struct resource *bus_res;
37622a6678bSJohn Baldwin     rman_res_t end, start;
3774edef187SJohn Baldwin     int rid;
3784edef187SJohn Baldwin #endif
3796f4acaf4SJeff Roberson     int error, domain;
3809debb532SJohn Baldwin     uint8_t busno;
3810ae55423SMike Smith 
382b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
38315e32d5dSMike Smith 
38415e32d5dSMike Smith     sc = device_get_softc(dev);
38515e32d5dSMike Smith     sc->ap_dev = dev;
38615e32d5dSMike Smith     sc->ap_handle = acpi_get_handle(dev);
38715e32d5dSMike Smith 
38815e32d5dSMike Smith     /*
3898f4c2e52SJohn Baldwin      * Don't attach if we're not really there.
3908f4c2e52SJohn Baldwin      */
3918f4c2e52SJohn Baldwin     if (!acpi_DeviceIsPresent(dev))
3928f4c2e52SJohn Baldwin 	return (ENXIO);
3938f4c2e52SJohn Baldwin 
39428586889SWarner Losh     acpi_pcib_osc(sc, 0);
395508c21b6SJohn Baldwin 
3968f4c2e52SJohn Baldwin     /*
3970d95597cSJohn Baldwin      * Get our segment number by evaluating _SEG.
3980668724bSJohn Baldwin      * It's OK for this to not exist.
3990668724bSJohn Baldwin      */
4000668724bSJohn Baldwin     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
4010668724bSJohn Baldwin     if (ACPI_FAILURE(status)) {
4020668724bSJohn Baldwin 	if (status != AE_NOT_FOUND) {
4030668724bSJohn Baldwin 	    device_printf(dev, "could not evaluate _SEG - %s\n",
4040668724bSJohn Baldwin 		AcpiFormatException(status));
4050668724bSJohn Baldwin 	    return_VALUE (ENXIO);
4060668724bSJohn Baldwin 	}
4070668724bSJohn Baldwin 	/* If it's not found, assume 0. */
4080668724bSJohn Baldwin 	sc->ap_segment = 0;
4090668724bSJohn Baldwin     }
4100668724bSJohn Baldwin 
4110d95597cSJohn Baldwin     /*
4120d95597cSJohn Baldwin      * Get the address (device and function) of the associated
4130d95597cSJohn Baldwin      * PCI-Host bridge device from _ADR.  Assume we don't have one if
4140d95597cSJohn Baldwin      * it doesn't exist.
4150d95597cSJohn Baldwin      */
4160d95597cSJohn Baldwin     status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
4170d95597cSJohn Baldwin     if (ACPI_FAILURE(status)) {
418109abf74SYuri 	if (status != AE_NOT_FOUND)
4190d95597cSJohn Baldwin 	    device_printf(dev, "could not evaluate _ADR - %s\n",
4200d95597cSJohn Baldwin 		AcpiFormatException(status));
4210d95597cSJohn Baldwin 	sc->ap_addr = -1;
4220d95597cSJohn Baldwin     }
4230d95597cSJohn Baldwin 
42434ff71eeSJohn Baldwin #ifdef NEW_PCIB
42534ff71eeSJohn Baldwin     /*
42634ff71eeSJohn Baldwin      * Determine which address ranges this bridge decodes and setup
42734ff71eeSJohn Baldwin      * resource managers for those ranges.
42834ff71eeSJohn Baldwin      */
42934ff71eeSJohn Baldwin     if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
43034ff71eeSJohn Baldwin 	    panic("failed to init hostb resources");
43134ff71eeSJohn Baldwin     if (!acpi_disabled("hostres")) {
43234ff71eeSJohn Baldwin 	status = AcpiWalkResources(sc->ap_handle, "_CRS",
43334ff71eeSJohn Baldwin 	    acpi_pcib_producer_handler, sc);
43434ff71eeSJohn Baldwin 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
43534ff71eeSJohn Baldwin 	    device_printf(sc->ap_dev, "failed to parse resources: %s\n",
43634ff71eeSJohn Baldwin 		AcpiFormatException(status));
43734ff71eeSJohn Baldwin     }
43834ff71eeSJohn Baldwin #endif
43934ff71eeSJohn Baldwin 
4400668724bSJohn Baldwin     /*
4419debb532SJohn Baldwin      * Get our base bus number by evaluating _BBN.
442c1b1f787SMike Smith      * If this doesn't work, we assume we're bus number 0.
44315e32d5dSMike Smith      *
44415e32d5dSMike Smith      * XXX note that it may also not exist in the case where we are
44515e32d5dSMike Smith      *     meant to use a private configuration space mechanism for this bus,
44615e32d5dSMike Smith      *     so we should dig out our resources and check to see if we have
44715e32d5dSMike Smith      *     anything like that.  How do we do this?
448042283a6SMike Smith      * XXX If we have the requisite information, and if we don't think the
449042283a6SMike Smith      *     default PCI configuration space handlers can deal with this bus,
450042283a6SMike Smith      *     we should attach our own handler.
451042283a6SMike Smith      * XXX invoke _REG on this for the PCI config space address space?
4529debb532SJohn Baldwin      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
4539debb532SJohn Baldwin      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
4540668724bSJohn Baldwin      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
4559debb532SJohn Baldwin      *     bus number from the configuration registers at address _ADR.
4560668724bSJohn Baldwin      *     We only do this for domain/segment 0 in the hopes that this is
4570668724bSJohn Baldwin      *     only needed for old single-domain machines.
45815e32d5dSMike Smith      */
459c310653eSNate Lawson     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
4609debb532SJohn Baldwin     if (ACPI_FAILURE(status)) {
46115e32d5dSMike Smith 	if (status != AE_NOT_FOUND) {
4629debb532SJohn Baldwin 	    device_printf(dev, "could not evaluate _BBN - %s\n",
4639debb532SJohn Baldwin 		AcpiFormatException(status));
4648f4c2e52SJohn Baldwin 	    return (ENXIO);
465c1b1f787SMike Smith 	} else {
466e3aa81b8SNate Lawson 	    /* If it's not found, assume 0. */
46715e32d5dSMike Smith 	    sc->ap_bus = 0;
46815e32d5dSMike Smith 	}
4698745ec57SJohn Baldwin     }
4709debb532SJohn Baldwin 
4719debb532SJohn Baldwin     /*
4720668724bSJohn Baldwin      * If this is segment 0, the bus is zero, and PCI bus 0 already
4730668724bSJohn Baldwin      * exists, read the bus number via PCI config space.
4749debb532SJohn Baldwin      */
4759debb532SJohn Baldwin     busok = 1;
4760668724bSJohn Baldwin     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
4778745ec57SJohn Baldwin 	busok = 0;
4780d95597cSJohn Baldwin 	if (sc->ap_addr != -1) {
4799debb532SJohn Baldwin 	    /* XXX: We assume bus 0. */
4800d95597cSJohn Baldwin 	    slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
4810d95597cSJohn Baldwin 	    func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
4829debb532SJohn Baldwin 	    if (bootverbose)
4839debb532SJohn Baldwin 		device_printf(dev, "reading config registers from 0:%d:%d\n",
4849debb532SJohn Baldwin 		    slot, func);
4858745ec57SJohn Baldwin 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
486e3aa81b8SNate Lawson 		device_printf(dev, "couldn't read bus number from cfg space\n");
4878745ec57SJohn Baldwin 	    else {
4889debb532SJohn Baldwin 		sc->ap_bus = busno;
4898745ec57SJohn Baldwin 		busok = 1;
4909debb532SJohn Baldwin 	    }
4919debb532SJohn Baldwin 	}
4929debb532SJohn Baldwin     }
4939debb532SJohn Baldwin 
4944edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
4954edef187SJohn Baldwin     /*
4964edef187SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
4974edef187SJohn Baldwin      * Host-PCI bridges in order and that as a result the next free
4984edef187SJohn Baldwin      * bus number is our bus number.
4994edef187SJohn Baldwin      */
5004edef187SJohn Baldwin     if (busok == 0) {
5014edef187SJohn Baldwin 	    /*
5024edef187SJohn Baldwin 	     * If we have a region of bus numbers, use the first
5034edef187SJohn Baldwin 	     * number for our bus.
5044edef187SJohn Baldwin 	     */
505f6c2774fSJohn Baldwin 	    if (get_decoded_bus_range(sc, &start, &end))
5064edef187SJohn Baldwin 		    sc->ap_bus = start;
5074edef187SJohn Baldwin 	    else {
5084edef187SJohn Baldwin 		    rid = 0;
5094edef187SJohn Baldwin 		    bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
5104edef187SJohn Baldwin 			PCI_BUSMAX, 1, 0);
5114edef187SJohn Baldwin 		    if (bus_res == NULL) {
5124edef187SJohn Baldwin 			    device_printf(dev,
5134edef187SJohn Baldwin 				"could not allocate bus number\n");
5144edef187SJohn Baldwin 			    pcib_host_res_free(dev, &sc->ap_host_res);
5154edef187SJohn Baldwin 			    return (ENXIO);
5164edef187SJohn Baldwin 		    }
5174edef187SJohn Baldwin 		    sc->ap_bus = rman_get_start(bus_res);
518*9dbf5b0eSJohn Baldwin 		    pci_domain_release_bus(sc->ap_segment, dev, bus_res);
5194edef187SJohn Baldwin 	    }
5204edef187SJohn Baldwin     } else {
521a1eff92bSJohn Baldwin 	    /*
52222a6678bSJohn Baldwin 	     * If there is a decoded bus range, assume the bus number is
52322a6678bSJohn Baldwin 	     * the first value in the range.  Warn if _BBN doesn't match.
524a1eff92bSJohn Baldwin 	     */
525f6c2774fSJohn Baldwin 	    if (get_decoded_bus_range(sc, &start, &end)) {
52622a6678bSJohn Baldwin 		    if (sc->ap_bus != start) {
527a1eff92bSJohn Baldwin 			    device_printf(dev,
52822a6678bSJohn Baldwin 				"WARNING: BIOS configured bus number (%d) is "
52922a6678bSJohn Baldwin 				"not within decoded bus number range "
53022a6678bSJohn Baldwin 				"(%ju - %ju).\n",
53122a6678bSJohn Baldwin 				sc->ap_bus, (uintmax_t)start, (uintmax_t)end);
53222a6678bSJohn Baldwin 			    device_printf(dev,
53322a6678bSJohn Baldwin 				"Using range start (%ju) as bus number.\n",
53422a6678bSJohn Baldwin 				(uintmax_t)start);
53522a6678bSJohn Baldwin 			    sc->ap_bus = start;
53622a6678bSJohn Baldwin 		    }
537a1eff92bSJohn Baldwin 	    }
5384edef187SJohn Baldwin     }
5394edef187SJohn Baldwin #else
5409debb532SJohn Baldwin     /*
5419debb532SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
5429debb532SJohn Baldwin      * host-PCI bridges in order and that as a result our unit number
5439debb532SJohn Baldwin      * is actually our bus number.  There are several reasons this
5449debb532SJohn Baldwin      * might not be true.
5459debb532SJohn Baldwin      */
5469debb532SJohn Baldwin     if (busok == 0) {
5479debb532SJohn Baldwin 	sc->ap_bus = device_get_unit(dev);
5489debb532SJohn Baldwin 	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
549c1b1f787SMike Smith     }
5504edef187SJohn Baldwin #endif
55115e32d5dSMike Smith 
5520668724bSJohn Baldwin     /* If this is bus 0 on segment 0, note that it has been seen already. */
5530668724bSJohn Baldwin     if (sc->ap_segment == 0 && sc->ap_bus == 0)
5540668724bSJohn Baldwin 	    bus0_seen = 1;
55515e32d5dSMike Smith 
55667e7d085SJohn Baldwin     acpi_pcib_fetch_prt(dev, &sc->ap_prt);
55767e7d085SJohn Baldwin 
5586f4acaf4SJeff Roberson     error = bus_dma_tag_create(bus_get_dma_tag(dev), 1,
5596f4acaf4SJeff Roberson 	0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
5606f4acaf4SJeff Roberson 	NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
5616f4acaf4SJeff Roberson 	BUS_SPACE_MAXSIZE, 0, NULL, NULL, &sc->ap_dma_tag);
5626f4acaf4SJeff Roberson     if (error != 0)
5636f4acaf4SJeff Roberson 	goto errout;
5646f4acaf4SJeff Roberson     error = bus_get_domain(dev, &domain);
5656f4acaf4SJeff Roberson     if (error == 0)
5666f4acaf4SJeff Roberson 	error = bus_dma_tag_set_domain(sc->ap_dma_tag, domain);
5676f4acaf4SJeff Roberson     /* Don't fail to attach if the domain can't be queried or set. */
5686f4acaf4SJeff Roberson     error = 0;
5696f4acaf4SJeff Roberson 
570554e6778SSepherosa Ziehau     bus_generic_probe(dev);
57167e7d085SJohn Baldwin     if (device_add_child(dev, "pci", -1) == NULL) {
5726f4acaf4SJeff Roberson 	bus_dma_tag_destroy(sc->ap_dma_tag);
5736f4acaf4SJeff Roberson 	sc->ap_dma_tag = NULL;
5746f4acaf4SJeff Roberson 	error = ENXIO;
5756f4acaf4SJeff Roberson 	goto errout;
5766f4acaf4SJeff Roberson     }
5776f4acaf4SJeff Roberson     return (bus_generic_attach(dev));
5786f4acaf4SJeff Roberson 
5796f4acaf4SJeff Roberson errout:
58067e7d085SJohn Baldwin     device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
581a1eff92bSJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
582a1eff92bSJohn Baldwin     pcib_host_res_free(dev, &sc->ap_host_res);
583a1eff92bSJohn Baldwin #endif
5846f4acaf4SJeff Roberson     return (error);
58515e32d5dSMike Smith }
58615e32d5dSMike Smith 
5874fa387b6SMike Smith /*
5884fa387b6SMike Smith  * Support for standard PCI bridge ivars.
5894fa387b6SMike Smith  */
59015e32d5dSMike Smith static int
59115e32d5dSMike Smith acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
59215e32d5dSMike Smith {
5932ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
59415e32d5dSMike Smith 
59515e32d5dSMike Smith     switch (which) {
59655aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
59724c93a6fSJohn Baldwin 	*result = sc->ap_segment;
59855aaf894SMarius Strobl 	return (0);
59915e32d5dSMike Smith     case PCIB_IVAR_BUS:
60015e32d5dSMike Smith 	*result = sc->ap_bus;
60115e32d5dSMike Smith 	return (0);
6022ccfc932SJohn Baldwin     case ACPI_IVAR_HANDLE:
6032ccfc932SJohn Baldwin 	*result = (uintptr_t)sc->ap_handle;
6042ccfc932SJohn Baldwin 	return (0);
605d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
606d4b9ff91SNate Lawson 	*result = (uintptr_t)sc->ap_flags;
607d4b9ff91SNate Lawson 	return (0);
60815e32d5dSMike Smith     }
60915e32d5dSMike Smith     return (ENOENT);
61015e32d5dSMike Smith }
61115e32d5dSMike Smith 
61215e32d5dSMike Smith static int
61315e32d5dSMike Smith acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
61415e32d5dSMike Smith {
6152ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
61615e32d5dSMike Smith 
61715e32d5dSMike Smith     switch (which) {
61855aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
61955aaf894SMarius Strobl 	return (EINVAL);
62015e32d5dSMike Smith     case PCIB_IVAR_BUS:
62115e32d5dSMike Smith 	sc->ap_bus = value;
62215e32d5dSMike Smith 	return (0);
623d4b9ff91SNate Lawson     case ACPI_IVAR_HANDLE:
624d4b9ff91SNate Lawson 	sc->ap_handle = (ACPI_HANDLE)value;
625d4b9ff91SNate Lawson 	return (0);
626d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
627d4b9ff91SNate Lawson 	sc->ap_flags = (int)value;
628d4b9ff91SNate Lawson 	return (0);
62915e32d5dSMike Smith     }
63015e32d5dSMike Smith     return (ENOENT);
63115e32d5dSMike Smith }
63215e32d5dSMike Smith 
633e3aa81b8SNate Lawson static uint32_t
6341496d4a9SWarner Losh acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
6351496d4a9SWarner Losh     u_int reg, int bytes)
63615e32d5dSMike Smith {
6371587a9dbSJohn Baldwin     struct acpi_hpcib_softc *sc = device_get_softc(dev);
6381587a9dbSJohn Baldwin 
6391587a9dbSJohn Baldwin     return (pci_cfgregread(sc->ap_segment, bus, slot, func, reg, bytes));
64015e32d5dSMike Smith }
64115e32d5dSMike Smith 
64215e32d5dSMike Smith static void
6431496d4a9SWarner Losh acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
6441496d4a9SWarner Losh     u_int reg, uint32_t data, int bytes)
64515e32d5dSMike Smith {
6461587a9dbSJohn Baldwin     struct acpi_hpcib_softc *sc = device_get_softc(dev);
6471587a9dbSJohn Baldwin 
6481587a9dbSJohn Baldwin     pci_cfgregwrite(sc->ap_segment, bus, slot, func, reg, data, bytes);
64915e32d5dSMike Smith }
65015e32d5dSMike Smith 
65115e32d5dSMike Smith static int
6522ccfc932SJohn Baldwin acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
65315e32d5dSMike Smith {
6545e1ba6d4SJohn Baldwin     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
6554fa387b6SMike Smith 
6565e1ba6d4SJohn Baldwin     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
65715e32d5dSMike Smith }
658cd8b53edSWarner Losh 
6598964299aSJohn Baldwin static int
6608964299aSJohn Baldwin acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
6618964299aSJohn Baldwin     int *irqs)
6628964299aSJohn Baldwin {
6638964299aSJohn Baldwin 	device_t bus;
6648964299aSJohn Baldwin 
6658964299aSJohn Baldwin 	bus = device_get_parent(pcib);
6668964299aSJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
6678964299aSJohn Baldwin 	    irqs));
6688964299aSJohn Baldwin }
6698964299aSJohn Baldwin 
6708964299aSJohn Baldwin static int
671e706f7f0SJohn Baldwin acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
6728964299aSJohn Baldwin {
6738964299aSJohn Baldwin 	device_t bus;
6748964299aSJohn Baldwin 
6758964299aSJohn Baldwin 	bus = device_get_parent(pcib);
676e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
677e706f7f0SJohn Baldwin }
678e706f7f0SJohn Baldwin 
679e706f7f0SJohn Baldwin static int
680e706f7f0SJohn Baldwin acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
681e706f7f0SJohn Baldwin     uint32_t *data)
682e706f7f0SJohn Baldwin {
6830d95597cSJohn Baldwin 	struct acpi_hpcib_softc *sc;
6840d95597cSJohn Baldwin 	device_t bus, hostb;
6850d95597cSJohn Baldwin 	int error;
686e706f7f0SJohn Baldwin 
687e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
6880d95597cSJohn Baldwin 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
6890d95597cSJohn Baldwin 	if (error)
6900d95597cSJohn Baldwin 		return (error);
6910d95597cSJohn Baldwin 
692af0d1ee9SNeel Natu 	sc = device_get_softc(pcib);
6930d95597cSJohn Baldwin 	if (sc->ap_addr == -1)
6940d95597cSJohn Baldwin 		return (0);
6950d95597cSJohn Baldwin 	/* XXX: Assumes all bridges are on bus 0. */
6960d95597cSJohn Baldwin 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
6970d95597cSJohn Baldwin 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
6980d95597cSJohn Baldwin 	if (hostb != NULL)
6990d95597cSJohn Baldwin 		pci_ht_map_msi(hostb, *addr);
7000d95597cSJohn Baldwin 	return (0);
7018964299aSJohn Baldwin }
7028964299aSJohn Baldwin 
703cd8b53edSWarner Losh struct resource *
704cd8b53edSWarner Losh acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
7052dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
706cd8b53edSWarner Losh {
70734ff71eeSJohn Baldwin #ifdef NEW_PCIB
70834ff71eeSJohn Baldwin     struct acpi_hpcib_softc *sc;
7095d0d779bSJohn Baldwin     struct resource *res;
71034ff71eeSJohn Baldwin #endif
71138d7a61bSJohn Baldwin 
71238d7a61bSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
71338d7a61bSJohn Baldwin     start = hostb_alloc_start(type, start, end, count);
71438d7a61bSJohn Baldwin #endif
71534ff71eeSJohn Baldwin 
71634ff71eeSJohn Baldwin #ifdef NEW_PCIB
71734ff71eeSJohn Baldwin     sc = device_get_softc(dev);
7184edef187SJohn Baldwin #ifdef PCI_RES_BUS
7194edef187SJohn Baldwin     if (type == PCI_RES_BUS)
7204edef187SJohn Baldwin 	return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
7214edef187SJohn Baldwin 	    count, flags));
7224edef187SJohn Baldwin #endif
7235d0d779bSJohn Baldwin     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
7245d0d779bSJohn Baldwin 	count, flags);
725a74d6952SJohn Baldwin 
726a74d6952SJohn Baldwin     /*
727a74d6952SJohn Baldwin      * XXX: If this is a request for a specific range, assume it is
728a74d6952SJohn Baldwin      * correct and pass it up to the parent.  What we probably want to
729a74d6952SJohn Baldwin      * do long-term is explicitly trust any firmware-configured
730a74d6952SJohn Baldwin      * resources during the initial bus scan on boot and then disable
731a74d6952SJohn Baldwin      * this after that.
732a74d6952SJohn Baldwin      */
7335d0d779bSJohn Baldwin     if (res == NULL && start + count - 1 == end)
734a74d6952SJohn Baldwin 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
735a74d6952SJohn Baldwin 	    count, flags);
7365d0d779bSJohn Baldwin     return (res);
73734ff71eeSJohn Baldwin #else
738cd8b53edSWarner Losh     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
739cd8b53edSWarner Losh 	count, flags));
74034ff71eeSJohn Baldwin #endif
741cd8b53edSWarner Losh }
74234ff71eeSJohn Baldwin 
74334ff71eeSJohn Baldwin #ifdef NEW_PCIB
74434ff71eeSJohn Baldwin int
745fef01f04SJohn Baldwin acpi_pcib_acpi_adjust_resource(device_t dev, device_t child,
7462dd1bdf1SJustin Hibbits     struct resource *r, rman_res_t start, rman_res_t end)
74734ff71eeSJohn Baldwin {
74834ff71eeSJohn Baldwin 	struct acpi_hpcib_softc *sc;
74934ff71eeSJohn Baldwin 
75034ff71eeSJohn Baldwin 	sc = device_get_softc(dev);
7514edef187SJohn Baldwin #ifdef PCI_RES_BUS
752fef01f04SJohn Baldwin 	if (rman_get_type(r) == PCI_RES_BUS)
7534edef187SJohn Baldwin 		return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
7544edef187SJohn Baldwin 		    end));
7554edef187SJohn Baldwin #endif
756fef01f04SJohn Baldwin 	return (pcib_host_res_adjust(&sc->ap_host_res, child, r, start, end));
75734ff71eeSJohn Baldwin }
7584edef187SJohn Baldwin 
7594edef187SJohn Baldwin #ifdef PCI_RES_BUS
7604edef187SJohn Baldwin int
761*9dbf5b0eSJohn Baldwin acpi_pcib_acpi_release_resource(device_t dev, device_t child,
7624edef187SJohn Baldwin     struct resource *r)
7634edef187SJohn Baldwin {
7644edef187SJohn Baldwin 	struct acpi_hpcib_softc *sc;
7654edef187SJohn Baldwin 
7664edef187SJohn Baldwin 	sc = device_get_softc(dev);
767*9dbf5b0eSJohn Baldwin 	if (rman_get_type(r) == PCI_RES_BUS)
768*9dbf5b0eSJohn Baldwin 		return (pci_domain_release_bus(sc->ap_segment, child, r));
769*9dbf5b0eSJohn Baldwin 	return (bus_generic_release_resource(dev, child, r));
7704edef187SJohn Baldwin }
771c2d4fef6SJohn Baldwin 
772c2d4fef6SJohn Baldwin int
7732baed46eSJohn Baldwin acpi_pcib_acpi_activate_resource(device_t dev, device_t child,
774c2d4fef6SJohn Baldwin     struct resource *r)
775c2d4fef6SJohn Baldwin {
776c2d4fef6SJohn Baldwin 	struct acpi_hpcib_softc *sc;
777c2d4fef6SJohn Baldwin 
778c2d4fef6SJohn Baldwin 	sc = device_get_softc(dev);
7792baed46eSJohn Baldwin 	if (rman_get_type(r) == PCI_RES_BUS)
7802baed46eSJohn Baldwin 		return (pci_domain_activate_bus(sc->ap_segment, child, r));
7812baed46eSJohn Baldwin 	return (bus_generic_activate_resource(dev, child, r));
782c2d4fef6SJohn Baldwin }
783c2d4fef6SJohn Baldwin 
784c2d4fef6SJohn Baldwin int
7852baed46eSJohn Baldwin acpi_pcib_acpi_deactivate_resource(device_t dev, device_t child,
7862baed46eSJohn Baldwin     struct resource *r)
787c2d4fef6SJohn Baldwin {
788c2d4fef6SJohn Baldwin 	struct acpi_hpcib_softc *sc;
789c2d4fef6SJohn Baldwin 
790c2d4fef6SJohn Baldwin 	sc = device_get_softc(dev);
7912baed46eSJohn Baldwin 	if (rman_get_type(r) == PCI_RES_BUS)
7922baed46eSJohn Baldwin 		return (pci_domain_deactivate_bus(sc->ap_segment, child, r));
7932baed46eSJohn Baldwin 	return (bus_generic_deactivate_resource(dev, child, r));
794c2d4fef6SJohn Baldwin }
7954edef187SJohn Baldwin #endif
79634ff71eeSJohn Baldwin #endif
79728586889SWarner Losh 
79828586889SWarner Losh static int
79928586889SWarner Losh acpi_pcib_request_feature(device_t pcib, device_t dev, enum pci_feature feature)
80028586889SWarner Losh {
80128586889SWarner Losh 	uint32_t osc_ctl;
80228586889SWarner Losh 	struct acpi_hpcib_softc *sc;
80328586889SWarner Losh 
8041ffd07bdSJohn Baldwin 	sc = device_get_softc(pcib);
80528586889SWarner Losh 
80628586889SWarner Losh 	switch (feature) {
80728586889SWarner Losh 	case PCI_FEATURE_HP:
80828586889SWarner Losh 		osc_ctl = PCIM_OSC_CTL_PCIE_HP;
80928586889SWarner Losh 		break;
81028586889SWarner Losh 	case PCI_FEATURE_AER:
81128586889SWarner Losh 		osc_ctl = PCIM_OSC_CTL_PCIE_AER;
81228586889SWarner Losh 		break;
81328586889SWarner Losh 	default:
81428586889SWarner Losh 		return (EINVAL);
81528586889SWarner Losh 	}
81628586889SWarner Losh 
81728586889SWarner Losh 	return (acpi_pcib_osc(sc, osc_ctl));
81828586889SWarner Losh }
8196f4acaf4SJeff Roberson 
8206f4acaf4SJeff Roberson static bus_dma_tag_t
8216f4acaf4SJeff Roberson acpi_pcib_get_dma_tag(device_t bus, device_t child)
8226f4acaf4SJeff Roberson {
8236f4acaf4SJeff Roberson 	struct acpi_hpcib_softc *sc;
8246f4acaf4SJeff Roberson 
8256f4acaf4SJeff Roberson 	sc = device_get_softc(bus);
8266f4acaf4SJeff Roberson 
8276f4acaf4SJeff Roberson 	return (sc->ap_dma_tag);
8286f4acaf4SJeff Roberson }
829