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