blob: 24d1458a1822cc40bee94a52522799cdb5ffc75b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Ingo Molnar7b6aa332009-02-17 13:58:15 +010064#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010066#define __apicdebuginit(type) static type __init
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +040067#define for_each_irq_pin(entry, head) \
68 for (entry = head; entry; entry = entry->next)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020071 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int sis_apic_bug = -1;
75
Yinghai Luefa25592008-08-19 20:50:36 -070076static DEFINE_SPINLOCK(ioapic_lock);
77static DEFINE_SPINLOCK(vector_lock);
78
Yinghai Luefa25592008-08-19 20:50:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * # of IRQ routing registers
81 */
82int nr_ioapic_registers[MAX_IO_APICS];
83
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +053085struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086int nr_ioapics;
87
Feng Tang2a4ab642009-07-07 23:01:15 -040088/* IO APIC gsi routing info */
89struct mp_ioapic_gsi mp_gsi_routing[MAX_IO_APICS];
90
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040091/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053092struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040093
94/* # of MP IRQ source entries */
95int mp_irq_entries;
96
Thomas Gleixnerbc078442009-08-29 18:09:57 +020097/* Number of legacy interrupts */
98static int nr_legacy_irqs __read_mostly = NR_IRQS_LEGACY;
99/* GSI interrupts */
100static int nr_irqs_gsi = NR_IRQS_LEGACY;
101
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +0400102#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
103int mp_bus_id_to_type[MAX_MP_BUSSES];
104#endif
105
106DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
107
Yinghai Luefa25592008-08-19 20:50:36 -0700108int skip_ioapic_setup;
109
Ingo Molnar65a4e572009-01-31 03:36:17 +0100110void arch_disable_smp_support(void)
111{
112#ifdef CONFIG_PCI
113 noioapicquirk = 1;
114 noioapicreroute = -1;
115#endif
116 skip_ioapic_setup = 1;
117}
118
Ingo Molnar54168ed2008-08-20 09:07:45 +0200119static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700120{
121 /* disable IO-APIC */
Ingo Molnar65a4e572009-01-31 03:36:17 +0100122 arch_disable_smp_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700123 return 0;
124}
125early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200126
Yinghai Lu0f978f42008-08-19 20:50:26 -0700127struct irq_pin_list {
128 int apic, pin;
129 struct irq_pin_list *next;
130};
Yinghai Lu301e6192008-08-19 20:50:02 -0700131
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700132static struct irq_pin_list *get_one_free_irq_2_pin(int node)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800134 struct irq_pin_list *pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700135
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800136 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700137
Yinghai Lu0f978f42008-08-19 20:50:26 -0700138 return pin;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800141/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
142#ifdef CONFIG_SPARSE_IRQ
143static struct irq_cfg irq_cfgx[] = {
144#else
145static struct irq_cfg irq_cfgx[NR_IRQS] = {
146#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800147 [0] = { .vector = IRQ0_VECTOR, },
148 [1] = { .vector = IRQ1_VECTOR, },
149 [2] = { .vector = IRQ2_VECTOR, },
150 [3] = { .vector = IRQ3_VECTOR, },
151 [4] = { .vector = IRQ4_VECTOR, },
152 [5] = { .vector = IRQ5_VECTOR, },
153 [6] = { .vector = IRQ6_VECTOR, },
154 [7] = { .vector = IRQ7_VECTOR, },
155 [8] = { .vector = IRQ8_VECTOR, },
156 [9] = { .vector = IRQ9_VECTOR, },
157 [10] = { .vector = IRQ10_VECTOR, },
158 [11] = { .vector = IRQ11_VECTOR, },
159 [12] = { .vector = IRQ12_VECTOR, },
160 [13] = { .vector = IRQ13_VECTOR, },
161 [14] = { .vector = IRQ14_VECTOR, },
162 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800163};
164
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200165void __init io_apic_disable_legacy(void)
166{
167 nr_legacy_irqs = 0;
168 nr_irqs_gsi = 0;
169}
170
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800171int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800172{
173 struct irq_cfg *cfg;
174 struct irq_desc *desc;
175 int count;
Yinghai Ludad213ae2009-05-28 18:14:40 -0700176 int node;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800177 int i;
178
179 cfg = irq_cfgx;
180 count = ARRAY_SIZE(irq_cfgx);
Yinghai Ludad213ae2009-05-28 18:14:40 -0700181 node= cpu_to_node(boot_cpu_id);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800182
183 for (i = 0; i < count; i++) {
184 desc = irq_to_desc(i);
185 desc->chip_data = &cfg[i];
Yinghai Lu12274e92009-06-11 15:07:48 -0700186 zalloc_cpumask_var_node(&cfg[i].domain, GFP_NOWAIT, node);
187 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_NOWAIT, node);
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200188 if (i < nr_legacy_irqs)
Mike Travis22f65d32008-12-16 17:33:56 -0800189 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800190 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800191
192 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800193}
194
195#ifdef CONFIG_SPARSE_IRQ
Dimitri Sivanich9338ad62009-10-13 15:32:36 -0500196struct irq_cfg *irq_cfg(unsigned int irq)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800197{
198 struct irq_cfg *cfg = NULL;
199 struct irq_desc *desc;
200
201 desc = irq_to_desc(irq);
202 if (desc)
203 cfg = desc->chip_data;
204
205 return cfg;
206}
207
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700208static struct irq_cfg *get_one_free_irq_cfg(int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800209{
210 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800211
212 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800213 if (cfg) {
Li Zefan79f55992009-06-15 14:58:26 +0800214 if (!zalloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800215 kfree(cfg);
216 cfg = NULL;
Li Zefan79f55992009-06-15 14:58:26 +0800217 } else if (!zalloc_cpumask_var_node(&cfg->old_domain,
Mike Travis80855f72008-12-31 18:08:47 -0800218 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800219 free_cpumask_var(cfg->domain);
220 kfree(cfg);
221 cfg = NULL;
Mike Travis22f65d32008-12-16 17:33:56 -0800222 }
223 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800224
225 return cfg;
226}
227
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700228int arch_init_chip_data(struct irq_desc *desc, int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800229{
230 struct irq_cfg *cfg;
231
232 cfg = desc->chip_data;
233 if (!cfg) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700234 desc->chip_data = get_one_free_irq_cfg(node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800235 if (!desc->chip_data) {
236 printk(KERN_ERR "can not alloc irq_cfg\n");
237 BUG_ON(1);
238 }
239 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800240
241 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800242}
243
Yinghai Lufcef5912009-04-27 17:58:23 -0700244/* for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800245static void
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700246init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800247{
248 struct irq_pin_list *old_entry, *head, *tail, *entry;
249
250 cfg->irq_2_pin = NULL;
251 old_entry = old_cfg->irq_2_pin;
252 if (!old_entry)
253 return;
254
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700255 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800256 if (!entry)
257 return;
258
259 entry->apic = old_entry->apic;
260 entry->pin = old_entry->pin;
261 head = entry;
262 tail = entry;
263 old_entry = old_entry->next;
264 while (old_entry) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700265 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800266 if (!entry) {
267 entry = head;
268 while (entry) {
269 head = entry->next;
270 kfree(entry);
271 entry = head;
272 }
273 /* still use the old one */
274 return;
275 }
276 entry->apic = old_entry->apic;
277 entry->pin = old_entry->pin;
278 tail->next = entry;
279 tail = entry;
280 old_entry = old_entry->next;
281 }
282
283 tail->next = NULL;
284 cfg->irq_2_pin = head;
285}
286
287static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
288{
289 struct irq_pin_list *entry, *next;
290
291 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
292 return;
293
294 entry = old_cfg->irq_2_pin;
295
296 while (entry) {
297 next = entry->next;
298 kfree(entry);
299 entry = next;
300 }
301 old_cfg->irq_2_pin = NULL;
302}
303
304void arch_init_copy_chip_data(struct irq_desc *old_desc,
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700305 struct irq_desc *desc, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800306{
307 struct irq_cfg *cfg;
308 struct irq_cfg *old_cfg;
309
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700310 cfg = get_one_free_irq_cfg(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800311
312 if (!cfg)
313 return;
314
315 desc->chip_data = cfg;
316
317 old_cfg = old_desc->chip_data;
318
319 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
320
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700321 init_copy_irq_2_pin(old_cfg, cfg, node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800322}
323
324static void free_irq_cfg(struct irq_cfg *old_cfg)
325{
326 kfree(old_cfg);
327}
328
329void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
330{
331 struct irq_cfg *old_cfg, *cfg;
332
333 old_cfg = old_desc->chip_data;
334 cfg = desc->chip_data;
335
336 if (old_cfg == cfg)
337 return;
338
339 if (old_cfg) {
340 free_irq_2_pin(old_cfg, cfg);
341 free_irq_cfg(old_cfg);
342 old_desc->chip_data = NULL;
343 }
344}
Yinghai Lufcef5912009-04-27 17:58:23 -0700345/* end for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800346
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800347#else
Dimitri Sivanich9338ad62009-10-13 15:32:36 -0500348struct irq_cfg *irq_cfg(unsigned int irq)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800349{
350 return irq < nr_irqs ? irq_cfgx + irq : NULL;
351}
352
353#endif
354
Linus Torvalds130fe052006-11-01 09:11:00 -0800355struct io_apic {
356 unsigned int index;
357 unsigned int unused[3];
358 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700359 unsigned int unused2[11];
360 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800361};
362
363static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
364{
365 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +0530366 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800367}
368
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700369static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
370{
371 struct io_apic __iomem *io_apic = io_apic_base(apic);
372 writel(vector, &io_apic->eoi);
373}
374
Linus Torvalds130fe052006-11-01 09:11:00 -0800375static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
376{
377 struct io_apic __iomem *io_apic = io_apic_base(apic);
378 writel(reg, &io_apic->index);
379 return readl(&io_apic->data);
380}
381
382static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
383{
384 struct io_apic __iomem *io_apic = io_apic_base(apic);
385 writel(reg, &io_apic->index);
386 writel(value, &io_apic->data);
387}
388
389/*
390 * Re-write a value: to be used for read-modify-write
391 * cycles where the read already set up the index register.
392 *
393 * Older SiS APIC requires we rewrite the index register
394 */
395static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
396{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200397 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200398
399 if (sis_apic_bug)
400 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800401 writel(value, &io_apic->data);
402}
403
Yinghai Lu3145e942008-12-05 18:58:34 -0800404static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700405{
406 struct irq_pin_list *entry;
407 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700408
409 spin_lock_irqsave(&ioapic_lock, flags);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400410 for_each_irq_pin(entry, cfg->irq_2_pin) {
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700411 unsigned int reg;
412 int pin;
413
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700414 pin = entry->pin;
415 reg = io_apic_read(entry->apic, 0x10 + pin*2);
416 /* Is the remote IRR bit set? */
417 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
418 spin_unlock_irqrestore(&ioapic_lock, flags);
419 return true;
420 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700421 }
422 spin_unlock_irqrestore(&ioapic_lock, flags);
423
424 return false;
425}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700426
Andi Kleencf4c6a22006-09-26 10:52:30 +0200427union entry_union {
428 struct { u32 w1, w2; };
429 struct IO_APIC_route_entry entry;
430};
431
432static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
433{
434 union entry_union eu;
435 unsigned long flags;
436 spin_lock_irqsave(&ioapic_lock, flags);
437 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
438 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
439 spin_unlock_irqrestore(&ioapic_lock, flags);
440 return eu.entry;
441}
442
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800443/*
444 * When we write a new IO APIC routing entry, we need to write the high
445 * word first! If the mask bit in the low word is clear, we will enable
446 * the interrupt, and we need to make sure the entry is fully populated
447 * before that happens.
448 */
Andi Kleend15512f2006-12-07 02:14:07 +0100449static void
450__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
451{
Figo.zhang50a8d4d2009-06-17 22:25:20 +0800452 union entry_union eu = {{0, 0}};
453
Andi Kleend15512f2006-12-07 02:14:07 +0100454 eu.entry = e;
455 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
456 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
457}
458
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -0800459void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200460{
461 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200462 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100463 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800464 spin_unlock_irqrestore(&ioapic_lock, flags);
465}
466
467/*
468 * When we mask an IO APIC routing entry, we need to write the low
469 * word first, in order to set the mask bit before we change the
470 * high bits!
471 */
472static void ioapic_mask_entry(int apic, int pin)
473{
474 unsigned long flags;
475 union entry_union eu = { .entry.mask = 1 };
476
477 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200478 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
479 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
480 spin_unlock_irqrestore(&ioapic_lock, flags);
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*
484 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
485 * shared ISA-space IRQs, so we have to support them. We are super
486 * fast in the common case, and fast for shared ISA-space IRQs.
487 */
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400488static int
489add_pin_to_irq_node_nopanic(struct irq_cfg *cfg, int node, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400491 struct irq_pin_list **last, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400493 /* don't allow duplicates */
494 last = &cfg->irq_2_pin;
495 for_each_irq_pin(entry, cfg->irq_2_pin) {
Yinghai Lu0f978f42008-08-19 20:50:26 -0700496 if (entry->apic == apic && entry->pin == pin)
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400497 return 0;
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400498 last = &entry->next;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700499 }
500
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700501 entry = get_one_free_irq_2_pin(node);
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400502 if (!entry) {
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400503 printk(KERN_ERR "can not alloc irq_pin_list (%d,%d,%d)\n",
504 node, apic, pin);
505 return -ENOMEM;
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 entry->apic = apic;
508 entry->pin = pin;
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700509
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400510 *last = entry;
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400511 return 0;
512}
513
514static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
515{
516 if (add_pin_to_irq_node_nopanic(cfg, node, apic, pin))
517 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520/*
521 * Reroute an IRQ to a different pin.
522 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700523static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700524 int oldapic, int oldpin,
525 int newapic, int newpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700527 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400529 for_each_irq_pin(entry, cfg->irq_2_pin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (entry->apic == oldapic && entry->pin == oldpin) {
531 entry->apic = newapic;
532 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700533 /* every one is different, right? */
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700534 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700537
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700538 /* old apic/pin didn't exist, so just add new ones */
539 add_pin_to_irq_node(cfg, node, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700542static void io_apic_modify_irq(struct irq_cfg *cfg,
543 int mask_and, int mask_or,
544 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400545{
546 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400547 struct irq_pin_list *entry;
548
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400549 for_each_irq_pin(entry, cfg->irq_2_pin) {
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400550 unsigned int reg;
551 pin = entry->pin;
552 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
553 reg &= mask_and;
554 reg |= mask_or;
555 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
556 if (final)
557 final(entry);
558 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700559}
560
Yinghai Lu3145e942008-12-05 18:58:34 -0800561static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400562{
Yinghai Lu3145e942008-12-05 18:58:34 -0800563 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400564}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700565
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530566static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700567{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400568 /*
569 * Synchronize the IO-APIC and the CPU by doing
570 * a dummy read from the IO-APIC
571 */
572 struct io_apic __iomem *io_apic;
573 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700574 readl(&io_apic->data);
575}
576
Yinghai Lu3145e942008-12-05 18:58:34 -0800577static void __mask_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, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400580}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700581
Yinghai Lu3145e942008-12-05 18:58:34 -0800582static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400583{
Yinghai Lu3145e942008-12-05 18:58:34 -0800584 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400585 IO_APIC_REDIR_MASKED, NULL);
586}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700587
Yinghai Lu3145e942008-12-05 18:58:34 -0800588static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400589{
Yinghai Lu3145e942008-12-05 18:58:34 -0800590 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400591 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
592}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700593
Yinghai Lu3145e942008-12-05 18:58:34 -0800594static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Yinghai Lu3145e942008-12-05 18:58:34 -0800596 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned long flags;
598
Yinghai Lu3145e942008-12-05 18:58:34 -0800599 BUG_ON(!cfg);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800602 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_unlock_irqrestore(&ioapic_lock, flags);
604}
605
Yinghai Lu3145e942008-12-05 18:58:34 -0800606static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Yinghai Lu3145e942008-12-05 18:58:34 -0800608 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 unsigned long flags;
610
611 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800612 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 spin_unlock_irqrestore(&ioapic_lock, flags);
614}
615
Yinghai Lu3145e942008-12-05 18:58:34 -0800616static void mask_IO_APIC_irq(unsigned int irq)
617{
618 struct irq_desc *desc = irq_to_desc(irq);
619
620 mask_IO_APIC_irq_desc(desc);
621}
622static void unmask_IO_APIC_irq(unsigned int irq)
623{
624 struct irq_desc *desc = irq_to_desc(irq);
625
626 unmask_IO_APIC_irq_desc(desc);
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
630{
631 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200634 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (entry.delivery_mode == dest_SMI)
636 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * Disable it in the IO-APIC irq-routing table:
639 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800640 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Ingo Molnar54168ed2008-08-20 09:07:45 +0200643static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int apic, pin;
646
647 for (apic = 0; apic < nr_ioapics; apic++)
648 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
649 clear_IO_APIC_pin(apic, pin);
650}
651
Ingo Molnar54168ed2008-08-20 09:07:45 +0200652#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
654 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
655 * specific CPU-side IRQs.
656 */
657
658#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800659static int pirq_entries[MAX_PIRQS] = {
660 [0 ... MAX_PIRQS - 1] = -1
661};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663static int __init ioapic_pirq_setup(char *str)
664{
665 int i, max;
666 int ints[MAX_PIRQS+1];
667
668 get_options(str, ARRAY_SIZE(ints), ints);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 apic_printk(APIC_VERBOSE, KERN_INFO
671 "PIRQ redirection, working around broken MP-BIOS.\n");
672 max = MAX_PIRQS;
673 if (ints[0] < MAX_PIRQS)
674 max = ints[0];
675
676 for (i = 0; i < max; i++) {
677 apic_printk(APIC_VERBOSE, KERN_DEBUG
678 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
679 /*
680 * PIRQs are mapped upside down, usually.
681 */
682 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
683 }
684 return 1;
685}
686
687__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200688#endif /* CONFIG_X86_32 */
689
Fenghua Yub24696b2009-03-27 14:22:44 -0700690struct IO_APIC_route_entry **alloc_ioapic_entries(void)
691{
692 int apic;
693 struct IO_APIC_route_entry **ioapic_entries;
694
695 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
696 GFP_ATOMIC);
697 if (!ioapic_entries)
698 return 0;
699
700 for (apic = 0; apic < nr_ioapics; apic++) {
701 ioapic_entries[apic] =
702 kzalloc(sizeof(struct IO_APIC_route_entry) *
703 nr_ioapic_registers[apic], GFP_ATOMIC);
704 if (!ioapic_entries[apic])
705 goto nomem;
706 }
707
708 return ioapic_entries;
709
710nomem:
711 while (--apic >= 0)
712 kfree(ioapic_entries[apic]);
713 kfree(ioapic_entries);
714
715 return 0;
716}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200717
718/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700719 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200720 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700721int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200722{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200723 int apic, pin;
724
Fenghua Yub24696b2009-03-27 14:22:44 -0700725 if (!ioapic_entries)
726 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200727
728 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700729 if (!ioapic_entries[apic])
730 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200731
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700732 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
Fenghua Yub24696b2009-03-27 14:22:44 -0700733 ioapic_entries[apic][pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200734 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700735 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400736
Ingo Molnar54168ed2008-08-20 09:07:45 +0200737 return 0;
738}
739
Fenghua Yub24696b2009-03-27 14:22:44 -0700740/*
741 * Mask all IO APIC entries.
742 */
743void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700744{
745 int apic, pin;
746
Fenghua Yub24696b2009-03-27 14:22:44 -0700747 if (!ioapic_entries)
748 return;
749
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700750 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700751 if (!ioapic_entries[apic])
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700752 break;
Fenghua Yub24696b2009-03-27 14:22:44 -0700753
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700754 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
755 struct IO_APIC_route_entry entry;
756
Fenghua Yub24696b2009-03-27 14:22:44 -0700757 entry = ioapic_entries[apic][pin];
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700758 if (!entry.mask) {
759 entry.mask = 1;
760 ioapic_write_entry(apic, pin, entry);
761 }
762 }
763 }
764}
765
Fenghua Yub24696b2009-03-27 14:22:44 -0700766/*
767 * Restore IO APIC entries which was saved in ioapic_entries.
768 */
769int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200770{
771 int apic, pin;
772
Fenghua Yub24696b2009-03-27 14:22:44 -0700773 if (!ioapic_entries)
774 return -ENOMEM;
775
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400776 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700777 if (!ioapic_entries[apic])
778 return -ENOMEM;
779
Ingo Molnar54168ed2008-08-20 09:07:45 +0200780 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
781 ioapic_write_entry(apic, pin,
Fenghua Yub24696b2009-03-27 14:22:44 -0700782 ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400783 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700784 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200785}
786
Fenghua Yub24696b2009-03-27 14:22:44 -0700787void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
788{
789 int apic;
790
791 for (apic = 0; apic < nr_ioapics; apic++)
792 kfree(ioapic_entries[apic]);
793
794 kfree(ioapic_entries);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797/*
798 * Find the IRQ entry number of a certain pin.
799 */
800static int find_irq_entry(int apic, int pin, int type)
801{
802 int i;
803
804 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530805 if (mp_irqs[i].irqtype == type &&
806 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
807 mp_irqs[i].dstapic == MP_APIC_ALL) &&
808 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return i;
810
811 return -1;
812}
813
814/*
815 * Find the pin to which IRQ[irq] (ISA) is connected
816 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800817static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 int i;
820
821 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530822 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300824 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530825 (mp_irqs[i].irqtype == type) &&
826 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530828 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 return -1;
831}
832
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800833static int __init find_isa_irq_apic(int irq, int type)
834{
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;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800839
Alexey Starikovskiy73b29612008-03-20 14:54:24 +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))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800843 break;
844 }
845 if (i < mp_irq_entries) {
846 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200847 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530848 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800849 return apic;
850 }
851 }
852
853 return -1;
854}
855
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300856#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/*
858 * EISA Edge/Level control register, ELCR
859 */
860static int EISA_ELCR(unsigned int irq)
861{
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200862 if (irq < nr_legacy_irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 unsigned int port = 0x4d0 + (irq >> 3);
864 return (inb(port) >> (irq & 7)) & 1;
865 }
866 apic_printk(APIC_VERBOSE, KERN_INFO
867 "Broken MPtable reports ISA irq %d\n", irq);
868 return 0;
869}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200870
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300873/* ISA interrupts are always polarity zero edge triggered,
874 * when listed as conforming in the MP table. */
875
876#define default_ISA_trigger(idx) (0)
877#define default_ISA_polarity(idx) (0)
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/* EISA interrupts are always polarity zero and can be edge or level
880 * trigger depending on the ELCR value. If an interrupt is listed as
881 * EISA conforming in the MP table, that means its trigger type must
882 * be read in from the ELCR */
883
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530884#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300885#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887/* PCI interrupts are always polarity one level triggered,
888 * when listed as conforming in the MP table. */
889
890#define default_PCI_trigger(idx) (1)
891#define default_PCI_polarity(idx) (1)
892
893/* MCA interrupts are always polarity zero level triggered,
894 * when listed as conforming in the MP table. */
895
896#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300897#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Shaohua Li61fd47e2007-11-17 01:05:28 -0500899static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530901 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int polarity;
903
904 /*
905 * Determine IRQ line polarity (high active or low active):
906 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530907 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200909 case 0: /* conforms, ie. bus-type dependent polarity */
910 if (test_bit(bus, mp_bus_not_pci))
911 polarity = default_ISA_polarity(idx);
912 else
913 polarity = default_PCI_polarity(idx);
914 break;
915 case 1: /* high active */
916 {
917 polarity = 0;
918 break;
919 }
920 case 2: /* reserved */
921 {
922 printk(KERN_WARNING "broken BIOS!!\n");
923 polarity = 1;
924 break;
925 }
926 case 3: /* low active */
927 {
928 polarity = 1;
929 break;
930 }
931 default: /* invalid */
932 {
933 printk(KERN_WARNING "broken BIOS!!\n");
934 polarity = 1;
935 break;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 return polarity;
939}
940
941static int MPBIOS_trigger(int idx)
942{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530943 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int trigger;
945
946 /*
947 * Determine IRQ trigger mode (edge or level sensitive):
948 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530949 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200951 case 0: /* conforms, ie. bus-type dependent */
952 if (test_bit(bus, mp_bus_not_pci))
953 trigger = default_ISA_trigger(idx);
954 else
955 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300956#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200957 switch (mp_bus_id_to_type[bus]) {
958 case MP_BUS_ISA: /* ISA pin */
959 {
960 /* set before the switch */
961 break;
962 }
963 case MP_BUS_EISA: /* EISA pin */
964 {
965 trigger = default_EISA_trigger(idx);
966 break;
967 }
968 case MP_BUS_PCI: /* PCI pin */
969 {
970 /* set before the switch */
971 break;
972 }
973 case MP_BUS_MCA: /* MCA pin */
974 {
975 trigger = default_MCA_trigger(idx);
976 break;
977 }
978 default:
979 {
980 printk(KERN_WARNING "broken BIOS!!\n");
981 trigger = 1;
982 break;
983 }
984 }
985#endif
986 break;
987 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200988 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200989 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200990 break;
991 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200992 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200993 {
994 printk(KERN_WARNING "broken BIOS!!\n");
995 trigger = 1;
996 break;
997 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200998 case 3: /* level */
999 {
1000 trigger = 1;
1001 break;
1002 }
1003 default: /* invalid */
1004 {
1005 printk(KERN_WARNING "broken BIOS!!\n");
1006 trigger = 0;
1007 break;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 return trigger;
1011}
1012
1013static inline int irq_polarity(int idx)
1014{
1015 return MPBIOS_polarity(idx);
1016}
1017
1018static inline int irq_trigger(int idx)
1019{
1020 return MPBIOS_trigger(idx);
1021}
1022
Yinghai Luefa25592008-08-19 20:50:36 -07001023int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static int pin_2_irq(int idx, int apic, int pin)
1025{
1026 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301027 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /*
1030 * Debugging check, we are in big trouble if this message pops up!
1031 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301032 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1034
Ingo Molnar54168ed2008-08-20 09:07:45 +02001035 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301036 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001037 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001038 /*
1039 * PCI IRQs are mapped in order
1040 */
1041 i = irq = 0;
1042 while (i < apic)
1043 irq += nr_ioapic_registers[i++];
1044 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001045 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001046 * For MPS mode, so far only needed by ES7000 platform
1047 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001048 if (ioapic_renumber_irq)
1049 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Ingo Molnar54168ed2008-08-20 09:07:45 +02001052#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1055 */
1056 if ((pin >= 16) && (pin <= 23)) {
1057 if (pirq_entries[pin-16] != -1) {
1058 if (!pirq_entries[pin-16]) {
1059 apic_printk(APIC_VERBOSE, KERN_DEBUG
1060 "disabling PIRQ%d\n", pin-16);
1061 } else {
1062 irq = pirq_entries[pin-16];
1063 apic_printk(APIC_VERBOSE, KERN_DEBUG
1064 "using PIRQ%d -> IRQ %d\n",
1065 pin-16, irq);
1066 }
1067 }
1068 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001069#endif
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return irq;
1072}
1073
Yinghai Lue20c06f2009-05-06 10:08:22 -07001074/*
1075 * Find a specific PCI IRQ entry.
1076 * Not an __init, possibly needed by modules
1077 */
1078int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
Yinghai Lue5198072009-05-15 13:05:16 -07001079 struct io_apic_irq_attr *irq_attr)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001080{
1081 int apic, i, best_guess = -1;
1082
1083 apic_printk(APIC_DEBUG,
1084 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1085 bus, slot, pin);
1086 if (test_bit(bus, mp_bus_not_pci)) {
1087 apic_printk(APIC_VERBOSE,
1088 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1089 return -1;
1090 }
1091 for (i = 0; i < mp_irq_entries; i++) {
1092 int lbus = mp_irqs[i].srcbus;
1093
1094 for (apic = 0; apic < nr_ioapics; apic++)
1095 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1096 mp_irqs[i].dstapic == MP_APIC_ALL)
1097 break;
1098
1099 if (!test_bit(lbus, mp_bus_not_pci) &&
1100 !mp_irqs[i].irqtype &&
1101 (bus == lbus) &&
1102 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1103 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1104
1105 if (!(apic || IO_APIC_IRQ(irq)))
1106 continue;
1107
1108 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Yinghai Lue5198072009-05-15 13:05:16 -07001109 set_io_apic_irq_attr(irq_attr, apic,
1110 mp_irqs[i].dstirq,
1111 irq_trigger(i),
1112 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001113 return irq;
1114 }
1115 /*
1116 * Use the first all-but-pin matching entry as a
1117 * best-guess fuzzy result for broken mptables.
1118 */
1119 if (best_guess < 0) {
Yinghai Lue5198072009-05-15 13:05:16 -07001120 set_io_apic_irq_attr(irq_attr, apic,
1121 mp_irqs[i].dstirq,
1122 irq_trigger(i),
1123 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001124 best_guess = irq;
1125 }
1126 }
1127 }
1128 return best_guess;
1129}
1130EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1131
Yinghai Lu497c9a12008-08-19 20:50:28 -07001132void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001134 /* Used to the online set of cpus does not change
1135 * during assign_irq_vector.
1136 */
1137 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Yinghai Lu497c9a12008-08-19 20:50:28 -07001140void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001141{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001142 spin_unlock(&vector_lock);
1143}
1144
Mike Travise7986732008-12-16 17:33:52 -08001145static int
1146__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001147{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001148 /*
1149 * NOTE! The local APIC isn't very good at handling
1150 * multiple interrupts at the same interrupt level.
1151 * As the interrupt level is determined by taking the
1152 * vector number and shifting that right by 4, we
1153 * want to spread these out a bit so that they don't
1154 * all fall in the same interrupt level.
1155 *
1156 * Also, we've got to be careful not to trash gate
1157 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1158 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001159 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1160 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001161 int cpu, err;
1162 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001163
Suresh Siddha23359a82009-10-26 14:24:33 -08001164 if (cfg->move_in_progress)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001165 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001166
Mike Travis22f65d32008-12-16 17:33:56 -08001167 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1168 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001169
Ingo Molnar54168ed2008-08-20 09:07:45 +02001170 old_vector = cfg->vector;
1171 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001172 cpumask_and(tmp_mask, mask, cpu_online_mask);
1173 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1174 if (!cpumask_empty(tmp_mask)) {
1175 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001176 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001177 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001178 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001179
Mike Travise7986732008-12-16 17:33:52 -08001180 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001181 err = -ENOSPC;
1182 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001183 int new_cpu;
1184 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001185
Ingo Molnare2d40b12009-01-28 06:50:47 +01001186 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001187
Ingo Molnar54168ed2008-08-20 09:07:45 +02001188 vector = current_vector;
1189 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001190next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001191 vector += 8;
1192 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001193 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001194 offset = (offset + 1) % 8;
1195 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001196 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001197 if (unlikely(current_vector == vector))
1198 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001199
1200 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001201 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001202
Mike Travis22f65d32008-12-16 17:33:56 -08001203 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001204 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1205 goto next;
1206 /* Found one! */
1207 current_vector = vector;
1208 current_offset = offset;
1209 if (old_vector) {
1210 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001211 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001212 }
Mike Travis22f65d32008-12-16 17:33:56 -08001213 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001214 per_cpu(vector_irq, new_cpu)[vector] = irq;
1215 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001216 cpumask_copy(cfg->domain, tmp_mask);
1217 err = 0;
1218 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001219 }
Mike Travis22f65d32008-12-16 17:33:56 -08001220 free_cpumask_var(tmp_mask);
1221 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001222}
1223
Dimitri Sivanich9338ad62009-10-13 15:32:36 -05001224int assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001225{
1226 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001227 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001228
1229 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001230 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001231 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001232 return err;
1233}
1234
Yinghai Lu3145e942008-12-05 18:58:34 -08001235static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001236{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001237 int cpu, vector;
1238
Yinghai Lu497c9a12008-08-19 20:50:28 -07001239 BUG_ON(!cfg->vector);
1240
1241 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001242 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001243 per_cpu(vector_irq, cpu)[vector] = -1;
1244
1245 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001246 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001247
1248 if (likely(!cfg->move_in_progress))
1249 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001250 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001251 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1252 vector++) {
1253 if (per_cpu(vector_irq, cpu)[vector] != irq)
1254 continue;
1255 per_cpu(vector_irq, cpu)[vector] = -1;
1256 break;
1257 }
1258 }
1259 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001260}
1261
1262void __setup_vector_irq(int cpu)
1263{
1264 /* Initialize vector_irq on a new cpu */
1265 /* This function must be called with vector_lock held */
1266 int irq, vector;
1267 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001268 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001269
1270 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001271 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001272 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001273 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001274 continue;
1275 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001276 per_cpu(vector_irq, cpu)[vector] = irq;
1277 }
1278 /* Mark the free vectors */
1279 for (vector = 0; vector < NR_VECTORS; ++vector) {
1280 irq = per_cpu(vector_irq, cpu)[vector];
1281 if (irq < 0)
1282 continue;
1283
1284 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001285 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001286 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001287 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001288}
Glauber Costa3fde6902008-05-28 20:34:19 -07001289
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001290static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001291static struct irq_chip ir_ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Ingo Molnar54168ed2008-08-20 09:07:45 +02001293#define IOAPIC_AUTO -1
1294#define IOAPIC_EDGE 0
1295#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001297#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001298static inline int IO_APIC_irq_trigger(int irq)
1299{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001300 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001301
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001302 for (apic = 0; apic < nr_ioapics; apic++) {
1303 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1304 idx = find_irq_entry(apic, pin, mp_INT);
1305 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1306 return irq_trigger(idx);
1307 }
1308 }
1309 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310 * nonexistent IRQs are edge default
1311 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001312 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001313}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001314#else
1315static inline int IO_APIC_irq_trigger(int irq)
1316{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001317 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001318}
1319#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001320
Yinghai Lu3145e942008-12-05 18:58:34 -08001321static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Yinghai Lu199751d2008-08-19 20:50:27 -07001323
Jan Beulich6ebcc002006-06-26 13:56:46 +02001324 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001325 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001326 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001327 else
1328 desc->status &= ~IRQ_LEVEL;
1329
Ingo Molnar54168ed2008-08-20 09:07:45 +02001330 if (irq_remapped(irq)) {
1331 desc->status |= IRQ_MOVE_PCNTXT;
1332 if (trigger)
1333 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1334 handle_fasteoi_irq,
1335 "fasteoi");
1336 else
1337 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1338 handle_edge_irq, "edge");
1339 return;
1340 }
Suresh Siddha29b61be2009-03-16 17:05:02 -07001341
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001342 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1343 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001344 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001345 handle_fasteoi_irq,
1346 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001347 else
Ingo Molnara460e742006-10-17 00:10:03 -07001348 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001349 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001350}
1351
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -08001352int setup_ioapic_entry(int apic_id, int irq,
1353 struct IO_APIC_route_entry *entry,
1354 unsigned int destination, int trigger,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001355 int polarity, int vector, int pin)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001356{
1357 /*
1358 * add it to the IO-APIC irq-routing table:
1359 */
1360 memset(entry,0,sizeof(*entry));
1361
Ingo Molnar54168ed2008-08-20 09:07:45 +02001362 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001363 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001364 struct irte irte;
1365 struct IR_IO_APIC_route_entry *ir_entry =
1366 (struct IR_IO_APIC_route_entry *) entry;
1367 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001368
Ingo Molnar54168ed2008-08-20 09:07:45 +02001369 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001370 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001371
1372 index = alloc_irte(iommu, irq, 1);
1373 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001374 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001375
1376 memset(&irte, 0, sizeof(irte));
1377
1378 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001379 irte.dst_mode = apic->irq_dest_mode;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001380 /*
1381 * Trigger mode in the IRTE will always be edge, and the
1382 * actual level or edge trigger will be setup in the IO-APIC
1383 * RTE. This will help simplify level triggered irq migration.
1384 * For more details, see the comments above explainig IO-APIC
1385 * irq migration in the presence of interrupt-remapping.
1386 */
1387 irte.trigger_mode = 0;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001388 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001389 irte.vector = vector;
1390 irte.dest_id = IRTE_DEST(destination);
1391
Weidong Hanf007e992009-05-23 00:41:15 +08001392 /* Set source-id of interrupt request */
1393 set_ioapic_sid(&irte, apic_id);
1394
Ingo Molnar54168ed2008-08-20 09:07:45 +02001395 modify_irte(irq, &irte);
1396
1397 ir_entry->index2 = (index >> 15) & 0x1;
1398 ir_entry->zero = 0;
1399 ir_entry->format = 1;
1400 ir_entry->index = (index & 0x7fff);
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001401 /*
1402 * IO-APIC RTE will be configured with virtual vector.
1403 * irq handler will do the explicit EOI to the io-apic.
1404 */
1405 ir_entry->vector = pin;
Suresh Siddha29b61be2009-03-16 17:05:02 -07001406 } else {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001407 entry->delivery_mode = apic->irq_delivery_mode;
1408 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001409 entry->dest = destination;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001410 entry->vector = vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001411 }
1412
1413 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001414 entry->trigger = trigger;
1415 entry->polarity = polarity;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001416
1417 /* Mask level triggered irqs.
1418 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1419 */
1420 if (trigger)
1421 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001422 return 0;
1423}
1424
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001425static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001426 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001427{
1428 struct irq_cfg *cfg;
1429 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001430 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001431
1432 if (!IO_APIC_IRQ(irq))
1433 return;
1434
Yinghai Lu3145e942008-12-05 18:58:34 -08001435 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001436
Ingo Molnarfe402e12009-01-28 04:32:51 +01001437 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001438 return;
1439
Ingo Molnardebccb32009-01-28 15:20:18 +01001440 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001441
1442 apic_printk(APIC_VERBOSE,KERN_DEBUG
1443 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1444 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001445 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001446 irq, trigger, polarity);
1447
1448
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001449 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001450 dest, trigger, polarity, cfg->vector, pin)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001451 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001452 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001453 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001454 return;
1455 }
1456
Yinghai Lu3145e942008-12-05 18:58:34 -08001457 ioapic_register_intr(irq, desc, trigger);
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001458 if (irq < nr_legacy_irqs)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001459 disable_8259A_irq(irq);
1460
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001461 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Yinghai Lub9c61b702009-05-06 10:10:06 -07001464static struct {
1465 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1466} mp_ioapic_routing[MAX_IO_APICS];
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468static void __init setup_IO_APIC_irqs(void)
1469{
Yinghai Lub9c61b702009-05-06 10:10:06 -07001470 int apic_id = 0, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001471 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001472 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001473 struct irq_cfg *cfg;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07001474 int node = cpu_to_node(boot_cpu_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1477
Yinghai Lub9c61b702009-05-06 10:10:06 -07001478#ifdef CONFIG_ACPI
1479 if (!acpi_disabled && acpi_ioapic) {
1480 apic_id = mp_find_ioapic(0);
1481 if (apic_id < 0)
1482 apic_id = 0;
1483 }
1484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Yinghai Lub9c61b702009-05-06 10:10:06 -07001486 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1487 idx = find_irq_entry(apic_id, pin, mp_INT);
1488 if (idx == -1) {
1489 if (!notcon) {
1490 notcon = 1;
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001491 apic_printk(APIC_VERBOSE,
Yinghai Lub9c61b702009-05-06 10:10:06 -07001492 KERN_DEBUG " %d-%d",
1493 mp_ioapics[apic_id].apicid, pin);
1494 } else
1495 apic_printk(APIC_VERBOSE, " %d-%d",
1496 mp_ioapics[apic_id].apicid, pin);
1497 continue;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001498 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07001499 if (notcon) {
1500 apic_printk(APIC_VERBOSE,
1501 " (apicid-pin) not connected\n");
1502 notcon = 0;
1503 }
1504
1505 irq = pin_2_irq(idx, apic_id, pin);
1506
1507 /*
1508 * Skip the timer IRQ if there's a quirk handler
1509 * installed and if it returns 1:
1510 */
1511 if (apic->multi_timer_check &&
1512 apic->multi_timer_check(apic_id, irq))
1513 continue;
1514
1515 desc = irq_to_desc_alloc_node(irq, node);
1516 if (!desc) {
1517 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1518 continue;
1519 }
1520 cfg = desc->chip_data;
1521 add_pin_to_irq_node(cfg, node, apic_id, pin);
Yinghai Lu4c6f18f2009-05-18 10:23:28 -07001522 /*
1523 * don't mark it in pin_programmed, so later acpi could
1524 * set it correctly when irq < 16
1525 */
Yinghai Lub9c61b702009-05-06 10:10:06 -07001526 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1527 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001530 if (notcon)
1531 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001532 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001536 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001538static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001539 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Ingo Molnar54168ed2008-08-20 09:07:45 +02001543 if (intr_remapping_enabled)
1544 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001545
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001546 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /*
1549 * We use logical delivery to get the timer IRQ
1550 * to the first CPU.
1551 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001552 entry.dest_mode = apic->irq_dest_mode;
Yinghai Luf72dcca2009-02-08 16:18:03 -08001553 entry.mask = 0; /* don't mask IRQ for edge */
Ingo Molnardebccb32009-01-28 15:20:18 +01001554 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001555 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 entry.polarity = 0;
1557 entry.trigger = 0;
1558 entry.vector = vector;
1559
1560 /*
1561 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001562 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001564 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /*
1567 * Add it to the IO-APIC irq-routing table:
1568 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001569 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001572
1573__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 int apic, i;
1576 union IO_APIC_reg_00 reg_00;
1577 union IO_APIC_reg_01 reg_01;
1578 union IO_APIC_reg_02 reg_02;
1579 union IO_APIC_reg_03 reg_03;
1580 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001581 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001582 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001583 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001585 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 for (i = 0; i < nr_ioapics; i++)
1587 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301588 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
1591 * We are a bit conservative about what we expect. We have to
1592 * know about every hardware change ASAP.
1593 */
1594 printk(KERN_INFO "testing the IO APIC.......................\n");
1595
1596 for (apic = 0; apic < nr_ioapics; apic++) {
1597
1598 spin_lock_irqsave(&ioapic_lock, flags);
1599 reg_00.raw = io_apic_read(apic, 0);
1600 reg_01.raw = io_apic_read(apic, 1);
1601 if (reg_01.bits.version >= 0x10)
1602 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001603 if (reg_01.bits.version >= 0x20)
1604 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 spin_unlock_irqrestore(&ioapic_lock, flags);
1606
Ingo Molnar54168ed2008-08-20 09:07:45 +02001607 printk("\n");
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301608 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1610 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1611 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1612 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Ingo Molnar54168ed2008-08-20 09:07:45 +02001614 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1618 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 /*
1621 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1622 * but the value of reg_02 is read as the previous read register
1623 * value, so ignore it if reg_02 == reg_01.
1624 */
1625 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1626 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1627 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629
1630 /*
1631 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1632 * or reg_03, but the value of reg_0[23] is read as the previous read
1633 * register value, so ignore it if reg_03 == reg_0[12].
1634 */
1635 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1636 reg_03.raw != reg_01.raw) {
1637 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1638 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640
1641 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1642
Yinghai Lud83e94a2008-08-19 20:50:33 -07001643 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1644 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 for (i = 0; i <= reg_01.bits.entries; i++) {
1647 struct IO_APIC_route_entry entry;
1648
Andi Kleencf4c6a22006-09-26 10:52:30 +02001649 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Ingo Molnar54168ed2008-08-20 09:07:45 +02001651 printk(KERN_DEBUG " %02x %03X ",
1652 i,
1653 entry.dest
1654 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1657 entry.mask,
1658 entry.trigger,
1659 entry.irr,
1660 entry.polarity,
1661 entry.delivery_status,
1662 entry.dest_mode,
1663 entry.delivery_mode,
1664 entry.vector
1665 );
1666 }
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001669 for_each_irq_desc(irq, desc) {
1670 struct irq_pin_list *entry;
1671
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001672 cfg = desc->chip_data;
1673 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001674 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001676 printk(KERN_DEBUG "IRQ%d ", irq);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04001677 for_each_irq_pin(entry, cfg->irq_2_pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 printk("-> %d:%d", entry->apic, entry->pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 printk("\n");
1680 }
1681
1682 printk(KERN_INFO ".................................... done.\n");
1683
1684 return;
1685}
1686
Ingo Molnar251e1e42009-07-02 08:54:01 +02001687__apicdebuginit(void) print_APIC_field(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Ingo Molnar251e1e42009-07-02 08:54:01 +02001689 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Ingo Molnar251e1e42009-07-02 08:54:01 +02001691 printk(KERN_DEBUG);
1692
1693 for (i = 0; i < 8; i++)
1694 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1695
1696 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001699__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Andreas Herrmann97a52712009-05-08 18:23:50 +02001701 unsigned int i, v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001702 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Ingo Molnar251e1e42009-07-02 08:54:01 +02001704 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001706 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001707 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 v = apic_read(APIC_LVR);
1709 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1710 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001711 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 v = apic_read(APIC_TASKPRI);
1714 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1715
Ingo Molnar54168ed2008-08-20 09:07:45 +02001716 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001717 if (!APIC_XAPIC(ver)) {
1718 v = apic_read(APIC_ARBPRI);
1719 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1720 v & APIC_ARBPRI_MASK);
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 v = apic_read(APIC_PROCPRI);
1723 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1724 }
1725
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001726 /*
1727 * Remote read supported only in the 82489DX and local APIC for
1728 * Pentium processors.
1729 */
1730 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1731 v = apic_read(APIC_RRR);
1732 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1733 }
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 v = apic_read(APIC_LDR);
1736 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001737 if (!x2apic_enabled()) {
1738 v = apic_read(APIC_DFR);
1739 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 v = apic_read(APIC_SPIV);
1742 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1743
1744 printk(KERN_DEBUG "... APIC ISR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001745 print_APIC_field(APIC_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 printk(KERN_DEBUG "... APIC TMR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001747 print_APIC_field(APIC_TMR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 printk(KERN_DEBUG "... APIC IRR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001749 print_APIC_field(APIC_IRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Ingo Molnar54168ed2008-08-20 09:07:45 +02001751 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1752 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 v = apic_read(APIC_ESR);
1756 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1757 }
1758
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001759 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001760 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1761 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 v = apic_read(APIC_LVTT);
1764 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1765
1766 if (maxlvt > 3) { /* PC is LVT#4. */
1767 v = apic_read(APIC_LVTPC);
1768 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1769 }
1770 v = apic_read(APIC_LVT0);
1771 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1772 v = apic_read(APIC_LVT1);
1773 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1774
1775 if (maxlvt > 2) { /* ERR is LVT#3. */
1776 v = apic_read(APIC_LVTERR);
1777 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1778 }
1779
1780 v = apic_read(APIC_TMICT);
1781 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1782 v = apic_read(APIC_TMCCT);
1783 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1784 v = apic_read(APIC_TDCR);
1785 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
Andreas Herrmann97a52712009-05-08 18:23:50 +02001786
1787 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1788 v = apic_read(APIC_EFEAT);
1789 maxlvt = (v >> 16) & 0xff;
1790 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1791 v = apic_read(APIC_ECTRL);
1792 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1793 for (i = 0; i < maxlvt; i++) {
1794 v = apic_read(APIC_EILVTn(i));
1795 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1796 }
1797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 printk("\n");
1799}
1800
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001801__apicdebuginit(void) print_local_APICs(int maxcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001803 int cpu;
1804
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001805 if (!maxcpu)
1806 return;
1807
Yinghai Luffd5aae2008-08-19 20:50:50 -07001808 preempt_disable();
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001809 for_each_online_cpu(cpu) {
1810 if (cpu >= maxcpu)
1811 break;
Yinghai Luffd5aae2008-08-19 20:50:50 -07001812 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001813 }
Yinghai Luffd5aae2008-08-19 20:50:50 -07001814 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001817__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 unsigned int v;
1820 unsigned long flags;
1821
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001822 if (!nr_legacy_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return;
1824
1825 printk(KERN_DEBUG "\nprinting PIC contents\n");
1826
1827 spin_lock_irqsave(&i8259A_lock, flags);
1828
1829 v = inb(0xa1) << 8 | inb(0x21);
1830 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1831
1832 v = inb(0xa0) << 8 | inb(0x20);
1833 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1834
Ingo Molnar54168ed2008-08-20 09:07:45 +02001835 outb(0x0b,0xa0);
1836 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001838 outb(0x0a,0xa0);
1839 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 spin_unlock_irqrestore(&i8259A_lock, flags);
1842
1843 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1844
1845 v = inb(0x4d1) << 8 | inb(0x4d0);
1846 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1847}
1848
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001849static int __initdata show_lapic = 1;
1850static __init int setup_show_lapic(char *arg)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001851{
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001852 int num = -1;
1853
1854 if (strcmp(arg, "all") == 0) {
1855 show_lapic = CONFIG_NR_CPUS;
1856 } else {
1857 get_option(&arg, &num);
1858 if (num >= 0)
1859 show_lapic = num;
1860 }
1861
1862 return 1;
1863}
1864__setup("show_lapic=", setup_show_lapic);
1865
1866__apicdebuginit(int) print_ICs(void)
1867{
1868 if (apic_verbosity == APIC_QUIET)
1869 return 0;
1870
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001871 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
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001877 print_local_APICs(show_lapic);
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001878 print_IO_APIC();
1879
1880 return 0;
1881}
1882
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001883fs_initcall(print_ICs);
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001884
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
Dimitri Sivanich9338ad62009-10-13 15:32:36 -05002231void send_cleanup_vector(struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002232{
2233 cpumask_var_t cleanup_mask;
2234
2235 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2236 unsigned int i;
Gary Hadee85abf82009-04-08 14:07:25 -07002237 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2238 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2239 } else {
2240 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002241 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2242 free_cpumask_var(cleanup_mask);
2243 }
2244 cfg->move_in_progress = 0;
2245}
2246
Ingo Molnar44204712009-05-01 19:02:50 +02002247static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002248{
2249 int apic, pin;
2250 struct irq_pin_list *entry;
2251 u8 vector = cfg->vector;
2252
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04002253 for_each_irq_pin(entry, cfg->irq_2_pin) {
Gary Hadee85abf82009-04-08 14:07:25 -07002254 unsigned int reg;
2255
Gary Hadee85abf82009-04-08 14:07:25 -07002256 apic = entry->apic;
2257 pin = entry->pin;
2258 /*
2259 * With interrupt-remapping, destination information comes
2260 * from interrupt-remapping table entry.
2261 */
2262 if (!irq_remapped(irq))
2263 io_apic_write(apic, 0x11 + pin*2, dest);
2264 reg = io_apic_read(apic, 0x10 + pin*2);
2265 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2266 reg |= vector;
2267 io_apic_modify(apic, 0x10 + pin*2, reg);
Gary Hadee85abf82009-04-08 14:07:25 -07002268 }
2269}
2270
2271/*
2272 * Either sets desc->affinity to a valid value, and returns
2273 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
2274 * leaves desc->affinity untouched.
2275 */
Dimitri Sivanich9338ad62009-10-13 15:32:36 -05002276unsigned int
Gary Hadee85abf82009-04-08 14:07:25 -07002277set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
2278{
2279 struct irq_cfg *cfg;
2280 unsigned int irq;
2281
2282 if (!cpumask_intersects(mask, cpu_online_mask))
2283 return BAD_APICID;
2284
2285 irq = desc->irq;
2286 cfg = desc->chip_data;
2287 if (assign_irq_vector(irq, cfg, mask))
2288 return BAD_APICID;
2289
Gary Hadee85abf82009-04-08 14:07:25 -07002290 cpumask_copy(desc->affinity, mask);
2291
2292 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2293}
2294
Ingo Molnar44204712009-05-01 19:02:50 +02002295static int
Gary Hadee85abf82009-04-08 14:07:25 -07002296set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2297{
2298 struct irq_cfg *cfg;
2299 unsigned long flags;
2300 unsigned int dest;
2301 unsigned int irq;
Ingo Molnar44204712009-05-01 19:02:50 +02002302 int ret = -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002303
2304 irq = desc->irq;
2305 cfg = desc->chip_data;
2306
2307 spin_lock_irqsave(&ioapic_lock, flags);
2308 dest = set_desc_affinity(desc, mask);
2309 if (dest != BAD_APICID) {
2310 /* Only the high 8 bits are valid. */
2311 dest = SET_APIC_LOGICAL_ID(dest);
2312 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar44204712009-05-01 19:02:50 +02002313 ret = 0;
Gary Hadee85abf82009-04-08 14:07:25 -07002314 }
2315 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnar44204712009-05-01 19:02:50 +02002316
2317 return ret;
Gary Hadee85abf82009-04-08 14:07:25 -07002318}
2319
Ingo Molnar44204712009-05-01 19:02:50 +02002320static int
Gary Hadee85abf82009-04-08 14:07:25 -07002321set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2322{
2323 struct irq_desc *desc;
2324
2325 desc = irq_to_desc(irq);
2326
Ingo Molnar44204712009-05-01 19:02:50 +02002327 return set_ioapic_affinity_irq_desc(desc, mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002328}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002329
2330#ifdef CONFIG_INTR_REMAP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002331
2332/*
2333 * Migrate the IO-APIC irq in the presence of intr-remapping.
2334 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002335 * For both level and edge triggered, irq migration is a simple atomic
2336 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002337 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002338 * For level triggered, we eliminate the io-apic RTE modification (with the
2339 * updated vector information), by using a virtual vector (io-apic pin number).
2340 * Real vector that is used for interrupting cpu will be coming from
2341 * the interrupt-remapping table entry.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002342 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002343static int
Mike Travise7986732008-12-16 17:33:52 -08002344migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002345{
2346 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002347 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002348 unsigned int dest;
Yinghai Lu3145e942008-12-05 18:58:34 -08002349 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -07002350 int ret = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002351
Mike Travis22f65d32008-12-16 17:33:56 -08002352 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002353 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002354
Yinghai Lu3145e942008-12-05 18:58:34 -08002355 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002356 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002357 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002358
Yinghai Lu3145e942008-12-05 18:58:34 -08002359 cfg = desc->chip_data;
2360 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002361 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002362
Ingo Molnardebccb32009-01-28 15:20:18 +01002363 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002364
Ingo Molnar54168ed2008-08-20 09:07:45 +02002365 irte.vector = cfg->vector;
2366 irte.dest_id = IRTE_DEST(dest);
2367
2368 /*
2369 * Modified the IRTE and flushes the Interrupt entry cache.
2370 */
2371 modify_irte(irq, &irte);
2372
Mike Travis22f65d32008-12-16 17:33:56 -08002373 if (cfg->move_in_progress)
2374 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002375
Mike Travis7f7ace02009-01-10 21:58:08 -08002376 cpumask_copy(desc->affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -07002377
2378 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002379}
2380
Ingo Molnar54168ed2008-08-20 09:07:45 +02002381/*
2382 * Migrates the IRQ destination in the process context.
2383 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002384static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Rusty Russell968ea6d2008-12-13 21:55:51 +10302385 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002386{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002387 return migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002388}
Yinghai Lud5dedd42009-04-27 17:59:21 -07002389static int set_ir_ioapic_affinity_irq(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +10302390 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002391{
2392 struct irq_desc *desc = irq_to_desc(irq);
2393
Yinghai Lud5dedd42009-04-27 17:59:21 -07002394 return set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002395}
Suresh Siddha29b61be2009-03-16 17:05:02 -07002396#else
Yinghai Lud5dedd42009-04-27 17:59:21 -07002397static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Suresh Siddha29b61be2009-03-16 17:05:02 -07002398 const struct cpumask *mask)
2399{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002400 return 0;
Suresh Siddha29b61be2009-03-16 17:05:02 -07002401}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002402#endif
2403
Yinghai Lu497c9a12008-08-19 20:50:28 -07002404asmlinkage void smp_irq_move_cleanup_interrupt(void)
2405{
2406 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002407
Yinghai Lu497c9a12008-08-19 20:50:28 -07002408 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002409 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002410 irq_enter();
2411
2412 me = smp_processor_id();
2413 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2414 unsigned int irq;
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002415 unsigned int irr;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002416 struct irq_desc *desc;
2417 struct irq_cfg *cfg;
2418 irq = __get_cpu_var(vector_irq)[vector];
2419
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002420 if (irq == -1)
2421 continue;
2422
Yinghai Lu497c9a12008-08-19 20:50:28 -07002423 desc = irq_to_desc(irq);
2424 if (!desc)
2425 continue;
2426
2427 cfg = irq_cfg(irq);
2428 spin_lock(&desc->lock);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002429
Mike Travis22f65d32008-12-16 17:33:56 -08002430 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002431 goto unlock;
2432
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002433 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2434 /*
2435 * Check if the vector that needs to be cleanedup is
2436 * registered at the cpu's IRR. If so, then this is not
2437 * the best time to clean it up. Lets clean it up in the
2438 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2439 * to myself.
2440 */
2441 if (irr & (1 << (vector % 32))) {
2442 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2443 goto unlock;
2444 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002445 __get_cpu_var(vector_irq)[vector] = -1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002446unlock:
2447 spin_unlock(&desc->lock);
2448 }
2449
2450 irq_exit();
2451}
2452
Suresh Siddhaa5e74b82009-10-26 14:24:34 -08002453static void __irq_complete_move(struct irq_desc **descp, unsigned vector)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002454{
Yinghai Lu3145e942008-12-05 18:58:34 -08002455 struct irq_desc *desc = *descp;
2456 struct irq_cfg *cfg = desc->chip_data;
Suresh Siddhaa5e74b82009-10-26 14:24:34 -08002457 unsigned me;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002458
Yinghai Lufcef5912009-04-27 17:58:23 -07002459 if (likely(!cfg->move_in_progress))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002460 return;
2461
Yinghai Lu497c9a12008-08-19 20:50:28 -07002462 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002463
Yinghai Lufcef5912009-04-27 17:58:23 -07002464 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Mike Travis22f65d32008-12-16 17:33:56 -08002465 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002466}
Suresh Siddhaa5e74b82009-10-26 14:24:34 -08002467
2468static void irq_complete_move(struct irq_desc **descp)
2469{
2470 __irq_complete_move(descp, ~get_irq_regs()->orig_ax);
2471}
2472
2473void irq_force_complete_move(int irq)
2474{
2475 struct irq_desc *desc = irq_to_desc(irq);
2476 struct irq_cfg *cfg = desc->chip_data;
2477
2478 __irq_complete_move(&desc, cfg->vector);
2479}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002480#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002481static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002482#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002483
Yinghai Lu1d025192008-08-19 20:50:34 -07002484static void ack_apic_edge(unsigned int irq)
2485{
Yinghai Lu3145e942008-12-05 18:58:34 -08002486 struct irq_desc *desc = irq_to_desc(irq);
2487
2488 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002489 move_native_irq(irq);
2490 ack_APIC_irq();
2491}
2492
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002493atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002494
Suresh Siddhab3ec0a32009-10-26 14:24:35 -08002495static int use_eoi_reg __read_mostly;
2496
2497static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2498{
2499 struct irq_pin_list *entry;
2500
2501 for_each_irq_pin(entry, cfg->irq_2_pin) {
2502 if (irq_remapped(irq))
2503 io_apic_eoi(entry->apic, entry->pin);
2504 else
2505 io_apic_eoi(entry->apic, cfg->vector);
2506 }
2507}
2508
2509static void eoi_ioapic_irq(struct irq_desc *desc)
2510{
2511 struct irq_cfg *cfg;
2512 unsigned long flags;
2513 unsigned int irq;
2514
2515 irq = desc->irq;
2516 cfg = desc->chip_data;
2517
2518 spin_lock_irqsave(&ioapic_lock, flags);
2519 __eoi_ioapic_irq(irq, cfg);
2520 spin_unlock_irqrestore(&ioapic_lock, flags);
2521}
2522
2523static int ioapic_supports_eoi(void)
2524{
2525 struct pci_dev *root;
2526
2527 root = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
2528 if (root && root->vendor == PCI_VENDOR_ID_INTEL &&
2529 mp_ioapics[0].apicver >= 0x2) {
2530 use_eoi_reg = 1;
2531 printk(KERN_INFO "IO-APIC supports EOI register\n");
2532 } else
2533 printk(KERN_INFO "IO-APIC doesn't support EOI\n");
2534
2535 return 0;
2536}
2537
2538fs_initcall(ioapic_supports_eoi);
2539
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002540static void ack_apic_level(unsigned int irq)
2541{
Yinghai Lu3145e942008-12-05 18:58:34 -08002542 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002543 unsigned long v;
2544 int i;
Yinghai Lu3145e942008-12-05 18:58:34 -08002545 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002546 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002547
Yinghai Lu3145e942008-12-05 18:58:34 -08002548 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002549#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002550 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002551 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002552 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002553 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002554 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002555#endif
2556
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002557 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002558 * It appears there is an erratum which affects at least version 0x11
2559 * of I/O APIC (that's the 82093AA and cores integrated into various
2560 * chipsets). Under certain conditions a level-triggered interrupt is
2561 * erroneously delivered as edge-triggered one but the respective IRR
2562 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2563 * message but it will never arrive and further interrupts are blocked
2564 * from the source. The exact reason is so far unknown, but the
2565 * phenomenon was observed when two consecutive interrupt requests
2566 * from a given source get delivered to the same CPU and the source is
2567 * temporarily disabled in between.
2568 *
2569 * A workaround is to simulate an EOI message manually. We achieve it
2570 * by setting the trigger mode to edge and then to level when the edge
2571 * trigger mode gets detected in the TMR of a local APIC for a
2572 * level-triggered interrupt. We mask the source for the time of the
2573 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2574 * The idea is from Manfred Spraul. --macro
2575 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002576 cfg = desc->chip_data;
2577 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002578 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002579
Ingo Molnar54168ed2008-08-20 09:07:45 +02002580 /*
2581 * We must acknowledge the irq before we move it or the acknowledge will
2582 * not propagate properly.
2583 */
2584 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002585
Ingo Molnar54168ed2008-08-20 09:07:45 +02002586 /* Now we can move and renable the irq */
2587 if (unlikely(do_unmask_irq)) {
2588 /* Only migrate the irq if the ack has been received.
2589 *
2590 * On rare occasions the broadcast level triggered ack gets
2591 * delayed going to ioapics, and if we reprogram the
2592 * vector while Remote IRR is still set the irq will never
2593 * fire again.
2594 *
2595 * To prevent this scenario we read the Remote IRR bit
2596 * of the ioapic. This has two effects.
2597 * - On any sane system the read of the ioapic will
2598 * flush writes (and acks) going to the ioapic from
2599 * this cpu.
2600 * - We get to see if the ACK has actually been delivered.
2601 *
2602 * Based on failed experiments of reprogramming the
2603 * ioapic entry from outside of irq context starting
2604 * with masking the ioapic entry and then polling until
2605 * Remote IRR was clear before reprogramming the
2606 * ioapic I don't trust the Remote IRR bit to be
2607 * completey accurate.
2608 *
2609 * However there appears to be no other way to plug
2610 * this race, so if the Remote IRR bit is not
2611 * accurate and is causing problems then it is a hardware bug
2612 * and you can go talk to the chipset vendor about it.
2613 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002614 cfg = desc->chip_data;
2615 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002616 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002617 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002618 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002619
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002620 /* Tail end of version 0x11 I/O APIC bug workaround */
Yinghai Lu1d025192008-08-19 20:50:34 -07002621 if (!(v & (1 << (i & 0x1f)))) {
2622 atomic_inc(&irq_mis_count);
Suresh Siddhab3ec0a32009-10-26 14:24:35 -08002623
2624 if (use_eoi_reg)
2625 eoi_ioapic_irq(desc);
2626 else {
2627 spin_lock(&ioapic_lock);
2628 __mask_and_edge_IO_APIC_irq(cfg);
2629 __unmask_and_level_IO_APIC_irq(cfg);
2630 spin_unlock(&ioapic_lock);
2631 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002632 }
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002633}
Yinghai Lu1d025192008-08-19 20:50:34 -07002634
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002635#ifdef CONFIG_INTR_REMAP
2636static void ir_ack_apic_edge(unsigned int irq)
2637{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002638 ack_APIC_irq();
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002639}
2640
2641static void ir_ack_apic_level(unsigned int irq)
2642{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002643 struct irq_desc *desc = irq_to_desc(irq);
2644
2645 ack_APIC_irq();
2646 eoi_ioapic_irq(desc);
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002647}
2648#endif /* CONFIG_INTR_REMAP */
2649
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002650static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002651 .name = "IO-APIC",
2652 .startup = startup_ioapic_irq,
2653 .mask = mask_IO_APIC_irq,
2654 .unmask = unmask_IO_APIC_irq,
2655 .ack = ack_apic_edge,
2656 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002657#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002658 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002659#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002660 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661};
2662
Ingo Molnar54168ed2008-08-20 09:07:45 +02002663static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002664 .name = "IR-IO-APIC",
2665 .startup = startup_ioapic_irq,
2666 .mask = mask_IO_APIC_irq,
2667 .unmask = unmask_IO_APIC_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302668#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002669 .ack = ir_ack_apic_edge,
2670 .eoi = ir_ack_apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002671#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002672 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002673#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302674#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002675 .retrigger = ioapic_retrigger_irq,
2676};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678static inline void init_IO_APIC_traps(void)
2679{
2680 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002681 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002682 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /*
2685 * NOTE! The local APIC isn't very good at handling
2686 * multiple interrupts at the same interrupt level.
2687 * As the interrupt level is determined by taking the
2688 * vector number and shifting that right by 4, we
2689 * want to spread these out a bit so that they don't
2690 * all fall in the same interrupt level.
2691 *
2692 * Also, we've got to be careful not to trash gate
2693 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2694 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002695 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002696 cfg = desc->chip_data;
2697 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /*
2699 * Hmm.. We don't have an entry for this,
2700 * so default to an old-fashioned 8259
2701 * interrupt if we can..
2702 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002703 if (irq < nr_legacy_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002705 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002707 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709 }
2710}
2711
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002712/*
2713 * The local APIC irq-chip implementation:
2714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002716static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 unsigned long v;
2719
2720 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002721 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002724static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002726 unsigned long v;
2727
2728 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002729 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Yinghai Lu3145e942008-12-05 18:58:34 -08002732static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002733{
2734 ack_APIC_irq();
2735}
2736
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002737static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002738 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002739 .mask = mask_lapic_irq,
2740 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002741 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742};
2743
Yinghai Lu3145e942008-12-05 18:58:34 -08002744static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002745{
Yinghai Lu08678b02008-08-19 20:50:05 -07002746 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002747 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2748 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002749}
2750
Jan Beuliche9427102008-01-30 13:31:24 +01002751static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002754 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 * We put the 8259A master into AEOI mode and
2756 * unmask on all local APICs LVT0 as NMI.
2757 *
2758 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2759 * is from Maciej W. Rozycki - so we do not have to EOI from
2760 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2763
Jan Beuliche9427102008-01-30 13:31:24 +01002764 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 apic_printk(APIC_VERBOSE, " done.\n");
2767}
2768
2769/*
2770 * This looks a bit hackish but it's about the only one way of sending
2771 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2772 * not support the ExtINT mode, unfortunately. We need to send these
2773 * cycles as some i82489DX-based boards have glue logic that keeps the
2774 * 8259A interrupt line asserted until INTA. --macro
2775 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002776static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002778 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct IO_APIC_route_entry entry0, entry1;
2780 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002782 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002783 if (pin == -1) {
2784 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002786 }
2787 apic = find_isa_irq_apic(8, mp_INT);
2788 if (apic == -1) {
2789 WARN_ON_ONCE(1);
2790 return;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Andi Kleencf4c6a22006-09-26 10:52:30 +02002793 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002794 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 memset(&entry1, 0, sizeof(entry1));
2797
2798 entry1.dest_mode = 0; /* physical delivery */
2799 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002800 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 entry1.delivery_mode = dest_ExtINT;
2802 entry1.polarity = entry0.polarity;
2803 entry1.trigger = 0;
2804 entry1.vector = 0;
2805
Andi Kleencf4c6a22006-09-26 10:52:30 +02002806 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 save_control = CMOS_READ(RTC_CONTROL);
2809 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2810 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2811 RTC_FREQ_SELECT);
2812 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2813
2814 i = 100;
2815 while (i-- > 0) {
2816 mdelay(10);
2817 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2818 i -= 10;
2819 }
2820
2821 CMOS_WRITE(save_control, RTC_CONTROL);
2822 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002823 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Andi Kleencf4c6a22006-09-26 10:52:30 +02002825 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
Yinghai Luefa25592008-08-19 20:50:36 -07002828static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002829/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002830static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002831{
2832 disable_timer_pin_1 = 1;
2833 return 0;
2834}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002835early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002836
2837int timer_through_8259 __initdata;
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839/*
2840 * This code may look a bit paranoid, but it's supposed to cooperate with
2841 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2842 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2843 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002844 *
2845 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002847static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Yinghai Lu3145e942008-12-05 18:58:34 -08002849 struct irq_desc *desc = irq_to_desc(0);
2850 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002851 int node = cpu_to_node(boot_cpu_id);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002852 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002853 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002854 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002855
2856 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 /*
2859 * get/set the timer IRQ vector:
2860 */
2861 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002862 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002865 * As IRQ0 is to be enabled in the 8259A, the virtual
2866 * wire has to be disabled in the local APIC. Also
2867 * timer interrupts need to be acknowledged manually in
2868 * the 8259A for the i82489DX when using the NMI
2869 * watchdog as that APIC treats NMIs as level-triggered.
2870 * The AEOI mode will finish them in the 8259A
2871 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002873 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002875#ifdef CONFIG_X86_32
Yinghai Luf72dcca2009-02-08 16:18:03 -08002876 {
2877 unsigned int ver;
2878
2879 ver = apic_read(APIC_LVR);
2880 ver = GET_APIC_VERSION(ver);
2881 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2882 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002885 pin1 = find_isa_irq_pin(0, mp_INT);
2886 apic1 = find_isa_irq_apic(0, mp_INT);
2887 pin2 = ioapic_i8259.pin;
2888 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002890 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2891 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002892 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002894 /*
2895 * Some BIOS writers are clueless and report the ExtINTA
2896 * I/O APIC input from the cascaded 8259A as the timer
2897 * interrupt input. So just in case, if only one pin
2898 * was found above, try it both directly and through the
2899 * 8259A.
2900 */
2901 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002902 if (intr_remapping_enabled)
2903 panic("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002904 pin1 = pin2;
2905 apic1 = apic2;
2906 no_pin1 = 1;
2907 } else if (pin2 == -1) {
2908 pin2 = pin1;
2909 apic2 = apic1;
2910 }
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (pin1 != -1) {
2913 /*
2914 * Ok, does IRQ0 through the IOAPIC work?
2915 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002916 if (no_pin1) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002917 add_pin_to_irq_node(cfg, node, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002918 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002919 } else {
2920 /* for edge trigger, setup_IO_APIC_irq already
2921 * leave it unmasked.
2922 * so only need to unmask if it is level-trigger
2923 * do we really have level trigger timer?
2924 */
2925 int idx;
2926 idx = find_irq_entry(apic1, pin1, mp_INT);
2927 if (idx != -1 && irq_trigger(idx))
2928 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (timer_irq_works()) {
2931 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 setup_nmi();
2933 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002935 if (disable_timer_pin_1 > 0)
2936 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002939 if (intr_remapping_enabled)
2940 panic("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002941 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002942 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002943 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002944 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2945 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002947 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2948 "(IRQ0) through the 8259A ...\n");
2949 apic_printk(APIC_QUIET, KERN_INFO
2950 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /*
2952 * legacy devices should be connected to IO APIC #0
2953 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002954 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002955 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002956 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002958 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002959 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002961 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002963 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967 /*
2968 * Cleanup, just in case ...
2969 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002970 local_irq_disable();
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002971 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002972 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002973 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002977 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2978 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002979 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002981#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002982 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002983#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002985 apic_printk(APIC_QUIET, KERN_INFO
2986 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Yinghai Lu3145e942008-12-05 18:58:34 -08002988 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002989 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 enable_8259A_irq(0);
2991
2992 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002993 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002996 local_irq_disable();
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002997 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002998 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002999 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003001 apic_printk(APIC_QUIET, KERN_INFO
3002 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 init_8259A(0);
3005 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01003006 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 unlock_ExtINT_logic();
3009
3010 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003011 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08003014 local_irq_disable();
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003015 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003017 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003018out:
3019 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
3022/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003023 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3024 * to devices. However there may be an I/O APIC pin available for
3025 * this interrupt regardless. The pin may be left unconnected, but
3026 * typically it will be reused as an ExtINT cascade interrupt for
3027 * the master 8259A. In the MPS case such a pin will normally be
3028 * reported as an ExtINT interrupt in the MP table. With ACPI
3029 * there is no provision for ExtINT interrupts, and in the absence
3030 * of an override it would be treated as an ordinary ISA I/O APIC
3031 * interrupt, that is edge-triggered and unmasked by default. We
3032 * used to do this, but it caused problems on some systems because
3033 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3034 * the same ExtINT cascade interrupt to drive the local APIC of the
3035 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3036 * the I/O APIC in all cases now. No actual device should request
3037 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003039#define PIC_IRQS (1UL << PIC_CASCADE_IR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041void __init setup_IO_APIC(void)
3042{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003043
Ingo Molnar54168ed2008-08-20 09:07:45 +02003044 /*
3045 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3046 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003047 io_apic_irqs = nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Ingo Molnar54168ed2008-08-20 09:07:45 +02003049 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003050 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003051 * Set up IO-APIC IRQ routing.
3052 */
Thomas Gleixnerde934102009-08-20 09:27:29 +02003053 x86_init.mpparse.setup_ioapic_ids();
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 sync_Arb_IDs();
3056 setup_IO_APIC_irqs();
3057 init_IO_APIC_traps();
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003058 if (nr_legacy_irqs)
3059 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
3062/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003063 * Called after all the initialization is done. If we didnt find any
3064 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003066
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067static int __init io_apic_bug_finalize(void)
3068{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003069 if (sis_apic_bug == -1)
3070 sis_apic_bug = 0;
3071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
3074late_initcall(io_apic_bug_finalize);
3075
3076struct sysfs_ioapic_data {
3077 struct sys_device dev;
3078 struct IO_APIC_route_entry entry[0];
3079};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003080static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Pavel Machek438510f2005-04-16 15:25:24 -07003082static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 struct IO_APIC_route_entry *entry;
3085 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 data = container_of(dev, struct sysfs_ioapic_data, dev);
3089 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003090 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3091 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 return 0;
3094}
3095
3096static int ioapic_resume(struct sys_device *dev)
3097{
3098 struct IO_APIC_route_entry *entry;
3099 struct sysfs_ioapic_data *data;
3100 unsigned long flags;
3101 union IO_APIC_reg_00 reg_00;
3102 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 data = container_of(dev, struct sysfs_ioapic_data, dev);
3105 entry = data->entry;
3106
3107 spin_lock_irqsave(&ioapic_lock, flags);
3108 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05303109 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3110 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 io_apic_write(dev->id, 0, reg_00.raw);
3112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003114 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003115 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 return 0;
3118}
3119
3120static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003121 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 .suspend = ioapic_suspend,
3123 .resume = ioapic_resume,
3124};
3125
3126static int __init ioapic_init_sysfs(void)
3127{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003128 struct sys_device * dev;
3129 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 error = sysdev_class_register(&ioapic_sysdev_class);
3132 if (error)
3133 return error;
3134
Ingo Molnar54168ed2008-08-20 09:07:45 +02003135 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003136 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003138 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (!mp_ioapic_data[i]) {
3140 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3141 continue;
3142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003144 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 dev->cls = &ioapic_sysdev_class;
3146 error = sysdev_register(dev);
3147 if (error) {
3148 kfree(mp_ioapic_data[i]);
3149 mp_ioapic_data[i] = NULL;
3150 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3151 continue;
3152 }
3153 }
3154
3155 return 0;
3156}
3157
3158device_initcall(ioapic_init_sysfs);
3159
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003160/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003161 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003162 */
Yinghai Lud047f53a2009-04-27 18:02:23 -07003163unsigned int create_irq_nr(unsigned int irq_want, int node)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003164{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003165 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003166 unsigned int irq;
3167 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003168 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003169 struct irq_cfg *cfg_new = NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003170 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003171
3172 irq = 0;
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003173 if (irq_want < nr_irqs_gsi)
3174 irq_want = nr_irqs_gsi;
3175
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003176 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003177 for (new = irq_want; new < nr_irqs; new++) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07003178 desc_new = irq_to_desc_alloc_node(new, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003179 if (!desc_new) {
3180 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003181 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003182 }
3183 cfg_new = desc_new->chip_data;
3184
3185 if (cfg_new->vector != 0)
3186 continue;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003187
Yinghai Lu15e957d2009-04-30 01:17:50 -07003188 desc_new = move_irq_desc(desc_new, node);
Yinghai Lud047f53a2009-04-27 18:02:23 -07003189
Ingo Molnarfe402e12009-01-28 04:32:51 +01003190 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003191 irq = new;
3192 break;
3193 }
3194 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003195
Yinghai Lu199751d2008-08-19 20:50:27 -07003196 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003197 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003198 /* restore it, in case dynamic_irq_init clear it */
3199 if (desc_new)
3200 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003201 }
3202 return irq;
3203}
3204
Yinghai Lu199751d2008-08-19 20:50:27 -07003205int create_irq(void)
3206{
Yinghai Lud047f53a2009-04-27 18:02:23 -07003207 int node = cpu_to_node(boot_cpu_id);
Yinghai Lube5d5352008-12-05 18:58:33 -08003208 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003209 int irq;
3210
Yinghai Lube5d5352008-12-05 18:58:33 -08003211 irq_want = nr_irqs_gsi;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003212 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003213
3214 if (irq == 0)
3215 irq = -1;
3216
3217 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003218}
3219
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003220void destroy_irq(unsigned int irq)
3221{
3222 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003223 struct irq_cfg *cfg;
3224 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003225
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003226 /* store it, in case dynamic_irq_cleanup clear it */
3227 desc = irq_to_desc(irq);
3228 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003229 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003230 /* connect back irq_cfg */
Bartlomiej Zolnierkiewicz25f6e892009-07-30 23:21:18 +02003231 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003232
Ingo Molnar54168ed2008-08-20 09:07:45 +02003233 free_irte(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003234 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003235 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003236 spin_unlock_irqrestore(&vector_lock, flags);
3237}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003238
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003239/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003240 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003241 */
3242#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003243static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003244{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003245 struct irq_cfg *cfg;
3246 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003247 unsigned dest;
3248
Jan Beulichf1182632009-01-14 12:27:35 +00003249 if (disable_apic)
3250 return -ENXIO;
3251
Yinghai Lu3145e942008-12-05 18:58:34 -08003252 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003253 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003254 if (err)
3255 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003256
Ingo Molnardebccb32009-01-28 15:20:18 +01003257 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003258
Ingo Molnar54168ed2008-08-20 09:07:45 +02003259 if (irq_remapped(irq)) {
3260 struct irte irte;
3261 int ir_index;
3262 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003263
Ingo Molnar54168ed2008-08-20 09:07:45 +02003264 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3265 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003266
Ingo Molnar54168ed2008-08-20 09:07:45 +02003267 memset (&irte, 0, sizeof(irte));
3268
3269 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003270 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003271 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003272 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003273 irte.vector = cfg->vector;
3274 irte.dest_id = IRTE_DEST(dest);
3275
Weidong Hanf007e992009-05-23 00:41:15 +08003276 /* Set source-id of interrupt request */
3277 set_msi_sid(&irte, pdev);
3278
Ingo Molnar54168ed2008-08-20 09:07:45 +02003279 modify_irte(irq, &irte);
3280
3281 msg->address_hi = MSI_ADDR_BASE_HI;
3282 msg->data = sub_handle;
3283 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3284 MSI_ADDR_IR_SHV |
3285 MSI_ADDR_IR_INDEX1(ir_index) |
3286 MSI_ADDR_IR_INDEX2(ir_index);
Suresh Siddha29b61be2009-03-16 17:05:02 -07003287 } else {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003288 if (x2apic_enabled())
3289 msg->address_hi = MSI_ADDR_BASE_HI |
3290 MSI_ADDR_EXT_DEST_ID(dest);
3291 else
3292 msg->address_hi = MSI_ADDR_BASE_HI;
3293
Ingo Molnar54168ed2008-08-20 09:07:45 +02003294 msg->address_lo =
3295 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003296 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003297 MSI_ADDR_DEST_MODE_PHYSICAL:
3298 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003299 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003300 MSI_ADDR_REDIRECTION_CPU:
3301 MSI_ADDR_REDIRECTION_LOWPRI) |
3302 MSI_ADDR_DEST_ID(dest);
3303
3304 msg->data =
3305 MSI_DATA_TRIGGER_EDGE |
3306 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003307 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003308 MSI_DATA_DELIVERY_FIXED:
3309 MSI_DATA_DELIVERY_LOWPRI) |
3310 MSI_DATA_VECTOR(cfg->vector);
3311 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003312 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003313}
3314
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003315#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003316static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003317{
Yinghai Lu3145e942008-12-05 18:58:34 -08003318 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003319 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003320 struct msi_msg msg;
3321 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003322
Mike Travis22f65d32008-12-16 17:33:56 -08003323 dest = set_desc_affinity(desc, mask);
3324 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003325 return -1;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003326
Yinghai Lu3145e942008-12-05 18:58:34 -08003327 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003328
Yinghai Lu3145e942008-12-05 18:58:34 -08003329 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003330
3331 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003332 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003333 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3334 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3335
Yinghai Lu3145e942008-12-05 18:58:34 -08003336 write_msi_msg_desc(desc, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003337
3338 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003339}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003340#ifdef CONFIG_INTR_REMAP
3341/*
3342 * Migrate the MSI irq to another cpumask. This migration is
3343 * done in the process context using interrupt-remapping hardware.
3344 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07003345static int
Mike Travise7986732008-12-16 17:33:52 -08003346ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003347{
Yinghai Lu3145e942008-12-05 18:58:34 -08003348 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003349 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003350 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003351 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003352
3353 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003354 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003355
Mike Travis22f65d32008-12-16 17:33:56 -08003356 dest = set_desc_affinity(desc, mask);
3357 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003358 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003359
Ingo Molnar54168ed2008-08-20 09:07:45 +02003360 irte.vector = cfg->vector;
3361 irte.dest_id = IRTE_DEST(dest);
3362
3363 /*
3364 * atomically update the IRTE with the new destination and vector.
3365 */
3366 modify_irte(irq, &irte);
3367
3368 /*
3369 * After this point, all the interrupts will start arriving
3370 * at the new destination. So, time to cleanup the previous
3371 * vector allocation.
3372 */
Mike Travis22f65d32008-12-16 17:33:56 -08003373 if (cfg->move_in_progress)
3374 send_cleanup_vector(cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003375
3376 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003377}
Yinghai Lu3145e942008-12-05 18:58:34 -08003378
Ingo Molnar54168ed2008-08-20 09:07:45 +02003379#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003380#endif /* CONFIG_SMP */
3381
3382/*
3383 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3384 * which implement the MSI or MSI-X Capability Structure.
3385 */
3386static struct irq_chip msi_chip = {
3387 .name = "PCI-MSI",
3388 .unmask = unmask_msi_irq,
3389 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003390 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003391#ifdef CONFIG_SMP
3392 .set_affinity = set_msi_irq_affinity,
3393#endif
3394 .retrigger = ioapic_retrigger_irq,
3395};
3396
Ingo Molnar54168ed2008-08-20 09:07:45 +02003397static struct irq_chip msi_ir_chip = {
3398 .name = "IR-PCI-MSI",
3399 .unmask = unmask_msi_irq,
3400 .mask = mask_msi_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303401#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08003402 .ack = ir_ack_apic_edge,
Ingo Molnar54168ed2008-08-20 09:07:45 +02003403#ifdef CONFIG_SMP
3404 .set_affinity = ir_set_msi_irq_affinity,
3405#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303406#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02003407 .retrigger = ioapic_retrigger_irq,
3408};
3409
3410/*
3411 * Map the PCI dev to the corresponding remapping hardware unit
3412 * and allocate 'nvec' consecutive interrupt-remapping table entries
3413 * in it.
3414 */
3415static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3416{
3417 struct intel_iommu *iommu;
3418 int index;
3419
3420 iommu = map_dev_to_ir(dev);
3421 if (!iommu) {
3422 printk(KERN_ERR
3423 "Unable to map PCI %s to iommu\n", pci_name(dev));
3424 return -ENOENT;
3425 }
3426
3427 index = alloc_irte(iommu, irq, nvec);
3428 if (index < 0) {
3429 printk(KERN_ERR
3430 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003431 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003432 return -ENOSPC;
3433 }
3434 return index;
3435}
Yinghai Lu1d025192008-08-19 20:50:34 -07003436
Yinghai Lu3145e942008-12-05 18:58:34 -08003437static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003438{
3439 int ret;
3440 struct msi_msg msg;
3441
3442 ret = msi_compose_msg(dev, irq, &msg);
3443 if (ret < 0)
3444 return ret;
3445
Yinghai Lu3145e942008-12-05 18:58:34 -08003446 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003447 write_msi_msg(irq, &msg);
3448
Ingo Molnar54168ed2008-08-20 09:07:45 +02003449 if (irq_remapped(irq)) {
3450 struct irq_desc *desc = irq_to_desc(irq);
3451 /*
3452 * irq migration in process context
3453 */
3454 desc->status |= IRQ_MOVE_PCNTXT;
3455 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3456 } else
Ingo Molnar54168ed2008-08-20 09:07:45 +02003457 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003458
Yinghai Luc81bba42008-09-25 11:53:11 -07003459 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3460
Yinghai Lu1d025192008-08-19 20:50:34 -07003461 return 0;
3462}
3463
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003464int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3465{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003466 unsigned int irq;
3467 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003468 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003469 unsigned int irq_want;
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003470 struct intel_iommu *iommu = NULL;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003471 int index = 0;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003472 int node;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003473
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -04003474 /* x86 doesn't support multiple MSI yet */
3475 if (type == PCI_CAP_ID_MSI && nvec > 1)
3476 return 1;
3477
Yinghai Lud047f53a2009-04-27 18:02:23 -07003478 node = dev_to_node(&dev->dev);
Yinghai Lube5d5352008-12-05 18:58:33 -08003479 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003480 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003481 list_for_each_entry(msidesc, &dev->msi_list, list) {
Yinghai Lud047f53a2009-04-27 18:02:23 -07003482 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003483 if (irq == 0)
3484 return -1;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003485 irq_want = irq + 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003486 if (!intr_remapping_enabled)
3487 goto no_ir;
3488
3489 if (!sub_handle) {
3490 /*
3491 * allocate the consecutive block of IRTE's
3492 * for 'nvec'
3493 */
3494 index = msi_alloc_irte(dev, irq, nvec);
3495 if (index < 0) {
3496 ret = index;
3497 goto error;
3498 }
3499 } else {
3500 iommu = map_dev_to_ir(dev);
3501 if (!iommu) {
3502 ret = -ENOENT;
3503 goto error;
3504 }
3505 /*
3506 * setup the mapping between the irq and the IRTE
3507 * base index, the sub_handle pointing to the
3508 * appropriate interrupt remap table entry.
3509 */
3510 set_irte_irq(irq, iommu, index, sub_handle);
3511 }
3512no_ir:
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003513 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003514 if (ret < 0)
3515 goto error;
3516 sub_handle++;
3517 }
3518 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003519
3520error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003521 destroy_irq(irq);
3522 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003523}
3524
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003525void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003526{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003527 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003528}
3529
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003530#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003531#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003532static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003533{
Yinghai Lu3145e942008-12-05 18:58:34 -08003534 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003535 struct irq_cfg *cfg;
3536 struct msi_msg msg;
3537 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003538
Mike Travis22f65d32008-12-16 17:33:56 -08003539 dest = set_desc_affinity(desc, mask);
3540 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003541 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003542
Yinghai Lu3145e942008-12-05 18:58:34 -08003543 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003544
3545 dmar_msi_read(irq, &msg);
3546
3547 msg.data &= ~MSI_DATA_VECTOR_MASK;
3548 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3549 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3550 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3551
3552 dmar_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003553
3554 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003555}
Yinghai Lu3145e942008-12-05 18:58:34 -08003556
Ingo Molnar54168ed2008-08-20 09:07:45 +02003557#endif /* CONFIG_SMP */
3558
Jaswinder Singh Rajput8f7007a2009-06-10 12:41:01 -07003559static struct irq_chip dmar_msi_type = {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003560 .name = "DMAR_MSI",
3561 .unmask = dmar_msi_unmask,
3562 .mask = dmar_msi_mask,
3563 .ack = ack_apic_edge,
3564#ifdef CONFIG_SMP
3565 .set_affinity = dmar_msi_set_affinity,
3566#endif
3567 .retrigger = ioapic_retrigger_irq,
3568};
3569
3570int arch_setup_dmar_msi(unsigned int irq)
3571{
3572 int ret;
3573 struct msi_msg msg;
3574
3575 ret = msi_compose_msg(NULL, irq, &msg);
3576 if (ret < 0)
3577 return ret;
3578 dmar_msi_write(irq, &msg);
3579 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3580 "edge");
3581 return 0;
3582}
3583#endif
3584
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003585#ifdef CONFIG_HPET_TIMER
3586
3587#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003588static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003589{
Yinghai Lu3145e942008-12-05 18:58:34 -08003590 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003591 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003592 struct msi_msg msg;
3593 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003594
Mike Travis22f65d32008-12-16 17:33:56 -08003595 dest = set_desc_affinity(desc, mask);
3596 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003597 return -1;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003598
Yinghai Lu3145e942008-12-05 18:58:34 -08003599 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003600
3601 hpet_msi_read(irq, &msg);
3602
3603 msg.data &= ~MSI_DATA_VECTOR_MASK;
3604 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3605 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3606 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3607
3608 hpet_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003609
3610 return 0;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003611}
Yinghai Lu3145e942008-12-05 18:58:34 -08003612
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003613#endif /* CONFIG_SMP */
3614
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003615static struct irq_chip hpet_msi_type = {
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003616 .name = "HPET_MSI",
3617 .unmask = hpet_msi_unmask,
3618 .mask = hpet_msi_mask,
3619 .ack = ack_apic_edge,
3620#ifdef CONFIG_SMP
3621 .set_affinity = hpet_msi_set_affinity,
3622#endif
3623 .retrigger = ioapic_retrigger_irq,
3624};
3625
3626int arch_setup_hpet_msi(unsigned int irq)
3627{
3628 int ret;
3629 struct msi_msg msg;
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003630 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003631
3632 ret = msi_compose_msg(NULL, irq, &msg);
3633 if (ret < 0)
3634 return ret;
3635
3636 hpet_msi_write(irq, &msg);
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003637 desc->status |= IRQ_MOVE_PCNTXT;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003638 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3639 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003640
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003641 return 0;
3642}
3643#endif
3644
Ingo Molnar54168ed2008-08-20 09:07:45 +02003645#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003646/*
3647 * Hypertransport interrupt support
3648 */
3649#ifdef CONFIG_HT_IRQ
3650
3651#ifdef CONFIG_SMP
3652
Yinghai Lu497c9a12008-08-19 20:50:28 -07003653static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003654{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003655 struct ht_irq_msg msg;
3656 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003657
Yinghai Lu497c9a12008-08-19 20:50:28 -07003658 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003659 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003660
Yinghai Lu497c9a12008-08-19 20:50:28 -07003661 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003662 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003663
Eric W. Biedermanec683072006-11-08 17:44:57 -08003664 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003665}
3666
Yinghai Lud5dedd42009-04-27 17:59:21 -07003667static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003668{
Yinghai Lu3145e942008-12-05 18:58:34 -08003669 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003670 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003671 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003672
Mike Travis22f65d32008-12-16 17:33:56 -08003673 dest = set_desc_affinity(desc, mask);
3674 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003675 return -1;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003676
Yinghai Lu3145e942008-12-05 18:58:34 -08003677 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003678
Yinghai Lu497c9a12008-08-19 20:50:28 -07003679 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003680
3681 return 0;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003682}
Yinghai Lu3145e942008-12-05 18:58:34 -08003683
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003684#endif
3685
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003686static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003687 .name = "PCI-HT",
3688 .mask = mask_ht_irq,
3689 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003690 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003691#ifdef CONFIG_SMP
3692 .set_affinity = set_ht_irq_affinity,
3693#endif
3694 .retrigger = ioapic_retrigger_irq,
3695};
3696
3697int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3698{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003699 struct irq_cfg *cfg;
3700 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003701
Jan Beulichf1182632009-01-14 12:27:35 +00003702 if (disable_apic)
3703 return -ENXIO;
3704
Yinghai Lu3145e942008-12-05 18:58:34 -08003705 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003706 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003707 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003708 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003709 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003710
Ingo Molnardebccb32009-01-28 15:20:18 +01003711 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3712 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003713
Eric W. Biedermanec683072006-11-08 17:44:57 -08003714 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003715
Eric W. Biedermanec683072006-11-08 17:44:57 -08003716 msg.address_lo =
3717 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003718 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003719 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003720 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003721 HT_IRQ_LOW_DM_PHYSICAL :
3722 HT_IRQ_LOW_DM_LOGICAL) |
3723 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003724 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003725 HT_IRQ_LOW_MT_FIXED :
3726 HT_IRQ_LOW_MT_ARBITRATED) |
3727 HT_IRQ_LOW_IRQ_MASKED;
3728
Eric W. Biedermanec683072006-11-08 17:44:57 -08003729 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003730
Ingo Molnara460e742006-10-17 00:10:03 -07003731 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3732 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003733
3734 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003735 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003736 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003737}
3738#endif /* CONFIG_HT_IRQ */
3739
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003740int __init io_apic_get_redir_entries (int ioapic)
3741{
3742 union IO_APIC_reg_01 reg_01;
3743 unsigned long flags;
3744
3745 spin_lock_irqsave(&ioapic_lock, flags);
3746 reg_01.raw = io_apic_read(ioapic, 1);
3747 spin_unlock_irqrestore(&ioapic_lock, flags);
3748
3749 return reg_01.bits.entries;
3750}
3751
Yinghai Lube5d5352008-12-05 18:58:33 -08003752void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003753{
Yinghai Lube5d5352008-12-05 18:58:33 -08003754 int nr = 0;
3755
Yinghai Lucc6c5002009-02-08 16:18:03 -08003756 nr = acpi_probe_gsi();
3757 if (nr > nr_irqs_gsi) {
Yinghai Lube5d5352008-12-05 18:58:33 -08003758 nr_irqs_gsi = nr;
Yinghai Lucc6c5002009-02-08 16:18:03 -08003759 } else {
3760 /* for acpi=off or acpi is not compiled in */
3761 int idx;
3762
3763 nr = 0;
3764 for (idx = 0; idx < nr_ioapics; idx++)
3765 nr += io_apic_get_redir_entries(idx) + 1;
3766
3767 if (nr > nr_irqs_gsi)
3768 nr_irqs_gsi = nr;
3769 }
3770
3771 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003772}
3773
Yinghai Lu4a046d12009-01-12 17:39:24 -08003774#ifdef CONFIG_SPARSE_IRQ
3775int __init arch_probe_nr_irqs(void)
3776{
3777 int nr;
3778
Yinghai Luf1ee5542009-02-08 16:18:03 -08003779 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3780 nr_irqs = NR_VECTORS * nr_cpu_ids;
Yinghai Lu4a046d12009-01-12 17:39:24 -08003781
Yinghai Luf1ee5542009-02-08 16:18:03 -08003782 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3783#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3784 /*
3785 * for MSI and HT dyn irq
3786 */
3787 nr += nr_irqs_gsi * 16;
3788#endif
3789 if (nr < nr_irqs)
Yinghai Lu4a046d12009-01-12 17:39:24 -08003790 nr_irqs = nr;
3791
3792 return 0;
3793}
3794#endif
3795
Yinghai Lue5198072009-05-15 13:05:16 -07003796static int __io_apic_set_pci_routing(struct device *dev, int irq,
3797 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003798{
3799 struct irq_desc *desc;
3800 struct irq_cfg *cfg;
3801 int node;
Yinghai Lue5198072009-05-15 13:05:16 -07003802 int ioapic, pin;
3803 int trigger, polarity;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003804
Yinghai Lue5198072009-05-15 13:05:16 -07003805 ioapic = irq_attr->ioapic;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003806 if (!IO_APIC_IRQ(irq)) {
3807 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3808 ioapic);
3809 return -EINVAL;
3810 }
3811
3812 if (dev)
3813 node = dev_to_node(dev);
3814 else
3815 node = cpu_to_node(boot_cpu_id);
3816
3817 desc = irq_to_desc_alloc_node(irq, node);
3818 if (!desc) {
3819 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3820 return 0;
3821 }
3822
Yinghai Lue5198072009-05-15 13:05:16 -07003823 pin = irq_attr->ioapic_pin;
3824 trigger = irq_attr->trigger;
3825 polarity = irq_attr->polarity;
3826
Yinghai Lu5ef21832009-05-06 10:08:50 -07003827 /*
3828 * IRQs < 16 are already in the irq_2_pin[] map
3829 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003830 if (irq >= nr_legacy_irqs) {
Yinghai Lu5ef21832009-05-06 10:08:50 -07003831 cfg = desc->chip_data;
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +04003832 if (add_pin_to_irq_node_nopanic(cfg, node, ioapic, pin)) {
3833 printk(KERN_INFO "can not add pin %d for irq %d\n",
3834 pin, irq);
3835 return 0;
3836 }
Yinghai Lu5ef21832009-05-06 10:08:50 -07003837 }
3838
Yinghai Lue5198072009-05-15 13:05:16 -07003839 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003840
3841 return 0;
3842}
3843
Yinghai Lue5198072009-05-15 13:05:16 -07003844int io_apic_set_pci_routing(struct device *dev, int irq,
3845 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003846{
Yinghai Lue5198072009-05-15 13:05:16 -07003847 int ioapic, pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003848 /*
3849 * Avoid pin reprogramming. PRTs typically include entries
3850 * with redundant pin->gsi mappings (but unique PCI devices);
3851 * we only program the IOAPIC on the first.
3852 */
Yinghai Lue5198072009-05-15 13:05:16 -07003853 ioapic = irq_attr->ioapic;
3854 pin = irq_attr->ioapic_pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003855 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3856 pr_debug("Pin %d-%d already programmed\n",
3857 mp_ioapics[ioapic].apicid, pin);
3858 return 0;
3859 }
3860 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3861
Yinghai Lue5198072009-05-15 13:05:16 -07003862 return __io_apic_set_pci_routing(dev, irq, irq_attr);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003863}
3864
Feng Tang2a4ab642009-07-07 23:01:15 -04003865u8 __init io_apic_unique_id(u8 id)
3866{
3867#ifdef CONFIG_X86_32
3868 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3869 !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3870 return io_apic_get_unique_id(nr_ioapics, id);
3871 else
3872 return id;
3873#else
3874 int i;
3875 DECLARE_BITMAP(used, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Feng Tang2a4ab642009-07-07 23:01:15 -04003877 bitmap_zero(used, 256);
3878 for (i = 0; i < nr_ioapics; i++) {
3879 struct mpc_ioapic *ia = &mp_ioapics[i];
3880 __set_bit(ia->apicid, used);
3881 }
3882 if (!test_bit(id, used))
3883 return id;
3884 return find_first_zero_bit(used, 256);
3885#endif
3886}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Ingo Molnar54168ed2008-08-20 09:07:45 +02003888#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003889int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 union IO_APIC_reg_00 reg_00;
3892 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3893 physid_mask_t tmp;
3894 unsigned long flags;
3895 int i = 0;
3896
3897 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003898 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3899 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003901 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3903 * advantage of new APIC bus architecture.
3904 */
3905
3906 if (physids_empty(apic_id_map))
Ingo Molnard190cb82009-01-28 06:50:47 +01003907 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 spin_lock_irqsave(&ioapic_lock, flags);
3910 reg_00.raw = io_apic_read(ioapic, 0);
3911 spin_unlock_irqrestore(&ioapic_lock, flags);
3912
3913 if (apic_id >= get_physical_broadcast()) {
3914 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3915 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3916 apic_id = reg_00.bits.ID;
3917 }
3918
3919 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003920 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 * 'stuck on smp_invalidate_needed IPI wait' messages.
3922 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003923 if (apic->check_apicid_used(apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
3925 for (i = 0; i < get_physical_broadcast(); i++) {
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003926 if (!apic->check_apicid_used(apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 break;
3928 }
3929
3930 if (i == get_physical_broadcast())
3931 panic("Max apic_id exceeded!\n");
3932
3933 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3934 "trying %d\n", ioapic, apic_id, i);
3935
3936 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Ingo Molnar80587142009-01-28 06:50:47 +01003939 tmp = apic->apicid_to_cpu_present(apic_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 physids_or(apic_id_map, apic_id_map, tmp);
3941
3942 if (reg_00.bits.ID != apic_id) {
3943 reg_00.bits.ID = apic_id;
3944
3945 spin_lock_irqsave(&ioapic_lock, flags);
3946 io_apic_write(ioapic, 0, reg_00.raw);
3947 reg_00.raw = io_apic_read(ioapic, 0);
3948 spin_unlock_irqrestore(&ioapic_lock, flags);
3949
3950 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003951 if (reg_00.bits.ID != apic_id) {
3952 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3953 return -1;
3954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956
3957 apic_printk(APIC_VERBOSE, KERN_INFO
3958 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3959
3960 return apic_id;
3961}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00003962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003964int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
3966 union IO_APIC_reg_01 reg_01;
3967 unsigned long flags;
3968
3969 spin_lock_irqsave(&ioapic_lock, flags);
3970 reg_01.raw = io_apic_read(ioapic, 1);
3971 spin_unlock_irqrestore(&ioapic_lock, flags);
3972
3973 return reg_01.bits.version;
3974}
3975
Shaohua Li61fd47e2007-11-17 01:05:28 -05003976int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3977{
3978 int i;
3979
3980 if (skip_ioapic_setup)
3981 return -1;
3982
3983 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05303984 if (mp_irqs[i].irqtype == mp_INT &&
3985 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003986 break;
3987 if (i >= mp_irq_entries)
3988 return -1;
3989
3990 *trigger = irq_trigger(i);
3991 *polarity = irq_polarity(i);
3992 return 0;
3993}
3994
Yinghai Lu497c9a12008-08-19 20:50:28 -07003995/*
3996 * This function currently is only a helper for the i386 smp boot process where
3997 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01003998 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07003999 */
4000#ifdef CONFIG_SMP
4001void __init setup_ioapic_dest(void)
4002{
Yinghai Lub9c61b702009-05-06 10:10:06 -07004003 int pin, ioapic = 0, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004004 struct irq_desc *desc;
Mike Travis22f65d32008-12-16 17:33:56 -08004005 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004006
4007 if (skip_ioapic_setup == 1)
4008 return;
4009
Yinghai Lub9c61b702009-05-06 10:10:06 -07004010#ifdef CONFIG_ACPI
4011 if (!acpi_disabled && acpi_ioapic) {
4012 ioapic = mp_find_ioapic(0);
4013 if (ioapic < 0)
4014 ioapic = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004015 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07004016#endif
4017
4018 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4019 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4020 if (irq_entry == -1)
4021 continue;
4022 irq = pin_2_irq(irq_entry, ioapic, pin);
4023
4024 desc = irq_to_desc(irq);
4025
4026 /*
4027 * Honour affinities which have been set in early boot
4028 */
4029 if (desc->status &
4030 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4031 mask = desc->affinity;
4032 else
4033 mask = apic->target_cpus();
4034
4035 if (intr_remapping_enabled)
4036 set_ir_ioapic_affinity_irq_desc(desc, mask);
4037 else
4038 set_ioapic_affinity_irq_desc(desc, mask);
4039 }
4040
Yinghai Lu497c9a12008-08-19 20:50:28 -07004041}
4042#endif
4043
Ingo Molnar54168ed2008-08-20 09:07:45 +02004044#define IOAPIC_RESOURCE_NAME_SIZE 11
4045
4046static struct resource *ioapic_resources;
4047
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004048static struct resource * __init ioapic_setup_resources(int nr_ioapics)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004049{
4050 unsigned long n;
4051 struct resource *res;
4052 char *mem;
4053 int i;
4054
4055 if (nr_ioapics <= 0)
4056 return NULL;
4057
4058 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4059 n *= nr_ioapics;
4060
4061 mem = alloc_bootmem(n);
4062 res = (void *)mem;
4063
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004064 mem += sizeof(struct resource) * nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004065
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004066 for (i = 0; i < nr_ioapics; i++) {
4067 res[i].name = mem;
4068 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Cyrill Gorcunov4343fe12009-11-08 18:54:31 +03004069 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004070 mem += IOAPIC_RESOURCE_NAME_SIZE;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004071 }
4072
4073 ioapic_resources = res;
4074
4075 return res;
4076}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004077
Yinghai Luf3294a32008-06-27 01:41:56 -07004078void __init ioapic_init_mappings(void)
4079{
4080 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004081 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004082 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004083
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004084 ioapic_res = ioapic_setup_resources(nr_ioapics);
Yinghai Luf3294a32008-06-27 01:41:56 -07004085 for (i = 0; i < nr_ioapics; i++) {
4086 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05304087 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004088#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004089 if (!ioapic_phys) {
4090 printk(KERN_ERR
4091 "WARNING: bogus zero IO-APIC "
4092 "address found in MPTABLE, "
4093 "disabling IO/APIC support!\n");
4094 smp_found_config = 0;
4095 skip_ioapic_setup = 1;
4096 goto fake_ioapic_page;
4097 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004098#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004099 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004100#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004101fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004102#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004103 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004104 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004105 ioapic_phys = __pa(ioapic_phys);
4106 }
4107 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004108 apic_printk(APIC_VERBOSE,
4109 "mapped IOAPIC to %08lx (%08lx)\n",
4110 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004111 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004112
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004113 ioapic_res->start = ioapic_phys;
Cyrill Gorcunov46dc2812009-11-08 18:53:56 +03004114 ioapic_res->end = ioapic_phys + PAGE_SIZE-1;
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004115 ioapic_res++;
Yinghai Luf3294a32008-06-27 01:41:56 -07004116 }
4117}
4118
Yinghai Lu857fdc52009-07-10 09:36:20 -07004119void __init ioapic_insert_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004120{
4121 int i;
4122 struct resource *r = ioapic_resources;
4123
4124 if (!r) {
Yinghai Lu857fdc52009-07-10 09:36:20 -07004125 if (nr_ioapics > 0)
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01004126 printk(KERN_ERR
4127 "IO APIC resources couldn't be allocated.\n");
Yinghai Lu857fdc52009-07-10 09:36:20 -07004128 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004129 }
4130
4131 for (i = 0; i < nr_ioapics; i++) {
4132 insert_resource(&iomem_resource, r);
4133 r++;
4134 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004135}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004136
Feng Tang2a4ab642009-07-07 23:01:15 -04004137int mp_find_ioapic(int gsi)
4138{
4139 int i = 0;
4140
4141 /* Find the IOAPIC that manages this GSI. */
4142 for (i = 0; i < nr_ioapics; i++) {
4143 if ((gsi >= mp_gsi_routing[i].gsi_base)
4144 && (gsi <= mp_gsi_routing[i].gsi_end))
4145 return i;
4146 }
4147
4148 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
4149 return -1;
4150}
4151
4152int mp_find_ioapic_pin(int ioapic, int gsi)
4153{
4154 if (WARN_ON(ioapic == -1))
4155 return -1;
4156 if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
4157 return -1;
4158
4159 return gsi - mp_gsi_routing[ioapic].gsi_base;
4160}
4161
4162static int bad_ioapic(unsigned long address)
4163{
4164 if (nr_ioapics >= MAX_IO_APICS) {
4165 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
4166 "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
4167 return 1;
4168 }
4169 if (!address) {
4170 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
4171 " found in table, skipping!\n");
4172 return 1;
4173 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004174 return 0;
4175}
4176
Feng Tang2a4ab642009-07-07 23:01:15 -04004177void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
4178{
4179 int idx = 0;
4180
4181 if (bad_ioapic(address))
4182 return;
4183
4184 idx = nr_ioapics;
4185
4186 mp_ioapics[idx].type = MP_IOAPIC;
4187 mp_ioapics[idx].flags = MPC_APIC_USABLE;
4188 mp_ioapics[idx].apicaddr = address;
4189
4190 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4191 mp_ioapics[idx].apicid = io_apic_unique_id(id);
4192 mp_ioapics[idx].apicver = io_apic_get_version(idx);
4193
4194 /*
4195 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4196 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4197 */
4198 mp_gsi_routing[idx].gsi_base = gsi_base;
4199 mp_gsi_routing[idx].gsi_end = gsi_base +
4200 io_apic_get_redir_entries(idx);
4201
4202 printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4203 "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4204 mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4205 mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);
4206
4207 nr_ioapics++;
4208}