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