blob: 683610517d2ab1197992c5238d1e162f7605607e [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
Yinghai Lu199751d2008-08-19 20:50:27 -07001260 /* first time to use this irq_desc */
1261 if (irq < 16)
1262 desc = irq_to_desc(irq);
1263 else
1264 desc = irq_to_desc_alloc(irq);
1265
Jan Beulich6ebcc002006-06-26 13:56:46 +02001266 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001267 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001268 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001269 else
1270 desc->status &= ~IRQ_LEVEL;
1271
Ingo Molnar54168ed2008-08-20 09:07:45 +02001272#ifdef CONFIG_INTR_REMAP
1273 if (irq_remapped(irq)) {
1274 desc->status |= IRQ_MOVE_PCNTXT;
1275 if (trigger)
1276 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1277 handle_fasteoi_irq,
1278 "fasteoi");
1279 else
1280 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1281 handle_edge_irq, "edge");
1282 return;
1283 }
1284#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001285 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1286 trigger == IOAPIC_LEVEL)
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_fasteoi_irq,
1289 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001290 else
Ingo Molnara460e742006-10-17 00:10:03 -07001291 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001292 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001293}
1294
1295static int setup_ioapic_entry(int apic, int irq,
1296 struct IO_APIC_route_entry *entry,
1297 unsigned int destination, int trigger,
1298 int polarity, int vector)
1299{
1300 /*
1301 * add it to the IO-APIC irq-routing table:
1302 */
1303 memset(entry,0,sizeof(*entry));
1304
Ingo Molnar54168ed2008-08-20 09:07:45 +02001305#ifdef CONFIG_INTR_REMAP
1306 if (intr_remapping_enabled) {
1307 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1308 struct irte irte;
1309 struct IR_IO_APIC_route_entry *ir_entry =
1310 (struct IR_IO_APIC_route_entry *) entry;
1311 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001312
Ingo Molnar54168ed2008-08-20 09:07:45 +02001313 if (!iommu)
1314 panic("No mapping iommu for ioapic %d\n", apic);
1315
1316 index = alloc_irte(iommu, irq, 1);
1317 if (index < 0)
1318 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1319
1320 memset(&irte, 0, sizeof(irte));
1321
1322 irte.present = 1;
1323 irte.dst_mode = INT_DEST_MODE;
1324 irte.trigger_mode = trigger;
1325 irte.dlvry_mode = INT_DELIVERY_MODE;
1326 irte.vector = vector;
1327 irte.dest_id = IRTE_DEST(destination);
1328
1329 modify_irte(irq, &irte);
1330
1331 ir_entry->index2 = (index >> 15) & 0x1;
1332 ir_entry->zero = 0;
1333 ir_entry->format = 1;
1334 ir_entry->index = (index & 0x7fff);
1335 } else
1336#endif
1337 {
1338 entry->delivery_mode = INT_DELIVERY_MODE;
1339 entry->dest_mode = INT_DEST_MODE;
1340 entry->dest = destination;
1341 }
1342
1343 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001344 entry->trigger = trigger;
1345 entry->polarity = polarity;
1346 entry->vector = vector;
1347
1348 /* Mask level triggered irqs.
1349 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1350 */
1351 if (trigger)
1352 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001353 return 0;
1354}
1355
1356static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001357 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001358{
1359 struct irq_cfg *cfg;
1360 struct IO_APIC_route_entry entry;
1361 cpumask_t mask;
1362
1363 if (!IO_APIC_IRQ(irq))
1364 return;
1365
1366 cfg = irq_cfg(irq);
1367
1368 mask = TARGET_CPUS;
1369 if (assign_irq_vector(irq, mask))
1370 return;
1371
1372 cpus_and(mask, cfg->domain, mask);
1373
1374 apic_printk(APIC_VERBOSE,KERN_DEBUG
1375 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1376 "IRQ %d Mode:%i Active:%i)\n",
1377 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1378 irq, trigger, polarity);
1379
1380
1381 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1382 cpu_mask_to_apicid(mask), trigger, polarity,
1383 cfg->vector)) {
1384 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1385 mp_ioapics[apic].mp_apicid, pin);
1386 __clear_irq_vector(irq);
1387 return;
1388 }
1389
1390 ioapic_register_intr(irq, trigger);
1391 if (irq < 16)
1392 disable_8259A_irq(irq);
1393
1394 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397static void __init setup_IO_APIC_irqs(void)
1398{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001399 int apic, pin, idx, irq;
1400 int notcon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1403
1404 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001405 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001407 idx = find_irq_entry(apic, pin, mp_INT);
1408 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001409 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001410 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001411 apic_printk(APIC_VERBOSE,
1412 KERN_DEBUG " %d-%d",
1413 mp_ioapics[apic].mp_apicid,
1414 pin);
1415 } else
1416 apic_printk(APIC_VERBOSE, " %d-%d",
1417 mp_ioapics[apic].mp_apicid,
1418 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001419 continue;
1420 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001421 if (notcon) {
1422 apic_printk(APIC_VERBOSE,
1423 " (apicid-pin) not connected\n");
1424 notcon = 0;
1425 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001426
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001427 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001428#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001429 if (multi_timer_check(apic, irq))
1430 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001431#endif
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001432 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001433
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001434 setup_IO_APIC_irq(apic, pin, irq,
1435 irq_trigger(idx), irq_polarity(idx));
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001439 if (notcon)
1440 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001441 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001445 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001447static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1448 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Ingo Molnar54168ed2008-08-20 09:07:45 +02001452#ifdef CONFIG_INTR_REMAP
1453 if (intr_remapping_enabled)
1454 return;
1455#endif
1456
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001457 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /*
1460 * We use logical delivery to get the timer IRQ
1461 * to the first CPU.
1462 */
1463 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001464 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001465 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 entry.delivery_mode = INT_DELIVERY_MODE;
1467 entry.polarity = 0;
1468 entry.trigger = 0;
1469 entry.vector = vector;
1470
1471 /*
1472 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001473 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001475 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /*
1478 * Add it to the IO-APIC irq-routing table:
1479 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001480 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001483
1484__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 int apic, i;
1487 union IO_APIC_reg_00 reg_00;
1488 union IO_APIC_reg_01 reg_01;
1489 union IO_APIC_reg_02 reg_02;
1490 union IO_APIC_reg_03 reg_03;
1491 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001492 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001493 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (apic_verbosity == APIC_QUIET)
1496 return;
1497
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001498 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 for (i = 0; i < nr_ioapics; i++)
1500 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001501 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /*
1504 * We are a bit conservative about what we expect. We have to
1505 * know about every hardware change ASAP.
1506 */
1507 printk(KERN_INFO "testing the IO APIC.......................\n");
1508
1509 for (apic = 0; apic < nr_ioapics; apic++) {
1510
1511 spin_lock_irqsave(&ioapic_lock, flags);
1512 reg_00.raw = io_apic_read(apic, 0);
1513 reg_01.raw = io_apic_read(apic, 1);
1514 if (reg_01.bits.version >= 0x10)
1515 reg_02.raw = io_apic_read(apic, 2);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001516 if (reg_01.bits.version >= 0x20)
1517 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 spin_unlock_irqrestore(&ioapic_lock, flags);
1519
Ingo Molnar54168ed2008-08-20 09:07:45 +02001520 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001521 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1523 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1524 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1525 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Ingo Molnar54168ed2008-08-20 09:07:45 +02001527 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1531 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1535 * but the value of reg_02 is read as the previous read register
1536 * value, so ignore it if reg_02 == reg_01.
1537 */
1538 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1539 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1540 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
1543 /*
1544 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1545 * or reg_03, but the value of reg_0[23] is read as the previous read
1546 * register value, so ignore it if reg_03 == reg_0[12].
1547 */
1548 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1549 reg_03.raw != reg_01.raw) {
1550 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1551 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
1554 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1555
Yinghai Lud83e94a2008-08-19 20:50:33 -07001556 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1557 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 for (i = 0; i <= reg_01.bits.entries; i++) {
1560 struct IO_APIC_route_entry entry;
1561
Andi Kleencf4c6a22006-09-26 10:52:30 +02001562 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Ingo Molnar54168ed2008-08-20 09:07:45 +02001564 printk(KERN_DEBUG " %02x %03X ",
1565 i,
1566 entry.dest
1567 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1570 entry.mask,
1571 entry.trigger,
1572 entry.irr,
1573 entry.polarity,
1574 entry.delivery_status,
1575 entry.dest_mode,
1576 entry.delivery_mode,
1577 entry.vector
1578 );
1579 }
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001582 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001583 struct irq_pin_list *entry = cfg->irq_2_pin;
1584 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001586 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 for (;;) {
1588 printk("-> %d:%d", entry->apic, entry->pin);
1589 if (!entry->next)
1590 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001591 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 printk("\n");
1594 }
1595
1596 printk(KERN_INFO ".................................... done.\n");
1597
1598 return;
1599}
1600
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001601__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 unsigned int v;
1604 int i, j;
1605
1606 if (apic_verbosity == APIC_QUIET)
1607 return;
1608
1609 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1610 for (i = 0; i < 8; i++) {
1611 v = apic_read(base + i*0x10);
1612 for (j = 0; j < 32; j++) {
1613 if (v & (1<<j))
1614 printk("1");
1615 else
1616 printk("0");
1617 }
1618 printk("\n");
1619 }
1620}
1621
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001622__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001625 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 if (apic_verbosity == APIC_QUIET)
1628 return;
1629
1630 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1631 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001632 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001633 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 v = apic_read(APIC_LVR);
1635 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1636 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001637 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 v = apic_read(APIC_TASKPRI);
1640 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1641
Ingo Molnar54168ed2008-08-20 09:07:45 +02001642 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001643 if (!APIC_XAPIC(ver)) {
1644 v = apic_read(APIC_ARBPRI);
1645 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1646 v & APIC_ARBPRI_MASK);
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 v = apic_read(APIC_PROCPRI);
1649 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1650 }
1651
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001652 /*
1653 * Remote read supported only in the 82489DX and local APIC for
1654 * Pentium processors.
1655 */
1656 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1657 v = apic_read(APIC_RRR);
1658 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1659 }
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 v = apic_read(APIC_LDR);
1662 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001663 if (!x2apic_enabled()) {
1664 v = apic_read(APIC_DFR);
1665 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 v = apic_read(APIC_SPIV);
1668 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1669
1670 printk(KERN_DEBUG "... APIC ISR field:\n");
1671 print_APIC_bitfield(APIC_ISR);
1672 printk(KERN_DEBUG "... APIC TMR field:\n");
1673 print_APIC_bitfield(APIC_TMR);
1674 printk(KERN_DEBUG "... APIC IRR field:\n");
1675 print_APIC_bitfield(APIC_IRR);
1676
Ingo Molnar54168ed2008-08-20 09:07:45 +02001677 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1678 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 v = apic_read(APIC_ESR);
1682 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1683 }
1684
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001685 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001686 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1687 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 v = apic_read(APIC_LVTT);
1690 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1691
1692 if (maxlvt > 3) { /* PC is LVT#4. */
1693 v = apic_read(APIC_LVTPC);
1694 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1695 }
1696 v = apic_read(APIC_LVT0);
1697 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1698 v = apic_read(APIC_LVT1);
1699 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1700
1701 if (maxlvt > 2) { /* ERR is LVT#3. */
1702 v = apic_read(APIC_LVTERR);
1703 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1704 }
1705
1706 v = apic_read(APIC_TMICT);
1707 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1708 v = apic_read(APIC_TMCCT);
1709 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1710 v = apic_read(APIC_TDCR);
1711 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1712 printk("\n");
1713}
1714
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001715__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001717 int cpu;
1718
1719 preempt_disable();
1720 for_each_online_cpu(cpu)
1721 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1722 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001725__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 unsigned int v;
1728 unsigned long flags;
1729
1730 if (apic_verbosity == APIC_QUIET)
1731 return;
1732
1733 printk(KERN_DEBUG "\nprinting PIC contents\n");
1734
1735 spin_lock_irqsave(&i8259A_lock, flags);
1736
1737 v = inb(0xa1) << 8 | inb(0x21);
1738 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1739
1740 v = inb(0xa0) << 8 | inb(0x20);
1741 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1742
Ingo Molnar54168ed2008-08-20 09:07:45 +02001743 outb(0x0b,0xa0);
1744 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001746 outb(0x0a,0xa0);
1747 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 spin_unlock_irqrestore(&i8259A_lock, flags);
1750
1751 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1752
1753 v = inb(0x4d1) << 8 | inb(0x4d0);
1754 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1755}
1756
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001757__apicdebuginit(int) print_all_ICs(void)
1758{
1759 print_PIC();
1760 print_all_local_APICs();
1761 print_IO_APIC();
1762
1763 return 0;
1764}
1765
1766fs_initcall(print_all_ICs);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Yinghai Luefa25592008-08-19 20:50:36 -07001769/* Where if anywhere is the i8259 connect in external int mode */
1770static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1771
Ingo Molnar54168ed2008-08-20 09:07:45 +02001772void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001775 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001776 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 unsigned long flags;
1778
Ingo Molnar54168ed2008-08-20 09:07:45 +02001779#ifdef CONFIG_X86_32
1780 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (!pirqs_enabled)
1782 for (i = 0; i < MAX_PIRQS; i++)
1783 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001784#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /*
1787 * The number of IO-APIC IRQ registers (== #pins):
1788 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001789 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001791 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001793 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1794 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001795 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001796 int pin;
1797 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001798 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001799 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001800 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001801
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001802 /* If the interrupt line is enabled and in ExtInt mode
1803 * I have found the pin where the i8259 is connected.
1804 */
1805 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1806 ioapic_i8259.apic = apic;
1807 ioapic_i8259.pin = pin;
1808 goto found_i8259;
1809 }
1810 }
1811 }
1812 found_i8259:
1813 /* Look to see what if the MP table has reported the ExtINT */
1814 /* If we could not find the appropriate pin by looking at the ioapic
1815 * the i8259 probably is not connected the ioapic but give the
1816 * mptable a chance anyway.
1817 */
1818 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1819 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1820 /* Trust the MP table if nothing is setup in the hardware */
1821 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1822 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1823 ioapic_i8259.pin = i8259_pin;
1824 ioapic_i8259.apic = i8259_apic;
1825 }
1826 /* Complain if the MP table and the hardware disagree */
1827 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1828 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1829 {
1830 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
1833 /*
1834 * Do not trust the IO-APIC being empty at bootup
1835 */
1836 clear_IO_APIC();
1837}
1838
1839/*
1840 * Not an __init, needed by the reboot code
1841 */
1842void disable_IO_APIC(void)
1843{
1844 /*
1845 * Clear the IO-APIC before rebooting:
1846 */
1847 clear_IO_APIC();
1848
Eric W. Biederman650927e2005-06-25 14:57:44 -07001849 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001850 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001851 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001852 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001853 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001854 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001855 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001856
1857 memset(&entry, 0, sizeof(entry));
1858 entry.mask = 0; /* Enabled */
1859 entry.trigger = 0; /* Edge */
1860 entry.irr = 0;
1861 entry.polarity = 0; /* High */
1862 entry.delivery_status = 0;
1863 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001864 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001865 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001866 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001867
1868 /*
1869 * Add it to the IO-APIC irq-routing table:
1870 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001871 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001872 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001873
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001874 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Ingo Molnar54168ed2008-08-20 09:07:45 +02001877#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
1879 * function to set the IO-APIC physical IDs based on the
1880 * values stored in the MPC table.
1881 *
1882 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1883 */
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885static void __init setup_ioapic_ids_from_mpc(void)
1886{
1887 union IO_APIC_reg_00 reg_00;
1888 physid_mask_t phys_id_present_map;
1889 int apic;
1890 int i;
1891 unsigned char old_id;
1892 unsigned long flags;
1893
Yinghai Lua4dbc342008-07-25 02:14:28 -07001894 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001895 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001898 * Don't check I/O APIC IDs for xAPIC systems. They have
1899 * no meaning without the serial APIC bus.
1900 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001901 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1902 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001903 return;
1904 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * This is broken; anything with a real cpu count has to
1906 * circumvent this idiocy regardless.
1907 */
1908 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1909
1910 /*
1911 * Set the IOAPIC ID to the value stored in the MPC table.
1912 */
1913 for (apic = 0; apic < nr_ioapics; apic++) {
1914
1915 /* Read the register 0 value */
1916 spin_lock_irqsave(&ioapic_lock, flags);
1917 reg_00.raw = io_apic_read(apic, 0);
1918 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001919
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001920 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001922 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001924 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1926 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001927 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * Sanity check, is the ID really free? Every APIC in a
1932 * system must have a unique ID or we get lots of nice
1933 * 'stuck on smp_invalidate_needed IPI wait' messages.
1934 */
1935 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001936 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001938 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 for (i = 0; i < get_physical_broadcast(); i++)
1940 if (!physid_isset(i, phys_id_present_map))
1941 break;
1942 if (i >= get_physical_broadcast())
1943 panic("Max APIC ID exceeded!\n");
1944 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1945 i);
1946 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001947 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 } else {
1949 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001950 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 apic_printk(APIC_VERBOSE, "Setting %d in the "
1952 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001953 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1955 }
1956
1957
1958 /*
1959 * We need to adjust the IRQ routing table
1960 * if the ID changed.
1961 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001962 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001964 if (mp_irqs[i].mp_dstapic == old_id)
1965 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001966 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /*
1969 * Read the right value from the MPC table and
1970 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 apic_printk(APIC_VERBOSE, KERN_INFO
1973 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001974 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001976 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07001978 io_apic_write(apic, 0, reg_00.raw);
1979 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /*
1982 * Sanity check
1983 */
1984 spin_lock_irqsave(&ioapic_lock, flags);
1985 reg_00.raw = io_apic_read(apic, 0);
1986 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001987 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 printk("could not set ID!\n");
1989 else
1990 apic_printk(APIC_VERBOSE, " ok.\n");
1991 }
1992}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001995int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001996
1997static int __init notimercheck(char *s)
1998{
1999 no_timer_check = 1;
2000 return 1;
2001}
2002__setup("no_timer_check", notimercheck);
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/*
2005 * There is a nasty bug in some older SMP boards, their mptable lies
2006 * about the timer IRQ. We do the following to work around the situation:
2007 *
2008 * - timer IRQ defaults to IO-APIC IRQ
2009 * - if this function detects that timer IRQs are defunct, then we fall
2010 * back to ISA timer IRQs
2011 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002012static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002015 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Zachary Amsden8542b202006-12-07 02:14:09 +01002017 if (no_timer_check)
2018 return 1;
2019
Ingo Molnar4aae0702007-12-18 18:05:58 +01002020 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 local_irq_enable();
2022 /* Let ten ticks pass... */
2023 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002024 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 /*
2027 * Expect a few ticks at least, to be sure some possible
2028 * glue logic does not lock up after one or two first
2029 * ticks in a non-ExtINT mode. Also the local APIC
2030 * might have cached one ExtINT interrupt. Finally, at
2031 * least one tick may be lost due to delays.
2032 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002033
2034 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002035 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
2038}
2039
2040/*
2041 * In the SMP+IOAPIC case it might happen that there are an unspecified
2042 * number of pending IRQ events unhandled. These cases are very rare,
2043 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2044 * better to do it this way as thus we do not have to be aware of
2045 * 'pending' interrupts in the IRQ path, except at this point.
2046 */
2047/*
2048 * Edge triggered needs to resend any interrupt
2049 * that was delayed but this is now handled in the device
2050 * independent code.
2051 */
2052
2053/*
2054 * Starting up a edge-triggered IO-APIC interrupt is
2055 * nasty - we need to make sure that we get the edge.
2056 * If it is already asserted for some reason, we need
2057 * return 1 to indicate that is was pending.
2058 *
2059 * This is not complete - we should be able to fake
2060 * an edge even if it isn't on the 8259A...
2061 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002062
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002063static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int was_pending = 0;
2066 unsigned long flags;
2067
2068 spin_lock_irqsave(&ioapic_lock, flags);
2069 if (irq < 16) {
2070 disable_8259A_irq(irq);
2071 if (i8259A_irq_pending(irq))
2072 was_pending = 1;
2073 }
2074 __unmask_IO_APIC_irq(irq);
2075 spin_unlock_irqrestore(&ioapic_lock, flags);
2076
2077 return was_pending;
2078}
2079
Ingo Molnar54168ed2008-08-20 09:07:45 +02002080#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002081static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002083
2084 struct irq_cfg *cfg = irq_cfg(irq);
2085 unsigned long flags;
2086
2087 spin_lock_irqsave(&vector_lock, flags);
2088 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2089 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002090
2091 return 1;
2092}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002093#else
2094static int ioapic_retrigger_irq(unsigned int irq)
2095{
2096 send_IPI_self(irq_cfg(irq)->vector);
2097
2098 return 1;
2099}
2100#endif
2101
2102/*
2103 * Level and edge triggered IO-APIC interrupts need different handling,
2104 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2105 * handled with the level-triggered descriptor, but that one has slightly
2106 * more overhead. Level-triggered interrupts cannot be handled with the
2107 * edge-triggered handler, without risking IRQ storms and other ugly
2108 * races.
2109 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002110
Yinghai Lu497c9a12008-08-19 20:50:28 -07002111#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002112
2113#ifdef CONFIG_INTR_REMAP
2114static void ir_irq_migration(struct work_struct *work);
2115
2116static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2117
2118/*
2119 * Migrate the IO-APIC irq in the presence of intr-remapping.
2120 *
2121 * For edge triggered, irq migration is a simple atomic update(of vector
2122 * and cpu destination) of IRTE and flush the hardware cache.
2123 *
2124 * For level triggered, we need to modify the io-apic RTE aswell with the update
2125 * vector information, along with modifying IRTE with vector and destination.
2126 * So irq migration for level triggered is little bit more complex compared to
2127 * edge triggered migration. But the good news is, we use the same algorithm
2128 * for level triggered migration as we have today, only difference being,
2129 * we now initiate the irq migration from process context instead of the
2130 * interrupt context.
2131 *
2132 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2133 * suppression) to the IO-APIC, level triggered irq migration will also be
2134 * as simple as edge triggered migration and we can do the irq migration
2135 * with a simple atomic update to IO-APIC RTE.
2136 */
2137static void migrate_ioapic_irq(int irq, cpumask_t mask)
2138{
2139 struct irq_cfg *cfg;
2140 struct irq_desc *desc;
2141 cpumask_t tmp, cleanup_mask;
2142 struct irte irte;
2143 int modify_ioapic_rte;
2144 unsigned int dest;
2145 unsigned long flags;
2146
2147 cpus_and(tmp, mask, cpu_online_map);
2148 if (cpus_empty(tmp))
2149 return;
2150
2151 if (get_irte(irq, &irte))
2152 return;
2153
2154 if (assign_irq_vector(irq, mask))
2155 return;
2156
2157 cfg = irq_cfg(irq);
2158 cpus_and(tmp, cfg->domain, mask);
2159 dest = cpu_mask_to_apicid(tmp);
2160
2161 desc = irq_to_desc(irq);
2162 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2163 if (modify_ioapic_rte) {
2164 spin_lock_irqsave(&ioapic_lock, flags);
2165 __target_IO_APIC_irq(irq, dest, cfg->vector);
2166 spin_unlock_irqrestore(&ioapic_lock, flags);
2167 }
2168
2169 irte.vector = cfg->vector;
2170 irte.dest_id = IRTE_DEST(dest);
2171
2172 /*
2173 * Modified the IRTE and flushes the Interrupt entry cache.
2174 */
2175 modify_irte(irq, &irte);
2176
2177 if (cfg->move_in_progress) {
2178 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2179 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2180 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2181 cfg->move_in_progress = 0;
2182 }
2183
2184 desc->affinity = mask;
2185}
2186
2187static int migrate_irq_remapped_level(int irq)
2188{
2189 int ret = -1;
2190 struct irq_desc *desc = irq_to_desc(irq);
2191
2192 mask_IO_APIC_irq(irq);
2193
2194 if (io_apic_level_ack_pending(irq)) {
2195 /*
2196 * Interrupt in progress. Migrating irq now will change the
2197 * vector information in the IO-APIC RTE and that will confuse
2198 * the EOI broadcast performed by cpu.
2199 * So, delay the irq migration to the next instance.
2200 */
2201 schedule_delayed_work(&ir_migration_work, 1);
2202 goto unmask;
2203 }
2204
2205 /* everthing is clear. we have right of way */
2206 migrate_ioapic_irq(irq, desc->pending_mask);
2207
2208 ret = 0;
2209 desc->status &= ~IRQ_MOVE_PENDING;
2210 cpus_clear(desc->pending_mask);
2211
2212unmask:
2213 unmask_IO_APIC_irq(irq);
2214 return ret;
2215}
2216
2217static void ir_irq_migration(struct work_struct *work)
2218{
2219 unsigned int irq;
2220 struct irq_desc *desc;
2221
2222 for_each_irq_desc(irq, desc) {
2223 if (desc->status & IRQ_MOVE_PENDING) {
2224 unsigned long flags;
2225
2226 spin_lock_irqsave(&desc->lock, flags);
2227 if (!desc->chip->set_affinity ||
2228 !(desc->status & IRQ_MOVE_PENDING)) {
2229 desc->status &= ~IRQ_MOVE_PENDING;
2230 spin_unlock_irqrestore(&desc->lock, flags);
2231 continue;
2232 }
2233
2234 desc->chip->set_affinity(irq, desc->pending_mask);
2235 spin_unlock_irqrestore(&desc->lock, flags);
2236 }
2237 }
2238}
2239
2240/*
2241 * Migrates the IRQ destination in the process context.
2242 */
2243static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2244{
2245 struct irq_desc *desc = irq_to_desc(irq);
2246
2247 if (desc->status & IRQ_LEVEL) {
2248 desc->status |= IRQ_MOVE_PENDING;
2249 desc->pending_mask = mask;
2250 migrate_irq_remapped_level(irq);
2251 return;
2252 }
2253
2254 migrate_ioapic_irq(irq, mask);
2255}
2256#endif
2257
Yinghai Lu497c9a12008-08-19 20:50:28 -07002258asmlinkage void smp_irq_move_cleanup_interrupt(void)
2259{
2260 unsigned vector, me;
2261 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002262#ifdef CONFIG_X86_64
2263 exit_idle();
2264#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002265 irq_enter();
2266
2267 me = smp_processor_id();
2268 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2269 unsigned int irq;
2270 struct irq_desc *desc;
2271 struct irq_cfg *cfg;
2272 irq = __get_cpu_var(vector_irq)[vector];
2273
2274 desc = irq_to_desc(irq);
2275 if (!desc)
2276 continue;
2277
2278 cfg = irq_cfg(irq);
2279 spin_lock(&desc->lock);
2280 if (!cfg->move_cleanup_count)
2281 goto unlock;
2282
2283 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2284 goto unlock;
2285
2286 __get_cpu_var(vector_irq)[vector] = -1;
2287 cfg->move_cleanup_count--;
2288unlock:
2289 spin_unlock(&desc->lock);
2290 }
2291
2292 irq_exit();
2293}
2294
2295static void irq_complete_move(unsigned int irq)
2296{
2297 struct irq_cfg *cfg = irq_cfg(irq);
2298 unsigned vector, me;
2299
2300 if (likely(!cfg->move_in_progress))
2301 return;
2302
2303 vector = ~get_irq_regs()->orig_ax;
2304 me = smp_processor_id();
2305 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2306 cpumask_t cleanup_mask;
2307
2308 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2309 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2310 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2311 cfg->move_in_progress = 0;
2312 }
2313}
2314#else
2315static inline void irq_complete_move(unsigned int irq) {}
2316#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002317#ifdef CONFIG_INTR_REMAP
2318static void ack_x2apic_level(unsigned int irq)
2319{
2320 ack_x2APIC_irq();
2321}
2322
2323static void ack_x2apic_edge(unsigned int irq)
2324{
2325 ack_x2APIC_irq();
2326}
2327#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002328
Yinghai Lu1d025192008-08-19 20:50:34 -07002329static void ack_apic_edge(unsigned int irq)
2330{
2331 irq_complete_move(irq);
2332 move_native_irq(irq);
2333 ack_APIC_irq();
2334}
2335
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002336#ifdef CONFIG_X86_32
2337atomic_t irq_mis_count;
2338#endif
2339
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002340static void ack_apic_level(unsigned int irq)
2341{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002342#ifdef CONFIG_X86_32
2343 unsigned long v;
2344 int i;
2345#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002346 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002347
Ingo Molnar54168ed2008-08-20 09:07:45 +02002348 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002349#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002350 /* If we are moving the irq we need to mask it */
2351 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2352 do_unmask_irq = 1;
2353 mask_IO_APIC_irq(irq);
2354 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002355#endif
2356
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002357#ifdef CONFIG_X86_32
2358 /*
2359 * It appears there is an erratum which affects at least version 0x11
2360 * of I/O APIC (that's the 82093AA and cores integrated into various
2361 * chipsets). Under certain conditions a level-triggered interrupt is
2362 * erroneously delivered as edge-triggered one but the respective IRR
2363 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2364 * message but it will never arrive and further interrupts are blocked
2365 * from the source. The exact reason is so far unknown, but the
2366 * phenomenon was observed when two consecutive interrupt requests
2367 * from a given source get delivered to the same CPU and the source is
2368 * temporarily disabled in between.
2369 *
2370 * A workaround is to simulate an EOI message manually. We achieve it
2371 * by setting the trigger mode to edge and then to level when the edge
2372 * trigger mode gets detected in the TMR of a local APIC for a
2373 * level-triggered interrupt. We mask the source for the time of the
2374 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2375 * The idea is from Manfred Spraul. --macro
2376 */
2377 i = irq_cfg(irq)->vector;
2378
2379 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2380#endif
2381
Ingo Molnar54168ed2008-08-20 09:07:45 +02002382 /*
2383 * We must acknowledge the irq before we move it or the acknowledge will
2384 * not propagate properly.
2385 */
2386 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002387
Ingo Molnar54168ed2008-08-20 09:07:45 +02002388 /* Now we can move and renable the irq */
2389 if (unlikely(do_unmask_irq)) {
2390 /* Only migrate the irq if the ack has been received.
2391 *
2392 * On rare occasions the broadcast level triggered ack gets
2393 * delayed going to ioapics, and if we reprogram the
2394 * vector while Remote IRR is still set the irq will never
2395 * fire again.
2396 *
2397 * To prevent this scenario we read the Remote IRR bit
2398 * of the ioapic. This has two effects.
2399 * - On any sane system the read of the ioapic will
2400 * flush writes (and acks) going to the ioapic from
2401 * this cpu.
2402 * - We get to see if the ACK has actually been delivered.
2403 *
2404 * Based on failed experiments of reprogramming the
2405 * ioapic entry from outside of irq context starting
2406 * with masking the ioapic entry and then polling until
2407 * Remote IRR was clear before reprogramming the
2408 * ioapic I don't trust the Remote IRR bit to be
2409 * completey accurate.
2410 *
2411 * However there appears to be no other way to plug
2412 * this race, so if the Remote IRR bit is not
2413 * accurate and is causing problems then it is a hardware bug
2414 * and you can go talk to the chipset vendor about it.
2415 */
2416 if (!io_apic_level_ack_pending(irq))
2417 move_masked_irq(irq);
2418 unmask_IO_APIC_irq(irq);
2419 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002420
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002421#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002422 if (!(v & (1 << (i & 0x1f)))) {
2423 atomic_inc(&irq_mis_count);
2424 spin_lock(&ioapic_lock);
2425 __mask_and_edge_IO_APIC_irq(irq);
2426 __unmask_and_level_IO_APIC_irq(irq);
2427 spin_unlock(&ioapic_lock);
2428 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002429#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002430}
Yinghai Lu1d025192008-08-19 20:50:34 -07002431
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002432static struct irq_chip ioapic_chip __read_mostly = {
2433 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002434 .startup = startup_ioapic_irq,
2435 .mask = mask_IO_APIC_irq,
2436 .unmask = unmask_IO_APIC_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07002437 .ack = ack_apic_edge,
2438 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002439#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002440 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002441#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002442 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443};
2444
Ingo Molnar54168ed2008-08-20 09:07:45 +02002445#ifdef CONFIG_INTR_REMAP
2446static struct irq_chip ir_ioapic_chip __read_mostly = {
2447 .name = "IR-IO-APIC",
2448 .startup = startup_ioapic_irq,
2449 .mask = mask_IO_APIC_irq,
2450 .unmask = unmask_IO_APIC_irq,
2451 .ack = ack_x2apic_edge,
2452 .eoi = ack_x2apic_level,
2453#ifdef CONFIG_SMP
2454 .set_affinity = set_ir_ioapic_affinity_irq,
2455#endif
2456 .retrigger = ioapic_retrigger_irq,
2457};
2458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460static inline void init_IO_APIC_traps(void)
2461{
2462 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002463 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002464 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 /*
2467 * NOTE! The local APIC isn't very good at handling
2468 * multiple interrupts at the same interrupt level.
2469 * As the interrupt level is determined by taking the
2470 * vector number and shifting that right by 4, we
2471 * want to spread these out a bit so that they don't
2472 * all fall in the same interrupt level.
2473 *
2474 * Also, we've got to be careful not to trash gate
2475 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2476 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002477 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002478 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 /*
2480 * Hmm.. We don't have an entry for this,
2481 * so default to an old-fashioned 8259
2482 * interrupt if we can..
2483 */
2484 if (irq < 16)
2485 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002486 else {
2487 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002489 desc->chip = &no_irq_chip;
2490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492 }
2493}
2494
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002495/*
2496 * The local APIC irq-chip implementation:
2497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002499static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 unsigned long v;
2502
2503 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002504 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002507static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002509 unsigned long v;
2510
2511 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002512 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Ingo Molnar54168ed2008-08-20 09:07:45 +02002515static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002516{
2517 ack_APIC_irq();
2518}
2519
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002520static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002521 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002522 .mask = mask_lapic_irq,
2523 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002524 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525};
2526
Yinghai Lu497c9a12008-08-19 20:50:28 -07002527static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002528{
Yinghai Lu08678b02008-08-19 20:50:05 -07002529 struct irq_desc *desc;
2530
2531 desc = irq_to_desc(irq);
2532 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002533 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2534 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002535}
2536
Jan Beuliche9427102008-01-30 13:31:24 +01002537static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002540 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 * We put the 8259A master into AEOI mode and
2542 * unmask on all local APICs LVT0 as NMI.
2543 *
2544 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2545 * is from Maciej W. Rozycki - so we do not have to EOI from
2546 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2549
Jan Beuliche9427102008-01-30 13:31:24 +01002550 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 apic_printk(APIC_VERBOSE, " done.\n");
2553}
2554
2555/*
2556 * This looks a bit hackish but it's about the only one way of sending
2557 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2558 * not support the ExtINT mode, unfortunately. We need to send these
2559 * cycles as some i82489DX-based boards have glue logic that keeps the
2560 * 8259A interrupt line asserted until INTA. --macro
2561 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002562static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002564 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct IO_APIC_route_entry entry0, entry1;
2566 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002568 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002569 if (pin == -1) {
2570 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002572 }
2573 apic = find_isa_irq_apic(8, mp_INT);
2574 if (apic == -1) {
2575 WARN_ON_ONCE(1);
2576 return;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andi Kleencf4c6a22006-09-26 10:52:30 +02002579 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002580 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 memset(&entry1, 0, sizeof(entry1));
2583
2584 entry1.dest_mode = 0; /* physical delivery */
2585 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002586 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 entry1.delivery_mode = dest_ExtINT;
2588 entry1.polarity = entry0.polarity;
2589 entry1.trigger = 0;
2590 entry1.vector = 0;
2591
Andi Kleencf4c6a22006-09-26 10:52:30 +02002592 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 save_control = CMOS_READ(RTC_CONTROL);
2595 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2596 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2597 RTC_FREQ_SELECT);
2598 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2599
2600 i = 100;
2601 while (i-- > 0) {
2602 mdelay(10);
2603 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2604 i -= 10;
2605 }
2606
2607 CMOS_WRITE(save_control, RTC_CONTROL);
2608 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002609 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Andi Kleencf4c6a22006-09-26 10:52:30 +02002611 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Yinghai Luefa25592008-08-19 20:50:36 -07002614static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002615/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002616static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002617{
2618 disable_timer_pin_1 = 1;
2619 return 0;
2620}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002621early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002622
2623int timer_through_8259 __initdata;
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625/*
2626 * This code may look a bit paranoid, but it's supposed to cooperate with
2627 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2628 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2629 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002630 *
2631 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002633static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002635 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002636 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002637 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002638 unsigned int ver;
2639 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002640
2641 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002642
Ingo Molnar54168ed2008-08-20 09:07:45 +02002643 ver = apic_read(APIC_LVR);
2644 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /*
2647 * get/set the timer IRQ vector:
2648 */
2649 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002650 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002653 * As IRQ0 is to be enabled in the 8259A, the virtual
2654 * wire has to be disabled in the local APIC. Also
2655 * timer interrupts need to be acknowledged manually in
2656 * the 8259A for the i82489DX when using the NMI
2657 * watchdog as that APIC treats NMIs as level-triggered.
2658 * The AEOI mode will finish them in the 8259A
2659 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002661 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002663#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002664 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002665#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002667 pin1 = find_isa_irq_pin(0, mp_INT);
2668 apic1 = find_isa_irq_apic(0, mp_INT);
2669 pin2 = ioapic_i8259.pin;
2670 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002672 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2673 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002674 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002676 /*
2677 * Some BIOS writers are clueless and report the ExtINTA
2678 * I/O APIC input from the cascaded 8259A as the timer
2679 * interrupt input. So just in case, if only one pin
2680 * was found above, try it both directly and through the
2681 * 8259A.
2682 */
2683 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002684#ifdef CONFIG_INTR_REMAP
2685 if (intr_remapping_enabled)
2686 panic("BIOS bug: timer not connected to IO-APIC");
2687#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002688 pin1 = pin2;
2689 apic1 = apic2;
2690 no_pin1 = 1;
2691 } else if (pin2 == -1) {
2692 pin2 = pin1;
2693 apic2 = apic1;
2694 }
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (pin1 != -1) {
2697 /*
2698 * Ok, does IRQ0 through the IOAPIC work?
2699 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002700 if (no_pin1) {
2701 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002702 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 unmask_IO_APIC_irq(0);
2705 if (timer_irq_works()) {
2706 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 setup_nmi();
2708 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002710 if (disable_timer_pin_1 > 0)
2711 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002714#ifdef CONFIG_INTR_REMAP
2715 if (intr_remapping_enabled)
2716 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2717#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002718 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002719 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002720 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2721 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002723 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2724 "(IRQ0) through the 8259A ...\n");
2725 apic_printk(APIC_QUIET, KERN_INFO
2726 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * legacy devices should be connected to IO APIC #0
2729 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002730 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002731 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002732 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002733 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002735 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002736 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002738 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002740 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002742 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 /*
2745 * Cleanup, just in case ...
2746 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002747 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002748 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002749 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002753 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2754 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002755 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002757#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002758 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002761 apic_printk(APIC_QUIET, KERN_INFO
2762 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Yinghai Lu497c9a12008-08-19 20:50:28 -07002764 lapic_register_intr(0);
2765 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 enable_8259A_irq(0);
2767
2768 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002769 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002772 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002773 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002774 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002776 apic_printk(APIC_QUIET, KERN_INFO
2777 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 init_8259A(0);
2780 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002781 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 unlock_ExtINT_logic();
2784
2785 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002786 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002789 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002791 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002792out:
2793 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002797 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2798 * to devices. However there may be an I/O APIC pin available for
2799 * this interrupt regardless. The pin may be left unconnected, but
2800 * typically it will be reused as an ExtINT cascade interrupt for
2801 * the master 8259A. In the MPS case such a pin will normally be
2802 * reported as an ExtINT interrupt in the MP table. With ACPI
2803 * there is no provision for ExtINT interrupts, and in the absence
2804 * of an override it would be treated as an ordinary ISA I/O APIC
2805 * interrupt, that is edge-triggered and unmasked by default. We
2806 * used to do this, but it caused problems on some systems because
2807 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2808 * the same ExtINT cascade interrupt to drive the local APIC of the
2809 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2810 * the I/O APIC in all cases now. No actual device should request
2811 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 */
2813#define PIC_IRQS (1 << PIC_CASCADE_IR)
2814
2815void __init setup_IO_APIC(void)
2816{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002817
2818#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002820#else
2821 /*
2822 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2823 */
2824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002826 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Ingo Molnar54168ed2008-08-20 09:07:45 +02002828 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2829 /*
2830 * Set up IO-APIC IRQ routing.
2831 */
2832#ifdef CONFIG_X86_32
2833 if (!acpi_ioapic)
2834 setup_ioapic_ids_from_mpc();
2835#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 sync_Arb_IDs();
2837 setup_IO_APIC_irqs();
2838 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002839 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002843 * Called after all the initialization is done. If we didnt find any
2844 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847static int __init io_apic_bug_finalize(void)
2848{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002849 if (sis_apic_bug == -1)
2850 sis_apic_bug = 0;
2851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
2854late_initcall(io_apic_bug_finalize);
2855
2856struct sysfs_ioapic_data {
2857 struct sys_device dev;
2858 struct IO_APIC_route_entry entry[0];
2859};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002860static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Pavel Machek438510f2005-04-16 15:25:24 -07002862static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct IO_APIC_route_entry *entry;
2865 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 data = container_of(dev, struct sysfs_ioapic_data, dev);
2869 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002870 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2871 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 return 0;
2874}
2875
2876static int ioapic_resume(struct sys_device *dev)
2877{
2878 struct IO_APIC_route_entry *entry;
2879 struct sysfs_ioapic_data *data;
2880 unsigned long flags;
2881 union IO_APIC_reg_00 reg_00;
2882 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 data = container_of(dev, struct sysfs_ioapic_data, dev);
2885 entry = data->entry;
2886
2887 spin_lock_irqsave(&ioapic_lock, flags);
2888 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002889 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2890 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 io_apic_write(dev->id, 0, reg_00.raw);
2892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002894 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002895 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 return 0;
2898}
2899
2900static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002901 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 .suspend = ioapic_suspend,
2903 .resume = ioapic_resume,
2904};
2905
2906static int __init ioapic_init_sysfs(void)
2907{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002908 struct sys_device * dev;
2909 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 error = sysdev_class_register(&ioapic_sysdev_class);
2912 if (error)
2913 return error;
2914
Ingo Molnar54168ed2008-08-20 09:07:45 +02002915 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002916 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002918 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (!mp_ioapic_data[i]) {
2920 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2921 continue;
2922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002924 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 dev->cls = &ioapic_sysdev_class;
2926 error = sysdev_register(dev);
2927 if (error) {
2928 kfree(mp_ioapic_data[i]);
2929 mp_ioapic_data[i] = NULL;
2930 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2931 continue;
2932 }
2933 }
2934
2935 return 0;
2936}
2937
2938device_initcall(ioapic_init_sysfs);
2939
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002940/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002941 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002942 */
Yinghai Lu199751d2008-08-19 20:50:27 -07002943unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002944{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002945 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002946 unsigned int irq;
2947 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002948 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07002949 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002950
Yinghai Lu199751d2008-08-19 20:50:27 -07002951 irq_want = nr_irqs - 1;
2952
2953 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002954 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002955 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002956 if (platform_legacy_irq(new))
2957 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002958 cfg_new = irq_cfg(new);
2959 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002960 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002961 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07002962 if (!cfg_new)
2963 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002964 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002965 irq = new;
2966 break;
2967 }
2968 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002969
Yinghai Lu199751d2008-08-19 20:50:27 -07002970 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002971 dynamic_irq_init(irq);
2972 }
2973 return irq;
2974}
2975
Yinghai Lu199751d2008-08-19 20:50:27 -07002976int create_irq(void)
2977{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002978 int irq;
2979
2980 irq = create_irq_nr(nr_irqs - 1);
2981
2982 if (irq == 0)
2983 irq = -1;
2984
2985 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07002986}
2987
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002988void destroy_irq(unsigned int irq)
2989{
2990 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002991
2992 dynamic_irq_cleanup(irq);
2993
Ingo Molnar54168ed2008-08-20 09:07:45 +02002994#ifdef CONFIG_INTR_REMAP
2995 free_irte(irq);
2996#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002997 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002998 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002999 spin_unlock_irqrestore(&vector_lock, flags);
3000}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003001
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003002/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003003 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003004 */
3005#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003006static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003007{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003008 struct irq_cfg *cfg;
3009 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003010 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003011 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003012
Yinghai Lu497c9a12008-08-19 20:50:28 -07003013 tmp = TARGET_CPUS;
3014 err = assign_irq_vector(irq, tmp);
3015 if (err)
3016 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003017
Yinghai Lu497c9a12008-08-19 20:50:28 -07003018 cfg = irq_cfg(irq);
3019 cpus_and(tmp, cfg->domain, tmp);
3020 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003021
Ingo Molnar54168ed2008-08-20 09:07:45 +02003022#ifdef CONFIG_INTR_REMAP
3023 if (irq_remapped(irq)) {
3024 struct irte irte;
3025 int ir_index;
3026 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003027
Ingo Molnar54168ed2008-08-20 09:07:45 +02003028 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3029 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003030
Ingo Molnar54168ed2008-08-20 09:07:45 +02003031 memset (&irte, 0, sizeof(irte));
3032
3033 irte.present = 1;
3034 irte.dst_mode = INT_DEST_MODE;
3035 irte.trigger_mode = 0; /* edge */
3036 irte.dlvry_mode = INT_DELIVERY_MODE;
3037 irte.vector = cfg->vector;
3038 irte.dest_id = IRTE_DEST(dest);
3039
3040 modify_irte(irq, &irte);
3041
3042 msg->address_hi = MSI_ADDR_BASE_HI;
3043 msg->data = sub_handle;
3044 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3045 MSI_ADDR_IR_SHV |
3046 MSI_ADDR_IR_INDEX1(ir_index) |
3047 MSI_ADDR_IR_INDEX2(ir_index);
3048 } else
3049#endif
3050 {
3051 msg->address_hi = MSI_ADDR_BASE_HI;
3052 msg->address_lo =
3053 MSI_ADDR_BASE_LO |
3054 ((INT_DEST_MODE == 0) ?
3055 MSI_ADDR_DEST_MODE_PHYSICAL:
3056 MSI_ADDR_DEST_MODE_LOGICAL) |
3057 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3058 MSI_ADDR_REDIRECTION_CPU:
3059 MSI_ADDR_REDIRECTION_LOWPRI) |
3060 MSI_ADDR_DEST_ID(dest);
3061
3062 msg->data =
3063 MSI_DATA_TRIGGER_EDGE |
3064 MSI_DATA_LEVEL_ASSERT |
3065 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3066 MSI_DATA_DELIVERY_FIXED:
3067 MSI_DATA_DELIVERY_LOWPRI) |
3068 MSI_DATA_VECTOR(cfg->vector);
3069 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003070 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003071}
3072
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003073#ifdef CONFIG_SMP
3074static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3075{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003076 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003077 struct msi_msg msg;
3078 unsigned int dest;
3079 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003080 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003081
3082 cpus_and(tmp, mask, cpu_online_map);
3083 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003084 return;
3085
Yinghai Lu497c9a12008-08-19 20:50:28 -07003086 if (assign_irq_vector(irq, mask))
3087 return;
3088
3089 cfg = irq_cfg(irq);
3090 cpus_and(tmp, cfg->domain, mask);
3091 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003092
3093 read_msi_msg(irq, &msg);
3094
3095 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003096 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003097 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3098 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3099
3100 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003101 desc = irq_to_desc(irq);
3102 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003103}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003104
3105#ifdef CONFIG_INTR_REMAP
3106/*
3107 * Migrate the MSI irq to another cpumask. This migration is
3108 * done in the process context using interrupt-remapping hardware.
3109 */
3110static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3111{
3112 struct irq_cfg *cfg;
3113 unsigned int dest;
3114 cpumask_t tmp, cleanup_mask;
3115 struct irte irte;
3116 struct irq_desc *desc;
3117
3118 cpus_and(tmp, mask, cpu_online_map);
3119 if (cpus_empty(tmp))
3120 return;
3121
3122 if (get_irte(irq, &irte))
3123 return;
3124
3125 if (assign_irq_vector(irq, mask))
3126 return;
3127
3128 cfg = irq_cfg(irq);
3129 cpus_and(tmp, cfg->domain, mask);
3130 dest = cpu_mask_to_apicid(tmp);
3131
3132 irte.vector = cfg->vector;
3133 irte.dest_id = IRTE_DEST(dest);
3134
3135 /*
3136 * atomically update the IRTE with the new destination and vector.
3137 */
3138 modify_irte(irq, &irte);
3139
3140 /*
3141 * After this point, all the interrupts will start arriving
3142 * at the new destination. So, time to cleanup the previous
3143 * vector allocation.
3144 */
3145 if (cfg->move_in_progress) {
3146 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3147 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3148 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3149 cfg->move_in_progress = 0;
3150 }
3151
3152 desc = irq_to_desc(irq);
3153 desc->affinity = mask;
3154}
3155#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003156#endif /* CONFIG_SMP */
3157
3158/*
3159 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3160 * which implement the MSI or MSI-X Capability Structure.
3161 */
3162static struct irq_chip msi_chip = {
3163 .name = "PCI-MSI",
3164 .unmask = unmask_msi_irq,
3165 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003166 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003167#ifdef CONFIG_SMP
3168 .set_affinity = set_msi_irq_affinity,
3169#endif
3170 .retrigger = ioapic_retrigger_irq,
3171};
3172
Ingo Molnar54168ed2008-08-20 09:07:45 +02003173#ifdef CONFIG_INTR_REMAP
3174static struct irq_chip msi_ir_chip = {
3175 .name = "IR-PCI-MSI",
3176 .unmask = unmask_msi_irq,
3177 .mask = mask_msi_irq,
3178 .ack = ack_x2apic_edge,
3179#ifdef CONFIG_SMP
3180 .set_affinity = ir_set_msi_irq_affinity,
3181#endif
3182 .retrigger = ioapic_retrigger_irq,
3183};
3184
3185/*
3186 * Map the PCI dev to the corresponding remapping hardware unit
3187 * and allocate 'nvec' consecutive interrupt-remapping table entries
3188 * in it.
3189 */
3190static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3191{
3192 struct intel_iommu *iommu;
3193 int index;
3194
3195 iommu = map_dev_to_ir(dev);
3196 if (!iommu) {
3197 printk(KERN_ERR
3198 "Unable to map PCI %s to iommu\n", pci_name(dev));
3199 return -ENOENT;
3200 }
3201
3202 index = alloc_irte(iommu, irq, nvec);
3203 if (index < 0) {
3204 printk(KERN_ERR
3205 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3206 pci_name(dev));
3207 return -ENOSPC;
3208 }
3209 return index;
3210}
3211#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003212
3213static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3214{
3215 int ret;
3216 struct msi_msg msg;
3217
3218 ret = msi_compose_msg(dev, irq, &msg);
3219 if (ret < 0)
3220 return ret;
3221
3222 set_irq_msi(irq, desc);
3223 write_msi_msg(irq, &msg);
3224
Ingo Molnar54168ed2008-08-20 09:07:45 +02003225#ifdef CONFIG_INTR_REMAP
3226 if (irq_remapped(irq)) {
3227 struct irq_desc *desc = irq_to_desc(irq);
3228 /*
3229 * irq migration in process context
3230 */
3231 desc->status |= IRQ_MOVE_PCNTXT;
3232 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3233 } else
3234#endif
3235 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003236
Yinghai Luc81bba42008-09-25 11:53:11 -07003237 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3238
Yinghai Lu1d025192008-08-19 20:50:34 -07003239 return 0;
3240}
3241
Yinghai Lu199751d2008-08-19 20:50:27 -07003242static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3243{
3244 unsigned int irq;
3245
3246 irq = dev->bus->number;
3247 irq <<= 8;
3248 irq |= dev->devfn;
3249 irq <<= 12;
3250
3251 return irq;
3252}
3253
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003254int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003255{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003256 unsigned int irq;
3257 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003258 unsigned int irq_want;
3259
3260 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3261
3262 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003263 if (irq == 0)
3264 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003265
Ingo Molnar54168ed2008-08-20 09:07:45 +02003266#ifdef CONFIG_INTR_REMAP
3267 if (!intr_remapping_enabled)
3268 goto no_ir;
3269
3270 ret = msi_alloc_irte(dev, irq, 1);
3271 if (ret < 0)
3272 goto error;
3273no_ir:
3274#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003275 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003276 if (ret < 0) {
3277 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003278 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003279 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003280 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003281
3282#ifdef CONFIG_INTR_REMAP
3283error:
3284 destroy_irq(irq);
3285 return ret;
3286#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003287}
3288
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003289int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3290{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003291 unsigned int irq;
3292 int ret, sub_handle;
3293 struct msi_desc *desc;
3294 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003295
Ingo Molnar54168ed2008-08-20 09:07:45 +02003296#ifdef CONFIG_INTR_REMAP
3297 struct intel_iommu *iommu = 0;
3298 int index = 0;
3299#endif
3300
3301 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3302 sub_handle = 0;
3303 list_for_each_entry(desc, &dev->msi_list, list) {
3304 irq = create_irq_nr(irq_want--);
3305 if (irq == 0)
3306 return -1;
3307#ifdef CONFIG_INTR_REMAP
3308 if (!intr_remapping_enabled)
3309 goto no_ir;
3310
3311 if (!sub_handle) {
3312 /*
3313 * allocate the consecutive block of IRTE's
3314 * for 'nvec'
3315 */
3316 index = msi_alloc_irte(dev, irq, nvec);
3317 if (index < 0) {
3318 ret = index;
3319 goto error;
3320 }
3321 } else {
3322 iommu = map_dev_to_ir(dev);
3323 if (!iommu) {
3324 ret = -ENOENT;
3325 goto error;
3326 }
3327 /*
3328 * setup the mapping between the irq and the IRTE
3329 * base index, the sub_handle pointing to the
3330 * appropriate interrupt remap table entry.
3331 */
3332 set_irte_irq(irq, iommu, index, sub_handle);
3333 }
3334no_ir:
3335#endif
3336 ret = setup_msi_irq(dev, desc, irq);
3337 if (ret < 0)
3338 goto error;
3339 sub_handle++;
3340 }
3341 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003342
3343error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003344 destroy_irq(irq);
3345 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003346}
3347
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003348void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003349{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003350 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003351}
3352
Ingo Molnar54168ed2008-08-20 09:07:45 +02003353#ifdef CONFIG_DMAR
3354#ifdef CONFIG_SMP
3355static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3356{
3357 struct irq_cfg *cfg;
3358 struct msi_msg msg;
3359 unsigned int dest;
3360 cpumask_t tmp;
3361 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003362
Ingo Molnar54168ed2008-08-20 09:07:45 +02003363 cpus_and(tmp, mask, cpu_online_map);
3364 if (cpus_empty(tmp))
3365 return;
3366
3367 if (assign_irq_vector(irq, mask))
3368 return;
3369
3370 cfg = irq_cfg(irq);
3371 cpus_and(tmp, cfg->domain, mask);
3372 dest = cpu_mask_to_apicid(tmp);
3373
3374 dmar_msi_read(irq, &msg);
3375
3376 msg.data &= ~MSI_DATA_VECTOR_MASK;
3377 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3378 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3379 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3380
3381 dmar_msi_write(irq, &msg);
3382 desc = irq_to_desc(irq);
3383 desc->affinity = mask;
3384}
3385#endif /* CONFIG_SMP */
3386
3387struct irq_chip dmar_msi_type = {
3388 .name = "DMAR_MSI",
3389 .unmask = dmar_msi_unmask,
3390 .mask = dmar_msi_mask,
3391 .ack = ack_apic_edge,
3392#ifdef CONFIG_SMP
3393 .set_affinity = dmar_msi_set_affinity,
3394#endif
3395 .retrigger = ioapic_retrigger_irq,
3396};
3397
3398int arch_setup_dmar_msi(unsigned int irq)
3399{
3400 int ret;
3401 struct msi_msg msg;
3402
3403 ret = msi_compose_msg(NULL, irq, &msg);
3404 if (ret < 0)
3405 return ret;
3406 dmar_msi_write(irq, &msg);
3407 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3408 "edge");
3409 return 0;
3410}
3411#endif
3412
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003413#ifdef CONFIG_HPET_TIMER
3414
3415#ifdef CONFIG_SMP
3416static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3417{
3418 struct irq_cfg *cfg;
3419 struct irq_desc *desc;
3420 struct msi_msg msg;
3421 unsigned int dest;
3422 cpumask_t tmp;
3423
3424 cpus_and(tmp, mask, cpu_online_map);
3425 if (cpus_empty(tmp))
3426 return;
3427
3428 if (assign_irq_vector(irq, mask))
3429 return;
3430
3431 cfg = irq_cfg(irq);
3432 cpus_and(tmp, cfg->domain, mask);
3433 dest = cpu_mask_to_apicid(tmp);
3434
3435 hpet_msi_read(irq, &msg);
3436
3437 msg.data &= ~MSI_DATA_VECTOR_MASK;
3438 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3439 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3440 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3441
3442 hpet_msi_write(irq, &msg);
3443 desc = irq_to_desc(irq);
3444 desc->affinity = mask;
3445}
3446#endif /* CONFIG_SMP */
3447
3448struct irq_chip hpet_msi_type = {
3449 .name = "HPET_MSI",
3450 .unmask = hpet_msi_unmask,
3451 .mask = hpet_msi_mask,
3452 .ack = ack_apic_edge,
3453#ifdef CONFIG_SMP
3454 .set_affinity = hpet_msi_set_affinity,
3455#endif
3456 .retrigger = ioapic_retrigger_irq,
3457};
3458
3459int arch_setup_hpet_msi(unsigned int irq)
3460{
3461 int ret;
3462 struct msi_msg msg;
3463
3464 ret = msi_compose_msg(NULL, irq, &msg);
3465 if (ret < 0)
3466 return ret;
3467
3468 hpet_msi_write(irq, &msg);
3469 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3470 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003471
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003472 return 0;
3473}
3474#endif
3475
Ingo Molnar54168ed2008-08-20 09:07:45 +02003476#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003477/*
3478 * Hypertransport interrupt support
3479 */
3480#ifdef CONFIG_HT_IRQ
3481
3482#ifdef CONFIG_SMP
3483
Yinghai Lu497c9a12008-08-19 20:50:28 -07003484static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003485{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003486 struct ht_irq_msg msg;
3487 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003488
Yinghai Lu497c9a12008-08-19 20:50:28 -07003489 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003490 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003491
Yinghai Lu497c9a12008-08-19 20:50:28 -07003492 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003493 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003494
Eric W. Biedermanec683072006-11-08 17:44:57 -08003495 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003496}
3497
3498static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3499{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003500 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003501 unsigned int dest;
3502 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003503 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003504
3505 cpus_and(tmp, mask, cpu_online_map);
3506 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003507 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003508
Yinghai Lu497c9a12008-08-19 20:50:28 -07003509 if (assign_irq_vector(irq, mask))
3510 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003511
Yinghai Lu497c9a12008-08-19 20:50:28 -07003512 cfg = irq_cfg(irq);
3513 cpus_and(tmp, cfg->domain, mask);
3514 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003515
Yinghai Lu497c9a12008-08-19 20:50:28 -07003516 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003517 desc = irq_to_desc(irq);
3518 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003519}
3520#endif
3521
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003522static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003523 .name = "PCI-HT",
3524 .mask = mask_ht_irq,
3525 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003526 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003527#ifdef CONFIG_SMP
3528 .set_affinity = set_ht_irq_affinity,
3529#endif
3530 .retrigger = ioapic_retrigger_irq,
3531};
3532
3533int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3534{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003535 struct irq_cfg *cfg;
3536 int err;
3537 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003538
Yinghai Lu497c9a12008-08-19 20:50:28 -07003539 tmp = TARGET_CPUS;
3540 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003541 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003542 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003543 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003544
Yinghai Lu497c9a12008-08-19 20:50:28 -07003545 cfg = irq_cfg(irq);
3546 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003547 dest = cpu_mask_to_apicid(tmp);
3548
Eric W. Biedermanec683072006-11-08 17:44:57 -08003549 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003550
Eric W. Biedermanec683072006-11-08 17:44:57 -08003551 msg.address_lo =
3552 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003553 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003554 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003555 ((INT_DEST_MODE == 0) ?
3556 HT_IRQ_LOW_DM_PHYSICAL :
3557 HT_IRQ_LOW_DM_LOGICAL) |
3558 HT_IRQ_LOW_RQEOI_EDGE |
3559 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3560 HT_IRQ_LOW_MT_FIXED :
3561 HT_IRQ_LOW_MT_ARBITRATED) |
3562 HT_IRQ_LOW_IRQ_MASKED;
3563
Eric W. Biedermanec683072006-11-08 17:44:57 -08003564 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003565
Ingo Molnara460e742006-10-17 00:10:03 -07003566 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3567 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003568
3569 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003570 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003571 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003572}
3573#endif /* CONFIG_HT_IRQ */
3574
Dean Nelson4173a0e2008-10-02 12:18:21 -05003575#ifdef CONFIG_X86_64
3576/*
3577 * Re-target the irq to the specified CPU and enable the specified MMR located
3578 * on the specified blade to allow the sending of MSIs to the specified CPU.
3579 */
3580int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3581 unsigned long mmr_offset)
3582{
3583 const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
3584 struct irq_cfg *cfg;
3585 int mmr_pnode;
3586 unsigned long mmr_value;
3587 struct uv_IO_APIC_route_entry *entry;
3588 unsigned long flags;
3589 int err;
3590
3591 err = assign_irq_vector(irq, *eligible_cpu);
3592 if (err != 0)
3593 return err;
3594
3595 spin_lock_irqsave(&vector_lock, flags);
3596 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3597 irq_name);
3598 spin_unlock_irqrestore(&vector_lock, flags);
3599
3600 cfg = irq_cfg(irq);
3601
3602 mmr_value = 0;
3603 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3604 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3605
3606 entry->vector = cfg->vector;
3607 entry->delivery_mode = INT_DELIVERY_MODE;
3608 entry->dest_mode = INT_DEST_MODE;
3609 entry->polarity = 0;
3610 entry->trigger = 0;
3611 entry->mask = 0;
3612 entry->dest = cpu_mask_to_apicid(*eligible_cpu);
3613
3614 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3615 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3616
3617 return irq;
3618}
3619
3620/*
3621 * Disable the specified MMR located on the specified blade so that MSIs are
3622 * longer allowed to be sent.
3623 */
3624void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3625{
3626 unsigned long mmr_value;
3627 struct uv_IO_APIC_route_entry *entry;
3628 int mmr_pnode;
3629
3630 mmr_value = 0;
3631 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3632 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3633
3634 entry->mask = 1;
3635
3636 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3637 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3638}
3639#endif /* CONFIG_X86_64 */
3640
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003641int __init io_apic_get_redir_entries (int ioapic)
3642{
3643 union IO_APIC_reg_01 reg_01;
3644 unsigned long flags;
3645
3646 spin_lock_irqsave(&ioapic_lock, flags);
3647 reg_01.raw = io_apic_read(ioapic, 1);
3648 spin_unlock_irqrestore(&ioapic_lock, flags);
3649
3650 return reg_01.bits.entries;
3651}
3652
3653int __init probe_nr_irqs(void)
3654{
3655 int idx;
3656 int nr = 0;
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003657#ifndef CONFIG_XEN
3658 int nr_min = 32;
3659#else
3660 int nr_min = NR_IRQS;
3661#endif
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003662
3663 for (idx = 0; idx < nr_ioapics; idx++)
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003664 nr += io_apic_get_redir_entries(idx) + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003665
3666 /* double it for hotplug and msi and nmi */
3667 nr <<= 1;
3668
3669 /* something wrong ? */
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003670 if (nr < nr_min)
3671 nr = nr_min;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003672
3673 return nr;
3674}
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003677 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 -------------------------------------------------------------------------- */
3679
Len Brown888ba6c2005-08-24 12:07:20 -04003680#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Ingo Molnar54168ed2008-08-20 09:07:45 +02003682#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003683int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
3685 union IO_APIC_reg_00 reg_00;
3686 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3687 physid_mask_t tmp;
3688 unsigned long flags;
3689 int i = 0;
3690
3691 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003692 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3693 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3697 * advantage of new APIC bus architecture.
3698 */
3699
3700 if (physids_empty(apic_id_map))
3701 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3702
3703 spin_lock_irqsave(&ioapic_lock, flags);
3704 reg_00.raw = io_apic_read(ioapic, 0);
3705 spin_unlock_irqrestore(&ioapic_lock, flags);
3706
3707 if (apic_id >= get_physical_broadcast()) {
3708 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3709 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3710 apic_id = reg_00.bits.ID;
3711 }
3712
3713 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003714 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 * 'stuck on smp_invalidate_needed IPI wait' messages.
3716 */
3717 if (check_apicid_used(apic_id_map, apic_id)) {
3718
3719 for (i = 0; i < get_physical_broadcast(); i++) {
3720 if (!check_apicid_used(apic_id_map, i))
3721 break;
3722 }
3723
3724 if (i == get_physical_broadcast())
3725 panic("Max apic_id exceeded!\n");
3726
3727 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3728 "trying %d\n", ioapic, apic_id, i);
3729
3730 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 tmp = apicid_to_cpu_present(apic_id);
3734 physids_or(apic_id_map, apic_id_map, tmp);
3735
3736 if (reg_00.bits.ID != apic_id) {
3737 reg_00.bits.ID = apic_id;
3738
3739 spin_lock_irqsave(&ioapic_lock, flags);
3740 io_apic_write(ioapic, 0, reg_00.raw);
3741 reg_00.raw = io_apic_read(ioapic, 0);
3742 spin_unlock_irqrestore(&ioapic_lock, flags);
3743
3744 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003745 if (reg_00.bits.ID != apic_id) {
3746 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3747 return -1;
3748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 }
3750
3751 apic_printk(APIC_VERBOSE, KERN_INFO
3752 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3753
3754 return apic_id;
3755}
3756
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003757int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
3759 union IO_APIC_reg_01 reg_01;
3760 unsigned long flags;
3761
3762 spin_lock_irqsave(&ioapic_lock, flags);
3763 reg_01.raw = io_apic_read(ioapic, 1);
3764 spin_unlock_irqrestore(&ioapic_lock, flags);
3765
3766 return reg_01.bits.version;
3767}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Ingo Molnar54168ed2008-08-20 09:07:45 +02003770int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003773 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 ioapic);
3775 return -EINVAL;
3776 }
3777
3778 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 * IRQs < 16 are already in the irq_2_pin[] map
3780 */
3781 if (irq >= 16)
3782 add_pin_to_irq(irq, ioapic, pin);
3783
Yinghai Lu497c9a12008-08-19 20:50:28 -07003784 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 return 0;
3787}
3788
Ingo Molnar54168ed2008-08-20 09:07:45 +02003789
Shaohua Li61fd47e2007-11-17 01:05:28 -05003790int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3791{
3792 int i;
3793
3794 if (skip_ioapic_setup)
3795 return -1;
3796
3797 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003798 if (mp_irqs[i].mp_irqtype == mp_INT &&
3799 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003800 break;
3801 if (i >= mp_irq_entries)
3802 return -1;
3803
3804 *trigger = irq_trigger(i);
3805 *polarity = irq_polarity(i);
3806 return 0;
3807}
3808
Len Brown888ba6c2005-08-24 12:07:20 -04003809#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003810
Yinghai Lu497c9a12008-08-19 20:50:28 -07003811/*
3812 * This function currently is only a helper for the i386 smp boot process where
3813 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3814 * so mask in all cases should simply be TARGET_CPUS
3815 */
3816#ifdef CONFIG_SMP
3817void __init setup_ioapic_dest(void)
3818{
3819 int pin, ioapic, irq, irq_entry;
3820 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003821
3822 if (skip_ioapic_setup == 1)
3823 return;
3824
3825 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3826 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3827 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3828 if (irq_entry == -1)
3829 continue;
3830 irq = pin_2_irq(irq_entry, ioapic, pin);
3831
3832 /* setup_IO_APIC_irqs could fail to get vector for some device
3833 * when you have too many devices, because at that time only boot
3834 * cpu is online.
3835 */
3836 cfg = irq_cfg(irq);
3837 if (!cfg->vector)
3838 setup_IO_APIC_irq(ioapic, pin, irq,
3839 irq_trigger(irq_entry),
3840 irq_polarity(irq_entry));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003841#ifdef CONFIG_INTR_REMAP
3842 else if (intr_remapping_enabled)
3843 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3844#endif
3845 else
Yinghai Lu497c9a12008-08-19 20:50:28 -07003846 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003847 }
3848
3849 }
3850}
3851#endif
3852
Ingo Molnar54168ed2008-08-20 09:07:45 +02003853#define IOAPIC_RESOURCE_NAME_SIZE 11
3854
3855static struct resource *ioapic_resources;
3856
3857static struct resource * __init ioapic_setup_resources(void)
3858{
3859 unsigned long n;
3860 struct resource *res;
3861 char *mem;
3862 int i;
3863
3864 if (nr_ioapics <= 0)
3865 return NULL;
3866
3867 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3868 n *= nr_ioapics;
3869
3870 mem = alloc_bootmem(n);
3871 res = (void *)mem;
3872
3873 if (mem != NULL) {
3874 mem += sizeof(struct resource) * nr_ioapics;
3875
3876 for (i = 0; i < nr_ioapics; i++) {
3877 res[i].name = mem;
3878 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3879 sprintf(mem, "IOAPIC %u", i);
3880 mem += IOAPIC_RESOURCE_NAME_SIZE;
3881 }
3882 }
3883
3884 ioapic_resources = res;
3885
3886 return res;
3887}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003888
Yinghai Luf3294a32008-06-27 01:41:56 -07003889void __init ioapic_init_mappings(void)
3890{
3891 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3892 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003893 struct resource *ioapic_res;
Yinghai Luf3294a32008-06-27 01:41:56 -07003894
Ingo Molnar54168ed2008-08-20 09:07:45 +02003895 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003896 for (i = 0; i < nr_ioapics; i++) {
3897 if (smp_found_config) {
3898 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003899#ifdef CONFIG_X86_32
3900 if (!ioapic_phys) {
3901 printk(KERN_ERR
3902 "WARNING: bogus zero IO-APIC "
3903 "address found in MPTABLE, "
3904 "disabling IO/APIC support!\n");
3905 smp_found_config = 0;
3906 skip_ioapic_setup = 1;
3907 goto fake_ioapic_page;
3908 }
3909#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003910 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003911#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003912fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003913#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003914 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003915 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003916 ioapic_phys = __pa(ioapic_phys);
3917 }
3918 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003919 apic_printk(APIC_VERBOSE,
3920 "mapped IOAPIC to %08lx (%08lx)\n",
3921 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003922 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003923
Ingo Molnar54168ed2008-08-20 09:07:45 +02003924 if (ioapic_res != NULL) {
3925 ioapic_res->start = ioapic_phys;
3926 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3927 ioapic_res++;
3928 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003929 }
3930}
3931
Ingo Molnar54168ed2008-08-20 09:07:45 +02003932static int __init ioapic_insert_resources(void)
3933{
3934 int i;
3935 struct resource *r = ioapic_resources;
3936
3937 if (!r) {
3938 printk(KERN_ERR
3939 "IO APIC resources could be not be allocated.\n");
3940 return -1;
3941 }
3942
3943 for (i = 0; i < nr_ioapics; i++) {
3944 insert_resource(&iomem_resource, r);
3945 r++;
3946 }
3947
3948 return 0;
3949}
3950
3951/* Insert the IO APIC resources after PCI initialization has occured to handle
3952 * IO APICS that are mapped in on a BAR in PCI space. */
3953late_initcall(ioapic_insert_resources);