Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.40.2.1
      1  1.40.2.1   thorpej /*	$NetBSD: pci_map.c,v 1.40.2.1 2021/01/03 16:34:58 thorpej Exp $	*/
      2       1.1   mycroft 
      3       1.5   mycroft /*-
      4  1.40.2.1   thorpej  * Copyright (c) 1998, 2000, 2020 The NetBSD Foundation, Inc.
      5       1.5   mycroft  * All rights reserved.
      6       1.5   mycroft  *
      7       1.5   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8       1.7   thorpej  * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
      9       1.1   mycroft  *
     10       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11       1.1   mycroft  * modification, are permitted provided that the following conditions
     12       1.1   mycroft  * are met:
     13       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.1   mycroft  *
     19       1.5   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.5   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.5   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.5   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.5   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.5   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.5   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.5   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.5   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.5   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.5   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   mycroft  */
     31       1.1   mycroft 
     32       1.1   mycroft /*
     33       1.1   mycroft  * PCI device mapping.
     34       1.1   mycroft  */
     35      1.10     lukem 
     36      1.10     lukem #include <sys/cdefs.h>
     37  1.40.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.40.2.1 2021/01/03 16:34:58 thorpej Exp $");
     38       1.1   mycroft 
     39       1.1   mycroft #include <sys/param.h>
     40       1.1   mycroft #include <sys/systm.h>
     41       1.1   mycroft #include <sys/device.h>
     42       1.1   mycroft 
     43       1.1   mycroft #include <dev/pci/pcireg.h>
     44       1.1   mycroft #include <dev/pci/pcivar.h>
     45       1.1   mycroft 
     46      1.36  jakllsch bool pci_mapreg_map_enable_decode = true;
     47      1.35  jakllsch 
     48       1.1   mycroft static int
     49      1.18  christos pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
     50       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
     51       1.1   mycroft {
     52      1.40    bouyer 	pcireg_t address, mask, csr;
     53       1.1   mycroft 	int s;
     54       1.1   mycroft 
     55       1.8   thorpej 	if (reg < PCI_MAPREG_START ||
     56       1.8   thorpej #if 0
     57       1.8   thorpej 	    /*
     58       1.8   thorpej 	     * Can't do this check; some devices have mapping registers
     59       1.8   thorpej 	     * way out in left field.
     60       1.8   thorpej 	     */
     61       1.8   thorpej 	    reg >= PCI_MAPREG_END ||
     62       1.8   thorpej #endif
     63       1.8   thorpej 	    (reg & 3))
     64       1.1   mycroft 		panic("pci_io_find: bad request");
     65       1.1   mycroft 
     66       1.1   mycroft 	/*
     67       1.1   mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
     68       1.1   mycroft 	 *
     69       1.1   mycroft 	 * 1) The builtin software should have already mapped the device in a
     70       1.1   mycroft 	 * reasonable way.
     71       1.1   mycroft 	 *
     72       1.1   mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
     73       1.1   mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
     74       1.1   mycroft 	 * what we get back.
     75       1.1   mycroft 	 */
     76       1.1   mycroft 	s = splhigh();
     77       1.1   mycroft 	address = pci_conf_read(pc, tag, reg);
     78      1.40    bouyer 	/*
     79      1.40    bouyer 	 * Disable decoding via the command register before writing to the
     80      1.40    bouyer 	 * BAR register. Changing the decoding address to all-one is
     81      1.40    bouyer 	 * not a valid address and could have side effects.
     82      1.40    bouyer 	 */
     83      1.40    bouyer 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
     84      1.40    bouyer 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
     85      1.40    bouyer 	    csr & ~PCI_COMMAND_IO_ENABLE) ;
     86       1.1   mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
     87       1.1   mycroft 	mask = pci_conf_read(pc, tag, reg);
     88       1.1   mycroft 	pci_conf_write(pc, tag, reg, address);
     89      1.40    bouyer 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
     90       1.1   mycroft 	splx(s);
     91       1.1   mycroft 
     92       1.1   mycroft 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     93      1.22  jmcneill 		aprint_debug("pci_io_find: expected type i/o, found mem\n");
     94      1.25    dyoung 		return 1;
     95       1.1   mycroft 	}
     96       1.1   mycroft 
     97       1.2   mycroft 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
     98      1.19  macallan 		aprint_debug("pci_io_find: void region\n");
     99      1.25    dyoung 		return 1;
    100       1.2   mycroft 	}
    101       1.2   mycroft 
    102      1.25    dyoung 	if (basep != NULL)
    103       1.1   mycroft 		*basep = PCI_MAPREG_IO_ADDR(address);
    104      1.25    dyoung 	if (sizep != NULL)
    105       1.1   mycroft 		*sizep = PCI_MAPREG_IO_SIZE(mask);
    106      1.25    dyoung 	if (flagsp != NULL)
    107       1.1   mycroft 		*flagsp = 0;
    108       1.1   mycroft 
    109      1.25    dyoung 	return 0;
    110       1.1   mycroft }
    111       1.1   mycroft 
    112       1.1   mycroft static int
    113       1.9   thorpej pci_mem_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    114       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    115       1.1   mycroft {
    116       1.7   thorpej 	pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
    117      1.37   msaitoh 	uint64_t waddress, wmask;
    118      1.15   gdamore 	int s, is64bit, isrom;
    119      1.40    bouyer 	pcireg_t csr;
    120       1.7   thorpej 
    121       1.7   thorpej 	is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
    122      1.15   gdamore 	isrom = (reg == PCI_MAPREG_ROM);
    123       1.1   mycroft 
    124      1.15   gdamore 	if ((!isrom) && (reg < PCI_MAPREG_START ||
    125       1.8   thorpej #if 0
    126       1.8   thorpej 	    /*
    127       1.8   thorpej 	     * Can't do this check; some devices have mapping registers
    128       1.8   thorpej 	     * way out in left field.
    129       1.8   thorpej 	     */
    130       1.8   thorpej 	    reg >= PCI_MAPREG_END ||
    131       1.8   thorpej #endif
    132      1.15   gdamore 	    (reg & 3)))
    133       1.7   thorpej 		panic("pci_mem_find: bad request");
    134       1.7   thorpej 
    135       1.7   thorpej 	if (is64bit && (reg + 4) >= PCI_MAPREG_END)
    136       1.7   thorpej 		panic("pci_mem_find: bad 64-bit request");
    137       1.1   mycroft 
    138       1.1   mycroft 	/*
    139       1.1   mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
    140       1.1   mycroft 	 *
    141       1.1   mycroft 	 * 1) The builtin software should have already mapped the device in a
    142       1.1   mycroft 	 * reasonable way.
    143       1.1   mycroft 	 *
    144       1.1   mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    145       1.1   mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    146      1.30      matt 	 * what we get back.  Only probe the upper BAR of a mem64 BAR if bit 31
    147      1.30      matt 	 * is readonly.
    148       1.1   mycroft 	 */
    149       1.1   mycroft 	s = splhigh();
    150       1.1   mycroft 	address = pci_conf_read(pc, tag, reg);
    151      1.40    bouyer 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    152      1.40    bouyer 	/*
    153      1.40    bouyer 	 * Disable decoding via the command register before writing to the
    154      1.40    bouyer 	 * BAR register. Changing the decoding address to all-one is
    155      1.40    bouyer 	 * not a valid address and could have side effects.
    156      1.40    bouyer 	 */
    157      1.40    bouyer 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    158      1.40    bouyer 	    csr & ~PCI_COMMAND_MEM_ENABLE) ;
    159       1.1   mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
    160       1.1   mycroft 	mask = pci_conf_read(pc, tag, reg);
    161       1.1   mycroft 	pci_conf_write(pc, tag, reg, address);
    162       1.7   thorpej 	if (is64bit) {
    163       1.7   thorpej 		address1 = pci_conf_read(pc, tag, reg + 4);
    164      1.30      matt 		if ((mask & 0x80000000) == 0) {
    165      1.30      matt 			pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    166      1.30      matt 			mask1 = pci_conf_read(pc, tag, reg + 4);
    167      1.30      matt 			pci_conf_write(pc, tag, reg + 4, address1);
    168      1.30      matt 		}
    169       1.7   thorpej 	}
    170      1.40    bouyer 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    171       1.1   mycroft 	splx(s);
    172       1.1   mycroft 
    173      1.15   gdamore 	if (!isrom) {
    174      1.15   gdamore 		/*
    175      1.15   gdamore 		 * roms should have an enable bit instead of a memory
    176      1.15   gdamore 		 * type decoder bit.  For normal BARs, make sure that
    177      1.15   gdamore 		 * the address decoder type matches what we asked for.
    178      1.15   gdamore 		 */
    179      1.15   gdamore 		if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
    180      1.15   gdamore 			printf("pci_mem_find: expected type mem, found i/o\n");
    181      1.25    dyoung 			return 1;
    182      1.15   gdamore 		}
    183      1.21     joerg 		/* XXX Allow 64bit bars for 32bit requests.*/
    184      1.15   gdamore 		if (PCI_MAPREG_MEM_TYPE(address) !=
    185      1.21     joerg 		    PCI_MAPREG_MEM_TYPE(type) &&
    186      1.21     joerg 		    PCI_MAPREG_MEM_TYPE(address) !=
    187      1.21     joerg 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    188      1.15   gdamore 			printf("pci_mem_find: "
    189      1.15   gdamore 			    "expected mem type %08x, found %08x\n",
    190      1.15   gdamore 			    PCI_MAPREG_MEM_TYPE(type),
    191      1.15   gdamore 			    PCI_MAPREG_MEM_TYPE(address));
    192      1.25    dyoung 			return 1;
    193      1.15   gdamore 		}
    194       1.2   mycroft 	}
    195       1.2   mycroft 
    196      1.37   msaitoh 	waddress = (uint64_t)address1 << 32UL | address;
    197      1.37   msaitoh 	wmask = (uint64_t)mask1 << 32UL | mask;
    198       1.7   thorpej 
    199      1.11    bouyer 	if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) ||
    200      1.11    bouyer 	    (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) {
    201      1.27  jmcneill 		aprint_debug("pci_mem_find: void region\n");
    202      1.25    dyoung 		return 1;
    203       1.1   mycroft 	}
    204       1.1   mycroft 
    205       1.1   mycroft 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    206       1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT:
    207       1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    208       1.1   mycroft 		break;
    209       1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_64BIT:
    210       1.7   thorpej 		/*
    211       1.7   thorpej 		 * Handle the case of a 64-bit memory register on a
    212       1.7   thorpej 		 * platform with 32-bit addressing.  Make sure that
    213       1.7   thorpej 		 * the address assigned and the device's memory size
    214       1.7   thorpej 		 * fit in 32 bits.  We implicitly assume that if
    215       1.7   thorpej 		 * bus_addr_t is 64-bit, then so is bus_size_t.
    216       1.7   thorpej 		 */
    217      1.37   msaitoh 		if (sizeof(uint64_t) > sizeof(bus_addr_t) &&
    218       1.7   thorpej 		    (address1 != 0 || mask1 != 0xffffffff)) {
    219       1.7   thorpej 			printf("pci_mem_find: 64-bit memory map which is "
    220       1.7   thorpej 			    "inaccessible on a 32-bit platform\n");
    221      1.25    dyoung 			return 1;
    222       1.7   thorpej 		}
    223       1.7   thorpej 		break;
    224       1.1   mycroft 	default:
    225       1.1   mycroft 		printf("pci_mem_find: reserved mapping register type\n");
    226      1.25    dyoung 		return 1;
    227       1.1   mycroft 	}
    228       1.1   mycroft 
    229      1.37   msaitoh 	if (sizeof(uint64_t) > sizeof(bus_addr_t)) {
    230      1.25    dyoung 		if (basep != NULL)
    231       1.7   thorpej 			*basep = PCI_MAPREG_MEM_ADDR(address);
    232      1.25    dyoung 		if (sizep != NULL)
    233       1.7   thorpej 			*sizep = PCI_MAPREG_MEM_SIZE(mask);
    234       1.7   thorpej 	} else {
    235      1.25    dyoung 		if (basep != NULL)
    236       1.7   thorpej 			*basep = PCI_MAPREG_MEM64_ADDR(waddress);
    237      1.25    dyoung 		if (sizep != NULL)
    238       1.7   thorpej 			*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
    239       1.7   thorpej 	}
    240      1.25    dyoung 	if (flagsp != NULL)
    241      1.15   gdamore 		*flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ?
    242       1.6  drochner 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    243       1.1   mycroft 
    244      1.25    dyoung 	return 0;
    245       1.7   thorpej }
    246       1.7   thorpej 
    247  1.40.2.1   thorpej static const char *
    248  1.40.2.1   thorpej bar_type_string(pcireg_t type)
    249  1.40.2.1   thorpej {
    250  1.40.2.1   thorpej 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    251  1.40.2.1   thorpej 		return "IO";
    252  1.40.2.1   thorpej 
    253  1.40.2.1   thorpej 	switch (PCI_MAPREG_MEM_TYPE(type)) {
    254  1.40.2.1   thorpej 	case PCI_MAPREG_MEM_TYPE_32BIT:
    255  1.40.2.1   thorpej 		return "MEM32";
    256  1.40.2.1   thorpej 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    257  1.40.2.1   thorpej 		return "MEM32-1M";
    258  1.40.2.1   thorpej 	case PCI_MAPREG_MEM_TYPE_64BIT:
    259  1.40.2.1   thorpej 		return "MEM64";
    260  1.40.2.1   thorpej 	}
    261  1.40.2.1   thorpej 	return "<UNKNOWN>";
    262  1.40.2.1   thorpej }
    263  1.40.2.1   thorpej 
    264  1.40.2.1   thorpej enum {
    265  1.40.2.1   thorpej 	EA_ptr_dw0		= 0,
    266  1.40.2.1   thorpej 	EA_ptr_base_lower	= 1,
    267  1.40.2.1   thorpej 	EA_ptr_base_upper	= 2,
    268  1.40.2.1   thorpej 	EA_ptr_maxoffset_lower	= 3,
    269  1.40.2.1   thorpej 	EA_ptr_maxoffset_upper	= 4,
    270  1.40.2.1   thorpej 
    271  1.40.2.1   thorpej 	EA_PTR_COUNT		= 5
    272  1.40.2.1   thorpej };
    273  1.40.2.1   thorpej 
    274  1.40.2.1   thorpej struct pci_ea_entry {
    275  1.40.2.1   thorpej 	/* entry field pointers */
    276  1.40.2.1   thorpej 	int		ea_ptrs[EA_PTR_COUNT];
    277  1.40.2.1   thorpej 
    278  1.40.2.1   thorpej 	/* Raw register values. */
    279  1.40.2.1   thorpej 	pcireg_t	dw0;
    280  1.40.2.1   thorpej 	pcireg_t	base_lower;
    281  1.40.2.1   thorpej 	pcireg_t	base_upper;
    282  1.40.2.1   thorpej 	pcireg_t	maxoffset_lower;
    283  1.40.2.1   thorpej 	pcireg_t	maxoffset_upper;
    284  1.40.2.1   thorpej 
    285  1.40.2.1   thorpej 	/* Interesting tidbits derived from them. */
    286  1.40.2.1   thorpej 	uint64_t	base;
    287  1.40.2.1   thorpej 	uint64_t	maxoffset;
    288  1.40.2.1   thorpej 	unsigned int	bei;
    289  1.40.2.1   thorpej 	unsigned int	props[2];
    290  1.40.2.1   thorpej 	bool		base_is_64;
    291  1.40.2.1   thorpej 	bool		maxoffset_is_64;
    292  1.40.2.1   thorpej 	bool		enabled;
    293  1.40.2.1   thorpej 	bool		writable;
    294  1.40.2.1   thorpej };
    295  1.40.2.1   thorpej 
    296  1.40.2.1   thorpej static int
    297  1.40.2.1   thorpej pci_ea_lookup(pci_chipset_tag_t pc, pcitag_t tag, int ea_cap_ptr,
    298  1.40.2.1   thorpej     int reg, struct pci_ea_entry *entryp)
    299  1.40.2.1   thorpej {
    300  1.40.2.1   thorpej 	struct pci_ea_entry entry = {
    301  1.40.2.1   thorpej 		.ea_ptrs[EA_ptr_dw0] = ea_cap_ptr + 4,
    302  1.40.2.1   thorpej 	};
    303  1.40.2.1   thorpej 	unsigned int i, num_entries;
    304  1.40.2.1   thorpej 	unsigned int wanted_bei;
    305  1.40.2.1   thorpej 	pcireg_t val;
    306  1.40.2.1   thorpej 
    307  1.40.2.1   thorpej 	if (reg >= PCI_BAR0 && reg <= PCI_BAR5)
    308  1.40.2.1   thorpej 		wanted_bei = PCI_EA_BEI_BAR0 + ((reg - PCI_BAR0) / 4);
    309  1.40.2.1   thorpej 	else if (reg == PCI_MAPREG_ROM)
    310  1.40.2.1   thorpej 		wanted_bei = PCI_EA_BEI_EXPROM;
    311  1.40.2.1   thorpej 	else {
    312  1.40.2.1   thorpej 		/* Invalid BAR. */
    313  1.40.2.1   thorpej 		return 1;
    314  1.40.2.1   thorpej 	}
    315  1.40.2.1   thorpej 
    316  1.40.2.1   thorpej 	val = pci_conf_read(pc, tag, ea_cap_ptr + PCI_EA_CAP1);
    317  1.40.2.1   thorpej 	num_entries = __SHIFTOUT(val, PCI_EA_CAP1_NUMENTRIES);
    318  1.40.2.1   thorpej 
    319  1.40.2.1   thorpej 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
    320  1.40.2.1   thorpej 	if (PCI_HDRTYPE_TYPE(val) == PCI_HDRTYPE_PPB) {
    321  1.40.2.1   thorpej 		/* Need to skip over PCI_EA_CAP2 on PPBs. */
    322  1.40.2.1   thorpej 		entry.ea_ptrs[EA_ptr_dw0] += 4;
    323  1.40.2.1   thorpej 	}
    324  1.40.2.1   thorpej 
    325  1.40.2.1   thorpej 	for (i = 0; i < num_entries; i++) {
    326  1.40.2.1   thorpej 		val = pci_conf_read(pc, tag, entry.ea_ptrs[EA_ptr_dw0]);
    327  1.40.2.1   thorpej 		unsigned int entry_size = __SHIFTOUT(val, PCI_EA_ES);
    328  1.40.2.1   thorpej 
    329  1.40.2.1   thorpej 		entry.bei = __SHIFTOUT(val, PCI_EA_BEI);
    330  1.40.2.1   thorpej 		entry.props[0] = __SHIFTOUT(val, PCI_EA_PP);
    331  1.40.2.1   thorpej 		entry.props[1] = __SHIFTOUT(val, PCI_EA_SP);
    332  1.40.2.1   thorpej 		entry.writable = (val & PCI_EA_W) ? true : false;
    333  1.40.2.1   thorpej 		entry.enabled = (val & PCI_EA_E) ? true : false;
    334  1.40.2.1   thorpej 
    335  1.40.2.1   thorpej 		if (entry.bei != wanted_bei || entry_size == 0) {
    336  1.40.2.1   thorpej 			entry.ea_ptrs[EA_ptr_dw0] += 4 * (entry_size + 1);
    337  1.40.2.1   thorpej 			continue;
    338  1.40.2.1   thorpej 		}
    339  1.40.2.1   thorpej 
    340  1.40.2.1   thorpej 		entry.ea_ptrs[EA_ptr_base_lower] =
    341  1.40.2.1   thorpej 		    entry.ea_ptrs[EA_ptr_dw0] + 4;
    342  1.40.2.1   thorpej 		entry.ea_ptrs[EA_ptr_maxoffset_lower] =
    343  1.40.2.1   thorpej 		    entry.ea_ptrs[EA_ptr_dw0] + 8;
    344  1.40.2.1   thorpej 
    345  1.40.2.1   thorpej 		/* Base */
    346  1.40.2.1   thorpej 		entry.base_lower = pci_conf_read(pc, tag,
    347  1.40.2.1   thorpej 		    entry.ea_ptrs[EA_ptr_base_lower]);
    348  1.40.2.1   thorpej 		entry.base_is_64 =
    349  1.40.2.1   thorpej 		    (entry.base_lower & PCI_EA_BASEMAXOFFSET_64BIT)
    350  1.40.2.1   thorpej 		    ? true : false;
    351  1.40.2.1   thorpej 		if (entry.base_is_64) {
    352  1.40.2.1   thorpej 			entry.ea_ptrs[EA_ptr_base_upper] =
    353  1.40.2.1   thorpej 			    entry.ea_ptrs[EA_ptr_dw0] + 12;
    354  1.40.2.1   thorpej 			entry.base_upper = pci_conf_read(pc, tag,
    355  1.40.2.1   thorpej 			    entry.ea_ptrs[EA_ptr_base_upper]);
    356  1.40.2.1   thorpej 		} else {
    357  1.40.2.1   thorpej 			entry.ea_ptrs[EA_ptr_base_upper] = 0;
    358  1.40.2.1   thorpej 			entry.base_upper = 0;
    359  1.40.2.1   thorpej 		}
    360  1.40.2.1   thorpej 
    361  1.40.2.1   thorpej 		entry.base = (entry.base_lower & PCI_EA_LOWMASK) |
    362  1.40.2.1   thorpej 		    ((uint64_t)entry.base_upper << 32);
    363  1.40.2.1   thorpej 
    364  1.40.2.1   thorpej 		/* MaxOffset */
    365  1.40.2.1   thorpej 		entry.maxoffset_lower = pci_conf_read(pc, tag,
    366  1.40.2.1   thorpej 		    entry.ea_ptrs[EA_ptr_maxoffset_lower]);
    367  1.40.2.1   thorpej 		entry.maxoffset_is_64 =
    368  1.40.2.1   thorpej 		    (entry.maxoffset_lower & PCI_EA_BASEMAXOFFSET_64BIT)
    369  1.40.2.1   thorpej 		    ? true : false;
    370  1.40.2.1   thorpej 		if (entry.maxoffset_is_64) {
    371  1.40.2.1   thorpej 			entry.ea_ptrs[EA_ptr_maxoffset_upper] =
    372  1.40.2.1   thorpej 			    entry.ea_ptrs[EA_ptr_dw0] +
    373  1.40.2.1   thorpej 			    (entry.base_is_64 ? 16 : 12);
    374  1.40.2.1   thorpej 			entry.maxoffset_upper = pci_conf_read(pc, tag,
    375  1.40.2.1   thorpej 			    entry.ea_ptrs[EA_ptr_maxoffset_upper]);
    376  1.40.2.1   thorpej 		} else {
    377  1.40.2.1   thorpej 			entry.ea_ptrs[EA_ptr_maxoffset_upper] = 0;
    378  1.40.2.1   thorpej 			entry.maxoffset_upper = 0;
    379  1.40.2.1   thorpej 		}
    380  1.40.2.1   thorpej 
    381  1.40.2.1   thorpej 		entry.maxoffset = (entry.maxoffset_lower & PCI_EA_LOWMASK) |
    382  1.40.2.1   thorpej 		    ((uint64_t)entry.maxoffset_upper << 32);
    383  1.40.2.1   thorpej 
    384  1.40.2.1   thorpej 		if (entryp)
    385  1.40.2.1   thorpej 			*entryp = entry;
    386  1.40.2.1   thorpej 		return 0;
    387  1.40.2.1   thorpej 	}
    388  1.40.2.1   thorpej 	return 1;
    389  1.40.2.1   thorpej }
    390  1.40.2.1   thorpej 
    391  1.40.2.1   thorpej static int
    392  1.40.2.1   thorpej pci_ea_find(pci_chipset_tag_t pc, pcitag_t tag, int ea_cap_ptr,
    393  1.40.2.1   thorpej     int reg, pcireg_t type, bus_addr_t *basep, bus_size_t *sizep, int *flagsp,
    394  1.40.2.1   thorpej     struct pci_ea_entry *entryp)
    395  1.40.2.1   thorpej {
    396  1.40.2.1   thorpej 
    397  1.40.2.1   thorpej 	struct pci_ea_entry entry;
    398  1.40.2.1   thorpej 	int rv = pci_ea_lookup(pc, tag, ea_cap_ptr, reg, &entry);
    399  1.40.2.1   thorpej 	if (rv)
    400  1.40.2.1   thorpej 		return rv;
    401  1.40.2.1   thorpej 
    402  1.40.2.1   thorpej 	pcireg_t wanted_type;
    403  1.40.2.1   thorpej 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    404  1.40.2.1   thorpej 		wanted_type = PCI_MAPREG_TYPE_IO;
    405  1.40.2.1   thorpej 	else {
    406  1.40.2.1   thorpej 		/*
    407  1.40.2.1   thorpej 		 * This covers ROM as well.  We allow any user-specified
    408  1.40.2.1   thorpej 		 * memory type to match an EA memory region with no regard
    409  1.40.2.1   thorpej 		 * for 32 vs. 64.
    410  1.40.2.1   thorpej 		 *
    411  1.40.2.1   thorpej 		 * XXX Should it?
    412  1.40.2.1   thorpej 		 */
    413  1.40.2.1   thorpej 		wanted_type = PCI_MAPREG_TYPE_MEM;
    414  1.40.2.1   thorpej 	}
    415  1.40.2.1   thorpej 
    416  1.40.2.1   thorpej 	/*
    417  1.40.2.1   thorpej 	 * MaxOffset is the last offset where you can issue a
    418  1.40.2.1   thorpej 	 * 32-bit read in the region.  Therefore, the size of
    419  1.40.2.1   thorpej 	 * the region is MaxOffset + 4.
    420  1.40.2.1   thorpej 	 */
    421  1.40.2.1   thorpej 	uint64_t region_size = entry.maxoffset + 4;
    422  1.40.2.1   thorpej 
    423  1.40.2.1   thorpej 	unsigned int which_prop;
    424  1.40.2.1   thorpej 	for (which_prop = 0; which_prop < 2; which_prop++) {
    425  1.40.2.1   thorpej 		int mapflags = 0;
    426  1.40.2.1   thorpej 
    427  1.40.2.1   thorpej 		switch (entry.props[which_prop]) {
    428  1.40.2.1   thorpej 		case PCI_EA_PROP_MEM_PREF:
    429  1.40.2.1   thorpej 			mapflags |= BUS_SPACE_MAP_PREFETCHABLE;
    430  1.40.2.1   thorpej 			/* FALLTHROUGH */
    431  1.40.2.1   thorpej 		case PCI_EA_PROP_MEM_NONPREF:
    432  1.40.2.1   thorpej 			if (PCI_MAPREG_TYPE(wanted_type) != PCI_MAPREG_TYPE_MEM)
    433  1.40.2.1   thorpej 				goto unexpected_type;
    434  1.40.2.1   thorpej 			break;
    435  1.40.2.1   thorpej 
    436  1.40.2.1   thorpej 		case PCI_EA_PROP_IO:
    437  1.40.2.1   thorpej 			if (PCI_MAPREG_TYPE(wanted_type) != PCI_MAPREG_TYPE_IO)
    438  1.40.2.1   thorpej 				goto unexpected_type;
    439  1.40.2.1   thorpej 			break;
    440  1.40.2.1   thorpej 
    441  1.40.2.1   thorpej 		case PCI_EA_PROP_MEM_UNAVAIL:
    442  1.40.2.1   thorpej 		case PCI_EA_PROP_IO_UNAVAIL:
    443  1.40.2.1   thorpej 		case PCI_EA_PROP_UNAVAIL:
    444  1.40.2.1   thorpej 			return 1;
    445  1.40.2.1   thorpej 
    446  1.40.2.1   thorpej 		/* XXX Don't support these yet. */
    447  1.40.2.1   thorpej 		case PCI_EA_PROP_VF_MEM_PREF:
    448  1.40.2.1   thorpej 		case PCI_EA_PROP_VF_MEM_NONPREF:
    449  1.40.2.1   thorpej 		case PCI_EA_PROP_BB_MEM_PREF:
    450  1.40.2.1   thorpej 		case PCI_EA_PROP_BB_MEM_NONPREF:
    451  1.40.2.1   thorpej 		case PCI_EA_PROP_BB_IO:
    452  1.40.2.1   thorpej 		default:
    453  1.40.2.1   thorpej 			printf("%s: bei %u props[%u]=0x%x\n",
    454  1.40.2.1   thorpej 			    __func__, entry.bei, which_prop,
    455  1.40.2.1   thorpej 			    entry.props[which_prop]);
    456  1.40.2.1   thorpej 			    continue;
    457  1.40.2.1   thorpej 			continue;
    458  1.40.2.1   thorpej 		}
    459  1.40.2.1   thorpej 
    460  1.40.2.1   thorpej 		if ((sizeof(uint64_t) > sizeof(bus_addr_t) ||
    461  1.40.2.1   thorpej 		     PCI_MAPREG_TYPE(wanted_type) == PCI_MAPREG_TYPE_IO) &&
    462  1.40.2.1   thorpej 		    (entry.base + region_size) > 0x100000000ULL) {
    463  1.40.2.1   thorpej 			goto inaccessible_64bit_region;
    464  1.40.2.1   thorpej 		}
    465  1.40.2.1   thorpej 
    466  1.40.2.1   thorpej 		*basep  = (bus_addr_t)entry.base;
    467  1.40.2.1   thorpej 		*sizep  = (bus_size_t)region_size;
    468  1.40.2.1   thorpej 		*flagsp = mapflags;
    469  1.40.2.1   thorpej 		if (entryp)
    470  1.40.2.1   thorpej 			*entryp = entry;
    471  1.40.2.1   thorpej 		return 0;
    472  1.40.2.1   thorpej 	}
    473  1.40.2.1   thorpej 
    474  1.40.2.1   thorpej 	/* BAR not found. */
    475  1.40.2.1   thorpej 	return 1;
    476  1.40.2.1   thorpej 
    477  1.40.2.1   thorpej  unexpected_type:
    478  1.40.2.1   thorpej 	printf("%s: unexpected type; wanted %s, got 0x%02x\n",
    479  1.40.2.1   thorpej 	    __func__, bar_type_string(wanted_type), entry.props[which_prop]);
    480  1.40.2.1   thorpej 	return 1;
    481  1.40.2.1   thorpej 
    482  1.40.2.1   thorpej  inaccessible_64bit_region:
    483  1.40.2.1   thorpej 	if (PCI_MAPREG_TYPE(wanted_type) == PCI_MAPREG_TYPE_IO) {
    484  1.40.2.1   thorpej 		printf("%s: 64-bit IO regions are unsupported\n",
    485  1.40.2.1   thorpej 		    __func__);
    486  1.40.2.1   thorpej 		return 1;
    487  1.40.2.1   thorpej 	}
    488  1.40.2.1   thorpej 	printf("%s: 64-bit memory region inaccessible on 32-bit platform\n",
    489  1.40.2.1   thorpej 	    __func__);
    490  1.40.2.1   thorpej 	return 1;
    491  1.40.2.1   thorpej }
    492  1.40.2.1   thorpej 
    493      1.12  drochner #define _PCI_MAPREG_TYPEBITS(reg) \
    494      1.12  drochner 	(PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
    495      1.12  drochner 	reg & PCI_MAPREG_TYPE_MASK : \
    496      1.12  drochner 	reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
    497      1.12  drochner 
    498       1.7   thorpej pcireg_t
    499       1.9   thorpej pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    500       1.7   thorpej {
    501       1.7   thorpej 
    502      1.25    dyoung 	return _PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg));
    503      1.12  drochner }
    504      1.12  drochner 
    505      1.12  drochner int
    506      1.12  drochner pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
    507      1.12  drochner {
    508      1.40    bouyer 	pcireg_t address, mask, csr;
    509      1.12  drochner 	int s;
    510      1.13     perry 
    511      1.12  drochner 	s = splhigh();
    512      1.12  drochner 	address = pci_conf_read(pc, tag, reg);
    513      1.40    bouyer 	/*
    514      1.40    bouyer 	 * Disable decoding via the command register before writing to the
    515      1.40    bouyer 	 * BAR register. Changing the decoding address to all-one is
    516      1.40    bouyer 	 * not a valid address and could have side effects.
    517      1.40    bouyer 	 */
    518      1.40    bouyer 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    519      1.40    bouyer 	if (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO) {
    520      1.40    bouyer 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    521      1.40    bouyer 		    csr & ~PCI_COMMAND_IO_ENABLE);
    522      1.40    bouyer 	} else {
    523      1.40    bouyer 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    524      1.40    bouyer 		    csr & ~PCI_COMMAND_MEM_ENABLE);
    525      1.40    bouyer 	}
    526      1.12  drochner 	pci_conf_write(pc, tag, reg, 0xffffffff);
    527      1.12  drochner 	mask = pci_conf_read(pc, tag, reg);
    528      1.12  drochner 	pci_conf_write(pc, tag, reg, address);
    529      1.40    bouyer 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    530      1.12  drochner 	splx(s);
    531      1.12  drochner 
    532      1.12  drochner 	if (mask == 0) /* unimplemented mapping register */
    533      1.25    dyoung 		return 0;
    534      1.12  drochner 
    535      1.25    dyoung 	if (typep != NULL)
    536      1.12  drochner 		*typep = _PCI_MAPREG_TYPEBITS(address);
    537      1.25    dyoung 	return 1;
    538       1.1   mycroft }
    539       1.1   mycroft 
    540       1.1   mycroft int
    541       1.9   thorpej pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    542       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    543       1.1   mycroft {
    544  1.40.2.1   thorpej 	int ea_cap_ptr;
    545  1.40.2.1   thorpej 
    546  1.40.2.1   thorpej 	if (pci_get_capability(pc, tag, PCI_CAP_EA, &ea_cap_ptr, NULL))
    547  1.40.2.1   thorpej 		return pci_ea_find(pc, tag, ea_cap_ptr, reg, type,
    548  1.40.2.1   thorpej 		    basep, sizep, flagsp, NULL);
    549       1.1   mycroft 
    550       1.1   mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    551      1.25    dyoung 		return pci_io_find(pc, tag, reg, type, basep, sizep,
    552      1.25    dyoung 		    flagsp);
    553       1.1   mycroft 	else
    554      1.25    dyoung 		return pci_mem_find(pc, tag, reg, type, basep, sizep,
    555      1.25    dyoung 		    flagsp);
    556       1.1   mycroft }
    557       1.1   mycroft 
    558       1.1   mycroft int
    559      1.28    dyoung pci_mapreg_map(const struct pci_attach_args *pa, int reg, pcireg_t type,
    560       1.9   thorpej     int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
    561       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep)
    562       1.1   mycroft {
    563  1.40.2.1   thorpej 	return pci_mapreg_submap(pa, reg, type, busflags, 0, 0, tagp,
    564      1.24       bjs 	    handlep, basep, sizep);
    565      1.24       bjs }
    566      1.24       bjs 
    567      1.32   msaitoh int
    568      1.28    dyoung pci_mapreg_submap(const struct pci_attach_args *pa, int reg, pcireg_t type,
    569      1.33   msaitoh     int busflags, bus_size_t reqsize, bus_size_t offset, bus_space_tag_t *tagp,
    570      1.24       bjs 	bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
    571      1.24       bjs {
    572       1.1   mycroft 	bus_space_tag_t tag;
    573       1.1   mycroft 	bus_space_handle_t handle;
    574       1.1   mycroft 	bus_addr_t base;
    575      1.33   msaitoh 	bus_size_t realmaxsize;
    576      1.34  jakllsch 	pcireg_t csr;
    577      1.34  jakllsch 	int flags, s;
    578  1.40.2.1   thorpej 	int ea_cap_ptr;
    579  1.40.2.1   thorpej 	bool have_ea = false;
    580  1.40.2.1   thorpej 	struct pci_ea_entry entry;
    581       1.1   mycroft 
    582       1.1   mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    583      1.29    dyoung 		if ((pa->pa_flags & PCI_FLAGS_IO_OKAY) == 0)
    584      1.25    dyoung 			return 1;
    585       1.1   mycroft 		tag = pa->pa_iot;
    586       1.1   mycroft 	} else {
    587      1.29    dyoung 		if ((pa->pa_flags & PCI_FLAGS_MEM_OKAY) == 0)
    588      1.25    dyoung 			return 1;
    589       1.1   mycroft 		tag = pa->pa_memt;
    590       1.1   mycroft 	}
    591       1.1   mycroft 
    592  1.40.2.1   thorpej 	if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_EA, &ea_cap_ptr,
    593  1.40.2.1   thorpej 	    NULL)) {
    594  1.40.2.1   thorpej 		have_ea = true;
    595  1.40.2.1   thorpej 		if (pci_ea_find(pa->pa_pc, pa->pa_tag, ea_cap_ptr, reg, type,
    596  1.40.2.1   thorpej 		    &base, &realmaxsize, &flags, &entry))
    597  1.40.2.1   thorpej 			return 1;
    598  1.40.2.1   thorpej 		if (reg != PCI_MAPREG_ROM && !entry.enabled) {
    599  1.40.2.1   thorpej 			/* Entry not enabled.  Try the regular BAR? */
    600  1.40.2.1   thorpej 			have_ea = false;
    601  1.40.2.1   thorpej 		}
    602  1.40.2.1   thorpej 	}
    603  1.40.2.1   thorpej 
    604  1.40.2.1   thorpej 	if (!have_ea) {
    605  1.40.2.1   thorpej 		if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    606  1.40.2.1   thorpej 			if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type,
    607  1.40.2.1   thorpej 			    &base, &realmaxsize, &flags))
    608  1.40.2.1   thorpej 				return 1;
    609  1.40.2.1   thorpej 		} else {
    610  1.40.2.1   thorpej 			if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type,
    611  1.40.2.1   thorpej 			    &base, &realmaxsize, &flags))
    612  1.40.2.1   thorpej 				return 1;
    613  1.40.2.1   thorpej 		}
    614  1.40.2.1   thorpej 	}
    615  1.40.2.1   thorpej 
    616      1.15   gdamore 	if (reg == PCI_MAPREG_ROM) {
    617  1.40.2.1   thorpej 		/* Enable the ROM address decoder, if necessary. */
    618  1.40.2.1   thorpej 		if (have_ea) {
    619  1.40.2.1   thorpej 			if (!entry.enabled) {
    620  1.40.2.1   thorpej 				entry.dw0 |= PCI_EA_E;
    621  1.40.2.1   thorpej 				pci_conf_write(pa->pa_pc, pa->pa_tag,
    622  1.40.2.1   thorpej 				    entry.ea_ptrs[EA_ptr_dw0],
    623  1.40.2.1   thorpej 				    entry.dw0);
    624  1.40.2.1   thorpej 				entry.enabled = true;
    625  1.40.2.1   thorpej 			}
    626  1.40.2.1   thorpej 		} else {
    627  1.40.2.1   thorpej 			s = splhigh();
    628  1.40.2.1   thorpej 			pcireg_t mask =
    629  1.40.2.1   thorpej 			    pci_conf_read(pa->pa_pc, pa->pa_tag, reg);
    630  1.40.2.1   thorpej 			if ((mask & PCI_MAPREG_ROM_ENABLE) == 0) {
    631  1.40.2.1   thorpej 				mask |= PCI_MAPREG_ROM_ENABLE;
    632  1.40.2.1   thorpej 				pci_conf_write(pa->pa_pc, pa->pa_tag, reg,
    633  1.40.2.1   thorpej 				    mask);
    634  1.40.2.1   thorpej 			}
    635  1.40.2.1   thorpej 			splx(s);
    636  1.40.2.1   thorpej 		}
    637      1.15   gdamore 	}
    638      1.15   gdamore 
    639  1.40.2.1   thorpej 	/*
    640  1.40.2.1   thorpej 	 * If we're called with maxsize/offset of 0, behave like
    641      1.24       bjs 	 * pci_mapreg_map.
    642      1.24       bjs 	 */
    643      1.24       bjs 
    644      1.33   msaitoh 	reqsize = (reqsize != 0) ? reqsize : realmaxsize;
    645      1.24       bjs 	base += offset;
    646      1.24       bjs 
    647      1.33   msaitoh 	if (realmaxsize < (offset + reqsize))
    648      1.25    dyoung 		return 1;
    649      1.24       bjs 
    650      1.38  riastrad 	if (bus_space_map(tag, base, reqsize, busflags, &handle))
    651      1.25    dyoung 		return 1;
    652       1.1   mycroft 
    653      1.35  jakllsch 	if (pci_mapreg_map_enable_decode) {
    654      1.35  jakllsch 		s = splhigh();
    655      1.35  jakllsch 		csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    656      1.35  jakllsch 		csr |= (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) ?
    657      1.35  jakllsch 		    PCI_COMMAND_IO_ENABLE : PCI_COMMAND_MEM_ENABLE;
    658      1.35  jakllsch 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    659      1.35  jakllsch 		splx(s);
    660      1.35  jakllsch 	}
    661      1.35  jakllsch 
    662      1.25    dyoung 	if (tagp != NULL)
    663       1.1   mycroft 		*tagp = tag;
    664      1.25    dyoung 	if (handlep != NULL)
    665       1.1   mycroft 		*handlep = handle;
    666      1.25    dyoung 	if (basep != NULL)
    667       1.1   mycroft 		*basep = base;
    668      1.25    dyoung 	if (sizep != NULL)
    669      1.33   msaitoh 		*sizep = reqsize;
    670       1.1   mycroft 
    671      1.25    dyoung 	return 0;
    672       1.1   mycroft }
    673      1.16   gdamore 
    674      1.16   gdamore int
    675      1.28    dyoung pci_find_rom(const struct pci_attach_args *pa, bus_space_tag_t bst,
    676      1.31  riastrad     bus_space_handle_t bsh, bus_size_t sz, int type,
    677      1.31  riastrad     bus_space_handle_t *romh, bus_size_t *romsz)
    678      1.16   gdamore {
    679      1.31  riastrad 	bus_size_t	offset = 0, imagesz;
    680      1.16   gdamore 	uint16_t	ptr;
    681      1.16   gdamore 	int		done = 0;
    682      1.16   gdamore 
    683      1.16   gdamore 	/*
    684      1.16   gdamore 	 * no upper bound check; i cannot imagine a 4GB ROM, but
    685      1.16   gdamore 	 * it appears the spec would allow it!
    686      1.16   gdamore 	 */
    687      1.31  riastrad 	if (sz < 1024)
    688      1.16   gdamore 		return 1;
    689      1.16   gdamore 
    690      1.31  riastrad 	while (offset < sz && !done){
    691      1.16   gdamore 		struct pci_rom_header	hdr;
    692      1.16   gdamore 		struct pci_rom		rom;
    693      1.16   gdamore 
    694      1.16   gdamore 		hdr.romh_magic = bus_space_read_2(bst, bsh,
    695      1.16   gdamore 		    offset + offsetof (struct pci_rom_header, romh_magic));
    696      1.16   gdamore 		hdr.romh_data_ptr = bus_space_read_2(bst, bsh,
    697      1.16   gdamore 		    offset + offsetof (struct pci_rom_header, romh_data_ptr));
    698      1.16   gdamore 
    699      1.16   gdamore 		/* no warning: quite possibly ROM is simply not populated */
    700      1.16   gdamore 		if (hdr.romh_magic != PCI_ROM_HEADER_MAGIC)
    701      1.16   gdamore 			return 1;
    702      1.16   gdamore 
    703      1.16   gdamore 		ptr = offset + hdr.romh_data_ptr;
    704  1.40.2.1   thorpej 
    705      1.31  riastrad 		if (ptr > sz) {
    706      1.16   gdamore 			printf("pci_find_rom: rom data ptr out of range\n");
    707      1.16   gdamore 			return 1;
    708      1.16   gdamore 		}
    709      1.16   gdamore 
    710      1.16   gdamore 		rom.rom_signature = bus_space_read_4(bst, bsh, ptr);
    711      1.16   gdamore 		rom.rom_vendor = bus_space_read_2(bst, bsh, ptr +
    712      1.16   gdamore 		    offsetof(struct pci_rom, rom_vendor));
    713      1.16   gdamore 		rom.rom_product = bus_space_read_2(bst, bsh, ptr +
    714      1.16   gdamore 		    offsetof(struct pci_rom, rom_product));
    715      1.16   gdamore 		rom.rom_class = bus_space_read_1(bst, bsh,
    716      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_class));
    717      1.16   gdamore 		rom.rom_subclass = bus_space_read_1(bst, bsh,
    718      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_subclass));
    719      1.16   gdamore 		rom.rom_interface = bus_space_read_1(bst, bsh,
    720      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_interface));
    721      1.16   gdamore 		rom.rom_len = bus_space_read_2(bst, bsh,
    722      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_len));
    723      1.16   gdamore 		rom.rom_code_type = bus_space_read_1(bst, bsh,
    724      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_code_type));
    725      1.16   gdamore 		rom.rom_indicator = bus_space_read_1(bst, bsh,
    726      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_indicator));
    727      1.16   gdamore 
    728      1.16   gdamore 		if (rom.rom_signature != PCI_ROM_SIGNATURE) {
    729      1.16   gdamore 			printf("pci_find_rom: bad rom data signature\n");
    730      1.16   gdamore 			return 1;
    731      1.16   gdamore 		}
    732      1.16   gdamore 
    733      1.16   gdamore 		imagesz = rom.rom_len * 512;
    734      1.16   gdamore 
    735      1.16   gdamore 		if ((rom.rom_vendor == PCI_VENDOR(pa->pa_id)) &&
    736      1.16   gdamore 		    (rom.rom_product == PCI_PRODUCT(pa->pa_id)) &&
    737      1.16   gdamore 		    (rom.rom_class == PCI_CLASS(pa->pa_class)) &&
    738      1.16   gdamore 		    (rom.rom_subclass == PCI_SUBCLASS(pa->pa_class)) &&
    739      1.16   gdamore 		    (rom.rom_interface == PCI_INTERFACE(pa->pa_class)) &&
    740      1.16   gdamore 		    (rom.rom_code_type == type)) {
    741      1.31  riastrad 			*romsz = imagesz;
    742      1.16   gdamore 			bus_space_subregion(bst, bsh, offset, imagesz, romh);
    743      1.16   gdamore 			return 0;
    744      1.16   gdamore 		}
    745  1.40.2.1   thorpej 
    746      1.16   gdamore 		/* last image check */
    747      1.16   gdamore 		if (rom.rom_indicator & PCI_ROM_INDICATOR_LAST)
    748      1.16   gdamore 			return 1;
    749      1.16   gdamore 
    750      1.16   gdamore 		/* offset by size */
    751      1.16   gdamore 		offset += imagesz;
    752      1.16   gdamore 	}
    753      1.16   gdamore 	return 1;
    754      1.16   gdamore }
    755