blob: 124b2d27b4acd2e912d0d7db6f9dbe1a85bb5019 [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>
Aaron Durbin39928722006-12-07 02:14:01 +010028#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/atomic.h>
31#include <asm/smp.h>
32#include <asm/mtrr.h>
33#include <asm/mpspec.h>
34#include <asm/pgalloc.h>
35#include <asm/mach_apic.h>
Andi Kleen75152112005-05-16 21:53:34 -070036#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010037#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010038#include <asm/proto.h>
39#include <asm/timex.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020040#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Andi Kleenb7f5e3c2006-09-26 10:52:34 +020042int apic_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010044int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010045int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47int disable_apic_timer __initdata;
48
Aaron Durbin39928722006-12-07 02:14:01 +010049static struct resource *ioapic_resources;
50static struct resource lapic_resource = {
51 .name = "Local APIC",
52 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
53};
54
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010055/*
56 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
57 * IPIs in place of local APIC timers
58 */
59static cpumask_t timer_interrupt_broadcast_ipi_mask;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070062int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void apic_pm_activate(void);
65
66void enable_NMI_through_LVT0 (void * dummy)
67{
Andi Kleen11a8e772006-01-11 22:46:51 +010068 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 v = APIC_DM_NMI; /* unmask and set to NMI */
Andi Kleen11a8e772006-01-11 22:46:51 +010071 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74int get_maxlvt(void)
75{
Andi Kleen11a8e772006-01-11 22:46:51 +010076 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 maxlvt = GET_APIC_MAXLVT(v);
80 return maxlvt;
81}
82
Andi Kleen3777a952006-02-03 21:51:53 +010083/*
84 * 'what should we do if we get a hw irq event on an illegal vector'.
85 * each architecture has to answer this themselves.
86 */
87void ack_bad_irq(unsigned int irq)
88{
89 printk("unexpected IRQ trap at vector %02x\n", irq);
90 /*
91 * Currently unexpected vectors happen only on SMP and APIC.
92 * We _must_ ack these because every local APIC has only N
93 * irq slots per priority level, and a 'hanging, unacked' IRQ
94 * holds up an irq slot - in excessive cases (when multiple
95 * unexpected vectors occur) that might lock up the APIC
96 * completely.
97 * But don't ack when the APIC is disabled. -AK
98 */
99 if (!disable_apic)
100 ack_APIC_irq();
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103void clear_local_APIC(void)
104{
105 int maxlvt;
106 unsigned int v;
107
108 maxlvt = get_maxlvt();
109
110 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200111 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * if the vector is zero. Mask LVTERR first to prevent this.
113 */
114 if (maxlvt >= 3) {
115 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100116 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 /*
119 * Careful: we have to set masks only first to deassert
120 * any level-triggered sources.
121 */
122 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100123 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100125 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100127 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (maxlvt >= 4) {
129 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100130 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
133 /*
134 * Clean APIC state for other OSs:
135 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100136 apic_write(APIC_LVTT, APIC_LVT_MASKED);
137 apic_write(APIC_LVT0, APIC_LVT_MASKED);
138 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100140 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100142 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200143 apic_write(APIC_ESR, 0);
144 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Eric W. Biederman208fb932005-06-25 14:57:45 -0700147void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200149 /* Go back to Virtual Wire compatibility mode */
150 unsigned long value;
151
152 /* For the spurious interrupt use vector F, and enable it */
153 value = apic_read(APIC_SPIV);
154 value &= ~APIC_VECTOR_MASK;
155 value |= APIC_SPIV_APIC_ENABLED;
156 value |= 0xf;
157 apic_write(APIC_SPIV, value);
158
159 if (!virt_wire_setup) {
160 /* For LVT0 make it edge triggered, active high, external and enabled */
161 value = apic_read(APIC_LVT0);
162 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
163 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
164 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
165 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
166 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
167 apic_write(APIC_LVT0, value);
168 } else {
169 /* Disable LVT0 */
170 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700172
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200173 /* For LVT1 make it edge triggered, active high, nmi and enabled */
174 value = apic_read(APIC_LVT1);
175 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700176 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
177 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200178 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
179 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
180 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183void disable_local_APIC(void)
184{
185 unsigned int value;
186
187 clear_local_APIC();
188
189 /*
190 * Disable APIC (implies clearing of registers
191 * for 82489DX!).
192 */
193 value = apic_read(APIC_SPIV);
194 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100195 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * This is to verify that we're looking at a real local APIC.
200 * Check these against your board if the CPUs aren't getting
201 * started for no apparent reason.
202 */
203int __init verify_local_APIC(void)
204{
205 unsigned int reg0, reg1;
206
207 /*
208 * The version register is read-only in a real APIC.
209 */
210 reg0 = apic_read(APIC_LVR);
211 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
212 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
213 reg1 = apic_read(APIC_LVR);
214 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
215
216 /*
217 * The two version reads above should print the same
218 * numbers. If the second one is different, then we
219 * poke at a non-APIC.
220 */
221 if (reg1 != reg0)
222 return 0;
223
224 /*
225 * Check if the version looks reasonably.
226 */
227 reg1 = GET_APIC_VERSION(reg0);
228 if (reg1 == 0x00 || reg1 == 0xff)
229 return 0;
230 reg1 = get_maxlvt();
231 if (reg1 < 0x02 || reg1 == 0xff)
232 return 0;
233
234 /*
235 * The ID register is read/write in a real APIC.
236 */
237 reg0 = apic_read(APIC_ID);
238 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
239 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
240 reg1 = apic_read(APIC_ID);
241 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
242 apic_write(APIC_ID, reg0);
243 if (reg1 != (reg0 ^ APIC_ID_MASK))
244 return 0;
245
246 /*
247 * The next two are just to see if we have sane values.
248 * They're only really relevant if we're in Virtual Wire
249 * compatibility mode, but most boxes are anymore.
250 */
251 reg0 = apic_read(APIC_LVT0);
252 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
253 reg1 = apic_read(APIC_LVT1);
254 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
255
256 return 1;
257}
258
259void __init sync_Arb_IDs(void)
260{
261 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
262 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
263 if (ver >= 0x14) /* P4 or higher */
264 return;
265
266 /*
267 * Wait for idle.
268 */
269 apic_wait_icr_idle();
270
271 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100272 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 | APIC_DM_INIT);
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * An initial setup of the virtual wire mode.
278 */
279void __init init_bsp_APIC(void)
280{
Andi Kleen11a8e772006-01-11 22:46:51 +0100281 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /*
284 * Don't do the setup now if we have a SMP BIOS as the
285 * through-I/O-APIC virtual wire mode might be active.
286 */
287 if (smp_found_config || !cpu_has_apic)
288 return;
289
290 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * Do not trust the local APIC being empty at bootup.
294 */
295 clear_local_APIC();
296
297 /*
298 * Enable APIC.
299 */
300 value = apic_read(APIC_SPIV);
301 value &= ~APIC_VECTOR_MASK;
302 value |= APIC_SPIV_APIC_ENABLED;
303 value |= APIC_SPIV_FOCUS_DISABLED;
304 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100305 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /*
308 * Set up the virtual wire mode.
309 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100310 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100312 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Ashok Raje6982c62005-06-25 14:54:58 -0700315void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Andi Kleen11a8e772006-01-11 22:46:51 +0100317 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100318 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Andi Kleenfe7414a2006-09-26 10:52:30 +0200322 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /*
325 * Double-check whether this APIC is really registered.
326 * This is meaningless in clustered apic mode, so we skip it.
327 */
328 if (!apic_id_registered())
329 BUG();
330
331 /*
332 * Intel recommends to set DFR, LDR and TPR before enabling
333 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
334 * document number 292116). So here it goes...
335 */
336 init_apic_ldr();
337
338 /*
339 * Set Task Priority to 'accept all'. We never change this
340 * later on.
341 */
342 value = apic_read(APIC_TASKPRI);
343 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100344 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100347 * After a crash, we no longer service the interrupts and a pending
348 * interrupt from previous kernel might still have ISR bit set.
349 *
350 * Most probably by now CPU has serviced that pending interrupt and
351 * it might not have done the ack_APIC_irq() because it thought,
352 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
353 * does not clear the ISR bit and cpu thinks it has already serivced
354 * the interrupt. Hence a vector might get locked. It was noticed
355 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
356 */
357 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
358 value = apic_read(APIC_ISR + i*0x10);
359 for (j = 31; j >= 0; j--) {
360 if (value & (1<<j))
361 ack_APIC_irq();
362 }
363 }
364
365 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Now that we are all set up, enable the APIC
367 */
368 value = apic_read(APIC_SPIV);
369 value &= ~APIC_VECTOR_MASK;
370 /*
371 * Enable APIC
372 */
373 value |= APIC_SPIV_APIC_ENABLED;
374
Andi Kleen3f14c742006-09-26 10:52:29 +0200375 /* We always use processor focus */
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Set spurious IRQ vector
379 */
380 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100381 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Set up LVT0, LVT1:
385 *
386 * set up through-local-APIC on the BP's LINT0. This is not
387 * strictly necessary in pure symmetric-IO mode, but sometimes
388 * we delegate interrupts to the 8259A.
389 */
390 /*
391 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
392 */
393 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200394 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 value = APIC_DM_EXTINT;
396 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
397 } else {
398 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
399 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
400 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100401 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * only the BP should see the LINT1 NMI signal, obviously.
405 */
406 if (!smp_processor_id())
407 value = APIC_DM_NMI;
408 else
409 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100410 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Andi Kleen61c11342005-09-12 18:49:23 +0200412 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned oldvalue;
414 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 oldvalue = apic_read(APIC_ESR);
416 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100417 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * spec says clear errors after enabling vector.
420 */
421 if (maxlvt > 3)
422 apic_write(APIC_ESR, 0);
423 value = apic_read(APIC_ESR);
424 if (value != oldvalue)
425 apic_printk(APIC_VERBOSE,
426 "ESR value after enabling vector: %08x, after %08x\n",
427 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200431 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 apic_pm_activate();
433}
434
435#ifdef CONFIG_PM
436
437static struct {
438 /* 'active' is true if the local APIC was enabled by us and
439 not the BIOS; this signifies that we are also responsible
440 for disabling it before entering apm/acpi suspend */
441 int active;
442 /* r/w apic fields */
443 unsigned int apic_id;
444 unsigned int apic_taskpri;
445 unsigned int apic_ldr;
446 unsigned int apic_dfr;
447 unsigned int apic_spiv;
448 unsigned int apic_lvtt;
449 unsigned int apic_lvtpc;
450 unsigned int apic_lvt0;
451 unsigned int apic_lvt1;
452 unsigned int apic_lvterr;
453 unsigned int apic_tmict;
454 unsigned int apic_tdcr;
455 unsigned int apic_thmr;
456} apic_pm_state;
457
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700458static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100461 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (!apic_pm_state.active)
464 return 0;
465
Karsten Wiesef990fff2006-12-07 02:14:11 +0100466 maxlvt = get_maxlvt();
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 apic_pm_state.apic_id = apic_read(APIC_ID);
469 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
470 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
471 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
472 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
473 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100474 if (maxlvt >= 4)
475 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
477 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
478 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
479 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
480 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100481#ifdef CONFIG_X86_MCE_INTEL
482 if (maxlvt >= 5)
483 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
484#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200485 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 disable_local_APIC();
487 local_irq_restore(flags);
488 return 0;
489}
490
491static int lapic_resume(struct sys_device *dev)
492{
493 unsigned int l, h;
494 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100495 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (!apic_pm_state.active)
498 return 0;
499
Karsten Wiesef990fff2006-12-07 02:14:11 +0100500 maxlvt = get_maxlvt();
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 local_irq_save(flags);
503 rdmsr(MSR_IA32_APICBASE, l, h);
504 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100505 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 wrmsr(MSR_IA32_APICBASE, l, h);
507 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
508 apic_write(APIC_ID, apic_pm_state.apic_id);
509 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
510 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
511 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
512 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
513 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
514 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100515#ifdef CONFIG_X86_MCE_INTEL
516 if (maxlvt >= 5)
517 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
518#endif
519 if (maxlvt >= 4)
520 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
522 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
523 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
524 apic_write(APIC_ESR, 0);
525 apic_read(APIC_ESR);
526 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
527 apic_write(APIC_ESR, 0);
528 apic_read(APIC_ESR);
529 local_irq_restore(flags);
530 return 0;
531}
532
533static struct sysdev_class lapic_sysclass = {
534 set_kset_name("lapic"),
535 .resume = lapic_resume,
536 .suspend = lapic_suspend,
537};
538
539static struct sys_device device_lapic = {
540 .id = 0,
541 .cls = &lapic_sysclass,
542};
543
Ashok Raje6982c62005-06-25 14:54:58 -0700544static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 apic_pm_state.active = 1;
547}
548
549static int __init init_lapic_sysfs(void)
550{
551 int error;
552 if (!cpu_has_apic)
553 return 0;
554 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
555 error = sysdev_class_register(&lapic_sysclass);
556 if (!error)
557 error = sysdev_register(&device_lapic);
558 return error;
559}
560device_initcall(init_lapic_sysfs);
561
562#else /* CONFIG_PM */
563
564static void apic_pm_activate(void) { }
565
566#endif /* CONFIG_PM */
567
568static int __init apic_set_verbosity(char *str)
569{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200570 if (str == NULL) {
571 skip_ioapic_setup = 0;
572 ioapic_force = 1;
573 return 0;
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (strcmp("debug", str) == 0)
576 apic_verbosity = APIC_DEBUG;
577 else if (strcmp("verbose", str) == 0)
578 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200579 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200581 " use apic=verbose or apic=debug\n", str);
582 return -EINVAL;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200587early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/*
590 * Detect and enable local APICs on non-SMP boards.
591 * Original code written by Keir Fraser.
592 * On AMD64 we trust the BIOS - if it says no APIC it is likely
593 * not correctly set up (usually the APIC timer won't work etc.)
594 */
595
596static int __init detect_init_APIC (void)
597{
598 if (!cpu_has_apic) {
599 printk(KERN_INFO "No local APIC present\n");
600 return -1;
601 }
602
603 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
604 boot_cpu_id = 0;
605 return 0;
606}
607
Aaron Durbin39928722006-12-07 02:14:01 +0100608#ifdef CONFIG_X86_IO_APIC
609static struct resource * __init ioapic_setup_resources(void)
610{
611#define IOAPIC_RESOURCE_NAME_SIZE 11
612 unsigned long n;
613 struct resource *res;
614 char *mem;
615 int i;
616
617 if (nr_ioapics <= 0)
618 return NULL;
619
620 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
621 n *= nr_ioapics;
622
623 mem = alloc_bootmem(n);
624 res = (void *)mem;
625
626 if (mem != NULL) {
627 memset(mem, 0, n);
628 mem += sizeof(struct resource) * nr_ioapics;
629
630 for (i = 0; i < nr_ioapics; i++) {
631 res[i].name = mem;
632 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
633 sprintf(mem, "IOAPIC %u", i);
634 mem += IOAPIC_RESOURCE_NAME_SIZE;
635 }
636 }
637
638 ioapic_resources = res;
639
640 return res;
641}
642
643static int __init ioapic_insert_resources(void)
644{
645 int i;
646 struct resource *r = ioapic_resources;
647
648 if (!r) {
649 printk("IO APIC resources could be not be allocated.\n");
650 return -1;
651 }
652
653 for (i = 0; i < nr_ioapics; i++) {
654 insert_resource(&iomem_resource, r);
655 r++;
656 }
657
658 return 0;
659}
660
661/* Insert the IO APIC resources after PCI initialization has occured to handle
662 * IO APICS that are mapped in on a BAR in PCI space. */
663late_initcall(ioapic_insert_resources);
664#endif
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666void __init init_apic_mappings(void)
667{
668 unsigned long apic_phys;
669
670 /*
671 * If no local APIC can be found then set up a fake all
672 * zeroes page to simulate the local APIC and another
673 * one for the IO-APIC.
674 */
675 if (!smp_found_config && detect_init_APIC()) {
676 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
677 apic_phys = __pa(apic_phys);
678 } else
679 apic_phys = mp_lapic_addr;
680
681 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Andi Kleenb7f5e3c2006-09-26 10:52:34 +0200682 apic_mapped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
684
Aaron Durbin39928722006-12-07 02:14:01 +0100685 /* Put local APIC into the resource map. */
686 lapic_resource.start = apic_phys;
687 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
688 insert_resource(&iomem_resource, &lapic_resource);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Fetch the APIC ID of the BSP in case we have a
692 * default configuration (or the MP table is broken).
693 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200694 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 {
697 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
698 int i;
Aaron Durbin39928722006-12-07 02:14:01 +0100699 struct resource *ioapic_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Aaron Durbin39928722006-12-07 02:14:01 +0100701 ioapic_res = ioapic_setup_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 for (i = 0; i < nr_ioapics; i++) {
703 if (smp_found_config) {
704 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
705 } else {
706 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
707 ioapic_phys = __pa(ioapic_phys);
708 }
709 set_fixmap_nocache(idx, ioapic_phys);
710 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
711 __fix_to_virt(idx), ioapic_phys);
712 idx++;
Aaron Durbin39928722006-12-07 02:14:01 +0100713
714 if (ioapic_res != NULL) {
715 ioapic_res->start = ioapic_phys;
716 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
717 ioapic_res++;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
724 * This function sets up the local APIC timer, with a timeout of
725 * 'clocks' APIC bus clock. During calibration we actually call
726 * this function twice on the boot CPU, once with a bogus timeout
727 * value, second time for real. The other (noncalibrating) CPUs
728 * call this function only once, with the real, calibrated value.
729 *
730 * We do reads before writes even if unnecessary, to get around the
731 * P5 APIC double write bug.
732 */
733
734#define APIC_DIVISOR 16
735
736static void __setup_APIC_LVTT(unsigned int clocks)
737{
David Rientjes86bd58b2006-12-07 02:14:11 +0100738 unsigned int lvtt_value, tmp_value;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100739 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100742
743 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
744 lvtt_value |= APIC_LVT_MASKED;
745
Andi Kleen11a8e772006-01-11 22:46:51 +0100746 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 * Divide PICLK by 16
750 */
751 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100752 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
754 | APIC_TDR_DIV_16);
755
Andi Kleen11a8e772006-01-11 22:46:51 +0100756 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static void setup_APIC_timer(unsigned int clocks)
760{
761 unsigned long flags;
762
763 local_irq_save(flags);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /* wait for irq slice */
Chris McDermott33042a92006-02-11 17:55:50 -0800766 if (vxtime.hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int trigger = hpet_readl(HPET_T0_CMP);
768 while (hpet_readl(HPET_COUNTER) >= trigger)
769 /* do nothing */ ;
770 while (hpet_readl(HPET_COUNTER) < trigger)
771 /* do nothing */ ;
772 } else {
773 int c1, c2;
774 outb_p(0x00, 0x43);
775 c2 = inb_p(0x40);
776 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100777 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 c1 = c2;
779 outb_p(0x00, 0x43);
780 c2 = inb_p(0x40);
781 c2 |= inb_p(0x40) << 8;
782 } while (c2 - c1 < 300);
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100785 /* Turn off PIT interrupt if we use APIC timer as main timer.
786 Only works with the PM timer right now
787 TBD fix it for HPET too. */
788 if (vxtime.mode == VXTIME_PMTMR &&
789 smp_processor_id() == boot_cpu_id &&
790 apic_runs_main_timer == 1 &&
791 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
792 stop_timer_interrupt();
793 apic_runs_main_timer++;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 local_irq_restore(flags);
796}
797
798/*
799 * In this function we calibrate APIC bus clocks to the external
800 * timer. Unfortunately we cannot use jiffies and the timer irq
801 * to calibrate, since some later bootup code depends on getting
802 * the first irq? Ugh.
803 *
804 * We want to do the calibration only once since we
805 * want to have local timer irqs syncron. CPUs connected
806 * by the same APIC bus have the very same bus frequency.
807 * And we want to have irqs off anyways, no accidental
808 * APIC irq that way.
809 */
810
811#define TICK_COUNT 100000000
812
813static int __init calibrate_APIC_clock(void)
814{
815 int apic, apic_start, tsc, tsc_start;
816 int result;
817 /*
818 * Put whatever arbitrary (but long enough) timeout
819 * value into the APIC clock, we just want to get the
820 * counter running for calibration.
821 */
822 __setup_APIC_LVTT(1000000000);
823
824 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100825#ifdef CONFIG_X86_PM_TIMER
826 if (apic_calibrate_pmtmr && pmtmr_ioport) {
827 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100829 result = (apic_start - apic) * 1000L / 5;
830 } else
831#endif
832 {
833 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Andi Kleen0c3749c2006-02-03 21:51:41 +0100835 do {
836 apic = apic_read(APIC_TMCCT);
837 rdtscl(tsc);
838 } while ((tsc - tsc_start) < TICK_COUNT &&
839 (apic - apic_start) < TICK_COUNT);
840
841 result = (apic_start - apic) * 1000L * cpu_khz /
842 (tsc - tsc_start);
843 }
844 printk("result %d\n", result);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
848 result / 1000 / 1000, result / 1000 % 1000);
849
850 return result * APIC_DIVISOR / HZ;
851}
852
853static unsigned int calibration_result;
854
855void __init setup_boot_APIC_clock (void)
856{
857 if (disable_apic_timer) {
858 printk(KERN_INFO "Disabling APIC timer\n");
859 return;
860 }
861
862 printk(KERN_INFO "Using local APIC timer interrupts.\n");
863 using_apic_timer = 1;
864
865 local_irq_disable();
866
867 calibration_result = calibrate_APIC_clock();
868 /*
869 * Now set up the timer for real.
870 */
871 setup_APIC_timer(calibration_result);
872
873 local_irq_enable();
874}
875
Ashok Raje6982c62005-06-25 14:54:58 -0700876void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 local_irq_disable(); /* FIXME: Do we need this? --RR */
879 setup_APIC_timer(calibration_result);
880 local_irq_enable();
881}
882
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100883void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 if (using_apic_timer) {
886 unsigned long v;
887
888 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200889 /*
890 * When an illegal vector value (0-15) is written to an LVT
891 * entry and delivery mode is Fixed, the APIC may signal an
892 * illegal vector error, with out regard to whether the mask
893 * bit is set or whether an interrupt is actually seen on input.
894 *
895 * Boot sequence might call this function when the LVTT has
896 * '0' vector value. So make sure vector field is set to
897 * valid value.
898 */
899 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
900 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902}
903
904void enable_APIC_timer(void)
905{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100906 int cpu = smp_processor_id();
907
908 if (using_apic_timer &&
909 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned long v;
911
912 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100913 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915}
916
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100917void switch_APIC_timer_to_ipi(void *cpumask)
918{
919 cpumask_t mask = *(cpumask_t *)cpumask;
920 int cpu = smp_processor_id();
921
922 if (cpu_isset(cpu, mask) &&
923 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
924 disable_APIC_timer();
925 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
926 }
927}
928EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
929
930void smp_send_timer_broadcast_ipi(void)
931{
932 cpumask_t mask;
933
934 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
935 if (!cpus_empty(mask)) {
936 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
937 }
938}
939
940void switch_ipi_to_APIC_timer(void *cpumask)
941{
942 cpumask_t mask = *(cpumask_t *)cpumask;
943 int cpu = smp_processor_id();
944
945 if (cpu_isset(cpu, mask) &&
946 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
947 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
948 enable_APIC_timer();
949 }
950}
951EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953int setup_profiling_timer(unsigned int multiplier)
954{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100955 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Jacob Shin17fc14f2006-06-26 13:58:47 +0200958void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
959 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100960{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200961 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
962 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100963 apic_write(reg, v);
964}
Jacob Shin89b831e2005-11-05 17:25:53 +0100965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#undef APIC_DIVISOR
967
968/*
969 * Local timer interrupt handler. It does both profiling and
970 * process statistics/rescheduling.
971 *
972 * We do profiling in every local tick, statistics/rescheduling
973 * happen only every 'profiling multiplier' ticks. The default
974 * multiplier is 1 and it can be changed by writing the new multiplier
975 * value into /proc/profile.
976 */
977
David Howells7d12e782006-10-05 14:55:46 +0100978void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
David Howells7d12e782006-10-05 14:55:46 +0100980 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +0100982 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100984 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
David Howells7d12e782006-10-05 14:55:46 +0100985 main_timer_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /*
987 * We take the 'long' return path, and there every subsystem
988 * grabs the appropriate locks (kernel lock/ irq lock).
989 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +0200990 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * and do the profiling totally in assembly.
992 *
993 * Currently this isn't too much of an issue (performance wise),
994 * we can take more than 100K local irqs per second on a 100 MHz P5.
995 */
996}
997
998/*
999 * Local APIC timer interrupt. This is the most natural way for doing
1000 * local interrupts, but local timer interrupts can be emulated by
1001 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1002 *
1003 * [ if a single-CPU system runs an SMP kernel then we call the local
1004 * interrupt as well. Thus we cannot inline the local irq ... ]
1005 */
Andrew Mortond150ad72006-10-06 13:28:09 -07001006void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Andrew Mortond150ad72006-10-06 13:28:09 -07001008 struct pt_regs *old_regs = set_irq_regs(regs);
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
1011 * the NMI deadlock-detector uses this.
1012 */
1013 add_pda(apic_timer_irqs, 1);
1014
1015 /*
1016 * NOTE! We'd better ACK the irq immediately,
1017 * because timer handling can be slow.
1018 */
1019 ack_APIC_irq();
1020 /*
1021 * update_process_times() expects us to have done irq_enter().
1022 * Besides, if we don't timer interrupts ignore the global
1023 * interrupt lock, which is the WrongThing (tm) to do.
1024 */
Andi Kleen95833c82006-01-11 22:44:36 +01001025 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +01001027 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 irq_exit();
Andrew Mortond150ad72006-10-06 13:28:09 -07001029 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001033 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 *
1035 * Thus far, the major user of this is IBM's Summit2 series:
1036 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001037 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * multi-chassis. Use available data to take a good guess.
1039 * If in doubt, go HPET.
1040 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001041__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int i, clusters, zeros;
1044 unsigned id;
1045 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1046
Suresh Siddha376ec332005-05-16 21:53:32 -07001047 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 for (i = 0; i < NR_CPUS; i++) {
1050 id = bios_cpu_apicid[i];
1051 if (id != BAD_APICID)
1052 __set_bit(APIC_CLUSTERID(id), clustermap);
1053 }
1054
1055 /* Problem: Partially populated chassis may not have CPUs in some of
1056 * the APIC clusters they have been allocated. Only present CPUs have
1057 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1058 * clusters are allocated sequentially, count zeros only if they are
1059 * bounded by ones.
1060 */
1061 clusters = 0;
1062 zeros = 0;
1063 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1064 if (test_bit(i, clustermap)) {
1065 clusters += 1 + zeros;
1066 zeros = 0;
1067 } else
1068 ++zeros;
1069 }
1070
1071 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001072 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * May have to revisit this when multi-core + hyperthreaded CPUs come
1074 * out, but AFAIK this will work even for them.
1075 */
1076 return (clusters > 2);
1077}
1078
1079/*
1080 * This interrupt should _never_ happen with our APIC/SMP architecture
1081 */
1082asmlinkage void smp_spurious_interrupt(void)
1083{
1084 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001085 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 irq_enter();
1087 /*
1088 * Check if this really is a spurious interrupt and ACK it
1089 * if it is a vectored one. Just in case...
1090 * Spurious interrupts should not be ACKed.
1091 */
1092 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1093 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1094 ack_APIC_irq();
1095
1096#if 0
1097 static unsigned long last_warning;
1098 static unsigned long skipped;
1099
1100 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1101 if (time_before(last_warning+30*HZ,jiffies)) {
1102 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1103 smp_processor_id(), skipped);
1104 last_warning = jiffies;
1105 skipped = 0;
1106 } else {
1107 skipped++;
1108 }
1109#endif
1110 irq_exit();
1111}
1112
1113/*
1114 * This interrupt should never happen with our APIC/SMP architecture
1115 */
1116
1117asmlinkage void smp_error_interrupt(void)
1118{
1119 unsigned int v, v1;
1120
Andi Kleen95833c82006-01-11 22:44:36 +01001121 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 irq_enter();
1123 /* First tickle the hardware, only then report what went on. -- REW */
1124 v = apic_read(APIC_ESR);
1125 apic_write(APIC_ESR, 0);
1126 v1 = apic_read(APIC_ESR);
1127 ack_APIC_irq();
1128 atomic_inc(&irq_err_count);
1129
1130 /* Here is what the APIC error bits mean:
1131 0: Send CS error
1132 1: Receive CS error
1133 2: Send accept error
1134 3: Receive accept error
1135 4: Reserved
1136 5: Send illegal vector
1137 6: Received illegal vector
1138 7: Illegal register address
1139 */
1140 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1141 smp_processor_id(), v , v1);
1142 irq_exit();
1143}
1144
1145int disable_apic;
1146
1147/*
1148 * This initializes the IO-APIC and APIC hardware if this is
1149 * a UP kernel.
1150 */
1151int __init APIC_init_uniprocessor (void)
1152{
1153 if (disable_apic) {
1154 printk(KERN_INFO "Apic disabled\n");
1155 return -1;
1156 }
1157 if (!cpu_has_apic) {
1158 disable_apic = 1;
1159 printk(KERN_INFO "Apic disabled by BIOS\n");
1160 return -1;
1161 }
1162
1163 verify_local_APIC();
1164
Andi Kleen357e11d2005-09-12 18:49:24 +02001165 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001166 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 setup_local_APIC();
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001171 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 else
1173 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001175 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177}
1178
1179static __init int setup_disableapic(char *str)
1180{
1181 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001182 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1183 return 0;
1184}
1185early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001187/* same as disableapic, for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188static __init int setup_nolapic(char *str)
1189{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001190 return setup_disableapic(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001192early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194static __init int setup_noapictimer(char *str)
1195{
Andi Kleen73dea472006-02-03 21:50:50 +01001196 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001199 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Andi Kleen73dea472006-02-03 21:50:50 +01001202static __init int setup_apicmaintimer(char *str)
1203{
1204 apic_runs_main_timer = 1;
1205 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001206 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001207}
1208__setup("apicmaintimer", setup_apicmaintimer);
1209
1210static __init int setup_noapicmaintimer(char *str)
1211{
1212 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001213 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001214}
1215__setup("noapicmaintimer", setup_noapicmaintimer);
1216
Andi Kleen0c3749c2006-02-03 21:51:41 +01001217static __init int setup_apicpmtimer(char *s)
1218{
1219 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001220 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001221 return setup_apicmaintimer(NULL);
1222}
1223__setup("apicpmtimer", setup_apicpmtimer);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225__setup("noapictimer", setup_noapictimer);
1226