xref: /openbmc/linux/arch/x86/include/asm/apic.h (revision 4f45ed9f)
1 #ifndef _ASM_X86_APIC_H
2 #define _ASM_X86_APIC_H
3 
4 #include <linux/cpumask.h>
5 
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>
12 #include <asm/msr.h>
13 
14 #define ARCH_APICTIMER_STOPS_ON_C3	1
15 
16 /*
17  * Debugging macros
18  */
19 #define APIC_QUIET   0
20 #define APIC_VERBOSE 1
21 #define APIC_DEBUG   2
22 
23 /* Macros for apic_extnmi which controls external NMI masking */
24 #define APIC_EXTNMI_BSP		0 /* Default */
25 #define APIC_EXTNMI_ALL		1
26 #define APIC_EXTNMI_NONE	2
27 
28 /*
29  * Define the default level of output to be very little
30  * This can be turned up by using apic=verbose for more
31  * information and apic=debug for _lots_ of information.
32  * apic_verbosity is defined in apic.c
33  */
34 #define apic_printk(v, s, a...) do {       \
35 		if ((v) <= apic_verbosity) \
36 			printk(s, ##a);    \
37 	} while (0)
38 
39 
40 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32)
41 extern void generic_apic_probe(void);
42 #else
43 static inline void generic_apic_probe(void)
44 {
45 }
46 #endif
47 
48 #ifdef CONFIG_X86_LOCAL_APIC
49 
50 extern unsigned int apic_verbosity;
51 extern int local_apic_timer_c2_ok;
52 
53 extern int disable_apic;
54 extern unsigned int lapic_timer_frequency;
55 
56 extern enum apic_intr_mode_id apic_intr_mode;
57 enum apic_intr_mode_id {
58 	APIC_PIC,
59 	APIC_VIRTUAL_WIRE,
60 	APIC_VIRTUAL_WIRE_NO_CONFIG,
61 	APIC_SYMMETRIC_IO,
62 	APIC_SYMMETRIC_IO_NO_ROUTING
63 };
64 
65 #ifdef CONFIG_SMP
66 extern void __inquire_remote_apic(int apicid);
67 #else /* CONFIG_SMP */
68 static inline void __inquire_remote_apic(int apicid)
69 {
70 }
71 #endif /* CONFIG_SMP */
72 
73 static inline void default_inquire_remote_apic(int apicid)
74 {
75 	if (apic_verbosity >= APIC_DEBUG)
76 		__inquire_remote_apic(apicid);
77 }
78 
79 /*
80  * With 82489DX we can't rely on apic feature bit
81  * retrieved via cpuid but still have to deal with
82  * such an apic chip so we assume that SMP configuration
83  * is found from MP table (64bit case uses ACPI mostly
84  * which set smp presence flag as well so we are safe
85  * to use this helper too).
86  */
87 static inline bool apic_from_smp_config(void)
88 {
89 	return smp_found_config && !disable_apic;
90 }
91 
92 /*
93  * Basic functions accessing APICs.
94  */
95 #ifdef CONFIG_PARAVIRT
96 #include <asm/paravirt.h>
97 #endif
98 
99 extern int setup_profiling_timer(unsigned int);
100 
101 static inline void native_apic_mem_write(u32 reg, u32 v)
102 {
103 	volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg);
104 
105 	alternative_io("movl %0, %P1", "xchgl %0, %P1", X86_BUG_11AP,
106 		       ASM_OUTPUT2("=r" (v), "=m" (*addr)),
107 		       ASM_OUTPUT2("0" (v), "m" (*addr)));
108 }
109 
110 static inline u32 native_apic_mem_read(u32 reg)
111 {
112 	return *((volatile u32 *)(APIC_BASE + reg));
113 }
114 
115 extern void native_apic_wait_icr_idle(void);
116 extern u32 native_safe_apic_wait_icr_idle(void);
117 extern void native_apic_icr_write(u32 low, u32 id);
118 extern u64 native_apic_icr_read(void);
119 
120 static inline bool apic_is_x2apic_enabled(void)
121 {
122 	u64 msr;
123 
124 	if (rdmsrl_safe(MSR_IA32_APICBASE, &msr))
125 		return false;
126 	return msr & X2APIC_ENABLE;
127 }
128 
129 extern void enable_IR_x2apic(void);
130 
131 extern int get_physical_broadcast(void);
132 
133 extern int lapic_get_maxlvt(void);
134 extern void clear_local_APIC(void);
135 extern void disconnect_bsp_APIC(int virt_wire_setup);
136 extern void disable_local_APIC(void);
137 extern void lapic_shutdown(void);
138 extern void sync_Arb_IDs(void);
139 extern void init_bsp_APIC(void);
140 extern void apic_intr_mode_init(void);
141 extern void setup_local_APIC(void);
142 extern void init_apic_mappings(void);
143 void register_lapic_address(unsigned long address);
144 extern void setup_boot_APIC_clock(void);
145 extern void setup_secondary_APIC_clock(void);
146 extern void lapic_update_tsc_freq(void);
147 extern int APIC_init_uniprocessor(void);
148 
149 #ifdef CONFIG_X86_64
150 static inline int apic_force_enable(unsigned long addr)
151 {
152 	return -1;
153 }
154 #else
155 extern int apic_force_enable(unsigned long addr);
156 #endif
157 
158 extern void apic_bsp_setup(bool upmode);
159 extern void apic_ap_setup(void);
160 
161 /*
162  * On 32bit this is mach-xxx local
163  */
164 #ifdef CONFIG_X86_64
165 extern int apic_is_clustered_box(void);
166 #else
167 static inline int apic_is_clustered_box(void)
168 {
169 	return 0;
170 }
171 #endif
172 
173 extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
174 
175 #else /* !CONFIG_X86_LOCAL_APIC */
176 static inline void lapic_shutdown(void) { }
177 #define local_apic_timer_c2_ok		1
178 static inline void init_apic_mappings(void) { }
179 static inline void disable_local_APIC(void) { }
180 # define setup_boot_APIC_clock x86_init_noop
181 # define setup_secondary_APIC_clock x86_init_noop
182 static inline void lapic_update_tsc_freq(void) { }
183 static inline void apic_intr_mode_init(void) { }
184 #endif /* !CONFIG_X86_LOCAL_APIC */
185 
186 #ifdef CONFIG_X86_X2APIC
187 /*
188  * Make previous memory operations globally visible before
189  * sending the IPI through x2apic wrmsr. We need a serializing instruction or
190  * mfence for this.
191  */
192 static inline void x2apic_wrmsr_fence(void)
193 {
194 	asm volatile("mfence" : : : "memory");
195 }
196 
197 static inline void native_apic_msr_write(u32 reg, u32 v)
198 {
199 	if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
200 	    reg == APIC_LVR)
201 		return;
202 
203 	wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
204 }
205 
206 static inline void native_apic_msr_eoi_write(u32 reg, u32 v)
207 {
208 	__wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0);
209 }
210 
211 static inline u32 native_apic_msr_read(u32 reg)
212 {
213 	u64 msr;
214 
215 	if (reg == APIC_DFR)
216 		return -1;
217 
218 	rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
219 	return (u32)msr;
220 }
221 
222 static inline void native_x2apic_wait_icr_idle(void)
223 {
224 	/* no need to wait for icr idle in x2apic */
225 	return;
226 }
227 
228 static inline u32 native_safe_x2apic_wait_icr_idle(void)
229 {
230 	/* no need to wait for icr idle in x2apic */
231 	return 0;
232 }
233 
234 static inline void native_x2apic_icr_write(u32 low, u32 id)
235 {
236 	wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
237 }
238 
239 static inline u64 native_x2apic_icr_read(void)
240 {
241 	unsigned long val;
242 
243 	rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
244 	return val;
245 }
246 
247 extern int x2apic_mode;
248 extern int x2apic_phys;
249 extern void __init check_x2apic(void);
250 extern void x2apic_setup(void);
251 static inline int x2apic_enabled(void)
252 {
253 	return boot_cpu_has(X86_FEATURE_X2APIC) && apic_is_x2apic_enabled();
254 }
255 
256 #define x2apic_supported()	(boot_cpu_has(X86_FEATURE_X2APIC))
257 #else /* !CONFIG_X86_X2APIC */
258 static inline void check_x2apic(void) { }
259 static inline void x2apic_setup(void) { }
260 static inline int x2apic_enabled(void) { return 0; }
261 
262 #define x2apic_mode		(0)
263 #define	x2apic_supported()	(0)
264 #endif /* !CONFIG_X86_X2APIC */
265 
266 struct irq_data;
267 
268 /*
269  * Copyright 2004 James Cleverdon, IBM.
270  * Subject to the GNU Public License, v.2
271  *
272  * Generic APIC sub-arch data struct.
273  *
274  * Hacked for x86-64 by James Cleverdon from i386 architecture code by
275  * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
276  * James Cleverdon.
277  */
278 struct apic {
279 	char *name;
280 
281 	int (*probe)(void);
282 	int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
283 	int (*apic_id_valid)(int apicid);
284 	int (*apic_id_registered)(void);
285 
286 	u32 irq_delivery_mode;
287 	u32 irq_dest_mode;
288 
289 	const struct cpumask *(*target_cpus)(void);
290 
291 	int disable_esr;
292 
293 	int dest_logical;
294 	unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid);
295 
296 	void (*vector_allocation_domain)(int cpu, struct cpumask *retmask,
297 					 const struct cpumask *mask);
298 	void (*init_apic_ldr)(void);
299 
300 	void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
301 
302 	void (*setup_apic_routing)(void);
303 	int (*cpu_present_to_apicid)(int mps_cpu);
304 	void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
305 	int (*check_phys_apicid_present)(int phys_apicid);
306 	int (*phys_pkg_id)(int cpuid_apic, int index_msb);
307 
308 	unsigned int (*get_apic_id)(unsigned long x);
309 	/* Can't be NULL on 64-bit */
310 	unsigned long (*set_apic_id)(unsigned int id);
311 
312 	int (*cpu_mask_to_apicid)(const struct cpumask *cpumask,
313 				  struct irq_data *irqdata,
314 				  unsigned int *apicid);
315 
316 	/* ipi */
317 	void (*send_IPI)(int cpu, int vector);
318 	void (*send_IPI_mask)(const struct cpumask *mask, int vector);
319 	void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
320 					 int vector);
321 	void (*send_IPI_allbutself)(int vector);
322 	void (*send_IPI_all)(int vector);
323 	void (*send_IPI_self)(int vector);
324 
325 	/* wakeup_secondary_cpu */
326 	int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
327 
328 	void (*inquire_remote_apic)(int apicid);
329 
330 	/* apic ops */
331 	u32 (*read)(u32 reg);
332 	void (*write)(u32 reg, u32 v);
333 	/*
334 	 * ->eoi_write() has the same signature as ->write().
335 	 *
336 	 * Drivers can support both ->eoi_write() and ->write() by passing the same
337 	 * callback value. Kernel can override ->eoi_write() and fall back
338 	 * on write for EOI.
339 	 */
340 	void (*eoi_write)(u32 reg, u32 v);
341 	void (*native_eoi_write)(u32 reg, u32 v);
342 	u64 (*icr_read)(void);
343 	void (*icr_write)(u32 low, u32 high);
344 	void (*wait_icr_idle)(void);
345 	u32 (*safe_wait_icr_idle)(void);
346 
347 #ifdef CONFIG_X86_32
348 	/*
349 	 * Called very early during boot from get_smp_config().  It should
350 	 * return the logical apicid.  x86_[bios]_cpu_to_apicid is
351 	 * initialized before this function is called.
352 	 *
353 	 * If logical apicid can't be determined that early, the function
354 	 * may return BAD_APICID.  Logical apicid will be configured after
355 	 * init_apic_ldr() while bringing up CPUs.  Note that NUMA affinity
356 	 * won't be applied properly during early boot in this case.
357 	 */
358 	int (*x86_32_early_logical_apicid)(int cpu);
359 #endif
360 };
361 
362 /*
363  * Pointer to the local APIC driver in use on this system (there's
364  * always just one such driver in use - the kernel decides via an
365  * early probing process which one it picks - and then sticks to it):
366  */
367 extern struct apic *apic;
368 
369 /*
370  * APIC drivers are probed based on how they are listed in the .apicdrivers
371  * section. So the order is important and enforced by the ordering
372  * of different apic driver files in the Makefile.
373  *
374  * For the files having two apic drivers, we use apic_drivers()
375  * to enforce the order with in them.
376  */
377 #define apic_driver(sym)					\
378 	static const struct apic *__apicdrivers_##sym __used		\
379 	__aligned(sizeof(struct apic *))			\
380 	__section(.apicdrivers) = { &sym }
381 
382 #define apic_drivers(sym1, sym2)					\
383 	static struct apic *__apicdrivers_##sym1##sym2[2] __used	\
384 	__aligned(sizeof(struct apic *))				\
385 	__section(.apicdrivers) = { &sym1, &sym2 }
386 
387 extern struct apic *__apicdrivers[], *__apicdrivers_end[];
388 
389 /*
390  * APIC functionality to boot other CPUs - only used on SMP:
391  */
392 #ifdef CONFIG_SMP
393 extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
394 #endif
395 
396 #ifdef CONFIG_X86_LOCAL_APIC
397 
398 static inline u32 apic_read(u32 reg)
399 {
400 	return apic->read(reg);
401 }
402 
403 static inline void apic_write(u32 reg, u32 val)
404 {
405 	apic->write(reg, val);
406 }
407 
408 static inline void apic_eoi(void)
409 {
410 	apic->eoi_write(APIC_EOI, APIC_EOI_ACK);
411 }
412 
413 static inline u64 apic_icr_read(void)
414 {
415 	return apic->icr_read();
416 }
417 
418 static inline void apic_icr_write(u32 low, u32 high)
419 {
420 	apic->icr_write(low, high);
421 }
422 
423 static inline void apic_wait_icr_idle(void)
424 {
425 	apic->wait_icr_idle();
426 }
427 
428 static inline u32 safe_apic_wait_icr_idle(void)
429 {
430 	return apic->safe_wait_icr_idle();
431 }
432 
433 extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v));
434 
435 #else /* CONFIG_X86_LOCAL_APIC */
436 
437 static inline u32 apic_read(u32 reg) { return 0; }
438 static inline void apic_write(u32 reg, u32 val) { }
439 static inline void apic_eoi(void) { }
440 static inline u64 apic_icr_read(void) { return 0; }
441 static inline void apic_icr_write(u32 low, u32 high) { }
442 static inline void apic_wait_icr_idle(void) { }
443 static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
444 static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {}
445 
446 #endif /* CONFIG_X86_LOCAL_APIC */
447 
448 static inline void ack_APIC_irq(void)
449 {
450 	/*
451 	 * ack_APIC_irq() actually gets compiled as a single instruction
452 	 * ... yummie.
453 	 */
454 	apic_eoi();
455 }
456 
457 static inline unsigned default_get_apic_id(unsigned long x)
458 {
459 	unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
460 
461 	if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
462 		return (x >> 24) & 0xFF;
463 	else
464 		return (x >> 24) & 0x0F;
465 }
466 
467 /*
468  * Warm reset vector position:
469  */
470 #define TRAMPOLINE_PHYS_LOW		0x467
471 #define TRAMPOLINE_PHYS_HIGH		0x469
472 
473 #ifdef CONFIG_X86_64
474 extern void apic_send_IPI_self(int vector);
475 
476 DECLARE_PER_CPU(int, x2apic_extra_bits);
477 
478 extern int default_cpu_present_to_apicid(int mps_cpu);
479 extern int default_check_phys_apicid_present(int phys_apicid);
480 #endif
481 
482 extern void generic_bigsmp_probe(void);
483 
484 
485 #ifdef CONFIG_X86_LOCAL_APIC
486 
487 #include <asm/smp.h>
488 
489 #define APIC_DFR_VALUE	(APIC_DFR_FLAT)
490 
491 static inline const struct cpumask *default_target_cpus(void)
492 {
493 #ifdef CONFIG_SMP
494 	return cpu_online_mask;
495 #else
496 	return cpumask_of(0);
497 #endif
498 }
499 
500 static inline const struct cpumask *online_target_cpus(void)
501 {
502 	return cpu_online_mask;
503 }
504 
505 DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid);
506 
507 
508 static inline unsigned int read_apic_id(void)
509 {
510 	unsigned int reg;
511 
512 	reg = apic_read(APIC_ID);
513 
514 	return apic->get_apic_id(reg);
515 }
516 
517 static inline int default_apic_id_valid(int apicid)
518 {
519 	return (apicid < 255);
520 }
521 
522 extern int default_acpi_madt_oem_check(char *, char *);
523 
524 extern void default_setup_apic_routing(void);
525 
526 extern struct apic apic_noop;
527 
528 #ifdef CONFIG_X86_32
529 
530 static inline int noop_x86_32_early_logical_apicid(int cpu)
531 {
532 	return BAD_APICID;
533 }
534 
535 /*
536  * Set up the logical destination ID.
537  *
538  * Intel recommends to set DFR, LDR and TPR before enabling
539  * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
540  * document number 292116).  So here it goes...
541  */
542 extern void default_init_apic_ldr(void);
543 
544 static inline int default_apic_id_registered(void)
545 {
546 	return physid_isset(read_apic_id(), phys_cpu_present_map);
547 }
548 
549 static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
550 {
551 	return cpuid_apic >> index_msb;
552 }
553 
554 #endif
555 
556 extern int flat_cpu_mask_to_apicid(const struct cpumask *cpumask,
557 				   struct irq_data *irqdata,
558 				   unsigned int *apicid);
559 extern int default_cpu_mask_to_apicid(const struct cpumask *cpumask,
560 				      struct irq_data *irqdata,
561 				      unsigned int *apicid);
562 
563 static inline void
564 flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
565 			      const struct cpumask *mask)
566 {
567 	/* Careful. Some cpus do not strictly honor the set of cpus
568 	 * specified in the interrupt destination when using lowest
569 	 * priority interrupt delivery mode.
570 	 *
571 	 * In particular there was a hyperthreading cpu observed to
572 	 * deliver interrupts to the wrong hyperthread when only one
573 	 * hyperthread was specified in the interrupt desitination.
574 	 */
575 	cpumask_clear(retmask);
576 	cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
577 }
578 
579 static inline void
580 default_vector_allocation_domain(int cpu, struct cpumask *retmask,
581 				 const struct cpumask *mask)
582 {
583 	cpumask_copy(retmask, cpumask_of(cpu));
584 }
585 
586 static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid)
587 {
588 	return physid_isset(apicid, *map);
589 }
590 
591 static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
592 {
593 	*retmap = *phys_map;
594 }
595 
596 static inline int __default_cpu_present_to_apicid(int mps_cpu)
597 {
598 	if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
599 		return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
600 	else
601 		return BAD_APICID;
602 }
603 
604 static inline int
605 __default_check_phys_apicid_present(int phys_apicid)
606 {
607 	return physid_isset(phys_apicid, phys_cpu_present_map);
608 }
609 
610 #ifdef CONFIG_X86_32
611 static inline int default_cpu_present_to_apicid(int mps_cpu)
612 {
613 	return __default_cpu_present_to_apicid(mps_cpu);
614 }
615 
616 static inline int
617 default_check_phys_apicid_present(int phys_apicid)
618 {
619 	return __default_check_phys_apicid_present(phys_apicid);
620 }
621 #else
622 extern int default_cpu_present_to_apicid(int mps_cpu);
623 extern int default_check_phys_apicid_present(int phys_apicid);
624 #endif
625 
626 #endif /* CONFIG_X86_LOCAL_APIC */
627 extern void irq_enter(void);
628 extern void irq_exit(void);
629 
630 static inline void entering_irq(void)
631 {
632 	irq_enter();
633 }
634 
635 static inline void entering_ack_irq(void)
636 {
637 	entering_irq();
638 	ack_APIC_irq();
639 }
640 
641 static inline void ipi_entering_ack_irq(void)
642 {
643 	irq_enter();
644 	ack_APIC_irq();
645 }
646 
647 static inline void exiting_irq(void)
648 {
649 	irq_exit();
650 }
651 
652 static inline void exiting_ack_irq(void)
653 {
654 	ack_APIC_irq();
655 	irq_exit();
656 }
657 
658 extern void ioapic_zap_locks(void);
659 
660 #endif /* _ASM_X86_APIC_H */
661