blob: ffd8fdfcbe4c4392b64f43342d38d54676e1c9c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053049#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070051#include <asm/proto.h>
52#include <asm/acpi.h>
53#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070055#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020056#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070057#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070058#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070059#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070060#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070061#include <asm/hpet.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053062#include <asm/hw_irq.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050063#include <asm/uv/uv_hub.h>
64#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ingo Molnar7b6aa332009-02-17 13:58:15 +010066#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +040069#define for_each_irq_pin(entry, head) \
70 for (entry = head; entry; entry = entry->next)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020073 * Is the SiS APIC rmw bug present ?
74 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76int sis_apic_bug = -1;
77
Yinghai Luefa25592008-08-19 20:50:36 -070078static DEFINE_SPINLOCK(ioapic_lock);
79static DEFINE_SPINLOCK(vector_lock);
80
Yinghai Luefa25592008-08-19 20:50:36 -070081/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * # of IRQ routing registers
83 */
84int nr_ioapic_registers[MAX_IO_APICS];
85
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +053087struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040088int nr_ioapics;
89
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040090/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053091struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040092
93/* # of MP IRQ source entries */
94int mp_irq_entries;
95
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040096#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
97int mp_bus_id_to_type[MAX_MP_BUSSES];
98#endif
99
100DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
101
Yinghai Luefa25592008-08-19 20:50:36 -0700102int skip_ioapic_setup;
103
Ingo Molnar65a4e572009-01-31 03:36:17 +0100104void arch_disable_smp_support(void)
105{
106#ifdef CONFIG_PCI
107 noioapicquirk = 1;
108 noioapicreroute = -1;
109#endif
110 skip_ioapic_setup = 1;
111}
112
Ingo Molnar54168ed2008-08-20 09:07:45 +0200113static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700114{
115 /* disable IO-APIC */
Ingo Molnar65a4e572009-01-31 03:36:17 +0100116 arch_disable_smp_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700117 return 0;
118}
119early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200120
Yinghai Lu0f978f42008-08-19 20:50:26 -0700121struct irq_pin_list {
122 int apic, pin;
123 struct irq_pin_list *next;
124};
Yinghai Lu301e6192008-08-19 20:50:02 -0700125
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700126static struct irq_pin_list *get_one_free_irq_2_pin(int node)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700127{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800128 struct irq_pin_list *pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700129
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800130 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700131
Yinghai Lu0f978f42008-08-19 20:50:26 -0700132 return pin;
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jeremy Fitzhardinge8e13d692009-06-08 03:14:59 -0700135/*
136 * This is performance-critical, we want to do it O(1)
137 *
138 * Most irqs are mapped 1:1 with pins.
139 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800140struct irq_cfg {
141 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800142 cpumask_var_t domain;
143 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800144 unsigned move_cleanup_count;
145 u8 vector;
146 u8 move_in_progress : 1;
147};
148
149/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
150#ifdef CONFIG_SPARSE_IRQ
151static struct irq_cfg irq_cfgx[] = {
152#else
153static struct irq_cfg irq_cfgx[NR_IRQS] = {
154#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800155 [0] = { .vector = IRQ0_VECTOR, },
156 [1] = { .vector = IRQ1_VECTOR, },
157 [2] = { .vector = IRQ2_VECTOR, },
158 [3] = { .vector = IRQ3_VECTOR, },
159 [4] = { .vector = IRQ4_VECTOR, },
160 [5] = { .vector = IRQ5_VECTOR, },
161 [6] = { .vector = IRQ6_VECTOR, },
162 [7] = { .vector = IRQ7_VECTOR, },
163 [8] = { .vector = IRQ8_VECTOR, },
164 [9] = { .vector = IRQ9_VECTOR, },
165 [10] = { .vector = IRQ10_VECTOR, },
166 [11] = { .vector = IRQ11_VECTOR, },
167 [12] = { .vector = IRQ12_VECTOR, },
168 [13] = { .vector = IRQ13_VECTOR, },
169 [14] = { .vector = IRQ14_VECTOR, },
170 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800171};
172
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800173int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800174{
175 struct irq_cfg *cfg;
176 struct irq_desc *desc;
177 int count;
Yinghai Ludad213ae2009-05-28 18:14:40 -0700178 int node;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800179 int i;
180
181 cfg = irq_cfgx;
182 count = ARRAY_SIZE(irq_cfgx);
Yinghai Ludad213ae2009-05-28 18:14:40 -0700183 node= cpu_to_node(boot_cpu_id);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800184
185 for (i = 0; i < count; i++) {
186 desc = irq_to_desc(i);
187 desc->chip_data = &cfg[i];
Yinghai Lu12274e92009-06-11 15:07:48 -0700188 zalloc_cpumask_var_node(&cfg[i].domain, GFP_NOWAIT, node);
189 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_NOWAIT, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800190 if (i < NR_IRQS_LEGACY)
191 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800192 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800193
194 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800195}
196
197#ifdef CONFIG_SPARSE_IRQ
198static struct irq_cfg *irq_cfg(unsigned int irq)
199{
200 struct irq_cfg *cfg = NULL;
201 struct irq_desc *desc;
202
203 desc = irq_to_desc(irq);
204 if (desc)
205 cfg = desc->chip_data;
206
207 return cfg;
208}
209
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700210static struct irq_cfg *get_one_free_irq_cfg(int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800211{
212 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800213
214 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800215 if (cfg) {
Mike Travis80855f72008-12-31 18:08:47 -0800216 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800217 kfree(cfg);
218 cfg = NULL;
Mike Travis80855f72008-12-31 18:08:47 -0800219 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
220 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800221 free_cpumask_var(cfg->domain);
222 kfree(cfg);
223 cfg = NULL;
224 } else {
225 cpumask_clear(cfg->domain);
226 cpumask_clear(cfg->old_domain);
227 }
228 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800229
230 return cfg;
231}
232
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700233int arch_init_chip_data(struct irq_desc *desc, int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800234{
235 struct irq_cfg *cfg;
236
237 cfg = desc->chip_data;
238 if (!cfg) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700239 desc->chip_data = get_one_free_irq_cfg(node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800240 if (!desc->chip_data) {
241 printk(KERN_ERR "can not alloc irq_cfg\n");
242 BUG_ON(1);
243 }
244 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800245
246 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800247}
248
Yinghai Lufcef5912009-04-27 17:58:23 -0700249/* for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800250static void
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700251init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800252{
253 struct irq_pin_list *old_entry, *head, *tail, *entry;
254
255 cfg->irq_2_pin = NULL;
256 old_entry = old_cfg->irq_2_pin;
257 if (!old_entry)
258 return;
259
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700260 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800261 if (!entry)
262 return;
263
264 entry->apic = old_entry->apic;
265 entry->pin = old_entry->pin;
266 head = entry;
267 tail = entry;
268 old_entry = old_entry->next;
269 while (old_entry) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700270 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800271 if (!entry) {
272 entry = head;
273 while (entry) {
274 head = entry->next;
275 kfree(entry);
276 entry = head;
277 }
278 /* still use the old one */
279 return;
280 }
281 entry->apic = old_entry->apic;
282 entry->pin = old_entry->pin;
283 tail->next = entry;
284 tail = entry;
285 old_entry = old_entry->next;
286 }
287
288 tail->next = NULL;
289 cfg->irq_2_pin = head;
290}
291
292static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
293{
294 struct irq_pin_list *entry, *next;
295
296 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
297 return;
298
299 entry = old_cfg->irq_2_pin;
300
301 while (entry) {
302 next = entry->next;
303 kfree(entry);
304 entry = next;
305 }
306 old_cfg->irq_2_pin = NULL;
307}
308
309void arch_init_copy_chip_data(struct irq_desc *old_desc,
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700310 struct irq_desc *desc, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800311{
312 struct irq_cfg *cfg;
313 struct irq_cfg *old_cfg;
314
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700315 cfg = get_one_free_irq_cfg(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800316
317 if (!cfg)
318 return;
319
320 desc->chip_data = cfg;
321
322 old_cfg = old_desc->chip_data;
323
324 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
325
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700326 init_copy_irq_2_pin(old_cfg, cfg, node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800327}
328
329static void free_irq_cfg(struct irq_cfg *old_cfg)
330{
331 kfree(old_cfg);
332}
333
334void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
335{
336 struct irq_cfg *old_cfg, *cfg;
337
338 old_cfg = old_desc->chip_data;
339 cfg = desc->chip_data;
340
341 if (old_cfg == cfg)
342 return;
343
344 if (old_cfg) {
345 free_irq_2_pin(old_cfg, cfg);
346 free_irq_cfg(old_cfg);
347 old_desc->chip_data = NULL;
348 }
349}
Yinghai Lufcef5912009-04-27 17:58:23 -0700350/* end for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800351
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800352#else
353static struct irq_cfg *irq_cfg(unsigned int irq)
354{
355 return irq < nr_irqs ? irq_cfgx + irq : NULL;
356}
357
358#endif
359
Linus Torvalds130fe052006-11-01 09:11:00 -0800360struct io_apic {
361 unsigned int index;
362 unsigned int unused[3];
363 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700364 unsigned int unused2[11];
365 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800366};
367
368static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
369{
370 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +0530371 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800372}
373
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700374static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
375{
376 struct io_apic __iomem *io_apic = io_apic_base(apic);
377 writel(vector, &io_apic->eoi);
378}
379
Linus Torvalds130fe052006-11-01 09:11:00 -0800380static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
381{
382 struct io_apic __iomem *io_apic = io_apic_base(apic);
383 writel(reg, &io_apic->index);
384 return readl(&io_apic->data);
385}
386
387static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
388{
389 struct io_apic __iomem *io_apic = io_apic_base(apic);
390 writel(reg, &io_apic->index);
391 writel(value, &io_apic->data);
392}
393
394/*
395 * Re-write a value: to be used for read-modify-write
396 * cycles where the read already set up the index register.
397 *
398 * Older SiS APIC requires we rewrite the index register
399 */
400static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
401{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200402 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200403
404 if (sis_apic_bug)
405 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800406 writel(value, &io_apic->data);
407}
408
Yinghai Lu3145e942008-12-05 18:58:34 -0800409static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700410{
411 struct irq_pin_list *entry;
412 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700413
414 spin_lock_irqsave(&ioapic_lock, flags);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400415 for_each_irq_pin(entry, cfg->irq_2_pin) {
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700416 unsigned int reg;
417 int pin;
418
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700419 pin = entry->pin;
420 reg = io_apic_read(entry->apic, 0x10 + pin*2);
421 /* Is the remote IRR bit set? */
422 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
423 spin_unlock_irqrestore(&ioapic_lock, flags);
424 return true;
425 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700426 }
427 spin_unlock_irqrestore(&ioapic_lock, flags);
428
429 return false;
430}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700431
Andi Kleencf4c6a22006-09-26 10:52:30 +0200432union entry_union {
433 struct { u32 w1, w2; };
434 struct IO_APIC_route_entry entry;
435};
436
437static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
438{
439 union entry_union eu;
440 unsigned long flags;
441 spin_lock_irqsave(&ioapic_lock, flags);
442 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
443 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
444 spin_unlock_irqrestore(&ioapic_lock, flags);
445 return eu.entry;
446}
447
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800448/*
449 * When we write a new IO APIC routing entry, we need to write the high
450 * word first! If the mask bit in the low word is clear, we will enable
451 * the interrupt, and we need to make sure the entry is fully populated
452 * before that happens.
453 */
Andi Kleend15512f2006-12-07 02:14:07 +0100454static void
455__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
456{
Figo.zhang50a8d4d2009-06-17 22:25:20 +0800457 union entry_union eu = {{0, 0}};
458
Andi Kleend15512f2006-12-07 02:14:07 +0100459 eu.entry = e;
460 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
461 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
462}
463
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -0800464void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200465{
466 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200467 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100468 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800469 spin_unlock_irqrestore(&ioapic_lock, flags);
470}
471
472/*
473 * When we mask an IO APIC routing entry, we need to write the low
474 * word first, in order to set the mask bit before we change the
475 * high bits!
476 */
477static void ioapic_mask_entry(int apic, int pin)
478{
479 unsigned long flags;
480 union entry_union eu = { .entry.mask = 1 };
481
482 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200483 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
484 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
485 spin_unlock_irqrestore(&ioapic_lock, flags);
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
489 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
490 * shared ISA-space IRQs, so we have to support them. We are super
491 * fast in the common case, and fast for shared ISA-space IRQs.
492 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700493static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400495 struct irq_pin_list **last, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400497 /* don't allow duplicates */
498 last = &cfg->irq_2_pin;
499 for_each_irq_pin(entry, cfg->irq_2_pin) {
Yinghai Lu0f978f42008-08-19 20:50:26 -0700500 if (entry->apic == apic && entry->pin == pin)
501 return;
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400502 last = &entry->next;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700503 }
504
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700505 entry = get_one_free_irq_2_pin(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 entry->apic = apic;
507 entry->pin = pin;
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700508
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400509 *last = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/*
513 * Reroute an IRQ to a different pin.
514 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700515static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700516 int oldapic, int oldpin,
517 int newapic, int newpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700519 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400521 for_each_irq_pin(entry, cfg->irq_2_pin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (entry->apic == oldapic && entry->pin == oldpin) {
523 entry->apic = newapic;
524 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700525 /* every one is different, right? */
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700526 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700529
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700530 /* old apic/pin didn't exist, so just add new ones */
531 add_pin_to_irq_node(cfg, node, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700534static void io_apic_modify_irq(struct irq_cfg *cfg,
535 int mask_and, int mask_or,
536 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400537{
538 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400539 struct irq_pin_list *entry;
540
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400541 for_each_irq_pin(entry, cfg->irq_2_pin) {
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400542 unsigned int reg;
543 pin = entry->pin;
544 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
545 reg &= mask_and;
546 reg |= mask_or;
547 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
548 if (final)
549 final(entry);
550 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700551}
552
Yinghai Lu3145e942008-12-05 18:58:34 -0800553static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400554{
Yinghai Lu3145e942008-12-05 18:58:34 -0800555 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400556}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700557
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530558static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700559{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400560 /*
561 * Synchronize the IO-APIC and the CPU by doing
562 * a dummy read from the IO-APIC
563 */
564 struct io_apic __iomem *io_apic;
565 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700566 readl(&io_apic->data);
567}
568
Yinghai Lu3145e942008-12-05 18:58:34 -0800569static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400570{
Yinghai Lu3145e942008-12-05 18:58:34 -0800571 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400572}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700573
Yinghai Lu3145e942008-12-05 18:58:34 -0800574static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400575{
Yinghai Lu3145e942008-12-05 18:58:34 -0800576 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400577 IO_APIC_REDIR_MASKED, NULL);
578}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700579
Yinghai Lu3145e942008-12-05 18:58:34 -0800580static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400581{
Yinghai Lu3145e942008-12-05 18:58:34 -0800582 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400583 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
584}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700585
Yinghai Lu3145e942008-12-05 18:58:34 -0800586static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Yinghai Lu3145e942008-12-05 18:58:34 -0800588 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 unsigned long flags;
590
Yinghai Lu3145e942008-12-05 18:58:34 -0800591 BUG_ON(!cfg);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800594 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_unlock_irqrestore(&ioapic_lock, flags);
596}
597
Yinghai Lu3145e942008-12-05 18:58:34 -0800598static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Yinghai Lu3145e942008-12-05 18:58:34 -0800600 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 unsigned long flags;
602
603 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800604 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 spin_unlock_irqrestore(&ioapic_lock, flags);
606}
607
Yinghai Lu3145e942008-12-05 18:58:34 -0800608static void mask_IO_APIC_irq(unsigned int irq)
609{
610 struct irq_desc *desc = irq_to_desc(irq);
611
612 mask_IO_APIC_irq_desc(desc);
613}
614static void unmask_IO_APIC_irq(unsigned int irq)
615{
616 struct irq_desc *desc = irq_to_desc(irq);
617
618 unmask_IO_APIC_irq_desc(desc);
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
622{
623 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200626 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (entry.delivery_mode == dest_SMI)
628 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Disable it in the IO-APIC irq-routing table:
631 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800632 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Ingo Molnar54168ed2008-08-20 09:07:45 +0200635static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 int apic, pin;
638
639 for (apic = 0; apic < nr_ioapics; apic++)
640 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
641 clear_IO_APIC_pin(apic, pin);
642}
643
Ingo Molnar54168ed2008-08-20 09:07:45 +0200644#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/*
646 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
647 * specific CPU-side IRQs.
648 */
649
650#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800651static int pirq_entries[MAX_PIRQS] = {
652 [0 ... MAX_PIRQS - 1] = -1
653};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static int __init ioapic_pirq_setup(char *str)
656{
657 int i, max;
658 int ints[MAX_PIRQS+1];
659
660 get_options(str, ARRAY_SIZE(ints), ints);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 apic_printk(APIC_VERBOSE, KERN_INFO
663 "PIRQ redirection, working around broken MP-BIOS.\n");
664 max = MAX_PIRQS;
665 if (ints[0] < MAX_PIRQS)
666 max = ints[0];
667
668 for (i = 0; i < max; i++) {
669 apic_printk(APIC_VERBOSE, KERN_DEBUG
670 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
671 /*
672 * PIRQs are mapped upside down, usually.
673 */
674 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
675 }
676 return 1;
677}
678
679__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200680#endif /* CONFIG_X86_32 */
681
Fenghua Yub24696b2009-03-27 14:22:44 -0700682struct IO_APIC_route_entry **alloc_ioapic_entries(void)
683{
684 int apic;
685 struct IO_APIC_route_entry **ioapic_entries;
686
687 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
688 GFP_ATOMIC);
689 if (!ioapic_entries)
690 return 0;
691
692 for (apic = 0; apic < nr_ioapics; apic++) {
693 ioapic_entries[apic] =
694 kzalloc(sizeof(struct IO_APIC_route_entry) *
695 nr_ioapic_registers[apic], GFP_ATOMIC);
696 if (!ioapic_entries[apic])
697 goto nomem;
698 }
699
700 return ioapic_entries;
701
702nomem:
703 while (--apic >= 0)
704 kfree(ioapic_entries[apic]);
705 kfree(ioapic_entries);
706
707 return 0;
708}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200709
710/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700711 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200712 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700713int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200714{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200715 int apic, pin;
716
Fenghua Yub24696b2009-03-27 14:22:44 -0700717 if (!ioapic_entries)
718 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200719
720 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700721 if (!ioapic_entries[apic])
722 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200723
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700724 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
Fenghua Yub24696b2009-03-27 14:22:44 -0700725 ioapic_entries[apic][pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200726 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700727 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400728
Ingo Molnar54168ed2008-08-20 09:07:45 +0200729 return 0;
730}
731
Fenghua Yub24696b2009-03-27 14:22:44 -0700732/*
733 * Mask all IO APIC entries.
734 */
735void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700736{
737 int apic, pin;
738
Fenghua Yub24696b2009-03-27 14:22:44 -0700739 if (!ioapic_entries)
740 return;
741
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700742 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700743 if (!ioapic_entries[apic])
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700744 break;
Fenghua Yub24696b2009-03-27 14:22:44 -0700745
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700746 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
747 struct IO_APIC_route_entry entry;
748
Fenghua Yub24696b2009-03-27 14:22:44 -0700749 entry = ioapic_entries[apic][pin];
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700750 if (!entry.mask) {
751 entry.mask = 1;
752 ioapic_write_entry(apic, pin, entry);
753 }
754 }
755 }
756}
757
Fenghua Yub24696b2009-03-27 14:22:44 -0700758/*
759 * Restore IO APIC entries which was saved in ioapic_entries.
760 */
761int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200762{
763 int apic, pin;
764
Fenghua Yub24696b2009-03-27 14:22:44 -0700765 if (!ioapic_entries)
766 return -ENOMEM;
767
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400768 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700769 if (!ioapic_entries[apic])
770 return -ENOMEM;
771
Ingo Molnar54168ed2008-08-20 09:07:45 +0200772 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
773 ioapic_write_entry(apic, pin,
Fenghua Yub24696b2009-03-27 14:22:44 -0700774 ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400775 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700776 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200777}
778
Fenghua Yub24696b2009-03-27 14:22:44 -0700779void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
780{
781 int apic;
782
783 for (apic = 0; apic < nr_ioapics; apic++)
784 kfree(ioapic_entries[apic]);
785
786 kfree(ioapic_entries);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200787}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789/*
790 * Find the IRQ entry number of a certain pin.
791 */
792static int find_irq_entry(int apic, int pin, int type)
793{
794 int i;
795
796 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530797 if (mp_irqs[i].irqtype == type &&
798 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
799 mp_irqs[i].dstapic == MP_APIC_ALL) &&
800 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return i;
802
803 return -1;
804}
805
806/*
807 * Find the pin to which IRQ[irq] (ISA) is connected
808 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800809static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 int i;
812
813 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530814 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300816 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530817 (mp_irqs[i].irqtype == type) &&
818 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530820 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 return -1;
823}
824
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800825static int __init find_isa_irq_apic(int irq, int type)
826{
827 int i;
828
829 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530830 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800831
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300832 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530833 (mp_irqs[i].irqtype == type) &&
834 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800835 break;
836 }
837 if (i < mp_irq_entries) {
838 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200839 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530840 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800841 return apic;
842 }
843 }
844
845 return -1;
846}
847
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300848#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/*
850 * EISA Edge/Level control register, ELCR
851 */
852static int EISA_ELCR(unsigned int irq)
853{
Yinghai Lu99d093d2008-12-05 18:58:32 -0800854 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned int port = 0x4d0 + (irq >> 3);
856 return (inb(port) >> (irq & 7)) & 1;
857 }
858 apic_printk(APIC_VERBOSE, KERN_INFO
859 "Broken MPtable reports ISA irq %d\n", irq);
860 return 0;
861}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200862
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300865/* ISA interrupts are always polarity zero edge triggered,
866 * when listed as conforming in the MP table. */
867
868#define default_ISA_trigger(idx) (0)
869#define default_ISA_polarity(idx) (0)
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/* EISA interrupts are always polarity zero and can be edge or level
872 * trigger depending on the ELCR value. If an interrupt is listed as
873 * EISA conforming in the MP table, that means its trigger type must
874 * be read in from the ELCR */
875
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530876#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300877#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879/* PCI interrupts are always polarity one level triggered,
880 * when listed as conforming in the MP table. */
881
882#define default_PCI_trigger(idx) (1)
883#define default_PCI_polarity(idx) (1)
884
885/* MCA interrupts are always polarity zero level triggered,
886 * when listed as conforming in the MP table. */
887
888#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300889#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Shaohua Li61fd47e2007-11-17 01:05:28 -0500891static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530893 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int polarity;
895
896 /*
897 * Determine IRQ line polarity (high active or low active):
898 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530899 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200901 case 0: /* conforms, ie. bus-type dependent polarity */
902 if (test_bit(bus, mp_bus_not_pci))
903 polarity = default_ISA_polarity(idx);
904 else
905 polarity = default_PCI_polarity(idx);
906 break;
907 case 1: /* high active */
908 {
909 polarity = 0;
910 break;
911 }
912 case 2: /* reserved */
913 {
914 printk(KERN_WARNING "broken BIOS!!\n");
915 polarity = 1;
916 break;
917 }
918 case 3: /* low active */
919 {
920 polarity = 1;
921 break;
922 }
923 default: /* invalid */
924 {
925 printk(KERN_WARNING "broken BIOS!!\n");
926 polarity = 1;
927 break;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 return polarity;
931}
932
933static int MPBIOS_trigger(int idx)
934{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530935 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int trigger;
937
938 /*
939 * Determine IRQ trigger mode (edge or level sensitive):
940 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530941 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200943 case 0: /* conforms, ie. bus-type dependent */
944 if (test_bit(bus, mp_bus_not_pci))
945 trigger = default_ISA_trigger(idx);
946 else
947 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300948#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200949 switch (mp_bus_id_to_type[bus]) {
950 case MP_BUS_ISA: /* ISA pin */
951 {
952 /* set before the switch */
953 break;
954 }
955 case MP_BUS_EISA: /* EISA pin */
956 {
957 trigger = default_EISA_trigger(idx);
958 break;
959 }
960 case MP_BUS_PCI: /* PCI pin */
961 {
962 /* set before the switch */
963 break;
964 }
965 case MP_BUS_MCA: /* MCA pin */
966 {
967 trigger = default_MCA_trigger(idx);
968 break;
969 }
970 default:
971 {
972 printk(KERN_WARNING "broken BIOS!!\n");
973 trigger = 1;
974 break;
975 }
976 }
977#endif
978 break;
979 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200980 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200981 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200982 break;
983 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200984 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200985 {
986 printk(KERN_WARNING "broken BIOS!!\n");
987 trigger = 1;
988 break;
989 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200990 case 3: /* level */
991 {
992 trigger = 1;
993 break;
994 }
995 default: /* invalid */
996 {
997 printk(KERN_WARNING "broken BIOS!!\n");
998 trigger = 0;
999 break;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 return trigger;
1003}
1004
1005static inline int irq_polarity(int idx)
1006{
1007 return MPBIOS_polarity(idx);
1008}
1009
1010static inline int irq_trigger(int idx)
1011{
1012 return MPBIOS_trigger(idx);
1013}
1014
Yinghai Luefa25592008-08-19 20:50:36 -07001015int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static int pin_2_irq(int idx, int apic, int pin)
1017{
1018 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301019 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /*
1022 * Debugging check, we are in big trouble if this message pops up!
1023 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301024 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1026
Ingo Molnar54168ed2008-08-20 09:07:45 +02001027 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301028 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001029 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001030 /*
1031 * PCI IRQs are mapped in order
1032 */
1033 i = irq = 0;
1034 while (i < apic)
1035 irq += nr_ioapic_registers[i++];
1036 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001037 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001038 * For MPS mode, so far only needed by ES7000 platform
1039 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001040 if (ioapic_renumber_irq)
1041 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Ingo Molnar54168ed2008-08-20 09:07:45 +02001044#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1047 */
1048 if ((pin >= 16) && (pin <= 23)) {
1049 if (pirq_entries[pin-16] != -1) {
1050 if (!pirq_entries[pin-16]) {
1051 apic_printk(APIC_VERBOSE, KERN_DEBUG
1052 "disabling PIRQ%d\n", pin-16);
1053 } else {
1054 irq = pirq_entries[pin-16];
1055 apic_printk(APIC_VERBOSE, KERN_DEBUG
1056 "using PIRQ%d -> IRQ %d\n",
1057 pin-16, irq);
1058 }
1059 }
1060 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001061#endif
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return irq;
1064}
1065
Yinghai Lue20c06f2009-05-06 10:08:22 -07001066/*
1067 * Find a specific PCI IRQ entry.
1068 * Not an __init, possibly needed by modules
1069 */
1070int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
Yinghai Lue5198072009-05-15 13:05:16 -07001071 struct io_apic_irq_attr *irq_attr)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001072{
1073 int apic, i, best_guess = -1;
1074
1075 apic_printk(APIC_DEBUG,
1076 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1077 bus, slot, pin);
1078 if (test_bit(bus, mp_bus_not_pci)) {
1079 apic_printk(APIC_VERBOSE,
1080 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1081 return -1;
1082 }
1083 for (i = 0; i < mp_irq_entries; i++) {
1084 int lbus = mp_irqs[i].srcbus;
1085
1086 for (apic = 0; apic < nr_ioapics; apic++)
1087 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1088 mp_irqs[i].dstapic == MP_APIC_ALL)
1089 break;
1090
1091 if (!test_bit(lbus, mp_bus_not_pci) &&
1092 !mp_irqs[i].irqtype &&
1093 (bus == lbus) &&
1094 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1095 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1096
1097 if (!(apic || IO_APIC_IRQ(irq)))
1098 continue;
1099
1100 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Yinghai Lue5198072009-05-15 13:05:16 -07001101 set_io_apic_irq_attr(irq_attr, apic,
1102 mp_irqs[i].dstirq,
1103 irq_trigger(i),
1104 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001105 return irq;
1106 }
1107 /*
1108 * Use the first all-but-pin matching entry as a
1109 * best-guess fuzzy result for broken mptables.
1110 */
1111 if (best_guess < 0) {
Yinghai Lue5198072009-05-15 13:05:16 -07001112 set_io_apic_irq_attr(irq_attr, apic,
1113 mp_irqs[i].dstirq,
1114 irq_trigger(i),
1115 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001116 best_guess = irq;
1117 }
1118 }
1119 }
1120 return best_guess;
1121}
1122EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1123
Yinghai Lu497c9a12008-08-19 20:50:28 -07001124void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001126 /* Used to the online set of cpus does not change
1127 * during assign_irq_vector.
1128 */
1129 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Yinghai Lu497c9a12008-08-19 20:50:28 -07001132void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001133{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001134 spin_unlock(&vector_lock);
1135}
1136
Mike Travise7986732008-12-16 17:33:52 -08001137static int
1138__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001139{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001140 /*
1141 * NOTE! The local APIC isn't very good at handling
1142 * multiple interrupts at the same interrupt level.
1143 * As the interrupt level is determined by taking the
1144 * vector number and shifting that right by 4, we
1145 * want to spread these out a bit so that they don't
1146 * all fall in the same interrupt level.
1147 *
1148 * Also, we've got to be careful not to trash gate
1149 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1150 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001151 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1152 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001153 int cpu, err;
1154 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001155
Ingo Molnar54168ed2008-08-20 09:07:45 +02001156 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1157 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001158
Mike Travis22f65d32008-12-16 17:33:56 -08001159 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1160 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001161
Ingo Molnar54168ed2008-08-20 09:07:45 +02001162 old_vector = cfg->vector;
1163 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001164 cpumask_and(tmp_mask, mask, cpu_online_mask);
1165 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1166 if (!cpumask_empty(tmp_mask)) {
1167 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001168 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001169 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001170 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001171
Mike Travise7986732008-12-16 17:33:52 -08001172 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001173 err = -ENOSPC;
1174 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001175 int new_cpu;
1176 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001177
Ingo Molnare2d40b12009-01-28 06:50:47 +01001178 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001179
Ingo Molnar54168ed2008-08-20 09:07:45 +02001180 vector = current_vector;
1181 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001182next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001183 vector += 8;
1184 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001185 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001186 offset = (offset + 1) % 8;
1187 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001188 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001189 if (unlikely(current_vector == vector))
1190 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001191
1192 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001193 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001194
Mike Travis22f65d32008-12-16 17:33:56 -08001195 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001196 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1197 goto next;
1198 /* Found one! */
1199 current_vector = vector;
1200 current_offset = offset;
1201 if (old_vector) {
1202 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001203 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001204 }
Mike Travis22f65d32008-12-16 17:33:56 -08001205 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001206 per_cpu(vector_irq, new_cpu)[vector] = irq;
1207 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001208 cpumask_copy(cfg->domain, tmp_mask);
1209 err = 0;
1210 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001211 }
Mike Travis22f65d32008-12-16 17:33:56 -08001212 free_cpumask_var(tmp_mask);
1213 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001214}
1215
Mike Travise7986732008-12-16 17:33:52 -08001216static int
1217assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001218{
1219 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001220 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001221
1222 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001223 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001224 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001225 return err;
1226}
1227
Yinghai Lu3145e942008-12-05 18:58:34 -08001228static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001229{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001230 int cpu, vector;
1231
Yinghai Lu497c9a12008-08-19 20:50:28 -07001232 BUG_ON(!cfg->vector);
1233
1234 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001235 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001236 per_cpu(vector_irq, cpu)[vector] = -1;
1237
1238 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001239 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001240
1241 if (likely(!cfg->move_in_progress))
1242 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001243 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001244 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1245 vector++) {
1246 if (per_cpu(vector_irq, cpu)[vector] != irq)
1247 continue;
1248 per_cpu(vector_irq, cpu)[vector] = -1;
1249 break;
1250 }
1251 }
1252 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001253}
1254
1255void __setup_vector_irq(int cpu)
1256{
1257 /* Initialize vector_irq on a new cpu */
1258 /* This function must be called with vector_lock held */
1259 int irq, vector;
1260 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001261 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001262
1263 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001264 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001265 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001266 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001267 continue;
1268 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001269 per_cpu(vector_irq, cpu)[vector] = irq;
1270 }
1271 /* Mark the free vectors */
1272 for (vector = 0; vector < NR_VECTORS; ++vector) {
1273 irq = per_cpu(vector_irq, cpu)[vector];
1274 if (irq < 0)
1275 continue;
1276
1277 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001278 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001279 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001280 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001281}
Glauber Costa3fde6902008-05-28 20:34:19 -07001282
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001283static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001284static struct irq_chip ir_ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Ingo Molnar54168ed2008-08-20 09:07:45 +02001286#define IOAPIC_AUTO -1
1287#define IOAPIC_EDGE 0
1288#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001290#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001291static inline int IO_APIC_irq_trigger(int irq)
1292{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001293 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001294
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001295 for (apic = 0; apic < nr_ioapics; apic++) {
1296 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1297 idx = find_irq_entry(apic, pin, mp_INT);
1298 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1299 return irq_trigger(idx);
1300 }
1301 }
1302 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001303 * nonexistent IRQs are edge default
1304 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001305 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001306}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001307#else
1308static inline int IO_APIC_irq_trigger(int irq)
1309{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001311}
1312#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001313
Yinghai Lu3145e942008-12-05 18:58:34 -08001314static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Yinghai Lu199751d2008-08-19 20:50:27 -07001316
Jan Beulich6ebcc002006-06-26 13:56:46 +02001317 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001318 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001319 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001320 else
1321 desc->status &= ~IRQ_LEVEL;
1322
Ingo Molnar54168ed2008-08-20 09:07:45 +02001323 if (irq_remapped(irq)) {
1324 desc->status |= IRQ_MOVE_PCNTXT;
1325 if (trigger)
1326 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1327 handle_fasteoi_irq,
1328 "fasteoi");
1329 else
1330 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1331 handle_edge_irq, "edge");
1332 return;
1333 }
Suresh Siddha29b61be2009-03-16 17:05:02 -07001334
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001335 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1336 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001337 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001338 handle_fasteoi_irq,
1339 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001340 else
Ingo Molnara460e742006-10-17 00:10:03 -07001341 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001342 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001343}
1344
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -08001345int setup_ioapic_entry(int apic_id, int irq,
1346 struct IO_APIC_route_entry *entry,
1347 unsigned int destination, int trigger,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001348 int polarity, int vector, int pin)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001349{
1350 /*
1351 * add it to the IO-APIC irq-routing table:
1352 */
1353 memset(entry,0,sizeof(*entry));
1354
Ingo Molnar54168ed2008-08-20 09:07:45 +02001355 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001356 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001357 struct irte irte;
1358 struct IR_IO_APIC_route_entry *ir_entry =
1359 (struct IR_IO_APIC_route_entry *) entry;
1360 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001361
Ingo Molnar54168ed2008-08-20 09:07:45 +02001362 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001363 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001364
1365 index = alloc_irte(iommu, irq, 1);
1366 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001367 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001368
1369 memset(&irte, 0, sizeof(irte));
1370
1371 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001372 irte.dst_mode = apic->irq_dest_mode;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001373 /*
1374 * Trigger mode in the IRTE will always be edge, and the
1375 * actual level or edge trigger will be setup in the IO-APIC
1376 * RTE. This will help simplify level triggered irq migration.
1377 * For more details, see the comments above explainig IO-APIC
1378 * irq migration in the presence of interrupt-remapping.
1379 */
1380 irte.trigger_mode = 0;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001381 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001382 irte.vector = vector;
1383 irte.dest_id = IRTE_DEST(destination);
1384
Weidong Hanf007e992009-05-23 00:41:15 +08001385 /* Set source-id of interrupt request */
1386 set_ioapic_sid(&irte, apic_id);
1387
Ingo Molnar54168ed2008-08-20 09:07:45 +02001388 modify_irte(irq, &irte);
1389
1390 ir_entry->index2 = (index >> 15) & 0x1;
1391 ir_entry->zero = 0;
1392 ir_entry->format = 1;
1393 ir_entry->index = (index & 0x7fff);
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001394 /*
1395 * IO-APIC RTE will be configured with virtual vector.
1396 * irq handler will do the explicit EOI to the io-apic.
1397 */
1398 ir_entry->vector = pin;
Suresh Siddha29b61be2009-03-16 17:05:02 -07001399 } else {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001400 entry->delivery_mode = apic->irq_delivery_mode;
1401 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001402 entry->dest = destination;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001403 entry->vector = vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001404 }
1405
1406 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001407 entry->trigger = trigger;
1408 entry->polarity = polarity;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001409
1410 /* Mask level triggered irqs.
1411 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1412 */
1413 if (trigger)
1414 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001415 return 0;
1416}
1417
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001418static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001419 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001420{
1421 struct irq_cfg *cfg;
1422 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001423 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001424
1425 if (!IO_APIC_IRQ(irq))
1426 return;
1427
Yinghai Lu3145e942008-12-05 18:58:34 -08001428 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001429
Ingo Molnarfe402e12009-01-28 04:32:51 +01001430 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001431 return;
1432
Ingo Molnardebccb32009-01-28 15:20:18 +01001433 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001434
1435 apic_printk(APIC_VERBOSE,KERN_DEBUG
1436 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1437 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001438 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001439 irq, trigger, polarity);
1440
1441
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001442 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001443 dest, trigger, polarity, cfg->vector, pin)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001444 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001445 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001446 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001447 return;
1448 }
1449
Yinghai Lu3145e942008-12-05 18:58:34 -08001450 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001451 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001452 disable_8259A_irq(irq);
1453
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001454 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Yinghai Lub9c61b702009-05-06 10:10:06 -07001457static struct {
1458 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1459} mp_ioapic_routing[MAX_IO_APICS];
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static void __init setup_IO_APIC_irqs(void)
1462{
Yinghai Lub9c61b702009-05-06 10:10:06 -07001463 int apic_id = 0, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001464 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001465 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001466 struct irq_cfg *cfg;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07001467 int node = cpu_to_node(boot_cpu_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1470
Yinghai Lub9c61b702009-05-06 10:10:06 -07001471#ifdef CONFIG_ACPI
1472 if (!acpi_disabled && acpi_ioapic) {
1473 apic_id = mp_find_ioapic(0);
1474 if (apic_id < 0)
1475 apic_id = 0;
1476 }
1477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Yinghai Lub9c61b702009-05-06 10:10:06 -07001479 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1480 idx = find_irq_entry(apic_id, pin, mp_INT);
1481 if (idx == -1) {
1482 if (!notcon) {
1483 notcon = 1;
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001484 apic_printk(APIC_VERBOSE,
Yinghai Lub9c61b702009-05-06 10:10:06 -07001485 KERN_DEBUG " %d-%d",
1486 mp_ioapics[apic_id].apicid, pin);
1487 } else
1488 apic_printk(APIC_VERBOSE, " %d-%d",
1489 mp_ioapics[apic_id].apicid, pin);
1490 continue;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001491 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07001492 if (notcon) {
1493 apic_printk(APIC_VERBOSE,
1494 " (apicid-pin) not connected\n");
1495 notcon = 0;
1496 }
1497
1498 irq = pin_2_irq(idx, apic_id, pin);
1499
1500 /*
1501 * Skip the timer IRQ if there's a quirk handler
1502 * installed and if it returns 1:
1503 */
1504 if (apic->multi_timer_check &&
1505 apic->multi_timer_check(apic_id, irq))
1506 continue;
1507
1508 desc = irq_to_desc_alloc_node(irq, node);
1509 if (!desc) {
1510 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1511 continue;
1512 }
1513 cfg = desc->chip_data;
1514 add_pin_to_irq_node(cfg, node, apic_id, pin);
Yinghai Lu4c6f18f2009-05-18 10:23:28 -07001515 /*
1516 * don't mark it in pin_programmed, so later acpi could
1517 * set it correctly when irq < 16
1518 */
Yinghai Lub9c61b702009-05-06 10:10:06 -07001519 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1520 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001523 if (notcon)
1524 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001525 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001529 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001531static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001532 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Ingo Molnar54168ed2008-08-20 09:07:45 +02001536 if (intr_remapping_enabled)
1537 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001538
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001539 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /*
1542 * We use logical delivery to get the timer IRQ
1543 * to the first CPU.
1544 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001545 entry.dest_mode = apic->irq_dest_mode;
Yinghai Luf72dcca2009-02-08 16:18:03 -08001546 entry.mask = 0; /* don't mask IRQ for edge */
Ingo Molnardebccb32009-01-28 15:20:18 +01001547 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001548 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 entry.polarity = 0;
1550 entry.trigger = 0;
1551 entry.vector = vector;
1552
1553 /*
1554 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001555 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001557 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /*
1560 * Add it to the IO-APIC irq-routing table:
1561 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001562 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001565
1566__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 int apic, i;
1569 union IO_APIC_reg_00 reg_00;
1570 union IO_APIC_reg_01 reg_01;
1571 union IO_APIC_reg_02 reg_02;
1572 union IO_APIC_reg_03 reg_03;
1573 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001574 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001575 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001576 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 if (apic_verbosity == APIC_QUIET)
1579 return;
1580
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001581 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 for (i = 0; i < nr_ioapics; i++)
1583 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301584 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /*
1587 * We are a bit conservative about what we expect. We have to
1588 * know about every hardware change ASAP.
1589 */
1590 printk(KERN_INFO "testing the IO APIC.......................\n");
1591
1592 for (apic = 0; apic < nr_ioapics; apic++) {
1593
1594 spin_lock_irqsave(&ioapic_lock, flags);
1595 reg_00.raw = io_apic_read(apic, 0);
1596 reg_01.raw = io_apic_read(apic, 1);
1597 if (reg_01.bits.version >= 0x10)
1598 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001599 if (reg_01.bits.version >= 0x20)
1600 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 spin_unlock_irqrestore(&ioapic_lock, flags);
1602
Ingo Molnar54168ed2008-08-20 09:07:45 +02001603 printk("\n");
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301604 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1606 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1607 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1608 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Ingo Molnar54168ed2008-08-20 09:07:45 +02001610 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1614 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /*
1617 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1618 * but the value of reg_02 is read as the previous read register
1619 * value, so ignore it if reg_02 == reg_01.
1620 */
1621 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1622 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1623 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
1626 /*
1627 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1628 * or reg_03, but the value of reg_0[23] is read as the previous read
1629 * register value, so ignore it if reg_03 == reg_0[12].
1630 */
1631 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1632 reg_03.raw != reg_01.raw) {
1633 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1634 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1638
Yinghai Lud83e94a2008-08-19 20:50:33 -07001639 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1640 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 for (i = 0; i <= reg_01.bits.entries; i++) {
1643 struct IO_APIC_route_entry entry;
1644
Andi Kleencf4c6a22006-09-26 10:52:30 +02001645 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Ingo Molnar54168ed2008-08-20 09:07:45 +02001647 printk(KERN_DEBUG " %02x %03X ",
1648 i,
1649 entry.dest
1650 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1653 entry.mask,
1654 entry.trigger,
1655 entry.irr,
1656 entry.polarity,
1657 entry.delivery_status,
1658 entry.dest_mode,
1659 entry.delivery_mode,
1660 entry.vector
1661 );
1662 }
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001665 for_each_irq_desc(irq, desc) {
1666 struct irq_pin_list *entry;
1667
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001668 cfg = desc->chip_data;
1669 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001670 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001672 printk(KERN_DEBUG "IRQ%d ", irq);
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04001673 for_each_irq_pin(entry, cfg->irq_2_pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 printk("-> %d:%d", entry->apic, entry->pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 printk("\n");
1676 }
1677
1678 printk(KERN_INFO ".................................... done.\n");
1679
1680 return;
1681}
1682
Ingo Molnar251e1e42009-07-02 08:54:01 +02001683__apicdebuginit(void) print_APIC_field(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Ingo Molnar251e1e42009-07-02 08:54:01 +02001685 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 if (apic_verbosity == APIC_QUIET)
1688 return;
1689
Ingo Molnar251e1e42009-07-02 08:54:01 +02001690 printk(KERN_DEBUG);
1691
1692 for (i = 0; i < 8; i++)
1693 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1694
1695 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001698__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Andreas Herrmann97a52712009-05-08 18:23:50 +02001700 unsigned int i, v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001701 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 if (apic_verbosity == APIC_QUIET)
1704 return;
1705
Ingo Molnar251e1e42009-07-02 08:54:01 +02001706 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001708 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001709 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 v = apic_read(APIC_LVR);
1711 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1712 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001713 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 v = apic_read(APIC_TASKPRI);
1716 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1717
Ingo Molnar54168ed2008-08-20 09:07:45 +02001718 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001719 if (!APIC_XAPIC(ver)) {
1720 v = apic_read(APIC_ARBPRI);
1721 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1722 v & APIC_ARBPRI_MASK);
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 v = apic_read(APIC_PROCPRI);
1725 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1726 }
1727
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001728 /*
1729 * Remote read supported only in the 82489DX and local APIC for
1730 * Pentium processors.
1731 */
1732 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1733 v = apic_read(APIC_RRR);
1734 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1735 }
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 v = apic_read(APIC_LDR);
1738 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001739 if (!x2apic_enabled()) {
1740 v = apic_read(APIC_DFR);
1741 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 v = apic_read(APIC_SPIV);
1744 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1745
1746 printk(KERN_DEBUG "... APIC ISR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001747 print_APIC_field(APIC_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 printk(KERN_DEBUG "... APIC TMR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001749 print_APIC_field(APIC_TMR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 printk(KERN_DEBUG "... APIC IRR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001751 print_APIC_field(APIC_IRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Ingo Molnar54168ed2008-08-20 09:07:45 +02001753 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1754 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 v = apic_read(APIC_ESR);
1758 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1759 }
1760
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001761 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001762 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1763 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 v = apic_read(APIC_LVTT);
1766 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1767
1768 if (maxlvt > 3) { /* PC is LVT#4. */
1769 v = apic_read(APIC_LVTPC);
1770 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1771 }
1772 v = apic_read(APIC_LVT0);
1773 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1774 v = apic_read(APIC_LVT1);
1775 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1776
1777 if (maxlvt > 2) { /* ERR is LVT#3. */
1778 v = apic_read(APIC_LVTERR);
1779 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1780 }
1781
1782 v = apic_read(APIC_TMICT);
1783 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1784 v = apic_read(APIC_TMCCT);
1785 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1786 v = apic_read(APIC_TDCR);
1787 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
Andreas Herrmann97a52712009-05-08 18:23:50 +02001788
1789 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1790 v = apic_read(APIC_EFEAT);
1791 maxlvt = (v >> 16) & 0xff;
1792 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1793 v = apic_read(APIC_ECTRL);
1794 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1795 for (i = 0; i < maxlvt; i++) {
1796 v = apic_read(APIC_EILVTn(i));
1797 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1798 }
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 printk("\n");
1801}
1802
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001803__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001805 int cpu;
1806
1807 preempt_disable();
1808 for_each_online_cpu(cpu)
1809 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1810 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001813__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 unsigned int v;
1816 unsigned long flags;
1817
1818 if (apic_verbosity == APIC_QUIET)
1819 return;
1820
1821 printk(KERN_DEBUG "\nprinting PIC contents\n");
1822
1823 spin_lock_irqsave(&i8259A_lock, flags);
1824
1825 v = inb(0xa1) << 8 | inb(0x21);
1826 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1827
1828 v = inb(0xa0) << 8 | inb(0x20);
1829 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1830
Ingo Molnar54168ed2008-08-20 09:07:45 +02001831 outb(0x0b,0xa0);
1832 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001834 outb(0x0a,0xa0);
1835 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 spin_unlock_irqrestore(&i8259A_lock, flags);
1838
1839 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1840
1841 v = inb(0x4d1) << 8 | inb(0x4d0);
1842 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1843}
1844
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001845__apicdebuginit(int) print_all_ICs(void)
1846{
1847 print_PIC();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001848
1849 /* don't print out if apic is not there */
1850 if (!cpu_has_apic || disable_apic)
1851 return 0;
1852
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001853 print_all_local_APICs();
1854 print_IO_APIC();
1855
1856 return 0;
1857}
1858
1859fs_initcall(print_all_ICs);
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Yinghai Luefa25592008-08-19 20:50:36 -07001862/* Where if anywhere is the i8259 connect in external int mode */
1863static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1864
Ingo Molnar54168ed2008-08-20 09:07:45 +02001865void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001868 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001869 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 unsigned long flags;
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
1873 * The number of IO-APIC IRQ registers (== #pins):
1874 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001875 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001877 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001879 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1880 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001881 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001882 int pin;
1883 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001884 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001885 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001886 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001887
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001888 /* If the interrupt line is enabled and in ExtInt mode
1889 * I have found the pin where the i8259 is connected.
1890 */
1891 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1892 ioapic_i8259.apic = apic;
1893 ioapic_i8259.pin = pin;
1894 goto found_i8259;
1895 }
1896 }
1897 }
1898 found_i8259:
1899 /* Look to see what if the MP table has reported the ExtINT */
1900 /* If we could not find the appropriate pin by looking at the ioapic
1901 * the i8259 probably is not connected the ioapic but give the
1902 * mptable a chance anyway.
1903 */
1904 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1905 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1906 /* Trust the MP table if nothing is setup in the hardware */
1907 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1908 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1909 ioapic_i8259.pin = i8259_pin;
1910 ioapic_i8259.apic = i8259_apic;
1911 }
1912 /* Complain if the MP table and the hardware disagree */
1913 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1914 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1915 {
1916 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918
1919 /*
1920 * Do not trust the IO-APIC being empty at bootup
1921 */
1922 clear_IO_APIC();
1923}
1924
1925/*
1926 * Not an __init, needed by the reboot code
1927 */
1928void disable_IO_APIC(void)
1929{
1930 /*
1931 * Clear the IO-APIC before rebooting:
1932 */
1933 clear_IO_APIC();
1934
Eric W. Biederman650927e2005-06-25 14:57:44 -07001935 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001936 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001937 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001938 * so legacy interrupts can be delivered.
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001939 *
1940 * With interrupt-remapping, for now we will use virtual wire A mode,
1941 * as virtual wire B is little complex (need to configure both
1942 * IOAPIC RTE aswell as interrupt-remapping table entry).
1943 * As this gets called during crash dump, keep this simple for now.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001944 */
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001945 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001946 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001947
1948 memset(&entry, 0, sizeof(entry));
1949 entry.mask = 0; /* Enabled */
1950 entry.trigger = 0; /* Edge */
1951 entry.irr = 0;
1952 entry.polarity = 0; /* High */
1953 entry.delivery_status = 0;
1954 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001955 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001956 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001957 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001958
1959 /*
1960 * Add it to the IO-APIC irq-routing table:
1961 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001962 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001963 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001964
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001965 /*
1966 * Use virtual wire A mode when interrupt remapping is enabled.
1967 */
Cyrill Gorcunov3f4c3952009-06-17 22:13:22 +04001968 if (cpu_has_apic)
1969 disconnect_bsp_APIC(!intr_remapping_enabled &&
1970 ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Ingo Molnar54168ed2008-08-20 09:07:45 +02001973#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
1975 * function to set the IO-APIC physical IDs based on the
1976 * values stored in the MPC table.
1977 *
1978 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1979 */
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981static void __init setup_ioapic_ids_from_mpc(void)
1982{
1983 union IO_APIC_reg_00 reg_00;
1984 physid_mask_t phys_id_present_map;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001985 int apic_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int i;
1987 unsigned char old_id;
1988 unsigned long flags;
1989
Yinghai Lua4dbc342008-07-25 02:14:28 -07001990 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001991 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001994 * Don't check I/O APIC IDs for xAPIC systems. They have
1995 * no meaning without the serial APIC bus.
1996 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001997 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1998 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001999 return;
2000 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * This is broken; anything with a real cpu count has to
2002 * circumvent this idiocy regardless.
2003 */
Ingo Molnard190cb82009-01-28 06:50:47 +01002004 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 /*
2007 * Set the IOAPIC ID to the value stored in the MPC table.
2008 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002009 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 /* Read the register 0 value */
2012 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002013 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002015
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002016 old_id = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002018 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002020 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2022 reg_00.bits.ID);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002023 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /*
2027 * Sanity check, is the ID really free? Every APIC in a
2028 * system must have a unique ID or we get lots of nice
2029 * 'stuck on smp_invalidate_needed IPI wait' messages.
2030 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01002031 if (apic->check_apicid_used(phys_id_present_map,
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002032 mp_ioapics[apic_id].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002034 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 for (i = 0; i < get_physical_broadcast(); i++)
2036 if (!physid_isset(i, phys_id_present_map))
2037 break;
2038 if (i >= get_physical_broadcast())
2039 panic("Max APIC ID exceeded!\n");
2040 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2041 i);
2042 physid_set(i, phys_id_present_map);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002043 mp_ioapics[apic_id].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 } else {
2045 physid_mask_t tmp;
Ingo Molnar80587142009-01-28 06:50:47 +01002046 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 apic_printk(APIC_VERBOSE, "Setting %d in the "
2048 "phys_id_present_map\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002049 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2051 }
2052
2053
2054 /*
2055 * We need to adjust the IRQ routing table
2056 * if the ID changed.
2057 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002058 if (old_id != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302060 if (mp_irqs[i].dstapic == old_id)
2061 mp_irqs[i].dstapic
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002062 = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /*
2065 * Read the right value from the MPC table and
2066 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 apic_printk(APIC_VERBOSE, KERN_INFO
2069 "...changing IO-APIC physical APIC ID to %d ...",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002070 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002072 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002074 io_apic_write(apic_id, 0, reg_00.raw);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002075 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /*
2078 * Sanity check
2079 */
2080 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002081 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002083 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 printk("could not set ID!\n");
2085 else
2086 apic_printk(APIC_VERBOSE, " ok.\n");
2087 }
2088}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002091int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002092
2093static int __init notimercheck(char *s)
2094{
2095 no_timer_check = 1;
2096 return 1;
2097}
2098__setup("no_timer_check", notimercheck);
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100/*
2101 * There is a nasty bug in some older SMP boards, their mptable lies
2102 * about the timer IRQ. We do the following to work around the situation:
2103 *
2104 * - timer IRQ defaults to IO-APIC IRQ
2105 * - if this function detects that timer IRQs are defunct, then we fall
2106 * back to ISA timer IRQs
2107 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002108static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002111 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Zachary Amsden8542b202006-12-07 02:14:09 +01002113 if (no_timer_check)
2114 return 1;
2115
Ingo Molnar4aae0702007-12-18 18:05:58 +01002116 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 local_irq_enable();
2118 /* Let ten ticks pass... */
2119 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002120 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /*
2123 * Expect a few ticks at least, to be sure some possible
2124 * glue logic does not lock up after one or two first
2125 * ticks in a non-ExtINT mode. Also the local APIC
2126 * might have cached one ExtINT interrupt. Finally, at
2127 * least one tick may be lost due to delays.
2128 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002129
2130 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002131 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return 0;
2134}
2135
2136/*
2137 * In the SMP+IOAPIC case it might happen that there are an unspecified
2138 * number of pending IRQ events unhandled. These cases are very rare,
2139 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2140 * better to do it this way as thus we do not have to be aware of
2141 * 'pending' interrupts in the IRQ path, except at this point.
2142 */
2143/*
2144 * Edge triggered needs to resend any interrupt
2145 * that was delayed but this is now handled in the device
2146 * independent code.
2147 */
2148
2149/*
2150 * Starting up a edge-triggered IO-APIC interrupt is
2151 * nasty - we need to make sure that we get the edge.
2152 * If it is already asserted for some reason, we need
2153 * return 1 to indicate that is was pending.
2154 *
2155 * This is not complete - we should be able to fake
2156 * an edge even if it isn't on the 8259A...
2157 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002158
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002159static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 int was_pending = 0;
2162 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002163 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002166 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 disable_8259A_irq(irq);
2168 if (i8259A_irq_pending(irq))
2169 was_pending = 1;
2170 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002171 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002172 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 spin_unlock_irqrestore(&ioapic_lock, flags);
2174
2175 return was_pending;
2176}
2177
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002178static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002180
2181 struct irq_cfg *cfg = irq_cfg(irq);
2182 unsigned long flags;
2183
2184 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnardac5f412009-01-28 15:42:24 +01002185 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002186 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002187
2188 return 1;
2189}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002190
2191/*
2192 * Level and edge triggered IO-APIC interrupts need different handling,
2193 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2194 * handled with the level-triggered descriptor, but that one has slightly
2195 * more overhead. Level-triggered interrupts cannot be handled with the
2196 * edge-triggered handler, without risking IRQ storms and other ugly
2197 * races.
2198 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002199
Yinghai Lu497c9a12008-08-19 20:50:28 -07002200#ifdef CONFIG_SMP
Gary Hadee85abf82009-04-08 14:07:25 -07002201static void send_cleanup_vector(struct irq_cfg *cfg)
2202{
2203 cpumask_var_t cleanup_mask;
2204
2205 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2206 unsigned int i;
2207 cfg->move_cleanup_count = 0;
2208 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2209 cfg->move_cleanup_count++;
2210 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2211 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2212 } else {
2213 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2214 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
2215 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2216 free_cpumask_var(cleanup_mask);
2217 }
2218 cfg->move_in_progress = 0;
2219}
2220
Ingo Molnar44204712009-05-01 19:02:50 +02002221static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002222{
2223 int apic, pin;
2224 struct irq_pin_list *entry;
2225 u8 vector = cfg->vector;
2226
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04002227 for_each_irq_pin(entry, cfg->irq_2_pin) {
Gary Hadee85abf82009-04-08 14:07:25 -07002228 unsigned int reg;
2229
Gary Hadee85abf82009-04-08 14:07:25 -07002230 apic = entry->apic;
2231 pin = entry->pin;
2232 /*
2233 * With interrupt-remapping, destination information comes
2234 * from interrupt-remapping table entry.
2235 */
2236 if (!irq_remapped(irq))
2237 io_apic_write(apic, 0x11 + pin*2, dest);
2238 reg = io_apic_read(apic, 0x10 + pin*2);
2239 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2240 reg |= vector;
2241 io_apic_modify(apic, 0x10 + pin*2, reg);
Gary Hadee85abf82009-04-08 14:07:25 -07002242 }
2243}
2244
Ingo Molnar44204712009-05-01 19:02:50 +02002245static int
2246assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
2247
Gary Hadee85abf82009-04-08 14:07:25 -07002248/*
2249 * Either sets desc->affinity to a valid value, and returns
2250 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
2251 * leaves desc->affinity untouched.
2252 */
2253static unsigned int
2254set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
2255{
2256 struct irq_cfg *cfg;
2257 unsigned int irq;
2258
2259 if (!cpumask_intersects(mask, cpu_online_mask))
2260 return BAD_APICID;
2261
2262 irq = desc->irq;
2263 cfg = desc->chip_data;
2264 if (assign_irq_vector(irq, cfg, mask))
2265 return BAD_APICID;
2266
Gary Hadee85abf82009-04-08 14:07:25 -07002267 cpumask_copy(desc->affinity, mask);
2268
2269 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2270}
2271
Ingo Molnar44204712009-05-01 19:02:50 +02002272static int
Gary Hadee85abf82009-04-08 14:07:25 -07002273set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2274{
2275 struct irq_cfg *cfg;
2276 unsigned long flags;
2277 unsigned int dest;
2278 unsigned int irq;
Ingo Molnar44204712009-05-01 19:02:50 +02002279 int ret = -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002280
2281 irq = desc->irq;
2282 cfg = desc->chip_data;
2283
2284 spin_lock_irqsave(&ioapic_lock, flags);
2285 dest = set_desc_affinity(desc, mask);
2286 if (dest != BAD_APICID) {
2287 /* Only the high 8 bits are valid. */
2288 dest = SET_APIC_LOGICAL_ID(dest);
2289 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar44204712009-05-01 19:02:50 +02002290 ret = 0;
Gary Hadee85abf82009-04-08 14:07:25 -07002291 }
2292 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnar44204712009-05-01 19:02:50 +02002293
2294 return ret;
Gary Hadee85abf82009-04-08 14:07:25 -07002295}
2296
Ingo Molnar44204712009-05-01 19:02:50 +02002297static int
Gary Hadee85abf82009-04-08 14:07:25 -07002298set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2299{
2300 struct irq_desc *desc;
2301
2302 desc = irq_to_desc(irq);
2303
Ingo Molnar44204712009-05-01 19:02:50 +02002304 return set_ioapic_affinity_irq_desc(desc, mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002305}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002306
2307#ifdef CONFIG_INTR_REMAP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002308
2309/*
2310 * Migrate the IO-APIC irq in the presence of intr-remapping.
2311 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002312 * For both level and edge triggered, irq migration is a simple atomic
2313 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002314 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002315 * For level triggered, we eliminate the io-apic RTE modification (with the
2316 * updated vector information), by using a virtual vector (io-apic pin number).
2317 * Real vector that is used for interrupting cpu will be coming from
2318 * the interrupt-remapping table entry.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002319 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002320static int
Mike Travise7986732008-12-16 17:33:52 -08002321migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002322{
2323 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002324 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002325 unsigned int dest;
Yinghai Lu3145e942008-12-05 18:58:34 -08002326 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -07002327 int ret = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002328
Mike Travis22f65d32008-12-16 17:33:56 -08002329 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002330 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002331
Yinghai Lu3145e942008-12-05 18:58:34 -08002332 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002333 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002334 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002335
Yinghai Lu3145e942008-12-05 18:58:34 -08002336 cfg = desc->chip_data;
2337 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002338 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002339
Ingo Molnardebccb32009-01-28 15:20:18 +01002340 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002341
Ingo Molnar54168ed2008-08-20 09:07:45 +02002342 irte.vector = cfg->vector;
2343 irte.dest_id = IRTE_DEST(dest);
2344
2345 /*
2346 * Modified the IRTE and flushes the Interrupt entry cache.
2347 */
2348 modify_irte(irq, &irte);
2349
Mike Travis22f65d32008-12-16 17:33:56 -08002350 if (cfg->move_in_progress)
2351 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002352
Mike Travis7f7ace02009-01-10 21:58:08 -08002353 cpumask_copy(desc->affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -07002354
2355 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002356}
2357
Ingo Molnar54168ed2008-08-20 09:07:45 +02002358/*
2359 * Migrates the IRQ destination in the process context.
2360 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002361static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Rusty Russell968ea6d2008-12-13 21:55:51 +10302362 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002363{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002364 return migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002365}
Yinghai Lud5dedd42009-04-27 17:59:21 -07002366static int set_ir_ioapic_affinity_irq(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +10302367 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002368{
2369 struct irq_desc *desc = irq_to_desc(irq);
2370
Yinghai Lud5dedd42009-04-27 17:59:21 -07002371 return set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372}
Suresh Siddha29b61be2009-03-16 17:05:02 -07002373#else
Yinghai Lud5dedd42009-04-27 17:59:21 -07002374static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Suresh Siddha29b61be2009-03-16 17:05:02 -07002375 const struct cpumask *mask)
2376{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002377 return 0;
Suresh Siddha29b61be2009-03-16 17:05:02 -07002378}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002379#endif
2380
Yinghai Lu497c9a12008-08-19 20:50:28 -07002381asmlinkage void smp_irq_move_cleanup_interrupt(void)
2382{
2383 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002384
Yinghai Lu497c9a12008-08-19 20:50:28 -07002385 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002386 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002387 irq_enter();
2388
2389 me = smp_processor_id();
2390 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2391 unsigned int irq;
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002392 unsigned int irr;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002393 struct irq_desc *desc;
2394 struct irq_cfg *cfg;
2395 irq = __get_cpu_var(vector_irq)[vector];
2396
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002397 if (irq == -1)
2398 continue;
2399
Yinghai Lu497c9a12008-08-19 20:50:28 -07002400 desc = irq_to_desc(irq);
2401 if (!desc)
2402 continue;
2403
2404 cfg = irq_cfg(irq);
2405 spin_lock(&desc->lock);
2406 if (!cfg->move_cleanup_count)
2407 goto unlock;
2408
Mike Travis22f65d32008-12-16 17:33:56 -08002409 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002410 goto unlock;
2411
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002412 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2413 /*
2414 * Check if the vector that needs to be cleanedup is
2415 * registered at the cpu's IRR. If so, then this is not
2416 * the best time to clean it up. Lets clean it up in the
2417 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2418 * to myself.
2419 */
2420 if (irr & (1 << (vector % 32))) {
2421 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2422 goto unlock;
2423 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002424 __get_cpu_var(vector_irq)[vector] = -1;
2425 cfg->move_cleanup_count--;
2426unlock:
2427 spin_unlock(&desc->lock);
2428 }
2429
2430 irq_exit();
2431}
2432
Yinghai Lu3145e942008-12-05 18:58:34 -08002433static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002434{
Yinghai Lu3145e942008-12-05 18:58:34 -08002435 struct irq_desc *desc = *descp;
2436 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002437 unsigned vector, me;
2438
Yinghai Lufcef5912009-04-27 17:58:23 -07002439 if (likely(!cfg->move_in_progress))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002440 return;
2441
2442 vector = ~get_irq_regs()->orig_ax;
2443 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002444
Yinghai Lufcef5912009-04-27 17:58:23 -07002445 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Mike Travis22f65d32008-12-16 17:33:56 -08002446 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002447}
2448#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002449static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002450#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002451
Yinghai Lu1d025192008-08-19 20:50:34 -07002452static void ack_apic_edge(unsigned int irq)
2453{
Yinghai Lu3145e942008-12-05 18:58:34 -08002454 struct irq_desc *desc = irq_to_desc(irq);
2455
2456 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002457 move_native_irq(irq);
2458 ack_APIC_irq();
2459}
2460
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002461atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002462
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002463static void ack_apic_level(unsigned int irq)
2464{
Yinghai Lu3145e942008-12-05 18:58:34 -08002465 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002466 unsigned long v;
2467 int i;
Yinghai Lu3145e942008-12-05 18:58:34 -08002468 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002469 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002470
Yinghai Lu3145e942008-12-05 18:58:34 -08002471 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002472#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002473 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002474 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002475 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002476 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002477 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002478#endif
2479
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002480 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002481 * It appears there is an erratum which affects at least version 0x11
2482 * of I/O APIC (that's the 82093AA and cores integrated into various
2483 * chipsets). Under certain conditions a level-triggered interrupt is
2484 * erroneously delivered as edge-triggered one but the respective IRR
2485 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2486 * message but it will never arrive and further interrupts are blocked
2487 * from the source. The exact reason is so far unknown, but the
2488 * phenomenon was observed when two consecutive interrupt requests
2489 * from a given source get delivered to the same CPU and the source is
2490 * temporarily disabled in between.
2491 *
2492 * A workaround is to simulate an EOI message manually. We achieve it
2493 * by setting the trigger mode to edge and then to level when the edge
2494 * trigger mode gets detected in the TMR of a local APIC for a
2495 * level-triggered interrupt. We mask the source for the time of the
2496 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2497 * The idea is from Manfred Spraul. --macro
2498 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002499 cfg = desc->chip_data;
2500 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002501 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002502
Ingo Molnar54168ed2008-08-20 09:07:45 +02002503 /*
2504 * We must acknowledge the irq before we move it or the acknowledge will
2505 * not propagate properly.
2506 */
2507 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002508
Ingo Molnar54168ed2008-08-20 09:07:45 +02002509 /* Now we can move and renable the irq */
2510 if (unlikely(do_unmask_irq)) {
2511 /* Only migrate the irq if the ack has been received.
2512 *
2513 * On rare occasions the broadcast level triggered ack gets
2514 * delayed going to ioapics, and if we reprogram the
2515 * vector while Remote IRR is still set the irq will never
2516 * fire again.
2517 *
2518 * To prevent this scenario we read the Remote IRR bit
2519 * of the ioapic. This has two effects.
2520 * - On any sane system the read of the ioapic will
2521 * flush writes (and acks) going to the ioapic from
2522 * this cpu.
2523 * - We get to see if the ACK has actually been delivered.
2524 *
2525 * Based on failed experiments of reprogramming the
2526 * ioapic entry from outside of irq context starting
2527 * with masking the ioapic entry and then polling until
2528 * Remote IRR was clear before reprogramming the
2529 * ioapic I don't trust the Remote IRR bit to be
2530 * completey accurate.
2531 *
2532 * However there appears to be no other way to plug
2533 * this race, so if the Remote IRR bit is not
2534 * accurate and is causing problems then it is a hardware bug
2535 * and you can go talk to the chipset vendor about it.
2536 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002537 cfg = desc->chip_data;
2538 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002539 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002540 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002541 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002542
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002543 /* Tail end of version 0x11 I/O APIC bug workaround */
Yinghai Lu1d025192008-08-19 20:50:34 -07002544 if (!(v & (1 << (i & 0x1f)))) {
2545 atomic_inc(&irq_mis_count);
2546 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002547 __mask_and_edge_IO_APIC_irq(cfg);
2548 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002549 spin_unlock(&ioapic_lock);
2550 }
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002551}
Yinghai Lu1d025192008-08-19 20:50:34 -07002552
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002553#ifdef CONFIG_INTR_REMAP
Suresh Siddha25629d82009-04-20 13:02:28 -07002554static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2555{
Suresh Siddha25629d82009-04-20 13:02:28 -07002556 struct irq_pin_list *entry;
2557
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +04002558 for_each_irq_pin(entry, cfg->irq_2_pin)
2559 io_apic_eoi(entry->apic, entry->pin);
Suresh Siddha25629d82009-04-20 13:02:28 -07002560}
2561
2562static void
2563eoi_ioapic_irq(struct irq_desc *desc)
2564{
2565 struct irq_cfg *cfg;
2566 unsigned long flags;
2567 unsigned int irq;
2568
2569 irq = desc->irq;
2570 cfg = desc->chip_data;
2571
2572 spin_lock_irqsave(&ioapic_lock, flags);
2573 __eoi_ioapic_irq(irq, cfg);
2574 spin_unlock_irqrestore(&ioapic_lock, flags);
2575}
2576
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002577static void ir_ack_apic_edge(unsigned int irq)
2578{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002579 ack_APIC_irq();
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002580}
2581
2582static void ir_ack_apic_level(unsigned int irq)
2583{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002584 struct irq_desc *desc = irq_to_desc(irq);
2585
2586 ack_APIC_irq();
2587 eoi_ioapic_irq(desc);
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002588}
2589#endif /* CONFIG_INTR_REMAP */
2590
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002591static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002592 .name = "IO-APIC",
2593 .startup = startup_ioapic_irq,
2594 .mask = mask_IO_APIC_irq,
2595 .unmask = unmask_IO_APIC_irq,
2596 .ack = ack_apic_edge,
2597 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002598#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002599 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002600#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002601 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602};
2603
Ingo Molnar54168ed2008-08-20 09:07:45 +02002604static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002605 .name = "IR-IO-APIC",
2606 .startup = startup_ioapic_irq,
2607 .mask = mask_IO_APIC_irq,
2608 .unmask = unmask_IO_APIC_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302609#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002610 .ack = ir_ack_apic_edge,
2611 .eoi = ir_ack_apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002612#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002613 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002614#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302615#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002616 .retrigger = ioapic_retrigger_irq,
2617};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619static inline void init_IO_APIC_traps(void)
2620{
2621 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002622 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002623 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 /*
2626 * NOTE! The local APIC isn't very good at handling
2627 * multiple interrupts at the same interrupt level.
2628 * As the interrupt level is determined by taking the
2629 * vector number and shifting that right by 4, we
2630 * want to spread these out a bit so that they don't
2631 * all fall in the same interrupt level.
2632 *
2633 * Also, we've got to be careful not to trash gate
2634 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2635 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002636 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002637 cfg = desc->chip_data;
2638 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /*
2640 * Hmm.. We don't have an entry for this,
2641 * so default to an old-fashioned 8259
2642 * interrupt if we can..
2643 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002644 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002646 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002648 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
2650 }
2651}
2652
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002653/*
2654 * The local APIC irq-chip implementation:
2655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002657static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 unsigned long v;
2660
2661 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002662 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002665static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002667 unsigned long v;
2668
2669 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002670 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Yinghai Lu3145e942008-12-05 18:58:34 -08002673static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002674{
2675 ack_APIC_irq();
2676}
2677
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002678static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002679 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002680 .mask = mask_lapic_irq,
2681 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002682 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683};
2684
Yinghai Lu3145e942008-12-05 18:58:34 -08002685static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002686{
Yinghai Lu08678b02008-08-19 20:50:05 -07002687 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002688 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2689 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002690}
2691
Jan Beuliche9427102008-01-30 13:31:24 +01002692static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002695 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * We put the 8259A master into AEOI mode and
2697 * unmask on all local APICs LVT0 as NMI.
2698 *
2699 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2700 * is from Maciej W. Rozycki - so we do not have to EOI from
2701 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2704
Jan Beuliche9427102008-01-30 13:31:24 +01002705 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 apic_printk(APIC_VERBOSE, " done.\n");
2708}
2709
2710/*
2711 * This looks a bit hackish but it's about the only one way of sending
2712 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2713 * not support the ExtINT mode, unfortunately. We need to send these
2714 * cycles as some i82489DX-based boards have glue logic that keeps the
2715 * 8259A interrupt line asserted until INTA. --macro
2716 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002717static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002719 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct IO_APIC_route_entry entry0, entry1;
2721 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002723 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002724 if (pin == -1) {
2725 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002727 }
2728 apic = find_isa_irq_apic(8, mp_INT);
2729 if (apic == -1) {
2730 WARN_ON_ONCE(1);
2731 return;
2732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Andi Kleencf4c6a22006-09-26 10:52:30 +02002734 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002735 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 memset(&entry1, 0, sizeof(entry1));
2738
2739 entry1.dest_mode = 0; /* physical delivery */
2740 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002741 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 entry1.delivery_mode = dest_ExtINT;
2743 entry1.polarity = entry0.polarity;
2744 entry1.trigger = 0;
2745 entry1.vector = 0;
2746
Andi Kleencf4c6a22006-09-26 10:52:30 +02002747 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 save_control = CMOS_READ(RTC_CONTROL);
2750 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2751 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2752 RTC_FREQ_SELECT);
2753 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2754
2755 i = 100;
2756 while (i-- > 0) {
2757 mdelay(10);
2758 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2759 i -= 10;
2760 }
2761
2762 CMOS_WRITE(save_control, RTC_CONTROL);
2763 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002764 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Andi Kleencf4c6a22006-09-26 10:52:30 +02002766 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
Yinghai Luefa25592008-08-19 20:50:36 -07002769static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002770/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002771static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002772{
2773 disable_timer_pin_1 = 1;
2774 return 0;
2775}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002776early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002777
2778int timer_through_8259 __initdata;
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780/*
2781 * This code may look a bit paranoid, but it's supposed to cooperate with
2782 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2783 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2784 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002785 *
2786 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002788static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Yinghai Lu3145e942008-12-05 18:58:34 -08002790 struct irq_desc *desc = irq_to_desc(0);
2791 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002792 int node = cpu_to_node(boot_cpu_id);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002793 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002794 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002795 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002796
2797 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /*
2800 * get/set the timer IRQ vector:
2801 */
2802 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002803 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002806 * As IRQ0 is to be enabled in the 8259A, the virtual
2807 * wire has to be disabled in the local APIC. Also
2808 * timer interrupts need to be acknowledged manually in
2809 * the 8259A for the i82489DX when using the NMI
2810 * watchdog as that APIC treats NMIs as level-triggered.
2811 * The AEOI mode will finish them in the 8259A
2812 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002814 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002816#ifdef CONFIG_X86_32
Yinghai Luf72dcca2009-02-08 16:18:03 -08002817 {
2818 unsigned int ver;
2819
2820 ver = apic_read(APIC_LVR);
2821 ver = GET_APIC_VERSION(ver);
2822 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2823 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002826 pin1 = find_isa_irq_pin(0, mp_INT);
2827 apic1 = find_isa_irq_apic(0, mp_INT);
2828 pin2 = ioapic_i8259.pin;
2829 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002831 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2832 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002833 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002835 /*
2836 * Some BIOS writers are clueless and report the ExtINTA
2837 * I/O APIC input from the cascaded 8259A as the timer
2838 * interrupt input. So just in case, if only one pin
2839 * was found above, try it both directly and through the
2840 * 8259A.
2841 */
2842 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002843 if (intr_remapping_enabled)
2844 panic("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002845 pin1 = pin2;
2846 apic1 = apic2;
2847 no_pin1 = 1;
2848 } else if (pin2 == -1) {
2849 pin2 = pin1;
2850 apic2 = apic1;
2851 }
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (pin1 != -1) {
2854 /*
2855 * Ok, does IRQ0 through the IOAPIC work?
2856 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002857 if (no_pin1) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002858 add_pin_to_irq_node(cfg, node, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002859 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002860 } else {
2861 /* for edge trigger, setup_IO_APIC_irq already
2862 * leave it unmasked.
2863 * so only need to unmask if it is level-trigger
2864 * do we really have level trigger timer?
2865 */
2866 int idx;
2867 idx = find_irq_entry(apic1, pin1, mp_INT);
2868 if (idx != -1 && irq_trigger(idx))
2869 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (timer_irq_works()) {
2872 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 setup_nmi();
2874 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002876 if (disable_timer_pin_1 > 0)
2877 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002878 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002880 if (intr_remapping_enabled)
2881 panic("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002882 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002883 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002884 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002885 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2886 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002888 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2889 "(IRQ0) through the 8259A ...\n");
2890 apic_printk(APIC_QUIET, KERN_INFO
2891 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 /*
2893 * legacy devices should be connected to IO APIC #0
2894 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002895 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002896 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002897 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002899 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002900 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002902 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002904 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908 /*
2909 * Cleanup, just in case ...
2910 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002911 local_irq_disable();
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002912 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002913 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002914 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002918 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2919 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002920 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002922#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002923 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002926 apic_printk(APIC_QUIET, KERN_INFO
2927 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Yinghai Lu3145e942008-12-05 18:58:34 -08002929 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002930 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 enable_8259A_irq(0);
2932
2933 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002934 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002937 local_irq_disable();
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002938 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002939 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002940 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002942 apic_printk(APIC_QUIET, KERN_INFO
2943 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 init_8259A(0);
2946 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002947 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 unlock_ExtINT_logic();
2950
2951 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002952 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002955 local_irq_disable();
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002956 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002958 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002959out:
2960 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
2963/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002964 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2965 * to devices. However there may be an I/O APIC pin available for
2966 * this interrupt regardless. The pin may be left unconnected, but
2967 * typically it will be reused as an ExtINT cascade interrupt for
2968 * the master 8259A. In the MPS case such a pin will normally be
2969 * reported as an ExtINT interrupt in the MP table. With ACPI
2970 * there is no provision for ExtINT interrupts, and in the absence
2971 * of an override it would be treated as an ordinary ISA I/O APIC
2972 * interrupt, that is edge-triggered and unmasked by default. We
2973 * used to do this, but it caused problems on some systems because
2974 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2975 * the same ExtINT cascade interrupt to drive the local APIC of the
2976 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2977 * the I/O APIC in all cases now. No actual device should request
2978 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 */
2980#define PIC_IRQS (1 << PIC_CASCADE_IR)
2981
2982void __init setup_IO_APIC(void)
2983{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002984
Ingo Molnar54168ed2008-08-20 09:07:45 +02002985 /*
2986 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002989 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Ingo Molnar54168ed2008-08-20 09:07:45 +02002991 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002992 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002993 * Set up IO-APIC IRQ routing.
2994 */
2995#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002996 if (!acpi_ioapic)
2997 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 sync_Arb_IDs();
3000 setup_IO_APIC_irqs();
3001 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003002 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
3005/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003006 * Called after all the initialization is done. If we didnt find any
3007 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010static int __init io_apic_bug_finalize(void)
3011{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003012 if (sis_apic_bug == -1)
3013 sis_apic_bug = 0;
3014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017late_initcall(io_apic_bug_finalize);
3018
3019struct sysfs_ioapic_data {
3020 struct sys_device dev;
3021 struct IO_APIC_route_entry entry[0];
3022};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003023static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Pavel Machek438510f2005-04-16 15:25:24 -07003025static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 struct IO_APIC_route_entry *entry;
3028 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 data = container_of(dev, struct sysfs_ioapic_data, dev);
3032 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003033 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3034 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 return 0;
3037}
3038
3039static int ioapic_resume(struct sys_device *dev)
3040{
3041 struct IO_APIC_route_entry *entry;
3042 struct sysfs_ioapic_data *data;
3043 unsigned long flags;
3044 union IO_APIC_reg_00 reg_00;
3045 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 data = container_of(dev, struct sysfs_ioapic_data, dev);
3048 entry = data->entry;
3049
3050 spin_lock_irqsave(&ioapic_lock, flags);
3051 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05303052 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3053 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 io_apic_write(dev->id, 0, reg_00.raw);
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003057 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003058 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060 return 0;
3061}
3062
3063static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003064 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .suspend = ioapic_suspend,
3066 .resume = ioapic_resume,
3067};
3068
3069static int __init ioapic_init_sysfs(void)
3070{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003071 struct sys_device * dev;
3072 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 error = sysdev_class_register(&ioapic_sysdev_class);
3075 if (error)
3076 return error;
3077
Ingo Molnar54168ed2008-08-20 09:07:45 +02003078 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003079 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003081 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (!mp_ioapic_data[i]) {
3083 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3084 continue;
3085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003087 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 dev->cls = &ioapic_sysdev_class;
3089 error = sysdev_register(dev);
3090 if (error) {
3091 kfree(mp_ioapic_data[i]);
3092 mp_ioapic_data[i] = NULL;
3093 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3094 continue;
3095 }
3096 }
3097
3098 return 0;
3099}
3100
3101device_initcall(ioapic_init_sysfs);
3102
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003103static int nr_irqs_gsi = NR_IRQS_LEGACY;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003104/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003105 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003106 */
Yinghai Lud047f53a2009-04-27 18:02:23 -07003107unsigned int create_irq_nr(unsigned int irq_want, int node)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003108{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003109 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003110 unsigned int irq;
3111 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003112 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003113 struct irq_cfg *cfg_new = NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003114 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003115
3116 irq = 0;
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003117 if (irq_want < nr_irqs_gsi)
3118 irq_want = nr_irqs_gsi;
3119
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003120 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003121 for (new = irq_want; new < nr_irqs; new++) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07003122 desc_new = irq_to_desc_alloc_node(new, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003123 if (!desc_new) {
3124 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003125 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003126 }
3127 cfg_new = desc_new->chip_data;
3128
3129 if (cfg_new->vector != 0)
3130 continue;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003131
Yinghai Lu15e957d2009-04-30 01:17:50 -07003132 desc_new = move_irq_desc(desc_new, node);
Yinghai Lud047f53a2009-04-27 18:02:23 -07003133
Ingo Molnarfe402e12009-01-28 04:32:51 +01003134 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003135 irq = new;
3136 break;
3137 }
3138 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003139
Yinghai Lu199751d2008-08-19 20:50:27 -07003140 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003141 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003142 /* restore it, in case dynamic_irq_init clear it */
3143 if (desc_new)
3144 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003145 }
3146 return irq;
3147}
3148
Yinghai Lu199751d2008-08-19 20:50:27 -07003149int create_irq(void)
3150{
Yinghai Lud047f53a2009-04-27 18:02:23 -07003151 int node = cpu_to_node(boot_cpu_id);
Yinghai Lube5d5352008-12-05 18:58:33 -08003152 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003153 int irq;
3154
Yinghai Lube5d5352008-12-05 18:58:33 -08003155 irq_want = nr_irqs_gsi;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003156 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003157
3158 if (irq == 0)
3159 irq = -1;
3160
3161 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003162}
3163
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003164void destroy_irq(unsigned int irq)
3165{
3166 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003167 struct irq_cfg *cfg;
3168 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003169
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003170 /* store it, in case dynamic_irq_cleanup clear it */
3171 desc = irq_to_desc(irq);
3172 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003173 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003174 /* connect back irq_cfg */
Bartlomiej Zolnierkiewicz25f6e892009-07-30 23:21:18 +02003175 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003176
Ingo Molnar54168ed2008-08-20 09:07:45 +02003177 free_irte(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003178 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003179 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003180 spin_unlock_irqrestore(&vector_lock, flags);
3181}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003182
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003183/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003184 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003185 */
3186#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003187static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003188{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003189 struct irq_cfg *cfg;
3190 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003191 unsigned dest;
3192
Jan Beulichf1182632009-01-14 12:27:35 +00003193 if (disable_apic)
3194 return -ENXIO;
3195
Yinghai Lu3145e942008-12-05 18:58:34 -08003196 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003197 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003198 if (err)
3199 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003200
Ingo Molnardebccb32009-01-28 15:20:18 +01003201 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003202
Ingo Molnar54168ed2008-08-20 09:07:45 +02003203 if (irq_remapped(irq)) {
3204 struct irte irte;
3205 int ir_index;
3206 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003207
Ingo Molnar54168ed2008-08-20 09:07:45 +02003208 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3209 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003210
Ingo Molnar54168ed2008-08-20 09:07:45 +02003211 memset (&irte, 0, sizeof(irte));
3212
3213 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003214 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003215 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003216 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003217 irte.vector = cfg->vector;
3218 irte.dest_id = IRTE_DEST(dest);
3219
Weidong Hanf007e992009-05-23 00:41:15 +08003220 /* Set source-id of interrupt request */
3221 set_msi_sid(&irte, pdev);
3222
Ingo Molnar54168ed2008-08-20 09:07:45 +02003223 modify_irte(irq, &irte);
3224
3225 msg->address_hi = MSI_ADDR_BASE_HI;
3226 msg->data = sub_handle;
3227 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3228 MSI_ADDR_IR_SHV |
3229 MSI_ADDR_IR_INDEX1(ir_index) |
3230 MSI_ADDR_IR_INDEX2(ir_index);
Suresh Siddha29b61be2009-03-16 17:05:02 -07003231 } else {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003232 if (x2apic_enabled())
3233 msg->address_hi = MSI_ADDR_BASE_HI |
3234 MSI_ADDR_EXT_DEST_ID(dest);
3235 else
3236 msg->address_hi = MSI_ADDR_BASE_HI;
3237
Ingo Molnar54168ed2008-08-20 09:07:45 +02003238 msg->address_lo =
3239 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003240 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003241 MSI_ADDR_DEST_MODE_PHYSICAL:
3242 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003243 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003244 MSI_ADDR_REDIRECTION_CPU:
3245 MSI_ADDR_REDIRECTION_LOWPRI) |
3246 MSI_ADDR_DEST_ID(dest);
3247
3248 msg->data =
3249 MSI_DATA_TRIGGER_EDGE |
3250 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003251 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003252 MSI_DATA_DELIVERY_FIXED:
3253 MSI_DATA_DELIVERY_LOWPRI) |
3254 MSI_DATA_VECTOR(cfg->vector);
3255 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003256 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003257}
3258
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003259#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003260static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003261{
Yinghai Lu3145e942008-12-05 18:58:34 -08003262 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003263 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003264 struct msi_msg msg;
3265 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003266
Mike Travis22f65d32008-12-16 17:33:56 -08003267 dest = set_desc_affinity(desc, mask);
3268 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003269 return -1;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003270
Yinghai Lu3145e942008-12-05 18:58:34 -08003271 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003272
Yinghai Lu3145e942008-12-05 18:58:34 -08003273 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003274
3275 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003276 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003277 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3278 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3279
Yinghai Lu3145e942008-12-05 18:58:34 -08003280 write_msi_msg_desc(desc, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003281
3282 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003283}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003284#ifdef CONFIG_INTR_REMAP
3285/*
3286 * Migrate the MSI irq to another cpumask. This migration is
3287 * done in the process context using interrupt-remapping hardware.
3288 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07003289static int
Mike Travise7986732008-12-16 17:33:52 -08003290ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003291{
Yinghai Lu3145e942008-12-05 18:58:34 -08003292 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003293 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003294 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003295 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003296
3297 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003298 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003299
Mike Travis22f65d32008-12-16 17:33:56 -08003300 dest = set_desc_affinity(desc, mask);
3301 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003302 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003303
Ingo Molnar54168ed2008-08-20 09:07:45 +02003304 irte.vector = cfg->vector;
3305 irte.dest_id = IRTE_DEST(dest);
3306
3307 /*
3308 * atomically update the IRTE with the new destination and vector.
3309 */
3310 modify_irte(irq, &irte);
3311
3312 /*
3313 * After this point, all the interrupts will start arriving
3314 * at the new destination. So, time to cleanup the previous
3315 * vector allocation.
3316 */
Mike Travis22f65d32008-12-16 17:33:56 -08003317 if (cfg->move_in_progress)
3318 send_cleanup_vector(cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003319
3320 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003321}
Yinghai Lu3145e942008-12-05 18:58:34 -08003322
Ingo Molnar54168ed2008-08-20 09:07:45 +02003323#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003324#endif /* CONFIG_SMP */
3325
3326/*
3327 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3328 * which implement the MSI or MSI-X Capability Structure.
3329 */
3330static struct irq_chip msi_chip = {
3331 .name = "PCI-MSI",
3332 .unmask = unmask_msi_irq,
3333 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003334 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003335#ifdef CONFIG_SMP
3336 .set_affinity = set_msi_irq_affinity,
3337#endif
3338 .retrigger = ioapic_retrigger_irq,
3339};
3340
Ingo Molnar54168ed2008-08-20 09:07:45 +02003341static struct irq_chip msi_ir_chip = {
3342 .name = "IR-PCI-MSI",
3343 .unmask = unmask_msi_irq,
3344 .mask = mask_msi_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303345#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08003346 .ack = ir_ack_apic_edge,
Ingo Molnar54168ed2008-08-20 09:07:45 +02003347#ifdef CONFIG_SMP
3348 .set_affinity = ir_set_msi_irq_affinity,
3349#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303350#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02003351 .retrigger = ioapic_retrigger_irq,
3352};
3353
3354/*
3355 * Map the PCI dev to the corresponding remapping hardware unit
3356 * and allocate 'nvec' consecutive interrupt-remapping table entries
3357 * in it.
3358 */
3359static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3360{
3361 struct intel_iommu *iommu;
3362 int index;
3363
3364 iommu = map_dev_to_ir(dev);
3365 if (!iommu) {
3366 printk(KERN_ERR
3367 "Unable to map PCI %s to iommu\n", pci_name(dev));
3368 return -ENOENT;
3369 }
3370
3371 index = alloc_irte(iommu, irq, nvec);
3372 if (index < 0) {
3373 printk(KERN_ERR
3374 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003375 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003376 return -ENOSPC;
3377 }
3378 return index;
3379}
Yinghai Lu1d025192008-08-19 20:50:34 -07003380
Yinghai Lu3145e942008-12-05 18:58:34 -08003381static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003382{
3383 int ret;
3384 struct msi_msg msg;
3385
3386 ret = msi_compose_msg(dev, irq, &msg);
3387 if (ret < 0)
3388 return ret;
3389
Yinghai Lu3145e942008-12-05 18:58:34 -08003390 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003391 write_msi_msg(irq, &msg);
3392
Ingo Molnar54168ed2008-08-20 09:07:45 +02003393 if (irq_remapped(irq)) {
3394 struct irq_desc *desc = irq_to_desc(irq);
3395 /*
3396 * irq migration in process context
3397 */
3398 desc->status |= IRQ_MOVE_PCNTXT;
3399 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3400 } else
Ingo Molnar54168ed2008-08-20 09:07:45 +02003401 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003402
Yinghai Luc81bba42008-09-25 11:53:11 -07003403 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3404
Yinghai Lu1d025192008-08-19 20:50:34 -07003405 return 0;
3406}
3407
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003408int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3409{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003410 unsigned int irq;
3411 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003412 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003413 unsigned int irq_want;
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003414 struct intel_iommu *iommu = NULL;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003415 int index = 0;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003416 int node;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003417
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -04003418 /* x86 doesn't support multiple MSI yet */
3419 if (type == PCI_CAP_ID_MSI && nvec > 1)
3420 return 1;
3421
Yinghai Lud047f53a2009-04-27 18:02:23 -07003422 node = dev_to_node(&dev->dev);
Yinghai Lube5d5352008-12-05 18:58:33 -08003423 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003424 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003425 list_for_each_entry(msidesc, &dev->msi_list, list) {
Yinghai Lud047f53a2009-04-27 18:02:23 -07003426 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003427 if (irq == 0)
3428 return -1;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003429 irq_want = irq + 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003430 if (!intr_remapping_enabled)
3431 goto no_ir;
3432
3433 if (!sub_handle) {
3434 /*
3435 * allocate the consecutive block of IRTE's
3436 * for 'nvec'
3437 */
3438 index = msi_alloc_irte(dev, irq, nvec);
3439 if (index < 0) {
3440 ret = index;
3441 goto error;
3442 }
3443 } else {
3444 iommu = map_dev_to_ir(dev);
3445 if (!iommu) {
3446 ret = -ENOENT;
3447 goto error;
3448 }
3449 /*
3450 * setup the mapping between the irq and the IRTE
3451 * base index, the sub_handle pointing to the
3452 * appropriate interrupt remap table entry.
3453 */
3454 set_irte_irq(irq, iommu, index, sub_handle);
3455 }
3456no_ir:
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003457 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003458 if (ret < 0)
3459 goto error;
3460 sub_handle++;
3461 }
3462 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003463
3464error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003465 destroy_irq(irq);
3466 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003467}
3468
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003469void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003470{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003471 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003472}
3473
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003474#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003475#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003476static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003477{
Yinghai Lu3145e942008-12-05 18:58:34 -08003478 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003479 struct irq_cfg *cfg;
3480 struct msi_msg msg;
3481 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003482
Mike Travis22f65d32008-12-16 17:33:56 -08003483 dest = set_desc_affinity(desc, mask);
3484 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003485 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003486
Yinghai Lu3145e942008-12-05 18:58:34 -08003487 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003488
3489 dmar_msi_read(irq, &msg);
3490
3491 msg.data &= ~MSI_DATA_VECTOR_MASK;
3492 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3493 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3494 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3495
3496 dmar_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003497
3498 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003499}
Yinghai Lu3145e942008-12-05 18:58:34 -08003500
Ingo Molnar54168ed2008-08-20 09:07:45 +02003501#endif /* CONFIG_SMP */
3502
Jaswinder Singh Rajput8f7007a2009-06-10 12:41:01 -07003503static struct irq_chip dmar_msi_type = {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003504 .name = "DMAR_MSI",
3505 .unmask = dmar_msi_unmask,
3506 .mask = dmar_msi_mask,
3507 .ack = ack_apic_edge,
3508#ifdef CONFIG_SMP
3509 .set_affinity = dmar_msi_set_affinity,
3510#endif
3511 .retrigger = ioapic_retrigger_irq,
3512};
3513
3514int arch_setup_dmar_msi(unsigned int irq)
3515{
3516 int ret;
3517 struct msi_msg msg;
3518
3519 ret = msi_compose_msg(NULL, irq, &msg);
3520 if (ret < 0)
3521 return ret;
3522 dmar_msi_write(irq, &msg);
3523 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3524 "edge");
3525 return 0;
3526}
3527#endif
3528
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003529#ifdef CONFIG_HPET_TIMER
3530
3531#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003532static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003533{
Yinghai Lu3145e942008-12-05 18:58:34 -08003534 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003535 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003536 struct msi_msg msg;
3537 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003538
Mike Travis22f65d32008-12-16 17:33:56 -08003539 dest = set_desc_affinity(desc, mask);
3540 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003541 return -1;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003542
Yinghai Lu3145e942008-12-05 18:58:34 -08003543 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003544
3545 hpet_msi_read(irq, &msg);
3546
3547 msg.data &= ~MSI_DATA_VECTOR_MASK;
3548 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3549 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3550 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3551
3552 hpet_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003553
3554 return 0;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003555}
Yinghai Lu3145e942008-12-05 18:58:34 -08003556
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003557#endif /* CONFIG_SMP */
3558
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003559static struct irq_chip hpet_msi_type = {
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003560 .name = "HPET_MSI",
3561 .unmask = hpet_msi_unmask,
3562 .mask = hpet_msi_mask,
3563 .ack = ack_apic_edge,
3564#ifdef CONFIG_SMP
3565 .set_affinity = hpet_msi_set_affinity,
3566#endif
3567 .retrigger = ioapic_retrigger_irq,
3568};
3569
3570int arch_setup_hpet_msi(unsigned int irq)
3571{
3572 int ret;
3573 struct msi_msg msg;
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003574 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003575
3576 ret = msi_compose_msg(NULL, irq, &msg);
3577 if (ret < 0)
3578 return ret;
3579
3580 hpet_msi_write(irq, &msg);
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003581 desc->status |= IRQ_MOVE_PCNTXT;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003582 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3583 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003584
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003585 return 0;
3586}
3587#endif
3588
Ingo Molnar54168ed2008-08-20 09:07:45 +02003589#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003590/*
3591 * Hypertransport interrupt support
3592 */
3593#ifdef CONFIG_HT_IRQ
3594
3595#ifdef CONFIG_SMP
3596
Yinghai Lu497c9a12008-08-19 20:50:28 -07003597static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003598{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003599 struct ht_irq_msg msg;
3600 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003601
Yinghai Lu497c9a12008-08-19 20:50:28 -07003602 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003603 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003604
Yinghai Lu497c9a12008-08-19 20:50:28 -07003605 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003606 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003607
Eric W. Biedermanec683072006-11-08 17:44:57 -08003608 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003609}
3610
Yinghai Lud5dedd42009-04-27 17:59:21 -07003611static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003612{
Yinghai Lu3145e942008-12-05 18:58:34 -08003613 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003614 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003615 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003616
Mike Travis22f65d32008-12-16 17:33:56 -08003617 dest = set_desc_affinity(desc, mask);
3618 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003619 return -1;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003620
Yinghai Lu3145e942008-12-05 18:58:34 -08003621 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003622
Yinghai Lu497c9a12008-08-19 20:50:28 -07003623 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003624
3625 return 0;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003626}
Yinghai Lu3145e942008-12-05 18:58:34 -08003627
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003628#endif
3629
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003630static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003631 .name = "PCI-HT",
3632 .mask = mask_ht_irq,
3633 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003634 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003635#ifdef CONFIG_SMP
3636 .set_affinity = set_ht_irq_affinity,
3637#endif
3638 .retrigger = ioapic_retrigger_irq,
3639};
3640
3641int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3642{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003643 struct irq_cfg *cfg;
3644 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003645
Jan Beulichf1182632009-01-14 12:27:35 +00003646 if (disable_apic)
3647 return -ENXIO;
3648
Yinghai Lu3145e942008-12-05 18:58:34 -08003649 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003650 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003651 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003652 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003653 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003654
Ingo Molnardebccb32009-01-28 15:20:18 +01003655 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3656 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003657
Eric W. Biedermanec683072006-11-08 17:44:57 -08003658 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003659
Eric W. Biedermanec683072006-11-08 17:44:57 -08003660 msg.address_lo =
3661 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003662 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003663 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003664 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003665 HT_IRQ_LOW_DM_PHYSICAL :
3666 HT_IRQ_LOW_DM_LOGICAL) |
3667 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003668 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003669 HT_IRQ_LOW_MT_FIXED :
3670 HT_IRQ_LOW_MT_ARBITRATED) |
3671 HT_IRQ_LOW_IRQ_MASKED;
3672
Eric W. Biedermanec683072006-11-08 17:44:57 -08003673 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003674
Ingo Molnara460e742006-10-17 00:10:03 -07003675 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3676 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003677
3678 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003679 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003680 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003681}
3682#endif /* CONFIG_HT_IRQ */
3683
Nick Piggin03b48632009-01-20 04:36:04 +01003684#ifdef CONFIG_X86_UV
Dean Nelson4173a0e2008-10-02 12:18:21 -05003685/*
3686 * Re-target the irq to the specified CPU and enable the specified MMR located
3687 * on the specified blade to allow the sending of MSIs to the specified CPU.
3688 */
3689int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3690 unsigned long mmr_offset)
3691{
Mike Travis22f65d32008-12-16 17:33:56 -08003692 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003693 struct irq_cfg *cfg;
3694 int mmr_pnode;
3695 unsigned long mmr_value;
3696 struct uv_IO_APIC_route_entry *entry;
3697 unsigned long flags;
3698 int err;
3699
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003700 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3701
Yinghai Lu3145e942008-12-05 18:58:34 -08003702 cfg = irq_cfg(irq);
3703
Mike Travise7986732008-12-16 17:33:52 -08003704 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003705 if (err != 0)
3706 return err;
3707
3708 spin_lock_irqsave(&vector_lock, flags);
3709 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3710 irq_name);
3711 spin_unlock_irqrestore(&vector_lock, flags);
3712
Dean Nelson4173a0e2008-10-02 12:18:21 -05003713 mmr_value = 0;
3714 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003715 entry->vector = cfg->vector;
3716 entry->delivery_mode = apic->irq_delivery_mode;
3717 entry->dest_mode = apic->irq_dest_mode;
3718 entry->polarity = 0;
3719 entry->trigger = 0;
3720 entry->mask = 0;
3721 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003722
3723 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3724 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3725
3726 return irq;
3727}
3728
3729/*
3730 * Disable the specified MMR located on the specified blade so that MSIs are
3731 * longer allowed to be sent.
3732 */
3733void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3734{
3735 unsigned long mmr_value;
3736 struct uv_IO_APIC_route_entry *entry;
3737 int mmr_pnode;
3738
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003739 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3740
Dean Nelson4173a0e2008-10-02 12:18:21 -05003741 mmr_value = 0;
3742 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
Dean Nelson4173a0e2008-10-02 12:18:21 -05003743 entry->mask = 1;
3744
3745 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3746 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3747}
3748#endif /* CONFIG_X86_64 */
3749
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003750int __init io_apic_get_redir_entries (int ioapic)
3751{
3752 union IO_APIC_reg_01 reg_01;
3753 unsigned long flags;
3754
3755 spin_lock_irqsave(&ioapic_lock, flags);
3756 reg_01.raw = io_apic_read(ioapic, 1);
3757 spin_unlock_irqrestore(&ioapic_lock, flags);
3758
3759 return reg_01.bits.entries;
3760}
3761
Yinghai Lube5d5352008-12-05 18:58:33 -08003762void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003763{
Yinghai Lube5d5352008-12-05 18:58:33 -08003764 int nr = 0;
3765
Yinghai Lucc6c5002009-02-08 16:18:03 -08003766 nr = acpi_probe_gsi();
3767 if (nr > nr_irqs_gsi) {
Yinghai Lube5d5352008-12-05 18:58:33 -08003768 nr_irqs_gsi = nr;
Yinghai Lucc6c5002009-02-08 16:18:03 -08003769 } else {
3770 /* for acpi=off or acpi is not compiled in */
3771 int idx;
3772
3773 nr = 0;
3774 for (idx = 0; idx < nr_ioapics; idx++)
3775 nr += io_apic_get_redir_entries(idx) + 1;
3776
3777 if (nr > nr_irqs_gsi)
3778 nr_irqs_gsi = nr;
3779 }
3780
3781 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003782}
3783
Yinghai Lu4a046d12009-01-12 17:39:24 -08003784#ifdef CONFIG_SPARSE_IRQ
3785int __init arch_probe_nr_irqs(void)
3786{
3787 int nr;
3788
Yinghai Luf1ee5542009-02-08 16:18:03 -08003789 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3790 nr_irqs = NR_VECTORS * nr_cpu_ids;
Yinghai Lu4a046d12009-01-12 17:39:24 -08003791
Yinghai Luf1ee5542009-02-08 16:18:03 -08003792 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3793#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3794 /*
3795 * for MSI and HT dyn irq
3796 */
3797 nr += nr_irqs_gsi * 16;
3798#endif
3799 if (nr < nr_irqs)
Yinghai Lu4a046d12009-01-12 17:39:24 -08003800 nr_irqs = nr;
3801
3802 return 0;
3803}
3804#endif
3805
Yinghai Lue5198072009-05-15 13:05:16 -07003806static int __io_apic_set_pci_routing(struct device *dev, int irq,
3807 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003808{
3809 struct irq_desc *desc;
3810 struct irq_cfg *cfg;
3811 int node;
Yinghai Lue5198072009-05-15 13:05:16 -07003812 int ioapic, pin;
3813 int trigger, polarity;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003814
Yinghai Lue5198072009-05-15 13:05:16 -07003815 ioapic = irq_attr->ioapic;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003816 if (!IO_APIC_IRQ(irq)) {
3817 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3818 ioapic);
3819 return -EINVAL;
3820 }
3821
3822 if (dev)
3823 node = dev_to_node(dev);
3824 else
3825 node = cpu_to_node(boot_cpu_id);
3826
3827 desc = irq_to_desc_alloc_node(irq, node);
3828 if (!desc) {
3829 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3830 return 0;
3831 }
3832
Yinghai Lue5198072009-05-15 13:05:16 -07003833 pin = irq_attr->ioapic_pin;
3834 trigger = irq_attr->trigger;
3835 polarity = irq_attr->polarity;
3836
Yinghai Lu5ef21832009-05-06 10:08:50 -07003837 /*
3838 * IRQs < 16 are already in the irq_2_pin[] map
3839 */
3840 if (irq >= NR_IRQS_LEGACY) {
3841 cfg = desc->chip_data;
3842 add_pin_to_irq_node(cfg, node, ioapic, pin);
3843 }
3844
Yinghai Lue5198072009-05-15 13:05:16 -07003845 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003846
3847 return 0;
3848}
3849
Yinghai Lue5198072009-05-15 13:05:16 -07003850int io_apic_set_pci_routing(struct device *dev, int irq,
3851 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003852{
Yinghai Lue5198072009-05-15 13:05:16 -07003853 int ioapic, pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003854 /*
3855 * Avoid pin reprogramming. PRTs typically include entries
3856 * with redundant pin->gsi mappings (but unique PCI devices);
3857 * we only program the IOAPIC on the first.
3858 */
Yinghai Lue5198072009-05-15 13:05:16 -07003859 ioapic = irq_attr->ioapic;
3860 pin = irq_attr->ioapic_pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003861 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3862 pr_debug("Pin %d-%d already programmed\n",
3863 mp_ioapics[ioapic].apicid, pin);
3864 return 0;
3865 }
3866 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3867
Yinghai Lue5198072009-05-15 13:05:16 -07003868 return __io_apic_set_pci_routing(dev, irq, irq_attr);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003872 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 -------------------------------------------------------------------------- */
3874
Len Brown888ba6c2005-08-24 12:07:20 -04003875#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Ingo Molnar54168ed2008-08-20 09:07:45 +02003877#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003878int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
3880 union IO_APIC_reg_00 reg_00;
3881 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3882 physid_mask_t tmp;
3883 unsigned long flags;
3884 int i = 0;
3885
3886 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003887 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3888 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003890 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3892 * advantage of new APIC bus architecture.
3893 */
3894
3895 if (physids_empty(apic_id_map))
Ingo Molnard190cb82009-01-28 06:50:47 +01003896 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
3898 spin_lock_irqsave(&ioapic_lock, flags);
3899 reg_00.raw = io_apic_read(ioapic, 0);
3900 spin_unlock_irqrestore(&ioapic_lock, flags);
3901
3902 if (apic_id >= get_physical_broadcast()) {
3903 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3904 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3905 apic_id = reg_00.bits.ID;
3906 }
3907
3908 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003909 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 * 'stuck on smp_invalidate_needed IPI wait' messages.
3911 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003912 if (apic->check_apicid_used(apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 for (i = 0; i < get_physical_broadcast(); i++) {
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003915 if (!apic->check_apicid_used(apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 break;
3917 }
3918
3919 if (i == get_physical_broadcast())
3920 panic("Max apic_id exceeded!\n");
3921
3922 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3923 "trying %d\n", ioapic, apic_id, i);
3924
3925 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Ingo Molnar80587142009-01-28 06:50:47 +01003928 tmp = apic->apicid_to_cpu_present(apic_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 physids_or(apic_id_map, apic_id_map, tmp);
3930
3931 if (reg_00.bits.ID != apic_id) {
3932 reg_00.bits.ID = apic_id;
3933
3934 spin_lock_irqsave(&ioapic_lock, flags);
3935 io_apic_write(ioapic, 0, reg_00.raw);
3936 reg_00.raw = io_apic_read(ioapic, 0);
3937 spin_unlock_irqrestore(&ioapic_lock, flags);
3938
3939 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003940 if (reg_00.bits.ID != apic_id) {
3941 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3942 return -1;
3943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
3945
3946 apic_printk(APIC_VERBOSE, KERN_INFO
3947 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3948
3949 return apic_id;
3950}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00003951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003953int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 union IO_APIC_reg_01 reg_01;
3956 unsigned long flags;
3957
3958 spin_lock_irqsave(&ioapic_lock, flags);
3959 reg_01.raw = io_apic_read(ioapic, 1);
3960 spin_unlock_irqrestore(&ioapic_lock, flags);
3961
3962 return reg_01.bits.version;
3963}
3964
Shaohua Li61fd47e2007-11-17 01:05:28 -05003965int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3966{
3967 int i;
3968
3969 if (skip_ioapic_setup)
3970 return -1;
3971
3972 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05303973 if (mp_irqs[i].irqtype == mp_INT &&
3974 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003975 break;
3976 if (i >= mp_irq_entries)
3977 return -1;
3978
3979 *trigger = irq_trigger(i);
3980 *polarity = irq_polarity(i);
3981 return 0;
3982}
3983
Len Brown888ba6c2005-08-24 12:07:20 -04003984#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003985
Yinghai Lu497c9a12008-08-19 20:50:28 -07003986/*
3987 * This function currently is only a helper for the i386 smp boot process where
3988 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01003989 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07003990 */
3991#ifdef CONFIG_SMP
3992void __init setup_ioapic_dest(void)
3993{
Yinghai Lub9c61b702009-05-06 10:10:06 -07003994 int pin, ioapic = 0, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003995 struct irq_desc *desc;
Mike Travis22f65d32008-12-16 17:33:56 -08003996 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003997
3998 if (skip_ioapic_setup == 1)
3999 return;
4000
Yinghai Lub9c61b702009-05-06 10:10:06 -07004001#ifdef CONFIG_ACPI
4002 if (!acpi_disabled && acpi_ioapic) {
4003 ioapic = mp_find_ioapic(0);
4004 if (ioapic < 0)
4005 ioapic = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004006 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07004007#endif
4008
4009 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4010 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4011 if (irq_entry == -1)
4012 continue;
4013 irq = pin_2_irq(irq_entry, ioapic, pin);
4014
4015 desc = irq_to_desc(irq);
4016
4017 /*
4018 * Honour affinities which have been set in early boot
4019 */
4020 if (desc->status &
4021 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4022 mask = desc->affinity;
4023 else
4024 mask = apic->target_cpus();
4025
4026 if (intr_remapping_enabled)
4027 set_ir_ioapic_affinity_irq_desc(desc, mask);
4028 else
4029 set_ioapic_affinity_irq_desc(desc, mask);
4030 }
4031
Yinghai Lu497c9a12008-08-19 20:50:28 -07004032}
4033#endif
4034
Ingo Molnar54168ed2008-08-20 09:07:45 +02004035#define IOAPIC_RESOURCE_NAME_SIZE 11
4036
4037static struct resource *ioapic_resources;
4038
4039static struct resource * __init ioapic_setup_resources(void)
4040{
4041 unsigned long n;
4042 struct resource *res;
4043 char *mem;
4044 int i;
4045
4046 if (nr_ioapics <= 0)
4047 return NULL;
4048
4049 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4050 n *= nr_ioapics;
4051
4052 mem = alloc_bootmem(n);
4053 res = (void *)mem;
4054
4055 if (mem != NULL) {
4056 mem += sizeof(struct resource) * nr_ioapics;
4057
4058 for (i = 0; i < nr_ioapics; i++) {
4059 res[i].name = mem;
4060 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4061 sprintf(mem, "IOAPIC %u", i);
4062 mem += IOAPIC_RESOURCE_NAME_SIZE;
4063 }
4064 }
4065
4066 ioapic_resources = res;
4067
4068 return res;
4069}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004070
Yinghai Luf3294a32008-06-27 01:41:56 -07004071void __init ioapic_init_mappings(void)
4072{
4073 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004074 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004075 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004076
Ingo Molnar54168ed2008-08-20 09:07:45 +02004077 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004078 for (i = 0; i < nr_ioapics; i++) {
4079 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05304080 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004081#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004082 if (!ioapic_phys) {
4083 printk(KERN_ERR
4084 "WARNING: bogus zero IO-APIC "
4085 "address found in MPTABLE, "
4086 "disabling IO/APIC support!\n");
4087 smp_found_config = 0;
4088 skip_ioapic_setup = 1;
4089 goto fake_ioapic_page;
4090 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004091#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004092 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004093#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004094fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004095#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004096 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004097 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004098 ioapic_phys = __pa(ioapic_phys);
4099 }
4100 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004101 apic_printk(APIC_VERBOSE,
4102 "mapped IOAPIC to %08lx (%08lx)\n",
4103 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004104 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004105
Ingo Molnar54168ed2008-08-20 09:07:45 +02004106 if (ioapic_res != NULL) {
4107 ioapic_res->start = ioapic_phys;
4108 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4109 ioapic_res++;
4110 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004111 }
4112}
4113
Ingo Molnar54168ed2008-08-20 09:07:45 +02004114static int __init ioapic_insert_resources(void)
4115{
4116 int i;
4117 struct resource *r = ioapic_resources;
4118
4119 if (!r) {
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01004120 if (nr_ioapics > 0) {
4121 printk(KERN_ERR
4122 "IO APIC resources couldn't be allocated.\n");
4123 return -1;
4124 }
4125 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004126 }
4127
4128 for (i = 0; i < nr_ioapics; i++) {
4129 insert_resource(&iomem_resource, r);
4130 r++;
4131 }
4132
4133 return 0;
4134}
4135
4136/* Insert the IO APIC resources after PCI initialization has occured to handle
4137 * IO APICS that are mapped in on a BAR in PCI space. */
4138late_initcall(ioapic_insert_resources);