coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
delay_tsc.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <arch/cpu.h>
4 #include <cpu/x86/tsc.h>
5 #include <delay.h>
6 #include <thread.h>
7 
8 void init_timer(void)
9 {
10  (void)tsc_freq_mhz();
11 }
12 
13 void udelay(unsigned int us)
14 {
15  unsigned long long start;
16  unsigned long long current;
17  unsigned long long clocks;
18 
20  return;
21 
22  start = rdtscll();
23  clocks = us;
24  clocks *= tsc_freq_mhz();
25  current = rdtscll();
26  while ((current - start) < clocks) {
27  cpu_relax();
28  current = rdtscll();
29  }
30 }
31 
32 #if CONFIG(TSC_MONOTONIC_TIMER)
33 #include <timer.h>
34 
35 static struct monotonic_counter {
36  int initialized;
37  struct mono_time time;
39 } mono_counter;
40 
41 void timer_monotonic_get(struct mono_time *mt)
42 {
43  uint64_t current_tick;
44  uint64_t ticks_elapsed;
45  unsigned long ticks_per_usec;
46 
48  init_timer();
51  }
52 
53  current_tick = rdtscll();
54  ticks_elapsed = current_tick - mono_counter.last_value;
55  ticks_per_usec = tsc_freq_mhz();
56 
57  /* Update current time and tick values only if a full tick occurred. */
58  if (ticks_elapsed >= ticks_per_usec) {
59  uint64_t usecs_elapsed;
60 
61  usecs_elapsed = ticks_elapsed / ticks_per_usec;
62  mono_time_add_usecs(&mono_counter.time, (long)usecs_elapsed);
63  mono_counter.last_value = current_tick;
64  }
65 
66  /* Save result. */
67  *mt = mono_counter.time;
68 }
69 #endif
static void cpu_relax(void)
Definition: cpu.h:6
void timer_monotonic_get(struct mono_time *mt)
Definition: arch_timer.c:6
void init_timer(void)
Definition: delay_tsc.c:8
void udelay(unsigned int us)
Definition: delay_tsc.c:13
unsigned long tsc_freq_mhz(void)
Definition: fsb.c:118
static void mono_time_add_usecs(struct mono_time *mt, long us)
Definition: timer.h:65
int thread_yield_microseconds(unsigned int microsecs)
Definition: thread.c:342
static struct monotonic_counter mono_counter
unsigned long long uint64_t
Definition: stdint.h:17
uint64_t last_value
Definition: arch_timer.c:17
struct mono_time time
Definition: arch_timer.c:16
static unsigned long long rdtscll(void)
Definition: tsc.h:49
typedef void(X86APIP X86EMU_intrFuncs)(int num)