blob: a969ef78e12a0842329cc05e9d6877b1fc34cdbc [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>
Aaron Durbin39928722006-12-07 02:14:01 +010026#include <linux/ioport.h>
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020027#include <linux/clockchips.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010028#include <linux/acpi_pmtmr.h>
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/atomic.h>
32#include <asm/smp.h>
33#include <asm/mtrr.h>
34#include <asm/mpspec.h>
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010035#include <asm/hpet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/pgalloc.h>
Andi Kleen75152112005-05-16 21:53:34 -070037#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010038#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010039#include <asm/proto.h>
40#include <asm/timex.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020041#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Glauber Costa5af55732008-03-25 13:28:56 -030043#include <mach_ipi.h>
Glauber Costadd46e3c2008-03-25 18:10:46 -030044#include <mach_apic.h>
Glauber Costa5af55732008-03-25 13:28:56 -030045
Thomas Gleixneraa276e12008-06-09 19:15:00 +020046static int disable_apic_timer __cpuinitdata;
Chris Wrightbc1d99c2007-10-12 23:04:23 +020047static int apic_calibrate_pmtmr __initdata;
Thomas Gleixner0e078e22008-01-30 13:30:20 +010048int disable_apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010050/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -070051int local_apic_timer_c2_ok;
52EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
53
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010054/*
55 * Debug level, exported for io_apic.c
56 */
57int apic_verbosity;
58
Alexey Starikovskiybab4b272008-05-19 19:47:03 +040059/* Have we found an MP table */
60int smp_found_config;
61
Aaron Durbin39928722006-12-07 02:14:01 +010062static struct resource lapic_resource = {
63 .name = "Local APIC",
64 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
65};
66
Thomas Gleixnerd03030e2007-10-12 23:04:06 +020067static unsigned int calibration_result;
68
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020069static int lapic_next_event(unsigned long delta,
70 struct clock_event_device *evt);
71static void lapic_timer_setup(enum clock_event_mode mode,
72 struct clock_event_device *evt);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020073static void lapic_timer_broadcast(cpumask_t mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +010074static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020075
76static struct clock_event_device lapic_clockevent = {
77 .name = "lapic",
78 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
79 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
80 .shift = 32,
81 .set_mode = lapic_timer_setup,
82 .set_next_event = lapic_next_event,
83 .broadcast = lapic_timer_broadcast,
84 .rating = 100,
85 .irq = -1,
86};
87static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
88
Andi Kleend3432892008-01-30 13:33:17 +010089static unsigned long apic_phys;
90
Alexey Starikovskiy3f530702008-03-27 23:55:47 +030091unsigned long mp_lapic_addr;
92
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +030093unsigned int __cpuinitdata maxcpus = NR_CPUS;
Thomas Gleixner0e078e22008-01-30 13:30:20 +010094/*
95 * Get the LAPIC version
96 */
97static inline int lapic_get_version(void)
98{
99 return GET_APIC_VERSION(apic_read(APIC_LVR));
100}
101
102/*
103 * Check, if the APIC is integrated or a seperate chip
104 */
105static inline int lapic_is_integrated(void)
106{
107 return 1;
108}
109
110/*
111 * Check, whether this is a modern or a first generation APIC
112 */
113static int modern_apic(void)
114{
115 /* AMD systems use old APIC versions, so check the CPU */
116 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
117 boot_cpu_data.x86 >= 0xf)
118 return 1;
119 return lapic_get_version() >= 0x14;
120}
121
Suresh Siddha1b374e42008-07-10 11:16:49 -0700122void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100123{
124 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
125 cpu_relax();
126}
127
Suresh Siddha1b374e42008-07-10 11:16:49 -0700128u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100129{
130 u32 send_status;
131 int timeout;
132
133 timeout = 0;
134 do {
135 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
136 if (!send_status)
137 break;
138 udelay(100);
139 } while (timeout++ < 1000);
140
141 return send_status;
142}
143
Suresh Siddha1b374e42008-07-10 11:16:49 -0700144void xapic_icr_write(u32 low, u32 id)
145{
146 apic_write(APIC_ICR2, id << 24);
147 apic_write(APIC_ICR, low);
148}
149
150u64 xapic_icr_read(void)
151{
152 u32 icr1, icr2;
153
154 icr2 = apic_read(APIC_ICR2);
155 icr1 = apic_read(APIC_ICR);
156
157 return (icr1 | ((u64)icr2 << 32));
158}
159
160static struct apic_ops xapic_ops = {
161 .read = native_apic_mem_read,
162 .write = native_apic_mem_write,
163 .write_atomic = native_apic_mem_write_atomic,
164 .icr_read = xapic_icr_read,
165 .icr_write = xapic_icr_write,
166 .wait_icr_idle = xapic_wait_icr_idle,
167 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
168};
169
170struct apic_ops __read_mostly *apic_ops = &xapic_ops;
171
172EXPORT_SYMBOL_GPL(apic_ops);
173
Suresh Siddha13c88fb2008-07-10 11:16:52 -0700174static void x2apic_wait_icr_idle(void)
175{
176 /* no need to wait for icr idle in x2apic */
177 return;
178}
179
180static u32 safe_x2apic_wait_icr_idle(void)
181{
182 /* no need to wait for icr idle in x2apic */
183 return 0;
184}
185
186void x2apic_icr_write(u32 low, u32 id)
187{
188 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
189}
190
191u64 x2apic_icr_read(void)
192{
193 unsigned long val;
194
195 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
196 return val;
197}
198
199static struct apic_ops x2apic_ops = {
200 .read = native_apic_msr_read,
201 .write = native_apic_msr_write,
202 .write_atomic = native_apic_msr_write,
203 .icr_read = x2apic_icr_read,
204 .icr_write = x2apic_icr_write,
205 .wait_icr_idle = x2apic_wait_icr_idle,
206 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
207};
208
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100209/**
210 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
211 */
Jan Beuliche9427102008-01-30 13:31:24 +0100212void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100213{
214 unsigned int v;
215
216 /* unmask and set to NMI */
217 v = APIC_DM_NMI;
218 apic_write(APIC_LVT0, v);
219}
220
221/**
222 * lapic_get_maxlvt - get the maximum number of local vector table entries
223 */
224int lapic_get_maxlvt(void)
225{
226 unsigned int v, maxlvt;
227
228 v = apic_read(APIC_LVR);
229 maxlvt = GET_APIC_MAXLVT(v);
230 return maxlvt;
231}
232
233/*
234 * This function sets up the local APIC timer, with a timeout of
235 * 'clocks' APIC bus clock. During calibration we actually call
236 * this function twice on the boot CPU, once with a bogus timeout
237 * value, second time for real. The other (noncalibrating) CPUs
238 * call this function only once, with the real, calibrated value.
239 *
240 * We do reads before writes even if unnecessary, to get around the
241 * P5 APIC double write bug.
242 */
243
244static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
245{
246 unsigned int lvtt_value, tmp_value;
247
248 lvtt_value = LOCAL_TIMER_VECTOR;
249 if (!oneshot)
250 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
251 if (!irqen)
252 lvtt_value |= APIC_LVT_MASKED;
253
254 apic_write(APIC_LVTT, lvtt_value);
255
256 /*
257 * Divide PICLK by 16
258 */
259 tmp_value = apic_read(APIC_TDCR);
260 apic_write(APIC_TDCR, (tmp_value
261 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
262 | APIC_TDR_DIV_16);
263
264 if (!oneshot)
265 apic_write(APIC_TMICT, clocks);
266}
267
268/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100269 * Setup extended LVT, AMD specific (K8, family 10h)
270 *
271 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
272 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100273 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100274
275#define APIC_EILVT_LVTOFF_MCE 0
276#define APIC_EILVT_LVTOFF_IBS 1
277
278static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100279{
Robert Richter7b83dae2008-01-30 13:30:40 +0100280 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100281 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
282
283 apic_write(reg, v);
284}
285
Robert Richter7b83dae2008-01-30 13:30:40 +0100286u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
287{
288 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
289 return APIC_EILVT_LVTOFF_MCE;
290}
291
292u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
293{
294 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
295 return APIC_EILVT_LVTOFF_IBS;
296}
297
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100298/*
299 * Program the next event, relative to now
300 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200301static int lapic_next_event(unsigned long delta,
302 struct clock_event_device *evt)
303{
304 apic_write(APIC_TMICT, delta);
305 return 0;
306}
307
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100308/*
309 * Setup the lapic timer in periodic or oneshot mode
310 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200311static void lapic_timer_setup(enum clock_event_mode mode,
312 struct clock_event_device *evt)
313{
314 unsigned long flags;
315 unsigned int v;
316
317 /* Lapic used as dummy for broadcast ? */
318 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
319 return;
320
321 local_irq_save(flags);
322
323 switch (mode) {
324 case CLOCK_EVT_MODE_PERIODIC:
325 case CLOCK_EVT_MODE_ONESHOT:
326 __setup_APIC_LVTT(calibration_result,
327 mode != CLOCK_EVT_MODE_PERIODIC, 1);
328 break;
329 case CLOCK_EVT_MODE_UNUSED:
330 case CLOCK_EVT_MODE_SHUTDOWN:
331 v = apic_read(APIC_LVTT);
332 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
333 apic_write(APIC_LVTT, v);
334 break;
335 case CLOCK_EVT_MODE_RESUME:
336 /* Nothing to do here */
337 break;
338 }
339
340 local_irq_restore(flags);
341}
342
343/*
344 * Local APIC timer broadcast function
345 */
346static void lapic_timer_broadcast(cpumask_t mask)
347{
348#ifdef CONFIG_SMP
349 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
350#endif
351}
352
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100353/*
354 * Setup the local APIC timer for this CPU. Copy the initilized values
355 * of the boot CPU and register the clock event in the framework.
356 */
357static void setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200358{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100359 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
360
361 memcpy(levt, &lapic_clockevent, sizeof(*levt));
362 levt->cpumask = cpumask_of_cpu(smp_processor_id());
363
364 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200365}
366
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100367/*
368 * In this function we calibrate APIC bus clocks to the external
369 * timer. Unfortunately we cannot use jiffies and the timer irq
370 * to calibrate, since some later bootup code depends on getting
371 * the first irq? Ugh.
372 *
373 * We want to do the calibration only once since we
374 * want to have local timer irqs syncron. CPUs connected
375 * by the same APIC bus have the very same bus frequency.
376 * And we want to have irqs off anyways, no accidental
377 * APIC irq that way.
378 */
379
380#define TICK_COUNT 100000000
381
382static void __init calibrate_APIC_clock(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200383{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100384 unsigned apic, apic_start;
385 unsigned long tsc, tsc_start;
386 int result;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200387
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100388 local_irq_disable();
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200389
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100390 /*
391 * Put whatever arbitrary (but long enough) timeout
392 * value into the APIC clock, we just want to get the
393 * counter running for calibration.
394 *
395 * No interrupt enable !
396 */
397 __setup_APIC_LVTT(250000000, 0, 0);
398
399 apic_start = apic_read(APIC_TMCCT);
400#ifdef CONFIG_X86_PM_TIMER
401 if (apic_calibrate_pmtmr && pmtmr_ioport) {
402 pmtimer_wait(5000); /* 5ms wait */
403 apic = apic_read(APIC_TMCCT);
404 result = (apic_start - apic) * 1000L / 5;
405 } else
406#endif
407 {
408 rdtscll(tsc_start);
409
410 do {
411 apic = apic_read(APIC_TMCCT);
412 rdtscll(tsc);
413 } while ((tsc - tsc_start) < TICK_COUNT &&
414 (apic_start - apic) < TICK_COUNT);
415
416 result = (apic_start - apic) * 1000L * tsc_khz /
417 (tsc - tsc_start);
418 }
419
420 local_irq_enable();
421
422 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
423
424 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
425 result / 1000 / 1000, result / 1000 % 1000);
426
427 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900428 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
429 lapic_clockevent.shift);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100430 lapic_clockevent.max_delta_ns =
431 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
432 lapic_clockevent.min_delta_ns =
433 clockevent_delta2ns(0xF, &lapic_clockevent);
434
435 calibration_result = result / HZ;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200436}
437
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100438/*
439 * Setup the boot APIC
440 *
441 * Calibrate and verify the result.
442 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100443void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100445 /*
446 * The local apic timer can be disabled via the kernel commandline.
447 * Register the lapic timer as a dummy clock event source on SMP
448 * systems, so the broadcast mechanism is used. On UP systems simply
449 * ignore it.
450 */
451 if (disable_apic_timer) {
452 printk(KERN_INFO "Disabling APIC timer\n");
453 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100454 if (num_possible_cpus() > 1) {
455 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100456 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100457 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100458 return;
459 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200460
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100461 printk(KERN_INFO "Using local APIC timer interrupts.\n");
462 calibrate_APIC_clock();
463
464 /*
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100465 * Do a sanity check on the APIC calibration result
466 */
467 if (calibration_result < (1000000 / HZ)) {
468 printk(KERN_WARNING
469 "APIC frequency too slow, disabling apic timer\n");
470 /* No broadcast on UP ! */
471 if (num_possible_cpus() > 1)
472 setup_APIC_timer();
473 return;
474 }
475
476 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100477 * If nmi_watchdog is set to IO_APIC, we need the
478 * PIT/HPET going. Otherwise register lapic as a dummy
479 * device.
480 */
481 if (nmi_watchdog != NMI_IO_APIC)
482 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
483 else
484 printk(KERN_WARNING "APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200485 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100486
487 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100490void __cpuinit setup_secondary_APIC_clock(void)
491{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100492 setup_APIC_timer();
493}
494
495/*
496 * The guts of the apic timer interrupt
497 */
498static void local_apic_timer_interrupt(void)
499{
500 int cpu = smp_processor_id();
501 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
502
503 /*
504 * Normally we should not be here till LAPIC has been initialized but
505 * in some cases like kdump, its possible that there is a pending LAPIC
506 * timer interrupt from previous kernel's context and is delivered in
507 * new kernel the moment interrupts are enabled.
508 *
509 * Interrupts are enabled early and LAPIC is setup much later, hence
510 * its possible that when we get here evt->event_handler is NULL.
511 * Check for event_handler being NULL and discard the interrupt as
512 * spurious.
513 */
514 if (!evt->event_handler) {
515 printk(KERN_WARNING
516 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
517 /* Switch it off */
518 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
519 return;
520 }
521
522 /*
523 * the NMI deadlock-detector uses this.
524 */
525 add_pda(apic_timer_irqs, 1);
526
527 evt->event_handler(evt);
528}
529
530/*
531 * Local APIC timer interrupt. This is the most natural way for doing
532 * local interrupts, but local timer interrupts can be emulated by
533 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
534 *
535 * [ if a single-CPU system runs an SMP kernel then we call the local
536 * interrupt as well. Thus we cannot inline the local irq ... ]
537 */
538void smp_apic_timer_interrupt(struct pt_regs *regs)
539{
540 struct pt_regs *old_regs = set_irq_regs(regs);
541
542 /*
543 * NOTE! We'd better ACK the irq immediately,
544 * because timer handling can be slow.
545 */
546 ack_APIC_irq();
547 /*
548 * update_process_times() expects us to have done irq_enter().
549 * Besides, if we don't timer interrupts ignore the global
550 * interrupt lock, which is the WrongThing (tm) to do.
551 */
552 exit_idle();
553 irq_enter();
554 local_apic_timer_interrupt();
555 irq_exit();
556 set_irq_regs(old_regs);
557}
558
559int setup_profiling_timer(unsigned int multiplier)
560{
561 return -EINVAL;
562}
563
564
565/*
566 * Local APIC start and shutdown
567 */
568
569/**
570 * clear_local_APIC - shutdown the local APIC
571 *
572 * This is called, when a CPU is disabled and before rebooting, so the state of
573 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
574 * leftovers during boot.
575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576void clear_local_APIC(void)
577{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400578 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100579 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Andi Kleend3432892008-01-30 13:33:17 +0100581 /* APIC hasn't been mapped yet */
582 if (!apic_phys)
583 return;
584
585 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200587 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * if the vector is zero. Mask LVTERR first to prevent this.
589 */
590 if (maxlvt >= 3) {
591 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100592 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 /*
595 * Careful: we have to set masks only first to deassert
596 * any level-triggered sources.
597 */
598 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100599 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100601 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100603 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (maxlvt >= 4) {
605 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100606 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 /*
610 * Clean APIC state for other OSs:
611 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100612 apic_write(APIC_LVTT, APIC_LVT_MASKED);
613 apic_write(APIC_LVT0, APIC_LVT_MASKED);
614 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100616 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100618 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200619 apic_write(APIC_ESR, 0);
620 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100623/**
624 * disable_local_APIC - clear and disable the local APIC
625 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626void disable_local_APIC(void)
627{
628 unsigned int value;
629
630 clear_local_APIC();
631
632 /*
633 * Disable APIC (implies clearing of registers
634 * for 82489DX!).
635 */
636 value = apic_read(APIC_SPIV);
637 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100638 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700641void lapic_shutdown(void)
642{
643 unsigned long flags;
644
645 if (!cpu_has_apic)
646 return;
647
648 local_irq_save(flags);
649
650 disable_local_APIC();
651
652 local_irq_restore(flags);
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
656 * This is to verify that we're looking at a real local APIC.
657 * Check these against your board if the CPUs aren't getting
658 * started for no apparent reason.
659 */
660int __init verify_local_APIC(void)
661{
662 unsigned int reg0, reg1;
663
664 /*
665 * The version register is read-only in a real APIC.
666 */
667 reg0 = apic_read(APIC_LVR);
668 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
669 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
670 reg1 = apic_read(APIC_LVR);
671 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
672
673 /*
674 * The two version reads above should print the same
675 * numbers. If the second one is different, then we
676 * poke at a non-APIC.
677 */
678 if (reg1 != reg0)
679 return 0;
680
681 /*
682 * Check if the version looks reasonably.
683 */
684 reg1 = GET_APIC_VERSION(reg0);
685 if (reg1 == 0x00 || reg1 == 0xff)
686 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100687 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (reg1 < 0x02 || reg1 == 0xff)
689 return 0;
690
691 /*
692 * The ID register is read/write in a real APIC.
693 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700694 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
696 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700697 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
699 apic_write(APIC_ID, reg0);
700 if (reg1 != (reg0 ^ APIC_ID_MASK))
701 return 0;
702
703 /*
704 * The next two are just to see if we have sane values.
705 * They're only really relevant if we're in Virtual Wire
706 * compatibility mode, but most boxes are anymore.
707 */
708 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100709 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 reg1 = apic_read(APIC_LVT1);
711 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
712
713 return 1;
714}
715
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100716/**
717 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719void __init sync_Arb_IDs(void)
720{
721 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100722 if (modern_apic())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return;
724
725 /*
726 * Wait for idle.
727 */
728 apic_wait_icr_idle();
729
730 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100731 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 | APIC_DM_INIT);
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * An initial setup of the virtual wire mode.
737 */
738void __init init_bsp_APIC(void)
739{
Andi Kleen11a8e772006-01-11 22:46:51 +0100740 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /*
743 * Don't do the setup now if we have a SMP BIOS as the
744 * through-I/O-APIC virtual wire mode might be active.
745 */
746 if (smp_found_config || !cpu_has_apic)
747 return;
748
749 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * Do not trust the local APIC being empty at bootup.
753 */
754 clear_local_APIC();
755
756 /*
757 * Enable APIC.
758 */
759 value = apic_read(APIC_SPIV);
760 value &= ~APIC_VECTOR_MASK;
761 value |= APIC_SPIV_APIC_ENABLED;
762 value |= APIC_SPIV_FOCUS_DISABLED;
763 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100764 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /*
767 * Set up the virtual wire mode.
768 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100769 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100771 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100774/**
775 * setup_local_APIC - setup the local APIC
776 */
777void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Andi Kleen739f33b2008-01-30 13:30:40 +0100779 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100780 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Jack Steinerac23d4e2008-03-28 14:12:16 -0500782 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Andi Kleenfe7414a2006-09-26 10:52:30 +0200785 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /*
788 * Double-check whether this APIC is really registered.
789 * This is meaningless in clustered apic mode, so we skip it.
790 */
791 if (!apic_id_registered())
792 BUG();
793
794 /*
795 * Intel recommends to set DFR, LDR and TPR before enabling
796 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
797 * document number 292116). So here it goes...
798 */
799 init_apic_ldr();
800
801 /*
802 * Set Task Priority to 'accept all'. We never change this
803 * later on.
804 */
805 value = apic_read(APIC_TASKPRI);
806 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100807 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100810 * After a crash, we no longer service the interrupts and a pending
811 * interrupt from previous kernel might still have ISR bit set.
812 *
813 * Most probably by now CPU has serviced that pending interrupt and
814 * it might not have done the ack_APIC_irq() because it thought,
815 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
816 * does not clear the ISR bit and cpu thinks it has already serivced
817 * the interrupt. Hence a vector might get locked. It was noticed
818 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
819 */
820 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
821 value = apic_read(APIC_ISR + i*0x10);
822 for (j = 31; j >= 0; j--) {
823 if (value & (1<<j))
824 ack_APIC_irq();
825 }
826 }
827
828 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * Now that we are all set up, enable the APIC
830 */
831 value = apic_read(APIC_SPIV);
832 value &= ~APIC_VECTOR_MASK;
833 /*
834 * Enable APIC
835 */
836 value |= APIC_SPIV_APIC_ENABLED;
837
Andi Kleen3f14c742006-09-26 10:52:29 +0200838 /* We always use processor focus */
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * Set spurious IRQ vector
842 */
843 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100844 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /*
847 * Set up LVT0, LVT1:
848 *
849 * set up through-local-APIC on the BP's LINT0. This is not
850 * strictly necessary in pure symmetric-IO mode, but sometimes
851 * we delegate interrupts to the 8259A.
852 */
853 /*
854 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
855 */
856 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200857 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200859 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
860 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 } else {
862 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200863 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
864 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100866 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /*
869 * only the BP should see the LINT1 NMI signal, obviously.
870 */
871 if (!smp_processor_id())
872 value = APIC_DM_NMI;
873 else
874 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100875 apic_write(APIC_LVT1, value);
Jack Steinerac23d4e2008-03-28 14:12:16 -0500876 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +0100877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Ingo Molnara4928cf2008-04-23 13:20:56 +0200879static void __cpuinit lapic_setup_esr(void)
Andi Kleen739f33b2008-01-30 13:30:40 +0100880{
881 unsigned maxlvt = lapic_get_maxlvt();
882
883 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
Yinghai Lu1c695242008-01-30 13:30:39 +0100884 /*
Andi Kleen739f33b2008-01-30 13:30:40 +0100885 * spec says clear errors after enabling vector.
Yinghai Lu1c695242008-01-30 13:30:39 +0100886 */
Andi Kleen739f33b2008-01-30 13:30:40 +0100887 if (maxlvt > 3)
888 apic_write(APIC_ESR, 0);
889}
Yinghai Lu1c695242008-01-30 13:30:39 +0100890
Andi Kleen739f33b2008-01-30 13:30:40 +0100891void __cpuinit end_local_APIC_setup(void)
892{
893 lapic_setup_esr();
Don Zickusf2802e72006-09-26 10:52:26 +0200894 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 apic_pm_activate();
896}
897
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100898/*
899 * Detect and enable local APICs on non-SMP boards.
900 * Original code written by Keir Fraser.
901 * On AMD64 we trust the BIOS - if it says no APIC it is likely
902 * not correctly set up (usually the APIC timer won't work etc.)
903 */
904static int __init detect_init_APIC(void)
905{
906 if (!cpu_has_apic) {
907 printk(KERN_INFO "No local APIC present\n");
908 return -1;
909 }
910
911 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -0300912 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100913 return 0;
914}
915
Yinghai Lu8643f9d2008-02-19 03:21:06 -0800916void __init early_init_lapic_mapping(void)
917{
Thomas Gleixner431ee792008-05-12 15:43:35 +0200918 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -0800919
920 /*
921 * If no local APIC can be found then go out
922 * : it means there is no mpatable and MADT
923 */
924 if (!smp_found_config)
925 return;
926
Thomas Gleixner431ee792008-05-12 15:43:35 +0200927 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -0800928
Thomas Gleixner431ee792008-05-12 15:43:35 +0200929 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -0800930 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +0200931 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -0800932
933 /*
934 * Fetch the APIC ID of the BSP in case we have a
935 * default configuration (or the MP table is broken).
936 */
Jack Steiner05f2d122008-03-28 14:12:02 -0500937 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Yinghai Lu8643f9d2008-02-19 03:21:06 -0800938}
939
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100940/**
941 * init_apic_mappings - initialize APIC mappings
942 */
943void __init init_apic_mappings(void)
944{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100945 /*
946 * If no local APIC can be found then set up a fake all
947 * zeroes page to simulate the local APIC and another
948 * one for the IO-APIC.
949 */
950 if (!smp_found_config && detect_init_APIC()) {
951 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
952 apic_phys = __pa(apic_phys);
953 } else
954 apic_phys = mp_lapic_addr;
955
956 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
957 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
958 APIC_BASE, apic_phys);
959
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100960 /*
961 * Fetch the APIC ID of the BSP in case we have a
962 * default configuration (or the MP table is broken).
963 */
Jack Steiner05f2d122008-03-28 14:12:02 -0500964 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100965}
966
967/*
968 * This initializes the IO-APIC and APIC hardware if this is
969 * a UP kernel.
970 */
971int __init APIC_init_uniprocessor(void)
972{
973 if (disable_apic) {
974 printk(KERN_INFO "Apic disabled\n");
975 return -1;
976 }
977 if (!cpu_has_apic) {
978 disable_apic = 1;
979 printk(KERN_INFO "Apic disabled by BIOS\n");
980 return -1;
981 }
982
983 verify_local_APIC();
984
Glauber Costab5841762008-05-28 13:38:28 -0300985 connect_bsp_APIC();
986
Jack Steinerb6df1b82008-06-19 21:51:05 -0500987 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -0300988 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100989
990 setup_local_APIC();
991
Andi Kleen739f33b2008-01-30 13:30:40 +0100992 /*
993 * Now enable IO-APICs, actually call clear_IO_APIC
994 * We need clear_IO_APIC before enabling vector on BP
995 */
996 if (!skip_ioapic_setup && nr_ioapics)
997 enable_IO_APIC();
998
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +0100999 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1000 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001001 end_local_APIC_setup();
1002
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001003 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1004 setup_IO_APIC();
1005 else
1006 nr_ioapics = 0;
1007 setup_boot_APIC_clock();
1008 check_nmi_watchdog();
1009 return 0;
1010}
1011
1012/*
1013 * Local APIC interrupts
1014 */
1015
1016/*
1017 * This interrupt should _never_ happen with our APIC/SMP architecture
1018 */
1019asmlinkage void smp_spurious_interrupt(void)
1020{
1021 unsigned int v;
1022 exit_idle();
1023 irq_enter();
1024 /*
1025 * Check if this really is a spurious interrupt and ACK it
1026 * if it is a vectored one. Just in case...
1027 * Spurious interrupts should not be ACKed.
1028 */
1029 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1030 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1031 ack_APIC_irq();
1032
1033 add_pda(irq_spurious_count, 1);
1034 irq_exit();
1035}
1036
1037/*
1038 * This interrupt should never happen with our APIC/SMP architecture
1039 */
1040asmlinkage void smp_error_interrupt(void)
1041{
1042 unsigned int v, v1;
1043
1044 exit_idle();
1045 irq_enter();
1046 /* First tickle the hardware, only then report what went on. -- REW */
1047 v = apic_read(APIC_ESR);
1048 apic_write(APIC_ESR, 0);
1049 v1 = apic_read(APIC_ESR);
1050 ack_APIC_irq();
1051 atomic_inc(&irq_err_count);
1052
1053 /* Here is what the APIC error bits mean:
1054 0: Send CS error
1055 1: Receive CS error
1056 2: Send accept error
1057 3: Receive accept error
1058 4: Reserved
1059 5: Send illegal vector
1060 6: Received illegal vector
1061 7: Illegal register address
1062 */
1063 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1064 smp_processor_id(), v , v1);
1065 irq_exit();
1066}
1067
Glauber Costab5841762008-05-28 13:38:28 -03001068/**
1069 * * connect_bsp_APIC - attach the APIC to the interrupt system
1070 * */
1071void __init connect_bsp_APIC(void)
1072{
1073 enable_apic_mode();
1074}
1075
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001076void disconnect_bsp_APIC(int virt_wire_setup)
1077{
1078 /* Go back to Virtual Wire compatibility mode */
1079 unsigned long value;
1080
1081 /* For the spurious interrupt use vector F, and enable it */
1082 value = apic_read(APIC_SPIV);
1083 value &= ~APIC_VECTOR_MASK;
1084 value |= APIC_SPIV_APIC_ENABLED;
1085 value |= 0xf;
1086 apic_write(APIC_SPIV, value);
1087
1088 if (!virt_wire_setup) {
1089 /*
1090 * For LVT0 make it edge triggered, active high,
1091 * external and enabled
1092 */
1093 value = apic_read(APIC_LVT0);
1094 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1095 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1096 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1097 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1098 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1099 apic_write(APIC_LVT0, value);
1100 } else {
1101 /* Disable LVT0 */
1102 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1103 }
1104
1105 /* For LVT1 make it edge triggered, active high, nmi and enabled */
1106 value = apic_read(APIC_LVT1);
1107 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1108 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1109 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1110 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1111 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1112 apic_write(APIC_LVT1, value);
1113}
1114
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001115void __cpuinit generic_processor_info(int apicid, int version)
1116{
1117 int cpu;
1118 cpumask_t tmp_map;
1119
1120 if (num_processors >= NR_CPUS) {
1121 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1122 " Processor ignored.\n", NR_CPUS);
1123 return;
1124 }
1125
1126 if (num_processors >= maxcpus) {
1127 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1128 " Processor ignored.\n", maxcpus);
1129 return;
1130 }
1131
1132 num_processors++;
1133 cpus_complement(tmp_map, cpu_present_map);
1134 cpu = first_cpu(tmp_map);
1135
1136 physid_set(apicid, phys_cpu_present_map);
1137 if (apicid == boot_cpu_physical_apicid) {
1138 /*
1139 * x86_bios_cpu_apicid is required to have processors listed
1140 * in same order as logical cpu numbers. Hence the first
1141 * entry is BSP, and so on.
1142 */
1143 cpu = 0;
1144 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001145 if (apicid > max_physical_apicid)
1146 max_physical_apicid = apicid;
1147
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001148 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001149 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1150 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1151 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001152
1153 cpu_to_apicid[cpu] = apicid;
1154 bios_cpu_apicid[cpu] = apicid;
1155 } else {
1156 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1157 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1158 }
1159
1160 cpu_set(cpu, cpu_possible_map);
1161 cpu_set(cpu, cpu_present_map);
1162}
1163
Suresh Siddha0c81c742008-07-10 11:16:48 -07001164int hard_smp_processor_id(void)
1165{
1166 return read_apic_id();
1167}
1168
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001169/*
1170 * Power management
1171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#ifdef CONFIG_PM
1173
1174static struct {
1175 /* 'active' is true if the local APIC was enabled by us and
1176 not the BIOS; this signifies that we are also responsible
1177 for disabling it before entering apm/acpi suspend */
1178 int active;
1179 /* r/w apic fields */
1180 unsigned int apic_id;
1181 unsigned int apic_taskpri;
1182 unsigned int apic_ldr;
1183 unsigned int apic_dfr;
1184 unsigned int apic_spiv;
1185 unsigned int apic_lvtt;
1186 unsigned int apic_lvtpc;
1187 unsigned int apic_lvt0;
1188 unsigned int apic_lvt1;
1189 unsigned int apic_lvterr;
1190 unsigned int apic_tmict;
1191 unsigned int apic_tdcr;
1192 unsigned int apic_thmr;
1193} apic_pm_state;
1194
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001195static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001198 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if (!apic_pm_state.active)
1201 return 0;
1202
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001203 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001204
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001205 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1207 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1208 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1209 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1210 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001211 if (maxlvt >= 4)
1212 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1214 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1215 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1216 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1217 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001218#ifdef CONFIG_X86_MCE_INTEL
1219 if (maxlvt >= 5)
1220 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1221#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001222 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 disable_local_APIC();
1224 local_irq_restore(flags);
1225 return 0;
1226}
1227
1228static int lapic_resume(struct sys_device *dev)
1229{
1230 unsigned int l, h;
1231 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001232 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!apic_pm_state.active)
1235 return 0;
1236
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001237 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 local_irq_save(flags);
1240 rdmsr(MSR_IA32_APICBASE, l, h);
1241 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +01001242 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 wrmsr(MSR_IA32_APICBASE, l, h);
1244 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1245 apic_write(APIC_ID, apic_pm_state.apic_id);
1246 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1247 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1248 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1249 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1250 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1251 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001252#ifdef CONFIG_X86_MCE_INTEL
1253 if (maxlvt >= 5)
1254 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1255#endif
1256 if (maxlvt >= 4)
1257 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1259 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1260 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1261 apic_write(APIC_ESR, 0);
1262 apic_read(APIC_ESR);
1263 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1264 apic_write(APIC_ESR, 0);
1265 apic_read(APIC_ESR);
1266 local_irq_restore(flags);
1267 return 0;
1268}
1269
1270static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001271 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 .resume = lapic_resume,
1273 .suspend = lapic_suspend,
1274};
1275
1276static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001277 .id = 0,
1278 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279};
1280
Ashok Raje6982c62005-06-25 14:54:58 -07001281static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 apic_pm_state.active = 1;
1284}
1285
1286static int __init init_lapic_sysfs(void)
1287{
1288 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!cpu_has_apic)
1291 return 0;
1292 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 error = sysdev_class_register(&lapic_sysclass);
1295 if (!error)
1296 error = sysdev_register(&device_lapic);
1297 return error;
1298}
1299device_initcall(init_lapic_sysfs);
1300
1301#else /* CONFIG_PM */
1302
1303static void apic_pm_activate(void) { }
1304
1305#endif /* CONFIG_PM */
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001308 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 *
1310 * Thus far, the major user of this is IBM's Summit2 series:
1311 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001312 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * multi-chassis. Use available data to take a good guess.
1314 * If in doubt, go HPET.
1315 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001316__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 int i, clusters, zeros;
1319 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08001320 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1322
Yinghai Lu322850a2008-02-23 21:48:42 -08001323 /*
1324 * there is not this kind of box with AMD CPU yet.
1325 * Some AMD box with quadcore cpu and 8 sockets apicid
1326 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08001327 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08001328 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001329 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08001330 return 0;
1331
Mike Travis23ca4bb2008-05-12 21:21:12 +02001332 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07001333 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 for (i = 0; i < NR_CPUS; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001336 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01001337 if (bios_cpu_apicid) {
1338 id = bios_cpu_apicid[i];
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001339 }
1340 else if (i < nr_cpu_ids) {
1341 if (cpu_present(i))
1342 id = per_cpu(x86_bios_cpu_apicid, i);
1343 else
1344 continue;
1345 }
1346 else
1347 break;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (id != BAD_APICID)
1350 __set_bit(APIC_CLUSTERID(id), clustermap);
1351 }
1352
1353 /* Problem: Partially populated chassis may not have CPUs in some of
1354 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01001355 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1356 * Since clusters are allocated sequentially, count zeros only if
1357 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
1359 clusters = 0;
1360 zeros = 0;
1361 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1362 if (test_bit(i, clustermap)) {
1363 clusters += 1 + zeros;
1364 zeros = 0;
1365 } else
1366 ++zeros;
1367 }
1368
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001369 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1370 * not guaranteed to be synced between boards
1371 */
1372 if (is_vsmp_box() && clusters > 1)
1373 return 1;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001376 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * May have to revisit this when multi-core + hyperthreaded CPUs come
1378 * out, but AFAIK this will work even for them.
1379 */
1380 return (clusters > 2);
1381}
1382
1383/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001384 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001386static int __init apic_set_verbosity(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001388 if (str == NULL) {
1389 skip_ioapic_setup = 0;
1390 ioapic_force = 1;
1391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001393 if (strcmp("debug", str) == 0)
1394 apic_verbosity = APIC_DEBUG;
1395 else if (strcmp("verbose", str) == 0)
1396 apic_verbosity = APIC_VERBOSE;
1397 else {
1398 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1399 " use apic=verbose or apic=debug\n", str);
1400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return 0;
1404}
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001405early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001407static __init int setup_disableapic(char *str)
1408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 disable_apic = 1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001410 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001411 return 0;
1412}
1413early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001415/* same as disableapic, for compatibility */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001416static __init int setup_nolapic(char *str)
1417{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001418 return setup_disableapic(str);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001419}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001420early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001422static int __init parse_lapic_timer_c2_ok(char *arg)
1423{
1424 local_apic_timer_c2_ok = 1;
1425 return 0;
1426}
1427early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1428
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001429static __init int setup_noapictimer(char *str)
1430{
Andi Kleen73dea472006-02-03 21:50:50 +01001431 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001434 return 1;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001435}
Thomas Gleixner9f75e9b2007-10-12 23:04:23 +02001436__setup("noapictimer", setup_noapictimer);
Andi Kleen73dea472006-02-03 21:50:50 +01001437
Andi Kleen0c3749c2006-02-03 21:51:41 +01001438static __init int setup_apicpmtimer(char *s)
1439{
1440 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001441 notsc_setup(NULL);
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001442 return 0;
Andi Kleen0c3749c2006-02-03 21:51:41 +01001443}
1444__setup("apicpmtimer", setup_apicpmtimer);
1445
Yinghai Lu1e934dd2008-02-22 13:37:26 -08001446static int __init lapic_insert_resource(void)
1447{
1448 if (!apic_phys)
1449 return -1;
1450
1451 /* Put local APIC into the resource map. */
1452 lapic_resource.start = apic_phys;
1453 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1454 insert_resource(&iomem_resource, &lapic_resource);
1455
1456 return 0;
1457}
1458
1459/*
1460 * need call insert after e820_reserve_resources()
1461 * that is using request_resource
1462 */
1463late_initcall(lapic_insert_resource);