x86: untable __init references between IO data
[deliverable/linux.git] / arch / x86 / kernel / apic_64.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/module.h>
27 #include <linux/ioport.h>
28 #include <linux/clockchips.h>
29 #include <linux/acpi_pmtmr.h>
30
31 #include <asm/atomic.h>
32 #include <asm/smp.h>
33 #include <asm/mtrr.h>
34 #include <asm/mpspec.h>
35 #include <asm/pgalloc.h>
36 #include <asm/mach_apic.h>
37 #include <asm/nmi.h>
38 #include <asm/idle.h>
39 #include <asm/proto.h>
40 #include <asm/timex.h>
41 #include <asm/hpet.h>
42 #include <asm/apic.h>
43
44 int apic_verbosity;
45 int disable_apic_timer __cpuinitdata;
46 static int apic_calibrate_pmtmr __initdata;
47 int disable_apic;
48
49 /* Local APIC timer works in C2? */
50 int local_apic_timer_c2_ok;
51 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
52
53 static struct resource lapic_resource = {
54 .name = "Local APIC",
55 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
56 };
57
58 static unsigned int calibration_result;
59
60 static int lapic_next_event(unsigned long delta,
61 struct clock_event_device *evt);
62 static void lapic_timer_setup(enum clock_event_mode mode,
63 struct clock_event_device *evt);
64 static void lapic_timer_broadcast(cpumask_t mask);
65 static void apic_pm_activate(void);
66
67 static struct clock_event_device lapic_clockevent = {
68 .name = "lapic",
69 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
70 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
71 .shift = 32,
72 .set_mode = lapic_timer_setup,
73 .set_next_event = lapic_next_event,
74 .broadcast = lapic_timer_broadcast,
75 .rating = 100,
76 .irq = -1,
77 };
78 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
79
80 /*
81 * Get the LAPIC version
82 */
83 static inline int lapic_get_version(void)
84 {
85 return GET_APIC_VERSION(apic_read(APIC_LVR));
86 }
87
88 /*
89 * Check, if the APIC is integrated or a seperate chip
90 */
91 static inline int lapic_is_integrated(void)
92 {
93 return 1;
94 }
95
96 /*
97 * Check, whether this is a modern or a first generation APIC
98 */
99 static int modern_apic(void)
100 {
101 /* AMD systems use old APIC versions, so check the CPU */
102 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
103 boot_cpu_data.x86 >= 0xf)
104 return 1;
105 return lapic_get_version() >= 0x14;
106 }
107
108 void apic_wait_icr_idle(void)
109 {
110 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
111 cpu_relax();
112 }
113
114 u32 safe_apic_wait_icr_idle(void)
115 {
116 u32 send_status;
117 int timeout;
118
119 timeout = 0;
120 do {
121 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
122 if (!send_status)
123 break;
124 udelay(100);
125 } while (timeout++ < 1000);
126
127 return send_status;
128 }
129
130 /**
131 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
132 */
133 void enable_NMI_through_LVT0(void *dummy)
134 {
135 unsigned int v;
136
137 /* unmask and set to NMI */
138 v = APIC_DM_NMI;
139 apic_write(APIC_LVT0, v);
140 }
141
142 /**
143 * lapic_get_maxlvt - get the maximum number of local vector table entries
144 */
145 int lapic_get_maxlvt(void)
146 {
147 unsigned int v, maxlvt;
148
149 v = apic_read(APIC_LVR);
150 maxlvt = GET_APIC_MAXLVT(v);
151 return maxlvt;
152 }
153
154 /*
155 * This function sets up the local APIC timer, with a timeout of
156 * 'clocks' APIC bus clock. During calibration we actually call
157 * this function twice on the boot CPU, once with a bogus timeout
158 * value, second time for real. The other (noncalibrating) CPUs
159 * call this function only once, with the real, calibrated value.
160 *
161 * We do reads before writes even if unnecessary, to get around the
162 * P5 APIC double write bug.
163 */
164
165 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
166 {
167 unsigned int lvtt_value, tmp_value;
168
169 lvtt_value = LOCAL_TIMER_VECTOR;
170 if (!oneshot)
171 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
172 if (!irqen)
173 lvtt_value |= APIC_LVT_MASKED;
174
175 apic_write(APIC_LVTT, lvtt_value);
176
177 /*
178 * Divide PICLK by 16
179 */
180 tmp_value = apic_read(APIC_TDCR);
181 apic_write(APIC_TDCR, (tmp_value
182 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
183 | APIC_TDR_DIV_16);
184
185 if (!oneshot)
186 apic_write(APIC_TMICT, clocks);
187 }
188
189 /*
190 * Setup extended LVT (K8 specific)
191 */
192 void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
193 unsigned char msg_type, unsigned char mask)
194 {
195 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
196 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
197
198 apic_write(reg, v);
199 }
200
201 /*
202 * Program the next event, relative to now
203 */
204 static int lapic_next_event(unsigned long delta,
205 struct clock_event_device *evt)
206 {
207 apic_write(APIC_TMICT, delta);
208 return 0;
209 }
210
211 /*
212 * Setup the lapic timer in periodic or oneshot mode
213 */
214 static void lapic_timer_setup(enum clock_event_mode mode,
215 struct clock_event_device *evt)
216 {
217 unsigned long flags;
218 unsigned int v;
219
220 /* Lapic used as dummy for broadcast ? */
221 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
222 return;
223
224 local_irq_save(flags);
225
226 switch (mode) {
227 case CLOCK_EVT_MODE_PERIODIC:
228 case CLOCK_EVT_MODE_ONESHOT:
229 __setup_APIC_LVTT(calibration_result,
230 mode != CLOCK_EVT_MODE_PERIODIC, 1);
231 break;
232 case CLOCK_EVT_MODE_UNUSED:
233 case CLOCK_EVT_MODE_SHUTDOWN:
234 v = apic_read(APIC_LVTT);
235 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
236 apic_write(APIC_LVTT, v);
237 break;
238 case CLOCK_EVT_MODE_RESUME:
239 /* Nothing to do here */
240 break;
241 }
242
243 local_irq_restore(flags);
244 }
245
246 /*
247 * Local APIC timer broadcast function
248 */
249 static void lapic_timer_broadcast(cpumask_t mask)
250 {
251 #ifdef CONFIG_SMP
252 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
253 #endif
254 }
255
256 /*
257 * Setup the local APIC timer for this CPU. Copy the initilized values
258 * of the boot CPU and register the clock event in the framework.
259 */
260 static void setup_APIC_timer(void)
261 {
262 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
263
264 memcpy(levt, &lapic_clockevent, sizeof(*levt));
265 levt->cpumask = cpumask_of_cpu(smp_processor_id());
266
267 clockevents_register_device(levt);
268 }
269
270 /*
271 * In this function we calibrate APIC bus clocks to the external
272 * timer. Unfortunately we cannot use jiffies and the timer irq
273 * to calibrate, since some later bootup code depends on getting
274 * the first irq? Ugh.
275 *
276 * We want to do the calibration only once since we
277 * want to have local timer irqs syncron. CPUs connected
278 * by the same APIC bus have the very same bus frequency.
279 * And we want to have irqs off anyways, no accidental
280 * APIC irq that way.
281 */
282
283 #define TICK_COUNT 100000000
284
285 static void __init calibrate_APIC_clock(void)
286 {
287 unsigned apic, apic_start;
288 unsigned long tsc, tsc_start;
289 int result;
290
291 local_irq_disable();
292
293 /*
294 * Put whatever arbitrary (but long enough) timeout
295 * value into the APIC clock, we just want to get the
296 * counter running for calibration.
297 *
298 * No interrupt enable !
299 */
300 __setup_APIC_LVTT(250000000, 0, 0);
301
302 apic_start = apic_read(APIC_TMCCT);
303 #ifdef CONFIG_X86_PM_TIMER
304 if (apic_calibrate_pmtmr && pmtmr_ioport) {
305 pmtimer_wait(5000); /* 5ms wait */
306 apic = apic_read(APIC_TMCCT);
307 result = (apic_start - apic) * 1000L / 5;
308 } else
309 #endif
310 {
311 rdtscll(tsc_start);
312
313 do {
314 apic = apic_read(APIC_TMCCT);
315 rdtscll(tsc);
316 } while ((tsc - tsc_start) < TICK_COUNT &&
317 (apic_start - apic) < TICK_COUNT);
318
319 result = (apic_start - apic) * 1000L * tsc_khz /
320 (tsc - tsc_start);
321 }
322
323 local_irq_enable();
324
325 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
326
327 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
328 result / 1000 / 1000, result / 1000 % 1000);
329
330 /* Calculate the scaled math multiplication factor */
331 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC, 32);
332 lapic_clockevent.max_delta_ns =
333 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
334 lapic_clockevent.min_delta_ns =
335 clockevent_delta2ns(0xF, &lapic_clockevent);
336
337 calibration_result = result / HZ;
338 }
339
340 void __init setup_boot_APIC_clock(void)
341 {
342 /*
343 * The local apic timer can be disabled via the kernel commandline.
344 * Register the lapic timer as a dummy clock event source on SMP
345 * systems, so the broadcast mechanism is used. On UP systems simply
346 * ignore it.
347 */
348 if (disable_apic_timer) {
349 printk(KERN_INFO "Disabling APIC timer\n");
350 /* No broadcast on UP ! */
351 if (num_possible_cpus() > 1)
352 setup_APIC_timer();
353 return;
354 }
355
356 printk(KERN_INFO "Using local APIC timer interrupts.\n");
357 calibrate_APIC_clock();
358
359 /*
360 * If nmi_watchdog is set to IO_APIC, we need the
361 * PIT/HPET going. Otherwise register lapic as a dummy
362 * device.
363 */
364 if (nmi_watchdog != NMI_IO_APIC)
365 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
366 else
367 printk(KERN_WARNING "APIC timer registered as dummy,"
368 " due to nmi_watchdog=1!\n");
369
370 setup_APIC_timer();
371 }
372
373 /*
374 * AMD C1E enabled CPUs have a real nasty problem: Some BIOSes set the
375 * C1E flag only in the secondary CPU, so when we detect the wreckage
376 * we already have enabled the boot CPU local apic timer. Check, if
377 * disable_apic_timer is set and the DUMMY flag is cleared. If yes,
378 * set the DUMMY flag again and force the broadcast mode in the
379 * clockevents layer.
380 */
381 void __cpuinit check_boot_apic_timer_broadcast(void)
382 {
383 if (!disable_apic_timer ||
384 (lapic_clockevent.features & CLOCK_EVT_FEAT_DUMMY))
385 return;
386
387 printk(KERN_INFO "AMD C1E detected late. Force timer broadcast.\n");
388 lapic_clockevent.features |= CLOCK_EVT_FEAT_DUMMY;
389
390 local_irq_enable();
391 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE, &boot_cpu_id);
392 local_irq_disable();
393 }
394
395 void __cpuinit setup_secondary_APIC_clock(void)
396 {
397 check_boot_apic_timer_broadcast();
398 setup_APIC_timer();
399 }
400
401 /*
402 * The guts of the apic timer interrupt
403 */
404 static void local_apic_timer_interrupt(void)
405 {
406 int cpu = smp_processor_id();
407 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
408
409 /*
410 * Normally we should not be here till LAPIC has been initialized but
411 * in some cases like kdump, its possible that there is a pending LAPIC
412 * timer interrupt from previous kernel's context and is delivered in
413 * new kernel the moment interrupts are enabled.
414 *
415 * Interrupts are enabled early and LAPIC is setup much later, hence
416 * its possible that when we get here evt->event_handler is NULL.
417 * Check for event_handler being NULL and discard the interrupt as
418 * spurious.
419 */
420 if (!evt->event_handler) {
421 printk(KERN_WARNING
422 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
423 /* Switch it off */
424 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
425 return;
426 }
427
428 /*
429 * the NMI deadlock-detector uses this.
430 */
431 add_pda(apic_timer_irqs, 1);
432
433 evt->event_handler(evt);
434 }
435
436 /*
437 * Local APIC timer interrupt. This is the most natural way for doing
438 * local interrupts, but local timer interrupts can be emulated by
439 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
440 *
441 * [ if a single-CPU system runs an SMP kernel then we call the local
442 * interrupt as well. Thus we cannot inline the local irq ... ]
443 */
444 void smp_apic_timer_interrupt(struct pt_regs *regs)
445 {
446 struct pt_regs *old_regs = set_irq_regs(regs);
447
448 /*
449 * NOTE! We'd better ACK the irq immediately,
450 * because timer handling can be slow.
451 */
452 ack_APIC_irq();
453 /*
454 * update_process_times() expects us to have done irq_enter().
455 * Besides, if we don't timer interrupts ignore the global
456 * interrupt lock, which is the WrongThing (tm) to do.
457 */
458 exit_idle();
459 irq_enter();
460 local_apic_timer_interrupt();
461 irq_exit();
462 set_irq_regs(old_regs);
463 }
464
465 int setup_profiling_timer(unsigned int multiplier)
466 {
467 return -EINVAL;
468 }
469
470
471 /*
472 * Local APIC start and shutdown
473 */
474
475 /**
476 * clear_local_APIC - shutdown the local APIC
477 *
478 * This is called, when a CPU is disabled and before rebooting, so the state of
479 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
480 * leftovers during boot.
481 */
482 void clear_local_APIC(void)
483 {
484 int maxlvt = lapic_get_maxlvt();
485 u32 v;
486
487 /*
488 * Masking an LVT entry can trigger a local APIC error
489 * if the vector is zero. Mask LVTERR first to prevent this.
490 */
491 if (maxlvt >= 3) {
492 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
493 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
494 }
495 /*
496 * Careful: we have to set masks only first to deassert
497 * any level-triggered sources.
498 */
499 v = apic_read(APIC_LVTT);
500 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
501 v = apic_read(APIC_LVT0);
502 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
503 v = apic_read(APIC_LVT1);
504 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
505 if (maxlvt >= 4) {
506 v = apic_read(APIC_LVTPC);
507 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
508 }
509
510 /*
511 * Clean APIC state for other OSs:
512 */
513 apic_write(APIC_LVTT, APIC_LVT_MASKED);
514 apic_write(APIC_LVT0, APIC_LVT_MASKED);
515 apic_write(APIC_LVT1, APIC_LVT_MASKED);
516 if (maxlvt >= 3)
517 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
518 if (maxlvt >= 4)
519 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
520 apic_write(APIC_ESR, 0);
521 apic_read(APIC_ESR);
522 }
523
524 /**
525 * disable_local_APIC - clear and disable the local APIC
526 */
527 void disable_local_APIC(void)
528 {
529 unsigned int value;
530
531 clear_local_APIC();
532
533 /*
534 * Disable APIC (implies clearing of registers
535 * for 82489DX!).
536 */
537 value = apic_read(APIC_SPIV);
538 value &= ~APIC_SPIV_APIC_ENABLED;
539 apic_write(APIC_SPIV, value);
540 }
541
542 void lapic_shutdown(void)
543 {
544 unsigned long flags;
545
546 if (!cpu_has_apic)
547 return;
548
549 local_irq_save(flags);
550
551 disable_local_APIC();
552
553 local_irq_restore(flags);
554 }
555
556 /*
557 * This is to verify that we're looking at a real local APIC.
558 * Check these against your board if the CPUs aren't getting
559 * started for no apparent reason.
560 */
561 int __init verify_local_APIC(void)
562 {
563 unsigned int reg0, reg1;
564
565 /*
566 * The version register is read-only in a real APIC.
567 */
568 reg0 = apic_read(APIC_LVR);
569 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
570 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
571 reg1 = apic_read(APIC_LVR);
572 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
573
574 /*
575 * The two version reads above should print the same
576 * numbers. If the second one is different, then we
577 * poke at a non-APIC.
578 */
579 if (reg1 != reg0)
580 return 0;
581
582 /*
583 * Check if the version looks reasonably.
584 */
585 reg1 = GET_APIC_VERSION(reg0);
586 if (reg1 == 0x00 || reg1 == 0xff)
587 return 0;
588 reg1 = lapic_get_maxlvt();
589 if (reg1 < 0x02 || reg1 == 0xff)
590 return 0;
591
592 /*
593 * The ID register is read/write in a real APIC.
594 */
595 reg0 = apic_read(APIC_ID);
596 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
597 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
598 reg1 = apic_read(APIC_ID);
599 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
600 apic_write(APIC_ID, reg0);
601 if (reg1 != (reg0 ^ APIC_ID_MASK))
602 return 0;
603
604 /*
605 * The next two are just to see if we have sane values.
606 * They're only really relevant if we're in Virtual Wire
607 * compatibility mode, but most boxes are anymore.
608 */
609 reg0 = apic_read(APIC_LVT0);
610 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
611 reg1 = apic_read(APIC_LVT1);
612 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
613
614 return 1;
615 }
616
617 /**
618 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
619 */
620 void __init sync_Arb_IDs(void)
621 {
622 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
623 if (modern_apic())
624 return;
625
626 /*
627 * Wait for idle.
628 */
629 apic_wait_icr_idle();
630
631 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
632 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
633 | APIC_DM_INIT);
634 }
635
636 /*
637 * An initial setup of the virtual wire mode.
638 */
639 void __init init_bsp_APIC(void)
640 {
641 unsigned int value;
642
643 /*
644 * Don't do the setup now if we have a SMP BIOS as the
645 * through-I/O-APIC virtual wire mode might be active.
646 */
647 if (smp_found_config || !cpu_has_apic)
648 return;
649
650 value = apic_read(APIC_LVR);
651
652 /*
653 * Do not trust the local APIC being empty at bootup.
654 */
655 clear_local_APIC();
656
657 /*
658 * Enable APIC.
659 */
660 value = apic_read(APIC_SPIV);
661 value &= ~APIC_VECTOR_MASK;
662 value |= APIC_SPIV_APIC_ENABLED;
663 value |= APIC_SPIV_FOCUS_DISABLED;
664 value |= SPURIOUS_APIC_VECTOR;
665 apic_write(APIC_SPIV, value);
666
667 /*
668 * Set up the virtual wire mode.
669 */
670 apic_write(APIC_LVT0, APIC_DM_EXTINT);
671 value = APIC_DM_NMI;
672 apic_write(APIC_LVT1, value);
673 }
674
675 /**
676 * setup_local_APIC - setup the local APIC
677 */
678 void __cpuinit setup_local_APIC(void)
679 {
680 unsigned int value;
681 int i, j;
682
683 value = apic_read(APIC_LVR);
684
685 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
686
687 /*
688 * Double-check whether this APIC is really registered.
689 * This is meaningless in clustered apic mode, so we skip it.
690 */
691 if (!apic_id_registered())
692 BUG();
693
694 /*
695 * Intel recommends to set DFR, LDR and TPR before enabling
696 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
697 * document number 292116). So here it goes...
698 */
699 init_apic_ldr();
700
701 /*
702 * Set Task Priority to 'accept all'. We never change this
703 * later on.
704 */
705 value = apic_read(APIC_TASKPRI);
706 value &= ~APIC_TPRI_MASK;
707 apic_write(APIC_TASKPRI, value);
708
709 /*
710 * After a crash, we no longer service the interrupts and a pending
711 * interrupt from previous kernel might still have ISR bit set.
712 *
713 * Most probably by now CPU has serviced that pending interrupt and
714 * it might not have done the ack_APIC_irq() because it thought,
715 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
716 * does not clear the ISR bit and cpu thinks it has already serivced
717 * the interrupt. Hence a vector might get locked. It was noticed
718 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
719 */
720 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
721 value = apic_read(APIC_ISR + i*0x10);
722 for (j = 31; j >= 0; j--) {
723 if (value & (1<<j))
724 ack_APIC_irq();
725 }
726 }
727
728 /*
729 * Now that we are all set up, enable the APIC
730 */
731 value = apic_read(APIC_SPIV);
732 value &= ~APIC_VECTOR_MASK;
733 /*
734 * Enable APIC
735 */
736 value |= APIC_SPIV_APIC_ENABLED;
737
738 /* We always use processor focus */
739
740 /*
741 * Set spurious IRQ vector
742 */
743 value |= SPURIOUS_APIC_VECTOR;
744 apic_write(APIC_SPIV, value);
745
746 /*
747 * Set up LVT0, LVT1:
748 *
749 * set up through-local-APIC on the BP's LINT0. This is not
750 * strictly necessary in pure symmetric-IO mode, but sometimes
751 * we delegate interrupts to the 8259A.
752 */
753 /*
754 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
755 */
756 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
757 if (!smp_processor_id() && !value) {
758 value = APIC_DM_EXTINT;
759 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
760 smp_processor_id());
761 } else {
762 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
763 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
764 smp_processor_id());
765 }
766 apic_write(APIC_LVT0, value);
767
768 /*
769 * only the BP should see the LINT1 NMI signal, obviously.
770 */
771 if (!smp_processor_id())
772 value = APIC_DM_NMI;
773 else
774 value = APIC_DM_NMI | APIC_LVT_MASKED;
775 apic_write(APIC_LVT1, value);
776 }
777
778 void __cpuinit lapic_setup_esr(void)
779 {
780 unsigned maxlvt = lapic_get_maxlvt();
781
782 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
783 /*
784 * spec says clear errors after enabling vector.
785 */
786 if (maxlvt > 3)
787 apic_write(APIC_ESR, 0);
788 }
789
790 void __cpuinit end_local_APIC_setup(void)
791 {
792 lapic_setup_esr();
793 nmi_watchdog_default();
794 setup_apic_nmi_watchdog(NULL);
795 apic_pm_activate();
796 }
797
798 /*
799 * Detect and enable local APICs on non-SMP boards.
800 * Original code written by Keir Fraser.
801 * On AMD64 we trust the BIOS - if it says no APIC it is likely
802 * not correctly set up (usually the APIC timer won't work etc.)
803 */
804 static int __init detect_init_APIC(void)
805 {
806 if (!cpu_has_apic) {
807 printk(KERN_INFO "No local APIC present\n");
808 return -1;
809 }
810
811 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
812 boot_cpu_id = 0;
813 return 0;
814 }
815
816 /**
817 * init_apic_mappings - initialize APIC mappings
818 */
819 void __init init_apic_mappings(void)
820 {
821 unsigned long apic_phys;
822
823 /*
824 * If no local APIC can be found then set up a fake all
825 * zeroes page to simulate the local APIC and another
826 * one for the IO-APIC.
827 */
828 if (!smp_found_config && detect_init_APIC()) {
829 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
830 apic_phys = __pa(apic_phys);
831 } else
832 apic_phys = mp_lapic_addr;
833
834 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
835 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
836 APIC_BASE, apic_phys);
837
838 /* Put local APIC into the resource map. */
839 lapic_resource.start = apic_phys;
840 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
841 insert_resource(&iomem_resource, &lapic_resource);
842
843 /*
844 * Fetch the APIC ID of the BSP in case we have a
845 * default configuration (or the MP table is broken).
846 */
847 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
848 }
849
850 /*
851 * This initializes the IO-APIC and APIC hardware if this is
852 * a UP kernel.
853 */
854 int __init APIC_init_uniprocessor(void)
855 {
856 if (disable_apic) {
857 printk(KERN_INFO "Apic disabled\n");
858 return -1;
859 }
860 if (!cpu_has_apic) {
861 disable_apic = 1;
862 printk(KERN_INFO "Apic disabled by BIOS\n");
863 return -1;
864 }
865
866 verify_local_APIC();
867
868 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
869 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
870
871 setup_local_APIC();
872
873 /*
874 * Now enable IO-APICs, actually call clear_IO_APIC
875 * We need clear_IO_APIC before enabling vector on BP
876 */
877 if (!skip_ioapic_setup && nr_ioapics)
878 enable_IO_APIC();
879
880 end_local_APIC_setup();
881
882 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
883 setup_IO_APIC();
884 else
885 nr_ioapics = 0;
886 setup_boot_APIC_clock();
887 check_nmi_watchdog();
888 return 0;
889 }
890
891 /*
892 * Local APIC interrupts
893 */
894
895 /*
896 * This interrupt should _never_ happen with our APIC/SMP architecture
897 */
898 asmlinkage void smp_spurious_interrupt(void)
899 {
900 unsigned int v;
901 exit_idle();
902 irq_enter();
903 /*
904 * Check if this really is a spurious interrupt and ACK it
905 * if it is a vectored one. Just in case...
906 * Spurious interrupts should not be ACKed.
907 */
908 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
909 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
910 ack_APIC_irq();
911
912 add_pda(irq_spurious_count, 1);
913 irq_exit();
914 }
915
916 /*
917 * This interrupt should never happen with our APIC/SMP architecture
918 */
919 asmlinkage void smp_error_interrupt(void)
920 {
921 unsigned int v, v1;
922
923 exit_idle();
924 irq_enter();
925 /* First tickle the hardware, only then report what went on. -- REW */
926 v = apic_read(APIC_ESR);
927 apic_write(APIC_ESR, 0);
928 v1 = apic_read(APIC_ESR);
929 ack_APIC_irq();
930 atomic_inc(&irq_err_count);
931
932 /* Here is what the APIC error bits mean:
933 0: Send CS error
934 1: Receive CS error
935 2: Send accept error
936 3: Receive accept error
937 4: Reserved
938 5: Send illegal vector
939 6: Received illegal vector
940 7: Illegal register address
941 */
942 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
943 smp_processor_id(), v , v1);
944 irq_exit();
945 }
946
947 void disconnect_bsp_APIC(int virt_wire_setup)
948 {
949 /* Go back to Virtual Wire compatibility mode */
950 unsigned long value;
951
952 /* For the spurious interrupt use vector F, and enable it */
953 value = apic_read(APIC_SPIV);
954 value &= ~APIC_VECTOR_MASK;
955 value |= APIC_SPIV_APIC_ENABLED;
956 value |= 0xf;
957 apic_write(APIC_SPIV, value);
958
959 if (!virt_wire_setup) {
960 /*
961 * For LVT0 make it edge triggered, active high,
962 * external and enabled
963 */
964 value = apic_read(APIC_LVT0);
965 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
966 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
967 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
968 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
969 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
970 apic_write(APIC_LVT0, value);
971 } else {
972 /* Disable LVT0 */
973 apic_write(APIC_LVT0, APIC_LVT_MASKED);
974 }
975
976 /* For LVT1 make it edge triggered, active high, nmi and enabled */
977 value = apic_read(APIC_LVT1);
978 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
979 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
980 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
981 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
982 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
983 apic_write(APIC_LVT1, value);
984 }
985
986 /*
987 * Power management
988 */
989 #ifdef CONFIG_PM
990
991 static struct {
992 /* 'active' is true if the local APIC was enabled by us and
993 not the BIOS; this signifies that we are also responsible
994 for disabling it before entering apm/acpi suspend */
995 int active;
996 /* r/w apic fields */
997 unsigned int apic_id;
998 unsigned int apic_taskpri;
999 unsigned int apic_ldr;
1000 unsigned int apic_dfr;
1001 unsigned int apic_spiv;
1002 unsigned int apic_lvtt;
1003 unsigned int apic_lvtpc;
1004 unsigned int apic_lvt0;
1005 unsigned int apic_lvt1;
1006 unsigned int apic_lvterr;
1007 unsigned int apic_tmict;
1008 unsigned int apic_tdcr;
1009 unsigned int apic_thmr;
1010 } apic_pm_state;
1011
1012 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1013 {
1014 unsigned long flags;
1015 int maxlvt;
1016
1017 if (!apic_pm_state.active)
1018 return 0;
1019
1020 maxlvt = lapic_get_maxlvt();
1021
1022 apic_pm_state.apic_id = apic_read(APIC_ID);
1023 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1024 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1025 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1026 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1027 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1028 if (maxlvt >= 4)
1029 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1030 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1031 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1032 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1033 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1034 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1035 #ifdef CONFIG_X86_MCE_INTEL
1036 if (maxlvt >= 5)
1037 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1038 #endif
1039 local_irq_save(flags);
1040 disable_local_APIC();
1041 local_irq_restore(flags);
1042 return 0;
1043 }
1044
1045 static int lapic_resume(struct sys_device *dev)
1046 {
1047 unsigned int l, h;
1048 unsigned long flags;
1049 int maxlvt;
1050
1051 if (!apic_pm_state.active)
1052 return 0;
1053
1054 maxlvt = lapic_get_maxlvt();
1055
1056 local_irq_save(flags);
1057 rdmsr(MSR_IA32_APICBASE, l, h);
1058 l &= ~MSR_IA32_APICBASE_BASE;
1059 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1060 wrmsr(MSR_IA32_APICBASE, l, h);
1061 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1062 apic_write(APIC_ID, apic_pm_state.apic_id);
1063 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1064 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1065 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1066 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1067 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1068 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1069 #ifdef CONFIG_X86_MCE_INTEL
1070 if (maxlvt >= 5)
1071 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1072 #endif
1073 if (maxlvt >= 4)
1074 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1075 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1076 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1077 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1078 apic_write(APIC_ESR, 0);
1079 apic_read(APIC_ESR);
1080 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1081 apic_write(APIC_ESR, 0);
1082 apic_read(APIC_ESR);
1083 local_irq_restore(flags);
1084 return 0;
1085 }
1086
1087 static struct sysdev_class lapic_sysclass = {
1088 .name = "lapic",
1089 .resume = lapic_resume,
1090 .suspend = lapic_suspend,
1091 };
1092
1093 static struct sys_device device_lapic = {
1094 .id = 0,
1095 .cls = &lapic_sysclass,
1096 };
1097
1098 static void __cpuinit apic_pm_activate(void)
1099 {
1100 apic_pm_state.active = 1;
1101 }
1102
1103 static int __init init_lapic_sysfs(void)
1104 {
1105 int error;
1106 if (!cpu_has_apic)
1107 return 0;
1108 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1109 error = sysdev_class_register(&lapic_sysclass);
1110 if (!error)
1111 error = sysdev_register(&device_lapic);
1112 return error;
1113 }
1114 device_initcall(init_lapic_sysfs);
1115
1116 #else /* CONFIG_PM */
1117
1118 static void apic_pm_activate(void) { }
1119
1120 #endif /* CONFIG_PM */
1121
1122 /*
1123 * apic_is_clustered_box() -- Check if we can expect good TSC
1124 *
1125 * Thus far, the major user of this is IBM's Summit2 series:
1126 *
1127 * Clustered boxes may have unsynced TSC problems if they are
1128 * multi-chassis. Use available data to take a good guess.
1129 * If in doubt, go HPET.
1130 */
1131 __cpuinit int apic_is_clustered_box(void)
1132 {
1133 int i, clusters, zeros;
1134 unsigned id;
1135 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1136
1137 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1138
1139 for (i = 0; i < NR_CPUS; i++) {
1140 id = bios_cpu_apicid[i];
1141 if (id != BAD_APICID)
1142 __set_bit(APIC_CLUSTERID(id), clustermap);
1143 }
1144
1145 /* Problem: Partially populated chassis may not have CPUs in some of
1146 * the APIC clusters they have been allocated. Only present CPUs have
1147 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1148 * clusters are allocated sequentially, count zeros only if they are
1149 * bounded by ones.
1150 */
1151 clusters = 0;
1152 zeros = 0;
1153 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1154 if (test_bit(i, clustermap)) {
1155 clusters += 1 + zeros;
1156 zeros = 0;
1157 } else
1158 ++zeros;
1159 }
1160
1161 /*
1162 * If clusters > 2, then should be multi-chassis.
1163 * May have to revisit this when multi-core + hyperthreaded CPUs come
1164 * out, but AFAIK this will work even for them.
1165 */
1166 return (clusters > 2);
1167 }
1168
1169 /*
1170 * APIC command line parameters
1171 */
1172 static int __init apic_set_verbosity(char *str)
1173 {
1174 if (str == NULL) {
1175 skip_ioapic_setup = 0;
1176 ioapic_force = 1;
1177 return 0;
1178 }
1179 if (strcmp("debug", str) == 0)
1180 apic_verbosity = APIC_DEBUG;
1181 else if (strcmp("verbose", str) == 0)
1182 apic_verbosity = APIC_VERBOSE;
1183 else {
1184 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1185 " use apic=verbose or apic=debug\n", str);
1186 return -EINVAL;
1187 }
1188
1189 return 0;
1190 }
1191 early_param("apic", apic_set_verbosity);
1192
1193 static __init int setup_disableapic(char *str)
1194 {
1195 disable_apic = 1;
1196 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1197 return 0;
1198 }
1199 early_param("disableapic", setup_disableapic);
1200
1201 /* same as disableapic, for compatibility */
1202 static __init int setup_nolapic(char *str)
1203 {
1204 return setup_disableapic(str);
1205 }
1206 early_param("nolapic", setup_nolapic);
1207
1208 static int __init parse_lapic_timer_c2_ok(char *arg)
1209 {
1210 local_apic_timer_c2_ok = 1;
1211 return 0;
1212 }
1213 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1214
1215 static __init int setup_noapictimer(char *str)
1216 {
1217 if (str[0] != ' ' && str[0] != 0)
1218 return 0;
1219 disable_apic_timer = 1;
1220 return 1;
1221 }
1222 __setup("noapictimer", setup_noapictimer);
1223
1224 static __init int setup_apicpmtimer(char *s)
1225 {
1226 apic_calibrate_pmtmr = 1;
1227 notsc_setup(NULL);
1228 return 0;
1229 }
1230 __setup("apicpmtimer", setup_apicpmtimer);
1231
This page took 0.083284 seconds and 5 git commands to generate.