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