blob: ec2f1d72118786b8180e57e75c2f3ba3c2a97b67 [file] [log] [blame]
Martin Roth6add44b2017-02-09 17:51:20 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Aaron Durbin01dfdc52016-04-08 21:28:11 -050014#include <arch/early_variables.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000015#include <console/console.h>
16#include <arch/io.h>
17#include <cpu/x86/msr.h>
18#include <cpu/x86/tsc.h>
19#include <smp/spinlock.h>
20#include <delay.h>
Aaron Durbin38c326d2013-05-06 12:22:23 -050021#include <thread.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000022
Aaron Durbin01dfdc52016-04-08 21:28:11 -050023static unsigned long clocks_per_usec CAR_GLOBAL;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000024
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000025#define CLOCK_TICK_RATE 1193180U /* Underlying HZ */
26
Stefan Reinauer14e22772010-04-27 06:56:47 +000027/* ------ Calibrate the TSC -------
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000028 * Too much 64-bit arithmetic here to do this cleanly in C, and for
29 * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
30 * output busy loop as low as possible. We avoid reading the CTC registers
31 * directly because of the awkward 8-bit access mechanism of the 82C54
32 * device.
33 */
34
Kevin O'Connor3e4daf12010-09-07 07:53:26 +000035#define CALIBRATE_INTERVAL ((2*CLOCK_TICK_RATE)/1000) /* 2ms */
36#define CALIBRATE_DIVISOR (2*1000) /* 2ms / 2000 == 1usec */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000037
Aaron Durbinb8671ea2016-04-08 21:41:01 -050038static unsigned long calibrate_tsc_with_pit(void)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000039{
40 /* Set the Gate high, disable speaker */
41 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
42
43 /*
44 * Now let's take care of CTC channel 2
45 *
46 * Set the Gate high, program CTC channel 2 for mode 0,
47 * (interrupt on terminal count mode), binary count,
48 * load 5 * LATCH count, (LSB and MSB) to begin countdown.
49 */
Lee Leahyc5917072017-03-15 16:38:51 -070050 outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
51
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000052 outb(CALIBRATE_INTERVAL & 0xff, 0x42); /* LSB of count */
53 outb(CALIBRATE_INTERVAL >> 8, 0x42); /* MSB of count */
54
55 {
56 tsc_t start;
57 tsc_t end;
58 unsigned long count;
59
60 start = rdtsc();
61 count = 0;
62 do {
63 count++;
64 } while ((inb(0x61) & 0x20) == 0);
65 end = rdtsc();
66
67 /* Error: ECTCNEVERSET */
68 if (count <= 1)
69 goto bad_ctc;
70
71 /* 64-bit subtract - gcc just messes up with long longs */
72 __asm__("subl %2,%0\n\t"
73 "sbbl %3,%1"
Lee Leahy8bad6d22017-03-15 14:15:38 -070074 : "=a" (end.lo), "=d" (end.hi)
75 : "g" (start.lo), "g" (start.hi),
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000076 "0" (end.lo), "1" (end.hi));
77
78 /* Error: ECPUTOOFAST */
79 if (end.hi)
80 goto bad_ctc;
81
82
83 /* Error: ECPUTOOSLOW */
84 if (end.lo <= CALIBRATE_DIVISOR)
85 goto bad_ctc;
86
Edward O'Callaghan7116ac82014-07-08 01:53:24 +100087 return CEIL_DIV(end.lo, CALIBRATE_DIVISOR);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000088 }
89
90 /*
91 * The CTC wasn't reliable: we got a hit on the very first read,
92 * or the CPU was so fast/slow that the quotient wouldn't fit in
93 * 32 bits..
94 */
95bad_ctc:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000096 printk(BIOS_ERR, "bad_ctc\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000097 return 0;
98}
99
Aaron Durbinb8671ea2016-04-08 21:41:01 -0500100static unsigned long calibrate_tsc(void)
101{
102 if (IS_ENABLED(CONFIG_TSC_CONSTANT_RATE))
103 return tsc_freq_mhz();
104 else
105 return calibrate_tsc_with_pit();
106}
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000107
108void init_timer(void)
109{
Aaron Durbin01dfdc52016-04-08 21:28:11 -0500110 if (!car_get_var(clocks_per_usec))
111 car_set_var(clocks_per_usec, calibrate_tsc());
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000112}
113
Aaron Durbin8e73b5d2013-05-01 15:27:09 -0500114static inline unsigned long get_clocks_per_usec(void)
115{
116 init_timer();
Aaron Durbin01dfdc52016-04-08 21:28:11 -0500117 return car_get_var(clocks_per_usec);
Aaron Durbin8e73b5d2013-05-01 15:27:09 -0500118}
Aaron Durbin8e73b5d2013-05-01 15:27:09 -0500119
Lee Leahy8ca9a212017-03-15 14:55:05 -0700120void udelay(unsigned int us)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000121{
Aaron Durbin703aa972013-05-01 15:55:14 -0500122 unsigned long long start;
123 unsigned long long current;
124 unsigned long long clocks;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000125
Aaron Durbin38c326d2013-05-06 12:22:23 -0500126 if (!thread_yield_microseconds(us))
127 return;
128
Aaron Durbin703aa972013-05-01 15:55:14 -0500129 start = rdtscll();
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000130 clocks = us;
Aaron Durbin8e73b5d2013-05-01 15:27:09 -0500131 clocks *= get_clocks_per_usec();
Aaron Durbin703aa972013-05-01 15:55:14 -0500132 current = rdtscll();
Elyes HAOUAScbe7464c2016-08-23 21:07:28 +0200133 while ((current - start) < clocks) {
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000134 cpu_relax();
Aaron Durbin703aa972013-05-01 15:55:14 -0500135 current = rdtscll();
136 }
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000137}
Aaron Durbine8501642013-04-29 22:22:55 -0500138
Martin Roth0fa92b32017-06-24 13:53:20 -0600139#if IS_ENABLED(CONFIG_TSC_MONOTONIC_TIMER)
Aaron Durbine8501642013-04-29 22:22:55 -0500140#include <timer.h>
141
142static struct monotonic_counter {
143 int initialized;
144 struct mono_time time;
145 uint64_t last_value;
Aaron Durbin01dfdc52016-04-08 21:28:11 -0500146} mono_counter_g CAR_GLOBAL;
Aaron Durbin711bfa92016-04-08 21:12:01 -0500147
148static inline struct monotonic_counter *get_monotonic_context(void)
149{
Aaron Durbin01dfdc52016-04-08 21:28:11 -0500150 return car_get_var_ptr(&mono_counter_g);
Aaron Durbin711bfa92016-04-08 21:12:01 -0500151}
Aaron Durbine8501642013-04-29 22:22:55 -0500152
153void timer_monotonic_get(struct mono_time *mt)
154{
155 uint64_t current_tick;
156 uint64_t ticks_elapsed;
Aaron Durbin711bfa92016-04-08 21:12:01 -0500157 unsigned long ticks_per_usec;
158 struct monotonic_counter *mono_counter;
Aaron Durbine8501642013-04-29 22:22:55 -0500159
Aaron Durbin711bfa92016-04-08 21:12:01 -0500160 mono_counter = get_monotonic_context();
161 if (!mono_counter->initialized) {
Aaron Durbine8501642013-04-29 22:22:55 -0500162 init_timer();
Aaron Durbin711bfa92016-04-08 21:12:01 -0500163 mono_counter->last_value = rdtscll();
164 mono_counter->initialized = 1;
Aaron Durbine8501642013-04-29 22:22:55 -0500165 }
166
167 current_tick = rdtscll();
Aaron Durbin711bfa92016-04-08 21:12:01 -0500168 ticks_elapsed = current_tick - mono_counter->last_value;
169 ticks_per_usec = get_clocks_per_usec();
Aaron Durbine8501642013-04-29 22:22:55 -0500170
171 /* Update current time and tick values only if a full tick occurred. */
Aaron Durbin711bfa92016-04-08 21:12:01 -0500172 if (ticks_elapsed >= ticks_per_usec) {
Aaron Durbine8501642013-04-29 22:22:55 -0500173 uint64_t usecs_elapsed;
174
Aaron Durbin711bfa92016-04-08 21:12:01 -0500175 usecs_elapsed = ticks_elapsed / ticks_per_usec;
176 mono_time_add_usecs(&mono_counter->time, (long)usecs_elapsed);
177 mono_counter->last_value = current_tick;
Aaron Durbine8501642013-04-29 22:22:55 -0500178 }
179
180 /* Save result. */
Aaron Durbin711bfa92016-04-08 21:12:01 -0500181 *mt = mono_counter->time;
Aaron Durbine8501642013-04-29 22:22:55 -0500182}
183#endif