- Peter Anvin: more P4 configuration parsing
[davej-history.git] / arch / i386 / kernel / apic.c
blobfc54896f8e272b18175f67faba9098858bd89af3
1 /*
2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
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.
14 #include <linux/config.h>
15 #include <linux/init.h>
17 #include <linux/mm.h>
18 #include <linux/irq.h>
19 #include <linux/delay.h>
20 #include <linux/bootmem.h>
21 #include <linux/smp_lock.h>
22 #include <linux/interrupt.h>
23 #include <linux/mc146818rtc.h>
24 #include <linux/kernel_stat.h>
26 #include <asm/smp.h>
27 #include <asm/mtrr.h>
28 #include <asm/mpspec.h>
29 #include <asm/pgalloc.h>
31 int prof_multiplier[NR_CPUS] = { 1, };
32 int prof_old_multiplier[NR_CPUS] = { 1, };
33 int prof_counter[NR_CPUS] = { 1, };
35 int get_maxlvt(void)
37 unsigned int v, ver, maxlvt;
39 v = apic_read(APIC_LVR);
40 ver = GET_APIC_VERSION(v);
41 /* 82489DXs do not report # of LVT entries. */
42 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
43 return maxlvt;
46 static void clear_local_APIC(void)
48 int maxlvt;
49 unsigned long v;
51 maxlvt = get_maxlvt();
54 * Careful: we have to set masks only first to deassert
55 * any level-triggered sources.
57 v = apic_read(APIC_LVTT);
58 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
59 v = apic_read(APIC_LVT0);
60 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
61 v = apic_read(APIC_LVT1);
62 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
63 if (maxlvt >= 3) {
64 v = apic_read(APIC_LVTERR);
65 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
67 if (maxlvt >= 4) {
68 v = apic_read(APIC_LVTPC);
69 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
73 * Clean APIC state for other OSs:
75 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
76 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
77 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
78 if (maxlvt >= 3)
79 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
80 if (maxlvt >= 4)
81 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
84 void __init connect_bsp_APIC(void)
86 if (pic_mode) {
88 * Do not trust the local APIC being empty at bootup.
90 clear_local_APIC();
92 * PIC mode, enable symmetric IO mode in the IMCR,
93 * i.e. connect BSP's local APIC to INT and NMI lines.
95 printk("leaving PIC mode, enabling symmetric IO mode.\n");
96 outb(0x70, 0x22);
97 outb(0x01, 0x23);
101 void disconnect_bsp_APIC(void)
103 if (pic_mode) {
105 * Put the board back into PIC mode (has an effect
106 * only on certain older boards). Note that APIC
107 * interrupts, including IPIs, won't work beyond
108 * this point! The only exception are INIT IPIs.
110 printk("disabling symmetric IO mode, entering PIC mode.\n");
111 outb(0x70, 0x22);
112 outb(0x00, 0x23);
116 void disable_local_APIC(void)
118 unsigned long value;
120 clear_local_APIC();
123 * Disable APIC (implies clearing of registers
124 * for 82489DX!).
126 value = apic_read(APIC_SPIV);
127 value &= ~(1<<8);
128 apic_write_around(APIC_SPIV, value);
132 * This is to verify that we're looking at a real local APIC.
133 * Check these against your board if the CPUs aren't getting
134 * started for no apparent reason.
136 int __init verify_local_APIC(void)
138 unsigned int reg0, reg1;
141 * The version register is read-only in a real APIC.
143 reg0 = apic_read(APIC_LVR);
144 Dprintk("Getting VERSION: %x\n", reg0);
145 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
146 reg1 = apic_read(APIC_LVR);
147 Dprintk("Getting VERSION: %x\n", reg1);
150 * The two version reads above should print the same
151 * numbers. If the second one is different, then we
152 * poke at a non-APIC.
154 if (reg1 != reg0)
155 return 0;
158 * Check if the version looks reasonably.
160 reg1 = GET_APIC_VERSION(reg0);
161 if (reg1 == 0x00 || reg1 == 0xff)
162 return 0;
163 reg1 = get_maxlvt();
164 if (reg1 < 0x02 || reg1 == 0xff)
165 return 0;
168 * The ID register is read/write in a real APIC.
170 reg0 = apic_read(APIC_ID);
171 Dprintk("Getting ID: %x\n", reg0);
172 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
173 reg1 = apic_read(APIC_ID);
174 Dprintk("Getting ID: %x\n", reg1);
175 apic_write(APIC_ID, reg0);
176 if (reg1 != (reg0 ^ APIC_ID_MASK))
177 return 0;
180 * The next two are just to see if we have sane values.
181 * They're only really relevant if we're in Virtual Wire
182 * compatibility mode, but most boxes are anymore.
184 reg0 = apic_read(APIC_LVT0);
185 Dprintk("Getting LVT0: %x\n", reg0);
186 reg1 = apic_read(APIC_LVT1);
187 Dprintk("Getting LVT1: %x\n", reg1);
189 return 1;
192 void __init sync_Arb_IDs(void)
195 * Wait for idle.
197 apic_wait_icr_idle();
199 Dprintk("Synchronizing Arb IDs.\n");
200 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
201 | APIC_DM_INIT);
204 extern void __error_in_apic_c (void);
206 void __init setup_local_APIC (void)
208 unsigned long value, ver, maxlvt;
210 value = apic_read(APIC_LVR);
211 ver = GET_APIC_VERSION(value);
213 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
214 __error_in_apic_c();
217 * Double-check wether this APIC is really registered.
219 if (!test_bit(GET_APIC_ID(apic_read(APIC_ID)), &phys_cpu_present_map))
220 BUG();
223 * Set up LVT0, LVT1:
225 * set up through-local-APIC on the BP's LINT0. This is not
226 * strictly necessery in pure symmetric-IO mode, but sometimes
227 * we delegate interrupts to the 8259A.
230 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
232 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
233 if (!smp_processor_id() && (pic_mode || !value)) {
234 value = APIC_DM_EXTINT;
235 printk("enabled ExtINT on CPU#%d\n", smp_processor_id());
236 } else {
237 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
238 printk("masked ExtINT on CPU#%d\n", smp_processor_id());
240 apic_write_around(APIC_LVT0, value);
243 * only the BP should see the LINT1 NMI signal, obviously.
245 if (!smp_processor_id())
246 value = APIC_DM_NMI;
247 else
248 value = APIC_DM_NMI | APIC_LVT_MASKED;
249 if (!APIC_INTEGRATED(ver)) /* 82489DX */
250 value |= APIC_LVT_LEVEL_TRIGGER;
251 apic_write_around(APIC_LVT1, value);
253 if (APIC_INTEGRATED(ver)) { /* !82489DX */
254 maxlvt = get_maxlvt();
255 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
256 apic_write(APIC_ESR, 0);
257 value = apic_read(APIC_ESR);
258 printk("ESR value before enabling vector: %08lx\n", value);
260 value = ERROR_APIC_VECTOR; // enables sending errors
261 apic_write_around(APIC_LVTERR, value);
263 * spec says clear errors after enabling vector.
265 if (maxlvt > 3)
266 apic_write(APIC_ESR, 0);
267 value = apic_read(APIC_ESR);
268 printk("ESR value after enabling vector: %08lx\n", value);
269 } else
270 printk("No ESR for 82489DX.\n");
273 * Set Task Priority to 'accept all'. We never change this
274 * later on.
276 value = apic_read(APIC_TASKPRI);
277 value &= ~APIC_TPRI_MASK;
278 apic_write_around(APIC_TASKPRI, value);
281 * Set up the logical destination ID and put the
282 * APIC into flat delivery mode.
284 value = apic_read(APIC_LDR);
285 value &= ~APIC_LDR_MASK;
286 value |= (1<<(smp_processor_id()+24));
287 apic_write_around(APIC_LDR, value);
290 * Must be "all ones" explicitly for 82489DX.
292 apic_write_around(APIC_DFR, 0xffffffff);
295 * Now that we are all set up, enable the APIC
297 value = apic_read(APIC_SPIV);
298 value &= ~APIC_VECTOR_MASK;
300 * Enable APIC
302 value |= (1<<8);
305 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
306 * certain networking cards. If high frequency interrupts are
307 * happening on a particular IOAPIC pin, plus the IOAPIC routing
308 * entry is masked/unmasked at a high rate as well then sooner or
309 * later IOAPIC line gets 'stuck', no more interrupts are received
310 * from the device. If focus CPU is disabled then the hang goes
311 * away, oh well :-(
313 * [ This bug can be reproduced easily with a level-triggered
314 * PCI Ne2000 networking cards and PII/PIII processors, dual
315 * BX chipset. ]
317 #if 0
318 /* Enable focus processor (bit==0) */
319 value &= ~(1<<9);
320 #else
321 /* Disable focus processor (bit==1) */
322 value |= (1<<9);
323 #endif
325 * Set spurious IRQ vector
327 value |= SPURIOUS_APIC_VECTOR;
328 apic_write_around(APIC_SPIV, value);
331 void __init init_apic_mappings(void)
333 unsigned long apic_phys;
335 if (smp_found_config) {
336 apic_phys = mp_lapic_addr;
337 } else {
339 * set up a fake all zeroes page to simulate the
340 * local APIC and another one for the IO-APIC. We
341 * could use the real zero-page, but it's safer
342 * this way if some buggy code writes to this page ...
344 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
345 apic_phys = __pa(apic_phys);
347 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
348 Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
351 * Fetch the APIC ID of the BSP in case we have a
352 * default configuration (or the MP table is broken).
354 if (boot_cpu_id == -1U)
355 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
357 #ifdef CONFIG_X86_IO_APIC
359 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
360 int i;
362 for (i = 0; i < nr_ioapics; i++) {
363 if (smp_found_config) {
364 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
365 } else {
366 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
367 ioapic_phys = __pa(ioapic_phys);
369 set_fixmap_nocache(idx, ioapic_phys);
370 Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
371 __fix_to_virt(idx), ioapic_phys);
372 idx++;
375 #endif
379 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
380 * per second. We assume that the caller has already set up the local
381 * APIC.
383 * The APIC timer is not exactly sync with the external timer chip, it
384 * closely follows bus clocks.
388 * The timer chip is already set up at HZ interrupts per second here,
389 * but we do not accept timer interrupts yet. We only allow the BP
390 * to calibrate.
392 static unsigned int __init get_8254_timer_count(void)
394 extern spinlock_t i8253_lock;
395 unsigned long flags;
397 unsigned int count;
399 spin_lock_irqsave(&i8253_lock, flags);
401 outb_p(0x00, 0x43);
402 count = inb_p(0x40);
403 count |= inb_p(0x40) << 8;
405 spin_unlock_irqrestore(&i8253_lock, flags);
407 return count;
410 void __init wait_8254_wraparound(void)
412 unsigned int curr_count, prev_count=~0;
413 int delta;
415 curr_count = get_8254_timer_count();
417 do {
418 prev_count = curr_count;
419 curr_count = get_8254_timer_count();
420 delta = curr_count-prev_count;
423 * This limit for delta seems arbitrary, but it isn't, it's
424 * slightly above the level of error a buggy Mercury/Neptune
425 * chipset timer can cause.
428 } while (delta < 300);
432 * This function sets up the local APIC timer, with a timeout of
433 * 'clocks' APIC bus clock. During calibration we actually call
434 * this function twice on the boot CPU, once with a bogus timeout
435 * value, second time for real. The other (noncalibrating) CPUs
436 * call this function only once, with the real, calibrated value.
438 * We do reads before writes even if unnecessary, to get around the
439 * P5 APIC double write bug.
442 #define APIC_DIVISOR 16
444 void __setup_APIC_LVTT(unsigned int clocks)
446 unsigned int lvtt1_value, tmp_value;
448 lvtt1_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) |
449 APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
450 apic_write_around(APIC_LVTT, lvtt1_value);
453 * Divide PICLK by 16
455 tmp_value = apic_read(APIC_TDCR);
456 apic_write_around(APIC_TDCR, (tmp_value
457 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
458 | APIC_TDR_DIV_16);
460 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
463 void setup_APIC_timer(void * data)
465 unsigned int clocks = (unsigned int) data, slice, t0, t1;
466 unsigned long flags;
467 int delta;
469 __save_flags(flags);
470 __sti();
472 * ok, Intel has some smart code in their APIC that knows
473 * if a CPU was in 'hlt' lowpower mode, and this increases
474 * its APIC arbitration priority. To avoid the external timer
475 * IRQ APIC event being in synchron with the APIC clock we
476 * introduce an interrupt skew to spread out timer events.
478 * The number of slices within a 'big' timeslice is smp_num_cpus+1
481 slice = clocks / (smp_num_cpus+1);
482 printk("cpu: %d, clocks: %d, slice: %d\n",
483 smp_processor_id(), clocks, slice);
486 * Wait for IRQ0's slice:
488 wait_8254_wraparound();
490 __setup_APIC_LVTT(clocks);
492 t0 = apic_read(APIC_TMICT)*APIC_DIVISOR;
493 /* Wait till TMCCT gets reloaded from TMICT... */
494 do {
495 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
496 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
497 } while (delta >= 0);
498 /* Now wait for our slice for real. */
499 do {
500 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
501 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
502 } while (delta < 0);
504 __setup_APIC_LVTT(clocks);
506 printk("CPU%d<T0:%d,T1:%d,D:%d,S:%d,C:%d>\n",
507 smp_processor_id(), t0, t1, delta, slice, clocks);
509 __restore_flags(flags);
513 * In this function we calibrate APIC bus clocks to the external
514 * timer. Unfortunately we cannot use jiffies and the timer irq
515 * to calibrate, since some later bootup code depends on getting
516 * the first irq? Ugh.
518 * We want to do the calibration only once since we
519 * want to have local timer irqs syncron. CPUs connected
520 * by the same APIC bus have the very same bus frequency.
521 * And we want to have irqs off anyways, no accidental
522 * APIC irq that way.
525 int __init calibrate_APIC_clock(void)
527 unsigned long long t1 = 0, t2 = 0;
528 long tt1, tt2;
529 long result;
530 int i;
531 const int LOOPS = HZ/10;
533 printk("calibrating APIC timer ...\n");
536 * Put whatever arbitrary (but long enough) timeout
537 * value into the APIC clock, we just want to get the
538 * counter running for calibration.
540 __setup_APIC_LVTT(1000000000);
543 * The timer chip counts down to zero. Let's wait
544 * for a wraparound to start exact measurement:
545 * (the current tick might have been already half done)
548 wait_8254_wraparound();
551 * We wrapped around just now. Let's start:
553 if (cpu_has_tsc)
554 rdtscll(t1);
555 tt1 = apic_read(APIC_TMCCT);
558 * Let's wait LOOPS wraprounds:
560 for (i = 0; i < LOOPS; i++)
561 wait_8254_wraparound();
563 tt2 = apic_read(APIC_TMCCT);
564 if (cpu_has_tsc)
565 rdtscll(t2);
568 * The APIC bus clock counter is 32 bits only, it
569 * might have overflown, but note that we use signed
570 * longs, thus no extra care needed.
572 * underflown to be exact, as the timer counts down ;)
575 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
577 if (cpu_has_tsc)
578 printk("..... CPU clock speed is %ld.%04ld MHz.\n",
579 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
580 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
582 printk("..... host bus clock speed is %ld.%04ld MHz.\n",
583 result/(1000000/HZ),
584 result%(1000000/HZ));
586 return result;
589 static unsigned int calibration_result;
591 void __init setup_APIC_clocks (void)
593 __cli();
595 calibration_result = calibrate_APIC_clock();
597 * Now set up the timer for real.
599 setup_APIC_timer((void *)calibration_result);
601 __sti();
603 /* and update all other cpus */
604 smp_call_function(setup_APIC_timer, (void *)calibration_result, 1, 1);
608 * the frequency of the profiling timer can be changed
609 * by writing a multiplier value into /proc/profile.
611 int setup_profiling_timer(unsigned int multiplier)
613 int i;
616 * Sanity check. [at least 500 APIC cycles should be
617 * between APIC interrupts as a rule of thumb, to avoid
618 * irqs flooding us]
620 if ( (!multiplier) || (calibration_result/multiplier < 500))
621 return -EINVAL;
624 * Set the new multiplier for each CPU. CPUs don't start using the
625 * new values until the next timer interrupt in which they do process
626 * accounting. At that time they also adjust their APIC timers
627 * accordingly.
629 for (i = 0; i < NR_CPUS; ++i)
630 prof_multiplier[i] = multiplier;
632 return 0;
635 #undef APIC_DIVISOR
638 * Local timer interrupt handler. It does both profiling and
639 * process statistics/rescheduling.
641 * We do profiling in every local tick, statistics/rescheduling
642 * happen only every 'profiling multiplier' ticks. The default
643 * multiplier is 1 and it can be changed by writing the new multiplier
644 * value into /proc/profile.
647 inline void smp_local_timer_interrupt(struct pt_regs * regs)
649 int user = user_mode(regs);
650 int cpu = smp_processor_id();
653 * The profiling function is SMP safe. (nothing can mess
654 * around with "current", and the profiling counters are
655 * updated with atomic operations). This is especially
656 * useful with a profiling multiplier != 1
658 if (!user)
659 x86_do_profile(regs->eip);
661 if (--prof_counter[cpu] <= 0) {
663 * The multiplier may have changed since the last time we got
664 * to this point as a result of the user writing to
665 * /proc/profile. In this case we need to adjust the APIC
666 * timer accordingly.
668 * Interrupts are already masked off at this point.
670 prof_counter[cpu] = prof_multiplier[cpu];
671 if (prof_counter[cpu] != prof_old_multiplier[cpu]) {
672 __setup_APIC_LVTT(calibration_result/prof_counter[cpu]);
673 prof_old_multiplier[cpu] = prof_counter[cpu];
676 #ifdef CONFIG_SMP
677 update_process_times(user);
678 #endif
682 * We take the 'long' return path, and there every subsystem
683 * grabs the apropriate locks (kernel lock/ irq lock).
685 * we might want to decouple profiling from the 'long path',
686 * and do the profiling totally in assembly.
688 * Currently this isn't too much of an issue (performance wise),
689 * we can take more than 100K local irqs per second on a 100 MHz P5.
694 * Local APIC timer interrupt. This is the most natural way for doing
695 * local interrupts, but local timer interrupts can be emulated by
696 * broadcast interrupts too. [in case the hw doesnt support APIC timers]
698 * [ if a single-CPU system runs an SMP kernel then we call the local
699 * interrupt as well. Thus we cannot inline the local irq ... ]
701 unsigned int apic_timer_irqs [NR_CPUS];
703 void smp_apic_timer_interrupt(struct pt_regs * regs)
705 int cpu = smp_processor_id();
708 * the NMI deadlock-detector uses this.
710 apic_timer_irqs[cpu]++;
713 * NOTE! We'd better ACK the irq immediately,
714 * because timer handling can be slow.
716 ack_APIC_irq();
718 * update_process_times() expects us to have done irq_enter().
719 * Besides, if we don't timer interrupts ignore the global
720 * interrupt lock, which is the WrongThing (tm) to do.
722 irq_enter(cpu, 0);
723 smp_local_timer_interrupt(regs);
724 irq_exit(cpu, 0);
728 * This interrupt should _never_ happen with our APIC/SMP architecture
730 asmlinkage void smp_spurious_interrupt(void)
732 unsigned long v;
735 * Check if this really is a spurious interrupt and ACK it
736 * if it is a vectored one. Just in case...
737 * Spurious interrupts should not be ACKed.
739 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
740 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
741 ack_APIC_irq();
743 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
744 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
745 smp_processor_id());
749 * This interrupt should never happen with our APIC/SMP architecture
752 asmlinkage void smp_error_interrupt(void)
754 unsigned long v, v1;
756 /* First tickle the hardware, only then report what went on. -- REW */
757 v = apic_read(APIC_ESR);
758 apic_write(APIC_ESR, 0);
759 v1 = apic_read(APIC_ESR);
760 ack_APIC_irq();
761 irq_err_count++;
763 /* Here is what the APIC error bits mean:
764 0: Send CS error
765 1: Receive CS error
766 2: Send accept error
767 3: Receive accept error
768 4: Reserved
769 5: Send illegal vector
770 6: Received illegal vector
771 7: Illegal register address
773 printk (KERN_ERR "APIC error on CPU%d: %02lx(%02lx)\n",
774 smp_processor_id(), v , v1);