]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/MetronomeDxe/Metronome.c
EmbeddedPkg: Apply uncrustify changes
[mirror_edk2.git] / EmbeddedPkg / MetronomeDxe / Metronome.c
1 /** @file
2
3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2013, ARM Ltd. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiDxe.h>
11
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16 #include <Library/UefiLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/TimerLib.h>
19
20 #include <Protocol/Metronome.h>
21
22 EFI_STATUS
23 EFIAPI
24 WaitForTick (
25 IN EFI_METRONOME_ARCH_PROTOCOL *This,
26 IN UINT32 TickNumber
27 );
28
29 /**
30 Interface structure for the Metronome Architectural Protocol.
31
32 @par Protocol Description:
33 This protocol provides access to a known time source in the platform to the
34 core. The core uses this known time source to produce core services that
35 require calibrated delays.
36
37 @param WaitForTick
38 Waits for a specified number of ticks from a known time source
39 in the platform. The actual time passed between entry of this
40 function and the first tick is between 0 and TickPeriod 100 nS
41 units. If you want to guarantee that at least TickPeriod time
42 has elapsed, wait for two ticks.
43
44 @param TickPeriod
45 The period of platform's known time source in 100 nS units.
46 This value on any platform must be at least 10 uS, and must not
47 exceed 200 uS. The value in this field is a constant that must
48 not be modified after the Metronome architectural protocol is
49 installed. All consumers must treat this as a read-only field.
50
51 **/
52 EFI_METRONOME_ARCH_PROTOCOL gMetronome = {
53 WaitForTick,
54 FixedPcdGet32 (PcdMetronomeTickPeriod)
55 };
56
57 /**
58 The WaitForTick() function waits for the number of ticks specified by
59 TickNumber from a known time source in the platform. If TickNumber of
60 ticks are detected, then EFI_SUCCESS is returned. The actual time passed
61 between entry of this function and the first tick is between 0 and
62 TickPeriod 100 nS units. If you want to guarantee that at least TickPeriod
63 time has elapsed, wait for two ticks. This function waits for a hardware
64 event to determine when a tick occurs. It is possible for interrupt
65 processing, or exception processing to interrupt the execution of the
66 WaitForTick() function. Depending on the hardware source for the ticks, it
67 is possible for a tick to be missed. This function cannot guarantee that
68 ticks will not be missed. If a timeout occurs waiting for the specified
69 number of ticks, then EFI_TIMEOUT is returned.
70
71 @param This The EFI_METRONOME_ARCH_PROTOCOL instance.
72 @param TickNumber Number of ticks to wait.
73
74 @retval EFI_SUCCESS The wait for the number of ticks specified by TickNumber
75 succeeded.
76 @retval EFI_TIMEOUT A timeout occurred waiting for the specified number of ticks.
77
78 **/
79 EFI_STATUS
80 EFIAPI
81 WaitForTick (
82 IN EFI_METRONOME_ARCH_PROTOCOL *This,
83 IN UINT32 TickNumber
84 )
85 {
86 //
87 // Compute how long to stall the CPU.
88 // gMetronome.TickPeriod is in 100 ns units so it needs to be divided by 10
89 // to get it in microseconds units.
90 //
91 MicroSecondDelay (TickNumber * gMetronome.TickPeriod / 10);
92 return EFI_SUCCESS;
93 }
94
95 EFI_HANDLE gMetronomeHandle = NULL;
96
97 /**
98 Initialize the state information for the CPU Architectural Protocol
99
100 @param ImageHandle of the loaded driver
101 @param SystemTable Pointer to the System Table
102
103 @retval EFI_SUCCESS Protocol registered
104 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
105 @retval EFI_DEVICE_ERROR Hardware problems
106
107 **/
108 EFI_STATUS
109 EFIAPI
110 MetronomeInitialize (
111 IN EFI_HANDLE ImageHandle,
112 IN EFI_SYSTEM_TABLE *SystemTable
113 )
114 {
115 EFI_STATUS Status;
116
117 //
118 // Do any hardware init required to make WaitForTick () to work here.
119 //
120
121 Status = gBS->InstallMultipleProtocolInterfaces (
122 &gMetronomeHandle,
123 &gEfiMetronomeArchProtocolGuid,
124 &gMetronome,
125 NULL
126 );
127 ASSERT_EFI_ERROR (Status);
128
129 return Status;
130 }