1 #ifndef _ASM_X86_APIC_H
2 #define _ASM_X86_APIC_H
4 #include <linux/cpumask.h>
6 #include <asm/alternative.h>
7 #include <asm/cpufeature.h>
8 #include <asm/apicdef.h>
9 #include <linux/atomic.h>
10 #include <asm/fixmap.h>
11 #include <asm/mpspec.h>
13 #include <asm/hardirq.h>
15 #define ARCH_APICTIMER_STOPS_ON_C3 1
21 #define APIC_VERBOSE 1
24 /* Macros for apic_extnmi which controls external NMI masking */
25 #define APIC_EXTNMI_BSP 0 /* Default */
26 #define APIC_EXTNMI_ALL 1
27 #define APIC_EXTNMI_NONE 2
30 * Define the default level of output to be very little
31 * This can be turned up by using apic=verbose for more
32 * information and apic=debug for _lots_ of information.
33 * apic_verbosity is defined in apic.c
35 #define apic_printk(v, s, a...) do { \
36 if ((v) <= apic_verbosity) \
41 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32)
42 extern void generic_apic_probe(void);
44 static inline void generic_apic_probe(void)
49 #ifdef CONFIG_X86_LOCAL_APIC
51 extern unsigned int apic_verbosity;
52 extern int local_apic_timer_c2_ok;
54 extern int disable_apic;
55 extern unsigned int lapic_timer_frequency;
58 extern void __inquire_remote_apic(int apicid);
59 #else /* CONFIG_SMP */
60 static inline void __inquire_remote_apic(int apicid)
63 #endif /* CONFIG_SMP */
65 static inline void default_inquire_remote_apic(int apicid)
67 if (apic_verbosity >= APIC_DEBUG)
68 __inquire_remote_apic(apicid);
72 * With 82489DX we can't rely on apic feature bit
73 * retrieved via cpuid but still have to deal with
74 * such an apic chip so we assume that SMP configuration
75 * is found from MP table (64bit case uses ACPI mostly
76 * which set smp presence flag as well so we are safe
77 * to use this helper too).
79 static inline bool apic_from_smp_config(void)
81 return smp_found_config && !disable_apic;
85 * Basic functions accessing APICs.
87 #ifdef CONFIG_PARAVIRT
88 #include <asm/paravirt.h>
91 extern int setup_profiling_timer(unsigned int);
93 static inline void native_apic_mem_write(u32 reg, u32 v)
95 volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg);
97 alternative_io("movl %0, %P1", "xchgl %0, %P1", X86_BUG_11AP,
98 ASM_OUTPUT2("=r" (v), "=m" (*addr)),
99 ASM_OUTPUT2("0" (v), "m" (*addr)));
102 static inline u32 native_apic_mem_read(u32 reg)
104 return *((volatile u32 *)(APIC_BASE + reg));
107 extern void native_apic_wait_icr_idle(void);
108 extern u32 native_safe_apic_wait_icr_idle(void);
109 extern void native_apic_icr_write(u32 low, u32 id);
110 extern u64 native_apic_icr_read(void);
112 static inline bool apic_is_x2apic_enabled(void)
116 if (rdmsrl_safe(MSR_IA32_APICBASE, &msr))
118 return msr & X2APIC_ENABLE;
121 extern void enable_IR_x2apic(void);
123 extern int get_physical_broadcast(void);
125 extern int lapic_get_maxlvt(void);
126 extern void clear_local_APIC(void);
127 extern void disconnect_bsp_APIC(int virt_wire_setup);
128 extern void disable_local_APIC(void);
129 extern void lapic_shutdown(void);
130 extern void sync_Arb_IDs(void);
131 extern void init_bsp_APIC(void);
132 extern void setup_local_APIC(void);
133 extern void init_apic_mappings(void);
134 void register_lapic_address(unsigned long address);
135 extern void setup_boot_APIC_clock(void);
136 extern void setup_secondary_APIC_clock(void);
137 extern void lapic_update_tsc_freq(void);
138 extern int APIC_init_uniprocessor(void);
141 static inline int apic_force_enable(unsigned long addr)
146 extern int apic_force_enable(unsigned long addr);
149 extern int apic_bsp_setup(bool upmode);
150 extern void apic_ap_setup(void);
153 * On 32bit this is mach-xxx local
156 extern int apic_is_clustered_box(void);
158 static inline int apic_is_clustered_box(void)
164 extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
166 #else /* !CONFIG_X86_LOCAL_APIC */
167 static inline void lapic_shutdown(void) { }
168 #define local_apic_timer_c2_ok 1
169 static inline void init_apic_mappings(void) { }
170 static inline void disable_local_APIC(void) { }
171 # define setup_boot_APIC_clock x86_init_noop
172 # define setup_secondary_APIC_clock x86_init_noop
173 static inline void lapic_update_tsc_freq(void) { }
174 #endif /* !CONFIG_X86_LOCAL_APIC */
176 #ifdef CONFIG_X86_X2APIC
178 * Make previous memory operations globally visible before
179 * sending the IPI through x2apic wrmsr. We need a serializing instruction or
182 static inline void x2apic_wrmsr_fence(void)
184 asm volatile("mfence" : : : "memory");
187 static inline void native_apic_msr_write(u32 reg, u32 v)
189 if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
193 wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
196 static inline void native_apic_msr_eoi_write(u32 reg, u32 v)
198 __wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0);
201 static inline u32 native_apic_msr_read(u32 reg)
208 rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
212 static inline void native_x2apic_wait_icr_idle(void)
214 /* no need to wait for icr idle in x2apic */
218 static inline u32 native_safe_x2apic_wait_icr_idle(void)
220 /* no need to wait for icr idle in x2apic */
224 static inline void native_x2apic_icr_write(u32 low, u32 id)
226 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
229 static inline u64 native_x2apic_icr_read(void)
233 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
237 extern int x2apic_mode;
238 extern int x2apic_phys;
239 extern void __init check_x2apic(void);
240 extern void x2apic_setup(void);
241 static inline int x2apic_enabled(void)
243 return boot_cpu_has(X86_FEATURE_X2APIC) && apic_is_x2apic_enabled();
246 #define x2apic_supported() (boot_cpu_has(X86_FEATURE_X2APIC))
247 #else /* !CONFIG_X86_X2APIC */
248 static inline void check_x2apic(void) { }
249 static inline void x2apic_setup(void) { }
250 static inline int x2apic_enabled(void) { return 0; }
252 #define x2apic_mode (0)
253 #define x2apic_supported() (0)
254 #endif /* !CONFIG_X86_X2APIC */
259 * Copyright 2004 James Cleverdon, IBM.
260 * Subject to the GNU Public License, v.2
262 * Generic APIC sub-arch data struct.
264 * Hacked for x86-64 by James Cleverdon from i386 architecture code by
265 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
272 int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
273 int (*apic_id_valid)(int apicid);
274 int (*apic_id_registered)(void);
276 u32 irq_delivery_mode;
279 const struct cpumask *(*target_cpus)(void);
284 unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid);
286 void (*vector_allocation_domain)(int cpu, struct cpumask *retmask,
287 const struct cpumask *mask);
288 void (*init_apic_ldr)(void);
290 void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
292 void (*setup_apic_routing)(void);
293 int (*cpu_present_to_apicid)(int mps_cpu);
294 void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
295 int (*check_phys_apicid_present)(int phys_apicid);
296 int (*phys_pkg_id)(int cpuid_apic, int index_msb);
298 unsigned int (*get_apic_id)(unsigned long x);
299 /* Can't be NULL on 64-bit */
300 unsigned long (*set_apic_id)(unsigned int id);
302 int (*cpu_mask_to_apicid)(const struct cpumask *cpumask,
303 struct irq_data *irqdata,
304 unsigned int *apicid);
307 void (*send_IPI)(int cpu, int vector);
308 void (*send_IPI_mask)(const struct cpumask *mask, int vector);
309 void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
311 void (*send_IPI_allbutself)(int vector);
312 void (*send_IPI_all)(int vector);
313 void (*send_IPI_self)(int vector);
315 /* wakeup_secondary_cpu */
316 int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
318 void (*inquire_remote_apic)(int apicid);
321 u32 (*read)(u32 reg);
322 void (*write)(u32 reg, u32 v);
324 * ->eoi_write() has the same signature as ->write().
326 * Drivers can support both ->eoi_write() and ->write() by passing the same
327 * callback value. Kernel can override ->eoi_write() and fall back
330 void (*eoi_write)(u32 reg, u32 v);
331 void (*native_eoi_write)(u32 reg, u32 v);
332 u64 (*icr_read)(void);
333 void (*icr_write)(u32 low, u32 high);
334 void (*wait_icr_idle)(void);
335 u32 (*safe_wait_icr_idle)(void);
339 * Called very early during boot from get_smp_config(). It should
340 * return the logical apicid. x86_[bios]_cpu_to_apicid is
341 * initialized before this function is called.
343 * If logical apicid can't be determined that early, the function
344 * may return BAD_APICID. Logical apicid will be configured after
345 * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity
346 * won't be applied properly during early boot in this case.
348 int (*x86_32_early_logical_apicid)(int cpu);
353 * Pointer to the local APIC driver in use on this system (there's
354 * always just one such driver in use - the kernel decides via an
355 * early probing process which one it picks - and then sticks to it):
357 extern struct apic *apic;
360 * APIC drivers are probed based on how they are listed in the .apicdrivers
361 * section. So the order is important and enforced by the ordering
362 * of different apic driver files in the Makefile.
364 * For the files having two apic drivers, we use apic_drivers()
365 * to enforce the order with in them.
367 #define apic_driver(sym) \
368 static const struct apic *__apicdrivers_##sym __used \
369 __aligned(sizeof(struct apic *)) \
370 __section(.apicdrivers) = { &sym }
372 #define apic_drivers(sym1, sym2) \
373 static struct apic *__apicdrivers_##sym1##sym2[2] __used \
374 __aligned(sizeof(struct apic *)) \
375 __section(.apicdrivers) = { &sym1, &sym2 }
377 extern struct apic *__apicdrivers[], *__apicdrivers_end[];
380 * APIC functionality to boot other CPUs - only used on SMP:
383 extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
386 #ifdef CONFIG_X86_LOCAL_APIC
388 static inline u32 apic_read(u32 reg)
390 return apic->read(reg);
393 static inline void apic_write(u32 reg, u32 val)
395 apic->write(reg, val);
398 static inline void apic_eoi(void)
400 apic->eoi_write(APIC_EOI, APIC_EOI_ACK);
403 static inline u64 apic_icr_read(void)
405 return apic->icr_read();
408 static inline void apic_icr_write(u32 low, u32 high)
410 apic->icr_write(low, high);
413 static inline void apic_wait_icr_idle(void)
415 apic->wait_icr_idle();
418 static inline u32 safe_apic_wait_icr_idle(void)
420 return apic->safe_wait_icr_idle();
423 extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v));
425 #else /* CONFIG_X86_LOCAL_APIC */
427 static inline u32 apic_read(u32 reg) { return 0; }
428 static inline void apic_write(u32 reg, u32 val) { }
429 static inline void apic_eoi(void) { }
430 static inline u64 apic_icr_read(void) { return 0; }
431 static inline void apic_icr_write(u32 low, u32 high) { }
432 static inline void apic_wait_icr_idle(void) { }
433 static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
434 static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {}
436 #endif /* CONFIG_X86_LOCAL_APIC */
438 static inline void ack_APIC_irq(void)
441 * ack_APIC_irq() actually gets compiled as a single instruction
447 static inline unsigned default_get_apic_id(unsigned long x)
449 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
451 if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
452 return (x >> 24) & 0xFF;
454 return (x >> 24) & 0x0F;
458 * Warm reset vector position:
460 #define TRAMPOLINE_PHYS_LOW 0x467
461 #define TRAMPOLINE_PHYS_HIGH 0x469
464 extern void apic_send_IPI_self(int vector);
466 DECLARE_PER_CPU(int, x2apic_extra_bits);
468 extern int default_cpu_present_to_apicid(int mps_cpu);
469 extern int default_check_phys_apicid_present(int phys_apicid);
472 extern void generic_bigsmp_probe(void);
475 #ifdef CONFIG_X86_LOCAL_APIC
479 #define APIC_DFR_VALUE (APIC_DFR_FLAT)
481 static inline const struct cpumask *default_target_cpus(void)
484 return cpu_online_mask;
486 return cpumask_of(0);
490 static inline const struct cpumask *online_target_cpus(void)
492 return cpu_online_mask;
495 DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid);
498 static inline unsigned int read_apic_id(void)
502 reg = apic_read(APIC_ID);
504 return apic->get_apic_id(reg);
507 static inline int default_apic_id_valid(int apicid)
509 return (apicid < 255);
512 extern int default_acpi_madt_oem_check(char *, char *);
514 extern void default_setup_apic_routing(void);
516 extern struct apic apic_noop;
520 static inline int noop_x86_32_early_logical_apicid(int cpu)
526 * Set up the logical destination ID.
528 * Intel recommends to set DFR, LDR and TPR before enabling
529 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
530 * document number 292116). So here it goes...
532 extern void default_init_apic_ldr(void);
534 static inline int default_apic_id_registered(void)
536 return physid_isset(read_apic_id(), phys_cpu_present_map);
539 static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
541 return cpuid_apic >> index_msb;
546 extern int flat_cpu_mask_to_apicid(const struct cpumask *cpumask,
547 struct irq_data *irqdata,
548 unsigned int *apicid);
549 extern int default_cpu_mask_to_apicid(const struct cpumask *cpumask,
550 struct irq_data *irqdata,
551 unsigned int *apicid);
554 flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
555 const struct cpumask *mask)
557 /* Careful. Some cpus do not strictly honor the set of cpus
558 * specified in the interrupt destination when using lowest
559 * priority interrupt delivery mode.
561 * In particular there was a hyperthreading cpu observed to
562 * deliver interrupts to the wrong hyperthread when only one
563 * hyperthread was specified in the interrupt desitination.
565 cpumask_clear(retmask);
566 cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
570 default_vector_allocation_domain(int cpu, struct cpumask *retmask,
571 const struct cpumask *mask)
573 cpumask_copy(retmask, cpumask_of(cpu));
576 static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid)
578 return physid_isset(apicid, *map);
581 static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
586 static inline int __default_cpu_present_to_apicid(int mps_cpu)
588 if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
589 return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
595 __default_check_phys_apicid_present(int phys_apicid)
597 return physid_isset(phys_apicid, phys_cpu_present_map);
601 static inline int default_cpu_present_to_apicid(int mps_cpu)
603 return __default_cpu_present_to_apicid(mps_cpu);
607 default_check_phys_apicid_present(int phys_apicid)
609 return __default_check_phys_apicid_present(phys_apicid);
612 extern int default_cpu_present_to_apicid(int mps_cpu);
613 extern int default_check_phys_apicid_present(int phys_apicid);
616 #endif /* CONFIG_X86_LOCAL_APIC */
619 bool apic_id_is_primary_thread(unsigned int id);
621 static inline bool apic_id_is_primary_thread(unsigned int id) { return false; }
624 extern void irq_enter(void);
625 extern void irq_exit(void);
627 static inline void entering_irq(void)
630 kvm_set_cpu_l1tf_flush_l1d();
633 static inline void entering_ack_irq(void)
639 static inline void ipi_entering_ack_irq(void)
643 kvm_set_cpu_l1tf_flush_l1d();
646 static inline void exiting_irq(void)
651 static inline void exiting_ack_irq(void)
657 extern void ioapic_zap_locks(void);
659 #endif /* _ASM_X86_APIC_H */