blob: 40938ef99dc2dc9956d5bd42827eaecae3edeea7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/atomic.h>
30#include <asm/smp.h>
31#include <asm/mtrr.h>
32#include <asm/mpspec.h>
33#include <asm/pgalloc.h>
34#include <asm/mach_apic.h>
Andi Kleen75152112005-05-16 21:53:34 -070035#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010036#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010037#include <asm/proto.h>
38#include <asm/timex.h>
john stultz2d0c87c2007-02-16 01:28:18 -080039#include <asm/hpet.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020040#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010043int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010044int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46int disable_apic_timer __initdata;
47
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
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010060/*
61 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
62 * IPIs in place of local APIC timers
63 */
64static cpumask_t timer_interrupt_broadcast_ipi_mask;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070067int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void apic_pm_activate(void);
70
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +020071void apic_wait_icr_idle(void)
72{
73 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
74 cpu_relax();
75}
76
77unsigned int safe_apic_wait_icr_idle(void)
78{
79 unsigned int send_status;
80 int timeout;
81
82 timeout = 0;
83 do {
84 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
85 if (!send_status)
86 break;
87 udelay(100);
88 } while (timeout++ < 1000);
89
90 return send_status;
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void enable_NMI_through_LVT0 (void * dummy)
94{
Andi Kleen11a8e772006-01-11 22:46:51 +010095 unsigned int v;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +020096
97 /* unmask and set to NMI */
98 v = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +010099 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102int get_maxlvt(void)
103{
Andi Kleen11a8e772006-01-11 22:46:51 +0100104 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 maxlvt = GET_APIC_MAXLVT(v);
108 return maxlvt;
109}
110
Andi Kleen3777a952006-02-03 21:51:53 +0100111/*
112 * 'what should we do if we get a hw irq event on an illegal vector'.
113 * each architecture has to answer this themselves.
114 */
115void ack_bad_irq(unsigned int irq)
116{
117 printk("unexpected IRQ trap at vector %02x\n", irq);
118 /*
119 * Currently unexpected vectors happen only on SMP and APIC.
120 * We _must_ ack these because every local APIC has only N
121 * irq slots per priority level, and a 'hanging, unacked' IRQ
122 * holds up an irq slot - in excessive cases (when multiple
123 * unexpected vectors occur) that might lock up the APIC
124 * completely.
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200125 * But don't ack when the APIC is disabled. -AK
Andi Kleen3777a952006-02-03 21:51:53 +0100126 */
127 if (!disable_apic)
128 ack_APIC_irq();
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131void clear_local_APIC(void)
132{
133 int maxlvt;
134 unsigned int v;
135
136 maxlvt = get_maxlvt();
137
138 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200139 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * if the vector is zero. Mask LVTERR first to prevent this.
141 */
142 if (maxlvt >= 3) {
143 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100144 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 /*
147 * Careful: we have to set masks only first to deassert
148 * any level-triggered sources.
149 */
150 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100151 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100153 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100155 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (maxlvt >= 4) {
157 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100158 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 /*
162 * Clean APIC state for other OSs:
163 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100164 apic_write(APIC_LVTT, APIC_LVT_MASKED);
165 apic_write(APIC_LVT0, APIC_LVT_MASKED);
166 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100168 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100170 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200171 apic_write(APIC_ESR, 0);
172 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Eric W. Biederman208fb932005-06-25 14:57:45 -0700175void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200177 /* Go back to Virtual Wire compatibility mode */
178 unsigned long value;
179
180 /* For the spurious interrupt use vector F, and enable it */
181 value = apic_read(APIC_SPIV);
182 value &= ~APIC_VECTOR_MASK;
183 value |= APIC_SPIV_APIC_ENABLED;
184 value |= 0xf;
185 apic_write(APIC_SPIV, value);
186
187 if (!virt_wire_setup) {
188 /* For LVT0 make it edge triggered, active high, external and enabled */
189 value = apic_read(APIC_LVT0);
190 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
191 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
192 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
193 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
194 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
195 apic_write(APIC_LVT0, value);
196 } else {
197 /* Disable LVT0 */
198 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700200
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200201 /* For LVT1 make it edge triggered, active high, nmi and enabled */
202 value = apic_read(APIC_LVT1);
203 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700204 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
205 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200206 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
207 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
208 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211void disable_local_APIC(void)
212{
213 unsigned int value;
214
215 clear_local_APIC();
216
217 /*
218 * Disable APIC (implies clearing of registers
219 * for 82489DX!).
220 */
221 value = apic_read(APIC_SPIV);
222 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100223 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/*
227 * This is to verify that we're looking at a real local APIC.
228 * Check these against your board if the CPUs aren't getting
229 * started for no apparent reason.
230 */
231int __init verify_local_APIC(void)
232{
233 unsigned int reg0, reg1;
234
235 /*
236 * The version register is read-only in a real APIC.
237 */
238 reg0 = apic_read(APIC_LVR);
239 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
240 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
241 reg1 = apic_read(APIC_LVR);
242 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
243
244 /*
245 * The two version reads above should print the same
246 * numbers. If the second one is different, then we
247 * poke at a non-APIC.
248 */
249 if (reg1 != reg0)
250 return 0;
251
252 /*
253 * Check if the version looks reasonably.
254 */
255 reg1 = GET_APIC_VERSION(reg0);
256 if (reg1 == 0x00 || reg1 == 0xff)
257 return 0;
258 reg1 = get_maxlvt();
259 if (reg1 < 0x02 || reg1 == 0xff)
260 return 0;
261
262 /*
263 * The ID register is read/write in a real APIC.
264 */
265 reg0 = apic_read(APIC_ID);
266 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
267 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
268 reg1 = apic_read(APIC_ID);
269 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
270 apic_write(APIC_ID, reg0);
271 if (reg1 != (reg0 ^ APIC_ID_MASK))
272 return 0;
273
274 /*
275 * The next two are just to see if we have sane values.
276 * They're only really relevant if we're in Virtual Wire
277 * compatibility mode, but most boxes are anymore.
278 */
279 reg0 = apic_read(APIC_LVT0);
280 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
281 reg1 = apic_read(APIC_LVT1);
282 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
283
284 return 1;
285}
286
287void __init sync_Arb_IDs(void)
288{
289 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
290 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
291 if (ver >= 0x14) /* P4 or higher */
292 return;
293
294 /*
295 * Wait for idle.
296 */
297 apic_wait_icr_idle();
298
299 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100300 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 | APIC_DM_INIT);
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*
305 * An initial setup of the virtual wire mode.
306 */
307void __init init_bsp_APIC(void)
308{
Andi Kleen11a8e772006-01-11 22:46:51 +0100309 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /*
312 * Don't do the setup now if we have a SMP BIOS as the
313 * through-I/O-APIC virtual wire mode might be active.
314 */
315 if (smp_found_config || !cpu_has_apic)
316 return;
317
318 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /*
321 * Do not trust the local APIC being empty at bootup.
322 */
323 clear_local_APIC();
324
325 /*
326 * Enable APIC.
327 */
328 value = apic_read(APIC_SPIV);
329 value &= ~APIC_VECTOR_MASK;
330 value |= APIC_SPIV_APIC_ENABLED;
331 value |= APIC_SPIV_FOCUS_DISABLED;
332 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100333 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /*
336 * Set up the virtual wire mode.
337 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100338 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100340 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Ashok Raje6982c62005-06-25 14:54:58 -0700343void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Andi Kleen11a8e772006-01-11 22:46:51 +0100345 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100346 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Andi Kleenfe7414a2006-09-26 10:52:30 +0200350 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /*
353 * Double-check whether this APIC is really registered.
354 * This is meaningless in clustered apic mode, so we skip it.
355 */
356 if (!apic_id_registered())
357 BUG();
358
359 /*
360 * Intel recommends to set DFR, LDR and TPR before enabling
361 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
362 * document number 292116). So here it goes...
363 */
364 init_apic_ldr();
365
366 /*
367 * Set Task Priority to 'accept all'. We never change this
368 * later on.
369 */
370 value = apic_read(APIC_TASKPRI);
371 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100372 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100375 * After a crash, we no longer service the interrupts and a pending
376 * interrupt from previous kernel might still have ISR bit set.
377 *
378 * Most probably by now CPU has serviced that pending interrupt and
379 * it might not have done the ack_APIC_irq() because it thought,
380 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
381 * does not clear the ISR bit and cpu thinks it has already serivced
382 * the interrupt. Hence a vector might get locked. It was noticed
383 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
384 */
385 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
386 value = apic_read(APIC_ISR + i*0x10);
387 for (j = 31; j >= 0; j--) {
388 if (value & (1<<j))
389 ack_APIC_irq();
390 }
391 }
392
393 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * Now that we are all set up, enable the APIC
395 */
396 value = apic_read(APIC_SPIV);
397 value &= ~APIC_VECTOR_MASK;
398 /*
399 * Enable APIC
400 */
401 value |= APIC_SPIV_APIC_ENABLED;
402
Andi Kleen3f14c742006-09-26 10:52:29 +0200403 /* We always use processor focus */
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Set spurious IRQ vector
407 */
408 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100409 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /*
412 * Set up LVT0, LVT1:
413 *
414 * set up through-local-APIC on the BP's LINT0. This is not
415 * strictly necessary in pure symmetric-IO mode, but sometimes
416 * we delegate interrupts to the 8259A.
417 */
418 /*
419 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
420 */
421 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200422 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 value = APIC_DM_EXTINT;
424 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
425 } else {
426 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
427 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
428 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100429 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * only the BP should see the LINT1 NMI signal, obviously.
433 */
434 if (!smp_processor_id())
435 value = APIC_DM_NMI;
436 else
437 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100438 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Andi Kleen61c11342005-09-12 18:49:23 +0200440 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned oldvalue;
442 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 oldvalue = apic_read(APIC_ESR);
444 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100445 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * spec says clear errors after enabling vector.
448 */
449 if (maxlvt > 3)
450 apic_write(APIC_ESR, 0);
451 value = apic_read(APIC_ESR);
452 if (value != oldvalue)
453 apic_printk(APIC_VERBOSE,
454 "ESR value after enabling vector: %08x, after %08x\n",
455 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200459 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 apic_pm_activate();
461}
462
463#ifdef CONFIG_PM
464
465static struct {
466 /* 'active' is true if the local APIC was enabled by us and
467 not the BIOS; this signifies that we are also responsible
468 for disabling it before entering apm/acpi suspend */
469 int active;
470 /* r/w apic fields */
471 unsigned int apic_id;
472 unsigned int apic_taskpri;
473 unsigned int apic_ldr;
474 unsigned int apic_dfr;
475 unsigned int apic_spiv;
476 unsigned int apic_lvtt;
477 unsigned int apic_lvtpc;
478 unsigned int apic_lvt0;
479 unsigned int apic_lvt1;
480 unsigned int apic_lvterr;
481 unsigned int apic_tmict;
482 unsigned int apic_tdcr;
483 unsigned int apic_thmr;
484} apic_pm_state;
485
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700486static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100489 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (!apic_pm_state.active)
492 return 0;
493
Karsten Wiesef990fff2006-12-07 02:14:11 +0100494 maxlvt = get_maxlvt();
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 apic_pm_state.apic_id = apic_read(APIC_ID);
497 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
498 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
499 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
500 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
501 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100502 if (maxlvt >= 4)
503 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
505 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
506 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
507 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
508 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100509#ifdef CONFIG_X86_MCE_INTEL
510 if (maxlvt >= 5)
511 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
512#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200513 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 disable_local_APIC();
515 local_irq_restore(flags);
516 return 0;
517}
518
519static int lapic_resume(struct sys_device *dev)
520{
521 unsigned int l, h;
522 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100523 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if (!apic_pm_state.active)
526 return 0;
527
Karsten Wiesef990fff2006-12-07 02:14:11 +0100528 maxlvt = get_maxlvt();
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 local_irq_save(flags);
531 rdmsr(MSR_IA32_APICBASE, l, h);
532 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100533 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 wrmsr(MSR_IA32_APICBASE, l, h);
535 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
536 apic_write(APIC_ID, apic_pm_state.apic_id);
537 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
538 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
539 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
540 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
541 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
542 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100543#ifdef CONFIG_X86_MCE_INTEL
544 if (maxlvt >= 5)
545 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
546#endif
547 if (maxlvt >= 4)
548 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
550 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
551 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
552 apic_write(APIC_ESR, 0);
553 apic_read(APIC_ESR);
554 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
555 apic_write(APIC_ESR, 0);
556 apic_read(APIC_ESR);
557 local_irq_restore(flags);
558 return 0;
559}
560
561static struct sysdev_class lapic_sysclass = {
562 set_kset_name("lapic"),
563 .resume = lapic_resume,
564 .suspend = lapic_suspend,
565};
566
567static struct sys_device device_lapic = {
568 .id = 0,
569 .cls = &lapic_sysclass,
570};
571
Ashok Raje6982c62005-06-25 14:54:58 -0700572static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 apic_pm_state.active = 1;
575}
576
577static int __init init_lapic_sysfs(void)
578{
579 int error;
580 if (!cpu_has_apic)
581 return 0;
582 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
583 error = sysdev_class_register(&lapic_sysclass);
584 if (!error)
585 error = sysdev_register(&device_lapic);
586 return error;
587}
588device_initcall(init_lapic_sysfs);
589
590#else /* CONFIG_PM */
591
592static void apic_pm_activate(void) { }
593
594#endif /* CONFIG_PM */
595
596static int __init apic_set_verbosity(char *str)
597{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200598 if (str == NULL) {
599 skip_ioapic_setup = 0;
600 ioapic_force = 1;
601 return 0;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (strcmp("debug", str) == 0)
604 apic_verbosity = APIC_DEBUG;
605 else if (strcmp("verbose", str) == 0)
606 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200607 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200609 " use apic=verbose or apic=debug\n", str);
610 return -EINVAL;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200615early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/*
618 * Detect and enable local APICs on non-SMP boards.
619 * Original code written by Keir Fraser.
620 * On AMD64 we trust the BIOS - if it says no APIC it is likely
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200621 * not correctly set up (usually the APIC timer won't work etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623
624static int __init detect_init_APIC (void)
625{
626 if (!cpu_has_apic) {
627 printk(KERN_INFO "No local APIC present\n");
628 return -1;
629 }
630
631 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
632 boot_cpu_id = 0;
633 return 0;
634}
635
Aaron Durbin39928722006-12-07 02:14:01 +0100636#ifdef CONFIG_X86_IO_APIC
637static struct resource * __init ioapic_setup_resources(void)
638{
639#define IOAPIC_RESOURCE_NAME_SIZE 11
640 unsigned long n;
641 struct resource *res;
642 char *mem;
643 int i;
644
645 if (nr_ioapics <= 0)
646 return NULL;
647
648 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
649 n *= nr_ioapics;
650
651 mem = alloc_bootmem(n);
652 res = (void *)mem;
653
654 if (mem != NULL) {
655 memset(mem, 0, n);
656 mem += sizeof(struct resource) * nr_ioapics;
657
658 for (i = 0; i < nr_ioapics; i++) {
659 res[i].name = mem;
660 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
661 sprintf(mem, "IOAPIC %u", i);
662 mem += IOAPIC_RESOURCE_NAME_SIZE;
663 }
664 }
665
666 ioapic_resources = res;
667
668 return res;
669}
670
671static int __init ioapic_insert_resources(void)
672{
673 int i;
674 struct resource *r = ioapic_resources;
675
676 if (!r) {
677 printk("IO APIC resources could be not be allocated.\n");
678 return -1;
679 }
680
681 for (i = 0; i < nr_ioapics; i++) {
682 insert_resource(&iomem_resource, r);
683 r++;
684 }
685
686 return 0;
687}
688
689/* Insert the IO APIC resources after PCI initialization has occured to handle
690 * IO APICS that are mapped in on a BAR in PCI space. */
691late_initcall(ioapic_insert_resources);
692#endif
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694void __init init_apic_mappings(void)
695{
696 unsigned long apic_phys;
697
698 /*
699 * If no local APIC can be found then set up a fake all
700 * zeroes page to simulate the local APIC and another
701 * one for the IO-APIC.
702 */
703 if (!smp_found_config && detect_init_APIC()) {
704 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
705 apic_phys = __pa(apic_phys);
706 } else
707 apic_phys = mp_lapic_addr;
708
709 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu7ffeeb12007-10-12 23:04:06 +0200710 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
711 APIC_BASE, apic_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Aaron Durbin39928722006-12-07 02:14:01 +0100713 /* Put local APIC into the resource map. */
714 lapic_resource.start = apic_phys;
715 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
716 insert_resource(&iomem_resource, &lapic_resource);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /*
719 * Fetch the APIC ID of the BSP in case we have a
720 * default configuration (or the MP table is broken).
721 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200722 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 {
725 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
726 int i;
Aaron Durbin39928722006-12-07 02:14:01 +0100727 struct resource *ioapic_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Aaron Durbin39928722006-12-07 02:14:01 +0100729 ioapic_res = ioapic_setup_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 for (i = 0; i < nr_ioapics; i++) {
731 if (smp_found_config) {
732 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
733 } else {
734 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
735 ioapic_phys = __pa(ioapic_phys);
736 }
737 set_fixmap_nocache(idx, ioapic_phys);
738 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
739 __fix_to_virt(idx), ioapic_phys);
740 idx++;
Aaron Durbin39928722006-12-07 02:14:01 +0100741
742 if (ioapic_res != NULL) {
743 ioapic_res->start = ioapic_phys;
744 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
745 ioapic_res++;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * This function sets up the local APIC timer, with a timeout of
753 * 'clocks' APIC bus clock. During calibration we actually call
754 * this function twice on the boot CPU, once with a bogus timeout
755 * value, second time for real. The other (noncalibrating) CPUs
756 * call this function only once, with the real, calibrated value.
757 *
758 * We do reads before writes even if unnecessary, to get around the
759 * P5 APIC double write bug.
760 */
761
Thomas Gleixner80174092007-10-12 23:04:06 +0200762static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
David Rientjes86bd58b2006-12-07 02:14:11 +0100764 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Thomas Gleixner80174092007-10-12 23:04:06 +0200766 lvtt_value = LOCAL_TIMER_VECTOR;
767 if (!oneshot)
768 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
769 if (!irqen)
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100770 lvtt_value |= APIC_LVT_MASKED;
771
Andi Kleen11a8e772006-01-11 22:46:51 +0100772 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /*
775 * Divide PICLK by 16
776 */
777 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100778 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
780 | APIC_TDR_DIV_16);
781
Thomas Gleixner80174092007-10-12 23:04:06 +0200782 if (!oneshot)
Thomas Gleixnerb58eb002007-10-12 23:04:06 +0200783 apic_write(APIC_TMICT, clocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200786static void setup_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 unsigned long flags;
Thomas Gleixner80174092007-10-12 23:04:06 +0200789 int irqen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 local_irq_save(flags);
792
Thomas Gleixner80174092007-10-12 23:04:06 +0200793 irqen = ! cpu_isset(smp_processor_id(),
794 timer_interrupt_broadcast_ipi_mask);
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200795 __setup_APIC_LVTT(calibration_result, 0, irqen);
Andi Kleen73dea472006-02-03 21:50:50 +0100796 /* Turn off PIT interrupt if we use APIC timer as main timer.
797 Only works with the PM timer right now
798 TBD fix it for HPET too. */
john stultz14899392007-02-16 01:28:20 -0800799 if ((pmtmr_ioport != 0) &&
Andi Kleen73dea472006-02-03 21:50:50 +0100800 smp_processor_id() == boot_cpu_id &&
801 apic_runs_main_timer == 1 &&
802 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
803 stop_timer_interrupt();
804 apic_runs_main_timer++;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 local_irq_restore(flags);
807}
808
809/*
810 * In this function we calibrate APIC bus clocks to the external
811 * timer. Unfortunately we cannot use jiffies and the timer irq
812 * to calibrate, since some later bootup code depends on getting
813 * the first irq? Ugh.
814 *
815 * We want to do the calibration only once since we
816 * want to have local timer irqs syncron. CPUs connected
817 * by the same APIC bus have the very same bus frequency.
818 * And we want to have irqs off anyways, no accidental
819 * APIC irq that way.
820 */
821
822#define TICK_COUNT 100000000
823
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200824static void __init calibrate_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
David P. Reed4637a742007-05-02 19:27:20 +0200826 unsigned apic, apic_start;
827 unsigned long tsc, tsc_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int result;
Thomas Gleixnerc4d58cb2007-10-12 23:04:07 +0200829
830 local_irq_disable();
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * Put whatever arbitrary (but long enough) timeout
834 * value into the APIC clock, we just want to get the
835 * counter running for calibration.
Thomas Gleixner80174092007-10-12 23:04:06 +0200836 *
837 * No interrupt enable !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
Thomas Gleixnerb58eb002007-10-12 23:04:06 +0200839 __setup_APIC_LVTT(250000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100842#ifdef CONFIG_X86_PM_TIMER
843 if (apic_calibrate_pmtmr && pmtmr_ioport) {
844 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100846 result = (apic_start - apic) * 1000L / 5;
847 } else
848#endif
849 {
David P. Reed4637a742007-05-02 19:27:20 +0200850 rdtscll(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Andi Kleen0c3749c2006-02-03 21:51:41 +0100852 do {
853 apic = apic_read(APIC_TMCCT);
David P. Reed4637a742007-05-02 19:27:20 +0200854 rdtscll(tsc);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100855 } while ((tsc - tsc_start) < TICK_COUNT &&
David P. Reed4637a742007-05-02 19:27:20 +0200856 (apic_start - apic) < TICK_COUNT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100857
Joerg Roedel6b37f5a2007-05-02 19:27:06 +0200858 result = (apic_start - apic) * 1000L * tsc_khz /
Andi Kleen0c3749c2006-02-03 21:51:41 +0100859 (tsc - tsc_start);
860 }
Thomas Gleixnerc4d58cb2007-10-12 23:04:07 +0200861
862 local_irq_enable();
863
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200864 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
867 result / 1000 / 1000, result / 1000 % 1000);
868
Thomas Gleixnerb58eb002007-10-12 23:04:06 +0200869 calibration_result = result / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872void __init setup_boot_APIC_clock (void)
873{
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200874 if (disable_apic_timer) {
875 printk(KERN_INFO "Disabling APIC timer\n");
876 return;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 printk(KERN_INFO "Using local APIC timer interrupts.\n");
880 using_apic_timer = 1;
881
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200882 calibrate_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Now set up the timer for real.
885 */
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200886 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Ashok Raje6982c62005-06-25 14:54:58 -0700889void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Thomas Gleixnerabc63fc2007-10-12 23:04:07 +0200891 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100894void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 if (using_apic_timer) {
897 unsigned long v;
898
899 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200900 /*
901 * When an illegal vector value (0-15) is written to an LVT
902 * entry and delivery mode is Fixed, the APIC may signal an
903 * illegal vector error, with out regard to whether the mask
904 * bit is set or whether an interrupt is actually seen on input.
905 *
906 * Boot sequence might call this function when the LVTT has
907 * '0' vector value. So make sure vector field is set to
908 * valid value.
909 */
910 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
911 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913}
914
915void enable_APIC_timer(void)
916{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100917 int cpu = smp_processor_id();
918
919 if (using_apic_timer &&
920 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned long v;
922
923 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100924 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926}
927
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100928void switch_APIC_timer_to_ipi(void *cpumask)
929{
930 cpumask_t mask = *(cpumask_t *)cpumask;
931 int cpu = smp_processor_id();
932
933 if (cpu_isset(cpu, mask) &&
934 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
935 disable_APIC_timer();
936 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
937 }
938}
939EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
940
941void smp_send_timer_broadcast_ipi(void)
942{
Thomas Gleixnerf33bc552007-03-23 17:14:37 -0700943 int cpu = smp_processor_id();
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100944 cpumask_t mask;
945
946 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
Thomas Gleixnerf33bc552007-03-23 17:14:37 -0700947
948 if (cpu_isset(cpu, mask)) {
949 cpu_clear(cpu, mask);
950 add_pda(apic_timer_irqs, 1);
951 smp_local_timer_interrupt();
952 }
953
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100954 if (!cpus_empty(mask)) {
955 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
956 }
957}
958
959void switch_ipi_to_APIC_timer(void *cpumask)
960{
961 cpumask_t mask = *(cpumask_t *)cpumask;
962 int cpu = smp_processor_id();
963
964 if (cpu_isset(cpu, mask) &&
965 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
966 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
967 enable_APIC_timer();
968 }
969}
970EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972int setup_profiling_timer(unsigned int multiplier)
973{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Thomas Gleixnerf40f31b2007-07-21 17:10:14 +0200977void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
978 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100979{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200980 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
981 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100982 apic_write(reg, v);
983}
Jacob Shin89b831e2005-11-05 17:25:53 +0100984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/*
986 * Local timer interrupt handler. It does both profiling and
987 * process statistics/rescheduling.
988 *
989 * We do profiling in every local tick, statistics/rescheduling
990 * happen only every 'profiling multiplier' ticks. The default
991 * multiplier is 1 and it can be changed by writing the new multiplier
992 * value into /proc/profile.
993 */
994
David Howells7d12e782006-10-05 14:55:46 +0100995void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
David Howells7d12e782006-10-05 14:55:46 +0100997 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +0100999 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#endif
Andi Kleen73dea472006-02-03 21:50:50 +01001001 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
David Howells7d12e782006-10-05 14:55:46 +01001002 main_timer_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * We take the 'long' return path, and there every subsystem
1005 * grabs the appropriate locks (kernel lock/ irq lock).
1006 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +02001007 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * and do the profiling totally in assembly.
1009 *
1010 * Currently this isn't too much of an issue (performance wise),
1011 * we can take more than 100K local irqs per second on a 100 MHz P5.
1012 */
1013}
1014
1015/*
1016 * Local APIC timer interrupt. This is the most natural way for doing
1017 * local interrupts, but local timer interrupts can be emulated by
1018 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1019 *
1020 * [ if a single-CPU system runs an SMP kernel then we call the local
1021 * interrupt as well. Thus we cannot inline the local irq ... ]
1022 */
Andrew Mortond150ad72006-10-06 13:28:09 -07001023void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Andrew Mortond150ad72006-10-06 13:28:09 -07001025 struct pt_regs *old_regs = set_irq_regs(regs);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
1028 * the NMI deadlock-detector uses this.
1029 */
1030 add_pda(apic_timer_irqs, 1);
1031
1032 /*
1033 * NOTE! We'd better ACK the irq immediately,
1034 * because timer handling can be slow.
1035 */
1036 ack_APIC_irq();
1037 /*
1038 * update_process_times() expects us to have done irq_enter().
1039 * Besides, if we don't timer interrupts ignore the global
1040 * interrupt lock, which is the WrongThing (tm) to do.
1041 */
Andi Kleen95833c82006-01-11 22:44:36 +01001042 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +01001044 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 irq_exit();
Andrew Mortond150ad72006-10-06 13:28:09 -07001046 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001050 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
1052 * Thus far, the major user of this is IBM's Summit2 series:
1053 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001054 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * multi-chassis. Use available data to take a good guess.
1056 * If in doubt, go HPET.
1057 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001058__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 int i, clusters, zeros;
1061 unsigned id;
1062 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1063
Suresh Siddha376ec332005-05-16 21:53:32 -07001064 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 for (i = 0; i < NR_CPUS; i++) {
1067 id = bios_cpu_apicid[i];
1068 if (id != BAD_APICID)
1069 __set_bit(APIC_CLUSTERID(id), clustermap);
1070 }
1071
1072 /* Problem: Partially populated chassis may not have CPUs in some of
1073 * the APIC clusters they have been allocated. Only present CPUs have
1074 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1075 * clusters are allocated sequentially, count zeros only if they are
1076 * bounded by ones.
1077 */
1078 clusters = 0;
1079 zeros = 0;
1080 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1081 if (test_bit(i, clustermap)) {
1082 clusters += 1 + zeros;
1083 zeros = 0;
1084 } else
1085 ++zeros;
1086 }
1087
1088 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001089 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * May have to revisit this when multi-core + hyperthreaded CPUs come
1091 * out, but AFAIK this will work even for them.
1092 */
1093 return (clusters > 2);
1094}
1095
1096/*
1097 * This interrupt should _never_ happen with our APIC/SMP architecture
1098 */
1099asmlinkage void smp_spurious_interrupt(void)
1100{
1101 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001102 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 irq_enter();
1104 /*
1105 * Check if this really is a spurious interrupt and ACK it
1106 * if it is a vectored one. Just in case...
1107 * Spurious interrupts should not be ACKed.
1108 */
1109 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1110 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1111 ack_APIC_irq();
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 irq_exit();
1114}
1115
1116/*
1117 * This interrupt should never happen with our APIC/SMP architecture
1118 */
1119
1120asmlinkage void smp_error_interrupt(void)
1121{
1122 unsigned int v, v1;
1123
Andi Kleen95833c82006-01-11 22:44:36 +01001124 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 irq_enter();
1126 /* First tickle the hardware, only then report what went on. -- REW */
1127 v = apic_read(APIC_ESR);
1128 apic_write(APIC_ESR, 0);
1129 v1 = apic_read(APIC_ESR);
1130 ack_APIC_irq();
1131 atomic_inc(&irq_err_count);
1132
1133 /* Here is what the APIC error bits mean:
1134 0: Send CS error
1135 1: Receive CS error
1136 2: Send accept error
1137 3: Receive accept error
1138 4: Reserved
1139 5: Send illegal vector
1140 6: Received illegal vector
1141 7: Illegal register address
1142 */
1143 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001144 smp_processor_id(), v , v1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 irq_exit();
1146}
1147
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001148int disable_apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150/*
1151 * This initializes the IO-APIC and APIC hardware if this is
1152 * a UP kernel.
1153 */
1154int __init APIC_init_uniprocessor (void)
1155{
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001156 if (disable_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 printk(KERN_INFO "Apic disabled\n");
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001158 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001160 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 disable_apic = 1;
1162 printk(KERN_INFO "Apic disabled by BIOS\n");
1163 return -1;
1164 }
1165
1166 verify_local_APIC();
1167
Andi Kleen357e11d2005-09-12 18:49:24 +02001168 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001169 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 setup_local_APIC();
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001174 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 else
1176 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001178 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180}
1181
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001182static __init int setup_disableapic(char *str)
1183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001185 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1186 return 0;
1187}
1188early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001190/* same as disableapic, for compatibility */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001191static __init int setup_nolapic(char *str)
1192{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001193 return setup_disableapic(str);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001194}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001195early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001197static int __init parse_lapic_timer_c2_ok(char *arg)
1198{
1199 local_apic_timer_c2_ok = 1;
1200 return 0;
1201}
1202early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1203
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001204static __init int setup_noapictimer(char *str)
1205{
Andi Kleen73dea472006-02-03 21:50:50 +01001206 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001209 return 1;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Andi Kleen73dea472006-02-03 21:50:50 +01001212static __init int setup_apicmaintimer(char *str)
1213{
1214 apic_runs_main_timer = 1;
1215 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001216 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001217}
1218__setup("apicmaintimer", setup_apicmaintimer);
1219
1220static __init int setup_noapicmaintimer(char *str)
1221{
1222 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001223 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001224}
1225__setup("noapicmaintimer", setup_noapicmaintimer);
1226
Andi Kleen0c3749c2006-02-03 21:51:41 +01001227static __init int setup_apicpmtimer(char *s)
1228{
1229 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001230 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001231 return setup_apicmaintimer(NULL);
1232}
1233__setup("apicpmtimer", setup_apicpmtimer);
1234
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001235__setup("noapictimer", setup_noapictimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236