Skip to content

Commit 55f5297

Browse files
mingqiangchiwenlingz
authored andcommitted
hv:move several tsc APIs to timer.c
-- change 'tsc_khz' to static -- move these APIs from rtl.h to timer.c us_to_ticks() ticks_to_us() ticks_to_ms() rdtsc() Tracked-On: #1842 Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 36f6a41 commit 55f5297

File tree

5 files changed

+87
-34
lines changed

5 files changed

+87
-34
lines changed

doc/developer-guides/hld/hv-timer.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,17 @@ Interfaces Design
4545
.. doxygenfunction:: calibrate_tsc
4646
:project: Project ACRN
4747

48+
.. doxygenfunction:: us_to_ticks
49+
:project: Project ACRN
50+
51+
.. doxygenfunction:: ticks_to_us
52+
:project: Project ACRN
53+
54+
.. doxygenfunction:: ticks_to_ms
55+
:project: Project ACRN
56+
57+
.. doxygenfunction:: rdtsc
58+
:project: Project ACRN
59+
60+
.. doxygenfunction:: get_tsc_khz
61+
:project: Project ACRN

hypervisor/arch/x86/guest/vcpuid.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void init_vcpuid_entry(uint32_t leaf, uint32_t subleaf,
139139
cpuid_subleaf(leaf, subleaf, &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
140140
} else {
141141
/* Use the tsc to derive the emulated 0x16U cpuid. */
142-
entry->eax = (uint32_t) (tsc_khz / 1000U);
142+
entry->eax = (uint32_t) (get_tsc_khz() / 1000U);
143143
entry->ebx = entry->eax;
144144
/* Bus frequency: hard coded to 100M */
145145
entry->ecx = 100U;
@@ -178,7 +178,7 @@ static void init_vcpuid_entry(uint32_t leaf, uint32_t subleaf,
178178
* EBX, ECX, EDX: RESERVED (reserved fields are set to zero).
179179
*/
180180
case 0x40000010U:
181-
entry->eax = tsc_khz;
181+
entry->eax = get_tsc_khz();
182182
entry->ebx = 0U;
183183
entry->ecx = 0U;
184184
entry->edx = 0U;

hypervisor/arch/x86/timer.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616
#define CAL_MS 10U
1717
#define MIN_TIMER_PERIOD_US 500U
1818

19-
uint32_t tsc_khz = 0U;
19+
static uint32_t tsc_khz = 0U;
20+
21+
uint64_t rdtsc(void)
22+
{
23+
uint32_t lo, hi;
24+
25+
asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
26+
return ((uint64_t)hi << 32U) | lo;
27+
}
2028

2129
static void run_timer(const struct hv_timer *timer)
2230
{
@@ -283,3 +291,28 @@ void calibrate_tsc(void)
283291
tsc_khz = (uint32_t)(tsc_hz / 1000UL);
284292
printf("%s, tsc_khz=%lu\n", __func__, tsc_khz);
285293
}
294+
295+
uint32_t get_tsc_khz(void)
296+
{
297+
return tsc_khz;
298+
}
299+
300+
/**
301+
* Frequency of TSC in KHz (where 1KHz = 1000Hz). Only valid after
302+
* calibrate_tsc() returns.
303+
*/
304+
305+
uint64_t us_to_ticks(uint32_t us)
306+
{
307+
return (((uint64_t)us * (uint64_t)tsc_khz) / 1000UL);
308+
}
309+
310+
uint64_t ticks_to_us(uint64_t ticks)
311+
{
312+
return (ticks * 1000UL) / (uint64_t)tsc_khz;
313+
}
314+
315+
uint64_t ticks_to_ms(uint64_t ticks)
316+
{
317+
return ticks / (uint64_t)tsc_khz;
318+
}

hypervisor/include/arch/x86/timer.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#define TIMER_H
99

1010
#include <list.h>
11-
#include <rtl.h>
1211

1312
/**
1413
* @brief Timer
@@ -48,6 +47,36 @@ struct hv_timer {
4847

4948
/* External Interfaces */
5049

50+
#define CYCLES_PER_MS us_to_ticks(1000U)
51+
52+
/**
53+
* @brief convert us to ticks.
54+
*
55+
* @return ticks
56+
*/
57+
uint64_t us_to_ticks(uint32_t us);
58+
59+
/**
60+
* @brief convert ticks to us.
61+
*
62+
* @return microsecond
63+
*/
64+
uint64_t ticks_to_us(uint64_t ticks);
65+
66+
/**
67+
* @brief convert ticks to ms.
68+
*
69+
* @return millisecond
70+
*/
71+
uint64_t ticks_to_ms(uint64_t ticks);
72+
73+
/**
74+
* @brief read tsc.
75+
*
76+
* @return tsc value
77+
*/
78+
uint64_t rdtsc(void);
79+
5180
/**
5281
* @brief Initialize a timer structure.
5382
*
@@ -138,6 +167,13 @@ void timer_init(void);
138167
*/
139168
void calibrate_tsc(void);
140169

170+
/**
171+
* @brief Get tsc.
172+
*
173+
* @return tsc(KHz)
174+
*/
175+
uint32_t get_tsc_khz(void);
176+
141177
/**
142178
* @}
143179
*/

hypervisor/include/lib/rtl.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,4 @@ uint64_t strtoul_hex(const char *nptr);
4343
char *strstr_s(const char *str1, size_t maxlen1,
4444
const char *str2, size_t maxlen2);
4545

46-
/**
47-
* Frequency of TSC in KHz (where 1KHz = 1000Hz). Only valid after
48-
* calibrate_tsc() returns.
49-
*/
50-
extern uint32_t tsc_khz;
51-
52-
static inline uint64_t us_to_ticks(uint32_t us)
53-
{
54-
return (((uint64_t)us * (uint64_t)tsc_khz) / 1000UL);
55-
}
56-
57-
#define CYCLES_PER_MS us_to_ticks(1000U)
58-
59-
static inline uint64_t ticks_to_us(uint64_t ticks)
60-
{
61-
return (ticks * 1000UL) / (uint64_t)tsc_khz;
62-
}
63-
64-
static inline uint64_t ticks_to_ms(uint64_t ticks)
65-
{
66-
return ticks / (uint64_t)tsc_khz;
67-
}
68-
69-
static inline uint64_t rdtsc(void)
70-
{
71-
uint32_t lo, hi;
72-
73-
asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
74-
return ((uint64_t)hi << 32U) | lo;
75-
}
7646
#endif /* RTL_H */

0 commit comments

Comments
 (0)