cpumask: Optimize cpumask_of_cpu in arch/x86/kernel/io_apic_64.c
[linux-block.git] / arch / x86 / kernel / io_apic_64.c
CommitLineData
1da177e4
LT
1/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
1da177e4
LT
24#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
589e367f 28#include <linux/pci.h>
1da177e4
LT
29#include <linux/mc146818rtc.h>
30#include <linux/acpi.h>
31#include <linux/sysdev.h>
3b7d1921 32#include <linux/msi.h>
95d77884 33#include <linux/htirq.h>
3460a6d9 34#include <linux/dmar.h>
1d16b53e 35#include <linux/jiffies.h>
ab688059
AK
36#ifdef CONFIG_ACPI
37#include <acpi/acpi_bus.h>
38#endif
3e35a0e5 39#include <linux/bootmem.h>
1da177e4 40
61014292 41#include <asm/idle.h>
1da177e4
LT
42#include <asm/io.h>
43#include <asm/smp.h>
44#include <asm/desc.h>
45#include <asm/proto.h>
8d916406 46#include <asm/acpi.h>
ca8642f6 47#include <asm/dma.h>
3e4ff115 48#include <asm/nmi.h>
589e367f 49#include <asm/msidef.h>
8b955b0d 50#include <asm/hypertransport.h>
1da177e4 51
5af5573e 52#include <mach_ipi.h>
dd46e3ca 53#include <mach_apic.h>
5af5573e 54
13a79503
EB
55struct irq_cfg {
56 cpumask_t domain;
61014292
EB
57 cpumask_t old_domain;
58 unsigned move_cleanup_count;
13a79503 59 u8 vector;
61014292 60 u8 move_in_progress : 1;
13a79503
EB
61};
62
63/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
7223daf5 64static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
bc5e81a1
EB
65 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
66 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
67 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
68 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
69 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
70 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
71 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
72 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
73 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
74 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
75 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
76 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
77 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
78 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
79 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
80 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
13a79503
EB
81};
82
dfbffdd8 83static int assign_irq_vector(int irq, cpumask_t mask);
04b9267b 84
305b92a2
AM
85int first_system_vector = 0xfe;
86
87char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
88
1da177e4
LT
89#define __apicdebuginit __init
90
91int sis_apic_bug; /* not actually supported, dummy for compile */
92
14d98cad
AK
93static int no_timer_check;
94
fea5f1e1
LT
95static int disable_timer_pin_1 __initdata;
96
35542c5e 97int timer_through_8259 __initdata;
fea5f1e1 98
1008fddc
EB
99/* Where if anywhere is the i8259 connect in external int mode */
100static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
101
1da177e4 102static DEFINE_SPINLOCK(ioapic_lock);
70a0a535 103DEFINE_SPINLOCK(vector_lock);
1da177e4
LT
104
105/*
106 * # of IRQ routing registers
107 */
108int nr_ioapic_registers[MAX_IO_APICS];
109
9c7408f3 110/* I/O APIC entries */
ec2cd0a2 111struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
9c7408f3
AS
112int nr_ioapics;
113
350bae1d 114/* MP IRQ source entries */
2fddb6e2 115struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
350bae1d
AS
116
117/* # of MP IRQ source entries */
118int mp_irq_entries;
119
8732fc4b
AS
120DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
121
1da177e4
LT
122/*
123 * Rough estimation of how many shared IRQs there are, can
124 * be changed anytime.
125 */
e273d140 126#define MAX_PLUS_SHARED_IRQS NR_IRQS
1da177e4
LT
127#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
128
129/*
130 * This is performance-critical, we want to do it O(1)
131 *
132 * the indexing order of this array favors 1:1 mappings
133 * between pins and IRQs.
134 */
135
136static struct irq_pin_list {
137 short apic, pin, next;
138} irq_2_pin[PIN_MAP_SIZE];
139
6c0ffb9d
LT
140struct io_apic {
141 unsigned int index;
142 unsigned int unused[3];
143 unsigned int data;
144};
145
146static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
147{
148 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
ec2cd0a2 149 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
6c0ffb9d
LT
150}
151
152static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
153{
154 struct io_apic __iomem *io_apic = io_apic_base(apic);
155 writel(reg, &io_apic->index);
156 return readl(&io_apic->data);
157}
158
159static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
160{
161 struct io_apic __iomem *io_apic = io_apic_base(apic);
162 writel(reg, &io_apic->index);
163 writel(value, &io_apic->data);
164}
165
166/*
167 * Re-write a value: to be used for read-modify-write
168 * cycles where the read already set up the index register.
169 */
170static inline void io_apic_modify(unsigned int apic, unsigned int value)
171{
172 struct io_apic __iomem *io_apic = io_apic_base(apic);
173 writel(value, &io_apic->data);
174}
175
9d25cb08 176static bool io_apic_level_ack_pending(unsigned int irq)
ef3e28c5
EB
177{
178 struct irq_pin_list *entry;
179 unsigned long flags;
ef3e28c5
EB
180
181 spin_lock_irqsave(&ioapic_lock, flags);
182 entry = irq_2_pin + irq;
183 for (;;) {
184 unsigned int reg;
185 int pin;
186
187 pin = entry->pin;
188 if (pin == -1)
189 break;
190 reg = io_apic_read(entry->apic, 0x10 + pin*2);
191 /* Is the remote IRR bit set? */
46b3b4ef 192 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
9d25cb08
AM
193 spin_unlock_irqrestore(&ioapic_lock, flags);
194 return true;
195 }
ef3e28c5
EB
196 if (!entry->next)
197 break;
198 entry = irq_2_pin + entry->next;
199 }
200 spin_unlock_irqrestore(&ioapic_lock, flags);
9d25cb08
AM
201
202 return false;
ef3e28c5
EB
203}
204
6c0ffb9d
LT
205/*
206 * Synchronize the IO-APIC and the CPU by doing
207 * a dummy read from the IO-APIC
208 */
209static inline void io_apic_sync(unsigned int apic)
210{
211 struct io_apic __iomem *io_apic = io_apic_base(apic);
212 readl(&io_apic->data);
213}
214
54d5d424
AR
215#define __DO_ACTION(R, ACTION, FINAL) \
216 \
217{ \
218 int pin; \
219 struct irq_pin_list *entry = irq_2_pin + irq; \
220 \
6004e1b7 221 BUG_ON(irq >= NR_IRQS); \
54d5d424
AR
222 for (;;) { \
223 unsigned int reg; \
224 pin = entry->pin; \
225 if (pin == -1) \
226 break; \
227 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
228 reg ACTION; \
229 io_apic_modify(entry->apic, reg); \
f45bcd70 230 FINAL; \
54d5d424
AR
231 if (!entry->next) \
232 break; \
233 entry = irq_2_pin + entry->next; \
234 } \
54d5d424
AR
235}
236
eea0e11c
AK
237union entry_union {
238 struct { u32 w1, w2; };
239 struct IO_APIC_route_entry entry;
240};
241
242static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
243{
244 union entry_union eu;
245 unsigned long flags;
246 spin_lock_irqsave(&ioapic_lock, flags);
247 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
248 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
249 spin_unlock_irqrestore(&ioapic_lock, flags);
250 return eu.entry;
251}
252
48797ebd
LT
253/*
254 * When we write a new IO APIC routing entry, we need to write the high
255 * word first! If the mask bit in the low word is clear, we will enable
256 * the interrupt, and we need to make sure the entry is fully populated
257 * before that happens.
258 */
516d2836
AK
259static void
260__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
eea0e11c 261{
eea0e11c
AK
262 union entry_union eu;
263 eu.entry = e;
48797ebd
LT
264 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
265 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
516d2836
AK
266}
267
268static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
269{
270 unsigned long flags;
271 spin_lock_irqsave(&ioapic_lock, flags);
272 __ioapic_write_entry(apic, pin, e);
48797ebd
LT
273 spin_unlock_irqrestore(&ioapic_lock, flags);
274}
275
276/*
277 * When we mask an IO APIC routing entry, we need to write the low
278 * word first, in order to set the mask bit before we change the
279 * high bits!
280 */
281static void ioapic_mask_entry(int apic, int pin)
282{
283 unsigned long flags;
284 union entry_union eu = { .entry.mask = 1 };
285
eea0e11c
AK
286 spin_lock_irqsave(&ioapic_lock, flags);
287 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
288 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
289 spin_unlock_irqrestore(&ioapic_lock, flags);
290}
291
54d5d424 292#ifdef CONFIG_SMP
550f2299
EB
293static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
294{
295 int apic, pin;
296 struct irq_pin_list *entry = irq_2_pin + irq;
297
298 BUG_ON(irq >= NR_IRQS);
299 for (;;) {
300 unsigned int reg;
301 apic = entry->apic;
302 pin = entry->pin;
303 if (pin == -1)
304 break;
305 io_apic_write(apic, 0x11 + pin*2, dest);
306 reg = io_apic_read(apic, 0x10 + pin*2);
46b3b4ef 307 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
550f2299
EB
308 reg |= vector;
309 io_apic_modify(apic, reg);
310 if (!entry->next)
311 break;
312 entry = irq_2_pin + entry->next;
313 }
314}
315
54d5d424
AR
316static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
317{
dfbffdd8 318 struct irq_cfg *cfg = irq_cfg + irq;
54d5d424
AR
319 unsigned long flags;
320 unsigned int dest;
321 cpumask_t tmp;
322
323 cpus_and(tmp, mask, cpu_online_map);
324 if (cpus_empty(tmp))
5ff5115e 325 return;
54d5d424 326
dfbffdd8 327 if (assign_irq_vector(irq, mask))
550f2299
EB
328 return;
329
dfbffdd8 330 cpus_and(tmp, cfg->domain, mask);
550f2299 331 dest = cpu_mask_to_apicid(tmp);
54d5d424
AR
332
333 /*
334 * Only the high 8 bits are valid.
335 */
336 dest = SET_APIC_LOGICAL_ID(dest);
337
338 spin_lock_irqsave(&ioapic_lock, flags);
dfbffdd8 339 __target_IO_APIC_irq(irq, dest, cfg->vector);
9f0a5ba5 340 irq_desc[irq].affinity = mask;
54d5d424
AR
341 spin_unlock_irqrestore(&ioapic_lock, flags);
342}
343#endif
344
1da177e4
LT
345/*
346 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
347 * shared ISA-space IRQs, so we have to support them. We are super
348 * fast in the common case, and fast for shared ISA-space IRQs.
349 */
350static void add_pin_to_irq(unsigned int irq, int apic, int pin)
351{
352 static int first_free_entry = NR_IRQS;
353 struct irq_pin_list *entry = irq_2_pin + irq;
354
6004e1b7 355 BUG_ON(irq >= NR_IRQS);
1da177e4
LT
356 while (entry->next)
357 entry = irq_2_pin + entry->next;
358
359 if (entry->pin != -1) {
360 entry->next = first_free_entry;
361 entry = irq_2_pin + entry->next;
362 if (++first_free_entry >= PIN_MAP_SIZE)
6004e1b7 363 panic("io_apic.c: ran out of irq_2_pin entries!");
1da177e4
LT
364 }
365 entry->apic = apic;
366 entry->pin = pin;
367}
368
0b9f4f49
MR
369/*
370 * Reroute an IRQ to a different pin.
371 */
372static void __init replace_pin_at_irq(unsigned int irq,
373 int oldapic, int oldpin,
374 int newapic, int newpin)
375{
376 struct irq_pin_list *entry = irq_2_pin + irq;
377
378 while (1) {
379 if (entry->apic == oldapic && entry->pin == oldpin) {
380 entry->apic = newapic;
381 entry->pin = newpin;
382 }
383 if (!entry->next)
384 break;
385 entry = irq_2_pin + entry->next;
386 }
387}
388
1da177e4
LT
389
390#define DO_ACTION(name,R,ACTION, FINAL) \
391 \
392 static void name##_IO_APIC_irq (unsigned int irq) \
393 __DO_ACTION(R, ACTION, FINAL)
394
46b3b4ef
CG
395/* mask = 1 */
396DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
397
398/* mask = 0 */
399DO_ACTION(__unmask, 0, &= ~IO_APIC_REDIR_MASKED, )
1da177e4
LT
400
401static void mask_IO_APIC_irq (unsigned int irq)
402{
403 unsigned long flags;
404
405 spin_lock_irqsave(&ioapic_lock, flags);
406 __mask_IO_APIC_irq(irq);
407 spin_unlock_irqrestore(&ioapic_lock, flags);
408}
409
410static void unmask_IO_APIC_irq (unsigned int irq)
411{
412 unsigned long flags;
413
414 spin_lock_irqsave(&ioapic_lock, flags);
415 __unmask_IO_APIC_irq(irq);
416 spin_unlock_irqrestore(&ioapic_lock, flags);
417}
418
419static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
420{
421 struct IO_APIC_route_entry entry;
1da177e4
LT
422
423 /* Check delivery_mode to be sure we're not clearing an SMI pin */
eea0e11c 424 entry = ioapic_read_entry(apic, pin);
1da177e4
LT
425 if (entry.delivery_mode == dest_SMI)
426 return;
427 /*
428 * Disable it in the IO-APIC irq-routing table:
429 */
48797ebd 430 ioapic_mask_entry(apic, pin);
1da177e4
LT
431}
432
433static void clear_IO_APIC (void)
434{
435 int apic, pin;
436
437 for (apic = 0; apic < nr_ioapics; apic++)
438 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
439 clear_IO_APIC_pin(apic, pin);
440}
441
1da177e4
LT
442int skip_ioapic_setup;
443int ioapic_force;
444
61ec7567 445static int __init parse_noapic(char *str)
1da177e4 446{
61ec7567 447 disable_ioapic_setup();
2c8c0e6b 448 return 0;
1da177e4 449}
61ec7567 450early_param("noapic", parse_noapic);
1da177e4 451
fea5f1e1
LT
452/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
453static int __init disable_timer_pin_setup(char *arg)
454{
455 disable_timer_pin_1 = 1;
456 return 1;
457}
458__setup("disable_timer_pin_1", disable_timer_pin_setup);
459
fea5f1e1 460
1da177e4
LT
461/*
462 * Find the IRQ entry number of a certain pin.
463 */
464static int find_irq_entry(int apic, int pin, int type)
465{
466 int i;
467
468 for (i = 0; i < mp_irq_entries; i++)
2fddb6e2
AS
469 if (mp_irqs[i].mp_irqtype == type &&
470 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
471 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
472 mp_irqs[i].mp_dstirq == pin)
1da177e4
LT
473 return i;
474
475 return -1;
476}
477
478/*
479 * Find the pin to which IRQ[irq] (ISA) is connected
480 */
1008fddc 481static int __init find_isa_irq_pin(int irq, int type)
1da177e4
LT
482{
483 int i;
484
485 for (i = 0; i < mp_irq_entries; i++) {
2fddb6e2 486 int lbus = mp_irqs[i].mp_srcbus;
1da177e4 487
55f05ffa 488 if (test_bit(lbus, mp_bus_not_pci) &&
2fddb6e2
AS
489 (mp_irqs[i].mp_irqtype == type) &&
490 (mp_irqs[i].mp_srcbusirq == irq))
1da177e4 491
2fddb6e2 492 return mp_irqs[i].mp_dstirq;
1da177e4
LT
493 }
494 return -1;
495}
496
1008fddc
EB
497static int __init find_isa_irq_apic(int irq, int type)
498{
499 int i;
500
501 for (i = 0; i < mp_irq_entries; i++) {
2fddb6e2 502 int lbus = mp_irqs[i].mp_srcbus;
1008fddc 503
55f05ffa 504 if (test_bit(lbus, mp_bus_not_pci) &&
2fddb6e2
AS
505 (mp_irqs[i].mp_irqtype == type) &&
506 (mp_irqs[i].mp_srcbusirq == irq))
1008fddc
EB
507 break;
508 }
509 if (i < mp_irq_entries) {
510 int apic;
511 for(apic = 0; apic < nr_ioapics; apic++) {
2fddb6e2 512 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
1008fddc
EB
513 return apic;
514 }
515 }
516
517 return -1;
518}
519
1da177e4
LT
520/*
521 * Find a specific PCI IRQ entry.
522 * Not an __init, possibly needed by modules
523 */
524static int pin_2_irq(int idx, int apic, int pin);
525
526int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
527{
528 int apic, i, best_guess = -1;
529
530 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
531 bus, slot, pin);
ce6444d3 532 if (test_bit(bus, mp_bus_not_pci)) {
1da177e4
LT
533 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
534 return -1;
535 }
536 for (i = 0; i < mp_irq_entries; i++) {
2fddb6e2 537 int lbus = mp_irqs[i].mp_srcbus;
1da177e4
LT
538
539 for (apic = 0; apic < nr_ioapics; apic++)
2fddb6e2
AS
540 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
541 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
1da177e4
LT
542 break;
543
55f05ffa 544 if (!test_bit(lbus, mp_bus_not_pci) &&
2fddb6e2 545 !mp_irqs[i].mp_irqtype &&
1da177e4 546 (bus == lbus) &&
2fddb6e2
AS
547 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
548 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
1da177e4
LT
549
550 if (!(apic || IO_APIC_IRQ(irq)))
551 continue;
552
2fddb6e2 553 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
1da177e4
LT
554 return irq;
555 /*
556 * Use the first all-but-pin matching entry as a
557 * best-guess fuzzy result for broken mptables.
558 */
559 if (best_guess < 0)
560 best_guess = irq;
561 }
562 }
6004e1b7 563 BUG_ON(best_guess >= NR_IRQS);
1da177e4
LT
564 return best_guess;
565}
566
1da177e4
LT
567/* ISA interrupts are always polarity zero edge triggered,
568 * when listed as conforming in the MP table. */
569
570#define default_ISA_trigger(idx) (0)
571#define default_ISA_polarity(idx) (0)
572
573/* PCI interrupts are always polarity one level triggered,
574 * when listed as conforming in the MP table. */
575
576#define default_PCI_trigger(idx) (1)
577#define default_PCI_polarity(idx) (1)
578
61fd47e0 579static int MPBIOS_polarity(int idx)
1da177e4 580{
2fddb6e2 581 int bus = mp_irqs[idx].mp_srcbus;
1da177e4
LT
582 int polarity;
583
584 /*
585 * Determine IRQ line polarity (high active or low active):
586 */
2fddb6e2 587 switch (mp_irqs[idx].mp_irqflag & 3)
1da177e4
LT
588 {
589 case 0: /* conforms, ie. bus-type dependent polarity */
55f05ffa
AK
590 if (test_bit(bus, mp_bus_not_pci))
591 polarity = default_ISA_polarity(idx);
592 else
593 polarity = default_PCI_polarity(idx);
1da177e4 594 break;
1da177e4
LT
595 case 1: /* high active */
596 {
597 polarity = 0;
598 break;
599 }
600 case 2: /* reserved */
601 {
602 printk(KERN_WARNING "broken BIOS!!\n");
603 polarity = 1;
604 break;
605 }
606 case 3: /* low active */
607 {
608 polarity = 1;
609 break;
610 }
611 default: /* invalid */
612 {
613 printk(KERN_WARNING "broken BIOS!!\n");
614 polarity = 1;
615 break;
616 }
617 }
618 return polarity;
619}
620
621static int MPBIOS_trigger(int idx)
622{
2fddb6e2 623 int bus = mp_irqs[idx].mp_srcbus;
1da177e4
LT
624 int trigger;
625
626 /*
627 * Determine IRQ trigger mode (edge or level sensitive):
628 */
2fddb6e2 629 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
1da177e4
LT
630 {
631 case 0: /* conforms, ie. bus-type dependent */
55f05ffa
AK
632 if (test_bit(bus, mp_bus_not_pci))
633 trigger = default_ISA_trigger(idx);
634 else
635 trigger = default_PCI_trigger(idx);
1da177e4 636 break;
1da177e4
LT
637 case 1: /* edge */
638 {
639 trigger = 0;
640 break;
641 }
642 case 2: /* reserved */
643 {
644 printk(KERN_WARNING "broken BIOS!!\n");
645 trigger = 1;
646 break;
647 }
648 case 3: /* level */
649 {
650 trigger = 1;
651 break;
652 }
653 default: /* invalid */
654 {
655 printk(KERN_WARNING "broken BIOS!!\n");
656 trigger = 0;
657 break;
658 }
659 }
660 return trigger;
661}
662
663static inline int irq_polarity(int idx)
664{
665 return MPBIOS_polarity(idx);
666}
667
668static inline int irq_trigger(int idx)
669{
670 return MPBIOS_trigger(idx);
671}
672
673static int pin_2_irq(int idx, int apic, int pin)
674{
675 int irq, i;
2fddb6e2 676 int bus = mp_irqs[idx].mp_srcbus;
1da177e4
LT
677
678 /*
679 * Debugging check, we are in big trouble if this message pops up!
680 */
2fddb6e2 681 if (mp_irqs[idx].mp_dstirq != pin)
1da177e4
LT
682 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
683
55f05ffa 684 if (test_bit(bus, mp_bus_not_pci)) {
2fddb6e2 685 irq = mp_irqs[idx].mp_srcbusirq;
55f05ffa
AK
686 } else {
687 /*
688 * PCI IRQs are mapped in order
689 */
690 i = irq = 0;
691 while (i < apic)
692 irq += nr_ioapic_registers[i++];
693 irq += pin;
1da177e4 694 }
6004e1b7 695 BUG_ON(irq >= NR_IRQS);
1da177e4
LT
696 return irq;
697}
698
dfbffdd8 699static int __assign_irq_vector(int irq, cpumask_t mask)
1da177e4 700{
550f2299
EB
701 /*
702 * NOTE! The local APIC isn't very good at handling
703 * multiple interrupts at the same interrupt level.
704 * As the interrupt level is determined by taking the
705 * vector number and shifting that right by 4, we
706 * want to spread these out a bit so that they don't
707 * all fall in the same interrupt level.
708 *
709 * Also, we've got to be careful not to trash gate
710 * 0x80, because int 0x80 is hm, kind of importantish. ;)
711 */
d1752aa8 712 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
dfbffdd8 713 unsigned int old_vector;
550f2299 714 int cpu;
13a79503 715 struct irq_cfg *cfg;
1da177e4 716
e273d140 717 BUG_ON((unsigned)irq >= NR_IRQS);
13a79503 718 cfg = &irq_cfg[irq];
0a1ad60d 719
70a0a535
EB
720 /* Only try and allocate irqs on cpus that are present */
721 cpus_and(mask, mask, cpu_online_map);
722
61014292
EB
723 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
724 return -EBUSY;
725
dfbffdd8
EB
726 old_vector = cfg->vector;
727 if (old_vector) {
728 cpumask_t tmp;
729 cpus_and(tmp, cfg->domain, mask);
730 if (!cpus_empty(tmp))
731 return 0;
0a1ad60d 732 }
550f2299 733
334ef7a7 734 for_each_cpu_mask_nr(cpu, mask) {
70a0a535 735 cpumask_t domain, new_mask;
61014292 736 int new_cpu;
550f2299 737 int vector, offset;
c7111c13
EB
738
739 domain = vector_allocation_domain(cpu);
70a0a535 740 cpus_and(new_mask, domain, cpu_online_map);
c7111c13 741
d1752aa8
EB
742 vector = current_vector;
743 offset = current_offset;
1da177e4 744next:
550f2299 745 vector += 8;
305b92a2 746 if (vector >= first_system_vector) {
550f2299
EB
747 /* If we run out of vectors on large boxen, must share them. */
748 offset = (offset + 1) % 8;
749 vector = FIRST_DEVICE_VECTOR + offset;
750 }
d1752aa8 751 if (unlikely(current_vector == vector))
550f2299
EB
752 continue;
753 if (vector == IA32_SYSCALL_VECTOR)
754 goto next;
334ef7a7 755 for_each_cpu_mask_nr(new_cpu, new_mask)
45edfd1d 756 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
c7111c13 757 goto next;
550f2299 758 /* Found one! */
d1752aa8
EB
759 current_vector = vector;
760 current_offset = offset;
61014292
EB
761 if (old_vector) {
762 cfg->move_in_progress = 1;
763 cfg->old_domain = cfg->domain;
764 }
334ef7a7 765 for_each_cpu_mask_nr(new_cpu, new_mask)
c7111c13 766 per_cpu(vector_irq, new_cpu)[vector] = irq;
13a79503
EB
767 cfg->vector = vector;
768 cfg->domain = domain;
dfbffdd8 769 return 0;
1da177e4 770 }
550f2299 771 return -ENOSPC;
04b9267b
EB
772}
773
dfbffdd8 774static int assign_irq_vector(int irq, cpumask_t mask)
04b9267b 775{
dfbffdd8 776 int err;
04b9267b 777 unsigned long flags;
0a1ad60d 778
04b9267b 779 spin_lock_irqsave(&vector_lock, flags);
dfbffdd8 780 err = __assign_irq_vector(irq, mask);
26a3c49c 781 spin_unlock_irqrestore(&vector_lock, flags);
dfbffdd8 782 return err;
1da177e4
LT
783}
784
5df0287e
YL
785static void __clear_irq_vector(int irq)
786{
13a79503 787 struct irq_cfg *cfg;
5df0287e
YL
788 cpumask_t mask;
789 int cpu, vector;
790
13a79503
EB
791 BUG_ON((unsigned)irq >= NR_IRQS);
792 cfg = &irq_cfg[irq];
793 BUG_ON(!cfg->vector);
5df0287e 794
13a79503
EB
795 vector = cfg->vector;
796 cpus_and(mask, cfg->domain, cpu_online_map);
334ef7a7 797 for_each_cpu_mask_nr(cpu, mask)
5df0287e
YL
798 per_cpu(vector_irq, cpu)[vector] = -1;
799
13a79503 800 cfg->vector = 0;
d366f8cb 801 cpus_clear(cfg->domain);
5df0287e
YL
802}
803
3fde6900 804static void __setup_vector_irq(int cpu)
70a0a535
EB
805{
806 /* Initialize vector_irq on a new cpu */
807 /* This function must be called with vector_lock held */
70a0a535
EB
808 int irq, vector;
809
70a0a535 810 /* Mark the inuse vectors */
e273d140 811 for (irq = 0; irq < NR_IRQS; ++irq) {
13a79503 812 if (!cpu_isset(cpu, irq_cfg[irq].domain))
70a0a535 813 continue;
13a79503 814 vector = irq_cfg[irq].vector;
70a0a535
EB
815 per_cpu(vector_irq, cpu)[vector] = irq;
816 }
817 /* Mark the free vectors */
818 for (vector = 0; vector < NR_VECTORS; ++vector) {
819 irq = per_cpu(vector_irq, cpu)[vector];
820 if (irq < 0)
821 continue;
13a79503 822 if (!cpu_isset(cpu, irq_cfg[irq].domain))
70a0a535
EB
823 per_cpu(vector_irq, cpu)[vector] = -1;
824 }
825}
826
3fde6900
GC
827void setup_vector_irq(int cpu)
828{
829 spin_lock(&vector_lock);
830 __setup_vector_irq(smp_processor_id());
831 spin_unlock(&vector_lock);
832}
833
70a0a535 834
f29bd1ba 835static struct irq_chip ioapic_chip;
1da177e4 836
a27bc06d 837static void ioapic_register_intr(int irq, unsigned long trigger)
1da177e4 838{
cc75b92d
TG
839 if (trigger) {
840 irq_desc[irq].status |= IRQ_LEVEL;
a460e745
IM
841 set_irq_chip_and_handler_name(irq, &ioapic_chip,
842 handle_fasteoi_irq, "fasteoi");
cc75b92d
TG
843 } else {
844 irq_desc[irq].status &= ~IRQ_LEVEL;
a460e745
IM
845 set_irq_chip_and_handler_name(irq, &ioapic_chip,
846 handle_edge_irq, "edge");
cc75b92d 847 }
1da177e4 848}
a8c8a367
EB
849
850static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
851 int trigger, int polarity)
1da177e4 852{
dfbffdd8 853 struct irq_cfg *cfg = irq_cfg + irq;
1da177e4 854 struct IO_APIC_route_entry entry;
a8c8a367 855 cpumask_t mask;
1da177e4 856
a8c8a367
EB
857 if (!IO_APIC_IRQ(irq))
858 return;
859
dfbffdd8
EB
860 mask = TARGET_CPUS;
861 if (assign_irq_vector(irq, mask))
a8c8a367
EB
862 return;
863
dfbffdd8
EB
864 cpus_and(mask, cfg->domain, mask);
865
a8c8a367
EB
866 apic_printk(APIC_VERBOSE,KERN_DEBUG
867 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
868 "IRQ %d Mode:%i Active:%i)\n",
ec2cd0a2 869 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
a8c8a367 870 irq, trigger, polarity);
1da177e4 871
ad892f5e
YL
872 /*
873 * add it to the IO-APIC irq-routing table:
874 */
875 memset(&entry,0,sizeof(entry));
1da177e4 876
ad892f5e
YL
877 entry.delivery_mode = INT_DELIVERY_MODE;
878 entry.dest_mode = INT_DEST_MODE;
a8c8a367 879 entry.dest = cpu_mask_to_apicid(mask);
ad892f5e 880 entry.mask = 0; /* enable IRQ */
a8c8a367
EB
881 entry.trigger = trigger;
882 entry.polarity = polarity;
dfbffdd8 883 entry.vector = cfg->vector;
1da177e4 884
a8c8a367
EB
885 /* Mask level triggered irqs.
886 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
887 */
888 if (trigger)
ad892f5e 889 entry.mask = 1;
ad892f5e 890
a8c8a367
EB
891 ioapic_register_intr(irq, trigger);
892 if (irq < 16)
893 disable_8259A_irq(irq);
ad892f5e
YL
894
895 ioapic_write_entry(apic, pin, entry);
ad892f5e
YL
896}
897
898static void __init setup_IO_APIC_irqs(void)
899{
900 int apic, pin, idx, irq, first_notcon = 1;
901
902 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
903
904 for (apic = 0; apic < nr_ioapics; apic++) {
905 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1da177e4
LT
906
907 idx = find_irq_entry(apic,pin,mp_INT);
908 if (idx == -1) {
909 if (first_notcon) {
ec2cd0a2 910 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
1da177e4
LT
911 first_notcon = 0;
912 } else
ec2cd0a2 913 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
1da177e4
LT
914 continue;
915 }
20d225b9
YL
916 if (!first_notcon) {
917 apic_printk(APIC_VERBOSE, " not connected.\n");
918 first_notcon = 1;
919 }
1da177e4 920
1da177e4
LT
921 irq = pin_2_irq(idx, apic, pin);
922 add_pin_to_irq(irq, apic, pin);
923
a8c8a367
EB
924 setup_IO_APIC_irq(apic, pin, irq,
925 irq_trigger(idx), irq_polarity(idx));
1da177e4
LT
926 }
927 }
928
929 if (!first_notcon)
20d225b9 930 apic_printk(APIC_VERBOSE, " not connected.\n");
1da177e4
LT
931}
932
933/*
f7633ce5 934 * Set up the timer pin, possibly with the 8259A-master behind.
1da177e4 935 */
f7633ce5
MR
936static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
937 int vector)
1da177e4
LT
938{
939 struct IO_APIC_route_entry entry;
1da177e4 940
a2249cba 941 memset(&entry, 0, sizeof(entry));
1da177e4 942
1da177e4
LT
943 /*
944 * We use logical delivery to get the timer IRQ
945 * to the first CPU.
946 */
947 entry.dest_mode = INT_DEST_MODE;
03be7505 948 entry.mask = 1; /* mask IRQ now */
ee4eff6f 949 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1da177e4
LT
950 entry.delivery_mode = INT_DELIVERY_MODE;
951 entry.polarity = 0;
952 entry.trigger = 0;
953 entry.vector = vector;
954
955 /*
956 * The timer IRQ doesn't have to know that behind the
f7633ce5 957 * scene we may have a 8259A-master in AEOI mode ...
1da177e4 958 */
a460e745 959 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1da177e4
LT
960
961 /*
962 * Add it to the IO-APIC irq-routing table:
963 */
a2249cba 964 ioapic_write_entry(apic, pin, entry);
1da177e4
LT
965}
966
1da177e4
LT
967void __apicdebuginit print_IO_APIC(void)
968{
969 int apic, i;
970 union IO_APIC_reg_00 reg_00;
971 union IO_APIC_reg_01 reg_01;
972 union IO_APIC_reg_02 reg_02;
973 unsigned long flags;
974
975 if (apic_verbosity == APIC_QUIET)
976 return;
977
978 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
979 for (i = 0; i < nr_ioapics; i++)
980 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
ec2cd0a2 981 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
1da177e4
LT
982
983 /*
984 * We are a bit conservative about what we expect. We have to
985 * know about every hardware change ASAP.
986 */
987 printk(KERN_INFO "testing the IO APIC.......................\n");
988
989 for (apic = 0; apic < nr_ioapics; apic++) {
990
991 spin_lock_irqsave(&ioapic_lock, flags);
992 reg_00.raw = io_apic_read(apic, 0);
993 reg_01.raw = io_apic_read(apic, 1);
994 if (reg_01.bits.version >= 0x10)
995 reg_02.raw = io_apic_read(apic, 2);
996 spin_unlock_irqrestore(&ioapic_lock, flags);
997
998 printk("\n");
ec2cd0a2 999 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1da177e4
LT
1000 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1001 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1da177e4
LT
1002
1003 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1004 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1da177e4
LT
1005
1006 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1007 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1da177e4
LT
1008
1009 if (reg_01.bits.version >= 0x10) {
1010 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1011 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1da177e4
LT
1012 }
1013
1014 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1015
ee4eff6f
BR
1016 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1017 " Stat Dmod Deli Vect: \n");
1da177e4
LT
1018
1019 for (i = 0; i <= reg_01.bits.entries; i++) {
1020 struct IO_APIC_route_entry entry;
1021
eea0e11c 1022 entry = ioapic_read_entry(apic, i);
1da177e4 1023
ee4eff6f 1024 printk(KERN_DEBUG " %02x %03X ",
1da177e4 1025 i,
ee4eff6f 1026 entry.dest
1da177e4
LT
1027 );
1028
1029 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1030 entry.mask,
1031 entry.trigger,
1032 entry.irr,
1033 entry.polarity,
1034 entry.delivery_status,
1035 entry.dest_mode,
1036 entry.delivery_mode,
1037 entry.vector
1038 );
1039 }
1040 }
1da177e4
LT
1041 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1042 for (i = 0; i < NR_IRQS; i++) {
1043 struct irq_pin_list *entry = irq_2_pin + i;
1044 if (entry->pin < 0)
1045 continue;
04b9267b 1046 printk(KERN_DEBUG "IRQ%d ", i);
1da177e4
LT
1047 for (;;) {
1048 printk("-> %d:%d", entry->apic, entry->pin);
1049 if (!entry->next)
1050 break;
1051 entry = irq_2_pin + entry->next;
1052 }
1053 printk("\n");
1054 }
1055
1056 printk(KERN_INFO ".................................... done.\n");
1057
1058 return;
1059}
1060
1061#if 0
1062
1063static __apicdebuginit void print_APIC_bitfield (int base)
1064{
1065 unsigned int v;
1066 int i, j;
1067
1068 if (apic_verbosity == APIC_QUIET)
1069 return;
1070
1071 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1072 for (i = 0; i < 8; i++) {
1073 v = apic_read(base + i*0x10);
1074 for (j = 0; j < 32; j++) {
1075 if (v & (1<<j))
1076 printk("1");
1077 else
1078 printk("0");
1079 }
1080 printk("\n");
1081 }
1082}
1083
1084void __apicdebuginit print_local_APIC(void * dummy)
1085{
1086 unsigned int v, ver, maxlvt;
1087
1088 if (apic_verbosity == APIC_QUIET)
1089 return;
1090
1091 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1092 smp_processor_id(), hard_smp_processor_id());
66823114 1093 v = apic_read(APIC_ID);
05f2d12c 1094 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(read_apic_id()));
1da177e4
LT
1095 v = apic_read(APIC_LVR);
1096 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1097 ver = GET_APIC_VERSION(v);
37e650c7 1098 maxlvt = lapic_get_maxlvt();
1da177e4
LT
1099
1100 v = apic_read(APIC_TASKPRI);
1101 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1102
5a40b7c2
AK
1103 v = apic_read(APIC_ARBPRI);
1104 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1105 v & APIC_ARBPRI_MASK);
1106 v = apic_read(APIC_PROCPRI);
1107 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1da177e4
LT
1108
1109 v = apic_read(APIC_EOI);
1110 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1111 v = apic_read(APIC_RRR);
1112 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1113 v = apic_read(APIC_LDR);
1114 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1115 v = apic_read(APIC_DFR);
1116 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1117 v = apic_read(APIC_SPIV);
1118 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1119
1120 printk(KERN_DEBUG "... APIC ISR field:\n");
1121 print_APIC_bitfield(APIC_ISR);
1122 printk(KERN_DEBUG "... APIC TMR field:\n");
1123 print_APIC_bitfield(APIC_TMR);
1124 printk(KERN_DEBUG "... APIC IRR field:\n");
1125 print_APIC_bitfield(APIC_IRR);
1126
5a40b7c2
AK
1127 v = apic_read(APIC_ESR);
1128 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1da177e4
LT
1129
1130 v = apic_read(APIC_ICR);
1131 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1132 v = apic_read(APIC_ICR2);
1133 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1134
1135 v = apic_read(APIC_LVTT);
1136 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1137
1138 if (maxlvt > 3) { /* PC is LVT#4. */
1139 v = apic_read(APIC_LVTPC);
1140 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1141 }
1142 v = apic_read(APIC_LVT0);
1143 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1144 v = apic_read(APIC_LVT1);
1145 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1146
1147 if (maxlvt > 2) { /* ERR is LVT#3. */
1148 v = apic_read(APIC_LVTERR);
1149 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1150 }
1151
1152 v = apic_read(APIC_TMICT);
1153 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1154 v = apic_read(APIC_TMCCT);
1155 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1156 v = apic_read(APIC_TDCR);
1157 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1158 printk("\n");
1159}
1160
1161void print_all_local_APICs (void)
1162{
15c8b6c1 1163 on_each_cpu(print_local_APIC, NULL, 1);
1da177e4
LT
1164}
1165
1166void __apicdebuginit print_PIC(void)
1167{
1da177e4
LT
1168 unsigned int v;
1169 unsigned long flags;
1170
1171 if (apic_verbosity == APIC_QUIET)
1172 return;
1173
1174 printk(KERN_DEBUG "\nprinting PIC contents\n");
1175
1176 spin_lock_irqsave(&i8259A_lock, flags);
1177
1178 v = inb(0xa1) << 8 | inb(0x21);
1179 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1180
1181 v = inb(0xa0) << 8 | inb(0x20);
1182 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1183
1184 outb(0x0b,0xa0);
1185 outb(0x0b,0x20);
1186 v = inb(0xa0) << 8 | inb(0x20);
1187 outb(0x0a,0xa0);
1188 outb(0x0a,0x20);
1189
1190 spin_unlock_irqrestore(&i8259A_lock, flags);
1191
1192 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1193
1194 v = inb(0x4d1) << 8 | inb(0x4d0);
1195 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1196}
1197
1198#endif /* 0 */
1199
1c69524c 1200void __init enable_IO_APIC(void)
1da177e4
LT
1201{
1202 union IO_APIC_reg_01 reg_01;
1008fddc
EB
1203 int i8259_apic, i8259_pin;
1204 int i, apic;
1da177e4
LT
1205 unsigned long flags;
1206
1207 for (i = 0; i < PIN_MAP_SIZE; i++) {
1208 irq_2_pin[i].pin = -1;
1209 irq_2_pin[i].next = 0;
1210 }
1da177e4
LT
1211
1212 /*
1213 * The number of IO-APIC IRQ registers (== #pins):
1214 */
1008fddc 1215 for (apic = 0; apic < nr_ioapics; apic++) {
1da177e4 1216 spin_lock_irqsave(&ioapic_lock, flags);
1008fddc 1217 reg_01.raw = io_apic_read(apic, 1);
1da177e4 1218 spin_unlock_irqrestore(&ioapic_lock, flags);
1008fddc
EB
1219 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1220 }
1221 for(apic = 0; apic < nr_ioapics; apic++) {
1222 int pin;
1223 /* See if any of the pins is in ExtINT mode */
1224 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1225 struct IO_APIC_route_entry entry;
eea0e11c 1226 entry = ioapic_read_entry(apic, pin);
1008fddc
EB
1227
1228 /* If the interrupt line is enabled and in ExtInt mode
1229 * I have found the pin where the i8259 is connected.
1230 */
1231 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1232 ioapic_i8259.apic = apic;
1233 ioapic_i8259.pin = pin;
1234 goto found_i8259;
1235 }
1236 }
1237 }
1238 found_i8259:
1239 /* Look to see what if the MP table has reported the ExtINT */
1240 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1241 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1242 /* Trust the MP table if nothing is setup in the hardware */
1243 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1244 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1245 ioapic_i8259.pin = i8259_pin;
1246 ioapic_i8259.apic = i8259_apic;
1247 }
1248 /* Complain if the MP table and the hardware disagree */
1249 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1250 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1251 {
1252 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1da177e4
LT
1253 }
1254
1255 /*
1256 * Do not trust the IO-APIC being empty at bootup
1257 */
1258 clear_IO_APIC();
1259}
1260
1261/*
1262 * Not an __init, needed by the reboot code
1263 */
1264void disable_IO_APIC(void)
1265{
1266 /*
1267 * Clear the IO-APIC before rebooting:
1268 */
1269 clear_IO_APIC();
1270
208fb931 1271 /*
0b968d23 1272 * If the i8259 is routed through an IOAPIC
208fb931 1273 * Put that IOAPIC in virtual wire mode
0b968d23 1274 * so legacy interrupts can be delivered.
208fb931 1275 */
1008fddc 1276 if (ioapic_i8259.pin != -1) {
208fb931 1277 struct IO_APIC_route_entry entry;
208fb931
EB
1278
1279 memset(&entry, 0, sizeof(entry));
1280 entry.mask = 0; /* Enabled */
1281 entry.trigger = 0; /* Edge */
1282 entry.irr = 0;
1283 entry.polarity = 0; /* High */
1284 entry.delivery_status = 0;
1285 entry.dest_mode = 0; /* Physical */
1008fddc 1286 entry.delivery_mode = dest_ExtINT; /* ExtInt */
208fb931 1287 entry.vector = 0;
05f2d12c 1288 entry.dest = GET_APIC_ID(read_apic_id());
208fb931 1289
208fb931
EB
1290 /*
1291 * Add it to the IO-APIC irq-routing table:
1292 */
eea0e11c 1293 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
208fb931
EB
1294 }
1295
1008fddc 1296 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1da177e4
LT
1297}
1298
1da177e4
LT
1299/*
1300 * There is a nasty bug in some older SMP boards, their mptable lies
1301 * about the timer IRQ. We do the following to work around the situation:
1302 *
1303 * - timer IRQ defaults to IO-APIC IRQ
1304 * - if this function detects that timer IRQs are defunct, then we fall
1305 * back to ISA timer IRQs
1306 */
1307static int __init timer_irq_works(void)
1308{
1309 unsigned long t1 = jiffies;
4aae0702 1310 unsigned long flags;
1da177e4 1311
4aae0702 1312 local_save_flags(flags);
1da177e4
LT
1313 local_irq_enable();
1314 /* Let ten ticks pass... */
1315 mdelay((10 * 1000) / HZ);
4aae0702 1316 local_irq_restore(flags);
1da177e4
LT
1317
1318 /*
1319 * Expect a few ticks at least, to be sure some possible
1320 * glue logic does not lock up after one or two first
1321 * ticks in a non-ExtINT mode. Also the local APIC
1322 * might have cached one ExtINT interrupt. Finally, at
1323 * least one tick may be lost due to delays.
1324 */
1325
1326 /* jiffies wrap? */
1d16b53e 1327 if (time_after(jiffies, t1 + 4))
1da177e4
LT
1328 return 1;
1329 return 0;
1330}
1331
1332/*
1333 * In the SMP+IOAPIC case it might happen that there are an unspecified
1334 * number of pending IRQ events unhandled. These cases are very rare,
1335 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1336 * better to do it this way as thus we do not have to be aware of
1337 * 'pending' interrupts in the IRQ path, except at this point.
1338 */
1339/*
1340 * Edge triggered needs to resend any interrupt
1341 * that was delayed but this is now handled in the device
1342 * independent code.
1343 */
1344
1345/*
1346 * Starting up a edge-triggered IO-APIC interrupt is
1347 * nasty - we need to make sure that we get the edge.
1348 * If it is already asserted for some reason, we need
1349 * return 1 to indicate that is was pending.
1350 *
1351 * This is not complete - we should be able to fake
1352 * an edge even if it isn't on the 8259A...
1353 */
1354
f29bd1ba 1355static unsigned int startup_ioapic_irq(unsigned int irq)
1da177e4
LT
1356{
1357 int was_pending = 0;
1358 unsigned long flags;
1359
1360 spin_lock_irqsave(&ioapic_lock, flags);
1361 if (irq < 16) {
1362 disable_8259A_irq(irq);
1363 if (i8259A_irq_pending(irq))
1364 was_pending = 1;
1365 }
1366 __unmask_IO_APIC_irq(irq);
1367 spin_unlock_irqrestore(&ioapic_lock, flags);
1368
1369 return was_pending;
1370}
1371
04b9267b 1372static int ioapic_retrigger_irq(unsigned int irq)
c0ad90a3 1373{
13a79503 1374 struct irq_cfg *cfg = &irq_cfg[irq];
6bf2dafa 1375 unsigned long flags;
550f2299 1376
6bf2dafa 1377 spin_lock_irqsave(&vector_lock, flags);
cb6d2be6 1378 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
6bf2dafa 1379 spin_unlock_irqrestore(&vector_lock, flags);
c0ad90a3
IM
1380
1381 return 1;
1382}
1383
1da177e4
LT
1384/*
1385 * Level and edge triggered IO-APIC interrupts need different handling,
1386 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1387 * handled with the level-triggered descriptor, but that one has slightly
1388 * more overhead. Level-triggered interrupts cannot be handled with the
1389 * edge-triggered handler, without risking IRQ storms and other ugly
1390 * races.
1391 */
1392
61014292
EB
1393#ifdef CONFIG_SMP
1394asmlinkage void smp_irq_move_cleanup_interrupt(void)
1395{
1396 unsigned vector, me;
1397 ack_APIC_irq();
1398 exit_idle();
1399 irq_enter();
1400
1401 me = smp_processor_id();
1402 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1403 unsigned int irq;
1404 struct irq_desc *desc;
1405 struct irq_cfg *cfg;
1406 irq = __get_cpu_var(vector_irq)[vector];
1407 if (irq >= NR_IRQS)
1408 continue;
1409
1410 desc = irq_desc + irq;
1411 cfg = irq_cfg + irq;
1412 spin_lock(&desc->lock);
1413 if (!cfg->move_cleanup_count)
1414 goto unlock;
1415
1416 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1417 goto unlock;
1418
1419 __get_cpu_var(vector_irq)[vector] = -1;
1420 cfg->move_cleanup_count--;
1421unlock:
1422 spin_unlock(&desc->lock);
1423 }
1424
1425 irq_exit();
1426}
1427
1428static void irq_complete_move(unsigned int irq)
1429{
1430 struct irq_cfg *cfg = irq_cfg + irq;
1431 unsigned vector, me;
1432
1433 if (likely(!cfg->move_in_progress))
1434 return;
1435
65ea5b03 1436 vector = ~get_irq_regs()->orig_ax;
61014292 1437 me = smp_processor_id();
f0e13ae7 1438 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
61014292
EB
1439 cpumask_t cleanup_mask;
1440
1441 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1442 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1443 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1444 cfg->move_in_progress = 0;
1445 }
1446}
1447#else
1448static inline void irq_complete_move(unsigned int irq) {}
1449#endif
1450
0be6652f
EB
1451static void ack_apic_edge(unsigned int irq)
1452{
61014292 1453 irq_complete_move(irq);
0be6652f
EB
1454 move_native_irq(irq);
1455 ack_APIC_irq();
1456}
1457
1458static void ack_apic_level(unsigned int irq)
1459{
1460 int do_unmask_irq = 0;
1461
61014292 1462 irq_complete_move(irq);
52e3d90d 1463#ifdef CONFIG_GENERIC_PENDING_IRQ
0be6652f
EB
1464 /* If we are moving the irq we need to mask it */
1465 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1466 do_unmask_irq = 1;
1467 mask_IO_APIC_irq(irq);
1468 }
1469#endif
1470
1471 /*
1472 * We must acknowledge the irq before we move it or the acknowledge will
beb7dd86 1473 * not propagate properly.
0be6652f
EB
1474 */
1475 ack_APIC_irq();
1476
1477 /* Now we can move and renable the irq */
ef3e28c5
EB
1478 if (unlikely(do_unmask_irq)) {
1479 /* Only migrate the irq if the ack has been received.
1480 *
1481 * On rare occasions the broadcast level triggered ack gets
1482 * delayed going to ioapics, and if we reprogram the
1483 * vector while Remote IRR is still set the irq will never
1484 * fire again.
1485 *
1486 * To prevent this scenario we read the Remote IRR bit
1487 * of the ioapic. This has two effects.
1488 * - On any sane system the read of the ioapic will
1489 * flush writes (and acks) going to the ioapic from
1490 * this cpu.
1491 * - We get to see if the ACK has actually been delivered.
1492 *
1493 * Based on failed experiments of reprogramming the
1494 * ioapic entry from outside of irq context starting
1495 * with masking the ioapic entry and then polling until
1496 * Remote IRR was clear before reprogramming the
1497 * ioapic I don't trust the Remote IRR bit to be
1498 * completey accurate.
1499 *
1500 * However there appears to be no other way to plug
1501 * this race, so if the Remote IRR bit is not
1502 * accurate and is causing problems then it is a hardware bug
1503 * and you can go talk to the chipset vendor about it.
1504 */
1505 if (!io_apic_level_ack_pending(irq))
1506 move_masked_irq(irq);
0be6652f 1507 unmask_IO_APIC_irq(irq);
ef3e28c5 1508 }
0be6652f
EB
1509}
1510
f29bd1ba
IM
1511static struct irq_chip ioapic_chip __read_mostly = {
1512 .name = "IO-APIC",
04b9267b
EB
1513 .startup = startup_ioapic_irq,
1514 .mask = mask_IO_APIC_irq,
1515 .unmask = unmask_IO_APIC_irq,
0be6652f
EB
1516 .ack = ack_apic_edge,
1517 .eoi = ack_apic_level,
54d5d424 1518#ifdef CONFIG_SMP
04b9267b 1519 .set_affinity = set_ioapic_affinity_irq,
54d5d424 1520#endif
04b9267b 1521 .retrigger = ioapic_retrigger_irq,
1da177e4
LT
1522};
1523
1524static inline void init_IO_APIC_traps(void)
1525{
1526 int irq;
1527
1528 /*
1529 * NOTE! The local APIC isn't very good at handling
1530 * multiple interrupts at the same interrupt level.
1531 * As the interrupt level is determined by taking the
1532 * vector number and shifting that right by 4, we
1533 * want to spread these out a bit so that they don't
1534 * all fall in the same interrupt level.
1535 *
1536 * Also, we've got to be careful not to trash gate
1537 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1538 */
1539 for (irq = 0; irq < NR_IRQS ; irq++) {
addfc66b 1540 if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
1da177e4
LT
1541 /*
1542 * Hmm.. We don't have an entry for this,
1543 * so default to an old-fashioned 8259
1544 * interrupt if we can..
1545 */
1546 if (irq < 16)
1547 make_8259A_irq(irq);
1548 else
1549 /* Strange. Oh, well.. */
f29bd1ba 1550 irq_desc[irq].chip = &no_irq_chip;
1da177e4
LT
1551 }
1552 }
1553}
1554
c88ac1df 1555static void unmask_lapic_irq(unsigned int irq)
1da177e4
LT
1556{
1557 unsigned long v;
1558
1559 v = apic_read(APIC_LVT0);
11a8e778 1560 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1da177e4
LT
1561}
1562
c88ac1df 1563static void mask_lapic_irq(unsigned int irq)
1da177e4
LT
1564{
1565 unsigned long v;
1566
1567 v = apic_read(APIC_LVT0);
11a8e778 1568 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1da177e4
LT
1569}
1570
1571static void ack_lapic_irq (unsigned int irq)
1572{
1573 ack_APIC_irq();
1574}
1575
c88ac1df
MR
1576static struct irq_chip lapic_chip __read_mostly = {
1577 .name = "local-APIC",
1578 .mask = mask_lapic_irq,
1579 .unmask = unmask_lapic_irq,
1580 .ack = ack_lapic_irq,
1da177e4
LT
1581};
1582
c88ac1df
MR
1583static void lapic_register_intr(int irq)
1584{
1585 irq_desc[irq].status &= ~IRQ_LEVEL;
1586 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1587 "edge");
1588}
1589
e9427101 1590static void __init setup_nmi(void)
1da177e4
LT
1591{
1592 /*
1593 * Dirty trick to enable the NMI watchdog ...
1594 * We put the 8259A master into AEOI mode and
1595 * unmask on all local APICs LVT0 as NMI.
1596 *
1597 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1598 * is from Maciej W. Rozycki - so we do not have to EOI from
1599 * the NMI handler or the timer interrupt.
1600 */
1601 printk(KERN_INFO "activating NMI Watchdog ...");
1602
e9427101 1603 enable_NMI_through_LVT0();
1da177e4
LT
1604
1605 printk(" done.\n");
1606}
1607
1608/*
1609 * This looks a bit hackish but it's about the only one way of sending
1610 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1611 * not support the ExtINT mode, unfortunately. We need to send these
1612 * cycles as some i82489DX-based boards have glue logic that keeps the
1613 * 8259A interrupt line asserted until INTA. --macro
1614 */
5afca33a 1615static inline void __init unlock_ExtINT_logic(void)
1da177e4 1616{
1008fddc 1617 int apic, pin, i;
1da177e4
LT
1618 struct IO_APIC_route_entry entry0, entry1;
1619 unsigned char save_control, save_freq_select;
1da177e4 1620
1008fddc
EB
1621 pin = find_isa_irq_pin(8, mp_INT);
1622 apic = find_isa_irq_apic(8, mp_INT);
1da177e4
LT
1623 if (pin == -1)
1624 return;
1625
a2249cba
AM
1626 entry0 = ioapic_read_entry(apic, pin);
1627
1008fddc 1628 clear_IO_APIC_pin(apic, pin);
1da177e4
LT
1629
1630 memset(&entry1, 0, sizeof(entry1));
1631
1632 entry1.dest_mode = 0; /* physical delivery */
1633 entry1.mask = 0; /* unmask IRQ now */
ee4eff6f 1634 entry1.dest = hard_smp_processor_id();
1da177e4
LT
1635 entry1.delivery_mode = dest_ExtINT;
1636 entry1.polarity = entry0.polarity;
1637 entry1.trigger = 0;
1638 entry1.vector = 0;
1639
a2249cba 1640 ioapic_write_entry(apic, pin, entry1);
1da177e4
LT
1641
1642 save_control = CMOS_READ(RTC_CONTROL);
1643 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1644 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1645 RTC_FREQ_SELECT);
1646 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1647
1648 i = 100;
1649 while (i-- > 0) {
1650 mdelay(10);
1651 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1652 i -= 10;
1653 }
1654
1655 CMOS_WRITE(save_control, RTC_CONTROL);
1656 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1008fddc 1657 clear_IO_APIC_pin(apic, pin);
1da177e4 1658
a2249cba 1659 ioapic_write_entry(apic, pin, entry0);
1da177e4
LT
1660}
1661
1662/*
1663 * This code may look a bit paranoid, but it's supposed to cooperate with
1664 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1665 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1666 * fanatically on his truly buggy board.
fea5f1e1
LT
1667 *
1668 * FIXME: really need to revamp this for modern platforms only.
1da177e4 1669 */
e9427101 1670static inline void __init check_timer(void)
1da177e4 1671{
dfbffdd8 1672 struct irq_cfg *cfg = irq_cfg + 0;
1008fddc 1673 int apic1, pin1, apic2, pin2;
4aae0702 1674 unsigned long flags;
691874fa 1675 int no_pin1 = 0;
4aae0702
IM
1676
1677 local_irq_save(flags);
1da177e4
LT
1678
1679 /*
1680 * get/set the timer IRQ vector:
1681 */
1682 disable_8259A_irq(0);
dfbffdd8 1683 assign_irq_vector(0, TARGET_CPUS);
1da177e4
LT
1684
1685 /*
d11d5794
MR
1686 * As IRQ0 is to be enabled in the 8259A, the virtual
1687 * wire has to be disabled in the local APIC.
1da177e4 1688 */
11a8e778 1689 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1da177e4 1690 init_8259A(1);
1da177e4 1691
1008fddc
EB
1692 pin1 = find_isa_irq_pin(0, mp_INT);
1693 apic1 = find_isa_irq_apic(0, mp_INT);
1694 pin2 = ioapic_i8259.pin;
1695 apic2 = ioapic_i8259.apic;
1da177e4 1696
fea5f1e1 1697 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
dfbffdd8 1698 cfg->vector, apic1, pin1, apic2, pin2);
b0268726 1699
691874fa
MR
1700 /*
1701 * Some BIOS writers are clueless and report the ExtINTA
1702 * I/O APIC input from the cascaded 8259A as the timer
1703 * interrupt input. So just in case, if only one pin
1704 * was found above, try it both directly and through the
1705 * 8259A.
1706 */
1707 if (pin1 == -1) {
1708 pin1 = pin2;
1709 apic1 = apic2;
1710 no_pin1 = 1;
1711 } else if (pin2 == -1) {
1712 pin2 = pin1;
1713 apic2 = apic1;
1714 }
1715
fea5f1e1
LT
1716 if (pin1 != -1) {
1717 /*
1718 * Ok, does IRQ0 through the IOAPIC work?
1719 */
691874fa
MR
1720 if (no_pin1) {
1721 add_pin_to_irq(0, apic1, pin1);
b1b57ee1 1722 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
691874fa 1723 }
fea5f1e1
LT
1724 unmask_IO_APIC_irq(0);
1725 if (!no_timer_check && timer_irq_works()) {
fea5f1e1 1726 if (nmi_watchdog == NMI_IO_APIC) {
fea5f1e1
LT
1727 setup_nmi();
1728 enable_8259A_irq(0);
1729 }
1730 if (disable_timer_pin_1 > 0)
1731 clear_IO_APIC_pin(0, pin1);
4aae0702 1732 goto out;
fea5f1e1
LT
1733 }
1734 clear_IO_APIC_pin(apic1, pin1);
691874fa
MR
1735 if (!no_pin1)
1736 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: "
1737 "8254 timer not connected to IO-APIC\n");
1da177e4 1738
691874fa
MR
1739 apic_printk(APIC_VERBOSE,KERN_INFO
1740 "...trying to set up timer (IRQ0) "
1741 "through the 8259A ... ");
fea5f1e1
LT
1742 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1743 apic2, pin2);
1744 /*
1745 * legacy devices should be connected to IO APIC #0
1746 */
0b9f4f49 1747 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
f7633ce5 1748 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
24742ece 1749 unmask_IO_APIC_irq(0);
ecd29476 1750 enable_8259A_irq(0);
fea5f1e1
LT
1751 if (timer_irq_works()) {
1752 apic_printk(APIC_VERBOSE," works.\n");
35542c5e 1753 timer_through_8259 = 1;
fea5f1e1 1754 if (nmi_watchdog == NMI_IO_APIC) {
60134ebe 1755 disable_8259A_irq(0);
fea5f1e1 1756 setup_nmi();
60134ebe 1757 enable_8259A_irq(0);
fea5f1e1 1758 }
4aae0702 1759 goto out;
fea5f1e1
LT
1760 }
1761 /*
1762 * Cleanup, just in case ...
1763 */
ecd29476 1764 disable_8259A_irq(0);
fea5f1e1 1765 clear_IO_APIC_pin(apic2, pin2);
691874fa 1766 apic_printk(APIC_VERBOSE," failed.\n");
1da177e4 1767 }
1da177e4 1768
1f992153 1769 if (nmi_watchdog == NMI_IO_APIC) {
1da177e4 1770 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
067fa0ff 1771 nmi_watchdog = NMI_NONE;
1da177e4
LT
1772 }
1773
1774 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1775
c88ac1df 1776 lapic_register_intr(0);
dfbffdd8 1777 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
1da177e4
LT
1778 enable_8259A_irq(0);
1779
1780 if (timer_irq_works()) {
5b922cd4 1781 apic_printk(APIC_VERBOSE," works.\n");
4aae0702 1782 goto out;
1da177e4 1783 }
e67465f1 1784 disable_8259A_irq(0);
dfbffdd8 1785 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
1da177e4
LT
1786 apic_printk(APIC_VERBOSE," failed.\n");
1787
1788 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1789
1790 init_8259A(0);
1791 make_8259A_irq(0);
11a8e778 1792 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4
LT
1793
1794 unlock_ExtINT_logic();
1795
1796 if (timer_irq_works()) {
1797 apic_printk(APIC_VERBOSE," works.\n");
4aae0702 1798 goto out;
1da177e4
LT
1799 }
1800 apic_printk(APIC_VERBOSE," failed :(.\n");
1801 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
4aae0702
IM
1802out:
1803 local_irq_restore(flags);
1da177e4
LT
1804}
1805
14d98cad
AK
1806static int __init notimercheck(char *s)
1807{
1808 no_timer_check = 1;
1809 return 1;
1810}
1811__setup("no_timer_check", notimercheck);
1812
1da177e4 1813/*
af174783
MR
1814 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
1815 * to devices. However there may be an I/O APIC pin available for
1816 * this interrupt regardless. The pin may be left unconnected, but
1817 * typically it will be reused as an ExtINT cascade interrupt for
1818 * the master 8259A. In the MPS case such a pin will normally be
1819 * reported as an ExtINT interrupt in the MP table. With ACPI
1820 * there is no provision for ExtINT interrupts, and in the absence
1821 * of an override it would be treated as an ordinary ISA I/O APIC
1822 * interrupt, that is edge-triggered and unmasked by default. We
1823 * used to do this, but it caused problems on some systems because
1824 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
1825 * the same ExtINT cascade interrupt to drive the local APIC of the
1826 * bootstrap processor. Therefore we refrain from routing IRQ2 to
1827 * the I/O APIC in all cases now. No actual device should request
1828 * it anyway. --macro
1da177e4
LT
1829 */
1830#define PIC_IRQS (1<<2)
1831
1832void __init setup_IO_APIC(void)
1833{
1c69524c
YL
1834
1835 /*
1836 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
1837 */
1da177e4 1838
af174783 1839 io_apic_irqs = ~PIC_IRQS;
1da177e4
LT
1840
1841 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1842
1da177e4
LT
1843 sync_Arb_IDs();
1844 setup_IO_APIC_irqs();
1845 init_IO_APIC_traps();
1846 check_timer();
1847 if (!acpi_ioapic)
1848 print_IO_APIC();
1849}
1850
1851struct sysfs_ioapic_data {
1852 struct sys_device dev;
1853 struct IO_APIC_route_entry entry[0];
1854};
1855static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1856
0b9c33a7 1857static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
1858{
1859 struct IO_APIC_route_entry *entry;
1860 struct sysfs_ioapic_data *data;
1da177e4
LT
1861 int i;
1862
1863 data = container_of(dev, struct sysfs_ioapic_data, dev);
1864 entry = data->entry;
eea0e11c
AK
1865 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1866 *entry = ioapic_read_entry(dev->id, i);
1da177e4
LT
1867
1868 return 0;
1869}
1870
1871static int ioapic_resume(struct sys_device *dev)
1872{
1873 struct IO_APIC_route_entry *entry;
1874 struct sysfs_ioapic_data *data;
1875 unsigned long flags;
1876 union IO_APIC_reg_00 reg_00;
1877 int i;
1878
1879 data = container_of(dev, struct sysfs_ioapic_data, dev);
1880 entry = data->entry;
1881
1882 spin_lock_irqsave(&ioapic_lock, flags);
1883 reg_00.raw = io_apic_read(dev->id, 0);
ec2cd0a2
AS
1884 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
1885 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
1da177e4
LT
1886 io_apic_write(dev->id, 0, reg_00.raw);
1887 }
1da177e4 1888 spin_unlock_irqrestore(&ioapic_lock, flags);
eea0e11c
AK
1889 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1890 ioapic_write_entry(dev->id, i, entry[i]);
1da177e4
LT
1891
1892 return 0;
1893}
1894
1895static struct sysdev_class ioapic_sysdev_class = {
af5ca3f4 1896 .name = "ioapic",
1da177e4
LT
1897 .suspend = ioapic_suspend,
1898 .resume = ioapic_resume,
1899};
1900
1901static int __init ioapic_init_sysfs(void)
1902{
1903 struct sys_device * dev;
cddf7ff7 1904 int i, size, error;
1da177e4
LT
1905
1906 error = sysdev_class_register(&ioapic_sysdev_class);
1907 if (error)
1908 return error;
1909
1910 for (i = 0; i < nr_ioapics; i++ ) {
1911 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1912 * sizeof(struct IO_APIC_route_entry);
cddf7ff7 1913 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
1da177e4
LT
1914 if (!mp_ioapic_data[i]) {
1915 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1916 continue;
1917 }
1da177e4
LT
1918 dev = &mp_ioapic_data[i]->dev;
1919 dev->id = i;
1920 dev->cls = &ioapic_sysdev_class;
1921 error = sysdev_register(dev);
1922 if (error) {
1923 kfree(mp_ioapic_data[i]);
1924 mp_ioapic_data[i] = NULL;
1925 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1926 continue;
1927 }
1928 }
1929
1930 return 0;
1931}
1932
1933device_initcall(ioapic_init_sysfs);
1934
c4fa0bbf 1935/*
04b9267b 1936 * Dynamic irq allocate and deallocation
c4fa0bbf
EB
1937 */
1938int create_irq(void)
1939{
04b9267b
EB
1940 /* Allocate an unused irq */
1941 int irq;
1942 int new;
c4fa0bbf 1943 unsigned long flags;
c4fa0bbf 1944
04b9267b
EB
1945 irq = -ENOSPC;
1946 spin_lock_irqsave(&vector_lock, flags);
1947 for (new = (NR_IRQS - 1); new >= 0; new--) {
1948 if (platform_legacy_irq(new))
1949 continue;
13a79503 1950 if (irq_cfg[new].vector != 0)
04b9267b 1951 continue;
dfbffdd8 1952 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
04b9267b
EB
1953 irq = new;
1954 break;
1955 }
1956 spin_unlock_irqrestore(&vector_lock, flags);
c4fa0bbf 1957
04b9267b 1958 if (irq >= 0) {
c4fa0bbf
EB
1959 dynamic_irq_init(irq);
1960 }
1961 return irq;
1962}
1963
1964void destroy_irq(unsigned int irq)
1965{
1966 unsigned long flags;
c4fa0bbf
EB
1967
1968 dynamic_irq_cleanup(irq);
1969
1970 spin_lock_irqsave(&vector_lock, flags);
5df0287e 1971 __clear_irq_vector(irq);
c4fa0bbf
EB
1972 spin_unlock_irqrestore(&vector_lock, flags);
1973}
c4fa0bbf 1974
589e367f 1975/*
676b1855 1976 * MSI message composition
589e367f
EB
1977 */
1978#ifdef CONFIG_PCI_MSI
3b7d1921 1979static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
589e367f 1980{
dfbffdd8
EB
1981 struct irq_cfg *cfg = irq_cfg + irq;
1982 int err;
589e367f 1983 unsigned dest;
c7111c13 1984 cpumask_t tmp;
589e367f 1985
dfbffdd8
EB
1986 tmp = TARGET_CPUS;
1987 err = assign_irq_vector(irq, tmp);
1988 if (!err) {
1989 cpus_and(tmp, cfg->domain, tmp);
589e367f
EB
1990 dest = cpu_mask_to_apicid(tmp);
1991
1992 msg->address_hi = MSI_ADDR_BASE_HI;
1993 msg->address_lo =
1994 MSI_ADDR_BASE_LO |
1995 ((INT_DEST_MODE == 0) ?
1996 MSI_ADDR_DEST_MODE_PHYSICAL:
1997 MSI_ADDR_DEST_MODE_LOGICAL) |
1998 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1999 MSI_ADDR_REDIRECTION_CPU:
2000 MSI_ADDR_REDIRECTION_LOWPRI) |
2001 MSI_ADDR_DEST_ID(dest);
2002
2003 msg->data =
2004 MSI_DATA_TRIGGER_EDGE |
2005 MSI_DATA_LEVEL_ASSERT |
2006 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2007 MSI_DATA_DELIVERY_FIXED:
2008 MSI_DATA_DELIVERY_LOWPRI) |
dfbffdd8 2009 MSI_DATA_VECTOR(cfg->vector);
589e367f 2010 }
dfbffdd8 2011 return err;
589e367f
EB
2012}
2013
3b7d1921
EB
2014#ifdef CONFIG_SMP
2015static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
589e367f 2016{
dfbffdd8 2017 struct irq_cfg *cfg = irq_cfg + irq;
3b7d1921
EB
2018 struct msi_msg msg;
2019 unsigned int dest;
2020 cpumask_t tmp;
3b7d1921
EB
2021
2022 cpus_and(tmp, mask, cpu_online_map);
2023 if (cpus_empty(tmp))
5ff5115e 2024 return;
589e367f 2025
dfbffdd8 2026 if (assign_irq_vector(irq, mask))
3b7d1921 2027 return;
550f2299 2028
dfbffdd8 2029 cpus_and(tmp, cfg->domain, mask);
3b7d1921 2030 dest = cpu_mask_to_apicid(tmp);
589e367f 2031
3b7d1921
EB
2032 read_msi_msg(irq, &msg);
2033
2034 msg.data &= ~MSI_DATA_VECTOR_MASK;
dfbffdd8 2035 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3b7d1921
EB
2036 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2037 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2038
2039 write_msi_msg(irq, &msg);
9f0a5ba5 2040 irq_desc[irq].affinity = mask;
589e367f 2041}
3b7d1921 2042#endif /* CONFIG_SMP */
589e367f 2043
3b7d1921
EB
2044/*
2045 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2046 * which implement the MSI or MSI-X Capability Structure.
2047 */
2048static struct irq_chip msi_chip = {
2049 .name = "PCI-MSI",
2050 .unmask = unmask_msi_irq,
2051 .mask = mask_msi_irq,
2052 .ack = ack_apic_edge,
2053#ifdef CONFIG_SMP
2054 .set_affinity = set_msi_irq_affinity,
2055#endif
2056 .retrigger = ioapic_retrigger_irq,
589e367f
EB
2057};
2058
f7feaca7 2059int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
3b7d1921
EB
2060{
2061 struct msi_msg msg;
f7feaca7
EB
2062 int irq, ret;
2063 irq = create_irq();
2064 if (irq < 0)
2065 return irq;
2066
3b7d1921 2067 ret = msi_compose_msg(dev, irq, &msg);
f7feaca7
EB
2068 if (ret < 0) {
2069 destroy_irq(irq);
3b7d1921 2070 return ret;
f7feaca7 2071 }
3b7d1921 2072
7fe3730d 2073 set_irq_msi(irq, desc);
3b7d1921
EB
2074 write_msi_msg(irq, &msg);
2075
a460e745 2076 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3b7d1921 2077
7fe3730d 2078 return 0;
3b7d1921
EB
2079}
2080
2081void arch_teardown_msi_irq(unsigned int irq)
2082{
f7feaca7 2083 destroy_irq(irq);
3b7d1921
EB
2084}
2085
3460a6d9
KA
2086#ifdef CONFIG_DMAR
2087#ifdef CONFIG_SMP
2088static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
2089{
2090 struct irq_cfg *cfg = irq_cfg + irq;
2091 struct msi_msg msg;
2092 unsigned int dest;
2093 cpumask_t tmp;
2094
2095 cpus_and(tmp, mask, cpu_online_map);
2096 if (cpus_empty(tmp))
2097 return;
2098
2099 if (assign_irq_vector(irq, mask))
2100 return;
2101
2102 cpus_and(tmp, cfg->domain, mask);
2103 dest = cpu_mask_to_apicid(tmp);
2104
2105 dmar_msi_read(irq, &msg);
2106
2107 msg.data &= ~MSI_DATA_VECTOR_MASK;
2108 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2109 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2110 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2111
2112 dmar_msi_write(irq, &msg);
2113 irq_desc[irq].affinity = mask;
2114}
2115#endif /* CONFIG_SMP */
2116
2117struct irq_chip dmar_msi_type = {
2118 .name = "DMAR_MSI",
2119 .unmask = dmar_msi_unmask,
2120 .mask = dmar_msi_mask,
2121 .ack = ack_apic_edge,
2122#ifdef CONFIG_SMP
2123 .set_affinity = dmar_msi_set_affinity,
2124#endif
2125 .retrigger = ioapic_retrigger_irq,
2126};
2127
2128int arch_setup_dmar_msi(unsigned int irq)
2129{
2130 int ret;
2131 struct msi_msg msg;
2132
2133 ret = msi_compose_msg(NULL, irq, &msg);
2134 if (ret < 0)
2135 return ret;
2136 dmar_msi_write(irq, &msg);
2137 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2138 "edge");
2139 return 0;
2140}
2141#endif
589e367f 2142
3460a6d9 2143#endif /* CONFIG_PCI_MSI */
8b955b0d
EB
2144/*
2145 * Hypertransport interrupt support
2146 */
2147#ifdef CONFIG_HT_IRQ
2148
2149#ifdef CONFIG_SMP
2150
2151static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2152{
ec68307c
EB
2153 struct ht_irq_msg msg;
2154 fetch_ht_irq_msg(irq, &msg);
8b955b0d 2155
ec68307c
EB
2156 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2157 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
8b955b0d 2158
ec68307c
EB
2159 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2160 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
8b955b0d 2161
ec68307c 2162 write_ht_irq_msg(irq, &msg);
8b955b0d
EB
2163}
2164
2165static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2166{
dfbffdd8 2167 struct irq_cfg *cfg = irq_cfg + irq;
8b955b0d
EB
2168 unsigned int dest;
2169 cpumask_t tmp;
8b955b0d
EB
2170
2171 cpus_and(tmp, mask, cpu_online_map);
2172 if (cpus_empty(tmp))
5ff5115e 2173 return;
8b955b0d 2174
dfbffdd8 2175 if (assign_irq_vector(irq, mask))
8b955b0d
EB
2176 return;
2177
dfbffdd8 2178 cpus_and(tmp, cfg->domain, mask);
8b955b0d
EB
2179 dest = cpu_mask_to_apicid(tmp);
2180
dfbffdd8 2181 target_ht_irq(irq, dest, cfg->vector);
9f0a5ba5 2182 irq_desc[irq].affinity = mask;
8b955b0d
EB
2183}
2184#endif
2185
c37e108d 2186static struct irq_chip ht_irq_chip = {
8b955b0d
EB
2187 .name = "PCI-HT",
2188 .mask = mask_ht_irq,
2189 .unmask = unmask_ht_irq,
2190 .ack = ack_apic_edge,
2191#ifdef CONFIG_SMP
2192 .set_affinity = set_ht_irq_affinity,
2193#endif
2194 .retrigger = ioapic_retrigger_irq,
2195};
2196
2197int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2198{
dfbffdd8
EB
2199 struct irq_cfg *cfg = irq_cfg + irq;
2200 int err;
c7111c13 2201 cpumask_t tmp;
8b955b0d 2202
dfbffdd8
EB
2203 tmp = TARGET_CPUS;
2204 err = assign_irq_vector(irq, tmp);
2205 if (!err) {
ec68307c 2206 struct ht_irq_msg msg;
8b955b0d 2207 unsigned dest;
8b955b0d 2208
dfbffdd8 2209 cpus_and(tmp, cfg->domain, tmp);
8b955b0d
EB
2210 dest = cpu_mask_to_apicid(tmp);
2211
ec68307c 2212 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
8b955b0d 2213
ec68307c
EB
2214 msg.address_lo =
2215 HT_IRQ_LOW_BASE |
8b955b0d 2216 HT_IRQ_LOW_DEST_ID(dest) |
dfbffdd8 2217 HT_IRQ_LOW_VECTOR(cfg->vector) |
8b955b0d
EB
2218 ((INT_DEST_MODE == 0) ?
2219 HT_IRQ_LOW_DM_PHYSICAL :
2220 HT_IRQ_LOW_DM_LOGICAL) |
2221 HT_IRQ_LOW_RQEOI_EDGE |
2222 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2223 HT_IRQ_LOW_MT_FIXED :
ec68307c
EB
2224 HT_IRQ_LOW_MT_ARBITRATED) |
2225 HT_IRQ_LOW_IRQ_MASKED;
8b955b0d 2226
ec68307c 2227 write_ht_irq_msg(irq, &msg);
8b955b0d 2228
a460e745
IM
2229 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2230 handle_edge_irq, "edge");
8b955b0d 2231 }
dfbffdd8 2232 return err;
8b955b0d
EB
2233}
2234#endif /* CONFIG_HT_IRQ */
2235
1da177e4
LT
2236/* --------------------------------------------------------------------------
2237 ACPI-based IOAPIC Configuration
2238 -------------------------------------------------------------------------- */
2239
888ba6c6 2240#ifdef CONFIG_ACPI
1da177e4
LT
2241
2242#define IO_APIC_MAX_ID 0xFE
2243
1da177e4
LT
2244int __init io_apic_get_redir_entries (int ioapic)
2245{
2246 union IO_APIC_reg_01 reg_01;
2247 unsigned long flags;
2248
2249 spin_lock_irqsave(&ioapic_lock, flags);
2250 reg_01.raw = io_apic_read(ioapic, 1);
2251 spin_unlock_irqrestore(&ioapic_lock, flags);
2252
2253 return reg_01.bits.entries;
2254}
2255
2256
50eca3eb 2257int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
1da177e4 2258{
1da177e4
LT
2259 if (!IO_APIC_IRQ(irq)) {
2260 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2261 ioapic);
2262 return -EINVAL;
2263 }
2264
550f2299
EB
2265 /*
2266 * IRQs < 16 are already in the irq_2_pin[] map
2267 */
2268 if (irq >= 16)
2269 add_pin_to_irq(irq, ioapic, pin);
2270
a8c8a367 2271 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
1da177e4
LT
2272
2273 return 0;
2274}
2275
1da177e4 2276
61fd47e0
SL
2277int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2278{
2279 int i;
2280
2281 if (skip_ioapic_setup)
2282 return -1;
2283
2284 for (i = 0; i < mp_irq_entries; i++)
2fddb6e2
AS
2285 if (mp_irqs[i].mp_irqtype == mp_INT &&
2286 mp_irqs[i].mp_srcbusirq == bus_irq)
61fd47e0
SL
2287 break;
2288 if (i >= mp_irq_entries)
2289 return -1;
2290
2291 *trigger = irq_trigger(i);
2292 *polarity = irq_polarity(i);
2293 return 0;
2294}
2295
2296#endif /* CONFIG_ACPI */
1da177e4
LT
2297
2298/*
2299 * This function currently is only a helper for the i386 smp boot process where
2300 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2301 * so mask in all cases should simply be TARGET_CPUS
2302 */
54d5d424 2303#ifdef CONFIG_SMP
1da177e4
LT
2304void __init setup_ioapic_dest(void)
2305{
2306 int pin, ioapic, irq, irq_entry;
2307
2308 if (skip_ioapic_setup == 1)
2309 return;
2310
2311 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2312 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2313 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2314 if (irq_entry == -1)
2315 continue;
2316 irq = pin_2_irq(irq_entry, ioapic, pin);
ad892f5e
YL
2317
2318 /* setup_IO_APIC_irqs could fail to get vector for some device
2319 * when you have too many devices, because at that time only boot
2320 * cpu is online.
2321 */
13a79503 2322 if (!irq_cfg[irq].vector)
a8c8a367
EB
2323 setup_IO_APIC_irq(ioapic, pin, irq,
2324 irq_trigger(irq_entry),
2325 irq_polarity(irq_entry));
ad892f5e
YL
2326 else
2327 set_ioapic_affinity_irq(irq, TARGET_CPUS);
1da177e4
LT
2328 }
2329
2330 }
2331}
54d5d424 2332#endif
61fd47e0 2333
3e35a0e5
TG
2334#define IOAPIC_RESOURCE_NAME_SIZE 11
2335
2336static struct resource *ioapic_resources;
2337
2338static struct resource * __init ioapic_setup_resources(void)
2339{
2340 unsigned long n;
2341 struct resource *res;
2342 char *mem;
2343 int i;
2344
2345 if (nr_ioapics <= 0)
2346 return NULL;
2347
2348 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
2349 n *= nr_ioapics;
2350
2351 mem = alloc_bootmem(n);
2352 res = (void *)mem;
2353
2354 if (mem != NULL) {
3e35a0e5
TG
2355 mem += sizeof(struct resource) * nr_ioapics;
2356
2357 for (i = 0; i < nr_ioapics; i++) {
2358 res[i].name = mem;
2359 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
2360 sprintf(mem, "IOAPIC %u", i);
2361 mem += IOAPIC_RESOURCE_NAME_SIZE;
2362 }
2363 }
2364
2365 ioapic_resources = res;
2366
2367 return res;
2368}
2369
2370void __init ioapic_init_mappings(void)
2371{
2372 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2373 struct resource *ioapic_res;
2374 int i;
2375
2376 ioapic_res = ioapic_setup_resources();
2377 for (i = 0; i < nr_ioapics; i++) {
2378 if (smp_found_config) {
ec2cd0a2 2379 ioapic_phys = mp_ioapics[i].mp_apicaddr;
3e35a0e5
TG
2380 } else {
2381 ioapic_phys = (unsigned long)
2382 alloc_bootmem_pages(PAGE_SIZE);
2383 ioapic_phys = __pa(ioapic_phys);
2384 }
2385 set_fixmap_nocache(idx, ioapic_phys);
2386 apic_printk(APIC_VERBOSE,
2387 "mapped IOAPIC to %016lx (%016lx)\n",
2388 __fix_to_virt(idx), ioapic_phys);
2389 idx++;
2390
2391 if (ioapic_res != NULL) {
2392 ioapic_res->start = ioapic_phys;
2393 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
2394 ioapic_res++;
2395 }
2396 }
2397}
2398
2399static int __init ioapic_insert_resources(void)
2400{
2401 int i;
2402 struct resource *r = ioapic_resources;
2403
2404 if (!r) {
2405 printk(KERN_ERR
2406 "IO APIC resources could be not be allocated.\n");
2407 return -1;
2408 }
2409
2410 for (i = 0; i < nr_ioapics; i++) {
2411 insert_resource(&iomem_resource, r);
2412 r++;
2413 }
2414
2415 return 0;
2416}
2417
2418/* Insert the IO APIC resources after PCI initialization has occured to handle
2419 * IO APICS that are mapped in on a BAR in PCI space. */
2420late_initcall(ioapic_insert_resources);
2421