1 #ifndef CPU_X86_LAPIC_H
2 #define CPU_X86_LAPIC_H
4 #include <cpu/x86/lapic_def.h>
5 #include <cpu/x86/msr.h>
8 /* See if I need to initialize the local apic */
9 #if CONFIG_SMP || CONFIG_IOAPIC
15 static inline __attribute__((always_inline)) unsigned long lapic_read(unsigned long reg)
17 return *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg));
20 static inline __attribute__((always_inline)) void lapic_write(unsigned long reg, unsigned long v)
22 *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg)) = v;
25 static inline __attribute__((always_inline)) void lapic_wait_icr_idle(void)
27 do { } while ( lapic_read( LAPIC_ICR ) & LAPIC_ICR_BUSY );
30 static inline void enable_lapic(void)
34 msr = rdmsr(LAPIC_BASE_MSR);
37 msr.lo |= LAPIC_DEFAULT_BASE | (1 << 11);
38 wrmsr(LAPIC_BASE_MSR, msr);
41 static inline void disable_lapic(void)
44 msr = rdmsr(LAPIC_BASE_MSR);
46 wrmsr(LAPIC_BASE_MSR, msr);
49 static inline __attribute__((always_inline)) unsigned long lapicid(void)
51 return lapic_read(LAPIC_ID) >> 24;
55 #if CONFIG_AP_IN_SIPI_WAIT != 1
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
59 static inline __attribute__((always_inline)) void stop_this_cpu(void)
61 /* Called by an AP when it is ready to halt and wait for a new task */
67 void stop_this_cpu(void);
70 #if !defined(__PRE_RAM__)
72 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
74 struct __xchg_dummy { unsigned long a[100]; };
75 #define __xg(x) ((struct __xchg_dummy *)(x))
78 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
79 * Note 2: xchg has side effect, so that attribute volatile is necessary,
80 * but generally the primitive is invalid, *ptr is output argument. --ANK
82 static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
86 __asm__ __volatile__("xchgb %b0,%1"
88 :"m" (*__xg(ptr)), "0" (x)
92 __asm__ __volatile__("xchgw %w0,%1"
94 :"m" (*__xg(ptr)), "0" (x)
98 __asm__ __volatile__("xchgl %0,%1"
100 :"m" (*__xg(ptr)), "0" (x)
107 static inline void lapic_write_atomic(unsigned long reg, unsigned long v)
109 (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg), v);
114 # define FORCE_READ_AROUND_WRITE 0
115 # define lapic_read_around(x) lapic_read(x)
116 # define lapic_write_around(x,y) lapic_write((x),(y))
118 # define FORCE_READ_AROUND_WRITE 1
119 # define lapic_read_around(x) lapic_read(x)
120 # define lapic_write_around(x,y) lapic_write_atomic((x),(y))
123 static inline int lapic_remote_read(int apicid, int reg, unsigned long *pvalue)
126 unsigned long status;
128 lapic_wait_icr_idle();
129 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
130 lapic_write_around(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4));
136 status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK;
137 } while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000);
140 if (status == LAPIC_ICR_RR_VALID) {
141 *pvalue = lapic_read(LAPIC_RRR);
148 void setup_lapic(void);
152 int start_cpu(struct device *cpu);
153 #endif /* CONFIG_SMP */
155 #endif /* !__PRE_RAM__ */
160 #endif /* CPU_X86_LAPIC_H */