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