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