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