rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2009 Corey Tabaka |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <sys/types.h> |
| 24 | #include <err.h> |
| 25 | #include <reg.h> |
| 26 | #include <debug.h> |
| 27 | #include <kernel/thread.h> |
| 28 | #include <kernel/spinlock.h> |
| 29 | #include <platform.h> |
| 30 | #include <platform/interrupts.h> |
| 31 | #include <platform/console.h> |
| 32 | #include <platform/timer.h> |
| 33 | #include <platform/pc.h> |
| 34 | #include "platform_p.h" |
| 35 | #include <arch/x86.h> |
| 36 | |
| 37 | static platform_timer_callback t_callback; |
| 38 | static void *callback_arg; |
| 39 | static spin_lock_t lock; |
| 40 | |
| 41 | static uint64_t next_trigger_time; |
| 42 | static uint64_t next_trigger_delta; |
| 43 | static uint64_t ticks_per_ms; |
| 44 | |
| 45 | static uint64_t timer_delta_time; |
| 46 | static volatile uint64_t timer_current_time; |
| 47 | |
| 48 | static uint16_t divisor; |
| 49 | |
| 50 | #define INTERNAL_FREQ 1193182ULL |
| 51 | #define INTERNAL_FREQ_3X 3579546ULL |
| 52 | |
| 53 | /* Maximum amount of time that can be program on the timer to schedule the next |
| 54 | * interrupt, in miliseconds */ |
| 55 | #define MAX_TIMER_INTERVAL 55 |
| 56 | |
| 57 | |
| 58 | |
| 59 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval) |
| 60 | { |
| 61 | t_callback = callback; |
| 62 | callback_arg = arg; |
| 63 | |
| 64 | next_trigger_delta = (uint64_t) interval << 32; |
| 65 | next_trigger_time = timer_current_time + next_trigger_delta; |
| 66 | |
| 67 | return NO_ERROR; |
| 68 | } |
| 69 | |
| 70 | lk_time_t current_time(void) |
| 71 | { |
| 72 | lk_time_t time; |
| 73 | |
| 74 | // XXX slight race |
| 75 | time = (lk_time_t) (timer_current_time >> 32); |
| 76 | |
| 77 | return time; |
| 78 | } |
| 79 | |
| 80 | lk_bigtime_t current_time_hires(void) |
| 81 | { |
| 82 | lk_bigtime_t time; |
| 83 | |
| 84 | // XXX slight race |
| 85 | time = (lk_bigtime_t) ((timer_current_time >> 22) * 1000) >> 10; |
| 86 | |
| 87 | return time; |
| 88 | } |
| 89 | static enum handler_return os_timer_tick(void *arg) |
| 90 | { |
| 91 | uint64_t delta; |
| 92 | |
| 93 | timer_current_time += timer_delta_time; |
| 94 | |
| 95 | lk_time_t time = current_time(); |
| 96 | //lk_bigtime_t btime = current_time_hires(); |
| 97 | //printf_xy(71, 0, WHITE, "%08u", (uint32_t) time); |
| 98 | //printf_xy(63, 1, WHITE, "%016llu", (uint64_t) btime); |
| 99 | |
| 100 | if (t_callback && timer_current_time >= next_trigger_time) { |
| 101 | delta = timer_current_time - next_trigger_time; |
| 102 | next_trigger_time = timer_current_time + next_trigger_delta - delta; |
| 103 | |
| 104 | return t_callback(callback_arg, time); |
| 105 | } else { |
| 106 | return INT_NO_RESCHEDULE; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void set_pit_frequency(uint32_t frequency) |
| 111 | { |
| 112 | uint32_t count, remainder; |
| 113 | |
| 114 | /* figure out the correct divisor for the desired frequency */ |
| 115 | if (frequency <= 18) { |
| 116 | count = 0xffff; |
| 117 | } else if (frequency >= INTERNAL_FREQ) { |
| 118 | count = 1; |
| 119 | } else { |
| 120 | count = INTERNAL_FREQ_3X / frequency; |
| 121 | remainder = INTERNAL_FREQ_3X % frequency; |
| 122 | |
| 123 | if (remainder >= INTERNAL_FREQ_3X / 2) { |
| 124 | count += 1; |
| 125 | } |
| 126 | |
| 127 | count /= 3; |
| 128 | remainder = count % 3; |
| 129 | |
| 130 | if (remainder >= 1) { |
| 131 | count += 1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | divisor = count & 0xffff; |
| 136 | |
| 137 | /* |
| 138 | * funky math that i don't feel like explaining. essentially 32.32 fixed |
| 139 | * point representation of the configured timer delta. |
| 140 | */ |
| 141 | timer_delta_time = (3685982306ULL * count) >> 10; |
| 142 | |
| 143 | //dprintf(DEBUG, "set_pit_frequency: dt=%016llx\n", timer_delta_time); |
| 144 | //dprintf(DEBUG, "set_pit_frequency: divisor=%04x\n", divisor); |
| 145 | |
| 146 | /* |
| 147 | * setup the Programmable Interval Timer |
| 148 | * timer 0, mode 2, binary counter, LSB followed by MSB |
| 149 | */ |
| 150 | outp(I8253_CONTROL_REG, 0x34); |
| 151 | outp(I8253_DATA_REG, divisor & 0xff); // LSB |
| 152 | outp(I8253_DATA_REG, divisor >> 8); // MSB |
| 153 | } |
| 154 | |
| 155 | void platform_init_timer(void) |
| 156 | { |
| 157 | |
| 158 | timer_current_time = 0; |
| 159 | ticks_per_ms = INTERNAL_FREQ/1000; |
| 160 | set_pit_frequency(1000); // ~1ms granularity |
| 161 | register_int_handler(INT_PIT, &os_timer_tick, NULL); |
| 162 | unmask_interrupt(INT_PIT); |
| 163 | } |
| 164 | |
| 165 | void platform_halt_timers(void) |
| 166 | { |
| 167 | mask_interrupt(INT_PIT); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | status_t platform_set_oneshot_timer(platform_timer_callback callback, |
| 173 | void *arg, lk_time_t interval) |
| 174 | { |
| 175 | |
| 176 | uint32_t count; |
| 177 | |
| 178 | spin_lock_saved_state_t state; |
| 179 | spin_lock_irqsave(&lock, state); |
| 180 | |
| 181 | t_callback = callback; |
| 182 | callback_arg = arg; |
| 183 | |
| 184 | |
| 185 | if (interval > MAX_TIMER_INTERVAL) |
| 186 | interval = MAX_TIMER_INTERVAL; |
| 187 | if (interval < 1) interval = 1; |
| 188 | |
| 189 | count = ticks_per_ms * interval; |
| 190 | |
| 191 | divisor = count & 0xffff; |
| 192 | timer_delta_time = (3685982306ULL * count) >> 10; |
| 193 | /* Program PIT in teh software strobe configuration, to send one pulse |
| 194 | * after the count reach 0 */ |
| 195 | outp(I8253_CONTROL_REG, 0x38); |
| 196 | outp(I8253_DATA_REG, divisor & 0xff); // LSB |
| 197 | outp(I8253_DATA_REG, divisor >> 8); // MSB |
| 198 | |
| 199 | |
| 200 | unmask_interrupt(INT_PIT); |
| 201 | spin_unlock_irqrestore(&lock, state); |
| 202 | |
| 203 | return NO_ERROR; |
| 204 | } |
| 205 | |
| 206 | void platform_stop_timer(void) |
| 207 | { |
| 208 | /* Enable interrupt mode that will stop the decreasing counter of the PIT */ |
| 209 | outp(I8253_CONTROL_REG, 0x30); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | /* vim: set noexpandtab */ |