blob: 8f551276681e0e1239b57d09383fb7687ddeb5a7 [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>
Suresh Siddha6e1cb382008-07-10 11:16:58 -070030#include <linux/dmar.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/atomic.h>
33#include <asm/smp.h>
34#include <asm/mtrr.h>
35#include <asm/mpspec.h>
Yinghai Luefa25592008-08-19 20:50:36 -070036#include <asm/desc.h>
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010037#include <asm/hpet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/pgalloc.h>
Andi Kleen75152112005-05-16 21:53:34 -070039#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010040#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010041#include <asm/proto.h>
42#include <asm/timex.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020043#include <asm/apic.h>
Suresh Siddha6e1cb382008-07-10 11:16:58 -070044#include <asm/i8259.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Glauber Costa5af55732008-03-25 13:28:56 -030046#include <mach_ipi.h>
Glauber Costadd46e3c2008-03-25 18:10:46 -030047#include <mach_apic.h>
Glauber Costa5af55732008-03-25 13:28:56 -030048
Cyrill Gorcunov36fef092008-08-15 13:51:20 +020049/* Disable local APIC timer from the kernel commandline or via dmi quirk */
Thomas Gleixneraa276e12008-06-09 19:15:00 +020050static int disable_apic_timer __cpuinitdata;
Chris Wrightbc1d99c2007-10-12 23:04:23 +020051static int apic_calibrate_pmtmr __initdata;
Thomas Gleixner0e078e22008-01-30 13:30:20 +010052int disable_apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -070053int disable_x2apic;
Suresh Siddha89027d32008-07-10 11:16:56 -070054int x2apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Suresh Siddha6e1cb382008-07-10 11:16:58 -070056/* x2apic enabled before OS handover */
57int x2apic_preenabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010059/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -070060int local_apic_timer_c2_ok;
61EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
62
Yinghai Luefa25592008-08-19 20:50:36 -070063int first_system_vector = 0xfe;
64
65char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
66
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010067/*
68 * Debug level, exported for io_apic.c
69 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +010070unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010071
Alexey Starikovskiybab4b272008-05-19 19:47:03 +040072/* Have we found an MP table */
73int smp_found_config;
74
Aaron Durbin39928722006-12-07 02:14:01 +010075static struct resource lapic_resource = {
76 .name = "Local APIC",
77 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
78};
79
Thomas Gleixnerd03030e2007-10-12 23:04:06 +020080static unsigned int calibration_result;
81
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020082static int lapic_next_event(unsigned long delta,
83 struct clock_event_device *evt);
84static void lapic_timer_setup(enum clock_event_mode mode,
85 struct clock_event_device *evt);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020086static void lapic_timer_broadcast(cpumask_t mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +010087static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020088
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +040089/*
90 * The local apic timer can be used for any function which is CPU local.
91 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020092static struct clock_event_device lapic_clockevent = {
93 .name = "lapic",
94 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
95 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
96 .shift = 32,
97 .set_mode = lapic_timer_setup,
98 .set_next_event = lapic_next_event,
99 .broadcast = lapic_timer_broadcast,
100 .rating = 100,
101 .irq = -1,
102};
103static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
104
Andi Kleend3432892008-01-30 13:33:17 +0100105static unsigned long apic_phys;
106
Alexey Starikovskiy3f530702008-03-27 23:55:47 +0300107unsigned long mp_lapic_addr;
108
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100109/*
110 * Get the LAPIC version
111 */
112static inline int lapic_get_version(void)
113{
114 return GET_APIC_VERSION(apic_read(APIC_LVR));
115}
116
117/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400118 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100119 */
120static inline int lapic_is_integrated(void)
121{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400122#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100123 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400124#else
125 return APIC_INTEGRATED(lapic_get_version());
126#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100127}
128
129/*
130 * Check, whether this is a modern or a first generation APIC
131 */
132static int modern_apic(void)
133{
134 /* AMD systems use old APIC versions, so check the CPU */
135 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
136 boot_cpu_data.x86 >= 0xf)
137 return 1;
138 return lapic_get_version() >= 0x14;
139}
140
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400141/*
142 * Paravirt kernels also might be using these below ops. So we still
143 * use generic apic_read()/apic_write(), which might be pointing to different
144 * ops in PARAVIRT case.
145 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700146void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100147{
148 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
149 cpu_relax();
150}
151
Suresh Siddha1b374e42008-07-10 11:16:49 -0700152u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100153{
154 u32 send_status;
155 int timeout;
156
157 timeout = 0;
158 do {
159 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
160 if (!send_status)
161 break;
162 udelay(100);
163 } while (timeout++ < 1000);
164
165 return send_status;
166}
167
Suresh Siddha1b374e42008-07-10 11:16:49 -0700168void xapic_icr_write(u32 low, u32 id)
169{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200170 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700171 apic_write(APIC_ICR, low);
172}
173
174u64 xapic_icr_read(void)
175{
176 u32 icr1, icr2;
177
178 icr2 = apic_read(APIC_ICR2);
179 icr1 = apic_read(APIC_ICR);
180
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400181 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700182}
183
184static struct apic_ops xapic_ops = {
185 .read = native_apic_mem_read,
186 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700187 .icr_read = xapic_icr_read,
188 .icr_write = xapic_icr_write,
189 .wait_icr_idle = xapic_wait_icr_idle,
190 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
191};
192
193struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700194EXPORT_SYMBOL_GPL(apic_ops);
195
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700196static void x2apic_wait_icr_idle(void)
197{
198 /* no need to wait for icr idle in x2apic */
199 return;
200}
201
202static u32 safe_x2apic_wait_icr_idle(void)
203{
204 /* no need to wait for icr idle in x2apic */
205 return 0;
206}
207
208void x2apic_icr_write(u32 low, u32 id)
209{
210 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
211}
212
213u64 x2apic_icr_read(void)
214{
215 unsigned long val;
216
217 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
218 return val;
219}
220
221static struct apic_ops x2apic_ops = {
222 .read = native_apic_msr_read,
223 .write = native_apic_msr_write,
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700224 .icr_read = x2apic_icr_read,
225 .icr_write = x2apic_icr_write,
226 .wait_icr_idle = x2apic_wait_icr_idle,
227 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
228};
229
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100230/**
231 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
232 */
Jan Beuliche9427102008-01-30 13:31:24 +0100233void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100234{
235 unsigned int v;
236
237 /* unmask and set to NMI */
238 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200239
240 /* Level triggered for 82489DX (32bit mode) */
241 if (!lapic_is_integrated())
242 v |= APIC_LVT_LEVEL_TRIGGER;
243
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100244 apic_write(APIC_LVT0, v);
245}
246
247/**
248 * lapic_get_maxlvt - get the maximum number of local vector table entries
249 */
250int lapic_get_maxlvt(void)
251{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200252 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100253
254 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200255 /*
256 * - we always have APIC integrated on 64bit mode
257 * - 82489DXs do not report # of LVT entries
258 */
259 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100260}
261
262/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400263 * Local APIC timer
264 */
265
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400266/* Clock divisor */
267#ifdef CONFG_X86_64
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200268#define APIC_DIVISOR 1
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400269#else
270#define APIC_DIVISOR 16
271#endif
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200272
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100273/*
274 * This function sets up the local APIC timer, with a timeout of
275 * 'clocks' APIC bus clock. During calibration we actually call
276 * this function twice on the boot CPU, once with a bogus timeout
277 * value, second time for real. The other (noncalibrating) CPUs
278 * call this function only once, with the real, calibrated value.
279 *
280 * We do reads before writes even if unnecessary, to get around the
281 * P5 APIC double write bug.
282 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100283static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
284{
285 unsigned int lvtt_value, tmp_value;
286
287 lvtt_value = LOCAL_TIMER_VECTOR;
288 if (!oneshot)
289 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200290 if (!lapic_is_integrated())
291 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
292
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100293 if (!irqen)
294 lvtt_value |= APIC_LVT_MASKED;
295
296 apic_write(APIC_LVTT, lvtt_value);
297
298 /*
299 * Divide PICLK by 16
300 */
301 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400302 apic_write(APIC_TDCR,
303 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
304 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100305
306 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200307 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100308}
309
310/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100311 * Setup extended LVT, AMD specific (K8, family 10h)
312 *
313 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
314 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200315 *
316 * If mask=1, the LVT entry does not generate interrupts while mask=0
317 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100318 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100319
320#define APIC_EILVT_LVTOFF_MCE 0
321#define APIC_EILVT_LVTOFF_IBS 1
322
323static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100324{
Robert Richter7b83dae2008-01-30 13:30:40 +0100325 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100326 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
327
328 apic_write(reg, v);
329}
330
Robert Richter7b83dae2008-01-30 13:30:40 +0100331u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
332{
333 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
334 return APIC_EILVT_LVTOFF_MCE;
335}
336
337u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
338{
339 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
340 return APIC_EILVT_LVTOFF_IBS;
341}
Robert Richter6aa360e2008-07-23 15:28:14 +0200342EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100343
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100344/*
345 * Program the next event, relative to now
346 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200347static int lapic_next_event(unsigned long delta,
348 struct clock_event_device *evt)
349{
350 apic_write(APIC_TMICT, delta);
351 return 0;
352}
353
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100354/*
355 * Setup the lapic timer in periodic or oneshot mode
356 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200357static void lapic_timer_setup(enum clock_event_mode mode,
358 struct clock_event_device *evt)
359{
360 unsigned long flags;
361 unsigned int v;
362
363 /* Lapic used as dummy for broadcast ? */
364 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
365 return;
366
367 local_irq_save(flags);
368
369 switch (mode) {
370 case CLOCK_EVT_MODE_PERIODIC:
371 case CLOCK_EVT_MODE_ONESHOT:
372 __setup_APIC_LVTT(calibration_result,
373 mode != CLOCK_EVT_MODE_PERIODIC, 1);
374 break;
375 case CLOCK_EVT_MODE_UNUSED:
376 case CLOCK_EVT_MODE_SHUTDOWN:
377 v = apic_read(APIC_LVTT);
378 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
379 apic_write(APIC_LVTT, v);
380 break;
381 case CLOCK_EVT_MODE_RESUME:
382 /* Nothing to do here */
383 break;
384 }
385
386 local_irq_restore(flags);
387}
388
389/*
390 * Local APIC timer broadcast function
391 */
392static void lapic_timer_broadcast(cpumask_t mask)
393{
394#ifdef CONFIG_SMP
395 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
396#endif
397}
398
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100399/*
400 * Setup the local APIC timer for this CPU. Copy the initilized values
401 * of the boot CPU and register the clock event in the framework.
402 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700403static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200404{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100405 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
406
407 memcpy(levt, &lapic_clockevent, sizeof(*levt));
408 levt->cpumask = cpumask_of_cpu(smp_processor_id());
409
410 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200411}
412
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100413/*
414 * In this function we calibrate APIC bus clocks to the external
415 * timer. Unfortunately we cannot use jiffies and the timer irq
416 * to calibrate, since some later bootup code depends on getting
417 * the first irq? Ugh.
418 *
419 * We want to do the calibration only once since we
420 * want to have local timer irqs syncron. CPUs connected
421 * by the same APIC bus have the very same bus frequency.
422 * And we want to have irqs off anyways, no accidental
423 * APIC irq that way.
424 */
425
426#define TICK_COUNT 100000000
427
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400428static int __init calibrate_APIC_clock(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200429{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100430 unsigned apic, apic_start;
431 unsigned long tsc, tsc_start;
432 int result;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200433
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100434 local_irq_disable();
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200435
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100436 /*
437 * Put whatever arbitrary (but long enough) timeout
438 * value into the APIC clock, we just want to get the
439 * counter running for calibration.
440 *
441 * No interrupt enable !
442 */
443 __setup_APIC_LVTT(250000000, 0, 0);
444
445 apic_start = apic_read(APIC_TMCCT);
446#ifdef CONFIG_X86_PM_TIMER
447 if (apic_calibrate_pmtmr && pmtmr_ioport) {
448 pmtimer_wait(5000); /* 5ms wait */
449 apic = apic_read(APIC_TMCCT);
450 result = (apic_start - apic) * 1000L / 5;
451 } else
452#endif
453 {
454 rdtscll(tsc_start);
455
456 do {
457 apic = apic_read(APIC_TMCCT);
458 rdtscll(tsc);
459 } while ((tsc - tsc_start) < TICK_COUNT &&
460 (apic_start - apic) < TICK_COUNT);
461
462 result = (apic_start - apic) * 1000L * tsc_khz /
463 (tsc - tsc_start);
464 }
465
466 local_irq_enable();
467
468 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
469
470 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
471 result / 1000 / 1000, result / 1000 % 1000);
472
473 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900474 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
475 lapic_clockevent.shift);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100476 lapic_clockevent.max_delta_ns =
477 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
478 lapic_clockevent.min_delta_ns =
479 clockevent_delta2ns(0xF, &lapic_clockevent);
480
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200481 calibration_result = (result * APIC_DIVISOR) / HZ;
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400482
483 /*
484 * Do a sanity check on the APIC calibration result
485 */
486 if (calibration_result < (1000000 / HZ)) {
487 printk(KERN_WARNING
488 "APIC frequency too slow, disabling apic timer\n");
489 return -1;
490 }
491
492 return 0;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200493}
494
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100495/*
496 * Setup the boot APIC
497 *
498 * Calibrate and verify the result.
499 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100500void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100502 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400503 * The local apic timer can be disabled via the kernel
504 * commandline or from the CPU detection code. Register the lapic
505 * timer as a dummy clock event source on SMP systems, so the
506 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100507 */
508 if (disable_apic_timer) {
509 printk(KERN_INFO "Disabling APIC timer\n");
510 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100511 if (num_possible_cpus() > 1) {
512 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100513 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100514 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100515 return;
516 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200517
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400518 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
519 "calibrating APIC timer ...\n");
520
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400521 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100522 /* No broadcast on UP ! */
523 if (num_possible_cpus() > 1)
524 setup_APIC_timer();
525 return;
526 }
527
528 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100529 * If nmi_watchdog is set to IO_APIC, we need the
530 * PIT/HPET going. Otherwise register lapic as a dummy
531 * device.
532 */
533 if (nmi_watchdog != NMI_IO_APIC)
534 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
535 else
536 printk(KERN_WARNING "APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200537 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100538
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400539 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100540 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100543void __cpuinit setup_secondary_APIC_clock(void)
544{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100545 setup_APIC_timer();
546}
547
548/*
549 * The guts of the apic timer interrupt
550 */
551static void local_apic_timer_interrupt(void)
552{
553 int cpu = smp_processor_id();
554 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
555
556 /*
557 * Normally we should not be here till LAPIC has been initialized but
558 * in some cases like kdump, its possible that there is a pending LAPIC
559 * timer interrupt from previous kernel's context and is delivered in
560 * new kernel the moment interrupts are enabled.
561 *
562 * Interrupts are enabled early and LAPIC is setup much later, hence
563 * its possible that when we get here evt->event_handler is NULL.
564 * Check for event_handler being NULL and discard the interrupt as
565 * spurious.
566 */
567 if (!evt->event_handler) {
568 printk(KERN_WARNING
569 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
570 /* Switch it off */
571 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
572 return;
573 }
574
575 /*
576 * the NMI deadlock-detector uses this.
577 */
Cyrill Gorcunov0b23e8c2008-08-18 20:45:59 +0400578#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100579 add_pda(apic_timer_irqs, 1);
Cyrill Gorcunov0b23e8c2008-08-18 20:45:59 +0400580#else
581 per_cpu(irq_stat, cpu).apic_timer_irqs++;
582#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100583
584 evt->event_handler(evt);
585}
586
587/*
588 * Local APIC timer interrupt. This is the most natural way for doing
589 * local interrupts, but local timer interrupts can be emulated by
590 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
591 *
592 * [ if a single-CPU system runs an SMP kernel then we call the local
593 * interrupt as well. Thus we cannot inline the local irq ... ]
594 */
595void smp_apic_timer_interrupt(struct pt_regs *regs)
596{
597 struct pt_regs *old_regs = set_irq_regs(regs);
598
599 /*
600 * NOTE! We'd better ACK the irq immediately,
601 * because timer handling can be slow.
602 */
603 ack_APIC_irq();
604 /*
605 * update_process_times() expects us to have done irq_enter().
606 * Besides, if we don't timer interrupts ignore the global
607 * interrupt lock, which is the WrongThing (tm) to do.
608 */
609 exit_idle();
610 irq_enter();
611 local_apic_timer_interrupt();
612 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400613
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100614 set_irq_regs(old_regs);
615}
616
617int setup_profiling_timer(unsigned int multiplier)
618{
619 return -EINVAL;
620}
621
622
623/*
624 * Local APIC start and shutdown
625 */
626
627/**
628 * clear_local_APIC - shutdown the local APIC
629 *
630 * This is called, when a CPU is disabled and before rebooting, so the state of
631 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
632 * leftovers during boot.
633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634void clear_local_APIC(void)
635{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400636 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100637 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Andi Kleend3432892008-01-30 13:33:17 +0100639 /* APIC hasn't been mapped yet */
640 if (!apic_phys)
641 return;
642
643 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200645 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * if the vector is zero. Mask LVTERR first to prevent this.
647 */
648 if (maxlvt >= 3) {
649 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100650 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 /*
653 * Careful: we have to set masks only first to deassert
654 * any level-triggered sources.
655 */
656 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100657 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100659 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100661 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (maxlvt >= 4) {
663 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100664 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400667 /* lets not touch this if we didn't frob it */
668#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
669 if (maxlvt >= 5) {
670 v = apic_read(APIC_LVTTHMR);
671 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
672 }
673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Clean APIC state for other OSs:
676 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100677 apic_write(APIC_LVTT, APIC_LVT_MASKED);
678 apic_write(APIC_LVT0, APIC_LVT_MASKED);
679 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100681 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100683 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400684
685 /* Integrated APIC (!82489DX) ? */
686 if (lapic_is_integrated()) {
687 if (maxlvt > 3)
688 /* Clear ESR due to Pentium errata 3AP and 11AP */
689 apic_write(APIC_ESR, 0);
690 apic_read(APIC_ESR);
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100694/**
695 * disable_local_APIC - clear and disable the local APIC
696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697void disable_local_APIC(void)
698{
699 unsigned int value;
700
701 clear_local_APIC();
702
703 /*
704 * Disable APIC (implies clearing of registers
705 * for 82489DX!).
706 */
707 value = apic_read(APIC_SPIV);
708 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100709 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400710
711#ifdef CONFIG_X86_32
712 /*
713 * When LAPIC was disabled by the BIOS and enabled by the kernel,
714 * restore the disabled state.
715 */
716 if (enabled_via_apicbase) {
717 unsigned int l, h;
718
719 rdmsr(MSR_IA32_APICBASE, l, h);
720 l &= ~MSR_IA32_APICBASE_ENABLE;
721 wrmsr(MSR_IA32_APICBASE, l, h);
722 }
723#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400726/*
727 * If Linux enabled the LAPIC against the BIOS default disable it down before
728 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
729 * not power-off. Additionally clear all LVT entries before disable_local_APIC
730 * for the case where Linux didn't enable the LAPIC.
731 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700732void lapic_shutdown(void)
733{
734 unsigned long flags;
735
736 if (!cpu_has_apic)
737 return;
738
739 local_irq_save(flags);
740
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400741#ifdef CONFIG_X86_32
742 if (!enabled_via_apicbase)
743 clear_local_APIC();
744 else
745#endif
746 disable_local_APIC();
747
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700748
749 local_irq_restore(flags);
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * This is to verify that we're looking at a real local APIC.
754 * Check these against your board if the CPUs aren't getting
755 * started for no apparent reason.
756 */
757int __init verify_local_APIC(void)
758{
759 unsigned int reg0, reg1;
760
761 /*
762 * The version register is read-only in a real APIC.
763 */
764 reg0 = apic_read(APIC_LVR);
765 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
766 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
767 reg1 = apic_read(APIC_LVR);
768 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
769
770 /*
771 * The two version reads above should print the same
772 * numbers. If the second one is different, then we
773 * poke at a non-APIC.
774 */
775 if (reg1 != reg0)
776 return 0;
777
778 /*
779 * Check if the version looks reasonably.
780 */
781 reg1 = GET_APIC_VERSION(reg0);
782 if (reg1 == 0x00 || reg1 == 0xff)
783 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100784 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (reg1 < 0x02 || reg1 == 0xff)
786 return 0;
787
788 /*
789 * The ID register is read/write in a real APIC.
790 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700791 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
793 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700794 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
796 apic_write(APIC_ID, reg0);
797 if (reg1 != (reg0 ^ APIC_ID_MASK))
798 return 0;
799
800 /*
801 * The next two are just to see if we have sane values.
802 * They're only really relevant if we're in Virtual Wire
803 * compatibility mode, but most boxes are anymore.
804 */
805 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100806 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 reg1 = apic_read(APIC_LVT1);
808 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
809
810 return 1;
811}
812
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100813/**
814 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816void __init sync_Arb_IDs(void)
817{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +0200818 /*
819 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
820 * needed on AMD.
821 */
822 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return;
824
825 /*
826 * Wait for idle.
827 */
828 apic_wait_icr_idle();
829
830 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +0400831 apic_write(APIC_ICR, APIC_DEST_ALLINC |
832 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/*
836 * An initial setup of the virtual wire mode.
837 */
838void __init init_bsp_APIC(void)
839{
Andi Kleen11a8e772006-01-11 22:46:51 +0100840 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /*
843 * Don't do the setup now if we have a SMP BIOS as the
844 * through-I/O-APIC virtual wire mode might be active.
845 */
846 if (smp_found_config || !cpu_has_apic)
847 return;
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * Do not trust the local APIC being empty at bootup.
851 */
852 clear_local_APIC();
853
854 /*
855 * Enable APIC.
856 */
857 value = apic_read(APIC_SPIV);
858 value &= ~APIC_VECTOR_MASK;
859 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +0400860
861#ifdef CONFIG_X86_32
862 /* This bit is reserved on P4/Xeon and should be cleared */
863 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
864 (boot_cpu_data.x86 == 15))
865 value &= ~APIC_SPIV_FOCUS_DISABLED;
866 else
867#endif
868 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100870 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /*
873 * Set up the virtual wire mode.
874 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100875 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +0400877 if (!lapic_is_integrated()) /* 82489DX */
878 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +0100879 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +0400882static void __cpuinit lapic_setup_esr(void)
883{
884 unsigned long oldvalue, value, maxlvt;
885 if (lapic_is_integrated() && !esr_disable) {
886 if (esr_disable) {
887 /*
888 * Something untraceable is creating bad interrupts on
889 * secondary quads ... for the moment, just leave the
890 * ESR disabled - we can't do anything useful with the
891 * errors anyway - mbligh
892 */
893 printk(KERN_INFO "Leaving ESR disabled.\n");
894 return;
895 }
896 /* !82489DX */
897 maxlvt = lapic_get_maxlvt();
898 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
899 apic_write(APIC_ESR, 0);
900 oldvalue = apic_read(APIC_ESR);
901
902 /* enables sending errors */
903 value = ERROR_APIC_VECTOR;
904 apic_write(APIC_LVTERR, value);
905 /*
906 * spec says clear errors after enabling vector.
907 */
908 if (maxlvt > 3)
909 apic_write(APIC_ESR, 0);
910 value = apic_read(APIC_ESR);
911 if (value != oldvalue)
912 apic_printk(APIC_VERBOSE, "ESR value before enabling "
913 "vector: 0x%08lx after: 0x%08lx\n",
914 oldvalue, value);
915 } else {
916 printk(KERN_INFO "No ESR for 82489DX.\n");
917 }
918}
919
920
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100921/**
922 * setup_local_APIC - setup the local APIC
923 */
924void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Andi Kleen739f33b2008-01-30 13:30:40 +0100926 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100927 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Jack Steinerac23d4e2008-03-28 14:12:16 -0500929 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Andi Kleenfe7414a2006-09-26 10:52:30 +0200932 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /*
935 * Double-check whether this APIC is really registered.
936 * This is meaningless in clustered apic mode, so we skip it.
937 */
938 if (!apic_id_registered())
939 BUG();
940
941 /*
942 * Intel recommends to set DFR, LDR and TPR before enabling
943 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
944 * document number 292116). So here it goes...
945 */
946 init_apic_ldr();
947
948 /*
949 * Set Task Priority to 'accept all'. We never change this
950 * later on.
951 */
952 value = apic_read(APIC_TASKPRI);
953 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100954 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100957 * After a crash, we no longer service the interrupts and a pending
958 * interrupt from previous kernel might still have ISR bit set.
959 *
960 * Most probably by now CPU has serviced that pending interrupt and
961 * it might not have done the ack_APIC_irq() because it thought,
962 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
963 * does not clear the ISR bit and cpu thinks it has already serivced
964 * the interrupt. Hence a vector might get locked. It was noticed
965 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
966 */
967 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
968 value = apic_read(APIC_ISR + i*0x10);
969 for (j = 31; j >= 0; j--) {
970 if (value & (1<<j))
971 ack_APIC_irq();
972 }
973 }
974
975 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * Now that we are all set up, enable the APIC
977 */
978 value = apic_read(APIC_SPIV);
979 value &= ~APIC_VECTOR_MASK;
980 /*
981 * Enable APIC
982 */
983 value |= APIC_SPIV_APIC_ENABLED;
984
Andi Kleen3f14c742006-09-26 10:52:29 +0200985 /* We always use processor focus */
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /*
988 * Set spurious IRQ vector
989 */
990 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100991 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /*
994 * Set up LVT0, LVT1:
995 *
996 * set up through-local-APIC on the BP's LINT0. This is not
997 * strictly necessary in pure symmetric-IO mode, but sometimes
998 * we delegate interrupts to the 8259A.
999 */
1000 /*
1001 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1002 */
1003 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +02001004 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001006 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1007 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else {
1009 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001010 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1011 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001013 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /*
1016 * only the BP should see the LINT1 NMI signal, obviously.
1017 */
1018 if (!smp_processor_id())
1019 value = APIC_DM_NMI;
1020 else
1021 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +01001022 apic_write(APIC_LVT1, value);
Jack Steinerac23d4e2008-03-28 14:12:16 -05001023 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Andi Kleen739f33b2008-01-30 13:30:40 +01001026void __cpuinit end_local_APIC_setup(void)
1027{
1028 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001029
1030#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001031 {
1032 unsigned int value;
1033 /* Disable the local apic timer */
1034 value = apic_read(APIC_LVTT);
1035 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1036 apic_write(APIC_LVTT, value);
1037 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001038#endif
1039
Don Zickusf2802e72006-09-26 10:52:26 +02001040 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 apic_pm_activate();
1042}
1043
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001044void check_x2apic(void)
1045{
1046 int msr, msr2;
1047
1048 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1049
1050 if (msr & X2APIC_ENABLE) {
1051 printk("x2apic enabled by BIOS, switching to x2apic ops\n");
1052 x2apic_preenabled = x2apic = 1;
1053 apic_ops = &x2apic_ops;
1054 }
1055}
1056
1057void enable_x2apic(void)
1058{
1059 int msr, msr2;
1060
1061 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1062 if (!(msr & X2APIC_ENABLE)) {
1063 printk("Enabling x2apic\n");
1064 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1065 }
1066}
1067
1068void enable_IR_x2apic(void)
1069{
1070#ifdef CONFIG_INTR_REMAP
1071 int ret;
1072 unsigned long flags;
1073
1074 if (!cpu_has_x2apic)
1075 return;
1076
1077 if (!x2apic_preenabled && disable_x2apic) {
1078 printk(KERN_INFO
1079 "Skipped enabling x2apic and Interrupt-remapping "
1080 "because of nox2apic\n");
1081 return;
1082 }
1083
1084 if (x2apic_preenabled && disable_x2apic)
1085 panic("Bios already enabled x2apic, can't enforce nox2apic");
1086
1087 if (!x2apic_preenabled && skip_ioapic_setup) {
1088 printk(KERN_INFO
1089 "Skipped enabling x2apic and Interrupt-remapping "
1090 "because of skipping io-apic setup\n");
1091 return;
1092 }
1093
1094 ret = dmar_table_init();
1095 if (ret) {
1096 printk(KERN_INFO
1097 "dmar_table_init() failed with %d:\n", ret);
1098
1099 if (x2apic_preenabled)
1100 panic("x2apic enabled by bios. But IR enabling failed");
1101 else
1102 printk(KERN_INFO
1103 "Not enabling x2apic,Intr-remapping\n");
1104 return;
1105 }
1106
1107 local_irq_save(flags);
1108 mask_8259A();
1109 save_mask_IO_APIC_setup();
1110
1111 ret = enable_intr_remapping(1);
1112
1113 if (ret && x2apic_preenabled) {
1114 local_irq_restore(flags);
1115 panic("x2apic enabled by bios. But IR enabling failed");
1116 }
1117
1118 if (ret)
1119 goto end;
1120
1121 if (!x2apic) {
1122 x2apic = 1;
1123 apic_ops = &x2apic_ops;
1124 enable_x2apic();
1125 }
1126end:
1127 if (ret)
1128 /*
1129 * IR enabling failed
1130 */
1131 restore_IO_APIC_setup();
1132 else
1133 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1134
1135 unmask_8259A();
1136 local_irq_restore(flags);
1137
1138 if (!ret) {
1139 if (!x2apic_preenabled)
1140 printk(KERN_INFO
1141 "Enabled x2apic and interrupt-remapping\n");
1142 else
1143 printk(KERN_INFO
1144 "Enabled Interrupt-remapping\n");
1145 } else
1146 printk(KERN_ERR
1147 "Failed to enable Interrupt-remapping and x2apic\n");
1148#else
1149 if (!cpu_has_x2apic)
1150 return;
1151
1152 if (x2apic_preenabled)
1153 panic("x2apic enabled prior OS handover,"
1154 " enable CONFIG_INTR_REMAP");
1155
1156 printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1157 " and x2apic\n");
1158#endif
1159
1160 return;
1161}
1162
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001163/*
1164 * Detect and enable local APICs on non-SMP boards.
1165 * Original code written by Keir Fraser.
1166 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1167 * not correctly set up (usually the APIC timer won't work etc.)
1168 */
1169static int __init detect_init_APIC(void)
1170{
1171 if (!cpu_has_apic) {
1172 printk(KERN_INFO "No local APIC present\n");
1173 return -1;
1174 }
1175
1176 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001177 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001178 return 0;
1179}
1180
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001181void __init early_init_lapic_mapping(void)
1182{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001183 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001184
1185 /*
1186 * If no local APIC can be found then go out
1187 * : it means there is no mpatable and MADT
1188 */
1189 if (!smp_found_config)
1190 return;
1191
Thomas Gleixner431ee792008-05-12 15:43:35 +02001192 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001193
Thomas Gleixner431ee792008-05-12 15:43:35 +02001194 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001195 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001196 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001197
1198 /*
1199 * Fetch the APIC ID of the BSP in case we have a
1200 * default configuration (or the MP table is broken).
1201 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001202 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001203}
1204
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001205/**
1206 * init_apic_mappings - initialize APIC mappings
1207 */
1208void __init init_apic_mappings(void)
1209{
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001210 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001211 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001212 return;
1213 }
1214
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001215 /*
1216 * If no local APIC can be found then set up a fake all
1217 * zeroes page to simulate the local APIC and another
1218 * one for the IO-APIC.
1219 */
1220 if (!smp_found_config && detect_init_APIC()) {
1221 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1222 apic_phys = __pa(apic_phys);
1223 } else
1224 apic_phys = mp_lapic_addr;
1225
1226 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1227 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1228 APIC_BASE, apic_phys);
1229
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001230 /*
1231 * Fetch the APIC ID of the BSP in case we have a
1232 * default configuration (or the MP table is broken).
1233 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001234 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001235}
1236
1237/*
1238 * This initializes the IO-APIC and APIC hardware if this is
1239 * a UP kernel.
1240 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001241int apic_version[MAX_APICS];
1242
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001243int __init APIC_init_uniprocessor(void)
1244{
1245 if (disable_apic) {
1246 printk(KERN_INFO "Apic disabled\n");
1247 return -1;
1248 }
1249 if (!cpu_has_apic) {
1250 disable_apic = 1;
1251 printk(KERN_INFO "Apic disabled by BIOS\n");
1252 return -1;
1253 }
1254
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001255 enable_IR_x2apic();
1256 setup_apic_routing();
1257
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001258 verify_local_APIC();
1259
Glauber Costab5841762008-05-28 13:38:28 -03001260 connect_bsp_APIC();
1261
Jack Steinerb6df1b82008-06-19 21:51:05 -05001262 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001263 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001264
1265 setup_local_APIC();
1266
Andi Kleen739f33b2008-01-30 13:30:40 +01001267 /*
1268 * Now enable IO-APICs, actually call clear_IO_APIC
1269 * We need clear_IO_APIC before enabling vector on BP
1270 */
1271 if (!skip_ioapic_setup && nr_ioapics)
1272 enable_IO_APIC();
1273
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001274 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1275 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001276 end_local_APIC_setup();
1277
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001278 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1279 setup_IO_APIC();
1280 else
1281 nr_ioapics = 0;
1282 setup_boot_APIC_clock();
1283 check_nmi_watchdog();
1284 return 0;
1285}
1286
1287/*
1288 * Local APIC interrupts
1289 */
1290
1291/*
1292 * This interrupt should _never_ happen with our APIC/SMP architecture
1293 */
1294asmlinkage void smp_spurious_interrupt(void)
1295{
1296 unsigned int v;
1297 exit_idle();
1298 irq_enter();
1299 /*
1300 * Check if this really is a spurious interrupt and ACK it
1301 * if it is a vectored one. Just in case...
1302 * Spurious interrupts should not be ACKed.
1303 */
1304 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1305 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1306 ack_APIC_irq();
1307
1308 add_pda(irq_spurious_count, 1);
1309 irq_exit();
1310}
1311
1312/*
1313 * This interrupt should never happen with our APIC/SMP architecture
1314 */
1315asmlinkage void smp_error_interrupt(void)
1316{
1317 unsigned int v, v1;
1318
1319 exit_idle();
1320 irq_enter();
1321 /* First tickle the hardware, only then report what went on. -- REW */
1322 v = apic_read(APIC_ESR);
1323 apic_write(APIC_ESR, 0);
1324 v1 = apic_read(APIC_ESR);
1325 ack_APIC_irq();
1326 atomic_inc(&irq_err_count);
1327
1328 /* Here is what the APIC error bits mean:
1329 0: Send CS error
1330 1: Receive CS error
1331 2: Send accept error
1332 3: Receive accept error
1333 4: Reserved
1334 5: Send illegal vector
1335 6: Received illegal vector
1336 7: Illegal register address
1337 */
1338 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1339 smp_processor_id(), v , v1);
1340 irq_exit();
1341}
1342
Glauber Costab5841762008-05-28 13:38:28 -03001343/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001344 * connect_bsp_APIC - attach the APIC to the interrupt system
1345 */
Glauber Costab5841762008-05-28 13:38:28 -03001346void __init connect_bsp_APIC(void)
1347{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001348#ifdef CONFIG_X86_32
1349 if (pic_mode) {
1350 /*
1351 * Do not trust the local APIC being empty at bootup.
1352 */
1353 clear_local_APIC();
1354 /*
1355 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1356 * local APIC to INT and NMI lines.
1357 */
1358 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1359 "enabling APIC mode.\n");
1360 outb(0x70, 0x22);
1361 outb(0x01, 0x23);
1362 }
1363#endif
Glauber Costab5841762008-05-28 13:38:28 -03001364 enable_apic_mode();
1365}
1366
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001367/**
1368 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1369 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1370 *
1371 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1372 * APIC is disabled.
1373 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001374void disconnect_bsp_APIC(int virt_wire_setup)
1375{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001376 unsigned int value;
1377
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001378#ifdef CONFIG_X86_32
1379 if (pic_mode) {
1380 /*
1381 * Put the board back into PIC mode (has an effect only on
1382 * certain older boards). Note that APIC interrupts, including
1383 * IPIs, won't work beyond this point! The only exception are
1384 * INIT IPIs.
1385 */
1386 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1387 "entering PIC mode.\n");
1388 outb(0x70, 0x22);
1389 outb(0x00, 0x23);
1390 return;
1391 }
1392#endif
1393
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001394 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001395
1396 /* For the spurious interrupt use vector F, and enable it */
1397 value = apic_read(APIC_SPIV);
1398 value &= ~APIC_VECTOR_MASK;
1399 value |= APIC_SPIV_APIC_ENABLED;
1400 value |= 0xf;
1401 apic_write(APIC_SPIV, value);
1402
1403 if (!virt_wire_setup) {
1404 /*
1405 * For LVT0 make it edge triggered, active high,
1406 * external and enabled
1407 */
1408 value = apic_read(APIC_LVT0);
1409 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1410 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1411 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1412 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1413 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1414 apic_write(APIC_LVT0, value);
1415 } else {
1416 /* Disable LVT0 */
1417 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1418 }
1419
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001420 /*
1421 * For LVT1 make it edge triggered, active high,
1422 * nmi and enabled
1423 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001424 value = apic_read(APIC_LVT1);
1425 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1426 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1427 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1428 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1429 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1430 apic_write(APIC_LVT1, value);
1431}
1432
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001433void __cpuinit generic_processor_info(int apicid, int version)
1434{
1435 int cpu;
1436 cpumask_t tmp_map;
1437
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001438 /*
1439 * Validate version
1440 */
1441 if (version == 0x0) {
1442 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1443 "fixing up to 0x10. (tell your hw vendor)\n",
1444 version);
1445 version = 0x10;
1446 }
1447 apic_version[apicid] = version;
1448
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001449 if (num_processors >= NR_CPUS) {
1450 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001451 " Processor ignored.\n", NR_CPUS);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001452 return;
1453 }
1454
1455 num_processors++;
1456 cpus_complement(tmp_map, cpu_present_map);
1457 cpu = first_cpu(tmp_map);
1458
1459 physid_set(apicid, phys_cpu_present_map);
1460 if (apicid == boot_cpu_physical_apicid) {
1461 /*
1462 * x86_bios_cpu_apicid is required to have processors listed
1463 * in same order as logical cpu numbers. Hence the first
1464 * entry is BSP, and so on.
1465 */
1466 cpu = 0;
1467 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001468 if (apicid > max_physical_apicid)
1469 max_physical_apicid = apicid;
1470
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001471#ifdef CONFIG_X86_32
1472 /*
1473 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1474 * but we need to work other dependencies like SMP_SUSPEND etc
1475 * before this can be done without some confusion.
1476 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1477 * - Ashok Raj <ashok.raj@intel.com>
1478 */
1479 if (max_physical_apicid >= 8) {
1480 switch (boot_cpu_data.x86_vendor) {
1481 case X86_VENDOR_INTEL:
1482 if (!APIC_XAPIC(version)) {
1483 def_to_bigsmp = 0;
1484 break;
1485 }
1486 /* If P4 and above fall through */
1487 case X86_VENDOR_AMD:
1488 def_to_bigsmp = 1;
1489 }
1490 }
1491#endif
1492
1493#if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001494 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001495 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1496 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1497 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001498
1499 cpu_to_apicid[cpu] = apicid;
1500 bios_cpu_apicid[cpu] = apicid;
1501 } else {
1502 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1503 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1504 }
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001505#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001506
1507 cpu_set(cpu, cpu_possible_map);
1508 cpu_set(cpu, cpu_present_map);
1509}
1510
Suresh Siddha0c81c742008-07-10 11:16:48 -07001511int hard_smp_processor_id(void)
1512{
1513 return read_apic_id();
1514}
1515
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001516/*
1517 * Power management
1518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519#ifdef CONFIG_PM
1520
1521static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001522 /*
1523 * 'active' is true if the local APIC was enabled by us and
1524 * not the BIOS; this signifies that we are also responsible
1525 * for disabling it before entering apm/acpi suspend
1526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int active;
1528 /* r/w apic fields */
1529 unsigned int apic_id;
1530 unsigned int apic_taskpri;
1531 unsigned int apic_ldr;
1532 unsigned int apic_dfr;
1533 unsigned int apic_spiv;
1534 unsigned int apic_lvtt;
1535 unsigned int apic_lvtpc;
1536 unsigned int apic_lvt0;
1537 unsigned int apic_lvt1;
1538 unsigned int apic_lvterr;
1539 unsigned int apic_tmict;
1540 unsigned int apic_tdcr;
1541 unsigned int apic_thmr;
1542} apic_pm_state;
1543
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001544static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001547 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (!apic_pm_state.active)
1550 return 0;
1551
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001552 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001553
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001554 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1556 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1557 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1558 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1559 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001560 if (maxlvt >= 4)
1561 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1563 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1564 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1565 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1566 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001567#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001568 if (maxlvt >= 5)
1569 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1570#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001571
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001572 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 disable_local_APIC();
1574 local_irq_restore(flags);
1575 return 0;
1576}
1577
1578static int lapic_resume(struct sys_device *dev)
1579{
1580 unsigned int l, h;
1581 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001582 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (!apic_pm_state.active)
1585 return 0;
1586
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001587 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001590
1591#ifdef CONFIG_X86_64
1592 if (x2apic)
1593 enable_x2apic();
1594 else
1595#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07001596 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001597 /*
1598 * Make sure the APICBASE points to the right address
1599 *
1600 * FIXME! This will be wrong if we ever support suspend on
1601 * SMP! We'll need to do this as part of the CPU restore!
1602 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001603 rdmsr(MSR_IA32_APICBASE, l, h);
1604 l &= ~MSR_IA32_APICBASE_BASE;
1605 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1606 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07001607 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1610 apic_write(APIC_ID, apic_pm_state.apic_id);
1611 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1612 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1613 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1614 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1615 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1616 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001617#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001618 if (maxlvt >= 5)
1619 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1620#endif
1621 if (maxlvt >= 4)
1622 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1624 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1625 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1626 apic_write(APIC_ESR, 0);
1627 apic_read(APIC_ESR);
1628 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1629 apic_write(APIC_ESR, 0);
1630 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 0;
1635}
1636
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001637/*
1638 * This device has no shutdown method - fully functioning local APICs
1639 * are needed on every CPU up until machine_halt/restart/poweroff.
1640 */
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001643 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 .resume = lapic_resume,
1645 .suspend = lapic_suspend,
1646};
1647
1648static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001649 .id = 0,
1650 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651};
1652
Ashok Raje6982c62005-06-25 14:54:58 -07001653static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 apic_pm_state.active = 1;
1656}
1657
1658static int __init init_lapic_sysfs(void)
1659{
1660 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (!cpu_has_apic)
1663 return 0;
1664 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 error = sysdev_class_register(&lapic_sysclass);
1667 if (!error)
1668 error = sysdev_register(&device_lapic);
1669 return error;
1670}
1671device_initcall(init_lapic_sysfs);
1672
1673#else /* CONFIG_PM */
1674
1675static void apic_pm_activate(void) { }
1676
1677#endif /* CONFIG_PM */
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001680 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *
1682 * Thus far, the major user of this is IBM's Summit2 series:
1683 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001684 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * multi-chassis. Use available data to take a good guess.
1686 * If in doubt, go HPET.
1687 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001688__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 int i, clusters, zeros;
1691 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08001692 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1694
Yinghai Lu322850a2008-02-23 21:48:42 -08001695 /*
1696 * there is not this kind of box with AMD CPU yet.
1697 * Some AMD box with quadcore cpu and 8 sockets apicid
1698 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08001699 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08001700 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001701 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08001702 return 0;
1703
Mike Travis23ca4bb2008-05-12 21:21:12 +02001704 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07001705 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 for (i = 0; i < NR_CPUS; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001708 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01001709 if (bios_cpu_apicid) {
1710 id = bios_cpu_apicid[i];
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001711 }
1712 else if (i < nr_cpu_ids) {
1713 if (cpu_present(i))
1714 id = per_cpu(x86_bios_cpu_apicid, i);
1715 else
1716 continue;
1717 }
1718 else
1719 break;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (id != BAD_APICID)
1722 __set_bit(APIC_CLUSTERID(id), clustermap);
1723 }
1724
1725 /* Problem: Partially populated chassis may not have CPUs in some of
1726 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01001727 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1728 * Since clusters are allocated sequentially, count zeros only if
1729 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
1731 clusters = 0;
1732 zeros = 0;
1733 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1734 if (test_bit(i, clustermap)) {
1735 clusters += 1 + zeros;
1736 zeros = 0;
1737 } else
1738 ++zeros;
1739 }
1740
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001741 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1742 * not guaranteed to be synced between boards
1743 */
1744 if (is_vsmp_box() && clusters > 1)
1745 return 1;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001748 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * May have to revisit this when multi-core + hyperthreaded CPUs come
1750 * out, but AFAIK this will work even for them.
1751 */
1752 return (clusters > 2);
1753}
1754
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001755static __init int setup_nox2apic(char *str)
1756{
1757 disable_x2apic = 1;
1758 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_X2APIC);
1759 return 0;
1760}
1761early_param("nox2apic", setup_nox2apic);
1762
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001765 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001767static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07001770 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001771 return 0;
1772}
1773early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001775/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001776static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001777{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001778 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001779}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001780early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001782static int __init parse_lapic_timer_c2_ok(char *arg)
1783{
1784 local_apic_timer_c2_ok = 1;
1785 return 0;
1786}
1787early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1788
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001789static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001792 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001793}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001794early_param("noapictimer", parse_disable_apic_timer);
1795
1796static int __init parse_nolapic_timer(char *arg)
1797{
1798 disable_apic_timer = 1;
1799 return 0;
1800}
1801early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01001802
Andi Kleen0c3749c2006-02-03 21:51:41 +01001803static __init int setup_apicpmtimer(char *s)
1804{
1805 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001806 notsc_setup(NULL);
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001807 return 0;
Andi Kleen0c3749c2006-02-03 21:51:41 +01001808}
1809__setup("apicpmtimer", setup_apicpmtimer);
1810
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04001811static int __init apic_set_verbosity(char *arg)
1812{
1813 if (!arg) {
1814#ifdef CONFIG_X86_64
1815 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04001816 return 0;
1817#endif
1818 return -EINVAL;
1819 }
1820
1821 if (strcmp("debug", arg) == 0)
1822 apic_verbosity = APIC_DEBUG;
1823 else if (strcmp("verbose", arg) == 0)
1824 apic_verbosity = APIC_VERBOSE;
1825 else {
1826 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1827 " use apic=verbose or apic=debug\n", arg);
1828 return -EINVAL;
1829 }
1830
1831 return 0;
1832}
1833early_param("apic", apic_set_verbosity);
1834
Yinghai Lu1e934dd2008-02-22 13:37:26 -08001835static int __init lapic_insert_resource(void)
1836{
1837 if (!apic_phys)
1838 return -1;
1839
1840 /* Put local APIC into the resource map. */
1841 lapic_resource.start = apic_phys;
1842 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1843 insert_resource(&iomem_resource, &lapic_resource);
1844
1845 return 0;
1846}
1847
1848/*
1849 * need call insert after e820_reserve_resources()
1850 * that is using request_resource
1851 */
1852late_initcall(lapic_insert_resource);