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