blob: 409f4943dc1a3f818a2f35131bfff1b6e73c7e74 [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
Suresh Siddhac29d9db2009-12-01 15:31:16 -0800542static void __io_apic_modify_irq(struct irq_pin_list *entry,
543 int mask_and, int mask_or,
544 void (*final)(struct irq_pin_list *entry))
545{
546 unsigned int reg, pin;
547
548 pin = entry->pin;
549 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
550 reg &= mask_and;
551 reg |= mask_or;
552 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
553 if (final)
554 final(entry);
555}
556
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700557static void io_apic_modify_irq(struct irq_cfg *cfg,
558 int mask_and, int mask_or,
559 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400560{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400561 struct irq_pin_list *entry;
562
Suresh Siddhac29d9db2009-12-01 15:31:16 -0800563 for_each_irq_pin(entry, cfg->irq_2_pin)
564 __io_apic_modify_irq(entry, mask_and, mask_or, final);
565}
566
567static void __mask_and_edge_IO_APIC_irq(struct irq_pin_list *entry)
568{
569 __io_apic_modify_irq(entry, ~IO_APIC_REDIR_LEVEL_TRIGGER,
570 IO_APIC_REDIR_MASKED, NULL);
571}
572
573static void __unmask_and_level_IO_APIC_irq(struct irq_pin_list *entry)
574{
575 __io_apic_modify_irq(entry, ~IO_APIC_REDIR_MASKED,
576 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700577}
578
Yinghai Lu3145e942008-12-05 18:58:34 -0800579static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400580{
Yinghai Lu3145e942008-12-05 18:58:34 -0800581 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400582}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700583
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530584static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700585{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400586 /*
587 * Synchronize the IO-APIC and the CPU by doing
588 * a dummy read from the IO-APIC
589 */
590 struct io_apic __iomem *io_apic;
591 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700592 readl(&io_apic->data);
593}
594
Yinghai Lu3145e942008-12-05 18:58:34 -0800595static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400596{
Yinghai Lu3145e942008-12-05 18:58:34 -0800597 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400598}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700599
Yinghai Lu3145e942008-12-05 18:58:34 -0800600static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Yinghai Lu3145e942008-12-05 18:58:34 -0800602 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned long flags;
604
Yinghai Lu3145e942008-12-05 18:58:34 -0800605 BUG_ON(!cfg);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800608 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 spin_unlock_irqrestore(&ioapic_lock, flags);
610}
611
Yinghai Lu3145e942008-12-05 18:58:34 -0800612static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Yinghai Lu3145e942008-12-05 18:58:34 -0800614 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned long flags;
616
617 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800618 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 spin_unlock_irqrestore(&ioapic_lock, flags);
620}
621
Yinghai Lu3145e942008-12-05 18:58:34 -0800622static void mask_IO_APIC_irq(unsigned int irq)
623{
624 struct irq_desc *desc = irq_to_desc(irq);
625
626 mask_IO_APIC_irq_desc(desc);
627}
628static void unmask_IO_APIC_irq(unsigned int irq)
629{
630 struct irq_desc *desc = irq_to_desc(irq);
631
632 unmask_IO_APIC_irq_desc(desc);
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
636{
637 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200640 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (entry.delivery_mode == dest_SMI)
642 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * Disable it in the IO-APIC irq-routing table:
645 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800646 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Ingo Molnar54168ed2008-08-20 09:07:45 +0200649static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 int apic, pin;
652
653 for (apic = 0; apic < nr_ioapics; apic++)
654 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
655 clear_IO_APIC_pin(apic, pin);
656}
657
Ingo Molnar54168ed2008-08-20 09:07:45 +0200658#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
661 * specific CPU-side IRQs.
662 */
663
664#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800665static int pirq_entries[MAX_PIRQS] = {
666 [0 ... MAX_PIRQS - 1] = -1
667};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static int __init ioapic_pirq_setup(char *str)
670{
671 int i, max;
672 int ints[MAX_PIRQS+1];
673
674 get_options(str, ARRAY_SIZE(ints), ints);
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 apic_printk(APIC_VERBOSE, KERN_INFO
677 "PIRQ redirection, working around broken MP-BIOS.\n");
678 max = MAX_PIRQS;
679 if (ints[0] < MAX_PIRQS)
680 max = ints[0];
681
682 for (i = 0; i < max; i++) {
683 apic_printk(APIC_VERBOSE, KERN_DEBUG
684 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
685 /*
686 * PIRQs are mapped upside down, usually.
687 */
688 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
689 }
690 return 1;
691}
692
693__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200694#endif /* CONFIG_X86_32 */
695
Fenghua Yub24696b2009-03-27 14:22:44 -0700696struct IO_APIC_route_entry **alloc_ioapic_entries(void)
697{
698 int apic;
699 struct IO_APIC_route_entry **ioapic_entries;
700
701 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
702 GFP_ATOMIC);
703 if (!ioapic_entries)
704 return 0;
705
706 for (apic = 0; apic < nr_ioapics; apic++) {
707 ioapic_entries[apic] =
708 kzalloc(sizeof(struct IO_APIC_route_entry) *
709 nr_ioapic_registers[apic], GFP_ATOMIC);
710 if (!ioapic_entries[apic])
711 goto nomem;
712 }
713
714 return ioapic_entries;
715
716nomem:
717 while (--apic >= 0)
718 kfree(ioapic_entries[apic]);
719 kfree(ioapic_entries);
720
721 return 0;
722}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200723
724/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700725 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200726 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700727int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200728{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200729 int apic, pin;
730
Fenghua Yub24696b2009-03-27 14:22:44 -0700731 if (!ioapic_entries)
732 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200733
734 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700735 if (!ioapic_entries[apic])
736 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200737
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700738 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
Fenghua Yub24696b2009-03-27 14:22:44 -0700739 ioapic_entries[apic][pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200740 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700741 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400742
Ingo Molnar54168ed2008-08-20 09:07:45 +0200743 return 0;
744}
745
Fenghua Yub24696b2009-03-27 14:22:44 -0700746/*
747 * Mask all IO APIC entries.
748 */
749void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700750{
751 int apic, pin;
752
Fenghua Yub24696b2009-03-27 14:22:44 -0700753 if (!ioapic_entries)
754 return;
755
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700756 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700757 if (!ioapic_entries[apic])
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700758 break;
Fenghua Yub24696b2009-03-27 14:22:44 -0700759
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700760 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
761 struct IO_APIC_route_entry entry;
762
Fenghua Yub24696b2009-03-27 14:22:44 -0700763 entry = ioapic_entries[apic][pin];
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700764 if (!entry.mask) {
765 entry.mask = 1;
766 ioapic_write_entry(apic, pin, entry);
767 }
768 }
769 }
770}
771
Fenghua Yub24696b2009-03-27 14:22:44 -0700772/*
773 * Restore IO APIC entries which was saved in ioapic_entries.
774 */
775int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200776{
777 int apic, pin;
778
Fenghua Yub24696b2009-03-27 14:22:44 -0700779 if (!ioapic_entries)
780 return -ENOMEM;
781
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400782 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700783 if (!ioapic_entries[apic])
784 return -ENOMEM;
785
Ingo Molnar54168ed2008-08-20 09:07:45 +0200786 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
787 ioapic_write_entry(apic, pin,
Fenghua Yub24696b2009-03-27 14:22:44 -0700788 ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400789 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700790 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200791}
792
Fenghua Yub24696b2009-03-27 14:22:44 -0700793void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
794{
795 int apic;
796
797 for (apic = 0; apic < nr_ioapics; apic++)
798 kfree(ioapic_entries[apic]);
799
800 kfree(ioapic_entries);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200801}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803/*
804 * Find the IRQ entry number of a certain pin.
805 */
806static int find_irq_entry(int apic, int pin, int type)
807{
808 int i;
809
810 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530811 if (mp_irqs[i].irqtype == type &&
812 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
813 mp_irqs[i].dstapic == MP_APIC_ALL) &&
814 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return i;
816
817 return -1;
818}
819
820/*
821 * Find the pin to which IRQ[irq] (ISA) is connected
822 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800823static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 int i;
826
827 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530828 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300830 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530831 (mp_irqs[i].irqtype == type) &&
832 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530834 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 return -1;
837}
838
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800839static int __init find_isa_irq_apic(int irq, int type)
840{
841 int i;
842
843 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530844 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800845
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300846 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530847 (mp_irqs[i].irqtype == type) &&
848 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800849 break;
850 }
851 if (i < mp_irq_entries) {
852 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200853 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530854 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800855 return apic;
856 }
857 }
858
859 return -1;
860}
861
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300862#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * EISA Edge/Level control register, ELCR
865 */
866static int EISA_ELCR(unsigned int irq)
867{
Thomas Gleixnerbc078442009-08-29 18:09:57 +0200868 if (irq < nr_legacy_irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned int port = 0x4d0 + (irq >> 3);
870 return (inb(port) >> (irq & 7)) & 1;
871 }
872 apic_printk(APIC_VERBOSE, KERN_INFO
873 "Broken MPtable reports ISA irq %d\n", irq);
874 return 0;
875}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200876
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300879/* ISA interrupts are always polarity zero edge triggered,
880 * when listed as conforming in the MP table. */
881
882#define default_ISA_trigger(idx) (0)
883#define default_ISA_polarity(idx) (0)
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/* EISA interrupts are always polarity zero and can be edge or level
886 * trigger depending on the ELCR value. If an interrupt is listed as
887 * EISA conforming in the MP table, that means its trigger type must
888 * be read in from the ELCR */
889
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530890#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300891#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893/* PCI interrupts are always polarity one level triggered,
894 * when listed as conforming in the MP table. */
895
896#define default_PCI_trigger(idx) (1)
897#define default_PCI_polarity(idx) (1)
898
899/* MCA interrupts are always polarity zero level triggered,
900 * when listed as conforming in the MP table. */
901
902#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300903#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Shaohua Li61fd47e2007-11-17 01:05:28 -0500905static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530907 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 int polarity;
909
910 /*
911 * Determine IRQ line polarity (high active or low active):
912 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530913 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200915 case 0: /* conforms, ie. bus-type dependent polarity */
916 if (test_bit(bus, mp_bus_not_pci))
917 polarity = default_ISA_polarity(idx);
918 else
919 polarity = default_PCI_polarity(idx);
920 break;
921 case 1: /* high active */
922 {
923 polarity = 0;
924 break;
925 }
926 case 2: /* reserved */
927 {
928 printk(KERN_WARNING "broken BIOS!!\n");
929 polarity = 1;
930 break;
931 }
932 case 3: /* low active */
933 {
934 polarity = 1;
935 break;
936 }
937 default: /* invalid */
938 {
939 printk(KERN_WARNING "broken BIOS!!\n");
940 polarity = 1;
941 break;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 return polarity;
945}
946
947static int MPBIOS_trigger(int idx)
948{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530949 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int trigger;
951
952 /*
953 * Determine IRQ trigger mode (edge or level sensitive):
954 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530955 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200957 case 0: /* conforms, ie. bus-type dependent */
958 if (test_bit(bus, mp_bus_not_pci))
959 trigger = default_ISA_trigger(idx);
960 else
961 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300962#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200963 switch (mp_bus_id_to_type[bus]) {
964 case MP_BUS_ISA: /* ISA pin */
965 {
966 /* set before the switch */
967 break;
968 }
969 case MP_BUS_EISA: /* EISA pin */
970 {
971 trigger = default_EISA_trigger(idx);
972 break;
973 }
974 case MP_BUS_PCI: /* PCI pin */
975 {
976 /* set before the switch */
977 break;
978 }
979 case MP_BUS_MCA: /* MCA pin */
980 {
981 trigger = default_MCA_trigger(idx);
982 break;
983 }
984 default:
985 {
986 printk(KERN_WARNING "broken BIOS!!\n");
987 trigger = 1;
988 break;
989 }
990 }
991#endif
992 break;
993 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200994 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200995 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200996 break;
997 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200998 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200999 {
1000 printk(KERN_WARNING "broken BIOS!!\n");
1001 trigger = 1;
1002 break;
1003 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001004 case 3: /* level */
1005 {
1006 trigger = 1;
1007 break;
1008 }
1009 default: /* invalid */
1010 {
1011 printk(KERN_WARNING "broken BIOS!!\n");
1012 trigger = 0;
1013 break;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 return trigger;
1017}
1018
1019static inline int irq_polarity(int idx)
1020{
1021 return MPBIOS_polarity(idx);
1022}
1023
1024static inline int irq_trigger(int idx)
1025{
1026 return MPBIOS_trigger(idx);
1027}
1028
Yinghai Luefa25592008-08-19 20:50:36 -07001029int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030static int pin_2_irq(int idx, int apic, int pin)
1031{
1032 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301033 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /*
1036 * Debugging check, we are in big trouble if this message pops up!
1037 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301038 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1040
Ingo Molnar54168ed2008-08-20 09:07:45 +02001041 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301042 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001043 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001044 /*
1045 * PCI IRQs are mapped in order
1046 */
1047 i = irq = 0;
1048 while (i < apic)
1049 irq += nr_ioapic_registers[i++];
1050 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001051 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001052 * For MPS mode, so far only needed by ES7000 platform
1053 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001054 if (ioapic_renumber_irq)
1055 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Ingo Molnar54168ed2008-08-20 09:07:45 +02001058#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /*
1060 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1061 */
1062 if ((pin >= 16) && (pin <= 23)) {
1063 if (pirq_entries[pin-16] != -1) {
1064 if (!pirq_entries[pin-16]) {
1065 apic_printk(APIC_VERBOSE, KERN_DEBUG
1066 "disabling PIRQ%d\n", pin-16);
1067 } else {
1068 irq = pirq_entries[pin-16];
1069 apic_printk(APIC_VERBOSE, KERN_DEBUG
1070 "using PIRQ%d -> IRQ %d\n",
1071 pin-16, irq);
1072 }
1073 }
1074 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001075#endif
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return irq;
1078}
1079
Yinghai Lue20c06f2009-05-06 10:08:22 -07001080/*
1081 * Find a specific PCI IRQ entry.
1082 * Not an __init, possibly needed by modules
1083 */
1084int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
Yinghai Lue5198072009-05-15 13:05:16 -07001085 struct io_apic_irq_attr *irq_attr)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001086{
1087 int apic, i, best_guess = -1;
1088
1089 apic_printk(APIC_DEBUG,
1090 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1091 bus, slot, pin);
1092 if (test_bit(bus, mp_bus_not_pci)) {
1093 apic_printk(APIC_VERBOSE,
1094 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1095 return -1;
1096 }
1097 for (i = 0; i < mp_irq_entries; i++) {
1098 int lbus = mp_irqs[i].srcbus;
1099
1100 for (apic = 0; apic < nr_ioapics; apic++)
1101 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1102 mp_irqs[i].dstapic == MP_APIC_ALL)
1103 break;
1104
1105 if (!test_bit(lbus, mp_bus_not_pci) &&
1106 !mp_irqs[i].irqtype &&
1107 (bus == lbus) &&
1108 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1109 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1110
1111 if (!(apic || IO_APIC_IRQ(irq)))
1112 continue;
1113
1114 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Yinghai Lue5198072009-05-15 13:05:16 -07001115 set_io_apic_irq_attr(irq_attr, apic,
1116 mp_irqs[i].dstirq,
1117 irq_trigger(i),
1118 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001119 return irq;
1120 }
1121 /*
1122 * Use the first all-but-pin matching entry as a
1123 * best-guess fuzzy result for broken mptables.
1124 */
1125 if (best_guess < 0) {
Yinghai Lue5198072009-05-15 13:05:16 -07001126 set_io_apic_irq_attr(irq_attr, apic,
1127 mp_irqs[i].dstirq,
1128 irq_trigger(i),
1129 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001130 best_guess = irq;
1131 }
1132 }
1133 }
1134 return best_guess;
1135}
1136EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1137
Yinghai Lu497c9a12008-08-19 20:50:28 -07001138void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001140 /* Used to the online set of cpus does not change
1141 * during assign_irq_vector.
1142 */
1143 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Yinghai Lu497c9a12008-08-19 20:50:28 -07001146void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001147{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001148 spin_unlock(&vector_lock);
1149}
1150
Mike Travise7986732008-12-16 17:33:52 -08001151static int
1152__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001153{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001154 /*
1155 * NOTE! The local APIC isn't very good at handling
1156 * multiple interrupts at the same interrupt level.
1157 * As the interrupt level is determined by taking the
1158 * vector number and shifting that right by 4, we
1159 * want to spread these out a bit so that they don't
1160 * all fall in the same interrupt level.
1161 *
1162 * Also, we've got to be careful not to trash gate
1163 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1164 */
Suresh Siddha6579b472010-01-13 16:19:11 -08001165 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
H. Peter Anvinea943962010-01-04 21:14:41 -08001166 static int current_offset = VECTOR_OFFSET_START % 8;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001167 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001168 int cpu, err;
1169 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001170
Suresh Siddha23359a82009-10-26 14:24:33 -08001171 if (cfg->move_in_progress)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001172 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001173
Mike Travis22f65d32008-12-16 17:33:56 -08001174 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1175 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001176
Ingo Molnar54168ed2008-08-20 09:07:45 +02001177 old_vector = cfg->vector;
1178 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001179 cpumask_and(tmp_mask, mask, cpu_online_mask);
1180 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1181 if (!cpumask_empty(tmp_mask)) {
1182 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001183 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001184 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001185 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001186
Mike Travise7986732008-12-16 17:33:52 -08001187 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001188 err = -ENOSPC;
1189 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001190 int new_cpu;
1191 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001192
Ingo Molnare2d40b12009-01-28 06:50:47 +01001193 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001194
Ingo Molnar54168ed2008-08-20 09:07:45 +02001195 vector = current_vector;
1196 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001197next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001198 vector += 8;
1199 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001200 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001201 offset = (offset + 1) % 8;
Suresh Siddha6579b472010-01-13 16:19:11 -08001202 vector = FIRST_EXTERNAL_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001203 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001204 if (unlikely(current_vector == vector))
1205 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001206
1207 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001208 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001209
Mike Travis22f65d32008-12-16 17:33:56 -08001210 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001211 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1212 goto next;
1213 /* Found one! */
1214 current_vector = vector;
1215 current_offset = offset;
1216 if (old_vector) {
1217 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001218 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001219 }
Mike Travis22f65d32008-12-16 17:33:56 -08001220 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001221 per_cpu(vector_irq, new_cpu)[vector] = irq;
1222 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001223 cpumask_copy(cfg->domain, tmp_mask);
1224 err = 0;
1225 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001226 }
Mike Travis22f65d32008-12-16 17:33:56 -08001227 free_cpumask_var(tmp_mask);
1228 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001229}
1230
Dimitri Sivanich9338ad62009-10-13 15:32:36 -05001231int assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001232{
1233 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001234 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001235
1236 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001237 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001238 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001239 return err;
1240}
1241
Yinghai Lu3145e942008-12-05 18:58:34 -08001242static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001243{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001244 int cpu, vector;
1245
Yinghai Lu497c9a12008-08-19 20:50:28 -07001246 BUG_ON(!cfg->vector);
1247
1248 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001249 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001250 per_cpu(vector_irq, cpu)[vector] = -1;
1251
1252 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001253 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001254
1255 if (likely(!cfg->move_in_progress))
1256 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001257 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001258 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1259 vector++) {
1260 if (per_cpu(vector_irq, cpu)[vector] != irq)
1261 continue;
1262 per_cpu(vector_irq, cpu)[vector] = -1;
1263 break;
1264 }
1265 }
1266 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001267}
1268
1269void __setup_vector_irq(int cpu)
1270{
1271 /* Initialize vector_irq on a new cpu */
1272 /* This function must be called with vector_lock held */
1273 int irq, vector;
1274 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001275 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001276
1277 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001278 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001279 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001280 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001281 continue;
1282 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001283 per_cpu(vector_irq, cpu)[vector] = irq;
1284 }
1285 /* Mark the free vectors */
1286 for (vector = 0; vector < NR_VECTORS; ++vector) {
1287 irq = per_cpu(vector_irq, cpu)[vector];
1288 if (irq < 0)
1289 continue;
1290
1291 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001292 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001293 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001294 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001295}
Glauber Costa3fde6902008-05-28 20:34:19 -07001296
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001297static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001298static struct irq_chip ir_ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Ingo Molnar54168ed2008-08-20 09:07:45 +02001300#define IOAPIC_AUTO -1
1301#define IOAPIC_EDGE 0
1302#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001304#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001305static inline int IO_APIC_irq_trigger(int irq)
1306{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001307 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001308
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001309 for (apic = 0; apic < nr_ioapics; apic++) {
1310 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1311 idx = find_irq_entry(apic, pin, mp_INT);
1312 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1313 return irq_trigger(idx);
1314 }
1315 }
1316 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001317 * nonexistent IRQs are edge default
1318 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001319 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001320}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001321#else
1322static inline int IO_APIC_irq_trigger(int irq)
1323{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001324 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001325}
1326#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001327
Yinghai Lu3145e942008-12-05 18:58:34 -08001328static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Yinghai Lu199751d2008-08-19 20:50:27 -07001330
Jan Beulich6ebcc002006-06-26 13:56:46 +02001331 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001332 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001333 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001334 else
1335 desc->status &= ~IRQ_LEVEL;
1336
Ingo Molnar54168ed2008-08-20 09:07:45 +02001337 if (irq_remapped(irq)) {
1338 desc->status |= IRQ_MOVE_PCNTXT;
1339 if (trigger)
1340 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1341 handle_fasteoi_irq,
1342 "fasteoi");
1343 else
1344 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1345 handle_edge_irq, "edge");
1346 return;
1347 }
Suresh Siddha29b61be2009-03-16 17:05:02 -07001348
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001349 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1350 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001351 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001352 handle_fasteoi_irq,
1353 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001354 else
Ingo Molnara460e742006-10-17 00:10:03 -07001355 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001356 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001357}
1358
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -08001359int setup_ioapic_entry(int apic_id, int irq,
1360 struct IO_APIC_route_entry *entry,
1361 unsigned int destination, int trigger,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001362 int polarity, int vector, int pin)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001363{
1364 /*
1365 * add it to the IO-APIC irq-routing table:
1366 */
1367 memset(entry,0,sizeof(*entry));
1368
Ingo Molnar54168ed2008-08-20 09:07:45 +02001369 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001370 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001371 struct irte irte;
1372 struct IR_IO_APIC_route_entry *ir_entry =
1373 (struct IR_IO_APIC_route_entry *) entry;
1374 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001375
Ingo Molnar54168ed2008-08-20 09:07:45 +02001376 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001377 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001378
1379 index = alloc_irte(iommu, irq, 1);
1380 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001381 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001382
1383 memset(&irte, 0, sizeof(irte));
1384
1385 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001386 irte.dst_mode = apic->irq_dest_mode;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001387 /*
1388 * Trigger mode in the IRTE will always be edge, and the
1389 * actual level or edge trigger will be setup in the IO-APIC
1390 * RTE. This will help simplify level triggered irq migration.
1391 * For more details, see the comments above explainig IO-APIC
1392 * irq migration in the presence of interrupt-remapping.
1393 */
1394 irte.trigger_mode = 0;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001395 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001396 irte.vector = vector;
1397 irte.dest_id = IRTE_DEST(destination);
1398
Weidong Hanf007e992009-05-23 00:41:15 +08001399 /* Set source-id of interrupt request */
1400 set_ioapic_sid(&irte, apic_id);
1401
Ingo Molnar54168ed2008-08-20 09:07:45 +02001402 modify_irte(irq, &irte);
1403
1404 ir_entry->index2 = (index >> 15) & 0x1;
1405 ir_entry->zero = 0;
1406 ir_entry->format = 1;
1407 ir_entry->index = (index & 0x7fff);
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001408 /*
1409 * IO-APIC RTE will be configured with virtual vector.
1410 * irq handler will do the explicit EOI to the io-apic.
1411 */
1412 ir_entry->vector = pin;
Suresh Siddha29b61be2009-03-16 17:05:02 -07001413 } else {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001414 entry->delivery_mode = apic->irq_delivery_mode;
1415 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001416 entry->dest = destination;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001417 entry->vector = vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001418 }
1419
1420 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001421 entry->trigger = trigger;
1422 entry->polarity = polarity;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001423
1424 /* Mask level triggered irqs.
1425 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1426 */
1427 if (trigger)
1428 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001429 return 0;
1430}
1431
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001432static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001433 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001434{
1435 struct irq_cfg *cfg;
1436 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001437 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001438
1439 if (!IO_APIC_IRQ(irq))
1440 return;
1441
Yinghai Lu3145e942008-12-05 18:58:34 -08001442 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001443
Ingo Molnarfe402e12009-01-28 04:32:51 +01001444 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001445 return;
1446
Ingo Molnardebccb32009-01-28 15:20:18 +01001447 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001448
1449 apic_printk(APIC_VERBOSE,KERN_DEBUG
1450 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1451 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001452 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001453 irq, trigger, polarity);
1454
1455
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001456 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001457 dest, trigger, polarity, cfg->vector, pin)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001458 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001459 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001460 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001461 return;
1462 }
1463
Yinghai Lu3145e942008-12-05 18:58:34 -08001464 ioapic_register_intr(irq, desc, trigger);
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001465 if (irq < nr_legacy_irqs)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001466 disable_8259A_irq(irq);
1467
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001468 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Yinghai Lub9c61b702009-05-06 10:10:06 -07001471static struct {
1472 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1473} mp_ioapic_routing[MAX_IO_APICS];
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475static void __init setup_IO_APIC_irqs(void)
1476{
Yinghai Lub9c61b702009-05-06 10:10:06 -07001477 int apic_id = 0, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001478 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001479 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001480 struct irq_cfg *cfg;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07001481 int node = cpu_to_node(boot_cpu_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1484
Yinghai Lub9c61b702009-05-06 10:10:06 -07001485#ifdef CONFIG_ACPI
1486 if (!acpi_disabled && acpi_ioapic) {
1487 apic_id = mp_find_ioapic(0);
1488 if (apic_id < 0)
1489 apic_id = 0;
1490 }
1491#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Yinghai Lub9c61b702009-05-06 10:10:06 -07001493 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1494 idx = find_irq_entry(apic_id, pin, mp_INT);
1495 if (idx == -1) {
1496 if (!notcon) {
1497 notcon = 1;
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001498 apic_printk(APIC_VERBOSE,
Yinghai Lub9c61b702009-05-06 10:10:06 -07001499 KERN_DEBUG " %d-%d",
1500 mp_ioapics[apic_id].apicid, pin);
1501 } else
1502 apic_printk(APIC_VERBOSE, " %d-%d",
1503 mp_ioapics[apic_id].apicid, pin);
1504 continue;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001505 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07001506 if (notcon) {
1507 apic_printk(APIC_VERBOSE,
1508 " (apicid-pin) not connected\n");
1509 notcon = 0;
1510 }
1511
1512 irq = pin_2_irq(idx, apic_id, pin);
1513
1514 /*
1515 * Skip the timer IRQ if there's a quirk handler
1516 * installed and if it returns 1:
1517 */
1518 if (apic->multi_timer_check &&
1519 apic->multi_timer_check(apic_id, irq))
1520 continue;
1521
1522 desc = irq_to_desc_alloc_node(irq, node);
1523 if (!desc) {
1524 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1525 continue;
1526 }
1527 cfg = desc->chip_data;
1528 add_pin_to_irq_node(cfg, node, apic_id, pin);
Yinghai Lu4c6f18f2009-05-18 10:23:28 -07001529 /*
1530 * don't mark it in pin_programmed, so later acpi could
1531 * set it correctly when irq < 16
1532 */
Yinghai Lub9c61b702009-05-06 10:10:06 -07001533 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1534 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001537 if (notcon)
1538 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001539 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001543 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001545static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001546 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Ingo Molnar54168ed2008-08-20 09:07:45 +02001550 if (intr_remapping_enabled)
1551 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001552
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001553 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 /*
1556 * We use logical delivery to get the timer IRQ
1557 * to the first CPU.
1558 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001559 entry.dest_mode = apic->irq_dest_mode;
Yinghai Luf72dcca2009-02-08 16:18:03 -08001560 entry.mask = 0; /* don't mask IRQ for edge */
Ingo Molnardebccb32009-01-28 15:20:18 +01001561 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001562 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 entry.polarity = 0;
1564 entry.trigger = 0;
1565 entry.vector = vector;
1566
1567 /*
1568 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001569 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001571 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * Add it to the IO-APIC irq-routing table:
1575 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001576 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001579
1580__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 int apic, i;
1583 union IO_APIC_reg_00 reg_00;
1584 union IO_APIC_reg_01 reg_01;
1585 union IO_APIC_reg_02 reg_02;
1586 union IO_APIC_reg_03 reg_03;
1587 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001588 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001589 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001590 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001592 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 for (i = 0; i < nr_ioapics; i++)
1594 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301595 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
1598 * We are a bit conservative about what we expect. We have to
1599 * know about every hardware change ASAP.
1600 */
1601 printk(KERN_INFO "testing the IO APIC.......................\n");
1602
1603 for (apic = 0; apic < nr_ioapics; apic++) {
1604
1605 spin_lock_irqsave(&ioapic_lock, flags);
1606 reg_00.raw = io_apic_read(apic, 0);
1607 reg_01.raw = io_apic_read(apic, 1);
1608 if (reg_01.bits.version >= 0x10)
1609 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001610 if (reg_01.bits.version >= 0x20)
1611 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 spin_unlock_irqrestore(&ioapic_lock, flags);
1613
Ingo Molnar54168ed2008-08-20 09:07:45 +02001614 printk("\n");
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301615 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1617 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1618 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1619 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Ingo Molnar54168ed2008-08-20 09:07:45 +02001621 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1625 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 /*
1628 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1629 * but the value of reg_02 is read as the previous read register
1630 * value, so ignore it if reg_02 == reg_01.
1631 */
1632 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1633 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1634 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 /*
1638 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1639 * or reg_03, but the value of reg_0[23] is read as the previous read
1640 * register value, so ignore it if reg_03 == reg_0[12].
1641 */
1642 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1643 reg_03.raw != reg_01.raw) {
1644 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1645 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
1648 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1649
Yinghai Lud83e94a2008-08-19 20:50:33 -07001650 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1651 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 for (i = 0; i <= reg_01.bits.entries; i++) {
1654 struct IO_APIC_route_entry entry;
1655
Andi Kleencf4c6a22006-09-26 10:52:30 +02001656 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Ingo Molnar54168ed2008-08-20 09:07:45 +02001658 printk(KERN_DEBUG " %02x %03X ",
1659 i,
1660 entry.dest
1661 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1664 entry.mask,
1665 entry.trigger,
1666 entry.irr,
1667 entry.polarity,
1668 entry.delivery_status,
1669 entry.dest_mode,
1670 entry.delivery_mode,
1671 entry.vector
1672 );
1673 }
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001676 for_each_irq_desc(irq, desc) {
1677 struct irq_pin_list *entry;
1678
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001679 cfg = desc->chip_data;
1680 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001681 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001683 printk(KERN_DEBUG "IRQ%d ", irq);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04001684 for_each_irq_pin(entry, cfg->irq_2_pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 printk("-> %d:%d", entry->apic, entry->pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 printk("\n");
1687 }
1688
1689 printk(KERN_INFO ".................................... done.\n");
1690
1691 return;
1692}
1693
Ingo Molnar251e1e42009-07-02 08:54:01 +02001694__apicdebuginit(void) print_APIC_field(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Ingo Molnar251e1e42009-07-02 08:54:01 +02001696 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Ingo Molnar251e1e42009-07-02 08:54:01 +02001698 printk(KERN_DEBUG);
1699
1700 for (i = 0; i < 8; i++)
1701 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1702
1703 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001706__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Andreas Herrmann97a52712009-05-08 18:23:50 +02001708 unsigned int i, v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001709 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Ingo Molnar251e1e42009-07-02 08:54:01 +02001711 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001713 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001714 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 v = apic_read(APIC_LVR);
1716 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1717 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001718 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 v = apic_read(APIC_TASKPRI);
1721 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1722
Ingo Molnar54168ed2008-08-20 09:07:45 +02001723 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001724 if (!APIC_XAPIC(ver)) {
1725 v = apic_read(APIC_ARBPRI);
1726 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1727 v & APIC_ARBPRI_MASK);
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 v = apic_read(APIC_PROCPRI);
1730 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1731 }
1732
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001733 /*
1734 * Remote read supported only in the 82489DX and local APIC for
1735 * Pentium processors.
1736 */
1737 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1738 v = apic_read(APIC_RRR);
1739 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1740 }
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 v = apic_read(APIC_LDR);
1743 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001744 if (!x2apic_enabled()) {
1745 v = apic_read(APIC_DFR);
1746 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 v = apic_read(APIC_SPIV);
1749 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1750
1751 printk(KERN_DEBUG "... APIC ISR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001752 print_APIC_field(APIC_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 printk(KERN_DEBUG "... APIC TMR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001754 print_APIC_field(APIC_TMR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 printk(KERN_DEBUG "... APIC IRR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001756 print_APIC_field(APIC_IRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Ingo Molnar54168ed2008-08-20 09:07:45 +02001758 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1759 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 v = apic_read(APIC_ESR);
1763 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1764 }
1765
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001766 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001767 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1768 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 v = apic_read(APIC_LVTT);
1771 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1772
1773 if (maxlvt > 3) { /* PC is LVT#4. */
1774 v = apic_read(APIC_LVTPC);
1775 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1776 }
1777 v = apic_read(APIC_LVT0);
1778 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1779 v = apic_read(APIC_LVT1);
1780 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1781
1782 if (maxlvt > 2) { /* ERR is LVT#3. */
1783 v = apic_read(APIC_LVTERR);
1784 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1785 }
1786
1787 v = apic_read(APIC_TMICT);
1788 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1789 v = apic_read(APIC_TMCCT);
1790 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1791 v = apic_read(APIC_TDCR);
1792 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
Andreas Herrmann97a52712009-05-08 18:23:50 +02001793
1794 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1795 v = apic_read(APIC_EFEAT);
1796 maxlvt = (v >> 16) & 0xff;
1797 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1798 v = apic_read(APIC_ECTRL);
1799 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1800 for (i = 0; i < maxlvt; i++) {
1801 v = apic_read(APIC_EILVTn(i));
1802 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1803 }
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 printk("\n");
1806}
1807
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001808__apicdebuginit(void) print_local_APICs(int maxcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001810 int cpu;
1811
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001812 if (!maxcpu)
1813 return;
1814
Yinghai Luffd5aae2008-08-19 20:50:50 -07001815 preempt_disable();
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001816 for_each_online_cpu(cpu) {
1817 if (cpu >= maxcpu)
1818 break;
Yinghai Luffd5aae2008-08-19 20:50:50 -07001819 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001820 }
Yinghai Luffd5aae2008-08-19 20:50:50 -07001821 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001824__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 unsigned int v;
1827 unsigned long flags;
1828
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001829 if (!nr_legacy_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return;
1831
1832 printk(KERN_DEBUG "\nprinting PIC contents\n");
1833
1834 spin_lock_irqsave(&i8259A_lock, flags);
1835
1836 v = inb(0xa1) << 8 | inb(0x21);
1837 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1838
1839 v = inb(0xa0) << 8 | inb(0x20);
1840 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1841
Ingo Molnar54168ed2008-08-20 09:07:45 +02001842 outb(0x0b,0xa0);
1843 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001845 outb(0x0a,0xa0);
1846 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 spin_unlock_irqrestore(&i8259A_lock, flags);
1849
1850 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1851
1852 v = inb(0x4d1) << 8 | inb(0x4d0);
1853 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1854}
1855
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001856static int __initdata show_lapic = 1;
1857static __init int setup_show_lapic(char *arg)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001858{
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001859 int num = -1;
1860
1861 if (strcmp(arg, "all") == 0) {
1862 show_lapic = CONFIG_NR_CPUS;
1863 } else {
1864 get_option(&arg, &num);
1865 if (num >= 0)
1866 show_lapic = num;
1867 }
1868
1869 return 1;
1870}
1871__setup("show_lapic=", setup_show_lapic);
1872
1873__apicdebuginit(int) print_ICs(void)
1874{
1875 if (apic_verbosity == APIC_QUIET)
1876 return 0;
1877
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001878 print_PIC();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001879
1880 /* don't print out if apic is not there */
Cyrill Gorcunov83121362009-09-15 11:12:30 +04001881 if (!cpu_has_apic && !apic_from_smp_config())
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001882 return 0;
1883
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001884 print_local_APICs(show_lapic);
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001885 print_IO_APIC();
1886
1887 return 0;
1888}
1889
Cyrill Gorcunov2626eb22009-10-14 00:07:05 +04001890fs_initcall(print_ICs);
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Yinghai Luefa25592008-08-19 20:50:36 -07001893/* Where if anywhere is the i8259 connect in external int mode */
1894static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1895
Ingo Molnar54168ed2008-08-20 09:07:45 +02001896void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001899 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001900 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 unsigned long flags;
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /*
1904 * The number of IO-APIC IRQ registers (== #pins):
1905 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001906 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001908 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001910 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1911 }
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001912
1913 if (!nr_legacy_irqs)
1914 return;
1915
Ingo Molnar54168ed2008-08-20 09:07:45 +02001916 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001917 int pin;
1918 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001919 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001920 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001921 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001922
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001923 /* If the interrupt line is enabled and in ExtInt mode
1924 * I have found the pin where the i8259 is connected.
1925 */
1926 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1927 ioapic_i8259.apic = apic;
1928 ioapic_i8259.pin = pin;
1929 goto found_i8259;
1930 }
1931 }
1932 }
1933 found_i8259:
1934 /* Look to see what if the MP table has reported the ExtINT */
1935 /* If we could not find the appropriate pin by looking at the ioapic
1936 * the i8259 probably is not connected the ioapic but give the
1937 * mptable a chance anyway.
1938 */
1939 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1940 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1941 /* Trust the MP table if nothing is setup in the hardware */
1942 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1943 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1944 ioapic_i8259.pin = i8259_pin;
1945 ioapic_i8259.apic = i8259_apic;
1946 }
1947 /* Complain if the MP table and the hardware disagree */
1948 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1949 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1950 {
1951 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
1954 /*
1955 * Do not trust the IO-APIC being empty at bootup
1956 */
1957 clear_IO_APIC();
1958}
1959
1960/*
1961 * Not an __init, needed by the reboot code
1962 */
1963void disable_IO_APIC(void)
1964{
1965 /*
1966 * Clear the IO-APIC before rebooting:
1967 */
1968 clear_IO_APIC();
1969
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001970 if (!nr_legacy_irqs)
1971 return;
1972
Eric W. Biederman650927e2005-06-25 14:57:44 -07001973 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001974 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001975 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001976 * so legacy interrupts can be delivered.
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001977 *
1978 * With interrupt-remapping, for now we will use virtual wire A mode,
1979 * as virtual wire B is little complex (need to configure both
1980 * IOAPIC RTE aswell as interrupt-remapping table entry).
1981 * As this gets called during crash dump, keep this simple for now.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001982 */
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001983 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001984 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001985
1986 memset(&entry, 0, sizeof(entry));
1987 entry.mask = 0; /* Enabled */
1988 entry.trigger = 0; /* Edge */
1989 entry.irr = 0;
1990 entry.polarity = 0; /* High */
1991 entry.delivery_status = 0;
1992 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001993 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001994 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001995 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001996
1997 /*
1998 * Add it to the IO-APIC irq-routing table:
1999 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02002000 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07002001 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002002
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07002003 /*
2004 * Use virtual wire A mode when interrupt remapping is enabled.
2005 */
Cyrill Gorcunov83121362009-09-15 11:12:30 +04002006 if (cpu_has_apic || apic_from_smp_config())
Cyrill Gorcunov3f4c3952009-06-17 22:13:22 +04002007 disconnect_bsp_APIC(!intr_remapping_enabled &&
2008 ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Ingo Molnar54168ed2008-08-20 09:07:45 +02002011#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/*
2013 * function to set the IO-APIC physical IDs based on the
2014 * values stored in the MPC table.
2015 *
2016 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2017 */
2018
Thomas Gleixnerde934102009-08-20 09:27:29 +02002019void __init setup_ioapic_ids_from_mpc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 union IO_APIC_reg_00 reg_00;
2022 physid_mask_t phys_id_present_map;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002023 int apic_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int i;
2025 unsigned char old_id;
2026 unsigned long flags;
2027
Thomas Gleixnerde934102009-08-20 09:27:29 +02002028 if (acpi_ioapic)
Yinghai Lud49c4282008-06-08 18:31:54 -07002029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002031 * Don't check I/O APIC IDs for xAPIC systems. They have
2032 * no meaning without the serial APIC bus.
2033 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002034 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2035 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002036 return;
2037 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 * This is broken; anything with a real cpu count has to
2039 * circumvent this idiocy regardless.
2040 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002041 apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /*
2044 * Set the IOAPIC ID to the value stored in the MPC table.
2045 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002046 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* Read the register 0 value */
2049 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002050 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002052
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002053 old_id = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002055 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002057 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2059 reg_00.bits.ID);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002060 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /*
2064 * Sanity check, is the ID really free? Every APIC in a
2065 * system must have a unique ID or we get lots of nice
2066 * 'stuck on smp_invalidate_needed IPI wait' messages.
2067 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002068 if (apic->check_apicid_used(&phys_id_present_map,
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002069 mp_ioapics[apic_id].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002071 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 for (i = 0; i < get_physical_broadcast(); i++)
2073 if (!physid_isset(i, phys_id_present_map))
2074 break;
2075 if (i >= get_physical_broadcast())
2076 panic("Max APIC ID exceeded!\n");
2077 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2078 i);
2079 physid_set(i, phys_id_present_map);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002080 mp_ioapics[apic_id].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 } else {
2082 physid_mask_t tmp;
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002083 apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 apic_printk(APIC_VERBOSE, "Setting %d in the "
2085 "phys_id_present_map\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002086 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2088 }
2089
2090
2091 /*
2092 * We need to adjust the IRQ routing table
2093 * if the ID changed.
2094 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002095 if (old_id != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302097 if (mp_irqs[i].dstapic == old_id)
2098 mp_irqs[i].dstapic
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002099 = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /*
2102 * Read the right value from the MPC table and
2103 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 apic_printk(APIC_VERBOSE, KERN_INFO
2106 "...changing IO-APIC physical APIC ID to %d ...",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002107 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002109 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002111 io_apic_write(apic_id, 0, reg_00.raw);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002112 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /*
2115 * Sanity check
2116 */
2117 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002118 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002120 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 printk("could not set ID!\n");
2122 else
2123 apic_printk(APIC_VERBOSE, " ok.\n");
2124 }
2125}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002128int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002129
2130static int __init notimercheck(char *s)
2131{
2132 no_timer_check = 1;
2133 return 1;
2134}
2135__setup("no_timer_check", notimercheck);
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137/*
2138 * There is a nasty bug in some older SMP boards, their mptable lies
2139 * about the timer IRQ. We do the following to work around the situation:
2140 *
2141 * - timer IRQ defaults to IO-APIC IRQ
2142 * - if this function detects that timer IRQs are defunct, then we fall
2143 * back to ISA timer IRQs
2144 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002145static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002148 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Zachary Amsden8542b202006-12-07 02:14:09 +01002150 if (no_timer_check)
2151 return 1;
2152
Ingo Molnar4aae0702007-12-18 18:05:58 +01002153 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 local_irq_enable();
2155 /* Let ten ticks pass... */
2156 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002157 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 /*
2160 * Expect a few ticks at least, to be sure some possible
2161 * glue logic does not lock up after one or two first
2162 * ticks in a non-ExtINT mode. Also the local APIC
2163 * might have cached one ExtINT interrupt. Finally, at
2164 * least one tick may be lost due to delays.
2165 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002166
2167 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002168 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171}
2172
2173/*
2174 * In the SMP+IOAPIC case it might happen that there are an unspecified
2175 * number of pending IRQ events unhandled. These cases are very rare,
2176 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2177 * better to do it this way as thus we do not have to be aware of
2178 * 'pending' interrupts in the IRQ path, except at this point.
2179 */
2180/*
2181 * Edge triggered needs to resend any interrupt
2182 * that was delayed but this is now handled in the device
2183 * independent code.
2184 */
2185
2186/*
2187 * Starting up a edge-triggered IO-APIC interrupt is
2188 * nasty - we need to make sure that we get the edge.
2189 * If it is already asserted for some reason, we need
2190 * return 1 to indicate that is was pending.
2191 *
2192 * This is not complete - we should be able to fake
2193 * an edge even if it isn't on the 8259A...
2194 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002195
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002196static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 int was_pending = 0;
2199 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002200 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 spin_lock_irqsave(&ioapic_lock, flags);
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002203 if (irq < nr_legacy_irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 disable_8259A_irq(irq);
2205 if (i8259A_irq_pending(irq))
2206 was_pending = 1;
2207 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002208 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002209 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 spin_unlock_irqrestore(&ioapic_lock, flags);
2211
2212 return was_pending;
2213}
2214
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002215static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002217
2218 struct irq_cfg *cfg = irq_cfg(irq);
2219 unsigned long flags;
2220
2221 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnardac5f412009-01-28 15:42:24 +01002222 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002223 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002224
2225 return 1;
2226}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002227
2228/*
2229 * Level and edge triggered IO-APIC interrupts need different handling,
2230 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2231 * handled with the level-triggered descriptor, but that one has slightly
2232 * more overhead. Level-triggered interrupts cannot be handled with the
2233 * edge-triggered handler, without risking IRQ storms and other ugly
2234 * races.
2235 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002236
Yinghai Lu497c9a12008-08-19 20:50:28 -07002237#ifdef CONFIG_SMP
Dimitri Sivanich9338ad62009-10-13 15:32:36 -05002238void send_cleanup_vector(struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002239{
2240 cpumask_var_t cleanup_mask;
2241
2242 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2243 unsigned int i;
Gary Hadee85abf82009-04-08 14:07:25 -07002244 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2245 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2246 } else {
2247 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002248 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2249 free_cpumask_var(cleanup_mask);
2250 }
2251 cfg->move_in_progress = 0;
2252}
2253
Ingo Molnar44204712009-05-01 19:02:50 +02002254static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002255{
2256 int apic, pin;
2257 struct irq_pin_list *entry;
2258 u8 vector = cfg->vector;
2259
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04002260 for_each_irq_pin(entry, cfg->irq_2_pin) {
Gary Hadee85abf82009-04-08 14:07:25 -07002261 unsigned int reg;
2262
Gary Hadee85abf82009-04-08 14:07:25 -07002263 apic = entry->apic;
2264 pin = entry->pin;
2265 /*
2266 * With interrupt-remapping, destination information comes
2267 * from interrupt-remapping table entry.
2268 */
2269 if (!irq_remapped(irq))
2270 io_apic_write(apic, 0x11 + pin*2, dest);
2271 reg = io_apic_read(apic, 0x10 + pin*2);
2272 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2273 reg |= vector;
2274 io_apic_modify(apic, 0x10 + pin*2, reg);
Gary Hadee85abf82009-04-08 14:07:25 -07002275 }
2276}
2277
2278/*
2279 * Either sets desc->affinity to a valid value, and returns
Suresh Siddha18374d82009-12-17 18:29:46 -08002280 * ->cpu_mask_to_apicid of that in dest_id, or returns -1 and
Gary Hadee85abf82009-04-08 14:07:25 -07002281 * leaves desc->affinity untouched.
2282 */
Dimitri Sivanich9338ad62009-10-13 15:32:36 -05002283unsigned int
Suresh Siddha18374d82009-12-17 18:29:46 -08002284set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask,
2285 unsigned int *dest_id)
Gary Hadee85abf82009-04-08 14:07:25 -07002286{
2287 struct irq_cfg *cfg;
2288 unsigned int irq;
2289
2290 if (!cpumask_intersects(mask, cpu_online_mask))
Suresh Siddha18374d82009-12-17 18:29:46 -08002291 return -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002292
2293 irq = desc->irq;
2294 cfg = desc->chip_data;
2295 if (assign_irq_vector(irq, cfg, mask))
Suresh Siddha18374d82009-12-17 18:29:46 -08002296 return -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002297
Gary Hadee85abf82009-04-08 14:07:25 -07002298 cpumask_copy(desc->affinity, mask);
2299
Suresh Siddha18374d82009-12-17 18:29:46 -08002300 *dest_id = apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2301 return 0;
Gary Hadee85abf82009-04-08 14:07:25 -07002302}
2303
Ingo Molnar44204712009-05-01 19:02:50 +02002304static int
Gary Hadee85abf82009-04-08 14:07:25 -07002305set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2306{
2307 struct irq_cfg *cfg;
2308 unsigned long flags;
2309 unsigned int dest;
2310 unsigned int irq;
Ingo Molnar44204712009-05-01 19:02:50 +02002311 int ret = -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002312
2313 irq = desc->irq;
2314 cfg = desc->chip_data;
2315
2316 spin_lock_irqsave(&ioapic_lock, flags);
Suresh Siddha18374d82009-12-17 18:29:46 -08002317 ret = set_desc_affinity(desc, mask, &dest);
2318 if (!ret) {
Gary Hadee85abf82009-04-08 14:07:25 -07002319 /* Only the high 8 bits are valid. */
2320 dest = SET_APIC_LOGICAL_ID(dest);
2321 __target_IO_APIC_irq(irq, dest, cfg);
2322 }
2323 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnar44204712009-05-01 19:02:50 +02002324
2325 return ret;
Gary Hadee85abf82009-04-08 14:07:25 -07002326}
2327
Ingo Molnar44204712009-05-01 19:02:50 +02002328static int
Gary Hadee85abf82009-04-08 14:07:25 -07002329set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2330{
2331 struct irq_desc *desc;
2332
2333 desc = irq_to_desc(irq);
2334
Ingo Molnar44204712009-05-01 19:02:50 +02002335 return set_ioapic_affinity_irq_desc(desc, mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002336}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002337
2338#ifdef CONFIG_INTR_REMAP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002339
2340/*
2341 * Migrate the IO-APIC irq in the presence of intr-remapping.
2342 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002343 * For both level and edge triggered, irq migration is a simple atomic
2344 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002345 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002346 * For level triggered, we eliminate the io-apic RTE modification (with the
2347 * updated vector information), by using a virtual vector (io-apic pin number).
2348 * Real vector that is used for interrupting cpu will be coming from
2349 * the interrupt-remapping table entry.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002350 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002351static int
Mike Travise7986732008-12-16 17:33:52 -08002352migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002353{
2354 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002355 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002356 unsigned int dest;
Yinghai Lu3145e942008-12-05 18:58:34 -08002357 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -07002358 int ret = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002359
Mike Travis22f65d32008-12-16 17:33:56 -08002360 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002361 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002362
Yinghai Lu3145e942008-12-05 18:58:34 -08002363 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002364 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002365 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002366
Yinghai Lu3145e942008-12-05 18:58:34 -08002367 cfg = desc->chip_data;
2368 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002369 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002370
Ingo Molnardebccb32009-01-28 15:20:18 +01002371 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372
Ingo Molnar54168ed2008-08-20 09:07:45 +02002373 irte.vector = cfg->vector;
2374 irte.dest_id = IRTE_DEST(dest);
2375
2376 /*
2377 * Modified the IRTE and flushes the Interrupt entry cache.
2378 */
2379 modify_irte(irq, &irte);
2380
Mike Travis22f65d32008-12-16 17:33:56 -08002381 if (cfg->move_in_progress)
2382 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002383
Mike Travis7f7ace02009-01-10 21:58:08 -08002384 cpumask_copy(desc->affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -07002385
2386 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002387}
2388
Ingo Molnar54168ed2008-08-20 09:07:45 +02002389/*
2390 * Migrates the IRQ destination in the process context.
2391 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002392static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Rusty Russell968ea6d2008-12-13 21:55:51 +10302393 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002394{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002395 return migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002396}
Yinghai Lud5dedd42009-04-27 17:59:21 -07002397static int set_ir_ioapic_affinity_irq(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +10302398 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002399{
2400 struct irq_desc *desc = irq_to_desc(irq);
2401
Yinghai Lud5dedd42009-04-27 17:59:21 -07002402 return set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002403}
Suresh Siddha29b61be2009-03-16 17:05:02 -07002404#else
Yinghai Lud5dedd42009-04-27 17:59:21 -07002405static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Suresh Siddha29b61be2009-03-16 17:05:02 -07002406 const struct cpumask *mask)
2407{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002408 return 0;
Suresh Siddha29b61be2009-03-16 17:05:02 -07002409}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002410#endif
2411
Yinghai Lu497c9a12008-08-19 20:50:28 -07002412asmlinkage void smp_irq_move_cleanup_interrupt(void)
2413{
2414 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002415
Yinghai Lu497c9a12008-08-19 20:50:28 -07002416 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002417 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002418 irq_enter();
2419
2420 me = smp_processor_id();
2421 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2422 unsigned int irq;
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002423 unsigned int irr;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002424 struct irq_desc *desc;
2425 struct irq_cfg *cfg;
2426 irq = __get_cpu_var(vector_irq)[vector];
2427
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002428 if (irq == -1)
2429 continue;
2430
Yinghai Lu497c9a12008-08-19 20:50:28 -07002431 desc = irq_to_desc(irq);
2432 if (!desc)
2433 continue;
2434
2435 cfg = irq_cfg(irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +01002436 raw_spin_lock(&desc->lock);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002437
Mike Travis22f65d32008-12-16 17:33:56 -08002438 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002439 goto unlock;
2440
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002441 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2442 /*
2443 * Check if the vector that needs to be cleanedup is
2444 * registered at the cpu's IRR. If so, then this is not
2445 * the best time to clean it up. Lets clean it up in the
2446 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2447 * to myself.
2448 */
2449 if (irr & (1 << (vector % 32))) {
2450 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2451 goto unlock;
2452 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002453 __get_cpu_var(vector_irq)[vector] = -1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002454unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +01002455 raw_spin_unlock(&desc->lock);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002456 }
2457
2458 irq_exit();
2459}
2460
Suresh Siddhaa5e74b82009-10-26 14:24:34 -08002461static void __irq_complete_move(struct irq_desc **descp, unsigned vector)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002462{
Yinghai Lu3145e942008-12-05 18:58:34 -08002463 struct irq_desc *desc = *descp;
2464 struct irq_cfg *cfg = desc->chip_data;
Suresh Siddhaa5e74b82009-10-26 14:24:34 -08002465 unsigned me;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002466
Yinghai Lufcef5912009-04-27 17:58:23 -07002467 if (likely(!cfg->move_in_progress))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002468 return;
2469
Yinghai Lu497c9a12008-08-19 20:50:28 -07002470 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002471
Yinghai Lufcef5912009-04-27 17:58:23 -07002472 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Mike Travis22f65d32008-12-16 17:33:56 -08002473 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002474}
Suresh Siddhaa5e74b82009-10-26 14:24:34 -08002475
2476static void irq_complete_move(struct irq_desc **descp)
2477{
2478 __irq_complete_move(descp, ~get_irq_regs()->orig_ax);
2479}
2480
2481void irq_force_complete_move(int irq)
2482{
2483 struct irq_desc *desc = irq_to_desc(irq);
2484 struct irq_cfg *cfg = desc->chip_data;
2485
2486 __irq_complete_move(&desc, cfg->vector);
2487}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002488#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002489static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002490#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002491
Yinghai Lu1d025192008-08-19 20:50:34 -07002492static void ack_apic_edge(unsigned int irq)
2493{
Yinghai Lu3145e942008-12-05 18:58:34 -08002494 struct irq_desc *desc = irq_to_desc(irq);
2495
2496 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002497 move_native_irq(irq);
2498 ack_APIC_irq();
2499}
2500
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002501atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002502
Suresh Siddhac29d9db2009-12-01 15:31:16 -08002503/*
2504 * IO-APIC versions below 0x20 don't support EOI register.
2505 * For the record, here is the information about various versions:
2506 * 0Xh 82489DX
2507 * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
2508 * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
2509 * 30h-FFh Reserved
2510 *
2511 * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
2512 * version as 0x2. This is an error with documentation and these ICH chips
2513 * use io-apic's of version 0x20.
2514 *
2515 * For IO-APIC's with EOI register, we use that to do an explicit EOI.
2516 * Otherwise, we simulate the EOI message manually by changing the trigger
2517 * mode to edge and then back to level, with RTE being masked during this.
2518*/
Suresh Siddhab3ec0a32009-10-26 14:24:35 -08002519static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2520{
2521 struct irq_pin_list *entry;
2522
2523 for_each_irq_pin(entry, cfg->irq_2_pin) {
Suresh Siddhac29d9db2009-12-01 15:31:16 -08002524 if (mp_ioapics[entry->apic].apicver >= 0x20) {
2525 /*
2526 * Intr-remapping uses pin number as the virtual vector
2527 * in the RTE. Actual vector is programmed in
2528 * intr-remapping table entry. Hence for the io-apic
2529 * EOI we use the pin number.
2530 */
2531 if (irq_remapped(irq))
2532 io_apic_eoi(entry->apic, entry->pin);
2533 else
2534 io_apic_eoi(entry->apic, cfg->vector);
2535 } else {
2536 __mask_and_edge_IO_APIC_irq(entry);
2537 __unmask_and_level_IO_APIC_irq(entry);
2538 }
Suresh Siddhab3ec0a32009-10-26 14:24:35 -08002539 }
2540}
2541
2542static void eoi_ioapic_irq(struct irq_desc *desc)
2543{
2544 struct irq_cfg *cfg;
2545 unsigned long flags;
2546 unsigned int irq;
2547
2548 irq = desc->irq;
2549 cfg = desc->chip_data;
2550
2551 spin_lock_irqsave(&ioapic_lock, flags);
2552 __eoi_ioapic_irq(irq, cfg);
2553 spin_unlock_irqrestore(&ioapic_lock, flags);
2554}
2555
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002556static void ack_apic_level(unsigned int irq)
2557{
Yinghai Lu3145e942008-12-05 18:58:34 -08002558 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002559 unsigned long v;
2560 int i;
Yinghai Lu3145e942008-12-05 18:58:34 -08002561 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002562 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002563
Yinghai Lu3145e942008-12-05 18:58:34 -08002564 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002565#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002566 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002567 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002568 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002569 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002570 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002571#endif
2572
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002573 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002574 * It appears there is an erratum which affects at least version 0x11
2575 * of I/O APIC (that's the 82093AA and cores integrated into various
2576 * chipsets). Under certain conditions a level-triggered interrupt is
2577 * erroneously delivered as edge-triggered one but the respective IRR
2578 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2579 * message but it will never arrive and further interrupts are blocked
2580 * from the source. The exact reason is so far unknown, but the
2581 * phenomenon was observed when two consecutive interrupt requests
2582 * from a given source get delivered to the same CPU and the source is
2583 * temporarily disabled in between.
2584 *
2585 * A workaround is to simulate an EOI message manually. We achieve it
2586 * by setting the trigger mode to edge and then to level when the edge
2587 * trigger mode gets detected in the TMR of a local APIC for a
2588 * level-triggered interrupt. We mask the source for the time of the
2589 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2590 * The idea is from Manfred Spraul. --macro
Suresh Siddha1c839952009-12-01 15:31:17 -08002591 *
2592 * Also in the case when cpu goes offline, fixup_irqs() will forward
2593 * any unhandled interrupt on the offlined cpu to the new cpu
2594 * destination that is handling the corresponding interrupt. This
2595 * interrupt forwarding is done via IPI's. Hence, in this case also
2596 * level-triggered io-apic interrupt will be seen as an edge
2597 * interrupt in the IRR. And we can't rely on the cpu's EOI
2598 * to be broadcasted to the IO-APIC's which will clear the remoteIRR
2599 * corresponding to the level-triggered interrupt. Hence on IO-APIC's
2600 * supporting EOI register, we do an explicit EOI to clear the
2601 * remote IRR and on IO-APIC's which don't have an EOI register,
2602 * we use the above logic (mask+edge followed by unmask+level) from
2603 * Manfred Spraul to clear the remote IRR.
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002604 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002605 cfg = desc->chip_data;
2606 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002607 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002608
Ingo Molnar54168ed2008-08-20 09:07:45 +02002609 /*
2610 * We must acknowledge the irq before we move it or the acknowledge will
2611 * not propagate properly.
2612 */
2613 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002614
Suresh Siddha1c839952009-12-01 15:31:17 -08002615 /*
2616 * Tail end of clearing remote IRR bit (either by delivering the EOI
2617 * message via io-apic EOI register write or simulating it using
2618 * mask+edge followed by unnask+level logic) manually when the
2619 * level triggered interrupt is seen as the edge triggered interrupt
2620 * at the cpu.
2621 */
Maciej W. Rozyckica64c472009-12-01 15:31:15 -08002622 if (!(v & (1 << (i & 0x1f)))) {
2623 atomic_inc(&irq_mis_count);
2624
Suresh Siddhac29d9db2009-12-01 15:31:16 -08002625 eoi_ioapic_irq(desc);
Maciej W. Rozyckica64c472009-12-01 15:31:15 -08002626 }
2627
Ingo Molnar54168ed2008-08-20 09:07:45 +02002628 /* Now we can move and renable the irq */
2629 if (unlikely(do_unmask_irq)) {
2630 /* Only migrate the irq if the ack has been received.
2631 *
2632 * On rare occasions the broadcast level triggered ack gets
2633 * delayed going to ioapics, and if we reprogram the
2634 * vector while Remote IRR is still set the irq will never
2635 * fire again.
2636 *
2637 * To prevent this scenario we read the Remote IRR bit
2638 * of the ioapic. This has two effects.
2639 * - On any sane system the read of the ioapic will
2640 * flush writes (and acks) going to the ioapic from
2641 * this cpu.
2642 * - We get to see if the ACK has actually been delivered.
2643 *
2644 * Based on failed experiments of reprogramming the
2645 * ioapic entry from outside of irq context starting
2646 * with masking the ioapic entry and then polling until
2647 * Remote IRR was clear before reprogramming the
2648 * ioapic I don't trust the Remote IRR bit to be
2649 * completey accurate.
2650 *
2651 * However there appears to be no other way to plug
2652 * this race, so if the Remote IRR bit is not
2653 * accurate and is causing problems then it is a hardware bug
2654 * and you can go talk to the chipset vendor about it.
2655 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002656 cfg = desc->chip_data;
2657 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002658 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002659 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002660 }
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002661}
Yinghai Lu1d025192008-08-19 20:50:34 -07002662
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002663#ifdef CONFIG_INTR_REMAP
2664static void ir_ack_apic_edge(unsigned int irq)
2665{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002666 ack_APIC_irq();
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002667}
2668
2669static void ir_ack_apic_level(unsigned int irq)
2670{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002671 struct irq_desc *desc = irq_to_desc(irq);
2672
2673 ack_APIC_irq();
2674 eoi_ioapic_irq(desc);
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002675}
2676#endif /* CONFIG_INTR_REMAP */
2677
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002678static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002679 .name = "IO-APIC",
2680 .startup = startup_ioapic_irq,
2681 .mask = mask_IO_APIC_irq,
2682 .unmask = unmask_IO_APIC_irq,
2683 .ack = ack_apic_edge,
2684 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002685#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002686 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002687#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002688 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689};
2690
Ingo Molnar54168ed2008-08-20 09:07:45 +02002691static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002692 .name = "IR-IO-APIC",
2693 .startup = startup_ioapic_irq,
2694 .mask = mask_IO_APIC_irq,
2695 .unmask = unmask_IO_APIC_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302696#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002697 .ack = ir_ack_apic_edge,
2698 .eoi = ir_ack_apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002699#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002700 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002701#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302702#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002703 .retrigger = ioapic_retrigger_irq,
2704};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706static inline void init_IO_APIC_traps(void)
2707{
2708 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002709 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002710 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 /*
2713 * NOTE! The local APIC isn't very good at handling
2714 * multiple interrupts at the same interrupt level.
2715 * As the interrupt level is determined by taking the
2716 * vector number and shifting that right by 4, we
2717 * want to spread these out a bit so that they don't
2718 * all fall in the same interrupt level.
2719 *
2720 * Also, we've got to be careful not to trash gate
2721 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2722 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002723 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002724 cfg = desc->chip_data;
2725 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /*
2727 * Hmm.. We don't have an entry for this,
2728 * so default to an old-fashioned 8259
2729 * interrupt if we can..
2730 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002731 if (irq < nr_legacy_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002733 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002735 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
2737 }
2738}
2739
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002740/*
2741 * The local APIC irq-chip implementation:
2742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002744static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 unsigned long v;
2747
2748 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002749 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002752static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002754 unsigned long v;
2755
2756 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002757 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
Yinghai Lu3145e942008-12-05 18:58:34 -08002760static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002761{
2762 ack_APIC_irq();
2763}
2764
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002765static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002766 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002767 .mask = mask_lapic_irq,
2768 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002769 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770};
2771
Yinghai Lu3145e942008-12-05 18:58:34 -08002772static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002773{
Yinghai Lu08678b02008-08-19 20:50:05 -07002774 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002775 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2776 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002777}
2778
Jan Beuliche9427102008-01-30 13:31:24 +01002779static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
2781 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002782 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 * We put the 8259A master into AEOI mode and
2784 * unmask on all local APICs LVT0 as NMI.
2785 *
2786 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2787 * is from Maciej W. Rozycki - so we do not have to EOI from
2788 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2791
Jan Beuliche9427102008-01-30 13:31:24 +01002792 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 apic_printk(APIC_VERBOSE, " done.\n");
2795}
2796
2797/*
2798 * This looks a bit hackish but it's about the only one way of sending
2799 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2800 * not support the ExtINT mode, unfortunately. We need to send these
2801 * cycles as some i82489DX-based boards have glue logic that keeps the
2802 * 8259A interrupt line asserted until INTA. --macro
2803 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002804static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002806 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 struct IO_APIC_route_entry entry0, entry1;
2808 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002810 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002811 if (pin == -1) {
2812 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002814 }
2815 apic = find_isa_irq_apic(8, mp_INT);
2816 if (apic == -1) {
2817 WARN_ON_ONCE(1);
2818 return;
2819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Andi Kleencf4c6a22006-09-26 10:52:30 +02002821 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002822 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 memset(&entry1, 0, sizeof(entry1));
2825
2826 entry1.dest_mode = 0; /* physical delivery */
2827 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002828 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 entry1.delivery_mode = dest_ExtINT;
2830 entry1.polarity = entry0.polarity;
2831 entry1.trigger = 0;
2832 entry1.vector = 0;
2833
Andi Kleencf4c6a22006-09-26 10:52:30 +02002834 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 save_control = CMOS_READ(RTC_CONTROL);
2837 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2838 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2839 RTC_FREQ_SELECT);
2840 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2841
2842 i = 100;
2843 while (i-- > 0) {
2844 mdelay(10);
2845 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2846 i -= 10;
2847 }
2848
2849 CMOS_WRITE(save_control, RTC_CONTROL);
2850 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002851 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Andi Kleencf4c6a22006-09-26 10:52:30 +02002853 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Yinghai Luefa25592008-08-19 20:50:36 -07002856static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002857/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002858static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002859{
2860 disable_timer_pin_1 = 1;
2861 return 0;
2862}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002863early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002864
2865int timer_through_8259 __initdata;
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867/*
2868 * This code may look a bit paranoid, but it's supposed to cooperate with
2869 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2870 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2871 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002872 *
2873 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002875static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Yinghai Lu3145e942008-12-05 18:58:34 -08002877 struct irq_desc *desc = irq_to_desc(0);
2878 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002879 int node = cpu_to_node(boot_cpu_id);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002880 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002881 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002882 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002883
2884 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /*
2887 * get/set the timer IRQ vector:
2888 */
2889 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002890 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002893 * As IRQ0 is to be enabled in the 8259A, the virtual
2894 * wire has to be disabled in the local APIC. Also
2895 * timer interrupts need to be acknowledged manually in
2896 * the 8259A for the i82489DX when using the NMI
2897 * watchdog as that APIC treats NMIs as level-triggered.
2898 * The AEOI mode will finish them in the 8259A
2899 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002901 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002903#ifdef CONFIG_X86_32
Yinghai Luf72dcca2009-02-08 16:18:03 -08002904 {
2905 unsigned int ver;
2906
2907 ver = apic_read(APIC_LVR);
2908 ver = GET_APIC_VERSION(ver);
2909 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2910 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002913 pin1 = find_isa_irq_pin(0, mp_INT);
2914 apic1 = find_isa_irq_apic(0, mp_INT);
2915 pin2 = ioapic_i8259.pin;
2916 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002918 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2919 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002920 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002922 /*
2923 * Some BIOS writers are clueless and report the ExtINTA
2924 * I/O APIC input from the cascaded 8259A as the timer
2925 * interrupt input. So just in case, if only one pin
2926 * was found above, try it both directly and through the
2927 * 8259A.
2928 */
2929 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002930 if (intr_remapping_enabled)
2931 panic("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002932 pin1 = pin2;
2933 apic1 = apic2;
2934 no_pin1 = 1;
2935 } else if (pin2 == -1) {
2936 pin2 = pin1;
2937 apic2 = apic1;
2938 }
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (pin1 != -1) {
2941 /*
2942 * Ok, does IRQ0 through the IOAPIC work?
2943 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002944 if (no_pin1) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002945 add_pin_to_irq_node(cfg, node, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002946 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002947 } else {
2948 /* for edge trigger, setup_IO_APIC_irq already
2949 * leave it unmasked.
2950 * so only need to unmask if it is level-trigger
2951 * do we really have level trigger timer?
2952 */
2953 int idx;
2954 idx = find_irq_entry(apic1, pin1, mp_INT);
2955 if (idx != -1 && irq_trigger(idx))
2956 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (timer_irq_works()) {
2959 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 setup_nmi();
2961 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002963 if (disable_timer_pin_1 > 0)
2964 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002967 if (intr_remapping_enabled)
2968 panic("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002969 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002970 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002971 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002972 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2973 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002975 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2976 "(IRQ0) through the 8259A ...\n");
2977 apic_printk(APIC_QUIET, KERN_INFO
2978 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /*
2980 * legacy devices should be connected to IO APIC #0
2981 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002982 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002983 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002984 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002986 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002987 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002989 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002991 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 /*
2996 * Cleanup, just in case ...
2997 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002998 local_irq_disable();
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002999 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08003000 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003001 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003005 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
3006 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04003007 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02003009#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01003010 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003013 apic_printk(APIC_QUIET, KERN_INFO
3014 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Yinghai Lu3145e942008-12-05 18:58:34 -08003016 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003017 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 enable_8259A_irq(0);
3019
3020 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003021 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08003024 local_irq_disable();
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01003025 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003026 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003027 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003029 apic_printk(APIC_QUIET, KERN_INFO
3030 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 init_8259A(0);
3033 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01003034 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 unlock_ExtINT_logic();
3037
3038 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003039 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08003042 local_irq_disable();
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003043 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003045 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003046out:
3047 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
3050/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003051 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3052 * to devices. However there may be an I/O APIC pin available for
3053 * this interrupt regardless. The pin may be left unconnected, but
3054 * typically it will be reused as an ExtINT cascade interrupt for
3055 * the master 8259A. In the MPS case such a pin will normally be
3056 * reported as an ExtINT interrupt in the MP table. With ACPI
3057 * there is no provision for ExtINT interrupts, and in the absence
3058 * of an override it would be treated as an ordinary ISA I/O APIC
3059 * interrupt, that is edge-triggered and unmasked by default. We
3060 * used to do this, but it caused problems on some systems because
3061 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3062 * the same ExtINT cascade interrupt to drive the local APIC of the
3063 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3064 * the I/O APIC in all cases now. No actual device should request
3065 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003067#define PIC_IRQS (1UL << PIC_CASCADE_IR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069void __init setup_IO_APIC(void)
3070{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003071
Ingo Molnar54168ed2008-08-20 09:07:45 +02003072 /*
3073 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3074 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003075 io_apic_irqs = nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Ingo Molnar54168ed2008-08-20 09:07:45 +02003077 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003078 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003079 * Set up IO-APIC IRQ routing.
3080 */
Thomas Gleixnerde934102009-08-20 09:27:29 +02003081 x86_init.mpparse.setup_ioapic_ids();
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 sync_Arb_IDs();
3084 setup_IO_APIC_irqs();
3085 init_IO_APIC_traps();
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003086 if (nr_legacy_irqs)
3087 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
3090/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003091 * Called after all the initialization is done. If we didnt find any
3092 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095static int __init io_apic_bug_finalize(void)
3096{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003097 if (sis_apic_bug == -1)
3098 sis_apic_bug = 0;
3099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
3102late_initcall(io_apic_bug_finalize);
3103
3104struct sysfs_ioapic_data {
3105 struct sys_device dev;
3106 struct IO_APIC_route_entry entry[0];
3107};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003108static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Pavel Machek438510f2005-04-16 15:25:24 -07003110static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct IO_APIC_route_entry *entry;
3113 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 data = container_of(dev, struct sysfs_ioapic_data, dev);
3117 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003118 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3119 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 return 0;
3122}
3123
3124static int ioapic_resume(struct sys_device *dev)
3125{
3126 struct IO_APIC_route_entry *entry;
3127 struct sysfs_ioapic_data *data;
3128 unsigned long flags;
3129 union IO_APIC_reg_00 reg_00;
3130 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 data = container_of(dev, struct sysfs_ioapic_data, dev);
3133 entry = data->entry;
3134
3135 spin_lock_irqsave(&ioapic_lock, flags);
3136 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05303137 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3138 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 io_apic_write(dev->id, 0, reg_00.raw);
3140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003142 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003143 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 return 0;
3146}
3147
3148static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003149 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 .suspend = ioapic_suspend,
3151 .resume = ioapic_resume,
3152};
3153
3154static int __init ioapic_init_sysfs(void)
3155{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003156 struct sys_device * dev;
3157 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 error = sysdev_class_register(&ioapic_sysdev_class);
3160 if (error)
3161 return error;
3162
Ingo Molnar54168ed2008-08-20 09:07:45 +02003163 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003164 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003166 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (!mp_ioapic_data[i]) {
3168 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3169 continue;
3170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003172 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 dev->cls = &ioapic_sysdev_class;
3174 error = sysdev_register(dev);
3175 if (error) {
3176 kfree(mp_ioapic_data[i]);
3177 mp_ioapic_data[i] = NULL;
3178 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3179 continue;
3180 }
3181 }
3182
3183 return 0;
3184}
3185
3186device_initcall(ioapic_init_sysfs);
3187
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003188/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003189 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003190 */
Yinghai Lud047f53a2009-04-27 18:02:23 -07003191unsigned int create_irq_nr(unsigned int irq_want, int node)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003192{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003193 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003194 unsigned int irq;
3195 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003196 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003197 struct irq_cfg *cfg_new = NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003198 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003199
3200 irq = 0;
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003201 if (irq_want < nr_irqs_gsi)
3202 irq_want = nr_irqs_gsi;
3203
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003204 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003205 for (new = irq_want; new < nr_irqs; new++) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07003206 desc_new = irq_to_desc_alloc_node(new, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003207 if (!desc_new) {
3208 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003209 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003210 }
3211 cfg_new = desc_new->chip_data;
3212
3213 if (cfg_new->vector != 0)
3214 continue;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003215
Yinghai Lu15e957d2009-04-30 01:17:50 -07003216 desc_new = move_irq_desc(desc_new, node);
Yinghai Lu37ef2a32009-11-21 00:23:37 -08003217 cfg_new = desc_new->chip_data;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003218
Ingo Molnarfe402e12009-01-28 04:32:51 +01003219 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003220 irq = new;
3221 break;
3222 }
3223 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003224
Yinghai Lu199751d2008-08-19 20:50:27 -07003225 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003226 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003227 /* restore it, in case dynamic_irq_init clear it */
3228 if (desc_new)
3229 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003230 }
3231 return irq;
3232}
3233
Yinghai Lu199751d2008-08-19 20:50:27 -07003234int create_irq(void)
3235{
Yinghai Lud047f53a2009-04-27 18:02:23 -07003236 int node = cpu_to_node(boot_cpu_id);
Yinghai Lube5d5352008-12-05 18:58:33 -08003237 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003238 int irq;
3239
Yinghai Lube5d5352008-12-05 18:58:33 -08003240 irq_want = nr_irqs_gsi;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003241 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003242
3243 if (irq == 0)
3244 irq = -1;
3245
3246 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003247}
3248
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003249void destroy_irq(unsigned int irq)
3250{
3251 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003252 struct irq_cfg *cfg;
3253 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003254
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003255 /* store it, in case dynamic_irq_cleanup clear it */
3256 desc = irq_to_desc(irq);
3257 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003258 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003259 /* connect back irq_cfg */
Bartlomiej Zolnierkiewicz25f6e892009-07-30 23:21:18 +02003260 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003261
Ingo Molnar54168ed2008-08-20 09:07:45 +02003262 free_irte(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003263 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003264 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003265 spin_unlock_irqrestore(&vector_lock, flags);
3266}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003267
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003268/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003269 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003270 */
3271#ifdef CONFIG_PCI_MSI
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003272static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq,
3273 struct msi_msg *msg, u8 hpet_id)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003274{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003275 struct irq_cfg *cfg;
3276 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003277 unsigned dest;
3278
Jan Beulichf1182632009-01-14 12:27:35 +00003279 if (disable_apic)
3280 return -ENXIO;
3281
Yinghai Lu3145e942008-12-05 18:58:34 -08003282 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003283 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003284 if (err)
3285 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003286
Ingo Molnardebccb32009-01-28 15:20:18 +01003287 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003288
Ingo Molnar54168ed2008-08-20 09:07:45 +02003289 if (irq_remapped(irq)) {
3290 struct irte irte;
3291 int ir_index;
3292 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003293
Ingo Molnar54168ed2008-08-20 09:07:45 +02003294 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3295 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003296
Ingo Molnar54168ed2008-08-20 09:07:45 +02003297 memset (&irte, 0, sizeof(irte));
3298
3299 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003300 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003301 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003302 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003303 irte.vector = cfg->vector;
3304 irte.dest_id = IRTE_DEST(dest);
3305
Weidong Hanf007e992009-05-23 00:41:15 +08003306 /* Set source-id of interrupt request */
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003307 if (pdev)
3308 set_msi_sid(&irte, pdev);
3309 else
3310 set_hpet_sid(&irte, hpet_id);
Weidong Hanf007e992009-05-23 00:41:15 +08003311
Ingo Molnar54168ed2008-08-20 09:07:45 +02003312 modify_irte(irq, &irte);
3313
3314 msg->address_hi = MSI_ADDR_BASE_HI;
3315 msg->data = sub_handle;
3316 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3317 MSI_ADDR_IR_SHV |
3318 MSI_ADDR_IR_INDEX1(ir_index) |
3319 MSI_ADDR_IR_INDEX2(ir_index);
Suresh Siddha29b61be2009-03-16 17:05:02 -07003320 } else {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003321 if (x2apic_enabled())
3322 msg->address_hi = MSI_ADDR_BASE_HI |
3323 MSI_ADDR_EXT_DEST_ID(dest);
3324 else
3325 msg->address_hi = MSI_ADDR_BASE_HI;
3326
Ingo Molnar54168ed2008-08-20 09:07:45 +02003327 msg->address_lo =
3328 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003329 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003330 MSI_ADDR_DEST_MODE_PHYSICAL:
3331 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003332 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003333 MSI_ADDR_REDIRECTION_CPU:
3334 MSI_ADDR_REDIRECTION_LOWPRI) |
3335 MSI_ADDR_DEST_ID(dest);
3336
3337 msg->data =
3338 MSI_DATA_TRIGGER_EDGE |
3339 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003340 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003341 MSI_DATA_DELIVERY_FIXED:
3342 MSI_DATA_DELIVERY_LOWPRI) |
3343 MSI_DATA_VECTOR(cfg->vector);
3344 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003345 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003346}
3347
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003348#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003349static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003350{
Yinghai Lu3145e942008-12-05 18:58:34 -08003351 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003352 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003353 struct msi_msg msg;
3354 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003355
Suresh Siddha18374d82009-12-17 18:29:46 -08003356 if (set_desc_affinity(desc, mask, &dest))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003357 return -1;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003358
Yinghai Lu3145e942008-12-05 18:58:34 -08003359 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003360
Yinghai Lu3145e942008-12-05 18:58:34 -08003361 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003362
3363 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003364 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003365 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3366 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3367
Yinghai Lu3145e942008-12-05 18:58:34 -08003368 write_msi_msg_desc(desc, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003369
3370 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003371}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003372#ifdef CONFIG_INTR_REMAP
3373/*
3374 * Migrate the MSI irq to another cpumask. This migration is
3375 * done in the process context using interrupt-remapping hardware.
3376 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07003377static int
Mike Travise7986732008-12-16 17:33:52 -08003378ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003379{
Yinghai Lu3145e942008-12-05 18:58:34 -08003380 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003381 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003382 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003383 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003384
3385 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003386 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003387
Suresh Siddha18374d82009-12-17 18:29:46 -08003388 if (set_desc_affinity(desc, mask, &dest))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003389 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003390
Ingo Molnar54168ed2008-08-20 09:07:45 +02003391 irte.vector = cfg->vector;
3392 irte.dest_id = IRTE_DEST(dest);
3393
3394 /*
3395 * atomically update the IRTE with the new destination and vector.
3396 */
3397 modify_irte(irq, &irte);
3398
3399 /*
3400 * After this point, all the interrupts will start arriving
3401 * at the new destination. So, time to cleanup the previous
3402 * vector allocation.
3403 */
Mike Travis22f65d32008-12-16 17:33:56 -08003404 if (cfg->move_in_progress)
3405 send_cleanup_vector(cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003406
3407 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003408}
Yinghai Lu3145e942008-12-05 18:58:34 -08003409
Ingo Molnar54168ed2008-08-20 09:07:45 +02003410#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003411#endif /* CONFIG_SMP */
3412
3413/*
3414 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3415 * which implement the MSI or MSI-X Capability Structure.
3416 */
3417static struct irq_chip msi_chip = {
3418 .name = "PCI-MSI",
3419 .unmask = unmask_msi_irq,
3420 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003421 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003422#ifdef CONFIG_SMP
3423 .set_affinity = set_msi_irq_affinity,
3424#endif
3425 .retrigger = ioapic_retrigger_irq,
3426};
3427
Ingo Molnar54168ed2008-08-20 09:07:45 +02003428static struct irq_chip msi_ir_chip = {
3429 .name = "IR-PCI-MSI",
3430 .unmask = unmask_msi_irq,
3431 .mask = mask_msi_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303432#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08003433 .ack = ir_ack_apic_edge,
Ingo Molnar54168ed2008-08-20 09:07:45 +02003434#ifdef CONFIG_SMP
3435 .set_affinity = ir_set_msi_irq_affinity,
3436#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303437#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02003438 .retrigger = ioapic_retrigger_irq,
3439};
3440
3441/*
3442 * Map the PCI dev to the corresponding remapping hardware unit
3443 * and allocate 'nvec' consecutive interrupt-remapping table entries
3444 * in it.
3445 */
3446static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3447{
3448 struct intel_iommu *iommu;
3449 int index;
3450
3451 iommu = map_dev_to_ir(dev);
3452 if (!iommu) {
3453 printk(KERN_ERR
3454 "Unable to map PCI %s to iommu\n", pci_name(dev));
3455 return -ENOENT;
3456 }
3457
3458 index = alloc_irte(iommu, irq, nvec);
3459 if (index < 0) {
3460 printk(KERN_ERR
3461 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003462 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003463 return -ENOSPC;
3464 }
3465 return index;
3466}
Yinghai Lu1d025192008-08-19 20:50:34 -07003467
Yinghai Lu3145e942008-12-05 18:58:34 -08003468static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003469{
3470 int ret;
3471 struct msi_msg msg;
3472
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003473 ret = msi_compose_msg(dev, irq, &msg, -1);
Yinghai Lu1d025192008-08-19 20:50:34 -07003474 if (ret < 0)
3475 return ret;
3476
Yinghai Lu3145e942008-12-05 18:58:34 -08003477 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003478 write_msi_msg(irq, &msg);
3479
Ingo Molnar54168ed2008-08-20 09:07:45 +02003480 if (irq_remapped(irq)) {
3481 struct irq_desc *desc = irq_to_desc(irq);
3482 /*
3483 * irq migration in process context
3484 */
3485 desc->status |= IRQ_MOVE_PCNTXT;
3486 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3487 } else
Ingo Molnar54168ed2008-08-20 09:07:45 +02003488 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003489
Yinghai Luc81bba42008-09-25 11:53:11 -07003490 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3491
Yinghai Lu1d025192008-08-19 20:50:34 -07003492 return 0;
3493}
3494
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003495int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3496{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003497 unsigned int irq;
3498 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003499 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003500 unsigned int irq_want;
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003501 struct intel_iommu *iommu = NULL;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003502 int index = 0;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003503 int node;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003504
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -04003505 /* x86 doesn't support multiple MSI yet */
3506 if (type == PCI_CAP_ID_MSI && nvec > 1)
3507 return 1;
3508
Yinghai Lud047f53a2009-04-27 18:02:23 -07003509 node = dev_to_node(&dev->dev);
Yinghai Lube5d5352008-12-05 18:58:33 -08003510 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003511 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003512 list_for_each_entry(msidesc, &dev->msi_list, list) {
Yinghai Lud047f53a2009-04-27 18:02:23 -07003513 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003514 if (irq == 0)
3515 return -1;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003516 irq_want = irq + 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003517 if (!intr_remapping_enabled)
3518 goto no_ir;
3519
3520 if (!sub_handle) {
3521 /*
3522 * allocate the consecutive block of IRTE's
3523 * for 'nvec'
3524 */
3525 index = msi_alloc_irte(dev, irq, nvec);
3526 if (index < 0) {
3527 ret = index;
3528 goto error;
3529 }
3530 } else {
3531 iommu = map_dev_to_ir(dev);
3532 if (!iommu) {
3533 ret = -ENOENT;
3534 goto error;
3535 }
3536 /*
3537 * setup the mapping between the irq and the IRTE
3538 * base index, the sub_handle pointing to the
3539 * appropriate interrupt remap table entry.
3540 */
3541 set_irte_irq(irq, iommu, index, sub_handle);
3542 }
3543no_ir:
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003544 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003545 if (ret < 0)
3546 goto error;
3547 sub_handle++;
3548 }
3549 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003550
3551error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003552 destroy_irq(irq);
3553 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003554}
3555
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003556void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003557{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003558 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003559}
3560
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003561#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003562#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003563static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003564{
Yinghai Lu3145e942008-12-05 18:58:34 -08003565 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003566 struct irq_cfg *cfg;
3567 struct msi_msg msg;
3568 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003569
Suresh Siddha18374d82009-12-17 18:29:46 -08003570 if (set_desc_affinity(desc, mask, &dest))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003571 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003572
Yinghai Lu3145e942008-12-05 18:58:34 -08003573 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003574
3575 dmar_msi_read(irq, &msg);
3576
3577 msg.data &= ~MSI_DATA_VECTOR_MASK;
3578 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3579 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3580 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3581
3582 dmar_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003583
3584 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003585}
Yinghai Lu3145e942008-12-05 18:58:34 -08003586
Ingo Molnar54168ed2008-08-20 09:07:45 +02003587#endif /* CONFIG_SMP */
3588
Jaswinder Singh Rajput8f7007a2009-06-10 12:41:01 -07003589static struct irq_chip dmar_msi_type = {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003590 .name = "DMAR_MSI",
3591 .unmask = dmar_msi_unmask,
3592 .mask = dmar_msi_mask,
3593 .ack = ack_apic_edge,
3594#ifdef CONFIG_SMP
3595 .set_affinity = dmar_msi_set_affinity,
3596#endif
3597 .retrigger = ioapic_retrigger_irq,
3598};
3599
3600int arch_setup_dmar_msi(unsigned int irq)
3601{
3602 int ret;
3603 struct msi_msg msg;
3604
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003605 ret = msi_compose_msg(NULL, irq, &msg, -1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003606 if (ret < 0)
3607 return ret;
3608 dmar_msi_write(irq, &msg);
3609 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3610 "edge");
3611 return 0;
3612}
3613#endif
3614
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003615#ifdef CONFIG_HPET_TIMER
3616
3617#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003618static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003619{
Yinghai Lu3145e942008-12-05 18:58:34 -08003620 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003621 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003622 struct msi_msg msg;
3623 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003624
Suresh Siddha18374d82009-12-17 18:29:46 -08003625 if (set_desc_affinity(desc, mask, &dest))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003626 return -1;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003627
Yinghai Lu3145e942008-12-05 18:58:34 -08003628 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003629
3630 hpet_msi_read(irq, &msg);
3631
3632 msg.data &= ~MSI_DATA_VECTOR_MASK;
3633 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3634 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3635 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3636
3637 hpet_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003638
3639 return 0;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003640}
Yinghai Lu3145e942008-12-05 18:58:34 -08003641
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003642#endif /* CONFIG_SMP */
3643
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003644static struct irq_chip ir_hpet_msi_type = {
3645 .name = "IR-HPET_MSI",
3646 .unmask = hpet_msi_unmask,
3647 .mask = hpet_msi_mask,
3648#ifdef CONFIG_INTR_REMAP
3649 .ack = ir_ack_apic_edge,
3650#ifdef CONFIG_SMP
3651 .set_affinity = ir_set_msi_irq_affinity,
3652#endif
3653#endif
3654 .retrigger = ioapic_retrigger_irq,
3655};
3656
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003657static struct irq_chip hpet_msi_type = {
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003658 .name = "HPET_MSI",
3659 .unmask = hpet_msi_unmask,
3660 .mask = hpet_msi_mask,
3661 .ack = ack_apic_edge,
3662#ifdef CONFIG_SMP
3663 .set_affinity = hpet_msi_set_affinity,
3664#endif
3665 .retrigger = ioapic_retrigger_irq,
3666};
3667
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003668int arch_setup_hpet_msi(unsigned int irq, unsigned int id)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003669{
3670 int ret;
3671 struct msi_msg msg;
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003672 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003673
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003674 if (intr_remapping_enabled) {
3675 struct intel_iommu *iommu = map_hpet_to_ir(id);
3676 int index;
3677
3678 if (!iommu)
3679 return -1;
3680
3681 index = alloc_irte(iommu, irq, 1);
3682 if (index < 0)
3683 return -1;
3684 }
3685
3686 ret = msi_compose_msg(NULL, irq, &msg, id);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003687 if (ret < 0)
3688 return ret;
3689
3690 hpet_msi_write(irq, &msg);
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003691 desc->status |= IRQ_MOVE_PCNTXT;
Suresh Siddhac8bc6f32009-08-04 12:07:09 -07003692 if (irq_remapped(irq))
3693 set_irq_chip_and_handler_name(irq, &ir_hpet_msi_type,
3694 handle_edge_irq, "edge");
3695 else
3696 set_irq_chip_and_handler_name(irq, &hpet_msi_type,
3697 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003698
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003699 return 0;
3700}
3701#endif
3702
Ingo Molnar54168ed2008-08-20 09:07:45 +02003703#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003704/*
3705 * Hypertransport interrupt support
3706 */
3707#ifdef CONFIG_HT_IRQ
3708
3709#ifdef CONFIG_SMP
3710
Yinghai Lu497c9a12008-08-19 20:50:28 -07003711static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003712{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003713 struct ht_irq_msg msg;
3714 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003715
Yinghai Lu497c9a12008-08-19 20:50:28 -07003716 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003717 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003718
Yinghai Lu497c9a12008-08-19 20:50:28 -07003719 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003720 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003721
Eric W. Biedermanec683072006-11-08 17:44:57 -08003722 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003723}
3724
Yinghai Lud5dedd42009-04-27 17:59:21 -07003725static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003726{
Yinghai Lu3145e942008-12-05 18:58:34 -08003727 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003728 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003729 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003730
Suresh Siddha18374d82009-12-17 18:29:46 -08003731 if (set_desc_affinity(desc, mask, &dest))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003732 return -1;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003733
Yinghai Lu3145e942008-12-05 18:58:34 -08003734 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003735
Yinghai Lu497c9a12008-08-19 20:50:28 -07003736 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003737
3738 return 0;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003739}
Yinghai Lu3145e942008-12-05 18:58:34 -08003740
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003741#endif
3742
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003743static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003744 .name = "PCI-HT",
3745 .mask = mask_ht_irq,
3746 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003747 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003748#ifdef CONFIG_SMP
3749 .set_affinity = set_ht_irq_affinity,
3750#endif
3751 .retrigger = ioapic_retrigger_irq,
3752};
3753
3754int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3755{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003756 struct irq_cfg *cfg;
3757 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003758
Jan Beulichf1182632009-01-14 12:27:35 +00003759 if (disable_apic)
3760 return -ENXIO;
3761
Yinghai Lu3145e942008-12-05 18:58:34 -08003762 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003763 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003764 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003765 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003766 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003767
Ingo Molnardebccb32009-01-28 15:20:18 +01003768 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3769 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003770
Eric W. Biedermanec683072006-11-08 17:44:57 -08003771 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003772
Eric W. Biedermanec683072006-11-08 17:44:57 -08003773 msg.address_lo =
3774 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003775 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003776 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003777 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003778 HT_IRQ_LOW_DM_PHYSICAL :
3779 HT_IRQ_LOW_DM_LOGICAL) |
3780 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003781 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003782 HT_IRQ_LOW_MT_FIXED :
3783 HT_IRQ_LOW_MT_ARBITRATED) |
3784 HT_IRQ_LOW_IRQ_MASKED;
3785
Eric W. Biedermanec683072006-11-08 17:44:57 -08003786 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003787
Ingo Molnara460e742006-10-17 00:10:03 -07003788 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3789 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003790
3791 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003792 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003793 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003794}
3795#endif /* CONFIG_HT_IRQ */
3796
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003797int __init io_apic_get_redir_entries (int ioapic)
3798{
3799 union IO_APIC_reg_01 reg_01;
3800 unsigned long flags;
3801
3802 spin_lock_irqsave(&ioapic_lock, flags);
3803 reg_01.raw = io_apic_read(ioapic, 1);
3804 spin_unlock_irqrestore(&ioapic_lock, flags);
3805
3806 return reg_01.bits.entries;
3807}
3808
Yinghai Lube5d5352008-12-05 18:58:33 -08003809void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003810{
Yinghai Lube5d5352008-12-05 18:58:33 -08003811 int nr = 0;
3812
Yinghai Lucc6c5002009-02-08 16:18:03 -08003813 nr = acpi_probe_gsi();
3814 if (nr > nr_irqs_gsi) {
Yinghai Lube5d5352008-12-05 18:58:33 -08003815 nr_irqs_gsi = nr;
Yinghai Lucc6c5002009-02-08 16:18:03 -08003816 } else {
3817 /* for acpi=off or acpi is not compiled in */
3818 int idx;
3819
3820 nr = 0;
3821 for (idx = 0; idx < nr_ioapics; idx++)
3822 nr += io_apic_get_redir_entries(idx) + 1;
3823
3824 if (nr > nr_irqs_gsi)
3825 nr_irqs_gsi = nr;
3826 }
3827
3828 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003829}
3830
Yinghai Lu4a046d12009-01-12 17:39:24 -08003831#ifdef CONFIG_SPARSE_IRQ
3832int __init arch_probe_nr_irqs(void)
3833{
3834 int nr;
3835
Yinghai Luf1ee5542009-02-08 16:18:03 -08003836 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3837 nr_irqs = NR_VECTORS * nr_cpu_ids;
Yinghai Lu4a046d12009-01-12 17:39:24 -08003838
Yinghai Luf1ee5542009-02-08 16:18:03 -08003839 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3840#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3841 /*
3842 * for MSI and HT dyn irq
3843 */
Yinghai Lu9959c882009-12-28 21:08:29 -08003844 nr += nr_irqs_gsi * 64;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003845#endif
3846 if (nr < nr_irqs)
Yinghai Lu4a046d12009-01-12 17:39:24 -08003847 nr_irqs = nr;
3848
3849 return 0;
3850}
3851#endif
3852
Yinghai Lue5198072009-05-15 13:05:16 -07003853static int __io_apic_set_pci_routing(struct device *dev, int irq,
3854 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003855{
3856 struct irq_desc *desc;
3857 struct irq_cfg *cfg;
3858 int node;
Yinghai Lue5198072009-05-15 13:05:16 -07003859 int ioapic, pin;
3860 int trigger, polarity;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003861
Yinghai Lue5198072009-05-15 13:05:16 -07003862 ioapic = irq_attr->ioapic;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003863 if (!IO_APIC_IRQ(irq)) {
3864 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3865 ioapic);
3866 return -EINVAL;
3867 }
3868
3869 if (dev)
3870 node = dev_to_node(dev);
3871 else
3872 node = cpu_to_node(boot_cpu_id);
3873
3874 desc = irq_to_desc_alloc_node(irq, node);
3875 if (!desc) {
3876 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3877 return 0;
3878 }
3879
Yinghai Lue5198072009-05-15 13:05:16 -07003880 pin = irq_attr->ioapic_pin;
3881 trigger = irq_attr->trigger;
3882 polarity = irq_attr->polarity;
3883
Yinghai Lu5ef21832009-05-06 10:08:50 -07003884 /*
3885 * IRQs < 16 are already in the irq_2_pin[] map
3886 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02003887 if (irq >= nr_legacy_irqs) {
Yinghai Lu5ef21832009-05-06 10:08:50 -07003888 cfg = desc->chip_data;
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +04003889 if (add_pin_to_irq_node_nopanic(cfg, node, ioapic, pin)) {
3890 printk(KERN_INFO "can not add pin %d for irq %d\n",
3891 pin, irq);
3892 return 0;
3893 }
Yinghai Lu5ef21832009-05-06 10:08:50 -07003894 }
3895
Yinghai Lue5198072009-05-15 13:05:16 -07003896 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003897
3898 return 0;
3899}
3900
Yinghai Lue5198072009-05-15 13:05:16 -07003901int io_apic_set_pci_routing(struct device *dev, int irq,
3902 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003903{
Yinghai Lue5198072009-05-15 13:05:16 -07003904 int ioapic, pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003905 /*
3906 * Avoid pin reprogramming. PRTs typically include entries
3907 * with redundant pin->gsi mappings (but unique PCI devices);
3908 * we only program the IOAPIC on the first.
3909 */
Yinghai Lue5198072009-05-15 13:05:16 -07003910 ioapic = irq_attr->ioapic;
3911 pin = irq_attr->ioapic_pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003912 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3913 pr_debug("Pin %d-%d already programmed\n",
3914 mp_ioapics[ioapic].apicid, pin);
3915 return 0;
3916 }
3917 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3918
Yinghai Lue5198072009-05-15 13:05:16 -07003919 return __io_apic_set_pci_routing(dev, irq, irq_attr);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003920}
3921
Feng Tang2a4ab642009-07-07 23:01:15 -04003922u8 __init io_apic_unique_id(u8 id)
3923{
3924#ifdef CONFIG_X86_32
3925 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3926 !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3927 return io_apic_get_unique_id(nr_ioapics, id);
3928 else
3929 return id;
3930#else
3931 int i;
3932 DECLARE_BITMAP(used, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Feng Tang2a4ab642009-07-07 23:01:15 -04003934 bitmap_zero(used, 256);
3935 for (i = 0; i < nr_ioapics; i++) {
3936 struct mpc_ioapic *ia = &mp_ioapics[i];
3937 __set_bit(ia->apicid, used);
3938 }
3939 if (!test_bit(id, used))
3940 return id;
3941 return find_first_zero_bit(used, 256);
3942#endif
3943}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Ingo Molnar54168ed2008-08-20 09:07:45 +02003945#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003946int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947{
3948 union IO_APIC_reg_00 reg_00;
3949 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3950 physid_mask_t tmp;
3951 unsigned long flags;
3952 int i = 0;
3953
3954 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003955 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3956 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003958 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3960 * advantage of new APIC bus architecture.
3961 */
3962
3963 if (physids_empty(apic_id_map))
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03003964 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966 spin_lock_irqsave(&ioapic_lock, flags);
3967 reg_00.raw = io_apic_read(ioapic, 0);
3968 spin_unlock_irqrestore(&ioapic_lock, flags);
3969
3970 if (apic_id >= get_physical_broadcast()) {
3971 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3972 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3973 apic_id = reg_00.bits.ID;
3974 }
3975
3976 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003977 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 * 'stuck on smp_invalidate_needed IPI wait' messages.
3979 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03003980 if (apic->check_apicid_used(&apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 for (i = 0; i < get_physical_broadcast(); i++) {
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03003983 if (!apic->check_apicid_used(&apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 break;
3985 }
3986
3987 if (i == get_physical_broadcast())
3988 panic("Max apic_id exceeded!\n");
3989
3990 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3991 "trying %d\n", ioapic, apic_id, i);
3992
3993 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03003996 apic->apicid_to_cpu_present(apic_id, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 physids_or(apic_id_map, apic_id_map, tmp);
3998
3999 if (reg_00.bits.ID != apic_id) {
4000 reg_00.bits.ID = apic_id;
4001
4002 spin_lock_irqsave(&ioapic_lock, flags);
4003 io_apic_write(ioapic, 0, reg_00.raw);
4004 reg_00.raw = io_apic_read(ioapic, 0);
4005 spin_unlock_irqrestore(&ioapic_lock, flags);
4006
4007 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01004008 if (reg_00.bits.ID != apic_id) {
4009 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
4010 return -1;
4011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 }
4013
4014 apic_printk(APIC_VERBOSE, KERN_INFO
4015 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
4016
4017 return apic_id;
4018}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00004019#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02004021int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
4023 union IO_APIC_reg_01 reg_01;
4024 unsigned long flags;
4025
4026 spin_lock_irqsave(&ioapic_lock, flags);
4027 reg_01.raw = io_apic_read(ioapic, 1);
4028 spin_unlock_irqrestore(&ioapic_lock, flags);
4029
4030 return reg_01.bits.version;
4031}
4032
Shaohua Li61fd47e2007-11-17 01:05:28 -05004033int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
4034{
4035 int i;
4036
4037 if (skip_ioapic_setup)
4038 return -1;
4039
4040 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05304041 if (mp_irqs[i].irqtype == mp_INT &&
4042 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05004043 break;
4044 if (i >= mp_irq_entries)
4045 return -1;
4046
4047 *trigger = irq_trigger(i);
4048 *polarity = irq_polarity(i);
4049 return 0;
4050}
4051
Yinghai Lu497c9a12008-08-19 20:50:28 -07004052/*
4053 * This function currently is only a helper for the i386 smp boot process where
4054 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01004055 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07004056 */
4057#ifdef CONFIG_SMP
4058void __init setup_ioapic_dest(void)
4059{
Yinghai Lub9c61b702009-05-06 10:10:06 -07004060 int pin, ioapic = 0, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004061 struct irq_desc *desc;
Mike Travis22f65d32008-12-16 17:33:56 -08004062 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004063
4064 if (skip_ioapic_setup == 1)
4065 return;
4066
Yinghai Lub9c61b702009-05-06 10:10:06 -07004067#ifdef CONFIG_ACPI
4068 if (!acpi_disabled && acpi_ioapic) {
4069 ioapic = mp_find_ioapic(0);
4070 if (ioapic < 0)
4071 ioapic = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004072 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07004073#endif
4074
4075 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4076 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4077 if (irq_entry == -1)
4078 continue;
4079 irq = pin_2_irq(irq_entry, ioapic, pin);
4080
4081 desc = irq_to_desc(irq);
4082
4083 /*
4084 * Honour affinities which have been set in early boot
4085 */
4086 if (desc->status &
4087 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4088 mask = desc->affinity;
4089 else
4090 mask = apic->target_cpus();
4091
4092 if (intr_remapping_enabled)
4093 set_ir_ioapic_affinity_irq_desc(desc, mask);
4094 else
4095 set_ioapic_affinity_irq_desc(desc, mask);
4096 }
4097
Yinghai Lu497c9a12008-08-19 20:50:28 -07004098}
4099#endif
4100
Ingo Molnar54168ed2008-08-20 09:07:45 +02004101#define IOAPIC_RESOURCE_NAME_SIZE 11
4102
4103static struct resource *ioapic_resources;
4104
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004105static struct resource * __init ioapic_setup_resources(int nr_ioapics)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004106{
4107 unsigned long n;
4108 struct resource *res;
4109 char *mem;
4110 int i;
4111
4112 if (nr_ioapics <= 0)
4113 return NULL;
4114
4115 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4116 n *= nr_ioapics;
4117
4118 mem = alloc_bootmem(n);
4119 res = (void *)mem;
4120
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004121 mem += sizeof(struct resource) * nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004122
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004123 for (i = 0; i < nr_ioapics; i++) {
4124 res[i].name = mem;
4125 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Cyrill Gorcunov4343fe12009-11-08 18:54:31 +03004126 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004127 mem += IOAPIC_RESOURCE_NAME_SIZE;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004128 }
4129
4130 ioapic_resources = res;
4131
4132 return res;
4133}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004134
Yinghai Luf3294a32008-06-27 01:41:56 -07004135void __init ioapic_init_mappings(void)
4136{
4137 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004138 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004139 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004140
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004141 ioapic_res = ioapic_setup_resources(nr_ioapics);
Yinghai Luf3294a32008-06-27 01:41:56 -07004142 for (i = 0; i < nr_ioapics; i++) {
4143 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05304144 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004145#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004146 if (!ioapic_phys) {
4147 printk(KERN_ERR
4148 "WARNING: bogus zero IO-APIC "
4149 "address found in MPTABLE, "
4150 "disabling IO/APIC support!\n");
4151 smp_found_config = 0;
4152 skip_ioapic_setup = 1;
4153 goto fake_ioapic_page;
4154 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004155#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004156 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004157#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004158fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004159#endif
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03004160 ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004161 ioapic_phys = __pa(ioapic_phys);
4162 }
4163 set_fixmap_nocache(idx, ioapic_phys);
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03004164 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
4165 __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
4166 ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004167 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004168
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004169 ioapic_res->start = ioapic_phys;
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03004170 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04004171 ioapic_res++;
Yinghai Luf3294a32008-06-27 01:41:56 -07004172 }
4173}
4174
Yinghai Lu857fdc52009-07-10 09:36:20 -07004175void __init ioapic_insert_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004176{
4177 int i;
4178 struct resource *r = ioapic_resources;
4179
4180 if (!r) {
Yinghai Lu857fdc52009-07-10 09:36:20 -07004181 if (nr_ioapics > 0)
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01004182 printk(KERN_ERR
4183 "IO APIC resources couldn't be allocated.\n");
Yinghai Lu857fdc52009-07-10 09:36:20 -07004184 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004185 }
4186
4187 for (i = 0; i < nr_ioapics; i++) {
4188 insert_resource(&iomem_resource, r);
4189 r++;
4190 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004191}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004192
Feng Tang2a4ab642009-07-07 23:01:15 -04004193int mp_find_ioapic(int gsi)
4194{
4195 int i = 0;
4196
4197 /* Find the IOAPIC that manages this GSI. */
4198 for (i = 0; i < nr_ioapics; i++) {
4199 if ((gsi >= mp_gsi_routing[i].gsi_base)
4200 && (gsi <= mp_gsi_routing[i].gsi_end))
4201 return i;
4202 }
4203
4204 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
4205 return -1;
4206}
4207
4208int mp_find_ioapic_pin(int ioapic, int gsi)
4209{
4210 if (WARN_ON(ioapic == -1))
4211 return -1;
4212 if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
4213 return -1;
4214
4215 return gsi - mp_gsi_routing[ioapic].gsi_base;
4216}
4217
4218static int bad_ioapic(unsigned long address)
4219{
4220 if (nr_ioapics >= MAX_IO_APICS) {
4221 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
4222 "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
4223 return 1;
4224 }
4225 if (!address) {
4226 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
4227 " found in table, skipping!\n");
4228 return 1;
4229 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004230 return 0;
4231}
4232
Feng Tang2a4ab642009-07-07 23:01:15 -04004233void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
4234{
4235 int idx = 0;
4236
4237 if (bad_ioapic(address))
4238 return;
4239
4240 idx = nr_ioapics;
4241
4242 mp_ioapics[idx].type = MP_IOAPIC;
4243 mp_ioapics[idx].flags = MPC_APIC_USABLE;
4244 mp_ioapics[idx].apicaddr = address;
4245
4246 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4247 mp_ioapics[idx].apicid = io_apic_unique_id(id);
4248 mp_ioapics[idx].apicver = io_apic_get_version(idx);
4249
4250 /*
4251 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4252 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4253 */
4254 mp_gsi_routing[idx].gsi_base = gsi_base;
4255 mp_gsi_routing[idx].gsi_end = gsi_base +
4256 io_apic_get_redir_entries(idx);
4257
4258 printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4259 "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4260 mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4261 mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);
4262
4263 nr_ioapics++;
4264}