blob: 5ac3c5e2e8a14b581c76a74e32fbad65e9235094 [file] [log] [blame]
Eric Biedermanc84c1902004-10-14 20:13:01 +00001#ifndef CPU_X86_LAPIC_H
2#define CPU_X86_LAPIC_H
3
Aaron Durbin75a62e72018-09-13 02:10:45 -06004#include <compiler.h>
Eric Biedermanc84c1902004-10-14 20:13:01 +00005#include <cpu/x86/lapic_def.h>
6#include <cpu/x86/msr.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +01007#include <halt.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +02008#include <smp/node.h>
Eric Biedermanc84c1902004-10-14 20:13:01 +00009
Aaron Durbin75a62e72018-09-13 02:10:45 -060010static __always_inline unsigned long lapic_read(unsigned long reg)
Eric Biedermanc84c1902004-10-14 20:13:01 +000011{
12 return *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg));
13}
14
Aaron Durbin75a62e72018-09-13 02:10:45 -060015static __always_inline void lapic_write(unsigned long reg, unsigned long v)
Eric Biedermanc84c1902004-10-14 20:13:01 +000016{
17 *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg)) = v;
18}
19
Aaron Durbin75a62e72018-09-13 02:10:45 -060020static __always_inline void lapic_wait_icr_idle(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000021{
Lee Leahy91d1e762017-03-07 14:31:19 -080022 do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY);
Eric Biedermanc84c1902004-10-14 20:13:01 +000023}
24
Eric Biedermanc84c1902004-10-14 20:13:01 +000025static inline void enable_lapic(void)
26{
Eric Biedermanc84c1902004-10-14 20:13:01 +000027 msr_t msr;
28 msr = rdmsr(LAPIC_BASE_MSR);
29 msr.hi &= 0xffffff00;
Kyösti Mälkkiff284f62017-08-18 12:11:16 +030030 msr.lo &= ~LAPIC_BASE_MSR_ADDR_MASK;
31 msr.lo |= LAPIC_DEFAULT_BASE;
32 msr.lo |= LAPIC_BASE_MSR_ENABLE;
Eric Biedermanc84c1902004-10-14 20:13:01 +000033 wrmsr(LAPIC_BASE_MSR, msr);
34}
35
36static inline void disable_lapic(void)
37{
38 msr_t msr;
39 msr = rdmsr(LAPIC_BASE_MSR);
Kyösti Mälkkiff284f62017-08-18 12:11:16 +030040 msr.lo &= ~LAPIC_BASE_MSR_ENABLE;
Eric Biedermanc84c1902004-10-14 20:13:01 +000041 wrmsr(LAPIC_BASE_MSR, msr);
42}
43
Aaron Durbin75a62e72018-09-13 02:10:45 -060044static __always_inline unsigned long lapicid(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000045{
46 return lapic_read(LAPIC_ID) >> 24;
47}
48
Martin Roth96345472017-06-24 14:13:53 -060049#if !IS_ENABLED(CONFIG_AP_IN_SIPI_WAIT)
Sven Schnelle51676b12012-07-29 19:18:03 +020050/* 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 Durbin75a62e72018-09-13 02:10:45 -060053static __always_inline void stop_this_cpu(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000054{
Sven Schnelle51676b12012-07-29 19:18:03 +020055 /* Called by an AP when it is ready to halt and wait for a new task */
Patrick Georgibd79c5e2014-11-28 22:35:36 +010056 halt();
Eric Biedermanc84c1902004-10-14 20:13:01 +000057}
Sven Schnelle51676b12012-07-29 19:18:03 +020058#else
59void stop_this_cpu(void);
60#endif
Eric Biedermanc84c1902004-10-14 20:13:01 +000061
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000062#if !defined(__PRE_RAM__)
Eric Biedermanc84c1902004-10-14 20:13:01 +000063
Lee Leahy6a566d72017-03-07 17:45:12 -080064#define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
65 sizeof(*(ptr))))
Eric Biedermanc84c1902004-10-14 20:13:01 +000066
67struct __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 Leahy6a566d72017-03-07 17:45:12 -080075static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
76 int size)
Eric Biedermanc84c1902004-10-14 20:13:01 +000077{
78 switch (size) {
Lee Leahydb469a62017-03-08 10:05:16 -080079 case 1:
80 __asm__ __volatile__("xchgb %b0,%1"
Lee Leahy74f1dc02017-03-08 10:08:08 -080081 : "=q" (x)
82 : "m" (*__xg(ptr)), "0" (x)
83 : "memory");
Lee Leahydb469a62017-03-08 10:05:16 -080084 break;
85 case 2:
86 __asm__ __volatile__("xchgw %w0,%1"
Lee Leahy74f1dc02017-03-08 10:08:08 -080087 : "=r" (x)
88 : "m" (*__xg(ptr)), "0" (x)
89 : "memory");
Lee Leahydb469a62017-03-08 10:05:16 -080090 break;
91 case 4:
92 __asm__ __volatile__("xchgl %0,%1"
Lee Leahy74f1dc02017-03-08 10:08:08 -080093 : "=r" (x)
94 : "m" (*__xg(ptr)), "0" (x)
95 : "memory");
Lee Leahydb469a62017-03-08 10:05:16 -080096 break;
Eric Biedermanc84c1902004-10-14 20:13:01 +000097 }
98 return x;
99}
100
Stefan Reinauer68524062008-08-02 15:15:23 +0000101static inline void lapic_write_atomic(unsigned long reg, unsigned long v)
Eric Biedermanc84c1902004-10-14 20:13:01 +0000102{
Patrick Georgi1a341652012-03-11 19:42:33 +0100103 (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg), v);
Eric Biedermanc84c1902004-10-14 20:13:01 +0000104}
105
106
Myles Watson0bc61542009-10-17 13:25:07 +0000107#ifdef X86_GOOD_APIC
Eric Biedermanc84c1902004-10-14 20:13:01 +0000108# define FORCE_READ_AROUND_WRITE 0
109# define lapic_read_around(x) lapic_read(x)
Lee Leahyae3fd342017-03-07 12:55:23 -0800110# define lapic_write_around(x, y) lapic_write((x), (y))
Eric Biedermanc84c1902004-10-14 20:13:01 +0000111#else
112# define FORCE_READ_AROUND_WRITE 1
113# define lapic_read_around(x) lapic_read(x)
Lee Leahyae3fd342017-03-07 12:55:23 -0800114# define lapic_write_around(x, y) lapic_write_atomic((x), (y))
Eric Biedermanc84c1902004-10-14 20:13:01 +0000115#endif
116
117static 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älkkiff284f62017-08-18 12:11:16 +0300141void do_lapic_init(void);
Eric Biedermanc84c1902004-10-14 20:13:01 +0000142
Kyösti Mälkkiff284f62017-08-18 12:11:16 +0300143/* See if I need to initialize the local APIC */
144static inline int need_lapic_init(void)
145{
146 return IS_ENABLED(CONFIG_SMP) || IS_ENABLED(CONFIG_IOAPIC);
147}
Eric Biedermanc84c1902004-10-14 20:13:01 +0000148
Kyösti Mälkkiff284f62017-08-18 12:11:16 +0300149static inline void setup_lapic(void)
150{
151 if (need_lapic_init())
152 do_lapic_init();
153 else
154 disable_lapic();
155}
156
Eric Biedermanc84c1902004-10-14 20:13:01 +0000157struct device;
158int start_cpu(struct device *cpu);
Eric Biedermanc84c1902004-10-14 20:13:01 +0000159
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +0000160#endif /* !__PRE_RAM__ */
Eric Biedermanc84c1902004-10-14 20:13:01 +0000161
Eric Biedermanc84c1902004-10-14 20:13:01 +0000162#endif /* CPU_X86_LAPIC_H */