blob: b95d1e378b96a19ec307ba8264edb3f3100e6258 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
22#include <linux/smp_lock.h>
23#include <linux/interrupt.h>
24#include <linux/mc146818rtc.h>
25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h>
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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>
Andi Kleen95833c82006-01-11 22:44:36 +010036#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010037#include <asm/proto.h>
38#include <asm/timex.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020039#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010042int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010043int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45int disable_apic_timer __initdata;
46
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010047/*
48 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
49 * IPIs in place of local APIC timers
50 */
51static cpumask_t timer_interrupt_broadcast_ipi_mask;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070054int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static void apic_pm_activate(void);
57
58void enable_NMI_through_LVT0 (void * dummy)
59{
Andi Kleen11a8e772006-01-11 22:46:51 +010060 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 v = APIC_DM_NMI; /* unmask and set to NMI */
Andi Kleen11a8e772006-01-11 22:46:51 +010063 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66int get_maxlvt(void)
67{
Andi Kleen11a8e772006-01-11 22:46:51 +010068 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 maxlvt = GET_APIC_MAXLVT(v);
72 return maxlvt;
73}
74
Andi Kleen3777a952006-02-03 21:51:53 +010075/*
76 * 'what should we do if we get a hw irq event on an illegal vector'.
77 * each architecture has to answer this themselves.
78 */
79void ack_bad_irq(unsigned int irq)
80{
81 printk("unexpected IRQ trap at vector %02x\n", irq);
82 /*
83 * Currently unexpected vectors happen only on SMP and APIC.
84 * We _must_ ack these because every local APIC has only N
85 * irq slots per priority level, and a 'hanging, unacked' IRQ
86 * holds up an irq slot - in excessive cases (when multiple
87 * unexpected vectors occur) that might lock up the APIC
88 * completely.
89 * But don't ack when the APIC is disabled. -AK
90 */
91 if (!disable_apic)
92 ack_APIC_irq();
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095void clear_local_APIC(void)
96{
97 int maxlvt;
98 unsigned int v;
99
100 maxlvt = get_maxlvt();
101
102 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200103 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * if the vector is zero. Mask LVTERR first to prevent this.
105 */
106 if (maxlvt >= 3) {
107 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100108 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 /*
111 * Careful: we have to set masks only first to deassert
112 * any level-triggered sources.
113 */
114 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100115 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100117 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100119 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (maxlvt >= 4) {
121 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100122 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
125 /*
126 * Clean APIC state for other OSs:
127 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100128 apic_write(APIC_LVTT, APIC_LVT_MASKED);
129 apic_write(APIC_LVT0, APIC_LVT_MASKED);
130 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100132 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100134 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 v = GET_APIC_VERSION(apic_read(APIC_LVR));
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200136 apic_write(APIC_ESR, 0);
137 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Eric W. Biederman208fb932005-06-25 14:57:45 -0700140void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200142 /* 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(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(APIC_LVT0, value);
161 } else {
162 /* Disable LVT0 */
163 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700165
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200166 /* For LVT1 make it edge triggered, active high, nmi and enabled */
167 value = apic_read(APIC_LVT1);
168 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700169 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
170 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200171 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
172 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
173 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176void disable_local_APIC(void)
177{
178 unsigned int value;
179
180 clear_local_APIC();
181
182 /*
183 * Disable APIC (implies clearing of registers
184 * for 82489DX!).
185 */
186 value = apic_read(APIC_SPIV);
187 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100188 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/*
192 * This is to verify that we're looking at a real local APIC.
193 * Check these against your board if the CPUs aren't getting
194 * started for no apparent reason.
195 */
196int __init verify_local_APIC(void)
197{
198 unsigned int reg0, reg1;
199
200 /*
201 * The version register is read-only in a real APIC.
202 */
203 reg0 = apic_read(APIC_LVR);
204 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
205 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
206 reg1 = apic_read(APIC_LVR);
207 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
208
209 /*
210 * The two version reads above should print the same
211 * numbers. If the second one is different, then we
212 * poke at a non-APIC.
213 */
214 if (reg1 != reg0)
215 return 0;
216
217 /*
218 * Check if the version looks reasonably.
219 */
220 reg1 = GET_APIC_VERSION(reg0);
221 if (reg1 == 0x00 || reg1 == 0xff)
222 return 0;
223 reg1 = get_maxlvt();
224 if (reg1 < 0x02 || reg1 == 0xff)
225 return 0;
226
227 /*
228 * The ID register is read/write in a real APIC.
229 */
230 reg0 = apic_read(APIC_ID);
231 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
232 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
233 reg1 = apic_read(APIC_ID);
234 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
235 apic_write(APIC_ID, reg0);
236 if (reg1 != (reg0 ^ APIC_ID_MASK))
237 return 0;
238
239 /*
240 * The next two are just to see if we have sane values.
241 * They're only really relevant if we're in Virtual Wire
242 * compatibility mode, but most boxes are anymore.
243 */
244 reg0 = apic_read(APIC_LVT0);
245 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
246 reg1 = apic_read(APIC_LVT1);
247 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
248
249 return 1;
250}
251
252void __init sync_Arb_IDs(void)
253{
254 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
255 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
256 if (ver >= 0x14) /* P4 or higher */
257 return;
258
259 /*
260 * Wait for idle.
261 */
262 apic_wait_icr_idle();
263
264 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100265 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 | APIC_DM_INIT);
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
270 * An initial setup of the virtual wire mode.
271 */
272void __init init_bsp_APIC(void)
273{
Andi Kleen11a8e772006-01-11 22:46:51 +0100274 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * Don't do the setup now if we have a SMP BIOS as the
278 * through-I/O-APIC virtual wire mode might be active.
279 */
280 if (smp_found_config || !cpu_has_apic)
281 return;
282
283 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * Do not trust the local APIC being empty at bootup.
287 */
288 clear_local_APIC();
289
290 /*
291 * Enable APIC.
292 */
293 value = apic_read(APIC_SPIV);
294 value &= ~APIC_VECTOR_MASK;
295 value |= APIC_SPIV_APIC_ENABLED;
296 value |= APIC_SPIV_FOCUS_DISABLED;
297 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100298 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /*
301 * Set up the virtual wire mode.
302 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100303 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100305 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Ashok Raje6982c62005-06-25 14:54:58 -0700308void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Andi Kleen11a8e772006-01-11 22:46:51 +0100310 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100311 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Andi Kleenfe7414a2006-09-26 10:52:30 +0200315 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
318 * Double-check whether this APIC is really registered.
319 * This is meaningless in clustered apic mode, so we skip it.
320 */
321 if (!apic_id_registered())
322 BUG();
323
324 /*
325 * Intel recommends to set DFR, LDR and TPR before enabling
326 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
327 * document number 292116). So here it goes...
328 */
329 init_apic_ldr();
330
331 /*
332 * Set Task Priority to 'accept all'. We never change this
333 * later on.
334 */
335 value = apic_read(APIC_TASKPRI);
336 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100337 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100340 * After a crash, we no longer service the interrupts and a pending
341 * interrupt from previous kernel might still have ISR bit set.
342 *
343 * Most probably by now CPU has serviced that pending interrupt and
344 * it might not have done the ack_APIC_irq() because it thought,
345 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
346 * does not clear the ISR bit and cpu thinks it has already serivced
347 * the interrupt. Hence a vector might get locked. It was noticed
348 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
349 */
350 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
351 value = apic_read(APIC_ISR + i*0x10);
352 for (j = 31; j >= 0; j--) {
353 if (value & (1<<j))
354 ack_APIC_irq();
355 }
356 }
357
358 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * Now that we are all set up, enable the APIC
360 */
361 value = apic_read(APIC_SPIV);
362 value &= ~APIC_VECTOR_MASK;
363 /*
364 * Enable APIC
365 */
366 value |= APIC_SPIV_APIC_ENABLED;
367
Andi Kleen3f14c742006-09-26 10:52:29 +0200368 /* We always use processor focus */
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Set spurious IRQ vector
372 */
373 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100374 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /*
377 * Set up LVT0, LVT1:
378 *
379 * set up through-local-APIC on the BP's LINT0. This is not
380 * strictly necessary in pure symmetric-IO mode, but sometimes
381 * we delegate interrupts to the 8259A.
382 */
383 /*
384 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
385 */
386 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200387 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 value = APIC_DM_EXTINT;
389 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
390 } else {
391 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
392 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
393 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100394 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * only the BP should see the LINT1 NMI signal, obviously.
398 */
399 if (!smp_processor_id())
400 value = APIC_DM_NMI;
401 else
402 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100403 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Andi Kleen61c11342005-09-12 18:49:23 +0200405 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 unsigned oldvalue;
407 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 oldvalue = apic_read(APIC_ESR);
409 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100410 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * spec says clear errors after enabling vector.
413 */
414 if (maxlvt > 3)
415 apic_write(APIC_ESR, 0);
416 value = apic_read(APIC_ESR);
417 if (value != oldvalue)
418 apic_printk(APIC_VERBOSE,
419 "ESR value after enabling vector: %08x, after %08x\n",
420 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200424 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 apic_pm_activate();
426}
427
428#ifdef CONFIG_PM
429
430static struct {
431 /* 'active' is true if the local APIC was enabled by us and
432 not the BIOS; this signifies that we are also responsible
433 for disabling it before entering apm/acpi suspend */
434 int active;
435 /* r/w apic fields */
436 unsigned int apic_id;
437 unsigned int apic_taskpri;
438 unsigned int apic_ldr;
439 unsigned int apic_dfr;
440 unsigned int apic_spiv;
441 unsigned int apic_lvtt;
442 unsigned int apic_lvtpc;
443 unsigned int apic_lvt0;
444 unsigned int apic_lvt1;
445 unsigned int apic_lvterr;
446 unsigned int apic_tmict;
447 unsigned int apic_tdcr;
448 unsigned int apic_thmr;
449} apic_pm_state;
450
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700451static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 unsigned long flags;
454
455 if (!apic_pm_state.active)
456 return 0;
457
458 apic_pm_state.apic_id = apic_read(APIC_ID);
459 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
460 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
461 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
462 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
463 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
464 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
465 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
466 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
467 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
468 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
469 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
470 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200471 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 disable_local_APIC();
473 local_irq_restore(flags);
474 return 0;
475}
476
477static int lapic_resume(struct sys_device *dev)
478{
479 unsigned int l, h;
480 unsigned long flags;
481
482 if (!apic_pm_state.active)
483 return 0;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 local_irq_save(flags);
486 rdmsr(MSR_IA32_APICBASE, l, h);
487 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100488 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 wrmsr(MSR_IA32_APICBASE, l, h);
490 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
491 apic_write(APIC_ID, apic_pm_state.apic_id);
492 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
493 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
494 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
495 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
496 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
497 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
498 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
499 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
500 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
501 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
502 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
503 apic_write(APIC_ESR, 0);
504 apic_read(APIC_ESR);
505 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
506 apic_write(APIC_ESR, 0);
507 apic_read(APIC_ESR);
508 local_irq_restore(flags);
509 return 0;
510}
511
512static struct sysdev_class lapic_sysclass = {
513 set_kset_name("lapic"),
514 .resume = lapic_resume,
515 .suspend = lapic_suspend,
516};
517
518static struct sys_device device_lapic = {
519 .id = 0,
520 .cls = &lapic_sysclass,
521};
522
Ashok Raje6982c62005-06-25 14:54:58 -0700523static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 apic_pm_state.active = 1;
526}
527
528static int __init init_lapic_sysfs(void)
529{
530 int error;
531 if (!cpu_has_apic)
532 return 0;
533 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
534 error = sysdev_class_register(&lapic_sysclass);
535 if (!error)
536 error = sysdev_register(&device_lapic);
537 return error;
538}
539device_initcall(init_lapic_sysfs);
540
541#else /* CONFIG_PM */
542
543static void apic_pm_activate(void) { }
544
545#endif /* CONFIG_PM */
546
547static int __init apic_set_verbosity(char *str)
548{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200549 if (str == NULL) {
550 skip_ioapic_setup = 0;
551 ioapic_force = 1;
552 return 0;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (strcmp("debug", str) == 0)
555 apic_verbosity = APIC_DEBUG;
556 else if (strcmp("verbose", str) == 0)
557 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200558 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200560 " use apic=verbose or apic=debug\n", str);
561 return -EINVAL;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200566early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * Detect and enable local APICs on non-SMP boards.
570 * Original code written by Keir Fraser.
571 * On AMD64 we trust the BIOS - if it says no APIC it is likely
572 * not correctly set up (usually the APIC timer won't work etc.)
573 */
574
575static int __init detect_init_APIC (void)
576{
577 if (!cpu_has_apic) {
578 printk(KERN_INFO "No local APIC present\n");
579 return -1;
580 }
581
582 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
583 boot_cpu_id = 0;
584 return 0;
585}
586
587void __init init_apic_mappings(void)
588{
589 unsigned long apic_phys;
590
591 /*
592 * If no local APIC can be found then set up a fake all
593 * zeroes page to simulate the local APIC and another
594 * one for the IO-APIC.
595 */
596 if (!smp_found_config && detect_init_APIC()) {
597 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
598 apic_phys = __pa(apic_phys);
599 } else
600 apic_phys = mp_lapic_addr;
601
602 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
603 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
604
605 /*
606 * Fetch the APIC ID of the BSP in case we have a
607 * default configuration (or the MP table is broken).
608 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200609 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 {
612 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
613 int i;
614
615 for (i = 0; i < nr_ioapics; i++) {
616 if (smp_found_config) {
617 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
618 } else {
619 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
620 ioapic_phys = __pa(ioapic_phys);
621 }
622 set_fixmap_nocache(idx, ioapic_phys);
623 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
624 __fix_to_virt(idx), ioapic_phys);
625 idx++;
626 }
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/*
631 * This function sets up the local APIC timer, with a timeout of
632 * 'clocks' APIC bus clock. During calibration we actually call
633 * this function twice on the boot CPU, once with a bogus timeout
634 * value, second time for real. The other (noncalibrating) CPUs
635 * call this function only once, with the real, calibrated value.
636 *
637 * We do reads before writes even if unnecessary, to get around the
638 * P5 APIC double write bug.
639 */
640
641#define APIC_DIVISOR 16
642
643static void __setup_APIC_LVTT(unsigned int clocks)
644{
645 unsigned int lvtt_value, tmp_value, ver;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100646 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
649 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100650
651 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
652 lvtt_value |= APIC_LVT_MASKED;
653
Andi Kleen11a8e772006-01-11 22:46:51 +0100654 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * Divide PICLK by 16
658 */
659 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100660 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
662 | APIC_TDR_DIV_16);
663
Andi Kleen11a8e772006-01-11 22:46:51 +0100664 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static void setup_APIC_timer(unsigned int clocks)
668{
669 unsigned long flags;
670
671 local_irq_save(flags);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* wait for irq slice */
Chris McDermott33042a92006-02-11 17:55:50 -0800674 if (vxtime.hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int trigger = hpet_readl(HPET_T0_CMP);
676 while (hpet_readl(HPET_COUNTER) >= trigger)
677 /* do nothing */ ;
678 while (hpet_readl(HPET_COUNTER) < trigger)
679 /* do nothing */ ;
680 } else {
681 int c1, c2;
682 outb_p(0x00, 0x43);
683 c2 = inb_p(0x40);
684 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100685 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 c1 = c2;
687 outb_p(0x00, 0x43);
688 c2 = inb_p(0x40);
689 c2 |= inb_p(0x40) << 8;
690 } while (c2 - c1 < 300);
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100693 /* Turn off PIT interrupt if we use APIC timer as main timer.
694 Only works with the PM timer right now
695 TBD fix it for HPET too. */
696 if (vxtime.mode == VXTIME_PMTMR &&
697 smp_processor_id() == boot_cpu_id &&
698 apic_runs_main_timer == 1 &&
699 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
700 stop_timer_interrupt();
701 apic_runs_main_timer++;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 local_irq_restore(flags);
704}
705
706/*
707 * In this function we calibrate APIC bus clocks to the external
708 * timer. Unfortunately we cannot use jiffies and the timer irq
709 * to calibrate, since some later bootup code depends on getting
710 * the first irq? Ugh.
711 *
712 * We want to do the calibration only once since we
713 * want to have local timer irqs syncron. CPUs connected
714 * by the same APIC bus have the very same bus frequency.
715 * And we want to have irqs off anyways, no accidental
716 * APIC irq that way.
717 */
718
719#define TICK_COUNT 100000000
720
721static int __init calibrate_APIC_clock(void)
722{
723 int apic, apic_start, tsc, tsc_start;
724 int result;
725 /*
726 * Put whatever arbitrary (but long enough) timeout
727 * value into the APIC clock, we just want to get the
728 * counter running for calibration.
729 */
730 __setup_APIC_LVTT(1000000000);
731
732 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100733#ifdef CONFIG_X86_PM_TIMER
734 if (apic_calibrate_pmtmr && pmtmr_ioport) {
735 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100737 result = (apic_start - apic) * 1000L / 5;
738 } else
739#endif
740 {
741 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Andi Kleen0c3749c2006-02-03 21:51:41 +0100743 do {
744 apic = apic_read(APIC_TMCCT);
745 rdtscl(tsc);
746 } while ((tsc - tsc_start) < TICK_COUNT &&
747 (apic - apic_start) < TICK_COUNT);
748
749 result = (apic_start - apic) * 1000L * cpu_khz /
750 (tsc - tsc_start);
751 }
752 printk("result %d\n", result);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
756 result / 1000 / 1000, result / 1000 % 1000);
757
758 return result * APIC_DIVISOR / HZ;
759}
760
761static unsigned int calibration_result;
762
763void __init setup_boot_APIC_clock (void)
764{
765 if (disable_apic_timer) {
766 printk(KERN_INFO "Disabling APIC timer\n");
767 return;
768 }
769
770 printk(KERN_INFO "Using local APIC timer interrupts.\n");
771 using_apic_timer = 1;
772
773 local_irq_disable();
774
775 calibration_result = calibrate_APIC_clock();
776 /*
777 * Now set up the timer for real.
778 */
779 setup_APIC_timer(calibration_result);
780
781 local_irq_enable();
782}
783
Ashok Raje6982c62005-06-25 14:54:58 -0700784void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 local_irq_disable(); /* FIXME: Do we need this? --RR */
787 setup_APIC_timer(calibration_result);
788 local_irq_enable();
789}
790
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100791void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 if (using_apic_timer) {
794 unsigned long v;
795
796 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200797 /*
798 * When an illegal vector value (0-15) is written to an LVT
799 * entry and delivery mode is Fixed, the APIC may signal an
800 * illegal vector error, with out regard to whether the mask
801 * bit is set or whether an interrupt is actually seen on input.
802 *
803 * Boot sequence might call this function when the LVTT has
804 * '0' vector value. So make sure vector field is set to
805 * valid value.
806 */
807 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
808 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810}
811
812void enable_APIC_timer(void)
813{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100814 int cpu = smp_processor_id();
815
816 if (using_apic_timer &&
817 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 unsigned long v;
819
820 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100821 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823}
824
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100825void switch_APIC_timer_to_ipi(void *cpumask)
826{
827 cpumask_t mask = *(cpumask_t *)cpumask;
828 int cpu = smp_processor_id();
829
830 if (cpu_isset(cpu, mask) &&
831 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
832 disable_APIC_timer();
833 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
834 }
835}
836EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
837
838void smp_send_timer_broadcast_ipi(void)
839{
840 cpumask_t mask;
841
842 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
843 if (!cpus_empty(mask)) {
844 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
845 }
846}
847
848void switch_ipi_to_APIC_timer(void *cpumask)
849{
850 cpumask_t mask = *(cpumask_t *)cpumask;
851 int cpu = smp_processor_id();
852
853 if (cpu_isset(cpu, mask) &&
854 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
855 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
856 enable_APIC_timer();
857 }
858}
859EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861int setup_profiling_timer(unsigned int multiplier)
862{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100863 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Jacob Shin17fc14f2006-06-26 13:58:47 +0200866void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
867 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100868{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200869 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
870 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100871 apic_write(reg, v);
872}
Jacob Shin89b831e2005-11-05 17:25:53 +0100873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#undef APIC_DIVISOR
875
876/*
877 * Local timer interrupt handler. It does both profiling and
878 * process statistics/rescheduling.
879 *
880 * We do profiling in every local tick, statistics/rescheduling
881 * happen only every 'profiling multiplier' ticks. The default
882 * multiplier is 1 and it can be changed by writing the new multiplier
883 * value into /proc/profile.
884 */
885
886void smp_local_timer_interrupt(struct pt_regs *regs)
887{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#ifdef CONFIG_SMP
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100890 update_process_times(user_mode(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100892 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
893 main_timer_handler(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * We take the 'long' return path, and there every subsystem
896 * grabs the appropriate locks (kernel lock/ irq lock).
897 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +0200898 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * and do the profiling totally in assembly.
900 *
901 * Currently this isn't too much of an issue (performance wise),
902 * we can take more than 100K local irqs per second on a 100 MHz P5.
903 */
904}
905
906/*
907 * Local APIC timer interrupt. This is the most natural way for doing
908 * local interrupts, but local timer interrupts can be emulated by
909 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
910 *
911 * [ if a single-CPU system runs an SMP kernel then we call the local
912 * interrupt as well. Thus we cannot inline the local irq ... ]
913 */
914void smp_apic_timer_interrupt(struct pt_regs *regs)
915{
916 /*
917 * the NMI deadlock-detector uses this.
918 */
919 add_pda(apic_timer_irqs, 1);
920
921 /*
922 * NOTE! We'd better ACK the irq immediately,
923 * because timer handling can be slow.
924 */
925 ack_APIC_irq();
926 /*
927 * update_process_times() expects us to have done irq_enter().
928 * Besides, if we don't timer interrupts ignore the global
929 * interrupt lock, which is the WrongThing (tm) to do.
930 */
Andi Kleen95833c82006-01-11 22:44:36 +0100931 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 irq_enter();
933 smp_local_timer_interrupt(regs);
934 irq_exit();
935}
936
937/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200938 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
940 * Thus far, the major user of this is IBM's Summit2 series:
941 *
Linus Torvalds637029c2006-02-27 20:41:56 -0800942 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * multi-chassis. Use available data to take a good guess.
944 * If in doubt, go HPET.
945 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200946__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 int i, clusters, zeros;
949 unsigned id;
950 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
951
Suresh Siddha376ec332005-05-16 21:53:32 -0700952 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 for (i = 0; i < NR_CPUS; i++) {
955 id = bios_cpu_apicid[i];
956 if (id != BAD_APICID)
957 __set_bit(APIC_CLUSTERID(id), clustermap);
958 }
959
960 /* Problem: Partially populated chassis may not have CPUs in some of
961 * the APIC clusters they have been allocated. Only present CPUs have
962 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
963 * clusters are allocated sequentially, count zeros only if they are
964 * bounded by ones.
965 */
966 clusters = 0;
967 zeros = 0;
968 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
969 if (test_bit(i, clustermap)) {
970 clusters += 1 + zeros;
971 zeros = 0;
972 } else
973 ++zeros;
974 }
975
976 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200977 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * May have to revisit this when multi-core + hyperthreaded CPUs come
979 * out, but AFAIK this will work even for them.
980 */
981 return (clusters > 2);
982}
983
984/*
985 * This interrupt should _never_ happen with our APIC/SMP architecture
986 */
987asmlinkage void smp_spurious_interrupt(void)
988{
989 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +0100990 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 irq_enter();
992 /*
993 * Check if this really is a spurious interrupt and ACK it
994 * if it is a vectored one. Just in case...
995 * Spurious interrupts should not be ACKed.
996 */
997 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
998 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
999 ack_APIC_irq();
1000
1001#if 0
1002 static unsigned long last_warning;
1003 static unsigned long skipped;
1004
1005 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1006 if (time_before(last_warning+30*HZ,jiffies)) {
1007 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1008 smp_processor_id(), skipped);
1009 last_warning = jiffies;
1010 skipped = 0;
1011 } else {
1012 skipped++;
1013 }
1014#endif
1015 irq_exit();
1016}
1017
1018/*
1019 * This interrupt should never happen with our APIC/SMP architecture
1020 */
1021
1022asmlinkage void smp_error_interrupt(void)
1023{
1024 unsigned int v, v1;
1025
Andi Kleen95833c82006-01-11 22:44:36 +01001026 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 irq_enter();
1028 /* First tickle the hardware, only then report what went on. -- REW */
1029 v = apic_read(APIC_ESR);
1030 apic_write(APIC_ESR, 0);
1031 v1 = apic_read(APIC_ESR);
1032 ack_APIC_irq();
1033 atomic_inc(&irq_err_count);
1034
1035 /* Here is what the APIC error bits mean:
1036 0: Send CS error
1037 1: Receive CS error
1038 2: Send accept error
1039 3: Receive accept error
1040 4: Reserved
1041 5: Send illegal vector
1042 6: Received illegal vector
1043 7: Illegal register address
1044 */
1045 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1046 smp_processor_id(), v , v1);
1047 irq_exit();
1048}
1049
1050int disable_apic;
1051
1052/*
1053 * This initializes the IO-APIC and APIC hardware if this is
1054 * a UP kernel.
1055 */
1056int __init APIC_init_uniprocessor (void)
1057{
1058 if (disable_apic) {
1059 printk(KERN_INFO "Apic disabled\n");
1060 return -1;
1061 }
1062 if (!cpu_has_apic) {
1063 disable_apic = 1;
1064 printk(KERN_INFO "Apic disabled by BIOS\n");
1065 return -1;
1066 }
1067
1068 verify_local_APIC();
1069
Andi Kleen357e11d2005-09-12 18:49:24 +02001070 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001071 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 setup_local_APIC();
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001076 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 else
1078 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001080 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083
1084static __init int setup_disableapic(char *str)
1085{
1086 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001087 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1088 return 0;
1089}
1090early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001092/* same as disableapic, for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093static __init int setup_nolapic(char *str)
1094{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001095 return setup_disableapic(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001097early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099static __init int setup_noapictimer(char *str)
1100{
Andi Kleen73dea472006-02-03 21:50:50 +01001101 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Andi Kleen73dea472006-02-03 21:50:50 +01001107static __init int setup_apicmaintimer(char *str)
1108{
1109 apic_runs_main_timer = 1;
1110 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001111 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001112}
1113__setup("apicmaintimer", setup_apicmaintimer);
1114
1115static __init int setup_noapicmaintimer(char *str)
1116{
1117 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001118 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001119}
1120__setup("noapicmaintimer", setup_noapicmaintimer);
1121
Andi Kleen0c3749c2006-02-03 21:51:41 +01001122static __init int setup_apicpmtimer(char *s)
1123{
1124 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001125 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001126 return setup_apicmaintimer(NULL);
1127}
1128__setup("apicpmtimer", setup_apicpmtimer);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130__setup("noapictimer", setup_noapictimer);
1131