blob: c2160cfdec9b236337118ec8eaaa5f7e24ffcae9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Luf3294a32008-06-27 01:41:56 -070028#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070034#include <linux/pci.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070035#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070036#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020038#include <linux/kthread.h>
Julia Lawall1d16b532008-01-30 13:32:19 +010039#include <linux/jiffies.h> /* time_after() */
Ashok Raj54d5d422005-09-06 15:16:15 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/smp.h>
43#include <asm/desc.h>
44#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070045#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020046#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070047#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070048#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070049#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020052#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010054#define __apicdebuginit(type) static type __init
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056int (*ioapic_renumber_irq)(int ioapic, int irq);
57atomic_t irq_mis_count;
58
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080059/* Where if anywhere is the i8259 connect in external int mode */
60static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(ioapic_lock);
Eric W. Biedermand388e5f2008-08-09 15:09:02 -070063DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010065int timer_through_8259 __initdata;
Andi Kleenf9262c12006-03-08 17:57:25 -080066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Is the SiS APIC rmw bug present ?
69 * -1 = don't know, 0 = no, 1 = yes
70 */
71int sis_apic_bug = -1;
72
Yinghai Lu301e6192008-08-19 20:50:02 -070073int first_free_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * # of IRQ routing registers
76 */
77int nr_ioapic_registers[MAX_IO_APICS];
78
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040079/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040080struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040081int nr_ioapics;
82
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040083/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040084struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040085
86/* # of MP IRQ source entries */
87int mp_irq_entries;
88
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040089#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
90int mp_bus_id_to_type[MAX_MP_BUSSES];
91#endif
92
93DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
94
Rusty Russell1a3f2392006-09-26 10:52:32 +020095static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * Rough estimation of how many shared IRQs there are, can
99 * be changed anytime.
100 */
Yinghai Lu301e6192008-08-19 20:50:02 -0700101int pin_map_size;
Yinghai Lu0799e432008-08-19 20:49:48 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * This is performance-critical, we want to do it O(1)
105 *
106 * the indexing order of this array favors 1:1 mappings
107 * between pins and IRQs.
108 */
109
110static struct irq_pin_list {
111 int apic, pin, next;
Yinghai Lu301e6192008-08-19 20:50:02 -0700112} *irq_2_pin;
113
114DEFINE_DYN_ARRAY(irq_2_pin, sizeof(struct irq_pin_list), pin_map_size, 16, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds130fe052006-11-01 09:11:00 -0800116struct io_apic {
117 unsigned int index;
118 unsigned int unused[3];
119 unsigned int data;
120};
121
122static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
123{
124 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400125 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800126}
127
128static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
129{
130 struct io_apic __iomem *io_apic = io_apic_base(apic);
131 writel(reg, &io_apic->index);
132 return readl(&io_apic->data);
133}
134
135static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
136{
137 struct io_apic __iomem *io_apic = io_apic_base(apic);
138 writel(reg, &io_apic->index);
139 writel(value, &io_apic->data);
140}
141
142/*
143 * Re-write a value: to be used for read-modify-write
144 * cycles where the read already set up the index register.
145 *
146 * Older SiS APIC requires we rewrite the index register
147 */
148static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
149{
Al Virocb468982007-02-09 16:39:25 +0000150 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800151 if (sis_apic_bug)
152 writel(reg, &io_apic->index);
153 writel(value, &io_apic->data);
154}
155
Andi Kleencf4c6a22006-09-26 10:52:30 +0200156union entry_union {
157 struct { u32 w1, w2; };
158 struct IO_APIC_route_entry entry;
159};
160
161static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
162{
163 union entry_union eu;
164 unsigned long flags;
165 spin_lock_irqsave(&ioapic_lock, flags);
166 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
167 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
168 spin_unlock_irqrestore(&ioapic_lock, flags);
169 return eu.entry;
170}
171
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800172/*
173 * When we write a new IO APIC routing entry, we need to write the high
174 * word first! If the mask bit in the low word is clear, we will enable
175 * the interrupt, and we need to make sure the entry is fully populated
176 * before that happens.
177 */
Andi Kleend15512f2006-12-07 02:14:07 +0100178static void
179__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
180{
181 union entry_union eu;
182 eu.entry = e;
183 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
184 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
185}
186
Andi Kleencf4c6a22006-09-26 10:52:30 +0200187static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
188{
189 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200190 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100191 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800192 spin_unlock_irqrestore(&ioapic_lock, flags);
193}
194
195/*
196 * When we mask an IO APIC routing entry, we need to write the low
197 * word first, in order to set the mask bit before we change the
198 * high bits!
199 */
200static void ioapic_mask_entry(int apic, int pin)
201{
202 unsigned long flags;
203 union entry_union eu = { .entry.mask = 1 };
204
205 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200206 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
207 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
208 spin_unlock_irqrestore(&ioapic_lock, flags);
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
213 * shared ISA-space IRQs, so we have to support them. We are super
214 * fast in the common case, and fast for shared ISA-space IRQs.
215 */
216static void add_pin_to_irq(unsigned int irq, int apic, int pin)
217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct irq_pin_list *entry = irq_2_pin + irq;
219
220 while (entry->next)
221 entry = irq_2_pin + entry->next;
222
223 if (entry->pin != -1) {
224 entry->next = first_free_entry;
225 entry = irq_2_pin + entry->next;
Yinghai Lu0799e432008-08-19 20:49:48 -0700226 if (++first_free_entry >= pin_map_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 panic("io_apic.c: whoops");
228 }
229 entry->apic = apic;
230 entry->pin = pin;
231}
232
233/*
234 * Reroute an IRQ to a different pin.
235 */
236static void __init replace_pin_at_irq(unsigned int irq,
237 int oldapic, int oldpin,
238 int newapic, int newpin)
239{
240 struct irq_pin_list *entry = irq_2_pin + irq;
241
242 while (1) {
243 if (entry->apic == oldapic && entry->pin == oldpin) {
244 entry->apic = newapic;
245 entry->pin = newpin;
246 }
247 if (!entry->next)
248 break;
249 entry = irq_2_pin + entry->next;
250 }
251}
252
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200253static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct irq_pin_list *entry = irq_2_pin + irq;
256 unsigned int pin, reg;
257
258 for (;;) {
259 pin = entry->pin;
260 if (pin == -1)
261 break;
262 reg = io_apic_read(entry->apic, 0x10 + pin*2);
263 reg &= ~disable;
264 reg |= enable;
265 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
266 if (!entry->next)
267 break;
268 entry = irq_2_pin + entry->next;
269 }
270}
271
272/* mask = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200273static void __mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400275 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/* mask = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200279static void __unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400281 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/* mask = 1, trigger = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200285static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400287 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
288 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/* mask = 0, trigger = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200292static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400294 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
295 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200298static void mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned long flags;
301
302 spin_lock_irqsave(&ioapic_lock, flags);
303 __mask_IO_APIC_irq(irq);
304 spin_unlock_irqrestore(&ioapic_lock, flags);
305}
306
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200307static void unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned long flags;
310
311 spin_lock_irqsave(&ioapic_lock, flags);
312 __unmask_IO_APIC_irq(irq);
313 spin_unlock_irqrestore(&ioapic_lock, flags);
314}
315
316static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
317{
318 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200321 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (entry.delivery_mode == dest_SMI)
323 return;
324
325 /*
326 * Disable it in the IO-APIC irq-routing table:
327 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800328 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200331static void clear_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int apic, pin;
334
335 for (apic = 0; apic < nr_ioapics; apic++)
336 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
337 clear_IO_APIC_pin(apic, pin);
338}
339
Ashok Raj54d5d422005-09-06 15:16:15 -0700340#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
342{
343 unsigned long flags;
344 int pin;
345 struct irq_pin_list *entry = irq_2_pin + irq;
346 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700347 cpumask_t tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -0700348 struct irq_desc *desc;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200349
Ashok Raj54d5d422005-09-06 15:16:15 -0700350 cpus_and(tmp, cpumask, cpu_online_map);
351 if (cpus_empty(tmp))
352 tmp = TARGET_CPUS;
353
354 cpus_and(cpumask, tmp, CPU_MASK_ALL);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 apicid_value = cpu_mask_to_apicid(cpumask);
357 /* Prepare to do the io_apic_write */
358 apicid_value = apicid_value << 24;
359 spin_lock_irqsave(&ioapic_lock, flags);
360 for (;;) {
361 pin = entry->pin;
362 if (pin == -1)
363 break;
364 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
365 if (!entry->next)
366 break;
367 entry = irq_2_pin + entry->next;
368 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700369 desc = irq_to_desc(irq);
370 desc->affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_unlock_irqrestore(&ioapic_lock, flags);
372}
373
374#if defined(CONFIG_IRQBALANCE)
375# include <asm/processor.h> /* kernel_thread() */
376# include <linux/kernel_stat.h> /* kstat */
377# include <linux/slab.h> /* kmalloc() */
Julia Lawall1d16b532008-01-30 13:32:19 +0100378# include <linux/timer.h>
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#define IRQBALANCE_CHECK_ARCH -999
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700381#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
382#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
383#define BALANCED_IRQ_MORE_DELTA (HZ/10)
384#define BALANCED_IRQ_LESS_DELTA (HZ)
385
386static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
387static int physical_balance __read_mostly;
388static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390static struct irq_cpu_info {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200391 unsigned long *last_irq;
392 unsigned long *irq_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned long irq;
394} irq_cpu_data[NR_CPUS];
395
396#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200397#define LAST_CPU_IRQ(cpu, irq) (irq_cpu_data[cpu].last_irq[irq])
398#define IRQ_DELTA(cpu, irq) (irq_cpu_data[cpu].irq_delta[irq])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400#define IDLE_ENOUGH(cpu,now) \
401 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
402
403#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
404
Mike Travisd5a74302007-10-16 01:24:05 -0700405#define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Yinghai Lu301e6192008-08-19 20:50:02 -0700407static cpumask_t balance_irq_affinity_init __initdata = CPU_MASK_ALL;
408
409static cpumask_t *balance_irq_affinity;
410
411
412static void __init irq_affinity_init_work(void *data)
413{
414 struct dyn_array *da = data;
415
416 int i;
417 struct balance_irq_affinity *affinity;
418
419 affinity = *da->name;
420
421 for (i = 0; i < *da->nr; i++)
422 memcpy(&affinity[i], &balance_irq_affinity_init,
423 sizeof(struct balance_irq_affinity));
424
425}
426
427DEFINE_DYN_ARRAY(balance_irq_affinity, sizeof(struct balance_irq_affinity), nr_irqs, PAGE_SIZE, irq_affinity_init_work);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700430void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
431{
432 balance_irq_affinity[irq] = mask;
433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
436 unsigned long now, int direction)
437{
438 int search_idle = 1;
439 int cpu = curr_cpu;
440
441 goto inside;
442
443 do {
444 if (unlikely(cpu == curr_cpu))
445 search_idle = 0;
446inside:
447 if (direction == 1) {
448 cpu++;
449 if (cpu >= NR_CPUS)
450 cpu = 0;
451 } else {
452 cpu--;
453 if (cpu == -1)
454 cpu = NR_CPUS-1;
455 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200456 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu, allowed_mask) ||
457 (search_idle && !IDLE_ENOUGH(cpu, now)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 return cpu;
460}
461
462static inline void balance_irq(int cpu, int irq)
463{
464 unsigned long now = jiffies;
465 cpumask_t allowed_mask;
466 unsigned int new_cpu;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (irqbalance_disabled)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200469 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700471 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 new_cpu = move(cpu, allowed_mask, now, 1);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200473 if (cpu != new_cpu)
Ashok Raj54d5d422005-09-06 15:16:15 -0700474 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
478{
479 int i, j;
Yinghai Lu08678b02008-08-19 20:50:05 -0700480 struct irq_desc *desc;
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200481
Andrew Morton394e3902006-03-23 03:01:05 -0800482 for_each_online_cpu(i) {
Yinghai Lu0799e432008-08-19 20:49:48 -0700483 for (j = 0; j < nr_irqs; j++) {
Yinghai Lu08678b02008-08-19 20:50:05 -0700484 desc = irq_to_desc(j);
485 if (!desc->action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 continue;
487 /* Is it a significant load ? */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200488 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i), j) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 useful_load_threshold)
490 continue;
491 balance_irq(i, j);
492 }
493 }
494 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200495 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
497}
498
499static void do_irq_balance(void)
500{
501 int i, j;
502 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
503 unsigned long move_this_load = 0;
504 int max_loaded = 0, min_loaded = 0;
505 int load;
506 unsigned long useful_load_threshold = balanced_irq_interval + 10;
507 int selected_irq;
508 int tmp_loaded, first_attempt = 1;
509 unsigned long tmp_cpu_irq;
510 unsigned long imbalance = 0;
511 cpumask_t allowed_mask, target_cpu_mask, tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -0700512 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800514 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int package_index;
516 CPU_IRQ(i) = 0;
517 if (!cpu_online(i))
518 continue;
519 package_index = CPU_TO_PACKAGEINDEX(i);
Yinghai Lu0799e432008-08-19 20:49:48 -0700520 for (j = 0; j < nr_irqs; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned long value_now, delta;
Thomas Gleixner950f4422007-02-16 01:27:24 -0800522 /* Is this an active IRQ or balancing disabled ? */
Yinghai Lu08678b02008-08-19 20:50:05 -0700523 desc = irq_to_desc(j);
524 if (!desc->action || irq_balancing_disabled(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200526 if (package_index == i)
527 IRQ_DELTA(package_index, j) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Determine the total count per processor per IRQ */
529 value_now = (unsigned long) kstat_cpu(i).irqs[j];
530
531 /* Determine the activity per processor per IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200532 delta = value_now - LAST_CPU_IRQ(i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Update last_cpu_irq[][] for the next time */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200535 LAST_CPU_IRQ(i, j) = value_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Ignore IRQs whose rate is less than the clock */
538 if (delta < useful_load_threshold)
539 continue;
540 /* update the load for the processor or package total */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200541 IRQ_DELTA(package_index, j) += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* Keep track of the higher numbered sibling as well */
544 if (i != package_index)
545 CPU_IRQ(i) += delta;
546 /*
547 * We have sibling A and sibling B in the package
548 *
549 * cpu_irq[A] = load for cpu A + load for cpu B
550 * cpu_irq[B] = load for cpu B
551 */
552 CPU_IRQ(package_index) += delta;
553 }
554 }
555 /* Find the least loaded processor package */
Andrew Morton394e3902006-03-23 03:01:05 -0800556 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (i != CPU_TO_PACKAGEINDEX(i))
558 continue;
559 if (min_cpu_irq > CPU_IRQ(i)) {
560 min_cpu_irq = CPU_IRQ(i);
561 min_loaded = i;
562 }
563 }
564 max_cpu_irq = ULONG_MAX;
565
566tryanothercpu:
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200567 /*
568 * Look for heaviest loaded processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * We may come back to get the next heaviest loaded processor.
570 * Skip processors with trivial loads.
571 */
572 tmp_cpu_irq = 0;
573 tmp_loaded = -1;
Andrew Morton394e3902006-03-23 03:01:05 -0800574 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (i != CPU_TO_PACKAGEINDEX(i))
576 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200577 if (max_cpu_irq <= CPU_IRQ(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 continue;
579 if (tmp_cpu_irq < CPU_IRQ(i)) {
580 tmp_cpu_irq = CPU_IRQ(i);
581 tmp_loaded = i;
582 }
583 }
584
585 if (tmp_loaded == -1) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200586 /*
587 * In the case of small number of heavy interrupt sources,
588 * loading some of the cpus too much. We use Ingo's original
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * approach to rotate them around.
590 */
591 if (!first_attempt && imbalance >= useful_load_threshold) {
592 rotate_irqs_among_cpus(useful_load_threshold);
593 return;
594 }
595 goto not_worth_the_effort;
596 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 first_attempt = 0; /* heaviest search */
599 max_cpu_irq = tmp_cpu_irq; /* load */
600 max_loaded = tmp_loaded; /* processor */
601 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200602
603 /*
604 * if imbalance is less than approx 10% of max load, then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * observe diminishing returns action. - quit
606 */
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200607 if (imbalance < (max_cpu_irq >> 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto not_worth_the_effort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610tryanotherirq:
611 /* if we select an IRQ to move that can't go where we want, then
612 * see if there is another one to try.
613 */
614 move_this_load = 0;
615 selected_irq = -1;
Yinghai Lu0799e432008-08-19 20:49:48 -0700616 for (j = 0; j < nr_irqs; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Is this an active IRQ? */
Yinghai Lu08678b02008-08-19 20:50:05 -0700618 desc = irq_to_desc(j);
619 if (!desc->action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 continue;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200621 if (imbalance <= IRQ_DELTA(max_loaded, j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 continue;
623 /* Try to find the IRQ that is closest to the imbalance
624 * without going over.
625 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200626 if (move_this_load < IRQ_DELTA(max_loaded, j)) {
627 move_this_load = IRQ_DELTA(max_loaded, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 selected_irq = j;
629 }
630 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200631 if (selected_irq == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto tryanothercpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 imbalance = move_this_load;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200635
Simon Arlott27b46d72007-10-20 01:13:56 +0200636 /* For physical_balance case, we accumulated both load
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * values in the one of the siblings cpu_irq[],
638 * to use the same code for physical and logical processors
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200639 * as much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
641 * NOTE: the cpu_irq[] array holds the sum of the load for
642 * sibling A and sibling B in the slot for the lowest numbered
643 * sibling (A), _AND_ the load for sibling B in the slot for
644 * the higher numbered sibling.
645 *
646 * We seek the least loaded sibling by making the comparison
647 * (A+B)/2 vs B
648 */
649 load = CPU_IRQ(min_loaded) >> 1;
Mike Travisd5a74302007-10-16 01:24:05 -0700650 for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (load > CPU_IRQ(j)) {
652 /* This won't change cpu_sibling_map[min_loaded] */
653 load = CPU_IRQ(j);
654 min_loaded = j;
655 }
656 }
657
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700658 cpus_and(allowed_mask,
659 cpu_online_map,
660 balance_irq_affinity[selected_irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 target_cpu_mask = cpumask_of_cpu(min_loaded);
662 cpus_and(tmp, target_cpu_mask, allowed_mask);
663
664 if (!cpus_empty(tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* mark for change destination */
Ashok Raj54d5d422005-09-06 15:16:15 -0700666 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
667
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200668 /* Since we made a change, come back sooner to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * check for more variation.
670 */
671 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200672 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return;
674 }
675 goto tryanotherirq;
676
677not_worth_the_effort:
678 /*
679 * if we did not find an IRQ to move, then adjust the time interval
680 * upward
681 */
682 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200683 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return;
685}
686
687static int balanced_irq(void *unused)
688{
689 int i;
690 unsigned long prev_balance_time = jiffies;
691 long time_remaining = balanced_irq_interval;
Yinghai Lu08678b02008-08-19 20:50:05 -0700692 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* push everything to CPU 0 to give us a starting point. */
Yinghai Lu0799e432008-08-19 20:49:48 -0700695 for (i = 0 ; i < nr_irqs ; i++) {
Yinghai Lu08678b02008-08-19 20:50:05 -0700696 desc = irq_to_desc(i);
697 desc->pending_mask = cpumask_of_cpu(0);
Ashok Raj54d5d422005-09-06 15:16:15 -0700698 set_pending_irq(i, cpumask_of_cpu(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700701 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 for ( ; ; ) {
Nishanth Aravamudan52e6e632005-09-10 00:27:26 -0700703 time_remaining = schedule_timeout_interruptible(time_remaining);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700704 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (time_after(jiffies,
706 prev_balance_time+balanced_irq_interval)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700707 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 do_irq_balance();
709 prev_balance_time = jiffies;
710 time_remaining = balanced_irq_interval;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700711 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 }
714 return 0;
715}
716
717static int __init balanced_irq_init(void)
718{
719 int i;
720 struct cpuinfo_x86 *c;
721 cpumask_t tmp;
722
723 cpus_shift_right(tmp, cpu_online_map, 2);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200724 c = &boot_cpu_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* When not overwritten by the command line ask subarchitecture. */
726 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
727 irqbalance_disabled = NO_BALANCE_IRQ;
728 if (irqbalance_disabled)
729 return 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* disable irqbalance completely if there is only one processor online */
732 if (num_online_cpus() < 2) {
733 irqbalance_disabled = 1;
734 return 0;
735 }
736 /*
737 * Enable physical balance only if more than 1 physical processor
738 * is present
739 */
740 if (smp_num_siblings > 1 && !cpus_empty(tmp))
741 physical_balance = 1;
742
Andrew Morton394e3902006-03-23 03:01:05 -0800743 for_each_online_cpu(i) {
Yinghai Lu0799e432008-08-19 20:49:48 -0700744 irq_cpu_data[i].irq_delta = kzalloc(sizeof(unsigned long) * nr_irqs, GFP_KERNEL);
745 irq_cpu_data[i].last_irq = kzalloc(sizeof(unsigned long) * nr_irqs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
747 printk(KERN_ERR "balanced_irq_init: out of memory");
748 goto failed;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 printk(KERN_INFO "Starting balanced_irq\n");
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200753 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200755 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756failed:
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800757 for_each_possible_cpu(i) {
Jesper Juhl4ae66732005-06-25 14:58:48 -0700758 kfree(irq_cpu_data[i].irq_delta);
Andrew Morton394e3902006-03-23 03:01:05 -0800759 irq_cpu_data[i].irq_delta = NULL;
Jesper Juhl4ae66732005-06-25 14:58:48 -0700760 kfree(irq_cpu_data[i].last_irq);
Andrew Morton394e3902006-03-23 03:01:05 -0800761 irq_cpu_data[i].last_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 return 0;
764}
765
Andrew Mortonc2481cc2007-04-08 16:04:04 -0700766int __devinit irqbalance_disable(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 irqbalance_disabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800769 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772__setup("noirqbalance", irqbalance_disable);
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774late_initcall(balanced_irq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#endif /* CONFIG_IRQBALANCE */
Ashok Raj54d5d422005-09-06 15:16:15 -0700776#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100779void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 unsigned int cfg;
782
783 /*
784 * Wait for idle.
785 */
786 apic_wait_icr_idle();
787 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
788 /*
789 * Send the IPI. The write to APIC_ICR fires this off.
790 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100791 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793#endif /* !CONFIG_SMP */
794
795
796/*
797 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
798 * specific CPU-side IRQs.
799 */
800
801#define MAX_PIRQS 8
802static int pirq_entries [MAX_PIRQS];
803static int pirqs_enabled;
804int skip_ioapic_setup;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static int __init ioapic_pirq_setup(char *str)
807{
808 int i, max;
809 int ints[MAX_PIRQS+1];
810
811 get_options(str, ARRAY_SIZE(ints), ints);
812
813 for (i = 0; i < MAX_PIRQS; i++)
814 pirq_entries[i] = -1;
815
816 pirqs_enabled = 1;
817 apic_printk(APIC_VERBOSE, KERN_INFO
818 "PIRQ redirection, working around broken MP-BIOS.\n");
819 max = MAX_PIRQS;
820 if (ints[0] < MAX_PIRQS)
821 max = ints[0];
822
823 for (i = 0; i < max; i++) {
824 apic_printk(APIC_VERBOSE, KERN_DEBUG
825 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
826 /*
827 * PIRQs are mapped upside down, usually.
828 */
829 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
830 }
831 return 1;
832}
833
834__setup("pirq=", ioapic_pirq_setup);
835
836/*
837 * Find the IRQ entry number of a certain pin.
838 */
839static int find_irq_entry(int apic, int pin, int type)
840{
841 int i;
842
843 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400844 if (mp_irqs[i].mp_irqtype == type &&
845 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
846 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
847 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return i;
849
850 return -1;
851}
852
853/*
854 * Find the pin to which IRQ[irq] (ISA) is connected
855 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800856static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 int i;
859
860 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400861 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300863 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400864 (mp_irqs[i].mp_irqtype == type) &&
865 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400867 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 return -1;
870}
871
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800872static int __init find_isa_irq_apic(int irq, int type)
873{
874 int i;
875
876 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400877 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800878
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300879 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400880 (mp_irqs[i].mp_irqtype == type) &&
881 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800882 break;
883 }
884 if (i < mp_irq_entries) {
885 int apic;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200886 for (apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400887 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800888 return apic;
889 }
890 }
891
892 return -1;
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * Find a specific PCI IRQ entry.
897 * Not an __init, possibly needed by modules
898 */
899static int pin_2_irq(int idx, int apic, int pin);
900
901int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
902{
903 int apic, i, best_guess = -1;
904
905 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
906 "slot:%d, pin:%d.\n", bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400907 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
909 return -1;
910 }
911 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400912 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400915 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
916 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300919 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400920 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400922 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar3de352b2008-07-08 11:14:58 +0200923 int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (!(apic || IO_APIC_IRQ(irq)))
926 continue;
927
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400928 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return irq;
930 /*
931 * Use the first all-but-pin matching entry as a
932 * best-guess fuzzy result for broken mptables.
933 */
934 if (best_guess < 0)
935 best_guess = irq;
936 }
937 }
938 return best_guess;
939}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700940EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942/*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200943 * This function currently is only a helper for the i386 smp boot process where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * we need to reprogram the ioredtbls to cater for the cpus which have come online
945 * so mask in all cases should simply be TARGET_CPUS
946 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700947#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948void __init setup_ioapic_dest(void)
949{
950 int pin, ioapic, irq, irq_entry;
951
952 if (skip_ioapic_setup == 1)
953 return;
954
955 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
956 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
957 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
958 if (irq_entry == -1)
959 continue;
960 irq = pin_2_irq(irq_entry, ioapic, pin);
961 set_ioapic_affinity_irq(irq, TARGET_CPUS);
962 }
963
964 }
965}
Ashok Raj54d5d422005-09-06 15:16:15 -0700966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300968#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * EISA Edge/Level control register, ELCR
971 */
972static int EISA_ELCR(unsigned int irq)
973{
974 if (irq < 16) {
975 unsigned int port = 0x4d0 + (irq >> 3);
976 return (inb(port) >> (irq & 7)) & 1;
977 }
978 apic_printk(APIC_VERBOSE, KERN_INFO
979 "Broken MPtable reports ISA irq %d\n", irq);
980 return 0;
981}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300982#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300984/* ISA interrupts are always polarity zero edge triggered,
985 * when listed as conforming in the MP table. */
986
987#define default_ISA_trigger(idx) (0)
988#define default_ISA_polarity(idx) (0)
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/* EISA interrupts are always polarity zero and can be edge or level
991 * trigger depending on the ELCR value. If an interrupt is listed as
992 * EISA conforming in the MP table, that means its trigger type must
993 * be read in from the ELCR */
994
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400995#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300996#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998/* PCI interrupts are always polarity one level triggered,
999 * when listed as conforming in the MP table. */
1000
1001#define default_PCI_trigger(idx) (1)
1002#define default_PCI_polarity(idx) (1)
1003
1004/* MCA interrupts are always polarity zero level triggered,
1005 * when listed as conforming in the MP table. */
1006
1007#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001008#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Shaohua Li61fd47e2007-11-17 01:05:28 -05001010static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001012 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int polarity;
1014
1015 /*
1016 * Determine IRQ line polarity (high active or low active):
1017 */
Ingo Molnar3de352b2008-07-08 11:14:58 +02001018 switch (mp_irqs[idx].mp_irqflag & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001019 case 0: /* conforms, ie. bus-type dependent polarity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001021 polarity = test_bit(bus, mp_bus_not_pci)?
1022 default_ISA_polarity(idx):
1023 default_PCI_polarity(idx);
1024 break;
1025 }
1026 case 1: /* high active */
1027 {
1028 polarity = 0;
1029 break;
1030 }
1031 case 2: /* reserved */
1032 {
1033 printk(KERN_WARNING "broken BIOS!!\n");
1034 polarity = 1;
1035 break;
1036 }
1037 case 3: /* low active */
1038 {
1039 polarity = 1;
1040 break;
1041 }
1042 default: /* invalid */
1043 {
1044 printk(KERN_WARNING "broken BIOS!!\n");
1045 polarity = 1;
1046 break;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 return polarity;
1050}
1051
1052static int MPBIOS_trigger(int idx)
1053{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001054 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int trigger;
1056
1057 /*
1058 * Determine IRQ trigger mode (edge or level sensitive):
1059 */
Ingo Molnar3de352b2008-07-08 11:14:58 +02001060 switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001061 case 0: /* conforms, ie. bus-type dependent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001063 trigger = test_bit(bus, mp_bus_not_pci)?
1064 default_ISA_trigger(idx):
1065 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001066#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001067 switch (mp_bus_id_to_type[bus]) {
1068 case MP_BUS_ISA: /* ISA pin */
1069 {
1070 /* set before the switch */
1071 break;
1072 }
1073 case MP_BUS_EISA: /* EISA pin */
1074 {
1075 trigger = default_EISA_trigger(idx);
1076 break;
1077 }
1078 case MP_BUS_PCI: /* PCI pin */
1079 {
1080 /* set before the switch */
1081 break;
1082 }
1083 case MP_BUS_MCA: /* MCA pin */
1084 {
1085 trigger = default_MCA_trigger(idx);
1086 break;
1087 }
1088 default:
1089 {
1090 printk(KERN_WARNING "broken BIOS!!\n");
1091 trigger = 1;
1092 break;
1093 }
1094 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001095#endif
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001096 break;
1097 }
1098 case 1: /* edge */
1099 {
1100 trigger = 0;
1101 break;
1102 }
1103 case 2: /* reserved */
1104 {
1105 printk(KERN_WARNING "broken BIOS!!\n");
1106 trigger = 1;
1107 break;
1108 }
1109 case 3: /* level */
1110 {
1111 trigger = 1;
1112 break;
1113 }
1114 default: /* invalid */
1115 {
1116 printk(KERN_WARNING "broken BIOS!!\n");
1117 trigger = 0;
1118 break;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 return trigger;
1122}
1123
1124static inline int irq_polarity(int idx)
1125{
1126 return MPBIOS_polarity(idx);
1127}
1128
1129static inline int irq_trigger(int idx)
1130{
1131 return MPBIOS_trigger(idx);
1132}
1133
1134static int pin_2_irq(int idx, int apic, int pin)
1135{
1136 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001137 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /*
1140 * Debugging check, we are in big trouble if this message pops up!
1141 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001142 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1144
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001145 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001146 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001147 else {
1148 /*
1149 * PCI IRQs are mapped in order
1150 */
1151 i = irq = 0;
1152 while (i < apic)
1153 irq += nr_ioapic_registers[i++];
1154 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001156 /*
1157 * For MPS mode, so far only needed by ES7000 platform
1158 */
1159 if (ioapic_renumber_irq)
1160 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /*
1164 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1165 */
1166 if ((pin >= 16) && (pin <= 23)) {
1167 if (pirq_entries[pin-16] != -1) {
1168 if (!pirq_entries[pin-16]) {
1169 apic_printk(APIC_VERBOSE, KERN_DEBUG
1170 "disabling PIRQ%d\n", pin-16);
1171 } else {
1172 irq = pirq_entries[pin-16];
1173 apic_printk(APIC_VERBOSE, KERN_DEBUG
1174 "using PIRQ%d -> IRQ %d\n",
1175 pin-16, irq);
1176 }
1177 }
1178 }
1179 return irq;
1180}
1181
1182static inline int IO_APIC_irq_trigger(int irq)
1183{
1184 int apic, idx, pin;
1185
1186 for (apic = 0; apic < nr_ioapics; apic++) {
1187 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001188 idx = find_irq_entry(apic, pin, mp_INT);
1189 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return irq_trigger(idx);
1191 }
1192 }
1193 /*
1194 * nonexistent IRQs are edge default
1195 */
1196 return 0;
1197}
1198
1199/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Yinghai Lu301e6192008-08-19 20:50:02 -07001200static u8 irq_vector_init_first __initdata = FIRST_DEVICE_VECTOR;
1201static u8 *irq_vector;
1202
1203static void __init irq_vector_init_work(void *data)
1204{
1205 struct dyn_array *da = data;
1206
1207 u8 *irq_vec;
1208
1209 irq_vec = *da->name;
1210
1211 irq_vec[0] = irq_vector_init_first;
1212}
1213
1214DEFINE_DYN_ARRAY(irq_vector, sizeof(u8), nr_irqs, PAGE_SIZE, irq_vector_init_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001216static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001218 static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001219 int vector, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Yinghai Lu301e6192008-08-19 20:50:02 -07001221 BUG_ON((unsigned)irq >= nr_irqs);
Jan Beulich0a1ad602006-06-26 13:56:43 +02001222
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001223 if (irq_vector[irq] > 0)
1224 return irq_vector[irq];
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001225
Jan Beulich0a1ad602006-06-26 13:56:43 +02001226 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001227 offset = current_offset;
1228next:
1229 vector += 8;
Alan Mayer305b92a2008-04-15 15:36:56 -05001230 if (vector >= first_system_vector) {
Eric W. Biederman8339f002007-01-29 13:19:05 -07001231 offset = (offset + 1) % 8;
1232 vector = FIRST_DEVICE_VECTOR + offset;
1233 }
1234 if (vector == current_vector)
1235 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001236 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -07001237 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001238
1239 current_vector = vector;
1240 current_offset = offset;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001241 irq_vector[irq] = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +02001242
1243 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001246static int assign_irq_vector(int irq)
1247{
1248 unsigned long flags;
1249 int vector;
1250
1251 spin_lock_irqsave(&vector_lock, flags);
1252 vector = __assign_irq_vector(irq);
1253 spin_unlock_irqrestore(&vector_lock, flags);
1254
1255 return vector;
1256}
Glauber Costa3fde6902008-05-28 20:34:19 -07001257
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001258static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260#define IOAPIC_AUTO -1
1261#define IOAPIC_EDGE 0
1262#define IOAPIC_LEVEL 1
1263
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001264static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Yinghai Lu08678b02008-08-19 20:50:05 -07001266 struct irq_desc *desc;
1267
1268 desc = irq_to_desc(irq);
Jan Beulich6ebcc002006-06-26 13:56:46 +02001269 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001270 trigger == IOAPIC_LEVEL) {
Yinghai Lu08678b02008-08-19 20:50:05 -07001271 desc->status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001272 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1273 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001274 } else {
Yinghai Lu08678b02008-08-19 20:50:05 -07001275 desc->status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001276 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1277 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001278 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001279 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282static void __init setup_IO_APIC_irqs(void)
1283{
1284 struct IO_APIC_route_entry entry;
1285 int apic, pin, idx, irq, first_notcon = 1, vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1288
1289 for (apic = 0; apic < nr_ioapics; apic++) {
1290 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1291
1292 /*
1293 * add it to the IO-APIC irq-routing table:
1294 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001295 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 entry.delivery_mode = INT_DELIVERY_MODE;
1298 entry.dest_mode = INT_DEST_MODE;
1299 entry.mask = 0; /* enable IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001300 entry.dest.logical.logical_dest =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 cpu_mask_to_apicid(TARGET_CPUS);
1302
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001303 idx = find_irq_entry(apic, pin, mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (idx == -1) {
1305 if (first_notcon) {
1306 apic_printk(APIC_VERBOSE, KERN_DEBUG
1307 " IO-APIC (apicid-pin) %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001308 mp_ioapics[apic].mp_apicid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 pin);
1310 first_notcon = 0;
1311 } else
1312 apic_printk(APIC_VERBOSE, ", %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001313 mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 continue;
1315 }
1316
Yinghai Lu20d225b2007-10-17 18:04:41 +02001317 if (!first_notcon) {
1318 apic_printk(APIC_VERBOSE, " not connected.\n");
1319 first_notcon = 1;
1320 }
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 entry.trigger = irq_trigger(idx);
1323 entry.polarity = irq_polarity(idx);
1324
1325 if (irq_trigger(idx)) {
1326 entry.trigger = 1;
1327 entry.mask = 1;
1328 }
1329
1330 irq = pin_2_irq(idx, apic, pin);
1331 /*
1332 * skip adding the timer int on secondary nodes, which causes
1333 * a small but painful rift in the time-space continuum
1334 */
1335 if (multi_timer_check(apic, irq))
1336 continue;
1337 else
1338 add_pin_to_irq(irq, apic, pin);
1339
1340 if (!apic && !IO_APIC_IRQ(irq))
1341 continue;
1342
1343 if (IO_APIC_IRQ(irq)) {
1344 vector = assign_irq_vector(irq);
1345 entry.vector = vector;
1346 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (!apic && (irq < 16))
1349 disable_8259A_irq(irq);
1350 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09001351 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 }
1354
1355 if (!first_notcon)
1356 apic_printk(APIC_VERBOSE, " not connected.\n");
1357}
1358
1359/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001360 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001362static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1363 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001367 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /*
1370 * We use logical delivery to get the timer IRQ
1371 * to the first CPU.
1372 */
1373 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001374 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1376 entry.delivery_mode = INT_DELIVERY_MODE;
1377 entry.polarity = 0;
1378 entry.trigger = 0;
1379 entry.vector = vector;
1380
1381 /*
1382 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001383 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Maciej W. Rozyckif0825262008-05-27 21:19:16 +01001385 ioapic_register_intr(0, vector, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /*
1388 * Add it to the IO-APIC irq-routing table:
1389 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001390 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001393
1394__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 int apic, i;
1397 union IO_APIC_reg_00 reg_00;
1398 union IO_APIC_reg_01 reg_01;
1399 union IO_APIC_reg_02 reg_02;
1400 union IO_APIC_reg_03 reg_03;
1401 unsigned long flags;
1402
1403 if (apic_verbosity == APIC_QUIET)
1404 return;
1405
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001406 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 for (i = 0; i < nr_ioapics; i++)
1408 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001409 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /*
1412 * We are a bit conservative about what we expect. We have to
1413 * know about every hardware change ASAP.
1414 */
1415 printk(KERN_INFO "testing the IO APIC.......................\n");
1416
1417 for (apic = 0; apic < nr_ioapics; apic++) {
1418
1419 spin_lock_irqsave(&ioapic_lock, flags);
1420 reg_00.raw = io_apic_read(apic, 0);
1421 reg_01.raw = io_apic_read(apic, 1);
1422 if (reg_01.bits.version >= 0x10)
1423 reg_02.raw = io_apic_read(apic, 2);
1424 if (reg_01.bits.version >= 0x20)
1425 reg_03.raw = io_apic_read(apic, 3);
1426 spin_unlock_irqrestore(&ioapic_lock, flags);
1427
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001428 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1430 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1431 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1432 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1435 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1438 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /*
1441 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1442 * but the value of reg_02 is read as the previous read register
1443 * value, so ignore it if reg_02 == reg_01.
1444 */
1445 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1446 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1447 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
1450 /*
1451 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1452 * or reg_03, but the value of reg_0[23] is read as the previous read
1453 * register value, so ignore it if reg_03 == reg_0[12].
1454 */
1455 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1456 reg_03.raw != reg_01.raw) {
1457 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1458 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
1461 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1462
1463 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1464 " Stat Dest Deli Vect: \n");
1465
1466 for (i = 0; i <= reg_01.bits.entries; i++) {
1467 struct IO_APIC_route_entry entry;
1468
Andi Kleencf4c6a22006-09-26 10:52:30 +02001469 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 printk(KERN_DEBUG " %02x %03X %02X ",
1472 i,
1473 entry.dest.logical.logical_dest,
1474 entry.dest.physical.physical_dest
1475 );
1476
1477 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1478 entry.mask,
1479 entry.trigger,
1480 entry.irr,
1481 entry.polarity,
1482 entry.delivery_status,
1483 entry.dest_mode,
1484 entry.delivery_mode,
1485 entry.vector
1486 );
1487 }
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0799e432008-08-19 20:49:48 -07001490 for (i = 0; i < nr_irqs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 struct irq_pin_list *entry = irq_2_pin + i;
1492 if (entry->pin < 0)
1493 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001494 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 for (;;) {
1496 printk("-> %d:%d", entry->apic, entry->pin);
1497 if (!entry->next)
1498 break;
1499 entry = irq_2_pin + entry->next;
1500 }
1501 printk("\n");
1502 }
1503
1504 printk(KERN_INFO ".................................... done.\n");
1505
1506 return;
1507}
1508
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001509__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 unsigned int v;
1512 int i, j;
1513
1514 if (apic_verbosity == APIC_QUIET)
1515 return;
1516
1517 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1518 for (i = 0; i < 8; i++) {
1519 v = apic_read(base + i*0x10);
1520 for (j = 0; j < 32; j++) {
1521 if (v & (1<<j))
1522 printk("1");
1523 else
1524 printk("0");
1525 }
1526 printk("\n");
1527 }
1528}
1529
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001530__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001533 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (apic_verbosity == APIC_QUIET)
1536 return;
1537
1538 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1539 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001540 v = apic_read(APIC_ID);
Jack Steiner05f2d122008-03-28 14:12:02 -05001541 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001542 GET_APIC_ID(v));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 v = apic_read(APIC_LVR);
1544 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1545 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001546 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 v = apic_read(APIC_TASKPRI);
1549 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1550
1551 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1552 v = apic_read(APIC_ARBPRI);
1553 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1554 v & APIC_ARBPRI_MASK);
1555 v = apic_read(APIC_PROCPRI);
1556 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1557 }
1558
1559 v = apic_read(APIC_EOI);
1560 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1561 v = apic_read(APIC_RRR);
1562 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1563 v = apic_read(APIC_LDR);
1564 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1565 v = apic_read(APIC_DFR);
1566 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1567 v = apic_read(APIC_SPIV);
1568 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1569
1570 printk(KERN_DEBUG "... APIC ISR field:\n");
1571 print_APIC_bitfield(APIC_ISR);
1572 printk(KERN_DEBUG "... APIC TMR field:\n");
1573 print_APIC_bitfield(APIC_TMR);
1574 printk(KERN_DEBUG "... APIC IRR field:\n");
1575 print_APIC_bitfield(APIC_IRR);
1576
1577 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1578 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1579 apic_write(APIC_ESR, 0);
1580 v = apic_read(APIC_ESR);
1581 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1582 }
1583
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001584 icr = apic_icr_read();
1585 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1586 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 v = apic_read(APIC_LVTT);
1589 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1590
1591 if (maxlvt > 3) { /* PC is LVT#4. */
1592 v = apic_read(APIC_LVTPC);
1593 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1594 }
1595 v = apic_read(APIC_LVT0);
1596 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1597 v = apic_read(APIC_LVT1);
1598 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1599
1600 if (maxlvt > 2) { /* ERR is LVT#3. */
1601 v = apic_read(APIC_LVTERR);
1602 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1603 }
1604
1605 v = apic_read(APIC_TMICT);
1606 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1607 v = apic_read(APIC_TMCCT);
1608 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1609 v = apic_read(APIC_TDCR);
1610 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1611 printk("\n");
1612}
1613
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001614__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001616 on_each_cpu(print_local_APIC, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001619__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 unsigned int v;
1622 unsigned long flags;
1623
1624 if (apic_verbosity == APIC_QUIET)
1625 return;
1626
1627 printk(KERN_DEBUG "\nprinting PIC contents\n");
1628
1629 spin_lock_irqsave(&i8259A_lock, flags);
1630
1631 v = inb(0xa1) << 8 | inb(0x21);
1632 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1633
1634 v = inb(0xa0) << 8 | inb(0x20);
1635 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1636
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001637 outb(0x0b, 0xa0);
1638 outb(0x0b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 v = inb(0xa0) << 8 | inb(0x20);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001640 outb(0x0a, 0xa0);
1641 outb(0x0a, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 spin_unlock_irqrestore(&i8259A_lock, flags);
1644
1645 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1646
1647 v = inb(0x4d1) << 8 | inb(0x4d0);
1648 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1649}
1650
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001651__apicdebuginit(int) print_all_ICs(void)
1652{
1653 print_PIC();
1654 print_all_local_APICs();
1655 print_IO_APIC();
1656
1657 return 0;
1658}
1659
1660fs_initcall(print_all_ICs);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663static void __init enable_IO_APIC(void)
1664{
1665 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001666 int i8259_apic, i8259_pin;
1667 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 unsigned long flags;
1669
Yinghai Lu0799e432008-08-19 20:49:48 -07001670 for (i = 0; i < pin_map_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 irq_2_pin[i].pin = -1;
1672 irq_2_pin[i].next = 0;
1673 }
1674 if (!pirqs_enabled)
1675 for (i = 0; i < MAX_PIRQS; i++)
1676 pirq_entries[i] = -1;
1677
1678 /*
1679 * The number of IO-APIC IRQ registers (== #pins):
1680 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001681 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001683 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001685 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1686 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001687 for (apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001688 int pin;
1689 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001690 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001691 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001692 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001693
1694
1695 /* If the interrupt line is enabled and in ExtInt mode
1696 * I have found the pin where the i8259 is connected.
1697 */
1698 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1699 ioapic_i8259.apic = apic;
1700 ioapic_i8259.pin = pin;
1701 goto found_i8259;
1702 }
1703 }
1704 }
1705 found_i8259:
1706 /* Look to see what if the MP table has reported the ExtINT */
1707 /* If we could not find the appropriate pin by looking at the ioapic
1708 * the i8259 probably is not connected the ioapic but give the
1709 * mptable a chance anyway.
1710 */
1711 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1712 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1713 /* Trust the MP table if nothing is setup in the hardware */
1714 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1715 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1716 ioapic_i8259.pin = i8259_pin;
1717 ioapic_i8259.apic = i8259_apic;
1718 }
1719 /* Complain if the MP table and the hardware disagree */
1720 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1721 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1722 {
1723 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
1726 /*
1727 * Do not trust the IO-APIC being empty at bootup
1728 */
1729 clear_IO_APIC();
1730}
1731
1732/*
1733 * Not an __init, needed by the reboot code
1734 */
1735void disable_IO_APIC(void)
1736{
1737 /*
1738 * Clear the IO-APIC before rebooting:
1739 */
1740 clear_IO_APIC();
1741
Eric W. Biederman650927e2005-06-25 14:57:44 -07001742 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001743 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001744 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001745 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001746 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001747 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001748 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001749
1750 memset(&entry, 0, sizeof(entry));
1751 entry.mask = 0; /* Enabled */
1752 entry.trigger = 0; /* Edge */
1753 entry.irr = 0;
1754 entry.polarity = 0; /* High */
1755 entry.delivery_status = 0;
1756 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001757 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001758 entry.vector = 0;
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001759 entry.dest.physical.physical_dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001760
1761 /*
1762 * Add it to the IO-APIC irq-routing table:
1763 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001764 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001765 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001766 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
1769/*
1770 * function to set the IO-APIC physical IDs based on the
1771 * values stored in the MPC table.
1772 *
1773 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1774 */
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776static void __init setup_ioapic_ids_from_mpc(void)
1777{
1778 union IO_APIC_reg_00 reg_00;
1779 physid_mask_t phys_id_present_map;
1780 int apic;
1781 int i;
1782 unsigned char old_id;
1783 unsigned long flags;
1784
Yinghai Lua4dbc342008-07-25 02:14:28 -07001785 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001786 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001789 * Don't check I/O APIC IDs for xAPIC systems. They have
1790 * no meaning without the serial APIC bus.
1791 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001792 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1793 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001794 return;
1795 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * This is broken; anything with a real cpu count has to
1797 * circumvent this idiocy regardless.
1798 */
1799 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1800
1801 /*
1802 * Set the IOAPIC ID to the value stored in the MPC table.
1803 */
1804 for (apic = 0; apic < nr_ioapics; apic++) {
1805
1806 /* Read the register 0 value */
1807 spin_lock_irqsave(&ioapic_lock, flags);
1808 reg_00.raw = io_apic_read(apic, 0);
1809 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001810
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001811 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001813 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001815 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1817 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001818 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /*
1822 * Sanity check, is the ID really free? Every APIC in a
1823 * system must have a unique ID or we get lots of nice
1824 * 'stuck on smp_invalidate_needed IPI wait' messages.
1825 */
1826 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001827 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001829 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 for (i = 0; i < get_physical_broadcast(); i++)
1831 if (!physid_isset(i, phys_id_present_map))
1832 break;
1833 if (i >= get_physical_broadcast())
1834 panic("Max APIC ID exceeded!\n");
1835 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1836 i);
1837 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001838 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 } else {
1840 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001841 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 apic_printk(APIC_VERBOSE, "Setting %d in the "
1843 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001844 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1846 }
1847
1848
1849 /*
1850 * We need to adjust the IRQ routing table
1851 * if the ID changed.
1852 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001853 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001855 if (mp_irqs[i].mp_dstapic == old_id)
1856 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001857 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /*
1860 * Read the right value from the MPC table and
1861 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 apic_printk(APIC_VERBOSE, KERN_INFO
1864 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001865 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001867 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 spin_lock_irqsave(&ioapic_lock, flags);
1869 io_apic_write(apic, 0, reg_00.raw);
1870 spin_unlock_irqrestore(&ioapic_lock, flags);
1871
1872 /*
1873 * Sanity check
1874 */
1875 spin_lock_irqsave(&ioapic_lock, flags);
1876 reg_00.raw = io_apic_read(apic, 0);
1877 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001878 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 printk("could not set ID!\n");
1880 else
1881 apic_printk(APIC_VERBOSE, " ok.\n");
1882 }
1883}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001885int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001886
1887static int __init notimercheck(char *s)
1888{
1889 no_timer_check = 1;
1890 return 1;
1891}
1892__setup("no_timer_check", notimercheck);
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894/*
1895 * There is a nasty bug in some older SMP boards, their mptable lies
1896 * about the timer IRQ. We do the following to work around the situation:
1897 *
1898 * - timer IRQ defaults to IO-APIC IRQ
1899 * - if this function detects that timer IRQs are defunct, then we fall
1900 * back to ISA timer IRQs
1901 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001902static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001905 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Zachary Amsden8542b202006-12-07 02:14:09 +01001907 if (no_timer_check)
1908 return 1;
1909
Ingo Molnar4aae0702007-12-18 18:05:58 +01001910 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 local_irq_enable();
1912 /* Let ten ticks pass... */
1913 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001914 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /*
1917 * Expect a few ticks at least, to be sure some possible
1918 * glue logic does not lock up after one or two first
1919 * ticks in a non-ExtINT mode. Also the local APIC
1920 * might have cached one ExtINT interrupt. Finally, at
1921 * least one tick may be lost due to delays.
1922 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001923 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 1;
1925
1926 return 0;
1927}
1928
1929/*
1930 * In the SMP+IOAPIC case it might happen that there are an unspecified
1931 * number of pending IRQ events unhandled. These cases are very rare,
1932 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1933 * better to do it this way as thus we do not have to be aware of
1934 * 'pending' interrupts in the IRQ path, except at this point.
1935 */
1936/*
1937 * Edge triggered needs to resend any interrupt
1938 * that was delayed but this is now handled in the device
1939 * independent code.
1940 */
1941
1942/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001943 * Startup quirk:
1944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * Starting up a edge-triggered IO-APIC interrupt is
1946 * nasty - we need to make sure that we get the edge.
1947 * If it is already asserted for some reason, we need
1948 * return 1 to indicate that is was pending.
1949 *
1950 * This is not complete - we should be able to fake
1951 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001952 *
1953 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001955static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 int was_pending = 0;
1958 unsigned long flags;
1959
1960 spin_lock_irqsave(&ioapic_lock, flags);
1961 if (irq < 16) {
1962 disable_8259A_irq(irq);
1963 if (i8259A_irq_pending(irq))
1964 was_pending = 1;
1965 }
1966 __unmask_IO_APIC_irq(irq);
1967 spin_unlock_irqrestore(&ioapic_lock, flags);
1968
1969 return was_pending;
1970}
1971
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001972static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001974 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 ack_APIC_irq();
1976}
1977
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001978static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 unsigned long v;
1981 int i;
1982
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001983 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984/*
1985 * It appears there is an erratum which affects at least version 0x11
1986 * of I/O APIC (that's the 82093AA and cores integrated into various
1987 * chipsets). Under certain conditions a level-triggered interrupt is
1988 * erroneously delivered as edge-triggered one but the respective IRR
1989 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1990 * message but it will never arrive and further interrupts are blocked
1991 * from the source. The exact reason is so far unknown, but the
1992 * phenomenon was observed when two consecutive interrupt requests
1993 * from a given source get delivered to the same CPU and the source is
1994 * temporarily disabled in between.
1995 *
1996 * A workaround is to simulate an EOI message manually. We achieve it
1997 * by setting the trigger mode to edge and then to level when the edge
1998 * trigger mode gets detected in the TMR of a local APIC for a
1999 * level-triggered interrupt. We mask the source for the time of the
2000 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2001 * The idea is from Manfred Spraul. --macro
2002 */
Eric W. Biedermanb940d222006-10-08 07:43:46 -06002003 i = irq_vector[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2006
2007 ack_APIC_irq();
2008
2009 if (!(v & (1 << (i & 0x1f)))) {
2010 atomic_inc(&irq_mis_count);
2011 spin_lock(&ioapic_lock);
2012 __mask_and_edge_IO_APIC_irq(irq);
2013 __unmask_and_level_IO_APIC_irq(irq);
2014 spin_unlock(&ioapic_lock);
2015 }
2016}
2017
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002018static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Eric W. Biedermanb940d222006-10-08 07:43:46 -06002020 send_IPI_self(irq_vector[irq]);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002021
2022 return 1;
2023}
2024
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002025static struct irq_chip ioapic_chip __read_mostly = {
2026 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002027 .startup = startup_ioapic_irq,
2028 .mask = mask_IO_APIC_irq,
2029 .unmask = unmask_IO_APIC_irq,
2030 .ack = ack_ioapic_irq,
2031 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002032#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002033 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002034#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002035 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036};
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039static inline void init_IO_APIC_traps(void)
2040{
2041 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002042 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 /*
2045 * NOTE! The local APIC isn't very good at handling
2046 * multiple interrupts at the same interrupt level.
2047 * As the interrupt level is determined by taking the
2048 * vector number and shifting that right by 4, we
2049 * want to spread these out a bit so that they don't
2050 * all fall in the same interrupt level.
2051 *
2052 * Also, we've got to be careful not to trash gate
2053 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2054 */
Yinghai Lu0799e432008-08-19 20:49:48 -07002055 for (irq = 0; irq < nr_irqs ; irq++) {
Akinobu Mitaaddfc662008-04-05 22:39:07 +09002056 if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /*
2058 * Hmm.. We don't have an entry for this,
2059 * so default to an old-fashioned 8259
2060 * interrupt if we can..
2061 */
2062 if (irq < 16)
2063 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002064 else {
2065 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002067 desc->chip = &no_irq_chip;
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070 }
2071}
2072
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002073/*
2074 * The local APIC irq-chip implementation:
2075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002077static void ack_lapic_irq(unsigned int irq)
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002078{
2079 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002082static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 unsigned long v;
2085
2086 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002087 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002090static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002092 unsigned long v;
2093
2094 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002095 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002098static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002099 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002100 .mask = mask_lapic_irq,
2101 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002102 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103};
2104
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002105static void lapic_register_intr(int irq, int vector)
2106{
Yinghai Lu08678b02008-08-19 20:50:05 -07002107 struct irq_desc *desc;
2108
2109 desc = irq_to_desc(irq);
2110 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002111 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2112 "edge");
2113 set_intr_gate(vector, interrupt[irq]);
2114}
2115
Jan Beuliche9427102008-01-30 13:31:24 +01002116static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002119 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * We put the 8259A master into AEOI mode and
2121 * unmask on all local APICs LVT0 as NMI.
2122 *
2123 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2124 * is from Maciej W. Rozycki - so we do not have to EOI from
2125 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2128
Jan Beuliche9427102008-01-30 13:31:24 +01002129 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 apic_printk(APIC_VERBOSE, " done.\n");
2132}
2133
2134/*
2135 * This looks a bit hackish but it's about the only one way of sending
2136 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2137 * not support the ExtINT mode, unfortunately. We need to send these
2138 * cycles as some i82489DX-based boards have glue logic that keeps the
2139 * 8259A interrupt line asserted until INTA. --macro
2140 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002141static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002143 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct IO_APIC_route_entry entry0, entry1;
2145 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002147 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002148 if (pin == -1) {
2149 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002151 }
2152 apic = find_isa_irq_apic(8, mp_INT);
2153 if (apic == -1) {
2154 WARN_ON_ONCE(1);
2155 return;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Andi Kleencf4c6a22006-09-26 10:52:30 +02002158 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002159 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 memset(&entry1, 0, sizeof(entry1));
2162
2163 entry1.dest_mode = 0; /* physical delivery */
2164 entry1.mask = 0; /* unmask IRQ now */
2165 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2166 entry1.delivery_mode = dest_ExtINT;
2167 entry1.polarity = entry0.polarity;
2168 entry1.trigger = 0;
2169 entry1.vector = 0;
2170
Andi Kleencf4c6a22006-09-26 10:52:30 +02002171 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 save_control = CMOS_READ(RTC_CONTROL);
2174 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2175 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2176 RTC_FREQ_SELECT);
2177 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2178
2179 i = 100;
2180 while (i-- > 0) {
2181 mdelay(10);
2182 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2183 i -= 10;
2184 }
2185
2186 CMOS_WRITE(save_control, RTC_CONTROL);
2187 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002188 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Andi Kleencf4c6a22006-09-26 10:52:30 +02002190 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
2193/*
2194 * This code may look a bit paranoid, but it's supposed to cooperate with
2195 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2196 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2197 * fanatically on his truly buggy board.
2198 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002199static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002201 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002202 int no_pin1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int vector;
Ingo Molnar6e908942008-03-21 14:32:36 +01002204 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002205 unsigned long flags;
2206
2207 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002208
Ingo Molnar6e908942008-03-21 14:32:36 +01002209 ver = apic_read(APIC_LVR);
2210 ver = GET_APIC_VERSION(ver);
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /*
2213 * get/set the timer IRQ vector:
2214 */
2215 disable_8259A_irq(0);
2216 vector = assign_irq_vector(0);
2217 set_intr_gate(vector, interrupt[0]);
2218
2219 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002220 * As IRQ0 is to be enabled in the 8259A, the virtual
2221 * wire has to be disabled in the local APIC. Also
2222 * timer interrupts need to be acknowledged manually in
2223 * the 8259A for the i82489DX when using the NMI
2224 * watchdog as that APIC treats NMIs as level-triggered.
2225 * The AEOI mode will finish them in the 8259A
2226 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002228 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002230 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002232 pin1 = find_isa_irq_pin(0, mp_INT);
2233 apic1 = find_isa_irq_apic(0, mp_INT);
2234 pin2 = ioapic_i8259.pin;
2235 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002237 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2238 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2239 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002241 /*
2242 * Some BIOS writers are clueless and report the ExtINTA
2243 * I/O APIC input from the cascaded 8259A as the timer
2244 * interrupt input. So just in case, if only one pin
2245 * was found above, try it both directly and through the
2246 * 8259A.
2247 */
2248 if (pin1 == -1) {
2249 pin1 = pin2;
2250 apic1 = apic2;
2251 no_pin1 = 1;
2252 } else if (pin2 == -1) {
2253 pin2 = pin1;
2254 apic2 = apic1;
2255 }
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (pin1 != -1) {
2258 /*
2259 * Ok, does IRQ0 through the IOAPIC work?
2260 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002261 if (no_pin1) {
2262 add_pin_to_irq(0, apic1, pin1);
2263 setup_timer_IRQ0_pin(apic1, pin1, vector);
2264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 unmask_IO_APIC_irq(0);
2266 if (timer_irq_works()) {
2267 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 setup_nmi();
2269 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002271 if (disable_timer_pin_1 > 0)
2272 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002275 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002276 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002277 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2278 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002280 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2281 "(IRQ0) through the 8259A ...\n");
2282 apic_printk(APIC_QUIET, KERN_INFO
2283 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /*
2285 * legacy devices should be connected to IO APIC #0
2286 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002287 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01002288 setup_timer_IRQ0_pin(apic2, pin2, vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002289 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002290 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002292 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002293 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002295 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002297 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 /*
2302 * Cleanup, just in case ...
2303 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002304 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002305 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002306 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002310 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2311 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002312 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002314 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002316 apic_printk(APIC_QUIET, KERN_INFO
2317 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002319 lapic_register_intr(0, vector);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002320 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 enable_8259A_irq(0);
2322
2323 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002324 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002327 disable_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002328 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002329 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002331 apic_printk(APIC_QUIET, KERN_INFO
2332 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 init_8259A(0);
2335 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002336 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 unlock_ExtINT_logic();
2339
2340 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002341 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002342 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002344 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002346 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002347out:
2348 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
2351/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002352 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2353 * to devices. However there may be an I/O APIC pin available for
2354 * this interrupt regardless. The pin may be left unconnected, but
2355 * typically it will be reused as an ExtINT cascade interrupt for
2356 * the master 8259A. In the MPS case such a pin will normally be
2357 * reported as an ExtINT interrupt in the MP table. With ACPI
2358 * there is no provision for ExtINT interrupts, and in the absence
2359 * of an override it would be treated as an ordinary ISA I/O APIC
2360 * interrupt, that is edge-triggered and unmasked by default. We
2361 * used to do this, but it caused problems on some systems because
2362 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2363 * the same ExtINT cascade interrupt to drive the local APIC of the
2364 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2365 * the I/O APIC in all cases now. No actual device should request
2366 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 */
2368#define PIC_IRQS (1 << PIC_CASCADE_IR)
2369
2370void __init setup_IO_APIC(void)
2371{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002372 int i;
2373
2374 /* Reserve all the system vectors. */
Alan Mayer305b92a2008-04-15 15:36:56 -05002375 for (i = first_system_vector; i < NR_VECTORS; i++)
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002376 set_bit(i, used_vectors);
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 enable_IO_APIC();
2379
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002380 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 printk("ENABLING IO-APIC IRQs\n");
2383
2384 /*
2385 * Set up IO-APIC IRQ routing.
2386 */
2387 if (!acpi_ioapic)
2388 setup_ioapic_ids_from_mpc();
2389 sync_Arb_IDs();
2390 setup_IO_APIC_irqs();
2391 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002392 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
2395/*
2396 * Called after all the initialization is done. If we didnt find any
2397 * APIC bugs then we can allow the modify fast path
2398 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400static int __init io_apic_bug_finalize(void)
2401{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002402 if (sis_apic_bug == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 sis_apic_bug = 0;
2404 return 0;
2405}
2406
2407late_initcall(io_apic_bug_finalize);
2408
2409struct sysfs_ioapic_data {
2410 struct sys_device dev;
2411 struct IO_APIC_route_entry entry[0];
2412};
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002413static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Pavel Machek438510f2005-04-16 15:25:24 -07002415static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 struct IO_APIC_route_entry *entry;
2418 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 data = container_of(dev, struct sysfs_ioapic_data, dev);
2422 entry = data->entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002423 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002424 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 return 0;
2427}
2428
2429static int ioapic_resume(struct sys_device *dev)
2430{
2431 struct IO_APIC_route_entry *entry;
2432 struct sysfs_ioapic_data *data;
2433 unsigned long flags;
2434 union IO_APIC_reg_00 reg_00;
2435 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 data = container_of(dev, struct sysfs_ioapic_data, dev);
2438 entry = data->entry;
2439
2440 spin_lock_irqsave(&ioapic_lock, flags);
2441 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002442 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2443 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 io_apic_write(dev->id, 0, reg_00.raw);
2445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002447 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002448 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 return 0;
2451}
2452
2453static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002454 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .suspend = ioapic_suspend,
2456 .resume = ioapic_resume,
2457};
2458
2459static int __init ioapic_init_sysfs(void)
2460{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002461 struct sys_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 int i, size, error = 0;
2463
2464 error = sysdev_class_register(&ioapic_sysdev_class);
2465 if (error)
2466 return error;
2467
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002468 for (i = 0; i < nr_ioapics; i++) {
2469 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002471 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (!mp_ioapic_data[i]) {
2473 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2474 continue;
2475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002477 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 dev->cls = &ioapic_sysdev_class;
2479 error = sysdev_register(dev);
2480 if (error) {
2481 kfree(mp_ioapic_data[i]);
2482 mp_ioapic_data[i] = NULL;
2483 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2484 continue;
2485 }
2486 }
2487
2488 return 0;
2489}
2490
2491device_initcall(ioapic_init_sysfs);
2492
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002493/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002494 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002495 */
2496int create_irq(void)
2497{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002498 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002499 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002500 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002501
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002502 irq = -ENOSPC;
2503 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu0799e432008-08-19 20:49:48 -07002504 for (new = (nr_irqs - 1); new >= 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002505 if (platform_legacy_irq(new))
2506 continue;
2507 if (irq_vector[new] != 0)
2508 continue;
2509 vector = __assign_irq_vector(new);
2510 if (likely(vector > 0))
2511 irq = new;
2512 break;
2513 }
2514 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002515
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002516 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002517 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002518 dynamic_irq_init(irq);
2519 }
2520 return irq;
2521}
2522
2523void destroy_irq(unsigned int irq)
2524{
2525 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002526
2527 dynamic_irq_cleanup(irq);
2528
2529 spin_lock_irqsave(&vector_lock, flags);
PJ Waskiewicz9d9ad4b2008-04-25 17:58:52 -07002530 clear_bit(irq_vector[irq], used_vectors);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002531 irq_vector[irq] = 0;
2532 spin_unlock_irqrestore(&vector_lock, flags);
2533}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002534
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002535/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002536 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002537 */
2538#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002539static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002540{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002541 int vector;
2542 unsigned dest;
2543
2544 vector = assign_irq_vector(irq);
2545 if (vector >= 0) {
2546 dest = cpu_mask_to_apicid(TARGET_CPUS);
2547
2548 msg->address_hi = MSI_ADDR_BASE_HI;
2549 msg->address_lo =
2550 MSI_ADDR_BASE_LO |
2551 ((INT_DEST_MODE == 0) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002552MSI_ADDR_DEST_MODE_PHYSICAL:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002553 MSI_ADDR_DEST_MODE_LOGICAL) |
2554 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2555 MSI_ADDR_REDIRECTION_CPU:
2556 MSI_ADDR_REDIRECTION_LOWPRI) |
2557 MSI_ADDR_DEST_ID(dest);
2558
2559 msg->data =
2560 MSI_DATA_TRIGGER_EDGE |
2561 MSI_DATA_LEVEL_ASSERT |
2562 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002563MSI_DATA_DELIVERY_FIXED:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002564 MSI_DATA_DELIVERY_LOWPRI) |
2565 MSI_DATA_VECTOR(vector);
2566 }
2567 return vector;
2568}
2569
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002570#ifdef CONFIG_SMP
2571static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2572{
2573 struct msi_msg msg;
2574 unsigned int dest;
2575 cpumask_t tmp;
2576 int vector;
Yinghai Lu08678b02008-08-19 20:50:05 -07002577 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002578
2579 cpus_and(tmp, mask, cpu_online_map);
2580 if (cpus_empty(tmp))
2581 tmp = TARGET_CPUS;
2582
2583 vector = assign_irq_vector(irq);
2584 if (vector < 0)
2585 return;
2586
2587 dest = cpu_mask_to_apicid(mask);
2588
2589 read_msi_msg(irq, &msg);
2590
2591 msg.data &= ~MSI_DATA_VECTOR_MASK;
2592 msg.data |= MSI_DATA_VECTOR(vector);
2593 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2594 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2595
2596 write_msi_msg(irq, &msg);
Yinghai Lu08678b02008-08-19 20:50:05 -07002597 desc = irq_to_desc(irq);
2598 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002599}
2600#endif /* CONFIG_SMP */
2601
2602/*
2603 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2604 * which implement the MSI or MSI-X Capability Structure.
2605 */
2606static struct irq_chip msi_chip = {
2607 .name = "PCI-MSI",
2608 .unmask = unmask_msi_irq,
2609 .mask = mask_msi_irq,
2610 .ack = ack_ioapic_irq,
2611#ifdef CONFIG_SMP
2612 .set_affinity = set_msi_irq_affinity,
2613#endif
2614 .retrigger = ioapic_retrigger_irq,
2615};
2616
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002617int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002618{
2619 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002620 int irq, ret;
2621 irq = create_irq();
2622 if (irq < 0)
2623 return irq;
2624
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002625 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002626 if (ret < 0) {
2627 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002628 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002629 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002630
Michael Ellerman7fe37302007-04-18 19:39:21 +10002631 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002632 write_msi_msg(irq, &msg);
2633
Ingo Molnara460e742006-10-17 00:10:03 -07002634 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2635 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002636
Michael Ellerman7fe37302007-04-18 19:39:21 +10002637 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002638}
2639
2640void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002641{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002642 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002643}
2644
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002645#endif /* CONFIG_PCI_MSI */
2646
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002647/*
2648 * Hypertransport interrupt support
2649 */
2650#ifdef CONFIG_HT_IRQ
2651
2652#ifdef CONFIG_SMP
2653
2654static void target_ht_irq(unsigned int irq, unsigned int dest)
2655{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002656 struct ht_irq_msg msg;
2657 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002658
Eric W. Biedermanec683072006-11-08 17:44:57 -08002659 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2660 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002661
Eric W. Biedermanec683072006-11-08 17:44:57 -08002662 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2663 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002664
Eric W. Biedermanec683072006-11-08 17:44:57 -08002665 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002666}
2667
2668static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2669{
2670 unsigned int dest;
2671 cpumask_t tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -07002672 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002673
2674 cpus_and(tmp, mask, cpu_online_map);
2675 if (cpus_empty(tmp))
2676 tmp = TARGET_CPUS;
2677
2678 cpus_and(mask, tmp, CPU_MASK_ALL);
2679
2680 dest = cpu_mask_to_apicid(mask);
2681
2682 target_ht_irq(irq, dest);
Yinghai Lu08678b02008-08-19 20:50:05 -07002683 desc = irq_to_desc(irq);
2684 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002685}
2686#endif
2687
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002688static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002689 .name = "PCI-HT",
2690 .mask = mask_ht_irq,
2691 .unmask = unmask_ht_irq,
2692 .ack = ack_ioapic_irq,
2693#ifdef CONFIG_SMP
2694 .set_affinity = set_ht_irq_affinity,
2695#endif
2696 .retrigger = ioapic_retrigger_irq,
2697};
2698
2699int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2700{
2701 int vector;
2702
2703 vector = assign_irq_vector(irq);
2704 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002705 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002706 unsigned dest;
2707 cpumask_t tmp;
2708
2709 cpus_clear(tmp);
2710 cpu_set(vector >> 8, tmp);
2711 dest = cpu_mask_to_apicid(tmp);
2712
Eric W. Biedermanec683072006-11-08 17:44:57 -08002713 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002714
Eric W. Biedermanec683072006-11-08 17:44:57 -08002715 msg.address_lo =
2716 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002717 HT_IRQ_LOW_DEST_ID(dest) |
2718 HT_IRQ_LOW_VECTOR(vector) |
2719 ((INT_DEST_MODE == 0) ?
2720 HT_IRQ_LOW_DM_PHYSICAL :
2721 HT_IRQ_LOW_DM_LOGICAL) |
2722 HT_IRQ_LOW_RQEOI_EDGE |
2723 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2724 HT_IRQ_LOW_MT_FIXED :
2725 HT_IRQ_LOW_MT_ARBITRATED) |
2726 HT_IRQ_LOW_IRQ_MASKED;
2727
Eric W. Biedermanec683072006-11-08 17:44:57 -08002728 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002729
Ingo Molnara460e742006-10-17 00:10:03 -07002730 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2731 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002732 }
2733 return vector;
2734}
2735#endif /* CONFIG_HT_IRQ */
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737/* --------------------------------------------------------------------------
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002738 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 -------------------------------------------------------------------------- */
2740
Len Brown888ba6c2005-08-24 12:07:20 -04002741#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002743int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 union IO_APIC_reg_00 reg_00;
2746 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2747 physid_mask_t tmp;
2748 unsigned long flags;
2749 int i = 0;
2750
2751 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002752 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2753 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2757 * advantage of new APIC bus architecture.
2758 */
2759
2760 if (physids_empty(apic_id_map))
2761 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2762
2763 spin_lock_irqsave(&ioapic_lock, flags);
2764 reg_00.raw = io_apic_read(ioapic, 0);
2765 spin_unlock_irqrestore(&ioapic_lock, flags);
2766
2767 if (apic_id >= get_physical_broadcast()) {
2768 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2769 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2770 apic_id = reg_00.bits.ID;
2771 }
2772
2773 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002774 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 * 'stuck on smp_invalidate_needed IPI wait' messages.
2776 */
2777 if (check_apicid_used(apic_id_map, apic_id)) {
2778
2779 for (i = 0; i < get_physical_broadcast(); i++) {
2780 if (!check_apicid_used(apic_id_map, i))
2781 break;
2782 }
2783
2784 if (i == get_physical_broadcast())
2785 panic("Max apic_id exceeded!\n");
2786
2787 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2788 "trying %d\n", ioapic, apic_id, i);
2789
2790 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 tmp = apicid_to_cpu_present(apic_id);
2794 physids_or(apic_id_map, apic_id_map, tmp);
2795
2796 if (reg_00.bits.ID != apic_id) {
2797 reg_00.bits.ID = apic_id;
2798
2799 spin_lock_irqsave(&ioapic_lock, flags);
2800 io_apic_write(ioapic, 0, reg_00.raw);
2801 reg_00.raw = io_apic_read(ioapic, 0);
2802 spin_unlock_irqrestore(&ioapic_lock, flags);
2803
2804 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002805 if (reg_00.bits.ID != apic_id) {
2806 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2807 return -1;
2808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
2810
2811 apic_printk(APIC_VERBOSE, KERN_INFO
2812 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2813
2814 return apic_id;
2815}
2816
2817
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002818int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
2820 union IO_APIC_reg_01 reg_01;
2821 unsigned long flags;
2822
2823 spin_lock_irqsave(&ioapic_lock, flags);
2824 reg_01.raw = io_apic_read(ioapic, 1);
2825 spin_unlock_irqrestore(&ioapic_lock, flags);
2826
2827 return reg_01.bits.version;
2828}
2829
2830
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002831int __init io_apic_get_redir_entries(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 union IO_APIC_reg_01 reg_01;
2834 unsigned long flags;
2835
2836 spin_lock_irqsave(&ioapic_lock, flags);
2837 reg_01.raw = io_apic_read(ioapic, 1);
2838 spin_unlock_irqrestore(&ioapic_lock, flags);
2839
2840 return reg_01.bits.entries;
2841}
2842
2843
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002844int 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 -07002845{
2846 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 if (!IO_APIC_IRQ(irq)) {
2849 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2850 ioapic);
2851 return -EINVAL;
2852 }
2853
2854 /*
2855 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2856 * Note that we mask (disable) IRQs now -- these get enabled when the
2857 * corresponding device driver registers for this IRQ.
2858 */
2859
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002860 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 entry.delivery_mode = INT_DELIVERY_MODE;
2863 entry.dest_mode = INT_DEST_MODE;
2864 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2865 entry.trigger = edge_level;
2866 entry.polarity = active_high_low;
2867 entry.mask = 1;
2868
2869 /*
2870 * IRQs < 16 are already in the irq_2_pin[] map
2871 */
2872 if (irq >= 16)
2873 add_pin_to_irq(irq, ioapic, pin);
2874
2875 entry.vector = assign_irq_vector(irq);
2876
2877 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2878 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002879 mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 edge_level, active_high_low);
2881
2882 ioapic_register_intr(irq, entry.vector, edge_level);
2883
2884 if (!ioapic && (irq < 16))
2885 disable_8259A_irq(irq);
2886
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002887 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 return 0;
2890}
2891
Shaohua Li61fd47e2007-11-17 01:05:28 -05002892int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2893{
2894 int i;
2895
2896 if (skip_ioapic_setup)
2897 return -1;
2898
2899 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002900 if (mp_irqs[i].mp_irqtype == mp_INT &&
2901 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002902 break;
2903 if (i >= mp_irq_entries)
2904 return -1;
2905
2906 *trigger = irq_trigger(i);
2907 *polarity = irq_polarity(i);
2908 return 0;
2909}
2910
Len Brown888ba6c2005-08-24 12:07:20 -04002911#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002912
2913static int __init parse_disable_timer_pin_1(char *arg)
2914{
2915 disable_timer_pin_1 = 1;
2916 return 0;
2917}
2918early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2919
2920static int __init parse_enable_timer_pin_1(char *arg)
2921{
2922 disable_timer_pin_1 = -1;
2923 return 0;
2924}
2925early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2926
2927static int __init parse_noapic(char *arg)
2928{
2929 /* disable IO-APIC */
2930 disable_ioapic_setup();
2931 return 0;
2932}
2933early_param("noapic", parse_noapic);
Yinghai Luf3294a32008-06-27 01:41:56 -07002934
2935void __init ioapic_init_mappings(void)
2936{
2937 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2938 int i;
2939
2940 for (i = 0; i < nr_ioapics; i++) {
2941 if (smp_found_config) {
2942 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2943 if (!ioapic_phys) {
2944 printk(KERN_ERR
2945 "WARNING: bogus zero IO-APIC "
2946 "address found in MPTABLE, "
2947 "disabling IO/APIC support!\n");
2948 smp_found_config = 0;
2949 skip_ioapic_setup = 1;
2950 goto fake_ioapic_page;
2951 }
2952 } else {
2953fake_ioapic_page:
2954 ioapic_phys = (unsigned long)
2955 alloc_bootmem_pages(PAGE_SIZE);
2956 ioapic_phys = __pa(ioapic_phys);
2957 }
2958 set_fixmap_nocache(idx, ioapic_phys);
2959 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
2960 __fix_to_virt(idx), ioapic_phys);
2961 idx++;
2962 }
2963}
2964