Check that bus freq is not zero
[akaros.git] / kern / arch / x86 / time.c
1 /* Copyright (c) 2009 The Regents of the University of California
2  * David (Yu) Zhu <yuzhu@cs.berkeley.edu>
3  * Barret Rhoden <brho@cs.berkeley.edu>
4  *
5  * See LICENSE for details. */
6
7 #include <arch/x86.h>
8 #include <arch/arch.h>
9 #include <arch/pic.h>
10 #include <arch/apic.h>
11 #include <time.h>
12 #include <trap.h>
13 #include <assert.h>
14 #include <stdio.h>
15 #include <ros/procinfo.h>
16
17 static uint16_t pit_divisor;
18 static uint8_t pit_mode;
19
20 // timer init calibrates both tsc timer and lapic timer using PIT
21 void timer_init(void){
22         /* some boards have this unmasked early on. */
23         pic_mask_irq(0, 0 + PIC1_OFFSET);
24         uint64_t tscval[2];
25         long timercount[2];
26         pit_set_timer(0xffff, TIMER_RATEGEN);
27         // assume tsc exist
28         tscval[0] = read_tsc();
29         udelay_pit(1000000);
30         tscval[1] = read_tsc();
31         __proc_global_info.tsc_freq = tscval[1] - tscval[0];
32         cprintf("TSC Frequency: %llu\n", __proc_global_info.tsc_freq);
33         __lapic_set_timer(0xffffffff, IdtLAPIC_TIMER, FALSE,
34                           LAPIC_TIMER_DIVISOR_BITS);
35         // Mask the LAPIC Timer, so we never receive this interrupt (minor race)
36         mask_lapic_lvt(MSR_LAPIC_LVT_TIMER);
37         timercount[0] = apicrget(MSR_LAPIC_CURRENT_COUNT);
38         udelay_pit(1000000);
39         timercount[1] = apicrget(MSR_LAPIC_CURRENT_COUNT);
40         __proc_global_info.bus_freq = (timercount[0] - timercount[1])
41                                  * LAPIC_TIMER_DIVISOR_VAL;
42         assert(__proc_global_info.bus_freq);
43         /* The time base for the timer is derived from the processor's bus clock,
44          * divided by the value specified in the divide configuration register.
45          * Note we mult and div by the divisor, saving the actual freq (even though
46          * we don't use it yet). */
47         cprintf("Bus Frequency: %llu\n", __proc_global_info.bus_freq);
48 }
49
50 void pit_set_timer(uint32_t divisor, uint32_t mode)
51 {
52         if (divisor & 0xffff0000)
53                 warn("Divisor too large!");
54         mode = TIMER_SEL0|TIMER_16BIT|mode;
55         outb(TIMER_MODE, mode);
56         outb(TIMER_CNTR0, divisor & 0xff);
57         outb(TIMER_CNTR0, (divisor >> 8) );
58         pit_mode = mode;
59         pit_divisor = divisor;
60         // cprintf("timer mode set to %d, divisor %d\n",mode, divisor);
61 }
62
63 static int getpit()
64 {
65     int high, low;
66         // TODO: need a lock to protect access to PIT
67
68     /* Select counter 0 and latch counter value. */
69     outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
70
71     low = inb(TIMER_CNTR0);
72     high = inb(TIMER_CNTR0);
73
74     return ((high << 8) | low);
75 }
76
77 // forces cpu to relax for usec miliseconds.  declared in kern/include/time.h
78 void udelay(uint64_t usec)
79 {
80         #if !defined(__BOCHS__)
81         if (__proc_global_info.tsc_freq != 0)
82         {
83                 uint64_t start, end, now;
84
85                 start = read_tsc();
86         end = start + usec2tsc(usec);
87         //cprintf("start %llu, end %llu\n", start, end);
88                 if (end == 0) cprintf("This is terribly wrong \n");
89                 do {
90             cpu_relax();
91             now = read_tsc();
92                         //cprintf("now %llu\n", now);
93                 } while (now < end || (now > start && end < start));
94         return;
95
96         } else
97         #endif
98         {
99                 udelay_pit(usec);
100         }
101 }
102
103 void udelay_pit(uint64_t usec)
104 {
105         int64_t delta, prev_tick, tick, ticks_left;
106
107         if (usec <= 0)
108                 return;
109
110         prev_tick = getpit();
111         /*
112          * Calculate ticks as (usec * (i8254_freq / 1e6)) rounded up
113          * without using floating point and without any avoidable overflows.
114          */
115         ticks_left = ((usec * PIT_FREQ) + 999999) / 1000000;
116         while (ticks_left > 0) {
117                 tick = getpit();
118                 delta = prev_tick - tick;
119                 prev_tick = tick;
120                 if (delta < 0) {
121                         // counter looped around during the delta time period
122                         delta += pit_divisor; // maximum count
123                         if (delta < 0)
124                                 delta = 0;
125                 }
126                 ticks_left -= delta;
127         }
128 }
129
130 uint64_t gettimer(void)
131 {
132         return read_tsc();
133 }
134
135 uint64_t getfreq(void)
136 {
137         return __proc_global_info.tsc_freq;
138 }
139
140 void set_core_timer(uint32_t usec, bool periodic)
141 {
142         if (usec)
143                 lapic_set_timer(usec, periodic);
144         else
145                 lapic_disable_timer();
146 }