1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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/pci.h>
29#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
32#include <linux/module.h>
33#include <linux/sysdev.h>
34#include <linux/msi.h>
35#include <linux/htirq.h>
36#include <linux/freezer.h>
37#include <linux/kthread.h>
38#include <linux/jiffies.h>
39#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
44#include <linux/hpet.h>
45
46#include <asm/idle.h>
47#include <asm/io.h>
48#include <asm/smp.h>
49#include <asm/cpu.h>
50#include <asm/desc.h>
51#include <asm/proto.h>
52#include <asm/acpi.h>
53#include <asm/dma.h>
54#include <asm/timer.h>
55#include <asm/i8259.h>
56#include <asm/nmi.h>
57#include <asm/msidef.h>
58#include <asm/hypertransport.h>
59#include <asm/setup.h>
60#include <asm/irq_remapping.h>
61#include <asm/hpet.h>
62#include <asm/hw_irq.h>
63#include <asm/uv/uv_hub.h>
64#include <asm/uv/uv_irq.h>
65
66#include <asm/apic.h>
67
68#define __apicdebuginit(type) static type __init
69#define for_each_irq_pin(entry, head) \
70 for (entry = head; entry; entry = entry->next)
71
72
73
74
75
76int sis_apic_bug = -1;
77
78static DEFINE_SPINLOCK(ioapic_lock);
79static DEFINE_SPINLOCK(vector_lock);
80
81
82
83
84int nr_ioapic_registers[MAX_IO_APICS];
85
86
87struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
88int nr_ioapics;
89
90
91struct mp_ioapic_gsi mp_gsi_routing[MAX_IO_APICS];
92
93
94struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
95
96
97int mp_irq_entries;
98
99
100static int nr_legacy_irqs __read_mostly = NR_IRQS_LEGACY;
101
102static int nr_irqs_gsi = NR_IRQS_LEGACY;
103
104#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
105int mp_bus_id_to_type[MAX_MP_BUSSES];
106#endif
107
108DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
109
110int skip_ioapic_setup;
111
112void arch_disable_smp_support(void)
113{
114#ifdef CONFIG_PCI
115 noioapicquirk = 1;
116 noioapicreroute = -1;
117#endif
118 skip_ioapic_setup = 1;
119}
120
121static int __init parse_noapic(char *str)
122{
123
124 arch_disable_smp_support();
125 return 0;
126}
127early_param("noapic", parse_noapic);
128
129struct irq_pin_list {
130 int apic, pin;
131 struct irq_pin_list *next;
132};
133
134static struct irq_pin_list *get_one_free_irq_2_pin(int node)
135{
136 struct irq_pin_list *pin;
137
138 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
139
140 return pin;
141}
142
143
144
145
146
147
148struct irq_cfg {
149 struct irq_pin_list *irq_2_pin;
150 cpumask_var_t domain;
151 cpumask_var_t old_domain;
152 unsigned move_cleanup_count;
153 u8 vector;
154 u8 move_in_progress : 1;
155};
156
157
158#ifdef CONFIG_SPARSE_IRQ
159static struct irq_cfg irq_cfgx[] = {
160#else
161static struct irq_cfg irq_cfgx[NR_IRQS] = {
162#endif
163 [0] = { .vector = IRQ0_VECTOR, },
164 [1] = { .vector = IRQ1_VECTOR, },
165 [2] = { .vector = IRQ2_VECTOR, },
166 [3] = { .vector = IRQ3_VECTOR, },
167 [4] = { .vector = IRQ4_VECTOR, },
168 [5] = { .vector = IRQ5_VECTOR, },
169 [6] = { .vector = IRQ6_VECTOR, },
170 [7] = { .vector = IRQ7_VECTOR, },
171 [8] = { .vector = IRQ8_VECTOR, },
172 [9] = { .vector = IRQ9_VECTOR, },
173 [10] = { .vector = IRQ10_VECTOR, },
174 [11] = { .vector = IRQ11_VECTOR, },
175 [12] = { .vector = IRQ12_VECTOR, },
176 [13] = { .vector = IRQ13_VECTOR, },
177 [14] = { .vector = IRQ14_VECTOR, },
178 [15] = { .vector = IRQ15_VECTOR, },
179};
180
181void __init io_apic_disable_legacy(void)
182{
183 nr_legacy_irqs = 0;
184 nr_irqs_gsi = 0;
185}
186
187int __init arch_early_irq_init(void)
188{
189 struct irq_cfg *cfg;
190 struct irq_desc *desc;
191 int count;
192 int node;
193 int i;
194
195 cfg = irq_cfgx;
196 count = ARRAY_SIZE(irq_cfgx);
197 node= cpu_to_node(boot_cpu_id);
198
199 for (i = 0; i < count; i++) {
200 desc = irq_to_desc(i);
201 desc->chip_data = &cfg[i];
202 zalloc_cpumask_var_node(&cfg[i].domain, GFP_NOWAIT, node);
203 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_NOWAIT, node);
204 if (i < nr_legacy_irqs)
205 cpumask_setall(cfg[i].domain);
206 }
207
208 return 0;
209}
210
211#ifdef CONFIG_SPARSE_IRQ
212static struct irq_cfg *irq_cfg(unsigned int irq)
213{
214 struct irq_cfg *cfg = NULL;
215 struct irq_desc *desc;
216
217 desc = irq_to_desc(irq);
218 if (desc)
219 cfg = desc->chip_data;
220
221 return cfg;
222}
223
224static struct irq_cfg *get_one_free_irq_cfg(int node)
225{
226 struct irq_cfg *cfg;
227
228 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
229 if (cfg) {
230 if (!zalloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
231 kfree(cfg);
232 cfg = NULL;
233 } else if (!zalloc_cpumask_var_node(&cfg->old_domain,
234 GFP_ATOMIC, node)) {
235 free_cpumask_var(cfg->domain);
236 kfree(cfg);
237 cfg = NULL;
238 }
239 }
240
241 return cfg;
242}
243
244int arch_init_chip_data(struct irq_desc *desc, int node)
245{
246 struct irq_cfg *cfg;
247
248 cfg = desc->chip_data;
249 if (!cfg) {
250 desc->chip_data = get_one_free_irq_cfg(node);
251 if (!desc->chip_data) {
252 printk(KERN_ERR "can not alloc irq_cfg\n");
253 BUG_ON(1);
254 }
255 }
256
257 return 0;
258}
259
260
261static void
262init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int node)
263{
264 struct irq_pin_list *old_entry, *head, *tail, *entry;
265
266 cfg->irq_2_pin = NULL;
267 old_entry = old_cfg->irq_2_pin;
268 if (!old_entry)
269 return;
270
271 entry = get_one_free_irq_2_pin(node);
272 if (!entry)
273 return;
274
275 entry->apic = old_entry->apic;
276 entry->pin = old_entry->pin;
277 head = entry;
278 tail = entry;
279 old_entry = old_entry->next;
280 while (old_entry) {
281 entry = get_one_free_irq_2_pin(node);
282 if (!entry) {
283 entry = head;
284 while (entry) {
285 head = entry->next;
286 kfree(entry);
287 entry = head;
288 }
289
290 return;
291 }
292 entry->apic = old_entry->apic;
293 entry->pin = old_entry->pin;
294 tail->next = entry;
295 tail = entry;
296 old_entry = old_entry->next;
297 }
298
299 tail->next = NULL;
300 cfg->irq_2_pin = head;
301}
302
303static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
304{
305 struct irq_pin_list *entry, *next;
306
307 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
308 return;
309
310 entry = old_cfg->irq_2_pin;
311
312 while (entry) {
313 next = entry->next;
314 kfree(entry);
315 entry = next;
316 }
317 old_cfg->irq_2_pin = NULL;
318}
319
320void arch_init_copy_chip_data(struct irq_desc *old_desc,
321 struct irq_desc *desc, int node)
322{
323 struct irq_cfg *cfg;
324 struct irq_cfg *old_cfg;
325
326 cfg = get_one_free_irq_cfg(node);
327
328 if (!cfg)
329 return;
330
331 desc->chip_data = cfg;
332
333 old_cfg = old_desc->chip_data;
334
335 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
336
337 init_copy_irq_2_pin(old_cfg, cfg, node);
338}
339
340static void free_irq_cfg(struct irq_cfg *old_cfg)
341{
342 kfree(old_cfg);
343}
344
345void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
346{
347 struct irq_cfg *old_cfg, *cfg;
348
349 old_cfg = old_desc->chip_data;
350 cfg = desc->chip_data;
351
352 if (old_cfg == cfg)
353 return;
354
355 if (old_cfg) {
356 free_irq_2_pin(old_cfg, cfg);
357 free_irq_cfg(old_cfg);
358 old_desc->chip_data = NULL;
359 }
360}
361
362
363#else
364static struct irq_cfg *irq_cfg(unsigned int irq)
365{
366 return irq < nr_irqs ? irq_cfgx + irq : NULL;
367}
368
369#endif
370
371struct io_apic {
372 unsigned int index;
373 unsigned int unused[3];
374 unsigned int data;
375 unsigned int unused2[11];
376 unsigned int eoi;
377};
378
379static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
380{
381 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
382 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
383}
384
385static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
386{
387 struct io_apic __iomem *io_apic = io_apic_base(apic);
388 writel(vector, &io_apic->eoi);
389}
390
391static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
392{
393 struct io_apic __iomem *io_apic = io_apic_base(apic);
394 writel(reg, &io_apic->index);
395 return readl(&io_apic->data);
396}
397
398static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
399{
400 struct io_apic __iomem *io_apic = io_apic_base(apic);
401 writel(reg, &io_apic->index);
402 writel(value, &io_apic->data);
403}
404
405
406
407
408
409
410
411static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
412{
413 struct io_apic __iomem *io_apic = io_apic_base(apic);
414
415 if (sis_apic_bug)
416 writel(reg, &io_apic->index);
417 writel(value, &io_apic->data);
418}
419
420static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
421{
422 struct irq_pin_list *entry;
423 unsigned long flags;
424
425 spin_lock_irqsave(&ioapic_lock, flags);
426 for_each_irq_pin(entry, cfg->irq_2_pin) {
427 unsigned int reg;
428 int pin;
429
430 pin = entry->pin;
431 reg = io_apic_read(entry->apic, 0x10 + pin*2);
432
433 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
434 spin_unlock_irqrestore(&ioapic_lock, flags);
435 return true;
436 }
437 }
438 spin_unlock_irqrestore(&ioapic_lock, flags);
439
440 return false;
441}
442
443union entry_union {
444 struct { u32 w1, w2; };
445 struct IO_APIC_route_entry entry;
446};
447
448static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
449{
450 union entry_union eu;
451 unsigned long flags;
452 spin_lock_irqsave(&ioapic_lock, flags);
453 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
454 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
455 spin_unlock_irqrestore(&ioapic_lock, flags);
456 return eu.entry;
457}
458
459
460
461
462
463
464
465static void
466__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
467{
468 union entry_union eu = {{0, 0}};
469
470 eu.entry = e;
471 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
472 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
473}
474
475void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
476{
477 unsigned long flags;
478 spin_lock_irqsave(&ioapic_lock, flags);
479 __ioapic_write_entry(apic, pin, e);
480 spin_unlock_irqrestore(&ioapic_lock, flags);
481}
482
483
484
485
486
487
488static void ioapic_mask_entry(int apic, int pin)
489{
490 unsigned long flags;
491 union entry_union eu = { .entry.mask = 1 };
492
493 spin_lock_irqsave(&ioapic_lock, flags);
494 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
495 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
496 spin_unlock_irqrestore(&ioapic_lock, flags);
497}
498
499
500
501
502
503
504static int
505add_pin_to_irq_node_nopanic(struct irq_cfg *cfg, int node, int apic, int pin)
506{
507 struct irq_pin_list **last, *entry;
508
509
510 last = &cfg->irq_2_pin;
511 for_each_irq_pin(entry, cfg->irq_2_pin) {
512 if (entry->apic == apic && entry->pin == pin)
513 return 0;
514 last = &entry->next;
515 }
516
517 entry = get_one_free_irq_2_pin(node);
518 if (!entry) {
519 printk(KERN_ERR "can not alloc irq_pin_list (%d,%d,%d)\n",
520 node, apic, pin);
521 return -ENOMEM;
522 }
523 entry->apic = apic;
524 entry->pin = pin;
525
526 *last = entry;
527 return 0;
528}
529
530static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
531{
532 if (add_pin_to_irq_node_nopanic(cfg, node, apic, pin))
533 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
534}
535
536
537
538
539static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
540 int oldapic, int oldpin,
541 int newapic, int newpin)
542{
543 struct irq_pin_list *entry;
544
545 for_each_irq_pin(entry, cfg->irq_2_pin) {
546 if (entry->apic == oldapic && entry->pin == oldpin) {
547 entry->apic = newapic;
548 entry->pin = newpin;
549
550 return;
551 }
552 }
553
554
555 add_pin_to_irq_node(cfg, node, newapic, newpin);
556}
557
558static void io_apic_modify_irq(struct irq_cfg *cfg,
559 int mask_and, int mask_or,
560 void (*final)(struct irq_pin_list *entry))
561{
562 int pin;
563 struct irq_pin_list *entry;
564
565 for_each_irq_pin(entry, cfg->irq_2_pin) {
566 unsigned int reg;
567 pin = entry->pin;
568 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
569 reg &= mask_and;
570 reg |= mask_or;
571 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
572 if (final)
573 final(entry);
574 }
575}
576
577static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
578{
579 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
580}
581
582static void io_apic_sync(struct irq_pin_list *entry)
583{
584
585
586
587
588 struct io_apic __iomem *io_apic;
589 io_apic = io_apic_base(entry->apic);
590 readl(&io_apic->data);
591}
592
593static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
594{
595 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
596}
597
598static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
599{
600 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
601 IO_APIC_REDIR_MASKED, NULL);
602}
603
604static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
605{
606 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
607 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
608}
609
610static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
611{
612 struct irq_cfg *cfg = desc->chip_data;
613 unsigned long flags;
614
615 BUG_ON(!cfg);
616
617 spin_lock_irqsave(&ioapic_lock, flags);
618 __mask_IO_APIC_irq(cfg);
619 spin_unlock_irqrestore(&ioapic_lock, flags);
620}
621
622static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
623{
624 struct irq_cfg *cfg = desc->chip_data;
625 unsigned long flags;
626
627 spin_lock_irqsave(&ioapic_lock, flags);
628 __unmask_IO_APIC_irq(cfg);
629 spin_unlock_irqrestore(&ioapic_lock, flags);
630}
631
632static void mask_IO_APIC_irq(unsigned int irq)
633{
634 struct irq_desc *desc = irq_to_desc(irq);
635
636 mask_IO_APIC_irq_desc(desc);
637}
638static void unmask_IO_APIC_irq(unsigned int irq)
639{
640 struct irq_desc *desc = irq_to_desc(irq);
641
642 unmask_IO_APIC_irq_desc(desc);
643}
644
645static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
646{
647 struct IO_APIC_route_entry entry;
648
649
650 entry = ioapic_read_entry(apic, pin);
651 if (entry.delivery_mode == dest_SMI)
652 return;
653
654
655
656 ioapic_mask_entry(apic, pin);
657}
658
659static void clear_IO_APIC (void)
660{
661 int apic, pin;
662
663 for (apic = 0; apic < nr_ioapics; apic++)
664 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
665 clear_IO_APIC_pin(apic, pin);
666}
667
668#ifdef CONFIG_X86_32
669
670
671
672
673
674#define MAX_PIRQS 8
675static int pirq_entries[MAX_PIRQS] = {
676 [0 ... MAX_PIRQS - 1] = -1
677};
678
679static int __init ioapic_pirq_setup(char *str)
680{
681 int i, max;
682 int ints[MAX_PIRQS+1];
683
684 get_options(str, ARRAY_SIZE(ints), ints);
685
686 apic_printk(APIC_VERBOSE, KERN_INFO
687 "PIRQ redirection, working around broken MP-BIOS.\n");
688 max = MAX_PIRQS;
689 if (ints[0] < MAX_PIRQS)
690 max = ints[0];
691
692 for (i = 0; i < max; i++) {
693 apic_printk(APIC_VERBOSE, KERN_DEBUG
694 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
695
696
697
698 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
699 }
700 return 1;
701}
702
703__setup("pirq=", ioapic_pirq_setup);
704#endif
705
706struct IO_APIC_route_entry **alloc_ioapic_entries(void)
707{
708 int apic;
709 struct IO_APIC_route_entry **ioapic_entries;
710
711 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
712 GFP_ATOMIC);
713 if (!ioapic_entries)
714 return 0;
715
716 for (apic = 0; apic < nr_ioapics; apic++) {
717 ioapic_entries[apic] =
718 kzalloc(sizeof(struct IO_APIC_route_entry) *
719 nr_ioapic_registers[apic], GFP_ATOMIC);
720 if (!ioapic_entries[apic])
721 goto nomem;
722 }
723
724 return ioapic_entries;
725
726nomem:
727 while (--apic >= 0)
728 kfree(ioapic_entries[apic]);
729 kfree(ioapic_entries);
730
731 return 0;
732}
733
734
735
736
737int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
738{
739 int apic, pin;
740
741 if (!ioapic_entries)
742 return -ENOMEM;
743
744 for (apic = 0; apic < nr_ioapics; apic++) {
745 if (!ioapic_entries[apic])
746 return -ENOMEM;
747
748 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
749 ioapic_entries[apic][pin] =
750 ioapic_read_entry(apic, pin);
751 }
752
753 return 0;
754}
755
756
757
758
759void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
760{
761 int apic, pin;
762
763 if (!ioapic_entries)
764 return;
765
766 for (apic = 0; apic < nr_ioapics; apic++) {
767 if (!ioapic_entries[apic])
768 break;
769
770 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
771 struct IO_APIC_route_entry entry;
772
773 entry = ioapic_entries[apic][pin];
774 if (!entry.mask) {
775 entry.mask = 1;
776 ioapic_write_entry(apic, pin, entry);
777 }
778 }
779 }
780}
781
782
783
784
785int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
786{
787 int apic, pin;
788
789 if (!ioapic_entries)
790 return -ENOMEM;
791
792 for (apic = 0; apic < nr_ioapics; apic++) {
793 if (!ioapic_entries[apic])
794 return -ENOMEM;
795
796 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
797 ioapic_write_entry(apic, pin,
798 ioapic_entries[apic][pin]);
799 }
800 return 0;
801}
802
803void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
804{
805 int apic;
806
807 for (apic = 0; apic < nr_ioapics; apic++)
808 kfree(ioapic_entries[apic]);
809
810 kfree(ioapic_entries);
811}
812
813
814
815
816static int find_irq_entry(int apic, int pin, int type)
817{
818 int i;
819
820 for (i = 0; i < mp_irq_entries; i++)
821 if (mp_irqs[i].irqtype == type &&
822 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
823 mp_irqs[i].dstapic == MP_APIC_ALL) &&
824 mp_irqs[i].dstirq == pin)
825 return i;
826
827 return -1;
828}
829
830
831
832
833static int __init find_isa_irq_pin(int irq, int type)
834{
835 int i;
836
837 for (i = 0; i < mp_irq_entries; i++) {
838 int lbus = mp_irqs[i].srcbus;
839
840 if (test_bit(lbus, mp_bus_not_pci) &&
841 (mp_irqs[i].irqtype == type) &&
842 (mp_irqs[i].srcbusirq == irq))
843
844 return mp_irqs[i].dstirq;
845 }
846 return -1;
847}
848
849static int __init find_isa_irq_apic(int irq, int type)
850{
851 int i;
852
853 for (i = 0; i < mp_irq_entries; i++) {
854 int lbus = mp_irqs[i].srcbus;
855
856 if (test_bit(lbus, mp_bus_not_pci) &&
857 (mp_irqs[i].irqtype == type) &&
858 (mp_irqs[i].srcbusirq == irq))
859 break;
860 }
861 if (i < mp_irq_entries) {
862 int apic;
863 for(apic = 0; apic < nr_ioapics; apic++) {
864 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
865 return apic;
866 }
867 }
868
869 return -1;
870}
871
872#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
873
874
875
876static int EISA_ELCR(unsigned int irq)
877{
878 if (irq < nr_legacy_irqs) {
879 unsigned int port = 0x4d0 + (irq >> 3);
880 return (inb(port) >> (irq & 7)) & 1;
881 }
882 apic_printk(APIC_VERBOSE, KERN_INFO
883 "Broken MPtable reports ISA irq %d\n", irq);
884 return 0;
885}
886
887#endif
888
889
890
891
892#define default_ISA_trigger(idx) (0)
893#define default_ISA_polarity(idx) (0)
894
895
896
897
898
899
900#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
901#define default_EISA_polarity(idx) default_ISA_polarity(idx)
902
903
904
905
906#define default_PCI_trigger(idx) (1)
907#define default_PCI_polarity(idx) (1)
908
909
910
911
912#define default_MCA_trigger(idx) (1)
913#define default_MCA_polarity(idx) default_ISA_polarity(idx)
914
915static int MPBIOS_polarity(int idx)
916{
917 int bus = mp_irqs[idx].srcbus;
918 int polarity;
919
920
921
922
923 switch (mp_irqs[idx].irqflag & 3)
924 {
925 case 0:
926 if (test_bit(bus, mp_bus_not_pci))
927 polarity = default_ISA_polarity(idx);
928 else
929 polarity = default_PCI_polarity(idx);
930 break;
931 case 1:
932 {
933 polarity = 0;
934 break;
935 }
936 case 2:
937 {
938 printk(KERN_WARNING "broken BIOS!!\n");
939 polarity = 1;
940 break;
941 }
942 case 3:
943 {
944 polarity = 1;
945 break;
946 }
947 default:
948 {
949 printk(KERN_WARNING "broken BIOS!!\n");
950 polarity = 1;
951 break;
952 }
953 }
954 return polarity;
955}
956
957static int MPBIOS_trigger(int idx)
958{
959 int bus = mp_irqs[idx].srcbus;
960 int trigger;
961
962
963
964
965 switch ((mp_irqs[idx].irqflag>>2) & 3)
966 {
967 case 0:
968 if (test_bit(bus, mp_bus_not_pci))
969 trigger = default_ISA_trigger(idx);
970 else
971 trigger = default_PCI_trigger(idx);
972#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
973 switch (mp_bus_id_to_type[bus]) {
974 case MP_BUS_ISA:
975 {
976
977 break;
978 }
979 case MP_BUS_EISA:
980 {
981 trigger = default_EISA_trigger(idx);
982 break;
983 }
984 case MP_BUS_PCI:
985 {
986
987 break;
988 }
989 case MP_BUS_MCA:
990 {
991 trigger = default_MCA_trigger(idx);
992 break;
993 }
994 default:
995 {
996 printk(KERN_WARNING "broken BIOS!!\n");
997 trigger = 1;
998 break;
999 }
1000 }
1001#endif
1002 break;
1003 case 1:
1004 {
1005 trigger = 0;
1006 break;
1007 }
1008 case 2:
1009 {
1010 printk(KERN_WARNING "broken BIOS!!\n");
1011 trigger = 1;
1012 break;
1013 }
1014 case 3:
1015 {
1016 trigger = 1;
1017 break;
1018 }
1019 default:
1020 {
1021 printk(KERN_WARNING "broken BIOS!!\n");
1022 trigger = 0;
1023 break;
1024 }
1025 }
1026 return trigger;
1027}
1028
1029static inline int irq_polarity(int idx)
1030{
1031 return MPBIOS_polarity(idx);
1032}
1033
1034static inline int irq_trigger(int idx)
1035{
1036 return MPBIOS_trigger(idx);
1037}
1038
1039int (*ioapic_renumber_irq)(int ioapic, int irq);
1040static int pin_2_irq(int idx, int apic, int pin)
1041{
1042 int irq, i;
1043 int bus = mp_irqs[idx].srcbus;
1044
1045
1046
1047
1048 if (mp_irqs[idx].dstirq != pin)
1049 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1050
1051 if (test_bit(bus, mp_bus_not_pci)) {
1052 irq = mp_irqs[idx].srcbusirq;
1053 } else {
1054
1055
1056
1057 i = irq = 0;
1058 while (i < apic)
1059 irq += nr_ioapic_registers[i++];
1060 irq += pin;
1061
1062
1063
1064 if (ioapic_renumber_irq)
1065 irq = ioapic_renumber_irq(apic, irq);
1066 }
1067
1068#ifdef CONFIG_X86_32
1069
1070
1071
1072 if ((pin >= 16) && (pin <= 23)) {
1073 if (pirq_entries[pin-16] != -1) {
1074 if (!pirq_entries[pin-16]) {
1075 apic_printk(APIC_VERBOSE, KERN_DEBUG
1076 "disabling PIRQ%d\n", pin-16);
1077 } else {
1078 irq = pirq_entries[pin-16];
1079 apic_printk(APIC_VERBOSE, KERN_DEBUG
1080 "using PIRQ%d -> IRQ %d\n",
1081 pin-16, irq);
1082 }
1083 }
1084 }
1085#endif
1086
1087 return irq;
1088}
1089
1090
1091
1092
1093
1094int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
1095 struct io_apic_irq_attr *irq_attr)
1096{
1097 int apic, i, best_guess = -1;
1098
1099 apic_printk(APIC_DEBUG,
1100 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1101 bus, slot, pin);
1102 if (test_bit(bus, mp_bus_not_pci)) {
1103 apic_printk(APIC_VERBOSE,
1104 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1105 return -1;
1106 }
1107 for (i = 0; i < mp_irq_entries; i++) {
1108 int lbus = mp_irqs[i].srcbus;
1109
1110 for (apic = 0; apic < nr_ioapics; apic++)
1111 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1112 mp_irqs[i].dstapic == MP_APIC_ALL)
1113 break;
1114
1115 if (!test_bit(lbus, mp_bus_not_pci) &&
1116 !mp_irqs[i].irqtype &&
1117 (bus == lbus) &&
1118 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1119 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1120
1121 if (!(apic || IO_APIC_IRQ(irq)))
1122 continue;
1123
1124 if (pin == (mp_irqs[i].srcbusirq & 3)) {
1125 set_io_apic_irq_attr(irq_attr, apic,
1126 mp_irqs[i].dstirq,
1127 irq_trigger(i),
1128 irq_polarity(i));
1129 return irq;
1130 }
1131
1132
1133
1134
1135 if (best_guess < 0) {
1136 set_io_apic_irq_attr(irq_attr, apic,
1137 mp_irqs[i].dstirq,
1138 irq_trigger(i),
1139 irq_polarity(i));
1140 best_guess = irq;
1141 }
1142 }
1143 }
1144 return best_guess;
1145}
1146EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1147
1148void lock_vector_lock(void)
1149{
1150
1151
1152
1153 spin_lock(&vector_lock);
1154}
1155
1156void unlock_vector_lock(void)
1157{
1158 spin_unlock(&vector_lock);
1159}
1160
1161static int
1162__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1163{
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1176 unsigned int old_vector;
1177 int cpu, err;
1178 cpumask_var_t tmp_mask;
1179
1180 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1181 return -EBUSY;
1182
1183 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1184 return -ENOMEM;
1185
1186 old_vector = cfg->vector;
1187 if (old_vector) {
1188 cpumask_and(tmp_mask, mask, cpu_online_mask);
1189 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1190 if (!cpumask_empty(tmp_mask)) {
1191 free_cpumask_var(tmp_mask);
1192 return 0;
1193 }
1194 }
1195
1196
1197 err = -ENOSPC;
1198 for_each_cpu_and(cpu, mask, cpu_online_mask) {
1199 int new_cpu;
1200 int vector, offset;
1201
1202 apic->vector_allocation_domain(cpu, tmp_mask);
1203
1204 vector = current_vector;
1205 offset = current_offset;
1206next:
1207 vector += 8;
1208 if (vector >= first_system_vector) {
1209
1210 offset = (offset + 1) % 8;
1211 vector = FIRST_DEVICE_VECTOR + offset;
1212 }
1213 if (unlikely(current_vector == vector))
1214 continue;
1215
1216 if (test_bit(vector, used_vectors))
1217 goto next;
1218
1219 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1220 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1221 goto next;
1222
1223 current_vector = vector;
1224 current_offset = offset;
1225 if (old_vector) {
1226 cfg->move_in_progress = 1;
1227 cpumask_copy(cfg->old_domain, cfg->domain);
1228 }
1229 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1230 per_cpu(vector_irq, new_cpu)[vector] = irq;
1231 cfg->vector = vector;
1232 cpumask_copy(cfg->domain, tmp_mask);
1233 err = 0;
1234 break;
1235 }
1236 free_cpumask_var(tmp_mask);
1237 return err;
1238}
1239
1240static int
1241assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1242{
1243 int err;
1244 unsigned long flags;
1245
1246 spin_lock_irqsave(&vector_lock, flags);
1247 err = __assign_irq_vector(irq, cfg, mask);
1248 spin_unlock_irqrestore(&vector_lock, flags);
1249 return err;
1250}
1251
1252static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
1253{
1254 int cpu, vector;
1255
1256 BUG_ON(!cfg->vector);
1257
1258 vector = cfg->vector;
1259 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
1260 per_cpu(vector_irq, cpu)[vector] = -1;
1261
1262 cfg->vector = 0;
1263 cpumask_clear(cfg->domain);
1264
1265 if (likely(!cfg->move_in_progress))
1266 return;
1267 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
1268 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1269 vector++) {
1270 if (per_cpu(vector_irq, cpu)[vector] != irq)
1271 continue;
1272 per_cpu(vector_irq, cpu)[vector] = -1;
1273 break;
1274 }
1275 }
1276 cfg->move_in_progress = 0;
1277}
1278
1279void __setup_vector_irq(int cpu)
1280{
1281
1282
1283 int irq, vector;
1284 struct irq_cfg *cfg;
1285 struct irq_desc *desc;
1286
1287
1288 for_each_irq_desc(irq, desc) {
1289 cfg = desc->chip_data;
1290 if (!cpumask_test_cpu(cpu, cfg->domain))
1291 continue;
1292 vector = cfg->vector;
1293 per_cpu(vector_irq, cpu)[vector] = irq;
1294 }
1295
1296 for (vector = 0; vector < NR_VECTORS; ++vector) {
1297 irq = per_cpu(vector_irq, cpu)[vector];
1298 if (irq < 0)
1299 continue;
1300
1301 cfg = irq_cfg(irq);
1302 if (!cpumask_test_cpu(cpu, cfg->domain))
1303 per_cpu(vector_irq, cpu)[vector] = -1;
1304 }
1305}
1306
1307static struct irq_chip ioapic_chip;
1308static struct irq_chip ir_ioapic_chip;
1309
1310#define IOAPIC_AUTO -1
1311#define IOAPIC_EDGE 0
1312#define IOAPIC_LEVEL 1
1313
1314#ifdef CONFIG_X86_32
1315static inline int IO_APIC_irq_trigger(int irq)
1316{
1317 int apic, idx, pin;
1318
1319 for (apic = 0; apic < nr_ioapics; apic++) {
1320 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1321 idx = find_irq_entry(apic, pin, mp_INT);
1322 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1323 return irq_trigger(idx);
1324 }
1325 }
1326
1327
1328
1329 return 0;
1330}
1331#else
1332static inline int IO_APIC_irq_trigger(int irq)
1333{
1334 return 1;
1335}
1336#endif
1337
1338static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
1339{
1340
1341 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1342 trigger == IOAPIC_LEVEL)
1343 desc->status |= IRQ_LEVEL;
1344 else
1345 desc->status &= ~IRQ_LEVEL;
1346
1347 if (irq_remapped(irq)) {
1348 desc->status |= IRQ_MOVE_PCNTXT;
1349 if (trigger)
1350 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1351 handle_fasteoi_irq,
1352 "fasteoi");
1353 else
1354 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1355 handle_edge_irq, "edge");
1356 return;
1357 }
1358
1359 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1360 trigger == IOAPIC_LEVEL)
1361 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1362 handle_fasteoi_irq,
1363 "fasteoi");
1364 else
1365 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1366 handle_edge_irq, "edge");
1367}
1368
1369int setup_ioapic_entry(int apic_id, int irq,
1370 struct IO_APIC_route_entry *entry,
1371 unsigned int destination, int trigger,
1372 int polarity, int vector, int pin)
1373{
1374
1375
1376
1377 memset(entry,0,sizeof(*entry));
1378
1379 if (intr_remapping_enabled) {
1380 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
1381 struct irte irte;
1382 struct IR_IO_APIC_route_entry *ir_entry =
1383 (struct IR_IO_APIC_route_entry *) entry;
1384 int index;
1385
1386 if (!iommu)
1387 panic("No mapping iommu for ioapic %d\n", apic_id);
1388
1389 index = alloc_irte(iommu, irq, 1);
1390 if (index < 0)
1391 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
1392
1393 memset(&irte, 0, sizeof(irte));
1394
1395 irte.present = 1;
1396 irte.dst_mode = apic->irq_dest_mode;
1397
1398
1399
1400
1401
1402
1403
1404 irte.trigger_mode = 0;
1405 irte.dlvry_mode = apic->irq_delivery_mode;
1406 irte.vector = vector;
1407 irte.dest_id = IRTE_DEST(destination);
1408
1409
1410 set_ioapic_sid(&irte, apic_id);
1411
1412 modify_irte(irq, &irte);
1413
1414 ir_entry->index2 = (index >> 15) & 0x1;
1415 ir_entry->zero = 0;
1416 ir_entry->format = 1;
1417 ir_entry->index = (index & 0x7fff);
1418
1419
1420
1421
1422 ir_entry->vector = pin;
1423 } else {
1424 entry->delivery_mode = apic->irq_delivery_mode;
1425 entry->dest_mode = apic->irq_dest_mode;
1426 entry->dest = destination;
1427 entry->vector = vector;
1428 }
1429
1430 entry->mask = 0;
1431 entry->trigger = trigger;
1432 entry->polarity = polarity;
1433
1434
1435
1436
1437 if (trigger)
1438 entry->mask = 1;
1439 return 0;
1440}
1441
1442static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
1443 int trigger, int polarity)
1444{
1445 struct irq_cfg *cfg;
1446 struct IO_APIC_route_entry entry;
1447 unsigned int dest;
1448
1449 if (!IO_APIC_IRQ(irq))
1450 return;
1451
1452 cfg = desc->chip_data;
1453
1454 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
1455 return;
1456
1457 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
1458
1459 apic_printk(APIC_VERBOSE,KERN_DEBUG
1460 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1461 "IRQ %d Mode:%i Active:%i)\n",
1462 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
1463 irq, trigger, polarity);
1464
1465
1466 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
1467 dest, trigger, polarity, cfg->vector, pin)) {
1468 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1469 mp_ioapics[apic_id].apicid, pin);
1470 __clear_irq_vector(irq, cfg);
1471 return;
1472 }
1473
1474 ioapic_register_intr(irq, desc, trigger);
1475 if (irq < nr_legacy_irqs)
1476 disable_8259A_irq(irq);
1477
1478 ioapic_write_entry(apic_id, pin, entry);
1479}
1480
1481static struct {
1482 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1483} mp_ioapic_routing[MAX_IO_APICS];
1484
1485static void __init setup_IO_APIC_irqs(void)
1486{
1487 int apic_id = 0, pin, idx, irq;
1488 int notcon = 0;
1489 struct irq_desc *desc;
1490 struct irq_cfg *cfg;
1491 int node = cpu_to_node(boot_cpu_id);
1492
1493 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1494
1495#ifdef CONFIG_ACPI
1496 if (!acpi_disabled && acpi_ioapic) {
1497 apic_id = mp_find_ioapic(0);
1498 if (apic_id < 0)
1499 apic_id = 0;
1500 }
1501#endif
1502
1503 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1504 idx = find_irq_entry(apic_id, pin, mp_INT);
1505 if (idx == -1) {
1506 if (!notcon) {
1507 notcon = 1;
1508 apic_printk(APIC_VERBOSE,
1509 KERN_DEBUG " %d-%d",
1510 mp_ioapics[apic_id].apicid, pin);
1511 } else
1512 apic_printk(APIC_VERBOSE, " %d-%d",
1513 mp_ioapics[apic_id].apicid, pin);
1514 continue;
1515 }
1516 if (notcon) {
1517 apic_printk(APIC_VERBOSE,
1518 " (apicid-pin) not connected\n");
1519 notcon = 0;
1520 }
1521
1522 irq = pin_2_irq(idx, apic_id, pin);
1523
1524
1525
1526
1527
1528 if (apic->multi_timer_check &&
1529 apic->multi_timer_check(apic_id, irq))
1530 continue;
1531
1532 desc = irq_to_desc_alloc_node(irq, node);
1533 if (!desc) {
1534 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1535 continue;
1536 }
1537 cfg = desc->chip_data;
1538 add_pin_to_irq_node(cfg, node, apic_id, pin);
1539
1540
1541
1542
1543 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1544 irq_trigger(idx), irq_polarity(idx));
1545 }
1546
1547 if (notcon)
1548 apic_printk(APIC_VERBOSE,
1549 " (apicid-pin) not connected\n");
1550}
1551
1552
1553
1554
1555static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
1556 int vector)
1557{
1558 struct IO_APIC_route_entry entry;
1559
1560 if (intr_remapping_enabled)
1561 return;
1562
1563 memset(&entry, 0, sizeof(entry));
1564
1565
1566
1567
1568
1569 entry.dest_mode = apic->irq_dest_mode;
1570 entry.mask = 0;
1571 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
1572 entry.delivery_mode = apic->irq_delivery_mode;
1573 entry.polarity = 0;
1574 entry.trigger = 0;
1575 entry.vector = vector;
1576
1577
1578
1579
1580
1581 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1582
1583
1584
1585
1586 ioapic_write_entry(apic_id, pin, entry);
1587}
1588
1589
1590__apicdebuginit(void) print_IO_APIC(void)
1591{
1592 int apic, i;
1593 union IO_APIC_reg_00 reg_00;
1594 union IO_APIC_reg_01 reg_01;
1595 union IO_APIC_reg_02 reg_02;
1596 union IO_APIC_reg_03 reg_03;
1597 unsigned long flags;
1598 struct irq_cfg *cfg;
1599 struct irq_desc *desc;
1600 unsigned int irq;
1601
1602 if (apic_verbosity == APIC_QUIET)
1603 return;
1604
1605 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1606 for (i = 0; i < nr_ioapics; i++)
1607 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1608 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
1609
1610
1611
1612
1613
1614 printk(KERN_INFO "testing the IO APIC.......................\n");
1615
1616 for (apic = 0; apic < nr_ioapics; apic++) {
1617
1618 spin_lock_irqsave(&ioapic_lock, flags);
1619 reg_00.raw = io_apic_read(apic, 0);
1620 reg_01.raw = io_apic_read(apic, 1);
1621 if (reg_01.bits.version >= 0x10)
1622 reg_02.raw = io_apic_read(apic, 2);
1623 if (reg_01.bits.version >= 0x20)
1624 reg_03.raw = io_apic_read(apic, 3);
1625 spin_unlock_irqrestore(&ioapic_lock, flags);
1626
1627 printk("\n");
1628 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
1629 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1630 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1631 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1632 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1633
1634 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1635 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1636
1637 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1638 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1639
1640
1641
1642
1643
1644
1645 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1646 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1647 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1648 }
1649
1650
1651
1652
1653
1654
1655 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1656 reg_03.raw != reg_01.raw) {
1657 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1658 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1659 }
1660
1661 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1662
1663 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1664 " Stat Dmod Deli Vect: \n");
1665
1666 for (i = 0; i <= reg_01.bits.entries; i++) {
1667 struct IO_APIC_route_entry entry;
1668
1669 entry = ioapic_read_entry(apic, i);
1670
1671 printk(KERN_DEBUG " %02x %03X ",
1672 i,
1673 entry.dest
1674 );
1675
1676 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1677 entry.mask,
1678 entry.trigger,
1679 entry.irr,
1680 entry.polarity,
1681 entry.delivery_status,
1682 entry.dest_mode,
1683 entry.delivery_mode,
1684 entry.vector
1685 );
1686 }
1687 }
1688 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1689 for_each_irq_desc(irq, desc) {
1690 struct irq_pin_list *entry;
1691
1692 cfg = desc->chip_data;
1693 entry = cfg->irq_2_pin;
1694 if (!entry)
1695 continue;
1696 printk(KERN_DEBUG "IRQ%d ", irq);
1697 for_each_irq_pin(entry, cfg->irq_2_pin)
1698 printk("-> %d:%d", entry->apic, entry->pin);
1699 printk("\n");
1700 }
1701
1702 printk(KERN_INFO ".................................... done.\n");
1703
1704 return;
1705}
1706
1707__apicdebuginit(void) print_APIC_field(int base)
1708{
1709 int i;
1710
1711 if (apic_verbosity == APIC_QUIET)
1712 return;
1713
1714 printk(KERN_DEBUG);
1715
1716 for (i = 0; i < 8; i++)
1717 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1718
1719 printk(KERN_CONT "\n");
1720}
1721
1722__apicdebuginit(void) print_local_APIC(void *dummy)
1723{
1724 unsigned int i, v, ver, maxlvt;
1725 u64 icr;
1726
1727 if (apic_verbosity == APIC_QUIET)
1728 return;
1729
1730 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1731 smp_processor_id(), hard_smp_processor_id());
1732 v = apic_read(APIC_ID);
1733 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
1734 v = apic_read(APIC_LVR);
1735 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1736 ver = GET_APIC_VERSION(v);
1737 maxlvt = lapic_get_maxlvt();
1738
1739 v = apic_read(APIC_TASKPRI);
1740 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1741
1742 if (APIC_INTEGRATED(ver)) {
1743 if (!APIC_XAPIC(ver)) {
1744 v = apic_read(APIC_ARBPRI);
1745 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1746 v & APIC_ARBPRI_MASK);
1747 }
1748 v = apic_read(APIC_PROCPRI);
1749 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1750 }
1751
1752
1753
1754
1755
1756 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1757 v = apic_read(APIC_RRR);
1758 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1759 }
1760
1761 v = apic_read(APIC_LDR);
1762 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1763 if (!x2apic_enabled()) {
1764 v = apic_read(APIC_DFR);
1765 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1766 }
1767 v = apic_read(APIC_SPIV);
1768 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1769
1770 printk(KERN_DEBUG "... APIC ISR field:\n");
1771 print_APIC_field(APIC_ISR);
1772 printk(KERN_DEBUG "... APIC TMR field:\n");
1773 print_APIC_field(APIC_TMR);
1774 printk(KERN_DEBUG "... APIC IRR field:\n");
1775 print_APIC_field(APIC_IRR);
1776
1777 if (APIC_INTEGRATED(ver)) {
1778 if (maxlvt > 3)
1779 apic_write(APIC_ESR, 0);
1780
1781 v = apic_read(APIC_ESR);
1782 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1783 }
1784
1785 icr = apic_icr_read();
1786 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1787 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1788
1789 v = apic_read(APIC_LVTT);
1790 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1791
1792 if (maxlvt > 3) {
1793 v = apic_read(APIC_LVTPC);
1794 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1795 }
1796 v = apic_read(APIC_LVT0);
1797 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1798 v = apic_read(APIC_LVT1);
1799 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1800
1801 if (maxlvt > 2) {
1802 v = apic_read(APIC_LVTERR);
1803 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1804 }
1805
1806 v = apic_read(APIC_TMICT);
1807 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1808 v = apic_read(APIC_TMCCT);
1809 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1810 v = apic_read(APIC_TDCR);
1811 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1812
1813 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1814 v = apic_read(APIC_EFEAT);
1815 maxlvt = (v >> 16) & 0xff;
1816 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1817 v = apic_read(APIC_ECTRL);
1818 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1819 for (i = 0; i < maxlvt; i++) {
1820 v = apic_read(APIC_EILVTn(i));
1821 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1822 }
1823 }
1824 printk("\n");
1825}
1826
1827__apicdebuginit(void) print_all_local_APICs(void)
1828{
1829 int cpu;
1830
1831 preempt_disable();
1832 for_each_online_cpu(cpu)
1833 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1834 preempt_enable();
1835}
1836
1837__apicdebuginit(void) print_PIC(void)
1838{
1839 unsigned int v;
1840 unsigned long flags;
1841
1842 if (apic_verbosity == APIC_QUIET || !nr_legacy_irqs)
1843 return;
1844
1845 printk(KERN_DEBUG "\nprinting PIC contents\n");
1846
1847 spin_lock_irqsave(&i8259A_lock, flags);
1848
1849 v = inb(0xa1) << 8 | inb(0x21);
1850 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1851
1852 v = inb(0xa0) << 8 | inb(0x20);
1853 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1854
1855 outb(0x0b,0xa0);
1856 outb(0x0b,0x20);
1857 v = inb(0xa0) << 8 | inb(0x20);
1858 outb(0x0a,0xa0);
1859 outb(0x0a,0x20);
1860
1861 spin_unlock_irqrestore(&i8259A_lock, flags);
1862
1863 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1864
1865 v = inb(0x4d1) << 8 | inb(0x4d0);
1866 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1867}
1868
1869__apicdebuginit(int) print_all_ICs(void)
1870{
1871 print_PIC();
1872
1873
1874 if (!cpu_has_apic && !apic_from_smp_config())
1875 return 0;
1876
1877 print_all_local_APICs();
1878 print_IO_APIC();
1879
1880 return 0;
1881}
1882
1883fs_initcall(print_all_ICs);
1884
1885
1886
1887static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1888
1889void __init enable_IO_APIC(void)
1890{
1891 union IO_APIC_reg_01 reg_01;
1892 int i8259_apic, i8259_pin;
1893 int apic;
1894 unsigned long flags;
1895
1896
1897
1898
1899 for (apic = 0; apic < nr_ioapics; apic++) {
1900 spin_lock_irqsave(&ioapic_lock, flags);
1901 reg_01.raw = io_apic_read(apic, 1);
1902 spin_unlock_irqrestore(&ioapic_lock, flags);
1903 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1904 }
1905
1906 if (!nr_legacy_irqs)
1907 return;
1908
1909 for(apic = 0; apic < nr_ioapics; apic++) {
1910 int pin;
1911
1912 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1913 struct IO_APIC_route_entry entry;
1914 entry = ioapic_read_entry(apic, pin);
1915
1916
1917
1918
1919 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1920 ioapic_i8259.apic = apic;
1921 ioapic_i8259.pin = pin;
1922 goto found_i8259;
1923 }
1924 }
1925 }
1926 found_i8259:
1927
1928
1929
1930
1931
1932 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1933 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1934
1935 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1936 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1937 ioapic_i8259.pin = i8259_pin;
1938 ioapic_i8259.apic = i8259_apic;
1939 }
1940
1941 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1942 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1943 {
1944 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1945 }
1946
1947
1948
1949
1950 clear_IO_APIC();
1951}
1952
1953
1954
1955
1956void disable_IO_APIC(void)
1957{
1958
1959
1960
1961 clear_IO_APIC();
1962
1963 if (!nr_legacy_irqs)
1964 return;
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
1977 struct IO_APIC_route_entry entry;
1978
1979 memset(&entry, 0, sizeof(entry));
1980 entry.mask = 0;
1981 entry.trigger = 0;
1982 entry.irr = 0;
1983 entry.polarity = 0;
1984 entry.delivery_status = 0;
1985 entry.dest_mode = 0;
1986 entry.delivery_mode = dest_ExtINT;
1987 entry.vector = 0;
1988 entry.dest = read_apic_id();
1989
1990
1991
1992
1993 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1994 }
1995
1996
1997
1998
1999 if (cpu_has_apic || apic_from_smp_config())
2000 disconnect_bsp_APIC(!intr_remapping_enabled &&
2001 ioapic_i8259.pin != -1);
2002}
2003
2004#ifdef CONFIG_X86_32
2005
2006
2007
2008
2009
2010
2011
2012void __init setup_ioapic_ids_from_mpc(void)
2013{
2014 union IO_APIC_reg_00 reg_00;
2015 physid_mask_t phys_id_present_map;
2016 int apic_id;
2017 int i;
2018 unsigned char old_id;
2019 unsigned long flags;
2020
2021 if (acpi_ioapic)
2022 return;
2023
2024
2025
2026
2027 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2028 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
2029 return;
2030
2031
2032
2033
2034 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
2035
2036
2037
2038
2039 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
2040
2041
2042 spin_lock_irqsave(&ioapic_lock, flags);
2043 reg_00.raw = io_apic_read(apic_id, 0);
2044 spin_unlock_irqrestore(&ioapic_lock, flags);
2045
2046 old_id = mp_ioapics[apic_id].apicid;
2047
2048 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
2049 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
2050 apic_id, mp_ioapics[apic_id].apicid);
2051 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2052 reg_00.bits.ID);
2053 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
2054 }
2055
2056
2057
2058
2059
2060
2061 if (apic->check_apicid_used(phys_id_present_map,
2062 mp_ioapics[apic_id].apicid)) {
2063 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
2064 apic_id, mp_ioapics[apic_id].apicid);
2065 for (i = 0; i < get_physical_broadcast(); i++)
2066 if (!physid_isset(i, phys_id_present_map))
2067 break;
2068 if (i >= get_physical_broadcast())
2069 panic("Max APIC ID exceeded!\n");
2070 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2071 i);
2072 physid_set(i, phys_id_present_map);
2073 mp_ioapics[apic_id].apicid = i;
2074 } else {
2075 physid_mask_t tmp;
2076 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
2077 apic_printk(APIC_VERBOSE, "Setting %d in the "
2078 "phys_id_present_map\n",
2079 mp_ioapics[apic_id].apicid);
2080 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2081 }
2082
2083
2084
2085
2086
2087
2088 if (old_id != mp_ioapics[apic_id].apicid)
2089 for (i = 0; i < mp_irq_entries; i++)
2090 if (mp_irqs[i].dstapic == old_id)
2091 mp_irqs[i].dstapic
2092 = mp_ioapics[apic_id].apicid;
2093
2094
2095
2096
2097
2098 apic_printk(APIC_VERBOSE, KERN_INFO
2099 "...changing IO-APIC physical APIC ID to %d ...",
2100 mp_ioapics[apic_id].apicid);
2101
2102 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
2103 spin_lock_irqsave(&ioapic_lock, flags);
2104 io_apic_write(apic_id, 0, reg_00.raw);
2105 spin_unlock_irqrestore(&ioapic_lock, flags);
2106
2107
2108
2109
2110 spin_lock_irqsave(&ioapic_lock, flags);
2111 reg_00.raw = io_apic_read(apic_id, 0);
2112 spin_unlock_irqrestore(&ioapic_lock, flags);
2113 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
2114 printk("could not set ID!\n");
2115 else
2116 apic_printk(APIC_VERBOSE, " ok.\n");
2117 }
2118}
2119#endif
2120
2121int no_timer_check __initdata;
2122
2123static int __init notimercheck(char *s)
2124{
2125 no_timer_check = 1;
2126 return 1;
2127}
2128__setup("no_timer_check", notimercheck);
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138static int __init timer_irq_works(void)
2139{
2140 unsigned long t1 = jiffies;
2141 unsigned long flags;
2142
2143 if (no_timer_check)
2144 return 1;
2145
2146 local_save_flags(flags);
2147 local_irq_enable();
2148
2149 mdelay((10 * 1000) / HZ);
2150 local_irq_restore(flags);
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161 if (time_after(jiffies, t1 + 4))
2162 return 1;
2163 return 0;
2164}
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189static unsigned int startup_ioapic_irq(unsigned int irq)
2190{
2191 int was_pending = 0;
2192 unsigned long flags;
2193 struct irq_cfg *cfg;
2194
2195 spin_lock_irqsave(&ioapic_lock, flags);
2196 if (irq < nr_legacy_irqs) {
2197 disable_8259A_irq(irq);
2198 if (i8259A_irq_pending(irq))
2199 was_pending = 1;
2200 }
2201 cfg = irq_cfg(irq);
2202 __unmask_IO_APIC_irq(cfg);
2203 spin_unlock_irqrestore(&ioapic_lock, flags);
2204
2205 return was_pending;
2206}
2207
2208static int ioapic_retrigger_irq(unsigned int irq)
2209{
2210
2211 struct irq_cfg *cfg = irq_cfg(irq);
2212 unsigned long flags;
2213
2214 spin_lock_irqsave(&vector_lock, flags);
2215 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
2216 spin_unlock_irqrestore(&vector_lock, flags);
2217
2218 return 1;
2219}
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230#ifdef CONFIG_SMP
2231static void send_cleanup_vector(struct irq_cfg *cfg)
2232{
2233 cpumask_var_t cleanup_mask;
2234
2235 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2236 unsigned int i;
2237 cfg->move_cleanup_count = 0;
2238 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2239 cfg->move_cleanup_count++;
2240 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2241 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2242 } else {
2243 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2244 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
2245 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2246 free_cpumask_var(cleanup_mask);
2247 }
2248 cfg->move_in_progress = 0;
2249}
2250
2251static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
2252{
2253 int apic, pin;
2254 struct irq_pin_list *entry;
2255 u8 vector = cfg->vector;
2256
2257 for_each_irq_pin(entry, cfg->irq_2_pin) {
2258 unsigned int reg;
2259
2260 apic = entry->apic;
2261 pin = entry->pin;
2262
2263
2264
2265
2266 if (!irq_remapped(irq))
2267 io_apic_write(apic, 0x11 + pin*2, dest);
2268 reg = io_apic_read(apic, 0x10 + pin*2);
2269 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2270 reg |= vector;
2271 io_apic_modify(apic, 0x10 + pin*2, reg);
2272 }
2273}
2274
2275static int
2276assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
2277
2278
2279
2280
2281
2282
2283static unsigned int
2284set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
2285{
2286 struct irq_cfg *cfg;
2287 unsigned int irq;
2288
2289 if (!cpumask_intersects(mask, cpu_online_mask))
2290 return BAD_APICID;
2291
2292 irq = desc->irq;
2293 cfg = desc->chip_data;
2294 if (assign_irq_vector(irq, cfg, mask))
2295 return BAD_APICID;
2296
2297 cpumask_copy(desc->affinity, mask);
2298
2299 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2300}
2301
2302static int
2303set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2304{
2305 struct irq_cfg *cfg;
2306 unsigned long flags;
2307 unsigned int dest;
2308 unsigned int irq;
2309 int ret = -1;
2310
2311 irq = desc->irq;
2312 cfg = desc->chip_data;
2313
2314 spin_lock_irqsave(&ioapic_lock, flags);
2315 dest = set_desc_affinity(desc, mask);
2316 if (dest != BAD_APICID) {
2317
2318 dest = SET_APIC_LOGICAL_ID(dest);
2319 __target_IO_APIC_irq(irq, dest, cfg);
2320 ret = 0;
2321 }
2322 spin_unlock_irqrestore(&ioapic_lock, flags);
2323
2324 return ret;
2325}
2326
2327static int
2328set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2329{
2330 struct irq_desc *desc;
2331
2332 desc = irq_to_desc(irq);
2333
2334 return set_ioapic_affinity_irq_desc(desc, mask);
2335}
2336
2337#ifdef CONFIG_INTR_REMAP
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350static int
2351migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2352{
2353 struct irq_cfg *cfg;
2354 struct irte irte;
2355 unsigned int dest;
2356 unsigned int irq;
2357 int ret = -1;
2358
2359 if (!cpumask_intersects(mask, cpu_online_mask))
2360 return ret;
2361
2362 irq = desc->irq;
2363 if (get_irte(irq, &irte))
2364 return ret;
2365
2366 cfg = desc->chip_data;
2367 if (assign_irq_vector(irq, cfg, mask))
2368 return ret;
2369
2370 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
2371
2372 irte.vector = cfg->vector;
2373 irte.dest_id = IRTE_DEST(dest);
2374
2375
2376
2377
2378 modify_irte(irq, &irte);
2379
2380 if (cfg->move_in_progress)
2381 send_cleanup_vector(cfg);
2382
2383 cpumask_copy(desc->affinity, mask);
2384
2385 return 0;
2386}
2387
2388
2389
2390
2391static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2392 const struct cpumask *mask)
2393{
2394 return migrate_ioapic_irq_desc(desc, mask);
2395}
2396static int set_ir_ioapic_affinity_irq(unsigned int irq,
2397 const struct cpumask *mask)
2398{
2399 struct irq_desc *desc = irq_to_desc(irq);
2400
2401 return set_ir_ioapic_affinity_irq_desc(desc, mask);
2402}
2403#else
2404static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2405 const struct cpumask *mask)
2406{
2407 return 0;
2408}
2409#endif
2410
2411asmlinkage void smp_irq_move_cleanup_interrupt(void)
2412{
2413 unsigned vector, me;
2414
2415 ack_APIC_irq();
2416 exit_idle();
2417 irq_enter();
2418
2419 me = smp_processor_id();
2420 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2421 unsigned int irq;
2422 unsigned int irr;
2423 struct irq_desc *desc;
2424 struct irq_cfg *cfg;
2425 irq = __get_cpu_var(vector_irq)[vector];
2426
2427 if (irq == -1)
2428 continue;
2429
2430 desc = irq_to_desc(irq);
2431 if (!desc)
2432 continue;
2433
2434 cfg = irq_cfg(irq);
2435 spin_lock(&desc->lock);
2436 if (!cfg->move_cleanup_count)
2437 goto unlock;
2438
2439 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2440 goto unlock;
2441
2442 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2443
2444
2445
2446
2447
2448
2449
2450 if (irr & (1 << (vector % 32))) {
2451 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2452 goto unlock;
2453 }
2454 __get_cpu_var(vector_irq)[vector] = -1;
2455 cfg->move_cleanup_count--;
2456unlock:
2457 spin_unlock(&desc->lock);
2458 }
2459
2460 irq_exit();
2461}
2462
2463static void irq_complete_move(struct irq_desc **descp)
2464{
2465 struct irq_desc *desc = *descp;
2466 struct irq_cfg *cfg = desc->chip_data;
2467 unsigned vector, me;
2468
2469 if (likely(!cfg->move_in_progress))
2470 return;
2471
2472 vector = ~get_irq_regs()->orig_ax;
2473 me = smp_processor_id();
2474
2475 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2476 send_cleanup_vector(cfg);
2477}
2478#else
2479static inline void irq_complete_move(struct irq_desc **descp) {}
2480#endif
2481
2482static void ack_apic_edge(unsigned int irq)
2483{
2484 struct irq_desc *desc = irq_to_desc(irq);
2485
2486 irq_complete_move(&desc);
2487 move_native_irq(irq);
2488 ack_APIC_irq();
2489}
2490
2491atomic_t irq_mis_count;
2492
2493static void ack_apic_level(unsigned int irq)
2494{
2495 struct irq_desc *desc = irq_to_desc(irq);
2496 unsigned long v;
2497 int i;
2498 struct irq_cfg *cfg;
2499 int do_unmask_irq = 0;
2500
2501 irq_complete_move(&desc);
2502#ifdef CONFIG_GENERIC_PENDING_IRQ
2503
2504 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
2505 do_unmask_irq = 1;
2506 mask_IO_APIC_irq_desc(desc);
2507 }
2508#endif
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529 cfg = desc->chip_data;
2530 i = cfg->vector;
2531 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2532
2533
2534
2535
2536
2537 ack_APIC_irq();
2538
2539
2540 if (unlikely(do_unmask_irq)) {
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567 cfg = desc->chip_data;
2568 if (!io_apic_level_ack_pending(cfg))
2569 move_masked_irq(irq);
2570 unmask_IO_APIC_irq_desc(desc);
2571 }
2572
2573
2574 if (!(v & (1 << (i & 0x1f)))) {
2575 atomic_inc(&irq_mis_count);
2576 spin_lock(&ioapic_lock);
2577 __mask_and_edge_IO_APIC_irq(cfg);
2578 __unmask_and_level_IO_APIC_irq(cfg);
2579 spin_unlock(&ioapic_lock);
2580 }
2581}
2582
2583#ifdef CONFIG_INTR_REMAP
2584static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2585{
2586 struct irq_pin_list *entry;
2587
2588 for_each_irq_pin(entry, cfg->irq_2_pin)
2589 io_apic_eoi(entry->apic, entry->pin);
2590}
2591
2592static void
2593eoi_ioapic_irq(struct irq_desc *desc)
2594{
2595 struct irq_cfg *cfg;
2596 unsigned long flags;
2597 unsigned int irq;
2598
2599 irq = desc->irq;
2600 cfg = desc->chip_data;
2601
2602 spin_lock_irqsave(&ioapic_lock, flags);
2603 __eoi_ioapic_irq(irq, cfg);
2604 spin_unlock_irqrestore(&ioapic_lock, flags);
2605}
2606
2607static void ir_ack_apic_edge(unsigned int irq)
2608{
2609 ack_APIC_irq();
2610}
2611
2612static void ir_ack_apic_level(unsigned int irq)
2613{
2614 struct irq_desc *desc = irq_to_desc(irq);
2615
2616 ack_APIC_irq();
2617 eoi_ioapic_irq(desc);
2618}
2619#endif
2620
2621static struct irq_chip ioapic_chip __read_mostly = {
2622 .name = "IO-APIC",
2623 .startup = startup_ioapic_irq,
2624 .mask = mask_IO_APIC_irq,
2625 .unmask = unmask_IO_APIC_irq,
2626 .ack = ack_apic_edge,
2627 .eoi = ack_apic_level,
2628#ifdef CONFIG_SMP
2629 .set_affinity = set_ioapic_affinity_irq,
2630#endif
2631 .retrigger = ioapic_retrigger_irq,
2632};
2633
2634static struct irq_chip ir_ioapic_chip __read_mostly = {
2635 .name = "IR-IO-APIC",
2636 .startup = startup_ioapic_irq,
2637 .mask = mask_IO_APIC_irq,
2638 .unmask = unmask_IO_APIC_irq,
2639#ifdef CONFIG_INTR_REMAP
2640 .ack = ir_ack_apic_edge,
2641 .eoi = ir_ack_apic_level,
2642#ifdef CONFIG_SMP
2643 .set_affinity = set_ir_ioapic_affinity_irq,
2644#endif
2645#endif
2646 .retrigger = ioapic_retrigger_irq,
2647};
2648
2649static inline void init_IO_APIC_traps(void)
2650{
2651 int irq;
2652 struct irq_desc *desc;
2653 struct irq_cfg *cfg;
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666 for_each_irq_desc(irq, desc) {
2667 cfg = desc->chip_data;
2668 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2669
2670
2671
2672
2673
2674 if (irq < nr_legacy_irqs)
2675 make_8259A_irq(irq);
2676 else
2677
2678 desc->chip = &no_irq_chip;
2679 }
2680 }
2681}
2682
2683
2684
2685
2686
2687static void mask_lapic_irq(unsigned int irq)
2688{
2689 unsigned long v;
2690
2691 v = apic_read(APIC_LVT0);
2692 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2693}
2694
2695static void unmask_lapic_irq(unsigned int irq)
2696{
2697 unsigned long v;
2698
2699 v = apic_read(APIC_LVT0);
2700 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2701}
2702
2703static void ack_lapic_irq(unsigned int irq)
2704{
2705 ack_APIC_irq();
2706}
2707
2708static struct irq_chip lapic_chip __read_mostly = {
2709 .name = "local-APIC",
2710 .mask = mask_lapic_irq,
2711 .unmask = unmask_lapic_irq,
2712 .ack = ack_lapic_irq,
2713};
2714
2715static void lapic_register_intr(int irq, struct irq_desc *desc)
2716{
2717 desc->status &= ~IRQ_LEVEL;
2718 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2719 "edge");
2720}
2721
2722static void __init setup_nmi(void)
2723{
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2734
2735 enable_NMI_through_LVT0();
2736
2737 apic_printk(APIC_VERBOSE, " done.\n");
2738}
2739
2740
2741
2742
2743
2744
2745
2746
2747static inline void __init unlock_ExtINT_logic(void)
2748{
2749 int apic, pin, i;
2750 struct IO_APIC_route_entry entry0, entry1;
2751 unsigned char save_control, save_freq_select;
2752
2753 pin = find_isa_irq_pin(8, mp_INT);
2754 if (pin == -1) {
2755 WARN_ON_ONCE(1);
2756 return;
2757 }
2758 apic = find_isa_irq_apic(8, mp_INT);
2759 if (apic == -1) {
2760 WARN_ON_ONCE(1);
2761 return;
2762 }
2763
2764 entry0 = ioapic_read_entry(apic, pin);
2765 clear_IO_APIC_pin(apic, pin);
2766
2767 memset(&entry1, 0, sizeof(entry1));
2768
2769 entry1.dest_mode = 0;
2770 entry1.mask = 0;
2771 entry1.dest = hard_smp_processor_id();
2772 entry1.delivery_mode = dest_ExtINT;
2773 entry1.polarity = entry0.polarity;
2774 entry1.trigger = 0;
2775 entry1.vector = 0;
2776
2777 ioapic_write_entry(apic, pin, entry1);
2778
2779 save_control = CMOS_READ(RTC_CONTROL);
2780 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2781 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2782 RTC_FREQ_SELECT);
2783 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2784
2785 i = 100;
2786 while (i-- > 0) {
2787 mdelay(10);
2788 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2789 i -= 10;
2790 }
2791
2792 CMOS_WRITE(save_control, RTC_CONTROL);
2793 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2794 clear_IO_APIC_pin(apic, pin);
2795
2796 ioapic_write_entry(apic, pin, entry0);
2797}
2798
2799static int disable_timer_pin_1 __initdata;
2800
2801static int __init disable_timer_pin_setup(char *arg)
2802{
2803 disable_timer_pin_1 = 1;
2804 return 0;
2805}
2806early_param("disable_timer_pin_1", disable_timer_pin_setup);
2807
2808int timer_through_8259 __initdata;
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818static inline void __init check_timer(void)
2819{
2820 struct irq_desc *desc = irq_to_desc(0);
2821 struct irq_cfg *cfg = desc->chip_data;
2822 int node = cpu_to_node(boot_cpu_id);
2823 int apic1, pin1, apic2, pin2;
2824 unsigned long flags;
2825 int no_pin1 = 0;
2826
2827 local_irq_save(flags);
2828
2829
2830
2831
2832 disable_8259A_irq(0);
2833 assign_irq_vector(0, cfg, apic->target_cpus());
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2845 init_8259A(1);
2846#ifdef CONFIG_X86_32
2847 {
2848 unsigned int ver;
2849
2850 ver = apic_read(APIC_LVR);
2851 ver = GET_APIC_VERSION(ver);
2852 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2853 }
2854#endif
2855
2856 pin1 = find_isa_irq_pin(0, mp_INT);
2857 apic1 = find_isa_irq_apic(0, mp_INT);
2858 pin2 = ioapic_i8259.pin;
2859 apic2 = ioapic_i8259.apic;
2860
2861 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2862 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2863 cfg->vector, apic1, pin1, apic2, pin2);
2864
2865
2866
2867
2868
2869
2870
2871
2872 if (pin1 == -1) {
2873 if (intr_remapping_enabled)
2874 panic("BIOS bug: timer not connected to IO-APIC");
2875 pin1 = pin2;
2876 apic1 = apic2;
2877 no_pin1 = 1;
2878 } else if (pin2 == -1) {
2879 pin2 = pin1;
2880 apic2 = apic1;
2881 }
2882
2883 if (pin1 != -1) {
2884
2885
2886
2887 if (no_pin1) {
2888 add_pin_to_irq_node(cfg, node, apic1, pin1);
2889 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2890 } else {
2891
2892
2893
2894
2895
2896 int idx;
2897 idx = find_irq_entry(apic1, pin1, mp_INT);
2898 if (idx != -1 && irq_trigger(idx))
2899 unmask_IO_APIC_irq_desc(desc);
2900 }
2901 if (timer_irq_works()) {
2902 if (nmi_watchdog == NMI_IO_APIC) {
2903 setup_nmi();
2904 enable_8259A_irq(0);
2905 }
2906 if (disable_timer_pin_1 > 0)
2907 clear_IO_APIC_pin(0, pin1);
2908 goto out;
2909 }
2910 if (intr_remapping_enabled)
2911 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2912 local_irq_disable();
2913 clear_IO_APIC_pin(apic1, pin1);
2914 if (!no_pin1)
2915 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2916 "8254 timer not connected to IO-APIC\n");
2917
2918 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2919 "(IRQ0) through the 8259A ...\n");
2920 apic_printk(APIC_QUIET, KERN_INFO
2921 "..... (found apic %d pin %d) ...\n", apic2, pin2);
2922
2923
2924
2925 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
2926 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2927 enable_8259A_irq(0);
2928 if (timer_irq_works()) {
2929 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2930 timer_through_8259 = 1;
2931 if (nmi_watchdog == NMI_IO_APIC) {
2932 disable_8259A_irq(0);
2933 setup_nmi();
2934 enable_8259A_irq(0);
2935 }
2936 goto out;
2937 }
2938
2939
2940
2941 local_irq_disable();
2942 disable_8259A_irq(0);
2943 clear_IO_APIC_pin(apic2, pin2);
2944 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2945 }
2946
2947 if (nmi_watchdog == NMI_IO_APIC) {
2948 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2949 "through the IO-APIC - disabling NMI Watchdog!\n");
2950 nmi_watchdog = NMI_NONE;
2951 }
2952#ifdef CONFIG_X86_32
2953 timer_ack = 0;
2954#endif
2955
2956 apic_printk(APIC_QUIET, KERN_INFO
2957 "...trying to set up timer as Virtual Wire IRQ...\n");
2958
2959 lapic_register_intr(0, desc);
2960 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);
2961 enable_8259A_irq(0);
2962
2963 if (timer_irq_works()) {
2964 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2965 goto out;
2966 }
2967 local_irq_disable();
2968 disable_8259A_irq(0);
2969 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2970 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2971
2972 apic_printk(APIC_QUIET, KERN_INFO
2973 "...trying to set up timer as ExtINT IRQ...\n");
2974
2975 init_8259A(0);
2976 make_8259A_irq(0);
2977 apic_write(APIC_LVT0, APIC_DM_EXTINT);
2978
2979 unlock_ExtINT_logic();
2980
2981 if (timer_irq_works()) {
2982 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2983 goto out;
2984 }
2985 local_irq_disable();
2986 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2987 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2988 "report. Then try booting with the 'noapic' option.\n");
2989out:
2990 local_irq_restore(flags);
2991}
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010#define PIC_IRQS (1UL << PIC_CASCADE_IR)
3011
3012void __init setup_IO_APIC(void)
3013{
3014
3015
3016
3017
3018 io_apic_irqs = nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
3019
3020 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
3021
3022
3023
3024 x86_init.mpparse.setup_ioapic_ids();
3025
3026 sync_Arb_IDs();
3027 setup_IO_APIC_irqs();
3028 init_IO_APIC_traps();
3029 if (nr_legacy_irqs)
3030 check_timer();
3031}
3032
3033
3034
3035
3036
3037
3038static int __init io_apic_bug_finalize(void)
3039{
3040 if (sis_apic_bug == -1)
3041 sis_apic_bug = 0;
3042 return 0;
3043}
3044
3045late_initcall(io_apic_bug_finalize);
3046
3047struct sysfs_ioapic_data {
3048 struct sys_device dev;
3049 struct IO_APIC_route_entry entry[0];
3050};
3051static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
3052
3053static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
3054{
3055 struct IO_APIC_route_entry *entry;
3056 struct sysfs_ioapic_data *data;
3057 int i;
3058
3059 data = container_of(dev, struct sysfs_ioapic_data, dev);
3060 entry = data->entry;
3061 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3062 *entry = ioapic_read_entry(dev->id, i);
3063
3064 return 0;
3065}
3066
3067static int ioapic_resume(struct sys_device *dev)
3068{
3069 struct IO_APIC_route_entry *entry;
3070 struct sysfs_ioapic_data *data;
3071 unsigned long flags;
3072 union IO_APIC_reg_00 reg_00;
3073 int i;
3074
3075 data = container_of(dev, struct sysfs_ioapic_data, dev);
3076 entry = data->entry;
3077
3078 spin_lock_irqsave(&ioapic_lock, flags);
3079 reg_00.raw = io_apic_read(dev->id, 0);
3080 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3081 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
3082 io_apic_write(dev->id, 0, reg_00.raw);
3083 }
3084 spin_unlock_irqrestore(&ioapic_lock, flags);
3085 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
3086 ioapic_write_entry(dev->id, i, entry[i]);
3087
3088 return 0;
3089}
3090
3091static struct sysdev_class ioapic_sysdev_class = {
3092 .name = "ioapic",
3093 .suspend = ioapic_suspend,
3094 .resume = ioapic_resume,
3095};
3096
3097static int __init ioapic_init_sysfs(void)
3098{
3099 struct sys_device * dev;
3100 int i, size, error;
3101
3102 error = sysdev_class_register(&ioapic_sysdev_class);
3103 if (error)
3104 return error;
3105
3106 for (i = 0; i < nr_ioapics; i++ ) {
3107 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
3108 * sizeof(struct IO_APIC_route_entry);
3109 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
3110 if (!mp_ioapic_data[i]) {
3111 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3112 continue;
3113 }
3114 dev = &mp_ioapic_data[i]->dev;
3115 dev->id = i;
3116 dev->cls = &ioapic_sysdev_class;
3117 error = sysdev_register(dev);
3118 if (error) {
3119 kfree(mp_ioapic_data[i]);
3120 mp_ioapic_data[i] = NULL;
3121 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3122 continue;
3123 }
3124 }
3125
3126 return 0;
3127}
3128
3129device_initcall(ioapic_init_sysfs);
3130
3131
3132
3133
3134unsigned int create_irq_nr(unsigned int irq_want, int node)
3135{
3136
3137 unsigned int irq;
3138 unsigned int new;
3139 unsigned long flags;
3140 struct irq_cfg *cfg_new = NULL;
3141 struct irq_desc *desc_new = NULL;
3142
3143 irq = 0;
3144 if (irq_want < nr_irqs_gsi)
3145 irq_want = nr_irqs_gsi;
3146
3147 spin_lock_irqsave(&vector_lock, flags);
3148 for (new = irq_want; new < nr_irqs; new++) {
3149 desc_new = irq_to_desc_alloc_node(new, node);
3150 if (!desc_new) {
3151 printk(KERN_INFO "can not get irq_desc for %d\n", new);
3152 continue;
3153 }
3154 cfg_new = desc_new->chip_data;
3155
3156 if (cfg_new->vector != 0)
3157 continue;
3158
3159 desc_new = move_irq_desc(desc_new, node);
3160
3161 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
3162 irq = new;
3163 break;
3164 }
3165 spin_unlock_irqrestore(&vector_lock, flags);
3166
3167 if (irq > 0) {
3168 dynamic_irq_init(irq);
3169
3170 if (desc_new)
3171 desc_new->chip_data = cfg_new;
3172 }
3173 return irq;
3174}
3175
3176int create_irq(void)
3177{
3178 int node = cpu_to_node(boot_cpu_id);
3179 unsigned int irq_want;
3180 int irq;
3181
3182 irq_want = nr_irqs_gsi;
3183 irq = create_irq_nr(irq_want, node);
3184
3185 if (irq == 0)
3186 irq = -1;
3187
3188 return irq;
3189}
3190
3191void destroy_irq(unsigned int irq)
3192{
3193 unsigned long flags;
3194 struct irq_cfg *cfg;
3195 struct irq_desc *desc;
3196
3197
3198 desc = irq_to_desc(irq);
3199 cfg = desc->chip_data;
3200 dynamic_irq_cleanup(irq);
3201
3202 desc->chip_data = cfg;
3203
3204 free_irte(irq);
3205 spin_lock_irqsave(&vector_lock, flags);
3206 __clear_irq_vector(irq, cfg);
3207 spin_unlock_irqrestore(&vector_lock, flags);
3208}
3209
3210
3211
3212
3213#ifdef CONFIG_PCI_MSI
3214static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
3215{
3216 struct irq_cfg *cfg;
3217 int err;
3218 unsigned dest;
3219
3220 if (disable_apic)
3221 return -ENXIO;
3222
3223 cfg = irq_cfg(irq);
3224 err = assign_irq_vector(irq, cfg, apic->target_cpus());
3225 if (err)
3226 return err;
3227
3228 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
3229
3230 if (irq_remapped(irq)) {
3231 struct irte irte;
3232 int ir_index;
3233 u16 sub_handle;
3234
3235 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3236 BUG_ON(ir_index == -1);
3237
3238 memset (&irte, 0, sizeof(irte));
3239
3240 irte.present = 1;
3241 irte.dst_mode = apic->irq_dest_mode;
3242 irte.trigger_mode = 0;
3243 irte.dlvry_mode = apic->irq_delivery_mode;
3244 irte.vector = cfg->vector;
3245 irte.dest_id = IRTE_DEST(dest);
3246
3247
3248 set_msi_sid(&irte, pdev);
3249
3250 modify_irte(irq, &irte);
3251
3252 msg->address_hi = MSI_ADDR_BASE_HI;
3253 msg->data = sub_handle;
3254 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3255 MSI_ADDR_IR_SHV |
3256 MSI_ADDR_IR_INDEX1(ir_index) |
3257 MSI_ADDR_IR_INDEX2(ir_index);
3258 } else {
3259 if (x2apic_enabled())
3260 msg->address_hi = MSI_ADDR_BASE_HI |
3261 MSI_ADDR_EXT_DEST_ID(dest);
3262 else
3263 msg->address_hi = MSI_ADDR_BASE_HI;
3264
3265 msg->address_lo =
3266 MSI_ADDR_BASE_LO |
3267 ((apic->irq_dest_mode == 0) ?
3268 MSI_ADDR_DEST_MODE_PHYSICAL:
3269 MSI_ADDR_DEST_MODE_LOGICAL) |
3270 ((apic->irq_delivery_mode != dest_LowestPrio) ?
3271 MSI_ADDR_REDIRECTION_CPU:
3272 MSI_ADDR_REDIRECTION_LOWPRI) |
3273 MSI_ADDR_DEST_ID(dest);
3274
3275 msg->data =
3276 MSI_DATA_TRIGGER_EDGE |
3277 MSI_DATA_LEVEL_ASSERT |
3278 ((apic->irq_delivery_mode != dest_LowestPrio) ?
3279 MSI_DATA_DELIVERY_FIXED:
3280 MSI_DATA_DELIVERY_LOWPRI) |
3281 MSI_DATA_VECTOR(cfg->vector);
3282 }
3283 return err;
3284}
3285
3286#ifdef CONFIG_SMP
3287static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
3288{
3289 struct irq_desc *desc = irq_to_desc(irq);
3290 struct irq_cfg *cfg;
3291 struct msi_msg msg;
3292 unsigned int dest;
3293
3294 dest = set_desc_affinity(desc, mask);
3295 if (dest == BAD_APICID)
3296 return -1;
3297
3298 cfg = desc->chip_data;
3299
3300 read_msi_msg_desc(desc, &msg);
3301
3302 msg.data &= ~MSI_DATA_VECTOR_MASK;
3303 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3304 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3305 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3306
3307 write_msi_msg_desc(desc, &msg);
3308
3309 return 0;
3310}
3311#ifdef CONFIG_INTR_REMAP
3312
3313
3314
3315
3316static int
3317ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
3318{
3319 struct irq_desc *desc = irq_to_desc(irq);
3320 struct irq_cfg *cfg = desc->chip_data;
3321 unsigned int dest;
3322 struct irte irte;
3323
3324 if (get_irte(irq, &irte))
3325 return -1;
3326
3327 dest = set_desc_affinity(desc, mask);
3328 if (dest == BAD_APICID)
3329 return -1;
3330
3331 irte.vector = cfg->vector;
3332 irte.dest_id = IRTE_DEST(dest);
3333
3334
3335
3336
3337 modify_irte(irq, &irte);
3338
3339
3340
3341
3342
3343
3344 if (cfg->move_in_progress)
3345 send_cleanup_vector(cfg);
3346
3347 return 0;
3348}
3349
3350#endif
3351#endif
3352
3353
3354
3355
3356
3357static struct irq_chip msi_chip = {
3358 .name = "PCI-MSI",
3359 .unmask = unmask_msi_irq,
3360 .mask = mask_msi_irq,
3361 .ack = ack_apic_edge,
3362#ifdef CONFIG_SMP
3363 .set_affinity = set_msi_irq_affinity,
3364#endif
3365 .retrigger = ioapic_retrigger_irq,
3366};
3367
3368static struct irq_chip msi_ir_chip = {
3369 .name = "IR-PCI-MSI",
3370 .unmask = unmask_msi_irq,
3371 .mask = mask_msi_irq,
3372#ifdef CONFIG_INTR_REMAP
3373 .ack = ir_ack_apic_edge,
3374#ifdef CONFIG_SMP
3375 .set_affinity = ir_set_msi_irq_affinity,
3376#endif
3377#endif
3378 .retrigger = ioapic_retrigger_irq,
3379};
3380
3381
3382
3383
3384
3385
3386static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3387{
3388 struct intel_iommu *iommu;
3389 int index;
3390
3391 iommu = map_dev_to_ir(dev);
3392 if (!iommu) {
3393 printk(KERN_ERR
3394 "Unable to map PCI %s to iommu\n", pci_name(dev));
3395 return -ENOENT;
3396 }
3397
3398 index = alloc_irte(iommu, irq, nvec);
3399 if (index < 0) {
3400 printk(KERN_ERR
3401 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3402 pci_name(dev));
3403 return -ENOSPC;
3404 }
3405 return index;
3406}
3407
3408static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
3409{
3410 int ret;
3411 struct msi_msg msg;
3412
3413 ret = msi_compose_msg(dev, irq, &msg);
3414 if (ret < 0)
3415 return ret;
3416
3417 set_irq_msi(irq, msidesc);
3418 write_msi_msg(irq, &msg);
3419
3420 if (irq_remapped(irq)) {
3421 struct irq_desc *desc = irq_to_desc(irq);
3422
3423
3424
3425 desc->status |= IRQ_MOVE_PCNTXT;
3426 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3427 } else
3428 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3429
3430 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3431
3432 return 0;
3433}
3434
3435int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3436{
3437 unsigned int irq;
3438 int ret, sub_handle;
3439 struct msi_desc *msidesc;
3440 unsigned int irq_want;
3441 struct intel_iommu *iommu = NULL;
3442 int index = 0;
3443 int node;
3444
3445
3446 if (type == PCI_CAP_ID_MSI && nvec > 1)
3447 return 1;
3448
3449 node = dev_to_node(&dev->dev);
3450 irq_want = nr_irqs_gsi;
3451 sub_handle = 0;
3452 list_for_each_entry(msidesc, &dev->msi_list, list) {
3453 irq = create_irq_nr(irq_want, node);
3454 if (irq == 0)
3455 return -1;
3456 irq_want = irq + 1;
3457 if (!intr_remapping_enabled)
3458 goto no_ir;
3459
3460 if (!sub_handle) {
3461
3462
3463
3464
3465 index = msi_alloc_irte(dev, irq, nvec);
3466 if (index < 0) {
3467 ret = index;
3468 goto error;
3469 }
3470 } else {
3471 iommu = map_dev_to_ir(dev);
3472 if (!iommu) {
3473 ret = -ENOENT;
3474 goto error;
3475 }
3476
3477
3478
3479
3480
3481 set_irte_irq(irq, iommu, index, sub_handle);
3482 }
3483no_ir:
3484 ret = setup_msi_irq(dev, msidesc, irq);
3485 if (ret < 0)
3486 goto error;
3487 sub_handle++;
3488 }
3489 return 0;
3490
3491error:
3492 destroy_irq(irq);
3493 return ret;
3494}
3495
3496void arch_teardown_msi_irq(unsigned int irq)
3497{
3498 destroy_irq(irq);
3499}
3500
3501#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
3502#ifdef CONFIG_SMP
3503static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
3504{
3505 struct irq_desc *desc = irq_to_desc(irq);
3506 struct irq_cfg *cfg;
3507 struct msi_msg msg;
3508 unsigned int dest;
3509
3510 dest = set_desc_affinity(desc, mask);
3511 if (dest == BAD_APICID)
3512 return -1;
3513
3514 cfg = desc->chip_data;
3515
3516 dmar_msi_read(irq, &msg);
3517
3518 msg.data &= ~MSI_DATA_VECTOR_MASK;
3519 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3520 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3521 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3522
3523 dmar_msi_write(irq, &msg);
3524
3525 return 0;
3526}
3527
3528#endif
3529
3530static struct irq_chip dmar_msi_type = {
3531 .name = "DMAR_MSI",
3532 .unmask = dmar_msi_unmask,
3533 .mask = dmar_msi_mask,
3534 .ack = ack_apic_edge,
3535#ifdef CONFIG_SMP
3536 .set_affinity = dmar_msi_set_affinity,
3537#endif
3538 .retrigger = ioapic_retrigger_irq,
3539};
3540
3541int arch_setup_dmar_msi(unsigned int irq)
3542{
3543 int ret;
3544 struct msi_msg msg;
3545
3546 ret = msi_compose_msg(NULL, irq, &msg);
3547 if (ret < 0)
3548 return ret;
3549 dmar_msi_write(irq, &msg);
3550 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3551 "edge");
3552 return 0;
3553}
3554#endif
3555
3556#ifdef CONFIG_HPET_TIMER
3557
3558#ifdef CONFIG_SMP
3559static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
3560{
3561 struct irq_desc *desc = irq_to_desc(irq);
3562 struct irq_cfg *cfg;
3563 struct msi_msg msg;
3564 unsigned int dest;
3565
3566 dest = set_desc_affinity(desc, mask);
3567 if (dest == BAD_APICID)
3568 return -1;
3569
3570 cfg = desc->chip_data;
3571
3572 hpet_msi_read(irq, &msg);
3573
3574 msg.data &= ~MSI_DATA_VECTOR_MASK;
3575 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3576 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3577 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3578
3579 hpet_msi_write(irq, &msg);
3580
3581 return 0;
3582}
3583
3584#endif
3585
3586static struct irq_chip hpet_msi_type = {
3587 .name = "HPET_MSI",
3588 .unmask = hpet_msi_unmask,
3589 .mask = hpet_msi_mask,
3590 .ack = ack_apic_edge,
3591#ifdef CONFIG_SMP
3592 .set_affinity = hpet_msi_set_affinity,
3593#endif
3594 .retrigger = ioapic_retrigger_irq,
3595};
3596
3597int arch_setup_hpet_msi(unsigned int irq)
3598{
3599 int ret;
3600 struct msi_msg msg;
3601 struct irq_desc *desc = irq_to_desc(irq);
3602
3603 ret = msi_compose_msg(NULL, irq, &msg);
3604 if (ret < 0)
3605 return ret;
3606
3607 hpet_msi_write(irq, &msg);
3608 desc->status |= IRQ_MOVE_PCNTXT;
3609 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3610 "edge");
3611
3612 return 0;
3613}
3614#endif
3615
3616#endif
3617
3618
3619
3620#ifdef CONFIG_HT_IRQ
3621
3622#ifdef CONFIG_SMP
3623
3624static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3625{
3626 struct ht_irq_msg msg;
3627 fetch_ht_irq_msg(irq, &msg);
3628
3629 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3630 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3631
3632 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3633 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3634
3635 write_ht_irq_msg(irq, &msg);
3636}
3637
3638static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
3639{
3640 struct irq_desc *desc = irq_to_desc(irq);
3641 struct irq_cfg *cfg;
3642 unsigned int dest;
3643
3644 dest = set_desc_affinity(desc, mask);
3645 if (dest == BAD_APICID)
3646 return -1;
3647
3648 cfg = desc->chip_data;
3649
3650 target_ht_irq(irq, dest, cfg->vector);
3651
3652 return 0;
3653}
3654
3655#endif
3656
3657static struct irq_chip ht_irq_chip = {
3658 .name = "PCI-HT",
3659 .mask = mask_ht_irq,
3660 .unmask = unmask_ht_irq,
3661 .ack = ack_apic_edge,
3662#ifdef CONFIG_SMP
3663 .set_affinity = set_ht_irq_affinity,
3664#endif
3665 .retrigger = ioapic_retrigger_irq,
3666};
3667
3668int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3669{
3670 struct irq_cfg *cfg;
3671 int err;
3672
3673 if (disable_apic)
3674 return -ENXIO;
3675
3676 cfg = irq_cfg(irq);
3677 err = assign_irq_vector(irq, cfg, apic->target_cpus());
3678 if (!err) {
3679 struct ht_irq_msg msg;
3680 unsigned dest;
3681
3682 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3683 apic->target_cpus());
3684
3685 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3686
3687 msg.address_lo =
3688 HT_IRQ_LOW_BASE |
3689 HT_IRQ_LOW_DEST_ID(dest) |
3690 HT_IRQ_LOW_VECTOR(cfg->vector) |
3691 ((apic->irq_dest_mode == 0) ?
3692 HT_IRQ_LOW_DM_PHYSICAL :
3693 HT_IRQ_LOW_DM_LOGICAL) |
3694 HT_IRQ_LOW_RQEOI_EDGE |
3695 ((apic->irq_delivery_mode != dest_LowestPrio) ?
3696 HT_IRQ_LOW_MT_FIXED :
3697 HT_IRQ_LOW_MT_ARBITRATED) |
3698 HT_IRQ_LOW_IRQ_MASKED;
3699
3700 write_ht_irq_msg(irq, &msg);
3701
3702 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3703 handle_edge_irq, "edge");
3704
3705 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
3706 }
3707 return err;
3708}
3709#endif
3710
3711#ifdef CONFIG_X86_UV
3712
3713
3714
3715
3716int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3717 unsigned long mmr_offset)
3718{
3719 const struct cpumask *eligible_cpu = cpumask_of(cpu);
3720 struct irq_cfg *cfg;
3721 int mmr_pnode;
3722 unsigned long mmr_value;
3723 struct uv_IO_APIC_route_entry *entry;
3724 unsigned long flags;
3725 int err;
3726
3727 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3728
3729 cfg = irq_cfg(irq);
3730
3731 err = assign_irq_vector(irq, cfg, eligible_cpu);
3732 if (err != 0)
3733 return err;
3734
3735 spin_lock_irqsave(&vector_lock, flags);
3736 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3737 irq_name);
3738 spin_unlock_irqrestore(&vector_lock, flags);
3739
3740 mmr_value = 0;
3741 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3742 entry->vector = cfg->vector;
3743 entry->delivery_mode = apic->irq_delivery_mode;
3744 entry->dest_mode = apic->irq_dest_mode;
3745 entry->polarity = 0;
3746 entry->trigger = 0;
3747 entry->mask = 0;
3748 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
3749
3750 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3751 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3752
3753 if (cfg->move_in_progress)
3754 send_cleanup_vector(cfg);
3755
3756 return irq;
3757}
3758
3759
3760
3761
3762
3763void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3764{
3765 unsigned long mmr_value;
3766 struct uv_IO_APIC_route_entry *entry;
3767 int mmr_pnode;
3768
3769 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3770
3771 mmr_value = 0;
3772 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3773 entry->mask = 1;
3774
3775 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3776 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3777}
3778#endif
3779
3780int __init io_apic_get_redir_entries (int ioapic)
3781{
3782 union IO_APIC_reg_01 reg_01;
3783 unsigned long flags;
3784
3785 spin_lock_irqsave(&ioapic_lock, flags);
3786 reg_01.raw = io_apic_read(ioapic, 1);
3787 spin_unlock_irqrestore(&ioapic_lock, flags);
3788
3789 return reg_01.bits.entries;
3790}
3791
3792void __init probe_nr_irqs_gsi(void)
3793{
3794 int nr = 0;
3795
3796 nr = acpi_probe_gsi();
3797 if (nr > nr_irqs_gsi) {
3798 nr_irqs_gsi = nr;
3799 } else {
3800
3801 int idx;
3802
3803 nr = 0;
3804 for (idx = 0; idx < nr_ioapics; idx++)
3805 nr += io_apic_get_redir_entries(idx) + 1;
3806
3807 if (nr > nr_irqs_gsi)
3808 nr_irqs_gsi = nr;
3809 }
3810
3811 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
3812}
3813
3814#ifdef CONFIG_SPARSE_IRQ
3815int __init arch_probe_nr_irqs(void)
3816{
3817 int nr;
3818
3819 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3820 nr_irqs = NR_VECTORS * nr_cpu_ids;
3821
3822 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3823#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3824
3825
3826
3827 nr += nr_irqs_gsi * 16;
3828#endif
3829 if (nr < nr_irqs)
3830 nr_irqs = nr;
3831
3832 return 0;
3833}
3834#endif
3835
3836static int __io_apic_set_pci_routing(struct device *dev, int irq,
3837 struct io_apic_irq_attr *irq_attr)
3838{
3839 struct irq_desc *desc;
3840 struct irq_cfg *cfg;
3841 int node;
3842 int ioapic, pin;
3843 int trigger, polarity;
3844
3845 ioapic = irq_attr->ioapic;
3846 if (!IO_APIC_IRQ(irq)) {
3847 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3848 ioapic);
3849 return -EINVAL;
3850 }
3851
3852 if (dev)
3853 node = dev_to_node(dev);
3854 else
3855 node = cpu_to_node(boot_cpu_id);
3856
3857 desc = irq_to_desc_alloc_node(irq, node);
3858 if (!desc) {
3859 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3860 return 0;
3861 }
3862
3863 pin = irq_attr->ioapic_pin;
3864 trigger = irq_attr->trigger;
3865 polarity = irq_attr->polarity;
3866
3867
3868
3869
3870 if (irq >= nr_legacy_irqs) {
3871 cfg = desc->chip_data;
3872 if (add_pin_to_irq_node_nopanic(cfg, node, ioapic, pin)) {
3873 printk(KERN_INFO "can not add pin %d for irq %d\n",
3874 pin, irq);
3875 return 0;
3876 }
3877 }
3878
3879 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
3880
3881 return 0;
3882}
3883
3884int io_apic_set_pci_routing(struct device *dev, int irq,
3885 struct io_apic_irq_attr *irq_attr)
3886{
3887 int ioapic, pin;
3888
3889
3890
3891
3892
3893 ioapic = irq_attr->ioapic;
3894 pin = irq_attr->ioapic_pin;
3895 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3896 pr_debug("Pin %d-%d already programmed\n",
3897 mp_ioapics[ioapic].apicid, pin);
3898 return 0;
3899 }
3900 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3901
3902 return __io_apic_set_pci_routing(dev, irq, irq_attr);
3903}
3904
3905u8 __init io_apic_unique_id(u8 id)
3906{
3907#ifdef CONFIG_X86_32
3908 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3909 !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3910 return io_apic_get_unique_id(nr_ioapics, id);
3911 else
3912 return id;
3913#else
3914 int i;
3915 DECLARE_BITMAP(used, 256);
3916
3917 bitmap_zero(used, 256);
3918 for (i = 0; i < nr_ioapics; i++) {
3919 struct mpc_ioapic *ia = &mp_ioapics[i];
3920 __set_bit(ia->apicid, used);
3921 }
3922 if (!test_bit(id, used))
3923 return id;
3924 return find_first_zero_bit(used, 256);
3925#endif
3926}
3927
3928#ifdef CONFIG_X86_32
3929int __init io_apic_get_unique_id(int ioapic, int apic_id)
3930{
3931 union IO_APIC_reg_00 reg_00;
3932 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3933 physid_mask_t tmp;
3934 unsigned long flags;
3935 int i = 0;
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946 if (physids_empty(apic_id_map))
3947 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
3948
3949 spin_lock_irqsave(&ioapic_lock, flags);
3950 reg_00.raw = io_apic_read(ioapic, 0);
3951 spin_unlock_irqrestore(&ioapic_lock, flags);
3952
3953 if (apic_id >= get_physical_broadcast()) {
3954 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3955 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3956 apic_id = reg_00.bits.ID;
3957 }
3958
3959
3960
3961
3962
3963 if (apic->check_apicid_used(apic_id_map, apic_id)) {
3964
3965 for (i = 0; i < get_physical_broadcast(); i++) {
3966 if (!apic->check_apicid_used(apic_id_map, i))
3967 break;
3968 }
3969
3970 if (i == get_physical_broadcast())
3971 panic("Max apic_id exceeded!\n");
3972
3973 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3974 "trying %d\n", ioapic, apic_id, i);
3975
3976 apic_id = i;
3977 }
3978
3979 tmp = apic->apicid_to_cpu_present(apic_id);
3980 physids_or(apic_id_map, apic_id_map, tmp);
3981
3982 if (reg_00.bits.ID != apic_id) {
3983 reg_00.bits.ID = apic_id;
3984
3985 spin_lock_irqsave(&ioapic_lock, flags);
3986 io_apic_write(ioapic, 0, reg_00.raw);
3987 reg_00.raw = io_apic_read(ioapic, 0);
3988 spin_unlock_irqrestore(&ioapic_lock, flags);
3989
3990
3991 if (reg_00.bits.ID != apic_id) {
3992 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3993 return -1;
3994 }
3995 }
3996
3997 apic_printk(APIC_VERBOSE, KERN_INFO
3998 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3999
4000 return apic_id;
4001}
4002#endif
4003
4004int __init io_apic_get_version(int ioapic)
4005{
4006 union IO_APIC_reg_01 reg_01;
4007 unsigned long flags;
4008
4009 spin_lock_irqsave(&ioapic_lock, flags);
4010 reg_01.raw = io_apic_read(ioapic, 1);
4011 spin_unlock_irqrestore(&ioapic_lock, flags);
4012
4013 return reg_01.bits.version;
4014}
4015
4016int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
4017{
4018 int i;
4019
4020 if (skip_ioapic_setup)
4021 return -1;
4022
4023 for (i = 0; i < mp_irq_entries; i++)
4024 if (mp_irqs[i].irqtype == mp_INT &&
4025 mp_irqs[i].srcbusirq == bus_irq)
4026 break;
4027 if (i >= mp_irq_entries)
4028 return -1;
4029
4030 *trigger = irq_trigger(i);
4031 *polarity = irq_polarity(i);
4032 return 0;
4033}
4034
4035
4036
4037
4038
4039
4040#ifdef CONFIG_SMP
4041void __init setup_ioapic_dest(void)
4042{
4043 int pin, ioapic = 0, irq, irq_entry;
4044 struct irq_desc *desc;
4045 const struct cpumask *mask;
4046
4047 if (skip_ioapic_setup == 1)
4048 return;
4049
4050#ifdef CONFIG_ACPI
4051 if (!acpi_disabled && acpi_ioapic) {
4052 ioapic = mp_find_ioapic(0);
4053 if (ioapic < 0)
4054 ioapic = 0;
4055 }
4056#endif
4057
4058 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4059 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4060 if (irq_entry == -1)
4061 continue;
4062 irq = pin_2_irq(irq_entry, ioapic, pin);
4063
4064 desc = irq_to_desc(irq);
4065
4066
4067
4068
4069 if (desc->status &
4070 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4071 mask = desc->affinity;
4072 else
4073 mask = apic->target_cpus();
4074
4075 if (intr_remapping_enabled)
4076 set_ir_ioapic_affinity_irq_desc(desc, mask);
4077 else
4078 set_ioapic_affinity_irq_desc(desc, mask);
4079 }
4080
4081}
4082#endif
4083
4084#define IOAPIC_RESOURCE_NAME_SIZE 11
4085
4086static struct resource *ioapic_resources;
4087
4088static struct resource * __init ioapic_setup_resources(int nr_ioapics)
4089{
4090 unsigned long n;
4091 struct resource *res;
4092 char *mem;
4093 int i;
4094
4095 if (nr_ioapics <= 0)
4096 return NULL;
4097
4098 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4099 n *= nr_ioapics;
4100
4101 mem = alloc_bootmem(n);
4102 res = (void *)mem;
4103
4104 mem += sizeof(struct resource) * nr_ioapics;
4105
4106 for (i = 0; i < nr_ioapics; i++) {
4107 res[i].name = mem;
4108 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4109 sprintf(mem, "IOAPIC %u", i);
4110 mem += IOAPIC_RESOURCE_NAME_SIZE;
4111 }
4112
4113 ioapic_resources = res;
4114
4115 return res;
4116}
4117
4118void __init ioapic_init_mappings(void)
4119{
4120 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
4121 struct resource *ioapic_res;
4122 int i;
4123
4124 ioapic_res = ioapic_setup_resources(nr_ioapics);
4125 for (i = 0; i < nr_ioapics; i++) {
4126 if (smp_found_config) {
4127 ioapic_phys = mp_ioapics[i].apicaddr;
4128#ifdef CONFIG_X86_32
4129 if (!ioapic_phys) {
4130 printk(KERN_ERR
4131 "WARNING: bogus zero IO-APIC "
4132 "address found in MPTABLE, "
4133 "disabling IO/APIC support!\n");
4134 smp_found_config = 0;
4135 skip_ioapic_setup = 1;
4136 goto fake_ioapic_page;
4137 }
4138#endif
4139 } else {
4140#ifdef CONFIG_X86_32
4141fake_ioapic_page:
4142#endif
4143 ioapic_phys = (unsigned long)
4144 alloc_bootmem_pages(PAGE_SIZE);
4145 ioapic_phys = __pa(ioapic_phys);
4146 }
4147 set_fixmap_nocache(idx, ioapic_phys);
4148 apic_printk(APIC_VERBOSE,
4149 "mapped IOAPIC to %08lx (%08lx)\n",
4150 __fix_to_virt(idx), ioapic_phys);
4151 idx++;
4152
4153 ioapic_res->start = ioapic_phys;
4154 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4155 ioapic_res++;
4156 }
4157}
4158
4159void __init ioapic_insert_resources(void)
4160{
4161 int i;
4162 struct resource *r = ioapic_resources;
4163
4164 if (!r) {
4165 if (nr_ioapics > 0)
4166 printk(KERN_ERR
4167 "IO APIC resources couldn't be allocated.\n");
4168 return;
4169 }
4170
4171 for (i = 0; i < nr_ioapics; i++) {
4172 insert_resource(&iomem_resource, r);
4173 r++;
4174 }
4175}
4176
4177int mp_find_ioapic(int gsi)
4178{
4179 int i = 0;
4180
4181
4182 for (i = 0; i < nr_ioapics; i++) {
4183 if ((gsi >= mp_gsi_routing[i].gsi_base)
4184 && (gsi <= mp_gsi_routing[i].gsi_end))
4185 return i;
4186 }
4187
4188 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
4189 return -1;
4190}
4191
4192int mp_find_ioapic_pin(int ioapic, int gsi)
4193{
4194 if (WARN_ON(ioapic == -1))
4195 return -1;
4196 if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
4197 return -1;
4198
4199 return gsi - mp_gsi_routing[ioapic].gsi_base;
4200}
4201
4202static int bad_ioapic(unsigned long address)
4203{
4204 if (nr_ioapics >= MAX_IO_APICS) {
4205 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
4206 "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
4207 return 1;
4208 }
4209 if (!address) {
4210 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
4211 " found in table, skipping!\n");
4212 return 1;
4213 }
4214 return 0;
4215}
4216
4217void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
4218{
4219 int idx = 0;
4220
4221 if (bad_ioapic(address))
4222 return;
4223
4224 idx = nr_ioapics;
4225
4226 mp_ioapics[idx].type = MP_IOAPIC;
4227 mp_ioapics[idx].flags = MPC_APIC_USABLE;
4228 mp_ioapics[idx].apicaddr = address;
4229
4230 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4231 mp_ioapics[idx].apicid = io_apic_unique_id(id);
4232 mp_ioapics[idx].apicver = io_apic_get_version(idx);
4233
4234
4235
4236
4237
4238 mp_gsi_routing[idx].gsi_base = gsi_base;
4239 mp_gsi_routing[idx].gsi_end = gsi_base +
4240 io_apic_get_redir_entries(idx);
4241
4242 printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4243 "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4244 mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4245 mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);
4246
4247 nr_ioapics++;
4248}
4249