blob: 39e063a9a28adee09a11d93fdbfea858d2b48de9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020051#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053int (*ioapic_renumber_irq)(int ioapic, int irq);
54atomic_t irq_mis_count;
55
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080056/* Where if anywhere is the i8259 connect in external int mode */
57static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static DEFINE_SPINLOCK(ioapic_lock);
Jan Beulich0a1ad602006-06-26 13:56:43 +020060static DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010062int timer_through_8259 __initdata;
Andi Kleenf9262c12006-03-08 17:57:25 -080063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Is the SiS APIC rmw bug present ?
66 * -1 = don't know, 0 = no, 1 = yes
67 */
68int sis_apic_bug = -1;
69
70/*
71 * # of IRQ routing registers
72 */
73int nr_ioapic_registers[MAX_IO_APICS];
74
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040075/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040076struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040077int nr_ioapics;
78
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040079/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040080struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040081
82/* # of MP IRQ source entries */
83int mp_irq_entries;
84
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040085#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
86int mp_bus_id_to_type[MAX_MP_BUSSES];
87#endif
88
89DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
90
Rusty Russell1a3f2392006-09-26 10:52:32 +020091static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * Rough estimation of how many shared IRQs there are, can
95 * be changed anytime.
96 */
97#define MAX_PLUS_SHARED_IRQS NR_IRQS
98#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
99
100/*
101 * This is performance-critical, we want to do it O(1)
102 *
103 * the indexing order of this array favors 1:1 mappings
104 * between pins and IRQs.
105 */
106
107static struct irq_pin_list {
108 int apic, pin, next;
109} irq_2_pin[PIN_MAP_SIZE];
110
Linus Torvalds130fe052006-11-01 09:11:00 -0800111struct io_apic {
112 unsigned int index;
113 unsigned int unused[3];
114 unsigned int data;
115};
116
117static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
118{
119 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400120 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800121}
122
123static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
124{
125 struct io_apic __iomem *io_apic = io_apic_base(apic);
126 writel(reg, &io_apic->index);
127 return readl(&io_apic->data);
128}
129
130static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
131{
132 struct io_apic __iomem *io_apic = io_apic_base(apic);
133 writel(reg, &io_apic->index);
134 writel(value, &io_apic->data);
135}
136
137/*
138 * Re-write a value: to be used for read-modify-write
139 * cycles where the read already set up the index register.
140 *
141 * Older SiS APIC requires we rewrite the index register
142 */
143static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
144{
Al Virocb468982007-02-09 16:39:25 +0000145 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800146 if (sis_apic_bug)
147 writel(reg, &io_apic->index);
148 writel(value, &io_apic->data);
149}
150
Andi Kleencf4c6a22006-09-26 10:52:30 +0200151union entry_union {
152 struct { u32 w1, w2; };
153 struct IO_APIC_route_entry entry;
154};
155
156static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
157{
158 union entry_union eu;
159 unsigned long flags;
160 spin_lock_irqsave(&ioapic_lock, flags);
161 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
162 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
163 spin_unlock_irqrestore(&ioapic_lock, flags);
164 return eu.entry;
165}
166
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800167/*
168 * When we write a new IO APIC routing entry, we need to write the high
169 * word first! If the mask bit in the low word is clear, we will enable
170 * the interrupt, and we need to make sure the entry is fully populated
171 * before that happens.
172 */
Andi Kleend15512f2006-12-07 02:14:07 +0100173static void
174__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
175{
176 union entry_union eu;
177 eu.entry = e;
178 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
179 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
180}
181
Andi Kleencf4c6a22006-09-26 10:52:30 +0200182static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
183{
184 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200185 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100186 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800187 spin_unlock_irqrestore(&ioapic_lock, flags);
188}
189
190/*
191 * When we mask an IO APIC routing entry, we need to write the low
192 * word first, in order to set the mask bit before we change the
193 * high bits!
194 */
195static void ioapic_mask_entry(int apic, int pin)
196{
197 unsigned long flags;
198 union entry_union eu = { .entry.mask = 1 };
199
200 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200201 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
202 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
203 spin_unlock_irqrestore(&ioapic_lock, flags);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
208 * shared ISA-space IRQs, so we have to support them. We are super
209 * fast in the common case, and fast for shared ISA-space IRQs.
210 */
211static void add_pin_to_irq(unsigned int irq, int apic, int pin)
212{
213 static int first_free_entry = NR_IRQS;
214 struct irq_pin_list *entry = irq_2_pin + irq;
215
216 while (entry->next)
217 entry = irq_2_pin + entry->next;
218
219 if (entry->pin != -1) {
220 entry->next = first_free_entry;
221 entry = irq_2_pin + entry->next;
222 if (++first_free_entry >= PIN_MAP_SIZE)
223 panic("io_apic.c: whoops");
224 }
225 entry->apic = apic;
226 entry->pin = pin;
227}
228
229/*
230 * Reroute an IRQ to a different pin.
231 */
232static void __init replace_pin_at_irq(unsigned int irq,
233 int oldapic, int oldpin,
234 int newapic, int newpin)
235{
236 struct irq_pin_list *entry = irq_2_pin + irq;
237
238 while (1) {
239 if (entry->apic == oldapic && entry->pin == oldpin) {
240 entry->apic = newapic;
241 entry->pin = newpin;
242 }
243 if (!entry->next)
244 break;
245 entry = irq_2_pin + entry->next;
246 }
247}
248
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200249static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct irq_pin_list *entry = irq_2_pin + irq;
252 unsigned int pin, reg;
253
254 for (;;) {
255 pin = entry->pin;
256 if (pin == -1)
257 break;
258 reg = io_apic_read(entry->apic, 0x10 + pin*2);
259 reg &= ~disable;
260 reg |= enable;
261 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
262 if (!entry->next)
263 break;
264 entry = irq_2_pin + entry->next;
265 }
266}
267
268/* mask = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200269static void __mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400271 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/* mask = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200275static void __unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400277 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/* mask = 1, trigger = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200281static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400283 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
284 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/* mask = 0, trigger = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200288static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400290 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
291 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200294static void mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 unsigned long flags;
297
298 spin_lock_irqsave(&ioapic_lock, flags);
299 __mask_IO_APIC_irq(irq);
300 spin_unlock_irqrestore(&ioapic_lock, flags);
301}
302
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200303static void unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 unsigned long flags;
306
307 spin_lock_irqsave(&ioapic_lock, flags);
308 __unmask_IO_APIC_irq(irq);
309 spin_unlock_irqrestore(&ioapic_lock, flags);
310}
311
312static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
313{
314 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200317 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (entry.delivery_mode == dest_SMI)
319 return;
320
321 /*
322 * Disable it in the IO-APIC irq-routing table:
323 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800324 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200327static void clear_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int apic, pin;
330
331 for (apic = 0; apic < nr_ioapics; apic++)
332 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
333 clear_IO_APIC_pin(apic, pin);
334}
335
Ashok Raj54d5d422005-09-06 15:16:15 -0700336#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
338{
339 unsigned long flags;
340 int pin;
341 struct irq_pin_list *entry = irq_2_pin + irq;
342 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700343 cpumask_t tmp;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200344
Ashok Raj54d5d422005-09-06 15:16:15 -0700345 cpus_and(tmp, cpumask, cpu_online_map);
346 if (cpus_empty(tmp))
347 tmp = TARGET_CPUS;
348
349 cpus_and(cpumask, tmp, CPU_MASK_ALL);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 apicid_value = cpu_mask_to_apicid(cpumask);
352 /* Prepare to do the io_apic_write */
353 apicid_value = apicid_value << 24;
354 spin_lock_irqsave(&ioapic_lock, flags);
355 for (;;) {
356 pin = entry->pin;
357 if (pin == -1)
358 break;
359 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
360 if (!entry->next)
361 break;
362 entry = irq_2_pin + entry->next;
363 }
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700364 irq_desc[irq].affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 spin_unlock_irqrestore(&ioapic_lock, flags);
366}
367
368#if defined(CONFIG_IRQBALANCE)
369# include <asm/processor.h> /* kernel_thread() */
370# include <linux/kernel_stat.h> /* kstat */
371# include <linux/slab.h> /* kmalloc() */
Julia Lawall1d16b532008-01-30 13:32:19 +0100372# include <linux/timer.h>
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define IRQBALANCE_CHECK_ARCH -999
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700375#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
376#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
377#define BALANCED_IRQ_MORE_DELTA (HZ/10)
378#define BALANCED_IRQ_LESS_DELTA (HZ)
379
380static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
381static int physical_balance __read_mostly;
382static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384static struct irq_cpu_info {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200385 unsigned long *last_irq;
386 unsigned long *irq_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned long irq;
388} irq_cpu_data[NR_CPUS];
389
390#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200391#define LAST_CPU_IRQ(cpu, irq) (irq_cpu_data[cpu].last_irq[irq])
392#define IRQ_DELTA(cpu, irq) (irq_cpu_data[cpu].irq_delta[irq])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394#define IDLE_ENOUGH(cpu,now) \
395 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
396
397#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
398
Mike Travisd5a74302007-10-16 01:24:05 -0700399#define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700401static cpumask_t balance_irq_affinity[NR_IRQS] = {
402 [0 ... NR_IRQS-1] = CPU_MASK_ALL
403};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700405void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
406{
407 balance_irq_affinity[irq] = mask;
408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
411 unsigned long now, int direction)
412{
413 int search_idle = 1;
414 int cpu = curr_cpu;
415
416 goto inside;
417
418 do {
419 if (unlikely(cpu == curr_cpu))
420 search_idle = 0;
421inside:
422 if (direction == 1) {
423 cpu++;
424 if (cpu >= NR_CPUS)
425 cpu = 0;
426 } else {
427 cpu--;
428 if (cpu == -1)
429 cpu = NR_CPUS-1;
430 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200431 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu, allowed_mask) ||
432 (search_idle && !IDLE_ENOUGH(cpu, now)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return cpu;
435}
436
437static inline void balance_irq(int cpu, int irq)
438{
439 unsigned long now = jiffies;
440 cpumask_t allowed_mask;
441 unsigned int new_cpu;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (irqbalance_disabled)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700446 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 new_cpu = move(cpu, allowed_mask, now, 1);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200448 if (cpu != new_cpu)
Ashok Raj54d5d422005-09-06 15:16:15 -0700449 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
453{
454 int i, j;
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200455
Andrew Morton394e3902006-03-23 03:01:05 -0800456 for_each_online_cpu(i) {
457 for (j = 0; j < NR_IRQS; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!irq_desc[j].action)
459 continue;
460 /* Is it a significant load ? */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200461 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i), j) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 useful_load_threshold)
463 continue;
464 balance_irq(i, j);
465 }
466 }
467 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200468 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return;
470}
471
472static void do_irq_balance(void)
473{
474 int i, j;
475 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
476 unsigned long move_this_load = 0;
477 int max_loaded = 0, min_loaded = 0;
478 int load;
479 unsigned long useful_load_threshold = balanced_irq_interval + 10;
480 int selected_irq;
481 int tmp_loaded, first_attempt = 1;
482 unsigned long tmp_cpu_irq;
483 unsigned long imbalance = 0;
484 cpumask_t allowed_mask, target_cpu_mask, tmp;
485
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800486 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int package_index;
488 CPU_IRQ(i) = 0;
489 if (!cpu_online(i))
490 continue;
491 package_index = CPU_TO_PACKAGEINDEX(i);
492 for (j = 0; j < NR_IRQS; j++) {
493 unsigned long value_now, delta;
Thomas Gleixner950f4422007-02-16 01:27:24 -0800494 /* Is this an active IRQ or balancing disabled ? */
495 if (!irq_desc[j].action || irq_balancing_disabled(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200497 if (package_index == i)
498 IRQ_DELTA(package_index, j) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Determine the total count per processor per IRQ */
500 value_now = (unsigned long) kstat_cpu(i).irqs[j];
501
502 /* Determine the activity per processor per IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200503 delta = value_now - LAST_CPU_IRQ(i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* Update last_cpu_irq[][] for the next time */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200506 LAST_CPU_IRQ(i, j) = value_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* Ignore IRQs whose rate is less than the clock */
509 if (delta < useful_load_threshold)
510 continue;
511 /* update the load for the processor or package total */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200512 IRQ_DELTA(package_index, j) += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /* Keep track of the higher numbered sibling as well */
515 if (i != package_index)
516 CPU_IRQ(i) += delta;
517 /*
518 * We have sibling A and sibling B in the package
519 *
520 * cpu_irq[A] = load for cpu A + load for cpu B
521 * cpu_irq[B] = load for cpu B
522 */
523 CPU_IRQ(package_index) += delta;
524 }
525 }
526 /* Find the least loaded processor package */
Andrew Morton394e3902006-03-23 03:01:05 -0800527 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (i != CPU_TO_PACKAGEINDEX(i))
529 continue;
530 if (min_cpu_irq > CPU_IRQ(i)) {
531 min_cpu_irq = CPU_IRQ(i);
532 min_loaded = i;
533 }
534 }
535 max_cpu_irq = ULONG_MAX;
536
537tryanothercpu:
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200538 /*
539 * Look for heaviest loaded processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * We may come back to get the next heaviest loaded processor.
541 * Skip processors with trivial loads.
542 */
543 tmp_cpu_irq = 0;
544 tmp_loaded = -1;
Andrew Morton394e3902006-03-23 03:01:05 -0800545 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (i != CPU_TO_PACKAGEINDEX(i))
547 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200548 if (max_cpu_irq <= CPU_IRQ(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 continue;
550 if (tmp_cpu_irq < CPU_IRQ(i)) {
551 tmp_cpu_irq = CPU_IRQ(i);
552 tmp_loaded = i;
553 }
554 }
555
556 if (tmp_loaded == -1) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200557 /*
558 * In the case of small number of heavy interrupt sources,
559 * loading some of the cpus too much. We use Ingo's original
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * approach to rotate them around.
561 */
562 if (!first_attempt && imbalance >= useful_load_threshold) {
563 rotate_irqs_among_cpus(useful_load_threshold);
564 return;
565 }
566 goto not_worth_the_effort;
567 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 first_attempt = 0; /* heaviest search */
570 max_cpu_irq = tmp_cpu_irq; /* load */
571 max_loaded = tmp_loaded; /* processor */
572 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200573
574 /*
575 * if imbalance is less than approx 10% of max load, then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * observe diminishing returns action. - quit
577 */
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200578 if (imbalance < (max_cpu_irq >> 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto not_worth_the_effort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581tryanotherirq:
582 /* if we select an IRQ to move that can't go where we want, then
583 * see if there is another one to try.
584 */
585 move_this_load = 0;
586 selected_irq = -1;
587 for (j = 0; j < NR_IRQS; j++) {
588 /* Is this an active IRQ? */
589 if (!irq_desc[j].action)
590 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200591 if (imbalance <= IRQ_DELTA(max_loaded, j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 continue;
593 /* Try to find the IRQ that is closest to the imbalance
594 * without going over.
595 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200596 if (move_this_load < IRQ_DELTA(max_loaded, j)) {
597 move_this_load = IRQ_DELTA(max_loaded, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 selected_irq = j;
599 }
600 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200601 if (selected_irq == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto tryanothercpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 imbalance = move_this_load;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200605
Simon Arlott27b46d72007-10-20 01:13:56 +0200606 /* For physical_balance case, we accumulated both load
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * values in the one of the siblings cpu_irq[],
608 * to use the same code for physical and logical processors
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200609 * as much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
611 * NOTE: the cpu_irq[] array holds the sum of the load for
612 * sibling A and sibling B in the slot for the lowest numbered
613 * sibling (A), _AND_ the load for sibling B in the slot for
614 * the higher numbered sibling.
615 *
616 * We seek the least loaded sibling by making the comparison
617 * (A+B)/2 vs B
618 */
619 load = CPU_IRQ(min_loaded) >> 1;
Mike Travisd5a74302007-10-16 01:24:05 -0700620 for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (load > CPU_IRQ(j)) {
622 /* This won't change cpu_sibling_map[min_loaded] */
623 load = CPU_IRQ(j);
624 min_loaded = j;
625 }
626 }
627
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700628 cpus_and(allowed_mask,
629 cpu_online_map,
630 balance_irq_affinity[selected_irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 target_cpu_mask = cpumask_of_cpu(min_loaded);
632 cpus_and(tmp, target_cpu_mask, allowed_mask);
633
634 if (!cpus_empty(tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* mark for change destination */
Ashok Raj54d5d422005-09-06 15:16:15 -0700636 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
637
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200638 /* Since we made a change, come back sooner to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * check for more variation.
640 */
641 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200642 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return;
644 }
645 goto tryanotherirq;
646
647not_worth_the_effort:
648 /*
649 * if we did not find an IRQ to move, then adjust the time interval
650 * upward
651 */
652 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200653 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return;
655}
656
657static int balanced_irq(void *unused)
658{
659 int i;
660 unsigned long prev_balance_time = jiffies;
661 long time_remaining = balanced_irq_interval;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* push everything to CPU 0 to give us a starting point. */
664 for (i = 0 ; i < NR_IRQS ; i++) {
Ingo Molnarcd916d32006-06-29 02:24:42 -0700665 irq_desc[i].pending_mask = cpumask_of_cpu(0);
Ashok Raj54d5d422005-09-06 15:16:15 -0700666 set_pending_irq(i, cpumask_of_cpu(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700669 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 for ( ; ; ) {
Nishanth Aravamudan52e6e632005-09-10 00:27:26 -0700671 time_remaining = schedule_timeout_interruptible(time_remaining);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700672 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (time_after(jiffies,
674 prev_balance_time+balanced_irq_interval)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700675 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 do_irq_balance();
677 prev_balance_time = jiffies;
678 time_remaining = balanced_irq_interval;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700679 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 }
682 return 0;
683}
684
685static int __init balanced_irq_init(void)
686{
687 int i;
688 struct cpuinfo_x86 *c;
689 cpumask_t tmp;
690
691 cpus_shift_right(tmp, cpu_online_map, 2);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200692 c = &boot_cpu_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* When not overwritten by the command line ask subarchitecture. */
694 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
695 irqbalance_disabled = NO_BALANCE_IRQ;
696 if (irqbalance_disabled)
697 return 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* disable irqbalance completely if there is only one processor online */
700 if (num_online_cpus() < 2) {
701 irqbalance_disabled = 1;
702 return 0;
703 }
704 /*
705 * Enable physical balance only if more than 1 physical processor
706 * is present
707 */
708 if (smp_num_siblings > 1 && !cpus_empty(tmp))
709 physical_balance = 1;
710
Andrew Morton394e3902006-03-23 03:01:05 -0800711 for_each_online_cpu(i) {
Christophe Jaillet25556c12008-06-22 22:13:48 +0200712 irq_cpu_data[i].irq_delta = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
713 irq_cpu_data[i].last_irq = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
715 printk(KERN_ERR "balanced_irq_init: out of memory");
716 goto failed;
717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 printk(KERN_INFO "Starting balanced_irq\n");
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200721 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200723 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724failed:
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800725 for_each_possible_cpu(i) {
Jesper Juhl4ae66732005-06-25 14:58:48 -0700726 kfree(irq_cpu_data[i].irq_delta);
Andrew Morton394e3902006-03-23 03:01:05 -0800727 irq_cpu_data[i].irq_delta = NULL;
Jesper Juhl4ae66732005-06-25 14:58:48 -0700728 kfree(irq_cpu_data[i].last_irq);
Andrew Morton394e3902006-03-23 03:01:05 -0800729 irq_cpu_data[i].last_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 return 0;
732}
733
Andrew Mortonc2481cc2007-04-08 16:04:04 -0700734int __devinit irqbalance_disable(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 irqbalance_disabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800737 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740__setup("noirqbalance", irqbalance_disable);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742late_initcall(balanced_irq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743#endif /* CONFIG_IRQBALANCE */
Ashok Raj54d5d422005-09-06 15:16:15 -0700744#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100747void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 unsigned int cfg;
750
751 /*
752 * Wait for idle.
753 */
754 apic_wait_icr_idle();
755 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
756 /*
757 * Send the IPI. The write to APIC_ICR fires this off.
758 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100759 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761#endif /* !CONFIG_SMP */
762
763
764/*
765 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
766 * specific CPU-side IRQs.
767 */
768
769#define MAX_PIRQS 8
770static int pirq_entries [MAX_PIRQS];
771static int pirqs_enabled;
772int skip_ioapic_setup;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static int __init ioapic_pirq_setup(char *str)
775{
776 int i, max;
777 int ints[MAX_PIRQS+1];
778
779 get_options(str, ARRAY_SIZE(ints), ints);
780
781 for (i = 0; i < MAX_PIRQS; i++)
782 pirq_entries[i] = -1;
783
784 pirqs_enabled = 1;
785 apic_printk(APIC_VERBOSE, KERN_INFO
786 "PIRQ redirection, working around broken MP-BIOS.\n");
787 max = MAX_PIRQS;
788 if (ints[0] < MAX_PIRQS)
789 max = ints[0];
790
791 for (i = 0; i < max; i++) {
792 apic_printk(APIC_VERBOSE, KERN_DEBUG
793 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
794 /*
795 * PIRQs are mapped upside down, usually.
796 */
797 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
798 }
799 return 1;
800}
801
802__setup("pirq=", ioapic_pirq_setup);
803
804/*
805 * Find the IRQ entry number of a certain pin.
806 */
807static int find_irq_entry(int apic, int pin, int type)
808{
809 int i;
810
811 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400812 if (mp_irqs[i].mp_irqtype == type &&
813 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
814 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
815 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return i;
817
818 return -1;
819}
820
821/*
822 * Find the pin to which IRQ[irq] (ISA) is connected
823 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800824static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 int i;
827
828 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400829 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300831 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400832 (mp_irqs[i].mp_irqtype == type) &&
833 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400835 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 return -1;
838}
839
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800840static int __init find_isa_irq_apic(int irq, int type)
841{
842 int i;
843
844 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400845 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800846
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300847 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400848 (mp_irqs[i].mp_irqtype == type) &&
849 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800850 break;
851 }
852 if (i < mp_irq_entries) {
853 int apic;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200854 for (apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400855 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800856 return apic;
857 }
858 }
859
860 return -1;
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * Find a specific PCI IRQ entry.
865 * Not an __init, possibly needed by modules
866 */
867static int pin_2_irq(int idx, int apic, int pin);
868
869int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
870{
871 int apic, i, best_guess = -1;
872
873 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
874 "slot:%d, pin:%d.\n", bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400875 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
877 return -1;
878 }
879 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400880 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400883 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
884 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
886
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300887 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400888 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400890 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar3de352b2008-07-08 11:14:58 +0200891 int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (!(apic || IO_APIC_IRQ(irq)))
894 continue;
895
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400896 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return irq;
898 /*
899 * Use the first all-but-pin matching entry as a
900 * best-guess fuzzy result for broken mptables.
901 */
902 if (best_guess < 0)
903 best_guess = irq;
904 }
905 }
906 return best_guess;
907}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700908EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910/*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200911 * This function currently is only a helper for the i386 smp boot process where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * we need to reprogram the ioredtbls to cater for the cpus which have come online
913 * so mask in all cases should simply be TARGET_CPUS
914 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700915#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916void __init setup_ioapic_dest(void)
917{
918 int pin, ioapic, irq, irq_entry;
919
920 if (skip_ioapic_setup == 1)
921 return;
922
923 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
924 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
925 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
926 if (irq_entry == -1)
927 continue;
928 irq = pin_2_irq(irq_entry, ioapic, pin);
929 set_ioapic_affinity_irq(irq, TARGET_CPUS);
930 }
931
932 }
933}
Ashok Raj54d5d422005-09-06 15:16:15 -0700934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300936#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
938 * EISA Edge/Level control register, ELCR
939 */
940static int EISA_ELCR(unsigned int irq)
941{
942 if (irq < 16) {
943 unsigned int port = 0x4d0 + (irq >> 3);
944 return (inb(port) >> (irq & 7)) & 1;
945 }
946 apic_printk(APIC_VERBOSE, KERN_INFO
947 "Broken MPtable reports ISA irq %d\n", irq);
948 return 0;
949}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300950#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300952/* ISA interrupts are always polarity zero edge triggered,
953 * when listed as conforming in the MP table. */
954
955#define default_ISA_trigger(idx) (0)
956#define default_ISA_polarity(idx) (0)
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/* EISA interrupts are always polarity zero and can be edge or level
959 * trigger depending on the ELCR value. If an interrupt is listed as
960 * EISA conforming in the MP table, that means its trigger type must
961 * be read in from the ELCR */
962
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400963#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300964#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966/* PCI interrupts are always polarity one level triggered,
967 * when listed as conforming in the MP table. */
968
969#define default_PCI_trigger(idx) (1)
970#define default_PCI_polarity(idx) (1)
971
972/* MCA interrupts are always polarity zero level triggered,
973 * when listed as conforming in the MP table. */
974
975#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300976#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Shaohua Li61fd47e2007-11-17 01:05:28 -0500978static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400980 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int polarity;
982
983 /*
984 * Determine IRQ line polarity (high active or low active):
985 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200986 switch (mp_irqs[idx].mp_irqflag & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200987 case 0: /* conforms, ie. bus-type dependent polarity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200989 polarity = test_bit(bus, mp_bus_not_pci)?
990 default_ISA_polarity(idx):
991 default_PCI_polarity(idx);
992 break;
993 }
994 case 1: /* high active */
995 {
996 polarity = 0;
997 break;
998 }
999 case 2: /* reserved */
1000 {
1001 printk(KERN_WARNING "broken BIOS!!\n");
1002 polarity = 1;
1003 break;
1004 }
1005 case 3: /* low active */
1006 {
1007 polarity = 1;
1008 break;
1009 }
1010 default: /* invalid */
1011 {
1012 printk(KERN_WARNING "broken BIOS!!\n");
1013 polarity = 1;
1014 break;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 return polarity;
1018}
1019
1020static int MPBIOS_trigger(int idx)
1021{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001022 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int trigger;
1024
1025 /*
1026 * Determine IRQ trigger mode (edge or level sensitive):
1027 */
Ingo Molnar3de352b2008-07-08 11:14:58 +02001028 switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001029 case 0: /* conforms, ie. bus-type dependent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001031 trigger = test_bit(bus, mp_bus_not_pci)?
1032 default_ISA_trigger(idx):
1033 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001034#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001035 switch (mp_bus_id_to_type[bus]) {
1036 case MP_BUS_ISA: /* ISA pin */
1037 {
1038 /* set before the switch */
1039 break;
1040 }
1041 case MP_BUS_EISA: /* EISA pin */
1042 {
1043 trigger = default_EISA_trigger(idx);
1044 break;
1045 }
1046 case MP_BUS_PCI: /* PCI pin */
1047 {
1048 /* set before the switch */
1049 break;
1050 }
1051 case MP_BUS_MCA: /* MCA pin */
1052 {
1053 trigger = default_MCA_trigger(idx);
1054 break;
1055 }
1056 default:
1057 {
1058 printk(KERN_WARNING "broken BIOS!!\n");
1059 trigger = 1;
1060 break;
1061 }
1062 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001063#endif
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001064 break;
1065 }
1066 case 1: /* edge */
1067 {
1068 trigger = 0;
1069 break;
1070 }
1071 case 2: /* reserved */
1072 {
1073 printk(KERN_WARNING "broken BIOS!!\n");
1074 trigger = 1;
1075 break;
1076 }
1077 case 3: /* level */
1078 {
1079 trigger = 1;
1080 break;
1081 }
1082 default: /* invalid */
1083 {
1084 printk(KERN_WARNING "broken BIOS!!\n");
1085 trigger = 0;
1086 break;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 return trigger;
1090}
1091
1092static inline int irq_polarity(int idx)
1093{
1094 return MPBIOS_polarity(idx);
1095}
1096
1097static inline int irq_trigger(int idx)
1098{
1099 return MPBIOS_trigger(idx);
1100}
1101
1102static int pin_2_irq(int idx, int apic, int pin)
1103{
1104 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001105 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /*
1108 * Debugging check, we are in big trouble if this message pops up!
1109 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001110 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1112
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001113 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001114 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001115 else {
1116 /*
1117 * PCI IRQs are mapped in order
1118 */
1119 i = irq = 0;
1120 while (i < apic)
1121 irq += nr_ioapic_registers[i++];
1122 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001124 /*
1125 * For MPS mode, so far only needed by ES7000 platform
1126 */
1127 if (ioapic_renumber_irq)
1128 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130
1131 /*
1132 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1133 */
1134 if ((pin >= 16) && (pin <= 23)) {
1135 if (pirq_entries[pin-16] != -1) {
1136 if (!pirq_entries[pin-16]) {
1137 apic_printk(APIC_VERBOSE, KERN_DEBUG
1138 "disabling PIRQ%d\n", pin-16);
1139 } else {
1140 irq = pirq_entries[pin-16];
1141 apic_printk(APIC_VERBOSE, KERN_DEBUG
1142 "using PIRQ%d -> IRQ %d\n",
1143 pin-16, irq);
1144 }
1145 }
1146 }
1147 return irq;
1148}
1149
1150static inline int IO_APIC_irq_trigger(int irq)
1151{
1152 int apic, idx, pin;
1153
1154 for (apic = 0; apic < nr_ioapics; apic++) {
1155 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001156 idx = find_irq_entry(apic, pin, mp_INT);
1157 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return irq_trigger(idx);
1159 }
1160 }
1161 /*
1162 * nonexistent IRQs are edge default
1163 */
1164 return 0;
1165}
1166
1167/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Adrian Bunk7e95b592006-12-07 02:14:11 +01001168static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001170static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001172 static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001173 int vector, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001175 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
Jan Beulich0a1ad602006-06-26 13:56:43 +02001176
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001177 if (irq_vector[irq] > 0)
1178 return irq_vector[irq];
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001179
Jan Beulich0a1ad602006-06-26 13:56:43 +02001180 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001181 offset = current_offset;
1182next:
1183 vector += 8;
Alan Mayer305b92a2008-04-15 15:36:56 -05001184 if (vector >= first_system_vector) {
Eric W. Biederman8339f002007-01-29 13:19:05 -07001185 offset = (offset + 1) % 8;
1186 vector = FIRST_DEVICE_VECTOR + offset;
1187 }
1188 if (vector == current_vector)
1189 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001190 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -07001191 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001192
1193 current_vector = vector;
1194 current_offset = offset;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001195 irq_vector[irq] = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +02001196
1197 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001200static int assign_irq_vector(int irq)
1201{
1202 unsigned long flags;
1203 int vector;
1204
1205 spin_lock_irqsave(&vector_lock, flags);
1206 vector = __assign_irq_vector(irq);
1207 spin_unlock_irqrestore(&vector_lock, flags);
1208
1209 return vector;
1210}
Glauber Costa3fde6902008-05-28 20:34:19 -07001211
1212void setup_vector_irq(int cpu)
1213{
1214}
1215
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001216static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218#define IOAPIC_AUTO -1
1219#define IOAPIC_EDGE 0
1220#define IOAPIC_LEVEL 1
1221
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001222static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jan Beulich6ebcc002006-06-26 13:56:46 +02001224 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001225 trigger == IOAPIC_LEVEL) {
1226 irq_desc[irq].status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001227 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1228 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001229 } else {
1230 irq_desc[irq].status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001231 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1232 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001233 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001234 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static void __init setup_IO_APIC_irqs(void)
1238{
1239 struct IO_APIC_route_entry entry;
1240 int apic, pin, idx, irq, first_notcon = 1, vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1243
1244 for (apic = 0; apic < nr_ioapics; apic++) {
1245 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1246
1247 /*
1248 * add it to the IO-APIC irq-routing table:
1249 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001250 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 entry.delivery_mode = INT_DELIVERY_MODE;
1253 entry.dest_mode = INT_DEST_MODE;
1254 entry.mask = 0; /* enable IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001255 entry.dest.logical.logical_dest =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 cpu_mask_to_apicid(TARGET_CPUS);
1257
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001258 idx = find_irq_entry(apic, pin, mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (idx == -1) {
1260 if (first_notcon) {
1261 apic_printk(APIC_VERBOSE, KERN_DEBUG
1262 " IO-APIC (apicid-pin) %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001263 mp_ioapics[apic].mp_apicid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 pin);
1265 first_notcon = 0;
1266 } else
1267 apic_printk(APIC_VERBOSE, ", %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001268 mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 continue;
1270 }
1271
Yinghai Lu20d225b2007-10-17 18:04:41 +02001272 if (!first_notcon) {
1273 apic_printk(APIC_VERBOSE, " not connected.\n");
1274 first_notcon = 1;
1275 }
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 entry.trigger = irq_trigger(idx);
1278 entry.polarity = irq_polarity(idx);
1279
1280 if (irq_trigger(idx)) {
1281 entry.trigger = 1;
1282 entry.mask = 1;
1283 }
1284
1285 irq = pin_2_irq(idx, apic, pin);
1286 /*
1287 * skip adding the timer int on secondary nodes, which causes
1288 * a small but painful rift in the time-space continuum
1289 */
1290 if (multi_timer_check(apic, irq))
1291 continue;
1292 else
1293 add_pin_to_irq(irq, apic, pin);
1294
1295 if (!apic && !IO_APIC_IRQ(irq))
1296 continue;
1297
1298 if (IO_APIC_IRQ(irq)) {
1299 vector = assign_irq_vector(irq);
1300 entry.vector = vector;
1301 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!apic && (irq < 16))
1304 disable_8259A_irq(irq);
1305 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09001306 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 }
1309
1310 if (!first_notcon)
1311 apic_printk(APIC_VERBOSE, " not connected.\n");
1312}
1313
1314/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001315 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001317static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1318 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001322 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /*
1325 * We use logical delivery to get the timer IRQ
1326 * to the first CPU.
1327 */
1328 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001329 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1331 entry.delivery_mode = INT_DELIVERY_MODE;
1332 entry.polarity = 0;
1333 entry.trigger = 0;
1334 entry.vector = vector;
1335
1336 /*
1337 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001338 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Maciej W. Rozyckif0825262008-05-27 21:19:16 +01001340 ioapic_register_intr(0, vector, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /*
1343 * Add it to the IO-APIC irq-routing table:
1344 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001345 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348void __init print_IO_APIC(void)
1349{
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
1463#if 0
1464
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001465static void print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 unsigned int v;
1468 int i, j;
1469
1470 if (apic_verbosity == APIC_QUIET)
1471 return;
1472
1473 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1474 for (i = 0; i < 8; i++) {
1475 v = apic_read(base + i*0x10);
1476 for (j = 0; j < 32; j++) {
1477 if (v & (1<<j))
1478 printk("1");
1479 else
1480 printk("0");
1481 }
1482 printk("\n");
1483 }
1484}
1485
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001486void /*__init*/ print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001489 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (apic_verbosity == APIC_QUIET)
1492 return;
1493
1494 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1495 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001496 v = apic_read(APIC_ID);
Jack Steiner05f2d122008-03-28 14:12:02 -05001497 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001498 GET_APIC_ID(v));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 v = apic_read(APIC_LVR);
1500 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1501 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001502 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 v = apic_read(APIC_TASKPRI);
1505 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1506
1507 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1508 v = apic_read(APIC_ARBPRI);
1509 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1510 v & APIC_ARBPRI_MASK);
1511 v = apic_read(APIC_PROCPRI);
1512 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1513 }
1514
1515 v = apic_read(APIC_EOI);
1516 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1517 v = apic_read(APIC_RRR);
1518 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1519 v = apic_read(APIC_LDR);
1520 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1521 v = apic_read(APIC_DFR);
1522 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1523 v = apic_read(APIC_SPIV);
1524 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1525
1526 printk(KERN_DEBUG "... APIC ISR field:\n");
1527 print_APIC_bitfield(APIC_ISR);
1528 printk(KERN_DEBUG "... APIC TMR field:\n");
1529 print_APIC_bitfield(APIC_TMR);
1530 printk(KERN_DEBUG "... APIC IRR field:\n");
1531 print_APIC_bitfield(APIC_IRR);
1532
1533 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1534 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1535 apic_write(APIC_ESR, 0);
1536 v = apic_read(APIC_ESR);
1537 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1538 }
1539
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001540 icr = apic_icr_read();
1541 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1542 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 v = apic_read(APIC_LVTT);
1545 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1546
1547 if (maxlvt > 3) { /* PC is LVT#4. */
1548 v = apic_read(APIC_LVTPC);
1549 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1550 }
1551 v = apic_read(APIC_LVT0);
1552 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1553 v = apic_read(APIC_LVT1);
1554 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1555
1556 if (maxlvt > 2) { /* ERR is LVT#3. */
1557 v = apic_read(APIC_LVTERR);
1558 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1559 }
1560
1561 v = apic_read(APIC_TMICT);
1562 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1563 v = apic_read(APIC_TMCCT);
1564 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1565 v = apic_read(APIC_TDCR);
1566 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1567 printk("\n");
1568}
1569
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001570void print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001572 on_each_cpu(print_local_APIC, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
1575void /*__init*/ print_PIC(void)
1576{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 unsigned int v;
1578 unsigned long flags;
1579
1580 if (apic_verbosity == APIC_QUIET)
1581 return;
1582
1583 printk(KERN_DEBUG "\nprinting PIC contents\n");
1584
1585 spin_lock_irqsave(&i8259A_lock, flags);
1586
1587 v = inb(0xa1) << 8 | inb(0x21);
1588 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1589
1590 v = inb(0xa0) << 8 | inb(0x20);
1591 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1592
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001593 outb(0x0b, 0xa0);
1594 outb(0x0b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 v = inb(0xa0) << 8 | inb(0x20);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001596 outb(0x0a, 0xa0);
1597 outb(0x0a, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 spin_unlock_irqrestore(&i8259A_lock, flags);
1600
1601 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1602
1603 v = inb(0x4d1) << 8 | inb(0x4d0);
1604 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1605}
1606
1607#endif /* 0 */
1608
1609static void __init enable_IO_APIC(void)
1610{
1611 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001612 int i8259_apic, i8259_pin;
1613 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 unsigned long flags;
1615
1616 for (i = 0; i < PIN_MAP_SIZE; i++) {
1617 irq_2_pin[i].pin = -1;
1618 irq_2_pin[i].next = 0;
1619 }
1620 if (!pirqs_enabled)
1621 for (i = 0; i < MAX_PIRQS; i++)
1622 pirq_entries[i] = -1;
1623
1624 /*
1625 * The number of IO-APIC IRQ registers (== #pins):
1626 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001627 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001629 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001631 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1632 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001633 for (apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001634 int pin;
1635 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001636 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001637 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001638 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001639
1640
1641 /* If the interrupt line is enabled and in ExtInt mode
1642 * I have found the pin where the i8259 is connected.
1643 */
1644 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1645 ioapic_i8259.apic = apic;
1646 ioapic_i8259.pin = pin;
1647 goto found_i8259;
1648 }
1649 }
1650 }
1651 found_i8259:
1652 /* Look to see what if the MP table has reported the ExtINT */
1653 /* If we could not find the appropriate pin by looking at the ioapic
1654 * the i8259 probably is not connected the ioapic but give the
1655 * mptable a chance anyway.
1656 */
1657 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1658 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1659 /* Trust the MP table if nothing is setup in the hardware */
1660 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1661 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1662 ioapic_i8259.pin = i8259_pin;
1663 ioapic_i8259.apic = i8259_apic;
1664 }
1665 /* Complain if the MP table and the hardware disagree */
1666 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1667 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1668 {
1669 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672 /*
1673 * Do not trust the IO-APIC being empty at bootup
1674 */
1675 clear_IO_APIC();
1676}
1677
1678/*
1679 * Not an __init, needed by the reboot code
1680 */
1681void disable_IO_APIC(void)
1682{
1683 /*
1684 * Clear the IO-APIC before rebooting:
1685 */
1686 clear_IO_APIC();
1687
Eric W. Biederman650927e2005-06-25 14:57:44 -07001688 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001689 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001690 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001691 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001692 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001693 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001694 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001695
1696 memset(&entry, 0, sizeof(entry));
1697 entry.mask = 0; /* Enabled */
1698 entry.trigger = 0; /* Edge */
1699 entry.irr = 0;
1700 entry.polarity = 0; /* High */
1701 entry.delivery_status = 0;
1702 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001703 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001704 entry.vector = 0;
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001705 entry.dest.physical.physical_dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001706
1707 /*
1708 * Add it to the IO-APIC irq-routing table:
1709 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001710 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001711 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001712 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
1715/*
1716 * function to set the IO-APIC physical IDs based on the
1717 * values stored in the MPC table.
1718 *
1719 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1720 */
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static void __init setup_ioapic_ids_from_mpc(void)
1723{
1724 union IO_APIC_reg_00 reg_00;
1725 physid_mask_t phys_id_present_map;
1726 int apic;
1727 int i;
1728 unsigned char old_id;
1729 unsigned long flags;
1730
Yinghai Lud49c4282008-06-08 18:31:54 -07001731#ifdef CONFIG_X86_NUMAQ
1732 if (found_numaq)
1733 return;
1734#endif
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001737 * Don't check I/O APIC IDs for xAPIC systems. They have
1738 * no meaning without the serial APIC bus.
1739 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001740 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1741 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001742 return;
1743 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * This is broken; anything with a real cpu count has to
1745 * circumvent this idiocy regardless.
1746 */
1747 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1748
1749 /*
1750 * Set the IOAPIC ID to the value stored in the MPC table.
1751 */
1752 for (apic = 0; apic < nr_ioapics; apic++) {
1753
1754 /* Read the register 0 value */
1755 spin_lock_irqsave(&ioapic_lock, flags);
1756 reg_00.raw = io_apic_read(apic, 0);
1757 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001758
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001759 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001761 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001763 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1765 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001766 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * Sanity check, is the ID really free? Every APIC in a
1771 * system must have a unique ID or we get lots of nice
1772 * 'stuck on smp_invalidate_needed IPI wait' messages.
1773 */
1774 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001775 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001777 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 for (i = 0; i < get_physical_broadcast(); i++)
1779 if (!physid_isset(i, phys_id_present_map))
1780 break;
1781 if (i >= get_physical_broadcast())
1782 panic("Max APIC ID exceeded!\n");
1783 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1784 i);
1785 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001786 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 } else {
1788 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001789 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 apic_printk(APIC_VERBOSE, "Setting %d in the "
1791 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001792 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1794 }
1795
1796
1797 /*
1798 * We need to adjust the IRQ routing table
1799 * if the ID changed.
1800 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001801 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001803 if (mp_irqs[i].mp_dstapic == old_id)
1804 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001805 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 /*
1808 * Read the right value from the MPC table and
1809 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 apic_printk(APIC_VERBOSE, KERN_INFO
1812 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001813 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001815 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 spin_lock_irqsave(&ioapic_lock, flags);
1817 io_apic_write(apic, 0, reg_00.raw);
1818 spin_unlock_irqrestore(&ioapic_lock, flags);
1819
1820 /*
1821 * Sanity check
1822 */
1823 spin_lock_irqsave(&ioapic_lock, flags);
1824 reg_00.raw = io_apic_read(apic, 0);
1825 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001826 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 printk("could not set ID!\n");
1828 else
1829 apic_printk(APIC_VERBOSE, " ok.\n");
1830 }
1831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001833int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001834
1835static int __init notimercheck(char *s)
1836{
1837 no_timer_check = 1;
1838 return 1;
1839}
1840__setup("no_timer_check", notimercheck);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842/*
1843 * There is a nasty bug in some older SMP boards, their mptable lies
1844 * about the timer IRQ. We do the following to work around the situation:
1845 *
1846 * - timer IRQ defaults to IO-APIC IRQ
1847 * - if this function detects that timer IRQs are defunct, then we fall
1848 * back to ISA timer IRQs
1849 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001850static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001853 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Zachary Amsden8542b202006-12-07 02:14:09 +01001855 if (no_timer_check)
1856 return 1;
1857
Ingo Molnar4aae0702007-12-18 18:05:58 +01001858 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 local_irq_enable();
1860 /* Let ten ticks pass... */
1861 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001862 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 /*
1865 * Expect a few ticks at least, to be sure some possible
1866 * glue logic does not lock up after one or two first
1867 * ticks in a non-ExtINT mode. Also the local APIC
1868 * might have cached one ExtINT interrupt. Finally, at
1869 * least one tick may be lost due to delays.
1870 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001871 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return 1;
1873
1874 return 0;
1875}
1876
1877/*
1878 * In the SMP+IOAPIC case it might happen that there are an unspecified
1879 * number of pending IRQ events unhandled. These cases are very rare,
1880 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1881 * better to do it this way as thus we do not have to be aware of
1882 * 'pending' interrupts in the IRQ path, except at this point.
1883 */
1884/*
1885 * Edge triggered needs to resend any interrupt
1886 * that was delayed but this is now handled in the device
1887 * independent code.
1888 */
1889
1890/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001891 * Startup quirk:
1892 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * Starting up a edge-triggered IO-APIC interrupt is
1894 * nasty - we need to make sure that we get the edge.
1895 * If it is already asserted for some reason, we need
1896 * return 1 to indicate that is was pending.
1897 *
1898 * This is not complete - we should be able to fake
1899 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001900 *
1901 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001903static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 int was_pending = 0;
1906 unsigned long flags;
1907
1908 spin_lock_irqsave(&ioapic_lock, flags);
1909 if (irq < 16) {
1910 disable_8259A_irq(irq);
1911 if (i8259A_irq_pending(irq))
1912 was_pending = 1;
1913 }
1914 __unmask_IO_APIC_irq(irq);
1915 spin_unlock_irqrestore(&ioapic_lock, flags);
1916
1917 return was_pending;
1918}
1919
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001920static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001922 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 ack_APIC_irq();
1924}
1925
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001926static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 unsigned long v;
1929 int i;
1930
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001931 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
1933 * It appears there is an erratum which affects at least version 0x11
1934 * of I/O APIC (that's the 82093AA and cores integrated into various
1935 * chipsets). Under certain conditions a level-triggered interrupt is
1936 * erroneously delivered as edge-triggered one but the respective IRR
1937 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1938 * message but it will never arrive and further interrupts are blocked
1939 * from the source. The exact reason is so far unknown, but the
1940 * phenomenon was observed when two consecutive interrupt requests
1941 * from a given source get delivered to the same CPU and the source is
1942 * temporarily disabled in between.
1943 *
1944 * A workaround is to simulate an EOI message manually. We achieve it
1945 * by setting the trigger mode to edge and then to level when the edge
1946 * trigger mode gets detected in the TMR of a local APIC for a
1947 * level-triggered interrupt. We mask the source for the time of the
1948 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1949 * The idea is from Manfred Spraul. --macro
1950 */
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001951 i = irq_vector[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1954
1955 ack_APIC_irq();
1956
1957 if (!(v & (1 << (i & 0x1f)))) {
1958 atomic_inc(&irq_mis_count);
1959 spin_lock(&ioapic_lock);
1960 __mask_and_edge_IO_APIC_irq(irq);
1961 __unmask_and_level_IO_APIC_irq(irq);
1962 spin_unlock(&ioapic_lock);
1963 }
1964}
1965
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001966static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001968 send_IPI_self(irq_vector[irq]);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001969
1970 return 1;
1971}
1972
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001973static struct irq_chip ioapic_chip __read_mostly = {
1974 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001975 .startup = startup_ioapic_irq,
1976 .mask = mask_IO_APIC_irq,
1977 .unmask = unmask_IO_APIC_irq,
1978 .ack = ack_ioapic_irq,
1979 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001980#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001981 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001982#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001983 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984};
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987static inline void init_IO_APIC_traps(void)
1988{
1989 int irq;
1990
1991 /*
1992 * NOTE! The local APIC isn't very good at handling
1993 * multiple interrupts at the same interrupt level.
1994 * As the interrupt level is determined by taking the
1995 * vector number and shifting that right by 4, we
1996 * want to spread these out a bit so that they don't
1997 * all fall in the same interrupt level.
1998 *
1999 * Also, we've got to be careful not to trash gate
2000 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2001 */
2002 for (irq = 0; irq < NR_IRQS ; irq++) {
Akinobu Mitaaddfc662008-04-05 22:39:07 +09002003 if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /*
2005 * Hmm.. We don't have an entry for this,
2006 * so default to an old-fashioned 8259
2007 * interrupt if we can..
2008 */
2009 if (irq < 16)
2010 make_8259A_irq(irq);
2011 else
2012 /* Strange. Oh, well.. */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002013 irq_desc[irq].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 }
2016}
2017
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002018/*
2019 * The local APIC irq-chip implementation:
2020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002022static void ack_lapic_irq(unsigned int irq)
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002023{
2024 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002027static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 unsigned long v;
2030
2031 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002032 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002035static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002037 unsigned long v;
2038
2039 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002040 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002043static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002044 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002045 .mask = mask_lapic_irq,
2046 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002047 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048};
2049
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002050static void lapic_register_intr(int irq, int vector)
2051{
2052 irq_desc[irq].status &= ~IRQ_LEVEL;
2053 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2054 "edge");
2055 set_intr_gate(vector, interrupt[irq]);
2056}
2057
Jan Beuliche9427102008-01-30 13:31:24 +01002058static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002061 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * We put the 8259A master into AEOI mode and
2063 * unmask on all local APICs LVT0 as NMI.
2064 *
2065 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2066 * is from Maciej W. Rozycki - so we do not have to EOI from
2067 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2070
Jan Beuliche9427102008-01-30 13:31:24 +01002071 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 apic_printk(APIC_VERBOSE, " done.\n");
2074}
2075
2076/*
2077 * This looks a bit hackish but it's about the only one way of sending
2078 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2079 * not support the ExtINT mode, unfortunately. We need to send these
2080 * cycles as some i82489DX-based boards have glue logic that keeps the
2081 * 8259A interrupt line asserted until INTA. --macro
2082 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002083static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002085 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 struct IO_APIC_route_entry entry0, entry1;
2087 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002089 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002090 if (pin == -1) {
2091 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002093 }
2094 apic = find_isa_irq_apic(8, mp_INT);
2095 if (apic == -1) {
2096 WARN_ON_ONCE(1);
2097 return;
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Andi Kleencf4c6a22006-09-26 10:52:30 +02002100 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002101 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 memset(&entry1, 0, sizeof(entry1));
2104
2105 entry1.dest_mode = 0; /* physical delivery */
2106 entry1.mask = 0; /* unmask IRQ now */
2107 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2108 entry1.delivery_mode = dest_ExtINT;
2109 entry1.polarity = entry0.polarity;
2110 entry1.trigger = 0;
2111 entry1.vector = 0;
2112
Andi Kleencf4c6a22006-09-26 10:52:30 +02002113 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 save_control = CMOS_READ(RTC_CONTROL);
2116 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2117 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2118 RTC_FREQ_SELECT);
2119 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2120
2121 i = 100;
2122 while (i-- > 0) {
2123 mdelay(10);
2124 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2125 i -= 10;
2126 }
2127
2128 CMOS_WRITE(save_control, RTC_CONTROL);
2129 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002130 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Andi Kleencf4c6a22006-09-26 10:52:30 +02002132 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135/*
2136 * This code may look a bit paranoid, but it's supposed to cooperate with
2137 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2138 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2139 * fanatically on his truly buggy board.
2140 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002141static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002143 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002144 int no_pin1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int vector;
Ingo Molnar6e908942008-03-21 14:32:36 +01002146 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002147 unsigned long flags;
2148
2149 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002150
Ingo Molnar6e908942008-03-21 14:32:36 +01002151 ver = apic_read(APIC_LVR);
2152 ver = GET_APIC_VERSION(ver);
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
2155 * get/set the timer IRQ vector:
2156 */
2157 disable_8259A_irq(0);
2158 vector = assign_irq_vector(0);
2159 set_intr_gate(vector, interrupt[0]);
2160
2161 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002162 * As IRQ0 is to be enabled in the 8259A, the virtual
2163 * wire has to be disabled in the local APIC. Also
2164 * timer interrupts need to be acknowledged manually in
2165 * the 8259A for the i82489DX when using the NMI
2166 * watchdog as that APIC treats NMIs as level-triggered.
2167 * The AEOI mode will finish them in the 8259A
2168 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002170 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002172 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002174 pin1 = find_isa_irq_pin(0, mp_INT);
2175 apic1 = find_isa_irq_apic(0, mp_INT);
2176 pin2 = ioapic_i8259.pin;
2177 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002179 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2180 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2181 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002183 /*
2184 * Some BIOS writers are clueless and report the ExtINTA
2185 * I/O APIC input from the cascaded 8259A as the timer
2186 * interrupt input. So just in case, if only one pin
2187 * was found above, try it both directly and through the
2188 * 8259A.
2189 */
2190 if (pin1 == -1) {
2191 pin1 = pin2;
2192 apic1 = apic2;
2193 no_pin1 = 1;
2194 } else if (pin2 == -1) {
2195 pin2 = pin1;
2196 apic2 = apic1;
2197 }
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (pin1 != -1) {
2200 /*
2201 * Ok, does IRQ0 through the IOAPIC work?
2202 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002203 if (no_pin1) {
2204 add_pin_to_irq(0, apic1, pin1);
2205 setup_timer_IRQ0_pin(apic1, pin1, vector);
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 unmask_IO_APIC_irq(0);
2208 if (timer_irq_works()) {
2209 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 setup_nmi();
2211 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002213 if (disable_timer_pin_1 > 0)
2214 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002215 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002217 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002218 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002219 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2220 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002222 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2223 "(IRQ0) through the 8259A ...\n");
2224 apic_printk(APIC_QUIET, KERN_INFO
2225 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * legacy devices should be connected to IO APIC #0
2228 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002229 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01002230 setup_timer_IRQ0_pin(apic2, pin2, vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002231 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002232 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002234 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002235 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002237 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002239 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243 /*
2244 * Cleanup, just in case ...
2245 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002246 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002247 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002248 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002252 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2253 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002254 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002256 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002258 apic_printk(APIC_QUIET, KERN_INFO
2259 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002261 lapic_register_intr(0, vector);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002262 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 enable_8259A_irq(0);
2264
2265 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002266 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002269 disable_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002270 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002271 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002273 apic_printk(APIC_QUIET, KERN_INFO
2274 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 init_8259A(0);
2277 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002278 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 unlock_ExtINT_logic();
2281
2282 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002283 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002286 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002288 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002289out:
2290 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
2293/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002294 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2295 * to devices. However there may be an I/O APIC pin available for
2296 * this interrupt regardless. The pin may be left unconnected, but
2297 * typically it will be reused as an ExtINT cascade interrupt for
2298 * the master 8259A. In the MPS case such a pin will normally be
2299 * reported as an ExtINT interrupt in the MP table. With ACPI
2300 * there is no provision for ExtINT interrupts, and in the absence
2301 * of an override it would be treated as an ordinary ISA I/O APIC
2302 * interrupt, that is edge-triggered and unmasked by default. We
2303 * used to do this, but it caused problems on some systems because
2304 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2305 * the same ExtINT cascade interrupt to drive the local APIC of the
2306 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2307 * the I/O APIC in all cases now. No actual device should request
2308 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 */
2310#define PIC_IRQS (1 << PIC_CASCADE_IR)
2311
2312void __init setup_IO_APIC(void)
2313{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002314 int i;
2315
2316 /* Reserve all the system vectors. */
Alan Mayer305b92a2008-04-15 15:36:56 -05002317 for (i = first_system_vector; i < NR_VECTORS; i++)
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002318 set_bit(i, used_vectors);
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 enable_IO_APIC();
2321
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002322 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 printk("ENABLING IO-APIC IRQs\n");
2325
2326 /*
2327 * Set up IO-APIC IRQ routing.
2328 */
2329 if (!acpi_ioapic)
2330 setup_ioapic_ids_from_mpc();
2331 sync_Arb_IDs();
2332 setup_IO_APIC_irqs();
2333 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002334 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (!acpi_ioapic)
2336 print_IO_APIC();
2337}
2338
2339/*
2340 * Called after all the initialization is done. If we didnt find any
2341 * APIC bugs then we can allow the modify fast path
2342 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344static int __init io_apic_bug_finalize(void)
2345{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002346 if (sis_apic_bug == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 sis_apic_bug = 0;
2348 return 0;
2349}
2350
2351late_initcall(io_apic_bug_finalize);
2352
2353struct sysfs_ioapic_data {
2354 struct sys_device dev;
2355 struct IO_APIC_route_entry entry[0];
2356};
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002357static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Pavel Machek438510f2005-04-16 15:25:24 -07002359static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 struct IO_APIC_route_entry *entry;
2362 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 data = container_of(dev, struct sysfs_ioapic_data, dev);
2366 entry = data->entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002367 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002368 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 return 0;
2371}
2372
2373static int ioapic_resume(struct sys_device *dev)
2374{
2375 struct IO_APIC_route_entry *entry;
2376 struct sysfs_ioapic_data *data;
2377 unsigned long flags;
2378 union IO_APIC_reg_00 reg_00;
2379 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 data = container_of(dev, struct sysfs_ioapic_data, dev);
2382 entry = data->entry;
2383
2384 spin_lock_irqsave(&ioapic_lock, flags);
2385 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002386 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2387 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 io_apic_write(dev->id, 0, reg_00.raw);
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002391 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002392 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 return 0;
2395}
2396
2397static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002398 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 .suspend = ioapic_suspend,
2400 .resume = ioapic_resume,
2401};
2402
2403static int __init ioapic_init_sysfs(void)
2404{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002405 struct sys_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 int i, size, error = 0;
2407
2408 error = sysdev_class_register(&ioapic_sysdev_class);
2409 if (error)
2410 return error;
2411
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002412 for (i = 0; i < nr_ioapics; i++) {
2413 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002415 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (!mp_ioapic_data[i]) {
2417 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2418 continue;
2419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002421 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 dev->cls = &ioapic_sysdev_class;
2423 error = sysdev_register(dev);
2424 if (error) {
2425 kfree(mp_ioapic_data[i]);
2426 mp_ioapic_data[i] = NULL;
2427 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2428 continue;
2429 }
2430 }
2431
2432 return 0;
2433}
2434
2435device_initcall(ioapic_init_sysfs);
2436
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002437/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002438 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002439 */
2440int create_irq(void)
2441{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002442 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002443 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002444 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002445
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002446 irq = -ENOSPC;
2447 spin_lock_irqsave(&vector_lock, flags);
2448 for (new = (NR_IRQS - 1); new >= 0; new--) {
2449 if (platform_legacy_irq(new))
2450 continue;
2451 if (irq_vector[new] != 0)
2452 continue;
2453 vector = __assign_irq_vector(new);
2454 if (likely(vector > 0))
2455 irq = new;
2456 break;
2457 }
2458 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002459
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002460 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002461 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002462 dynamic_irq_init(irq);
2463 }
2464 return irq;
2465}
2466
2467void destroy_irq(unsigned int irq)
2468{
2469 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002470
2471 dynamic_irq_cleanup(irq);
2472
2473 spin_lock_irqsave(&vector_lock, flags);
PJ Waskiewicz9d9ad4b2008-04-25 17:58:52 -07002474 clear_bit(irq_vector[irq], used_vectors);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002475 irq_vector[irq] = 0;
2476 spin_unlock_irqrestore(&vector_lock, flags);
2477}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002478
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002479/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002480 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002481 */
2482#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002483static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002484{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002485 int vector;
2486 unsigned dest;
2487
2488 vector = assign_irq_vector(irq);
2489 if (vector >= 0) {
2490 dest = cpu_mask_to_apicid(TARGET_CPUS);
2491
2492 msg->address_hi = MSI_ADDR_BASE_HI;
2493 msg->address_lo =
2494 MSI_ADDR_BASE_LO |
2495 ((INT_DEST_MODE == 0) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002496MSI_ADDR_DEST_MODE_PHYSICAL:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002497 MSI_ADDR_DEST_MODE_LOGICAL) |
2498 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2499 MSI_ADDR_REDIRECTION_CPU:
2500 MSI_ADDR_REDIRECTION_LOWPRI) |
2501 MSI_ADDR_DEST_ID(dest);
2502
2503 msg->data =
2504 MSI_DATA_TRIGGER_EDGE |
2505 MSI_DATA_LEVEL_ASSERT |
2506 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002507MSI_DATA_DELIVERY_FIXED:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002508 MSI_DATA_DELIVERY_LOWPRI) |
2509 MSI_DATA_VECTOR(vector);
2510 }
2511 return vector;
2512}
2513
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002514#ifdef CONFIG_SMP
2515static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2516{
2517 struct msi_msg msg;
2518 unsigned int dest;
2519 cpumask_t tmp;
2520 int vector;
2521
2522 cpus_and(tmp, mask, cpu_online_map);
2523 if (cpus_empty(tmp))
2524 tmp = TARGET_CPUS;
2525
2526 vector = assign_irq_vector(irq);
2527 if (vector < 0)
2528 return;
2529
2530 dest = cpu_mask_to_apicid(mask);
2531
2532 read_msi_msg(irq, &msg);
2533
2534 msg.data &= ~MSI_DATA_VECTOR_MASK;
2535 msg.data |= MSI_DATA_VECTOR(vector);
2536 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2537 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2538
2539 write_msi_msg(irq, &msg);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002540 irq_desc[irq].affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002541}
2542#endif /* CONFIG_SMP */
2543
2544/*
2545 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2546 * which implement the MSI or MSI-X Capability Structure.
2547 */
2548static struct irq_chip msi_chip = {
2549 .name = "PCI-MSI",
2550 .unmask = unmask_msi_irq,
2551 .mask = mask_msi_irq,
2552 .ack = ack_ioapic_irq,
2553#ifdef CONFIG_SMP
2554 .set_affinity = set_msi_irq_affinity,
2555#endif
2556 .retrigger = ioapic_retrigger_irq,
2557};
2558
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002559int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002560{
2561 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002562 int irq, ret;
2563 irq = create_irq();
2564 if (irq < 0)
2565 return irq;
2566
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002567 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002568 if (ret < 0) {
2569 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002570 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002571 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002572
Michael Ellerman7fe37302007-04-18 19:39:21 +10002573 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002574 write_msi_msg(irq, &msg);
2575
Ingo Molnara460e742006-10-17 00:10:03 -07002576 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2577 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002578
Michael Ellerman7fe37302007-04-18 19:39:21 +10002579 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002580}
2581
2582void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002583{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002584 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002585}
2586
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002587#endif /* CONFIG_PCI_MSI */
2588
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002589/*
2590 * Hypertransport interrupt support
2591 */
2592#ifdef CONFIG_HT_IRQ
2593
2594#ifdef CONFIG_SMP
2595
2596static void target_ht_irq(unsigned int irq, unsigned int dest)
2597{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002598 struct ht_irq_msg msg;
2599 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002600
Eric W. Biedermanec683072006-11-08 17:44:57 -08002601 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2602 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002603
Eric W. Biedermanec683072006-11-08 17:44:57 -08002604 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2605 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002606
Eric W. Biedermanec683072006-11-08 17:44:57 -08002607 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002608}
2609
2610static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2611{
2612 unsigned int dest;
2613 cpumask_t tmp;
2614
2615 cpus_and(tmp, mask, cpu_online_map);
2616 if (cpus_empty(tmp))
2617 tmp = TARGET_CPUS;
2618
2619 cpus_and(mask, tmp, CPU_MASK_ALL);
2620
2621 dest = cpu_mask_to_apicid(mask);
2622
2623 target_ht_irq(irq, dest);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002624 irq_desc[irq].affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002625}
2626#endif
2627
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002628static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002629 .name = "PCI-HT",
2630 .mask = mask_ht_irq,
2631 .unmask = unmask_ht_irq,
2632 .ack = ack_ioapic_irq,
2633#ifdef CONFIG_SMP
2634 .set_affinity = set_ht_irq_affinity,
2635#endif
2636 .retrigger = ioapic_retrigger_irq,
2637};
2638
2639int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2640{
2641 int vector;
2642
2643 vector = assign_irq_vector(irq);
2644 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002645 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002646 unsigned dest;
2647 cpumask_t tmp;
2648
2649 cpus_clear(tmp);
2650 cpu_set(vector >> 8, tmp);
2651 dest = cpu_mask_to_apicid(tmp);
2652
Eric W. Biedermanec683072006-11-08 17:44:57 -08002653 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002654
Eric W. Biedermanec683072006-11-08 17:44:57 -08002655 msg.address_lo =
2656 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002657 HT_IRQ_LOW_DEST_ID(dest) |
2658 HT_IRQ_LOW_VECTOR(vector) |
2659 ((INT_DEST_MODE == 0) ?
2660 HT_IRQ_LOW_DM_PHYSICAL :
2661 HT_IRQ_LOW_DM_LOGICAL) |
2662 HT_IRQ_LOW_RQEOI_EDGE |
2663 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2664 HT_IRQ_LOW_MT_FIXED :
2665 HT_IRQ_LOW_MT_ARBITRATED) |
2666 HT_IRQ_LOW_IRQ_MASKED;
2667
Eric W. Biedermanec683072006-11-08 17:44:57 -08002668 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002669
Ingo Molnara460e742006-10-17 00:10:03 -07002670 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2671 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002672 }
2673 return vector;
2674}
2675#endif /* CONFIG_HT_IRQ */
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677/* --------------------------------------------------------------------------
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002678 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 -------------------------------------------------------------------------- */
2680
Len Brown888ba6c2005-08-24 12:07:20 -04002681#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002683int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 union IO_APIC_reg_00 reg_00;
2686 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2687 physid_mask_t tmp;
2688 unsigned long flags;
2689 int i = 0;
2690
2691 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002692 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2693 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2697 * advantage of new APIC bus architecture.
2698 */
2699
2700 if (physids_empty(apic_id_map))
2701 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2702
2703 spin_lock_irqsave(&ioapic_lock, flags);
2704 reg_00.raw = io_apic_read(ioapic, 0);
2705 spin_unlock_irqrestore(&ioapic_lock, flags);
2706
2707 if (apic_id >= get_physical_broadcast()) {
2708 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2709 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2710 apic_id = reg_00.bits.ID;
2711 }
2712
2713 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002714 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * 'stuck on smp_invalidate_needed IPI wait' messages.
2716 */
2717 if (check_apicid_used(apic_id_map, apic_id)) {
2718
2719 for (i = 0; i < get_physical_broadcast(); i++) {
2720 if (!check_apicid_used(apic_id_map, i))
2721 break;
2722 }
2723
2724 if (i == get_physical_broadcast())
2725 panic("Max apic_id exceeded!\n");
2726
2727 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2728 "trying %d\n", ioapic, apic_id, i);
2729
2730 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 tmp = apicid_to_cpu_present(apic_id);
2734 physids_or(apic_id_map, apic_id_map, tmp);
2735
2736 if (reg_00.bits.ID != apic_id) {
2737 reg_00.bits.ID = apic_id;
2738
2739 spin_lock_irqsave(&ioapic_lock, flags);
2740 io_apic_write(ioapic, 0, reg_00.raw);
2741 reg_00.raw = io_apic_read(ioapic, 0);
2742 spin_unlock_irqrestore(&ioapic_lock, flags);
2743
2744 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002745 if (reg_00.bits.ID != apic_id) {
2746 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2747 return -1;
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750
2751 apic_printk(APIC_VERBOSE, KERN_INFO
2752 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2753
2754 return apic_id;
2755}
2756
2757
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002758int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 union IO_APIC_reg_01 reg_01;
2761 unsigned long flags;
2762
2763 spin_lock_irqsave(&ioapic_lock, flags);
2764 reg_01.raw = io_apic_read(ioapic, 1);
2765 spin_unlock_irqrestore(&ioapic_lock, flags);
2766
2767 return reg_01.bits.version;
2768}
2769
2770
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002771int __init io_apic_get_redir_entries(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 union IO_APIC_reg_01 reg_01;
2774 unsigned long flags;
2775
2776 spin_lock_irqsave(&ioapic_lock, flags);
2777 reg_01.raw = io_apic_read(ioapic, 1);
2778 spin_unlock_irqrestore(&ioapic_lock, flags);
2779
2780 return reg_01.bits.entries;
2781}
2782
2783
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002784int 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 -07002785{
2786 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 if (!IO_APIC_IRQ(irq)) {
2789 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2790 ioapic);
2791 return -EINVAL;
2792 }
2793
2794 /*
2795 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2796 * Note that we mask (disable) IRQs now -- these get enabled when the
2797 * corresponding device driver registers for this IRQ.
2798 */
2799
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002800 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 entry.delivery_mode = INT_DELIVERY_MODE;
2803 entry.dest_mode = INT_DEST_MODE;
2804 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2805 entry.trigger = edge_level;
2806 entry.polarity = active_high_low;
2807 entry.mask = 1;
2808
2809 /*
2810 * IRQs < 16 are already in the irq_2_pin[] map
2811 */
2812 if (irq >= 16)
2813 add_pin_to_irq(irq, ioapic, pin);
2814
2815 entry.vector = assign_irq_vector(irq);
2816
2817 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2818 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002819 mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 edge_level, active_high_low);
2821
2822 ioapic_register_intr(irq, entry.vector, edge_level);
2823
2824 if (!ioapic && (irq < 16))
2825 disable_8259A_irq(irq);
2826
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002827 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 return 0;
2830}
2831
Shaohua Li61fd47e2007-11-17 01:05:28 -05002832int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2833{
2834 int i;
2835
2836 if (skip_ioapic_setup)
2837 return -1;
2838
2839 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002840 if (mp_irqs[i].mp_irqtype == mp_INT &&
2841 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002842 break;
2843 if (i >= mp_irq_entries)
2844 return -1;
2845
2846 *trigger = irq_trigger(i);
2847 *polarity = irq_polarity(i);
2848 return 0;
2849}
2850
Len Brown888ba6c2005-08-24 12:07:20 -04002851#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002852
2853static int __init parse_disable_timer_pin_1(char *arg)
2854{
2855 disable_timer_pin_1 = 1;
2856 return 0;
2857}
2858early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2859
2860static int __init parse_enable_timer_pin_1(char *arg)
2861{
2862 disable_timer_pin_1 = -1;
2863 return 0;
2864}
2865early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2866
2867static int __init parse_noapic(char *arg)
2868{
2869 /* disable IO-APIC */
2870 disable_ioapic_setup();
2871 return 0;
2872}
2873early_param("noapic", parse_noapic);
Yinghai Luf3294a32008-06-27 01:41:56 -07002874
2875void __init ioapic_init_mappings(void)
2876{
2877 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2878 int i;
2879
2880 for (i = 0; i < nr_ioapics; i++) {
2881 if (smp_found_config) {
2882 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2883 if (!ioapic_phys) {
2884 printk(KERN_ERR
2885 "WARNING: bogus zero IO-APIC "
2886 "address found in MPTABLE, "
2887 "disabling IO/APIC support!\n");
2888 smp_found_config = 0;
2889 skip_ioapic_setup = 1;
2890 goto fake_ioapic_page;
2891 }
2892 } else {
2893fake_ioapic_page:
2894 ioapic_phys = (unsigned long)
2895 alloc_bootmem_pages(PAGE_SIZE);
2896 ioapic_phys = __pa(ioapic_phys);
2897 }
2898 set_fixmap_nocache(idx, ioapic_phys);
2899 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
2900 __fix_to_virt(idx), ioapic_phys);
2901 idx++;
2902 }
2903}
2904