blob: 29e7186b0e873914d4bea408a6bad19762d08886 [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/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
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070052/*
53 * Sanity check
54 */
55#if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
56# error SPURIOUS_APIC_VECTOR definition error
57#endif
58
Brian Gerstec70de82009-01-27 12:56:47 +090059unsigned int num_processors;
60unsigned disabled_cpus __cpuinitdata;
61/* Processor that is doing the boot up */
62unsigned int boot_cpu_physical_apicid = -1U;
63EXPORT_SYMBOL(boot_cpu_physical_apicid);
64unsigned int max_physical_apicid;
65
66/* Bitmask of physically existing CPUs */
67physid_mask_t phys_cpu_present_map;
68
69/*
70 * Map cpu index to physical APIC ID
71 */
72DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
73DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
74EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
75EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
76
Yinghai Lub3c51172008-08-24 02:01:46 -070077#ifdef CONFIG_X86_32
78/*
79 * Knob to control our willingness to enable the local APIC.
80 *
81 * +1=force-enable
82 */
83static int force_enable_local_apic;
84/*
85 * APIC command line parameters
86 */
87static int __init parse_lapic(char *arg)
88{
89 force_enable_local_apic = 1;
90 return 0;
91}
92early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -070093/* Local APIC was disabled by the BIOS and enabled by the kernel */
94static int enabled_via_apicbase;
95
Yinghai Lub3c51172008-08-24 02:01:46 -070096#endif
97
98#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +020099static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -0700100static __init int setup_apicpmtimer(char *s)
101{
102 apic_calibrate_pmtmr = 1;
103 notsc_setup(NULL);
104 return 0;
105}
106__setup("apicpmtimer", setup_apicpmtimer);
107#endif
108
Yinghai Lu49899ea2008-08-24 02:01:47 -0700109#ifdef CONFIG_X86_64
110#define HAVE_X2APIC
111#endif
112
113#ifdef HAVE_X2APIC
Suresh Siddha89027d32008-07-10 11:16:56 -0700114int x2apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700115/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530116static int x2apic_preenabled;
117static int disable_x2apic;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700118static __init int setup_nox2apic(char *str)
119{
120 disable_x2apic = 1;
121 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
122 return 0;
123}
124early_param("nox2apic", setup_nox2apic);
125#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Yinghai Lub3c51172008-08-24 02:01:46 -0700127unsigned long mp_lapic_addr;
128int disable_apic;
129/* Disable local APIC timer from the kernel commandline or via dmi quirk */
130static int disable_apic_timer __cpuinitdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100131/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700132int local_apic_timer_c2_ok;
133EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
134
Yinghai Luefa25592008-08-19 20:50:36 -0700135int first_system_vector = 0xfe;
136
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100137/*
138 * Debug level, exported for io_apic.c
139 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100140unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100141
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700142int pic_mode;
143
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400144/* Have we found an MP table */
145int smp_found_config;
146
Aaron Durbin39928722006-12-07 02:14:01 +0100147static struct resource lapic_resource = {
148 .name = "Local APIC",
149 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
150};
151
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200152static unsigned int calibration_result;
153
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200154static int lapic_next_event(unsigned long delta,
155 struct clock_event_device *evt);
156static void lapic_timer_setup(enum clock_event_mode mode,
157 struct clock_event_device *evt);
Mike Travis96289372008-12-31 18:08:46 -0800158static void lapic_timer_broadcast(const struct cpumask *mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100159static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200160
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400161/*
162 * The local apic timer can be used for any function which is CPU local.
163 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200164static struct clock_event_device lapic_clockevent = {
165 .name = "lapic",
166 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
167 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
168 .shift = 32,
169 .set_mode = lapic_timer_setup,
170 .set_next_event = lapic_next_event,
171 .broadcast = lapic_timer_broadcast,
172 .rating = 100,
173 .irq = -1,
174};
175static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
176
Andi Kleend3432892008-01-30 13:33:17 +0100177static unsigned long apic_phys;
178
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100179/*
180 * Get the LAPIC version
181 */
182static inline int lapic_get_version(void)
183{
184 return GET_APIC_VERSION(apic_read(APIC_LVR));
185}
186
187/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400188 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100189 */
190static inline int lapic_is_integrated(void)
191{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400192#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100193 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400194#else
195 return APIC_INTEGRATED(lapic_get_version());
196#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100197}
198
199/*
200 * Check, whether this is a modern or a first generation APIC
201 */
202static int modern_apic(void)
203{
204 /* AMD systems use old APIC versions, so check the CPU */
205 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
206 boot_cpu_data.x86 >= 0xf)
207 return 1;
208 return lapic_get_version() >= 0x14;
209}
210
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400211/*
212 * Paravirt kernels also might be using these below ops. So we still
213 * use generic apic_read()/apic_write(), which might be pointing to different
214 * ops in PARAVIRT case.
215 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700216void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100217{
218 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
219 cpu_relax();
220}
221
Suresh Siddha1b374e42008-07-10 11:16:49 -0700222u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100223{
224 u32 send_status;
225 int timeout;
226
227 timeout = 0;
228 do {
229 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
230 if (!send_status)
231 break;
232 udelay(100);
233 } while (timeout++ < 1000);
234
235 return send_status;
236}
237
Suresh Siddha1b374e42008-07-10 11:16:49 -0700238void xapic_icr_write(u32 low, u32 id)
239{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200240 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700241 apic_write(APIC_ICR, low);
242}
243
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530244static u64 xapic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700245{
246 u32 icr1, icr2;
247
248 icr2 = apic_read(APIC_ICR2);
249 icr1 = apic_read(APIC_ICR);
250
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400251 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700252}
253
254static struct apic_ops xapic_ops = {
255 .read = native_apic_mem_read,
256 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700257 .icr_read = xapic_icr_read,
258 .icr_write = xapic_icr_write,
259 .wait_icr_idle = xapic_wait_icr_idle,
260 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
261};
262
263struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700264EXPORT_SYMBOL_GPL(apic_ops);
265
Yinghai Lu49899ea2008-08-24 02:01:47 -0700266#ifdef HAVE_X2APIC
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700267static void x2apic_wait_icr_idle(void)
268{
269 /* no need to wait for icr idle in x2apic */
270 return;
271}
272
273static u32 safe_x2apic_wait_icr_idle(void)
274{
275 /* no need to wait for icr idle in x2apic */
276 return 0;
277}
278
279void x2apic_icr_write(u32 low, u32 id)
280{
281 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
282}
283
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530284static u64 x2apic_icr_read(void)
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700285{
286 unsigned long val;
287
288 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
289 return val;
290}
291
292static struct apic_ops x2apic_ops = {
293 .read = native_apic_msr_read,
294 .write = native_apic_msr_write,
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700295 .icr_read = x2apic_icr_read,
296 .icr_write = x2apic_icr_write,
297 .wait_icr_idle = x2apic_wait_icr_idle,
298 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
299};
Yinghai Lu49899ea2008-08-24 02:01:47 -0700300#endif
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700301
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100302/**
303 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
304 */
Jan Beuliche9427102008-01-30 13:31:24 +0100305void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100306{
307 unsigned int v;
308
309 /* unmask and set to NMI */
310 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200311
312 /* Level triggered for 82489DX (32bit mode) */
313 if (!lapic_is_integrated())
314 v |= APIC_LVT_LEVEL_TRIGGER;
315
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100316 apic_write(APIC_LVT0, v);
317}
318
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700319#ifdef CONFIG_X86_32
320/**
321 * get_physical_broadcast - Get number of physical broadcast IDs
322 */
323int get_physical_broadcast(void)
324{
325 return modern_apic() ? 0xff : 0xf;
326}
327#endif
328
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100329/**
330 * lapic_get_maxlvt - get the maximum number of local vector table entries
331 */
332int lapic_get_maxlvt(void)
333{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200334 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100335
336 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200337 /*
338 * - we always have APIC integrated on 64bit mode
339 * - 82489DXs do not report # of LVT entries
340 */
341 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100342}
343
344/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400345 * Local APIC timer
346 */
347
Cyrill Gorcunovc40aaec62008-08-18 20:45:55 +0400348/* Clock divisor */
Cyrill Gorcunovc40aaec62008-08-18 20:45:55 +0400349#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200350
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100351/*
352 * This function sets up the local APIC timer, with a timeout of
353 * 'clocks' APIC bus clock. During calibration we actually call
354 * this function twice on the boot CPU, once with a bogus timeout
355 * value, second time for real. The other (noncalibrating) CPUs
356 * call this function only once, with the real, calibrated value.
357 *
358 * We do reads before writes even if unnecessary, to get around the
359 * P5 APIC double write bug.
360 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100361static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
362{
363 unsigned int lvtt_value, tmp_value;
364
365 lvtt_value = LOCAL_TIMER_VECTOR;
366 if (!oneshot)
367 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200368 if (!lapic_is_integrated())
369 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
370
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100371 if (!irqen)
372 lvtt_value |= APIC_LVT_MASKED;
373
374 apic_write(APIC_LVTT, lvtt_value);
375
376 /*
377 * Divide PICLK by 16
378 */
379 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec62008-08-18 20:45:55 +0400380 apic_write(APIC_TDCR,
381 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
382 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100383
384 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200385 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100386}
387
388/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100389 * Setup extended LVT, AMD specific (K8, family 10h)
390 *
391 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
392 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200393 *
394 * If mask=1, the LVT entry does not generate interrupts while mask=0
395 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100396 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100397
398#define APIC_EILVT_LVTOFF_MCE 0
399#define APIC_EILVT_LVTOFF_IBS 1
400
401static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100402{
Robert Richter7b83dae2008-01-30 13:30:40 +0100403 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100404 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
405
406 apic_write(reg, v);
407}
408
Robert Richter7b83dae2008-01-30 13:30:40 +0100409u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
410{
411 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
412 return APIC_EILVT_LVTOFF_MCE;
413}
414
415u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
416{
417 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
418 return APIC_EILVT_LVTOFF_IBS;
419}
Robert Richter6aa360e2008-07-23 15:28:14 +0200420EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100421
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100422/*
423 * Program the next event, relative to now
424 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200425static int lapic_next_event(unsigned long delta,
426 struct clock_event_device *evt)
427{
428 apic_write(APIC_TMICT, delta);
429 return 0;
430}
431
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100432/*
433 * Setup the lapic timer in periodic or oneshot mode
434 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200435static void lapic_timer_setup(enum clock_event_mode mode,
436 struct clock_event_device *evt)
437{
438 unsigned long flags;
439 unsigned int v;
440
441 /* Lapic used as dummy for broadcast ? */
442 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
443 return;
444
445 local_irq_save(flags);
446
447 switch (mode) {
448 case CLOCK_EVT_MODE_PERIODIC:
449 case CLOCK_EVT_MODE_ONESHOT:
450 __setup_APIC_LVTT(calibration_result,
451 mode != CLOCK_EVT_MODE_PERIODIC, 1);
452 break;
453 case CLOCK_EVT_MODE_UNUSED:
454 case CLOCK_EVT_MODE_SHUTDOWN:
455 v = apic_read(APIC_LVTT);
456 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
457 apic_write(APIC_LVTT, v);
Thomas Gleixnera98f8fd2008-11-06 01:13:39 +0100458 apic_write(APIC_TMICT, 0xffffffff);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200459 break;
460 case CLOCK_EVT_MODE_RESUME:
461 /* Nothing to do here */
462 break;
463 }
464
465 local_irq_restore(flags);
466}
467
468/*
469 * Local APIC timer broadcast function
470 */
Mike Travis96289372008-12-31 18:08:46 -0800471static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200472{
473#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100474 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200475#endif
476}
477
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100478/*
479 * Setup the local APIC timer for this CPU. Copy the initilized values
480 * of the boot CPU and register the clock event in the framework.
481 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700482static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200483{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100484 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
485
486 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030487 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100488
489 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200490}
491
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700492/*
493 * In this functions we calibrate APIC bus clocks to the external timer.
494 *
495 * We want to do the calibration only once since we want to have local timer
496 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
497 * frequency.
498 *
499 * This was previously done by reading the PIT/HPET and waiting for a wrap
500 * around to find out, that a tick has elapsed. I have a box, where the PIT
501 * readout is broken, so it never gets out of the wait loop again. This was
502 * also reported by others.
503 *
504 * Monitoring the jiffies value is inaccurate and the clockevents
505 * infrastructure allows us to do a simple substitution of the interrupt
506 * handler.
507 *
508 * The calibration routine also uses the pm_timer when possible, as the PIT
509 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
510 * back to normal later in the boot process).
511 */
512
513#define LAPIC_CAL_LOOPS (HZ/10)
514
515static __initdata int lapic_cal_loops = -1;
516static __initdata long lapic_cal_t1, lapic_cal_t2;
517static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
518static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
519static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
520
521/*
522 * Temporary interrupt handler.
523 */
524static void __init lapic_cal_handler(struct clock_event_device *dev)
525{
526 unsigned long long tsc = 0;
527 long tapic = apic_read(APIC_TMCCT);
528 unsigned long pm = acpi_pm_read_early();
529
530 if (cpu_has_tsc)
531 rdtscll(tsc);
532
533 switch (lapic_cal_loops++) {
534 case 0:
535 lapic_cal_t1 = tapic;
536 lapic_cal_tsc1 = tsc;
537 lapic_cal_pm1 = pm;
538 lapic_cal_j1 = jiffies;
539 break;
540
541 case LAPIC_CAL_LOOPS:
542 lapic_cal_t2 = tapic;
543 lapic_cal_tsc2 = tsc;
544 if (pm < lapic_cal_pm1)
545 pm += ACPI_PM_OVRRUN;
546 lapic_cal_pm2 = pm;
547 lapic_cal_j2 = jiffies;
548 break;
549 }
550}
551
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400552static int __init calibrate_by_pmtimer(long deltapm, long *delta)
553{
554 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
555 const long pm_thresh = pm_100ms / 100;
556 unsigned long mult;
557 u64 res;
558
559#ifndef CONFIG_X86_PM_TIMER
560 return -1;
561#endif
562
563 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
564
565 /* Check, if the PM timer is available */
566 if (!deltapm)
567 return -1;
568
569 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
570
571 if (deltapm > (pm_100ms - pm_thresh) &&
572 deltapm < (pm_100ms + pm_thresh)) {
573 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
574 } else {
575 res = (((u64)deltapm) * mult) >> 22;
576 do_div(res, 1000000);
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100577 pr_warning("APIC calibration not consistent "
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400578 "with PM Timer: %ldms instead of 100ms\n",
579 (long)res);
580 /* Correct the lapic counter value */
581 res = (((u64)(*delta)) * pm_100ms);
582 do_div(res, deltapm);
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100583 pr_info("APIC delta adjusted to PM-Timer: "
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400584 "%lu (%ld)\n", (unsigned long)res, *delta);
585 *delta = (long)res;
586 }
587
588 return 0;
589}
590
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700591static int __init calibrate_APIC_clock(void)
592{
593 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700594 void (*real_handler)(struct clock_event_device *dev);
595 unsigned long deltaj;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400596 long delta;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700597 int pm_referenced = 0;
598
599 local_irq_disable();
600
601 /* Replace the global interrupt handler */
602 real_handler = global_clock_event->event_handler;
603 global_clock_event->event_handler = lapic_cal_handler;
604
605 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400606 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700607 * can underflow in the 100ms detection time frame
608 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400609 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700610
611 /* Let the interrupts run */
612 local_irq_enable();
613
614 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
615 cpu_relax();
616
617 local_irq_disable();
618
619 /* Restore the real event handler */
620 global_clock_event->event_handler = real_handler;
621
622 /* Build delta t1-t2 as apic timer counts down */
623 delta = lapic_cal_t1 - lapic_cal_t2;
624 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
625
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400626 /* we trust the PM based calibration if possible */
627 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
628 &delta);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700629
630 /* Calculate the scaled math multiplication factor */
631 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
632 lapic_clockevent.shift);
633 lapic_clockevent.max_delta_ns =
634 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
635 lapic_clockevent.min_delta_ns =
636 clockevent_delta2ns(0xF, &lapic_clockevent);
637
638 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
639
640 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
641 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
642 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
643 calibration_result);
644
645 if (cpu_has_tsc) {
646 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
647 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
648 "%ld.%04ld MHz.\n",
649 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
650 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
651 }
652
653 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
654 "%u.%04u MHz.\n",
655 calibration_result / (1000000 / HZ),
656 calibration_result % (1000000 / HZ));
657
658 /*
659 * Do a sanity check on the APIC calibration result
660 */
661 if (calibration_result < (1000000 / HZ)) {
662 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100663 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700664 return -1;
665 }
666
667 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
668
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400669 /*
670 * PM timer calibration failed or not turned on
671 * so lets try APIC timer based calibration
672 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700673 if (!pm_referenced) {
674 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
675
676 /*
677 * Setup the apic timer manually
678 */
679 levt->event_handler = lapic_cal_handler;
680 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
681 lapic_cal_loops = -1;
682
683 /* Let the interrupts run */
684 local_irq_enable();
685
686 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
687 cpu_relax();
688
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700689 /* Stop the lapic timer */
690 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
691
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700692 /* Jiffies delta */
693 deltaj = lapic_cal_j2 - lapic_cal_j1;
694 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
695
696 /* Check, if the jiffies result is consistent */
697 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
698 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
699 else
700 levt->features |= CLOCK_EVT_FEAT_DUMMY;
701 } else
702 local_irq_enable();
703
704 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530705 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700706 return -1;
707 }
708
709 return 0;
710}
711
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100712/*
713 * Setup the boot APIC
714 *
715 * Calibrate and verify the result.
716 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100717void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100719 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400720 * The local apic timer can be disabled via the kernel
721 * commandline or from the CPU detection code. Register the lapic
722 * timer as a dummy clock event source on SMP systems, so the
723 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100724 */
725 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100726 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100727 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100728 if (num_possible_cpus() > 1) {
729 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100730 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100731 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100732 return;
733 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200734
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400735 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
736 "calibrating APIC timer ...\n");
737
Cyrill Gorcunov89b3b1f42008-07-15 21:02:54 +0400738 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100739 /* No broadcast on UP ! */
740 if (num_possible_cpus() > 1)
741 setup_APIC_timer();
742 return;
743 }
744
745 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100746 * If nmi_watchdog is set to IO_APIC, we need the
747 * PIT/HPET going. Otherwise register lapic as a dummy
748 * device.
749 */
750 if (nmi_watchdog != NMI_IO_APIC)
751 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
752 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100753 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200754 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100755
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400756 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100757 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100760void __cpuinit setup_secondary_APIC_clock(void)
761{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100762 setup_APIC_timer();
763}
764
765/*
766 * The guts of the apic timer interrupt
767 */
768static void local_apic_timer_interrupt(void)
769{
770 int cpu = smp_processor_id();
771 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
772
773 /*
774 * Normally we should not be here till LAPIC has been initialized but
775 * in some cases like kdump, its possible that there is a pending LAPIC
776 * timer interrupt from previous kernel's context and is delivered in
777 * new kernel the moment interrupts are enabled.
778 *
779 * Interrupts are enabled early and LAPIC is setup much later, hence
780 * its possible that when we get here evt->event_handler is NULL.
781 * Check for event_handler being NULL and discard the interrupt as
782 * spurious.
783 */
784 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100785 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100786 /* Switch it off */
787 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
788 return;
789 }
790
791 /*
792 * the NMI deadlock-detector uses this.
793 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800794 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100795
796 evt->event_handler(evt);
797}
798
799/*
800 * Local APIC timer interrupt. This is the most natural way for doing
801 * local interrupts, but local timer interrupts can be emulated by
802 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
803 *
804 * [ if a single-CPU system runs an SMP kernel then we call the local
805 * interrupt as well. Thus we cannot inline the local irq ... ]
806 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100807void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100808{
809 struct pt_regs *old_regs = set_irq_regs(regs);
810
811 /*
812 * NOTE! We'd better ACK the irq immediately,
813 * because timer handling can be slow.
814 */
815 ack_APIC_irq();
816 /*
817 * update_process_times() expects us to have done irq_enter().
818 * Besides, if we don't timer interrupts ignore the global
819 * interrupt lock, which is the WrongThing (tm) to do.
820 */
821 exit_idle();
822 irq_enter();
823 local_apic_timer_interrupt();
824 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400825
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100826 set_irq_regs(old_regs);
827}
828
829int setup_profiling_timer(unsigned int multiplier)
830{
831 return -EINVAL;
832}
833
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100834/*
835 * Local APIC start and shutdown
836 */
837
838/**
839 * clear_local_APIC - shutdown the local APIC
840 *
841 * This is called, when a CPU is disabled and before rebooting, so the state of
842 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
843 * leftovers during boot.
844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845void clear_local_APIC(void)
846{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400847 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100848 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Andi Kleend3432892008-01-30 13:33:17 +0100850 /* APIC hasn't been mapped yet */
851 if (!apic_phys)
852 return;
853
854 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200856 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * if the vector is zero. Mask LVTERR first to prevent this.
858 */
859 if (maxlvt >= 3) {
860 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100861 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 /*
864 * Careful: we have to set masks only first to deassert
865 * any level-triggered sources.
866 */
867 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100868 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100870 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100872 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (maxlvt >= 4) {
874 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100875 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400878 /* lets not touch this if we didn't frob it */
879#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
880 if (maxlvt >= 5) {
881 v = apic_read(APIC_LVTTHMR);
882 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
883 }
884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /*
886 * Clean APIC state for other OSs:
887 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100888 apic_write(APIC_LVTT, APIC_LVT_MASKED);
889 apic_write(APIC_LVT0, APIC_LVT_MASKED);
890 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100892 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100894 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400895
896 /* Integrated APIC (!82489DX) ? */
897 if (lapic_is_integrated()) {
898 if (maxlvt > 3)
899 /* Clear ESR due to Pentium errata 3AP and 11AP */
900 apic_write(APIC_ESR, 0);
901 apic_read(APIC_ESR);
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100905/**
906 * disable_local_APIC - clear and disable the local APIC
907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908void disable_local_APIC(void)
909{
910 unsigned int value;
911
Jan Beulicha08c4742009-01-14 12:28:51 +0000912 /* APIC hasn't been mapped yet */
913 if (!apic_phys)
914 return;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 clear_local_APIC();
917
918 /*
919 * Disable APIC (implies clearing of registers
920 * for 82489DX!).
921 */
922 value = apic_read(APIC_SPIV);
923 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100924 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400925
926#ifdef CONFIG_X86_32
927 /*
928 * When LAPIC was disabled by the BIOS and enabled by the kernel,
929 * restore the disabled state.
930 */
931 if (enabled_via_apicbase) {
932 unsigned int l, h;
933
934 rdmsr(MSR_IA32_APICBASE, l, h);
935 l &= ~MSR_IA32_APICBASE_ENABLE;
936 wrmsr(MSR_IA32_APICBASE, l, h);
937 }
938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400941/*
942 * If Linux enabled the LAPIC against the BIOS default disable it down before
943 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
944 * not power-off. Additionally clear all LVT entries before disable_local_APIC
945 * for the case where Linux didn't enable the LAPIC.
946 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700947void lapic_shutdown(void)
948{
949 unsigned long flags;
950
951 if (!cpu_has_apic)
952 return;
953
954 local_irq_save(flags);
955
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400956#ifdef CONFIG_X86_32
957 if (!enabled_via_apicbase)
958 clear_local_APIC();
959 else
960#endif
961 disable_local_APIC();
962
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700963
964 local_irq_restore(flags);
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
968 * This is to verify that we're looking at a real local APIC.
969 * Check these against your board if the CPUs aren't getting
970 * started for no apparent reason.
971 */
972int __init verify_local_APIC(void)
973{
974 unsigned int reg0, reg1;
975
976 /*
977 * The version register is read-only in a real APIC.
978 */
979 reg0 = apic_read(APIC_LVR);
980 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
981 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
982 reg1 = apic_read(APIC_LVR);
983 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
984
985 /*
986 * The two version reads above should print the same
987 * numbers. If the second one is different, then we
988 * poke at a non-APIC.
989 */
990 if (reg1 != reg0)
991 return 0;
992
993 /*
994 * Check if the version looks reasonably.
995 */
996 reg1 = GET_APIC_VERSION(reg0);
997 if (reg1 == 0x00 || reg1 == 0xff)
998 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100999 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (reg1 < 0x02 || reg1 == 0xff)
1001 return 0;
1002
1003 /*
1004 * The ID register is read/write in a real APIC.
1005 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001006 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001008 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001009 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1011 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001012 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014
1015 /*
1016 * The next two are just to see if we have sane values.
1017 * They're only really relevant if we're in Virtual Wire
1018 * compatibility mode, but most boxes are anymore.
1019 */
1020 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001021 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 reg1 = apic_read(APIC_LVT1);
1023 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1024
1025 return 1;
1026}
1027
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001028/**
1029 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031void __init sync_Arb_IDs(void)
1032{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001033 /*
1034 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1035 * needed on AMD.
1036 */
1037 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
1039
1040 /*
1041 * Wait for idle.
1042 */
1043 apic_wait_icr_idle();
1044
1045 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001046 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1047 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*
1051 * An initial setup of the virtual wire mode.
1052 */
1053void __init init_bsp_APIC(void)
1054{
Andi Kleen11a8e772006-01-11 22:46:51 +01001055 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /*
1058 * Don't do the setup now if we have a SMP BIOS as the
1059 * through-I/O-APIC virtual wire mode might be active.
1060 */
1061 if (smp_found_config || !cpu_has_apic)
1062 return;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /*
1065 * Do not trust the local APIC being empty at bootup.
1066 */
1067 clear_local_APIC();
1068
1069 /*
1070 * Enable APIC.
1071 */
1072 value = apic_read(APIC_SPIV);
1073 value &= ~APIC_VECTOR_MASK;
1074 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001075
1076#ifdef CONFIG_X86_32
1077 /* This bit is reserved on P4/Xeon and should be cleared */
1078 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1079 (boot_cpu_data.x86 == 15))
1080 value &= ~APIC_SPIV_FOCUS_DISABLED;
1081 else
1082#endif
1083 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001085 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Set up the virtual wire mode.
1089 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001090 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001092 if (!lapic_is_integrated()) /* 82489DX */
1093 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001094 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001097static void __cpuinit lapic_setup_esr(void)
1098{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001099 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001100
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001101 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001102 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001103 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001104 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001105
Ingo Molnar08125d32009-01-28 05:08:44 +01001106 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001107 /*
1108 * Something untraceable is creating bad interrupts on
1109 * secondary quads ... for the moment, just leave the
1110 * ESR disabled - we can't do anything useful with the
1111 * errors anyway - mbligh
1112 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001113 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001114 return;
1115 }
1116
1117 maxlvt = lapic_get_maxlvt();
1118 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1119 apic_write(APIC_ESR, 0);
1120 oldvalue = apic_read(APIC_ESR);
1121
1122 /* enables sending errors */
1123 value = ERROR_APIC_VECTOR;
1124 apic_write(APIC_LVTERR, value);
1125
1126 /*
1127 * spec says clear errors after enabling vector.
1128 */
1129 if (maxlvt > 3)
1130 apic_write(APIC_ESR, 0);
1131 value = apic_read(APIC_ESR);
1132 if (value != oldvalue)
1133 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1134 "vector: 0x%08x after: 0x%08x\n",
1135 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001136}
1137
1138
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001139/**
1140 * setup_local_APIC - setup the local APIC
1141 */
1142void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Andi Kleen739f33b2008-01-30 13:30:40 +01001144 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001145 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jan Beulichf1182632009-01-14 12:27:35 +00001147 if (disable_apic) {
Ingo Molnar5cdc5e9e2009-01-19 20:49:37 +01001148#ifdef CONFIG_X86_IO_APIC
Jan Beulichf1182632009-01-14 12:27:35 +00001149 disable_ioapic_setup();
Ingo Molnar5cdc5e9e2009-01-19 20:49:37 +01001150#endif
Jan Beulichf1182632009-01-14 12:27:35 +00001151 return;
1152 }
1153
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001154#ifdef CONFIG_X86_32
1155 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001156 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001157 apic_write(APIC_ESR, 0);
1158 apic_write(APIC_ESR, 0);
1159 apic_write(APIC_ESR, 0);
1160 apic_write(APIC_ESR, 0);
1161 }
1162#endif
1163
Jack Steinerac23d4e2008-03-28 14:12:16 -05001164 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /*
1167 * Double-check whether this APIC is really registered.
1168 * This is meaningless in clustered apic mode, so we skip it.
1169 */
Ingo Molnar7ed248d2009-01-28 03:43:47 +01001170 if (!apic->apic_id_registered())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 BUG();
1172
1173 /*
1174 * Intel recommends to set DFR, LDR and TPR before enabling
1175 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1176 * document number 292116). So here it goes...
1177 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001178 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 /*
1181 * Set Task Priority to 'accept all'. We never change this
1182 * later on.
1183 */
1184 value = apic_read(APIC_TASKPRI);
1185 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001186 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001189 * After a crash, we no longer service the interrupts and a pending
1190 * interrupt from previous kernel might still have ISR bit set.
1191 *
1192 * Most probably by now CPU has serviced that pending interrupt and
1193 * it might not have done the ack_APIC_irq() because it thought,
1194 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1195 * does not clear the ISR bit and cpu thinks it has already serivced
1196 * the interrupt. Hence a vector might get locked. It was noticed
1197 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1198 */
1199 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1200 value = apic_read(APIC_ISR + i*0x10);
1201 for (j = 31; j >= 0; j--) {
1202 if (value & (1<<j))
1203 ack_APIC_irq();
1204 }
1205 }
1206
1207 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * Now that we are all set up, enable the APIC
1209 */
1210 value = apic_read(APIC_SPIV);
1211 value &= ~APIC_VECTOR_MASK;
1212 /*
1213 * Enable APIC
1214 */
1215 value |= APIC_SPIV_APIC_ENABLED;
1216
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001217#ifdef CONFIG_X86_32
1218 /*
1219 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1220 * certain networking cards. If high frequency interrupts are
1221 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1222 * entry is masked/unmasked at a high rate as well then sooner or
1223 * later IOAPIC line gets 'stuck', no more interrupts are received
1224 * from the device. If focus CPU is disabled then the hang goes
1225 * away, oh well :-(
1226 *
1227 * [ This bug can be reproduced easily with a level-triggered
1228 * PCI Ne2000 networking cards and PII/PIII processors, dual
1229 * BX chipset. ]
1230 */
1231 /*
1232 * Actually disabling the focus CPU check just makes the hang less
1233 * frequent as it makes the interrupt distributon model be more
1234 * like LRU than MRU (the short-term load is more even across CPUs).
1235 * See also the comment in end_level_ioapic_irq(). --macro
1236 */
1237
1238 /*
1239 * - enable focus processor (bit==0)
1240 * - 64bit mode always use processor focus
1241 * so no need to set it
1242 */
1243 value &= ~APIC_SPIV_FOCUS_DISABLED;
1244#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Set spurious IRQ vector
1248 */
1249 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001250 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
1253 * Set up LVT0, LVT1:
1254 *
1255 * set up through-local-APIC on the BP's LINT0. This is not
1256 * strictly necessary in pure symmetric-IO mode, but sometimes
1257 * we delegate interrupts to the 8259A.
1258 */
1259 /*
1260 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1261 */
1262 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001263 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001265 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001266 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } else {
1268 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001269 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001270 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001272 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /*
1275 * only the BP should see the LINT1 NMI signal, obviously.
1276 */
1277 if (!smp_processor_id())
1278 value = APIC_DM_NMI;
1279 else
1280 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001281 if (!lapic_is_integrated()) /* 82489DX */
1282 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001283 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001284
Jack Steinerac23d4e2008-03-28 14:12:16 -05001285 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Andi Kleen739f33b2008-01-30 13:30:40 +01001288void __cpuinit end_local_APIC_setup(void)
1289{
1290 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001291
1292#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001293 {
1294 unsigned int value;
1295 /* Disable the local apic timer */
1296 value = apic_read(APIC_LVTT);
1297 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1298 apic_write(APIC_LVTT, value);
1299 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001300#endif
1301
Don Zickusf2802e72006-09-26 10:52:26 +02001302 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 apic_pm_activate();
1304}
1305
Yinghai Lu49899ea2008-08-24 02:01:47 -07001306#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001307void check_x2apic(void)
1308{
1309 int msr, msr2;
1310
1311 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1312
1313 if (msr & X2APIC_ENABLE) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001314 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001315 x2apic_preenabled = x2apic = 1;
1316 apic_ops = &x2apic_ops;
1317 }
1318}
1319
1320void enable_x2apic(void)
1321{
1322 int msr, msr2;
1323
1324 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1325 if (!(msr & X2APIC_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001326 pr_info("Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001327 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1328 }
1329}
1330
Al Viro2236d252008-11-22 17:37:34 +00001331void __init enable_IR_x2apic(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001332{
1333#ifdef CONFIG_INTR_REMAP
1334 int ret;
1335 unsigned long flags;
1336
1337 if (!cpu_has_x2apic)
1338 return;
1339
1340 if (!x2apic_preenabled && disable_x2apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001341 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1342 "because of nox2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001343 return;
1344 }
1345
1346 if (x2apic_preenabled && disable_x2apic)
1347 panic("Bios already enabled x2apic, can't enforce nox2apic");
1348
1349 if (!x2apic_preenabled && skip_ioapic_setup) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001350 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1351 "because of skipping io-apic setup\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001352 return;
1353 }
1354
1355 ret = dmar_table_init();
1356 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001357 pr_info("dmar_table_init() failed with %d:\n", ret);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001358
1359 if (x2apic_preenabled)
1360 panic("x2apic enabled by bios. But IR enabling failed");
1361 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001362 pr_info("Not enabling x2apic,Intr-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001363 return;
1364 }
1365
1366 local_irq_save(flags);
1367 mask_8259A();
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001368
1369 ret = save_mask_IO_APIC_setup();
1370 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001371 pr_info("Saving IO-APIC state failed: %d\n", ret);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001372 goto end;
1373 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001374
1375 ret = enable_intr_remapping(1);
1376
1377 if (ret && x2apic_preenabled) {
1378 local_irq_restore(flags);
1379 panic("x2apic enabled by bios. But IR enabling failed");
1380 }
1381
1382 if (ret)
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001383 goto end_restore;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001384
1385 if (!x2apic) {
1386 x2apic = 1;
1387 apic_ops = &x2apic_ops;
1388 enable_x2apic();
1389 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001390
1391end_restore:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001392 if (ret)
1393 /*
1394 * IR enabling failed
1395 */
1396 restore_IO_APIC_setup();
1397 else
1398 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1399
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001400end:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001401 unmask_8259A();
1402 local_irq_restore(flags);
1403
1404 if (!ret) {
1405 if (!x2apic_preenabled)
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001406 pr_info("Enabled x2apic and interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001407 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001408 pr_info("Enabled Interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001409 } else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001410 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001411#else
1412 if (!cpu_has_x2apic)
1413 return;
1414
1415 if (x2apic_preenabled)
1416 panic("x2apic enabled prior OS handover,"
1417 " enable CONFIG_INTR_REMAP");
1418
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001419 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1420 " and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001421#endif
1422
1423 return;
1424}
Yinghai Lu49899ea2008-08-24 02:01:47 -07001425#endif /* HAVE_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001426
Yinghai Lube7a6562008-08-24 02:01:51 -07001427#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001428/*
1429 * Detect and enable local APICs on non-SMP boards.
1430 * Original code written by Keir Fraser.
1431 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1432 * not correctly set up (usually the APIC timer won't work etc.)
1433 */
1434static int __init detect_init_APIC(void)
1435{
1436 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001437 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001438 return -1;
1439 }
1440
1441 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001442 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001443 return 0;
1444}
Yinghai Lube7a6562008-08-24 02:01:51 -07001445#else
1446/*
1447 * Detect and initialize APIC
1448 */
1449static int __init detect_init_APIC(void)
1450{
1451 u32 h, l, features;
1452
1453 /* Disabled by kernel option? */
1454 if (disable_apic)
1455 return -1;
1456
1457 switch (boot_cpu_data.x86_vendor) {
1458 case X86_VENDOR_AMD:
1459 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1460 (boot_cpu_data.x86 == 15))
1461 break;
1462 goto no_apic;
1463 case X86_VENDOR_INTEL:
1464 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1465 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1466 break;
1467 goto no_apic;
1468 default:
1469 goto no_apic;
1470 }
1471
1472 if (!cpu_has_apic) {
1473 /*
1474 * Over-ride BIOS and try to enable the local APIC only if
1475 * "lapic" specified.
1476 */
1477 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001478 pr_info("Local APIC disabled by BIOS -- "
1479 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001480 return -1;
1481 }
1482 /*
1483 * Some BIOSes disable the local APIC in the APIC_BASE
1484 * MSR. This can only be done in software for Intel P6 or later
1485 * and AMD K7 (Model > 1) or later.
1486 */
1487 rdmsr(MSR_IA32_APICBASE, l, h);
1488 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001489 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001490 l &= ~MSR_IA32_APICBASE_BASE;
1491 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1492 wrmsr(MSR_IA32_APICBASE, l, h);
1493 enabled_via_apicbase = 1;
1494 }
1495 }
1496 /*
1497 * The APIC feature bit should now be enabled
1498 * in `cpuid'
1499 */
1500 features = cpuid_edx(1);
1501 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001502 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001503 return -1;
1504 }
1505 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1506 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1507
1508 /* The BIOS may have set up the APIC at some other address */
1509 rdmsr(MSR_IA32_APICBASE, l, h);
1510 if (l & MSR_IA32_APICBASE_ENABLE)
1511 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1512
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001513 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001514
1515 apic_pm_activate();
1516
1517 return 0;
1518
1519no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001520 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001521 return -1;
1522}
1523#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001524
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001525#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001526void __init early_init_lapic_mapping(void)
1527{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001528 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001529
1530 /*
1531 * If no local APIC can be found then go out
1532 * : it means there is no mpatable and MADT
1533 */
1534 if (!smp_found_config)
1535 return;
1536
Thomas Gleixner431ee792008-05-12 15:43:35 +02001537 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001538
Thomas Gleixner431ee792008-05-12 15:43:35 +02001539 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001540 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001541 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001542
1543 /*
1544 * Fetch the APIC ID of the BSP in case we have a
1545 * default configuration (or the MP table is broken).
1546 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001547 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001548}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001549#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001550
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001551/**
1552 * init_apic_mappings - initialize APIC mappings
1553 */
1554void __init init_apic_mappings(void)
1555{
Yinghai Lu49899ea2008-08-24 02:01:47 -07001556#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001557 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001558 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001559 return;
1560 }
Yinghai Lu49899ea2008-08-24 02:01:47 -07001561#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001562
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001563 /*
1564 * If no local APIC can be found then set up a fake all
1565 * zeroes page to simulate the local APIC and another
1566 * one for the IO-APIC.
1567 */
1568 if (!smp_found_config && detect_init_APIC()) {
1569 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1570 apic_phys = __pa(apic_phys);
1571 } else
1572 apic_phys = mp_lapic_addr;
1573
1574 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu79c09692008-09-07 17:58:57 -07001575 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001576 APIC_BASE, apic_phys);
1577
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001578 /*
1579 * Fetch the APIC ID of the BSP in case we have a
1580 * default configuration (or the MP table is broken).
1581 */
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001582 if (boot_cpu_physical_apicid == -1U)
1583 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001584}
1585
1586/*
1587 * This initializes the IO-APIC and APIC hardware if this is
1588 * a UP kernel.
1589 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001590int apic_version[MAX_APICS];
1591
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001592int __init APIC_init_uniprocessor(void)
1593{
1594 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001595 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001596 return -1;
1597 }
Jan Beulichf1182632009-01-14 12:27:35 +00001598#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001599 if (!cpu_has_apic) {
1600 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001601 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001602 return -1;
1603 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001604#else
1605 if (!smp_found_config && !cpu_has_apic)
1606 return -1;
1607
1608 /*
1609 * Complain if the BIOS pretends there is one.
1610 */
1611 if (!cpu_has_apic &&
1612 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001613 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1614 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001615 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1616 return -1;
1617 }
1618#endif
1619
Yinghai Lu49899ea2008-08-24 02:01:47 -07001620#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001621 enable_IR_x2apic();
Yinghai Lu49899ea2008-08-24 02:01:47 -07001622#endif
Yinghai Lufa2bd352008-08-24 02:01:50 -07001623#ifdef CONFIG_X86_64
Ingo Molnar72ce0162009-01-28 06:50:47 +01001624 default_setup_apic_routing();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001625#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001626
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001627 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001628 connect_bsp_APIC();
1629
Yinghai Lufa2bd352008-08-24 02:01:50 -07001630#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001631 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001632#else
1633 /*
1634 * Hack: In case of kdump, after a crash, kernel might be booting
1635 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1636 * might be zero if read from MP tables. Get it from LAPIC.
1637 */
1638# ifdef CONFIG_CRASH_DUMP
1639 boot_cpu_physical_apicid = read_apic_id();
1640# endif
1641#endif
1642 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001643 setup_local_APIC();
1644
Yinghai Lufa2bd352008-08-24 02:01:50 -07001645#ifdef CONFIG_X86_64
Andi Kleen739f33b2008-01-30 13:30:40 +01001646 /*
1647 * Now enable IO-APICs, actually call clear_IO_APIC
1648 * We need clear_IO_APIC before enabling vector on BP
1649 */
1650 if (!skip_ioapic_setup && nr_ioapics)
1651 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001652#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001653
Yinghai Lufa2bd352008-08-24 02:01:50 -07001654#ifdef CONFIG_X86_IO_APIC
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001655 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
Yinghai Lufa2bd352008-08-24 02:01:50 -07001656#endif
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001657 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001658 end_local_APIC_setup();
1659
Yinghai Lufa2bd352008-08-24 02:01:50 -07001660#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001661 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1662 setup_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001663# ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001664 else
1665 nr_ioapics = 0;
Yinghai Lufa2bd352008-08-24 02:01:50 -07001666# endif
1667#endif
1668
1669#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001670 setup_boot_APIC_clock();
1671 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001672#else
1673 setup_boot_clock();
1674#endif
1675
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001676 return 0;
1677}
1678
1679/*
1680 * Local APIC interrupts
1681 */
1682
1683/*
1684 * This interrupt should _never_ happen with our APIC/SMP architecture
1685 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001686void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001687{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001688 u32 v;
1689
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001690 exit_idle();
1691 irq_enter();
1692 /*
1693 * Check if this really is a spurious interrupt and ACK it
1694 * if it is a vectored one. Just in case...
1695 * Spurious interrupts should not be ACKed.
1696 */
1697 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1698 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1699 ack_APIC_irq();
1700
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001701 inc_irq_stat(irq_spurious_count);
1702
Yinghai Ludc1528d2008-08-24 02:01:53 -07001703 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001704 pr_info("spurious APIC interrupt on CPU#%d, "
1705 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001706 irq_exit();
1707}
1708
1709/*
1710 * This interrupt should never happen with our APIC/SMP architecture
1711 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001712void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001713{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001714 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001715
1716 exit_idle();
1717 irq_enter();
1718 /* First tickle the hardware, only then report what went on. -- REW */
1719 v = apic_read(APIC_ESR);
1720 apic_write(APIC_ESR, 0);
1721 v1 = apic_read(APIC_ESR);
1722 ack_APIC_irq();
1723 atomic_inc(&irq_err_count);
1724
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001725 /*
1726 * Here is what the APIC error bits mean:
1727 * 0: Send CS error
1728 * 1: Receive CS error
1729 * 2: Send accept error
1730 * 3: Receive accept error
1731 * 4: Reserved
1732 * 5: Send illegal vector
1733 * 6: Received illegal vector
1734 * 7: Illegal register address
1735 */
1736 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001737 smp_processor_id(), v , v1);
1738 irq_exit();
1739}
1740
Glauber Costab5841762008-05-28 13:38:28 -03001741/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001742 * connect_bsp_APIC - attach the APIC to the interrupt system
1743 */
Glauber Costab5841762008-05-28 13:38:28 -03001744void __init connect_bsp_APIC(void)
1745{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001746#ifdef CONFIG_X86_32
1747 if (pic_mode) {
1748 /*
1749 * Do not trust the local APIC being empty at bootup.
1750 */
1751 clear_local_APIC();
1752 /*
1753 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1754 * local APIC to INT and NMI lines.
1755 */
1756 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1757 "enabling APIC mode.\n");
1758 outb(0x70, 0x22);
1759 outb(0x01, 0x23);
1760 }
1761#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001762 if (apic->enable_apic_mode)
1763 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001764}
1765
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001766/**
1767 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1768 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1769 *
1770 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1771 * APIC is disabled.
1772 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001773void disconnect_bsp_APIC(int virt_wire_setup)
1774{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001775 unsigned int value;
1776
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001777#ifdef CONFIG_X86_32
1778 if (pic_mode) {
1779 /*
1780 * Put the board back into PIC mode (has an effect only on
1781 * certain older boards). Note that APIC interrupts, including
1782 * IPIs, won't work beyond this point! The only exception are
1783 * INIT IPIs.
1784 */
1785 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1786 "entering PIC mode.\n");
1787 outb(0x70, 0x22);
1788 outb(0x00, 0x23);
1789 return;
1790 }
1791#endif
1792
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001793 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001794
1795 /* For the spurious interrupt use vector F, and enable it */
1796 value = apic_read(APIC_SPIV);
1797 value &= ~APIC_VECTOR_MASK;
1798 value |= APIC_SPIV_APIC_ENABLED;
1799 value |= 0xf;
1800 apic_write(APIC_SPIV, value);
1801
1802 if (!virt_wire_setup) {
1803 /*
1804 * For LVT0 make it edge triggered, active high,
1805 * external and enabled
1806 */
1807 value = apic_read(APIC_LVT0);
1808 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1809 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1810 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1811 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1812 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1813 apic_write(APIC_LVT0, value);
1814 } else {
1815 /* Disable LVT0 */
1816 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1817 }
1818
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001819 /*
1820 * For LVT1 make it edge triggered, active high,
1821 * nmi and enabled
1822 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001823 value = apic_read(APIC_LVT1);
1824 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1825 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1826 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1827 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1828 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1829 apic_write(APIC_LVT1, value);
1830}
1831
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001832void __cpuinit generic_processor_info(int apicid, int version)
1833{
1834 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001835
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001836 /*
1837 * Validate version
1838 */
1839 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001840 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001841 "fixing up to 0x10. (tell your hw vendor)\n",
1842 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001843 version = 0x10;
1844 }
1845 apic_version[apicid] = version;
1846
Mike Travis3b11ce72008-12-17 15:21:39 -08001847 if (num_processors >= nr_cpu_ids) {
1848 int max = nr_cpu_ids;
1849 int thiscpu = max + disabled_cpus;
1850
1851 pr_warning(
1852 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1853 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1854
1855 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001856 return;
1857 }
1858
1859 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001860 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001861
Mike Traviscef30b32009-01-16 15:58:13 -08001862 if (version != apic_version[boot_cpu_physical_apicid])
1863 WARN_ONCE(1,
1864 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1865 apic_version[boot_cpu_physical_apicid], cpu, version);
1866
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001867 physid_set(apicid, phys_cpu_present_map);
1868 if (apicid == boot_cpu_physical_apicid) {
1869 /*
1870 * x86_bios_cpu_apicid is required to have processors listed
1871 * in same order as logical cpu numbers. Hence the first
1872 * entry is BSP, and so on.
1873 */
1874 cpu = 0;
1875 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001876 if (apicid > max_physical_apicid)
1877 max_physical_apicid = apicid;
1878
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001879#ifdef CONFIG_X86_32
1880 /*
1881 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1882 * but we need to work other dependencies like SMP_SUSPEND etc
1883 * before this can be done without some confusion.
1884 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1885 * - Ashok Raj <ashok.raj@intel.com>
1886 */
1887 if (max_physical_apicid >= 8) {
1888 switch (boot_cpu_data.x86_vendor) {
1889 case X86_VENDOR_INTEL:
1890 if (!APIC_XAPIC(version)) {
1891 def_to_bigsmp = 0;
1892 break;
1893 }
1894 /* If P4 and above fall through */
1895 case X86_VENDOR_AMD:
1896 def_to_bigsmp = 1;
1897 }
1898 }
1899#endif
1900
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001901#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd472009-01-13 20:41:34 +09001902 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1903 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001904#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001905
Mike Travis1de88cd2008-12-16 17:34:02 -08001906 set_cpu_possible(cpu, true);
1907 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001908}
1909
Suresh Siddha0c81c742008-07-10 11:16:48 -07001910int hard_smp_processor_id(void)
1911{
1912 return read_apic_id();
1913}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001914
1915void default_init_apic_ldr(void)
1916{
1917 unsigned long val;
1918
1919 apic_write(APIC_DFR, APIC_DFR_VALUE);
1920 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1921 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1922 apic_write(APIC_LDR, val);
1923}
1924
1925#ifdef CONFIG_X86_32
1926int default_apicid_to_node(int logical_apicid)
1927{
1928#ifdef CONFIG_SMP
1929 return apicid_2_node[hard_smp_processor_id()];
1930#else
1931 return 0;
1932#endif
1933}
Yinghai Lu34919982008-08-24 02:01:48 -07001934#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07001935
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001936/*
1937 * Power management
1938 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939#ifdef CONFIG_PM
1940
1941static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001942 /*
1943 * 'active' is true if the local APIC was enabled by us and
1944 * not the BIOS; this signifies that we are also responsible
1945 * for disabling it before entering apm/acpi suspend
1946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 int active;
1948 /* r/w apic fields */
1949 unsigned int apic_id;
1950 unsigned int apic_taskpri;
1951 unsigned int apic_ldr;
1952 unsigned int apic_dfr;
1953 unsigned int apic_spiv;
1954 unsigned int apic_lvtt;
1955 unsigned int apic_lvtpc;
1956 unsigned int apic_lvt0;
1957 unsigned int apic_lvt1;
1958 unsigned int apic_lvterr;
1959 unsigned int apic_tmict;
1960 unsigned int apic_tdcr;
1961 unsigned int apic_thmr;
1962} apic_pm_state;
1963
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001964static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001967 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 if (!apic_pm_state.active)
1970 return 0;
1971
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001972 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001973
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001974 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1976 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1977 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1978 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1979 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001980 if (maxlvt >= 4)
1981 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1983 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1984 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1985 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1986 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001987#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001988 if (maxlvt >= 5)
1989 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1990#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001991
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001992 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 disable_local_APIC();
1994 local_irq_restore(flags);
1995 return 0;
1996}
1997
1998static int lapic_resume(struct sys_device *dev)
1999{
2000 unsigned int l, h;
2001 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002002 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 if (!apic_pm_state.active)
2005 return 0;
2006
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002007 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002010
Yinghai Lu49899ea2008-08-24 02:01:47 -07002011#ifdef HAVE_X2APIC
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002012 if (x2apic)
2013 enable_x2apic();
2014 else
2015#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07002016 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002017 /*
2018 * Make sure the APICBASE points to the right address
2019 *
2020 * FIXME! This will be wrong if we ever support suspend on
2021 * SMP! We'll need to do this as part of the CPU restore!
2022 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002023 rdmsr(MSR_IA32_APICBASE, l, h);
2024 l &= ~MSR_IA32_APICBASE_BASE;
2025 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2026 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002027 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2030 apic_write(APIC_ID, apic_pm_state.apic_id);
2031 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2032 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2033 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2034 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2035 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2036 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002037#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002038 if (maxlvt >= 5)
2039 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2040#endif
2041 if (maxlvt >= 4)
2042 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2044 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2045 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2046 apic_write(APIC_ESR, 0);
2047 apic_read(APIC_ESR);
2048 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2049 apic_write(APIC_ESR, 0);
2050 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 0;
2055}
2056
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002057/*
2058 * This device has no shutdown method - fully functioning local APICs
2059 * are needed on every CPU up until machine_halt/restart/poweroff.
2060 */
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01002063 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 .resume = lapic_resume,
2065 .suspend = lapic_suspend,
2066};
2067
2068static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002069 .id = 0,
2070 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071};
2072
Ashok Raje6982c62005-06-25 14:54:58 -07002073static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 apic_pm_state.active = 1;
2076}
2077
2078static int __init init_lapic_sysfs(void)
2079{
2080 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (!cpu_has_apic)
2083 return 0;
2084 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 error = sysdev_class_register(&lapic_sysclass);
2087 if (!error)
2088 error = sysdev_register(&device_lapic);
2089 return error;
2090}
2091device_initcall(init_lapic_sysfs);
2092
2093#else /* CONFIG_PM */
2094
2095static void apic_pm_activate(void) { }
2096
2097#endif /* CONFIG_PM */
2098
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002099#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002101 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 *
2103 * Thus far, the major user of this is IBM's Summit2 series:
2104 *
Linus Torvalds637029c2006-02-27 20:41:56 -08002105 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * multi-chassis. Use available data to take a good guess.
2107 * If in doubt, go HPET.
2108 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002109__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 int i, clusters, zeros;
2112 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002113 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2115
Yinghai Lu322850a2008-02-23 21:48:42 -08002116 /*
2117 * there is not this kind of box with AMD CPU yet.
2118 * Some AMD box with quadcore cpu and 8 sockets apicid
2119 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08002120 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08002121 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002122 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08002123 return 0;
2124
Mike Travis23ca4bb2008-05-12 21:21:12 +02002125 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec33f2005-05-16 21:53:32 -07002126 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Mike Travis168ef542008-12-16 17:34:01 -08002128 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002129 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002130 if (bios_cpu_apicid) {
2131 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302132 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002133 if (cpu_present(i))
2134 id = per_cpu(x86_bios_cpu_apicid, i);
2135 else
2136 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302137 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002138 break;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (id != BAD_APICID)
2141 __set_bit(APIC_CLUSTERID(id), clustermap);
2142 }
2143
2144 /* Problem: Partially populated chassis may not have CPUs in some of
2145 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002146 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2147 * Since clusters are allocated sequentially, count zeros only if
2148 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
2150 clusters = 0;
2151 zeros = 0;
2152 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2153 if (test_bit(i, clustermap)) {
2154 clusters += 1 + zeros;
2155 zeros = 0;
2156 } else
2157 ++zeros;
2158 }
2159
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002160 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2161 * not guaranteed to be synced between boards
2162 */
2163 if (is_vsmp_box() && clusters > 1)
2164 return 1;
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002167 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 * May have to revisit this when multi-core + hyperthreaded CPUs come
2169 * out, but AFAIK this will work even for them.
2170 */
2171 return (clusters > 2);
2172}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002176 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002178static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002181 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002182 return 0;
2183}
2184early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002186/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002187static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002188{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002189 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002190}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002191early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002193static int __init parse_lapic_timer_c2_ok(char *arg)
2194{
2195 local_apic_timer_c2_ok = 1;
2196 return 0;
2197}
2198early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2199
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002200static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002203 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002204}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002205early_param("noapictimer", parse_disable_apic_timer);
2206
2207static int __init parse_nolapic_timer(char *arg)
2208{
2209 disable_apic_timer = 1;
2210 return 0;
2211}
2212early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002213
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002214static int __init apic_set_verbosity(char *arg)
2215{
2216 if (!arg) {
2217#ifdef CONFIG_X86_64
2218 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002219 return 0;
2220#endif
2221 return -EINVAL;
2222 }
2223
2224 if (strcmp("debug", arg) == 0)
2225 apic_verbosity = APIC_DEBUG;
2226 else if (strcmp("verbose", arg) == 0)
2227 apic_verbosity = APIC_VERBOSE;
2228 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002229 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002230 " use apic=verbose or apic=debug\n", arg);
2231 return -EINVAL;
2232 }
2233
2234 return 0;
2235}
2236early_param("apic", apic_set_verbosity);
2237
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002238static int __init lapic_insert_resource(void)
2239{
2240 if (!apic_phys)
2241 return -1;
2242
2243 /* Put local APIC into the resource map. */
2244 lapic_resource.start = apic_phys;
2245 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2246 insert_resource(&iomem_resource, &lapic_resource);
2247
2248 return 0;
2249}
2250
2251/*
2252 * need call insert after e820_reserve_resources()
2253 * that is using request_resource
2254 */
2255late_initcall(lapic_insert_resource);