1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/perf_event.h>
19#include <linux/kernel_stat.h>
20#include <linux/mc146818rtc.h>
21#include <linux/acpi_pmtmr.h>
22#include <linux/clockchips.h>
23#include <linux/interrupt.h>
24#include <linux/memblock.h>
25#include <linux/ftrace.h>
26#include <linux/ioport.h>
27#include <linux/export.h>
28#include <linux/syscore_ops.h>
29#include <linux/delay.h>
30#include <linux/timex.h>
31#include <linux/i8253.h>
32#include <linux/dmar.h>
33#include <linux/init.h>
34#include <linux/cpu.h>
35#include <linux/dmi.h>
36#include <linux/smp.h>
37#include <linux/mm.h>
38
39#include <asm/trace/irq_vectors.h>
40#include <asm/irq_remapping.h>
41#include <asm/perf_event.h>
42#include <asm/x86_init.h>
43#include <linux/atomic.h>
44#include <asm/barrier.h>
45#include <asm/mpspec.h>
46#include <asm/i8259.h>
47#include <asm/proto.h>
48#include <asm/traps.h>
49#include <asm/apic.h>
50#include <asm/acpi.h>
51#include <asm/io_apic.h>
52#include <asm/desc.h>
53#include <asm/hpet.h>
54#include <asm/mtrr.h>
55#include <asm/time.h>
56#include <asm/smp.h>
57#include <asm/mce.h>
58#include <asm/tsc.h>
59#include <asm/hypervisor.h>
60#include <asm/cpu_device_id.h>
61#include <asm/intel-family.h>
62#include <asm/irq_regs.h>
63
64unsigned int num_processors;
65
66unsigned disabled_cpus;
67
68
69unsigned int boot_cpu_physical_apicid __ro_after_init = -1U;
70EXPORT_SYMBOL_GPL(boot_cpu_physical_apicid);
71
72u8 boot_cpu_apic_version __ro_after_init;
73
74
75
76
77static unsigned int max_physical_apicid;
78
79
80
81
82physid_mask_t phys_cpu_present_map;
83
84
85
86
87
88
89static unsigned int disabled_cpu_apicid __ro_after_init = BAD_APICID;
90
91
92
93
94
95static int apic_extnmi __ro_after_init = APIC_EXTNMI_BSP;
96
97
98
99
100static bool virt_ext_dest_id __ro_after_init;
101
102
103
104
105DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_cpu_to_apicid, BAD_APICID);
106DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid, BAD_APICID);
107DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_acpiid, U32_MAX);
108EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
109EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
110EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_acpiid);
111
112#ifdef CONFIG_X86_32
113
114
115
116
117
118
119
120DEFINE_EARLY_PER_CPU_READ_MOSTLY(int, x86_cpu_to_logical_apicid, BAD_APICID);
121
122
123static int enabled_via_apicbase __ro_after_init;
124
125
126
127
128
129
130
131
132
133static inline void imcr_pic_to_apic(void)
134{
135
136 outb(0x70, 0x22);
137
138 outb(0x01, 0x23);
139}
140
141static inline void imcr_apic_to_pic(void)
142{
143
144 outb(0x70, 0x22);
145
146 outb(0x00, 0x23);
147}
148#endif
149
150
151
152
153
154
155static int force_enable_local_apic __initdata;
156
157
158
159
160static int __init parse_lapic(char *arg)
161{
162 if (IS_ENABLED(CONFIG_X86_32) && !arg)
163 force_enable_local_apic = 1;
164 else if (arg && !strncmp(arg, "notscdeadline", 13))
165 setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
166 return 0;
167}
168early_param("lapic", parse_lapic);
169
170#ifdef CONFIG_X86_64
171static int apic_calibrate_pmtmr __initdata;
172static __init int setup_apicpmtimer(char *s)
173{
174 apic_calibrate_pmtmr = 1;
175 notsc_setup(NULL);
176 return 0;
177}
178__setup("apicpmtimer", setup_apicpmtimer);
179#endif
180
181unsigned long mp_lapic_addr __ro_after_init;
182int disable_apic __ro_after_init;
183
184static int disable_apic_timer __initdata;
185
186int local_apic_timer_c2_ok __ro_after_init;
187EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
188
189
190
191
192int apic_verbosity __ro_after_init;
193
194int pic_mode __ro_after_init;
195
196
197int smp_found_config __ro_after_init;
198
199static struct resource lapic_resource = {
200 .name = "Local APIC",
201 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
202};
203
204unsigned int lapic_timer_period = 0;
205
206static void apic_pm_activate(void);
207
208static unsigned long apic_phys __ro_after_init;
209
210
211
212
213static inline int lapic_get_version(void)
214{
215 return GET_APIC_VERSION(apic_read(APIC_LVR));
216}
217
218
219
220
221static inline int lapic_is_integrated(void)
222{
223 return APIC_INTEGRATED(lapic_get_version());
224}
225
226
227
228
229static int modern_apic(void)
230{
231
232 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
233 boot_cpu_data.x86 >= 0xf)
234 return 1;
235
236
237 if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
238 return 1;
239
240 return lapic_get_version() >= 0x14;
241}
242
243
244
245
246
247static void __init apic_disable(void)
248{
249 pr_info("APIC: switched to apic NOOP\n");
250 apic = &apic_noop;
251}
252
253void native_apic_wait_icr_idle(void)
254{
255 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
256 cpu_relax();
257}
258
259u32 native_safe_apic_wait_icr_idle(void)
260{
261 u32 send_status;
262 int timeout;
263
264 timeout = 0;
265 do {
266 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
267 if (!send_status)
268 break;
269 inc_irq_stat(icr_read_retry_count);
270 udelay(100);
271 } while (timeout++ < 1000);
272
273 return send_status;
274}
275
276void native_apic_icr_write(u32 low, u32 id)
277{
278 unsigned long flags;
279
280 local_irq_save(flags);
281 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
282 apic_write(APIC_ICR, low);
283 local_irq_restore(flags);
284}
285
286u64 native_apic_icr_read(void)
287{
288 u32 icr1, icr2;
289
290 icr2 = apic_read(APIC_ICR2);
291 icr1 = apic_read(APIC_ICR);
292
293 return icr1 | ((u64)icr2 << 32);
294}
295
296#ifdef CONFIG_X86_32
297
298
299
300int get_physical_broadcast(void)
301{
302 return modern_apic() ? 0xff : 0xf;
303}
304#endif
305
306
307
308
309int lapic_get_maxlvt(void)
310{
311
312
313
314
315 return lapic_is_integrated() ? GET_APIC_MAXLVT(apic_read(APIC_LVR)) : 2;
316}
317
318
319
320
321
322
323#define APIC_DIVISOR 16
324#define TSC_DIVISOR 8
325
326
327
328
329
330
331
332
333
334
335
336static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
337{
338 unsigned int lvtt_value, tmp_value;
339
340 lvtt_value = LOCAL_TIMER_VECTOR;
341 if (!oneshot)
342 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
343 else if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
344 lvtt_value |= APIC_LVT_TIMER_TSCDEADLINE;
345
346 if (!lapic_is_integrated())
347 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
348
349 if (!irqen)
350 lvtt_value |= APIC_LVT_MASKED;
351
352 apic_write(APIC_LVTT, lvtt_value);
353
354 if (lvtt_value & APIC_LVT_TIMER_TSCDEADLINE) {
355
356
357
358
359
360 asm volatile("mfence" : : : "memory");
361 return;
362 }
363
364
365
366
367 tmp_value = apic_read(APIC_TDCR);
368 apic_write(APIC_TDCR,
369 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
370 APIC_TDR_DIV_16);
371
372 if (!oneshot)
373 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
374}
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
397
398static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
399{
400 return (old & APIC_EILVT_MASKED)
401 || (new == APIC_EILVT_MASKED)
402 || ((new & ~APIC_EILVT_MASKED) == old);
403}
404
405static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
406{
407 unsigned int rsvd, vector;
408
409 if (offset >= APIC_EILVT_NR_MAX)
410 return ~0;
411
412 rsvd = atomic_read(&eilvt_offsets[offset]);
413 do {
414 vector = rsvd & ~APIC_EILVT_MASKED;
415 if (vector && !eilvt_entry_is_changeable(vector, new))
416
417 return rsvd;
418 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
419 } while (rsvd != new);
420
421 rsvd &= ~APIC_EILVT_MASKED;
422 if (rsvd && rsvd != vector)
423 pr_info("LVT offset %d assigned for vector 0x%02x\n",
424 offset, rsvd);
425
426 return new;
427}
428
429
430
431
432
433
434
435int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
436{
437 unsigned long reg = APIC_EILVTn(offset);
438 unsigned int new, old, reserved;
439
440 new = (mask << 16) | (msg_type << 8) | vector;
441 old = apic_read(reg);
442 reserved = reserve_eilvt_offset(offset, new);
443
444 if (reserved != new) {
445 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
446 "vector 0x%x, but the register is already in use for "
447 "vector 0x%x on another cpu\n",
448 smp_processor_id(), reg, offset, new, reserved);
449 return -EINVAL;
450 }
451
452 if (!eilvt_entry_is_changeable(old, new)) {
453 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
454 "vector 0x%x, but the register is already in use for "
455 "vector 0x%x on this cpu\n",
456 smp_processor_id(), reg, offset, new, old);
457 return -EBUSY;
458 }
459
460 apic_write(reg, new);
461
462 return 0;
463}
464EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
465
466
467
468
469static int lapic_next_event(unsigned long delta,
470 struct clock_event_device *evt)
471{
472 apic_write(APIC_TMICT, delta);
473 return 0;
474}
475
476static int lapic_next_deadline(unsigned long delta,
477 struct clock_event_device *evt)
478{
479 u64 tsc;
480
481
482 weak_wrmsr_fence();
483
484 tsc = rdtsc();
485 wrmsrl(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
486 return 0;
487}
488
489static int lapic_timer_shutdown(struct clock_event_device *evt)
490{
491 unsigned int v;
492
493
494 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
495 return 0;
496
497 v = apic_read(APIC_LVTT);
498 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
499 apic_write(APIC_LVTT, v);
500 apic_write(APIC_TMICT, 0);
501 return 0;
502}
503
504static inline int
505lapic_timer_set_periodic_oneshot(struct clock_event_device *evt, bool oneshot)
506{
507
508 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
509 return 0;
510
511 __setup_APIC_LVTT(lapic_timer_period, oneshot, 1);
512 return 0;
513}
514
515static int lapic_timer_set_periodic(struct clock_event_device *evt)
516{
517 return lapic_timer_set_periodic_oneshot(evt, false);
518}
519
520static int lapic_timer_set_oneshot(struct clock_event_device *evt)
521{
522 return lapic_timer_set_periodic_oneshot(evt, true);
523}
524
525
526
527
528static void lapic_timer_broadcast(const struct cpumask *mask)
529{
530#ifdef CONFIG_SMP
531 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
532#endif
533}
534
535
536
537
538
539static struct clock_event_device lapic_clockevent = {
540 .name = "lapic",
541 .features = CLOCK_EVT_FEAT_PERIODIC |
542 CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP
543 | CLOCK_EVT_FEAT_DUMMY,
544 .shift = 32,
545 .set_state_shutdown = lapic_timer_shutdown,
546 .set_state_periodic = lapic_timer_set_periodic,
547 .set_state_oneshot = lapic_timer_set_oneshot,
548 .set_state_oneshot_stopped = lapic_timer_shutdown,
549 .set_next_event = lapic_next_event,
550 .broadcast = lapic_timer_broadcast,
551 .rating = 100,
552 .irq = -1,
553};
554static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
555
556static const struct x86_cpu_id deadline_match[] __initconst = {
557 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(HASWELL_X, X86_STEPPINGS(0x2, 0x2), 0x3a),
558 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(HASWELL_X, X86_STEPPINGS(0x4, 0x4), 0x0f),
559
560 X86_MATCH_INTEL_FAM6_MODEL( BROADWELL_X, 0x0b000020),
561
562 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(BROADWELL_D, X86_STEPPINGS(0x2, 0x2), 0x00000011),
563 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(BROADWELL_D, X86_STEPPINGS(0x3, 0x3), 0x0700000e),
564 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(BROADWELL_D, X86_STEPPINGS(0x4, 0x4), 0x0f00000c),
565 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(BROADWELL_D, X86_STEPPINGS(0x5, 0x5), 0x0e000003),
566
567 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(SKYLAKE_X, X86_STEPPINGS(0x3, 0x3), 0x01000136),
568 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(SKYLAKE_X, X86_STEPPINGS(0x4, 0x4), 0x02000014),
569 X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS(SKYLAKE_X, X86_STEPPINGS(0x5, 0xf), 0),
570
571 X86_MATCH_INTEL_FAM6_MODEL( HASWELL, 0x22),
572 X86_MATCH_INTEL_FAM6_MODEL( HASWELL_L, 0x20),
573 X86_MATCH_INTEL_FAM6_MODEL( HASWELL_G, 0x17),
574
575 X86_MATCH_INTEL_FAM6_MODEL( BROADWELL, 0x25),
576 X86_MATCH_INTEL_FAM6_MODEL( BROADWELL_G, 0x17),
577
578 X86_MATCH_INTEL_FAM6_MODEL( SKYLAKE_L, 0xb2),
579 X86_MATCH_INTEL_FAM6_MODEL( SKYLAKE, 0xb2),
580
581 X86_MATCH_INTEL_FAM6_MODEL( KABYLAKE_L, 0x52),
582 X86_MATCH_INTEL_FAM6_MODEL( KABYLAKE, 0x52),
583
584 {},
585};
586
587static __init bool apic_validate_deadline_timer(void)
588{
589 const struct x86_cpu_id *m;
590 u32 rev;
591
592 if (!boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
593 return false;
594 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
595 return true;
596
597 m = x86_match_cpu(deadline_match);
598 if (!m)
599 return true;
600
601 rev = (u32)m->driver_data;
602
603 if (boot_cpu_data.microcode >= rev)
604 return true;
605
606 setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
607 pr_err(FW_BUG "TSC_DEADLINE disabled due to Errata; "
608 "please update microcode to version: 0x%x (or later)\n", rev);
609 return false;
610}
611
612
613
614
615
616static void setup_APIC_timer(void)
617{
618 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
619
620 if (this_cpu_has(X86_FEATURE_ARAT)) {
621 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
622
623 lapic_clockevent.rating = 150;
624 }
625
626 memcpy(levt, &lapic_clockevent, sizeof(*levt));
627 levt->cpumask = cpumask_of(smp_processor_id());
628
629 if (this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
630 levt->name = "lapic-deadline";
631 levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC |
632 CLOCK_EVT_FEAT_DUMMY);
633 levt->set_next_event = lapic_next_deadline;
634 clockevents_config_and_register(levt,
635 tsc_khz * (1000 / TSC_DIVISOR),
636 0xF, ~0UL);
637 } else
638 clockevents_register_device(levt);
639}
640
641
642
643
644
645static void __lapic_update_tsc_freq(void *info)
646{
647 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
648
649 if (!this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
650 return;
651
652 clockevents_update_freq(levt, tsc_khz * (1000 / TSC_DIVISOR));
653}
654
655void lapic_update_tsc_freq(void)
656{
657
658
659
660
661
662 on_each_cpu(__lapic_update_tsc_freq, NULL, 0);
663}
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686#define LAPIC_CAL_LOOPS (HZ/10)
687
688static __initdata int lapic_cal_loops = -1;
689static __initdata long lapic_cal_t1, lapic_cal_t2;
690static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
691static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
692static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
693
694
695
696
697static void __init lapic_cal_handler(struct clock_event_device *dev)
698{
699 unsigned long long tsc = 0;
700 long tapic = apic_read(APIC_TMCCT);
701 unsigned long pm = acpi_pm_read_early();
702
703 if (boot_cpu_has(X86_FEATURE_TSC))
704 tsc = rdtsc();
705
706 switch (lapic_cal_loops++) {
707 case 0:
708 lapic_cal_t1 = tapic;
709 lapic_cal_tsc1 = tsc;
710 lapic_cal_pm1 = pm;
711 lapic_cal_j1 = jiffies;
712 break;
713
714 case LAPIC_CAL_LOOPS:
715 lapic_cal_t2 = tapic;
716 lapic_cal_tsc2 = tsc;
717 if (pm < lapic_cal_pm1)
718 pm += ACPI_PM_OVRRUN;
719 lapic_cal_pm2 = pm;
720 lapic_cal_j2 = jiffies;
721 break;
722 }
723}
724
725static int __init
726calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
727{
728 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
729 const long pm_thresh = pm_100ms / 100;
730 unsigned long mult;
731 u64 res;
732
733#ifndef CONFIG_X86_PM_TIMER
734 return -1;
735#endif
736
737 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
738
739
740 if (!deltapm)
741 return -1;
742
743 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
744
745 if (deltapm > (pm_100ms - pm_thresh) &&
746 deltapm < (pm_100ms + pm_thresh)) {
747 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
748 return 0;
749 }
750
751 res = (((u64)deltapm) * mult) >> 22;
752 do_div(res, 1000000);
753 pr_warn("APIC calibration not consistent "
754 "with PM-Timer: %ldms instead of 100ms\n", (long)res);
755
756
757 res = (((u64)(*delta)) * pm_100ms);
758 do_div(res, deltapm);
759 pr_info("APIC delta adjusted to PM-Timer: "
760 "%lu (%ld)\n", (unsigned long)res, *delta);
761 *delta = (long)res;
762
763
764 if (boot_cpu_has(X86_FEATURE_TSC)) {
765 res = (((u64)(*deltatsc)) * pm_100ms);
766 do_div(res, deltapm);
767 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
768 "PM-Timer: %lu (%ld)\n",
769 (unsigned long)res, *deltatsc);
770 *deltatsc = (long)res;
771 }
772
773 return 0;
774}
775
776static int __init lapic_init_clockevent(void)
777{
778 if (!lapic_timer_period)
779 return -1;
780
781
782 lapic_clockevent.mult = div_sc(lapic_timer_period/APIC_DIVISOR,
783 TICK_NSEC, lapic_clockevent.shift);
784 lapic_clockevent.max_delta_ns =
785 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
786 lapic_clockevent.max_delta_ticks = 0x7FFFFFFF;
787 lapic_clockevent.min_delta_ns =
788 clockevent_delta2ns(0xF, &lapic_clockevent);
789 lapic_clockevent.min_delta_ticks = 0xF;
790
791 return 0;
792}
793
794bool __init apic_needs_pit(void)
795{
796
797
798
799
800 if (!tsc_khz || !cpu_khz)
801 return true;
802
803
804 if (!boot_cpu_has(X86_FEATURE_APIC) || disable_apic)
805 return true;
806
807
808
809
810
811
812 if (apic_intr_mode == APIC_PIC ||
813 apic_intr_mode == APIC_VIRTUAL_WIRE_NO_CONFIG)
814 return true;
815
816
817 if (!boot_cpu_has(X86_FEATURE_ARAT))
818 return true;
819
820
821 if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
822 return false;
823
824
825 if (disable_apic_timer)
826 return true;
827
828
829
830
831 return lapic_timer_period == 0;
832}
833
834static int __init calibrate_APIC_clock(void)
835{
836 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
837 u64 tsc_perj = 0, tsc_start = 0;
838 unsigned long jif_start;
839 unsigned long deltaj;
840 long delta, deltatsc;
841 int pm_referenced = 0;
842
843 if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
844 return 0;
845
846
847
848
849
850
851 if (!lapic_init_clockevent()) {
852 apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
853 lapic_timer_period);
854
855
856
857
858 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
859 return 0;
860 }
861
862 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
863 "calibrating APIC timer ...\n");
864
865
866
867
868
869
870 local_irq_disable();
871
872
873
874
875
876 __setup_APIC_LVTT(0xffffffff, 0, 0);
877
878
879
880
881
882
883 jif_start = READ_ONCE(jiffies);
884
885 if (tsc_khz) {
886 tsc_start = rdtsc();
887 tsc_perj = div_u64((u64)tsc_khz * 1000, HZ);
888 }
889
890
891
892
893
894 local_irq_enable();
895
896 while (lapic_cal_loops <= LAPIC_CAL_LOOPS) {
897
898 while (1) {
899 if (tsc_khz) {
900 u64 tsc_now = rdtsc();
901 if ((tsc_now - tsc_start) >= tsc_perj) {
902 tsc_start += tsc_perj;
903 break;
904 }
905 } else {
906 unsigned long jif_now = READ_ONCE(jiffies);
907
908 if (time_after(jif_now, jif_start)) {
909 jif_start = jif_now;
910 break;
911 }
912 }
913 cpu_relax();
914 }
915
916
917 local_irq_disable();
918 lapic_cal_handler(NULL);
919 local_irq_enable();
920 }
921
922 local_irq_disable();
923
924
925 delta = lapic_cal_t1 - lapic_cal_t2;
926 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
927
928 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
929
930
931 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
932 &delta, &deltatsc);
933
934 lapic_timer_period = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
935 lapic_init_clockevent();
936
937 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
938 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
939 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
940 lapic_timer_period);
941
942 if (boot_cpu_has(X86_FEATURE_TSC)) {
943 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
944 "%ld.%04ld MHz.\n",
945 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
946 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
947 }
948
949 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
950 "%u.%04u MHz.\n",
951 lapic_timer_period / (1000000 / HZ),
952 lapic_timer_period % (1000000 / HZ));
953
954
955
956
957 if (lapic_timer_period < (1000000 / HZ)) {
958 local_irq_enable();
959 pr_warn("APIC frequency too slow, disabling apic timer\n");
960 return -1;
961 }
962
963 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
964
965
966
967
968
969
970 if (!pm_referenced && global_clock_event) {
971 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
972
973
974
975
976 levt->event_handler = lapic_cal_handler;
977 lapic_timer_set_periodic(levt);
978 lapic_cal_loops = -1;
979
980
981 local_irq_enable();
982
983 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
984 cpu_relax();
985
986
987 local_irq_disable();
988 lapic_timer_shutdown(levt);
989
990
991 deltaj = lapic_cal_j2 - lapic_cal_j1;
992 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
993
994
995 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
996 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
997 else
998 levt->features |= CLOCK_EVT_FEAT_DUMMY;
999 }
1000 local_irq_enable();
1001
1002 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
1003 pr_warn("APIC timer disabled due to verification failure\n");
1004 return -1;
1005 }
1006
1007 return 0;
1008}
1009
1010
1011
1012
1013
1014
1015void __init setup_boot_APIC_clock(void)
1016{
1017
1018
1019
1020
1021
1022
1023 if (disable_apic_timer) {
1024 pr_info("Disabling APIC timer\n");
1025
1026 if (num_possible_cpus() > 1) {
1027 lapic_clockevent.mult = 1;
1028 setup_APIC_timer();
1029 }
1030 return;
1031 }
1032
1033 if (calibrate_APIC_clock()) {
1034
1035 if (num_possible_cpus() > 1)
1036 setup_APIC_timer();
1037 return;
1038 }
1039
1040
1041
1042
1043
1044
1045 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
1046
1047
1048 setup_APIC_timer();
1049 amd_e400_c1e_apic_setup();
1050}
1051
1052void setup_secondary_APIC_clock(void)
1053{
1054 setup_APIC_timer();
1055 amd_e400_c1e_apic_setup();
1056}
1057
1058
1059
1060
1061static void local_apic_timer_interrupt(void)
1062{
1063 struct clock_event_device *evt = this_cpu_ptr(&lapic_events);
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 if (!evt->event_handler) {
1077 pr_warn("Spurious LAPIC timer interrupt on cpu %d\n",
1078 smp_processor_id());
1079
1080 lapic_timer_shutdown(evt);
1081 return;
1082 }
1083
1084
1085
1086
1087 inc_irq_stat(apic_timer_irqs);
1088
1089 evt->event_handler(evt);
1090}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100DEFINE_IDTENTRY_SYSVEC(sysvec_apic_timer_interrupt)
1101{
1102 struct pt_regs *old_regs = set_irq_regs(regs);
1103
1104 ack_APIC_irq();
1105 trace_local_timer_entry(LOCAL_TIMER_VECTOR);
1106 local_apic_timer_interrupt();
1107 trace_local_timer_exit(LOCAL_TIMER_VECTOR);
1108
1109 set_irq_regs(old_regs);
1110}
1111
1112int setup_profiling_timer(unsigned int multiplier)
1113{
1114 return -EINVAL;
1115}
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128void clear_local_APIC(void)
1129{
1130 int maxlvt;
1131 u32 v;
1132
1133
1134 if (!x2apic_mode && !apic_phys)
1135 return;
1136
1137 maxlvt = lapic_get_maxlvt();
1138
1139
1140
1141
1142 if (maxlvt >= 3) {
1143 v = ERROR_APIC_VECTOR;
1144 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
1145 }
1146
1147
1148
1149
1150 v = apic_read(APIC_LVTT);
1151 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1152 v = apic_read(APIC_LVT0);
1153 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1154 v = apic_read(APIC_LVT1);
1155 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1156 if (maxlvt >= 4) {
1157 v = apic_read(APIC_LVTPC);
1158 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1159 }
1160
1161
1162#ifdef CONFIG_X86_THERMAL_VECTOR
1163 if (maxlvt >= 5) {
1164 v = apic_read(APIC_LVTTHMR);
1165 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
1166 }
1167#endif
1168#ifdef CONFIG_X86_MCE_INTEL
1169 if (maxlvt >= 6) {
1170 v = apic_read(APIC_LVTCMCI);
1171 if (!(v & APIC_LVT_MASKED))
1172 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
1173 }
1174#endif
1175
1176
1177
1178
1179 apic_write(APIC_LVTT, APIC_LVT_MASKED);
1180 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1181 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1182 if (maxlvt >= 3)
1183 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1184 if (maxlvt >= 4)
1185 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
1186
1187
1188 if (lapic_is_integrated()) {
1189 if (maxlvt > 3)
1190
1191 apic_write(APIC_ESR, 0);
1192 apic_read(APIC_ESR);
1193 }
1194}
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207void apic_soft_disable(void)
1208{
1209 u32 value;
1210
1211 clear_local_APIC();
1212
1213
1214 value = apic_read(APIC_SPIV);
1215 value &= ~APIC_SPIV_APIC_ENABLED;
1216 apic_write(APIC_SPIV, value);
1217}
1218
1219
1220
1221
1222void disable_local_APIC(void)
1223{
1224
1225 if (!x2apic_mode && !apic_phys)
1226 return;
1227
1228 apic_soft_disable();
1229
1230#ifdef CONFIG_X86_32
1231
1232
1233
1234
1235 if (enabled_via_apicbase) {
1236 unsigned int l, h;
1237
1238 rdmsr(MSR_IA32_APICBASE, l, h);
1239 l &= ~MSR_IA32_APICBASE_ENABLE;
1240 wrmsr(MSR_IA32_APICBASE, l, h);
1241 }
1242#endif
1243}
1244
1245
1246
1247
1248
1249
1250
1251void lapic_shutdown(void)
1252{
1253 unsigned long flags;
1254
1255 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
1256 return;
1257
1258 local_irq_save(flags);
1259
1260#ifdef CONFIG_X86_32
1261 if (!enabled_via_apicbase)
1262 clear_local_APIC();
1263 else
1264#endif
1265 disable_local_APIC();
1266
1267
1268 local_irq_restore(flags);
1269}
1270
1271
1272
1273
1274void __init sync_Arb_IDs(void)
1275{
1276
1277
1278
1279
1280 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1281 return;
1282
1283
1284
1285
1286 apic_wait_icr_idle();
1287
1288 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1289 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1290 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1291}
1292
1293enum apic_intr_mode_id apic_intr_mode __ro_after_init;
1294
1295static int __init __apic_intr_mode_select(void)
1296{
1297
1298 if (disable_apic) {
1299 pr_info("APIC disabled via kernel command line\n");
1300 return APIC_PIC;
1301 }
1302
1303
1304#ifdef CONFIG_X86_64
1305
1306 if (!boot_cpu_has(X86_FEATURE_APIC)) {
1307 disable_apic = 1;
1308 pr_info("APIC disabled by BIOS\n");
1309 return APIC_PIC;
1310 }
1311#else
1312
1313
1314
1315 if (!boot_cpu_has(X86_FEATURE_APIC) && !smp_found_config) {
1316 disable_apic = 1;
1317 return APIC_PIC;
1318 }
1319
1320
1321 if (!boot_cpu_has(X86_FEATURE_APIC) &&
1322 APIC_INTEGRATED(boot_cpu_apic_version)) {
1323 disable_apic = 1;
1324 pr_err(FW_BUG "Local APIC %d not detected, force emulation\n",
1325 boot_cpu_physical_apicid);
1326 return APIC_PIC;
1327 }
1328#endif
1329
1330
1331 if (!smp_found_config) {
1332 disable_ioapic_support();
1333 if (!acpi_lapic) {
1334 pr_info("APIC: ACPI MADT or MP tables are not detected\n");
1335 return APIC_VIRTUAL_WIRE_NO_CONFIG;
1336 }
1337 return APIC_VIRTUAL_WIRE;
1338 }
1339
1340#ifdef CONFIG_SMP
1341
1342 if (!setup_max_cpus) {
1343 pr_info("APIC: SMP mode deactivated\n");
1344 return APIC_SYMMETRIC_IO_NO_ROUTING;
1345 }
1346
1347 if (read_apic_id() != boot_cpu_physical_apicid) {
1348 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
1349 read_apic_id(), boot_cpu_physical_apicid);
1350
1351 }
1352#endif
1353
1354 return APIC_SYMMETRIC_IO;
1355}
1356
1357
1358void __init apic_intr_mode_select(void)
1359{
1360 apic_intr_mode = __apic_intr_mode_select();
1361}
1362
1363
1364
1365
1366void __init init_bsp_APIC(void)
1367{
1368 unsigned int value;
1369
1370
1371
1372
1373
1374 if (smp_found_config || !boot_cpu_has(X86_FEATURE_APIC))
1375 return;
1376
1377
1378
1379
1380 clear_local_APIC();
1381
1382
1383
1384
1385 value = apic_read(APIC_SPIV);
1386 value &= ~APIC_VECTOR_MASK;
1387 value |= APIC_SPIV_APIC_ENABLED;
1388
1389#ifdef CONFIG_X86_32
1390
1391 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1392 (boot_cpu_data.x86 == 15))
1393 value &= ~APIC_SPIV_FOCUS_DISABLED;
1394 else
1395#endif
1396 value |= APIC_SPIV_FOCUS_DISABLED;
1397 value |= SPURIOUS_APIC_VECTOR;
1398 apic_write(APIC_SPIV, value);
1399
1400
1401
1402
1403 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1404 value = APIC_DM_NMI;
1405 if (!lapic_is_integrated())
1406 value |= APIC_LVT_LEVEL_TRIGGER;
1407 if (apic_extnmi == APIC_EXTNMI_NONE)
1408 value |= APIC_LVT_MASKED;
1409 apic_write(APIC_LVT1, value);
1410}
1411
1412static void __init apic_bsp_setup(bool upmode);
1413
1414
1415void __init apic_intr_mode_init(void)
1416{
1417 bool upmode = IS_ENABLED(CONFIG_UP_LATE_INIT);
1418
1419 switch (apic_intr_mode) {
1420 case APIC_PIC:
1421 pr_info("APIC: Keep in PIC mode(8259)\n");
1422 return;
1423 case APIC_VIRTUAL_WIRE:
1424 pr_info("APIC: Switch to virtual wire mode setup\n");
1425 default_setup_apic_routing();
1426 break;
1427 case APIC_VIRTUAL_WIRE_NO_CONFIG:
1428 pr_info("APIC: Switch to virtual wire mode setup with no configuration\n");
1429 upmode = true;
1430 default_setup_apic_routing();
1431 break;
1432 case APIC_SYMMETRIC_IO:
1433 pr_info("APIC: Switch to symmetric I/O mode setup\n");
1434 default_setup_apic_routing();
1435 break;
1436 case APIC_SYMMETRIC_IO_NO_ROUTING:
1437 pr_info("APIC: Switch to symmetric I/O mode setup in no SMP routine\n");
1438 break;
1439 }
1440
1441 if (x86_platform.apic_post_init)
1442 x86_platform.apic_post_init();
1443
1444 apic_bsp_setup(upmode);
1445}
1446
1447static void lapic_setup_esr(void)
1448{
1449 unsigned int oldvalue, value, maxlvt;
1450
1451 if (!lapic_is_integrated()) {
1452 pr_info("No ESR for 82489DX.\n");
1453 return;
1454 }
1455
1456 if (apic->disable_esr) {
1457
1458
1459
1460
1461
1462
1463 pr_info("Leaving ESR disabled.\n");
1464 return;
1465 }
1466
1467 maxlvt = lapic_get_maxlvt();
1468 if (maxlvt > 3)
1469 apic_write(APIC_ESR, 0);
1470 oldvalue = apic_read(APIC_ESR);
1471
1472
1473 value = ERROR_APIC_VECTOR;
1474 apic_write(APIC_LVTERR, value);
1475
1476
1477
1478
1479 if (maxlvt > 3)
1480 apic_write(APIC_ESR, 0);
1481 value = apic_read(APIC_ESR);
1482 if (value != oldvalue)
1483 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1484 "vector: 0x%08x after: 0x%08x\n",
1485 oldvalue, value);
1486}
1487
1488#define APIC_IR_REGS APIC_ISR_NR
1489#define APIC_IR_BITS (APIC_IR_REGS * 32)
1490#define APIC_IR_MAPSIZE (APIC_IR_BITS / BITS_PER_LONG)
1491
1492union apic_ir {
1493 unsigned long map[APIC_IR_MAPSIZE];
1494 u32 regs[APIC_IR_REGS];
1495};
1496
1497static bool apic_check_and_ack(union apic_ir *irr, union apic_ir *isr)
1498{
1499 int i, bit;
1500
1501
1502 for (i = 0; i < APIC_IR_REGS; i++)
1503 irr->regs[i] = apic_read(APIC_IRR + i * 0x10);
1504
1505
1506 for (i = 0; i < APIC_IR_REGS; i++)
1507 isr->regs[i] = apic_read(APIC_ISR + i * 0x10);
1508
1509
1510
1511
1512
1513
1514 if (!bitmap_empty(isr->map, APIC_IR_BITS)) {
1515
1516
1517
1518
1519
1520 for_each_set_bit(bit, isr->map, APIC_IR_BITS)
1521 ack_APIC_irq();
1522 return true;
1523 }
1524
1525 return !bitmap_empty(irr->map, APIC_IR_BITS);
1526}
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542static void apic_pending_intr_clear(void)
1543{
1544 union apic_ir irr, isr;
1545 unsigned int i;
1546
1547
1548 for (i = 0; i < 512; i++) {
1549 if (!apic_check_and_ack(&irr, &isr))
1550 return;
1551 }
1552
1553 pr_warn("APIC: Stale IRR: %256pb ISR: %256pb\n", irr.map, isr.map);
1554}
1555
1556
1557
1558
1559
1560
1561
1562static void setup_local_APIC(void)
1563{
1564 int cpu = smp_processor_id();
1565 unsigned int value;
1566
1567 if (disable_apic) {
1568 disable_ioapic_support();
1569 return;
1570 }
1571
1572
1573
1574
1575
1576 value = apic_read(APIC_SPIV);
1577 value &= ~APIC_SPIV_APIC_ENABLED;
1578 apic_write(APIC_SPIV, value);
1579
1580#ifdef CONFIG_X86_32
1581
1582 if (lapic_is_integrated() && apic->disable_esr) {
1583 apic_write(APIC_ESR, 0);
1584 apic_write(APIC_ESR, 0);
1585 apic_write(APIC_ESR, 0);
1586 apic_write(APIC_ESR, 0);
1587 }
1588#endif
1589
1590
1591
1592
1593 BUG_ON(!apic->apic_id_registered());
1594
1595
1596
1597
1598
1599
1600 apic->init_apic_ldr();
1601
1602#ifdef CONFIG_X86_32
1603 if (apic->dest_mode_logical) {
1604 int logical_apicid, ldr_apicid;
1605
1606
1607
1608
1609
1610
1611 logical_apicid = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1612 ldr_apicid = GET_APIC_LOGICAL_ID(apic_read(APIC_LDR));
1613 if (logical_apicid != BAD_APICID)
1614 WARN_ON(logical_apicid != ldr_apicid);
1615
1616 early_per_cpu(x86_cpu_to_logical_apicid, cpu) = ldr_apicid;
1617 }
1618#endif
1619
1620
1621
1622
1623
1624
1625
1626 value = apic_read(APIC_TASKPRI);
1627 value &= ~APIC_TPRI_MASK;
1628 value |= 0x10;
1629 apic_write(APIC_TASKPRI, value);
1630
1631
1632 apic_pending_intr_clear();
1633
1634
1635
1636
1637 value = apic_read(APIC_SPIV);
1638 value &= ~APIC_VECTOR_MASK;
1639
1640
1641
1642 value |= APIC_SPIV_APIC_ENABLED;
1643
1644#ifdef CONFIG_X86_32
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669 value &= ~APIC_SPIV_FOCUS_DISABLED;
1670#endif
1671
1672
1673
1674
1675 value |= SPURIOUS_APIC_VECTOR;
1676 apic_write(APIC_SPIV, value);
1677
1678 perf_events_lapic_init();
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1691 if (!cpu && (pic_mode || !value || skip_ioapic_setup)) {
1692 value = APIC_DM_EXTINT;
1693 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1694 } else {
1695 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1696 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1697 }
1698 apic_write(APIC_LVT0, value);
1699
1700
1701
1702
1703
1704 if ((!cpu && apic_extnmi != APIC_EXTNMI_NONE) ||
1705 apic_extnmi == APIC_EXTNMI_ALL)
1706 value = APIC_DM_NMI;
1707 else
1708 value = APIC_DM_NMI | APIC_LVT_MASKED;
1709
1710
1711 if (!lapic_is_integrated())
1712 value |= APIC_LVT_LEVEL_TRIGGER;
1713 apic_write(APIC_LVT1, value);
1714
1715#ifdef CONFIG_X86_MCE_INTEL
1716
1717 if (!cpu)
1718 cmci_recheck();
1719#endif
1720}
1721
1722static void end_local_APIC_setup(void)
1723{
1724 lapic_setup_esr();
1725
1726#ifdef CONFIG_X86_32
1727 {
1728 unsigned int value;
1729
1730 value = apic_read(APIC_LVTT);
1731 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1732 apic_write(APIC_LVTT, value);
1733 }
1734#endif
1735
1736 apic_pm_activate();
1737}
1738
1739
1740
1741
1742void apic_ap_setup(void)
1743{
1744 setup_local_APIC();
1745 end_local_APIC_setup();
1746}
1747
1748#ifdef CONFIG_X86_X2APIC
1749int x2apic_mode;
1750EXPORT_SYMBOL_GPL(x2apic_mode);
1751
1752enum {
1753 X2APIC_OFF,
1754 X2APIC_ON,
1755 X2APIC_DISABLED,
1756};
1757static int x2apic_state;
1758
1759static void __x2apic_disable(void)
1760{
1761 u64 msr;
1762
1763 if (!boot_cpu_has(X86_FEATURE_APIC))
1764 return;
1765
1766 rdmsrl(MSR_IA32_APICBASE, msr);
1767 if (!(msr & X2APIC_ENABLE))
1768 return;
1769
1770 wrmsrl(MSR_IA32_APICBASE, msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1771 wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1772 printk_once(KERN_INFO "x2apic disabled\n");
1773}
1774
1775static void __x2apic_enable(void)
1776{
1777 u64 msr;
1778
1779 rdmsrl(MSR_IA32_APICBASE, msr);
1780 if (msr & X2APIC_ENABLE)
1781 return;
1782 wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1783 printk_once(KERN_INFO "x2apic enabled\n");
1784}
1785
1786static int __init setup_nox2apic(char *str)
1787{
1788 if (x2apic_enabled()) {
1789 int apicid = native_apic_msr_read(APIC_ID);
1790
1791 if (apicid >= 255) {
1792 pr_warn("Apicid: %08x, cannot enforce nox2apic\n",
1793 apicid);
1794 return 0;
1795 }
1796 pr_warn("x2apic already enabled.\n");
1797 __x2apic_disable();
1798 }
1799 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
1800 x2apic_state = X2APIC_DISABLED;
1801 x2apic_mode = 0;
1802 return 0;
1803}
1804early_param("nox2apic", setup_nox2apic);
1805
1806
1807void x2apic_setup(void)
1808{
1809
1810
1811
1812
1813 if (x2apic_state != X2APIC_ON) {
1814 __x2apic_disable();
1815 return;
1816 }
1817 __x2apic_enable();
1818}
1819
1820static __init void x2apic_disable(void)
1821{
1822 u32 x2apic_id, state = x2apic_state;
1823
1824 x2apic_mode = 0;
1825 x2apic_state = X2APIC_DISABLED;
1826
1827 if (state != X2APIC_ON)
1828 return;
1829
1830 x2apic_id = read_apic_id();
1831 if (x2apic_id >= 255)
1832 panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
1833
1834 __x2apic_disable();
1835 register_lapic_address(mp_lapic_addr);
1836}
1837
1838static __init void x2apic_enable(void)
1839{
1840 if (x2apic_state != X2APIC_OFF)
1841 return;
1842
1843 x2apic_mode = 1;
1844 x2apic_state = X2APIC_ON;
1845 __x2apic_enable();
1846}
1847
1848static __init void try_to_enable_x2apic(int remap_mode)
1849{
1850 if (x2apic_state == X2APIC_DISABLED)
1851 return;
1852
1853 if (remap_mode != IRQ_REMAP_X2APIC_MODE) {
1854 u32 apic_limit = 255;
1855
1856
1857
1858
1859
1860 if (!x86_init.hyper.x2apic_available()) {
1861 pr_info("x2apic: IRQ remapping doesn't support X2APIC mode\n");
1862 x2apic_disable();
1863 return;
1864 }
1865
1866
1867
1868
1869
1870
1871 if (x86_init.hyper.msi_ext_dest_id()) {
1872 virt_ext_dest_id = 1;
1873 apic_limit = 32767;
1874 }
1875
1876
1877
1878
1879
1880
1881 x2apic_set_max_apicid(apic_limit);
1882 x2apic_phys = 1;
1883 }
1884 x2apic_enable();
1885}
1886
1887void __init check_x2apic(void)
1888{
1889 if (x2apic_enabled()) {
1890 pr_info("x2apic: enabled by BIOS, switching to x2apic ops\n");
1891 x2apic_mode = 1;
1892 x2apic_state = X2APIC_ON;
1893 } else if (!boot_cpu_has(X86_FEATURE_X2APIC)) {
1894 x2apic_state = X2APIC_DISABLED;
1895 }
1896}
1897#else
1898static int __init validate_x2apic(void)
1899{
1900 if (!apic_is_x2apic_enabled())
1901 return 0;
1902
1903
1904
1905 panic("BIOS has enabled x2apic but kernel doesn't support x2apic, please disable x2apic in BIOS.\n");
1906}
1907early_initcall(validate_x2apic);
1908
1909static inline void try_to_enable_x2apic(int remap_mode) { }
1910static inline void __x2apic_enable(void) { }
1911#endif
1912
1913void __init enable_IR_x2apic(void)
1914{
1915 unsigned long flags;
1916 int ret, ir_stat;
1917
1918 if (skip_ioapic_setup) {
1919 pr_info("Not enabling interrupt remapping due to skipped IO-APIC setup\n");
1920 return;
1921 }
1922
1923 ir_stat = irq_remapping_prepare();
1924 if (ir_stat < 0 && !x2apic_supported())
1925 return;
1926
1927 ret = save_ioapic_entries();
1928 if (ret) {
1929 pr_info("Saving IO-APIC state failed: %d\n", ret);
1930 return;
1931 }
1932
1933 local_irq_save(flags);
1934 legacy_pic->mask_all();
1935 mask_ioapic_entries();
1936
1937
1938 if (ir_stat >= 0)
1939 ir_stat = irq_remapping_enable();
1940
1941 try_to_enable_x2apic(ir_stat);
1942
1943 if (ir_stat < 0)
1944 restore_ioapic_entries();
1945 legacy_pic->restore_mask();
1946 local_irq_restore(flags);
1947}
1948
1949#ifdef CONFIG_X86_64
1950
1951
1952
1953
1954
1955
1956static int __init detect_init_APIC(void)
1957{
1958 if (!boot_cpu_has(X86_FEATURE_APIC)) {
1959 pr_info("No local APIC present\n");
1960 return -1;
1961 }
1962
1963 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1964 return 0;
1965}
1966#else
1967
1968static int __init apic_verify(void)
1969{
1970 u32 features, h, l;
1971
1972
1973
1974
1975
1976 features = cpuid_edx(1);
1977 if (!(features & (1 << X86_FEATURE_APIC))) {
1978 pr_warn("Could not enable APIC!\n");
1979 return -1;
1980 }
1981 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1982 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1983
1984
1985 if (boot_cpu_data.x86 >= 6) {
1986 rdmsr(MSR_IA32_APICBASE, l, h);
1987 if (l & MSR_IA32_APICBASE_ENABLE)
1988 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1989 }
1990
1991 pr_info("Found and enabled local APIC!\n");
1992 return 0;
1993}
1994
1995int __init apic_force_enable(unsigned long addr)
1996{
1997 u32 h, l;
1998
1999 if (disable_apic)
2000 return -1;
2001
2002
2003
2004
2005
2006
2007 if (boot_cpu_data.x86 >= 6) {
2008 rdmsr(MSR_IA32_APICBASE, l, h);
2009 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
2010 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
2011 l &= ~MSR_IA32_APICBASE_BASE;
2012 l |= MSR_IA32_APICBASE_ENABLE | addr;
2013 wrmsr(MSR_IA32_APICBASE, l, h);
2014 enabled_via_apicbase = 1;
2015 }
2016 }
2017 return apic_verify();
2018}
2019
2020
2021
2022
2023static int __init detect_init_APIC(void)
2024{
2025
2026 if (disable_apic)
2027 return -1;
2028
2029 switch (boot_cpu_data.x86_vendor) {
2030 case X86_VENDOR_AMD:
2031 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
2032 (boot_cpu_data.x86 >= 15))
2033 break;
2034 goto no_apic;
2035 case X86_VENDOR_HYGON:
2036 break;
2037 case X86_VENDOR_INTEL:
2038 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
2039 (boot_cpu_data.x86 == 5 && boot_cpu_has(X86_FEATURE_APIC)))
2040 break;
2041 goto no_apic;
2042 default:
2043 goto no_apic;
2044 }
2045
2046 if (!boot_cpu_has(X86_FEATURE_APIC)) {
2047
2048
2049
2050
2051 if (!force_enable_local_apic) {
2052 pr_info("Local APIC disabled by BIOS -- "
2053 "you can enable it with \"lapic\"\n");
2054 return -1;
2055 }
2056 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
2057 return -1;
2058 } else {
2059 if (apic_verify())
2060 return -1;
2061 }
2062
2063 apic_pm_activate();
2064
2065 return 0;
2066
2067no_apic:
2068 pr_info("No local APIC present or hardware disabled\n");
2069 return -1;
2070}
2071#endif
2072
2073
2074
2075
2076void __init init_apic_mappings(void)
2077{
2078 unsigned int new_apicid;
2079
2080 if (apic_validate_deadline_timer())
2081 pr_info("TSC deadline timer available\n");
2082
2083 if (x2apic_mode) {
2084 boot_cpu_physical_apicid = read_apic_id();
2085 return;
2086 }
2087
2088
2089 if (!smp_found_config && detect_init_APIC()) {
2090
2091 pr_info("APIC: disable apic facility\n");
2092 apic_disable();
2093 } else {
2094 apic_phys = mp_lapic_addr;
2095
2096
2097
2098
2099
2100 if (!acpi_lapic && !smp_found_config)
2101 register_lapic_address(apic_phys);
2102 }
2103
2104
2105
2106
2107
2108 new_apicid = read_apic_id();
2109 if (boot_cpu_physical_apicid != new_apicid) {
2110 boot_cpu_physical_apicid = new_apicid;
2111
2112
2113
2114
2115
2116
2117
2118 boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
2119 }
2120}
2121
2122void __init register_lapic_address(unsigned long address)
2123{
2124 mp_lapic_addr = address;
2125
2126 if (!x2apic_mode) {
2127 set_fixmap_nocache(FIX_APIC_BASE, address);
2128 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
2129 APIC_BASE, address);
2130 }
2131 if (boot_cpu_physical_apicid == -1U) {
2132 boot_cpu_physical_apicid = read_apic_id();
2133 boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
2134 }
2135}
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145static noinline void handle_spurious_interrupt(u8 vector)
2146{
2147 u32 v;
2148
2149 trace_spurious_apic_entry(vector);
2150
2151 inc_irq_stat(irq_spurious_count);
2152
2153
2154
2155
2156 if (vector == SPURIOUS_APIC_VECTOR) {
2157
2158 pr_info("Spurious APIC interrupt (vector 0xFF) on CPU#%d, should never happen.\n",
2159 smp_processor_id());
2160 goto out;
2161 }
2162
2163
2164
2165
2166
2167 v = apic_read(APIC_ISR + ((vector & ~0x1f) >> 1));
2168 if (v & (1 << (vector & 0x1f))) {
2169 pr_info("Spurious interrupt (vector 0x%02x) on CPU#%d. Acked\n",
2170 vector, smp_processor_id());
2171 ack_APIC_irq();
2172 } else {
2173 pr_info("Spurious interrupt (vector 0x%02x) on CPU#%d. Not pending!\n",
2174 vector, smp_processor_id());
2175 }
2176out:
2177 trace_spurious_apic_exit(vector);
2178}
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189DEFINE_IDTENTRY_IRQ(spurious_interrupt)
2190{
2191 handle_spurious_interrupt(vector);
2192}
2193
2194DEFINE_IDTENTRY_SYSVEC(sysvec_spurious_apic_interrupt)
2195{
2196 handle_spurious_interrupt(SPURIOUS_APIC_VECTOR);
2197}
2198
2199
2200
2201
2202DEFINE_IDTENTRY_SYSVEC(sysvec_error_interrupt)
2203{
2204 static const char * const error_interrupt_reason[] = {
2205 "Send CS error",
2206 "Receive CS error",
2207 "Send accept error",
2208 "Receive accept error",
2209 "Redirectable IPI",
2210 "Send illegal vector",
2211 "Received illegal vector",
2212 "Illegal register address",
2213 };
2214 u32 v, i = 0;
2215
2216 trace_error_apic_entry(ERROR_APIC_VECTOR);
2217
2218
2219 if (lapic_get_maxlvt() > 3)
2220 apic_write(APIC_ESR, 0);
2221 v = apic_read(APIC_ESR);
2222 ack_APIC_irq();
2223 atomic_inc(&irq_err_count);
2224
2225 apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x",
2226 smp_processor_id(), v);
2227
2228 v &= 0xff;
2229 while (v) {
2230 if (v & 0x1)
2231 apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
2232 i++;
2233 v >>= 1;
2234 }
2235
2236 apic_printk(APIC_DEBUG, KERN_CONT "\n");
2237
2238 trace_error_apic_exit(ERROR_APIC_VECTOR);
2239}
2240
2241
2242
2243
2244static void __init connect_bsp_APIC(void)
2245{
2246#ifdef CONFIG_X86_32
2247 if (pic_mode) {
2248
2249
2250
2251 clear_local_APIC();
2252
2253
2254
2255
2256 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
2257 "enabling APIC mode.\n");
2258 imcr_pic_to_apic();
2259 }
2260#endif
2261}
2262
2263
2264
2265
2266
2267
2268
2269
2270void disconnect_bsp_APIC(int virt_wire_setup)
2271{
2272 unsigned int value;
2273
2274#ifdef CONFIG_X86_32
2275 if (pic_mode) {
2276
2277
2278
2279
2280
2281
2282 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
2283 "entering PIC mode.\n");
2284 imcr_apic_to_pic();
2285 return;
2286 }
2287#endif
2288
2289
2290
2291
2292 value = apic_read(APIC_SPIV);
2293 value &= ~APIC_VECTOR_MASK;
2294 value |= APIC_SPIV_APIC_ENABLED;
2295 value |= 0xf;
2296 apic_write(APIC_SPIV, value);
2297
2298 if (!virt_wire_setup) {
2299
2300
2301
2302
2303 value = apic_read(APIC_LVT0);
2304 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2305 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2306 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2307 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2308 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
2309 apic_write(APIC_LVT0, value);
2310 } else {
2311
2312 apic_write(APIC_LVT0, APIC_LVT_MASKED);
2313 }
2314
2315
2316
2317
2318
2319 value = apic_read(APIC_LVT1);
2320 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2321 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2322 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2323 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2324 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
2325 apic_write(APIC_LVT1, value);
2326}
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336static int nr_logical_cpuids = 1;
2337
2338
2339
2340
2341static int cpuid_to_apicid[] = {
2342 [0 ... NR_CPUS - 1] = -1,
2343};
2344
2345bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
2346{
2347 return phys_id == cpuid_to_apicid[cpu];
2348}
2349
2350#ifdef CONFIG_SMP
2351
2352
2353
2354
2355bool apic_id_is_primary_thread(unsigned int apicid)
2356{
2357 u32 mask;
2358
2359 if (smp_num_siblings == 1)
2360 return true;
2361
2362 mask = (1U << (fls(smp_num_siblings) - 1)) - 1;
2363 return !(apicid & mask);
2364}
2365#endif
2366
2367
2368
2369
2370
2371static int allocate_logical_cpuid(int apicid)
2372{
2373 int i;
2374
2375
2376
2377
2378
2379 for (i = 0; i < nr_logical_cpuids; i++) {
2380 if (cpuid_to_apicid[i] == apicid)
2381 return i;
2382 }
2383
2384
2385 if (nr_logical_cpuids >= nr_cpu_ids) {
2386 WARN_ONCE(1, "APIC: NR_CPUS/possible_cpus limit of %u reached. "
2387 "Processor %d/0x%x and the rest are ignored.\n",
2388 nr_cpu_ids, nr_logical_cpuids, apicid);
2389 return -EINVAL;
2390 }
2391
2392 cpuid_to_apicid[nr_logical_cpuids] = apicid;
2393 return nr_logical_cpuids++;
2394}
2395
2396int generic_processor_info(int apicid, int version)
2397{
2398 int cpu, max = nr_cpu_ids;
2399 bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
2400 phys_cpu_present_map);
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421 if (disabled_cpu_apicid != BAD_APICID &&
2422 disabled_cpu_apicid != read_apic_id() &&
2423 disabled_cpu_apicid == apicid) {
2424 int thiscpu = num_processors + disabled_cpus;
2425
2426 pr_warn("APIC: Disabling requested cpu."
2427 " Processor %d/0x%x ignored.\n", thiscpu, apicid);
2428
2429 disabled_cpus++;
2430 return -ENODEV;
2431 }
2432
2433
2434
2435
2436
2437 if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
2438 apicid != boot_cpu_physical_apicid) {
2439 int thiscpu = max + disabled_cpus - 1;
2440
2441 pr_warn("APIC: NR_CPUS/possible_cpus limit of %i almost"
2442 " reached. Keeping one slot for boot cpu."
2443 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2444
2445 disabled_cpus++;
2446 return -ENODEV;
2447 }
2448
2449 if (num_processors >= nr_cpu_ids) {
2450 int thiscpu = max + disabled_cpus;
2451
2452 pr_warn("APIC: NR_CPUS/possible_cpus limit of %i reached. "
2453 "Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2454
2455 disabled_cpus++;
2456 return -EINVAL;
2457 }
2458
2459 if (apicid == boot_cpu_physical_apicid) {
2460
2461
2462
2463
2464
2465
2466
2467 cpu = 0;
2468
2469
2470 cpuid_to_apicid[0] = apicid;
2471 } else {
2472 cpu = allocate_logical_cpuid(apicid);
2473 if (cpu < 0) {
2474 disabled_cpus++;
2475 return -EINVAL;
2476 }
2477 }
2478
2479
2480
2481
2482 if (version == 0x0) {
2483 pr_warn("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2484 cpu, apicid);
2485 version = 0x10;
2486 }
2487
2488 if (version != boot_cpu_apic_version) {
2489 pr_warn("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2490 boot_cpu_apic_version, cpu, version);
2491 }
2492
2493 if (apicid > max_physical_apicid)
2494 max_physical_apicid = apicid;
2495
2496#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2497 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2498 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
2499#endif
2500#ifdef CONFIG_X86_32
2501 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2502 apic->x86_32_early_logical_apicid(cpu);
2503#endif
2504 set_cpu_possible(cpu, true);
2505 physid_set(apicid, phys_cpu_present_map);
2506 set_cpu_present(cpu, true);
2507 num_processors++;
2508
2509 return cpu;
2510}
2511
2512int hard_smp_processor_id(void)
2513{
2514 return read_apic_id();
2515}
2516
2517void __irq_msi_compose_msg(struct irq_cfg *cfg, struct msi_msg *msg,
2518 bool dmar)
2519{
2520 memset(msg, 0, sizeof(*msg));
2521
2522 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
2523 msg->arch_addr_lo.dest_mode_logical = apic->dest_mode_logical;
2524 msg->arch_addr_lo.destid_0_7 = cfg->dest_apicid & 0xFF;
2525
2526 msg->arch_data.delivery_mode = APIC_DELIVERY_MODE_FIXED;
2527 msg->arch_data.vector = cfg->vector;
2528
2529 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539 if (dmar)
2540 msg->arch_addr_hi.destid_8_31 = cfg->dest_apicid >> 8;
2541 else if (virt_ext_dest_id && cfg->dest_apicid < 0x8000)
2542 msg->arch_addr_lo.virt_destid_8_14 = cfg->dest_apicid >> 8;
2543 else
2544 WARN_ON_ONCE(cfg->dest_apicid > 0xFF);
2545}
2546
2547u32 x86_msi_msg_get_destid(struct msi_msg *msg, bool extid)
2548{
2549 u32 dest = msg->arch_addr_lo.destid_0_7;
2550
2551 if (extid)
2552 dest |= msg->arch_addr_hi.destid_8_31 << 8;
2553 return dest;
2554}
2555EXPORT_SYMBOL_GPL(x86_msi_msg_get_destid);
2556
2557
2558
2559
2560
2561
2562
2563void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v))
2564{
2565 struct apic **drv;
2566
2567 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
2568
2569 WARN_ON((*drv)->eoi_write == eoi_write);
2570 (*drv)->native_eoi_write = (*drv)->eoi_write;
2571 (*drv)->eoi_write = eoi_write;
2572 }
2573}
2574
2575static void __init apic_bsp_up_setup(void)
2576{
2577#ifdef CONFIG_X86_64
2578 apic_write(APIC_ID, apic->set_apic_id(boot_cpu_physical_apicid));
2579#else
2580
2581
2582
2583
2584
2585# ifdef CONFIG_CRASH_DUMP
2586 boot_cpu_physical_apicid = read_apic_id();
2587# endif
2588#endif
2589 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
2590}
2591
2592
2593
2594
2595
2596static void __init apic_bsp_setup(bool upmode)
2597{
2598 connect_bsp_APIC();
2599 if (upmode)
2600 apic_bsp_up_setup();
2601 setup_local_APIC();
2602
2603 enable_IO_APIC();
2604 end_local_APIC_setup();
2605 irq_remap_enable_fault_handling();
2606 setup_IO_APIC();
2607 lapic_update_legacy_vectors();
2608}
2609
2610#ifdef CONFIG_UP_LATE_INIT
2611void __init up_late_init(void)
2612{
2613 if (apic_intr_mode == APIC_PIC)
2614 return;
2615
2616
2617 x86_init.timers.setup_percpu_clockev();
2618}
2619#endif
2620
2621
2622
2623
2624#ifdef CONFIG_PM
2625
2626static struct {
2627
2628
2629
2630
2631
2632 int active;
2633
2634 unsigned int apic_id;
2635 unsigned int apic_taskpri;
2636 unsigned int apic_ldr;
2637 unsigned int apic_dfr;
2638 unsigned int apic_spiv;
2639 unsigned int apic_lvtt;
2640 unsigned int apic_lvtpc;
2641 unsigned int apic_lvt0;
2642 unsigned int apic_lvt1;
2643 unsigned int apic_lvterr;
2644 unsigned int apic_tmict;
2645 unsigned int apic_tdcr;
2646 unsigned int apic_thmr;
2647 unsigned int apic_cmci;
2648} apic_pm_state;
2649
2650static int lapic_suspend(void)
2651{
2652 unsigned long flags;
2653 int maxlvt;
2654
2655 if (!apic_pm_state.active)
2656 return 0;
2657
2658 maxlvt = lapic_get_maxlvt();
2659
2660 apic_pm_state.apic_id = apic_read(APIC_ID);
2661 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2662 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2663 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2664 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2665 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2666 if (maxlvt >= 4)
2667 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2668 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2669 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2670 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2671 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2672 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
2673#ifdef CONFIG_X86_THERMAL_VECTOR
2674 if (maxlvt >= 5)
2675 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2676#endif
2677#ifdef CONFIG_X86_MCE_INTEL
2678 if (maxlvt >= 6)
2679 apic_pm_state.apic_cmci = apic_read(APIC_LVTCMCI);
2680#endif
2681
2682 local_irq_save(flags);
2683
2684
2685
2686
2687
2688 mask_ioapic_entries();
2689
2690 disable_local_APIC();
2691
2692 irq_remapping_disable();
2693
2694 local_irq_restore(flags);
2695 return 0;
2696}
2697
2698static void lapic_resume(void)
2699{
2700 unsigned int l, h;
2701 unsigned long flags;
2702 int maxlvt;
2703
2704 if (!apic_pm_state.active)
2705 return;
2706
2707 local_irq_save(flags);
2708
2709
2710
2711
2712
2713
2714
2715 mask_ioapic_entries();
2716 legacy_pic->mask_all();
2717
2718 if (x2apic_mode) {
2719 __x2apic_enable();
2720 } else {
2721
2722
2723
2724
2725
2726
2727 if (boot_cpu_data.x86 >= 6) {
2728 rdmsr(MSR_IA32_APICBASE, l, h);
2729 l &= ~MSR_IA32_APICBASE_BASE;
2730 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2731 wrmsr(MSR_IA32_APICBASE, l, h);
2732 }
2733 }
2734
2735 maxlvt = lapic_get_maxlvt();
2736 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2737 apic_write(APIC_ID, apic_pm_state.apic_id);
2738 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2739 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2740 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2741 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2742 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2743 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2744#ifdef CONFIG_X86_THERMAL_VECTOR
2745 if (maxlvt >= 5)
2746 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2747#endif
2748#ifdef CONFIG_X86_MCE_INTEL
2749 if (maxlvt >= 6)
2750 apic_write(APIC_LVTCMCI, apic_pm_state.apic_cmci);
2751#endif
2752 if (maxlvt >= 4)
2753 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2754 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2755 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2756 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2757 apic_write(APIC_ESR, 0);
2758 apic_read(APIC_ESR);
2759 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2760 apic_write(APIC_ESR, 0);
2761 apic_read(APIC_ESR);
2762
2763 irq_remapping_reenable(x2apic_mode);
2764
2765 local_irq_restore(flags);
2766}
2767
2768
2769
2770
2771
2772
2773static struct syscore_ops lapic_syscore_ops = {
2774 .resume = lapic_resume,
2775 .suspend = lapic_suspend,
2776};
2777
2778static void apic_pm_activate(void)
2779{
2780 apic_pm_state.active = 1;
2781}
2782
2783static int __init init_lapic_sysfs(void)
2784{
2785
2786 if (boot_cpu_has(X86_FEATURE_APIC))
2787 register_syscore_ops(&lapic_syscore_ops);
2788
2789 return 0;
2790}
2791
2792
2793core_initcall(init_lapic_sysfs);
2794
2795#else
2796
2797static void apic_pm_activate(void) { }
2798
2799#endif
2800
2801#ifdef CONFIG_X86_64
2802
2803static int multi_checked;
2804static int multi;
2805
2806static int set_multi(const struct dmi_system_id *d)
2807{
2808 if (multi)
2809 return 0;
2810 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
2811 multi = 1;
2812 return 0;
2813}
2814
2815static const struct dmi_system_id multi_dmi_table[] = {
2816 {
2817 .callback = set_multi,
2818 .ident = "IBM System Summit2",
2819 .matches = {
2820 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2821 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2822 },
2823 },
2824 {}
2825};
2826
2827static void dmi_check_multi(void)
2828{
2829 if (multi_checked)
2830 return;
2831
2832 dmi_check_system(multi_dmi_table);
2833 multi_checked = 1;
2834}
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844int apic_is_clustered_box(void)
2845{
2846 dmi_check_multi();
2847 return multi;
2848}
2849#endif
2850
2851
2852
2853
2854static int __init setup_disableapic(char *arg)
2855{
2856 disable_apic = 1;
2857 setup_clear_cpu_cap(X86_FEATURE_APIC);
2858 return 0;
2859}
2860early_param("disableapic", setup_disableapic);
2861
2862
2863static int __init setup_nolapic(char *arg)
2864{
2865 return setup_disableapic(arg);
2866}
2867early_param("nolapic", setup_nolapic);
2868
2869static int __init parse_lapic_timer_c2_ok(char *arg)
2870{
2871 local_apic_timer_c2_ok = 1;
2872 return 0;
2873}
2874early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2875
2876static int __init parse_disable_apic_timer(char *arg)
2877{
2878 disable_apic_timer = 1;
2879 return 0;
2880}
2881early_param("noapictimer", parse_disable_apic_timer);
2882
2883static int __init parse_nolapic_timer(char *arg)
2884{
2885 disable_apic_timer = 1;
2886 return 0;
2887}
2888early_param("nolapic_timer", parse_nolapic_timer);
2889
2890static int __init apic_set_verbosity(char *arg)
2891{
2892 if (!arg) {
2893#ifdef CONFIG_X86_64
2894 skip_ioapic_setup = 0;
2895 return 0;
2896#endif
2897 return -EINVAL;
2898 }
2899
2900 if (strcmp("debug", arg) == 0)
2901 apic_verbosity = APIC_DEBUG;
2902 else if (strcmp("verbose", arg) == 0)
2903 apic_verbosity = APIC_VERBOSE;
2904#ifdef CONFIG_X86_64
2905 else {
2906 pr_warn("APIC Verbosity level %s not recognised"
2907 " use apic=verbose or apic=debug\n", arg);
2908 return -EINVAL;
2909 }
2910#endif
2911
2912 return 0;
2913}
2914early_param("apic", apic_set_verbosity);
2915
2916static int __init lapic_insert_resource(void)
2917{
2918 if (!apic_phys)
2919 return -1;
2920
2921
2922 lapic_resource.start = apic_phys;
2923 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2924 insert_resource(&iomem_resource, &lapic_resource);
2925
2926 return 0;
2927}
2928
2929
2930
2931
2932
2933late_initcall(lapic_insert_resource);
2934
2935static int __init apic_set_disabled_cpu_apicid(char *arg)
2936{
2937 if (!arg || !get_option(&arg, &disabled_cpu_apicid))
2938 return -EINVAL;
2939
2940 return 0;
2941}
2942early_param("disable_cpu_apicid", apic_set_disabled_cpu_apicid);
2943
2944static int __init apic_set_extnmi(char *arg)
2945{
2946 if (!arg)
2947 return -EINVAL;
2948
2949 if (!strncmp("all", arg, 3))
2950 apic_extnmi = APIC_EXTNMI_ALL;
2951 else if (!strncmp("none", arg, 4))
2952 apic_extnmi = APIC_EXTNMI_NONE;
2953 else if (!strncmp("bsp", arg, 3))
2954 apic_extnmi = APIC_EXTNMI_BSP;
2955 else {
2956 pr_warn("Unknown external NMI delivery mode `%s' ignored\n", arg);
2957 return -EINVAL;
2958 }
2959
2960 return 0;
2961}
2962early_param("apic_extnmi", apic_set_extnmi);
2963