blob: 6121230a228428e58d3f874e192f8b184389a30c [file] [log] [blame]
Eric Biedermanc84c1902004-10-14 20:13:01 +00001#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 Georgibd79c5e2014-11-28 22:35:36 +01006#include <halt.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +02007#include <smp/node.h>
Eric Biedermanc84c1902004-10-14 20:13:01 +00008
Lee Leahy6a566d72017-03-07 17:45:12 -08009static inline __attribute__((always_inline)) unsigned long lapic_read(
10 unsigned long reg)
Eric Biedermanc84c1902004-10-14 20:13:01 +000011{
12 return *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg));
13}
14
Lee Leahy6a566d72017-03-07 17:45:12 -080015static inline __attribute__((always_inline)) void lapic_write(unsigned long reg,
16 unsigned long v)
Eric Biedermanc84c1902004-10-14 20:13:01 +000017{
18 *((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg)) = v;
19}
20
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000021static inline __attribute__((always_inline)) void lapic_wait_icr_idle(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000022{
Lee Leahy91d1e762017-03-07 14:31:19 -080023 do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY);
Eric Biedermanc84c1902004-10-14 20:13:01 +000024}
25
Eric Biedermanc84c1902004-10-14 20:13:01 +000026static inline void enable_lapic(void)
27{
Eric Biedermanc84c1902004-10-14 20:13:01 +000028 msr_t msr;
29 msr = rdmsr(LAPIC_BASE_MSR);
30 msr.hi &= 0xffffff00;
Kyösti Mälkkiff284f62017-08-18 12:11:16 +030031 msr.lo &= ~LAPIC_BASE_MSR_ADDR_MASK;
32 msr.lo |= LAPIC_DEFAULT_BASE;
33 msr.lo |= LAPIC_BASE_MSR_ENABLE;
Eric Biedermanc84c1902004-10-14 20:13:01 +000034 wrmsr(LAPIC_BASE_MSR, msr);
35}
36
37static inline void disable_lapic(void)
38{
39 msr_t msr;
40 msr = rdmsr(LAPIC_BASE_MSR);
Kyösti Mälkkiff284f62017-08-18 12:11:16 +030041 msr.lo &= ~LAPIC_BASE_MSR_ENABLE;
Eric Biedermanc84c1902004-10-14 20:13:01 +000042 wrmsr(LAPIC_BASE_MSR, msr);
43}
44
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000045static inline __attribute__((always_inline)) unsigned long lapicid(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000046{
47 return lapic_read(LAPIC_ID) >> 24;
48}
49
Martin Roth96345472017-06-24 14:13:53 -060050#if !IS_ENABLED(CONFIG_AP_IN_SIPI_WAIT)
Sven Schnelle51676b12012-07-29 19:18:03 +020051/* 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 Reinauer7ce8c542005-12-02 21:52:30 +000054static inline __attribute__((always_inline)) void stop_this_cpu(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000055{
Sven Schnelle51676b12012-07-29 19:18:03 +020056 /* Called by an AP when it is ready to halt and wait for a new task */
Patrick Georgibd79c5e2014-11-28 22:35:36 +010057 halt();
Eric Biedermanc84c1902004-10-14 20:13:01 +000058}
Sven Schnelle51676b12012-07-29 19:18:03 +020059#else
60void stop_this_cpu(void);
61#endif
Eric Biedermanc84c1902004-10-14 20:13:01 +000062
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000063#if !defined(__PRE_RAM__)
Eric Biedermanc84c1902004-10-14 20:13:01 +000064
Lee Leahy6a566d72017-03-07 17:45:12 -080065#define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
66 sizeof(*(ptr))))
Eric Biedermanc84c1902004-10-14 20:13:01 +000067
68struct __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 Leahy6a566d72017-03-07 17:45:12 -080076static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
77 int size)
Eric Biedermanc84c1902004-10-14 20:13:01 +000078{
79 switch (size) {
Lee Leahydb469a62017-03-08 10:05:16 -080080 case 1:
81 __asm__ __volatile__("xchgb %b0,%1"
Lee Leahy74f1dc02017-03-08 10:08:08 -080082 : "=q" (x)
83 : "m" (*__xg(ptr)), "0" (x)
84 : "memory");
Lee Leahydb469a62017-03-08 10:05:16 -080085 break;
86 case 2:
87 __asm__ __volatile__("xchgw %w0,%1"
Lee Leahy74f1dc02017-03-08 10:08:08 -080088 : "=r" (x)
89 : "m" (*__xg(ptr)), "0" (x)
90 : "memory");
Lee Leahydb469a62017-03-08 10:05:16 -080091 break;
92 case 4:
93 __asm__ __volatile__("xchgl %0,%1"
Lee Leahy74f1dc02017-03-08 10:08:08 -080094 : "=r" (x)
95 : "m" (*__xg(ptr)), "0" (x)
96 : "memory");
Lee Leahydb469a62017-03-08 10:05:16 -080097 break;
Eric Biedermanc84c1902004-10-14 20:13:01 +000098 }
99 return x;
100}
101
Stefan Reinauer68524062008-08-02 15:15:23 +0000102static inline void lapic_write_atomic(unsigned long reg, unsigned long v)
Eric Biedermanc84c1902004-10-14 20:13:01 +0000103{
Patrick Georgi1a341652012-03-11 19:42:33 +0100104 (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE+reg), v);
Eric Biedermanc84c1902004-10-14 20:13:01 +0000105}
106
107
Myles Watson0bc61542009-10-17 13:25:07 +0000108#ifdef X86_GOOD_APIC
Eric Biedermanc84c1902004-10-14 20:13:01 +0000109# define FORCE_READ_AROUND_WRITE 0
110# define lapic_read_around(x) lapic_read(x)
Lee Leahyae3fd342017-03-07 12:55:23 -0800111# define lapic_write_around(x, y) lapic_write((x), (y))
Eric Biedermanc84c1902004-10-14 20:13:01 +0000112#else
113# define FORCE_READ_AROUND_WRITE 1
114# define lapic_read_around(x) lapic_read(x)
Lee Leahyae3fd342017-03-07 12:55:23 -0800115# define lapic_write_around(x, y) lapic_write_atomic((x), (y))
Eric Biedermanc84c1902004-10-14 20:13:01 +0000116#endif
117
118static 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älkkiff284f62017-08-18 12:11:16 +0300142void do_lapic_init(void);
Eric Biedermanc84c1902004-10-14 20:13:01 +0000143
Kyösti Mälkkiff284f62017-08-18 12:11:16 +0300144/* See if I need to initialize the local APIC */
145static inline int need_lapic_init(void)
146{
147 return IS_ENABLED(CONFIG_SMP) || IS_ENABLED(CONFIG_IOAPIC);
148}
Eric Biedermanc84c1902004-10-14 20:13:01 +0000149
Kyösti Mälkkiff284f62017-08-18 12:11:16 +0300150static inline void setup_lapic(void)
151{
152 if (need_lapic_init())
153 do_lapic_init();
154 else
155 disable_lapic();
156}
157
Eric Biedermanc84c1902004-10-14 20:13:01 +0000158struct device;
159int start_cpu(struct device *cpu);
Eric Biedermanc84c1902004-10-14 20:13:01 +0000160
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +0000161#endif /* !__PRE_RAM__ */
Eric Biedermanc84c1902004-10-14 20:13:01 +0000162
Eric Biedermanc84c1902004-10-14 20:13:01 +0000163#endif /* CPU_X86_LAPIC_H */