]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Metronome/Metronome.c
MdeModulePkg: Clean up source files
[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 - 2018, Intel Corporation. All rights reserved.<BR>
15 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 "Metronome.h"
26
27 //
28 // Handle for the Metronome Architectural Protocol instance produced by this driver
29 //
30 EFI_HANDLE mMetronomeHandle = NULL;
31
32 //
33 // The Metronome Architectural Protocol instance produced by this driver
34 //
35 EFI_METRONOME_ARCH_PROTOCOL mMetronome = {
36 WaitForTick,
37 1 // TickPeriod = 1*100 ns units
38 };
39
40 /**
41 Waits for the specified number of ticks.
42
43 This function implements EFI_METRONOME_ARCH_PROTOCOL.WaitForTick().
44 The WaitForTick() function waits for the number of ticks specified by
45 TickNumber from a known time source in the platform. If TickNumber of
46 ticks are detected, then EFI_SUCCESS is returned. The actual time passed
47 between entry of this function and the first tick is between 0 and
48 TickPeriod 100 nS units. If you want to guarantee that at least TickPeriod
49 time has elapsed, wait for two ticks. This function waits for a hardware
50 event to determine when a tick occurs. It is possible for interrupt
51 processing, or exception processing to interrupt the execution of the
52 WaitForTick() function. Depending on the hardware source for the ticks, it
53 is possible for a tick to be missed. This function cannot guarantee that
54 ticks will not be missed. If a timeout occurs waiting for the specified
55 number of ticks, then EFI_TIMEOUT is returned.
56
57 @param This The EFI_METRONOME_ARCH_PROTOCOL instance.
58 @param TickNumber Number of ticks to wait.
59
60 @retval EFI_SUCCESS The wait for the number of ticks specified by TickNumber
61 succeeded.
62 @retval EFI_TIMEOUT A timeout occurred waiting for the specified number of ticks.
63
64 **/
65 EFI_STATUS
66 EFIAPI
67 WaitForTick (
68 IN EFI_METRONOME_ARCH_PROTOCOL *This,
69 IN UINT32 TickNumber
70 )
71 {
72 //
73 // Check the value of TickNumber, so a 32-bit overflow can be avoided
74 // when TickNumber of converted to nanosecond units
75 //
76 if (TickNumber < 10000000) {
77 //
78 // If TickNumber is small, then use NanoSecondDelay()
79 //
80 NanoSecondDelay (TickNumber * 100);
81 } else {
82 //
83 // If TickNumber is large, then use MicroSecondDelay()
84 //
85 MicroSecondDelay (TickNumber / 10);
86 }
87 return EFI_SUCCESS;
88 }
89
90 /**
91 The user Entry Point for module Metronome. The user code starts with this function.
92
93 @param[in] ImageHandle The firmware allocated handle for the EFI image.
94 @param[in] SystemTable A pointer to the EFI System Table.
95
96 @retval EFI_SUCCESS The entry point is executed successfully.
97 @retval other Some error occurs when executing this entry point.
98
99 **/
100 EFI_STATUS
101 EFIAPI
102 InstallMetronome (
103 IN EFI_HANDLE ImageHandle,
104 IN EFI_SYSTEM_TABLE *SystemTable
105 )
106 {
107 EFI_STATUS Status;
108
109 //
110 // Make sure the Metronome Architectural Protocol is not already installed in the system
111 //
112 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMetronomeArchProtocolGuid);
113
114 //
115 // Install on a new handle
116 //
117 Status = gBS->InstallMultipleProtocolInterfaces (
118 &mMetronomeHandle,
119 &gEfiMetronomeArchProtocolGuid, &mMetronome,
120 NULL
121 );
122 ASSERT_EFI_ERROR (Status);
123
124 return Status;
125 }