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