1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Interrupt request handling routines. On the
4*4882a593Smuzhiyun * Sparc the IRQs are basically 'cast in stone'
5*4882a593Smuzhiyun * and you are supposed to probe the prom's device
6*4882a593Smuzhiyun * node trees to find out who's got which IRQ.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
9*4882a593Smuzhiyun * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
10*4882a593Smuzhiyun * Copyright (C) 1995,2002 Pete A. Zaitcev (zaitcev@yahoo.com)
11*4882a593Smuzhiyun * Copyright (C) 1996 Dave Redman (djhr@tadpole.co.uk)
12*4882a593Smuzhiyun * Copyright (C) 1998-2000 Anton Blanchard (anton@samba.org)
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/kernel_stat.h>
16*4882a593Smuzhiyun #include <linux/seq_file.h>
17*4882a593Smuzhiyun #include <linux/export.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <asm/cacheflush.h>
20*4882a593Smuzhiyun #include <asm/cpudata.h>
21*4882a593Smuzhiyun #include <asm/setup.h>
22*4882a593Smuzhiyun #include <asm/pcic.h>
23*4882a593Smuzhiyun #include <asm/leon.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include "kernel.h"
26*4882a593Smuzhiyun #include "irq.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* platform specific irq setup */
29*4882a593Smuzhiyun struct sparc_config sparc_config;
30*4882a593Smuzhiyun
arch_local_irq_save(void)31*4882a593Smuzhiyun unsigned long arch_local_irq_save(void)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun unsigned long retval;
34*4882a593Smuzhiyun unsigned long tmp;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun __asm__ __volatile__(
37*4882a593Smuzhiyun "rd %%psr, %0\n\t"
38*4882a593Smuzhiyun "or %0, %2, %1\n\t"
39*4882a593Smuzhiyun "wr %1, 0, %%psr\n\t"
40*4882a593Smuzhiyun "nop; nop; nop\n"
41*4882a593Smuzhiyun : "=&r" (retval), "=r" (tmp)
42*4882a593Smuzhiyun : "i" (PSR_PIL)
43*4882a593Smuzhiyun : "memory");
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun return retval;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun EXPORT_SYMBOL(arch_local_irq_save);
48*4882a593Smuzhiyun
arch_local_irq_enable(void)49*4882a593Smuzhiyun void arch_local_irq_enable(void)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun unsigned long tmp;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun __asm__ __volatile__(
54*4882a593Smuzhiyun "rd %%psr, %0\n\t"
55*4882a593Smuzhiyun "andn %0, %1, %0\n\t"
56*4882a593Smuzhiyun "wr %0, 0, %%psr\n\t"
57*4882a593Smuzhiyun "nop; nop; nop\n"
58*4882a593Smuzhiyun : "=&r" (tmp)
59*4882a593Smuzhiyun : "i" (PSR_PIL)
60*4882a593Smuzhiyun : "memory");
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun EXPORT_SYMBOL(arch_local_irq_enable);
63*4882a593Smuzhiyun
arch_local_irq_restore(unsigned long old_psr)64*4882a593Smuzhiyun void arch_local_irq_restore(unsigned long old_psr)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun unsigned long tmp;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun __asm__ __volatile__(
69*4882a593Smuzhiyun "rd %%psr, %0\n\t"
70*4882a593Smuzhiyun "and %2, %1, %2\n\t"
71*4882a593Smuzhiyun "andn %0, %1, %0\n\t"
72*4882a593Smuzhiyun "wr %0, %2, %%psr\n\t"
73*4882a593Smuzhiyun "nop; nop; nop\n"
74*4882a593Smuzhiyun : "=&r" (tmp)
75*4882a593Smuzhiyun : "i" (PSR_PIL), "r" (old_psr)
76*4882a593Smuzhiyun : "memory");
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun EXPORT_SYMBOL(arch_local_irq_restore);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Dave Redman (djhr@tadpole.co.uk)
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * IRQ numbers.. These are no longer restricted to 15..
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * this is done to enable SBUS cards and onboard IO to be masked
86*4882a593Smuzhiyun * correctly. using the interrupt level isn't good enough.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * For example:
89*4882a593Smuzhiyun * A device interrupting at sbus level6 and the Floppy both come in
90*4882a593Smuzhiyun * at IRQ11, but enabling and disabling them requires writing to
91*4882a593Smuzhiyun * different bits in the SLAVIO/SEC.
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * As a result of these changes sun4m machines could now support
94*4882a593Smuzhiyun * directed CPU interrupts using the existing enable/disable irq code
95*4882a593Smuzhiyun * with tweaks.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * Sun4d complicates things even further. IRQ numbers are arbitrary
98*4882a593Smuzhiyun * 32-bit values in that case. Since this is similar to sparc64,
99*4882a593Smuzhiyun * we adopt a virtual IRQ numbering scheme as is done there.
100*4882a593Smuzhiyun * Virutal interrupt numbers are allocated by build_irq(). So NR_IRQS
101*4882a593Smuzhiyun * just becomes a limit of how many interrupt sources we can handle in
102*4882a593Smuzhiyun * a single system. Even fully loaded SS2000 machines top off at
103*4882a593Smuzhiyun * about 32 interrupt sources or so, therefore a NR_IRQS value of 64
104*4882a593Smuzhiyun * is more than enough.
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * We keep a map of per-PIL enable interrupts. These get wired
107*4882a593Smuzhiyun * up via the irq_chip->startup() method which gets invoked by
108*4882a593Smuzhiyun * the generic IRQ layer during request_irq().
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* Table of allocated irqs. Unused entries has irq == 0 */
113*4882a593Smuzhiyun static struct irq_bucket irq_table[NR_IRQS];
114*4882a593Smuzhiyun /* Protect access to irq_table */
115*4882a593Smuzhiyun static DEFINE_SPINLOCK(irq_table_lock);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Map between the irq identifier used in hw to the irq_bucket. */
118*4882a593Smuzhiyun struct irq_bucket *irq_map[SUN4D_MAX_IRQ];
119*4882a593Smuzhiyun /* Protect access to irq_map */
120*4882a593Smuzhiyun static DEFINE_SPINLOCK(irq_map_lock);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* Allocate a new irq from the irq_table */
irq_alloc(unsigned int real_irq,unsigned int pil)123*4882a593Smuzhiyun unsigned int irq_alloc(unsigned int real_irq, unsigned int pil)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun unsigned long flags;
126*4882a593Smuzhiyun unsigned int i;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun spin_lock_irqsave(&irq_table_lock, flags);
129*4882a593Smuzhiyun for (i = 1; i < NR_IRQS; i++) {
130*4882a593Smuzhiyun if (irq_table[i].real_irq == real_irq && irq_table[i].pil == pil)
131*4882a593Smuzhiyun goto found;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun for (i = 1; i < NR_IRQS; i++) {
135*4882a593Smuzhiyun if (!irq_table[i].irq)
136*4882a593Smuzhiyun break;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (i < NR_IRQS) {
140*4882a593Smuzhiyun irq_table[i].real_irq = real_irq;
141*4882a593Smuzhiyun irq_table[i].irq = i;
142*4882a593Smuzhiyun irq_table[i].pil = pil;
143*4882a593Smuzhiyun } else {
144*4882a593Smuzhiyun printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
145*4882a593Smuzhiyun i = 0;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun found:
148*4882a593Smuzhiyun spin_unlock_irqrestore(&irq_table_lock, flags);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun return i;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* Based on a single pil handler_irq may need to call several
154*4882a593Smuzhiyun * interrupt handlers. Use irq_map as entry to irq_table,
155*4882a593Smuzhiyun * and let each entry in irq_table point to the next entry.
156*4882a593Smuzhiyun */
irq_link(unsigned int irq)157*4882a593Smuzhiyun void irq_link(unsigned int irq)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct irq_bucket *p;
160*4882a593Smuzhiyun unsigned long flags;
161*4882a593Smuzhiyun unsigned int pil;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun BUG_ON(irq >= NR_IRQS);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun spin_lock_irqsave(&irq_map_lock, flags);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun p = &irq_table[irq];
168*4882a593Smuzhiyun pil = p->pil;
169*4882a593Smuzhiyun BUG_ON(pil >= SUN4D_MAX_IRQ);
170*4882a593Smuzhiyun p->next = irq_map[pil];
171*4882a593Smuzhiyun irq_map[pil] = p;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun spin_unlock_irqrestore(&irq_map_lock, flags);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
irq_unlink(unsigned int irq)176*4882a593Smuzhiyun void irq_unlink(unsigned int irq)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun struct irq_bucket *p, **pnext;
179*4882a593Smuzhiyun unsigned long flags;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun BUG_ON(irq >= NR_IRQS);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun spin_lock_irqsave(&irq_map_lock, flags);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun p = &irq_table[irq];
186*4882a593Smuzhiyun BUG_ON(p->pil >= SUN4D_MAX_IRQ);
187*4882a593Smuzhiyun pnext = &irq_map[p->pil];
188*4882a593Smuzhiyun while (*pnext != p)
189*4882a593Smuzhiyun pnext = &(*pnext)->next;
190*4882a593Smuzhiyun *pnext = p->next;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun spin_unlock_irqrestore(&irq_map_lock, flags);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /* /proc/interrupts printing */
arch_show_interrupts(struct seq_file * p,int prec)197*4882a593Smuzhiyun int arch_show_interrupts(struct seq_file *p, int prec)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun int j;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun #ifdef CONFIG_SMP
202*4882a593Smuzhiyun seq_printf(p, "RES: ");
203*4882a593Smuzhiyun for_each_online_cpu(j)
204*4882a593Smuzhiyun seq_printf(p, "%10u ", cpu_data(j).irq_resched_count);
205*4882a593Smuzhiyun seq_printf(p, " IPI rescheduling interrupts\n");
206*4882a593Smuzhiyun seq_printf(p, "CAL: ");
207*4882a593Smuzhiyun for_each_online_cpu(j)
208*4882a593Smuzhiyun seq_printf(p, "%10u ", cpu_data(j).irq_call_count);
209*4882a593Smuzhiyun seq_printf(p, " IPI function call interrupts\n");
210*4882a593Smuzhiyun #endif
211*4882a593Smuzhiyun seq_printf(p, "NMI: ");
212*4882a593Smuzhiyun for_each_online_cpu(j)
213*4882a593Smuzhiyun seq_printf(p, "%10u ", cpu_data(j).counter);
214*4882a593Smuzhiyun seq_printf(p, " Non-maskable interrupts\n");
215*4882a593Smuzhiyun return 0;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
handler_irq(unsigned int pil,struct pt_regs * regs)218*4882a593Smuzhiyun void handler_irq(unsigned int pil, struct pt_regs *regs)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun struct pt_regs *old_regs;
221*4882a593Smuzhiyun struct irq_bucket *p;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun BUG_ON(pil > 15);
224*4882a593Smuzhiyun old_regs = set_irq_regs(regs);
225*4882a593Smuzhiyun irq_enter();
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun p = irq_map[pil];
228*4882a593Smuzhiyun while (p) {
229*4882a593Smuzhiyun struct irq_bucket *next = p->next;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun generic_handle_irq(p->irq);
232*4882a593Smuzhiyun p = next;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun irq_exit();
235*4882a593Smuzhiyun set_irq_regs(old_regs);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun #if defined(CONFIG_BLK_DEV_FD) || defined(CONFIG_BLK_DEV_FD_MODULE)
239*4882a593Smuzhiyun static unsigned int floppy_irq;
240*4882a593Smuzhiyun
sparc_floppy_request_irq(unsigned int irq,irq_handler_t irq_handler)241*4882a593Smuzhiyun int sparc_floppy_request_irq(unsigned int irq, irq_handler_t irq_handler)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun unsigned int cpu_irq;
244*4882a593Smuzhiyun int err;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun err = request_irq(irq, irq_handler, 0, "floppy", NULL);
248*4882a593Smuzhiyun if (err)
249*4882a593Smuzhiyun return -1;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* Save for later use in floppy interrupt handler */
252*4882a593Smuzhiyun floppy_irq = irq;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun cpu_irq = (irq & (NR_IRQS - 1));
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* Dork with trap table if we get this far. */
257*4882a593Smuzhiyun #define INSTANTIATE(table) \
258*4882a593Smuzhiyun table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_one = SPARC_RD_PSR_L0; \
259*4882a593Smuzhiyun table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two = \
260*4882a593Smuzhiyun SPARC_BRANCH((unsigned long) floppy_hardint, \
261*4882a593Smuzhiyun (unsigned long) &table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two);\
262*4882a593Smuzhiyun table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_three = SPARC_RD_WIM_L3; \
263*4882a593Smuzhiyun table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_four = SPARC_NOP;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun INSTANTIATE(sparc_ttable)
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun #if defined CONFIG_SMP
268*4882a593Smuzhiyun if (sparc_cpu_model != sparc_leon) {
269*4882a593Smuzhiyun struct tt_entry *trap_table;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun trap_table = &trapbase_cpu1;
272*4882a593Smuzhiyun INSTANTIATE(trap_table)
273*4882a593Smuzhiyun trap_table = &trapbase_cpu2;
274*4882a593Smuzhiyun INSTANTIATE(trap_table)
275*4882a593Smuzhiyun trap_table = &trapbase_cpu3;
276*4882a593Smuzhiyun INSTANTIATE(trap_table)
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun #endif
279*4882a593Smuzhiyun #undef INSTANTIATE
280*4882a593Smuzhiyun /*
281*4882a593Smuzhiyun * XXX Correct thing whould be to flush only I- and D-cache lines
282*4882a593Smuzhiyun * which contain the handler in question. But as of time of the
283*4882a593Smuzhiyun * writing we have no CPU-neutral interface to fine-grained flushes.
284*4882a593Smuzhiyun */
285*4882a593Smuzhiyun flush_cache_all();
286*4882a593Smuzhiyun return 0;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun EXPORT_SYMBOL(sparc_floppy_request_irq);
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /*
291*4882a593Smuzhiyun * These variables are used to access state from the assembler
292*4882a593Smuzhiyun * interrupt handler, floppy_hardint, so we cannot put these in
293*4882a593Smuzhiyun * the floppy driver image because that would not work in the
294*4882a593Smuzhiyun * modular case.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun volatile unsigned char *fdc_status;
297*4882a593Smuzhiyun EXPORT_SYMBOL(fdc_status);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun char *pdma_vaddr;
300*4882a593Smuzhiyun EXPORT_SYMBOL(pdma_vaddr);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun unsigned long pdma_size;
303*4882a593Smuzhiyun EXPORT_SYMBOL(pdma_size);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun volatile int doing_pdma;
306*4882a593Smuzhiyun EXPORT_SYMBOL(doing_pdma);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun char *pdma_base;
309*4882a593Smuzhiyun EXPORT_SYMBOL(pdma_base);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun unsigned long pdma_areasize;
312*4882a593Smuzhiyun EXPORT_SYMBOL(pdma_areasize);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* Use the generic irq support to call floppy_interrupt
315*4882a593Smuzhiyun * which was setup using request_irq() in sparc_floppy_request_irq().
316*4882a593Smuzhiyun * We only have one floppy interrupt so we do not need to check
317*4882a593Smuzhiyun * for additional handlers being wired up by irq_link()
318*4882a593Smuzhiyun */
sparc_floppy_irq(int irq,void * dev_id,struct pt_regs * regs)319*4882a593Smuzhiyun void sparc_floppy_irq(int irq, void *dev_id, struct pt_regs *regs)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun struct pt_regs *old_regs;
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun old_regs = set_irq_regs(regs);
324*4882a593Smuzhiyun irq_enter();
325*4882a593Smuzhiyun generic_handle_irq(floppy_irq);
326*4882a593Smuzhiyun irq_exit();
327*4882a593Smuzhiyun set_irq_regs(old_regs);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun #endif
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /* djhr
332*4882a593Smuzhiyun * This could probably be made indirect too and assigned in the CPU
333*4882a593Smuzhiyun * bits of the code. That would be much nicer I think and would also
334*4882a593Smuzhiyun * fit in with the idea of being able to tune your kernel for your machine
335*4882a593Smuzhiyun * by removing unrequired machine and device support.
336*4882a593Smuzhiyun *
337*4882a593Smuzhiyun */
338*4882a593Smuzhiyun
init_IRQ(void)339*4882a593Smuzhiyun void __init init_IRQ(void)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun switch (sparc_cpu_model) {
342*4882a593Smuzhiyun case sun4m:
343*4882a593Smuzhiyun pcic_probe();
344*4882a593Smuzhiyun if (pcic_present())
345*4882a593Smuzhiyun sun4m_pci_init_IRQ();
346*4882a593Smuzhiyun else
347*4882a593Smuzhiyun sun4m_init_IRQ();
348*4882a593Smuzhiyun break;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun case sun4d:
351*4882a593Smuzhiyun sun4d_init_IRQ();
352*4882a593Smuzhiyun break;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun case sparc_leon:
355*4882a593Smuzhiyun leon_init_IRQ();
356*4882a593Smuzhiyun break;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun default:
359*4882a593Smuzhiyun prom_printf("Cannot initialize IRQs on this Sun machine...");
360*4882a593Smuzhiyun break;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364