blob: 63500a4d264fe0e8b0f8bcc56a6e97859ed1391f [file] [log] [blame]
Aaron Durbinc46cc6f2013-04-29 16:57:10 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbinc46cc6f2013-04-29 16:57:10 -050014 */
15#include <stdint.h>
16#include <cpu/x86/msr.h>
17#include <timer.h>
18
19#define MSR_COUNTER_24_MHz 0x637
20static struct monotonic_counter {
21 int initialized;
22 struct mono_time time;
23 uint32_t last_value;
24} mono_counter;
25
26static inline uint32_t read_counter_msr(void)
27{
28 /* Even though the MSR is 64-bit it is assumed that the hardware
29 * is polled frequently enough to only use the lower 32-bits. */
30 msr_t counter_msr;
31
32 counter_msr = rdmsr(MSR_COUNTER_24_MHz);
33
34 return counter_msr.lo;
35}
36
37void timer_monotonic_get(struct mono_time *mt)
38{
39 uint32_t current_tick;
40 uint32_t usecs_elapsed;
41
42 if (!mono_counter.initialized) {
43 mono_counter.last_value = read_counter_msr();
44 mono_counter.initialized = 1;
45 }
46
47 current_tick = read_counter_msr();
48 usecs_elapsed = (current_tick - mono_counter.last_value) / 24;
49
50 /* Update current time and tick values only if a full tick occurred. */
51 if (usecs_elapsed) {
52 mono_time_add_usecs(&mono_counter.time, usecs_elapsed);
53 mono_counter.last_value = current_tick;
54 }
55
56 /* Save result. */
57 *mt = mono_counter.time;
58}