isaio.c

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

C
778
字号
/*++

Copyright (c) 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:

  IsaIo.c
  
Abstract:

  The specific part of implementation for EFI_ISA_IO_PROTOCOL. 
  Please refer to the common part: CommonIsaIo.c for both EFI_
  ISA_IO_PROTOCOL and EFI_LIGHT_ISA_IO_PROTOCOl.

--*/

#include "IsaIo.h"

EFI_STATUS
IsaIoVerifyAccess (
  IN     ISA_IO_DEVICE              *IsaIoDevice,
  IN     ISA_ACCESS_TYPE            Type,
  IN     EFI_ISA_IO_PROTOCOL_WIDTH  Width,
  IN     UINTN                      Count,
  IN OUT UINT32                     *Offset
  )
/*++

Routine Description:

  Verifies access to an ISA device

Arguments:

  IsaIoDevice           - The ISA device to be verified.
  Type                  - The Access type.
  Width                 - Signifies the width of the memory operation.
  Count                 - The number of memory operations to perform. 
  Offset                - The offset in ISA memory space to start the memory operation.  
  
Returns:

  EFI_SUCCESS           - Verify success.
  EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
  EFI_UNSUPPORTED       - The device ont support the access type.

--*/
{
  EFI_ISA_ACPI_RESOURCE *Item;
  EFI_STATUS            Status;

  if (Width < EfiIsaIoWidthUint8 ||
      Width >= EfiIsaIoWidthMaximum ||
      Width == EfiIsaIoWidthReserved ||
      Width == EfiIsaIoWidthFifoReserved ||
      Width == EfiIsaIoWidthFillReserved
      ) {
    return EFI_INVALID_PARAMETER;
  }

  if ((Type != IsaAccessTypeMem) && (Type != IsaAccessTypeIo)) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // If Width is EfiIsaIoWidthFifoUintX then convert to EfiIsaIoWidthUintX
  // If Width is EfiIsaIoWidthFillUintX then convert to EfiIsaIoWidthUintX
  //
  if (Width >= EfiIsaIoWidthFifoUint8 && Width <= EfiIsaIoWidthFifoReserved) {
    Count = 1;
  }

  Width &= 0x03;

  Status  = EFI_UNSUPPORTED;
  Item    = IsaIoDevice->IsaIo.ResourceList->ResourceItem;
  while (Item->Type != EfiIsaAcpiResourceEndOfList) {
    if ((Type == IsaAccessTypeMem && Item->Type == EfiIsaAcpiResourceMemory) ||
        (Type == IsaAccessTypeIo && Item->Type == EfiIsaAcpiResourceIo)
        ) {
      if (*Offset >= Item->StartRange && (*Offset + Count * (1 << Width)) - 1 <= Item->EndRange) {
        return EFI_SUCCESS;
      }

      if (*Offset >= Item->StartRange && *Offset <= Item->EndRange) {
        Status = EFI_INVALID_PARAMETER;
      }
    }

    Item++;
  }

  return Status;
}

EFI_STATUS
EFIAPI
IsaIoMemRead (
  IN     EFI_INTERFACE_DEFINITION_FOR_ISA_IO       *This,
  IN     EFI_ISA_IO_PROTOCOL_WIDTH                 Width,
  IN     UINT32                                    Offset,
  IN     UINTN                                     Count,
  IN OUT VOID                                      *Buffer
  )
/*++

Routine Description:

  Performs an ISA Memory Read Cycle

Arguments:

  This                  - A pointer to the EFI_ISA_IO_PROTOCOL instance.
  Width                 - Signifies the width of the memory operation.
  Offset                - The offset in ISA memory space to start the memory operation.  
  Count                 - The number of memory operations to perform. 
  Buffer                - The destination buffer to store the results
  
Returns:

  EFI_SUCCESS           - The data was read from the device successfully.
  EFI_UNSUPPORTED       - The Offset is not valid for this device.
  EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
  EFI_OUT_OF_RESOURCES  - The request could not be completed due to a lack of resources.

--*/
{
  EFI_STATUS    Status;
  ISA_IO_DEVICE *IsaIoDevice;

  IsaIoDevice = ISA_IO_DEVICE_FROM_ISA_IO_THIS (This);

  //
  // Verify the Isa Io Access
  //
  Status = IsaIoVerifyAccess (
             IsaIoDevice,
             IsaAccessTypeMem,
             Width,
             Count,
             &Offset
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Call PciIo->Mem.Read
  //
  Status = IsaIoDevice->PciIo->Mem.Read (
                                     IsaIoDevice->PciIo,
                                     (EFI_PCI_IO_PROTOCOL_WIDTH) Width,
                                     EFI_PCI_IO_PASS_THROUGH_BAR,
                                     Offset,
                                     Count,
                                     Buffer
                                     );

  if (EFI_ERROR (Status)) {
    ReportErrorStatusCode (EFI_IO_BUS_LPC | EFI_IOB_EC_CONTROLLER_ERROR);
  }

  return Status;
}

EFI_STATUS
EFIAPI
IsaIoMemWrite (
  IN     EFI_INTERFACE_DEFINITION_FOR_ISA_IO        *This,
  IN     EFI_ISA_IO_PROTOCOL_WIDTH                  Width,
  IN     UINT32                                     Offset,
  IN     UINTN                                      Count,
  IN OUT VOID                                       *Buffer
  )
/*++

Routine Description:

  Performs an ISA Memory Write Cycle

Arguments:

  This                  - A pointer to the EFI_ISA_IO_PROTOCOL instance.  
  Width                 - Signifies the width of the memory operation.
  Offset                - The offset in ISA memory space to start the memory operation.  
  Count                 - The number of memory operations to perform. 
  Buffer                - The source buffer to write data from

Returns:

  EFI_SUCCESS           - The data was written to the device sucessfully.
  EFI_UNSUPPORTED       - The Offset is not valid for this device.
  EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
  EFI_OUT_OF_RESOURCES  - The request could not be completed due to a lack of resources.

--*/
{
  EFI_STATUS    Status;
  ISA_IO_DEVICE *IsaIoDevice;

  IsaIoDevice = ISA_IO_DEVICE_FROM_ISA_IO_THIS (This);

  //
  // Verify Isa IO Access
  //
  Status = IsaIoVerifyAccess (
             IsaIoDevice,
             IsaAccessTypeMem,
             Width,
             Count,
             &Offset
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Call PciIo->Mem.Write
  //
  Status = IsaIoDevice->PciIo->Mem.Write (
                                     IsaIoDevice->PciIo,
                                     (EFI_PCI_IO_PROTOCOL_WIDTH) Width,
                                     EFI_PCI_IO_PASS_THROUGH_BAR,
                                     Offset,
                                     Count,
                                     Buffer
                                     );

  if (EFI_ERROR (Status)) {
    ReportErrorStatusCode (EFI_IO_BUS_LPC | EFI_IOB_EC_CONTROLLER_ERROR);
  }

  return Status;
}

EFI_STATUS
EFIAPI
IsaIoCopyMem (
  IN EFI_INTERFACE_DEFINITION_FOR_ISA_IO        *This,
  IN EFI_ISA_IO_PROTOCOL_WIDTH                  Width,
  IN UINT32                                     DestOffset,
  IN UINT32                                     SrcOffset,
  IN UINTN                                      Count
  )
/*++

Routine Description:

  Performs an ISA I/O Copy Memory 

Arguments:

  This                  - A pointer to the EFI_ISA_IO_PROTOCOL instance.
  Width                 - Signifies the width of the memory copy operation.
  DestOffset            - The offset of the destination 
  SrcOffset             - The offset of the source
  Count                 - The number of memory copy  operations to perform

Returns:

  EFI_SUCCESS           - The data was copied sucessfully.
  EFI_UNSUPPORTED       - The DestOffset or SrcOffset is not valid for this device.
  EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
  EFI_OUT_OF_RESOURCES  - The request could not be completed due to a lack of resources.

--*/
{
  EFI_STATUS    Status;
  ISA_IO_DEVICE *IsaIoDevice;

  IsaIoDevice = ISA_IO_DEVICE_FROM_ISA_IO_THIS (This);

  //
  // Verify Isa IO Access for destination and source
  //
  Status = IsaIoVerifyAccess (
             IsaIoDevice,
             IsaAccessTypeMem,
             Width,
             Count,
             &DestOffset
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = IsaIoVerifyAccess (
             IsaIoDevice,
             IsaAccessTypeMem,
             Width,
             Count,
             &SrcOffset
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Call PciIo->CopyMem
  //
  Status = IsaIoDevice->PciIo->CopyMem (
                                 IsaIoDevice->PciIo,
                                 (EFI_PCI_IO_PROTOCOL_WIDTH) Width,
                                 EFI_PCI_IO_PASS_THROUGH_BAR,
                                 DestOffset,
                                 EFI_PCI_IO_PASS_THROUGH_BAR,
                                 SrcOffset,
                                 Count
                                 );

  if (EFI_ERROR (Status)) {
    ReportErrorStatusCode (EFI_IO_BUS_LPC | EFI_IOB_EC_CONTROLLER_ERROR);
  }

  return Status;
}

EFI_STATUS
EFIAPI
IsaIoMap (
  IN     EFI_INTERFACE_DEFINITION_FOR_ISA_IO                  *This,
  IN     EFI_ISA_IO_PROTOCOL_OPERATION                        Operation,
  IN     UINT8                                                ChannelNumber         OPTIONAL,
  IN     UINT32                                               ChannelAttributes,
  IN     VOID                                                 *HostAddress,
  IN OUT UINTN                                                *NumberOfBytes,
  OUT    EFI_PHYSICAL_ADDRESS                                 *DeviceAddress,
  OUT    VOID                                                 **Mapping
  )
/*++

Routine Description:

  Maps a memory region for DMA

Arguments:

  This                  - A pointer to the EFI_ISA_IO_PROTOCOL instance.
  Operation             - Indicates the type of DMA (slave or bus master), and if 
                          the DMA operation is going to read or write to system memory. 
  ChannelNumber         - The slave channel number to use for this DMA operation. 
                          If Operation and ChannelAttributes shows that this device 
                          performs bus mastering DMA, then this field is ignored.  
                          The legal range for this field is 0..7.  
  ChannelAttributes     - The attributes of the DMA channel to use for this DMA operation
  HostAddress           - The system memory address to map to the device.  
  NumberOfBytes         - On input the number of bytes to map.  On output the number 
                          of bytes that were mapped.
  DeviceAddress         - The resulting map address for the bus master device to use 
                        - to access the hosts HostAddress.  
  Mapping               - A resulting value to pass to EFI_ISA_IO.Unmap().

Returns:

  EFI_SUCCESS           - The range was mapped for the returned NumberOfBytes.
  EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
  EFI_UNSUPPORTED       - The HostAddress can not be mapped as a common buffer.
  EFI_DEVICE_ERROR      - The system hardware could not map the requested address.
  EFI_OUT_OF_RESOURCES  - The memory pages could not be allocated.

--*/
{
  EFI_STATUS            Status;
  ISA_IO_DEVICE         *IsaIoDevice;
  BOOLEAN               Master;
  BOOLEAN               Read;
  EFI_PHYSICAL_ADDRESS  PhysicalAddress;
  ISA_MAP_INFO          *IsaMapInfo;
  UINT8                 DmaMode;
  UINTN                 MaxNumberOfBytes;
  UINT32                BaseAddress;
  UINT16                Count;

  UINT8                 DmaMask;
  UINT8                 DmaClear;
  UINT8                 DmaChannelMode;

  if ((NULL == This) ||
      (NULL == HostAddress) ||
      (NULL == NumberOfBytes) ||
      (NULL == DeviceAddress) ||
      (NULL == Mapping)
      ) {
    return EFI_INVALID_PARAMETER;
  }

⌨️ 快捷键说明

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