blob: dc69f28489f5bc9a2873a8d22b084bec313d9912 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053049#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070051#include <asm/proto.h>
52#include <asm/acpi.h>
53#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070055#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020056#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070057#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070058#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070059#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070060#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070061#include <asm/hpet.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053062#include <asm/hw_irq.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050063#include <asm/uv/uv_hub.h>
64#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ingo Molnar7b6aa332009-02-17 13:58:15 +010066#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +040069#define for_each_irq_pin(entry, head) \
70 for (entry = head; entry; entry = entry->next)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020073 * Is the SiS APIC rmw bug present ?
74 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76int sis_apic_bug = -1;
77
Yinghai Luefa25592008-08-19 20:50:36 -070078static DEFINE_SPINLOCK(ioapic_lock);
79static DEFINE_SPINLOCK(vector_lock);
80
Yinghai Luefa25592008-08-19 20:50:36 -070081/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * # of IRQ routing registers
83 */
84int nr_ioapic_registers[MAX_IO_APICS];
85
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +053087struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040088int nr_ioapics;
89
Feng Tang2a4ab642009-07-07 23:01:15 -040090/* IO APIC gsi routing info */
91struct mp_ioapic_gsi mp_gsi_routing[MAX_IO_APICS];
92
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040093/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053094struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040095
96/* # of MP IRQ source entries */
97int mp_irq_entries;
98
Thomas Gleixnerbc078442009-08-29 18:09:57 +020099/* Number of legacy interrupts */
100static int nr_legacy_irqs __read_mostly = NR_IRQS_LEGACY;
101/* GSI interrupts */
102static int nr_irqs_gsi = NR_IRQS_LEGACY;
103
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +0400104#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
105int mp_bus_id_to_type[MAX_MP_BUSSES];
106#endif
107
108DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
109
Yinghai Luefa25592008-08-19 20:50:36 -0700110int skip_ioapic_setup;
111
Ingo Molnar65a4e572009-01-31 03:36:17 +0100112void arch_disable_smp_support(void)
113{
114#ifdef CONFIG_PCI
115 noioapicquirk = 1;
116 noioapicreroute = -1;
117#endif
118 skip_ioapic_setup = 1;
119}
120
Ingo Molnar54168ed2008-08-20 09:07:45 +0200121static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700122{
123 /* disable IO-APIC */
Ingo Molnar65a4e572009-01-31 03:36:17 +0100124 arch_disable_smp_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700125 return 0;
126}
127early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200128
Yinghai Lu0f978f42008-08-19 20:50:26 -0700129struct irq_pin_list {
130 int apic, pin;
131 struct irq_pin_list *next;
132};
Yinghai Lu301e6192008-08-19 20:50:02 -0700133
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700134static struct irq_pin_list *get_one_free_irq_2_pin(int node)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700135{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800136 struct irq_pin_list *pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700137
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800138 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700139
Yinghai Lu0f978f42008-08-19 20:50:26 -0700140 return pin;
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeremy Fitzhardinge8e13d692009-06-08 03:14:59 -0700143/*
144 * This is performance-critical, we want to do it O(1)
145 *
146 * Most irqs are mapped 1:1 with pins.
147 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800148struct irq_cfg {
149 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800150 cpumask_var_t domain;
151 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800152 unsigned move_cleanup_count;
153 u8 vector;
154 u8 move_in_progress : 1;
155};
156
157/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
158#ifdef CONFIG_SPARSE_IRQ
159static struct irq_cfg irq_cfgx[] = {
160#else
161static struct irq_cfg irq_cfgx[NR_IRQS] = {
162#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800163 [0] = { .vector = IRQ0_VECTOR, },
164 [1] = { .vector = IRQ1_VECTOR, },
165 [2] = { .vector = IRQ2_VECTOR, },
166 [3] = { .vector = IRQ3_VECTOR, },
167 [4] = { .vector = IRQ4_VECTOR, },
168 [5] = { .vector = IRQ5_VECTOR, },
169 [6] = { .vector = IRQ6_VECTOR, },
170 [7] = { .vector = IRQ7_VECTOR, },
171 [8] = { .vector = IRQ8_VECTOR, },
172 [9] = { .vector = IRQ9_VECTOR, },
173 [10] = { .vector = IRQ10_VECTOR, },
174 [11] = { .vector = IRQ11_VECTOR, },
175 [12] = { .vector = IRQ12_VECTOR, },
176 [13] = { .vector = IRQ13_VECTOR, },
177 [14] = { .vector = IRQ14_VECTOR, },
178 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800179};
180
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200181void __init io_apic_disable_legacy(void)
182{
183 nr_legacy_irqs = 0;
184 nr_irqs_gsi = 0;
185}
186
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800187int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800188{
189 struct irq_cfg *cfg;
190 struct irq_desc *desc;
191 int count;
Yinghai Ludad213a2009-05-28 18:14:40 -0700192 int node;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800193 int i;
194
195 cfg = irq_cfgx;
196 count = ARRAY_SIZE(irq_cfgx);
Yinghai Ludad213a2009-05-28 18:14:40 -0700197 node= cpu_to_node(boot_cpu_id);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800198
199 for (i = 0; i < count; i++) {
200 desc = irq_to_desc(i);
201 desc->chip_data = &cfg[i];
Yinghai Lu12274e92009-06-11 15:07:48 -0700202 zalloc_cpumask_var_node(&cfg[i].domain, GFP_NOWAIT, node);
203 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_NOWAIT, node);
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200204 if (i < nr_legacy_irqs)
Mike Travis22f65d32008-12-16 17:33:56 -0800205 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800206 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800207
208 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800209}
210
211#ifdef CONFIG_SPARSE_IRQ
212static struct irq_cfg *irq_cfg(unsigned int irq)
213{
214 struct irq_cfg *cfg = NULL;
215 struct irq_desc *desc;
216
217 desc = irq_to_desc(irq);
218 if (desc)
219 cfg = desc->chip_data;
220
221 return cfg;
222}
223
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700224static struct irq_cfg *get_one_free_irq_cfg(int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800225{
226 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800227
228 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800229 if (cfg) {
Li Zefan79f55992009-06-15 14:58:26 +0800230 if (!zalloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800231 kfree(cfg);
232 cfg = NULL;
Li Zefan79f55992009-06-15 14:58:26 +0800233 } else if (!zalloc_cpumask_var_node(&cfg->old_domain,
Mike Travis80855f72008-12-31 18:08:47 -0800234 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800235 free_cpumask_var(cfg->domain);
236 kfree(cfg);
237 cfg = NULL;
Mike Travis22f65d32008-12-16 17:33:56 -0800238 }
239 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800240
241 return cfg;
242}
243
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700244int arch_init_chip_data(struct irq_desc *desc, int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800245{
246 struct irq_cfg *cfg;
247
248 cfg = desc->chip_data;
249 if (!cfg) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700250 desc->chip_data = get_one_free_irq_cfg(node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800251 if (!desc->chip_data) {
252 printk(KERN_ERR "can not alloc irq_cfg\n");
253 BUG_ON(1);
254 }
255 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800256
257 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800258}
259
Yinghai Lufcef5912009-04-27 17:58:23 -0700260/* for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800261static void
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700262init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800263{
264 struct irq_pin_list *old_entry, *head, *tail, *entry;
265
266 cfg->irq_2_pin = NULL;
267 old_entry = old_cfg->irq_2_pin;
268 if (!old_entry)
269 return;
270
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700271 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800272 if (!entry)
273 return;
274
275 entry->apic = old_entry->apic;
276 entry->pin = old_entry->pin;
277 head = entry;
278 tail = entry;
279 old_entry = old_entry->next;
280 while (old_entry) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700281 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800282 if (!entry) {
283 entry = head;
284 while (entry) {
285 head = entry->next;
286 kfree(entry);
287 entry = head;
288 }
289 /* still use the old one */
290 return;
291 }
292 entry->apic = old_entry->apic;
293 entry->pin = old_entry->pin;
294 tail->next = entry;
295 tail = entry;
296 old_entry = old_entry->next;
297 }
298
299 tail->next = NULL;
300 cfg->irq_2_pin = head;
301}
302
303static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
304{
305 struct irq_pin_list *entry, *next;
306
307 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
308 return;
309
310 entry = old_cfg->irq_2_pin;
311
312 while (entry) {
313 next = entry->next;
314 kfree(entry);
315 entry = next;
316 }
317 old_cfg->irq_2_pin = NULL;
318}
319
320void arch_init_copy_chip_data(struct irq_desc *old_desc,
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700321 struct irq_desc *desc, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800322{
323 struct irq_cfg *cfg;
324 struct irq_cfg *old_cfg;
325
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700326 cfg = get_one_free_irq_cfg(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800327
328 if (!cfg)
329 return;
330
331 desc->chip_data = cfg;
332
333 old_cfg = old_desc->chip_data;
334
335 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
336
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700337 init_copy_irq_2_pin(old_cfg, cfg, node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800338}
339
340static void free_irq_cfg(struct irq_cfg *old_cfg)
341{
342 kfree(old_cfg);
343}
344
345void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
346{
347 struct irq_cfg *old_cfg, *cfg;
348
349 old_cfg = old_desc->chip_data;
350 cfg = desc->chip_data;
351
352 if (old_cfg == cfg)
353 return;
354
355 if (old_cfg) {
356 free_irq_2_pin(old_cfg, cfg);
357 free_irq_cfg(old_cfg);
358 old_desc->chip_data = NULL;
359 }
360}
Yinghai Lufcef5912009-04-27 17:58:23 -0700361/* end for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800362
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800363#else
364static struct irq_cfg *irq_cfg(unsigned int irq)
365{
366 return irq < nr_irqs ? irq_cfgx + irq : NULL;
367}
368
369#endif
370
Linus Torvalds130fe052006-11-01 09:11:00 -0800371struct io_apic {
372 unsigned int index;
373 unsigned int unused[3];
374 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700375 unsigned int unused2[11];
376 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800377};
378
379static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
380{
381 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +0530382 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800383}
384
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700385static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
386{
387 struct io_apic __iomem *io_apic = io_apic_base(apic);
388 writel(vector, &io_apic->eoi);
389}
390
Linus Torvalds130fe052006-11-01 09:11:00 -0800391static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
392{
393 struct io_apic __iomem *io_apic = io_apic_base(apic);
394 writel(reg, &io_apic->index);
395 return readl(&io_apic->data);
396}
397
398static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
399{
400 struct io_apic __iomem *io_apic = io_apic_base(apic);
401 writel(reg, &io_apic->index);
402 writel(value, &io_apic->data);
403}
404
405/*
406 * Re-write a value: to be used for read-modify-write
407 * cycles where the read already set up the index register.
408 *
409 * Older SiS APIC requires we rewrite the index register
410 */
411static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
412{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200413 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200414
415 if (sis_apic_bug)
416 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800417 writel(value, &io_apic->data);
418}
419
Yinghai Lu3145e942008-12-05 18:58:34 -0800420static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700421{
422 struct irq_pin_list *entry;
423 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700424
425 spin_lock_irqsave(&ioapic_lock, flags);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400426 for_each_irq_pin(entry, cfg->irq_2_pin) {
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700427 unsigned int reg;
428 int pin;
429
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700430 pin = entry->pin;
431 reg = io_apic_read(entry->apic, 0x10 + pin*2);
432 /* Is the remote IRR bit set? */
433 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
434 spin_unlock_irqrestore(&ioapic_lock, flags);
435 return true;
436 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700437 }
438 spin_unlock_irqrestore(&ioapic_lock, flags);
439
440 return false;
441}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700442
Andi Kleencf4c6a22006-09-26 10:52:30 +0200443union entry_union {
444 struct { u32 w1, w2; };
445 struct IO_APIC_route_entry entry;
446};
447
448static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
449{
450 union entry_union eu;
451 unsigned long flags;
452 spin_lock_irqsave(&ioapic_lock, flags);
453 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
454 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
455 spin_unlock_irqrestore(&ioapic_lock, flags);
456 return eu.entry;
457}
458
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800459/*
460 * When we write a new IO APIC routing entry, we need to write the high
461 * word first! If the mask bit in the low word is clear, we will enable
462 * the interrupt, and we need to make sure the entry is fully populated
463 * before that happens.
464 */
Andi Kleend15512f2006-12-07 02:14:07 +0100465static void
466__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
467{
Figo.zhang50a8d4d22009-06-17 22:25:20 +0800468 union entry_union eu = {{0, 0}};
469
Andi Kleend15512f2006-12-07 02:14:07 +0100470 eu.entry = e;
471 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
472 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
473}
474
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -0800475void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200476{
477 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200478 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100479 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800480 spin_unlock_irqrestore(&ioapic_lock, flags);
481}
482
483/*
484 * When we mask an IO APIC routing entry, we need to write the low
485 * word first, in order to set the mask bit before we change the
486 * high bits!
487 */
488static void ioapic_mask_entry(int apic, int pin)
489{
490 unsigned long flags;
491 union entry_union eu = { .entry.mask = 1 };
492
493 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200494 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
495 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
496 spin_unlock_irqrestore(&ioapic_lock, flags);
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/*
500 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
501 * shared ISA-space IRQs, so we have to support them. We are super
502 * fast in the common case, and fast for shared ISA-space IRQs.
503 */
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400504static int
505add_pin_to_irq_node_nopanic(struct irq_cfg *cfg, int node, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400507 struct irq_pin_list **last, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400509 /* don't allow duplicates */
510 last = &cfg->irq_2_pin;
511 for_each_irq_pin(entry, cfg->irq_2_pin) {
Yinghai Lu0f978f42008-08-19 20:50:26 -0700512 if (entry->apic == apic && entry->pin == pin)
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400513 return 0;
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400514 last = &entry->next;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700515 }
516
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700517 entry = get_one_free_irq_2_pin(node);
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400518 if (!entry) {
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400519 printk(KERN_ERR "can not alloc irq_pin_list (%d,%d,%d)\n",
520 node, apic, pin);
521 return -ENOMEM;
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 entry->apic = apic;
524 entry->pin = pin;
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700525
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400526 *last = entry;
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400527 return 0;
528}
529
530static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
531{
532 if (add_pin_to_irq_node_nopanic(cfg, node, apic, pin))
533 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * Reroute an IRQ to a different pin.
538 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700539static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700540 int oldapic, int oldpin,
541 int newapic, int newpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700543 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400545 for_each_irq_pin(entry, cfg->irq_2_pin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (entry->apic == oldapic && entry->pin == oldpin) {
547 entry->apic = newapic;
548 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700549 /* every one is different, right? */
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700550 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700553
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700554 /* old apic/pin didn't exist, so just add new ones */
555 add_pin_to_irq_node(cfg, node, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700558static void io_apic_modify_irq(struct irq_cfg *cfg,
559 int mask_and, int mask_or,
560 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400561{
562 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400563 struct irq_pin_list *entry;
564
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400565 for_each_irq_pin(entry, cfg->irq_2_pin) {
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400566 unsigned int reg;
567 pin = entry->pin;
568 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
569 reg &= mask_and;
570 reg |= mask_or;
571 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
572 if (final)
573 final(entry);
574 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700575}
576
Yinghai Lu3145e942008-12-05 18:58:34 -0800577static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400578{
Yinghai Lu3145e942008-12-05 18:58:34 -0800579 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400580}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700581
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530582static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700583{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400584 /*
585 * Synchronize the IO-APIC and the CPU by doing
586 * a dummy read from the IO-APIC
587 */
588 struct io_apic __iomem *io_apic;
589 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700590 readl(&io_apic->data);
591}
592
Yinghai Lu3145e942008-12-05 18:58:34 -0800593static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400594{
Yinghai Lu3145e942008-12-05 18:58:34 -0800595 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400596}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700597
Yinghai Lu3145e942008-12-05 18:58:34 -0800598static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400599{
Yinghai Lu3145e942008-12-05 18:58:34 -0800600 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400601 IO_APIC_REDIR_MASKED, NULL);
602}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700603
Yinghai Lu3145e942008-12-05 18:58:34 -0800604static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400605{
Yinghai Lu3145e942008-12-05 18:58:34 -0800606 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400607 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
608}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700609
Yinghai Lu3145e942008-12-05 18:58:34 -0800610static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Yinghai Lu3145e942008-12-05 18:58:34 -0800612 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 unsigned long flags;
614
Yinghai Lu3145e942008-12-05 18:58:34 -0800615 BUG_ON(!cfg);
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800618 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 spin_unlock_irqrestore(&ioapic_lock, flags);
620}
621
Yinghai Lu3145e942008-12-05 18:58:34 -0800622static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Yinghai Lu3145e942008-12-05 18:58:34 -0800624 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned long flags;
626
627 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800628 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 spin_unlock_irqrestore(&ioapic_lock, flags);
630}
631
Yinghai Lu3145e942008-12-05 18:58:34 -0800632static void mask_IO_APIC_irq(unsigned int irq)
633{
634 struct irq_desc *desc = irq_to_desc(irq);
635
636 mask_IO_APIC_irq_desc(desc);
637}
638static void unmask_IO_APIC_irq(unsigned int irq)
639{
640 struct irq_desc *desc = irq_to_desc(irq);
641
642 unmask_IO_APIC_irq_desc(desc);
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
646{
647 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200650 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (entry.delivery_mode == dest_SMI)
652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * Disable it in the IO-APIC irq-routing table:
655 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800656 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Ingo Molnar54168ed2008-08-20 09:07:45 +0200659static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int apic, pin;
662
663 for (apic = 0; apic < nr_ioapics; apic++)
664 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
665 clear_IO_APIC_pin(apic, pin);
666}
667
Ingo Molnar54168ed2008-08-20 09:07:45 +0200668#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
670 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
671 * specific CPU-side IRQs.
672 */
673
674#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800675static int pirq_entries[MAX_PIRQS] = {
676 [0 ... MAX_PIRQS - 1] = -1
677};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static int __init ioapic_pirq_setup(char *str)
680{
681 int i, max;
682 int ints[MAX_PIRQS+1];
683
684 get_options(str, ARRAY_SIZE(ints), ints);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 apic_printk(APIC_VERBOSE, KERN_INFO
687 "PIRQ redirection, working around broken MP-BIOS.\n");
688 max = MAX_PIRQS;
689 if (ints[0] < MAX_PIRQS)
690 max = ints[0];
691
692 for (i = 0; i < max; i++) {
693 apic_printk(APIC_VERBOSE, KERN_DEBUG
694 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
695 /*
696 * PIRQs are mapped upside down, usually.
697 */
698 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
699 }
700 return 1;
701}
702
703__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200704#endif /* CONFIG_X86_32 */
705
Fenghua Yub24696b2009-03-27 14:22:44 -0700706struct IO_APIC_route_entry **alloc_ioapic_entries(void)
707{
708 int apic;
709 struct IO_APIC_route_entry **ioapic_entries;
710
711 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
712 GFP_ATOMIC);
713 if (!ioapic_entries)
714 return 0;
715
716 for (apic = 0; apic < nr_ioapics; apic++) {
717 ioapic_entries[apic] =
718 kzalloc(sizeof(struct IO_APIC_route_entry) *
719 nr_ioapic_registers[apic], GFP_ATOMIC);
720 if (!ioapic_entries[apic])
721 goto nomem;
722 }
723
724 return ioapic_entries;
725
726nomem:
727 while (--apic >= 0)
728 kfree(ioapic_entries[apic]);
729 kfree(ioapic_entries);
730
731 return 0;
732}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200733
734/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700735 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200736 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700737int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200738{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200739 int apic, pin;
740
Fenghua Yub24696b2009-03-27 14:22:44 -0700741 if (!ioapic_entries)
742 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200743
744 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700745 if (!ioapic_entries[apic])
746 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200747
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700748 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
Fenghua Yub24696b2009-03-27 14:22:44 -0700749 ioapic_entries[apic][pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200750 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700751 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400752
Ingo Molnar54168ed2008-08-20 09:07:45 +0200753 return 0;
754}
755
Fenghua Yub24696b2009-03-27 14:22:44 -0700756/*
757 * Mask all IO APIC entries.
758 */
759void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700760{
761 int apic, pin;
762
Fenghua Yub24696b2009-03-27 14:22:44 -0700763 if (!ioapic_entries)
764 return;
765
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700766 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700767 if (!ioapic_entries[apic])
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700768 break;
Fenghua Yub24696b2009-03-27 14:22:44 -0700769
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700770 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
771 struct IO_APIC_route_entry entry;
772
Fenghua Yub24696b2009-03-27 14:22:44 -0700773 entry = ioapic_entries[apic][pin];
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700774 if (!entry.mask) {
775 entry.mask = 1;
776 ioapic_write_entry(apic, pin, entry);
777 }
778 }
779 }
780}
781
Fenghua Yub24696b2009-03-27 14:22:44 -0700782/*
783 * Restore IO APIC entries which was saved in ioapic_entries.
784 */
785int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200786{
787 int apic, pin;
788
Fenghua Yub24696b2009-03-27 14:22:44 -0700789 if (!ioapic_entries)
790 return -ENOMEM;
791
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400792 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700793 if (!ioapic_entries[apic])
794 return -ENOMEM;
795
Ingo Molnar54168ed2008-08-20 09:07:45 +0200796 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
797 ioapic_write_entry(apic, pin,
Fenghua Yub24696b2009-03-27 14:22:44 -0700798 ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400799 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700800 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200801}
802
Fenghua Yub24696b2009-03-27 14:22:44 -0700803void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
804{
805 int apic;
806
807 for (apic = 0; apic < nr_ioapics; apic++)
808 kfree(ioapic_entries[apic]);
809
810 kfree(ioapic_entries);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200811}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813/*
814 * Find the IRQ entry number of a certain pin.
815 */
816static int find_irq_entry(int apic, int pin, int type)
817{
818 int i;
819
820 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530821 if (mp_irqs[i].irqtype == type &&
822 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
823 mp_irqs[i].dstapic == MP_APIC_ALL) &&
824 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return i;
826
827 return -1;
828}
829
830/*
831 * Find the pin to which IRQ[irq] (ISA) is connected
832 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800833static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int i;
836
837 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530838 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300840 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530841 (mp_irqs[i].irqtype == type) &&
842 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530844 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 return -1;
847}
848
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800849static int __init find_isa_irq_apic(int irq, int type)
850{
851 int i;
852
853 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530854 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800855
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300856 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530857 (mp_irqs[i].irqtype == type) &&
858 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800859 break;
860 }
861 if (i < mp_irq_entries) {
862 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200863 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530864 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800865 return apic;
866 }
867 }
868
869 return -1;
870}
871
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300872#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * EISA Edge/Level control register, ELCR
875 */
876static int EISA_ELCR(unsigned int irq)
877{
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200878 if (irq < nr_legacy_irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned int port = 0x4d0 + (irq >> 3);
880 return (inb(port) >> (irq & 7)) & 1;
881 }
882 apic_printk(APIC_VERBOSE, KERN_INFO
883 "Broken MPtable reports ISA irq %d\n", irq);
884 return 0;
885}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200886
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300889/* ISA interrupts are always polarity zero edge triggered,
890 * when listed as conforming in the MP table. */
891
892#define default_ISA_trigger(idx) (0)
893#define default_ISA_polarity(idx) (0)
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/* EISA interrupts are always polarity zero and can be edge or level
896 * trigger depending on the ELCR value. If an interrupt is listed as
897 * EISA conforming in the MP table, that means its trigger type must
898 * be read in from the ELCR */
899
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530900#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300901#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903/* PCI interrupts are always polarity one level triggered,
904 * when listed as conforming in the MP table. */
905
906#define default_PCI_trigger(idx) (1)
907#define default_PCI_polarity(idx) (1)
908
909/* MCA interrupts are always polarity zero level triggered,
910 * when listed as conforming in the MP table. */
911
912#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300913#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Shaohua Li61fd47e2007-11-17 01:05:28 -0500915static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530917 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int polarity;
919
920 /*
921 * Determine IRQ line polarity (high active or low active):
922 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530923 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200925 case 0: /* conforms, ie. bus-type dependent polarity */
926 if (test_bit(bus, mp_bus_not_pci))
927 polarity = default_ISA_polarity(idx);
928 else
929 polarity = default_PCI_polarity(idx);
930 break;
931 case 1: /* high active */
932 {
933 polarity = 0;
934 break;
935 }
936 case 2: /* reserved */
937 {
938 printk(KERN_WARNING "broken BIOS!!\n");
939 polarity = 1;
940 break;
941 }
942 case 3: /* low active */
943 {
944 polarity = 1;
945 break;
946 }
947 default: /* invalid */
948 {
949 printk(KERN_WARNING "broken BIOS!!\n");
950 polarity = 1;
951 break;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 return polarity;
955}
956
957static int MPBIOS_trigger(int idx)
958{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530959 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int trigger;
961
962 /*
963 * Determine IRQ trigger mode (edge or level sensitive):
964 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530965 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200967 case 0: /* conforms, ie. bus-type dependent */
968 if (test_bit(bus, mp_bus_not_pci))
969 trigger = default_ISA_trigger(idx);
970 else
971 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300972#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200973 switch (mp_bus_id_to_type[bus]) {
974 case MP_BUS_ISA: /* ISA pin */
975 {
976 /* set before the switch */
977 break;
978 }
979 case MP_BUS_EISA: /* EISA pin */
980 {
981 trigger = default_EISA_trigger(idx);
982 break;
983 }
984 case MP_BUS_PCI: /* PCI pin */
985 {
986 /* set before the switch */
987 break;
988 }
989 case MP_BUS_MCA: /* MCA pin */
990 {
991 trigger = default_MCA_trigger(idx);
992 break;
993 }
994 default:
995 {
996 printk(KERN_WARNING "broken BIOS!!\n");
997 trigger = 1;
998 break;
999 }
1000 }
1001#endif
1002 break;
1003 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001004 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001005 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001006 break;
1007 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001008 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001009 {
1010 printk(KERN_WARNING "broken BIOS!!\n");
1011 trigger = 1;
1012 break;
1013 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001014 case 3: /* level */
1015 {
1016 trigger = 1;
1017 break;
1018 }
1019 default: /* invalid */
1020 {
1021 printk(KERN_WARNING "broken BIOS!!\n");
1022 trigger = 0;
1023 break;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 return trigger;
1027}
1028
1029static inline int irq_polarity(int idx)
1030{
1031 return MPBIOS_polarity(idx);
1032}
1033
1034static inline int irq_trigger(int idx)
1035{
1036 return MPBIOS_trigger(idx);
1037}
1038
Yinghai Luefa25592008-08-19 20:50:36 -07001039int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int pin_2_irq(int idx, int apic, int pin)
1041{
1042 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301043 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
1046 * Debugging check, we are in big trouble if this message pops up!
1047 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301048 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1050
Ingo Molnar54168ed2008-08-20 09:07:45 +02001051 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301052 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001053 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001054 /*
1055 * PCI IRQs are mapped in order
1056 */
1057 i = irq = 0;
1058 while (i < apic)
1059 irq += nr_ioapic_registers[i++];
1060 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001061 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001062 * For MPS mode, so far only needed by ES7000 platform
1063 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001064 if (ioapic_renumber_irq)
1065 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
Ingo Molnar54168ed2008-08-20 09:07:45 +02001068#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1071 */
1072 if ((pin >= 16) && (pin <= 23)) {
1073 if (pirq_entries[pin-16] != -1) {
1074 if (!pirq_entries[pin-16]) {
1075 apic_printk(APIC_VERBOSE, KERN_DEBUG
1076 "disabling PIRQ%d\n", pin-16);
1077 } else {
1078 irq = pirq_entries[pin-16];
1079 apic_printk(APIC_VERBOSE, KERN_DEBUG
1080 "using PIRQ%d -> IRQ %d\n",
1081 pin-16, irq);
1082 }
1083 }
1084 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001085#endif
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return irq;
1088}
1089
Yinghai Lue20c06f2009-05-06 10:08:22 -07001090/*
1091 * Find a specific PCI IRQ entry.
1092 * Not an __init, possibly needed by modules
1093 */
1094int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
Yinghai Lue5198072009-05-15 13:05:16 -07001095 struct io_apic_irq_attr *irq_attr)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001096{
1097 int apic, i, best_guess = -1;
1098
1099 apic_printk(APIC_DEBUG,
1100 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1101 bus, slot, pin);
1102 if (test_bit(bus, mp_bus_not_pci)) {
1103 apic_printk(APIC_VERBOSE,
1104 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1105 return -1;
1106 }
1107 for (i = 0; i < mp_irq_entries; i++) {
1108 int lbus = mp_irqs[i].srcbus;
1109
1110 for (apic = 0; apic < nr_ioapics; apic++)
1111 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1112 mp_irqs[i].dstapic == MP_APIC_ALL)
1113 break;
1114
1115 if (!test_bit(lbus, mp_bus_not_pci) &&
1116 !mp_irqs[i].irqtype &&
1117 (bus == lbus) &&
1118 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1119 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1120
1121 if (!(apic || IO_APIC_IRQ(irq)))
1122 continue;
1123
1124 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Yinghai Lue5198072009-05-15 13:05:16 -07001125 set_io_apic_irq_attr(irq_attr, apic,
1126 mp_irqs[i].dstirq,
1127 irq_trigger(i),
1128 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001129 return irq;
1130 }
1131 /*
1132 * Use the first all-but-pin matching entry as a
1133 * best-guess fuzzy result for broken mptables.
1134 */
1135 if (best_guess < 0) {
Yinghai Lue5198072009-05-15 13:05:16 -07001136 set_io_apic_irq_attr(irq_attr, apic,
1137 mp_irqs[i].dstirq,
1138 irq_trigger(i),
1139 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001140 best_guess = irq;
1141 }
1142 }
1143 }
1144 return best_guess;
1145}
1146EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1147
Yinghai Lu497c9a12008-08-19 20:50:28 -07001148void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001150 /* Used to the online set of cpus does not change
1151 * during assign_irq_vector.
1152 */
1153 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Yinghai Lu497c9a12008-08-19 20:50:28 -07001156void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001157{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001158 spin_unlock(&vector_lock);
1159}
1160
Mike Travise7986732008-12-16 17:33:52 -08001161static int
1162__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001163{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001164 /*
1165 * NOTE! The local APIC isn't very good at handling
1166 * multiple interrupts at the same interrupt level.
1167 * As the interrupt level is determined by taking the
1168 * vector number and shifting that right by 4, we
1169 * want to spread these out a bit so that they don't
1170 * all fall in the same interrupt level.
1171 *
1172 * Also, we've got to be careful not to trash gate
1173 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1174 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001175 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1176 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001177 int cpu, err;
1178 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001179
Ingo Molnar54168ed2008-08-20 09:07:45 +02001180 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1181 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001182
Mike Travis22f65d32008-12-16 17:33:56 -08001183 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1184 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001185
Ingo Molnar54168ed2008-08-20 09:07:45 +02001186 old_vector = cfg->vector;
1187 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001188 cpumask_and(tmp_mask, mask, cpu_online_mask);
1189 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1190 if (!cpumask_empty(tmp_mask)) {
1191 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001192 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001193 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001194 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001195
Mike Travise7986732008-12-16 17:33:52 -08001196 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001197 err = -ENOSPC;
1198 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001199 int new_cpu;
1200 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001201
Ingo Molnare2d40b12009-01-28 06:50:47 +01001202 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001203
Ingo Molnar54168ed2008-08-20 09:07:45 +02001204 vector = current_vector;
1205 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001206next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001207 vector += 8;
1208 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001209 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001210 offset = (offset + 1) % 8;
1211 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001212 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001213 if (unlikely(current_vector == vector))
1214 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001215
1216 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001217 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001218
Mike Travis22f65d32008-12-16 17:33:56 -08001219 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001220 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1221 goto next;
1222 /* Found one! */
1223 current_vector = vector;
1224 current_offset = offset;
1225 if (old_vector) {
1226 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001227 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001228 }
Mike Travis22f65d32008-12-16 17:33:56 -08001229 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001230 per_cpu(vector_irq, new_cpu)[vector] = irq;
1231 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001232 cpumask_copy(cfg->domain, tmp_mask);
1233 err = 0;
1234 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001235 }
Mike Travis22f65d32008-12-16 17:33:56 -08001236 free_cpumask_var(tmp_mask);
1237 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001238}
1239
Mike Travise7986732008-12-16 17:33:52 -08001240static int
1241assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001242{
1243 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001244 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001245
1246 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001247 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001248 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001249 return err;
1250}
1251
Yinghai Lu3145e942008-12-05 18:58:34 -08001252static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001253{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001254 int cpu, vector;
1255
Yinghai Lu497c9a12008-08-19 20:50:28 -07001256 BUG_ON(!cfg->vector);
1257
1258 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001259 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001260 per_cpu(vector_irq, cpu)[vector] = -1;
1261
1262 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001263 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001264
1265 if (likely(!cfg->move_in_progress))
1266 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001267 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001268 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1269 vector++) {
1270 if (per_cpu(vector_irq, cpu)[vector] != irq)
1271 continue;
1272 per_cpu(vector_irq, cpu)[vector] = -1;
1273 break;
1274 }
1275 }
1276 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001277}
1278
1279void __setup_vector_irq(int cpu)
1280{
1281 /* Initialize vector_irq on a new cpu */
1282 /* This function must be called with vector_lock held */
1283 int irq, vector;
1284 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001285 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001286
1287 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001288 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001289 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001290 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001291 continue;
1292 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001293 per_cpu(vector_irq, cpu)[vector] = irq;
1294 }
1295 /* Mark the free vectors */
1296 for (vector = 0; vector < NR_VECTORS; ++vector) {
1297 irq = per_cpu(vector_irq, cpu)[vector];
1298 if (irq < 0)
1299 continue;
1300
1301 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001302 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001303 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001304 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001305}
Glauber Costa3fde6902008-05-28 20:34:19 -07001306
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001307static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001308static struct irq_chip ir_ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310#define IOAPIC_AUTO -1
1311#define IOAPIC_EDGE 0
1312#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001314#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001315static inline int IO_APIC_irq_trigger(int irq)
1316{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001317 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001318
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001319 for (apic = 0; apic < nr_ioapics; apic++) {
1320 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1321 idx = find_irq_entry(apic, pin, mp_INT);
1322 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1323 return irq_trigger(idx);
1324 }
1325 }
1326 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001327 * nonexistent IRQs are edge default
1328 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001329 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001330}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001331#else
1332static inline int IO_APIC_irq_trigger(int irq)
1333{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001334 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001335}
1336#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001337
Yinghai Lu3145e942008-12-05 18:58:34 -08001338static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Yinghai Lu199751d2008-08-19 20:50:27 -07001340
Jan Beulich6ebcc002006-06-26 13:56:46 +02001341 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001342 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001343 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001344 else
1345 desc->status &= ~IRQ_LEVEL;
1346
Ingo Molnar54168ed2008-08-20 09:07:45 +02001347 if (irq_remapped(irq)) {
1348 desc->status |= IRQ_MOVE_PCNTXT;
1349 if (trigger)
1350 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1351 handle_fasteoi_irq,
1352 "fasteoi");
1353 else
1354 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1355 handle_edge_irq, "edge");
1356 return;
1357 }
Suresh Siddha29b61be2009-03-16 17:05:02 -07001358
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001359 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1360 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001361 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001362 handle_fasteoi_irq,
1363 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001364 else
Ingo Molnara460e742006-10-17 00:10:03 -07001365 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001366 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001367}
1368
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -08001369int setup_ioapic_entry(int apic_id, int irq,
1370 struct IO_APIC_route_entry *entry,
1371 unsigned int destination, int trigger,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001372 int polarity, int vector, int pin)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001373{
1374 /*
1375 * add it to the IO-APIC irq-routing table:
1376 */
1377 memset(entry,0,sizeof(*entry));
1378
Ingo Molnar54168ed2008-08-20 09:07:45 +02001379 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001380 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001381 struct irte irte;
1382 struct IR_IO_APIC_route_entry *ir_entry =
1383 (struct IR_IO_APIC_route_entry *) entry;
1384 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001385
Ingo Molnar54168ed2008-08-20 09:07:45 +02001386 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001387 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001388
1389 index = alloc_irte(iommu, irq, 1);
1390 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001391 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001392
1393 memset(&irte, 0, sizeof(irte));
1394
1395 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001396 irte.dst_mode = apic->irq_dest_mode;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001397 /*
1398 * Trigger mode in the IRTE will always be edge, and the
1399 * actual level or edge trigger will be setup in the IO-APIC
1400 * RTE. This will help simplify level triggered irq migration.
1401 * For more details, see the comments above explainig IO-APIC
1402 * irq migration in the presence of interrupt-remapping.
1403 */
1404 irte.trigger_mode = 0;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001405 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001406 irte.vector = vector;
1407 irte.dest_id = IRTE_DEST(destination);
1408
Weidong Hanf007e992009-05-23 00:41:15 +08001409 /* Set source-id of interrupt request */
1410 set_ioapic_sid(&irte, apic_id);
1411
Ingo Molnar54168ed2008-08-20 09:07:45 +02001412 modify_irte(irq, &irte);
1413
1414 ir_entry->index2 = (index >> 15) & 0x1;
1415 ir_entry->zero = 0;
1416 ir_entry->format = 1;
1417 ir_entry->index = (index & 0x7fff);
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001418 /*
1419 * IO-APIC RTE will be configured with virtual vector.
1420 * irq handler will do the explicit EOI to the io-apic.
1421 */
1422 ir_entry->vector = pin;
Suresh Siddha29b61be2009-03-16 17:05:02 -07001423 } else {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001424 entry->delivery_mode = apic->irq_delivery_mode;
1425 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001426 entry->dest = destination;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001427 entry->vector = vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001428 }
1429
1430 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001431 entry->trigger = trigger;
1432 entry->polarity = polarity;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001433
1434 /* Mask level triggered irqs.
1435 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1436 */
1437 if (trigger)
1438 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001439 return 0;
1440}
1441
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001442static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001443 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001444{
1445 struct irq_cfg *cfg;
1446 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001447 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001448
1449 if (!IO_APIC_IRQ(irq))
1450 return;
1451
Yinghai Lu3145e942008-12-05 18:58:34 -08001452 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001453
Ingo Molnarfe402e12009-01-28 04:32:51 +01001454 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001455 return;
1456
Ingo Molnardebccb32009-01-28 15:20:18 +01001457 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001458
1459 apic_printk(APIC_VERBOSE,KERN_DEBUG
1460 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1461 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001462 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001463 irq, trigger, polarity);
1464
1465
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001466 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001467 dest, trigger, polarity, cfg->vector, pin)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001468 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001469 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001470 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001471 return;
1472 }
1473
Yinghai Lu3145e942008-12-05 18:58:34 -08001474 ioapic_register_intr(irq, desc, trigger);
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001475 if (irq < nr_legacy_irqs)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001476 disable_8259A_irq(irq);
1477
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001478 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Yinghai Lub9c61b702009-05-06 10:10:06 -07001481static struct {
1482 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1483} mp_ioapic_routing[MAX_IO_APICS];
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485static void __init setup_IO_APIC_irqs(void)
1486{
Yinghai Lub9c61b702009-05-06 10:10:06 -07001487 int apic_id = 0, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001488 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001489 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001490 struct irq_cfg *cfg;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07001491 int node = cpu_to_node(boot_cpu_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1494
Yinghai Lub9c61b702009-05-06 10:10:06 -07001495#ifdef CONFIG_ACPI
1496 if (!acpi_disabled && acpi_ioapic) {
1497 apic_id = mp_find_ioapic(0);
1498 if (apic_id < 0)
1499 apic_id = 0;
1500 }
1501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Yinghai Lub9c61b702009-05-06 10:10:06 -07001503 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1504 idx = find_irq_entry(apic_id, pin, mp_INT);
1505 if (idx == -1) {
1506 if (!notcon) {
1507 notcon = 1;
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001508 apic_printk(APIC_VERBOSE,
Yinghai Lub9c61b702009-05-06 10:10:06 -07001509 KERN_DEBUG " %d-%d",
1510 mp_ioapics[apic_id].apicid, pin);
1511 } else
1512 apic_printk(APIC_VERBOSE, " %d-%d",
1513 mp_ioapics[apic_id].apicid, pin);
1514 continue;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001515 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07001516 if (notcon) {
1517 apic_printk(APIC_VERBOSE,
1518 " (apicid-pin) not connected\n");
1519 notcon = 0;
1520 }
1521
1522 irq = pin_2_irq(idx, apic_id, pin);
1523
1524 /*
1525 * Skip the timer IRQ if there's a quirk handler
1526 * installed and if it returns 1:
1527 */
1528 if (apic->multi_timer_check &&
1529 apic->multi_timer_check(apic_id, irq))
1530 continue;
1531
1532 desc = irq_to_desc_alloc_node(irq, node);
1533 if (!desc) {
1534 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1535 continue;
1536 }
1537 cfg = desc->chip_data;
1538 add_pin_to_irq_node(cfg, node, apic_id, pin);
Yinghai Lu4c6f18f2009-05-18 10:23:28 -07001539 /*
1540 * don't mark it in pin_programmed, so later acpi could
1541 * set it correctly when irq < 16
1542 */
Yinghai Lub9c61b702009-05-06 10:10:06 -07001543 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1544 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001547 if (notcon)
1548 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001549 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001553 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001555static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001556 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ingo Molnar54168ed2008-08-20 09:07:45 +02001560 if (intr_remapping_enabled)
1561 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001562
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001563 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /*
1566 * We use logical delivery to get the timer IRQ
1567 * to the first CPU.
1568 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001569 entry.dest_mode = apic->irq_dest_mode;
Yinghai Luf72dcca2009-02-08 16:18:03 -08001570 entry.mask = 0; /* don't mask IRQ for edge */
Ingo Molnardebccb32009-01-28 15:20:18 +01001571 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001572 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 entry.polarity = 0;
1574 entry.trigger = 0;
1575 entry.vector = vector;
1576
1577 /*
1578 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001579 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001581 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 /*
1584 * Add it to the IO-APIC irq-routing table:
1585 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001586 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001589
1590__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 int apic, i;
1593 union IO_APIC_reg_00 reg_00;
1594 union IO_APIC_reg_01 reg_01;
1595 union IO_APIC_reg_02 reg_02;
1596 union IO_APIC_reg_03 reg_03;
1597 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001598 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001599 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001600 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 if (apic_verbosity == APIC_QUIET)
1603 return;
1604
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001605 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 for (i = 0; i < nr_ioapics; i++)
1607 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05301608 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /*
1611 * We are a bit conservative about what we expect. We have to
1612 * know about every hardware change ASAP.
1613 */
1614 printk(KERN_INFO "testing the IO APIC.......................\n");
1615
1616 for (apic = 0; apic < nr_ioapics; apic++) {
1617
1618 spin_lock_irqsave(&ioapic_lock, flags);
1619 reg_00.raw = io_apic_read(apic, 0);
1620 reg_01.raw = io_apic_read(apic, 1);
1621 if (reg_01.bits.version >= 0x10)
1622 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001623 if (reg_01.bits.version >= 0x20)
1624 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 spin_unlock_irqrestore(&ioapic_lock, flags);
1626
Ingo Molnar54168ed2008-08-20 09:07:45 +02001627 printk("\n");
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05301628 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1630 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1631 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1632 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Ingo Molnar54168ed2008-08-20 09:07:45 +02001634 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1638 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /*
1641 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1642 * but the value of reg_02 is read as the previous read register
1643 * value, so ignore it if reg_02 == reg_01.
1644 */
1645 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1646 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1647 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
1650 /*
1651 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1652 * or reg_03, but the value of reg_0[23] is read as the previous read
1653 * register value, so ignore it if reg_03 == reg_0[12].
1654 */
1655 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1656 reg_03.raw != reg_01.raw) {
1657 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1658 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
1661 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1662
Yinghai Lud83e94a2008-08-19 20:50:33 -07001663 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1664 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 for (i = 0; i <= reg_01.bits.entries; i++) {
1667 struct IO_APIC_route_entry entry;
1668
Andi Kleencf4c6a22006-09-26 10:52:30 +02001669 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Ingo Molnar54168ed2008-08-20 09:07:45 +02001671 printk(KERN_DEBUG " %02x %03X ",
1672 i,
1673 entry.dest
1674 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1677 entry.mask,
1678 entry.trigger,
1679 entry.irr,
1680 entry.polarity,
1681 entry.delivery_status,
1682 entry.dest_mode,
1683 entry.delivery_mode,
1684 entry.vector
1685 );
1686 }
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001689 for_each_irq_desc(irq, desc) {
1690 struct irq_pin_list *entry;
1691
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001692 cfg = desc->chip_data;
1693 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001694 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001696 printk(KERN_DEBUG "IRQ%d ", irq);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04001697 for_each_irq_pin(entry, cfg->irq_2_pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 printk("-> %d:%d", entry->apic, entry->pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 printk("\n");
1700 }
1701
1702 printk(KERN_INFO ".................................... done.\n");
1703
1704 return;
1705}
1706
Ingo Molnar251e1e42009-07-02 08:54:01 +02001707__apicdebuginit(void) print_APIC_field(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Ingo Molnar251e1e42009-07-02 08:54:01 +02001709 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 if (apic_verbosity == APIC_QUIET)
1712 return;
1713
Ingo Molnar251e1e42009-07-02 08:54:01 +02001714 printk(KERN_DEBUG);
1715
1716 for (i = 0; i < 8; i++)
1717 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1718
1719 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001722__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Andreas Herrmann97a52712009-05-08 18:23:50 +02001724 unsigned int i, v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001725 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 if (apic_verbosity == APIC_QUIET)
1728 return;
1729
Ingo Molnar251e1e42009-07-02 08:54:01 +02001730 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001732 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001733 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 v = apic_read(APIC_LVR);
1735 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1736 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001737 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 v = apic_read(APIC_TASKPRI);
1740 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1741
Ingo Molnar54168ed2008-08-20 09:07:45 +02001742 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001743 if (!APIC_XAPIC(ver)) {
1744 v = apic_read(APIC_ARBPRI);
1745 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1746 v & APIC_ARBPRI_MASK);
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 v = apic_read(APIC_PROCPRI);
1749 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1750 }
1751
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001752 /*
1753 * Remote read supported only in the 82489DX and local APIC for
1754 * Pentium processors.
1755 */
1756 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1757 v = apic_read(APIC_RRR);
1758 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1759 }
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 v = apic_read(APIC_LDR);
1762 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001763 if (!x2apic_enabled()) {
1764 v = apic_read(APIC_DFR);
1765 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 v = apic_read(APIC_SPIV);
1768 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1769
1770 printk(KERN_DEBUG "... APIC ISR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001771 print_APIC_field(APIC_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 printk(KERN_DEBUG "... APIC TMR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001773 print_APIC_field(APIC_TMR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 printk(KERN_DEBUG "... APIC IRR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001775 print_APIC_field(APIC_IRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Ingo Molnar54168ed2008-08-20 09:07:45 +02001777 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1778 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 v = apic_read(APIC_ESR);
1782 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1783 }
1784
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001785 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001786 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1787 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 v = apic_read(APIC_LVTT);
1790 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1791
1792 if (maxlvt > 3) { /* PC is LVT#4. */
1793 v = apic_read(APIC_LVTPC);
1794 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1795 }
1796 v = apic_read(APIC_LVT0);
1797 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1798 v = apic_read(APIC_LVT1);
1799 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1800
1801 if (maxlvt > 2) { /* ERR is LVT#3. */
1802 v = apic_read(APIC_LVTERR);
1803 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1804 }
1805
1806 v = apic_read(APIC_TMICT);
1807 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1808 v = apic_read(APIC_TMCCT);
1809 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1810 v = apic_read(APIC_TDCR);
1811 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
Andreas Herrmann97a52712009-05-08 18:23:50 +02001812
1813 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1814 v = apic_read(APIC_EFEAT);
1815 maxlvt = (v >> 16) & 0xff;
1816 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1817 v = apic_read(APIC_ECTRL);
1818 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1819 for (i = 0; i < maxlvt; i++) {
1820 v = apic_read(APIC_EILVTn(i));
1821 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1822 }
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 printk("\n");
1825}
1826
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001827__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001829 int cpu;
1830
1831 preempt_disable();
1832 for_each_online_cpu(cpu)
1833 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1834 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001837__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 unsigned int v;
1840 unsigned long flags;
1841
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001842 if (apic_verbosity == APIC_QUIET || !nr_legacy_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return;
1844
1845 printk(KERN_DEBUG "\nprinting PIC contents\n");
1846
1847 spin_lock_irqsave(&i8259A_lock, flags);
1848
1849 v = inb(0xa1) << 8 | inb(0x21);
1850 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1851
1852 v = inb(0xa0) << 8 | inb(0x20);
1853 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1854
Ingo Molnar54168ed2008-08-20 09:07:45 +02001855 outb(0x0b,0xa0);
1856 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001858 outb(0x0a,0xa0);
1859 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 spin_unlock_irqrestore(&i8259A_lock, flags);
1862
1863 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1864
1865 v = inb(0x4d1) << 8 | inb(0x4d0);
1866 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1867}
1868
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001869__apicdebuginit(int) print_all_ICs(void)
1870{
1871 print_PIC();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001872
1873 /* don't print out if apic is not there */
Cyrill Gorcunov83121362009-09-15 11:12:30 +04001874 if (!cpu_has_apic && !apic_from_smp_config())
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001875 return 0;
1876
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001877 print_all_local_APICs();
1878 print_IO_APIC();
1879
1880 return 0;
1881}
1882
1883fs_initcall(print_all_ICs);
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Yinghai Luefa25592008-08-19 20:50:36 -07001886/* Where if anywhere is the i8259 connect in external int mode */
1887static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1888
Ingo Molnar54168ed2008-08-20 09:07:45 +02001889void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001892 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001893 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 unsigned long flags;
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /*
1897 * The number of IO-APIC IRQ registers (== #pins):
1898 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001899 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001901 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001903 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1904 }
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001905
1906 if (!nr_legacy_irqs)
1907 return;
1908
Ingo Molnar54168ed2008-08-20 09:07:45 +02001909 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001910 int pin;
1911 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001912 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001913 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001914 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001915
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001916 /* If the interrupt line is enabled and in ExtInt mode
1917 * I have found the pin where the i8259 is connected.
1918 */
1919 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1920 ioapic_i8259.apic = apic;
1921 ioapic_i8259.pin = pin;
1922 goto found_i8259;
1923 }
1924 }
1925 }
1926 found_i8259:
1927 /* Look to see what if the MP table has reported the ExtINT */
1928 /* If we could not find the appropriate pin by looking at the ioapic
1929 * the i8259 probably is not connected the ioapic but give the
1930 * mptable a chance anyway.
1931 */
1932 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1933 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1934 /* Trust the MP table if nothing is setup in the hardware */
1935 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1936 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1937 ioapic_i8259.pin = i8259_pin;
1938 ioapic_i8259.apic = i8259_apic;
1939 }
1940 /* Complain if the MP table and the hardware disagree */
1941 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1942 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1943 {
1944 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
1947 /*
1948 * Do not trust the IO-APIC being empty at bootup
1949 */
1950 clear_IO_APIC();
1951}
1952
1953/*
1954 * Not an __init, needed by the reboot code
1955 */
1956void disable_IO_APIC(void)
1957{
1958 /*
1959 * Clear the IO-APIC before rebooting:
1960 */
1961 clear_IO_APIC();
1962
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001963 if (!nr_legacy_irqs)
1964 return;
1965
Eric W. Biederman650927e2005-06-25 14:57:44 -07001966 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001967 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001968 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001969 * so legacy interrupts can be delivered.
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001970 *
1971 * With interrupt-remapping, for now we will use virtual wire A mode,
1972 * as virtual wire B is little complex (need to configure both
1973 * IOAPIC RTE aswell as interrupt-remapping table entry).
1974 * As this gets called during crash dump, keep this simple for now.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001975 */
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001976 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001977 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001978
1979 memset(&entry, 0, sizeof(entry));
1980 entry.mask = 0; /* Enabled */
1981 entry.trigger = 0; /* Edge */
1982 entry.irr = 0;
1983 entry.polarity = 0; /* High */
1984 entry.delivery_status = 0;
1985 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001986 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001987 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001988 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001989
1990 /*
1991 * Add it to the IO-APIC irq-routing table:
1992 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001993 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001994 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001995
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001996 /*
1997 * Use virtual wire A mode when interrupt remapping is enabled.
1998 */
Cyrill Gorcunov83121362009-09-15 11:12:30 +04001999 if (cpu_has_apic || apic_from_smp_config())
Cyrill Gorcunov3f4c3952009-06-17 22:13:22 +04002000 disconnect_bsp_APIC(!intr_remapping_enabled &&
2001 ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Ingo Molnar54168ed2008-08-20 09:07:45 +02002004#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005/*
2006 * function to set the IO-APIC physical IDs based on the
2007 * values stored in the MPC table.
2008 *
2009 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2010 */
2011
Thomas Gleixnerde934102009-08-20 09:27:29 +02002012void __init setup_ioapic_ids_from_mpc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 union IO_APIC_reg_00 reg_00;
2015 physid_mask_t phys_id_present_map;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002016 int apic_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int i;
2018 unsigned char old_id;
2019 unsigned long flags;
2020
Thomas Gleixnerde934102009-08-20 09:27:29 +02002021 if (acpi_ioapic)
Yinghai Lud49c4282008-06-08 18:31:54 -07002022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002024 * Don't check I/O APIC IDs for xAPIC systems. They have
2025 * no meaning without the serial APIC bus.
2026 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002027 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2028 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002029 return;
2030 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 * This is broken; anything with a real cpu count has to
2032 * circumvent this idiocy regardless.
2033 */
Ingo Molnard190cb82009-01-28 06:50:47 +01002034 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 /*
2037 * Set the IOAPIC ID to the value stored in the MPC table.
2038 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002039 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 /* Read the register 0 value */
2042 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002043 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002045
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002046 old_id = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002048 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002050 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2052 reg_00.bits.ID);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002053 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Sanity check, is the ID really free? Every APIC in a
2058 * system must have a unique ID or we get lots of nice
2059 * 'stuck on smp_invalidate_needed IPI wait' messages.
2060 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01002061 if (apic->check_apicid_used(phys_id_present_map,
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002062 mp_ioapics[apic_id].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002064 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 for (i = 0; i < get_physical_broadcast(); i++)
2066 if (!physid_isset(i, phys_id_present_map))
2067 break;
2068 if (i >= get_physical_broadcast())
2069 panic("Max APIC ID exceeded!\n");
2070 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2071 i);
2072 physid_set(i, phys_id_present_map);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002073 mp_ioapics[apic_id].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 } else {
2075 physid_mask_t tmp;
Ingo Molnar80587142009-01-28 06:50:47 +01002076 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 apic_printk(APIC_VERBOSE, "Setting %d in the "
2078 "phys_id_present_map\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002079 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2081 }
2082
2083
2084 /*
2085 * We need to adjust the IRQ routing table
2086 * if the ID changed.
2087 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002088 if (old_id != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302090 if (mp_irqs[i].dstapic == old_id)
2091 mp_irqs[i].dstapic
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002092 = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /*
2095 * Read the right value from the MPC table and
2096 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 apic_printk(APIC_VERBOSE, KERN_INFO
2099 "...changing IO-APIC physical APIC ID to %d ...",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002100 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002102 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002104 io_apic_write(apic_id, 0, reg_00.raw);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002105 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /*
2108 * Sanity check
2109 */
2110 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002111 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002113 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 printk("could not set ID!\n");
2115 else
2116 apic_printk(APIC_VERBOSE, " ok.\n");
2117 }
2118}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002121int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002122
2123static int __init notimercheck(char *s)
2124{
2125 no_timer_check = 1;
2126 return 1;
2127}
2128__setup("no_timer_check", notimercheck);
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130/*
2131 * There is a nasty bug in some older SMP boards, their mptable lies
2132 * about the timer IRQ. We do the following to work around the situation:
2133 *
2134 * - timer IRQ defaults to IO-APIC IRQ
2135 * - if this function detects that timer IRQs are defunct, then we fall
2136 * back to ISA timer IRQs
2137 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002138static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002141 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Zachary Amsden8542b202006-12-07 02:14:09 +01002143 if (no_timer_check)
2144 return 1;
2145
Ingo Molnar4aae0702007-12-18 18:05:58 +01002146 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 local_irq_enable();
2148 /* Let ten ticks pass... */
2149 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002150 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /*
2153 * Expect a few ticks at least, to be sure some possible
2154 * glue logic does not lock up after one or two first
2155 * ticks in a non-ExtINT mode. Also the local APIC
2156 * might have cached one ExtINT interrupt. Finally, at
2157 * least one tick may be lost due to delays.
2158 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002159
2160 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002161 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return 0;
2164}
2165
2166/*
2167 * In the SMP+IOAPIC case it might happen that there are an unspecified
2168 * number of pending IRQ events unhandled. These cases are very rare,
2169 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2170 * better to do it this way as thus we do not have to be aware of
2171 * 'pending' interrupts in the IRQ path, except at this point.
2172 */
2173/*
2174 * Edge triggered needs to resend any interrupt
2175 * that was delayed but this is now handled in the device
2176 * independent code.
2177 */
2178
2179/*
2180 * Starting up a edge-triggered IO-APIC interrupt is
2181 * nasty - we need to make sure that we get the edge.
2182 * If it is already asserted for some reason, we need
2183 * return 1 to indicate that is was pending.
2184 *
2185 * This is not complete - we should be able to fake
2186 * an edge even if it isn't on the 8259A...
2187 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002188
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002189static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 int was_pending = 0;
2192 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002193 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 spin_lock_irqsave(&ioapic_lock, flags);
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002196 if (irq < nr_legacy_irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 disable_8259A_irq(irq);
2198 if (i8259A_irq_pending(irq))
2199 was_pending = 1;
2200 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002201 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002202 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 spin_unlock_irqrestore(&ioapic_lock, flags);
2204
2205 return was_pending;
2206}
2207
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002208static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002210
2211 struct irq_cfg *cfg = irq_cfg(irq);
2212 unsigned long flags;
2213
2214 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnardac5f412009-01-28 15:42:24 +01002215 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002216 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002217
2218 return 1;
2219}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002220
2221/*
2222 * Level and edge triggered IO-APIC interrupts need different handling,
2223 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2224 * handled with the level-triggered descriptor, but that one has slightly
2225 * more overhead. Level-triggered interrupts cannot be handled with the
2226 * edge-triggered handler, without risking IRQ storms and other ugly
2227 * races.
2228 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002229
Yinghai Lu497c9a12008-08-19 20:50:28 -07002230#ifdef CONFIG_SMP
Gary Hadee85abf82009-04-08 14:07:25 -07002231static void send_cleanup_vector(struct irq_cfg *cfg)
2232{
2233 cpumask_var_t cleanup_mask;
2234
2235 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2236 unsigned int i;
2237 cfg->move_cleanup_count = 0;
2238 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2239 cfg->move_cleanup_count++;
2240 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2241 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2242 } else {
2243 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2244 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
2245 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2246 free_cpumask_var(cleanup_mask);
2247 }
2248 cfg->move_in_progress = 0;
2249}
2250
Ingo Molnar44204712009-05-01 19:02:50 +02002251static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002252{
2253 int apic, pin;
2254 struct irq_pin_list *entry;
2255 u8 vector = cfg->vector;
2256
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04002257 for_each_irq_pin(entry, cfg->irq_2_pin) {
Gary Hadee85abf82009-04-08 14:07:25 -07002258 unsigned int reg;
2259
Gary Hadee85abf82009-04-08 14:07:25 -07002260 apic = entry->apic;
2261 pin = entry->pin;
2262 /*
2263 * With interrupt-remapping, destination information comes
2264 * from interrupt-remapping table entry.
2265 */
2266 if (!irq_remapped(irq))
2267 io_apic_write(apic, 0x11 + pin*2, dest);
2268 reg = io_apic_read(apic, 0x10 + pin*2);
2269 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2270 reg |= vector;
2271 io_apic_modify(apic, 0x10 + pin*2, reg);
Gary Hadee85abf82009-04-08 14:07:25 -07002272 }
2273}
2274
Ingo Molnar44204712009-05-01 19:02:50 +02002275static int
2276assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
2277
Gary Hadee85abf82009-04-08 14:07:25 -07002278/*
2279 * Either sets desc->affinity to a valid value, and returns
2280 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
2281 * leaves desc->affinity untouched.
2282 */
2283static unsigned int
2284set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
2285{
2286 struct irq_cfg *cfg;
2287 unsigned int irq;
2288
2289 if (!cpumask_intersects(mask, cpu_online_mask))
2290 return BAD_APICID;
2291
2292 irq = desc->irq;
2293 cfg = desc->chip_data;
2294 if (assign_irq_vector(irq, cfg, mask))
2295 return BAD_APICID;
2296
Gary Hadee85abf82009-04-08 14:07:25 -07002297 cpumask_copy(desc->affinity, mask);
2298
2299 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2300}
2301
Ingo Molnar44204712009-05-01 19:02:50 +02002302static int
Gary Hadee85abf82009-04-08 14:07:25 -07002303set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2304{
2305 struct irq_cfg *cfg;
2306 unsigned long flags;
2307 unsigned int dest;
2308 unsigned int irq;
Ingo Molnar44204712009-05-01 19:02:50 +02002309 int ret = -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002310
2311 irq = desc->irq;
2312 cfg = desc->chip_data;
2313
2314 spin_lock_irqsave(&ioapic_lock, flags);
2315 dest = set_desc_affinity(desc, mask);
2316 if (dest != BAD_APICID) {
2317 /* Only the high 8 bits are valid. */
2318 dest = SET_APIC_LOGICAL_ID(dest);
2319 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar44204712009-05-01 19:02:50 +02002320 ret = 0;
Gary Hadee85abf82009-04-08 14:07:25 -07002321 }
2322 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnar44204712009-05-01 19:02:50 +02002323
2324 return ret;
Gary Hadee85abf82009-04-08 14:07:25 -07002325}
2326
Ingo Molnar44204712009-05-01 19:02:50 +02002327static int
Gary Hadee85abf82009-04-08 14:07:25 -07002328set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2329{
2330 struct irq_desc *desc;
2331
2332 desc = irq_to_desc(irq);
2333
Ingo Molnar44204712009-05-01 19:02:50 +02002334 return set_ioapic_affinity_irq_desc(desc, mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002335}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002336
2337#ifdef CONFIG_INTR_REMAP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002338
2339/*
2340 * Migrate the IO-APIC irq in the presence of intr-remapping.
2341 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002342 * For both level and edge triggered, irq migration is a simple atomic
2343 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002344 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002345 * For level triggered, we eliminate the io-apic RTE modification (with the
2346 * updated vector information), by using a virtual vector (io-apic pin number).
2347 * Real vector that is used for interrupting cpu will be coming from
2348 * the interrupt-remapping table entry.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002349 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002350static int
Mike Travise7986732008-12-16 17:33:52 -08002351migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002352{
2353 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002354 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002355 unsigned int dest;
Yinghai Lu3145e942008-12-05 18:58:34 -08002356 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -07002357 int ret = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002358
Mike Travis22f65d32008-12-16 17:33:56 -08002359 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002360 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002361
Yinghai Lu3145e942008-12-05 18:58:34 -08002362 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002363 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002364 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002365
Yinghai Lu3145e942008-12-05 18:58:34 -08002366 cfg = desc->chip_data;
2367 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002368 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002369
Ingo Molnardebccb32009-01-28 15:20:18 +01002370 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002371
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372 irte.vector = cfg->vector;
2373 irte.dest_id = IRTE_DEST(dest);
2374
2375 /*
2376 * Modified the IRTE and flushes the Interrupt entry cache.
2377 */
2378 modify_irte(irq, &irte);
2379
Mike Travis22f65d32008-12-16 17:33:56 -08002380 if (cfg->move_in_progress)
2381 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002382
Mike Travis7f7ace02009-01-10 21:58:08 -08002383 cpumask_copy(desc->affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -07002384
2385 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002386}
2387
Ingo Molnar54168ed2008-08-20 09:07:45 +02002388/*
2389 * Migrates the IRQ destination in the process context.
2390 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002391static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Rusty Russell968ea6d2008-12-13 21:55:51 +10302392 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002393{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002394 return migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002395}
Yinghai Lud5dedd42009-04-27 17:59:21 -07002396static int set_ir_ioapic_affinity_irq(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +10302397 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002398{
2399 struct irq_desc *desc = irq_to_desc(irq);
2400
Yinghai Lud5dedd42009-04-27 17:59:21 -07002401 return set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002402}
Suresh Siddha29b61be2009-03-16 17:05:02 -07002403#else
Yinghai Lud5dedd42009-04-27 17:59:21 -07002404static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Suresh Siddha29b61be2009-03-16 17:05:02 -07002405 const struct cpumask *mask)
2406{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002407 return 0;
Suresh Siddha29b61be2009-03-16 17:05:02 -07002408}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002409#endif
2410
Yinghai Lu497c9a12008-08-19 20:50:28 -07002411asmlinkage void smp_irq_move_cleanup_interrupt(void)
2412{
2413 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002414
Yinghai Lu497c9a12008-08-19 20:50:28 -07002415 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002416 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002417 irq_enter();
2418
2419 me = smp_processor_id();
2420 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2421 unsigned int irq;
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002422 unsigned int irr;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002423 struct irq_desc *desc;
2424 struct irq_cfg *cfg;
2425 irq = __get_cpu_var(vector_irq)[vector];
2426
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002427 if (irq == -1)
2428 continue;
2429
Yinghai Lu497c9a12008-08-19 20:50:28 -07002430 desc = irq_to_desc(irq);
2431 if (!desc)
2432 continue;
2433
2434 cfg = irq_cfg(irq);
2435 spin_lock(&desc->lock);
2436 if (!cfg->move_cleanup_count)
2437 goto unlock;
2438
Mike Travis22f65d32008-12-16 17:33:56 -08002439 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002440 goto unlock;
2441
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002442 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2443 /*
2444 * Check if the vector that needs to be cleanedup is
2445 * registered at the cpu's IRR. If so, then this is not
2446 * the best time to clean it up. Lets clean it up in the
2447 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2448 * to myself.
2449 */
2450 if (irr & (1 << (vector % 32))) {
2451 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2452 goto unlock;
2453 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002454 __get_cpu_var(vector_irq)[vector] = -1;
2455 cfg->move_cleanup_count--;
2456unlock:
2457 spin_unlock(&desc->lock);
2458 }
2459
2460 irq_exit();
2461}
2462
Yinghai Lu3145e942008-12-05 18:58:34 -08002463static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002464{
Yinghai Lu3145e942008-12-05 18:58:34 -08002465 struct irq_desc *desc = *descp;
2466 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002467 unsigned vector, me;
2468
Yinghai Lufcef5912009-04-27 17:58:23 -07002469 if (likely(!cfg->move_in_progress))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002470 return;
2471
2472 vector = ~get_irq_regs()->orig_ax;
2473 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002474
Yinghai Lufcef5912009-04-27 17:58:23 -07002475 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Mike Travis22f65d32008-12-16 17:33:56 -08002476 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002477}
2478#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002479static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002480#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002481
Yinghai Lu1d025192008-08-19 20:50:34 -07002482static void ack_apic_edge(unsigned int irq)
2483{
Yinghai Lu3145e942008-12-05 18:58:34 -08002484 struct irq_desc *desc = irq_to_desc(irq);
2485
2486 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002487 move_native_irq(irq);
2488 ack_APIC_irq();
2489}
2490
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002491atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002492
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002493static void ack_apic_level(unsigned int irq)
2494{
Yinghai Lu3145e942008-12-05 18:58:34 -08002495 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002496 unsigned long v;
2497 int i;
Yinghai Lu3145e942008-12-05 18:58:34 -08002498 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002499 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002500
Yinghai Lu3145e942008-12-05 18:58:34 -08002501 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002502#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002503 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002504 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002505 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002506 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002507 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002508#endif
2509
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002510 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002511 * It appears there is an erratum which affects at least version 0x11
2512 * of I/O APIC (that's the 82093AA and cores integrated into various
2513 * chipsets). Under certain conditions a level-triggered interrupt is
2514 * erroneously delivered as edge-triggered one but the respective IRR
2515 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2516 * message but it will never arrive and further interrupts are blocked
2517 * from the source. The exact reason is so far unknown, but the
2518 * phenomenon was observed when two consecutive interrupt requests
2519 * from a given source get delivered to the same CPU and the source is
2520 * temporarily disabled in between.
2521 *
2522 * A workaround is to simulate an EOI message manually. We achieve it
2523 * by setting the trigger mode to edge and then to level when the edge
2524 * trigger mode gets detected in the TMR of a local APIC for a
2525 * level-triggered interrupt. We mask the source for the time of the
2526 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2527 * The idea is from Manfred Spraul. --macro
2528 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002529 cfg = desc->chip_data;
2530 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002531 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002532
Ingo Molnar54168ed2008-08-20 09:07:45 +02002533 /*
2534 * We must acknowledge the irq before we move it or the acknowledge will
2535 * not propagate properly.
2536 */
2537 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002538
Ingo Molnar54168ed2008-08-20 09:07:45 +02002539 /* Now we can move and renable the irq */
2540 if (unlikely(do_unmask_irq)) {
2541 /* Only migrate the irq if the ack has been received.
2542 *
2543 * On rare occasions the broadcast level triggered ack gets
2544 * delayed going to ioapics, and if we reprogram the
2545 * vector while Remote IRR is still set the irq will never
2546 * fire again.
2547 *
2548 * To prevent this scenario we read the Remote IRR bit
2549 * of the ioapic. This has two effects.
2550 * - On any sane system the read of the ioapic will
2551 * flush writes (and acks) going to the ioapic from
2552 * this cpu.
2553 * - We get to see if the ACK has actually been delivered.
2554 *
2555 * Based on failed experiments of reprogramming the
2556 * ioapic entry from outside of irq context starting
2557 * with masking the ioapic entry and then polling until
2558 * Remote IRR was clear before reprogramming the
2559 * ioapic I don't trust the Remote IRR bit to be
2560 * completey accurate.
2561 *
2562 * However there appears to be no other way to plug
2563 * this race, so if the Remote IRR bit is not
2564 * accurate and is causing problems then it is a hardware bug
2565 * and you can go talk to the chipset vendor about it.
2566 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002567 cfg = desc->chip_data;
2568 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002569 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002570 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002571 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002572
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002573 /* Tail end of version 0x11 I/O APIC bug workaround */
Yinghai Lu1d025192008-08-19 20:50:34 -07002574 if (!(v & (1 << (i & 0x1f)))) {
2575 atomic_inc(&irq_mis_count);
2576 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002577 __mask_and_edge_IO_APIC_irq(cfg);
2578 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002579 spin_unlock(&ioapic_lock);
2580 }
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002581}
Yinghai Lu1d025192008-08-19 20:50:34 -07002582
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002583#ifdef CONFIG_INTR_REMAP
Suresh Siddha25629d82009-04-20 13:02:28 -07002584static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2585{
Suresh Siddha25629d82009-04-20 13:02:28 -07002586 struct irq_pin_list *entry;
2587
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04002588 for_each_irq_pin(entry, cfg->irq_2_pin)
2589 io_apic_eoi(entry->apic, entry->pin);
Suresh Siddha25629d82009-04-20 13:02:28 -07002590}
2591
2592static void
2593eoi_ioapic_irq(struct irq_desc *desc)
2594{
2595 struct irq_cfg *cfg;
2596 unsigned long flags;
2597 unsigned int irq;
2598
2599 irq = desc->irq;
2600 cfg = desc->chip_data;
2601
2602 spin_lock_irqsave(&ioapic_lock, flags);
2603 __eoi_ioapic_irq(irq, cfg);
2604 spin_unlock_irqrestore(&ioapic_lock, flags);
2605}
2606
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002607static void ir_ack_apic_edge(unsigned int irq)
2608{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002609 ack_APIC_irq();
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002610}
2611
2612static void ir_ack_apic_level(unsigned int irq)
2613{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002614 struct irq_desc *desc = irq_to_desc(irq);
2615
2616 ack_APIC_irq();
2617 eoi_ioapic_irq(desc);
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002618}
2619#endif /* CONFIG_INTR_REMAP */
2620
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002621static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002622 .name = "IO-APIC",
2623 .startup = startup_ioapic_irq,
2624 .mask = mask_IO_APIC_irq,
2625 .unmask = unmask_IO_APIC_irq,
2626 .ack = ack_apic_edge,
2627 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002628#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002629 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002630#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002631 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632};
2633
Ingo Molnar54168ed2008-08-20 09:07:45 +02002634static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002635 .name = "IR-IO-APIC",
2636 .startup = startup_ioapic_irq,
2637 .mask = mask_IO_APIC_irq,
2638 .unmask = unmask_IO_APIC_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302639#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002640 .ack = ir_ack_apic_edge,
2641 .eoi = ir_ack_apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002642#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002643 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002644#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302645#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002646 .retrigger = ioapic_retrigger_irq,
2647};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649static inline void init_IO_APIC_traps(void)
2650{
2651 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002652 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002653 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 /*
2656 * NOTE! The local APIC isn't very good at handling
2657 * multiple interrupts at the same interrupt level.
2658 * As the interrupt level is determined by taking the
2659 * vector number and shifting that right by 4, we
2660 * want to spread these out a bit so that they don't
2661 * all fall in the same interrupt level.
2662 *
2663 * Also, we've got to be careful not to trash gate
2664 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2665 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002666 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002667 cfg = desc->chip_data;
2668 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 /*
2670 * Hmm.. We don't have an entry for this,
2671 * so default to an old-fashioned 8259
2672 * interrupt if we can..
2673 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002674 if (irq < nr_legacy_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002676 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002678 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680 }
2681}
2682
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002683/*
2684 * The local APIC irq-chip implementation:
2685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002687static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 unsigned long v;
2690
2691 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002692 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002695static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002697 unsigned long v;
2698
2699 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002700 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Yinghai Lu3145e942008-12-05 18:58:34 -08002703static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002704{
2705 ack_APIC_irq();
2706}
2707
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002708static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002709 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002710 .mask = mask_lapic_irq,
2711 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002712 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713};
2714
Yinghai Lu3145e942008-12-05 18:58:34 -08002715static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002716{
Yinghai Lu08678b02008-08-19 20:50:05 -07002717 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002718 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2719 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002720}
2721
Jan Beuliche9427102008-01-30 13:31:24 +01002722static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
2724 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002725 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 * We put the 8259A master into AEOI mode and
2727 * unmask on all local APICs LVT0 as NMI.
2728 *
2729 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2730 * is from Maciej W. Rozycki - so we do not have to EOI from
2731 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2734
Jan Beuliche9427102008-01-30 13:31:24 +01002735 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 apic_printk(APIC_VERBOSE, " done.\n");
2738}
2739
2740/*
2741 * This looks a bit hackish but it's about the only one way of sending
2742 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2743 * not support the ExtINT mode, unfortunately. We need to send these
2744 * cycles as some i82489DX-based boards have glue logic that keeps the
2745 * 8259A interrupt line asserted until INTA. --macro
2746 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002747static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002749 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 struct IO_APIC_route_entry entry0, entry1;
2751 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002753 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002754 if (pin == -1) {
2755 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002757 }
2758 apic = find_isa_irq_apic(8, mp_INT);
2759 if (apic == -1) {
2760 WARN_ON_ONCE(1);
2761 return;
2762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Andi Kleencf4c6a22006-09-26 10:52:30 +02002764 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002765 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 memset(&entry1, 0, sizeof(entry1));
2768
2769 entry1.dest_mode = 0; /* physical delivery */
2770 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002771 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 entry1.delivery_mode = dest_ExtINT;
2773 entry1.polarity = entry0.polarity;
2774 entry1.trigger = 0;
2775 entry1.vector = 0;
2776
Andi Kleencf4c6a22006-09-26 10:52:30 +02002777 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 save_control = CMOS_READ(RTC_CONTROL);
2780 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2781 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2782 RTC_FREQ_SELECT);
2783 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2784
2785 i = 100;
2786 while (i-- > 0) {
2787 mdelay(10);
2788 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2789 i -= 10;
2790 }
2791
2792 CMOS_WRITE(save_control, RTC_CONTROL);
2793 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002794 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Andi Kleencf4c6a22006-09-26 10:52:30 +02002796 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
2798
Yinghai Luefa25592008-08-19 20:50:36 -07002799static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002800/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002801static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002802{
2803 disable_timer_pin_1 = 1;
2804 return 0;
2805}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002806early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002807
2808int timer_through_8259 __initdata;
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810/*
2811 * This code may look a bit paranoid, but it's supposed to cooperate with
2812 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2813 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2814 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002815 *
2816 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002818static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Yinghai Lu3145e942008-12-05 18:58:34 -08002820 struct irq_desc *desc = irq_to_desc(0);
2821 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002822 int node = cpu_to_node(boot_cpu_id);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002823 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002824 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002825 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002826
2827 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /*
2830 * get/set the timer IRQ vector:
2831 */
2832 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002833 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002836 * As IRQ0 is to be enabled in the 8259A, the virtual
2837 * wire has to be disabled in the local APIC. Also
2838 * timer interrupts need to be acknowledged manually in
2839 * the 8259A for the i82489DX when using the NMI
2840 * watchdog as that APIC treats NMIs as level-triggered.
2841 * The AEOI mode will finish them in the 8259A
2842 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002844 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002846#ifdef CONFIG_X86_32
Yinghai Luf72dcca2009-02-08 16:18:03 -08002847 {
2848 unsigned int ver;
2849
2850 ver = apic_read(APIC_LVR);
2851 ver = GET_APIC_VERSION(ver);
2852 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2853 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002854#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002856 pin1 = find_isa_irq_pin(0, mp_INT);
2857 apic1 = find_isa_irq_apic(0, mp_INT);
2858 pin2 = ioapic_i8259.pin;
2859 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002861 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2862 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002863 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002865 /*
2866 * Some BIOS writers are clueless and report the ExtINTA
2867 * I/O APIC input from the cascaded 8259A as the timer
2868 * interrupt input. So just in case, if only one pin
2869 * was found above, try it both directly and through the
2870 * 8259A.
2871 */
2872 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002873 if (intr_remapping_enabled)
2874 panic("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002875 pin1 = pin2;
2876 apic1 = apic2;
2877 no_pin1 = 1;
2878 } else if (pin2 == -1) {
2879 pin2 = pin1;
2880 apic2 = apic1;
2881 }
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (pin1 != -1) {
2884 /*
2885 * Ok, does IRQ0 through the IOAPIC work?
2886 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002887 if (no_pin1) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002888 add_pin_to_irq_node(cfg, node, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002889 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002890 } else {
2891 /* for edge trigger, setup_IO_APIC_irq already
2892 * leave it unmasked.
2893 * so only need to unmask if it is level-trigger
2894 * do we really have level trigger timer?
2895 */
2896 int idx;
2897 idx = find_irq_entry(apic1, pin1, mp_INT);
2898 if (idx != -1 && irq_trigger(idx))
2899 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (timer_irq_works()) {
2902 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 setup_nmi();
2904 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002906 if (disable_timer_pin_1 > 0)
2907 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002910 if (intr_remapping_enabled)
2911 panic("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002912 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002913 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002914 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002915 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2916 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002918 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2919 "(IRQ0) through the 8259A ...\n");
2920 apic_printk(APIC_QUIET, KERN_INFO
2921 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /*
2923 * legacy devices should be connected to IO APIC #0
2924 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002925 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002926 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002927 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002929 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002930 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002932 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002934 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938 /*
2939 * Cleanup, just in case ...
2940 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002941 local_irq_disable();
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002942 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002943 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002944 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002948 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2949 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002950 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002952#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002953 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002956 apic_printk(APIC_QUIET, KERN_INFO
2957 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Yinghai Lu3145e942008-12-05 18:58:34 -08002959 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002960 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 enable_8259A_irq(0);
2962
2963 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002964 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002967 local_irq_disable();
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002968 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002969 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002970 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002972 apic_printk(APIC_QUIET, KERN_INFO
2973 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 init_8259A(0);
2976 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002977 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 unlock_ExtINT_logic();
2980
2981 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002982 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002985 local_irq_disable();
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002986 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002988 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002989out:
2990 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002994 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2995 * to devices. However there may be an I/O APIC pin available for
2996 * this interrupt regardless. The pin may be left unconnected, but
2997 * typically it will be reused as an ExtINT cascade interrupt for
2998 * the master 8259A. In the MPS case such a pin will normally be
2999 * reported as an ExtINT interrupt in the MP table. With ACPI
3000 * there is no provision for ExtINT interrupts, and in the absence
3001 * of an override it would be treated as an ordinary ISA I/O APIC
3002 * interrupt, that is edge-triggered and unmasked by default. We
3003 * used to do this, but it caused problems on some systems because
3004 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3005 * the same ExtINT cascade interrupt to drive the local APIC of the
3006 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3007 * the I/O APIC in all cases now. No actual device should request
3008 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003010#define PIC_IRQS (1UL << PIC_CASCADE_IR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012void __init setup_IO_APIC(void)
3013{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003014
Ingo Molnar54168ed2008-08-20 09:07:45 +02003015 /*
3016 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3017 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003018 io_apic_irqs = nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Ingo Molnar54168ed2008-08-20 09:07:45 +02003020 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003021 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003022 * Set up IO-APIC IRQ routing.
3023 */
Thomas Gleixnerde934102009-08-20 09:27:29 +02003024 x86_init.mpparse.setup_ioapic_ids();
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 sync_Arb_IDs();
3027 setup_IO_APIC_irqs();
3028 init_IO_APIC_traps();
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003029 if (nr_legacy_irqs)
3030 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003034 * Called after all the initialization is done. If we didnt find any
3035 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038static int __init io_apic_bug_finalize(void)
3039{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003040 if (sis_apic_bug == -1)
3041 sis_apic_bug = 0;
3042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
3045late_initcall(io_apic_bug_finalize);
3046
3047struct sysfs_ioapic_data {
3048 struct sys_device dev;
3049 struct IO_APIC_route_entry entry[0];
3050};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003051static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Pavel Machek438510f2005-04-16 15:25:24 -07003053static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct IO_APIC_route_entry *entry;
3056 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 data = container_of(dev, struct sysfs_ioapic_data, dev);
3060 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003061 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3062 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 return 0;
3065}
3066
3067static int ioapic_resume(struct sys_device *dev)
3068{
3069 struct IO_APIC_route_entry *entry;
3070 struct sysfs_ioapic_data *data;
3071 unsigned long flags;
3072 union IO_APIC_reg_00 reg_00;
3073 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 data = container_of(dev, struct sysfs_ioapic_data, dev);
3076 entry = data->entry;
3077
3078 spin_lock_irqsave(&ioapic_lock, flags);
3079 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05303080 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3081 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 io_apic_write(dev->id, 0, reg_00.raw);
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003085 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003086 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 return 0;
3089}
3090
3091static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01003092 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 .suspend = ioapic_suspend,
3094 .resume = ioapic_resume,
3095};
3096
3097static int __init ioapic_init_sysfs(void)
3098{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003099 struct sys_device * dev;
3100 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 error = sysdev_class_register(&ioapic_sysdev_class);
3103 if (error)
3104 return error;
3105
Ingo Molnar54168ed2008-08-20 09:07:45 +02003106 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003107 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003109 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!mp_ioapic_data[i]) {
3111 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3112 continue;
3113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003115 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 dev->cls = &ioapic_sysdev_class;
3117 error = sysdev_register(dev);
3118 if (error) {
3119 kfree(mp_ioapic_data[i]);
3120 mp_ioapic_data[i] = NULL;
3121 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3122 continue;
3123 }
3124 }
3125
3126 return 0;
3127}
3128
3129device_initcall(ioapic_init_sysfs);
3130
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003131/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003132 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003133 */
Yinghai Lud047f53a2009-04-27 18:02:23 -07003134unsigned int create_irq_nr(unsigned int irq_want, int node)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003135{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003136 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003137 unsigned int irq;
3138 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003139 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003140 struct irq_cfg *cfg_new = NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003141 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003142
3143 irq = 0;
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003144 if (irq_want < nr_irqs_gsi)
3145 irq_want = nr_irqs_gsi;
3146
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003147 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003148 for (new = irq_want; new < nr_irqs; new++) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07003149 desc_new = irq_to_desc_alloc_node(new, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003150 if (!desc_new) {
3151 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003152 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003153 }
3154 cfg_new = desc_new->chip_data;
3155
3156 if (cfg_new->vector != 0)
3157 continue;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003158
Yinghai Lu15e957d2009-04-30 01:17:50 -07003159 desc_new = move_irq_desc(desc_new, node);
Yinghai Lud047f53a2009-04-27 18:02:23 -07003160
Ingo Molnarfe402e12009-01-28 04:32:51 +01003161 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003162 irq = new;
3163 break;
3164 }
3165 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003166
Yinghai Lu199751d2008-08-19 20:50:27 -07003167 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003168 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003169 /* restore it, in case dynamic_irq_init clear it */
3170 if (desc_new)
3171 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003172 }
3173 return irq;
3174}
3175
Yinghai Lu199751d2008-08-19 20:50:27 -07003176int create_irq(void)
3177{
Yinghai Lud047f53a2009-04-27 18:02:23 -07003178 int node = cpu_to_node(boot_cpu_id);
Yinghai Lube5d5352008-12-05 18:58:33 -08003179 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003180 int irq;
3181
Yinghai Lube5d5352008-12-05 18:58:33 -08003182 irq_want = nr_irqs_gsi;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003183 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003184
3185 if (irq == 0)
3186 irq = -1;
3187
3188 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003189}
3190
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003191void destroy_irq(unsigned int irq)
3192{
3193 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003194 struct irq_cfg *cfg;
3195 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003196
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003197 /* store it, in case dynamic_irq_cleanup clear it */
3198 desc = irq_to_desc(irq);
3199 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003200 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003201 /* connect back irq_cfg */
Bartlomiej Zolnierkiewicz25f6e892009-07-30 23:21:18 +02003202 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003203
Ingo Molnar54168ed2008-08-20 09:07:45 +02003204 free_irte(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003205 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003206 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003207 spin_unlock_irqrestore(&vector_lock, flags);
3208}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003209
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003210/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003211 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003212 */
3213#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003214static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003215{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003216 struct irq_cfg *cfg;
3217 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003218 unsigned dest;
3219
Jan Beulichf1182632009-01-14 12:27:35 +00003220 if (disable_apic)
3221 return -ENXIO;
3222
Yinghai Lu3145e942008-12-05 18:58:34 -08003223 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003224 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003225 if (err)
3226 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003227
Ingo Molnardebccb32009-01-28 15:20:18 +01003228 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003229
Ingo Molnar54168ed2008-08-20 09:07:45 +02003230 if (irq_remapped(irq)) {
3231 struct irte irte;
3232 int ir_index;
3233 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003234
Ingo Molnar54168ed2008-08-20 09:07:45 +02003235 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3236 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003237
Ingo Molnar54168ed2008-08-20 09:07:45 +02003238 memset (&irte, 0, sizeof(irte));
3239
3240 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003241 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003242 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003243 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003244 irte.vector = cfg->vector;
3245 irte.dest_id = IRTE_DEST(dest);
3246
Weidong Hanf007e992009-05-23 00:41:15 +08003247 /* Set source-id of interrupt request */
3248 set_msi_sid(&irte, pdev);
3249
Ingo Molnar54168ed2008-08-20 09:07:45 +02003250 modify_irte(irq, &irte);
3251
3252 msg->address_hi = MSI_ADDR_BASE_HI;
3253 msg->data = sub_handle;
3254 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3255 MSI_ADDR_IR_SHV |
3256 MSI_ADDR_IR_INDEX1(ir_index) |
3257 MSI_ADDR_IR_INDEX2(ir_index);
Suresh Siddha29b61be2009-03-16 17:05:02 -07003258 } else {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003259 if (x2apic_enabled())
3260 msg->address_hi = MSI_ADDR_BASE_HI |
3261 MSI_ADDR_EXT_DEST_ID(dest);
3262 else
3263 msg->address_hi = MSI_ADDR_BASE_HI;
3264
Ingo Molnar54168ed2008-08-20 09:07:45 +02003265 msg->address_lo =
3266 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003267 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003268 MSI_ADDR_DEST_MODE_PHYSICAL:
3269 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003270 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003271 MSI_ADDR_REDIRECTION_CPU:
3272 MSI_ADDR_REDIRECTION_LOWPRI) |
3273 MSI_ADDR_DEST_ID(dest);
3274
3275 msg->data =
3276 MSI_DATA_TRIGGER_EDGE |
3277 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003278 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003279 MSI_DATA_DELIVERY_FIXED:
3280 MSI_DATA_DELIVERY_LOWPRI) |
3281 MSI_DATA_VECTOR(cfg->vector);
3282 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003283 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003284}
3285
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003286#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003287static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003288{
Yinghai Lu3145e942008-12-05 18:58:34 -08003289 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003290 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003291 struct msi_msg msg;
3292 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003293
Mike Travis22f65d32008-12-16 17:33:56 -08003294 dest = set_desc_affinity(desc, mask);
3295 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003296 return -1;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003297
Yinghai Lu3145e942008-12-05 18:58:34 -08003298 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003299
Yinghai Lu3145e942008-12-05 18:58:34 -08003300 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003301
3302 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003303 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003304 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3305 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3306
Yinghai Lu3145e942008-12-05 18:58:34 -08003307 write_msi_msg_desc(desc, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003308
3309 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003310}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003311#ifdef CONFIG_INTR_REMAP
3312/*
3313 * Migrate the MSI irq to another cpumask. This migration is
3314 * done in the process context using interrupt-remapping hardware.
3315 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07003316static int
Mike Travise7986732008-12-16 17:33:52 -08003317ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003318{
Yinghai Lu3145e942008-12-05 18:58:34 -08003319 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003320 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003321 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003322 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003323
3324 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003325 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003326
Mike Travis22f65d32008-12-16 17:33:56 -08003327 dest = set_desc_affinity(desc, mask);
3328 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003329 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003330
Ingo Molnar54168ed2008-08-20 09:07:45 +02003331 irte.vector = cfg->vector;
3332 irte.dest_id = IRTE_DEST(dest);
3333
3334 /*
3335 * atomically update the IRTE with the new destination and vector.
3336 */
3337 modify_irte(irq, &irte);
3338
3339 /*
3340 * After this point, all the interrupts will start arriving
3341 * at the new destination. So, time to cleanup the previous
3342 * vector allocation.
3343 */
Mike Travis22f65d32008-12-16 17:33:56 -08003344 if (cfg->move_in_progress)
3345 send_cleanup_vector(cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003346
3347 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003348}
Yinghai Lu3145e942008-12-05 18:58:34 -08003349
Ingo Molnar54168ed2008-08-20 09:07:45 +02003350#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003351#endif /* CONFIG_SMP */
3352
3353/*
3354 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3355 * which implement the MSI or MSI-X Capability Structure.
3356 */
3357static struct irq_chip msi_chip = {
3358 .name = "PCI-MSI",
3359 .unmask = unmask_msi_irq,
3360 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003361 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003362#ifdef CONFIG_SMP
3363 .set_affinity = set_msi_irq_affinity,
3364#endif
3365 .retrigger = ioapic_retrigger_irq,
3366};
3367
Ingo Molnar54168ed2008-08-20 09:07:45 +02003368static struct irq_chip msi_ir_chip = {
3369 .name = "IR-PCI-MSI",
3370 .unmask = unmask_msi_irq,
3371 .mask = mask_msi_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303372#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08003373 .ack = ir_ack_apic_edge,
Ingo Molnar54168ed2008-08-20 09:07:45 +02003374#ifdef CONFIG_SMP
3375 .set_affinity = ir_set_msi_irq_affinity,
3376#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303377#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02003378 .retrigger = ioapic_retrigger_irq,
3379};
3380
3381/*
3382 * Map the PCI dev to the corresponding remapping hardware unit
3383 * and allocate 'nvec' consecutive interrupt-remapping table entries
3384 * in it.
3385 */
3386static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3387{
3388 struct intel_iommu *iommu;
3389 int index;
3390
3391 iommu = map_dev_to_ir(dev);
3392 if (!iommu) {
3393 printk(KERN_ERR
3394 "Unable to map PCI %s to iommu\n", pci_name(dev));
3395 return -ENOENT;
3396 }
3397
3398 index = alloc_irte(iommu, irq, nvec);
3399 if (index < 0) {
3400 printk(KERN_ERR
3401 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003402 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003403 return -ENOSPC;
3404 }
3405 return index;
3406}
Yinghai Lu1d025192008-08-19 20:50:34 -07003407
Yinghai Lu3145e942008-12-05 18:58:34 -08003408static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003409{
3410 int ret;
3411 struct msi_msg msg;
3412
3413 ret = msi_compose_msg(dev, irq, &msg);
3414 if (ret < 0)
3415 return ret;
3416
Yinghai Lu3145e942008-12-05 18:58:34 -08003417 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003418 write_msi_msg(irq, &msg);
3419
Ingo Molnar54168ed2008-08-20 09:07:45 +02003420 if (irq_remapped(irq)) {
3421 struct irq_desc *desc = irq_to_desc(irq);
3422 /*
3423 * irq migration in process context
3424 */
3425 desc->status |= IRQ_MOVE_PCNTXT;
3426 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3427 } else
Ingo Molnar54168ed2008-08-20 09:07:45 +02003428 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003429
Yinghai Luc81bba42008-09-25 11:53:11 -07003430 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3431
Yinghai Lu1d025192008-08-19 20:50:34 -07003432 return 0;
3433}
3434
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003435int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3436{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003437 unsigned int irq;
3438 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003439 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003440 unsigned int irq_want;
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003441 struct intel_iommu *iommu = NULL;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003442 int index = 0;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003443 int node;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003444
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -04003445 /* x86 doesn't support multiple MSI yet */
3446 if (type == PCI_CAP_ID_MSI && nvec > 1)
3447 return 1;
3448
Yinghai Lud047f53a2009-04-27 18:02:23 -07003449 node = dev_to_node(&dev->dev);
Yinghai Lube5d5352008-12-05 18:58:33 -08003450 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003451 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003452 list_for_each_entry(msidesc, &dev->msi_list, list) {
Yinghai Lud047f53a2009-04-27 18:02:23 -07003453 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003454 if (irq == 0)
3455 return -1;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003456 irq_want = irq + 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003457 if (!intr_remapping_enabled)
3458 goto no_ir;
3459
3460 if (!sub_handle) {
3461 /*
3462 * allocate the consecutive block of IRTE's
3463 * for 'nvec'
3464 */
3465 index = msi_alloc_irte(dev, irq, nvec);
3466 if (index < 0) {
3467 ret = index;
3468 goto error;
3469 }
3470 } else {
3471 iommu = map_dev_to_ir(dev);
3472 if (!iommu) {
3473 ret = -ENOENT;
3474 goto error;
3475 }
3476 /*
3477 * setup the mapping between the irq and the IRTE
3478 * base index, the sub_handle pointing to the
3479 * appropriate interrupt remap table entry.
3480 */
3481 set_irte_irq(irq, iommu, index, sub_handle);
3482 }
3483no_ir:
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003484 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003485 if (ret < 0)
3486 goto error;
3487 sub_handle++;
3488 }
3489 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003490
3491error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003492 destroy_irq(irq);
3493 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003494}
3495
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003496void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003497{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003498 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003499}
3500
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003501#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003502#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003503static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003504{
Yinghai Lu3145e942008-12-05 18:58:34 -08003505 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003506 struct irq_cfg *cfg;
3507 struct msi_msg msg;
3508 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003509
Mike Travis22f65d32008-12-16 17:33:56 -08003510 dest = set_desc_affinity(desc, mask);
3511 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003512 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003513
Yinghai Lu3145e942008-12-05 18:58:34 -08003514 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003515
3516 dmar_msi_read(irq, &msg);
3517
3518 msg.data &= ~MSI_DATA_VECTOR_MASK;
3519 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3520 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3521 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3522
3523 dmar_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003524
3525 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003526}
Yinghai Lu3145e942008-12-05 18:58:34 -08003527
Ingo Molnar54168ed2008-08-20 09:07:45 +02003528#endif /* CONFIG_SMP */
3529
Jaswinder Singh Rajput8f7007a2009-06-10 12:41:01 -07003530static struct irq_chip dmar_msi_type = {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003531 .name = "DMAR_MSI",
3532 .unmask = dmar_msi_unmask,
3533 .mask = dmar_msi_mask,
3534 .ack = ack_apic_edge,
3535#ifdef CONFIG_SMP
3536 .set_affinity = dmar_msi_set_affinity,
3537#endif
3538 .retrigger = ioapic_retrigger_irq,
3539};
3540
3541int arch_setup_dmar_msi(unsigned int irq)
3542{
3543 int ret;
3544 struct msi_msg msg;
3545
3546 ret = msi_compose_msg(NULL, irq, &msg);
3547 if (ret < 0)
3548 return ret;
3549 dmar_msi_write(irq, &msg);
3550 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3551 "edge");
3552 return 0;
3553}
3554#endif
3555
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003556#ifdef CONFIG_HPET_TIMER
3557
3558#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003559static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003560{
Yinghai Lu3145e942008-12-05 18:58:34 -08003561 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003562 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003563 struct msi_msg msg;
3564 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003565
Mike Travis22f65d32008-12-16 17:33:56 -08003566 dest = set_desc_affinity(desc, mask);
3567 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003568 return -1;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003569
Yinghai Lu3145e942008-12-05 18:58:34 -08003570 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003571
3572 hpet_msi_read(irq, &msg);
3573
3574 msg.data &= ~MSI_DATA_VECTOR_MASK;
3575 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3576 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3577 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3578
3579 hpet_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003580
3581 return 0;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003582}
Yinghai Lu3145e942008-12-05 18:58:34 -08003583
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003584#endif /* CONFIG_SMP */
3585
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003586static struct irq_chip hpet_msi_type = {
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003587 .name = "HPET_MSI",
3588 .unmask = hpet_msi_unmask,
3589 .mask = hpet_msi_mask,
3590 .ack = ack_apic_edge,
3591#ifdef CONFIG_SMP
3592 .set_affinity = hpet_msi_set_affinity,
3593#endif
3594 .retrigger = ioapic_retrigger_irq,
3595};
3596
3597int arch_setup_hpet_msi(unsigned int irq)
3598{
3599 int ret;
3600 struct msi_msg msg;
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003601 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003602
3603 ret = msi_compose_msg(NULL, irq, &msg);
3604 if (ret < 0)
3605 return ret;
3606
3607 hpet_msi_write(irq, &msg);
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003608 desc->status |= IRQ_MOVE_PCNTXT;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003609 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3610 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003611
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003612 return 0;
3613}
3614#endif
3615
Ingo Molnar54168ed2008-08-20 09:07:45 +02003616#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003617/*
3618 * Hypertransport interrupt support
3619 */
3620#ifdef CONFIG_HT_IRQ
3621
3622#ifdef CONFIG_SMP
3623
Yinghai Lu497c9a12008-08-19 20:50:28 -07003624static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003625{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003626 struct ht_irq_msg msg;
3627 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003628
Yinghai Lu497c9a12008-08-19 20:50:28 -07003629 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003630 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003631
Yinghai Lu497c9a12008-08-19 20:50:28 -07003632 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003633 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003634
Eric W. Biedermanec683072006-11-08 17:44:57 -08003635 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003636}
3637
Yinghai Lud5dedd42009-04-27 17:59:21 -07003638static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003639{
Yinghai Lu3145e942008-12-05 18:58:34 -08003640 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003641 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003642 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003643
Mike Travis22f65d32008-12-16 17:33:56 -08003644 dest = set_desc_affinity(desc, mask);
3645 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003646 return -1;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003647
Yinghai Lu3145e942008-12-05 18:58:34 -08003648 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003649
Yinghai Lu497c9a12008-08-19 20:50:28 -07003650 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003651
3652 return 0;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003653}
Yinghai Lu3145e942008-12-05 18:58:34 -08003654
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003655#endif
3656
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003657static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003658 .name = "PCI-HT",
3659 .mask = mask_ht_irq,
3660 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003661 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003662#ifdef CONFIG_SMP
3663 .set_affinity = set_ht_irq_affinity,
3664#endif
3665 .retrigger = ioapic_retrigger_irq,
3666};
3667
3668int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3669{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003670 struct irq_cfg *cfg;
3671 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003672
Jan Beulichf1182632009-01-14 12:27:35 +00003673 if (disable_apic)
3674 return -ENXIO;
3675
Yinghai Lu3145e942008-12-05 18:58:34 -08003676 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003677 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003678 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003679 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003680 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003681
Ingo Molnardebccb32009-01-28 15:20:18 +01003682 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3683 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003684
Eric W. Biedermanec683072006-11-08 17:44:57 -08003685 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003686
Eric W. Biedermanec683072006-11-08 17:44:57 -08003687 msg.address_lo =
3688 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003689 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003690 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003691 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003692 HT_IRQ_LOW_DM_PHYSICAL :
3693 HT_IRQ_LOW_DM_LOGICAL) |
3694 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003695 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003696 HT_IRQ_LOW_MT_FIXED :
3697 HT_IRQ_LOW_MT_ARBITRATED) |
3698 HT_IRQ_LOW_IRQ_MASKED;
3699
Eric W. Biedermanec683072006-11-08 17:44:57 -08003700 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003701
Ingo Molnara460e742006-10-17 00:10:03 -07003702 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3703 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003704
3705 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003706 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003707 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003708}
3709#endif /* CONFIG_HT_IRQ */
3710
Nick Piggin03b48632009-01-20 04:36:04 +01003711#ifdef CONFIG_X86_UV
Dean Nelson4173a0e2008-10-02 12:18:21 -05003712/*
3713 * Re-target the irq to the specified CPU and enable the specified MMR located
3714 * on the specified blade to allow the sending of MSIs to the specified CPU.
3715 */
3716int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3717 unsigned long mmr_offset)
3718{
Mike Travis22f65d32008-12-16 17:33:56 -08003719 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003720 struct irq_cfg *cfg;
3721 int mmr_pnode;
3722 unsigned long mmr_value;
3723 struct uv_IO_APIC_route_entry *entry;
3724 unsigned long flags;
3725 int err;
3726
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003727 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3728
Yinghai Lu3145e942008-12-05 18:58:34 -08003729 cfg = irq_cfg(irq);
3730
Mike Travise7986732008-12-16 17:33:52 -08003731 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003732 if (err != 0)
3733 return err;
3734
3735 spin_lock_irqsave(&vector_lock, flags);
3736 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3737 irq_name);
3738 spin_unlock_irqrestore(&vector_lock, flags);
3739
Dean Nelson4173a0e2008-10-02 12:18:21 -05003740 mmr_value = 0;
3741 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003742 entry->vector = cfg->vector;
3743 entry->delivery_mode = apic->irq_delivery_mode;
3744 entry->dest_mode = apic->irq_dest_mode;
3745 entry->polarity = 0;
3746 entry->trigger = 0;
3747 entry->mask = 0;
3748 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003749
3750 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3751 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3752
Jack Steiner2a5ef412009-07-20 09:28:41 -05003753 if (cfg->move_in_progress)
3754 send_cleanup_vector(cfg);
3755
Dean Nelson4173a0e2008-10-02 12:18:21 -05003756 return irq;
3757}
3758
3759/*
3760 * Disable the specified MMR located on the specified blade so that MSIs are
3761 * longer allowed to be sent.
3762 */
3763void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3764{
3765 unsigned long mmr_value;
3766 struct uv_IO_APIC_route_entry *entry;
3767 int mmr_pnode;
3768
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003769 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3770
Dean Nelson4173a0e2008-10-02 12:18:21 -05003771 mmr_value = 0;
3772 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
Dean Nelson4173a0e2008-10-02 12:18:21 -05003773 entry->mask = 1;
3774
3775 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3776 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3777}
3778#endif /* CONFIG_X86_64 */
3779
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003780int __init io_apic_get_redir_entries (int ioapic)
3781{
3782 union IO_APIC_reg_01 reg_01;
3783 unsigned long flags;
3784
3785 spin_lock_irqsave(&ioapic_lock, flags);
3786 reg_01.raw = io_apic_read(ioapic, 1);
3787 spin_unlock_irqrestore(&ioapic_lock, flags);
3788
3789 return reg_01.bits.entries;
3790}
3791
Yinghai Lube5d5352008-12-05 18:58:33 -08003792void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003793{
Yinghai Lube5d5352008-12-05 18:58:33 -08003794 int nr = 0;
3795
Yinghai Lucc6c5002009-02-08 16:18:03 -08003796 nr = acpi_probe_gsi();
3797 if (nr > nr_irqs_gsi) {
Yinghai Lube5d5352008-12-05 18:58:33 -08003798 nr_irqs_gsi = nr;
Yinghai Lucc6c5002009-02-08 16:18:03 -08003799 } else {
3800 /* for acpi=off or acpi is not compiled in */
3801 int idx;
3802
3803 nr = 0;
3804 for (idx = 0; idx < nr_ioapics; idx++)
3805 nr += io_apic_get_redir_entries(idx) + 1;
3806
3807 if (nr > nr_irqs_gsi)
3808 nr_irqs_gsi = nr;
3809 }
3810
3811 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003812}
3813
Yinghai Lu4a046d12009-01-12 17:39:24 -08003814#ifdef CONFIG_SPARSE_IRQ
3815int __init arch_probe_nr_irqs(void)
3816{
3817 int nr;
3818
Yinghai Luf1ee5542009-02-08 16:18:03 -08003819 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3820 nr_irqs = NR_VECTORS * nr_cpu_ids;
Yinghai Lu4a046d12009-01-12 17:39:24 -08003821
Yinghai Luf1ee5542009-02-08 16:18:03 -08003822 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3823#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3824 /*
3825 * for MSI and HT dyn irq
3826 */
3827 nr += nr_irqs_gsi * 16;
3828#endif
3829 if (nr < nr_irqs)
Yinghai Lu4a046d12009-01-12 17:39:24 -08003830 nr_irqs = nr;
3831
3832 return 0;
3833}
3834#endif
3835
Yinghai Lue5198072009-05-15 13:05:16 -07003836static int __io_apic_set_pci_routing(struct device *dev, int irq,
3837 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003838{
3839 struct irq_desc *desc;
3840 struct irq_cfg *cfg;
3841 int node;
Yinghai Lue5198072009-05-15 13:05:16 -07003842 int ioapic, pin;
3843 int trigger, polarity;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003844
Yinghai Lue5198072009-05-15 13:05:16 -07003845 ioapic = irq_attr->ioapic;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003846 if (!IO_APIC_IRQ(irq)) {
3847 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3848 ioapic);
3849 return -EINVAL;
3850 }
3851
3852 if (dev)
3853 node = dev_to_node(dev);
3854 else
3855 node = cpu_to_node(boot_cpu_id);
3856
3857 desc = irq_to_desc_alloc_node(irq, node);
3858 if (!desc) {
3859 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3860 return 0;
3861 }
3862
Yinghai Lue5198072009-05-15 13:05:16 -07003863 pin = irq_attr->ioapic_pin;
3864 trigger = irq_attr->trigger;
3865 polarity = irq_attr->polarity;
3866
Yinghai Lu5ef21832009-05-06 10:08:50 -07003867 /*
3868 * IRQs < 16 are already in the irq_2_pin[] map
3869 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003870 if (irq >= nr_legacy_irqs) {
Yinghai Lu5ef21832009-05-06 10:08:50 -07003871 cfg = desc->chip_data;
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +04003872 if (add_pin_to_irq_node_nopanic(cfg, node, ioapic, pin)) {
3873 printk(KERN_INFO "can not add pin %d for irq %d\n",
3874 pin, irq);
3875 return 0;
3876 }
Yinghai Lu5ef21832009-05-06 10:08:50 -07003877 }
3878
Yinghai Lue5198072009-05-15 13:05:16 -07003879 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003880
3881 return 0;
3882}
3883
Yinghai Lue5198072009-05-15 13:05:16 -07003884int io_apic_set_pci_routing(struct device *dev, int irq,
3885 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003886{
Yinghai Lue5198072009-05-15 13:05:16 -07003887 int ioapic, pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003888 /*
3889 * Avoid pin reprogramming. PRTs typically include entries
3890 * with redundant pin->gsi mappings (but unique PCI devices);
3891 * we only program the IOAPIC on the first.
3892 */
Yinghai Lue5198072009-05-15 13:05:16 -07003893 ioapic = irq_attr->ioapic;
3894 pin = irq_attr->ioapic_pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003895 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3896 pr_debug("Pin %d-%d already programmed\n",
3897 mp_ioapics[ioapic].apicid, pin);
3898 return 0;
3899 }
3900 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3901
Yinghai Lue5198072009-05-15 13:05:16 -07003902 return __io_apic_set_pci_routing(dev, irq, irq_attr);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003903}
3904
Feng Tang2a4ab642009-07-07 23:01:15 -04003905u8 __init io_apic_unique_id(u8 id)
3906{
3907#ifdef CONFIG_X86_32
3908 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3909 !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3910 return io_apic_get_unique_id(nr_ioapics, id);
3911 else
3912 return id;
3913#else
3914 int i;
3915 DECLARE_BITMAP(used, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Feng Tang2a4ab642009-07-07 23:01:15 -04003917 bitmap_zero(used, 256);
3918 for (i = 0; i < nr_ioapics; i++) {
3919 struct mpc_ioapic *ia = &mp_ioapics[i];
3920 __set_bit(ia->apicid, used);
3921 }
3922 if (!test_bit(id, used))
3923 return id;
3924 return find_first_zero_bit(used, 256);
3925#endif
3926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Ingo Molnar54168ed2008-08-20 09:07:45 +02003928#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003929int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
3931 union IO_APIC_reg_00 reg_00;
3932 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3933 physid_mask_t tmp;
3934 unsigned long flags;
3935 int i = 0;
3936
3937 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003938 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3939 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003941 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3943 * advantage of new APIC bus architecture.
3944 */
3945
3946 if (physids_empty(apic_id_map))
Ingo Molnard190cb82009-01-28 06:50:47 +01003947 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
3949 spin_lock_irqsave(&ioapic_lock, flags);
3950 reg_00.raw = io_apic_read(ioapic, 0);
3951 spin_unlock_irqrestore(&ioapic_lock, flags);
3952
3953 if (apic_id >= get_physical_broadcast()) {
3954 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3955 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3956 apic_id = reg_00.bits.ID;
3957 }
3958
3959 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003960 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 * 'stuck on smp_invalidate_needed IPI wait' messages.
3962 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003963 if (apic->check_apicid_used(apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
3965 for (i = 0; i < get_physical_broadcast(); i++) {
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003966 if (!apic->check_apicid_used(apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 break;
3968 }
3969
3970 if (i == get_physical_broadcast())
3971 panic("Max apic_id exceeded!\n");
3972
3973 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3974 "trying %d\n", ioapic, apic_id, i);
3975
3976 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Ingo Molnar80587142009-01-28 06:50:47 +01003979 tmp = apic->apicid_to_cpu_present(apic_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 physids_or(apic_id_map, apic_id_map, tmp);
3981
3982 if (reg_00.bits.ID != apic_id) {
3983 reg_00.bits.ID = apic_id;
3984
3985 spin_lock_irqsave(&ioapic_lock, flags);
3986 io_apic_write(ioapic, 0, reg_00.raw);
3987 reg_00.raw = io_apic_read(ioapic, 0);
3988 spin_unlock_irqrestore(&ioapic_lock, flags);
3989
3990 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003991 if (reg_00.bits.ID != apic_id) {
3992 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3993 return -1;
3994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996
3997 apic_printk(APIC_VERBOSE, KERN_INFO
3998 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3999
4000 return apic_id;
4001}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00004002#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02004004int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 union IO_APIC_reg_01 reg_01;
4007 unsigned long flags;
4008
4009 spin_lock_irqsave(&ioapic_lock, flags);
4010 reg_01.raw = io_apic_read(ioapic, 1);
4011 spin_unlock_irqrestore(&ioapic_lock, flags);
4012
4013 return reg_01.bits.version;
4014}
4015
Shaohua Li61fd47e2007-11-17 01:05:28 -05004016int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
4017{
4018 int i;
4019
4020 if (skip_ioapic_setup)
4021 return -1;
4022
4023 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05304024 if (mp_irqs[i].irqtype == mp_INT &&
4025 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05004026 break;
4027 if (i >= mp_irq_entries)
4028 return -1;
4029
4030 *trigger = irq_trigger(i);
4031 *polarity = irq_polarity(i);
4032 return 0;
4033}
4034
Yinghai Lu497c9a12008-08-19 20:50:28 -07004035/*
4036 * This function currently is only a helper for the i386 smp boot process where
4037 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01004038 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07004039 */
4040#ifdef CONFIG_SMP
4041void __init setup_ioapic_dest(void)
4042{
Yinghai Lub9c61b702009-05-06 10:10:06 -07004043 int pin, ioapic = 0, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004044 struct irq_desc *desc;
Mike Travis22f65d32008-12-16 17:33:56 -08004045 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004046
4047 if (skip_ioapic_setup == 1)
4048 return;
4049
Yinghai Lub9c61b702009-05-06 10:10:06 -07004050#ifdef CONFIG_ACPI
4051 if (!acpi_disabled && acpi_ioapic) {
4052 ioapic = mp_find_ioapic(0);
4053 if (ioapic < 0)
4054 ioapic = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004055 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07004056#endif
4057
4058 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4059 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4060 if (irq_entry == -1)
4061 continue;
4062 irq = pin_2_irq(irq_entry, ioapic, pin);
4063
4064 desc = irq_to_desc(irq);
4065
4066 /*
4067 * Honour affinities which have been set in early boot
4068 */
4069 if (desc->status &
4070 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4071 mask = desc->affinity;
4072 else
4073 mask = apic->target_cpus();
4074
4075 if (intr_remapping_enabled)
4076 set_ir_ioapic_affinity_irq_desc(desc, mask);
4077 else
4078 set_ioapic_affinity_irq_desc(desc, mask);
4079 }
4080
Yinghai Lu497c9a12008-08-19 20:50:28 -07004081}
4082#endif
4083
Ingo Molnar54168ed2008-08-20 09:07:45 +02004084#define IOAPIC_RESOURCE_NAME_SIZE 11
4085
4086static struct resource *ioapic_resources;
4087
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004088static struct resource * __init ioapic_setup_resources(int nr_ioapics)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004089{
4090 unsigned long n;
4091 struct resource *res;
4092 char *mem;
4093 int i;
4094
4095 if (nr_ioapics <= 0)
4096 return NULL;
4097
4098 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4099 n *= nr_ioapics;
4100
4101 mem = alloc_bootmem(n);
4102 res = (void *)mem;
4103
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004104 mem += sizeof(struct resource) * nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004105
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004106 for (i = 0; i < nr_ioapics; i++) {
4107 res[i].name = mem;
4108 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4109 sprintf(mem, "IOAPIC %u", i);
4110 mem += IOAPIC_RESOURCE_NAME_SIZE;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004111 }
4112
4113 ioapic_resources = res;
4114
4115 return res;
4116}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004117
Yinghai Luf3294a32008-06-27 01:41:56 -07004118void __init ioapic_init_mappings(void)
4119{
4120 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004121 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004122 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004123
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004124 ioapic_res = ioapic_setup_resources(nr_ioapics);
Yinghai Luf3294a32008-06-27 01:41:56 -07004125 for (i = 0; i < nr_ioapics; i++) {
4126 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05304127 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004128#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004129 if (!ioapic_phys) {
4130 printk(KERN_ERR
4131 "WARNING: bogus zero IO-APIC "
4132 "address found in MPTABLE, "
4133 "disabling IO/APIC support!\n");
4134 smp_found_config = 0;
4135 skip_ioapic_setup = 1;
4136 goto fake_ioapic_page;
4137 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004138#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004139 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004140#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004141fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004142#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004143 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004144 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004145 ioapic_phys = __pa(ioapic_phys);
4146 }
4147 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004148 apic_printk(APIC_VERBOSE,
4149 "mapped IOAPIC to %08lx (%08lx)\n",
4150 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004151 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004152
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004153 ioapic_res->start = ioapic_phys;
4154 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4155 ioapic_res++;
Yinghai Luf3294a32008-06-27 01:41:56 -07004156 }
4157}
4158
Yinghai Lu857fdc52009-07-10 09:36:20 -07004159void __init ioapic_insert_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004160{
4161 int i;
4162 struct resource *r = ioapic_resources;
4163
4164 if (!r) {
Yinghai Lu857fdc52009-07-10 09:36:20 -07004165 if (nr_ioapics > 0)
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01004166 printk(KERN_ERR
4167 "IO APIC resources couldn't be allocated.\n");
Yinghai Lu857fdc52009-07-10 09:36:20 -07004168 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004169 }
4170
4171 for (i = 0; i < nr_ioapics; i++) {
4172 insert_resource(&iomem_resource, r);
4173 r++;
4174 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004175}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004176
Feng Tang2a4ab642009-07-07 23:01:15 -04004177int mp_find_ioapic(int gsi)
4178{
4179 int i = 0;
4180
4181 /* Find the IOAPIC that manages this GSI. */
4182 for (i = 0; i < nr_ioapics; i++) {
4183 if ((gsi >= mp_gsi_routing[i].gsi_base)
4184 && (gsi <= mp_gsi_routing[i].gsi_end))
4185 return i;
4186 }
4187
4188 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
4189 return -1;
4190}
4191
4192int mp_find_ioapic_pin(int ioapic, int gsi)
4193{
4194 if (WARN_ON(ioapic == -1))
4195 return -1;
4196 if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
4197 return -1;
4198
4199 return gsi - mp_gsi_routing[ioapic].gsi_base;
4200}
4201
4202static int bad_ioapic(unsigned long address)
4203{
4204 if (nr_ioapics >= MAX_IO_APICS) {
4205 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
4206 "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
4207 return 1;
4208 }
4209 if (!address) {
4210 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
4211 " found in table, skipping!\n");
4212 return 1;
4213 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004214 return 0;
4215}
4216
Feng Tang2a4ab642009-07-07 23:01:15 -04004217void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
4218{
4219 int idx = 0;
4220
4221 if (bad_ioapic(address))
4222 return;
4223
4224 idx = nr_ioapics;
4225
4226 mp_ioapics[idx].type = MP_IOAPIC;
4227 mp_ioapics[idx].flags = MPC_APIC_USABLE;
4228 mp_ioapics[idx].apicaddr = address;
4229
4230 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4231 mp_ioapics[idx].apicid = io_apic_unique_id(id);
4232 mp_ioapics[idx].apicver = io_apic_get_version(idx);
4233
4234 /*
4235 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4236 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4237 */
4238 mp_gsi_routing[idx].gsi_base = gsi_base;
4239 mp_gsi_routing[idx].gsi_end = gsi_base +
4240 io_apic_get_redir_entries(idx);
4241
4242 printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4243 "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4244 mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4245 mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);
4246
4247 nr_ioapics++;
4248}