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