x86: apic - introduce imcr_ helpers
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / kernel / apic / apic.c
1 /*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000, 2009 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/kernel_stat.h>
18 #include <linux/mc146818rtc.h>
19 #include <linux/acpi_pmtmr.h>
20 #include <linux/clockchips.h>
21 #include <linux/interrupt.h>
22 #include <linux/bootmem.h>
23 #include <linux/ftrace.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/sysdev.h>
27 #include <linux/delay.h>
28 #include <linux/timex.h>
29 #include <linux/dmar.h>
30 #include <linux/init.h>
31 #include <linux/cpu.h>
32 #include <linux/dmi.h>
33 #include <linux/nmi.h>
34 #include <linux/smp.h>
35 #include <linux/mm.h>
36
37 #include <asm/pgalloc.h>
38 #include <asm/atomic.h>
39 #include <asm/mpspec.h>
40 #include <asm/i8253.h>
41 #include <asm/i8259.h>
42 #include <asm/proto.h>
43 #include <asm/apic.h>
44 #include <asm/desc.h>
45 #include <asm/hpet.h>
46 #include <asm/idle.h>
47 #include <asm/mtrr.h>
48 #include <asm/smp.h>
49 #include <asm/mce.h>
50
51 unsigned int num_processors;
52
53 unsigned disabled_cpus __cpuinitdata;
54
55 /* Processor that is doing the boot up */
56 unsigned int boot_cpu_physical_apicid = -1U;
57
58 /*
59 * The highest APIC ID seen during enumeration.
60 *
61 * This determines the messaging protocol we can use: if all APIC IDs
62 * are in the 0 ... 7 range, then we can use logical addressing which
63 * has some performance advantages (better broadcasting).
64 *
65 * If there's an APIC ID above 8, we use physical addressing.
66 */
67 unsigned int max_physical_apicid;
68
69 /*
70 * Bitmask of physically existing CPUs:
71 */
72 physid_mask_t phys_cpu_present_map;
73
74 /*
75 * Map cpu index to physical APIC ID
76 */
77 DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
78 DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
79 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
80 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
81
82 #ifdef CONFIG_X86_32
83 /*
84 * Knob to control our willingness to enable the local APIC.
85 *
86 * +1=force-enable
87 */
88 static int force_enable_local_apic;
89 /*
90 * APIC command line parameters
91 */
92 static int __init parse_lapic(char *arg)
93 {
94 force_enable_local_apic = 1;
95 return 0;
96 }
97 early_param("lapic", parse_lapic);
98 /* Local APIC was disabled by the BIOS and enabled by the kernel */
99 static int enabled_via_apicbase;
100
101 /*
102 * Handle interrupt mode configuration register (IMCR).
103 * This register controls whether the interrupt signals
104 * that reach the BSP come from the master PIC or from the
105 * local APIC. Before entering Symmetric I/O Mode, either
106 * the BIOS or the operating system must switch out of
107 * PIC Mode by changing the IMCR.
108 */
109 static inline imcr_pic_to_apic(void)
110 {
111 /* select IMCR register */
112 outb(0x70, 0x22);
113 /* NMI and 8259 INTR go through APIC */
114 outb(0x01, 0x23);
115 }
116
117 static inline imcr_apic_to_pic(void)
118 {
119 /* select IMCR register */
120 outb(0x70, 0x22);
121 /* NMI and 8259 INTR go directly to BSP */
122 outb(0x00, 0x23);
123 }
124 #endif
125
126 #ifdef CONFIG_X86_64
127 static int apic_calibrate_pmtmr __initdata;
128 static __init int setup_apicpmtimer(char *s)
129 {
130 apic_calibrate_pmtmr = 1;
131 notsc_setup(NULL);
132 return 0;
133 }
134 __setup("apicpmtimer", setup_apicpmtimer);
135 #endif
136
137 #ifdef CONFIG_X86_X2APIC
138 int x2apic;
139 /* x2apic enabled before OS handover */
140 static int x2apic_preenabled;
141 static int disable_x2apic;
142 static __init int setup_nox2apic(char *str)
143 {
144 disable_x2apic = 1;
145 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
146 return 0;
147 }
148 early_param("nox2apic", setup_nox2apic);
149 #endif
150
151 unsigned long mp_lapic_addr;
152 int disable_apic;
153 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
154 static int disable_apic_timer __cpuinitdata;
155 /* Local APIC timer works in C2 */
156 int local_apic_timer_c2_ok;
157 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
158
159 int first_system_vector = 0xfe;
160
161 /*
162 * Debug level, exported for io_apic.c
163 */
164 unsigned int apic_verbosity;
165
166 int pic_mode;
167
168 /* Have we found an MP table */
169 int smp_found_config;
170
171 static struct resource lapic_resource = {
172 .name = "Local APIC",
173 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
174 };
175
176 static unsigned int calibration_result;
177
178 static int lapic_next_event(unsigned long delta,
179 struct clock_event_device *evt);
180 static void lapic_timer_setup(enum clock_event_mode mode,
181 struct clock_event_device *evt);
182 static void lapic_timer_broadcast(const struct cpumask *mask);
183 static void apic_pm_activate(void);
184
185 /*
186 * The local apic timer can be used for any function which is CPU local.
187 */
188 static struct clock_event_device lapic_clockevent = {
189 .name = "lapic",
190 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
191 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
192 .shift = 32,
193 .set_mode = lapic_timer_setup,
194 .set_next_event = lapic_next_event,
195 .broadcast = lapic_timer_broadcast,
196 .rating = 100,
197 .irq = -1,
198 };
199 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
200
201 static unsigned long apic_phys;
202
203 /*
204 * Get the LAPIC version
205 */
206 static inline int lapic_get_version(void)
207 {
208 return GET_APIC_VERSION(apic_read(APIC_LVR));
209 }
210
211 /*
212 * Check, if the APIC is integrated or a separate chip
213 */
214 static inline int lapic_is_integrated(void)
215 {
216 #ifdef CONFIG_X86_64
217 return 1;
218 #else
219 return APIC_INTEGRATED(lapic_get_version());
220 #endif
221 }
222
223 /*
224 * Check, whether this is a modern or a first generation APIC
225 */
226 static int modern_apic(void)
227 {
228 /* AMD systems use old APIC versions, so check the CPU */
229 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
230 boot_cpu_data.x86 >= 0xf)
231 return 1;
232 return lapic_get_version() >= 0x14;
233 }
234
235 void native_apic_wait_icr_idle(void)
236 {
237 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
238 cpu_relax();
239 }
240
241 u32 native_safe_apic_wait_icr_idle(void)
242 {
243 u32 send_status;
244 int timeout;
245
246 timeout = 0;
247 do {
248 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
249 if (!send_status)
250 break;
251 udelay(100);
252 } while (timeout++ < 1000);
253
254 return send_status;
255 }
256
257 void native_apic_icr_write(u32 low, u32 id)
258 {
259 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
260 apic_write(APIC_ICR, low);
261 }
262
263 u64 native_apic_icr_read(void)
264 {
265 u32 icr1, icr2;
266
267 icr2 = apic_read(APIC_ICR2);
268 icr1 = apic_read(APIC_ICR);
269
270 return icr1 | ((u64)icr2 << 32);
271 }
272
273 /**
274 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
275 */
276 void __cpuinit enable_NMI_through_LVT0(void)
277 {
278 unsigned int v;
279
280 /* unmask and set to NMI */
281 v = APIC_DM_NMI;
282
283 /* Level triggered for 82489DX (32bit mode) */
284 if (!lapic_is_integrated())
285 v |= APIC_LVT_LEVEL_TRIGGER;
286
287 apic_write(APIC_LVT0, v);
288 }
289
290 #ifdef CONFIG_X86_32
291 /**
292 * get_physical_broadcast - Get number of physical broadcast IDs
293 */
294 int get_physical_broadcast(void)
295 {
296 return modern_apic() ? 0xff : 0xf;
297 }
298 #endif
299
300 /**
301 * lapic_get_maxlvt - get the maximum number of local vector table entries
302 */
303 int lapic_get_maxlvt(void)
304 {
305 unsigned int v;
306
307 v = apic_read(APIC_LVR);
308 /*
309 * - we always have APIC integrated on 64bit mode
310 * - 82489DXs do not report # of LVT entries
311 */
312 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
313 }
314
315 /*
316 * Local APIC timer
317 */
318
319 /* Clock divisor */
320 #define APIC_DIVISOR 16
321
322 /*
323 * This function sets up the local APIC timer, with a timeout of
324 * 'clocks' APIC bus clock. During calibration we actually call
325 * this function twice on the boot CPU, once with a bogus timeout
326 * value, second time for real. The other (noncalibrating) CPUs
327 * call this function only once, with the real, calibrated value.
328 *
329 * We do reads before writes even if unnecessary, to get around the
330 * P5 APIC double write bug.
331 */
332 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
333 {
334 unsigned int lvtt_value, tmp_value;
335
336 lvtt_value = LOCAL_TIMER_VECTOR;
337 if (!oneshot)
338 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
339 if (!lapic_is_integrated())
340 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
341
342 if (!irqen)
343 lvtt_value |= APIC_LVT_MASKED;
344
345 apic_write(APIC_LVTT, lvtt_value);
346
347 /*
348 * Divide PICLK by 16
349 */
350 tmp_value = apic_read(APIC_TDCR);
351 apic_write(APIC_TDCR,
352 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
353 APIC_TDR_DIV_16);
354
355 if (!oneshot)
356 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
357 }
358
359 /*
360 * Setup extended LVT, AMD specific (K8, family 10h)
361 *
362 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
363 * MCE interrupts are supported. Thus MCE offset must be set to 0.
364 *
365 * If mask=1, the LVT entry does not generate interrupts while mask=0
366 * enables the vector. See also the BKDGs.
367 */
368
369 #define APIC_EILVT_LVTOFF_MCE 0
370 #define APIC_EILVT_LVTOFF_IBS 1
371
372 static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
373 {
374 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
375 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
376
377 apic_write(reg, v);
378 }
379
380 u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
381 {
382 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
383 return APIC_EILVT_LVTOFF_MCE;
384 }
385
386 u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
387 {
388 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
389 return APIC_EILVT_LVTOFF_IBS;
390 }
391 EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
392
393 /*
394 * Program the next event, relative to now
395 */
396 static int lapic_next_event(unsigned long delta,
397 struct clock_event_device *evt)
398 {
399 apic_write(APIC_TMICT, delta);
400 return 0;
401 }
402
403 /*
404 * Setup the lapic timer in periodic or oneshot mode
405 */
406 static void lapic_timer_setup(enum clock_event_mode mode,
407 struct clock_event_device *evt)
408 {
409 unsigned long flags;
410 unsigned int v;
411
412 /* Lapic used as dummy for broadcast ? */
413 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
414 return;
415
416 local_irq_save(flags);
417
418 switch (mode) {
419 case CLOCK_EVT_MODE_PERIODIC:
420 case CLOCK_EVT_MODE_ONESHOT:
421 __setup_APIC_LVTT(calibration_result,
422 mode != CLOCK_EVT_MODE_PERIODIC, 1);
423 break;
424 case CLOCK_EVT_MODE_UNUSED:
425 case CLOCK_EVT_MODE_SHUTDOWN:
426 v = apic_read(APIC_LVTT);
427 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
428 apic_write(APIC_LVTT, v);
429 apic_write(APIC_TMICT, 0xffffffff);
430 break;
431 case CLOCK_EVT_MODE_RESUME:
432 /* Nothing to do here */
433 break;
434 }
435
436 local_irq_restore(flags);
437 }
438
439 /*
440 * Local APIC timer broadcast function
441 */
442 static void lapic_timer_broadcast(const struct cpumask *mask)
443 {
444 #ifdef CONFIG_SMP
445 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
446 #endif
447 }
448
449 /*
450 * Setup the local APIC timer for this CPU. Copy the initilized values
451 * of the boot CPU and register the clock event in the framework.
452 */
453 static void __cpuinit setup_APIC_timer(void)
454 {
455 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
456
457 memcpy(levt, &lapic_clockevent, sizeof(*levt));
458 levt->cpumask = cpumask_of(smp_processor_id());
459
460 clockevents_register_device(levt);
461 }
462
463 /*
464 * In this functions we calibrate APIC bus clocks to the external timer.
465 *
466 * We want to do the calibration only once since we want to have local timer
467 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
468 * frequency.
469 *
470 * This was previously done by reading the PIT/HPET and waiting for a wrap
471 * around to find out, that a tick has elapsed. I have a box, where the PIT
472 * readout is broken, so it never gets out of the wait loop again. This was
473 * also reported by others.
474 *
475 * Monitoring the jiffies value is inaccurate and the clockevents
476 * infrastructure allows us to do a simple substitution of the interrupt
477 * handler.
478 *
479 * The calibration routine also uses the pm_timer when possible, as the PIT
480 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
481 * back to normal later in the boot process).
482 */
483
484 #define LAPIC_CAL_LOOPS (HZ/10)
485
486 static __initdata int lapic_cal_loops = -1;
487 static __initdata long lapic_cal_t1, lapic_cal_t2;
488 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
489 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
490 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
491
492 /*
493 * Temporary interrupt handler.
494 */
495 static void __init lapic_cal_handler(struct clock_event_device *dev)
496 {
497 unsigned long long tsc = 0;
498 long tapic = apic_read(APIC_TMCCT);
499 unsigned long pm = acpi_pm_read_early();
500
501 if (cpu_has_tsc)
502 rdtscll(tsc);
503
504 switch (lapic_cal_loops++) {
505 case 0:
506 lapic_cal_t1 = tapic;
507 lapic_cal_tsc1 = tsc;
508 lapic_cal_pm1 = pm;
509 lapic_cal_j1 = jiffies;
510 break;
511
512 case LAPIC_CAL_LOOPS:
513 lapic_cal_t2 = tapic;
514 lapic_cal_tsc2 = tsc;
515 if (pm < lapic_cal_pm1)
516 pm += ACPI_PM_OVRRUN;
517 lapic_cal_pm2 = pm;
518 lapic_cal_j2 = jiffies;
519 break;
520 }
521 }
522
523 static int __init
524 calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
525 {
526 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
527 const long pm_thresh = pm_100ms / 100;
528 unsigned long mult;
529 u64 res;
530
531 #ifndef CONFIG_X86_PM_TIMER
532 return -1;
533 #endif
534
535 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
536
537 /* Check, if the PM timer is available */
538 if (!deltapm)
539 return -1;
540
541 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
542
543 if (deltapm > (pm_100ms - pm_thresh) &&
544 deltapm < (pm_100ms + pm_thresh)) {
545 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
546 return 0;
547 }
548
549 res = (((u64)deltapm) * mult) >> 22;
550 do_div(res, 1000000);
551 pr_warning("APIC calibration not consistent "
552 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
553
554 /* Correct the lapic counter value */
555 res = (((u64)(*delta)) * pm_100ms);
556 do_div(res, deltapm);
557 pr_info("APIC delta adjusted to PM-Timer: "
558 "%lu (%ld)\n", (unsigned long)res, *delta);
559 *delta = (long)res;
560
561 /* Correct the tsc counter value */
562 if (cpu_has_tsc) {
563 res = (((u64)(*deltatsc)) * pm_100ms);
564 do_div(res, deltapm);
565 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
566 "PM-Timer: %lu (%ld) \n",
567 (unsigned long)res, *deltatsc);
568 *deltatsc = (long)res;
569 }
570
571 return 0;
572 }
573
574 static int __init calibrate_APIC_clock(void)
575 {
576 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
577 void (*real_handler)(struct clock_event_device *dev);
578 unsigned long deltaj;
579 long delta, deltatsc;
580 int pm_referenced = 0;
581
582 local_irq_disable();
583
584 /* Replace the global interrupt handler */
585 real_handler = global_clock_event->event_handler;
586 global_clock_event->event_handler = lapic_cal_handler;
587
588 /*
589 * Setup the APIC counter to maximum. There is no way the lapic
590 * can underflow in the 100ms detection time frame
591 */
592 __setup_APIC_LVTT(0xffffffff, 0, 0);
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 /* Restore the real event handler */
603 global_clock_event->event_handler = real_handler;
604
605 /* Build delta t1-t2 as apic timer counts down */
606 delta = lapic_cal_t1 - lapic_cal_t2;
607 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
608
609 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
610
611 /* we trust the PM based calibration if possible */
612 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
613 &delta, &deltatsc);
614
615 /* Calculate the scaled math multiplication factor */
616 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
617 lapic_clockevent.shift);
618 lapic_clockevent.max_delta_ns =
619 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
620 lapic_clockevent.min_delta_ns =
621 clockevent_delta2ns(0xF, &lapic_clockevent);
622
623 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
624
625 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
626 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
627 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
628 calibration_result);
629
630 if (cpu_has_tsc) {
631 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
632 "%ld.%04ld MHz.\n",
633 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
634 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
635 }
636
637 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
638 "%u.%04u MHz.\n",
639 calibration_result / (1000000 / HZ),
640 calibration_result % (1000000 / HZ));
641
642 /*
643 * Do a sanity check on the APIC calibration result
644 */
645 if (calibration_result < (1000000 / HZ)) {
646 local_irq_enable();
647 pr_warning("APIC frequency too slow, disabling apic timer\n");
648 return -1;
649 }
650
651 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
652
653 /*
654 * PM timer calibration failed or not turned on
655 * so lets try APIC timer based calibration
656 */
657 if (!pm_referenced) {
658 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
659
660 /*
661 * Setup the apic timer manually
662 */
663 levt->event_handler = lapic_cal_handler;
664 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
665 lapic_cal_loops = -1;
666
667 /* Let the interrupts run */
668 local_irq_enable();
669
670 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
671 cpu_relax();
672
673 /* Stop the lapic timer */
674 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
675
676 /* Jiffies delta */
677 deltaj = lapic_cal_j2 - lapic_cal_j1;
678 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
679
680 /* Check, if the jiffies result is consistent */
681 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
682 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
683 else
684 levt->features |= CLOCK_EVT_FEAT_DUMMY;
685 } else
686 local_irq_enable();
687
688 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
689 pr_warning("APIC timer disabled due to verification failure\n");
690 return -1;
691 }
692
693 return 0;
694 }
695
696 /*
697 * Setup the boot APIC
698 *
699 * Calibrate and verify the result.
700 */
701 void __init setup_boot_APIC_clock(void)
702 {
703 /*
704 * The local apic timer can be disabled via the kernel
705 * commandline or from the CPU detection code. Register the lapic
706 * timer as a dummy clock event source on SMP systems, so the
707 * broadcast mechanism is used. On UP systems simply ignore it.
708 */
709 if (disable_apic_timer) {
710 pr_info("Disabling APIC timer\n");
711 /* No broadcast on UP ! */
712 if (num_possible_cpus() > 1) {
713 lapic_clockevent.mult = 1;
714 setup_APIC_timer();
715 }
716 return;
717 }
718
719 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
720 "calibrating APIC timer ...\n");
721
722 if (calibrate_APIC_clock()) {
723 /* No broadcast on UP ! */
724 if (num_possible_cpus() > 1)
725 setup_APIC_timer();
726 return;
727 }
728
729 /*
730 * If nmi_watchdog is set to IO_APIC, we need the
731 * PIT/HPET going. Otherwise register lapic as a dummy
732 * device.
733 */
734 if (nmi_watchdog != NMI_IO_APIC)
735 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
736 else
737 pr_warning("APIC timer registered as dummy,"
738 " due to nmi_watchdog=%d!\n", nmi_watchdog);
739
740 /* Setup the lapic or request the broadcast */
741 setup_APIC_timer();
742 }
743
744 void __cpuinit setup_secondary_APIC_clock(void)
745 {
746 setup_APIC_timer();
747 }
748
749 /*
750 * The guts of the apic timer interrupt
751 */
752 static void local_apic_timer_interrupt(void)
753 {
754 int cpu = smp_processor_id();
755 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
756
757 /*
758 * Normally we should not be here till LAPIC has been initialized but
759 * in some cases like kdump, its possible that there is a pending LAPIC
760 * timer interrupt from previous kernel's context and is delivered in
761 * new kernel the moment interrupts are enabled.
762 *
763 * Interrupts are enabled early and LAPIC is setup much later, hence
764 * its possible that when we get here evt->event_handler is NULL.
765 * Check for event_handler being NULL and discard the interrupt as
766 * spurious.
767 */
768 if (!evt->event_handler) {
769 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
770 /* Switch it off */
771 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
772 return;
773 }
774
775 /*
776 * the NMI deadlock-detector uses this.
777 */
778 inc_irq_stat(apic_timer_irqs);
779
780 evt->event_handler(evt);
781 }
782
783 /*
784 * Local APIC timer interrupt. This is the most natural way for doing
785 * local interrupts, but local timer interrupts can be emulated by
786 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
787 *
788 * [ if a single-CPU system runs an SMP kernel then we call the local
789 * interrupt as well. Thus we cannot inline the local irq ... ]
790 */
791 void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
792 {
793 struct pt_regs *old_regs = set_irq_regs(regs);
794
795 /*
796 * NOTE! We'd better ACK the irq immediately,
797 * because timer handling can be slow.
798 */
799 ack_APIC_irq();
800 /*
801 * update_process_times() expects us to have done irq_enter().
802 * Besides, if we don't timer interrupts ignore the global
803 * interrupt lock, which is the WrongThing (tm) to do.
804 */
805 exit_idle();
806 irq_enter();
807 local_apic_timer_interrupt();
808 irq_exit();
809
810 set_irq_regs(old_regs);
811 }
812
813 int setup_profiling_timer(unsigned int multiplier)
814 {
815 return -EINVAL;
816 }
817
818 /*
819 * Local APIC start and shutdown
820 */
821
822 /**
823 * clear_local_APIC - shutdown the local APIC
824 *
825 * This is called, when a CPU is disabled and before rebooting, so the state of
826 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
827 * leftovers during boot.
828 */
829 void clear_local_APIC(void)
830 {
831 int maxlvt;
832 u32 v;
833
834 /* APIC hasn't been mapped yet */
835 if (!x2apic && !apic_phys)
836 return;
837
838 maxlvt = lapic_get_maxlvt();
839 /*
840 * Masking an LVT entry can trigger a local APIC error
841 * if the vector is zero. Mask LVTERR first to prevent this.
842 */
843 if (maxlvt >= 3) {
844 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
845 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
846 }
847 /*
848 * Careful: we have to set masks only first to deassert
849 * any level-triggered sources.
850 */
851 v = apic_read(APIC_LVTT);
852 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
853 v = apic_read(APIC_LVT0);
854 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
855 v = apic_read(APIC_LVT1);
856 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
857 if (maxlvt >= 4) {
858 v = apic_read(APIC_LVTPC);
859 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
860 }
861
862 /* lets not touch this if we didn't frob it */
863 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
864 if (maxlvt >= 5) {
865 v = apic_read(APIC_LVTTHMR);
866 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
867 }
868 #endif
869 #ifdef CONFIG_X86_MCE_INTEL
870 if (maxlvt >= 6) {
871 v = apic_read(APIC_LVTCMCI);
872 if (!(v & APIC_LVT_MASKED))
873 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
874 }
875 #endif
876
877 /*
878 * Clean APIC state for other OSs:
879 */
880 apic_write(APIC_LVTT, APIC_LVT_MASKED);
881 apic_write(APIC_LVT0, APIC_LVT_MASKED);
882 apic_write(APIC_LVT1, APIC_LVT_MASKED);
883 if (maxlvt >= 3)
884 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
885 if (maxlvt >= 4)
886 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
887
888 /* Integrated APIC (!82489DX) ? */
889 if (lapic_is_integrated()) {
890 if (maxlvt > 3)
891 /* Clear ESR due to Pentium errata 3AP and 11AP */
892 apic_write(APIC_ESR, 0);
893 apic_read(APIC_ESR);
894 }
895 }
896
897 /**
898 * disable_local_APIC - clear and disable the local APIC
899 */
900 void disable_local_APIC(void)
901 {
902 unsigned int value;
903
904 /* APIC hasn't been mapped yet */
905 if (!apic_phys)
906 return;
907
908 clear_local_APIC();
909
910 /*
911 * Disable APIC (implies clearing of registers
912 * for 82489DX!).
913 */
914 value = apic_read(APIC_SPIV);
915 value &= ~APIC_SPIV_APIC_ENABLED;
916 apic_write(APIC_SPIV, value);
917
918 #ifdef CONFIG_X86_32
919 /*
920 * When LAPIC was disabled by the BIOS and enabled by the kernel,
921 * restore the disabled state.
922 */
923 if (enabled_via_apicbase) {
924 unsigned int l, h;
925
926 rdmsr(MSR_IA32_APICBASE, l, h);
927 l &= ~MSR_IA32_APICBASE_ENABLE;
928 wrmsr(MSR_IA32_APICBASE, l, h);
929 }
930 #endif
931 }
932
933 /*
934 * If Linux enabled the LAPIC against the BIOS default disable it down before
935 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
936 * not power-off. Additionally clear all LVT entries before disable_local_APIC
937 * for the case where Linux didn't enable the LAPIC.
938 */
939 void lapic_shutdown(void)
940 {
941 unsigned long flags;
942
943 if (!cpu_has_apic)
944 return;
945
946 local_irq_save(flags);
947
948 #ifdef CONFIG_X86_32
949 if (!enabled_via_apicbase)
950 clear_local_APIC();
951 else
952 #endif
953 disable_local_APIC();
954
955
956 local_irq_restore(flags);
957 }
958
959 /*
960 * This is to verify that we're looking at a real local APIC.
961 * Check these against your board if the CPUs aren't getting
962 * started for no apparent reason.
963 */
964 int __init verify_local_APIC(void)
965 {
966 unsigned int reg0, reg1;
967
968 /*
969 * The version register is read-only in a real APIC.
970 */
971 reg0 = apic_read(APIC_LVR);
972 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
973 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
974 reg1 = apic_read(APIC_LVR);
975 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
976
977 /*
978 * The two version reads above should print the same
979 * numbers. If the second one is different, then we
980 * poke at a non-APIC.
981 */
982 if (reg1 != reg0)
983 return 0;
984
985 /*
986 * Check if the version looks reasonably.
987 */
988 reg1 = GET_APIC_VERSION(reg0);
989 if (reg1 == 0x00 || reg1 == 0xff)
990 return 0;
991 reg1 = lapic_get_maxlvt();
992 if (reg1 < 0x02 || reg1 == 0xff)
993 return 0;
994
995 /*
996 * The ID register is read/write in a real APIC.
997 */
998 reg0 = apic_read(APIC_ID);
999 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
1000 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
1001 reg1 = apic_read(APIC_ID);
1002 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1003 apic_write(APIC_ID, reg0);
1004 if (reg1 != (reg0 ^ apic->apic_id_mask))
1005 return 0;
1006
1007 /*
1008 * The next two are just to see if we have sane values.
1009 * They're only really relevant if we're in Virtual Wire
1010 * compatibility mode, but most boxes are anymore.
1011 */
1012 reg0 = apic_read(APIC_LVT0);
1013 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1014 reg1 = apic_read(APIC_LVT1);
1015 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1016
1017 return 1;
1018 }
1019
1020 /**
1021 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1022 */
1023 void __init sync_Arb_IDs(void)
1024 {
1025 /*
1026 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1027 * needed on AMD.
1028 */
1029 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1030 return;
1031
1032 /*
1033 * Wait for idle.
1034 */
1035 apic_wait_icr_idle();
1036
1037 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1038 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1039 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1040 }
1041
1042 /*
1043 * An initial setup of the virtual wire mode.
1044 */
1045 void __init init_bsp_APIC(void)
1046 {
1047 unsigned int value;
1048
1049 /*
1050 * Don't do the setup now if we have a SMP BIOS as the
1051 * through-I/O-APIC virtual wire mode might be active.
1052 */
1053 if (smp_found_config || !cpu_has_apic)
1054 return;
1055
1056 /*
1057 * Do not trust the local APIC being empty at bootup.
1058 */
1059 clear_local_APIC();
1060
1061 /*
1062 * Enable APIC.
1063 */
1064 value = apic_read(APIC_SPIV);
1065 value &= ~APIC_VECTOR_MASK;
1066 value |= APIC_SPIV_APIC_ENABLED;
1067
1068 #ifdef CONFIG_X86_32
1069 /* This bit is reserved on P4/Xeon and should be cleared */
1070 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1071 (boot_cpu_data.x86 == 15))
1072 value &= ~APIC_SPIV_FOCUS_DISABLED;
1073 else
1074 #endif
1075 value |= APIC_SPIV_FOCUS_DISABLED;
1076 value |= SPURIOUS_APIC_VECTOR;
1077 apic_write(APIC_SPIV, value);
1078
1079 /*
1080 * Set up the virtual wire mode.
1081 */
1082 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1083 value = APIC_DM_NMI;
1084 if (!lapic_is_integrated()) /* 82489DX */
1085 value |= APIC_LVT_LEVEL_TRIGGER;
1086 apic_write(APIC_LVT1, value);
1087 }
1088
1089 static void __cpuinit lapic_setup_esr(void)
1090 {
1091 unsigned int oldvalue, value, maxlvt;
1092
1093 if (!lapic_is_integrated()) {
1094 pr_info("No ESR for 82489DX.\n");
1095 return;
1096 }
1097
1098 if (apic->disable_esr) {
1099 /*
1100 * Something untraceable is creating bad interrupts on
1101 * secondary quads ... for the moment, just leave the
1102 * ESR disabled - we can't do anything useful with the
1103 * errors anyway - mbligh
1104 */
1105 pr_info("Leaving ESR disabled.\n");
1106 return;
1107 }
1108
1109 maxlvt = lapic_get_maxlvt();
1110 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1111 apic_write(APIC_ESR, 0);
1112 oldvalue = apic_read(APIC_ESR);
1113
1114 /* enables sending errors */
1115 value = ERROR_APIC_VECTOR;
1116 apic_write(APIC_LVTERR, value);
1117
1118 /*
1119 * spec says clear errors after enabling vector.
1120 */
1121 if (maxlvt > 3)
1122 apic_write(APIC_ESR, 0);
1123 value = apic_read(APIC_ESR);
1124 if (value != oldvalue)
1125 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1126 "vector: 0x%08x after: 0x%08x\n",
1127 oldvalue, value);
1128 }
1129
1130
1131 /**
1132 * setup_local_APIC - setup the local APIC
1133 */
1134 void __cpuinit setup_local_APIC(void)
1135 {
1136 unsigned int value;
1137 int i, j;
1138
1139 if (disable_apic) {
1140 arch_disable_smp_support();
1141 return;
1142 }
1143
1144 #ifdef CONFIG_X86_32
1145 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1146 if (lapic_is_integrated() && apic->disable_esr) {
1147 apic_write(APIC_ESR, 0);
1148 apic_write(APIC_ESR, 0);
1149 apic_write(APIC_ESR, 0);
1150 apic_write(APIC_ESR, 0);
1151 }
1152 #endif
1153
1154 preempt_disable();
1155
1156 /*
1157 * Double-check whether this APIC is really registered.
1158 * This is meaningless in clustered apic mode, so we skip it.
1159 */
1160 if (!apic->apic_id_registered())
1161 BUG();
1162
1163 /*
1164 * Intel recommends to set DFR, LDR and TPR before enabling
1165 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1166 * document number 292116). So here it goes...
1167 */
1168 apic->init_apic_ldr();
1169
1170 /*
1171 * Set Task Priority to 'accept all'. We never change this
1172 * later on.
1173 */
1174 value = apic_read(APIC_TASKPRI);
1175 value &= ~APIC_TPRI_MASK;
1176 apic_write(APIC_TASKPRI, value);
1177
1178 /*
1179 * After a crash, we no longer service the interrupts and a pending
1180 * interrupt from previous kernel might still have ISR bit set.
1181 *
1182 * Most probably by now CPU has serviced that pending interrupt and
1183 * it might not have done the ack_APIC_irq() because it thought,
1184 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1185 * does not clear the ISR bit and cpu thinks it has already serivced
1186 * the interrupt. Hence a vector might get locked. It was noticed
1187 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1188 */
1189 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1190 value = apic_read(APIC_ISR + i*0x10);
1191 for (j = 31; j >= 0; j--) {
1192 if (value & (1<<j))
1193 ack_APIC_irq();
1194 }
1195 }
1196
1197 /*
1198 * Now that we are all set up, enable the APIC
1199 */
1200 value = apic_read(APIC_SPIV);
1201 value &= ~APIC_VECTOR_MASK;
1202 /*
1203 * Enable APIC
1204 */
1205 value |= APIC_SPIV_APIC_ENABLED;
1206
1207 #ifdef CONFIG_X86_32
1208 /*
1209 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1210 * certain networking cards. If high frequency interrupts are
1211 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1212 * entry is masked/unmasked at a high rate as well then sooner or
1213 * later IOAPIC line gets 'stuck', no more interrupts are received
1214 * from the device. If focus CPU is disabled then the hang goes
1215 * away, oh well :-(
1216 *
1217 * [ This bug can be reproduced easily with a level-triggered
1218 * PCI Ne2000 networking cards and PII/PIII processors, dual
1219 * BX chipset. ]
1220 */
1221 /*
1222 * Actually disabling the focus CPU check just makes the hang less
1223 * frequent as it makes the interrupt distributon model be more
1224 * like LRU than MRU (the short-term load is more even across CPUs).
1225 * See also the comment in end_level_ioapic_irq(). --macro
1226 */
1227
1228 /*
1229 * - enable focus processor (bit==0)
1230 * - 64bit mode always use processor focus
1231 * so no need to set it
1232 */
1233 value &= ~APIC_SPIV_FOCUS_DISABLED;
1234 #endif
1235
1236 /*
1237 * Set spurious IRQ vector
1238 */
1239 value |= SPURIOUS_APIC_VECTOR;
1240 apic_write(APIC_SPIV, value);
1241
1242 /*
1243 * Set up LVT0, LVT1:
1244 *
1245 * set up through-local-APIC on the BP's LINT0. This is not
1246 * strictly necessary in pure symmetric-IO mode, but sometimes
1247 * we delegate interrupts to the 8259A.
1248 */
1249 /*
1250 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1251 */
1252 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1253 if (!smp_processor_id() && (pic_mode || !value)) {
1254 value = APIC_DM_EXTINT;
1255 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1256 smp_processor_id());
1257 } else {
1258 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1259 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1260 smp_processor_id());
1261 }
1262 apic_write(APIC_LVT0, value);
1263
1264 /*
1265 * only the BP should see the LINT1 NMI signal, obviously.
1266 */
1267 if (!smp_processor_id())
1268 value = APIC_DM_NMI;
1269 else
1270 value = APIC_DM_NMI | APIC_LVT_MASKED;
1271 if (!lapic_is_integrated()) /* 82489DX */
1272 value |= APIC_LVT_LEVEL_TRIGGER;
1273 apic_write(APIC_LVT1, value);
1274
1275 preempt_enable();
1276
1277 #ifdef CONFIG_X86_MCE_INTEL
1278 /* Recheck CMCI information after local APIC is up on CPU #0 */
1279 if (smp_processor_id() == 0)
1280 cmci_recheck();
1281 #endif
1282 }
1283
1284 void __cpuinit end_local_APIC_setup(void)
1285 {
1286 lapic_setup_esr();
1287
1288 #ifdef CONFIG_X86_32
1289 {
1290 unsigned int value;
1291 /* Disable the local apic timer */
1292 value = apic_read(APIC_LVTT);
1293 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1294 apic_write(APIC_LVTT, value);
1295 }
1296 #endif
1297
1298 setup_apic_nmi_watchdog(NULL);
1299 apic_pm_activate();
1300 }
1301
1302 #ifdef CONFIG_X86_X2APIC
1303 void check_x2apic(void)
1304 {
1305 if (x2apic_enabled()) {
1306 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1307 x2apic_preenabled = x2apic = 1;
1308 }
1309 }
1310
1311 void enable_x2apic(void)
1312 {
1313 int msr, msr2;
1314
1315 if (!x2apic)
1316 return;
1317
1318 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1319 if (!(msr & X2APIC_ENABLE)) {
1320 pr_info("Enabling x2apic\n");
1321 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1322 }
1323 }
1324
1325 void __init enable_IR_x2apic(void)
1326 {
1327 #ifdef CONFIG_INTR_REMAP
1328 int ret;
1329 unsigned long flags;
1330 struct IO_APIC_route_entry **ioapic_entries = NULL;
1331
1332 if (!cpu_has_x2apic)
1333 return;
1334
1335 if (!x2apic_preenabled && disable_x2apic) {
1336 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1337 "because of nox2apic\n");
1338 return;
1339 }
1340
1341 if (x2apic_preenabled && disable_x2apic)
1342 panic("Bios already enabled x2apic, can't enforce nox2apic");
1343
1344 if (!x2apic_preenabled && skip_ioapic_setup) {
1345 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1346 "because of skipping io-apic setup\n");
1347 return;
1348 }
1349
1350 ret = dmar_table_init();
1351 if (ret) {
1352 pr_info("dmar_table_init() failed with %d:\n", ret);
1353
1354 if (x2apic_preenabled)
1355 panic("x2apic enabled by bios. But IR enabling failed");
1356 else
1357 pr_info("Not enabling x2apic,Intr-remapping\n");
1358 return;
1359 }
1360
1361 ioapic_entries = alloc_ioapic_entries();
1362 if (!ioapic_entries) {
1363 pr_info("Allocate ioapic_entries failed: %d\n", ret);
1364 goto end;
1365 }
1366
1367 ret = save_IO_APIC_setup(ioapic_entries);
1368 if (ret) {
1369 pr_info("Saving IO-APIC state failed: %d\n", ret);
1370 goto end;
1371 }
1372
1373 local_irq_save(flags);
1374 mask_IO_APIC_setup(ioapic_entries);
1375 mask_8259A();
1376
1377 ret = enable_intr_remapping(EIM_32BIT_APIC_ID);
1378
1379 if (ret && x2apic_preenabled) {
1380 local_irq_restore(flags);
1381 panic("x2apic enabled by bios. But IR enabling failed");
1382 }
1383
1384 if (ret)
1385 goto end_restore;
1386
1387 if (!x2apic) {
1388 x2apic = 1;
1389 enable_x2apic();
1390 }
1391
1392 end_restore:
1393 if (ret)
1394 /*
1395 * IR enabling failed
1396 */
1397 restore_IO_APIC_setup(ioapic_entries);
1398 else
1399 reinit_intr_remapped_IO_APIC(x2apic_preenabled, ioapic_entries);
1400
1401 unmask_8259A();
1402 local_irq_restore(flags);
1403
1404 end:
1405 if (!ret) {
1406 if (!x2apic_preenabled)
1407 pr_info("Enabled x2apic and interrupt-remapping\n");
1408 else
1409 pr_info("Enabled Interrupt-remapping\n");
1410 } else
1411 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
1412 if (ioapic_entries)
1413 free_ioapic_entries(ioapic_entries);
1414 #else
1415 if (!cpu_has_x2apic)
1416 return;
1417
1418 if (x2apic_preenabled)
1419 panic("x2apic enabled prior OS handover,"
1420 " enable CONFIG_INTR_REMAP");
1421
1422 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1423 " and x2apic\n");
1424 #endif
1425
1426 return;
1427 }
1428 #endif /* CONFIG_X86_X2APIC */
1429
1430 #ifdef CONFIG_X86_64
1431 /*
1432 * Detect and enable local APICs on non-SMP boards.
1433 * Original code written by Keir Fraser.
1434 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1435 * not correctly set up (usually the APIC timer won't work etc.)
1436 */
1437 static int __init detect_init_APIC(void)
1438 {
1439 if (!cpu_has_apic) {
1440 pr_info("No local APIC present\n");
1441 return -1;
1442 }
1443
1444 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1445 boot_cpu_physical_apicid = 0;
1446 return 0;
1447 }
1448 #else
1449 /*
1450 * Detect and initialize APIC
1451 */
1452 static int __init detect_init_APIC(void)
1453 {
1454 u32 h, l, features;
1455
1456 /* Disabled by kernel option? */
1457 if (disable_apic)
1458 return -1;
1459
1460 switch (boot_cpu_data.x86_vendor) {
1461 case X86_VENDOR_AMD:
1462 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1463 (boot_cpu_data.x86 >= 15))
1464 break;
1465 goto no_apic;
1466 case X86_VENDOR_INTEL:
1467 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1468 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1469 break;
1470 goto no_apic;
1471 default:
1472 goto no_apic;
1473 }
1474
1475 if (!cpu_has_apic) {
1476 /*
1477 * Over-ride BIOS and try to enable the local APIC only if
1478 * "lapic" specified.
1479 */
1480 if (!force_enable_local_apic) {
1481 pr_info("Local APIC disabled by BIOS -- "
1482 "you can enable it with \"lapic\"\n");
1483 return -1;
1484 }
1485 /*
1486 * Some BIOSes disable the local APIC in the APIC_BASE
1487 * MSR. This can only be done in software for Intel P6 or later
1488 * and AMD K7 (Model > 1) or later.
1489 */
1490 rdmsr(MSR_IA32_APICBASE, l, h);
1491 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1492 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1493 l &= ~MSR_IA32_APICBASE_BASE;
1494 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1495 wrmsr(MSR_IA32_APICBASE, l, h);
1496 enabled_via_apicbase = 1;
1497 }
1498 }
1499 /*
1500 * The APIC feature bit should now be enabled
1501 * in `cpuid'
1502 */
1503 features = cpuid_edx(1);
1504 if (!(features & (1 << X86_FEATURE_APIC))) {
1505 pr_warning("Could not enable APIC!\n");
1506 return -1;
1507 }
1508 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1509 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1510
1511 /* The BIOS may have set up the APIC at some other address */
1512 rdmsr(MSR_IA32_APICBASE, l, h);
1513 if (l & MSR_IA32_APICBASE_ENABLE)
1514 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1515
1516 pr_info("Found and enabled local APIC!\n");
1517
1518 apic_pm_activate();
1519
1520 return 0;
1521
1522 no_apic:
1523 pr_info("No local APIC present or hardware disabled\n");
1524 return -1;
1525 }
1526 #endif
1527
1528 #ifdef CONFIG_X86_64
1529 void __init early_init_lapic_mapping(void)
1530 {
1531 unsigned long phys_addr;
1532
1533 /*
1534 * If no local APIC can be found then go out
1535 * : it means there is no mpatable and MADT
1536 */
1537 if (!smp_found_config)
1538 return;
1539
1540 phys_addr = mp_lapic_addr;
1541
1542 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
1543 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1544 APIC_BASE, phys_addr);
1545
1546 /*
1547 * Fetch the APIC ID of the BSP in case we have a
1548 * default configuration (or the MP table is broken).
1549 */
1550 boot_cpu_physical_apicid = read_apic_id();
1551 }
1552 #endif
1553
1554 /**
1555 * init_apic_mappings - initialize APIC mappings
1556 */
1557 void __init init_apic_mappings(void)
1558 {
1559 if (x2apic) {
1560 boot_cpu_physical_apicid = read_apic_id();
1561 return;
1562 }
1563
1564 /*
1565 * If no local APIC can be found then set up a fake all
1566 * zeroes page to simulate the local APIC and another
1567 * one for the IO-APIC.
1568 */
1569 if (!smp_found_config && detect_init_APIC()) {
1570 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1571 apic_phys = __pa(apic_phys);
1572 } else
1573 apic_phys = mp_lapic_addr;
1574
1575 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1576 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
1577 APIC_BASE, apic_phys);
1578
1579 /*
1580 * Fetch the APIC ID of the BSP in case we have a
1581 * default configuration (or the MP table is broken).
1582 */
1583 if (boot_cpu_physical_apicid == -1U)
1584 boot_cpu_physical_apicid = read_apic_id();
1585 }
1586
1587 /*
1588 * This initializes the IO-APIC and APIC hardware if this is
1589 * a UP kernel.
1590 */
1591 int apic_version[MAX_APICS];
1592
1593 int __init APIC_init_uniprocessor(void)
1594 {
1595 if (disable_apic) {
1596 pr_info("Apic disabled\n");
1597 return -1;
1598 }
1599 #ifdef CONFIG_X86_64
1600 if (!cpu_has_apic) {
1601 disable_apic = 1;
1602 pr_info("Apic disabled by BIOS\n");
1603 return -1;
1604 }
1605 #else
1606 if (!smp_found_config && !cpu_has_apic)
1607 return -1;
1608
1609 /*
1610 * Complain if the BIOS pretends there is one.
1611 */
1612 if (!cpu_has_apic &&
1613 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1614 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1615 boot_cpu_physical_apicid);
1616 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1617 return -1;
1618 }
1619 #endif
1620
1621 enable_IR_x2apic();
1622 #ifdef CONFIG_X86_64
1623 default_setup_apic_routing();
1624 #endif
1625
1626 verify_local_APIC();
1627 connect_bsp_APIC();
1628
1629 #ifdef CONFIG_X86_64
1630 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1631 #else
1632 /*
1633 * Hack: In case of kdump, after a crash, kernel might be booting
1634 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1635 * might be zero if read from MP tables. Get it from LAPIC.
1636 */
1637 # ifdef CONFIG_CRASH_DUMP
1638 boot_cpu_physical_apicid = read_apic_id();
1639 # endif
1640 #endif
1641 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1642 setup_local_APIC();
1643
1644 #ifdef CONFIG_X86_IO_APIC
1645 /*
1646 * Now enable IO-APICs, actually call clear_IO_APIC
1647 * We need clear_IO_APIC before enabling error vector
1648 */
1649 if (!skip_ioapic_setup && nr_ioapics)
1650 enable_IO_APIC();
1651 #endif
1652
1653 end_local_APIC_setup();
1654
1655 #ifdef CONFIG_X86_IO_APIC
1656 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1657 setup_IO_APIC();
1658 else {
1659 nr_ioapics = 0;
1660 localise_nmi_watchdog();
1661 }
1662 #else
1663 localise_nmi_watchdog();
1664 #endif
1665
1666 setup_boot_clock();
1667 #ifdef CONFIG_X86_64
1668 check_nmi_watchdog();
1669 #endif
1670
1671 return 0;
1672 }
1673
1674 /*
1675 * Local APIC interrupts
1676 */
1677
1678 /*
1679 * This interrupt should _never_ happen with our APIC/SMP architecture
1680 */
1681 void smp_spurious_interrupt(struct pt_regs *regs)
1682 {
1683 u32 v;
1684
1685 exit_idle();
1686 irq_enter();
1687 /*
1688 * Check if this really is a spurious interrupt and ACK it
1689 * if it is a vectored one. Just in case...
1690 * Spurious interrupts should not be ACKed.
1691 */
1692 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1693 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1694 ack_APIC_irq();
1695
1696 inc_irq_stat(irq_spurious_count);
1697
1698 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1699 pr_info("spurious APIC interrupt on CPU#%d, "
1700 "should never happen.\n", smp_processor_id());
1701 irq_exit();
1702 }
1703
1704 /*
1705 * This interrupt should never happen with our APIC/SMP architecture
1706 */
1707 void smp_error_interrupt(struct pt_regs *regs)
1708 {
1709 u32 v, v1;
1710
1711 exit_idle();
1712 irq_enter();
1713 /* First tickle the hardware, only then report what went on. -- REW */
1714 v = apic_read(APIC_ESR);
1715 apic_write(APIC_ESR, 0);
1716 v1 = apic_read(APIC_ESR);
1717 ack_APIC_irq();
1718 atomic_inc(&irq_err_count);
1719
1720 /*
1721 * Here is what the APIC error bits mean:
1722 * 0: Send CS error
1723 * 1: Receive CS error
1724 * 2: Send accept error
1725 * 3: Receive accept error
1726 * 4: Reserved
1727 * 5: Send illegal vector
1728 * 6: Received illegal vector
1729 * 7: Illegal register address
1730 */
1731 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
1732 smp_processor_id(), v , v1);
1733 irq_exit();
1734 }
1735
1736 /**
1737 * connect_bsp_APIC - attach the APIC to the interrupt system
1738 */
1739 void __init connect_bsp_APIC(void)
1740 {
1741 #ifdef CONFIG_X86_32
1742 if (pic_mode) {
1743 /*
1744 * Do not trust the local APIC being empty at bootup.
1745 */
1746 clear_local_APIC();
1747 /*
1748 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1749 * local APIC to INT and NMI lines.
1750 */
1751 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1752 "enabling APIC mode.\n");
1753 imcr_pic_to_apic();
1754 }
1755 #endif
1756 if (apic->enable_apic_mode)
1757 apic->enable_apic_mode();
1758 }
1759
1760 /**
1761 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1762 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1763 *
1764 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1765 * APIC is disabled.
1766 */
1767 void disconnect_bsp_APIC(int virt_wire_setup)
1768 {
1769 unsigned int value;
1770
1771 #ifdef CONFIG_X86_32
1772 if (pic_mode) {
1773 /*
1774 * Put the board back into PIC mode (has an effect only on
1775 * certain older boards). Note that APIC interrupts, including
1776 * IPIs, won't work beyond this point! The only exception are
1777 * INIT IPIs.
1778 */
1779 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1780 "entering PIC mode.\n");
1781 imcr_apic_to_pic();
1782 return;
1783 }
1784 #endif
1785
1786 /* Go back to Virtual Wire compatibility mode */
1787
1788 /* For the spurious interrupt use vector F, and enable it */
1789 value = apic_read(APIC_SPIV);
1790 value &= ~APIC_VECTOR_MASK;
1791 value |= APIC_SPIV_APIC_ENABLED;
1792 value |= 0xf;
1793 apic_write(APIC_SPIV, value);
1794
1795 if (!virt_wire_setup) {
1796 /*
1797 * For LVT0 make it edge triggered, active high,
1798 * external and enabled
1799 */
1800 value = apic_read(APIC_LVT0);
1801 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1802 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1803 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1804 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1805 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1806 apic_write(APIC_LVT0, value);
1807 } else {
1808 /* Disable LVT0 */
1809 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1810 }
1811
1812 /*
1813 * For LVT1 make it edge triggered, active high,
1814 * nmi and enabled
1815 */
1816 value = apic_read(APIC_LVT1);
1817 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1818 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1819 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1820 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1821 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1822 apic_write(APIC_LVT1, value);
1823 }
1824
1825 void __cpuinit generic_processor_info(int apicid, int version)
1826 {
1827 int cpu;
1828
1829 /*
1830 * Validate version
1831 */
1832 if (version == 0x0) {
1833 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
1834 "fixing up to 0x10. (tell your hw vendor)\n",
1835 version);
1836 version = 0x10;
1837 }
1838 apic_version[apicid] = version;
1839
1840 if (num_processors >= nr_cpu_ids) {
1841 int max = nr_cpu_ids;
1842 int thiscpu = max + disabled_cpus;
1843
1844 pr_warning(
1845 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1846 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1847
1848 disabled_cpus++;
1849 return;
1850 }
1851
1852 num_processors++;
1853 cpu = cpumask_next_zero(-1, cpu_present_mask);
1854
1855 if (version != apic_version[boot_cpu_physical_apicid])
1856 WARN_ONCE(1,
1857 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1858 apic_version[boot_cpu_physical_apicid], cpu, version);
1859
1860 physid_set(apicid, phys_cpu_present_map);
1861 if (apicid == boot_cpu_physical_apicid) {
1862 /*
1863 * x86_bios_cpu_apicid is required to have processors listed
1864 * in same order as logical cpu numbers. Hence the first
1865 * entry is BSP, and so on.
1866 */
1867 cpu = 0;
1868 }
1869 if (apicid > max_physical_apicid)
1870 max_physical_apicid = apicid;
1871
1872 #ifdef CONFIG_X86_32
1873 /*
1874 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1875 * but we need to work other dependencies like SMP_SUSPEND etc
1876 * before this can be done without some confusion.
1877 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1878 * - Ashok Raj <ashok.raj@intel.com>
1879 */
1880 if (max_physical_apicid >= 8) {
1881 switch (boot_cpu_data.x86_vendor) {
1882 case X86_VENDOR_INTEL:
1883 if (!APIC_XAPIC(version)) {
1884 def_to_bigsmp = 0;
1885 break;
1886 }
1887 /* If P4 and above fall through */
1888 case X86_VENDOR_AMD:
1889 def_to_bigsmp = 1;
1890 }
1891 }
1892 #endif
1893
1894 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
1895 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1896 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1897 #endif
1898
1899 set_cpu_possible(cpu, true);
1900 set_cpu_present(cpu, true);
1901 }
1902
1903 int hard_smp_processor_id(void)
1904 {
1905 return read_apic_id();
1906 }
1907
1908 void default_init_apic_ldr(void)
1909 {
1910 unsigned long val;
1911
1912 apic_write(APIC_DFR, APIC_DFR_VALUE);
1913 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1914 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1915 apic_write(APIC_LDR, val);
1916 }
1917
1918 #ifdef CONFIG_X86_32
1919 int default_apicid_to_node(int logical_apicid)
1920 {
1921 #ifdef CONFIG_SMP
1922 return apicid_2_node[hard_smp_processor_id()];
1923 #else
1924 return 0;
1925 #endif
1926 }
1927 #endif
1928
1929 /*
1930 * Power management
1931 */
1932 #ifdef CONFIG_PM
1933
1934 static struct {
1935 /*
1936 * 'active' is true if the local APIC was enabled by us and
1937 * not the BIOS; this signifies that we are also responsible
1938 * for disabling it before entering apm/acpi suspend
1939 */
1940 int active;
1941 /* r/w apic fields */
1942 unsigned int apic_id;
1943 unsigned int apic_taskpri;
1944 unsigned int apic_ldr;
1945 unsigned int apic_dfr;
1946 unsigned int apic_spiv;
1947 unsigned int apic_lvtt;
1948 unsigned int apic_lvtpc;
1949 unsigned int apic_lvt0;
1950 unsigned int apic_lvt1;
1951 unsigned int apic_lvterr;
1952 unsigned int apic_tmict;
1953 unsigned int apic_tdcr;
1954 unsigned int apic_thmr;
1955 } apic_pm_state;
1956
1957 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1958 {
1959 unsigned long flags;
1960 int maxlvt;
1961
1962 if (!apic_pm_state.active)
1963 return 0;
1964
1965 maxlvt = lapic_get_maxlvt();
1966
1967 apic_pm_state.apic_id = apic_read(APIC_ID);
1968 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1969 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1970 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1971 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1972 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1973 if (maxlvt >= 4)
1974 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1975 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1976 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1977 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1978 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1979 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1980 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
1981 if (maxlvt >= 5)
1982 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1983 #endif
1984
1985 local_irq_save(flags);
1986 disable_local_APIC();
1987 #ifdef CONFIG_INTR_REMAP
1988 if (intr_remapping_enabled)
1989 disable_intr_remapping();
1990 #endif
1991 local_irq_restore(flags);
1992 return 0;
1993 }
1994
1995 static int lapic_resume(struct sys_device *dev)
1996 {
1997 unsigned int l, h;
1998 unsigned long flags;
1999 int maxlvt;
2000
2001 #ifdef CONFIG_INTR_REMAP
2002 int ret;
2003 struct IO_APIC_route_entry **ioapic_entries = NULL;
2004
2005 if (!apic_pm_state.active)
2006 return 0;
2007
2008 local_irq_save(flags);
2009 if (x2apic) {
2010 ioapic_entries = alloc_ioapic_entries();
2011 if (!ioapic_entries) {
2012 WARN(1, "Alloc ioapic_entries in lapic resume failed.");
2013 return -ENOMEM;
2014 }
2015
2016 ret = save_IO_APIC_setup(ioapic_entries);
2017 if (ret) {
2018 WARN(1, "Saving IO-APIC state failed: %d\n", ret);
2019 free_ioapic_entries(ioapic_entries);
2020 return ret;
2021 }
2022
2023 mask_IO_APIC_setup(ioapic_entries);
2024 mask_8259A();
2025 enable_x2apic();
2026 }
2027 #else
2028 if (!apic_pm_state.active)
2029 return 0;
2030
2031 local_irq_save(flags);
2032 if (x2apic)
2033 enable_x2apic();
2034 #endif
2035
2036 else {
2037 /*
2038 * Make sure the APICBASE points to the right address
2039 *
2040 * FIXME! This will be wrong if we ever support suspend on
2041 * SMP! We'll need to do this as part of the CPU restore!
2042 */
2043 rdmsr(MSR_IA32_APICBASE, l, h);
2044 l &= ~MSR_IA32_APICBASE_BASE;
2045 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2046 wrmsr(MSR_IA32_APICBASE, l, h);
2047 }
2048
2049 maxlvt = lapic_get_maxlvt();
2050 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2051 apic_write(APIC_ID, apic_pm_state.apic_id);
2052 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2053 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2054 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2055 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2056 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2057 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2058 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2059 if (maxlvt >= 5)
2060 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2061 #endif
2062 if (maxlvt >= 4)
2063 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2064 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2065 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2066 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2067 apic_write(APIC_ESR, 0);
2068 apic_read(APIC_ESR);
2069 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2070 apic_write(APIC_ESR, 0);
2071 apic_read(APIC_ESR);
2072
2073 #ifdef CONFIG_INTR_REMAP
2074 if (intr_remapping_enabled)
2075 reenable_intr_remapping(EIM_32BIT_APIC_ID);
2076
2077 if (x2apic) {
2078 unmask_8259A();
2079 restore_IO_APIC_setup(ioapic_entries);
2080 free_ioapic_entries(ioapic_entries);
2081 }
2082 #endif
2083
2084 local_irq_restore(flags);
2085
2086
2087 return 0;
2088 }
2089
2090 /*
2091 * This device has no shutdown method - fully functioning local APICs
2092 * are needed on every CPU up until machine_halt/restart/poweroff.
2093 */
2094
2095 static struct sysdev_class lapic_sysclass = {
2096 .name = "lapic",
2097 .resume = lapic_resume,
2098 .suspend = lapic_suspend,
2099 };
2100
2101 static struct sys_device device_lapic = {
2102 .id = 0,
2103 .cls = &lapic_sysclass,
2104 };
2105
2106 static void __cpuinit apic_pm_activate(void)
2107 {
2108 apic_pm_state.active = 1;
2109 }
2110
2111 static int __init init_lapic_sysfs(void)
2112 {
2113 int error;
2114
2115 if (!cpu_has_apic)
2116 return 0;
2117 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2118
2119 error = sysdev_class_register(&lapic_sysclass);
2120 if (!error)
2121 error = sysdev_register(&device_lapic);
2122 return error;
2123 }
2124
2125 /* local apic needs to resume before other devices access its registers. */
2126 core_initcall(init_lapic_sysfs);
2127
2128 #else /* CONFIG_PM */
2129
2130 static void apic_pm_activate(void) { }
2131
2132 #endif /* CONFIG_PM */
2133
2134 #ifdef CONFIG_X86_64
2135 /*
2136 * apic_is_clustered_box() -- Check if we can expect good TSC
2137 *
2138 * Thus far, the major user of this is IBM's Summit2 series:
2139 *
2140 * Clustered boxes may have unsynced TSC problems if they are
2141 * multi-chassis. Use available data to take a good guess.
2142 * If in doubt, go HPET.
2143 */
2144 __cpuinit int apic_is_clustered_box(void)
2145 {
2146 int i, clusters, zeros;
2147 unsigned id;
2148 u16 *bios_cpu_apicid;
2149 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2150
2151 /*
2152 * there is not this kind of box with AMD CPU yet.
2153 * Some AMD box with quadcore cpu and 8 sockets apicid
2154 * will be [4, 0x23] or [8, 0x27] could be thought to
2155 * vsmp box still need checking...
2156 */
2157 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
2158 return 0;
2159
2160 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
2161 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
2162
2163 for (i = 0; i < nr_cpu_ids; i++) {
2164 /* are we being called early in kernel startup? */
2165 if (bios_cpu_apicid) {
2166 id = bios_cpu_apicid[i];
2167 } else if (i < nr_cpu_ids) {
2168 if (cpu_present(i))
2169 id = per_cpu(x86_bios_cpu_apicid, i);
2170 else
2171 continue;
2172 } else
2173 break;
2174
2175 if (id != BAD_APICID)
2176 __set_bit(APIC_CLUSTERID(id), clustermap);
2177 }
2178
2179 /* Problem: Partially populated chassis may not have CPUs in some of
2180 * the APIC clusters they have been allocated. Only present CPUs have
2181 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2182 * Since clusters are allocated sequentially, count zeros only if
2183 * they are bounded by ones.
2184 */
2185 clusters = 0;
2186 zeros = 0;
2187 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2188 if (test_bit(i, clustermap)) {
2189 clusters += 1 + zeros;
2190 zeros = 0;
2191 } else
2192 ++zeros;
2193 }
2194
2195 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2196 * not guaranteed to be synced between boards
2197 */
2198 if (is_vsmp_box() && clusters > 1)
2199 return 1;
2200
2201 /*
2202 * If clusters > 2, then should be multi-chassis.
2203 * May have to revisit this when multi-core + hyperthreaded CPUs come
2204 * out, but AFAIK this will work even for them.
2205 */
2206 return (clusters > 2);
2207 }
2208 #endif
2209
2210 /*
2211 * APIC command line parameters
2212 */
2213 static int __init setup_disableapic(char *arg)
2214 {
2215 disable_apic = 1;
2216 setup_clear_cpu_cap(X86_FEATURE_APIC);
2217 return 0;
2218 }
2219 early_param("disableapic", setup_disableapic);
2220
2221 /* same as disableapic, for compatibility */
2222 static int __init setup_nolapic(char *arg)
2223 {
2224 return setup_disableapic(arg);
2225 }
2226 early_param("nolapic", setup_nolapic);
2227
2228 static int __init parse_lapic_timer_c2_ok(char *arg)
2229 {
2230 local_apic_timer_c2_ok = 1;
2231 return 0;
2232 }
2233 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2234
2235 static int __init parse_disable_apic_timer(char *arg)
2236 {
2237 disable_apic_timer = 1;
2238 return 0;
2239 }
2240 early_param("noapictimer", parse_disable_apic_timer);
2241
2242 static int __init parse_nolapic_timer(char *arg)
2243 {
2244 disable_apic_timer = 1;
2245 return 0;
2246 }
2247 early_param("nolapic_timer", parse_nolapic_timer);
2248
2249 static int __init apic_set_verbosity(char *arg)
2250 {
2251 if (!arg) {
2252 #ifdef CONFIG_X86_64
2253 skip_ioapic_setup = 0;
2254 return 0;
2255 #endif
2256 return -EINVAL;
2257 }
2258
2259 if (strcmp("debug", arg) == 0)
2260 apic_verbosity = APIC_DEBUG;
2261 else if (strcmp("verbose", arg) == 0)
2262 apic_verbosity = APIC_VERBOSE;
2263 else {
2264 pr_warning("APIC Verbosity level %s not recognised"
2265 " use apic=verbose or apic=debug\n", arg);
2266 return -EINVAL;
2267 }
2268
2269 return 0;
2270 }
2271 early_param("apic", apic_set_verbosity);
2272
2273 static int __init lapic_insert_resource(void)
2274 {
2275 if (!apic_phys)
2276 return -1;
2277
2278 /* Put local APIC into the resource map. */
2279 lapic_resource.start = apic_phys;
2280 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2281 insert_resource(&iomem_resource, &lapic_resource);
2282
2283 return 0;
2284 }
2285
2286 /*
2287 * need call insert after e820_reserve_resources()
2288 * that is using request_resource
2289 */
2290 late_initcall(lapic_insert_resource);