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