blob: 85d8b50d1af70dab633ff82453c873d5e17ba978 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * (c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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/kernel_stat.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010018#include <linux/mc146818rtc.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010019#include <linux/acpi_pmtmr.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010020#include <linux/clockchips.h>
21#include <linux/interrupt.h>
22#include <linux/bootmem.h>
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +010023#include <linux/ftrace.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010024#include <linux/ioport.h>
25#include <linux/module.h>
26#include <linux/sysdev.h>
27#include <linux/delay.h>
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +053028#include <linux/timex.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010029#include <linux/dmar.h>
30#include <linux/init.h>
31#include <linux/cpu.h>
32#include <linux/dmi.h>
33#include <linux/nmi.h>
34#include <linux/smp.h>
35#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Yinghai Lu773763d2008-08-24 02:01:52 -070037#include <asm/arch_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/pgalloc.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010039#include <asm/genapic.h>
40#include <asm/atomic.h>
41#include <asm/mpspec.h>
Yinghai Lu773763d2008-08-24 02:01:52 -070042#include <asm/i8253.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010043#include <asm/i8259.h>
Andi Kleen73dea472006-02-03 21:50:50 +010044#include <asm/proto.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020045#include <asm/apic.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010046#include <asm/desc.h>
47#include <asm/hpet.h>
48#include <asm/idle.h>
49#include <asm/mtrr.h>
Jaswinder Singh Rajput2bc13792009-01-11 20:34:47 +053050#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Brian Gerstec70de82009-01-27 12:56:47 +090052unsigned int num_processors;
53unsigned disabled_cpus __cpuinitdata;
54/* Processor that is doing the boot up */
55unsigned int boot_cpu_physical_apicid = -1U;
56EXPORT_SYMBOL(boot_cpu_physical_apicid);
57unsigned int max_physical_apicid;
58
59/* Bitmask of physically existing CPUs */
60physid_mask_t phys_cpu_present_map;
61
62/*
63 * Map cpu index to physical APIC ID
64 */
65DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
66DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
67EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
68EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
69
Yinghai Lub3c51172008-08-24 02:01:46 -070070#ifdef CONFIG_X86_32
71/*
72 * Knob to control our willingness to enable the local APIC.
73 *
74 * +1=force-enable
75 */
76static int force_enable_local_apic;
77/*
78 * APIC command line parameters
79 */
80static int __init parse_lapic(char *arg)
81{
82 force_enable_local_apic = 1;
83 return 0;
84}
85early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -070086/* Local APIC was disabled by the BIOS and enabled by the kernel */
87static int enabled_via_apicbase;
88
Yinghai Lub3c51172008-08-24 02:01:46 -070089#endif
90
91#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +020092static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -070093static __init int setup_apicpmtimer(char *s)
94{
95 apic_calibrate_pmtmr = 1;
96 notsc_setup(NULL);
97 return 0;
98}
99__setup("apicpmtimer", setup_apicpmtimer);
100#endif
101
Yinghai Lu49899ea2008-08-24 02:01:47 -0700102#ifdef CONFIG_X86_64
103#define HAVE_X2APIC
104#endif
105
106#ifdef HAVE_X2APIC
Suresh Siddha89027d32008-07-10 11:16:56 -0700107int x2apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700108/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530109static int x2apic_preenabled;
110static int disable_x2apic;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700111static __init int setup_nox2apic(char *str)
112{
113 disable_x2apic = 1;
114 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
115 return 0;
116}
117early_param("nox2apic", setup_nox2apic);
118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Yinghai Lub3c51172008-08-24 02:01:46 -0700120unsigned long mp_lapic_addr;
121int disable_apic;
122/* Disable local APIC timer from the kernel commandline or via dmi quirk */
123static int disable_apic_timer __cpuinitdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100124/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700125int local_apic_timer_c2_ok;
126EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
127
Yinghai Luefa25592008-08-19 20:50:36 -0700128int first_system_vector = 0xfe;
129
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100130/*
131 * Debug level, exported for io_apic.c
132 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100133unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100134
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700135int pic_mode;
136
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400137/* Have we found an MP table */
138int smp_found_config;
139
Aaron Durbin39928722006-12-07 02:14:01 +0100140static struct resource lapic_resource = {
141 .name = "Local APIC",
142 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
143};
144
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200145static unsigned int calibration_result;
146
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200147static int lapic_next_event(unsigned long delta,
148 struct clock_event_device *evt);
149static void lapic_timer_setup(enum clock_event_mode mode,
150 struct clock_event_device *evt);
Mike Travis96289372008-12-31 18:08:46 -0800151static void lapic_timer_broadcast(const struct cpumask *mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100152static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200153
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400154/*
155 * The local apic timer can be used for any function which is CPU local.
156 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200157static struct clock_event_device lapic_clockevent = {
158 .name = "lapic",
159 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
160 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
161 .shift = 32,
162 .set_mode = lapic_timer_setup,
163 .set_next_event = lapic_next_event,
164 .broadcast = lapic_timer_broadcast,
165 .rating = 100,
166 .irq = -1,
167};
168static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
169
Andi Kleend3432892008-01-30 13:33:17 +0100170static unsigned long apic_phys;
171
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100172/*
173 * Get the LAPIC version
174 */
175static inline int lapic_get_version(void)
176{
177 return GET_APIC_VERSION(apic_read(APIC_LVR));
178}
179
180/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400181 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100182 */
183static inline int lapic_is_integrated(void)
184{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400185#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100186 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400187#else
188 return APIC_INTEGRATED(lapic_get_version());
189#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100190}
191
192/*
193 * Check, whether this is a modern or a first generation APIC
194 */
195static int modern_apic(void)
196{
197 /* AMD systems use old APIC versions, so check the CPU */
198 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
199 boot_cpu_data.x86 >= 0xf)
200 return 1;
201 return lapic_get_version() >= 0x14;
202}
203
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400204/*
205 * Paravirt kernels also might be using these below ops. So we still
206 * use generic apic_read()/apic_write(), which might be pointing to different
207 * ops in PARAVIRT case.
208 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700209void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100210{
211 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
212 cpu_relax();
213}
214
Suresh Siddha1b374e42008-07-10 11:16:49 -0700215u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100216{
217 u32 send_status;
218 int timeout;
219
220 timeout = 0;
221 do {
222 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
223 if (!send_status)
224 break;
225 udelay(100);
226 } while (timeout++ < 1000);
227
228 return send_status;
229}
230
Suresh Siddha1b374e42008-07-10 11:16:49 -0700231void xapic_icr_write(u32 low, u32 id)
232{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200233 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700234 apic_write(APIC_ICR, low);
235}
236
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530237static u64 xapic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700238{
239 u32 icr1, icr2;
240
241 icr2 = apic_read(APIC_ICR2);
242 icr1 = apic_read(APIC_ICR);
243
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400244 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700245}
246
247static struct apic_ops xapic_ops = {
248 .read = native_apic_mem_read,
249 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700250 .icr_read = xapic_icr_read,
251 .icr_write = xapic_icr_write,
252 .wait_icr_idle = xapic_wait_icr_idle,
253 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
254};
255
256struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700257EXPORT_SYMBOL_GPL(apic_ops);
258
Yinghai Lu49899ea2008-08-24 02:01:47 -0700259#ifdef HAVE_X2APIC
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700260static void x2apic_wait_icr_idle(void)
261{
262 /* no need to wait for icr idle in x2apic */
263 return;
264}
265
266static u32 safe_x2apic_wait_icr_idle(void)
267{
268 /* no need to wait for icr idle in x2apic */
269 return 0;
270}
271
272void x2apic_icr_write(u32 low, u32 id)
273{
274 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
275}
276
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530277static u64 x2apic_icr_read(void)
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700278{
279 unsigned long val;
280
281 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
282 return val;
283}
284
285static struct apic_ops x2apic_ops = {
286 .read = native_apic_msr_read,
287 .write = native_apic_msr_write,
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700288 .icr_read = x2apic_icr_read,
289 .icr_write = x2apic_icr_write,
290 .wait_icr_idle = x2apic_wait_icr_idle,
291 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
292};
Yinghai Lu49899ea2008-08-24 02:01:47 -0700293#endif
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700294
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100295/**
296 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
297 */
Jan Beuliche9427102008-01-30 13:31:24 +0100298void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100299{
300 unsigned int v;
301
302 /* unmask and set to NMI */
303 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200304
305 /* Level triggered for 82489DX (32bit mode) */
306 if (!lapic_is_integrated())
307 v |= APIC_LVT_LEVEL_TRIGGER;
308
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100309 apic_write(APIC_LVT0, v);
310}
311
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700312#ifdef CONFIG_X86_32
313/**
314 * get_physical_broadcast - Get number of physical broadcast IDs
315 */
316int get_physical_broadcast(void)
317{
318 return modern_apic() ? 0xff : 0xf;
319}
320#endif
321
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100322/**
323 * lapic_get_maxlvt - get the maximum number of local vector table entries
324 */
325int lapic_get_maxlvt(void)
326{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200327 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100328
329 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200330 /*
331 * - we always have APIC integrated on 64bit mode
332 * - 82489DXs do not report # of LVT entries
333 */
334 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100335}
336
337/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400338 * Local APIC timer
339 */
340
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400341/* Clock divisor */
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400342#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200343
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100344/*
345 * This function sets up the local APIC timer, with a timeout of
346 * 'clocks' APIC bus clock. During calibration we actually call
347 * this function twice on the boot CPU, once with a bogus timeout
348 * value, second time for real. The other (noncalibrating) CPUs
349 * call this function only once, with the real, calibrated value.
350 *
351 * We do reads before writes even if unnecessary, to get around the
352 * P5 APIC double write bug.
353 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100354static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
355{
356 unsigned int lvtt_value, tmp_value;
357
358 lvtt_value = LOCAL_TIMER_VECTOR;
359 if (!oneshot)
360 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200361 if (!lapic_is_integrated())
362 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
363
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100364 if (!irqen)
365 lvtt_value |= APIC_LVT_MASKED;
366
367 apic_write(APIC_LVTT, lvtt_value);
368
369 /*
370 * Divide PICLK by 16
371 */
372 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400373 apic_write(APIC_TDCR,
374 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
375 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100376
377 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200378 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100379}
380
381/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100382 * Setup extended LVT, AMD specific (K8, family 10h)
383 *
384 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
385 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200386 *
387 * If mask=1, the LVT entry does not generate interrupts while mask=0
388 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100389 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100390
391#define APIC_EILVT_LVTOFF_MCE 0
392#define APIC_EILVT_LVTOFF_IBS 1
393
394static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100395{
Robert Richter7b83dae2008-01-30 13:30:40 +0100396 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100397 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
398
399 apic_write(reg, v);
400}
401
Robert Richter7b83dae2008-01-30 13:30:40 +0100402u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
403{
404 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
405 return APIC_EILVT_LVTOFF_MCE;
406}
407
408u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
409{
410 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
411 return APIC_EILVT_LVTOFF_IBS;
412}
Robert Richter6aa360e2008-07-23 15:28:14 +0200413EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100414
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100415/*
416 * Program the next event, relative to now
417 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200418static int lapic_next_event(unsigned long delta,
419 struct clock_event_device *evt)
420{
421 apic_write(APIC_TMICT, delta);
422 return 0;
423}
424
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100425/*
426 * Setup the lapic timer in periodic or oneshot mode
427 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200428static void lapic_timer_setup(enum clock_event_mode mode,
429 struct clock_event_device *evt)
430{
431 unsigned long flags;
432 unsigned int v;
433
434 /* Lapic used as dummy for broadcast ? */
435 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
436 return;
437
438 local_irq_save(flags);
439
440 switch (mode) {
441 case CLOCK_EVT_MODE_PERIODIC:
442 case CLOCK_EVT_MODE_ONESHOT:
443 __setup_APIC_LVTT(calibration_result,
444 mode != CLOCK_EVT_MODE_PERIODIC, 1);
445 break;
446 case CLOCK_EVT_MODE_UNUSED:
447 case CLOCK_EVT_MODE_SHUTDOWN:
448 v = apic_read(APIC_LVTT);
449 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
450 apic_write(APIC_LVTT, v);
Thomas Gleixnera98f8fd2008-11-06 01:13:39 +0100451 apic_write(APIC_TMICT, 0xffffffff);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200452 break;
453 case CLOCK_EVT_MODE_RESUME:
454 /* Nothing to do here */
455 break;
456 }
457
458 local_irq_restore(flags);
459}
460
461/*
462 * Local APIC timer broadcast function
463 */
Mike Travis96289372008-12-31 18:08:46 -0800464static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200465{
466#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100467 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200468#endif
469}
470
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100471/*
472 * Setup the local APIC timer for this CPU. Copy the initilized values
473 * of the boot CPU and register the clock event in the framework.
474 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700475static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200476{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100477 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
478
479 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030480 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100481
482 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200483}
484
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700485/*
486 * In this functions we calibrate APIC bus clocks to the external timer.
487 *
488 * We want to do the calibration only once since we want to have local timer
489 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
490 * frequency.
491 *
492 * This was previously done by reading the PIT/HPET and waiting for a wrap
493 * around to find out, that a tick has elapsed. I have a box, where the PIT
494 * readout is broken, so it never gets out of the wait loop again. This was
495 * also reported by others.
496 *
497 * Monitoring the jiffies value is inaccurate and the clockevents
498 * infrastructure allows us to do a simple substitution of the interrupt
499 * handler.
500 *
501 * The calibration routine also uses the pm_timer when possible, as the PIT
502 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
503 * back to normal later in the boot process).
504 */
505
506#define LAPIC_CAL_LOOPS (HZ/10)
507
508static __initdata int lapic_cal_loops = -1;
509static __initdata long lapic_cal_t1, lapic_cal_t2;
510static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
511static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
512static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
513
514/*
515 * Temporary interrupt handler.
516 */
517static void __init lapic_cal_handler(struct clock_event_device *dev)
518{
519 unsigned long long tsc = 0;
520 long tapic = apic_read(APIC_TMCCT);
521 unsigned long pm = acpi_pm_read_early();
522
523 if (cpu_has_tsc)
524 rdtscll(tsc);
525
526 switch (lapic_cal_loops++) {
527 case 0:
528 lapic_cal_t1 = tapic;
529 lapic_cal_tsc1 = tsc;
530 lapic_cal_pm1 = pm;
531 lapic_cal_j1 = jiffies;
532 break;
533
534 case LAPIC_CAL_LOOPS:
535 lapic_cal_t2 = tapic;
536 lapic_cal_tsc2 = tsc;
537 if (pm < lapic_cal_pm1)
538 pm += ACPI_PM_OVRRUN;
539 lapic_cal_pm2 = pm;
540 lapic_cal_j2 = jiffies;
541 break;
542 }
543}
544
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400545static int __init calibrate_by_pmtimer(long deltapm, long *delta)
546{
547 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
548 const long pm_thresh = pm_100ms / 100;
549 unsigned long mult;
550 u64 res;
551
552#ifndef CONFIG_X86_PM_TIMER
553 return -1;
554#endif
555
556 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
557
558 /* Check, if the PM timer is available */
559 if (!deltapm)
560 return -1;
561
562 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
563
564 if (deltapm > (pm_100ms - pm_thresh) &&
565 deltapm < (pm_100ms + pm_thresh)) {
566 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
567 } else {
568 res = (((u64)deltapm) * mult) >> 22;
569 do_div(res, 1000000);
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100570 pr_warning("APIC calibration not consistent "
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400571 "with PM Timer: %ldms instead of 100ms\n",
572 (long)res);
573 /* Correct the lapic counter value */
574 res = (((u64)(*delta)) * pm_100ms);
575 do_div(res, deltapm);
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100576 pr_info("APIC delta adjusted to PM-Timer: "
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400577 "%lu (%ld)\n", (unsigned long)res, *delta);
578 *delta = (long)res;
579 }
580
581 return 0;
582}
583
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700584static int __init calibrate_APIC_clock(void)
585{
586 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700587 void (*real_handler)(struct clock_event_device *dev);
588 unsigned long deltaj;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400589 long delta;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700590 int pm_referenced = 0;
591
592 local_irq_disable();
593
594 /* Replace the global interrupt handler */
595 real_handler = global_clock_event->event_handler;
596 global_clock_event->event_handler = lapic_cal_handler;
597
598 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400599 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700600 * can underflow in the 100ms detection time frame
601 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400602 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700603
604 /* Let the interrupts run */
605 local_irq_enable();
606
607 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
608 cpu_relax();
609
610 local_irq_disable();
611
612 /* Restore the real event handler */
613 global_clock_event->event_handler = real_handler;
614
615 /* Build delta t1-t2 as apic timer counts down */
616 delta = lapic_cal_t1 - lapic_cal_t2;
617 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
618
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400619 /* we trust the PM based calibration if possible */
620 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
621 &delta);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700622
623 /* Calculate the scaled math multiplication factor */
624 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
625 lapic_clockevent.shift);
626 lapic_clockevent.max_delta_ns =
627 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
628 lapic_clockevent.min_delta_ns =
629 clockevent_delta2ns(0xF, &lapic_clockevent);
630
631 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
632
633 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
634 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
635 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
636 calibration_result);
637
638 if (cpu_has_tsc) {
639 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
640 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
641 "%ld.%04ld MHz.\n",
642 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
643 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
644 }
645
646 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
647 "%u.%04u MHz.\n",
648 calibration_result / (1000000 / HZ),
649 calibration_result % (1000000 / HZ));
650
651 /*
652 * Do a sanity check on the APIC calibration result
653 */
654 if (calibration_result < (1000000 / HZ)) {
655 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100656 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700657 return -1;
658 }
659
660 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
661
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400662 /*
663 * PM timer calibration failed or not turned on
664 * so lets try APIC timer based calibration
665 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700666 if (!pm_referenced) {
667 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
668
669 /*
670 * Setup the apic timer manually
671 */
672 levt->event_handler = lapic_cal_handler;
673 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
674 lapic_cal_loops = -1;
675
676 /* Let the interrupts run */
677 local_irq_enable();
678
679 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
680 cpu_relax();
681
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700682 /* Stop the lapic timer */
683 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
684
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700685 /* Jiffies delta */
686 deltaj = lapic_cal_j2 - lapic_cal_j1;
687 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
688
689 /* Check, if the jiffies result is consistent */
690 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
691 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
692 else
693 levt->features |= CLOCK_EVT_FEAT_DUMMY;
694 } else
695 local_irq_enable();
696
697 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530698 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700699 return -1;
700 }
701
702 return 0;
703}
704
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100705/*
706 * Setup the boot APIC
707 *
708 * Calibrate and verify the result.
709 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100710void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100712 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400713 * The local apic timer can be disabled via the kernel
714 * commandline or from the CPU detection code. Register the lapic
715 * timer as a dummy clock event source on SMP systems, so the
716 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100717 */
718 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100719 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100720 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100721 if (num_possible_cpus() > 1) {
722 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100723 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100724 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100725 return;
726 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200727
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400728 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
729 "calibrating APIC timer ...\n");
730
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400731 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100732 /* No broadcast on UP ! */
733 if (num_possible_cpus() > 1)
734 setup_APIC_timer();
735 return;
736 }
737
738 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100739 * If nmi_watchdog is set to IO_APIC, we need the
740 * PIT/HPET going. Otherwise register lapic as a dummy
741 * device.
742 */
743 if (nmi_watchdog != NMI_IO_APIC)
744 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
745 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100746 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200747 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100748
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400749 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100750 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100753void __cpuinit setup_secondary_APIC_clock(void)
754{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100755 setup_APIC_timer();
756}
757
758/*
759 * The guts of the apic timer interrupt
760 */
761static void local_apic_timer_interrupt(void)
762{
763 int cpu = smp_processor_id();
764 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
765
766 /*
767 * Normally we should not be here till LAPIC has been initialized but
768 * in some cases like kdump, its possible that there is a pending LAPIC
769 * timer interrupt from previous kernel's context and is delivered in
770 * new kernel the moment interrupts are enabled.
771 *
772 * Interrupts are enabled early and LAPIC is setup much later, hence
773 * its possible that when we get here evt->event_handler is NULL.
774 * Check for event_handler being NULL and discard the interrupt as
775 * spurious.
776 */
777 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100778 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100779 /* Switch it off */
780 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
781 return;
782 }
783
784 /*
785 * the NMI deadlock-detector uses this.
786 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800787 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100788
789 evt->event_handler(evt);
790}
791
792/*
793 * Local APIC timer interrupt. This is the most natural way for doing
794 * local interrupts, but local timer interrupts can be emulated by
795 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
796 *
797 * [ if a single-CPU system runs an SMP kernel then we call the local
798 * interrupt as well. Thus we cannot inline the local irq ... ]
799 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100800void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100801{
802 struct pt_regs *old_regs = set_irq_regs(regs);
803
804 /*
805 * NOTE! We'd better ACK the irq immediately,
806 * because timer handling can be slow.
807 */
808 ack_APIC_irq();
809 /*
810 * update_process_times() expects us to have done irq_enter().
811 * Besides, if we don't timer interrupts ignore the global
812 * interrupt lock, which is the WrongThing (tm) to do.
813 */
814 exit_idle();
815 irq_enter();
816 local_apic_timer_interrupt();
817 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400818
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100819 set_irq_regs(old_regs);
820}
821
822int setup_profiling_timer(unsigned int multiplier)
823{
824 return -EINVAL;
825}
826
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100827/*
828 * Local APIC start and shutdown
829 */
830
831/**
832 * clear_local_APIC - shutdown the local APIC
833 *
834 * This is called, when a CPU is disabled and before rebooting, so the state of
835 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
836 * leftovers during boot.
837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838void clear_local_APIC(void)
839{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400840 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100841 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Andi Kleend3432892008-01-30 13:33:17 +0100843 /* APIC hasn't been mapped yet */
844 if (!apic_phys)
845 return;
846
847 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200849 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * if the vector is zero. Mask LVTERR first to prevent this.
851 */
852 if (maxlvt >= 3) {
853 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100854 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 /*
857 * Careful: we have to set masks only first to deassert
858 * any level-triggered sources.
859 */
860 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100861 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100863 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100865 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (maxlvt >= 4) {
867 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100868 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400871 /* lets not touch this if we didn't frob it */
872#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
873 if (maxlvt >= 5) {
874 v = apic_read(APIC_LVTTHMR);
875 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
876 }
877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /*
879 * Clean APIC state for other OSs:
880 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100881 apic_write(APIC_LVTT, APIC_LVT_MASKED);
882 apic_write(APIC_LVT0, APIC_LVT_MASKED);
883 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100885 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100887 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400888
889 /* Integrated APIC (!82489DX) ? */
890 if (lapic_is_integrated()) {
891 if (maxlvt > 3)
892 /* Clear ESR due to Pentium errata 3AP and 11AP */
893 apic_write(APIC_ESR, 0);
894 apic_read(APIC_ESR);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100898/**
899 * disable_local_APIC - clear and disable the local APIC
900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901void disable_local_APIC(void)
902{
903 unsigned int value;
904
Jan Beulicha08c4742009-01-14 12:28:51 +0000905 /* APIC hasn't been mapped yet */
906 if (!apic_phys)
907 return;
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 clear_local_APIC();
910
911 /*
912 * Disable APIC (implies clearing of registers
913 * for 82489DX!).
914 */
915 value = apic_read(APIC_SPIV);
916 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100917 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400918
919#ifdef CONFIG_X86_32
920 /*
921 * When LAPIC was disabled by the BIOS and enabled by the kernel,
922 * restore the disabled state.
923 */
924 if (enabled_via_apicbase) {
925 unsigned int l, h;
926
927 rdmsr(MSR_IA32_APICBASE, l, h);
928 l &= ~MSR_IA32_APICBASE_ENABLE;
929 wrmsr(MSR_IA32_APICBASE, l, h);
930 }
931#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400934/*
935 * If Linux enabled the LAPIC against the BIOS default disable it down before
936 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
937 * not power-off. Additionally clear all LVT entries before disable_local_APIC
938 * for the case where Linux didn't enable the LAPIC.
939 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700940void lapic_shutdown(void)
941{
942 unsigned long flags;
943
944 if (!cpu_has_apic)
945 return;
946
947 local_irq_save(flags);
948
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400949#ifdef CONFIG_X86_32
950 if (!enabled_via_apicbase)
951 clear_local_APIC();
952 else
953#endif
954 disable_local_APIC();
955
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700956
957 local_irq_restore(flags);
958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/*
961 * This is to verify that we're looking at a real local APIC.
962 * Check these against your board if the CPUs aren't getting
963 * started for no apparent reason.
964 */
965int __init verify_local_APIC(void)
966{
967 unsigned int reg0, reg1;
968
969 /*
970 * The version register is read-only in a real APIC.
971 */
972 reg0 = apic_read(APIC_LVR);
973 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
974 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
975 reg1 = apic_read(APIC_LVR);
976 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
977
978 /*
979 * The two version reads above should print the same
980 * numbers. If the second one is different, then we
981 * poke at a non-APIC.
982 */
983 if (reg1 != reg0)
984 return 0;
985
986 /*
987 * Check if the version looks reasonably.
988 */
989 reg1 = GET_APIC_VERSION(reg0);
990 if (reg1 == 0x00 || reg1 == 0xff)
991 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100992 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (reg1 < 0x02 || reg1 == 0xff)
994 return 0;
995
996 /*
997 * The ID register is read/write in a real APIC.
998 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700999 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001001 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001002 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1004 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001005 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
1007
1008 /*
1009 * The next two are just to see if we have sane values.
1010 * They're only really relevant if we're in Virtual Wire
1011 * compatibility mode, but most boxes are anymore.
1012 */
1013 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001014 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 reg1 = apic_read(APIC_LVT1);
1016 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1017
1018 return 1;
1019}
1020
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001021/**
1022 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024void __init sync_Arb_IDs(void)
1025{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001026 /*
1027 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1028 * needed on AMD.
1029 */
1030 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return;
1032
1033 /*
1034 * Wait for idle.
1035 */
1036 apic_wait_icr_idle();
1037
1038 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001039 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1040 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
1044 * An initial setup of the virtual wire mode.
1045 */
1046void __init init_bsp_APIC(void)
1047{
Andi Kleen11a8e772006-01-11 22:46:51 +01001048 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /*
1051 * Don't do the setup now if we have a SMP BIOS as the
1052 * through-I/O-APIC virtual wire mode might be active.
1053 */
1054 if (smp_found_config || !cpu_has_apic)
1055 return;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * Do not trust the local APIC being empty at bootup.
1059 */
1060 clear_local_APIC();
1061
1062 /*
1063 * Enable APIC.
1064 */
1065 value = apic_read(APIC_SPIV);
1066 value &= ~APIC_VECTOR_MASK;
1067 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001068
1069#ifdef CONFIG_X86_32
1070 /* This bit is reserved on P4/Xeon and should be cleared */
1071 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1072 (boot_cpu_data.x86 == 15))
1073 value &= ~APIC_SPIV_FOCUS_DISABLED;
1074 else
1075#endif
1076 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001078 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
1081 * Set up the virtual wire mode.
1082 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001083 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001085 if (!lapic_is_integrated()) /* 82489DX */
1086 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001087 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001090static void __cpuinit lapic_setup_esr(void)
1091{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001092 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001093
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001094 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001095 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001096 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001097 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001098
Ingo Molnar08125d32009-01-28 05:08:44 +01001099 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001100 /*
1101 * Something untraceable is creating bad interrupts on
1102 * secondary quads ... for the moment, just leave the
1103 * ESR disabled - we can't do anything useful with the
1104 * errors anyway - mbligh
1105 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001106 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001107 return;
1108 }
1109
1110 maxlvt = lapic_get_maxlvt();
1111 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1112 apic_write(APIC_ESR, 0);
1113 oldvalue = apic_read(APIC_ESR);
1114
1115 /* enables sending errors */
1116 value = ERROR_APIC_VECTOR;
1117 apic_write(APIC_LVTERR, value);
1118
1119 /*
1120 * spec says clear errors after enabling vector.
1121 */
1122 if (maxlvt > 3)
1123 apic_write(APIC_ESR, 0);
1124 value = apic_read(APIC_ESR);
1125 if (value != oldvalue)
1126 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1127 "vector: 0x%08x after: 0x%08x\n",
1128 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001129}
1130
1131
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001132/**
1133 * setup_local_APIC - setup the local APIC
1134 */
1135void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Andi Kleen739f33b2008-01-30 13:30:40 +01001137 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001138 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jan Beulichf1182632009-01-14 12:27:35 +00001140 if (disable_apic) {
Ingo Molnar5cdc5e9e2009-01-19 20:49:37 +01001141#ifdef CONFIG_X86_IO_APIC
Jan Beulichf1182632009-01-14 12:27:35 +00001142 disable_ioapic_setup();
Ingo Molnar5cdc5e9e2009-01-19 20:49:37 +01001143#endif
Jan Beulichf1182632009-01-14 12:27:35 +00001144 return;
1145 }
1146
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001147#ifdef CONFIG_X86_32
1148 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001149 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001150 apic_write(APIC_ESR, 0);
1151 apic_write(APIC_ESR, 0);
1152 apic_write(APIC_ESR, 0);
1153 apic_write(APIC_ESR, 0);
1154 }
1155#endif
1156
Jack Steinerac23d4e2008-03-28 14:12:16 -05001157 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * Double-check whether this APIC is really registered.
1161 * This is meaningless in clustered apic mode, so we skip it.
1162 */
Ingo Molnar7ed248d2009-01-28 03:43:47 +01001163 if (!apic->apic_id_registered())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 BUG();
1165
1166 /*
1167 * Intel recommends to set DFR, LDR and TPR before enabling
1168 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1169 * document number 292116). So here it goes...
1170 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001171 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /*
1174 * Set Task Priority to 'accept all'. We never change this
1175 * later on.
1176 */
1177 value = apic_read(APIC_TASKPRI);
1178 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001179 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001182 * After a crash, we no longer service the interrupts and a pending
1183 * interrupt from previous kernel might still have ISR bit set.
1184 *
1185 * Most probably by now CPU has serviced that pending interrupt and
1186 * it might not have done the ack_APIC_irq() because it thought,
1187 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1188 * does not clear the ISR bit and cpu thinks it has already serivced
1189 * the interrupt. Hence a vector might get locked. It was noticed
1190 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1191 */
1192 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1193 value = apic_read(APIC_ISR + i*0x10);
1194 for (j = 31; j >= 0; j--) {
1195 if (value & (1<<j))
1196 ack_APIC_irq();
1197 }
1198 }
1199
1200 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 * Now that we are all set up, enable the APIC
1202 */
1203 value = apic_read(APIC_SPIV);
1204 value &= ~APIC_VECTOR_MASK;
1205 /*
1206 * Enable APIC
1207 */
1208 value |= APIC_SPIV_APIC_ENABLED;
1209
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001210#ifdef CONFIG_X86_32
1211 /*
1212 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1213 * certain networking cards. If high frequency interrupts are
1214 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1215 * entry is masked/unmasked at a high rate as well then sooner or
1216 * later IOAPIC line gets 'stuck', no more interrupts are received
1217 * from the device. If focus CPU is disabled then the hang goes
1218 * away, oh well :-(
1219 *
1220 * [ This bug can be reproduced easily with a level-triggered
1221 * PCI Ne2000 networking cards and PII/PIII processors, dual
1222 * BX chipset. ]
1223 */
1224 /*
1225 * Actually disabling the focus CPU check just makes the hang less
1226 * frequent as it makes the interrupt distributon model be more
1227 * like LRU than MRU (the short-term load is more even across CPUs).
1228 * See also the comment in end_level_ioapic_irq(). --macro
1229 */
1230
1231 /*
1232 * - enable focus processor (bit==0)
1233 * - 64bit mode always use processor focus
1234 * so no need to set it
1235 */
1236 value &= ~APIC_SPIV_FOCUS_DISABLED;
1237#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * Set spurious IRQ vector
1241 */
1242 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001243 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
1246 * Set up LVT0, LVT1:
1247 *
1248 * set up through-local-APIC on the BP's LINT0. This is not
1249 * strictly necessary in pure symmetric-IO mode, but sometimes
1250 * we delegate interrupts to the 8259A.
1251 */
1252 /*
1253 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1254 */
1255 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001256 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001258 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001259 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else {
1261 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001262 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001263 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001265 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * only the BP should see the LINT1 NMI signal, obviously.
1269 */
1270 if (!smp_processor_id())
1271 value = APIC_DM_NMI;
1272 else
1273 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001274 if (!lapic_is_integrated()) /* 82489DX */
1275 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001276 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001277
Jack Steinerac23d4e2008-03-28 14:12:16 -05001278 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001279}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Andi Kleen739f33b2008-01-30 13:30:40 +01001281void __cpuinit end_local_APIC_setup(void)
1282{
1283 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001284
1285#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001286 {
1287 unsigned int value;
1288 /* Disable the local apic timer */
1289 value = apic_read(APIC_LVTT);
1290 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1291 apic_write(APIC_LVTT, value);
1292 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001293#endif
1294
Don Zickusf2802e72006-09-26 10:52:26 +02001295 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 apic_pm_activate();
1297}
1298
Yinghai Lu49899ea2008-08-24 02:01:47 -07001299#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001300void check_x2apic(void)
1301{
1302 int msr, msr2;
1303
1304 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1305
1306 if (msr & X2APIC_ENABLE) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001307 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001308 x2apic_preenabled = x2apic = 1;
1309 apic_ops = &x2apic_ops;
1310 }
1311}
1312
1313void enable_x2apic(void)
1314{
1315 int msr, msr2;
1316
1317 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1318 if (!(msr & X2APIC_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001319 pr_info("Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001320 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1321 }
1322}
1323
Al Viro2236d252008-11-22 17:37:34 +00001324void __init enable_IR_x2apic(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001325{
1326#ifdef CONFIG_INTR_REMAP
1327 int ret;
1328 unsigned long flags;
1329
1330 if (!cpu_has_x2apic)
1331 return;
1332
1333 if (!x2apic_preenabled && disable_x2apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001334 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1335 "because of nox2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001336 return;
1337 }
1338
1339 if (x2apic_preenabled && disable_x2apic)
1340 panic("Bios already enabled x2apic, can't enforce nox2apic");
1341
1342 if (!x2apic_preenabled && skip_ioapic_setup) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001343 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1344 "because of skipping io-apic setup\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001345 return;
1346 }
1347
1348 ret = dmar_table_init();
1349 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001350 pr_info("dmar_table_init() failed with %d:\n", ret);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001351
1352 if (x2apic_preenabled)
1353 panic("x2apic enabled by bios. But IR enabling failed");
1354 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001355 pr_info("Not enabling x2apic,Intr-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001356 return;
1357 }
1358
1359 local_irq_save(flags);
1360 mask_8259A();
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001361
1362 ret = save_mask_IO_APIC_setup();
1363 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001364 pr_info("Saving IO-APIC state failed: %d\n", ret);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001365 goto end;
1366 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001367
1368 ret = enable_intr_remapping(1);
1369
1370 if (ret && x2apic_preenabled) {
1371 local_irq_restore(flags);
1372 panic("x2apic enabled by bios. But IR enabling failed");
1373 }
1374
1375 if (ret)
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001376 goto end_restore;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001377
1378 if (!x2apic) {
1379 x2apic = 1;
1380 apic_ops = &x2apic_ops;
1381 enable_x2apic();
1382 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001383
1384end_restore:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001385 if (ret)
1386 /*
1387 * IR enabling failed
1388 */
1389 restore_IO_APIC_setup();
1390 else
1391 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1392
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001393end:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001394 unmask_8259A();
1395 local_irq_restore(flags);
1396
1397 if (!ret) {
1398 if (!x2apic_preenabled)
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001399 pr_info("Enabled x2apic and interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001400 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001401 pr_info("Enabled Interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001402 } else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001403 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001404#else
1405 if (!cpu_has_x2apic)
1406 return;
1407
1408 if (x2apic_preenabled)
1409 panic("x2apic enabled prior OS handover,"
1410 " enable CONFIG_INTR_REMAP");
1411
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001412 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1413 " and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001414#endif
1415
1416 return;
1417}
Yinghai Lu49899ea2008-08-24 02:01:47 -07001418#endif /* HAVE_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001419
Yinghai Lube7a6562008-08-24 02:01:51 -07001420#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001421/*
1422 * Detect and enable local APICs on non-SMP boards.
1423 * Original code written by Keir Fraser.
1424 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1425 * not correctly set up (usually the APIC timer won't work etc.)
1426 */
1427static int __init detect_init_APIC(void)
1428{
1429 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001430 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001431 return -1;
1432 }
1433
1434 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001435 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001436 return 0;
1437}
Yinghai Lube7a6562008-08-24 02:01:51 -07001438#else
1439/*
1440 * Detect and initialize APIC
1441 */
1442static int __init detect_init_APIC(void)
1443{
1444 u32 h, l, features;
1445
1446 /* Disabled by kernel option? */
1447 if (disable_apic)
1448 return -1;
1449
1450 switch (boot_cpu_data.x86_vendor) {
1451 case X86_VENDOR_AMD:
1452 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1453 (boot_cpu_data.x86 == 15))
1454 break;
1455 goto no_apic;
1456 case X86_VENDOR_INTEL:
1457 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1458 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1459 break;
1460 goto no_apic;
1461 default:
1462 goto no_apic;
1463 }
1464
1465 if (!cpu_has_apic) {
1466 /*
1467 * Over-ride BIOS and try to enable the local APIC only if
1468 * "lapic" specified.
1469 */
1470 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001471 pr_info("Local APIC disabled by BIOS -- "
1472 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001473 return -1;
1474 }
1475 /*
1476 * Some BIOSes disable the local APIC in the APIC_BASE
1477 * MSR. This can only be done in software for Intel P6 or later
1478 * and AMD K7 (Model > 1) or later.
1479 */
1480 rdmsr(MSR_IA32_APICBASE, l, h);
1481 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001482 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001483 l &= ~MSR_IA32_APICBASE_BASE;
1484 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1485 wrmsr(MSR_IA32_APICBASE, l, h);
1486 enabled_via_apicbase = 1;
1487 }
1488 }
1489 /*
1490 * The APIC feature bit should now be enabled
1491 * in `cpuid'
1492 */
1493 features = cpuid_edx(1);
1494 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001495 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001496 return -1;
1497 }
1498 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1499 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1500
1501 /* The BIOS may have set up the APIC at some other address */
1502 rdmsr(MSR_IA32_APICBASE, l, h);
1503 if (l & MSR_IA32_APICBASE_ENABLE)
1504 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1505
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001506 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001507
1508 apic_pm_activate();
1509
1510 return 0;
1511
1512no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001513 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001514 return -1;
1515}
1516#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001517
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001518#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001519void __init early_init_lapic_mapping(void)
1520{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001521 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001522
1523 /*
1524 * If no local APIC can be found then go out
1525 * : it means there is no mpatable and MADT
1526 */
1527 if (!smp_found_config)
1528 return;
1529
Thomas Gleixner431ee792008-05-12 15:43:35 +02001530 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001531
Thomas Gleixner431ee792008-05-12 15:43:35 +02001532 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001533 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001534 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001535
1536 /*
1537 * Fetch the APIC ID of the BSP in case we have a
1538 * default configuration (or the MP table is broken).
1539 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001540 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001541}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001542#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001543
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001544/**
1545 * init_apic_mappings - initialize APIC mappings
1546 */
1547void __init init_apic_mappings(void)
1548{
Yinghai Lu49899ea2008-08-24 02:01:47 -07001549#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001550 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001551 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001552 return;
1553 }
Yinghai Lu49899ea2008-08-24 02:01:47 -07001554#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001555
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001556 /*
1557 * If no local APIC can be found then set up a fake all
1558 * zeroes page to simulate the local APIC and another
1559 * one for the IO-APIC.
1560 */
1561 if (!smp_found_config && detect_init_APIC()) {
1562 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1563 apic_phys = __pa(apic_phys);
1564 } else
1565 apic_phys = mp_lapic_addr;
1566
1567 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu79c09692008-09-07 17:58:57 -07001568 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001569 APIC_BASE, apic_phys);
1570
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001571 /*
1572 * Fetch the APIC ID of the BSP in case we have a
1573 * default configuration (or the MP table is broken).
1574 */
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001575 if (boot_cpu_physical_apicid == -1U)
1576 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001577}
1578
1579/*
1580 * This initializes the IO-APIC and APIC hardware if this is
1581 * a UP kernel.
1582 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001583int apic_version[MAX_APICS];
1584
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001585int __init APIC_init_uniprocessor(void)
1586{
1587 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001588 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001589 return -1;
1590 }
Jan Beulichf1182632009-01-14 12:27:35 +00001591#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001592 if (!cpu_has_apic) {
1593 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001594 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001595 return -1;
1596 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001597#else
1598 if (!smp_found_config && !cpu_has_apic)
1599 return -1;
1600
1601 /*
1602 * Complain if the BIOS pretends there is one.
1603 */
1604 if (!cpu_has_apic &&
1605 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001606 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1607 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001608 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1609 return -1;
1610 }
1611#endif
1612
Yinghai Lu49899ea2008-08-24 02:01:47 -07001613#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001614 enable_IR_x2apic();
Yinghai Lu49899ea2008-08-24 02:01:47 -07001615#endif
Yinghai Lufa2bd352008-08-24 02:01:50 -07001616#ifdef CONFIG_X86_64
Ingo Molnar72ce0162009-01-28 06:50:47 +01001617 default_setup_apic_routing();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001618#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001619
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001620 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001621 connect_bsp_APIC();
1622
Yinghai Lufa2bd352008-08-24 02:01:50 -07001623#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001624 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001625#else
1626 /*
1627 * Hack: In case of kdump, after a crash, kernel might be booting
1628 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1629 * might be zero if read from MP tables. Get it from LAPIC.
1630 */
1631# ifdef CONFIG_CRASH_DUMP
1632 boot_cpu_physical_apicid = read_apic_id();
1633# endif
1634#endif
1635 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001636 setup_local_APIC();
1637
Yinghai Lufa2bd352008-08-24 02:01:50 -07001638#ifdef CONFIG_X86_64
Andi Kleen739f33b2008-01-30 13:30:40 +01001639 /*
1640 * Now enable IO-APICs, actually call clear_IO_APIC
1641 * We need clear_IO_APIC before enabling vector on BP
1642 */
1643 if (!skip_ioapic_setup && nr_ioapics)
1644 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001645#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001646
Yinghai Lufa2bd352008-08-24 02:01:50 -07001647#ifdef CONFIG_X86_IO_APIC
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001648 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
Yinghai Lufa2bd352008-08-24 02:01:50 -07001649#endif
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001650 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001651 end_local_APIC_setup();
1652
Yinghai Lufa2bd352008-08-24 02:01:50 -07001653#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001654 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1655 setup_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001656# ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001657 else
1658 nr_ioapics = 0;
Yinghai Lufa2bd352008-08-24 02:01:50 -07001659# endif
1660#endif
1661
1662#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001663 setup_boot_APIC_clock();
1664 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001665#else
1666 setup_boot_clock();
1667#endif
1668
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001669 return 0;
1670}
1671
1672/*
1673 * Local APIC interrupts
1674 */
1675
1676/*
1677 * This interrupt should _never_ happen with our APIC/SMP architecture
1678 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001679void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001680{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001681 u32 v;
1682
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001683 exit_idle();
1684 irq_enter();
1685 /*
1686 * Check if this really is a spurious interrupt and ACK it
1687 * if it is a vectored one. Just in case...
1688 * Spurious interrupts should not be ACKed.
1689 */
1690 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1691 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1692 ack_APIC_irq();
1693
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001694 inc_irq_stat(irq_spurious_count);
1695
Yinghai Ludc1528d2008-08-24 02:01:53 -07001696 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001697 pr_info("spurious APIC interrupt on CPU#%d, "
1698 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001699 irq_exit();
1700}
1701
1702/*
1703 * This interrupt should never happen with our APIC/SMP architecture
1704 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001705void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001706{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001707 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001708
1709 exit_idle();
1710 irq_enter();
1711 /* First tickle the hardware, only then report what went on. -- REW */
1712 v = apic_read(APIC_ESR);
1713 apic_write(APIC_ESR, 0);
1714 v1 = apic_read(APIC_ESR);
1715 ack_APIC_irq();
1716 atomic_inc(&irq_err_count);
1717
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001718 /*
1719 * Here is what the APIC error bits mean:
1720 * 0: Send CS error
1721 * 1: Receive CS error
1722 * 2: Send accept error
1723 * 3: Receive accept error
1724 * 4: Reserved
1725 * 5: Send illegal vector
1726 * 6: Received illegal vector
1727 * 7: Illegal register address
1728 */
1729 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001730 smp_processor_id(), v , v1);
1731 irq_exit();
1732}
1733
Glauber Costab5841762008-05-28 13:38:28 -03001734/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001735 * connect_bsp_APIC - attach the APIC to the interrupt system
1736 */
Glauber Costab5841762008-05-28 13:38:28 -03001737void __init connect_bsp_APIC(void)
1738{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001739#ifdef CONFIG_X86_32
1740 if (pic_mode) {
1741 /*
1742 * Do not trust the local APIC being empty at bootup.
1743 */
1744 clear_local_APIC();
1745 /*
1746 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1747 * local APIC to INT and NMI lines.
1748 */
1749 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1750 "enabling APIC mode.\n");
1751 outb(0x70, 0x22);
1752 outb(0x01, 0x23);
1753 }
1754#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001755 if (apic->enable_apic_mode)
1756 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001757}
1758
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001759/**
1760 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1761 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1762 *
1763 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1764 * APIC is disabled.
1765 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001766void disconnect_bsp_APIC(int virt_wire_setup)
1767{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001768 unsigned int value;
1769
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001770#ifdef CONFIG_X86_32
1771 if (pic_mode) {
1772 /*
1773 * Put the board back into PIC mode (has an effect only on
1774 * certain older boards). Note that APIC interrupts, including
1775 * IPIs, won't work beyond this point! The only exception are
1776 * INIT IPIs.
1777 */
1778 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1779 "entering PIC mode.\n");
1780 outb(0x70, 0x22);
1781 outb(0x00, 0x23);
1782 return;
1783 }
1784#endif
1785
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001786 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001787
1788 /* For the spurious interrupt use vector F, and enable it */
1789 value = apic_read(APIC_SPIV);
1790 value &= ~APIC_VECTOR_MASK;
1791 value |= APIC_SPIV_APIC_ENABLED;
1792 value |= 0xf;
1793 apic_write(APIC_SPIV, value);
1794
1795 if (!virt_wire_setup) {
1796 /*
1797 * For LVT0 make it edge triggered, active high,
1798 * external and enabled
1799 */
1800 value = apic_read(APIC_LVT0);
1801 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1802 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1803 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1804 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1805 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1806 apic_write(APIC_LVT0, value);
1807 } else {
1808 /* Disable LVT0 */
1809 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1810 }
1811
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001812 /*
1813 * For LVT1 make it edge triggered, active high,
1814 * nmi and enabled
1815 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001816 value = apic_read(APIC_LVT1);
1817 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1818 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1819 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1820 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1821 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1822 apic_write(APIC_LVT1, value);
1823}
1824
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001825void __cpuinit generic_processor_info(int apicid, int version)
1826{
1827 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001828
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001829 /*
1830 * Validate version
1831 */
1832 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001833 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001834 "fixing up to 0x10. (tell your hw vendor)\n",
1835 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001836 version = 0x10;
1837 }
1838 apic_version[apicid] = version;
1839
Mike Travis3b11ce72008-12-17 15:21:39 -08001840 if (num_processors >= nr_cpu_ids) {
1841 int max = nr_cpu_ids;
1842 int thiscpu = max + disabled_cpus;
1843
1844 pr_warning(
1845 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1846 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1847
1848 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001849 return;
1850 }
1851
1852 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001853 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001854
Mike Traviscef30b32009-01-16 15:58:13 -08001855 if (version != apic_version[boot_cpu_physical_apicid])
1856 WARN_ONCE(1,
1857 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1858 apic_version[boot_cpu_physical_apicid], cpu, version);
1859
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001860 physid_set(apicid, phys_cpu_present_map);
1861 if (apicid == boot_cpu_physical_apicid) {
1862 /*
1863 * x86_bios_cpu_apicid is required to have processors listed
1864 * in same order as logical cpu numbers. Hence the first
1865 * entry is BSP, and so on.
1866 */
1867 cpu = 0;
1868 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001869 if (apicid > max_physical_apicid)
1870 max_physical_apicid = apicid;
1871
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001872#ifdef CONFIG_X86_32
1873 /*
1874 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1875 * but we need to work other dependencies like SMP_SUSPEND etc
1876 * before this can be done without some confusion.
1877 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1878 * - Ashok Raj <ashok.raj@intel.com>
1879 */
1880 if (max_physical_apicid >= 8) {
1881 switch (boot_cpu_data.x86_vendor) {
1882 case X86_VENDOR_INTEL:
1883 if (!APIC_XAPIC(version)) {
1884 def_to_bigsmp = 0;
1885 break;
1886 }
1887 /* If P4 and above fall through */
1888 case X86_VENDOR_AMD:
1889 def_to_bigsmp = 1;
1890 }
1891 }
1892#endif
1893
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001894#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001895 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1896 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001897#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001898
Mike Travis1de88cd2008-12-16 17:34:02 -08001899 set_cpu_possible(cpu, true);
1900 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001901}
1902
Suresh Siddha0c81c742008-07-10 11:16:48 -07001903int hard_smp_processor_id(void)
1904{
1905 return read_apic_id();
1906}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001907
1908void default_init_apic_ldr(void)
1909{
1910 unsigned long val;
1911
1912 apic_write(APIC_DFR, APIC_DFR_VALUE);
1913 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1914 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1915 apic_write(APIC_LDR, val);
1916}
1917
1918#ifdef CONFIG_X86_32
1919int default_apicid_to_node(int logical_apicid)
1920{
1921#ifdef CONFIG_SMP
1922 return apicid_2_node[hard_smp_processor_id()];
1923#else
1924 return 0;
1925#endif
1926}
Yinghai Lu34919982008-08-24 02:01:48 -07001927#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07001928
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001929/*
1930 * Power management
1931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932#ifdef CONFIG_PM
1933
1934static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001935 /*
1936 * 'active' is true if the local APIC was enabled by us and
1937 * not the BIOS; this signifies that we are also responsible
1938 * for disabling it before entering apm/acpi suspend
1939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int active;
1941 /* r/w apic fields */
1942 unsigned int apic_id;
1943 unsigned int apic_taskpri;
1944 unsigned int apic_ldr;
1945 unsigned int apic_dfr;
1946 unsigned int apic_spiv;
1947 unsigned int apic_lvtt;
1948 unsigned int apic_lvtpc;
1949 unsigned int apic_lvt0;
1950 unsigned int apic_lvt1;
1951 unsigned int apic_lvterr;
1952 unsigned int apic_tmict;
1953 unsigned int apic_tdcr;
1954 unsigned int apic_thmr;
1955} apic_pm_state;
1956
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001957static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001960 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 if (!apic_pm_state.active)
1963 return 0;
1964
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001965 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001966
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001967 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1969 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1970 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1971 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1972 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001973 if (maxlvt >= 4)
1974 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1976 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1977 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1978 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1979 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001980#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001981 if (maxlvt >= 5)
1982 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1983#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001984
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001985 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 disable_local_APIC();
1987 local_irq_restore(flags);
1988 return 0;
1989}
1990
1991static int lapic_resume(struct sys_device *dev)
1992{
1993 unsigned int l, h;
1994 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001995 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 if (!apic_pm_state.active)
1998 return 0;
1999
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002000 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002003
Yinghai Lu49899ea2008-08-24 02:01:47 -07002004#ifdef HAVE_X2APIC
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002005 if (x2apic)
2006 enable_x2apic();
2007 else
2008#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07002009 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002010 /*
2011 * Make sure the APICBASE points to the right address
2012 *
2013 * FIXME! This will be wrong if we ever support suspend on
2014 * SMP! We'll need to do this as part of the CPU restore!
2015 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002016 rdmsr(MSR_IA32_APICBASE, l, h);
2017 l &= ~MSR_IA32_APICBASE_BASE;
2018 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2019 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002020 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2023 apic_write(APIC_ID, apic_pm_state.apic_id);
2024 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2025 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2026 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2027 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2028 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2029 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002030#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002031 if (maxlvt >= 5)
2032 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2033#endif
2034 if (maxlvt >= 4)
2035 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2037 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2038 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2039 apic_write(APIC_ESR, 0);
2040 apic_read(APIC_ESR);
2041 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2042 apic_write(APIC_ESR, 0);
2043 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return 0;
2048}
2049
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002050/*
2051 * This device has no shutdown method - fully functioning local APICs
2052 * are needed on every CPU up until machine_halt/restart/poweroff.
2053 */
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002056 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 .resume = lapic_resume,
2058 .suspend = lapic_suspend,
2059};
2060
2061static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002062 .id = 0,
2063 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064};
2065
Ashok Raje6982c62005-06-25 14:54:58 -07002066static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 apic_pm_state.active = 1;
2069}
2070
2071static int __init init_lapic_sysfs(void)
2072{
2073 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!cpu_has_apic)
2076 return 0;
2077 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 error = sysdev_class_register(&lapic_sysclass);
2080 if (!error)
2081 error = sysdev_register(&device_lapic);
2082 return error;
2083}
2084device_initcall(init_lapic_sysfs);
2085
2086#else /* CONFIG_PM */
2087
2088static void apic_pm_activate(void) { }
2089
2090#endif /* CONFIG_PM */
2091
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002092#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002094 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 *
2096 * Thus far, the major user of this is IBM's Summit2 series:
2097 *
Linus Torvalds637029c2006-02-27 20:41:56 -08002098 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * multi-chassis. Use available data to take a good guess.
2100 * If in doubt, go HPET.
2101 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002102__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int i, clusters, zeros;
2105 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002106 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2108
Yinghai Lu322850a2008-02-23 21:48:42 -08002109 /*
2110 * there is not this kind of box with AMD CPU yet.
2111 * Some AMD box with quadcore cpu and 8 sockets apicid
2112 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08002113 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08002114 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002115 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08002116 return 0;
2117
Mike Travis23ca4bb2008-05-12 21:21:12 +02002118 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002119 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Mike Travis168ef542008-12-16 17:34:01 -08002121 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002122 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002123 if (bios_cpu_apicid) {
2124 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302125 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002126 if (cpu_present(i))
2127 id = per_cpu(x86_bios_cpu_apicid, i);
2128 else
2129 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302130 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002131 break;
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (id != BAD_APICID)
2134 __set_bit(APIC_CLUSTERID(id), clustermap);
2135 }
2136
2137 /* Problem: Partially populated chassis may not have CPUs in some of
2138 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002139 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2140 * Since clusters are allocated sequentially, count zeros only if
2141 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 */
2143 clusters = 0;
2144 zeros = 0;
2145 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2146 if (test_bit(i, clustermap)) {
2147 clusters += 1 + zeros;
2148 zeros = 0;
2149 } else
2150 ++zeros;
2151 }
2152
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002153 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2154 * not guaranteed to be synced between boards
2155 */
2156 if (is_vsmp_box() && clusters > 1)
2157 return 1;
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002160 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 * May have to revisit this when multi-core + hyperthreaded CPUs come
2162 * out, but AFAIK this will work even for them.
2163 */
2164 return (clusters > 2);
2165}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002169 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002171static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002174 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002175 return 0;
2176}
2177early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002179/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002180static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002181{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002182 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002183}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002184early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002186static int __init parse_lapic_timer_c2_ok(char *arg)
2187{
2188 local_apic_timer_c2_ok = 1;
2189 return 0;
2190}
2191early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2192
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002193static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002196 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002197}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002198early_param("noapictimer", parse_disable_apic_timer);
2199
2200static int __init parse_nolapic_timer(char *arg)
2201{
2202 disable_apic_timer = 1;
2203 return 0;
2204}
2205early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002206
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002207static int __init apic_set_verbosity(char *arg)
2208{
2209 if (!arg) {
2210#ifdef CONFIG_X86_64
2211 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002212 return 0;
2213#endif
2214 return -EINVAL;
2215 }
2216
2217 if (strcmp("debug", arg) == 0)
2218 apic_verbosity = APIC_DEBUG;
2219 else if (strcmp("verbose", arg) == 0)
2220 apic_verbosity = APIC_VERBOSE;
2221 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002222 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002223 " use apic=verbose or apic=debug\n", arg);
2224 return -EINVAL;
2225 }
2226
2227 return 0;
2228}
2229early_param("apic", apic_set_verbosity);
2230
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002231static int __init lapic_insert_resource(void)
2232{
2233 if (!apic_phys)
2234 return -1;
2235
2236 /* Put local APIC into the resource map. */
2237 lapic_resource.start = apic_phys;
2238 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2239 insert_resource(&iomem_resource, &lapic_resource);
2240
2241 return 0;
2242}
2243
2244/*
2245 * need call insert after e820_reserve_resources()
2246 * that is using request_resource
2247 */
2248late_initcall(lapic_insert_resource);