11965aae3SH. Peter Anvin #ifndef _ASM_X86_APIC_H 21965aae3SH. Peter Anvin #define _ASM_X86_APIC_H 3bb898558SAl Viro 4e2780a68SIngo Molnar #include <linux/cpumask.h> 5e2780a68SIngo Molnar #include <linux/pm.h> 6bb898558SAl Viro 7bb898558SAl Viro #include <asm/alternative.h> 8bb898558SAl Viro #include <asm/cpufeature.h> 9e2780a68SIngo Molnar #include <asm/apicdef.h> 1060063497SArun Sharma #include <linux/atomic.h> 11e2780a68SIngo Molnar #include <asm/fixmap.h> 12e2780a68SIngo Molnar #include <asm/mpspec.h> 13bb898558SAl Viro #include <asm/msr.h> 14eddc0e92SSeiji Aguchi #include <asm/idle.h> 15bb898558SAl Viro 16bb898558SAl Viro #define ARCH_APICTIMER_STOPS_ON_C3 1 17bb898558SAl Viro 18bb898558SAl Viro /* 19bb898558SAl Viro * Debugging macros 20bb898558SAl Viro */ 21bb898558SAl Viro #define APIC_QUIET 0 22bb898558SAl Viro #define APIC_VERBOSE 1 23bb898558SAl Viro #define APIC_DEBUG 2 24bb898558SAl Viro 25b7c4948eSHidehiro Kawai /* Macros for apic_extnmi which controls external NMI masking */ 26b7c4948eSHidehiro Kawai #define APIC_EXTNMI_BSP 0 /* Default */ 27b7c4948eSHidehiro Kawai #define APIC_EXTNMI_ALL 1 28b7c4948eSHidehiro Kawai #define APIC_EXTNMI_NONE 2 29b7c4948eSHidehiro Kawai 30bb898558SAl Viro /* 31bb898558SAl Viro * Define the default level of output to be very little 32bb898558SAl Viro * This can be turned up by using apic=verbose for more 33bb898558SAl Viro * information and apic=debug for _lots_ of information. 34bb898558SAl Viro * apic_verbosity is defined in apic.c 35bb898558SAl Viro */ 36bb898558SAl Viro #define apic_printk(v, s, a...) do { \ 37bb898558SAl Viro if ((v) <= apic_verbosity) \ 38bb898558SAl Viro printk(s, ##a); \ 39bb898558SAl Viro } while (0) 40bb898558SAl Viro 41bb898558SAl Viro 42160d8dacSIngo Molnar #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32) 43bb898558SAl Viro extern void generic_apic_probe(void); 44160d8dacSIngo Molnar #else 45160d8dacSIngo Molnar static inline void generic_apic_probe(void) 46160d8dacSIngo Molnar { 47160d8dacSIngo Molnar } 48160d8dacSIngo Molnar #endif 49bb898558SAl Viro 50bb898558SAl Viro #ifdef CONFIG_X86_LOCAL_APIC 51bb898558SAl Viro 52bb898558SAl Viro extern unsigned int apic_verbosity; 53bb898558SAl Viro extern int local_apic_timer_c2_ok; 54bb898558SAl Viro 55bb898558SAl Viro extern int disable_apic; 561ade93efSJacob Pan extern unsigned int lapic_timer_frequency; 570939e4fdSIngo Molnar 580939e4fdSIngo Molnar #ifdef CONFIG_SMP 590939e4fdSIngo Molnar extern void __inquire_remote_apic(int apicid); 600939e4fdSIngo Molnar #else /* CONFIG_SMP */ 610939e4fdSIngo Molnar static inline void __inquire_remote_apic(int apicid) 620939e4fdSIngo Molnar { 630939e4fdSIngo Molnar } 640939e4fdSIngo Molnar #endif /* CONFIG_SMP */ 650939e4fdSIngo Molnar 660939e4fdSIngo Molnar static inline void default_inquire_remote_apic(int apicid) 670939e4fdSIngo Molnar { 680939e4fdSIngo Molnar if (apic_verbosity >= APIC_DEBUG) 690939e4fdSIngo Molnar __inquire_remote_apic(apicid); 700939e4fdSIngo Molnar } 710939e4fdSIngo Molnar 72bb898558SAl Viro /* 738312136fSCyrill Gorcunov * With 82489DX we can't rely on apic feature bit 748312136fSCyrill Gorcunov * retrieved via cpuid but still have to deal with 758312136fSCyrill Gorcunov * such an apic chip so we assume that SMP configuration 768312136fSCyrill Gorcunov * is found from MP table (64bit case uses ACPI mostly 778312136fSCyrill Gorcunov * which set smp presence flag as well so we are safe 788312136fSCyrill Gorcunov * to use this helper too). 798312136fSCyrill Gorcunov */ 808312136fSCyrill Gorcunov static inline bool apic_from_smp_config(void) 818312136fSCyrill Gorcunov { 828312136fSCyrill Gorcunov return smp_found_config && !disable_apic; 838312136fSCyrill Gorcunov } 848312136fSCyrill Gorcunov 858312136fSCyrill Gorcunov /* 86bb898558SAl Viro * Basic functions accessing APICs. 87bb898558SAl Viro */ 88bb898558SAl Viro #ifdef CONFIG_PARAVIRT 89bb898558SAl Viro #include <asm/paravirt.h> 90bb898558SAl Viro #endif 91bb898558SAl Viro 92bb898558SAl Viro extern int setup_profiling_timer(unsigned int); 93bb898558SAl Viro 94bb898558SAl Viro static inline void native_apic_mem_write(u32 reg, u32 v) 95bb898558SAl Viro { 96bb898558SAl Viro volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg); 97bb898558SAl Viro 98a930dc45SBorislav Petkov alternative_io("movl %0, %P1", "xchgl %0, %P1", X86_BUG_11AP, 99bb898558SAl Viro ASM_OUTPUT2("=r" (v), "=m" (*addr)), 100bb898558SAl Viro ASM_OUTPUT2("0" (v), "m" (*addr))); 101bb898558SAl Viro } 102bb898558SAl Viro 103bb898558SAl Viro static inline u32 native_apic_mem_read(u32 reg) 104bb898558SAl Viro { 105bb898558SAl Viro return *((volatile u32 *)(APIC_BASE + reg)); 106bb898558SAl Viro } 107bb898558SAl Viro 108c1eeb2deSYinghai Lu extern void native_apic_wait_icr_idle(void); 109c1eeb2deSYinghai Lu extern u32 native_safe_apic_wait_icr_idle(void); 110c1eeb2deSYinghai Lu extern void native_apic_icr_write(u32 low, u32 id); 111c1eeb2deSYinghai Lu extern u64 native_apic_icr_read(void); 112c1eeb2deSYinghai Lu 1138d806960SThomas Gleixner static inline bool apic_is_x2apic_enabled(void) 1148d806960SThomas Gleixner { 1158d806960SThomas Gleixner u64 msr; 1168d806960SThomas Gleixner 1178d806960SThomas Gleixner if (rdmsrl_safe(MSR_IA32_APICBASE, &msr)) 1188d806960SThomas Gleixner return false; 1198d806960SThomas Gleixner return msr & X2APIC_ENABLE; 1208d806960SThomas Gleixner } 1218d806960SThomas Gleixner 122e02ae387SPaolo Bonzini extern void enable_IR_x2apic(void); 123e02ae387SPaolo Bonzini 124e02ae387SPaolo Bonzini extern int get_physical_broadcast(void); 125e02ae387SPaolo Bonzini 126e02ae387SPaolo Bonzini extern int lapic_get_maxlvt(void); 127e02ae387SPaolo Bonzini extern void clear_local_APIC(void); 128e02ae387SPaolo Bonzini extern void disconnect_bsp_APIC(int virt_wire_setup); 129e02ae387SPaolo Bonzini extern void disable_local_APIC(void); 130e02ae387SPaolo Bonzini extern void lapic_shutdown(void); 131e02ae387SPaolo Bonzini extern void sync_Arb_IDs(void); 132e02ae387SPaolo Bonzini extern void init_bsp_APIC(void); 133e02ae387SPaolo Bonzini extern void setup_local_APIC(void); 134e02ae387SPaolo Bonzini extern void init_apic_mappings(void); 135e02ae387SPaolo Bonzini void register_lapic_address(unsigned long address); 136e02ae387SPaolo Bonzini extern void setup_boot_APIC_clock(void); 137e02ae387SPaolo Bonzini extern void setup_secondary_APIC_clock(void); 138e02ae387SPaolo Bonzini extern int APIC_init_uniprocessor(void); 139e02ae387SPaolo Bonzini 140e02ae387SPaolo Bonzini #ifdef CONFIG_X86_64 141e02ae387SPaolo Bonzini static inline int apic_force_enable(unsigned long addr) 142e02ae387SPaolo Bonzini { 143e02ae387SPaolo Bonzini return -1; 144e02ae387SPaolo Bonzini } 145e02ae387SPaolo Bonzini #else 146e02ae387SPaolo Bonzini extern int apic_force_enable(unsigned long addr); 147e02ae387SPaolo Bonzini #endif 148e02ae387SPaolo Bonzini 149e02ae387SPaolo Bonzini extern int apic_bsp_setup(bool upmode); 150e02ae387SPaolo Bonzini extern void apic_ap_setup(void); 151e02ae387SPaolo Bonzini 152e02ae387SPaolo Bonzini /* 153e02ae387SPaolo Bonzini * On 32bit this is mach-xxx local 154e02ae387SPaolo Bonzini */ 155e02ae387SPaolo Bonzini #ifdef CONFIG_X86_64 156e02ae387SPaolo Bonzini extern int apic_is_clustered_box(void); 157e02ae387SPaolo Bonzini #else 158e02ae387SPaolo Bonzini static inline int apic_is_clustered_box(void) 159e02ae387SPaolo Bonzini { 160e02ae387SPaolo Bonzini return 0; 161e02ae387SPaolo Bonzini } 162e02ae387SPaolo Bonzini #endif 163e02ae387SPaolo Bonzini 164e02ae387SPaolo Bonzini extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask); 165e02ae387SPaolo Bonzini 166e02ae387SPaolo Bonzini #else /* !CONFIG_X86_LOCAL_APIC */ 167e02ae387SPaolo Bonzini static inline void lapic_shutdown(void) { } 168e02ae387SPaolo Bonzini #define local_apic_timer_c2_ok 1 169e02ae387SPaolo Bonzini static inline void init_apic_mappings(void) { } 170e02ae387SPaolo Bonzini static inline void disable_local_APIC(void) { } 171e02ae387SPaolo Bonzini # define setup_boot_APIC_clock x86_init_noop 172e02ae387SPaolo Bonzini # define setup_secondary_APIC_clock x86_init_noop 173e02ae387SPaolo Bonzini #endif /* !CONFIG_X86_LOCAL_APIC */ 174e02ae387SPaolo Bonzini 175d0b03bd1SHan, Weidong #ifdef CONFIG_X86_X2APIC 176ce4e240cSSuresh Siddha /* 177ce4e240cSSuresh Siddha * Make previous memory operations globally visible before 178ce4e240cSSuresh Siddha * sending the IPI through x2apic wrmsr. We need a serializing instruction or 179ce4e240cSSuresh Siddha * mfence for this. 180ce4e240cSSuresh Siddha */ 181ce4e240cSSuresh Siddha static inline void x2apic_wrmsr_fence(void) 182ce4e240cSSuresh Siddha { 183ce4e240cSSuresh Siddha asm volatile("mfence" : : : "memory"); 184ce4e240cSSuresh Siddha } 185ce4e240cSSuresh Siddha 186bb898558SAl Viro static inline void native_apic_msr_write(u32 reg, u32 v) 187bb898558SAl Viro { 188bb898558SAl Viro if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR || 189bb898558SAl Viro reg == APIC_LVR) 190bb898558SAl Viro return; 191bb898558SAl Viro 192bb898558SAl Viro wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0); 193bb898558SAl Viro } 194bb898558SAl Viro 1950ab711aeSMichael S. Tsirkin static inline void native_apic_msr_eoi_write(u32 reg, u32 v) 1960ab711aeSMichael S. Tsirkin { 1970ab711aeSMichael S. Tsirkin wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0); 1980ab711aeSMichael S. Tsirkin } 1990ab711aeSMichael S. Tsirkin 200bb898558SAl Viro static inline u32 native_apic_msr_read(u32 reg) 201bb898558SAl Viro { 2020059b243SAndi Kleen u64 msr; 203bb898558SAl Viro 204bb898558SAl Viro if (reg == APIC_DFR) 205bb898558SAl Viro return -1; 206bb898558SAl Viro 2070059b243SAndi Kleen rdmsrl(APIC_BASE_MSR + (reg >> 4), msr); 2080059b243SAndi Kleen return (u32)msr; 209bb898558SAl Viro } 210bb898558SAl Viro 211c1eeb2deSYinghai Lu static inline void native_x2apic_wait_icr_idle(void) 212c1eeb2deSYinghai Lu { 213c1eeb2deSYinghai Lu /* no need to wait for icr idle in x2apic */ 214c1eeb2deSYinghai Lu return; 215c1eeb2deSYinghai Lu } 216c1eeb2deSYinghai Lu 217c1eeb2deSYinghai Lu static inline u32 native_safe_x2apic_wait_icr_idle(void) 218c1eeb2deSYinghai Lu { 219c1eeb2deSYinghai Lu /* no need to wait for icr idle in x2apic */ 220c1eeb2deSYinghai Lu return 0; 221c1eeb2deSYinghai Lu } 222c1eeb2deSYinghai Lu 223c1eeb2deSYinghai Lu static inline void native_x2apic_icr_write(u32 low, u32 id) 224c1eeb2deSYinghai Lu { 225c1eeb2deSYinghai Lu wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low); 226c1eeb2deSYinghai Lu } 227c1eeb2deSYinghai Lu 228c1eeb2deSYinghai Lu static inline u64 native_x2apic_icr_read(void) 229c1eeb2deSYinghai Lu { 230c1eeb2deSYinghai Lu unsigned long val; 231c1eeb2deSYinghai Lu 232c1eeb2deSYinghai Lu rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val); 233c1eeb2deSYinghai Lu return val; 234c1eeb2deSYinghai Lu } 235c1eeb2deSYinghai Lu 23681a46dd8SThomas Gleixner extern int x2apic_mode; 237fc1edaf9SSuresh Siddha extern int x2apic_phys; 238d524165cSThomas Gleixner extern void __init check_x2apic(void); 239659006bfSThomas Gleixner extern void x2apic_setup(void); 240bb898558SAl Viro static inline int x2apic_enabled(void) 241bb898558SAl Viro { 24262436a4dSBorislav Petkov return boot_cpu_has(X86_FEATURE_X2APIC) && apic_is_x2apic_enabled(); 243bb898558SAl Viro } 244fc1edaf9SSuresh Siddha 24562436a4dSBorislav Petkov #define x2apic_supported() (boot_cpu_has(X86_FEATURE_X2APIC)) 246e02ae387SPaolo Bonzini #else /* !CONFIG_X86_X2APIC */ 24755eae7deSThomas Gleixner static inline void check_x2apic(void) { } 248659006bfSThomas Gleixner static inline void x2apic_setup(void) { } 24955eae7deSThomas Gleixner static inline int x2apic_enabled(void) { return 0; } 250cf6567feSSuresh Siddha 25181a46dd8SThomas Gleixner #define x2apic_mode (0) 25281a46dd8SThomas Gleixner #define x2apic_supported() (0) 253e02ae387SPaolo Bonzini #endif /* !CONFIG_X86_X2APIC */ 254bb898558SAl Viro 2551f75ed0cSIngo Molnar #ifdef CONFIG_X86_64 2561f75ed0cSIngo Molnar #define SET_APIC_ID(x) (apic->set_apic_id(x)) 2571f75ed0cSIngo Molnar #else 2581f75ed0cSIngo Molnar 2591f75ed0cSIngo Molnar #endif 2601f75ed0cSIngo Molnar 261e2780a68SIngo Molnar /* 262e2780a68SIngo Molnar * Copyright 2004 James Cleverdon, IBM. 263e2780a68SIngo Molnar * Subject to the GNU Public License, v.2 264e2780a68SIngo Molnar * 265e2780a68SIngo Molnar * Generic APIC sub-arch data struct. 266e2780a68SIngo Molnar * 267e2780a68SIngo Molnar * Hacked for x86-64 by James Cleverdon from i386 architecture code by 268e2780a68SIngo Molnar * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and 269e2780a68SIngo Molnar * James Cleverdon. 270e2780a68SIngo Molnar */ 271be163a15SIngo Molnar struct apic { 272e2780a68SIngo Molnar char *name; 273e2780a68SIngo Molnar 274e2780a68SIngo Molnar int (*probe)(void); 275e2780a68SIngo Molnar int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id); 276fa63030eSDaniel J Blueman int (*apic_id_valid)(int apicid); 277e2780a68SIngo Molnar int (*apic_id_registered)(void); 278e2780a68SIngo Molnar 279e2780a68SIngo Molnar u32 irq_delivery_mode; 280e2780a68SIngo Molnar u32 irq_dest_mode; 281e2780a68SIngo Molnar 282e2780a68SIngo Molnar const struct cpumask *(*target_cpus)(void); 283e2780a68SIngo Molnar 284e2780a68SIngo Molnar int disable_esr; 285e2780a68SIngo Molnar 286e2780a68SIngo Molnar int dest_logical; 2877abc0753SCyrill Gorcunov unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid); 288e2780a68SIngo Molnar 2891ac322d0SSuresh Siddha void (*vector_allocation_domain)(int cpu, struct cpumask *retmask, 2901ac322d0SSuresh Siddha const struct cpumask *mask); 291e2780a68SIngo Molnar void (*init_apic_ldr)(void); 292e2780a68SIngo Molnar 2937abc0753SCyrill Gorcunov void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap); 294e2780a68SIngo Molnar 295e2780a68SIngo Molnar void (*setup_apic_routing)(void); 296e2780a68SIngo Molnar int (*cpu_present_to_apicid)(int mps_cpu); 2977abc0753SCyrill Gorcunov void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap); 298e11dadabSThomas Gleixner int (*check_phys_apicid_present)(int phys_apicid); 299e2780a68SIngo Molnar int (*phys_pkg_id)(int cpuid_apic, int index_msb); 300e2780a68SIngo Molnar 301e2780a68SIngo Molnar unsigned int (*get_apic_id)(unsigned long x); 302e2780a68SIngo Molnar unsigned long (*set_apic_id)(unsigned int id); 303e2780a68SIngo Molnar unsigned long apic_id_mask; 304e2780a68SIngo Molnar 305ff164324SAlexander Gordeev int (*cpu_mask_to_apicid_and)(const struct cpumask *cpumask, 306ff164324SAlexander Gordeev const struct cpumask *andmask, 307ff164324SAlexander Gordeev unsigned int *apicid); 308e2780a68SIngo Molnar 309e2780a68SIngo Molnar /* ipi */ 310539da787SLinus Torvalds void (*send_IPI)(int cpu, int vector); 311e2780a68SIngo Molnar void (*send_IPI_mask)(const struct cpumask *mask, int vector); 312e2780a68SIngo Molnar void (*send_IPI_mask_allbutself)(const struct cpumask *mask, 313e2780a68SIngo Molnar int vector); 314e2780a68SIngo Molnar void (*send_IPI_allbutself)(int vector); 315e2780a68SIngo Molnar void (*send_IPI_all)(int vector); 316e2780a68SIngo Molnar void (*send_IPI_self)(int vector); 317e2780a68SIngo Molnar 318e2780a68SIngo Molnar /* wakeup_secondary_cpu */ 3191f5bcabfSIngo Molnar int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip); 320e2780a68SIngo Molnar 321e2780a68SIngo Molnar void (*inquire_remote_apic)(int apicid); 322e2780a68SIngo Molnar 323e2780a68SIngo Molnar /* apic ops */ 324e2780a68SIngo Molnar u32 (*read)(u32 reg); 325e2780a68SIngo Molnar void (*write)(u32 reg, u32 v); 3262a43195dSMichael S. Tsirkin /* 3272a43195dSMichael S. Tsirkin * ->eoi_write() has the same signature as ->write(). 3282a43195dSMichael S. Tsirkin * 3292a43195dSMichael S. Tsirkin * Drivers can support both ->eoi_write() and ->write() by passing the same 3302a43195dSMichael S. Tsirkin * callback value. Kernel can override ->eoi_write() and fall back 3312a43195dSMichael S. Tsirkin * on write for EOI. 3322a43195dSMichael S. Tsirkin */ 3332a43195dSMichael S. Tsirkin void (*eoi_write)(u32 reg, u32 v); 334e2780a68SIngo Molnar u64 (*icr_read)(void); 335e2780a68SIngo Molnar void (*icr_write)(u32 low, u32 high); 336e2780a68SIngo Molnar void (*wait_icr_idle)(void); 337e2780a68SIngo Molnar u32 (*safe_wait_icr_idle)(void); 338acb8bc09STejun Heo 339acb8bc09STejun Heo #ifdef CONFIG_X86_32 340acb8bc09STejun Heo /* 341acb8bc09STejun Heo * Called very early during boot from get_smp_config(). It should 342acb8bc09STejun Heo * return the logical apicid. x86_[bios]_cpu_to_apicid is 343acb8bc09STejun Heo * initialized before this function is called. 344acb8bc09STejun Heo * 345acb8bc09STejun Heo * If logical apicid can't be determined that early, the function 346acb8bc09STejun Heo * may return BAD_APICID. Logical apicid will be configured after 347acb8bc09STejun Heo * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity 348acb8bc09STejun Heo * won't be applied properly during early boot in this case. 349acb8bc09STejun Heo */ 350acb8bc09STejun Heo int (*x86_32_early_logical_apicid)(int cpu); 351acb8bc09STejun Heo #endif 352e2780a68SIngo Molnar }; 353e2780a68SIngo Molnar 3540917c01fSIngo Molnar /* 3550917c01fSIngo Molnar * Pointer to the local APIC driver in use on this system (there's 3560917c01fSIngo Molnar * always just one such driver in use - the kernel decides via an 3570917c01fSIngo Molnar * early probing process which one it picks - and then sticks to it): 3580917c01fSIngo Molnar */ 359be163a15SIngo Molnar extern struct apic *apic; 3600917c01fSIngo Molnar 3610917c01fSIngo Molnar /* 362107e0e0cSSuresh Siddha * APIC drivers are probed based on how they are listed in the .apicdrivers 363107e0e0cSSuresh Siddha * section. So the order is important and enforced by the ordering 364107e0e0cSSuresh Siddha * of different apic driver files in the Makefile. 365107e0e0cSSuresh Siddha * 366107e0e0cSSuresh Siddha * For the files having two apic drivers, we use apic_drivers() 367107e0e0cSSuresh Siddha * to enforce the order with in them. 368107e0e0cSSuresh Siddha */ 369107e0e0cSSuresh Siddha #define apic_driver(sym) \ 37075fdd155SAndi Kleen static const struct apic *__apicdrivers_##sym __used \ 371107e0e0cSSuresh Siddha __aligned(sizeof(struct apic *)) \ 372107e0e0cSSuresh Siddha __section(.apicdrivers) = { &sym } 373107e0e0cSSuresh Siddha 374107e0e0cSSuresh Siddha #define apic_drivers(sym1, sym2) \ 375107e0e0cSSuresh Siddha static struct apic *__apicdrivers_##sym1##sym2[2] __used \ 376107e0e0cSSuresh Siddha __aligned(sizeof(struct apic *)) \ 377107e0e0cSSuresh Siddha __section(.apicdrivers) = { &sym1, &sym2 } 378107e0e0cSSuresh Siddha 379107e0e0cSSuresh Siddha extern struct apic *__apicdrivers[], *__apicdrivers_end[]; 380107e0e0cSSuresh Siddha 381107e0e0cSSuresh Siddha /* 3820917c01fSIngo Molnar * APIC functionality to boot other CPUs - only used on SMP: 3830917c01fSIngo Molnar */ 3840917c01fSIngo Molnar #ifdef CONFIG_SMP 3852b6163bfSYinghai Lu extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip); 3860917c01fSIngo Molnar #endif 387e2780a68SIngo Molnar 388d674cd19SCyrill Gorcunov #ifdef CONFIG_X86_LOCAL_APIC 389346b46beSFernando Luis Vázquez Cao 390e2780a68SIngo Molnar static inline u32 apic_read(u32 reg) 391e2780a68SIngo Molnar { 392e2780a68SIngo Molnar return apic->read(reg); 393e2780a68SIngo Molnar } 394e2780a68SIngo Molnar 395e2780a68SIngo Molnar static inline void apic_write(u32 reg, u32 val) 396e2780a68SIngo Molnar { 397e2780a68SIngo Molnar apic->write(reg, val); 398e2780a68SIngo Molnar } 399e2780a68SIngo Molnar 4002a43195dSMichael S. Tsirkin static inline void apic_eoi(void) 4012a43195dSMichael S. Tsirkin { 4022a43195dSMichael S. Tsirkin apic->eoi_write(APIC_EOI, APIC_EOI_ACK); 4032a43195dSMichael S. Tsirkin } 4042a43195dSMichael S. Tsirkin 405e2780a68SIngo Molnar static inline u64 apic_icr_read(void) 406e2780a68SIngo Molnar { 407e2780a68SIngo Molnar return apic->icr_read(); 408e2780a68SIngo Molnar } 409e2780a68SIngo Molnar 410e2780a68SIngo Molnar static inline void apic_icr_write(u32 low, u32 high) 411e2780a68SIngo Molnar { 412e2780a68SIngo Molnar apic->icr_write(low, high); 413e2780a68SIngo Molnar } 414e2780a68SIngo Molnar 415e2780a68SIngo Molnar static inline void apic_wait_icr_idle(void) 416e2780a68SIngo Molnar { 417e2780a68SIngo Molnar apic->wait_icr_idle(); 418e2780a68SIngo Molnar } 419e2780a68SIngo Molnar 420e2780a68SIngo Molnar static inline u32 safe_apic_wait_icr_idle(void) 421e2780a68SIngo Molnar { 422e2780a68SIngo Molnar return apic->safe_wait_icr_idle(); 423e2780a68SIngo Molnar } 424e2780a68SIngo Molnar 4251551df64SMichael S. Tsirkin extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)); 4261551df64SMichael S. Tsirkin 427d674cd19SCyrill Gorcunov #else /* CONFIG_X86_LOCAL_APIC */ 428d674cd19SCyrill Gorcunov 429d674cd19SCyrill Gorcunov static inline u32 apic_read(u32 reg) { return 0; } 430d674cd19SCyrill Gorcunov static inline void apic_write(u32 reg, u32 val) { } 4312a43195dSMichael S. Tsirkin static inline void apic_eoi(void) { } 432d674cd19SCyrill Gorcunov static inline u64 apic_icr_read(void) { return 0; } 433d674cd19SCyrill Gorcunov static inline void apic_icr_write(u32 low, u32 high) { } 434d674cd19SCyrill Gorcunov static inline void apic_wait_icr_idle(void) { } 435d674cd19SCyrill Gorcunov static inline u32 safe_apic_wait_icr_idle(void) { return 0; } 4361551df64SMichael S. Tsirkin static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {} 437d674cd19SCyrill Gorcunov 438d674cd19SCyrill Gorcunov #endif /* CONFIG_X86_LOCAL_APIC */ 439e2780a68SIngo Molnar 440e2780a68SIngo Molnar static inline void ack_APIC_irq(void) 441e2780a68SIngo Molnar { 442e2780a68SIngo Molnar /* 443e2780a68SIngo Molnar * ack_APIC_irq() actually gets compiled as a single instruction 444e2780a68SIngo Molnar * ... yummie. 445e2780a68SIngo Molnar */ 4462a43195dSMichael S. Tsirkin apic_eoi(); 447e2780a68SIngo Molnar } 448e2780a68SIngo Molnar 449e2780a68SIngo Molnar static inline unsigned default_get_apic_id(unsigned long x) 450e2780a68SIngo Molnar { 451e2780a68SIngo Molnar unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR)); 452e2780a68SIngo Molnar 45342937e81SAndreas Herrmann if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID)) 454e2780a68SIngo Molnar return (x >> 24) & 0xFF; 455e2780a68SIngo Molnar else 456e2780a68SIngo Molnar return (x >> 24) & 0x0F; 457e2780a68SIngo Molnar } 458e2780a68SIngo Molnar 459e2780a68SIngo Molnar /* 4606ab1b27cSDavid Rientjes * Warm reset vector position: 461e2780a68SIngo Molnar */ 4626ab1b27cSDavid Rientjes #define TRAMPOLINE_PHYS_LOW 0x467 4636ab1b27cSDavid Rientjes #define TRAMPOLINE_PHYS_HIGH 0x469 464e2780a68SIngo Molnar 4652b6163bfSYinghai Lu #ifdef CONFIG_X86_64 466e2780a68SIngo Molnar extern void apic_send_IPI_self(int vector); 467e2780a68SIngo Molnar 468e2780a68SIngo Molnar DECLARE_PER_CPU(int, x2apic_extra_bits); 469e2780a68SIngo Molnar 470e2780a68SIngo Molnar extern int default_cpu_present_to_apicid(int mps_cpu); 471e11dadabSThomas Gleixner extern int default_check_phys_apicid_present(int phys_apicid); 472e2780a68SIngo Molnar #endif 473e2780a68SIngo Molnar 474838312beSJan Beulich extern void generic_bigsmp_probe(void); 475e2780a68SIngo Molnar 476e2780a68SIngo Molnar 477e2780a68SIngo Molnar #ifdef CONFIG_X86_LOCAL_APIC 478e2780a68SIngo Molnar 479e2780a68SIngo Molnar #include <asm/smp.h> 480e2780a68SIngo Molnar 481e2780a68SIngo Molnar #define APIC_DFR_VALUE (APIC_DFR_FLAT) 482e2780a68SIngo Molnar 483e2780a68SIngo Molnar static inline const struct cpumask *default_target_cpus(void) 484e2780a68SIngo Molnar { 485e2780a68SIngo Molnar #ifdef CONFIG_SMP 486e2780a68SIngo Molnar return cpu_online_mask; 487e2780a68SIngo Molnar #else 488e2780a68SIngo Molnar return cpumask_of(0); 489e2780a68SIngo Molnar #endif 490e2780a68SIngo Molnar } 491e2780a68SIngo Molnar 492bf721d3aSAlexander Gordeev static inline const struct cpumask *online_target_cpus(void) 493bf721d3aSAlexander Gordeev { 494bf721d3aSAlexander Gordeev return cpu_online_mask; 495bf721d3aSAlexander Gordeev } 496bf721d3aSAlexander Gordeev 4970816b0f0SVlad Zolotarov DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid); 498e2780a68SIngo Molnar 499e2780a68SIngo Molnar 500e2780a68SIngo Molnar static inline unsigned int read_apic_id(void) 501e2780a68SIngo Molnar { 502e2780a68SIngo Molnar unsigned int reg; 503e2780a68SIngo Molnar 504e2780a68SIngo Molnar reg = apic_read(APIC_ID); 505e2780a68SIngo Molnar 506e2780a68SIngo Molnar return apic->get_apic_id(reg); 507e2780a68SIngo Molnar } 508e2780a68SIngo Molnar 509fa63030eSDaniel J Blueman static inline int default_apic_id_valid(int apicid) 510fa63030eSDaniel J Blueman { 511b7157acfSSteffen Persvold return (apicid < 255); 512fa63030eSDaniel J Blueman } 513fa63030eSDaniel J Blueman 514a491cc90SJiang Liu extern int default_acpi_madt_oem_check(char *, char *); 515a491cc90SJiang Liu 516e2780a68SIngo Molnar extern void default_setup_apic_routing(void); 517e2780a68SIngo Molnar 5189844ab11SCyrill Gorcunov extern struct apic apic_noop; 5199844ab11SCyrill Gorcunov 520e2780a68SIngo Molnar #ifdef CONFIG_X86_32 5212c1b284eSJaswinder Singh Rajput 522acb8bc09STejun Heo static inline int noop_x86_32_early_logical_apicid(int cpu) 523acb8bc09STejun Heo { 524acb8bc09STejun Heo return BAD_APICID; 525acb8bc09STejun Heo } 526acb8bc09STejun Heo 527e2780a68SIngo Molnar /* 528e2780a68SIngo Molnar * Set up the logical destination ID. 529e2780a68SIngo Molnar * 530e2780a68SIngo Molnar * Intel recommends to set DFR, LDR and TPR before enabling 531e2780a68SIngo Molnar * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel 532e2780a68SIngo Molnar * document number 292116). So here it goes... 533e2780a68SIngo Molnar */ 534e2780a68SIngo Molnar extern void default_init_apic_ldr(void); 535e2780a68SIngo Molnar 536e2780a68SIngo Molnar static inline int default_apic_id_registered(void) 537e2780a68SIngo Molnar { 538e2780a68SIngo Molnar return physid_isset(read_apic_id(), phys_cpu_present_map); 539e2780a68SIngo Molnar } 540e2780a68SIngo Molnar 541f56e5034SYinghai Lu static inline int default_phys_pkg_id(int cpuid_apic, int index_msb) 542f56e5034SYinghai Lu { 543f56e5034SYinghai Lu return cpuid_apic >> index_msb; 544f56e5034SYinghai Lu } 545f56e5034SYinghai Lu 546f56e5034SYinghai Lu #endif 547f56e5034SYinghai Lu 548ff164324SAlexander Gordeev static inline int 549a5a39156SAlexander Gordeev flat_cpu_mask_to_apicid_and(const struct cpumask *cpumask, 550a5a39156SAlexander Gordeev const struct cpumask *andmask, 551a5a39156SAlexander Gordeev unsigned int *apicid) 552e2780a68SIngo Molnar { 553a5a39156SAlexander Gordeev unsigned long cpu_mask = cpumask_bits(cpumask)[0] & 554a5a39156SAlexander Gordeev cpumask_bits(andmask)[0] & 555a5a39156SAlexander Gordeev cpumask_bits(cpu_online_mask)[0] & 556a5a39156SAlexander Gordeev APIC_ALL_CPUS; 557a5a39156SAlexander Gordeev 558ff164324SAlexander Gordeev if (likely(cpu_mask)) { 559ff164324SAlexander Gordeev *apicid = (unsigned int)cpu_mask; 560ff164324SAlexander Gordeev return 0; 561ff164324SAlexander Gordeev } else { 562ff164324SAlexander Gordeev return -EINVAL; 563ff164324SAlexander Gordeev } 564e2780a68SIngo Molnar } 565e2780a68SIngo Molnar 566ff164324SAlexander Gordeev extern int 5676398268dSAlexander Gordeev default_cpu_mask_to_apicid_and(const struct cpumask *cpumask, 568ff164324SAlexander Gordeev const struct cpumask *andmask, 569ff164324SAlexander Gordeev unsigned int *apicid); 5706398268dSAlexander Gordeev 571b39f25a8SSuresh Siddha static inline void 5721ac322d0SSuresh Siddha flat_vector_allocation_domain(int cpu, struct cpumask *retmask, 5731ac322d0SSuresh Siddha const struct cpumask *mask) 5749d8e1066SAlexander Gordeev { 5759d8e1066SAlexander Gordeev /* Careful. Some cpus do not strictly honor the set of cpus 5769d8e1066SAlexander Gordeev * specified in the interrupt destination when using lowest 5779d8e1066SAlexander Gordeev * priority interrupt delivery mode. 5789d8e1066SAlexander Gordeev * 5799d8e1066SAlexander Gordeev * In particular there was a hyperthreading cpu observed to 5809d8e1066SAlexander Gordeev * deliver interrupts to the wrong hyperthread when only one 5819d8e1066SAlexander Gordeev * hyperthread was specified in the interrupt desitination. 5829d8e1066SAlexander Gordeev */ 5839d8e1066SAlexander Gordeev cpumask_clear(retmask); 5849d8e1066SAlexander Gordeev cpumask_bits(retmask)[0] = APIC_ALL_CPUS; 5859d8e1066SAlexander Gordeev } 5869d8e1066SAlexander Gordeev 587b39f25a8SSuresh Siddha static inline void 5881ac322d0SSuresh Siddha default_vector_allocation_domain(int cpu, struct cpumask *retmask, 5891ac322d0SSuresh Siddha const struct cpumask *mask) 5909d8e1066SAlexander Gordeev { 5919d8e1066SAlexander Gordeev cpumask_copy(retmask, cpumask_of(cpu)); 5929d8e1066SAlexander Gordeev } 5939d8e1066SAlexander Gordeev 5947abc0753SCyrill Gorcunov static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid) 595e2780a68SIngo Molnar { 5967abc0753SCyrill Gorcunov return physid_isset(apicid, *map); 597e2780a68SIngo Molnar } 598e2780a68SIngo Molnar 5997abc0753SCyrill Gorcunov static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap) 600e2780a68SIngo Molnar { 6017abc0753SCyrill Gorcunov *retmap = *phys_map; 602e2780a68SIngo Molnar } 603e2780a68SIngo Molnar 604e2780a68SIngo Molnar static inline int __default_cpu_present_to_apicid(int mps_cpu) 605e2780a68SIngo Molnar { 606e2780a68SIngo Molnar if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu)) 607e2780a68SIngo Molnar return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu); 608e2780a68SIngo Molnar else 609e2780a68SIngo Molnar return BAD_APICID; 610e2780a68SIngo Molnar } 611e2780a68SIngo Molnar 612e2780a68SIngo Molnar static inline int 613e11dadabSThomas Gleixner __default_check_phys_apicid_present(int phys_apicid) 614e2780a68SIngo Molnar { 615e11dadabSThomas Gleixner return physid_isset(phys_apicid, phys_cpu_present_map); 616e2780a68SIngo Molnar } 617e2780a68SIngo Molnar 618e2780a68SIngo Molnar #ifdef CONFIG_X86_32 619e2780a68SIngo Molnar static inline int default_cpu_present_to_apicid(int mps_cpu) 620e2780a68SIngo Molnar { 621e2780a68SIngo Molnar return __default_cpu_present_to_apicid(mps_cpu); 622e2780a68SIngo Molnar } 623e2780a68SIngo Molnar 624e2780a68SIngo Molnar static inline int 625e11dadabSThomas Gleixner default_check_phys_apicid_present(int phys_apicid) 626e2780a68SIngo Molnar { 627e11dadabSThomas Gleixner return __default_check_phys_apicid_present(phys_apicid); 628e2780a68SIngo Molnar } 629e2780a68SIngo Molnar #else 630e2780a68SIngo Molnar extern int default_cpu_present_to_apicid(int mps_cpu); 631e11dadabSThomas Gleixner extern int default_check_phys_apicid_present(int phys_apicid); 632e2780a68SIngo Molnar #endif 633e2780a68SIngo Molnar 634e2780a68SIngo Molnar #endif /* CONFIG_X86_LOCAL_APIC */ 635eddc0e92SSeiji Aguchi extern void irq_enter(void); 636eddc0e92SSeiji Aguchi extern void irq_exit(void); 637eddc0e92SSeiji Aguchi 638eddc0e92SSeiji Aguchi static inline void entering_irq(void) 639eddc0e92SSeiji Aguchi { 640eddc0e92SSeiji Aguchi irq_enter(); 641eddc0e92SSeiji Aguchi exit_idle(); 642eddc0e92SSeiji Aguchi } 643eddc0e92SSeiji Aguchi 644eddc0e92SSeiji Aguchi static inline void entering_ack_irq(void) 645eddc0e92SSeiji Aguchi { 646eddc0e92SSeiji Aguchi entering_irq(); 6477834c103SDave Jones ack_APIC_irq(); 648eddc0e92SSeiji Aguchi } 649eddc0e92SSeiji Aguchi 6506dc17876SThomas Gleixner static inline void ipi_entering_ack_irq(void) 6516dc17876SThomas Gleixner { 6526dc17876SThomas Gleixner ack_APIC_irq(); 6536dc17876SThomas Gleixner irq_enter(); 6546dc17876SThomas Gleixner } 6556dc17876SThomas Gleixner 656eddc0e92SSeiji Aguchi static inline void exiting_irq(void) 657eddc0e92SSeiji Aguchi { 658eddc0e92SSeiji Aguchi irq_exit(); 659eddc0e92SSeiji Aguchi } 660eddc0e92SSeiji Aguchi 661eddc0e92SSeiji Aguchi static inline void exiting_ack_irq(void) 662eddc0e92SSeiji Aguchi { 663eddc0e92SSeiji Aguchi irq_exit(); 664eddc0e92SSeiji Aguchi /* Ack only at the end to avoid potential reentry */ 665eddc0e92SSeiji Aguchi ack_APIC_irq(); 666eddc0e92SSeiji Aguchi } 667e2780a68SIngo Molnar 66817405453SYoshihiro YUNOMAE extern void ioapic_zap_locks(void); 66917405453SYoshihiro YUNOMAE 6701965aae3SH. Peter Anvin #endif /* _ASM_X86_APIC_H */ 671