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