blob: 77c5e5eb820eec6910449a5831e4ac13cc62c77f [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>
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010036#include <asm/hpet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/pgalloc.h>
Andi Kleen75152112005-05-16 21:53:34 -070038#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010039#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010040#include <asm/proto.h>
41#include <asm/timex.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020042#include <asm/apic.h>
Suresh Siddha6e1cb382008-07-10 11:16:58 -070043#include <asm/i8259.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Glauber Costa5af55732008-03-25 13:28:56 -030045#include <mach_ipi.h>
Glauber Costadd46e3c2008-03-25 18:10:46 -030046#include <mach_apic.h>
Glauber Costa5af55732008-03-25 13:28:56 -030047
Thomas Gleixneraa276e12008-06-09 19:15:00 +020048static int disable_apic_timer __cpuinitdata;
Chris Wrightbc1d99c2007-10-12 23:04:23 +020049static int apic_calibrate_pmtmr __initdata;
Thomas Gleixner0e078e22008-01-30 13:30:20 +010050int disable_apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -070051int disable_x2apic;
Suresh Siddha89027d32008-07-10 11:16:56 -070052int x2apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Suresh Siddha6e1cb382008-07-10 11:16:58 -070054/* x2apic enabled before OS handover */
55int x2apic_preenabled;
56
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010057/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -070058int local_apic_timer_c2_ok;
59EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
60
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010061/*
62 * Debug level, exported for io_apic.c
63 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +010064unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +010065
Alexey Starikovskiybab4b272008-05-19 19:47:03 +040066/* Have we found an MP table */
67int smp_found_config;
68
Aaron Durbin39928722006-12-07 02:14:01 +010069static struct resource lapic_resource = {
70 .name = "Local APIC",
71 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
72};
73
Thomas Gleixnerd03030e2007-10-12 23:04:06 +020074static unsigned int calibration_result;
75
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020076static int lapic_next_event(unsigned long delta,
77 struct clock_event_device *evt);
78static void lapic_timer_setup(enum clock_event_mode mode,
79 struct clock_event_device *evt);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020080static void lapic_timer_broadcast(cpumask_t mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +010081static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +020082
83static struct clock_event_device lapic_clockevent = {
84 .name = "lapic",
85 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
86 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
87 .shift = 32,
88 .set_mode = lapic_timer_setup,
89 .set_next_event = lapic_next_event,
90 .broadcast = lapic_timer_broadcast,
91 .rating = 100,
92 .irq = -1,
93};
94static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
95
Andi Kleend3432892008-01-30 13:33:17 +010096static unsigned long apic_phys;
97
Alexey Starikovskiy3f530702008-03-27 23:55:47 +030098unsigned long mp_lapic_addr;
99
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +0300100unsigned int __cpuinitdata maxcpus = NR_CPUS;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100101/*
102 * Get the LAPIC version
103 */
104static inline int lapic_get_version(void)
105{
106 return GET_APIC_VERSION(apic_read(APIC_LVR));
107}
108
109/*
110 * Check, if the APIC is integrated or a seperate chip
111 */
112static inline int lapic_is_integrated(void)
113{
114 return 1;
115}
116
117/*
118 * Check, whether this is a modern or a first generation APIC
119 */
120static int modern_apic(void)
121{
122 /* AMD systems use old APIC versions, so check the CPU */
123 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
124 boot_cpu_data.x86 >= 0xf)
125 return 1;
126 return lapic_get_version() >= 0x14;
127}
128
Suresh Siddha1b374e42008-07-10 11:16:49 -0700129void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100130{
131 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
132 cpu_relax();
133}
134
Suresh Siddha1b374e42008-07-10 11:16:49 -0700135u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100136{
137 u32 send_status;
138 int timeout;
139
140 timeout = 0;
141 do {
142 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
143 if (!send_status)
144 break;
145 udelay(100);
146 } while (timeout++ < 1000);
147
148 return send_status;
149}
150
Suresh Siddha1b374e42008-07-10 11:16:49 -0700151void xapic_icr_write(u32 low, u32 id)
152{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200153 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700154 apic_write(APIC_ICR, low);
155}
156
157u64 xapic_icr_read(void)
158{
159 u32 icr1, icr2;
160
161 icr2 = apic_read(APIC_ICR2);
162 icr1 = apic_read(APIC_ICR);
163
164 return (icr1 | ((u64)icr2 << 32));
165}
166
167static struct apic_ops xapic_ops = {
168 .read = native_apic_mem_read,
169 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700170 .icr_read = xapic_icr_read,
171 .icr_write = xapic_icr_write,
172 .wait_icr_idle = xapic_wait_icr_idle,
173 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
174};
175
176struct apic_ops __read_mostly *apic_ops = &xapic_ops;
177
178EXPORT_SYMBOL_GPL(apic_ops);
179
Suresh Siddha13c88fb2008-07-10 11:16:52 -0700180static void x2apic_wait_icr_idle(void)
181{
182 /* no need to wait for icr idle in x2apic */
183 return;
184}
185
186static u32 safe_x2apic_wait_icr_idle(void)
187{
188 /* no need to wait for icr idle in x2apic */
189 return 0;
190}
191
192void x2apic_icr_write(u32 low, u32 id)
193{
194 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
195}
196
197u64 x2apic_icr_read(void)
198{
199 unsigned long val;
200
201 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
202 return val;
203}
204
205static struct apic_ops x2apic_ops = {
206 .read = native_apic_msr_read,
207 .write = native_apic_msr_write,
Suresh Siddha13c88fb2008-07-10 11:16:52 -0700208 .icr_read = x2apic_icr_read,
209 .icr_write = x2apic_icr_write,
210 .wait_icr_idle = x2apic_wait_icr_idle,
211 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
212};
213
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100214/**
215 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
216 */
Jan Beuliche9427102008-01-30 13:31:24 +0100217void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100218{
219 unsigned int v;
220
221 /* unmask and set to NMI */
222 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200223
224 /* Level triggered for 82489DX (32bit mode) */
225 if (!lapic_is_integrated())
226 v |= APIC_LVT_LEVEL_TRIGGER;
227
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100228 apic_write(APIC_LVT0, v);
229}
230
231/**
232 * lapic_get_maxlvt - get the maximum number of local vector table entries
233 */
234int lapic_get_maxlvt(void)
235{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200236 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100237
238 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200239 /*
240 * - we always have APIC integrated on 64bit mode
241 * - 82489DXs do not report # of LVT entries
242 */
243 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100244}
245
246/*
247 * This function sets up the local APIC timer, with a timeout of
248 * 'clocks' APIC bus clock. During calibration we actually call
249 * this function twice on the boot CPU, once with a bogus timeout
250 * value, second time for real. The other (noncalibrating) CPUs
251 * call this function only once, with the real, calibrated value.
252 *
253 * We do reads before writes even if unnecessary, to get around the
254 * P5 APIC double write bug.
255 */
256
257static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
258{
259 unsigned int lvtt_value, tmp_value;
260
261 lvtt_value = LOCAL_TIMER_VECTOR;
262 if (!oneshot)
263 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
264 if (!irqen)
265 lvtt_value |= APIC_LVT_MASKED;
266
267 apic_write(APIC_LVTT, lvtt_value);
268
269 /*
270 * Divide PICLK by 16
271 */
272 tmp_value = apic_read(APIC_TDCR);
273 apic_write(APIC_TDCR, (tmp_value
274 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
275 | APIC_TDR_DIV_16);
276
277 if (!oneshot)
278 apic_write(APIC_TMICT, clocks);
279}
280
281/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100282 * Setup extended LVT, AMD specific (K8, family 10h)
283 *
284 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
285 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100286 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100287
288#define APIC_EILVT_LVTOFF_MCE 0
289#define APIC_EILVT_LVTOFF_IBS 1
290
291static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100292{
Robert Richter7b83dae2008-01-30 13:30:40 +0100293 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100294 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
295
296 apic_write(reg, v);
297}
298
Robert Richter7b83dae2008-01-30 13:30:40 +0100299u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
300{
301 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
302 return APIC_EILVT_LVTOFF_MCE;
303}
304
305u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
306{
307 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
308 return APIC_EILVT_LVTOFF_IBS;
309}
310
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100311/*
312 * Program the next event, relative to now
313 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200314static int lapic_next_event(unsigned long delta,
315 struct clock_event_device *evt)
316{
317 apic_write(APIC_TMICT, delta);
318 return 0;
319}
320
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100321/*
322 * Setup the lapic timer in periodic or oneshot mode
323 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200324static void lapic_timer_setup(enum clock_event_mode mode,
325 struct clock_event_device *evt)
326{
327 unsigned long flags;
328 unsigned int v;
329
330 /* Lapic used as dummy for broadcast ? */
331 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
332 return;
333
334 local_irq_save(flags);
335
336 switch (mode) {
337 case CLOCK_EVT_MODE_PERIODIC:
338 case CLOCK_EVT_MODE_ONESHOT:
339 __setup_APIC_LVTT(calibration_result,
340 mode != CLOCK_EVT_MODE_PERIODIC, 1);
341 break;
342 case CLOCK_EVT_MODE_UNUSED:
343 case CLOCK_EVT_MODE_SHUTDOWN:
344 v = apic_read(APIC_LVTT);
345 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
346 apic_write(APIC_LVTT, v);
347 break;
348 case CLOCK_EVT_MODE_RESUME:
349 /* Nothing to do here */
350 break;
351 }
352
353 local_irq_restore(flags);
354}
355
356/*
357 * Local APIC timer broadcast function
358 */
359static void lapic_timer_broadcast(cpumask_t mask)
360{
361#ifdef CONFIG_SMP
362 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
363#endif
364}
365
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100366/*
367 * Setup the local APIC timer for this CPU. Copy the initilized values
368 * of the boot CPU and register the clock event in the framework.
369 */
370static void setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200371{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100372 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
373
374 memcpy(levt, &lapic_clockevent, sizeof(*levt));
375 levt->cpumask = cpumask_of_cpu(smp_processor_id());
376
377 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200378}
379
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100380/*
381 * In this function we calibrate APIC bus clocks to the external
382 * timer. Unfortunately we cannot use jiffies and the timer irq
383 * to calibrate, since some later bootup code depends on getting
384 * the first irq? Ugh.
385 *
386 * We want to do the calibration only once since we
387 * want to have local timer irqs syncron. CPUs connected
388 * by the same APIC bus have the very same bus frequency.
389 * And we want to have irqs off anyways, no accidental
390 * APIC irq that way.
391 */
392
393#define TICK_COUNT 100000000
394
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400395static int __init calibrate_APIC_clock(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200396{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100397 unsigned apic, apic_start;
398 unsigned long tsc, tsc_start;
399 int result;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200400
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100401 local_irq_disable();
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200402
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100403 /*
404 * Put whatever arbitrary (but long enough) timeout
405 * value into the APIC clock, we just want to get the
406 * counter running for calibration.
407 *
408 * No interrupt enable !
409 */
410 __setup_APIC_LVTT(250000000, 0, 0);
411
412 apic_start = apic_read(APIC_TMCCT);
413#ifdef CONFIG_X86_PM_TIMER
414 if (apic_calibrate_pmtmr && pmtmr_ioport) {
415 pmtimer_wait(5000); /* 5ms wait */
416 apic = apic_read(APIC_TMCCT);
417 result = (apic_start - apic) * 1000L / 5;
418 } else
419#endif
420 {
421 rdtscll(tsc_start);
422
423 do {
424 apic = apic_read(APIC_TMCCT);
425 rdtscll(tsc);
426 } while ((tsc - tsc_start) < TICK_COUNT &&
427 (apic_start - apic) < TICK_COUNT);
428
429 result = (apic_start - apic) * 1000L * tsc_khz /
430 (tsc - tsc_start);
431 }
432
433 local_irq_enable();
434
435 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
436
437 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
438 result / 1000 / 1000, result / 1000 % 1000);
439
440 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900441 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
442 lapic_clockevent.shift);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100443 lapic_clockevent.max_delta_ns =
444 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
445 lapic_clockevent.min_delta_ns =
446 clockevent_delta2ns(0xF, &lapic_clockevent);
447
448 calibration_result = result / HZ;
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400449
450 /*
451 * Do a sanity check on the APIC calibration result
452 */
453 if (calibration_result < (1000000 / HZ)) {
454 printk(KERN_WARNING
455 "APIC frequency too slow, disabling apic timer\n");
456 return -1;
457 }
458
459 return 0;
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200460}
461
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100462/*
463 * Setup the boot APIC
464 *
465 * Calibrate and verify the result.
466 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100467void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100469 /*
470 * The local apic timer can be disabled via the kernel commandline.
471 * Register the lapic timer as a dummy clock event source on SMP
472 * systems, so the broadcast mechanism is used. On UP systems simply
473 * ignore it.
474 */
475 if (disable_apic_timer) {
476 printk(KERN_INFO "Disabling APIC timer\n");
477 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100478 if (num_possible_cpus() > 1) {
479 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100480 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100481 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100482 return;
483 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200484
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100485 printk(KERN_INFO "Using local APIC timer interrupts.\n");
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400486 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100487 /* No broadcast on UP ! */
488 if (num_possible_cpus() > 1)
489 setup_APIC_timer();
490 return;
491 }
492
493 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100494 * If nmi_watchdog is set to IO_APIC, we need the
495 * PIT/HPET going. Otherwise register lapic as a dummy
496 * device.
497 */
498 if (nmi_watchdog != NMI_IO_APIC)
499 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
500 else
501 printk(KERN_WARNING "APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200502 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100503
504 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100507void __cpuinit setup_secondary_APIC_clock(void)
508{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100509 setup_APIC_timer();
510}
511
512/*
513 * The guts of the apic timer interrupt
514 */
515static void local_apic_timer_interrupt(void)
516{
517 int cpu = smp_processor_id();
518 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
519
520 /*
521 * Normally we should not be here till LAPIC has been initialized but
522 * in some cases like kdump, its possible that there is a pending LAPIC
523 * timer interrupt from previous kernel's context and is delivered in
524 * new kernel the moment interrupts are enabled.
525 *
526 * Interrupts are enabled early and LAPIC is setup much later, hence
527 * its possible that when we get here evt->event_handler is NULL.
528 * Check for event_handler being NULL and discard the interrupt as
529 * spurious.
530 */
531 if (!evt->event_handler) {
532 printk(KERN_WARNING
533 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
534 /* Switch it off */
535 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
536 return;
537 }
538
539 /*
540 * the NMI deadlock-detector uses this.
541 */
542 add_pda(apic_timer_irqs, 1);
543
544 evt->event_handler(evt);
545}
546
547/*
548 * Local APIC timer interrupt. This is the most natural way for doing
549 * local interrupts, but local timer interrupts can be emulated by
550 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
551 *
552 * [ if a single-CPU system runs an SMP kernel then we call the local
553 * interrupt as well. Thus we cannot inline the local irq ... ]
554 */
555void smp_apic_timer_interrupt(struct pt_regs *regs)
556{
557 struct pt_regs *old_regs = set_irq_regs(regs);
558
559 /*
560 * NOTE! We'd better ACK the irq immediately,
561 * because timer handling can be slow.
562 */
563 ack_APIC_irq();
564 /*
565 * update_process_times() expects us to have done irq_enter().
566 * Besides, if we don't timer interrupts ignore the global
567 * interrupt lock, which is the WrongThing (tm) to do.
568 */
569 exit_idle();
570 irq_enter();
571 local_apic_timer_interrupt();
572 irq_exit();
573 set_irq_regs(old_regs);
574}
575
576int setup_profiling_timer(unsigned int multiplier)
577{
578 return -EINVAL;
579}
580
581
582/*
583 * Local APIC start and shutdown
584 */
585
586/**
587 * clear_local_APIC - shutdown the local APIC
588 *
589 * This is called, when a CPU is disabled and before rebooting, so the state of
590 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
591 * leftovers during boot.
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593void clear_local_APIC(void)
594{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400595 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100596 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Andi Kleend3432892008-01-30 13:33:17 +0100598 /* APIC hasn't been mapped yet */
599 if (!apic_phys)
600 return;
601
602 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200604 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * if the vector is zero. Mask LVTERR first to prevent this.
606 */
607 if (maxlvt >= 3) {
608 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100609 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611 /*
612 * Careful: we have to set masks only first to deassert
613 * any level-triggered sources.
614 */
615 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100616 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100618 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100620 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (maxlvt >= 4) {
622 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100623 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
626 /*
627 * Clean APIC state for other OSs:
628 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100629 apic_write(APIC_LVTT, APIC_LVT_MASKED);
630 apic_write(APIC_LVT0, APIC_LVT_MASKED);
631 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100633 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100635 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200636 apic_write(APIC_ESR, 0);
637 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100640/**
641 * disable_local_APIC - clear and disable the local APIC
642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643void disable_local_APIC(void)
644{
645 unsigned int value;
646
647 clear_local_APIC();
648
649 /*
650 * Disable APIC (implies clearing of registers
651 * for 82489DX!).
652 */
653 value = apic_read(APIC_SPIV);
654 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100655 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700658void lapic_shutdown(void)
659{
660 unsigned long flags;
661
662 if (!cpu_has_apic)
663 return;
664
665 local_irq_save(flags);
666
667 disable_local_APIC();
668
669 local_irq_restore(flags);
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/*
673 * This is to verify that we're looking at a real local APIC.
674 * Check these against your board if the CPUs aren't getting
675 * started for no apparent reason.
676 */
677int __init verify_local_APIC(void)
678{
679 unsigned int reg0, reg1;
680
681 /*
682 * The version register is read-only in a real APIC.
683 */
684 reg0 = apic_read(APIC_LVR);
685 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
686 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
687 reg1 = apic_read(APIC_LVR);
688 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
689
690 /*
691 * The two version reads above should print the same
692 * numbers. If the second one is different, then we
693 * poke at a non-APIC.
694 */
695 if (reg1 != reg0)
696 return 0;
697
698 /*
699 * Check if the version looks reasonably.
700 */
701 reg1 = GET_APIC_VERSION(reg0);
702 if (reg1 == 0x00 || reg1 == 0xff)
703 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100704 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (reg1 < 0x02 || reg1 == 0xff)
706 return 0;
707
708 /*
709 * The ID register is read/write in a real APIC.
710 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700711 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
713 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700714 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
716 apic_write(APIC_ID, reg0);
717 if (reg1 != (reg0 ^ APIC_ID_MASK))
718 return 0;
719
720 /*
721 * The next two are just to see if we have sane values.
722 * They're only really relevant if we're in Virtual Wire
723 * compatibility mode, but most boxes are anymore.
724 */
725 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100726 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 reg1 = apic_read(APIC_LVT1);
728 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
729
730 return 1;
731}
732
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100733/**
734 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736void __init sync_Arb_IDs(void)
737{
738 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100739 if (modern_apic())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return;
741
742 /*
743 * Wait for idle.
744 */
745 apic_wait_icr_idle();
746
747 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100748 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 | APIC_DM_INIT);
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * An initial setup of the virtual wire mode.
754 */
755void __init init_bsp_APIC(void)
756{
Andi Kleen11a8e772006-01-11 22:46:51 +0100757 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * Don't do the setup now if we have a SMP BIOS as the
761 * through-I/O-APIC virtual wire mode might be active.
762 */
763 if (smp_found_config || !cpu_has_apic)
764 return;
765
766 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * Do not trust the local APIC being empty at bootup.
770 */
771 clear_local_APIC();
772
773 /*
774 * Enable APIC.
775 */
776 value = apic_read(APIC_SPIV);
777 value &= ~APIC_VECTOR_MASK;
778 value |= APIC_SPIV_APIC_ENABLED;
779 value |= APIC_SPIV_FOCUS_DISABLED;
780 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100781 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /*
784 * Set up the virtual wire mode.
785 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100786 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100788 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100791/**
792 * setup_local_APIC - setup the local APIC
793 */
794void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Andi Kleen739f33b2008-01-30 13:30:40 +0100796 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100797 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jack Steinerac23d4e2008-03-28 14:12:16 -0500799 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Andi Kleenfe7414a2006-09-26 10:52:30 +0200802 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /*
805 * Double-check whether this APIC is really registered.
806 * This is meaningless in clustered apic mode, so we skip it.
807 */
808 if (!apic_id_registered())
809 BUG();
810
811 /*
812 * Intel recommends to set DFR, LDR and TPR before enabling
813 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
814 * document number 292116). So here it goes...
815 */
816 init_apic_ldr();
817
818 /*
819 * Set Task Priority to 'accept all'. We never change this
820 * later on.
821 */
822 value = apic_read(APIC_TASKPRI);
823 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100824 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100827 * After a crash, we no longer service the interrupts and a pending
828 * interrupt from previous kernel might still have ISR bit set.
829 *
830 * Most probably by now CPU has serviced that pending interrupt and
831 * it might not have done the ack_APIC_irq() because it thought,
832 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
833 * does not clear the ISR bit and cpu thinks it has already serivced
834 * the interrupt. Hence a vector might get locked. It was noticed
835 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
836 */
837 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
838 value = apic_read(APIC_ISR + i*0x10);
839 for (j = 31; j >= 0; j--) {
840 if (value & (1<<j))
841 ack_APIC_irq();
842 }
843 }
844
845 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * Now that we are all set up, enable the APIC
847 */
848 value = apic_read(APIC_SPIV);
849 value &= ~APIC_VECTOR_MASK;
850 /*
851 * Enable APIC
852 */
853 value |= APIC_SPIV_APIC_ENABLED;
854
Andi Kleen3f14c742006-09-26 10:52:29 +0200855 /* We always use processor focus */
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * Set spurious IRQ vector
859 */
860 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100861 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /*
864 * Set up LVT0, LVT1:
865 *
866 * set up through-local-APIC on the BP's LINT0. This is not
867 * strictly necessary in pure symmetric-IO mode, but sometimes
868 * we delegate interrupts to the 8259A.
869 */
870 /*
871 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
872 */
873 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200874 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200876 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
877 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 } else {
879 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200880 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
881 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100883 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /*
886 * only the BP should see the LINT1 NMI signal, obviously.
887 */
888 if (!smp_processor_id())
889 value = APIC_DM_NMI;
890 else
891 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100892 apic_write(APIC_LVT1, value);
Jack Steinerac23d4e2008-03-28 14:12:16 -0500893 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +0100894}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Ingo Molnara4928cf2008-04-23 13:20:56 +0200896static void __cpuinit lapic_setup_esr(void)
Andi Kleen739f33b2008-01-30 13:30:40 +0100897{
898 unsigned maxlvt = lapic_get_maxlvt();
899
900 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
Yinghai Lu1c695242008-01-30 13:30:39 +0100901 /*
Andi Kleen739f33b2008-01-30 13:30:40 +0100902 * spec says clear errors after enabling vector.
Yinghai Lu1c695242008-01-30 13:30:39 +0100903 */
Andi Kleen739f33b2008-01-30 13:30:40 +0100904 if (maxlvt > 3)
905 apic_write(APIC_ESR, 0);
906}
Yinghai Lu1c695242008-01-30 13:30:39 +0100907
Andi Kleen739f33b2008-01-30 13:30:40 +0100908void __cpuinit end_local_APIC_setup(void)
909{
910 lapic_setup_esr();
Don Zickusf2802e72006-09-26 10:52:26 +0200911 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 apic_pm_activate();
913}
914
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700915void check_x2apic(void)
916{
917 int msr, msr2;
918
919 rdmsr(MSR_IA32_APICBASE, msr, msr2);
920
921 if (msr & X2APIC_ENABLE) {
922 printk("x2apic enabled by BIOS, switching to x2apic ops\n");
923 x2apic_preenabled = x2apic = 1;
924 apic_ops = &x2apic_ops;
925 }
926}
927
928void enable_x2apic(void)
929{
930 int msr, msr2;
931
932 rdmsr(MSR_IA32_APICBASE, msr, msr2);
933 if (!(msr & X2APIC_ENABLE)) {
934 printk("Enabling x2apic\n");
935 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
936 }
937}
938
939void enable_IR_x2apic(void)
940{
941#ifdef CONFIG_INTR_REMAP
942 int ret;
943 unsigned long flags;
944
945 if (!cpu_has_x2apic)
946 return;
947
948 if (!x2apic_preenabled && disable_x2apic) {
949 printk(KERN_INFO
950 "Skipped enabling x2apic and Interrupt-remapping "
951 "because of nox2apic\n");
952 return;
953 }
954
955 if (x2apic_preenabled && disable_x2apic)
956 panic("Bios already enabled x2apic, can't enforce nox2apic");
957
958 if (!x2apic_preenabled && skip_ioapic_setup) {
959 printk(KERN_INFO
960 "Skipped enabling x2apic and Interrupt-remapping "
961 "because of skipping io-apic setup\n");
962 return;
963 }
964
965 ret = dmar_table_init();
966 if (ret) {
967 printk(KERN_INFO
968 "dmar_table_init() failed with %d:\n", ret);
969
970 if (x2apic_preenabled)
971 panic("x2apic enabled by bios. But IR enabling failed");
972 else
973 printk(KERN_INFO
974 "Not enabling x2apic,Intr-remapping\n");
975 return;
976 }
977
978 local_irq_save(flags);
979 mask_8259A();
980 save_mask_IO_APIC_setup();
981
982 ret = enable_intr_remapping(1);
983
984 if (ret && x2apic_preenabled) {
985 local_irq_restore(flags);
986 panic("x2apic enabled by bios. But IR enabling failed");
987 }
988
989 if (ret)
990 goto end;
991
992 if (!x2apic) {
993 x2apic = 1;
994 apic_ops = &x2apic_ops;
995 enable_x2apic();
996 }
997end:
998 if (ret)
999 /*
1000 * IR enabling failed
1001 */
1002 restore_IO_APIC_setup();
1003 else
1004 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1005
1006 unmask_8259A();
1007 local_irq_restore(flags);
1008
1009 if (!ret) {
1010 if (!x2apic_preenabled)
1011 printk(KERN_INFO
1012 "Enabled x2apic and interrupt-remapping\n");
1013 else
1014 printk(KERN_INFO
1015 "Enabled Interrupt-remapping\n");
1016 } else
1017 printk(KERN_ERR
1018 "Failed to enable Interrupt-remapping and x2apic\n");
1019#else
1020 if (!cpu_has_x2apic)
1021 return;
1022
1023 if (x2apic_preenabled)
1024 panic("x2apic enabled prior OS handover,"
1025 " enable CONFIG_INTR_REMAP");
1026
1027 printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1028 " and x2apic\n");
1029#endif
1030
1031 return;
1032}
1033
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001034/*
1035 * Detect and enable local APICs on non-SMP boards.
1036 * Original code written by Keir Fraser.
1037 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1038 * not correctly set up (usually the APIC timer won't work etc.)
1039 */
1040static int __init detect_init_APIC(void)
1041{
1042 if (!cpu_has_apic) {
1043 printk(KERN_INFO "No local APIC present\n");
1044 return -1;
1045 }
1046
1047 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001048 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001049 return 0;
1050}
1051
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001052void __init early_init_lapic_mapping(void)
1053{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001054 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001055
1056 /*
1057 * If no local APIC can be found then go out
1058 * : it means there is no mpatable and MADT
1059 */
1060 if (!smp_found_config)
1061 return;
1062
Thomas Gleixner431ee792008-05-12 15:43:35 +02001063 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001064
Thomas Gleixner431ee792008-05-12 15:43:35 +02001065 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001066 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001067 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001068
1069 /*
1070 * Fetch the APIC ID of the BSP in case we have a
1071 * default configuration (or the MP table is broken).
1072 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001073 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001074}
1075
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001076/**
1077 * init_apic_mappings - initialize APIC mappings
1078 */
1079void __init init_apic_mappings(void)
1080{
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001081 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001082 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001083 return;
1084 }
1085
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001086 /*
1087 * If no local APIC can be found then set up a fake all
1088 * zeroes page to simulate the local APIC and another
1089 * one for the IO-APIC.
1090 */
1091 if (!smp_found_config && detect_init_APIC()) {
1092 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1093 apic_phys = __pa(apic_phys);
1094 } else
1095 apic_phys = mp_lapic_addr;
1096
1097 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1098 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1099 APIC_BASE, apic_phys);
1100
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001101 /*
1102 * Fetch the APIC ID of the BSP in case we have a
1103 * default configuration (or the MP table is broken).
1104 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001105 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001106}
1107
1108/*
1109 * This initializes the IO-APIC and APIC hardware if this is
1110 * a UP kernel.
1111 */
1112int __init APIC_init_uniprocessor(void)
1113{
1114 if (disable_apic) {
1115 printk(KERN_INFO "Apic disabled\n");
1116 return -1;
1117 }
1118 if (!cpu_has_apic) {
1119 disable_apic = 1;
1120 printk(KERN_INFO "Apic disabled by BIOS\n");
1121 return -1;
1122 }
1123
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001124 enable_IR_x2apic();
1125 setup_apic_routing();
1126
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001127 verify_local_APIC();
1128
Glauber Costab5841762008-05-28 13:38:28 -03001129 connect_bsp_APIC();
1130
Jack Steinerb6df1b82008-06-19 21:51:05 -05001131 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001132 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001133
1134 setup_local_APIC();
1135
Andi Kleen739f33b2008-01-30 13:30:40 +01001136 /*
1137 * Now enable IO-APICs, actually call clear_IO_APIC
1138 * We need clear_IO_APIC before enabling vector on BP
1139 */
1140 if (!skip_ioapic_setup && nr_ioapics)
1141 enable_IO_APIC();
1142
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001143 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1144 localise_nmi_watchdog();
Andi Kleen739f33b2008-01-30 13:30:40 +01001145 end_local_APIC_setup();
1146
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001147 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1148 setup_IO_APIC();
1149 else
1150 nr_ioapics = 0;
1151 setup_boot_APIC_clock();
1152 check_nmi_watchdog();
1153 return 0;
1154}
1155
1156/*
1157 * Local APIC interrupts
1158 */
1159
1160/*
1161 * This interrupt should _never_ happen with our APIC/SMP architecture
1162 */
1163asmlinkage void smp_spurious_interrupt(void)
1164{
1165 unsigned int v;
1166 exit_idle();
1167 irq_enter();
1168 /*
1169 * Check if this really is a spurious interrupt and ACK it
1170 * if it is a vectored one. Just in case...
1171 * Spurious interrupts should not be ACKed.
1172 */
1173 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1174 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1175 ack_APIC_irq();
1176
1177 add_pda(irq_spurious_count, 1);
1178 irq_exit();
1179}
1180
1181/*
1182 * This interrupt should never happen with our APIC/SMP architecture
1183 */
1184asmlinkage void smp_error_interrupt(void)
1185{
1186 unsigned int v, v1;
1187
1188 exit_idle();
1189 irq_enter();
1190 /* First tickle the hardware, only then report what went on. -- REW */
1191 v = apic_read(APIC_ESR);
1192 apic_write(APIC_ESR, 0);
1193 v1 = apic_read(APIC_ESR);
1194 ack_APIC_irq();
1195 atomic_inc(&irq_err_count);
1196
1197 /* Here is what the APIC error bits mean:
1198 0: Send CS error
1199 1: Receive CS error
1200 2: Send accept error
1201 3: Receive accept error
1202 4: Reserved
1203 5: Send illegal vector
1204 6: Received illegal vector
1205 7: Illegal register address
1206 */
1207 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1208 smp_processor_id(), v , v1);
1209 irq_exit();
1210}
1211
Glauber Costab5841762008-05-28 13:38:28 -03001212/**
1213 * * connect_bsp_APIC - attach the APIC to the interrupt system
1214 * */
1215void __init connect_bsp_APIC(void)
1216{
1217 enable_apic_mode();
1218}
1219
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001220void disconnect_bsp_APIC(int virt_wire_setup)
1221{
1222 /* Go back to Virtual Wire compatibility mode */
1223 unsigned long value;
1224
1225 /* For the spurious interrupt use vector F, and enable it */
1226 value = apic_read(APIC_SPIV);
1227 value &= ~APIC_VECTOR_MASK;
1228 value |= APIC_SPIV_APIC_ENABLED;
1229 value |= 0xf;
1230 apic_write(APIC_SPIV, value);
1231
1232 if (!virt_wire_setup) {
1233 /*
1234 * For LVT0 make it edge triggered, active high,
1235 * external and enabled
1236 */
1237 value = apic_read(APIC_LVT0);
1238 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1239 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1240 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1241 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1242 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1243 apic_write(APIC_LVT0, value);
1244 } else {
1245 /* Disable LVT0 */
1246 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1247 }
1248
1249 /* For LVT1 make it edge triggered, active high, nmi and enabled */
1250 value = apic_read(APIC_LVT1);
1251 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1252 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1253 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1254 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1255 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1256 apic_write(APIC_LVT1, value);
1257}
1258
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001259void __cpuinit generic_processor_info(int apicid, int version)
1260{
1261 int cpu;
1262 cpumask_t tmp_map;
1263
1264 if (num_processors >= NR_CPUS) {
1265 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1266 " Processor ignored.\n", NR_CPUS);
1267 return;
1268 }
1269
1270 if (num_processors >= maxcpus) {
1271 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1272 " Processor ignored.\n", maxcpus);
1273 return;
1274 }
1275
1276 num_processors++;
1277 cpus_complement(tmp_map, cpu_present_map);
1278 cpu = first_cpu(tmp_map);
1279
1280 physid_set(apicid, phys_cpu_present_map);
1281 if (apicid == boot_cpu_physical_apicid) {
1282 /*
1283 * x86_bios_cpu_apicid is required to have processors listed
1284 * in same order as logical cpu numbers. Hence the first
1285 * entry is BSP, and so on.
1286 */
1287 cpu = 0;
1288 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001289 if (apicid > max_physical_apicid)
1290 max_physical_apicid = apicid;
1291
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001292 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001293 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1294 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1295 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001296
1297 cpu_to_apicid[cpu] = apicid;
1298 bios_cpu_apicid[cpu] = apicid;
1299 } else {
1300 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1301 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1302 }
1303
1304 cpu_set(cpu, cpu_possible_map);
1305 cpu_set(cpu, cpu_present_map);
1306}
1307
Suresh Siddha0c81c742008-07-10 11:16:48 -07001308int hard_smp_processor_id(void)
1309{
1310 return read_apic_id();
1311}
1312
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001313/*
1314 * Power management
1315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316#ifdef CONFIG_PM
1317
1318static struct {
1319 /* 'active' is true if the local APIC was enabled by us and
1320 not the BIOS; this signifies that we are also responsible
1321 for disabling it before entering apm/acpi suspend */
1322 int active;
1323 /* r/w apic fields */
1324 unsigned int apic_id;
1325 unsigned int apic_taskpri;
1326 unsigned int apic_ldr;
1327 unsigned int apic_dfr;
1328 unsigned int apic_spiv;
1329 unsigned int apic_lvtt;
1330 unsigned int apic_lvtpc;
1331 unsigned int apic_lvt0;
1332 unsigned int apic_lvt1;
1333 unsigned int apic_lvterr;
1334 unsigned int apic_tmict;
1335 unsigned int apic_tdcr;
1336 unsigned int apic_thmr;
1337} apic_pm_state;
1338
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001339static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001342 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if (!apic_pm_state.active)
1345 return 0;
1346
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001347 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001348
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001349 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1351 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1352 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1353 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1354 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001355 if (maxlvt >= 4)
1356 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1358 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1359 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1360 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1361 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001362#ifdef CONFIG_X86_MCE_INTEL
1363 if (maxlvt >= 5)
1364 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1365#endif
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001366 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 disable_local_APIC();
1368 local_irq_restore(flags);
1369 return 0;
1370}
1371
1372static int lapic_resume(struct sys_device *dev)
1373{
1374 unsigned int l, h;
1375 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001376 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 if (!apic_pm_state.active)
1379 return 0;
1380
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001381 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 local_irq_save(flags);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001384 if (!x2apic) {
1385 rdmsr(MSR_IA32_APICBASE, l, h);
1386 l &= ~MSR_IA32_APICBASE_BASE;
1387 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1388 wrmsr(MSR_IA32_APICBASE, l, h);
1389 } else
1390 enable_x2apic();
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1393 apic_write(APIC_ID, apic_pm_state.apic_id);
1394 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1395 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1396 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1397 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1398 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1399 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001400#ifdef CONFIG_X86_MCE_INTEL
1401 if (maxlvt >= 5)
1402 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1403#endif
1404 if (maxlvt >= 4)
1405 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1407 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1408 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1409 apic_write(APIC_ESR, 0);
1410 apic_read(APIC_ESR);
1411 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1412 apic_write(APIC_ESR, 0);
1413 apic_read(APIC_ESR);
1414 local_irq_restore(flags);
1415 return 0;
1416}
1417
1418static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001419 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .resume = lapic_resume,
1421 .suspend = lapic_suspend,
1422};
1423
1424static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001425 .id = 0,
1426 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427};
1428
Ashok Raje6982c62005-06-25 14:54:58 -07001429static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 apic_pm_state.active = 1;
1432}
1433
1434static int __init init_lapic_sysfs(void)
1435{
1436 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (!cpu_has_apic)
1439 return 0;
1440 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 error = sysdev_class_register(&lapic_sysclass);
1443 if (!error)
1444 error = sysdev_register(&device_lapic);
1445 return error;
1446}
1447device_initcall(init_lapic_sysfs);
1448
1449#else /* CONFIG_PM */
1450
1451static void apic_pm_activate(void) { }
1452
1453#endif /* CONFIG_PM */
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001456 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * Thus far, the major user of this is IBM's Summit2 series:
1459 *
Linus Torvalds637029c2006-02-27 20:41:56 -08001460 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * multi-chassis. Use available data to take a good guess.
1462 * If in doubt, go HPET.
1463 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001464__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 int i, clusters, zeros;
1467 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08001468 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1470
Yinghai Lu322850a2008-02-23 21:48:42 -08001471 /*
1472 * there is not this kind of box with AMD CPU yet.
1473 * Some AMD box with quadcore cpu and 8 sockets apicid
1474 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08001475 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08001476 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001477 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08001478 return 0;
1479
Mike Travis23ca4bb2008-05-12 21:21:12 +02001480 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07001481 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 for (i = 0; i < NR_CPUS; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001484 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01001485 if (bios_cpu_apicid) {
1486 id = bios_cpu_apicid[i];
travis@sgi.come8c10ef2008-01-30 13:33:12 +01001487 }
1488 else if (i < nr_cpu_ids) {
1489 if (cpu_present(i))
1490 id = per_cpu(x86_bios_cpu_apicid, i);
1491 else
1492 continue;
1493 }
1494 else
1495 break;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (id != BAD_APICID)
1498 __set_bit(APIC_CLUSTERID(id), clustermap);
1499 }
1500
1501 /* Problem: Partially populated chassis may not have CPUs in some of
1502 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01001503 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1504 * Since clusters are allocated sequentially, count zeros only if
1505 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
1507 clusters = 0;
1508 zeros = 0;
1509 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1510 if (test_bit(i, clustermap)) {
1511 clusters += 1 + zeros;
1512 zeros = 0;
1513 } else
1514 ++zeros;
1515 }
1516
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07001517 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1518 * not guaranteed to be synced between boards
1519 */
1520 if (is_vsmp_box() && clusters > 1)
1521 return 1;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001524 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * May have to revisit this when multi-core + hyperthreaded CPUs come
1526 * out, but AFAIK this will work even for them.
1527 */
1528 return (clusters > 2);
1529}
1530
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001531static __init int setup_nox2apic(char *str)
1532{
1533 disable_x2apic = 1;
1534 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_X2APIC);
1535 return 0;
1536}
1537early_param("nox2apic", setup_nox2apic);
1538
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001541 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001543static int __init apic_set_verbosity(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001545 if (str == NULL) {
1546 skip_ioapic_setup = 0;
1547 ioapic_force = 1;
1548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001550 if (strcmp("debug", str) == 0)
1551 apic_verbosity = APIC_DEBUG;
1552 else if (strcmp("verbose", str) == 0)
1553 apic_verbosity = APIC_VERBOSE;
1554 else {
1555 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1556 " use apic=verbose or apic=debug\n", str);
1557 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 0;
1561}
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001562early_param("apic", apic_set_verbosity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001564static __init int setup_disableapic(char *str)
1565{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07001567 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001568 return 0;
1569}
1570early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001572/* same as disableapic, for compatibility */
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001573static __init int setup_nolapic(char *str)
1574{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001575 return setup_disableapic(str);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001576}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001577early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds2e7c2832007-03-23 11:32:31 -07001579static int __init parse_lapic_timer_c2_ok(char *arg)
1580{
1581 local_apic_timer_c2_ok = 1;
1582 return 0;
1583}
1584early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1585
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001586static __init int setup_noapictimer(char *str)
1587{
Andi Kleen73dea472006-02-03 21:50:50 +01001588 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001591 return 1;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02001592}
Thomas Gleixner9f75e9b2007-10-12 23:04:23 +02001593__setup("noapictimer", setup_noapictimer);
Andi Kleen73dea472006-02-03 21:50:50 +01001594
Andi Kleen0c3749c2006-02-03 21:51:41 +01001595static __init int setup_apicpmtimer(char *s)
1596{
1597 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001598 notsc_setup(NULL);
Thomas Gleixnerb8ce3352007-10-12 23:04:07 +02001599 return 0;
Andi Kleen0c3749c2006-02-03 21:51:41 +01001600}
1601__setup("apicpmtimer", setup_apicpmtimer);
1602
Yinghai Lu1e934dd2008-02-22 13:37:26 -08001603static int __init lapic_insert_resource(void)
1604{
1605 if (!apic_phys)
1606 return -1;
1607
1608 /* Put local APIC into the resource map. */
1609 lapic_resource.start = apic_phys;
1610 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1611 insert_resource(&iomem_resource, &lapic_resource);
1612
1613 return 0;
1614}
1615
1616/*
1617 * need call insert after e820_reserve_resources()
1618 * that is using request_resource
1619 */
1620late_initcall(lapic_insert_resource);