blob: e710289f673e86503d1ba9d77e1770e3559c1ccd [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
73/*
74 * # of IRQ routing registers
75 */
76int nr_ioapic_registers[MAX_IO_APICS];
77
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040078/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040079struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040080int nr_ioapics;
81
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040082/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040083struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040084
85/* # of MP IRQ source entries */
86int mp_irq_entries;
87
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040088#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
89int mp_bus_id_to_type[MAX_MP_BUSSES];
90#endif
91
92DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
93
Rusty Russell1a3f2392006-09-26 10:52:32 +020094static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Rough estimation of how many shared IRQs there are, can
98 * be changed anytime.
99 */
100#define MAX_PLUS_SHARED_IRQS NR_IRQS
101#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
102
103/*
104 * This is performance-critical, we want to do it O(1)
105 *
106 * the indexing order of this array favors 1:1 mappings
107 * between pins and IRQs.
108 */
109
110static struct irq_pin_list {
111 int apic, pin, next;
112} irq_2_pin[PIN_MAP_SIZE];
113
Linus Torvalds130fe052006-11-01 09:11:00 -0800114struct io_apic {
115 unsigned int index;
116 unsigned int unused[3];
117 unsigned int data;
118};
119
120static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
121{
122 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400123 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800124}
125
126static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
127{
128 struct io_apic __iomem *io_apic = io_apic_base(apic);
129 writel(reg, &io_apic->index);
130 return readl(&io_apic->data);
131}
132
133static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
134{
135 struct io_apic __iomem *io_apic = io_apic_base(apic);
136 writel(reg, &io_apic->index);
137 writel(value, &io_apic->data);
138}
139
140/*
141 * Re-write a value: to be used for read-modify-write
142 * cycles where the read already set up the index register.
143 *
144 * Older SiS APIC requires we rewrite the index register
145 */
146static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
147{
Al Virocb468982007-02-09 16:39:25 +0000148 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800149 if (sis_apic_bug)
150 writel(reg, &io_apic->index);
151 writel(value, &io_apic->data);
152}
153
Andi Kleencf4c6a22006-09-26 10:52:30 +0200154union entry_union {
155 struct { u32 w1, w2; };
156 struct IO_APIC_route_entry entry;
157};
158
159static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
160{
161 union entry_union eu;
162 unsigned long flags;
163 spin_lock_irqsave(&ioapic_lock, flags);
164 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
165 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
166 spin_unlock_irqrestore(&ioapic_lock, flags);
167 return eu.entry;
168}
169
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800170/*
171 * When we write a new IO APIC routing entry, we need to write the high
172 * word first! If the mask bit in the low word is clear, we will enable
173 * the interrupt, and we need to make sure the entry is fully populated
174 * before that happens.
175 */
Andi Kleend15512f2006-12-07 02:14:07 +0100176static void
177__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
178{
179 union entry_union eu;
180 eu.entry = e;
181 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
182 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
183}
184
Andi Kleencf4c6a22006-09-26 10:52:30 +0200185static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
186{
187 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200188 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100189 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800190 spin_unlock_irqrestore(&ioapic_lock, flags);
191}
192
193/*
194 * When we mask an IO APIC routing entry, we need to write the low
195 * word first, in order to set the mask bit before we change the
196 * high bits!
197 */
198static void ioapic_mask_entry(int apic, int pin)
199{
200 unsigned long flags;
201 union entry_union eu = { .entry.mask = 1 };
202
203 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200204 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
205 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
206 spin_unlock_irqrestore(&ioapic_lock, flags);
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
211 * shared ISA-space IRQs, so we have to support them. We are super
212 * fast in the common case, and fast for shared ISA-space IRQs.
213 */
214static void add_pin_to_irq(unsigned int irq, int apic, int pin)
215{
216 static int first_free_entry = NR_IRQS;
217 struct irq_pin_list *entry = irq_2_pin + irq;
218
219 while (entry->next)
220 entry = irq_2_pin + entry->next;
221
222 if (entry->pin != -1) {
223 entry->next = first_free_entry;
224 entry = irq_2_pin + entry->next;
225 if (++first_free_entry >= PIN_MAP_SIZE)
226 panic("io_apic.c: whoops");
227 }
228 entry->apic = apic;
229 entry->pin = pin;
230}
231
232/*
233 * Reroute an IRQ to a different pin.
234 */
235static void __init replace_pin_at_irq(unsigned int irq,
236 int oldapic, int oldpin,
237 int newapic, int newpin)
238{
239 struct irq_pin_list *entry = irq_2_pin + irq;
240
241 while (1) {
242 if (entry->apic == oldapic && entry->pin == oldpin) {
243 entry->apic = newapic;
244 entry->pin = newpin;
245 }
246 if (!entry->next)
247 break;
248 entry = irq_2_pin + entry->next;
249 }
250}
251
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200252static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct irq_pin_list *entry = irq_2_pin + irq;
255 unsigned int pin, reg;
256
257 for (;;) {
258 pin = entry->pin;
259 if (pin == -1)
260 break;
261 reg = io_apic_read(entry->apic, 0x10 + pin*2);
262 reg &= ~disable;
263 reg |= enable;
264 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
265 if (!entry->next)
266 break;
267 entry = irq_2_pin + entry->next;
268 }
269}
270
271/* mask = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200272static void __mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400274 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/* mask = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200278static void __unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400280 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/* mask = 1, trigger = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200284static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400286 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
287 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/* mask = 0, trigger = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200291static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400293 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
294 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200297static void mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&ioapic_lock, flags);
302 __mask_IO_APIC_irq(irq);
303 spin_unlock_irqrestore(&ioapic_lock, flags);
304}
305
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200306static void unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 unsigned long flags;
309
310 spin_lock_irqsave(&ioapic_lock, flags);
311 __unmask_IO_APIC_irq(irq);
312 spin_unlock_irqrestore(&ioapic_lock, flags);
313}
314
315static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
316{
317 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200320 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (entry.delivery_mode == dest_SMI)
322 return;
323
324 /*
325 * Disable it in the IO-APIC irq-routing table:
326 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800327 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200330static void clear_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 int apic, pin;
333
334 for (apic = 0; apic < nr_ioapics; apic++)
335 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
336 clear_IO_APIC_pin(apic, pin);
337}
338
Ashok Raj54d5d422005-09-06 15:16:15 -0700339#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
341{
342 unsigned long flags;
343 int pin;
344 struct irq_pin_list *entry = irq_2_pin + irq;
345 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700346 cpumask_t tmp;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200347
Ashok Raj54d5d422005-09-06 15:16:15 -0700348 cpus_and(tmp, cpumask, cpu_online_map);
349 if (cpus_empty(tmp))
350 tmp = TARGET_CPUS;
351
352 cpus_and(cpumask, tmp, CPU_MASK_ALL);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 apicid_value = cpu_mask_to_apicid(cpumask);
355 /* Prepare to do the io_apic_write */
356 apicid_value = apicid_value << 24;
357 spin_lock_irqsave(&ioapic_lock, flags);
358 for (;;) {
359 pin = entry->pin;
360 if (pin == -1)
361 break;
362 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
363 if (!entry->next)
364 break;
365 entry = irq_2_pin + entry->next;
366 }
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700367 irq_desc[irq].affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_unlock_irqrestore(&ioapic_lock, flags);
369}
370
371#if defined(CONFIG_IRQBALANCE)
372# include <asm/processor.h> /* kernel_thread() */
373# include <linux/kernel_stat.h> /* kstat */
374# include <linux/slab.h> /* kmalloc() */
Julia Lawall1d16b532008-01-30 13:32:19 +0100375# include <linux/timer.h>
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define IRQBALANCE_CHECK_ARCH -999
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700378#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
379#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
380#define BALANCED_IRQ_MORE_DELTA (HZ/10)
381#define BALANCED_IRQ_LESS_DELTA (HZ)
382
383static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
384static int physical_balance __read_mostly;
385static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387static struct irq_cpu_info {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200388 unsigned long *last_irq;
389 unsigned long *irq_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned long irq;
391} irq_cpu_data[NR_CPUS];
392
393#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200394#define LAST_CPU_IRQ(cpu, irq) (irq_cpu_data[cpu].last_irq[irq])
395#define IRQ_DELTA(cpu, irq) (irq_cpu_data[cpu].irq_delta[irq])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397#define IDLE_ENOUGH(cpu,now) \
398 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
399
400#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
401
Mike Travisd5a74302007-10-16 01:24:05 -0700402#define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700404static cpumask_t balance_irq_affinity[NR_IRQS] = {
405 [0 ... NR_IRQS-1] = CPU_MASK_ALL
406};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700408void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
409{
410 balance_irq_affinity[irq] = mask;
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
414 unsigned long now, int direction)
415{
416 int search_idle = 1;
417 int cpu = curr_cpu;
418
419 goto inside;
420
421 do {
422 if (unlikely(cpu == curr_cpu))
423 search_idle = 0;
424inside:
425 if (direction == 1) {
426 cpu++;
427 if (cpu >= NR_CPUS)
428 cpu = 0;
429 } else {
430 cpu--;
431 if (cpu == -1)
432 cpu = NR_CPUS-1;
433 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200434 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu, allowed_mask) ||
435 (search_idle && !IDLE_ENOUGH(cpu, now)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return cpu;
438}
439
440static inline void balance_irq(int cpu, int irq)
441{
442 unsigned long now = jiffies;
443 cpumask_t allowed_mask;
444 unsigned int new_cpu;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (irqbalance_disabled)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200447 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700449 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 new_cpu = move(cpu, allowed_mask, now, 1);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200451 if (cpu != new_cpu)
Ashok Raj54d5d422005-09-06 15:16:15 -0700452 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
456{
457 int i, j;
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200458
Andrew Morton394e3902006-03-23 03:01:05 -0800459 for_each_online_cpu(i) {
460 for (j = 0; j < NR_IRQS; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!irq_desc[j].action)
462 continue;
463 /* Is it a significant load ? */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200464 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i), j) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 useful_load_threshold)
466 continue;
467 balance_irq(i, j);
468 }
469 }
470 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200471 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return;
473}
474
475static void do_irq_balance(void)
476{
477 int i, j;
478 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
479 unsigned long move_this_load = 0;
480 int max_loaded = 0, min_loaded = 0;
481 int load;
482 unsigned long useful_load_threshold = balanced_irq_interval + 10;
483 int selected_irq;
484 int tmp_loaded, first_attempt = 1;
485 unsigned long tmp_cpu_irq;
486 unsigned long imbalance = 0;
487 cpumask_t allowed_mask, target_cpu_mask, tmp;
488
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800489 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int package_index;
491 CPU_IRQ(i) = 0;
492 if (!cpu_online(i))
493 continue;
494 package_index = CPU_TO_PACKAGEINDEX(i);
495 for (j = 0; j < NR_IRQS; j++) {
496 unsigned long value_now, delta;
Thomas Gleixner950f4422007-02-16 01:27:24 -0800497 /* Is this an active IRQ or balancing disabled ? */
498 if (!irq_desc[j].action || irq_balancing_disabled(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200500 if (package_index == i)
501 IRQ_DELTA(package_index, j) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Determine the total count per processor per IRQ */
503 value_now = (unsigned long) kstat_cpu(i).irqs[j];
504
505 /* Determine the activity per processor per IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200506 delta = value_now - LAST_CPU_IRQ(i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* Update last_cpu_irq[][] for the next time */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200509 LAST_CPU_IRQ(i, j) = value_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Ignore IRQs whose rate is less than the clock */
512 if (delta < useful_load_threshold)
513 continue;
514 /* update the load for the processor or package total */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200515 IRQ_DELTA(package_index, j) += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* Keep track of the higher numbered sibling as well */
518 if (i != package_index)
519 CPU_IRQ(i) += delta;
520 /*
521 * We have sibling A and sibling B in the package
522 *
523 * cpu_irq[A] = load for cpu A + load for cpu B
524 * cpu_irq[B] = load for cpu B
525 */
526 CPU_IRQ(package_index) += delta;
527 }
528 }
529 /* Find the least loaded processor package */
Andrew Morton394e3902006-03-23 03:01:05 -0800530 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (i != CPU_TO_PACKAGEINDEX(i))
532 continue;
533 if (min_cpu_irq > CPU_IRQ(i)) {
534 min_cpu_irq = CPU_IRQ(i);
535 min_loaded = i;
536 }
537 }
538 max_cpu_irq = ULONG_MAX;
539
540tryanothercpu:
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200541 /*
542 * Look for heaviest loaded processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * We may come back to get the next heaviest loaded processor.
544 * Skip processors with trivial loads.
545 */
546 tmp_cpu_irq = 0;
547 tmp_loaded = -1;
Andrew Morton394e3902006-03-23 03:01:05 -0800548 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (i != CPU_TO_PACKAGEINDEX(i))
550 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200551 if (max_cpu_irq <= CPU_IRQ(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 continue;
553 if (tmp_cpu_irq < CPU_IRQ(i)) {
554 tmp_cpu_irq = CPU_IRQ(i);
555 tmp_loaded = i;
556 }
557 }
558
559 if (tmp_loaded == -1) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200560 /*
561 * In the case of small number of heavy interrupt sources,
562 * loading some of the cpus too much. We use Ingo's original
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * approach to rotate them around.
564 */
565 if (!first_attempt && imbalance >= useful_load_threshold) {
566 rotate_irqs_among_cpus(useful_load_threshold);
567 return;
568 }
569 goto not_worth_the_effort;
570 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 first_attempt = 0; /* heaviest search */
573 max_cpu_irq = tmp_cpu_irq; /* load */
574 max_loaded = tmp_loaded; /* processor */
575 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200576
577 /*
578 * if imbalance is less than approx 10% of max load, then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * observe diminishing returns action. - quit
580 */
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200581 if (imbalance < (max_cpu_irq >> 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto not_worth_the_effort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584tryanotherirq:
585 /* if we select an IRQ to move that can't go where we want, then
586 * see if there is another one to try.
587 */
588 move_this_load = 0;
589 selected_irq = -1;
590 for (j = 0; j < NR_IRQS; j++) {
591 /* Is this an active IRQ? */
592 if (!irq_desc[j].action)
593 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200594 if (imbalance <= IRQ_DELTA(max_loaded, j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 continue;
596 /* Try to find the IRQ that is closest to the imbalance
597 * without going over.
598 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200599 if (move_this_load < IRQ_DELTA(max_loaded, j)) {
600 move_this_load = IRQ_DELTA(max_loaded, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 selected_irq = j;
602 }
603 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200604 if (selected_irq == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto tryanothercpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 imbalance = move_this_load;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200608
Simon Arlott27b46d72007-10-20 01:13:56 +0200609 /* For physical_balance case, we accumulated both load
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * values in the one of the siblings cpu_irq[],
611 * to use the same code for physical and logical processors
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200612 * as much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
614 * NOTE: the cpu_irq[] array holds the sum of the load for
615 * sibling A and sibling B in the slot for the lowest numbered
616 * sibling (A), _AND_ the load for sibling B in the slot for
617 * the higher numbered sibling.
618 *
619 * We seek the least loaded sibling by making the comparison
620 * (A+B)/2 vs B
621 */
622 load = CPU_IRQ(min_loaded) >> 1;
Mike Travisd5a74302007-10-16 01:24:05 -0700623 for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (load > CPU_IRQ(j)) {
625 /* This won't change cpu_sibling_map[min_loaded] */
626 load = CPU_IRQ(j);
627 min_loaded = j;
628 }
629 }
630
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700631 cpus_and(allowed_mask,
632 cpu_online_map,
633 balance_irq_affinity[selected_irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 target_cpu_mask = cpumask_of_cpu(min_loaded);
635 cpus_and(tmp, target_cpu_mask, allowed_mask);
636
637 if (!cpus_empty(tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* mark for change destination */
Ashok Raj54d5d422005-09-06 15:16:15 -0700639 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
640
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200641 /* Since we made a change, come back sooner to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * check for more variation.
643 */
644 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200645 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647 }
648 goto tryanotherirq;
649
650not_worth_the_effort:
651 /*
652 * if we did not find an IRQ to move, then adjust the time interval
653 * upward
654 */
655 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200656 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return;
658}
659
660static int balanced_irq(void *unused)
661{
662 int i;
663 unsigned long prev_balance_time = jiffies;
664 long time_remaining = balanced_irq_interval;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* push everything to CPU 0 to give us a starting point. */
667 for (i = 0 ; i < NR_IRQS ; i++) {
Ingo Molnarcd916d32006-06-29 02:24:42 -0700668 irq_desc[i].pending_mask = cpumask_of_cpu(0);
Ashok Raj54d5d422005-09-06 15:16:15 -0700669 set_pending_irq(i, cpumask_of_cpu(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700672 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 for ( ; ; ) {
Nishanth Aravamudan52e6e632005-09-10 00:27:26 -0700674 time_remaining = schedule_timeout_interruptible(time_remaining);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700675 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (time_after(jiffies,
677 prev_balance_time+balanced_irq_interval)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700678 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 do_irq_balance();
680 prev_balance_time = jiffies;
681 time_remaining = balanced_irq_interval;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700682 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 }
685 return 0;
686}
687
688static int __init balanced_irq_init(void)
689{
690 int i;
691 struct cpuinfo_x86 *c;
692 cpumask_t tmp;
693
694 cpus_shift_right(tmp, cpu_online_map, 2);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200695 c = &boot_cpu_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* When not overwritten by the command line ask subarchitecture. */
697 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
698 irqbalance_disabled = NO_BALANCE_IRQ;
699 if (irqbalance_disabled)
700 return 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* disable irqbalance completely if there is only one processor online */
703 if (num_online_cpus() < 2) {
704 irqbalance_disabled = 1;
705 return 0;
706 }
707 /*
708 * Enable physical balance only if more than 1 physical processor
709 * is present
710 */
711 if (smp_num_siblings > 1 && !cpus_empty(tmp))
712 physical_balance = 1;
713
Andrew Morton394e3902006-03-23 03:01:05 -0800714 for_each_online_cpu(i) {
Christophe Jaillet25556c12008-06-22 22:13:48 +0200715 irq_cpu_data[i].irq_delta = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
716 irq_cpu_data[i].last_irq = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
718 printk(KERN_ERR "balanced_irq_init: out of memory");
719 goto failed;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 printk(KERN_INFO "Starting balanced_irq\n");
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200724 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200726 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727failed:
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800728 for_each_possible_cpu(i) {
Jesper Juhl4ae66732005-06-25 14:58:48 -0700729 kfree(irq_cpu_data[i].irq_delta);
Andrew Morton394e3902006-03-23 03:01:05 -0800730 irq_cpu_data[i].irq_delta = NULL;
Jesper Juhl4ae66732005-06-25 14:58:48 -0700731 kfree(irq_cpu_data[i].last_irq);
Andrew Morton394e3902006-03-23 03:01:05 -0800732 irq_cpu_data[i].last_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 return 0;
735}
736
Andrew Mortonc2481cc2007-04-08 16:04:04 -0700737int __devinit irqbalance_disable(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 irqbalance_disabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800740 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743__setup("noirqbalance", irqbalance_disable);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745late_initcall(balanced_irq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#endif /* CONFIG_IRQBALANCE */
Ashok Raj54d5d422005-09-06 15:16:15 -0700747#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100750void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 unsigned int cfg;
753
754 /*
755 * Wait for idle.
756 */
757 apic_wait_icr_idle();
758 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
759 /*
760 * Send the IPI. The write to APIC_ICR fires this off.
761 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100762 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764#endif /* !CONFIG_SMP */
765
766
767/*
768 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
769 * specific CPU-side IRQs.
770 */
771
772#define MAX_PIRQS 8
773static int pirq_entries [MAX_PIRQS];
774static int pirqs_enabled;
775int skip_ioapic_setup;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int __init ioapic_pirq_setup(char *str)
778{
779 int i, max;
780 int ints[MAX_PIRQS+1];
781
782 get_options(str, ARRAY_SIZE(ints), ints);
783
784 for (i = 0; i < MAX_PIRQS; i++)
785 pirq_entries[i] = -1;
786
787 pirqs_enabled = 1;
788 apic_printk(APIC_VERBOSE, KERN_INFO
789 "PIRQ redirection, working around broken MP-BIOS.\n");
790 max = MAX_PIRQS;
791 if (ints[0] < MAX_PIRQS)
792 max = ints[0];
793
794 for (i = 0; i < max; i++) {
795 apic_printk(APIC_VERBOSE, KERN_DEBUG
796 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
797 /*
798 * PIRQs are mapped upside down, usually.
799 */
800 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
801 }
802 return 1;
803}
804
805__setup("pirq=", ioapic_pirq_setup);
806
807/*
808 * Find the IRQ entry number of a certain pin.
809 */
810static int find_irq_entry(int apic, int pin, int type)
811{
812 int i;
813
814 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400815 if (mp_irqs[i].mp_irqtype == type &&
816 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
817 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
818 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return i;
820
821 return -1;
822}
823
824/*
825 * Find the pin to which IRQ[irq] (ISA) is connected
826 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800827static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 int i;
830
831 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400832 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300834 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400835 (mp_irqs[i].mp_irqtype == type) &&
836 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400838 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 return -1;
841}
842
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800843static int __init find_isa_irq_apic(int irq, int type)
844{
845 int i;
846
847 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400848 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800849
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300850 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400851 (mp_irqs[i].mp_irqtype == type) &&
852 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800853 break;
854 }
855 if (i < mp_irq_entries) {
856 int apic;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200857 for (apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400858 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800859 return apic;
860 }
861 }
862
863 return -1;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * Find a specific PCI IRQ entry.
868 * Not an __init, possibly needed by modules
869 */
870static int pin_2_irq(int idx, int apic, int pin);
871
872int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
873{
874 int apic, i, best_guess = -1;
875
876 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
877 "slot:%d, pin:%d.\n", bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400878 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
880 return -1;
881 }
882 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400883 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400886 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
887 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
889
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300890 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400891 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400893 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar3de352b2008-07-08 11:14:58 +0200894 int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (!(apic || IO_APIC_IRQ(irq)))
897 continue;
898
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400899 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return irq;
901 /*
902 * Use the first all-but-pin matching entry as a
903 * best-guess fuzzy result for broken mptables.
904 */
905 if (best_guess < 0)
906 best_guess = irq;
907 }
908 }
909 return best_guess;
910}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700911EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913/*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200914 * This function currently is only a helper for the i386 smp boot process where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * we need to reprogram the ioredtbls to cater for the cpus which have come online
916 * so mask in all cases should simply be TARGET_CPUS
917 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700918#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919void __init setup_ioapic_dest(void)
920{
921 int pin, ioapic, irq, irq_entry;
922
923 if (skip_ioapic_setup == 1)
924 return;
925
926 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
927 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
928 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
929 if (irq_entry == -1)
930 continue;
931 irq = pin_2_irq(irq_entry, ioapic, pin);
932 set_ioapic_affinity_irq(irq, TARGET_CPUS);
933 }
934
935 }
936}
Ashok Raj54d5d422005-09-06 15:16:15 -0700937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300939#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*
941 * EISA Edge/Level control register, ELCR
942 */
943static int EISA_ELCR(unsigned int irq)
944{
945 if (irq < 16) {
946 unsigned int port = 0x4d0 + (irq >> 3);
947 return (inb(port) >> (irq & 7)) & 1;
948 }
949 apic_printk(APIC_VERBOSE, KERN_INFO
950 "Broken MPtable reports ISA irq %d\n", irq);
951 return 0;
952}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300955/* ISA interrupts are always polarity zero edge triggered,
956 * when listed as conforming in the MP table. */
957
958#define default_ISA_trigger(idx) (0)
959#define default_ISA_polarity(idx) (0)
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/* EISA interrupts are always polarity zero and can be edge or level
962 * trigger depending on the ELCR value. If an interrupt is listed as
963 * EISA conforming in the MP table, that means its trigger type must
964 * be read in from the ELCR */
965
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400966#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300967#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969/* PCI interrupts are always polarity one level triggered,
970 * when listed as conforming in the MP table. */
971
972#define default_PCI_trigger(idx) (1)
973#define default_PCI_polarity(idx) (1)
974
975/* MCA interrupts are always polarity zero level triggered,
976 * when listed as conforming in the MP table. */
977
978#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300979#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Shaohua Li61fd47e2007-11-17 01:05:28 -0500981static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400983 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int polarity;
985
986 /*
987 * Determine IRQ line polarity (high active or low active):
988 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200989 switch (mp_irqs[idx].mp_irqflag & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200990 case 0: /* conforms, ie. bus-type dependent polarity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200992 polarity = test_bit(bus, mp_bus_not_pci)?
993 default_ISA_polarity(idx):
994 default_PCI_polarity(idx);
995 break;
996 }
997 case 1: /* high active */
998 {
999 polarity = 0;
1000 break;
1001 }
1002 case 2: /* reserved */
1003 {
1004 printk(KERN_WARNING "broken BIOS!!\n");
1005 polarity = 1;
1006 break;
1007 }
1008 case 3: /* low active */
1009 {
1010 polarity = 1;
1011 break;
1012 }
1013 default: /* invalid */
1014 {
1015 printk(KERN_WARNING "broken BIOS!!\n");
1016 polarity = 1;
1017 break;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 return polarity;
1021}
1022
1023static int MPBIOS_trigger(int idx)
1024{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001025 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int trigger;
1027
1028 /*
1029 * Determine IRQ trigger mode (edge or level sensitive):
1030 */
Ingo Molnar3de352b2008-07-08 11:14:58 +02001031 switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001032 case 0: /* conforms, ie. bus-type dependent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001034 trigger = test_bit(bus, mp_bus_not_pci)?
1035 default_ISA_trigger(idx):
1036 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001037#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001038 switch (mp_bus_id_to_type[bus]) {
1039 case MP_BUS_ISA: /* ISA pin */
1040 {
1041 /* set before the switch */
1042 break;
1043 }
1044 case MP_BUS_EISA: /* EISA pin */
1045 {
1046 trigger = default_EISA_trigger(idx);
1047 break;
1048 }
1049 case MP_BUS_PCI: /* PCI pin */
1050 {
1051 /* set before the switch */
1052 break;
1053 }
1054 case MP_BUS_MCA: /* MCA pin */
1055 {
1056 trigger = default_MCA_trigger(idx);
1057 break;
1058 }
1059 default:
1060 {
1061 printk(KERN_WARNING "broken BIOS!!\n");
1062 trigger = 1;
1063 break;
1064 }
1065 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001066#endif
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001067 break;
1068 }
1069 case 1: /* edge */
1070 {
1071 trigger = 0;
1072 break;
1073 }
1074 case 2: /* reserved */
1075 {
1076 printk(KERN_WARNING "broken BIOS!!\n");
1077 trigger = 1;
1078 break;
1079 }
1080 case 3: /* level */
1081 {
1082 trigger = 1;
1083 break;
1084 }
1085 default: /* invalid */
1086 {
1087 printk(KERN_WARNING "broken BIOS!!\n");
1088 trigger = 0;
1089 break;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 return trigger;
1093}
1094
1095static inline int irq_polarity(int idx)
1096{
1097 return MPBIOS_polarity(idx);
1098}
1099
1100static inline int irq_trigger(int idx)
1101{
1102 return MPBIOS_trigger(idx);
1103}
1104
1105static int pin_2_irq(int idx, int apic, int pin)
1106{
1107 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001108 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 /*
1111 * Debugging check, we are in big trouble if this message pops up!
1112 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001113 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1115
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001116 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001117 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001118 else {
1119 /*
1120 * PCI IRQs are mapped in order
1121 */
1122 i = irq = 0;
1123 while (i < apic)
1124 irq += nr_ioapic_registers[i++];
1125 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001127 /*
1128 * For MPS mode, so far only needed by ES7000 platform
1129 */
1130 if (ioapic_renumber_irq)
1131 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
1134 /*
1135 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1136 */
1137 if ((pin >= 16) && (pin <= 23)) {
1138 if (pirq_entries[pin-16] != -1) {
1139 if (!pirq_entries[pin-16]) {
1140 apic_printk(APIC_VERBOSE, KERN_DEBUG
1141 "disabling PIRQ%d\n", pin-16);
1142 } else {
1143 irq = pirq_entries[pin-16];
1144 apic_printk(APIC_VERBOSE, KERN_DEBUG
1145 "using PIRQ%d -> IRQ %d\n",
1146 pin-16, irq);
1147 }
1148 }
1149 }
1150 return irq;
1151}
1152
1153static inline int IO_APIC_irq_trigger(int irq)
1154{
1155 int apic, idx, pin;
1156
1157 for (apic = 0; apic < nr_ioapics; apic++) {
1158 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001159 idx = find_irq_entry(apic, pin, mp_INT);
1160 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return irq_trigger(idx);
1162 }
1163 }
1164 /*
1165 * nonexistent IRQs are edge default
1166 */
1167 return 0;
1168}
1169
1170/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Adrian Bunk7e95b592006-12-07 02:14:11 +01001171static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001173static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001175 static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001176 int vector, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001178 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
Jan Beulich0a1ad602006-06-26 13:56:43 +02001179
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001180 if (irq_vector[irq] > 0)
1181 return irq_vector[irq];
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001182
Jan Beulich0a1ad602006-06-26 13:56:43 +02001183 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001184 offset = current_offset;
1185next:
1186 vector += 8;
Alan Mayer305b92a2008-04-15 15:36:56 -05001187 if (vector >= first_system_vector) {
Eric W. Biederman8339f002007-01-29 13:19:05 -07001188 offset = (offset + 1) % 8;
1189 vector = FIRST_DEVICE_VECTOR + offset;
1190 }
1191 if (vector == current_vector)
1192 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001193 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -07001194 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001195
1196 current_vector = vector;
1197 current_offset = offset;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001198 irq_vector[irq] = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +02001199
1200 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001203static int assign_irq_vector(int irq)
1204{
1205 unsigned long flags;
1206 int vector;
1207
1208 spin_lock_irqsave(&vector_lock, flags);
1209 vector = __assign_irq_vector(irq);
1210 spin_unlock_irqrestore(&vector_lock, flags);
1211
1212 return vector;
1213}
Glauber Costa3fde6902008-05-28 20:34:19 -07001214
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001215static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217#define IOAPIC_AUTO -1
1218#define IOAPIC_EDGE 0
1219#define IOAPIC_LEVEL 1
1220
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001221static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jan Beulich6ebcc002006-06-26 13:56:46 +02001223 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001224 trigger == IOAPIC_LEVEL) {
1225 irq_desc[irq].status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001226 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1227 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001228 } else {
1229 irq_desc[irq].status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001230 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1231 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001232 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001233 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static void __init setup_IO_APIC_irqs(void)
1237{
1238 struct IO_APIC_route_entry entry;
1239 int apic, pin, idx, irq, first_notcon = 1, vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1242
1243 for (apic = 0; apic < nr_ioapics; apic++) {
1244 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1245
1246 /*
1247 * add it to the IO-APIC irq-routing table:
1248 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001249 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 entry.delivery_mode = INT_DELIVERY_MODE;
1252 entry.dest_mode = INT_DEST_MODE;
1253 entry.mask = 0; /* enable IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001254 entry.dest.logical.logical_dest =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 cpu_mask_to_apicid(TARGET_CPUS);
1256
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001257 idx = find_irq_entry(apic, pin, mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (idx == -1) {
1259 if (first_notcon) {
1260 apic_printk(APIC_VERBOSE, KERN_DEBUG
1261 " IO-APIC (apicid-pin) %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001262 mp_ioapics[apic].mp_apicid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 pin);
1264 first_notcon = 0;
1265 } else
1266 apic_printk(APIC_VERBOSE, ", %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001267 mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 continue;
1269 }
1270
Yinghai Lu20d225b2007-10-17 18:04:41 +02001271 if (!first_notcon) {
1272 apic_printk(APIC_VERBOSE, " not connected.\n");
1273 first_notcon = 1;
1274 }
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 entry.trigger = irq_trigger(idx);
1277 entry.polarity = irq_polarity(idx);
1278
1279 if (irq_trigger(idx)) {
1280 entry.trigger = 1;
1281 entry.mask = 1;
1282 }
1283
1284 irq = pin_2_irq(idx, apic, pin);
1285 /*
1286 * skip adding the timer int on secondary nodes, which causes
1287 * a small but painful rift in the time-space continuum
1288 */
1289 if (multi_timer_check(apic, irq))
1290 continue;
1291 else
1292 add_pin_to_irq(irq, apic, pin);
1293
1294 if (!apic && !IO_APIC_IRQ(irq))
1295 continue;
1296
1297 if (IO_APIC_IRQ(irq)) {
1298 vector = assign_irq_vector(irq);
1299 entry.vector = vector;
1300 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!apic && (irq < 16))
1303 disable_8259A_irq(irq);
1304 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09001305 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 }
1308
1309 if (!first_notcon)
1310 apic_printk(APIC_VERBOSE, " not connected.\n");
1311}
1312
1313/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001314 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001316static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1317 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001321 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /*
1324 * We use logical delivery to get the timer IRQ
1325 * to the first CPU.
1326 */
1327 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001328 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1330 entry.delivery_mode = INT_DELIVERY_MODE;
1331 entry.polarity = 0;
1332 entry.trigger = 0;
1333 entry.vector = vector;
1334
1335 /*
1336 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001337 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Maciej W. Rozyckif0825262008-05-27 21:19:16 +01001339 ioapic_register_intr(0, vector, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /*
1342 * Add it to the IO-APIC irq-routing table:
1343 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001344 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001347
1348__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 int apic, i;
1351 union IO_APIC_reg_00 reg_00;
1352 union IO_APIC_reg_01 reg_01;
1353 union IO_APIC_reg_02 reg_02;
1354 union IO_APIC_reg_03 reg_03;
1355 unsigned long flags;
1356
1357 if (apic_verbosity == APIC_QUIET)
1358 return;
1359
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001360 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 for (i = 0; i < nr_ioapics; i++)
1362 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001363 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * We are a bit conservative about what we expect. We have to
1367 * know about every hardware change ASAP.
1368 */
1369 printk(KERN_INFO "testing the IO APIC.......................\n");
1370
1371 for (apic = 0; apic < nr_ioapics; apic++) {
1372
1373 spin_lock_irqsave(&ioapic_lock, flags);
1374 reg_00.raw = io_apic_read(apic, 0);
1375 reg_01.raw = io_apic_read(apic, 1);
1376 if (reg_01.bits.version >= 0x10)
1377 reg_02.raw = io_apic_read(apic, 2);
1378 if (reg_01.bits.version >= 0x20)
1379 reg_03.raw = io_apic_read(apic, 3);
1380 spin_unlock_irqrestore(&ioapic_lock, flags);
1381
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001382 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1384 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1385 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1386 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1389 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1392 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /*
1395 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1396 * but the value of reg_02 is read as the previous read register
1397 * value, so ignore it if reg_02 == reg_01.
1398 */
1399 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1400 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1401 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
1404 /*
1405 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1406 * or reg_03, but the value of reg_0[23] is read as the previous read
1407 * register value, so ignore it if reg_03 == reg_0[12].
1408 */
1409 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1410 reg_03.raw != reg_01.raw) {
1411 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1412 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1416
1417 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1418 " Stat Dest Deli Vect: \n");
1419
1420 for (i = 0; i <= reg_01.bits.entries; i++) {
1421 struct IO_APIC_route_entry entry;
1422
Andi Kleencf4c6a22006-09-26 10:52:30 +02001423 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 printk(KERN_DEBUG " %02x %03X %02X ",
1426 i,
1427 entry.dest.logical.logical_dest,
1428 entry.dest.physical.physical_dest
1429 );
1430
1431 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1432 entry.mask,
1433 entry.trigger,
1434 entry.irr,
1435 entry.polarity,
1436 entry.delivery_status,
1437 entry.dest_mode,
1438 entry.delivery_mode,
1439 entry.vector
1440 );
1441 }
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1444 for (i = 0; i < NR_IRQS; i++) {
1445 struct irq_pin_list *entry = irq_2_pin + i;
1446 if (entry->pin < 0)
1447 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001448 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 for (;;) {
1450 printk("-> %d:%d", entry->apic, entry->pin);
1451 if (!entry->next)
1452 break;
1453 entry = irq_2_pin + entry->next;
1454 }
1455 printk("\n");
1456 }
1457
1458 printk(KERN_INFO ".................................... done.\n");
1459
1460 return;
1461}
1462
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001463__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 unsigned int v;
1466 int i, j;
1467
1468 if (apic_verbosity == APIC_QUIET)
1469 return;
1470
1471 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1472 for (i = 0; i < 8; i++) {
1473 v = apic_read(base + i*0x10);
1474 for (j = 0; j < 32; j++) {
1475 if (v & (1<<j))
1476 printk("1");
1477 else
1478 printk("0");
1479 }
1480 printk("\n");
1481 }
1482}
1483
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001484__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001487 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (apic_verbosity == APIC_QUIET)
1490 return;
1491
1492 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1493 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001494 v = apic_read(APIC_ID);
Jack Steiner05f2d122008-03-28 14:12:02 -05001495 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001496 GET_APIC_ID(v));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 v = apic_read(APIC_LVR);
1498 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1499 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001500 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 v = apic_read(APIC_TASKPRI);
1503 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1504
1505 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1506 v = apic_read(APIC_ARBPRI);
1507 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1508 v & APIC_ARBPRI_MASK);
1509 v = apic_read(APIC_PROCPRI);
1510 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1511 }
1512
1513 v = apic_read(APIC_EOI);
1514 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1515 v = apic_read(APIC_RRR);
1516 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1517 v = apic_read(APIC_LDR);
1518 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1519 v = apic_read(APIC_DFR);
1520 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1521 v = apic_read(APIC_SPIV);
1522 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1523
1524 printk(KERN_DEBUG "... APIC ISR field:\n");
1525 print_APIC_bitfield(APIC_ISR);
1526 printk(KERN_DEBUG "... APIC TMR field:\n");
1527 print_APIC_bitfield(APIC_TMR);
1528 printk(KERN_DEBUG "... APIC IRR field:\n");
1529 print_APIC_bitfield(APIC_IRR);
1530
1531 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1532 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1533 apic_write(APIC_ESR, 0);
1534 v = apic_read(APIC_ESR);
1535 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1536 }
1537
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001538 icr = apic_icr_read();
1539 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1540 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 v = apic_read(APIC_LVTT);
1543 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1544
1545 if (maxlvt > 3) { /* PC is LVT#4. */
1546 v = apic_read(APIC_LVTPC);
1547 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1548 }
1549 v = apic_read(APIC_LVT0);
1550 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1551 v = apic_read(APIC_LVT1);
1552 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1553
1554 if (maxlvt > 2) { /* ERR is LVT#3. */
1555 v = apic_read(APIC_LVTERR);
1556 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1557 }
1558
1559 v = apic_read(APIC_TMICT);
1560 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1561 v = apic_read(APIC_TMCCT);
1562 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1563 v = apic_read(APIC_TDCR);
1564 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1565 printk("\n");
1566}
1567
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001568__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001570 on_each_cpu(print_local_APIC, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001573__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 unsigned int v;
1576 unsigned long flags;
1577
1578 if (apic_verbosity == APIC_QUIET)
1579 return;
1580
1581 printk(KERN_DEBUG "\nprinting PIC contents\n");
1582
1583 spin_lock_irqsave(&i8259A_lock, flags);
1584
1585 v = inb(0xa1) << 8 | inb(0x21);
1586 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1587
1588 v = inb(0xa0) << 8 | inb(0x20);
1589 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1590
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001591 outb(0x0b, 0xa0);
1592 outb(0x0b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 v = inb(0xa0) << 8 | inb(0x20);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001594 outb(0x0a, 0xa0);
1595 outb(0x0a, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 spin_unlock_irqrestore(&i8259A_lock, flags);
1598
1599 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1600
1601 v = inb(0x4d1) << 8 | inb(0x4d0);
1602 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1603}
1604
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001605__apicdebuginit(int) print_all_ICs(void)
1606{
1607 print_PIC();
1608 print_all_local_APICs();
1609 print_IO_APIC();
1610
1611 return 0;
1612}
1613
1614fs_initcall(print_all_ICs);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617static void __init enable_IO_APIC(void)
1618{
1619 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001620 int i8259_apic, i8259_pin;
1621 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 unsigned long flags;
1623
1624 for (i = 0; i < PIN_MAP_SIZE; i++) {
1625 irq_2_pin[i].pin = -1;
1626 irq_2_pin[i].next = 0;
1627 }
1628 if (!pirqs_enabled)
1629 for (i = 0; i < MAX_PIRQS; i++)
1630 pirq_entries[i] = -1;
1631
1632 /*
1633 * The number of IO-APIC IRQ registers (== #pins):
1634 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001635 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001637 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001639 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1640 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001641 for (apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001642 int pin;
1643 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001644 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001645 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001646 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001647
1648
1649 /* If the interrupt line is enabled and in ExtInt mode
1650 * I have found the pin where the i8259 is connected.
1651 */
1652 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1653 ioapic_i8259.apic = apic;
1654 ioapic_i8259.pin = pin;
1655 goto found_i8259;
1656 }
1657 }
1658 }
1659 found_i8259:
1660 /* Look to see what if the MP table has reported the ExtINT */
1661 /* If we could not find the appropriate pin by looking at the ioapic
1662 * the i8259 probably is not connected the ioapic but give the
1663 * mptable a chance anyway.
1664 */
1665 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1666 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1667 /* Trust the MP table if nothing is setup in the hardware */
1668 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1669 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1670 ioapic_i8259.pin = i8259_pin;
1671 ioapic_i8259.apic = i8259_apic;
1672 }
1673 /* Complain if the MP table and the hardware disagree */
1674 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1675 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1676 {
1677 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
1680 /*
1681 * Do not trust the IO-APIC being empty at bootup
1682 */
1683 clear_IO_APIC();
1684}
1685
1686/*
1687 * Not an __init, needed by the reboot code
1688 */
1689void disable_IO_APIC(void)
1690{
1691 /*
1692 * Clear the IO-APIC before rebooting:
1693 */
1694 clear_IO_APIC();
1695
Eric W. Biederman650927e2005-06-25 14:57:44 -07001696 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001697 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001698 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001699 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001700 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001701 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001702 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001703
1704 memset(&entry, 0, sizeof(entry));
1705 entry.mask = 0; /* Enabled */
1706 entry.trigger = 0; /* Edge */
1707 entry.irr = 0;
1708 entry.polarity = 0; /* High */
1709 entry.delivery_status = 0;
1710 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001711 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001712 entry.vector = 0;
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001713 entry.dest.physical.physical_dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001714
1715 /*
1716 * Add it to the IO-APIC irq-routing table:
1717 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001718 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001719 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001720 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723/*
1724 * function to set the IO-APIC physical IDs based on the
1725 * values stored in the MPC table.
1726 *
1727 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1728 */
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730static void __init setup_ioapic_ids_from_mpc(void)
1731{
1732 union IO_APIC_reg_00 reg_00;
1733 physid_mask_t phys_id_present_map;
1734 int apic;
1735 int i;
1736 unsigned char old_id;
1737 unsigned long flags;
1738
Yinghai Lua4dbc342008-07-25 02:14:28 -07001739 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001740 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001743 * Don't check I/O APIC IDs for xAPIC systems. They have
1744 * no meaning without the serial APIC bus.
1745 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001746 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1747 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001748 return;
1749 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * This is broken; anything with a real cpu count has to
1751 * circumvent this idiocy regardless.
1752 */
1753 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1754
1755 /*
1756 * Set the IOAPIC ID to the value stored in the MPC table.
1757 */
1758 for (apic = 0; apic < nr_ioapics; apic++) {
1759
1760 /* Read the register 0 value */
1761 spin_lock_irqsave(&ioapic_lock, flags);
1762 reg_00.raw = io_apic_read(apic, 0);
1763 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001764
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001765 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001767 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001769 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1771 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001772 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /*
1776 * Sanity check, is the ID really free? Every APIC in a
1777 * system must have a unique ID or we get lots of nice
1778 * 'stuck on smp_invalidate_needed IPI wait' messages.
1779 */
1780 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001781 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001783 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 for (i = 0; i < get_physical_broadcast(); i++)
1785 if (!physid_isset(i, phys_id_present_map))
1786 break;
1787 if (i >= get_physical_broadcast())
1788 panic("Max APIC ID exceeded!\n");
1789 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1790 i);
1791 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001792 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 } else {
1794 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001795 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 apic_printk(APIC_VERBOSE, "Setting %d in the "
1797 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001798 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1800 }
1801
1802
1803 /*
1804 * We need to adjust the IRQ routing table
1805 * if the ID changed.
1806 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001807 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001809 if (mp_irqs[i].mp_dstapic == old_id)
1810 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001811 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /*
1814 * Read the right value from the MPC table and
1815 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 apic_printk(APIC_VERBOSE, KERN_INFO
1818 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001819 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001821 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 spin_lock_irqsave(&ioapic_lock, flags);
1823 io_apic_write(apic, 0, reg_00.raw);
1824 spin_unlock_irqrestore(&ioapic_lock, flags);
1825
1826 /*
1827 * Sanity check
1828 */
1829 spin_lock_irqsave(&ioapic_lock, flags);
1830 reg_00.raw = io_apic_read(apic, 0);
1831 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001832 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 printk("could not set ID!\n");
1834 else
1835 apic_printk(APIC_VERBOSE, " ok.\n");
1836 }
1837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001839int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001840
1841static int __init notimercheck(char *s)
1842{
1843 no_timer_check = 1;
1844 return 1;
1845}
1846__setup("no_timer_check", notimercheck);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/*
1849 * There is a nasty bug in some older SMP boards, their mptable lies
1850 * about the timer IRQ. We do the following to work around the situation:
1851 *
1852 * - timer IRQ defaults to IO-APIC IRQ
1853 * - if this function detects that timer IRQs are defunct, then we fall
1854 * back to ISA timer IRQs
1855 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001856static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001859 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Zachary Amsden8542b202006-12-07 02:14:09 +01001861 if (no_timer_check)
1862 return 1;
1863
Ingo Molnar4aae0702007-12-18 18:05:58 +01001864 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 local_irq_enable();
1866 /* Let ten ticks pass... */
1867 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001868 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /*
1871 * Expect a few ticks at least, to be sure some possible
1872 * glue logic does not lock up after one or two first
1873 * ticks in a non-ExtINT mode. Also the local APIC
1874 * might have cached one ExtINT interrupt. Finally, at
1875 * least one tick may be lost due to delays.
1876 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001877 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return 1;
1879
1880 return 0;
1881}
1882
1883/*
1884 * In the SMP+IOAPIC case it might happen that there are an unspecified
1885 * number of pending IRQ events unhandled. These cases are very rare,
1886 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1887 * better to do it this way as thus we do not have to be aware of
1888 * 'pending' interrupts in the IRQ path, except at this point.
1889 */
1890/*
1891 * Edge triggered needs to resend any interrupt
1892 * that was delayed but this is now handled in the device
1893 * independent code.
1894 */
1895
1896/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001897 * Startup quirk:
1898 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * Starting up a edge-triggered IO-APIC interrupt is
1900 * nasty - we need to make sure that we get the edge.
1901 * If it is already asserted for some reason, we need
1902 * return 1 to indicate that is was pending.
1903 *
1904 * This is not complete - we should be able to fake
1905 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001906 *
1907 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001909static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 int was_pending = 0;
1912 unsigned long flags;
1913
1914 spin_lock_irqsave(&ioapic_lock, flags);
1915 if (irq < 16) {
1916 disable_8259A_irq(irq);
1917 if (i8259A_irq_pending(irq))
1918 was_pending = 1;
1919 }
1920 __unmask_IO_APIC_irq(irq);
1921 spin_unlock_irqrestore(&ioapic_lock, flags);
1922
1923 return was_pending;
1924}
1925
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001926static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001928 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 ack_APIC_irq();
1930}
1931
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001932static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 unsigned long v;
1935 int i;
1936
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001937 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/*
1939 * It appears there is an erratum which affects at least version 0x11
1940 * of I/O APIC (that's the 82093AA and cores integrated into various
1941 * chipsets). Under certain conditions a level-triggered interrupt is
1942 * erroneously delivered as edge-triggered one but the respective IRR
1943 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1944 * message but it will never arrive and further interrupts are blocked
1945 * from the source. The exact reason is so far unknown, but the
1946 * phenomenon was observed when two consecutive interrupt requests
1947 * from a given source get delivered to the same CPU and the source is
1948 * temporarily disabled in between.
1949 *
1950 * A workaround is to simulate an EOI message manually. We achieve it
1951 * by setting the trigger mode to edge and then to level when the edge
1952 * trigger mode gets detected in the TMR of a local APIC for a
1953 * level-triggered interrupt. We mask the source for the time of the
1954 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1955 * The idea is from Manfred Spraul. --macro
1956 */
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001957 i = irq_vector[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1960
1961 ack_APIC_irq();
1962
1963 if (!(v & (1 << (i & 0x1f)))) {
1964 atomic_inc(&irq_mis_count);
1965 spin_lock(&ioapic_lock);
1966 __mask_and_edge_IO_APIC_irq(irq);
1967 __unmask_and_level_IO_APIC_irq(irq);
1968 spin_unlock(&ioapic_lock);
1969 }
1970}
1971
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001972static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001974 send_IPI_self(irq_vector[irq]);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001975
1976 return 1;
1977}
1978
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001979static struct irq_chip ioapic_chip __read_mostly = {
1980 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001981 .startup = startup_ioapic_irq,
1982 .mask = mask_IO_APIC_irq,
1983 .unmask = unmask_IO_APIC_irq,
1984 .ack = ack_ioapic_irq,
1985 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001986#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001987 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001988#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001989 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990};
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993static inline void init_IO_APIC_traps(void)
1994{
1995 int irq;
1996
1997 /*
1998 * NOTE! The local APIC isn't very good at handling
1999 * multiple interrupts at the same interrupt level.
2000 * As the interrupt level is determined by taking the
2001 * vector number and shifting that right by 4, we
2002 * want to spread these out a bit so that they don't
2003 * all fall in the same interrupt level.
2004 *
2005 * Also, we've got to be careful not to trash gate
2006 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2007 */
2008 for (irq = 0; irq < NR_IRQS ; irq++) {
Akinobu Mitaaddfc662008-04-05 22:39:07 +09002009 if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /*
2011 * Hmm.. We don't have an entry for this,
2012 * so default to an old-fashioned 8259
2013 * interrupt if we can..
2014 */
2015 if (irq < 16)
2016 make_8259A_irq(irq);
2017 else
2018 /* Strange. Oh, well.. */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002019 irq_desc[irq].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 }
2022}
2023
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002024/*
2025 * The local APIC irq-chip implementation:
2026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002028static void ack_lapic_irq(unsigned int irq)
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002029{
2030 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002033static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 unsigned long v;
2036
2037 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002038 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002041static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002043 unsigned long v;
2044
2045 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002046 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002049static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002050 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002051 .mask = mask_lapic_irq,
2052 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002053 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054};
2055
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002056static void lapic_register_intr(int irq, int vector)
2057{
2058 irq_desc[irq].status &= ~IRQ_LEVEL;
2059 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2060 "edge");
2061 set_intr_gate(vector, interrupt[irq]);
2062}
2063
Jan Beuliche9427102008-01-30 13:31:24 +01002064static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002067 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 * We put the 8259A master into AEOI mode and
2069 * unmask on all local APICs LVT0 as NMI.
2070 *
2071 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2072 * is from Maciej W. Rozycki - so we do not have to EOI from
2073 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2076
Jan Beuliche9427102008-01-30 13:31:24 +01002077 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 apic_printk(APIC_VERBOSE, " done.\n");
2080}
2081
2082/*
2083 * This looks a bit hackish but it's about the only one way of sending
2084 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2085 * not support the ExtINT mode, unfortunately. We need to send these
2086 * cycles as some i82489DX-based boards have glue logic that keeps the
2087 * 8259A interrupt line asserted until INTA. --macro
2088 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002089static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002091 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct IO_APIC_route_entry entry0, entry1;
2093 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002095 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002096 if (pin == -1) {
2097 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002099 }
2100 apic = find_isa_irq_apic(8, mp_INT);
2101 if (apic == -1) {
2102 WARN_ON_ONCE(1);
2103 return;
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Andi Kleencf4c6a22006-09-26 10:52:30 +02002106 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002107 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 memset(&entry1, 0, sizeof(entry1));
2110
2111 entry1.dest_mode = 0; /* physical delivery */
2112 entry1.mask = 0; /* unmask IRQ now */
2113 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2114 entry1.delivery_mode = dest_ExtINT;
2115 entry1.polarity = entry0.polarity;
2116 entry1.trigger = 0;
2117 entry1.vector = 0;
2118
Andi Kleencf4c6a22006-09-26 10:52:30 +02002119 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 save_control = CMOS_READ(RTC_CONTROL);
2122 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2123 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2124 RTC_FREQ_SELECT);
2125 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2126
2127 i = 100;
2128 while (i-- > 0) {
2129 mdelay(10);
2130 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2131 i -= 10;
2132 }
2133
2134 CMOS_WRITE(save_control, RTC_CONTROL);
2135 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002136 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Andi Kleencf4c6a22006-09-26 10:52:30 +02002138 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141/*
2142 * This code may look a bit paranoid, but it's supposed to cooperate with
2143 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2144 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2145 * fanatically on his truly buggy board.
2146 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002147static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002149 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002150 int no_pin1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 int vector;
Ingo Molnar6e908942008-03-21 14:32:36 +01002152 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002153 unsigned long flags;
2154
2155 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002156
Ingo Molnar6e908942008-03-21 14:32:36 +01002157 ver = apic_read(APIC_LVR);
2158 ver = GET_APIC_VERSION(ver);
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
2161 * get/set the timer IRQ vector:
2162 */
2163 disable_8259A_irq(0);
2164 vector = assign_irq_vector(0);
2165 set_intr_gate(vector, interrupt[0]);
2166
2167 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002168 * As IRQ0 is to be enabled in the 8259A, the virtual
2169 * wire has to be disabled in the local APIC. Also
2170 * timer interrupts need to be acknowledged manually in
2171 * the 8259A for the i82489DX when using the NMI
2172 * watchdog as that APIC treats NMIs as level-triggered.
2173 * The AEOI mode will finish them in the 8259A
2174 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002176 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002178 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002180 pin1 = find_isa_irq_pin(0, mp_INT);
2181 apic1 = find_isa_irq_apic(0, mp_INT);
2182 pin2 = ioapic_i8259.pin;
2183 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002185 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2186 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2187 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002189 /*
2190 * Some BIOS writers are clueless and report the ExtINTA
2191 * I/O APIC input from the cascaded 8259A as the timer
2192 * interrupt input. So just in case, if only one pin
2193 * was found above, try it both directly and through the
2194 * 8259A.
2195 */
2196 if (pin1 == -1) {
2197 pin1 = pin2;
2198 apic1 = apic2;
2199 no_pin1 = 1;
2200 } else if (pin2 == -1) {
2201 pin2 = pin1;
2202 apic2 = apic1;
2203 }
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (pin1 != -1) {
2206 /*
2207 * Ok, does IRQ0 through the IOAPIC work?
2208 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002209 if (no_pin1) {
2210 add_pin_to_irq(0, apic1, pin1);
2211 setup_timer_IRQ0_pin(apic1, pin1, vector);
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 unmask_IO_APIC_irq(0);
2214 if (timer_irq_works()) {
2215 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 setup_nmi();
2217 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002219 if (disable_timer_pin_1 > 0)
2220 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002223 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002224 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002225 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2226 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002228 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2229 "(IRQ0) through the 8259A ...\n");
2230 apic_printk(APIC_QUIET, KERN_INFO
2231 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /*
2233 * legacy devices should be connected to IO APIC #0
2234 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002235 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01002236 setup_timer_IRQ0_pin(apic2, pin2, vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002237 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002238 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002240 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002241 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002243 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002245 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249 /*
2250 * Cleanup, just in case ...
2251 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002252 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002253 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002254 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002258 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2259 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002260 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002262 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002264 apic_printk(APIC_QUIET, KERN_INFO
2265 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002267 lapic_register_intr(0, vector);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002268 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 enable_8259A_irq(0);
2270
2271 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002272 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002275 disable_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002276 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002277 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002279 apic_printk(APIC_QUIET, KERN_INFO
2280 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 init_8259A(0);
2283 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002284 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 unlock_ExtINT_logic();
2287
2288 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002289 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002292 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002294 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002295out:
2296 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
2299/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002300 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2301 * to devices. However there may be an I/O APIC pin available for
2302 * this interrupt regardless. The pin may be left unconnected, but
2303 * typically it will be reused as an ExtINT cascade interrupt for
2304 * the master 8259A. In the MPS case such a pin will normally be
2305 * reported as an ExtINT interrupt in the MP table. With ACPI
2306 * there is no provision for ExtINT interrupts, and in the absence
2307 * of an override it would be treated as an ordinary ISA I/O APIC
2308 * interrupt, that is edge-triggered and unmasked by default. We
2309 * used to do this, but it caused problems on some systems because
2310 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2311 * the same ExtINT cascade interrupt to drive the local APIC of the
2312 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2313 * the I/O APIC in all cases now. No actual device should request
2314 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
2316#define PIC_IRQS (1 << PIC_CASCADE_IR)
2317
2318void __init setup_IO_APIC(void)
2319{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002320 int i;
2321
2322 /* Reserve all the system vectors. */
Alan Mayer305b92a2008-04-15 15:36:56 -05002323 for (i = first_system_vector; i < NR_VECTORS; i++)
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002324 set_bit(i, used_vectors);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 enable_IO_APIC();
2327
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002328 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 printk("ENABLING IO-APIC IRQs\n");
2331
2332 /*
2333 * Set up IO-APIC IRQ routing.
2334 */
2335 if (!acpi_ioapic)
2336 setup_ioapic_ids_from_mpc();
2337 sync_Arb_IDs();
2338 setup_IO_APIC_irqs();
2339 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002340 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343/*
2344 * Called after all the initialization is done. If we didnt find any
2345 * APIC bugs then we can allow the modify fast path
2346 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348static int __init io_apic_bug_finalize(void)
2349{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002350 if (sis_apic_bug == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 sis_apic_bug = 0;
2352 return 0;
2353}
2354
2355late_initcall(io_apic_bug_finalize);
2356
2357struct sysfs_ioapic_data {
2358 struct sys_device dev;
2359 struct IO_APIC_route_entry entry[0];
2360};
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002361static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Pavel Machek438510f2005-04-16 15:25:24 -07002363static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 struct IO_APIC_route_entry *entry;
2366 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 data = container_of(dev, struct sysfs_ioapic_data, dev);
2370 entry = data->entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002371 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002372 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 return 0;
2375}
2376
2377static int ioapic_resume(struct sys_device *dev)
2378{
2379 struct IO_APIC_route_entry *entry;
2380 struct sysfs_ioapic_data *data;
2381 unsigned long flags;
2382 union IO_APIC_reg_00 reg_00;
2383 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 data = container_of(dev, struct sysfs_ioapic_data, dev);
2386 entry = data->entry;
2387
2388 spin_lock_irqsave(&ioapic_lock, flags);
2389 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002390 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2391 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 io_apic_write(dev->id, 0, reg_00.raw);
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002395 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002396 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 return 0;
2399}
2400
2401static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002402 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 .suspend = ioapic_suspend,
2404 .resume = ioapic_resume,
2405};
2406
2407static int __init ioapic_init_sysfs(void)
2408{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002409 struct sys_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 int i, size, error = 0;
2411
2412 error = sysdev_class_register(&ioapic_sysdev_class);
2413 if (error)
2414 return error;
2415
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002416 for (i = 0; i < nr_ioapics; i++) {
2417 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002419 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (!mp_ioapic_data[i]) {
2421 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2422 continue;
2423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002425 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 dev->cls = &ioapic_sysdev_class;
2427 error = sysdev_register(dev);
2428 if (error) {
2429 kfree(mp_ioapic_data[i]);
2430 mp_ioapic_data[i] = NULL;
2431 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2432 continue;
2433 }
2434 }
2435
2436 return 0;
2437}
2438
2439device_initcall(ioapic_init_sysfs);
2440
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002441/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002442 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002443 */
2444int create_irq(void)
2445{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002446 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002447 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002448 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002449
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002450 irq = -ENOSPC;
2451 spin_lock_irqsave(&vector_lock, flags);
2452 for (new = (NR_IRQS - 1); new >= 0; new--) {
2453 if (platform_legacy_irq(new))
2454 continue;
2455 if (irq_vector[new] != 0)
2456 continue;
2457 vector = __assign_irq_vector(new);
2458 if (likely(vector > 0))
2459 irq = new;
2460 break;
2461 }
2462 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002463
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002464 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002465 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002466 dynamic_irq_init(irq);
2467 }
2468 return irq;
2469}
2470
2471void destroy_irq(unsigned int irq)
2472{
2473 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002474
2475 dynamic_irq_cleanup(irq);
2476
2477 spin_lock_irqsave(&vector_lock, flags);
PJ Waskiewicz9d9ad4b2008-04-25 17:58:52 -07002478 clear_bit(irq_vector[irq], used_vectors);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002479 irq_vector[irq] = 0;
2480 spin_unlock_irqrestore(&vector_lock, flags);
2481}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002482
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002483/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002484 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002485 */
2486#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002487static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002488{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002489 int vector;
2490 unsigned dest;
2491
2492 vector = assign_irq_vector(irq);
2493 if (vector >= 0) {
2494 dest = cpu_mask_to_apicid(TARGET_CPUS);
2495
2496 msg->address_hi = MSI_ADDR_BASE_HI;
2497 msg->address_lo =
2498 MSI_ADDR_BASE_LO |
2499 ((INT_DEST_MODE == 0) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002500MSI_ADDR_DEST_MODE_PHYSICAL:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002501 MSI_ADDR_DEST_MODE_LOGICAL) |
2502 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2503 MSI_ADDR_REDIRECTION_CPU:
2504 MSI_ADDR_REDIRECTION_LOWPRI) |
2505 MSI_ADDR_DEST_ID(dest);
2506
2507 msg->data =
2508 MSI_DATA_TRIGGER_EDGE |
2509 MSI_DATA_LEVEL_ASSERT |
2510 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002511MSI_DATA_DELIVERY_FIXED:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002512 MSI_DATA_DELIVERY_LOWPRI) |
2513 MSI_DATA_VECTOR(vector);
2514 }
2515 return vector;
2516}
2517
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002518#ifdef CONFIG_SMP
2519static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2520{
2521 struct msi_msg msg;
2522 unsigned int dest;
2523 cpumask_t tmp;
2524 int vector;
2525
2526 cpus_and(tmp, mask, cpu_online_map);
2527 if (cpus_empty(tmp))
2528 tmp = TARGET_CPUS;
2529
2530 vector = assign_irq_vector(irq);
2531 if (vector < 0)
2532 return;
2533
2534 dest = cpu_mask_to_apicid(mask);
2535
2536 read_msi_msg(irq, &msg);
2537
2538 msg.data &= ~MSI_DATA_VECTOR_MASK;
2539 msg.data |= MSI_DATA_VECTOR(vector);
2540 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2541 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2542
2543 write_msi_msg(irq, &msg);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002544 irq_desc[irq].affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002545}
2546#endif /* CONFIG_SMP */
2547
2548/*
2549 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2550 * which implement the MSI or MSI-X Capability Structure.
2551 */
2552static struct irq_chip msi_chip = {
2553 .name = "PCI-MSI",
2554 .unmask = unmask_msi_irq,
2555 .mask = mask_msi_irq,
2556 .ack = ack_ioapic_irq,
2557#ifdef CONFIG_SMP
2558 .set_affinity = set_msi_irq_affinity,
2559#endif
2560 .retrigger = ioapic_retrigger_irq,
2561};
2562
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002563int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002564{
2565 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002566 int irq, ret;
2567 irq = create_irq();
2568 if (irq < 0)
2569 return irq;
2570
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002571 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002572 if (ret < 0) {
2573 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002574 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002575 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002576
Michael Ellerman7fe37302007-04-18 19:39:21 +10002577 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002578 write_msi_msg(irq, &msg);
2579
Ingo Molnara460e742006-10-17 00:10:03 -07002580 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2581 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002582
Michael Ellerman7fe37302007-04-18 19:39:21 +10002583 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002584}
2585
2586void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002587{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002588 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002589}
2590
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002591#endif /* CONFIG_PCI_MSI */
2592
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002593/*
2594 * Hypertransport interrupt support
2595 */
2596#ifdef CONFIG_HT_IRQ
2597
2598#ifdef CONFIG_SMP
2599
2600static void target_ht_irq(unsigned int irq, unsigned int dest)
2601{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002602 struct ht_irq_msg msg;
2603 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002604
Eric W. Biedermanec683072006-11-08 17:44:57 -08002605 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2606 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002607
Eric W. Biedermanec683072006-11-08 17:44:57 -08002608 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2609 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002610
Eric W. Biedermanec683072006-11-08 17:44:57 -08002611 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002612}
2613
2614static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2615{
2616 unsigned int dest;
2617 cpumask_t tmp;
2618
2619 cpus_and(tmp, mask, cpu_online_map);
2620 if (cpus_empty(tmp))
2621 tmp = TARGET_CPUS;
2622
2623 cpus_and(mask, tmp, CPU_MASK_ALL);
2624
2625 dest = cpu_mask_to_apicid(mask);
2626
2627 target_ht_irq(irq, dest);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002628 irq_desc[irq].affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002629}
2630#endif
2631
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002632static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002633 .name = "PCI-HT",
2634 .mask = mask_ht_irq,
2635 .unmask = unmask_ht_irq,
2636 .ack = ack_ioapic_irq,
2637#ifdef CONFIG_SMP
2638 .set_affinity = set_ht_irq_affinity,
2639#endif
2640 .retrigger = ioapic_retrigger_irq,
2641};
2642
2643int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2644{
2645 int vector;
2646
2647 vector = assign_irq_vector(irq);
2648 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002649 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002650 unsigned dest;
2651 cpumask_t tmp;
2652
2653 cpus_clear(tmp);
2654 cpu_set(vector >> 8, tmp);
2655 dest = cpu_mask_to_apicid(tmp);
2656
Eric W. Biedermanec683072006-11-08 17:44:57 -08002657 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002658
Eric W. Biedermanec683072006-11-08 17:44:57 -08002659 msg.address_lo =
2660 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002661 HT_IRQ_LOW_DEST_ID(dest) |
2662 HT_IRQ_LOW_VECTOR(vector) |
2663 ((INT_DEST_MODE == 0) ?
2664 HT_IRQ_LOW_DM_PHYSICAL :
2665 HT_IRQ_LOW_DM_LOGICAL) |
2666 HT_IRQ_LOW_RQEOI_EDGE |
2667 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2668 HT_IRQ_LOW_MT_FIXED :
2669 HT_IRQ_LOW_MT_ARBITRATED) |
2670 HT_IRQ_LOW_IRQ_MASKED;
2671
Eric W. Biedermanec683072006-11-08 17:44:57 -08002672 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002673
Ingo Molnara460e742006-10-17 00:10:03 -07002674 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2675 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002676 }
2677 return vector;
2678}
2679#endif /* CONFIG_HT_IRQ */
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/* --------------------------------------------------------------------------
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002682 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 -------------------------------------------------------------------------- */
2684
Len Brown888ba6c2005-08-24 12:07:20 -04002685#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002687int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 union IO_APIC_reg_00 reg_00;
2690 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2691 physid_mask_t tmp;
2692 unsigned long flags;
2693 int i = 0;
2694
2695 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002696 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2697 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002699 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2701 * advantage of new APIC bus architecture.
2702 */
2703
2704 if (physids_empty(apic_id_map))
2705 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2706
2707 spin_lock_irqsave(&ioapic_lock, flags);
2708 reg_00.raw = io_apic_read(ioapic, 0);
2709 spin_unlock_irqrestore(&ioapic_lock, flags);
2710
2711 if (apic_id >= get_physical_broadcast()) {
2712 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2713 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2714 apic_id = reg_00.bits.ID;
2715 }
2716
2717 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002718 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 * 'stuck on smp_invalidate_needed IPI wait' messages.
2720 */
2721 if (check_apicid_used(apic_id_map, apic_id)) {
2722
2723 for (i = 0; i < get_physical_broadcast(); i++) {
2724 if (!check_apicid_used(apic_id_map, i))
2725 break;
2726 }
2727
2728 if (i == get_physical_broadcast())
2729 panic("Max apic_id exceeded!\n");
2730
2731 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2732 "trying %d\n", ioapic, apic_id, i);
2733
2734 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 tmp = apicid_to_cpu_present(apic_id);
2738 physids_or(apic_id_map, apic_id_map, tmp);
2739
2740 if (reg_00.bits.ID != apic_id) {
2741 reg_00.bits.ID = apic_id;
2742
2743 spin_lock_irqsave(&ioapic_lock, flags);
2744 io_apic_write(ioapic, 0, reg_00.raw);
2745 reg_00.raw = io_apic_read(ioapic, 0);
2746 spin_unlock_irqrestore(&ioapic_lock, flags);
2747
2748 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002749 if (reg_00.bits.ID != apic_id) {
2750 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2751 return -1;
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
2754
2755 apic_printk(APIC_VERBOSE, KERN_INFO
2756 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2757
2758 return apic_id;
2759}
2760
2761
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002762int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 union IO_APIC_reg_01 reg_01;
2765 unsigned long flags;
2766
2767 spin_lock_irqsave(&ioapic_lock, flags);
2768 reg_01.raw = io_apic_read(ioapic, 1);
2769 spin_unlock_irqrestore(&ioapic_lock, flags);
2770
2771 return reg_01.bits.version;
2772}
2773
2774
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002775int __init io_apic_get_redir_entries(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 union IO_APIC_reg_01 reg_01;
2778 unsigned long flags;
2779
2780 spin_lock_irqsave(&ioapic_lock, flags);
2781 reg_01.raw = io_apic_read(ioapic, 1);
2782 spin_unlock_irqrestore(&ioapic_lock, flags);
2783
2784 return reg_01.bits.entries;
2785}
2786
2787
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002788int 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 -07002789{
2790 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 if (!IO_APIC_IRQ(irq)) {
2793 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2794 ioapic);
2795 return -EINVAL;
2796 }
2797
2798 /*
2799 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2800 * Note that we mask (disable) IRQs now -- these get enabled when the
2801 * corresponding device driver registers for this IRQ.
2802 */
2803
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002804 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 entry.delivery_mode = INT_DELIVERY_MODE;
2807 entry.dest_mode = INT_DEST_MODE;
2808 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2809 entry.trigger = edge_level;
2810 entry.polarity = active_high_low;
2811 entry.mask = 1;
2812
2813 /*
2814 * IRQs < 16 are already in the irq_2_pin[] map
2815 */
2816 if (irq >= 16)
2817 add_pin_to_irq(irq, ioapic, pin);
2818
2819 entry.vector = assign_irq_vector(irq);
2820
2821 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2822 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002823 mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 edge_level, active_high_low);
2825
2826 ioapic_register_intr(irq, entry.vector, edge_level);
2827
2828 if (!ioapic && (irq < 16))
2829 disable_8259A_irq(irq);
2830
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002831 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 return 0;
2834}
2835
Shaohua Li61fd47e2007-11-17 01:05:28 -05002836int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2837{
2838 int i;
2839
2840 if (skip_ioapic_setup)
2841 return -1;
2842
2843 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002844 if (mp_irqs[i].mp_irqtype == mp_INT &&
2845 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002846 break;
2847 if (i >= mp_irq_entries)
2848 return -1;
2849
2850 *trigger = irq_trigger(i);
2851 *polarity = irq_polarity(i);
2852 return 0;
2853}
2854
Len Brown888ba6c2005-08-24 12:07:20 -04002855#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002856
2857static int __init parse_disable_timer_pin_1(char *arg)
2858{
2859 disable_timer_pin_1 = 1;
2860 return 0;
2861}
2862early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2863
2864static int __init parse_enable_timer_pin_1(char *arg)
2865{
2866 disable_timer_pin_1 = -1;
2867 return 0;
2868}
2869early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2870
2871static int __init parse_noapic(char *arg)
2872{
2873 /* disable IO-APIC */
2874 disable_ioapic_setup();
2875 return 0;
2876}
2877early_param("noapic", parse_noapic);
Yinghai Luf3294a32008-06-27 01:41:56 -07002878
2879void __init ioapic_init_mappings(void)
2880{
2881 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2882 int i;
2883
2884 for (i = 0; i < nr_ioapics; i++) {
2885 if (smp_found_config) {
2886 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2887 if (!ioapic_phys) {
2888 printk(KERN_ERR
2889 "WARNING: bogus zero IO-APIC "
2890 "address found in MPTABLE, "
2891 "disabling IO/APIC support!\n");
2892 smp_found_config = 0;
2893 skip_ioapic_setup = 1;
2894 goto fake_ioapic_page;
2895 }
2896 } else {
2897fake_ioapic_page:
2898 ioapic_phys = (unsigned long)
2899 alloc_bootmem_pages(PAGE_SIZE);
2900 ioapic_phys = __pa(ioapic_phys);
2901 }
2902 set_fixmap_nocache(idx, ioapic_phys);
2903 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
2904 __fix_to_virt(idx), ioapic_phys);
2905 idx++;
2906 }
2907}
2908