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