[PATCH] x86_64: Report hardware breakpoints in user space when triggered by the kernel
[deliverable/linux.git] / arch / x86_64 / kernel / apic.c
CommitLineData
1da177e4
LT
1/*
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
17#include <linux/config.h>
18#include <linux/init.h>
19
20#include <linux/mm.h>
1da177e4
LT
21#include <linux/delay.h>
22#include <linux/bootmem.h>
23#include <linux/smp_lock.h>
24#include <linux/interrupt.h>
25#include <linux/mc146818rtc.h>
26#include <linux/kernel_stat.h>
27#include <linux/sysdev.h>
28
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>
75152114 35#include <asm/nmi.h>
1da177e4
LT
36
37int apic_verbosity;
38
39int disable_apic_timer __initdata;
40
41/* Using APIC to generate smp_local_timer_interrupt? */
42int using_apic_timer = 0;
43
44static DEFINE_PER_CPU(int, prof_multiplier) = 1;
45static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
46static DEFINE_PER_CPU(int, prof_counter) = 1;
47
48static void apic_pm_activate(void);
49
50void enable_NMI_through_LVT0 (void * dummy)
51{
52 unsigned int v, ver;
53
54 ver = apic_read(APIC_LVR);
55 ver = GET_APIC_VERSION(ver);
56 v = APIC_DM_NMI; /* unmask and set to NMI */
57 apic_write_around(APIC_LVT0, v);
58}
59
60int get_maxlvt(void)
61{
62 unsigned int v, ver, maxlvt;
63
64 v = apic_read(APIC_LVR);
65 ver = GET_APIC_VERSION(v);
66 maxlvt = GET_APIC_MAXLVT(v);
67 return maxlvt;
68}
69
70void clear_local_APIC(void)
71{
72 int maxlvt;
73 unsigned int v;
74
75 maxlvt = get_maxlvt();
76
77 /*
78 * Masking an LVT entry on a P6 can trigger a local APIC error
79 * if the vector is zero. Mask LVTERR first to prevent this.
80 */
81 if (maxlvt >= 3) {
82 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
83 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
84 }
85 /*
86 * Careful: we have to set masks only first to deassert
87 * any level-triggered sources.
88 */
89 v = apic_read(APIC_LVTT);
90 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
91 v = apic_read(APIC_LVT0);
92 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
93 v = apic_read(APIC_LVT1);
94 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
95 if (maxlvt >= 4) {
96 v = apic_read(APIC_LVTPC);
97 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
98 }
99
100 /*
101 * Clean APIC state for other OSs:
102 */
103 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
104 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
105 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
106 if (maxlvt >= 3)
107 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
108 if (maxlvt >= 4)
109 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
110 v = GET_APIC_VERSION(apic_read(APIC_LVR));
5a40b7c2
AK
111 apic_write(APIC_ESR, 0);
112 apic_read(APIC_ESR);
1da177e4
LT
113}
114
115void __init connect_bsp_APIC(void)
116{
117 if (pic_mode) {
118 /*
119 * Do not trust the local APIC being empty at bootup.
120 */
121 clear_local_APIC();
122 /*
123 * PIC mode, enable APIC mode in the IMCR, i.e.
124 * connect BSP's local APIC to INT and NMI lines.
125 */
126 apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
127 outb(0x70, 0x22);
128 outb(0x01, 0x23);
129 }
130}
131
208fb931 132void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4
LT
133{
134 if (pic_mode) {
135 /*
136 * Put the board back into PIC mode (has an effect
137 * only on certain older boards). Note that APIC
138 * interrupts, including IPIs, won't work beyond
139 * this point! The only exception are INIT IPIs.
140 */
141 apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
142 outb(0x70, 0x22);
143 outb(0x00, 0x23);
144 }
208fb931
EB
145 else {
146 /* Go back to Virtual Wire compatibility mode */
147 unsigned long value;
148
149 /* For the spurious interrupt use vector F, and enable it */
150 value = apic_read(APIC_SPIV);
151 value &= ~APIC_VECTOR_MASK;
152 value |= APIC_SPIV_APIC_ENABLED;
153 value |= 0xf;
154 apic_write_around(APIC_SPIV, value);
155
156 if (!virt_wire_setup) {
157 /* For LVT0 make it edge triggered, active high, external and enabled */
158 value = apic_read(APIC_LVT0);
159 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
160 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
161 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
162 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
163 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
164 apic_write_around(APIC_LVT0, value);
165 }
166 else {
167 /* Disable LVT0 */
168 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
169 }
170
171 /* For LVT1 make it edge triggered, active high, nmi and enabled */
172 value = apic_read(APIC_LVT1);
173 value &= ~(
174 APIC_MODE_MASK | APIC_SEND_PENDING |
175 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
176 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
177 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
178 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
179 apic_write_around(APIC_LVT1, value);
180 }
1da177e4
LT
181}
182
183void disable_local_APIC(void)
184{
185 unsigned int value;
186
187 clear_local_APIC();
188
189 /*
190 * Disable APIC (implies clearing of registers
191 * for 82489DX!).
192 */
193 value = apic_read(APIC_SPIV);
194 value &= ~APIC_SPIV_APIC_ENABLED;
195 apic_write_around(APIC_SPIV, value);
196}
197
198/*
199 * This is to verify that we're looking at a real local APIC.
200 * Check these against your board if the CPUs aren't getting
201 * started for no apparent reason.
202 */
203int __init verify_local_APIC(void)
204{
205 unsigned int reg0, reg1;
206
207 /*
208 * The version register is read-only in a real APIC.
209 */
210 reg0 = apic_read(APIC_LVR);
211 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
212 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
213 reg1 = apic_read(APIC_LVR);
214 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
215
216 /*
217 * The two version reads above should print the same
218 * numbers. If the second one is different, then we
219 * poke at a non-APIC.
220 */
221 if (reg1 != reg0)
222 return 0;
223
224 /*
225 * Check if the version looks reasonably.
226 */
227 reg1 = GET_APIC_VERSION(reg0);
228 if (reg1 == 0x00 || reg1 == 0xff)
229 return 0;
230 reg1 = get_maxlvt();
231 if (reg1 < 0x02 || reg1 == 0xff)
232 return 0;
233
234 /*
235 * The ID register is read/write in a real APIC.
236 */
237 reg0 = apic_read(APIC_ID);
238 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
239 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
240 reg1 = apic_read(APIC_ID);
241 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
242 apic_write(APIC_ID, reg0);
243 if (reg1 != (reg0 ^ APIC_ID_MASK))
244 return 0;
245
246 /*
247 * The next two are just to see if we have sane values.
248 * They're only really relevant if we're in Virtual Wire
249 * compatibility mode, but most boxes are anymore.
250 */
251 reg0 = apic_read(APIC_LVT0);
252 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
253 reg1 = apic_read(APIC_LVT1);
254 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
255
256 return 1;
257}
258
259void __init sync_Arb_IDs(void)
260{
261 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
262 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
263 if (ver >= 0x14) /* P4 or higher */
264 return;
265
266 /*
267 * Wait for idle.
268 */
269 apic_wait_icr_idle();
270
271 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
272 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
273 | APIC_DM_INIT);
274}
275
276extern void __error_in_apic_c (void);
277
278/*
279 * An initial setup of the virtual wire mode.
280 */
281void __init init_bsp_APIC(void)
282{
283 unsigned int value, ver;
284
285 /*
286 * Don't do the setup now if we have a SMP BIOS as the
287 * through-I/O-APIC virtual wire mode might be active.
288 */
289 if (smp_found_config || !cpu_has_apic)
290 return;
291
292 value = apic_read(APIC_LVR);
293 ver = GET_APIC_VERSION(value);
294
295 /*
296 * Do not trust the local APIC being empty at bootup.
297 */
298 clear_local_APIC();
299
300 /*
301 * Enable APIC.
302 */
303 value = apic_read(APIC_SPIV);
304 value &= ~APIC_VECTOR_MASK;
305 value |= APIC_SPIV_APIC_ENABLED;
306 value |= APIC_SPIV_FOCUS_DISABLED;
307 value |= SPURIOUS_APIC_VECTOR;
308 apic_write_around(APIC_SPIV, value);
309
310 /*
311 * Set up the virtual wire mode.
312 */
313 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
314 value = APIC_DM_NMI;
1da177e4
LT
315 apic_write_around(APIC_LVT1, value);
316}
317
e6982c67 318void __cpuinit setup_local_APIC (void)
1da177e4
LT
319{
320 unsigned int value, ver, maxlvt;
321
1da177e4
LT
322 value = apic_read(APIC_LVR);
323 ver = GET_APIC_VERSION(value);
324
325 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
326 __error_in_apic_c();
327
328 /*
329 * Double-check whether this APIC is really registered.
330 * This is meaningless in clustered apic mode, so we skip it.
331 */
332 if (!apic_id_registered())
333 BUG();
334
335 /*
336 * Intel recommends to set DFR, LDR and TPR before enabling
337 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
338 * document number 292116). So here it goes...
339 */
340 init_apic_ldr();
341
342 /*
343 * Set Task Priority to 'accept all'. We never change this
344 * later on.
345 */
346 value = apic_read(APIC_TASKPRI);
347 value &= ~APIC_TPRI_MASK;
348 apic_write_around(APIC_TASKPRI, value);
349
350 /*
351 * Now that we are all set up, enable the APIC
352 */
353 value = apic_read(APIC_SPIV);
354 value &= ~APIC_VECTOR_MASK;
355 /*
356 * Enable APIC
357 */
358 value |= APIC_SPIV_APIC_ENABLED;
359
360 /*
361 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
362 * certain networking cards. If high frequency interrupts are
363 * happening on a particular IOAPIC pin, plus the IOAPIC routing
364 * entry is masked/unmasked at a high rate as well then sooner or
365 * later IOAPIC line gets 'stuck', no more interrupts are received
366 * from the device. If focus CPU is disabled then the hang goes
367 * away, oh well :-(
368 *
369 * [ This bug can be reproduced easily with a level-triggered
370 * PCI Ne2000 networking cards and PII/PIII processors, dual
371 * BX chipset. ]
372 */
373 /*
374 * Actually disabling the focus CPU check just makes the hang less
375 * frequent as it makes the interrupt distributon model be more
376 * like LRU than MRU (the short-term load is more even across CPUs).
377 * See also the comment in end_level_ioapic_irq(). --macro
378 */
379#if 1
380 /* Enable focus processor (bit==0) */
381 value &= ~APIC_SPIV_FOCUS_DISABLED;
382#else
383 /* Disable focus processor (bit==1) */
384 value |= APIC_SPIV_FOCUS_DISABLED;
385#endif
386 /*
387 * Set spurious IRQ vector
388 */
389 value |= SPURIOUS_APIC_VECTOR;
390 apic_write_around(APIC_SPIV, value);
391
392 /*
393 * Set up LVT0, LVT1:
394 *
395 * set up through-local-APIC on the BP's LINT0. This is not
396 * strictly necessary in pure symmetric-IO mode, but sometimes
397 * we delegate interrupts to the 8259A.
398 */
399 /*
400 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
401 */
402 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
403 if (!smp_processor_id() && (pic_mode || !value)) {
404 value = APIC_DM_EXTINT;
405 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
406 } else {
407 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
408 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
409 }
410 apic_write_around(APIC_LVT0, value);
411
412 /*
413 * only the BP should see the LINT1 NMI signal, obviously.
414 */
415 if (!smp_processor_id())
416 value = APIC_DM_NMI;
417 else
418 value = APIC_DM_NMI | APIC_LVT_MASKED;
1da177e4
LT
419 apic_write_around(APIC_LVT1, value);
420
61c11341 421 {
1da177e4
LT
422 unsigned oldvalue;
423 maxlvt = get_maxlvt();
1da177e4
LT
424 oldvalue = apic_read(APIC_ESR);
425 value = ERROR_APIC_VECTOR; // enables sending errors
426 apic_write_around(APIC_LVTERR, value);
427 /*
428 * spec says clear errors after enabling vector.
429 */
430 if (maxlvt > 3)
431 apic_write(APIC_ESR, 0);
432 value = apic_read(APIC_ESR);
433 if (value != oldvalue)
434 apic_printk(APIC_VERBOSE,
435 "ESR value after enabling vector: %08x, after %08x\n",
436 oldvalue, value);
1da177e4
LT
437 }
438
439 nmi_watchdog_default();
440 if (nmi_watchdog == NMI_LOCAL_APIC)
441 setup_apic_nmi_watchdog();
442 apic_pm_activate();
443}
444
445#ifdef CONFIG_PM
446
447static struct {
448 /* 'active' is true if the local APIC was enabled by us and
449 not the BIOS; this signifies that we are also responsible
450 for disabling it before entering apm/acpi suspend */
451 int active;
452 /* r/w apic fields */
453 unsigned int apic_id;
454 unsigned int apic_taskpri;
455 unsigned int apic_ldr;
456 unsigned int apic_dfr;
457 unsigned int apic_spiv;
458 unsigned int apic_lvtt;
459 unsigned int apic_lvtpc;
460 unsigned int apic_lvt0;
461 unsigned int apic_lvt1;
462 unsigned int apic_lvterr;
463 unsigned int apic_tmict;
464 unsigned int apic_tdcr;
465 unsigned int apic_thmr;
466} apic_pm_state;
467
0b9c33a7 468static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
469{
470 unsigned long flags;
471
472 if (!apic_pm_state.active)
473 return 0;
474
475 apic_pm_state.apic_id = apic_read(APIC_ID);
476 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
477 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
478 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
479 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
480 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
481 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
482 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
483 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
484 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
485 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
486 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
487 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
488 local_save_flags(flags);
489 local_irq_disable();
490 disable_local_APIC();
491 local_irq_restore(flags);
492 return 0;
493}
494
495static int lapic_resume(struct sys_device *dev)
496{
497 unsigned int l, h;
498 unsigned long flags;
499
500 if (!apic_pm_state.active)
501 return 0;
502
503 /* XXX: Pavel needs this for S3 resume, but can't explain why */
504 set_fixmap_nocache(FIX_APIC_BASE, APIC_DEFAULT_PHYS_BASE);
505
506 local_irq_save(flags);
507 rdmsr(MSR_IA32_APICBASE, l, h);
508 l &= ~MSR_IA32_APICBASE_BASE;
509 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
510 wrmsr(MSR_IA32_APICBASE, l, h);
511 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
512 apic_write(APIC_ID, apic_pm_state.apic_id);
513 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
514 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
515 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
516 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
517 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
518 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
519 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
520 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
521 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
522 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
523 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
524 apic_write(APIC_ESR, 0);
525 apic_read(APIC_ESR);
526 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
527 apic_write(APIC_ESR, 0);
528 apic_read(APIC_ESR);
529 local_irq_restore(flags);
530 return 0;
531}
532
533static struct sysdev_class lapic_sysclass = {
534 set_kset_name("lapic"),
535 .resume = lapic_resume,
536 .suspend = lapic_suspend,
537};
538
539static struct sys_device device_lapic = {
540 .id = 0,
541 .cls = &lapic_sysclass,
542};
543
e6982c67 544static void __cpuinit apic_pm_activate(void)
1da177e4
LT
545{
546 apic_pm_state.active = 1;
547}
548
549static int __init init_lapic_sysfs(void)
550{
551 int error;
552 if (!cpu_has_apic)
553 return 0;
554 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
555 error = sysdev_class_register(&lapic_sysclass);
556 if (!error)
557 error = sysdev_register(&device_lapic);
558 return error;
559}
560device_initcall(init_lapic_sysfs);
561
562#else /* CONFIG_PM */
563
564static void apic_pm_activate(void) { }
565
566#endif /* CONFIG_PM */
567
568static int __init apic_set_verbosity(char *str)
569{
570 if (strcmp("debug", str) == 0)
571 apic_verbosity = APIC_DEBUG;
572 else if (strcmp("verbose", str) == 0)
573 apic_verbosity = APIC_VERBOSE;
574 else
575 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
576 " use apic=verbose or apic=debug", str);
577
578 return 0;
579}
580
581__setup("apic=", apic_set_verbosity);
582
583/*
584 * Detect and enable local APICs on non-SMP boards.
585 * Original code written by Keir Fraser.
586 * On AMD64 we trust the BIOS - if it says no APIC it is likely
587 * not correctly set up (usually the APIC timer won't work etc.)
588 */
589
590static int __init detect_init_APIC (void)
591{
592 if (!cpu_has_apic) {
593 printk(KERN_INFO "No local APIC present\n");
594 return -1;
595 }
596
597 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
598 boot_cpu_id = 0;
599 return 0;
600}
601
602void __init init_apic_mappings(void)
603{
604 unsigned long apic_phys;
605
606 /*
607 * If no local APIC can be found then set up a fake all
608 * zeroes page to simulate the local APIC and another
609 * one for the IO-APIC.
610 */
611 if (!smp_found_config && detect_init_APIC()) {
612 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
613 apic_phys = __pa(apic_phys);
614 } else
615 apic_phys = mp_lapic_addr;
616
617 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
618 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
619
620 /*
621 * Fetch the APIC ID of the BSP in case we have a
622 * default configuration (or the MP table is broken).
623 */
1d3fbbf9 624 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
1da177e4
LT
625
626#ifdef CONFIG_X86_IO_APIC
627 {
628 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
629 int i;
630
631 for (i = 0; i < nr_ioapics; i++) {
632 if (smp_found_config) {
633 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
634 } else {
635 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
636 ioapic_phys = __pa(ioapic_phys);
637 }
638 set_fixmap_nocache(idx, ioapic_phys);
639 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
640 __fix_to_virt(idx), ioapic_phys);
641 idx++;
642 }
643 }
644#endif
645}
646
647/*
648 * This function sets up the local APIC timer, with a timeout of
649 * 'clocks' APIC bus clock. During calibration we actually call
650 * this function twice on the boot CPU, once with a bogus timeout
651 * value, second time for real. The other (noncalibrating) CPUs
652 * call this function only once, with the real, calibrated value.
653 *
654 * We do reads before writes even if unnecessary, to get around the
655 * P5 APIC double write bug.
656 */
657
658#define APIC_DIVISOR 16
659
660static void __setup_APIC_LVTT(unsigned int clocks)
661{
662 unsigned int lvtt_value, tmp_value, ver;
663
664 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
665 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
1da177e4
LT
666 apic_write_around(APIC_LVTT, lvtt_value);
667
668 /*
669 * Divide PICLK by 16
670 */
671 tmp_value = apic_read(APIC_TDCR);
672 apic_write_around(APIC_TDCR, (tmp_value
673 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
674 | APIC_TDR_DIV_16);
675
676 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
677}
678
679static void setup_APIC_timer(unsigned int clocks)
680{
681 unsigned long flags;
682
683 local_irq_save(flags);
684
685 /* For some reasons this doesn't work on Simics, so fake it for now */
686 if (!strstr(boot_cpu_data.x86_model_id, "Screwdriver")) {
687 __setup_APIC_LVTT(clocks);
688 return;
689 }
690
691 /* wait for irq slice */
692 if (vxtime.hpet_address) {
693 int trigger = hpet_readl(HPET_T0_CMP);
694 while (hpet_readl(HPET_COUNTER) >= trigger)
695 /* do nothing */ ;
696 while (hpet_readl(HPET_COUNTER) < trigger)
697 /* do nothing */ ;
698 } else {
699 int c1, c2;
700 outb_p(0x00, 0x43);
701 c2 = inb_p(0x40);
702 c2 |= inb_p(0x40) << 8;
703 do {
704 c1 = c2;
705 outb_p(0x00, 0x43);
706 c2 = inb_p(0x40);
707 c2 |= inb_p(0x40) << 8;
708 } while (c2 - c1 < 300);
709 }
710
711 __setup_APIC_LVTT(clocks);
712
713 local_irq_restore(flags);
714}
715
716/*
717 * In this function we calibrate APIC bus clocks to the external
718 * timer. Unfortunately we cannot use jiffies and the timer irq
719 * to calibrate, since some later bootup code depends on getting
720 * the first irq? Ugh.
721 *
722 * We want to do the calibration only once since we
723 * want to have local timer irqs syncron. CPUs connected
724 * by the same APIC bus have the very same bus frequency.
725 * And we want to have irqs off anyways, no accidental
726 * APIC irq that way.
727 */
728
729#define TICK_COUNT 100000000
730
731static int __init calibrate_APIC_clock(void)
732{
733 int apic, apic_start, tsc, tsc_start;
734 int result;
735 /*
736 * Put whatever arbitrary (but long enough) timeout
737 * value into the APIC clock, we just want to get the
738 * counter running for calibration.
739 */
740 __setup_APIC_LVTT(1000000000);
741
742 apic_start = apic_read(APIC_TMCCT);
743 rdtscl(tsc_start);
744
745 do {
746 apic = apic_read(APIC_TMCCT);
747 rdtscl(tsc);
748 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
749
750 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
751
752 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
753 result / 1000 / 1000, result / 1000 % 1000);
754
755 return result * APIC_DIVISOR / HZ;
756}
757
758static unsigned int calibration_result;
759
760void __init setup_boot_APIC_clock (void)
761{
762 if (disable_apic_timer) {
763 printk(KERN_INFO "Disabling APIC timer\n");
764 return;
765 }
766
767 printk(KERN_INFO "Using local APIC timer interrupts.\n");
768 using_apic_timer = 1;
769
770 local_irq_disable();
771
772 calibration_result = calibrate_APIC_clock();
773 /*
774 * Now set up the timer for real.
775 */
776 setup_APIC_timer(calibration_result);
777
778 local_irq_enable();
779}
780
e6982c67 781void __cpuinit setup_secondary_APIC_clock(void)
1da177e4
LT
782{
783 local_irq_disable(); /* FIXME: Do we need this? --RR */
784 setup_APIC_timer(calibration_result);
785 local_irq_enable();
786}
787
e6982c67 788void __cpuinit disable_APIC_timer(void)
1da177e4
LT
789{
790 if (using_apic_timer) {
791 unsigned long v;
792
793 v = apic_read(APIC_LVTT);
794 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
795 }
796}
797
798void enable_APIC_timer(void)
799{
800 if (using_apic_timer) {
801 unsigned long v;
802
803 v = apic_read(APIC_LVTT);
804 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
805 }
806}
807
808/*
809 * the frequency of the profiling timer can be changed
810 * by writing a multiplier value into /proc/profile.
811 */
812int setup_profiling_timer(unsigned int multiplier)
813{
814 int i;
815
816 /*
817 * Sanity check. [at least 500 APIC cycles should be
818 * between APIC interrupts as a rule of thumb, to avoid
819 * irqs flooding us]
820 */
821 if ( (!multiplier) || (calibration_result/multiplier < 500))
822 return -EINVAL;
823
824 /*
825 * Set the new multiplier for each CPU. CPUs don't start using the
826 * new values until the next timer interrupt in which they do process
827 * accounting. At that time they also adjust their APIC timers
828 * accordingly.
829 */
830 for (i = 0; i < NR_CPUS; ++i)
831 per_cpu(prof_multiplier, i) = multiplier;
832
833 return 0;
834}
835
89b831ef
JS
836#ifdef CONFIG_X86_MCE_AMD
837void setup_threshold_lvt(unsigned long lvt_off)
838{
839 unsigned int v = 0;
840 unsigned long reg = (lvt_off << 4) + 0x500;
841 v |= THRESHOLD_APIC_VECTOR;
842 apic_write(reg, v);
843}
844#endif /* CONFIG_X86_MCE_AMD */
845
1da177e4
LT
846#undef APIC_DIVISOR
847
848/*
849 * Local timer interrupt handler. It does both profiling and
850 * process statistics/rescheduling.
851 *
852 * We do profiling in every local tick, statistics/rescheduling
853 * happen only every 'profiling multiplier' ticks. The default
854 * multiplier is 1 and it can be changed by writing the new multiplier
855 * value into /proc/profile.
856 */
857
858void smp_local_timer_interrupt(struct pt_regs *regs)
859{
860 int cpu = smp_processor_id();
861
862 profile_tick(CPU_PROFILING, regs);
863 if (--per_cpu(prof_counter, cpu) <= 0) {
864 /*
865 * The multiplier may have changed since the last time we got
866 * to this point as a result of the user writing to
867 * /proc/profile. In this case we need to adjust the APIC
868 * timer accordingly.
869 *
870 * Interrupts are already masked off at this point.
871 */
872 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
873 if (per_cpu(prof_counter, cpu) !=
874 per_cpu(prof_old_multiplier, cpu)) {
875 __setup_APIC_LVTT(calibration_result/
876 per_cpu(prof_counter, cpu));
877 per_cpu(prof_old_multiplier, cpu) =
878 per_cpu(prof_counter, cpu);
879 }
880
881#ifdef CONFIG_SMP
882 update_process_times(user_mode(regs));
883#endif
884 }
885
886 /*
887 * We take the 'long' return path, and there every subsystem
888 * grabs the appropriate locks (kernel lock/ irq lock).
889 *
890 * we might want to decouple profiling from the 'long path',
891 * and do the profiling totally in assembly.
892 *
893 * Currently this isn't too much of an issue (performance wise),
894 * we can take more than 100K local irqs per second on a 100 MHz P5.
895 */
896}
897
898/*
899 * Local APIC timer interrupt. This is the most natural way for doing
900 * local interrupts, but local timer interrupts can be emulated by
901 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
902 *
903 * [ if a single-CPU system runs an SMP kernel then we call the local
904 * interrupt as well. Thus we cannot inline the local irq ... ]
905 */
906void smp_apic_timer_interrupt(struct pt_regs *regs)
907{
908 /*
909 * the NMI deadlock-detector uses this.
910 */
911 add_pda(apic_timer_irqs, 1);
912
913 /*
914 * NOTE! We'd better ACK the irq immediately,
915 * because timer handling can be slow.
916 */
917 ack_APIC_irq();
918 /*
919 * update_process_times() expects us to have done irq_enter().
920 * Besides, if we don't timer interrupts ignore the global
921 * interrupt lock, which is the WrongThing (tm) to do.
922 */
923 irq_enter();
924 smp_local_timer_interrupt(regs);
925 irq_exit();
926}
927
928/*
929 * oem_force_hpet_timer -- force HPET mode for some boxes.
930 *
931 * Thus far, the major user of this is IBM's Summit2 series:
932 *
933 * Clustered boxes may have unsynced TSC problems if they are
934 * multi-chassis. Use available data to take a good guess.
935 * If in doubt, go HPET.
936 */
937__init int oem_force_hpet_timer(void)
938{
939 int i, clusters, zeros;
940 unsigned id;
941 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
942
376ec33f 943 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1da177e4
LT
944
945 for (i = 0; i < NR_CPUS; i++) {
946 id = bios_cpu_apicid[i];
947 if (id != BAD_APICID)
948 __set_bit(APIC_CLUSTERID(id), clustermap);
949 }
950
951 /* Problem: Partially populated chassis may not have CPUs in some of
952 * the APIC clusters they have been allocated. Only present CPUs have
953 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
954 * clusters are allocated sequentially, count zeros only if they are
955 * bounded by ones.
956 */
957 clusters = 0;
958 zeros = 0;
959 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
960 if (test_bit(i, clustermap)) {
961 clusters += 1 + zeros;
962 zeros = 0;
963 } else
964 ++zeros;
965 }
966
967 /*
968 * If clusters > 2, then should be multi-chassis. Return 1 for HPET.
969 * Else return 0 to use TSC.
970 * May have to revisit this when multi-core + hyperthreaded CPUs come
971 * out, but AFAIK this will work even for them.
972 */
973 return (clusters > 2);
974}
975
976/*
977 * This interrupt should _never_ happen with our APIC/SMP architecture
978 */
979asmlinkage void smp_spurious_interrupt(void)
980{
981 unsigned int v;
982 irq_enter();
983 /*
984 * Check if this really is a spurious interrupt and ACK it
985 * if it is a vectored one. Just in case...
986 * Spurious interrupts should not be ACKed.
987 */
988 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
989 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
990 ack_APIC_irq();
991
992#if 0
993 static unsigned long last_warning;
994 static unsigned long skipped;
995
996 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
997 if (time_before(last_warning+30*HZ,jiffies)) {
998 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
999 smp_processor_id(), skipped);
1000 last_warning = jiffies;
1001 skipped = 0;
1002 } else {
1003 skipped++;
1004 }
1005#endif
1006 irq_exit();
1007}
1008
1009/*
1010 * This interrupt should never happen with our APIC/SMP architecture
1011 */
1012
1013asmlinkage void smp_error_interrupt(void)
1014{
1015 unsigned int v, v1;
1016
1017 irq_enter();
1018 /* First tickle the hardware, only then report what went on. -- REW */
1019 v = apic_read(APIC_ESR);
1020 apic_write(APIC_ESR, 0);
1021 v1 = apic_read(APIC_ESR);
1022 ack_APIC_irq();
1023 atomic_inc(&irq_err_count);
1024
1025 /* Here is what the APIC error bits mean:
1026 0: Send CS error
1027 1: Receive CS error
1028 2: Send accept error
1029 3: Receive accept error
1030 4: Reserved
1031 5: Send illegal vector
1032 6: Received illegal vector
1033 7: Illegal register address
1034 */
1035 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1036 smp_processor_id(), v , v1);
1037 irq_exit();
1038}
1039
1040int disable_apic;
1041
1042/*
1043 * This initializes the IO-APIC and APIC hardware if this is
1044 * a UP kernel.
1045 */
1046int __init APIC_init_uniprocessor (void)
1047{
1048 if (disable_apic) {
1049 printk(KERN_INFO "Apic disabled\n");
1050 return -1;
1051 }
1052 if (!cpu_has_apic) {
1053 disable_apic = 1;
1054 printk(KERN_INFO "Apic disabled by BIOS\n");
1055 return -1;
1056 }
1057
1058 verify_local_APIC();
1059
1060 connect_bsp_APIC();
1061
357e11d4 1062 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
1da177e4
LT
1063 apic_write_around(APIC_ID, boot_cpu_id);
1064
1065 setup_local_APIC();
1066
1067#ifdef CONFIG_X86_IO_APIC
1068 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1069 setup_IO_APIC();
1070 else
1071 nr_ioapics = 0;
1072#endif
1073 setup_boot_APIC_clock();
75152114 1074 check_nmi_watchdog();
1da177e4
LT
1075 return 0;
1076}
1077
1078static __init int setup_disableapic(char *str)
1079{
1080 disable_apic = 1;
1081 return 0;
1082}
1083
1084static __init int setup_nolapic(char *str)
1085{
1086 disable_apic = 1;
1087 return 0;
1088}
1089
1090static __init int setup_noapictimer(char *str)
1091{
1092 disable_apic_timer = 1;
1093 return 0;
1094}
1095
1096/* dummy parsing: see setup.c */
1097
1098__setup("disableapic", setup_disableapic);
1099__setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1100
1101__setup("noapictimer", setup_noapictimer);
1102
1103/* no "lapic" flag - we only use the lapic when the BIOS tells us so. */
This page took 0.153851 seconds and 5 git commands to generate.