blob: 78c43b1a30644acb85393109fb13edc939a3fabe [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
139void __init connect_bsp_APIC(void)
140{
141 if (pic_mode) {
142 /*
143 * Do not trust the local APIC being empty at bootup.
144 */
145 clear_local_APIC();
146 /*
147 * PIC mode, enable APIC mode in the IMCR, i.e.
148 * connect BSP's local APIC to INT and NMI lines.
149 */
150 apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
151 outb(0x70, 0x22);
152 outb(0x01, 0x23);
153 }
154}
155
Eric W. Biederman208fb932005-06-25 14:57:45 -0700156void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 if (pic_mode) {
159 /*
160 * Put the board back into PIC mode (has an effect
161 * only on certain older boards). Note that APIC
162 * interrupts, including IPIs, won't work beyond
163 * this point! The only exception are INIT IPIs.
164 */
165 apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
166 outb(0x70, 0x22);
167 outb(0x00, 0x23);
168 }
Eric W. Biederman208fb932005-06-25 14:57:45 -0700169 else {
170 /* Go back to Virtual Wire compatibility mode */
171 unsigned long value;
172
173 /* For the spurious interrupt use vector F, and enable it */
174 value = apic_read(APIC_SPIV);
175 value &= ~APIC_VECTOR_MASK;
176 value |= APIC_SPIV_APIC_ENABLED;
177 value |= 0xf;
Andi Kleen11a8e772006-01-11 22:46:51 +0100178 apic_write(APIC_SPIV, value);
Eric W. Biederman208fb932005-06-25 14:57:45 -0700179
180 if (!virt_wire_setup) {
181 /* For LVT0 make it edge triggered, active high, external and enabled */
182 value = apic_read(APIC_LVT0);
183 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
184 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
185 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
186 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
187 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100188 apic_write(APIC_LVT0, value);
Eric W. Biederman208fb932005-06-25 14:57:45 -0700189 }
190 else {
191 /* Disable LVT0 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100192 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Eric W. Biederman208fb932005-06-25 14:57:45 -0700193 }
194
195 /* For LVT1 make it edge triggered, active high, nmi and enabled */
196 value = apic_read(APIC_LVT1);
197 value &= ~(
198 APIC_MODE_MASK | APIC_SEND_PENDING |
199 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
200 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
201 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
202 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
Andi Kleen11a8e772006-01-11 22:46:51 +0100203 apic_write(APIC_LVT1, value);
Eric W. Biederman208fb932005-06-25 14:57:45 -0700204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207void disable_local_APIC(void)
208{
209 unsigned int value;
210
211 clear_local_APIC();
212
213 /*
214 * Disable APIC (implies clearing of registers
215 * for 82489DX!).
216 */
217 value = apic_read(APIC_SPIV);
218 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100219 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/*
223 * This is to verify that we're looking at a real local APIC.
224 * Check these against your board if the CPUs aren't getting
225 * started for no apparent reason.
226 */
227int __init verify_local_APIC(void)
228{
229 unsigned int reg0, reg1;
230
231 /*
232 * The version register is read-only in a real APIC.
233 */
234 reg0 = apic_read(APIC_LVR);
235 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
236 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
237 reg1 = apic_read(APIC_LVR);
238 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
239
240 /*
241 * The two version reads above should print the same
242 * numbers. If the second one is different, then we
243 * poke at a non-APIC.
244 */
245 if (reg1 != reg0)
246 return 0;
247
248 /*
249 * Check if the version looks reasonably.
250 */
251 reg1 = GET_APIC_VERSION(reg0);
252 if (reg1 == 0x00 || reg1 == 0xff)
253 return 0;
254 reg1 = get_maxlvt();
255 if (reg1 < 0x02 || reg1 == 0xff)
256 return 0;
257
258 /*
259 * The ID register is read/write in a real APIC.
260 */
261 reg0 = apic_read(APIC_ID);
262 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
263 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
264 reg1 = apic_read(APIC_ID);
265 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
266 apic_write(APIC_ID, reg0);
267 if (reg1 != (reg0 ^ APIC_ID_MASK))
268 return 0;
269
270 /*
271 * The next two are just to see if we have sane values.
272 * They're only really relevant if we're in Virtual Wire
273 * compatibility mode, but most boxes are anymore.
274 */
275 reg0 = apic_read(APIC_LVT0);
276 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
277 reg1 = apic_read(APIC_LVT1);
278 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
279
280 return 1;
281}
282
283void __init sync_Arb_IDs(void)
284{
285 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
286 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
287 if (ver >= 0x14) /* P4 or higher */
288 return;
289
290 /*
291 * Wait for idle.
292 */
293 apic_wait_icr_idle();
294
295 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Andi Kleen11a8e772006-01-11 22:46:51 +0100296 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 | APIC_DM_INIT);
298}
299
300extern void __error_in_apic_c (void);
301
302/*
303 * An initial setup of the virtual wire mode.
304 */
305void __init init_bsp_APIC(void)
306{
Andi Kleen11a8e772006-01-11 22:46:51 +0100307 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /*
310 * Don't do the setup now if we have a SMP BIOS as the
311 * through-I/O-APIC virtual wire mode might be active.
312 */
313 if (smp_found_config || !cpu_has_apic)
314 return;
315
316 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
319 * Do not trust the local APIC being empty at bootup.
320 */
321 clear_local_APIC();
322
323 /*
324 * Enable APIC.
325 */
326 value = apic_read(APIC_SPIV);
327 value &= ~APIC_VECTOR_MASK;
328 value |= APIC_SPIV_APIC_ENABLED;
329 value |= APIC_SPIV_FOCUS_DISABLED;
330 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100331 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
334 * Set up the virtual wire mode.
335 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100336 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 value = APIC_DM_NMI;
Andi Kleen11a8e772006-01-11 22:46:51 +0100338 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Ashok Raje6982c62005-06-25 14:54:58 -0700341void __cpuinit setup_local_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Andi Kleen11a8e772006-01-11 22:46:51 +0100343 unsigned int value, maxlvt;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100344 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 value = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
349 __error_in_apic_c();
350
351 /*
352 * Double-check whether this APIC is really registered.
353 * This is meaningless in clustered apic mode, so we skip it.
354 */
355 if (!apic_id_registered())
356 BUG();
357
358 /*
359 * Intel recommends to set DFR, LDR and TPR before enabling
360 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
361 * document number 292116). So here it goes...
362 */
363 init_apic_ldr();
364
365 /*
366 * Set Task Priority to 'accept all'. We never change this
367 * later on.
368 */
369 value = apic_read(APIC_TASKPRI);
370 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +0100371 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +0100374 * After a crash, we no longer service the interrupts and a pending
375 * interrupt from previous kernel might still have ISR bit set.
376 *
377 * Most probably by now CPU has serviced that pending interrupt and
378 * it might not have done the ack_APIC_irq() because it thought,
379 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
380 * does not clear the ISR bit and cpu thinks it has already serivced
381 * the interrupt. Hence a vector might get locked. It was noticed
382 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
383 */
384 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
385 value = apic_read(APIC_ISR + i*0x10);
386 for (j = 31; j >= 0; j--) {
387 if (value & (1<<j))
388 ack_APIC_irq();
389 }
390 }
391
392 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * Now that we are all set up, enable the APIC
394 */
395 value = apic_read(APIC_SPIV);
396 value &= ~APIC_VECTOR_MASK;
397 /*
398 * Enable APIC
399 */
400 value |= APIC_SPIV_APIC_ENABLED;
401
Andi Kleen3f14c742006-09-26 10:52:29 +0200402 /* We always use processor focus */
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
405 * Set spurious IRQ vector
406 */
407 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +0100408 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
411 * Set up LVT0, LVT1:
412 *
413 * set up through-local-APIC on the BP's LINT0. This is not
414 * strictly necessary in pure symmetric-IO mode, but sometimes
415 * we delegate interrupts to the 8259A.
416 */
417 /*
418 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
419 */
420 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
421 if (!smp_processor_id() && (pic_mode || !value)) {
422 value = APIC_DM_EXTINT;
423 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
424 } else {
425 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
426 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
427 }
Andi Kleen11a8e772006-01-11 22:46:51 +0100428 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /*
431 * only the BP should see the LINT1 NMI signal, obviously.
432 */
433 if (!smp_processor_id())
434 value = APIC_DM_NMI;
435 else
436 value = APIC_DM_NMI | APIC_LVT_MASKED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100437 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Andi Kleen61c11342005-09-12 18:49:23 +0200439 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned oldvalue;
441 maxlvt = get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 oldvalue = apic_read(APIC_ESR);
443 value = ERROR_APIC_VECTOR; // enables sending errors
Andi Kleen11a8e772006-01-11 22:46:51 +0100444 apic_write(APIC_LVTERR, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * spec says clear errors after enabling vector.
447 */
448 if (maxlvt > 3)
449 apic_write(APIC_ESR, 0);
450 value = apic_read(APIC_ESR);
451 if (value != oldvalue)
452 apic_printk(APIC_VERBOSE,
453 "ESR value after enabling vector: %08x, after %08x\n",
454 oldvalue, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
457 nmi_watchdog_default();
Don Zickusf2802e72006-09-26 10:52:26 +0200458 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 apic_pm_activate();
460}
461
462#ifdef CONFIG_PM
463
464static struct {
465 /* 'active' is true if the local APIC was enabled by us and
466 not the BIOS; this signifies that we are also responsible
467 for disabling it before entering apm/acpi suspend */
468 int active;
469 /* r/w apic fields */
470 unsigned int apic_id;
471 unsigned int apic_taskpri;
472 unsigned int apic_ldr;
473 unsigned int apic_dfr;
474 unsigned int apic_spiv;
475 unsigned int apic_lvtt;
476 unsigned int apic_lvtpc;
477 unsigned int apic_lvt0;
478 unsigned int apic_lvt1;
479 unsigned int apic_lvterr;
480 unsigned int apic_tmict;
481 unsigned int apic_tdcr;
482 unsigned int apic_thmr;
483} apic_pm_state;
484
Pavel Machek0b9c33a2005-04-16 15:25:31 -0700485static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 unsigned long flags;
488
489 if (!apic_pm_state.active)
490 return 0;
491
492 apic_pm_state.apic_id = apic_read(APIC_ID);
493 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
494 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
495 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
496 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
497 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
498 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
499 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
500 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
501 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
502 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
503 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
504 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
505 local_save_flags(flags);
506 local_irq_disable();
507 disable_local_APIC();
508 local_irq_restore(flags);
509 return 0;
510}
511
512static int lapic_resume(struct sys_device *dev)
513{
514 unsigned int l, h;
515 unsigned long flags;
516
517 if (!apic_pm_state.active)
518 return 0;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 local_irq_save(flags);
521 rdmsr(MSR_IA32_APICBASE, l, h);
522 l &= ~MSR_IA32_APICBASE_BASE;
Shaohua Li5b743572006-01-16 01:56:45 +0100523 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 wrmsr(MSR_IA32_APICBASE, l, h);
525 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
526 apic_write(APIC_ID, apic_pm_state.apic_id);
527 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
528 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
529 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
530 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
531 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
532 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
533 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
534 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
535 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
536 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
537 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
538 apic_write(APIC_ESR, 0);
539 apic_read(APIC_ESR);
540 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
541 apic_write(APIC_ESR, 0);
542 apic_read(APIC_ESR);
543 local_irq_restore(flags);
544 return 0;
545}
546
547static struct sysdev_class lapic_sysclass = {
548 set_kset_name("lapic"),
549 .resume = lapic_resume,
550 .suspend = lapic_suspend,
551};
552
553static struct sys_device device_lapic = {
554 .id = 0,
555 .cls = &lapic_sysclass,
556};
557
Ashok Raje6982c62005-06-25 14:54:58 -0700558static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 apic_pm_state.active = 1;
561}
562
563static int __init init_lapic_sysfs(void)
564{
565 int error;
566 if (!cpu_has_apic)
567 return 0;
568 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
569 error = sysdev_class_register(&lapic_sysclass);
570 if (!error)
571 error = sysdev_register(&device_lapic);
572 return error;
573}
574device_initcall(init_lapic_sysfs);
575
576#else /* CONFIG_PM */
577
578static void apic_pm_activate(void) { }
579
580#endif /* CONFIG_PM */
581
582static int __init apic_set_verbosity(char *str)
583{
584 if (strcmp("debug", str) == 0)
585 apic_verbosity = APIC_DEBUG;
586 else if (strcmp("verbose", str) == 0)
587 apic_verbosity = APIC_VERBOSE;
588 else
589 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
590 " use apic=verbose or apic=debug", str);
591
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800592 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595__setup("apic=", apic_set_verbosity);
596
597/*
598 * Detect and enable local APICs on non-SMP boards.
599 * Original code written by Keir Fraser.
600 * On AMD64 we trust the BIOS - if it says no APIC it is likely
601 * not correctly set up (usually the APIC timer won't work etc.)
602 */
603
604static int __init detect_init_APIC (void)
605{
606 if (!cpu_has_apic) {
607 printk(KERN_INFO "No local APIC present\n");
608 return -1;
609 }
610
611 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
612 boot_cpu_id = 0;
613 return 0;
614}
615
616void __init init_apic_mappings(void)
617{
618 unsigned long apic_phys;
619
620 /*
621 * If no local APIC can be found then set up a fake all
622 * zeroes page to simulate the local APIC and another
623 * one for the IO-APIC.
624 */
625 if (!smp_found_config && detect_init_APIC()) {
626 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
627 apic_phys = __pa(apic_phys);
628 } else
629 apic_phys = mp_lapic_addr;
630
631 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
632 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
633
634 /*
635 * Fetch the APIC ID of the BSP in case we have a
636 * default configuration (or the MP table is broken).
637 */
Andi Kleen1d3fbbf2005-09-12 18:49:24 +0200638 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 {
641 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
642 int i;
643
644 for (i = 0; i < nr_ioapics; i++) {
645 if (smp_found_config) {
646 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
647 } else {
648 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
649 ioapic_phys = __pa(ioapic_phys);
650 }
651 set_fixmap_nocache(idx, ioapic_phys);
652 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
653 __fix_to_virt(idx), ioapic_phys);
654 idx++;
655 }
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659/*
660 * This function sets up the local APIC timer, with a timeout of
661 * 'clocks' APIC bus clock. During calibration we actually call
662 * this function twice on the boot CPU, once with a bogus timeout
663 * value, second time for real. The other (noncalibrating) CPUs
664 * call this function only once, with the real, calibrated value.
665 *
666 * We do reads before writes even if unnecessary, to get around the
667 * P5 APIC double write bug.
668 */
669
670#define APIC_DIVISOR 16
671
672static void __setup_APIC_LVTT(unsigned int clocks)
673{
674 unsigned int lvtt_value, tmp_value, ver;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100675 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
678 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100679
680 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
681 lvtt_value |= APIC_LVT_MASKED;
682
Andi Kleen11a8e772006-01-11 22:46:51 +0100683 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /*
686 * Divide PICLK by 16
687 */
688 tmp_value = apic_read(APIC_TDCR);
Andi Kleen11a8e772006-01-11 22:46:51 +0100689 apic_write(APIC_TDCR, (tmp_value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
691 | APIC_TDR_DIV_16);
692
Andi Kleen11a8e772006-01-11 22:46:51 +0100693 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696static void setup_APIC_timer(unsigned int clocks)
697{
698 unsigned long flags;
699
700 local_irq_save(flags);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* wait for irq slice */
Chris McDermott33042a92006-02-11 17:55:50 -0800703 if (vxtime.hpet_address && hpet_use_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int trigger = hpet_readl(HPET_T0_CMP);
705 while (hpet_readl(HPET_COUNTER) >= trigger)
706 /* do nothing */ ;
707 while (hpet_readl(HPET_COUNTER) < trigger)
708 /* do nothing */ ;
709 } else {
710 int c1, c2;
711 outb_p(0x00, 0x43);
712 c2 = inb_p(0x40);
713 c2 |= inb_p(0x40) << 8;
Andi Kleen11a8e772006-01-11 22:46:51 +0100714 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 c1 = c2;
716 outb_p(0x00, 0x43);
717 c2 = inb_p(0x40);
718 c2 |= inb_p(0x40) << 8;
719 } while (c2 - c1 < 300);
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 __setup_APIC_LVTT(clocks);
Andi Kleen73dea472006-02-03 21:50:50 +0100722 /* Turn off PIT interrupt if we use APIC timer as main timer.
723 Only works with the PM timer right now
724 TBD fix it for HPET too. */
725 if (vxtime.mode == VXTIME_PMTMR &&
726 smp_processor_id() == boot_cpu_id &&
727 apic_runs_main_timer == 1 &&
728 !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
729 stop_timer_interrupt();
730 apic_runs_main_timer++;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 local_irq_restore(flags);
733}
734
735/*
736 * In this function we calibrate APIC bus clocks to the external
737 * timer. Unfortunately we cannot use jiffies and the timer irq
738 * to calibrate, since some later bootup code depends on getting
739 * the first irq? Ugh.
740 *
741 * We want to do the calibration only once since we
742 * want to have local timer irqs syncron. CPUs connected
743 * by the same APIC bus have the very same bus frequency.
744 * And we want to have irqs off anyways, no accidental
745 * APIC irq that way.
746 */
747
748#define TICK_COUNT 100000000
749
750static int __init calibrate_APIC_clock(void)
751{
752 int apic, apic_start, tsc, tsc_start;
753 int result;
754 /*
755 * Put whatever arbitrary (but long enough) timeout
756 * value into the APIC clock, we just want to get the
757 * counter running for calibration.
758 */
759 __setup_APIC_LVTT(1000000000);
760
761 apic_start = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100762#ifdef CONFIG_X86_PM_TIMER
763 if (apic_calibrate_pmtmr && pmtmr_ioport) {
764 pmtimer_wait(5000); /* 5ms wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 apic = apic_read(APIC_TMCCT);
Andi Kleen0c3749c2006-02-03 21:51:41 +0100766 result = (apic_start - apic) * 1000L / 5;
767 } else
768#endif
769 {
770 rdtscl(tsc_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Andi Kleen0c3749c2006-02-03 21:51:41 +0100772 do {
773 apic = apic_read(APIC_TMCCT);
774 rdtscl(tsc);
775 } while ((tsc - tsc_start) < TICK_COUNT &&
776 (apic - apic_start) < TICK_COUNT);
777
778 result = (apic_start - apic) * 1000L * cpu_khz /
779 (tsc - tsc_start);
780 }
781 printk("result %d\n", result);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
785 result / 1000 / 1000, result / 1000 % 1000);
786
787 return result * APIC_DIVISOR / HZ;
788}
789
790static unsigned int calibration_result;
791
792void __init setup_boot_APIC_clock (void)
793{
794 if (disable_apic_timer) {
795 printk(KERN_INFO "Disabling APIC timer\n");
796 return;
797 }
798
799 printk(KERN_INFO "Using local APIC timer interrupts.\n");
800 using_apic_timer = 1;
801
802 local_irq_disable();
803
804 calibration_result = calibrate_APIC_clock();
805 /*
806 * Now set up the timer for real.
807 */
808 setup_APIC_timer(calibration_result);
809
810 local_irq_enable();
811}
812
Ashok Raje6982c62005-06-25 14:54:58 -0700813void __cpuinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 local_irq_disable(); /* FIXME: Do we need this? --RR */
816 setup_APIC_timer(calibration_result);
817 local_irq_enable();
818}
819
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100820void disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 if (using_apic_timer) {
823 unsigned long v;
824
825 v = apic_read(APIC_LVTT);
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200826 /*
827 * When an illegal vector value (0-15) is written to an LVT
828 * entry and delivery mode is Fixed, the APIC may signal an
829 * illegal vector error, with out regard to whether the mask
830 * bit is set or whether an interrupt is actually seen on input.
831 *
832 * Boot sequence might call this function when the LVTT has
833 * '0' vector value. So make sure vector field is set to
834 * valid value.
835 */
836 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
837 apic_write(APIC_LVTT, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839}
840
841void enable_APIC_timer(void)
842{
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100843 int cpu = smp_processor_id();
844
845 if (using_apic_timer &&
846 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 unsigned long v;
848
849 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100850 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852}
853
Venkatesh Pallipadid25bf7e2006-01-11 22:44:24 +0100854void switch_APIC_timer_to_ipi(void *cpumask)
855{
856 cpumask_t mask = *(cpumask_t *)cpumask;
857 int cpu = smp_processor_id();
858
859 if (cpu_isset(cpu, mask) &&
860 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
861 disable_APIC_timer();
862 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
863 }
864}
865EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
866
867void smp_send_timer_broadcast_ipi(void)
868{
869 cpumask_t mask;
870
871 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
872 if (!cpus_empty(mask)) {
873 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
874 }
875}
876
877void switch_ipi_to_APIC_timer(void *cpumask)
878{
879 cpumask_t mask = *(cpumask_t *)cpumask;
880 int cpu = smp_processor_id();
881
882 if (cpu_isset(cpu, mask) &&
883 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
884 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
885 enable_APIC_timer();
886 }
887}
888EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890int setup_profiling_timer(unsigned int multiplier)
891{
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100892 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Jacob Shin17fc14f2006-06-26 13:58:47 +0200895void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
896 unsigned char msg_type, unsigned char mask)
Jacob Shin89b831e2005-11-05 17:25:53 +0100897{
Jacob Shin17fc14f2006-06-26 13:58:47 +0200898 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
899 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
Jacob Shin89b831e2005-11-05 17:25:53 +0100900 apic_write(reg, v);
901}
Jacob Shin89b831e2005-11-05 17:25:53 +0100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#undef APIC_DIVISOR
904
905/*
906 * Local timer interrupt handler. It does both profiling and
907 * process statistics/rescheduling.
908 *
909 * We do profiling in every local tick, statistics/rescheduling
910 * happen only every 'profiling multiplier' ticks. The default
911 * multiplier is 1 and it can be changed by writing the new multiplier
912 * value into /proc/profile.
913 */
914
915void smp_local_timer_interrupt(struct pt_regs *regs)
916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#ifdef CONFIG_SMP
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100919 update_process_times(user_mode(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920#endif
Andi Kleen73dea472006-02-03 21:50:50 +0100921 if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
922 main_timer_handler(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /*
924 * We take the 'long' return path, and there every subsystem
925 * grabs the appropriate locks (kernel lock/ irq lock).
926 *
Adam Henleyd5d9ca62006-09-26 10:52:28 +0200927 * We might want to decouple profiling from the 'long path',
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * and do the profiling totally in assembly.
929 *
930 * Currently this isn't too much of an issue (performance wise),
931 * we can take more than 100K local irqs per second on a 100 MHz P5.
932 */
933}
934
935/*
936 * Local APIC timer interrupt. This is the most natural way for doing
937 * local interrupts, but local timer interrupts can be emulated by
938 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
939 *
940 * [ if a single-CPU system runs an SMP kernel then we call the local
941 * interrupt as well. Thus we cannot inline the local irq ... ]
942 */
943void smp_apic_timer_interrupt(struct pt_regs *regs)
944{
945 /*
946 * the NMI deadlock-detector uses this.
947 */
948 add_pda(apic_timer_irqs, 1);
949
950 /*
951 * NOTE! We'd better ACK the irq immediately,
952 * because timer handling can be slow.
953 */
954 ack_APIC_irq();
955 /*
956 * update_process_times() expects us to have done irq_enter().
957 * Besides, if we don't timer interrupts ignore the global
958 * interrupt lock, which is the WrongThing (tm) to do.
959 */
Andi Kleen95833c82006-01-11 22:44:36 +0100960 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 irq_enter();
962 smp_local_timer_interrupt(regs);
963 irq_exit();
964}
965
966/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200967 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
969 * Thus far, the major user of this is IBM's Summit2 series:
970 *
Linus Torvalds637029c2006-02-27 20:41:56 -0800971 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * multi-chassis. Use available data to take a good guess.
973 * If in doubt, go HPET.
974 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +0200975__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 int i, clusters, zeros;
978 unsigned id;
979 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
980
Suresh Siddha376ec332005-05-16 21:53:32 -0700981 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 for (i = 0; i < NR_CPUS; i++) {
984 id = bios_cpu_apicid[i];
985 if (id != BAD_APICID)
986 __set_bit(APIC_CLUSTERID(id), clustermap);
987 }
988
989 /* Problem: Partially populated chassis may not have CPUs in some of
990 * the APIC clusters they have been allocated. Only present CPUs have
991 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
992 * clusters are allocated sequentially, count zeros only if they are
993 * bounded by ones.
994 */
995 clusters = 0;
996 zeros = 0;
997 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
998 if (test_bit(i, clustermap)) {
999 clusters += 1 + zeros;
1000 zeros = 0;
1001 } else
1002 ++zeros;
1003 }
1004
1005 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02001006 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 * May have to revisit this when multi-core + hyperthreaded CPUs come
1008 * out, but AFAIK this will work even for them.
1009 */
1010 return (clusters > 2);
1011}
1012
1013/*
1014 * This interrupt should _never_ happen with our APIC/SMP architecture
1015 */
1016asmlinkage void smp_spurious_interrupt(void)
1017{
1018 unsigned int v;
Andi Kleen95833c82006-01-11 22:44:36 +01001019 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 irq_enter();
1021 /*
1022 * Check if this really is a spurious interrupt and ACK it
1023 * if it is a vectored one. Just in case...
1024 * Spurious interrupts should not be ACKed.
1025 */
1026 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1027 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1028 ack_APIC_irq();
1029
1030#if 0
1031 static unsigned long last_warning;
1032 static unsigned long skipped;
1033
1034 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1035 if (time_before(last_warning+30*HZ,jiffies)) {
1036 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1037 smp_processor_id(), skipped);
1038 last_warning = jiffies;
1039 skipped = 0;
1040 } else {
1041 skipped++;
1042 }
1043#endif
1044 irq_exit();
1045}
1046
1047/*
1048 * This interrupt should never happen with our APIC/SMP architecture
1049 */
1050
1051asmlinkage void smp_error_interrupt(void)
1052{
1053 unsigned int v, v1;
1054
Andi Kleen95833c82006-01-11 22:44:36 +01001055 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 irq_enter();
1057 /* First tickle the hardware, only then report what went on. -- REW */
1058 v = apic_read(APIC_ESR);
1059 apic_write(APIC_ESR, 0);
1060 v1 = apic_read(APIC_ESR);
1061 ack_APIC_irq();
1062 atomic_inc(&irq_err_count);
1063
1064 /* Here is what the APIC error bits mean:
1065 0: Send CS error
1066 1: Receive CS error
1067 2: Send accept error
1068 3: Receive accept error
1069 4: Reserved
1070 5: Send illegal vector
1071 6: Received illegal vector
1072 7: Illegal register address
1073 */
1074 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1075 smp_processor_id(), v , v1);
1076 irq_exit();
1077}
1078
1079int disable_apic;
1080
1081/*
1082 * This initializes the IO-APIC and APIC hardware if this is
1083 * a UP kernel.
1084 */
1085int __init APIC_init_uniprocessor (void)
1086{
1087 if (disable_apic) {
1088 printk(KERN_INFO "Apic disabled\n");
1089 return -1;
1090 }
1091 if (!cpu_has_apic) {
1092 disable_apic = 1;
1093 printk(KERN_INFO "Apic disabled by BIOS\n");
1094 return -1;
1095 }
1096
1097 verify_local_APIC();
1098
1099 connect_bsp_APIC();
1100
Andi Kleen357e11d2005-09-12 18:49:24 +02001101 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
Andi Kleen11a8e772006-01-11 22:46:51 +01001102 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 setup_local_APIC();
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
Andi Kleen7f11d8a2006-09-26 10:52:29 +02001107 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 else
1109 nr_ioapics = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 setup_boot_APIC_clock();
Andi Kleen75152112005-05-16 21:53:34 -07001111 check_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
1113}
1114
1115static __init int setup_disableapic(char *str)
1116{
1117 disable_apic = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121static __init int setup_nolapic(char *str)
1122{
1123 disable_apic = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001124 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static __init int setup_noapictimer(char *str)
1128{
Andi Kleen73dea472006-02-03 21:50:50 +01001129 if (str[0] != ' ' && str[0] != 0)
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 disable_apic_timer = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001132 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Andi Kleen73dea472006-02-03 21:50:50 +01001135static __init int setup_apicmaintimer(char *str)
1136{
1137 apic_runs_main_timer = 1;
1138 nohpet = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001139 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001140}
1141__setup("apicmaintimer", setup_apicmaintimer);
1142
1143static __init int setup_noapicmaintimer(char *str)
1144{
1145 apic_runs_main_timer = -1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001146 return 1;
Andi Kleen73dea472006-02-03 21:50:50 +01001147}
1148__setup("noapicmaintimer", setup_noapicmaintimer);
1149
Andi Kleen0c3749c2006-02-03 21:51:41 +01001150static __init int setup_apicpmtimer(char *s)
1151{
1152 apic_calibrate_pmtmr = 1;
Andi Kleen7fd67842006-02-16 23:42:07 +01001153 notsc_setup(NULL);
Andi Kleen0c3749c2006-02-03 21:51:41 +01001154 return setup_apicmaintimer(NULL);
1155}
1156__setup("apicpmtimer", setup_apicpmtimer);
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/* dummy parsing: see setup.c */
1159
1160__setup("disableapic", setup_disableapic);
1161__setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1162
1163__setup("noapictimer", setup_noapictimer);
1164
1165/* no "lapic" flag - we only use the lapic when the BIOS tells us so. */