xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision 4edef187b8acc2ca09a38cb6bb6969ff5ead938b)
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"
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>
47*4edef187SJohn 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;
61d4b9ff91SNate Lawson     int			ap_flags;
6215e32d5dSMike Smith 
630d95597cSJohn Baldwin     int			ap_segment;	/* PCI domain */
6415e32d5dSMike Smith     int			ap_bus;		/* bios-assigned bus number */
650d95597cSJohn Baldwin     int			ap_addr;	/* device/func of PCI-Host bridge */
664fa387b6SMike Smith 
674fa387b6SMike Smith     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
6834ff71eeSJohn Baldwin #ifdef NEW_PCIB
6934ff71eeSJohn Baldwin     struct pcib_host_resources ap_host_res;
7034ff71eeSJohn Baldwin #endif
7115e32d5dSMike Smith };
7215e32d5dSMike Smith 
732ccfc932SJohn Baldwin static int		acpi_pcib_acpi_probe(device_t bus);
742ccfc932SJohn Baldwin static int		acpi_pcib_acpi_attach(device_t bus);
75e3aa81b8SNate Lawson static int		acpi_pcib_read_ivar(device_t dev, device_t child,
76e3aa81b8SNate Lawson 			    int which, uintptr_t *result);
77e3aa81b8SNate Lawson static int		acpi_pcib_write_ivar(device_t dev, device_t child,
78e3aa81b8SNate Lawson 			    int which, uintptr_t value);
791496d4a9SWarner Losh static uint32_t		acpi_pcib_read_config(device_t dev, u_int bus,
801496d4a9SWarner Losh 			    u_int slot, u_int func, u_int reg, int bytes);
811496d4a9SWarner Losh static void		acpi_pcib_write_config(device_t dev, u_int bus,
821496d4a9SWarner Losh 			    u_int slot, u_int func, u_int reg, uint32_t data,
831496d4a9SWarner Losh 			    int bytes);
842ccfc932SJohn Baldwin static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
852ccfc932SJohn Baldwin 			    device_t dev, int pin);
868964299aSJohn Baldwin static int		acpi_pcib_alloc_msi(device_t pcib, device_t dev,
878964299aSJohn Baldwin 			    int count, int maxcount, int *irqs);
88e706f7f0SJohn Baldwin static int		acpi_pcib_map_msi(device_t pcib, device_t dev,
89e706f7f0SJohn Baldwin 			    int irq, uint64_t *addr, uint32_t *data);
908964299aSJohn Baldwin static int		acpi_pcib_alloc_msix(device_t pcib, device_t dev,
91e706f7f0SJohn Baldwin 			    int *irq);
92cd8b53edSWarner Losh static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
93cd8b53edSWarner Losh 			    device_t child, int type, int *rid,
94cd8b53edSWarner Losh 			    u_long start, u_long end, u_long count,
95cd8b53edSWarner Losh 			    u_int flags);
9634ff71eeSJohn Baldwin #ifdef NEW_PCIB
9734ff71eeSJohn Baldwin static int		acpi_pcib_acpi_adjust_resource(device_t dev,
9834ff71eeSJohn Baldwin 			    device_t child, int type, struct resource *r,
9934ff71eeSJohn Baldwin 			    u_long start, u_long end);
100*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
101*4edef187SJohn Baldwin static int		acpi_pcib_acpi_release_resource(device_t dev,
102*4edef187SJohn Baldwin 			    device_t child, int type, int rid,
103*4edef187SJohn Baldwin 			    struct resource *r);
104*4edef187SJohn Baldwin #endif
10534ff71eeSJohn Baldwin #endif
10615e32d5dSMike Smith 
1072ccfc932SJohn Baldwin static device_method_t acpi_pcib_acpi_methods[] = {
10815e32d5dSMike Smith     /* Device interface */
1092ccfc932SJohn Baldwin     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
1102ccfc932SJohn Baldwin     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
11115e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
11215e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
1137d23a9b3SJohn Baldwin     DEVMETHOD(device_resume,		bus_generic_resume),
11415e32d5dSMike Smith 
11515e32d5dSMike Smith     /* Bus interface */
11615e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
11715e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
118cd8b53edSWarner Losh     DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
11934ff71eeSJohn Baldwin #ifdef NEW_PCIB
12034ff71eeSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	acpi_pcib_acpi_adjust_resource),
12134ff71eeSJohn Baldwin #else
122d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
12334ff71eeSJohn Baldwin #endif
124*4edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
125*4edef187SJohn Baldwin     DEVMETHOD(bus_release_resource,	acpi_pcib_acpi_release_resource),
126*4edef187SJohn Baldwin #else
12715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
128*4edef187SJohn Baldwin #endif
12915e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
13015e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
13115e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
13215e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
13315e32d5dSMike Smith 
13415e32d5dSMike Smith     /* pcib interface */
1352ccfc932SJohn Baldwin     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
13615e32d5dSMike Smith     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
13715e32d5dSMike Smith     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
1382ccfc932SJohn Baldwin     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
1398964299aSJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
1409bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1418964299aSJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
1429bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
143e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
14462508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
14515e32d5dSMike Smith 
1464b7ec270SMarius Strobl     DEVMETHOD_END
14715e32d5dSMike Smith };
14815e32d5dSMike Smith 
14904dda605SJohn Baldwin static devclass_t pcib_devclass;
15015e32d5dSMike Smith 
15104dda605SJohn Baldwin DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
15204dda605SJohn Baldwin     sizeof(struct acpi_hpcib_softc));
1532ccfc932SJohn Baldwin DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
15464278df5SNate Lawson MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
15515e32d5dSMike Smith 
15615e32d5dSMike Smith static int
1572ccfc932SJohn Baldwin acpi_pcib_acpi_probe(device_t dev)
15815e32d5dSMike Smith {
15992488a57SJung-uk Kim     ACPI_DEVICE_INFO	*devinfo;
16092488a57SJung-uk Kim     ACPI_HANDLE		h;
16192488a57SJung-uk Kim     int			root;
16215e32d5dSMike Smith 
16392488a57SJung-uk Kim     if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
16492488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
16592488a57SJung-uk Kim 	return (ENXIO);
16692488a57SJung-uk Kim     root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
16792488a57SJung-uk Kim     AcpiOsFree(devinfo);
16892488a57SJung-uk Kim     if (!root || pci_cfgregopen() == 0)
1695fcc8a58SNate Lawson 	return (ENXIO);
17015e32d5dSMike Smith 
1712ccfc932SJohn Baldwin     device_set_desc(dev, "ACPI Host-PCI bridge");
17215e32d5dSMike Smith     return (0);
17315e32d5dSMike Smith }
17415e32d5dSMike Smith 
17534ff71eeSJohn Baldwin #ifdef NEW_PCIB
17634ff71eeSJohn Baldwin static ACPI_STATUS
17734ff71eeSJohn Baldwin acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
17834ff71eeSJohn Baldwin {
17934ff71eeSJohn Baldwin 	struct acpi_hpcib_softc *sc;
18034ff71eeSJohn Baldwin 	UINT64 length, min, max;
18134ff71eeSJohn Baldwin 	u_int flags;
18234ff71eeSJohn Baldwin 	int error, type;
18334ff71eeSJohn Baldwin 
18434ff71eeSJohn Baldwin 	sc = context;
18534ff71eeSJohn Baldwin 	switch (res->Type) {
18634ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
18734ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
18834ff71eeSJohn Baldwin 		panic("host bridge has depenedent resources");
18934ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS16:
19034ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS32:
19134ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_ADDRESS64:
19234ff71eeSJohn Baldwin 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
19334ff71eeSJohn Baldwin 		if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
19434ff71eeSJohn Baldwin 			break;
19534ff71eeSJohn Baldwin 		switch (res->Type) {
19634ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS16:
19734ff71eeSJohn Baldwin 			min = res->Data.Address16.Minimum;
19834ff71eeSJohn Baldwin 			max = res->Data.Address16.Maximum;
19934ff71eeSJohn Baldwin 			length = res->Data.Address16.AddressLength;
20034ff71eeSJohn Baldwin 			break;
20134ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS32:
20234ff71eeSJohn Baldwin 			min = res->Data.Address32.Minimum;
20334ff71eeSJohn Baldwin 			max = res->Data.Address32.Maximum;
20434ff71eeSJohn Baldwin 			length = res->Data.Address32.AddressLength;
20534ff71eeSJohn Baldwin 			break;
20634ff71eeSJohn Baldwin 		case ACPI_RESOURCE_TYPE_ADDRESS64:
20734ff71eeSJohn Baldwin 			min = res->Data.Address64.Minimum;
20834ff71eeSJohn Baldwin 			max = res->Data.Address64.Maximum;
20934ff71eeSJohn Baldwin 			length = res->Data.Address64.AddressLength;
21034ff71eeSJohn Baldwin 			break;
21134ff71eeSJohn Baldwin 		default:
21234ff71eeSJohn Baldwin 			KASSERT(res->Type ==
21334ff71eeSJohn Baldwin 			    ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
21434ff71eeSJohn Baldwin 			    ("should never happen"));
21534ff71eeSJohn Baldwin 			min = res->Data.ExtAddress64.Minimum;
21634ff71eeSJohn Baldwin 			max = res->Data.ExtAddress64.Maximum;
21734ff71eeSJohn Baldwin 			length = res->Data.ExtAddress64.AddressLength;
21834ff71eeSJohn Baldwin 			break;
21934ff71eeSJohn Baldwin 		}
220e9f91b2bSJohn Baldwin 		if (length == 0)
221e9f91b2bSJohn Baldwin 			break;
222e9f91b2bSJohn Baldwin 		if (min + length - 1 != max &&
223e9f91b2bSJohn Baldwin 		    (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
224e9f91b2bSJohn Baldwin 		    res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
22534ff71eeSJohn Baldwin 			break;
22634ff71eeSJohn Baldwin 		flags = 0;
22734ff71eeSJohn Baldwin 		switch (res->Data.Address.ResourceType) {
22834ff71eeSJohn Baldwin 		case ACPI_MEMORY_RANGE:
22934ff71eeSJohn Baldwin 			type = SYS_RES_MEMORY;
23034ff71eeSJohn Baldwin 			if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
23134ff71eeSJohn Baldwin 				if (res->Data.Address.Info.Mem.Caching ==
23234ff71eeSJohn Baldwin 				    ACPI_PREFETCHABLE_MEMORY)
23334ff71eeSJohn Baldwin 					flags |= RF_PREFETCHABLE;
23434ff71eeSJohn Baldwin 			} else {
23534ff71eeSJohn Baldwin 				/*
23634ff71eeSJohn Baldwin 				 * XXX: Parse prefetch flag out of
23734ff71eeSJohn Baldwin 				 * TypeSpecific.
23834ff71eeSJohn Baldwin 				 */
23934ff71eeSJohn Baldwin 			}
24034ff71eeSJohn Baldwin 			break;
24134ff71eeSJohn Baldwin 		case ACPI_IO_RANGE:
24234ff71eeSJohn Baldwin 			type = SYS_RES_IOPORT;
24334ff71eeSJohn Baldwin 			break;
24434ff71eeSJohn Baldwin #ifdef PCI_RES_BUS
24534ff71eeSJohn Baldwin 		case ACPI_BUS_NUMBER_RANGE:
24634ff71eeSJohn Baldwin 			type = PCI_RES_BUS;
24734ff71eeSJohn Baldwin 			break;
24834ff71eeSJohn Baldwin #endif
24934ff71eeSJohn Baldwin 		default:
25034ff71eeSJohn Baldwin 			return (AE_OK);
25134ff71eeSJohn Baldwin 		}
25234ff71eeSJohn Baldwin 
25334ff71eeSJohn Baldwin 		if (min + length - 1 != max)
25434ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
25534ff71eeSJohn Baldwin 			    "Length mismatch for %d range: %jx vs %jx\n", type,
2560c10b85aSJung-uk Kim 			    (uintmax_t)(max - min + 1), (uintmax_t)length);
25734ff71eeSJohn Baldwin #ifdef __i386__
25834ff71eeSJohn Baldwin 		if (min > ULONG_MAX) {
25934ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
26034ff71eeSJohn Baldwin 			    "Ignoring %d range above 4GB (%#jx-%#jx)\n",
26134ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max);
26234ff71eeSJohn Baldwin 			break;
26334ff71eeSJohn Baldwin 		}
26434ff71eeSJohn Baldwin 		if (max > ULONG_MAX) {
26534ff71eeSJohn Baldwin 			device_printf(sc->ap_dev,
26634ff71eeSJohn Baldwin        		    "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
26734ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max);
26834ff71eeSJohn Baldwin 			max = ULONG_MAX;
26934ff71eeSJohn Baldwin 		}
27034ff71eeSJohn Baldwin #endif
27134ff71eeSJohn Baldwin 		error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
27234ff71eeSJohn Baldwin 		    flags);
27334ff71eeSJohn Baldwin 		if (error)
27434ff71eeSJohn Baldwin 			panic("Failed to manage %d range (%#jx-%#jx): %d",
27534ff71eeSJohn Baldwin 			    type, (uintmax_t)min, (uintmax_t)max, error);
27634ff71eeSJohn Baldwin 		break;
27734ff71eeSJohn Baldwin 	default:
27834ff71eeSJohn Baldwin 		break;
27934ff71eeSJohn Baldwin 	}
28034ff71eeSJohn Baldwin 	return (AE_OK);
28134ff71eeSJohn Baldwin }
28234ff71eeSJohn Baldwin #endif
28334ff71eeSJohn Baldwin 
284*4edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
285*4edef187SJohn Baldwin static int
286*4edef187SJohn Baldwin first_decoded_bus(struct acpi_hpcib_softc *sc, u_long *startp)
287*4edef187SJohn Baldwin {
288*4edef187SJohn Baldwin 	struct resource_list_entry *rle;
289*4edef187SJohn Baldwin 
290*4edef187SJohn Baldwin 	rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
291*4edef187SJohn Baldwin 	if (rle == NULL)
292*4edef187SJohn Baldwin 		return (ENXIO);
293*4edef187SJohn Baldwin 	*startp = rle->start;
294*4edef187SJohn Baldwin 	return (0);
295*4edef187SJohn Baldwin }
296*4edef187SJohn Baldwin #endif
297*4edef187SJohn Baldwin 
29815e32d5dSMike Smith static int
2992ccfc932SJohn Baldwin acpi_pcib_acpi_attach(device_t dev)
30015e32d5dSMike Smith {
3012ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc;
30215e32d5dSMike Smith     ACPI_STATUS			status;
3030668724bSJohn Baldwin     static int bus0_seen = 0;
3040d95597cSJohn Baldwin     u_int slot, func, busok;
305*4edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
306*4edef187SJohn Baldwin     struct resource *bus_res;
307*4edef187SJohn Baldwin     u_long start;
308*4edef187SJohn Baldwin     int rid;
309*4edef187SJohn Baldwin #endif
3109debb532SJohn Baldwin     uint8_t busno;
3110ae55423SMike Smith 
312b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
31315e32d5dSMike Smith 
31415e32d5dSMike Smith     sc = device_get_softc(dev);
31515e32d5dSMike Smith     sc->ap_dev = dev;
31615e32d5dSMike Smith     sc->ap_handle = acpi_get_handle(dev);
31715e32d5dSMike Smith 
31815e32d5dSMike Smith     /*
3198f4c2e52SJohn Baldwin      * Don't attach if we're not really there.
3208f4c2e52SJohn Baldwin      */
3218f4c2e52SJohn Baldwin     if (!acpi_DeviceIsPresent(dev))
3228f4c2e52SJohn Baldwin 	return (ENXIO);
3238f4c2e52SJohn Baldwin 
3248f4c2e52SJohn Baldwin     /*
3250d95597cSJohn Baldwin      * Get our segment number by evaluating _SEG.
3260668724bSJohn Baldwin      * It's OK for this to not exist.
3270668724bSJohn Baldwin      */
3280668724bSJohn Baldwin     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
3290668724bSJohn Baldwin     if (ACPI_FAILURE(status)) {
3300668724bSJohn Baldwin 	if (status != AE_NOT_FOUND) {
3310668724bSJohn Baldwin 	    device_printf(dev, "could not evaluate _SEG - %s\n",
3320668724bSJohn Baldwin 		AcpiFormatException(status));
3330668724bSJohn Baldwin 	    return_VALUE (ENXIO);
3340668724bSJohn Baldwin 	}
3350668724bSJohn Baldwin 	/* If it's not found, assume 0. */
3360668724bSJohn Baldwin 	sc->ap_segment = 0;
3370668724bSJohn Baldwin     }
3380668724bSJohn Baldwin 
3390d95597cSJohn Baldwin     /*
3400d95597cSJohn Baldwin      * Get the address (device and function) of the associated
3410d95597cSJohn Baldwin      * PCI-Host bridge device from _ADR.  Assume we don't have one if
3420d95597cSJohn Baldwin      * it doesn't exist.
3430d95597cSJohn Baldwin      */
3440d95597cSJohn Baldwin     status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
3450d95597cSJohn Baldwin     if (ACPI_FAILURE(status)) {
3460d95597cSJohn Baldwin 	device_printf(dev, "could not evaluate _ADR - %s\n",
3470d95597cSJohn Baldwin 	    AcpiFormatException(status));
3480d95597cSJohn Baldwin 	sc->ap_addr = -1;
3490d95597cSJohn Baldwin     }
3500d95597cSJohn Baldwin 
35134ff71eeSJohn Baldwin #ifdef NEW_PCIB
35234ff71eeSJohn Baldwin     /*
35334ff71eeSJohn Baldwin      * Determine which address ranges this bridge decodes and setup
35434ff71eeSJohn Baldwin      * resource managers for those ranges.
35534ff71eeSJohn Baldwin      */
35634ff71eeSJohn Baldwin     if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
35734ff71eeSJohn Baldwin 	    panic("failed to init hostb resources");
35834ff71eeSJohn Baldwin     if (!acpi_disabled("hostres")) {
35934ff71eeSJohn Baldwin 	status = AcpiWalkResources(sc->ap_handle, "_CRS",
36034ff71eeSJohn Baldwin 	    acpi_pcib_producer_handler, sc);
36134ff71eeSJohn Baldwin 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
36234ff71eeSJohn Baldwin 	    device_printf(sc->ap_dev, "failed to parse resources: %s\n",
36334ff71eeSJohn Baldwin 		AcpiFormatException(status));
36434ff71eeSJohn Baldwin     }
36534ff71eeSJohn Baldwin #endif
36634ff71eeSJohn Baldwin 
3670668724bSJohn Baldwin     /*
3689debb532SJohn Baldwin      * Get our base bus number by evaluating _BBN.
369c1b1f787SMike Smith      * If this doesn't work, we assume we're bus number 0.
37015e32d5dSMike Smith      *
37115e32d5dSMike Smith      * XXX note that it may also not exist in the case where we are
37215e32d5dSMike Smith      *     meant to use a private configuration space mechanism for this bus,
37315e32d5dSMike Smith      *     so we should dig out our resources and check to see if we have
37415e32d5dSMike Smith      *     anything like that.  How do we do this?
375042283a6SMike Smith      * XXX If we have the requisite information, and if we don't think the
376042283a6SMike Smith      *     default PCI configuration space handlers can deal with this bus,
377042283a6SMike Smith      *     we should attach our own handler.
378042283a6SMike Smith      * XXX invoke _REG on this for the PCI config space address space?
3799debb532SJohn Baldwin      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
3809debb532SJohn Baldwin      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
3810668724bSJohn Baldwin      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
3829debb532SJohn Baldwin      *     bus number from the configuration registers at address _ADR.
3830668724bSJohn Baldwin      *     We only do this for domain/segment 0 in the hopes that this is
3840668724bSJohn Baldwin      *     only needed for old single-domain machines.
38515e32d5dSMike Smith      */
386c310653eSNate Lawson     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
3879debb532SJohn Baldwin     if (ACPI_FAILURE(status)) {
38815e32d5dSMike Smith 	if (status != AE_NOT_FOUND) {
3899debb532SJohn Baldwin 	    device_printf(dev, "could not evaluate _BBN - %s\n",
3909debb532SJohn Baldwin 		AcpiFormatException(status));
3918f4c2e52SJohn Baldwin 	    return (ENXIO);
392c1b1f787SMike Smith 	} else {
393e3aa81b8SNate Lawson 	    /* If it's not found, assume 0. */
39415e32d5dSMike Smith 	    sc->ap_bus = 0;
39515e32d5dSMike Smith 	}
3968745ec57SJohn Baldwin     }
3979debb532SJohn Baldwin 
3989debb532SJohn Baldwin     /*
3990668724bSJohn Baldwin      * If this is segment 0, the bus is zero, and PCI bus 0 already
4000668724bSJohn Baldwin      * exists, read the bus number via PCI config space.
4019debb532SJohn Baldwin      */
4029debb532SJohn Baldwin     busok = 1;
4030668724bSJohn Baldwin     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
4048745ec57SJohn Baldwin 	busok = 0;
4050d95597cSJohn Baldwin 	if (sc->ap_addr != -1) {
4069debb532SJohn Baldwin 	    /* XXX: We assume bus 0. */
4070d95597cSJohn Baldwin 	    slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
4080d95597cSJohn Baldwin 	    func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
4099debb532SJohn Baldwin 	    if (bootverbose)
4109debb532SJohn Baldwin 		device_printf(dev, "reading config registers from 0:%d:%d\n",
4119debb532SJohn Baldwin 		    slot, func);
4128745ec57SJohn Baldwin 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
413e3aa81b8SNate Lawson 		device_printf(dev, "couldn't read bus number from cfg space\n");
4148745ec57SJohn Baldwin 	    else {
4159debb532SJohn Baldwin 		sc->ap_bus = busno;
4168745ec57SJohn Baldwin 		busok = 1;
4179debb532SJohn Baldwin 	    }
4189debb532SJohn Baldwin 	}
4199debb532SJohn Baldwin     }
4209debb532SJohn Baldwin 
421*4edef187SJohn Baldwin #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
422*4edef187SJohn Baldwin     /*
423*4edef187SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
424*4edef187SJohn Baldwin      * Host-PCI bridges in order and that as a result the next free
425*4edef187SJohn Baldwin      * bus number is our bus number.
426*4edef187SJohn Baldwin      */
427*4edef187SJohn Baldwin     if (busok == 0) {
428*4edef187SJohn Baldwin 	    /*
429*4edef187SJohn Baldwin 	     * If we have a region of bus numbers, use the first
430*4edef187SJohn Baldwin 	     * number for our bus.
431*4edef187SJohn Baldwin 	     */
432*4edef187SJohn Baldwin 	    if (first_decoded_bus(sc, &start) == 0)
433*4edef187SJohn Baldwin 		    sc->ap_bus = start;
434*4edef187SJohn Baldwin 	    else {
435*4edef187SJohn Baldwin 		    rid = 0;
436*4edef187SJohn Baldwin 		    bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
437*4edef187SJohn Baldwin 			PCI_BUSMAX, 1, 0);
438*4edef187SJohn Baldwin 		    if (bus_res == NULL) {
439*4edef187SJohn Baldwin 			    device_printf(dev,
440*4edef187SJohn Baldwin 				"could not allocate bus number\n");
441*4edef187SJohn Baldwin 			    pcib_host_res_free(dev, &sc->ap_host_res);
442*4edef187SJohn Baldwin 			    return (ENXIO);
443*4edef187SJohn Baldwin 		    }
444*4edef187SJohn Baldwin 		    sc->ap_bus = rman_get_start(bus_res);
445*4edef187SJohn Baldwin 		    pci_domain_release_bus(sc->ap_segment, dev, rid, bus_res);
446*4edef187SJohn Baldwin 	    }
447*4edef187SJohn Baldwin     } else {
448*4edef187SJohn Baldwin #ifdef INVARIANTS
449*4edef187SJohn Baldwin 	    if (first_decoded_bus(sc, &start) == 0)
450*4edef187SJohn Baldwin 		    KASSERT(start == sc->ap_bus, ("bus number mismatch"));
451*4edef187SJohn Baldwin #endif
452*4edef187SJohn Baldwin     }
453*4edef187SJohn Baldwin #else
4549debb532SJohn Baldwin     /*
4559debb532SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
4569debb532SJohn Baldwin      * host-PCI bridges in order and that as a result our unit number
4579debb532SJohn Baldwin      * is actually our bus number.  There are several reasons this
4589debb532SJohn Baldwin      * might not be true.
4599debb532SJohn Baldwin      */
4609debb532SJohn Baldwin     if (busok == 0) {
4619debb532SJohn Baldwin 	sc->ap_bus = device_get_unit(dev);
4629debb532SJohn Baldwin 	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
463c1b1f787SMike Smith     }
464*4edef187SJohn Baldwin #endif
46515e32d5dSMike Smith 
4660668724bSJohn Baldwin     /* If this is bus 0 on segment 0, note that it has been seen already. */
4670668724bSJohn Baldwin     if (sc->ap_segment == 0 && sc->ap_bus == 0)
4680668724bSJohn Baldwin 	    bus0_seen = 1;
46915e32d5dSMike Smith 
4702ccfc932SJohn Baldwin     return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
47115e32d5dSMike Smith }
47215e32d5dSMike Smith 
4734fa387b6SMike Smith /*
4744fa387b6SMike Smith  * Support for standard PCI bridge ivars.
4754fa387b6SMike Smith  */
47615e32d5dSMike Smith static int
47715e32d5dSMike Smith acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
47815e32d5dSMike Smith {
4792ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
48015e32d5dSMike Smith 
48115e32d5dSMike Smith     switch (which) {
48255aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
48324c93a6fSJohn Baldwin 	*result = sc->ap_segment;
48455aaf894SMarius Strobl 	return (0);
48515e32d5dSMike Smith     case PCIB_IVAR_BUS:
48615e32d5dSMike Smith 	*result = sc->ap_bus;
48715e32d5dSMike Smith 	return (0);
4882ccfc932SJohn Baldwin     case ACPI_IVAR_HANDLE:
4892ccfc932SJohn Baldwin 	*result = (uintptr_t)sc->ap_handle;
4902ccfc932SJohn Baldwin 	return (0);
491d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
492d4b9ff91SNate Lawson 	*result = (uintptr_t)sc->ap_flags;
493d4b9ff91SNate Lawson 	return (0);
49415e32d5dSMike Smith     }
49515e32d5dSMike Smith     return (ENOENT);
49615e32d5dSMike Smith }
49715e32d5dSMike Smith 
49815e32d5dSMike Smith static int
49915e32d5dSMike Smith acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
50015e32d5dSMike Smith {
5012ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
50215e32d5dSMike Smith 
50315e32d5dSMike Smith     switch (which) {
50455aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
50555aaf894SMarius Strobl 	return (EINVAL);
50615e32d5dSMike Smith     case PCIB_IVAR_BUS:
50715e32d5dSMike Smith 	sc->ap_bus = value;
50815e32d5dSMike Smith 	return (0);
509d4b9ff91SNate Lawson     case ACPI_IVAR_HANDLE:
510d4b9ff91SNate Lawson 	sc->ap_handle = (ACPI_HANDLE)value;
511d4b9ff91SNate Lawson 	return (0);
512d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
513d4b9ff91SNate Lawson 	sc->ap_flags = (int)value;
514d4b9ff91SNate Lawson 	return (0);
51515e32d5dSMike Smith     }
51615e32d5dSMike Smith     return (ENOENT);
51715e32d5dSMike Smith }
51815e32d5dSMike Smith 
519e3aa81b8SNate Lawson static uint32_t
5201496d4a9SWarner Losh acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
5211496d4a9SWarner Losh     u_int reg, int bytes)
52215e32d5dSMike Smith {
52315e32d5dSMike Smith     return (pci_cfgregread(bus, slot, func, reg, bytes));
52415e32d5dSMike Smith }
52515e32d5dSMike Smith 
52615e32d5dSMike Smith static void
5271496d4a9SWarner Losh acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
5281496d4a9SWarner Losh     u_int reg, uint32_t data, int bytes)
52915e32d5dSMike Smith {
53015e32d5dSMike Smith     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
53115e32d5dSMike Smith }
53215e32d5dSMike Smith 
53315e32d5dSMike Smith static int
5342ccfc932SJohn Baldwin acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
53515e32d5dSMike Smith {
5365e1ba6d4SJohn Baldwin     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
5374fa387b6SMike Smith 
5385e1ba6d4SJohn Baldwin     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
53915e32d5dSMike Smith }
540cd8b53edSWarner Losh 
5418964299aSJohn Baldwin static int
5428964299aSJohn Baldwin acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
5438964299aSJohn Baldwin     int *irqs)
5448964299aSJohn Baldwin {
5458964299aSJohn Baldwin 	device_t bus;
5468964299aSJohn Baldwin 
5478964299aSJohn Baldwin 	bus = device_get_parent(pcib);
5488964299aSJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
5498964299aSJohn Baldwin 	    irqs));
5508964299aSJohn Baldwin }
5518964299aSJohn Baldwin 
5528964299aSJohn Baldwin static int
553e706f7f0SJohn Baldwin acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
5548964299aSJohn Baldwin {
5558964299aSJohn Baldwin 	device_t bus;
5568964299aSJohn Baldwin 
5578964299aSJohn Baldwin 	bus = device_get_parent(pcib);
558e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
559e706f7f0SJohn Baldwin }
560e706f7f0SJohn Baldwin 
561e706f7f0SJohn Baldwin static int
562e706f7f0SJohn Baldwin acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
563e706f7f0SJohn Baldwin     uint32_t *data)
564e706f7f0SJohn Baldwin {
5650d95597cSJohn Baldwin 	struct acpi_hpcib_softc *sc;
5660d95597cSJohn Baldwin 	device_t bus, hostb;
5670d95597cSJohn Baldwin 	int error;
568e706f7f0SJohn Baldwin 
569e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
5700d95597cSJohn Baldwin 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
5710d95597cSJohn Baldwin 	if (error)
5720d95597cSJohn Baldwin 		return (error);
5730d95597cSJohn Baldwin 
574af0d1ee9SNeel Natu 	sc = device_get_softc(pcib);
5750d95597cSJohn Baldwin 	if (sc->ap_addr == -1)
5760d95597cSJohn Baldwin 		return (0);
5770d95597cSJohn Baldwin 	/* XXX: Assumes all bridges are on bus 0. */
5780d95597cSJohn Baldwin 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
5790d95597cSJohn Baldwin 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
5800d95597cSJohn Baldwin 	if (hostb != NULL)
5810d95597cSJohn Baldwin 		pci_ht_map_msi(hostb, *addr);
5820d95597cSJohn Baldwin 	return (0);
5838964299aSJohn Baldwin }
5848964299aSJohn Baldwin 
585cd8b53edSWarner Losh struct resource *
586cd8b53edSWarner Losh acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
587cd8b53edSWarner Losh     u_long start, u_long end, u_long count, u_int flags)
588cd8b53edSWarner Losh {
58934ff71eeSJohn Baldwin #ifdef NEW_PCIB
59034ff71eeSJohn Baldwin     struct acpi_hpcib_softc *sc;
5915d0d779bSJohn Baldwin     struct resource *res;
59234ff71eeSJohn Baldwin #endif
59338d7a61bSJohn Baldwin 
59438d7a61bSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
59538d7a61bSJohn Baldwin     start = hostb_alloc_start(type, start, end, count);
59638d7a61bSJohn Baldwin #endif
59734ff71eeSJohn Baldwin 
59834ff71eeSJohn Baldwin #ifdef NEW_PCIB
59934ff71eeSJohn Baldwin     sc = device_get_softc(dev);
600*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
601*4edef187SJohn Baldwin     if (type == PCI_RES_BUS)
602*4edef187SJohn Baldwin 	return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
603*4edef187SJohn Baldwin 	    count, flags));
604*4edef187SJohn Baldwin #endif
6055d0d779bSJohn Baldwin     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
6065d0d779bSJohn Baldwin 	count, flags);
607a74d6952SJohn Baldwin 
608a74d6952SJohn Baldwin     /*
609a74d6952SJohn Baldwin      * XXX: If this is a request for a specific range, assume it is
610a74d6952SJohn Baldwin      * correct and pass it up to the parent.  What we probably want to
611a74d6952SJohn Baldwin      * do long-term is explicitly trust any firmware-configured
612a74d6952SJohn Baldwin      * resources during the initial bus scan on boot and then disable
613a74d6952SJohn Baldwin      * this after that.
614a74d6952SJohn Baldwin      */
6155d0d779bSJohn Baldwin     if (res == NULL && start + count - 1 == end)
616a74d6952SJohn Baldwin 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
617a74d6952SJohn Baldwin 	    count, flags);
6185d0d779bSJohn Baldwin     return (res);
61934ff71eeSJohn Baldwin #else
620cd8b53edSWarner Losh     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
621cd8b53edSWarner Losh 	count, flags));
62234ff71eeSJohn Baldwin #endif
623cd8b53edSWarner Losh }
62434ff71eeSJohn Baldwin 
62534ff71eeSJohn Baldwin #ifdef NEW_PCIB
62634ff71eeSJohn Baldwin int
62734ff71eeSJohn Baldwin acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type,
62834ff71eeSJohn Baldwin     struct resource *r, u_long start, u_long end)
62934ff71eeSJohn Baldwin {
63034ff71eeSJohn Baldwin 	struct acpi_hpcib_softc *sc;
63134ff71eeSJohn Baldwin 
63234ff71eeSJohn Baldwin 	sc = device_get_softc(dev);
633*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
634*4edef187SJohn Baldwin 	if (type == PCI_RES_BUS)
635*4edef187SJohn Baldwin 		return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
636*4edef187SJohn Baldwin 		    end));
637*4edef187SJohn Baldwin #endif
63834ff71eeSJohn Baldwin 	return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start,
63934ff71eeSJohn Baldwin 	    end));
64034ff71eeSJohn Baldwin }
641*4edef187SJohn Baldwin 
642*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
643*4edef187SJohn Baldwin int
644*4edef187SJohn Baldwin acpi_pcib_acpi_release_resource(device_t dev, device_t child, int type, int rid,
645*4edef187SJohn Baldwin     struct resource *r)
646*4edef187SJohn Baldwin {
647*4edef187SJohn Baldwin 	struct acpi_hpcib_softc *sc;
648*4edef187SJohn Baldwin 
649*4edef187SJohn Baldwin 	sc = device_get_softc(dev);
650*4edef187SJohn Baldwin 	if (type == PCI_RES_BUS)
651*4edef187SJohn Baldwin 		return (pci_domain_release_bus(sc->ap_segment, child, rid, r));
652*4edef187SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
653*4edef187SJohn Baldwin }
654*4edef187SJohn Baldwin #endif
65534ff71eeSJohn Baldwin #endif
656