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