blob: 4587e16f73ecf7c677e8e4aea8bec8f89e9ff3e3 [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 Gorcunov80e56092008-08-24 02:01:42 -070049/*
50 * Sanity check
51 */
52#if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
53# error SPURIOUS_APIC_VECTOR definition error
54#endif
55
Cyrill Gorcunov36fef092008-08-15 13:51:20 +020056/* Disable local APIC timer from the kernel commandline or via dmi quirk */
Thomas Gleixneraa276e12008-06-09 19:15:00 +020057static int disable_apic_timer __cpuinitdata;
Chris Wrightbc1d99c2007-10-12 23:04:23 +020058static int apic_calibrate_pmtmr __initdata;
Thomas Gleixner0e078e22008-01-30 13:30:20 +010059int disable_apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -070060int disable_x2apic;
Suresh Siddha89027d32008-07-10 11:16:56 -070061int x2apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Suresh Siddha6e1cb382008-07-10 11:16:58 -070063/* x2apic enabled before OS handover */
64int x2apic_preenabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010066/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -070067int local_apic_timer_c2_ok;
68EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
69
Yinghai Luefa25592008-08-19 20:50:36 -070070int first_system_vector = 0xfe;
71
72char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
73
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010074/*
75 * Debug level, exported for io_apic.c
76 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +010077unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010078
Alexey Starikovskiybab4b272008-05-19 19:47:03 +040079/* Have we found an MP table */
80int smp_found_config;
81
Aaron Durbin39928722006-12-07 02:14:01 +010082static struct resource lapic_resource = {
83 .name = "Local APIC",
84 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
85};
86
Thomas Gleixnerd03030e2007-10-12 23:04:06 +020087static unsigned int calibration_result;
88
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020089static int lapic_next_event(unsigned long delta,
90 struct clock_event_device *evt);
91static void lapic_timer_setup(enum clock_event_mode mode,
92 struct clock_event_device *evt);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020093static void lapic_timer_broadcast(cpumask_t mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +010094static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020095
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +040096/*
97 * The local apic timer can be used for any function which is CPU local.
98 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020099static struct clock_event_device lapic_clockevent = {
100 .name = "lapic",
101 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
102 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
103 .shift = 32,
104 .set_mode = lapic_timer_setup,
105 .set_next_event = lapic_next_event,
106 .broadcast = lapic_timer_broadcast,
107 .rating = 100,
108 .irq = -1,
109};
110static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
111
Andi Kleend3432892008-01-30 13:33:17 +0100112static unsigned long apic_phys;
113
Alexey Starikovskiy3f530702008-03-27 23:55:47 +0300114unsigned long mp_lapic_addr;
115
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100116/*
117 * Get the LAPIC version
118 */
119static inline int lapic_get_version(void)
120{
121 return GET_APIC_VERSION(apic_read(APIC_LVR));
122}
123
124/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400125 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100126 */
127static inline int lapic_is_integrated(void)
128{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400129#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100130 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400131#else
132 return APIC_INTEGRATED(lapic_get_version());
133#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100134}
135
136/*
137 * Check, whether this is a modern or a first generation APIC
138 */
139static int modern_apic(void)
140{
141 /* AMD systems use old APIC versions, so check the CPU */
142 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
143 boot_cpu_data.x86 >= 0xf)
144 return 1;
145 return lapic_get_version() >= 0x14;
146}
147
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400148/*
149 * Paravirt kernels also might be using these below ops. So we still
150 * use generic apic_read()/apic_write(), which might be pointing to different
151 * ops in PARAVIRT case.
152 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700153void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100154{
155 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
156 cpu_relax();
157}
158
Suresh Siddha1b374e42008-07-10 11:16:49 -0700159u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100160{
161 u32 send_status;
162 int timeout;
163
164 timeout = 0;
165 do {
166 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
167 if (!send_status)
168 break;
169 udelay(100);
170 } while (timeout++ < 1000);
171
172 return send_status;
173}
174
Suresh Siddha1b374e42008-07-10 11:16:49 -0700175void xapic_icr_write(u32 low, u32 id)
176{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200177 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700178 apic_write(APIC_ICR, low);
179}
180
181u64 xapic_icr_read(void)
182{
183 u32 icr1, icr2;
184
185 icr2 = apic_read(APIC_ICR2);
186 icr1 = apic_read(APIC_ICR);
187
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400188 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700189}
190
191static struct apic_ops xapic_ops = {
192 .read = native_apic_mem_read,
193 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700194 .icr_read = xapic_icr_read,
195 .icr_write = xapic_icr_write,
196 .wait_icr_idle = xapic_wait_icr_idle,
197 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
198};
199
200struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700201EXPORT_SYMBOL_GPL(apic_ops);
202
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700203static void x2apic_wait_icr_idle(void)
204{
205 /* no need to wait for icr idle in x2apic */
206 return;
207}
208
209static u32 safe_x2apic_wait_icr_idle(void)
210{
211 /* no need to wait for icr idle in x2apic */
212 return 0;
213}
214
215void x2apic_icr_write(u32 low, u32 id)
216{
217 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
218}
219
220u64 x2apic_icr_read(void)
221{
222 unsigned long val;
223
224 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
225 return val;
226}
227
228static struct apic_ops x2apic_ops = {
229 .read = native_apic_msr_read,
230 .write = native_apic_msr_write,
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700231 .icr_read = x2apic_icr_read,
232 .icr_write = x2apic_icr_write,
233 .wait_icr_idle = x2apic_wait_icr_idle,
234 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
235};
236
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100237/**
238 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
239 */
Jan Beuliche9427102008-01-30 13:31:24 +0100240void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100241{
242 unsigned int v;
243
244 /* unmask and set to NMI */
245 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200246
247 /* Level triggered for 82489DX (32bit mode) */
248 if (!lapic_is_integrated())
249 v |= APIC_LVT_LEVEL_TRIGGER;
250
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100251 apic_write(APIC_LVT0, v);
252}
253
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700254#ifdef CONFIG_X86_32
255/**
256 * get_physical_broadcast - Get number of physical broadcast IDs
257 */
258int get_physical_broadcast(void)
259{
260 return modern_apic() ? 0xff : 0xf;
261}
262#endif
263
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100264/**
265 * lapic_get_maxlvt - get the maximum number of local vector table entries
266 */
267int lapic_get_maxlvt(void)
268{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200269 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100270
271 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200272 /*
273 * - we always have APIC integrated on 64bit mode
274 * - 82489DXs do not report # of LVT entries
275 */
276 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100277}
278
279/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400280 * Local APIC timer
281 */
282
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400283/* Clock divisor */
284#ifdef CONFG_X86_64
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200285#define APIC_DIVISOR 1
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400286#else
287#define APIC_DIVISOR 16
288#endif
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200289
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100290/*
291 * This function sets up the local APIC timer, with a timeout of
292 * 'clocks' APIC bus clock. During calibration we actually call
293 * this function twice on the boot CPU, once with a bogus timeout
294 * value, second time for real. The other (noncalibrating) CPUs
295 * call this function only once, with the real, calibrated value.
296 *
297 * We do reads before writes even if unnecessary, to get around the
298 * P5 APIC double write bug.
299 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100300static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
301{
302 unsigned int lvtt_value, tmp_value;
303
304 lvtt_value = LOCAL_TIMER_VECTOR;
305 if (!oneshot)
306 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200307 if (!lapic_is_integrated())
308 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
309
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100310 if (!irqen)
311 lvtt_value |= APIC_LVT_MASKED;
312
313 apic_write(APIC_LVTT, lvtt_value);
314
315 /*
316 * Divide PICLK by 16
317 */
318 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400319 apic_write(APIC_TDCR,
320 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
321 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100322
323 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200324 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100325}
326
327/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100328 * Setup extended LVT, AMD specific (K8, family 10h)
329 *
330 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
331 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200332 *
333 * If mask=1, the LVT entry does not generate interrupts while mask=0
334 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100335 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100336
337#define APIC_EILVT_LVTOFF_MCE 0
338#define APIC_EILVT_LVTOFF_IBS 1
339
340static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100341{
Robert Richter7b83dae2008-01-30 13:30:40 +0100342 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100343 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
344
345 apic_write(reg, v);
346}
347
Robert Richter7b83dae2008-01-30 13:30:40 +0100348u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
349{
350 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
351 return APIC_EILVT_LVTOFF_MCE;
352}
353
354u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
355{
356 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
357 return APIC_EILVT_LVTOFF_IBS;
358}
Robert Richter6aa360e2008-07-23 15:28:14 +0200359EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100360
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100361/*
362 * Program the next event, relative to now
363 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200364static int lapic_next_event(unsigned long delta,
365 struct clock_event_device *evt)
366{
367 apic_write(APIC_TMICT, delta);
368 return 0;
369}
370
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100371/*
372 * Setup the lapic timer in periodic or oneshot mode
373 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200374static void lapic_timer_setup(enum clock_event_mode mode,
375 struct clock_event_device *evt)
376{
377 unsigned long flags;
378 unsigned int v;
379
380 /* Lapic used as dummy for broadcast ? */
381 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
382 return;
383
384 local_irq_save(flags);
385
386 switch (mode) {
387 case CLOCK_EVT_MODE_PERIODIC:
388 case CLOCK_EVT_MODE_ONESHOT:
389 __setup_APIC_LVTT(calibration_result,
390 mode != CLOCK_EVT_MODE_PERIODIC, 1);
391 break;
392 case CLOCK_EVT_MODE_UNUSED:
393 case CLOCK_EVT_MODE_SHUTDOWN:
394 v = apic_read(APIC_LVTT);
395 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
396 apic_write(APIC_LVTT, v);
397 break;
398 case CLOCK_EVT_MODE_RESUME:
399 /* Nothing to do here */
400 break;
401 }
402
403 local_irq_restore(flags);
404}
405
406/*
407 * Local APIC timer broadcast function
408 */
409static void lapic_timer_broadcast(cpumask_t mask)
410{
411#ifdef CONFIG_SMP
412 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
413#endif
414}
415
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100416/*
417 * Setup the local APIC timer for this CPU. Copy the initilized values
418 * of the boot CPU and register the clock event in the framework.
419 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700420static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200421{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100422 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
423
424 memcpy(levt, &lapic_clockevent, sizeof(*levt));
425 levt->cpumask = cpumask_of_cpu(smp_processor_id());
426
427 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200428}
429
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100430/*
431 * In this function we calibrate APIC bus clocks to the external
432 * timer. Unfortunately we cannot use jiffies and the timer irq
433 * to calibrate, since some later bootup code depends on getting
434 * the first irq? Ugh.
435 *
436 * We want to do the calibration only once since we
437 * want to have local timer irqs syncron. CPUs connected
438 * by the same APIC bus have the very same bus frequency.
439 * And we want to have irqs off anyways, no accidental
440 * APIC irq that way.
441 */
442
443#define TICK_COUNT 100000000
444
Cyrill Gorcunov89b3b1f42008-07-15 21:02:54 +0400445static int __init calibrate_APIC_clock(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200446{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100447 unsigned apic, apic_start;
448 unsigned long tsc, tsc_start;
449 int result;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200450
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100451 local_irq_disable();
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200452
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100453 /*
454 * Put whatever arbitrary (but long enough) timeout
455 * value into the APIC clock, we just want to get the
456 * counter running for calibration.
457 *
458 * No interrupt enable !
459 */
460 __setup_APIC_LVTT(250000000, 0, 0);
461
462 apic_start = apic_read(APIC_TMCCT);
463#ifdef CONFIG_X86_PM_TIMER
464 if (apic_calibrate_pmtmr && pmtmr_ioport) {
465 pmtimer_wait(5000); /* 5ms wait */
466 apic = apic_read(APIC_TMCCT);
467 result = (apic_start - apic) * 1000L / 5;
468 } else
469#endif
470 {
471 rdtscll(tsc_start);
472
473 do {
474 apic = apic_read(APIC_TMCCT);
475 rdtscll(tsc);
476 } while ((tsc - tsc_start) < TICK_COUNT &&
477 (apic_start - apic) < TICK_COUNT);
478
479 result = (apic_start - apic) * 1000L * tsc_khz /
480 (tsc - tsc_start);
481 }
482
483 local_irq_enable();
484
485 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
486
487 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
488 result / 1000 / 1000, result / 1000 % 1000);
489
490 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900491 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
492 lapic_clockevent.shift);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100493 lapic_clockevent.max_delta_ns =
494 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
495 lapic_clockevent.min_delta_ns =
496 clockevent_delta2ns(0xF, &lapic_clockevent);
497
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200498 calibration_result = (result * APIC_DIVISOR) / HZ;
Cyrill Gorcunov89b3b1f42008-07-15 21:02:54 +0400499
500 /*
501 * Do a sanity check on the APIC calibration result
502 */
503 if (calibration_result < (1000000 / HZ)) {
504 printk(KERN_WARNING
505 "APIC frequency too slow, disabling apic timer\n");
506 return -1;
507 }
508
509 return 0;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200510}
511
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100512/*
513 * Setup the boot APIC
514 *
515 * Calibrate and verify the result.
516 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100517void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100519 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400520 * The local apic timer can be disabled via the kernel
521 * commandline or from the CPU detection code. Register the lapic
522 * timer as a dummy clock event source on SMP systems, so the
523 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100524 */
525 if (disable_apic_timer) {
526 printk(KERN_INFO "Disabling APIC timer\n");
527 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100528 if (num_possible_cpus() > 1) {
529 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100530 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100531 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100532 return;
533 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200534
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400535 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
536 "calibrating APIC timer ...\n");
537
Cyrill Gorcunov89b3b1f42008-07-15 21:02:54 +0400538 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100539 /* No broadcast on UP ! */
540 if (num_possible_cpus() > 1)
541 setup_APIC_timer();
542 return;
543 }
544
545 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100546 * If nmi_watchdog is set to IO_APIC, we need the
547 * PIT/HPET going. Otherwise register lapic as a dummy
548 * device.
549 */
550 if (nmi_watchdog != NMI_IO_APIC)
551 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
552 else
553 printk(KERN_WARNING "APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200554 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100555
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400556 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100557 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100560void __cpuinit setup_secondary_APIC_clock(void)
561{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100562 setup_APIC_timer();
563}
564
565/*
566 * The guts of the apic timer interrupt
567 */
568static void local_apic_timer_interrupt(void)
569{
570 int cpu = smp_processor_id();
571 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
572
573 /*
574 * Normally we should not be here till LAPIC has been initialized but
575 * in some cases like kdump, its possible that there is a pending LAPIC
576 * timer interrupt from previous kernel's context and is delivered in
577 * new kernel the moment interrupts are enabled.
578 *
579 * Interrupts are enabled early and LAPIC is setup much later, hence
580 * its possible that when we get here evt->event_handler is NULL.
581 * Check for event_handler being NULL and discard the interrupt as
582 * spurious.
583 */
584 if (!evt->event_handler) {
585 printk(KERN_WARNING
586 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
587 /* Switch it off */
588 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
589 return;
590 }
591
592 /*
593 * the NMI deadlock-detector uses this.
594 */
Cyrill Gorcunov0b23e8c2008-08-18 20:45:59 +0400595#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100596 add_pda(apic_timer_irqs, 1);
Cyrill Gorcunov0b23e8c2008-08-18 20:45:59 +0400597#else
598 per_cpu(irq_stat, cpu).apic_timer_irqs++;
599#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100600
601 evt->event_handler(evt);
602}
603
604/*
605 * Local APIC timer interrupt. This is the most natural way for doing
606 * local interrupts, but local timer interrupts can be emulated by
607 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
608 *
609 * [ if a single-CPU system runs an SMP kernel then we call the local
610 * interrupt as well. Thus we cannot inline the local irq ... ]
611 */
612void smp_apic_timer_interrupt(struct pt_regs *regs)
613{
614 struct pt_regs *old_regs = set_irq_regs(regs);
615
616 /*
617 * NOTE! We'd better ACK the irq immediately,
618 * because timer handling can be slow.
619 */
620 ack_APIC_irq();
621 /*
622 * update_process_times() expects us to have done irq_enter().
623 * Besides, if we don't timer interrupts ignore the global
624 * interrupt lock, which is the WrongThing (tm) to do.
625 */
626 exit_idle();
627 irq_enter();
628 local_apic_timer_interrupt();
629 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400630
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100631 set_irq_regs(old_regs);
632}
633
634int setup_profiling_timer(unsigned int multiplier)
635{
636 return -EINVAL;
637}
638
639
640/*
641 * Local APIC start and shutdown
642 */
643
644/**
645 * clear_local_APIC - shutdown the local APIC
646 *
647 * This is called, when a CPU is disabled and before rebooting, so the state of
648 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
649 * leftovers during boot.
650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651void clear_local_APIC(void)
652{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400653 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100654 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Andi Kleend3432892008-01-30 13:33:17 +0100656 /* APIC hasn't been mapped yet */
657 if (!apic_phys)
658 return;
659
660 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200662 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * if the vector is zero. Mask LVTERR first to prevent this.
664 */
665 if (maxlvt >= 3) {
666 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100667 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 /*
670 * Careful: we have to set masks only first to deassert
671 * any level-triggered sources.
672 */
673 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100674 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100676 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100678 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (maxlvt >= 4) {
680 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100681 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400684 /* lets not touch this if we didn't frob it */
685#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
686 if (maxlvt >= 5) {
687 v = apic_read(APIC_LVTTHMR);
688 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
689 }
690#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Clean APIC state for other OSs:
693 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100694 apic_write(APIC_LVTT, APIC_LVT_MASKED);
695 apic_write(APIC_LVT0, APIC_LVT_MASKED);
696 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100698 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100700 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400701
702 /* Integrated APIC (!82489DX) ? */
703 if (lapic_is_integrated()) {
704 if (maxlvt > 3)
705 /* Clear ESR due to Pentium errata 3AP and 11AP */
706 apic_write(APIC_ESR, 0);
707 apic_read(APIC_ESR);
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100711/**
712 * disable_local_APIC - clear and disable the local APIC
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714void disable_local_APIC(void)
715{
716 unsigned int value;
717
718 clear_local_APIC();
719
720 /*
721 * Disable APIC (implies clearing of registers
722 * for 82489DX!).
723 */
724 value = apic_read(APIC_SPIV);
725 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100726 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400727
728#ifdef CONFIG_X86_32
729 /*
730 * When LAPIC was disabled by the BIOS and enabled by the kernel,
731 * restore the disabled state.
732 */
733 if (enabled_via_apicbase) {
734 unsigned int l, h;
735
736 rdmsr(MSR_IA32_APICBASE, l, h);
737 l &= ~MSR_IA32_APICBASE_ENABLE;
738 wrmsr(MSR_IA32_APICBASE, l, h);
739 }
740#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400743/*
744 * If Linux enabled the LAPIC against the BIOS default disable it down before
745 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
746 * not power-off. Additionally clear all LVT entries before disable_local_APIC
747 * for the case where Linux didn't enable the LAPIC.
748 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700749void lapic_shutdown(void)
750{
751 unsigned long flags;
752
753 if (!cpu_has_apic)
754 return;
755
756 local_irq_save(flags);
757
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400758#ifdef CONFIG_X86_32
759 if (!enabled_via_apicbase)
760 clear_local_APIC();
761 else
762#endif
763 disable_local_APIC();
764
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700765
766 local_irq_restore(flags);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * This is to verify that we're looking at a real local APIC.
771 * Check these against your board if the CPUs aren't getting
772 * started for no apparent reason.
773 */
774int __init verify_local_APIC(void)
775{
776 unsigned int reg0, reg1;
777
778 /*
779 * The version register is read-only in a real APIC.
780 */
781 reg0 = apic_read(APIC_LVR);
782 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
783 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
784 reg1 = apic_read(APIC_LVR);
785 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
786
787 /*
788 * The two version reads above should print the same
789 * numbers. If the second one is different, then we
790 * poke at a non-APIC.
791 */
792 if (reg1 != reg0)
793 return 0;
794
795 /*
796 * Check if the version looks reasonably.
797 */
798 reg1 = GET_APIC_VERSION(reg0);
799 if (reg1 == 0x00 || reg1 == 0xff)
800 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100801 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (reg1 < 0x02 || reg1 == 0xff)
803 return 0;
804
805 /*
806 * The ID register is read/write in a real APIC.
807 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700808 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
810 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700811 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
813 apic_write(APIC_ID, reg0);
814 if (reg1 != (reg0 ^ APIC_ID_MASK))
815 return 0;
816
817 /*
818 * The next two are just to see if we have sane values.
819 * They're only really relevant if we're in Virtual Wire
820 * compatibility mode, but most boxes are anymore.
821 */
822 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100823 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 reg1 = apic_read(APIC_LVT1);
825 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
826
827 return 1;
828}
829
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100830/**
831 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833void __init sync_Arb_IDs(void)
834{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +0200835 /*
836 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
837 * needed on AMD.
838 */
839 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
841
842 /*
843 * Wait for idle.
844 */
845 apic_wait_icr_idle();
846
847 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +0400848 apic_write(APIC_ICR, APIC_DEST_ALLINC |
849 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*
853 * An initial setup of the virtual wire mode.
854 */
855void __init init_bsp_APIC(void)
856{
Andi Kleen11a8e772006-01-11 22:46:51 +0100857 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /*
860 * Don't do the setup now if we have a SMP BIOS as the
861 * through-I/O-APIC virtual wire mode might be active.
862 */
863 if (smp_found_config || !cpu_has_apic)
864 return;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * Do not trust the local APIC being empty at bootup.
868 */
869 clear_local_APIC();
870
871 /*
872 * Enable APIC.
873 */
874 value = apic_read(APIC_SPIV);
875 value &= ~APIC_VECTOR_MASK;
876 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +0400877
878#ifdef CONFIG_X86_32
879 /* This bit is reserved on P4/Xeon and should be cleared */
880 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
881 (boot_cpu_data.x86 == 15))
882 value &= ~APIC_SPIV_FOCUS_DISABLED;
883 else
884#endif
885 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100887 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /*
890 * Set up the virtual wire mode.
891 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100892 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +0400894 if (!lapic_is_integrated()) /* 82489DX */
895 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +0100896 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +0400899static void __cpuinit lapic_setup_esr(void)
900{
901 unsigned long oldvalue, value, maxlvt;
902 if (lapic_is_integrated() && !esr_disable) {
903 if (esr_disable) {
904 /*
905 * Something untraceable is creating bad interrupts on
906 * secondary quads ... for the moment, just leave the
907 * ESR disabled - we can't do anything useful with the
908 * errors anyway - mbligh
909 */
910 printk(KERN_INFO "Leaving ESR disabled.\n");
911 return;
912 }
913 /* !82489DX */
914 maxlvt = lapic_get_maxlvt();
915 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
916 apic_write(APIC_ESR, 0);
917 oldvalue = apic_read(APIC_ESR);
918
919 /* enables sending errors */
920 value = ERROR_APIC_VECTOR;
921 apic_write(APIC_LVTERR, value);
922 /*
923 * spec says clear errors after enabling vector.
924 */
925 if (maxlvt > 3)
926 apic_write(APIC_ESR, 0);
927 value = apic_read(APIC_ESR);
928 if (value != oldvalue)
929 apic_printk(APIC_VERBOSE, "ESR value before enabling "
930 "vector: 0x%08lx after: 0x%08lx\n",
931 oldvalue, value);
932 } else {
933 printk(KERN_INFO "No ESR for 82489DX.\n");
934 }
935}
936
937
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100938/**
939 * setup_local_APIC - setup the local APIC
940 */
941void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Andi Kleen739f33b2008-01-30 13:30:40 +0100943 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100944 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jack Steinerac23d4e2008-03-28 14:12:16 -0500946 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /*
950 * Double-check whether this APIC is really registered.
951 * This is meaningless in clustered apic mode, so we skip it.
952 */
953 if (!apic_id_registered())
954 BUG();
955
956 /*
957 * Intel recommends to set DFR, LDR and TPR before enabling
958 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
959 * document number 292116). So here it goes...
960 */
961 init_apic_ldr();
962
963 /*
964 * Set Task Priority to 'accept all'. We never change this
965 * later on.
966 */
967 value = apic_read(APIC_TASKPRI);
968 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100969 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100972 * After a crash, we no longer service the interrupts and a pending
973 * interrupt from previous kernel might still have ISR bit set.
974 *
975 * Most probably by now CPU has serviced that pending interrupt and
976 * it might not have done the ack_APIC_irq() because it thought,
977 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
978 * does not clear the ISR bit and cpu thinks it has already serivced
979 * the interrupt. Hence a vector might get locked. It was noticed
980 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
981 */
982 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
983 value = apic_read(APIC_ISR + i*0x10);
984 for (j = 31; j >= 0; j--) {
985 if (value & (1<<j))
986 ack_APIC_irq();
987 }
988 }
989
990 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * Now that we are all set up, enable the APIC
992 */
993 value = apic_read(APIC_SPIV);
994 value &= ~APIC_VECTOR_MASK;
995 /*
996 * Enable APIC
997 */
998 value |= APIC_SPIV_APIC_ENABLED;
999
Andi Kleen3f14c742006-09-26 10:52:29 +02001000 /* We always use processor focus */
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /*
1003 * Set spurious IRQ vector
1004 */
1005 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001006 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /*
1009 * Set up LVT0, LVT1:
1010 *
1011 * set up through-local-APIC on the BP's LINT0. This is not
1012 * strictly necessary in pure symmetric-IO mode, but sometimes
1013 * we delegate interrupts to the 8259A.
1014 */
1015 /*
1016 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1017 */
1018 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +02001019 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001021 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1022 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else {
1024 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001025 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1026 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001028 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /*
1031 * only the BP should see the LINT1 NMI signal, obviously.
1032 */
1033 if (!smp_processor_id())
1034 value = APIC_DM_NMI;
1035 else
1036 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +01001037 apic_write(APIC_LVT1, value);
Jack Steinerac23d4e2008-03-28 14:12:16 -05001038 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Andi Kleen739f33b2008-01-30 13:30:40 +01001041void __cpuinit end_local_APIC_setup(void)
1042{
1043 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001044
1045#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001046 {
1047 unsigned int value;
1048 /* Disable the local apic timer */
1049 value = apic_read(APIC_LVTT);
1050 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1051 apic_write(APIC_LVTT, value);
1052 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001053#endif
1054
Don Zickusf2802e72006-09-26 10:52:26 +02001055 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 apic_pm_activate();
1057}
1058
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001059void check_x2apic(void)
1060{
1061 int msr, msr2;
1062
1063 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1064
1065 if (msr & X2APIC_ENABLE) {
1066 printk("x2apic enabled by BIOS, switching to x2apic ops\n");
1067 x2apic_preenabled = x2apic = 1;
1068 apic_ops = &x2apic_ops;
1069 }
1070}
1071
1072void enable_x2apic(void)
1073{
1074 int msr, msr2;
1075
1076 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1077 if (!(msr & X2APIC_ENABLE)) {
1078 printk("Enabling x2apic\n");
1079 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1080 }
1081}
1082
1083void enable_IR_x2apic(void)
1084{
1085#ifdef CONFIG_INTR_REMAP
1086 int ret;
1087 unsigned long flags;
1088
1089 if (!cpu_has_x2apic)
1090 return;
1091
1092 if (!x2apic_preenabled && disable_x2apic) {
1093 printk(KERN_INFO
1094 "Skipped enabling x2apic and Interrupt-remapping "
1095 "because of nox2apic\n");
1096 return;
1097 }
1098
1099 if (x2apic_preenabled && disable_x2apic)
1100 panic("Bios already enabled x2apic, can't enforce nox2apic");
1101
1102 if (!x2apic_preenabled && skip_ioapic_setup) {
1103 printk(KERN_INFO
1104 "Skipped enabling x2apic and Interrupt-remapping "
1105 "because of skipping io-apic setup\n");
1106 return;
1107 }
1108
1109 ret = dmar_table_init();
1110 if (ret) {
1111 printk(KERN_INFO
1112 "dmar_table_init() failed with %d:\n", ret);
1113
1114 if (x2apic_preenabled)
1115 panic("x2apic enabled by bios. But IR enabling failed");
1116 else
1117 printk(KERN_INFO
1118 "Not enabling x2apic,Intr-remapping\n");
1119 return;
1120 }
1121
1122 local_irq_save(flags);
1123 mask_8259A();
1124 save_mask_IO_APIC_setup();
1125
1126 ret = enable_intr_remapping(1);
1127
1128 if (ret && x2apic_preenabled) {
1129 local_irq_restore(flags);
1130 panic("x2apic enabled by bios. But IR enabling failed");
1131 }
1132
1133 if (ret)
1134 goto end;
1135
1136 if (!x2apic) {
1137 x2apic = 1;
1138 apic_ops = &x2apic_ops;
1139 enable_x2apic();
1140 }
1141end:
1142 if (ret)
1143 /*
1144 * IR enabling failed
1145 */
1146 restore_IO_APIC_setup();
1147 else
1148 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1149
1150 unmask_8259A();
1151 local_irq_restore(flags);
1152
1153 if (!ret) {
1154 if (!x2apic_preenabled)
1155 printk(KERN_INFO
1156 "Enabled x2apic and interrupt-remapping\n");
1157 else
1158 printk(KERN_INFO
1159 "Enabled Interrupt-remapping\n");
1160 } else
1161 printk(KERN_ERR
1162 "Failed to enable Interrupt-remapping and x2apic\n");
1163#else
1164 if (!cpu_has_x2apic)
1165 return;
1166
1167 if (x2apic_preenabled)
1168 panic("x2apic enabled prior OS handover,"
1169 " enable CONFIG_INTR_REMAP");
1170
1171 printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1172 " and x2apic\n");
1173#endif
1174
1175 return;
1176}
1177
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001178/*
1179 * Detect and enable local APICs on non-SMP boards.
1180 * Original code written by Keir Fraser.
1181 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1182 * not correctly set up (usually the APIC timer won't work etc.)
1183 */
1184static int __init detect_init_APIC(void)
1185{
1186 if (!cpu_has_apic) {
1187 printk(KERN_INFO "No local APIC present\n");
1188 return -1;
1189 }
1190
1191 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001192 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001193 return 0;
1194}
1195
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001196void __init early_init_lapic_mapping(void)
1197{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001198 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001199
1200 /*
1201 * If no local APIC can be found then go out
1202 * : it means there is no mpatable and MADT
1203 */
1204 if (!smp_found_config)
1205 return;
1206
Thomas Gleixner431ee792008-05-12 15:43:35 +02001207 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001208
Thomas Gleixner431ee792008-05-12 15:43:35 +02001209 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001210 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001211 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001212
1213 /*
1214 * Fetch the APIC ID of the BSP in case we have a
1215 * default configuration (or the MP table is broken).
1216 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001217 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001218}
1219
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001220/**
1221 * init_apic_mappings - initialize APIC mappings
1222 */
1223void __init init_apic_mappings(void)
1224{
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001225 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001226 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001227 return;
1228 }
1229
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001230 /*
1231 * If no local APIC can be found then set up a fake all
1232 * zeroes page to simulate the local APIC and another
1233 * one for the IO-APIC.
1234 */
1235 if (!smp_found_config && detect_init_APIC()) {
1236 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1237 apic_phys = __pa(apic_phys);
1238 } else
1239 apic_phys = mp_lapic_addr;
1240
1241 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1242 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1243 APIC_BASE, apic_phys);
1244
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001245 /*
1246 * Fetch the APIC ID of the BSP in case we have a
1247 * default configuration (or the MP table is broken).
1248 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001249 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001250}
1251
1252/*
1253 * This initializes the IO-APIC and APIC hardware if this is
1254 * a UP kernel.
1255 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001256int apic_version[MAX_APICS];
1257
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001258int __init APIC_init_uniprocessor(void)
1259{
1260 if (disable_apic) {
1261 printk(KERN_INFO "Apic disabled\n");
1262 return -1;
1263 }
1264 if (!cpu_has_apic) {
1265 disable_apic = 1;
1266 printk(KERN_INFO "Apic disabled by BIOS\n");
1267 return -1;
1268 }
1269
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001270 enable_IR_x2apic();
1271 setup_apic_routing();
1272
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001273 verify_local_APIC();
1274
Glauber Costab5841762008-05-28 13:38:28 -03001275 connect_bsp_APIC();
1276
Jack Steinerb6df1b82008-06-19 21:51:05 -05001277 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001278 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001279
1280 setup_local_APIC();
1281
Andi Kleen739f33b2008-01-30 13:30:40 +01001282 /*
1283 * Now enable IO-APICs, actually call clear_IO_APIC
1284 * We need clear_IO_APIC before enabling vector on BP
1285 */
1286 if (!skip_ioapic_setup && nr_ioapics)
1287 enable_IO_APIC();
1288
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001289 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1290 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001291 end_local_APIC_setup();
1292
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001293 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1294 setup_IO_APIC();
1295 else
1296 nr_ioapics = 0;
1297 setup_boot_APIC_clock();
1298 check_nmi_watchdog();
1299 return 0;
1300}
1301
1302/*
1303 * Local APIC interrupts
1304 */
1305
1306/*
1307 * This interrupt should _never_ happen with our APIC/SMP architecture
1308 */
1309asmlinkage void smp_spurious_interrupt(void)
1310{
1311 unsigned int v;
1312 exit_idle();
1313 irq_enter();
1314 /*
1315 * Check if this really is a spurious interrupt and ACK it
1316 * if it is a vectored one. Just in case...
1317 * Spurious interrupts should not be ACKed.
1318 */
1319 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1320 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1321 ack_APIC_irq();
1322
1323 add_pda(irq_spurious_count, 1);
1324 irq_exit();
1325}
1326
1327/*
1328 * This interrupt should never happen with our APIC/SMP architecture
1329 */
1330asmlinkage void smp_error_interrupt(void)
1331{
1332 unsigned int v, v1;
1333
1334 exit_idle();
1335 irq_enter();
1336 /* First tickle the hardware, only then report what went on. -- REW */
1337 v = apic_read(APIC_ESR);
1338 apic_write(APIC_ESR, 0);
1339 v1 = apic_read(APIC_ESR);
1340 ack_APIC_irq();
1341 atomic_inc(&irq_err_count);
1342
1343 /* Here is what the APIC error bits mean:
1344 0: Send CS error
1345 1: Receive CS error
1346 2: Send accept error
1347 3: Receive accept error
1348 4: Reserved
1349 5: Send illegal vector
1350 6: Received illegal vector
1351 7: Illegal register address
1352 */
1353 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1354 smp_processor_id(), v , v1);
1355 irq_exit();
1356}
1357
Glauber Costab5841762008-05-28 13:38:28 -03001358/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001359 * connect_bsp_APIC - attach the APIC to the interrupt system
1360 */
Glauber Costab5841762008-05-28 13:38:28 -03001361void __init connect_bsp_APIC(void)
1362{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001363#ifdef CONFIG_X86_32
1364 if (pic_mode) {
1365 /*
1366 * Do not trust the local APIC being empty at bootup.
1367 */
1368 clear_local_APIC();
1369 /*
1370 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1371 * local APIC to INT and NMI lines.
1372 */
1373 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1374 "enabling APIC mode.\n");
1375 outb(0x70, 0x22);
1376 outb(0x01, 0x23);
1377 }
1378#endif
Glauber Costab5841762008-05-28 13:38:28 -03001379 enable_apic_mode();
1380}
1381
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001382/**
1383 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1384 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1385 *
1386 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1387 * APIC is disabled.
1388 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001389void disconnect_bsp_APIC(int virt_wire_setup)
1390{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001391 unsigned int value;
1392
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001393#ifdef CONFIG_X86_32
1394 if (pic_mode) {
1395 /*
1396 * Put the board back into PIC mode (has an effect only on
1397 * certain older boards). Note that APIC interrupts, including
1398 * IPIs, won't work beyond this point! The only exception are
1399 * INIT IPIs.
1400 */
1401 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1402 "entering PIC mode.\n");
1403 outb(0x70, 0x22);
1404 outb(0x00, 0x23);
1405 return;
1406 }
1407#endif
1408
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001409 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001410
1411 /* For the spurious interrupt use vector F, and enable it */
1412 value = apic_read(APIC_SPIV);
1413 value &= ~APIC_VECTOR_MASK;
1414 value |= APIC_SPIV_APIC_ENABLED;
1415 value |= 0xf;
1416 apic_write(APIC_SPIV, value);
1417
1418 if (!virt_wire_setup) {
1419 /*
1420 * For LVT0 make it edge triggered, active high,
1421 * external and enabled
1422 */
1423 value = apic_read(APIC_LVT0);
1424 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1425 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1426 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1427 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1428 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1429 apic_write(APIC_LVT0, value);
1430 } else {
1431 /* Disable LVT0 */
1432 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1433 }
1434
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001435 /*
1436 * For LVT1 make it edge triggered, active high,
1437 * nmi and enabled
1438 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001439 value = apic_read(APIC_LVT1);
1440 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1441 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1442 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1443 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1444 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1445 apic_write(APIC_LVT1, value);
1446}
1447
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001448void __cpuinit generic_processor_info(int apicid, int version)
1449{
1450 int cpu;
1451 cpumask_t tmp_map;
1452
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001453 /*
1454 * Validate version
1455 */
1456 if (version == 0x0) {
1457 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1458 "fixing up to 0x10. (tell your hw vendor)\n",
1459 version);
1460 version = 0x10;
1461 }
1462 apic_version[apicid] = version;
1463
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001464 if (num_processors >= NR_CPUS) {
1465 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001466 " Processor ignored.\n", NR_CPUS);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001467 return;
1468 }
1469
1470 num_processors++;
1471 cpus_complement(tmp_map, cpu_present_map);
1472 cpu = first_cpu(tmp_map);
1473
1474 physid_set(apicid, phys_cpu_present_map);
1475 if (apicid == boot_cpu_physical_apicid) {
1476 /*
1477 * x86_bios_cpu_apicid is required to have processors listed
1478 * in same order as logical cpu numbers. Hence the first
1479 * entry is BSP, and so on.
1480 */
1481 cpu = 0;
1482 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001483 if (apicid > max_physical_apicid)
1484 max_physical_apicid = apicid;
1485
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001486#ifdef CONFIG_X86_32
1487 /*
1488 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1489 * but we need to work other dependencies like SMP_SUSPEND etc
1490 * before this can be done without some confusion.
1491 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1492 * - Ashok Raj <ashok.raj@intel.com>
1493 */
1494 if (max_physical_apicid >= 8) {
1495 switch (boot_cpu_data.x86_vendor) {
1496 case X86_VENDOR_INTEL:
1497 if (!APIC_XAPIC(version)) {
1498 def_to_bigsmp = 0;
1499 break;
1500 }
1501 /* If P4 and above fall through */
1502 case X86_VENDOR_AMD:
1503 def_to_bigsmp = 1;
1504 }
1505 }
1506#endif
1507
1508#if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001509 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001510 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1511 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1512 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001513
1514 cpu_to_apicid[cpu] = apicid;
1515 bios_cpu_apicid[cpu] = apicid;
1516 } else {
1517 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1518 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1519 }
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001520#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001521
1522 cpu_set(cpu, cpu_possible_map);
1523 cpu_set(cpu, cpu_present_map);
1524}
1525
Suresh Siddha0c81c742008-07-10 11:16:48 -07001526int hard_smp_processor_id(void)
1527{
1528 return read_apic_id();
1529}
1530
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001531/*
1532 * Power management
1533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534#ifdef CONFIG_PM
1535
1536static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001537 /*
1538 * 'active' is true if the local APIC was enabled by us and
1539 * not the BIOS; this signifies that we are also responsible
1540 * for disabling it before entering apm/acpi suspend
1541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 int active;
1543 /* r/w apic fields */
1544 unsigned int apic_id;
1545 unsigned int apic_taskpri;
1546 unsigned int apic_ldr;
1547 unsigned int apic_dfr;
1548 unsigned int apic_spiv;
1549 unsigned int apic_lvtt;
1550 unsigned int apic_lvtpc;
1551 unsigned int apic_lvt0;
1552 unsigned int apic_lvt1;
1553 unsigned int apic_lvterr;
1554 unsigned int apic_tmict;
1555 unsigned int apic_tdcr;
1556 unsigned int apic_thmr;
1557} apic_pm_state;
1558
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001559static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001562 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 if (!apic_pm_state.active)
1565 return 0;
1566
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001567 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001568
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001569 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1571 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1572 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1573 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1574 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001575 if (maxlvt >= 4)
1576 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1578 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1579 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1580 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1581 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001582#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001583 if (maxlvt >= 5)
1584 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1585#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001586
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001587 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 disable_local_APIC();
1589 local_irq_restore(flags);
1590 return 0;
1591}
1592
1593static int lapic_resume(struct sys_device *dev)
1594{
1595 unsigned int l, h;
1596 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001597 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (!apic_pm_state.active)
1600 return 0;
1601
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001602 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001605
1606#ifdef CONFIG_X86_64
1607 if (x2apic)
1608 enable_x2apic();
1609 else
1610#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07001611 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001612 /*
1613 * Make sure the APICBASE points to the right address
1614 *
1615 * FIXME! This will be wrong if we ever support suspend on
1616 * SMP! We'll need to do this as part of the CPU restore!
1617 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001618 rdmsr(MSR_IA32_APICBASE, l, h);
1619 l &= ~MSR_IA32_APICBASE_BASE;
1620 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1621 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07001622 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1625 apic_write(APIC_ID, apic_pm_state.apic_id);
1626 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1627 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1628 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1629 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1630 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1631 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001632#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001633 if (maxlvt >= 5)
1634 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1635#endif
1636 if (maxlvt >= 4)
1637 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1639 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1640 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1641 apic_write(APIC_ESR, 0);
1642 apic_read(APIC_ESR);
1643 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1644 apic_write(APIC_ESR, 0);
1645 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650}
1651
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001652/*
1653 * This device has no shutdown method - fully functioning local APICs
1654 * are needed on every CPU up until machine_halt/restart/poweroff.
1655 */
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001658 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 .resume = lapic_resume,
1660 .suspend = lapic_suspend,
1661};
1662
1663static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001664 .id = 0,
1665 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666};
1667
Ashok Raje6982c62005-06-25 14:54:58 -07001668static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 apic_pm_state.active = 1;
1671}
1672
1673static int __init init_lapic_sysfs(void)
1674{
1675 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (!cpu_has_apic)
1678 return 0;
1679 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 error = sysdev_class_register(&lapic_sysclass);
1682 if (!error)
1683 error = sysdev_register(&device_lapic);
1684 return error;
1685}
1686device_initcall(init_lapic_sysfs);
1687
1688#else /* CONFIG_PM */
1689
1690static void apic_pm_activate(void) { }
1691
1692#endif /* CONFIG_PM */
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001695 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 *
1697 * Thus far, the major user of this is IBM's Summit2 series:
1698 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001699 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 * multi-chassis. Use available data to take a good guess.
1701 * If in doubt, go HPET.
1702 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001703__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 int i, clusters, zeros;
1706 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08001707 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1709
Yinghai Lu322850a2008-02-23 21:48:42 -08001710 /*
1711 * there is not this kind of box with AMD CPU yet.
1712 * Some AMD box with quadcore cpu and 8 sockets apicid
1713 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08001714 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08001715 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001716 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08001717 return 0;
1718
Mike Travis23ca4bb2008-05-12 21:21:12 +02001719 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec33f2005-05-16 21:53:32 -07001720 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 for (i = 0; i < NR_CPUS; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001723 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01001724 if (bios_cpu_apicid) {
1725 id = bios_cpu_apicid[i];
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001726 }
1727 else if (i < nr_cpu_ids) {
1728 if (cpu_present(i))
1729 id = per_cpu(x86_bios_cpu_apicid, i);
1730 else
1731 continue;
1732 }
1733 else
1734 break;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (id != BAD_APICID)
1737 __set_bit(APIC_CLUSTERID(id), clustermap);
1738 }
1739
1740 /* Problem: Partially populated chassis may not have CPUs in some of
1741 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01001742 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1743 * Since clusters are allocated sequentially, count zeros only if
1744 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
1746 clusters = 0;
1747 zeros = 0;
1748 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1749 if (test_bit(i, clustermap)) {
1750 clusters += 1 + zeros;
1751 zeros = 0;
1752 } else
1753 ++zeros;
1754 }
1755
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001756 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1757 * not guaranteed to be synced between boards
1758 */
1759 if (is_vsmp_box() && clusters > 1)
1760 return 1;
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001763 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * May have to revisit this when multi-core + hyperthreaded CPUs come
1765 * out, but AFAIK this will work even for them.
1766 */
1767 return (clusters > 2);
1768}
1769
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001770static __init int setup_nox2apic(char *str)
1771{
1772 disable_x2apic = 1;
1773 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_X2APIC);
1774 return 0;
1775}
1776early_param("nox2apic", setup_nox2apic);
1777
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001780 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001782static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001783{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07001785 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001786 return 0;
1787}
1788early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001790/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001791static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001792{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001793 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001794}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001795early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001797static int __init parse_lapic_timer_c2_ok(char *arg)
1798{
1799 local_apic_timer_c2_ok = 1;
1800 return 0;
1801}
1802early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1803
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001804static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001807 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001808}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001809early_param("noapictimer", parse_disable_apic_timer);
1810
1811static int __init parse_nolapic_timer(char *arg)
1812{
1813 disable_apic_timer = 1;
1814 return 0;
1815}
1816early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01001817
Cyrill Gorcunov920fa7a2008-08-24 02:01:41 -07001818#ifdef CONFIG_X86_64
Andi Kleen0c3749c2006-02-03 21:51:41 +01001819static __init int setup_apicpmtimer(char *s)
1820{
1821 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001822 notsc_setup(NULL);
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001823 return 0;
Andi Kleen0c3749c2006-02-03 21:51:41 +01001824}
1825__setup("apicpmtimer", setup_apicpmtimer);
Cyrill Gorcunov920fa7a2008-08-24 02:01:41 -07001826#endif
Andi Kleen0c3749c2006-02-03 21:51:41 +01001827
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04001828static int __init apic_set_verbosity(char *arg)
1829{
1830 if (!arg) {
1831#ifdef CONFIG_X86_64
1832 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04001833 return 0;
1834#endif
1835 return -EINVAL;
1836 }
1837
1838 if (strcmp("debug", arg) == 0)
1839 apic_verbosity = APIC_DEBUG;
1840 else if (strcmp("verbose", arg) == 0)
1841 apic_verbosity = APIC_VERBOSE;
1842 else {
1843 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1844 " use apic=verbose or apic=debug\n", arg);
1845 return -EINVAL;
1846 }
1847
1848 return 0;
1849}
1850early_param("apic", apic_set_verbosity);
1851
Yinghai Lu1e934dd2008-02-22 13:37:26 -08001852static int __init lapic_insert_resource(void)
1853{
1854 if (!apic_phys)
1855 return -1;
1856
1857 /* Put local APIC into the resource map. */
1858 lapic_resource.start = apic_phys;
1859 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1860 insert_resource(&iomem_resource, &lapic_resource);
1861
1862 return 0;
1863}
1864
1865/*
1866 * need call insert after e820_reserve_resources()
1867 * that is using request_resource
1868 */
1869late_initcall(lapic_insert_resource);