pcilib.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,425 行 · 第 1/3 页

C
1,425
字号
/*++

Copyright (c) 2004 - 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  PciLib.c 
  
Abstract:

  PCI Bus Driver Lib file
  It abstracts some functions that can be different 
  between light PCI bus driver and full PCI bus driver

Revision History

--*/

#include "pcibus.h"

EFI_PCI_HOTPLUG_REQUEST_PROTOCOL gPciHotPlugRequest = {
  PciHotPlugRequestNotify
};


VOID
InstallHotPlugRequestProtocol (
  IN EFI_STATUS *Status
  )
/*++

Routine Description:

Arguments:
  Status   - A pointer to the status.

Returns:

  None

--*/
{
  EFI_HANDLE  Handle;

  Handle = NULL;
  *Status = gBS->InstallProtocolInterface (
                  &Handle,
                  &gEfiPciHotPlugRequestProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &gPciHotPlugRequest
                  );
}

VOID
InstallPciHotplugGuid (
  IN  PCI_IO_DEVICE                  *PciIoDevice
  )
/*++

Routine Description:

Arguments:

  PciIoDevice    -  A pointer to the PCI_IO_DEVICE.

Returns:

  None

--*/
{
  EFI_STATUS  Status;

  if (IS_CARDBUS_BRIDGE (&PciIoDevice->Parent->Pci)) {

    Status = gBS->InstallProtocolInterface (
                    &PciIoDevice->Handle,
                    &gEfiPciHotplugDeviceGuid,
                    EFI_NATIVE_INTERFACE,
                    NULL
                    );
  }
}

VOID
UninstallPciHotplugGuid (
  IN  PCI_IO_DEVICE                  *PciIoDevice
  )
/*++

Routine Description:

Arguments:

  PciIoDevice    - A pointer to the PCI_IO_DEVICE.

Returns:

  None

--*/
{
  EFI_STATUS  Status;

  Status = gBS->OpenProtocol (
                  PciIoDevice->Handle,
                  &gEfiPciHotplugDeviceGuid,
                  NULL,
                  NULL,
                  NULL,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );

  if (Status == EFI_SUCCESS) {
    //
    // This may triger CardBus driver to stop for
    // Pccard devices opened the GUID via BY_DRIVER
    //
    Status = gBS->UninstallProtocolInterface (
                    PciIoDevice->Handle,
                    &gEfiPciHotplugDeviceGuid,
                    NULL
                    );
  }
}

VOID
GetBackPcCardBar (
  IN  PCI_IO_DEVICE                  *PciIoDevice
  )
/*++

Routine Description:


Arguments:

  PciIoDevice      - A pointer to the PCI_IO_DEVICE.

Returns:

  None

--*/
{
  UINT32  Address;

  //
  // Read PciBar information from the bar register
  //
  if (!gFullEnumeration) {

    Address = 0;
    PciIoDevice->PciIo.Pci.Read (
                            &(PciIoDevice->PciIo),
                            EfiPciIoWidthUint32,
                            0x1c,
                            1,
                            &Address
                            );

    (PciIoDevice->PciBar)[P2C_MEM_1].BaseAddress  = (UINT64) (Address);
    (PciIoDevice->PciBar)[P2C_MEM_1].Length       = 0x2000000;
    (PciIoDevice->PciBar)[P2C_MEM_1].BarType      = PciBarTypeMem32;

    Address = 0;
    PciIoDevice->PciIo.Pci.Read (
                            &(PciIoDevice->PciIo),
                            EfiPciIoWidthUint32,
                            0x20,
                            1,
                            &Address
                            );
    (PciIoDevice->PciBar)[P2C_MEM_2].BaseAddress  = (UINT64) (Address);
    (PciIoDevice->PciBar)[P2C_MEM_2].Length       = 0x2000000;
    (PciIoDevice->PciBar)[P2C_MEM_2].BarType      = PciBarTypePMem32;

    Address = 0;
    PciIoDevice->PciIo.Pci.Read (
                            &(PciIoDevice->PciIo),
                            EfiPciIoWidthUint32,
                            0x2c,
                            1,
                            &Address
                            );
    (PciIoDevice->PciBar)[P2C_IO_1].BaseAddress = (UINT64) (Address);
    (PciIoDevice->PciBar)[P2C_IO_1].Length      = 0x100;
    (PciIoDevice->PciBar)[P2C_IO_1].BarType     = PciBarTypeIo16;

    Address = 0;
    PciIoDevice->PciIo.Pci.Read (
                            &(PciIoDevice->PciIo),
                            EfiPciIoWidthUint32,
                            0x34,
                            1,
                            &Address
                            );
    (PciIoDevice->PciBar)[P2C_IO_2].BaseAddress = (UINT64) (Address);
    (PciIoDevice->PciBar)[P2C_IO_2].Length      = 0x100;
    (PciIoDevice->PciBar)[P2C_IO_2].BarType     = PciBarTypeIo16;

  }

  if (gPciHotPlugInit != NULL) {
    GetResourcePaddingForHpb (PciIoDevice);
  }
}

EFI_STATUS
RemoveRejectedPciDevices (
  EFI_HANDLE        RootBridgeHandle,
  IN PCI_IO_DEVICE  *Bridge
  )
/*++

Routine Description:


Arguments:

  RootBridgeHandle   - An efi handle.
  Bridge             - An pointer to the PCI_IO_DEVICE.

Returns:

  None

--*/
// TODO:    EFI_SUCCESS - add return value to function comment
{
  PCI_IO_DEVICE   *Temp;
  EFI_LIST_ENTRY  *CurrentLink;
  EFI_LIST_ENTRY  *LastLink;

  CurrentLink = Bridge->ChildList.ForwardLink;

  while (CurrentLink && CurrentLink != &Bridge->ChildList) {

    Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);

    if (IS_PCI_BRIDGE (&Temp->Pci)) {
      //
      // Remove rejected devices recusively
      //
      RemoveRejectedPciDevices (RootBridgeHandle, Temp);
    } else {
      //
      // Skip rejection for all PPBs, while detect rejection for others
      //
      if (IsPciDeviceRejected (Temp)) {
                
        //
        // For P2C, remove all devices on it
        //
                
        if (!IsListEmpty (&Temp->ChildList)) {
          RemoveAllPciDeviceOnBridge (RootBridgeHandle, Temp);
        }
        
        //
        // Finally remove itself
        //
        
        LastLink = CurrentLink->BackLink;
        RemoveEntryList (CurrentLink);
        FreePciDevice (Temp);

        CurrentLink = LastLink;
      }
    }

    CurrentLink = CurrentLink->ForwardLink;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
PciHostBridgeResourceAllocator (
  IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *PciResAlloc
  )
/*++

Routine Description:

  Host brige resource allocator.

Arguments:

  PciResAlloc  - A pointer to the EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.

Returns:

  EFI Status.

--*/
// TODO:    EFI_NOT_FOUND - add return value to function comment
// TODO:    EFI_NOT_FOUND - add return value to function comment
// TODO:    EFI_NOT_FOUND - add return value to function comment
// TODO:    EFI_SUCCESS - add return value to function comment
{
  PCI_IO_DEVICE                         *RootBridgeDev;
  EFI_HANDLE                            RootBridgeHandle;
  VOID                                  *AcpiConfig;
  EFI_STATUS                            Status;
  UINT64                                IoBase;
  UINT64                                Mem32Base;
  UINT64                                PMem32Base;
  UINT64                                Mem64Base;
  UINT64                                PMem64Base;
  UINT64                                IoResStatus;
  UINT64                                Mem32ResStatus;
  UINT64                                PMem32ResStatus;
  UINT64                                Mem64ResStatus;
  UINT64                                PMem64ResStatus;
  UINT64                                MaxOptionRomSize;
  PCI_RESOURCE_NODE                     *IoBridge;
  PCI_RESOURCE_NODE                     *Mem32Bridge;
  PCI_RESOURCE_NODE                     *PMem32Bridge;
  PCI_RESOURCE_NODE                     *Mem64Bridge;
  PCI_RESOURCE_NODE                     *PMem64Bridge;
  PCI_RESOURCE_NODE                     IoPool;
  PCI_RESOURCE_NODE                     Mem32Pool;
  PCI_RESOURCE_NODE                     PMem32Pool;
  PCI_RESOURCE_NODE                     Mem64Pool;
  PCI_RESOURCE_NODE                     PMem64Pool;
  BOOLEAN                               ReAllocate;
  EFI_DEVICE_HANDLE_EXTENDED_DATA       HandleExtendedData;
  EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA AllocFailExtendedData;

  //
  // Reallocate flag
  //
  ReAllocate = FALSE;

  //
  // It will try several times if the resource allocation fails
  //
  while (TRUE) {

    //
    // Initialize resource pool
    //    
    InitializeResourcePool (&IoPool, PciBarTypeIo16);
    InitializeResourcePool (&Mem32Pool, PciBarTypeMem32);
    InitializeResourcePool (&PMem32Pool, PciBarTypePMem32);
    InitializeResourcePool (&Mem64Pool, PciBarTypeMem64);
    InitializeResourcePool (&PMem64Pool, PciBarTypePMem64);

    RootBridgeDev     = NULL;
    RootBridgeHandle  = 0;

    while (PciResAlloc->GetNextRootBridge (PciResAlloc, &RootBridgeHandle) == EFI_SUCCESS) {

      //
      // Get RootBridg Device by handle
      //
      RootBridgeDev = GetRootBridgeByHandle (RootBridgeHandle);

      if (RootBridgeDev == NULL) {
        return EFI_NOT_FOUND;
      }

      //
      // Create the entire system resource map from the information collected by
      // enumerator. Several resource tree was created
      //

      IoBridge = CreateResourceNode (
                  RootBridgeDev,
                  0,
                  0xFFF,
                  0,
                  PciBarTypeIo16,
                  PciResUsageTypical
                  );

      Mem32Bridge = CreateResourceNode (
                      RootBridgeDev,
                      0,
                      0xFFFFF,
                      0,
                      PciBarTypeMem32,
                      PciResUsageTypical
                      );

      PMem32Bridge = CreateResourceNode (
                      RootBridgeDev,
                      0,
                      0xFFFFF,
                      0,
                      PciBarTypePMem32,
                      PciResUsageTypical
                      );

      Mem64Bridge = CreateResourceNode (
                      RootBridgeDev,
                      0,
                      0xFFFFF,
                      0,
                      PciBarTypeMem64,
                      PciResUsageTypical
                      );

      PMem64Bridge = CreateResourceNode (
                      RootBridgeDev,
                      0,
                      0xFFFFF,
                      0,
                      PciBarTypePMem64,
                      PciResUsageTypical
                      );

      //
      // Create resourcemap by going through all the devices subject to this root bridge
      //
      Status = CreateResourceMap (
                RootBridgeDev,
                IoBridge,
                Mem32Bridge,
                PMem32Bridge,
                Mem64Bridge,
                PMem64Bridge
                );

      //
      // Get the max ROM size that the root bridge can process
      //
      RootBridgeDev->RomSize = Mem32Bridge->Length;

      //
      // Skip to enlarge the resource request during realloction
      //
      if (!ReAllocate) {
        //
        // Get Max Option Rom size for current root bridge
        //
        MaxOptionRomSize = GetMaxOptionRomSize (RootBridgeDev);

        //
        // Enlarger the mem32 resource to accomdate the option rom
        // if the mem32 resource is not enough to hold the rom
        //
        if (MaxOptionRomSize > Mem32Bridge->Length) {

          Mem32Bridge->Length     = MaxOptionRomSize;
          RootBridgeDev->RomSize  = MaxOptionRomSize;

          //
          // Alignment should be adjusted as well
          //
          if (Mem32Bridge->Alignment < MaxOptionRomSize - 1) {
            Mem32Bridge->Alignment = MaxOptionRomSize - 1;
          }
        }
      }    
      
      //
      // Based on the all the resource tree, contruct ACPI resource node to
      // submit the resource aperture to pci host bridge protocol
      //
      Status = ConstructAcpiResourceRequestor (
                RootBridgeDev,
                IoBridge,
                Mem32Bridge,
                PMem32Bridge,
                Mem64Bridge,
                PMem64Bridge,

⌨️ 快捷键说明

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