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