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