2
* Intel IO-APIC support for multi-Pentium hosts.
4
* Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6
* Many thanks to Stig Venaas for trying out countless experimental
7
* patches and reporting/debugging problems patiently!
9
* (c) 1999, Multiple IO-APIC support, developed by
10
* Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11
* Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12
* further tested and cleaned up by Zach Brown <zab@redhat.com>
13
* and Ingo Molnar <mingo@redhat.com>
16
* Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17
* thanks to Eric Gilmore
19
* for testing these extensively
20
* Paul Diefenbaugh : Added full ACPI support
23
#include <xen/config.h>
27
#include <xen/delay.h>
28
#include <xen/sched.h>
31
#include <xen/pci_regs.h>
32
#include <xen/keyhandler.h>
33
#include <asm/mc146818rtc.h>
37
#include <mach_apic.h>
39
#include <public/physdev.h>
41
/* Where if anywhere is the i8259 connect in external int mode */
42
static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
44
static DEFINE_SPINLOCK(ioapic_lock);
46
bool_t __read_mostly skip_ioapic_setup;
50
* Is the SiS APIC rmw bug present?
51
* -1 = don't know, 0 = no, 1 = yes
53
int sis_apic_bug = -1;
57
* # of IRQ routing registers
59
int __read_mostly nr_ioapic_registers[MAX_IO_APICS];
60
int __read_mostly nr_ioapics;
63
* Rough estimation of how many shared IRQs there are, can
66
#define MAX_PLUS_SHARED_IRQS nr_irqs_gsi
67
#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + nr_irqs_gsi)
70
#define ioapic_has_eoi_reg(apic) (mp_ioapics[(apic)].mpc_apicver >= 0x20)
72
#define io_apic_eoi_vector(apic, vector) io_apic_eoi((apic), (vector), -1)
73
#define io_apic_eoi_pin(apic, pin) io_apic_eoi((apic), -1, (pin))
77
* This is performance-critical, we want to do it O(1)
79
* the indexing order of this array favors 1:1 mappings
80
* between pins and IRQs.
83
static struct irq_pin_list {
88
static unsigned int irq_2_pin_free_entry;
91
* The common case is 1:1 IRQ<->pin mappings. Sometimes there are
92
* shared ISA-space IRQs, so we have to support them. We are super
93
* fast in the common case, and fast for shared ISA-space IRQs.
95
static void add_pin_to_irq(unsigned int irq, int apic, int pin)
97
struct irq_pin_list *entry = irq_2_pin + irq;
100
BUG_ON((entry->apic == apic) && (entry->pin == pin));
101
entry = irq_2_pin + entry->next;
104
BUG_ON((entry->apic == apic) && (entry->pin == pin));
106
if (entry->pin != -1) {
107
if (irq_2_pin_free_entry >= PIN_MAP_SIZE)
108
panic("io_apic.c: whoops");
109
entry->next = irq_2_pin_free_entry;
110
entry = irq_2_pin + entry->next;
111
irq_2_pin_free_entry = entry->next;
119
* Reroute an IRQ to a different pin.
121
static void __init replace_pin_at_irq(unsigned int irq,
122
int oldapic, int oldpin,
123
int newapic, int newpin)
125
struct irq_pin_list *entry = irq_2_pin + irq;
128
if (entry->apic == oldapic && entry->pin == oldpin) {
129
entry->apic = newapic;
134
entry = irq_2_pin + entry->next;
138
struct IO_APIC_route_entry **alloc_ioapic_entries(void)
141
struct IO_APIC_route_entry **ioapic_entries;
143
ioapic_entries = xmalloc_array(struct IO_APIC_route_entry *, nr_ioapics);
147
for (apic = 0; apic < nr_ioapics; apic++) {
148
ioapic_entries[apic] =
149
xmalloc_array(struct IO_APIC_route_entry,
150
nr_ioapic_registers[apic]);
151
if (!ioapic_entries[apic])
155
return ioapic_entries;
159
xfree(ioapic_entries[apic]);
160
xfree(ioapic_entries);
166
struct { u32 w1, w2; };
167
struct IO_APIC_route_entry entry;
170
static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin, int raw)
172
unsigned int (*read)(unsigned int, unsigned int)
173
= raw ? __io_apic_read : io_apic_read;
174
union entry_union eu;
175
eu.w1 = (*read)(apic, 0x10 + 2 * pin);
176
eu.w2 = (*read)(apic, 0x11 + 2 * pin);
180
static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin, int raw)
182
struct IO_APIC_route_entry entry;
185
spin_lock_irqsave(&ioapic_lock, flags);
186
entry = __ioapic_read_entry(apic, pin, raw);
187
spin_unlock_irqrestore(&ioapic_lock, flags);
192
__ioapic_write_entry(int apic, int pin, int raw, struct IO_APIC_route_entry e)
194
void (*write)(unsigned int, unsigned int, unsigned int)
195
= raw ? __io_apic_write : io_apic_write;
196
union entry_union eu = {{0, 0}};
199
(*write)(apic, 0x11 + 2*pin, eu.w2);
200
(*write)(apic, 0x10 + 2*pin, eu.w1);
203
static void ioapic_write_entry(int apic, int pin, int raw, struct IO_APIC_route_entry e)
206
spin_lock_irqsave(&ioapic_lock, flags);
207
__ioapic_write_entry(apic, pin, raw, e);
208
spin_unlock_irqrestore(&ioapic_lock, flags);
211
/* EOI an IO-APIC entry. One of vector or pin may be -1, indicating that
212
* it should be worked out using the other. This function expect that the
213
* ioapic_lock is taken, and interrupts are disabled (or there is a good reason
214
* not to), and that if both pin and vector are passed, that they refer to the
215
* same redirection entry in the IO-APIC. */
216
static void __io_apic_eoi(unsigned int apic, unsigned int vector, unsigned int pin)
218
/* Ensure some useful information is passed in */
219
BUG_ON( (vector == -1 && pin == -1) );
221
/* Prefer the use of the EOI register if available */
222
if ( ioapic_has_eoi_reg(apic) )
224
/* If vector is unknown, read it from the IO-APIC */
226
vector = __ioapic_read_entry(apic, pin, TRUE).vector;
228
*(IO_APIC_BASE(apic)+16) = vector;
232
/* Else fake an EOI by switching to edge triggered mode
234
struct IO_APIC_route_entry entry;
235
bool_t need_to_unmask = 0;
237
/* If pin is unknown, search for it */
241
for ( p = 0; p < nr_ioapic_registers[apic]; ++p )
243
entry = __ioapic_read_entry(apic, p, TRUE);
244
if ( entry.vector == vector )
249
/* Here should be a break out of the loop, but at the
250
* Xen code doesn't actually prevent multiple IO-APIC
251
* entries being assigned the same vector, so EOI all
252
* pins which have the correct vector.
254
* Remove the following code when the above assertion
256
__io_apic_eoi(apic, vector, p);
260
/* If search fails, nothing to do */
262
/* if ( pin == -1 ) */
264
/* Because the loop wasn't broken out of (see comment above),
265
* all relevant pins have been EOI, so we can always return.
267
* Re-instate the if statement above when the Xen logic has been
273
entry = __ioapic_read_entry(apic, pin, TRUE);
277
/* If entry is not currently masked, mask it and make
278
* a note to unmask it later */
280
__ioapic_write_entry(apic, pin, TRUE, entry);
284
/* Flip the trigger mode to edge and back */
286
__ioapic_write_entry(apic, pin, TRUE, entry);
288
__ioapic_write_entry(apic, pin, TRUE, entry);
290
if ( need_to_unmask )
292
/* Unmask if neccesary */
294
__ioapic_write_entry(apic, pin, TRUE, entry);
299
/* EOI an IO-APIC entry. One of vector or pin may be -1, indicating that
300
* it should be worked out using the other. This function disables interrupts
301
* and takes the ioapic_lock */
302
static void io_apic_eoi(unsigned int apic, unsigned int vector, unsigned int pin)
305
spin_lock_irqsave(&ioapic_lock, flags);
306
__io_apic_eoi(apic, vector, pin);
307
spin_unlock_irqrestore(&ioapic_lock, flags);
311
* Saves all the IO-APIC RTE's
313
int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
320
for (apic = 0; apic < nr_ioapics; apic++) {
321
if (!ioapic_entries[apic])
324
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
325
ioapic_entries[apic][pin] = __ioapic_read_entry(apic, pin, 1);
332
* Mask all IO APIC entries.
334
void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
341
for (apic = 0; apic < nr_ioapics; apic++) {
342
if (!ioapic_entries[apic])
345
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
346
struct IO_APIC_route_entry entry;
348
entry = ioapic_entries[apic][pin];
352
ioapic_write_entry(apic, pin, 1, entry);
359
* Restore IO APIC entries which was saved in ioapic_entries.
361
int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
368
for (apic = 0; apic < nr_ioapics; apic++) {
369
if (!ioapic_entries[apic])
372
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
373
ioapic_write_entry(apic, pin, 1, ioapic_entries[apic][pin]);
379
void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
383
for (apic = 0; apic < nr_ioapics; apic++)
384
xfree(ioapic_entries[apic]);
386
xfree(ioapic_entries);
389
static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
391
struct irq_pin_list *entry = irq_2_pin + irq;
392
unsigned int pin, reg;
398
reg = io_apic_read(entry->apic, 0x10 + pin*2);
401
io_apic_modify(entry->apic, 0x10 + pin*2, reg);
404
entry = irq_2_pin + entry->next;
409
static void __mask_IO_APIC_irq (unsigned int irq)
411
__modify_IO_APIC_irq(irq, 0x00010000, 0);
415
static void __unmask_IO_APIC_irq (unsigned int irq)
417
__modify_IO_APIC_irq(irq, 0, 0x00010000);
421
static void __edge_IO_APIC_irq (unsigned int irq)
423
__modify_IO_APIC_irq(irq, 0, 0x00008000);
427
static void __level_IO_APIC_irq (unsigned int irq)
429
__modify_IO_APIC_irq(irq, 0x00008000, 0);
432
static void mask_IO_APIC_irq (unsigned int irq)
436
spin_lock_irqsave(&ioapic_lock, flags);
437
__mask_IO_APIC_irq(irq);
438
spin_unlock_irqrestore(&ioapic_lock, flags);
441
static void unmask_IO_APIC_irq (unsigned int irq)
445
spin_lock_irqsave(&ioapic_lock, flags);
446
__unmask_IO_APIC_irq(irq);
447
spin_unlock_irqrestore(&ioapic_lock, flags);
450
static void __eoi_IO_APIC_irq(unsigned int irq)
452
struct irq_pin_list *entry = irq_2_pin + irq;
453
unsigned int pin, vector = IO_APIC_VECTOR(irq);
459
__io_apic_eoi(entry->apic, vector, pin);
462
entry = irq_2_pin + entry->next;
466
static void eoi_IO_APIC_irq(unsigned int irq)
469
spin_lock_irqsave(&ioapic_lock, flags);
470
__eoi_IO_APIC_irq(irq);
471
spin_unlock_irqrestore(&ioapic_lock, flags);
474
#define clear_IO_APIC_pin(a,p) __clear_IO_APIC_pin(a,p,0)
475
#define clear_IO_APIC_pin_raw(a,p) __clear_IO_APIC_pin(a,p,1)
476
static void __clear_IO_APIC_pin(unsigned int apic, unsigned int pin, int raw)
478
struct IO_APIC_route_entry entry;
480
/* Check delivery_mode to be sure we're not clearing an SMI pin */
481
entry = ioapic_read_entry(apic, pin, raw);
482
if (entry.delivery_mode == dest_SMI)
486
* Disable it in the IO-APIC irq-routing table:
488
memset(&entry, 0, sizeof(entry));
490
ioapic_write_entry(apic, pin, raw, entry);
493
static void clear_IO_APIC (void)
497
for (apic = 0; apic < nr_ioapics; apic++) {
498
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
499
clear_IO_APIC_pin(apic, pin);
500
clear_IO_APIC_pin_raw(apic, pin);
506
fastcall void smp_irq_move_cleanup_interrupt(struct cpu_user_regs *regs)
509
struct cpu_user_regs *old_regs = set_irq_regs(regs);
514
me = smp_processor_id();
515
for (vector = FIRST_DYNAMIC_VECTOR; vector < NR_VECTORS; vector++) {
518
struct irq_desc *desc;
520
irq = __get_cpu_var(vector_irq)[vector];
525
desc = irq_to_desc(irq);
529
cfg = desc->chip_data;
530
spin_lock(&desc->lock);
531
if (!cfg->move_cleanup_count)
534
if (vector == cfg->vector && cpu_isset(me, cfg->cpu_mask))
537
irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
539
* Check if the vector that needs to be cleanedup is
540
* registered at the cpu's IRR. If so, then this is not
541
* the best time to clean it up. Lets clean it up in the
542
* next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
545
if (irr & (1 << (vector % 32))) {
546
genapic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
549
__get_cpu_var(vector_irq)[vector] = -1;
550
cfg->move_cleanup_count--;
552
spin_unlock(&desc->lock);
556
set_irq_regs(old_regs);
559
static void send_cleanup_vector(struct irq_cfg *cfg)
561
cpumask_t cleanup_mask;
563
cpus_and(cleanup_mask, cfg->old_cpu_mask, cpu_online_map);
564
cfg->move_cleanup_count = cpus_weight(cleanup_mask);
565
genapic->send_IPI_mask(&cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
567
cfg->move_in_progress = 0;
570
void irq_complete_move(struct irq_desc **descp)
572
struct irq_desc *desc = *descp;
573
struct irq_cfg *cfg = desc->chip_data;
576
if (likely(!cfg->move_in_progress))
579
vector = get_irq_regs()->entry_vector;
580
me = smp_processor_id();
582
if (vector == cfg->vector && cpu_isset(me, cfg->cpu_mask))
583
send_cleanup_vector(cfg);
586
unsigned int set_desc_affinity(struct irq_desc *desc, const cpumask_t *mask)
594
if (!cpus_intersects(*mask, cpu_online_map))
598
cfg = desc->chip_data;
600
local_irq_save(flags);
602
ret = __assign_irq_vector(irq, cfg, mask);
603
unlock_vector_lock();
604
local_irq_restore(flags);
609
cpus_copy(desc->affinity, *mask);
610
cpus_and(dest_mask, *mask, cfg->cpu_mask);
612
return cpu_mask_to_apicid(&dest_mask);
616
set_ioapic_affinity_irq_desc(struct irq_desc *desc, const cpumask_t *mask)
622
struct irq_pin_list *entry;
625
cfg = desc->chip_data;
627
spin_lock_irqsave(&ioapic_lock, flags);
628
dest = set_desc_affinity(desc, mask);
629
if (dest != BAD_APICID) {
630
if ( !x2apic_enabled )
631
dest = SET_APIC_LOGICAL_ID(dest);
632
entry = irq_2_pin + irq;
639
io_apic_write(entry->apic, 0x10 + 1 + pin*2, dest);
640
data = io_apic_read(entry->apic, 0x10 + pin*2);
641
data &= ~IO_APIC_REDIR_VECTOR_MASK;
642
data |= cfg->vector & 0xFF;
643
io_apic_modify(entry->apic, 0x10 + pin*2, data);
647
entry = irq_2_pin + entry->next;
650
spin_unlock_irqrestore(&ioapic_lock, flags);
655
set_ioapic_affinity_irq(unsigned int irq, const struct cpumask mask)
657
struct irq_desc *desc;
659
desc = irq_to_desc(irq);
661
set_ioapic_affinity_irq_desc(desc, &mask);
663
#endif /* CONFIG_SMP */
666
* Find the IRQ entry number of a certain pin.
668
static int find_irq_entry(int apic, int pin, int type)
672
for (i = 0; i < mp_irq_entries; i++)
673
if (mp_irqs[i].mpc_irqtype == type &&
674
(mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
675
mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
676
mp_irqs[i].mpc_dstirq == pin)
683
* Find the pin to which IRQ[irq] (ISA) is connected
685
static int __init find_isa_irq_pin(int irq, int type)
689
for (i = 0; i < mp_irq_entries; i++) {
690
int lbus = mp_irqs[i].mpc_srcbus;
692
if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
693
mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
694
mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
695
mp_bus_id_to_type[lbus] == MP_BUS_NEC98
697
(mp_irqs[i].mpc_irqtype == type) &&
698
(mp_irqs[i].mpc_srcbusirq == irq))
700
return mp_irqs[i].mpc_dstirq;
705
static int __init find_isa_irq_apic(int irq, int type)
709
for (i = 0; i < mp_irq_entries; i++) {
710
int lbus = mp_irqs[i].mpc_srcbus;
712
if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
713
mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
714
mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
715
mp_bus_id_to_type[lbus] == MP_BUS_NEC98
717
(mp_irqs[i].mpc_irqtype == type) &&
718
(mp_irqs[i].mpc_srcbusirq == irq))
721
if (i < mp_irq_entries) {
723
for(apic = 0; apic < nr_ioapics; apic++) {
724
if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
733
* Find a specific PCI IRQ entry.
734
* Not an __init, possibly needed by modules
736
static int pin_2_irq(int idx, int apic, int pin);
739
* This function currently is only a helper for the i386 smp boot process where
740
* we need to reprogram the ioredtbls to cater for the cpus which have come online
741
* so mask in all cases should simply be TARGET_CPUS
744
void /*__init*/ setup_ioapic_dest(void)
746
int pin, ioapic, irq, irq_entry;
749
if (skip_ioapic_setup)
752
for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
753
for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
754
irq_entry = find_irq_entry(ioapic, pin, mp_INT);
757
irq = pin_2_irq(irq_entry, ioapic, pin);
759
BUG_ON(cpus_empty(cfg->cpu_mask));
760
set_ioapic_affinity_irq(irq, cfg->cpu_mask);
768
* EISA Edge/Level control register, ELCR
770
static int EISA_ELCR(unsigned int irq)
772
if (platform_legacy_irq(irq)) {
773
unsigned int port = 0x4d0 + (irq >> 3);
774
return (inb(port) >> (irq & 7)) & 1;
776
apic_printk(APIC_VERBOSE, KERN_INFO
777
"Broken MPtable reports ISA irq %d\n", irq);
781
/* EISA interrupts are always polarity zero and can be edge or level
782
* trigger depending on the ELCR value. If an interrupt is listed as
783
* EISA conforming in the MP table, that means its trigger type must
784
* be read in from the ELCR */
786
#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
787
#define default_EISA_polarity(idx) (0)
789
/* ISA interrupts are always polarity zero edge triggered,
790
* when listed as conforming in the MP table. */
792
#define default_ISA_trigger(idx) (0)
793
#define default_ISA_polarity(idx) (0)
795
/* PCI interrupts are always polarity one level triggered,
796
* when listed as conforming in the MP table. */
798
#define default_PCI_trigger(idx) (1)
799
#define default_PCI_polarity(idx) (1)
801
/* MCA interrupts are always polarity zero level triggered,
802
* when listed as conforming in the MP table. */
804
#define default_MCA_trigger(idx) (1)
805
#define default_MCA_polarity(idx) (0)
807
/* NEC98 interrupts are always polarity zero edge triggered,
808
* when listed as conforming in the MP table. */
810
#define default_NEC98_trigger(idx) (0)
811
#define default_NEC98_polarity(idx) (0)
813
static int __init MPBIOS_polarity(int idx)
815
int bus = mp_irqs[idx].mpc_srcbus;
819
* Determine IRQ line polarity (high active or low active):
821
switch (mp_irqs[idx].mpc_irqflag & 3)
823
case 0: /* conforms, ie. bus-type dependent polarity */
825
switch (mp_bus_id_to_type[bus])
827
case MP_BUS_ISA: /* ISA pin */
829
polarity = default_ISA_polarity(idx);
832
case MP_BUS_EISA: /* EISA pin */
834
polarity = default_EISA_polarity(idx);
837
case MP_BUS_PCI: /* PCI pin */
839
polarity = default_PCI_polarity(idx);
842
case MP_BUS_MCA: /* MCA pin */
844
polarity = default_MCA_polarity(idx);
847
case MP_BUS_NEC98: /* NEC 98 pin */
849
polarity = default_NEC98_polarity(idx);
854
printk(KERN_WARNING "broken BIOS!!\n");
861
case 1: /* high active */
866
case 2: /* reserved */
868
printk(KERN_WARNING "broken BIOS!!\n");
872
case 3: /* low active */
877
default: /* invalid */
879
printk(KERN_WARNING "broken BIOS!!\n");
887
static int MPBIOS_trigger(int idx)
889
int bus = mp_irqs[idx].mpc_srcbus;
893
* Determine IRQ trigger mode (edge or level sensitive):
895
switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
897
case 0: /* conforms, ie. bus-type dependent */
899
switch (mp_bus_id_to_type[bus])
901
case MP_BUS_ISA: /* ISA pin */
903
trigger = default_ISA_trigger(idx);
906
case MP_BUS_EISA: /* EISA pin */
908
trigger = default_EISA_trigger(idx);
911
case MP_BUS_PCI: /* PCI pin */
913
trigger = default_PCI_trigger(idx);
916
case MP_BUS_MCA: /* MCA pin */
918
trigger = default_MCA_trigger(idx);
921
case MP_BUS_NEC98: /* NEC 98 pin */
923
trigger = default_NEC98_trigger(idx);
928
printk(KERN_WARNING "broken BIOS!!\n");
940
case 2: /* reserved */
942
printk(KERN_WARNING "broken BIOS!!\n");
951
default: /* invalid */
953
printk(KERN_WARNING "broken BIOS!!\n");
961
static inline int irq_polarity(int idx)
963
return MPBIOS_polarity(idx);
966
static inline int irq_trigger(int idx)
968
return MPBIOS_trigger(idx);
971
static int pin_2_irq(int idx, int apic, int pin)
974
int bus = mp_irqs[idx].mpc_srcbus;
977
* Debugging check, we are in big trouble if this message pops up!
979
if (mp_irqs[idx].mpc_dstirq != pin)
980
printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
982
switch (mp_bus_id_to_type[bus])
984
case MP_BUS_ISA: /* ISA pin */
989
irq = mp_irqs[idx].mpc_srcbusirq;
992
case MP_BUS_PCI: /* PCI pin */
995
* PCI IRQs are mapped in order
999
irq += nr_ioapic_registers[i++];
1005
printk(KERN_ERR "unknown bus type %d.\n",bus);
1014
static inline int IO_APIC_irq_trigger(int irq)
1018
for (apic = 0; apic < nr_ioapics; apic++) {
1019
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1020
idx = find_irq_entry(apic,pin,mp_INT);
1021
if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1022
return irq_trigger(idx);
1026
* nonexistent IRQs are edge default
1031
static hw_irq_controller ioapic_level_type;
1032
static hw_irq_controller ioapic_edge_type;
1034
#define IOAPIC_AUTO -1
1035
#define IOAPIC_EDGE 0
1036
#define IOAPIC_LEVEL 1
1038
#define SET_DEST(x, y, value) \
1039
do { if ( x2apic_enabled ) x = value; else y = value; } while(0)
1041
static inline void ioapic_register_intr(int irq, unsigned long trigger)
1043
if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1044
trigger == IOAPIC_LEVEL)
1045
irq_desc[irq].handler = &ioapic_level_type;
1047
irq_desc[irq].handler = &ioapic_edge_type;
1050
static void __init setup_IO_APIC_irqs(void)
1052
struct IO_APIC_route_entry entry;
1053
int apic, pin, idx, irq, first_notcon = 1, vector;
1054
unsigned long flags;
1055
struct irq_cfg *cfg;
1057
apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1059
for (apic = 0; apic < nr_ioapics; apic++) {
1060
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1063
* add it to the IO-APIC irq-routing table:
1065
memset(&entry,0,sizeof(entry));
1067
entry.delivery_mode = INT_DELIVERY_MODE;
1068
entry.dest_mode = INT_DEST_MODE;
1069
entry.mask = 0; /* enable IRQ */
1071
idx = find_irq_entry(apic,pin,mp_INT);
1074
apic_printk(APIC_VERBOSE, KERN_DEBUG
1075
" IO-APIC (apicid-pin) %d-%d",
1076
mp_ioapics[apic].mpc_apicid,
1080
apic_printk(APIC_VERBOSE, ", %d-%d",
1081
mp_ioapics[apic].mpc_apicid, pin);
1085
entry.trigger = irq_trigger(idx);
1086
entry.polarity = irq_polarity(idx);
1088
if (irq_trigger(idx)) {
1093
irq = pin_2_irq(idx, apic, pin);
1095
* skip adding the timer int on secondary nodes, which causes
1096
* a small but painful rift in the time-space continuum
1098
if (multi_timer_check(apic, irq))
1101
add_pin_to_irq(irq, apic, pin);
1103
if (!apic && !IO_APIC_IRQ(irq))
1106
if (IO_APIC_IRQ(irq)) {
1107
vector = assign_irq_vector(irq);
1109
entry.vector = vector;
1110
ioapic_register_intr(irq, IOAPIC_AUTO);
1112
if (!apic && platform_legacy_irq(irq))
1113
disable_8259A_irq(irq);
1116
SET_DEST(entry.dest.dest32, entry.dest.logical.logical_dest,
1117
cpu_mask_to_apicid(&cfg->cpu_mask));
1118
spin_lock_irqsave(&ioapic_lock, flags);
1119
__ioapic_write_entry(apic, pin, 0, entry);
1120
set_native_irq_info(irq, TARGET_CPUS);
1121
spin_unlock_irqrestore(&ioapic_lock, flags);
1126
apic_printk(APIC_VERBOSE, " not connected.\n");
1130
* Set up the 8259A-master output pin:
1132
static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
1134
struct IO_APIC_route_entry entry;
1136
memset(&entry,0,sizeof(entry));
1138
disable_8259A_irq(0);
1141
apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1144
* We use logical delivery to get the timer IRQ
1147
entry.dest_mode = INT_DEST_MODE;
1148
entry.mask = 0; /* unmask IRQ now */
1149
SET_DEST(entry.dest.dest32, entry.dest.logical.logical_dest,
1150
cpu_mask_to_apicid(TARGET_CPUS));
1151
entry.delivery_mode = INT_DELIVERY_MODE;
1154
entry.vector = vector;
1157
* The timer IRQ doesn't have to know that behind the
1158
* scene we have a 8259A-master in AEOI mode ...
1160
irq_desc[0].handler = &ioapic_edge_type;
1163
* Add it to the IO-APIC irq-routing table:
1165
ioapic_write_entry(apic, pin, 0, entry);
1167
enable_8259A_irq(0);
1170
static inline void UNEXPECTED_IO_APIC(void)
1174
static void /*__init*/ __print_IO_APIC(void)
1177
union IO_APIC_reg_00 reg_00;
1178
union IO_APIC_reg_01 reg_01;
1179
union IO_APIC_reg_02 reg_02;
1180
union IO_APIC_reg_03 reg_03;
1181
unsigned long flags;
1183
printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1184
for (i = 0; i < nr_ioapics; i++)
1185
printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1186
mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1189
* We are a bit conservative about what we expect. We have to
1190
* know about every hardware change ASAP.
1192
printk(KERN_INFO "testing the IO APIC.......................\n");
1194
for (apic = 0; apic < nr_ioapics; apic++) {
1196
spin_lock_irqsave(&ioapic_lock, flags);
1197
reg_00.raw = io_apic_read(apic, 0);
1198
reg_01.raw = io_apic_read(apic, 1);
1199
if (reg_01.bits.version >= 0x10)
1200
reg_02.raw = io_apic_read(apic, 2);
1201
if (reg_01.bits.version >= 0x20)
1202
reg_03.raw = io_apic_read(apic, 3);
1203
spin_unlock_irqrestore(&ioapic_lock, flags);
1205
printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1206
printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1207
printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1208
printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1209
printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1210
if (reg_00.bits.ID >= get_physical_broadcast())
1211
UNEXPECTED_IO_APIC();
1212
if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1213
UNEXPECTED_IO_APIC();
1215
printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1216
printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1217
if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1218
(reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1219
(reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1220
(reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1221
(reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1222
(reg_01.bits.entries != 0x2E) &&
1223
(reg_01.bits.entries != 0x3F)
1225
UNEXPECTED_IO_APIC();
1227
printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1228
printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1229
if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1230
(reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1231
(reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1232
(reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1233
(reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1235
UNEXPECTED_IO_APIC();
1236
if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1237
UNEXPECTED_IO_APIC();
1240
* Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1241
* but the value of reg_02 is read as the previous read register
1242
* value, so ignore it if reg_02 == reg_01.
1244
if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1245
printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1246
printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1247
if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1248
UNEXPECTED_IO_APIC();
1252
* Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1253
* or reg_03, but the value of reg_0[23] is read as the previous read
1254
* register value, so ignore it if reg_03 == reg_0[12].
1256
if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1257
reg_03.raw != reg_01.raw) {
1258
printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1259
printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1260
if (reg_03.bits.__reserved_1)
1261
UNEXPECTED_IO_APIC();
1264
printk(KERN_DEBUG ".... IRQ redirection table:\n");
1266
printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1267
" Stat Dest Deli Vect: \n");
1269
for (i = 0; i <= reg_01.bits.entries; i++) {
1270
struct IO_APIC_route_entry entry;
1272
entry = ioapic_read_entry(apic, i, 0);
1274
printk(KERN_DEBUG " %02x %03X %02X ",
1276
entry.dest.logical.logical_dest,
1277
entry.dest.physical.physical_dest
1280
printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1285
entry.delivery_status,
1287
entry.delivery_mode,
1292
printk(KERN_INFO "Using vector-based indexing\n");
1293
printk(KERN_DEBUG "IRQ to pin mappings:\n");
1294
for (i = 0; i < nr_irqs_gsi; i++) {
1295
struct irq_pin_list *entry = irq_2_pin + i;
1298
printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1300
printk("-> %d:%d", entry->apic, entry->pin);
1303
entry = irq_2_pin + entry->next;
1308
printk(KERN_INFO ".................................... done.\n");
1313
void print_IO_APIC(void)
1315
if (apic_verbosity != APIC_QUIET)
1319
static void _print_IO_APIC_keyhandler(unsigned char key)
1323
static struct keyhandler print_IO_APIC_keyhandler = {
1325
.u.fn = _print_IO_APIC_keyhandler,
1326
.desc = "print ioapic info"
1329
static void __init enable_IO_APIC(void)
1331
int i8259_apic, i8259_pin;
1334
/* Initialise dynamic irq_2_pin free list. */
1335
irq_2_pin = xmalloc_array(struct irq_pin_list, PIN_MAP_SIZE);
1336
memset(irq_2_pin, 0, PIN_MAP_SIZE * sizeof(*irq_2_pin));
1338
for (i = 0; i < PIN_MAP_SIZE; i++)
1339
irq_2_pin[i].pin = -1;
1340
for (i = irq_2_pin_free_entry = nr_irqs_gsi; i < PIN_MAP_SIZE; i++)
1341
irq_2_pin[i].next = i + 1;
1343
for(apic = 0; apic < nr_ioapics; apic++) {
1345
/* See if any of the pins is in ExtINT mode */
1346
for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1347
struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin, 0);
1349
/* If the interrupt line is enabled and in ExtInt mode
1350
* I have found the pin where the i8259 is connected.
1352
if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1353
ioapic_i8259.apic = apic;
1354
ioapic_i8259.pin = pin;
1360
/* Look to see what if the MP table has reported the ExtINT */
1361
/* If we could not find the appropriate pin by looking at the ioapic
1362
* the i8259 probably is not connected the ioapic but give the
1363
* mptable a chance anyway.
1365
i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1366
i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1367
/* Trust the MP table if nothing is setup in the hardware */
1368
if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1369
printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1370
ioapic_i8259.pin = i8259_pin;
1371
ioapic_i8259.apic = i8259_apic;
1373
/* Complain if the MP table and the hardware disagree */
1374
if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1375
(i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1377
printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1381
* Do not trust the IO-APIC being empty at bootup
1387
* Not an __init, needed by the reboot code
1389
void disable_IO_APIC(void)
1392
* Clear the IO-APIC before rebooting:
1397
* If the i8259 is routed through an IOAPIC
1398
* Put that IOAPIC in virtual wire mode
1399
* so legacy interrupts can be delivered.
1401
if (ioapic_i8259.pin != -1) {
1402
struct IO_APIC_route_entry entry;
1404
memset(&entry, 0, sizeof(entry));
1405
entry.mask = 0; /* Enabled */
1406
entry.trigger = 0; /* Edge */
1408
entry.polarity = 0; /* High */
1409
entry.delivery_status = 0;
1410
entry.dest_mode = 0; /* Physical */
1411
entry.delivery_mode = dest_ExtINT; /* ExtInt */
1413
SET_DEST(entry.dest.dest32, entry.dest.physical.physical_dest,
1417
* Add it to the IO-APIC irq-routing table:
1419
ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, 0, entry);
1421
disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1425
* function to set the IO-APIC physical IDs based on the
1426
* values stored in the MPC table.
1428
* by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1431
#ifndef CONFIG_X86_NUMAQ
1432
static void __init setup_ioapic_ids_from_mpc(void)
1434
union IO_APIC_reg_00 reg_00;
1435
physid_mask_t phys_id_present_map;
1438
unsigned char old_id;
1439
unsigned long flags;
1442
* Don't check I/O APIC IDs for xAPIC systems. They have
1443
* no meaning without the serial APIC bus.
1445
if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1446
|| APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
1450
* This is broken; anything with a real cpu count has to
1451
* circumvent this idiocy regardless.
1453
phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1456
* Set the IOAPIC ID to the value stored in the MPC table.
1458
for (apic = 0; apic < nr_ioapics; apic++) {
1460
/* Read the register 0 value */
1461
spin_lock_irqsave(&ioapic_lock, flags);
1462
reg_00.raw = io_apic_read(apic, 0);
1463
spin_unlock_irqrestore(&ioapic_lock, flags);
1465
old_id = mp_ioapics[apic].mpc_apicid;
1467
if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1468
printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1469
apic, mp_ioapics[apic].mpc_apicid);
1470
printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1472
mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1476
* Sanity check, is the ID really free? Every APIC in a
1477
* system must have a unique ID or we get lots of nice
1478
* 'stuck on smp_invalidate_needed IPI wait' messages.
1480
if (check_apicid_used(phys_id_present_map,
1481
mp_ioapics[apic].mpc_apicid)) {
1482
printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1483
apic, mp_ioapics[apic].mpc_apicid);
1484
for (i = 0; i < get_physical_broadcast(); i++)
1485
if (!physid_isset(i, phys_id_present_map))
1487
if (i >= get_physical_broadcast())
1488
panic("Max APIC ID exceeded!\n");
1489
printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1491
physid_set(i, phys_id_present_map);
1492
mp_ioapics[apic].mpc_apicid = i;
1495
tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1496
apic_printk(APIC_VERBOSE, "Setting %d in the "
1497
"phys_id_present_map\n",
1498
mp_ioapics[apic].mpc_apicid);
1499
physids_or(phys_id_present_map, phys_id_present_map, tmp);
1504
* We need to adjust the IRQ routing table
1505
* if the ID changed.
1507
if (old_id != mp_ioapics[apic].mpc_apicid)
1508
for (i = 0; i < mp_irq_entries; i++)
1509
if (mp_irqs[i].mpc_dstapic == old_id)
1510
mp_irqs[i].mpc_dstapic
1511
= mp_ioapics[apic].mpc_apicid;
1514
* Read the right value from the MPC table and
1515
* write it into the ID register.
1517
apic_printk(APIC_VERBOSE, KERN_INFO
1518
"...changing IO-APIC physical APIC ID to %d ...",
1519
mp_ioapics[apic].mpc_apicid);
1521
reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1522
spin_lock_irqsave(&ioapic_lock, flags);
1523
io_apic_write(apic, 0, reg_00.raw);
1524
spin_unlock_irqrestore(&ioapic_lock, flags);
1529
spin_lock_irqsave(&ioapic_lock, flags);
1530
reg_00.raw = io_apic_read(apic, 0);
1531
spin_unlock_irqrestore(&ioapic_lock, flags);
1532
if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1533
printk("could not set ID!\n");
1535
apic_printk(APIC_VERBOSE, " ok.\n");
1539
static void __init setup_ioapic_ids_from_mpc(void) { }
1543
* There is a nasty bug in some older SMP boards, their mptable lies
1544
* about the timer IRQ. We do the following to work around the situation:
1546
* - timer IRQ defaults to IO-APIC IRQ
1547
* - if this function detects that timer IRQs are defunct, then we fall
1548
* back to ISA timer IRQs
1550
static int __init timer_irq_works(void)
1552
extern unsigned long pit0_ticks;
1553
unsigned long t1, flags;
1558
local_save_flags(flags);
1560
/* Let ten ticks pass... */
1561
mdelay((10 * 1000) / HZ);
1562
local_irq_restore(flags);
1565
* Expect a few ticks at least, to be sure some possible
1566
* glue logic does not lock up after one or two first
1567
* ticks in a non-ExtINT mode. Also the local APIC
1568
* might have cached one ExtINT interrupt. Finally, at
1569
* least one tick may be lost due to delays.
1572
if (pit0_ticks - t1 > 4)
1579
* In the SMP+IOAPIC case it might happen that there are an unspecified
1580
* number of pending IRQ events unhandled. These cases are very rare,
1581
* so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1582
* better to do it this way as thus we do not have to be aware of
1583
* 'pending' interrupts in the IRQ path, except at this point.
1586
* Edge triggered needs to resend any interrupt
1587
* that was delayed but this is now handled in the device
1592
* Starting up a edge-triggered IO-APIC interrupt is
1593
* nasty - we need to make sure that we get the edge.
1594
* If it is already asserted for some reason, we need
1595
* return 1 to indicate that is was pending.
1597
* This is not complete - we should be able to fake
1598
* an edge even if it isn't on the 8259A...
1600
static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1602
int was_pending = 0;
1603
unsigned long flags;
1605
spin_lock_irqsave(&ioapic_lock, flags);
1606
if (platform_legacy_irq(irq)) {
1607
disable_8259A_irq(irq);
1608
if (i8259A_irq_pending(irq))
1611
__unmask_IO_APIC_irq(irq);
1612
spin_unlock_irqrestore(&ioapic_lock, flags);
1618
* Once we have recorded IRQ_PENDING already, we can mask the
1619
* interrupt for real. This prevents IRQ storms from unhandled
1622
static void ack_edge_ioapic_irq(unsigned int irq)
1624
struct irq_desc *desc = irq_to_desc(irq);
1626
irq_complete_move(&desc);
1627
move_native_irq(irq);
1629
if ((desc->status & (IRQ_PENDING | IRQ_DISABLED))
1630
== (IRQ_PENDING | IRQ_DISABLED))
1631
mask_IO_APIC_irq(irq);
1636
* Level triggered interrupts can just be masked,
1637
* and shutting down and starting up the interrupt
1638
* is the same as enabling and disabling them -- except
1639
* with a startup need to return a "was pending" value.
1641
* Level triggered interrupts are special because we
1642
* do not touch any IO-APIC register while handling
1643
* them. We ack the APIC in the end-IRQ handler, not
1644
* in the start-IRQ-handler. Protection against reentrance
1645
* from the same interrupt is still provided, both by the
1646
* generic IRQ layer and by the fact that an unacked local
1647
* APIC does not accept IRQs.
1649
static unsigned int startup_level_ioapic_irq (unsigned int irq)
1651
unmask_IO_APIC_irq(irq);
1653
return 0; /* don't check for pending */
1656
int __read_mostly ioapic_ack_new = 1;
1657
static void setup_ioapic_ack(char *s)
1659
if ( !strcmp(s, "old") )
1661
else if ( !strcmp(s, "new") )
1664
printk("Unknown ioapic_ack value specified: '%s'\n", s);
1666
custom_param("ioapic_ack", setup_ioapic_ack);
1668
static bool_t io_apic_level_ack_pending(unsigned int irq)
1670
struct irq_pin_list *entry;
1671
unsigned long flags;
1673
spin_lock_irqsave(&ioapic_lock, flags);
1674
entry = &irq_2_pin[irq];
1685
reg = io_apic_read(entry->apic, 0x10 + pin*2);
1686
/* Is the remote IRR bit set? */
1687
if (reg & IO_APIC_REDIR_REMOTE_IRR) {
1688
spin_unlock_irqrestore(&ioapic_lock, flags);
1693
entry = irq_2_pin + entry->next;
1695
spin_unlock_irqrestore(&ioapic_lock, flags);
1700
static void mask_and_ack_level_ioapic_irq (unsigned int irq)
1704
struct irq_desc *desc = irq_to_desc(irq);
1706
irq_complete_move(&desc);
1708
if ( ioapic_ack_new )
1711
if ( !directed_eoi_enabled )
1712
mask_IO_APIC_irq(irq);
1715
* It appears there is an erratum which affects at least version 0x11
1716
* of I/O APIC (that's the 82093AA and cores integrated into various
1717
* chipsets). Under certain conditions a level-triggered interrupt is
1718
* erroneously delivered as edge-triggered one but the respective IRR
1719
* bit gets set nevertheless. As a result the I/O unit expects an EOI
1720
* message but it will never arrive and further interrupts are blocked
1721
* from the source. The exact reason is so far unknown, but the
1722
* phenomenon was observed when two consecutive interrupt requests
1723
* from a given source get delivered to the same CPU and the source is
1724
* temporarily disabled in between.
1726
* A workaround is to simulate an EOI message manually. We achieve it
1727
* by setting the trigger mode to edge and then to level when the edge
1728
* trigger mode gets detected in the TMR of a local APIC for a
1729
* level-triggered interrupt. We mask the source for the time of the
1730
* operation to prevent an edge-triggered interrupt escaping meanwhile.
1731
* The idea is from Manfred Spraul. --macro
1733
i = IO_APIC_VECTOR(irq);
1735
v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1739
if ( directed_eoi_enabled )
1742
if ((irq_desc[irq].status & IRQ_MOVE_PENDING) &&
1743
!io_apic_level_ack_pending(irq))
1744
move_masked_irq(irq);
1746
if ( !(v & (1 << (i & 0x1f))) ) {
1747
spin_lock(&ioapic_lock);
1748
__edge_IO_APIC_irq(irq);
1749
__level_IO_APIC_irq(irq);
1750
spin_unlock(&ioapic_lock);
1754
static void end_level_ioapic_irq (unsigned int irq, u8 vector)
1759
if ( !ioapic_ack_new )
1761
if ( directed_eoi_enabled )
1763
if ( !(irq_desc[irq].status & (IRQ_DISABLED|IRQ_MOVE_PENDING)) )
1765
eoi_IO_APIC_irq(irq);
1769
mask_IO_APIC_irq(irq);
1770
eoi_IO_APIC_irq(irq);
1771
if ( (irq_desc[irq].status & IRQ_MOVE_PENDING) &&
1772
!io_apic_level_ack_pending(irq) )
1773
move_masked_irq(irq);
1776
if ( !(irq_desc[irq].status & IRQ_DISABLED) )
1777
unmask_IO_APIC_irq(irq);
1783
* It appears there is an erratum which affects at least version 0x11
1784
* of I/O APIC (that's the 82093AA and cores integrated into various
1785
* chipsets). Under certain conditions a level-triggered interrupt is
1786
* erroneously delivered as edge-triggered one but the respective IRR
1787
* bit gets set nevertheless. As a result the I/O unit expects an EOI
1788
* message but it will never arrive and further interrupts are blocked
1789
* from the source. The exact reason is so far unknown, but the
1790
* phenomenon was observed when two consecutive interrupt requests
1791
* from a given source get delivered to the same CPU and the source is
1792
* temporarily disabled in between.
1794
* A workaround is to simulate an EOI message manually. We achieve it
1795
* by setting the trigger mode to edge and then to level when the edge
1796
* trigger mode gets detected in the TMR of a local APIC for a
1797
* level-triggered interrupt. We mask the source for the time of the
1798
* operation to prevent an edge-triggered interrupt escaping meanwhile.
1799
* The idea is from Manfred Spraul. --macro
1801
i = IO_APIC_VECTOR(irq);
1803
/* Manually EOI the old vector if we are moving to the new */
1804
if ( vector && i != vector )
1807
for (ioapic = 0; ioapic < nr_ioapics; ioapic++)
1808
io_apic_eoi_vector(ioapic, i);
1811
v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1815
if ((irq_desc[irq].status & IRQ_MOVE_PENDING) &&
1816
!io_apic_level_ack_pending(irq))
1817
move_native_irq(irq);
1819
if (!(v & (1 << (i & 0x1f)))) {
1820
spin_lock(&ioapic_lock);
1821
__mask_IO_APIC_irq(irq);
1822
__edge_IO_APIC_irq(irq);
1823
__level_IO_APIC_irq(irq);
1824
if ( !(irq_desc[irq].status & IRQ_DISABLED) )
1825
__unmask_IO_APIC_irq(irq);
1826
spin_unlock(&ioapic_lock);
1830
static void disable_edge_ioapic_irq(unsigned int irq)
1834
static void end_edge_ioapic_irq(unsigned int irq, u8 vector)
1839
* Level and edge triggered IO-APIC interrupts need different handling,
1840
* so we use two separate IRQ descriptors. Edge triggered IRQs can be
1841
* handled with the level-triggered descriptor, but that one has slightly
1842
* more overhead. Level-triggered interrupts cannot be handled with the
1843
* edge-triggered handler, without risking IRQ storms and other ugly
1846
static hw_irq_controller ioapic_edge_type = {
1847
.typename = "IO-APIC-edge",
1848
.startup = startup_edge_ioapic_irq,
1849
.shutdown = disable_edge_ioapic_irq,
1850
.enable = unmask_IO_APIC_irq,
1851
.disable = disable_edge_ioapic_irq,
1852
.ack = ack_edge_ioapic_irq,
1853
.end = end_edge_ioapic_irq,
1854
.set_affinity = set_ioapic_affinity_irq,
1857
static hw_irq_controller ioapic_level_type = {
1858
.typename = "IO-APIC-level",
1859
.startup = startup_level_ioapic_irq,
1860
.shutdown = mask_IO_APIC_irq,
1861
.enable = unmask_IO_APIC_irq,
1862
.disable = mask_IO_APIC_irq,
1863
.ack = mask_and_ack_level_ioapic_irq,
1864
.end = end_level_ioapic_irq,
1865
.set_affinity = set_ioapic_affinity_irq,
1868
static unsigned int startup_msi_irq(unsigned int irq)
1870
unmask_msi_irq(irq);
1874
static void ack_msi_irq(unsigned int irq)
1876
struct irq_desc *desc = irq_to_desc(irq);
1878
irq_complete_move(&desc);
1879
move_native_irq(irq);
1881
if ( msi_maskable_irq(desc->msi_desc) )
1882
ack_APIC_irq(); /* ACKTYPE_NONE */
1885
static void end_msi_irq(unsigned int irq, u8 vector)
1887
if ( !msi_maskable_irq(irq_desc[irq].msi_desc) )
1888
ack_APIC_irq(); /* ACKTYPE_EOI */
1891
#define shutdown_msi_irq mask_msi_irq
1894
* IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
1895
* which implement the MSI or MSI-X Capability Structure.
1897
hw_irq_controller pci_msi_type = {
1898
.typename = "PCI-MSI",
1899
.startup = startup_msi_irq,
1900
.shutdown = shutdown_msi_irq,
1901
.enable = unmask_msi_irq,
1902
.disable = mask_msi_irq,
1905
.set_affinity = set_msi_affinity,
1908
static inline void init_IO_APIC_traps(void)
1911
/* Xen: This is way simpler than the Linux implementation. */
1912
for (irq = 0; platform_legacy_irq(irq); irq++)
1913
if (IO_APIC_IRQ(irq) && !IO_APIC_VECTOR(irq))
1914
make_8259A_irq(irq);
1917
static void enable_lapic_irq(unsigned int irq)
1921
v = apic_read(APIC_LVT0);
1922
apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1925
static void disable_lapic_irq(unsigned int irq)
1929
v = apic_read(APIC_LVT0);
1930
apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1933
static void ack_lapic_irq(unsigned int irq)
1938
#define end_lapic_irq end_edge_ioapic_irq
1940
static hw_irq_controller lapic_irq_type = {
1941
.typename = "local-APIC-edge",
1942
.startup = NULL, /* startup_irq() not used for IRQ0 */
1943
.shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1944
.enable = enable_lapic_irq,
1945
.disable = disable_lapic_irq,
1946
.ack = ack_lapic_irq,
1947
.end = end_lapic_irq,
1951
* This looks a bit hackish but it's about the only one way of sending
1952
* a few INTA cycles to 8259As and any associated glue logic. ICR does
1953
* not support the ExtINT mode, unfortunately. We need to send these
1954
* cycles as some i82489DX-based boards have glue logic that keeps the
1955
* 8259A interrupt line asserted until INTA. --macro
1957
static void __init unlock_ExtINT_logic(void)
1960
struct IO_APIC_route_entry entry0, entry1;
1961
unsigned char save_control, save_freq_select;
1963
pin = find_isa_irq_pin(8, mp_INT);
1964
apic = find_isa_irq_apic(8, mp_INT);
1968
entry0 = ioapic_read_entry(apic, pin, 0);
1969
clear_IO_APIC_pin(apic, pin);
1971
memset(&entry1, 0, sizeof(entry1));
1973
entry1.dest_mode = 0; /* physical delivery */
1974
entry1.mask = 0; /* unmask IRQ now */
1975
SET_DEST(entry1.dest.dest32, entry1.dest.physical.physical_dest,
1976
hard_smp_processor_id());
1977
entry1.delivery_mode = dest_ExtINT;
1978
entry1.polarity = entry0.polarity;
1982
ioapic_write_entry(apic, pin, 0, entry1);
1984
save_control = CMOS_READ(RTC_CONTROL);
1985
save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1986
CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1988
CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1993
if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1997
CMOS_WRITE(save_control, RTC_CONTROL);
1998
CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1999
clear_IO_APIC_pin(apic, pin);
2001
ioapic_write_entry(apic, pin, 0, entry0);
2005
* This code may look a bit paranoid, but it's supposed to cooperate with
2006
* a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2007
* is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2008
* fanatically on his truly buggy board.
2010
static void __init check_timer(void)
2012
int apic1, pin1, apic2, pin2;
2014
unsigned long flags;
2016
local_irq_save(flags);
2019
* get/set the timer IRQ vector:
2021
disable_8259A_irq(0);
2022
vector = FIRST_HIPRIORITY_VECTOR;
2023
clear_irq_vector(0);
2025
if ((ret = bind_irq_vector(0, vector, (cpumask_t)CPU_MASK_ALL)))
2026
printk(KERN_ERR"..IRQ0 is not set correctly with ioapic!!!, err:%d\n", ret);
2028
irq_desc[0].depth = 0;
2029
irq_desc[0].status &= ~IRQ_DISABLED;
2032
* Subtle, code in do_timer_interrupt() expects an AEOI
2033
* mode for the 8259A whenever interrupts are routed
2034
* through I/O APICs. Also IRQ0 has to be enabled in
2035
* the 8259A which implies the virtual wire has to be
2036
* disabled in the local APIC.
2038
apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2040
/* XEN: Ripped out the legacy missed-tick logic, so below is not needed. */
2042
/*enable_8259A_irq(0);*/
2044
pin1 = find_isa_irq_pin(0, mp_INT);
2045
apic1 = find_isa_irq_apic(0, mp_INT);
2046
pin2 = ioapic_i8259.pin;
2047
apic2 = ioapic_i8259.apic;
2049
printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2050
vector, apic1, pin1, apic2, pin2);
2054
* Ok, does IRQ0 through the IOAPIC work?
2056
unmask_IO_APIC_irq(0);
2057
if (timer_irq_works()) {
2058
local_irq_restore(flags);
2061
clear_IO_APIC_pin(apic1, pin1);
2062
printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to "
2066
printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
2068
printk("\n..... (found pin %d) ...", pin2);
2070
* legacy devices should be connected to IO APIC #0
2072
setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
2073
if (timer_irq_works()) {
2074
local_irq_restore(flags);
2077
replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2079
add_pin_to_irq(0, apic2, pin2);
2083
* Cleanup, just in case ...
2085
clear_IO_APIC_pin(apic2, pin2);
2087
printk(" failed.\n");
2089
if (nmi_watchdog == NMI_IO_APIC) {
2090
printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2094
printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2096
disable_8259A_irq(0);
2097
irq_desc[0].handler = &lapic_irq_type;
2098
apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2099
enable_8259A_irq(0);
2101
if (timer_irq_works()) {
2102
local_irq_restore(flags);
2103
printk(" works.\n");
2106
apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2107
printk(" failed.\n");
2109
printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2114
apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2116
unlock_ExtINT_logic();
2118
local_irq_restore(flags);
2120
if (timer_irq_works()) {
2121
printk(" works.\n");
2124
printk(" failed :(.\n");
2125
panic("IO-APIC + timer doesn't work! Boot with apic_verbosity=debug "
2126
"and send a report. Then try booting with the 'noapic' option");
2131
* IRQ's that are handled by the PIC in the MPS IOAPIC case.
2132
* - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2133
* Linux doesn't really care, as it's not actually used
2134
* for any interrupt handling anyway.
2136
#define PIC_IRQS (1 << PIC_CASCADE_IR)
2138
static struct IO_APIC_route_entry *ioapic_pm_state;
2140
static void __init ioapic_pm_state_alloc(void)
2142
int i, nr_entry = 0;
2144
for (i = 0; i < nr_ioapics; i++)
2145
nr_entry += nr_ioapic_registers[i];
2147
ioapic_pm_state = _xmalloc(sizeof(struct IO_APIC_route_entry)*nr_entry,
2148
sizeof(struct IO_APIC_route_entry));
2149
BUG_ON(ioapic_pm_state == NULL);
2152
void __init setup_IO_APIC(void)
2157
io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2159
io_apic_irqs = ~PIC_IRQS;
2161
printk("ENABLING IO-APIC IRQs\n");
2162
printk(" -> Using %s ACK method\n", ioapic_ack_new ? "new" : "old");
2165
* Set up IO-APIC IRQ routing.
2168
setup_ioapic_ids_from_mpc();
2170
setup_IO_APIC_irqs();
2171
init_IO_APIC_traps();
2174
ioapic_pm_state_alloc();
2176
register_keyhandler('z', &print_IO_APIC_keyhandler);
2179
void ioapic_suspend(void)
2181
struct IO_APIC_route_entry *entry = ioapic_pm_state;
2182
unsigned long flags;
2185
spin_lock_irqsave(&ioapic_lock, flags);
2186
for (apic = 0; apic < nr_ioapics; apic++) {
2187
for (i = 0; i < nr_ioapic_registers[apic]; i ++, entry ++ ) {
2188
*(((int *)entry) + 1) = __io_apic_read(apic, 0x11 + 2 * i);
2189
*(((int *)entry) + 0) = __io_apic_read(apic, 0x10 + 2 * i);
2192
spin_unlock_irqrestore(&ioapic_lock, flags);
2195
void ioapic_resume(void)
2197
struct IO_APIC_route_entry *entry = ioapic_pm_state;
2198
unsigned long flags;
2199
union IO_APIC_reg_00 reg_00;
2202
spin_lock_irqsave(&ioapic_lock, flags);
2203
for (apic = 0; apic < nr_ioapics; apic++){
2204
reg_00.raw = __io_apic_read(apic, 0);
2205
if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid) {
2206
reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
2207
__io_apic_write(apic, 0, reg_00.raw);
2209
for (i = 0; i < nr_ioapic_registers[apic]; i++, entry++) {
2210
__io_apic_write(apic, 0x11+2*i, *(((int *)entry)+1));
2211
__io_apic_write(apic, 0x10+2*i, *(((int *)entry)+0));
2214
spin_unlock_irqrestore(&ioapic_lock, flags);
2217
/* --------------------------------------------------------------------------
2218
ACPI-based IOAPIC Configuration
2219
-------------------------------------------------------------------------- */
2221
#ifdef CONFIG_ACPI_BOOT
2223
int __init io_apic_get_unique_id (int ioapic, int apic_id)
2225
union IO_APIC_reg_00 reg_00;
2226
static physid_mask_t __initdata apic_id_map = PHYSID_MASK_NONE;
2228
unsigned long flags;
2232
* The P4 platform supports up to 256 APIC IDs on two separate APIC
2233
* buses (one for LAPICs, one for IOAPICs), where predecessors only
2234
* supports up to 16 on one shared APIC bus.
2236
* TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2237
* advantage of new APIC bus architecture.
2240
if (physids_empty(apic_id_map))
2241
apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2243
spin_lock_irqsave(&ioapic_lock, flags);
2244
reg_00.raw = io_apic_read(ioapic, 0);
2245
spin_unlock_irqrestore(&ioapic_lock, flags);
2247
if (apic_id >= get_physical_broadcast()) {
2248
printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2249
"%d\n", ioapic, apic_id, reg_00.bits.ID);
2250
apic_id = reg_00.bits.ID;
2254
* Every APIC in a system must have a unique ID or we get lots of nice
2255
* 'stuck on smp_invalidate_needed IPI wait' messages.
2257
if (check_apicid_used(apic_id_map, apic_id)) {
2259
for (i = 0; i < get_physical_broadcast(); i++) {
2260
if (!check_apicid_used(apic_id_map, i))
2264
if (i == get_physical_broadcast())
2265
panic("Max apic_id exceeded!\n");
2267
printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2268
"trying %d\n", ioapic, apic_id, i);
2273
tmp = apicid_to_cpu_present(apic_id);
2274
physids_or(apic_id_map, apic_id_map, tmp);
2276
if (reg_00.bits.ID != apic_id) {
2277
reg_00.bits.ID = apic_id;
2279
spin_lock_irqsave(&ioapic_lock, flags);
2280
io_apic_write(ioapic, 0, reg_00.raw);
2281
reg_00.raw = io_apic_read(ioapic, 0);
2282
spin_unlock_irqrestore(&ioapic_lock, flags);
2285
if (reg_00.bits.ID != apic_id) {
2286
printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2291
apic_printk(APIC_VERBOSE, KERN_INFO
2292
"IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2298
int __init io_apic_get_version (int ioapic)
2300
union IO_APIC_reg_01 reg_01;
2301
unsigned long flags;
2303
spin_lock_irqsave(&ioapic_lock, flags);
2304
reg_01.raw = io_apic_read(ioapic, 1);
2305
spin_unlock_irqrestore(&ioapic_lock, flags);
2307
return reg_01.bits.version;
2311
int __init io_apic_get_redir_entries (int ioapic)
2313
union IO_APIC_reg_01 reg_01;
2314
unsigned long flags;
2316
spin_lock_irqsave(&ioapic_lock, flags);
2317
reg_01.raw = io_apic_read(ioapic, 1);
2318
spin_unlock_irqrestore(&ioapic_lock, flags);
2320
return reg_01.bits.entries;
2324
int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2326
struct irq_desc *desc = irq_to_desc(irq);
2327
struct IO_APIC_route_entry entry;
2328
unsigned long flags;
2331
if (!IO_APIC_IRQ(irq)) {
2332
printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2338
* Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2339
* Note that we mask (disable) IRQs now -- these get enabled when the
2340
* corresponding device driver registers for this IRQ.
2343
memset(&entry,0,sizeof(entry));
2345
entry.delivery_mode = INT_DELIVERY_MODE;
2346
entry.dest_mode = INT_DEST_MODE;
2347
SET_DEST(entry.dest.dest32, entry.dest.logical.logical_dest,
2348
cpu_mask_to_apicid(TARGET_CPUS));
2349
entry.trigger = edge_level;
2350
entry.polarity = active_high_low;
2354
* IRQs < 16 are already in the irq_2_pin[] map
2356
if (!platform_legacy_irq(irq))
2357
add_pin_to_irq(irq, ioapic, pin);
2359
vector = assign_irq_vector(irq);
2362
entry.vector = vector;
2364
apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2365
"(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2366
mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2367
edge_level, active_high_low);
2369
ioapic_register_intr(irq, edge_level);
2371
if (!ioapic && platform_legacy_irq(irq))
2372
disable_8259A_irq(irq);
2374
spin_lock_irqsave(&ioapic_lock, flags);
2375
__ioapic_write_entry(ioapic, pin, 0, entry);
2376
set_native_irq_info(irq, TARGET_CPUS);
2377
spin_unlock(&ioapic_lock);
2379
spin_lock(&desc->lock);
2380
if (!(desc->status & (IRQ_DISABLED | IRQ_GUEST)))
2381
desc->handler->startup(irq);
2382
spin_unlock_irqrestore(&desc->lock, flags);
2387
#endif /*CONFIG_ACPI_BOOT*/
2389
static int ioapic_physbase_to_id(unsigned long physbase)
2392
for ( apic = 0; apic < nr_ioapics; apic++ )
2393
if ( mp_ioapics[apic].mpc_apicaddr == physbase )
2398
unsigned apic_gsi_base(int apic);
2400
static int apic_pin_2_gsi_irq(int apic, int pin)
2407
irq = apic_gsi_base(apic) + pin;
2409
idx = find_irq_entry(apic, pin, mp_INT);
2411
irq = pin_2_irq(idx, apic, pin);
2416
int ioapic_guest_read(unsigned long physbase, unsigned int reg, u32 *pval)
2419
unsigned long flags;
2421
if ( (apic = ioapic_physbase_to_id(physbase)) < 0 )
2424
spin_lock_irqsave(&ioapic_lock, flags);
2425
*pval = io_apic_read(apic, reg);
2426
spin_unlock_irqrestore(&ioapic_lock, flags);
2431
#define WARN_BOGUS_WRITE(f, a...) \
2432
dprintk(XENLOG_INFO, "\n%s: " \
2433
"apic=%d, pin=%d, irq=%d\n" \
2434
"%s: new_entry=%08x\n" \
2435
"%s: " f, __FUNCTION__, apic, pin, irq, \
2436
__FUNCTION__, *(u32 *)&rte, \
2437
__FUNCTION__ , ##a )
2439
int ioapic_guest_write(unsigned long physbase, unsigned int reg, u32 val)
2441
int apic, pin, irq, ret, vector, pirq;
2442
struct IO_APIC_route_entry rte = { 0 };
2443
unsigned long flags;
2444
struct irq_cfg *cfg;
2445
struct irq_desc *desc;
2447
if ( (apic = ioapic_physbase_to_id(physbase)) < 0 )
2450
/* Only write to the first half of a route entry. */
2451
if ( (reg < 0x10) || (reg & 1) )
2454
pin = (reg - 0x10) >> 1;
2456
/* Write first half from guest; second half is target info. */
2460
* What about weird destination types?
2461
* SMI: Ignore? Ought to be set up by the BIOS.
2462
* NMI: Ignore? Watchdog functionality is Xen's concern.
2463
* INIT: Definitely ignore: probably a guest OS bug.
2464
* ExtINT: Ignore? Linux only asserts this at start of day.
2465
* For now, print a message and return an error. We can fix up on demand.
2467
if ( rte.delivery_mode > dest_LowestPrio )
2469
printk("ERROR: Attempt to write weird IOAPIC destination mode!\n");
2470
printk(" APIC=%d/%d, lo-reg=%x\n", apic, pin, val);
2475
* The guest does not know physical APIC arrangement (flat vs. cluster).
2476
* Apply genapic conventions for this platform.
2478
rte.delivery_mode = INT_DELIVERY_MODE;
2479
rte.dest_mode = INT_DEST_MODE;
2481
irq = apic_pin_2_gsi_irq(apic, pin);
2485
desc = irq_to_desc(irq);
2486
cfg = desc->chip_data;
2489
* Since PHYSDEVOP_alloc_irq_vector is dummy, rte.vector is the pirq
2490
* which corresponds to this ioapic pin, retrieve it for building
2491
* pirq and irq mapping. Where the GSI is greater than 256, we assume
2492
* that dom0 pirq == irq.
2494
pirq = (irq >= 256) ? irq : rte.vector;
2495
if ( (pirq < 0) || (pirq >= dom0->nr_pirqs) )
2500
spin_lock_irqsave(&ioapic_lock, flags);
2501
ret = io_apic_read(apic, 0x10 + 2 * pin);
2502
spin_unlock_irqrestore(&ioapic_lock, flags);
2503
rte.vector = cfg->vector;
2504
if ( *(u32*)&rte != ret )
2505
WARN_BOGUS_WRITE("old_entry=%08x pirq=%d\n%s: "
2506
"Attempt to modify IO-APIC pin for in-use IRQ!\n",
2507
ret, pirq, __FUNCTION__);
2511
if ( cfg->vector <= 0 || cfg->vector > LAST_DYNAMIC_VECTOR ) {
2512
vector = assign_irq_vector(irq);
2516
printk(XENLOG_INFO "allocated vector %02x for irq %d\n", vector, irq);
2518
add_pin_to_irq(irq, apic, pin);
2520
spin_lock(&pcidevs_lock);
2521
spin_lock(&dom0->event_lock);
2522
ret = map_domain_pirq(dom0, pirq, irq,
2523
MAP_PIRQ_TYPE_GSI, NULL);
2524
spin_unlock(&dom0->event_lock);
2525
spin_unlock(&pcidevs_lock);
2529
spin_lock_irqsave(&ioapic_lock, flags);
2530
/* Set the correct irq-handling type. */
2531
desc->handler = rte.trigger ?
2532
&ioapic_level_type: &ioapic_edge_type;
2534
/* Mask iff level triggered. */
2535
rte.mask = rte.trigger;
2536
/* Set the vector field to the real vector! */
2537
rte.vector = cfg->vector;
2539
SET_DEST(rte.dest.dest32, rte.dest.logical.logical_dest,
2540
cpu_mask_to_apicid(&cfg->cpu_mask));
2542
io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&rte) + 0));
2543
io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&rte) + 1));
2545
spin_unlock_irqrestore(&ioapic_lock, flags);
2550
void dump_ioapic_irq_info(void)
2552
struct irq_pin_list *entry;
2553
struct IO_APIC_route_entry rte;
2554
unsigned int irq, pin, printed = 0;
2555
unsigned long flags;
2560
for ( irq = 0; irq < nr_irqs_gsi; irq++ )
2562
entry = &irq_2_pin[irq];
2563
if ( entry->pin == -1 )
2567
printk("IO-APIC interrupt information:\n");
2569
printk(" IRQ%3d Vec%3d:\n", irq, irq_to_vector(irq));
2575
printk(" Apic 0x%02x, Pin %2d: ", entry->apic, pin);
2577
spin_lock_irqsave(&ioapic_lock, flags);
2578
*(((int *)&rte) + 0) = io_apic_read(entry->apic, 0x10 + 2 * pin);
2579
*(((int *)&rte) + 1) = io_apic_read(entry->apic, 0x11 + 2 * pin);
2580
spin_unlock_irqrestore(&ioapic_lock, flags);
2582
printk("vector=%u, delivery_mode=%u, dest_mode=%s, "
2583
"delivery_status=%d, polarity=%d, irr=%d, "
2584
"trigger=%s, mask=%d, dest_id:%d\n",
2585
rte.vector, rte.delivery_mode,
2586
rte.dest_mode ? "logical" : "physical",
2587
rte.delivery_status, rte.polarity, rte.irr,
2588
rte.trigger ? "level" : "edge", rte.mask,
2589
rte.dest.logical.logical_dest);
2591
if ( entry->next == 0 )
2593
entry = &irq_2_pin[entry->next];
2598
unsigned highest_gsi(void);
2600
static unsigned int __initdata max_gsi_irqs;
2601
integer_param("max_gsi_irqs", max_gsi_irqs);
2603
void __init init_ioapic_mappings(void)
2605
unsigned long ioapic_phys;
2606
unsigned int i, idx = FIX_IO_APIC_BASE_0;
2607
union IO_APIC_reg_01 reg_01;
2609
if ( smp_found_config )
2611
for ( i = 0; i < nr_ioapics; i++ )
2613
if ( smp_found_config )
2615
ioapic_phys = mp_ioapics[i].mpc_apicaddr;
2618
printk(KERN_ERR "WARNING: bogus zero IO-APIC address "
2619
"found in MPTABLE, disabling IO/APIC support!\n");
2620
smp_found_config = 0;
2621
skip_ioapic_setup = 1;
2622
goto fake_ioapic_page;
2628
ioapic_phys = __pa(alloc_xenheap_page());
2629
clear_page(__va(ioapic_phys));
2631
set_fixmap_nocache(idx, ioapic_phys);
2632
apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
2633
__fix_to_virt(idx), ioapic_phys);
2636
if ( smp_found_config )
2638
/* The number of IO-APIC IRQ registers (== #pins): */
2639
reg_01.raw = io_apic_read(i, 1);
2640
nr_ioapic_registers[i] = reg_01.bits.entries + 1;
2641
nr_irqs_gsi += nr_ioapic_registers[i];
2645
nr_irqs_gsi = max(nr_irqs_gsi, highest_gsi());
2647
if ( max_gsi_irqs == 0 )
2648
max_gsi_irqs = nr_irqs ? nr_irqs / 8 : PAGE_SIZE;
2649
else if ( nr_irqs != 0 && max_gsi_irqs > nr_irqs )
2651
printk(XENLOG_WARNING "\"max_gsi_irqs=\" cannot be specified larger"
2652
" than \"nr_irqs=\"\n");
2653
max_gsi_irqs = nr_irqs;
2655
if ( max_gsi_irqs < 16 )
2658
/* for PHYSDEVOP_pirq_eoi_gmfn guest assumptions */
2659
if ( max_gsi_irqs > PAGE_SIZE * 8 )
2660
max_gsi_irqs = PAGE_SIZE * 8;
2662
if ( !smp_found_config || skip_ioapic_setup || nr_irqs_gsi < 16 )
2664
else if ( nr_irqs_gsi > max_gsi_irqs )
2666
printk(XENLOG_WARNING "Limiting to %u GSI IRQs (found %u)\n",
2667
max_gsi_irqs, nr_irqs_gsi);
2668
nr_irqs_gsi = max_gsi_irqs;
2672
nr_irqs = cpu_has_apic ?
2673
max(16U + num_present_cpus() * NR_DYNAMIC_VECTORS,
2676
else if ( nr_irqs < 16 )
2678
printk(XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n",
2679
nr_irqs_gsi, nr_irqs - nr_irqs_gsi);