Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2007 Advanced Micro Devices, Inc. |
| 5 | * Copyright (C) 2009 coresystems GmbH |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stdint.h> |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 18 | #include <console/console.h> |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 19 | #include <delay.h> |
Aaron Durbin | 38c326d | 2013-05-06 12:22:23 -0500 | [diff] [blame] | 20 | #include <thread.h> |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 21 | #include <arch/cpu.h> |
Stefan Reinauer | fd4f413 | 2013-06-19 12:25:44 -0700 | [diff] [blame] | 22 | #include <arch/early_variables.h> |
Elyes HAOUAS | d731a24 | 2018-12-07 14:49:20 +0100 | [diff] [blame] | 23 | #include <cpu/intel/fsb.h> |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 24 | #include <cpu/x86/msr.h> |
| 25 | #include <cpu/x86/lapic.h> |
Patrick Georgi | 8cc8468 | 2013-02-09 15:56:04 +0100 | [diff] [blame] | 26 | #include <cpu/intel/speedstep.h> |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 27 | |
| 28 | /* NOTE: This code uses global variables, so it can not be used during |
| 29 | * memory init. |
| 30 | */ |
| 31 | |
Martin Roth | 0fa92b3 | 2017-06-24 13:53:20 -0600 | [diff] [blame] | 32 | #if CONFIG_UDELAY_LAPIC_FIXED_FSB != 0 |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 33 | static inline u32 get_timer_fsb(void) |
| 34 | { |
| 35 | return CONFIG_UDELAY_LAPIC_FIXED_FSB; |
| 36 | } |
Patrick Georgi | e135ac5 | 2012-11-20 11:53:47 +0100 | [diff] [blame] | 37 | |
| 38 | static int set_timer_fsb(void) |
| 39 | { |
| 40 | return 0; |
| 41 | } |
| 42 | #else |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 43 | static u32 g_timer_fsb CAR_GLOBAL; |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 44 | |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 45 | static int set_timer_fsb(void) |
| 46 | { |
Elyes HAOUAS | d731a24 | 2018-12-07 14:49:20 +0100 | [diff] [blame] | 47 | int ia32_fsb = get_ia32_fsb(); |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 48 | |
Elyes HAOUAS | d731a24 | 2018-12-07 14:49:20 +0100 | [diff] [blame] | 49 | if (ia32_fsb > 0) { |
| 50 | car_set_var(g_timer_fsb, ia32_fsb); |
| 51 | return 0; |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 52 | } |
Elyes HAOUAS | d731a24 | 2018-12-07 14:49:20 +0100 | [diff] [blame] | 53 | return -1; |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 54 | } |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 55 | |
| 56 | static inline u32 get_timer_fsb(void) |
| 57 | { |
| 58 | return car_get_var(g_timer_fsb); |
| 59 | } |
Patrick Georgi | e135ac5 | 2012-11-20 11:53:47 +0100 | [diff] [blame] | 60 | #endif |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 61 | |
| 62 | void init_timer(void) |
| 63 | { |
Elyes HAOUAS | d6e9686 | 2016-08-21 10:12:15 +0200 | [diff] [blame] | 64 | /* Set the APIC timer to no interrupts and periodic mode */ |
Vikram Narayanan | 6649d97 | 2011-05-10 21:47:57 +0000 | [diff] [blame] | 65 | lapic_write(LAPIC_LVTT, (LAPIC_LVT_TIMER_PERIODIC | LAPIC_LVT_MASKED)); |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 66 | |
| 67 | /* Set the divider to 1, no divider */ |
| 68 | lapic_write(LAPIC_TDCR, LAPIC_TDR_DIV_1); |
| 69 | |
| 70 | /* Set the initial counter to 0xffffffff */ |
| 71 | lapic_write(LAPIC_TMICT, 0xffffffff); |
| 72 | |
| 73 | /* Set FSB frequency to a reasonable value */ |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 74 | set_timer_fsb(); |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void udelay(u32 usecs) |
| 78 | { |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 79 | u32 start, value, ticks, timer_fsb; |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 80 | |
Aaron Durbin | 38c326d | 2013-05-06 12:22:23 -0500 | [diff] [blame] | 81 | if (!thread_yield_microseconds(usecs)) |
| 82 | return; |
| 83 | |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 84 | timer_fsb = get_timer_fsb(); |
Stefan Reinauer | ac2ec34 | 2012-07-10 15:19:23 -0700 | [diff] [blame] | 85 | if (!timer_fsb || (lapic_read(LAPIC_LVTT) & |
| 86 | (LAPIC_LVT_TIMER_PERIODIC | LAPIC_LVT_MASKED)) != |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 87 | (LAPIC_LVT_TIMER_PERIODIC | LAPIC_LVT_MASKED)) { |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 88 | init_timer(); |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 89 | timer_fsb = get_timer_fsb(); |
| 90 | } |
Stefan Reinauer | 5b6404e | 2012-04-03 16:11:02 -0700 | [diff] [blame] | 91 | |
Lee Leahy | c591707 | 2017-03-15 16:38:51 -0700 | [diff] [blame] | 92 | /* Calculate the number of ticks to run, our FSB runs at timer_fsb Mhz |
| 93 | */ |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 94 | ticks = usecs * timer_fsb; |
| 95 | start = lapic_read(LAPIC_TMCCT); |
| 96 | do { |
| 97 | value = lapic_read(LAPIC_TMCCT); |
Elyes HAOUAS | cbe7464c | 2016-08-23 21:07:28 +0200 | [diff] [blame] | 98 | } while ((start - value) < ticks); |
Stefan Reinauer | 4fbefdd | 2009-07-21 21:19:06 +0000 | [diff] [blame] | 99 | } |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 100 | |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 101 | #if IS_ENABLED(CONFIG_LAPIC_MONOTONIC_TIMER) |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 102 | #include <timer.h> |
| 103 | |
| 104 | static struct monotonic_counter { |
| 105 | int initialized; |
| 106 | struct mono_time time; |
| 107 | uint32_t last_value; |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 108 | } mono_counter_g CAR_GLOBAL; |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 109 | |
| 110 | void timer_monotonic_get(struct mono_time *mt) |
| 111 | { |
| 112 | uint32_t current_tick; |
| 113 | uint32_t usecs_elapsed; |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 114 | uint32_t timer_fsb; |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 115 | struct monotonic_counter *mono_counter; |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 116 | |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 117 | mono_counter = car_get_var_ptr(&mono_counter_g); |
| 118 | |
| 119 | if (!mono_counter->initialized) { |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 120 | init_timer(); |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 121 | timer_fsb = get_timer_fsb(); |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 122 | /* An FSB frequency of 200Mhz provides a 20 second polling |
| 123 | * interval between timer_monotonic_get() calls before wrap |
| 124 | * around occurs. */ |
| 125 | if (timer_fsb > 200) |
| 126 | printk(BIOS_WARNING, |
| 127 | "apic timer freq (%d) may be too fast.\n", |
| 128 | timer_fsb); |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 129 | mono_counter->last_value = lapic_read(LAPIC_TMCCT); |
| 130 | mono_counter->initialized = 1; |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Aaron Durbin | 905bfb0 | 2014-03-28 08:40:59 -0500 | [diff] [blame] | 133 | timer_fsb = get_timer_fsb(); |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 134 | current_tick = lapic_read(LAPIC_TMCCT); |
| 135 | /* Note that the APIC timer counts down. */ |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 136 | usecs_elapsed = (mono_counter->last_value - current_tick) / timer_fsb; |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 137 | |
| 138 | /* Update current time and tick values only if a full tick occurred. */ |
| 139 | if (usecs_elapsed) { |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 140 | mono_time_add_usecs(&mono_counter->time, usecs_elapsed); |
| 141 | mono_counter->last_value = current_tick; |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Save result. */ |
Arthur Heymans | b3587a6 | 2018-08-02 13:21:57 +0200 | [diff] [blame] | 145 | *mt = mono_counter->time; |
Aaron Durbin | fd8291c | 2013-04-29 17:18:49 -0500 | [diff] [blame] | 146 | } |
| 147 | #endif |