blob: 1bc4f7e4ed21eb54462c306bf57cd265b499f50b [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>
Ashok Raj54d5d422005-09-06 15:16:15 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h>
40#include <asm/smp.h>
41#include <asm/desc.h>
42#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070043#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020044#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070045#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070046#include <asm/hypertransport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020049#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "io_ports.h"
52
53int (*ioapic_renumber_irq)(int ioapic, int irq);
54atomic_t irq_mis_count;
55
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080056/* Where if anywhere is the i8259 connect in external int mode */
57static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static DEFINE_SPINLOCK(ioapic_lock);
Jan Beulich0a1ad602006-06-26 13:56:43 +020060static DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Andi Kleenf9262c12006-03-08 17:57:25 -080062int timer_over_8254 __initdata = 1;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Is the SiS APIC rmw bug present ?
66 * -1 = don't know, 0 = no, 1 = yes
67 */
68int sis_apic_bug = -1;
69
70/*
71 * # of IRQ routing registers
72 */
73int nr_ioapic_registers[MAX_IO_APICS];
74
Rusty Russell1a3f2392006-09-26 10:52:32 +020075static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * Rough estimation of how many shared IRQs there are, can
79 * be changed anytime.
80 */
81#define MAX_PLUS_SHARED_IRQS NR_IRQS
82#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
83
84/*
85 * This is performance-critical, we want to do it O(1)
86 *
87 * the indexing order of this array favors 1:1 mappings
88 * between pins and IRQs.
89 */
90
91static struct irq_pin_list {
92 int apic, pin, next;
93} irq_2_pin[PIN_MAP_SIZE];
94
Linus Torvalds130fe052006-11-01 09:11:00 -080095struct io_apic {
96 unsigned int index;
97 unsigned int unused[3];
98 unsigned int data;
99};
100
101static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
102{
103 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
104 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
105}
106
107static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
108{
109 struct io_apic __iomem *io_apic = io_apic_base(apic);
110 writel(reg, &io_apic->index);
111 return readl(&io_apic->data);
112}
113
114static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
115{
116 struct io_apic __iomem *io_apic = io_apic_base(apic);
117 writel(reg, &io_apic->index);
118 writel(value, &io_apic->data);
119}
120
121/*
122 * Re-write a value: to be used for read-modify-write
123 * cycles where the read already set up the index register.
124 *
125 * Older SiS APIC requires we rewrite the index register
126 */
127static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
128{
Al Virocb468982007-02-09 16:39:25 +0000129 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800130 if (sis_apic_bug)
131 writel(reg, &io_apic->index);
132 writel(value, &io_apic->data);
133}
134
Andi Kleencf4c6a22006-09-26 10:52:30 +0200135union entry_union {
136 struct { u32 w1, w2; };
137 struct IO_APIC_route_entry entry;
138};
139
140static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
141{
142 union entry_union eu;
143 unsigned long flags;
144 spin_lock_irqsave(&ioapic_lock, flags);
145 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
146 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
147 spin_unlock_irqrestore(&ioapic_lock, flags);
148 return eu.entry;
149}
150
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800151/*
152 * When we write a new IO APIC routing entry, we need to write the high
153 * word first! If the mask bit in the low word is clear, we will enable
154 * the interrupt, and we need to make sure the entry is fully populated
155 * before that happens.
156 */
Andi Kleend15512f2006-12-07 02:14:07 +0100157static void
158__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
159{
160 union entry_union eu;
161 eu.entry = e;
162 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
163 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
164}
165
Andi Kleencf4c6a22006-09-26 10:52:30 +0200166static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
167{
168 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200169 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100170 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800171 spin_unlock_irqrestore(&ioapic_lock, flags);
172}
173
174/*
175 * When we mask an IO APIC routing entry, we need to write the low
176 * word first, in order to set the mask bit before we change the
177 * high bits!
178 */
179static void ioapic_mask_entry(int apic, int pin)
180{
181 unsigned long flags;
182 union entry_union eu = { .entry.mask = 1 };
183
184 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200185 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
186 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
187 spin_unlock_irqrestore(&ioapic_lock, flags);
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
192 * shared ISA-space IRQs, so we have to support them. We are super
193 * fast in the common case, and fast for shared ISA-space IRQs.
194 */
195static void add_pin_to_irq(unsigned int irq, int apic, int pin)
196{
197 static int first_free_entry = NR_IRQS;
198 struct irq_pin_list *entry = irq_2_pin + irq;
199
200 while (entry->next)
201 entry = irq_2_pin + entry->next;
202
203 if (entry->pin != -1) {
204 entry->next = first_free_entry;
205 entry = irq_2_pin + entry->next;
206 if (++first_free_entry >= PIN_MAP_SIZE)
207 panic("io_apic.c: whoops");
208 }
209 entry->apic = apic;
210 entry->pin = pin;
211}
212
213/*
214 * Reroute an IRQ to a different pin.
215 */
216static void __init replace_pin_at_irq(unsigned int irq,
217 int oldapic, int oldpin,
218 int newapic, int newpin)
219{
220 struct irq_pin_list *entry = irq_2_pin + irq;
221
222 while (1) {
223 if (entry->apic == oldapic && entry->pin == oldpin) {
224 entry->apic = newapic;
225 entry->pin = newpin;
226 }
227 if (!entry->next)
228 break;
229 entry = irq_2_pin + entry->next;
230 }
231}
232
233static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
234{
235 struct irq_pin_list *entry = irq_2_pin + irq;
236 unsigned int pin, reg;
237
238 for (;;) {
239 pin = entry->pin;
240 if (pin == -1)
241 break;
242 reg = io_apic_read(entry->apic, 0x10 + pin*2);
243 reg &= ~disable;
244 reg |= enable;
245 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
246 if (!entry->next)
247 break;
248 entry = irq_2_pin + entry->next;
249 }
250}
251
252/* mask = 1 */
253static void __mask_IO_APIC_irq (unsigned int irq)
254{
255 __modify_IO_APIC_irq(irq, 0x00010000, 0);
256}
257
258/* mask = 0 */
259static void __unmask_IO_APIC_irq (unsigned int irq)
260{
261 __modify_IO_APIC_irq(irq, 0, 0x00010000);
262}
263
264/* mask = 1, trigger = 0 */
265static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
266{
267 __modify_IO_APIC_irq(irq, 0x00010000, 0x00008000);
268}
269
270/* mask = 0, trigger = 1 */
271static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
272{
273 __modify_IO_APIC_irq(irq, 0x00008000, 0x00010000);
274}
275
276static void mask_IO_APIC_irq (unsigned int irq)
277{
278 unsigned long flags;
279
280 spin_lock_irqsave(&ioapic_lock, flags);
281 __mask_IO_APIC_irq(irq);
282 spin_unlock_irqrestore(&ioapic_lock, flags);
283}
284
285static void unmask_IO_APIC_irq (unsigned int irq)
286{
287 unsigned long flags;
288
289 spin_lock_irqsave(&ioapic_lock, flags);
290 __unmask_IO_APIC_irq(irq);
291 spin_unlock_irqrestore(&ioapic_lock, flags);
292}
293
294static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
295{
296 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200299 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (entry.delivery_mode == dest_SMI)
301 return;
302
303 /*
304 * Disable it in the IO-APIC irq-routing table:
305 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800306 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309static void clear_IO_APIC (void)
310{
311 int apic, pin;
312
313 for (apic = 0; apic < nr_ioapics; apic++)
314 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
315 clear_IO_APIC_pin(apic, pin);
316}
317
Ashok Raj54d5d422005-09-06 15:16:15 -0700318#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
320{
321 unsigned long flags;
322 int pin;
323 struct irq_pin_list *entry = irq_2_pin + irq;
324 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700325 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Ashok Raj54d5d422005-09-06 15:16:15 -0700327 cpus_and(tmp, cpumask, cpu_online_map);
328 if (cpus_empty(tmp))
329 tmp = TARGET_CPUS;
330
331 cpus_and(cpumask, tmp, CPU_MASK_ALL);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 apicid_value = cpu_mask_to_apicid(cpumask);
334 /* Prepare to do the io_apic_write */
335 apicid_value = apicid_value << 24;
336 spin_lock_irqsave(&ioapic_lock, flags);
337 for (;;) {
338 pin = entry->pin;
339 if (pin == -1)
340 break;
341 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
342 if (!entry->next)
343 break;
344 entry = irq_2_pin + entry->next;
345 }
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700346 irq_desc[irq].affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_unlock_irqrestore(&ioapic_lock, flags);
348}
349
350#if defined(CONFIG_IRQBALANCE)
351# include <asm/processor.h> /* kernel_thread() */
352# include <linux/kernel_stat.h> /* kstat */
353# include <linux/slab.h> /* kmalloc() */
354# include <linux/timer.h> /* time_after() */
355
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700356#ifdef CONFIG_BALANCED_IRQ_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357# define TDprintk(x...) do { printk("<%ld:%s:%d>: ", jiffies, __FILE__, __LINE__); printk(x); } while (0)
358# define Dprintk(x...) do { TDprintk(x); } while (0)
359# else
360# define TDprintk(x...)
361# define Dprintk(x...)
362# endif
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#define IRQBALANCE_CHECK_ARCH -999
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700365#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
366#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
367#define BALANCED_IRQ_MORE_DELTA (HZ/10)
368#define BALANCED_IRQ_LESS_DELTA (HZ)
369
370static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
371static int physical_balance __read_mostly;
372static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374static struct irq_cpu_info {
375 unsigned long * last_irq;
376 unsigned long * irq_delta;
377 unsigned long irq;
378} irq_cpu_data[NR_CPUS];
379
380#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
381#define LAST_CPU_IRQ(cpu,irq) (irq_cpu_data[cpu].last_irq[irq])
382#define IRQ_DELTA(cpu,irq) (irq_cpu_data[cpu].irq_delta[irq])
383
384#define IDLE_ENOUGH(cpu,now) \
385 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
386
387#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
388
389#define CPU_TO_PACKAGEINDEX(i) (first_cpu(cpu_sibling_map[i]))
390
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700391static cpumask_t balance_irq_affinity[NR_IRQS] = {
392 [0 ... NR_IRQS-1] = CPU_MASK_ALL
393};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700395void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
396{
397 balance_irq_affinity[irq] = mask;
398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
401 unsigned long now, int direction)
402{
403 int search_idle = 1;
404 int cpu = curr_cpu;
405
406 goto inside;
407
408 do {
409 if (unlikely(cpu == curr_cpu))
410 search_idle = 0;
411inside:
412 if (direction == 1) {
413 cpu++;
414 if (cpu >= NR_CPUS)
415 cpu = 0;
416 } else {
417 cpu--;
418 if (cpu == -1)
419 cpu = NR_CPUS-1;
420 }
421 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
422 (search_idle && !IDLE_ENOUGH(cpu,now)));
423
424 return cpu;
425}
426
427static inline void balance_irq(int cpu, int irq)
428{
429 unsigned long now = jiffies;
430 cpumask_t allowed_mask;
431 unsigned int new_cpu;
432
433 if (irqbalance_disabled)
434 return;
435
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700436 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 new_cpu = move(cpu, allowed_mask, now, 1);
438 if (cpu != new_cpu) {
Ashok Raj54d5d422005-09-06 15:16:15 -0700439 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441}
442
443static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
444{
445 int i, j;
446 Dprintk("Rotating IRQs among CPUs.\n");
Andrew Morton394e3902006-03-23 03:01:05 -0800447 for_each_online_cpu(i) {
448 for (j = 0; j < NR_IRQS; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!irq_desc[j].action)
450 continue;
451 /* Is it a significant load ? */
452 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
453 useful_load_threshold)
454 continue;
455 balance_irq(i, j);
456 }
457 }
458 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
459 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
460 return;
461}
462
463static void do_irq_balance(void)
464{
465 int i, j;
466 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
467 unsigned long move_this_load = 0;
468 int max_loaded = 0, min_loaded = 0;
469 int load;
470 unsigned long useful_load_threshold = balanced_irq_interval + 10;
471 int selected_irq;
472 int tmp_loaded, first_attempt = 1;
473 unsigned long tmp_cpu_irq;
474 unsigned long imbalance = 0;
475 cpumask_t allowed_mask, target_cpu_mask, tmp;
476
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800477 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int package_index;
479 CPU_IRQ(i) = 0;
480 if (!cpu_online(i))
481 continue;
482 package_index = CPU_TO_PACKAGEINDEX(i);
483 for (j = 0; j < NR_IRQS; j++) {
484 unsigned long value_now, delta;
Thomas Gleixner950f4422007-02-16 01:27:24 -0800485 /* Is this an active IRQ or balancing disabled ? */
486 if (!irq_desc[j].action || irq_balancing_disabled(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 continue;
488 if ( package_index == i )
489 IRQ_DELTA(package_index,j) = 0;
490 /* Determine the total count per processor per IRQ */
491 value_now = (unsigned long) kstat_cpu(i).irqs[j];
492
493 /* Determine the activity per processor per IRQ */
494 delta = value_now - LAST_CPU_IRQ(i,j);
495
496 /* Update last_cpu_irq[][] for the next time */
497 LAST_CPU_IRQ(i,j) = value_now;
498
499 /* Ignore IRQs whose rate is less than the clock */
500 if (delta < useful_load_threshold)
501 continue;
502 /* update the load for the processor or package total */
503 IRQ_DELTA(package_index,j) += delta;
504
505 /* Keep track of the higher numbered sibling as well */
506 if (i != package_index)
507 CPU_IRQ(i) += delta;
508 /*
509 * We have sibling A and sibling B in the package
510 *
511 * cpu_irq[A] = load for cpu A + load for cpu B
512 * cpu_irq[B] = load for cpu B
513 */
514 CPU_IRQ(package_index) += delta;
515 }
516 }
517 /* Find the least loaded processor package */
Andrew Morton394e3902006-03-23 03:01:05 -0800518 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (i != CPU_TO_PACKAGEINDEX(i))
520 continue;
521 if (min_cpu_irq > CPU_IRQ(i)) {
522 min_cpu_irq = CPU_IRQ(i);
523 min_loaded = i;
524 }
525 }
526 max_cpu_irq = ULONG_MAX;
527
528tryanothercpu:
529 /* Look for heaviest loaded processor.
530 * We may come back to get the next heaviest loaded processor.
531 * Skip processors with trivial loads.
532 */
533 tmp_cpu_irq = 0;
534 tmp_loaded = -1;
Andrew Morton394e3902006-03-23 03:01:05 -0800535 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (i != CPU_TO_PACKAGEINDEX(i))
537 continue;
538 if (max_cpu_irq <= CPU_IRQ(i))
539 continue;
540 if (tmp_cpu_irq < CPU_IRQ(i)) {
541 tmp_cpu_irq = CPU_IRQ(i);
542 tmp_loaded = i;
543 }
544 }
545
546 if (tmp_loaded == -1) {
547 /* In the case of small number of heavy interrupt sources,
548 * loading some of the cpus too much. We use Ingo's original
549 * approach to rotate them around.
550 */
551 if (!first_attempt && imbalance >= useful_load_threshold) {
552 rotate_irqs_among_cpus(useful_load_threshold);
553 return;
554 }
555 goto not_worth_the_effort;
556 }
557
558 first_attempt = 0; /* heaviest search */
559 max_cpu_irq = tmp_cpu_irq; /* load */
560 max_loaded = tmp_loaded; /* processor */
561 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
562
563 Dprintk("max_loaded cpu = %d\n", max_loaded);
564 Dprintk("min_loaded cpu = %d\n", min_loaded);
565 Dprintk("max_cpu_irq load = %ld\n", max_cpu_irq);
566 Dprintk("min_cpu_irq load = %ld\n", min_cpu_irq);
567 Dprintk("load imbalance = %lu\n", imbalance);
568
569 /* if imbalance is less than approx 10% of max load, then
570 * observe diminishing returns action. - quit
571 */
572 if (imbalance < (max_cpu_irq >> 3)) {
573 Dprintk("Imbalance too trivial\n");
574 goto not_worth_the_effort;
575 }
576
577tryanotherirq:
578 /* if we select an IRQ to move that can't go where we want, then
579 * see if there is another one to try.
580 */
581 move_this_load = 0;
582 selected_irq = -1;
583 for (j = 0; j < NR_IRQS; j++) {
584 /* Is this an active IRQ? */
585 if (!irq_desc[j].action)
586 continue;
587 if (imbalance <= IRQ_DELTA(max_loaded,j))
588 continue;
589 /* Try to find the IRQ that is closest to the imbalance
590 * without going over.
591 */
592 if (move_this_load < IRQ_DELTA(max_loaded,j)) {
593 move_this_load = IRQ_DELTA(max_loaded,j);
594 selected_irq = j;
595 }
596 }
597 if (selected_irq == -1) {
598 goto tryanothercpu;
599 }
600
601 imbalance = move_this_load;
602
603 /* For physical_balance case, we accumlated both load
604 * values in the one of the siblings cpu_irq[],
605 * to use the same code for physical and logical processors
606 * as much as possible.
607 *
608 * NOTE: the cpu_irq[] array holds the sum of the load for
609 * sibling A and sibling B in the slot for the lowest numbered
610 * sibling (A), _AND_ the load for sibling B in the slot for
611 * the higher numbered sibling.
612 *
613 * We seek the least loaded sibling by making the comparison
614 * (A+B)/2 vs B
615 */
616 load = CPU_IRQ(min_loaded) >> 1;
617 for_each_cpu_mask(j, cpu_sibling_map[min_loaded]) {
618 if (load > CPU_IRQ(j)) {
619 /* This won't change cpu_sibling_map[min_loaded] */
620 load = CPU_IRQ(j);
621 min_loaded = j;
622 }
623 }
624
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700625 cpus_and(allowed_mask,
626 cpu_online_map,
627 balance_irq_affinity[selected_irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 target_cpu_mask = cpumask_of_cpu(min_loaded);
629 cpus_and(tmp, target_cpu_mask, allowed_mask);
630
631 if (!cpus_empty(tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 Dprintk("irq = %d moved to cpu = %d\n",
634 selected_irq, min_loaded);
635 /* mark for change destination */
Ashok Raj54d5d422005-09-06 15:16:15 -0700636 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Since we made a change, come back sooner to
639 * check for more variation.
640 */
641 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
642 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
643 return;
644 }
645 goto tryanotherirq;
646
647not_worth_the_effort:
648 /*
649 * if we did not find an IRQ to move, then adjust the time interval
650 * upward
651 */
652 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
653 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
654 Dprintk("IRQ worth rotating not found\n");
655 return;
656}
657
658static int balanced_irq(void *unused)
659{
660 int i;
661 unsigned long prev_balance_time = jiffies;
662 long time_remaining = balanced_irq_interval;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* push everything to CPU 0 to give us a starting point. */
665 for (i = 0 ; i < NR_IRQS ; i++) {
Ingo Molnarcd916d32006-06-29 02:24:42 -0700666 irq_desc[i].pending_mask = cpumask_of_cpu(0);
Ashok Raj54d5d422005-09-06 15:16:15 -0700667 set_pending_irq(i, cpumask_of_cpu(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700670 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 for ( ; ; ) {
Nishanth Aravamudan52e6e632005-09-10 00:27:26 -0700672 time_remaining = schedule_timeout_interruptible(time_remaining);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700673 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (time_after(jiffies,
675 prev_balance_time+balanced_irq_interval)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700676 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 do_irq_balance();
678 prev_balance_time = jiffies;
679 time_remaining = balanced_irq_interval;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700680 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
683 return 0;
684}
685
686static int __init balanced_irq_init(void)
687{
688 int i;
689 struct cpuinfo_x86 *c;
690 cpumask_t tmp;
691
692 cpus_shift_right(tmp, cpu_online_map, 2);
693 c = &boot_cpu_data;
694 /* When not overwritten by the command line ask subarchitecture. */
695 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
696 irqbalance_disabled = NO_BALANCE_IRQ;
697 if (irqbalance_disabled)
698 return 0;
699
700 /* disable irqbalance completely if there is only one processor online */
701 if (num_online_cpus() < 2) {
702 irqbalance_disabled = 1;
703 return 0;
704 }
705 /*
706 * Enable physical balance only if more than 1 physical processor
707 * is present
708 */
709 if (smp_num_siblings > 1 && !cpus_empty(tmp))
710 physical_balance = 1;
711
Andrew Morton394e3902006-03-23 03:01:05 -0800712 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
714 irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
715 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
716 printk(KERN_ERR "balanced_irq_init: out of memory");
717 goto failed;
718 }
719 memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
720 memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
721 }
722
723 printk(KERN_INFO "Starting balanced_irq\n");
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200724 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200726 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727failed:
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800728 for_each_possible_cpu(i) {
Jesper Juhl4ae66732005-06-25 14:58:48 -0700729 kfree(irq_cpu_data[i].irq_delta);
Andrew Morton394e3902006-03-23 03:01:05 -0800730 irq_cpu_data[i].irq_delta = NULL;
Jesper Juhl4ae66732005-06-25 14:58:48 -0700731 kfree(irq_cpu_data[i].last_irq);
Andrew Morton394e3902006-03-23 03:01:05 -0800732 irq_cpu_data[i].last_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 return 0;
735}
736
Andrew Mortonc2481cc2007-04-08 16:04:04 -0700737int __devinit irqbalance_disable(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 irqbalance_disabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800740 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743__setup("noirqbalance", irqbalance_disable);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745late_initcall(balanced_irq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#endif /* CONFIG_IRQBALANCE */
Ashok Raj54d5d422005-09-06 15:16:15 -0700747#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749#ifndef CONFIG_SMP
750void fastcall send_IPI_self(int vector)
751{
752 unsigned int cfg;
753
754 /*
755 * Wait for idle.
756 */
757 apic_wait_icr_idle();
758 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
759 /*
760 * Send the IPI. The write to APIC_ICR fires this off.
761 */
762 apic_write_around(APIC_ICR, cfg);
763}
764#endif /* !CONFIG_SMP */
765
766
767/*
768 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
769 * specific CPU-side IRQs.
770 */
771
772#define MAX_PIRQS 8
773static int pirq_entries [MAX_PIRQS];
774static int pirqs_enabled;
775int skip_ioapic_setup;
776
777static int __init ioapic_setup(char *str)
778{
779 skip_ioapic_setup = 1;
780 return 1;
781}
782
783__setup("noapic", ioapic_setup);
784
785static int __init ioapic_pirq_setup(char *str)
786{
787 int i, max;
788 int ints[MAX_PIRQS+1];
789
790 get_options(str, ARRAY_SIZE(ints), ints);
791
792 for (i = 0; i < MAX_PIRQS; i++)
793 pirq_entries[i] = -1;
794
795 pirqs_enabled = 1;
796 apic_printk(APIC_VERBOSE, KERN_INFO
797 "PIRQ redirection, working around broken MP-BIOS.\n");
798 max = MAX_PIRQS;
799 if (ints[0] < MAX_PIRQS)
800 max = ints[0];
801
802 for (i = 0; i < max; i++) {
803 apic_printk(APIC_VERBOSE, KERN_DEBUG
804 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
805 /*
806 * PIRQs are mapped upside down, usually.
807 */
808 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
809 }
810 return 1;
811}
812
813__setup("pirq=", ioapic_pirq_setup);
814
815/*
816 * Find the IRQ entry number of a certain pin.
817 */
818static int find_irq_entry(int apic, int pin, int type)
819{
820 int i;
821
822 for (i = 0; i < mp_irq_entries; i++)
823 if (mp_irqs[i].mpc_irqtype == type &&
824 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
825 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
826 mp_irqs[i].mpc_dstirq == pin)
827 return i;
828
829 return -1;
830}
831
832/*
833 * Find the pin to which IRQ[irq] (ISA) is connected
834 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800835static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 int i;
838
839 for (i = 0; i < mp_irq_entries; i++) {
840 int lbus = mp_irqs[i].mpc_srcbus;
841
842 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
843 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
Adrian Bunkd7fb0272006-12-07 02:14:19 +0100844 mp_bus_id_to_type[lbus] == MP_BUS_MCA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 ) &&
846 (mp_irqs[i].mpc_irqtype == type) &&
847 (mp_irqs[i].mpc_srcbusirq == irq))
848
849 return mp_irqs[i].mpc_dstirq;
850 }
851 return -1;
852}
853
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800854static int __init find_isa_irq_apic(int irq, int type)
855{
856 int i;
857
858 for (i = 0; i < mp_irq_entries; i++) {
859 int lbus = mp_irqs[i].mpc_srcbus;
860
861 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
862 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
Adrian Bunkd7fb0272006-12-07 02:14:19 +0100863 mp_bus_id_to_type[lbus] == MP_BUS_MCA
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800864 ) &&
865 (mp_irqs[i].mpc_irqtype == type) &&
866 (mp_irqs[i].mpc_srcbusirq == irq))
867 break;
868 }
869 if (i < mp_irq_entries) {
870 int apic;
871 for(apic = 0; apic < nr_ioapics; apic++) {
872 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
873 return apic;
874 }
875 }
876
877 return -1;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * Find a specific PCI IRQ entry.
882 * Not an __init, possibly needed by modules
883 */
884static int pin_2_irq(int idx, int apic, int pin);
885
886int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
887{
888 int apic, i, best_guess = -1;
889
890 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
891 "slot:%d, pin:%d.\n", bus, slot, pin);
892 if (mp_bus_id_to_pci_bus[bus] == -1) {
893 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
894 return -1;
895 }
896 for (i = 0; i < mp_irq_entries; i++) {
897 int lbus = mp_irqs[i].mpc_srcbus;
898
899 for (apic = 0; apic < nr_ioapics; apic++)
900 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
901 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
902 break;
903
904 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
905 !mp_irqs[i].mpc_irqtype &&
906 (bus == lbus) &&
907 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
908 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
909
910 if (!(apic || IO_APIC_IRQ(irq)))
911 continue;
912
913 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
914 return irq;
915 /*
916 * Use the first all-but-pin matching entry as a
917 * best-guess fuzzy result for broken mptables.
918 */
919 if (best_guess < 0)
920 best_guess = irq;
921 }
922 }
923 return best_guess;
924}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700925EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/*
928 * This function currently is only a helper for the i386 smp boot process where
929 * we need to reprogram the ioredtbls to cater for the cpus which have come online
930 * so mask in all cases should simply be TARGET_CPUS
931 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700932#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933void __init setup_ioapic_dest(void)
934{
935 int pin, ioapic, irq, irq_entry;
936
937 if (skip_ioapic_setup == 1)
938 return;
939
940 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
941 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
942 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
943 if (irq_entry == -1)
944 continue;
945 irq = pin_2_irq(irq_entry, ioapic, pin);
946 set_ioapic_affinity_irq(irq, TARGET_CPUS);
947 }
948
949 }
950}
Ashok Raj54d5d422005-09-06 15:16:15 -0700951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953/*
954 * EISA Edge/Level control register, ELCR
955 */
956static int EISA_ELCR(unsigned int irq)
957{
958 if (irq < 16) {
959 unsigned int port = 0x4d0 + (irq >> 3);
960 return (inb(port) >> (irq & 7)) & 1;
961 }
962 apic_printk(APIC_VERBOSE, KERN_INFO
963 "Broken MPtable reports ISA irq %d\n", irq);
964 return 0;
965}
966
967/* EISA interrupts are always polarity zero and can be edge or level
968 * trigger depending on the ELCR value. If an interrupt is listed as
969 * EISA conforming in the MP table, that means its trigger type must
970 * be read in from the ELCR */
971
972#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
973#define default_EISA_polarity(idx) (0)
974
975/* ISA interrupts are always polarity zero edge triggered,
976 * when listed as conforming in the MP table. */
977
978#define default_ISA_trigger(idx) (0)
979#define default_ISA_polarity(idx) (0)
980
981/* PCI interrupts are always polarity one level triggered,
982 * when listed as conforming in the MP table. */
983
984#define default_PCI_trigger(idx) (1)
985#define default_PCI_polarity(idx) (1)
986
987/* MCA interrupts are always polarity zero level triggered,
988 * when listed as conforming in the MP table. */
989
990#define default_MCA_trigger(idx) (1)
991#define default_MCA_polarity(idx) (0)
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993static int __init MPBIOS_polarity(int idx)
994{
995 int bus = mp_irqs[idx].mpc_srcbus;
996 int polarity;
997
998 /*
999 * Determine IRQ line polarity (high active or low active):
1000 */
1001 switch (mp_irqs[idx].mpc_irqflag & 3)
1002 {
1003 case 0: /* conforms, ie. bus-type dependent polarity */
1004 {
1005 switch (mp_bus_id_to_type[bus])
1006 {
1007 case MP_BUS_ISA: /* ISA pin */
1008 {
1009 polarity = default_ISA_polarity(idx);
1010 break;
1011 }
1012 case MP_BUS_EISA: /* EISA pin */
1013 {
1014 polarity = default_EISA_polarity(idx);
1015 break;
1016 }
1017 case MP_BUS_PCI: /* PCI pin */
1018 {
1019 polarity = default_PCI_polarity(idx);
1020 break;
1021 }
1022 case MP_BUS_MCA: /* MCA pin */
1023 {
1024 polarity = default_MCA_polarity(idx);
1025 break;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 default:
1028 {
1029 printk(KERN_WARNING "broken BIOS!!\n");
1030 polarity = 1;
1031 break;
1032 }
1033 }
1034 break;
1035 }
1036 case 1: /* high active */
1037 {
1038 polarity = 0;
1039 break;
1040 }
1041 case 2: /* reserved */
1042 {
1043 printk(KERN_WARNING "broken BIOS!!\n");
1044 polarity = 1;
1045 break;
1046 }
1047 case 3: /* low active */
1048 {
1049 polarity = 1;
1050 break;
1051 }
1052 default: /* invalid */
1053 {
1054 printk(KERN_WARNING "broken BIOS!!\n");
1055 polarity = 1;
1056 break;
1057 }
1058 }
1059 return polarity;
1060}
1061
1062static int MPBIOS_trigger(int idx)
1063{
1064 int bus = mp_irqs[idx].mpc_srcbus;
1065 int trigger;
1066
1067 /*
1068 * Determine IRQ trigger mode (edge or level sensitive):
1069 */
1070 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
1071 {
1072 case 0: /* conforms, ie. bus-type dependent */
1073 {
1074 switch (mp_bus_id_to_type[bus])
1075 {
1076 case MP_BUS_ISA: /* ISA pin */
1077 {
1078 trigger = default_ISA_trigger(idx);
1079 break;
1080 }
1081 case MP_BUS_EISA: /* EISA pin */
1082 {
1083 trigger = default_EISA_trigger(idx);
1084 break;
1085 }
1086 case MP_BUS_PCI: /* PCI pin */
1087 {
1088 trigger = default_PCI_trigger(idx);
1089 break;
1090 }
1091 case MP_BUS_MCA: /* MCA pin */
1092 {
1093 trigger = default_MCA_trigger(idx);
1094 break;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 default:
1097 {
1098 printk(KERN_WARNING "broken BIOS!!\n");
1099 trigger = 1;
1100 break;
1101 }
1102 }
1103 break;
1104 }
1105 case 1: /* edge */
1106 {
1107 trigger = 0;
1108 break;
1109 }
1110 case 2: /* reserved */
1111 {
1112 printk(KERN_WARNING "broken BIOS!!\n");
1113 trigger = 1;
1114 break;
1115 }
1116 case 3: /* level */
1117 {
1118 trigger = 1;
1119 break;
1120 }
1121 default: /* invalid */
1122 {
1123 printk(KERN_WARNING "broken BIOS!!\n");
1124 trigger = 0;
1125 break;
1126 }
1127 }
1128 return trigger;
1129}
1130
1131static inline int irq_polarity(int idx)
1132{
1133 return MPBIOS_polarity(idx);
1134}
1135
1136static inline int irq_trigger(int idx)
1137{
1138 return MPBIOS_trigger(idx);
1139}
1140
1141static int pin_2_irq(int idx, int apic, int pin)
1142{
1143 int irq, i;
1144 int bus = mp_irqs[idx].mpc_srcbus;
1145
1146 /*
1147 * Debugging check, we are in big trouble if this message pops up!
1148 */
1149 if (mp_irqs[idx].mpc_dstirq != pin)
1150 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1151
1152 switch (mp_bus_id_to_type[bus])
1153 {
1154 case MP_BUS_ISA: /* ISA pin */
1155 case MP_BUS_EISA:
1156 case MP_BUS_MCA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 {
1158 irq = mp_irqs[idx].mpc_srcbusirq;
1159 break;
1160 }
1161 case MP_BUS_PCI: /* PCI pin */
1162 {
1163 /*
1164 * PCI IRQs are mapped in order
1165 */
1166 i = irq = 0;
1167 while (i < apic)
1168 irq += nr_ioapic_registers[i++];
1169 irq += pin;
1170
1171 /*
1172 * For MPS mode, so far only needed by ES7000 platform
1173 */
1174 if (ioapic_renumber_irq)
1175 irq = ioapic_renumber_irq(apic, irq);
1176
1177 break;
1178 }
1179 default:
1180 {
1181 printk(KERN_ERR "unknown bus type %d.\n",bus);
1182 irq = 0;
1183 break;
1184 }
1185 }
1186
1187 /*
1188 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1189 */
1190 if ((pin >= 16) && (pin <= 23)) {
1191 if (pirq_entries[pin-16] != -1) {
1192 if (!pirq_entries[pin-16]) {
1193 apic_printk(APIC_VERBOSE, KERN_DEBUG
1194 "disabling PIRQ%d\n", pin-16);
1195 } else {
1196 irq = pirq_entries[pin-16];
1197 apic_printk(APIC_VERBOSE, KERN_DEBUG
1198 "using PIRQ%d -> IRQ %d\n",
1199 pin-16, irq);
1200 }
1201 }
1202 }
1203 return irq;
1204}
1205
1206static inline int IO_APIC_irq_trigger(int irq)
1207{
1208 int apic, idx, pin;
1209
1210 for (apic = 0; apic < nr_ioapics; apic++) {
1211 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1212 idx = find_irq_entry(apic,pin,mp_INT);
1213 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1214 return irq_trigger(idx);
1215 }
1216 }
1217 /*
1218 * nonexistent IRQs are edge default
1219 */
1220 return 0;
1221}
1222
1223/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Adrian Bunk7e95b592006-12-07 02:14:11 +01001224static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001226static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Eric W. Biederman8339f002007-01-29 13:19:05 -07001228 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1229 int vector, offset, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001231 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
Jan Beulich0a1ad602006-06-26 13:56:43 +02001232
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001233 if (irq_vector[irq] > 0)
1234 return irq_vector[irq];
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001235
Jan Beulich0a1ad602006-06-26 13:56:43 +02001236 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001237 offset = current_offset;
1238next:
1239 vector += 8;
1240 if (vector >= FIRST_SYSTEM_VECTOR) {
1241 offset = (offset + 1) % 8;
1242 vector = FIRST_DEVICE_VECTOR + offset;
1243 }
1244 if (vector == current_vector)
1245 return -ENOSPC;
1246 if (vector == SYSCALL_VECTOR)
1247 goto next;
1248 for (i = 0; i < NR_IRQ_VECTORS; i++)
1249 if (irq_vector[i] == vector)
1250 goto next;
1251
1252 current_vector = vector;
1253 current_offset = offset;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001254 irq_vector[irq] = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +02001255
1256 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001259static int assign_irq_vector(int irq)
1260{
1261 unsigned long flags;
1262 int vector;
1263
1264 spin_lock_irqsave(&vector_lock, flags);
1265 vector = __assign_irq_vector(irq);
1266 spin_unlock_irqrestore(&vector_lock, flags);
1267
1268 return vector;
1269}
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001270static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272#define IOAPIC_AUTO -1
1273#define IOAPIC_EDGE 0
1274#define IOAPIC_LEVEL 1
1275
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001276static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Jan Beulich6ebcc002006-06-26 13:56:46 +02001278 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1279 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001280 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1281 handle_fasteoi_irq, "fasteoi");
Ingo Molnard7e25f32007-02-16 01:28:24 -08001282 else
Ingo Molnara460e742006-10-17 00:10:03 -07001283 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1284 handle_edge_irq, "edge");
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001285 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288static void __init setup_IO_APIC_irqs(void)
1289{
1290 struct IO_APIC_route_entry entry;
1291 int apic, pin, idx, irq, first_notcon = 1, vector;
1292 unsigned long flags;
1293
1294 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1295
1296 for (apic = 0; apic < nr_ioapics; apic++) {
1297 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1298
1299 /*
1300 * add it to the IO-APIC irq-routing table:
1301 */
1302 memset(&entry,0,sizeof(entry));
1303
1304 entry.delivery_mode = INT_DELIVERY_MODE;
1305 entry.dest_mode = INT_DEST_MODE;
1306 entry.mask = 0; /* enable IRQ */
1307 entry.dest.logical.logical_dest =
1308 cpu_mask_to_apicid(TARGET_CPUS);
1309
1310 idx = find_irq_entry(apic,pin,mp_INT);
1311 if (idx == -1) {
1312 if (first_notcon) {
1313 apic_printk(APIC_VERBOSE, KERN_DEBUG
1314 " IO-APIC (apicid-pin) %d-%d",
1315 mp_ioapics[apic].mpc_apicid,
1316 pin);
1317 first_notcon = 0;
1318 } else
1319 apic_printk(APIC_VERBOSE, ", %d-%d",
1320 mp_ioapics[apic].mpc_apicid, pin);
1321 continue;
1322 }
1323
1324 entry.trigger = irq_trigger(idx);
1325 entry.polarity = irq_polarity(idx);
1326
1327 if (irq_trigger(idx)) {
1328 entry.trigger = 1;
1329 entry.mask = 1;
1330 }
1331
1332 irq = pin_2_irq(idx, apic, pin);
1333 /*
1334 * skip adding the timer int on secondary nodes, which causes
1335 * a small but painful rift in the time-space continuum
1336 */
1337 if (multi_timer_check(apic, irq))
1338 continue;
1339 else
1340 add_pin_to_irq(irq, apic, pin);
1341
1342 if (!apic && !IO_APIC_IRQ(irq))
1343 continue;
1344
1345 if (IO_APIC_IRQ(irq)) {
1346 vector = assign_irq_vector(irq);
1347 entry.vector = vector;
1348 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1349
1350 if (!apic && (irq < 16))
1351 disable_8259A_irq(irq);
1352 }
1353 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +01001354 __ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 spin_unlock_irqrestore(&ioapic_lock, flags);
1356 }
1357 }
1358
1359 if (!first_notcon)
1360 apic_printk(APIC_VERBOSE, " not connected.\n");
1361}
1362
1363/*
1364 * Set up the 8259A-master output pin:
1365 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001366static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 memset(&entry,0,sizeof(entry));
1371
1372 disable_8259A_irq(0);
1373
1374 /* mask LVT0 */
1375 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1376
1377 /*
1378 * We use logical delivery to get the timer IRQ
1379 * to the first CPU.
1380 */
1381 entry.dest_mode = INT_DEST_MODE;
1382 entry.mask = 0; /* unmask IRQ now */
1383 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1384 entry.delivery_mode = INT_DELIVERY_MODE;
1385 entry.polarity = 0;
1386 entry.trigger = 0;
1387 entry.vector = vector;
1388
1389 /*
1390 * The timer IRQ doesn't have to know that behind the
1391 * scene we have a 8259A-master in AEOI mode ...
1392 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001393 irq_desc[0].chip = &ioapic_chip;
1394 set_irq_handler(0, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /*
1397 * Add it to the IO-APIC irq-routing table:
1398 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001399 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 enable_8259A_irq(0);
1402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404void __init print_IO_APIC(void)
1405{
1406 int apic, i;
1407 union IO_APIC_reg_00 reg_00;
1408 union IO_APIC_reg_01 reg_01;
1409 union IO_APIC_reg_02 reg_02;
1410 union IO_APIC_reg_03 reg_03;
1411 unsigned long flags;
1412
1413 if (apic_verbosity == APIC_QUIET)
1414 return;
1415
1416 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1417 for (i = 0; i < nr_ioapics; i++)
1418 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1419 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1420
1421 /*
1422 * We are a bit conservative about what we expect. We have to
1423 * know about every hardware change ASAP.
1424 */
1425 printk(KERN_INFO "testing the IO APIC.......................\n");
1426
1427 for (apic = 0; apic < nr_ioapics; apic++) {
1428
1429 spin_lock_irqsave(&ioapic_lock, flags);
1430 reg_00.raw = io_apic_read(apic, 0);
1431 reg_01.raw = io_apic_read(apic, 1);
1432 if (reg_01.bits.version >= 0x10)
1433 reg_02.raw = io_apic_read(apic, 2);
1434 if (reg_01.bits.version >= 0x20)
1435 reg_03.raw = io_apic_read(apic, 3);
1436 spin_unlock_irqrestore(&ioapic_lock, flags);
1437
1438 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1439 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1440 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1441 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1442 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1445 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1448 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
1451 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1452 * but the value of reg_02 is read as the previous read register
1453 * value, so ignore it if reg_02 == reg_01.
1454 */
1455 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1456 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1457 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
1460 /*
1461 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1462 * or reg_03, but the value of reg_0[23] is read as the previous read
1463 * register value, so ignore it if reg_03 == reg_0[12].
1464 */
1465 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1466 reg_03.raw != reg_01.raw) {
1467 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1468 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1472
1473 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1474 " Stat Dest Deli Vect: \n");
1475
1476 for (i = 0; i <= reg_01.bits.entries; i++) {
1477 struct IO_APIC_route_entry entry;
1478
Andi Kleencf4c6a22006-09-26 10:52:30 +02001479 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 printk(KERN_DEBUG " %02x %03X %02X ",
1482 i,
1483 entry.dest.logical.logical_dest,
1484 entry.dest.physical.physical_dest
1485 );
1486
1487 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1488 entry.mask,
1489 entry.trigger,
1490 entry.irr,
1491 entry.polarity,
1492 entry.delivery_status,
1493 entry.dest_mode,
1494 entry.delivery_mode,
1495 entry.vector
1496 );
1497 }
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1500 for (i = 0; i < NR_IRQS; i++) {
1501 struct irq_pin_list *entry = irq_2_pin + i;
1502 if (entry->pin < 0)
1503 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001504 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 for (;;) {
1506 printk("-> %d:%d", entry->apic, entry->pin);
1507 if (!entry->next)
1508 break;
1509 entry = irq_2_pin + entry->next;
1510 }
1511 printk("\n");
1512 }
1513
1514 printk(KERN_INFO ".................................... done.\n");
1515
1516 return;
1517}
1518
1519#if 0
1520
1521static void print_APIC_bitfield (int base)
1522{
1523 unsigned int v;
1524 int i, j;
1525
1526 if (apic_verbosity == APIC_QUIET)
1527 return;
1528
1529 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1530 for (i = 0; i < 8; i++) {
1531 v = apic_read(base + i*0x10);
1532 for (j = 0; j < 32; j++) {
1533 if (v & (1<<j))
1534 printk("1");
1535 else
1536 printk("0");
1537 }
1538 printk("\n");
1539 }
1540}
1541
1542void /*__init*/ print_local_APIC(void * dummy)
1543{
1544 unsigned int v, ver, maxlvt;
1545
1546 if (apic_verbosity == APIC_QUIET)
1547 return;
1548
1549 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1550 smp_processor_id(), hard_smp_processor_id());
1551 v = apic_read(APIC_ID);
1552 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1553 v = apic_read(APIC_LVR);
1554 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1555 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001556 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 v = apic_read(APIC_TASKPRI);
1559 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1560
1561 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1562 v = apic_read(APIC_ARBPRI);
1563 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1564 v & APIC_ARBPRI_MASK);
1565 v = apic_read(APIC_PROCPRI);
1566 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1567 }
1568
1569 v = apic_read(APIC_EOI);
1570 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1571 v = apic_read(APIC_RRR);
1572 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1573 v = apic_read(APIC_LDR);
1574 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1575 v = apic_read(APIC_DFR);
1576 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1577 v = apic_read(APIC_SPIV);
1578 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1579
1580 printk(KERN_DEBUG "... APIC ISR field:\n");
1581 print_APIC_bitfield(APIC_ISR);
1582 printk(KERN_DEBUG "... APIC TMR field:\n");
1583 print_APIC_bitfield(APIC_TMR);
1584 printk(KERN_DEBUG "... APIC IRR field:\n");
1585 print_APIC_bitfield(APIC_IRR);
1586
1587 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1588 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1589 apic_write(APIC_ESR, 0);
1590 v = apic_read(APIC_ESR);
1591 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1592 }
1593
1594 v = apic_read(APIC_ICR);
1595 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1596 v = apic_read(APIC_ICR2);
1597 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1598
1599 v = apic_read(APIC_LVTT);
1600 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1601
1602 if (maxlvt > 3) { /* PC is LVT#4. */
1603 v = apic_read(APIC_LVTPC);
1604 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1605 }
1606 v = apic_read(APIC_LVT0);
1607 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1608 v = apic_read(APIC_LVT1);
1609 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1610
1611 if (maxlvt > 2) { /* ERR is LVT#3. */
1612 v = apic_read(APIC_LVTERR);
1613 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1614 }
1615
1616 v = apic_read(APIC_TMICT);
1617 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1618 v = apic_read(APIC_TMCCT);
1619 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1620 v = apic_read(APIC_TDCR);
1621 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1622 printk("\n");
1623}
1624
1625void print_all_local_APICs (void)
1626{
1627 on_each_cpu(print_local_APIC, NULL, 1, 1);
1628}
1629
1630void /*__init*/ print_PIC(void)
1631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 unsigned int v;
1633 unsigned long flags;
1634
1635 if (apic_verbosity == APIC_QUIET)
1636 return;
1637
1638 printk(KERN_DEBUG "\nprinting PIC contents\n");
1639
1640 spin_lock_irqsave(&i8259A_lock, flags);
1641
1642 v = inb(0xa1) << 8 | inb(0x21);
1643 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1644
1645 v = inb(0xa0) << 8 | inb(0x20);
1646 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1647
1648 outb(0x0b,0xa0);
1649 outb(0x0b,0x20);
1650 v = inb(0xa0) << 8 | inb(0x20);
1651 outb(0x0a,0xa0);
1652 outb(0x0a,0x20);
1653
1654 spin_unlock_irqrestore(&i8259A_lock, flags);
1655
1656 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1657
1658 v = inb(0x4d1) << 8 | inb(0x4d0);
1659 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1660}
1661
1662#endif /* 0 */
1663
1664static void __init enable_IO_APIC(void)
1665{
1666 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001667 int i8259_apic, i8259_pin;
1668 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 unsigned long flags;
1670
1671 for (i = 0; i < PIN_MAP_SIZE; i++) {
1672 irq_2_pin[i].pin = -1;
1673 irq_2_pin[i].next = 0;
1674 }
1675 if (!pirqs_enabled)
1676 for (i = 0; i < MAX_PIRQS; i++)
1677 pirq_entries[i] = -1;
1678
1679 /*
1680 * The number of IO-APIC IRQ registers (== #pins):
1681 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001682 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001684 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001686 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1687 }
1688 for(apic = 0; apic < nr_ioapics; apic++) {
1689 int pin;
1690 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001691 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001692 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001693 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001694
1695
1696 /* If the interrupt line is enabled and in ExtInt mode
1697 * I have found the pin where the i8259 is connected.
1698 */
1699 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1700 ioapic_i8259.apic = apic;
1701 ioapic_i8259.pin = pin;
1702 goto found_i8259;
1703 }
1704 }
1705 }
1706 found_i8259:
1707 /* Look to see what if the MP table has reported the ExtINT */
1708 /* If we could not find the appropriate pin by looking at the ioapic
1709 * the i8259 probably is not connected the ioapic but give the
1710 * mptable a chance anyway.
1711 */
1712 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1713 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1714 /* Trust the MP table if nothing is setup in the hardware */
1715 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1716 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1717 ioapic_i8259.pin = i8259_pin;
1718 ioapic_i8259.apic = i8259_apic;
1719 }
1720 /* Complain if the MP table and the hardware disagree */
1721 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1722 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1723 {
1724 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
1727 /*
1728 * Do not trust the IO-APIC being empty at bootup
1729 */
1730 clear_IO_APIC();
1731}
1732
1733/*
1734 * Not an __init, needed by the reboot code
1735 */
1736void disable_IO_APIC(void)
1737{
1738 /*
1739 * Clear the IO-APIC before rebooting:
1740 */
1741 clear_IO_APIC();
1742
Eric W. Biederman650927e2005-06-25 14:57:44 -07001743 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001744 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001745 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001746 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001747 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001748 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001749 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001750
1751 memset(&entry, 0, sizeof(entry));
1752 entry.mask = 0; /* Enabled */
1753 entry.trigger = 0; /* Edge */
1754 entry.irr = 0;
1755 entry.polarity = 0; /* High */
1756 entry.delivery_status = 0;
1757 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001758 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001759 entry.vector = 0;
Vivek Goyal76865c32006-01-06 00:12:19 -08001760 entry.dest.physical.physical_dest =
1761 GET_APIC_ID(apic_read(APIC_ID));
Eric W. Biederman650927e2005-06-25 14:57:44 -07001762
1763 /*
1764 * Add it to the IO-APIC irq-routing table:
1765 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001766 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001767 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001768 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771/*
1772 * function to set the IO-APIC physical IDs based on the
1773 * values stored in the MPC table.
1774 *
1775 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1776 */
1777
1778#ifndef CONFIG_X86_NUMAQ
1779static void __init setup_ioapic_ids_from_mpc(void)
1780{
1781 union IO_APIC_reg_00 reg_00;
1782 physid_mask_t phys_id_present_map;
1783 int apic;
1784 int i;
1785 unsigned char old_id;
1786 unsigned long flags;
1787
1788 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001789 * Don't check I/O APIC IDs for xAPIC systems. They have
1790 * no meaning without the serial APIC bus.
1791 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001792 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1793 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001794 return;
1795 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * This is broken; anything with a real cpu count has to
1797 * circumvent this idiocy regardless.
1798 */
1799 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1800
1801 /*
1802 * Set the IOAPIC ID to the value stored in the MPC table.
1803 */
1804 for (apic = 0; apic < nr_ioapics; apic++) {
1805
1806 /* Read the register 0 value */
1807 spin_lock_irqsave(&ioapic_lock, flags);
1808 reg_00.raw = io_apic_read(apic, 0);
1809 spin_unlock_irqrestore(&ioapic_lock, flags);
1810
1811 old_id = mp_ioapics[apic].mpc_apicid;
1812
1813 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1814 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1815 apic, mp_ioapics[apic].mpc_apicid);
1816 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1817 reg_00.bits.ID);
1818 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1819 }
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /*
1822 * Sanity check, is the ID really free? Every APIC in a
1823 * system must have a unique ID or we get lots of nice
1824 * 'stuck on smp_invalidate_needed IPI wait' messages.
1825 */
1826 if (check_apicid_used(phys_id_present_map,
1827 mp_ioapics[apic].mpc_apicid)) {
1828 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1829 apic, mp_ioapics[apic].mpc_apicid);
1830 for (i = 0; i < get_physical_broadcast(); i++)
1831 if (!physid_isset(i, phys_id_present_map))
1832 break;
1833 if (i >= get_physical_broadcast())
1834 panic("Max APIC ID exceeded!\n");
1835 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1836 i);
1837 physid_set(i, phys_id_present_map);
1838 mp_ioapics[apic].mpc_apicid = i;
1839 } else {
1840 physid_mask_t tmp;
1841 tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1842 apic_printk(APIC_VERBOSE, "Setting %d in the "
1843 "phys_id_present_map\n",
1844 mp_ioapics[apic].mpc_apicid);
1845 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1846 }
1847
1848
1849 /*
1850 * We need to adjust the IRQ routing table
1851 * if the ID changed.
1852 */
1853 if (old_id != mp_ioapics[apic].mpc_apicid)
1854 for (i = 0; i < mp_irq_entries; i++)
1855 if (mp_irqs[i].mpc_dstapic == old_id)
1856 mp_irqs[i].mpc_dstapic
1857 = mp_ioapics[apic].mpc_apicid;
1858
1859 /*
1860 * Read the right value from the MPC table and
1861 * write it into the ID register.
1862 */
1863 apic_printk(APIC_VERBOSE, KERN_INFO
1864 "...changing IO-APIC physical APIC ID to %d ...",
1865 mp_ioapics[apic].mpc_apicid);
1866
1867 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1868 spin_lock_irqsave(&ioapic_lock, flags);
1869 io_apic_write(apic, 0, reg_00.raw);
1870 spin_unlock_irqrestore(&ioapic_lock, flags);
1871
1872 /*
1873 * Sanity check
1874 */
1875 spin_lock_irqsave(&ioapic_lock, flags);
1876 reg_00.raw = io_apic_read(apic, 0);
1877 spin_unlock_irqrestore(&ioapic_lock, flags);
1878 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1879 printk("could not set ID!\n");
1880 else
1881 apic_printk(APIC_VERBOSE, " ok.\n");
1882 }
1883}
1884#else
1885static void __init setup_ioapic_ids_from_mpc(void) { }
1886#endif
1887
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001888int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001889
1890static int __init notimercheck(char *s)
1891{
1892 no_timer_check = 1;
1893 return 1;
1894}
1895__setup("no_timer_check", notimercheck);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/*
1898 * There is a nasty bug in some older SMP boards, their mptable lies
1899 * about the timer IRQ. We do the following to work around the situation:
1900 *
1901 * - timer IRQ defaults to IO-APIC IRQ
1902 * - if this function detects that timer IRQs are defunct, then we fall
1903 * back to ISA timer IRQs
1904 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001905static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 unsigned long t1 = jiffies;
1908
Zachary Amsden8542b202006-12-07 02:14:09 +01001909 if (no_timer_check)
1910 return 1;
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 local_irq_enable();
1913 /* Let ten ticks pass... */
1914 mdelay((10 * 1000) / HZ);
1915
1916 /*
1917 * Expect a few ticks at least, to be sure some possible
1918 * glue logic does not lock up after one or two first
1919 * ticks in a non-ExtINT mode. Also the local APIC
1920 * might have cached one ExtINT interrupt. Finally, at
1921 * least one tick may be lost due to delays.
1922 */
1923 if (jiffies - t1 > 4)
1924 return 1;
1925
1926 return 0;
1927}
1928
1929/*
1930 * In the SMP+IOAPIC case it might happen that there are an unspecified
1931 * number of pending IRQ events unhandled. These cases are very rare,
1932 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1933 * better to do it this way as thus we do not have to be aware of
1934 * 'pending' interrupts in the IRQ path, except at this point.
1935 */
1936/*
1937 * Edge triggered needs to resend any interrupt
1938 * that was delayed but this is now handled in the device
1939 * independent code.
1940 */
1941
1942/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001943 * Startup quirk:
1944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * Starting up a edge-triggered IO-APIC interrupt is
1946 * nasty - we need to make sure that we get the edge.
1947 * If it is already asserted for some reason, we need
1948 * return 1 to indicate that is was pending.
1949 *
1950 * This is not complete - we should be able to fake
1951 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001952 *
1953 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001955static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 int was_pending = 0;
1958 unsigned long flags;
1959
1960 spin_lock_irqsave(&ioapic_lock, flags);
1961 if (irq < 16) {
1962 disable_8259A_irq(irq);
1963 if (i8259A_irq_pending(irq))
1964 was_pending = 1;
1965 }
1966 __unmask_IO_APIC_irq(irq);
1967 spin_unlock_irqrestore(&ioapic_lock, flags);
1968
1969 return was_pending;
1970}
1971
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001972static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001974 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 ack_APIC_irq();
1976}
1977
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001978static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 unsigned long v;
1981 int i;
1982
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001983 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984/*
1985 * It appears there is an erratum which affects at least version 0x11
1986 * of I/O APIC (that's the 82093AA and cores integrated into various
1987 * chipsets). Under certain conditions a level-triggered interrupt is
1988 * erroneously delivered as edge-triggered one but the respective IRR
1989 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1990 * message but it will never arrive and further interrupts are blocked
1991 * from the source. The exact reason is so far unknown, but the
1992 * phenomenon was observed when two consecutive interrupt requests
1993 * from a given source get delivered to the same CPU and the source is
1994 * temporarily disabled in between.
1995 *
1996 * A workaround is to simulate an EOI message manually. We achieve it
1997 * by setting the trigger mode to edge and then to level when the edge
1998 * trigger mode gets detected in the TMR of a local APIC for a
1999 * level-triggered interrupt. We mask the source for the time of the
2000 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2001 * The idea is from Manfred Spraul. --macro
2002 */
Eric W. Biedermanb940d222006-10-08 07:43:46 -06002003 i = irq_vector[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2006
2007 ack_APIC_irq();
2008
2009 if (!(v & (1 << (i & 0x1f)))) {
2010 atomic_inc(&irq_mis_count);
2011 spin_lock(&ioapic_lock);
2012 __mask_and_edge_IO_APIC_irq(irq);
2013 __unmask_and_level_IO_APIC_irq(irq);
2014 spin_unlock(&ioapic_lock);
2015 }
2016}
2017
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002018static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Eric W. Biedermanb940d222006-10-08 07:43:46 -06002020 send_IPI_self(irq_vector[irq]);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002021
2022 return 1;
2023}
2024
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002025static struct irq_chip ioapic_chip __read_mostly = {
2026 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002027 .startup = startup_ioapic_irq,
2028 .mask = mask_IO_APIC_irq,
2029 .unmask = unmask_IO_APIC_irq,
2030 .ack = ack_ioapic_irq,
2031 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002032#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002033 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002034#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002035 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036};
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039static inline void init_IO_APIC_traps(void)
2040{
2041 int irq;
2042
2043 /*
2044 * NOTE! The local APIC isn't very good at handling
2045 * multiple interrupts at the same interrupt level.
2046 * As the interrupt level is determined by taking the
2047 * vector number and shifting that right by 4, we
2048 * want to spread these out a bit so that they don't
2049 * all fall in the same interrupt level.
2050 *
2051 * Also, we've got to be careful not to trash gate
2052 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2053 */
2054 for (irq = 0; irq < NR_IRQS ; irq++) {
2055 int tmp = irq;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06002056 if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /*
2058 * Hmm.. We don't have an entry for this,
2059 * so default to an old-fashioned 8259
2060 * interrupt if we can..
2061 */
2062 if (irq < 16)
2063 make_8259A_irq(irq);
2064 else
2065 /* Strange. Oh, well.. */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002066 irq_desc[irq].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 }
2069}
2070
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002071/*
2072 * The local APIC irq-chip implementation:
2073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002075static void ack_apic(unsigned int irq)
2076{
2077 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002080static void mask_lapic_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 unsigned long v;
2083
2084 v = apic_read(APIC_LVT0);
2085 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2086}
2087
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002088static void unmask_lapic_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002090 unsigned long v;
2091
2092 v = apic_read(APIC_LVT0);
2093 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002096static struct irq_chip lapic_chip __read_mostly = {
2097 .name = "local-APIC-edge",
2098 .mask = mask_lapic_irq,
2099 .unmask = unmask_lapic_irq,
2100 .eoi = ack_apic,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101};
2102
2103static void setup_nmi (void)
2104{
2105 /*
2106 * Dirty trick to enable the NMI watchdog ...
2107 * We put the 8259A master into AEOI mode and
2108 * unmask on all local APICs LVT0 as NMI.
2109 *
2110 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2111 * is from Maciej W. Rozycki - so we do not have to EOI from
2112 * the NMI handler or the timer interrupt.
2113 */
2114 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2115
2116 on_each_cpu(enable_NMI_through_LVT0, NULL, 1, 1);
2117
2118 apic_printk(APIC_VERBOSE, " done.\n");
2119}
2120
2121/*
2122 * This looks a bit hackish but it's about the only one way of sending
2123 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2124 * not support the ExtINT mode, unfortunately. We need to send these
2125 * cycles as some i82489DX-based boards have glue logic that keeps the
2126 * 8259A interrupt line asserted until INTA. --macro
2127 */
2128static inline void unlock_ExtINT_logic(void)
2129{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002130 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct IO_APIC_route_entry entry0, entry1;
2132 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002134 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002135 if (pin == -1) {
2136 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002138 }
2139 apic = find_isa_irq_apic(8, mp_INT);
2140 if (apic == -1) {
2141 WARN_ON_ONCE(1);
2142 return;
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Andi Kleencf4c6a22006-09-26 10:52:30 +02002145 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002146 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 memset(&entry1, 0, sizeof(entry1));
2149
2150 entry1.dest_mode = 0; /* physical delivery */
2151 entry1.mask = 0; /* unmask IRQ now */
2152 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2153 entry1.delivery_mode = dest_ExtINT;
2154 entry1.polarity = entry0.polarity;
2155 entry1.trigger = 0;
2156 entry1.vector = 0;
2157
Andi Kleencf4c6a22006-09-26 10:52:30 +02002158 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 save_control = CMOS_READ(RTC_CONTROL);
2161 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2162 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2163 RTC_FREQ_SELECT);
2164 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2165
2166 i = 100;
2167 while (i-- > 0) {
2168 mdelay(10);
2169 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2170 i -= 10;
2171 }
2172
2173 CMOS_WRITE(save_control, RTC_CONTROL);
2174 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002175 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Andi Kleencf4c6a22006-09-26 10:52:30 +02002177 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Kimball Murraye0c1e9b2006-05-08 15:17:16 +02002180int timer_uses_ioapic_pin_0;
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/*
2183 * This code may look a bit paranoid, but it's supposed to cooperate with
2184 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2185 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2186 * fanatically on his truly buggy board.
2187 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002188static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002190 int apic1, pin1, apic2, pin2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 int vector;
2192
2193 /*
2194 * get/set the timer IRQ vector:
2195 */
2196 disable_8259A_irq(0);
2197 vector = assign_irq_vector(0);
2198 set_intr_gate(vector, interrupt[0]);
2199
2200 /*
2201 * Subtle, code in do_timer_interrupt() expects an AEOI
2202 * mode for the 8259A whenever interrupts are routed
2203 * through I/O APICs. Also IRQ0 has to be enabled in
2204 * the 8259A which implies the virtual wire has to be
2205 * disabled in the local APIC.
2206 */
2207 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2208 init_8259A(1);
2209 timer_ack = 1;
Andi Kleenf9262c12006-03-08 17:57:25 -08002210 if (timer_over_8254 > 0)
2211 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002213 pin1 = find_isa_irq_pin(0, mp_INT);
2214 apic1 = find_isa_irq_apic(0, mp_INT);
2215 pin2 = ioapic_i8259.pin;
2216 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Kimball Murraye0c1e9b2006-05-08 15:17:16 +02002218 if (pin1 == 0)
2219 timer_uses_ioapic_pin_0 = 1;
2220
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002221 printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2222 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (pin1 != -1) {
2225 /*
2226 * Ok, does IRQ0 through the IOAPIC work?
2227 */
2228 unmask_IO_APIC_irq(0);
2229 if (timer_irq_works()) {
2230 if (nmi_watchdog == NMI_IO_APIC) {
2231 disable_8259A_irq(0);
2232 setup_nmi();
2233 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002235 if (disable_timer_pin_1 > 0)
2236 clear_IO_APIC_pin(0, pin1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return;
2238 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002239 clear_IO_APIC_pin(apic1, pin1);
2240 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to "
2241 "IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243
2244 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
2245 if (pin2 != -1) {
2246 printk("\n..... (found pin %d) ...", pin2);
2247 /*
2248 * legacy devices should be connected to IO APIC #0
2249 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002250 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (timer_irq_works()) {
2252 printk("works.\n");
2253 if (pin1 != -1)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002254 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 else
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002256 add_pin_to_irq(0, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (nmi_watchdog == NMI_IO_APIC) {
2258 setup_nmi();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260 return;
2261 }
2262 /*
2263 * Cleanup, just in case ...
2264 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002265 clear_IO_APIC_pin(apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 printk(" failed.\n");
2268
2269 if (nmi_watchdog == NMI_IO_APIC) {
2270 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2271 nmi_watchdog = 0;
2272 }
2273
2274 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2275
2276 disable_8259A_irq(0);
Ingo Molnara460e742006-10-17 00:10:03 -07002277 set_irq_chip_and_handler_name(0, &lapic_chip, handle_fasteoi_irq,
Maciej W. Rozycki2e188932007-02-13 13:26:20 +01002278 "fasteoi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2280 enable_8259A_irq(0);
2281
2282 if (timer_irq_works()) {
2283 printk(" works.\n");
2284 return;
2285 }
2286 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2287 printk(" failed.\n");
2288
2289 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2290
2291 timer_ack = 0;
2292 init_8259A(0);
2293 make_8259A_irq(0);
2294 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2295
2296 unlock_ExtINT_logic();
2297
2298 if (timer_irq_works()) {
2299 printk(" works.\n");
2300 return;
2301 }
2302 printk(" failed :(.\n");
2303 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2304 "report. Then try booting with the 'noapic' option");
2305}
2306
2307/*
2308 *
2309 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2310 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2311 * Linux doesn't really care, as it's not actually used
2312 * for any interrupt handling anyway.
2313 */
2314#define PIC_IRQS (1 << PIC_CASCADE_IR)
2315
2316void __init setup_IO_APIC(void)
2317{
2318 enable_IO_APIC();
2319
2320 if (acpi_ioapic)
2321 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2322 else
2323 io_apic_irqs = ~PIC_IRQS;
2324
2325 printk("ENABLING IO-APIC IRQs\n");
2326
2327 /*
2328 * Set up IO-APIC IRQ routing.
2329 */
2330 if (!acpi_ioapic)
2331 setup_ioapic_ids_from_mpc();
2332 sync_Arb_IDs();
2333 setup_IO_APIC_irqs();
2334 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002335 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (!acpi_ioapic)
2337 print_IO_APIC();
2338}
2339
Andi Kleenf9262c12006-03-08 17:57:25 -08002340static int __init setup_disable_8254_timer(char *s)
2341{
2342 timer_over_8254 = -1;
2343 return 1;
2344}
2345static int __init setup_enable_8254_timer(char *s)
2346{
2347 timer_over_8254 = 2;
2348 return 1;
2349}
2350
2351__setup("disable_8254_timer", setup_disable_8254_timer);
2352__setup("enable_8254_timer", setup_enable_8254_timer);
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354/*
2355 * Called after all the initialization is done. If we didnt find any
2356 * APIC bugs then we can allow the modify fast path
2357 */
2358
2359static int __init io_apic_bug_finalize(void)
2360{
2361 if(sis_apic_bug == -1)
2362 sis_apic_bug = 0;
2363 return 0;
2364}
2365
2366late_initcall(io_apic_bug_finalize);
2367
2368struct sysfs_ioapic_data {
2369 struct sys_device dev;
2370 struct IO_APIC_route_entry entry[0];
2371};
2372static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2373
Pavel Machek438510f2005-04-16 15:25:24 -07002374static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 struct IO_APIC_route_entry *entry;
2377 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 int i;
2379
2380 data = container_of(dev, struct sysfs_ioapic_data, dev);
2381 entry = data->entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02002382 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2383 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 return 0;
2386}
2387
2388static int ioapic_resume(struct sys_device *dev)
2389{
2390 struct IO_APIC_route_entry *entry;
2391 struct sysfs_ioapic_data *data;
2392 unsigned long flags;
2393 union IO_APIC_reg_00 reg_00;
2394 int i;
2395
2396 data = container_of(dev, struct sysfs_ioapic_data, dev);
2397 entry = data->entry;
2398
2399 spin_lock_irqsave(&ioapic_lock, flags);
2400 reg_00.raw = io_apic_read(dev->id, 0);
2401 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2402 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2403 io_apic_write(dev->id, 0, reg_00.raw);
2404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 spin_unlock_irqrestore(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +02002406 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2407 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 return 0;
2410}
2411
2412static struct sysdev_class ioapic_sysdev_class = {
2413 set_kset_name("ioapic"),
2414 .suspend = ioapic_suspend,
2415 .resume = ioapic_resume,
2416};
2417
2418static int __init ioapic_init_sysfs(void)
2419{
2420 struct sys_device * dev;
2421 int i, size, error = 0;
2422
2423 error = sysdev_class_register(&ioapic_sysdev_class);
2424 if (error)
2425 return error;
2426
2427 for (i = 0; i < nr_ioapics; i++ ) {
2428 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2429 * sizeof(struct IO_APIC_route_entry);
2430 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2431 if (!mp_ioapic_data[i]) {
2432 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2433 continue;
2434 }
2435 memset(mp_ioapic_data[i], 0, size);
2436 dev = &mp_ioapic_data[i]->dev;
2437 dev->id = i;
2438 dev->cls = &ioapic_sysdev_class;
2439 error = sysdev_register(dev);
2440 if (error) {
2441 kfree(mp_ioapic_data[i]);
2442 mp_ioapic_data[i] = NULL;
2443 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2444 continue;
2445 }
2446 }
2447
2448 return 0;
2449}
2450
2451device_initcall(ioapic_init_sysfs);
2452
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002453/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002454 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002455 */
2456int create_irq(void)
2457{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002458 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002459 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002460 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002461
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002462 irq = -ENOSPC;
2463 spin_lock_irqsave(&vector_lock, flags);
2464 for (new = (NR_IRQS - 1); new >= 0; new--) {
2465 if (platform_legacy_irq(new))
2466 continue;
2467 if (irq_vector[new] != 0)
2468 continue;
2469 vector = __assign_irq_vector(new);
2470 if (likely(vector > 0))
2471 irq = new;
2472 break;
2473 }
2474 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002475
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002476 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002477 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002478 dynamic_irq_init(irq);
2479 }
2480 return irq;
2481}
2482
2483void destroy_irq(unsigned int irq)
2484{
2485 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002486
2487 dynamic_irq_cleanup(irq);
2488
2489 spin_lock_irqsave(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002490 irq_vector[irq] = 0;
2491 spin_unlock_irqrestore(&vector_lock, flags);
2492}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002493
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002494/*
2495 * MSI mesage composition
2496 */
2497#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002498static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002499{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002500 int vector;
2501 unsigned dest;
2502
2503 vector = assign_irq_vector(irq);
2504 if (vector >= 0) {
2505 dest = cpu_mask_to_apicid(TARGET_CPUS);
2506
2507 msg->address_hi = MSI_ADDR_BASE_HI;
2508 msg->address_lo =
2509 MSI_ADDR_BASE_LO |
2510 ((INT_DEST_MODE == 0) ?
2511 MSI_ADDR_DEST_MODE_PHYSICAL:
2512 MSI_ADDR_DEST_MODE_LOGICAL) |
2513 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2514 MSI_ADDR_REDIRECTION_CPU:
2515 MSI_ADDR_REDIRECTION_LOWPRI) |
2516 MSI_ADDR_DEST_ID(dest);
2517
2518 msg->data =
2519 MSI_DATA_TRIGGER_EDGE |
2520 MSI_DATA_LEVEL_ASSERT |
2521 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2522 MSI_DATA_DELIVERY_FIXED:
2523 MSI_DATA_DELIVERY_LOWPRI) |
2524 MSI_DATA_VECTOR(vector);
2525 }
2526 return vector;
2527}
2528
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002529#ifdef CONFIG_SMP
2530static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2531{
2532 struct msi_msg msg;
2533 unsigned int dest;
2534 cpumask_t tmp;
2535 int vector;
2536
2537 cpus_and(tmp, mask, cpu_online_map);
2538 if (cpus_empty(tmp))
2539 tmp = TARGET_CPUS;
2540
2541 vector = assign_irq_vector(irq);
2542 if (vector < 0)
2543 return;
2544
2545 dest = cpu_mask_to_apicid(mask);
2546
2547 read_msi_msg(irq, &msg);
2548
2549 msg.data &= ~MSI_DATA_VECTOR_MASK;
2550 msg.data |= MSI_DATA_VECTOR(vector);
2551 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2552 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2553
2554 write_msi_msg(irq, &msg);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002555 irq_desc[irq].affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002556}
2557#endif /* CONFIG_SMP */
2558
2559/*
2560 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2561 * which implement the MSI or MSI-X Capability Structure.
2562 */
2563static struct irq_chip msi_chip = {
2564 .name = "PCI-MSI",
2565 .unmask = unmask_msi_irq,
2566 .mask = mask_msi_irq,
2567 .ack = ack_ioapic_irq,
2568#ifdef CONFIG_SMP
2569 .set_affinity = set_msi_irq_affinity,
2570#endif
2571 .retrigger = ioapic_retrigger_irq,
2572};
2573
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002574int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002575{
2576 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002577 int irq, ret;
2578 irq = create_irq();
2579 if (irq < 0)
2580 return irq;
2581
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002582 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002583 if (ret < 0) {
2584 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002585 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002586 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002587
Michael Ellerman7fe37302007-04-18 19:39:21 +10002588 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002589 write_msi_msg(irq, &msg);
2590
Ingo Molnara460e742006-10-17 00:10:03 -07002591 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2592 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002593
Michael Ellerman7fe37302007-04-18 19:39:21 +10002594 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002595}
2596
2597void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002598{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002599 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002600}
2601
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002602#endif /* CONFIG_PCI_MSI */
2603
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002604/*
2605 * Hypertransport interrupt support
2606 */
2607#ifdef CONFIG_HT_IRQ
2608
2609#ifdef CONFIG_SMP
2610
2611static void target_ht_irq(unsigned int irq, unsigned int dest)
2612{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002613 struct ht_irq_msg msg;
2614 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002615
Eric W. Biedermanec683072006-11-08 17:44:57 -08002616 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2617 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002618
Eric W. Biedermanec683072006-11-08 17:44:57 -08002619 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2620 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002621
Eric W. Biedermanec683072006-11-08 17:44:57 -08002622 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002623}
2624
2625static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2626{
2627 unsigned int dest;
2628 cpumask_t tmp;
2629
2630 cpus_and(tmp, mask, cpu_online_map);
2631 if (cpus_empty(tmp))
2632 tmp = TARGET_CPUS;
2633
2634 cpus_and(mask, tmp, CPU_MASK_ALL);
2635
2636 dest = cpu_mask_to_apicid(mask);
2637
2638 target_ht_irq(irq, dest);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002639 irq_desc[irq].affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002640}
2641#endif
2642
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002643static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002644 .name = "PCI-HT",
2645 .mask = mask_ht_irq,
2646 .unmask = unmask_ht_irq,
2647 .ack = ack_ioapic_irq,
2648#ifdef CONFIG_SMP
2649 .set_affinity = set_ht_irq_affinity,
2650#endif
2651 .retrigger = ioapic_retrigger_irq,
2652};
2653
2654int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2655{
2656 int vector;
2657
2658 vector = assign_irq_vector(irq);
2659 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002660 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002661 unsigned dest;
2662 cpumask_t tmp;
2663
2664 cpus_clear(tmp);
2665 cpu_set(vector >> 8, tmp);
2666 dest = cpu_mask_to_apicid(tmp);
2667
Eric W. Biedermanec683072006-11-08 17:44:57 -08002668 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002669
Eric W. Biedermanec683072006-11-08 17:44:57 -08002670 msg.address_lo =
2671 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002672 HT_IRQ_LOW_DEST_ID(dest) |
2673 HT_IRQ_LOW_VECTOR(vector) |
2674 ((INT_DEST_MODE == 0) ?
2675 HT_IRQ_LOW_DM_PHYSICAL :
2676 HT_IRQ_LOW_DM_LOGICAL) |
2677 HT_IRQ_LOW_RQEOI_EDGE |
2678 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2679 HT_IRQ_LOW_MT_FIXED :
2680 HT_IRQ_LOW_MT_ARBITRATED) |
2681 HT_IRQ_LOW_IRQ_MASKED;
2682
Eric W. Biedermanec683072006-11-08 17:44:57 -08002683 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002684
Ingo Molnara460e742006-10-17 00:10:03 -07002685 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2686 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002687 }
2688 return vector;
2689}
2690#endif /* CONFIG_HT_IRQ */
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692/* --------------------------------------------------------------------------
2693 ACPI-based IOAPIC Configuration
2694 -------------------------------------------------------------------------- */
2695
Len Brown888ba6c2005-08-24 12:07:20 -04002696#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698int __init io_apic_get_unique_id (int ioapic, int apic_id)
2699{
2700 union IO_APIC_reg_00 reg_00;
2701 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2702 physid_mask_t tmp;
2703 unsigned long flags;
2704 int i = 0;
2705
2706 /*
2707 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2708 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2709 * supports up to 16 on one shared APIC bus.
2710 *
2711 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2712 * advantage of new APIC bus architecture.
2713 */
2714
2715 if (physids_empty(apic_id_map))
2716 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2717
2718 spin_lock_irqsave(&ioapic_lock, flags);
2719 reg_00.raw = io_apic_read(ioapic, 0);
2720 spin_unlock_irqrestore(&ioapic_lock, flags);
2721
2722 if (apic_id >= get_physical_broadcast()) {
2723 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2724 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2725 apic_id = reg_00.bits.ID;
2726 }
2727
2728 /*
2729 * Every APIC in a system must have a unique ID or we get lots of nice
2730 * 'stuck on smp_invalidate_needed IPI wait' messages.
2731 */
2732 if (check_apicid_used(apic_id_map, apic_id)) {
2733
2734 for (i = 0; i < get_physical_broadcast(); i++) {
2735 if (!check_apicid_used(apic_id_map, i))
2736 break;
2737 }
2738
2739 if (i == get_physical_broadcast())
2740 panic("Max apic_id exceeded!\n");
2741
2742 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2743 "trying %d\n", ioapic, apic_id, i);
2744
2745 apic_id = i;
2746 }
2747
2748 tmp = apicid_to_cpu_present(apic_id);
2749 physids_or(apic_id_map, apic_id_map, tmp);
2750
2751 if (reg_00.bits.ID != apic_id) {
2752 reg_00.bits.ID = apic_id;
2753
2754 spin_lock_irqsave(&ioapic_lock, flags);
2755 io_apic_write(ioapic, 0, reg_00.raw);
2756 reg_00.raw = io_apic_read(ioapic, 0);
2757 spin_unlock_irqrestore(&ioapic_lock, flags);
2758
2759 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002760 if (reg_00.bits.ID != apic_id) {
2761 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2762 return -1;
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765
2766 apic_printk(APIC_VERBOSE, KERN_INFO
2767 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2768
2769 return apic_id;
2770}
2771
2772
2773int __init io_apic_get_version (int ioapic)
2774{
2775 union IO_APIC_reg_01 reg_01;
2776 unsigned long flags;
2777
2778 spin_lock_irqsave(&ioapic_lock, flags);
2779 reg_01.raw = io_apic_read(ioapic, 1);
2780 spin_unlock_irqrestore(&ioapic_lock, flags);
2781
2782 return reg_01.bits.version;
2783}
2784
2785
2786int __init io_apic_get_redir_entries (int ioapic)
2787{
2788 union IO_APIC_reg_01 reg_01;
2789 unsigned long flags;
2790
2791 spin_lock_irqsave(&ioapic_lock, flags);
2792 reg_01.raw = io_apic_read(ioapic, 1);
2793 spin_unlock_irqrestore(&ioapic_lock, flags);
2794
2795 return reg_01.bits.entries;
2796}
2797
2798
2799int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2800{
2801 struct IO_APIC_route_entry entry;
2802 unsigned long flags;
2803
2804 if (!IO_APIC_IRQ(irq)) {
2805 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2806 ioapic);
2807 return -EINVAL;
2808 }
2809
2810 /*
2811 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2812 * Note that we mask (disable) IRQs now -- these get enabled when the
2813 * corresponding device driver registers for this IRQ.
2814 */
2815
2816 memset(&entry,0,sizeof(entry));
2817
2818 entry.delivery_mode = INT_DELIVERY_MODE;
2819 entry.dest_mode = INT_DEST_MODE;
2820 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2821 entry.trigger = edge_level;
2822 entry.polarity = active_high_low;
2823 entry.mask = 1;
2824
2825 /*
2826 * IRQs < 16 are already in the irq_2_pin[] map
2827 */
2828 if (irq >= 16)
2829 add_pin_to_irq(irq, ioapic, pin);
2830
2831 entry.vector = assign_irq_vector(irq);
2832
2833 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2834 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2835 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2836 edge_level, active_high_low);
2837
2838 ioapic_register_intr(irq, entry.vector, edge_level);
2839
2840 if (!ioapic && (irq < 16))
2841 disable_8259A_irq(irq);
2842
2843 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +01002844 __ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 spin_unlock_irqrestore(&ioapic_lock, flags);
2846
2847 return 0;
2848}
2849
Len Brown888ba6c2005-08-24 12:07:20 -04002850#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002851
2852static int __init parse_disable_timer_pin_1(char *arg)
2853{
2854 disable_timer_pin_1 = 1;
2855 return 0;
2856}
2857early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2858
2859static int __init parse_enable_timer_pin_1(char *arg)
2860{
2861 disable_timer_pin_1 = -1;
2862 return 0;
2863}
2864early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2865
2866static int __init parse_noapic(char *arg)
2867{
2868 /* disable IO-APIC */
2869 disable_ioapic_setup();
2870 return 0;
2871}
2872early_param("noapic", parse_noapic);