summaryrefslogtreecommitdiffstats
path: root/arch/ia64/dig/iosapic.c
blob: 7d9a084fdaff06faaf5cc8750e4ab62c31ab86e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * Streamlined APIC support.
 *
 * Copyright (C) 1999 Intel Corp.
 * Copyright (C) 1999 Asit Mallick <asit.k.mallick@intel.com>
 * Copyright (C) 1999-2000 Hewlett-Packard Co.
 * Copyright (C) 1999-2000 David Mosberger-Tang <davidm@hpl.hp.com>
 * Copyright (C) 1999 VA Linux Systems
 * Copyright (C) 1999,2000 Walt Drummond <drummond@valinux.com>
 *
 * 00/04/19	D. Mosberger	Rewritten to mirror more closely the x86 I/O APIC code.
 *				In particular, we now have separate handlers for edge
 *				and level triggered interrupts.
 */
#include <linux/config.h>

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/smp.h>
#include <linux/smp_lock.h>
#include <linux/string.h>
#include <linux/irq.h>

#include <asm/acpi-ext.h>
#include <asm/delay.h>
#include <asm/io.h>
#include <asm/iosapic.h>
#include <asm/machvec.h>
#include <asm/processor.h>
#include <asm/ptrace.h>
#include <asm/system.h>

#undef DEBUG_IRQ_ROUTING

static spinlock_t iosapic_lock = SPIN_LOCK_UNLOCKED;

struct iosapic_vector iosapic_vector[NR_IRQS] = {
	[0 ... NR_IRQS-1] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
};

/*
 * find the IRQ in the IOSAPIC map for the PCI device on bus/slot/pin
 */
int
iosapic_get_PCI_irq_vector (int bus, int slot, int pci_pin)
{
	int i;

	for (i = 0; i < NR_IRQS; i++) {
		if ((iosapic_bustype(i) == BUS_PCI) &&
		    (iosapic_bus(i) == bus) &&
		    (iosapic_busdata(i) == ((slot << 16) | pci_pin))) {
			return i;
		}
	}
	return -1;
}

static void
set_rte (unsigned long iosapic_addr, int entry, int pol, int trigger, int delivery,
	 long dest, int vector)
{
	u32 low32;
	u32 high32;

	low32 = ((pol << IO_SAPIC_POLARITY_SHIFT) |
		 (trigger << IO_SAPIC_TRIGGER_SHIFT) |
		 (delivery << IO_SAPIC_DELIVERY_SHIFT) |
		 vector);

#ifdef CONFIG_IA64_AZUSA_HACKS
	/* set Flush Disable bit */
	if (iosapic_addr != 0xc0000000fec00000)
		low32 |= (1 << 17);
#endif

	/* dest contains both id and eid */
	high32 = (dest << IO_SAPIC_DEST_SHIFT);	

	writel(IO_SAPIC_RTE_HIGH(entry), iosapic_addr + IO_SAPIC_REG_SELECT);
	writel(high32, iosapic_addr + IO_SAPIC_WINDOW);
	writel(IO_SAPIC_RTE_LOW(entry), iosapic_addr + IO_SAPIC_REG_SELECT);
	writel(low32, iosapic_addr + IO_SAPIC_WINDOW);
}

static void
nop (unsigned int irq)
{
	/* do nothing... */
}

static void 
mask_irq (unsigned int irq)
{
	unsigned long flags, iosapic_addr = iosapic_addr(irq);
	u32 low32;

	spin_lock_irqsave(&iosapic_lock, flags);
	{
		writel(IO_SAPIC_RTE_LOW(iosapic_pin(irq)), iosapic_addr + IO_SAPIC_REG_SELECT);
		low32 = readl(iosapic_addr + IO_SAPIC_WINDOW);

		low32 |= (1 << IO_SAPIC_MASK_SHIFT);    /* Zero only the mask bit */
		writel(low32, iosapic_addr + IO_SAPIC_WINDOW);
	}
	spin_unlock_irqrestore(&iosapic_lock, flags);
}

static void 
unmask_irq (unsigned int irq)
{
	unsigned long flags, iosapic_addr = iosapic_addr(irq);
	u32 low32;

	spin_lock_irqsave(&iosapic_lock, flags);
	{
		writel(IO_SAPIC_RTE_LOW(iosapic_pin(irq)), iosapic_addr + IO_SAPIC_REG_SELECT);
		low32 = readl(iosapic_addr + IO_SAPIC_WINDOW);

		low32 &= ~(1 << IO_SAPIC_MASK_SHIFT);    /* Zero only the mask bit */
		writel(low32, iosapic_addr + IO_SAPIC_WINDOW);
	}
	spin_unlock_irqrestore(&iosapic_lock, flags);
}


static void
iosapic_set_affinity (unsigned int irq, unsigned long mask)
{
	printk("iosapic_set_affinity: not implemented yet\n");
}

/*
 * Handlers for level-triggered interrupts.
 */

static unsigned int
iosapic_startup_level_irq (unsigned int irq)
{
	unmask_irq(irq);
	return 0;
}

static void
iosapic_end_level_irq (unsigned int irq)
{
	writel(irq, iosapic_addr(irq) + IO_SAPIC_EOI);
}

#define iosapic_shutdown_level_irq	mask_irq
#define iosapic_enable_level_irq	unmask_irq
#define iosapic_disable_level_irq	mask_irq
#define iosapic_ack_level_irq		nop

struct hw_interrupt_type irq_type_iosapic_level = {
	typename:	"IO-SAPIC-level",
	startup:	iosapic_startup_level_irq,
	shutdown:	iosapic_shutdown_level_irq,
	enable:		iosapic_enable_level_irq,
	disable:	iosapic_disable_level_irq,
	ack:		iosapic_ack_level_irq,
	end:		iosapic_end_level_irq,
	set_affinity:	iosapic_set_affinity
};

/*
 * Handlers for edge-triggered interrupts.
 */

static unsigned int
iosapic_startup_edge_irq (unsigned int irq)
{
	unmask_irq(irq);
	/*
	 * IOSAPIC simply drops interrupts pended while the
	 * corresponding pin was masked, so we can't know if an
	 * interrupt is pending already.  Let's hope not...
	 */
	return 0;
}

static void
iosapic_ack_edge_irq (unsigned int irq)
{
	/*
	 * Once we have recorded IRQ_PENDING already, we can mask the
	 * interrupt for real. This prevents IRQ storms from unhandled
	 * devices.
	 */
	if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED)) == (IRQ_PENDING | IRQ_DISABLED))
		mask_irq(irq);
}

#define iosapic_enable_edge_irq		unmask_irq
#define iosapic_disable_edge_irq	nop
#define iosapic_end_edge_irq		nop

struct hw_interrupt_type irq_type_iosapic_edge = {
	typename:	"IO-SAPIC-edge",
	startup:	iosapic_startup_edge_irq,
	shutdown:	iosapic_disable_edge_irq,
	enable:		iosapic_enable_edge_irq,
	disable:	iosapic_disable_edge_irq,
	ack:		iosapic_ack_edge_irq,
	end:		iosapic_end_edge_irq,
	set_affinity:	iosapic_set_affinity
};

unsigned int
iosapic_version (unsigned long base_addr) 
{
	/*
	 * IOSAPIC Version Register return 32 bit structure like:
	 * {
	 *	unsigned int version   : 8;
	 *	unsigned int reserved1 : 8;
	 *	unsigned int pins      : 8;
	 *	unsigned int reserved2 : 8;
	 * }
	 */
	writel(IO_SAPIC_VERSION, base_addr + IO_SAPIC_REG_SELECT);
	return readl(IO_SAPIC_WINDOW + base_addr);
}

void
iosapic_init (unsigned long address, int irqbase)
{
	struct hw_interrupt_type *irq_type;
	struct pci_vector_struct *vectors;
	int i, irq;

	if (irqbase == 0)
		/* 
		 * Map the legacy ISA devices into the IOSAPIC data.
		 * Some of these may get reprogrammed later on with
		 * data from the ACPI Interrupt Source Override table.
		 */
		for (i = 0; i < 16; i++) {
			irq = isa_irq_to_vector(i);
			iosapic_pin(irq) = i; 
			iosapic_bus(irq) = BUS_ISA;
			iosapic_busdata(irq) = 0;
			iosapic_dmode(irq) = IO_SAPIC_LOWEST_PRIORITY;
			iosapic_trigger(irq)  = IO_SAPIC_EDGE;
			iosapic_polarity(irq) = IO_SAPIC_POL_HIGH;
#ifdef DEBUG_IRQ_ROUTING
			printk("ISA: IRQ %02x -> Vector %02x IOSAPIC Pin %d\n",
			       i, irq, iosapic_pin(irq));
#endif
		}

#ifndef CONFIG_IA64_SOFTSDV_HACKS
	/* 
	 * Map the PCI Interrupt data into the ACPI IOSAPIC data using
	 * the info that the bootstrap loader passed to us.
	 */
	ia64_boot_param.pci_vectors = (__u64) __va(ia64_boot_param.pci_vectors);
	vectors = (struct pci_vector_struct *) ia64_boot_param.pci_vectors;
	for (i = 0; i < ia64_boot_param.num_pci_vectors; i++) {
		irq = vectors[i].irq;
		if (irq < 16)
			irq = isa_irq_to_vector(irq);
		if (iosapic_baseirq(irq) != irqbase)
			continue;

		iosapic_bustype(irq) = BUS_PCI;
		iosapic_pin(irq) = irq - iosapic_baseirq(irq);
		iosapic_bus(irq) = vectors[i].bus;
		/*
		 * Map the PCI slot and pin data into iosapic_busdata()
		 */
		iosapic_busdata(irq) = (vectors[i].pci_id & 0xffff0000) | vectors[i].pin;

		/* Default settings for PCI */
		iosapic_dmode(irq) = IO_SAPIC_LOWEST_PRIORITY;
		iosapic_trigger(irq)  = IO_SAPIC_LEVEL;
		iosapic_polarity(irq) = IO_SAPIC_POL_LOW;

#ifdef DEBUG_IRQ_ROUTING
		printk("PCI: BUS %d Slot %x Pin %x IRQ %02x --> Vector %02x IOSAPIC Pin %d\n", 
		       vectors[i].bus, vectors[i].pci_id>>16, vectors[i].pin, vectors[i].irq, 
		       irq, iosapic_pin(irq));
#endif
	}
#endif /* CONFIG_IA64_SOFTSDV_HACKS */

	for (i = 0; i < NR_IRQS; ++i) {
		if (iosapic_baseirq(i) != irqbase)
			continue;

		if (iosapic_pin(i) != -1) {
			if (iosapic_trigger(i) == IO_SAPIC_LEVEL)
			  irq_type = &irq_type_iosapic_level;
			else
			  irq_type = &irq_type_iosapic_edge;
			if (irq_desc[i].handler != &no_irq_type)
				printk("dig_irq_init: warning: changing vector %d from %s to %s\n",
				       i, irq_desc[i].handler->typename,
				       irq_type->typename);
			irq_desc[i].handler = irq_type;

			/* program the IOSAPIC routing table: */
			set_rte(iosapic_addr(i), iosapic_pin(i), iosapic_polarity(i),
				iosapic_trigger(i), iosapic_dmode(i),
				(ia64_get_lid() >> 16) & 0xffff, i);
		}
	}
}

void
dig_irq_init (void)
{
	/*
	 * Disable the compatibility mode interrupts (8259 style), needs IN/OUT support
	 * enabled.
	 */
	outb(0xff, 0xA1);
	outb(0xff, 0x21);
}

void
dig_pci_fixup (void)
{
	struct	pci_dev	*dev;
	int		irq;
	unsigned char 	pin;

	pci_for_each_dev(dev) {
		pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
		if (pin) {
			pin--;          /* interrupt pins are numbered starting from 1 */
			irq = iosapic_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn),
							 pin);
			if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
				struct pci_dev * bridge = dev->bus->self;

				/* allow for multiple bridges on an adapter */
				do {
					/* do the bridge swizzle... */
					pin = (pin + PCI_SLOT(dev->devfn)) % 4;
					irq = iosapic_get_PCI_irq_vector(bridge->bus->number,
									 PCI_SLOT(bridge->devfn), pin);
				} while (irq < 0 && (bridge = bridge->bus->self));
				if (irq >= 0)
					printk(KERN_WARNING
					       "PCI: using PPB(B%d,I%d,P%d) to get irq %02x\n",
					       bridge->bus->number, PCI_SLOT(bridge->devfn),
					       pin, irq);
				else
					printk(KERN_WARNING
					       "PCI: Couldn't map irq for B%d,I%d,P%d\n",
					       bridge->bus->number, PCI_SLOT(bridge->devfn),
					       pin);
			}
			if (irq >= 0) {
				printk("PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %02x\n",
				       dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
				dev->irq = irq;
			}
		}
		/*
		 * Nothing to fixup
		 * Fix out-of-range IRQ numbers
		 */
		if (dev->irq >= NR_IRQS)
			dev->irq = 15;	/* Spurious interrupts */
	}
}

/*
 * Register an IOSAPIC discovered via ACPI.
 */
void __init
dig_register_iosapic (acpi_entry_iosapic_t *iosapic)
{
	unsigned int ver, v;
	int l, max_pin;

	ver = iosapic_version(iosapic->address);
	max_pin = (ver >> 16) & 0xff;
	
	printk("IOSAPIC Version %x.%x: address 0x%lx IRQs 0x%x - 0x%x\n", 
	       (ver & 0xf0) >> 4, (ver & 0x0f), iosapic->address, 
	       iosapic->irq_base, iosapic->irq_base + max_pin);
	
	for (l = 0; l <= max_pin; l++) {
		v = iosapic->irq_base + l;
		if (v < 16)
			v = isa_irq_to_vector(v);
		if (v > IA64_MAX_VECTORED_IRQ) {
			printk("    !!! bad IOSAPIC interrupt vector: %u\n", v);
			continue;
		}
		/* XXX Check for IOSAPIC collisions */
		iosapic_addr(v) = (unsigned long) ioremap(iosapic->address, 0);
		iosapic_baseirq(v) = iosapic->irq_base;
	}
	iosapic_init(iosapic->address, iosapic->irq_base);
}