⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcicfg.c

📁 PCI总线的驱动程序
💻 C
📖 第 1 页 / 共 5 页
字号:
    return RetVal;
}


//
// Initialize and configure device
//
static BOOL
PCICfgDevice(
    PPCI_DEV_INFO pBusInfo,
    ULONG Bus,
    ULONG Device,
    ULONG Function,
    PPCI_COMMON_CONFIG pCfg,
    PULONG pMemSize,
    PULONG pIoSize
    )
{
    DWORD Reg;
    PCI_DEV_INFO Info;
    
    DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!PCICfgDevice+(Bus %d, Device %d, Function %d, Vendor ID 0x%X, Device ID 0x%X)\r\n",
        Bus, Device, Function, pCfg->VendorID, pCfg->DeviceID));

    // Initialize Info structure
    PCIInitInfo(pBusInfo->RegPath, Bus, Device, Function, pCfg, &Info);

    // Attempt to find match for this device in the registry and get info
    if (PCICfgFindMatch(&Info)) {
        Info.Matched = TRUE;

        if (!RegGetInfo(&Info)) {
            return FALSE;
        }
        if ( (Info.DevFlags & DEVFLAGS_BOOTPHASE_1 ) && !IsSystemPhase1()) {
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!PCICfgDevice: Registry says only configure this device in phase 1\r\n"));
            // Do not load driver that set only loaded on phase 1.
            return TRUE;
        }

        if (!Info.Configure) {
            // If instructed not to configure, disable device and return
            DEBUGMSG(ZONE_WARNING, (L"PCIBUS!PCICfgDevice: Registry says don't configure this device\r\n"));

            if (pCfg->Command & (PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE | PCI_ENABLE_BUS_MASTER)) {
                PCIConfig_Write(Bus, Device, Function, PCI_CONFIG_COMMAND_STATUS, 0xFFFF0000);
            }
            
            return TRUE;
        }

        // Make sure the sum of Base entries is less than or equal to 6
        if (Info.MemBase.Num + Info.IoBase.Num > PCI_TYPE0_ADDRESSES) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgDevice: The number of registry values '%s' and '%s' under '%s' must sum to be less than %d.\r\n",
                PCIBUS_MEMBASE_VALNAME, PCIBUS_IOBASE_VALNAME, Info.RegPath, PCI_TYPE0_ADDRESSES));

            return FALSE;
        }

        // Make sure the sum of Len entries is less than or equal to 6
        if (Info.MemLen.Num + Info.IoLen.Num > PCI_TYPE0_ADDRESSES) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgDevice: The number of registry values '%s' and '%s' under '%s' must sum to be less than %d.\r\n",
                PCIBUS_MEMLEN_VALNAME, PCIBUS_IOLEN_VALNAME, Info.RegPath, PCI_TYPE0_ADDRESSES));

            return FALSE;
        }
    }

    // If ConfigEntry defined, call it instead of CheckBARs
    if (Info.ConfigEntry) {
        DWORD Status;
        
        // Load driver Dll and obtain ConfigEntry function address
        if (!LoadConfigEntry(&Info)) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgDevice: Failed LoadConfigEntry call\r\n"));

            return FALSE;
        }

        Status = ((PFN_CONFIGENTRY)(Info.ConfigInfo->ConfigEntryFn))(PCIBUS_CONFIG_RSRC, &Info, g_MemHead, g_IoHead, pMemSize, pIoSize);
        
        if (Status != ERROR_SUCCESS) {
            if (Status == ERROR_NOT_SUPPORTED) {
                // ConfigEntry point doesn't support this type of call, so do it here
                // Read the base address registers to determine space to allocate
                if (!PCICfgCheckBARs(&Info, pMemSize, pIoSize)) {
                    DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgDevice: Failed PCICfgCheckBARs call\r\n"));
                    
                    return FALSE;
                }
            } else {
                DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgDevice: Failed %s:%s call\r\n", Info.DllName, Info.ConfigEntryName));

                return FALSE;
            }
        }
    } else {
        // Read the base address registers to determine space to allocate
        if (!PCICfgCheckBARs(&Info, pMemSize, pIoSize)) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgDevice: Failed PCICfgCheckBARs call\r\n"));
            
            return FALSE;
        }
    }

    // If device not already placed, configure interrupt
    if (!(pCfg->Command & (PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE))) {
        DEVICE_LOCATION DevLoc;
        DWORD Irq = 0xFF;

        Reg = PCIConfig_Read(Bus, Device, Function, PCI_CONFIG_INTERRUPT);
        
        // Fill in device location structure
        DevLoc.IfcType = PCIBus;
        DevLoc.BusNumber = Bus;
        DevLoc.LogicalLoc = (Bus << 16) | (Device << 8) | Function;
        DevLoc.Pin = ((PPCI_INT)(&Reg))->InterruptPin;

        if (DevLoc.Pin) {
            // Interrupts used, get IRQ from OAL
            if (!RequestIrq(&DevLoc, &Irq)) {
                DEBUGMSG(ZONE_INIT, (L"PCIBUS!PCICfgDevice: Get IRQ call failed.\r\n"));

                // No interrupts
                Irq = 0xFF;
            } else if (Irq > 254) {
                DEBUGMSG(ZONE_INIT, (L"PCIBUS!PCICfgDevice: IRQ returned from OAL is outside of range (0-254).  Ignoring.\r\n"));

                // No interrupts
                Irq = 0xFF;
            }
        }

        DEBUGMSG(ZONE_INIT, (L"RequestIrq(%d/%d/%d) returns %d\r\n", Bus, Device, Function, Irq));
        
        ((PPCI_INT)(&Reg))->InterruptLine = (BYTE)Irq;
        PCIConfig_Write(Bus, Device, Function, PCI_CONFIG_INTERRUPT, Reg);      

		// Disable expansion ROM
		PCIConfig_Write(Bus, Device, Function, PCI_CONFIG_ROM, 0);
    }

    // Set Latency register
	Info.Latency = (Info.Latency > 0) ? Info.Latency : pBusInfo->Latency;   // use global bus value if not defined for device
    if (Info.Latency) {
        // Reprogram only if non-zero
    	Reg = PCIConfig_Read(Bus, Device, Function, PCI_CONFIG_HEAD);
    	Reg = (Reg & 0xFFFF00FF) | ((Info.Latency & 0xFF) << 8);
    	PCIConfig_Write(Bus, Device, Function, PCI_CONFIG_HEAD, Reg);
    }

	// Write command value and reset device status
	if (!(pCfg->Command & (PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE))) {
	    Reg = PCIConfig_Read(Bus, Device, Function, PCI_CONFIG_COMMAND_STATUS);
	    Reg &= 0x7;
	    Reg |= 0xFFFF0000 | (Info.Command & 0x000003FF);
	    PCIConfig_Write(Bus, Device, Function, PCI_CONFIG_COMMAND_STATUS, Reg);
	}
    DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!PCICfgDevice-\r\n"));
        
    return TRUE;
}


//
// Initialize and configure PCI-PCI bridge
//
static BOOL
PCICfgBridge(
    PPCI_DEV_INFO pBusInfo,
    ULONG Bus,
    ULONG Device,
    ULONG Function,
    PPCI_COMMON_CONFIG pCfg,
    PULONG pSubordinateBus,
    PULONG pMemSize,
    PULONG pIoSize
    )
{
    ULONG BusReg;
    ULONG SecondaryMemSize;
    ULONG SecondaryIoSize;
    ULONG i;
    ULONG Reg;

    PCI_DEV_INFO Info;

    BOOL MemPlaced = FALSE;
    BOOL IOPlaced = FALSE;
    DWORD SavedMemBase = 0xFFF00000, SavedMemSize = 0;
    DWORD SavedIOBase = 0xF000, SavedIOSize = 0;

    DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!PCICfgBridge+(Bus %d, Device %d, Function %d, Device ID/Vendor ID 0x%X)\r\n",
        Bus, Device, Function, PCIConfig_Read(Bus, Device, Function, 0)));
    
    // Check to see if bridge memory space already enabled
    Reg = PCIConfig_Read(Bus, Device, Function, PCI_CONFIG_COMMAND_STATUS);
    if (Reg & PCI_ENABLE_MEMORY_SPACE) {
        DWORD MemLimit;
        
        // Memory access enabled
        MemPlaced = TRUE;

        // Save off current values of memory and I/O bases and sizes
        Reg = PCIConfig_Read(Bus, Device, Function, PCIBRIDGE_MEMORY);
        SavedMemBase = (Reg & 0xFFF0) << 16;
        MemLimit = (Reg & 0xFFF00000);

        if (MemLimit < SavedMemBase) {
            // Invalid memory base/limit pair
            SavedMemSize = 0;
        } else {
            SavedMemSize = (MemLimit + 0x000FFFFF + 1) - SavedMemBase;
        }
    }

    // Check to see if bridge I/O space already enabled
    Reg = PCIConfig_Read(Bus, Device, Function, PCI_CONFIG_COMMAND_STATUS);
    if (Reg & PCI_ENABLE_IO_SPACE) {
        DWORD IOLimit;
        
        // I/O access enabled
        IOPlaced = TRUE;
        
        // Save off current values of I/O base and size
        Reg = PCIConfig_Read(Bus, Device, Function, PCIBRIDGE_IO);

        SavedIOBase = (Reg & 0xF0) << 8;
        IOLimit = (Reg & 0xF000);

        if ((Reg & 0xF) == PCI_SECONDARY_IO32) {
            // 32-bit I/O
            Reg = PCIConfig_Read(Bus, Device, Function, PCIBRIDGE_IO_UPPER16);

            SavedIOBase |= (Reg & 0x0000FFFF) << 16;
            IOLimit |= (Reg & 0xFFFF0000);
        }

        if (IOLimit < SavedIOBase) {
            // Invalid I/O base/limit combination
            SavedIOSize = 0;
        } else {
            SavedIOSize = (IOLimit + 0x0FFF + 1) - SavedIOBase;
        }
    }

    // Initialize Info structure
    PCIInitInfo(pBusInfo->RegPath, Bus, Device, Function, pCfg, &Info);

    // Attempt to find match for this device in the registry and get info
    if (PCICfgFindMatch(&Info)) {
        Info.Matched = TRUE;

        if (!RegGetInfo(&Info)) {
            return FALSE;
        }

        // Make sure the sum of Base entries is less than or equal to 2
        if (Info.MemBase.Num + Info.IoBase.Num > PCI_TYPE1_ADDRESSES) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgBridge: The number of registry values '%s' and '%s' under '%s' must sum to be less than %d.\r\n",
                PCIBUS_MEMBASE_VALNAME, PCIBUS_IOBASE_VALNAME, Info.RegPath, PCI_TYPE1_ADDRESSES));

            return FALSE;
        }

        // Make sure the sum of Len entries is less than or equal to 2
        if (Info.MemLen.Num + Info.IoLen.Num > PCI_TYPE1_ADDRESSES) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgBridge: The number of registry values '%s' and '%s' under '%s' must sum to be less than %d.\r\n",
                PCIBUS_MEMLEN_VALNAME, PCIBUS_IOLEN_VALNAME, Info.RegPath, PCI_TYPE1_ADDRESSES));

            return FALSE;
        }
    }

    // If ConfigEntry defined, call it instead of CheckBARs
    if (Info.ConfigEntry) {
        DWORD Status;
        
        // Load driver Dll and obtain ConfigEntry function address
        if (!LoadConfigEntry(&Info)) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgBridge: Failed LoadConfigEntry call\r\n"));

            return FALSE;
        }

        Status = ((PFN_CONFIGENTRY)(Info.ConfigInfo->ConfigEntryFn))(PCIBUS_CONFIG_RSRC, &Info, g_MemHead, g_IoHead, pMemSize, pIoSize);
        
        if (Status != ERROR_SUCCESS) {
            if (Status == ERROR_NOT_SUPPORTED) {
                // ConfigEntry point doesn't support this type of call, so do it here
                // Read the base address registers to determine space to allocate
                if (!PCICfgCheckBARs(&Info, pMemSize, pIoSize)) {
                    DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgBridge: Failed PCICfgCheckBARs call\r\n"));
                    
                    return FALSE;
                }
            } else {
                DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgBridge: Failed %s:%s call\r\n", Info.DllName, Info.ConfigEntryName));

                return FALSE;
            }
        }
    } else {
        // Read the base address registers to determine space to allocate
        if (!PCICfgCheckBARs(&Info, pMemSize, pIoSize)) {
            DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCICfgBridge: Failed PCICfgCheckBARs call\r\n"));
            
            return FALSE;
        }
    }

    // If bridge not already placed, configure interrupt
    if (!(pCfg->Command & (PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE))) {
        DEVICE_LOCATION DevLoc;
        DWORD Irq = 0xFF;

        Reg = PCIConfig_Read(Bus, Device, Function, PCI_CONFIG_INTERRUPT);
        
        // Fill in device location structure
        DevLoc.IfcType = PCIBus;
        DevLoc.BusNumber = Bus;
        DevLoc.LogicalLoc = (Bus << 16) | (Device << 8) | Function;
        DevLoc.Pin = ((PPCI_INT)(&Reg))->InterruptPin;

        if (DevLoc.Pin) {
            // Interrupts used, get IRQ from OAL
            if (!RequestIrq(&DevLoc, &Irq)) {
                DEBUGMSG(ZONE_INIT, (L"PCIBUS!PCICfgBridge: Get IRQ call failed.\r\n"));

                // No interrupts
                Irq = 0xFF;
            } else if (Irq > 254) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -