blob: 033ad953bee33a60d37f8971d064b76f5b03cd37 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Luf3294a32008-06-27 01:41:56 -070028#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070034#include <linux/pci.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070035#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070036#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020038#include <linux/kthread.h>
Julia Lawall1d16b532008-01-30 13:32:19 +010039#include <linux/jiffies.h> /* time_after() */
Ashok Raj54d5d422005-09-06 15:16:15 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/smp.h>
43#include <asm/desc.h>
44#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070045#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020046#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070047#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070048#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070049#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020052#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010054#define __apicdebuginit(type) static type __init
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056int (*ioapic_renumber_irq)(int ioapic, int irq);
57atomic_t irq_mis_count;
58
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080059/* Where if anywhere is the i8259 connect in external int mode */
60static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(ioapic_lock);
Eric W. Biedermand388e5f2008-08-09 15:09:02 -070063DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010065int timer_through_8259 __initdata;
Andi Kleenf9262c12006-03-08 17:57:25 -080066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Is the SiS APIC rmw bug present ?
69 * -1 = don't know, 0 = no, 1 = yes
70 */
71int sis_apic_bug = -1;
72
Yinghai Lu301e6192008-08-19 20:50:02 -070073int first_free_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * # of IRQ routing registers
76 */
77int nr_ioapic_registers[MAX_IO_APICS];
78
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040079/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040080struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040081int nr_ioapics;
82
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040083/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040084struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040085
86/* # of MP IRQ source entries */
87int mp_irq_entries;
88
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040089#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
Rusty Russell1a3f2392006-09-26 10:52:32 +020095static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020096
Yinghai Lua1420f32008-08-19 20:50:24 -070097struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Rough estimation of how many shared IRQs there are, can
136 * be changed anytime.
137 */
Yinghai Lu301e6192008-08-19 20:50:02 -0700138int pin_map_size;
Yinghai Lu0799e432008-08-19 20:49:48 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
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;
Yinghai Lu301e6192008-08-19 20:50:02 -0700149} *irq_2_pin;
150
151DEFINE_DYN_ARRAY(irq_2_pin, sizeof(struct irq_pin_list), pin_map_size, 16, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds130fe052006-11-01 09:11:00 -0800153struct 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)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400162 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800163}
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{
Al Virocb468982007-02-09 16:39:25 +0000187 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800188 if (sis_apic_bug)
189 writel(reg, &io_apic->index);
190 writel(value, &io_apic->data);
191}
192
Andi Kleencf4c6a22006-09-26 10:52:30 +0200193union 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
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800209/*
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 */
Andi Kleend15512f2006-12-07 02:14:07 +0100215static void
216__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
217{
218 union entry_union eu;
219 eu.entry = e;
220 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
221 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
222}
223
Andi Kleencf4c6a22006-09-26 10:52:30 +0200224static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
225{
226 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200227 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100228 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800229 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
242 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200243 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 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;
Yinghai Lu0799e432008-08-19 20:49:48 -0700263 if (++first_free_entry >= pin_map_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200290static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
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 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200310static void __mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400312 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/* mask = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200316static void __unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400318 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/* mask = 1, trigger = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200322static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400324 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
325 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/* mask = 0, trigger = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200329static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400331 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
332 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200335static void mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200344static void unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
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;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200358 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (entry.delivery_mode == dest_SMI)
360 return;
361
362 /*
363 * Disable it in the IO-APIC irq-routing table:
364 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800365 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200368static void clear_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
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
Ashok Raj54d5d422005-09-06 15:16:15 -0700377#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static 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;
Ashok Raj54d5d422005-09-06 15:16:15 -0700384 cpumask_t tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -0700385 struct irq_desc *desc;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200386
Ashok Raj54d5d422005-09-06 15:16:15 -0700387 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 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 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700406 desc = irq_to_desc(irq);
407 desc->affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 spin_unlock_irqrestore(&ioapic_lock, flags);
409}
410
Ashok Raj54d5d422005-09-06 15:16:15 -0700411#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100414void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
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 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100426 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static 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++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400479 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return i;
484
485 return -1;
486}
487
488/*
489 * Find the pin to which IRQ[irq] (ISA) is connected
490 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800491static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 int i;
494
495 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400496 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300498 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400499 (mp_irqs[i].mp_irqtype == type) &&
500 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400502 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 return -1;
505}
506
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800507static int __init find_isa_irq_apic(int irq, int type)
508{
509 int i;
510
511 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400512 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800513
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300514 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400515 (mp_irqs[i].mp_irqtype == type) &&
516 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800517 break;
518 }
519 if (i < mp_irq_entries) {
520 int apic;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200521 for (apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400522 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800523 return apic;
524 }
525 }
526
527 return -1;
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/*
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);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400542 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 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++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400547 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400550 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
551 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300554 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400555 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400557 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar3de352b2008-07-08 11:14:58 +0200558 int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!(apic || IO_APIC_IRQ(irq)))
561 continue;
562
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400563 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 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}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700575EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200578 * This function currently is only a helper for the i386 smp boot process where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * 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 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700582#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583void __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}
Ashok Raj54d5d422005-09-06 15:16:15 -0700601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300603#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
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}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300619/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/* 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
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400630#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300631#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
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)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300643#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Shaohua Li61fd47e2007-11-17 01:05:28 -0500645static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400647 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int polarity;
649
650 /*
651 * Determine IRQ line polarity (high active or low active):
652 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200653 switch (mp_irqs[idx].mp_irqflag & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200654 case 0: /* conforms, ie. bus-type dependent polarity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200656 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 return polarity;
685}
686
687static int MPBIOS_trigger(int idx)
688{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400689 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int trigger;
691
692 /*
693 * Determine IRQ trigger mode (edge or level sensitive):
694 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200695 switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200696 case 0: /* conforms, ie. bus-type dependent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200698 trigger = test_bit(bus, mp_bus_not_pci)?
699 default_ISA_trigger(idx):
700 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300701#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200702 switch (mp_bus_id_to_type[bus]) {
703 case MP_BUS_ISA: /* ISA pin */
704 {
705 /* set before the switch */
706 break;
707 }
708 case MP_BUS_EISA: /* EISA pin */
709 {
710 trigger = default_EISA_trigger(idx);
711 break;
712 }
713 case MP_BUS_PCI: /* PCI pin */
714 {
715 /* set before the switch */
716 break;
717 }
718 case MP_BUS_MCA: /* MCA pin */
719 {
720 trigger = default_MCA_trigger(idx);
721 break;
722 }
723 default:
724 {
725 printk(KERN_WARNING "broken BIOS!!\n");
726 trigger = 1;
727 break;
728 }
729 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300730#endif
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200731 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
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;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400772 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /*
775 * Debugging check, we are in big trouble if this message pops up!
776 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400777 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
779
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300780 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400781 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300782 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300791 /*
792 * For MPS mode, so far only needed by ES7000 platform
793 */
794 if (ioapic_renumber_irq)
795 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
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++) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200823 idx = find_irq_entry(apic, pin, mp_INT);
824 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return irq_trigger(idx);
826 }
827 }
828 /*
829 * nonexistent IRQs are edge default
830 */
831 return 0;
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700835static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200837 static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200838 int vector, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Yinghai Lu301e6192008-08-19 20:50:02 -0700840 BUG_ON((unsigned)irq >= nr_irqs);
Jan Beulich0a1ad602006-06-26 13:56:43 +0200841
Yinghai Lua1420f32008-08-19 20:50:24 -0700842 if (irq_cfg(irq)->vector > 0)
843 return irq_cfg(irq)->vector;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700844
Jan Beulich0a1ad602006-06-26 13:56:43 +0200845 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -0700846 offset = current_offset;
847next:
848 vector += 8;
Alan Mayer305b92a2008-04-15 15:36:56 -0500849 if (vector >= first_system_vector) {
Eric W. Biederman8339f002007-01-29 13:19:05 -0700850 offset = (offset + 1) % 8;
851 vector = FIRST_DEVICE_VECTOR + offset;
852 }
853 if (vector == current_vector)
854 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200855 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -0700856 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -0700857
858 current_vector = vector;
859 current_offset = offset;
Yinghai Lua1420f32008-08-19 20:50:24 -0700860 irq_cfg(irq)->vector = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +0200861
862 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700865static 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);
872 spin_unlock_irqrestore(&vector_lock, flags);
873
874 return vector;
875}
Glauber Costa3fde6902008-05-28 20:34:19 -0700876
Ingo Molnarf5b9ed72006-10-04 02:16:26 -0700877static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879#define IOAPIC_AUTO -1
880#define IOAPIC_EDGE 0
881#define IOAPIC_LEVEL 1
882
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700883static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Yinghai Lu08678b02008-08-19 20:50:05 -0700885 struct irq_desc *desc;
886
887 desc = irq_to_desc(irq);
Jan Beulich6ebcc002006-06-26 13:56:46 +0200888 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +0000889 trigger == IOAPIC_LEVEL) {
Yinghai Lu08678b02008-08-19 20:50:05 -0700890 desc->status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -0700891 set_irq_chip_and_handler_name(irq, &ioapic_chip,
892 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +0000893 } else {
Yinghai Lu08678b02008-08-19 20:50:05 -0700894 desc->status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -0700895 set_irq_chip_and_handler_name(irq, &ioapic_chip,
896 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +0000897 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700898 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
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 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200914 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 entry.delivery_mode = INT_DELIVERY_MODE;
917 entry.dest_mode = INT_DEST_MODE;
918 entry.mask = 0; /* enable IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200919 entry.dest.logical.logical_dest =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 cpu_mask_to_apicid(TARGET_CPUS);
921
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200922 idx = find_irq_entry(apic, pin, mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (idx == -1) {
924 if (first_notcon) {
925 apic_printk(APIC_VERBOSE, KERN_DEBUG
926 " IO-APIC (apicid-pin) %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400927 mp_ioapics[apic].mp_apicid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 pin);
929 first_notcon = 0;
930 } else
931 apic_printk(APIC_VERBOSE, ", %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400932 mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 continue;
934 }
935
Yinghai Lu20d225b2007-10-17 18:04:41 +0200936 if (!first_notcon) {
937 apic_printk(APIC_VERBOSE, " not connected.\n");
938 first_notcon = 1;
939 }
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 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);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!apic && (irq < 16))
968 disable_8259A_irq(irq);
969 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +0900970 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 }
973
974 if (!first_notcon)
975 apic_printk(APIC_VERBOSE, " not connected.\n");
976}
977
978/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +0100979 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +0100981static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
982 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200986 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /*
989 * We use logical delivery to get the timer IRQ
990 * to the first CPU.
991 */
992 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +0100993 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 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
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001002 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
Maciej W. Rozyckif0825262008-05-27 21:19:16 +01001004 ioapic_register_intr(0, vector, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /*
1007 * Add it to the IO-APIC irq-routing table:
1008 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001009 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001012
1013__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001025 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 for (i = 0; i < nr_ioapics; i++)
1027 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001028 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
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
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001047 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1054 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
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
Andi Kleencf4c6a22006-09-26 10:52:30 +02001088 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0799e432008-08-19 20:49:48 -07001109 for (i = 0; i < nr_irqs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct irq_pin_list *entry = irq_2_pin + i;
1111 if (entry->pin < 0)
1112 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001113 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 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
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001128__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
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
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001149__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001152 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
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());
Andreas Herrmann66823112008-06-05 16:35:10 +02001159 v = apic_read(APIC_ID);
Jack Steiner05f2d122008-03-28 14:12:02 -05001160 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001161 GET_APIC_ID(v));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 v = apic_read(APIC_LVR);
1163 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1164 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001165 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
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
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001203 icr = apic_icr_read();
1204 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1205 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
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
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001233__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001235 on_each_cpu(print_local_APIC, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001238__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001256 outb(0x0b, 0xa0);
1257 outb(0x0b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 v = inb(0xa0) << 8 | inb(0x20);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001259 outb(0x0a, 0xa0);
1260 outb(0x0a, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
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
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001270__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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282static void __init enable_IO_APIC(void)
1283{
1284 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001285 int i8259_apic, i8259_pin;
1286 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unsigned long flags;
1288
Yinghai Lu0799e432008-08-19 20:49:48 -07001289 for (i = 0; i < pin_map_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 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 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001300 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001302 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001304 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1305 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001306 for (apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001307 int pin;
1308 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001309 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001310 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001311 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001312
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");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
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
Eric W. Biederman650927e2005-06-25 14:57:44 -07001361 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001362 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001363 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001364 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001365 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001366 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001367 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001368
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 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001376 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001377 entry.vector = 0;
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001378 entry.dest.physical.physical_dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001379
1380 /*
1381 * Add it to the IO-APIC irq-routing table:
1382 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001383 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001384 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001385 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395static 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
Yinghai Lua4dbc342008-07-25 02:14:28 -07001404 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001405 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001408 * Don't check I/O APIC IDs for xAPIC systems. They have
1409 * no meaning without the serial APIC bus.
1410 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001411 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1412 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001413 return;
1414 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * 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);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001429
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001430 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001432 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001434 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1436 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001437 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /*
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,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001446 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001448 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 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);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001457 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 } else {
1459 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001460 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 apic_printk(APIC_VERBOSE, "Setting %d in the "
1462 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001463 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 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 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001472 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001474 if (mp_irqs[i].mp_dstapic == old_id)
1475 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001476 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /*
1479 * Read the right value from the MPC table and
1480 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 apic_printk(APIC_VERBOSE, KERN_INFO
1483 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001484 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001486 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 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);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001497 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 printk("could not set ID!\n");
1499 else
1500 apic_printk(APIC_VERBOSE, " ok.\n");
1501 }
1502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001504int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001505
1506static int __init notimercheck(char *s)
1507{
1508 no_timer_check = 1;
1509 return 1;
1510}
1511__setup("no_timer_check", notimercheck);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/*
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 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001521static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001524 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Zachary Amsden8542b202006-12-07 02:14:09 +01001526 if (no_timer_check)
1527 return 1;
1528
Ingo Molnar4aae0702007-12-18 18:05:58 +01001529 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 local_irq_enable();
1531 /* Let ten ticks pass... */
1532 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001533 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
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 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001542 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 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/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001562 * Startup quirk:
1563 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * 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...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001571 *
1572 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001574static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
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
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001591static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001593 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ack_APIC_irq();
1595}
1596
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001597static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 unsigned long v;
1600 int i;
1601
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001602 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603/*
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 */
Yinghai Lua1420f32008-08-19 20:50:24 -07001622 i = irq_cfg(irq)->vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
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
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001637static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Yinghai Lua1420f32008-08-19 20:50:24 -07001639 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001640
1641 return 1;
1642}
1643
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001644static struct irq_chip ioapic_chip __read_mostly = {
1645 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001646 .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,
Ashok Raj54d5d422005-09-06 15:16:15 -07001651#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001652 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001653#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001654 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655};
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658static inline void init_IO_APIC_traps(void)
1659{
1660 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07001661 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
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 */
Yinghai Lu0799e432008-08-19 20:49:48 -07001674 for (irq = 0; irq < nr_irqs ; irq++) {
Yinghai Lua1420f32008-08-19 20:50:24 -07001675 if (IO_APIC_IRQ(irq) && !irq_cfg(irq)->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
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);
Yinghai Lu08678b02008-08-19 20:50:05 -07001683 else {
1684 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07001686 desc->chip = &no_irq_chip;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689 }
1690}
1691
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001692/*
1693 * The local APIC irq-chip implementation:
1694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001696static void ack_lapic_irq(unsigned int irq)
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001697{
1698 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001701static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 unsigned long v;
1704
1705 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001706 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001709static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001711 unsigned long v;
1712
1713 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001714 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001717static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01001718 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001719 .mask = mask_lapic_irq,
1720 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001721 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722};
1723
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001724static void lapic_register_intr(int irq, int vector)
1725{
Yinghai Lu08678b02008-08-19 20:50:05 -07001726 struct irq_desc *desc;
1727
1728 desc = irq_to_desc(irq);
1729 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001730 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1731 "edge");
1732 set_intr_gate(vector, interrupt[irq]);
1733}
1734
Jan Beuliche9427102008-01-30 13:31:24 +01001735static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001738 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 * 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.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
1747
Jan Beuliche9427102008-01-30 13:31:24 +01001748 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
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 */
Jacek Luczak28acf282008-04-12 17:41:12 +02001760static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001762 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct IO_APIC_route_entry entry0, entry1;
1764 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001766 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01001767 if (pin == -1) {
1768 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01001770 }
1771 apic = find_isa_irq_apic(8, mp_INT);
1772 if (apic == -1) {
1773 WARN_ON_ONCE(1);
1774 return;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Andi Kleencf4c6a22006-09-26 10:52:30 +02001777 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001778 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
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
Andi Kleencf4c6a22006-09-26 10:52:30 +02001790 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
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);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001807 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Andi Kleencf4c6a22006-09-26 10:52:30 +02001809 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
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 */
Zachary Amsden8542b202006-12-07 02:14:09 +01001818static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001820 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001821 int no_pin1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int vector;
Ingo Molnar6e908942008-03-21 14:32:36 +01001823 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001824 unsigned long flags;
1825
1826 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01001827
Ingo Molnar6e908942008-03-21 14:32:36 +01001828 ver = apic_read(APIC_LVR);
1829 ver = GET_APIC_VERSION(ver);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
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 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01001839 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001847 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01001849 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001851 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001856 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001860 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (pin1 != -1) {
1877 /*
1878 * Ok, does IRQ0 through the IOAPIC work?
1879 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001880 if (no_pin1) {
1881 add_pin_to_irq(0, apic1, pin1);
1882 setup_timer_IRQ0_pin(apic1, pin1, vector);
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 unmask_IO_APIC_irq(0);
1885 if (timer_irq_works()) {
1886 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 setup_nmi();
1888 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02001890 if (disable_timer_pin_1 > 0)
1891 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001894 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001895 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001896 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
1897 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001899 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /*
1904 * legacy devices should be connected to IO APIC #0
1905 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001906 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001907 setup_timer_IRQ0_pin(apic2, pin2, vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01001908 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01001909 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001911 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01001912 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01001914 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01001916 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01001918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920 /*
1921 * Cleanup, just in case ...
1922 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01001923 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001924 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001925 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001929 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
1930 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04001931 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01001933 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001935 apic_printk(APIC_QUIET, KERN_INFO
1936 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001938 lapic_register_intr(0, vector);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001939 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 enable_8259A_irq(0);
1941
1942 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001943 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01001944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01001946 disable_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001947 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001948 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001950 apic_printk(APIC_QUIET, KERN_INFO
1951 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 init_8259A(0);
1954 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001955 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 unlock_ExtINT_logic();
1958
1959 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001960 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01001961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001963 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01001965 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01001966out:
1967 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01001971 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
1987#define PIC_IRQS (1 << PIC_CASCADE_IR)
1988
1989void __init setup_IO_APIC(void)
1990{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001991 int i;
1992
1993 /* Reserve all the system vectors. */
Alan Mayer305b92a2008-04-15 15:36:56 -05001994 for (i = first_system_vector; i < NR_VECTORS; i++)
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001995 set_bit(i, used_vectors);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 enable_IO_APIC();
1998
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01001999 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
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();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002011 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
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 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019static int __init io_apic_bug_finalize(void)
2020{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002021 if (sis_apic_bug == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 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};
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002032static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Pavel Machek438510f2005-04-16 15:25:24 -07002034static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 struct IO_APIC_route_entry *entry;
2037 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 data = container_of(dev, struct sysfs_ioapic_data, dev);
2041 entry = data->entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002042 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002043 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
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;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 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);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002061 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2062 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 io_apic_write(dev->id, 0, reg_00.raw);
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002066 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002067 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 return 0;
2070}
2071
2072static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002073 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 .suspend = ioapic_suspend,
2075 .resume = ioapic_resume,
2076};
2077
2078static int __init ioapic_init_sysfs(void)
2079{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002080 struct sys_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 int i, size, error = 0;
2082
2083 error = sysdev_class_register(&ioapic_sysdev_class);
2084 if (error)
2085 return error;
2086
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002087 for (i = 0; i < nr_ioapics; i++) {
2088 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002090 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!mp_ioapic_data[i]) {
2092 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2093 continue;
2094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002096 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 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
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002112/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002113 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002114 */
2115int create_irq(void)
2116{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002117 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002118 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002119 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002120
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002121 irq = -ENOSPC;
2122 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu0799e432008-08-19 20:49:48 -07002123 for (new = (nr_irqs - 1); new >= 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002124 if (platform_legacy_irq(new))
2125 continue;
Yinghai Lua1420f32008-08-19 20:50:24 -07002126 if (irq_cfg(new)->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002127 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);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002134
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002135 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002136 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002137 dynamic_irq_init(irq);
2138 }
2139 return irq;
2140}
2141
2142void destroy_irq(unsigned int irq)
2143{
2144 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002145
2146 dynamic_irq_cleanup(irq);
2147
2148 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lua1420f32008-08-19 20:50:24 -07002149 clear_bit(irq_cfg(irq)->vector, used_vectors);
2150 irq_cfg(irq)->vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002151 spin_unlock_irqrestore(&vector_lock, flags);
2152}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002153
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002154/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002155 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002156 */
2157#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002158static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002159{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002160 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) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002171MSI_ADDR_DEST_MODE_PHYSICAL:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002172 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) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002182MSI_DATA_DELIVERY_FIXED:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002183 MSI_DATA_DELIVERY_LOWPRI) |
2184 MSI_DATA_VECTOR(vector);
2185 }
2186 return vector;
2187}
2188
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002189#ifdef CONFIG_SMP
2190static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2191{
2192 struct msi_msg msg;
2193 unsigned int dest;
2194 cpumask_t tmp;
2195 int vector;
Yinghai Lu08678b02008-08-19 20:50:05 -07002196 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002197
2198 cpus_and(tmp, mask, cpu_online_map);
2199 if (cpus_empty(tmp))
2200 tmp = TARGET_CPUS;
2201
2202 vector = assign_irq_vector(irq);
2203 if (vector < 0)
2204 return;
2205
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);
Yinghai Lu08678b02008-08-19 20:50:05 -07002216 desc = irq_to_desc(irq);
2217 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002218}
2219#endif /* CONFIG_SMP */
2220
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,
2234};
2235
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002236int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002237{
2238 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002239 int irq, ret;
2240 irq = create_irq();
2241 if (irq < 0)
2242 return irq;
2243
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002244 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002245 if (ret < 0) {
2246 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002247 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002248 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002249
Michael Ellerman7fe37302007-04-18 19:39:21 +10002250 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002251 write_msi_msg(irq, &msg);
2252
Ingo Molnara460e742006-10-17 00:10:03 -07002253 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2254 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002255
Michael Ellerman7fe37302007-04-18 19:39:21 +10002256 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002257}
2258
2259void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002260{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002261 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002262}
2263
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002264#endif /* CONFIG_PCI_MSI */
2265
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002266/*
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{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002275 struct ht_irq_msg msg;
2276 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002277
Eric W. Biedermanec683072006-11-08 17:44:57 -08002278 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2279 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002280
Eric W. Biedermanec683072006-11-08 17:44:57 -08002281 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2282 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002283
Eric W. Biedermanec683072006-11-08 17:44:57 -08002284 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002285}
2286
2287static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2288{
2289 unsigned int dest;
2290 cpumask_t tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -07002291 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002292
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);
Yinghai Lu08678b02008-08-19 20:50:05 -07002302 desc = irq_to_desc(irq);
2303 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002304}
2305#endif
2306
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002307static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002308 .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) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002324 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002325 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
Eric W. Biedermanec683072006-11-08 17:44:57 -08002332 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002333
Eric W. Biedermanec683072006-11-08 17:44:57 -08002334 msg.address_lo =
2335 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002336 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
Eric W. Biedermanec683072006-11-08 17:44:57 -08002347 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002348
Ingo Molnara460e742006-10-17 00:10:03 -07002349 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2350 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002351 }
2352 return vector;
2353}
2354#endif /* CONFIG_HT_IRQ */
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356/* --------------------------------------------------------------------------
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002357 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 -------------------------------------------------------------------------- */
2359
Len Brown888ba6c2005-08-24 12:07:20 -04002360#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002362int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
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 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002371 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2372 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002374 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 * 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 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002393 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 * '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;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
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 */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002424 if (reg_00.bits.ID != apic_id) {
2425 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2426 return -1;
2427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002437int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002450int __init io_apic_get_redir_entries(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002463int io_apic_set_pci_routing(int ioapic, int pin, int irq, int edge_level, int active_high_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
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
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002479 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
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,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002498 mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 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
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002506 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 return 0;
2509}
2510
Shaohua Li61fd47e2007-11-17 01:05:28 -05002511int 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++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002519 if (mp_irqs[i].mp_irqtype == mp_INT &&
2520 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002521 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
Len Brown888ba6c2005-08-24 12:07:20 -04002530#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002531
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);
Yinghai Luf3294a32008-06-27 01:41:56 -07002553
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