ip27-irq.c 10.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 * ip27-irq.c: Highlevel interrupt handling for IP27 architecture.
 *
 * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
 * Copyright (C) 1999 - 2001 Kanoj Sarcar
 */
8 9 10

#undef DEBUG

L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/timex.h>
20
#include <linux/smp.h>
L
Linus Torvalds 已提交
21
#include <linux/random.h>
22
#include <linux/kernel.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <linux/kernel_stat.h>
#include <linux/delay.h>
#include <linux/bitops.h>

#include <asm/bootinfo.h>
#include <asm/io.h>
#include <asm/mipsregs.h>

#include <asm/processor.h>
#include <asm/pci/bridge.h>
#include <asm/sn/addrs.h>
#include <asm/sn/agent.h>
#include <asm/sn/arch.h>
#include <asm/sn/hub.h>
#include <asm/sn/intr.h>

/*
 * Linux has a controller-independent x86 interrupt architecture.
 * every controller has a 'controller-template', that is used
 * by the main code to do the right thing. Each driver-visible
L
Lucas De Marchi 已提交
43
 * interrupt source is transparently wired to the appropriate
L
Linus Torvalds 已提交
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
 * controller. Thus drivers need not be aware of the
 * interrupt-controller.
 *
 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
 * (IO-APICs assumed to be messaging to Pentium local-APICs)
 *
 * the code is designed to be easily extended with new/different
 * interrupt controllers, without having to do assembly magic.
 */

extern asmlinkage void ip27_irq(void);

extern struct bridge_controller *irq_to_bridge[];
extern int irq_to_slot[];

/*
 * use these macros to get the encoded nasid and widget id
 * from the irq value
 */
#define IRQ_TO_BRIDGE(i)		irq_to_bridge[(i)]
#define	SLOT_FROM_PCI_IRQ(i)		irq_to_slot[i]

static inline int alloc_level(int cpu, int irq)
{
69
	struct hub_data *hub = hub_data(cpu_to_node(cpu));
L
Linus Torvalds 已提交
70
	struct slice_data *si = cpu_data[cpu].data;
71
	int level;
L
Linus Torvalds 已提交
72

73
	level = find_first_zero_bit(hub->irq_alloc_mask, LEVELS_PER_SLICE);
L
Linus Torvalds 已提交
74
	if (level >= LEVELS_PER_SLICE)
75
		panic("Cpu %d flooded with devices", cpu);
L
Linus Torvalds 已提交
76

77
	__set_bit(level, hub->irq_alloc_mask);
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86
	si->level_to_irq[level] = irq;

	return level;
}

static inline int find_level(cpuid_t *cpunum, int irq)
{
	int cpu, i;

87
	for_each_online_cpu(cpu) {
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97
		struct slice_data *si = cpu_data[cpu].data;

		for (i = BASE_PCI_IRQ; i < LEVELS_PER_SLICE; i++)
			if (si->level_to_irq[i] == irq) {
				*cpunum = cpu;

				return i;
			}
	}

98
	panic("Could not identify cpu/level for irq %d", irq);
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

/*
 * Find first bit set
 */
static int ms1bit(unsigned long x)
{
	int b = 0, s;

	s = 16; if (x >> 16 == 0) s = 0; b += s; x >>= s;
	s =  8; if (x >>  8 == 0) s = 0; b += s; x >>= s;
	s =  4; if (x >>  4 == 0) s = 0; b += s; x >>= s;
	s =  2; if (x >>  2 == 0) s = 0; b += s; x >>= s;
	s =  1; if (x >>  1 == 0) s = 0; b += s;

	return b;
}

/*
Y
Yong Zhang 已提交
118
 * This code is unnecessarily complex, because we do
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128
 * intr enabling. Basically, once we grab the set of intrs we need
 * to service, we must mask _all_ these interrupts; firstly, to make
 * sure the same intr does not intr again, causing recursion that
 * can lead to stack overflow. Secondly, we can not just mask the
 * one intr we are do_IRQing, because the non-masked intrs in the
 * first set might intr again, causing multiple servicings of the
 * same intr. This effect is mostly seen for intercpu intrs.
 * Kanoj 05.13.00
 */

129
static void ip27_do_irq_mask0(void)
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
{
	int irq, swlevel;
	hubreg_t pend0, mask0;
	cpuid_t cpu = smp_processor_id();
	int pi_int_mask0 =
		(cputoslice(cpu) == 0) ?  PI_INT_MASK0_A : PI_INT_MASK0_B;

	/* copied from Irix intpend0() */
	pend0 = LOCAL_HUB_L(PI_INT_PEND0);
	mask0 = LOCAL_HUB_L(pi_int_mask0);

	pend0 &= mask0;		/* Pick intrs we should look at */
	if (!pend0)
		return;

	swlevel = ms1bit(pend0);
#ifdef CONFIG_SMP
	if (pend0 & (1UL << CPU_RESCHED_A_IRQ)) {
		LOCAL_HUB_CLR_INTR(CPU_RESCHED_A_IRQ);
149
		scheduler_ipi();
L
Linus Torvalds 已提交
150 151
	} else if (pend0 & (1UL << CPU_RESCHED_B_IRQ)) {
		LOCAL_HUB_CLR_INTR(CPU_RESCHED_B_IRQ);
152
		scheduler_ipi();
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
	} else if (pend0 & (1UL << CPU_CALL_A_IRQ)) {
		LOCAL_HUB_CLR_INTR(CPU_CALL_A_IRQ);
		smp_call_function_interrupt();
	} else if (pend0 & (1UL << CPU_CALL_B_IRQ)) {
		LOCAL_HUB_CLR_INTR(CPU_CALL_B_IRQ);
		smp_call_function_interrupt();
	} else
#endif
	{
		/* "map" swlevel to irq */
		struct slice_data *si = cpu_data[cpu].data;

		irq = si->level_to_irq[swlevel];
166
		do_IRQ(irq);
L
Linus Torvalds 已提交
167 168 169 170 171
	}

	LOCAL_HUB_L(PI_INT_PEND0);
}

172
static void ip27_do_irq_mask1(void)
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{
	int irq, swlevel;
	hubreg_t pend1, mask1;
	cpuid_t cpu = smp_processor_id();
	int pi_int_mask1 = (cputoslice(cpu) == 0) ?  PI_INT_MASK1_A : PI_INT_MASK1_B;
	struct slice_data *si = cpu_data[cpu].data;

	/* copied from Irix intpend0() */
	pend1 = LOCAL_HUB_L(PI_INT_PEND1);
	mask1 = LOCAL_HUB_L(pi_int_mask1);

	pend1 &= mask1;		/* Pick intrs we should look at */
	if (!pend1)
		return;

	swlevel = ms1bit(pend1);
	/* "map" swlevel to irq */
	irq = si->level_to_irq[swlevel];
	LOCAL_HUB_CLR_INTR(swlevel);
192
	do_IRQ(irq);
L
Linus Torvalds 已提交
193 194 195 196

	LOCAL_HUB_L(PI_INT_PEND1);
}

197
static void ip27_prof_timer(void)
L
Linus Torvalds 已提交
198 199 200 201
{
	panic("CPU %d got a profiling interrupt", smp_processor_id());
}

202
static void ip27_hub_error(void)
L
Linus Torvalds 已提交
203 204 205 206 207 208 209 210 211
{
	panic("CPU %d got a hub error interrupt", smp_processor_id());
}

static int intr_connect_level(int cpu, int bit)
{
	nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
	struct slice_data *si = cpu_data[cpu].data;

212
	set_bit(bit, si->irq_enable_mask);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	if (!cputoslice(cpu)) {
		REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
		REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
	} else {
		REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
		REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
	}

	return 0;
}

static int intr_disconnect_level(int cpu, int bit)
{
	nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
	struct slice_data *si = cpu_data[cpu].data;

230
	clear_bit(bit, si->irq_enable_mask);
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243

	if (!cputoslice(cpu)) {
		REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
		REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
	} else {
		REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
		REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
	}

	return 0;
}

/* Startup one of the (PCI ...) IRQs routes over a bridge.  */
244
static unsigned int startup_bridge_irq(struct irq_data *d)
L
Linus Torvalds 已提交
245 246 247 248 249 250 251
{
	struct bridge_controller *bc;
	bridgereg_t device;
	bridge_t *bridge;
	int pin, swlevel;
	cpuid_t cpu;

252 253
	pin = SLOT_FROM_PCI_IRQ(d->irq);
	bc = IRQ_TO_BRIDGE(d->irq);
L
Linus Torvalds 已提交
254 255
	bridge = bc->base;

256
	pr_debug("bridge_startup(): irq= 0x%x  pin=%d\n", d->irq, pin);
L
Linus Torvalds 已提交
257 258 259 260
	/*
	 * "map" irq to a swlevel greater than 6 since the first 6 bits
	 * of INT_PEND0 are taken
	 */
261
	swlevel = find_level(&cpu, d->irq);
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	bridge->b_int_addr[pin].addr = (0x20000 | swlevel | (bc->nasid << 8));
	bridge->b_int_enable |= (1 << pin);
	bridge->b_int_enable |= 0x7ffffe00;	/* more stuff in int_enable */

	/*
	 * Enable sending of an interrupt clear packt to the hub on a high to
	 * low transition of the interrupt pin.
	 *
	 * IRIX sets additional bits in the address which are documented as
	 * reserved in the bridge docs.
	 */
	bridge->b_int_mode |= (1UL << pin);

	/*
	 * We assume the bridge to have a 1:1 mapping between devices
	 * (slots) and intr pins.
	 */
	device = bridge->b_int_device;
	device &= ~(7 << (pin*3));
	device |= (pin << (pin*3));
	bridge->b_int_device = device;

        bridge->b_wid_tflush;

286 287
	intr_connect_level(cpu, swlevel);

L
Linus Torvalds 已提交
288 289 290 291
        return 0;       /* Never anything pending.  */
}

/* Shutdown one of the (PCI ...) IRQs routes over a bridge.  */
292
static void shutdown_bridge_irq(struct irq_data *d)
L
Linus Torvalds 已提交
293
{
294
	struct bridge_controller *bc = IRQ_TO_BRIDGE(d->irq);
L
Linus Torvalds 已提交
295 296 297 298
	bridge_t *bridge = bc->base;
	int pin, swlevel;
	cpuid_t cpu;

299 300
	pr_debug("bridge_shutdown: irq 0x%x\n", d->irq);
	pin = SLOT_FROM_PCI_IRQ(d->irq);
L
Linus Torvalds 已提交
301 302 303 304 305

	/*
	 * map irq to a swlevel greater than 6 since the first 6 bits
	 * of INT_PEND0 are taken
	 */
306
	swlevel = find_level(&cpu, d->irq);
L
Linus Torvalds 已提交
307 308 309 310 311 312
	intr_disconnect_level(cpu, swlevel);

	bridge->b_int_enable &= ~(1 << pin);
	bridge->b_wid_tflush;
}

313
static inline void enable_bridge_irq(struct irq_data *d)
L
Linus Torvalds 已提交
314 315 316 317
{
	cpuid_t cpu;
	int swlevel;

318
	swlevel = find_level(&cpu, d->irq);	/* Criminal offence */
L
Linus Torvalds 已提交
319 320 321
	intr_connect_level(cpu, swlevel);
}

322
static inline void disable_bridge_irq(struct irq_data *d)
L
Linus Torvalds 已提交
323 324 325 326
{
	cpuid_t cpu;
	int swlevel;

327
	swlevel = find_level(&cpu, d->irq);	/* Criminal offence */
L
Linus Torvalds 已提交
328 329 330
	intr_disconnect_level(cpu, swlevel);
}

331
static struct irq_chip bridge_irq_type = {
332
	.name		= "bridge",
333 334 335 336
	.irq_startup	= startup_bridge_irq,
	.irq_shutdown	= shutdown_bridge_irq,
	.irq_mask	= disable_bridge_irq,
	.irq_unmask	= enable_bridge_irq,
L
Linus Torvalds 已提交
337 338
};

339
void register_bridge_irq(unsigned int irq)
L
Linus Torvalds 已提交
340
{
341
	irq_set_chip_and_handler(irq, &bridge_irq_type, handle_level_irq);
L
Linus Torvalds 已提交
342 343
}

344
int request_bridge_irq(struct bridge_controller *bc)
L
Linus Torvalds 已提交
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
{
	int irq = allocate_irqno();
	int swlevel, cpu;
	nasid_t nasid;

	if (irq < 0)
		return irq;

	/*
	 * "map" irq to a swlevel greater than 6 since the first 6 bits
	 * of INT_PEND0 are taken
	 */
	cpu = bc->irq_cpu;
	swlevel = alloc_level(cpu, irq);
	if (unlikely(swlevel < 0)) {
		free_irqno(irq);

		return -EAGAIN;
	}

	/* Make sure it's not already pending when we connect it. */
	nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
	REMOTE_HUB_CLR_INTR(nasid, swlevel);

	intr_connect_level(cpu, swlevel);

	register_bridge_irq(irq);

	return irq;
}

376
asmlinkage void plat_irq_dispatch(void)
377 378
{
	unsigned long pending = read_c0_cause() & read_c0_status();
379
	extern unsigned int rt_timer_irq;
380 381

	if (pending & CAUSEF_IP4)
382
		do_IRQ(rt_timer_irq);
383
	else if (pending & CAUSEF_IP2)	/* PI_INT_PEND_0 or CC_PEND_{A|B} */
384
		ip27_do_irq_mask0();
385
	else if (pending & CAUSEF_IP3)	/* PI_INT_PEND_1 */
386
		ip27_do_irq_mask1();
387
	else if (pending & CAUSEF_IP5)
388
		ip27_prof_timer();
389
	else if (pending & CAUSEF_IP6)
390
		ip27_hub_error();
391 392
}

L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401
void __init arch_init_irq(void)
{
}

void install_ipi(void)
{
	int slice = LOCAL_HUB_L(PI_CPU_NUM);
	int cpu = smp_processor_id();
	struct slice_data *si = cpu_data[cpu].data;
402 403 404 405 406 407 408 409 410 411 412 413
	struct hub_data *hub = hub_data(cpu_to_node(cpu));
	int resched, call;

	resched = CPU_RESCHED_A_IRQ + slice;
	__set_bit(resched, hub->irq_alloc_mask);
	__set_bit(resched, si->irq_enable_mask);
	LOCAL_HUB_CLR_INTR(resched);

	call = CPU_CALL_A_IRQ + slice;
	__set_bit(call, hub->irq_alloc_mask);
	__set_bit(call, si->irq_enable_mask);
	LOCAL_HUB_CLR_INTR(call);
L
Linus Torvalds 已提交
414 415

	if (slice == 0) {
416 417
		LOCAL_HUB_S(PI_INT_MASK0_A, si->irq_enable_mask[0]);
		LOCAL_HUB_S(PI_INT_MASK1_A, si->irq_enable_mask[1]);
L
Linus Torvalds 已提交
418
	} else {
419 420
		LOCAL_HUB_S(PI_INT_MASK0_B, si->irq_enable_mask[0]);
		LOCAL_HUB_S(PI_INT_MASK1_B, si->irq_enable_mask[1]);
L
Linus Torvalds 已提交
421 422
	}
}