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