1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ICS backend for OPAL managed interrupts.
5 * Copyright 2011 IBM Corp.
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/irq.h>
13 #include <linux/smp.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/cpu.h>
18 #include <linux/spinlock.h>
19 #include <linux/msi.h>
23 #include <asm/machdep.h>
25 #include <asm/errno.h>
28 #include <asm/firmware.h>
30 static int ics_opal_mangle_server(int server
)
36 static int ics_opal_unmangle_server(int server
)
42 static void ics_opal_unmask_irq(struct irq_data
*d
)
44 unsigned int hw_irq
= (unsigned int)irqd_to_hwirq(d
);
48 pr_devel("ics-hal: unmask virq %d [hw 0x%x]\n", d
->irq
, hw_irq
);
50 if (hw_irq
== XICS_IPI
|| hw_irq
== XICS_IRQ_SPURIOUS
)
53 server
= xics_get_irq_server(d
->irq
, irq_data_get_affinity_mask(d
), 0);
54 server
= ics_opal_mangle_server(server
);
56 rc
= opal_set_xive(hw_irq
, server
, DEFAULT_PRIORITY
);
57 if (rc
!= OPAL_SUCCESS
)
58 pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
60 __func__
, d
->irq
, hw_irq
, server
, rc
);
63 static unsigned int ics_opal_startup(struct irq_data
*d
)
67 * The generic MSI code returns with the interrupt disabled on the
68 * card, using the MSI mask bits. Firmware doesn't appear to unmask
69 * at that level, so we do it here by hand.
71 if (irq_data_get_msi_desc(d
))
72 pci_msi_unmask_irq(d
);
76 ics_opal_unmask_irq(d
);
80 static void ics_opal_mask_real_irq(unsigned int hw_irq
)
82 int server
= ics_opal_mangle_server(xics_default_server
);
85 if (hw_irq
== XICS_IPI
)
88 /* Have to set XIVE to 0xff to be able to remove a slot */
89 rc
= opal_set_xive(hw_irq
, server
, 0xff);
90 if (rc
!= OPAL_SUCCESS
)
91 pr_err("%s: opal_set_xive(0xff) irq=%u returned %lld\n",
92 __func__
, hw_irq
, rc
);
95 static void ics_opal_mask_irq(struct irq_data
*d
)
97 unsigned int hw_irq
= (unsigned int)irqd_to_hwirq(d
);
99 pr_devel("ics-hal: mask virq %d [hw 0x%x]\n", d
->irq
, hw_irq
);
101 if (hw_irq
== XICS_IPI
|| hw_irq
== XICS_IRQ_SPURIOUS
)
103 ics_opal_mask_real_irq(hw_irq
);
106 static int ics_opal_set_affinity(struct irq_data
*d
,
107 const struct cpumask
*cpumask
,
110 unsigned int hw_irq
= (unsigned int)irqd_to_hwirq(d
);
117 if (hw_irq
== XICS_IPI
|| hw_irq
== XICS_IRQ_SPURIOUS
)
120 rc
= opal_get_xive(hw_irq
, &oserver
, &priority
);
121 if (rc
!= OPAL_SUCCESS
) {
122 pr_err("%s: opal_get_xive(irq=%d [hw 0x%x]) error %lld\n",
123 __func__
, d
->irq
, hw_irq
, rc
);
126 server
= be16_to_cpu(oserver
);
128 wanted_server
= xics_get_irq_server(d
->irq
, cpumask
, 1);
129 if (wanted_server
< 0) {
130 pr_warn("%s: No online cpus in the mask %*pb for irq %d\n",
131 __func__
, cpumask_pr_args(cpumask
), d
->irq
);
134 server
= ics_opal_mangle_server(wanted_server
);
136 pr_devel("ics-hal: set-affinity irq %d [hw 0x%x] server: 0x%x/0x%x\n",
137 d
->irq
, hw_irq
, wanted_server
, server
);
139 rc
= opal_set_xive(hw_irq
, server
, priority
);
140 if (rc
!= OPAL_SUCCESS
) {
141 pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
143 __func__
, d
->irq
, hw_irq
, server
, rc
);
146 return IRQ_SET_MASK_OK
;
149 static struct irq_chip ics_opal_irq_chip
= {
151 .irq_startup
= ics_opal_startup
,
152 .irq_mask
= ics_opal_mask_irq
,
153 .irq_unmask
= ics_opal_unmask_irq
,
154 .irq_eoi
= NULL
, /* Patched at init time */
155 .irq_set_affinity
= ics_opal_set_affinity
,
156 .irq_set_type
= xics_set_irq_type
,
157 .irq_retrigger
= xics_retrigger
,
160 static int ics_opal_map(struct ics
*ics
, unsigned int virq
);
161 static void ics_opal_mask_unknown(struct ics
*ics
, unsigned long vec
);
162 static long ics_opal_get_server(struct ics
*ics
, unsigned long vec
);
164 static int ics_opal_host_match(struct ics
*ics
, struct device_node
*node
)
169 /* Only one global & state struct ics */
170 static struct ics ics_hal
= {
172 .mask_unknown
= ics_opal_mask_unknown
,
173 .get_server
= ics_opal_get_server
,
174 .host_match
= ics_opal_host_match
,
177 static int ics_opal_map(struct ics
*ics
, unsigned int virq
)
179 unsigned int hw_irq
= (unsigned int)virq_to_hw(virq
);
184 if (WARN_ON(hw_irq
== XICS_IPI
|| hw_irq
== XICS_IRQ_SPURIOUS
))
187 /* Check if HAL knows about this interrupt */
188 rc
= opal_get_xive(hw_irq
, &server
, &priority
);
189 if (rc
!= OPAL_SUCCESS
)
192 irq_set_chip_and_handler(virq
, &ics_opal_irq_chip
, handle_fasteoi_irq
);
193 irq_set_chip_data(virq
, &ics_hal
);
198 static void ics_opal_mask_unknown(struct ics
*ics
, unsigned long vec
)
204 /* Check if HAL knows about this interrupt */
205 rc
= opal_get_xive(vec
, &server
, &priority
);
206 if (rc
!= OPAL_SUCCESS
)
209 ics_opal_mask_real_irq(vec
);
212 static long ics_opal_get_server(struct ics
*ics
, unsigned long vec
)
218 /* Check if HAL knows about this interrupt */
219 rc
= opal_get_xive(vec
, &server
, &priority
);
220 if (rc
!= OPAL_SUCCESS
)
222 return ics_opal_unmangle_server(be16_to_cpu(server
));
225 int __init
ics_opal_init(void)
227 if (!firmware_has_feature(FW_FEATURE_OPAL
))
230 /* We need to patch our irq chip's EOI to point to the
233 ics_opal_irq_chip
.irq_eoi
= icp_ops
->eoi
;
235 /* Register ourselves */
236 xics_register_ics(&ics_hal
);
238 pr_info("ICS OPAL backend registered\n");