summaryrefslogtreecommitdiffstats
path: root/arch/efi/efi/clocksource.c
blob: 2f33b43cce54803dae18076d8a89b280e1668628 (plain) (blame)
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
#include <common.h>
#include <efi.h>
#include <mach/efi.h>
#include <clock.h>

#ifdef __x86_64__
uint64_t ticks_read(void)
{
	uint64_t a, d;

	__asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));

	return (d << 32) | a;
}
#else
uint64_t ticks_read(void)
{
	uint64_t val;

	__asm__ volatile ("rdtsc" : "=A" (val));

	return val;
}
#endif

static uint64_t freq;

/* count TSC ticks during a millisecond delay */
static uint64_t ticks_freq(void)
{
	uint64_t ticks_start, ticks_end;

	ticks_start = ticks_read();
	BS->stall(1000);
	ticks_end = ticks_read();

	return (ticks_end - ticks_start) * 1000;
}

static uint64_t efi_clocksource_read(void)
{
	return 1000 * 1000 * ticks_read() / freq;
}

static struct clocksource cs = {
	.read   = efi_clocksource_read,
	.mask   = CLOCKSOURCE_MASK(64),
	.shift  = 0,
};

int efi_clocksource_init(void)
{
	cs.mult = clocksource_hz2mult(1000 * 1000, cs.shift);

	freq = ticks_freq();

	init_clock(&cs);

	return 0;
}