blob: bb46711a0b1eba21eca78b416d26d899eee859d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
Aaron Durbin39928722006-12-07 02:14:01 +010026#include <linux/ioport.h>
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020027#include <linux/clockchips.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010028#include <linux/acpi_pmtmr.h>
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010029#include <linux/module.h>
Suresh Siddha6e1cb382008-07-10 11:16:58 -070030#include <linux/dmar.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/atomic.h>
33#include <asm/smp.h>
34#include <asm/mtrr.h>
35#include <asm/mpspec.h>
Yinghai Luefa25592008-08-19 20:50:36 -070036#include <asm/desc.h>
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010037#include <asm/hpet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/pgalloc.h>
Andi Kleen75152112005-05-16 21:53:34 -070039#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010040#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010041#include <asm/proto.h>
42#include <asm/timex.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020043#include <asm/apic.h>
Suresh Siddha6e1cb382008-07-10 11:16:58 -070044#include <asm/i8259.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Glauber Costa5af55732008-03-25 13:28:56 -030046#include <mach_ipi.h>
Glauber Costadd46e3c2008-03-25 18:10:46 -030047#include <mach_apic.h>
Glauber Costa5af55732008-03-25 13:28:56 -030048
Cyrill Gorcunov36fef092008-08-15 13:51:20 +020049/* Disable local APIC timer from the kernel commandline or via dmi quirk */
Thomas Gleixneraa276e12008-06-09 19:15:00 +020050static int disable_apic_timer __cpuinitdata;
Chris Wrightbc1d99c2007-10-12 23:04:23 +020051static int apic_calibrate_pmtmr __initdata;
Thomas Gleixner0e078e22008-01-30 13:30:20 +010052int disable_apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -070053int disable_x2apic;
Suresh Siddha89027d32008-07-10 11:16:56 -070054int x2apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Suresh Siddha6e1cb382008-07-10 11:16:58 -070056/* x2apic enabled before OS handover */
57int x2apic_preenabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010059/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -070060int local_apic_timer_c2_ok;
61EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
62
Yinghai Luefa25592008-08-19 20:50:36 -070063int first_system_vector = 0xfe;
64
65char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
66
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010067/*
68 * Debug level, exported for io_apic.c
69 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +010070unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010071
Alexey Starikovskiybab4b272008-05-19 19:47:03 +040072/* Have we found an MP table */
73int smp_found_config;
74
Aaron Durbin39928722006-12-07 02:14:01 +010075static struct resource lapic_resource = {
76 .name = "Local APIC",
77 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
78};
79
Thomas Gleixnerd03030e2007-10-12 23:04:06 +020080static unsigned int calibration_result;
81
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020082static int lapic_next_event(unsigned long delta,
83 struct clock_event_device *evt);
84static void lapic_timer_setup(enum clock_event_mode mode,
85 struct clock_event_device *evt);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020086static void lapic_timer_broadcast(cpumask_t mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +010087static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020088
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +040089/*
90 * The local apic timer can be used for any function which is CPU local.
91 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020092static struct clock_event_device lapic_clockevent = {
93 .name = "lapic",
94 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
95 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
96 .shift = 32,
97 .set_mode = lapic_timer_setup,
98 .set_next_event = lapic_next_event,
99 .broadcast = lapic_timer_broadcast,
100 .rating = 100,
101 .irq = -1,
102};
103static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
104
Andi Kleend3432892008-01-30 13:33:17 +0100105static unsigned long apic_phys;
106
Alexey Starikovskiy3f530702008-03-27 23:55:47 +0300107unsigned long mp_lapic_addr;
108
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100109/*
110 * Get the LAPIC version
111 */
112static inline int lapic_get_version(void)
113{
114 return GET_APIC_VERSION(apic_read(APIC_LVR));
115}
116
117/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400118 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100119 */
120static inline int lapic_is_integrated(void)
121{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400122#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100123 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400124#else
125 return APIC_INTEGRATED(lapic_get_version());
126#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100127}
128
129/*
130 * Check, whether this is a modern or a first generation APIC
131 */
132static int modern_apic(void)
133{
134 /* AMD systems use old APIC versions, so check the CPU */
135 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
136 boot_cpu_data.x86 >= 0xf)
137 return 1;
138 return lapic_get_version() >= 0x14;
139}
140
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400141/*
142 * Paravirt kernels also might be using these below ops. So we still
143 * use generic apic_read()/apic_write(), which might be pointing to different
144 * ops in PARAVIRT case.
145 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700146void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100147{
148 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
149 cpu_relax();
150}
151
Suresh Siddha1b374e42008-07-10 11:16:49 -0700152u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100153{
154 u32 send_status;
155 int timeout;
156
157 timeout = 0;
158 do {
159 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
160 if (!send_status)
161 break;
162 udelay(100);
163 } while (timeout++ < 1000);
164
165 return send_status;
166}
167
Suresh Siddha1b374e42008-07-10 11:16:49 -0700168void xapic_icr_write(u32 low, u32 id)
169{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200170 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700171 apic_write(APIC_ICR, low);
172}
173
174u64 xapic_icr_read(void)
175{
176 u32 icr1, icr2;
177
178 icr2 = apic_read(APIC_ICR2);
179 icr1 = apic_read(APIC_ICR);
180
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400181 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700182}
183
184static struct apic_ops xapic_ops = {
185 .read = native_apic_mem_read,
186 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700187 .icr_read = xapic_icr_read,
188 .icr_write = xapic_icr_write,
189 .wait_icr_idle = xapic_wait_icr_idle,
190 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
191};
192
193struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700194EXPORT_SYMBOL_GPL(apic_ops);
195
Suresh Siddha13c88fb2008-07-10 11:16:52 -0700196static void x2apic_wait_icr_idle(void)
197{
198 /* no need to wait for icr idle in x2apic */
199 return;
200}
201
202static u32 safe_x2apic_wait_icr_idle(void)
203{
204 /* no need to wait for icr idle in x2apic */
205 return 0;
206}
207
208void x2apic_icr_write(u32 low, u32 id)
209{
210 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
211}
212
213u64 x2apic_icr_read(void)
214{
215 unsigned long val;
216
217 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
218 return val;
219}
220
221static struct apic_ops x2apic_ops = {
222 .read = native_apic_msr_read,
223 .write = native_apic_msr_write,
Suresh Siddha13c88fb2008-07-10 11:16:52 -0700224 .icr_read = x2apic_icr_read,
225 .icr_write = x2apic_icr_write,
226 .wait_icr_idle = x2apic_wait_icr_idle,
227 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
228};
229
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100230/**
231 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
232 */
Jan Beuliche9427102008-01-30 13:31:24 +0100233void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100234{
235 unsigned int v;
236
237 /* unmask and set to NMI */
238 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200239
240 /* Level triggered for 82489DX (32bit mode) */
241 if (!lapic_is_integrated())
242 v |= APIC_LVT_LEVEL_TRIGGER;
243
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100244 apic_write(APIC_LVT0, v);
245}
246
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700247#ifdef CONFIG_X86_32
248/**
249 * get_physical_broadcast - Get number of physical broadcast IDs
250 */
251int get_physical_broadcast(void)
252{
253 return modern_apic() ? 0xff : 0xf;
254}
255#endif
256
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100257/**
258 * lapic_get_maxlvt - get the maximum number of local vector table entries
259 */
260int lapic_get_maxlvt(void)
261{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200262 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100263
264 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200265 /*
266 * - we always have APIC integrated on 64bit mode
267 * - 82489DXs do not report # of LVT entries
268 */
269 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100270}
271
272/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400273 * Local APIC timer
274 */
275
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400276/* Clock divisor */
277#ifdef CONFG_X86_64
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200278#define APIC_DIVISOR 1
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400279#else
280#define APIC_DIVISOR 16
281#endif
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200282
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100283/*
284 * This function sets up the local APIC timer, with a timeout of
285 * 'clocks' APIC bus clock. During calibration we actually call
286 * this function twice on the boot CPU, once with a bogus timeout
287 * value, second time for real. The other (noncalibrating) CPUs
288 * call this function only once, with the real, calibrated value.
289 *
290 * We do reads before writes even if unnecessary, to get around the
291 * P5 APIC double write bug.
292 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100293static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
294{
295 unsigned int lvtt_value, tmp_value;
296
297 lvtt_value = LOCAL_TIMER_VECTOR;
298 if (!oneshot)
299 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200300 if (!lapic_is_integrated())
301 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
302
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100303 if (!irqen)
304 lvtt_value |= APIC_LVT_MASKED;
305
306 apic_write(APIC_LVTT, lvtt_value);
307
308 /*
309 * Divide PICLK by 16
310 */
311 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400312 apic_write(APIC_TDCR,
313 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
314 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100315
316 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200317 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100318}
319
320/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100321 * Setup extended LVT, AMD specific (K8, family 10h)
322 *
323 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
324 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200325 *
326 * If mask=1, the LVT entry does not generate interrupts while mask=0
327 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100328 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100329
330#define APIC_EILVT_LVTOFF_MCE 0
331#define APIC_EILVT_LVTOFF_IBS 1
332
333static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100334{
Robert Richter7b83dae2008-01-30 13:30:40 +0100335 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100336 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
337
338 apic_write(reg, v);
339}
340
Robert Richter7b83dae2008-01-30 13:30:40 +0100341u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
342{
343 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
344 return APIC_EILVT_LVTOFF_MCE;
345}
346
347u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
348{
349 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
350 return APIC_EILVT_LVTOFF_IBS;
351}
Robert Richter6aa360e2008-07-23 15:28:14 +0200352EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100353
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100354/*
355 * Program the next event, relative to now
356 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200357static int lapic_next_event(unsigned long delta,
358 struct clock_event_device *evt)
359{
360 apic_write(APIC_TMICT, delta);
361 return 0;
362}
363
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100364/*
365 * Setup the lapic timer in periodic or oneshot mode
366 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200367static void lapic_timer_setup(enum clock_event_mode mode,
368 struct clock_event_device *evt)
369{
370 unsigned long flags;
371 unsigned int v;
372
373 /* Lapic used as dummy for broadcast ? */
374 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
375 return;
376
377 local_irq_save(flags);
378
379 switch (mode) {
380 case CLOCK_EVT_MODE_PERIODIC:
381 case CLOCK_EVT_MODE_ONESHOT:
382 __setup_APIC_LVTT(calibration_result,
383 mode != CLOCK_EVT_MODE_PERIODIC, 1);
384 break;
385 case CLOCK_EVT_MODE_UNUSED:
386 case CLOCK_EVT_MODE_SHUTDOWN:
387 v = apic_read(APIC_LVTT);
388 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
389 apic_write(APIC_LVTT, v);
390 break;
391 case CLOCK_EVT_MODE_RESUME:
392 /* Nothing to do here */
393 break;
394 }
395
396 local_irq_restore(flags);
397}
398
399/*
400 * Local APIC timer broadcast function
401 */
402static void lapic_timer_broadcast(cpumask_t mask)
403{
404#ifdef CONFIG_SMP
405 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
406#endif
407}
408
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100409/*
410 * Setup the local APIC timer for this CPU. Copy the initilized values
411 * of the boot CPU and register the clock event in the framework.
412 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700413static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200414{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100415 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
416
417 memcpy(levt, &lapic_clockevent, sizeof(*levt));
418 levt->cpumask = cpumask_of_cpu(smp_processor_id());
419
420 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200421}
422
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100423/*
424 * In this function we calibrate APIC bus clocks to the external
425 * timer. Unfortunately we cannot use jiffies and the timer irq
426 * to calibrate, since some later bootup code depends on getting
427 * the first irq? Ugh.
428 *
429 * We want to do the calibration only once since we
430 * want to have local timer irqs syncron. CPUs connected
431 * by the same APIC bus have the very same bus frequency.
432 * And we want to have irqs off anyways, no accidental
433 * APIC irq that way.
434 */
435
436#define TICK_COUNT 100000000
437
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400438static int __init calibrate_APIC_clock(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200439{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100440 unsigned apic, apic_start;
441 unsigned long tsc, tsc_start;
442 int result;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200443
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100444 local_irq_disable();
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200445
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100446 /*
447 * Put whatever arbitrary (but long enough) timeout
448 * value into the APIC clock, we just want to get the
449 * counter running for calibration.
450 *
451 * No interrupt enable !
452 */
453 __setup_APIC_LVTT(250000000, 0, 0);
454
455 apic_start = apic_read(APIC_TMCCT);
456#ifdef CONFIG_X86_PM_TIMER
457 if (apic_calibrate_pmtmr && pmtmr_ioport) {
458 pmtimer_wait(5000); /* 5ms wait */
459 apic = apic_read(APIC_TMCCT);
460 result = (apic_start - apic) * 1000L / 5;
461 } else
462#endif
463 {
464 rdtscll(tsc_start);
465
466 do {
467 apic = apic_read(APIC_TMCCT);
468 rdtscll(tsc);
469 } while ((tsc - tsc_start) < TICK_COUNT &&
470 (apic_start - apic) < TICK_COUNT);
471
472 result = (apic_start - apic) * 1000L * tsc_khz /
473 (tsc - tsc_start);
474 }
475
476 local_irq_enable();
477
478 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
479
480 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
481 result / 1000 / 1000, result / 1000 % 1000);
482
483 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900484 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
485 lapic_clockevent.shift);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100486 lapic_clockevent.max_delta_ns =
487 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
488 lapic_clockevent.min_delta_ns =
489 clockevent_delta2ns(0xF, &lapic_clockevent);
490
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200491 calibration_result = (result * APIC_DIVISOR) / HZ;
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400492
493 /*
494 * Do a sanity check on the APIC calibration result
495 */
496 if (calibration_result < (1000000 / HZ)) {
497 printk(KERN_WARNING
498 "APIC frequency too slow, disabling apic timer\n");
499 return -1;
500 }
501
502 return 0;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200503}
504
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100505/*
506 * Setup the boot APIC
507 *
508 * Calibrate and verify the result.
509 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100510void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100512 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400513 * The local apic timer can be disabled via the kernel
514 * commandline or from the CPU detection code. Register the lapic
515 * timer as a dummy clock event source on SMP systems, so the
516 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100517 */
518 if (disable_apic_timer) {
519 printk(KERN_INFO "Disabling APIC timer\n");
520 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100521 if (num_possible_cpus() > 1) {
522 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100523 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100524 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100525 return;
526 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200527
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400528 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
529 "calibrating APIC timer ...\n");
530
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400531 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100532 /* No broadcast on UP ! */
533 if (num_possible_cpus() > 1)
534 setup_APIC_timer();
535 return;
536 }
537
538 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100539 * If nmi_watchdog is set to IO_APIC, we need the
540 * PIT/HPET going. Otherwise register lapic as a dummy
541 * device.
542 */
543 if (nmi_watchdog != NMI_IO_APIC)
544 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
545 else
546 printk(KERN_WARNING "APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200547 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100548
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400549 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100550 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100553void __cpuinit setup_secondary_APIC_clock(void)
554{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100555 setup_APIC_timer();
556}
557
558/*
559 * The guts of the apic timer interrupt
560 */
561static void local_apic_timer_interrupt(void)
562{
563 int cpu = smp_processor_id();
564 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
565
566 /*
567 * Normally we should not be here till LAPIC has been initialized but
568 * in some cases like kdump, its possible that there is a pending LAPIC
569 * timer interrupt from previous kernel's context and is delivered in
570 * new kernel the moment interrupts are enabled.
571 *
572 * Interrupts are enabled early and LAPIC is setup much later, hence
573 * its possible that when we get here evt->event_handler is NULL.
574 * Check for event_handler being NULL and discard the interrupt as
575 * spurious.
576 */
577 if (!evt->event_handler) {
578 printk(KERN_WARNING
579 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
580 /* Switch it off */
581 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
582 return;
583 }
584
585 /*
586 * the NMI deadlock-detector uses this.
587 */
Cyrill Gorcunov0b23e8c2008-08-18 20:45:59 +0400588#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100589 add_pda(apic_timer_irqs, 1);
Cyrill Gorcunov0b23e8c2008-08-18 20:45:59 +0400590#else
591 per_cpu(irq_stat, cpu).apic_timer_irqs++;
592#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100593
594 evt->event_handler(evt);
595}
596
597/*
598 * Local APIC timer interrupt. This is the most natural way for doing
599 * local interrupts, but local timer interrupts can be emulated by
600 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
601 *
602 * [ if a single-CPU system runs an SMP kernel then we call the local
603 * interrupt as well. Thus we cannot inline the local irq ... ]
604 */
605void smp_apic_timer_interrupt(struct pt_regs *regs)
606{
607 struct pt_regs *old_regs = set_irq_regs(regs);
608
609 /*
610 * NOTE! We'd better ACK the irq immediately,
611 * because timer handling can be slow.
612 */
613 ack_APIC_irq();
614 /*
615 * update_process_times() expects us to have done irq_enter().
616 * Besides, if we don't timer interrupts ignore the global
617 * interrupt lock, which is the WrongThing (tm) to do.
618 */
619 exit_idle();
620 irq_enter();
621 local_apic_timer_interrupt();
622 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400623
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100624 set_irq_regs(old_regs);
625}
626
627int setup_profiling_timer(unsigned int multiplier)
628{
629 return -EINVAL;
630}
631
632
633/*
634 * Local APIC start and shutdown
635 */
636
637/**
638 * clear_local_APIC - shutdown the local APIC
639 *
640 * This is called, when a CPU is disabled and before rebooting, so the state of
641 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
642 * leftovers during boot.
643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644void clear_local_APIC(void)
645{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400646 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100647 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Andi Kleend3432892008-01-30 13:33:17 +0100649 /* APIC hasn't been mapped yet */
650 if (!apic_phys)
651 return;
652
653 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200655 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * if the vector is zero. Mask LVTERR first to prevent this.
657 */
658 if (maxlvt >= 3) {
659 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100660 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 /*
663 * Careful: we have to set masks only first to deassert
664 * any level-triggered sources.
665 */
666 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100667 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100669 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100671 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (maxlvt >= 4) {
673 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100674 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400677 /* lets not touch this if we didn't frob it */
678#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
679 if (maxlvt >= 5) {
680 v = apic_read(APIC_LVTTHMR);
681 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
682 }
683#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Clean APIC state for other OSs:
686 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100687 apic_write(APIC_LVTT, APIC_LVT_MASKED);
688 apic_write(APIC_LVT0, APIC_LVT_MASKED);
689 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100691 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100693 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400694
695 /* Integrated APIC (!82489DX) ? */
696 if (lapic_is_integrated()) {
697 if (maxlvt > 3)
698 /* Clear ESR due to Pentium errata 3AP and 11AP */
699 apic_write(APIC_ESR, 0);
700 apic_read(APIC_ESR);
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100704/**
705 * disable_local_APIC - clear and disable the local APIC
706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707void disable_local_APIC(void)
708{
709 unsigned int value;
710
711 clear_local_APIC();
712
713 /*
714 * Disable APIC (implies clearing of registers
715 * for 82489DX!).
716 */
717 value = apic_read(APIC_SPIV);
718 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100719 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400720
721#ifdef CONFIG_X86_32
722 /*
723 * When LAPIC was disabled by the BIOS and enabled by the kernel,
724 * restore the disabled state.
725 */
726 if (enabled_via_apicbase) {
727 unsigned int l, h;
728
729 rdmsr(MSR_IA32_APICBASE, l, h);
730 l &= ~MSR_IA32_APICBASE_ENABLE;
731 wrmsr(MSR_IA32_APICBASE, l, h);
732 }
733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400736/*
737 * If Linux enabled the LAPIC against the BIOS default disable it down before
738 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
739 * not power-off. Additionally clear all LVT entries before disable_local_APIC
740 * for the case where Linux didn't enable the LAPIC.
741 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700742void lapic_shutdown(void)
743{
744 unsigned long flags;
745
746 if (!cpu_has_apic)
747 return;
748
749 local_irq_save(flags);
750
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400751#ifdef CONFIG_X86_32
752 if (!enabled_via_apicbase)
753 clear_local_APIC();
754 else
755#endif
756 disable_local_APIC();
757
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700758
759 local_irq_restore(flags);
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * This is to verify that we're looking at a real local APIC.
764 * Check these against your board if the CPUs aren't getting
765 * started for no apparent reason.
766 */
767int __init verify_local_APIC(void)
768{
769 unsigned int reg0, reg1;
770
771 /*
772 * The version register is read-only in a real APIC.
773 */
774 reg0 = apic_read(APIC_LVR);
775 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
776 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
777 reg1 = apic_read(APIC_LVR);
778 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
779
780 /*
781 * The two version reads above should print the same
782 * numbers. If the second one is different, then we
783 * poke at a non-APIC.
784 */
785 if (reg1 != reg0)
786 return 0;
787
788 /*
789 * Check if the version looks reasonably.
790 */
791 reg1 = GET_APIC_VERSION(reg0);
792 if (reg1 == 0x00 || reg1 == 0xff)
793 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100794 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (reg1 < 0x02 || reg1 == 0xff)
796 return 0;
797
798 /*
799 * The ID register is read/write in a real APIC.
800 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700801 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
803 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700804 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
806 apic_write(APIC_ID, reg0);
807 if (reg1 != (reg0 ^ APIC_ID_MASK))
808 return 0;
809
810 /*
811 * The next two are just to see if we have sane values.
812 * They're only really relevant if we're in Virtual Wire
813 * compatibility mode, but most boxes are anymore.
814 */
815 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100816 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 reg1 = apic_read(APIC_LVT1);
818 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
819
820 return 1;
821}
822
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100823/**
824 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826void __init sync_Arb_IDs(void)
827{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +0200828 /*
829 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
830 * needed on AMD.
831 */
832 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return;
834
835 /*
836 * Wait for idle.
837 */
838 apic_wait_icr_idle();
839
840 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +0400841 apic_write(APIC_ICR, APIC_DEST_ALLINC |
842 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/*
846 * An initial setup of the virtual wire mode.
847 */
848void __init init_bsp_APIC(void)
849{
Andi Kleen11a8e772006-01-11 22:46:51 +0100850 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Don't do the setup now if we have a SMP BIOS as the
854 * through-I/O-APIC virtual wire mode might be active.
855 */
856 if (smp_found_config || !cpu_has_apic)
857 return;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /*
860 * Do not trust the local APIC being empty at bootup.
861 */
862 clear_local_APIC();
863
864 /*
865 * Enable APIC.
866 */
867 value = apic_read(APIC_SPIV);
868 value &= ~APIC_VECTOR_MASK;
869 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +0400870
871#ifdef CONFIG_X86_32
872 /* This bit is reserved on P4/Xeon and should be cleared */
873 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
874 (boot_cpu_data.x86 == 15))
875 value &= ~APIC_SPIV_FOCUS_DISABLED;
876 else
877#endif
878 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100880 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
883 * Set up the virtual wire mode.
884 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100885 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +0400887 if (!lapic_is_integrated()) /* 82489DX */
888 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +0100889 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +0400892static void __cpuinit lapic_setup_esr(void)
893{
894 unsigned long oldvalue, value, maxlvt;
895 if (lapic_is_integrated() && !esr_disable) {
896 if (esr_disable) {
897 /*
898 * Something untraceable is creating bad interrupts on
899 * secondary quads ... for the moment, just leave the
900 * ESR disabled - we can't do anything useful with the
901 * errors anyway - mbligh
902 */
903 printk(KERN_INFO "Leaving ESR disabled.\n");
904 return;
905 }
906 /* !82489DX */
907 maxlvt = lapic_get_maxlvt();
908 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
909 apic_write(APIC_ESR, 0);
910 oldvalue = apic_read(APIC_ESR);
911
912 /* enables sending errors */
913 value = ERROR_APIC_VECTOR;
914 apic_write(APIC_LVTERR, value);
915 /*
916 * spec says clear errors after enabling vector.
917 */
918 if (maxlvt > 3)
919 apic_write(APIC_ESR, 0);
920 value = apic_read(APIC_ESR);
921 if (value != oldvalue)
922 apic_printk(APIC_VERBOSE, "ESR value before enabling "
923 "vector: 0x%08lx after: 0x%08lx\n",
924 oldvalue, value);
925 } else {
926 printk(KERN_INFO "No ESR for 82489DX.\n");
927 }
928}
929
930
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100931/**
932 * setup_local_APIC - setup the local APIC
933 */
934void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Andi Kleen739f33b2008-01-30 13:30:40 +0100936 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100937 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jack Steinerac23d4e2008-03-28 14:12:16 -0500939 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Andi Kleenfe7414a2006-09-26 10:52:30 +0200942 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /*
945 * Double-check whether this APIC is really registered.
946 * This is meaningless in clustered apic mode, so we skip it.
947 */
948 if (!apic_id_registered())
949 BUG();
950
951 /*
952 * Intel recommends to set DFR, LDR and TPR before enabling
953 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
954 * document number 292116). So here it goes...
955 */
956 init_apic_ldr();
957
958 /*
959 * Set Task Priority to 'accept all'. We never change this
960 * later on.
961 */
962 value = apic_read(APIC_TASKPRI);
963 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100964 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100967 * After a crash, we no longer service the interrupts and a pending
968 * interrupt from previous kernel might still have ISR bit set.
969 *
970 * Most probably by now CPU has serviced that pending interrupt and
971 * it might not have done the ack_APIC_irq() because it thought,
972 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
973 * does not clear the ISR bit and cpu thinks it has already serivced
974 * the interrupt. Hence a vector might get locked. It was noticed
975 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
976 */
977 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
978 value = apic_read(APIC_ISR + i*0x10);
979 for (j = 31; j >= 0; j--) {
980 if (value & (1<<j))
981 ack_APIC_irq();
982 }
983 }
984
985 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Now that we are all set up, enable the APIC
987 */
988 value = apic_read(APIC_SPIV);
989 value &= ~APIC_VECTOR_MASK;
990 /*
991 * Enable APIC
992 */
993 value |= APIC_SPIV_APIC_ENABLED;
994
Andi Kleen3f14c742006-09-26 10:52:29 +0200995 /* We always use processor focus */
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * Set spurious IRQ vector
999 */
1000 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001001 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
1004 * Set up LVT0, LVT1:
1005 *
1006 * set up through-local-APIC on the BP's LINT0. This is not
1007 * strictly necessary in pure symmetric-IO mode, but sometimes
1008 * we delegate interrupts to the 8259A.
1009 */
1010 /*
1011 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1012 */
1013 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +02001014 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001016 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1017 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 } else {
1019 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001020 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1021 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001023 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /*
1026 * only the BP should see the LINT1 NMI signal, obviously.
1027 */
1028 if (!smp_processor_id())
1029 value = APIC_DM_NMI;
1030 else
1031 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +01001032 apic_write(APIC_LVT1, value);
Jack Steinerac23d4e2008-03-28 14:12:16 -05001033 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Andi Kleen739f33b2008-01-30 13:30:40 +01001036void __cpuinit end_local_APIC_setup(void)
1037{
1038 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001039
1040#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001041 {
1042 unsigned int value;
1043 /* Disable the local apic timer */
1044 value = apic_read(APIC_LVTT);
1045 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1046 apic_write(APIC_LVTT, value);
1047 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001048#endif
1049
Don Zickusf2802e72006-09-26 10:52:26 +02001050 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 apic_pm_activate();
1052}
1053
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001054void check_x2apic(void)
1055{
1056 int msr, msr2;
1057
1058 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1059
1060 if (msr & X2APIC_ENABLE) {
1061 printk("x2apic enabled by BIOS, switching to x2apic ops\n");
1062 x2apic_preenabled = x2apic = 1;
1063 apic_ops = &x2apic_ops;
1064 }
1065}
1066
1067void enable_x2apic(void)
1068{
1069 int msr, msr2;
1070
1071 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1072 if (!(msr & X2APIC_ENABLE)) {
1073 printk("Enabling x2apic\n");
1074 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1075 }
1076}
1077
1078void enable_IR_x2apic(void)
1079{
1080#ifdef CONFIG_INTR_REMAP
1081 int ret;
1082 unsigned long flags;
1083
1084 if (!cpu_has_x2apic)
1085 return;
1086
1087 if (!x2apic_preenabled && disable_x2apic) {
1088 printk(KERN_INFO
1089 "Skipped enabling x2apic and Interrupt-remapping "
1090 "because of nox2apic\n");
1091 return;
1092 }
1093
1094 if (x2apic_preenabled && disable_x2apic)
1095 panic("Bios already enabled x2apic, can't enforce nox2apic");
1096
1097 if (!x2apic_preenabled && skip_ioapic_setup) {
1098 printk(KERN_INFO
1099 "Skipped enabling x2apic and Interrupt-remapping "
1100 "because of skipping io-apic setup\n");
1101 return;
1102 }
1103
1104 ret = dmar_table_init();
1105 if (ret) {
1106 printk(KERN_INFO
1107 "dmar_table_init() failed with %d:\n", ret);
1108
1109 if (x2apic_preenabled)
1110 panic("x2apic enabled by bios. But IR enabling failed");
1111 else
1112 printk(KERN_INFO
1113 "Not enabling x2apic,Intr-remapping\n");
1114 return;
1115 }
1116
1117 local_irq_save(flags);
1118 mask_8259A();
1119 save_mask_IO_APIC_setup();
1120
1121 ret = enable_intr_remapping(1);
1122
1123 if (ret && x2apic_preenabled) {
1124 local_irq_restore(flags);
1125 panic("x2apic enabled by bios. But IR enabling failed");
1126 }
1127
1128 if (ret)
1129 goto end;
1130
1131 if (!x2apic) {
1132 x2apic = 1;
1133 apic_ops = &x2apic_ops;
1134 enable_x2apic();
1135 }
1136end:
1137 if (ret)
1138 /*
1139 * IR enabling failed
1140 */
1141 restore_IO_APIC_setup();
1142 else
1143 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1144
1145 unmask_8259A();
1146 local_irq_restore(flags);
1147
1148 if (!ret) {
1149 if (!x2apic_preenabled)
1150 printk(KERN_INFO
1151 "Enabled x2apic and interrupt-remapping\n");
1152 else
1153 printk(KERN_INFO
1154 "Enabled Interrupt-remapping\n");
1155 } else
1156 printk(KERN_ERR
1157 "Failed to enable Interrupt-remapping and x2apic\n");
1158#else
1159 if (!cpu_has_x2apic)
1160 return;
1161
1162 if (x2apic_preenabled)
1163 panic("x2apic enabled prior OS handover,"
1164 " enable CONFIG_INTR_REMAP");
1165
1166 printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1167 " and x2apic\n");
1168#endif
1169
1170 return;
1171}
1172
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001173/*
1174 * Detect and enable local APICs on non-SMP boards.
1175 * Original code written by Keir Fraser.
1176 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1177 * not correctly set up (usually the APIC timer won't work etc.)
1178 */
1179static int __init detect_init_APIC(void)
1180{
1181 if (!cpu_has_apic) {
1182 printk(KERN_INFO "No local APIC present\n");
1183 return -1;
1184 }
1185
1186 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001187 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001188 return 0;
1189}
1190
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001191void __init early_init_lapic_mapping(void)
1192{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001193 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001194
1195 /*
1196 * If no local APIC can be found then go out
1197 * : it means there is no mpatable and MADT
1198 */
1199 if (!smp_found_config)
1200 return;
1201
Thomas Gleixner431ee792008-05-12 15:43:35 +02001202 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001203
Thomas Gleixner431ee792008-05-12 15:43:35 +02001204 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001205 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001206 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001207
1208 /*
1209 * Fetch the APIC ID of the BSP in case we have a
1210 * default configuration (or the MP table is broken).
1211 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001212 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001213}
1214
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001215/**
1216 * init_apic_mappings - initialize APIC mappings
1217 */
1218void __init init_apic_mappings(void)
1219{
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001220 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001221 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001222 return;
1223 }
1224
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001225 /*
1226 * If no local APIC can be found then set up a fake all
1227 * zeroes page to simulate the local APIC and another
1228 * one for the IO-APIC.
1229 */
1230 if (!smp_found_config && detect_init_APIC()) {
1231 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1232 apic_phys = __pa(apic_phys);
1233 } else
1234 apic_phys = mp_lapic_addr;
1235
1236 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1237 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1238 APIC_BASE, apic_phys);
1239
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001240 /*
1241 * Fetch the APIC ID of the BSP in case we have a
1242 * default configuration (or the MP table is broken).
1243 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001244 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001245}
1246
1247/*
1248 * This initializes the IO-APIC and APIC hardware if this is
1249 * a UP kernel.
1250 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001251int apic_version[MAX_APICS];
1252
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001253int __init APIC_init_uniprocessor(void)
1254{
1255 if (disable_apic) {
1256 printk(KERN_INFO "Apic disabled\n");
1257 return -1;
1258 }
1259 if (!cpu_has_apic) {
1260 disable_apic = 1;
1261 printk(KERN_INFO "Apic disabled by BIOS\n");
1262 return -1;
1263 }
1264
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001265 enable_IR_x2apic();
1266 setup_apic_routing();
1267
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001268 verify_local_APIC();
1269
Glauber Costab5841762008-05-28 13:38:28 -03001270 connect_bsp_APIC();
1271
Jack Steinerb6df1b82008-06-19 21:51:05 -05001272 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001273 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001274
1275 setup_local_APIC();
1276
Andi Kleen739f33b2008-01-30 13:30:40 +01001277 /*
1278 * Now enable IO-APICs, actually call clear_IO_APIC
1279 * We need clear_IO_APIC before enabling vector on BP
1280 */
1281 if (!skip_ioapic_setup && nr_ioapics)
1282 enable_IO_APIC();
1283
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001284 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1285 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001286 end_local_APIC_setup();
1287
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001288 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1289 setup_IO_APIC();
1290 else
1291 nr_ioapics = 0;
1292 setup_boot_APIC_clock();
1293 check_nmi_watchdog();
1294 return 0;
1295}
1296
1297/*
1298 * Local APIC interrupts
1299 */
1300
1301/*
1302 * This interrupt should _never_ happen with our APIC/SMP architecture
1303 */
1304asmlinkage void smp_spurious_interrupt(void)
1305{
1306 unsigned int v;
1307 exit_idle();
1308 irq_enter();
1309 /*
1310 * Check if this really is a spurious interrupt and ACK it
1311 * if it is a vectored one. Just in case...
1312 * Spurious interrupts should not be ACKed.
1313 */
1314 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1315 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1316 ack_APIC_irq();
1317
1318 add_pda(irq_spurious_count, 1);
1319 irq_exit();
1320}
1321
1322/*
1323 * This interrupt should never happen with our APIC/SMP architecture
1324 */
1325asmlinkage void smp_error_interrupt(void)
1326{
1327 unsigned int v, v1;
1328
1329 exit_idle();
1330 irq_enter();
1331 /* First tickle the hardware, only then report what went on. -- REW */
1332 v = apic_read(APIC_ESR);
1333 apic_write(APIC_ESR, 0);
1334 v1 = apic_read(APIC_ESR);
1335 ack_APIC_irq();
1336 atomic_inc(&irq_err_count);
1337
1338 /* Here is what the APIC error bits mean:
1339 0: Send CS error
1340 1: Receive CS error
1341 2: Send accept error
1342 3: Receive accept error
1343 4: Reserved
1344 5: Send illegal vector
1345 6: Received illegal vector
1346 7: Illegal register address
1347 */
1348 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1349 smp_processor_id(), v , v1);
1350 irq_exit();
1351}
1352
Glauber Costab5841762008-05-28 13:38:28 -03001353/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001354 * connect_bsp_APIC - attach the APIC to the interrupt system
1355 */
Glauber Costab5841762008-05-28 13:38:28 -03001356void __init connect_bsp_APIC(void)
1357{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001358#ifdef CONFIG_X86_32
1359 if (pic_mode) {
1360 /*
1361 * Do not trust the local APIC being empty at bootup.
1362 */
1363 clear_local_APIC();
1364 /*
1365 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1366 * local APIC to INT and NMI lines.
1367 */
1368 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1369 "enabling APIC mode.\n");
1370 outb(0x70, 0x22);
1371 outb(0x01, 0x23);
1372 }
1373#endif
Glauber Costab5841762008-05-28 13:38:28 -03001374 enable_apic_mode();
1375}
1376
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001377/**
1378 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1379 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1380 *
1381 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1382 * APIC is disabled.
1383 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001384void disconnect_bsp_APIC(int virt_wire_setup)
1385{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001386 unsigned int value;
1387
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001388#ifdef CONFIG_X86_32
1389 if (pic_mode) {
1390 /*
1391 * Put the board back into PIC mode (has an effect only on
1392 * certain older boards). Note that APIC interrupts, including
1393 * IPIs, won't work beyond this point! The only exception are
1394 * INIT IPIs.
1395 */
1396 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1397 "entering PIC mode.\n");
1398 outb(0x70, 0x22);
1399 outb(0x00, 0x23);
1400 return;
1401 }
1402#endif
1403
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001404 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001405
1406 /* For the spurious interrupt use vector F, and enable it */
1407 value = apic_read(APIC_SPIV);
1408 value &= ~APIC_VECTOR_MASK;
1409 value |= APIC_SPIV_APIC_ENABLED;
1410 value |= 0xf;
1411 apic_write(APIC_SPIV, value);
1412
1413 if (!virt_wire_setup) {
1414 /*
1415 * For LVT0 make it edge triggered, active high,
1416 * external and enabled
1417 */
1418 value = apic_read(APIC_LVT0);
1419 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1420 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1421 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1422 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1423 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1424 apic_write(APIC_LVT0, value);
1425 } else {
1426 /* Disable LVT0 */
1427 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1428 }
1429
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001430 /*
1431 * For LVT1 make it edge triggered, active high,
1432 * nmi and enabled
1433 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001434 value = apic_read(APIC_LVT1);
1435 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1436 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1437 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1438 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1439 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1440 apic_write(APIC_LVT1, value);
1441}
1442
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001443void __cpuinit generic_processor_info(int apicid, int version)
1444{
1445 int cpu;
1446 cpumask_t tmp_map;
1447
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001448 /*
1449 * Validate version
1450 */
1451 if (version == 0x0) {
1452 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1453 "fixing up to 0x10. (tell your hw vendor)\n",
1454 version);
1455 version = 0x10;
1456 }
1457 apic_version[apicid] = version;
1458
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001459 if (num_processors >= NR_CPUS) {
1460 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001461 " Processor ignored.\n", NR_CPUS);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001462 return;
1463 }
1464
1465 num_processors++;
1466 cpus_complement(tmp_map, cpu_present_map);
1467 cpu = first_cpu(tmp_map);
1468
1469 physid_set(apicid, phys_cpu_present_map);
1470 if (apicid == boot_cpu_physical_apicid) {
1471 /*
1472 * x86_bios_cpu_apicid is required to have processors listed
1473 * in same order as logical cpu numbers. Hence the first
1474 * entry is BSP, and so on.
1475 */
1476 cpu = 0;
1477 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001478 if (apicid > max_physical_apicid)
1479 max_physical_apicid = apicid;
1480
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001481#ifdef CONFIG_X86_32
1482 /*
1483 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1484 * but we need to work other dependencies like SMP_SUSPEND etc
1485 * before this can be done without some confusion.
1486 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1487 * - Ashok Raj <ashok.raj@intel.com>
1488 */
1489 if (max_physical_apicid >= 8) {
1490 switch (boot_cpu_data.x86_vendor) {
1491 case X86_VENDOR_INTEL:
1492 if (!APIC_XAPIC(version)) {
1493 def_to_bigsmp = 0;
1494 break;
1495 }
1496 /* If P4 and above fall through */
1497 case X86_VENDOR_AMD:
1498 def_to_bigsmp = 1;
1499 }
1500 }
1501#endif
1502
1503#if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001504 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001505 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1506 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1507 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001508
1509 cpu_to_apicid[cpu] = apicid;
1510 bios_cpu_apicid[cpu] = apicid;
1511 } else {
1512 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1513 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1514 }
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001515#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001516
1517 cpu_set(cpu, cpu_possible_map);
1518 cpu_set(cpu, cpu_present_map);
1519}
1520
Suresh Siddha0c81c742008-07-10 11:16:48 -07001521int hard_smp_processor_id(void)
1522{
1523 return read_apic_id();
1524}
1525
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001526/*
1527 * Power management
1528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529#ifdef CONFIG_PM
1530
1531static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001532 /*
1533 * 'active' is true if the local APIC was enabled by us and
1534 * not the BIOS; this signifies that we are also responsible
1535 * for disabling it before entering apm/acpi suspend
1536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int active;
1538 /* r/w apic fields */
1539 unsigned int apic_id;
1540 unsigned int apic_taskpri;
1541 unsigned int apic_ldr;
1542 unsigned int apic_dfr;
1543 unsigned int apic_spiv;
1544 unsigned int apic_lvtt;
1545 unsigned int apic_lvtpc;
1546 unsigned int apic_lvt0;
1547 unsigned int apic_lvt1;
1548 unsigned int apic_lvterr;
1549 unsigned int apic_tmict;
1550 unsigned int apic_tdcr;
1551 unsigned int apic_thmr;
1552} apic_pm_state;
1553
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001554static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001557 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 if (!apic_pm_state.active)
1560 return 0;
1561
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001562 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001563
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001564 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1566 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1567 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1568 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1569 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001570 if (maxlvt >= 4)
1571 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1573 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1574 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1575 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1576 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001577#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001578 if (maxlvt >= 5)
1579 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1580#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001581
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001582 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 disable_local_APIC();
1584 local_irq_restore(flags);
1585 return 0;
1586}
1587
1588static int lapic_resume(struct sys_device *dev)
1589{
1590 unsigned int l, h;
1591 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001592 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (!apic_pm_state.active)
1595 return 0;
1596
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001597 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001600
1601#ifdef CONFIG_X86_64
1602 if (x2apic)
1603 enable_x2apic();
1604 else
1605#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07001606 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001607 /*
1608 * Make sure the APICBASE points to the right address
1609 *
1610 * FIXME! This will be wrong if we ever support suspend on
1611 * SMP! We'll need to do this as part of the CPU restore!
1612 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001613 rdmsr(MSR_IA32_APICBASE, l, h);
1614 l &= ~MSR_IA32_APICBASE_BASE;
1615 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1616 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07001617 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1620 apic_write(APIC_ID, apic_pm_state.apic_id);
1621 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1622 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1623 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1624 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1625 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1626 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001627#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001628 if (maxlvt >= 5)
1629 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1630#endif
1631 if (maxlvt >= 4)
1632 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1634 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1635 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1636 apic_write(APIC_ESR, 0);
1637 apic_read(APIC_ESR);
1638 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1639 apic_write(APIC_ESR, 0);
1640 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645}
1646
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001647/*
1648 * This device has no shutdown method - fully functioning local APICs
1649 * are needed on every CPU up until machine_halt/restart/poweroff.
1650 */
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001653 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .resume = lapic_resume,
1655 .suspend = lapic_suspend,
1656};
1657
1658static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001659 .id = 0,
1660 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661};
1662
Ashok Raje6982c62005-06-25 14:54:58 -07001663static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 apic_pm_state.active = 1;
1666}
1667
1668static int __init init_lapic_sysfs(void)
1669{
1670 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (!cpu_has_apic)
1673 return 0;
1674 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 error = sysdev_class_register(&lapic_sysclass);
1677 if (!error)
1678 error = sysdev_register(&device_lapic);
1679 return error;
1680}
1681device_initcall(init_lapic_sysfs);
1682
1683#else /* CONFIG_PM */
1684
1685static void apic_pm_activate(void) { }
1686
1687#endif /* CONFIG_PM */
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001690 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 *
1692 * Thus far, the major user of this is IBM's Summit2 series:
1693 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001694 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 * multi-chassis. Use available data to take a good guess.
1696 * If in doubt, go HPET.
1697 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001698__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 int i, clusters, zeros;
1701 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08001702 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1704
Yinghai Lu322850a2008-02-23 21:48:42 -08001705 /*
1706 * there is not this kind of box with AMD CPU yet.
1707 * Some AMD box with quadcore cpu and 8 sockets apicid
1708 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08001709 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08001710 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001711 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08001712 return 0;
1713
Mike Travis23ca4bb2008-05-12 21:21:12 +02001714 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07001715 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 for (i = 0; i < NR_CPUS; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001718 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01001719 if (bios_cpu_apicid) {
1720 id = bios_cpu_apicid[i];
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001721 }
1722 else if (i < nr_cpu_ids) {
1723 if (cpu_present(i))
1724 id = per_cpu(x86_bios_cpu_apicid, i);
1725 else
1726 continue;
1727 }
1728 else
1729 break;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (id != BAD_APICID)
1732 __set_bit(APIC_CLUSTERID(id), clustermap);
1733 }
1734
1735 /* Problem: Partially populated chassis may not have CPUs in some of
1736 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01001737 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1738 * Since clusters are allocated sequentially, count zeros only if
1739 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
1741 clusters = 0;
1742 zeros = 0;
1743 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1744 if (test_bit(i, clustermap)) {
1745 clusters += 1 + zeros;
1746 zeros = 0;
1747 } else
1748 ++zeros;
1749 }
1750
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001751 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1752 * not guaranteed to be synced between boards
1753 */
1754 if (is_vsmp_box() && clusters > 1)
1755 return 1;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001758 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 * May have to revisit this when multi-core + hyperthreaded CPUs come
1760 * out, but AFAIK this will work even for them.
1761 */
1762 return (clusters > 2);
1763}
1764
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001765static __init int setup_nox2apic(char *str)
1766{
1767 disable_x2apic = 1;
1768 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_X2APIC);
1769 return 0;
1770}
1771early_param("nox2apic", setup_nox2apic);
1772
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001775 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001777static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001778{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07001780 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001781 return 0;
1782}
1783early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001785/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001786static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001787{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04001788 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001789}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001790early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001792static int __init parse_lapic_timer_c2_ok(char *arg)
1793{
1794 local_apic_timer_c2_ok = 1;
1795 return 0;
1796}
1797early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1798
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001799static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001802 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001803}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001804early_param("noapictimer", parse_disable_apic_timer);
1805
1806static int __init parse_nolapic_timer(char *arg)
1807{
1808 disable_apic_timer = 1;
1809 return 0;
1810}
1811early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01001812
Andi Kleen0c3749c2006-02-03 21:51:41 +01001813static __init int setup_apicpmtimer(char *s)
1814{
1815 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001816 notsc_setup(NULL);
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001817 return 0;
Andi Kleen0c3749c2006-02-03 21:51:41 +01001818}
1819__setup("apicpmtimer", setup_apicpmtimer);
1820
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04001821static int __init apic_set_verbosity(char *arg)
1822{
1823 if (!arg) {
1824#ifdef CONFIG_X86_64
1825 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04001826 return 0;
1827#endif
1828 return -EINVAL;
1829 }
1830
1831 if (strcmp("debug", arg) == 0)
1832 apic_verbosity = APIC_DEBUG;
1833 else if (strcmp("verbose", arg) == 0)
1834 apic_verbosity = APIC_VERBOSE;
1835 else {
1836 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1837 " use apic=verbose or apic=debug\n", arg);
1838 return -EINVAL;
1839 }
1840
1841 return 0;
1842}
1843early_param("apic", apic_set_verbosity);
1844
Yinghai Lu1e934dd2008-02-22 13:37:26 -08001845static int __init lapic_insert_resource(void)
1846{
1847 if (!apic_phys)
1848 return -1;
1849
1850 /* Put local APIC into the resource map. */
1851 lapic_resource.start = apic_phys;
1852 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1853 insert_resource(&iomem_resource, &lapic_resource);
1854
1855 return 0;
1856}
1857
1858/*
1859 * need call insert after e820_reserve_resources()
1860 * that is using request_resource
1861 */
1862late_initcall(lapic_insert_resource);