1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2011 IBM Corporation.
6 #include <linux/types.h>
7 #include <linux/kernel.h>
10 #include <linux/interrupt.h>
11 #include <linux/init.h>
12 #include <linux/cpu.h>
14 #include <linux/spinlock.h>
15 #include <linux/module.h>
21 #include <asm/errno.h>
23 #include <asm/kvm_ppc.h>
24 #include <asm/dbell.h>
45 static struct icp_ipl __iomem
*icp_native_regs
[NR_CPUS
];
47 static inline unsigned int icp_native_get_xirr(void)
49 int cpu
= smp_processor_id();
52 /* Handled an interrupt latched by KVM */
53 xirr
= kvmppc_get_xics_latch();
57 return in_be32(&icp_native_regs
[cpu
]->xirr
.word
);
60 static inline void icp_native_set_xirr(unsigned int value
)
62 int cpu
= smp_processor_id();
64 out_be32(&icp_native_regs
[cpu
]->xirr
.word
, value
);
67 static inline void icp_native_set_cppr(u8 value
)
69 int cpu
= smp_processor_id();
71 out_8(&icp_native_regs
[cpu
]->xirr
.bytes
[0], value
);
74 static inline void icp_native_set_qirr(int n_cpu
, u8 value
)
76 out_8(&icp_native_regs
[n_cpu
]->qirr
.bytes
[0], value
);
79 static void icp_native_set_cpu_priority(unsigned char cppr
)
81 xics_set_base_cppr(cppr
);
82 icp_native_set_cppr(cppr
);
86 void icp_native_eoi(struct irq_data
*d
)
88 unsigned int hw_irq
= (unsigned int)irqd_to_hwirq(d
);
91 icp_native_set_xirr((xics_pop_cppr() << 24) | hw_irq
);
94 static void icp_native_teardown_cpu(void)
96 int cpu
= smp_processor_id();
98 /* Clear any pending IPI */
99 icp_native_set_qirr(cpu
, 0xff);
102 static void icp_native_flush_ipi(void)
104 /* We take the ipi irq but and never return so we
105 * need to EOI the IPI, but want to leave our priority 0
107 * should we check all the other interrupts too?
108 * should we be flagging idle loop instead?
109 * or creating some task to be scheduled?
112 icp_native_set_xirr((0x00 << 24) | XICS_IPI
);
115 static unsigned int icp_native_get_irq(void)
117 unsigned int xirr
= icp_native_get_xirr();
118 unsigned int vec
= xirr
& 0x00ffffff;
121 if (vec
== XICS_IRQ_SPURIOUS
)
124 irq
= irq_find_mapping(xics_host
, vec
);
130 /* We don't have a linux mapping, so have rtas mask it. */
131 xics_mask_unknown_vec(vec
);
133 /* We might learn about it later, so EOI it */
134 icp_native_set_xirr(xirr
);
141 static void icp_native_cause_ipi(int cpu
)
143 kvmppc_set_host_ipi(cpu
);
144 icp_native_set_qirr(cpu
, IPI_PRIORITY
);
147 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
148 void icp_native_cause_ipi_rm(int cpu
)
151 * Currently not used to send IPIs to another CPU
152 * on the same core. Only caller is KVM real mode.
153 * Need the physical address of the XICS to be
154 * previously saved in kvm_hstate in the paca.
156 void __iomem
*xics_phys
;
159 * Just like the cause_ipi functions, it is required to
160 * include a full barrier before causing the IPI.
162 xics_phys
= paca_ptrs
[cpu
]->kvm_hstate
.xics_phys
;
164 __raw_rm_writeb(IPI_PRIORITY
, xics_phys
+ XICS_MFRR
);
169 * Called when an interrupt is received on an off-line CPU to
170 * clear the interrupt, so that the CPU can go back to nap mode.
172 void icp_native_flush_interrupt(void)
174 unsigned int xirr
= icp_native_get_xirr();
175 unsigned int vec
= xirr
& 0x00ffffff;
177 if (vec
== XICS_IRQ_SPURIOUS
)
179 if (vec
== XICS_IPI
) {
180 /* Clear pending IPI */
181 int cpu
= smp_processor_id();
182 kvmppc_clear_host_ipi(cpu
);
183 icp_native_set_qirr(cpu
, 0xff);
185 pr_err("XICS: hw interrupt 0x%x to offline cpu, disabling\n",
187 xics_mask_unknown_vec(vec
);
189 /* EOI the interrupt */
190 icp_native_set_xirr(xirr
);
193 void xics_wake_cpu(int cpu
)
195 icp_native_set_qirr(cpu
, IPI_PRIORITY
);
197 EXPORT_SYMBOL_GPL(xics_wake_cpu
);
199 static irqreturn_t
icp_native_ipi_action(int irq
, void *dev_id
)
201 int cpu
= smp_processor_id();
203 kvmppc_clear_host_ipi(cpu
);
204 icp_native_set_qirr(cpu
, 0xff);
206 return smp_ipi_demux();
209 #endif /* CONFIG_SMP */
211 static int __init
icp_native_map_one_cpu(int hw_id
, unsigned long addr
,
217 /* This may look gross but it's good enough for now, we don't quite
218 * have a hard -> linux processor id matching.
220 for_each_possible_cpu(i
) {
223 if (hw_id
== get_hard_smp_processor_id(i
)) {
229 /* Fail, skip that CPU. Don't print, it's normal, some XICS come up
230 * with way more entries in there than you have CPUs
235 rname
= kasprintf(GFP_KERNEL
, "CPU %d [0x%x] Interrupt Presentation",
238 if (!request_mem_region(addr
, size
, rname
)) {
239 pr_warn("icp_native: Could not reserve ICP MMIO for CPU %d, interrupt server #0x%x\n",
244 icp_native_regs
[cpu
] = ioremap(addr
, size
);
245 kvmppc_set_xics_phys(cpu
, addr
);
246 if (!icp_native_regs
[cpu
]) {
247 pr_warn("icp_native: Failed ioremap for CPU %d, interrupt server #0x%x, addr %#lx\n",
249 release_mem_region(addr
, size
);
255 static int __init
icp_native_init_one_node(struct device_node
*np
,
264 /* This code does the theorically broken assumption that the interrupt
265 * server numbers are the same as the hard CPU numbers.
266 * This happens to be the case so far but we are playing with fire...
267 * should be fixed one of these days. -BenH.
269 ireg
= of_get_property(np
, "ibm,interrupt-server-ranges", &ilen
);
271 /* Do that ever happen ? we'll know soon enough... but even good'old
272 * f80 does have that property ..
274 WARN_ON((ireg
== NULL
) || (ilen
!= 2*sizeof(u32
)));
277 *indx
= of_read_number(ireg
, 1);
278 if (ilen
>= 2*sizeof(u32
))
279 num_servers
= of_read_number(ireg
+ 1, 1);
282 ireg
= of_get_property(np
, "reg", &ilen
);
284 pr_err("icp_native: Can't find interrupt reg property");
288 reg_tuple_size
= (of_n_addr_cells(np
) + of_n_size_cells(np
)) * 4;
289 if (((ilen
% reg_tuple_size
) != 0)
290 || (num_servers
&& (num_servers
!= (ilen
/ reg_tuple_size
)))) {
291 pr_err("icp_native: ICP reg len (%d) != num servers (%d)",
292 ilen
/ reg_tuple_size
, num_servers
);
296 for (i
= 0; i
< (ilen
/ reg_tuple_size
); i
++) {
300 err
= of_address_to_resource(np
, i
, &r
);
302 pr_err("icp_native: Could not translate ICP MMIO"
303 " for interrupt server 0x%x (%d)\n", *indx
, err
);
307 if (icp_native_map_one_cpu(*indx
, r
.start
, resource_size(&r
)))
315 static const struct icp_ops icp_native_ops
= {
316 .get_irq
= icp_native_get_irq
,
317 .eoi
= icp_native_eoi
,
318 .set_priority
= icp_native_set_cpu_priority
,
319 .teardown_cpu
= icp_native_teardown_cpu
,
320 .flush_ipi
= icp_native_flush_ipi
,
322 .ipi_action
= icp_native_ipi_action
,
323 .cause_ipi
= icp_native_cause_ipi
,
327 int __init
icp_native_init(void)
329 struct device_node
*np
;
333 for_each_compatible_node(np
, NULL
, "ibm,ppc-xicp")
334 if (icp_native_init_one_node(np
, &indx
) == 0)
337 for_each_node_by_type(np
,
338 "PowerPC-External-Interrupt-Presentation") {
339 if (icp_native_init_one_node(np
, &indx
) == 0)
347 icp_ops
= &icp_native_ops
;