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