1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/mm.h>
35#include <linux/interrupt.h>
36#include <linux/irq.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/sched.h>
40#include <linux/pci.h>
41#include <linux/mc146818rtc.h>
42#include <linux/compiler.h>
43#include <linux/acpi.h>
44#include <linux/export.h>
45#include <linux/syscore_ops.h>
46#include <linux/freezer.h>
47#include <linux/kthread.h>
48#include <linux/jiffies.h>
49#include <linux/slab.h>
50#include <linux/memblock.h>
51
52#include <asm/irqdomain.h>
53#include <asm/io.h>
54#include <asm/smp.h>
55#include <asm/cpu.h>
56#include <asm/desc.h>
57#include <asm/proto.h>
58#include <asm/acpi.h>
59#include <asm/dma.h>
60#include <asm/timer.h>
61#include <asm/time.h>
62#include <asm/i8259.h>
63#include <asm/setup.h>
64#include <asm/irq_remapping.h>
65#include <asm/hw_irq.h>
66
67#include <asm/apic.h>
68
69#define for_each_ioapic(idx) \
70 for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
71#define for_each_ioapic_reverse(idx) \
72 for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
73#define for_each_pin(idx, pin) \
74 for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
75#define for_each_ioapic_pin(idx, pin) \
76 for_each_ioapic((idx)) \
77 for_each_pin((idx), (pin))
78#define for_each_irq_pin(entry, head) \
79 list_for_each_entry(entry, &head, list)
80
81static DEFINE_RAW_SPINLOCK(ioapic_lock);
82static DEFINE_MUTEX(ioapic_mutex);
83static unsigned int ioapic_dynirq_base;
84static int ioapic_initialized;
85
86struct irq_pin_list {
87 struct list_head list;
88 int apic, pin;
89};
90
91struct mp_chip_data {
92 struct list_head irq_2_pin;
93 struct IO_APIC_route_entry entry;
94 int trigger;
95 int polarity;
96 u32 count;
97 bool isa_irq;
98};
99
100struct mp_ioapic_gsi {
101 u32 gsi_base;
102 u32 gsi_end;
103};
104
105static struct ioapic {
106
107
108
109 int nr_registers;
110
111
112
113 struct IO_APIC_route_entry *saved_registers;
114
115 struct mpc_ioapic mp_config;
116
117 struct mp_ioapic_gsi gsi_config;
118 struct ioapic_domain_cfg irqdomain_cfg;
119 struct irq_domain *irqdomain;
120 struct resource *iomem_res;
121} ioapics[MAX_IO_APICS];
122
123#define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
124
125int mpc_ioapic_id(int ioapic_idx)
126{
127 return ioapics[ioapic_idx].mp_config.apicid;
128}
129
130unsigned int mpc_ioapic_addr(int ioapic_idx)
131{
132 return ioapics[ioapic_idx].mp_config.apicaddr;
133}
134
135static inline struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
136{
137 return &ioapics[ioapic_idx].gsi_config;
138}
139
140static inline int mp_ioapic_pin_count(int ioapic)
141{
142 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
143
144 return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
145}
146
147static inline u32 mp_pin_to_gsi(int ioapic, int pin)
148{
149 return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
150}
151
152static inline bool mp_is_legacy_irq(int irq)
153{
154 return irq >= 0 && irq < nr_legacy_irqs();
155}
156
157static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
158{
159 return ioapics[ioapic].irqdomain;
160}
161
162int nr_ioapics;
163
164
165u32 gsi_top;
166
167
168struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
169
170
171int mp_irq_entries;
172
173#ifdef CONFIG_EISA
174int mp_bus_id_to_type[MAX_MP_BUSSES];
175#endif
176
177DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
178
179int skip_ioapic_setup;
180
181
182
183
184void disable_ioapic_support(void)
185{
186#ifdef CONFIG_PCI
187 noioapicquirk = 1;
188 noioapicreroute = -1;
189#endif
190 skip_ioapic_setup = 1;
191}
192
193static int __init parse_noapic(char *str)
194{
195
196 disable_ioapic_support();
197 return 0;
198}
199early_param("noapic", parse_noapic);
200
201
202void mp_save_irq(struct mpc_intsrc *m)
203{
204 int i;
205
206 apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
207 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
208 m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
209 m->srcbusirq, m->dstapic, m->dstirq);
210
211 for (i = 0; i < mp_irq_entries; i++) {
212 if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
213 return;
214 }
215
216 memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
217 if (++mp_irq_entries == MAX_IRQ_SOURCES)
218 panic("Max # of irq sources exceeded!!\n");
219}
220
221static void alloc_ioapic_saved_registers(int idx)
222{
223 size_t size;
224
225 if (ioapics[idx].saved_registers)
226 return;
227
228 size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
229 ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
230 if (!ioapics[idx].saved_registers)
231 pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
232}
233
234static void free_ioapic_saved_registers(int idx)
235{
236 kfree(ioapics[idx].saved_registers);
237 ioapics[idx].saved_registers = NULL;
238}
239
240int __init arch_early_ioapic_init(void)
241{
242 int i;
243
244 if (!nr_legacy_irqs())
245 io_apic_irqs = ~0UL;
246
247 for_each_ioapic(i)
248 alloc_ioapic_saved_registers(i);
249
250 return 0;
251}
252
253struct io_apic {
254 unsigned int index;
255 unsigned int unused[3];
256 unsigned int data;
257 unsigned int unused2[11];
258 unsigned int eoi;
259};
260
261static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
262{
263 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
264 + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
265}
266
267static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
268{
269 struct io_apic __iomem *io_apic = io_apic_base(apic);
270 writel(vector, &io_apic->eoi);
271}
272
273unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
274{
275 struct io_apic __iomem *io_apic = io_apic_base(apic);
276 writel(reg, &io_apic->index);
277 return readl(&io_apic->data);
278}
279
280static void io_apic_write(unsigned int apic, unsigned int reg,
281 unsigned int value)
282{
283 struct io_apic __iomem *io_apic = io_apic_base(apic);
284
285 writel(reg, &io_apic->index);
286 writel(value, &io_apic->data);
287}
288
289union entry_union {
290 struct { u32 w1, w2; };
291 struct IO_APIC_route_entry entry;
292};
293
294static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
295{
296 union entry_union eu;
297
298 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
299 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
300
301 return eu.entry;
302}
303
304static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
305{
306 union entry_union eu;
307 unsigned long flags;
308
309 raw_spin_lock_irqsave(&ioapic_lock, flags);
310 eu.entry = __ioapic_read_entry(apic, pin);
311 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
312
313 return eu.entry;
314}
315
316
317
318
319
320
321
322static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
323{
324 union entry_union eu = {{0, 0}};
325
326 eu.entry = e;
327 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
328 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
329}
330
331static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
332{
333 unsigned long flags;
334
335 raw_spin_lock_irqsave(&ioapic_lock, flags);
336 __ioapic_write_entry(apic, pin, e);
337 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
338}
339
340
341
342
343
344
345static void ioapic_mask_entry(int apic, int pin)
346{
347 unsigned long flags;
348 union entry_union eu = { .entry.mask = IOAPIC_MASKED };
349
350 raw_spin_lock_irqsave(&ioapic_lock, flags);
351 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
352 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
353 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
354}
355
356
357
358
359
360
361static int __add_pin_to_irq_node(struct mp_chip_data *data,
362 int node, int apic, int pin)
363{
364 struct irq_pin_list *entry;
365
366
367 for_each_irq_pin(entry, data->irq_2_pin)
368 if (entry->apic == apic && entry->pin == pin)
369 return 0;
370
371 entry = kzalloc_node(sizeof(struct irq_pin_list), GFP_ATOMIC, node);
372 if (!entry) {
373 pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
374 node, apic, pin);
375 return -ENOMEM;
376 }
377 entry->apic = apic;
378 entry->pin = pin;
379 list_add_tail(&entry->list, &data->irq_2_pin);
380
381 return 0;
382}
383
384static void __remove_pin_from_irq(struct mp_chip_data *data, int apic, int pin)
385{
386 struct irq_pin_list *tmp, *entry;
387
388 list_for_each_entry_safe(entry, tmp, &data->irq_2_pin, list)
389 if (entry->apic == apic && entry->pin == pin) {
390 list_del(&entry->list);
391 kfree(entry);
392 return;
393 }
394}
395
396static void add_pin_to_irq_node(struct mp_chip_data *data,
397 int node, int apic, int pin)
398{
399 if (__add_pin_to_irq_node(data, node, apic, pin))
400 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
401}
402
403
404
405
406static void __init replace_pin_at_irq_node(struct mp_chip_data *data, int node,
407 int oldapic, int oldpin,
408 int newapic, int newpin)
409{
410 struct irq_pin_list *entry;
411
412 for_each_irq_pin(entry, data->irq_2_pin) {
413 if (entry->apic == oldapic && entry->pin == oldpin) {
414 entry->apic = newapic;
415 entry->pin = newpin;
416
417 return;
418 }
419 }
420
421
422 add_pin_to_irq_node(data, node, newapic, newpin);
423}
424
425static void io_apic_modify_irq(struct mp_chip_data *data,
426 int mask_and, int mask_or,
427 void (*final)(struct irq_pin_list *entry))
428{
429 union entry_union eu;
430 struct irq_pin_list *entry;
431
432 eu.entry = data->entry;
433 eu.w1 &= mask_and;
434 eu.w1 |= mask_or;
435 data->entry = eu.entry;
436
437 for_each_irq_pin(entry, data->irq_2_pin) {
438 io_apic_write(entry->apic, 0x10 + 2 * entry->pin, eu.w1);
439 if (final)
440 final(entry);
441 }
442}
443
444static void io_apic_sync(struct irq_pin_list *entry)
445{
446
447
448
449
450 struct io_apic __iomem *io_apic;
451
452 io_apic = io_apic_base(entry->apic);
453 readl(&io_apic->data);
454}
455
456static void mask_ioapic_irq(struct irq_data *irq_data)
457{
458 struct mp_chip_data *data = irq_data->chip_data;
459 unsigned long flags;
460
461 raw_spin_lock_irqsave(&ioapic_lock, flags);
462 io_apic_modify_irq(data, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
463 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
464}
465
466static void __unmask_ioapic(struct mp_chip_data *data)
467{
468 io_apic_modify_irq(data, ~IO_APIC_REDIR_MASKED, 0, NULL);
469}
470
471static void unmask_ioapic_irq(struct irq_data *irq_data)
472{
473 struct mp_chip_data *data = irq_data->chip_data;
474 unsigned long flags;
475
476 raw_spin_lock_irqsave(&ioapic_lock, flags);
477 __unmask_ioapic(data);
478 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
479}
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497static void __eoi_ioapic_pin(int apic, int pin, int vector)
498{
499 if (mpc_ioapic_ver(apic) >= 0x20) {
500 io_apic_eoi(apic, vector);
501 } else {
502 struct IO_APIC_route_entry entry, entry1;
503
504 entry = entry1 = __ioapic_read_entry(apic, pin);
505
506
507
508
509 entry1.mask = IOAPIC_MASKED;
510 entry1.trigger = IOAPIC_EDGE;
511
512 __ioapic_write_entry(apic, pin, entry1);
513
514
515
516
517 __ioapic_write_entry(apic, pin, entry);
518 }
519}
520
521static void eoi_ioapic_pin(int vector, struct mp_chip_data *data)
522{
523 unsigned long flags;
524 struct irq_pin_list *entry;
525
526 raw_spin_lock_irqsave(&ioapic_lock, flags);
527 for_each_irq_pin(entry, data->irq_2_pin)
528 __eoi_ioapic_pin(entry->apic, entry->pin, vector);
529 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
530}
531
532static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
533{
534 struct IO_APIC_route_entry entry;
535
536
537 entry = ioapic_read_entry(apic, pin);
538 if (entry.delivery_mode == dest_SMI)
539 return;
540
541
542
543
544
545 if (entry.mask == IOAPIC_UNMASKED) {
546 entry.mask = IOAPIC_MASKED;
547 ioapic_write_entry(apic, pin, entry);
548 entry = ioapic_read_entry(apic, pin);
549 }
550
551 if (entry.irr) {
552 unsigned long flags;
553
554
555
556
557
558
559 if (entry.trigger == IOAPIC_EDGE) {
560 entry.trigger = IOAPIC_LEVEL;
561 ioapic_write_entry(apic, pin, entry);
562 }
563 raw_spin_lock_irqsave(&ioapic_lock, flags);
564 __eoi_ioapic_pin(apic, pin, entry.vector);
565 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
566 }
567
568
569
570
571
572 ioapic_mask_entry(apic, pin);
573 entry = ioapic_read_entry(apic, pin);
574 if (entry.irr)
575 pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
576 mpc_ioapic_id(apic), pin);
577}
578
579void clear_IO_APIC (void)
580{
581 int apic, pin;
582
583 for_each_ioapic_pin(apic, pin)
584 clear_IO_APIC_pin(apic, pin);
585}
586
587#ifdef CONFIG_X86_32
588
589
590
591
592
593#define MAX_PIRQS 8
594static int pirq_entries[MAX_PIRQS] = {
595 [0 ... MAX_PIRQS - 1] = -1
596};
597
598static int __init ioapic_pirq_setup(char *str)
599{
600 int i, max;
601 int ints[MAX_PIRQS+1];
602
603 get_options(str, ARRAY_SIZE(ints), ints);
604
605 apic_printk(APIC_VERBOSE, KERN_INFO
606 "PIRQ redirection, working around broken MP-BIOS.\n");
607 max = MAX_PIRQS;
608 if (ints[0] < MAX_PIRQS)
609 max = ints[0];
610
611 for (i = 0; i < max; i++) {
612 apic_printk(APIC_VERBOSE, KERN_DEBUG
613 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
614
615
616
617 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
618 }
619 return 1;
620}
621
622__setup("pirq=", ioapic_pirq_setup);
623#endif
624
625
626
627
628int save_ioapic_entries(void)
629{
630 int apic, pin;
631 int err = 0;
632
633 for_each_ioapic(apic) {
634 if (!ioapics[apic].saved_registers) {
635 err = -ENOMEM;
636 continue;
637 }
638
639 for_each_pin(apic, pin)
640 ioapics[apic].saved_registers[pin] =
641 ioapic_read_entry(apic, pin);
642 }
643
644 return err;
645}
646
647
648
649
650void mask_ioapic_entries(void)
651{
652 int apic, pin;
653
654 for_each_ioapic(apic) {
655 if (!ioapics[apic].saved_registers)
656 continue;
657
658 for_each_pin(apic, pin) {
659 struct IO_APIC_route_entry entry;
660
661 entry = ioapics[apic].saved_registers[pin];
662 if (entry.mask == IOAPIC_UNMASKED) {
663 entry.mask = IOAPIC_MASKED;
664 ioapic_write_entry(apic, pin, entry);
665 }
666 }
667 }
668}
669
670
671
672
673int restore_ioapic_entries(void)
674{
675 int apic, pin;
676
677 for_each_ioapic(apic) {
678 if (!ioapics[apic].saved_registers)
679 continue;
680
681 for_each_pin(apic, pin)
682 ioapic_write_entry(apic, pin,
683 ioapics[apic].saved_registers[pin]);
684 }
685 return 0;
686}
687
688
689
690
691static int find_irq_entry(int ioapic_idx, int pin, int type)
692{
693 int i;
694
695 for (i = 0; i < mp_irq_entries; i++)
696 if (mp_irqs[i].irqtype == type &&
697 (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
698 mp_irqs[i].dstapic == MP_APIC_ALL) &&
699 mp_irqs[i].dstirq == pin)
700 return i;
701
702 return -1;
703}
704
705
706
707
708static int __init find_isa_irq_pin(int irq, int type)
709{
710 int i;
711
712 for (i = 0; i < mp_irq_entries; i++) {
713 int lbus = mp_irqs[i].srcbus;
714
715 if (test_bit(lbus, mp_bus_not_pci) &&
716 (mp_irqs[i].irqtype == type) &&
717 (mp_irqs[i].srcbusirq == irq))
718
719 return mp_irqs[i].dstirq;
720 }
721 return -1;
722}
723
724static int __init find_isa_irq_apic(int irq, int type)
725{
726 int i;
727
728 for (i = 0; i < mp_irq_entries; i++) {
729 int lbus = mp_irqs[i].srcbus;
730
731 if (test_bit(lbus, mp_bus_not_pci) &&
732 (mp_irqs[i].irqtype == type) &&
733 (mp_irqs[i].srcbusirq == irq))
734 break;
735 }
736
737 if (i < mp_irq_entries) {
738 int ioapic_idx;
739
740 for_each_ioapic(ioapic_idx)
741 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
742 return ioapic_idx;
743 }
744
745 return -1;
746}
747
748#ifdef CONFIG_EISA
749
750
751
752static int EISA_ELCR(unsigned int irq)
753{
754 if (irq < nr_legacy_irqs()) {
755 unsigned int port = 0x4d0 + (irq >> 3);
756 return (inb(port) >> (irq & 7)) & 1;
757 }
758 apic_printk(APIC_VERBOSE, KERN_INFO
759 "Broken MPtable reports ISA irq %d\n", irq);
760 return 0;
761}
762
763#endif
764
765
766
767
768#define default_ISA_trigger(idx) (IOAPIC_EDGE)
769#define default_ISA_polarity(idx) (IOAPIC_POL_HIGH)
770
771
772
773
774
775
776#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
777#define default_EISA_polarity(idx) default_ISA_polarity(idx)
778
779
780
781
782#define default_PCI_trigger(idx) (IOAPIC_LEVEL)
783#define default_PCI_polarity(idx) (IOAPIC_POL_LOW)
784
785static int irq_polarity(int idx)
786{
787 int bus = mp_irqs[idx].srcbus;
788
789
790
791
792 switch (mp_irqs[idx].irqflag & MP_IRQPOL_MASK) {
793 case MP_IRQPOL_DEFAULT:
794
795 if (test_bit(bus, mp_bus_not_pci))
796 return default_ISA_polarity(idx);
797 else
798 return default_PCI_polarity(idx);
799 case MP_IRQPOL_ACTIVE_HIGH:
800 return IOAPIC_POL_HIGH;
801 case MP_IRQPOL_RESERVED:
802 pr_warn("IOAPIC: Invalid polarity: 2, defaulting to low\n");
803
804 case MP_IRQPOL_ACTIVE_LOW:
805 default:
806 return IOAPIC_POL_LOW;
807 }
808}
809
810#ifdef CONFIG_EISA
811static int eisa_irq_trigger(int idx, int bus, int trigger)
812{
813 switch (mp_bus_id_to_type[bus]) {
814 case MP_BUS_PCI:
815 case MP_BUS_ISA:
816 return trigger;
817 case MP_BUS_EISA:
818 return default_EISA_trigger(idx);
819 }
820 pr_warn("IOAPIC: Invalid srcbus: %d defaulting to level\n", bus);
821 return IOAPIC_LEVEL;
822}
823#else
824static inline int eisa_irq_trigger(int idx, int bus, int trigger)
825{
826 return trigger;
827}
828#endif
829
830static int irq_trigger(int idx)
831{
832 int bus = mp_irqs[idx].srcbus;
833 int trigger;
834
835
836
837
838 switch (mp_irqs[idx].irqflag & MP_IRQTRIG_MASK) {
839 case MP_IRQTRIG_DEFAULT:
840
841 if (test_bit(bus, mp_bus_not_pci))
842 trigger = default_ISA_trigger(idx);
843 else
844 trigger = default_PCI_trigger(idx);
845
846 return eisa_irq_trigger(idx, bus, trigger);
847 case MP_IRQTRIG_EDGE:
848 return IOAPIC_EDGE;
849 case MP_IRQTRIG_RESERVED:
850 pr_warn("IOAPIC: Invalid trigger mode 2 defaulting to level\n");
851
852 case MP_IRQTRIG_LEVEL:
853 default:
854 return IOAPIC_LEVEL;
855 }
856}
857
858void ioapic_set_alloc_attr(struct irq_alloc_info *info, int node,
859 int trigger, int polarity)
860{
861 init_irq_alloc_info(info, NULL);
862 info->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
863 info->ioapic_node = node;
864 info->ioapic_trigger = trigger;
865 info->ioapic_polarity = polarity;
866 info->ioapic_valid = 1;
867}
868
869#ifndef CONFIG_ACPI
870int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity);
871#endif
872
873static void ioapic_copy_alloc_attr(struct irq_alloc_info *dst,
874 struct irq_alloc_info *src,
875 u32 gsi, int ioapic_idx, int pin)
876{
877 int trigger, polarity;
878
879 copy_irq_alloc_info(dst, src);
880 dst->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
881 dst->ioapic_id = mpc_ioapic_id(ioapic_idx);
882 dst->ioapic_pin = pin;
883 dst->ioapic_valid = 1;
884 if (src && src->ioapic_valid) {
885 dst->ioapic_node = src->ioapic_node;
886 dst->ioapic_trigger = src->ioapic_trigger;
887 dst->ioapic_polarity = src->ioapic_polarity;
888 } else {
889 dst->ioapic_node = NUMA_NO_NODE;
890 if (acpi_get_override_irq(gsi, &trigger, &polarity) >= 0) {
891 dst->ioapic_trigger = trigger;
892 dst->ioapic_polarity = polarity;
893 } else {
894
895
896
897
898 dst->ioapic_trigger = IOAPIC_LEVEL;
899 dst->ioapic_polarity = IOAPIC_POL_LOW;
900 }
901 }
902}
903
904static int ioapic_alloc_attr_node(struct irq_alloc_info *info)
905{
906 return (info && info->ioapic_valid) ? info->ioapic_node : NUMA_NO_NODE;
907}
908
909static void mp_register_handler(unsigned int irq, unsigned long trigger)
910{
911 irq_flow_handler_t hdl;
912 bool fasteoi;
913
914 if (trigger) {
915 irq_set_status_flags(irq, IRQ_LEVEL);
916 fasteoi = true;
917 } else {
918 irq_clear_status_flags(irq, IRQ_LEVEL);
919 fasteoi = false;
920 }
921
922 hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
923 __irq_set_handler(irq, hdl, 0, fasteoi ? "fasteoi" : "edge");
924}
925
926static bool mp_check_pin_attr(int irq, struct irq_alloc_info *info)
927{
928 struct mp_chip_data *data = irq_get_chip_data(irq);
929
930
931
932
933
934
935 if (irq < nr_legacy_irqs() && data->count == 1) {
936 if (info->ioapic_trigger != data->trigger)
937 mp_register_handler(irq, info->ioapic_trigger);
938 data->entry.trigger = data->trigger = info->ioapic_trigger;
939 data->entry.polarity = data->polarity = info->ioapic_polarity;
940 }
941
942 return data->trigger == info->ioapic_trigger &&
943 data->polarity == info->ioapic_polarity;
944}
945
946static int alloc_irq_from_domain(struct irq_domain *domain, int ioapic, u32 gsi,
947 struct irq_alloc_info *info)
948{
949 bool legacy = false;
950 int irq = -1;
951 int type = ioapics[ioapic].irqdomain_cfg.type;
952
953 switch (type) {
954 case IOAPIC_DOMAIN_LEGACY:
955
956
957
958
959 if (!ioapic_initialized || gsi >= nr_legacy_irqs())
960 irq = gsi;
961 legacy = mp_is_legacy_irq(irq);
962 break;
963 case IOAPIC_DOMAIN_STRICT:
964 irq = gsi;
965 break;
966 case IOAPIC_DOMAIN_DYNAMIC:
967 break;
968 default:
969 WARN(1, "ioapic: unknown irqdomain type %d\n", type);
970 return -1;
971 }
972
973 return __irq_domain_alloc_irqs(domain, irq, 1,
974 ioapic_alloc_attr_node(info),
975 info, legacy, NULL);
976}
977
978
979
980
981
982
983
984
985
986
987
988static int alloc_isa_irq_from_domain(struct irq_domain *domain,
989 int irq, int ioapic, int pin,
990 struct irq_alloc_info *info)
991{
992 struct mp_chip_data *data;
993 struct irq_data *irq_data = irq_get_irq_data(irq);
994 int node = ioapic_alloc_attr_node(info);
995
996
997
998
999
1000
1001 if (irq_data && irq_data->parent_data) {
1002 if (!mp_check_pin_attr(irq, info))
1003 return -EBUSY;
1004 if (__add_pin_to_irq_node(irq_data->chip_data, node, ioapic,
1005 info->ioapic_pin))
1006 return -ENOMEM;
1007 } else {
1008 info->flags |= X86_IRQ_ALLOC_LEGACY;
1009 irq = __irq_domain_alloc_irqs(domain, irq, 1, node, info, true,
1010 NULL);
1011 if (irq >= 0) {
1012 irq_data = irq_domain_get_irq_data(domain, irq);
1013 data = irq_data->chip_data;
1014 data->isa_irq = true;
1015 }
1016 }
1017
1018 return irq;
1019}
1020
1021static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
1022 unsigned int flags, struct irq_alloc_info *info)
1023{
1024 int irq;
1025 bool legacy = false;
1026 struct irq_alloc_info tmp;
1027 struct mp_chip_data *data;
1028 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
1029
1030 if (!domain)
1031 return -ENOSYS;
1032
1033 if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
1034 irq = mp_irqs[idx].srcbusirq;
1035 legacy = mp_is_legacy_irq(irq);
1036 }
1037
1038 mutex_lock(&ioapic_mutex);
1039 if (!(flags & IOAPIC_MAP_ALLOC)) {
1040 if (!legacy) {
1041 irq = irq_find_mapping(domain, pin);
1042 if (irq == 0)
1043 irq = -ENOENT;
1044 }
1045 } else {
1046 ioapic_copy_alloc_attr(&tmp, info, gsi, ioapic, pin);
1047 if (legacy)
1048 irq = alloc_isa_irq_from_domain(domain, irq,
1049 ioapic, pin, &tmp);
1050 else if ((irq = irq_find_mapping(domain, pin)) == 0)
1051 irq = alloc_irq_from_domain(domain, ioapic, gsi, &tmp);
1052 else if (!mp_check_pin_attr(irq, &tmp))
1053 irq = -EBUSY;
1054 if (irq >= 0) {
1055 data = irq_get_chip_data(irq);
1056 data->count++;
1057 }
1058 }
1059 mutex_unlock(&ioapic_mutex);
1060
1061 return irq;
1062}
1063
1064static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
1065{
1066 u32 gsi = mp_pin_to_gsi(ioapic, pin);
1067
1068
1069
1070
1071 if (mp_irqs[idx].dstirq != pin)
1072 pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
1073
1074#ifdef CONFIG_X86_32
1075
1076
1077
1078 if ((pin >= 16) && (pin <= 23)) {
1079 if (pirq_entries[pin-16] != -1) {
1080 if (!pirq_entries[pin-16]) {
1081 apic_printk(APIC_VERBOSE, KERN_DEBUG
1082 "disabling PIRQ%d\n", pin-16);
1083 } else {
1084 int irq = pirq_entries[pin-16];
1085 apic_printk(APIC_VERBOSE, KERN_DEBUG
1086 "using PIRQ%d -> IRQ %d\n",
1087 pin-16, irq);
1088 return irq;
1089 }
1090 }
1091 }
1092#endif
1093
1094 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, NULL);
1095}
1096
1097int mp_map_gsi_to_irq(u32 gsi, unsigned int flags, struct irq_alloc_info *info)
1098{
1099 int ioapic, pin, idx;
1100
1101 ioapic = mp_find_ioapic(gsi);
1102 if (ioapic < 0)
1103 return -ENODEV;
1104
1105 pin = mp_find_ioapic_pin(ioapic, gsi);
1106 idx = find_irq_entry(ioapic, pin, mp_INT);
1107 if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
1108 return -ENODEV;
1109
1110 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, info);
1111}
1112
1113void mp_unmap_irq(int irq)
1114{
1115 struct irq_data *irq_data = irq_get_irq_data(irq);
1116 struct mp_chip_data *data;
1117
1118 if (!irq_data || !irq_data->domain)
1119 return;
1120
1121 data = irq_data->chip_data;
1122 if (!data || data->isa_irq)
1123 return;
1124
1125 mutex_lock(&ioapic_mutex);
1126 if (--data->count == 0)
1127 irq_domain_free_irqs(irq, 1);
1128 mutex_unlock(&ioapic_mutex);
1129}
1130
1131
1132
1133
1134
1135int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
1136{
1137 int irq, i, best_ioapic = -1, best_idx = -1;
1138
1139 apic_printk(APIC_DEBUG,
1140 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1141 bus, slot, pin);
1142 if (test_bit(bus, mp_bus_not_pci)) {
1143 apic_printk(APIC_VERBOSE,
1144 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1145 return -1;
1146 }
1147
1148 for (i = 0; i < mp_irq_entries; i++) {
1149 int lbus = mp_irqs[i].srcbus;
1150 int ioapic_idx, found = 0;
1151
1152 if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
1153 slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
1154 continue;
1155
1156 for_each_ioapic(ioapic_idx)
1157 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
1158 mp_irqs[i].dstapic == MP_APIC_ALL) {
1159 found = 1;
1160 break;
1161 }
1162 if (!found)
1163 continue;
1164
1165
1166 irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
1167 if (irq > 0 && !IO_APIC_IRQ(irq))
1168 continue;
1169
1170 if (pin == (mp_irqs[i].srcbusirq & 3)) {
1171 best_idx = i;
1172 best_ioapic = ioapic_idx;
1173 goto out;
1174 }
1175
1176
1177
1178
1179
1180 if (best_idx < 0) {
1181 best_idx = i;
1182 best_ioapic = ioapic_idx;
1183 }
1184 }
1185 if (best_idx < 0)
1186 return -1;
1187
1188out:
1189 return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
1190 IOAPIC_MAP_ALLOC);
1191}
1192EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1193
1194static struct irq_chip ioapic_chip, ioapic_ir_chip;
1195
1196static void __init setup_IO_APIC_irqs(void)
1197{
1198 unsigned int ioapic, pin;
1199 int idx;
1200
1201 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1202
1203 for_each_ioapic_pin(ioapic, pin) {
1204 idx = find_irq_entry(ioapic, pin, mp_INT);
1205 if (idx < 0)
1206 apic_printk(APIC_VERBOSE,
1207 KERN_DEBUG " apic %d pin %d not connected\n",
1208 mpc_ioapic_id(ioapic), pin);
1209 else
1210 pin_2_irq(idx, ioapic, pin,
1211 ioapic ? 0 : IOAPIC_MAP_ALLOC);
1212 }
1213}
1214
1215void ioapic_zap_locks(void)
1216{
1217 raw_spin_lock_init(&ioapic_lock);
1218}
1219
1220static void io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
1221{
1222 int i;
1223 char buf[256];
1224 struct IO_APIC_route_entry entry;
1225 struct IR_IO_APIC_route_entry *ir_entry = (void *)&entry;
1226
1227 printk(KERN_DEBUG "IOAPIC %d:\n", apic);
1228 for (i = 0; i <= nr_entries; i++) {
1229 entry = ioapic_read_entry(apic, i);
1230 snprintf(buf, sizeof(buf),
1231 " pin%02x, %s, %s, %s, V(%02X), IRR(%1d), S(%1d)",
1232 i,
1233 entry.mask == IOAPIC_MASKED ? "disabled" : "enabled ",
1234 entry.trigger == IOAPIC_LEVEL ? "level" : "edge ",
1235 entry.polarity == IOAPIC_POL_LOW ? "low " : "high",
1236 entry.vector, entry.irr, entry.delivery_status);
1237 if (ir_entry->format)
1238 printk(KERN_DEBUG "%s, remapped, I(%04X), Z(%X)\n",
1239 buf, (ir_entry->index2 << 15) | ir_entry->index,
1240 ir_entry->zero);
1241 else
1242 printk(KERN_DEBUG "%s, %s, D(%02X), M(%1d)\n",
1243 buf,
1244 entry.dest_mode == IOAPIC_DEST_MODE_LOGICAL ?
1245 "logical " : "physical",
1246 entry.dest, entry.delivery_mode);
1247 }
1248}
1249
1250static void __init print_IO_APIC(int ioapic_idx)
1251{
1252 union IO_APIC_reg_00 reg_00;
1253 union IO_APIC_reg_01 reg_01;
1254 union IO_APIC_reg_02 reg_02;
1255 union IO_APIC_reg_03 reg_03;
1256 unsigned long flags;
1257
1258 raw_spin_lock_irqsave(&ioapic_lock, flags);
1259 reg_00.raw = io_apic_read(ioapic_idx, 0);
1260 reg_01.raw = io_apic_read(ioapic_idx, 1);
1261 if (reg_01.bits.version >= 0x10)
1262 reg_02.raw = io_apic_read(ioapic_idx, 2);
1263 if (reg_01.bits.version >= 0x20)
1264 reg_03.raw = io_apic_read(ioapic_idx, 3);
1265 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1266
1267 printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
1268 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1269 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1270 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1271 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1272
1273 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1274 printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
1275 reg_01.bits.entries);
1276
1277 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1278 printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
1279 reg_01.bits.version);
1280
1281
1282
1283
1284
1285
1286 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1287 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1288 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1289 }
1290
1291
1292
1293
1294
1295
1296 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1297 reg_03.raw != reg_01.raw) {
1298 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1299 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1300 }
1301
1302 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1303 io_apic_print_entries(ioapic_idx, reg_01.bits.entries);
1304}
1305
1306void __init print_IO_APICs(void)
1307{
1308 int ioapic_idx;
1309 unsigned int irq;
1310
1311 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1312 for_each_ioapic(ioapic_idx)
1313 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1314 mpc_ioapic_id(ioapic_idx),
1315 ioapics[ioapic_idx].nr_registers);
1316
1317
1318
1319
1320
1321 printk(KERN_INFO "testing the IO APIC.......................\n");
1322
1323 for_each_ioapic(ioapic_idx)
1324 print_IO_APIC(ioapic_idx);
1325
1326 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1327 for_each_active_irq(irq) {
1328 struct irq_pin_list *entry;
1329 struct irq_chip *chip;
1330 struct mp_chip_data *data;
1331
1332 chip = irq_get_chip(irq);
1333 if (chip != &ioapic_chip && chip != &ioapic_ir_chip)
1334 continue;
1335 data = irq_get_chip_data(irq);
1336 if (!data)
1337 continue;
1338 if (list_empty(&data->irq_2_pin))
1339 continue;
1340
1341 printk(KERN_DEBUG "IRQ%d ", irq);
1342 for_each_irq_pin(entry, data->irq_2_pin)
1343 pr_cont("-> %d:%d", entry->apic, entry->pin);
1344 pr_cont("\n");
1345 }
1346
1347 printk(KERN_INFO ".................................... done.\n");
1348}
1349
1350
1351static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1352
1353void __init enable_IO_APIC(void)
1354{
1355 int i8259_apic, i8259_pin;
1356 int apic, pin;
1357
1358 if (skip_ioapic_setup)
1359 nr_ioapics = 0;
1360
1361 if (!nr_legacy_irqs() || !nr_ioapics)
1362 return;
1363
1364 for_each_ioapic_pin(apic, pin) {
1365
1366 struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
1367
1368
1369
1370
1371 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1372 ioapic_i8259.apic = apic;
1373 ioapic_i8259.pin = pin;
1374 goto found_i8259;
1375 }
1376 }
1377 found_i8259:
1378
1379
1380
1381
1382
1383 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1384 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1385
1386 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1387 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1388 ioapic_i8259.pin = i8259_pin;
1389 ioapic_i8259.apic = i8259_apic;
1390 }
1391
1392 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1393 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1394 {
1395 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1396 }
1397
1398
1399
1400
1401 clear_IO_APIC();
1402}
1403
1404void native_restore_boot_irq_mode(void)
1405{
1406
1407
1408
1409
1410
1411 if (ioapic_i8259.pin != -1) {
1412 struct IO_APIC_route_entry entry;
1413
1414 memset(&entry, 0, sizeof(entry));
1415 entry.mask = IOAPIC_UNMASKED;
1416 entry.trigger = IOAPIC_EDGE;
1417 entry.polarity = IOAPIC_POL_HIGH;
1418 entry.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
1419 entry.delivery_mode = dest_ExtINT;
1420 entry.dest = read_apic_id();
1421
1422
1423
1424
1425 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1426 }
1427
1428 if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
1429 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1430}
1431
1432void restore_boot_irq_mode(void)
1433{
1434 if (!nr_legacy_irqs())
1435 return;
1436
1437 x86_apic_ops.restore();
1438}
1439
1440#ifdef CONFIG_X86_32
1441
1442
1443
1444
1445
1446
1447void __init setup_ioapic_ids_from_mpc_nocheck(void)
1448{
1449 union IO_APIC_reg_00 reg_00;
1450 physid_mask_t phys_id_present_map;
1451 int ioapic_idx;
1452 int i;
1453 unsigned char old_id;
1454 unsigned long flags;
1455
1456
1457
1458
1459
1460 apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
1461
1462
1463
1464
1465 for_each_ioapic(ioapic_idx) {
1466
1467 raw_spin_lock_irqsave(&ioapic_lock, flags);
1468 reg_00.raw = io_apic_read(ioapic_idx, 0);
1469 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1470
1471 old_id = mpc_ioapic_id(ioapic_idx);
1472
1473 if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
1474 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1475 ioapic_idx, mpc_ioapic_id(ioapic_idx));
1476 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1477 reg_00.bits.ID);
1478 ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
1479 }
1480
1481
1482
1483
1484
1485
1486 if (apic->check_apicid_used(&phys_id_present_map,
1487 mpc_ioapic_id(ioapic_idx))) {
1488 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1489 ioapic_idx, mpc_ioapic_id(ioapic_idx));
1490 for (i = 0; i < get_physical_broadcast(); i++)
1491 if (!physid_isset(i, phys_id_present_map))
1492 break;
1493 if (i >= get_physical_broadcast())
1494 panic("Max APIC ID exceeded!\n");
1495 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1496 i);
1497 physid_set(i, phys_id_present_map);
1498 ioapics[ioapic_idx].mp_config.apicid = i;
1499 } else {
1500 physid_mask_t tmp;
1501 apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
1502 &tmp);
1503 apic_printk(APIC_VERBOSE, "Setting %d in the "
1504 "phys_id_present_map\n",
1505 mpc_ioapic_id(ioapic_idx));
1506 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1507 }
1508
1509
1510
1511
1512
1513 if (old_id != mpc_ioapic_id(ioapic_idx))
1514 for (i = 0; i < mp_irq_entries; i++)
1515 if (mp_irqs[i].dstapic == old_id)
1516 mp_irqs[i].dstapic
1517 = mpc_ioapic_id(ioapic_idx);
1518
1519
1520
1521
1522
1523 if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
1524 continue;
1525
1526 apic_printk(APIC_VERBOSE, KERN_INFO
1527 "...changing IO-APIC physical APIC ID to %d ...",
1528 mpc_ioapic_id(ioapic_idx));
1529
1530 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
1531 raw_spin_lock_irqsave(&ioapic_lock, flags);
1532 io_apic_write(ioapic_idx, 0, reg_00.raw);
1533 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1534
1535
1536
1537
1538 raw_spin_lock_irqsave(&ioapic_lock, flags);
1539 reg_00.raw = io_apic_read(ioapic_idx, 0);
1540 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1541 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
1542 pr_cont("could not set ID!\n");
1543 else
1544 apic_printk(APIC_VERBOSE, " ok.\n");
1545 }
1546}
1547
1548void __init setup_ioapic_ids_from_mpc(void)
1549{
1550
1551 if (acpi_ioapic)
1552 return;
1553
1554
1555
1556
1557 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1558 || APIC_XAPIC(boot_cpu_apic_version))
1559 return;
1560 setup_ioapic_ids_from_mpc_nocheck();
1561}
1562#endif
1563
1564int no_timer_check __initdata;
1565
1566static int __init notimercheck(char *s)
1567{
1568 no_timer_check = 1;
1569 return 1;
1570}
1571__setup("no_timer_check", notimercheck);
1572
1573static void __init delay_with_tsc(void)
1574{
1575 unsigned long long start, now;
1576 unsigned long end = jiffies + 4;
1577
1578 start = rdtsc();
1579
1580
1581
1582
1583
1584
1585
1586 do {
1587 rep_nop();
1588 now = rdtsc();
1589 } while ((now - start) < 40000000000ULL / HZ &&
1590 time_before_eq(jiffies, end));
1591}
1592
1593static void __init delay_without_tsc(void)
1594{
1595 unsigned long end = jiffies + 4;
1596 int band = 1;
1597
1598
1599
1600
1601
1602
1603
1604
1605 do {
1606 __delay(((1U << band++) * 10000000UL) / HZ);
1607 } while (band < 12 && time_before_eq(jiffies, end));
1608}
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618static int __init timer_irq_works(void)
1619{
1620 unsigned long t1 = jiffies;
1621 unsigned long flags;
1622
1623 if (no_timer_check)
1624 return 1;
1625
1626 local_save_flags(flags);
1627 local_irq_enable();
1628
1629 if (boot_cpu_has(X86_FEATURE_TSC))
1630 delay_with_tsc();
1631 else
1632 delay_without_tsc();
1633
1634 local_irq_restore(flags);
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645 if (time_after(jiffies, t1 + 4))
1646 return 1;
1647 return 0;
1648}
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672static unsigned int startup_ioapic_irq(struct irq_data *data)
1673{
1674 int was_pending = 0, irq = data->irq;
1675 unsigned long flags;
1676
1677 raw_spin_lock_irqsave(&ioapic_lock, flags);
1678 if (irq < nr_legacy_irqs()) {
1679 legacy_pic->mask(irq);
1680 if (legacy_pic->irq_pending(irq))
1681 was_pending = 1;
1682 }
1683 __unmask_ioapic(data->chip_data);
1684 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1685
1686 return was_pending;
1687}
1688
1689atomic_t irq_mis_count;
1690
1691#ifdef CONFIG_GENERIC_PENDING_IRQ
1692static bool io_apic_level_ack_pending(struct mp_chip_data *data)
1693{
1694 struct irq_pin_list *entry;
1695 unsigned long flags;
1696
1697 raw_spin_lock_irqsave(&ioapic_lock, flags);
1698 for_each_irq_pin(entry, data->irq_2_pin) {
1699 unsigned int reg;
1700 int pin;
1701
1702 pin = entry->pin;
1703 reg = io_apic_read(entry->apic, 0x10 + pin*2);
1704
1705 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
1706 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1707 return true;
1708 }
1709 }
1710 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1711
1712 return false;
1713}
1714
1715static inline bool ioapic_prepare_move(struct irq_data *data)
1716{
1717
1718 if (unlikely(irqd_is_setaffinity_pending(data))) {
1719 if (!irqd_irq_masked(data))
1720 mask_ioapic_irq(data);
1721 return true;
1722 }
1723 return false;
1724}
1725
1726static inline void ioapic_finish_move(struct irq_data *data, bool moveit)
1727{
1728 if (unlikely(moveit)) {
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755 if (!io_apic_level_ack_pending(data->chip_data))
1756 irq_move_masked_irq(data);
1757
1758 if (!irqd_irq_masked(data))
1759 unmask_ioapic_irq(data);
1760 }
1761}
1762#else
1763static inline bool ioapic_prepare_move(struct irq_data *data)
1764{
1765 return false;
1766}
1767static inline void ioapic_finish_move(struct irq_data *data, bool moveit)
1768{
1769}
1770#endif
1771
1772static void ioapic_ack_level(struct irq_data *irq_data)
1773{
1774 struct irq_cfg *cfg = irqd_cfg(irq_data);
1775 unsigned long v;
1776 bool moveit;
1777 int i;
1778
1779 irq_complete_move(cfg);
1780 moveit = ioapic_prepare_move(irq_data);
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814 i = cfg->vector;
1815 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1816
1817
1818
1819
1820
1821 ack_APIC_irq();
1822
1823
1824
1825
1826
1827
1828
1829
1830 if (!(v & (1 << (i & 0x1f)))) {
1831 atomic_inc(&irq_mis_count);
1832 eoi_ioapic_pin(cfg->vector, irq_data->chip_data);
1833 }
1834
1835 ioapic_finish_move(irq_data, moveit);
1836}
1837
1838static void ioapic_ir_ack_level(struct irq_data *irq_data)
1839{
1840 struct mp_chip_data *data = irq_data->chip_data;
1841
1842
1843
1844
1845
1846
1847
1848 apic_ack_irq(irq_data);
1849 eoi_ioapic_pin(data->entry.vector, data);
1850}
1851
1852static void ioapic_configure_entry(struct irq_data *irqd)
1853{
1854 struct mp_chip_data *mpd = irqd->chip_data;
1855 struct irq_cfg *cfg = irqd_cfg(irqd);
1856 struct irq_pin_list *entry;
1857
1858
1859
1860
1861
1862
1863 if (irqd->chip == &ioapic_chip) {
1864 mpd->entry.dest = cfg->dest_apicid;
1865 mpd->entry.vector = cfg->vector;
1866 }
1867 for_each_irq_pin(entry, mpd->irq_2_pin)
1868 __ioapic_write_entry(entry->apic, entry->pin, mpd->entry);
1869}
1870
1871static int ioapic_set_affinity(struct irq_data *irq_data,
1872 const struct cpumask *mask, bool force)
1873{
1874 struct irq_data *parent = irq_data->parent_data;
1875 unsigned long flags;
1876 int ret;
1877
1878 ret = parent->chip->irq_set_affinity(parent, mask, force);
1879 raw_spin_lock_irqsave(&ioapic_lock, flags);
1880 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE)
1881 ioapic_configure_entry(irq_data);
1882 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1883
1884 return ret;
1885}
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901static int ioapic_irq_get_chip_state(struct irq_data *irqd,
1902 enum irqchip_irq_state which,
1903 bool *state)
1904{
1905 struct mp_chip_data *mcd = irqd->chip_data;
1906 struct IO_APIC_route_entry rentry;
1907 struct irq_pin_list *p;
1908
1909 if (which != IRQCHIP_STATE_ACTIVE)
1910 return -EINVAL;
1911
1912 *state = false;
1913 raw_spin_lock(&ioapic_lock);
1914 for_each_irq_pin(p, mcd->irq_2_pin) {
1915 rentry = __ioapic_read_entry(p->apic, p->pin);
1916
1917
1918
1919
1920
1921
1922 if (rentry.irr && rentry.trigger) {
1923 *state = true;
1924 break;
1925 }
1926 }
1927 raw_spin_unlock(&ioapic_lock);
1928 return 0;
1929}
1930
1931static struct irq_chip ioapic_chip __read_mostly = {
1932 .name = "IO-APIC",
1933 .irq_startup = startup_ioapic_irq,
1934 .irq_mask = mask_ioapic_irq,
1935 .irq_unmask = unmask_ioapic_irq,
1936 .irq_ack = irq_chip_ack_parent,
1937 .irq_eoi = ioapic_ack_level,
1938 .irq_set_affinity = ioapic_set_affinity,
1939 .irq_retrigger = irq_chip_retrigger_hierarchy,
1940 .irq_get_irqchip_state = ioapic_irq_get_chip_state,
1941 .flags = IRQCHIP_SKIP_SET_WAKE,
1942};
1943
1944static struct irq_chip ioapic_ir_chip __read_mostly = {
1945 .name = "IR-IO-APIC",
1946 .irq_startup = startup_ioapic_irq,
1947 .irq_mask = mask_ioapic_irq,
1948 .irq_unmask = unmask_ioapic_irq,
1949 .irq_ack = irq_chip_ack_parent,
1950 .irq_eoi = ioapic_ir_ack_level,
1951 .irq_set_affinity = ioapic_set_affinity,
1952 .irq_retrigger = irq_chip_retrigger_hierarchy,
1953 .irq_get_irqchip_state = ioapic_irq_get_chip_state,
1954 .flags = IRQCHIP_SKIP_SET_WAKE,
1955};
1956
1957static inline void init_IO_APIC_traps(void)
1958{
1959 struct irq_cfg *cfg;
1960 unsigned int irq;
1961
1962 for_each_active_irq(irq) {
1963 cfg = irq_cfg(irq);
1964 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
1965
1966
1967
1968
1969
1970 if (irq < nr_legacy_irqs())
1971 legacy_pic->make_irq(irq);
1972 else
1973
1974 irq_set_chip(irq, &no_irq_chip);
1975 }
1976 }
1977}
1978
1979
1980
1981
1982
1983static void mask_lapic_irq(struct irq_data *data)
1984{
1985 unsigned long v;
1986
1987 v = apic_read(APIC_LVT0);
1988 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1989}
1990
1991static void unmask_lapic_irq(struct irq_data *data)
1992{
1993 unsigned long v;
1994
1995 v = apic_read(APIC_LVT0);
1996 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1997}
1998
1999static void ack_lapic_irq(struct irq_data *data)
2000{
2001 ack_APIC_irq();
2002}
2003
2004static struct irq_chip lapic_chip __read_mostly = {
2005 .name = "local-APIC",
2006 .irq_mask = mask_lapic_irq,
2007 .irq_unmask = unmask_lapic_irq,
2008 .irq_ack = ack_lapic_irq,
2009};
2010
2011static void lapic_register_intr(int irq)
2012{
2013 irq_clear_status_flags(irq, IRQ_LEVEL);
2014 irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2015 "edge");
2016}
2017
2018
2019
2020
2021
2022
2023
2024
2025static inline void __init unlock_ExtINT_logic(void)
2026{
2027 int apic, pin, i;
2028 struct IO_APIC_route_entry entry0, entry1;
2029 unsigned char save_control, save_freq_select;
2030
2031 pin = find_isa_irq_pin(8, mp_INT);
2032 if (pin == -1) {
2033 WARN_ON_ONCE(1);
2034 return;
2035 }
2036 apic = find_isa_irq_apic(8, mp_INT);
2037 if (apic == -1) {
2038 WARN_ON_ONCE(1);
2039 return;
2040 }
2041
2042 entry0 = ioapic_read_entry(apic, pin);
2043 clear_IO_APIC_pin(apic, pin);
2044
2045 memset(&entry1, 0, sizeof(entry1));
2046
2047 entry1.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
2048 entry1.mask = IOAPIC_UNMASKED;
2049 entry1.dest = hard_smp_processor_id();
2050 entry1.delivery_mode = dest_ExtINT;
2051 entry1.polarity = entry0.polarity;
2052 entry1.trigger = IOAPIC_EDGE;
2053 entry1.vector = 0;
2054
2055 ioapic_write_entry(apic, pin, entry1);
2056
2057 save_control = CMOS_READ(RTC_CONTROL);
2058 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2059 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2060 RTC_FREQ_SELECT);
2061 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2062
2063 i = 100;
2064 while (i-- > 0) {
2065 mdelay(10);
2066 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2067 i -= 10;
2068 }
2069
2070 CMOS_WRITE(save_control, RTC_CONTROL);
2071 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2072 clear_IO_APIC_pin(apic, pin);
2073
2074 ioapic_write_entry(apic, pin, entry0);
2075}
2076
2077static int disable_timer_pin_1 __initdata;
2078
2079static int __init disable_timer_pin_setup(char *arg)
2080{
2081 disable_timer_pin_1 = 1;
2082 return 0;
2083}
2084early_param("disable_timer_pin_1", disable_timer_pin_setup);
2085
2086static int mp_alloc_timer_irq(int ioapic, int pin)
2087{
2088 int irq = -1;
2089 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
2090
2091 if (domain) {
2092 struct irq_alloc_info info;
2093
2094 ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 0, 0);
2095 info.ioapic_id = mpc_ioapic_id(ioapic);
2096 info.ioapic_pin = pin;
2097 mutex_lock(&ioapic_mutex);
2098 irq = alloc_isa_irq_from_domain(domain, 0, ioapic, pin, &info);
2099 mutex_unlock(&ioapic_mutex);
2100 }
2101
2102 return irq;
2103}
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113static inline void __init check_timer(void)
2114{
2115 struct irq_data *irq_data = irq_get_irq_data(0);
2116 struct mp_chip_data *data = irq_data->chip_data;
2117 struct irq_cfg *cfg = irqd_cfg(irq_data);
2118 int node = cpu_to_node(0);
2119 int apic1, pin1, apic2, pin2;
2120 unsigned long flags;
2121 int no_pin1 = 0;
2122
2123 if (!global_clock_event)
2124 return;
2125
2126 local_irq_save(flags);
2127
2128
2129
2130
2131 legacy_pic->mask(0);
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2143 legacy_pic->init(1);
2144
2145 pin1 = find_isa_irq_pin(0, mp_INT);
2146 apic1 = find_isa_irq_apic(0, mp_INT);
2147 pin2 = ioapic_i8259.pin;
2148 apic2 = ioapic_i8259.apic;
2149
2150 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2151 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2152 cfg->vector, apic1, pin1, apic2, pin2);
2153
2154
2155
2156
2157
2158
2159
2160
2161 if (pin1 == -1) {
2162 panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
2163 pin1 = pin2;
2164 apic1 = apic2;
2165 no_pin1 = 1;
2166 } else if (pin2 == -1) {
2167 pin2 = pin1;
2168 apic2 = apic1;
2169 }
2170
2171 if (pin1 != -1) {
2172
2173 if (no_pin1) {
2174 mp_alloc_timer_irq(apic1, pin1);
2175 } else {
2176
2177
2178
2179
2180
2181 int idx;
2182 idx = find_irq_entry(apic1, pin1, mp_INT);
2183 if (idx != -1 && irq_trigger(idx))
2184 unmask_ioapic_irq(irq_get_irq_data(0));
2185 }
2186 irq_domain_deactivate_irq(irq_data);
2187 irq_domain_activate_irq(irq_data, false);
2188 if (timer_irq_works()) {
2189 if (disable_timer_pin_1 > 0)
2190 clear_IO_APIC_pin(0, pin1);
2191 goto out;
2192 }
2193 panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
2194 local_irq_disable();
2195 clear_IO_APIC_pin(apic1, pin1);
2196 if (!no_pin1)
2197 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2198 "8254 timer not connected to IO-APIC\n");
2199
2200 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2201 "(IRQ0) through the 8259A ...\n");
2202 apic_printk(APIC_QUIET, KERN_INFO
2203 "..... (found apic %d pin %d) ...\n", apic2, pin2);
2204
2205
2206
2207 replace_pin_at_irq_node(data, node, apic1, pin1, apic2, pin2);
2208 irq_domain_deactivate_irq(irq_data);
2209 irq_domain_activate_irq(irq_data, false);
2210 legacy_pic->unmask(0);
2211 if (timer_irq_works()) {
2212 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2213 goto out;
2214 }
2215
2216
2217
2218 local_irq_disable();
2219 legacy_pic->mask(0);
2220 clear_IO_APIC_pin(apic2, pin2);
2221 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2222 }
2223
2224 apic_printk(APIC_QUIET, KERN_INFO
2225 "...trying to set up timer as Virtual Wire IRQ...\n");
2226
2227 lapic_register_intr(0);
2228 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);
2229 legacy_pic->unmask(0);
2230
2231 if (timer_irq_works()) {
2232 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2233 goto out;
2234 }
2235 local_irq_disable();
2236 legacy_pic->mask(0);
2237 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2238 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2239
2240 apic_printk(APIC_QUIET, KERN_INFO
2241 "...trying to set up timer as ExtINT IRQ...\n");
2242
2243 legacy_pic->init(0);
2244 legacy_pic->make_irq(0);
2245 apic_write(APIC_LVT0, APIC_DM_EXTINT);
2246
2247 unlock_ExtINT_logic();
2248
2249 if (timer_irq_works()) {
2250 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2251 goto out;
2252 }
2253 local_irq_disable();
2254 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2255 if (apic_is_x2apic_enabled())
2256 apic_printk(APIC_QUIET, KERN_INFO
2257 "Perhaps problem with the pre-enabled x2apic mode\n"
2258 "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
2259 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2260 "report. Then try booting with the 'noapic' option.\n");
2261out:
2262 local_irq_restore(flags);
2263}
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282#define PIC_IRQS (1UL << PIC_CASCADE_IR)
2283
2284static int mp_irqdomain_create(int ioapic)
2285{
2286 struct irq_alloc_info info;
2287 struct irq_domain *parent;
2288 int hwirqs = mp_ioapic_pin_count(ioapic);
2289 struct ioapic *ip = &ioapics[ioapic];
2290 struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
2291 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2292 struct fwnode_handle *fn;
2293 char *name = "IO-APIC";
2294
2295 if (cfg->type == IOAPIC_DOMAIN_INVALID)
2296 return 0;
2297
2298 init_irq_alloc_info(&info, NULL);
2299 info.type = X86_IRQ_ALLOC_TYPE_IOAPIC;
2300 info.ioapic_id = mpc_ioapic_id(ioapic);
2301 parent = irq_remapping_get_ir_irq_domain(&info);
2302 if (!parent)
2303 parent = x86_vector_domain;
2304 else
2305 name = "IO-APIC-IR";
2306
2307
2308 if (cfg->dev) {
2309 fn = of_node_to_fwnode(cfg->dev);
2310 } else {
2311 fn = irq_domain_alloc_named_id_fwnode(name, ioapic);
2312 if (!fn)
2313 return -ENOMEM;
2314 }
2315
2316 ip->irqdomain = irq_domain_create_linear(fn, hwirqs, cfg->ops,
2317 (void *)(long)ioapic);
2318
2319 if (!ip->irqdomain) {
2320
2321 if (!cfg->dev)
2322 irq_domain_free_fwnode(fn);
2323 return -ENOMEM;
2324 }
2325
2326 ip->irqdomain->parent = parent;
2327
2328 if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
2329 cfg->type == IOAPIC_DOMAIN_STRICT)
2330 ioapic_dynirq_base = max(ioapic_dynirq_base,
2331 gsi_cfg->gsi_end + 1);
2332
2333 return 0;
2334}
2335
2336static void ioapic_destroy_irqdomain(int idx)
2337{
2338 if (ioapics[idx].irqdomain) {
2339 irq_domain_remove(ioapics[idx].irqdomain);
2340 ioapics[idx].irqdomain = NULL;
2341 }
2342}
2343
2344void __init setup_IO_APIC(void)
2345{
2346 int ioapic;
2347
2348 if (skip_ioapic_setup || !nr_ioapics)
2349 return;
2350
2351 io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
2352
2353 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2354 for_each_ioapic(ioapic)
2355 BUG_ON(mp_irqdomain_create(ioapic));
2356
2357
2358
2359
2360 x86_init.mpparse.setup_ioapic_ids();
2361
2362 sync_Arb_IDs();
2363 setup_IO_APIC_irqs();
2364 init_IO_APIC_traps();
2365 if (nr_legacy_irqs())
2366 check_timer();
2367
2368 ioapic_initialized = 1;
2369}
2370
2371static void resume_ioapic_id(int ioapic_idx)
2372{
2373 unsigned long flags;
2374 union IO_APIC_reg_00 reg_00;
2375
2376 raw_spin_lock_irqsave(&ioapic_lock, flags);
2377 reg_00.raw = io_apic_read(ioapic_idx, 0);
2378 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
2379 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
2380 io_apic_write(ioapic_idx, 0, reg_00.raw);
2381 }
2382 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2383}
2384
2385static void ioapic_resume(void)
2386{
2387 int ioapic_idx;
2388
2389 for_each_ioapic_reverse(ioapic_idx)
2390 resume_ioapic_id(ioapic_idx);
2391
2392 restore_ioapic_entries();
2393}
2394
2395static struct syscore_ops ioapic_syscore_ops = {
2396 .suspend = save_ioapic_entries,
2397 .resume = ioapic_resume,
2398};
2399
2400static int __init ioapic_init_ops(void)
2401{
2402 register_syscore_ops(&ioapic_syscore_ops);
2403
2404 return 0;
2405}
2406
2407device_initcall(ioapic_init_ops);
2408
2409static int io_apic_get_redir_entries(int ioapic)
2410{
2411 union IO_APIC_reg_01 reg_01;
2412 unsigned long flags;
2413
2414 raw_spin_lock_irqsave(&ioapic_lock, flags);
2415 reg_01.raw = io_apic_read(ioapic, 1);
2416 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2417
2418
2419
2420
2421
2422 return reg_01.bits.entries + 1;
2423}
2424
2425unsigned int arch_dynirq_lower_bound(unsigned int from)
2426{
2427
2428
2429
2430
2431 if (!ioapic_initialized)
2432 return gsi_top;
2433
2434
2435
2436
2437 return ioapic_dynirq_base ? : from;
2438}
2439
2440#ifdef CONFIG_X86_32
2441static int io_apic_get_unique_id(int ioapic, int apic_id)
2442{
2443 union IO_APIC_reg_00 reg_00;
2444 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2445 physid_mask_t tmp;
2446 unsigned long flags;
2447 int i = 0;
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458 if (physids_empty(apic_id_map))
2459 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
2460
2461 raw_spin_lock_irqsave(&ioapic_lock, flags);
2462 reg_00.raw = io_apic_read(ioapic, 0);
2463 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2464
2465 if (apic_id >= get_physical_broadcast()) {
2466 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2467 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2468 apic_id = reg_00.bits.ID;
2469 }
2470
2471
2472
2473
2474
2475 if (apic->check_apicid_used(&apic_id_map, apic_id)) {
2476
2477 for (i = 0; i < get_physical_broadcast(); i++) {
2478 if (!apic->check_apicid_used(&apic_id_map, i))
2479 break;
2480 }
2481
2482 if (i == get_physical_broadcast())
2483 panic("Max apic_id exceeded!\n");
2484
2485 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2486 "trying %d\n", ioapic, apic_id, i);
2487
2488 apic_id = i;
2489 }
2490
2491 apic->apicid_to_cpu_present(apic_id, &tmp);
2492 physids_or(apic_id_map, apic_id_map, tmp);
2493
2494 if (reg_00.bits.ID != apic_id) {
2495 reg_00.bits.ID = apic_id;
2496
2497 raw_spin_lock_irqsave(&ioapic_lock, flags);
2498 io_apic_write(ioapic, 0, reg_00.raw);
2499 reg_00.raw = io_apic_read(ioapic, 0);
2500 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2501
2502
2503 if (reg_00.bits.ID != apic_id) {
2504 pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
2505 ioapic);
2506 return -1;
2507 }
2508 }
2509
2510 apic_printk(APIC_VERBOSE, KERN_INFO
2511 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2512
2513 return apic_id;
2514}
2515
2516static u8 io_apic_unique_id(int idx, u8 id)
2517{
2518 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
2519 !APIC_XAPIC(boot_cpu_apic_version))
2520 return io_apic_get_unique_id(idx, id);
2521 else
2522 return id;
2523}
2524#else
2525static u8 io_apic_unique_id(int idx, u8 id)
2526{
2527 union IO_APIC_reg_00 reg_00;
2528 DECLARE_BITMAP(used, 256);
2529 unsigned long flags;
2530 u8 new_id;
2531 int i;
2532
2533 bitmap_zero(used, 256);
2534 for_each_ioapic(i)
2535 __set_bit(mpc_ioapic_id(i), used);
2536
2537
2538 if (!test_bit(id, used))
2539 return id;
2540
2541
2542
2543
2544
2545 raw_spin_lock_irqsave(&ioapic_lock, flags);
2546 reg_00.raw = io_apic_read(idx, 0);
2547 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2548 new_id = reg_00.bits.ID;
2549 if (!test_bit(new_id, used)) {
2550 apic_printk(APIC_VERBOSE, KERN_INFO
2551 "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
2552 idx, new_id, id);
2553 return new_id;
2554 }
2555
2556
2557
2558
2559 new_id = find_first_zero_bit(used, 256);
2560 reg_00.bits.ID = new_id;
2561 raw_spin_lock_irqsave(&ioapic_lock, flags);
2562 io_apic_write(idx, 0, reg_00.raw);
2563 reg_00.raw = io_apic_read(idx, 0);
2564 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2565
2566 BUG_ON(reg_00.bits.ID != new_id);
2567
2568 return new_id;
2569}
2570#endif
2571
2572static int io_apic_get_version(int ioapic)
2573{
2574 union IO_APIC_reg_01 reg_01;
2575 unsigned long flags;
2576
2577 raw_spin_lock_irqsave(&ioapic_lock, flags);
2578 reg_01.raw = io_apic_read(ioapic, 1);
2579 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2580
2581 return reg_01.bits.version;
2582}
2583
2584int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
2585{
2586 int ioapic, pin, idx;
2587
2588 if (skip_ioapic_setup)
2589 return -1;
2590
2591 ioapic = mp_find_ioapic(gsi);
2592 if (ioapic < 0)
2593 return -1;
2594
2595 pin = mp_find_ioapic_pin(ioapic, gsi);
2596 if (pin < 0)
2597 return -1;
2598
2599 idx = find_irq_entry(ioapic, pin, mp_INT);
2600 if (idx < 0)
2601 return -1;
2602
2603 *trigger = irq_trigger(idx);
2604 *polarity = irq_polarity(idx);
2605 return 0;
2606}
2607
2608
2609
2610
2611
2612#define IOAPIC_RESOURCE_NAME_SIZE 11
2613
2614static struct resource *ioapic_resources;
2615
2616static struct resource * __init ioapic_setup_resources(void)
2617{
2618 unsigned long n;
2619 struct resource *res;
2620 char *mem;
2621 int i;
2622
2623 if (nr_ioapics == 0)
2624 return NULL;
2625
2626 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
2627 n *= nr_ioapics;
2628
2629 mem = memblock_alloc(n, SMP_CACHE_BYTES);
2630 if (!mem)
2631 panic("%s: Failed to allocate %lu bytes\n", __func__, n);
2632 res = (void *)mem;
2633
2634 mem += sizeof(struct resource) * nr_ioapics;
2635
2636 for_each_ioapic(i) {
2637 res[i].name = mem;
2638 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
2639 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
2640 mem += IOAPIC_RESOURCE_NAME_SIZE;
2641 ioapics[i].iomem_res = &res[i];
2642 }
2643
2644 ioapic_resources = res;
2645
2646 return res;
2647}
2648
2649void __init io_apic_init_mappings(void)
2650{
2651 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2652 struct resource *ioapic_res;
2653 int i;
2654
2655 ioapic_res = ioapic_setup_resources();
2656 for_each_ioapic(i) {
2657 if (smp_found_config) {
2658 ioapic_phys = mpc_ioapic_addr(i);
2659#ifdef CONFIG_X86_32
2660 if (!ioapic_phys) {
2661 printk(KERN_ERR
2662 "WARNING: bogus zero IO-APIC "
2663 "address found in MPTABLE, "
2664 "disabling IO/APIC support!\n");
2665 smp_found_config = 0;
2666 skip_ioapic_setup = 1;
2667 goto fake_ioapic_page;
2668 }
2669#endif
2670 } else {
2671#ifdef CONFIG_X86_32
2672fake_ioapic_page:
2673#endif
2674 ioapic_phys = (unsigned long)memblock_alloc(PAGE_SIZE,
2675 PAGE_SIZE);
2676 if (!ioapic_phys)
2677 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
2678 __func__, PAGE_SIZE, PAGE_SIZE);
2679 ioapic_phys = __pa(ioapic_phys);
2680 }
2681 set_fixmap_nocache(idx, ioapic_phys);
2682 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
2683 __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
2684 ioapic_phys);
2685 idx++;
2686
2687 ioapic_res->start = ioapic_phys;
2688 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
2689 ioapic_res++;
2690 }
2691}
2692
2693void __init ioapic_insert_resources(void)
2694{
2695 int i;
2696 struct resource *r = ioapic_resources;
2697
2698 if (!r) {
2699 if (nr_ioapics > 0)
2700 printk(KERN_ERR
2701 "IO APIC resources couldn't be allocated.\n");
2702 return;
2703 }
2704
2705 for_each_ioapic(i) {
2706 insert_resource(&iomem_resource, r);
2707 r++;
2708 }
2709}
2710
2711int mp_find_ioapic(u32 gsi)
2712{
2713 int i;
2714
2715 if (nr_ioapics == 0)
2716 return -1;
2717
2718
2719 for_each_ioapic(i) {
2720 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
2721 if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
2722 return i;
2723 }
2724
2725 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
2726 return -1;
2727}
2728
2729int mp_find_ioapic_pin(int ioapic, u32 gsi)
2730{
2731 struct mp_ioapic_gsi *gsi_cfg;
2732
2733 if (WARN_ON(ioapic < 0))
2734 return -1;
2735
2736 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2737 if (WARN_ON(gsi > gsi_cfg->gsi_end))
2738 return -1;
2739
2740 return gsi - gsi_cfg->gsi_base;
2741}
2742
2743static int bad_ioapic_register(int idx)
2744{
2745 union IO_APIC_reg_00 reg_00;
2746 union IO_APIC_reg_01 reg_01;
2747 union IO_APIC_reg_02 reg_02;
2748
2749 reg_00.raw = io_apic_read(idx, 0);
2750 reg_01.raw = io_apic_read(idx, 1);
2751 reg_02.raw = io_apic_read(idx, 2);
2752
2753 if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
2754 pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
2755 mpc_ioapic_addr(idx));
2756 return 1;
2757 }
2758
2759 return 0;
2760}
2761
2762static int find_free_ioapic_entry(void)
2763{
2764 int idx;
2765
2766 for (idx = 0; idx < MAX_IO_APICS; idx++)
2767 if (ioapics[idx].nr_registers == 0)
2768 return idx;
2769
2770 return MAX_IO_APICS;
2771}
2772
2773
2774
2775
2776
2777
2778
2779
2780int mp_register_ioapic(int id, u32 address, u32 gsi_base,
2781 struct ioapic_domain_cfg *cfg)
2782{
2783 bool hotplug = !!ioapic_initialized;
2784 struct mp_ioapic_gsi *gsi_cfg;
2785 int idx, ioapic, entries;
2786 u32 gsi_end;
2787
2788 if (!address) {
2789 pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
2790 return -EINVAL;
2791 }
2792 for_each_ioapic(ioapic)
2793 if (ioapics[ioapic].mp_config.apicaddr == address) {
2794 pr_warn("address 0x%x conflicts with IOAPIC%d\n",
2795 address, ioapic);
2796 return -EEXIST;
2797 }
2798
2799 idx = find_free_ioapic_entry();
2800 if (idx >= MAX_IO_APICS) {
2801 pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
2802 MAX_IO_APICS, idx);
2803 return -ENOSPC;
2804 }
2805
2806 ioapics[idx].mp_config.type = MP_IOAPIC;
2807 ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
2808 ioapics[idx].mp_config.apicaddr = address;
2809
2810 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
2811 if (bad_ioapic_register(idx)) {
2812 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2813 return -ENODEV;
2814 }
2815
2816 ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
2817 ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
2818
2819
2820
2821
2822
2823 entries = io_apic_get_redir_entries(idx);
2824 gsi_end = gsi_base + entries - 1;
2825 for_each_ioapic(ioapic) {
2826 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2827 if ((gsi_base >= gsi_cfg->gsi_base &&
2828 gsi_base <= gsi_cfg->gsi_end) ||
2829 (gsi_end >= gsi_cfg->gsi_base &&
2830 gsi_end <= gsi_cfg->gsi_end)) {
2831 pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
2832 gsi_base, gsi_end,
2833 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
2834 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2835 return -ENOSPC;
2836 }
2837 }
2838 gsi_cfg = mp_ioapic_gsi_routing(idx);
2839 gsi_cfg->gsi_base = gsi_base;
2840 gsi_cfg->gsi_end = gsi_end;
2841
2842 ioapics[idx].irqdomain = NULL;
2843 ioapics[idx].irqdomain_cfg = *cfg;
2844
2845
2846
2847
2848
2849
2850 if (hotplug) {
2851 if (mp_irqdomain_create(idx)) {
2852 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2853 return -ENOMEM;
2854 }
2855 alloc_ioapic_saved_registers(idx);
2856 }
2857
2858 if (gsi_cfg->gsi_end >= gsi_top)
2859 gsi_top = gsi_cfg->gsi_end + 1;
2860 if (nr_ioapics <= idx)
2861 nr_ioapics = idx + 1;
2862
2863
2864 ioapics[idx].nr_registers = entries;
2865
2866 pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
2867 idx, mpc_ioapic_id(idx),
2868 mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
2869 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
2870
2871 return 0;
2872}
2873
2874int mp_unregister_ioapic(u32 gsi_base)
2875{
2876 int ioapic, pin;
2877 int found = 0;
2878
2879 for_each_ioapic(ioapic)
2880 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
2881 found = 1;
2882 break;
2883 }
2884 if (!found) {
2885 pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
2886 return -ENODEV;
2887 }
2888
2889 for_each_pin(ioapic, pin) {
2890 u32 gsi = mp_pin_to_gsi(ioapic, pin);
2891 int irq = mp_map_gsi_to_irq(gsi, 0, NULL);
2892 struct mp_chip_data *data;
2893
2894 if (irq >= 0) {
2895 data = irq_get_chip_data(irq);
2896 if (data && data->count) {
2897 pr_warn("pin%d on IOAPIC%d is still in use.\n",
2898 pin, ioapic);
2899 return -EBUSY;
2900 }
2901 }
2902 }
2903
2904
2905 ioapics[ioapic].nr_registers = 0;
2906 ioapic_destroy_irqdomain(ioapic);
2907 free_ioapic_saved_registers(ioapic);
2908 if (ioapics[ioapic].iomem_res)
2909 release_resource(ioapics[ioapic].iomem_res);
2910 clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
2911 memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
2912
2913 return 0;
2914}
2915
2916int mp_ioapic_registered(u32 gsi_base)
2917{
2918 int ioapic;
2919
2920 for_each_ioapic(ioapic)
2921 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
2922 return 1;
2923
2924 return 0;
2925}
2926
2927static void mp_irqdomain_get_attr(u32 gsi, struct mp_chip_data *data,
2928 struct irq_alloc_info *info)
2929{
2930 if (info && info->ioapic_valid) {
2931 data->trigger = info->ioapic_trigger;
2932 data->polarity = info->ioapic_polarity;
2933 } else if (acpi_get_override_irq(gsi, &data->trigger,
2934 &data->polarity) < 0) {
2935
2936 data->trigger = IOAPIC_LEVEL;
2937 data->polarity = IOAPIC_POL_LOW;
2938 }
2939}
2940
2941static void mp_setup_entry(struct irq_cfg *cfg, struct mp_chip_data *data,
2942 struct IO_APIC_route_entry *entry)
2943{
2944 memset(entry, 0, sizeof(*entry));
2945 entry->delivery_mode = apic->irq_delivery_mode;
2946 entry->dest_mode = apic->irq_dest_mode;
2947 entry->dest = cfg->dest_apicid;
2948 entry->vector = cfg->vector;
2949 entry->trigger = data->trigger;
2950 entry->polarity = data->polarity;
2951
2952
2953
2954
2955 if (data->trigger == IOAPIC_LEVEL)
2956 entry->mask = IOAPIC_MASKED;
2957 else
2958 entry->mask = IOAPIC_UNMASKED;
2959}
2960
2961int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2962 unsigned int nr_irqs, void *arg)
2963{
2964 int ret, ioapic, pin;
2965 struct irq_cfg *cfg;
2966 struct irq_data *irq_data;
2967 struct mp_chip_data *data;
2968 struct irq_alloc_info *info = arg;
2969 unsigned long flags;
2970
2971 if (!info || nr_irqs > 1)
2972 return -EINVAL;
2973 irq_data = irq_domain_get_irq_data(domain, virq);
2974 if (!irq_data)
2975 return -EINVAL;
2976
2977 ioapic = mp_irqdomain_ioapic_idx(domain);
2978 pin = info->ioapic_pin;
2979 if (irq_find_mapping(domain, (irq_hw_number_t)pin) > 0)
2980 return -EEXIST;
2981
2982 data = kzalloc(sizeof(*data), GFP_KERNEL);
2983 if (!data)
2984 return -ENOMEM;
2985
2986 info->ioapic_entry = &data->entry;
2987 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info);
2988 if (ret < 0) {
2989 kfree(data);
2990 return ret;
2991 }
2992
2993 INIT_LIST_HEAD(&data->irq_2_pin);
2994 irq_data->hwirq = info->ioapic_pin;
2995 irq_data->chip = (domain->parent == x86_vector_domain) ?
2996 &ioapic_chip : &ioapic_ir_chip;
2997 irq_data->chip_data = data;
2998 mp_irqdomain_get_attr(mp_pin_to_gsi(ioapic, pin), data, info);
2999
3000 cfg = irqd_cfg(irq_data);
3001 add_pin_to_irq_node(data, ioapic_alloc_attr_node(info), ioapic, pin);
3002
3003 local_irq_save(flags);
3004 if (info->ioapic_entry)
3005 mp_setup_entry(cfg, data, info->ioapic_entry);
3006 mp_register_handler(virq, data->trigger);
3007 if (virq < nr_legacy_irqs())
3008 legacy_pic->mask(virq);
3009 local_irq_restore(flags);
3010
3011 apic_printk(APIC_VERBOSE, KERN_DEBUG
3012 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i Dest:%d)\n",
3013 ioapic, mpc_ioapic_id(ioapic), pin, cfg->vector,
3014 virq, data->trigger, data->polarity, cfg->dest_apicid);
3015
3016 return 0;
3017}
3018
3019void mp_irqdomain_free(struct irq_domain *domain, unsigned int virq,
3020 unsigned int nr_irqs)
3021{
3022 struct irq_data *irq_data;
3023 struct mp_chip_data *data;
3024
3025 BUG_ON(nr_irqs != 1);
3026 irq_data = irq_domain_get_irq_data(domain, virq);
3027 if (irq_data && irq_data->chip_data) {
3028 data = irq_data->chip_data;
3029 __remove_pin_from_irq(data, mp_irqdomain_ioapic_idx(domain),
3030 (int)irq_data->hwirq);
3031 WARN_ON(!list_empty(&data->irq_2_pin));
3032 kfree(irq_data->chip_data);
3033 }
3034 irq_domain_free_irqs_top(domain, virq, nr_irqs);
3035}
3036
3037int mp_irqdomain_activate(struct irq_domain *domain,
3038 struct irq_data *irq_data, bool reserve)
3039{
3040 unsigned long flags;
3041
3042 raw_spin_lock_irqsave(&ioapic_lock, flags);
3043 ioapic_configure_entry(irq_data);
3044 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3045 return 0;
3046}
3047
3048void mp_irqdomain_deactivate(struct irq_domain *domain,
3049 struct irq_data *irq_data)
3050{
3051
3052 ioapic_mask_entry(mp_irqdomain_ioapic_idx(domain),
3053 (int)irq_data->hwirq);
3054}
3055
3056int mp_irqdomain_ioapic_idx(struct irq_domain *domain)
3057{
3058 return (int)(long)domain->host_data;
3059}
3060
3061const struct irq_domain_ops mp_ioapic_irqdomain_ops = {
3062 .alloc = mp_irqdomain_alloc,
3063 .free = mp_irqdomain_free,
3064 .activate = mp_irqdomain_activate,
3065 .deactivate = mp_irqdomain_deactivate,
3066};
3067