xref: /linux/drivers/irqchip/irq-loongson-eiointc.c (revision ee8287e068a3995b0f8001dd6931e221dfb7c530)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Loongson Extend I/O Interrupt Controller support
4  *
5  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6  */
7 
8 #define pr_fmt(fmt) "eiointc: " fmt
9 
10 #include <linux/cpuhotplug.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/irqchip.h>
14 #include <linux/irqdomain.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/kernel.h>
17 #include <linux/syscore_ops.h>
18 
19 #define EIOINTC_REG_NODEMAP	0x14a0
20 #define EIOINTC_REG_IPMAP	0x14c0
21 #define EIOINTC_REG_ENABLE	0x1600
22 #define EIOINTC_REG_BOUNCE	0x1680
23 #define EIOINTC_REG_ISR		0x1800
24 #define EIOINTC_REG_ROUTE	0x1c00
25 
26 #define VEC_REG_COUNT		4
27 #define VEC_COUNT_PER_REG	64
28 #define VEC_COUNT		(VEC_REG_COUNT * VEC_COUNT_PER_REG)
29 #define VEC_REG_IDX(irq_id)	((irq_id) / VEC_COUNT_PER_REG)
30 #define VEC_REG_BIT(irq_id)     ((irq_id) % VEC_COUNT_PER_REG)
31 #define EIOINTC_ALL_ENABLE	0xffffffff
32 
33 #define MAX_EIO_NODES		(NR_CPUS / CORES_PER_EIO_NODE)
34 
35 static int nr_pics;
36 
37 struct eiointc_priv {
38 	u32			node;
39 	u32			vec_count;
40 	nodemask_t		node_map;
41 	cpumask_t		cpuspan_map;
42 	struct fwnode_handle	*domain_handle;
43 	struct irq_domain	*eiointc_domain;
44 };
45 
46 static struct eiointc_priv *eiointc_priv[MAX_IO_PICS];
47 
48 static void eiointc_enable(void)
49 {
50 	uint64_t misc;
51 
52 	misc = iocsr_read64(LOONGARCH_IOCSR_MISC_FUNC);
53 	misc |= IOCSR_MISC_FUNC_EXT_IOI_EN;
54 	iocsr_write64(misc, LOONGARCH_IOCSR_MISC_FUNC);
55 }
56 
57 static int cpu_to_eio_node(int cpu)
58 {
59 	return cpu_logical_map(cpu) / CORES_PER_EIO_NODE;
60 }
61 
62 #ifdef CONFIG_SMP
63 static void eiointc_set_irq_route(int pos, unsigned int cpu, unsigned int mnode, nodemask_t *node_map)
64 {
65 	int i, node, cpu_node, route_node;
66 	unsigned char coremap;
67 	uint32_t pos_off, data, data_byte, data_mask;
68 
69 	pos_off = pos & ~3;
70 	data_byte = pos & 3;
71 	data_mask = ~BIT_MASK(data_byte) & 0xf;
72 
73 	/* Calculate node and coremap of target irq */
74 	cpu_node = cpu_logical_map(cpu) / CORES_PER_EIO_NODE;
75 	coremap = BIT(cpu_logical_map(cpu) % CORES_PER_EIO_NODE);
76 
77 	for_each_online_cpu(i) {
78 		node = cpu_to_eio_node(i);
79 		if (!node_isset(node, *node_map))
80 			continue;
81 
82 		/* EIO node 0 is in charge of inter-node interrupt dispatch */
83 		route_node = (node == mnode) ? cpu_node : node;
84 		data = ((coremap | (route_node << 4)) << (data_byte * 8));
85 		csr_any_send(EIOINTC_REG_ROUTE + pos_off, data, data_mask, node * CORES_PER_EIO_NODE);
86 	}
87 }
88 
89 static DEFINE_RAW_SPINLOCK(affinity_lock);
90 
91 static int eiointc_set_irq_affinity(struct irq_data *d, const struct cpumask *affinity, bool force)
92 {
93 	unsigned int cpu;
94 	unsigned long flags;
95 	uint32_t vector, regaddr;
96 	struct eiointc_priv *priv = d->domain->host_data;
97 
98 	raw_spin_lock_irqsave(&affinity_lock, flags);
99 
100 	cpu = cpumask_first_and_and(&priv->cpuspan_map, affinity, cpu_online_mask);
101 	if (cpu >= nr_cpu_ids) {
102 		raw_spin_unlock_irqrestore(&affinity_lock, flags);
103 		return -EINVAL;
104 	}
105 
106 	vector = d->hwirq;
107 	regaddr = EIOINTC_REG_ENABLE + ((vector >> 5) << 2);
108 
109 	/* Mask target vector */
110 	csr_any_send(regaddr, EIOINTC_ALL_ENABLE & (~BIT(vector & 0x1F)),
111 			0x0, priv->node * CORES_PER_EIO_NODE);
112 
113 	/* Set route for target vector */
114 	eiointc_set_irq_route(vector, cpu, priv->node, &priv->node_map);
115 
116 	/* Unmask target vector */
117 	csr_any_send(regaddr, EIOINTC_ALL_ENABLE,
118 			0x0, priv->node * CORES_PER_EIO_NODE);
119 
120 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
121 
122 	raw_spin_unlock_irqrestore(&affinity_lock, flags);
123 
124 	return IRQ_SET_MASK_OK;
125 }
126 #endif
127 
128 static int eiointc_index(int node)
129 {
130 	int i;
131 
132 	for (i = 0; i < nr_pics; i++) {
133 		if (node_isset(node, eiointc_priv[i]->node_map))
134 			return i;
135 	}
136 
137 	return -1;
138 }
139 
140 static int eiointc_router_init(unsigned int cpu)
141 {
142 	int i, bit;
143 	uint32_t data;
144 	uint32_t node = cpu_to_eio_node(cpu);
145 	int index = eiointc_index(node);
146 
147 	if (index < 0) {
148 		pr_err("Error: invalid nodemap!\n");
149 		return -1;
150 	}
151 
152 	if ((cpu_logical_map(cpu) % CORES_PER_EIO_NODE) == 0) {
153 		eiointc_enable();
154 
155 		for (i = 0; i < eiointc_priv[0]->vec_count / 32; i++) {
156 			data = (((1 << (i * 2 + 1)) << 16) | (1 << (i * 2)));
157 			iocsr_write32(data, EIOINTC_REG_NODEMAP + i * 4);
158 		}
159 
160 		for (i = 0; i < eiointc_priv[0]->vec_count / 32 / 4; i++) {
161 			bit = BIT(1 + index); /* Route to IP[1 + index] */
162 			data = bit | (bit << 8) | (bit << 16) | (bit << 24);
163 			iocsr_write32(data, EIOINTC_REG_IPMAP + i * 4);
164 		}
165 
166 		for (i = 0; i < eiointc_priv[0]->vec_count / 4; i++) {
167 			/* Route to Node-0 Core-0 */
168 			if (index == 0)
169 				bit = BIT(cpu_logical_map(0));
170 			else
171 				bit = (eiointc_priv[index]->node << 4) | 1;
172 
173 			data = bit | (bit << 8) | (bit << 16) | (bit << 24);
174 			iocsr_write32(data, EIOINTC_REG_ROUTE + i * 4);
175 		}
176 
177 		for (i = 0; i < eiointc_priv[0]->vec_count / 32; i++) {
178 			data = 0xffffffff;
179 			iocsr_write32(data, EIOINTC_REG_ENABLE + i * 4);
180 			iocsr_write32(data, EIOINTC_REG_BOUNCE + i * 4);
181 		}
182 	}
183 
184 	return 0;
185 }
186 
187 static void eiointc_irq_dispatch(struct irq_desc *desc)
188 {
189 	int i;
190 	u64 pending;
191 	bool handled = false;
192 	struct irq_chip *chip = irq_desc_get_chip(desc);
193 	struct eiointc_priv *priv = irq_desc_get_handler_data(desc);
194 
195 	chained_irq_enter(chip, desc);
196 
197 	for (i = 0; i < eiointc_priv[0]->vec_count / VEC_COUNT_PER_REG; i++) {
198 		pending = iocsr_read64(EIOINTC_REG_ISR + (i << 3));
199 
200 		/* Skip handling if pending bitmap is zero */
201 		if (!pending)
202 			continue;
203 
204 		/* Clear the IRQs */
205 		iocsr_write64(pending, EIOINTC_REG_ISR + (i << 3));
206 		while (pending) {
207 			int bit = __ffs(pending);
208 			int irq = bit + VEC_COUNT_PER_REG * i;
209 
210 			generic_handle_domain_irq(priv->eiointc_domain, irq);
211 			pending &= ~BIT(bit);
212 			handled = true;
213 		}
214 	}
215 
216 	if (!handled)
217 		spurious_interrupt();
218 
219 	chained_irq_exit(chip, desc);
220 }
221 
222 static void eiointc_ack_irq(struct irq_data *d)
223 {
224 }
225 
226 static void eiointc_mask_irq(struct irq_data *d)
227 {
228 }
229 
230 static void eiointc_unmask_irq(struct irq_data *d)
231 {
232 }
233 
234 static struct irq_chip eiointc_irq_chip = {
235 	.name			= "EIOINTC",
236 	.irq_ack		= eiointc_ack_irq,
237 	.irq_mask		= eiointc_mask_irq,
238 	.irq_unmask		= eiointc_unmask_irq,
239 #ifdef CONFIG_SMP
240 	.irq_set_affinity	= eiointc_set_irq_affinity,
241 #endif
242 };
243 
244 static int eiointc_domain_alloc(struct irq_domain *domain, unsigned int virq,
245 				unsigned int nr_irqs, void *arg)
246 {
247 	int ret;
248 	unsigned int i, type;
249 	unsigned long hwirq = 0;
250 	struct eiointc_priv *priv = domain->host_data;
251 
252 	ret = irq_domain_translate_onecell(domain, arg, &hwirq, &type);
253 	if (ret)
254 		return ret;
255 
256 	for (i = 0; i < nr_irqs; i++) {
257 		irq_domain_set_info(domain, virq + i, hwirq + i, &eiointc_irq_chip,
258 					priv, handle_edge_irq, NULL, NULL);
259 	}
260 
261 	return 0;
262 }
263 
264 static void eiointc_domain_free(struct irq_domain *domain, unsigned int virq,
265 				unsigned int nr_irqs)
266 {
267 	int i;
268 
269 	for (i = 0; i < nr_irqs; i++) {
270 		struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
271 
272 		irq_set_handler(virq + i, NULL);
273 		irq_domain_reset_irq_data(d);
274 	}
275 }
276 
277 static const struct irq_domain_ops eiointc_domain_ops = {
278 	.translate	= irq_domain_translate_onecell,
279 	.alloc		= eiointc_domain_alloc,
280 	.free		= eiointc_domain_free,
281 };
282 
283 static void acpi_set_vec_parent(int node, struct irq_domain *parent, struct acpi_vector_group *vec_group)
284 {
285 	int i;
286 
287 	for (i = 0; i < MAX_IO_PICS; i++) {
288 		if (node == vec_group[i].node) {
289 			vec_group[i].parent = parent;
290 			return;
291 		}
292 	}
293 }
294 
295 static struct irq_domain *acpi_get_vec_parent(int node, struct acpi_vector_group *vec_group)
296 {
297 	int i;
298 
299 	for (i = 0; i < MAX_IO_PICS; i++) {
300 		if (node == vec_group[i].node)
301 			return vec_group[i].parent;
302 	}
303 	return NULL;
304 }
305 
306 static int eiointc_suspend(void)
307 {
308 	return 0;
309 }
310 
311 static void eiointc_resume(void)
312 {
313 	eiointc_router_init(0);
314 }
315 
316 static struct syscore_ops eiointc_syscore_ops = {
317 	.suspend = eiointc_suspend,
318 	.resume = eiointc_resume,
319 };
320 
321 static int __init pch_pic_parse_madt(union acpi_subtable_headers *header,
322 					const unsigned long end)
323 {
324 	struct acpi_madt_bio_pic *pchpic_entry = (struct acpi_madt_bio_pic *)header;
325 	unsigned int node = (pchpic_entry->address >> 44) & 0xf;
326 	struct irq_domain *parent = acpi_get_vec_parent(node, pch_group);
327 
328 	if (parent)
329 		return pch_pic_acpi_init(parent, pchpic_entry);
330 
331 	return 0;
332 }
333 
334 static int __init pch_msi_parse_madt(union acpi_subtable_headers *header,
335 					const unsigned long end)
336 {
337 	struct irq_domain *parent;
338 	struct acpi_madt_msi_pic *pchmsi_entry = (struct acpi_madt_msi_pic *)header;
339 	int node;
340 
341 	if (cpu_has_flatmode)
342 		node = cpu_to_node(eiointc_priv[nr_pics - 1]->node * CORES_PER_EIO_NODE);
343 	else
344 		node = eiointc_priv[nr_pics - 1]->node;
345 
346 	parent = acpi_get_vec_parent(node, msi_group);
347 
348 	if (parent)
349 		return pch_msi_acpi_init(parent, pchmsi_entry);
350 
351 	return 0;
352 }
353 
354 static int __init acpi_cascade_irqdomain_init(void)
355 {
356 	int r;
357 
358 	r = acpi_table_parse_madt(ACPI_MADT_TYPE_BIO_PIC, pch_pic_parse_madt, 0);
359 	if (r < 0)
360 		return r;
361 
362 	r = acpi_table_parse_madt(ACPI_MADT_TYPE_MSI_PIC, pch_msi_parse_madt, 1);
363 	if (r < 0)
364 		return r;
365 
366 	return 0;
367 }
368 
369 static int __init eiointc_init(struct eiointc_priv *priv, int parent_irq,
370 			       u64 node_map)
371 {
372 	int i;
373 
374 	node_map = node_map ? node_map : -1ULL;
375 	for_each_possible_cpu(i) {
376 		if (node_map & (1ULL << (cpu_to_eio_node(i)))) {
377 			node_set(cpu_to_eio_node(i), priv->node_map);
378 			cpumask_or(&priv->cpuspan_map, &priv->cpuspan_map,
379 				   cpumask_of(i));
380 		}
381 	}
382 
383 	priv->eiointc_domain = irq_domain_create_linear(priv->domain_handle,
384 							priv->vec_count,
385 							&eiointc_domain_ops,
386 							priv);
387 	if (!priv->eiointc_domain) {
388 		pr_err("loongson-extioi: cannot add IRQ domain\n");
389 		return -ENOMEM;
390 	}
391 
392 	eiointc_priv[nr_pics++] = priv;
393 	eiointc_router_init(0);
394 	irq_set_chained_handler_and_data(parent_irq, eiointc_irq_dispatch, priv);
395 
396 	if (nr_pics == 1) {
397 		register_syscore_ops(&eiointc_syscore_ops);
398 		cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_LOONGARCH_STARTING,
399 					  "irqchip/loongarch/intc:starting",
400 					  eiointc_router_init, NULL);
401 	}
402 
403 	return 0;
404 }
405 
406 int __init eiointc_acpi_init(struct irq_domain *parent,
407 				     struct acpi_madt_eio_pic *acpi_eiointc)
408 {
409 	int parent_irq, ret;
410 	struct eiointc_priv *priv;
411 	int node;
412 
413 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
414 	if (!priv)
415 		return -ENOMEM;
416 
417 	priv->domain_handle = irq_domain_alloc_named_id_fwnode("EIOPIC",
418 							       acpi_eiointc->node);
419 	if (!priv->domain_handle) {
420 		pr_err("Unable to allocate domain handle\n");
421 		goto out_free_priv;
422 	}
423 
424 	priv->vec_count = VEC_COUNT;
425 	priv->node = acpi_eiointc->node;
426 
427 	parent_irq = irq_create_mapping(parent, acpi_eiointc->cascade);
428 
429 	ret = eiointc_init(priv, parent_irq, acpi_eiointc->node_map);
430 	if (ret < 0)
431 		goto out_free_handle;
432 
433 	if (cpu_has_flatmode)
434 		node = cpu_to_node(acpi_eiointc->node * CORES_PER_EIO_NODE);
435 	else
436 		node = acpi_eiointc->node;
437 	acpi_set_vec_parent(node, priv->eiointc_domain, pch_group);
438 	acpi_set_vec_parent(node, priv->eiointc_domain, msi_group);
439 
440 	ret = acpi_cascade_irqdomain_init();
441 	if (ret < 0)
442 		goto out_free_handle;
443 
444 	return ret;
445 
446 out_free_handle:
447 	irq_domain_free_fwnode(priv->domain_handle);
448 	priv->domain_handle = NULL;
449 out_free_priv:
450 	kfree(priv);
451 
452 	return -ENOMEM;
453 }
454 
455 static int __init eiointc_of_init(struct device_node *of_node,
456 				  struct device_node *parent)
457 {
458 	int parent_irq, ret;
459 	struct eiointc_priv *priv;
460 
461 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
462 	if (!priv)
463 		return -ENOMEM;
464 
465 	parent_irq = irq_of_parse_and_map(of_node, 0);
466 	if (parent_irq <= 0) {
467 		ret = -ENODEV;
468 		goto out_free_priv;
469 	}
470 
471 	ret = irq_set_handler_data(parent_irq, priv);
472 	if (ret < 0)
473 		goto out_free_priv;
474 
475 	/*
476 	 * In particular, the number of devices supported by the LS2K0500
477 	 * extended I/O interrupt vector is 128.
478 	 */
479 	if (of_device_is_compatible(of_node, "loongson,ls2k0500-eiointc"))
480 		priv->vec_count = 128;
481 	else
482 		priv->vec_count = VEC_COUNT;
483 
484 	priv->node = 0;
485 	priv->domain_handle = of_node_to_fwnode(of_node);
486 
487 	ret = eiointc_init(priv, parent_irq, 0);
488 	if (ret < 0)
489 		goto out_free_priv;
490 
491 	return 0;
492 
493 out_free_priv:
494 	kfree(priv);
495 	return ret;
496 }
497 
498 IRQCHIP_DECLARE(loongson_ls2k0500_eiointc, "loongson,ls2k0500-eiointc", eiointc_of_init);
499 IRQCHIP_DECLARE(loongson_ls2k2000_eiointc, "loongson,ls2k2000-eiointc", eiointc_of_init);
500