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