blob: af4a1c71a80ddd971b144c9c1dddab03aff81331 [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
Andi Kleenb7f5e3c2006-09-26 10:52:34 +020041int apic_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010043int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010044int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46int disable_apic_timer __initdata;
47
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010048/*
49 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
50 * IPIs in place of local APIC timers
51 */
52static cpumask_t timer_interrupt_broadcast_ipi_mask;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070055int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void apic_pm_activate(void);
58
59void enable_NMI_through_LVT0 (void * dummy)
60{
Andi Kleen11a8e772006-01-11 22:46:51 +010061 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 v = APIC_DM_NMI; /* unmask and set to NMI */
Andi Kleen11a8e772006-01-11 22:46:51 +010064 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67int get_maxlvt(void)
68{
Andi Kleen11a8e772006-01-11 22:46:51 +010069 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 maxlvt = GET_APIC_MAXLVT(v);
73 return maxlvt;
74}
75
Andi Kleen3777a952006-02-03 21:51:53 +010076/*
77 * 'what should we do if we get a hw irq event on an illegal vector'.
78 * each architecture has to answer this themselves.
79 */
80void ack_bad_irq(unsigned int irq)
81{
82 printk("unexpected IRQ trap at vector %02x\n", irq);
83 /*
84 * Currently unexpected vectors happen only on SMP and APIC.
85 * We _must_ ack these because every local APIC has only N
86 * irq slots per priority level, and a 'hanging, unacked' IRQ
87 * holds up an irq slot - in excessive cases (when multiple
88 * unexpected vectors occur) that might lock up the APIC
89 * completely.
90 * But don't ack when the APIC is disabled. -AK
91 */
92 if (!disable_apic)
93 ack_APIC_irq();
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096void clear_local_APIC(void)
97{
98 int maxlvt;
99 unsigned int v;
100
101 maxlvt = get_maxlvt();
102
103 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200104 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * if the vector is zero. Mask LVTERR first to prevent this.
106 */
107 if (maxlvt >= 3) {
108 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100109 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 /*
112 * Careful: we have to set masks only first to deassert
113 * any level-triggered sources.
114 */
115 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100116 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100118 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100120 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (maxlvt >= 4) {
122 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100123 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
126 /*
127 * Clean APIC state for other OSs:
128 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100129 apic_write(APIC_LVTT, APIC_LVT_MASKED);
130 apic_write(APIC_LVT0, APIC_LVT_MASKED);
131 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100133 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100135 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 v = GET_APIC_VERSION(apic_read(APIC_LVR));
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200137 apic_write(APIC_ESR, 0);
138 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Eric W. Biederman208fb932005-06-25 14:57:45 -0700141void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200143 /* Go back to Virtual Wire compatibility mode */
144 unsigned long value;
145
146 /* For the spurious interrupt use vector F, and enable it */
147 value = apic_read(APIC_SPIV);
148 value &= ~APIC_VECTOR_MASK;
149 value |= APIC_SPIV_APIC_ENABLED;
150 value |= 0xf;
151 apic_write(APIC_SPIV, value);
152
153 if (!virt_wire_setup) {
154 /* For LVT0 make it edge triggered, active high, external and enabled */
155 value = apic_read(APIC_LVT0);
156 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
157 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
158 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
159 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
160 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
161 apic_write(APIC_LVT0, value);
162 } else {
163 /* Disable LVT0 */
164 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700166
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200167 /* For LVT1 make it edge triggered, active high, nmi and enabled */
168 value = apic_read(APIC_LVT1);
169 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700170 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
171 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200172 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
173 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
174 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177void disable_local_APIC(void)
178{
179 unsigned int value;
180
181 clear_local_APIC();
182
183 /*
184 * Disable APIC (implies clearing of registers
185 * for 82489DX!).
186 */
187 value = apic_read(APIC_SPIV);
188 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100189 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/*
193 * This is to verify that we're looking at a real local APIC.
194 * Check these against your board if the CPUs aren't getting
195 * started for no apparent reason.
196 */
197int __init verify_local_APIC(void)
198{
199 unsigned int reg0, reg1;
200
201 /*
202 * The version register is read-only in a real APIC.
203 */
204 reg0 = apic_read(APIC_LVR);
205 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
206 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
207 reg1 = apic_read(APIC_LVR);
208 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
209
210 /*
211 * The two version reads above should print the same
212 * numbers. If the second one is different, then we
213 * poke at a non-APIC.
214 */
215 if (reg1 != reg0)
216 return 0;
217
218 /*
219 * Check if the version looks reasonably.
220 */
221 reg1 = GET_APIC_VERSION(reg0);
222 if (reg1 == 0x00 || reg1 == 0xff)
223 return 0;
224 reg1 = get_maxlvt();
225 if (reg1 < 0x02 || reg1 == 0xff)
226 return 0;
227
228 /*
229 * The ID register is read/write in a real APIC.
230 */
231 reg0 = apic_read(APIC_ID);
232 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
233 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
234 reg1 = apic_read(APIC_ID);
235 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
236 apic_write(APIC_ID, reg0);
237 if (reg1 != (reg0 ^ APIC_ID_MASK))
238 return 0;
239
240 /*
241 * The next two are just to see if we have sane values.
242 * They're only really relevant if we're in Virtual Wire
243 * compatibility mode, but most boxes are anymore.
244 */
245 reg0 = apic_read(APIC_LVT0);
246 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
247 reg1 = apic_read(APIC_LVT1);
248 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
249
250 return 1;
251}
252
253void __init sync_Arb_IDs(void)
254{
255 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
256 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
257 if (ver >= 0x14) /* P4 or higher */
258 return;
259
260 /*
261 * Wait for idle.
262 */
263 apic_wait_icr_idle();
264
265 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100266 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 | APIC_DM_INIT);
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
271 * An initial setup of the virtual wire mode.
272 */
273void __init init_bsp_APIC(void)
274{
Andi Kleen11a8e772006-01-11 22:46:51 +0100275 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
278 * Don't do the setup now if we have a SMP BIOS as the
279 * through-I/O-APIC virtual wire mode might be active.
280 */
281 if (smp_found_config || !cpu_has_apic)
282 return;
283
284 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /*
287 * Do not trust the local APIC being empty at bootup.
288 */
289 clear_local_APIC();
290
291 /*
292 * Enable APIC.
293 */
294 value = apic_read(APIC_SPIV);
295 value &= ~APIC_VECTOR_MASK;
296 value |= APIC_SPIV_APIC_ENABLED;
297 value |= APIC_SPIV_FOCUS_DISABLED;
298 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100299 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /*
302 * Set up the virtual wire mode.
303 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100304 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100306 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Ashok Raje6982c62005-06-25 14:54:58 -0700309void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Andi Kleen11a8e772006-01-11 22:46:51 +0100311 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100312 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Andi Kleenfe7414a2006-09-26 10:52:30 +0200316 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
319 * Double-check whether this APIC is really registered.
320 * This is meaningless in clustered apic mode, so we skip it.
321 */
322 if (!apic_id_registered())
323 BUG();
324
325 /*
326 * Intel recommends to set DFR, LDR and TPR before enabling
327 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
328 * document number 292116). So here it goes...
329 */
330 init_apic_ldr();
331
332 /*
333 * Set Task Priority to 'accept all'. We never change this
334 * later on.
335 */
336 value = apic_read(APIC_TASKPRI);
337 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100338 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100341 * After a crash, we no longer service the interrupts and a pending
342 * interrupt from previous kernel might still have ISR bit set.
343 *
344 * Most probably by now CPU has serviced that pending interrupt and
345 * it might not have done the ack_APIC_irq() because it thought,
346 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
347 * does not clear the ISR bit and cpu thinks it has already serivced
348 * the interrupt. Hence a vector might get locked. It was noticed
349 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
350 */
351 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
352 value = apic_read(APIC_ISR + i*0x10);
353 for (j = 31; j >= 0; j--) {
354 if (value & (1<<j))
355 ack_APIC_irq();
356 }
357 }
358
359 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * Now that we are all set up, enable the APIC
361 */
362 value = apic_read(APIC_SPIV);
363 value &= ~APIC_VECTOR_MASK;
364 /*
365 * Enable APIC
366 */
367 value |= APIC_SPIV_APIC_ENABLED;
368
Andi Kleen3f14c742006-09-26 10:52:29 +0200369 /* We always use processor focus */
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
372 * Set spurious IRQ vector
373 */
374 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100375 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /*
378 * Set up LVT0, LVT1:
379 *
380 * set up through-local-APIC on the BP's LINT0. This is not
381 * strictly necessary in pure symmetric-IO mode, but sometimes
382 * we delegate interrupts to the 8259A.
383 */
384 /*
385 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
386 */
387 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200388 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 value = APIC_DM_EXTINT;
390 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
391 } else {
392 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
393 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
394 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100395 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * only the BP should see the LINT1 NMI signal, obviously.
399 */
400 if (!smp_processor_id())
401 value = APIC_DM_NMI;
402 else
403 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100404 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Andi Kleen61c11342005-09-12 18:49:23 +0200406 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned oldvalue;
408 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 oldvalue = apic_read(APIC_ESR);
410 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100411 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * spec says clear errors after enabling vector.
414 */
415 if (maxlvt > 3)
416 apic_write(APIC_ESR, 0);
417 value = apic_read(APIC_ESR);
418 if (value != oldvalue)
419 apic_printk(APIC_VERBOSE,
420 "ESR value after enabling vector: %08x, after %08x\n",
421 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200425 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 apic_pm_activate();
427}
428
429#ifdef CONFIG_PM
430
431static struct {
432 /* 'active' is true if the local APIC was enabled by us and
433 not the BIOS; this signifies that we are also responsible
434 for disabling it before entering apm/acpi suspend */
435 int active;
436 /* r/w apic fields */
437 unsigned int apic_id;
438 unsigned int apic_taskpri;
439 unsigned int apic_ldr;
440 unsigned int apic_dfr;
441 unsigned int apic_spiv;
442 unsigned int apic_lvtt;
443 unsigned int apic_lvtpc;
444 unsigned int apic_lvt0;
445 unsigned int apic_lvt1;
446 unsigned int apic_lvterr;
447 unsigned int apic_tmict;
448 unsigned int apic_tdcr;
449 unsigned int apic_thmr;
450} apic_pm_state;
451
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700452static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 unsigned long flags;
455
456 if (!apic_pm_state.active)
457 return 0;
458
459 apic_pm_state.apic_id = apic_read(APIC_ID);
460 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
461 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
462 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
463 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
464 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
465 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
466 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
467 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
468 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
469 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
470 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
471 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200472 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 disable_local_APIC();
474 local_irq_restore(flags);
475 return 0;
476}
477
478static int lapic_resume(struct sys_device *dev)
479{
480 unsigned int l, h;
481 unsigned long flags;
482
483 if (!apic_pm_state.active)
484 return 0;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 local_irq_save(flags);
487 rdmsr(MSR_IA32_APICBASE, l, h);
488 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100489 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 wrmsr(MSR_IA32_APICBASE, l, h);
491 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
492 apic_write(APIC_ID, apic_pm_state.apic_id);
493 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
494 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
495 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
496 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
497 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
498 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
499 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
500 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
501 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
502 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
503 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
504 apic_write(APIC_ESR, 0);
505 apic_read(APIC_ESR);
506 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
507 apic_write(APIC_ESR, 0);
508 apic_read(APIC_ESR);
509 local_irq_restore(flags);
510 return 0;
511}
512
513static struct sysdev_class lapic_sysclass = {
514 set_kset_name("lapic"),
515 .resume = lapic_resume,
516 .suspend = lapic_suspend,
517};
518
519static struct sys_device device_lapic = {
520 .id = 0,
521 .cls = &lapic_sysclass,
522};
523
Ashok Raje6982c62005-06-25 14:54:58 -0700524static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 apic_pm_state.active = 1;
527}
528
529static int __init init_lapic_sysfs(void)
530{
531 int error;
532 if (!cpu_has_apic)
533 return 0;
534 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
535 error = sysdev_class_register(&lapic_sysclass);
536 if (!error)
537 error = sysdev_register(&device_lapic);
538 return error;
539}
540device_initcall(init_lapic_sysfs);
541
542#else /* CONFIG_PM */
543
544static void apic_pm_activate(void) { }
545
546#endif /* CONFIG_PM */
547
548static int __init apic_set_verbosity(char *str)
549{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200550 if (str == NULL) {
551 skip_ioapic_setup = 0;
552 ioapic_force = 1;
553 return 0;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (strcmp("debug", str) == 0)
556 apic_verbosity = APIC_DEBUG;
557 else if (strcmp("verbose", str) == 0)
558 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200559 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200561 " use apic=verbose or apic=debug\n", str);
562 return -EINVAL;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200567early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/*
570 * Detect and enable local APICs on non-SMP boards.
571 * Original code written by Keir Fraser.
572 * On AMD64 we trust the BIOS - if it says no APIC it is likely
573 * not correctly set up (usually the APIC timer won't work etc.)
574 */
575
576static int __init detect_init_APIC (void)
577{
578 if (!cpu_has_apic) {
579 printk(KERN_INFO "No local APIC present\n");
580 return -1;
581 }
582
583 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
584 boot_cpu_id = 0;
585 return 0;
586}
587
588void __init init_apic_mappings(void)
589{
590 unsigned long apic_phys;
591
592 /*
593 * If no local APIC can be found then set up a fake all
594 * zeroes page to simulate the local APIC and another
595 * one for the IO-APIC.
596 */
597 if (!smp_found_config && detect_init_APIC()) {
598 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
599 apic_phys = __pa(apic_phys);
600 } else
601 apic_phys = mp_lapic_addr;
602
603 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Andi Kleenb7f5e3c2006-09-26 10:52:34 +0200604 apic_mapped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
606
607 /*
608 * Fetch the APIC ID of the BSP in case we have a
609 * default configuration (or the MP table is broken).
610 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200611 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 {
614 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
615 int i;
616
617 for (i = 0; i < nr_ioapics; i++) {
618 if (smp_found_config) {
619 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
620 } else {
621 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
622 ioapic_phys = __pa(ioapic_phys);
623 }
624 set_fixmap_nocache(idx, ioapic_phys);
625 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
626 __fix_to_virt(idx), ioapic_phys);
627 idx++;
628 }
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632/*
633 * This function sets up the local APIC timer, with a timeout of
634 * 'clocks' APIC bus clock. During calibration we actually call
635 * this function twice on the boot CPU, once with a bogus timeout
636 * value, second time for real. The other (noncalibrating) CPUs
637 * call this function only once, with the real, calibrated value.
638 *
639 * We do reads before writes even if unnecessary, to get around the
640 * P5 APIC double write bug.
641 */
642
643#define APIC_DIVISOR 16
644
645static void __setup_APIC_LVTT(unsigned int clocks)
646{
647 unsigned int lvtt_value, tmp_value, ver;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100648 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
651 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100652
653 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
654 lvtt_value |= APIC_LVT_MASKED;
655
Andi Kleen11a8e772006-01-11 22:46:51 +0100656 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /*
659 * Divide PICLK by 16
660 */
661 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100662 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
664 | APIC_TDR_DIV_16);
665
Andi Kleen11a8e772006-01-11 22:46:51 +0100666 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static void setup_APIC_timer(unsigned int clocks)
670{
671 unsigned long flags;
672
673 local_irq_save(flags);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* wait for irq slice */
Chris McDermott33042a92006-02-11 17:55:50 -0800676 if (vxtime.hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int trigger = hpet_readl(HPET_T0_CMP);
678 while (hpet_readl(HPET_COUNTER) >= trigger)
679 /* do nothing */ ;
680 while (hpet_readl(HPET_COUNTER) < trigger)
681 /* do nothing */ ;
682 } else {
683 int c1, c2;
684 outb_p(0x00, 0x43);
685 c2 = inb_p(0x40);
686 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100687 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 c1 = c2;
689 outb_p(0x00, 0x43);
690 c2 = inb_p(0x40);
691 c2 |= inb_p(0x40) << 8;
692 } while (c2 - c1 < 300);
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100695 /* Turn off PIT interrupt if we use APIC timer as main timer.
696 Only works with the PM timer right now
697 TBD fix it for HPET too. */
698 if (vxtime.mode == VXTIME_PMTMR &&
699 smp_processor_id() == boot_cpu_id &&
700 apic_runs_main_timer == 1 &&
701 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
702 stop_timer_interrupt();
703 apic_runs_main_timer++;
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 local_irq_restore(flags);
706}
707
708/*
709 * In this function we calibrate APIC bus clocks to the external
710 * timer. Unfortunately we cannot use jiffies and the timer irq
711 * to calibrate, since some later bootup code depends on getting
712 * the first irq? Ugh.
713 *
714 * We want to do the calibration only once since we
715 * want to have local timer irqs syncron. CPUs connected
716 * by the same APIC bus have the very same bus frequency.
717 * And we want to have irqs off anyways, no accidental
718 * APIC irq that way.
719 */
720
721#define TICK_COUNT 100000000
722
723static int __init calibrate_APIC_clock(void)
724{
725 int apic, apic_start, tsc, tsc_start;
726 int result;
727 /*
728 * Put whatever arbitrary (but long enough) timeout
729 * value into the APIC clock, we just want to get the
730 * counter running for calibration.
731 */
732 __setup_APIC_LVTT(1000000000);
733
734 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100735#ifdef CONFIG_X86_PM_TIMER
736 if (apic_calibrate_pmtmr && pmtmr_ioport) {
737 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100739 result = (apic_start - apic) * 1000L / 5;
740 } else
741#endif
742 {
743 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Andi Kleen0c3749c2006-02-03 21:51:41 +0100745 do {
746 apic = apic_read(APIC_TMCCT);
747 rdtscl(tsc);
748 } while ((tsc - tsc_start) < TICK_COUNT &&
749 (apic - apic_start) < TICK_COUNT);
750
751 result = (apic_start - apic) * 1000L * cpu_khz /
752 (tsc - tsc_start);
753 }
754 printk("result %d\n", result);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
758 result / 1000 / 1000, result / 1000 % 1000);
759
760 return result * APIC_DIVISOR / HZ;
761}
762
763static unsigned int calibration_result;
764
765void __init setup_boot_APIC_clock (void)
766{
767 if (disable_apic_timer) {
768 printk(KERN_INFO "Disabling APIC timer\n");
769 return;
770 }
771
772 printk(KERN_INFO "Using local APIC timer interrupts.\n");
773 using_apic_timer = 1;
774
775 local_irq_disable();
776
777 calibration_result = calibrate_APIC_clock();
778 /*
779 * Now set up the timer for real.
780 */
781 setup_APIC_timer(calibration_result);
782
783 local_irq_enable();
784}
785
Ashok Raje6982c62005-06-25 14:54:58 -0700786void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 local_irq_disable(); /* FIXME: Do we need this? --RR */
789 setup_APIC_timer(calibration_result);
790 local_irq_enable();
791}
792
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100793void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 if (using_apic_timer) {
796 unsigned long v;
797
798 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200799 /*
800 * When an illegal vector value (0-15) is written to an LVT
801 * entry and delivery mode is Fixed, the APIC may signal an
802 * illegal vector error, with out regard to whether the mask
803 * bit is set or whether an interrupt is actually seen on input.
804 *
805 * Boot sequence might call this function when the LVTT has
806 * '0' vector value. So make sure vector field is set to
807 * valid value.
808 */
809 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
810 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812}
813
814void enable_APIC_timer(void)
815{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100816 int cpu = smp_processor_id();
817
818 if (using_apic_timer &&
819 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned long v;
821
822 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100823 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100827void switch_APIC_timer_to_ipi(void *cpumask)
828{
829 cpumask_t mask = *(cpumask_t *)cpumask;
830 int cpu = smp_processor_id();
831
832 if (cpu_isset(cpu, mask) &&
833 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
834 disable_APIC_timer();
835 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
836 }
837}
838EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
839
840void smp_send_timer_broadcast_ipi(void)
841{
842 cpumask_t mask;
843
844 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
845 if (!cpus_empty(mask)) {
846 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
847 }
848}
849
850void switch_ipi_to_APIC_timer(void *cpumask)
851{
852 cpumask_t mask = *(cpumask_t *)cpumask;
853 int cpu = smp_processor_id();
854
855 if (cpu_isset(cpu, mask) &&
856 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
857 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
858 enable_APIC_timer();
859 }
860}
861EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863int setup_profiling_timer(unsigned int multiplier)
864{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100865 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Jacob Shin17fc14f2006-06-26 13:58:47 +0200868void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
869 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100870{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200871 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
872 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100873 apic_write(reg, v);
874}
Jacob Shin89b831e2005-11-05 17:25:53 +0100875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#undef APIC_DIVISOR
877
878/*
879 * Local timer interrupt handler. It does both profiling and
880 * process statistics/rescheduling.
881 *
882 * We do profiling in every local tick, statistics/rescheduling
883 * happen only every 'profiling multiplier' ticks. The default
884 * multiplier is 1 and it can be changed by writing the new multiplier
885 * value into /proc/profile.
886 */
887
David Howells7d12e782006-10-05 14:55:46 +0100888void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
David Howells7d12e782006-10-05 14:55:46 +0100890 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +0100892 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100894 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
David Howells7d12e782006-10-05 14:55:46 +0100895 main_timer_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * We take the 'long' return path, and there every subsystem
898 * grabs the appropriate locks (kernel lock/ irq lock).
899 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +0200900 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * and do the profiling totally in assembly.
902 *
903 * Currently this isn't too much of an issue (performance wise),
904 * we can take more than 100K local irqs per second on a 100 MHz P5.
905 */
906}
907
908/*
909 * Local APIC timer interrupt. This is the most natural way for doing
910 * local interrupts, but local timer interrupts can be emulated by
911 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
912 *
913 * [ if a single-CPU system runs an SMP kernel then we call the local
914 * interrupt as well. Thus we cannot inline the local irq ... ]
915 */
David Howells7d12e782006-10-05 14:55:46 +0100916void smp_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 /*
919 * the NMI deadlock-detector uses this.
920 */
921 add_pda(apic_timer_irqs, 1);
922
923 /*
924 * NOTE! We'd better ACK the irq immediately,
925 * because timer handling can be slow.
926 */
927 ack_APIC_irq();
928 /*
929 * update_process_times() expects us to have done irq_enter().
930 * Besides, if we don't timer interrupts ignore the global
931 * interrupt lock, which is the WrongThing (tm) to do.
932 */
Andi Kleen95833c82006-01-11 22:44:36 +0100933 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +0100935 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 irq_exit();
937}
938
939/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200940 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 *
942 * Thus far, the major user of this is IBM's Summit2 series:
943 *
Linus Torvalds637029c2006-02-27 20:41:56 -0800944 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * multi-chassis. Use available data to take a good guess.
946 * If in doubt, go HPET.
947 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200948__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 int i, clusters, zeros;
951 unsigned id;
952 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
953
Suresh Siddha376ec33f2005-05-16 21:53:32 -0700954 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 for (i = 0; i < NR_CPUS; i++) {
957 id = bios_cpu_apicid[i];
958 if (id != BAD_APICID)
959 __set_bit(APIC_CLUSTERID(id), clustermap);
960 }
961
962 /* Problem: Partially populated chassis may not have CPUs in some of
963 * the APIC clusters they have been allocated. Only present CPUs have
964 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
965 * clusters are allocated sequentially, count zeros only if they are
966 * bounded by ones.
967 */
968 clusters = 0;
969 zeros = 0;
970 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
971 if (test_bit(i, clustermap)) {
972 clusters += 1 + zeros;
973 zeros = 0;
974 } else
975 ++zeros;
976 }
977
978 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200979 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * May have to revisit this when multi-core + hyperthreaded CPUs come
981 * out, but AFAIK this will work even for them.
982 */
983 return (clusters > 2);
984}
985
986/*
987 * This interrupt should _never_ happen with our APIC/SMP architecture
988 */
989asmlinkage void smp_spurious_interrupt(void)
990{
991 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +0100992 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 irq_enter();
994 /*
995 * Check if this really is a spurious interrupt and ACK it
996 * if it is a vectored one. Just in case...
997 * Spurious interrupts should not be ACKed.
998 */
999 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1000 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1001 ack_APIC_irq();
1002
1003#if 0
1004 static unsigned long last_warning;
1005 static unsigned long skipped;
1006
1007 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1008 if (time_before(last_warning+30*HZ,jiffies)) {
1009 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1010 smp_processor_id(), skipped);
1011 last_warning = jiffies;
1012 skipped = 0;
1013 } else {
1014 skipped++;
1015 }
1016#endif
1017 irq_exit();
1018}
1019
1020/*
1021 * This interrupt should never happen with our APIC/SMP architecture
1022 */
1023
1024asmlinkage void smp_error_interrupt(void)
1025{
1026 unsigned int v, v1;
1027
Andi Kleen95833c82006-01-11 22:44:36 +01001028 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 irq_enter();
1030 /* First tickle the hardware, only then report what went on. -- REW */
1031 v = apic_read(APIC_ESR);
1032 apic_write(APIC_ESR, 0);
1033 v1 = apic_read(APIC_ESR);
1034 ack_APIC_irq();
1035 atomic_inc(&irq_err_count);
1036
1037 /* Here is what the APIC error bits mean:
1038 0: Send CS error
1039 1: Receive CS error
1040 2: Send accept error
1041 3: Receive accept error
1042 4: Reserved
1043 5: Send illegal vector
1044 6: Received illegal vector
1045 7: Illegal register address
1046 */
1047 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1048 smp_processor_id(), v , v1);
1049 irq_exit();
1050}
1051
1052int disable_apic;
1053
1054/*
1055 * This initializes the IO-APIC and APIC hardware if this is
1056 * a UP kernel.
1057 */
1058int __init APIC_init_uniprocessor (void)
1059{
1060 if (disable_apic) {
1061 printk(KERN_INFO "Apic disabled\n");
1062 return -1;
1063 }
1064 if (!cpu_has_apic) {
1065 disable_apic = 1;
1066 printk(KERN_INFO "Apic disabled by BIOS\n");
1067 return -1;
1068 }
1069
1070 verify_local_APIC();
1071
Andi Kleen357e11d2005-09-12 18:49:24 +02001072 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001073 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 setup_local_APIC();
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001078 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 else
1080 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001082 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084}
1085
1086static __init int setup_disableapic(char *str)
1087{
1088 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001089 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1090 return 0;
1091}
1092early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001094/* same as disableapic, for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static __init int setup_nolapic(char *str)
1096{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001097 return setup_disableapic(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001099early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101static __init int setup_noapictimer(char *str)
1102{
Andi Kleen73dea472006-02-03 21:50:50 +01001103 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001106 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Andi Kleen73dea472006-02-03 21:50:50 +01001109static __init int setup_apicmaintimer(char *str)
1110{
1111 apic_runs_main_timer = 1;
1112 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001113 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001114}
1115__setup("apicmaintimer", setup_apicmaintimer);
1116
1117static __init int setup_noapicmaintimer(char *str)
1118{
1119 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001120 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001121}
1122__setup("noapicmaintimer", setup_noapicmaintimer);
1123
Andi Kleen0c3749c2006-02-03 21:51:41 +01001124static __init int setup_apicpmtimer(char *s)
1125{
1126 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001127 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001128 return setup_apicmaintimer(NULL);
1129}
1130__setup("apicpmtimer", setup_apicpmtimer);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132__setup("noapictimer", setup_noapictimer);
1133