blob: 1231365404c4eb809d307c4e7d39b0bdcfb98f56 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010026#include <linux/module.h>
Aaron Durbin39928722006-12-07 02:14:01 +010027#include <linux/ioport.h>
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020028#include <linux/clockchips.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>
john stultz2d0c87c2007-02-16 01:28:18 -080040#include <asm/hpet.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020041#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43int apic_verbosity;
Andi Kleen0c3749c2006-02-03 21:51:41 +010044int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Gleixnerfb79d222007-10-12 23:04:07 +020046int disable_apic_timer __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds2e7c2832007-03-23 11:32:31 -070048/* Local APIC timer works in C2? */
49int local_apic_timer_c2_ok;
50EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
51
Aaron Durbin39928722006-12-07 02:14:01 +010052static struct resource *ioapic_resources;
53static struct resource lapic_resource = {
54 .name = "Local APIC",
55 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
56};
57
Thomas Gleixnerd03030e2007-10-12 23:04:06 +020058static unsigned int calibration_result;
59
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020060static int lapic_next_event(unsigned long delta,
61 struct clock_event_device *evt);
62static void lapic_timer_setup(enum clock_event_mode mode,
63 struct clock_event_device *evt);
64
65static void lapic_timer_broadcast(cpumask_t mask);
66
67static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen);
68
69static struct clock_event_device lapic_clockevent = {
70 .name = "lapic",
71 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
72 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
73 .shift = 32,
74 .set_mode = lapic_timer_setup,
75 .set_next_event = lapic_next_event,
76 .broadcast = lapic_timer_broadcast,
77 .rating = 100,
78 .irq = -1,
79};
80static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
81
82static int lapic_next_event(unsigned long delta,
83 struct clock_event_device *evt)
84{
85 apic_write(APIC_TMICT, delta);
86 return 0;
87}
88
89static void lapic_timer_setup(enum clock_event_mode mode,
90 struct clock_event_device *evt)
91{
92 unsigned long flags;
93 unsigned int v;
94
95 /* Lapic used as dummy for broadcast ? */
96 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
97 return;
98
99 local_irq_save(flags);
100
101 switch (mode) {
102 case CLOCK_EVT_MODE_PERIODIC:
103 case CLOCK_EVT_MODE_ONESHOT:
104 __setup_APIC_LVTT(calibration_result,
105 mode != CLOCK_EVT_MODE_PERIODIC, 1);
106 break;
107 case CLOCK_EVT_MODE_UNUSED:
108 case CLOCK_EVT_MODE_SHUTDOWN:
109 v = apic_read(APIC_LVTT);
110 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
111 apic_write(APIC_LVTT, v);
112 break;
113 case CLOCK_EVT_MODE_RESUME:
114 /* Nothing to do here */
115 break;
116 }
117
118 local_irq_restore(flags);
119}
120
121/*
122 * Local APIC timer broadcast function
123 */
124static void lapic_timer_broadcast(cpumask_t mask)
125{
126#ifdef CONFIG_SMP
127 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
128#endif
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void apic_pm_activate(void);
132
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200133void apic_wait_icr_idle(void)
134{
135 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
136 cpu_relax();
137}
138
139unsigned int safe_apic_wait_icr_idle(void)
140{
141 unsigned int send_status;
142 int timeout;
143
144 timeout = 0;
145 do {
146 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
147 if (!send_status)
148 break;
149 udelay(100);
150 } while (timeout++ < 1000);
151
152 return send_status;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155void enable_NMI_through_LVT0 (void * dummy)
156{
Andi Kleen11a8e772006-01-11 22:46:51 +0100157 unsigned int v;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200158
159 /* unmask and set to NMI */
160 v = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100161 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164int get_maxlvt(void)
165{
Andi Kleen11a8e772006-01-11 22:46:51 +0100166 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 maxlvt = GET_APIC_MAXLVT(v);
170 return maxlvt;
171}
172
Andi Kleen3777a952006-02-03 21:51:53 +0100173/*
174 * 'what should we do if we get a hw irq event on an illegal vector'.
175 * each architecture has to answer this themselves.
176 */
177void ack_bad_irq(unsigned int irq)
178{
179 printk("unexpected IRQ trap at vector %02x\n", irq);
180 /*
181 * Currently unexpected vectors happen only on SMP and APIC.
182 * We _must_ ack these because every local APIC has only N
183 * irq slots per priority level, and a 'hanging, unacked' IRQ
184 * holds up an irq slot - in excessive cases (when multiple
185 * unexpected vectors occur) that might lock up the APIC
186 * completely.
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200187 * But don't ack when the APIC is disabled. -AK
Andi Kleen3777a952006-02-03 21:51:53 +0100188 */
189 if (!disable_apic)
190 ack_APIC_irq();
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193void clear_local_APIC(void)
194{
195 int maxlvt;
196 unsigned int v;
197
198 maxlvt = get_maxlvt();
199
200 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200201 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * if the vector is zero. Mask LVTERR first to prevent this.
203 */
204 if (maxlvt >= 3) {
205 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100206 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 /*
209 * Careful: we have to set masks only first to deassert
210 * any level-triggered sources.
211 */
212 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100213 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100215 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100217 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (maxlvt >= 4) {
219 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100220 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223 /*
224 * Clean APIC state for other OSs:
225 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100226 apic_write(APIC_LVTT, APIC_LVT_MASKED);
227 apic_write(APIC_LVT0, APIC_LVT_MASKED);
228 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100230 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100232 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200233 apic_write(APIC_ESR, 0);
234 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Eric W. Biederman208fb932005-06-25 14:57:45 -0700237void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200239 /* Go back to Virtual Wire compatibility mode */
240 unsigned long value;
241
242 /* For the spurious interrupt use vector F, and enable it */
243 value = apic_read(APIC_SPIV);
244 value &= ~APIC_VECTOR_MASK;
245 value |= APIC_SPIV_APIC_ENABLED;
246 value |= 0xf;
247 apic_write(APIC_SPIV, value);
248
249 if (!virt_wire_setup) {
250 /* For LVT0 make it edge triggered, active high, external and enabled */
251 value = apic_read(APIC_LVT0);
252 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
253 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
254 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
255 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
256 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
257 apic_write(APIC_LVT0, value);
258 } else {
259 /* Disable LVT0 */
260 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700262
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200263 /* For LVT1 make it edge triggered, active high, nmi and enabled */
264 value = apic_read(APIC_LVT1);
265 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700266 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
267 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200268 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
269 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
270 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273void disable_local_APIC(void)
274{
275 unsigned int value;
276
277 clear_local_APIC();
278
279 /*
280 * Disable APIC (implies clearing of registers
281 * for 82489DX!).
282 */
283 value = apic_read(APIC_SPIV);
284 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100285 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
289 * This is to verify that we're looking at a real local APIC.
290 * Check these against your board if the CPUs aren't getting
291 * started for no apparent reason.
292 */
293int __init verify_local_APIC(void)
294{
295 unsigned int reg0, reg1;
296
297 /*
298 * The version register is read-only in a real APIC.
299 */
300 reg0 = apic_read(APIC_LVR);
301 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
302 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
303 reg1 = apic_read(APIC_LVR);
304 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
305
306 /*
307 * The two version reads above should print the same
308 * numbers. If the second one is different, then we
309 * poke at a non-APIC.
310 */
311 if (reg1 != reg0)
312 return 0;
313
314 /*
315 * Check if the version looks reasonably.
316 */
317 reg1 = GET_APIC_VERSION(reg0);
318 if (reg1 == 0x00 || reg1 == 0xff)
319 return 0;
320 reg1 = get_maxlvt();
321 if (reg1 < 0x02 || reg1 == 0xff)
322 return 0;
323
324 /*
325 * The ID register is read/write in a real APIC.
326 */
327 reg0 = apic_read(APIC_ID);
328 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
329 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
330 reg1 = apic_read(APIC_ID);
331 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
332 apic_write(APIC_ID, reg0);
333 if (reg1 != (reg0 ^ APIC_ID_MASK))
334 return 0;
335
336 /*
337 * The next two are just to see if we have sane values.
338 * They're only really relevant if we're in Virtual Wire
339 * compatibility mode, but most boxes are anymore.
340 */
341 reg0 = apic_read(APIC_LVT0);
342 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
343 reg1 = apic_read(APIC_LVT1);
344 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
345
346 return 1;
347}
348
349void __init sync_Arb_IDs(void)
350{
351 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
352 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
353 if (ver >= 0x14) /* P4 or higher */
354 return;
355
356 /*
357 * Wait for idle.
358 */
359 apic_wait_icr_idle();
360
361 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100362 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 | APIC_DM_INIT);
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * An initial setup of the virtual wire mode.
368 */
369void __init init_bsp_APIC(void)
370{
Andi Kleen11a8e772006-01-11 22:46:51 +0100371 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
374 * Don't do the setup now if we have a SMP BIOS as the
375 * through-I/O-APIC virtual wire mode might be active.
376 */
377 if (smp_found_config || !cpu_has_apic)
378 return;
379
380 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
383 * Do not trust the local APIC being empty at bootup.
384 */
385 clear_local_APIC();
386
387 /*
388 * Enable APIC.
389 */
390 value = apic_read(APIC_SPIV);
391 value &= ~APIC_VECTOR_MASK;
392 value |= APIC_SPIV_APIC_ENABLED;
393 value |= APIC_SPIV_FOCUS_DISABLED;
394 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100395 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * Set up the virtual wire mode.
399 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100400 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100402 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Ashok Raje6982c62005-06-25 14:54:58 -0700405void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Andi Kleen11a8e772006-01-11 22:46:51 +0100407 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100408 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Andi Kleenfe7414a2006-09-26 10:52:30 +0200412 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /*
415 * Double-check whether this APIC is really registered.
416 * This is meaningless in clustered apic mode, so we skip it.
417 */
418 if (!apic_id_registered())
419 BUG();
420
421 /*
422 * Intel recommends to set DFR, LDR and TPR before enabling
423 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
424 * document number 292116). So here it goes...
425 */
426 init_apic_ldr();
427
428 /*
429 * Set Task Priority to 'accept all'. We never change this
430 * later on.
431 */
432 value = apic_read(APIC_TASKPRI);
433 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100434 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100437 * After a crash, we no longer service the interrupts and a pending
438 * interrupt from previous kernel might still have ISR bit set.
439 *
440 * Most probably by now CPU has serviced that pending interrupt and
441 * it might not have done the ack_APIC_irq() because it thought,
442 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
443 * does not clear the ISR bit and cpu thinks it has already serivced
444 * the interrupt. Hence a vector might get locked. It was noticed
445 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
446 */
447 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
448 value = apic_read(APIC_ISR + i*0x10);
449 for (j = 31; j >= 0; j--) {
450 if (value & (1<<j))
451 ack_APIC_irq();
452 }
453 }
454
455 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Now that we are all set up, enable the APIC
457 */
458 value = apic_read(APIC_SPIV);
459 value &= ~APIC_VECTOR_MASK;
460 /*
461 * Enable APIC
462 */
463 value |= APIC_SPIV_APIC_ENABLED;
464
Andi Kleen3f14c742006-09-26 10:52:29 +0200465 /* We always use processor focus */
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
468 * Set spurious IRQ vector
469 */
470 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100471 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * Set up LVT0, LVT1:
475 *
476 * set up through-local-APIC on the BP's LINT0. This is not
477 * strictly necessary in pure symmetric-IO mode, but sometimes
478 * we delegate interrupts to the 8259A.
479 */
480 /*
481 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
482 */
483 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200484 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 value = APIC_DM_EXTINT;
486 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
487 } else {
488 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
489 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
490 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100491 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * only the BP should see the LINT1 NMI signal, obviously.
495 */
496 if (!smp_processor_id())
497 value = APIC_DM_NMI;
498 else
499 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100500 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Andi Kleen61c11342005-09-12 18:49:23 +0200502 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 unsigned oldvalue;
504 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 oldvalue = apic_read(APIC_ESR);
506 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100507 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /*
509 * spec says clear errors after enabling vector.
510 */
511 if (maxlvt > 3)
512 apic_write(APIC_ESR, 0);
513 value = apic_read(APIC_ESR);
514 if (value != oldvalue)
515 apic_printk(APIC_VERBOSE,
516 "ESR value after enabling vector: %08x, after %08x\n",
517 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200521 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 apic_pm_activate();
523}
524
525#ifdef CONFIG_PM
526
527static struct {
528 /* 'active' is true if the local APIC was enabled by us and
529 not the BIOS; this signifies that we are also responsible
530 for disabling it before entering apm/acpi suspend */
531 int active;
532 /* r/w apic fields */
533 unsigned int apic_id;
534 unsigned int apic_taskpri;
535 unsigned int apic_ldr;
536 unsigned int apic_dfr;
537 unsigned int apic_spiv;
538 unsigned int apic_lvtt;
539 unsigned int apic_lvtpc;
540 unsigned int apic_lvt0;
541 unsigned int apic_lvt1;
542 unsigned int apic_lvterr;
543 unsigned int apic_tmict;
544 unsigned int apic_tdcr;
545 unsigned int apic_thmr;
546} apic_pm_state;
547
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700548static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100551 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (!apic_pm_state.active)
554 return 0;
555
Karsten Wiesef990fff2006-12-07 02:14:11 +0100556 maxlvt = get_maxlvt();
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 apic_pm_state.apic_id = apic_read(APIC_ID);
559 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
560 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
561 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
562 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
563 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100564 if (maxlvt >= 4)
565 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
567 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
568 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
569 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
570 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100571#ifdef CONFIG_X86_MCE_INTEL
572 if (maxlvt >= 5)
573 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
574#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200575 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 disable_local_APIC();
577 local_irq_restore(flags);
578 return 0;
579}
580
581static int lapic_resume(struct sys_device *dev)
582{
583 unsigned int l, h;
584 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100585 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (!apic_pm_state.active)
588 return 0;
589
Karsten Wiesef990fff2006-12-07 02:14:11 +0100590 maxlvt = get_maxlvt();
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 local_irq_save(flags);
593 rdmsr(MSR_IA32_APICBASE, l, h);
594 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100595 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 wrmsr(MSR_IA32_APICBASE, l, h);
597 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
598 apic_write(APIC_ID, apic_pm_state.apic_id);
599 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
600 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
601 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
602 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
603 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
604 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100605#ifdef CONFIG_X86_MCE_INTEL
606 if (maxlvt >= 5)
607 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
608#endif
609 if (maxlvt >= 4)
610 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
612 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
613 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
614 apic_write(APIC_ESR, 0);
615 apic_read(APIC_ESR);
616 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
617 apic_write(APIC_ESR, 0);
618 apic_read(APIC_ESR);
619 local_irq_restore(flags);
620 return 0;
621}
622
623static struct sysdev_class lapic_sysclass = {
624 set_kset_name("lapic"),
625 .resume = lapic_resume,
626 .suspend = lapic_suspend,
627};
628
629static struct sys_device device_lapic = {
630 .id = 0,
631 .cls = &lapic_sysclass,
632};
633
Ashok Raje6982c62005-06-25 14:54:58 -0700634static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 apic_pm_state.active = 1;
637}
638
639static int __init init_lapic_sysfs(void)
640{
641 int error;
642 if (!cpu_has_apic)
643 return 0;
644 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
645 error = sysdev_class_register(&lapic_sysclass);
646 if (!error)
647 error = sysdev_register(&device_lapic);
648 return error;
649}
650device_initcall(init_lapic_sysfs);
651
652#else /* CONFIG_PM */
653
654static void apic_pm_activate(void) { }
655
656#endif /* CONFIG_PM */
657
658static int __init apic_set_verbosity(char *str)
659{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200660 if (str == NULL) {
661 skip_ioapic_setup = 0;
662 ioapic_force = 1;
663 return 0;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (strcmp("debug", str) == 0)
666 apic_verbosity = APIC_DEBUG;
667 else if (strcmp("verbose", str) == 0)
668 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200669 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200671 " use apic=verbose or apic=debug\n", str);
672 return -EINVAL;
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200677early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/*
680 * Detect and enable local APICs on non-SMP boards.
681 * Original code written by Keir Fraser.
682 * On AMD64 we trust the BIOS - if it says no APIC it is likely
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200683 * not correctly set up (usually the APIC timer won't work etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
685
686static int __init detect_init_APIC (void)
687{
688 if (!cpu_has_apic) {
689 printk(KERN_INFO "No local APIC present\n");
690 return -1;
691 }
692
693 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
694 boot_cpu_id = 0;
695 return 0;
696}
697
Aaron Durbin39928722006-12-07 02:14:01 +0100698#ifdef CONFIG_X86_IO_APIC
699static struct resource * __init ioapic_setup_resources(void)
700{
701#define IOAPIC_RESOURCE_NAME_SIZE 11
702 unsigned long n;
703 struct resource *res;
704 char *mem;
705 int i;
706
707 if (nr_ioapics <= 0)
708 return NULL;
709
710 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
711 n *= nr_ioapics;
712
713 mem = alloc_bootmem(n);
714 res = (void *)mem;
715
716 if (mem != NULL) {
717 memset(mem, 0, n);
718 mem += sizeof(struct resource) * nr_ioapics;
719
720 for (i = 0; i < nr_ioapics; i++) {
721 res[i].name = mem;
722 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
723 sprintf(mem, "IOAPIC %u", i);
724 mem += IOAPIC_RESOURCE_NAME_SIZE;
725 }
726 }
727
728 ioapic_resources = res;
729
730 return res;
731}
732
733static int __init ioapic_insert_resources(void)
734{
735 int i;
736 struct resource *r = ioapic_resources;
737
738 if (!r) {
739 printk("IO APIC resources could be not be allocated.\n");
740 return -1;
741 }
742
743 for (i = 0; i < nr_ioapics; i++) {
744 insert_resource(&iomem_resource, r);
745 r++;
746 }
747
748 return 0;
749}
750
751/* Insert the IO APIC resources after PCI initialization has occured to handle
752 * IO APICS that are mapped in on a BAR in PCI space. */
753late_initcall(ioapic_insert_resources);
754#endif
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756void __init init_apic_mappings(void)
757{
758 unsigned long apic_phys;
759
760 /*
761 * If no local APIC can be found then set up a fake all
762 * zeroes page to simulate the local APIC and another
763 * one for the IO-APIC.
764 */
765 if (!smp_found_config && detect_init_APIC()) {
766 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
767 apic_phys = __pa(apic_phys);
768 } else
769 apic_phys = mp_lapic_addr;
770
771 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu7ffeeb12007-10-12 23:04:06 +0200772 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
773 APIC_BASE, apic_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Aaron Durbin39928722006-12-07 02:14:01 +0100775 /* Put local APIC into the resource map. */
776 lapic_resource.start = apic_phys;
777 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
778 insert_resource(&iomem_resource, &lapic_resource);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Fetch the APIC ID of the BSP in case we have a
782 * default configuration (or the MP table is broken).
783 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200784 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 {
787 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
788 int i;
Aaron Durbin39928722006-12-07 02:14:01 +0100789 struct resource *ioapic_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Aaron Durbin39928722006-12-07 02:14:01 +0100791 ioapic_res = ioapic_setup_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 for (i = 0; i < nr_ioapics; i++) {
793 if (smp_found_config) {
794 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
795 } else {
796 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
797 ioapic_phys = __pa(ioapic_phys);
798 }
799 set_fixmap_nocache(idx, ioapic_phys);
800 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
801 __fix_to_virt(idx), ioapic_phys);
802 idx++;
Aaron Durbin39928722006-12-07 02:14:01 +0100803
804 if (ioapic_res != NULL) {
805 ioapic_res->start = ioapic_phys;
806 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
807 ioapic_res++;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * This function sets up the local APIC timer, with a timeout of
815 * 'clocks' APIC bus clock. During calibration we actually call
816 * this function twice on the boot CPU, once with a bogus timeout
817 * value, second time for real. The other (noncalibrating) CPUs
818 * call this function only once, with the real, calibrated value.
819 *
820 * We do reads before writes even if unnecessary, to get around the
821 * P5 APIC double write bug.
822 */
823
Thomas Gleixner80174092007-10-12 23:04:06 +0200824static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
David Rientjes86bd58b2006-12-07 02:14:11 +0100826 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Thomas Gleixner80174092007-10-12 23:04:06 +0200828 lvtt_value = LOCAL_TIMER_VECTOR;
829 if (!oneshot)
830 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
831 if (!irqen)
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100832 lvtt_value |= APIC_LVT_MASKED;
833
Andi Kleen11a8e772006-01-11 22:46:51 +0100834 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /*
837 * Divide PICLK by 16
838 */
839 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100840 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
842 | APIC_TDR_DIV_16);
843
Thomas Gleixner80174092007-10-12 23:04:06 +0200844 if (!oneshot)
Thomas Gleixnerb58eb002007-10-12 23:04:06 +0200845 apic_write(APIC_TMICT, clocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200848static void setup_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200850 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200852 memcpy(levt, &lapic_clockevent, sizeof(*levt));
853 levt->cpumask = cpumask_of_cpu(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200855 clockevents_register_device(levt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858/*
859 * In this function we calibrate APIC bus clocks to the external
860 * timer. Unfortunately we cannot use jiffies and the timer irq
861 * to calibrate, since some later bootup code depends on getting
862 * the first irq? Ugh.
863 *
864 * We want to do the calibration only once since we
865 * want to have local timer irqs syncron. CPUs connected
866 * by the same APIC bus have the very same bus frequency.
867 * And we want to have irqs off anyways, no accidental
868 * APIC irq that way.
869 */
870
871#define TICK_COUNT 100000000
872
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200873static void __init calibrate_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
David P. Reed4637a742007-05-02 19:27:20 +0200875 unsigned apic, apic_start;
876 unsigned long tsc, tsc_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int result;
Thomas Gleixnerc4d58cb2007-10-12 23:04:07 +0200878
879 local_irq_disable();
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * Put whatever arbitrary (but long enough) timeout
883 * value into the APIC clock, we just want to get the
884 * counter running for calibration.
Thomas Gleixner80174092007-10-12 23:04:06 +0200885 *
886 * No interrupt enable !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Thomas Gleixnerb58eb002007-10-12 23:04:06 +0200888 __setup_APIC_LVTT(250000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100891#ifdef CONFIG_X86_PM_TIMER
892 if (apic_calibrate_pmtmr && pmtmr_ioport) {
893 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100895 result = (apic_start - apic) * 1000L / 5;
896 } else
897#endif
898 {
David P. Reed4637a742007-05-02 19:27:20 +0200899 rdtscll(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Andi Kleen0c3749c2006-02-03 21:51:41 +0100901 do {
902 apic = apic_read(APIC_TMCCT);
David P. Reed4637a742007-05-02 19:27:20 +0200903 rdtscll(tsc);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100904 } while ((tsc - tsc_start) < TICK_COUNT &&
David P. Reed4637a742007-05-02 19:27:20 +0200905 (apic_start - apic) < TICK_COUNT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100906
Joerg Roedel6b37f5a2007-05-02 19:27:06 +0200907 result = (apic_start - apic) * 1000L * tsc_khz /
Andi Kleen0c3749c2006-02-03 21:51:41 +0100908 (tsc - tsc_start);
909 }
Thomas Gleixnerc4d58cb2007-10-12 23:04:07 +0200910
911 local_irq_enable();
912
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200913 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
916 result / 1000 / 1000, result / 1000 % 1000);
917
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200918 /* Calculate the scaled math multiplication factor */
919 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC, 32);
920 lapic_clockevent.max_delta_ns =
921 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
922 lapic_clockevent.min_delta_ns =
923 clockevent_delta2ns(0xF, &lapic_clockevent);
924
Thomas Gleixnerb58eb002007-10-12 23:04:06 +0200925 calibration_result = result / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928void __init setup_boot_APIC_clock (void)
929{
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200930 /*
931 * The local apic timer can be disabled via the kernel commandline.
932 * Register the lapic timer as a dummy clock event source on SMP
933 * systems, so the broadcast mechanism is used. On UP systems simply
934 * ignore it.
935 */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200936 if (disable_apic_timer) {
937 printk(KERN_INFO "Disabling APIC timer\n");
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200938 /* No broadcast on UP ! */
939 if (num_possible_cpus() > 1)
940 setup_APIC_timer();
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200941 return;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 printk(KERN_INFO "Using local APIC timer interrupts.\n");
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200945 calibrate_APIC_clock();
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200948 * If nmi_watchdog is set to IO_APIC, we need the
949 * PIT/HPET going. Otherwise register lapic as a dummy
950 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200952 if (nmi_watchdog != NMI_IO_APIC)
953 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
954 else
955 printk(KERN_WARNING "APIC timer registered as dummy,"
956 " due to nmi_watchdog=1!\n");
957
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200958 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Ashok Raje6982c62005-06-25 14:54:58 -0700961void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200963 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966int setup_profiling_timer(unsigned int multiplier)
967{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Thomas Gleixnerf40f31b2007-07-21 17:10:14 +0200971void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
972 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100973{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200974 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
975 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100976 apic_write(reg, v);
977}
Jacob Shin89b831e2005-11-05 17:25:53 +0100978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * Local timer interrupt handler. It does both profiling and
981 * process statistics/rescheduling.
982 *
983 * We do profiling in every local tick, statistics/rescheduling
984 * happen only every 'profiling multiplier' ticks. The default
985 * multiplier is 1 and it can be changed by writing the new multiplier
986 * value into /proc/profile.
987 */
988
David Howells7d12e782006-10-05 14:55:46 +0100989void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200991 int cpu = smp_processor_id();
992 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +0200995 * Normally we should not be here till LAPIC has been initialized but
996 * in some cases like kdump, its possible that there is a pending LAPIC
997 * timer interrupt from previous kernel's context and is delivered in
998 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 *
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001000 * Interrupts are enabled early and LAPIC is setup much later, hence
1001 * its possible that when we get here evt->event_handler is NULL.
1002 * Check for event_handler being NULL and discard the interrupt as
1003 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001005 if (!evt->event_handler) {
1006 printk(KERN_WARNING
1007 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
1008 /* Switch it off */
1009 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
1010 return;
1011 }
1012
1013 /*
1014 * the NMI deadlock-detector uses this.
1015 */
1016 add_pda(apic_timer_irqs, 1);
1017
1018 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
1021/*
1022 * Local APIC timer interrupt. This is the most natural way for doing
1023 * local interrupts, but local timer interrupts can be emulated by
1024 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1025 *
1026 * [ if a single-CPU system runs an SMP kernel then we call the local
1027 * interrupt as well. Thus we cannot inline the local irq ... ]
1028 */
Andrew Mortond150ad72006-10-06 13:28:09 -07001029void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Andrew Mortond150ad72006-10-06 13:28:09 -07001031 struct pt_regs *old_regs = set_irq_regs(regs);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * NOTE! We'd better ACK the irq immediately,
1035 * because timer handling can be slow.
1036 */
1037 ack_APIC_irq();
1038 /*
1039 * update_process_times() expects us to have done irq_enter().
1040 * Besides, if we don't timer interrupts ignore the global
1041 * interrupt lock, which is the WrongThing (tm) to do.
1042 */
Andi Kleen95833c82006-01-11 22:44:36 +01001043 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +01001045 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 irq_exit();
Andrew Mortond150ad72006-10-06 13:28:09 -07001047 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001051 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 *
1053 * Thus far, the major user of this is IBM's Summit2 series:
1054 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001055 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * multi-chassis. Use available data to take a good guess.
1057 * If in doubt, go HPET.
1058 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001059__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int i, clusters, zeros;
1062 unsigned id;
1063 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1064
Suresh Siddha376ec332005-05-16 21:53:32 -07001065 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 for (i = 0; i < NR_CPUS; i++) {
1068 id = bios_cpu_apicid[i];
1069 if (id != BAD_APICID)
1070 __set_bit(APIC_CLUSTERID(id), clustermap);
1071 }
1072
1073 /* Problem: Partially populated chassis may not have CPUs in some of
1074 * the APIC clusters they have been allocated. Only present CPUs have
1075 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1076 * clusters are allocated sequentially, count zeros only if they are
1077 * bounded by ones.
1078 */
1079 clusters = 0;
1080 zeros = 0;
1081 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1082 if (test_bit(i, clustermap)) {
1083 clusters += 1 + zeros;
1084 zeros = 0;
1085 } else
1086 ++zeros;
1087 }
1088
1089 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001090 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * May have to revisit this when multi-core + hyperthreaded CPUs come
1092 * out, but AFAIK this will work even for them.
1093 */
1094 return (clusters > 2);
1095}
1096
1097/*
1098 * This interrupt should _never_ happen with our APIC/SMP architecture
1099 */
1100asmlinkage void smp_spurious_interrupt(void)
1101{
1102 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001103 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 irq_enter();
1105 /*
1106 * Check if this really is a spurious interrupt and ACK it
1107 * if it is a vectored one. Just in case...
1108 * Spurious interrupts should not be ACKed.
1109 */
1110 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1111 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1112 ack_APIC_irq();
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 irq_exit();
1115}
1116
1117/*
1118 * This interrupt should never happen with our APIC/SMP architecture
1119 */
1120
1121asmlinkage void smp_error_interrupt(void)
1122{
1123 unsigned int v, v1;
1124
Andi Kleen95833c82006-01-11 22:44:36 +01001125 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 irq_enter();
1127 /* First tickle the hardware, only then report what went on. -- REW */
1128 v = apic_read(APIC_ESR);
1129 apic_write(APIC_ESR, 0);
1130 v1 = apic_read(APIC_ESR);
1131 ack_APIC_irq();
1132 atomic_inc(&irq_err_count);
1133
1134 /* Here is what the APIC error bits mean:
1135 0: Send CS error
1136 1: Receive CS error
1137 2: Send accept error
1138 3: Receive accept error
1139 4: Reserved
1140 5: Send illegal vector
1141 6: Received illegal vector
1142 7: Illegal register address
1143 */
1144 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001145 smp_processor_id(), v , v1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 irq_exit();
1147}
1148
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001149int disable_apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151/*
1152 * This initializes the IO-APIC and APIC hardware if this is
1153 * a UP kernel.
1154 */
1155int __init APIC_init_uniprocessor (void)
1156{
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001157 if (disable_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 printk(KERN_INFO "Apic disabled\n");
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001159 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001161 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 disable_apic = 1;
1163 printk(KERN_INFO "Apic disabled by BIOS\n");
1164 return -1;
1165 }
1166
1167 verify_local_APIC();
1168
Andi Kleen357e11d2005-09-12 18:49:24 +02001169 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001170 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 setup_local_APIC();
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001175 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 else
1177 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001179 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 0;
1181}
1182
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001183static __init int setup_disableapic(char *str)
1184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001186 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1187 return 0;
1188}
1189early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001191/* same as disableapic, for compatibility */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001192static __init int setup_nolapic(char *str)
1193{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001194 return setup_disableapic(str);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001195}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001196early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001198static int __init parse_lapic_timer_c2_ok(char *arg)
1199{
1200 local_apic_timer_c2_ok = 1;
1201 return 0;
1202}
1203early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1204
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001205static __init int setup_noapictimer(char *str)
1206{
Andi Kleen73dea472006-02-03 21:50:50 +01001207 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001210 return 1;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001211}
Thomas Gleixner9f75e9b2007-10-12 23:04:23 +02001212__setup("noapictimer", setup_noapictimer);
Andi Kleen73dea472006-02-03 21:50:50 +01001213
Andi Kleen0c3749c2006-02-03 21:51:41 +01001214static __init int setup_apicpmtimer(char *s)
1215{
1216 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001217 notsc_setup(NULL);
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001218 return 0;
Andi Kleen0c3749c2006-02-03 21:51:41 +01001219}
1220__setup("apicpmtimer", setup_apicpmtimer);
1221