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