blob: 723417d924c09717c9c6659fbf5fe7361b0caafb [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
Aaron Durbin39928722006-12-07 02:14:01 +010050static struct resource *ioapic_resources;
51static struct resource lapic_resource = {
52 .name = "Local APIC",
53 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
54};
55
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010056/*
57 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
58 * IPIs in place of local APIC timers
59 */
60static cpumask_t timer_interrupt_broadcast_ipi_mask;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070063int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void apic_pm_activate(void);
66
67void enable_NMI_through_LVT0 (void * dummy)
68{
Andi Kleen11a8e772006-01-11 22:46:51 +010069 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 v = APIC_DM_NMI; /* unmask and set to NMI */
Andi Kleen11a8e772006-01-11 22:46:51 +010072 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75int get_maxlvt(void)
76{
Andi Kleen11a8e772006-01-11 22:46:51 +010077 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 maxlvt = GET_APIC_MAXLVT(v);
81 return maxlvt;
82}
83
Andi Kleen3777a952006-02-03 21:51:53 +010084/*
85 * 'what should we do if we get a hw irq event on an illegal vector'.
86 * each architecture has to answer this themselves.
87 */
88void ack_bad_irq(unsigned int irq)
89{
90 printk("unexpected IRQ trap at vector %02x\n", irq);
91 /*
92 * Currently unexpected vectors happen only on SMP and APIC.
93 * We _must_ ack these because every local APIC has only N
94 * irq slots per priority level, and a 'hanging, unacked' IRQ
95 * holds up an irq slot - in excessive cases (when multiple
96 * unexpected vectors occur) that might lock up the APIC
97 * completely.
98 * But don't ack when the APIC is disabled. -AK
99 */
100 if (!disable_apic)
101 ack_APIC_irq();
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104void clear_local_APIC(void)
105{
106 int maxlvt;
107 unsigned int v;
108
109 maxlvt = get_maxlvt();
110
111 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200112 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * if the vector is zero. Mask LVTERR first to prevent this.
114 */
115 if (maxlvt >= 3) {
116 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100117 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 /*
120 * Careful: we have to set masks only first to deassert
121 * any level-triggered sources.
122 */
123 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100124 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100126 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100128 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (maxlvt >= 4) {
130 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100131 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
134 /*
135 * Clean APIC state for other OSs:
136 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100137 apic_write(APIC_LVTT, APIC_LVT_MASKED);
138 apic_write(APIC_LVT0, APIC_LVT_MASKED);
139 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100141 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100143 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200144 apic_write(APIC_ESR, 0);
145 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Eric W. Biederman208fb932005-06-25 14:57:45 -0700148void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200150 /* Go back to Virtual Wire compatibility mode */
151 unsigned long value;
152
153 /* For the spurious interrupt use vector F, and enable it */
154 value = apic_read(APIC_SPIV);
155 value &= ~APIC_VECTOR_MASK;
156 value |= APIC_SPIV_APIC_ENABLED;
157 value |= 0xf;
158 apic_write(APIC_SPIV, value);
159
160 if (!virt_wire_setup) {
161 /* For LVT0 make it edge triggered, active high, external and enabled */
162 value = apic_read(APIC_LVT0);
163 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
164 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
165 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
166 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
167 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
168 apic_write(APIC_LVT0, value);
169 } else {
170 /* Disable LVT0 */
171 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700173
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200174 /* For LVT1 make it edge triggered, active high, nmi and enabled */
175 value = apic_read(APIC_LVT1);
176 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700177 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
178 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200179 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
180 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
181 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184void disable_local_APIC(void)
185{
186 unsigned int value;
187
188 clear_local_APIC();
189
190 /*
191 * Disable APIC (implies clearing of registers
192 * for 82489DX!).
193 */
194 value = apic_read(APIC_SPIV);
195 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100196 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/*
200 * This is to verify that we're looking at a real local APIC.
201 * Check these against your board if the CPUs aren't getting
202 * started for no apparent reason.
203 */
204int __init verify_local_APIC(void)
205{
206 unsigned int reg0, reg1;
207
208 /*
209 * The version register is read-only in a real APIC.
210 */
211 reg0 = apic_read(APIC_LVR);
212 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
213 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
214 reg1 = apic_read(APIC_LVR);
215 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
216
217 /*
218 * The two version reads above should print the same
219 * numbers. If the second one is different, then we
220 * poke at a non-APIC.
221 */
222 if (reg1 != reg0)
223 return 0;
224
225 /*
226 * Check if the version looks reasonably.
227 */
228 reg1 = GET_APIC_VERSION(reg0);
229 if (reg1 == 0x00 || reg1 == 0xff)
230 return 0;
231 reg1 = get_maxlvt();
232 if (reg1 < 0x02 || reg1 == 0xff)
233 return 0;
234
235 /*
236 * The ID register is read/write in a real APIC.
237 */
238 reg0 = apic_read(APIC_ID);
239 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
240 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
241 reg1 = apic_read(APIC_ID);
242 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
243 apic_write(APIC_ID, reg0);
244 if (reg1 != (reg0 ^ APIC_ID_MASK))
245 return 0;
246
247 /*
248 * The next two are just to see if we have sane values.
249 * They're only really relevant if we're in Virtual Wire
250 * compatibility mode, but most boxes are anymore.
251 */
252 reg0 = apic_read(APIC_LVT0);
253 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
254 reg1 = apic_read(APIC_LVT1);
255 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
256
257 return 1;
258}
259
260void __init sync_Arb_IDs(void)
261{
262 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
263 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
264 if (ver >= 0x14) /* P4 or higher */
265 return;
266
267 /*
268 * Wait for idle.
269 */
270 apic_wait_icr_idle();
271
272 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100273 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 | APIC_DM_INIT);
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * An initial setup of the virtual wire mode.
279 */
280void __init init_bsp_APIC(void)
281{
Andi Kleen11a8e772006-01-11 22:46:51 +0100282 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /*
285 * Don't do the setup now if we have a SMP BIOS as the
286 * through-I/O-APIC virtual wire mode might be active.
287 */
288 if (smp_found_config || !cpu_has_apic)
289 return;
290
291 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /*
294 * Do not trust the local APIC being empty at bootup.
295 */
296 clear_local_APIC();
297
298 /*
299 * Enable APIC.
300 */
301 value = apic_read(APIC_SPIV);
302 value &= ~APIC_VECTOR_MASK;
303 value |= APIC_SPIV_APIC_ENABLED;
304 value |= APIC_SPIV_FOCUS_DISABLED;
305 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100306 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
309 * Set up the virtual wire mode.
310 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100311 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100313 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Ashok Raje6982c62005-06-25 14:54:58 -0700316void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Andi Kleen11a8e772006-01-11 22:46:51 +0100318 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100319 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Andi Kleenfe7414a2006-09-26 10:52:30 +0200323 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /*
326 * Double-check whether this APIC is really registered.
327 * This is meaningless in clustered apic mode, so we skip it.
328 */
329 if (!apic_id_registered())
330 BUG();
331
332 /*
333 * Intel recommends to set DFR, LDR and TPR before enabling
334 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
335 * document number 292116). So here it goes...
336 */
337 init_apic_ldr();
338
339 /*
340 * Set Task Priority to 'accept all'. We never change this
341 * later on.
342 */
343 value = apic_read(APIC_TASKPRI);
344 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100345 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100348 * After a crash, we no longer service the interrupts and a pending
349 * interrupt from previous kernel might still have ISR bit set.
350 *
351 * Most probably by now CPU has serviced that pending interrupt and
352 * it might not have done the ack_APIC_irq() because it thought,
353 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
354 * does not clear the ISR bit and cpu thinks it has already serivced
355 * the interrupt. Hence a vector might get locked. It was noticed
356 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
357 */
358 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
359 value = apic_read(APIC_ISR + i*0x10);
360 for (j = 31; j >= 0; j--) {
361 if (value & (1<<j))
362 ack_APIC_irq();
363 }
364 }
365
366 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * Now that we are all set up, enable the APIC
368 */
369 value = apic_read(APIC_SPIV);
370 value &= ~APIC_VECTOR_MASK;
371 /*
372 * Enable APIC
373 */
374 value |= APIC_SPIV_APIC_ENABLED;
375
Andi Kleen3f14c742006-09-26 10:52:29 +0200376 /* We always use processor focus */
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Set spurious IRQ vector
380 */
381 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100382 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /*
385 * Set up LVT0, LVT1:
386 *
387 * set up through-local-APIC on the BP's LINT0. This is not
388 * strictly necessary in pure symmetric-IO mode, but sometimes
389 * we delegate interrupts to the 8259A.
390 */
391 /*
392 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
393 */
394 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200395 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 value = APIC_DM_EXTINT;
397 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
398 } else {
399 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
400 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
401 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100402 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
405 * only the BP should see the LINT1 NMI signal, obviously.
406 */
407 if (!smp_processor_id())
408 value = APIC_DM_NMI;
409 else
410 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100411 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Andi Kleen61c11342005-09-12 18:49:23 +0200413 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned oldvalue;
415 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 oldvalue = apic_read(APIC_ESR);
417 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100418 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * spec says clear errors after enabling vector.
421 */
422 if (maxlvt > 3)
423 apic_write(APIC_ESR, 0);
424 value = apic_read(APIC_ESR);
425 if (value != oldvalue)
426 apic_printk(APIC_VERBOSE,
427 "ESR value after enabling vector: %08x, after %08x\n",
428 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200432 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 apic_pm_activate();
434}
435
436#ifdef CONFIG_PM
437
438static struct {
439 /* 'active' is true if the local APIC was enabled by us and
440 not the BIOS; this signifies that we are also responsible
441 for disabling it before entering apm/acpi suspend */
442 int active;
443 /* r/w apic fields */
444 unsigned int apic_id;
445 unsigned int apic_taskpri;
446 unsigned int apic_ldr;
447 unsigned int apic_dfr;
448 unsigned int apic_spiv;
449 unsigned int apic_lvtt;
450 unsigned int apic_lvtpc;
451 unsigned int apic_lvt0;
452 unsigned int apic_lvt1;
453 unsigned int apic_lvterr;
454 unsigned int apic_tmict;
455 unsigned int apic_tdcr;
456 unsigned int apic_thmr;
457} apic_pm_state;
458
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700459static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100462 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (!apic_pm_state.active)
465 return 0;
466
Karsten Wiesef990fff2006-12-07 02:14:11 +0100467 maxlvt = get_maxlvt();
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 apic_pm_state.apic_id = apic_read(APIC_ID);
470 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
471 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
472 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
473 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
474 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100475 if (maxlvt >= 4)
476 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
478 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
479 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
480 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
481 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100482#ifdef CONFIG_X86_MCE_INTEL
483 if (maxlvt >= 5)
484 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
485#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200486 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 disable_local_APIC();
488 local_irq_restore(flags);
489 return 0;
490}
491
492static int lapic_resume(struct sys_device *dev)
493{
494 unsigned int l, h;
495 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100496 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (!apic_pm_state.active)
499 return 0;
500
Karsten Wiesef990fff2006-12-07 02:14:11 +0100501 maxlvt = get_maxlvt();
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 local_irq_save(flags);
504 rdmsr(MSR_IA32_APICBASE, l, h);
505 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100506 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 wrmsr(MSR_IA32_APICBASE, l, h);
508 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
509 apic_write(APIC_ID, apic_pm_state.apic_id);
510 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
511 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
512 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
513 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
514 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
515 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100516#ifdef CONFIG_X86_MCE_INTEL
517 if (maxlvt >= 5)
518 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
519#endif
520 if (maxlvt >= 4)
521 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
523 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
524 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
525 apic_write(APIC_ESR, 0);
526 apic_read(APIC_ESR);
527 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
528 apic_write(APIC_ESR, 0);
529 apic_read(APIC_ESR);
530 local_irq_restore(flags);
531 return 0;
532}
533
534static struct sysdev_class lapic_sysclass = {
535 set_kset_name("lapic"),
536 .resume = lapic_resume,
537 .suspend = lapic_suspend,
538};
539
540static struct sys_device device_lapic = {
541 .id = 0,
542 .cls = &lapic_sysclass,
543};
544
Ashok Raje6982c62005-06-25 14:54:58 -0700545static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 apic_pm_state.active = 1;
548}
549
550static int __init init_lapic_sysfs(void)
551{
552 int error;
553 if (!cpu_has_apic)
554 return 0;
555 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
556 error = sysdev_class_register(&lapic_sysclass);
557 if (!error)
558 error = sysdev_register(&device_lapic);
559 return error;
560}
561device_initcall(init_lapic_sysfs);
562
563#else /* CONFIG_PM */
564
565static void apic_pm_activate(void) { }
566
567#endif /* CONFIG_PM */
568
569static int __init apic_set_verbosity(char *str)
570{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200571 if (str == NULL) {
572 skip_ioapic_setup = 0;
573 ioapic_force = 1;
574 return 0;
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (strcmp("debug", str) == 0)
577 apic_verbosity = APIC_DEBUG;
578 else if (strcmp("verbose", str) == 0)
579 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200580 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200582 " use apic=verbose or apic=debug\n", str);
583 return -EINVAL;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200588early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590/*
591 * Detect and enable local APICs on non-SMP boards.
592 * Original code written by Keir Fraser.
593 * On AMD64 we trust the BIOS - if it says no APIC it is likely
594 * not correctly set up (usually the APIC timer won't work etc.)
595 */
596
597static int __init detect_init_APIC (void)
598{
599 if (!cpu_has_apic) {
600 printk(KERN_INFO "No local APIC present\n");
601 return -1;
602 }
603
604 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
605 boot_cpu_id = 0;
606 return 0;
607}
608
Aaron Durbin39928722006-12-07 02:14:01 +0100609#ifdef CONFIG_X86_IO_APIC
610static struct resource * __init ioapic_setup_resources(void)
611{
612#define IOAPIC_RESOURCE_NAME_SIZE 11
613 unsigned long n;
614 struct resource *res;
615 char *mem;
616 int i;
617
618 if (nr_ioapics <= 0)
619 return NULL;
620
621 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
622 n *= nr_ioapics;
623
624 mem = alloc_bootmem(n);
625 res = (void *)mem;
626
627 if (mem != NULL) {
628 memset(mem, 0, n);
629 mem += sizeof(struct resource) * nr_ioapics;
630
631 for (i = 0; i < nr_ioapics; i++) {
632 res[i].name = mem;
633 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
634 sprintf(mem, "IOAPIC %u", i);
635 mem += IOAPIC_RESOURCE_NAME_SIZE;
636 }
637 }
638
639 ioapic_resources = res;
640
641 return res;
642}
643
644static int __init ioapic_insert_resources(void)
645{
646 int i;
647 struct resource *r = ioapic_resources;
648
649 if (!r) {
650 printk("IO APIC resources could be not be allocated.\n");
651 return -1;
652 }
653
654 for (i = 0; i < nr_ioapics; i++) {
655 insert_resource(&iomem_resource, r);
656 r++;
657 }
658
659 return 0;
660}
661
662/* Insert the IO APIC resources after PCI initialization has occured to handle
663 * IO APICS that are mapped in on a BAR in PCI space. */
664late_initcall(ioapic_insert_resources);
665#endif
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667void __init init_apic_mappings(void)
668{
669 unsigned long apic_phys;
670
671 /*
672 * If no local APIC can be found then set up a fake all
673 * zeroes page to simulate the local APIC and another
674 * one for the IO-APIC.
675 */
676 if (!smp_found_config && detect_init_APIC()) {
677 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
678 apic_phys = __pa(apic_phys);
679 } else
680 apic_phys = mp_lapic_addr;
681
682 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Andi Kleenb7f5e3c2006-09-26 10:52:34 +0200683 apic_mapped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
685
Aaron Durbin39928722006-12-07 02:14:01 +0100686 /* Put local APIC into the resource map. */
687 lapic_resource.start = apic_phys;
688 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
689 insert_resource(&iomem_resource, &lapic_resource);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Fetch the APIC ID of the BSP in case we have a
693 * default configuration (or the MP table is broken).
694 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200695 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 {
698 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
699 int i;
Aaron Durbin39928722006-12-07 02:14:01 +0100700 struct resource *ioapic_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Aaron Durbin39928722006-12-07 02:14:01 +0100702 ioapic_res = ioapic_setup_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 for (i = 0; i < nr_ioapics; i++) {
704 if (smp_found_config) {
705 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
706 } else {
707 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
708 ioapic_phys = __pa(ioapic_phys);
709 }
710 set_fixmap_nocache(idx, ioapic_phys);
711 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
712 __fix_to_virt(idx), ioapic_phys);
713 idx++;
Aaron Durbin39928722006-12-07 02:14:01 +0100714
715 if (ioapic_res != NULL) {
716 ioapic_res->start = ioapic_phys;
717 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
718 ioapic_res++;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724/*
725 * This function sets up the local APIC timer, with a timeout of
726 * 'clocks' APIC bus clock. During calibration we actually call
727 * this function twice on the boot CPU, once with a bogus timeout
728 * value, second time for real. The other (noncalibrating) CPUs
729 * call this function only once, with the real, calibrated value.
730 *
731 * We do reads before writes even if unnecessary, to get around the
732 * P5 APIC double write bug.
733 */
734
735#define APIC_DIVISOR 16
736
737static void __setup_APIC_LVTT(unsigned int clocks)
738{
David Rientjes86bd58b2006-12-07 02:14:11 +0100739 unsigned int lvtt_value, tmp_value;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100740 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100743
744 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
745 lvtt_value |= APIC_LVT_MASKED;
746
Andi Kleen11a8e772006-01-11 22:46:51 +0100747 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /*
750 * Divide PICLK by 16
751 */
752 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100753 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
755 | APIC_TDR_DIV_16);
756
Andi Kleen11a8e772006-01-11 22:46:51 +0100757 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760static void setup_APIC_timer(unsigned int clocks)
761{
762 unsigned long flags;
763
764 local_irq_save(flags);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* wait for irq slice */
john stultz2d0c87c2007-02-16 01:28:18 -0800767 if (hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int trigger = hpet_readl(HPET_T0_CMP);
769 while (hpet_readl(HPET_COUNTER) >= trigger)
770 /* do nothing */ ;
771 while (hpet_readl(HPET_COUNTER) < trigger)
772 /* do nothing */ ;
773 } else {
774 int c1, c2;
775 outb_p(0x00, 0x43);
776 c2 = inb_p(0x40);
777 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100778 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 c1 = c2;
780 outb_p(0x00, 0x43);
781 c2 = inb_p(0x40);
782 c2 |= inb_p(0x40) << 8;
783 } while (c2 - c1 < 300);
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100786 /* Turn off PIT interrupt if we use APIC timer as main timer.
787 Only works with the PM timer right now
788 TBD fix it for HPET too. */
john stultz14899392007-02-16 01:28:20 -0800789 if ((pmtmr_ioport != 0) &&
Andi Kleen73dea472006-02-03 21:50:50 +0100790 smp_processor_id() == boot_cpu_id &&
791 apic_runs_main_timer == 1 &&
792 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
793 stop_timer_interrupt();
794 apic_runs_main_timer++;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 local_irq_restore(flags);
797}
798
799/*
800 * In this function we calibrate APIC bus clocks to the external
801 * timer. Unfortunately we cannot use jiffies and the timer irq
802 * to calibrate, since some later bootup code depends on getting
803 * the first irq? Ugh.
804 *
805 * We want to do the calibration only once since we
806 * want to have local timer irqs syncron. CPUs connected
807 * by the same APIC bus have the very same bus frequency.
808 * And we want to have irqs off anyways, no accidental
809 * APIC irq that way.
810 */
811
812#define TICK_COUNT 100000000
813
814static int __init calibrate_APIC_clock(void)
815{
816 int apic, apic_start, tsc, tsc_start;
817 int result;
818 /*
819 * Put whatever arbitrary (but long enough) timeout
820 * value into the APIC clock, we just want to get the
821 * counter running for calibration.
822 */
823 __setup_APIC_LVTT(1000000000);
824
825 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100826#ifdef CONFIG_X86_PM_TIMER
827 if (apic_calibrate_pmtmr && pmtmr_ioport) {
828 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100830 result = (apic_start - apic) * 1000L / 5;
831 } else
832#endif
833 {
834 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Andi Kleen0c3749c2006-02-03 21:51:41 +0100836 do {
837 apic = apic_read(APIC_TMCCT);
838 rdtscl(tsc);
839 } while ((tsc - tsc_start) < TICK_COUNT &&
840 (apic - apic_start) < TICK_COUNT);
841
842 result = (apic_start - apic) * 1000L * cpu_khz /
843 (tsc - tsc_start);
844 }
845 printk("result %d\n", result);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
849 result / 1000 / 1000, result / 1000 % 1000);
850
851 return result * APIC_DIVISOR / HZ;
852}
853
854static unsigned int calibration_result;
855
856void __init setup_boot_APIC_clock (void)
857{
858 if (disable_apic_timer) {
859 printk(KERN_INFO "Disabling APIC timer\n");
860 return;
861 }
862
863 printk(KERN_INFO "Using local APIC timer interrupts.\n");
864 using_apic_timer = 1;
865
866 local_irq_disable();
867
868 calibration_result = calibrate_APIC_clock();
869 /*
870 * Now set up the timer for real.
871 */
872 setup_APIC_timer(calibration_result);
873
874 local_irq_enable();
875}
876
Ashok Raje6982c62005-06-25 14:54:58 -0700877void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 local_irq_disable(); /* FIXME: Do we need this? --RR */
880 setup_APIC_timer(calibration_result);
881 local_irq_enable();
882}
883
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100884void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 if (using_apic_timer) {
887 unsigned long v;
888
889 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200890 /*
891 * When an illegal vector value (0-15) is written to an LVT
892 * entry and delivery mode is Fixed, the APIC may signal an
893 * illegal vector error, with out regard to whether the mask
894 * bit is set or whether an interrupt is actually seen on input.
895 *
896 * Boot sequence might call this function when the LVTT has
897 * '0' vector value. So make sure vector field is set to
898 * valid value.
899 */
900 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
901 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903}
904
905void enable_APIC_timer(void)
906{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100907 int cpu = smp_processor_id();
908
909 if (using_apic_timer &&
910 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned long v;
912
913 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100914 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916}
917
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100918void switch_APIC_timer_to_ipi(void *cpumask)
919{
920 cpumask_t mask = *(cpumask_t *)cpumask;
921 int cpu = smp_processor_id();
922
923 if (cpu_isset(cpu, mask) &&
924 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
925 disable_APIC_timer();
926 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
927 }
928}
929EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
930
931void smp_send_timer_broadcast_ipi(void)
932{
933 cpumask_t mask;
934
935 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
936 if (!cpus_empty(mask)) {
937 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
938 }
939}
940
941void switch_ipi_to_APIC_timer(void *cpumask)
942{
943 cpumask_t mask = *(cpumask_t *)cpumask;
944 int cpu = smp_processor_id();
945
946 if (cpu_isset(cpu, mask) &&
947 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
948 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
949 enable_APIC_timer();
950 }
951}
952EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954int setup_profiling_timer(unsigned int multiplier)
955{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Jacob Shin17fc14f2006-06-26 13:58:47 +0200959void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
960 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100961{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200962 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
963 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100964 apic_write(reg, v);
965}
Jacob Shin89b831e2005-11-05 17:25:53 +0100966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967#undef APIC_DIVISOR
968
969/*
970 * Local timer interrupt handler. It does both profiling and
971 * process statistics/rescheduling.
972 *
973 * We do profiling in every local tick, statistics/rescheduling
974 * happen only every 'profiling multiplier' ticks. The default
975 * multiplier is 1 and it can be changed by writing the new multiplier
976 * value into /proc/profile.
977 */
978
David Howells7d12e782006-10-05 14:55:46 +0100979void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
David Howells7d12e782006-10-05 14:55:46 +0100981 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +0100983 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100985 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
David Howells7d12e782006-10-05 14:55:46 +0100986 main_timer_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /*
988 * We take the 'long' return path, and there every subsystem
989 * grabs the appropriate locks (kernel lock/ irq lock).
990 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +0200991 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * and do the profiling totally in assembly.
993 *
994 * Currently this isn't too much of an issue (performance wise),
995 * we can take more than 100K local irqs per second on a 100 MHz P5.
996 */
997}
998
999/*
1000 * Local APIC timer interrupt. This is the most natural way for doing
1001 * local interrupts, but local timer interrupts can be emulated by
1002 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1003 *
1004 * [ if a single-CPU system runs an SMP kernel then we call the local
1005 * interrupt as well. Thus we cannot inline the local irq ... ]
1006 */
Andrew Mortond150ad72006-10-06 13:28:09 -07001007void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Andrew Mortond150ad72006-10-06 13:28:09 -07001009 struct pt_regs *old_regs = set_irq_regs(regs);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /*
1012 * the NMI deadlock-detector uses this.
1013 */
1014 add_pda(apic_timer_irqs, 1);
1015
1016 /*
1017 * NOTE! We'd better ACK the irq immediately,
1018 * because timer handling can be slow.
1019 */
1020 ack_APIC_irq();
1021 /*
1022 * update_process_times() expects us to have done irq_enter().
1023 * Besides, if we don't timer interrupts ignore the global
1024 * interrupt lock, which is the WrongThing (tm) to do.
1025 */
Andi Kleen95833c82006-01-11 22:44:36 +01001026 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +01001028 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 irq_exit();
Andrew Mortond150ad72006-10-06 13:28:09 -07001030 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001034 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 *
1036 * Thus far, the major user of this is IBM's Summit2 series:
1037 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001038 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * multi-chassis. Use available data to take a good guess.
1040 * If in doubt, go HPET.
1041 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001042__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 int i, clusters, zeros;
1045 unsigned id;
1046 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1047
Suresh Siddha376ec33f2005-05-16 21:53:32 -07001048 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 for (i = 0; i < NR_CPUS; i++) {
1051 id = bios_cpu_apicid[i];
1052 if (id != BAD_APICID)
1053 __set_bit(APIC_CLUSTERID(id), clustermap);
1054 }
1055
1056 /* Problem: Partially populated chassis may not have CPUs in some of
1057 * the APIC clusters they have been allocated. Only present CPUs have
1058 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1059 * clusters are allocated sequentially, count zeros only if they are
1060 * bounded by ones.
1061 */
1062 clusters = 0;
1063 zeros = 0;
1064 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1065 if (test_bit(i, clustermap)) {
1066 clusters += 1 + zeros;
1067 zeros = 0;
1068 } else
1069 ++zeros;
1070 }
1071
1072 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001073 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * May have to revisit this when multi-core + hyperthreaded CPUs come
1075 * out, but AFAIK this will work even for them.
1076 */
1077 return (clusters > 2);
1078}
1079
1080/*
1081 * This interrupt should _never_ happen with our APIC/SMP architecture
1082 */
1083asmlinkage void smp_spurious_interrupt(void)
1084{
1085 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001086 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 irq_enter();
1088 /*
1089 * Check if this really is a spurious interrupt and ACK it
1090 * if it is a vectored one. Just in case...
1091 * Spurious interrupts should not be ACKed.
1092 */
1093 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1094 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1095 ack_APIC_irq();
1096
1097#if 0
1098 static unsigned long last_warning;
1099 static unsigned long skipped;
1100
1101 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1102 if (time_before(last_warning+30*HZ,jiffies)) {
1103 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1104 smp_processor_id(), skipped);
1105 last_warning = jiffies;
1106 skipped = 0;
1107 } else {
1108 skipped++;
1109 }
1110#endif
1111 irq_exit();
1112}
1113
1114/*
1115 * This interrupt should never happen with our APIC/SMP architecture
1116 */
1117
1118asmlinkage void smp_error_interrupt(void)
1119{
1120 unsigned int v, v1;
1121
Andi Kleen95833c82006-01-11 22:44:36 +01001122 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 irq_enter();
1124 /* First tickle the hardware, only then report what went on. -- REW */
1125 v = apic_read(APIC_ESR);
1126 apic_write(APIC_ESR, 0);
1127 v1 = apic_read(APIC_ESR);
1128 ack_APIC_irq();
1129 atomic_inc(&irq_err_count);
1130
1131 /* Here is what the APIC error bits mean:
1132 0: Send CS error
1133 1: Receive CS error
1134 2: Send accept error
1135 3: Receive accept error
1136 4: Reserved
1137 5: Send illegal vector
1138 6: Received illegal vector
1139 7: Illegal register address
1140 */
1141 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1142 smp_processor_id(), v , v1);
1143 irq_exit();
1144}
1145
1146int disable_apic;
1147
1148/*
1149 * This initializes the IO-APIC and APIC hardware if this is
1150 * a UP kernel.
1151 */
1152int __init APIC_init_uniprocessor (void)
1153{
1154 if (disable_apic) {
1155 printk(KERN_INFO "Apic disabled\n");
1156 return -1;
1157 }
1158 if (!cpu_has_apic) {
1159 disable_apic = 1;
1160 printk(KERN_INFO "Apic disabled by BIOS\n");
1161 return -1;
1162 }
1163
1164 verify_local_APIC();
1165
Andi Kleen357e11d2005-09-12 18:49:24 +02001166 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001167 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 setup_local_APIC();
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001172 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 else
1174 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001176 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return 0;
1178}
1179
1180static __init int setup_disableapic(char *str)
1181{
1182 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001183 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1184 return 0;
1185}
1186early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001188/* same as disableapic, for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static __init int setup_nolapic(char *str)
1190{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001191 return setup_disableapic(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001193early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195static __init int setup_noapictimer(char *str)
1196{
Andi Kleen73dea472006-02-03 21:50:50 +01001197 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001200 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Andi Kleen73dea472006-02-03 21:50:50 +01001203static __init int setup_apicmaintimer(char *str)
1204{
1205 apic_runs_main_timer = 1;
1206 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001207 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001208}
1209__setup("apicmaintimer", setup_apicmaintimer);
1210
1211static __init int setup_noapicmaintimer(char *str)
1212{
1213 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001214 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001215}
1216__setup("noapicmaintimer", setup_noapicmaintimer);
1217
Andi Kleen0c3749c2006-02-03 21:51:41 +01001218static __init int setup_apicpmtimer(char *s)
1219{
1220 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001221 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001222 return setup_apicmaintimer(NULL);
1223}
1224__setup("apicpmtimer", setup_apicpmtimer);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226__setup("noapictimer", setup_noapictimer);
1227