1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/init.h>
18
19#include <linux/mm.h>
20#include <linux/delay.h>
21#include <linux/bootmem.h>
22#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
26#include <linux/cpu.h>
27#include <linux/clockchips.h>
28#include <linux/acpi_pmtmr.h>
29#include <linux/module.h>
30#include <linux/dmi.h>
31
32#include <asm/atomic.h>
33#include <asm/smp.h>
34#include <asm/mtrr.h>
35#include <asm/mpspec.h>
36#include <asm/desc.h>
37#include <asm/arch_hooks.h>
38#include <asm/hpet.h>
39#include <asm/i8253.h>
40#include <asm/nmi.h>
41
42#include <mach_apic.h>
43#include <mach_apicdef.h>
44#include <mach_ipi.h>
45
46#include "io_ports.h"
47
48
49
50
51#if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
52# error SPURIOUS_APIC_VECTOR definition error
53#endif
54
55
56
57
58
59
60static int enable_local_apic __initdata = 0;
61
62
63static int local_apic_timer_verify_ok;
64
65
66int local_apic_timer_disabled;
67
68int local_apic_timer_c2_ok;
69EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
70
71
72
73
74int apic_verbosity;
75
76static unsigned int calibration_result;
77
78static int lapic_next_event(unsigned long delta,
79 struct clock_event_device *evt);
80static void lapic_timer_setup(enum clock_event_mode mode,
81 struct clock_event_device *evt);
82static void lapic_timer_broadcast(cpumask_t mask);
83static void apic_pm_activate(void);
84
85
86
87
88static struct clock_event_device lapic_clockevent = {
89 .name = "lapic",
90 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
91 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
92 .shift = 32,
93 .set_mode = lapic_timer_setup,
94 .set_next_event = lapic_next_event,
95 .broadcast = lapic_timer_broadcast,
96 .rating = 100,
97 .irq = -1,
98};
99static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
100
101
102static int enabled_via_apicbase;
103
104
105
106
107static inline int lapic_get_version(void)
108{
109 return GET_APIC_VERSION(apic_read(APIC_LVR));
110}
111
112
113
114
115static inline int lapic_is_integrated(void)
116{
117 return APIC_INTEGRATED(lapic_get_version());
118}
119
120
121
122
123static int modern_apic(void)
124{
125
126 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
127 boot_cpu_data.x86 >= 0xf)
128 return 1;
129 return lapic_get_version() >= 0x14;
130}
131
132void apic_wait_icr_idle(void)
133{
134 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
135 cpu_relax();
136}
137
138unsigned long safe_apic_wait_icr_idle(void)
139{
140 unsigned long send_status;
141 int timeout;
142
143 timeout = 0;
144 do {
145 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
146 if (!send_status)
147 break;
148 udelay(100);
149 } while (timeout++ < 1000);
150
151 return send_status;
152}
153
154
155
156
157void enable_NMI_through_LVT0 (void * dummy)
158{
159 unsigned int v = APIC_DM_NMI;
160
161
162 if (!lapic_is_integrated())
163 v |= APIC_LVT_LEVEL_TRIGGER;
164 apic_write_around(APIC_LVT0, v);
165}
166
167
168
169
170int get_physical_broadcast(void)
171{
172 return modern_apic() ? 0xff : 0xf;
173}
174
175
176
177
178int lapic_get_maxlvt(void)
179{
180 unsigned int v = apic_read(APIC_LVR);
181
182
183 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
184}
185
186
187
188
189
190
191#define APIC_DIVISOR 16
192
193
194
195
196
197
198
199
200
201
202
203static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
204{
205 unsigned int lvtt_value, tmp_value;
206
207 lvtt_value = LOCAL_TIMER_VECTOR;
208 if (!oneshot)
209 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
210 if (!lapic_is_integrated())
211 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
212
213 if (!irqen)
214 lvtt_value |= APIC_LVT_MASKED;
215
216 apic_write_around(APIC_LVTT, lvtt_value);
217
218
219
220
221 tmp_value = apic_read(APIC_TDCR);
222 apic_write_around(APIC_TDCR, (tmp_value
223 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
224 | APIC_TDR_DIV_16);
225
226 if (!oneshot)
227 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
228}
229
230
231
232
233static int lapic_next_event(unsigned long delta,
234 struct clock_event_device *evt)
235{
236 apic_write_around(APIC_TMICT, delta);
237 return 0;
238}
239
240
241
242
243static void lapic_timer_setup(enum clock_event_mode mode,
244 struct clock_event_device *evt)
245{
246 unsigned long flags;
247 unsigned int v;
248
249
250 if (!local_apic_timer_verify_ok)
251 return;
252
253 local_irq_save(flags);
254
255 switch (mode) {
256 case CLOCK_EVT_MODE_PERIODIC:
257 case CLOCK_EVT_MODE_ONESHOT:
258 __setup_APIC_LVTT(calibration_result,
259 mode != CLOCK_EVT_MODE_PERIODIC, 1);
260 break;
261 case CLOCK_EVT_MODE_UNUSED:
262 case CLOCK_EVT_MODE_SHUTDOWN:
263 v = apic_read(APIC_LVTT);
264 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
265 apic_write_around(APIC_LVTT, v);
266 break;
267 }
268
269 local_irq_restore(flags);
270}
271
272
273
274
275static void lapic_timer_broadcast(cpumask_t mask)
276{
277#ifdef CONFIG_SMP
278 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
279#endif
280}
281
282
283
284
285
286static void __devinit setup_APIC_timer(void)
287{
288 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
289
290 memcpy(levt, &lapic_clockevent, sizeof(*levt));
291 levt->cpumask = cpumask_of_cpu(smp_processor_id());
292
293 clockevents_register_device(levt);
294}
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317#define LAPIC_CAL_LOOPS (HZ/10)
318
319static __initdata volatile int lapic_cal_loops = -1;
320static __initdata long lapic_cal_t1, lapic_cal_t2;
321static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
322static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
323static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
324
325
326
327
328static void __init lapic_cal_handler(struct clock_event_device *dev)
329{
330 unsigned long long tsc = 0;
331 long tapic = apic_read(APIC_TMCCT);
332 unsigned long pm = acpi_pm_read_early();
333
334 if (cpu_has_tsc)
335 rdtscll(tsc);
336
337 switch (lapic_cal_loops++) {
338 case 0:
339 lapic_cal_t1 = tapic;
340 lapic_cal_tsc1 = tsc;
341 lapic_cal_pm1 = pm;
342 lapic_cal_j1 = jiffies;
343 break;
344
345 case LAPIC_CAL_LOOPS:
346 lapic_cal_t2 = tapic;
347 lapic_cal_tsc2 = tsc;
348 if (pm < lapic_cal_pm1)
349 pm += ACPI_PM_OVRRUN;
350 lapic_cal_pm2 = pm;
351 lapic_cal_j2 = jiffies;
352 break;
353 }
354}
355
356
357
358
359
360
361void __init setup_boot_APIC_clock(void)
362{
363 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
364 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
365 const long pm_thresh = pm_100ms/100;
366 void (*real_handler)(struct clock_event_device *dev);
367 unsigned long deltaj;
368 long delta, deltapm;
369 int pm_referenced = 0;
370
371
372
373
374
375
376
377 if (local_apic_timer_disabled) {
378
379 if (num_possible_cpus() > 1)
380 setup_APIC_timer();
381 return;
382 }
383
384 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
385 "calibrating APIC timer ...\n");
386
387 local_irq_disable();
388
389
390 real_handler = global_clock_event->event_handler;
391 global_clock_event->event_handler = lapic_cal_handler;
392
393
394
395
396
397 __setup_APIC_LVTT(1000000000, 0, 0);
398
399
400 local_irq_enable();
401
402 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
403 cpu_relax();
404
405 local_irq_disable();
406
407
408 global_clock_event->event_handler = real_handler;
409
410
411 delta = lapic_cal_t1 - lapic_cal_t2;
412 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
413
414
415 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
416 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
417
418 if (deltapm) {
419 unsigned long mult;
420 u64 res;
421
422 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
423
424 if (deltapm > (pm_100ms - pm_thresh) &&
425 deltapm < (pm_100ms + pm_thresh)) {
426 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
427 } else {
428 res = (((u64) deltapm) * mult) >> 22;
429 do_div(res, 1000000);
430 printk(KERN_WARNING "APIC calibration not consistent "
431 "with PM Timer: %ldms instead of 100ms\n",
432 (long)res);
433
434 res = (((u64) delta ) * pm_100ms);
435 do_div(res, deltapm);
436 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
437 "%lu (%ld)\n", (unsigned long) res, delta);
438 delta = (long) res;
439 }
440 pm_referenced = 1;
441 }
442
443
444 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
445 lapic_clockevent.max_delta_ns =
446 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
447 lapic_clockevent.min_delta_ns =
448 clockevent_delta2ns(0xF, &lapic_clockevent);
449
450 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
451
452 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
453 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
454 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
455 calibration_result);
456
457 if (cpu_has_tsc) {
458 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
459 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
460 "%ld.%04ld MHz.\n",
461 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
462 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
463 }
464
465 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
466 "%u.%04u MHz.\n",
467 calibration_result / (1000000 / HZ),
468 calibration_result % (1000000 / HZ));
469
470 local_apic_timer_verify_ok = 1;
471
472
473 if (!pm_referenced) {
474 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
475
476
477
478
479 levt->event_handler = lapic_cal_handler;
480 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
481 lapic_cal_loops = -1;
482
483
484 local_irq_enable();
485
486 while(lapic_cal_loops <= LAPIC_CAL_LOOPS)
487 cpu_relax();
488
489 local_irq_disable();
490
491
492 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
493
494 local_irq_enable();
495
496
497 deltaj = lapic_cal_j2 - lapic_cal_j1;
498 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
499
500
501 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
502 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
503 else
504 local_apic_timer_verify_ok = 0;
505 } else
506 local_irq_enable();
507
508 if (!local_apic_timer_verify_ok) {
509 printk(KERN_WARNING
510 "APIC timer disabled due to verification failure.\n");
511
512 if (num_possible_cpus() == 1)
513 return;
514 } else {
515
516
517
518
519
520 if (nmi_watchdog != NMI_IO_APIC)
521 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
522 }
523
524
525 setup_APIC_timer();
526}
527
528void __devinit setup_secondary_APIC_clock(void)
529{
530 setup_APIC_timer();
531}
532
533
534
535
536static void local_apic_timer_interrupt(void)
537{
538 int cpu = smp_processor_id();
539 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
540
541
542
543
544
545
546
547
548
549
550
551
552 if (!evt->event_handler) {
553 printk(KERN_WARNING
554 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
555
556 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
557 return;
558 }
559
560 per_cpu(irq_stat, cpu).apic_timer_irqs++;
561
562 evt->event_handler(evt);
563}
564
565
566
567
568
569
570
571
572
573
574void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
575{
576 struct pt_regs *old_regs = set_irq_regs(regs);
577
578
579
580
581
582 ack_APIC_irq();
583
584
585
586
587
588 irq_enter();
589 local_apic_timer_interrupt();
590 irq_exit();
591
592 set_irq_regs(old_regs);
593}
594
595int setup_profiling_timer(unsigned int multiplier)
596{
597 return -EINVAL;
598}
599
600
601
602
603
604
605
606
607
608
609
610
611void clear_local_APIC(void)
612{
613 int maxlvt = lapic_get_maxlvt();
614 unsigned long v;
615
616
617
618
619
620 if (maxlvt >= 3) {
621 v = ERROR_APIC_VECTOR;
622 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
623 }
624
625
626
627
628 v = apic_read(APIC_LVTT);
629 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
630 v = apic_read(APIC_LVT0);
631 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
632 v = apic_read(APIC_LVT1);
633 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
634 if (maxlvt >= 4) {
635 v = apic_read(APIC_LVTPC);
636 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
637 }
638
639
640#ifdef CONFIG_X86_MCE_P4THERMAL
641 if (maxlvt >= 5) {
642 v = apic_read(APIC_LVTTHMR);
643 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
644 }
645#endif
646
647
648
649 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
650 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
651 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
652 if (maxlvt >= 3)
653 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
654 if (maxlvt >= 4)
655 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
656
657#ifdef CONFIG_X86_MCE_P4THERMAL
658 if (maxlvt >= 5)
659 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
660#endif
661
662 if (lapic_is_integrated()) {
663 if (maxlvt > 3)
664
665 apic_write(APIC_ESR, 0);
666 apic_read(APIC_ESR);
667 }
668}
669
670
671
672
673void disable_local_APIC(void)
674{
675 unsigned long value;
676
677 clear_local_APIC();
678
679
680
681
682
683 value = apic_read(APIC_SPIV);
684 value &= ~APIC_SPIV_APIC_ENABLED;
685 apic_write_around(APIC_SPIV, value);
686
687
688
689
690
691 if (enabled_via_apicbase) {
692 unsigned int l, h;
693
694 rdmsr(MSR_IA32_APICBASE, l, h);
695 l &= ~MSR_IA32_APICBASE_ENABLE;
696 wrmsr(MSR_IA32_APICBASE, l, h);
697 }
698}
699
700
701
702
703
704
705
706void lapic_shutdown(void)
707{
708 unsigned long flags;
709
710 if (!cpu_has_apic)
711 return;
712
713 local_irq_save(flags);
714 clear_local_APIC();
715
716 if (enabled_via_apicbase)
717 disable_local_APIC();
718
719 local_irq_restore(flags);
720}
721
722
723
724
725
726
727int __init verify_local_APIC(void)
728{
729 unsigned int reg0, reg1;
730
731
732
733
734 reg0 = apic_read(APIC_LVR);
735 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
736 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
737 reg1 = apic_read(APIC_LVR);
738 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
739
740
741
742
743
744
745 if (reg1 != reg0)
746 return 0;
747
748
749
750
751 reg1 = GET_APIC_VERSION(reg0);
752 if (reg1 == 0x00 || reg1 == 0xff)
753 return 0;
754 reg1 = lapic_get_maxlvt();
755 if (reg1 < 0x02 || reg1 == 0xff)
756 return 0;
757
758
759
760
761 reg0 = apic_read(APIC_ID);
762 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
763
764
765
766
767
768
769 reg0 = apic_read(APIC_LVT0);
770 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
771 reg1 = apic_read(APIC_LVT1);
772 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
773
774 return 1;
775}
776
777
778
779
780void __init sync_Arb_IDs(void)
781{
782
783
784
785
786 if (modern_apic())
787 return;
788
789
790
791 apic_wait_icr_idle();
792
793 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
794 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
795 | APIC_DM_INIT);
796}
797
798
799
800
801void __init init_bsp_APIC(void)
802{
803 unsigned long value;
804
805
806
807
808
809 if (smp_found_config || !cpu_has_apic)
810 return;
811
812
813
814
815 clear_local_APIC();
816
817
818
819
820 value = apic_read(APIC_SPIV);
821 value &= ~APIC_VECTOR_MASK;
822 value |= APIC_SPIV_APIC_ENABLED;
823
824
825 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
826 (boot_cpu_data.x86 == 15))
827 value &= ~APIC_SPIV_FOCUS_DISABLED;
828 else
829 value |= APIC_SPIV_FOCUS_DISABLED;
830 value |= SPURIOUS_APIC_VECTOR;
831 apic_write_around(APIC_SPIV, value);
832
833
834
835
836 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
837 value = APIC_DM_NMI;
838 if (!lapic_is_integrated())
839 value |= APIC_LVT_LEVEL_TRIGGER;
840 apic_write_around(APIC_LVT1, value);
841}
842
843
844
845
846void __devinit setup_local_APIC(void)
847{
848 unsigned long oldvalue, value, maxlvt, integrated;
849 int i, j;
850
851
852 if (esr_disable) {
853 apic_write(APIC_ESR, 0);
854 apic_write(APIC_ESR, 0);
855 apic_write(APIC_ESR, 0);
856 apic_write(APIC_ESR, 0);
857 }
858
859 integrated = lapic_is_integrated();
860
861
862
863
864 if (!apic_id_registered())
865 BUG();
866
867
868
869
870
871
872 init_apic_ldr();
873
874
875
876
877
878 value = apic_read(APIC_TASKPRI);
879 value &= ~APIC_TPRI_MASK;
880 apic_write_around(APIC_TASKPRI, value);
881
882
883
884
885
886
887
888
889
890
891
892
893 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
894 value = apic_read(APIC_ISR + i*0x10);
895 for (j = 31; j >= 0; j--) {
896 if (value & (1<<j))
897 ack_APIC_irq();
898 }
899 }
900
901
902
903
904 value = apic_read(APIC_SPIV);
905 value &= ~APIC_VECTOR_MASK;
906
907
908
909 value |= APIC_SPIV_APIC_ENABLED;
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932 value &= ~APIC_SPIV_FOCUS_DISABLED;
933
934
935
936
937 value |= SPURIOUS_APIC_VECTOR;
938 apic_write_around(APIC_SPIV, value);
939
940
941
942
943
944
945
946
947
948
949
950 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
951 if (!smp_processor_id() && (pic_mode || !value)) {
952 value = APIC_DM_EXTINT;
953 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
954 smp_processor_id());
955 } else {
956 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
957 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
958 smp_processor_id());
959 }
960 apic_write_around(APIC_LVT0, value);
961
962
963
964
965 if (!smp_processor_id())
966 value = APIC_DM_NMI;
967 else
968 value = APIC_DM_NMI | APIC_LVT_MASKED;
969 if (!integrated)
970 value |= APIC_LVT_LEVEL_TRIGGER;
971 apic_write_around(APIC_LVT1, value);
972
973 if (integrated && !esr_disable) {
974 maxlvt = lapic_get_maxlvt();
975 if (maxlvt > 3)
976 apic_write(APIC_ESR, 0);
977 oldvalue = apic_read(APIC_ESR);
978
979
980 value = ERROR_APIC_VECTOR;
981 apic_write_around(APIC_LVTERR, value);
982
983
984
985 if (maxlvt > 3)
986 apic_write(APIC_ESR, 0);
987 value = apic_read(APIC_ESR);
988 if (value != oldvalue)
989 apic_printk(APIC_VERBOSE, "ESR value before enabling "
990 "vector: 0x%08lx after: 0x%08lx\n",
991 oldvalue, value);
992 } else {
993 if (esr_disable)
994
995
996
997
998
999
1000 printk(KERN_INFO "Leaving ESR disabled.\n");
1001 else
1002 printk(KERN_INFO "No ESR for 82489DX.\n");
1003 }
1004
1005
1006 value = apic_read(APIC_LVTT);
1007 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1008 apic_write_around(APIC_LVTT, value);
1009
1010 setup_apic_nmi_watchdog(NULL);
1011 apic_pm_activate();
1012}
1013
1014
1015
1016
1017static int __init detect_init_APIC (void)
1018{
1019 u32 h, l, features;
1020
1021
1022 if (enable_local_apic < 0)
1023 return -1;
1024
1025 switch (boot_cpu_data.x86_vendor) {
1026 case X86_VENDOR_AMD:
1027 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1028 (boot_cpu_data.x86 == 15))
1029 break;
1030 goto no_apic;
1031 case X86_VENDOR_INTEL:
1032 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1033 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1034 break;
1035 goto no_apic;
1036 default:
1037 goto no_apic;
1038 }
1039
1040 if (!cpu_has_apic) {
1041
1042
1043
1044
1045 if (enable_local_apic <= 0) {
1046 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1047 "you can enable it with \"lapic\"\n");
1048 return -1;
1049 }
1050
1051
1052
1053
1054
1055 rdmsr(MSR_IA32_APICBASE, l, h);
1056 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1057 printk(KERN_INFO
1058 "Local APIC disabled by BIOS -- reenabling.\n");
1059 l &= ~MSR_IA32_APICBASE_BASE;
1060 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1061 wrmsr(MSR_IA32_APICBASE, l, h);
1062 enabled_via_apicbase = 1;
1063 }
1064 }
1065
1066
1067
1068
1069 features = cpuid_edx(1);
1070 if (!(features & (1 << X86_FEATURE_APIC))) {
1071 printk(KERN_WARNING "Could not enable APIC!\n");
1072 return -1;
1073 }
1074 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1075 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1076
1077
1078 rdmsr(MSR_IA32_APICBASE, l, h);
1079 if (l & MSR_IA32_APICBASE_ENABLE)
1080 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1081
1082 if (nmi_watchdog != NMI_NONE)
1083 nmi_watchdog = NMI_LOCAL_APIC;
1084
1085 printk(KERN_INFO "Found and enabled local APIC!\n");
1086
1087 apic_pm_activate();
1088
1089 return 0;
1090
1091no_apic:
1092 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1093 return -1;
1094}
1095
1096
1097
1098
1099void __init init_apic_mappings(void)
1100{
1101 unsigned long apic_phys;
1102
1103
1104
1105
1106
1107
1108 if (!smp_found_config && detect_init_APIC()) {
1109 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1110 apic_phys = __pa(apic_phys);
1111 } else
1112 apic_phys = mp_lapic_addr;
1113
1114 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1115 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1116 apic_phys);
1117
1118
1119
1120
1121
1122 if (boot_cpu_physical_apicid == -1U)
1123 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1124
1125#ifdef CONFIG_X86_IO_APIC
1126 {
1127 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1128 int i;
1129
1130 for (i = 0; i < nr_ioapics; i++) {
1131 if (smp_found_config) {
1132 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1133 if (!ioapic_phys) {
1134 printk(KERN_ERR
1135 "WARNING: bogus zero IO-APIC "
1136 "address found in MPTABLE, "
1137 "disabling IO/APIC support!\n");
1138 smp_found_config = 0;
1139 skip_ioapic_setup = 1;
1140 goto fake_ioapic_page;
1141 }
1142 } else {
1143fake_ioapic_page:
1144 ioapic_phys = (unsigned long)
1145 alloc_bootmem_pages(PAGE_SIZE);
1146 ioapic_phys = __pa(ioapic_phys);
1147 }
1148 set_fixmap_nocache(idx, ioapic_phys);
1149 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1150 __fix_to_virt(idx), ioapic_phys);
1151 idx++;
1152 }
1153 }
1154#endif
1155}
1156
1157
1158
1159
1160
1161int __init APIC_init_uniprocessor (void)
1162{
1163 if (enable_local_apic < 0)
1164 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1165
1166 if (!smp_found_config && !cpu_has_apic)
1167 return -1;
1168
1169
1170
1171
1172 if (!cpu_has_apic &&
1173 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1174 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1175 boot_cpu_physical_apicid);
1176 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1177 return -1;
1178 }
1179
1180 verify_local_APIC();
1181
1182 connect_bsp_APIC();
1183
1184
1185
1186
1187
1188
1189#ifdef CONFIG_CRASH_DUMP
1190 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1191#endif
1192 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1193
1194 setup_local_APIC();
1195
1196#ifdef CONFIG_X86_IO_APIC
1197 if (smp_found_config)
1198 if (!skip_ioapic_setup && nr_ioapics)
1199 setup_IO_APIC();
1200#endif
1201 setup_boot_clock();
1202
1203 return 0;
1204}
1205
1206
1207
1208
1209static int __init parse_lapic(char *arg)
1210{
1211 enable_local_apic = 1;
1212 return 0;
1213}
1214early_param("lapic", parse_lapic);
1215
1216static int __init parse_nolapic(char *arg)
1217{
1218 enable_local_apic = -1;
1219 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1220 return 0;
1221}
1222early_param("nolapic", parse_nolapic);
1223
1224static int __init parse_disable_lapic_timer(char *arg)
1225{
1226 local_apic_timer_disabled = 1;
1227 return 0;
1228}
1229early_param("nolapic_timer", parse_disable_lapic_timer);
1230
1231static int __init parse_lapic_timer_c2_ok(char *arg)
1232{
1233 local_apic_timer_c2_ok = 1;
1234 return 0;
1235}
1236early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1237
1238static int __init apic_set_verbosity(char *str)
1239{
1240 if (strcmp("debug", str) == 0)
1241 apic_verbosity = APIC_DEBUG;
1242 else if (strcmp("verbose", str) == 0)
1243 apic_verbosity = APIC_VERBOSE;
1244 return 1;
1245}
1246
1247__setup("apic=", apic_set_verbosity);
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257void smp_spurious_interrupt(struct pt_regs *regs)
1258{
1259 unsigned long v;
1260
1261 irq_enter();
1262
1263
1264
1265
1266
1267 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1268 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1269 ack_APIC_irq();
1270
1271
1272 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1273 "should never happen.\n", smp_processor_id());
1274 irq_exit();
1275}
1276
1277
1278
1279
1280void smp_error_interrupt(struct pt_regs *regs)
1281{
1282 unsigned long v, v1;
1283
1284 irq_enter();
1285
1286 v = apic_read(APIC_ESR);
1287 apic_write(APIC_ESR, 0);
1288 v1 = apic_read(APIC_ESR);
1289 ack_APIC_irq();
1290 atomic_inc(&irq_err_count);
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1303 smp_processor_id(), v , v1);
1304 irq_exit();
1305}
1306
1307
1308
1309
1310void __init apic_intr_init(void)
1311{
1312#ifdef CONFIG_SMP
1313 smp_intr_init();
1314#endif
1315
1316 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1317
1318
1319 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1320 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1321
1322
1323#ifdef CONFIG_X86_MCE_P4THERMAL
1324 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1325#endif
1326}
1327
1328
1329
1330
1331void __init connect_bsp_APIC(void)
1332{
1333 if (pic_mode) {
1334
1335
1336
1337 clear_local_APIC();
1338
1339
1340
1341
1342 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1343 "enabling APIC mode.\n");
1344 outb(0x70, 0x22);
1345 outb(0x01, 0x23);
1346 }
1347 enable_apic_mode();
1348}
1349
1350
1351
1352
1353
1354
1355
1356
1357void disconnect_bsp_APIC(int virt_wire_setup)
1358{
1359 if (pic_mode) {
1360
1361
1362
1363
1364
1365
1366 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1367 "entering PIC mode.\n");
1368 outb(0x70, 0x22);
1369 outb(0x00, 0x23);
1370 } else {
1371
1372 unsigned long value;
1373
1374
1375 value = apic_read(APIC_SPIV);
1376 value &= ~APIC_VECTOR_MASK;
1377 value |= APIC_SPIV_APIC_ENABLED;
1378 value |= 0xf;
1379 apic_write_around(APIC_SPIV, value);
1380
1381 if (!virt_wire_setup) {
1382
1383
1384
1385
1386 value = apic_read(APIC_LVT0);
1387 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1388 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1389 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1390 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1391 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1392 apic_write_around(APIC_LVT0, value);
1393 } else {
1394
1395 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1396 }
1397
1398
1399
1400
1401
1402 value = apic_read(APIC_LVT1);
1403 value &= ~(
1404 APIC_MODE_MASK | APIC_SEND_PENDING |
1405 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1406 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1407 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1408 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1409 apic_write_around(APIC_LVT1, value);
1410 }
1411}
1412
1413
1414
1415
1416#ifdef CONFIG_PM
1417
1418static struct {
1419 int active;
1420
1421 unsigned int apic_id;
1422 unsigned int apic_taskpri;
1423 unsigned int apic_ldr;
1424 unsigned int apic_dfr;
1425 unsigned int apic_spiv;
1426 unsigned int apic_lvtt;
1427 unsigned int apic_lvtpc;
1428 unsigned int apic_lvt0;
1429 unsigned int apic_lvt1;
1430 unsigned int apic_lvterr;
1431 unsigned int apic_tmict;
1432 unsigned int apic_tdcr;
1433 unsigned int apic_thmr;
1434} apic_pm_state;
1435
1436static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1437{
1438 unsigned long flags;
1439 int maxlvt;
1440
1441 if (!apic_pm_state.active)
1442 return 0;
1443
1444 maxlvt = lapic_get_maxlvt();
1445
1446 apic_pm_state.apic_id = apic_read(APIC_ID);
1447 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1448 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1449 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1450 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1451 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1452 if (maxlvt >= 4)
1453 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1454 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1455 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1456 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1457 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1458 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1459#ifdef CONFIG_X86_MCE_P4THERMAL
1460 if (maxlvt >= 5)
1461 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1462#endif
1463
1464 local_irq_save(flags);
1465 disable_local_APIC();
1466 local_irq_restore(flags);
1467 return 0;
1468}
1469
1470static int lapic_resume(struct sys_device *dev)
1471{
1472 unsigned int l, h;
1473 unsigned long flags;
1474 int maxlvt;
1475
1476 if (!apic_pm_state.active)
1477 return 0;
1478
1479 maxlvt = lapic_get_maxlvt();
1480
1481 local_irq_save(flags);
1482
1483
1484
1485
1486
1487
1488
1489 rdmsr(MSR_IA32_APICBASE, l, h);
1490 l &= ~MSR_IA32_APICBASE_BASE;
1491 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1492 wrmsr(MSR_IA32_APICBASE, l, h);
1493
1494 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1495 apic_write(APIC_ID, apic_pm_state.apic_id);
1496 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1497 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1498 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1499 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1500 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1501 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1502#ifdef CONFIG_X86_MCE_P4THERMAL
1503 if (maxlvt >= 5)
1504 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1505#endif
1506 if (maxlvt >= 4)
1507 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1508 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1509 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1510 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1511 apic_write(APIC_ESR, 0);
1512 apic_read(APIC_ESR);
1513 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1514 apic_write(APIC_ESR, 0);
1515 apic_read(APIC_ESR);
1516 local_irq_restore(flags);
1517 return 0;
1518}
1519
1520
1521
1522
1523
1524
1525static struct sysdev_class lapic_sysclass = {
1526 set_kset_name("lapic"),
1527 .resume = lapic_resume,
1528 .suspend = lapic_suspend,
1529};
1530
1531static struct sys_device device_lapic = {
1532 .id = 0,
1533 .cls = &lapic_sysclass,
1534};
1535
1536static void __devinit apic_pm_activate(void)
1537{
1538 apic_pm_state.active = 1;
1539}
1540
1541static int __init init_lapic_sysfs(void)
1542{
1543 int error;
1544
1545 if (!cpu_has_apic)
1546 return 0;
1547
1548
1549 error = sysdev_class_register(&lapic_sysclass);
1550 if (!error)
1551 error = sysdev_register(&device_lapic);
1552 return error;
1553}
1554device_initcall(init_lapic_sysfs);
1555
1556#else
1557
1558static void apic_pm_activate(void) { }
1559
1560#endif
1561