#include <sys/systm.h>
#include <sys/psw.h>
#include <sys/bootconf.h>
#include <sys/reboot.h>
#include <sys/pci_impl.h>
#include <sys/pci_cfgspace.h>
#include <sys/pci_cfgspace_impl.h>
#include <sys/pci_cfgacc.h>
#if defined(__xpv)
#include <sys/hypervisor.h>
#endif
#if defined(__xpv)
int pci_max_nbus = 0xFE;
#else
int pci_max_nbus = 0xFF;
#endif
int pci_bios_cfg_type = PCI_MECHANISM_UNKNOWN;
int pci_bios_maxbus;
int pci_bios_mech;
int pci_bios_vers;
int PCI_CFG_TYPE = 0;
int PCI_PROBE_TYPE = 0;
uint64_t mcfg_mem_base = 0;
uint8_t mcfg_bus_start = 0;
uint8_t mcfg_bus_end = 0xff;
uint_t pci_iocfg_max_offset = 0xff;
uint8_t (*pci_getb_func)(int bus, int dev, int func, int reg);
uint16_t (*pci_getw_func)(int bus, int dev, int func, int reg);
uint32_t (*pci_getl_func)(int bus, int dev, int func, int reg);
void (*pci_putb_func)(int bus, int dev, int func, int reg, uint8_t val);
void (*pci_putw_func)(int bus, int dev, int func, int reg, uint16_t val);
void (*pci_putl_func)(int bus, int dev, int func, int reg, uint32_t val);
extern void (*pci_cfgacc_acc_p)(pci_cfgacc_req_t *req);
static int pci_check(void);
#if !defined(__xpv)
static int pci_check_bios(void);
static int pci_get_cfg_type(void);
#endif
kmutex_t pcicfg_mutex;
kmutex_t pcicfg_mmio_mutex;
kmutex_t pcicfg_chipset_mutex;
void
pci_cfgspace_init(void)
{
mutex_init(&pcicfg_mutex, NULL, MUTEX_SPIN,
(ddi_iblock_cookie_t)ipltospl(15));
mutex_init(&pcicfg_mmio_mutex, NULL, MUTEX_SPIN,
(ddi_iblock_cookie_t)ipltospl(DISP_LEVEL));
mutex_init(&pcicfg_chipset_mutex, NULL, MUTEX_SPIN,
(ddi_iblock_cookie_t)ipltospl(15));
if (!pci_check()) {
mutex_destroy(&pcicfg_mutex);
mutex_destroy(&pcicfg_mmio_mutex);
mutex_destroy(&pcicfg_chipset_mutex);
}
}
static int
pci_check(void)
{
uint64_t ecfginfo[4];
if (pci_bios_cfg_type != PCI_MECHANISM_UNKNOWN)
return (TRUE);
#if defined(__xpv)
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
pci_bios_cfg_type = PCI_MECHANISM_1;
pci_getb_func = pci_mech1_getb;
pci_getw_func = pci_mech1_getw;
pci_getl_func = pci_mech1_getl;
pci_putb_func = pci_mech1_putb;
pci_putw_func = pci_mech1_putw;
pci_putl_func = pci_mech1_putl;
pci_bios_maxbus = pci_max_nbus;
}
#else
pci_bios_cfg_type = pci_check_bios();
if (pci_bios_cfg_type == PCI_MECHANISM_NONE) {
pci_bios_cfg_type = PCI_MECHANISM_1;
pci_bios_maxbus = pci_max_nbus;
}
switch (pci_get_cfg_type()) {
case PCI_MECHANISM_1:
if (pci_is_broken_orion()) {
pci_getb_func = pci_orion_getb;
pci_getw_func = pci_orion_getw;
pci_getl_func = pci_orion_getl;
pci_putb_func = pci_orion_putb;
pci_putw_func = pci_orion_putw;
pci_putl_func = pci_orion_putl;
} else if (pci_check_amd_ioecs()) {
pci_getb_func = pci_mech1_amd_getb;
pci_getw_func = pci_mech1_amd_getw;
pci_getl_func = pci_mech1_amd_getl;
pci_putb_func = pci_mech1_amd_putb;
pci_putw_func = pci_mech1_amd_putw;
pci_putl_func = pci_mech1_amd_putl;
pci_iocfg_max_offset = 0xfff;
} else {
pci_getb_func = pci_mech1_getb;
pci_getw_func = pci_mech1_getw;
pci_getl_func = pci_mech1_getl;
pci_putb_func = pci_mech1_putb;
pci_putw_func = pci_mech1_putw;
pci_putl_func = pci_mech1_putl;
}
break;
case PCI_MECHANISM_2:
if (pci_check_neptune()) {
pci_getb_func = pci_neptune_getb;
pci_getw_func = pci_neptune_getw;
pci_getl_func = pci_neptune_getl;
pci_putb_func = pci_neptune_putb;
pci_putw_func = pci_neptune_putw;
pci_putl_func = pci_neptune_putl;
} else {
pci_getb_func = pci_mech2_getb;
pci_getw_func = pci_mech2_getw;
pci_getl_func = pci_mech2_getl;
pci_putb_func = pci_mech2_putb;
pci_putw_func = pci_mech2_putw;
pci_putl_func = pci_mech2_putl;
}
break;
default:
return (FALSE);
}
#endif
if (do_bsys_getprop(NULL, MCFG_PROPNAME, ecfginfo) != -1) {
mcfg_mem_base = ecfginfo[0];
mcfg_bus_start = ecfginfo[2];
mcfg_bus_end = ecfginfo[3];
}
pci_cfgacc_acc_p = pci_cfgacc_acc;
return (TRUE);
}
#if !defined(__xpv)
static int
pci_check_bios(void)
{
struct bop_regs regs;
uint32_t carryflag;
uint16_t ax, dx;
if (BOP_GETPROPLEN(bootops, "efi-systab") > 0) {
pci_bios_mech = 1;
pci_bios_vers = 0;
pci_bios_maxbus = pci_max_nbus;
return (PCI_MECHANISM_1);
}
bzero(®s, sizeof (regs));
regs.eax.word.ax = (PCI_FUNCTION_ID << 8) | PCI_BIOS_PRESENT;
BOP_DOINT(bootops, 0x1a, ®s);
carryflag = regs.eflags & PS_C;
ax = regs.eax.word.ax;
dx = regs.edx.word.dx;
if (carryflag != 0)
return (PCI_MECHANISM_NONE);
if (dx != ('P' | 'C'<<8))
return (PCI_MECHANISM_NONE);
if ((ax & 0xff00) != 0)
return (PCI_MECHANISM_NONE);
pci_bios_mech = (ax & 0x3);
pci_bios_vers = regs.ebx.word.bx;
pci_bios_maxbus = pci_max_nbus;
switch (pci_bios_mech) {
default:
case 0:
return (PCI_MECHANISM_NONE);
case 1:
case 3:
return (PCI_MECHANISM_1);
case 2:
return (PCI_MECHANISM_2);
}
}
static int
pci_get_cfg_type(void)
{
switch (PCI_CFG_TYPE) {
default:
case 0:
break;
case 1:
return (PCI_MECHANISM_1);
case 2:
return (PCI_MECHANISM_2);
case -1:
return (PCI_MECHANISM_NONE);
}
switch (PCI_PROBE_TYPE) {
default:
case 0:
return (pci_bios_cfg_type);
case -1:
return (PCI_MECHANISM_NONE);
}
}
#endif