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