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