2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
10 * for testing these extensively
13 #include <linux/config.h>
14 #include <linux/init.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>
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, };
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;
45 static void clear_local_APIC(void)
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
);
63 v
= apic_read(APIC_LVTERR
);
64 apic_write_around(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
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
);
78 apic_write_around(APIC_LVTERR
, APIC_LVT_MASKED
);
80 apic_write_around(APIC_LVTPC
, APIC_LVT_MASKED
);
83 void __init
connect_bsp_APIC(void)
87 * Do not trust the local APIC being empty at bootup.
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");
100 void disconnect_bsp_APIC(void)
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");
115 void disable_local_APIC(void)
122 * Disable APIC (implies clearing of registers
125 value
= apic_read(APIC_SPIV
);
127 apic_write_around(APIC_SPIV
, value
);
130 void __init
sync_Arb_IDs(void)
135 apic_wait_icr_idle();
137 Dprintk("Synchronizing Arb IDs.\n");
138 apic_write_around(APIC_ICR
, APIC_DEST_ALLINC
| APIC_INT_LEVELTRIG
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)
155 * Double-check wether this APIC is really registered.
157 if (!test_bit(GET_APIC_ID(apic_read(APIC_ID
)), &phys_cpu_present_map
))
160 value
= apic_read(APIC_SPIV
);
161 value
&= ~APIC_VECTOR_MASK
;
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
176 * [ This bug can be reproduced easily with a level-triggered
177 * PCI Ne2000 networking cards and PII/PIII processors, dual
181 /* Enable focus processor (bit==0) */
184 /* Disable focus processor (bit==1) */
188 * Set spurious IRQ vector
190 value
|= SPURIOUS_APIC_VECTOR
;
191 apic_write_around(APIC_SPIV
, value
);
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());
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())
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.
237 apic_write(APIC_ESR
, 0);
238 value
= apic_read(APIC_ESR
);
239 printk("ESR value after enabling vector: %08lx\n", value
);
241 printk("No ESR for 82489DX.\n");
244 * Set Task Priority to 'accept all'. We never change this
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
;
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
;
297 for (i
= 0; i
< nr_ioapics
; i
++) {
298 if (smp_found_config
) {
299 ioapic_phys
= mp_ioapics
[i
].mpc_apicaddr
;
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
);
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
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
327 static unsigned int __init
get_8254_timer_count(void)
329 extern spinlock_t i8253_lock
;
334 spin_lock_irqsave(&i8253_lock
, flags
);
338 count
|= inb_p(0x40) << 8;
340 spin_unlock_irqrestore(&i8253_lock
, flags
);
345 void __init
wait_8254_wraparound(void)
347 unsigned int curr_count
, prev_count
=~0;
350 curr_count
= get_8254_timer_count();
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
);
390 tmp_value
= apic_read(APIC_TDCR
);
391 apic_write_around(APIC_TDCR
, (tmp_value
392 & ~(APIC_TDR_DIV_1
| APIC_TDR_DIV_TMBASE
))
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
;
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... */
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. */
435 t1
= apic_read(APIC_TMCCT
)*APIC_DIVISOR
;
436 delta
= (int)(t0
- t1
- slice
*(smp_processor_id()+1));
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
460 int __init
calibrate_APIC_clock(void)
462 unsigned long long t1
= 0, t2
= 0;
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:
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
);
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
;
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",
519 result
%(1000000/HZ
));
524 static unsigned int calibration_result
;
526 void __init
setup_APIC_clocks (void)
530 calibration_result
= calibrate_APIC_clock();
532 * Now set up the timer for real.
534 setup_APIC_timer((void *)calibration_result
);
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
)
551 * Sanity check. [at least 500 APIC cycles should be
552 * between APIC interrupts as a rule of thumb, to avoid
555 if ( (!multiplier
) || (calibration_result
/multiplier
< 500))
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
564 for (i
= 0; i
< NR_CPUS
; ++i
)
565 prof_multiplier
[i
] = multiplier
;
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
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
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
];
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.
618 update_process_times(user
);
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.
657 smp_local_timer_interrupt(regs
);
661 * This interrupt should _never_ happen with our APIC/SMP architecture
663 asmlinkage
void smp_spurious_interrupt(void)
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)))
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",
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)
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",
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)
705 printk(KERN_INFO
"... bit 0: APIC Send CS Error (hw problem).\n");
707 printk(KERN_INFO
"... bit 1: APIC Receive CS Error (hw problem).\n");
709 printk(KERN_INFO
"... bit 2: APIC Send Accept Error.\n");
711 printk(KERN_INFO
"... bit 3: APIC Receive Accept Error.\n");
713 printk(KERN_INFO
"... bit 4: Reserved!.\n");
715 printk(KERN_INFO
"... bit 5: Send Illegal Vector (kernel bug).\n");
717 printk(KERN_INFO
"... bit 6: Received Illegal Vector.\n");
719 printk(KERN_INFO
"... bit 7: Illegal Register Address.\n");
725 spin_unlock(&err_lock
);