blob: 42df9fafa943ecfbe807a807b05cfbc73a0333c1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ip27-irq.c: Highlevel interrupt handling for IP27 architecture.
4 *
5 * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
6 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
7 * Copyright (C) 1999 - 2001 Kanoj Sarcar
8 */
Ralf Baechled3ffd082005-08-08 12:42:26 +00009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/interrupt.h>
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010011#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/ioport.h>
Ralf Baechled3ffd082005-08-08 12:42:26 +000013#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bitops.h>
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +020015#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/io.h>
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010018#include <asm/irq_cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/sn/addrs.h>
20#include <asm/sn/agent.h>
21#include <asm/sn/arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/sn/intr.h>
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +020023#include <asm/sn/irq_alloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010025struct hub_irq_data {
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010026 u64 *irq_mask[2];
27 cpuid_t cpu;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010028};
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010030static DECLARE_BITMAP(hub_irq_map, IP27_HUB_IRQ_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010032static DEFINE_PER_CPU(unsigned long [2], irq_enable_mask);
33
34static inline int alloc_level(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010036 int level;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010038again:
39 level = find_first_zero_bit(hub_irq_map, IP27_HUB_IRQ_COUNT);
40 if (level >= IP27_HUB_IRQ_COUNT)
41 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010043 if (test_and_set_bit(level, hub_irq_map))
44 goto again;
45
46 return level;
47}
48
49static void enable_hub_irq(struct irq_data *d)
50{
51 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
52 unsigned long *mask = per_cpu(irq_enable_mask, hd->cpu);
53
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +020054 set_bit(d->hwirq, mask);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010055 __raw_writeq(mask[0], hd->irq_mask[0]);
56 __raw_writeq(mask[1], hd->irq_mask[1]);
57}
58
59static void disable_hub_irq(struct irq_data *d)
60{
61 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
62 unsigned long *mask = per_cpu(irq_enable_mask, hd->cpu);
63
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +020064 clear_bit(d->hwirq, mask);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010065 __raw_writeq(mask[0], hd->irq_mask[0]);
66 __raw_writeq(mask[1], hd->irq_mask[1]);
67}
68
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010069static void setup_hub_mask(struct hub_irq_data *hd, const struct cpumask *mask)
70{
71 nasid_t nasid;
72 int cpu;
73
74 cpu = cpumask_first_and(mask, cpu_online_mask);
Thomas Bogendoerfere3d765a2019-11-19 12:08:56 +010075 if (cpu >= nr_cpu_ids)
76 cpu = cpumask_any(cpu_online_mask);
77
Thomas Bogendoerfer4bf841e2019-10-03 14:27:24 +020078 nasid = cpu_to_node(cpu);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010079 hd->cpu = cpu;
80 if (!cputoslice(cpu)) {
81 hd->irq_mask[0] = REMOTE_HUB_PTR(nasid, PI_INT_MASK0_A);
82 hd->irq_mask[1] = REMOTE_HUB_PTR(nasid, PI_INT_MASK1_A);
83 } else {
84 hd->irq_mask[0] = REMOTE_HUB_PTR(nasid, PI_INT_MASK0_B);
85 hd->irq_mask[1] = REMOTE_HUB_PTR(nasid, PI_INT_MASK1_B);
86 }
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +010087}
88
89static int set_affinity_hub_irq(struct irq_data *d, const struct cpumask *mask,
90 bool force)
91{
92 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
93
94 if (!hd)
95 return -EINVAL;
96
97 if (irqd_is_started(d))
98 disable_hub_irq(d);
99
100 setup_hub_mask(hd, mask);
101
102 if (irqd_is_started(d))
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200103 enable_hub_irq(d);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100104
105 irq_data_update_effective_affinity(d, cpumask_of(hd->cpu));
106
107 return 0;
108}
109
110static struct irq_chip hub_irq_type = {
111 .name = "HUB",
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100112 .irq_mask = disable_hub_irq,
113 .irq_unmask = enable_hub_irq,
114 .irq_set_affinity = set_affinity_hub_irq,
115};
116
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200117static int hub_domain_alloc(struct irq_domain *domain, unsigned int virq,
118 unsigned int nr_irqs, void *arg)
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100119{
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200120 struct irq_alloc_info *info = arg;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100121 struct hub_irq_data *hd;
122 struct hub_data *hub;
123 struct irq_desc *desc;
124 int swlevel;
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200125
126 if (nr_irqs > 1 || !info)
127 return -EINVAL;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100128
129 hd = kzalloc(sizeof(*hd), GFP_KERNEL);
130 if (!hd)
131 return -ENOMEM;
132
133 swlevel = alloc_level();
134 if (unlikely(swlevel < 0)) {
135 kfree(hd);
136 return -EAGAIN;
137 }
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200138 irq_domain_set_info(domain, virq, swlevel, &hub_irq_type, hd,
139 handle_level_irq, NULL, NULL);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100140
141 /* use CPU connected to nearest hub */
Thomas Bogendoerfer4bf841e2019-10-03 14:27:24 +0200142 hub = hub_data(info->nasid);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100143 setup_hub_mask(hd, &hub->h_cpus);
Thomas Bogendoerfere3d765a2019-11-19 12:08:56 +0100144 info->nasid = cpu_to_node(hd->cpu);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100145
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200146 /* Make sure it's not already pending when we connect it. */
147 REMOTE_HUB_CLR_INTR(info->nasid, swlevel);
148
149 desc = irq_to_desc(virq);
150 desc->irq_common_data.node = info->nasid;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100151 cpumask_copy(desc->irq_common_data.affinity, &hub->h_cpus);
152
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200153 return 0;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100154}
155
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200156static void hub_domain_free(struct irq_domain *domain,
157 unsigned int virq, unsigned int nr_irqs)
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100158{
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200159 struct irq_data *irqd;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100160
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200161 if (nr_irqs > 1)
162 return;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100163
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200164 irqd = irq_domain_get_irq_data(domain, virq);
165 if (irqd && irqd->chip_data)
166 kfree(irqd->chip_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200169static const struct irq_domain_ops hub_domain_ops = {
170 .alloc = hub_domain_alloc,
171 .free = hub_domain_free,
172};
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Yong Zhang8b5690f2011-11-22 14:38:03 +0000175 * This code is unnecessarily complex, because we do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * intr enabling. Basically, once we grab the set of intrs we need
177 * to service, we must mask _all_ these interrupts; firstly, to make
178 * sure the same intr does not intr again, causing recursion that
179 * can lead to stack overflow. Secondly, we can not just mask the
180 * one intr we are do_IRQing, because the non-masked intrs in the
181 * first set might intr again, causing multiple servicings of the
182 * same intr. This effect is mostly seen for intercpu intrs.
183 * Kanoj 05.13.00
184 */
185
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100186static void ip27_do_irq_mask0(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 cpuid_t cpu = smp_processor_id();
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100189 unsigned long *mask = per_cpu(irq_enable_mask, cpu);
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200190 struct irq_domain *domain;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100191 u64 pend0;
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200192 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* copied from Irix intpend0() */
195 pend0 = LOCAL_HUB_L(PI_INT_PEND0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100197 pend0 &= mask[0]; /* Pick intrs we should look at */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!pend0)
199 return;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#ifdef CONFIG_SMP
202 if (pend0 & (1UL << CPU_RESCHED_A_IRQ)) {
203 LOCAL_HUB_CLR_INTR(CPU_RESCHED_A_IRQ);
Peter Zijlstra184748c2011-04-05 17:23:39 +0200204 scheduler_ipi();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else if (pend0 & (1UL << CPU_RESCHED_B_IRQ)) {
206 LOCAL_HUB_CLR_INTR(CPU_RESCHED_B_IRQ);
Peter Zijlstra184748c2011-04-05 17:23:39 +0200207 scheduler_ipi();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 } else if (pend0 & (1UL << CPU_CALL_A_IRQ)) {
209 LOCAL_HUB_CLR_INTR(CPU_CALL_A_IRQ);
Alex Smith4ace6132015-07-24 16:57:49 +0100210 generic_smp_call_function_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 } else if (pend0 & (1UL << CPU_CALL_B_IRQ)) {
212 LOCAL_HUB_CLR_INTR(CPU_CALL_B_IRQ);
Alex Smith4ace6132015-07-24 16:57:49 +0100213 generic_smp_call_function_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else
215#endif
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200216 {
217 domain = irq_desc_get_handler_data(desc);
218 irq = irq_linear_revmap(domain, __ffs(pend0));
219 if (irq)
220 generic_handle_irq(irq);
221 else
222 spurious_interrupt();
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 LOCAL_HUB_L(PI_INT_PEND0);
226}
227
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100228static void ip27_do_irq_mask1(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 cpuid_t cpu = smp_processor_id();
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100231 unsigned long *mask = per_cpu(irq_enable_mask, cpu);
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200232 struct irq_domain *domain;
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100233 u64 pend1;
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200234 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* copied from Irix intpend0() */
237 pend1 = LOCAL_HUB_L(PI_INT_PEND1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100239 pend1 &= mask[1]; /* Pick intrs we should look at */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!pend1)
241 return;
242
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200243 domain = irq_desc_get_handler_data(desc);
244 irq = irq_linear_revmap(domain, __ffs(pend1) + 64);
245 if (irq)
246 generic_handle_irq(irq);
247 else
248 spurious_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 LOCAL_HUB_L(PI_INT_PEND1);
251}
252
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100253void install_ipi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100255 int cpu = smp_processor_id();
256 unsigned long *mask = per_cpu(irq_enable_mask, cpu);
257 int slice = LOCAL_HUB_L(PI_CPU_NUM);
258 int resched, call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100260 resched = CPU_RESCHED_A_IRQ + slice;
261 set_bit(resched, mask);
262 LOCAL_HUB_CLR_INTR(resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100264 call = CPU_CALL_A_IRQ + slice;
265 set_bit(call, mask);
266 LOCAL_HUB_CLR_INTR(call);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100267
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100268 if (slice == 0) {
269 LOCAL_HUB_S(PI_INT_MASK0_A, mask[0]);
270 LOCAL_HUB_S(PI_INT_MASK1_A, mask[1]);
271 } else {
272 LOCAL_HUB_S(PI_INT_MASK0_B, mask[0]);
273 LOCAL_HUB_S(PI_INT_MASK1_B, mask[1]);
274 }
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277void __init arch_init_irq(void)
278{
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200279 struct irq_domain *domain;
280 struct fwnode_handle *fn;
281 int i;
282
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100283 mips_cpu_irq_init();
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200284
285 /*
286 * Some interrupts are reserved by hardware or by software convention.
287 * Mark these as reserved right away so they won't be used accidentally
288 * later.
289 */
Thomas Bogendoerfer78691e42020-01-09 13:33:49 +0100290 for (i = 0; i <= CPU_CALL_B_IRQ; i++)
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200291 set_bit(i, hub_irq_map);
292
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200293 for (i = NI_BRDCAST_ERR_A; i <= MSC_PANIC_INTR; i++)
294 set_bit(i, hub_irq_map);
295
296 fn = irq_domain_alloc_named_fwnode("HUB");
297 WARN_ON(fn == NULL);
298 if (!fn)
299 return;
300 domain = irq_domain_create_linear(fn, IP27_HUB_IRQ_COUNT,
301 &hub_domain_ops, NULL);
302 WARN_ON(domain == NULL);
303 if (!domain)
304 return;
305
306 irq_set_default_host(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100308 irq_set_percpu_devid(IP27_HUB_PEND0_IRQ);
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200309 irq_set_chained_handler_and_data(IP27_HUB_PEND0_IRQ, ip27_do_irq_mask0,
310 domain);
Thomas Bogendoerfer69a07a42019-02-19 16:57:20 +0100311 irq_set_percpu_devid(IP27_HUB_PEND1_IRQ);
Thomas Bogendoerfere6308b62019-05-07 23:09:15 +0200312 irq_set_chained_handler_and_data(IP27_HUB_PEND1_IRQ, ip27_do_irq_mask1,
313 domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}