Home | History | Annotate | Download | only in MetronomeDxe
      1 /** @file
      2 
      3   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
      4   Copyright (c) 2013, ARM Ltd. All rights reserved.
      5 
      6   This program and the accompanying materials
      7   are licensed and made available under the terms and conditions of the BSD License
      8   which accompanies this distribution.  The full text of the license may be found at
      9   http://opensource.org/licenses/bsd-license.php
     10 
     11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     13 
     14 **/
     15 
     16 #include <PiDxe.h>
     17 
     18 #include <Library/BaseLib.h>
     19 #include <Library/DebugLib.h>
     20 #include <Library/BaseMemoryLib.h>
     21 #include <Library/UefiBootServicesTableLib.h>
     22 #include <Library/UefiLib.h>
     23 #include <Library/PcdLib.h>
     24 #include <Library/TimerLib.h>
     25 
     26 #include <Protocol/Metronome.h>
     27 
     28 EFI_STATUS
     29 EFIAPI
     30 WaitForTick (
     31   IN EFI_METRONOME_ARCH_PROTOCOL  *This,
     32   IN UINT32                       TickNumber
     33   );
     34 
     35 /**
     36   Interface structure for the Metronome Architectural Protocol.
     37 
     38   @par Protocol Description:
     39   This protocol provides access to a known time source in the platform to the
     40   core.  The core uses this known time source to produce core services that
     41   require calibrated delays.
     42 
     43   @param WaitForTick
     44   Waits for a specified number of ticks from a known time source
     45   in the platform.  The actual time passed between entry of this
     46   function and the first tick is between 0 and TickPeriod 100 nS
     47   units.  If you want to guarantee that at least TickPeriod time
     48   has elapsed, wait for two ticks.
     49 
     50   @param TickPeriod
     51   The period of platform's known time source in 100 nS units.
     52   This value on any platform must be at least 10 uS, and must not
     53   exceed 200 uS.  The value in this field is a constant that must
     54   not be modified after the Metronome architectural protocol is
     55   installed.  All consumers must treat this as a read-only field.
     56 
     57 **/
     58 EFI_METRONOME_ARCH_PROTOCOL gMetronome = {
     59   WaitForTick,
     60   FixedPcdGet32 (PcdMetronomeTickPeriod)
     61 };
     62 
     63 
     64 /**
     65   The WaitForTick() function waits for the number of ticks specified by
     66   TickNumber from a known time source in the platform.  If TickNumber of
     67   ticks are detected, then EFI_SUCCESS is returned.  The actual time passed
     68   between entry of this function and the first tick is between 0 and
     69   TickPeriod 100 nS units.  If you want to guarantee that at least TickPeriod
     70   time has elapsed, wait for two ticks.  This function waits for a hardware
     71   event to determine when a tick occurs.  It is possible for interrupt
     72   processing, or exception processing to interrupt the execution of the
     73   WaitForTick() function.  Depending on the hardware source for the ticks, it
     74   is possible for a tick to be missed.  This function cannot guarantee that
     75   ticks will not be missed.  If a timeout occurs waiting for the specified
     76   number of ticks, then EFI_TIMEOUT is returned.
     77 
     78   @param  This             The EFI_METRONOME_ARCH_PROTOCOL instance.
     79   @param  TickNumber       Number of ticks to wait.
     80 
     81   @retval EFI_SUCCESS           The wait for the number of ticks specified by TickNumber
     82                                 succeeded.
     83   @retval EFI_TIMEOUT           A timeout occurred waiting for the specified number of ticks.
     84 
     85 **/
     86 EFI_STATUS
     87 EFIAPI
     88 WaitForTick (
     89   IN EFI_METRONOME_ARCH_PROTOCOL  *This,
     90   IN UINT32                       TickNumber
     91   )
     92 {
     93   //
     94   // Compute how long to stall the CPU.
     95   // gMetronome.TickPeriod is in 100 ns units so it needs to be divided by 10
     96   // to get it in microseconds units.
     97   //
     98   MicroSecondDelay (TickNumber * gMetronome.TickPeriod / 10);
     99   return EFI_SUCCESS;
    100 }
    101 
    102 
    103 EFI_HANDLE  gMetronomeHandle = NULL;
    104 
    105 
    106 
    107 /**
    108   Initialize the state information for the CPU Architectural Protocol
    109 
    110   @param  ImageHandle   of the loaded driver
    111   @param  SystemTable   Pointer to the System Table
    112 
    113   @retval EFI_SUCCESS           Protocol registered
    114   @retval EFI_OUT_OF_RESOURCES  Cannot allocate protocol data structure
    115   @retval EFI_DEVICE_ERROR      Hardware problems
    116 
    117 **/
    118 EFI_STATUS
    119 MetronomeInitialize (
    120   IN EFI_HANDLE         ImageHandle,
    121   IN EFI_SYSTEM_TABLE   *SystemTable
    122   )
    123 {
    124   EFI_STATUS  Status;
    125 
    126   //
    127   // Do any hardware init required to make WaitForTick () to work here.
    128   //
    129 
    130   Status = gBS->InstallMultipleProtocolInterfaces (
    131                   &gMetronomeHandle,
    132                   &gEfiMetronomeArchProtocolGuid,   &gMetronome,
    133                   NULL
    134                   );
    135   ASSERT_EFI_ERROR (Status);
    136 
    137   return Status;
    138 }
    139 
    140