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