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