1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2011 IBM Corporation.
6 #include <linux/types.h>
7 #include <linux/kernel.h>
9 #include <linux/irqdomain.h>
10 #include <linux/smp.h>
11 #include <linux/interrupt.h>
12 #include <linux/init.h>
13 #include <linux/cpu.h>
15 #include <linux/of_address.h>
16 #include <linux/spinlock.h>
17 #include <linux/module.h>
22 #include <asm/errno.h>
24 #include <asm/kvm_ppc.h>
25 #include <asm/dbell.h>
46 static struct icp_ipl __iomem *icp_native_regs[NR_CPUS];
48 static inline unsigned int icp_native_get_xirr(void)
50 int cpu = smp_processor_id();
53 /* Handled an interrupt latched by KVM */
54 xirr = kvmppc_get_xics_latch();
58 return in_be32(&icp_native_regs[cpu]->xirr.word);
61 static inline void icp_native_set_xirr(unsigned int value)
63 int cpu = smp_processor_id();
65 out_be32(&icp_native_regs[cpu]->xirr.word, value);
68 static inline void icp_native_set_cppr(u8 value)
70 int cpu = smp_processor_id();
72 out_8(&icp_native_regs[cpu]->xirr.bytes[0], value);
75 static inline void icp_native_set_qirr(int n_cpu, u8 value)
77 out_8(&icp_native_regs[n_cpu]->qirr.bytes[0], value);
80 static void icp_native_set_cpu_priority(unsigned char cppr)
82 xics_set_base_cppr(cppr);
83 icp_native_set_cppr(cppr);
87 void icp_native_eoi(struct irq_data *d)
89 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
92 icp_native_set_xirr((xics_pop_cppr() << 24) | hw_irq);
95 static void icp_native_teardown_cpu(void)
97 int cpu = smp_processor_id();
99 /* Clear any pending IPI */
100 icp_native_set_qirr(cpu, 0xff);
103 static void icp_native_flush_ipi(void)
105 /* We take the ipi irq but and never return so we
106 * need to EOI the IPI, but want to leave our priority 0
108 * should we check all the other interrupts too?
109 * should we be flagging idle loop instead?
110 * or creating some task to be scheduled?
113 icp_native_set_xirr((0x00 << 24) | XICS_IPI);
116 static unsigned int icp_native_get_irq(void)
118 unsigned int xirr = icp_native_get_xirr();
119 unsigned int vec = xirr & 0x00ffffff;
122 if (vec == XICS_IRQ_SPURIOUS)
125 irq = irq_find_mapping(xics_host, vec);
131 /* We don't have a linux mapping, so have rtas mask it. */
132 xics_mask_unknown_vec(vec);
134 /* We might learn about it later, so EOI it */
135 icp_native_set_xirr(xirr);
142 static void icp_native_cause_ipi(int cpu)
144 kvmppc_set_host_ipi(cpu);
145 icp_native_set_qirr(cpu, IPI_PRIORITY);
148 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
149 void icp_native_cause_ipi_rm(int cpu)
152 * Currently not used to send IPIs to another CPU
153 * on the same core. Only caller is KVM real mode.
154 * Need the physical address of the XICS to be
155 * previously saved in kvm_hstate in the paca.
157 void __iomem *xics_phys;
160 * Just like the cause_ipi functions, it is required to
161 * include a full barrier before causing the IPI.
163 xics_phys = paca_ptrs[cpu]->kvm_hstate.xics_phys;
165 __raw_rm_writeb(IPI_PRIORITY, xics_phys + XICS_MFRR);
170 * Called when an interrupt is received on an off-line CPU to
171 * clear the interrupt, so that the CPU can go back to nap mode.
173 void icp_native_flush_interrupt(void)
175 unsigned int xirr = icp_native_get_xirr();
176 unsigned int vec = xirr & 0x00ffffff;
178 if (vec == XICS_IRQ_SPURIOUS)
180 if (vec == XICS_IPI) {
181 /* Clear pending IPI */
182 int cpu = smp_processor_id();
183 kvmppc_clear_host_ipi(cpu);
184 icp_native_set_qirr(cpu, 0xff);
186 pr_err("XICS: hw interrupt 0x%x to offline cpu, disabling\n",
188 xics_mask_unknown_vec(vec);
190 /* EOI the interrupt */
191 icp_native_set_xirr(xirr);
194 void xics_wake_cpu(int cpu)
196 icp_native_set_qirr(cpu, IPI_PRIORITY);
198 EXPORT_SYMBOL_GPL(xics_wake_cpu);
200 static irqreturn_t icp_native_ipi_action(int irq, void *dev_id)
202 int cpu = smp_processor_id();
204 kvmppc_clear_host_ipi(cpu);
205 icp_native_set_qirr(cpu, 0xff);
207 return smp_ipi_demux();
210 #endif /* CONFIG_SMP */
212 static int __init icp_native_map_one_cpu(int hw_id, unsigned long addr,
218 /* This may look gross but it's good enough for now, we don't quite
219 * have a hard -> linux processor id matching.
221 for_each_possible_cpu(i) {
224 if (hw_id == get_hard_smp_processor_id(i)) {
230 /* Fail, skip that CPU. Don't print, it's normal, some XICS come up
231 * with way more entries in there than you have CPUs
236 rname = kasprintf(GFP_KERNEL, "CPU %d [0x%x] Interrupt Presentation",
239 if (!request_mem_region(addr, size, rname)) {
240 pr_warn("icp_native: Could not reserve ICP MMIO for CPU %d, interrupt server #0x%x\n",
245 icp_native_regs[cpu] = ioremap(addr, size);
246 kvmppc_set_xics_phys(cpu, addr);
247 if (!icp_native_regs[cpu]) {
248 pr_warn("icp_native: Failed ioremap for CPU %d, interrupt server #0x%x, addr %#lx\n",
250 release_mem_region(addr, size);
256 static int __init icp_native_init_one_node(struct device_node *np,
265 /* This code does the theorically broken assumption that the interrupt
266 * server numbers are the same as the hard CPU numbers.
267 * This happens to be the case so far but we are playing with fire...
268 * should be fixed one of these days. -BenH.
270 ireg = of_get_property(np, "ibm,interrupt-server-ranges", &ilen);
272 /* Do that ever happen ? we'll know soon enough... but even good'old
273 * f80 does have that property ..
275 WARN_ON((ireg == NULL) || (ilen != 2*sizeof(u32)));
278 *indx = of_read_number(ireg, 1);
279 if (ilen >= 2*sizeof(u32))
280 num_servers = of_read_number(ireg + 1, 1);
283 ireg = of_get_property(np, "reg", &ilen);
285 pr_err("icp_native: Can't find interrupt reg property");
289 reg_tuple_size = (of_n_addr_cells(np) + of_n_size_cells(np)) * 4;
290 if (((ilen % reg_tuple_size) != 0)
291 || (num_servers && (num_servers != (ilen / reg_tuple_size)))) {
292 pr_err("icp_native: ICP reg len (%d) != num servers (%d)",
293 ilen / reg_tuple_size, num_servers);
297 for (i = 0; i < (ilen / reg_tuple_size); i++) {
301 err = of_address_to_resource(np, i, &r);
303 pr_err("icp_native: Could not translate ICP MMIO"
304 " for interrupt server 0x%x (%d)\n", *indx, err);
308 if (icp_native_map_one_cpu(*indx, r.start, resource_size(&r)))
316 static const struct icp_ops icp_native_ops = {
317 .get_irq = icp_native_get_irq,
318 .eoi = icp_native_eoi,
319 .set_priority = icp_native_set_cpu_priority,
320 .teardown_cpu = icp_native_teardown_cpu,
321 .flush_ipi = icp_native_flush_ipi,
323 .ipi_action = icp_native_ipi_action,
324 .cause_ipi = icp_native_cause_ipi,
328 int __init icp_native_init(void)
330 struct device_node *np;
334 for_each_compatible_node(np, NULL, "ibm,ppc-xicp")
335 if (icp_native_init_one_node(np, &indx) == 0)
338 for_each_node_by_type(np,
339 "PowerPC-External-Interrupt-Presentation") {
340 if (icp_native_init_one_node(np, &indx) == 0)
348 icp_ops = &icp_native_ops;