blob: 97e62a01f1e2966197dc7ba2590f236617f447da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mc146818rtc.h>
29#include <linux/compiler.h>
30#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/sysdev.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070033#include <linux/pci.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>
Julia Lawall1d16b532008-01-30 13:32:19 +010038#include <linux/jiffies.h> /* time_after() */
Ashok Raj54d5d422005-09-06 15:16:15 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41#include <asm/smp.h>
42#include <asm/desc.h>
43#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070044#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020045#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070046#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070047#include <asm/hypertransport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020050#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int (*ioapic_renumber_irq)(int ioapic, int irq);
53atomic_t irq_mis_count;
54
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080055/* Where if anywhere is the i8259 connect in external int mode */
56static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static DEFINE_SPINLOCK(ioapic_lock);
Jan Beulich0a1ad602006-06-26 13:56:43 +020059static DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Andi Kleenf9262c12006-03-08 17:57:25 -080061int timer_over_8254 __initdata = 1;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Is the SiS APIC rmw bug present ?
65 * -1 = don't know, 0 = no, 1 = yes
66 */
67int sis_apic_bug = -1;
68
69/*
70 * # of IRQ routing registers
71 */
72int nr_ioapic_registers[MAX_IO_APICS];
73
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040074/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040075struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040076int nr_ioapics;
77
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040078/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040079struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040080
81/* # of MP IRQ source entries */
82int mp_irq_entries;
83
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040084#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
85int mp_bus_id_to_type[MAX_MP_BUSSES];
86#endif
87
88DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
89
Rusty Russell1a3f2392006-09-26 10:52:32 +020090static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * Rough estimation of how many shared IRQs there are, can
94 * be changed anytime.
95 */
96#define MAX_PLUS_SHARED_IRQS NR_IRQS
97#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
98
99/*
100 * This is performance-critical, we want to do it O(1)
101 *
102 * the indexing order of this array favors 1:1 mappings
103 * between pins and IRQs.
104 */
105
106static struct irq_pin_list {
107 int apic, pin, next;
108} irq_2_pin[PIN_MAP_SIZE];
109
Linus Torvalds130fe052006-11-01 09:11:00 -0800110struct io_apic {
111 unsigned int index;
112 unsigned int unused[3];
113 unsigned int data;
114};
115
116static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
117{
118 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400119 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800120}
121
122static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
123{
124 struct io_apic __iomem *io_apic = io_apic_base(apic);
125 writel(reg, &io_apic->index);
126 return readl(&io_apic->data);
127}
128
129static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
130{
131 struct io_apic __iomem *io_apic = io_apic_base(apic);
132 writel(reg, &io_apic->index);
133 writel(value, &io_apic->data);
134}
135
136/*
137 * Re-write a value: to be used for read-modify-write
138 * cycles where the read already set up the index register.
139 *
140 * Older SiS APIC requires we rewrite the index register
141 */
142static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
143{
Al Virocb468982007-02-09 16:39:25 +0000144 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800145 if (sis_apic_bug)
146 writel(reg, &io_apic->index);
147 writel(value, &io_apic->data);
148}
149
Andi Kleencf4c6a22006-09-26 10:52:30 +0200150union entry_union {
151 struct { u32 w1, w2; };
152 struct IO_APIC_route_entry entry;
153};
154
155static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
156{
157 union entry_union eu;
158 unsigned long flags;
159 spin_lock_irqsave(&ioapic_lock, flags);
160 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
161 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
162 spin_unlock_irqrestore(&ioapic_lock, flags);
163 return eu.entry;
164}
165
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800166/*
167 * When we write a new IO APIC routing entry, we need to write the high
168 * word first! If the mask bit in the low word is clear, we will enable
169 * the interrupt, and we need to make sure the entry is fully populated
170 * before that happens.
171 */
Andi Kleend15512f2006-12-07 02:14:07 +0100172static void
173__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
174{
175 union entry_union eu;
176 eu.entry = e;
177 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
178 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
179}
180
Andi Kleencf4c6a22006-09-26 10:52:30 +0200181static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
182{
183 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200184 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100185 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800186 spin_unlock_irqrestore(&ioapic_lock, flags);
187}
188
189/*
190 * When we mask an IO APIC routing entry, we need to write the low
191 * word first, in order to set the mask bit before we change the
192 * high bits!
193 */
194static void ioapic_mask_entry(int apic, int pin)
195{
196 unsigned long flags;
197 union entry_union eu = { .entry.mask = 1 };
198
199 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200200 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
201 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
202 spin_unlock_irqrestore(&ioapic_lock, flags);
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
207 * shared ISA-space IRQs, so we have to support them. We are super
208 * fast in the common case, and fast for shared ISA-space IRQs.
209 */
210static void add_pin_to_irq(unsigned int irq, int apic, int pin)
211{
212 static int first_free_entry = NR_IRQS;
213 struct irq_pin_list *entry = irq_2_pin + irq;
214
215 while (entry->next)
216 entry = irq_2_pin + entry->next;
217
218 if (entry->pin != -1) {
219 entry->next = first_free_entry;
220 entry = irq_2_pin + entry->next;
221 if (++first_free_entry >= PIN_MAP_SIZE)
222 panic("io_apic.c: whoops");
223 }
224 entry->apic = apic;
225 entry->pin = pin;
226}
227
228/*
229 * Reroute an IRQ to a different pin.
230 */
231static void __init replace_pin_at_irq(unsigned int irq,
232 int oldapic, int oldpin,
233 int newapic, int newpin)
234{
235 struct irq_pin_list *entry = irq_2_pin + irq;
236
237 while (1) {
238 if (entry->apic == oldapic && entry->pin == oldpin) {
239 entry->apic = newapic;
240 entry->pin = newpin;
241 }
242 if (!entry->next)
243 break;
244 entry = irq_2_pin + entry->next;
245 }
246}
247
248static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
249{
250 struct irq_pin_list *entry = irq_2_pin + irq;
251 unsigned int pin, reg;
252
253 for (;;) {
254 pin = entry->pin;
255 if (pin == -1)
256 break;
257 reg = io_apic_read(entry->apic, 0x10 + pin*2);
258 reg &= ~disable;
259 reg |= enable;
260 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
261 if (!entry->next)
262 break;
263 entry = irq_2_pin + entry->next;
264 }
265}
266
267/* mask = 1 */
268static void __mask_IO_APIC_irq (unsigned int irq)
269{
270 __modify_IO_APIC_irq(irq, 0x00010000, 0);
271}
272
273/* mask = 0 */
274static void __unmask_IO_APIC_irq (unsigned int irq)
275{
276 __modify_IO_APIC_irq(irq, 0, 0x00010000);
277}
278
279/* mask = 1, trigger = 0 */
280static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
281{
282 __modify_IO_APIC_irq(irq, 0x00010000, 0x00008000);
283}
284
285/* mask = 0, trigger = 1 */
286static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
287{
288 __modify_IO_APIC_irq(irq, 0x00008000, 0x00010000);
289}
290
291static void mask_IO_APIC_irq (unsigned int irq)
292{
293 unsigned long flags;
294
295 spin_lock_irqsave(&ioapic_lock, flags);
296 __mask_IO_APIC_irq(irq);
297 spin_unlock_irqrestore(&ioapic_lock, flags);
298}
299
300static void unmask_IO_APIC_irq (unsigned int irq)
301{
302 unsigned long flags;
303
304 spin_lock_irqsave(&ioapic_lock, flags);
305 __unmask_IO_APIC_irq(irq);
306 spin_unlock_irqrestore(&ioapic_lock, flags);
307}
308
309static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
310{
311 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200314 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (entry.delivery_mode == dest_SMI)
316 return;
317
318 /*
319 * Disable it in the IO-APIC irq-routing table:
320 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800321 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324static void clear_IO_APIC (void)
325{
326 int apic, pin;
327
328 for (apic = 0; apic < nr_ioapics; apic++)
329 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
330 clear_IO_APIC_pin(apic, pin);
331}
332
Ashok Raj54d5d422005-09-06 15:16:15 -0700333#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
335{
336 unsigned long flags;
337 int pin;
338 struct irq_pin_list *entry = irq_2_pin + irq;
339 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700340 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Ashok Raj54d5d422005-09-06 15:16:15 -0700342 cpus_and(tmp, cpumask, cpu_online_map);
343 if (cpus_empty(tmp))
344 tmp = TARGET_CPUS;
345
346 cpus_and(cpumask, tmp, CPU_MASK_ALL);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 apicid_value = cpu_mask_to_apicid(cpumask);
349 /* Prepare to do the io_apic_write */
350 apicid_value = apicid_value << 24;
351 spin_lock_irqsave(&ioapic_lock, flags);
352 for (;;) {
353 pin = entry->pin;
354 if (pin == -1)
355 break;
356 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
357 if (!entry->next)
358 break;
359 entry = irq_2_pin + entry->next;
360 }
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700361 irq_desc[irq].affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 spin_unlock_irqrestore(&ioapic_lock, flags);
363}
364
365#if defined(CONFIG_IRQBALANCE)
366# include <asm/processor.h> /* kernel_thread() */
367# include <linux/kernel_stat.h> /* kstat */
368# include <linux/slab.h> /* kmalloc() */
Julia Lawall1d16b532008-01-30 13:32:19 +0100369# include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#define IRQBALANCE_CHECK_ARCH -999
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700372#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
373#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
374#define BALANCED_IRQ_MORE_DELTA (HZ/10)
375#define BALANCED_IRQ_LESS_DELTA (HZ)
376
377static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
378static int physical_balance __read_mostly;
379static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381static struct irq_cpu_info {
382 unsigned long * last_irq;
383 unsigned long * irq_delta;
384 unsigned long irq;
385} irq_cpu_data[NR_CPUS];
386
387#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
388#define LAST_CPU_IRQ(cpu,irq) (irq_cpu_data[cpu].last_irq[irq])
389#define IRQ_DELTA(cpu,irq) (irq_cpu_data[cpu].irq_delta[irq])
390
391#define IDLE_ENOUGH(cpu,now) \
392 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
393
394#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
395
Mike Travisd5a74302007-10-16 01:24:05 -0700396#define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700398static cpumask_t balance_irq_affinity[NR_IRQS] = {
399 [0 ... NR_IRQS-1] = CPU_MASK_ALL
400};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700402void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
403{
404 balance_irq_affinity[irq] = mask;
405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
408 unsigned long now, int direction)
409{
410 int search_idle = 1;
411 int cpu = curr_cpu;
412
413 goto inside;
414
415 do {
416 if (unlikely(cpu == curr_cpu))
417 search_idle = 0;
418inside:
419 if (direction == 1) {
420 cpu++;
421 if (cpu >= NR_CPUS)
422 cpu = 0;
423 } else {
424 cpu--;
425 if (cpu == -1)
426 cpu = NR_CPUS-1;
427 }
428 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
429 (search_idle && !IDLE_ENOUGH(cpu,now)));
430
431 return cpu;
432}
433
434static inline void balance_irq(int cpu, int irq)
435{
436 unsigned long now = jiffies;
437 cpumask_t allowed_mask;
438 unsigned int new_cpu;
439
440 if (irqbalance_disabled)
441 return;
442
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700443 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 new_cpu = move(cpu, allowed_mask, now, 1);
445 if (cpu != new_cpu) {
Ashok Raj54d5d422005-09-06 15:16:15 -0700446 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448}
449
450static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
451{
452 int i, j;
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200453
Andrew Morton394e3902006-03-23 03:01:05 -0800454 for_each_online_cpu(i) {
455 for (j = 0; j < NR_IRQS; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!irq_desc[j].action)
457 continue;
458 /* Is it a significant load ? */
459 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
460 useful_load_threshold)
461 continue;
462 balance_irq(i, j);
463 }
464 }
465 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
466 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
467 return;
468}
469
470static void do_irq_balance(void)
471{
472 int i, j;
473 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
474 unsigned long move_this_load = 0;
475 int max_loaded = 0, min_loaded = 0;
476 int load;
477 unsigned long useful_load_threshold = balanced_irq_interval + 10;
478 int selected_irq;
479 int tmp_loaded, first_attempt = 1;
480 unsigned long tmp_cpu_irq;
481 unsigned long imbalance = 0;
482 cpumask_t allowed_mask, target_cpu_mask, tmp;
483
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800484 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int package_index;
486 CPU_IRQ(i) = 0;
487 if (!cpu_online(i))
488 continue;
489 package_index = CPU_TO_PACKAGEINDEX(i);
490 for (j = 0; j < NR_IRQS; j++) {
491 unsigned long value_now, delta;
Thomas Gleixner950f4422007-02-16 01:27:24 -0800492 /* Is this an active IRQ or balancing disabled ? */
493 if (!irq_desc[j].action || irq_balancing_disabled(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 continue;
495 if ( package_index == i )
496 IRQ_DELTA(package_index,j) = 0;
497 /* Determine the total count per processor per IRQ */
498 value_now = (unsigned long) kstat_cpu(i).irqs[j];
499
500 /* Determine the activity per processor per IRQ */
501 delta = value_now - LAST_CPU_IRQ(i,j);
502
503 /* Update last_cpu_irq[][] for the next time */
504 LAST_CPU_IRQ(i,j) = value_now;
505
506 /* Ignore IRQs whose rate is less than the clock */
507 if (delta < useful_load_threshold)
508 continue;
509 /* update the load for the processor or package total */
510 IRQ_DELTA(package_index,j) += delta;
511
512 /* Keep track of the higher numbered sibling as well */
513 if (i != package_index)
514 CPU_IRQ(i) += delta;
515 /*
516 * We have sibling A and sibling B in the package
517 *
518 * cpu_irq[A] = load for cpu A + load for cpu B
519 * cpu_irq[B] = load for cpu B
520 */
521 CPU_IRQ(package_index) += delta;
522 }
523 }
524 /* Find the least loaded processor package */
Andrew Morton394e3902006-03-23 03:01:05 -0800525 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (i != CPU_TO_PACKAGEINDEX(i))
527 continue;
528 if (min_cpu_irq > CPU_IRQ(i)) {
529 min_cpu_irq = CPU_IRQ(i);
530 min_loaded = i;
531 }
532 }
533 max_cpu_irq = ULONG_MAX;
534
535tryanothercpu:
536 /* Look for heaviest loaded processor.
537 * We may come back to get the next heaviest loaded processor.
538 * Skip processors with trivial loads.
539 */
540 tmp_cpu_irq = 0;
541 tmp_loaded = -1;
Andrew Morton394e3902006-03-23 03:01:05 -0800542 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (i != CPU_TO_PACKAGEINDEX(i))
544 continue;
545 if (max_cpu_irq <= CPU_IRQ(i))
546 continue;
547 if (tmp_cpu_irq < CPU_IRQ(i)) {
548 tmp_cpu_irq = CPU_IRQ(i);
549 tmp_loaded = i;
550 }
551 }
552
553 if (tmp_loaded == -1) {
554 /* In the case of small number of heavy interrupt sources,
555 * loading some of the cpus too much. We use Ingo's original
556 * approach to rotate them around.
557 */
558 if (!first_attempt && imbalance >= useful_load_threshold) {
559 rotate_irqs_among_cpus(useful_load_threshold);
560 return;
561 }
562 goto not_worth_the_effort;
563 }
564
565 first_attempt = 0; /* heaviest search */
566 max_cpu_irq = tmp_cpu_irq; /* load */
567 max_loaded = tmp_loaded; /* processor */
568 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* if imbalance is less than approx 10% of max load, then
571 * observe diminishing returns action. - quit
572 */
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200573 if (imbalance < (max_cpu_irq >> 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto not_worth_the_effort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576tryanotherirq:
577 /* if we select an IRQ to move that can't go where we want, then
578 * see if there is another one to try.
579 */
580 move_this_load = 0;
581 selected_irq = -1;
582 for (j = 0; j < NR_IRQS; j++) {
583 /* Is this an active IRQ? */
584 if (!irq_desc[j].action)
585 continue;
586 if (imbalance <= IRQ_DELTA(max_loaded,j))
587 continue;
588 /* Try to find the IRQ that is closest to the imbalance
589 * without going over.
590 */
591 if (move_this_load < IRQ_DELTA(max_loaded,j)) {
592 move_this_load = IRQ_DELTA(max_loaded,j);
593 selected_irq = j;
594 }
595 }
596 if (selected_irq == -1) {
597 goto tryanothercpu;
598 }
599
600 imbalance = move_this_load;
601
Simon Arlott27b46d72007-10-20 01:13:56 +0200602 /* For physical_balance case, we accumulated both load
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * values in the one of the siblings cpu_irq[],
604 * to use the same code for physical and logical processors
605 * as much as possible.
606 *
607 * NOTE: the cpu_irq[] array holds the sum of the load for
608 * sibling A and sibling B in the slot for the lowest numbered
609 * sibling (A), _AND_ the load for sibling B in the slot for
610 * the higher numbered sibling.
611 *
612 * We seek the least loaded sibling by making the comparison
613 * (A+B)/2 vs B
614 */
615 load = CPU_IRQ(min_loaded) >> 1;
Mike Travisd5a74302007-10-16 01:24:05 -0700616 for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (load > CPU_IRQ(j)) {
618 /* This won't change cpu_sibling_map[min_loaded] */
619 load = CPU_IRQ(j);
620 min_loaded = j;
621 }
622 }
623
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700624 cpus_and(allowed_mask,
625 cpu_online_map,
626 balance_irq_affinity[selected_irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 target_cpu_mask = cpumask_of_cpu(min_loaded);
628 cpus_and(tmp, target_cpu_mask, allowed_mask);
629
630 if (!cpus_empty(tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* mark for change destination */
Ashok Raj54d5d422005-09-06 15:16:15 -0700632 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Since we made a change, come back sooner to
635 * check for more variation.
636 */
637 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
638 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
639 return;
640 }
641 goto tryanotherirq;
642
643not_worth_the_effort:
644 /*
645 * if we did not find an IRQ to move, then adjust the time interval
646 * upward
647 */
648 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
649 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
651}
652
653static int balanced_irq(void *unused)
654{
655 int i;
656 unsigned long prev_balance_time = jiffies;
657 long time_remaining = balanced_irq_interval;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* push everything to CPU 0 to give us a starting point. */
660 for (i = 0 ; i < NR_IRQS ; i++) {
Ingo Molnarcd916d32006-06-29 02:24:42 -0700661 irq_desc[i].pending_mask = cpumask_of_cpu(0);
Ashok Raj54d5d422005-09-06 15:16:15 -0700662 set_pending_irq(i, cpumask_of_cpu(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700665 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 for ( ; ; ) {
Nishanth Aravamudan52e6e632005-09-10 00:27:26 -0700667 time_remaining = schedule_timeout_interruptible(time_remaining);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700668 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (time_after(jiffies,
670 prev_balance_time+balanced_irq_interval)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700671 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 do_irq_balance();
673 prev_balance_time = jiffies;
674 time_remaining = balanced_irq_interval;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700675 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 }
678 return 0;
679}
680
681static int __init balanced_irq_init(void)
682{
683 int i;
684 struct cpuinfo_x86 *c;
685 cpumask_t tmp;
686
687 cpus_shift_right(tmp, cpu_online_map, 2);
688 c = &boot_cpu_data;
689 /* When not overwritten by the command line ask subarchitecture. */
690 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
691 irqbalance_disabled = NO_BALANCE_IRQ;
692 if (irqbalance_disabled)
693 return 0;
694
695 /* disable irqbalance completely if there is only one processor online */
696 if (num_online_cpus() < 2) {
697 irqbalance_disabled = 1;
698 return 0;
699 }
700 /*
701 * Enable physical balance only if more than 1 physical processor
702 * is present
703 */
704 if (smp_num_siblings > 1 && !cpus_empty(tmp))
705 physical_balance = 1;
706
Andrew Morton394e3902006-03-23 03:01:05 -0800707 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
709 irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
710 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
711 printk(KERN_ERR "balanced_irq_init: out of memory");
712 goto failed;
713 }
714 memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
715 memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
716 }
717
718 printk(KERN_INFO "Starting balanced_irq\n");
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200719 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200721 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722failed:
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800723 for_each_possible_cpu(i) {
Jesper Juhl4ae66732005-06-25 14:58:48 -0700724 kfree(irq_cpu_data[i].irq_delta);
Andrew Morton394e3902006-03-23 03:01:05 -0800725 irq_cpu_data[i].irq_delta = NULL;
Jesper Juhl4ae66732005-06-25 14:58:48 -0700726 kfree(irq_cpu_data[i].last_irq);
Andrew Morton394e3902006-03-23 03:01:05 -0800727 irq_cpu_data[i].last_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 return 0;
730}
731
Andrew Mortonc2481cc2007-04-08 16:04:04 -0700732int __devinit irqbalance_disable(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 irqbalance_disabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800735 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738__setup("noirqbalance", irqbalance_disable);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740late_initcall(balanced_irq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741#endif /* CONFIG_IRQBALANCE */
Ashok Raj54d5d422005-09-06 15:16:15 -0700742#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100745void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 unsigned int cfg;
748
749 /*
750 * Wait for idle.
751 */
752 apic_wait_icr_idle();
753 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
754 /*
755 * Send the IPI. The write to APIC_ICR fires this off.
756 */
757 apic_write_around(APIC_ICR, cfg);
758}
759#endif /* !CONFIG_SMP */
760
761
762/*
763 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
764 * specific CPU-side IRQs.
765 */
766
767#define MAX_PIRQS 8
768static int pirq_entries [MAX_PIRQS];
769static int pirqs_enabled;
770int skip_ioapic_setup;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static int __init ioapic_pirq_setup(char *str)
773{
774 int i, max;
775 int ints[MAX_PIRQS+1];
776
777 get_options(str, ARRAY_SIZE(ints), ints);
778
779 for (i = 0; i < MAX_PIRQS; i++)
780 pirq_entries[i] = -1;
781
782 pirqs_enabled = 1;
783 apic_printk(APIC_VERBOSE, KERN_INFO
784 "PIRQ redirection, working around broken MP-BIOS.\n");
785 max = MAX_PIRQS;
786 if (ints[0] < MAX_PIRQS)
787 max = ints[0];
788
789 for (i = 0; i < max; i++) {
790 apic_printk(APIC_VERBOSE, KERN_DEBUG
791 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
792 /*
793 * PIRQs are mapped upside down, usually.
794 */
795 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
796 }
797 return 1;
798}
799
800__setup("pirq=", ioapic_pirq_setup);
801
802/*
803 * Find the IRQ entry number of a certain pin.
804 */
805static int find_irq_entry(int apic, int pin, int type)
806{
807 int i;
808
809 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400810 if (mp_irqs[i].mp_irqtype == type &&
811 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
812 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
813 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return i;
815
816 return -1;
817}
818
819/*
820 * Find the pin to which IRQ[irq] (ISA) is connected
821 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800822static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 int i;
825
826 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400827 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300829 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400830 (mp_irqs[i].mp_irqtype == type) &&
831 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400833 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 return -1;
836}
837
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800838static int __init find_isa_irq_apic(int irq, int type)
839{
840 int i;
841
842 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400843 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800844
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300845 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400846 (mp_irqs[i].mp_irqtype == type) &&
847 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800848 break;
849 }
850 if (i < mp_irq_entries) {
851 int apic;
852 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400853 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800854 return apic;
855 }
856 }
857
858 return -1;
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/*
862 * Find a specific PCI IRQ entry.
863 * Not an __init, possibly needed by modules
864 */
865static int pin_2_irq(int idx, int apic, int pin);
866
867int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
868{
869 int apic, i, best_guess = -1;
870
871 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
872 "slot:%d, pin:%d.\n", bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400873 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
875 return -1;
876 }
877 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400878 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400881 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
882 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300885 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400886 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400888 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
889 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (!(apic || IO_APIC_IRQ(irq)))
892 continue;
893
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400894 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return irq;
896 /*
897 * Use the first all-but-pin matching entry as a
898 * best-guess fuzzy result for broken mptables.
899 */
900 if (best_guess < 0)
901 best_guess = irq;
902 }
903 }
904 return best_guess;
905}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700906EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908/*
909 * This function currently is only a helper for the i386 smp boot process where
910 * we need to reprogram the ioredtbls to cater for the cpus which have come online
911 * so mask in all cases should simply be TARGET_CPUS
912 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700913#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914void __init setup_ioapic_dest(void)
915{
916 int pin, ioapic, irq, irq_entry;
917
918 if (skip_ioapic_setup == 1)
919 return;
920
921 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
922 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
923 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
924 if (irq_entry == -1)
925 continue;
926 irq = pin_2_irq(irq_entry, ioapic, pin);
927 set_ioapic_affinity_irq(irq, TARGET_CPUS);
928 }
929
930 }
931}
Ashok Raj54d5d422005-09-06 15:16:15 -0700932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300934#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/*
936 * EISA Edge/Level control register, ELCR
937 */
938static int EISA_ELCR(unsigned int irq)
939{
940 if (irq < 16) {
941 unsigned int port = 0x4d0 + (irq >> 3);
942 return (inb(port) >> (irq & 7)) & 1;
943 }
944 apic_printk(APIC_VERBOSE, KERN_INFO
945 "Broken MPtable reports ISA irq %d\n", irq);
946 return 0;
947}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300950/* ISA interrupts are always polarity zero edge triggered,
951 * when listed as conforming in the MP table. */
952
953#define default_ISA_trigger(idx) (0)
954#define default_ISA_polarity(idx) (0)
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/* EISA interrupts are always polarity zero and can be edge or level
957 * trigger depending on the ELCR value. If an interrupt is listed as
958 * EISA conforming in the MP table, that means its trigger type must
959 * be read in from the ELCR */
960
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400961#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300962#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964/* PCI interrupts are always polarity one level triggered,
965 * when listed as conforming in the MP table. */
966
967#define default_PCI_trigger(idx) (1)
968#define default_PCI_polarity(idx) (1)
969
970/* MCA interrupts are always polarity zero level triggered,
971 * when listed as conforming in the MP table. */
972
973#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300974#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Shaohua Li61fd47e2007-11-17 01:05:28 -0500976static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400978 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int polarity;
980
981 /*
982 * Determine IRQ line polarity (high active or low active):
983 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400984 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 {
986 case 0: /* conforms, ie. bus-type dependent polarity */
987 {
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300988 polarity = test_bit(bus, mp_bus_not_pci)?
989 default_ISA_polarity(idx):
990 default_PCI_polarity(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 }
993 case 1: /* high active */
994 {
995 polarity = 0;
996 break;
997 }
998 case 2: /* reserved */
999 {
1000 printk(KERN_WARNING "broken BIOS!!\n");
1001 polarity = 1;
1002 break;
1003 }
1004 case 3: /* low active */
1005 {
1006 polarity = 1;
1007 break;
1008 }
1009 default: /* invalid */
1010 {
1011 printk(KERN_WARNING "broken BIOS!!\n");
1012 polarity = 1;
1013 break;
1014 }
1015 }
1016 return polarity;
1017}
1018
1019static int MPBIOS_trigger(int idx)
1020{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001021 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int trigger;
1023
1024 /*
1025 * Determine IRQ trigger mode (edge or level sensitive):
1026 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001027 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 {
1029 case 0: /* conforms, ie. bus-type dependent */
1030 {
Alexey Starikovskiy9c0076c2008-03-20 14:54:43 +03001031 trigger = test_bit(bus, mp_bus_not_pci)?
1032 default_ISA_trigger(idx):
1033 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001034#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 switch (mp_bus_id_to_type[bus])
1036 {
1037 case MP_BUS_ISA: /* ISA pin */
1038 {
Alexey Starikovskiy9c0076c2008-03-20 14:54:43 +03001039 /* set before the switch */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
1041 }
1042 case MP_BUS_EISA: /* EISA pin */
1043 {
1044 trigger = default_EISA_trigger(idx);
1045 break;
1046 }
1047 case MP_BUS_PCI: /* PCI pin */
1048 {
Alexey Starikovskiy9c0076c2008-03-20 14:54:43 +03001049 /* set before the switch */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 }
1052 case MP_BUS_MCA: /* MCA pin */
1053 {
1054 trigger = default_MCA_trigger(idx);
1055 break;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 default:
1058 {
1059 printk(KERN_WARNING "broken BIOS!!\n");
1060 trigger = 1;
1061 break;
1062 }
1063 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001064#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066 }
1067 case 1: /* edge */
1068 {
1069 trigger = 0;
1070 break;
1071 }
1072 case 2: /* reserved */
1073 {
1074 printk(KERN_WARNING "broken BIOS!!\n");
1075 trigger = 1;
1076 break;
1077 }
1078 case 3: /* level */
1079 {
1080 trigger = 1;
1081 break;
1082 }
1083 default: /* invalid */
1084 {
1085 printk(KERN_WARNING "broken BIOS!!\n");
1086 trigger = 0;
1087 break;
1088 }
1089 }
1090 return trigger;
1091}
1092
1093static inline int irq_polarity(int idx)
1094{
1095 return MPBIOS_polarity(idx);
1096}
1097
1098static inline int irq_trigger(int idx)
1099{
1100 return MPBIOS_trigger(idx);
1101}
1102
1103static int pin_2_irq(int idx, int apic, int pin)
1104{
1105 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001106 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /*
1109 * Debugging check, we are in big trouble if this message pops up!
1110 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001111 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1113
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001114 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001115 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001116 else {
1117 /*
1118 * PCI IRQs are mapped in order
1119 */
1120 i = irq = 0;
1121 while (i < apic)
1122 irq += nr_ioapic_registers[i++];
1123 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001125 /*
1126 * For MPS mode, so far only needed by ES7000 platform
1127 */
1128 if (ioapic_renumber_irq)
1129 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
1132 /*
1133 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1134 */
1135 if ((pin >= 16) && (pin <= 23)) {
1136 if (pirq_entries[pin-16] != -1) {
1137 if (!pirq_entries[pin-16]) {
1138 apic_printk(APIC_VERBOSE, KERN_DEBUG
1139 "disabling PIRQ%d\n", pin-16);
1140 } else {
1141 irq = pirq_entries[pin-16];
1142 apic_printk(APIC_VERBOSE, KERN_DEBUG
1143 "using PIRQ%d -> IRQ %d\n",
1144 pin-16, irq);
1145 }
1146 }
1147 }
1148 return irq;
1149}
1150
1151static inline int IO_APIC_irq_trigger(int irq)
1152{
1153 int apic, idx, pin;
1154
1155 for (apic = 0; apic < nr_ioapics; apic++) {
1156 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1157 idx = find_irq_entry(apic,pin,mp_INT);
1158 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1159 return irq_trigger(idx);
1160 }
1161 }
1162 /*
1163 * nonexistent IRQs are edge default
1164 */
1165 return 0;
1166}
1167
1168/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Adrian Bunk7e95b592006-12-07 02:14:11 +01001169static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001171static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Eric W. Biederman8339f002007-01-29 13:19:05 -07001173 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001174 int vector, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001176 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
Jan Beulich0a1ad602006-06-26 13:56:43 +02001177
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001178 if (irq_vector[irq] > 0)
1179 return irq_vector[irq];
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001180
Jan Beulich0a1ad602006-06-26 13:56:43 +02001181 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001182 offset = current_offset;
1183next:
1184 vector += 8;
1185 if (vector >= FIRST_SYSTEM_VECTOR) {
1186 offset = (offset + 1) % 8;
1187 vector = FIRST_DEVICE_VECTOR + offset;
1188 }
1189 if (vector == current_vector)
1190 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001191 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -07001192 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001193
1194 current_vector = vector;
1195 current_offset = offset;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001196 irq_vector[irq] = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +02001197
1198 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001201static int assign_irq_vector(int irq)
1202{
1203 unsigned long flags;
1204 int vector;
1205
1206 spin_lock_irqsave(&vector_lock, flags);
1207 vector = __assign_irq_vector(irq);
1208 spin_unlock_irqrestore(&vector_lock, flags);
1209
1210 return vector;
1211}
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001212static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214#define IOAPIC_AUTO -1
1215#define IOAPIC_EDGE 0
1216#define IOAPIC_LEVEL 1
1217
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001218static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Jan Beulich6ebcc002006-06-26 13:56:46 +02001220 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001221 trigger == IOAPIC_LEVEL) {
1222 irq_desc[irq].status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001223 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1224 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001225 } else {
1226 irq_desc[irq].status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001227 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1228 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001229 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001230 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static void __init setup_IO_APIC_irqs(void)
1234{
1235 struct IO_APIC_route_entry entry;
1236 int apic, pin, idx, irq, first_notcon = 1, vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1239
1240 for (apic = 0; apic < nr_ioapics; apic++) {
1241 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1242
1243 /*
1244 * add it to the IO-APIC irq-routing table:
1245 */
1246 memset(&entry,0,sizeof(entry));
1247
1248 entry.delivery_mode = INT_DELIVERY_MODE;
1249 entry.dest_mode = INT_DEST_MODE;
1250 entry.mask = 0; /* enable IRQ */
1251 entry.dest.logical.logical_dest =
1252 cpu_mask_to_apicid(TARGET_CPUS);
1253
1254 idx = find_irq_entry(apic,pin,mp_INT);
1255 if (idx == -1) {
1256 if (first_notcon) {
1257 apic_printk(APIC_VERBOSE, KERN_DEBUG
1258 " IO-APIC (apicid-pin) %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001259 mp_ioapics[apic].mp_apicid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 pin);
1261 first_notcon = 0;
1262 } else
1263 apic_printk(APIC_VERBOSE, ", %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001264 mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 continue;
1266 }
1267
Yinghai Lu20d225b2007-10-17 18:04:41 +02001268 if (!first_notcon) {
1269 apic_printk(APIC_VERBOSE, " not connected.\n");
1270 first_notcon = 1;
1271 }
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 entry.trigger = irq_trigger(idx);
1274 entry.polarity = irq_polarity(idx);
1275
1276 if (irq_trigger(idx)) {
1277 entry.trigger = 1;
1278 entry.mask = 1;
1279 }
1280
1281 irq = pin_2_irq(idx, apic, pin);
1282 /*
1283 * skip adding the timer int on secondary nodes, which causes
1284 * a small but painful rift in the time-space continuum
1285 */
1286 if (multi_timer_check(apic, irq))
1287 continue;
1288 else
1289 add_pin_to_irq(irq, apic, pin);
1290
1291 if (!apic && !IO_APIC_IRQ(irq))
1292 continue;
1293
1294 if (IO_APIC_IRQ(irq)) {
1295 vector = assign_irq_vector(irq);
1296 entry.vector = vector;
1297 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1298
1299 if (!apic && (irq < 16))
1300 disable_8259A_irq(irq);
1301 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09001302 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 }
1305
1306 if (!first_notcon)
1307 apic_printk(APIC_VERBOSE, " not connected.\n");
1308}
1309
1310/*
1311 * Set up the 8259A-master output pin:
1312 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001313static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 memset(&entry,0,sizeof(entry));
1318
1319 disable_8259A_irq(0);
1320
1321 /* mask LVT0 */
1322 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1323
1324 /*
1325 * We use logical delivery to get the timer IRQ
1326 * to the first CPU.
1327 */
1328 entry.dest_mode = INT_DEST_MODE;
1329 entry.mask = 0; /* unmask IRQ now */
1330 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1331 entry.delivery_mode = INT_DELIVERY_MODE;
1332 entry.polarity = 0;
1333 entry.trigger = 0;
1334 entry.vector = vector;
1335
1336 /*
1337 * The timer IRQ doesn't have to know that behind the
1338 * scene we have a 8259A-master in AEOI mode ...
1339 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001340 irq_desc[0].chip = &ioapic_chip;
1341 set_irq_handler(0, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /*
1344 * Add it to the IO-APIC irq-routing table:
1345 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001346 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 enable_8259A_irq(0);
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351void __init print_IO_APIC(void)
1352{
1353 int apic, i;
1354 union IO_APIC_reg_00 reg_00;
1355 union IO_APIC_reg_01 reg_01;
1356 union IO_APIC_reg_02 reg_02;
1357 union IO_APIC_reg_03 reg_03;
1358 unsigned long flags;
1359
1360 if (apic_verbosity == APIC_QUIET)
1361 return;
1362
1363 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1364 for (i = 0; i < nr_ioapics; i++)
1365 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001366 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
1369 * We are a bit conservative about what we expect. We have to
1370 * know about every hardware change ASAP.
1371 */
1372 printk(KERN_INFO "testing the IO APIC.......................\n");
1373
1374 for (apic = 0; apic < nr_ioapics; apic++) {
1375
1376 spin_lock_irqsave(&ioapic_lock, flags);
1377 reg_00.raw = io_apic_read(apic, 0);
1378 reg_01.raw = io_apic_read(apic, 1);
1379 if (reg_01.bits.version >= 0x10)
1380 reg_02.raw = io_apic_read(apic, 2);
1381 if (reg_01.bits.version >= 0x20)
1382 reg_03.raw = io_apic_read(apic, 3);
1383 spin_unlock_irqrestore(&ioapic_lock, flags);
1384
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001385 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1387 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1388 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1389 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1392 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1395 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /*
1398 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1399 * but the value of reg_02 is read as the previous read register
1400 * value, so ignore it if reg_02 == reg_01.
1401 */
1402 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1403 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1404 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
1407 /*
1408 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1409 * or reg_03, but the value of reg_0[23] is read as the previous read
1410 * register value, so ignore it if reg_03 == reg_0[12].
1411 */
1412 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1413 reg_03.raw != reg_01.raw) {
1414 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1415 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1419
1420 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1421 " Stat Dest Deli Vect: \n");
1422
1423 for (i = 0; i <= reg_01.bits.entries; i++) {
1424 struct IO_APIC_route_entry entry;
1425
Andi Kleencf4c6a22006-09-26 10:52:30 +02001426 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 printk(KERN_DEBUG " %02x %03X %02X ",
1429 i,
1430 entry.dest.logical.logical_dest,
1431 entry.dest.physical.physical_dest
1432 );
1433
1434 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1435 entry.mask,
1436 entry.trigger,
1437 entry.irr,
1438 entry.polarity,
1439 entry.delivery_status,
1440 entry.dest_mode,
1441 entry.delivery_mode,
1442 entry.vector
1443 );
1444 }
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1447 for (i = 0; i < NR_IRQS; i++) {
1448 struct irq_pin_list *entry = irq_2_pin + i;
1449 if (entry->pin < 0)
1450 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001451 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 for (;;) {
1453 printk("-> %d:%d", entry->apic, entry->pin);
1454 if (!entry->next)
1455 break;
1456 entry = irq_2_pin + entry->next;
1457 }
1458 printk("\n");
1459 }
1460
1461 printk(KERN_INFO ".................................... done.\n");
1462
1463 return;
1464}
1465
1466#if 0
1467
1468static void print_APIC_bitfield (int base)
1469{
1470 unsigned int v;
1471 int i, j;
1472
1473 if (apic_verbosity == APIC_QUIET)
1474 return;
1475
1476 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1477 for (i = 0; i < 8; i++) {
1478 v = apic_read(base + i*0x10);
1479 for (j = 0; j < 32; j++) {
1480 if (v & (1<<j))
1481 printk("1");
1482 else
1483 printk("0");
1484 }
1485 printk("\n");
1486 }
1487}
1488
1489void /*__init*/ print_local_APIC(void * dummy)
1490{
1491 unsigned int v, ver, maxlvt;
1492
1493 if (apic_verbosity == APIC_QUIET)
1494 return;
1495
1496 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1497 smp_processor_id(), hard_smp_processor_id());
Jack Steiner05f2d122008-03-28 14:12:02 -05001498 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
1499 GET_APIC_ID(read_apic_id()));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 v = apic_read(APIC_LVR);
1501 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1502 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001503 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 v = apic_read(APIC_TASKPRI);
1506 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1507
1508 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1509 v = apic_read(APIC_ARBPRI);
1510 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1511 v & APIC_ARBPRI_MASK);
1512 v = apic_read(APIC_PROCPRI);
1513 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1514 }
1515
1516 v = apic_read(APIC_EOI);
1517 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1518 v = apic_read(APIC_RRR);
1519 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1520 v = apic_read(APIC_LDR);
1521 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1522 v = apic_read(APIC_DFR);
1523 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1524 v = apic_read(APIC_SPIV);
1525 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1526
1527 printk(KERN_DEBUG "... APIC ISR field:\n");
1528 print_APIC_bitfield(APIC_ISR);
1529 printk(KERN_DEBUG "... APIC TMR field:\n");
1530 print_APIC_bitfield(APIC_TMR);
1531 printk(KERN_DEBUG "... APIC IRR field:\n");
1532 print_APIC_bitfield(APIC_IRR);
1533
1534 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1535 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1536 apic_write(APIC_ESR, 0);
1537 v = apic_read(APIC_ESR);
1538 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1539 }
1540
1541 v = apic_read(APIC_ICR);
1542 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1543 v = apic_read(APIC_ICR2);
1544 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1545
1546 v = apic_read(APIC_LVTT);
1547 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1548
1549 if (maxlvt > 3) { /* PC is LVT#4. */
1550 v = apic_read(APIC_LVTPC);
1551 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1552 }
1553 v = apic_read(APIC_LVT0);
1554 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1555 v = apic_read(APIC_LVT1);
1556 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1557
1558 if (maxlvt > 2) { /* ERR is LVT#3. */
1559 v = apic_read(APIC_LVTERR);
1560 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1561 }
1562
1563 v = apic_read(APIC_TMICT);
1564 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1565 v = apic_read(APIC_TMCCT);
1566 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1567 v = apic_read(APIC_TDCR);
1568 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1569 printk("\n");
1570}
1571
1572void print_all_local_APICs (void)
1573{
1574 on_each_cpu(print_local_APIC, NULL, 1, 1);
1575}
1576
1577void /*__init*/ print_PIC(void)
1578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 unsigned int v;
1580 unsigned long flags;
1581
1582 if (apic_verbosity == APIC_QUIET)
1583 return;
1584
1585 printk(KERN_DEBUG "\nprinting PIC contents\n");
1586
1587 spin_lock_irqsave(&i8259A_lock, flags);
1588
1589 v = inb(0xa1) << 8 | inb(0x21);
1590 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1591
1592 v = inb(0xa0) << 8 | inb(0x20);
1593 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1594
1595 outb(0x0b,0xa0);
1596 outb(0x0b,0x20);
1597 v = inb(0xa0) << 8 | inb(0x20);
1598 outb(0x0a,0xa0);
1599 outb(0x0a,0x20);
1600
1601 spin_unlock_irqrestore(&i8259A_lock, flags);
1602
1603 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1604
1605 v = inb(0x4d1) << 8 | inb(0x4d0);
1606 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1607}
1608
1609#endif /* 0 */
1610
1611static void __init enable_IO_APIC(void)
1612{
1613 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001614 int i8259_apic, i8259_pin;
1615 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 unsigned long flags;
1617
1618 for (i = 0; i < PIN_MAP_SIZE; i++) {
1619 irq_2_pin[i].pin = -1;
1620 irq_2_pin[i].next = 0;
1621 }
1622 if (!pirqs_enabled)
1623 for (i = 0; i < MAX_PIRQS; i++)
1624 pirq_entries[i] = -1;
1625
1626 /*
1627 * The number of IO-APIC IRQ registers (== #pins):
1628 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001629 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001631 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001633 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1634 }
1635 for(apic = 0; apic < nr_ioapics; apic++) {
1636 int pin;
1637 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001638 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001639 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001640 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001641
1642
1643 /* If the interrupt line is enabled and in ExtInt mode
1644 * I have found the pin where the i8259 is connected.
1645 */
1646 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1647 ioapic_i8259.apic = apic;
1648 ioapic_i8259.pin = pin;
1649 goto found_i8259;
1650 }
1651 }
1652 }
1653 found_i8259:
1654 /* Look to see what if the MP table has reported the ExtINT */
1655 /* If we could not find the appropriate pin by looking at the ioapic
1656 * the i8259 probably is not connected the ioapic but give the
1657 * mptable a chance anyway.
1658 */
1659 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1660 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1661 /* Trust the MP table if nothing is setup in the hardware */
1662 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1663 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1664 ioapic_i8259.pin = i8259_pin;
1665 ioapic_i8259.apic = i8259_apic;
1666 }
1667 /* Complain if the MP table and the hardware disagree */
1668 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1669 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1670 {
1671 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 /*
1675 * Do not trust the IO-APIC being empty at bootup
1676 */
1677 clear_IO_APIC();
1678}
1679
1680/*
1681 * Not an __init, needed by the reboot code
1682 */
1683void disable_IO_APIC(void)
1684{
1685 /*
1686 * Clear the IO-APIC before rebooting:
1687 */
1688 clear_IO_APIC();
1689
Eric W. Biederman650927e2005-06-25 14:57:44 -07001690 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001691 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001692 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001693 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001694 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001695 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001696 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001697
1698 memset(&entry, 0, sizeof(entry));
1699 entry.mask = 0; /* Enabled */
1700 entry.trigger = 0; /* Edge */
1701 entry.irr = 0;
1702 entry.polarity = 0; /* High */
1703 entry.delivery_status = 0;
1704 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001705 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001706 entry.vector = 0;
Vivek Goyal76865c32006-01-06 00:12:19 -08001707 entry.dest.physical.physical_dest =
Jack Steiner05f2d122008-03-28 14:12:02 -05001708 GET_APIC_ID(read_apic_id());
Eric W. Biederman650927e2005-06-25 14:57:44 -07001709
1710 /*
1711 * Add it to the IO-APIC irq-routing table:
1712 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001713 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001714 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001715 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718/*
1719 * function to set the IO-APIC physical IDs based on the
1720 * values stored in the MPC table.
1721 *
1722 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1723 */
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725static void __init setup_ioapic_ids_from_mpc(void)
1726{
1727 union IO_APIC_reg_00 reg_00;
1728 physid_mask_t phys_id_present_map;
1729 int apic;
1730 int i;
1731 unsigned char old_id;
1732 unsigned long flags;
1733
Yinghai Lud49c4282008-06-08 18:31:54 -07001734#ifdef CONFIG_X86_NUMAQ
1735 if (found_numaq)
1736 return;
1737#endif
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001740 * Don't check I/O APIC IDs for xAPIC systems. They have
1741 * no meaning without the serial APIC bus.
1742 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001743 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1744 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001745 return;
1746 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * This is broken; anything with a real cpu count has to
1748 * circumvent this idiocy regardless.
1749 */
1750 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1751
1752 /*
1753 * Set the IOAPIC ID to the value stored in the MPC table.
1754 */
1755 for (apic = 0; apic < nr_ioapics; apic++) {
1756
1757 /* Read the register 0 value */
1758 spin_lock_irqsave(&ioapic_lock, flags);
1759 reg_00.raw = io_apic_read(apic, 0);
1760 spin_unlock_irqrestore(&ioapic_lock, flags);
1761
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001762 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001764 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001766 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1768 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001769 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /*
1773 * Sanity check, is the ID really free? Every APIC in a
1774 * system must have a unique ID or we get lots of nice
1775 * 'stuck on smp_invalidate_needed IPI wait' messages.
1776 */
1777 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001778 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001780 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 for (i = 0; i < get_physical_broadcast(); i++)
1782 if (!physid_isset(i, phys_id_present_map))
1783 break;
1784 if (i >= get_physical_broadcast())
1785 panic("Max APIC ID exceeded!\n");
1786 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1787 i);
1788 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001789 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 } else {
1791 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001792 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 apic_printk(APIC_VERBOSE, "Setting %d in the "
1794 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001795 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1797 }
1798
1799
1800 /*
1801 * We need to adjust the IRQ routing table
1802 * if the ID changed.
1803 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001804 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001806 if (mp_irqs[i].mp_dstapic == old_id)
1807 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001808 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /*
1811 * Read the right value from the MPC table and
1812 * write it into the ID register.
1813 */
1814 apic_printk(APIC_VERBOSE, KERN_INFO
1815 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001816 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001818 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 spin_lock_irqsave(&ioapic_lock, flags);
1820 io_apic_write(apic, 0, reg_00.raw);
1821 spin_unlock_irqrestore(&ioapic_lock, flags);
1822
1823 /*
1824 * Sanity check
1825 */
1826 spin_lock_irqsave(&ioapic_lock, flags);
1827 reg_00.raw = io_apic_read(apic, 0);
1828 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001829 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 printk("could not set ID!\n");
1831 else
1832 apic_printk(APIC_VERBOSE, " ok.\n");
1833 }
1834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001836int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001837
1838static int __init notimercheck(char *s)
1839{
1840 no_timer_check = 1;
1841 return 1;
1842}
1843__setup("no_timer_check", notimercheck);
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845/*
1846 * There is a nasty bug in some older SMP boards, their mptable lies
1847 * about the timer IRQ. We do the following to work around the situation:
1848 *
1849 * - timer IRQ defaults to IO-APIC IRQ
1850 * - if this function detects that timer IRQs are defunct, then we fall
1851 * back to ISA timer IRQs
1852 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001853static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001856 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Zachary Amsden8542b202006-12-07 02:14:09 +01001858 if (no_timer_check)
1859 return 1;
1860
Ingo Molnar4aae0702007-12-18 18:05:58 +01001861 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 local_irq_enable();
1863 /* Let ten ticks pass... */
1864 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001865 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /*
1868 * Expect a few ticks at least, to be sure some possible
1869 * glue logic does not lock up after one or two first
1870 * ticks in a non-ExtINT mode. Also the local APIC
1871 * might have cached one ExtINT interrupt. Finally, at
1872 * least one tick may be lost due to delays.
1873 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001874 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 1;
1876
1877 return 0;
1878}
1879
1880/*
1881 * In the SMP+IOAPIC case it might happen that there are an unspecified
1882 * number of pending IRQ events unhandled. These cases are very rare,
1883 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1884 * better to do it this way as thus we do not have to be aware of
1885 * 'pending' interrupts in the IRQ path, except at this point.
1886 */
1887/*
1888 * Edge triggered needs to resend any interrupt
1889 * that was delayed but this is now handled in the device
1890 * independent code.
1891 */
1892
1893/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001894 * Startup quirk:
1895 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 * Starting up a edge-triggered IO-APIC interrupt is
1897 * nasty - we need to make sure that we get the edge.
1898 * If it is already asserted for some reason, we need
1899 * return 1 to indicate that is was pending.
1900 *
1901 * This is not complete - we should be able to fake
1902 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001903 *
1904 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001906static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 int was_pending = 0;
1909 unsigned long flags;
1910
1911 spin_lock_irqsave(&ioapic_lock, flags);
1912 if (irq < 16) {
1913 disable_8259A_irq(irq);
1914 if (i8259A_irq_pending(irq))
1915 was_pending = 1;
1916 }
1917 __unmask_IO_APIC_irq(irq);
1918 spin_unlock_irqrestore(&ioapic_lock, flags);
1919
1920 return was_pending;
1921}
1922
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001923static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001925 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 ack_APIC_irq();
1927}
1928
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001929static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 unsigned long v;
1932 int i;
1933
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001934 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935/*
1936 * It appears there is an erratum which affects at least version 0x11
1937 * of I/O APIC (that's the 82093AA and cores integrated into various
1938 * chipsets). Under certain conditions a level-triggered interrupt is
1939 * erroneously delivered as edge-triggered one but the respective IRR
1940 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1941 * message but it will never arrive and further interrupts are blocked
1942 * from the source. The exact reason is so far unknown, but the
1943 * phenomenon was observed when two consecutive interrupt requests
1944 * from a given source get delivered to the same CPU and the source is
1945 * temporarily disabled in between.
1946 *
1947 * A workaround is to simulate an EOI message manually. We achieve it
1948 * by setting the trigger mode to edge and then to level when the edge
1949 * trigger mode gets detected in the TMR of a local APIC for a
1950 * level-triggered interrupt. We mask the source for the time of the
1951 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1952 * The idea is from Manfred Spraul. --macro
1953 */
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001954 i = irq_vector[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1957
1958 ack_APIC_irq();
1959
1960 if (!(v & (1 << (i & 0x1f)))) {
1961 atomic_inc(&irq_mis_count);
1962 spin_lock(&ioapic_lock);
1963 __mask_and_edge_IO_APIC_irq(irq);
1964 __unmask_and_level_IO_APIC_irq(irq);
1965 spin_unlock(&ioapic_lock);
1966 }
1967}
1968
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001969static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001971 send_IPI_self(irq_vector[irq]);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001972
1973 return 1;
1974}
1975
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001976static struct irq_chip ioapic_chip __read_mostly = {
1977 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001978 .startup = startup_ioapic_irq,
1979 .mask = mask_IO_APIC_irq,
1980 .unmask = unmask_IO_APIC_irq,
1981 .ack = ack_ioapic_irq,
1982 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001983#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001984 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001985#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001986 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987};
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990static inline void init_IO_APIC_traps(void)
1991{
1992 int irq;
1993
1994 /*
1995 * NOTE! The local APIC isn't very good at handling
1996 * multiple interrupts at the same interrupt level.
1997 * As the interrupt level is determined by taking the
1998 * vector number and shifting that right by 4, we
1999 * want to spread these out a bit so that they don't
2000 * all fall in the same interrupt level.
2001 *
2002 * Also, we've got to be careful not to trash gate
2003 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2004 */
2005 for (irq = 0; irq < NR_IRQS ; irq++) {
Akinobu Mitaaddfc662008-04-05 22:39:07 +09002006 if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * Hmm.. We don't have an entry for this,
2009 * so default to an old-fashioned 8259
2010 * interrupt if we can..
2011 */
2012 if (irq < 16)
2013 make_8259A_irq(irq);
2014 else
2015 /* Strange. Oh, well.. */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002016 irq_desc[irq].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 }
2019}
2020
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002021/*
2022 * The local APIC irq-chip implementation:
2023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002025static void ack_apic(unsigned int irq)
2026{
2027 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002030static void mask_lapic_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 unsigned long v;
2033
2034 v = apic_read(APIC_LVT0);
2035 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2036}
2037
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002038static void unmask_lapic_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002040 unsigned long v;
2041
2042 v = apic_read(APIC_LVT0);
2043 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002046static struct irq_chip lapic_chip __read_mostly = {
2047 .name = "local-APIC-edge",
2048 .mask = mask_lapic_irq,
2049 .unmask = unmask_lapic_irq,
2050 .eoi = ack_apic,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051};
2052
Jan Beuliche9427102008-01-30 13:31:24 +01002053static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 /*
2056 * Dirty trick to enable the NMI watchdog ...
2057 * We put the 8259A master into AEOI mode and
2058 * unmask on all local APICs LVT0 as NMI.
2059 *
2060 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2061 * is from Maciej W. Rozycki - so we do not have to EOI from
2062 * the NMI handler or the timer interrupt.
2063 */
2064 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2065
Jan Beuliche9427102008-01-30 13:31:24 +01002066 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 apic_printk(APIC_VERBOSE, " done.\n");
2069}
2070
2071/*
2072 * This looks a bit hackish but it's about the only one way of sending
2073 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2074 * not support the ExtINT mode, unfortunately. We need to send these
2075 * cycles as some i82489DX-based boards have glue logic that keeps the
2076 * 8259A interrupt line asserted until INTA. --macro
2077 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002078static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002080 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct IO_APIC_route_entry entry0, entry1;
2082 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002084 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002085 if (pin == -1) {
2086 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002088 }
2089 apic = find_isa_irq_apic(8, mp_INT);
2090 if (apic == -1) {
2091 WARN_ON_ONCE(1);
2092 return;
2093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Andi Kleencf4c6a22006-09-26 10:52:30 +02002095 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002096 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 memset(&entry1, 0, sizeof(entry1));
2099
2100 entry1.dest_mode = 0; /* physical delivery */
2101 entry1.mask = 0; /* unmask IRQ now */
2102 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2103 entry1.delivery_mode = dest_ExtINT;
2104 entry1.polarity = entry0.polarity;
2105 entry1.trigger = 0;
2106 entry1.vector = 0;
2107
Andi Kleencf4c6a22006-09-26 10:52:30 +02002108 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 save_control = CMOS_READ(RTC_CONTROL);
2111 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2112 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2113 RTC_FREQ_SELECT);
2114 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2115
2116 i = 100;
2117 while (i-- > 0) {
2118 mdelay(10);
2119 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2120 i -= 10;
2121 }
2122
2123 CMOS_WRITE(save_control, RTC_CONTROL);
2124 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002125 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Andi Kleencf4c6a22006-09-26 10:52:30 +02002127 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/*
2131 * This code may look a bit paranoid, but it's supposed to cooperate with
2132 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2133 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2134 * fanatically on his truly buggy board.
2135 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002136static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002138 int apic1, pin1, apic2, pin2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 int vector;
Ingo Molnar6e908942008-03-21 14:32:36 +01002140 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002141 unsigned long flags;
2142
2143 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002144
Ingo Molnar6e908942008-03-21 14:32:36 +01002145 ver = apic_read(APIC_LVR);
2146 ver = GET_APIC_VERSION(ver);
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 /*
2149 * get/set the timer IRQ vector:
2150 */
2151 disable_8259A_irq(0);
2152 vector = assign_irq_vector(0);
2153 set_intr_gate(vector, interrupt[0]);
2154
2155 /*
2156 * Subtle, code in do_timer_interrupt() expects an AEOI
2157 * mode for the 8259A whenever interrupts are routed
2158 * through I/O APICs. Also IRQ0 has to be enabled in
2159 * the 8259A which implies the virtual wire has to be
Ingo Molnar6e908942008-03-21 14:32:36 +01002160 * disabled in the local APIC. Finally timer interrupts
2161 * need to be acknowledged manually in the 8259A for
2162 * timer_interrupt() and for the i82489DX when using
2163 * the NMI watchdog.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
2165 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2166 init_8259A(1);
Ingo Molnar6e908942008-03-21 14:32:36 +01002167 timer_ack = !cpu_has_tsc;
2168 timer_ack |= (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Andi Kleenf9262c12006-03-08 17:57:25 -08002169 if (timer_over_8254 > 0)
2170 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002172 pin1 = find_isa_irq_pin(0, mp_INT);
2173 apic1 = find_isa_irq_apic(0, mp_INT);
2174 pin2 = ioapic_i8259.pin;
2175 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002177 printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2178 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 if (pin1 != -1) {
2181 /*
2182 * Ok, does IRQ0 through the IOAPIC work?
2183 */
2184 unmask_IO_APIC_irq(0);
2185 if (timer_irq_works()) {
2186 if (nmi_watchdog == NMI_IO_APIC) {
2187 disable_8259A_irq(0);
2188 setup_nmi();
2189 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002191 if (disable_timer_pin_1 > 0)
2192 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002195 clear_IO_APIC_pin(apic1, pin1);
2196 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to "
2197 "IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
2200 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
2201 if (pin2 != -1) {
2202 printk("\n..... (found pin %d) ...", pin2);
2203 /*
2204 * legacy devices should be connected to IO APIC #0
2205 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002206 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (timer_irq_works()) {
2208 printk("works.\n");
2209 if (pin1 != -1)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002210 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 else
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002212 add_pin_to_irq(0, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (nmi_watchdog == NMI_IO_APIC) {
2214 setup_nmi();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002216 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218 /*
2219 * Cleanup, just in case ...
2220 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002221 clear_IO_APIC_pin(apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223 printk(" failed.\n");
2224
2225 if (nmi_watchdog == NMI_IO_APIC) {
2226 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2227 nmi_watchdog = 0;
2228 }
2229
2230 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2231
2232 disable_8259A_irq(0);
Ingo Molnara460e742006-10-17 00:10:03 -07002233 set_irq_chip_and_handler_name(0, &lapic_chip, handle_fasteoi_irq,
Maciej W. Rozycki2e188932007-02-13 13:26:20 +01002234 "fasteoi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2236 enable_8259A_irq(0);
2237
2238 if (timer_irq_works()) {
2239 printk(" works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2243 printk(" failed.\n");
2244
2245 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2246
2247 timer_ack = 0;
2248 init_8259A(0);
2249 make_8259A_irq(0);
2250 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2251
2252 unlock_ExtINT_logic();
2253
2254 if (timer_irq_works()) {
2255 printk(" works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 printk(" failed :(.\n");
2259 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2260 "report. Then try booting with the 'noapic' option");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002261out:
2262 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265/*
2266 *
2267 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2268 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2269 * Linux doesn't really care, as it's not actually used
2270 * for any interrupt handling anyway.
2271 */
2272#define PIC_IRQS (1 << PIC_CASCADE_IR)
2273
2274void __init setup_IO_APIC(void)
2275{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002276 int i;
2277
2278 /* Reserve all the system vectors. */
2279 for (i = FIRST_SYSTEM_VECTOR; i < NR_VECTORS; i++)
2280 set_bit(i, used_vectors);
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 enable_IO_APIC();
2283
2284 if (acpi_ioapic)
2285 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2286 else
2287 io_apic_irqs = ~PIC_IRQS;
2288
2289 printk("ENABLING IO-APIC IRQs\n");
2290
2291 /*
2292 * Set up IO-APIC IRQ routing.
2293 */
2294 if (!acpi_ioapic)
2295 setup_ioapic_ids_from_mpc();
2296 sync_Arb_IDs();
2297 setup_IO_APIC_irqs();
2298 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002299 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (!acpi_ioapic)
2301 print_IO_APIC();
2302}
2303
Andi Kleenf9262c12006-03-08 17:57:25 -08002304static int __init setup_disable_8254_timer(char *s)
2305{
2306 timer_over_8254 = -1;
2307 return 1;
2308}
2309static int __init setup_enable_8254_timer(char *s)
2310{
2311 timer_over_8254 = 2;
2312 return 1;
2313}
2314
2315__setup("disable_8254_timer", setup_disable_8254_timer);
2316__setup("enable_8254_timer", setup_enable_8254_timer);
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318/*
2319 * Called after all the initialization is done. If we didnt find any
2320 * APIC bugs then we can allow the modify fast path
2321 */
2322
2323static int __init io_apic_bug_finalize(void)
2324{
2325 if(sis_apic_bug == -1)
2326 sis_apic_bug = 0;
2327 return 0;
2328}
2329
2330late_initcall(io_apic_bug_finalize);
2331
2332struct sysfs_ioapic_data {
2333 struct sys_device dev;
2334 struct IO_APIC_route_entry entry[0];
2335};
2336static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2337
Pavel Machek438510f2005-04-16 15:25:24 -07002338static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 struct IO_APIC_route_entry *entry;
2341 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int i;
2343
2344 data = container_of(dev, struct sysfs_ioapic_data, dev);
2345 entry = data->entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02002346 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2347 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 return 0;
2350}
2351
2352static int ioapic_resume(struct sys_device *dev)
2353{
2354 struct IO_APIC_route_entry *entry;
2355 struct sysfs_ioapic_data *data;
2356 unsigned long flags;
2357 union IO_APIC_reg_00 reg_00;
2358 int i;
2359
2360 data = container_of(dev, struct sysfs_ioapic_data, dev);
2361 entry = data->entry;
2362
2363 spin_lock_irqsave(&ioapic_lock, flags);
2364 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002365 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2366 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 io_apic_write(dev->id, 0, reg_00.raw);
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 spin_unlock_irqrestore(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +02002370 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2371 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 return 0;
2374}
2375
2376static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002377 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 .suspend = ioapic_suspend,
2379 .resume = ioapic_resume,
2380};
2381
2382static int __init ioapic_init_sysfs(void)
2383{
2384 struct sys_device * dev;
2385 int i, size, error = 0;
2386
2387 error = sysdev_class_register(&ioapic_sysdev_class);
2388 if (error)
2389 return error;
2390
2391 for (i = 0; i < nr_ioapics; i++ ) {
2392 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2393 * sizeof(struct IO_APIC_route_entry);
2394 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2395 if (!mp_ioapic_data[i]) {
2396 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2397 continue;
2398 }
2399 memset(mp_ioapic_data[i], 0, size);
2400 dev = &mp_ioapic_data[i]->dev;
2401 dev->id = i;
2402 dev->cls = &ioapic_sysdev_class;
2403 error = sysdev_register(dev);
2404 if (error) {
2405 kfree(mp_ioapic_data[i]);
2406 mp_ioapic_data[i] = NULL;
2407 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2408 continue;
2409 }
2410 }
2411
2412 return 0;
2413}
2414
2415device_initcall(ioapic_init_sysfs);
2416
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002417/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002418 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002419 */
2420int create_irq(void)
2421{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002422 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002423 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002424 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002425
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002426 irq = -ENOSPC;
2427 spin_lock_irqsave(&vector_lock, flags);
2428 for (new = (NR_IRQS - 1); new >= 0; new--) {
2429 if (platform_legacy_irq(new))
2430 continue;
2431 if (irq_vector[new] != 0)
2432 continue;
2433 vector = __assign_irq_vector(new);
2434 if (likely(vector > 0))
2435 irq = new;
2436 break;
2437 }
2438 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002439
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002440 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002441 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002442 dynamic_irq_init(irq);
2443 }
2444 return irq;
2445}
2446
2447void destroy_irq(unsigned int irq)
2448{
2449 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002450
2451 dynamic_irq_cleanup(irq);
2452
2453 spin_lock_irqsave(&vector_lock, flags);
PJ Waskiewicz9d9ad4b2008-04-25 17:58:52 -07002454 clear_bit(irq_vector[irq], used_vectors);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002455 irq_vector[irq] = 0;
2456 spin_unlock_irqrestore(&vector_lock, flags);
2457}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002458
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002459/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002460 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002461 */
2462#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002463static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002464{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002465 int vector;
2466 unsigned dest;
2467
2468 vector = assign_irq_vector(irq);
2469 if (vector >= 0) {
2470 dest = cpu_mask_to_apicid(TARGET_CPUS);
2471
2472 msg->address_hi = MSI_ADDR_BASE_HI;
2473 msg->address_lo =
2474 MSI_ADDR_BASE_LO |
2475 ((INT_DEST_MODE == 0) ?
2476 MSI_ADDR_DEST_MODE_PHYSICAL:
2477 MSI_ADDR_DEST_MODE_LOGICAL) |
2478 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2479 MSI_ADDR_REDIRECTION_CPU:
2480 MSI_ADDR_REDIRECTION_LOWPRI) |
2481 MSI_ADDR_DEST_ID(dest);
2482
2483 msg->data =
2484 MSI_DATA_TRIGGER_EDGE |
2485 MSI_DATA_LEVEL_ASSERT |
2486 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2487 MSI_DATA_DELIVERY_FIXED:
2488 MSI_DATA_DELIVERY_LOWPRI) |
2489 MSI_DATA_VECTOR(vector);
2490 }
2491 return vector;
2492}
2493
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002494#ifdef CONFIG_SMP
2495static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2496{
2497 struct msi_msg msg;
2498 unsigned int dest;
2499 cpumask_t tmp;
2500 int vector;
2501
2502 cpus_and(tmp, mask, cpu_online_map);
2503 if (cpus_empty(tmp))
2504 tmp = TARGET_CPUS;
2505
2506 vector = assign_irq_vector(irq);
2507 if (vector < 0)
2508 return;
2509
2510 dest = cpu_mask_to_apicid(mask);
2511
2512 read_msi_msg(irq, &msg);
2513
2514 msg.data &= ~MSI_DATA_VECTOR_MASK;
2515 msg.data |= MSI_DATA_VECTOR(vector);
2516 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2517 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2518
2519 write_msi_msg(irq, &msg);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002520 irq_desc[irq].affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002521}
2522#endif /* CONFIG_SMP */
2523
2524/*
2525 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2526 * which implement the MSI or MSI-X Capability Structure.
2527 */
2528static struct irq_chip msi_chip = {
2529 .name = "PCI-MSI",
2530 .unmask = unmask_msi_irq,
2531 .mask = mask_msi_irq,
2532 .ack = ack_ioapic_irq,
2533#ifdef CONFIG_SMP
2534 .set_affinity = set_msi_irq_affinity,
2535#endif
2536 .retrigger = ioapic_retrigger_irq,
2537};
2538
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002539int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002540{
2541 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002542 int irq, ret;
2543 irq = create_irq();
2544 if (irq < 0)
2545 return irq;
2546
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002547 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002548 if (ret < 0) {
2549 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002550 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002551 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002552
Michael Ellerman7fe37302007-04-18 19:39:21 +10002553 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002554 write_msi_msg(irq, &msg);
2555
Ingo Molnara460e742006-10-17 00:10:03 -07002556 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2557 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002558
Michael Ellerman7fe37302007-04-18 19:39:21 +10002559 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002560}
2561
2562void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002563{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002564 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002565}
2566
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002567#endif /* CONFIG_PCI_MSI */
2568
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002569/*
2570 * Hypertransport interrupt support
2571 */
2572#ifdef CONFIG_HT_IRQ
2573
2574#ifdef CONFIG_SMP
2575
2576static void target_ht_irq(unsigned int irq, unsigned int dest)
2577{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002578 struct ht_irq_msg msg;
2579 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002580
Eric W. Biedermanec683072006-11-08 17:44:57 -08002581 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2582 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002583
Eric W. Biedermanec683072006-11-08 17:44:57 -08002584 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2585 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002586
Eric W. Biedermanec683072006-11-08 17:44:57 -08002587 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002588}
2589
2590static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2591{
2592 unsigned int dest;
2593 cpumask_t tmp;
2594
2595 cpus_and(tmp, mask, cpu_online_map);
2596 if (cpus_empty(tmp))
2597 tmp = TARGET_CPUS;
2598
2599 cpus_and(mask, tmp, CPU_MASK_ALL);
2600
2601 dest = cpu_mask_to_apicid(mask);
2602
2603 target_ht_irq(irq, dest);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002604 irq_desc[irq].affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002605}
2606#endif
2607
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002608static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002609 .name = "PCI-HT",
2610 .mask = mask_ht_irq,
2611 .unmask = unmask_ht_irq,
2612 .ack = ack_ioapic_irq,
2613#ifdef CONFIG_SMP
2614 .set_affinity = set_ht_irq_affinity,
2615#endif
2616 .retrigger = ioapic_retrigger_irq,
2617};
2618
2619int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2620{
2621 int vector;
2622
2623 vector = assign_irq_vector(irq);
2624 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002625 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002626 unsigned dest;
2627 cpumask_t tmp;
2628
2629 cpus_clear(tmp);
2630 cpu_set(vector >> 8, tmp);
2631 dest = cpu_mask_to_apicid(tmp);
2632
Eric W. Biedermanec683072006-11-08 17:44:57 -08002633 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002634
Eric W. Biedermanec683072006-11-08 17:44:57 -08002635 msg.address_lo =
2636 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002637 HT_IRQ_LOW_DEST_ID(dest) |
2638 HT_IRQ_LOW_VECTOR(vector) |
2639 ((INT_DEST_MODE == 0) ?
2640 HT_IRQ_LOW_DM_PHYSICAL :
2641 HT_IRQ_LOW_DM_LOGICAL) |
2642 HT_IRQ_LOW_RQEOI_EDGE |
2643 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2644 HT_IRQ_LOW_MT_FIXED :
2645 HT_IRQ_LOW_MT_ARBITRATED) |
2646 HT_IRQ_LOW_IRQ_MASKED;
2647
Eric W. Biedermanec683072006-11-08 17:44:57 -08002648 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002649
Ingo Molnara460e742006-10-17 00:10:03 -07002650 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2651 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002652 }
2653 return vector;
2654}
2655#endif /* CONFIG_HT_IRQ */
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657/* --------------------------------------------------------------------------
2658 ACPI-based IOAPIC Configuration
2659 -------------------------------------------------------------------------- */
2660
Len Brown888ba6c2005-08-24 12:07:20 -04002661#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663int __init io_apic_get_unique_id (int ioapic, int apic_id)
2664{
2665 union IO_APIC_reg_00 reg_00;
2666 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2667 physid_mask_t tmp;
2668 unsigned long flags;
2669 int i = 0;
2670
2671 /*
2672 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2673 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2674 * supports up to 16 on one shared APIC bus.
2675 *
2676 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2677 * advantage of new APIC bus architecture.
2678 */
2679
2680 if (physids_empty(apic_id_map))
2681 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2682
2683 spin_lock_irqsave(&ioapic_lock, flags);
2684 reg_00.raw = io_apic_read(ioapic, 0);
2685 spin_unlock_irqrestore(&ioapic_lock, flags);
2686
2687 if (apic_id >= get_physical_broadcast()) {
2688 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2689 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2690 apic_id = reg_00.bits.ID;
2691 }
2692
2693 /*
2694 * Every APIC in a system must have a unique ID or we get lots of nice
2695 * 'stuck on smp_invalidate_needed IPI wait' messages.
2696 */
2697 if (check_apicid_used(apic_id_map, apic_id)) {
2698
2699 for (i = 0; i < get_physical_broadcast(); i++) {
2700 if (!check_apicid_used(apic_id_map, i))
2701 break;
2702 }
2703
2704 if (i == get_physical_broadcast())
2705 panic("Max apic_id exceeded!\n");
2706
2707 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2708 "trying %d\n", ioapic, apic_id, i);
2709
2710 apic_id = i;
2711 }
2712
2713 tmp = apicid_to_cpu_present(apic_id);
2714 physids_or(apic_id_map, apic_id_map, tmp);
2715
2716 if (reg_00.bits.ID != apic_id) {
2717 reg_00.bits.ID = apic_id;
2718
2719 spin_lock_irqsave(&ioapic_lock, flags);
2720 io_apic_write(ioapic, 0, reg_00.raw);
2721 reg_00.raw = io_apic_read(ioapic, 0);
2722 spin_unlock_irqrestore(&ioapic_lock, flags);
2723
2724 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002725 if (reg_00.bits.ID != apic_id) {
2726 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2727 return -1;
2728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
2730
2731 apic_printk(APIC_VERBOSE, KERN_INFO
2732 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2733
2734 return apic_id;
2735}
2736
2737
2738int __init io_apic_get_version (int ioapic)
2739{
2740 union IO_APIC_reg_01 reg_01;
2741 unsigned long flags;
2742
2743 spin_lock_irqsave(&ioapic_lock, flags);
2744 reg_01.raw = io_apic_read(ioapic, 1);
2745 spin_unlock_irqrestore(&ioapic_lock, flags);
2746
2747 return reg_01.bits.version;
2748}
2749
2750
2751int __init io_apic_get_redir_entries (int ioapic)
2752{
2753 union IO_APIC_reg_01 reg_01;
2754 unsigned long flags;
2755
2756 spin_lock_irqsave(&ioapic_lock, flags);
2757 reg_01.raw = io_apic_read(ioapic, 1);
2758 spin_unlock_irqrestore(&ioapic_lock, flags);
2759
2760 return reg_01.bits.entries;
2761}
2762
2763
2764int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2765{
2766 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 if (!IO_APIC_IRQ(irq)) {
2769 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2770 ioapic);
2771 return -EINVAL;
2772 }
2773
2774 /*
2775 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2776 * Note that we mask (disable) IRQs now -- these get enabled when the
2777 * corresponding device driver registers for this IRQ.
2778 */
2779
2780 memset(&entry,0,sizeof(entry));
2781
2782 entry.delivery_mode = INT_DELIVERY_MODE;
2783 entry.dest_mode = INT_DEST_MODE;
2784 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2785 entry.trigger = edge_level;
2786 entry.polarity = active_high_low;
2787 entry.mask = 1;
2788
2789 /*
2790 * IRQs < 16 are already in the irq_2_pin[] map
2791 */
2792 if (irq >= 16)
2793 add_pin_to_irq(irq, ioapic, pin);
2794
2795 entry.vector = assign_irq_vector(irq);
2796
2797 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2798 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002799 mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 edge_level, active_high_low);
2801
2802 ioapic_register_intr(irq, entry.vector, edge_level);
2803
2804 if (!ioapic && (irq < 16))
2805 disable_8259A_irq(irq);
2806
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002807 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 return 0;
2810}
2811
Shaohua Li61fd47e2007-11-17 01:05:28 -05002812int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2813{
2814 int i;
2815
2816 if (skip_ioapic_setup)
2817 return -1;
2818
2819 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002820 if (mp_irqs[i].mp_irqtype == mp_INT &&
2821 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002822 break;
2823 if (i >= mp_irq_entries)
2824 return -1;
2825
2826 *trigger = irq_trigger(i);
2827 *polarity = irq_polarity(i);
2828 return 0;
2829}
2830
Len Brown888ba6c2005-08-24 12:07:20 -04002831#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002832
2833static int __init parse_disable_timer_pin_1(char *arg)
2834{
2835 disable_timer_pin_1 = 1;
2836 return 0;
2837}
2838early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2839
2840static int __init parse_enable_timer_pin_1(char *arg)
2841{
2842 disable_timer_pin_1 = -1;
2843 return 0;
2844}
2845early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2846
2847static int __init parse_noapic(char *arg)
2848{
2849 /* disable IO-APIC */
2850 disable_ioapic_setup();
2851 return 0;
2852}
2853early_param("noapic", parse_noapic);