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