1 /*
2  * ip22-int.c: Routines for generic manipulation of the INT[23] ASIC
3  *             found on INDY and Indigo2 workstations.
4  *
5  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
6  * Copyright (C) 1997, 1998 Ralf Baechle (ralf@gnu.org)
7  * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu)
8  *                    - Indigo2 changes
9  *                    - Interrupt handling fixes
10  * Copyright (C) 2001, 2003 Ladislav Michl (ladis@linux-mips.org)
11  */
12 
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/signal.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 
21 #include <asm/mipsregs.h>
22 #include <asm/addrspace.h>
23 
24 #include <asm/sgi/ioc.h>
25 #include <asm/sgi/hpc3.h>
26 #include <asm/sgi/ip22.h>
27 
28 /* #define DEBUG_SGINT */
29 
30 /* So far nothing hangs here */
31 #undef USE_LIO3_IRQ
32 
33 struct sgint_regs *sgint;
34 
35 static char lc0msk_to_irqnr[256];
36 static char lc1msk_to_irqnr[256];
37 static char lc2msk_to_irqnr[256];
38 static char lc3msk_to_irqnr[256];
39 
40 extern asmlinkage void indyIRQ(void);
41 extern int ip22_eisa_init(void);
42 
enable_local0_irq(unsigned int irq)43 static void enable_local0_irq(unsigned int irq)
44 {
45 	unsigned long flags;
46 
47 	save_and_cli(flags);
48 	/* don't allow mappable interrupt to be enabled from setup_irq,
49 	 * we have our own way to do so */
50 	if (irq != SGI_MAP_0_IRQ)
51 		sgint->imask0 |= (1 << (irq - SGINT_LOCAL0));
52 	restore_flags(flags);
53 }
54 
startup_local0_irq(unsigned int irq)55 static unsigned int startup_local0_irq(unsigned int irq)
56 {
57 	enable_local0_irq(irq);
58 	return 0;		/* Never anything pending  */
59 }
60 
disable_local0_irq(unsigned int irq)61 static void disable_local0_irq(unsigned int irq)
62 {
63 	unsigned long flags;
64 
65 	save_and_cli(flags);
66 	sgint->imask0 &= ~(1 << (irq - SGINT_LOCAL0));
67 	restore_flags(flags);
68 }
69 
70 #define shutdown_local0_irq	disable_local0_irq
71 #define mask_and_ack_local0_irq	disable_local0_irq
72 
end_local0_irq(unsigned int irq)73 static void end_local0_irq (unsigned int irq)
74 {
75 	if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
76 		enable_local0_irq(irq);
77 }
78 
79 static struct hw_interrupt_type ip22_local0_irq_type = {
80 	.typename	= "IP22 local 0",
81 	.startup	= startup_local0_irq,
82 	.shutdown	= shutdown_local0_irq,
83 	.enable		= enable_local0_irq,
84 	.disable	= disable_local0_irq,
85 	.ack		= mask_and_ack_local0_irq,
86 	.end		= end_local0_irq,
87 };
88 
enable_local1_irq(unsigned int irq)89 static void enable_local1_irq(unsigned int irq)
90 {
91 	unsigned long flags;
92 
93 	save_and_cli(flags);
94 	/* don't allow mappable interrupt to be enabled from setup_irq,
95 	 * we have our own way to do so */
96 	if (irq != SGI_MAP_1_IRQ)
97 		sgint->imask1 |= (1 << (irq - SGINT_LOCAL1));
98 	restore_flags(flags);
99 }
100 
startup_local1_irq(unsigned int irq)101 static unsigned int startup_local1_irq(unsigned int irq)
102 {
103 	enable_local1_irq(irq);
104 	return 0;		/* Never anything pending  */
105 }
106 
disable_local1_irq(unsigned int irq)107 void disable_local1_irq(unsigned int irq)
108 {
109 	unsigned long flags;
110 
111 	save_and_cli(flags);
112 	sgint->imask1 &= ~(1 << (irq - SGINT_LOCAL1));
113 	restore_flags(flags);
114 }
115 
116 #define shutdown_local1_irq	disable_local1_irq
117 #define mask_and_ack_local1_irq	disable_local1_irq
118 
end_local1_irq(unsigned int irq)119 static void end_local1_irq (unsigned int irq)
120 {
121 	if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
122 		enable_local1_irq(irq);
123 }
124 
125 static struct hw_interrupt_type ip22_local1_irq_type = {
126 	.typename	= "IP22 local 1",
127 	.startup	= startup_local1_irq,
128 	.shutdown	= shutdown_local1_irq,
129 	.enable		= enable_local1_irq,
130 	.disable	= disable_local1_irq,
131 	.ack		= mask_and_ack_local1_irq,
132 	.end		= end_local1_irq,
133 };
134 
enable_local2_irq(unsigned int irq)135 static void enable_local2_irq(unsigned int irq)
136 {
137 	unsigned long flags;
138 
139 	save_and_cli(flags);
140 	sgint->imask0 |= (1 << (SGI_MAP_0_IRQ - SGINT_LOCAL0));
141 	sgint->cmeimask0 |= (1 << (irq - SGINT_LOCAL2));
142 	restore_flags(flags);
143 }
144 
startup_local2_irq(unsigned int irq)145 static unsigned int startup_local2_irq(unsigned int irq)
146 {
147 	enable_local2_irq(irq);
148 	return 0;		/* Never anything pending  */
149 }
150 
disable_local2_irq(unsigned int irq)151 void disable_local2_irq(unsigned int irq)
152 {
153 	unsigned long flags;
154 
155 	save_and_cli(flags);
156 	sgint->cmeimask0 &= ~(1 << (irq - SGINT_LOCAL2));
157 	if (!sgint->cmeimask0)
158 		sgint->imask0 &= ~(1 << (SGI_MAP_0_IRQ - SGINT_LOCAL0));
159 	restore_flags(flags);
160 }
161 
162 #define shutdown_local2_irq disable_local2_irq
163 #define mask_and_ack_local2_irq	disable_local2_irq
164 
end_local2_irq(unsigned int irq)165 static void end_local2_irq (unsigned int irq)
166 {
167 	if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
168 		enable_local2_irq(irq);
169 }
170 
171 static struct hw_interrupt_type ip22_local2_irq_type = {
172 	.typename	= "IP22 local 2",
173 	.startup	= startup_local2_irq,
174 	.shutdown	= shutdown_local2_irq,
175 	.enable		= enable_local2_irq,
176 	.disable	= disable_local2_irq,
177 	.ack		= mask_and_ack_local2_irq,
178 	.end		= end_local2_irq,
179 };
180 
enable_local3_irq(unsigned int irq)181 static void enable_local3_irq(unsigned int irq)
182 {
183 	unsigned long flags;
184 
185 	save_and_cli(flags);
186 	sgint->imask1 |= (1 << (SGI_MAP_1_IRQ - SGINT_LOCAL1));
187 	sgint->cmeimask1 |= (1 << (irq - SGINT_LOCAL3));
188 	restore_flags(flags);
189 }
190 
startup_local3_irq(unsigned int irq)191 static unsigned int startup_local3_irq(unsigned int irq)
192 {
193 	enable_local3_irq(irq);
194 	return 0;		/* Never anything pending  */
195 }
196 
disable_local3_irq(unsigned int irq)197 void disable_local3_irq(unsigned int irq)
198 {
199 	unsigned long flags;
200 
201 	save_and_cli(flags);
202 	sgint->cmeimask1 &= ~(1 << (irq - SGINT_LOCAL3));
203 	if (!sgint->cmeimask1)
204 		sgint->imask1 &= ~(1 << (SGI_MAP_1_IRQ - SGINT_LOCAL1));
205 	restore_flags(flags);
206 }
207 
208 #define shutdown_local3_irq disable_local3_irq
209 #define mask_and_ack_local3_irq	disable_local3_irq
210 
end_local3_irq(unsigned int irq)211 static void end_local3_irq (unsigned int irq)
212 {
213 	if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
214 		enable_local3_irq(irq);
215 }
216 
217 static struct hw_interrupt_type ip22_local3_irq_type = {
218 	.typename	= "IP22 local 3",
219 	.startup	= startup_local3_irq,
220 	.shutdown	= shutdown_local3_irq,
221 	.enable		= enable_local3_irq,
222 	.disable	= disable_local3_irq,
223 	.ack		= mask_and_ack_local3_irq,
224 	.end		= end_local3_irq,
225 };
226 
indy_local0_irqdispatch(struct pt_regs * regs)227 void indy_local0_irqdispatch(struct pt_regs *regs)
228 {
229 	u8 mask = sgint->istat0 & sgint->imask0;
230 	u8 mask2;
231 	int irq;
232 
233 	if (mask & SGINT_ISTAT0_LIO2) {
234 		mask2 = sgint->vmeistat & sgint->cmeimask0;
235 		irq = lc2msk_to_irqnr[mask2];
236 	} else
237 		irq = lc0msk_to_irqnr[mask];
238 
239 	/* if irq == 0, then the interrupt has already been cleared */
240 	if (irq)
241 		do_IRQ(irq, regs);
242 	return;
243 }
244 
indy_local1_irqdispatch(struct pt_regs * regs)245 void indy_local1_irqdispatch(struct pt_regs *regs)
246 {
247 	u8 mask = sgint->istat1 & sgint->imask1;
248 	u8 mask2;
249 	int irq;
250 
251 	if (mask & SGINT_ISTAT1_LIO3) {
252 		mask2 = sgint->vmeistat & sgint->cmeimask1;
253 		irq = lc3msk_to_irqnr[mask2];
254 	} else
255 		irq = lc1msk_to_irqnr[mask];
256 
257 	/* if irq == 0, then the interrupt has already been cleared */
258 	if (irq)
259 		do_IRQ(irq, regs);
260 	return;
261 }
262 
263 extern void ip22_be_interrupt(int irq, struct pt_regs *regs);
264 
indy_buserror_irq(struct pt_regs * regs)265 void indy_buserror_irq(struct pt_regs *regs)
266 {
267 	int cpu = smp_processor_id();
268 	int irq = SGI_BUSERR_IRQ;
269 
270 	irq_enter(cpu, irq);
271 	kstat.irqs[cpu][irq]++;
272 	ip22_be_interrupt(irq, regs);
273 	irq_exit(cpu, irq);
274 }
275 
276 static struct irqaction local0_cascade = {
277 	.handler	= no_action,
278 	.flags		= SA_INTERRUPT,
279 	.name		= "local0 cascade",
280 };
281 
282 static struct irqaction local1_cascade = {
283 	.handler	= no_action,
284 	.flags		= SA_INTERRUPT,
285 	.name		= "local1 cascade",
286 };
287 
288 static struct irqaction buserr = {
289 	.handler	= no_action,
290 	.flags		= SA_INTERRUPT,
291 	.name		= "Bus Error",
292 };
293 
294 static struct irqaction map0_cascade = {
295 	.handler	= no_action,
296 	.flags		= SA_INTERRUPT,
297 	.name		= "mapable0 cascade",
298 };
299 
300 #ifdef USE_LIO3_IRQ
301 static struct irqaction map1_cascade = {
302 	.handler	= no_action,
303 	.flags		= SA_INTERRUPT,
304 	.name		= "mapable1 cascade",
305 };
306 #define SGI_INTERRUPTS	SGINT_END
307 #else
308 #define SGI_INTERRUPTS	SGINT_LOCAL3
309 #endif
310 
311 extern void mips_cpu_irq_init(unsigned int irq_base);
312 
init_IRQ(void)313 void __init init_IRQ(void)
314 {
315 	int i;
316 
317 	/* Init local mask --> irq tables. */
318 	for (i = 0; i < 256; i++) {
319 		if (i & 0x80) {
320 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 7;
321 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 7;
322 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 7;
323 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 7;
324 		} else if (i & 0x40) {
325 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 6;
326 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 6;
327 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 6;
328 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 6;
329 		} else if (i & 0x20) {
330 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 5;
331 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 5;
332 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 5;
333 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 5;
334 		} else if (i & 0x10) {
335 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 4;
336 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 4;
337 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 4;
338 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 4;
339 		} else if (i & 0x08) {
340 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 3;
341 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 3;
342 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 3;
343 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 3;
344 		} else if (i & 0x04) {
345 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 2;
346 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 2;
347 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 2;
348 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 2;
349 		} else if (i & 0x02) {
350 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 1;
351 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 1;
352 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 1;
353 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 1;
354 		} else if (i & 0x01) {
355 			lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 0;
356 			lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 0;
357 			lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 0;
358 			lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 0;
359 		} else {
360 			lc0msk_to_irqnr[i] = 0;
361 			lc1msk_to_irqnr[i] = 0;
362 			lc2msk_to_irqnr[i] = 0;
363 			lc3msk_to_irqnr[i] = 0;
364 		}
365 	}
366 
367 	/* Mask out all interrupts. */
368 	sgint->imask0 = 0;
369 	sgint->imask1 = 0;
370 	sgint->cmeimask0 = 0;
371 	sgint->cmeimask1 = 0;
372 
373 	set_except_vector(0, indyIRQ);
374 
375 	init_generic_irq();
376 	/* init CPU irqs */
377 	mips_cpu_irq_init(SGINT_CPU);
378 
379 	for (i = SGINT_LOCAL0; i < SGI_INTERRUPTS; i++) {
380 		hw_irq_controller *handler;
381 
382 		if (i < SGINT_LOCAL1)
383 			handler		= &ip22_local0_irq_type;
384 		else if (i < SGINT_LOCAL2)
385 			handler		= &ip22_local1_irq_type;
386 		else if (i < SGINT_LOCAL3)
387 			handler		= &ip22_local2_irq_type;
388 		else
389 			handler		= &ip22_local3_irq_type;
390 
391 		irq_desc[i].status	= IRQ_DISABLED;
392 		irq_desc[i].action	= 0;
393 		irq_desc[i].depth	= 1;
394 		irq_desc[i].handler	= handler;
395 	}
396 
397 	/* vector handler. this register the IRQ as non-sharable */
398 	setup_irq(SGI_LOCAL_0_IRQ, &local0_cascade);
399 	setup_irq(SGI_LOCAL_1_IRQ, &local1_cascade);
400 	setup_irq(SGI_BUSERR_IRQ, &buserr);
401 
402 	/* cascade in cascade. i love Indy ;-) */
403 	setup_irq(SGI_MAP_0_IRQ, &map0_cascade);
404 #ifdef USE_LIO3_IRQ
405 	setup_irq(SGI_MAP_1_IRQ, &map1_cascade);
406 #endif
407 
408 #ifdef CONFIG_EISA
409 	if (ip22_is_fullhouse())	/* Only Indigo-2 have EISA stuff */
410 	        ip22_eisa_init ();
411 #endif
412 }
413