Import 2.4.0-test6pre4
[davej-history.git] / arch / i386 / kernel / apic.c
bloba839790f898f3ded16e25acf740e93bd35033011
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
13 #include <linux/config.h>
14 #include <linux/init.h>
16 #include <linux/mm.h>
17 #include <linux/irq.h>
18 #include <linux/delay.h>
19 #include <linux/bootmem.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
22 #include <linux/mc146818rtc.h>
23 #include <linux/kernel_stat.h>
25 #include <asm/smp.h>
26 #include <asm/mtrr.h>
27 #include <asm/mpspec.h>
28 #include <asm/pgalloc.h>
30 int prof_multiplier[NR_CPUS] = { 1, };
31 int prof_old_multiplier[NR_CPUS] = { 1, };
32 int prof_counter[NR_CPUS] = { 1, };
34 int get_maxlvt(void)
36 unsigned int v, ver, maxlvt;
38 v = apic_read(APIC_LVR);
39 ver = GET_APIC_VERSION(v);
40 /* 82489DXs do not report # of LVT entries. */
41 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
42 return maxlvt;
45 static void clear_local_APIC(void)
47 int maxlvt;
48 unsigned long v;
50 maxlvt = get_maxlvt();
53 * Careful: we have to set masks only first to deassert
54 * any level-triggered sources.
56 v = apic_read(APIC_LVTT);
57 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
58 v = apic_read(APIC_LVT0);
59 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
60 v = apic_read(APIC_LVT1);
61 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
62 if (maxlvt >= 3) {
63 v = apic_read(APIC_LVTERR);
64 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
66 if (maxlvt >= 4) {
67 v = apic_read(APIC_LVTPC);
68 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
72 * Clean APIC state for other OSs:
74 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
75 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
76 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
77 if (maxlvt >= 3)
78 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
79 if (maxlvt >= 4)
80 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
83 void __init connect_bsp_APIC(void)
85 if (pic_mode) {
87 * Do not trust the local APIC being empty at bootup.
89 clear_local_APIC();
91 * PIC mode, enable symmetric IO mode in the IMCR,
92 * i.e. connect BSP's local APIC to INT and NMI lines.
94 printk("leaving PIC mode, enabling symmetric IO mode.\n");
95 outb(0x70, 0x22);
96 outb(0x01, 0x23);
100 void disconnect_bsp_APIC(void)
102 if (pic_mode) {
104 * Put the board back into PIC mode (has an effect
105 * only on certain older boards). Note that APIC
106 * interrupts, including IPIs, won't work beyond
107 * this point! The only exception are INIT IPIs.
109 printk("disabling symmetric IO mode, entering PIC mode.\n");
110 outb(0x70, 0x22);
111 outb(0x00, 0x23);
115 void disable_local_APIC(void)
117 unsigned long value;
119 clear_local_APIC();
122 * Disable APIC (implies clearing of registers
123 * for 82489DX!).
125 value = apic_read(APIC_SPIV);
126 value &= ~(1<<8);
127 apic_write_around(APIC_SPIV, value);
130 void __init sync_Arb_IDs(void)
133 * Wait for idle.
135 apic_wait_icr_idle();
137 Dprintk("Synchronizing Arb IDs.\n");
138 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
139 | APIC_DM_INIT);
142 extern void __error_in_apic_c (void);
144 void __init setup_local_APIC (void)
146 unsigned long value, ver, maxlvt;
148 value = apic_read(APIC_LVR);
149 ver = GET_APIC_VERSION(value);
151 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
152 __error_in_apic_c();
155 * Double-check wether this APIC is really registered.
157 if (!test_bit(GET_APIC_ID(apic_read(APIC_ID)), &phys_cpu_present_map))
158 BUG();
160 value = apic_read(APIC_SPIV);
161 value &= ~APIC_VECTOR_MASK;
163 * Enable APIC
165 value |= (1<<8);
168 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
169 * certain networking cards. If high frequency interrupts are
170 * happening on a particular IOAPIC pin, plus the IOAPIC routing
171 * entry is masked/unmasked at a high rate as well then sooner or
172 * later IOAPIC line gets 'stuck', no more interrupts are received
173 * from the device. If focus CPU is disabled then the hang goes
174 * away, oh well :-(
176 * [ This bug can be reproduced easily with a level-triggered
177 * PCI Ne2000 networking cards and PII/PIII processors, dual
178 * BX chipset. ]
180 #if 0
181 /* Enable focus processor (bit==0) */
182 value &= ~(1<<9);
183 #else
184 /* Disable focus processor (bit==1) */
185 value |= (1<<9);
186 #endif
188 * Set spurious IRQ vector
190 value |= SPURIOUS_APIC_VECTOR;
191 apic_write_around(APIC_SPIV, value);
194 * Set up LVT0, LVT1:
196 * set up through-local-APIC on the BP's LINT0. This is not
197 * strictly necessery in pure symmetric-IO mode, but sometimes
198 * we delegate interrupts to the 8259A.
201 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
203 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
204 if (!smp_processor_id() && (pic_mode || !value)) {
205 value = APIC_DM_EXTINT;
206 printk("enabled ExtINT on CPU#%d\n", smp_processor_id());
207 } else {
208 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
209 printk("masked ExtINT on CPU#%d\n", smp_processor_id());
211 apic_write_around(APIC_LVT0, value);
214 * only the BP should see the LINT1 NMI signal, obviously.
216 if (!smp_processor_id())
217 value = APIC_DM_NMI;
218 else
219 value = APIC_DM_NMI | APIC_LVT_MASKED;
220 if (!APIC_INTEGRATED(ver)) /* 82489DX */
221 value |= APIC_LVT_LEVEL_TRIGGER;
222 apic_write_around(APIC_LVT1, value);
224 if (APIC_INTEGRATED(ver)) { /* !82489DX */
225 maxlvt = get_maxlvt();
226 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
227 apic_write(APIC_ESR, 0);
228 value = apic_read(APIC_ESR);
229 printk("ESR value before enabling vector: %08lx\n", value);
231 value = ERROR_APIC_VECTOR; // enables sending errors
232 apic_write_around(APIC_LVTERR, value);
234 * spec says clear errors after enabling vector.
236 if (maxlvt > 3)
237 apic_write(APIC_ESR, 0);
238 value = apic_read(APIC_ESR);
239 printk("ESR value after enabling vector: %08lx\n", value);
240 } else
241 printk("No ESR for 82489DX.\n");
244 * Set Task Priority to 'accept all'. We never change this
245 * later on.
247 value = apic_read(APIC_TASKPRI);
248 value &= ~APIC_TPRI_MASK;
249 apic_write_around(APIC_TASKPRI, value);
252 * Set up the logical destination ID and put the
253 * APIC into flat delivery mode.
255 value = apic_read(APIC_LDR);
256 value &= ~APIC_LDR_MASK;
257 value |= (1<<(smp_processor_id()+24));
258 apic_write_around(APIC_LDR, value);
261 * Must be "all ones" explicitly for 82489DX.
263 apic_write_around(APIC_DFR, 0xffffffff);
266 void __init init_apic_mappings(void)
268 unsigned long apic_phys;
270 if (smp_found_config) {
271 apic_phys = mp_lapic_addr;
272 } else {
274 * set up a fake all zeroes page to simulate the
275 * local APIC and another one for the IO-APIC. We
276 * could use the real zero-page, but it's safer
277 * this way if some buggy code writes to this page ...
279 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
280 apic_phys = __pa(apic_phys);
282 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
283 Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
286 * Fetch the APIC ID of the BSP in case we have a
287 * default configuration (or the MP table is broken).
289 if (boot_cpu_id == -1U)
290 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
292 #ifdef CONFIG_X86_IO_APIC
294 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
295 int i;
297 for (i = 0; i < nr_ioapics; i++) {
298 if (smp_found_config) {
299 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
300 } else {
301 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
302 ioapic_phys = __pa(ioapic_phys);
304 set_fixmap_nocache(idx, ioapic_phys);
305 Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
306 __fix_to_virt(idx), ioapic_phys);
307 idx++;
310 #endif
314 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
315 * per second. We assume that the caller has already set up the local
316 * APIC.
318 * The APIC timer is not exactly sync with the external timer chip, it
319 * closely follows bus clocks.
323 * The timer chip is already set up at HZ interrupts per second here,
324 * but we do not accept timer interrupts yet. We only allow the BP
325 * to calibrate.
327 static unsigned int __init get_8254_timer_count(void)
329 extern spinlock_t i8253_lock;
330 unsigned long flags;
332 unsigned int count;
334 spin_lock_irqsave(&i8253_lock, flags);
336 outb_p(0x00, 0x43);
337 count = inb_p(0x40);
338 count |= inb_p(0x40) << 8;
340 spin_unlock_irqrestore(&i8253_lock, flags);
342 return count;
345 void __init wait_8254_wraparound(void)
347 unsigned int curr_count, prev_count=~0;
348 int delta;
350 curr_count = get_8254_timer_count();
352 do {
353 prev_count = curr_count;
354 curr_count = get_8254_timer_count();
355 delta = curr_count-prev_count;
358 * This limit for delta seems arbitrary, but it isn't, it's
359 * slightly above the level of error a buggy Mercury/Neptune
360 * chipset timer can cause.
363 } while (delta < 300);
367 * This function sets up the local APIC timer, with a timeout of
368 * 'clocks' APIC bus clock. During calibration we actually call
369 * this function twice on the boot CPU, once with a bogus timeout
370 * value, second time for real. The other (noncalibrating) CPUs
371 * call this function only once, with the real, calibrated value.
373 * We do reads before writes even if unnecessary, to get around the
374 * P5 APIC double write bug.
377 #define APIC_DIVISOR 16
379 void __setup_APIC_LVTT(unsigned int clocks)
381 unsigned int lvtt1_value, tmp_value;
383 lvtt1_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) |
384 APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
385 apic_write_around(APIC_LVTT, lvtt1_value);
388 * Divide PICLK by 16
390 tmp_value = apic_read(APIC_TDCR);
391 apic_write_around(APIC_TDCR, (tmp_value
392 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
393 | APIC_TDR_DIV_16);
395 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
398 void setup_APIC_timer(void * data)
400 unsigned int clocks = (unsigned int) data, slice, t0, t1;
401 unsigned long flags;
402 int delta;
404 __save_flags(flags);
405 __sti();
407 * ok, Intel has some smart code in their APIC that knows
408 * if a CPU was in 'hlt' lowpower mode, and this increases
409 * its APIC arbitration priority. To avoid the external timer
410 * IRQ APIC event being in synchron with the APIC clock we
411 * introduce an interrupt skew to spread out timer events.
413 * The number of slices within a 'big' timeslice is smp_num_cpus+1
416 slice = clocks / (smp_num_cpus+1);
417 printk("cpu: %d, clocks: %d, slice: %d\n",
418 smp_processor_id(), clocks, slice);
421 * Wait for IRQ0's slice:
423 wait_8254_wraparound();
425 __setup_APIC_LVTT(clocks);
427 t0 = apic_read(APIC_TMICT)*APIC_DIVISOR;
428 /* Wait till TMCCT gets reloaded from TMICT... */
429 do {
430 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
431 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
432 } while (delta >= 0);
433 /* Now wait for our slice for real. */
434 do {
435 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
436 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
437 } while (delta < 0);
439 __setup_APIC_LVTT(clocks);
441 printk("CPU%d<T0:%d,T1:%d,D:%d,S:%d,C:%d>\n",
442 smp_processor_id(), t0, t1, delta, slice, clocks);
444 __restore_flags(flags);
448 * In this function we calibrate APIC bus clocks to the external
449 * timer. Unfortunately we cannot use jiffies and the timer irq
450 * to calibrate, since some later bootup code depends on getting
451 * the first irq? Ugh.
453 * We want to do the calibration only once since we
454 * want to have local timer irqs syncron. CPUs connected
455 * by the same APIC bus have the very same bus frequency.
456 * And we want to have irqs off anyways, no accidental
457 * APIC irq that way.
460 int __init calibrate_APIC_clock(void)
462 unsigned long long t1 = 0, t2 = 0;
463 long tt1, tt2;
464 long result;
465 int i;
466 const int LOOPS = HZ/10;
468 printk("calibrating APIC timer ...\n");
471 * Put whatever arbitrary (but long enough) timeout
472 * value into the APIC clock, we just want to get the
473 * counter running for calibration.
475 __setup_APIC_LVTT(1000000000);
478 * The timer chip counts down to zero. Let's wait
479 * for a wraparound to start exact measurement:
480 * (the current tick might have been already half done)
483 wait_8254_wraparound();
486 * We wrapped around just now. Let's start:
488 if (cpu_has_tsc)
489 rdtscll(t1);
490 tt1 = apic_read(APIC_TMCCT);
493 * Let's wait LOOPS wraprounds:
495 for (i = 0; i < LOOPS; i++)
496 wait_8254_wraparound();
498 tt2 = apic_read(APIC_TMCCT);
499 if (cpu_has_tsc)
500 rdtscll(t2);
503 * The APIC bus clock counter is 32 bits only, it
504 * might have overflown, but note that we use signed
505 * longs, thus no extra care needed.
507 * underflown to be exact, as the timer counts down ;)
510 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
512 if (cpu_has_tsc)
513 printk("..... CPU clock speed is %ld.%04ld MHz.\n",
514 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
515 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
517 printk("..... host bus clock speed is %ld.%04ld MHz.\n",
518 result/(1000000/HZ),
519 result%(1000000/HZ));
521 return result;
524 static unsigned int calibration_result;
526 void __init setup_APIC_clocks (void)
528 __cli();
530 calibration_result = calibrate_APIC_clock();
532 * Now set up the timer for real.
534 setup_APIC_timer((void *)calibration_result);
536 __sti();
538 /* and update all other cpus */
539 smp_call_function(setup_APIC_timer, (void *)calibration_result, 1, 1);
543 * the frequency of the profiling timer can be changed
544 * by writing a multiplier value into /proc/profile.
546 int setup_profiling_timer(unsigned int multiplier)
548 int i;
551 * Sanity check. [at least 500 APIC cycles should be
552 * between APIC interrupts as a rule of thumb, to avoid
553 * irqs flooding us]
555 if ( (!multiplier) || (calibration_result/multiplier < 500))
556 return -EINVAL;
559 * Set the new multiplier for each CPU. CPUs don't start using the
560 * new values until the next timer interrupt in which they do process
561 * accounting. At that time they also adjust their APIC timers
562 * accordingly.
564 for (i = 0; i < NR_CPUS; ++i)
565 prof_multiplier[i] = multiplier;
567 return 0;
570 #undef APIC_DIVISOR
573 * Local timer interrupt handler. It does both profiling and
574 * process statistics/rescheduling.
576 * We do profiling in every local tick, statistics/rescheduling
577 * happen only every 'profiling multiplier' ticks. The default
578 * multiplier is 1 and it can be changed by writing the new multiplier
579 * value into /proc/profile.
582 inline void smp_local_timer_interrupt(struct pt_regs * regs)
584 int user = user_mode(regs);
585 int cpu = smp_processor_id();
588 * The profiling function is SMP safe. (nothing can mess
589 * around with "current", and the profiling counters are
590 * updated with atomic operations). This is especially
591 * useful with a profiling multiplier != 1
593 if (!user)
594 x86_do_profile(regs->eip);
596 if (--prof_counter[cpu] <= 0) {
598 * The multiplier may have changed since the last time we got
599 * to this point as a result of the user writing to
600 * /proc/profile. In this case we need to adjust the APIC
601 * timer accordingly.
603 * Interrupts are already masked off at this point.
605 prof_counter[cpu] = prof_multiplier[cpu];
606 if (prof_counter[cpu] != prof_old_multiplier[cpu]) {
607 __setup_APIC_LVTT(calibration_result/prof_counter[cpu]);
608 prof_old_multiplier[cpu] = prof_counter[cpu];
611 #ifdef CONFIG_SMP
613 * update_process_times() expects us to have done irq_enter().
614 * Besides, if we don't timer interrupts ignore the global
615 * interrupt lock, which is the WrongThing (tm) to do.
617 irq_enter(cpu, 0);
618 update_process_times(user);
619 irq_exit(cpu, 0);
620 #endif
624 * We take the 'long' return path, and there every subsystem
625 * grabs the apropriate locks (kernel lock/ irq lock).
627 * we might want to decouple profiling from the 'long path',
628 * and do the profiling totally in assembly.
630 * Currently this isn't too much of an issue (performance wise),
631 * we can take more than 100K local irqs per second on a 100 MHz P5.
636 * Local APIC timer interrupt. This is the most natural way for doing
637 * local interrupts, but local timer interrupts can be emulated by
638 * broadcast interrupts too. [in case the hw doesnt support APIC timers]
640 * [ if a single-CPU system runs an SMP kernel then we call the local
641 * interrupt as well. Thus we cannot inline the local irq ... ]
643 unsigned int apic_timer_irqs [NR_CPUS] = { 0, };
645 void smp_apic_timer_interrupt(struct pt_regs * regs)
648 * the NMI deadlock-detector uses this.
650 apic_timer_irqs[smp_processor_id()]++;
653 * NOTE! We'd better ACK the irq immediately,
654 * because timer handling can be slow.
656 ack_APIC_irq();
657 smp_local_timer_interrupt(regs);
661 * This interrupt should _never_ happen with our APIC/SMP architecture
663 asmlinkage void smp_spurious_interrupt(void)
665 unsigned long v;
668 * Check if this really is a spurious interrupt and ACK it
669 * if it is a vectored one. Just in case...
670 * Spurious interrupts should not be ACKed.
672 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
673 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
674 ack_APIC_irq();
676 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
677 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
678 smp_processor_id());
682 * This interrupt should never happen with our APIC/SMP architecture
685 static spinlock_t err_lock = SPIN_LOCK_UNLOCKED;
687 asmlinkage void smp_error_interrupt(void)
689 unsigned long v;
691 spin_lock(&err_lock);
693 v = apic_read(APIC_ESR);
694 printk(KERN_INFO "APIC error interrupt on CPU#%d, should never happen.\n",
695 smp_processor_id());
696 printk(KERN_INFO "... APIC ESR0: %08lx\n", v);
698 apic_write(APIC_ESR, 0);
699 v |= apic_read(APIC_ESR);
700 printk(KERN_INFO "... APIC ESR1: %08lx\n", v);
702 * Be a bit more verbose. (multiple bits can be set)
704 if (v & 0x01)
705 printk(KERN_INFO "... bit 0: APIC Send CS Error (hw problem).\n");
706 if (v & 0x02)
707 printk(KERN_INFO "... bit 1: APIC Receive CS Error (hw problem).\n");
708 if (v & 0x04)
709 printk(KERN_INFO "... bit 2: APIC Send Accept Error.\n");
710 if (v & 0x08)
711 printk(KERN_INFO "... bit 3: APIC Receive Accept Error.\n");
712 if (v & 0x10)
713 printk(KERN_INFO "... bit 4: Reserved!.\n");
714 if (v & 0x20)
715 printk(KERN_INFO "... bit 5: Send Illegal Vector (kernel bug).\n");
716 if (v & 0x40)
717 printk(KERN_INFO "... bit 6: Received Illegal Vector.\n");
718 if (v & 0x80)
719 printk(KERN_INFO "... bit 7: Illegal Register Address.\n");
721 ack_APIC_irq();
723 irq_err_count++;
725 spin_unlock(&err_lock);