GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / metag / kernel / irq.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Linux/Meta general interrupt handling code
4  *
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/interrupt.h>
9 #include <linux/init.h>
10 #include <linux/irqchip/metag-ext.h>
11 #include <linux/irqchip/metag.h>
12 #include <linux/irqdomain.h>
13 #include <linux/ratelimit.h>
14
15 #include <asm/core_reg.h>
16 #include <asm/mach/arch.h>
17 #include <linux/uaccess.h>
18
19 #ifdef CONFIG_4KSTACKS
20 union irq_ctx {
21         struct thread_info      tinfo;
22         u32                     stack[THREAD_SIZE/sizeof(u32)];
23 };
24
25 static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
26 static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
27 #endif
28
29 static struct irq_domain *root_domain;
30
31 static unsigned int startup_meta_irq(struct irq_data *data)
32 {
33         tbi_startup_interrupt(data->hwirq);
34         return 0;
35 }
36
37 static void shutdown_meta_irq(struct irq_data *data)
38 {
39         tbi_shutdown_interrupt(data->hwirq);
40 }
41
42 void do_IRQ(int irq, struct pt_regs *regs)
43 {
44         struct pt_regs *old_regs = set_irq_regs(regs);
45 #ifdef CONFIG_4KSTACKS
46         struct irq_desc *desc;
47         union irq_ctx *curctx, *irqctx;
48         u32 *isp;
49 #endif
50
51         irq_enter();
52
53         irq = irq_linear_revmap(root_domain, irq);
54
55 #ifdef CONFIG_DEBUG_STACKOVERFLOW
56         /* Debugging check for stack overflow: is there less than 1KB free? */
57         {
58                 unsigned long sp;
59
60                 sp = __core_reg_get(A0StP);
61                 sp &= THREAD_SIZE - 1;
62
63                 if (unlikely(sp > (THREAD_SIZE - 1024)))
64                         pr_err("Stack overflow in do_IRQ: %ld\n", sp);
65         }
66 #endif
67
68
69 #ifdef CONFIG_4KSTACKS
70         curctx = (union irq_ctx *) current_thread_info();
71         irqctx = hardirq_ctx[smp_processor_id()];
72
73         /*
74          * this is where we switch to the IRQ stack. However, if we are
75          * already using the IRQ stack (because we interrupted a hardirq
76          * handler) we can't do that and just have to keep using the
77          * current stack (which is the irq stack already after all)
78          */
79         if (curctx != irqctx) {
80                 /* build the stack frame on the IRQ stack */
81                 isp = (u32 *) ((char *)irqctx + sizeof(struct thread_info));
82                 irqctx->tinfo.task = curctx->tinfo.task;
83
84                 /*
85                  * Copy the softirq bits in preempt_count so that the
86                  * softirq checks work in the hardirq context.
87                  */
88                 irqctx->tinfo.preempt_count =
89                         (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
90                         (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
91
92                 desc = irq_to_desc(irq);
93
94                 asm volatile (
95                         "MOV   D0.5,%0\n"
96                         "MOV   D1Ar1,%1\n"
97                         "MOV   D1RtP,%2\n"
98                         "SWAP  A0StP,D0.5\n"
99                         "SWAP  PC,D1RtP\n"
100                         "MOV   A0StP,D0.5\n"
101                         :
102                         : "r" (isp), "r" (desc), "r" (desc->handle_irq)
103                         : "memory", "cc", "D1Ar1", "D0Ar2", "D1Ar3", "D0Ar4",
104                           "D1Ar5", "D0Ar6", "D0Re0", "D1Re0", "D0.4", "D1RtP",
105                           "D0.5"
106                         );
107         } else
108 #endif
109                 generic_handle_irq(irq);
110
111         irq_exit();
112
113         set_irq_regs(old_regs);
114 }
115
116 #ifdef CONFIG_4KSTACKS
117
118 static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
119
120 static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
121
122 /*
123  * allocate per-cpu stacks for hardirq and for softirq processing
124  */
125 void irq_ctx_init(int cpu)
126 {
127         union irq_ctx *irqctx;
128
129         if (hardirq_ctx[cpu])
130                 return;
131
132         irqctx = (union irq_ctx *) &hardirq_stack[cpu * THREAD_SIZE];
133         irqctx->tinfo.task              = NULL;
134         irqctx->tinfo.cpu               = cpu;
135         irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
136         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
137
138         hardirq_ctx[cpu] = irqctx;
139
140         irqctx = (union irq_ctx *) &softirq_stack[cpu * THREAD_SIZE];
141         irqctx->tinfo.task              = NULL;
142         irqctx->tinfo.cpu               = cpu;
143         irqctx->tinfo.preempt_count     = 0;
144         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
145
146         softirq_ctx[cpu] = irqctx;
147
148         pr_info("CPU %u irqstacks, hard=%p soft=%p\n",
149                 cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
150 }
151
152 void irq_ctx_exit(int cpu)
153 {
154         hardirq_ctx[smp_processor_id()] = NULL;
155 }
156
157 extern asmlinkage void __do_softirq(void);
158
159 void do_softirq_own_stack(void)
160 {
161         struct thread_info *curctx;
162         union irq_ctx *irqctx;
163         u32 *isp;
164
165         curctx = current_thread_info();
166         irqctx = softirq_ctx[smp_processor_id()];
167         irqctx->tinfo.task = curctx->task;
168
169         /* build the stack frame on the softirq stack */
170         isp = (u32 *) ((char *)irqctx + sizeof(struct thread_info));
171
172         asm volatile (
173                 "MOV   D0.5,%0\n"
174                 "SWAP  A0StP,D0.5\n"
175                 "CALLR D1RtP,___do_softirq\n"
176                 "MOV   A0StP,D0.5\n"
177                 :
178                 : "r" (isp)
179                 : "memory", "cc", "D1Ar1", "D0Ar2", "D1Ar3", "D0Ar4",
180                   "D1Ar5", "D0Ar6", "D0Re0", "D1Re0", "D0.4", "D1RtP",
181                   "D0.5"
182                 );
183 }
184 #endif
185
186 static struct irq_chip meta_irq_type = {
187         .name = "META-IRQ",
188         .irq_startup = startup_meta_irq,
189         .irq_shutdown = shutdown_meta_irq,
190 };
191
192 /**
193  * tbisig_map() - Map a TBI signal number to a virtual IRQ number.
194  * @hw:         Number of the TBI signal. Must be in range.
195  *
196  * Returns:     The virtual IRQ number of the TBI signal number IRQ specified by
197  *              @hw.
198  */
199 int tbisig_map(unsigned int hw)
200 {
201         return irq_create_mapping(root_domain, hw);
202 }
203
204 /**
205  * metag_tbisig_map() - map a tbi signal to a Linux virtual IRQ number
206  * @d:          root irq domain
207  * @irq:        virtual irq number
208  * @hw:         hardware irq number (TBI signal number)
209  *
210  * This sets up a virtual irq for a specified TBI signal number.
211  */
212 static int metag_tbisig_map(struct irq_domain *d, unsigned int irq,
213                             irq_hw_number_t hw)
214 {
215 #ifdef CONFIG_SMP
216         irq_set_chip_and_handler(irq, &meta_irq_type, handle_percpu_irq);
217 #else
218         irq_set_chip_and_handler(irq, &meta_irq_type, handle_simple_irq);
219 #endif
220         return 0;
221 }
222
223 static const struct irq_domain_ops metag_tbisig_domain_ops = {
224         .map = metag_tbisig_map,
225 };
226
227 /*
228  * void init_IRQ(void)
229  *
230  * Parameters:  None
231  *
232  * Returns:     Nothing
233  *
234  * This function should be called during kernel startup to initialize
235  * the IRQ handling routines.
236  */
237 void __init init_IRQ(void)
238 {
239         root_domain = irq_domain_add_linear(NULL, 32,
240                                             &metag_tbisig_domain_ops, NULL);
241         if (unlikely(!root_domain))
242                 panic("init_IRQ: cannot add root IRQ domain");
243
244         irq_ctx_init(smp_processor_id());
245
246         init_internal_IRQ();
247         init_external_IRQ();
248
249         if (machine_desc->init_irq)
250                 machine_desc->init_irq();
251 }
252
253 int __init arch_probe_nr_irqs(void)
254 {
255         if (machine_desc->nr_irqs)
256                 nr_irqs = machine_desc->nr_irqs;
257         return 0;
258 }
259
260 #ifdef CONFIG_HOTPLUG_CPU
261 /*
262  * The CPU has been marked offline.  Migrate IRQs off this CPU.  If
263  * the affinity settings do not allow other CPUs, force them onto any
264  * available CPU.
265  */
266 void migrate_irqs(void)
267 {
268         unsigned int i, cpu = smp_processor_id();
269
270         for_each_active_irq(i) {
271                 struct irq_data *data = irq_get_irq_data(i);
272                 struct cpumask *mask;
273                 unsigned int newcpu;
274
275                 if (irqd_is_per_cpu(data))
276                         continue;
277
278                 mask = irq_data_get_affinity_mask(data);
279                 if (!cpumask_test_cpu(cpu, mask))
280                         continue;
281
282                 newcpu = cpumask_any_and(mask, cpu_online_mask);
283
284                 if (newcpu >= nr_cpu_ids) {
285                         pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
286                                             i, cpu);
287
288                         cpumask_setall(mask);
289                 }
290                 irq_set_affinity(i, mask);
291         }
292 }
293 #endif /* CONFIG_HOTPLUG_CPU */