blob: ffbf7c21bbc66d897a0da3d4b0bfb09de49eb270 [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
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel_stat.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010019#include <linux/mc146818rtc.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010020#include <linux/acpi_pmtmr.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010021#include <linux/clockchips.h>
22#include <linux/interrupt.h>
23#include <linux/bootmem.h>
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +010024#include <linux/ftrace.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010025#include <linux/ioport.h>
26#include <linux/module.h>
27#include <linux/sysdev.h>
28#include <linux/delay.h>
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +053029#include <linux/timex.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010030#include <linux/dmar.h>
31#include <linux/init.h>
32#include <linux/cpu.h>
33#include <linux/dmi.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010034#include <linux/smp.h>
35#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ingo Molnarcdd6c482009-09-21 12:02:48 +020037#include <asm/perf_event.h>
Thomas Gleixner736deca2009-08-19 12:35:53 +020038#include <asm/x86_init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/pgalloc.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010040#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>
Henrik Kretzschmar7167d082011-02-22 15:38:05 +010046#include <asm/io_apic.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010047#include <asm/desc.h>
48#include <asm/hpet.h>
49#include <asm/idle.h>
50#include <asm/mtrr.h>
Jaswinder Singh Rajput2bc13792009-01-11 20:34:47 +053051#include <asm/smp.h>
Andi Kleenbe71b852009-02-12 13:49:38 +010052#include <asm/mce.h>
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -070053#include <asm/tsc.h>
Sheng Yang2904ed82010-12-21 14:18:48 +080054#include <asm/hypervisor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Brian Gerstec70de82009-01-27 12:56:47 +090056unsigned int num_processors;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010057
Brian Gerstec70de82009-01-27 12:56:47 +090058unsigned disabled_cpus __cpuinitdata;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010059
Brian Gerstec70de82009-01-27 12:56:47 +090060/* Processor that is doing the boot up */
61unsigned int boot_cpu_physical_apicid = -1U;
Glauber Costa5af55732008-03-25 13:28:56 -030062
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070063/*
Ingo Molnarfdbecd92009-01-31 03:57:12 +010064 * The highest APIC ID seen during enumeration.
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070065 */
Brian Gerstec70de82009-01-27 12:56:47 +090066unsigned int max_physical_apicid;
67
Ingo Molnarfdbecd92009-01-31 03:57:12 +010068/*
69 * Bitmask of physically existing CPUs:
70 */
Brian Gerstec70de82009-01-27 12:56:47 +090071physid_mask_t phys_cpu_present_map;
72
73/*
74 * Map cpu index to physical APIC ID
75 */
76DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
77DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
78EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
79EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070080
Yinghai Lub3c51172008-08-24 02:01:46 -070081#ifdef CONFIG_X86_32
82/*
83 * Knob to control our willingness to enable the local APIC.
84 *
85 * +1=force-enable
86 */
Henrik Kretzschmar25874a22011-03-11 08:02:36 +010087static int force_enable_local_apic __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -070088/*
89 * APIC command line parameters
90 */
91static int __init parse_lapic(char *arg)
92{
93 force_enable_local_apic = 1;
94 return 0;
95}
96early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -070097/* Local APIC was disabled by the BIOS and enabled by the kernel */
98static int enabled_via_apicbase;
99
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +0400100/*
101 * Handle interrupt mode configuration register (IMCR).
102 * This register controls whether the interrupt signals
103 * that reach the BSP come from the master PIC or from the
104 * local APIC. Before entering Symmetric I/O Mode, either
105 * the BIOS or the operating system must switch out of
106 * PIC Mode by changing the IMCR.
107 */
Alexander van Heukelum5cda3952009-04-13 17:39:24 +0200108static inline void imcr_pic_to_apic(void)
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +0400109{
110 /* select IMCR register */
111 outb(0x70, 0x22);
112 /* NMI and 8259 INTR go through APIC */
113 outb(0x01, 0x23);
114}
115
Alexander van Heukelum5cda3952009-04-13 17:39:24 +0200116static inline void imcr_apic_to_pic(void)
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +0400117{
118 /* select IMCR register */
119 outb(0x70, 0x22);
120 /* NMI and 8259 INTR go directly to BSP */
121 outb(0x00, 0x23);
122}
Yinghai Lub3c51172008-08-24 02:01:46 -0700123#endif
124
125#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200126static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -0700127static __init int setup_apicpmtimer(char *s)
128{
129 apic_calibrate_pmtmr = 1;
130 notsc_setup(NULL);
131 return 0;
132}
133__setup("apicpmtimer", setup_apicpmtimer);
134#endif
135
Suresh Siddhafc1edaf2009-04-20 13:02:27 -0700136int x2apic_mode;
Yinghai Lu06cd9a72009-02-16 17:29:58 -0800137#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700138/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530139static int x2apic_preenabled;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700140static __init int setup_nox2apic(char *str)
141{
Suresh Siddha39d83a52009-04-20 13:02:29 -0700142 if (x2apic_enabled()) {
143 pr_warning("Bios already enabled x2apic, "
144 "can't enforce nox2apic");
145 return 0;
146 }
147
Yinghai Lu49899ea2008-08-24 02:01:47 -0700148 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
149 return 0;
150}
151early_param("nox2apic", setup_nox2apic);
152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Yinghai Lub3c51172008-08-24 02:01:46 -0700154unsigned long mp_lapic_addr;
155int disable_apic;
156/* Disable local APIC timer from the kernel commandline or via dmi quirk */
Henrik Kretzschmar25874a22011-03-11 08:02:36 +0100157static int disable_apic_timer __initdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100158/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700159int local_apic_timer_c2_ok;
160EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
161
Yinghai Luefa25592008-08-19 20:50:36 -0700162int first_system_vector = 0xfe;
163
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100164/*
165 * Debug level, exported for io_apic.c
166 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100167unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100168
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700169int pic_mode;
170
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400171/* Have we found an MP table */
172int smp_found_config;
173
Aaron Durbin39928722006-12-07 02:14:01 +0100174static struct resource lapic_resource = {
175 .name = "Local APIC",
176 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
177};
178
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200179static unsigned int calibration_result;
180
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100181static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200182
Andi Kleend3432892008-01-30 13:33:17 +0100183static unsigned long apic_phys;
184
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100185/*
186 * Get the LAPIC version
187 */
188static inline int lapic_get_version(void)
189{
190 return GET_APIC_VERSION(apic_read(APIC_LVR));
191}
192
193/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400194 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100195 */
196static inline int lapic_is_integrated(void)
197{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400198#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100199 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400200#else
201 return APIC_INTEGRATED(lapic_get_version());
202#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100203}
204
205/*
206 * Check, whether this is a modern or a first generation APIC
207 */
208static int modern_apic(void)
209{
210 /* AMD systems use old APIC versions, so check the CPU */
211 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
212 boot_cpu_data.x86 >= 0xf)
213 return 1;
214 return lapic_get_version() >= 0x14;
215}
216
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400217/*
Cyrill Gorcunova933c612009-10-14 00:07:04 +0400218 * right after this call apic become NOOP driven
219 * so apic->write/read doesn't do anything
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400220 */
Henrik Kretzschmar25874a22011-03-11 08:02:36 +0100221static void __init apic_disable(void)
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400222{
Cyrill Gorcunovf88f2b42009-10-15 19:04:16 +0400223 pr_info("APIC: switched to apic NOOP\n");
Cyrill Gorcunova933c612009-10-14 00:07:04 +0400224 apic = &apic_noop;
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400225}
226
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800227void native_apic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100228{
229 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
230 cpu_relax();
231}
232
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800233u32 native_safe_apic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100234{
235 u32 send_status;
236 int timeout;
237
238 timeout = 0;
239 do {
240 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
241 if (!send_status)
242 break;
243 udelay(100);
244 } while (timeout++ < 1000);
245
246 return send_status;
247}
248
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800249void native_apic_icr_write(u32 low, u32 id)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700250{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200251 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700252 apic_write(APIC_ICR, low);
253}
254
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800255u64 native_apic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700256{
257 u32 icr1, icr2;
258
259 icr2 = apic_read(APIC_ICR2);
260 icr1 = apic_read(APIC_ICR);
261
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400262 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700263}
264
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700265#ifdef CONFIG_X86_32
266/**
267 * get_physical_broadcast - Get number of physical broadcast IDs
268 */
269int get_physical_broadcast(void)
270{
271 return modern_apic() ? 0xff : 0xf;
272}
273#endif
274
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100275/**
276 * lapic_get_maxlvt - get the maximum number of local vector table entries
277 */
278int lapic_get_maxlvt(void)
279{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200280 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100281
282 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200283 /*
284 * - we always have APIC integrated on 64bit mode
285 * - 82489DXs do not report # of LVT entries
286 */
287 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100288}
289
290/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400291 * Local APIC timer
292 */
293
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400294/* Clock divisor */
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400295#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200296
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100297/*
298 * This function sets up the local APIC timer, with a timeout of
299 * 'clocks' APIC bus clock. During calibration we actually call
300 * this function twice on the boot CPU, once with a bogus timeout
301 * value, second time for real. The other (noncalibrating) CPUs
302 * call this function only once, with the real, calibrated value.
303 *
304 * We do reads before writes even if unnecessary, to get around the
305 * P5 APIC double write bug.
306 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100307static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
308{
309 unsigned int lvtt_value, tmp_value;
310
311 lvtt_value = LOCAL_TIMER_VECTOR;
312 if (!oneshot)
313 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200314 if (!lapic_is_integrated())
315 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
316
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100317 if (!irqen)
318 lvtt_value |= APIC_LVT_MASKED;
319
320 apic_write(APIC_LVTT, lvtt_value);
321
322 /*
323 * Divide PICLK by 16
324 */
325 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400326 apic_write(APIC_TDCR,
327 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
328 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100329
330 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200331 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100332}
333
334/*
Robert Richtera68c4392010-10-06 12:27:53 +0200335 * Setup extended LVT, AMD specific
Robert Richter7b83dae2008-01-30 13:30:40 +0100336 *
Robert Richtera68c4392010-10-06 12:27:53 +0200337 * Software should use the LVT offsets the BIOS provides. The offsets
338 * are determined by the subsystems using it like those for MCE
339 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
340 * are supported. Beginning with family 10h at least 4 offsets are
341 * available.
Robert Richter286f5712008-07-22 21:08:46 +0200342 *
Robert Richtera68c4392010-10-06 12:27:53 +0200343 * Since the offsets must be consistent for all cores, we keep track
344 * of the LVT offsets in software and reserve the offset for the same
345 * vector also to be used on other cores. An offset is freed by
346 * setting the entry to APIC_EILVT_MASKED.
347 *
348 * If the BIOS is right, there should be no conflicts. Otherwise a
349 * "[Firmware Bug]: ..." error message is generated. However, if
350 * software does not properly determines the offsets, it is not
351 * necessarily a BIOS bug.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100352 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100353
Robert Richtera68c4392010-10-06 12:27:53 +0200354static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100355
Robert Richtera68c4392010-10-06 12:27:53 +0200356static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
357{
358 return (old & APIC_EILVT_MASKED)
359 || (new == APIC_EILVT_MASKED)
360 || ((new & ~APIC_EILVT_MASKED) == old);
361}
362
363static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
364{
365 unsigned int rsvd; /* 0: uninitialized */
366
367 if (offset >= APIC_EILVT_NR_MAX)
368 return ~0;
369
370 rsvd = atomic_read(&eilvt_offsets[offset]) & ~APIC_EILVT_MASKED;
371 do {
372 if (rsvd &&
373 !eilvt_entry_is_changeable(rsvd, new))
374 /* may not change if vectors are different */
375 return rsvd;
376 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
377 } while (rsvd != new);
378
379 return new;
380}
381
382/*
383 * If mask=1, the LVT entry does not generate interrupts while mask=0
384 * enables the vector. See also the BKDGs.
385 */
386
Robert Richter27afdf22010-10-06 12:27:54 +0200387int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
Robert Richtera68c4392010-10-06 12:27:53 +0200388{
389 unsigned long reg = APIC_EILVTn(offset);
390 unsigned int new, old, reserved;
391
392 new = (mask << 16) | (msg_type << 8) | vector;
393 old = apic_read(reg);
394 reserved = reserve_eilvt_offset(offset, new);
395
396 if (reserved != new) {
Robert Richtereb48c9c2010-10-25 16:03:39 +0200397 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
398 "vector 0x%x, but the register is already in use for "
399 "vector 0x%x on another cpu\n",
400 smp_processor_id(), reg, offset, new, reserved);
Robert Richtera68c4392010-10-06 12:27:53 +0200401 return -EINVAL;
402 }
403
404 if (!eilvt_entry_is_changeable(old, new)) {
Robert Richtereb48c9c2010-10-25 16:03:39 +0200405 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
406 "vector 0x%x, but the register is already in use for "
407 "vector 0x%x on this cpu\n",
408 smp_processor_id(), reg, offset, new, old);
Robert Richtera68c4392010-10-06 12:27:53 +0200409 return -EBUSY;
410 }
411
412 apic_write(reg, new);
413
414 return 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100415}
Robert Richter27afdf22010-10-06 12:27:54 +0200416EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
Robert Richter7b83dae2008-01-30 13:30:40 +0100417
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100418/*
419 * Program the next event, relative to now
420 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200421static int lapic_next_event(unsigned long delta,
422 struct clock_event_device *evt)
423{
424 apic_write(APIC_TMICT, delta);
425 return 0;
426}
427
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100428/*
429 * Setup the lapic timer in periodic or oneshot mode
430 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200431static void lapic_timer_setup(enum clock_event_mode mode,
432 struct clock_event_device *evt)
433{
434 unsigned long flags;
435 unsigned int v;
436
437 /* Lapic used as dummy for broadcast ? */
438 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
439 return;
440
441 local_irq_save(flags);
442
443 switch (mode) {
444 case CLOCK_EVT_MODE_PERIODIC:
445 case CLOCK_EVT_MODE_ONESHOT:
446 __setup_APIC_LVTT(calibration_result,
447 mode != CLOCK_EVT_MODE_PERIODIC, 1);
448 break;
449 case CLOCK_EVT_MODE_UNUSED:
450 case CLOCK_EVT_MODE_SHUTDOWN:
451 v = apic_read(APIC_LVTT);
452 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
453 apic_write(APIC_LVTT, v);
Andreas Herrmann6f9b4102009-10-27 11:01:38 +0100454 apic_write(APIC_TMICT, 0);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200455 break;
456 case CLOCK_EVT_MODE_RESUME:
457 /* Nothing to do here */
458 break;
459 }
460
461 local_irq_restore(flags);
462}
463
464/*
465 * Local APIC timer broadcast function
466 */
Mike Travis96289372008-12-31 18:08:46 -0800467static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200468{
469#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100470 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200471#endif
472}
473
Henrik Kretzschmar25874a22011-03-11 08:02:36 +0100474
475/*
476 * The local apic timer can be used for any function which is CPU local.
477 */
478static struct clock_event_device lapic_clockevent = {
479 .name = "lapic",
480 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
481 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
482 .shift = 32,
483 .set_mode = lapic_timer_setup,
484 .set_next_event = lapic_next_event,
485 .broadcast = lapic_timer_broadcast,
486 .rating = 100,
487 .irq = -1,
488};
489static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
490
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100491/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200492 * Setup the local APIC timer for this CPU. Copy the initialized values
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100493 * of the boot CPU and register the clock event in the framework.
494 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700495static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200496{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100497 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
498
Tejun Heo7b543a52010-12-18 16:30:05 +0100499 if (cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_ARAT)) {
Venkatesh Pallipadidb954b52009-04-06 18:51:29 -0700500 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
501 /* Make LAPIC timer preferrable over percpu HPET */
502 lapic_clockevent.rating = 150;
503 }
504
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100505 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030506 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100507
508 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200509}
510
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700511/*
512 * In this functions we calibrate APIC bus clocks to the external timer.
513 *
514 * We want to do the calibration only once since we want to have local timer
515 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
516 * frequency.
517 *
518 * This was previously done by reading the PIT/HPET and waiting for a wrap
519 * around to find out, that a tick has elapsed. I have a box, where the PIT
520 * readout is broken, so it never gets out of the wait loop again. This was
521 * also reported by others.
522 *
523 * Monitoring the jiffies value is inaccurate and the clockevents
524 * infrastructure allows us to do a simple substitution of the interrupt
525 * handler.
526 *
527 * The calibration routine also uses the pm_timer when possible, as the PIT
528 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
529 * back to normal later in the boot process).
530 */
531
532#define LAPIC_CAL_LOOPS (HZ/10)
533
534static __initdata int lapic_cal_loops = -1;
535static __initdata long lapic_cal_t1, lapic_cal_t2;
536static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
537static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
538static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
539
540/*
541 * Temporary interrupt handler.
542 */
543static void __init lapic_cal_handler(struct clock_event_device *dev)
544{
545 unsigned long long tsc = 0;
546 long tapic = apic_read(APIC_TMCCT);
547 unsigned long pm = acpi_pm_read_early();
548
549 if (cpu_has_tsc)
550 rdtscll(tsc);
551
552 switch (lapic_cal_loops++) {
553 case 0:
554 lapic_cal_t1 = tapic;
555 lapic_cal_tsc1 = tsc;
556 lapic_cal_pm1 = pm;
557 lapic_cal_j1 = jiffies;
558 break;
559
560 case LAPIC_CAL_LOOPS:
561 lapic_cal_t2 = tapic;
562 lapic_cal_tsc2 = tsc;
563 if (pm < lapic_cal_pm1)
564 pm += ACPI_PM_OVRRUN;
565 lapic_cal_pm2 = pm;
566 lapic_cal_j2 = jiffies;
567 break;
568 }
569}
570
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900571static int __init
572calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400573{
574 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
575 const long pm_thresh = pm_100ms / 100;
576 unsigned long mult;
577 u64 res;
578
579#ifndef CONFIG_X86_PM_TIMER
580 return -1;
581#endif
582
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900583 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400584
585 /* Check, if the PM timer is available */
586 if (!deltapm)
587 return -1;
588
589 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
590
591 if (deltapm > (pm_100ms - pm_thresh) &&
592 deltapm < (pm_100ms + pm_thresh)) {
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900593 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900594 return 0;
595 }
596
597 res = (((u64)deltapm) * mult) >> 22;
598 do_div(res, 1000000);
599 pr_warning("APIC calibration not consistent "
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900600 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900601
602 /* Correct the lapic counter value */
603 res = (((u64)(*delta)) * pm_100ms);
604 do_div(res, deltapm);
605 pr_info("APIC delta adjusted to PM-Timer: "
606 "%lu (%ld)\n", (unsigned long)res, *delta);
607 *delta = (long)res;
608
609 /* Correct the tsc counter value */
610 if (cpu_has_tsc) {
611 res = (((u64)(*deltatsc)) * pm_100ms);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400612 do_div(res, deltapm);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900613 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
Frans Pop3235dc32010-02-06 18:47:17 +0100614 "PM-Timer: %lu (%ld)\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900615 (unsigned long)res, *deltatsc);
616 *deltatsc = (long)res;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400617 }
618
619 return 0;
620}
621
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700622static int __init calibrate_APIC_clock(void)
623{
624 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700625 void (*real_handler)(struct clock_event_device *dev);
626 unsigned long deltaj;
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900627 long delta, deltatsc;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700628 int pm_referenced = 0;
629
630 local_irq_disable();
631
632 /* Replace the global interrupt handler */
633 real_handler = global_clock_event->event_handler;
634 global_clock_event->event_handler = lapic_cal_handler;
635
636 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400637 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700638 * can underflow in the 100ms detection time frame
639 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400640 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700641
642 /* Let the interrupts run */
643 local_irq_enable();
644
645 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
646 cpu_relax();
647
648 local_irq_disable();
649
650 /* Restore the real event handler */
651 global_clock_event->event_handler = real_handler;
652
653 /* Build delta t1-t2 as apic timer counts down */
654 delta = lapic_cal_t1 - lapic_cal_t2;
655 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
656
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900657 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
658
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400659 /* we trust the PM based calibration if possible */
660 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900661 &delta, &deltatsc);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700662
663 /* Calculate the scaled math multiplication factor */
664 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
665 lapic_clockevent.shift);
666 lapic_clockevent.max_delta_ns =
Pierre Tardy4aed89d2011-01-06 16:23:29 +0100667 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700668 lapic_clockevent.min_delta_ns =
669 clockevent_delta2ns(0xF, &lapic_clockevent);
670
671 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
672
673 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
Thomas Gleixner411462f2009-11-16 11:52:39 +0100674 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700675 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
676 calibration_result);
677
678 if (cpu_has_tsc) {
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700679 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
680 "%ld.%04ld MHz.\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900681 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
682 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700683 }
684
685 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
686 "%u.%04u MHz.\n",
687 calibration_result / (1000000 / HZ),
688 calibration_result % (1000000 / HZ));
689
690 /*
691 * Do a sanity check on the APIC calibration result
692 */
693 if (calibration_result < (1000000 / HZ)) {
694 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100695 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700696 return -1;
697 }
698
699 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
700
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400701 /*
702 * PM timer calibration failed or not turned on
703 * so lets try APIC timer based calibration
704 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700705 if (!pm_referenced) {
706 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
707
708 /*
709 * Setup the apic timer manually
710 */
711 levt->event_handler = lapic_cal_handler;
712 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
713 lapic_cal_loops = -1;
714
715 /* Let the interrupts run */
716 local_irq_enable();
717
718 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
719 cpu_relax();
720
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700721 /* Stop the lapic timer */
722 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
723
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700724 /* Jiffies delta */
725 deltaj = lapic_cal_j2 - lapic_cal_j1;
726 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
727
728 /* Check, if the jiffies result is consistent */
729 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
730 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
731 else
732 levt->features |= CLOCK_EVT_FEAT_DUMMY;
733 } else
734 local_irq_enable();
735
736 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530737 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700738 return -1;
739 }
740
741 return 0;
742}
743
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100744/*
745 * Setup the boot APIC
746 *
747 * Calibrate and verify the result.
748 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100749void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100751 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400752 * The local apic timer can be disabled via the kernel
753 * commandline or from the CPU detection code. Register the lapic
754 * timer as a dummy clock event source on SMP systems, so the
755 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100756 */
757 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100758 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100759 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100760 if (num_possible_cpus() > 1) {
761 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100762 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100763 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100764 return;
765 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200766
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400767 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
768 "calibrating APIC timer ...\n");
769
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400770 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100771 /* No broadcast on UP ! */
772 if (num_possible_cpus() > 1)
773 setup_APIC_timer();
774 return;
775 }
776
777 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100778 * If nmi_watchdog is set to IO_APIC, we need the
779 * PIT/HPET going. Otherwise register lapic as a dummy
780 * device.
781 */
Don Zickus072b1982010-11-12 11:22:24 -0500782 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100783
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400784 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100785 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100788void __cpuinit setup_secondary_APIC_clock(void)
789{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100790 setup_APIC_timer();
791}
792
793/*
794 * The guts of the apic timer interrupt
795 */
796static void local_apic_timer_interrupt(void)
797{
798 int cpu = smp_processor_id();
799 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
800
801 /*
802 * Normally we should not be here till LAPIC has been initialized but
803 * in some cases like kdump, its possible that there is a pending LAPIC
804 * timer interrupt from previous kernel's context and is delivered in
805 * new kernel the moment interrupts are enabled.
806 *
807 * Interrupts are enabled early and LAPIC is setup much later, hence
808 * its possible that when we get here evt->event_handler is NULL.
809 * Check for event_handler being NULL and discard the interrupt as
810 * spurious.
811 */
812 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100813 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100814 /* Switch it off */
815 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
816 return;
817 }
818
819 /*
820 * the NMI deadlock-detector uses this.
821 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800822 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100823
824 evt->event_handler(evt);
825}
826
827/*
828 * Local APIC timer interrupt. This is the most natural way for doing
829 * local interrupts, but local timer interrupts can be emulated by
830 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
831 *
832 * [ if a single-CPU system runs an SMP kernel then we call the local
833 * interrupt as well. Thus we cannot inline the local irq ... ]
834 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100835void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100836{
837 struct pt_regs *old_regs = set_irq_regs(regs);
838
839 /*
840 * NOTE! We'd better ACK the irq immediately,
841 * because timer handling can be slow.
842 */
843 ack_APIC_irq();
844 /*
845 * update_process_times() expects us to have done irq_enter().
846 * Besides, if we don't timer interrupts ignore the global
847 * interrupt lock, which is the WrongThing (tm) to do.
848 */
849 exit_idle();
850 irq_enter();
851 local_apic_timer_interrupt();
852 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400853
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100854 set_irq_regs(old_regs);
855}
856
857int setup_profiling_timer(unsigned int multiplier)
858{
859 return -EINVAL;
860}
861
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100862/*
863 * Local APIC start and shutdown
864 */
865
866/**
867 * clear_local_APIC - shutdown the local APIC
868 *
869 * This is called, when a CPU is disabled and before rebooting, so the state of
870 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
871 * leftovers during boot.
872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873void clear_local_APIC(void)
874{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400875 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100876 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Andi Kleend3432892008-01-30 13:33:17 +0100878 /* APIC hasn't been mapped yet */
Suresh Siddhafc1edaf2009-04-20 13:02:27 -0700879 if (!x2apic_mode && !apic_phys)
Andi Kleend3432892008-01-30 13:33:17 +0100880 return;
881
882 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200884 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * if the vector is zero. Mask LVTERR first to prevent this.
886 */
887 if (maxlvt >= 3) {
888 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100889 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 /*
892 * Careful: we have to set masks only first to deassert
893 * any level-triggered sources.
894 */
895 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100896 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100898 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100900 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (maxlvt >= 4) {
902 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100903 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400906 /* lets not touch this if we didn't frob it */
Andi Kleen4efc0672009-04-28 19:07:31 +0200907#ifdef CONFIG_X86_THERMAL_VECTOR
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400908 if (maxlvt >= 5) {
909 v = apic_read(APIC_LVTTHMR);
910 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
911 }
912#endif
Andi Kleen5ca86812009-02-12 13:49:37 +0100913#ifdef CONFIG_X86_MCE_INTEL
914 if (maxlvt >= 6) {
915 v = apic_read(APIC_LVTCMCI);
916 if (!(v & APIC_LVT_MASKED))
917 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
918 }
919#endif
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /*
922 * Clean APIC state for other OSs:
923 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100924 apic_write(APIC_LVTT, APIC_LVT_MASKED);
925 apic_write(APIC_LVT0, APIC_LVT_MASKED);
926 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100928 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100930 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400931
932 /* Integrated APIC (!82489DX) ? */
933 if (lapic_is_integrated()) {
934 if (maxlvt > 3)
935 /* Clear ESR due to Pentium errata 3AP and 11AP */
936 apic_write(APIC_ESR, 0);
937 apic_read(APIC_ESR);
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100941/**
942 * disable_local_APIC - clear and disable the local APIC
943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944void disable_local_APIC(void)
945{
946 unsigned int value;
947
Jan Beulich4a13ad02009-01-14 12:28:51 +0000948 /* APIC hasn't been mapped yet */
Yinghai Lufd19dce2010-07-15 00:00:59 -0700949 if (!x2apic_mode && !apic_phys)
Jan Beulich4a13ad02009-01-14 12:28:51 +0000950 return;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 clear_local_APIC();
953
954 /*
955 * Disable APIC (implies clearing of registers
956 * for 82489DX!).
957 */
958 value = apic_read(APIC_SPIV);
959 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100960 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400961
962#ifdef CONFIG_X86_32
963 /*
964 * When LAPIC was disabled by the BIOS and enabled by the kernel,
965 * restore the disabled state.
966 */
967 if (enabled_via_apicbase) {
968 unsigned int l, h;
969
970 rdmsr(MSR_IA32_APICBASE, l, h);
971 l &= ~MSR_IA32_APICBASE_ENABLE;
972 wrmsr(MSR_IA32_APICBASE, l, h);
973 }
974#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400977/*
978 * If Linux enabled the LAPIC against the BIOS default disable it down before
979 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
980 * not power-off. Additionally clear all LVT entries before disable_local_APIC
981 * for the case where Linux didn't enable the LAPIC.
982 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700983void lapic_shutdown(void)
984{
985 unsigned long flags;
986
Cyrill Gorcunov83121362009-09-15 11:12:30 +0400987 if (!cpu_has_apic && !apic_from_smp_config())
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700988 return;
989
990 local_irq_save(flags);
991
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400992#ifdef CONFIG_X86_32
993 if (!enabled_via_apicbase)
994 clear_local_APIC();
995 else
996#endif
997 disable_local_APIC();
998
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700999
1000 local_irq_restore(flags);
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
1004 * This is to verify that we're looking at a real local APIC.
1005 * Check these against your board if the CPUs aren't getting
1006 * started for no apparent reason.
1007 */
1008int __init verify_local_APIC(void)
1009{
1010 unsigned int reg0, reg1;
1011
1012 /*
1013 * The version register is read-only in a real APIC.
1014 */
1015 reg0 = apic_read(APIC_LVR);
1016 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1017 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1018 reg1 = apic_read(APIC_LVR);
1019 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1020
1021 /*
1022 * The two version reads above should print the same
1023 * numbers. If the second one is different, then we
1024 * poke at a non-APIC.
1025 */
1026 if (reg1 != reg0)
1027 return 0;
1028
1029 /*
1030 * Check if the version looks reasonably.
1031 */
1032 reg1 = GET_APIC_VERSION(reg0);
1033 if (reg1 == 0x00 || reg1 == 0xff)
1034 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001035 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (reg1 < 0x02 || reg1 == 0xff)
1037 return 0;
1038
1039 /*
1040 * The ID register is read/write in a real APIC.
1041 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001042 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001044 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001045 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1047 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001048 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050
1051 /*
1052 * The next two are just to see if we have sane values.
1053 * They're only really relevant if we're in Virtual Wire
1054 * compatibility mode, but most boxes are anymore.
1055 */
1056 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001057 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 reg1 = apic_read(APIC_LVT1);
1059 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1060
1061 return 1;
1062}
1063
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001064/**
1065 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067void __init sync_Arb_IDs(void)
1068{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001069 /*
1070 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1071 * needed on AMD.
1072 */
1073 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return;
1075
1076 /*
1077 * Wait for idle.
1078 */
1079 apic_wait_icr_idle();
1080
1081 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001082 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1083 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/*
1087 * An initial setup of the virtual wire mode.
1088 */
1089void __init init_bsp_APIC(void)
1090{
Andi Kleen11a8e772006-01-11 22:46:51 +01001091 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /*
1094 * Don't do the setup now if we have a SMP BIOS as the
1095 * through-I/O-APIC virtual wire mode might be active.
1096 */
1097 if (smp_found_config || !cpu_has_apic)
1098 return;
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * Do not trust the local APIC being empty at bootup.
1102 */
1103 clear_local_APIC();
1104
1105 /*
1106 * Enable APIC.
1107 */
1108 value = apic_read(APIC_SPIV);
1109 value &= ~APIC_VECTOR_MASK;
1110 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001111
1112#ifdef CONFIG_X86_32
1113 /* This bit is reserved on P4/Xeon and should be cleared */
1114 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1115 (boot_cpu_data.x86 == 15))
1116 value &= ~APIC_SPIV_FOCUS_DISABLED;
1117 else
1118#endif
1119 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001121 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * Set up the virtual wire mode.
1125 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001126 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001128 if (!lapic_is_integrated()) /* 82489DX */
1129 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001130 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001133static void __cpuinit lapic_setup_esr(void)
1134{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001135 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001136
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001137 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001138 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001139 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001140 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001141
Ingo Molnar08125d32009-01-28 05:08:44 +01001142 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001143 /*
1144 * Something untraceable is creating bad interrupts on
1145 * secondary quads ... for the moment, just leave the
1146 * ESR disabled - we can't do anything useful with the
1147 * errors anyway - mbligh
1148 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001149 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001150 return;
1151 }
1152
1153 maxlvt = lapic_get_maxlvt();
1154 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1155 apic_write(APIC_ESR, 0);
1156 oldvalue = apic_read(APIC_ESR);
1157
1158 /* enables sending errors */
1159 value = ERROR_APIC_VECTOR;
1160 apic_write(APIC_LVTERR, value);
1161
1162 /*
1163 * spec says clear errors after enabling vector.
1164 */
1165 if (maxlvt > 3)
1166 apic_write(APIC_ESR, 0);
1167 value = apic_read(APIC_ESR);
1168 if (value != oldvalue)
1169 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1170 "vector: 0x%08x after: 0x%08x\n",
1171 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001172}
1173
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001174/**
1175 * setup_local_APIC - setup the local APIC
Tejun Heo0aa002f2010-12-09 11:47:21 +01001176 *
1177 * Used to setup local APIC while initializing BSP or bringin up APs.
1178 * Always called with preemption disabled.
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001179 */
1180void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Tejun Heo0aa002f2010-12-09 11:47:21 +01001182 int cpu = smp_processor_id();
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001183 unsigned int value, queued;
1184 int i, j, acked = 0;
1185 unsigned long long tsc = 0, ntsc;
1186 long long max_loops = cpu_khz;
1187
1188 if (cpu_has_tsc)
1189 rdtscll(tsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jan Beulichf1182632009-01-14 12:27:35 +00001191 if (disable_apic) {
Henrik Kretzschmar7167d082011-02-22 15:38:05 +01001192 disable_ioapic_support();
Jan Beulichf1182632009-01-14 12:27:35 +00001193 return;
1194 }
1195
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001196#ifdef CONFIG_X86_32
1197 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001198 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001199 apic_write(APIC_ESR, 0);
1200 apic_write(APIC_ESR, 0);
1201 apic_write(APIC_ESR, 0);
1202 apic_write(APIC_ESR, 0);
1203 }
1204#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 perf_events_lapic_init();
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * Double-check whether this APIC is really registered.
1209 * This is meaningless in clustered apic mode, so we skip it.
1210 */
Daniel Walkerc2777f92009-09-12 10:40:20 -07001211 BUG_ON(!apic->apic_id_registered());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /*
1214 * Intel recommends to set DFR, LDR and TPR before enabling
1215 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1216 * document number 292116). So here it goes...
1217 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001218 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /*
1221 * Set Task Priority to 'accept all'. We never change this
1222 * later on.
1223 */
1224 value = apic_read(APIC_TASKPRI);
1225 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001226 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001229 * After a crash, we no longer service the interrupts and a pending
1230 * interrupt from previous kernel might still have ISR bit set.
1231 *
1232 * Most probably by now CPU has serviced that pending interrupt and
1233 * it might not have done the ack_APIC_irq() because it thought,
1234 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1235 * does not clear the ISR bit and cpu thinks it has already serivced
1236 * the interrupt. Hence a vector might get locked. It was noticed
1237 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1238 */
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001239 do {
1240 queued = 0;
1241 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1242 queued |= apic_read(APIC_IRR + i*0x10);
1243
1244 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1245 value = apic_read(APIC_ISR + i*0x10);
1246 for (j = 31; j >= 0; j--) {
1247 if (value & (1<<j)) {
1248 ack_APIC_irq();
1249 acked++;
1250 }
1251 }
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001252 }
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001253 if (acked > 256) {
1254 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1255 acked);
1256 break;
1257 }
1258 if (cpu_has_tsc) {
1259 rdtscll(ntsc);
1260 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1261 } else
1262 max_loops--;
1263 } while (queued && max_loops > 0);
1264 WARN_ON(max_loops <= 0);
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001265
1266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * Now that we are all set up, enable the APIC
1268 */
1269 value = apic_read(APIC_SPIV);
1270 value &= ~APIC_VECTOR_MASK;
1271 /*
1272 * Enable APIC
1273 */
1274 value |= APIC_SPIV_APIC_ENABLED;
1275
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001276#ifdef CONFIG_X86_32
1277 /*
1278 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1279 * certain networking cards. If high frequency interrupts are
1280 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1281 * entry is masked/unmasked at a high rate as well then sooner or
1282 * later IOAPIC line gets 'stuck', no more interrupts are received
1283 * from the device. If focus CPU is disabled then the hang goes
1284 * away, oh well :-(
1285 *
1286 * [ This bug can be reproduced easily with a level-triggered
1287 * PCI Ne2000 networking cards and PII/PIII processors, dual
1288 * BX chipset. ]
1289 */
1290 /*
1291 * Actually disabling the focus CPU check just makes the hang less
1292 * frequent as it makes the interrupt distributon model be more
1293 * like LRU than MRU (the short-term load is more even across CPUs).
1294 * See also the comment in end_level_ioapic_irq(). --macro
1295 */
1296
1297 /*
1298 * - enable focus processor (bit==0)
1299 * - 64bit mode always use processor focus
1300 * so no need to set it
1301 */
1302 value &= ~APIC_SPIV_FOCUS_DISABLED;
1303#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /*
1306 * Set spurious IRQ vector
1307 */
1308 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001309 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /*
1312 * Set up LVT0, LVT1:
1313 *
1314 * set up through-local-APIC on the BP's LINT0. This is not
1315 * strictly necessary in pure symmetric-IO mode, but sometimes
1316 * we delegate interrupts to the 8259A.
1317 */
1318 /*
1319 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1320 */
1321 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Tejun Heo0aa002f2010-12-09 11:47:21 +01001322 if (!cpu && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 value = APIC_DM_EXTINT;
Tejun Heo0aa002f2010-12-09 11:47:21 +01001324 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else {
1326 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Tejun Heo0aa002f2010-12-09 11:47:21 +01001327 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001329 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /*
1332 * only the BP should see the LINT1 NMI signal, obviously.
1333 */
Tejun Heo0aa002f2010-12-09 11:47:21 +01001334 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 value = APIC_DM_NMI;
1336 else
1337 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001338 if (!lapic_is_integrated()) /* 82489DX */
1339 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001340 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001341
Andi Kleenbe71b852009-02-12 13:49:38 +01001342#ifdef CONFIG_X86_MCE_INTEL
1343 /* Recheck CMCI information after local APIC is up on CPU #0 */
Tejun Heo0aa002f2010-12-09 11:47:21 +01001344 if (!cpu)
Andi Kleenbe71b852009-02-12 13:49:38 +01001345 cmci_recheck();
1346#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Andi Kleen739f33b2008-01-30 13:30:40 +01001349void __cpuinit end_local_APIC_setup(void)
1350{
1351 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001352
1353#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001354 {
1355 unsigned int value;
1356 /* Disable the local apic timer */
1357 value = apic_read(APIC_LVTT);
1358 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1359 apic_write(APIC_LVTT, value);
1360 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001361#endif
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 apic_pm_activate();
Jan Beulich2fb270f2011-02-09 08:21:02 +00001364}
1365
1366void __init bsp_end_local_APIC_setup(void)
1367{
1368 end_local_APIC_setup();
Kenji Kaneshige7f7fbf42010-11-30 22:22:28 -08001369
1370 /*
1371 * Now that local APIC setup is completed for BP, configure the fault
1372 * handling for interrupt remapping.
1373 */
Jan Beulich2fb270f2011-02-09 08:21:02 +00001374 if (intr_remapping_enabled)
Kenji Kaneshige7f7fbf42010-11-30 22:22:28 -08001375 enable_drhd_fault_handling();
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001379#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001380void check_x2apic(void)
1381{
Suresh Siddhaef1f87a2009-02-21 14:23:21 -08001382 if (x2apic_enabled()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001383 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001384 x2apic_preenabled = x2apic_mode = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001385 }
1386}
1387
1388void enable_x2apic(void)
1389{
1390 int msr, msr2;
1391
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001392 if (!x2apic_mode)
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001393 return;
1394
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001395 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1396 if (!(msr & X2APIC_ENABLE)) {
Mike Travis450b1e82009-12-11 08:08:50 -08001397 printk_once(KERN_INFO "Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001398 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1399 }
1400}
Weidong Han93758232009-04-17 16:42:14 +08001401#endif /* CONFIG_X86_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001402
Gleb Natapovce69a782009-07-20 15:24:17 +03001403int __init enable_IR(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001404{
1405#ifdef CONFIG_INTR_REMAP
Weidong Han93758232009-04-17 16:42:14 +08001406 if (!intr_remapping_supported()) {
1407 pr_debug("intr-remapping not supported\n");
Gleb Natapovce69a782009-07-20 15:24:17 +03001408 return 0;
Weidong Han93758232009-04-17 16:42:14 +08001409 }
1410
Weidong Han93758232009-04-17 16:42:14 +08001411 if (!x2apic_preenabled && skip_ioapic_setup) {
1412 pr_info("Skipped enabling intr-remap because of skipping "
1413 "io-apic setup\n");
Gleb Natapovce69a782009-07-20 15:24:17 +03001414 return 0;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001415 }
1416
Gleb Natapovce69a782009-07-20 15:24:17 +03001417 if (enable_intr_remapping(x2apic_supported()))
1418 return 0;
1419
1420 pr_info("Enabled Interrupt-remapping\n");
1421
1422 return 1;
1423
1424#endif
1425 return 0;
1426}
1427
1428void __init enable_IR_x2apic(void)
1429{
1430 unsigned long flags;
Henrik Kretzschmar7d0f1922011-02-22 15:38:06 +01001431 struct IO_APIC_route_entry **ioapic_entries;
Gleb Natapovce69a782009-07-20 15:24:17 +03001432 int ret, x2apic_enabled = 0;
Yinghai Lue6707612009-11-21 00:23:37 -08001433 int dmar_table_init_ret;
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001434
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001435 dmar_table_init_ret = dmar_table_init();
Yinghai Lue6707612009-11-21 00:23:37 -08001436 if (dmar_table_init_ret && !x2apic_supported())
1437 return;
Gleb Natapovce69a782009-07-20 15:24:17 +03001438
Fenghua Yub24696b2009-03-27 14:22:44 -07001439 ioapic_entries = alloc_ioapic_entries();
1440 if (!ioapic_entries) {
Gleb Natapovce69a782009-07-20 15:24:17 +03001441 pr_err("Allocate ioapic_entries failed\n");
1442 goto out;
Fenghua Yub24696b2009-03-27 14:22:44 -07001443 }
1444
1445 ret = save_IO_APIC_setup(ioapic_entries);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001446 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001447 pr_info("Saving IO-APIC state failed: %d\n", ret);
Gleb Natapovce69a782009-07-20 15:24:17 +03001448 goto out;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001449 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001450
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001451 local_irq_save(flags);
Jacob Panb81bb372009-11-09 11:27:04 -08001452 legacy_pic->mask_all();
Gleb Natapovce69a782009-07-20 15:24:17 +03001453 mask_IO_APIC_setup(ioapic_entries);
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001454
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001455 if (dmar_table_init_ret)
1456 ret = 0;
1457 else
1458 ret = enable_IR();
1459
Gleb Natapovce69a782009-07-20 15:24:17 +03001460 if (!ret) {
1461 /* IR is required if there is APIC ID > 255 even when running
1462 * under KVM
1463 */
Sheng Yang2904ed82010-12-21 14:18:48 +08001464 if (max_physical_apicid > 255 ||
1465 !hypervisor_x2apic_available())
Gleb Natapovce69a782009-07-20 15:24:17 +03001466 goto nox2apic;
1467 /*
1468 * without IR all CPUs can be addressed by IOAPIC/MSI
1469 * only in physical mode
1470 */
1471 x2apic_force_phys();
1472 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001473
Gleb Natapovce69a782009-07-20 15:24:17 +03001474 x2apic_enabled = 1;
Weidong Han93758232009-04-17 16:42:14 +08001475
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001476 if (x2apic_supported() && !x2apic_mode) {
1477 x2apic_mode = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001478 enable_x2apic();
Weidong Han93758232009-04-17 16:42:14 +08001479 pr_info("Enabled x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001480 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001481
Gleb Natapovce69a782009-07-20 15:24:17 +03001482nox2apic:
1483 if (!ret) /* IR enabling failed */
Fenghua Yub24696b2009-03-27 14:22:44 -07001484 restore_IO_APIC_setup(ioapic_entries);
Jacob Panb81bb372009-11-09 11:27:04 -08001485 legacy_pic->restore_mask();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001486 local_irq_restore(flags);
1487
Gleb Natapovce69a782009-07-20 15:24:17 +03001488out:
Fenghua Yub24696b2009-03-27 14:22:44 -07001489 if (ioapic_entries)
1490 free_ioapic_entries(ioapic_entries);
Weidong Han93758232009-04-17 16:42:14 +08001491
Gleb Natapovce69a782009-07-20 15:24:17 +03001492 if (x2apic_enabled)
Weidong Han93758232009-04-17 16:42:14 +08001493 return;
1494
Weidong Han93758232009-04-17 16:42:14 +08001495 if (x2apic_preenabled)
Gleb Natapovce69a782009-07-20 15:24:17 +03001496 panic("x2apic: enabled by BIOS but kernel init failed.");
Weidong Han93758232009-04-17 16:42:14 +08001497 else if (cpu_has_x2apic)
Gleb Natapovce69a782009-07-20 15:24:17 +03001498 pr_info("Not enabling x2apic, Intr-remapping init failed.\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001499}
Weidong Han93758232009-04-17 16:42:14 +08001500
Yinghai Lube7a6562008-08-24 02:01:51 -07001501#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001502/*
1503 * Detect and enable local APICs on non-SMP boards.
1504 * Original code written by Keir Fraser.
1505 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1506 * not correctly set up (usually the APIC timer won't work etc.)
1507 */
1508static int __init detect_init_APIC(void)
1509{
1510 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001511 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001512 return -1;
1513 }
1514
1515 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001516 return 0;
1517}
Yinghai Lube7a6562008-08-24 02:01:51 -07001518#else
Thomas Gleixner5a7ae782010-10-19 10:46:28 -07001519
Henrik Kretzschmar25874a22011-03-11 08:02:36 +01001520static int __init apic_verify(void)
Thomas Gleixner5a7ae782010-10-19 10:46:28 -07001521{
1522 u32 features, h, l;
1523
1524 /*
1525 * The APIC feature bit should now be enabled
1526 * in `cpuid'
1527 */
1528 features = cpuid_edx(1);
1529 if (!(features & (1 << X86_FEATURE_APIC))) {
1530 pr_warning("Could not enable APIC!\n");
1531 return -1;
1532 }
1533 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1534 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1535
1536 /* The BIOS may have set up the APIC at some other address */
1537 rdmsr(MSR_IA32_APICBASE, l, h);
1538 if (l & MSR_IA32_APICBASE_ENABLE)
1539 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1540
1541 pr_info("Found and enabled local APIC!\n");
1542 return 0;
1543}
1544
Henrik Kretzschmar25874a22011-03-11 08:02:36 +01001545int __init apic_force_enable(unsigned long addr)
Thomas Gleixner5a7ae782010-10-19 10:46:28 -07001546{
1547 u32 h, l;
1548
1549 if (disable_apic)
1550 return -1;
1551
1552 /*
1553 * Some BIOSes disable the local APIC in the APIC_BASE
1554 * MSR. This can only be done in software for Intel P6 or later
1555 * and AMD K7 (Model > 1) or later.
1556 */
1557 rdmsr(MSR_IA32_APICBASE, l, h);
1558 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1559 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1560 l &= ~MSR_IA32_APICBASE_BASE;
Thomas Gleixnera906fda2011-02-25 16:09:31 +01001561 l |= MSR_IA32_APICBASE_ENABLE | addr;
Thomas Gleixner5a7ae782010-10-19 10:46:28 -07001562 wrmsr(MSR_IA32_APICBASE, l, h);
1563 enabled_via_apicbase = 1;
1564 }
1565 return apic_verify();
1566}
1567
Yinghai Lube7a6562008-08-24 02:01:51 -07001568/*
1569 * Detect and initialize APIC
1570 */
1571static int __init detect_init_APIC(void)
1572{
Yinghai Lube7a6562008-08-24 02:01:51 -07001573 /* Disabled by kernel option? */
1574 if (disable_apic)
1575 return -1;
1576
1577 switch (boot_cpu_data.x86_vendor) {
1578 case X86_VENDOR_AMD:
1579 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
Borislav Petkov85877062009-02-03 16:24:22 +01001580 (boot_cpu_data.x86 >= 15))
Yinghai Lube7a6562008-08-24 02:01:51 -07001581 break;
1582 goto no_apic;
1583 case X86_VENDOR_INTEL:
1584 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1585 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1586 break;
1587 goto no_apic;
1588 default:
1589 goto no_apic;
1590 }
1591
1592 if (!cpu_has_apic) {
1593 /*
1594 * Over-ride BIOS and try to enable the local APIC only if
1595 * "lapic" specified.
1596 */
1597 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001598 pr_info("Local APIC disabled by BIOS -- "
1599 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001600 return -1;
1601 }
Thomas Gleixnera906fda2011-02-25 16:09:31 +01001602 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
Thomas Gleixner5a7ae782010-10-19 10:46:28 -07001603 return -1;
1604 } else {
1605 if (apic_verify())
1606 return -1;
Yinghai Lube7a6562008-08-24 02:01:51 -07001607 }
Yinghai Lube7a6562008-08-24 02:01:51 -07001608
1609 apic_pm_activate();
1610
1611 return 0;
1612
1613no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001614 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001615 return -1;
1616}
1617#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001618
1619/**
1620 * init_apic_mappings - initialize APIC mappings
1621 */
1622void __init init_apic_mappings(void)
1623{
Yinghai Lu4401da62009-05-02 10:40:57 -07001624 unsigned int new_apicid;
1625
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001626 if (x2apic_mode) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001627 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001628 return;
1629 }
1630
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001631 /* If no local APIC can be found return early */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001632 if (!smp_found_config && detect_init_APIC()) {
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001633 /* lets NOP'ify apic operations */
Cyrill Gorcunovcec6be62009-05-11 17:41:40 +04001634 pr_info("APIC: disable apic facility\n");
1635 apic_disable();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001636 } else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001637 apic_phys = mp_lapic_addr;
1638
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001639 /*
1640 * acpi lapic path already maps that address in
1641 * acpi_register_lapic_address()
1642 */
Eric W. Biederman5989cd62010-08-04 13:30:27 -07001643 if (!acpi_lapic && !smp_found_config)
Yinghai Lu326a2e62010-12-07 00:55:38 -08001644 register_lapic_address(apic_phys);
Cyrill Gorcunovcec6be62009-05-11 17:41:40 +04001645 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001646
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001647 /*
1648 * Fetch the APIC ID of the BSP in case we have a
1649 * default configuration (or the MP table is broken).
1650 */
Yinghai Lu4401da62009-05-02 10:40:57 -07001651 new_apicid = read_apic_id();
1652 if (boot_cpu_physical_apicid != new_apicid) {
1653 boot_cpu_physical_apicid = new_apicid;
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001654 /*
1655 * yeah -- we lie about apic_version
1656 * in case if apic was disabled via boot option
1657 * but it's not a problem for SMP compiled kernel
1658 * since smp_sanity_check is prepared for such a case
1659 * and disable smp mode
1660 */
Yinghai Lu4401da62009-05-02 10:40:57 -07001661 apic_version[new_apicid] =
1662 GET_APIC_VERSION(apic_read(APIC_LVR));
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +04001663 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001664}
1665
Yinghai Luc0104d32010-12-07 00:55:17 -08001666void __init register_lapic_address(unsigned long address)
1667{
1668 mp_lapic_addr = address;
1669
Yinghai Lu04501932010-12-07 00:55:56 -08001670 if (!x2apic_mode) {
1671 set_fixmap_nocache(FIX_APIC_BASE, address);
1672 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1673 APIC_BASE, mp_lapic_addr);
1674 }
Yinghai Luc0104d32010-12-07 00:55:17 -08001675 if (boot_cpu_physical_apicid == -1U) {
1676 boot_cpu_physical_apicid = read_apic_id();
1677 apic_version[boot_cpu_physical_apicid] =
1678 GET_APIC_VERSION(apic_read(APIC_LVR));
1679 }
1680}
1681
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001682/*
1683 * This initializes the IO-APIC and APIC hardware if this is
1684 * a UP kernel.
1685 */
Yinghai Lu56d91f12010-12-16 19:09:24 -08001686int apic_version[MAX_LOCAL_APIC];
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001687
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001688int __init APIC_init_uniprocessor(void)
1689{
1690 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001691 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001692 return -1;
1693 }
Jan Beulichf1182632009-01-14 12:27:35 +00001694#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001695 if (!cpu_has_apic) {
1696 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001697 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001698 return -1;
1699 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001700#else
1701 if (!smp_found_config && !cpu_has_apic)
1702 return -1;
1703
1704 /*
1705 * Complain if the BIOS pretends there is one.
1706 */
1707 if (!cpu_has_apic &&
1708 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001709 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1710 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001711 return -1;
1712 }
1713#endif
1714
Ingo Molnar72ce0162009-01-28 06:50:47 +01001715 default_setup_apic_routing();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001716
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001717 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001718 connect_bsp_APIC();
1719
Yinghai Lufa2bd352008-08-24 02:01:50 -07001720#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001721 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001722#else
1723 /*
1724 * Hack: In case of kdump, after a crash, kernel might be booting
1725 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1726 * might be zero if read from MP tables. Get it from LAPIC.
1727 */
1728# ifdef CONFIG_CRASH_DUMP
1729 boot_cpu_physical_apicid = read_apic_id();
1730# endif
1731#endif
1732 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001733 setup_local_APIC();
1734
Yinghai Lu88d0f552009-02-14 23:57:28 -08001735#ifdef CONFIG_X86_IO_APIC
Andi Kleen739f33b2008-01-30 13:30:40 +01001736 /*
1737 * Now enable IO-APICs, actually call clear_IO_APIC
Yinghai Lu98c061b2009-02-16 00:00:50 -08001738 * We need clear_IO_APIC before enabling error vector
Andi Kleen739f33b2008-01-30 13:30:40 +01001739 */
1740 if (!skip_ioapic_setup && nr_ioapics)
1741 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001742#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001743
Jan Beulich2fb270f2011-02-09 08:21:02 +00001744 bsp_end_local_APIC_setup();
Andi Kleen739f33b2008-01-30 13:30:40 +01001745
Yinghai Lufa2bd352008-08-24 02:01:50 -07001746#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001747 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1748 setup_IO_APIC();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001749 else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001750 nr_ioapics = 0;
Yinghai Lu98c061b2009-02-16 00:00:50 -08001751 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001752#endif
1753
Thomas Gleixner736deca2009-08-19 12:35:53 +02001754 x86_init.timers.setup_percpu_clockev();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001755 return 0;
1756}
1757
1758/*
1759 * Local APIC interrupts
1760 */
1761
1762/*
1763 * This interrupt should _never_ happen with our APIC/SMP architecture
1764 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001765void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001766{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001767 u32 v;
1768
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001769 exit_idle();
1770 irq_enter();
1771 /*
1772 * Check if this really is a spurious interrupt and ACK it
1773 * if it is a vectored one. Just in case...
1774 * Spurious interrupts should not be ACKed.
1775 */
1776 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1777 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1778 ack_APIC_irq();
1779
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001780 inc_irq_stat(irq_spurious_count);
1781
Yinghai Ludc1528d2008-08-24 02:01:53 -07001782 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001783 pr_info("spurious APIC interrupt on CPU#%d, "
1784 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001785 irq_exit();
1786}
1787
1788/*
1789 * This interrupt should never happen with our APIC/SMP architecture
1790 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001791void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001792{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001793 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001794
1795 exit_idle();
1796 irq_enter();
1797 /* First tickle the hardware, only then report what went on. -- REW */
1798 v = apic_read(APIC_ESR);
1799 apic_write(APIC_ESR, 0);
1800 v1 = apic_read(APIC_ESR);
1801 ack_APIC_irq();
1802 atomic_inc(&irq_err_count);
1803
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001804 /*
1805 * Here is what the APIC error bits mean:
1806 * 0: Send CS error
1807 * 1: Receive CS error
1808 * 2: Send accept error
1809 * 3: Receive accept error
1810 * 4: Reserved
1811 * 5: Send illegal vector
1812 * 6: Received illegal vector
1813 * 7: Illegal register address
1814 */
1815 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001816 smp_processor_id(), v , v1);
1817 irq_exit();
1818}
1819
Glauber Costab5841762008-05-28 13:38:28 -03001820/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001821 * connect_bsp_APIC - attach the APIC to the interrupt system
1822 */
Glauber Costab5841762008-05-28 13:38:28 -03001823void __init connect_bsp_APIC(void)
1824{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001825#ifdef CONFIG_X86_32
1826 if (pic_mode) {
1827 /*
1828 * Do not trust the local APIC being empty at bootup.
1829 */
1830 clear_local_APIC();
1831 /*
1832 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1833 * local APIC to INT and NMI lines.
1834 */
1835 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1836 "enabling APIC mode.\n");
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +04001837 imcr_pic_to_apic();
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001838 }
1839#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001840 if (apic->enable_apic_mode)
1841 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001842}
1843
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001844/**
1845 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1846 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1847 *
1848 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1849 * APIC is disabled.
1850 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001851void disconnect_bsp_APIC(int virt_wire_setup)
1852{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001853 unsigned int value;
1854
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001855#ifdef CONFIG_X86_32
1856 if (pic_mode) {
1857 /*
1858 * Put the board back into PIC mode (has an effect only on
1859 * certain older boards). Note that APIC interrupts, including
1860 * IPIs, won't work beyond this point! The only exception are
1861 * INIT IPIs.
1862 */
1863 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1864 "entering PIC mode.\n");
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +04001865 imcr_apic_to_pic();
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001866 return;
1867 }
1868#endif
1869
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001870 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001871
1872 /* For the spurious interrupt use vector F, and enable it */
1873 value = apic_read(APIC_SPIV);
1874 value &= ~APIC_VECTOR_MASK;
1875 value |= APIC_SPIV_APIC_ENABLED;
1876 value |= 0xf;
1877 apic_write(APIC_SPIV, value);
1878
1879 if (!virt_wire_setup) {
1880 /*
1881 * For LVT0 make it edge triggered, active high,
1882 * external and enabled
1883 */
1884 value = apic_read(APIC_LVT0);
1885 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1886 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1887 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1888 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1889 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1890 apic_write(APIC_LVT0, value);
1891 } else {
1892 /* Disable LVT0 */
1893 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1894 }
1895
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001896 /*
1897 * For LVT1 make it edge triggered, active high,
1898 * nmi and enabled
1899 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001900 value = apic_read(APIC_LVT1);
1901 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1902 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1903 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1904 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1905 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1906 apic_write(APIC_LVT1, value);
1907}
1908
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001909void __cpuinit generic_processor_info(int apicid, int version)
1910{
1911 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001912
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001913 /*
1914 * Validate version
1915 */
1916 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001917 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001918 "fixing up to 0x10. (tell your hw vendor)\n",
1919 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001920 version = 0x10;
1921 }
1922 apic_version[apicid] = version;
1923
Mike Travis3b11ce72008-12-17 15:21:39 -08001924 if (num_processors >= nr_cpu_ids) {
1925 int max = nr_cpu_ids;
1926 int thiscpu = max + disabled_cpus;
1927
1928 pr_warning(
1929 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1930 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1931
1932 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001933 return;
1934 }
1935
1936 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001937 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001938
Mike Travisb2b815d2009-01-16 15:22:16 -08001939 if (version != apic_version[boot_cpu_physical_apicid])
1940 WARN_ONCE(1,
1941 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1942 apic_version[boot_cpu_physical_apicid], cpu, version);
1943
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001944 physid_set(apicid, phys_cpu_present_map);
1945 if (apicid == boot_cpu_physical_apicid) {
1946 /*
1947 * x86_bios_cpu_apicid is required to have processors listed
1948 * in same order as logical cpu numbers. Hence the first
1949 * entry is BSP, and so on.
1950 */
1951 cpu = 0;
1952 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001953 if (apicid > max_physical_apicid)
1954 max_physical_apicid = apicid;
1955
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001956#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001957 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1958 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001959#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001960
Mike Travis1de88cd2008-12-16 17:34:02 -08001961 set_cpu_possible(cpu, true);
1962 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001963}
1964
Suresh Siddha0c81c742008-07-10 11:16:48 -07001965int hard_smp_processor_id(void)
1966{
1967 return read_apic_id();
1968}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001969
1970void default_init_apic_ldr(void)
1971{
1972 unsigned long val;
1973
1974 apic_write(APIC_DFR, APIC_DFR_VALUE);
1975 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1976 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1977 apic_write(APIC_LDR, val);
1978}
1979
1980#ifdef CONFIG_X86_32
1981int default_apicid_to_node(int logical_apicid)
1982{
1983#ifdef CONFIG_SMP
1984 return apicid_2_node[hard_smp_processor_id()];
1985#else
1986 return 0;
1987#endif
1988}
Yinghai Lu34919982008-08-24 02:01:48 -07001989#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07001990
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001991/*
1992 * Power management
1993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994#ifdef CONFIG_PM
1995
1996static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001997 /*
1998 * 'active' is true if the local APIC was enabled by us and
1999 * not the BIOS; this signifies that we are also responsible
2000 * for disabling it before entering apm/acpi suspend
2001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int active;
2003 /* r/w apic fields */
2004 unsigned int apic_id;
2005 unsigned int apic_taskpri;
2006 unsigned int apic_ldr;
2007 unsigned int apic_dfr;
2008 unsigned int apic_spiv;
2009 unsigned int apic_lvtt;
2010 unsigned int apic_lvtpc;
2011 unsigned int apic_lvt0;
2012 unsigned int apic_lvt1;
2013 unsigned int apic_lvterr;
2014 unsigned int apic_tmict;
2015 unsigned int apic_tdcr;
2016 unsigned int apic_thmr;
2017} apic_pm_state;
2018
Pavel Machek0b9c33a2005-04-16 15:25:31 -07002019static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002022 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 if (!apic_pm_state.active)
2025 return 0;
2026
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002027 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002028
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07002029 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2031 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2032 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2033 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2034 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01002035 if (maxlvt >= 4)
2036 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2038 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2039 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2040 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2041 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Andi Kleen4efc0672009-04-28 19:07:31 +02002042#ifdef CONFIG_X86_THERMAL_VECTOR
Karsten Wiesef990fff2006-12-07 02:14:11 +01002043 if (maxlvt >= 5)
2044 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2045#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04002046
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02002047 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 disable_local_APIC();
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002049
Fenghua Yub24696b2009-03-27 14:22:44 -07002050 if (intr_remapping_enabled)
2051 disable_intr_remapping();
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 local_irq_restore(flags);
2054 return 0;
2055}
2056
2057static int lapic_resume(struct sys_device *dev)
2058{
2059 unsigned int l, h;
2060 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002061 int maxlvt;
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002062 int ret = 0;
Fenghua Yub24696b2009-03-27 14:22:44 -07002063 struct IO_APIC_route_entry **ioapic_entries = NULL;
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!apic_pm_state.active)
2066 return 0;
2067
Fenghua Yub24696b2009-03-27 14:22:44 -07002068 local_irq_save(flags);
Weidong Han9a2755c2009-04-17 16:42:16 +08002069 if (intr_remapping_enabled) {
Fenghua Yub24696b2009-03-27 14:22:44 -07002070 ioapic_entries = alloc_ioapic_entries();
2071 if (!ioapic_entries) {
2072 WARN(1, "Alloc ioapic_entries in lapic resume failed.");
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002073 ret = -ENOMEM;
2074 goto restore;
Fenghua Yub24696b2009-03-27 14:22:44 -07002075 }
2076
2077 ret = save_IO_APIC_setup(ioapic_entries);
2078 if (ret) {
2079 WARN(1, "Saving IO-APIC state failed: %d\n", ret);
2080 free_ioapic_entries(ioapic_entries);
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002081 goto restore;
Fenghua Yub24696b2009-03-27 14:22:44 -07002082 }
2083
2084 mask_IO_APIC_setup(ioapic_entries);
Jacob Panb81bb372009-11-09 11:27:04 -08002085 legacy_pic->mask_all();
Fenghua Yub24696b2009-03-27 14:22:44 -07002086 }
Karsten Wiesef990fff2006-12-07 02:14:11 +01002087
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002088 if (x2apic_mode)
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002089 enable_x2apic();
Suresh Siddhacf6567f2009-03-16 17:05:00 -07002090 else {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002091 /*
2092 * Make sure the APICBASE points to the right address
2093 *
2094 * FIXME! This will be wrong if we ever support suspend on
2095 * SMP! We'll need to do this as part of the CPU restore!
2096 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002097 rdmsr(MSR_IA32_APICBASE, l, h);
2098 l &= ~MSR_IA32_APICBASE_BASE;
2099 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2100 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002101 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002102
Fenghua Yub24696b2009-03-27 14:22:44 -07002103 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2105 apic_write(APIC_ID, apic_pm_state.apic_id);
2106 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2107 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2108 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2109 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2110 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2111 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002112#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002113 if (maxlvt >= 5)
2114 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2115#endif
2116 if (maxlvt >= 4)
2117 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2119 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2120 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2121 apic_write(APIC_ESR, 0);
2122 apic_read(APIC_ESR);
2123 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2124 apic_write(APIC_ESR, 0);
2125 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002126
Weidong Han9a2755c2009-04-17 16:42:16 +08002127 if (intr_remapping_enabled) {
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002128 reenable_intr_remapping(x2apic_mode);
Jacob Panb81bb372009-11-09 11:27:04 -08002129 legacy_pic->restore_mask();
Fenghua Yub24696b2009-03-27 14:22:44 -07002130 restore_IO_APIC_setup(ioapic_entries);
2131 free_ioapic_entries(ioapic_entries);
2132 }
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002133restore:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002135
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002139/*
2140 * This device has no shutdown method - fully functioning local APICs
2141 * are needed on every CPU up until machine_halt/restart/poweroff.
2142 */
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002145 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 .resume = lapic_resume,
2147 .suspend = lapic_suspend,
2148};
2149
2150static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002151 .id = 0,
2152 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153};
2154
Ashok Raje6982c62005-06-25 14:54:58 -07002155static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 apic_pm_state.active = 1;
2158}
2159
2160static int __init init_lapic_sysfs(void)
2161{
2162 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (!cpu_has_apic)
2165 return 0;
2166 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 error = sysdev_class_register(&lapic_sysclass);
2169 if (!error)
2170 error = sysdev_register(&device_lapic);
2171 return error;
2172}
Fenghua Yub24696b2009-03-27 14:22:44 -07002173
2174/* local apic needs to resume before other devices access its registers. */
2175core_initcall(init_lapic_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177#else /* CONFIG_PM */
2178
2179static void apic_pm_activate(void) { }
2180
2181#endif /* CONFIG_PM */
2182
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002183#ifdef CONFIG_X86_64
Yinghai Lue0e42142009-04-26 23:39:38 -07002184
2185static int __cpuinit apic_cluster_num(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 int i, clusters, zeros;
2188 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002189 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2191
Mike Travis23ca4bb2008-05-12 21:21:12 +02002192 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002193 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Mike Travis168ef542008-12-16 17:34:01 -08002195 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002196 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002197 if (bios_cpu_apicid) {
2198 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302199 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002200 if (cpu_present(i))
2201 id = per_cpu(x86_bios_cpu_apicid, i);
2202 else
2203 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302204 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002205 break;
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (id != BAD_APICID)
2208 __set_bit(APIC_CLUSTERID(id), clustermap);
2209 }
2210
2211 /* Problem: Partially populated chassis may not have CPUs in some of
2212 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002213 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2214 * Since clusters are allocated sequentially, count zeros only if
2215 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 */
2217 clusters = 0;
2218 zeros = 0;
2219 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2220 if (test_bit(i, clustermap)) {
2221 clusters += 1 + zeros;
2222 zeros = 0;
2223 } else
2224 ++zeros;
2225 }
2226
Yinghai Lue0e42142009-04-26 23:39:38 -07002227 return clusters;
2228}
2229
2230static int __cpuinitdata multi_checked;
2231static int __cpuinitdata multi;
2232
2233static int __cpuinit set_multi(const struct dmi_system_id *d)
2234{
2235 if (multi)
2236 return 0;
Cyrill Gorcunov6f0aced2009-05-01 23:54:25 +04002237 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
Yinghai Lue0e42142009-04-26 23:39:38 -07002238 multi = 1;
2239 return 0;
2240}
2241
2242static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
2243 {
2244 .callback = set_multi,
2245 .ident = "IBM System Summit2",
2246 .matches = {
2247 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2248 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2249 },
2250 },
2251 {}
2252};
2253
2254static void __cpuinit dmi_check_multi(void)
2255{
2256 if (multi_checked)
2257 return;
2258
2259 dmi_check_system(multi_dmi_table);
2260 multi_checked = 1;
2261}
2262
2263/*
2264 * apic_is_clustered_box() -- Check if we can expect good TSC
2265 *
2266 * Thus far, the major user of this is IBM's Summit2 series:
2267 * Clustered boxes may have unsynced TSC problems if they are
2268 * multi-chassis.
2269 * Use DMI to check them
2270 */
2271__cpuinit int apic_is_clustered_box(void)
2272{
2273 dmi_check_multi();
2274 if (multi)
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002275 return 1;
2276
Yinghai Lue0e42142009-04-26 23:39:38 -07002277 if (!is_vsmp_box())
2278 return 0;
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /*
Yinghai Lue0e42142009-04-26 23:39:38 -07002281 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2282 * not guaranteed to be synced between boards
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
Yinghai Lue0e42142009-04-26 23:39:38 -07002284 if (apic_cluster_num() > 1)
2285 return 1;
2286
2287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002292 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002294static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002297 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002298 return 0;
2299}
2300early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002302/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002303static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002304{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002305 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002306}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002307early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002309static int __init parse_lapic_timer_c2_ok(char *arg)
2310{
2311 local_apic_timer_c2_ok = 1;
2312 return 0;
2313}
2314early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2315
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002316static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002319 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002320}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002321early_param("noapictimer", parse_disable_apic_timer);
2322
2323static int __init parse_nolapic_timer(char *arg)
2324{
2325 disable_apic_timer = 1;
2326 return 0;
2327}
2328early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002329
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002330static int __init apic_set_verbosity(char *arg)
2331{
2332 if (!arg) {
2333#ifdef CONFIG_X86_64
2334 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002335 return 0;
2336#endif
2337 return -EINVAL;
2338 }
2339
2340 if (strcmp("debug", arg) == 0)
2341 apic_verbosity = APIC_DEBUG;
2342 else if (strcmp("verbose", arg) == 0)
2343 apic_verbosity = APIC_VERBOSE;
2344 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002345 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002346 " use apic=verbose or apic=debug\n", arg);
2347 return -EINVAL;
2348 }
2349
2350 return 0;
2351}
2352early_param("apic", apic_set_verbosity);
2353
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002354static int __init lapic_insert_resource(void)
2355{
2356 if (!apic_phys)
2357 return -1;
2358
2359 /* Put local APIC into the resource map. */
2360 lapic_resource.start = apic_phys;
2361 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2362 insert_resource(&iomem_resource, &lapic_resource);
2363
2364 return 0;
2365}
2366
2367/*
2368 * need call insert after e820_reserve_resources()
2369 * that is using request_resource
2370 */
2371late_initcall(lapic_insert_resource);