ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / apic.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/config.h>
18 #include <linux/init.h>
19
20 #include <linux/mm.h>
21 #include <linux/irq.h>
22 #include <linux/delay.h>
23 #include <linux/bootmem.h>
24 #include <linux/smp_lock.h>
25 #include <linux/interrupt.h>
26 #include <linux/mc146818rtc.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/sysdev.h>
29
30 #include <asm/atomic.h>
31 #include <asm/smp.h>
32 #include <asm/mtrr.h>
33 #include <asm/mpspec.h>
34 #include <asm/pgalloc.h>
35 #include <asm/desc.h>
36 #include <asm/arch_hooks.h>
37 #include <asm/hpet.h>
38
39 #include <mach_apic.h>
40
41 #include "io_ports.h"
42
43 static void apic_pm_activate(void);
44
45 void __init apic_intr_init(void)
46 {
47 #ifdef CONFIG_SMP
48         smp_intr_init();
49 #endif
50         /* self generated IPI for local APIC timer */
51         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
52
53         /* IPI vectors for APIC spurious and error interrupts */
54         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
55         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
56
57         /* thermal monitor LVT interrupt */
58 #ifdef CONFIG_X86_MCE_P4THERMAL
59         set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
60 #endif
61 }
62
63 /* Using APIC to generate smp_local_timer_interrupt? */
64 int using_apic_timer = 0;
65
66 static DEFINE_PER_CPU(int, prof_multiplier) = 1;
67 static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
68 static DEFINE_PER_CPU(int, prof_counter) = 1;
69
70 static int enabled_via_apicbase;
71
72 void enable_NMI_through_LVT0 (void * dummy)
73 {
74         unsigned int v, ver;
75
76         ver = apic_read(APIC_LVR);
77         ver = GET_APIC_VERSION(ver);
78         v = APIC_DM_NMI;                        /* unmask and set to NMI */
79         if (!APIC_INTEGRATED(ver))              /* 82489DX */
80                 v |= APIC_LVT_LEVEL_TRIGGER;
81         apic_write_around(APIC_LVT0, v);
82 }
83
84 int get_maxlvt(void)
85 {
86         unsigned int v, ver, maxlvt;
87
88         v = apic_read(APIC_LVR);
89         ver = GET_APIC_VERSION(v);
90         /* 82489DXs do not report # of LVT entries. */
91         maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
92         return maxlvt;
93 }
94
95 void clear_local_APIC(void)
96 {
97         int maxlvt;
98         unsigned long v;
99
100         maxlvt = get_maxlvt();
101
102         /*
103          * Masking an LVT entry on a P6 can trigger a local APIC error
104          * if the vector is zero. Mask LVTERR first to prevent this.
105          */
106         if (maxlvt >= 3) {
107                 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
108                 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
109         }
110         /*
111          * Careful: we have to set masks only first to deassert
112          * any level-triggered sources.
113          */
114         v = apic_read(APIC_LVTT);
115         apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
116         v = apic_read(APIC_LVT0);
117         apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
118         v = apic_read(APIC_LVT1);
119         apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
120         if (maxlvt >= 4) {
121                 v = apic_read(APIC_LVTPC);
122                 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
123         }
124
125 /* lets not touch this if we didn't frob it */
126 #ifdef CONFIG_X86_MCE_P4THERMAL
127         if (maxlvt >= 5) {
128                 v = apic_read(APIC_LVTTHMR);
129                 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
130         }
131 #endif
132         /*
133          * Clean APIC state for other OSs:
134          */
135         apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
136         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
137         apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
138         if (maxlvt >= 3)
139                 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
140         if (maxlvt >= 4)
141                 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
142
143 #ifdef CONFIG_X86_MCE_P4THERMAL
144         if (maxlvt >= 5)
145                 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
146 #endif
147         v = GET_APIC_VERSION(apic_read(APIC_LVR));
148         if (APIC_INTEGRATED(v)) {       /* !82489DX */
149                 if (maxlvt > 3)         /* Due to Pentium errata 3AP and 11AP. */
150                         apic_write(APIC_ESR, 0);
151                 apic_read(APIC_ESR);
152         }
153 }
154
155 void __init connect_bsp_APIC(void)
156 {
157         if (pic_mode) {
158                 /*
159                  * Do not trust the local APIC being empty at bootup.
160                  */
161                 clear_local_APIC();
162                 /*
163                  * PIC mode, enable APIC mode in the IMCR, i.e.
164                  * connect BSP's local APIC to INT and NMI lines.
165                  */
166                 printk("leaving PIC mode, enabling APIC mode.\n");
167                 outb(0x70, 0x22);
168                 outb(0x01, 0x23);
169         }
170         enable_apic_mode();
171 }
172
173 void disconnect_bsp_APIC(void)
174 {
175         if (pic_mode) {
176                 /*
177                  * Put the board back into PIC mode (has an effect
178                  * only on certain older boards).  Note that APIC
179                  * interrupts, including IPIs, won't work beyond
180                  * this point!  The only exception are INIT IPIs.
181                  */
182                 printk("disabling APIC mode, entering PIC mode.\n");
183                 outb(0x70, 0x22);
184                 outb(0x00, 0x23);
185         }
186 }
187
188 void disable_local_APIC(void)
189 {
190         unsigned long value;
191
192         clear_local_APIC();
193
194         /*
195          * Disable APIC (implies clearing of registers
196          * for 82489DX!).
197          */
198         value = apic_read(APIC_SPIV);
199         value &= ~APIC_SPIV_APIC_ENABLED;
200         apic_write_around(APIC_SPIV, value);
201
202         if (enabled_via_apicbase) {
203                 unsigned int l, h;
204                 rdmsr(MSR_IA32_APICBASE, l, h);
205                 l &= ~MSR_IA32_APICBASE_ENABLE;
206                 wrmsr(MSR_IA32_APICBASE, l, h);
207         }
208 }
209
210 /*
211  * This is to verify that we're looking at a real local APIC.
212  * Check these against your board if the CPUs aren't getting
213  * started for no apparent reason.
214  */
215 int __init verify_local_APIC(void)
216 {
217         unsigned int reg0, reg1;
218
219         /*
220          * The version register is read-only in a real APIC.
221          */
222         reg0 = apic_read(APIC_LVR);
223         Dprintk("Getting VERSION: %x\n", reg0);
224         apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
225         reg1 = apic_read(APIC_LVR);
226         Dprintk("Getting VERSION: %x\n", reg1);
227
228         /*
229          * The two version reads above should print the same
230          * numbers.  If the second one is different, then we
231          * poke at a non-APIC.
232          */
233         if (reg1 != reg0)
234                 return 0;
235
236         /*
237          * Check if the version looks reasonably.
238          */
239         reg1 = GET_APIC_VERSION(reg0);
240         if (reg1 == 0x00 || reg1 == 0xff)
241                 return 0;
242         reg1 = get_maxlvt();
243         if (reg1 < 0x02 || reg1 == 0xff)
244                 return 0;
245
246         /*
247          * The ID register is read/write in a real APIC.
248          */
249         reg0 = apic_read(APIC_ID);
250         Dprintk("Getting ID: %x\n", reg0);
251
252         /*
253          * The next two are just to see if we have sane values.
254          * They're only really relevant if we're in Virtual Wire
255          * compatibility mode, but most boxes are anymore.
256          */
257         reg0 = apic_read(APIC_LVT0);
258         Dprintk("Getting LVT0: %x\n", reg0);
259         reg1 = apic_read(APIC_LVT1);
260         Dprintk("Getting LVT1: %x\n", reg1);
261
262         return 1;
263 }
264
265 void __init sync_Arb_IDs(void)
266 {
267         /*
268          * Wait for idle.
269          */
270         apic_wait_icr_idle();
271
272         Dprintk("Synchronizing Arb IDs.\n");
273         apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
274                                 | APIC_DM_INIT);
275 }
276
277 extern void __error_in_apic_c (void);
278
279 /*
280  * An initial setup of the virtual wire mode.
281  */
282 void __init init_bsp_APIC(void)
283 {
284         unsigned long value, ver;
285
286         /*
287          * Don't do the setup now if we have a SMP BIOS as the
288          * through-I/O-APIC virtual wire mode might be active.
289          */
290         if (smp_found_config || !cpu_has_apic)
291                 return;
292
293         value = apic_read(APIC_LVR);
294         ver = GET_APIC_VERSION(value);
295
296         /*
297          * Do not trust the local APIC being empty at bootup.
298          */
299         clear_local_APIC();
300
301         /*
302          * Enable APIC.
303          */
304         value = apic_read(APIC_SPIV);
305         value &= ~APIC_VECTOR_MASK;
306         value |= APIC_SPIV_APIC_ENABLED;
307         
308         /* This bit is reserved on P4/Xeon and should be cleared */
309         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
310                 value &= ~APIC_SPIV_FOCUS_DISABLED;
311         else
312                 value |= APIC_SPIV_FOCUS_DISABLED;
313         value |= SPURIOUS_APIC_VECTOR;
314         apic_write_around(APIC_SPIV, value);
315
316         /*
317          * Set up the virtual wire mode.
318          */
319         apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
320         value = APIC_DM_NMI;
321         if (!APIC_INTEGRATED(ver))              /* 82489DX */
322                 value |= APIC_LVT_LEVEL_TRIGGER;
323         apic_write_around(APIC_LVT1, value);
324 }
325
326 void __init setup_local_APIC (void)
327 {
328         unsigned long value, ver, maxlvt;
329
330         /* Pound the ESR really hard over the head with a big hammer - mbligh */
331         if (esr_disable) {
332                 apic_write(APIC_ESR, 0);
333                 apic_write(APIC_ESR, 0);
334                 apic_write(APIC_ESR, 0);
335                 apic_write(APIC_ESR, 0);
336         }
337
338         value = apic_read(APIC_LVR);
339         ver = GET_APIC_VERSION(value);
340
341         if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
342                 __error_in_apic_c();
343
344         /*
345          * Double-check whether this APIC is really registered.
346          */
347         if (!apic_id_registered())
348                 BUG();
349
350         /*
351          * Intel recommends to set DFR, LDR and TPR before enabling
352          * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
353          * document number 292116).  So here it goes...
354          */
355         init_apic_ldr();
356
357         /*
358          * Set Task Priority to 'accept all'. We never change this
359          * later on.
360          */
361         value = apic_read(APIC_TASKPRI);
362         value &= ~APIC_TPRI_MASK;
363         apic_write_around(APIC_TASKPRI, value);
364
365         /*
366          * Now that we are all set up, enable the APIC
367          */
368         value = apic_read(APIC_SPIV);
369         value &= ~APIC_VECTOR_MASK;
370         /*
371          * Enable APIC
372          */
373         value |= APIC_SPIV_APIC_ENABLED;
374
375         /*
376          * Some unknown Intel IO/APIC (or APIC) errata is biting us with
377          * certain networking cards. If high frequency interrupts are
378          * happening on a particular IOAPIC pin, plus the IOAPIC routing
379          * entry is masked/unmasked at a high rate as well then sooner or
380          * later IOAPIC line gets 'stuck', no more interrupts are received
381          * from the device. If focus CPU is disabled then the hang goes
382          * away, oh well :-(
383          *
384          * [ This bug can be reproduced easily with a level-triggered
385          *   PCI Ne2000 networking cards and PII/PIII processors, dual
386          *   BX chipset. ]
387          */
388         /*
389          * Actually disabling the focus CPU check just makes the hang less
390          * frequent as it makes the interrupt distributon model be more
391          * like LRU than MRU (the short-term load is more even across CPUs).
392          * See also the comment in end_level_ioapic_irq().  --macro
393          */
394 #if 1
395         /* Enable focus processor (bit==0) */
396         value &= ~APIC_SPIV_FOCUS_DISABLED;
397 #else
398         /* Disable focus processor (bit==1) */
399         value |= APIC_SPIV_FOCUS_DISABLED;
400 #endif
401         /*
402          * Set spurious IRQ vector
403          */
404         value |= SPURIOUS_APIC_VECTOR;
405         apic_write_around(APIC_SPIV, value);
406
407         /*
408          * Set up LVT0, LVT1:
409          *
410          * set up through-local-APIC on the BP's LINT0. This is not
411          * strictly necessery in pure symmetric-IO mode, but sometimes
412          * we delegate interrupts to the 8259A.
413          */
414         /*
415          * TODO: set up through-local-APIC from through-I/O-APIC? --macro
416          */
417         value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
418         if (!smp_processor_id() && (pic_mode || !value)) {
419                 value = APIC_DM_EXTINT;
420                 printk("enabled ExtINT on CPU#%d\n", smp_processor_id());
421         } else {
422                 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
423                 printk("masked ExtINT on CPU#%d\n", smp_processor_id());
424         }
425         apic_write_around(APIC_LVT0, value);
426
427         /*
428          * only the BP should see the LINT1 NMI signal, obviously.
429          */
430         if (!smp_processor_id())
431                 value = APIC_DM_NMI;
432         else
433                 value = APIC_DM_NMI | APIC_LVT_MASKED;
434         if (!APIC_INTEGRATED(ver))              /* 82489DX */
435                 value |= APIC_LVT_LEVEL_TRIGGER;
436         apic_write_around(APIC_LVT1, value);
437
438         if (APIC_INTEGRATED(ver) && !esr_disable) {             /* !82489DX */
439                 maxlvt = get_maxlvt();
440                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
441                         apic_write(APIC_ESR, 0);
442                 value = apic_read(APIC_ESR);
443                 printk("ESR value before enabling vector: %08lx\n", value);
444
445                 value = ERROR_APIC_VECTOR;      // enables sending errors
446                 apic_write_around(APIC_LVTERR, value);
447                 /*
448                  * spec says clear errors after enabling vector.
449                  */
450                 if (maxlvt > 3)
451                         apic_write(APIC_ESR, 0);
452                 value = apic_read(APIC_ESR);
453                 printk("ESR value after enabling vector: %08lx\n", value);
454         } else {
455                 if (esr_disable)        
456                         /* 
457                          * Something untraceble is creating bad interrupts on 
458                          * secondary quads ... for the moment, just leave the
459                          * ESR disabled - we can't do anything useful with the
460                          * errors anyway - mbligh
461                          */
462                         printk("Leaving ESR disabled.\n");
463                 else 
464                         printk("No ESR for 82489DX.\n");
465         }
466
467         if (nmi_watchdog == NMI_LOCAL_APIC)
468                 setup_apic_nmi_watchdog();
469         apic_pm_activate();
470 }
471
472 #ifdef CONFIG_PM
473
474 static struct {
475         /* 'active' is true if the local APIC was enabled by us and
476            not the BIOS; this signifies that we are also responsible
477            for disabling it before entering apm/acpi suspend */
478         int active;
479         /* r/w apic fields */
480         unsigned int apic_id;
481         unsigned int apic_taskpri;
482         unsigned int apic_ldr;
483         unsigned int apic_dfr;
484         unsigned int apic_spiv;
485         unsigned int apic_lvtt;
486         unsigned int apic_lvtpc;
487         unsigned int apic_lvt0;
488         unsigned int apic_lvt1;
489         unsigned int apic_lvterr;
490         unsigned int apic_tmict;
491         unsigned int apic_tdcr;
492         unsigned int apic_thmr;
493 } apic_pm_state;
494
495 static int lapic_suspend(struct sys_device *dev, u32 state)
496 {
497         unsigned long flags;
498
499         if (!apic_pm_state.active)
500                 return 0;
501
502         apic_pm_state.apic_id = apic_read(APIC_ID);
503         apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
504         apic_pm_state.apic_ldr = apic_read(APIC_LDR);
505         apic_pm_state.apic_dfr = apic_read(APIC_DFR);
506         apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
507         apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
508         apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
509         apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
510         apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
511         apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
512         apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
513         apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
514         apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
515         
516         local_irq_save(flags);
517         disable_local_APIC();
518         local_irq_restore(flags);
519         return 0;
520 }
521
522 static int lapic_resume(struct sys_device *dev)
523 {
524         unsigned int l, h;
525         unsigned long flags;
526
527         if (!apic_pm_state.active)
528                 return 0;
529
530         local_irq_save(flags);
531
532         /*
533          * Make sure the APICBASE points to the right address
534          *
535          * FIXME! This will be wrong if we ever support suspend on
536          * SMP! We'll need to do this as part of the CPU restore!
537          */
538         rdmsr(MSR_IA32_APICBASE, l, h);
539         l &= ~MSR_IA32_APICBASE_BASE;
540         l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
541         wrmsr(MSR_IA32_APICBASE, l, h);
542
543         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
544         apic_write(APIC_ID, apic_pm_state.apic_id);
545         apic_write(APIC_DFR, apic_pm_state.apic_dfr);
546         apic_write(APIC_LDR, apic_pm_state.apic_ldr);
547         apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
548         apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
549         apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
550         apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
551         apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
552         apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
553         apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
554         apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
555         apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
556         apic_write(APIC_ESR, 0);
557         apic_read(APIC_ESR);
558         apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
559         apic_write(APIC_ESR, 0);
560         apic_read(APIC_ESR);
561         local_irq_restore(flags);
562         return 0;
563 }
564
565
566 static struct sysdev_class lapic_sysclass = {
567         set_kset_name("lapic"),
568         .resume         = lapic_resume,
569         .suspend        = lapic_suspend,
570 };
571
572 static struct sys_device device_lapic = {
573         .id     = 0,
574         .cls    = &lapic_sysclass,
575 };
576
577 static void __init apic_pm_activate(void)
578 {
579         apic_pm_state.active = 1;
580 }
581
582 static int __init init_lapic_sysfs(void)
583 {
584         int error;
585
586         if (!cpu_has_apic)
587                 return 0;
588         /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
589
590         error = sysdev_class_register(&lapic_sysclass);
591         if (!error)
592                 error = sysdev_register(&device_lapic);
593         return error;
594 }
595 device_initcall(init_lapic_sysfs);
596
597 #else   /* CONFIG_PM */
598
599 static void apic_pm_activate(void) { }
600
601 #endif  /* CONFIG_PM */
602
603 /*
604  * Detect and enable local APICs on non-SMP boards.
605  * Original code written by Keir Fraser.
606  */
607
608 /*
609  * Knob to control our willingness to enable the local APIC.
610  */
611 int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
612
613 static int __init lapic_disable(char *str)
614 {
615         enable_local_apic = -1;
616         clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
617         return 0;
618 }
619 __setup("nolapic", lapic_disable);
620
621 static int __init lapic_enable(char *str)
622 {
623         enable_local_apic = 1;
624         return 0;
625 }
626 __setup("lapic", lapic_enable);
627
628 static int __init detect_init_APIC (void)
629 {
630         u32 h, l, features;
631         extern void get_cpu_vendor(struct cpuinfo_x86*);
632
633         /* Disabled by DMI scan or kernel option? */
634         if (enable_local_apic < 0)
635                 return -1;
636
637         /* Workaround for us being called before identify_cpu(). */
638         get_cpu_vendor(&boot_cpu_data);
639
640         switch (boot_cpu_data.x86_vendor) {
641         case X86_VENDOR_AMD:
642                 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
643                     (boot_cpu_data.x86 == 15))      
644                         break;
645                 goto no_apic;
646         case X86_VENDOR_INTEL:
647                 if (boot_cpu_data.x86 == 6 ||
648                     (boot_cpu_data.x86 == 15 && (cpu_has_apic || enable_local_apic > 0)) ||
649                     (boot_cpu_data.x86 == 5 && cpu_has_apic))
650                         break;
651                 goto no_apic;
652         default:
653                 goto no_apic;
654         }
655
656         if (!cpu_has_apic) {
657                 /*
658                  * Some BIOSes disable the local APIC in the
659                  * APIC_BASE MSR. This can only be done in
660                  * software for Intel P6 and AMD K7 (Model > 1).
661                  */
662                 rdmsr(MSR_IA32_APICBASE, l, h);
663                 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
664                         printk("Local APIC disabled by BIOS -- reenabling.\n");
665                         l &= ~MSR_IA32_APICBASE_BASE;
666                         l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
667                         wrmsr(MSR_IA32_APICBASE, l, h);
668                         enabled_via_apicbase = 1;
669                 }
670         }
671         /*
672          * The APIC feature bit should now be enabled
673          * in `cpuid'
674          */
675         features = cpuid_edx(1);
676         if (!(features & (1 << X86_FEATURE_APIC))) {
677                 printk("Could not enable APIC!\n");
678                 return -1;
679         }
680         set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
681         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
682
683         /* The BIOS may have set up the APIC at some other address */
684         rdmsr(MSR_IA32_APICBASE, l, h);
685         if (l & MSR_IA32_APICBASE_ENABLE)
686                 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
687
688         if (nmi_watchdog != NMI_NONE)
689                 nmi_watchdog = NMI_LOCAL_APIC;
690
691         printk("Found and enabled local APIC!\n");
692
693         apic_pm_activate();
694
695         return 0;
696
697 no_apic:
698         printk("No local APIC present or hardware disabled\n");
699         return -1;
700 }
701
702 void __init init_apic_mappings(void)
703 {
704         unsigned long apic_phys;
705
706         /*
707          * If no local APIC can be found then set up a fake all
708          * zeroes page to simulate the local APIC and another
709          * one for the IO-APIC.
710          */
711         if (!smp_found_config && detect_init_APIC()) {
712                 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
713                 apic_phys = __pa(apic_phys);
714         } else
715                 apic_phys = mp_lapic_addr;
716
717         set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
718         Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
719
720         /*
721          * Fetch the APIC ID of the BSP in case we have a
722          * default configuration (or the MP table is broken).
723          */
724         if (boot_cpu_physical_apicid == -1U)
725                 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
726
727 #ifdef CONFIG_X86_IO_APIC
728         {
729                 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
730                 int i;
731
732                 for (i = 0; i < nr_ioapics; i++) {
733                         if (smp_found_config) {
734                                 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
735                                 if (!ioapic_phys) {
736                                         printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO/APIC support!\n");
737
738                                         smp_found_config = 0;
739                                         skip_ioapic_setup = 1;
740                                         goto fake_ioapic_page;
741                                 }
742                         } else {
743 fake_ioapic_page:
744                                 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
745                                 ioapic_phys = __pa(ioapic_phys);
746                         }
747                         set_fixmap_nocache(idx, ioapic_phys);
748                         Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
749                                         __fix_to_virt(idx), ioapic_phys);
750                         idx++;
751                 }
752         }
753 #endif
754 }
755
756 /*
757  * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
758  * per second. We assume that the caller has already set up the local
759  * APIC.
760  *
761  * The APIC timer is not exactly sync with the external timer chip, it
762  * closely follows bus clocks.
763  */
764
765 /*
766  * The timer chip is already set up at HZ interrupts per second here,
767  * but we do not accept timer interrupts yet. We only allow the BP
768  * to calibrate.
769  */
770 static unsigned int __init get_8254_timer_count(void)
771 {
772         extern spinlock_t i8253_lock;
773         unsigned long flags;
774
775         unsigned int count;
776
777         spin_lock_irqsave(&i8253_lock, flags);
778
779         outb_p(0x00, PIT_MODE);
780         count = inb_p(PIT_CH0);
781         count |= inb_p(PIT_CH0) << 8;
782
783         spin_unlock_irqrestore(&i8253_lock, flags);
784
785         return count;
786 }
787
788 /* next tick in 8254 can be caught by catching timer wraparound */
789 static void __init wait_8254_wraparound(void)
790 {
791         unsigned int curr_count, prev_count=~0;
792         int delta;
793
794         curr_count = get_8254_timer_count();
795
796         do {
797                 prev_count = curr_count;
798                 curr_count = get_8254_timer_count();
799                 delta = curr_count-prev_count;
800
801         /*
802          * This limit for delta seems arbitrary, but it isn't, it's
803          * slightly above the level of error a buggy Mercury/Neptune
804          * chipset timer can cause.
805          */
806
807         } while (delta < 300);
808 }
809
810 /*
811  * Default initialization for 8254 timers. If we use other timers like HPET,
812  * we override this later
813  */
814 void (*wait_timer_tick)(void) = wait_8254_wraparound;
815
816 /*
817  * This function sets up the local APIC timer, with a timeout of
818  * 'clocks' APIC bus clock. During calibration we actually call
819  * this function twice on the boot CPU, once with a bogus timeout
820  * value, second time for real. The other (noncalibrating) CPUs
821  * call this function only once, with the real, calibrated value.
822  *
823  * We do reads before writes even if unnecessary, to get around the
824  * P5 APIC double write bug.
825  */
826
827 #define APIC_DIVISOR 16
828
829 void __setup_APIC_LVTT(unsigned int clocks)
830 {
831         unsigned int lvtt_value, tmp_value, ver;
832
833         ver = GET_APIC_VERSION(apic_read(APIC_LVR));
834         lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
835         if (!APIC_INTEGRATED(ver))
836                 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
837         apic_write_around(APIC_LVTT, lvtt_value);
838
839         /*
840          * Divide PICLK by 16
841          */
842         tmp_value = apic_read(APIC_TDCR);
843         apic_write_around(APIC_TDCR, (tmp_value
844                                 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
845                                 | APIC_TDR_DIV_16);
846
847         apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
848 }
849
850 static void setup_APIC_timer(unsigned int clocks)
851 {
852         unsigned long flags;
853
854         local_irq_save(flags);
855
856         /*
857          * Wait for IRQ0's slice:
858          */
859         wait_timer_tick();
860
861         __setup_APIC_LVTT(clocks);
862
863         local_irq_restore(flags);
864 }
865
866 /*
867  * In this function we calibrate APIC bus clocks to the external
868  * timer. Unfortunately we cannot use jiffies and the timer irq
869  * to calibrate, since some later bootup code depends on getting
870  * the first irq? Ugh.
871  *
872  * We want to do the calibration only once since we
873  * want to have local timer irqs syncron. CPUs connected
874  * by the same APIC bus have the very same bus frequency.
875  * And we want to have irqs off anyways, no accidental
876  * APIC irq that way.
877  */
878
879 int __init calibrate_APIC_clock(void)
880 {
881         unsigned long long t1 = 0, t2 = 0;
882         long tt1, tt2;
883         long result;
884         int i;
885         const int LOOPS = HZ/10;
886
887         printk("calibrating APIC timer ...\n");
888
889         /*
890          * Put whatever arbitrary (but long enough) timeout
891          * value into the APIC clock, we just want to get the
892          * counter running for calibration.
893          */
894         __setup_APIC_LVTT(1000000000);
895
896         /*
897          * The timer chip counts down to zero. Let's wait
898          * for a wraparound to start exact measurement:
899          * (the current tick might have been already half done)
900          */
901
902         wait_timer_tick();
903
904         /*
905          * We wrapped around just now. Let's start:
906          */
907         if (cpu_has_tsc)
908                 rdtscll(t1);
909         tt1 = apic_read(APIC_TMCCT);
910
911         /*
912          * Let's wait LOOPS wraprounds:
913          */
914         for (i = 0; i < LOOPS; i++)
915                 wait_timer_tick();
916
917         tt2 = apic_read(APIC_TMCCT);
918         if (cpu_has_tsc)
919                 rdtscll(t2);
920
921         /*
922          * The APIC bus clock counter is 32 bits only, it
923          * might have overflown, but note that we use signed
924          * longs, thus no extra care needed.
925          *
926          * underflown to be exact, as the timer counts down ;)
927          */
928
929         result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
930
931         if (cpu_has_tsc)
932                 printk("..... CPU clock speed is %ld.%04ld MHz.\n",
933                         ((long)(t2-t1)/LOOPS)/(1000000/HZ),
934                         ((long)(t2-t1)/LOOPS)%(1000000/HZ));
935
936         printk("..... host bus clock speed is %ld.%04ld MHz.\n",
937                 result/(1000000/HZ),
938                 result%(1000000/HZ));
939
940         return result;
941 }
942
943 static unsigned int calibration_result;
944
945 void __init setup_boot_APIC_clock(void)
946 {
947         printk("Using local APIC timer interrupts.\n");
948         using_apic_timer = 1;
949
950         local_irq_disable();
951
952         calibration_result = calibrate_APIC_clock();
953         /*
954          * Now set up the timer for real.
955          */
956         setup_APIC_timer(calibration_result);
957
958         local_irq_enable();
959 }
960
961 void __init setup_secondary_APIC_clock(void)
962 {
963         local_irq_disable(); /* FIXME: Do we need this? --RR */
964         setup_APIC_timer(calibration_result);
965         local_irq_enable();
966 }
967
968 void __init disable_APIC_timer(void)
969 {
970         if (using_apic_timer) {
971                 unsigned long v;
972
973                 v = apic_read(APIC_LVTT);
974                 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
975         }
976 }
977
978 void enable_APIC_timer(void)
979 {
980         if (using_apic_timer) {
981                 unsigned long v;
982
983                 v = apic_read(APIC_LVTT);
984                 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
985         }
986 }
987
988 /*
989  * the frequency of the profiling timer can be changed
990  * by writing a multiplier value into /proc/profile.
991  */
992 int setup_profiling_timer(unsigned int multiplier)
993 {
994         int i;
995
996         /*
997          * Sanity check. [at least 500 APIC cycles should be
998          * between APIC interrupts as a rule of thumb, to avoid
999          * irqs flooding us]
1000          */
1001         if ( (!multiplier) || (calibration_result/multiplier < 500))
1002                 return -EINVAL;
1003
1004         /* 
1005          * Set the new multiplier for each CPU. CPUs don't start using the
1006          * new values until the next timer interrupt in which they do process
1007          * accounting. At that time they also adjust their APIC timers
1008          * accordingly.
1009          */
1010         for (i = 0; i < NR_CPUS; ++i)
1011                 per_cpu(prof_multiplier, i) = multiplier;
1012
1013         return 0;
1014 }
1015
1016 #undef APIC_DIVISOR
1017
1018 /*
1019  * Local timer interrupt handler. It does both profiling and
1020  * process statistics/rescheduling.
1021  *
1022  * We do profiling in every local tick, statistics/rescheduling
1023  * happen only every 'profiling multiplier' ticks. The default
1024  * multiplier is 1 and it can be changed by writing the new multiplier
1025  * value into /proc/profile.
1026  */
1027
1028 inline void smp_local_timer_interrupt(struct pt_regs * regs)
1029 {
1030         int cpu = smp_processor_id();
1031
1032         x86_do_profile(regs);
1033
1034         if (--per_cpu(prof_counter, cpu) <= 0) {
1035                 /*
1036                  * The multiplier may have changed since the last time we got
1037                  * to this point as a result of the user writing to
1038                  * /proc/profile. In this case we need to adjust the APIC
1039                  * timer accordingly.
1040                  *
1041                  * Interrupts are already masked off at this point.
1042                  */
1043                 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
1044                 if (per_cpu(prof_counter, cpu) !=
1045                                         per_cpu(prof_old_multiplier, cpu)) {
1046                         __setup_APIC_LVTT(
1047                                         calibration_result/
1048                                         per_cpu(prof_counter, cpu));
1049                         per_cpu(prof_old_multiplier, cpu) =
1050                                                 per_cpu(prof_counter, cpu);
1051                 }
1052
1053 #ifdef CONFIG_SMP
1054                 update_process_times(user_mode(regs));
1055 #endif
1056         }
1057
1058         /*
1059          * We take the 'long' return path, and there every subsystem
1060          * grabs the apropriate locks (kernel lock/ irq lock).
1061          *
1062          * we might want to decouple profiling from the 'long path',
1063          * and do the profiling totally in assembly.
1064          *
1065          * Currently this isn't too much of an issue (performance wise),
1066          * we can take more than 100K local irqs per second on a 100 MHz P5.
1067          */
1068 }
1069
1070 /*
1071  * Local APIC timer interrupt. This is the most natural way for doing
1072  * local interrupts, but local timer interrupts can be emulated by
1073  * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1074  *
1075  * [ if a single-CPU system runs an SMP kernel then we call the local
1076  *   interrupt as well. Thus we cannot inline the local irq ... ]
1077  */
1078
1079 void smp_apic_timer_interrupt(struct pt_regs regs)
1080 {
1081         int cpu = smp_processor_id();
1082
1083         /*
1084          * the NMI deadlock-detector uses this.
1085          */
1086         irq_stat[cpu].apic_timer_irqs++;
1087
1088         /*
1089          * NOTE! We'd better ACK the irq immediately,
1090          * because timer handling can be slow.
1091          */
1092         ack_APIC_irq();
1093         /*
1094          * update_process_times() expects us to have done irq_enter().
1095          * Besides, if we don't timer interrupts ignore the global
1096          * interrupt lock, which is the WrongThing (tm) to do.
1097          */
1098         irq_enter();
1099         smp_local_timer_interrupt(&regs);
1100         irq_exit();
1101 }
1102
1103 /*
1104  * This interrupt should _never_ happen with our APIC/SMP architecture
1105  */
1106 asmlinkage void smp_spurious_interrupt(void)
1107 {
1108         unsigned long v;
1109
1110         irq_enter();
1111         /*
1112          * Check if this really is a spurious interrupt and ACK it
1113          * if it is a vectored one.  Just in case...
1114          * Spurious interrupts should not be ACKed.
1115          */
1116         v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1117         if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1118                 ack_APIC_irq();
1119
1120         /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1121         printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1122                         smp_processor_id());
1123         irq_exit();
1124 }
1125
1126 /*
1127  * This interrupt should never happen with our APIC/SMP architecture
1128  */
1129
1130 asmlinkage void smp_error_interrupt(void)
1131 {
1132         unsigned long v, v1;
1133
1134         irq_enter();
1135         /* First tickle the hardware, only then report what went on. -- REW */
1136         v = apic_read(APIC_ESR);
1137         apic_write(APIC_ESR, 0);
1138         v1 = apic_read(APIC_ESR);
1139         ack_APIC_irq();
1140         atomic_inc(&irq_err_count);
1141
1142         /* Here is what the APIC error bits mean:
1143            0: Send CS error
1144            1: Receive CS error
1145            2: Send accept error
1146            3: Receive accept error
1147            4: Reserved
1148            5: Send illegal vector
1149            6: Received illegal vector
1150            7: Illegal register address
1151         */
1152         printk (KERN_INFO "APIC error on CPU%d: %02lx(%02lx)\n",
1153                 smp_processor_id(), v , v1);
1154         irq_exit();
1155 }
1156
1157 /*
1158  * This initializes the IO-APIC and APIC hardware if this is
1159  * a UP kernel.
1160  */
1161 int __init APIC_init_uniprocessor (void)
1162 {
1163         if (enable_local_apic < 0)
1164                 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1165
1166         if (!smp_found_config && !cpu_has_apic)
1167                 return -1;
1168
1169         /*
1170          * Complain if the BIOS pretends there is one.
1171          */
1172         if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1173                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1174                         boot_cpu_physical_apicid);
1175                 return -1;
1176         }
1177
1178         verify_local_APIC();
1179
1180         connect_bsp_APIC();
1181
1182         phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1183
1184         setup_local_APIC();
1185
1186         if (nmi_watchdog == NMI_LOCAL_APIC)
1187                 check_nmi_watchdog();
1188 #ifdef CONFIG_X86_IO_APIC
1189         if (smp_found_config)
1190                 if (!skip_ioapic_setup && nr_ioapics)
1191                         setup_IO_APIC();
1192 #endif
1193         setup_boot_APIC_clock();
1194
1195         return 0;
1196 }