1 // SPDX-License-Identifier: GPL-2.0
3 * ip27-irq.c: Highlevel interrupt handling for IP27 architecture.
6 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
7 * Copyright (C) 1999 - 2001 Kanoj Sarcar
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/ioport.h>
13 #include <linux/kernel.h>
14 #include <linux/bitops.h>
17 #include <asm/irq_cpu.h>
18 #include <asm/pci/bridge.h>
19 #include <asm/sn/addrs.h>
20 #include <asm/sn/agent.h>
21 #include <asm/sn/arch.h>
22 #include <asm/sn/hub.h>
23 #include <asm/sn/intr.h>
26 struct bridge_controller *bc;
33 static DECLARE_BITMAP(hub_irq_map, IP27_HUB_IRQ_COUNT);
35 static DEFINE_PER_CPU(unsigned long [2], irq_enable_mask);
37 static inline int alloc_level(void)
42 level = find_first_zero_bit(hub_irq_map, IP27_HUB_IRQ_COUNT);
43 if (level >= IP27_HUB_IRQ_COUNT)
46 if (test_and_set_bit(level, hub_irq_map))
52 static void enable_hub_irq(struct irq_data *d)
54 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
55 unsigned long *mask = per_cpu(irq_enable_mask, hd->cpu);
57 set_bit(hd->bit, mask);
58 __raw_writeq(mask[0], hd->irq_mask[0]);
59 __raw_writeq(mask[1], hd->irq_mask[1]);
62 static void disable_hub_irq(struct irq_data *d)
64 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
65 unsigned long *mask = per_cpu(irq_enable_mask, hd->cpu);
67 clear_bit(hd->bit, mask);
68 __raw_writeq(mask[0], hd->irq_mask[0]);
69 __raw_writeq(mask[1], hd->irq_mask[1]);
72 static unsigned int startup_bridge_irq(struct irq_data *d)
74 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
75 struct bridge_controller *bc;
86 nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(hd->cpu));
87 bridge_write(bc, b_int_addr[pin].addr,
88 (0x20000 | hd->bit | (nasid << 8)));
89 bridge_set(bc, b_int_enable, (1 << pin));
90 bridge_set(bc, b_int_enable, 0x7ffffe00); /* more stuff in int_enable */
93 * Enable sending of an interrupt clear packt to the hub on a high to
94 * low transition of the interrupt pin.
96 * IRIX sets additional bits in the address which are documented as
97 * reserved in the bridge docs.
99 bridge_set(bc, b_int_mode, (1UL << pin));
102 * We assume the bridge to have a 1:1 mapping between devices
103 * (slots) and intr pins.
105 device = bridge_read(bc, b_int_device);
106 device &= ~(7 << (pin*3));
107 device |= (pin << (pin*3));
108 bridge_write(bc, b_int_device, device);
110 bridge_read(bc, b_wid_tflush);
114 return 0; /* Never anything pending. */
117 static void shutdown_bridge_irq(struct irq_data *d)
119 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
120 struct bridge_controller *bc;
129 bridge_clr(bc, b_int_enable, (1 << pin));
130 bridge_read(bc, b_wid_tflush);
133 static void setup_hub_mask(struct hub_irq_data *hd, const struct cpumask *mask)
138 cpu = cpumask_first_and(mask, cpu_online_mask);
139 nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
141 if (!cputoslice(cpu)) {
142 hd->irq_mask[0] = REMOTE_HUB_PTR(nasid, PI_INT_MASK0_A);
143 hd->irq_mask[1] = REMOTE_HUB_PTR(nasid, PI_INT_MASK1_A);
145 hd->irq_mask[0] = REMOTE_HUB_PTR(nasid, PI_INT_MASK0_B);
146 hd->irq_mask[1] = REMOTE_HUB_PTR(nasid, PI_INT_MASK1_B);
149 /* Make sure it's not already pending when we connect it. */
150 REMOTE_HUB_CLR_INTR(nasid, hd->bit);
153 static int set_affinity_hub_irq(struct irq_data *d, const struct cpumask *mask,
156 struct hub_irq_data *hd = irq_data_get_irq_chip_data(d);
161 if (irqd_is_started(d))
164 setup_hub_mask(hd, mask);
166 if (irqd_is_started(d))
167 startup_bridge_irq(d);
169 irq_data_update_effective_affinity(d, cpumask_of(hd->cpu));
174 static struct irq_chip hub_irq_type = {
176 .irq_startup = startup_bridge_irq,
177 .irq_shutdown = shutdown_bridge_irq,
178 .irq_mask = disable_hub_irq,
179 .irq_unmask = enable_hub_irq,
180 .irq_set_affinity = set_affinity_hub_irq,
183 int request_bridge_irq(struct bridge_controller *bc, int pin)
185 struct hub_irq_data *hd;
186 struct hub_data *hub;
187 struct irq_desc *desc;
191 hd = kzalloc(sizeof(*hd), GFP_KERNEL);
195 swlevel = alloc_level();
196 if (unlikely(swlevel < 0)) {
200 irq = swlevel + IP27_HUB_IRQ_BASE;
205 irq_set_chip_data(irq, hd);
207 /* use CPU connected to nearest hub */
208 hub = hub_data(NASID_TO_COMPACT_NODEID(bc->nasid));
209 setup_hub_mask(hd, &hub->h_cpus);
211 desc = irq_to_desc(irq);
212 desc->irq_common_data.node = bc->nasid;
213 cpumask_copy(desc->irq_common_data.affinity, &hub->h_cpus);
218 void ip27_hub_irq_init(void)
222 for (i = IP27_HUB_IRQ_BASE;
223 i < (IP27_HUB_IRQ_BASE + IP27_HUB_IRQ_COUNT); i++)
224 irq_set_chip_and_handler(i, &hub_irq_type, handle_level_irq);
227 * Some interrupts are reserved by hardware or by software convention.
228 * Mark these as reserved right away so they won't be used accidentally
231 for (i = 0; i <= BASE_PCI_IRQ; i++)
232 set_bit(i, hub_irq_map);
234 set_bit(IP_PEND0_6_63, hub_irq_map);
236 for (i = NI_BRDCAST_ERR_A; i <= MSC_PANIC_INTR; i++)
237 set_bit(i, hub_irq_map);
241 * This code is unnecessarily complex, because we do
242 * intr enabling. Basically, once we grab the set of intrs we need
243 * to service, we must mask _all_ these interrupts; firstly, to make
244 * sure the same intr does not intr again, causing recursion that
245 * can lead to stack overflow. Secondly, we can not just mask the
246 * one intr we are do_IRQing, because the non-masked intrs in the
247 * first set might intr again, causing multiple servicings of the
248 * same intr. This effect is mostly seen for intercpu intrs.
252 static void ip27_do_irq_mask0(struct irq_desc *desc)
254 cpuid_t cpu = smp_processor_id();
255 unsigned long *mask = per_cpu(irq_enable_mask, cpu);
258 /* copied from Irix intpend0() */
259 pend0 = LOCAL_HUB_L(PI_INT_PEND0);
261 pend0 &= mask[0]; /* Pick intrs we should look at */
266 if (pend0 & (1UL << CPU_RESCHED_A_IRQ)) {
267 LOCAL_HUB_CLR_INTR(CPU_RESCHED_A_IRQ);
269 } else if (pend0 & (1UL << CPU_RESCHED_B_IRQ)) {
270 LOCAL_HUB_CLR_INTR(CPU_RESCHED_B_IRQ);
272 } else if (pend0 & (1UL << CPU_CALL_A_IRQ)) {
273 LOCAL_HUB_CLR_INTR(CPU_CALL_A_IRQ);
274 generic_smp_call_function_interrupt();
275 } else if (pend0 & (1UL << CPU_CALL_B_IRQ)) {
276 LOCAL_HUB_CLR_INTR(CPU_CALL_B_IRQ);
277 generic_smp_call_function_interrupt();
280 generic_handle_irq(__ffs(pend0) + IP27_HUB_IRQ_BASE);
282 LOCAL_HUB_L(PI_INT_PEND0);
285 static void ip27_do_irq_mask1(struct irq_desc *desc)
287 cpuid_t cpu = smp_processor_id();
288 unsigned long *mask = per_cpu(irq_enable_mask, cpu);
291 /* copied from Irix intpend0() */
292 pend1 = LOCAL_HUB_L(PI_INT_PEND1);
294 pend1 &= mask[1]; /* Pick intrs we should look at */
298 generic_handle_irq(__ffs(pend1) + IP27_HUB_IRQ_BASE + 64);
300 LOCAL_HUB_L(PI_INT_PEND1);
303 void install_ipi(void)
305 int cpu = smp_processor_id();
306 unsigned long *mask = per_cpu(irq_enable_mask, cpu);
307 int slice = LOCAL_HUB_L(PI_CPU_NUM);
310 resched = CPU_RESCHED_A_IRQ + slice;
311 set_bit(resched, mask);
312 LOCAL_HUB_CLR_INTR(resched);
314 call = CPU_CALL_A_IRQ + slice;
316 LOCAL_HUB_CLR_INTR(call);
319 LOCAL_HUB_S(PI_INT_MASK0_A, mask[0]);
320 LOCAL_HUB_S(PI_INT_MASK1_A, mask[1]);
322 LOCAL_HUB_S(PI_INT_MASK0_B, mask[0]);
323 LOCAL_HUB_S(PI_INT_MASK1_B, mask[1]);
327 void __init arch_init_irq(void)
332 irq_set_percpu_devid(IP27_HUB_PEND0_IRQ);
333 irq_set_chained_handler(IP27_HUB_PEND0_IRQ, ip27_do_irq_mask0);
334 irq_set_percpu_devid(IP27_HUB_PEND1_IRQ);
335 irq_set_chained_handler(IP27_HUB_PEND1_IRQ, ip27_do_irq_mask1);