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 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame^] | 4 | #include <compiler.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> |
Kyösti Mälkki | 5a5c886 | 2014-01-26 14:41:54 +0200 | [diff] [blame] | 8 | #include <smp/node.h> |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 9 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame^] | 10 | static __always_inline unsigned long lapic_read(unsigned long reg) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 11 | { |
| 12 | return *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg)); |
| 13 | } |
| 14 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame^] | 15 | static __always_inline void lapic_write(unsigned long reg, unsigned long v) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 16 | { |
| 17 | *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg)) = v; |
| 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 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame^] | 44 | static __always_inline unsigned long lapicid(void) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 45 | { |
| 46 | return lapic_read(LAPIC_ID) >> 24; |
| 47 | } |
| 48 | |
Martin Roth | 9634547 | 2017-06-24 14:13:53 -0600 | [diff] [blame] | 49 | #if !IS_ENABLED(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 | |
Stefan Reinauer | 35b6bbb | 2010-03-28 21:26:54 +0000 | [diff] [blame] | 62 | #if !defined(__PRE_RAM__) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 63 | |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 64 | #define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \ |
| 65 | sizeof(*(ptr)))) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 66 | |
| 67 | struct __xchg_dummy { unsigned long a[100]; }; |
| 68 | #define __xg(x) ((struct __xchg_dummy *)(x)) |
| 69 | |
| 70 | /* |
| 71 | * Note: no "lock" prefix even on SMP: xchg always implies lock anyway |
| 72 | * Note 2: xchg has side effect, so that attribute volatile is necessary, |
| 73 | * but generally the primitive is invalid, *ptr is output argument. --ANK |
| 74 | */ |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 75 | static inline unsigned long __xchg(unsigned long x, volatile void *ptr, |
| 76 | int size) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 77 | { |
| 78 | switch (size) { |
Lee Leahy | db469a6 | 2017-03-08 10:05:16 -0800 | [diff] [blame] | 79 | case 1: |
| 80 | __asm__ __volatile__("xchgb %b0,%1" |
Lee Leahy | 74f1dc0 | 2017-03-08 10:08:08 -0800 | [diff] [blame] | 81 | : "=q" (x) |
| 82 | : "m" (*__xg(ptr)), "0" (x) |
| 83 | : "memory"); |
Lee Leahy | db469a6 | 2017-03-08 10:05:16 -0800 | [diff] [blame] | 84 | break; |
| 85 | case 2: |
| 86 | __asm__ __volatile__("xchgw %w0,%1" |
Lee Leahy | 74f1dc0 | 2017-03-08 10:08:08 -0800 | [diff] [blame] | 87 | : "=r" (x) |
| 88 | : "m" (*__xg(ptr)), "0" (x) |
| 89 | : "memory"); |
Lee Leahy | db469a6 | 2017-03-08 10:05:16 -0800 | [diff] [blame] | 90 | break; |
| 91 | case 4: |
| 92 | __asm__ __volatile__("xchgl %0,%1" |
Lee Leahy | 74f1dc0 | 2017-03-08 10:08:08 -0800 | [diff] [blame] | 93 | : "=r" (x) |
| 94 | : "m" (*__xg(ptr)), "0" (x) |
| 95 | : "memory"); |
Lee Leahy | db469a6 | 2017-03-08 10:05:16 -0800 | [diff] [blame] | 96 | break; |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 97 | } |
| 98 | return x; |
| 99 | } |
| 100 | |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 101 | static inline void lapic_write_atomic(unsigned long reg, unsigned long v) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 102 | { |
Patrick Georgi | 1a34165 | 2012-03-11 19:42:33 +0100 | [diff] [blame] | 103 | (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg), v); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
Myles Watson | 0bc6154 | 2009-10-17 13:25:07 +0000 | [diff] [blame] | 107 | #ifdef X86_GOOD_APIC |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 108 | # define FORCE_READ_AROUND_WRITE 0 |
| 109 | # define lapic_read_around(x) lapic_read(x) |
Lee Leahy | ae3fd34 | 2017-03-07 12:55:23 -0800 | [diff] [blame] | 110 | # define lapic_write_around(x, y) lapic_write((x), (y)) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 111 | #else |
| 112 | # define FORCE_READ_AROUND_WRITE 1 |
| 113 | # define lapic_read_around(x) lapic_read(x) |
Lee Leahy | ae3fd34 | 2017-03-07 12:55:23 -0800 | [diff] [blame] | 114 | # define lapic_write_around(x, y) lapic_write_atomic((x), (y)) |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
| 117 | static inline int lapic_remote_read(int apicid, int reg, unsigned long *pvalue) |
| 118 | { |
| 119 | int timeout; |
| 120 | unsigned long status; |
| 121 | int result; |
| 122 | lapic_wait_icr_idle(); |
| 123 | lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid)); |
| 124 | lapic_write_around(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4)); |
| 125 | timeout = 0; |
| 126 | do { |
| 127 | #if 0 |
| 128 | udelay(100); |
| 129 | #endif |
| 130 | status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK; |
| 131 | } while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000); |
| 132 | |
| 133 | result = -1; |
| 134 | if (status == LAPIC_ICR_RR_VALID) { |
| 135 | *pvalue = lapic_read(LAPIC_RRR); |
| 136 | result = 0; |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 141 | void do_lapic_init(void); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 142 | |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 143 | /* See if I need to initialize the local APIC */ |
| 144 | static inline int need_lapic_init(void) |
| 145 | { |
| 146 | return IS_ENABLED(CONFIG_SMP) || IS_ENABLED(CONFIG_IOAPIC); |
| 147 | } |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 148 | |
Kyösti Mälkki | ff284f6 | 2017-08-18 12:11:16 +0300 | [diff] [blame] | 149 | static inline void setup_lapic(void) |
| 150 | { |
| 151 | if (need_lapic_init()) |
| 152 | do_lapic_init(); |
| 153 | else |
| 154 | disable_lapic(); |
| 155 | } |
| 156 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 157 | struct device; |
| 158 | int start_cpu(struct device *cpu); |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 159 | |
Stefan Reinauer | 35b6bbb | 2010-03-28 21:26:54 +0000 | [diff] [blame] | 160 | #endif /* !__PRE_RAM__ */ |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 161 | |
Eric Biederman | c84c190 | 2004-10-14 20:13:01 +0000 | [diff] [blame] | 162 | #endif /* CPU_X86_LAPIC_H */ |