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