/*-
* Copyright (c) 1998, 2000, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static int
pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
{
pcireg_t address, mask, csr;
int s;
if (reg < PCI_MAPREG_START ||
#if 0
/*
* Can't do this check; some devices have mapping registers
* way out in left field.
*/
reg >= PCI_MAPREG_END ||
#endif
(reg & 3))
panic("pci_io_find: bad request");
/*
* Section 6.2.5.1, `Address Maps', tells us that:
*
* 1) The builtin software should have already mapped the device in a
* reasonable way.
*
* 2) A device which wants 2^n bytes of memory will hardwire the bottom
* n bits of the address to 0. As recommended, we write all 1s and see
* what we get back.
*/
s = splhigh();
address = pci_conf_read(pc, tag, reg);
/*
* Disable decoding via the command register before writing to the
* BAR register. Changing the decoding address to all-one is
* not a valid address and could have side effects.
*/
csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
csr & ~PCI_COMMAND_IO_ENABLE) ;
pci_conf_write(pc, tag, reg, 0xffffffff);
mask = pci_conf_read(pc, tag, reg);
pci_conf_write(pc, tag, reg, address);
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
splx(s);
if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
aprint_debug("pci_io_find: expected type i/o, found mem\n");
return 1;
}
if ((!isrom) && (reg < PCI_MAPREG_START ||
#if 0
/*
* Can't do this check; some devices have mapping registers
* way out in left field.
*/
reg >= PCI_MAPREG_END ||
#endif
(reg & 3)))
panic("pci_mem_find: bad request");
if (is64bit && (reg + 4) >= PCI_MAPREG_END)
panic("pci_mem_find: bad 64-bit request");
/*
* Section 6.2.5.1, `Address Maps', tells us that:
*
* 1) The builtin software should have already mapped the device in a
* reasonable way.
*
* 2) A device which wants 2^n bytes of memory will hardwire the bottom
* n bits of the address to 0. As recommended, we write all 1s and see
* what we get back. Only probe the upper BAR of a mem64 BAR if bit 31
* is readonly.
*/
s = splhigh();
address = pci_conf_read(pc, tag, reg);
csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
/*
* Disable decoding via the command register before writing to the
* BAR register. Changing the decoding address to all-one is
* not a valid address and could have side effects.
*/
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
csr & ~PCI_COMMAND_MEM_ENABLE) ;
pci_conf_write(pc, tag, reg, 0xffffffff);
mask = pci_conf_read(pc, tag, reg);
pci_conf_write(pc, tag, reg, address);
if (is64bit) {
address1 = pci_conf_read(pc, tag, reg + 4);
if ((mask & 0x80000000) == 0) {
pci_conf_write(pc, tag, reg + 4, 0xffffffff);
mask1 = pci_conf_read(pc, tag, reg + 4);
pci_conf_write(pc, tag, reg + 4, address1);
}
}
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
splx(s);
if (!isrom) {
/*
* roms should have an enable bit instead of a memory
* type decoder bit. For normal BARs, make sure that
* the address decoder type matches what we asked for.
*/
if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
printf("pci_mem_find: expected type mem, found i/o\n");
return 1;
}
/* XXX Allow 64bit bars for 32bit requests.*/
if (PCI_MAPREG_MEM_TYPE(address) !=
PCI_MAPREG_MEM_TYPE(type) &&
PCI_MAPREG_MEM_TYPE(address) !=
PCI_MAPREG_MEM_TYPE_64BIT) {
printf("pci_mem_find: "
"expected mem type %08x, found %08x\n",
PCI_MAPREG_MEM_TYPE(type),
PCI_MAPREG_MEM_TYPE(address));
return 1;
}
}
switch (PCI_MAPREG_MEM_TYPE(address)) {
case PCI_MAPREG_MEM_TYPE_32BIT:
case PCI_MAPREG_MEM_TYPE_32BIT_1M:
break;
case PCI_MAPREG_MEM_TYPE_64BIT:
/*
* Handle the case of a 64-bit memory register on a
* platform with 32-bit addressing. Make sure that
* the address assigned and the device's memory size
* fit in 32 bits. We implicitly assume that if
* bus_addr_t is 64-bit, then so is bus_size_t.
*/
if (sizeof(uint64_t) > sizeof(bus_addr_t) &&
(address1 != 0 || mask1 != 0xffffffff)) {
printf("pci_mem_find: 64-bit memory map which is "
"inaccessible on a 32-bit platform\n");
return 1;
}
break;
default:
printf("pci_mem_find: reserved mapping register type\n");
return 1;
}
if (sizeof(uint64_t) > sizeof(bus_addr_t)) {
if (basep != NULL)
*basep = PCI_MAPREG_MEM_ADDR(address);
if (sizep != NULL)
*sizep = PCI_MAPREG_MEM_SIZE(mask);
} else {
if (basep != NULL)
*basep = PCI_MAPREG_MEM64_ADDR(waddress);
if (sizep != NULL)
*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
}
if (flagsp != NULL)
*flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ?
BUS_SPACE_MAP_PREFETCHABLE : 0;
val = pci_conf_read(pc, tag, PCI_BHLC_REG);
if (PCI_HDRTYPE_TYPE(val) == PCI_HDRTYPE_PPB) {
/* Need to skip over PCI_EA_CAP2 on PPBs. */
entry.ea_ptrs[EA_ptr_dw0] += 4;
}
for (i = 0; i < num_entries; i++) {
val = pci_conf_read(pc, tag, entry.ea_ptrs[EA_ptr_dw0]);
unsigned int entry_size = __SHIFTOUT(val, PCI_EA_ES);
static int
pci_ea_find(pci_chipset_tag_t pc, pcitag_t tag, int ea_cap_ptr,
int reg, pcireg_t type, bus_addr_t *basep, bus_size_t *sizep, int *flagsp,
struct pci_ea_entry *entryp)
{
struct pci_ea_entry entry;
int rv = pci_ea_lookup(pc, tag, ea_cap_ptr, reg, &entry);
if (rv)
return rv;
pcireg_t wanted_type;
if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
wanted_type = PCI_MAPREG_TYPE_IO;
else {
/*
* This covers ROM as well. We allow any user-specified
* memory type to match an EA memory region with no regard
* for 32 vs. 64.
*
* XXX Should it?
*/
wanted_type = PCI_MAPREG_TYPE_MEM;
}
/*
* MaxOffset is the last offset where you can issue a
* 32-bit read in the region. Therefore, the size of
* the region is MaxOffset + 4.
*/
uint64_t region_size = entry.maxoffset + 4;
unsigned int which_prop;
for (which_prop = 0; which_prop < 2; which_prop++) {
int mapflags = 0;
switch (entry.props[which_prop]) {
case PCI_EA_PROP_MEM_PREF:
mapflags |= BUS_SPACE_MAP_PREFETCHABLE;
/* FALLTHROUGH */
case PCI_EA_PROP_MEM_NONPREF:
if (PCI_MAPREG_TYPE(wanted_type) != PCI_MAPREG_TYPE_MEM)
goto unexpected_type;
break;
case PCI_EA_PROP_IO:
if (PCI_MAPREG_TYPE(wanted_type) != PCI_MAPREG_TYPE_IO)
goto unexpected_type;
break;
case PCI_EA_PROP_MEM_UNAVAIL:
case PCI_EA_PROP_IO_UNAVAIL:
case PCI_EA_PROP_UNAVAIL:
return 1;
/* XXX Don't support these yet. */
case PCI_EA_PROP_VF_MEM_PREF:
case PCI_EA_PROP_VF_MEM_NONPREF:
case PCI_EA_PROP_BB_MEM_PREF:
case PCI_EA_PROP_BB_MEM_NONPREF:
case PCI_EA_PROP_BB_IO:
default:
printf("%s: bei %u props[%u]=0x%x\n",
__func__, entry.bei, which_prop,
entry.props[which_prop]);
continue;
continue;
}
int
pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
{
pcireg_t address, mask, csr;
int s;
s = splhigh();
address = pci_conf_read(pc, tag, reg);
/*
* Disable decoding via the command register before writing to the
* BAR register. Changing the decoding address to all-one is
* not a valid address and could have side effects.
*/
csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
if (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO) {
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
csr & ~PCI_COMMAND_IO_ENABLE);
} else {
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
csr & ~PCI_COMMAND_MEM_ENABLE);
}
pci_conf_write(pc, tag, reg, 0xffffffff);
mask = pci_conf_read(pc, tag, reg);
pci_conf_write(pc, tag, reg, address);
pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
splx(s);