Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 1 | #ifndef CPU_X86_LAPIC_H |
| 2 | #define CPU_X86_LAPIC_H |
| 3 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame] | 4 | #include <arch/mmio.h> |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 5 | #include <arch/cpu.h> |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 6 | #include <cpu/x86/lapic_def.h> |
| 7 | #include <cpu/x86/msr.h> |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 8 | #include <halt.h> |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 10 | |
Kyösti Mälkki | 41e6216 | 2021-05-31 12:38:41 +0300 | [diff] [blame] | 11 | static __always_inline uint32_t xapic_read(unsigned int reg) |
| 12 | { |
| 13 | return read32((volatile void *)(uintptr_t)(LAPIC_DEFAULT_BASE + reg)); |
| 14 | } |
| 15 | |
| 16 | static __always_inline void xapic_write(unsigned int reg, uint32_t v) |
| 17 | { |
| 18 | write32((volatile void *)(uintptr_t)(LAPIC_DEFAULT_BASE + reg), v); |
| 19 | } |
| 20 | |
| 21 | static inline void xapic_write_atomic(unsigned long reg, uint32_t v) |
| 22 | { |
| 23 | volatile uint32_t *ptr; |
| 24 | |
| 25 | ptr = (volatile uint32_t *)(LAPIC_DEFAULT_BASE + reg); |
| 26 | |
| 27 | asm volatile ("xchgl %0, %1\n" |
| 28 | : "+r" (v), "+m" (*(ptr)) |
| 29 | : : "memory", "cc"); |
| 30 | } |
| 31 | |
| 32 | #define lapic_read_around(x) lapic_read(x) |
| 33 | #define lapic_write_around(x, y) xapic_write_atomic((x), (y)) |
| 34 | |
| 35 | |
| 36 | static __always_inline uint32_t x2apic_read(unsigned int reg) |
| 37 | { |
| 38 | uint32_t value, index; |
| 39 | msr_t msr; |
| 40 | |
| 41 | index = X2APIC_MSR_BASE_ADDRESS + (uint32_t)(reg >> 4); |
| 42 | msr = rdmsr(index); |
| 43 | value = msr.lo; |
| 44 | return value; |
| 45 | } |
| 46 | |
| 47 | static __always_inline void x2apic_write(unsigned int reg, uint32_t v) |
| 48 | { |
| 49 | uint32_t index; |
| 50 | msr_t msr; |
| 51 | |
| 52 | index = X2APIC_MSR_BASE_ADDRESS + (uint32_t)(reg >> 4); |
| 53 | msr.hi = 0x0; |
| 54 | msr.lo = v; |
| 55 | wrmsr(index, msr); |
| 56 | } |
| 57 | |
| 58 | static __always_inline void x2apic_send_ipi(uint32_t icrlow, uint32_t apicid) |
| 59 | { |
| 60 | msr_t icr; |
| 61 | icr.hi = apicid; |
| 62 | icr.lo = icrlow; |
| 63 | wrmsr(X2APIC_MSR_ICR_ADDRESS, icr); |
| 64 | } |
| 65 | |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 66 | static inline bool is_x2apic_mode(void) |
| 67 | { |
Kyösti Mälkki | 176c887 | 2021-05-29 20:33:22 +0300 | [diff] [blame] | 68 | if (CONFIG(XAPIC_ONLY)) |
| 69 | return false; |
| 70 | |
| 71 | if (CONFIG(X2APIC_ONLY)) |
| 72 | return true; |
| 73 | |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 74 | msr_t msr; |
| 75 | msr = rdmsr(LAPIC_BASE_MSR); |
Wonkyu Kim | a04256f | 2021-04-27 01:52:57 -0700 | [diff] [blame] | 76 | return ((msr.lo & LAPIC_BASE_X2APIC_ENABLED) == LAPIC_BASE_X2APIC_ENABLED); |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame] | 79 | static __always_inline uint32_t lapic_read(unsigned int reg) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 80 | { |
Kyösti Mälkki | 41e6216 | 2021-05-31 12:38:41 +0300 | [diff] [blame] | 81 | if (is_x2apic_mode()) |
| 82 | return x2apic_read(reg); |
| 83 | else |
| 84 | return xapic_read(reg); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame] | 87 | static __always_inline void lapic_write(unsigned int reg, uint32_t v) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 88 | { |
Kyösti Mälkki | 41e6216 | 2021-05-31 12:38:41 +0300 | [diff] [blame] | 89 | if (is_x2apic_mode()) |
| 90 | x2apic_write(reg, v); |
| 91 | else |
| 92 | xapic_write(reg, v); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kyösti Mälkki | 0cfa911 | 2021-05-31 10:38:45 +0300 | [diff] [blame] | 95 | static __always_inline void lapic_update32(unsigned int reg, uint32_t mask, uint32_t or) |
| 96 | { |
| 97 | if (is_x2apic_mode()) { |
| 98 | uint32_t index; |
| 99 | msr_t msr; |
| 100 | index = X2APIC_MSR_BASE_ADDRESS + (uint32_t)(reg >> 4); |
| 101 | msr = rdmsr(index); |
| 102 | msr.lo &= mask; |
| 103 | msr.lo |= or; |
| 104 | wrmsr(index, msr); |
| 105 | } else { |
| 106 | uint32_t value; |
| 107 | value = xapic_read(reg); |
| 108 | value &= mask; |
| 109 | value |= or; |
| 110 | xapic_write_atomic(reg, value); |
| 111 | } |
| 112 | } |
| 113 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 114 | static __always_inline void lapic_wait_icr_idle(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 115 | { |
Lee Leahy | 91d1e76 | 2017-03-07 14:31:19 -0800 | [diff] [blame] | 116 | do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 119 | static __always_inline unsigned int initial_lapicid(void) |
| 120 | { |
| 121 | uint32_t lapicid; |
Wonkyu Kim | a04256f | 2021-04-27 01:52:57 -0700 | [diff] [blame] | 122 | if (is_x2apic_mode() && cpuid_get_max_func() >= 0xb) |
| 123 | lapicid = cpuid_ext(0xb, 0).edx; |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 124 | else |
| 125 | lapicid = cpuid_ebx(1) >> 24; |
| 126 | return lapicid; |
| 127 | } |
| 128 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame] | 129 | static __always_inline unsigned int lapicid(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 130 | { |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 131 | uint32_t lapicid = lapic_read(LAPIC_ID); |
| 132 | |
| 133 | /* check x2apic mode and return accordingly */ |
| 134 | if (!is_x2apic_mode()) |
| 135 | lapicid >>= 24; |
| 136 | return lapicid; |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 139 | #if !CONFIG(AP_IN_SIPI_WAIT) |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 140 | /* If we need to go back to sipi wait, we use the long non-inlined version of |
| 141 | * this function in lapic_cpu_init.c |
| 142 | */ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 143 | static __always_inline void stop_this_cpu(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 144 | { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 145 | /* Called by an AP when it is ready to halt and wait for a new task */ |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 146 | halt(); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 147 | } |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 148 | #else |
| 149 | void stop_this_cpu(void); |
| 150 | #endif |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 151 | |
Kyösti Mälkki | 242f1d9 | 2021-06-06 16:58:19 +0300 | [diff] [blame^] | 152 | void enable_lapic(void); |
| 153 | void disable_lapic(void); |
| 154 | void setup_lapic(void); |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 155 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 156 | #endif /* CPU_X86_LAPIC_H */ |