]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Metronome/Metronome.c
add Metronome.inf into MdeModulePkg.dsc.
[mirror_edk2.git] / MdeModulePkg / Universal / Metronome / Metronome.c
1 /** @file
2 Produces the Metronome Architectural Protocol on top of Timer Library.
3
4 This is a generic implementation of the Metronome Architectural Protocol that
5 layers on top of an instance of the Timer Library. The Timer Library provides
6 functions for nanosecond and microsecond delays. This generic implementation
7 produces a fixed TickPeriod of 1 100ns unit, and when the WaitForTick() service
8 is called, the number of ticks passed in is converted to either nanosecond or
9 microsecond units. If the number of ticks is small, then nanoseconds are used.
10 If the number of ticks is large, then microseconds are used. This prevents
11 overflows that could occur for long delays if only nanoseconds were used and also
12 provides the greatest accuracy for small delays.
13
14 Copyright (c) 2008, Intel Corporation
15 All rights reserved. This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
19
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22
23 **/
24
25 #include <PiDxe.h>
26 #include <Protocol/Metronome.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/TimerLib.h>
29 #include <Library/DebugLib.h>
30
31 //
32 // Function Prototypes
33 //
34 EFI_STATUS
35 EFIAPI
36 WaitForTick (
37 IN EFI_METRONOME_ARCH_PROTOCOL *This,
38 IN UINT32 TickNumber
39 );
40
41 //
42 // Handle for the Metronome Architectural Protocol instance produced by this driver
43 //
44 EFI_HANDLE mMetronomeHandle = NULL;
45
46 //
47 // The Metronome Architectural Protocol instance produced by this driver
48 //
49 EFI_METRONOME_ARCH_PROTOCOL mMetronome = {
50 WaitForTick,
51 1 // TickPeriod = 1*100 ns units
52 };
53
54 /**
55 The WaitForTick() function waits for the number of ticks specified by
56 TickNumber from a known time source in the platform. If TickNumber of
57 ticks are detected, then EFI_SUCCESS is returned. The actual time passed
58 between entry of this function and the first tick is between 0 and
59 TickPeriod 100 nS units. If you want to guarantee that at least TickPeriod
60 time has elapsed, wait for two ticks. This function waits for a hardware
61 event to determine when a tick occurs. It is possible for interrupt
62 processing, or exception processing to interrupt the execution of the
63 WaitForTick() function. Depending on the hardware source for the ticks, it
64 is possible for a tick to be missed. This function cannot guarantee that
65 ticks will not be missed. If a timeout occurs waiting for the specified
66 number of ticks, then EFI_TIMEOUT is returned.
67
68 @param This The EFI_METRONOME_ARCH_PROTOCOL instance.
69 @param TickNumber Number of ticks to wait.
70
71 @retval EFI_SUCCESS The wait for the number of ticks specified by TickNumber
72 succeeded.
73 @retval EFI_TIMEOUT A timeout occurred waiting for the specified number of ticks.
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 WaitForTick (
79 IN EFI_METRONOME_ARCH_PROTOCOL *This,
80 IN UINT32 TickNumber
81 )
82 {
83 //
84 // Check the value of TickNumber, so a 32-bit overflow can be avoided
85 // when TickNumber of converted to nanosecond units
86 //
87 if (TickNumber < 10000000) {
88 //
89 // If TickNumber is small, then use NanoSecondDelay()
90 //
91 NanoSecondDelay (TickNumber * 100);
92 } else {
93 //
94 // If TickNumber is large, then use MicroSecondDelay()
95 //
96 MicroSecondDelay (TickNumber / 10);
97 }
98 return EFI_SUCCESS;
99 }
100
101 /**
102 The user Entry Point for module Metronome. The user code starts with this function.
103
104 @param[in] ImageHandle The firmware allocated handle for the EFI image.
105 @param[in] SystemTable A pointer to the EFI System Table.
106
107 @retval EFI_SUCCESS The entry point is executed successfully.
108 @retval other Some error occurs when executing this entry point.
109
110 **/
111 EFI_STATUS
112 EFIAPI
113 InstallMetronome (
114 IN EFI_HANDLE ImageHandle,
115 IN EFI_SYSTEM_TABLE *SystemTable
116 )
117 {
118 EFI_STATUS Status;
119
120 //
121 // Make sure the Metronome Architectural Protocol is not already installed in the system
122 //
123 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMetronomeArchProtocolGuid);
124
125 //
126 // Install on a new handle
127 //
128 Status = gBS->InstallMultipleProtocolInterfaces (
129 &mMetronomeHandle,
130 &gEfiMetronomeArchProtocolGuid, &mMetronome,
131 NULL
132 );
133 ASSERT_EFI_ERROR (Status);
134
135 return Status;
136 }