Home | History | Annotate | Line # | Download | only in acpi
acpi_mcfg.c revision 1.15
      1  1.15  jmcneill /*	$NetBSD: acpi_mcfg.c,v 1.15 2018/12/08 15:02:06 jmcneill Exp $	*/
      2   1.1   msaitoh 
      3   1.1   msaitoh /*-
      4   1.1   msaitoh  * Copyright (C) 2015 NONAKA Kimihiro <nonaka (at) NetBSD.org>
      5   1.1   msaitoh  * All rights reserved.
      6   1.1   msaitoh  *
      7   1.1   msaitoh  * Redistribution and use in source and binary forms, with or without
      8   1.1   msaitoh  * modification, are permitted provided that the following conditions
      9   1.1   msaitoh  * are met:
     10   1.1   msaitoh  * 1. Redistributions of source code must retain the above copyright
     11   1.1   msaitoh  *    notice, this list of conditions and the following disclaimer.
     12   1.1   msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   msaitoh  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   msaitoh  *    documentation and/or other materials provided with the distribution.
     15   1.1   msaitoh  *
     16   1.1   msaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1   msaitoh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1   msaitoh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1   msaitoh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1   msaitoh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1   msaitoh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1   msaitoh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1   msaitoh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1   msaitoh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1   msaitoh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1   msaitoh  */
     27   1.1   msaitoh 
     28   1.8  jmcneill #include "opt_pci.h"
     29   1.8  jmcneill 
     30   1.1   msaitoh #include <sys/cdefs.h>
     31  1.15  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_mcfg.c,v 1.15 2018/12/08 15:02:06 jmcneill Exp $");
     32   1.1   msaitoh 
     33   1.1   msaitoh #include <sys/param.h>
     34   1.1   msaitoh #include <sys/device.h>
     35   1.1   msaitoh #include <sys/kmem.h>
     36   1.1   msaitoh #include <sys/systm.h>
     37   1.8  jmcneill #include <sys/extent.h>
     38   1.1   msaitoh 
     39   1.1   msaitoh #include <dev/pci/pcireg.h>
     40   1.1   msaitoh #include <dev/pci/pcivar.h>
     41   1.8  jmcneill #include <dev/pci/pciconf.h>
     42   1.1   msaitoh #include <dev/pci/pcidevs.h>
     43   1.1   msaitoh 
     44   1.1   msaitoh #include <dev/acpi/acpireg.h>
     45   1.1   msaitoh #include <dev/acpi/acpivar.h>
     46   1.1   msaitoh #include <dev/acpi/acpi_mcfg.h>
     47   1.1   msaitoh 
     48   1.1   msaitoh #include "locators.h"
     49   1.1   msaitoh 
     50   1.2  christos #define _COMPONENT      ACPI_RESOURCE_COMPONENT
     51   1.2  christos ACPI_MODULE_NAME	("acpi_mcfg")
     52   1.2  christos 
     53   1.1   msaitoh #define	EXTCONF_OFFSET(d, f, r)	((((d) * 8 + (f)) * PCI_EXTCONF_SIZE) + (r))
     54   1.1   msaitoh 
     55  1.10  jmcneill #define	PCIDEV_SET_VALID(mb, d, f)	((mb)->valid_devs[(d)] |= __BIT((f)))
     56  1.10  jmcneill #define	PCIDEV_SET_INVALID(mb, d, f)	((mb)->valid_devs[(d)] &= ~__BIT((f)))
     57  1.10  jmcneill #define	PCIDEV_IS_VALID(mb, d, f)	((mb)->valid_devs[(d)] & __BIT((f)))
     58  1.10  jmcneill 
     59  1.10  jmcneill #define	EXTCONF_SET_VALID(mb, d, f)	((mb)->valid_extconf[(d)] |= __BIT((f)))
     60  1.10  jmcneill #define	EXTCONF_SET_INVALID(mb, d, f)	((mb)->valid_extconf[(d)] &= ~__BIT((f)))
     61  1.10  jmcneill #define	EXTCONF_IS_VALID(mb, d, f)	((mb)->valid_extconf[(d)] & __BIT((f)))
     62   1.1   msaitoh 
     63   1.1   msaitoh struct mcfg_segment {
     64   1.1   msaitoh 	uint64_t ms_address;		/* Base address */
     65   1.1   msaitoh 	int ms_segment;			/* Segment # */
     66   1.1   msaitoh 	int ms_bus_start;		/* Start bus # */
     67   1.1   msaitoh 	int ms_bus_end;			/* End bus # */
     68   1.1   msaitoh 	bus_space_tag_t ms_bst;
     69   1.1   msaitoh 	struct mcfg_bus {
     70   1.1   msaitoh 		bus_space_handle_t bsh[32][8];
     71   1.1   msaitoh 		uint8_t valid_devs[32];
     72  1.10  jmcneill 		uint8_t valid_extconf[32];
     73   1.1   msaitoh 		int valid_ndevs;
     74   1.1   msaitoh 		pcitag_t last_probed;
     75   1.1   msaitoh 	} *ms_bus;
     76   1.1   msaitoh };
     77   1.1   msaitoh 
     78   1.1   msaitoh static struct mcfg_segment *mcfg_segs;
     79   1.1   msaitoh static int mcfg_nsegs;
     80   1.1   msaitoh static ACPI_TABLE_MCFG *mcfg;
     81   1.1   msaitoh static int mcfg_inited;
     82   1.1   msaitoh static struct acpi_softc *acpi_sc;
     83   1.1   msaitoh 
     84   1.1   msaitoh static const struct acpimcfg_ops mcfg_default_ops = {
     85   1.1   msaitoh 	.ao_validate = acpimcfg_default_validate,
     86   1.1   msaitoh 
     87   1.1   msaitoh 	.ao_read = acpimcfg_default_read,
     88   1.1   msaitoh 	.ao_write = acpimcfg_default_write,
     89   1.1   msaitoh };
     90   1.1   msaitoh static const struct acpimcfg_ops *mcfg_ops = &mcfg_default_ops;
     91   1.1   msaitoh 
     92   1.1   msaitoh /*
     93   1.1   msaitoh  * default operations.
     94   1.1   msaitoh  */
     95   1.1   msaitoh bool
     96   1.1   msaitoh acpimcfg_default_validate(uint64_t address, int bus_start, int *bus_end)
     97   1.1   msaitoh {
     98   1.1   msaitoh 
     99   1.1   msaitoh 	/* Always Ok */
    100   1.1   msaitoh 	return true;
    101   1.1   msaitoh }
    102   1.1   msaitoh 
    103   1.1   msaitoh uint32_t
    104   1.1   msaitoh acpimcfg_default_read(bus_space_tag_t bst, bus_space_handle_t bsh,
    105   1.1   msaitoh     bus_addr_t addr)
    106   1.1   msaitoh {
    107   1.1   msaitoh 
    108   1.1   msaitoh 	return bus_space_read_4(bst, bsh, addr);
    109   1.1   msaitoh }
    110   1.1   msaitoh 
    111   1.1   msaitoh void
    112   1.1   msaitoh acpimcfg_default_write(bus_space_tag_t bst, bus_space_handle_t bsh,
    113   1.1   msaitoh     bus_addr_t addr, uint32_t data)
    114   1.1   msaitoh {
    115   1.1   msaitoh 
    116   1.1   msaitoh 	bus_space_write_4(bst, bsh, addr, data);
    117   1.1   msaitoh }
    118   1.1   msaitoh 
    119   1.1   msaitoh 
    120   1.1   msaitoh /*
    121   1.1   msaitoh  * Check MCFG memory region at system resource
    122   1.1   msaitoh  */
    123   1.1   msaitoh struct acpimcfg_memrange {
    124   1.1   msaitoh 	const char	*hid;
    125   1.1   msaitoh 	uint64_t	address;
    126   1.1   msaitoh 	int		bus_start;
    127   1.1   msaitoh 	int		bus_end;
    128   1.1   msaitoh 	bool		found;
    129   1.1   msaitoh };
    130   1.1   msaitoh 
    131   1.1   msaitoh static ACPI_STATUS
    132   1.1   msaitoh acpimcfg_parse_callback(ACPI_RESOURCE *res, void *ctx)
    133   1.1   msaitoh {
    134   1.1   msaitoh 	struct acpimcfg_memrange *mr = ctx;
    135   1.1   msaitoh 	const char *type;
    136   1.1   msaitoh 	uint64_t size, mapaddr, mapsize;
    137   1.1   msaitoh 	int n;
    138   1.1   msaitoh 
    139   1.1   msaitoh 	switch (res->Type) {
    140   1.1   msaitoh 	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
    141   1.1   msaitoh 		type = "FIXED_MEMORY32";
    142   1.1   msaitoh 		mapaddr = res->Data.FixedMemory32.Address;
    143   1.1   msaitoh 		mapsize = res->Data.FixedMemory32.AddressLength;
    144   1.1   msaitoh 		break;
    145   1.1   msaitoh 
    146   1.1   msaitoh 	case ACPI_RESOURCE_TYPE_ADDRESS32:
    147   1.1   msaitoh 		/* XXX Only fixed size supported for now */
    148   1.1   msaitoh 		if (res->Data.Address32.Address.AddressLength == 0 ||
    149   1.1   msaitoh 		    res->Data.Address32.ProducerConsumer != ACPI_CONSUMER)
    150   1.1   msaitoh 			goto out;
    151   1.1   msaitoh 
    152   1.1   msaitoh 		if (res->Data.Address32.ResourceType != ACPI_MEMORY_RANGE)
    153   1.1   msaitoh 			goto out;
    154   1.1   msaitoh 
    155   1.1   msaitoh 		if (res->Data.Address32.MinAddressFixed != ACPI_ADDRESS_FIXED ||
    156   1.1   msaitoh 		    res->Data.Address32.MaxAddressFixed != ACPI_ADDRESS_FIXED)
    157   1.1   msaitoh 			goto out;
    158   1.1   msaitoh 
    159   1.1   msaitoh 		type = "ADDRESS32";
    160   1.1   msaitoh 		mapaddr = res->Data.Address32.Address.Minimum;
    161   1.1   msaitoh 		mapsize = res->Data.Address32.Address.AddressLength;
    162   1.1   msaitoh 		break;
    163   1.1   msaitoh 
    164   1.1   msaitoh #ifdef _LP64
    165   1.1   msaitoh 	case ACPI_RESOURCE_TYPE_ADDRESS64:
    166   1.1   msaitoh 		/* XXX Only fixed size supported for now */
    167   1.1   msaitoh 		if (res->Data.Address64.Address.AddressLength == 0 ||
    168   1.1   msaitoh 		    res->Data.Address64.ProducerConsumer != ACPI_CONSUMER)
    169   1.1   msaitoh 			goto out;
    170   1.1   msaitoh 
    171   1.1   msaitoh 		if (res->Data.Address64.ResourceType != ACPI_MEMORY_RANGE)
    172   1.1   msaitoh 			goto out;
    173   1.1   msaitoh 
    174   1.1   msaitoh 		if (res->Data.Address64.MinAddressFixed != ACPI_ADDRESS_FIXED ||
    175   1.1   msaitoh 		    res->Data.Address64.MaxAddressFixed != ACPI_ADDRESS_FIXED)
    176   1.1   msaitoh 			goto out;
    177   1.1   msaitoh 
    178   1.1   msaitoh 		type = "ADDRESS64";
    179   1.1   msaitoh 		mapaddr = res->Data.Address64.Address.Minimum;
    180   1.1   msaitoh 		mapsize = res->Data.Address64.Address.AddressLength;
    181   1.1   msaitoh 		break;
    182   1.1   msaitoh #endif
    183   1.1   msaitoh 
    184   1.1   msaitoh 	default:
    185   1.1   msaitoh  out:
    186   1.1   msaitoh 		aprint_debug_dev(acpi_sc->sc_dev, "MCFG: %s: Type=%d\n",
    187   1.1   msaitoh 		    mr->hid, res->Type);
    188   1.1   msaitoh 		return_ACPI_STATUS(AE_OK);
    189   1.1   msaitoh 	}
    190   1.1   msaitoh 
    191   1.1   msaitoh 	aprint_debug_dev(acpi_sc->sc_dev, "MCFG: %s: Type=%d(%s), "
    192   1.1   msaitoh 	    "Address=0x%016" PRIx64 ", Length=0x%016" PRIx64 "\n",
    193   1.1   msaitoh 	    mr->hid, res->Type, type, mapaddr, mapsize);
    194   1.1   msaitoh 
    195   1.1   msaitoh 	if (mr->address < mapaddr || mr->address >= mapaddr + mapsize)
    196   1.1   msaitoh 		return_ACPI_STATUS(AE_OK);
    197   1.1   msaitoh 
    198   1.1   msaitoh 	size = (mr->bus_end - mr->bus_start + 1) * ACPIMCFG_SIZE_PER_BUS;
    199   1.1   msaitoh 
    200   1.1   msaitoh 	/* full map */
    201   1.1   msaitoh 	if (mr->address + size <= mapaddr + mapsize) {
    202   1.1   msaitoh 		mr->found = true;
    203   1.1   msaitoh 		return_ACPI_STATUS(AE_CTRL_TERMINATE);
    204   1.1   msaitoh 	}
    205   1.1   msaitoh 
    206   1.1   msaitoh 	/* partial map */
    207   1.1   msaitoh 	n = (mapsize - (mr->address - mapaddr)) / ACPIMCFG_SIZE_PER_BUS;
    208   1.1   msaitoh 	/* bus_start == bus_end is not allowed. */
    209   1.1   msaitoh 	if (n > 1) {
    210   1.1   msaitoh 		mr->bus_end = mr->bus_start + n - 1;
    211   1.1   msaitoh 		mr->found = true;
    212   1.1   msaitoh 		return_ACPI_STATUS(AE_CTRL_TERMINATE);
    213   1.1   msaitoh 	}
    214   1.1   msaitoh 
    215   1.1   msaitoh 	aprint_debug_dev(acpi_sc->sc_dev, "MCFG: bus %d-%d, "
    216   1.1   msaitoh 	    "address 0x%016" PRIx64 ": invalid size: request 0x%016" PRIx64
    217   1.1   msaitoh 	    ", actual 0x%016" PRIx64 "\n",
    218   1.1   msaitoh 	    mr->bus_start, mr->bus_end, mr->address, size, mapsize);
    219   1.1   msaitoh 
    220   1.1   msaitoh 	return_ACPI_STATUS(AE_OK);
    221   1.1   msaitoh }
    222   1.1   msaitoh 
    223   1.1   msaitoh static ACPI_STATUS
    224   1.1   msaitoh acpimcfg_check_system_resource(ACPI_HANDLE handle, UINT32 level, void *ctx,
    225   1.1   msaitoh     void **retval)
    226   1.1   msaitoh {
    227   1.1   msaitoh 	struct acpimcfg_memrange *mr = ctx;
    228   1.1   msaitoh 	ACPI_STATUS status;
    229   1.1   msaitoh 
    230   1.1   msaitoh 	status = AcpiWalkResources(handle, "_CRS", acpimcfg_parse_callback, mr);
    231   1.1   msaitoh 	if (ACPI_FAILURE(status))
    232   1.1   msaitoh 		return_ACPI_STATUS(status);
    233   1.1   msaitoh 
    234   1.1   msaitoh 	if (mr->found)
    235   1.1   msaitoh 		return_ACPI_STATUS(AE_CTRL_TERMINATE);
    236   1.1   msaitoh 
    237   1.1   msaitoh 	aprint_debug_dev(acpi_sc->sc_dev, "MCFG: %s: bus %d-%d, "
    238   1.1   msaitoh 	    "address 0x%016" PRIx64 ": no valid region\n", mr->hid,
    239   1.1   msaitoh 	    mr->bus_start, mr->bus_end, mr->address);
    240   1.1   msaitoh 
    241   1.1   msaitoh 	return_ACPI_STATUS(AE_OK);
    242   1.1   msaitoh }
    243   1.1   msaitoh 
    244   1.1   msaitoh static bool
    245   1.1   msaitoh acpimcfg_find_system_resource(uint64_t address, int bus_start, int *bus_end)
    246   1.1   msaitoh {
    247   1.1   msaitoh 	static const char *system_resource_hid[] = {
    248   1.1   msaitoh 		"PNP0C01",	/* System Board */
    249   1.1   msaitoh 		"PNP0C02"	/* General ID for reserving resources */
    250   1.1   msaitoh 	};
    251   1.1   msaitoh 	struct acpimcfg_memrange mr;
    252   1.1   msaitoh 	ACPI_STATUS status;
    253   1.1   msaitoh 	int i;
    254   1.1   msaitoh 
    255   1.1   msaitoh 	mr.address = address;
    256   1.1   msaitoh 	mr.bus_start = bus_start;
    257   1.1   msaitoh 	mr.bus_end = *bus_end;
    258   1.1   msaitoh 	mr.found = false;
    259   1.1   msaitoh 
    260   1.1   msaitoh 	for (i = 0; i < __arraycount(system_resource_hid); i++) {
    261   1.1   msaitoh 		mr.hid = system_resource_hid[i];
    262   1.1   msaitoh 		status = AcpiGetDevices(__UNCONST(system_resource_hid[i]),
    263   1.1   msaitoh 		    acpimcfg_check_system_resource, &mr, NULL);
    264   1.1   msaitoh 		if (ACPI_FAILURE(status))
    265   1.1   msaitoh 			continue;
    266   1.1   msaitoh 		if (mr.found) {
    267   1.1   msaitoh 			*bus_end = mr.bus_end;
    268   1.1   msaitoh 			return true;
    269   1.1   msaitoh 		}
    270   1.1   msaitoh 	}
    271   1.1   msaitoh 	return false;
    272   1.1   msaitoh }
    273   1.1   msaitoh 
    274   1.1   msaitoh 
    275   1.1   msaitoh /*
    276   1.1   msaitoh  * ACPI MCFG
    277   1.1   msaitoh  */
    278   1.1   msaitoh void
    279   1.1   msaitoh acpimcfg_probe(struct acpi_softc *sc)
    280   1.1   msaitoh {
    281   1.1   msaitoh 	ACPI_MCFG_ALLOCATION *ama;
    282   1.1   msaitoh 	ACPI_STATUS status;
    283   1.1   msaitoh 	uint32_t offset;
    284   1.1   msaitoh 	int i, nsegs;
    285   1.1   msaitoh 
    286   1.1   msaitoh 	if (acpi_sc != NULL)
    287   1.1   msaitoh 		panic("acpi_sc != NULL");
    288   1.1   msaitoh 	acpi_sc = sc;
    289   1.1   msaitoh 
    290   1.1   msaitoh 	status = AcpiGetTable(ACPI_SIG_MCFG, 0, (ACPI_TABLE_HEADER **)&mcfg);
    291   1.1   msaitoh 	if (ACPI_FAILURE(status)) {
    292   1.1   msaitoh 		mcfg = NULL;
    293   1.1   msaitoh 		return;
    294   1.1   msaitoh 	}
    295   1.1   msaitoh 
    296   1.1   msaitoh 	nsegs = 0;
    297   1.1   msaitoh 	offset = sizeof(ACPI_TABLE_MCFG);
    298   1.1   msaitoh 	ama = ACPI_ADD_PTR(ACPI_MCFG_ALLOCATION, mcfg, offset);
    299   1.6      maxv 	for (i = 0; offset + sizeof(ACPI_MCFG_ALLOCATION) <=
    300   1.6      maxv 	    mcfg->Header.Length; i++) {
    301   1.1   msaitoh 		aprint_debug_dev(sc->sc_dev,
    302   1.1   msaitoh 		    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64 "\n",
    303   1.1   msaitoh 		    ama->PciSegment, ama->StartBusNumber, ama->EndBusNumber,
    304   1.1   msaitoh 		    ama->Address);
    305   1.1   msaitoh 		nsegs++;
    306   1.1   msaitoh 		offset += sizeof(ACPI_MCFG_ALLOCATION);
    307  1.11  jmcneill 		ama = ACPI_ADD_PTR(ACPI_MCFG_ALLOCATION, mcfg, offset);
    308   1.1   msaitoh 	}
    309   1.1   msaitoh 	if (nsegs == 0) {
    310   1.1   msaitoh 		mcfg = NULL;
    311   1.1   msaitoh 		return;
    312   1.1   msaitoh 	}
    313   1.1   msaitoh 
    314   1.1   msaitoh 	mcfg_segs = kmem_zalloc(sizeof(*mcfg_segs) * nsegs, KM_SLEEP);
    315   1.1   msaitoh 	mcfg_nsegs = nsegs;
    316   1.1   msaitoh }
    317   1.1   msaitoh 
    318   1.1   msaitoh int
    319   1.1   msaitoh acpimcfg_init(bus_space_tag_t memt, const struct acpimcfg_ops *ops)
    320   1.1   msaitoh {
    321   1.1   msaitoh 	ACPI_MCFG_ALLOCATION *ama;
    322   1.1   msaitoh 	struct mcfg_segment *seg;
    323   1.1   msaitoh 	uint32_t offset;
    324   1.1   msaitoh 	int i, n, nsegs, bus_end;
    325   1.1   msaitoh 
    326   1.1   msaitoh 	if (mcfg == NULL)
    327   1.1   msaitoh 		return ENXIO;
    328   1.1   msaitoh 
    329   1.1   msaitoh 	if (mcfg_inited)
    330   1.1   msaitoh 		return 0;
    331   1.1   msaitoh 
    332   1.1   msaitoh 	if (ops != NULL)
    333   1.1   msaitoh 		mcfg_ops = ops;
    334   1.1   msaitoh 
    335   1.1   msaitoh 	nsegs = 0;
    336   1.1   msaitoh 	offset = sizeof(ACPI_TABLE_MCFG);
    337   1.1   msaitoh 	ama = ACPI_ADD_PTR(ACPI_MCFG_ALLOCATION, mcfg, offset);
    338   1.1   msaitoh 	for (i = 0; offset < mcfg->Header.Length; i++) {
    339   1.1   msaitoh #ifndef _LP64
    340   1.1   msaitoh 		if (ama->Address >= 0x100000000ULL) {
    341   1.1   msaitoh 			aprint_debug_dev(acpi_sc->sc_dev,
    342   1.1   msaitoh 			    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64
    343   1.1   msaitoh 			    ": ignore (64bit address)\n", ama->PciSegment,
    344   1.1   msaitoh 			    ama->StartBusNumber, ama->EndBusNumber,
    345   1.1   msaitoh 			    ama->Address);
    346   1.1   msaitoh 			goto next;
    347   1.1   msaitoh 		}
    348   1.1   msaitoh #endif
    349   1.1   msaitoh 		/*
    350   1.1   msaitoh 		 * Some (broken?) BIOSen have an MCFG table for an empty
    351   1.1   msaitoh 		 * bus range.  Ignore those tables.
    352   1.1   msaitoh 		 */
    353   1.1   msaitoh 		if (ama->StartBusNumber == ama->EndBusNumber) {
    354   1.1   msaitoh 			aprint_debug_dev(acpi_sc->sc_dev,
    355   1.1   msaitoh 			    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64
    356   1.1   msaitoh 			    ": ignore (bus %d == %d)\n", ama->PciSegment,
    357   1.1   msaitoh 			    ama->StartBusNumber, ama->EndBusNumber,
    358   1.1   msaitoh 			    ama->Address, ama->StartBusNumber,
    359   1.1   msaitoh 			    ama->EndBusNumber);
    360   1.1   msaitoh 			goto next;
    361   1.1   msaitoh 		}
    362   1.1   msaitoh 		if (ama->StartBusNumber > ama->EndBusNumber) {
    363   1.1   msaitoh 			aprint_debug_dev(acpi_sc->sc_dev,
    364   1.1   msaitoh 			    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64
    365   1.1   msaitoh 			    ": ignore (bus %d > %d)\n", ama->PciSegment,
    366   1.1   msaitoh 			    ama->StartBusNumber, ama->EndBusNumber,
    367   1.1   msaitoh 			    ama->Address, ama->StartBusNumber,
    368   1.1   msaitoh 			    ama->EndBusNumber);
    369   1.1   msaitoh 			goto next;
    370   1.1   msaitoh 		}
    371   1.1   msaitoh 
    372   1.1   msaitoh 		/* Validate MCFG memory range */
    373   1.1   msaitoh 		bus_end = ama->EndBusNumber;
    374   1.1   msaitoh 		if (mcfg_ops->ao_validate != NULL &&
    375   1.1   msaitoh 		    !mcfg_ops->ao_validate(ama->Address, ama->StartBusNumber,
    376   1.1   msaitoh 		      &bus_end)) {
    377   1.1   msaitoh 			if (!acpimcfg_find_system_resource( ama->Address,
    378   1.1   msaitoh 			    ama->StartBusNumber, &bus_end)) {
    379   1.1   msaitoh 				aprint_debug_dev(acpi_sc->sc_dev,
    380   1.1   msaitoh 				    "MCFG: segment %d, bus %d-%d, "
    381   1.1   msaitoh 				    "address 0x%016" PRIx64
    382   1.1   msaitoh 				    ": ignore (invalid address)\n",
    383   1.1   msaitoh 				    ama->PciSegment,
    384   1.1   msaitoh 				    ama->StartBusNumber, ama->EndBusNumber,
    385   1.1   msaitoh 				    ama->Address);
    386   1.1   msaitoh 				goto next;
    387   1.1   msaitoh 			}
    388   1.1   msaitoh 		}
    389   1.1   msaitoh 		if (ama->EndBusNumber != bus_end) {
    390   1.1   msaitoh 			aprint_debug_dev(acpi_sc->sc_dev,
    391   1.1   msaitoh 			    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64
    392   1.1   msaitoh 			    " -> bus %d-%d\n", ama->PciSegment,
    393   1.1   msaitoh 			    ama->StartBusNumber, ama->EndBusNumber,
    394   1.1   msaitoh 			    ama->Address, ama->StartBusNumber, bus_end);
    395   1.1   msaitoh 		}
    396   1.1   msaitoh 
    397  1.13  jmcneill #ifndef __HAVE_PCI_GET_SEGMENT
    398   1.1   msaitoh 		if (ama->PciSegment != 0) {
    399   1.1   msaitoh 			aprint_debug_dev(acpi_sc->sc_dev,
    400   1.1   msaitoh 			    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64
    401   1.1   msaitoh 			    ": ignore (non PCI segment 0)\n", ama->PciSegment,
    402   1.1   msaitoh 			    ama->StartBusNumber, bus_end, ama->Address);
    403   1.1   msaitoh 			goto next;
    404   1.1   msaitoh 		}
    405  1.13  jmcneill #endif
    406   1.1   msaitoh 
    407   1.1   msaitoh 		seg = &mcfg_segs[nsegs++];
    408   1.1   msaitoh 		seg->ms_address = ama->Address;
    409   1.1   msaitoh 		seg->ms_segment = ama->PciSegment;
    410   1.1   msaitoh 		seg->ms_bus_start = ama->StartBusNumber;
    411   1.1   msaitoh 		seg->ms_bus_end = bus_end;
    412   1.1   msaitoh 		seg->ms_bst = memt;
    413   1.1   msaitoh 		n = seg->ms_bus_end - seg->ms_bus_start + 1;
    414   1.1   msaitoh 		seg->ms_bus = kmem_zalloc(sizeof(*seg->ms_bus) * n, KM_SLEEP);
    415   1.1   msaitoh 
    416   1.1   msaitoh  next:
    417   1.1   msaitoh 		offset += sizeof(ACPI_MCFG_ALLOCATION);
    418  1.11  jmcneill 		ama = ACPI_ADD_PTR(ACPI_MCFG_ALLOCATION, mcfg, offset);
    419   1.1   msaitoh 	}
    420   1.1   msaitoh 	if (nsegs == 0)
    421   1.1   msaitoh 		return ENOENT;
    422   1.1   msaitoh 
    423   1.1   msaitoh 	for (i = 0; i < nsegs; i++) {
    424   1.1   msaitoh 		seg = &mcfg_segs[i];
    425   1.1   msaitoh 		aprint_verbose_dev(acpi_sc->sc_dev,
    426   1.1   msaitoh 		    "MCFG: segment %d, bus %d-%d, address 0x%016" PRIx64 "\n",
    427   1.1   msaitoh 		    seg->ms_segment, seg->ms_bus_start, seg->ms_bus_end,
    428   1.1   msaitoh 		    seg->ms_address);
    429   1.1   msaitoh 	}
    430   1.1   msaitoh 
    431   1.1   msaitoh 	/* Update # of segment */
    432   1.1   msaitoh 	mcfg_nsegs = nsegs;
    433   1.1   msaitoh 	mcfg_inited = true;
    434   1.1   msaitoh 
    435   1.1   msaitoh 	return 0;
    436   1.1   msaitoh }
    437   1.1   msaitoh 
    438   1.1   msaitoh static int
    439   1.1   msaitoh acpimcfg_ext_conf_is_aliased(pci_chipset_tag_t pc, pcitag_t tag)
    440   1.1   msaitoh {
    441   1.1   msaitoh 	pcireg_t id;
    442   1.1   msaitoh 	int i;
    443   1.1   msaitoh 
    444   1.1   msaitoh 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    445   1.1   msaitoh 	for (i = PCI_CONF_SIZE; i < PCI_EXTCONF_SIZE; i += PCI_CONF_SIZE) {
    446   1.1   msaitoh 		if (pci_conf_read(pc, tag, i) != id)
    447   1.1   msaitoh 			return false;
    448   1.1   msaitoh 	}
    449   1.1   msaitoh 	return true;
    450   1.1   msaitoh }
    451   1.1   msaitoh 
    452   1.1   msaitoh static struct mcfg_segment *
    453  1.12  jmcneill acpimcfg_get_segment(pci_chipset_tag_t pc, int bus)
    454   1.1   msaitoh {
    455   1.1   msaitoh 	struct mcfg_segment *seg;
    456  1.12  jmcneill 	u_int segment;
    457   1.1   msaitoh 	int i;
    458   1.1   msaitoh 
    459  1.12  jmcneill #ifdef __HAVE_PCI_GET_SEGMENT
    460  1.12  jmcneill 	segment = pci_get_segment(pc);
    461  1.12  jmcneill #else
    462  1.12  jmcneill 	segment = 0;
    463  1.12  jmcneill #endif
    464  1.12  jmcneill 
    465   1.1   msaitoh 	for (i = 0; i < mcfg_nsegs; i++) {
    466   1.1   msaitoh 		seg = &mcfg_segs[i];
    467  1.12  jmcneill 		if (segment == seg->ms_segment &&
    468  1.12  jmcneill 		    bus >= seg->ms_bus_start && bus <= seg->ms_bus_end)
    469   1.1   msaitoh 			return seg;
    470   1.1   msaitoh 	}
    471   1.1   msaitoh 	return NULL;
    472   1.1   msaitoh }
    473   1.1   msaitoh 
    474   1.1   msaitoh static int
    475   1.1   msaitoh acpimcfg_device_probe(const struct pci_attach_args *pa)
    476   1.1   msaitoh {
    477   1.1   msaitoh 	pci_chipset_tag_t pc = pa->pa_pc;
    478   1.1   msaitoh 	struct mcfg_segment *seg;
    479   1.1   msaitoh 	struct mcfg_bus *mb;
    480   1.1   msaitoh 	pcitag_t tag;
    481   1.1   msaitoh 	pcireg_t reg;
    482   1.1   msaitoh 	int bus = pa->pa_bus;
    483   1.1   msaitoh 	int dev = pa->pa_device;
    484   1.1   msaitoh 	int func = pa->pa_function;
    485   1.1   msaitoh 	int last_dev, last_func, end_func;
    486   1.1   msaitoh 	int alias = 0;
    487   1.5   msaitoh 	const struct pci_quirkdata *qd;
    488   1.5   msaitoh 	bool force_hasextcnf = false;
    489   1.5   msaitoh 	bool force_noextcnf = false;
    490   1.1   msaitoh 	int i, j;
    491   1.1   msaitoh 
    492  1.12  jmcneill 	seg = acpimcfg_get_segment(pc, bus);
    493   1.1   msaitoh 	if (seg == NULL)
    494   1.1   msaitoh 		return 0;
    495   1.1   msaitoh 
    496   1.1   msaitoh 	mb = &seg->ms_bus[bus - seg->ms_bus_start];
    497   1.1   msaitoh 	tag = pci_make_tag(pc, bus, dev, func);
    498   1.1   msaitoh 
    499   1.1   msaitoh 	/* Mark invalid between last probed device to probed device. */
    500   1.1   msaitoh 	pci_decompose_tag(pc, mb->last_probed, NULL, &last_dev, &last_func);
    501   1.1   msaitoh 	if (dev != 0 || func != 0) {
    502   1.1   msaitoh 		for (i = last_dev; i <= dev; i++) {
    503   1.1   msaitoh 			end_func = (i == dev) ? func : 8;
    504   1.1   msaitoh 			for (j = last_func; j < end_func; j++) {
    505   1.1   msaitoh 				if (i == last_dev && j == last_func)
    506   1.1   msaitoh 					continue;
    507  1.10  jmcneill 				PCIDEV_SET_INVALID(mb, i, j);
    508   1.1   msaitoh 			}
    509   1.1   msaitoh 			last_func = 0;
    510   1.1   msaitoh 		}
    511   1.1   msaitoh 	}
    512   1.1   msaitoh 	mb->last_probed = tag;
    513   1.1   msaitoh 
    514   1.5   msaitoh 	reg = pci_conf_read(pc, tag, PCI_ID_REG);
    515   1.5   msaitoh 	qd = pci_lookup_quirkdata(PCI_VENDOR(reg), PCI_PRODUCT(reg));
    516   1.5   msaitoh 	if (qd != NULL && (qd->quirks & PCI_QUIRK_HASEXTCNF) != 0)
    517   1.5   msaitoh 		force_hasextcnf = true;
    518   1.5   msaitoh 	if (qd != NULL && (qd->quirks & PCI_QUIRK_NOEXTCNF) != 0)
    519   1.5   msaitoh 		force_noextcnf = true;
    520   1.5   msaitoh 
    521   1.1   msaitoh 	/* Probe extended configuration space. */
    522   1.5   msaitoh 	if ((!force_hasextcnf) && ((force_noextcnf) ||
    523   1.5   msaitoh 		((reg = pci_conf_read(pc, tag, PCI_CONF_SIZE)) == (pcireg_t)-1)
    524   1.5   msaitoh 		|| (reg == 0)
    525   1.5   msaitoh 		|| (alias = acpimcfg_ext_conf_is_aliased(pc, tag)))) {
    526   1.1   msaitoh 		aprint_debug_dev(acpi_sc->sc_dev,
    527   1.1   msaitoh 		    "MCFG: %03d:%02d:%d: invalid config space "
    528   1.1   msaitoh 		    "(cfg[0x%03x]=0x%08x, alias=%s)\n", bus, dev, func,
    529   1.1   msaitoh 		    PCI_CONF_SIZE, reg, alias ? "true" : "false");
    530   1.1   msaitoh 		EXTCONF_SET_INVALID(mb, dev, func);
    531   1.1   msaitoh 	}
    532   1.1   msaitoh 
    533  1.10  jmcneill 	aprint_debug_dev(acpi_sc->sc_dev,
    534  1.10  jmcneill 	    "MCFG: %03d:%02d:%d: Ok (cfg[0x%03x]=0x%08x extconf=%c)\n",
    535  1.10  jmcneill 	    bus, dev, func, PCI_CONF_SIZE, reg,
    536  1.10  jmcneill 	    EXTCONF_IS_VALID(mb, dev, func) ? 'Y' : 'N');
    537  1.10  jmcneill 	mb->valid_ndevs++;
    538  1.10  jmcneill 
    539   1.1   msaitoh 	return 0;
    540   1.1   msaitoh }
    541   1.1   msaitoh 
    542  1.14    cherry #ifdef PCI_MACHDEP_ENUMERATE_BUS
    543  1.14    cherry #define pci_enumerate_bus PCI_MACHDEP_ENUMERATE_BUS
    544  1.14    cherry #endif
    545  1.14    cherry 
    546   1.1   msaitoh static void
    547   1.1   msaitoh acpimcfg_scan_bus(struct pci_softc *sc, pci_chipset_tag_t pc, int bus)
    548   1.1   msaitoh {
    549   1.1   msaitoh 	static const int wildcard[PCICF_NLOCS] = {
    550   1.1   msaitoh 		PCICF_DEV_DEFAULT, PCICF_FUNCTION_DEFAULT
    551   1.1   msaitoh 	};
    552   1.1   msaitoh 
    553   1.1   msaitoh 	sc->sc_bus = bus;	/* XXX */
    554   1.7  jmcneill 	sc->sc_pc = pc;
    555   1.1   msaitoh 
    556   1.1   msaitoh 	pci_enumerate_bus(sc, wildcard, acpimcfg_device_probe, NULL);
    557   1.1   msaitoh }
    558   1.1   msaitoh 
    559   1.1   msaitoh int
    560   1.1   msaitoh acpimcfg_map_bus(device_t self, pci_chipset_tag_t pc, int bus)
    561   1.1   msaitoh {
    562   1.1   msaitoh 	struct pci_softc *sc = device_private(self);
    563   1.1   msaitoh 	struct mcfg_segment *seg = NULL;
    564   1.1   msaitoh 	struct mcfg_bus *mb;
    565   1.1   msaitoh 	bus_space_handle_t bsh;
    566   1.1   msaitoh 	bus_addr_t baddr;
    567   1.3   hannken 	pcitag_t tag;
    568   1.3   hannken 	pcireg_t reg;
    569   1.3   hannken 	bool is_e7520_mch;
    570   1.1   msaitoh 	int boff;
    571   1.1   msaitoh 	int last_dev, last_func;
    572   1.1   msaitoh 	int i, j;
    573   1.1   msaitoh 	int error;
    574   1.1   msaitoh 
    575   1.1   msaitoh 	if (!mcfg_inited)
    576   1.1   msaitoh 		return ENXIO;
    577   1.1   msaitoh 
    578  1.12  jmcneill 	seg = acpimcfg_get_segment(pc, bus);
    579   1.1   msaitoh 	if (seg == NULL)
    580   1.1   msaitoh 		return ENOENT;
    581   1.1   msaitoh 
    582   1.1   msaitoh 	boff = bus - seg->ms_bus_start;
    583   1.1   msaitoh 	if (seg->ms_bus[boff].valid_ndevs > 0)
    584   1.1   msaitoh 		return 0;
    585   1.1   msaitoh 
    586   1.1   msaitoh 	mb = &seg->ms_bus[boff];
    587   1.1   msaitoh 	baddr = seg->ms_address + (boff * ACPIMCFG_SIZE_PER_BUS);
    588   1.1   msaitoh 
    589   1.1   msaitoh 	/* Map extended configration space of all dev/func. */
    590   1.1   msaitoh 	error = bus_space_map(seg->ms_bst, baddr, ACPIMCFG_SIZE_PER_BUS, 0,
    591   1.1   msaitoh 	    &bsh);
    592   1.1   msaitoh 	if (error != 0)
    593   1.1   msaitoh 		return error;
    594   1.1   msaitoh 	for (i = 0; i < 32; i++) {
    595   1.1   msaitoh 		for (j = 0; j < 8; j++) {
    596   1.1   msaitoh 			error = bus_space_subregion(seg->ms_bst, bsh,
    597   1.1   msaitoh 			    EXTCONF_OFFSET(i, j, 0), PCI_EXTCONF_SIZE,
    598   1.1   msaitoh 			    &mb->bsh[i][j]);
    599   1.1   msaitoh 			if (error != 0)
    600   1.1   msaitoh 				break;
    601   1.1   msaitoh 		}
    602   1.1   msaitoh 	}
    603   1.1   msaitoh 	if (error != 0)
    604   1.1   msaitoh 		return error;
    605   1.1   msaitoh 
    606   1.1   msaitoh 	aprint_debug("\n");
    607   1.1   msaitoh 
    608   1.1   msaitoh 	/* Probe extended configuration space of all devices. */
    609   1.1   msaitoh 	memset(mb->valid_devs, 0xff, sizeof(mb->valid_devs));
    610  1.10  jmcneill 	memset(mb->valid_extconf, 0xff, sizeof(mb->valid_extconf));
    611   1.1   msaitoh 	mb->valid_ndevs = 0;
    612   1.1   msaitoh 	mb->last_probed = pci_make_tag(pc, bus, 0, 0);
    613   1.1   msaitoh 
    614   1.3   hannken 	/*
    615   1.3   hannken 	 * On an Intel E7520 we have to temporarily disable
    616   1.3   hannken 	 * Enhanced Config Access error detection and reporting
    617   1.3   hannken 	 * by setting the appropriate error mask in HI_ERRMASK register.
    618   1.3   hannken 	 *
    619   1.3   hannken 	 * See "Intel E7520 Memory Controller Hub (MCH) Datasheet",
    620   1.3   hannken 	 * Document 303006-002, pg. 82
    621   1.3   hannken 	 */
    622   1.3   hannken 	tag = pci_make_tag(pc, 0, 0, 1);
    623   1.3   hannken 	reg = pci_conf_read(pc, tag, PCI_ID_REG);
    624   1.3   hannken 	is_e7520_mch = (reg ==
    625   1.3   hannken 	    PCI_ID_CODE(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_E7525_MCHER));
    626   1.3   hannken 	if (is_e7520_mch) {
    627   1.3   hannken 		reg = pci_conf_read(pc, tag, 0x54);
    628   1.3   hannken 		pci_conf_write(pc, tag, 0x54, reg | 0x20);
    629   1.3   hannken 	}
    630   1.3   hannken 
    631   1.1   msaitoh 	acpimcfg_scan_bus(sc, pc, bus);
    632   1.1   msaitoh 
    633   1.3   hannken 	if (is_e7520_mch) {
    634   1.3   hannken 		pci_conf_write(pc, tag, 0x54, reg);
    635   1.3   hannken 	}
    636   1.3   hannken 
    637  1.10  jmcneill 	/* Unmap configuration space of all dev/func. */
    638   1.1   msaitoh 	bus_space_unmap(seg->ms_bst, bsh, ACPIMCFG_SIZE_PER_BUS);
    639   1.1   msaitoh 	memset(mb->bsh, 0, sizeof(mb->bsh));
    640   1.1   msaitoh 
    641   1.1   msaitoh 	if (mb->valid_ndevs == 0) {
    642   1.1   msaitoh 		aprint_debug_dev(acpi_sc->sc_dev,
    643   1.1   msaitoh 		    "MCFG: bus %d: no valid devices.\n", bus);
    644   1.1   msaitoh 		memset(mb->valid_devs, 0, sizeof(mb->valid_devs));
    645   1.1   msaitoh 		goto out;
    646   1.1   msaitoh 	}
    647   1.1   msaitoh 
    648   1.1   msaitoh 	/* Mark invalid on remaining all devices. */
    649   1.1   msaitoh 	pci_decompose_tag(pc, mb->last_probed, NULL, &last_dev, &last_func);
    650   1.1   msaitoh 	for (i = last_dev; i < 32; i++) {
    651   1.1   msaitoh 		for (j = last_func; j < 8; j++) {
    652   1.1   msaitoh 			if (i == last_dev && j == last_func) {
    653   1.1   msaitoh 				/* Don't mark invalid to last probed device. */
    654   1.1   msaitoh 				continue;
    655   1.1   msaitoh 			}
    656  1.10  jmcneill 			PCIDEV_SET_INVALID(mb, i, j);
    657   1.1   msaitoh 		}
    658   1.1   msaitoh 		last_func = 0;
    659   1.1   msaitoh 	}
    660   1.1   msaitoh 
    661  1.10  jmcneill 	/* Map configuration space per dev/func. */
    662   1.1   msaitoh 	for (i = 0; i < 32; i++) {
    663   1.1   msaitoh 		for (j = 0; j < 8; j++) {
    664  1.10  jmcneill 			if (!PCIDEV_IS_VALID(mb, i, j))
    665   1.1   msaitoh 				continue;
    666   1.1   msaitoh 			error = bus_space_map(seg->ms_bst,
    667   1.1   msaitoh 			    baddr + EXTCONF_OFFSET(i, j, 0), PCI_EXTCONF_SIZE,
    668   1.1   msaitoh 			    0, &mb->bsh[i][j]);
    669   1.1   msaitoh 			if (error != 0) {
    670   1.1   msaitoh 				/* Unmap all handles when map failed. */
    671   1.1   msaitoh 				do {
    672   1.1   msaitoh 					while (--j >= 0) {
    673  1.10  jmcneill 						if (!PCIDEV_IS_VALID(mb, i, j))
    674   1.1   msaitoh 							continue;
    675   1.1   msaitoh 						bus_space_unmap(seg->ms_bst,
    676   1.1   msaitoh 						    mb->bsh[i][j],
    677   1.1   msaitoh 						    PCI_EXTCONF_SIZE);
    678   1.1   msaitoh 					}
    679   1.1   msaitoh 					j = 8;
    680   1.1   msaitoh 				} while (--i >= 0);
    681   1.1   msaitoh 				memset(mb->valid_devs, 0,
    682   1.1   msaitoh 				    sizeof(mb->valid_devs));
    683   1.1   msaitoh 				goto out;
    684   1.1   msaitoh 			}
    685   1.1   msaitoh 		}
    686   1.1   msaitoh 	}
    687   1.1   msaitoh 
    688   1.1   msaitoh 	aprint_debug_dev(acpi_sc->sc_dev, "MCFG: bus %d: valid devices\n", bus);
    689   1.1   msaitoh 	for (i = 0; i < 32; i++) {
    690   1.1   msaitoh 		for (j = 0; j < 8; j++) {
    691  1.10  jmcneill 			if (PCIDEV_IS_VALID(mb, i, j)) {
    692   1.1   msaitoh 				aprint_debug_dev(acpi_sc->sc_dev,
    693   1.1   msaitoh 				    "MCFG: %03d:%02d:%d\n", bus, i, j);
    694   1.1   msaitoh 			}
    695   1.1   msaitoh 		}
    696   1.1   msaitoh 	}
    697   1.1   msaitoh 
    698   1.1   msaitoh 	error = 0;
    699   1.1   msaitoh out:
    700   1.1   msaitoh 	aprint_debug_dev(acpi_sc->sc_dev, "%s done", __func__);
    701   1.1   msaitoh 
    702   1.1   msaitoh 	return error;
    703   1.1   msaitoh }
    704   1.1   msaitoh 
    705   1.8  jmcneill #ifdef PCI_NETBSD_CONFIGURE
    706   1.8  jmcneill struct acpimcfg_resource {
    707   1.8  jmcneill 	struct extent *ioext;
    708   1.8  jmcneill 	struct extent *memext;
    709   1.8  jmcneill 	struct extent *pmemext;
    710   1.8  jmcneill };
    711   1.8  jmcneill 
    712   1.8  jmcneill static ACPI_STATUS
    713   1.8  jmcneill acpimcfg_configure_bus_cb(ACPI_RESOURCE *res, void *ctx)
    714   1.8  jmcneill {
    715   1.8  jmcneill 	struct acpimcfg_resource *pcires = ctx;
    716   1.8  jmcneill 	struct extent *ex;
    717   1.8  jmcneill 	const char *s;
    718   1.8  jmcneill 	int error;
    719   1.8  jmcneill 
    720  1.15  jmcneill 	if (res->Type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
    721  1.15  jmcneill 	    res->Type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
    722   1.8  jmcneill 	    res->Type != ACPI_RESOURCE_TYPE_ADDRESS64)
    723   1.8  jmcneill 		return AE_OK;
    724   1.8  jmcneill 
    725   1.8  jmcneill 	if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
    726   1.8  jmcneill 		return AE_OK;
    727   1.8  jmcneill 
    728   1.8  jmcneill 	if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE &&
    729   1.8  jmcneill 	    res->Data.Address.ResourceType != ACPI_IO_RANGE)
    730   1.8  jmcneill 		return AE_OK;
    731   1.8  jmcneill 
    732   1.8  jmcneill 	if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE &&
    733   1.8  jmcneill 	    res->Data.Address.Info.Mem.Caching == ACPI_PREFETCHABLE_MEMORY) {
    734   1.8  jmcneill 		if (pcires->pmemext == NULL) {
    735   1.8  jmcneill 			pcires->pmemext = extent_create("pcipmem", 0, ULONG_MAX,
    736   1.8  jmcneill 			    NULL, 0, EX_WAITOK);
    737   1.8  jmcneill 			error = extent_alloc_region(pcires->pmemext, 0, ULONG_MAX,
    738   1.8  jmcneill 			    EX_WAITOK);
    739   1.8  jmcneill 			if (error) {
    740   1.8  jmcneill 				extent_destroy(pcires->pmemext);
    741   1.8  jmcneill 				pcires->pmemext = NULL;
    742   1.8  jmcneill 				return AE_NO_MEMORY;
    743   1.8  jmcneill 			}
    744   1.8  jmcneill 		}
    745   1.8  jmcneill 		ex = pcires->pmemext;
    746   1.8  jmcneill 		s = "prefetchable";
    747   1.8  jmcneill 	} else if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE &&
    748   1.8  jmcneill 	    res->Data.Address.Info.Mem.Caching != ACPI_PREFETCHABLE_MEMORY) {
    749   1.8  jmcneill 		if (pcires->memext == NULL) {
    750   1.8  jmcneill 			pcires->memext = extent_create("pcimem", 0, ULONG_MAX,
    751   1.8  jmcneill 			    NULL, 0, EX_WAITOK);
    752   1.8  jmcneill 			error = extent_alloc_region(pcires->memext, 0, ULONG_MAX,
    753   1.8  jmcneill 			    EX_WAITOK);
    754   1.8  jmcneill 			if (error) {
    755   1.8  jmcneill 				extent_destroy(pcires->memext);
    756   1.8  jmcneill 				pcires->memext = NULL;
    757   1.8  jmcneill 				return AE_NO_MEMORY;
    758   1.8  jmcneill 			}
    759   1.8  jmcneill 		}
    760   1.8  jmcneill 		ex = pcires->memext;
    761   1.8  jmcneill 		s = "non-prefetchable";
    762   1.8  jmcneill 	} else if (res->Data.Address.ResourceType == ACPI_IO_RANGE) {
    763   1.8  jmcneill 		if (pcires->ioext == NULL) {
    764   1.8  jmcneill 			pcires->ioext = extent_create("pciio", 0, ULONG_MAX,
    765   1.8  jmcneill 			    NULL, 0, EX_WAITOK);
    766   1.8  jmcneill 			error = extent_alloc_region(pcires->ioext, 0, ULONG_MAX,
    767   1.8  jmcneill 			    EX_WAITOK);
    768   1.8  jmcneill 			if (error) {
    769   1.8  jmcneill 				extent_destroy(pcires->ioext);
    770   1.8  jmcneill 				pcires->ioext = NULL;
    771   1.8  jmcneill 				return AE_NO_MEMORY;
    772   1.8  jmcneill 			}
    773   1.8  jmcneill 		}
    774   1.8  jmcneill 		ex = pcires->ioext;
    775   1.8  jmcneill 		s = "i/o";
    776   1.8  jmcneill 
    777   1.8  jmcneill 	}
    778   1.8  jmcneill 
    779   1.8  jmcneill 	switch (res->Type) {
    780   1.8  jmcneill 	case ACPI_RESOURCE_TYPE_ADDRESS16:
    781   1.8  jmcneill 		aprint_debug(
    782   1.8  jmcneill 		    "MCFG: range 0x%04" PRIx16 " size %#" PRIx16 " (16-bit %s)\n",
    783   1.8  jmcneill 		    res->Data.Address16.Address.Minimum,
    784   1.8  jmcneill 		    res->Data.Address16.Address.AddressLength,
    785   1.8  jmcneill 		    s);
    786   1.8  jmcneill 		error = extent_free(ex, res->Data.Address16.Address.Minimum,
    787   1.8  jmcneill 		    res->Data.Address16.Address.AddressLength, EX_WAITOK);
    788   1.8  jmcneill 		if (error)
    789   1.8  jmcneill 			return AE_NO_MEMORY;
    790   1.8  jmcneill 		break;
    791   1.8  jmcneill 	case ACPI_RESOURCE_TYPE_ADDRESS32:
    792   1.8  jmcneill 		aprint_debug(
    793   1.8  jmcneill 		    "MCFG: range 0x%08" PRIx32 " size %#" PRIx32 " (32-bit %s)\n",
    794   1.8  jmcneill 		    res->Data.Address32.Address.Minimum,
    795   1.8  jmcneill 		    res->Data.Address32.Address.AddressLength,
    796   1.8  jmcneill 		    s);
    797   1.8  jmcneill 		error = extent_free(ex, res->Data.Address32.Address.Minimum,
    798   1.8  jmcneill 		    res->Data.Address32.Address.AddressLength, EX_WAITOK);
    799   1.8  jmcneill 		if (error)
    800   1.8  jmcneill 			return AE_NO_MEMORY;
    801   1.8  jmcneill 		break;
    802   1.8  jmcneill 	case ACPI_RESOURCE_TYPE_ADDRESS64:
    803   1.8  jmcneill 		aprint_debug(
    804   1.8  jmcneill 		    "MCFG: range 0x%016" PRIx64 " size %#" PRIx64 " (64-bit %s)\n",
    805   1.8  jmcneill 		    res->Data.Address64.Address.Minimum,
    806   1.8  jmcneill 		    res->Data.Address64.Address.AddressLength,
    807   1.8  jmcneill 		    s);
    808   1.8  jmcneill 		error = extent_free(ex, res->Data.Address64.Address.Minimum,
    809   1.8  jmcneill 		    res->Data.Address64.Address.AddressLength, EX_WAITOK);
    810   1.8  jmcneill 		if (error)
    811   1.8  jmcneill 			return AE_NO_MEMORY;
    812   1.8  jmcneill 		break;
    813   1.8  jmcneill 	}
    814   1.8  jmcneill 
    815   1.8  jmcneill 	return AE_OK;
    816   1.8  jmcneill }
    817   1.8  jmcneill 
    818   1.8  jmcneill int
    819   1.8  jmcneill acpimcfg_configure_bus(device_t self, pci_chipset_tag_t pc, ACPI_HANDLE handle,
    820   1.8  jmcneill     int bus, int cacheline_size)
    821   1.8  jmcneill {
    822   1.8  jmcneill 	struct acpimcfg_resource res;
    823   1.8  jmcneill 	struct mcfg_segment *seg;
    824   1.8  jmcneill 	struct mcfg_bus *mb;
    825   1.8  jmcneill 	bus_space_handle_t bsh[256];
    826   1.8  jmcneill 	bool bsh_mapped[256];
    827   1.8  jmcneill 	int error, boff, b, d, f;
    828   1.8  jmcneill 	bus_addr_t baddr;
    829   1.8  jmcneill 	ACPI_STATUS rv;
    830   1.8  jmcneill 
    831  1.12  jmcneill 	seg = acpimcfg_get_segment(pc, bus);
    832   1.8  jmcneill 	if (seg == NULL)
    833   1.8  jmcneill 		return ENOENT;
    834   1.8  jmcneill 
    835   1.8  jmcneill 	/*
    836   1.8  jmcneill 	 * Map config space for all possible busses and mark them valid during
    837   1.8  jmcneill 	 * configuration so pci_configure_bus can access them through our chipset
    838   1.8  jmcneill 	 * tag with acpimcfg_conf_read/write below.
    839   1.8  jmcneill 	 */
    840   1.8  jmcneill 	memset(bsh_mapped, 0, sizeof(bsh_mapped));
    841   1.8  jmcneill 	for (b = seg->ms_bus_start; b <= seg->ms_bus_end; b++) {
    842   1.8  jmcneill 		boff = b - seg->ms_bus_start;
    843   1.8  jmcneill 		mb = &seg->ms_bus[boff];
    844   1.8  jmcneill 		baddr = seg->ms_address + (boff * ACPIMCFG_SIZE_PER_BUS);
    845   1.8  jmcneill 
    846   1.8  jmcneill 		/* Map extended configration space of all dev/func. */
    847   1.8  jmcneill 		error = bus_space_map(seg->ms_bst, baddr, ACPIMCFG_SIZE_PER_BUS, 0,
    848   1.8  jmcneill 		    &bsh[b]);
    849   1.8  jmcneill 		if (error != 0)
    850   1.8  jmcneill 			goto cleanup;
    851   1.8  jmcneill 		bsh_mapped[b] = true;
    852   1.8  jmcneill 		for (d = 0; d < 32; d++) {
    853   1.8  jmcneill 			for (f = 0; f < 8; f++) {
    854   1.8  jmcneill 				error = bus_space_subregion(seg->ms_bst, bsh[b],
    855   1.8  jmcneill 				    EXTCONF_OFFSET(d, f, 0), PCI_EXTCONF_SIZE,
    856   1.8  jmcneill 				    &mb->bsh[d][f]);
    857   1.8  jmcneill 				if (error != 0)
    858   1.8  jmcneill 					break;
    859   1.8  jmcneill 			}
    860   1.8  jmcneill 		}
    861   1.8  jmcneill 		if (error != 0)
    862   1.8  jmcneill 			goto cleanup;
    863   1.8  jmcneill 
    864   1.8  jmcneill 		memset(mb->valid_devs, 0xff, sizeof(mb->valid_devs));
    865   1.8  jmcneill 	}
    866   1.8  jmcneill 
    867   1.8  jmcneill 	memset(&res, 0, sizeof(res));
    868   1.8  jmcneill 	rv = AcpiWalkResources(handle, "_CRS", acpimcfg_configure_bus_cb, &res);
    869   1.8  jmcneill 	if (ACPI_FAILURE(rv)) {
    870   1.8  jmcneill 		error = ENXIO;
    871   1.8  jmcneill 		goto cleanup;
    872   1.8  jmcneill 	}
    873   1.8  jmcneill 
    874   1.8  jmcneill 	error = pci_configure_bus(pc, res.ioext, res.memext, res.pmemext, bus,
    875   1.8  jmcneill 	    cacheline_size);
    876   1.8  jmcneill 
    877   1.8  jmcneill cleanup:
    878   1.8  jmcneill 	/*
    879   1.8  jmcneill 	 * Unmap config space for the segment's busses. Valid devices will be
    880   1.8  jmcneill 	 * re-mapped later on by acpimcfg_map_bus.
    881   1.8  jmcneill 	 */
    882   1.8  jmcneill 	for (b = seg->ms_bus_start; b <= seg->ms_bus_end; b++) {
    883   1.8  jmcneill 		boff = b - seg->ms_bus_start;
    884   1.8  jmcneill 		mb = &seg->ms_bus[boff];
    885   1.8  jmcneill 		memset(mb->valid_devs, 0, sizeof(mb->valid_devs));
    886   1.8  jmcneill 
    887   1.8  jmcneill 		if (bsh_mapped[b])
    888   1.8  jmcneill 			bus_space_unmap(seg->ms_bst, bsh[b], ACPIMCFG_SIZE_PER_BUS);
    889   1.8  jmcneill 	}
    890   1.8  jmcneill 
    891   1.8  jmcneill 	if (res.ioext)
    892   1.8  jmcneill 		extent_destroy(res.ioext);
    893   1.8  jmcneill 	if (res.memext)
    894   1.8  jmcneill 		extent_destroy(res.memext);
    895   1.8  jmcneill 	if (res.pmemext)
    896   1.8  jmcneill 		extent_destroy(res.pmemext);
    897   1.8  jmcneill 
    898   1.8  jmcneill 	return error;
    899   1.8  jmcneill }
    900   1.8  jmcneill #else
    901   1.8  jmcneill int
    902   1.8  jmcneill acpimcfg_configure_bus(device_t self, pci_chipset_tag_t pc, ACPI_HANDLE handle,
    903   1.9  jmcneill     int bus, int cacheline_size)
    904   1.8  jmcneill {
    905   1.8  jmcneill 	return ENXIO;
    906   1.8  jmcneill }
    907   1.8  jmcneill #endif
    908   1.8  jmcneill 
    909   1.1   msaitoh int
    910   1.1   msaitoh acpimcfg_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
    911   1.1   msaitoh {
    912   1.1   msaitoh 	struct mcfg_segment *seg = NULL;
    913   1.1   msaitoh 	struct mcfg_bus *mb;
    914   1.1   msaitoh 	int bus, dev, func;
    915   1.1   msaitoh 
    916   1.1   msaitoh 	KASSERT(reg < PCI_EXTCONF_SIZE);
    917   1.1   msaitoh 	KASSERT((reg & 3) == 0);
    918   1.1   msaitoh 
    919   1.1   msaitoh 	if (!mcfg_inited) {
    920   1.1   msaitoh 		*data = -1;
    921   1.1   msaitoh 		return ENXIO;
    922   1.1   msaitoh 	}
    923   1.1   msaitoh 
    924   1.1   msaitoh 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    925   1.1   msaitoh 
    926  1.12  jmcneill 	seg = acpimcfg_get_segment(pc, bus);
    927   1.1   msaitoh 	if (seg == NULL) {
    928   1.1   msaitoh 		*data = -1;
    929   1.1   msaitoh 		return ERANGE;
    930   1.1   msaitoh 	}
    931   1.1   msaitoh 
    932   1.1   msaitoh 	mb = &seg->ms_bus[bus - seg->ms_bus_start];
    933  1.10  jmcneill 	if (!PCIDEV_IS_VALID(mb, dev, func)) {
    934  1.10  jmcneill 		*data = -1;
    935  1.10  jmcneill 		return EINVAL;
    936  1.10  jmcneill 	}
    937  1.10  jmcneill 	if (!EXTCONF_IS_VALID(mb, dev, func) && reg >= PCI_CONF_SIZE) {
    938   1.1   msaitoh 		*data = -1;
    939   1.1   msaitoh 		return EINVAL;
    940   1.1   msaitoh 	}
    941   1.1   msaitoh 
    942   1.1   msaitoh 	*data = mcfg_ops->ao_read(seg->ms_bst, mb->bsh[dev][func], reg);
    943   1.1   msaitoh 	return 0;
    944   1.1   msaitoh }
    945   1.1   msaitoh 
    946   1.1   msaitoh int
    947   1.1   msaitoh acpimcfg_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    948   1.1   msaitoh {
    949   1.1   msaitoh 	struct mcfg_segment *seg = NULL;
    950   1.1   msaitoh 	struct mcfg_bus *mb;
    951   1.1   msaitoh 	int bus, dev, func;
    952   1.1   msaitoh 
    953   1.1   msaitoh 	KASSERT(reg < PCI_EXTCONF_SIZE);
    954   1.1   msaitoh 	KASSERT((reg & 3) == 0);
    955   1.1   msaitoh 
    956   1.1   msaitoh 	if (!mcfg_inited)
    957   1.1   msaitoh 		return ENXIO;
    958   1.1   msaitoh 
    959   1.1   msaitoh 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    960   1.1   msaitoh 
    961  1.12  jmcneill 	seg = acpimcfg_get_segment(pc, bus);
    962   1.1   msaitoh 	if (seg == NULL)
    963   1.1   msaitoh 		return ERANGE;
    964   1.1   msaitoh 
    965   1.1   msaitoh 	mb = &seg->ms_bus[bus - seg->ms_bus_start];
    966  1.10  jmcneill 	if (!PCIDEV_IS_VALID(mb, dev, func))
    967  1.10  jmcneill 		return EINVAL;
    968  1.10  jmcneill 	if (!EXTCONF_IS_VALID(mb, dev, func) && reg >= PCI_CONF_SIZE)
    969   1.1   msaitoh 		return EINVAL;
    970   1.1   msaitoh 
    971   1.1   msaitoh 	mcfg_ops->ao_write(seg->ms_bst, mb->bsh[dev][func], reg, data);
    972   1.1   msaitoh 	return 0;
    973   1.1   msaitoh }
    974