blob: 496cd1bd2ae5803f2222bd4ce82f3e168223723c [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>
22#include <linux/smp_lock.h>
23#include <linux/interrupt.h>
24#include <linux/mc146818rtc.h>
25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h>
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/atomic.h>
30#include <asm/smp.h>
31#include <asm/mtrr.h>
32#include <asm/mpspec.h>
33#include <asm/pgalloc.h>
34#include <asm/mach_apic.h>
Andi Kleen75152112005-05-16 21:53:34 -070035#include <asm/nmi.h>
Andi Kleen95833c82006-01-11 22:44:36 +010036#include <asm/idle.h>
Andi Kleen73dea472006-02-03 21:50:50 +010037#include <asm/proto.h>
38#include <asm/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40int apic_verbosity;
Andi Kleen73dea472006-02-03 21:50:50 +010041int apic_runs_main_timer;
Andi Kleen0c3749c2006-02-03 21:51:41 +010042int apic_calibrate_pmtmr __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44int disable_apic_timer __initdata;
45
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +010046/*
47 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
48 * IPIs in place of local APIC timers
49 */
50static cpumask_t timer_interrupt_broadcast_ipi_mask;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* Using APIC to generate smp_local_timer_interrupt? */
Andreas Mohracae9d32006-06-23 02:04:25 -070053int using_apic_timer __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static void apic_pm_activate(void);
56
57void enable_NMI_through_LVT0 (void * dummy)
58{
Andi Kleen11a8e772006-01-11 22:46:51 +010059 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 v = APIC_DM_NMI; /* unmask and set to NMI */
Andi Kleen11a8e772006-01-11 22:46:51 +010062 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65int get_maxlvt(void)
66{
Andi Kleen11a8e772006-01-11 22:46:51 +010067 unsigned int v, maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 maxlvt = GET_APIC_MAXLVT(v);
71 return maxlvt;
72}
73
Andi Kleen3777a952006-02-03 21:51:53 +010074/*
75 * 'what should we do if we get a hw irq event on an illegal vector'.
76 * each architecture has to answer this themselves.
77 */
78void ack_bad_irq(unsigned int irq)
79{
80 printk("unexpected IRQ trap at vector %02x\n", irq);
81 /*
82 * Currently unexpected vectors happen only on SMP and APIC.
83 * We _must_ ack these because every local APIC has only N
84 * irq slots per priority level, and a 'hanging, unacked' IRQ
85 * holds up an irq slot - in excessive cases (when multiple
86 * unexpected vectors occur) that might lock up the APIC
87 * completely.
88 * But don't ack when the APIC is disabled. -AK
89 */
90 if (!disable_apic)
91 ack_APIC_irq();
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void clear_local_APIC(void)
95{
96 int maxlvt;
97 unsigned int v;
98
99 maxlvt = get_maxlvt();
100
101 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200102 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * if the vector is zero. Mask LVTERR first to prevent this.
104 */
105 if (maxlvt >= 3) {
106 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100107 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 /*
110 * Careful: we have to set masks only first to deassert
111 * any level-triggered sources.
112 */
113 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100114 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100116 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100118 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (maxlvt >= 4) {
120 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100121 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 /*
125 * Clean APIC state for other OSs:
126 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100127 apic_write(APIC_LVTT, APIC_LVT_MASKED);
128 apic_write(APIC_LVT0, APIC_LVT_MASKED);
129 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100131 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100133 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 v = GET_APIC_VERSION(apic_read(APIC_LVR));
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200135 apic_write(APIC_ESR, 0);
136 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Eric W. Biederman208fb932005-06-25 14:57:45 -0700139void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200141 /* Go back to Virtual Wire compatibility mode */
142 unsigned long value;
143
144 /* For the spurious interrupt use vector F, and enable it */
145 value = apic_read(APIC_SPIV);
146 value &= ~APIC_VECTOR_MASK;
147 value |= APIC_SPIV_APIC_ENABLED;
148 value |= 0xf;
149 apic_write(APIC_SPIV, value);
150
151 if (!virt_wire_setup) {
152 /* For LVT0 make it edge triggered, active high, external and enabled */
153 value = apic_read(APIC_LVT0);
154 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
155 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
156 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
157 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
158 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
159 apic_write(APIC_LVT0, value);
160 } else {
161 /* Disable LVT0 */
162 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700164
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200165 /* For LVT1 make it edge triggered, active high, nmi and enabled */
166 value = apic_read(APIC_LVT1);
167 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
Eric W. Biederman208fb932005-06-25 14:57:45 -0700168 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
169 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200170 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
171 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
172 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175void disable_local_APIC(void)
176{
177 unsigned int value;
178
179 clear_local_APIC();
180
181 /*
182 * Disable APIC (implies clearing of registers
183 * for 82489DX!).
184 */
185 value = apic_read(APIC_SPIV);
186 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100187 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/*
191 * This is to verify that we're looking at a real local APIC.
192 * Check these against your board if the CPUs aren't getting
193 * started for no apparent reason.
194 */
195int __init verify_local_APIC(void)
196{
197 unsigned int reg0, reg1;
198
199 /*
200 * The version register is read-only in a real APIC.
201 */
202 reg0 = apic_read(APIC_LVR);
203 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
204 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
205 reg1 = apic_read(APIC_LVR);
206 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
207
208 /*
209 * The two version reads above should print the same
210 * numbers. If the second one is different, then we
211 * poke at a non-APIC.
212 */
213 if (reg1 != reg0)
214 return 0;
215
216 /*
217 * Check if the version looks reasonably.
218 */
219 reg1 = GET_APIC_VERSION(reg0);
220 if (reg1 == 0x00 || reg1 == 0xff)
221 return 0;
222 reg1 = get_maxlvt();
223 if (reg1 < 0x02 || reg1 == 0xff)
224 return 0;
225
226 /*
227 * The ID register is read/write in a real APIC.
228 */
229 reg0 = apic_read(APIC_ID);
230 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
231 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
232 reg1 = apic_read(APIC_ID);
233 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
234 apic_write(APIC_ID, reg0);
235 if (reg1 != (reg0 ^ APIC_ID_MASK))
236 return 0;
237
238 /*
239 * The next two are just to see if we have sane values.
240 * They're only really relevant if we're in Virtual Wire
241 * compatibility mode, but most boxes are anymore.
242 */
243 reg0 = apic_read(APIC_LVT0);
244 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
245 reg1 = apic_read(APIC_LVT1);
246 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
247
248 return 1;
249}
250
251void __init sync_Arb_IDs(void)
252{
253 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
254 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
255 if (ver >= 0x14) /* P4 or higher */
256 return;
257
258 /*
259 * Wait for idle.
260 */
261 apic_wait_icr_idle();
262
263 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100264 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 | APIC_DM_INIT);
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/*
269 * An initial setup of the virtual wire mode.
270 */
271void __init init_bsp_APIC(void)
272{
Andi Kleen11a8e772006-01-11 22:46:51 +0100273 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * Don't do the setup now if we have a SMP BIOS as the
277 * through-I/O-APIC virtual wire mode might be active.
278 */
279 if (smp_found_config || !cpu_has_apic)
280 return;
281
282 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /*
285 * Do not trust the local APIC being empty at bootup.
286 */
287 clear_local_APIC();
288
289 /*
290 * Enable APIC.
291 */
292 value = apic_read(APIC_SPIV);
293 value &= ~APIC_VECTOR_MASK;
294 value |= APIC_SPIV_APIC_ENABLED;
295 value |= APIC_SPIV_FOCUS_DISABLED;
296 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100297 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /*
300 * Set up the virtual wire mode.
301 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100302 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100304 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Ashok Raje6982c62005-06-25 14:54:58 -0700307void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Andi Kleen11a8e772006-01-11 22:46:51 +0100309 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100310 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Andi Kleenfe7414a2006-09-26 10:52:30 +0200314 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
317 * Double-check whether this APIC is really registered.
318 * This is meaningless in clustered apic mode, so we skip it.
319 */
320 if (!apic_id_registered())
321 BUG();
322
323 /*
324 * Intel recommends to set DFR, LDR and TPR before enabling
325 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
326 * document number 292116). So here it goes...
327 */
328 init_apic_ldr();
329
330 /*
331 * Set Task Priority to 'accept all'. We never change this
332 * later on.
333 */
334 value = apic_read(APIC_TASKPRI);
335 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100336 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100339 * After a crash, we no longer service the interrupts and a pending
340 * interrupt from previous kernel might still have ISR bit set.
341 *
342 * Most probably by now CPU has serviced that pending interrupt and
343 * it might not have done the ack_APIC_irq() because it thought,
344 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
345 * does not clear the ISR bit and cpu thinks it has already serivced
346 * the interrupt. Hence a vector might get locked. It was noticed
347 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
348 */
349 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
350 value = apic_read(APIC_ISR + i*0x10);
351 for (j = 31; j >= 0; j--) {
352 if (value & (1<<j))
353 ack_APIC_irq();
354 }
355 }
356
357 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * Now that we are all set up, enable the APIC
359 */
360 value = apic_read(APIC_SPIV);
361 value &= ~APIC_VECTOR_MASK;
362 /*
363 * Enable APIC
364 */
365 value |= APIC_SPIV_APIC_ENABLED;
366
Andi Kleen3f14c742006-09-26 10:52:29 +0200367 /* We always use processor focus */
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
370 * Set spurious IRQ vector
371 */
372 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100373 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * Set up LVT0, LVT1:
377 *
378 * set up through-local-APIC on the BP's LINT0. This is not
379 * strictly necessary in pure symmetric-IO mode, but sometimes
380 * we delegate interrupts to the 8259A.
381 */
382 /*
383 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
384 */
385 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Andi Kleena8fcf1a2006-09-26 10:52:30 +0200386 if (!smp_processor_id() && !value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 value = APIC_DM_EXTINT;
388 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
389 } else {
390 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
391 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
392 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100393 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * only the BP should see the LINT1 NMI signal, obviously.
397 */
398 if (!smp_processor_id())
399 value = APIC_DM_NMI;
400 else
401 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100402 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Andi Kleen61c11342005-09-12 18:49:23 +0200404 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned oldvalue;
406 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 oldvalue = apic_read(APIC_ESR);
408 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100409 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * spec says clear errors after enabling vector.
412 */
413 if (maxlvt > 3)
414 apic_write(APIC_ESR, 0);
415 value = apic_read(APIC_ESR);
416 if (value != oldvalue)
417 apic_printk(APIC_VERBOSE,
418 "ESR value after enabling vector: %08x, after %08x\n",
419 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200423 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 apic_pm_activate();
425}
426
427#ifdef CONFIG_PM
428
429static struct {
430 /* 'active' is true if the local APIC was enabled by us and
431 not the BIOS; this signifies that we are also responsible
432 for disabling it before entering apm/acpi suspend */
433 int active;
434 /* r/w apic fields */
435 unsigned int apic_id;
436 unsigned int apic_taskpri;
437 unsigned int apic_ldr;
438 unsigned int apic_dfr;
439 unsigned int apic_spiv;
440 unsigned int apic_lvtt;
441 unsigned int apic_lvtpc;
442 unsigned int apic_lvt0;
443 unsigned int apic_lvt1;
444 unsigned int apic_lvterr;
445 unsigned int apic_tmict;
446 unsigned int apic_tdcr;
447 unsigned int apic_thmr;
448} apic_pm_state;
449
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700450static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 unsigned long flags;
453
454 if (!apic_pm_state.active)
455 return 0;
456
457 apic_pm_state.apic_id = apic_read(APIC_ID);
458 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
459 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
460 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
461 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
462 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
463 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
464 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
465 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
466 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
467 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
468 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
469 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
470 local_save_flags(flags);
471 local_irq_disable();
472 disable_local_APIC();
473 local_irq_restore(flags);
474 return 0;
475}
476
477static int lapic_resume(struct sys_device *dev)
478{
479 unsigned int l, h;
480 unsigned long flags;
481
482 if (!apic_pm_state.active)
483 return 0;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 local_irq_save(flags);
486 rdmsr(MSR_IA32_APICBASE, l, h);
487 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100488 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 wrmsr(MSR_IA32_APICBASE, l, h);
490 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
491 apic_write(APIC_ID, apic_pm_state.apic_id);
492 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
493 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
494 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
495 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
496 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
497 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
498 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
499 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
500 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
501 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
502 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
503 apic_write(APIC_ESR, 0);
504 apic_read(APIC_ESR);
505 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
506 apic_write(APIC_ESR, 0);
507 apic_read(APIC_ESR);
508 local_irq_restore(flags);
509 return 0;
510}
511
512static struct sysdev_class lapic_sysclass = {
513 set_kset_name("lapic"),
514 .resume = lapic_resume,
515 .suspend = lapic_suspend,
516};
517
518static struct sys_device device_lapic = {
519 .id = 0,
520 .cls = &lapic_sysclass,
521};
522
Ashok Raje6982c62005-06-25 14:54:58 -0700523static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 apic_pm_state.active = 1;
526}
527
528static int __init init_lapic_sysfs(void)
529{
530 int error;
531 if (!cpu_has_apic)
532 return 0;
533 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
534 error = sysdev_class_register(&lapic_sysclass);
535 if (!error)
536 error = sysdev_register(&device_lapic);
537 return error;
538}
539device_initcall(init_lapic_sysfs);
540
541#else /* CONFIG_PM */
542
543static void apic_pm_activate(void) { }
544
545#endif /* CONFIG_PM */
546
547static int __init apic_set_verbosity(char *str)
548{
549 if (strcmp("debug", str) == 0)
550 apic_verbosity = APIC_DEBUG;
551 else if (strcmp("verbose", str) == 0)
552 apic_verbosity = APIC_VERBOSE;
553 else
554 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
555 " use apic=verbose or apic=debug", str);
556
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800557 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560__setup("apic=", apic_set_verbosity);
561
562/*
563 * Detect and enable local APICs on non-SMP boards.
564 * Original code written by Keir Fraser.
565 * On AMD64 we trust the BIOS - if it says no APIC it is likely
566 * not correctly set up (usually the APIC timer won't work etc.)
567 */
568
569static int __init detect_init_APIC (void)
570{
571 if (!cpu_has_apic) {
572 printk(KERN_INFO "No local APIC present\n");
573 return -1;
574 }
575
576 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
577 boot_cpu_id = 0;
578 return 0;
579}
580
581void __init init_apic_mappings(void)
582{
583 unsigned long apic_phys;
584
585 /*
586 * If no local APIC can be found then set up a fake all
587 * zeroes page to simulate the local APIC and another
588 * one for the IO-APIC.
589 */
590 if (!smp_found_config && detect_init_APIC()) {
591 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
592 apic_phys = __pa(apic_phys);
593 } else
594 apic_phys = mp_lapic_addr;
595
596 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
597 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
598
599 /*
600 * Fetch the APIC ID of the BSP in case we have a
601 * default configuration (or the MP table is broken).
602 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200603 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 {
606 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
607 int i;
608
609 for (i = 0; i < nr_ioapics; i++) {
610 if (smp_found_config) {
611 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
612 } else {
613 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
614 ioapic_phys = __pa(ioapic_phys);
615 }
616 set_fixmap_nocache(idx, ioapic_phys);
617 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
618 __fix_to_virt(idx), ioapic_phys);
619 idx++;
620 }
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * This function sets up the local APIC timer, with a timeout of
626 * 'clocks' APIC bus clock. During calibration we actually call
627 * this function twice on the boot CPU, once with a bogus timeout
628 * value, second time for real. The other (noncalibrating) CPUs
629 * call this function only once, with the real, calibrated value.
630 *
631 * We do reads before writes even if unnecessary, to get around the
632 * P5 APIC double write bug.
633 */
634
635#define APIC_DIVISOR 16
636
637static void __setup_APIC_LVTT(unsigned int clocks)
638{
639 unsigned int lvtt_value, tmp_value, ver;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100640 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
643 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100644
645 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
646 lvtt_value |= APIC_LVT_MASKED;
647
Andi Kleen11a8e772006-01-11 22:46:51 +0100648 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /*
651 * Divide PICLK by 16
652 */
653 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100654 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
656 | APIC_TDR_DIV_16);
657
Andi Kleen11a8e772006-01-11 22:46:51 +0100658 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static void setup_APIC_timer(unsigned int clocks)
662{
663 unsigned long flags;
664
665 local_irq_save(flags);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* wait for irq slice */
Chris McDermott33042a92006-02-11 17:55:50 -0800668 if (vxtime.hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int trigger = hpet_readl(HPET_T0_CMP);
670 while (hpet_readl(HPET_COUNTER) >= trigger)
671 /* do nothing */ ;
672 while (hpet_readl(HPET_COUNTER) < trigger)
673 /* do nothing */ ;
674 } else {
675 int c1, c2;
676 outb_p(0x00, 0x43);
677 c2 = inb_p(0x40);
678 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100679 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 c1 = c2;
681 outb_p(0x00, 0x43);
682 c2 = inb_p(0x40);
683 c2 |= inb_p(0x40) << 8;
684 } while (c2 - c1 < 300);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100687 /* Turn off PIT interrupt if we use APIC timer as main timer.
688 Only works with the PM timer right now
689 TBD fix it for HPET too. */
690 if (vxtime.mode == VXTIME_PMTMR &&
691 smp_processor_id() == boot_cpu_id &&
692 apic_runs_main_timer == 1 &&
693 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
694 stop_timer_interrupt();
695 apic_runs_main_timer++;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 local_irq_restore(flags);
698}
699
700/*
701 * In this function we calibrate APIC bus clocks to the external
702 * timer. Unfortunately we cannot use jiffies and the timer irq
703 * to calibrate, since some later bootup code depends on getting
704 * the first irq? Ugh.
705 *
706 * We want to do the calibration only once since we
707 * want to have local timer irqs syncron. CPUs connected
708 * by the same APIC bus have the very same bus frequency.
709 * And we want to have irqs off anyways, no accidental
710 * APIC irq that way.
711 */
712
713#define TICK_COUNT 100000000
714
715static int __init calibrate_APIC_clock(void)
716{
717 int apic, apic_start, tsc, tsc_start;
718 int result;
719 /*
720 * Put whatever arbitrary (but long enough) timeout
721 * value into the APIC clock, we just want to get the
722 * counter running for calibration.
723 */
724 __setup_APIC_LVTT(1000000000);
725
726 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100727#ifdef CONFIG_X86_PM_TIMER
728 if (apic_calibrate_pmtmr && pmtmr_ioport) {
729 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100731 result = (apic_start - apic) * 1000L / 5;
732 } else
733#endif
734 {
735 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Andi Kleen0c3749c2006-02-03 21:51:41 +0100737 do {
738 apic = apic_read(APIC_TMCCT);
739 rdtscl(tsc);
740 } while ((tsc - tsc_start) < TICK_COUNT &&
741 (apic - apic_start) < TICK_COUNT);
742
743 result = (apic_start - apic) * 1000L * cpu_khz /
744 (tsc - tsc_start);
745 }
746 printk("result %d\n", result);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
750 result / 1000 / 1000, result / 1000 % 1000);
751
752 return result * APIC_DIVISOR / HZ;
753}
754
755static unsigned int calibration_result;
756
757void __init setup_boot_APIC_clock (void)
758{
759 if (disable_apic_timer) {
760 printk(KERN_INFO "Disabling APIC timer\n");
761 return;
762 }
763
764 printk(KERN_INFO "Using local APIC timer interrupts.\n");
765 using_apic_timer = 1;
766
767 local_irq_disable();
768
769 calibration_result = calibrate_APIC_clock();
770 /*
771 * Now set up the timer for real.
772 */
773 setup_APIC_timer(calibration_result);
774
775 local_irq_enable();
776}
777
Ashok Raje6982c62005-06-25 14:54:58 -0700778void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 local_irq_disable(); /* FIXME: Do we need this? --RR */
781 setup_APIC_timer(calibration_result);
782 local_irq_enable();
783}
784
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100785void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 if (using_apic_timer) {
788 unsigned long v;
789
790 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200791 /*
792 * When an illegal vector value (0-15) is written to an LVT
793 * entry and delivery mode is Fixed, the APIC may signal an
794 * illegal vector error, with out regard to whether the mask
795 * bit is set or whether an interrupt is actually seen on input.
796 *
797 * Boot sequence might call this function when the LVTT has
798 * '0' vector value. So make sure vector field is set to
799 * valid value.
800 */
801 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
802 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804}
805
806void enable_APIC_timer(void)
807{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100808 int cpu = smp_processor_id();
809
810 if (using_apic_timer &&
811 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned long v;
813
814 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100815 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817}
818
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100819void switch_APIC_timer_to_ipi(void *cpumask)
820{
821 cpumask_t mask = *(cpumask_t *)cpumask;
822 int cpu = smp_processor_id();
823
824 if (cpu_isset(cpu, mask) &&
825 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
826 disable_APIC_timer();
827 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
828 }
829}
830EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
831
832void smp_send_timer_broadcast_ipi(void)
833{
834 cpumask_t mask;
835
836 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
837 if (!cpus_empty(mask)) {
838 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
839 }
840}
841
842void switch_ipi_to_APIC_timer(void *cpumask)
843{
844 cpumask_t mask = *(cpumask_t *)cpumask;
845 int cpu = smp_processor_id();
846
847 if (cpu_isset(cpu, mask) &&
848 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
849 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
850 enable_APIC_timer();
851 }
852}
853EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855int setup_profiling_timer(unsigned int multiplier)
856{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100857 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Jacob Shin17fc14f2006-06-26 13:58:47 +0200860void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
861 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100862{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200863 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
864 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100865 apic_write(reg, v);
866}
Jacob Shin89b831e2005-11-05 17:25:53 +0100867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#undef APIC_DIVISOR
869
870/*
871 * Local timer interrupt handler. It does both profiling and
872 * process statistics/rescheduling.
873 *
874 * We do profiling in every local tick, statistics/rescheduling
875 * happen only every 'profiling multiplier' ticks. The default
876 * multiplier is 1 and it can be changed by writing the new multiplier
877 * value into /proc/profile.
878 */
879
880void smp_local_timer_interrupt(struct pt_regs *regs)
881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883#ifdef CONFIG_SMP
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100884 update_process_times(user_mode(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100886 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
887 main_timer_handler(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
889 * We take the 'long' return path, and there every subsystem
890 * grabs the appropriate locks (kernel lock/ irq lock).
891 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +0200892 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * and do the profiling totally in assembly.
894 *
895 * Currently this isn't too much of an issue (performance wise),
896 * we can take more than 100K local irqs per second on a 100 MHz P5.
897 */
898}
899
900/*
901 * Local APIC timer interrupt. This is the most natural way for doing
902 * local interrupts, but local timer interrupts can be emulated by
903 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
904 *
905 * [ if a single-CPU system runs an SMP kernel then we call the local
906 * interrupt as well. Thus we cannot inline the local irq ... ]
907 */
908void smp_apic_timer_interrupt(struct pt_regs *regs)
909{
910 /*
911 * the NMI deadlock-detector uses this.
912 */
913 add_pda(apic_timer_irqs, 1);
914
915 /*
916 * NOTE! We'd better ACK the irq immediately,
917 * because timer handling can be slow.
918 */
919 ack_APIC_irq();
920 /*
921 * update_process_times() expects us to have done irq_enter().
922 * Besides, if we don't timer interrupts ignore the global
923 * interrupt lock, which is the WrongThing (tm) to do.
924 */
Andi Kleen95833c82006-01-11 22:44:36 +0100925 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 irq_enter();
927 smp_local_timer_interrupt(regs);
928 irq_exit();
929}
930
931/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200932 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *
934 * Thus far, the major user of this is IBM's Summit2 series:
935 *
Linus Torvalds637029c2006-02-27 20:41:56 -0800936 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * multi-chassis. Use available data to take a good guess.
938 * If in doubt, go HPET.
939 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200940__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 int i, clusters, zeros;
943 unsigned id;
944 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
945
Suresh Siddha376ec332005-05-16 21:53:32 -0700946 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 for (i = 0; i < NR_CPUS; i++) {
949 id = bios_cpu_apicid[i];
950 if (id != BAD_APICID)
951 __set_bit(APIC_CLUSTERID(id), clustermap);
952 }
953
954 /* Problem: Partially populated chassis may not have CPUs in some of
955 * the APIC clusters they have been allocated. Only present CPUs have
956 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
957 * clusters are allocated sequentially, count zeros only if they are
958 * bounded by ones.
959 */
960 clusters = 0;
961 zeros = 0;
962 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
963 if (test_bit(i, clustermap)) {
964 clusters += 1 + zeros;
965 zeros = 0;
966 } else
967 ++zeros;
968 }
969
970 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200971 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * May have to revisit this when multi-core + hyperthreaded CPUs come
973 * out, but AFAIK this will work even for them.
974 */
975 return (clusters > 2);
976}
977
978/*
979 * This interrupt should _never_ happen with our APIC/SMP architecture
980 */
981asmlinkage void smp_spurious_interrupt(void)
982{
983 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +0100984 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 irq_enter();
986 /*
987 * Check if this really is a spurious interrupt and ACK it
988 * if it is a vectored one. Just in case...
989 * Spurious interrupts should not be ACKed.
990 */
991 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
992 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
993 ack_APIC_irq();
994
995#if 0
996 static unsigned long last_warning;
997 static unsigned long skipped;
998
999 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1000 if (time_before(last_warning+30*HZ,jiffies)) {
1001 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1002 smp_processor_id(), skipped);
1003 last_warning = jiffies;
1004 skipped = 0;
1005 } else {
1006 skipped++;
1007 }
1008#endif
1009 irq_exit();
1010}
1011
1012/*
1013 * This interrupt should never happen with our APIC/SMP architecture
1014 */
1015
1016asmlinkage void smp_error_interrupt(void)
1017{
1018 unsigned int v, v1;
1019
Andi Kleen95833c82006-01-11 22:44:36 +01001020 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 irq_enter();
1022 /* First tickle the hardware, only then report what went on. -- REW */
1023 v = apic_read(APIC_ESR);
1024 apic_write(APIC_ESR, 0);
1025 v1 = apic_read(APIC_ESR);
1026 ack_APIC_irq();
1027 atomic_inc(&irq_err_count);
1028
1029 /* Here is what the APIC error bits mean:
1030 0: Send CS error
1031 1: Receive CS error
1032 2: Send accept error
1033 3: Receive accept error
1034 4: Reserved
1035 5: Send illegal vector
1036 6: Received illegal vector
1037 7: Illegal register address
1038 */
1039 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1040 smp_processor_id(), v , v1);
1041 irq_exit();
1042}
1043
1044int disable_apic;
1045
1046/*
1047 * This initializes the IO-APIC and APIC hardware if this is
1048 * a UP kernel.
1049 */
1050int __init APIC_init_uniprocessor (void)
1051{
1052 if (disable_apic) {
1053 printk(KERN_INFO "Apic disabled\n");
1054 return -1;
1055 }
1056 if (!cpu_has_apic) {
1057 disable_apic = 1;
1058 printk(KERN_INFO "Apic disabled by BIOS\n");
1059 return -1;
1060 }
1061
1062 verify_local_APIC();
1063
Andi Kleen357e11d2005-09-12 18:49:24 +02001064 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001065 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 setup_local_APIC();
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001070 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 else
1072 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001074 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076}
1077
1078static __init int setup_disableapic(char *str)
1079{
1080 disable_apic = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001081 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static __init int setup_nolapic(char *str)
1085{
1086 disable_apic = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001087 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090static __init int setup_noapictimer(char *str)
1091{
Andi Kleen73dea472006-02-03 21:50:50 +01001092 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001095 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Andi Kleen73dea472006-02-03 21:50:50 +01001098static __init int setup_apicmaintimer(char *str)
1099{
1100 apic_runs_main_timer = 1;
1101 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001102 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001103}
1104__setup("apicmaintimer", setup_apicmaintimer);
1105
1106static __init int setup_noapicmaintimer(char *str)
1107{
1108 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001109 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001110}
1111__setup("noapicmaintimer", setup_noapicmaintimer);
1112
Andi Kleen0c3749c2006-02-03 21:51:41 +01001113static __init int setup_apicpmtimer(char *s)
1114{
1115 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001116 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001117 return setup_apicmaintimer(NULL);
1118}
1119__setup("apicpmtimer", setup_apicpmtimer);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* dummy parsing: see setup.c */
1122
1123__setup("disableapic", setup_disableapic);
1124__setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1125
1126__setup("noapictimer", setup_noapictimer);
1127
1128/* no "lapic" flag - we only use the lapic when the BIOS tells us so. */