blob: e03bc0f87eefe8d66cdcf135c571c11dfea4aca7 [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 Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.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. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
49#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070050#include <asm/proto.h>
51#include <asm/acpi.h>
52#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070054#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020055#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070056#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070057#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070058#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070059#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070060#include <asm/hpet.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050061#include <asm/uv/uv_hub.h>
62#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yinghai Lu497c9a12008-08-19 20:50:28 -070064#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020066#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020071 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int sis_apic_bug = -1;
75
Yinghai Luefa25592008-08-19 20:50:36 -070076static DEFINE_SPINLOCK(ioapic_lock);
77static DEFINE_SPINLOCK(vector_lock);
78
Yinghai Luefa25592008-08-19 20:50:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * # of IRQ routing registers
81 */
82int nr_ioapic_registers[MAX_IO_APICS];
83
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040085struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086int nr_ioapics;
87
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040088/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040089struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040090
91/* # of MP IRQ source entries */
92int mp_irq_entries;
93
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040094#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
95int mp_bus_id_to_type[MAX_MP_BUSSES];
96#endif
97
98DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
99
Yinghai Luefa25592008-08-19 20:50:36 -0700100int skip_ioapic_setup;
101
Ingo Molnar54168ed2008-08-20 09:07:45 +0200102static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700103{
104 /* disable IO-APIC */
105 disable_ioapic_setup();
106 return 0;
107}
108early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200109
Yinghai Luda51a822008-08-19 20:50:25 -0700110struct irq_cfg;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700111struct irq_pin_list;
Yinghai Lua1420f32008-08-19 20:50:24 -0700112struct irq_cfg {
Yinghai Luda51a822008-08-19 20:50:25 -0700113 unsigned int irq;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700114 struct irq_pin_list *irq_2_pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700115 cpumask_t domain;
116 cpumask_t old_domain;
117 unsigned move_cleanup_count;
Yinghai Lua1420f32008-08-19 20:50:24 -0700118 u8 vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700119 u8 move_in_progress : 1;
Yinghai Lua1420f32008-08-19 20:50:24 -0700120};
121
Yinghai Lua1420f32008-08-19 20:50:24 -0700122/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
123static struct irq_cfg irq_cfg_legacy[] __initdata = {
Yinghai Lu497c9a12008-08-19 20:50:28 -0700124 [0] = { .irq = 0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
125 [1] = { .irq = 1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
126 [2] = { .irq = 2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
127 [3] = { .irq = 3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
128 [4] = { .irq = 4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
129 [5] = { .irq = 5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
130 [6] = { .irq = 6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
131 [7] = { .irq = 7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
132 [8] = { .irq = 8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
133 [9] = { .irq = 9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
134 [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
135 [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
136 [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
137 [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
138 [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
139 [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
Yinghai Lua1420f32008-08-19 20:50:24 -0700140};
141
Yinghai Luda51a822008-08-19 20:50:25 -0700142static struct irq_cfg irq_cfg_init = { .irq = -1U, };
Yinghai Lua1420f32008-08-19 20:50:24 -0700143
Yinghai Luda51a822008-08-19 20:50:25 -0700144static void init_one_irq_cfg(struct irq_cfg *cfg)
145{
146 memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
Yinghai Lua1420f32008-08-19 20:50:24 -0700147}
148
149static struct irq_cfg *irq_cfgx;
Yinghai Lue89eb432008-08-20 20:46:25 -0700150
Yinghai Luda51a822008-08-19 20:50:25 -0700151static void __init init_work(void *data)
152{
153 struct dyn_array *da = data;
154 struct irq_cfg *cfg;
155 int legacy_count;
156 int i;
157
158 cfg = *da->name;
159
160 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
161
Cyrill Gorcunov676f4a92008-09-04 22:37:49 +0400162 legacy_count = ARRAY_SIZE(irq_cfg_legacy);
Yinghai Luda51a822008-08-19 20:50:25 -0700163 for (i = legacy_count; i < *da->nr; i++)
164 init_one_irq_cfg(&cfg[i]);
Yinghai Luda51a822008-08-19 20:50:25 -0700165}
166
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700167#define for_each_irq_cfg(irq, cfg) \
168 for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
169
170DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
171
172struct irq_cfg *irq_cfg(unsigned int irq)
173{
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200174 if (irq < nr_irqs)
175 return &irq_cfgx[irq];
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700176
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200177 return NULL;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700178}
179struct irq_cfg *irq_cfg_alloc(unsigned int irq)
180{
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200181 return irq_cfg(irq);
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
185 * This is performance-critical, we want to do it O(1)
186 *
187 * the indexing order of this array favors 1:1 mappings
188 * between pins and IRQs.
189 */
190
Yinghai Lu0f978f42008-08-19 20:50:26 -0700191struct irq_pin_list {
192 int apic, pin;
193 struct irq_pin_list *next;
194};
Yinghai Lu301e6192008-08-19 20:50:02 -0700195
Yinghai Lu0f978f42008-08-19 20:50:26 -0700196static struct irq_pin_list *irq_2_pin_head;
197/* fill one page ? */
198static int nr_irq_2_pin = 0x100;
199static struct irq_pin_list *irq_2_pin_ptr;
200static void __init irq_2_pin_init_work(void *data)
201{
202 struct dyn_array *da = data;
203 struct irq_pin_list *pin;
204 int i;
205
206 pin = *da->name;
207
208 for (i = 1; i < *da->nr; i++)
209 pin[i-1].next = &pin[i];
210
211 irq_2_pin_ptr = &pin[0];
212}
213DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
214
215static struct irq_pin_list *get_one_free_irq_2_pin(void)
216{
217 struct irq_pin_list *pin;
218 int i;
219
220 pin = irq_2_pin_ptr;
221
222 if (pin) {
223 irq_2_pin_ptr = pin->next;
224 pin->next = NULL;
225 return pin;
226 }
227
228 /*
229 * we run out of pre-allocate ones, allocate more
230 */
231 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
232
233 if (after_bootmem)
234 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
235 GFP_ATOMIC);
236 else
237 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
238 nr_irq_2_pin, PAGE_SIZE, 0);
239
240 if (!pin)
241 panic("can not get more irq_2_pin\n");
242
243 for (i = 1; i < nr_irq_2_pin; i++)
244 pin[i-1].next = &pin[i];
245
246 irq_2_pin_ptr = pin->next;
247 pin->next = NULL;
248
249 return pin;
250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds130fe052006-11-01 09:11:00 -0800252struct io_apic {
253 unsigned int index;
254 unsigned int unused[3];
255 unsigned int data;
256};
257
258static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
259{
260 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400261 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800262}
263
264static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
265{
266 struct io_apic __iomem *io_apic = io_apic_base(apic);
267 writel(reg, &io_apic->index);
268 return readl(&io_apic->data);
269}
270
271static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
272{
273 struct io_apic __iomem *io_apic = io_apic_base(apic);
274 writel(reg, &io_apic->index);
275 writel(value, &io_apic->data);
276}
277
278/*
279 * Re-write a value: to be used for read-modify-write
280 * cycles where the read already set up the index register.
281 *
282 * Older SiS APIC requires we rewrite the index register
283 */
284static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
285{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200286 struct io_apic __iomem *io_apic = io_apic_base(apic);
287 if (sis_apic_bug)
288 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800289 writel(value, &io_apic->data);
290}
291
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700292static bool io_apic_level_ack_pending(unsigned int irq)
293{
294 struct irq_pin_list *entry;
295 unsigned long flags;
296 struct irq_cfg *cfg = irq_cfg(irq);
297
298 spin_lock_irqsave(&ioapic_lock, flags);
299 entry = cfg->irq_2_pin;
300 for (;;) {
301 unsigned int reg;
302 int pin;
303
304 if (!entry)
305 break;
306 pin = entry->pin;
307 reg = io_apic_read(entry->apic, 0x10 + pin*2);
308 /* Is the remote IRR bit set? */
309 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
310 spin_unlock_irqrestore(&ioapic_lock, flags);
311 return true;
312 }
313 if (!entry->next)
314 break;
315 entry = entry->next;
316 }
317 spin_unlock_irqrestore(&ioapic_lock, flags);
318
319 return false;
320}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700321
Andi Kleencf4c6a22006-09-26 10:52:30 +0200322union entry_union {
323 struct { u32 w1, w2; };
324 struct IO_APIC_route_entry entry;
325};
326
327static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
328{
329 union entry_union eu;
330 unsigned long flags;
331 spin_lock_irqsave(&ioapic_lock, flags);
332 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
333 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
334 spin_unlock_irqrestore(&ioapic_lock, flags);
335 return eu.entry;
336}
337
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800338/*
339 * When we write a new IO APIC routing entry, we need to write the high
340 * word first! If the mask bit in the low word is clear, we will enable
341 * the interrupt, and we need to make sure the entry is fully populated
342 * before that happens.
343 */
Andi Kleend15512f2006-12-07 02:14:07 +0100344static void
345__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
346{
347 union entry_union eu;
348 eu.entry = e;
349 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
350 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
351}
352
Andi Kleencf4c6a22006-09-26 10:52:30 +0200353static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
354{
355 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200356 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100357 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800358 spin_unlock_irqrestore(&ioapic_lock, flags);
359}
360
361/*
362 * When we mask an IO APIC routing entry, we need to write the low
363 * word first, in order to set the mask bit before we change the
364 * high bits!
365 */
366static void ioapic_mask_entry(int apic, int pin)
367{
368 unsigned long flags;
369 union entry_union eu = { .entry.mask = 1 };
370
371 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200372 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
373 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
374 spin_unlock_irqrestore(&ioapic_lock, flags);
375}
376
Yinghai Lu497c9a12008-08-19 20:50:28 -0700377#ifdef CONFIG_SMP
378static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
379{
380 int apic, pin;
381 struct irq_cfg *cfg;
382 struct irq_pin_list *entry;
383
384 cfg = irq_cfg(irq);
385 entry = cfg->irq_2_pin;
386 for (;;) {
387 unsigned int reg;
388
389 if (!entry)
390 break;
391
392 apic = entry->apic;
393 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200394#ifdef CONFIG_INTR_REMAP
395 /*
396 * With interrupt-remapping, destination information comes
397 * from interrupt-remapping table entry.
398 */
399 if (!irq_remapped(irq))
400 io_apic_write(apic, 0x11 + pin*2, dest);
401#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700402 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200403#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700404 reg = io_apic_read(apic, 0x10 + pin*2);
405 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
406 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200407 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700408 if (!entry->next)
409 break;
410 entry = entry->next;
411 }
412}
Yinghai Luefa25592008-08-19 20:50:36 -0700413
414static int assign_irq_vector(int irq, cpumask_t mask);
415
Yinghai Lu497c9a12008-08-19 20:50:28 -0700416static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
417{
418 struct irq_cfg *cfg;
419 unsigned long flags;
420 unsigned int dest;
421 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200422 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700423
Yinghai Lu497c9a12008-08-19 20:50:28 -0700424 cpus_and(tmp, mask, cpu_online_map);
425 if (cpus_empty(tmp))
426 return;
427
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700428 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700429 if (assign_irq_vector(irq, mask))
430 return;
431
432 cpus_and(tmp, cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700433 dest = cpu_mask_to_apicid(tmp);
434 /*
435 * Only the high 8 bits are valid.
436 */
437 dest = SET_APIC_LOGICAL_ID(dest);
438
Ingo Molnar54168ed2008-08-20 09:07:45 +0200439 desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700440 spin_lock_irqsave(&ioapic_lock, flags);
441 __target_IO_APIC_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200442 desc->affinity = mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700443 spin_unlock_irqrestore(&ioapic_lock, flags);
444}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700445#endif /* CONFIG_SMP */
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
448 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
449 * shared ISA-space IRQs, so we have to support them. We are super
450 * fast in the common case, and fast for shared ISA-space IRQs.
451 */
452static void add_pin_to_irq(unsigned int irq, int apic, int pin)
453{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700454 struct irq_cfg *cfg;
455 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Yinghai Lu0f978f42008-08-19 20:50:26 -0700457 /* first time to refer irq_cfg, so with new */
458 cfg = irq_cfg_alloc(irq);
459 entry = cfg->irq_2_pin;
460 if (!entry) {
461 entry = get_one_free_irq_2_pin();
462 cfg->irq_2_pin = entry;
463 entry->apic = apic;
464 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700465 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700467
468 while (entry->next) {
469 /* not again, please */
470 if (entry->apic == apic && entry->pin == pin)
471 return;
472
473 entry = entry->next;
474 }
475
476 entry->next = get_one_free_irq_2_pin();
477 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 entry->apic = apic;
479 entry->pin = pin;
480}
481
482/*
483 * Reroute an IRQ to a different pin.
484 */
485static void __init replace_pin_at_irq(unsigned int irq,
486 int oldapic, int oldpin,
487 int newapic, int newpin)
488{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700489 struct irq_cfg *cfg = irq_cfg(irq);
490 struct irq_pin_list *entry = cfg->irq_2_pin;
491 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Yinghai Lu0f978f42008-08-19 20:50:26 -0700493 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (entry->apic == oldapic && entry->pin == oldpin) {
495 entry->apic = newapic;
496 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700497 replaced = 1;
498 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700500 }
501 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700503
504 /* why? call replace before add? */
505 if (!replaced)
506 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400509static inline void io_apic_modify_irq(unsigned int irq,
510 int mask_and, int mask_or,
511 void (*final)(struct irq_pin_list *entry))
512{
513 int pin;
514 struct irq_cfg *cfg;
515 struct irq_pin_list *entry;
516
517 cfg = irq_cfg(irq);
518 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
519 unsigned int reg;
520 pin = entry->pin;
521 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
522 reg &= mask_and;
523 reg |= mask_or;
524 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
525 if (final)
526 final(entry);
527 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700528}
529
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400530static void __unmask_IO_APIC_irq(unsigned int irq)
531{
532 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
533}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700534
535#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400536void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700537{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400538 /*
539 * Synchronize the IO-APIC and the CPU by doing
540 * a dummy read from the IO-APIC
541 */
542 struct io_apic __iomem *io_apic;
543 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700544 readl(&io_apic->data);
545}
546
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400547static void __mask_IO_APIC_irq(unsigned int irq)
548{
549 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
550}
551#else /* CONFIG_X86_32 */
552static void __mask_IO_APIC_irq(unsigned int irq)
553{
554 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
555}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700556
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400557static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
558{
559 io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
560 IO_APIC_REDIR_MASKED, NULL);
561}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700562
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400563static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
564{
565 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
566 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
567}
568#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700569
Ingo Molnar54168ed2008-08-20 09:07:45 +0200570static void mask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 unsigned long flags;
573
574 spin_lock_irqsave(&ioapic_lock, flags);
575 __mask_IO_APIC_irq(irq);
576 spin_unlock_irqrestore(&ioapic_lock, flags);
577}
578
Ingo Molnar54168ed2008-08-20 09:07:45 +0200579static void unmask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 unsigned long flags;
582
583 spin_lock_irqsave(&ioapic_lock, flags);
584 __unmask_IO_APIC_irq(irq);
585 spin_unlock_irqrestore(&ioapic_lock, flags);
586}
587
588static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
589{
590 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200593 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (entry.delivery_mode == dest_SMI)
595 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Disable it in the IO-APIC irq-routing table:
598 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800599 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Ingo Molnar54168ed2008-08-20 09:07:45 +0200602static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int apic, pin;
605
606 for (apic = 0; apic < nr_ioapics; apic++)
607 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
608 clear_IO_APIC_pin(apic, pin);
609}
610
Ingo Molnar54168ed2008-08-20 09:07:45 +0200611#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100612void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 unsigned int cfg;
615
616 /*
617 * Wait for idle.
618 */
619 apic_wait_icr_idle();
620 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
621 /*
622 * Send the IPI. The write to APIC_ICR fires this off.
623 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100624 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200626#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Ingo Molnar54168ed2008-08-20 09:07:45 +0200628#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
631 * specific CPU-side IRQs.
632 */
633
634#define MAX_PIRQS 8
635static int pirq_entries [MAX_PIRQS];
636static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static int __init ioapic_pirq_setup(char *str)
639{
640 int i, max;
641 int ints[MAX_PIRQS+1];
642
643 get_options(str, ARRAY_SIZE(ints), ints);
644
645 for (i = 0; i < MAX_PIRQS; i++)
646 pirq_entries[i] = -1;
647
648 pirqs_enabled = 1;
649 apic_printk(APIC_VERBOSE, KERN_INFO
650 "PIRQ redirection, working around broken MP-BIOS.\n");
651 max = MAX_PIRQS;
652 if (ints[0] < MAX_PIRQS)
653 max = ints[0];
654
655 for (i = 0; i < max; i++) {
656 apic_printk(APIC_VERBOSE, KERN_DEBUG
657 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
658 /*
659 * PIRQs are mapped upside down, usually.
660 */
661 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
662 }
663 return 1;
664}
665
666__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200667#endif /* CONFIG_X86_32 */
668
669#ifdef CONFIG_INTR_REMAP
670/* I/O APIC RTE contents at the OS boot up */
671static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
672
673/*
674 * Saves and masks all the unmasked IO-APIC RTE's
675 */
676int save_mask_IO_APIC_setup(void)
677{
678 union IO_APIC_reg_01 reg_01;
679 unsigned long flags;
680 int apic, pin;
681
682 /*
683 * The number of IO-APIC IRQ registers (== #pins):
684 */
685 for (apic = 0; apic < nr_ioapics; apic++) {
686 spin_lock_irqsave(&ioapic_lock, flags);
687 reg_01.raw = io_apic_read(apic, 1);
688 spin_unlock_irqrestore(&ioapic_lock, flags);
689 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
690 }
691
692 for (apic = 0; apic < nr_ioapics; apic++) {
693 early_ioapic_entries[apic] =
694 kzalloc(sizeof(struct IO_APIC_route_entry) *
695 nr_ioapic_registers[apic], GFP_KERNEL);
696 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400697 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200698 }
699
700 for (apic = 0; apic < nr_ioapics; apic++)
701 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
702 struct IO_APIC_route_entry entry;
703
704 entry = early_ioapic_entries[apic][pin] =
705 ioapic_read_entry(apic, pin);
706 if (!entry.mask) {
707 entry.mask = 1;
708 ioapic_write_entry(apic, pin, entry);
709 }
710 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400711
Ingo Molnar54168ed2008-08-20 09:07:45 +0200712 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400713
714nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400715 while (apic >= 0)
716 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400717 memset(early_ioapic_entries, 0,
718 ARRAY_SIZE(early_ioapic_entries));
719
720 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200721}
722
723void restore_IO_APIC_setup(void)
724{
725 int apic, pin;
726
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400727 for (apic = 0; apic < nr_ioapics; apic++) {
728 if (!early_ioapic_entries[apic])
729 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200730 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
731 ioapic_write_entry(apic, pin,
732 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400733 kfree(early_ioapic_entries[apic]);
734 early_ioapic_entries[apic] = NULL;
735 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200736}
737
738void reinit_intr_remapped_IO_APIC(int intr_remapping)
739{
740 /*
741 * for now plain restore of previous settings.
742 * TBD: In the case of OS enabling interrupt-remapping,
743 * IO-APIC RTE's need to be setup to point to interrupt-remapping
744 * table entries. for now, do a plain restore, and wait for
745 * the setup_IO_APIC_irqs() to do proper initialization.
746 */
747 restore_IO_APIC_setup();
748}
749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751/*
752 * Find the IRQ entry number of a certain pin.
753 */
754static int find_irq_entry(int apic, int pin, int type)
755{
756 int i;
757
758 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400759 if (mp_irqs[i].mp_irqtype == type &&
760 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
761 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
762 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return i;
764
765 return -1;
766}
767
768/*
769 * Find the pin to which IRQ[irq] (ISA) is connected
770 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800771static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 int i;
774
775 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400776 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300778 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400779 (mp_irqs[i].mp_irqtype == type) &&
780 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400782 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 return -1;
785}
786
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800787static int __init find_isa_irq_apic(int irq, int type)
788{
789 int i;
790
791 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400792 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800793
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300794 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400795 (mp_irqs[i].mp_irqtype == type) &&
796 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800797 break;
798 }
799 if (i < mp_irq_entries) {
800 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200801 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400802 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800803 return apic;
804 }
805 }
806
807 return -1;
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * Find a specific PCI IRQ entry.
812 * Not an __init, possibly needed by modules
813 */
814static int pin_2_irq(int idx, int apic, int pin);
815
816int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
817{
818 int apic, i, best_guess = -1;
819
Ingo Molnar54168ed2008-08-20 09:07:45 +0200820 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
821 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400822 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200823 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -1;
825 }
826 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400827 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400830 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
831 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
833
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300834 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400835 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400837 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200838 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (!(apic || IO_APIC_IRQ(irq)))
841 continue;
842
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400843 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return irq;
845 /*
846 * Use the first all-but-pin matching entry as a
847 * best-guess fuzzy result for broken mptables.
848 */
849 if (best_guess < 0)
850 best_guess = irq;
851 }
852 }
853 return best_guess;
854}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200855
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700856EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300858#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/*
860 * EISA Edge/Level control register, ELCR
861 */
862static int EISA_ELCR(unsigned int irq)
863{
864 if (irq < 16) {
865 unsigned int port = 0x4d0 + (irq >> 3);
866 return (inb(port) >> (irq & 7)) & 1;
867 }
868 apic_printk(APIC_VERBOSE, KERN_INFO
869 "Broken MPtable reports ISA irq %d\n", irq);
870 return 0;
871}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200872
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300875/* ISA interrupts are always polarity zero edge triggered,
876 * when listed as conforming in the MP table. */
877
878#define default_ISA_trigger(idx) (0)
879#define default_ISA_polarity(idx) (0)
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/* EISA interrupts are always polarity zero and can be edge or level
882 * trigger depending on the ELCR value. If an interrupt is listed as
883 * EISA conforming in the MP table, that means its trigger type must
884 * be read in from the ELCR */
885
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400886#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300887#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889/* PCI interrupts are always polarity one level triggered,
890 * when listed as conforming in the MP table. */
891
892#define default_PCI_trigger(idx) (1)
893#define default_PCI_polarity(idx) (1)
894
895/* MCA interrupts are always polarity zero level triggered,
896 * when listed as conforming in the MP table. */
897
898#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300899#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Shaohua Li61fd47e2007-11-17 01:05:28 -0500901static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400903 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int polarity;
905
906 /*
907 * Determine IRQ line polarity (high active or low active):
908 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200909 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200911 case 0: /* conforms, ie. bus-type dependent polarity */
912 if (test_bit(bus, mp_bus_not_pci))
913 polarity = default_ISA_polarity(idx);
914 else
915 polarity = default_PCI_polarity(idx);
916 break;
917 case 1: /* high active */
918 {
919 polarity = 0;
920 break;
921 }
922 case 2: /* reserved */
923 {
924 printk(KERN_WARNING "broken BIOS!!\n");
925 polarity = 1;
926 break;
927 }
928 case 3: /* low active */
929 {
930 polarity = 1;
931 break;
932 }
933 default: /* invalid */
934 {
935 printk(KERN_WARNING "broken BIOS!!\n");
936 polarity = 1;
937 break;
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 return polarity;
941}
942
943static int MPBIOS_trigger(int idx)
944{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400945 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int trigger;
947
948 /*
949 * Determine IRQ trigger mode (edge or level sensitive):
950 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200951 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200953 case 0: /* conforms, ie. bus-type dependent */
954 if (test_bit(bus, mp_bus_not_pci))
955 trigger = default_ISA_trigger(idx);
956 else
957 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300958#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200959 switch (mp_bus_id_to_type[bus]) {
960 case MP_BUS_ISA: /* ISA pin */
961 {
962 /* set before the switch */
963 break;
964 }
965 case MP_BUS_EISA: /* EISA pin */
966 {
967 trigger = default_EISA_trigger(idx);
968 break;
969 }
970 case MP_BUS_PCI: /* PCI pin */
971 {
972 /* set before the switch */
973 break;
974 }
975 case MP_BUS_MCA: /* MCA pin */
976 {
977 trigger = default_MCA_trigger(idx);
978 break;
979 }
980 default:
981 {
982 printk(KERN_WARNING "broken BIOS!!\n");
983 trigger = 1;
984 break;
985 }
986 }
987#endif
988 break;
989 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200990 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200991 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200992 break;
993 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200994 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200995 {
996 printk(KERN_WARNING "broken BIOS!!\n");
997 trigger = 1;
998 break;
999 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001000 case 3: /* level */
1001 {
1002 trigger = 1;
1003 break;
1004 }
1005 default: /* invalid */
1006 {
1007 printk(KERN_WARNING "broken BIOS!!\n");
1008 trigger = 0;
1009 break;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 return trigger;
1013}
1014
1015static inline int irq_polarity(int idx)
1016{
1017 return MPBIOS_polarity(idx);
1018}
1019
1020static inline int irq_trigger(int idx)
1021{
1022 return MPBIOS_trigger(idx);
1023}
1024
Yinghai Luefa25592008-08-19 20:50:36 -07001025int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static int pin_2_irq(int idx, int apic, int pin)
1027{
1028 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001029 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /*
1032 * Debugging check, we are in big trouble if this message pops up!
1033 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001034 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1036
Ingo Molnar54168ed2008-08-20 09:07:45 +02001037 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001038 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001039 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001040 /*
1041 * PCI IRQs are mapped in order
1042 */
1043 i = irq = 0;
1044 while (i < apic)
1045 irq += nr_ioapic_registers[i++];
1046 irq += pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001047 /*
1048 * For MPS mode, so far only needed by ES7000 platform
1049 */
1050 if (ioapic_renumber_irq)
1051 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Ingo Molnar54168ed2008-08-20 09:07:45 +02001054#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1057 */
1058 if ((pin >= 16) && (pin <= 23)) {
1059 if (pirq_entries[pin-16] != -1) {
1060 if (!pirq_entries[pin-16]) {
1061 apic_printk(APIC_VERBOSE, KERN_DEBUG
1062 "disabling PIRQ%d\n", pin-16);
1063 } else {
1064 irq = pirq_entries[pin-16];
1065 apic_printk(APIC_VERBOSE, KERN_DEBUG
1066 "using PIRQ%d -> IRQ %d\n",
1067 pin-16, irq);
1068 }
1069 }
1070 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001071#endif
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return irq;
1074}
1075
Yinghai Lu497c9a12008-08-19 20:50:28 -07001076void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001078 /* Used to the online set of cpus does not change
1079 * during assign_irq_vector.
1080 */
1081 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Yinghai Lu497c9a12008-08-19 20:50:28 -07001084void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001085{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001086 spin_unlock(&vector_lock);
1087}
1088
1089static int __assign_irq_vector(int irq, cpumask_t mask)
1090{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001091 /*
1092 * NOTE! The local APIC isn't very good at handling
1093 * multiple interrupts at the same interrupt level.
1094 * As the interrupt level is determined by taking the
1095 * vector number and shifting that right by 4, we
1096 * want to spread these out a bit so that they don't
1097 * all fall in the same interrupt level.
1098 *
1099 * Also, we've got to be careful not to trash gate
1100 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1101 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001102 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1103 unsigned int old_vector;
1104 int cpu;
1105 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001106
Ingo Molnar54168ed2008-08-20 09:07:45 +02001107 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001108
Ingo Molnar54168ed2008-08-20 09:07:45 +02001109 /* Only try and allocate irqs on cpus that are present */
1110 cpus_and(mask, mask, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001111
Ingo Molnar54168ed2008-08-20 09:07:45 +02001112 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1113 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001114
Ingo Molnar54168ed2008-08-20 09:07:45 +02001115 old_vector = cfg->vector;
1116 if (old_vector) {
1117 cpumask_t tmp;
1118 cpus_and(tmp, cfg->domain, mask);
1119 if (!cpus_empty(tmp))
1120 return 0;
1121 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001122
Ingo Molnar54168ed2008-08-20 09:07:45 +02001123 for_each_cpu_mask_nr(cpu, mask) {
1124 cpumask_t domain, new_mask;
1125 int new_cpu;
1126 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001127
Ingo Molnar54168ed2008-08-20 09:07:45 +02001128 domain = vector_allocation_domain(cpu);
1129 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001130
Ingo Molnar54168ed2008-08-20 09:07:45 +02001131 vector = current_vector;
1132 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001133next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001134 vector += 8;
1135 if (vector >= first_system_vector) {
1136 /* If we run out of vectors on large boxen, must share them. */
1137 offset = (offset + 1) % 8;
1138 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001139 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001140 if (unlikely(current_vector == vector))
1141 continue;
1142#ifdef CONFIG_X86_64
1143 if (vector == IA32_SYSCALL_VECTOR)
1144 goto next;
1145#else
1146 if (vector == SYSCALL_VECTOR)
1147 goto next;
1148#endif
1149 for_each_cpu_mask_nr(new_cpu, new_mask)
1150 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1151 goto next;
1152 /* Found one! */
1153 current_vector = vector;
1154 current_offset = offset;
1155 if (old_vector) {
1156 cfg->move_in_progress = 1;
1157 cfg->old_domain = cfg->domain;
1158 }
1159 for_each_cpu_mask_nr(new_cpu, new_mask)
1160 per_cpu(vector_irq, new_cpu)[vector] = irq;
1161 cfg->vector = vector;
1162 cfg->domain = domain;
1163 return 0;
1164 }
1165 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001166}
1167
1168static int assign_irq_vector(int irq, cpumask_t mask)
1169{
1170 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001171 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001172
1173 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001174 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001175 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001176 return err;
1177}
1178
1179static void __clear_irq_vector(int irq)
1180{
1181 struct irq_cfg *cfg;
1182 cpumask_t mask;
1183 int cpu, vector;
1184
1185 cfg = irq_cfg(irq);
1186 BUG_ON(!cfg->vector);
1187
1188 vector = cfg->vector;
1189 cpus_and(mask, cfg->domain, cpu_online_map);
1190 for_each_cpu_mask_nr(cpu, mask)
1191 per_cpu(vector_irq, cpu)[vector] = -1;
1192
1193 cfg->vector = 0;
1194 cpus_clear(cfg->domain);
1195}
1196
1197void __setup_vector_irq(int cpu)
1198{
1199 /* Initialize vector_irq on a new cpu */
1200 /* This function must be called with vector_lock held */
1201 int irq, vector;
1202 struct irq_cfg *cfg;
1203
1204 /* Mark the inuse vectors */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001205 for_each_irq_cfg(irq, cfg) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001206 if (!cpu_isset(cpu, cfg->domain))
1207 continue;
1208 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001209 per_cpu(vector_irq, cpu)[vector] = irq;
1210 }
1211 /* Mark the free vectors */
1212 for (vector = 0; vector < NR_VECTORS; ++vector) {
1213 irq = per_cpu(vector_irq, cpu)[vector];
1214 if (irq < 0)
1215 continue;
1216
1217 cfg = irq_cfg(irq);
1218 if (!cpu_isset(cpu, cfg->domain))
1219 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001220 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001221}
Glauber Costa3fde6902008-05-28 20:34:19 -07001222
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001223static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001224#ifdef CONFIG_INTR_REMAP
1225static struct irq_chip ir_ioapic_chip;
1226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Ingo Molnar54168ed2008-08-20 09:07:45 +02001228#define IOAPIC_AUTO -1
1229#define IOAPIC_EDGE 0
1230#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001232#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001233static inline int IO_APIC_irq_trigger(int irq)
1234{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001235 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001236
Ingo Molnar54168ed2008-08-20 09:07:45 +02001237 for (apic = 0; apic < nr_ioapics; apic++) {
1238 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1239 idx = find_irq_entry(apic, pin, mp_INT);
1240 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1241 return irq_trigger(idx);
1242 }
1243 }
1244 /*
1245 * nonexistent IRQs are edge default
1246 */
1247 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001248}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001249#else
1250static inline int IO_APIC_irq_trigger(int irq)
1251{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001252 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001253}
1254#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001255
Yinghai Lu497c9a12008-08-19 20:50:28 -07001256static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Yinghai Lu08678b02008-08-19 20:50:05 -07001258 struct irq_desc *desc;
1259
Thomas Gleixneree32c972008-10-15 14:34:09 +02001260 desc = irq_to_desc(irq);
Yinghai Lu199751d2008-08-19 20:50:27 -07001261
Jan Beulich6ebcc002006-06-26 13:56:46 +02001262 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001263 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001264 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001265 else
1266 desc->status &= ~IRQ_LEVEL;
1267
Ingo Molnar54168ed2008-08-20 09:07:45 +02001268#ifdef CONFIG_INTR_REMAP
1269 if (irq_remapped(irq)) {
1270 desc->status |= IRQ_MOVE_PCNTXT;
1271 if (trigger)
1272 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1273 handle_fasteoi_irq,
1274 "fasteoi");
1275 else
1276 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1277 handle_edge_irq, "edge");
1278 return;
1279 }
1280#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001281 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1282 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001283 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001284 handle_fasteoi_irq,
1285 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001286 else
Ingo Molnara460e742006-10-17 00:10:03 -07001287 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001288 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001289}
1290
1291static int setup_ioapic_entry(int apic, int irq,
1292 struct IO_APIC_route_entry *entry,
1293 unsigned int destination, int trigger,
1294 int polarity, int vector)
1295{
1296 /*
1297 * add it to the IO-APIC irq-routing table:
1298 */
1299 memset(entry,0,sizeof(*entry));
1300
Ingo Molnar54168ed2008-08-20 09:07:45 +02001301#ifdef CONFIG_INTR_REMAP
1302 if (intr_remapping_enabled) {
1303 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1304 struct irte irte;
1305 struct IR_IO_APIC_route_entry *ir_entry =
1306 (struct IR_IO_APIC_route_entry *) entry;
1307 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001308
Ingo Molnar54168ed2008-08-20 09:07:45 +02001309 if (!iommu)
1310 panic("No mapping iommu for ioapic %d\n", apic);
1311
1312 index = alloc_irte(iommu, irq, 1);
1313 if (index < 0)
1314 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1315
1316 memset(&irte, 0, sizeof(irte));
1317
1318 irte.present = 1;
1319 irte.dst_mode = INT_DEST_MODE;
1320 irte.trigger_mode = trigger;
1321 irte.dlvry_mode = INT_DELIVERY_MODE;
1322 irte.vector = vector;
1323 irte.dest_id = IRTE_DEST(destination);
1324
1325 modify_irte(irq, &irte);
1326
1327 ir_entry->index2 = (index >> 15) & 0x1;
1328 ir_entry->zero = 0;
1329 ir_entry->format = 1;
1330 ir_entry->index = (index & 0x7fff);
1331 } else
1332#endif
1333 {
1334 entry->delivery_mode = INT_DELIVERY_MODE;
1335 entry->dest_mode = INT_DEST_MODE;
1336 entry->dest = destination;
1337 }
1338
1339 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001340 entry->trigger = trigger;
1341 entry->polarity = polarity;
1342 entry->vector = vector;
1343
1344 /* Mask level triggered irqs.
1345 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1346 */
1347 if (trigger)
1348 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001349 return 0;
1350}
1351
1352static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001353 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001354{
1355 struct irq_cfg *cfg;
1356 struct IO_APIC_route_entry entry;
1357 cpumask_t mask;
1358
1359 if (!IO_APIC_IRQ(irq))
1360 return;
1361
1362 cfg = irq_cfg(irq);
1363
1364 mask = TARGET_CPUS;
1365 if (assign_irq_vector(irq, mask))
1366 return;
1367
1368 cpus_and(mask, cfg->domain, mask);
1369
1370 apic_printk(APIC_VERBOSE,KERN_DEBUG
1371 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1372 "IRQ %d Mode:%i Active:%i)\n",
1373 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1374 irq, trigger, polarity);
1375
1376
1377 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1378 cpu_mask_to_apicid(mask), trigger, polarity,
1379 cfg->vector)) {
1380 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1381 mp_ioapics[apic].mp_apicid, pin);
1382 __clear_irq_vector(irq);
1383 return;
1384 }
1385
1386 ioapic_register_intr(irq, trigger);
1387 if (irq < 16)
1388 disable_8259A_irq(irq);
1389
1390 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393static void __init setup_IO_APIC_irqs(void)
1394{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001395 int apic, pin, idx, irq;
1396 int notcon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1399
1400 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001401 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001403 idx = find_irq_entry(apic, pin, mp_INT);
1404 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001405 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001406 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001407 apic_printk(APIC_VERBOSE,
1408 KERN_DEBUG " %d-%d",
1409 mp_ioapics[apic].mp_apicid,
1410 pin);
1411 } else
1412 apic_printk(APIC_VERBOSE, " %d-%d",
1413 mp_ioapics[apic].mp_apicid,
1414 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001415 continue;
1416 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001417 if (notcon) {
1418 apic_printk(APIC_VERBOSE,
1419 " (apicid-pin) not connected\n");
1420 notcon = 0;
1421 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001422
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001423 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001424#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001425 if (multi_timer_check(apic, irq))
1426 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001427#endif
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001428 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001429
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001430 setup_IO_APIC_irq(apic, pin, irq,
1431 irq_trigger(idx), irq_polarity(idx));
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001435 if (notcon)
1436 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001437 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
1440/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001441 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001443static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1444 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Ingo Molnar54168ed2008-08-20 09:07:45 +02001448#ifdef CONFIG_INTR_REMAP
1449 if (intr_remapping_enabled)
1450 return;
1451#endif
1452
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001453 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /*
1456 * We use logical delivery to get the timer IRQ
1457 * to the first CPU.
1458 */
1459 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001460 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001461 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 entry.delivery_mode = INT_DELIVERY_MODE;
1463 entry.polarity = 0;
1464 entry.trigger = 0;
1465 entry.vector = vector;
1466
1467 /*
1468 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001469 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001471 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /*
1474 * Add it to the IO-APIC irq-routing table:
1475 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001476 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001479
1480__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 int apic, i;
1483 union IO_APIC_reg_00 reg_00;
1484 union IO_APIC_reg_01 reg_01;
1485 union IO_APIC_reg_02 reg_02;
1486 union IO_APIC_reg_03 reg_03;
1487 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001488 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001489 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (apic_verbosity == APIC_QUIET)
1492 return;
1493
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001494 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 for (i = 0; i < nr_ioapics; i++)
1496 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001497 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /*
1500 * We are a bit conservative about what we expect. We have to
1501 * know about every hardware change ASAP.
1502 */
1503 printk(KERN_INFO "testing the IO APIC.......................\n");
1504
1505 for (apic = 0; apic < nr_ioapics; apic++) {
1506
1507 spin_lock_irqsave(&ioapic_lock, flags);
1508 reg_00.raw = io_apic_read(apic, 0);
1509 reg_01.raw = io_apic_read(apic, 1);
1510 if (reg_01.bits.version >= 0x10)
1511 reg_02.raw = io_apic_read(apic, 2);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001512 if (reg_01.bits.version >= 0x20)
1513 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 spin_unlock_irqrestore(&ioapic_lock, flags);
1515
Ingo Molnar54168ed2008-08-20 09:07:45 +02001516 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001517 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1519 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1520 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1521 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Ingo Molnar54168ed2008-08-20 09:07:45 +02001523 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1527 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1531 * but the value of reg_02 is read as the previous read register
1532 * value, so ignore it if reg_02 == reg_01.
1533 */
1534 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1535 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1536 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
1539 /*
1540 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1541 * or reg_03, but the value of reg_0[23] is read as the previous read
1542 * register value, so ignore it if reg_03 == reg_0[12].
1543 */
1544 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1545 reg_03.raw != reg_01.raw) {
1546 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1547 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
1550 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1551
Yinghai Lud83e94a2008-08-19 20:50:33 -07001552 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1553 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 for (i = 0; i <= reg_01.bits.entries; i++) {
1556 struct IO_APIC_route_entry entry;
1557
Andi Kleencf4c6a22006-09-26 10:52:30 +02001558 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ingo Molnar54168ed2008-08-20 09:07:45 +02001560 printk(KERN_DEBUG " %02x %03X ",
1561 i,
1562 entry.dest
1563 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1566 entry.mask,
1567 entry.trigger,
1568 entry.irr,
1569 entry.polarity,
1570 entry.delivery_status,
1571 entry.dest_mode,
1572 entry.delivery_mode,
1573 entry.vector
1574 );
1575 }
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001578 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001579 struct irq_pin_list *entry = cfg->irq_2_pin;
1580 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001582 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 for (;;) {
1584 printk("-> %d:%d", entry->apic, entry->pin);
1585 if (!entry->next)
1586 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001587 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 printk("\n");
1590 }
1591
1592 printk(KERN_INFO ".................................... done.\n");
1593
1594 return;
1595}
1596
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001597__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 unsigned int v;
1600 int i, j;
1601
1602 if (apic_verbosity == APIC_QUIET)
1603 return;
1604
1605 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1606 for (i = 0; i < 8; i++) {
1607 v = apic_read(base + i*0x10);
1608 for (j = 0; j < 32; j++) {
1609 if (v & (1<<j))
1610 printk("1");
1611 else
1612 printk("0");
1613 }
1614 printk("\n");
1615 }
1616}
1617
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001618__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001621 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (apic_verbosity == APIC_QUIET)
1624 return;
1625
1626 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1627 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001628 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001629 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 v = apic_read(APIC_LVR);
1631 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1632 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001633 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 v = apic_read(APIC_TASKPRI);
1636 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1637
Ingo Molnar54168ed2008-08-20 09:07:45 +02001638 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001639 if (!APIC_XAPIC(ver)) {
1640 v = apic_read(APIC_ARBPRI);
1641 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1642 v & APIC_ARBPRI_MASK);
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 v = apic_read(APIC_PROCPRI);
1645 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1646 }
1647
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001648 /*
1649 * Remote read supported only in the 82489DX and local APIC for
1650 * Pentium processors.
1651 */
1652 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1653 v = apic_read(APIC_RRR);
1654 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1655 }
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 v = apic_read(APIC_LDR);
1658 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001659 if (!x2apic_enabled()) {
1660 v = apic_read(APIC_DFR);
1661 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 v = apic_read(APIC_SPIV);
1664 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1665
1666 printk(KERN_DEBUG "... APIC ISR field:\n");
1667 print_APIC_bitfield(APIC_ISR);
1668 printk(KERN_DEBUG "... APIC TMR field:\n");
1669 print_APIC_bitfield(APIC_TMR);
1670 printk(KERN_DEBUG "... APIC IRR field:\n");
1671 print_APIC_bitfield(APIC_IRR);
1672
Ingo Molnar54168ed2008-08-20 09:07:45 +02001673 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1674 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 v = apic_read(APIC_ESR);
1678 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1679 }
1680
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001681 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001682 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1683 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 v = apic_read(APIC_LVTT);
1686 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1687
1688 if (maxlvt > 3) { /* PC is LVT#4. */
1689 v = apic_read(APIC_LVTPC);
1690 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1691 }
1692 v = apic_read(APIC_LVT0);
1693 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1694 v = apic_read(APIC_LVT1);
1695 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1696
1697 if (maxlvt > 2) { /* ERR is LVT#3. */
1698 v = apic_read(APIC_LVTERR);
1699 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1700 }
1701
1702 v = apic_read(APIC_TMICT);
1703 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1704 v = apic_read(APIC_TMCCT);
1705 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1706 v = apic_read(APIC_TDCR);
1707 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1708 printk("\n");
1709}
1710
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001711__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001713 int cpu;
1714
1715 preempt_disable();
1716 for_each_online_cpu(cpu)
1717 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1718 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001721__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 unsigned int v;
1724 unsigned long flags;
1725
1726 if (apic_verbosity == APIC_QUIET)
1727 return;
1728
1729 printk(KERN_DEBUG "\nprinting PIC contents\n");
1730
1731 spin_lock_irqsave(&i8259A_lock, flags);
1732
1733 v = inb(0xa1) << 8 | inb(0x21);
1734 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1735
1736 v = inb(0xa0) << 8 | inb(0x20);
1737 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1738
Ingo Molnar54168ed2008-08-20 09:07:45 +02001739 outb(0x0b,0xa0);
1740 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001742 outb(0x0a,0xa0);
1743 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 spin_unlock_irqrestore(&i8259A_lock, flags);
1746
1747 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1748
1749 v = inb(0x4d1) << 8 | inb(0x4d0);
1750 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1751}
1752
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001753__apicdebuginit(int) print_all_ICs(void)
1754{
1755 print_PIC();
1756 print_all_local_APICs();
1757 print_IO_APIC();
1758
1759 return 0;
1760}
1761
1762fs_initcall(print_all_ICs);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Yinghai Luefa25592008-08-19 20:50:36 -07001765/* Where if anywhere is the i8259 connect in external int mode */
1766static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1767
Ingo Molnar54168ed2008-08-20 09:07:45 +02001768void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001771 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001772 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unsigned long flags;
1774
Ingo Molnar54168ed2008-08-20 09:07:45 +02001775#ifdef CONFIG_X86_32
1776 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!pirqs_enabled)
1778 for (i = 0; i < MAX_PIRQS; i++)
1779 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /*
1783 * The number of IO-APIC IRQ registers (== #pins):
1784 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001785 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001787 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001789 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1790 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001791 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001792 int pin;
1793 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001794 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001795 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001796 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001797
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001798 /* If the interrupt line is enabled and in ExtInt mode
1799 * I have found the pin where the i8259 is connected.
1800 */
1801 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1802 ioapic_i8259.apic = apic;
1803 ioapic_i8259.pin = pin;
1804 goto found_i8259;
1805 }
1806 }
1807 }
1808 found_i8259:
1809 /* Look to see what if the MP table has reported the ExtINT */
1810 /* If we could not find the appropriate pin by looking at the ioapic
1811 * the i8259 probably is not connected the ioapic but give the
1812 * mptable a chance anyway.
1813 */
1814 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1815 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1816 /* Trust the MP table if nothing is setup in the hardware */
1817 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1818 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1819 ioapic_i8259.pin = i8259_pin;
1820 ioapic_i8259.apic = i8259_apic;
1821 }
1822 /* Complain if the MP table and the hardware disagree */
1823 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1824 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1825 {
1826 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828
1829 /*
1830 * Do not trust the IO-APIC being empty at bootup
1831 */
1832 clear_IO_APIC();
1833}
1834
1835/*
1836 * Not an __init, needed by the reboot code
1837 */
1838void disable_IO_APIC(void)
1839{
1840 /*
1841 * Clear the IO-APIC before rebooting:
1842 */
1843 clear_IO_APIC();
1844
Eric W. Biederman650927e2005-06-25 14:57:44 -07001845 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001846 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001847 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001848 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001849 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001850 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001851 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001852
1853 memset(&entry, 0, sizeof(entry));
1854 entry.mask = 0; /* Enabled */
1855 entry.trigger = 0; /* Edge */
1856 entry.irr = 0;
1857 entry.polarity = 0; /* High */
1858 entry.delivery_status = 0;
1859 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001860 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001861 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001862 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001863
1864 /*
1865 * Add it to the IO-APIC irq-routing table:
1866 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001867 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001868 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001869
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001870 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Ingo Molnar54168ed2008-08-20 09:07:45 +02001873#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*
1875 * function to set the IO-APIC physical IDs based on the
1876 * values stored in the MPC table.
1877 *
1878 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1879 */
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881static void __init setup_ioapic_ids_from_mpc(void)
1882{
1883 union IO_APIC_reg_00 reg_00;
1884 physid_mask_t phys_id_present_map;
1885 int apic;
1886 int i;
1887 unsigned char old_id;
1888 unsigned long flags;
1889
Yinghai Lua4dbc342008-07-25 02:14:28 -07001890 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001891 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001894 * Don't check I/O APIC IDs for xAPIC systems. They have
1895 * no meaning without the serial APIC bus.
1896 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001897 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1898 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001899 return;
1900 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * This is broken; anything with a real cpu count has to
1902 * circumvent this idiocy regardless.
1903 */
1904 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1905
1906 /*
1907 * Set the IOAPIC ID to the value stored in the MPC table.
1908 */
1909 for (apic = 0; apic < nr_ioapics; apic++) {
1910
1911 /* Read the register 0 value */
1912 spin_lock_irqsave(&ioapic_lock, flags);
1913 reg_00.raw = io_apic_read(apic, 0);
1914 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001915
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001916 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001918 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001920 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1922 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001923 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /*
1927 * Sanity check, is the ID really free? Every APIC in a
1928 * system must have a unique ID or we get lots of nice
1929 * 'stuck on smp_invalidate_needed IPI wait' messages.
1930 */
1931 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001932 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001934 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 for (i = 0; i < get_physical_broadcast(); i++)
1936 if (!physid_isset(i, phys_id_present_map))
1937 break;
1938 if (i >= get_physical_broadcast())
1939 panic("Max APIC ID exceeded!\n");
1940 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1941 i);
1942 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001943 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 } else {
1945 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001946 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 apic_printk(APIC_VERBOSE, "Setting %d in the "
1948 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001949 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1951 }
1952
1953
1954 /*
1955 * We need to adjust the IRQ routing table
1956 * if the ID changed.
1957 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001958 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001960 if (mp_irqs[i].mp_dstapic == old_id)
1961 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001962 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /*
1965 * Read the right value from the MPC table and
1966 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 apic_printk(APIC_VERBOSE, KERN_INFO
1969 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001970 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001972 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07001974 io_apic_write(apic, 0, reg_00.raw);
1975 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /*
1978 * Sanity check
1979 */
1980 spin_lock_irqsave(&ioapic_lock, flags);
1981 reg_00.raw = io_apic_read(apic, 0);
1982 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001983 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 printk("could not set ID!\n");
1985 else
1986 apic_printk(APIC_VERBOSE, " ok.\n");
1987 }
1988}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001989#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001991int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001992
1993static int __init notimercheck(char *s)
1994{
1995 no_timer_check = 1;
1996 return 1;
1997}
1998__setup("no_timer_check", notimercheck);
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000/*
2001 * There is a nasty bug in some older SMP boards, their mptable lies
2002 * about the timer IRQ. We do the following to work around the situation:
2003 *
2004 * - timer IRQ defaults to IO-APIC IRQ
2005 * - if this function detects that timer IRQs are defunct, then we fall
2006 * back to ISA timer IRQs
2007 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002008static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002011 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Zachary Amsden8542b202006-12-07 02:14:09 +01002013 if (no_timer_check)
2014 return 1;
2015
Ingo Molnar4aae0702007-12-18 18:05:58 +01002016 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 local_irq_enable();
2018 /* Let ten ticks pass... */
2019 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002020 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 /*
2023 * Expect a few ticks at least, to be sure some possible
2024 * glue logic does not lock up after one or two first
2025 * ticks in a non-ExtINT mode. Also the local APIC
2026 * might have cached one ExtINT interrupt. Finally, at
2027 * least one tick may be lost due to delays.
2028 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002029
2030 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002031 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return 0;
2034}
2035
2036/*
2037 * In the SMP+IOAPIC case it might happen that there are an unspecified
2038 * number of pending IRQ events unhandled. These cases are very rare,
2039 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2040 * better to do it this way as thus we do not have to be aware of
2041 * 'pending' interrupts in the IRQ path, except at this point.
2042 */
2043/*
2044 * Edge triggered needs to resend any interrupt
2045 * that was delayed but this is now handled in the device
2046 * independent code.
2047 */
2048
2049/*
2050 * Starting up a edge-triggered IO-APIC interrupt is
2051 * nasty - we need to make sure that we get the edge.
2052 * If it is already asserted for some reason, we need
2053 * return 1 to indicate that is was pending.
2054 *
2055 * This is not complete - we should be able to fake
2056 * an edge even if it isn't on the 8259A...
2057 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002058
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002059static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 int was_pending = 0;
2062 unsigned long flags;
2063
2064 spin_lock_irqsave(&ioapic_lock, flags);
2065 if (irq < 16) {
2066 disable_8259A_irq(irq);
2067 if (i8259A_irq_pending(irq))
2068 was_pending = 1;
2069 }
2070 __unmask_IO_APIC_irq(irq);
2071 spin_unlock_irqrestore(&ioapic_lock, flags);
2072
2073 return was_pending;
2074}
2075
Ingo Molnar54168ed2008-08-20 09:07:45 +02002076#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002077static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002079
2080 struct irq_cfg *cfg = irq_cfg(irq);
2081 unsigned long flags;
2082
2083 spin_lock_irqsave(&vector_lock, flags);
2084 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2085 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002086
2087 return 1;
2088}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002089#else
2090static int ioapic_retrigger_irq(unsigned int irq)
2091{
2092 send_IPI_self(irq_cfg(irq)->vector);
2093
2094 return 1;
2095}
2096#endif
2097
2098/*
2099 * Level and edge triggered IO-APIC interrupts need different handling,
2100 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2101 * handled with the level-triggered descriptor, but that one has slightly
2102 * more overhead. Level-triggered interrupts cannot be handled with the
2103 * edge-triggered handler, without risking IRQ storms and other ugly
2104 * races.
2105 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002106
Yinghai Lu497c9a12008-08-19 20:50:28 -07002107#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002108
2109#ifdef CONFIG_INTR_REMAP
2110static void ir_irq_migration(struct work_struct *work);
2111
2112static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2113
2114/*
2115 * Migrate the IO-APIC irq in the presence of intr-remapping.
2116 *
2117 * For edge triggered, irq migration is a simple atomic update(of vector
2118 * and cpu destination) of IRTE and flush the hardware cache.
2119 *
2120 * For level triggered, we need to modify the io-apic RTE aswell with the update
2121 * vector information, along with modifying IRTE with vector and destination.
2122 * So irq migration for level triggered is little bit more complex compared to
2123 * edge triggered migration. But the good news is, we use the same algorithm
2124 * for level triggered migration as we have today, only difference being,
2125 * we now initiate the irq migration from process context instead of the
2126 * interrupt context.
2127 *
2128 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2129 * suppression) to the IO-APIC, level triggered irq migration will also be
2130 * as simple as edge triggered migration and we can do the irq migration
2131 * with a simple atomic update to IO-APIC RTE.
2132 */
2133static void migrate_ioapic_irq(int irq, cpumask_t mask)
2134{
2135 struct irq_cfg *cfg;
2136 struct irq_desc *desc;
2137 cpumask_t tmp, cleanup_mask;
2138 struct irte irte;
2139 int modify_ioapic_rte;
2140 unsigned int dest;
2141 unsigned long flags;
2142
2143 cpus_and(tmp, mask, cpu_online_map);
2144 if (cpus_empty(tmp))
2145 return;
2146
2147 if (get_irte(irq, &irte))
2148 return;
2149
2150 if (assign_irq_vector(irq, mask))
2151 return;
2152
2153 cfg = irq_cfg(irq);
2154 cpus_and(tmp, cfg->domain, mask);
2155 dest = cpu_mask_to_apicid(tmp);
2156
2157 desc = irq_to_desc(irq);
2158 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2159 if (modify_ioapic_rte) {
2160 spin_lock_irqsave(&ioapic_lock, flags);
2161 __target_IO_APIC_irq(irq, dest, cfg->vector);
2162 spin_unlock_irqrestore(&ioapic_lock, flags);
2163 }
2164
2165 irte.vector = cfg->vector;
2166 irte.dest_id = IRTE_DEST(dest);
2167
2168 /*
2169 * Modified the IRTE and flushes the Interrupt entry cache.
2170 */
2171 modify_irte(irq, &irte);
2172
2173 if (cfg->move_in_progress) {
2174 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2175 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2176 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2177 cfg->move_in_progress = 0;
2178 }
2179
2180 desc->affinity = mask;
2181}
2182
2183static int migrate_irq_remapped_level(int irq)
2184{
2185 int ret = -1;
2186 struct irq_desc *desc = irq_to_desc(irq);
2187
2188 mask_IO_APIC_irq(irq);
2189
2190 if (io_apic_level_ack_pending(irq)) {
2191 /*
2192 * Interrupt in progress. Migrating irq now will change the
2193 * vector information in the IO-APIC RTE and that will confuse
2194 * the EOI broadcast performed by cpu.
2195 * So, delay the irq migration to the next instance.
2196 */
2197 schedule_delayed_work(&ir_migration_work, 1);
2198 goto unmask;
2199 }
2200
2201 /* everthing is clear. we have right of way */
2202 migrate_ioapic_irq(irq, desc->pending_mask);
2203
2204 ret = 0;
2205 desc->status &= ~IRQ_MOVE_PENDING;
2206 cpus_clear(desc->pending_mask);
2207
2208unmask:
2209 unmask_IO_APIC_irq(irq);
2210 return ret;
2211}
2212
2213static void ir_irq_migration(struct work_struct *work)
2214{
2215 unsigned int irq;
2216 struct irq_desc *desc;
2217
2218 for_each_irq_desc(irq, desc) {
2219 if (desc->status & IRQ_MOVE_PENDING) {
2220 unsigned long flags;
2221
2222 spin_lock_irqsave(&desc->lock, flags);
2223 if (!desc->chip->set_affinity ||
2224 !(desc->status & IRQ_MOVE_PENDING)) {
2225 desc->status &= ~IRQ_MOVE_PENDING;
2226 spin_unlock_irqrestore(&desc->lock, flags);
2227 continue;
2228 }
2229
2230 desc->chip->set_affinity(irq, desc->pending_mask);
2231 spin_unlock_irqrestore(&desc->lock, flags);
2232 }
2233 }
2234}
2235
2236/*
2237 * Migrates the IRQ destination in the process context.
2238 */
2239static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2240{
2241 struct irq_desc *desc = irq_to_desc(irq);
2242
2243 if (desc->status & IRQ_LEVEL) {
2244 desc->status |= IRQ_MOVE_PENDING;
2245 desc->pending_mask = mask;
2246 migrate_irq_remapped_level(irq);
2247 return;
2248 }
2249
2250 migrate_ioapic_irq(irq, mask);
2251}
2252#endif
2253
Yinghai Lu497c9a12008-08-19 20:50:28 -07002254asmlinkage void smp_irq_move_cleanup_interrupt(void)
2255{
2256 unsigned vector, me;
2257 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002258#ifdef CONFIG_X86_64
2259 exit_idle();
2260#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002261 irq_enter();
2262
2263 me = smp_processor_id();
2264 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2265 unsigned int irq;
2266 struct irq_desc *desc;
2267 struct irq_cfg *cfg;
2268 irq = __get_cpu_var(vector_irq)[vector];
2269
2270 desc = irq_to_desc(irq);
2271 if (!desc)
2272 continue;
2273
2274 cfg = irq_cfg(irq);
2275 spin_lock(&desc->lock);
2276 if (!cfg->move_cleanup_count)
2277 goto unlock;
2278
2279 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2280 goto unlock;
2281
2282 __get_cpu_var(vector_irq)[vector] = -1;
2283 cfg->move_cleanup_count--;
2284unlock:
2285 spin_unlock(&desc->lock);
2286 }
2287
2288 irq_exit();
2289}
2290
2291static void irq_complete_move(unsigned int irq)
2292{
2293 struct irq_cfg *cfg = irq_cfg(irq);
2294 unsigned vector, me;
2295
2296 if (likely(!cfg->move_in_progress))
2297 return;
2298
2299 vector = ~get_irq_regs()->orig_ax;
2300 me = smp_processor_id();
2301 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2302 cpumask_t cleanup_mask;
2303
2304 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2305 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2306 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2307 cfg->move_in_progress = 0;
2308 }
2309}
2310#else
2311static inline void irq_complete_move(unsigned int irq) {}
2312#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002313#ifdef CONFIG_INTR_REMAP
2314static void ack_x2apic_level(unsigned int irq)
2315{
2316 ack_x2APIC_irq();
2317}
2318
2319static void ack_x2apic_edge(unsigned int irq)
2320{
2321 ack_x2APIC_irq();
2322}
2323#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002324
Yinghai Lu1d025192008-08-19 20:50:34 -07002325static void ack_apic_edge(unsigned int irq)
2326{
2327 irq_complete_move(irq);
2328 move_native_irq(irq);
2329 ack_APIC_irq();
2330}
2331
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002332#ifdef CONFIG_X86_32
2333atomic_t irq_mis_count;
2334#endif
2335
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002336static void ack_apic_level(unsigned int irq)
2337{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002338#ifdef CONFIG_X86_32
2339 unsigned long v;
2340 int i;
2341#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002342 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002343
Ingo Molnar54168ed2008-08-20 09:07:45 +02002344 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002345#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002346 /* If we are moving the irq we need to mask it */
2347 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2348 do_unmask_irq = 1;
2349 mask_IO_APIC_irq(irq);
2350 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002351#endif
2352
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002353#ifdef CONFIG_X86_32
2354 /*
2355 * It appears there is an erratum which affects at least version 0x11
2356 * of I/O APIC (that's the 82093AA and cores integrated into various
2357 * chipsets). Under certain conditions a level-triggered interrupt is
2358 * erroneously delivered as edge-triggered one but the respective IRR
2359 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2360 * message but it will never arrive and further interrupts are blocked
2361 * from the source. The exact reason is so far unknown, but the
2362 * phenomenon was observed when two consecutive interrupt requests
2363 * from a given source get delivered to the same CPU and the source is
2364 * temporarily disabled in between.
2365 *
2366 * A workaround is to simulate an EOI message manually. We achieve it
2367 * by setting the trigger mode to edge and then to level when the edge
2368 * trigger mode gets detected in the TMR of a local APIC for a
2369 * level-triggered interrupt. We mask the source for the time of the
2370 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2371 * The idea is from Manfred Spraul. --macro
2372 */
2373 i = irq_cfg(irq)->vector;
2374
2375 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2376#endif
2377
Ingo Molnar54168ed2008-08-20 09:07:45 +02002378 /*
2379 * We must acknowledge the irq before we move it or the acknowledge will
2380 * not propagate properly.
2381 */
2382 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002383
Ingo Molnar54168ed2008-08-20 09:07:45 +02002384 /* Now we can move and renable the irq */
2385 if (unlikely(do_unmask_irq)) {
2386 /* Only migrate the irq if the ack has been received.
2387 *
2388 * On rare occasions the broadcast level triggered ack gets
2389 * delayed going to ioapics, and if we reprogram the
2390 * vector while Remote IRR is still set the irq will never
2391 * fire again.
2392 *
2393 * To prevent this scenario we read the Remote IRR bit
2394 * of the ioapic. This has two effects.
2395 * - On any sane system the read of the ioapic will
2396 * flush writes (and acks) going to the ioapic from
2397 * this cpu.
2398 * - We get to see if the ACK has actually been delivered.
2399 *
2400 * Based on failed experiments of reprogramming the
2401 * ioapic entry from outside of irq context starting
2402 * with masking the ioapic entry and then polling until
2403 * Remote IRR was clear before reprogramming the
2404 * ioapic I don't trust the Remote IRR bit to be
2405 * completey accurate.
2406 *
2407 * However there appears to be no other way to plug
2408 * this race, so if the Remote IRR bit is not
2409 * accurate and is causing problems then it is a hardware bug
2410 * and you can go talk to the chipset vendor about it.
2411 */
2412 if (!io_apic_level_ack_pending(irq))
2413 move_masked_irq(irq);
2414 unmask_IO_APIC_irq(irq);
2415 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002416
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002417#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002418 if (!(v & (1 << (i & 0x1f)))) {
2419 atomic_inc(&irq_mis_count);
2420 spin_lock(&ioapic_lock);
2421 __mask_and_edge_IO_APIC_irq(irq);
2422 __unmask_and_level_IO_APIC_irq(irq);
2423 spin_unlock(&ioapic_lock);
2424 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002425#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002426}
Yinghai Lu1d025192008-08-19 20:50:34 -07002427
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002428static struct irq_chip ioapic_chip __read_mostly = {
2429 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002430 .startup = startup_ioapic_irq,
2431 .mask = mask_IO_APIC_irq,
2432 .unmask = unmask_IO_APIC_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07002433 .ack = ack_apic_edge,
2434 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002435#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002436 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002437#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002438 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439};
2440
Ingo Molnar54168ed2008-08-20 09:07:45 +02002441#ifdef CONFIG_INTR_REMAP
2442static struct irq_chip ir_ioapic_chip __read_mostly = {
2443 .name = "IR-IO-APIC",
2444 .startup = startup_ioapic_irq,
2445 .mask = mask_IO_APIC_irq,
2446 .unmask = unmask_IO_APIC_irq,
2447 .ack = ack_x2apic_edge,
2448 .eoi = ack_x2apic_level,
2449#ifdef CONFIG_SMP
2450 .set_affinity = set_ir_ioapic_affinity_irq,
2451#endif
2452 .retrigger = ioapic_retrigger_irq,
2453};
2454#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456static inline void init_IO_APIC_traps(void)
2457{
2458 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002459 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002460 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 /*
2463 * NOTE! The local APIC isn't very good at handling
2464 * multiple interrupts at the same interrupt level.
2465 * As the interrupt level is determined by taking the
2466 * vector number and shifting that right by 4, we
2467 * want to spread these out a bit so that they don't
2468 * all fall in the same interrupt level.
2469 *
2470 * Also, we've got to be careful not to trash gate
2471 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2472 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002473 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002474 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /*
2476 * Hmm.. We don't have an entry for this,
2477 * so default to an old-fashioned 8259
2478 * interrupt if we can..
2479 */
2480 if (irq < 16)
2481 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002482 else {
2483 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002485 desc->chip = &no_irq_chip;
2486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488 }
2489}
2490
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002491/*
2492 * The local APIC irq-chip implementation:
2493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002495static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 unsigned long v;
2498
2499 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002500 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002503static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002505 unsigned long v;
2506
2507 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002508 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Ingo Molnar54168ed2008-08-20 09:07:45 +02002511static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002512{
2513 ack_APIC_irq();
2514}
2515
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002516static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002517 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002518 .mask = mask_lapic_irq,
2519 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002520 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521};
2522
Yinghai Lu497c9a12008-08-19 20:50:28 -07002523static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002524{
Yinghai Lu08678b02008-08-19 20:50:05 -07002525 struct irq_desc *desc;
2526
2527 desc = irq_to_desc(irq);
2528 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002529 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2530 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002531}
2532
Jan Beuliche9427102008-01-30 13:31:24 +01002533static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002536 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 * We put the 8259A master into AEOI mode and
2538 * unmask on all local APICs LVT0 as NMI.
2539 *
2540 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2541 * is from Maciej W. Rozycki - so we do not have to EOI from
2542 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2545
Jan Beuliche9427102008-01-30 13:31:24 +01002546 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 apic_printk(APIC_VERBOSE, " done.\n");
2549}
2550
2551/*
2552 * This looks a bit hackish but it's about the only one way of sending
2553 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2554 * not support the ExtINT mode, unfortunately. We need to send these
2555 * cycles as some i82489DX-based boards have glue logic that keeps the
2556 * 8259A interrupt line asserted until INTA. --macro
2557 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002558static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002560 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 struct IO_APIC_route_entry entry0, entry1;
2562 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002564 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002565 if (pin == -1) {
2566 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002568 }
2569 apic = find_isa_irq_apic(8, mp_INT);
2570 if (apic == -1) {
2571 WARN_ON_ONCE(1);
2572 return;
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Andi Kleencf4c6a22006-09-26 10:52:30 +02002575 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002576 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 memset(&entry1, 0, sizeof(entry1));
2579
2580 entry1.dest_mode = 0; /* physical delivery */
2581 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002582 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 entry1.delivery_mode = dest_ExtINT;
2584 entry1.polarity = entry0.polarity;
2585 entry1.trigger = 0;
2586 entry1.vector = 0;
2587
Andi Kleencf4c6a22006-09-26 10:52:30 +02002588 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 save_control = CMOS_READ(RTC_CONTROL);
2591 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2592 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2593 RTC_FREQ_SELECT);
2594 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2595
2596 i = 100;
2597 while (i-- > 0) {
2598 mdelay(10);
2599 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2600 i -= 10;
2601 }
2602
2603 CMOS_WRITE(save_control, RTC_CONTROL);
2604 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002605 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Andi Kleencf4c6a22006-09-26 10:52:30 +02002607 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Yinghai Luefa25592008-08-19 20:50:36 -07002610static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002611/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002612static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002613{
2614 disable_timer_pin_1 = 1;
2615 return 0;
2616}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002617early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002618
2619int timer_through_8259 __initdata;
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621/*
2622 * This code may look a bit paranoid, but it's supposed to cooperate with
2623 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2624 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2625 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002626 *
2627 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002629static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002631 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002632 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002633 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002634 unsigned int ver;
2635 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002636
2637 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002638
Ingo Molnar54168ed2008-08-20 09:07:45 +02002639 ver = apic_read(APIC_LVR);
2640 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /*
2643 * get/set the timer IRQ vector:
2644 */
2645 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002646 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002649 * As IRQ0 is to be enabled in the 8259A, the virtual
2650 * wire has to be disabled in the local APIC. Also
2651 * timer interrupts need to be acknowledged manually in
2652 * the 8259A for the i82489DX when using the NMI
2653 * watchdog as that APIC treats NMIs as level-triggered.
2654 * The AEOI mode will finish them in the 8259A
2655 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002657 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002659#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002660 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002661#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002663 pin1 = find_isa_irq_pin(0, mp_INT);
2664 apic1 = find_isa_irq_apic(0, mp_INT);
2665 pin2 = ioapic_i8259.pin;
2666 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002668 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2669 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002670 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002672 /*
2673 * Some BIOS writers are clueless and report the ExtINTA
2674 * I/O APIC input from the cascaded 8259A as the timer
2675 * interrupt input. So just in case, if only one pin
2676 * was found above, try it both directly and through the
2677 * 8259A.
2678 */
2679 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002680#ifdef CONFIG_INTR_REMAP
2681 if (intr_remapping_enabled)
2682 panic("BIOS bug: timer not connected to IO-APIC");
2683#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002684 pin1 = pin2;
2685 apic1 = apic2;
2686 no_pin1 = 1;
2687 } else if (pin2 == -1) {
2688 pin2 = pin1;
2689 apic2 = apic1;
2690 }
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (pin1 != -1) {
2693 /*
2694 * Ok, does IRQ0 through the IOAPIC work?
2695 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002696 if (no_pin1) {
2697 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002698 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 unmask_IO_APIC_irq(0);
2701 if (timer_irq_works()) {
2702 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 setup_nmi();
2704 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002706 if (disable_timer_pin_1 > 0)
2707 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002710#ifdef CONFIG_INTR_REMAP
2711 if (intr_remapping_enabled)
2712 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2713#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002714 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002715 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002716 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2717 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002719 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2720 "(IRQ0) through the 8259A ...\n");
2721 apic_printk(APIC_QUIET, KERN_INFO
2722 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 /*
2724 * legacy devices should be connected to IO APIC #0
2725 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002726 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002727 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002728 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002729 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002731 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002732 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002734 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002736 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002738 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 /*
2741 * Cleanup, just in case ...
2742 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002743 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002744 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002745 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002749 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2750 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002751 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002753#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002754 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002757 apic_printk(APIC_QUIET, KERN_INFO
2758 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Yinghai Lu497c9a12008-08-19 20:50:28 -07002760 lapic_register_intr(0);
2761 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 enable_8259A_irq(0);
2763
2764 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002765 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002768 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002769 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002770 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002772 apic_printk(APIC_QUIET, KERN_INFO
2773 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 init_8259A(0);
2776 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002777 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 unlock_ExtINT_logic();
2780
2781 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002782 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002783 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002785 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002787 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002788out:
2789 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
2792/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002793 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2794 * to devices. However there may be an I/O APIC pin available for
2795 * this interrupt regardless. The pin may be left unconnected, but
2796 * typically it will be reused as an ExtINT cascade interrupt for
2797 * the master 8259A. In the MPS case such a pin will normally be
2798 * reported as an ExtINT interrupt in the MP table. With ACPI
2799 * there is no provision for ExtINT interrupts, and in the absence
2800 * of an override it would be treated as an ordinary ISA I/O APIC
2801 * interrupt, that is edge-triggered and unmasked by default. We
2802 * used to do this, but it caused problems on some systems because
2803 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2804 * the same ExtINT cascade interrupt to drive the local APIC of the
2805 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2806 * the I/O APIC in all cases now. No actual device should request
2807 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 */
2809#define PIC_IRQS (1 << PIC_CASCADE_IR)
2810
2811void __init setup_IO_APIC(void)
2812{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002813
2814#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002816#else
2817 /*
2818 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2819 */
2820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002822 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Ingo Molnar54168ed2008-08-20 09:07:45 +02002824 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2825 /*
2826 * Set up IO-APIC IRQ routing.
2827 */
2828#ifdef CONFIG_X86_32
2829 if (!acpi_ioapic)
2830 setup_ioapic_ids_from_mpc();
2831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 sync_Arb_IDs();
2833 setup_IO_APIC_irqs();
2834 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002835 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002839 * Called after all the initialization is done. If we didnt find any
2840 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static int __init io_apic_bug_finalize(void)
2844{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002845 if (sis_apic_bug == -1)
2846 sis_apic_bug = 0;
2847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
2850late_initcall(io_apic_bug_finalize);
2851
2852struct sysfs_ioapic_data {
2853 struct sys_device dev;
2854 struct IO_APIC_route_entry entry[0];
2855};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002856static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Pavel Machek438510f2005-04-16 15:25:24 -07002858static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 struct IO_APIC_route_entry *entry;
2861 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 data = container_of(dev, struct sysfs_ioapic_data, dev);
2865 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002866 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2867 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 return 0;
2870}
2871
2872static int ioapic_resume(struct sys_device *dev)
2873{
2874 struct IO_APIC_route_entry *entry;
2875 struct sysfs_ioapic_data *data;
2876 unsigned long flags;
2877 union IO_APIC_reg_00 reg_00;
2878 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 data = container_of(dev, struct sysfs_ioapic_data, dev);
2881 entry = data->entry;
2882
2883 spin_lock_irqsave(&ioapic_lock, flags);
2884 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002885 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2886 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 io_apic_write(dev->id, 0, reg_00.raw);
2888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002890 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002891 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 return 0;
2894}
2895
2896static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002897 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 .suspend = ioapic_suspend,
2899 .resume = ioapic_resume,
2900};
2901
2902static int __init ioapic_init_sysfs(void)
2903{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002904 struct sys_device * dev;
2905 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 error = sysdev_class_register(&ioapic_sysdev_class);
2908 if (error)
2909 return error;
2910
Ingo Molnar54168ed2008-08-20 09:07:45 +02002911 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002912 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002914 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (!mp_ioapic_data[i]) {
2916 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2917 continue;
2918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002920 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 dev->cls = &ioapic_sysdev_class;
2922 error = sysdev_register(dev);
2923 if (error) {
2924 kfree(mp_ioapic_data[i]);
2925 mp_ioapic_data[i] = NULL;
2926 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2927 continue;
2928 }
2929 }
2930
2931 return 0;
2932}
2933
2934device_initcall(ioapic_init_sysfs);
2935
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002936/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002937 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002938 */
Yinghai Lu199751d2008-08-19 20:50:27 -07002939unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002940{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002941 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002942 unsigned int irq;
2943 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002944 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07002945 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002946
Yinghai Lu199751d2008-08-19 20:50:27 -07002947 irq_want = nr_irqs - 1;
2948
2949 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002950 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002951 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002952 if (platform_legacy_irq(new))
2953 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002954 cfg_new = irq_cfg(new);
2955 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002956 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002957 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07002958 if (!cfg_new)
2959 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002960 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002961 irq = new;
2962 break;
2963 }
2964 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002965
Yinghai Lu199751d2008-08-19 20:50:27 -07002966 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002967 dynamic_irq_init(irq);
2968 }
2969 return irq;
2970}
2971
Yinghai Lu199751d2008-08-19 20:50:27 -07002972int create_irq(void)
2973{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002974 int irq;
2975
2976 irq = create_irq_nr(nr_irqs - 1);
2977
2978 if (irq == 0)
2979 irq = -1;
2980
2981 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07002982}
2983
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002984void destroy_irq(unsigned int irq)
2985{
2986 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002987
2988 dynamic_irq_cleanup(irq);
2989
Ingo Molnar54168ed2008-08-20 09:07:45 +02002990#ifdef CONFIG_INTR_REMAP
2991 free_irte(irq);
2992#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002993 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002994 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002995 spin_unlock_irqrestore(&vector_lock, flags);
2996}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002997
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002998/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002999 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003000 */
3001#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003002static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003003{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003004 struct irq_cfg *cfg;
3005 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003006 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003007 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003008
Yinghai Lu497c9a12008-08-19 20:50:28 -07003009 tmp = TARGET_CPUS;
3010 err = assign_irq_vector(irq, tmp);
3011 if (err)
3012 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003013
Yinghai Lu497c9a12008-08-19 20:50:28 -07003014 cfg = irq_cfg(irq);
3015 cpus_and(tmp, cfg->domain, tmp);
3016 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003017
Ingo Molnar54168ed2008-08-20 09:07:45 +02003018#ifdef CONFIG_INTR_REMAP
3019 if (irq_remapped(irq)) {
3020 struct irte irte;
3021 int ir_index;
3022 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003023
Ingo Molnar54168ed2008-08-20 09:07:45 +02003024 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3025 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003026
Ingo Molnar54168ed2008-08-20 09:07:45 +02003027 memset (&irte, 0, sizeof(irte));
3028
3029 irte.present = 1;
3030 irte.dst_mode = INT_DEST_MODE;
3031 irte.trigger_mode = 0; /* edge */
3032 irte.dlvry_mode = INT_DELIVERY_MODE;
3033 irte.vector = cfg->vector;
3034 irte.dest_id = IRTE_DEST(dest);
3035
3036 modify_irte(irq, &irte);
3037
3038 msg->address_hi = MSI_ADDR_BASE_HI;
3039 msg->data = sub_handle;
3040 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3041 MSI_ADDR_IR_SHV |
3042 MSI_ADDR_IR_INDEX1(ir_index) |
3043 MSI_ADDR_IR_INDEX2(ir_index);
3044 } else
3045#endif
3046 {
3047 msg->address_hi = MSI_ADDR_BASE_HI;
3048 msg->address_lo =
3049 MSI_ADDR_BASE_LO |
3050 ((INT_DEST_MODE == 0) ?
3051 MSI_ADDR_DEST_MODE_PHYSICAL:
3052 MSI_ADDR_DEST_MODE_LOGICAL) |
3053 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3054 MSI_ADDR_REDIRECTION_CPU:
3055 MSI_ADDR_REDIRECTION_LOWPRI) |
3056 MSI_ADDR_DEST_ID(dest);
3057
3058 msg->data =
3059 MSI_DATA_TRIGGER_EDGE |
3060 MSI_DATA_LEVEL_ASSERT |
3061 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3062 MSI_DATA_DELIVERY_FIXED:
3063 MSI_DATA_DELIVERY_LOWPRI) |
3064 MSI_DATA_VECTOR(cfg->vector);
3065 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003066 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003067}
3068
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003069#ifdef CONFIG_SMP
3070static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3071{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003072 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003073 struct msi_msg msg;
3074 unsigned int dest;
3075 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003076 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003077
3078 cpus_and(tmp, mask, cpu_online_map);
3079 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003080 return;
3081
Yinghai Lu497c9a12008-08-19 20:50:28 -07003082 if (assign_irq_vector(irq, mask))
3083 return;
3084
3085 cfg = irq_cfg(irq);
3086 cpus_and(tmp, cfg->domain, mask);
3087 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003088
3089 read_msi_msg(irq, &msg);
3090
3091 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003092 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003093 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3094 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3095
3096 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003097 desc = irq_to_desc(irq);
3098 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003099}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003100
3101#ifdef CONFIG_INTR_REMAP
3102/*
3103 * Migrate the MSI irq to another cpumask. This migration is
3104 * done in the process context using interrupt-remapping hardware.
3105 */
3106static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3107{
3108 struct irq_cfg *cfg;
3109 unsigned int dest;
3110 cpumask_t tmp, cleanup_mask;
3111 struct irte irte;
3112 struct irq_desc *desc;
3113
3114 cpus_and(tmp, mask, cpu_online_map);
3115 if (cpus_empty(tmp))
3116 return;
3117
3118 if (get_irte(irq, &irte))
3119 return;
3120
3121 if (assign_irq_vector(irq, mask))
3122 return;
3123
3124 cfg = irq_cfg(irq);
3125 cpus_and(tmp, cfg->domain, mask);
3126 dest = cpu_mask_to_apicid(tmp);
3127
3128 irte.vector = cfg->vector;
3129 irte.dest_id = IRTE_DEST(dest);
3130
3131 /*
3132 * atomically update the IRTE with the new destination and vector.
3133 */
3134 modify_irte(irq, &irte);
3135
3136 /*
3137 * After this point, all the interrupts will start arriving
3138 * at the new destination. So, time to cleanup the previous
3139 * vector allocation.
3140 */
3141 if (cfg->move_in_progress) {
3142 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3143 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3144 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3145 cfg->move_in_progress = 0;
3146 }
3147
3148 desc = irq_to_desc(irq);
3149 desc->affinity = mask;
3150}
3151#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003152#endif /* CONFIG_SMP */
3153
3154/*
3155 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3156 * which implement the MSI or MSI-X Capability Structure.
3157 */
3158static struct irq_chip msi_chip = {
3159 .name = "PCI-MSI",
3160 .unmask = unmask_msi_irq,
3161 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003162 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003163#ifdef CONFIG_SMP
3164 .set_affinity = set_msi_irq_affinity,
3165#endif
3166 .retrigger = ioapic_retrigger_irq,
3167};
3168
Ingo Molnar54168ed2008-08-20 09:07:45 +02003169#ifdef CONFIG_INTR_REMAP
3170static struct irq_chip msi_ir_chip = {
3171 .name = "IR-PCI-MSI",
3172 .unmask = unmask_msi_irq,
3173 .mask = mask_msi_irq,
3174 .ack = ack_x2apic_edge,
3175#ifdef CONFIG_SMP
3176 .set_affinity = ir_set_msi_irq_affinity,
3177#endif
3178 .retrigger = ioapic_retrigger_irq,
3179};
3180
3181/*
3182 * Map the PCI dev to the corresponding remapping hardware unit
3183 * and allocate 'nvec' consecutive interrupt-remapping table entries
3184 * in it.
3185 */
3186static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3187{
3188 struct intel_iommu *iommu;
3189 int index;
3190
3191 iommu = map_dev_to_ir(dev);
3192 if (!iommu) {
3193 printk(KERN_ERR
3194 "Unable to map PCI %s to iommu\n", pci_name(dev));
3195 return -ENOENT;
3196 }
3197
3198 index = alloc_irte(iommu, irq, nvec);
3199 if (index < 0) {
3200 printk(KERN_ERR
3201 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3202 pci_name(dev));
3203 return -ENOSPC;
3204 }
3205 return index;
3206}
3207#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003208
3209static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3210{
3211 int ret;
3212 struct msi_msg msg;
3213
3214 ret = msi_compose_msg(dev, irq, &msg);
3215 if (ret < 0)
3216 return ret;
3217
3218 set_irq_msi(irq, desc);
3219 write_msi_msg(irq, &msg);
3220
Ingo Molnar54168ed2008-08-20 09:07:45 +02003221#ifdef CONFIG_INTR_REMAP
3222 if (irq_remapped(irq)) {
3223 struct irq_desc *desc = irq_to_desc(irq);
3224 /*
3225 * irq migration in process context
3226 */
3227 desc->status |= IRQ_MOVE_PCNTXT;
3228 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3229 } else
3230#endif
3231 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003232
Yinghai Luc81bba42008-09-25 11:53:11 -07003233 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3234
Yinghai Lu1d025192008-08-19 20:50:34 -07003235 return 0;
3236}
3237
Yinghai Lu199751d2008-08-19 20:50:27 -07003238static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3239{
3240 unsigned int irq;
3241
3242 irq = dev->bus->number;
3243 irq <<= 8;
3244 irq |= dev->devfn;
3245 irq <<= 12;
3246
3247 return irq;
3248}
3249
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003250int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003251{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003252 unsigned int irq;
3253 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003254 unsigned int irq_want;
3255
3256 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3257
3258 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003259 if (irq == 0)
3260 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003261
Ingo Molnar54168ed2008-08-20 09:07:45 +02003262#ifdef CONFIG_INTR_REMAP
3263 if (!intr_remapping_enabled)
3264 goto no_ir;
3265
3266 ret = msi_alloc_irte(dev, irq, 1);
3267 if (ret < 0)
3268 goto error;
3269no_ir:
3270#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003271 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003272 if (ret < 0) {
3273 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003274 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003275 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003276 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003277
3278#ifdef CONFIG_INTR_REMAP
3279error:
3280 destroy_irq(irq);
3281 return ret;
3282#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003283}
3284
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003285int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3286{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003287 unsigned int irq;
3288 int ret, sub_handle;
3289 struct msi_desc *desc;
3290 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003291
Ingo Molnar54168ed2008-08-20 09:07:45 +02003292#ifdef CONFIG_INTR_REMAP
3293 struct intel_iommu *iommu = 0;
3294 int index = 0;
3295#endif
3296
3297 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3298 sub_handle = 0;
3299 list_for_each_entry(desc, &dev->msi_list, list) {
3300 irq = create_irq_nr(irq_want--);
3301 if (irq == 0)
3302 return -1;
3303#ifdef CONFIG_INTR_REMAP
3304 if (!intr_remapping_enabled)
3305 goto no_ir;
3306
3307 if (!sub_handle) {
3308 /*
3309 * allocate the consecutive block of IRTE's
3310 * for 'nvec'
3311 */
3312 index = msi_alloc_irte(dev, irq, nvec);
3313 if (index < 0) {
3314 ret = index;
3315 goto error;
3316 }
3317 } else {
3318 iommu = map_dev_to_ir(dev);
3319 if (!iommu) {
3320 ret = -ENOENT;
3321 goto error;
3322 }
3323 /*
3324 * setup the mapping between the irq and the IRTE
3325 * base index, the sub_handle pointing to the
3326 * appropriate interrupt remap table entry.
3327 */
3328 set_irte_irq(irq, iommu, index, sub_handle);
3329 }
3330no_ir:
3331#endif
3332 ret = setup_msi_irq(dev, desc, irq);
3333 if (ret < 0)
3334 goto error;
3335 sub_handle++;
3336 }
3337 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003338
3339error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003340 destroy_irq(irq);
3341 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003342}
3343
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003344void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003345{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003346 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003347}
3348
Ingo Molnar54168ed2008-08-20 09:07:45 +02003349#ifdef CONFIG_DMAR
3350#ifdef CONFIG_SMP
3351static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3352{
3353 struct irq_cfg *cfg;
3354 struct msi_msg msg;
3355 unsigned int dest;
3356 cpumask_t tmp;
3357 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003358
Ingo Molnar54168ed2008-08-20 09:07:45 +02003359 cpus_and(tmp, mask, cpu_online_map);
3360 if (cpus_empty(tmp))
3361 return;
3362
3363 if (assign_irq_vector(irq, mask))
3364 return;
3365
3366 cfg = irq_cfg(irq);
3367 cpus_and(tmp, cfg->domain, mask);
3368 dest = cpu_mask_to_apicid(tmp);
3369
3370 dmar_msi_read(irq, &msg);
3371
3372 msg.data &= ~MSI_DATA_VECTOR_MASK;
3373 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3374 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3375 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3376
3377 dmar_msi_write(irq, &msg);
3378 desc = irq_to_desc(irq);
3379 desc->affinity = mask;
3380}
3381#endif /* CONFIG_SMP */
3382
3383struct irq_chip dmar_msi_type = {
3384 .name = "DMAR_MSI",
3385 .unmask = dmar_msi_unmask,
3386 .mask = dmar_msi_mask,
3387 .ack = ack_apic_edge,
3388#ifdef CONFIG_SMP
3389 .set_affinity = dmar_msi_set_affinity,
3390#endif
3391 .retrigger = ioapic_retrigger_irq,
3392};
3393
3394int arch_setup_dmar_msi(unsigned int irq)
3395{
3396 int ret;
3397 struct msi_msg msg;
3398
3399 ret = msi_compose_msg(NULL, irq, &msg);
3400 if (ret < 0)
3401 return ret;
3402 dmar_msi_write(irq, &msg);
3403 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3404 "edge");
3405 return 0;
3406}
3407#endif
3408
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003409#ifdef CONFIG_HPET_TIMER
3410
3411#ifdef CONFIG_SMP
3412static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3413{
3414 struct irq_cfg *cfg;
3415 struct irq_desc *desc;
3416 struct msi_msg msg;
3417 unsigned int dest;
3418 cpumask_t tmp;
3419
3420 cpus_and(tmp, mask, cpu_online_map);
3421 if (cpus_empty(tmp))
3422 return;
3423
3424 if (assign_irq_vector(irq, mask))
3425 return;
3426
3427 cfg = irq_cfg(irq);
3428 cpus_and(tmp, cfg->domain, mask);
3429 dest = cpu_mask_to_apicid(tmp);
3430
3431 hpet_msi_read(irq, &msg);
3432
3433 msg.data &= ~MSI_DATA_VECTOR_MASK;
3434 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3435 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3436 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3437
3438 hpet_msi_write(irq, &msg);
3439 desc = irq_to_desc(irq);
3440 desc->affinity = mask;
3441}
3442#endif /* CONFIG_SMP */
3443
3444struct irq_chip hpet_msi_type = {
3445 .name = "HPET_MSI",
3446 .unmask = hpet_msi_unmask,
3447 .mask = hpet_msi_mask,
3448 .ack = ack_apic_edge,
3449#ifdef CONFIG_SMP
3450 .set_affinity = hpet_msi_set_affinity,
3451#endif
3452 .retrigger = ioapic_retrigger_irq,
3453};
3454
3455int arch_setup_hpet_msi(unsigned int irq)
3456{
3457 int ret;
3458 struct msi_msg msg;
3459
3460 ret = msi_compose_msg(NULL, irq, &msg);
3461 if (ret < 0)
3462 return ret;
3463
3464 hpet_msi_write(irq, &msg);
3465 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3466 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003467
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003468 return 0;
3469}
3470#endif
3471
Ingo Molnar54168ed2008-08-20 09:07:45 +02003472#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003473/*
3474 * Hypertransport interrupt support
3475 */
3476#ifdef CONFIG_HT_IRQ
3477
3478#ifdef CONFIG_SMP
3479
Yinghai Lu497c9a12008-08-19 20:50:28 -07003480static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003481{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003482 struct ht_irq_msg msg;
3483 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003484
Yinghai Lu497c9a12008-08-19 20:50:28 -07003485 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003486 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003487
Yinghai Lu497c9a12008-08-19 20:50:28 -07003488 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003489 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003490
Eric W. Biedermanec683072006-11-08 17:44:57 -08003491 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003492}
3493
3494static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3495{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003496 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003497 unsigned int dest;
3498 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003499 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003500
3501 cpus_and(tmp, mask, cpu_online_map);
3502 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003503 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003504
Yinghai Lu497c9a12008-08-19 20:50:28 -07003505 if (assign_irq_vector(irq, mask))
3506 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003507
Yinghai Lu497c9a12008-08-19 20:50:28 -07003508 cfg = irq_cfg(irq);
3509 cpus_and(tmp, cfg->domain, mask);
3510 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003511
Yinghai Lu497c9a12008-08-19 20:50:28 -07003512 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003513 desc = irq_to_desc(irq);
3514 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003515}
3516#endif
3517
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003518static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003519 .name = "PCI-HT",
3520 .mask = mask_ht_irq,
3521 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003522 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003523#ifdef CONFIG_SMP
3524 .set_affinity = set_ht_irq_affinity,
3525#endif
3526 .retrigger = ioapic_retrigger_irq,
3527};
3528
3529int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3530{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003531 struct irq_cfg *cfg;
3532 int err;
3533 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003534
Yinghai Lu497c9a12008-08-19 20:50:28 -07003535 tmp = TARGET_CPUS;
3536 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003537 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003538 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003539 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003540
Yinghai Lu497c9a12008-08-19 20:50:28 -07003541 cfg = irq_cfg(irq);
3542 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003543 dest = cpu_mask_to_apicid(tmp);
3544
Eric W. Biedermanec683072006-11-08 17:44:57 -08003545 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003546
Eric W. Biedermanec683072006-11-08 17:44:57 -08003547 msg.address_lo =
3548 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003549 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003550 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003551 ((INT_DEST_MODE == 0) ?
3552 HT_IRQ_LOW_DM_PHYSICAL :
3553 HT_IRQ_LOW_DM_LOGICAL) |
3554 HT_IRQ_LOW_RQEOI_EDGE |
3555 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3556 HT_IRQ_LOW_MT_FIXED :
3557 HT_IRQ_LOW_MT_ARBITRATED) |
3558 HT_IRQ_LOW_IRQ_MASKED;
3559
Eric W. Biedermanec683072006-11-08 17:44:57 -08003560 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003561
Ingo Molnara460e742006-10-17 00:10:03 -07003562 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3563 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003564
3565 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003566 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003567 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003568}
3569#endif /* CONFIG_HT_IRQ */
3570
Dean Nelson4173a0e2008-10-02 12:18:21 -05003571#ifdef CONFIG_X86_64
3572/*
3573 * Re-target the irq to the specified CPU and enable the specified MMR located
3574 * on the specified blade to allow the sending of MSIs to the specified CPU.
3575 */
3576int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3577 unsigned long mmr_offset)
3578{
3579 const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
3580 struct irq_cfg *cfg;
3581 int mmr_pnode;
3582 unsigned long mmr_value;
3583 struct uv_IO_APIC_route_entry *entry;
3584 unsigned long flags;
3585 int err;
3586
3587 err = assign_irq_vector(irq, *eligible_cpu);
3588 if (err != 0)
3589 return err;
3590
3591 spin_lock_irqsave(&vector_lock, flags);
3592 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3593 irq_name);
3594 spin_unlock_irqrestore(&vector_lock, flags);
3595
3596 cfg = irq_cfg(irq);
3597
3598 mmr_value = 0;
3599 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3600 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3601
3602 entry->vector = cfg->vector;
3603 entry->delivery_mode = INT_DELIVERY_MODE;
3604 entry->dest_mode = INT_DEST_MODE;
3605 entry->polarity = 0;
3606 entry->trigger = 0;
3607 entry->mask = 0;
3608 entry->dest = cpu_mask_to_apicid(*eligible_cpu);
3609
3610 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3611 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3612
3613 return irq;
3614}
3615
3616/*
3617 * Disable the specified MMR located on the specified blade so that MSIs are
3618 * longer allowed to be sent.
3619 */
3620void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3621{
3622 unsigned long mmr_value;
3623 struct uv_IO_APIC_route_entry *entry;
3624 int mmr_pnode;
3625
3626 mmr_value = 0;
3627 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3628 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3629
3630 entry->mask = 1;
3631
3632 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3633 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3634}
3635#endif /* CONFIG_X86_64 */
3636
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003637int __init io_apic_get_redir_entries (int ioapic)
3638{
3639 union IO_APIC_reg_01 reg_01;
3640 unsigned long flags;
3641
3642 spin_lock_irqsave(&ioapic_lock, flags);
3643 reg_01.raw = io_apic_read(ioapic, 1);
3644 spin_unlock_irqrestore(&ioapic_lock, flags);
3645
3646 return reg_01.bits.entries;
3647}
3648
3649int __init probe_nr_irqs(void)
3650{
3651 int idx;
3652 int nr = 0;
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003653#ifndef CONFIG_XEN
3654 int nr_min = 32;
3655#else
3656 int nr_min = NR_IRQS;
3657#endif
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003658
3659 for (idx = 0; idx < nr_ioapics; idx++)
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003660 nr += io_apic_get_redir_entries(idx) + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003661
3662 /* double it for hotplug and msi and nmi */
3663 nr <<= 1;
3664
3665 /* something wrong ? */
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003666 if (nr < nr_min)
3667 nr = nr_min;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003668
3669 return nr;
3670}
3671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003673 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 -------------------------------------------------------------------------- */
3675
Len Brown888ba6c2005-08-24 12:07:20 -04003676#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Ingo Molnar54168ed2008-08-20 09:07:45 +02003678#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003679int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
3681 union IO_APIC_reg_00 reg_00;
3682 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3683 physid_mask_t tmp;
3684 unsigned long flags;
3685 int i = 0;
3686
3687 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003688 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3689 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003691 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3693 * advantage of new APIC bus architecture.
3694 */
3695
3696 if (physids_empty(apic_id_map))
3697 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3698
3699 spin_lock_irqsave(&ioapic_lock, flags);
3700 reg_00.raw = io_apic_read(ioapic, 0);
3701 spin_unlock_irqrestore(&ioapic_lock, flags);
3702
3703 if (apic_id >= get_physical_broadcast()) {
3704 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3705 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3706 apic_id = reg_00.bits.ID;
3707 }
3708
3709 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003710 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 * 'stuck on smp_invalidate_needed IPI wait' messages.
3712 */
3713 if (check_apicid_used(apic_id_map, apic_id)) {
3714
3715 for (i = 0; i < get_physical_broadcast(); i++) {
3716 if (!check_apicid_used(apic_id_map, i))
3717 break;
3718 }
3719
3720 if (i == get_physical_broadcast())
3721 panic("Max apic_id exceeded!\n");
3722
3723 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3724 "trying %d\n", ioapic, apic_id, i);
3725
3726 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 tmp = apicid_to_cpu_present(apic_id);
3730 physids_or(apic_id_map, apic_id_map, tmp);
3731
3732 if (reg_00.bits.ID != apic_id) {
3733 reg_00.bits.ID = apic_id;
3734
3735 spin_lock_irqsave(&ioapic_lock, flags);
3736 io_apic_write(ioapic, 0, reg_00.raw);
3737 reg_00.raw = io_apic_read(ioapic, 0);
3738 spin_unlock_irqrestore(&ioapic_lock, flags);
3739
3740 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003741 if (reg_00.bits.ID != apic_id) {
3742 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3743 return -1;
3744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
3746
3747 apic_printk(APIC_VERBOSE, KERN_INFO
3748 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3749
3750 return apic_id;
3751}
3752
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003753int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 union IO_APIC_reg_01 reg_01;
3756 unsigned long flags;
3757
3758 spin_lock_irqsave(&ioapic_lock, flags);
3759 reg_01.raw = io_apic_read(ioapic, 1);
3760 spin_unlock_irqrestore(&ioapic_lock, flags);
3761
3762 return reg_01.bits.version;
3763}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003764#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Ingo Molnar54168ed2008-08-20 09:07:45 +02003766int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003769 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 ioapic);
3771 return -EINVAL;
3772 }
3773
3774 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 * IRQs < 16 are already in the irq_2_pin[] map
3776 */
3777 if (irq >= 16)
3778 add_pin_to_irq(irq, ioapic, pin);
3779
Yinghai Lu497c9a12008-08-19 20:50:28 -07003780 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 return 0;
3783}
3784
Ingo Molnar54168ed2008-08-20 09:07:45 +02003785
Shaohua Li61fd47e2007-11-17 01:05:28 -05003786int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3787{
3788 int i;
3789
3790 if (skip_ioapic_setup)
3791 return -1;
3792
3793 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003794 if (mp_irqs[i].mp_irqtype == mp_INT &&
3795 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003796 break;
3797 if (i >= mp_irq_entries)
3798 return -1;
3799
3800 *trigger = irq_trigger(i);
3801 *polarity = irq_polarity(i);
3802 return 0;
3803}
3804
Len Brown888ba6c2005-08-24 12:07:20 -04003805#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003806
Yinghai Lu497c9a12008-08-19 20:50:28 -07003807/*
3808 * This function currently is only a helper for the i386 smp boot process where
3809 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3810 * so mask in all cases should simply be TARGET_CPUS
3811 */
3812#ifdef CONFIG_SMP
3813void __init setup_ioapic_dest(void)
3814{
3815 int pin, ioapic, irq, irq_entry;
3816 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003817
3818 if (skip_ioapic_setup == 1)
3819 return;
3820
3821 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3822 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3823 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3824 if (irq_entry == -1)
3825 continue;
3826 irq = pin_2_irq(irq_entry, ioapic, pin);
3827
3828 /* setup_IO_APIC_irqs could fail to get vector for some device
3829 * when you have too many devices, because at that time only boot
3830 * cpu is online.
3831 */
3832 cfg = irq_cfg(irq);
3833 if (!cfg->vector)
3834 setup_IO_APIC_irq(ioapic, pin, irq,
3835 irq_trigger(irq_entry),
3836 irq_polarity(irq_entry));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003837#ifdef CONFIG_INTR_REMAP
3838 else if (intr_remapping_enabled)
3839 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3840#endif
3841 else
Yinghai Lu497c9a12008-08-19 20:50:28 -07003842 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003843 }
3844
3845 }
3846}
3847#endif
3848
Ingo Molnar54168ed2008-08-20 09:07:45 +02003849#define IOAPIC_RESOURCE_NAME_SIZE 11
3850
3851static struct resource *ioapic_resources;
3852
3853static struct resource * __init ioapic_setup_resources(void)
3854{
3855 unsigned long n;
3856 struct resource *res;
3857 char *mem;
3858 int i;
3859
3860 if (nr_ioapics <= 0)
3861 return NULL;
3862
3863 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3864 n *= nr_ioapics;
3865
3866 mem = alloc_bootmem(n);
3867 res = (void *)mem;
3868
3869 if (mem != NULL) {
3870 mem += sizeof(struct resource) * nr_ioapics;
3871
3872 for (i = 0; i < nr_ioapics; i++) {
3873 res[i].name = mem;
3874 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3875 sprintf(mem, "IOAPIC %u", i);
3876 mem += IOAPIC_RESOURCE_NAME_SIZE;
3877 }
3878 }
3879
3880 ioapic_resources = res;
3881
3882 return res;
3883}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003884
Yinghai Luf3294a32008-06-27 01:41:56 -07003885void __init ioapic_init_mappings(void)
3886{
3887 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3888 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003889 struct resource *ioapic_res;
Yinghai Luf3294a32008-06-27 01:41:56 -07003890
Ingo Molnar54168ed2008-08-20 09:07:45 +02003891 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003892 for (i = 0; i < nr_ioapics; i++) {
3893 if (smp_found_config) {
3894 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003895#ifdef CONFIG_X86_32
3896 if (!ioapic_phys) {
3897 printk(KERN_ERR
3898 "WARNING: bogus zero IO-APIC "
3899 "address found in MPTABLE, "
3900 "disabling IO/APIC support!\n");
3901 smp_found_config = 0;
3902 skip_ioapic_setup = 1;
3903 goto fake_ioapic_page;
3904 }
3905#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003906 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003907#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003908fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003909#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003910 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003911 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003912 ioapic_phys = __pa(ioapic_phys);
3913 }
3914 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003915 apic_printk(APIC_VERBOSE,
3916 "mapped IOAPIC to %08lx (%08lx)\n",
3917 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003918 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003919
Ingo Molnar54168ed2008-08-20 09:07:45 +02003920 if (ioapic_res != NULL) {
3921 ioapic_res->start = ioapic_phys;
3922 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3923 ioapic_res++;
3924 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003925 }
3926}
3927
Ingo Molnar54168ed2008-08-20 09:07:45 +02003928static int __init ioapic_insert_resources(void)
3929{
3930 int i;
3931 struct resource *r = ioapic_resources;
3932
3933 if (!r) {
3934 printk(KERN_ERR
3935 "IO APIC resources could be not be allocated.\n");
3936 return -1;
3937 }
3938
3939 for (i = 0; i < nr_ioapics; i++) {
3940 insert_resource(&iomem_resource, r);
3941 r++;
3942 }
3943
3944 return 0;
3945}
3946
3947/* Insert the IO APIC resources after PCI initialization has occured to handle
3948 * IO APICS that are mapped in on a BAR in PCI space. */
3949late_initcall(ioapic_insert_resources);