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