blob: bd3e45d47c372bd5289b7a9dd1f3099f8267f18e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
22#include <linux/smp_lock.h>
23#include <linux/interrupt.h>
24#include <linux/mc146818rtc.h>
25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h>
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010027#include <linux/module.h>
Aaron Durbin39928722006-12-07 02:14:01 +010028#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/atomic.h>
31#include <asm/smp.h>
32#include <asm/mtrr.h>
33#include <asm/mpspec.h>
34#include <asm/pgalloc.h>
35#include <asm/mach_apic.h>
Andi Kleen75152112005-05-16 21:53:34 -070036#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010037#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010038#include <asm/proto.h>
39#include <asm/timex.h>
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
Andi Kleenb7f5e3c2006-09-26 10:52:34 +020043int apic_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010045int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010046int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48int disable_apic_timer __initdata;
49
Linus Torvalds2e7c2832007-03-23 11:32:31 -070050/* Local APIC timer works in C2? */
51int local_apic_timer_c2_ok;
52EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
53
Aaron Durbin39928722006-12-07 02:14:01 +010054static struct resource *ioapic_resources;
55static struct resource lapic_resource = {
56 .name = "Local APIC",
57 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
58};
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
71void enable_NMI_through_LVT0 (void * dummy)
72{
Andi Kleen11a8e772006-01-11 22:46:51 +010073 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 v = APIC_DM_NMI; /* unmask and set to NMI */
Andi Kleen11a8e772006-01-11 22:46:51 +010076 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79int get_maxlvt(void)
80{
Andi Kleen11a8e772006-01-11 22:46:51 +010081 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 maxlvt = GET_APIC_MAXLVT(v);
85 return maxlvt;
86}
87
Andi Kleen3777a952006-02-03 21:51:53 +010088/*
89 * 'what should we do if we get a hw irq event on an illegal vector'.
90 * each architecture has to answer this themselves.
91 */
92void ack_bad_irq(unsigned int irq)
93{
94 printk("unexpected IRQ trap at vector %02x\n", irq);
95 /*
96 * Currently unexpected vectors happen only on SMP and APIC.
97 * We _must_ ack these because every local APIC has only N
98 * irq slots per priority level, and a 'hanging, unacked' IRQ
99 * holds up an irq slot - in excessive cases (when multiple
100 * unexpected vectors occur) that might lock up the APIC
101 * completely.
102 * But don't ack when the APIC is disabled. -AK
103 */
104 if (!disable_apic)
105 ack_APIC_irq();
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108void clear_local_APIC(void)
109{
110 int maxlvt;
111 unsigned int v;
112
113 maxlvt = get_maxlvt();
114
115 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200116 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * if the vector is zero. Mask LVTERR first to prevent this.
118 */
119 if (maxlvt >= 3) {
120 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100121 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 /*
124 * Careful: we have to set masks only first to deassert
125 * any level-triggered sources.
126 */
127 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100128 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100130 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100132 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (maxlvt >= 4) {
134 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100135 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137
138 /*
139 * Clean APIC state for other OSs:
140 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100141 apic_write(APIC_LVTT, APIC_LVT_MASKED);
142 apic_write(APIC_LVT0, APIC_LVT_MASKED);
143 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100145 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100147 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200148 apic_write(APIC_ESR, 0);
149 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Eric W. Biederman208fb932005-06-25 14:57:45 -0700152void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200154 /* Go back to Virtual Wire compatibility mode */
155 unsigned long value;
156
157 /* For the spurious interrupt use vector F, and enable it */
158 value = apic_read(APIC_SPIV);
159 value &= ~APIC_VECTOR_MASK;
160 value |= APIC_SPIV_APIC_ENABLED;
161 value |= 0xf;
162 apic_write(APIC_SPIV, value);
163
164 if (!virt_wire_setup) {
165 /* For LVT0 make it edge triggered, active high, external and enabled */
166 value = apic_read(APIC_LVT0);
167 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
168 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
169 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
170 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
171 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
172 apic_write(APIC_LVT0, value);
173 } else {
174 /* Disable LVT0 */
175 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700177
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200178 /* For LVT1 make it edge triggered, active high, nmi and enabled */
179 value = apic_read(APIC_LVT1);
180 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700181 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
182 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200183 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
184 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
185 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188void disable_local_APIC(void)
189{
190 unsigned int value;
191
192 clear_local_APIC();
193
194 /*
195 * Disable APIC (implies clearing of registers
196 * for 82489DX!).
197 */
198 value = apic_read(APIC_SPIV);
199 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100200 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/*
204 * This is to verify that we're looking at a real local APIC.
205 * Check these against your board if the CPUs aren't getting
206 * started for no apparent reason.
207 */
208int __init verify_local_APIC(void)
209{
210 unsigned int reg0, reg1;
211
212 /*
213 * The version register is read-only in a real APIC.
214 */
215 reg0 = apic_read(APIC_LVR);
216 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
217 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
218 reg1 = apic_read(APIC_LVR);
219 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
220
221 /*
222 * The two version reads above should print the same
223 * numbers. If the second one is different, then we
224 * poke at a non-APIC.
225 */
226 if (reg1 != reg0)
227 return 0;
228
229 /*
230 * Check if the version looks reasonably.
231 */
232 reg1 = GET_APIC_VERSION(reg0);
233 if (reg1 == 0x00 || reg1 == 0xff)
234 return 0;
235 reg1 = get_maxlvt();
236 if (reg1 < 0x02 || reg1 == 0xff)
237 return 0;
238
239 /*
240 * The ID register is read/write in a real APIC.
241 */
242 reg0 = apic_read(APIC_ID);
243 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
244 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
245 reg1 = apic_read(APIC_ID);
246 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
247 apic_write(APIC_ID, reg0);
248 if (reg1 != (reg0 ^ APIC_ID_MASK))
249 return 0;
250
251 /*
252 * The next two are just to see if we have sane values.
253 * They're only really relevant if we're in Virtual Wire
254 * compatibility mode, but most boxes are anymore.
255 */
256 reg0 = apic_read(APIC_LVT0);
257 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
258 reg1 = apic_read(APIC_LVT1);
259 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
260
261 return 1;
262}
263
264void __init sync_Arb_IDs(void)
265{
266 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
267 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
268 if (ver >= 0x14) /* P4 or higher */
269 return;
270
271 /*
272 * Wait for idle.
273 */
274 apic_wait_icr_idle();
275
276 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100277 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 | APIC_DM_INIT);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * An initial setup of the virtual wire mode.
283 */
284void __init init_bsp_APIC(void)
285{
Andi Kleen11a8e772006-01-11 22:46:51 +0100286 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /*
289 * Don't do the setup now if we have a SMP BIOS as the
290 * through-I/O-APIC virtual wire mode might be active.
291 */
292 if (smp_found_config || !cpu_has_apic)
293 return;
294
295 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /*
298 * Do not trust the local APIC being empty at bootup.
299 */
300 clear_local_APIC();
301
302 /*
303 * Enable APIC.
304 */
305 value = apic_read(APIC_SPIV);
306 value &= ~APIC_VECTOR_MASK;
307 value |= APIC_SPIV_APIC_ENABLED;
308 value |= APIC_SPIV_FOCUS_DISABLED;
309 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100310 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /*
313 * Set up the virtual wire mode.
314 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100315 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100317 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Ashok Raje6982c62005-06-25 14:54:58 -0700320void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Andi Kleen11a8e772006-01-11 22:46:51 +0100322 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100323 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Andi Kleenfe7414a2006-09-26 10:52:30 +0200327 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /*
330 * Double-check whether this APIC is really registered.
331 * This is meaningless in clustered apic mode, so we skip it.
332 */
333 if (!apic_id_registered())
334 BUG();
335
336 /*
337 * Intel recommends to set DFR, LDR and TPR before enabling
338 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
339 * document number 292116). So here it goes...
340 */
341 init_apic_ldr();
342
343 /*
344 * Set Task Priority to 'accept all'. We never change this
345 * later on.
346 */
347 value = apic_read(APIC_TASKPRI);
348 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100349 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100352 * After a crash, we no longer service the interrupts and a pending
353 * interrupt from previous kernel might still have ISR bit set.
354 *
355 * Most probably by now CPU has serviced that pending interrupt and
356 * it might not have done the ack_APIC_irq() because it thought,
357 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
358 * does not clear the ISR bit and cpu thinks it has already serivced
359 * the interrupt. Hence a vector might get locked. It was noticed
360 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
361 */
362 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
363 value = apic_read(APIC_ISR + i*0x10);
364 for (j = 31; j >= 0; j--) {
365 if (value & (1<<j))
366 ack_APIC_irq();
367 }
368 }
369
370 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Now that we are all set up, enable the APIC
372 */
373 value = apic_read(APIC_SPIV);
374 value &= ~APIC_VECTOR_MASK;
375 /*
376 * Enable APIC
377 */
378 value |= APIC_SPIV_APIC_ENABLED;
379
Andi Kleen3f14c742006-09-26 10:52:29 +0200380 /* We always use processor focus */
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * Set spurious IRQ vector
384 */
385 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100386 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /*
389 * Set up LVT0, LVT1:
390 *
391 * set up through-local-APIC on the BP's LINT0. This is not
392 * strictly necessary in pure symmetric-IO mode, but sometimes
393 * we delegate interrupts to the 8259A.
394 */
395 /*
396 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
397 */
398 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200399 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 value = APIC_DM_EXTINT;
401 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
402 } else {
403 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
404 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
405 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100406 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /*
409 * only the BP should see the LINT1 NMI signal, obviously.
410 */
411 if (!smp_processor_id())
412 value = APIC_DM_NMI;
413 else
414 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100415 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Andi Kleen61c11342005-09-12 18:49:23 +0200417 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unsigned oldvalue;
419 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 oldvalue = apic_read(APIC_ESR);
421 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100422 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * spec says clear errors after enabling vector.
425 */
426 if (maxlvt > 3)
427 apic_write(APIC_ESR, 0);
428 value = apic_read(APIC_ESR);
429 if (value != oldvalue)
430 apic_printk(APIC_VERBOSE,
431 "ESR value after enabling vector: %08x, after %08x\n",
432 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200436 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 apic_pm_activate();
438}
439
440#ifdef CONFIG_PM
441
442static struct {
443 /* 'active' is true if the local APIC was enabled by us and
444 not the BIOS; this signifies that we are also responsible
445 for disabling it before entering apm/acpi suspend */
446 int active;
447 /* r/w apic fields */
448 unsigned int apic_id;
449 unsigned int apic_taskpri;
450 unsigned int apic_ldr;
451 unsigned int apic_dfr;
452 unsigned int apic_spiv;
453 unsigned int apic_lvtt;
454 unsigned int apic_lvtpc;
455 unsigned int apic_lvt0;
456 unsigned int apic_lvt1;
457 unsigned int apic_lvterr;
458 unsigned int apic_tmict;
459 unsigned int apic_tdcr;
460 unsigned int apic_thmr;
461} apic_pm_state;
462
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700463static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100466 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (!apic_pm_state.active)
469 return 0;
470
Karsten Wiesef990fff2006-12-07 02:14:11 +0100471 maxlvt = get_maxlvt();
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 apic_pm_state.apic_id = apic_read(APIC_ID);
474 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
475 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
476 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
477 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
478 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100479 if (maxlvt >= 4)
480 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
482 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
483 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
484 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
485 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100486#ifdef CONFIG_X86_MCE_INTEL
487 if (maxlvt >= 5)
488 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
489#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200490 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 disable_local_APIC();
492 local_irq_restore(flags);
493 return 0;
494}
495
496static int lapic_resume(struct sys_device *dev)
497{
498 unsigned int l, h;
499 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100500 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!apic_pm_state.active)
503 return 0;
504
Karsten Wiesef990fff2006-12-07 02:14:11 +0100505 maxlvt = get_maxlvt();
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 local_irq_save(flags);
508 rdmsr(MSR_IA32_APICBASE, l, h);
509 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100510 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 wrmsr(MSR_IA32_APICBASE, l, h);
512 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
513 apic_write(APIC_ID, apic_pm_state.apic_id);
514 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
515 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
516 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
517 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
518 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
519 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100520#ifdef CONFIG_X86_MCE_INTEL
521 if (maxlvt >= 5)
522 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
523#endif
524 if (maxlvt >= 4)
525 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
527 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
528 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
529 apic_write(APIC_ESR, 0);
530 apic_read(APIC_ESR);
531 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
532 apic_write(APIC_ESR, 0);
533 apic_read(APIC_ESR);
534 local_irq_restore(flags);
535 return 0;
536}
537
538static struct sysdev_class lapic_sysclass = {
539 set_kset_name("lapic"),
540 .resume = lapic_resume,
541 .suspend = lapic_suspend,
542};
543
544static struct sys_device device_lapic = {
545 .id = 0,
546 .cls = &lapic_sysclass,
547};
548
Ashok Raje6982c62005-06-25 14:54:58 -0700549static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 apic_pm_state.active = 1;
552}
553
554static int __init init_lapic_sysfs(void)
555{
556 int error;
557 if (!cpu_has_apic)
558 return 0;
559 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
560 error = sysdev_class_register(&lapic_sysclass);
561 if (!error)
562 error = sysdev_register(&device_lapic);
563 return error;
564}
565device_initcall(init_lapic_sysfs);
566
567#else /* CONFIG_PM */
568
569static void apic_pm_activate(void) { }
570
571#endif /* CONFIG_PM */
572
573static int __init apic_set_verbosity(char *str)
574{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200575 if (str == NULL) {
576 skip_ioapic_setup = 0;
577 ioapic_force = 1;
578 return 0;
579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (strcmp("debug", str) == 0)
581 apic_verbosity = APIC_DEBUG;
582 else if (strcmp("verbose", str) == 0)
583 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200584 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200586 " use apic=verbose or apic=debug\n", str);
587 return -EINVAL;
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200592early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/*
595 * Detect and enable local APICs on non-SMP boards.
596 * Original code written by Keir Fraser.
597 * On AMD64 we trust the BIOS - if it says no APIC it is likely
598 * not correctly set up (usually the APIC timer won't work etc.)
599 */
600
601static int __init detect_init_APIC (void)
602{
603 if (!cpu_has_apic) {
604 printk(KERN_INFO "No local APIC present\n");
605 return -1;
606 }
607
608 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
609 boot_cpu_id = 0;
610 return 0;
611}
612
Aaron Durbin39928722006-12-07 02:14:01 +0100613#ifdef CONFIG_X86_IO_APIC
614static struct resource * __init ioapic_setup_resources(void)
615{
616#define IOAPIC_RESOURCE_NAME_SIZE 11
617 unsigned long n;
618 struct resource *res;
619 char *mem;
620 int i;
621
622 if (nr_ioapics <= 0)
623 return NULL;
624
625 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
626 n *= nr_ioapics;
627
628 mem = alloc_bootmem(n);
629 res = (void *)mem;
630
631 if (mem != NULL) {
632 memset(mem, 0, n);
633 mem += sizeof(struct resource) * nr_ioapics;
634
635 for (i = 0; i < nr_ioapics; i++) {
636 res[i].name = mem;
637 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
638 sprintf(mem, "IOAPIC %u", i);
639 mem += IOAPIC_RESOURCE_NAME_SIZE;
640 }
641 }
642
643 ioapic_resources = res;
644
645 return res;
646}
647
648static int __init ioapic_insert_resources(void)
649{
650 int i;
651 struct resource *r = ioapic_resources;
652
653 if (!r) {
654 printk("IO APIC resources could be not be allocated.\n");
655 return -1;
656 }
657
658 for (i = 0; i < nr_ioapics; i++) {
659 insert_resource(&iomem_resource, r);
660 r++;
661 }
662
663 return 0;
664}
665
666/* Insert the IO APIC resources after PCI initialization has occured to handle
667 * IO APICS that are mapped in on a BAR in PCI space. */
668late_initcall(ioapic_insert_resources);
669#endif
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671void __init init_apic_mappings(void)
672{
673 unsigned long apic_phys;
674
675 /*
676 * If no local APIC can be found then set up a fake all
677 * zeroes page to simulate the local APIC and another
678 * one for the IO-APIC.
679 */
680 if (!smp_found_config && detect_init_APIC()) {
681 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
682 apic_phys = __pa(apic_phys);
683 } else
684 apic_phys = mp_lapic_addr;
685
686 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Andi Kleenb7f5e3c2006-09-26 10:52:34 +0200687 apic_mapped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
689
Aaron Durbin39928722006-12-07 02:14:01 +0100690 /* Put local APIC into the resource map. */
691 lapic_resource.start = apic_phys;
692 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
693 insert_resource(&iomem_resource, &lapic_resource);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * Fetch the APIC ID of the BSP in case we have a
697 * default configuration (or the MP table is broken).
698 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200699 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 {
702 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
703 int i;
Aaron Durbin39928722006-12-07 02:14:01 +0100704 struct resource *ioapic_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Aaron Durbin39928722006-12-07 02:14:01 +0100706 ioapic_res = ioapic_setup_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 for (i = 0; i < nr_ioapics; i++) {
708 if (smp_found_config) {
709 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
710 } else {
711 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
712 ioapic_phys = __pa(ioapic_phys);
713 }
714 set_fixmap_nocache(idx, ioapic_phys);
715 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
716 __fix_to_virt(idx), ioapic_phys);
717 idx++;
Aaron Durbin39928722006-12-07 02:14:01 +0100718
719 if (ioapic_res != NULL) {
720 ioapic_res->start = ioapic_phys;
721 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
722 ioapic_res++;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/*
729 * This function sets up the local APIC timer, with a timeout of
730 * 'clocks' APIC bus clock. During calibration we actually call
731 * this function twice on the boot CPU, once with a bogus timeout
732 * value, second time for real. The other (noncalibrating) CPUs
733 * call this function only once, with the real, calibrated value.
734 *
735 * We do reads before writes even if unnecessary, to get around the
736 * P5 APIC double write bug.
737 */
738
739#define APIC_DIVISOR 16
740
741static void __setup_APIC_LVTT(unsigned int clocks)
742{
David Rientjes86bd58b2006-12-07 02:14:11 +0100743 unsigned int lvtt_value, tmp_value;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100744 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100747
748 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
749 lvtt_value |= APIC_LVT_MASKED;
750
Andi Kleen11a8e772006-01-11 22:46:51 +0100751 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * Divide PICLK by 16
755 */
756 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100757 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
759 | APIC_TDR_DIV_16);
760
Andi Kleen11a8e772006-01-11 22:46:51 +0100761 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static void setup_APIC_timer(unsigned int clocks)
765{
766 unsigned long flags;
767
768 local_irq_save(flags);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* wait for irq slice */
john stultz2d0c87c2007-02-16 01:28:18 -0800771 if (hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int trigger = hpet_readl(HPET_T0_CMP);
773 while (hpet_readl(HPET_COUNTER) >= trigger)
774 /* do nothing */ ;
775 while (hpet_readl(HPET_COUNTER) < trigger)
776 /* do nothing */ ;
777 } else {
778 int c1, c2;
779 outb_p(0x00, 0x43);
780 c2 = inb_p(0x40);
781 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100782 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 c1 = c2;
784 outb_p(0x00, 0x43);
785 c2 = inb_p(0x40);
786 c2 |= inb_p(0x40) << 8;
787 } while (c2 - c1 < 300);
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100790 /* Turn off PIT interrupt if we use APIC timer as main timer.
791 Only works with the PM timer right now
792 TBD fix it for HPET too. */
john stultz14899392007-02-16 01:28:20 -0800793 if ((pmtmr_ioport != 0) &&
Andi Kleen73dea472006-02-03 21:50:50 +0100794 smp_processor_id() == boot_cpu_id &&
795 apic_runs_main_timer == 1 &&
796 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
797 stop_timer_interrupt();
798 apic_runs_main_timer++;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 local_irq_restore(flags);
801}
802
803/*
804 * In this function we calibrate APIC bus clocks to the external
805 * timer. Unfortunately we cannot use jiffies and the timer irq
806 * to calibrate, since some later bootup code depends on getting
807 * the first irq? Ugh.
808 *
809 * We want to do the calibration only once since we
810 * want to have local timer irqs syncron. CPUs connected
811 * by the same APIC bus have the very same bus frequency.
812 * And we want to have irqs off anyways, no accidental
813 * APIC irq that way.
814 */
815
816#define TICK_COUNT 100000000
817
818static int __init calibrate_APIC_clock(void)
819{
820 int apic, apic_start, tsc, tsc_start;
821 int result;
822 /*
823 * Put whatever arbitrary (but long enough) timeout
824 * value into the APIC clock, we just want to get the
825 * counter running for calibration.
826 */
827 __setup_APIC_LVTT(1000000000);
828
829 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100830#ifdef CONFIG_X86_PM_TIMER
831 if (apic_calibrate_pmtmr && pmtmr_ioport) {
832 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100834 result = (apic_start - apic) * 1000L / 5;
835 } else
836#endif
837 {
838 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Andi Kleen0c3749c2006-02-03 21:51:41 +0100840 do {
841 apic = apic_read(APIC_TMCCT);
842 rdtscl(tsc);
843 } while ((tsc - tsc_start) < TICK_COUNT &&
844 (apic - apic_start) < TICK_COUNT);
845
846 result = (apic_start - apic) * 1000L * cpu_khz /
847 (tsc - tsc_start);
848 }
849 printk("result %d\n", result);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
853 result / 1000 / 1000, result / 1000 % 1000);
854
855 return result * APIC_DIVISOR / HZ;
856}
857
858static unsigned int calibration_result;
859
860void __init setup_boot_APIC_clock (void)
861{
862 if (disable_apic_timer) {
863 printk(KERN_INFO "Disabling APIC timer\n");
864 return;
865 }
866
867 printk(KERN_INFO "Using local APIC timer interrupts.\n");
868 using_apic_timer = 1;
869
870 local_irq_disable();
871
872 calibration_result = calibrate_APIC_clock();
873 /*
874 * Now set up the timer for real.
875 */
876 setup_APIC_timer(calibration_result);
877
878 local_irq_enable();
879}
880
Ashok Raje6982c62005-06-25 14:54:58 -0700881void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 local_irq_disable(); /* FIXME: Do we need this? --RR */
884 setup_APIC_timer(calibration_result);
885 local_irq_enable();
886}
887
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100888void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 if (using_apic_timer) {
891 unsigned long v;
892
893 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200894 /*
895 * When an illegal vector value (0-15) is written to an LVT
896 * entry and delivery mode is Fixed, the APIC may signal an
897 * illegal vector error, with out regard to whether the mask
898 * bit is set or whether an interrupt is actually seen on input.
899 *
900 * Boot sequence might call this function when the LVTT has
901 * '0' vector value. So make sure vector field is set to
902 * valid value.
903 */
904 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
905 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907}
908
909void enable_APIC_timer(void)
910{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100911 int cpu = smp_processor_id();
912
913 if (using_apic_timer &&
914 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned long v;
916
917 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100918 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920}
921
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100922void switch_APIC_timer_to_ipi(void *cpumask)
923{
924 cpumask_t mask = *(cpumask_t *)cpumask;
925 int cpu = smp_processor_id();
926
927 if (cpu_isset(cpu, mask) &&
928 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
929 disable_APIC_timer();
930 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
931 }
932}
933EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
934
935void smp_send_timer_broadcast_ipi(void)
936{
Thomas Gleixnerf33bc552007-03-23 17:14:37 -0700937 int cpu = smp_processor_id();
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100938 cpumask_t mask;
939
940 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
Thomas Gleixnerf33bc552007-03-23 17:14:37 -0700941
942 if (cpu_isset(cpu, mask)) {
943 cpu_clear(cpu, mask);
944 add_pda(apic_timer_irqs, 1);
945 smp_local_timer_interrupt();
946 }
947
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100948 if (!cpus_empty(mask)) {
949 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
950 }
951}
952
953void switch_ipi_to_APIC_timer(void *cpumask)
954{
955 cpumask_t mask = *(cpumask_t *)cpumask;
956 int cpu = smp_processor_id();
957
958 if (cpu_isset(cpu, mask) &&
959 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
960 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
961 enable_APIC_timer();
962 }
963}
964EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
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
Jacob Shin17fc14f2006-06-26 13:58:47 +0200971void setup_APIC_extened_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#undef APIC_DIVISOR
980
981/*
982 * Local timer interrupt handler. It does both profiling and
983 * process statistics/rescheduling.
984 *
985 * We do profiling in every local tick, statistics/rescheduling
986 * happen only every 'profiling multiplier' ticks. The default
987 * multiplier is 1 and it can be changed by writing the new multiplier
988 * value into /proc/profile.
989 */
990
David Howells7d12e782006-10-05 14:55:46 +0100991void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
David Howells7d12e782006-10-05 14:55:46 +0100993 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +0100995 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100997 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
David Howells7d12e782006-10-05 14:55:46 +0100998 main_timer_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
1000 * We take the 'long' return path, and there every subsystem
1001 * grabs the appropriate locks (kernel lock/ irq lock).
1002 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +02001003 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * and do the profiling totally in assembly.
1005 *
1006 * Currently this isn't too much of an issue (performance wise),
1007 * we can take more than 100K local irqs per second on a 100 MHz P5.
1008 */
1009}
1010
1011/*
1012 * Local APIC timer interrupt. This is the most natural way for doing
1013 * local interrupts, but local timer interrupts can be emulated by
1014 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1015 *
1016 * [ if a single-CPU system runs an SMP kernel then we call the local
1017 * interrupt as well. Thus we cannot inline the local irq ... ]
1018 */
Andrew Mortond150ad72006-10-06 13:28:09 -07001019void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Andrew Mortond150ad72006-10-06 13:28:09 -07001021 struct pt_regs *old_regs = set_irq_regs(regs);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * the NMI deadlock-detector uses this.
1025 */
1026 add_pda(apic_timer_irqs, 1);
1027
1028 /*
1029 * NOTE! We'd better ACK the irq immediately,
1030 * because timer handling can be slow.
1031 */
1032 ack_APIC_irq();
1033 /*
1034 * update_process_times() expects us to have done irq_enter().
1035 * Besides, if we don't timer interrupts ignore the global
1036 * interrupt lock, which is the WrongThing (tm) to do.
1037 */
Andi Kleen95833c82006-01-11 22:44:36 +01001038 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +01001040 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 irq_exit();
Andrew Mortond150ad72006-10-06 13:28:09 -07001042 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001046 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
1048 * Thus far, the major user of this is IBM's Summit2 series:
1049 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001050 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * multi-chassis. Use available data to take a good guess.
1052 * If in doubt, go HPET.
1053 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001054__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 int i, clusters, zeros;
1057 unsigned id;
1058 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1059
Suresh Siddha376ec332005-05-16 21:53:32 -07001060 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 for (i = 0; i < NR_CPUS; i++) {
1063 id = bios_cpu_apicid[i];
1064 if (id != BAD_APICID)
1065 __set_bit(APIC_CLUSTERID(id), clustermap);
1066 }
1067
1068 /* Problem: Partially populated chassis may not have CPUs in some of
1069 * the APIC clusters they have been allocated. Only present CPUs have
1070 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1071 * clusters are allocated sequentially, count zeros only if they are
1072 * bounded by ones.
1073 */
1074 clusters = 0;
1075 zeros = 0;
1076 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1077 if (test_bit(i, clustermap)) {
1078 clusters += 1 + zeros;
1079 zeros = 0;
1080 } else
1081 ++zeros;
1082 }
1083
1084 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001085 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * May have to revisit this when multi-core + hyperthreaded CPUs come
1087 * out, but AFAIK this will work even for them.
1088 */
1089 return (clusters > 2);
1090}
1091
1092/*
1093 * This interrupt should _never_ happen with our APIC/SMP architecture
1094 */
1095asmlinkage void smp_spurious_interrupt(void)
1096{
1097 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001098 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 irq_enter();
1100 /*
1101 * Check if this really is a spurious interrupt and ACK it
1102 * if it is a vectored one. Just in case...
1103 * Spurious interrupts should not be ACKed.
1104 */
1105 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1106 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1107 ack_APIC_irq();
1108
1109#if 0
1110 static unsigned long last_warning;
1111 static unsigned long skipped;
1112
1113 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1114 if (time_before(last_warning+30*HZ,jiffies)) {
1115 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1116 smp_processor_id(), skipped);
1117 last_warning = jiffies;
1118 skipped = 0;
1119 } else {
1120 skipped++;
1121 }
1122#endif
1123 irq_exit();
1124}
1125
1126/*
1127 * This interrupt should never happen with our APIC/SMP architecture
1128 */
1129
1130asmlinkage void smp_error_interrupt(void)
1131{
1132 unsigned int v, v1;
1133
Andi Kleen95833c82006-01-11 22:44:36 +01001134 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 irq_enter();
1136 /* First tickle the hardware, only then report what went on. -- REW */
1137 v = apic_read(APIC_ESR);
1138 apic_write(APIC_ESR, 0);
1139 v1 = apic_read(APIC_ESR);
1140 ack_APIC_irq();
1141 atomic_inc(&irq_err_count);
1142
1143 /* Here is what the APIC error bits mean:
1144 0: Send CS error
1145 1: Receive CS error
1146 2: Send accept error
1147 3: Receive accept error
1148 4: Reserved
1149 5: Send illegal vector
1150 6: Received illegal vector
1151 7: Illegal register address
1152 */
1153 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1154 smp_processor_id(), v , v1);
1155 irq_exit();
1156}
1157
1158int disable_apic;
1159
1160/*
1161 * This initializes the IO-APIC and APIC hardware if this is
1162 * a UP kernel.
1163 */
1164int __init APIC_init_uniprocessor (void)
1165{
1166 if (disable_apic) {
1167 printk(KERN_INFO "Apic disabled\n");
1168 return -1;
1169 }
1170 if (!cpu_has_apic) {
1171 disable_apic = 1;
1172 printk(KERN_INFO "Apic disabled by BIOS\n");
1173 return -1;
1174 }
1175
1176 verify_local_APIC();
1177
Andi Kleen357e11d2005-09-12 18:49:24 +02001178 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001179 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 setup_local_APIC();
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001184 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 else
1186 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001188 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190}
1191
1192static __init int setup_disableapic(char *str)
1193{
1194 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001195 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1196 return 0;
1197}
1198early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001200/* same as disableapic, for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201static __init int setup_nolapic(char *str)
1202{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001203 return setup_disableapic(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001205early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001207static int __init parse_lapic_timer_c2_ok(char *arg)
1208{
1209 local_apic_timer_c2_ok = 1;
1210 return 0;
1211}
1212early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214static __init int setup_noapictimer(char *str)
1215{
Andi Kleen73dea472006-02-03 21:50:50 +01001216 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001219 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Andi Kleen73dea472006-02-03 21:50:50 +01001222static __init int setup_apicmaintimer(char *str)
1223{
1224 apic_runs_main_timer = 1;
1225 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001226 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001227}
1228__setup("apicmaintimer", setup_apicmaintimer);
1229
1230static __init int setup_noapicmaintimer(char *str)
1231{
1232 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001233 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001234}
1235__setup("noapicmaintimer", setup_noapicmaintimer);
1236
Andi Kleen0c3749c2006-02-03 21:51:41 +01001237static __init int setup_apicpmtimer(char *s)
1238{
1239 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001240 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001241 return setup_apicmaintimer(NULL);
1242}
1243__setup("apicpmtimer", setup_apicpmtimer);
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245__setup("noapictimer", setup_noapictimer);
1246