blob: c3239f6c22be3fc9b1f72bb7cb5bf9f95338e990 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
17#include <linux/config.h>
18#include <linux/init.h>
19
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
22#include <linux/bootmem.h>
23#include <linux/smp_lock.h>
24#include <linux/interrupt.h>
25#include <linux/mc146818rtc.h>
26#include <linux/kernel_stat.h>
27#include <linux/sysdev.h>
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +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/pgalloc.h>
35#include <asm/mach_apic.h>
Andi Kleen75152112005-05-16 21:53:34 -070036#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010037#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39int apic_verbosity;
40
41int disable_apic_timer __initdata;
42
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010043/*
44 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
45 * IPIs in place of local APIC timers
46 */
47static cpumask_t timer_interrupt_broadcast_ipi_mask;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Using APIC to generate smp_local_timer_interrupt? */
50int using_apic_timer = 0;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static void apic_pm_activate(void);
53
54void enable_NMI_through_LVT0 (void * dummy)
55{
56 unsigned int v, ver;
57
58 ver = apic_read(APIC_LVR);
59 ver = GET_APIC_VERSION(ver);
60 v = APIC_DM_NMI; /* unmask and set to NMI */
61 apic_write_around(APIC_LVT0, v);
62}
63
64int get_maxlvt(void)
65{
66 unsigned int v, ver, maxlvt;
67
68 v = apic_read(APIC_LVR);
69 ver = GET_APIC_VERSION(v);
70 maxlvt = GET_APIC_MAXLVT(v);
71 return maxlvt;
72}
73
74void clear_local_APIC(void)
75{
76 int maxlvt;
77 unsigned int v;
78
79 maxlvt = get_maxlvt();
80
81 /*
82 * Masking an LVT entry on a P6 can trigger a local APIC error
83 * if the vector is zero. Mask LVTERR first to prevent this.
84 */
85 if (maxlvt >= 3) {
86 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
87 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
88 }
89 /*
90 * Careful: we have to set masks only first to deassert
91 * any level-triggered sources.
92 */
93 v = apic_read(APIC_LVTT);
94 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
95 v = apic_read(APIC_LVT0);
96 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
97 v = apic_read(APIC_LVT1);
98 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
99 if (maxlvt >= 4) {
100 v = apic_read(APIC_LVTPC);
101 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
102 }
103
104 /*
105 * Clean APIC state for other OSs:
106 */
107 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
108 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
109 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
110 if (maxlvt >= 3)
111 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
112 if (maxlvt >= 4)
113 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
114 v = GET_APIC_VERSION(apic_read(APIC_LVR));
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200115 apic_write(APIC_ESR, 0);
116 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119void __init connect_bsp_APIC(void)
120{
121 if (pic_mode) {
122 /*
123 * Do not trust the local APIC being empty at bootup.
124 */
125 clear_local_APIC();
126 /*
127 * PIC mode, enable APIC mode in the IMCR, i.e.
128 * connect BSP's local APIC to INT and NMI lines.
129 */
130 apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
131 outb(0x70, 0x22);
132 outb(0x01, 0x23);
133 }
134}
135
Eric W. Biederman208fb932005-06-25 14:57:45 -0700136void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 if (pic_mode) {
139 /*
140 * Put the board back into PIC mode (has an effect
141 * only on certain older boards). Note that APIC
142 * interrupts, including IPIs, won't work beyond
143 * this point! The only exception are INIT IPIs.
144 */
145 apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
146 outb(0x70, 0x22);
147 outb(0x00, 0x23);
148 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700149 else {
150 /* Go back to Virtual Wire compatibility mode */
151 unsigned long value;
152
153 /* For the spurious interrupt use vector F, and enable it */
154 value = apic_read(APIC_SPIV);
155 value &= ~APIC_VECTOR_MASK;
156 value |= APIC_SPIV_APIC_ENABLED;
157 value |= 0xf;
158 apic_write_around(APIC_SPIV, value);
159
160 if (!virt_wire_setup) {
161 /* For LVT0 make it edge triggered, active high, external and enabled */
162 value = apic_read(APIC_LVT0);
163 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
164 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
165 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
166 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
167 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
168 apic_write_around(APIC_LVT0, value);
169 }
170 else {
171 /* Disable LVT0 */
172 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
173 }
174
175 /* For LVT1 make it edge triggered, active high, nmi and enabled */
176 value = apic_read(APIC_LVT1);
177 value &= ~(
178 APIC_MODE_MASK | APIC_SEND_PENDING |
179 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
180 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
181 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
182 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
183 apic_write_around(APIC_LVT1, value);
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187void disable_local_APIC(void)
188{
189 unsigned int value;
190
191 clear_local_APIC();
192
193 /*
194 * Disable APIC (implies clearing of registers
195 * for 82489DX!).
196 */
197 value = apic_read(APIC_SPIV);
198 value &= ~APIC_SPIV_APIC_ENABLED;
199 apic_write_around(APIC_SPIV, value);
200}
201
202/*
203 * This is to verify that we're looking at a real local APIC.
204 * Check these against your board if the CPUs aren't getting
205 * started for no apparent reason.
206 */
207int __init verify_local_APIC(void)
208{
209 unsigned int reg0, reg1;
210
211 /*
212 * The version register is read-only in a real APIC.
213 */
214 reg0 = apic_read(APIC_LVR);
215 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
216 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
217 reg1 = apic_read(APIC_LVR);
218 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
219
220 /*
221 * The two version reads above should print the same
222 * numbers. If the second one is different, then we
223 * poke at a non-APIC.
224 */
225 if (reg1 != reg0)
226 return 0;
227
228 /*
229 * Check if the version looks reasonably.
230 */
231 reg1 = GET_APIC_VERSION(reg0);
232 if (reg1 == 0x00 || reg1 == 0xff)
233 return 0;
234 reg1 = get_maxlvt();
235 if (reg1 < 0x02 || reg1 == 0xff)
236 return 0;
237
238 /*
239 * The ID register is read/write in a real APIC.
240 */
241 reg0 = apic_read(APIC_ID);
242 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
243 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
244 reg1 = apic_read(APIC_ID);
245 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
246 apic_write(APIC_ID, reg0);
247 if (reg1 != (reg0 ^ APIC_ID_MASK))
248 return 0;
249
250 /*
251 * The next two are just to see if we have sane values.
252 * They're only really relevant if we're in Virtual Wire
253 * compatibility mode, but most boxes are anymore.
254 */
255 reg0 = apic_read(APIC_LVT0);
256 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
257 reg1 = apic_read(APIC_LVT1);
258 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
259
260 return 1;
261}
262
263void __init sync_Arb_IDs(void)
264{
265 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
266 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
267 if (ver >= 0x14) /* P4 or higher */
268 return;
269
270 /*
271 * Wait for idle.
272 */
273 apic_wait_icr_idle();
274
275 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
276 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
277 | APIC_DM_INIT);
278}
279
280extern void __error_in_apic_c (void);
281
282/*
283 * An initial setup of the virtual wire mode.
284 */
285void __init init_bsp_APIC(void)
286{
287 unsigned int value, ver;
288
289 /*
290 * Don't do the setup now if we have a SMP BIOS as the
291 * through-I/O-APIC virtual wire mode might be active.
292 */
293 if (smp_found_config || !cpu_has_apic)
294 return;
295
296 value = apic_read(APIC_LVR);
297 ver = GET_APIC_VERSION(value);
298
299 /*
300 * Do not trust the local APIC being empty at bootup.
301 */
302 clear_local_APIC();
303
304 /*
305 * Enable APIC.
306 */
307 value = apic_read(APIC_SPIV);
308 value &= ~APIC_VECTOR_MASK;
309 value |= APIC_SPIV_APIC_ENABLED;
310 value |= APIC_SPIV_FOCUS_DISABLED;
311 value |= SPURIOUS_APIC_VECTOR;
312 apic_write_around(APIC_SPIV, value);
313
314 /*
315 * Set up the virtual wire mode.
316 */
317 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
318 value = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 apic_write_around(APIC_LVT1, value);
320}
321
Ashok Raje6982c62005-06-25 14:54:58 -0700322void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 unsigned int value, ver, maxlvt;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 value = apic_read(APIC_LVR);
327 ver = GET_APIC_VERSION(value);
328
329 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
330 __error_in_apic_c();
331
332 /*
333 * Double-check whether this APIC is really registered.
334 * This is meaningless in clustered apic mode, so we skip it.
335 */
336 if (!apic_id_registered())
337 BUG();
338
339 /*
340 * Intel recommends to set DFR, LDR and TPR before enabling
341 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
342 * document number 292116). So here it goes...
343 */
344 init_apic_ldr();
345
346 /*
347 * Set Task Priority to 'accept all'. We never change this
348 * later on.
349 */
350 value = apic_read(APIC_TASKPRI);
351 value &= ~APIC_TPRI_MASK;
352 apic_write_around(APIC_TASKPRI, value);
353
354 /*
355 * Now that we are all set up, enable the APIC
356 */
357 value = apic_read(APIC_SPIV);
358 value &= ~APIC_VECTOR_MASK;
359 /*
360 * Enable APIC
361 */
362 value |= APIC_SPIV_APIC_ENABLED;
363
364 /*
365 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
366 * certain networking cards. If high frequency interrupts are
367 * happening on a particular IOAPIC pin, plus the IOAPIC routing
368 * entry is masked/unmasked at a high rate as well then sooner or
369 * later IOAPIC line gets 'stuck', no more interrupts are received
370 * from the device. If focus CPU is disabled then the hang goes
371 * away, oh well :-(
372 *
373 * [ This bug can be reproduced easily with a level-triggered
374 * PCI Ne2000 networking cards and PII/PIII processors, dual
375 * BX chipset. ]
376 */
377 /*
378 * Actually disabling the focus CPU check just makes the hang less
379 * frequent as it makes the interrupt distributon model be more
380 * like LRU than MRU (the short-term load is more even across CPUs).
381 * See also the comment in end_level_ioapic_irq(). --macro
382 */
383#if 1
384 /* Enable focus processor (bit==0) */
385 value &= ~APIC_SPIV_FOCUS_DISABLED;
386#else
387 /* Disable focus processor (bit==1) */
388 value |= APIC_SPIV_FOCUS_DISABLED;
389#endif
390 /*
391 * Set spurious IRQ vector
392 */
393 value |= SPURIOUS_APIC_VECTOR;
394 apic_write_around(APIC_SPIV, value);
395
396 /*
397 * Set up LVT0, LVT1:
398 *
399 * set up through-local-APIC on the BP's LINT0. This is not
400 * strictly necessary in pure symmetric-IO mode, but sometimes
401 * we delegate interrupts to the 8259A.
402 */
403 /*
404 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
405 */
406 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
407 if (!smp_processor_id() && (pic_mode || !value)) {
408 value = APIC_DM_EXTINT;
409 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
410 } else {
411 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
412 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
413 }
414 apic_write_around(APIC_LVT0, value);
415
416 /*
417 * only the BP should see the LINT1 NMI signal, obviously.
418 */
419 if (!smp_processor_id())
420 value = APIC_DM_NMI;
421 else
422 value = APIC_DM_NMI | APIC_LVT_MASKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 apic_write_around(APIC_LVT1, value);
424
Andi Kleen61c11342005-09-12 18:49:23 +0200425 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned oldvalue;
427 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 oldvalue = apic_read(APIC_ESR);
429 value = ERROR_APIC_VECTOR; // enables sending errors
430 apic_write_around(APIC_LVTERR, value);
431 /*
432 * spec says clear errors after enabling vector.
433 */
434 if (maxlvt > 3)
435 apic_write(APIC_ESR, 0);
436 value = apic_read(APIC_ESR);
437 if (value != oldvalue)
438 apic_printk(APIC_VERBOSE,
439 "ESR value after enabling vector: %08x, after %08x\n",
440 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 nmi_watchdog_default();
444 if (nmi_watchdog == NMI_LOCAL_APIC)
445 setup_apic_nmi_watchdog();
446 apic_pm_activate();
447}
448
449#ifdef CONFIG_PM
450
451static struct {
452 /* 'active' is true if the local APIC was enabled by us and
453 not the BIOS; this signifies that we are also responsible
454 for disabling it before entering apm/acpi suspend */
455 int active;
456 /* r/w apic fields */
457 unsigned int apic_id;
458 unsigned int apic_taskpri;
459 unsigned int apic_ldr;
460 unsigned int apic_dfr;
461 unsigned int apic_spiv;
462 unsigned int apic_lvtt;
463 unsigned int apic_lvtpc;
464 unsigned int apic_lvt0;
465 unsigned int apic_lvt1;
466 unsigned int apic_lvterr;
467 unsigned int apic_tmict;
468 unsigned int apic_tdcr;
469 unsigned int apic_thmr;
470} apic_pm_state;
471
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700472static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 unsigned long flags;
475
476 if (!apic_pm_state.active)
477 return 0;
478
479 apic_pm_state.apic_id = apic_read(APIC_ID);
480 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
481 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
482 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
483 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
484 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
485 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
486 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
487 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
488 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
489 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
490 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
491 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
492 local_save_flags(flags);
493 local_irq_disable();
494 disable_local_APIC();
495 local_irq_restore(flags);
496 return 0;
497}
498
499static int lapic_resume(struct sys_device *dev)
500{
501 unsigned int l, h;
502 unsigned long flags;
503
504 if (!apic_pm_state.active)
505 return 0;
506
507 /* XXX: Pavel needs this for S3 resume, but can't explain why */
508 set_fixmap_nocache(FIX_APIC_BASE, APIC_DEFAULT_PHYS_BASE);
509
510 local_irq_save(flags);
511 rdmsr(MSR_IA32_APICBASE, l, h);
512 l &= ~MSR_IA32_APICBASE_BASE;
513 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
514 wrmsr(MSR_IA32_APICBASE, l, h);
515 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
516 apic_write(APIC_ID, apic_pm_state.apic_id);
517 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
518 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
519 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
520 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
521 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
522 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
523 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
524 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
525 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
526 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
527 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
528 apic_write(APIC_ESR, 0);
529 apic_read(APIC_ESR);
530 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
531 apic_write(APIC_ESR, 0);
532 apic_read(APIC_ESR);
533 local_irq_restore(flags);
534 return 0;
535}
536
537static struct sysdev_class lapic_sysclass = {
538 set_kset_name("lapic"),
539 .resume = lapic_resume,
540 .suspend = lapic_suspend,
541};
542
543static struct sys_device device_lapic = {
544 .id = 0,
545 .cls = &lapic_sysclass,
546};
547
Ashok Raje6982c62005-06-25 14:54:58 -0700548static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 apic_pm_state.active = 1;
551}
552
553static int __init init_lapic_sysfs(void)
554{
555 int error;
556 if (!cpu_has_apic)
557 return 0;
558 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
559 error = sysdev_class_register(&lapic_sysclass);
560 if (!error)
561 error = sysdev_register(&device_lapic);
562 return error;
563}
564device_initcall(init_lapic_sysfs);
565
566#else /* CONFIG_PM */
567
568static void apic_pm_activate(void) { }
569
570#endif /* CONFIG_PM */
571
572static int __init apic_set_verbosity(char *str)
573{
574 if (strcmp("debug", str) == 0)
575 apic_verbosity = APIC_DEBUG;
576 else if (strcmp("verbose", str) == 0)
577 apic_verbosity = APIC_VERBOSE;
578 else
579 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
580 " use apic=verbose or apic=debug", str);
581
582 return 0;
583}
584
585__setup("apic=", apic_set_verbosity);
586
587/*
588 * Detect and enable local APICs on non-SMP boards.
589 * Original code written by Keir Fraser.
590 * On AMD64 we trust the BIOS - if it says no APIC it is likely
591 * not correctly set up (usually the APIC timer won't work etc.)
592 */
593
594static int __init detect_init_APIC (void)
595{
596 if (!cpu_has_apic) {
597 printk(KERN_INFO "No local APIC present\n");
598 return -1;
599 }
600
601 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
602 boot_cpu_id = 0;
603 return 0;
604}
605
606void __init init_apic_mappings(void)
607{
608 unsigned long apic_phys;
609
610 /*
611 * If no local APIC can be found then set up a fake all
612 * zeroes page to simulate the local APIC and another
613 * one for the IO-APIC.
614 */
615 if (!smp_found_config && detect_init_APIC()) {
616 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
617 apic_phys = __pa(apic_phys);
618 } else
619 apic_phys = mp_lapic_addr;
620
621 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
622 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
623
624 /*
625 * Fetch the APIC ID of the BSP in case we have a
626 * default configuration (or the MP table is broken).
627 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200628 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630#ifdef CONFIG_X86_IO_APIC
631 {
632 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
633 int i;
634
635 for (i = 0; i < nr_ioapics; i++) {
636 if (smp_found_config) {
637 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
638 } else {
639 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
640 ioapic_phys = __pa(ioapic_phys);
641 }
642 set_fixmap_nocache(idx, ioapic_phys);
643 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
644 __fix_to_virt(idx), ioapic_phys);
645 idx++;
646 }
647 }
648#endif
649}
650
651/*
652 * This function sets up the local APIC timer, with a timeout of
653 * 'clocks' APIC bus clock. During calibration we actually call
654 * this function twice on the boot CPU, once with a bogus timeout
655 * value, second time for real. The other (noncalibrating) CPUs
656 * call this function only once, with the real, calibrated value.
657 *
658 * We do reads before writes even if unnecessary, to get around the
659 * P5 APIC double write bug.
660 */
661
662#define APIC_DIVISOR 16
663
664static void __setup_APIC_LVTT(unsigned int clocks)
665{
666 unsigned int lvtt_value, tmp_value, ver;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100667 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
670 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100671
672 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
673 lvtt_value |= APIC_LVT_MASKED;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 apic_write_around(APIC_LVTT, lvtt_value);
676
677 /*
678 * Divide PICLK by 16
679 */
680 tmp_value = apic_read(APIC_TDCR);
681 apic_write_around(APIC_TDCR, (tmp_value
682 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
683 | APIC_TDR_DIV_16);
684
685 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
686}
687
688static void setup_APIC_timer(unsigned int clocks)
689{
690 unsigned long flags;
691
692 local_irq_save(flags);
693
694 /* For some reasons this doesn't work on Simics, so fake it for now */
695 if (!strstr(boot_cpu_data.x86_model_id, "Screwdriver")) {
696 __setup_APIC_LVTT(clocks);
697 return;
698 }
699
700 /* wait for irq slice */
701 if (vxtime.hpet_address) {
702 int trigger = hpet_readl(HPET_T0_CMP);
703 while (hpet_readl(HPET_COUNTER) >= trigger)
704 /* do nothing */ ;
705 while (hpet_readl(HPET_COUNTER) < trigger)
706 /* do nothing */ ;
707 } else {
708 int c1, c2;
709 outb_p(0x00, 0x43);
710 c2 = inb_p(0x40);
711 c2 |= inb_p(0x40) << 8;
712 do {
713 c1 = c2;
714 outb_p(0x00, 0x43);
715 c2 = inb_p(0x40);
716 c2 |= inb_p(0x40) << 8;
717 } while (c2 - c1 < 300);
718 }
719
720 __setup_APIC_LVTT(clocks);
721
722 local_irq_restore(flags);
723}
724
725/*
726 * In this function we calibrate APIC bus clocks to the external
727 * timer. Unfortunately we cannot use jiffies and the timer irq
728 * to calibrate, since some later bootup code depends on getting
729 * the first irq? Ugh.
730 *
731 * We want to do the calibration only once since we
732 * want to have local timer irqs syncron. CPUs connected
733 * by the same APIC bus have the very same bus frequency.
734 * And we want to have irqs off anyways, no accidental
735 * APIC irq that way.
736 */
737
738#define TICK_COUNT 100000000
739
740static int __init calibrate_APIC_clock(void)
741{
742 int apic, apic_start, tsc, tsc_start;
743 int result;
744 /*
745 * Put whatever arbitrary (but long enough) timeout
746 * value into the APIC clock, we just want to get the
747 * counter running for calibration.
748 */
749 __setup_APIC_LVTT(1000000000);
750
751 apic_start = apic_read(APIC_TMCCT);
752 rdtscl(tsc_start);
753
754 do {
755 apic = apic_read(APIC_TMCCT);
756 rdtscl(tsc);
757 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
758
759 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
760
761 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
762 result / 1000 / 1000, result / 1000 % 1000);
763
764 return result * APIC_DIVISOR / HZ;
765}
766
767static unsigned int calibration_result;
768
769void __init setup_boot_APIC_clock (void)
770{
771 if (disable_apic_timer) {
772 printk(KERN_INFO "Disabling APIC timer\n");
773 return;
774 }
775
776 printk(KERN_INFO "Using local APIC timer interrupts.\n");
777 using_apic_timer = 1;
778
779 local_irq_disable();
780
781 calibration_result = calibrate_APIC_clock();
782 /*
783 * Now set up the timer for real.
784 */
785 setup_APIC_timer(calibration_result);
786
787 local_irq_enable();
788}
789
Ashok Raje6982c62005-06-25 14:54:58 -0700790void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 local_irq_disable(); /* FIXME: Do we need this? --RR */
793 setup_APIC_timer(calibration_result);
794 local_irq_enable();
795}
796
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100797void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 if (using_apic_timer) {
800 unsigned long v;
801
802 v = apic_read(APIC_LVTT);
803 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
804 }
805}
806
807void enable_APIC_timer(void)
808{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100809 int cpu = smp_processor_id();
810
811 if (using_apic_timer &&
812 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned long v;
814
815 v = apic_read(APIC_LVTT);
816 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
817 }
818}
819
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100820void switch_APIC_timer_to_ipi(void *cpumask)
821{
822 cpumask_t mask = *(cpumask_t *)cpumask;
823 int cpu = smp_processor_id();
824
825 if (cpu_isset(cpu, mask) &&
826 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
827 disable_APIC_timer();
828 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
829 }
830}
831EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
832
833void smp_send_timer_broadcast_ipi(void)
834{
835 cpumask_t mask;
836
837 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
838 if (!cpus_empty(mask)) {
839 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
840 }
841}
842
843void switch_ipi_to_APIC_timer(void *cpumask)
844{
845 cpumask_t mask = *(cpumask_t *)cpumask;
846 int cpu = smp_processor_id();
847
848 if (cpu_isset(cpu, mask) &&
849 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
850 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
851 enable_APIC_timer();
852 }
853}
854EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856int setup_profiling_timer(unsigned int multiplier)
857{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100858 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Jacob Shin89b831e2005-11-05 17:25:53 +0100861#ifdef CONFIG_X86_MCE_AMD
862void setup_threshold_lvt(unsigned long lvt_off)
863{
864 unsigned int v = 0;
865 unsigned long reg = (lvt_off << 4) + 0x500;
866 v |= THRESHOLD_APIC_VECTOR;
867 apic_write(reg, v);
868}
869#endif /* CONFIG_X86_MCE_AMD */
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#undef APIC_DIVISOR
872
873/*
874 * Local timer interrupt handler. It does both profiling and
875 * process statistics/rescheduling.
876 *
877 * We do profiling in every local tick, statistics/rescheduling
878 * happen only every 'profiling multiplier' ticks. The default
879 * multiplier is 1 and it can be changed by writing the new multiplier
880 * value into /proc/profile.
881 */
882
883void smp_local_timer_interrupt(struct pt_regs *regs)
884{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#ifdef CONFIG_SMP
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100887 update_process_times(user_mode(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /*
890 * We take the 'long' return path, and there every subsystem
891 * grabs the appropriate locks (kernel lock/ irq lock).
892 *
893 * we might want to decouple profiling from the 'long path',
894 * and do the profiling totally in assembly.
895 *
896 * Currently this isn't too much of an issue (performance wise),
897 * we can take more than 100K local irqs per second on a 100 MHz P5.
898 */
899}
900
901/*
902 * Local APIC timer interrupt. This is the most natural way for doing
903 * local interrupts, but local timer interrupts can be emulated by
904 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
905 *
906 * [ if a single-CPU system runs an SMP kernel then we call the local
907 * interrupt as well. Thus we cannot inline the local irq ... ]
908 */
909void smp_apic_timer_interrupt(struct pt_regs *regs)
910{
911 /*
912 * the NMI deadlock-detector uses this.
913 */
914 add_pda(apic_timer_irqs, 1);
915
916 /*
917 * NOTE! We'd better ACK the irq immediately,
918 * because timer handling can be slow.
919 */
920 ack_APIC_irq();
921 /*
922 * update_process_times() expects us to have done irq_enter().
923 * Besides, if we don't timer interrupts ignore the global
924 * interrupt lock, which is the WrongThing (tm) to do.
925 */
Andi Kleen95833c82006-01-11 22:44:36 +0100926 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 irq_enter();
928 smp_local_timer_interrupt(regs);
929 irq_exit();
930}
931
932/*
933 * oem_force_hpet_timer -- force HPET mode for some boxes.
934 *
935 * Thus far, the major user of this is IBM's Summit2 series:
936 *
937 * Clustered boxes may have unsynced TSC problems if they are
938 * multi-chassis. Use available data to take a good guess.
939 * If in doubt, go HPET.
940 */
941__init int oem_force_hpet_timer(void)
942{
943 int i, clusters, zeros;
944 unsigned id;
945 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
946
Suresh Siddha376ec332005-05-16 21:53:32 -0700947 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 for (i = 0; i < NR_CPUS; i++) {
950 id = bios_cpu_apicid[i];
951 if (id != BAD_APICID)
952 __set_bit(APIC_CLUSTERID(id), clustermap);
953 }
954
955 /* Problem: Partially populated chassis may not have CPUs in some of
956 * the APIC clusters they have been allocated. Only present CPUs have
957 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
958 * clusters are allocated sequentially, count zeros only if they are
959 * bounded by ones.
960 */
961 clusters = 0;
962 zeros = 0;
963 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
964 if (test_bit(i, clustermap)) {
965 clusters += 1 + zeros;
966 zeros = 0;
967 } else
968 ++zeros;
969 }
970
971 /*
972 * If clusters > 2, then should be multi-chassis. Return 1 for HPET.
973 * Else return 0 to use TSC.
974 * May have to revisit this when multi-core + hyperthreaded CPUs come
975 * out, but AFAIK this will work even for them.
976 */
977 return (clusters > 2);
978}
979
980/*
981 * This interrupt should _never_ happen with our APIC/SMP architecture
982 */
983asmlinkage void smp_spurious_interrupt(void)
984{
985 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +0100986 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 irq_enter();
988 /*
989 * Check if this really is a spurious interrupt and ACK it
990 * if it is a vectored one. Just in case...
991 * Spurious interrupts should not be ACKed.
992 */
993 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
994 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
995 ack_APIC_irq();
996
997#if 0
998 static unsigned long last_warning;
999 static unsigned long skipped;
1000
1001 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1002 if (time_before(last_warning+30*HZ,jiffies)) {
1003 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1004 smp_processor_id(), skipped);
1005 last_warning = jiffies;
1006 skipped = 0;
1007 } else {
1008 skipped++;
1009 }
1010#endif
1011 irq_exit();
1012}
1013
1014/*
1015 * This interrupt should never happen with our APIC/SMP architecture
1016 */
1017
1018asmlinkage void smp_error_interrupt(void)
1019{
1020 unsigned int v, v1;
1021
Andi Kleen95833c82006-01-11 22:44:36 +01001022 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 irq_enter();
1024 /* First tickle the hardware, only then report what went on. -- REW */
1025 v = apic_read(APIC_ESR);
1026 apic_write(APIC_ESR, 0);
1027 v1 = apic_read(APIC_ESR);
1028 ack_APIC_irq();
1029 atomic_inc(&irq_err_count);
1030
1031 /* Here is what the APIC error bits mean:
1032 0: Send CS error
1033 1: Receive CS error
1034 2: Send accept error
1035 3: Receive accept error
1036 4: Reserved
1037 5: Send illegal vector
1038 6: Received illegal vector
1039 7: Illegal register address
1040 */
1041 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1042 smp_processor_id(), v , v1);
1043 irq_exit();
1044}
1045
1046int disable_apic;
1047
1048/*
1049 * This initializes the IO-APIC and APIC hardware if this is
1050 * a UP kernel.
1051 */
1052int __init APIC_init_uniprocessor (void)
1053{
1054 if (disable_apic) {
1055 printk(KERN_INFO "Apic disabled\n");
1056 return -1;
1057 }
1058 if (!cpu_has_apic) {
1059 disable_apic = 1;
1060 printk(KERN_INFO "Apic disabled by BIOS\n");
1061 return -1;
1062 }
1063
1064 verify_local_APIC();
1065
1066 connect_bsp_APIC();
1067
Andi Kleen357e11d2005-09-12 18:49:24 +02001068 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Vivek Goyalb9d1e4b2006-01-11 22:45:09 +01001069 apic_write_around(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 setup_local_APIC();
1072
1073#ifdef CONFIG_X86_IO_APIC
1074 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1075 setup_IO_APIC();
1076 else
1077 nr_ioapics = 0;
1078#endif
1079 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001080 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083
1084static __init int setup_disableapic(char *str)
1085{
1086 disable_apic = 1;
1087 return 0;
1088}
1089
1090static __init int setup_nolapic(char *str)
1091{
1092 disable_apic = 1;
1093 return 0;
1094}
1095
1096static __init int setup_noapictimer(char *str)
1097{
1098 disable_apic_timer = 1;
1099 return 0;
1100}
1101
1102/* dummy parsing: see setup.c */
1103
1104__setup("disableapic", setup_disableapic);
1105__setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1106
1107__setup("noapictimer", setup_noapictimer);
1108
1109/* no "lapic" flag - we only use the lapic when the BIOS tells us so. */