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