blob: ce2f8015d5ce376cfb38cef79a1a53dc3fd437b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010026#include <linux/module.h>
Aaron Durbin39928722006-12-07 02:14:01 +010027#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/atomic.h>
30#include <asm/smp.h>
31#include <asm/mtrr.h>
32#include <asm/mpspec.h>
33#include <asm/pgalloc.h>
34#include <asm/mach_apic.h>
Andi Kleen75152112005-05-16 21:53:34 -070035#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010036#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010037#include <asm/proto.h>
38#include <asm/timex.h>
john stultz2d0c87c2007-02-16 01:28:18 -080039#include <asm/hpet.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020040#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010043int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010044int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46int disable_apic_timer __initdata;
47
Linus Torvalds2e7c2832007-03-23 11:32:31 -070048/* Local APIC timer works in C2? */
49int local_apic_timer_c2_ok;
50EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
51
Aaron Durbin39928722006-12-07 02:14:01 +010052static struct resource *ioapic_resources;
53static struct resource lapic_resource = {
54 .name = "Local APIC",
55 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
56};
57
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010058/*
59 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
60 * IPIs in place of local APIC timers
61 */
62static cpumask_t timer_interrupt_broadcast_ipi_mask;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070065int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void apic_pm_activate(void);
68
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +020069void apic_wait_icr_idle(void)
70{
71 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
72 cpu_relax();
73}
74
75unsigned int safe_apic_wait_icr_idle(void)
76{
77 unsigned int send_status;
78 int timeout;
79
80 timeout = 0;
81 do {
82 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
83 if (!send_status)
84 break;
85 udelay(100);
86 } while (timeout++ < 1000);
87
88 return send_status;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091void enable_NMI_through_LVT0 (void * dummy)
92{
Andi Kleen11a8e772006-01-11 22:46:51 +010093 unsigned int v;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +020094
95 /* unmask and set to NMI */
96 v = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +010097 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100int get_maxlvt(void)
101{
Andi Kleen11a8e772006-01-11 22:46:51 +0100102 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 maxlvt = GET_APIC_MAXLVT(v);
106 return maxlvt;
107}
108
Andi Kleen3777a952006-02-03 21:51:53 +0100109/*
110 * 'what should we do if we get a hw irq event on an illegal vector'.
111 * each architecture has to answer this themselves.
112 */
113void ack_bad_irq(unsigned int irq)
114{
115 printk("unexpected IRQ trap at vector %02x\n", irq);
116 /*
117 * Currently unexpected vectors happen only on SMP and APIC.
118 * We _must_ ack these because every local APIC has only N
119 * irq slots per priority level, and a 'hanging, unacked' IRQ
120 * holds up an irq slot - in excessive cases (when multiple
121 * unexpected vectors occur) that might lock up the APIC
122 * completely.
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200123 * But don't ack when the APIC is disabled. -AK
Andi Kleen3777a952006-02-03 21:51:53 +0100124 */
125 if (!disable_apic)
126 ack_APIC_irq();
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void clear_local_APIC(void)
130{
131 int maxlvt;
132 unsigned int v;
133
134 maxlvt = get_maxlvt();
135
136 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200137 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * if the vector is zero. Mask LVTERR first to prevent this.
139 */
140 if (maxlvt >= 3) {
141 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100142 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 /*
145 * Careful: we have to set masks only first to deassert
146 * any level-triggered sources.
147 */
148 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100149 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100151 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100153 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (maxlvt >= 4) {
155 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100156 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159 /*
160 * Clean APIC state for other OSs:
161 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100162 apic_write(APIC_LVTT, APIC_LVT_MASKED);
163 apic_write(APIC_LVT0, APIC_LVT_MASKED);
164 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100166 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100168 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200169 apic_write(APIC_ESR, 0);
170 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Eric W. Biederman208fb932005-06-25 14:57:45 -0700173void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200175 /* Go back to Virtual Wire compatibility mode */
176 unsigned long value;
177
178 /* For the spurious interrupt use vector F, and enable it */
179 value = apic_read(APIC_SPIV);
180 value &= ~APIC_VECTOR_MASK;
181 value |= APIC_SPIV_APIC_ENABLED;
182 value |= 0xf;
183 apic_write(APIC_SPIV, value);
184
185 if (!virt_wire_setup) {
186 /* For LVT0 make it edge triggered, active high, external and enabled */
187 value = apic_read(APIC_LVT0);
188 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
189 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
190 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
191 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
192 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
193 apic_write(APIC_LVT0, value);
194 } else {
195 /* Disable LVT0 */
196 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700198
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200199 /* For LVT1 make it edge triggered, active high, nmi and enabled */
200 value = apic_read(APIC_LVT1);
201 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700202 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
203 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200204 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
205 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
206 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209void disable_local_APIC(void)
210{
211 unsigned int value;
212
213 clear_local_APIC();
214
215 /*
216 * Disable APIC (implies clearing of registers
217 * for 82489DX!).
218 */
219 value = apic_read(APIC_SPIV);
220 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100221 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * This is to verify that we're looking at a real local APIC.
226 * Check these against your board if the CPUs aren't getting
227 * started for no apparent reason.
228 */
229int __init verify_local_APIC(void)
230{
231 unsigned int reg0, reg1;
232
233 /*
234 * The version register is read-only in a real APIC.
235 */
236 reg0 = apic_read(APIC_LVR);
237 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
238 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
239 reg1 = apic_read(APIC_LVR);
240 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
241
242 /*
243 * The two version reads above should print the same
244 * numbers. If the second one is different, then we
245 * poke at a non-APIC.
246 */
247 if (reg1 != reg0)
248 return 0;
249
250 /*
251 * Check if the version looks reasonably.
252 */
253 reg1 = GET_APIC_VERSION(reg0);
254 if (reg1 == 0x00 || reg1 == 0xff)
255 return 0;
256 reg1 = get_maxlvt();
257 if (reg1 < 0x02 || reg1 == 0xff)
258 return 0;
259
260 /*
261 * The ID register is read/write in a real APIC.
262 */
263 reg0 = apic_read(APIC_ID);
264 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
265 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
266 reg1 = apic_read(APIC_ID);
267 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
268 apic_write(APIC_ID, reg0);
269 if (reg1 != (reg0 ^ APIC_ID_MASK))
270 return 0;
271
272 /*
273 * The next two are just to see if we have sane values.
274 * They're only really relevant if we're in Virtual Wire
275 * compatibility mode, but most boxes are anymore.
276 */
277 reg0 = apic_read(APIC_LVT0);
278 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
279 reg1 = apic_read(APIC_LVT1);
280 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
281
282 return 1;
283}
284
285void __init sync_Arb_IDs(void)
286{
287 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
288 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
289 if (ver >= 0x14) /* P4 or higher */
290 return;
291
292 /*
293 * Wait for idle.
294 */
295 apic_wait_icr_idle();
296
297 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100298 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 | APIC_DM_INIT);
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * An initial setup of the virtual wire mode.
304 */
305void __init init_bsp_APIC(void)
306{
Andi Kleen11a8e772006-01-11 22:46:51 +0100307 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /*
310 * Don't do the setup now if we have a SMP BIOS as the
311 * through-I/O-APIC virtual wire mode might be active.
312 */
313 if (smp_found_config || !cpu_has_apic)
314 return;
315
316 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
319 * Do not trust the local APIC being empty at bootup.
320 */
321 clear_local_APIC();
322
323 /*
324 * Enable APIC.
325 */
326 value = apic_read(APIC_SPIV);
327 value &= ~APIC_VECTOR_MASK;
328 value |= APIC_SPIV_APIC_ENABLED;
329 value |= APIC_SPIV_FOCUS_DISABLED;
330 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100331 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
334 * Set up the virtual wire mode.
335 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100336 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100338 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Ashok Raje6982c62005-06-25 14:54:58 -0700341void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Andi Kleen11a8e772006-01-11 22:46:51 +0100343 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100344 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Andi Kleenfe7414a2006-09-26 10:52:30 +0200348 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /*
351 * Double-check whether this APIC is really registered.
352 * This is meaningless in clustered apic mode, so we skip it.
353 */
354 if (!apic_id_registered())
355 BUG();
356
357 /*
358 * Intel recommends to set DFR, LDR and TPR before enabling
359 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
360 * document number 292116). So here it goes...
361 */
362 init_apic_ldr();
363
364 /*
365 * Set Task Priority to 'accept all'. We never change this
366 * later on.
367 */
368 value = apic_read(APIC_TASKPRI);
369 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100370 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100373 * After a crash, we no longer service the interrupts and a pending
374 * interrupt from previous kernel might still have ISR bit set.
375 *
376 * Most probably by now CPU has serviced that pending interrupt and
377 * it might not have done the ack_APIC_irq() because it thought,
378 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
379 * does not clear the ISR bit and cpu thinks it has already serivced
380 * the interrupt. Hence a vector might get locked. It was noticed
381 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
382 */
383 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
384 value = apic_read(APIC_ISR + i*0x10);
385 for (j = 31; j >= 0; j--) {
386 if (value & (1<<j))
387 ack_APIC_irq();
388 }
389 }
390
391 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * Now that we are all set up, enable the APIC
393 */
394 value = apic_read(APIC_SPIV);
395 value &= ~APIC_VECTOR_MASK;
396 /*
397 * Enable APIC
398 */
399 value |= APIC_SPIV_APIC_ENABLED;
400
Andi Kleen3f14c742006-09-26 10:52:29 +0200401 /* We always use processor focus */
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * Set spurious IRQ vector
405 */
406 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100407 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * Set up LVT0, LVT1:
411 *
412 * set up through-local-APIC on the BP's LINT0. This is not
413 * strictly necessary in pure symmetric-IO mode, but sometimes
414 * we delegate interrupts to the 8259A.
415 */
416 /*
417 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
418 */
419 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200420 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 value = APIC_DM_EXTINT;
422 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
423 } else {
424 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
425 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
426 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100427 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * only the BP should see the LINT1 NMI signal, obviously.
431 */
432 if (!smp_processor_id())
433 value = APIC_DM_NMI;
434 else
435 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100436 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Andi Kleen61c11342005-09-12 18:49:23 +0200438 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unsigned oldvalue;
440 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 oldvalue = apic_read(APIC_ESR);
442 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100443 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 * spec says clear errors after enabling vector.
446 */
447 if (maxlvt > 3)
448 apic_write(APIC_ESR, 0);
449 value = apic_read(APIC_ESR);
450 if (value != oldvalue)
451 apic_printk(APIC_VERBOSE,
452 "ESR value after enabling vector: %08x, after %08x\n",
453 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200457 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 apic_pm_activate();
459}
460
461#ifdef CONFIG_PM
462
463static struct {
464 /* 'active' is true if the local APIC was enabled by us and
465 not the BIOS; this signifies that we are also responsible
466 for disabling it before entering apm/acpi suspend */
467 int active;
468 /* r/w apic fields */
469 unsigned int apic_id;
470 unsigned int apic_taskpri;
471 unsigned int apic_ldr;
472 unsigned int apic_dfr;
473 unsigned int apic_spiv;
474 unsigned int apic_lvtt;
475 unsigned int apic_lvtpc;
476 unsigned int apic_lvt0;
477 unsigned int apic_lvt1;
478 unsigned int apic_lvterr;
479 unsigned int apic_tmict;
480 unsigned int apic_tdcr;
481 unsigned int apic_thmr;
482} apic_pm_state;
483
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700484static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100487 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (!apic_pm_state.active)
490 return 0;
491
Karsten Wiesef990fff2006-12-07 02:14:11 +0100492 maxlvt = get_maxlvt();
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 apic_pm_state.apic_id = apic_read(APIC_ID);
495 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
496 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
497 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
498 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
499 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100500 if (maxlvt >= 4)
501 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
503 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
504 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
505 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
506 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100507#ifdef CONFIG_X86_MCE_INTEL
508 if (maxlvt >= 5)
509 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
510#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +0200511 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 disable_local_APIC();
513 local_irq_restore(flags);
514 return 0;
515}
516
517static int lapic_resume(struct sys_device *dev)
518{
519 unsigned int l, h;
520 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +0100521 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (!apic_pm_state.active)
524 return 0;
525
Karsten Wiesef990fff2006-12-07 02:14:11 +0100526 maxlvt = get_maxlvt();
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 local_irq_save(flags);
529 rdmsr(MSR_IA32_APICBASE, l, h);
530 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100531 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 wrmsr(MSR_IA32_APICBASE, l, h);
533 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
534 apic_write(APIC_ID, apic_pm_state.apic_id);
535 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
536 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
537 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
538 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
539 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
540 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +0100541#ifdef CONFIG_X86_MCE_INTEL
542 if (maxlvt >= 5)
543 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
544#endif
545 if (maxlvt >= 4)
546 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
548 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
549 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
550 apic_write(APIC_ESR, 0);
551 apic_read(APIC_ESR);
552 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
553 apic_write(APIC_ESR, 0);
554 apic_read(APIC_ESR);
555 local_irq_restore(flags);
556 return 0;
557}
558
559static struct sysdev_class lapic_sysclass = {
560 set_kset_name("lapic"),
561 .resume = lapic_resume,
562 .suspend = lapic_suspend,
563};
564
565static struct sys_device device_lapic = {
566 .id = 0,
567 .cls = &lapic_sysclass,
568};
569
Ashok Raje6982c62005-06-25 14:54:58 -0700570static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 apic_pm_state.active = 1;
573}
574
575static int __init init_lapic_sysfs(void)
576{
577 int error;
578 if (!cpu_has_apic)
579 return 0;
580 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
581 error = sysdev_class_register(&lapic_sysclass);
582 if (!error)
583 error = sysdev_register(&device_lapic);
584 return error;
585}
586device_initcall(init_lapic_sysfs);
587
588#else /* CONFIG_PM */
589
590static void apic_pm_activate(void) { }
591
592#endif /* CONFIG_PM */
593
594static int __init apic_set_verbosity(char *str)
595{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200596 if (str == NULL) {
597 skip_ioapic_setup = 0;
598 ioapic_force = 1;
599 return 0;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (strcmp("debug", str) == 0)
602 apic_verbosity = APIC_DEBUG;
603 else if (strcmp("verbose", str) == 0)
604 apic_verbosity = APIC_VERBOSE;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200605 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200607 " use apic=verbose or apic=debug\n", str);
608 return -EINVAL;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200613early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/*
616 * Detect and enable local APICs on non-SMP boards.
617 * Original code written by Keir Fraser.
618 * On AMD64 we trust the BIOS - if it says no APIC it is likely
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200619 * not correctly set up (usually the APIC timer won't work etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
621
622static int __init detect_init_APIC (void)
623{
624 if (!cpu_has_apic) {
625 printk(KERN_INFO "No local APIC present\n");
626 return -1;
627 }
628
629 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
630 boot_cpu_id = 0;
631 return 0;
632}
633
Aaron Durbin39928722006-12-07 02:14:01 +0100634#ifdef CONFIG_X86_IO_APIC
635static struct resource * __init ioapic_setup_resources(void)
636{
637#define IOAPIC_RESOURCE_NAME_SIZE 11
638 unsigned long n;
639 struct resource *res;
640 char *mem;
641 int i;
642
643 if (nr_ioapics <= 0)
644 return NULL;
645
646 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
647 n *= nr_ioapics;
648
649 mem = alloc_bootmem(n);
650 res = (void *)mem;
651
652 if (mem != NULL) {
653 memset(mem, 0, n);
654 mem += sizeof(struct resource) * nr_ioapics;
655
656 for (i = 0; i < nr_ioapics; i++) {
657 res[i].name = mem;
658 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
659 sprintf(mem, "IOAPIC %u", i);
660 mem += IOAPIC_RESOURCE_NAME_SIZE;
661 }
662 }
663
664 ioapic_resources = res;
665
666 return res;
667}
668
669static int __init ioapic_insert_resources(void)
670{
671 int i;
672 struct resource *r = ioapic_resources;
673
674 if (!r) {
675 printk("IO APIC resources could be not be allocated.\n");
676 return -1;
677 }
678
679 for (i = 0; i < nr_ioapics; i++) {
680 insert_resource(&iomem_resource, r);
681 r++;
682 }
683
684 return 0;
685}
686
687/* Insert the IO APIC resources after PCI initialization has occured to handle
688 * IO APICS that are mapped in on a BAR in PCI space. */
689late_initcall(ioapic_insert_resources);
690#endif
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692void __init init_apic_mappings(void)
693{
694 unsigned long apic_phys;
695
696 /*
697 * If no local APIC can be found then set up a fake all
698 * zeroes page to simulate the local APIC and another
699 * one for the IO-APIC.
700 */
701 if (!smp_found_config && detect_init_APIC()) {
702 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
703 apic_phys = __pa(apic_phys);
704 } else
705 apic_phys = mp_lapic_addr;
706
707 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu7ffeeb12007-10-12 23:04:06 +0200708 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
709 APIC_BASE, apic_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Aaron Durbin39928722006-12-07 02:14:01 +0100711 /* Put local APIC into the resource map. */
712 lapic_resource.start = apic_phys;
713 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
714 insert_resource(&iomem_resource, &lapic_resource);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * Fetch the APIC ID of the BSP in case we have a
718 * default configuration (or the MP table is broken).
719 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200720 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 {
723 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
724 int i;
Aaron Durbin39928722006-12-07 02:14:01 +0100725 struct resource *ioapic_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Aaron Durbin39928722006-12-07 02:14:01 +0100727 ioapic_res = ioapic_setup_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 for (i = 0; i < nr_ioapics; i++) {
729 if (smp_found_config) {
730 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
731 } else {
732 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
733 ioapic_phys = __pa(ioapic_phys);
734 }
735 set_fixmap_nocache(idx, ioapic_phys);
736 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
737 __fix_to_virt(idx), ioapic_phys);
738 idx++;
Aaron Durbin39928722006-12-07 02:14:01 +0100739
740 if (ioapic_res != NULL) {
741 ioapic_res->start = ioapic_phys;
742 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
743 ioapic_res++;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * This function sets up the local APIC timer, with a timeout of
751 * 'clocks' APIC bus clock. During calibration we actually call
752 * this function twice on the boot CPU, once with a bogus timeout
753 * value, second time for real. The other (noncalibrating) CPUs
754 * call this function only once, with the real, calibrated value.
755 *
756 * We do reads before writes even if unnecessary, to get around the
757 * P5 APIC double write bug.
758 */
759
760#define APIC_DIVISOR 16
761
Thomas Gleixner80174092007-10-12 23:04:06 +0200762static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
David Rientjes86bd58b2006-12-07 02:14:11 +0100764 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Thomas Gleixner80174092007-10-12 23:04:06 +0200766 lvtt_value = LOCAL_TIMER_VECTOR;
767 if (!oneshot)
768 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
769 if (!irqen)
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100770 lvtt_value |= APIC_LVT_MASKED;
771
Andi Kleen11a8e772006-01-11 22:46:51 +0100772 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /*
775 * Divide PICLK by 16
776 */
777 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100778 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
780 | APIC_TDR_DIV_16);
781
Thomas Gleixner80174092007-10-12 23:04:06 +0200782 if (!oneshot)
783 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786static void setup_APIC_timer(unsigned int clocks)
787{
788 unsigned long flags;
Thomas Gleixner80174092007-10-12 23:04:06 +0200789 int irqen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 local_irq_save(flags);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* wait for irq slice */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200794 if (hpet_address && hpet_use_timer) {
Robin Holtb291aa72007-08-10 13:00:43 -0700795 u32 trigger = hpet_readl(HPET_T0_CMP);
796 while (hpet_readl(HPET_T0_CMP) == trigger)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200797 /* do nothing */ ;
798 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int c1, c2;
800 outb_p(0x00, 0x43);
801 c2 = inb_p(0x40);
802 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100803 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 c1 = c2;
805 outb_p(0x00, 0x43);
806 c2 = inb_p(0x40);
807 c2 |= inb_p(0x40) << 8;
808 } while (c2 - c1 < 300);
809 }
Thomas Gleixner80174092007-10-12 23:04:06 +0200810
811 irqen = ! cpu_isset(smp_processor_id(),
812 timer_interrupt_broadcast_ipi_mask);
813 __setup_APIC_LVTT(clocks, 0, irqen);
Andi Kleen73dea472006-02-03 21:50:50 +0100814 /* Turn off PIT interrupt if we use APIC timer as main timer.
815 Only works with the PM timer right now
816 TBD fix it for HPET too. */
john stultz14899392007-02-16 01:28:20 -0800817 if ((pmtmr_ioport != 0) &&
Andi Kleen73dea472006-02-03 21:50:50 +0100818 smp_processor_id() == boot_cpu_id &&
819 apic_runs_main_timer == 1 &&
820 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
821 stop_timer_interrupt();
822 apic_runs_main_timer++;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 local_irq_restore(flags);
825}
826
827/*
828 * In this function we calibrate APIC bus clocks to the external
829 * timer. Unfortunately we cannot use jiffies and the timer irq
830 * to calibrate, since some later bootup code depends on getting
831 * the first irq? Ugh.
832 *
833 * We want to do the calibration only once since we
834 * want to have local timer irqs syncron. CPUs connected
835 * by the same APIC bus have the very same bus frequency.
836 * And we want to have irqs off anyways, no accidental
837 * APIC irq that way.
838 */
839
840#define TICK_COUNT 100000000
841
842static int __init calibrate_APIC_clock(void)
843{
David P. Reed4637a742007-05-02 19:27:20 +0200844 unsigned apic, apic_start;
845 unsigned long tsc, tsc_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int result;
847 /*
848 * Put whatever arbitrary (but long enough) timeout
849 * value into the APIC clock, we just want to get the
850 * counter running for calibration.
Thomas Gleixner80174092007-10-12 23:04:06 +0200851 *
852 * No interrupt enable !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
Thomas Gleixner80174092007-10-12 23:04:06 +0200854 __setup_APIC_LVTT(4000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100857#ifdef CONFIG_X86_PM_TIMER
858 if (apic_calibrate_pmtmr && pmtmr_ioport) {
859 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100861 result = (apic_start - apic) * 1000L / 5;
862 } else
863#endif
864 {
David P. Reed4637a742007-05-02 19:27:20 +0200865 rdtscll(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Andi Kleen0c3749c2006-02-03 21:51:41 +0100867 do {
868 apic = apic_read(APIC_TMCCT);
David P. Reed4637a742007-05-02 19:27:20 +0200869 rdtscll(tsc);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100870 } while ((tsc - tsc_start) < TICK_COUNT &&
David P. Reed4637a742007-05-02 19:27:20 +0200871 (apic_start - apic) < TICK_COUNT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100872
Joerg Roedel6b37f5a2007-05-02 19:27:06 +0200873 result = (apic_start - apic) * 1000L * tsc_khz /
Andi Kleen0c3749c2006-02-03 21:51:41 +0100874 (tsc - tsc_start);
875 }
876 printk("result %d\n", result);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
880 result / 1000 / 1000, result / 1000 % 1000);
881
882 return result * APIC_DIVISOR / HZ;
883}
884
885static unsigned int calibration_result;
886
887void __init setup_boot_APIC_clock (void)
888{
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200889 if (disable_apic_timer) {
890 printk(KERN_INFO "Disabling APIC timer\n");
891 return;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 printk(KERN_INFO "Using local APIC timer interrupts.\n");
895 using_apic_timer = 1;
896
897 local_irq_disable();
898
899 calibration_result = calibrate_APIC_clock();
900 /*
901 * Now set up the timer for real.
902 */
903 setup_APIC_timer(calibration_result);
904
905 local_irq_enable();
906}
907
Ashok Raje6982c62005-06-25 14:54:58 -0700908void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 local_irq_disable(); /* FIXME: Do we need this? --RR */
911 setup_APIC_timer(calibration_result);
912 local_irq_enable();
913}
914
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100915void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 if (using_apic_timer) {
918 unsigned long v;
919
920 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200921 /*
922 * When an illegal vector value (0-15) is written to an LVT
923 * entry and delivery mode is Fixed, the APIC may signal an
924 * illegal vector error, with out regard to whether the mask
925 * bit is set or whether an interrupt is actually seen on input.
926 *
927 * Boot sequence might call this function when the LVTT has
928 * '0' vector value. So make sure vector field is set to
929 * valid value.
930 */
931 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
932 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934}
935
936void enable_APIC_timer(void)
937{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100938 int cpu = smp_processor_id();
939
940 if (using_apic_timer &&
941 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 unsigned long v;
943
944 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100945 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947}
948
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100949void switch_APIC_timer_to_ipi(void *cpumask)
950{
951 cpumask_t mask = *(cpumask_t *)cpumask;
952 int cpu = smp_processor_id();
953
954 if (cpu_isset(cpu, mask) &&
955 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
956 disable_APIC_timer();
957 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
958 }
959}
960EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
961
962void smp_send_timer_broadcast_ipi(void)
963{
Thomas Gleixnerf33bc552007-03-23 17:14:37 -0700964 int cpu = smp_processor_id();
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100965 cpumask_t mask;
966
967 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
Thomas Gleixnerf33bc552007-03-23 17:14:37 -0700968
969 if (cpu_isset(cpu, mask)) {
970 cpu_clear(cpu, mask);
971 add_pda(apic_timer_irqs, 1);
972 smp_local_timer_interrupt();
973 }
974
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100975 if (!cpus_empty(mask)) {
976 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
977 }
978}
979
980void switch_ipi_to_APIC_timer(void *cpumask)
981{
982 cpumask_t mask = *(cpumask_t *)cpumask;
983 int cpu = smp_processor_id();
984
985 if (cpu_isset(cpu, mask) &&
986 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
987 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
988 enable_APIC_timer();
989 }
990}
991EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993int setup_profiling_timer(unsigned int multiplier)
994{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Thomas Gleixnerf40f31b2007-07-21 17:10:14 +0200998void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
999 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +01001000{
Jacob Shin17fc14f2006-06-26 13:58:47 +02001001 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
1002 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +01001003 apic_write(reg, v);
1004}
Jacob Shin89b831e2005-11-05 17:25:53 +01001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#undef APIC_DIVISOR
1007
1008/*
1009 * Local timer interrupt handler. It does both profiling and
1010 * process statistics/rescheduling.
1011 *
1012 * We do profiling in every local tick, statistics/rescheduling
1013 * happen only every 'profiling multiplier' ticks. The default
1014 * multiplier is 1 and it can be changed by writing the new multiplier
1015 * value into /proc/profile.
1016 */
1017
David Howells7d12e782006-10-05 14:55:46 +01001018void smp_local_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
David Howells7d12e782006-10-05 14:55:46 +01001020 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +01001022 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023#endif
Andi Kleen73dea472006-02-03 21:50:50 +01001024 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
David Howells7d12e782006-10-05 14:55:46 +01001025 main_timer_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * We take the 'long' return path, and there every subsystem
1028 * grabs the appropriate locks (kernel lock/ irq lock).
1029 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +02001030 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * and do the profiling totally in assembly.
1032 *
1033 * Currently this isn't too much of an issue (performance wise),
1034 * we can take more than 100K local irqs per second on a 100 MHz P5.
1035 */
1036}
1037
1038/*
1039 * Local APIC timer interrupt. This is the most natural way for doing
1040 * local interrupts, but local timer interrupts can be emulated by
1041 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1042 *
1043 * [ if a single-CPU system runs an SMP kernel then we call the local
1044 * interrupt as well. Thus we cannot inline the local irq ... ]
1045 */
Andrew Mortond150ad72006-10-06 13:28:09 -07001046void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Andrew Mortond150ad72006-10-06 13:28:09 -07001048 struct pt_regs *old_regs = set_irq_regs(regs);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * the NMI deadlock-detector uses this.
1052 */
1053 add_pda(apic_timer_irqs, 1);
1054
1055 /*
1056 * NOTE! We'd better ACK the irq immediately,
1057 * because timer handling can be slow.
1058 */
1059 ack_APIC_irq();
1060 /*
1061 * update_process_times() expects us to have done irq_enter().
1062 * Besides, if we don't timer interrupts ignore the global
1063 * interrupt lock, which is the WrongThing (tm) to do.
1064 */
Andi Kleen95833c82006-01-11 22:44:36 +01001065 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 irq_enter();
David Howells7d12e782006-10-05 14:55:46 +01001067 smp_local_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 irq_exit();
Andrew Mortond150ad72006-10-06 13:28:09 -07001069 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001073 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
1075 * Thus far, the major user of this is IBM's Summit2 series:
1076 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001077 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * multi-chassis. Use available data to take a good guess.
1079 * If in doubt, go HPET.
1080 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001081__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 int i, clusters, zeros;
1084 unsigned id;
1085 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1086
Suresh Siddha376ec332005-05-16 21:53:32 -07001087 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 for (i = 0; i < NR_CPUS; i++) {
1090 id = bios_cpu_apicid[i];
1091 if (id != BAD_APICID)
1092 __set_bit(APIC_CLUSTERID(id), clustermap);
1093 }
1094
1095 /* Problem: Partially populated chassis may not have CPUs in some of
1096 * the APIC clusters they have been allocated. Only present CPUs have
1097 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1098 * clusters are allocated sequentially, count zeros only if they are
1099 * bounded by ones.
1100 */
1101 clusters = 0;
1102 zeros = 0;
1103 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1104 if (test_bit(i, clustermap)) {
1105 clusters += 1 + zeros;
1106 zeros = 0;
1107 } else
1108 ++zeros;
1109 }
1110
1111 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001112 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * May have to revisit this when multi-core + hyperthreaded CPUs come
1114 * out, but AFAIK this will work even for them.
1115 */
1116 return (clusters > 2);
1117}
1118
1119/*
1120 * This interrupt should _never_ happen with our APIC/SMP architecture
1121 */
1122asmlinkage void smp_spurious_interrupt(void)
1123{
1124 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001125 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 irq_enter();
1127 /*
1128 * Check if this really is a spurious interrupt and ACK it
1129 * if it is a vectored one. Just in case...
1130 * Spurious interrupts should not be ACKed.
1131 */
1132 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1133 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1134 ack_APIC_irq();
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 irq_exit();
1137}
1138
1139/*
1140 * This interrupt should never happen with our APIC/SMP architecture
1141 */
1142
1143asmlinkage void smp_error_interrupt(void)
1144{
1145 unsigned int v, v1;
1146
Andi Kleen95833c82006-01-11 22:44:36 +01001147 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 irq_enter();
1149 /* First tickle the hardware, only then report what went on. -- REW */
1150 v = apic_read(APIC_ESR);
1151 apic_write(APIC_ESR, 0);
1152 v1 = apic_read(APIC_ESR);
1153 ack_APIC_irq();
1154 atomic_inc(&irq_err_count);
1155
1156 /* Here is what the APIC error bits mean:
1157 0: Send CS error
1158 1: Receive CS error
1159 2: Send accept error
1160 3: Receive accept error
1161 4: Reserved
1162 5: Send illegal vector
1163 6: Received illegal vector
1164 7: Illegal register address
1165 */
1166 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001167 smp_processor_id(), v , v1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 irq_exit();
1169}
1170
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001171int disable_apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173/*
1174 * This initializes the IO-APIC and APIC hardware if this is
1175 * a UP kernel.
1176 */
1177int __init APIC_init_uniprocessor (void)
1178{
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001179 if (disable_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 printk(KERN_INFO "Apic disabled\n");
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001181 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001183 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 disable_apic = 1;
1185 printk(KERN_INFO "Apic disabled by BIOS\n");
1186 return -1;
1187 }
1188
1189 verify_local_APIC();
1190
Andi Kleen357e11d2005-09-12 18:49:24 +02001191 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001192 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 setup_local_APIC();
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001197 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 else
1199 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001201 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001205static __init int setup_disableapic(char *str)
1206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 disable_apic = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001208 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1209 return 0;
1210}
1211early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001213/* same as disableapic, for compatibility */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001214static __init int setup_nolapic(char *str)
1215{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001216 return setup_disableapic(str);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001217}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001218early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001220static int __init parse_lapic_timer_c2_ok(char *arg)
1221{
1222 local_apic_timer_c2_ok = 1;
1223 return 0;
1224}
1225early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1226
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001227static __init int setup_noapictimer(char *str)
1228{
Andi Kleen73dea472006-02-03 21:50:50 +01001229 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001232 return 1;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Andi Kleen73dea472006-02-03 21:50:50 +01001235static __init int setup_apicmaintimer(char *str)
1236{
1237 apic_runs_main_timer = 1;
1238 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001239 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001240}
1241__setup("apicmaintimer", setup_apicmaintimer);
1242
1243static __init int setup_noapicmaintimer(char *str)
1244{
1245 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001246 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001247}
1248__setup("noapicmaintimer", setup_noapicmaintimer);
1249
Andi Kleen0c3749c2006-02-03 21:51:41 +01001250static __init int setup_apicpmtimer(char *s)
1251{
1252 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001253 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001254 return setup_apicmaintimer(NULL);
1255}
1256__setup("apicpmtimer", setup_apicpmtimer);
1257
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001258__setup("noapictimer", setup_noapictimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259