#include <sys/conf.h>
#include <sys/pci.h>
#include <sys/sunndi.h>
#include <sys/pcie.h>
#include <sys/pcie_impl.h>
#include <sys/pci_cfgspace.h>
#include <io/pciex/pcie_nvidia.h>
extern int pci_boot_debug;
extern uint64_t mcfg_mem_base;
boolean_t
check_if_device_is_pciex(dev_info_t *cdip, uchar_t bus, uchar_t dev,
uchar_t func, boolean_t *slot_valid, ushort_t *slot_number,
ushort_t *is_pci_bridge)
{
boolean_t found_pciex = B_FALSE;
ushort_t cap;
ushort_t capsp;
ushort_t cap_count = PCI_CAP_MAX_PTR;
ushort_t status;
uint32_t slot_cap;
*slot_valid = B_FALSE;
status = (*pci_getw_func)(bus, dev, func, PCI_CONF_STAT);
if (!(status & PCI_STAT_CAP))
return (B_FALSE);
capsp = (*pci_getb_func)(bus, dev, func, PCI_CONF_CAP_PTR);
while (cap_count-- && capsp >= PCI_CAP_PTR_OFF) {
capsp &= PCI_CAP_PTR_MASK;
cap = (*pci_getb_func)(bus, dev, func, capsp);
if (cap == PCI_CAP_ID_PCI_E) {
#ifdef DEBUG
if (pci_boot_debug)
cmn_err(CE_CONT, "PCI-Express (%x,%x,%x) "
"capability found\n", bus, dev, func);
#endif
status = (*pci_getw_func)(bus, dev, func, capsp + 2);
*is_pci_bridge =
((status & PCIE_PCIECAP_DEV_TYPE_MASK) ==
PCIE_PCIECAP_DEV_TYPE_PCIE2PCI) ? 1 : 0;
if (status & PCIE_PCIECAP_SLOT_IMPL) {
slot_cap = (*pci_getl_func)(bus, dev, func,
capsp + PCIE_SLOTCAP);
*slot_valid = B_TRUE;
*slot_number =
PCIE_SLOTCAP_PHY_SLOT_NUM(slot_cap);
}
found_pciex = B_TRUE;
}
capsp = (*pci_getb_func)(bus, dev, func,
capsp + PCI_CAP_NEXT_PTR);
}
return (found_pciex);
}
boolean_t
look_for_any_pciex_device(uchar_t bus)
{
uchar_t dev, func;
uchar_t nfunc, header;
ushort_t venid, slot_num, is_pci_bridge = 0;
boolean_t slot_valid;
for (dev = 0; dev < 32; dev++) {
nfunc = 1;
for (func = 0; func < nfunc; func++) {
#ifdef DEBUG
if (pci_boot_debug)
cmn_err(CE_NOTE, "pciex dev 0x%x, func 0x%x",
dev, func);
#endif
venid = (*pci_getw_func)(bus, dev, func,
PCI_CONF_VENID);
if ((venid == 0xffff) || (venid == 0))
continue;
header = (*pci_getb_func)(bus, dev, func,
PCI_CONF_HEADER);
if (header == 0xff)
continue;
if ((func == 0) && (header & PCI_HEADER_MULTI))
nfunc = 8;
if (check_if_device_is_pciex(NULL, bus, dev, func,
&slot_valid, &slot_num, &is_pci_bridge) == B_TRUE)
return (B_TRUE);
}
}
return (B_FALSE);
}
boolean_t
create_pcie_root_bus(uchar_t bus, dev_info_t *dip)
{
if (look_for_any_pciex_device(bus) == B_FALSE)
return (B_FALSE);
#ifdef DEBUG
if (pci_boot_debug)
cmn_err(CE_CONT, "Found PCI-Ex in the system\n");
#endif
(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
"device_type", "pciex");
(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
"compatible", "pciex_root_complex");
pcie_rc_init_bus(dip);
return (B_TRUE);
}
void
add_nvidia_isa_bridge_props(dev_info_t *dip, uchar_t bus, uchar_t dev,
uchar_t func)
{
uint_t devloc, base;
pci_regspec_t regs[2] = {{0}};
pci_regspec_t assigned[2] = {{0}};
devloc = PCI_REG_MAKE_BDFR(bus, dev, func, 0);
regs[0].pci_phys_hi = devloc;
base = (*pci_getl_func)(bus, dev, func,
NVIDIA_CK804_ISA_SYSCTRL_BAR_OFF);
regs[0].pci_size_low = assigned[0].pci_size_low = PCI_CONF_HDR_SIZE;
assigned[0].pci_phys_hi = regs[0].pci_phys_hi = (PCI_RELOCAT_B |
PCI_ADDR_IO | devloc | NVIDIA_CK804_ISA_SYSCTRL_BAR_OFF);
assigned[0].pci_phys_low = regs[0].pci_phys_low =
base & PCI_BASE_IO_ADDR_M;
base = (*pci_getl_func)(bus, dev, func,
NVIDIA_CK804_ISA_ANALOG_BAR_OFF);
regs[1].pci_size_low = assigned[1].pci_size_low = PCI_CONF_HDR_SIZE;
assigned[1].pci_phys_hi = regs[1].pci_phys_hi = (PCI_RELOCAT_B |
PCI_ADDR_IO | devloc | NVIDIA_CK804_ISA_ANALOG_BAR_OFF);
assigned[1].pci_phys_low = regs[1].pci_phys_low =
base & PCI_BASE_IO_ADDR_M;
(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
(int *)regs, 2 * sizeof (pci_regspec_t) / sizeof (int));
(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
"assigned-addresses",
(int *)assigned, 2 * sizeof (pci_regspec_t) / sizeof (int));
}