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