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> |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 5 | #include <cpu/x86/lapic_def.h> |
| 6 | #include <cpu/x86/msr.h> |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 7 | #include <halt.h> |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 8 | #include <stdint.h> |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 9 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 10 | static __always_inline uint32_t lapic_read(unsigned int reg) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 11 | { |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 12 | return read32((volatile void *)(LAPIC_DEFAULT_BASE + reg)); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 13 | } |
| 14 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 15 | static __always_inline void lapic_write(unsigned int reg, uint32_t v) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 16 | { |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 17 | write32((volatile void *)(LAPIC_DEFAULT_BASE + reg), v); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 20 | static __always_inline void lapic_wait_icr_idle(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 21 | { |
Lee Leahy | 91d1e76 | 2017-03-07 14:31:19 -0800 | [diff] [blame] | 22 | do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 25 | static inline void enable_lapic(void) |
| 26 | { |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 27 | msr_t msr; |
| 28 | msr = rdmsr(LAPIC_BASE_MSR); |
| 29 | msr.hi &= 0xffffff00; |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 30 | msr.lo &= ~LAPIC_BASE_MSR_ADDR_MASK; |
| 31 | msr.lo |= LAPIC_DEFAULT_BASE; |
| 32 | msr.lo |= LAPIC_BASE_MSR_ENABLE; |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 33 | wrmsr(LAPIC_BASE_MSR, msr); |
| 34 | } |
| 35 | |
| 36 | static inline void disable_lapic(void) |
| 37 | { |
| 38 | msr_t msr; |
| 39 | msr = rdmsr(LAPIC_BASE_MSR); |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 40 | msr.lo &= ~LAPIC_BASE_MSR_ENABLE; |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 41 | wrmsr(LAPIC_BASE_MSR, msr); |
| 42 | } |
| 43 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 44 | static __always_inline unsigned int lapicid(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 45 | { |
| 46 | return lapic_read(LAPIC_ID) >> 24; |
| 47 | } |
| 48 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 49 | #if !CONFIG(AP_IN_SIPI_WAIT) |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 50 | /* If we need to go back to sipi wait, we use the long non-inlined version of |
| 51 | * this function in lapic_cpu_init.c |
| 52 | */ |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 53 | static __always_inline void stop_this_cpu(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 54 | { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 55 | /* 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] | 56 | halt(); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 57 | } |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 58 | #else |
| 59 | void stop_this_cpu(void); |
| 60 | #endif |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 61 | |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 62 | static inline void lapic_write_atomic(unsigned long reg, uint32_t v) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 63 | { |
Patrick Rudolph | fc57d6c | 2019-11-12 16:30:14 +0100 | [diff] [blame^] | 64 | volatile uint32_t *ptr; |
| 65 | |
| 66 | ptr = (volatile uint32_t *)(LAPIC_DEFAULT_BASE + reg); |
| 67 | |
| 68 | asm volatile ("xchgl %0, %1\n" |
| 69 | : "+r" (v), "+m" (*(ptr)) |
| 70 | : : "memory", "cc"); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 73 | # define lapic_read_around(x) lapic_read(x) |
Lee Leahy | ae3fd34 | 2017-03-07 12:55:23 -0800 | [diff] [blame] | 74 | # define lapic_write_around(x, y) lapic_write_atomic((x), (y)) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 75 | |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 76 | void do_lapic_init(void); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 77 | |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 78 | /* See if I need to initialize the local APIC */ |
| 79 | static inline int need_lapic_init(void) |
| 80 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 81 | return CONFIG(SMP) || CONFIG(IOAPIC); |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 82 | } |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 83 | |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 84 | static inline void setup_lapic(void) |
| 85 | { |
| 86 | if (need_lapic_init()) |
| 87 | do_lapic_init(); |
| 88 | else |
| 89 | disable_lapic(); |
| 90 | } |
| 91 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 92 | struct device; |
| 93 | int start_cpu(struct device *cpu); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 94 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 95 | #endif /* CPU_X86_LAPIC_H */ |