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