x86: add hard_smp_prossor_id with MACRO in io_apic_xx.c
[deliverable/linux.git] / arch / x86 / kernel / apic_32.c
1 /*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
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 /*
47 * Sanity check
48 */
49 #if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
50 # error SPURIOUS_APIC_VECTOR definition error
51 #endif
52
53 #ifdef CONFIG_X86_32
54 /*
55 * Knob to control our willingness to enable the local APIC.
56 *
57 * +1=force-enable
58 */
59 static int force_enable_local_apic;
60 /*
61 * APIC command line parameters
62 */
63 static int __init parse_lapic(char *arg)
64 {
65 force_enable_local_apic = 1;
66 return 0;
67 }
68 early_param("lapic", parse_lapic);
69 #endif
70
71 #ifdef CONFIG_X86_64
72 static int apic_calibrate_pmtmr __initdata;
73 static __init int setup_apicpmtimer(char *s)
74 {
75 apic_calibrate_pmtmr = 1;
76 notsc_setup(NULL);
77 return 0;
78 }
79 __setup("apicpmtimer", setup_apicpmtimer);
80 #endif
81
82 unsigned long mp_lapic_addr;
83 int disable_apic;
84 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
85 static int disable_apic_timer __cpuinitdata;
86 /* Local APIC timer works in C2 */
87 int local_apic_timer_c2_ok;
88 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
89
90 int first_system_vector = 0xfe;
91
92 char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
93
94 /*
95 * Debug level, exported for io_apic.c
96 */
97 unsigned int apic_verbosity;
98
99 int pic_mode;
100
101 /* Have we found an MP table */
102 int smp_found_config;
103
104 static struct resource lapic_resource = {
105 .name = "Local APIC",
106 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
107 };
108
109 static unsigned int calibration_result;
110
111 static int lapic_next_event(unsigned long delta,
112 struct clock_event_device *evt);
113 static void lapic_timer_setup(enum clock_event_mode mode,
114 struct clock_event_device *evt);
115 static void lapic_timer_broadcast(cpumask_t mask);
116 static void apic_pm_activate(void);
117
118 /*
119 * The local apic timer can be used for any function which is CPU local.
120 */
121 static struct clock_event_device lapic_clockevent = {
122 .name = "lapic",
123 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
124 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
125 .shift = 32,
126 .set_mode = lapic_timer_setup,
127 .set_next_event = lapic_next_event,
128 .broadcast = lapic_timer_broadcast,
129 .rating = 100,
130 .irq = -1,
131 };
132 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
133
134 /* Local APIC was disabled by the BIOS and enabled by the kernel */
135 static int enabled_via_apicbase;
136
137 static unsigned long apic_phys;
138
139 /*
140 * Get the LAPIC version
141 */
142 static inline int lapic_get_version(void)
143 {
144 return GET_APIC_VERSION(apic_read(APIC_LVR));
145 }
146
147 /*
148 * Check, if the APIC is integrated or a separate chip
149 */
150 static inline int lapic_is_integrated(void)
151 {
152 #ifdef CONFIG_X86_64
153 return 1;
154 #else
155 return APIC_INTEGRATED(lapic_get_version());
156 #endif
157 }
158
159 /*
160 * Check, whether this is a modern or a first generation APIC
161 */
162 static int modern_apic(void)
163 {
164 /* AMD systems use old APIC versions, so check the CPU */
165 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
166 boot_cpu_data.x86 >= 0xf)
167 return 1;
168 return lapic_get_version() >= 0x14;
169 }
170
171 /*
172 * Paravirt kernels also might be using these below ops. So we still
173 * use generic apic_read()/apic_write(), which might be pointing to different
174 * ops in PARAVIRT case.
175 */
176 void xapic_wait_icr_idle(void)
177 {
178 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
179 cpu_relax();
180 }
181
182 u32 safe_xapic_wait_icr_idle(void)
183 {
184 u32 send_status;
185 int timeout;
186
187 timeout = 0;
188 do {
189 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
190 if (!send_status)
191 break;
192 udelay(100);
193 } while (timeout++ < 1000);
194
195 return send_status;
196 }
197
198 void xapic_icr_write(u32 low, u32 id)
199 {
200 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
201 apic_write(APIC_ICR, low);
202 }
203
204 u64 xapic_icr_read(void)
205 {
206 u32 icr1, icr2;
207
208 icr2 = apic_read(APIC_ICR2);
209 icr1 = apic_read(APIC_ICR);
210
211 return icr1 | ((u64)icr2 << 32);
212 }
213
214 static struct apic_ops xapic_ops = {
215 .read = native_apic_mem_read,
216 .write = native_apic_mem_write,
217 .icr_read = xapic_icr_read,
218 .icr_write = xapic_icr_write,
219 .wait_icr_idle = xapic_wait_icr_idle,
220 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
221 };
222
223 struct apic_ops __read_mostly *apic_ops = &xapic_ops;
224 EXPORT_SYMBOL_GPL(apic_ops);
225
226 /**
227 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
228 */
229 void __cpuinit enable_NMI_through_LVT0(void)
230 {
231 unsigned int v;
232
233 /* unmask and set to NMI */
234 v = APIC_DM_NMI;
235
236 /* Level triggered for 82489DX (32bit mode) */
237 if (!lapic_is_integrated())
238 v |= APIC_LVT_LEVEL_TRIGGER;
239
240 apic_write(APIC_LVT0, v);
241 }
242
243 /**
244 * get_physical_broadcast - Get number of physical broadcast IDs
245 */
246 int get_physical_broadcast(void)
247 {
248 return modern_apic() ? 0xff : 0xf;
249 }
250
251 /**
252 * lapic_get_maxlvt - get the maximum number of local vector table entries
253 */
254 int lapic_get_maxlvt(void)
255 {
256 unsigned int v;
257
258 v = apic_read(APIC_LVR);
259 /*
260 * - we always have APIC integrated on 64bit mode
261 * - 82489DXs do not report # of LVT entries
262 */
263 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
264 }
265
266 /*
267 * Local APIC timer
268 */
269
270 /* Clock divisor */
271 #ifdef CONFG_X86_64
272 #define APIC_DIVISOR 1
273 #else
274 #define APIC_DIVISOR 16
275 #endif
276
277 /*
278 * This function sets up the local APIC timer, with a timeout of
279 * 'clocks' APIC bus clock. During calibration we actually call
280 * this function twice on the boot CPU, once with a bogus timeout
281 * value, second time for real. The other (noncalibrating) CPUs
282 * call this function only once, with the real, calibrated value.
283 *
284 * We do reads before writes even if unnecessary, to get around the
285 * P5 APIC double write bug.
286 */
287 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
288 {
289 unsigned int lvtt_value, tmp_value;
290
291 lvtt_value = LOCAL_TIMER_VECTOR;
292 if (!oneshot)
293 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
294 if (!lapic_is_integrated())
295 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
296
297 if (!irqen)
298 lvtt_value |= APIC_LVT_MASKED;
299
300 apic_write(APIC_LVTT, lvtt_value);
301
302 /*
303 * Divide PICLK by 16
304 */
305 tmp_value = apic_read(APIC_TDCR);
306 apic_write(APIC_TDCR,
307 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
308 APIC_TDR_DIV_16);
309
310 if (!oneshot)
311 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
312 }
313
314 /*
315 * Setup extended LVT, AMD specific (K8, family 10h)
316 *
317 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
318 * MCE interrupts are supported. Thus MCE offset must be set to 0.
319 *
320 * If mask=1, the LVT entry does not generate interrupts while mask=0
321 * enables the vector. See also the BKDGs.
322 */
323
324 #define APIC_EILVT_LVTOFF_MCE 0
325 #define APIC_EILVT_LVTOFF_IBS 1
326
327 static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
328 {
329 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
330 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
331
332 apic_write(reg, v);
333 }
334
335 u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
336 {
337 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
338 return APIC_EILVT_LVTOFF_MCE;
339 }
340
341 u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
342 {
343 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
344 return APIC_EILVT_LVTOFF_IBS;
345 }
346 EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
347
348 /*
349 * Program the next event, relative to now
350 */
351 static int lapic_next_event(unsigned long delta,
352 struct clock_event_device *evt)
353 {
354 apic_write(APIC_TMICT, delta);
355 return 0;
356 }
357
358 /*
359 * Setup the lapic timer in periodic or oneshot mode
360 */
361 static void lapic_timer_setup(enum clock_event_mode mode,
362 struct clock_event_device *evt)
363 {
364 unsigned long flags;
365 unsigned int v;
366
367 /* Lapic used as dummy for broadcast ? */
368 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
369 return;
370
371 local_irq_save(flags);
372
373 switch (mode) {
374 case CLOCK_EVT_MODE_PERIODIC:
375 case CLOCK_EVT_MODE_ONESHOT:
376 __setup_APIC_LVTT(calibration_result,
377 mode != CLOCK_EVT_MODE_PERIODIC, 1);
378 break;
379 case CLOCK_EVT_MODE_UNUSED:
380 case CLOCK_EVT_MODE_SHUTDOWN:
381 v = apic_read(APIC_LVTT);
382 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
383 apic_write(APIC_LVTT, v);
384 break;
385 case CLOCK_EVT_MODE_RESUME:
386 /* Nothing to do here */
387 break;
388 }
389
390 local_irq_restore(flags);
391 }
392
393 /*
394 * Local APIC timer broadcast function
395 */
396 static void lapic_timer_broadcast(cpumask_t mask)
397 {
398 #ifdef CONFIG_SMP
399 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
400 #endif
401 }
402
403 /*
404 * Setup the local APIC timer for this CPU. Copy the initilized values
405 * of the boot CPU and register the clock event in the framework.
406 */
407 static void __cpuinit setup_APIC_timer(void)
408 {
409 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
410
411 memcpy(levt, &lapic_clockevent, sizeof(*levt));
412 levt->cpumask = cpumask_of_cpu(smp_processor_id());
413
414 clockevents_register_device(levt);
415 }
416
417 /*
418 * In this functions we calibrate APIC bus clocks to the external timer.
419 *
420 * We want to do the calibration only once since we want to have local timer
421 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
422 * frequency.
423 *
424 * This was previously done by reading the PIT/HPET and waiting for a wrap
425 * around to find out, that a tick has elapsed. I have a box, where the PIT
426 * readout is broken, so it never gets out of the wait loop again. This was
427 * also reported by others.
428 *
429 * Monitoring the jiffies value is inaccurate and the clockevents
430 * infrastructure allows us to do a simple substitution of the interrupt
431 * handler.
432 *
433 * The calibration routine also uses the pm_timer when possible, as the PIT
434 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
435 * back to normal later in the boot process).
436 */
437
438 #define LAPIC_CAL_LOOPS (HZ/10)
439
440 static __initdata int lapic_cal_loops = -1;
441 static __initdata long lapic_cal_t1, lapic_cal_t2;
442 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
443 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
444 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
445
446 /*
447 * Temporary interrupt handler.
448 */
449 static void __init lapic_cal_handler(struct clock_event_device *dev)
450 {
451 unsigned long long tsc = 0;
452 long tapic = apic_read(APIC_TMCCT);
453 unsigned long pm = acpi_pm_read_early();
454
455 if (cpu_has_tsc)
456 rdtscll(tsc);
457
458 switch (lapic_cal_loops++) {
459 case 0:
460 lapic_cal_t1 = tapic;
461 lapic_cal_tsc1 = tsc;
462 lapic_cal_pm1 = pm;
463 lapic_cal_j1 = jiffies;
464 break;
465
466 case LAPIC_CAL_LOOPS:
467 lapic_cal_t2 = tapic;
468 lapic_cal_tsc2 = tsc;
469 if (pm < lapic_cal_pm1)
470 pm += ACPI_PM_OVRRUN;
471 lapic_cal_pm2 = pm;
472 lapic_cal_j2 = jiffies;
473 break;
474 }
475 }
476
477 static int __init calibrate_APIC_clock(void)
478 {
479 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
480 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
481 const long pm_thresh = pm_100ms/100;
482 void (*real_handler)(struct clock_event_device *dev);
483 unsigned long deltaj;
484 long delta, deltapm;
485 int pm_referenced = 0;
486
487 local_irq_disable();
488
489 /* Replace the global interrupt handler */
490 real_handler = global_clock_event->event_handler;
491 global_clock_event->event_handler = lapic_cal_handler;
492
493 /*
494 * Setup the APIC counter to 1e9. There is no way the lapic
495 * can underflow in the 100ms detection time frame
496 */
497 __setup_APIC_LVTT(1000000000, 0, 0);
498
499 /* Let the interrupts run */
500 local_irq_enable();
501
502 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
503 cpu_relax();
504
505 local_irq_disable();
506
507 /* Restore the real event handler */
508 global_clock_event->event_handler = real_handler;
509
510 /* Build delta t1-t2 as apic timer counts down */
511 delta = lapic_cal_t1 - lapic_cal_t2;
512 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
513
514 /* Check, if the PM timer is available */
515 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
516 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
517
518 if (deltapm) {
519 unsigned long mult;
520 u64 res;
521
522 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
523
524 if (deltapm > (pm_100ms - pm_thresh) &&
525 deltapm < (pm_100ms + pm_thresh)) {
526 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
527 } else {
528 res = (((u64) deltapm) * mult) >> 22;
529 do_div(res, 1000000);
530 printk(KERN_WARNING "APIC calibration not consistent "
531 "with PM Timer: %ldms instead of 100ms\n",
532 (long)res);
533 /* Correct the lapic counter value */
534 res = (((u64) delta) * pm_100ms);
535 do_div(res, deltapm);
536 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
537 "%lu (%ld)\n", (unsigned long) res, delta);
538 delta = (long) res;
539 }
540 pm_referenced = 1;
541 }
542
543 /* Calculate the scaled math multiplication factor */
544 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
545 lapic_clockevent.shift);
546 lapic_clockevent.max_delta_ns =
547 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
548 lapic_clockevent.min_delta_ns =
549 clockevent_delta2ns(0xF, &lapic_clockevent);
550
551 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
552
553 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
554 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
555 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
556 calibration_result);
557
558 if (cpu_has_tsc) {
559 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
560 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
561 "%ld.%04ld MHz.\n",
562 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
563 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
564 }
565
566 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
567 "%u.%04u MHz.\n",
568 calibration_result / (1000000 / HZ),
569 calibration_result % (1000000 / HZ));
570
571 /*
572 * Do a sanity check on the APIC calibration result
573 */
574 if (calibration_result < (1000000 / HZ)) {
575 local_irq_enable();
576 printk(KERN_WARNING
577 "APIC frequency too slow, disabling apic timer\n");
578 return -1;
579 }
580
581 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
582
583 /* We trust the pm timer based calibration */
584 if (!pm_referenced) {
585 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
586
587 /*
588 * Setup the apic timer manually
589 */
590 levt->event_handler = lapic_cal_handler;
591 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
592 lapic_cal_loops = -1;
593
594 /* Let the interrupts run */
595 local_irq_enable();
596
597 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
598 cpu_relax();
599
600 local_irq_disable();
601
602 /* Stop the lapic timer */
603 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
604
605 local_irq_enable();
606
607 /* Jiffies delta */
608 deltaj = lapic_cal_j2 - lapic_cal_j1;
609 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
610
611 /* Check, if the jiffies result is consistent */
612 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
613 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
614 else
615 levt->features |= CLOCK_EVT_FEAT_DUMMY;
616 } else
617 local_irq_enable();
618
619 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
620 printk(KERN_WARNING
621 "APIC timer disabled due to verification failure.\n");
622 return -1;
623 }
624
625 return 0;
626 }
627
628 /*
629 * Setup the boot APIC
630 *
631 * Calibrate and verify the result.
632 */
633 void __init setup_boot_APIC_clock(void)
634 {
635 /*
636 * The local apic timer can be disabled via the kernel
637 * commandline or from the CPU detection code. Register the lapic
638 * timer as a dummy clock event source on SMP systems, so the
639 * broadcast mechanism is used. On UP systems simply ignore it.
640 */
641 if (disable_apic_timer) {
642 printk(KERN_INFO "Disabling APIC timer\n");
643 /* No broadcast on UP ! */
644 if (num_possible_cpus() > 1) {
645 lapic_clockevent.mult = 1;
646 setup_APIC_timer();
647 }
648 return;
649 }
650
651 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
652 "calibrating APIC timer ...\n");
653
654 if (calibrate_APIC_clock()) {
655 /* No broadcast on UP ! */
656 if (num_possible_cpus() > 1)
657 setup_APIC_timer();
658 return;
659 }
660
661 /*
662 * If nmi_watchdog is set to IO_APIC, we need the
663 * PIT/HPET going. Otherwise register lapic as a dummy
664 * device.
665 */
666 if (nmi_watchdog != NMI_IO_APIC)
667 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
668 else
669 printk(KERN_WARNING "APIC timer registered as dummy,"
670 " due to nmi_watchdog=%d!\n", nmi_watchdog);
671
672 /* Setup the lapic or request the broadcast */
673 setup_APIC_timer();
674 }
675
676 void __cpuinit setup_secondary_APIC_clock(void)
677 {
678 setup_APIC_timer();
679 }
680
681 /*
682 * The guts of the apic timer interrupt
683 */
684 static void local_apic_timer_interrupt(void)
685 {
686 int cpu = smp_processor_id();
687 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
688
689 /*
690 * Normally we should not be here till LAPIC has been initialized but
691 * in some cases like kdump, its possible that there is a pending LAPIC
692 * timer interrupt from previous kernel's context and is delivered in
693 * new kernel the moment interrupts are enabled.
694 *
695 * Interrupts are enabled early and LAPIC is setup much later, hence
696 * its possible that when we get here evt->event_handler is NULL.
697 * Check for event_handler being NULL and discard the interrupt as
698 * spurious.
699 */
700 if (!evt->event_handler) {
701 printk(KERN_WARNING
702 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
703 /* Switch it off */
704 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
705 return;
706 }
707
708 /*
709 * the NMI deadlock-detector uses this.
710 */
711 #ifdef CONFIG_X86_64
712 add_pda(apic_timer_irqs, 1);
713 #else
714 per_cpu(irq_stat, cpu).apic_timer_irqs++;
715 #endif
716
717 evt->event_handler(evt);
718 }
719
720 /*
721 * Local APIC timer interrupt. This is the most natural way for doing
722 * local interrupts, but local timer interrupts can be emulated by
723 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
724 *
725 * [ if a single-CPU system runs an SMP kernel then we call the local
726 * interrupt as well. Thus we cannot inline the local irq ... ]
727 */
728 void smp_apic_timer_interrupt(struct pt_regs *regs)
729 {
730 struct pt_regs *old_regs = set_irq_regs(regs);
731
732 /*
733 * NOTE! We'd better ACK the irq immediately,
734 * because timer handling can be slow.
735 */
736 ack_APIC_irq();
737 /*
738 * update_process_times() expects us to have done irq_enter().
739 * Besides, if we don't timer interrupts ignore the global
740 * interrupt lock, which is the WrongThing (tm) to do.
741 */
742 #ifdef CONFIG_X86_64
743 exit_idle();
744 #endif
745 irq_enter();
746 local_apic_timer_interrupt();
747 irq_exit();
748
749 set_irq_regs(old_regs);
750 }
751
752 int setup_profiling_timer(unsigned int multiplier)
753 {
754 return -EINVAL;
755 }
756
757 /*
758 * Local APIC start and shutdown
759 */
760
761 /**
762 * clear_local_APIC - shutdown the local APIC
763 *
764 * This is called, when a CPU is disabled and before rebooting, so the state of
765 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
766 * leftovers during boot.
767 */
768 void clear_local_APIC(void)
769 {
770 int maxlvt;
771 u32 v;
772
773 /* APIC hasn't been mapped yet */
774 if (!apic_phys)
775 return;
776
777 maxlvt = lapic_get_maxlvt();
778 /*
779 * Masking an LVT entry can trigger a local APIC error
780 * if the vector is zero. Mask LVTERR first to prevent this.
781 */
782 if (maxlvt >= 3) {
783 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
784 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
785 }
786 /*
787 * Careful: we have to set masks only first to deassert
788 * any level-triggered sources.
789 */
790 v = apic_read(APIC_LVTT);
791 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
792 v = apic_read(APIC_LVT0);
793 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
794 v = apic_read(APIC_LVT1);
795 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
796 if (maxlvt >= 4) {
797 v = apic_read(APIC_LVTPC);
798 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
799 }
800
801 /* lets not touch this if we didn't frob it */
802 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
803 if (maxlvt >= 5) {
804 v = apic_read(APIC_LVTTHMR);
805 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
806 }
807 #endif
808 /*
809 * Clean APIC state for other OSs:
810 */
811 apic_write(APIC_LVTT, APIC_LVT_MASKED);
812 apic_write(APIC_LVT0, APIC_LVT_MASKED);
813 apic_write(APIC_LVT1, APIC_LVT_MASKED);
814 if (maxlvt >= 3)
815 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
816 if (maxlvt >= 4)
817 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
818
819 /* Integrated APIC (!82489DX) ? */
820 if (lapic_is_integrated()) {
821 if (maxlvt > 3)
822 /* Clear ESR due to Pentium errata 3AP and 11AP */
823 apic_write(APIC_ESR, 0);
824 apic_read(APIC_ESR);
825 }
826 }
827
828 /**
829 * disable_local_APIC - clear and disable the local APIC
830 */
831 void disable_local_APIC(void)
832 {
833 unsigned int value;
834
835 clear_local_APIC();
836
837 /*
838 * Disable APIC (implies clearing of registers
839 * for 82489DX!).
840 */
841 value = apic_read(APIC_SPIV);
842 value &= ~APIC_SPIV_APIC_ENABLED;
843 apic_write(APIC_SPIV, value);
844
845 #ifdef CONFIG_X86_32
846 /*
847 * When LAPIC was disabled by the BIOS and enabled by the kernel,
848 * restore the disabled state.
849 */
850 if (enabled_via_apicbase) {
851 unsigned int l, h;
852
853 rdmsr(MSR_IA32_APICBASE, l, h);
854 l &= ~MSR_IA32_APICBASE_ENABLE;
855 wrmsr(MSR_IA32_APICBASE, l, h);
856 }
857 #endif
858 }
859
860 /*
861 * If Linux enabled the LAPIC against the BIOS default disable it down before
862 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
863 * not power-off. Additionally clear all LVT entries before disable_local_APIC
864 * for the case where Linux didn't enable the LAPIC.
865 */
866 void lapic_shutdown(void)
867 {
868 unsigned long flags;
869
870 if (!cpu_has_apic)
871 return;
872
873 local_irq_save(flags);
874
875 #ifdef CONFIG_X86_32
876 if (!enabled_via_apicbase)
877 clear_local_APIC();
878 else
879 #endif
880 disable_local_APIC();
881
882
883 local_irq_restore(flags);
884 }
885
886 /*
887 * This is to verify that we're looking at a real local APIC.
888 * Check these against your board if the CPUs aren't getting
889 * started for no apparent reason.
890 */
891 int __init verify_local_APIC(void)
892 {
893 unsigned int reg0, reg1;
894
895 /*
896 * The version register is read-only in a real APIC.
897 */
898 reg0 = apic_read(APIC_LVR);
899 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
900 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
901 reg1 = apic_read(APIC_LVR);
902 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
903
904 /*
905 * The two version reads above should print the same
906 * numbers. If the second one is different, then we
907 * poke at a non-APIC.
908 */
909 if (reg1 != reg0)
910 return 0;
911
912 /*
913 * Check if the version looks reasonably.
914 */
915 reg1 = GET_APIC_VERSION(reg0);
916 if (reg1 == 0x00 || reg1 == 0xff)
917 return 0;
918 reg1 = lapic_get_maxlvt();
919 if (reg1 < 0x02 || reg1 == 0xff)
920 return 0;
921
922 /*
923 * The ID register is read/write in a real APIC.
924 */
925 reg0 = apic_read(APIC_ID);
926 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
927 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
928 reg1 = apic_read(APIC_ID);
929 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
930 apic_write(APIC_ID, reg0);
931 if (reg1 != (reg0 ^ APIC_ID_MASK))
932 return 0;
933
934 /*
935 * The next two are just to see if we have sane values.
936 * They're only really relevant if we're in Virtual Wire
937 * compatibility mode, but most boxes are anymore.
938 */
939 reg0 = apic_read(APIC_LVT0);
940 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
941 reg1 = apic_read(APIC_LVT1);
942 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
943
944 return 1;
945 }
946
947 /**
948 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
949 */
950 void __init sync_Arb_IDs(void)
951 {
952 /*
953 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
954 * needed on AMD.
955 */
956 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
957 return;
958
959 /*
960 * Wait for idle.
961 */
962 apic_wait_icr_idle();
963
964 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
965 apic_write(APIC_ICR, APIC_DEST_ALLINC |
966 APIC_INT_LEVELTRIG | APIC_DM_INIT);
967 }
968
969 /*
970 * An initial setup of the virtual wire mode.
971 */
972 void __init init_bsp_APIC(void)
973 {
974 unsigned int value;
975
976 /*
977 * Don't do the setup now if we have a SMP BIOS as the
978 * through-I/O-APIC virtual wire mode might be active.
979 */
980 if (smp_found_config || !cpu_has_apic)
981 return;
982
983 /*
984 * Do not trust the local APIC being empty at bootup.
985 */
986 clear_local_APIC();
987
988 /*
989 * Enable APIC.
990 */
991 value = apic_read(APIC_SPIV);
992 value &= ~APIC_VECTOR_MASK;
993 value |= APIC_SPIV_APIC_ENABLED;
994
995 #ifdef CONFIG_X86_32
996 /* This bit is reserved on P4/Xeon and should be cleared */
997 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
998 (boot_cpu_data.x86 == 15))
999 value &= ~APIC_SPIV_FOCUS_DISABLED;
1000 else
1001 #endif
1002 value |= APIC_SPIV_FOCUS_DISABLED;
1003 value |= SPURIOUS_APIC_VECTOR;
1004 apic_write(APIC_SPIV, value);
1005
1006 /*
1007 * Set up the virtual wire mode.
1008 */
1009 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1010 value = APIC_DM_NMI;
1011 if (!lapic_is_integrated()) /* 82489DX */
1012 value |= APIC_LVT_LEVEL_TRIGGER;
1013 apic_write(APIC_LVT1, value);
1014 }
1015
1016 static void __cpuinit lapic_setup_esr(void)
1017 {
1018 unsigned long oldvalue, value, maxlvt;
1019 if (lapic_is_integrated() && !esr_disable) {
1020 if (esr_disable) {
1021 /*
1022 * Something untraceable is creating bad interrupts on
1023 * secondary quads ... for the moment, just leave the
1024 * ESR disabled - we can't do anything useful with the
1025 * errors anyway - mbligh
1026 */
1027 printk(KERN_INFO "Leaving ESR disabled.\n");
1028 return;
1029 }
1030 /* !82489DX */
1031 maxlvt = lapic_get_maxlvt();
1032 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1033 apic_write(APIC_ESR, 0);
1034 oldvalue = apic_read(APIC_ESR);
1035
1036 /* enables sending errors */
1037 value = ERROR_APIC_VECTOR;
1038 apic_write(APIC_LVTERR, value);
1039 /*
1040 * spec says clear errors after enabling vector.
1041 */
1042 if (maxlvt > 3)
1043 apic_write(APIC_ESR, 0);
1044 value = apic_read(APIC_ESR);
1045 if (value != oldvalue)
1046 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1047 "vector: 0x%08lx after: 0x%08lx\n",
1048 oldvalue, value);
1049 } else {
1050 printk(KERN_INFO "No ESR for 82489DX.\n");
1051 }
1052 }
1053
1054
1055 /**
1056 * setup_local_APIC - setup the local APIC
1057 */
1058 void __cpuinit setup_local_APIC(void)
1059 {
1060 unsigned int value;
1061 int i, j;
1062
1063 #ifdef CONFIG_X86_32
1064 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1065 if (esr_disable) {
1066 apic_write(APIC_ESR, 0);
1067 apic_write(APIC_ESR, 0);
1068 apic_write(APIC_ESR, 0);
1069 apic_write(APIC_ESR, 0);
1070 }
1071 #endif
1072
1073 preempt_disable();
1074
1075 /*
1076 * Double-check whether this APIC is really registered.
1077 * This is meaningless in clustered apic mode, so we skip it.
1078 */
1079 if (!apic_id_registered())
1080 BUG();
1081
1082 /*
1083 * Intel recommends to set DFR, LDR and TPR before enabling
1084 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1085 * document number 292116). So here it goes...
1086 */
1087 init_apic_ldr();
1088
1089 /*
1090 * Set Task Priority to 'accept all'. We never change this
1091 * later on.
1092 */
1093 value = apic_read(APIC_TASKPRI);
1094 value &= ~APIC_TPRI_MASK;
1095 apic_write(APIC_TASKPRI, value);
1096
1097 /*
1098 * After a crash, we no longer service the interrupts and a pending
1099 * interrupt from previous kernel might still have ISR bit set.
1100 *
1101 * Most probably by now CPU has serviced that pending interrupt and
1102 * it might not have done the ack_APIC_irq() because it thought,
1103 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1104 * does not clear the ISR bit and cpu thinks it has already serivced
1105 * the interrupt. Hence a vector might get locked. It was noticed
1106 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1107 */
1108 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1109 value = apic_read(APIC_ISR + i*0x10);
1110 for (j = 31; j >= 0; j--) {
1111 if (value & (1<<j))
1112 ack_APIC_irq();
1113 }
1114 }
1115
1116 /*
1117 * Now that we are all set up, enable the APIC
1118 */
1119 value = apic_read(APIC_SPIV);
1120 value &= ~APIC_VECTOR_MASK;
1121 /*
1122 * Enable APIC
1123 */
1124 value |= APIC_SPIV_APIC_ENABLED;
1125
1126 #ifdef CONFIG_X86_32
1127 /*
1128 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1129 * certain networking cards. If high frequency interrupts are
1130 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1131 * entry is masked/unmasked at a high rate as well then sooner or
1132 * later IOAPIC line gets 'stuck', no more interrupts are received
1133 * from the device. If focus CPU is disabled then the hang goes
1134 * away, oh well :-(
1135 *
1136 * [ This bug can be reproduced easily with a level-triggered
1137 * PCI Ne2000 networking cards and PII/PIII processors, dual
1138 * BX chipset. ]
1139 */
1140 /*
1141 * Actually disabling the focus CPU check just makes the hang less
1142 * frequent as it makes the interrupt distributon model be more
1143 * like LRU than MRU (the short-term load is more even across CPUs).
1144 * See also the comment in end_level_ioapic_irq(). --macro
1145 */
1146
1147 /*
1148 * - enable focus processor (bit==0)
1149 * - 64bit mode always use processor focus
1150 * so no need to set it
1151 */
1152 value &= ~APIC_SPIV_FOCUS_DISABLED;
1153 #endif
1154
1155 /*
1156 * Set spurious IRQ vector
1157 */
1158 value |= SPURIOUS_APIC_VECTOR;
1159 apic_write(APIC_SPIV, value);
1160
1161 /*
1162 * Set up LVT0, LVT1:
1163 *
1164 * set up through-local-APIC on the BP's LINT0. This is not
1165 * strictly necessary in pure symmetric-IO mode, but sometimes
1166 * we delegate interrupts to the 8259A.
1167 */
1168 /*
1169 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1170 */
1171 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1172 if (!smp_processor_id() && (pic_mode || !value)) {
1173 value = APIC_DM_EXTINT;
1174 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1175 smp_processor_id());
1176 } else {
1177 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1178 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1179 smp_processor_id());
1180 }
1181 apic_write(APIC_LVT0, value);
1182
1183 /*
1184 * only the BP should see the LINT1 NMI signal, obviously.
1185 */
1186 if (!smp_processor_id())
1187 value = APIC_DM_NMI;
1188 else
1189 value = APIC_DM_NMI | APIC_LVT_MASKED;
1190 if (!lapic_is_integrated()) /* 82489DX */
1191 value |= APIC_LVT_LEVEL_TRIGGER;
1192 apic_write(APIC_LVT1, value);
1193
1194 preempt_enable();
1195 }
1196
1197 void __cpuinit end_local_APIC_setup(void)
1198 {
1199 lapic_setup_esr();
1200
1201 #ifdef CONFIG_X86_32
1202 {
1203 unsigned int value;
1204 /* Disable the local apic timer */
1205 value = apic_read(APIC_LVTT);
1206 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1207 apic_write(APIC_LVTT, value);
1208 }
1209 #endif
1210
1211 setup_apic_nmi_watchdog(NULL);
1212 apic_pm_activate();
1213 }
1214
1215 /*
1216 * Detect and initialize APIC
1217 */
1218 static int __init detect_init_APIC(void)
1219 {
1220 u32 h, l, features;
1221
1222 /* Disabled by kernel option? */
1223 if (disable_apic)
1224 return -1;
1225
1226 switch (boot_cpu_data.x86_vendor) {
1227 case X86_VENDOR_AMD:
1228 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1229 (boot_cpu_data.x86 == 15))
1230 break;
1231 goto no_apic;
1232 case X86_VENDOR_INTEL:
1233 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1234 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1235 break;
1236 goto no_apic;
1237 default:
1238 goto no_apic;
1239 }
1240
1241 if (!cpu_has_apic) {
1242 /*
1243 * Over-ride BIOS and try to enable the local APIC only if
1244 * "lapic" specified.
1245 */
1246 if (!force_enable_local_apic) {
1247 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1248 "you can enable it with \"lapic\"\n");
1249 return -1;
1250 }
1251 /*
1252 * Some BIOSes disable the local APIC in the APIC_BASE
1253 * MSR. This can only be done in software for Intel P6 or later
1254 * and AMD K7 (Model > 1) or later.
1255 */
1256 rdmsr(MSR_IA32_APICBASE, l, h);
1257 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1258 printk(KERN_INFO
1259 "Local APIC disabled by BIOS -- reenabling.\n");
1260 l &= ~MSR_IA32_APICBASE_BASE;
1261 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1262 wrmsr(MSR_IA32_APICBASE, l, h);
1263 enabled_via_apicbase = 1;
1264 }
1265 }
1266 /*
1267 * The APIC feature bit should now be enabled
1268 * in `cpuid'
1269 */
1270 features = cpuid_edx(1);
1271 if (!(features & (1 << X86_FEATURE_APIC))) {
1272 printk(KERN_WARNING "Could not enable APIC!\n");
1273 return -1;
1274 }
1275 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1276 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1277
1278 /* The BIOS may have set up the APIC at some other address */
1279 rdmsr(MSR_IA32_APICBASE, l, h);
1280 if (l & MSR_IA32_APICBASE_ENABLE)
1281 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1282
1283 printk(KERN_INFO "Found and enabled local APIC!\n");
1284
1285 apic_pm_activate();
1286
1287 return 0;
1288
1289 no_apic:
1290 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1291 return -1;
1292 }
1293
1294 /**
1295 * init_apic_mappings - initialize APIC mappings
1296 */
1297 void __init init_apic_mappings(void)
1298 {
1299 /*
1300 * If no local APIC can be found then set up a fake all
1301 * zeroes page to simulate the local APIC and another
1302 * one for the IO-APIC.
1303 */
1304 if (!smp_found_config && detect_init_APIC()) {
1305 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1306 apic_phys = __pa(apic_phys);
1307 } else
1308 apic_phys = mp_lapic_addr;
1309
1310 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1311 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1312 apic_phys);
1313
1314 /*
1315 * Fetch the APIC ID of the BSP in case we have a
1316 * default configuration (or the MP table is broken).
1317 */
1318 if (boot_cpu_physical_apicid == -1U)
1319 boot_cpu_physical_apicid = read_apic_id();
1320
1321 }
1322
1323 /*
1324 * This initializes the IO-APIC and APIC hardware if this is
1325 * a UP kernel.
1326 */
1327
1328 int apic_version[MAX_APICS];
1329
1330 int __init APIC_init_uniprocessor(void)
1331 {
1332 if (!smp_found_config && !cpu_has_apic)
1333 return -1;
1334
1335 /*
1336 * Complain if the BIOS pretends there is one.
1337 */
1338 if (!cpu_has_apic &&
1339 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1340 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1341 boot_cpu_physical_apicid);
1342 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1343 return -1;
1344 }
1345
1346 verify_local_APIC();
1347
1348 connect_bsp_APIC();
1349
1350 /*
1351 * Hack: In case of kdump, after a crash, kernel might be booting
1352 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1353 * might be zero if read from MP tables. Get it from LAPIC.
1354 */
1355 #ifdef CONFIG_CRASH_DUMP
1356 boot_cpu_physical_apicid = read_apic_id();
1357 #endif
1358 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1359
1360 setup_local_APIC();
1361
1362 #ifdef CONFIG_X86_IO_APIC
1363 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1364 #endif
1365 localise_nmi_watchdog();
1366 end_local_APIC_setup();
1367 #ifdef CONFIG_X86_IO_APIC
1368 if (smp_found_config)
1369 if (!skip_ioapic_setup && nr_ioapics)
1370 setup_IO_APIC();
1371 #endif
1372 setup_boot_clock();
1373
1374 return 0;
1375 }
1376
1377 /*
1378 * Local APIC interrupts
1379 */
1380
1381 /*
1382 * This interrupt should _never_ happen with our APIC/SMP architecture
1383 */
1384 void smp_spurious_interrupt(struct pt_regs *regs)
1385 {
1386 unsigned long v;
1387
1388 irq_enter();
1389 /*
1390 * Check if this really is a spurious interrupt and ACK it
1391 * if it is a vectored one. Just in case...
1392 * Spurious interrupts should not be ACKed.
1393 */
1394 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1395 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1396 ack_APIC_irq();
1397
1398 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1399 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1400 "should never happen.\n", smp_processor_id());
1401 __get_cpu_var(irq_stat).irq_spurious_count++;
1402 irq_exit();
1403 }
1404
1405 /*
1406 * This interrupt should never happen with our APIC/SMP architecture
1407 */
1408 void smp_error_interrupt(struct pt_regs *regs)
1409 {
1410 unsigned long v, v1;
1411
1412 irq_enter();
1413 /* First tickle the hardware, only then report what went on. -- REW */
1414 v = apic_read(APIC_ESR);
1415 apic_write(APIC_ESR, 0);
1416 v1 = apic_read(APIC_ESR);
1417 ack_APIC_irq();
1418 atomic_inc(&irq_err_count);
1419
1420 /* Here is what the APIC error bits mean:
1421 0: Send CS error
1422 1: Receive CS error
1423 2: Send accept error
1424 3: Receive accept error
1425 4: Reserved
1426 5: Send illegal vector
1427 6: Received illegal vector
1428 7: Illegal register address
1429 */
1430 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1431 smp_processor_id(), v , v1);
1432 irq_exit();
1433 }
1434
1435 /**
1436 * connect_bsp_APIC - attach the APIC to the interrupt system
1437 */
1438 void __init connect_bsp_APIC(void)
1439 {
1440 #ifdef CONFIG_X86_32
1441 if (pic_mode) {
1442 /*
1443 * Do not trust the local APIC being empty at bootup.
1444 */
1445 clear_local_APIC();
1446 /*
1447 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1448 * local APIC to INT and NMI lines.
1449 */
1450 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1451 "enabling APIC mode.\n");
1452 outb(0x70, 0x22);
1453 outb(0x01, 0x23);
1454 }
1455 #endif
1456 enable_apic_mode();
1457 }
1458
1459 /**
1460 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1461 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1462 *
1463 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1464 * APIC is disabled.
1465 */
1466 void disconnect_bsp_APIC(int virt_wire_setup)
1467 {
1468 unsigned int value;
1469
1470 #ifdef CONFIG_X86_32
1471 if (pic_mode) {
1472 /*
1473 * Put the board back into PIC mode (has an effect only on
1474 * certain older boards). Note that APIC interrupts, including
1475 * IPIs, won't work beyond this point! The only exception are
1476 * INIT IPIs.
1477 */
1478 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1479 "entering PIC mode.\n");
1480 outb(0x70, 0x22);
1481 outb(0x00, 0x23);
1482 return;
1483 }
1484 #endif
1485
1486 /* Go back to Virtual Wire compatibility mode */
1487
1488 /* For the spurious interrupt use vector F, and enable it */
1489 value = apic_read(APIC_SPIV);
1490 value &= ~APIC_VECTOR_MASK;
1491 value |= APIC_SPIV_APIC_ENABLED;
1492 value |= 0xf;
1493 apic_write(APIC_SPIV, value);
1494
1495 if (!virt_wire_setup) {
1496 /*
1497 * For LVT0 make it edge triggered, active high,
1498 * external and enabled
1499 */
1500 value = apic_read(APIC_LVT0);
1501 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1502 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1503 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1504 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1505 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1506 apic_write(APIC_LVT0, value);
1507 } else {
1508 /* Disable LVT0 */
1509 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1510 }
1511
1512 /*
1513 * For LVT1 make it edge triggered, active high,
1514 * nmi and enabled
1515 */
1516 value = apic_read(APIC_LVT1);
1517 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1518 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1519 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1520 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1521 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1522 apic_write(APIC_LVT1, value);
1523 }
1524
1525 void __cpuinit generic_processor_info(int apicid, int version)
1526 {
1527 int cpu;
1528 cpumask_t tmp_map;
1529
1530 /*
1531 * Validate version
1532 */
1533 if (version == 0x0) {
1534 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1535 "fixing up to 0x10. (tell your hw vendor)\n",
1536 version);
1537 version = 0x10;
1538 }
1539 apic_version[apicid] = version;
1540
1541 if (num_processors >= NR_CPUS) {
1542 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1543 " Processor ignored.\n", NR_CPUS);
1544 return;
1545 }
1546
1547 num_processors++;
1548 cpus_complement(tmp_map, cpu_present_map);
1549 cpu = first_cpu(tmp_map);
1550
1551 physid_set(apicid, phys_cpu_present_map);
1552 if (apicid == boot_cpu_physical_apicid) {
1553 /*
1554 * x86_bios_cpu_apicid is required to have processors listed
1555 * in same order as logical cpu numbers. Hence the first
1556 * entry is BSP, and so on.
1557 */
1558 cpu = 0;
1559 }
1560 if (apicid > max_physical_apicid)
1561 max_physical_apicid = apicid;
1562
1563 #ifdef CONFIG_X86_32
1564 /*
1565 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1566 * but we need to work other dependencies like SMP_SUSPEND etc
1567 * before this can be done without some confusion.
1568 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1569 * - Ashok Raj <ashok.raj@intel.com>
1570 */
1571 if (max_physical_apicid >= 8) {
1572 switch (boot_cpu_data.x86_vendor) {
1573 case X86_VENDOR_INTEL:
1574 if (!APIC_XAPIC(version)) {
1575 def_to_bigsmp = 0;
1576 break;
1577 }
1578 /* If P4 and above fall through */
1579 case X86_VENDOR_AMD:
1580 def_to_bigsmp = 1;
1581 }
1582 }
1583 #endif
1584
1585 #if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
1586 /* are we being called early in kernel startup? */
1587 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1588 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1589 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
1590
1591 cpu_to_apicid[cpu] = apicid;
1592 bios_cpu_apicid[cpu] = apicid;
1593 } else {
1594 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1595 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1596 }
1597 #endif
1598
1599 cpu_set(cpu, cpu_possible_map);
1600 cpu_set(cpu, cpu_present_map);
1601 }
1602
1603 #ifdef CONFIG_X86_64
1604 int hard_smp_processor_id(void)
1605 {
1606 return read_apic_id();
1607 }
1608 #endif
1609
1610 /*
1611 * Power management
1612 */
1613 #ifdef CONFIG_PM
1614
1615 static struct {
1616 /*
1617 * 'active' is true if the local APIC was enabled by us and
1618 * not the BIOS; this signifies that we are also responsible
1619 * for disabling it before entering apm/acpi suspend
1620 */
1621 int active;
1622 /* r/w apic fields */
1623 unsigned int apic_id;
1624 unsigned int apic_taskpri;
1625 unsigned int apic_ldr;
1626 unsigned int apic_dfr;
1627 unsigned int apic_spiv;
1628 unsigned int apic_lvtt;
1629 unsigned int apic_lvtpc;
1630 unsigned int apic_lvt0;
1631 unsigned int apic_lvt1;
1632 unsigned int apic_lvterr;
1633 unsigned int apic_tmict;
1634 unsigned int apic_tdcr;
1635 unsigned int apic_thmr;
1636 } apic_pm_state;
1637
1638 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1639 {
1640 unsigned long flags;
1641 int maxlvt;
1642
1643 if (!apic_pm_state.active)
1644 return 0;
1645
1646 maxlvt = lapic_get_maxlvt();
1647
1648 apic_pm_state.apic_id = apic_read(APIC_ID);
1649 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1650 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1651 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1652 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1653 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1654 if (maxlvt >= 4)
1655 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1656 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1657 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1658 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1659 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1660 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1661 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
1662 if (maxlvt >= 5)
1663 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1664 #endif
1665
1666 local_irq_save(flags);
1667 disable_local_APIC();
1668 local_irq_restore(flags);
1669 return 0;
1670 }
1671
1672 static int lapic_resume(struct sys_device *dev)
1673 {
1674 unsigned int l, h;
1675 unsigned long flags;
1676 int maxlvt;
1677
1678 if (!apic_pm_state.active)
1679 return 0;
1680
1681 maxlvt = lapic_get_maxlvt();
1682
1683 local_irq_save(flags);
1684
1685 #ifdef CONFIG_X86_64
1686 if (x2apic)
1687 enable_x2apic();
1688 else
1689 #endif
1690 {
1691 /*
1692 * Make sure the APICBASE points to the right address
1693 *
1694 * FIXME! This will be wrong if we ever support suspend on
1695 * SMP! We'll need to do this as part of the CPU restore!
1696 */
1697 rdmsr(MSR_IA32_APICBASE, l, h);
1698 l &= ~MSR_IA32_APICBASE_BASE;
1699 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1700 wrmsr(MSR_IA32_APICBASE, l, h);
1701 }
1702
1703 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1704 apic_write(APIC_ID, apic_pm_state.apic_id);
1705 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1706 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1707 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1708 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1709 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1710 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1711 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
1712 if (maxlvt >= 5)
1713 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1714 #endif
1715 if (maxlvt >= 4)
1716 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1717 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1718 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1719 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1720 apic_write(APIC_ESR, 0);
1721 apic_read(APIC_ESR);
1722 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1723 apic_write(APIC_ESR, 0);
1724 apic_read(APIC_ESR);
1725
1726 local_irq_restore(flags);
1727
1728 return 0;
1729 }
1730
1731 /*
1732 * This device has no shutdown method - fully functioning local APICs
1733 * are needed on every CPU up until machine_halt/restart/poweroff.
1734 */
1735
1736 static struct sysdev_class lapic_sysclass = {
1737 .name = "lapic",
1738 .resume = lapic_resume,
1739 .suspend = lapic_suspend,
1740 };
1741
1742 static struct sys_device device_lapic = {
1743 .id = 0,
1744 .cls = &lapic_sysclass,
1745 };
1746
1747 static void __cpuinit apic_pm_activate(void)
1748 {
1749 apic_pm_state.active = 1;
1750 }
1751
1752 static int __init init_lapic_sysfs(void)
1753 {
1754 int error;
1755
1756 if (!cpu_has_apic)
1757 return 0;
1758 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1759
1760 error = sysdev_class_register(&lapic_sysclass);
1761 if (!error)
1762 error = sysdev_register(&device_lapic);
1763 return error;
1764 }
1765 device_initcall(init_lapic_sysfs);
1766
1767 #else /* CONFIG_PM */
1768
1769 static void apic_pm_activate(void) { }
1770
1771 #endif /* CONFIG_PM */
1772
1773
1774 static int __init setup_disableapic(char *arg)
1775 {
1776 disable_apic = 1;
1777 setup_clear_cpu_cap(X86_FEATURE_APIC);
1778 return 0;
1779 }
1780 early_param("disableapic", setup_disableapic);
1781
1782 /* same as disableapic, for compatibility */
1783 static int __init setup_nolapic(char *arg)
1784 {
1785 return setup_disableapic(arg);
1786 }
1787 early_param("nolapic", setup_nolapic);
1788
1789 static int __init parse_lapic_timer_c2_ok(char *arg)
1790 {
1791 local_apic_timer_c2_ok = 1;
1792 return 0;
1793 }
1794 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1795
1796 static int __init parse_disable_apic_timer(char *arg)
1797 {
1798 disable_apic_timer = 1;
1799 return 0;
1800 }
1801 early_param("noapictimer", parse_disable_apic_timer);
1802
1803 static int __init parse_nolapic_timer(char *arg)
1804 {
1805 disable_apic_timer = 1;
1806 return 0;
1807 }
1808 early_param("nolapic_timer", parse_nolapic_timer);
1809
1810 static int __init apic_set_verbosity(char *arg)
1811 {
1812 if (!arg) {
1813 #ifdef CONFIG_X86_64
1814 skip_ioapic_setup = 0;
1815 ioapic_force = 1;
1816 return 0;
1817 #endif
1818 return -EINVAL;
1819 }
1820
1821 if (strcmp("debug", arg) == 0)
1822 apic_verbosity = APIC_DEBUG;
1823 else if (strcmp("verbose", arg) == 0)
1824 apic_verbosity = APIC_VERBOSE;
1825 else {
1826 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1827 " use apic=verbose or apic=debug\n", arg);
1828 return -EINVAL;
1829 }
1830
1831 return 0;
1832 }
1833 early_param("apic", apic_set_verbosity);
1834
1835 static int __init lapic_insert_resource(void)
1836 {
1837 if (!apic_phys)
1838 return -1;
1839
1840 /* Put local APIC into the resource map. */
1841 lapic_resource.start = apic_phys;
1842 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1843 insert_resource(&iomem_resource, &lapic_resource);
1844
1845 return 0;
1846 }
1847
1848 /*
1849 * need call insert after e820_reserve_resources()
1850 * that is using request_resource
1851 */
1852 late_initcall(lapic_insert_resource);
This page took 0.069172 seconds and 5 git commands to generate.