[PATCH] i386: Fix race in IO-APIC routing entry setup.
[safe/jmp/linux-2.6] / arch / i386 / kernel / io_apic.c
1 /*
2  *      Intel IO-APIC support for multi-Pentium hosts.
3  *
4  *      Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5  *
6  *      Many thanks to Stig Venaas for trying out countless experimental
7  *      patches and reporting/debugging problems patiently!
8  *
9  *      (c) 1999, Multiple IO-APIC support, developed by
10  *      Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11  *      Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12  *      further tested and cleaned up by Zach Brown <zab@redhat.com>
13  *      and Ingo Molnar <mingo@redhat.com>
14  *
15  *      Fixes
16  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs;
17  *                                      thanks to Eric Gilmore
18  *                                      and Rolf G. Tews
19  *                                      for testing these extensively
20  *      Paul Diefenbaugh        :       Added full ACPI support
21  */
22
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/compiler.h>
31 #include <linux/acpi.h>
32 #include <linux/module.h>
33 #include <linux/sysdev.h>
34 #include <linux/pci.h>
35 #include <linux/msi.h>
36 #include <linux/htirq.h>
37
38 #include <asm/io.h>
39 #include <asm/smp.h>
40 #include <asm/desc.h>
41 #include <asm/timer.h>
42 #include <asm/i8259.h>
43 #include <asm/nmi.h>
44 #include <asm/msidef.h>
45 #include <asm/hypertransport.h>
46
47 #include <mach_apic.h>
48 #include <mach_apicdef.h>
49
50 #include "io_ports.h"
51
52 int (*ioapic_renumber_irq)(int ioapic, int irq);
53 atomic_t irq_mis_count;
54
55 /* Where if anywhere is the i8259 connect in external int mode */
56 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
57
58 static DEFINE_SPINLOCK(ioapic_lock);
59 static DEFINE_SPINLOCK(vector_lock);
60
61 int timer_over_8254 __initdata = 1;
62
63 /*
64  *      Is the SiS APIC rmw bug present ?
65  *      -1 = don't know, 0 = no, 1 = yes
66  */
67 int sis_apic_bug = -1;
68
69 /*
70  * # of IRQ routing registers
71  */
72 int nr_ioapic_registers[MAX_IO_APICS];
73
74 static int disable_timer_pin_1 __initdata;
75
76 /*
77  * Rough estimation of how many shared IRQs there are, can
78  * be changed anytime.
79  */
80 #define MAX_PLUS_SHARED_IRQS NR_IRQS
81 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
82
83 /*
84  * This is performance-critical, we want to do it O(1)
85  *
86  * the indexing order of this array favors 1:1 mappings
87  * between pins and IRQs.
88  */
89
90 static struct irq_pin_list {
91         int apic, pin, next;
92 } irq_2_pin[PIN_MAP_SIZE];
93
94 struct io_apic {
95         unsigned int index;
96         unsigned int unused[3];
97         unsigned int data;
98 };
99
100 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
101 {
102         return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
103                 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
104 }
105
106 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
107 {
108         struct io_apic __iomem *io_apic = io_apic_base(apic);
109         writel(reg, &io_apic->index);
110         return readl(&io_apic->data);
111 }
112
113 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
114 {
115         struct io_apic __iomem *io_apic = io_apic_base(apic);
116         writel(reg, &io_apic->index);
117         writel(value, &io_apic->data);
118 }
119
120 /*
121  * Re-write a value: to be used for read-modify-write
122  * cycles where the read already set up the index register.
123  *
124  * Older SiS APIC requires we rewrite the index register
125  */
126 static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
127 {
128         volatile struct io_apic *io_apic = io_apic_base(apic);
129         if (sis_apic_bug)
130                 writel(reg, &io_apic->index);
131         writel(value, &io_apic->data);
132 }
133
134 union entry_union {
135         struct { u32 w1, w2; };
136         struct IO_APIC_route_entry entry;
137 };
138
139 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
140 {
141         union entry_union eu;
142         unsigned long flags;
143         spin_lock_irqsave(&ioapic_lock, flags);
144         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
145         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
146         spin_unlock_irqrestore(&ioapic_lock, flags);
147         return eu.entry;
148 }
149
150 /*
151  * When we write a new IO APIC routing entry, we need to write the high
152  * word first! If the mask bit in the low word is clear, we will enable
153  * the interrupt, and we need to make sure the entry is fully populated
154  * before that happens.
155  */
156 static void
157 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
158 {
159         union entry_union eu;
160         eu.entry = e;
161         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
162         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
163 }
164
165 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
166 {
167         unsigned long flags;
168         spin_lock_irqsave(&ioapic_lock, flags);
169         __ioapic_write_entry(apic, pin, e);
170         spin_unlock_irqrestore(&ioapic_lock, flags);
171 }
172
173 /*
174  * When we mask an IO APIC routing entry, we need to write the low
175  * word first, in order to set the mask bit before we change the
176  * high bits!
177  */
178 static void ioapic_mask_entry(int apic, int pin)
179 {
180         unsigned long flags;
181         union entry_union eu = { .entry.mask = 1 };
182
183         spin_lock_irqsave(&ioapic_lock, flags);
184         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
185         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
186         spin_unlock_irqrestore(&ioapic_lock, flags);
187 }
188
189 /*
190  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
191  * shared ISA-space IRQs, so we have to support them. We are super
192  * fast in the common case, and fast for shared ISA-space IRQs.
193  */
194 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
195 {
196         static int first_free_entry = NR_IRQS;
197         struct irq_pin_list *entry = irq_2_pin + irq;
198
199         while (entry->next)
200                 entry = irq_2_pin + entry->next;
201
202         if (entry->pin != -1) {
203                 entry->next = first_free_entry;
204                 entry = irq_2_pin + entry->next;
205                 if (++first_free_entry >= PIN_MAP_SIZE)
206                         panic("io_apic.c: whoops");
207         }
208         entry->apic = apic;
209         entry->pin = pin;
210 }
211
212 /*
213  * Reroute an IRQ to a different pin.
214  */
215 static void __init replace_pin_at_irq(unsigned int irq,
216                                       int oldapic, int oldpin,
217                                       int newapic, int newpin)
218 {
219         struct irq_pin_list *entry = irq_2_pin + irq;
220
221         while (1) {
222                 if (entry->apic == oldapic && entry->pin == oldpin) {
223                         entry->apic = newapic;
224                         entry->pin = newpin;
225                 }
226                 if (!entry->next)
227                         break;
228                 entry = irq_2_pin + entry->next;
229         }
230 }
231
232 static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
233 {
234         struct irq_pin_list *entry = irq_2_pin + irq;
235         unsigned int pin, reg;
236
237         for (;;) {
238                 pin = entry->pin;
239                 if (pin == -1)
240                         break;
241                 reg = io_apic_read(entry->apic, 0x10 + pin*2);
242                 reg &= ~disable;
243                 reg |= enable;
244                 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
245                 if (!entry->next)
246                         break;
247                 entry = irq_2_pin + entry->next;
248         }
249 }
250
251 /* mask = 1 */
252 static void __mask_IO_APIC_irq (unsigned int irq)
253 {
254         __modify_IO_APIC_irq(irq, 0x00010000, 0);
255 }
256
257 /* mask = 0 */
258 static void __unmask_IO_APIC_irq (unsigned int irq)
259 {
260         __modify_IO_APIC_irq(irq, 0, 0x00010000);
261 }
262
263 /* mask = 1, trigger = 0 */
264 static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
265 {
266         __modify_IO_APIC_irq(irq, 0x00010000, 0x00008000);
267 }
268
269 /* mask = 0, trigger = 1 */
270 static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
271 {
272         __modify_IO_APIC_irq(irq, 0x00008000, 0x00010000);
273 }
274
275 static void mask_IO_APIC_irq (unsigned int irq)
276 {
277         unsigned long flags;
278
279         spin_lock_irqsave(&ioapic_lock, flags);
280         __mask_IO_APIC_irq(irq);
281         spin_unlock_irqrestore(&ioapic_lock, flags);
282 }
283
284 static void unmask_IO_APIC_irq (unsigned int irq)
285 {
286         unsigned long flags;
287
288         spin_lock_irqsave(&ioapic_lock, flags);
289         __unmask_IO_APIC_irq(irq);
290         spin_unlock_irqrestore(&ioapic_lock, flags);
291 }
292
293 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
294 {
295         struct IO_APIC_route_entry entry;
296         
297         /* Check delivery_mode to be sure we're not clearing an SMI pin */
298         entry = ioapic_read_entry(apic, pin);
299         if (entry.delivery_mode == dest_SMI)
300                 return;
301
302         /*
303          * Disable it in the IO-APIC irq-routing table:
304          */
305         ioapic_mask_entry(apic, pin);
306 }
307
308 static void clear_IO_APIC (void)
309 {
310         int apic, pin;
311
312         for (apic = 0; apic < nr_ioapics; apic++)
313                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
314                         clear_IO_APIC_pin(apic, pin);
315 }
316
317 #ifdef CONFIG_SMP
318 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
319 {
320         unsigned long flags;
321         int pin;
322         struct irq_pin_list *entry = irq_2_pin + irq;
323         unsigned int apicid_value;
324         cpumask_t tmp;
325         
326         cpus_and(tmp, cpumask, cpu_online_map);
327         if (cpus_empty(tmp))
328                 tmp = TARGET_CPUS;
329
330         cpus_and(cpumask, tmp, CPU_MASK_ALL);
331
332         apicid_value = cpu_mask_to_apicid(cpumask);
333         /* Prepare to do the io_apic_write */
334         apicid_value = apicid_value << 24;
335         spin_lock_irqsave(&ioapic_lock, flags);
336         for (;;) {
337                 pin = entry->pin;
338                 if (pin == -1)
339                         break;
340                 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
341                 if (!entry->next)
342                         break;
343                 entry = irq_2_pin + entry->next;
344         }
345         set_native_irq_info(irq, cpumask);
346         spin_unlock_irqrestore(&ioapic_lock, flags);
347 }
348
349 #if defined(CONFIG_IRQBALANCE)
350 # include <asm/processor.h>     /* kernel_thread() */
351 # include <linux/kernel_stat.h> /* kstat */
352 # include <linux/slab.h>                /* kmalloc() */
353 # include <linux/timer.h>       /* time_after() */
354  
355 #ifdef CONFIG_BALANCED_IRQ_DEBUG
356 #  define TDprintk(x...) do { printk("<%ld:%s:%d>: ", jiffies, __FILE__, __LINE__); printk(x); } while (0)
357 #  define Dprintk(x...) do { TDprintk(x); } while (0)
358 # else
359 #  define TDprintk(x...) 
360 #  define Dprintk(x...) 
361 # endif
362
363 #define IRQBALANCE_CHECK_ARCH -999
364 #define MAX_BALANCED_IRQ_INTERVAL       (5*HZ)
365 #define MIN_BALANCED_IRQ_INTERVAL       (HZ/2)
366 #define BALANCED_IRQ_MORE_DELTA         (HZ/10)
367 #define BALANCED_IRQ_LESS_DELTA         (HZ)
368
369 static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
370 static int physical_balance __read_mostly;
371 static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
372
373 static struct irq_cpu_info {
374         unsigned long * last_irq;
375         unsigned long * irq_delta;
376         unsigned long irq;
377 } irq_cpu_data[NR_CPUS];
378
379 #define CPU_IRQ(cpu)            (irq_cpu_data[cpu].irq)
380 #define LAST_CPU_IRQ(cpu,irq)   (irq_cpu_data[cpu].last_irq[irq])
381 #define IRQ_DELTA(cpu,irq)      (irq_cpu_data[cpu].irq_delta[irq])
382
383 #define IDLE_ENOUGH(cpu,now) \
384         (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
385
386 #define IRQ_ALLOWED(cpu, allowed_mask)  cpu_isset(cpu, allowed_mask)
387
388 #define CPU_TO_PACKAGEINDEX(i) (first_cpu(cpu_sibling_map[i]))
389
390 static cpumask_t balance_irq_affinity[NR_IRQS] = {
391         [0 ... NR_IRQS-1] = CPU_MASK_ALL
392 };
393
394 void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
395 {
396         balance_irq_affinity[irq] = mask;
397 }
398
399 static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
400                         unsigned long now, int direction)
401 {
402         int search_idle = 1;
403         int cpu = curr_cpu;
404
405         goto inside;
406
407         do {
408                 if (unlikely(cpu == curr_cpu))
409                         search_idle = 0;
410 inside:
411                 if (direction == 1) {
412                         cpu++;
413                         if (cpu >= NR_CPUS)
414                                 cpu = 0;
415                 } else {
416                         cpu--;
417                         if (cpu == -1)
418                                 cpu = NR_CPUS-1;
419                 }
420         } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
421                         (search_idle && !IDLE_ENOUGH(cpu,now)));
422
423         return cpu;
424 }
425
426 static inline void balance_irq(int cpu, int irq)
427 {
428         unsigned long now = jiffies;
429         cpumask_t allowed_mask;
430         unsigned int new_cpu;
431                 
432         if (irqbalance_disabled)
433                 return; 
434
435         cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
436         new_cpu = move(cpu, allowed_mask, now, 1);
437         if (cpu != new_cpu) {
438                 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
439         }
440 }
441
442 static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
443 {
444         int i, j;
445         Dprintk("Rotating IRQs among CPUs.\n");
446         for_each_online_cpu(i) {
447                 for (j = 0; j < NR_IRQS; j++) {
448                         if (!irq_desc[j].action)
449                                 continue;
450                         /* Is it a significant load ?  */
451                         if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
452                                                 useful_load_threshold)
453                                 continue;
454                         balance_irq(i, j);
455                 }
456         }
457         balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
458                 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);       
459         return;
460 }
461
462 static void do_irq_balance(void)
463 {
464         int i, j;
465         unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
466         unsigned long move_this_load = 0;
467         int max_loaded = 0, min_loaded = 0;
468         int load;
469         unsigned long useful_load_threshold = balanced_irq_interval + 10;
470         int selected_irq;
471         int tmp_loaded, first_attempt = 1;
472         unsigned long tmp_cpu_irq;
473         unsigned long imbalance = 0;
474         cpumask_t allowed_mask, target_cpu_mask, tmp;
475
476         for_each_possible_cpu(i) {
477                 int package_index;
478                 CPU_IRQ(i) = 0;
479                 if (!cpu_online(i))
480                         continue;
481                 package_index = CPU_TO_PACKAGEINDEX(i);
482                 for (j = 0; j < NR_IRQS; j++) {
483                         unsigned long value_now, delta;
484                         /* Is this an active IRQ? */
485                         if (!irq_desc[j].action)
486                                 continue;
487                         if ( package_index == i )
488                                 IRQ_DELTA(package_index,j) = 0;
489                         /* Determine the total count per processor per IRQ */
490                         value_now = (unsigned long) kstat_cpu(i).irqs[j];
491
492                         /* Determine the activity per processor per IRQ */
493                         delta = value_now - LAST_CPU_IRQ(i,j);
494
495                         /* Update last_cpu_irq[][] for the next time */
496                         LAST_CPU_IRQ(i,j) = value_now;
497
498                         /* Ignore IRQs whose rate is less than the clock */
499                         if (delta < useful_load_threshold)
500                                 continue;
501                         /* update the load for the processor or package total */
502                         IRQ_DELTA(package_index,j) += delta;
503
504                         /* Keep track of the higher numbered sibling as well */
505                         if (i != package_index)
506                                 CPU_IRQ(i) += delta;
507                         /*
508                          * We have sibling A and sibling B in the package
509                          *
510                          * cpu_irq[A] = load for cpu A + load for cpu B
511                          * cpu_irq[B] = load for cpu B
512                          */
513                         CPU_IRQ(package_index) += delta;
514                 }
515         }
516         /* Find the least loaded processor package */
517         for_each_online_cpu(i) {
518                 if (i != CPU_TO_PACKAGEINDEX(i))
519                         continue;
520                 if (min_cpu_irq > CPU_IRQ(i)) {
521                         min_cpu_irq = CPU_IRQ(i);
522                         min_loaded = i;
523                 }
524         }
525         max_cpu_irq = ULONG_MAX;
526
527 tryanothercpu:
528         /* Look for heaviest loaded processor.
529          * We may come back to get the next heaviest loaded processor.
530          * Skip processors with trivial loads.
531          */
532         tmp_cpu_irq = 0;
533         tmp_loaded = -1;
534         for_each_online_cpu(i) {
535                 if (i != CPU_TO_PACKAGEINDEX(i))
536                         continue;
537                 if (max_cpu_irq <= CPU_IRQ(i)) 
538                         continue;
539                 if (tmp_cpu_irq < CPU_IRQ(i)) {
540                         tmp_cpu_irq = CPU_IRQ(i);
541                         tmp_loaded = i;
542                 }
543         }
544
545         if (tmp_loaded == -1) {
546          /* In the case of small number of heavy interrupt sources, 
547           * loading some of the cpus too much. We use Ingo's original 
548           * approach to rotate them around.
549           */
550                 if (!first_attempt && imbalance >= useful_load_threshold) {
551                         rotate_irqs_among_cpus(useful_load_threshold);
552                         return;
553                 }
554                 goto not_worth_the_effort;
555         }
556         
557         first_attempt = 0;              /* heaviest search */
558         max_cpu_irq = tmp_cpu_irq;      /* load */
559         max_loaded = tmp_loaded;        /* processor */
560         imbalance = (max_cpu_irq - min_cpu_irq) / 2;
561         
562         Dprintk("max_loaded cpu = %d\n", max_loaded);
563         Dprintk("min_loaded cpu = %d\n", min_loaded);
564         Dprintk("max_cpu_irq load = %ld\n", max_cpu_irq);
565         Dprintk("min_cpu_irq load = %ld\n", min_cpu_irq);
566         Dprintk("load imbalance = %lu\n", imbalance);
567
568         /* if imbalance is less than approx 10% of max load, then
569          * observe diminishing returns action. - quit
570          */
571         if (imbalance < (max_cpu_irq >> 3)) {
572                 Dprintk("Imbalance too trivial\n");
573                 goto not_worth_the_effort;
574         }
575
576 tryanotherirq:
577         /* if we select an IRQ to move that can't go where we want, then
578          * see if there is another one to try.
579          */
580         move_this_load = 0;
581         selected_irq = -1;
582         for (j = 0; j < NR_IRQS; j++) {
583                 /* Is this an active IRQ? */
584                 if (!irq_desc[j].action)
585                         continue;
586                 if (imbalance <= IRQ_DELTA(max_loaded,j))
587                         continue;
588                 /* Try to find the IRQ that is closest to the imbalance
589                  * without going over.
590                  */
591                 if (move_this_load < IRQ_DELTA(max_loaded,j)) {
592                         move_this_load = IRQ_DELTA(max_loaded,j);
593                         selected_irq = j;
594                 }
595         }
596         if (selected_irq == -1) {
597                 goto tryanothercpu;
598         }
599
600         imbalance = move_this_load;
601         
602         /* For physical_balance case, we accumlated both load
603          * values in the one of the siblings cpu_irq[],
604          * to use the same code for physical and logical processors
605          * as much as possible. 
606          *
607          * NOTE: the cpu_irq[] array holds the sum of the load for
608          * sibling A and sibling B in the slot for the lowest numbered
609          * sibling (A), _AND_ the load for sibling B in the slot for
610          * the higher numbered sibling.
611          *
612          * We seek the least loaded sibling by making the comparison
613          * (A+B)/2 vs B
614          */
615         load = CPU_IRQ(min_loaded) >> 1;
616         for_each_cpu_mask(j, cpu_sibling_map[min_loaded]) {
617                 if (load > CPU_IRQ(j)) {
618                         /* This won't change cpu_sibling_map[min_loaded] */
619                         load = CPU_IRQ(j);
620                         min_loaded = j;
621                 }
622         }
623
624         cpus_and(allowed_mask,
625                 cpu_online_map,
626                 balance_irq_affinity[selected_irq]);
627         target_cpu_mask = cpumask_of_cpu(min_loaded);
628         cpus_and(tmp, target_cpu_mask, allowed_mask);
629
630         if (!cpus_empty(tmp)) {
631
632                 Dprintk("irq = %d moved to cpu = %d\n",
633                                 selected_irq, min_loaded);
634                 /* mark for change destination */
635                 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
636
637                 /* Since we made a change, come back sooner to 
638                  * check for more variation.
639                  */
640                 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
641                         balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);       
642                 return;
643         }
644         goto tryanotherirq;
645
646 not_worth_the_effort:
647         /*
648          * if we did not find an IRQ to move, then adjust the time interval
649          * upward
650          */
651         balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
652                 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);       
653         Dprintk("IRQ worth rotating not found\n");
654         return;
655 }
656
657 static int balanced_irq(void *unused)
658 {
659         int i;
660         unsigned long prev_balance_time = jiffies;
661         long time_remaining = balanced_irq_interval;
662
663         daemonize("kirqd");
664         
665         /* push everything to CPU 0 to give us a starting point.  */
666         for (i = 0 ; i < NR_IRQS ; i++) {
667                 irq_desc[i].pending_mask = cpumask_of_cpu(0);
668                 set_pending_irq(i, cpumask_of_cpu(0));
669         }
670
671         for ( ; ; ) {
672                 time_remaining = schedule_timeout_interruptible(time_remaining);
673                 try_to_freeze();
674                 if (time_after(jiffies,
675                                 prev_balance_time+balanced_irq_interval)) {
676                         preempt_disable();
677                         do_irq_balance();
678                         prev_balance_time = jiffies;
679                         time_remaining = balanced_irq_interval;
680                         preempt_enable();
681                 }
682         }
683         return 0;
684 }
685
686 static int __init balanced_irq_init(void)
687 {
688         int i;
689         struct cpuinfo_x86 *c;
690         cpumask_t tmp;
691
692         cpus_shift_right(tmp, cpu_online_map, 2);
693         c = &boot_cpu_data;
694         /* When not overwritten by the command line ask subarchitecture. */
695         if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
696                 irqbalance_disabled = NO_BALANCE_IRQ;
697         if (irqbalance_disabled)
698                 return 0;
699         
700          /* disable irqbalance completely if there is only one processor online */
701         if (num_online_cpus() < 2) {
702                 irqbalance_disabled = 1;
703                 return 0;
704         }
705         /*
706          * Enable physical balance only if more than 1 physical processor
707          * is present
708          */
709         if (smp_num_siblings > 1 && !cpus_empty(tmp))
710                 physical_balance = 1;
711
712         for_each_online_cpu(i) {
713                 irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
714                 irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
715                 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
716                         printk(KERN_ERR "balanced_irq_init: out of memory");
717                         goto failed;
718                 }
719                 memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
720                 memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
721         }
722         
723         printk(KERN_INFO "Starting balanced_irq\n");
724         if (kernel_thread(balanced_irq, NULL, CLONE_KERNEL) >= 0) 
725                 return 0;
726         else 
727                 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
728 failed:
729         for_each_possible_cpu(i) {
730                 kfree(irq_cpu_data[i].irq_delta);
731                 irq_cpu_data[i].irq_delta = NULL;
732                 kfree(irq_cpu_data[i].last_irq);
733                 irq_cpu_data[i].last_irq = NULL;
734         }
735         return 0;
736 }
737
738 int __init irqbalance_disable(char *str)
739 {
740         irqbalance_disabled = 1;
741         return 1;
742 }
743
744 __setup("noirqbalance", irqbalance_disable);
745
746 late_initcall(balanced_irq_init);
747 #endif /* CONFIG_IRQBALANCE */
748 #endif /* CONFIG_SMP */
749
750 #ifndef CONFIG_SMP
751 void fastcall send_IPI_self(int vector)
752 {
753         unsigned int cfg;
754
755         /*
756          * Wait for idle.
757          */
758         apic_wait_icr_idle();
759         cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
760         /*
761          * Send the IPI. The write to APIC_ICR fires this off.
762          */
763         apic_write_around(APIC_ICR, cfg);
764 }
765 #endif /* !CONFIG_SMP */
766
767
768 /*
769  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
770  * specific CPU-side IRQs.
771  */
772
773 #define MAX_PIRQS 8
774 static int pirq_entries [MAX_PIRQS];
775 static int pirqs_enabled;
776 int skip_ioapic_setup;
777
778 static int __init ioapic_setup(char *str)
779 {
780         skip_ioapic_setup = 1;
781         return 1;
782 }
783
784 __setup("noapic", ioapic_setup);
785
786 static int __init ioapic_pirq_setup(char *str)
787 {
788         int i, max;
789         int ints[MAX_PIRQS+1];
790
791         get_options(str, ARRAY_SIZE(ints), ints);
792
793         for (i = 0; i < MAX_PIRQS; i++)
794                 pirq_entries[i] = -1;
795
796         pirqs_enabled = 1;
797         apic_printk(APIC_VERBOSE, KERN_INFO
798                         "PIRQ redirection, working around broken MP-BIOS.\n");
799         max = MAX_PIRQS;
800         if (ints[0] < MAX_PIRQS)
801                 max = ints[0];
802
803         for (i = 0; i < max; i++) {
804                 apic_printk(APIC_VERBOSE, KERN_DEBUG
805                                 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
806                 /*
807                  * PIRQs are mapped upside down, usually.
808                  */
809                 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
810         }
811         return 1;
812 }
813
814 __setup("pirq=", ioapic_pirq_setup);
815
816 /*
817  * Find the IRQ entry number of a certain pin.
818  */
819 static int find_irq_entry(int apic, int pin, int type)
820 {
821         int i;
822
823         for (i = 0; i < mp_irq_entries; i++)
824                 if (mp_irqs[i].mpc_irqtype == type &&
825                     (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
826                      mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
827                     mp_irqs[i].mpc_dstirq == pin)
828                         return i;
829
830         return -1;
831 }
832
833 /*
834  * Find the pin to which IRQ[irq] (ISA) is connected
835  */
836 static int __init find_isa_irq_pin(int irq, int type)
837 {
838         int i;
839
840         for (i = 0; i < mp_irq_entries; i++) {
841                 int lbus = mp_irqs[i].mpc_srcbus;
842
843                 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
844                      mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
845                      mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
846                      mp_bus_id_to_type[lbus] == MP_BUS_NEC98
847                     ) &&
848                     (mp_irqs[i].mpc_irqtype == type) &&
849                     (mp_irqs[i].mpc_srcbusirq == irq))
850
851                         return mp_irqs[i].mpc_dstirq;
852         }
853         return -1;
854 }
855
856 static int __init find_isa_irq_apic(int irq, int type)
857 {
858         int i;
859
860         for (i = 0; i < mp_irq_entries; i++) {
861                 int lbus = mp_irqs[i].mpc_srcbus;
862
863                 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
864                      mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
865                      mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
866                      mp_bus_id_to_type[lbus] == MP_BUS_NEC98
867                     ) &&
868                     (mp_irqs[i].mpc_irqtype == type) &&
869                     (mp_irqs[i].mpc_srcbusirq == irq))
870                         break;
871         }
872         if (i < mp_irq_entries) {
873                 int apic;
874                 for(apic = 0; apic < nr_ioapics; apic++) {
875                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
876                                 return apic;
877                 }
878         }
879
880         return -1;
881 }
882
883 /*
884  * Find a specific PCI IRQ entry.
885  * Not an __init, possibly needed by modules
886  */
887 static int pin_2_irq(int idx, int apic, int pin);
888
889 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
890 {
891         int apic, i, best_guess = -1;
892
893         apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
894                 "slot:%d, pin:%d.\n", bus, slot, pin);
895         if (mp_bus_id_to_pci_bus[bus] == -1) {
896                 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
897                 return -1;
898         }
899         for (i = 0; i < mp_irq_entries; i++) {
900                 int lbus = mp_irqs[i].mpc_srcbus;
901
902                 for (apic = 0; apic < nr_ioapics; apic++)
903                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
904                             mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
905                                 break;
906
907                 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
908                     !mp_irqs[i].mpc_irqtype &&
909                     (bus == lbus) &&
910                     (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
911                         int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
912
913                         if (!(apic || IO_APIC_IRQ(irq)))
914                                 continue;
915
916                         if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
917                                 return irq;
918                         /*
919                          * Use the first all-but-pin matching entry as a
920                          * best-guess fuzzy result for broken mptables.
921                          */
922                         if (best_guess < 0)
923                                 best_guess = irq;
924                 }
925         }
926         return best_guess;
927 }
928 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
929
930 /*
931  * This function currently is only a helper for the i386 smp boot process where 
932  * we need to reprogram the ioredtbls to cater for the cpus which have come online
933  * so mask in all cases should simply be TARGET_CPUS
934  */
935 #ifdef CONFIG_SMP
936 void __init setup_ioapic_dest(void)
937 {
938         int pin, ioapic, irq, irq_entry;
939
940         if (skip_ioapic_setup == 1)
941                 return;
942
943         for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
944                 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
945                         irq_entry = find_irq_entry(ioapic, pin, mp_INT);
946                         if (irq_entry == -1)
947                                 continue;
948                         irq = pin_2_irq(irq_entry, ioapic, pin);
949                         set_ioapic_affinity_irq(irq, TARGET_CPUS);
950                 }
951
952         }
953 }
954 #endif
955
956 /*
957  * EISA Edge/Level control register, ELCR
958  */
959 static int EISA_ELCR(unsigned int irq)
960 {
961         if (irq < 16) {
962                 unsigned int port = 0x4d0 + (irq >> 3);
963                 return (inb(port) >> (irq & 7)) & 1;
964         }
965         apic_printk(APIC_VERBOSE, KERN_INFO
966                         "Broken MPtable reports ISA irq %d\n", irq);
967         return 0;
968 }
969
970 /* EISA interrupts are always polarity zero and can be edge or level
971  * trigger depending on the ELCR value.  If an interrupt is listed as
972  * EISA conforming in the MP table, that means its trigger type must
973  * be read in from the ELCR */
974
975 #define default_EISA_trigger(idx)       (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
976 #define default_EISA_polarity(idx)      (0)
977
978 /* ISA interrupts are always polarity zero edge triggered,
979  * when listed as conforming in the MP table. */
980
981 #define default_ISA_trigger(idx)        (0)
982 #define default_ISA_polarity(idx)       (0)
983
984 /* PCI interrupts are always polarity one level triggered,
985  * when listed as conforming in the MP table. */
986
987 #define default_PCI_trigger(idx)        (1)
988 #define default_PCI_polarity(idx)       (1)
989
990 /* MCA interrupts are always polarity zero level triggered,
991  * when listed as conforming in the MP table. */
992
993 #define default_MCA_trigger(idx)        (1)
994 #define default_MCA_polarity(idx)       (0)
995
996 /* NEC98 interrupts are always polarity zero edge triggered,
997  * when listed as conforming in the MP table. */
998
999 #define default_NEC98_trigger(idx)     (0)
1000 #define default_NEC98_polarity(idx)    (0)
1001
1002 static int __init MPBIOS_polarity(int idx)
1003 {
1004         int bus = mp_irqs[idx].mpc_srcbus;
1005         int polarity;
1006
1007         /*
1008          * Determine IRQ line polarity (high active or low active):
1009          */
1010         switch (mp_irqs[idx].mpc_irqflag & 3)
1011         {
1012                 case 0: /* conforms, ie. bus-type dependent polarity */
1013                 {
1014                         switch (mp_bus_id_to_type[bus])
1015                         {
1016                                 case MP_BUS_ISA: /* ISA pin */
1017                                 {
1018                                         polarity = default_ISA_polarity(idx);
1019                                         break;
1020                                 }
1021                                 case MP_BUS_EISA: /* EISA pin */
1022                                 {
1023                                         polarity = default_EISA_polarity(idx);
1024                                         break;
1025                                 }
1026                                 case MP_BUS_PCI: /* PCI pin */
1027                                 {
1028                                         polarity = default_PCI_polarity(idx);
1029                                         break;
1030                                 }
1031                                 case MP_BUS_MCA: /* MCA pin */
1032                                 {
1033                                         polarity = default_MCA_polarity(idx);
1034                                         break;
1035                                 }
1036                                 case MP_BUS_NEC98: /* NEC 98 pin */
1037                                 {
1038                                         polarity = default_NEC98_polarity(idx);
1039                                         break;
1040                                 }
1041                                 default:
1042                                 {
1043                                         printk(KERN_WARNING "broken BIOS!!\n");
1044                                         polarity = 1;
1045                                         break;
1046                                 }
1047                         }
1048                         break;
1049                 }
1050                 case 1: /* high active */
1051                 {
1052                         polarity = 0;
1053                         break;
1054                 }
1055                 case 2: /* reserved */
1056                 {
1057                         printk(KERN_WARNING "broken BIOS!!\n");
1058                         polarity = 1;
1059                         break;
1060                 }
1061                 case 3: /* low active */
1062                 {
1063                         polarity = 1;
1064                         break;
1065                 }
1066                 default: /* invalid */
1067                 {
1068                         printk(KERN_WARNING "broken BIOS!!\n");
1069                         polarity = 1;
1070                         break;
1071                 }
1072         }
1073         return polarity;
1074 }
1075
1076 static int MPBIOS_trigger(int idx)
1077 {
1078         int bus = mp_irqs[idx].mpc_srcbus;
1079         int trigger;
1080
1081         /*
1082          * Determine IRQ trigger mode (edge or level sensitive):
1083          */
1084         switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
1085         {
1086                 case 0: /* conforms, ie. bus-type dependent */
1087                 {
1088                         switch (mp_bus_id_to_type[bus])
1089                         {
1090                                 case MP_BUS_ISA: /* ISA pin */
1091                                 {
1092                                         trigger = default_ISA_trigger(idx);
1093                                         break;
1094                                 }
1095                                 case MP_BUS_EISA: /* EISA pin */
1096                                 {
1097                                         trigger = default_EISA_trigger(idx);
1098                                         break;
1099                                 }
1100                                 case MP_BUS_PCI: /* PCI pin */
1101                                 {
1102                                         trigger = default_PCI_trigger(idx);
1103                                         break;
1104                                 }
1105                                 case MP_BUS_MCA: /* MCA pin */
1106                                 {
1107                                         trigger = default_MCA_trigger(idx);
1108                                         break;
1109                                 }
1110                                 case MP_BUS_NEC98: /* NEC 98 pin */
1111                                 {
1112                                         trigger = default_NEC98_trigger(idx);
1113                                         break;
1114                                 }
1115                                 default:
1116                                 {
1117                                         printk(KERN_WARNING "broken BIOS!!\n");
1118                                         trigger = 1;
1119                                         break;
1120                                 }
1121                         }
1122                         break;
1123                 }
1124                 case 1: /* edge */
1125                 {
1126                         trigger = 0;
1127                         break;
1128                 }
1129                 case 2: /* reserved */
1130                 {
1131                         printk(KERN_WARNING "broken BIOS!!\n");
1132                         trigger = 1;
1133                         break;
1134                 }
1135                 case 3: /* level */
1136                 {
1137                         trigger = 1;
1138                         break;
1139                 }
1140                 default: /* invalid */
1141                 {
1142                         printk(KERN_WARNING "broken BIOS!!\n");
1143                         trigger = 0;
1144                         break;
1145                 }
1146         }
1147         return trigger;
1148 }
1149
1150 static inline int irq_polarity(int idx)
1151 {
1152         return MPBIOS_polarity(idx);
1153 }
1154
1155 static inline int irq_trigger(int idx)
1156 {
1157         return MPBIOS_trigger(idx);
1158 }
1159
1160 static int pin_2_irq(int idx, int apic, int pin)
1161 {
1162         int irq, i;
1163         int bus = mp_irqs[idx].mpc_srcbus;
1164
1165         /*
1166          * Debugging check, we are in big trouble if this message pops up!
1167          */
1168         if (mp_irqs[idx].mpc_dstirq != pin)
1169                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1170
1171         switch (mp_bus_id_to_type[bus])
1172         {
1173                 case MP_BUS_ISA: /* ISA pin */
1174                 case MP_BUS_EISA:
1175                 case MP_BUS_MCA:
1176                 case MP_BUS_NEC98:
1177                 {
1178                         irq = mp_irqs[idx].mpc_srcbusirq;
1179                         break;
1180                 }
1181                 case MP_BUS_PCI: /* PCI pin */
1182                 {
1183                         /*
1184                          * PCI IRQs are mapped in order
1185                          */
1186                         i = irq = 0;
1187                         while (i < apic)
1188                                 irq += nr_ioapic_registers[i++];
1189                         irq += pin;
1190
1191                         /*
1192                          * For MPS mode, so far only needed by ES7000 platform
1193                          */
1194                         if (ioapic_renumber_irq)
1195                                 irq = ioapic_renumber_irq(apic, irq);
1196
1197                         break;
1198                 }
1199                 default:
1200                 {
1201                         printk(KERN_ERR "unknown bus type %d.\n",bus); 
1202                         irq = 0;
1203                         break;
1204                 }
1205         }
1206
1207         /*
1208          * PCI IRQ command line redirection. Yes, limits are hardcoded.
1209          */
1210         if ((pin >= 16) && (pin <= 23)) {
1211                 if (pirq_entries[pin-16] != -1) {
1212                         if (!pirq_entries[pin-16]) {
1213                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1214                                                 "disabling PIRQ%d\n", pin-16);
1215                         } else {
1216                                 irq = pirq_entries[pin-16];
1217                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1218                                                 "using PIRQ%d -> IRQ %d\n",
1219                                                 pin-16, irq);
1220                         }
1221                 }
1222         }
1223         return irq;
1224 }
1225
1226 static inline int IO_APIC_irq_trigger(int irq)
1227 {
1228         int apic, idx, pin;
1229
1230         for (apic = 0; apic < nr_ioapics; apic++) {
1231                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1232                         idx = find_irq_entry(apic,pin,mp_INT);
1233                         if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1234                                 return irq_trigger(idx);
1235                 }
1236         }
1237         /*
1238          * nonexistent IRQs are edge default
1239          */
1240         return 0;
1241 }
1242
1243 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
1244 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
1245
1246 static int __assign_irq_vector(int irq)
1247 {
1248         static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
1249         int vector;
1250
1251         BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
1252
1253         if (irq_vector[irq] > 0)
1254                 return irq_vector[irq];
1255
1256         current_vector += 8;
1257         if (current_vector == SYSCALL_VECTOR)
1258                 current_vector += 8;
1259
1260         if (current_vector >= FIRST_SYSTEM_VECTOR) {
1261                 offset++;
1262                 if (!(offset % 8))
1263                         return -ENOSPC;
1264                 current_vector = FIRST_DEVICE_VECTOR + offset;
1265         }
1266
1267         vector = current_vector;
1268         irq_vector[irq] = vector;
1269
1270         return vector;
1271 }
1272
1273 static int assign_irq_vector(int irq)
1274 {
1275         unsigned long flags;
1276         int vector;
1277
1278         spin_lock_irqsave(&vector_lock, flags);
1279         vector = __assign_irq_vector(irq);
1280         spin_unlock_irqrestore(&vector_lock, flags);
1281
1282         return vector;
1283 }
1284 static struct irq_chip ioapic_chip;
1285
1286 #define IOAPIC_AUTO     -1
1287 #define IOAPIC_EDGE     0
1288 #define IOAPIC_LEVEL    1
1289
1290 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
1291 {
1292         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1293                         trigger == IOAPIC_LEVEL)
1294                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1295                                          handle_fasteoi_irq, "fasteoi");
1296         else {
1297                 irq_desc[irq].status |= IRQ_DELAYED_DISABLE;
1298                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1299                                          handle_edge_irq, "edge");
1300         }
1301         set_intr_gate(vector, interrupt[irq]);
1302 }
1303
1304 static void __init setup_IO_APIC_irqs(void)
1305 {
1306         struct IO_APIC_route_entry entry;
1307         int apic, pin, idx, irq, first_notcon = 1, vector;
1308         unsigned long flags;
1309
1310         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1311
1312         for (apic = 0; apic < nr_ioapics; apic++) {
1313         for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1314
1315                 /*
1316                  * add it to the IO-APIC irq-routing table:
1317                  */
1318                 memset(&entry,0,sizeof(entry));
1319
1320                 entry.delivery_mode = INT_DELIVERY_MODE;
1321                 entry.dest_mode = INT_DEST_MODE;
1322                 entry.mask = 0;                         /* enable IRQ */
1323                 entry.dest.logical.logical_dest = 
1324                                         cpu_mask_to_apicid(TARGET_CPUS);
1325
1326                 idx = find_irq_entry(apic,pin,mp_INT);
1327                 if (idx == -1) {
1328                         if (first_notcon) {
1329                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1330                                                 " IO-APIC (apicid-pin) %d-%d",
1331                                                 mp_ioapics[apic].mpc_apicid,
1332                                                 pin);
1333                                 first_notcon = 0;
1334                         } else
1335                                 apic_printk(APIC_VERBOSE, ", %d-%d",
1336                                         mp_ioapics[apic].mpc_apicid, pin);
1337                         continue;
1338                 }
1339
1340                 entry.trigger = irq_trigger(idx);
1341                 entry.polarity = irq_polarity(idx);
1342
1343                 if (irq_trigger(idx)) {
1344                         entry.trigger = 1;
1345                         entry.mask = 1;
1346                 }
1347
1348                 irq = pin_2_irq(idx, apic, pin);
1349                 /*
1350                  * skip adding the timer int on secondary nodes, which causes
1351                  * a small but painful rift in the time-space continuum
1352                  */
1353                 if (multi_timer_check(apic, irq))
1354                         continue;
1355                 else
1356                         add_pin_to_irq(irq, apic, pin);
1357
1358                 if (!apic && !IO_APIC_IRQ(irq))
1359                         continue;
1360
1361                 if (IO_APIC_IRQ(irq)) {
1362                         vector = assign_irq_vector(irq);
1363                         entry.vector = vector;
1364                         ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1365                 
1366                         if (!apic && (irq < 16))
1367                                 disable_8259A_irq(irq);
1368                 }
1369                 spin_lock_irqsave(&ioapic_lock, flags);
1370                 __ioapic_write_entry(apic, pin, entry);
1371                 set_native_irq_info(irq, TARGET_CPUS);
1372                 spin_unlock_irqrestore(&ioapic_lock, flags);
1373         }
1374         }
1375
1376         if (!first_notcon)
1377                 apic_printk(APIC_VERBOSE, " not connected.\n");
1378 }
1379
1380 /*
1381  * Set up the 8259A-master output pin:
1382  */
1383 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
1384 {
1385         struct IO_APIC_route_entry entry;
1386
1387         memset(&entry,0,sizeof(entry));
1388
1389         disable_8259A_irq(0);
1390
1391         /* mask LVT0 */
1392         apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1393
1394         /*
1395          * We use logical delivery to get the timer IRQ
1396          * to the first CPU.
1397          */
1398         entry.dest_mode = INT_DEST_MODE;
1399         entry.mask = 0;                                 /* unmask IRQ now */
1400         entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1401         entry.delivery_mode = INT_DELIVERY_MODE;
1402         entry.polarity = 0;
1403         entry.trigger = 0;
1404         entry.vector = vector;
1405
1406         /*
1407          * The timer IRQ doesn't have to know that behind the
1408          * scene we have a 8259A-master in AEOI mode ...
1409          */
1410         irq_desc[0].chip = &ioapic_chip;
1411         set_irq_handler(0, handle_edge_irq);
1412
1413         /*
1414          * Add it to the IO-APIC irq-routing table:
1415          */
1416         ioapic_write_entry(apic, pin, entry);
1417
1418         enable_8259A_irq(0);
1419 }
1420
1421 static inline void UNEXPECTED_IO_APIC(void)
1422 {
1423 }
1424
1425 void __init print_IO_APIC(void)
1426 {
1427         int apic, i;
1428         union IO_APIC_reg_00 reg_00;
1429         union IO_APIC_reg_01 reg_01;
1430         union IO_APIC_reg_02 reg_02;
1431         union IO_APIC_reg_03 reg_03;
1432         unsigned long flags;
1433
1434         if (apic_verbosity == APIC_QUIET)
1435                 return;
1436
1437         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1438         for (i = 0; i < nr_ioapics; i++)
1439                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1440                        mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1441
1442         /*
1443          * We are a bit conservative about what we expect.  We have to
1444          * know about every hardware change ASAP.
1445          */
1446         printk(KERN_INFO "testing the IO APIC.......................\n");
1447
1448         for (apic = 0; apic < nr_ioapics; apic++) {
1449
1450         spin_lock_irqsave(&ioapic_lock, flags);
1451         reg_00.raw = io_apic_read(apic, 0);
1452         reg_01.raw = io_apic_read(apic, 1);
1453         if (reg_01.bits.version >= 0x10)
1454                 reg_02.raw = io_apic_read(apic, 2);
1455         if (reg_01.bits.version >= 0x20)
1456                 reg_03.raw = io_apic_read(apic, 3);
1457         spin_unlock_irqrestore(&ioapic_lock, flags);
1458
1459         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1460         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1461         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
1462         printk(KERN_DEBUG ".......    : Delivery Type: %X\n", reg_00.bits.delivery_type);
1463         printk(KERN_DEBUG ".......    : LTS          : %X\n", reg_00.bits.LTS);
1464         if (reg_00.bits.ID >= get_physical_broadcast())
1465                 UNEXPECTED_IO_APIC();
1466         if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1467                 UNEXPECTED_IO_APIC();
1468
1469         printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1470         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
1471         if (    (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1472                 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1473                 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1474                 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1475                 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1476                 (reg_01.bits.entries != 0x2E) &&
1477                 (reg_01.bits.entries != 0x3F)
1478         )
1479                 UNEXPECTED_IO_APIC();
1480
1481         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1482         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
1483         if (    (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1484                 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1485                 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1486                 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1487                 (reg_01.bits.version != 0x20)    /* Intel P64H (82806 AA) */
1488         )
1489                 UNEXPECTED_IO_APIC();
1490         if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1491                 UNEXPECTED_IO_APIC();
1492
1493         /*
1494          * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1495          * but the value of reg_02 is read as the previous read register
1496          * value, so ignore it if reg_02 == reg_01.
1497          */
1498         if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1499                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1500                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1501                 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1502                         UNEXPECTED_IO_APIC();
1503         }
1504
1505         /*
1506          * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1507          * or reg_03, but the value of reg_0[23] is read as the previous read
1508          * register value, so ignore it if reg_03 == reg_0[12].
1509          */
1510         if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1511             reg_03.raw != reg_01.raw) {
1512                 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1513                 printk(KERN_DEBUG ".......     : Boot DT    : %X\n", reg_03.bits.boot_DT);
1514                 if (reg_03.bits.__reserved_1)
1515                         UNEXPECTED_IO_APIC();
1516         }
1517
1518         printk(KERN_DEBUG ".... IRQ redirection table:\n");
1519
1520         printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1521                           " Stat Dest Deli Vect:   \n");
1522
1523         for (i = 0; i <= reg_01.bits.entries; i++) {
1524                 struct IO_APIC_route_entry entry;
1525
1526                 entry = ioapic_read_entry(apic, i);
1527
1528                 printk(KERN_DEBUG " %02x %03X %02X  ",
1529                         i,
1530                         entry.dest.logical.logical_dest,
1531                         entry.dest.physical.physical_dest
1532                 );
1533
1534                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
1535                         entry.mask,
1536                         entry.trigger,
1537                         entry.irr,
1538                         entry.polarity,
1539                         entry.delivery_status,
1540                         entry.dest_mode,
1541                         entry.delivery_mode,
1542                         entry.vector
1543                 );
1544         }
1545         }
1546         printk(KERN_DEBUG "IRQ to pin mappings:\n");
1547         for (i = 0; i < NR_IRQS; i++) {
1548                 struct irq_pin_list *entry = irq_2_pin + i;
1549                 if (entry->pin < 0)
1550                         continue;
1551                 printk(KERN_DEBUG "IRQ%d ", i);
1552                 for (;;) {
1553                         printk("-> %d:%d", entry->apic, entry->pin);
1554                         if (!entry->next)
1555                                 break;
1556                         entry = irq_2_pin + entry->next;
1557                 }
1558                 printk("\n");
1559         }
1560
1561         printk(KERN_INFO ".................................... done.\n");
1562
1563         return;
1564 }
1565
1566 #if 0
1567
1568 static void print_APIC_bitfield (int base)
1569 {
1570         unsigned int v;
1571         int i, j;
1572
1573         if (apic_verbosity == APIC_QUIET)
1574                 return;
1575
1576         printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1577         for (i = 0; i < 8; i++) {
1578                 v = apic_read(base + i*0x10);
1579                 for (j = 0; j < 32; j++) {
1580                         if (v & (1<<j))
1581                                 printk("1");
1582                         else
1583                                 printk("0");
1584                 }
1585                 printk("\n");
1586         }
1587 }
1588
1589 void /*__init*/ print_local_APIC(void * dummy)
1590 {
1591         unsigned int v, ver, maxlvt;
1592
1593         if (apic_verbosity == APIC_QUIET)
1594                 return;
1595
1596         printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1597                 smp_processor_id(), hard_smp_processor_id());
1598         v = apic_read(APIC_ID);
1599         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, GET_APIC_ID(v));
1600         v = apic_read(APIC_LVR);
1601         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1602         ver = GET_APIC_VERSION(v);
1603         maxlvt = get_maxlvt();
1604
1605         v = apic_read(APIC_TASKPRI);
1606         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1607
1608         if (APIC_INTEGRATED(ver)) {                     /* !82489DX */
1609                 v = apic_read(APIC_ARBPRI);
1610                 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1611                         v & APIC_ARBPRI_MASK);
1612                 v = apic_read(APIC_PROCPRI);
1613                 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1614         }
1615
1616         v = apic_read(APIC_EOI);
1617         printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1618         v = apic_read(APIC_RRR);
1619         printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1620         v = apic_read(APIC_LDR);
1621         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1622         v = apic_read(APIC_DFR);
1623         printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1624         v = apic_read(APIC_SPIV);
1625         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1626
1627         printk(KERN_DEBUG "... APIC ISR field:\n");
1628         print_APIC_bitfield(APIC_ISR);
1629         printk(KERN_DEBUG "... APIC TMR field:\n");
1630         print_APIC_bitfield(APIC_TMR);
1631         printk(KERN_DEBUG "... APIC IRR field:\n");
1632         print_APIC_bitfield(APIC_IRR);
1633
1634         if (APIC_INTEGRATED(ver)) {             /* !82489DX */
1635                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
1636                         apic_write(APIC_ESR, 0);
1637                 v = apic_read(APIC_ESR);
1638                 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1639         }
1640
1641         v = apic_read(APIC_ICR);
1642         printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1643         v = apic_read(APIC_ICR2);
1644         printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1645
1646         v = apic_read(APIC_LVTT);
1647         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1648
1649         if (maxlvt > 3) {                       /* PC is LVT#4. */
1650                 v = apic_read(APIC_LVTPC);
1651                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1652         }
1653         v = apic_read(APIC_LVT0);
1654         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1655         v = apic_read(APIC_LVT1);
1656         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1657
1658         if (maxlvt > 2) {                       /* ERR is LVT#3. */
1659                 v = apic_read(APIC_LVTERR);
1660                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1661         }
1662
1663         v = apic_read(APIC_TMICT);
1664         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1665         v = apic_read(APIC_TMCCT);
1666         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1667         v = apic_read(APIC_TDCR);
1668         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1669         printk("\n");
1670 }
1671
1672 void print_all_local_APICs (void)
1673 {
1674         on_each_cpu(print_local_APIC, NULL, 1, 1);
1675 }
1676
1677 void /*__init*/ print_PIC(void)
1678 {
1679         unsigned int v;
1680         unsigned long flags;
1681
1682         if (apic_verbosity == APIC_QUIET)
1683                 return;
1684
1685         printk(KERN_DEBUG "\nprinting PIC contents\n");
1686
1687         spin_lock_irqsave(&i8259A_lock, flags);
1688
1689         v = inb(0xa1) << 8 | inb(0x21);
1690         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1691
1692         v = inb(0xa0) << 8 | inb(0x20);
1693         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1694
1695         outb(0x0b,0xa0);
1696         outb(0x0b,0x20);
1697         v = inb(0xa0) << 8 | inb(0x20);
1698         outb(0x0a,0xa0);
1699         outb(0x0a,0x20);
1700
1701         spin_unlock_irqrestore(&i8259A_lock, flags);
1702
1703         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1704
1705         v = inb(0x4d1) << 8 | inb(0x4d0);
1706         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1707 }
1708
1709 #endif  /*  0  */
1710
1711 static void __init enable_IO_APIC(void)
1712 {
1713         union IO_APIC_reg_01 reg_01;
1714         int i8259_apic, i8259_pin;
1715         int i, apic;
1716         unsigned long flags;
1717
1718         for (i = 0; i < PIN_MAP_SIZE; i++) {
1719                 irq_2_pin[i].pin = -1;
1720                 irq_2_pin[i].next = 0;
1721         }
1722         if (!pirqs_enabled)
1723                 for (i = 0; i < MAX_PIRQS; i++)
1724                         pirq_entries[i] = -1;
1725
1726         /*
1727          * The number of IO-APIC IRQ registers (== #pins):
1728          */
1729         for (apic = 0; apic < nr_ioapics; apic++) {
1730                 spin_lock_irqsave(&ioapic_lock, flags);
1731                 reg_01.raw = io_apic_read(apic, 1);
1732                 spin_unlock_irqrestore(&ioapic_lock, flags);
1733                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1734         }
1735         for(apic = 0; apic < nr_ioapics; apic++) {
1736                 int pin;
1737                 /* See if any of the pins is in ExtINT mode */
1738                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1739                         struct IO_APIC_route_entry entry;
1740                         entry = ioapic_read_entry(apic, pin);
1741
1742
1743                         /* If the interrupt line is enabled and in ExtInt mode
1744                          * I have found the pin where the i8259 is connected.
1745                          */
1746                         if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1747                                 ioapic_i8259.apic = apic;
1748                                 ioapic_i8259.pin  = pin;
1749                                 goto found_i8259;
1750                         }
1751                 }
1752         }
1753  found_i8259:
1754         /* Look to see what if the MP table has reported the ExtINT */
1755         /* If we could not find the appropriate pin by looking at the ioapic
1756          * the i8259 probably is not connected the ioapic but give the
1757          * mptable a chance anyway.
1758          */
1759         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1760         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1761         /* Trust the MP table if nothing is setup in the hardware */
1762         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1763                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1764                 ioapic_i8259.pin  = i8259_pin;
1765                 ioapic_i8259.apic = i8259_apic;
1766         }
1767         /* Complain if the MP table and the hardware disagree */
1768         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1769                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1770         {
1771                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1772         }
1773
1774         /*
1775          * Do not trust the IO-APIC being empty at bootup
1776          */
1777         clear_IO_APIC();
1778 }
1779
1780 /*
1781  * Not an __init, needed by the reboot code
1782  */
1783 void disable_IO_APIC(void)
1784 {
1785         /*
1786          * Clear the IO-APIC before rebooting:
1787          */
1788         clear_IO_APIC();
1789
1790         /*
1791          * If the i8259 is routed through an IOAPIC
1792          * Put that IOAPIC in virtual wire mode
1793          * so legacy interrupts can be delivered.
1794          */
1795         if (ioapic_i8259.pin != -1) {
1796                 struct IO_APIC_route_entry entry;
1797
1798                 memset(&entry, 0, sizeof(entry));
1799                 entry.mask            = 0; /* Enabled */
1800                 entry.trigger         = 0; /* Edge */
1801                 entry.irr             = 0;
1802                 entry.polarity        = 0; /* High */
1803                 entry.delivery_status = 0;
1804                 entry.dest_mode       = 0; /* Physical */
1805                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1806                 entry.vector          = 0;
1807                 entry.dest.physical.physical_dest =
1808                                         GET_APIC_ID(apic_read(APIC_ID));
1809
1810                 /*
1811                  * Add it to the IO-APIC irq-routing table:
1812                  */
1813                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1814         }
1815         disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1816 }
1817
1818 /*
1819  * function to set the IO-APIC physical IDs based on the
1820  * values stored in the MPC table.
1821  *
1822  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
1823  */
1824
1825 #ifndef CONFIG_X86_NUMAQ
1826 static void __init setup_ioapic_ids_from_mpc(void)
1827 {
1828         union IO_APIC_reg_00 reg_00;
1829         physid_mask_t phys_id_present_map;
1830         int apic;
1831         int i;
1832         unsigned char old_id;
1833         unsigned long flags;
1834
1835         /*
1836          * Don't check I/O APIC IDs for xAPIC systems.  They have
1837          * no meaning without the serial APIC bus.
1838          */
1839         if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1840                 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
1841                 return;
1842         /*
1843          * This is broken; anything with a real cpu count has to
1844          * circumvent this idiocy regardless.
1845          */
1846         phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1847
1848         /*
1849          * Set the IOAPIC ID to the value stored in the MPC table.
1850          */
1851         for (apic = 0; apic < nr_ioapics; apic++) {
1852
1853                 /* Read the register 0 value */
1854                 spin_lock_irqsave(&ioapic_lock, flags);
1855                 reg_00.raw = io_apic_read(apic, 0);
1856                 spin_unlock_irqrestore(&ioapic_lock, flags);
1857                 
1858                 old_id = mp_ioapics[apic].mpc_apicid;
1859
1860                 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1861                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1862                                 apic, mp_ioapics[apic].mpc_apicid);
1863                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1864                                 reg_00.bits.ID);
1865                         mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1866                 }
1867
1868                 /*
1869                  * Sanity check, is the ID really free? Every APIC in a
1870                  * system must have a unique ID or we get lots of nice
1871                  * 'stuck on smp_invalidate_needed IPI wait' messages.
1872                  */
1873                 if (check_apicid_used(phys_id_present_map,
1874                                         mp_ioapics[apic].mpc_apicid)) {
1875                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1876                                 apic, mp_ioapics[apic].mpc_apicid);
1877                         for (i = 0; i < get_physical_broadcast(); i++)
1878                                 if (!physid_isset(i, phys_id_present_map))
1879                                         break;
1880                         if (i >= get_physical_broadcast())
1881                                 panic("Max APIC ID exceeded!\n");
1882                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1883                                 i);
1884                         physid_set(i, phys_id_present_map);
1885                         mp_ioapics[apic].mpc_apicid = i;
1886                 } else {
1887                         physid_mask_t tmp;
1888                         tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1889                         apic_printk(APIC_VERBOSE, "Setting %d in the "
1890                                         "phys_id_present_map\n",
1891                                         mp_ioapics[apic].mpc_apicid);
1892                         physids_or(phys_id_present_map, phys_id_present_map, tmp);
1893                 }
1894
1895
1896                 /*
1897                  * We need to adjust the IRQ routing table
1898                  * if the ID changed.
1899                  */
1900                 if (old_id != mp_ioapics[apic].mpc_apicid)
1901                         for (i = 0; i < mp_irq_entries; i++)
1902                                 if (mp_irqs[i].mpc_dstapic == old_id)
1903                                         mp_irqs[i].mpc_dstapic
1904                                                 = mp_ioapics[apic].mpc_apicid;
1905
1906                 /*
1907                  * Read the right value from the MPC table and
1908                  * write it into the ID register.
1909                  */
1910                 apic_printk(APIC_VERBOSE, KERN_INFO
1911                         "...changing IO-APIC physical APIC ID to %d ...",
1912                         mp_ioapics[apic].mpc_apicid);
1913
1914                 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1915                 spin_lock_irqsave(&ioapic_lock, flags);
1916                 io_apic_write(apic, 0, reg_00.raw);
1917                 spin_unlock_irqrestore(&ioapic_lock, flags);
1918
1919                 /*
1920                  * Sanity check
1921                  */
1922                 spin_lock_irqsave(&ioapic_lock, flags);
1923                 reg_00.raw = io_apic_read(apic, 0);
1924                 spin_unlock_irqrestore(&ioapic_lock, flags);
1925                 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1926                         printk("could not set ID!\n");
1927                 else
1928                         apic_printk(APIC_VERBOSE, " ok.\n");
1929         }
1930 }
1931 #else
1932 static void __init setup_ioapic_ids_from_mpc(void) { }
1933 #endif
1934
1935 /*
1936  * There is a nasty bug in some older SMP boards, their mptable lies
1937  * about the timer IRQ. We do the following to work around the situation:
1938  *
1939  *      - timer IRQ defaults to IO-APIC IRQ
1940  *      - if this function detects that timer IRQs are defunct, then we fall
1941  *        back to ISA timer IRQs
1942  */
1943 static int __init timer_irq_works(void)
1944 {
1945         unsigned long t1 = jiffies;
1946
1947         local_irq_enable();
1948         /* Let ten ticks pass... */
1949         mdelay((10 * 1000) / HZ);
1950
1951         /*
1952          * Expect a few ticks at least, to be sure some possible
1953          * glue logic does not lock up after one or two first
1954          * ticks in a non-ExtINT mode.  Also the local APIC
1955          * might have cached one ExtINT interrupt.  Finally, at
1956          * least one tick may be lost due to delays.
1957          */
1958         if (jiffies - t1 > 4)
1959                 return 1;
1960
1961         return 0;
1962 }
1963
1964 /*
1965  * In the SMP+IOAPIC case it might happen that there are an unspecified
1966  * number of pending IRQ events unhandled. These cases are very rare,
1967  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1968  * better to do it this way as thus we do not have to be aware of
1969  * 'pending' interrupts in the IRQ path, except at this point.
1970  */
1971 /*
1972  * Edge triggered needs to resend any interrupt
1973  * that was delayed but this is now handled in the device
1974  * independent code.
1975  */
1976
1977 /*
1978  * Startup quirk:
1979  *
1980  * Starting up a edge-triggered IO-APIC interrupt is
1981  * nasty - we need to make sure that we get the edge.
1982  * If it is already asserted for some reason, we need
1983  * return 1 to indicate that is was pending.
1984  *
1985  * This is not complete - we should be able to fake
1986  * an edge even if it isn't on the 8259A...
1987  *
1988  * (We do this for level-triggered IRQs too - it cannot hurt.)
1989  */
1990 static unsigned int startup_ioapic_irq(unsigned int irq)
1991 {
1992         int was_pending = 0;
1993         unsigned long flags;
1994
1995         spin_lock_irqsave(&ioapic_lock, flags);
1996         if (irq < 16) {
1997                 disable_8259A_irq(irq);
1998                 if (i8259A_irq_pending(irq))
1999                         was_pending = 1;
2000         }
2001         __unmask_IO_APIC_irq(irq);
2002         spin_unlock_irqrestore(&ioapic_lock, flags);
2003
2004         return was_pending;
2005 }
2006
2007 static void ack_ioapic_irq(unsigned int irq)
2008 {
2009         move_native_irq(irq);
2010         ack_APIC_irq();
2011 }
2012
2013 static void ack_ioapic_quirk_irq(unsigned int irq)
2014 {
2015         unsigned long v;
2016         int i;
2017
2018         move_native_irq(irq);
2019 /*
2020  * It appears there is an erratum which affects at least version 0x11
2021  * of I/O APIC (that's the 82093AA and cores integrated into various
2022  * chipsets).  Under certain conditions a level-triggered interrupt is
2023  * erroneously delivered as edge-triggered one but the respective IRR
2024  * bit gets set nevertheless.  As a result the I/O unit expects an EOI
2025  * message but it will never arrive and further interrupts are blocked
2026  * from the source.  The exact reason is so far unknown, but the
2027  * phenomenon was observed when two consecutive interrupt requests
2028  * from a given source get delivered to the same CPU and the source is
2029  * temporarily disabled in between.
2030  *
2031  * A workaround is to simulate an EOI message manually.  We achieve it
2032  * by setting the trigger mode to edge and then to level when the edge
2033  * trigger mode gets detected in the TMR of a local APIC for a
2034  * level-triggered interrupt.  We mask the source for the time of the
2035  * operation to prevent an edge-triggered interrupt escaping meanwhile.
2036  * The idea is from Manfred Spraul.  --macro
2037  */
2038         i = irq_vector[irq];
2039
2040         v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2041
2042         ack_APIC_irq();
2043
2044         if (!(v & (1 << (i & 0x1f)))) {
2045                 atomic_inc(&irq_mis_count);
2046                 spin_lock(&ioapic_lock);
2047                 __mask_and_edge_IO_APIC_irq(irq);
2048                 __unmask_and_level_IO_APIC_irq(irq);
2049                 spin_unlock(&ioapic_lock);
2050         }
2051 }
2052
2053 static int ioapic_retrigger_irq(unsigned int irq)
2054 {
2055         send_IPI_self(irq_vector[irq]);
2056
2057         return 1;
2058 }
2059
2060 static struct irq_chip ioapic_chip __read_mostly = {
2061         .name           = "IO-APIC",
2062         .startup        = startup_ioapic_irq,
2063         .mask           = mask_IO_APIC_irq,
2064         .unmask         = unmask_IO_APIC_irq,
2065         .ack            = ack_ioapic_irq,
2066         .eoi            = ack_ioapic_quirk_irq,
2067 #ifdef CONFIG_SMP
2068         .set_affinity   = set_ioapic_affinity_irq,
2069 #endif
2070         .retrigger      = ioapic_retrigger_irq,
2071 };
2072
2073
2074 static inline void init_IO_APIC_traps(void)
2075 {
2076         int irq;
2077
2078         /*
2079          * NOTE! The local APIC isn't very good at handling
2080          * multiple interrupts at the same interrupt level.
2081          * As the interrupt level is determined by taking the
2082          * vector number and shifting that right by 4, we
2083          * want to spread these out a bit so that they don't
2084          * all fall in the same interrupt level.
2085          *
2086          * Also, we've got to be careful not to trash gate
2087          * 0x80, because int 0x80 is hm, kind of importantish. ;)
2088          */
2089         for (irq = 0; irq < NR_IRQS ; irq++) {
2090                 int tmp = irq;
2091                 if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
2092                         /*
2093                          * Hmm.. We don't have an entry for this,
2094                          * so default to an old-fashioned 8259
2095                          * interrupt if we can..
2096                          */
2097                         if (irq < 16)
2098                                 make_8259A_irq(irq);
2099                         else
2100                                 /* Strange. Oh, well.. */
2101                                 irq_desc[irq].chip = &no_irq_chip;
2102                 }
2103         }
2104 }
2105
2106 /*
2107  * The local APIC irq-chip implementation:
2108  */
2109
2110 static void ack_apic(unsigned int irq)
2111 {
2112         ack_APIC_irq();
2113 }
2114
2115 static void mask_lapic_irq (unsigned int irq)
2116 {
2117         unsigned long v;
2118
2119         v = apic_read(APIC_LVT0);
2120         apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2121 }
2122
2123 static void unmask_lapic_irq (unsigned int irq)
2124 {
2125         unsigned long v;
2126
2127         v = apic_read(APIC_LVT0);
2128         apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
2129 }
2130
2131 static struct irq_chip lapic_chip __read_mostly = {
2132         .name           = "local-APIC-edge",
2133         .mask           = mask_lapic_irq,
2134         .unmask         = unmask_lapic_irq,
2135         .eoi            = ack_apic,
2136 };
2137
2138 static void setup_nmi (void)
2139 {
2140         /*
2141          * Dirty trick to enable the NMI watchdog ...
2142          * We put the 8259A master into AEOI mode and
2143          * unmask on all local APICs LVT0 as NMI.
2144          *
2145          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2146          * is from Maciej W. Rozycki - so we do not have to EOI from
2147          * the NMI handler or the timer interrupt.
2148          */ 
2149         apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2150
2151         on_each_cpu(enable_NMI_through_LVT0, NULL, 1, 1);
2152
2153         apic_printk(APIC_VERBOSE, " done.\n");
2154 }
2155
2156 /*
2157  * This looks a bit hackish but it's about the only one way of sending
2158  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
2159  * not support the ExtINT mode, unfortunately.  We need to send these
2160  * cycles as some i82489DX-based boards have glue logic that keeps the
2161  * 8259A interrupt line asserted until INTA.  --macro
2162  */
2163 static inline void unlock_ExtINT_logic(void)
2164 {
2165         int apic, pin, i;
2166         struct IO_APIC_route_entry entry0, entry1;
2167         unsigned char save_control, save_freq_select;
2168
2169         pin  = find_isa_irq_pin(8, mp_INT);
2170         apic = find_isa_irq_apic(8, mp_INT);
2171         if (pin == -1)
2172                 return;
2173
2174         entry0 = ioapic_read_entry(apic, pin);
2175         clear_IO_APIC_pin(apic, pin);
2176
2177         memset(&entry1, 0, sizeof(entry1));
2178
2179         entry1.dest_mode = 0;                   /* physical delivery */
2180         entry1.mask = 0;                        /* unmask IRQ now */
2181         entry1.dest.physical.physical_dest = hard_smp_processor_id();
2182         entry1.delivery_mode = dest_ExtINT;
2183         entry1.polarity = entry0.polarity;
2184         entry1.trigger = 0;
2185         entry1.vector = 0;
2186
2187         ioapic_write_entry(apic, pin, entry1);
2188
2189         save_control = CMOS_READ(RTC_CONTROL);
2190         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2191         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2192                    RTC_FREQ_SELECT);
2193         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2194
2195         i = 100;
2196         while (i-- > 0) {
2197                 mdelay(10);
2198                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2199                         i -= 10;
2200         }
2201
2202         CMOS_WRITE(save_control, RTC_CONTROL);
2203         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2204         clear_IO_APIC_pin(apic, pin);
2205
2206         ioapic_write_entry(apic, pin, entry0);
2207 }
2208
2209 int timer_uses_ioapic_pin_0;
2210
2211 /*
2212  * This code may look a bit paranoid, but it's supposed to cooperate with
2213  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
2214  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
2215  * fanatically on his truly buggy board.
2216  */
2217 static inline void check_timer(void)
2218 {
2219         int apic1, pin1, apic2, pin2;
2220         int vector;
2221
2222         /*
2223          * get/set the timer IRQ vector:
2224          */
2225         disable_8259A_irq(0);
2226         vector = assign_irq_vector(0);
2227         set_intr_gate(vector, interrupt[0]);
2228
2229         /*
2230          * Subtle, code in do_timer_interrupt() expects an AEOI
2231          * mode for the 8259A whenever interrupts are routed
2232          * through I/O APICs.  Also IRQ0 has to be enabled in
2233          * the 8259A which implies the virtual wire has to be
2234          * disabled in the local APIC.
2235          */
2236         apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2237         init_8259A(1);
2238         timer_ack = 1;
2239         if (timer_over_8254 > 0)
2240                 enable_8259A_irq(0);
2241
2242         pin1  = find_isa_irq_pin(0, mp_INT);
2243         apic1 = find_isa_irq_apic(0, mp_INT);
2244         pin2  = ioapic_i8259.pin;
2245         apic2 = ioapic_i8259.apic;
2246
2247         if (pin1 == 0)
2248                 timer_uses_ioapic_pin_0 = 1;
2249
2250         printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2251                 vector, apic1, pin1, apic2, pin2);
2252
2253         if (pin1 != -1) {
2254                 /*
2255                  * Ok, does IRQ0 through the IOAPIC work?
2256                  */
2257                 unmask_IO_APIC_irq(0);
2258                 if (timer_irq_works()) {
2259                         if (nmi_watchdog == NMI_IO_APIC) {
2260                                 disable_8259A_irq(0);
2261                                 setup_nmi();
2262                                 enable_8259A_irq(0);
2263                         }
2264                         if (disable_timer_pin_1 > 0)
2265                                 clear_IO_APIC_pin(0, pin1);
2266                         return;
2267                 }
2268                 clear_IO_APIC_pin(apic1, pin1);
2269                 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to "
2270                                 "IO-APIC\n");
2271         }
2272
2273         printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
2274         if (pin2 != -1) {
2275                 printk("\n..... (found pin %d) ...", pin2);
2276                 /*
2277                  * legacy devices should be connected to IO APIC #0
2278                  */
2279                 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
2280                 if (timer_irq_works()) {
2281                         printk("works.\n");
2282                         if (pin1 != -1)
2283                                 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2284                         else
2285                                 add_pin_to_irq(0, apic2, pin2);
2286                         if (nmi_watchdog == NMI_IO_APIC) {
2287                                 setup_nmi();
2288                         }
2289                         return;
2290                 }
2291                 /*
2292                  * Cleanup, just in case ...
2293                  */
2294                 clear_IO_APIC_pin(apic2, pin2);
2295         }
2296         printk(" failed.\n");
2297
2298         if (nmi_watchdog == NMI_IO_APIC) {
2299                 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2300                 nmi_watchdog = 0;
2301         }
2302
2303         printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2304
2305         disable_8259A_irq(0);
2306         set_irq_chip_and_handler_name(0, &lapic_chip, handle_fasteoi_irq,
2307                                       "fasteio");
2308         apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector);   /* Fixed mode */
2309         enable_8259A_irq(0);
2310
2311         if (timer_irq_works()) {
2312                 printk(" works.\n");
2313                 return;
2314         }
2315         apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2316         printk(" failed.\n");
2317
2318         printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2319
2320         timer_ack = 0;
2321         init_8259A(0);
2322         make_8259A_irq(0);
2323         apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2324
2325         unlock_ExtINT_logic();
2326
2327         if (timer_irq_works()) {
2328                 printk(" works.\n");
2329                 return;
2330         }
2331         printk(" failed :(.\n");
2332         panic("IO-APIC + timer doesn't work!  Boot with apic=debug and send a "
2333                 "report.  Then try booting with the 'noapic' option");
2334 }
2335
2336 /*
2337  *
2338  * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2339  * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2340  *   Linux doesn't really care, as it's not actually used
2341  *   for any interrupt handling anyway.
2342  */
2343 #define PIC_IRQS        (1 << PIC_CASCADE_IR)
2344
2345 void __init setup_IO_APIC(void)
2346 {
2347         enable_IO_APIC();
2348
2349         if (acpi_ioapic)
2350                 io_apic_irqs = ~0;      /* all IRQs go through IOAPIC */
2351         else
2352                 io_apic_irqs = ~PIC_IRQS;
2353
2354         printk("ENABLING IO-APIC IRQs\n");
2355
2356         /*
2357          * Set up IO-APIC IRQ routing.
2358          */
2359         if (!acpi_ioapic)
2360                 setup_ioapic_ids_from_mpc();
2361         sync_Arb_IDs();
2362         setup_IO_APIC_irqs();
2363         init_IO_APIC_traps();
2364         check_timer();
2365         if (!acpi_ioapic)
2366                 print_IO_APIC();
2367 }
2368
2369 static int __init setup_disable_8254_timer(char *s)
2370 {
2371         timer_over_8254 = -1;
2372         return 1;
2373 }
2374 static int __init setup_enable_8254_timer(char *s)
2375 {
2376         timer_over_8254 = 2;
2377         return 1;
2378 }
2379
2380 __setup("disable_8254_timer", setup_disable_8254_timer);
2381 __setup("enable_8254_timer", setup_enable_8254_timer);
2382
2383 /*
2384  *      Called after all the initialization is done. If we didnt find any
2385  *      APIC bugs then we can allow the modify fast path
2386  */
2387  
2388 static int __init io_apic_bug_finalize(void)
2389 {
2390         if(sis_apic_bug == -1)
2391                 sis_apic_bug = 0;
2392         return 0;
2393 }
2394
2395 late_initcall(io_apic_bug_finalize);
2396
2397 struct sysfs_ioapic_data {
2398         struct sys_device dev;
2399         struct IO_APIC_route_entry entry[0];
2400 };
2401 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2402
2403 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2404 {
2405         struct IO_APIC_route_entry *entry;
2406         struct sysfs_ioapic_data *data;
2407         int i;
2408         
2409         data = container_of(dev, struct sysfs_ioapic_data, dev);
2410         entry = data->entry;
2411         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2412                 entry[i] = ioapic_read_entry(dev->id, i);
2413
2414         return 0;
2415 }
2416
2417 static int ioapic_resume(struct sys_device *dev)
2418 {
2419         struct IO_APIC_route_entry *entry;
2420         struct sysfs_ioapic_data *data;
2421         unsigned long flags;
2422         union IO_APIC_reg_00 reg_00;
2423         int i;
2424         
2425         data = container_of(dev, struct sysfs_ioapic_data, dev);
2426         entry = data->entry;
2427
2428         spin_lock_irqsave(&ioapic_lock, flags);
2429         reg_00.raw = io_apic_read(dev->id, 0);
2430         if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2431                 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2432                 io_apic_write(dev->id, 0, reg_00.raw);
2433         }
2434         spin_unlock_irqrestore(&ioapic_lock, flags);
2435         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2436                 ioapic_write_entry(dev->id, i, entry[i]);
2437
2438         return 0;
2439 }
2440
2441 static struct sysdev_class ioapic_sysdev_class = {
2442         set_kset_name("ioapic"),
2443         .suspend = ioapic_suspend,
2444         .resume = ioapic_resume,
2445 };
2446
2447 static int __init ioapic_init_sysfs(void)
2448 {
2449         struct sys_device * dev;
2450         int i, size, error = 0;
2451
2452         error = sysdev_class_register(&ioapic_sysdev_class);
2453         if (error)
2454                 return error;
2455
2456         for (i = 0; i < nr_ioapics; i++ ) {
2457                 size = sizeof(struct sys_device) + nr_ioapic_registers[i] 
2458                         * sizeof(struct IO_APIC_route_entry);
2459                 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2460                 if (!mp_ioapic_data[i]) {
2461                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2462                         continue;
2463                 }
2464                 memset(mp_ioapic_data[i], 0, size);
2465                 dev = &mp_ioapic_data[i]->dev;
2466                 dev->id = i; 
2467                 dev->cls = &ioapic_sysdev_class;
2468                 error = sysdev_register(dev);
2469                 if (error) {
2470                         kfree(mp_ioapic_data[i]);
2471                         mp_ioapic_data[i] = NULL;
2472                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2473                         continue;
2474                 }
2475         }
2476
2477         return 0;
2478 }
2479
2480 device_initcall(ioapic_init_sysfs);
2481
2482 /*
2483  * Dynamic irq allocate and deallocation
2484  */
2485 int create_irq(void)
2486 {
2487         /* Allocate an unused irq */
2488         int irq, new, vector;
2489         unsigned long flags;
2490
2491         irq = -ENOSPC;
2492         spin_lock_irqsave(&vector_lock, flags);
2493         for (new = (NR_IRQS - 1); new >= 0; new--) {
2494                 if (platform_legacy_irq(new))
2495                         continue;
2496                 if (irq_vector[new] != 0)
2497                         continue;
2498                 vector = __assign_irq_vector(new);
2499                 if (likely(vector > 0))
2500                         irq = new;
2501                 break;
2502         }
2503         spin_unlock_irqrestore(&vector_lock, flags);
2504
2505         if (irq >= 0) {
2506                 set_intr_gate(vector, interrupt[irq]);
2507                 dynamic_irq_init(irq);
2508         }
2509         return irq;
2510 }
2511
2512 void destroy_irq(unsigned int irq)
2513 {
2514         unsigned long flags;
2515
2516         dynamic_irq_cleanup(irq);
2517
2518         spin_lock_irqsave(&vector_lock, flags);
2519         irq_vector[irq] = 0;
2520         spin_unlock_irqrestore(&vector_lock, flags);
2521 }
2522
2523 /*
2524  * MSI mesage composition
2525  */
2526 #ifdef CONFIG_PCI_MSI
2527 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
2528 {
2529         int vector;
2530         unsigned dest;
2531
2532         vector = assign_irq_vector(irq);
2533         if (vector >= 0) {
2534                 dest = cpu_mask_to_apicid(TARGET_CPUS);
2535
2536                 msg->address_hi = MSI_ADDR_BASE_HI;
2537                 msg->address_lo =
2538                         MSI_ADDR_BASE_LO |
2539                         ((INT_DEST_MODE == 0) ?
2540                                 MSI_ADDR_DEST_MODE_PHYSICAL:
2541                                 MSI_ADDR_DEST_MODE_LOGICAL) |
2542                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2543                                 MSI_ADDR_REDIRECTION_CPU:
2544                                 MSI_ADDR_REDIRECTION_LOWPRI) |
2545                         MSI_ADDR_DEST_ID(dest);
2546
2547                 msg->data =
2548                         MSI_DATA_TRIGGER_EDGE |
2549                         MSI_DATA_LEVEL_ASSERT |
2550                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2551                                 MSI_DATA_DELIVERY_FIXED:
2552                                 MSI_DATA_DELIVERY_LOWPRI) |
2553                         MSI_DATA_VECTOR(vector);
2554         }
2555         return vector;
2556 }
2557
2558 #ifdef CONFIG_SMP
2559 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2560 {
2561         struct msi_msg msg;
2562         unsigned int dest;
2563         cpumask_t tmp;
2564         int vector;
2565
2566         cpus_and(tmp, mask, cpu_online_map);
2567         if (cpus_empty(tmp))
2568                 tmp = TARGET_CPUS;
2569
2570         vector = assign_irq_vector(irq);
2571         if (vector < 0)
2572                 return;
2573
2574         dest = cpu_mask_to_apicid(mask);
2575
2576         read_msi_msg(irq, &msg);
2577
2578         msg.data &= ~MSI_DATA_VECTOR_MASK;
2579         msg.data |= MSI_DATA_VECTOR(vector);
2580         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2581         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2582
2583         write_msi_msg(irq, &msg);
2584         set_native_irq_info(irq, mask);
2585 }
2586 #endif /* CONFIG_SMP */
2587
2588 /*
2589  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2590  * which implement the MSI or MSI-X Capability Structure.
2591  */
2592 static struct irq_chip msi_chip = {
2593         .name           = "PCI-MSI",
2594         .unmask         = unmask_msi_irq,
2595         .mask           = mask_msi_irq,
2596         .ack            = ack_ioapic_irq,
2597 #ifdef CONFIG_SMP
2598         .set_affinity   = set_msi_irq_affinity,
2599 #endif
2600         .retrigger      = ioapic_retrigger_irq,
2601 };
2602
2603 int arch_setup_msi_irq(unsigned int irq, struct pci_dev *dev)
2604 {
2605         struct msi_msg msg;
2606         int ret;
2607         ret = msi_compose_msg(dev, irq, &msg);
2608         if (ret < 0)
2609                 return ret;
2610
2611         write_msi_msg(irq, &msg);
2612
2613         set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2614                                       "edge");
2615
2616         return 0;
2617 }
2618
2619 void arch_teardown_msi_irq(unsigned int irq)
2620 {
2621         return;
2622 }
2623
2624 #endif /* CONFIG_PCI_MSI */
2625
2626 /*
2627  * Hypertransport interrupt support
2628  */
2629 #ifdef CONFIG_HT_IRQ
2630
2631 #ifdef CONFIG_SMP
2632
2633 static void target_ht_irq(unsigned int irq, unsigned int dest)
2634 {
2635         struct ht_irq_msg msg;
2636         fetch_ht_irq_msg(irq, &msg);
2637
2638         msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2639         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2640
2641         msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2642         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2643
2644         write_ht_irq_msg(irq, &msg);
2645 }
2646
2647 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2648 {
2649         unsigned int dest;
2650         cpumask_t tmp;
2651
2652         cpus_and(tmp, mask, cpu_online_map);
2653         if (cpus_empty(tmp))
2654                 tmp = TARGET_CPUS;
2655
2656         cpus_and(mask, tmp, CPU_MASK_ALL);
2657
2658         dest = cpu_mask_to_apicid(mask);
2659
2660         target_ht_irq(irq, dest);
2661         set_native_irq_info(irq, mask);
2662 }
2663 #endif
2664
2665 static struct irq_chip ht_irq_chip = {
2666         .name           = "PCI-HT",
2667         .mask           = mask_ht_irq,
2668         .unmask         = unmask_ht_irq,
2669         .ack            = ack_ioapic_irq,
2670 #ifdef CONFIG_SMP
2671         .set_affinity   = set_ht_irq_affinity,
2672 #endif
2673         .retrigger      = ioapic_retrigger_irq,
2674 };
2675
2676 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2677 {
2678         int vector;
2679
2680         vector = assign_irq_vector(irq);
2681         if (vector >= 0) {
2682                 struct ht_irq_msg msg;
2683                 unsigned dest;
2684                 cpumask_t tmp;
2685
2686                 cpus_clear(tmp);
2687                 cpu_set(vector >> 8, tmp);
2688                 dest = cpu_mask_to_apicid(tmp);
2689
2690                 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2691
2692                 msg.address_lo =
2693                         HT_IRQ_LOW_BASE |
2694                         HT_IRQ_LOW_DEST_ID(dest) |
2695                         HT_IRQ_LOW_VECTOR(vector) |
2696                         ((INT_DEST_MODE == 0) ?
2697                                 HT_IRQ_LOW_DM_PHYSICAL :
2698                                 HT_IRQ_LOW_DM_LOGICAL) |
2699                         HT_IRQ_LOW_RQEOI_EDGE |
2700                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2701                                 HT_IRQ_LOW_MT_FIXED :
2702                                 HT_IRQ_LOW_MT_ARBITRATED) |
2703                         HT_IRQ_LOW_IRQ_MASKED;
2704
2705                 write_ht_irq_msg(irq, &msg);
2706
2707                 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2708                                               handle_edge_irq, "edge");
2709         }
2710         return vector;
2711 }
2712 #endif /* CONFIG_HT_IRQ */
2713
2714 /* --------------------------------------------------------------------------
2715                           ACPI-based IOAPIC Configuration
2716    -------------------------------------------------------------------------- */
2717
2718 #ifdef CONFIG_ACPI
2719
2720 int __init io_apic_get_unique_id (int ioapic, int apic_id)
2721 {
2722         union IO_APIC_reg_00 reg_00;
2723         static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2724         physid_mask_t tmp;
2725         unsigned long flags;
2726         int i = 0;
2727
2728         /*
2729          * The P4 platform supports up to 256 APIC IDs on two separate APIC 
2730          * buses (one for LAPICs, one for IOAPICs), where predecessors only 
2731          * supports up to 16 on one shared APIC bus.
2732          * 
2733          * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2734          *      advantage of new APIC bus architecture.
2735          */
2736
2737         if (physids_empty(apic_id_map))
2738                 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2739
2740         spin_lock_irqsave(&ioapic_lock, flags);
2741         reg_00.raw = io_apic_read(ioapic, 0);
2742         spin_unlock_irqrestore(&ioapic_lock, flags);
2743
2744         if (apic_id >= get_physical_broadcast()) {
2745                 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2746                         "%d\n", ioapic, apic_id, reg_00.bits.ID);
2747                 apic_id = reg_00.bits.ID;
2748         }
2749
2750         /*
2751          * Every APIC in a system must have a unique ID or we get lots of nice 
2752          * 'stuck on smp_invalidate_needed IPI wait' messages.
2753          */
2754         if (check_apicid_used(apic_id_map, apic_id)) {
2755
2756                 for (i = 0; i < get_physical_broadcast(); i++) {
2757                         if (!check_apicid_used(apic_id_map, i))
2758                                 break;
2759                 }
2760
2761                 if (i == get_physical_broadcast())
2762                         panic("Max apic_id exceeded!\n");
2763
2764                 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2765                         "trying %d\n", ioapic, apic_id, i);
2766
2767                 apic_id = i;
2768         } 
2769
2770         tmp = apicid_to_cpu_present(apic_id);
2771         physids_or(apic_id_map, apic_id_map, tmp);
2772
2773         if (reg_00.bits.ID != apic_id) {
2774                 reg_00.bits.ID = apic_id;
2775
2776                 spin_lock_irqsave(&ioapic_lock, flags);
2777                 io_apic_write(ioapic, 0, reg_00.raw);
2778                 reg_00.raw = io_apic_read(ioapic, 0);
2779                 spin_unlock_irqrestore(&ioapic_lock, flags);
2780
2781                 /* Sanity check */
2782                 if (reg_00.bits.ID != apic_id) {
2783                         printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2784                         return -1;
2785                 }
2786         }
2787
2788         apic_printk(APIC_VERBOSE, KERN_INFO
2789                         "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2790
2791         return apic_id;
2792 }
2793
2794
2795 int __init io_apic_get_version (int ioapic)
2796 {
2797         union IO_APIC_reg_01    reg_01;
2798         unsigned long flags;
2799
2800         spin_lock_irqsave(&ioapic_lock, flags);
2801         reg_01.raw = io_apic_read(ioapic, 1);
2802         spin_unlock_irqrestore(&ioapic_lock, flags);
2803
2804         return reg_01.bits.version;
2805 }
2806
2807
2808 int __init io_apic_get_redir_entries (int ioapic)
2809 {
2810         union IO_APIC_reg_01    reg_01;
2811         unsigned long flags;
2812
2813         spin_lock_irqsave(&ioapic_lock, flags);
2814         reg_01.raw = io_apic_read(ioapic, 1);
2815         spin_unlock_irqrestore(&ioapic_lock, flags);
2816
2817         return reg_01.bits.entries;
2818 }
2819
2820
2821 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2822 {
2823         struct IO_APIC_route_entry entry;
2824         unsigned long flags;
2825
2826         if (!IO_APIC_IRQ(irq)) {
2827                 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2828                         ioapic);
2829                 return -EINVAL;
2830         }
2831
2832         /*
2833          * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2834          * Note that we mask (disable) IRQs now -- these get enabled when the
2835          * corresponding device driver registers for this IRQ.
2836          */
2837
2838         memset(&entry,0,sizeof(entry));
2839
2840         entry.delivery_mode = INT_DELIVERY_MODE;
2841         entry.dest_mode = INT_DEST_MODE;
2842         entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2843         entry.trigger = edge_level;
2844         entry.polarity = active_high_low;
2845         entry.mask  = 1;
2846
2847         /*
2848          * IRQs < 16 are already in the irq_2_pin[] map
2849          */
2850         if (irq >= 16)
2851                 add_pin_to_irq(irq, ioapic, pin);
2852
2853         entry.vector = assign_irq_vector(irq);
2854
2855         apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2856                 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2857                 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2858                 edge_level, active_high_low);
2859
2860         ioapic_register_intr(irq, entry.vector, edge_level);
2861
2862         if (!ioapic && (irq < 16))
2863                 disable_8259A_irq(irq);
2864
2865         spin_lock_irqsave(&ioapic_lock, flags);
2866         __ioapic_write_entry(ioapic, pin, entry);
2867         set_native_irq_info(irq, TARGET_CPUS);
2868         spin_unlock_irqrestore(&ioapic_lock, flags);
2869
2870         return 0;
2871 }
2872
2873 #endif /* CONFIG_ACPI */
2874
2875 static int __init parse_disable_timer_pin_1(char *arg)
2876 {
2877         disable_timer_pin_1 = 1;
2878         return 0;
2879 }
2880 early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2881
2882 static int __init parse_enable_timer_pin_1(char *arg)
2883 {
2884         disable_timer_pin_1 = -1;
2885         return 0;
2886 }
2887 early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2888
2889 static int __init parse_noapic(char *arg)
2890 {
2891         /* disable IO-APIC */
2892         disable_ioapic_setup();
2893         return 0;
2894 }
2895 early_param("noapic", parse_noapic);