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