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