summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Universal/TimestampDxe/TimestampDxe.c
blob: bce2d8c0acd9905d0a6cdbfa09540f7d898abd0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/** @file
  Implementation of Timestamp Protocol using UEFI APIs.

Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/TimerLib.h>
#include <Library/BaseMemoryLib.h>
#include <Protocol/Timestamp.h>

//
// The StartValue in TimerLib
//
UINT64 mTimerLibStartValue = 0;

//
// The EndValue in TimerLib
//
UINT64 mTimerLibEndValue = 0;

//
// The properties of timestamp
//
EFI_TIMESTAMP_PROPERTIES mTimestampProperties = {
  0,
  0
};

/**
  Retrieves the current value of a 64-bit free running timestamp counter.

  The counter shall count up in proportion to the amount of time that has passed. The counter value
  will always roll over to zero. The properties of the counter can be retrieved from GetProperties().
  The caller should be prepared for the function to return the same value twice across successive calls.
  The counter value will not go backwards other than when wrapping, as defined by EndValue in GetProperties().
  The frequency of the returned timestamp counter value must remain constant. Power management operations that
  affect clocking must not change the returned counter frequency. The quantization of counter value updates may
  vary as long as the value reflecting time passed remains consistent.

  @retval The current value of the free running timestamp counter.

**/
UINT64
EFIAPI
TimestampDriverGetTimestamp (
  VOID
  )
{
  //
  // The timestamp of Timestamp Protocol
  //
  UINT64  TimestampValue;
  TimestampValue = 0;

  //
  // Get the timestamp
  //
  if (mTimerLibStartValue > mTimerLibEndValue) {
    TimestampValue = mTimerLibStartValue - GetPerformanceCounter();
  } else {
    TimestampValue = GetPerformanceCounter() - mTimerLibStartValue;
  }

  return TimestampValue;
}

/**
  Obtains timestamp counter properties including frequency and value limits.

  @param[out]  Properties              The properties of the timestamp counter.

  @retval      EFI_SUCCESS             The properties were successfully retrieved.
  @retval      EFI_DEVICE_ERROR        An error occurred trying to retrieve the properties of the timestamp
                                       counter subsystem. Properties is not pedated.
  @retval      EFI_INVALID_PARAMETER   Properties is NULL.

**/
EFI_STATUS
EFIAPI
TimestampDriverGetProperties(
  OUT   EFI_TIMESTAMP_PROPERTIES       *Properties
  )
{
  if (Properties == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Get timestamp properties
  //
  CopyMem((VOID *) Properties, (VOID *) &mTimestampProperties, sizeof (mTimestampProperties));

  return EFI_SUCCESS;
}

//
// The Timestamp Protocol instance produced by this driver
//
EFI_TIMESTAMP_PROTOCOL  mTimestamp = {
  TimestampDriverGetTimestamp,
  TimestampDriverGetProperties
};

/**
  Entry point of the Timestamp Protocol driver.

  @param  ImageHandle   The image handle of this driver.
  @param  SystemTable   The pointer of EFI_SYSTEM_TABLE.

  @retval EFI_SUCCESS   Watchdog Timer Architectural Protocol successfully installed.

**/
EFI_STATUS
EFIAPI
TimestampDriverInitialize (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

  EFI_HANDLE  TimestampHandle;
  TimestampHandle = NULL;

  //
  // Get the start value, end value and frequency in Timerlib
  //
  mTimestampProperties.Frequency = GetPerformanceCounterProperties(&mTimerLibStartValue, &mTimerLibEndValue);

  //
  // Set the EndValue
  //
  if (mTimerLibEndValue > mTimerLibStartValue) {
    mTimestampProperties.EndValue = mTimerLibEndValue - mTimerLibStartValue;
  } else {
    mTimestampProperties.EndValue = mTimerLibStartValue - mTimerLibEndValue;
  }

  DEBUG ((EFI_D_INFO, "TimerFrequency:0x%lx, TimerLibStartTime:0x%lx, TimerLibEndtime:0x%lx\n", mTimestampProperties.Frequency, mTimerLibStartValue, mTimerLibEndValue));

  //
  // Install the Timestamp Protocol onto a new handle
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &TimestampHandle,
                  &gEfiTimestampProtocolGuid,
                  &mTimestamp,
                  NULL
                  );

  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}