xics/kvm: synchonize state before 'info pic'
[qemu.git] / hw / intc / xics_kvm.c
blob89fb20e2c55ceaa9657bc3614a81faa76e9f81d7
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics, in-kernel emulation
6 * Copyright (c) 2013 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "cpu.h"
32 #include "hw/hw.h"
33 #include "trace.h"
34 #include "sysemu/kvm.h"
35 #include "hw/ppc/spapr.h"
36 #include "hw/ppc/xics.h"
37 #include "kvm_ppc.h"
38 #include "qemu/config-file.h"
39 #include "qemu/error-report.h"
41 #include <sys/ioctl.h>
43 static int kernel_xics_fd = -1;
45 typedef struct KVMEnabledICP {
46 unsigned long vcpu_id;
47 QLIST_ENTRY(KVMEnabledICP) node;
48 } KVMEnabledICP;
50 static QLIST_HEAD(, KVMEnabledICP)
51 kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
54 * ICP-KVM
56 static void icp_get_kvm_state(ICPState *icp)
58 uint64_t state;
59 struct kvm_one_reg reg = {
60 .id = KVM_REG_PPC_ICP_STATE,
61 .addr = (uintptr_t)&state,
63 int ret;
65 /* ICP for this CPU thread is not in use, exiting */
66 if (!icp->cs) {
67 return;
70 ret = kvm_vcpu_ioctl(icp->cs, KVM_GET_ONE_REG, &reg);
71 if (ret != 0) {
72 error_report("Unable to retrieve KVM interrupt controller state"
73 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
74 exit(1);
77 icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
78 icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
79 & KVM_REG_PPC_ICP_MFRR_MASK;
80 icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
81 & KVM_REG_PPC_ICP_PPRI_MASK;
84 static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
86 icp_get_kvm_state(arg.host_ptr);
89 static void icp_synchronize_state(ICPState *icp)
91 if (icp->cs) {
92 run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
96 static int icp_set_kvm_state(ICPState *icp, int version_id)
98 uint64_t state;
99 struct kvm_one_reg reg = {
100 .id = KVM_REG_PPC_ICP_STATE,
101 .addr = (uintptr_t)&state,
103 int ret;
105 /* ICP for this CPU thread is not in use, exiting */
106 if (!icp->cs) {
107 return 0;
110 state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
111 | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
112 | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
114 ret = kvm_vcpu_ioctl(icp->cs, KVM_SET_ONE_REG, &reg);
115 if (ret != 0) {
116 error_report("Unable to restore KVM interrupt controller state (0x%"
117 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs),
118 strerror(errno));
119 return ret;
122 return 0;
125 static void icp_kvm_reset(ICPState *icp)
127 icp_set_kvm_state(icp, 1);
130 static void icp_kvm_realize(ICPState *icp, Error **errp)
132 CPUState *cs = icp->cs;
133 KVMEnabledICP *enabled_icp;
134 unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
135 int ret;
137 if (kernel_xics_fd == -1) {
138 abort();
142 * If we are reusing a parked vCPU fd corresponding to the CPU
143 * which was hot-removed earlier we don't have to renable
144 * KVM_CAP_IRQ_XICS capability again.
146 QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
147 if (enabled_icp->vcpu_id == vcpu_id) {
148 return;
152 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
153 if (ret < 0) {
154 error_setg(errp, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id,
155 strerror(errno));
156 return;
158 enabled_icp = g_malloc(sizeof(*enabled_icp));
159 enabled_icp->vcpu_id = vcpu_id;
160 QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
163 static void icp_kvm_class_init(ObjectClass *klass, void *data)
165 ICPStateClass *icpc = ICP_CLASS(klass);
167 icpc->pre_save = icp_get_kvm_state;
168 icpc->post_load = icp_set_kvm_state;
169 icpc->realize = icp_kvm_realize;
170 icpc->reset = icp_kvm_reset;
171 icpc->synchronize_state = icp_synchronize_state;
174 static const TypeInfo icp_kvm_info = {
175 .name = TYPE_KVM_ICP,
176 .parent = TYPE_ICP,
177 .instance_size = sizeof(ICPState),
178 .class_init = icp_kvm_class_init,
179 .class_size = sizeof(ICPStateClass),
183 * ICS-KVM
185 static void ics_get_kvm_state(ICSState *ics)
187 uint64_t state;
188 struct kvm_device_attr attr = {
189 .flags = 0,
190 .group = KVM_DEV_XICS_GRP_SOURCES,
191 .addr = (uint64_t)(uintptr_t)&state,
193 int i;
195 for (i = 0; i < ics->nr_irqs; i++) {
196 ICSIRQState *irq = &ics->irqs[i];
197 int ret;
199 attr.attr = i + ics->offset;
201 ret = ioctl(kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr);
202 if (ret != 0) {
203 error_report("Unable to retrieve KVM interrupt controller state"
204 " for IRQ %d: %s", i + ics->offset, strerror(errno));
205 exit(1);
208 irq->server = state & KVM_XICS_DESTINATION_MASK;
209 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
210 & KVM_XICS_PRIORITY_MASK;
212 * To be consistent with the software emulation in xics.c, we
213 * split out the masked state + priority that we get from the
214 * kernel into 'current priority' (0xff if masked) and
215 * 'saved priority' (if masked, this is the priority the
216 * interrupt had before it was masked). Masking and unmasking
217 * are done with the ibm,int-off and ibm,int-on RTAS calls.
219 if (state & KVM_XICS_MASKED) {
220 irq->priority = 0xff;
221 } else {
222 irq->priority = irq->saved_priority;
225 irq->status = 0;
226 if (state & KVM_XICS_PENDING) {
227 if (state & KVM_XICS_LEVEL_SENSITIVE) {
228 irq->status |= XICS_STATUS_ASSERTED;
229 } else {
231 * A pending edge-triggered interrupt (or MSI)
232 * must have been rejected previously when we
233 * first detected it and tried to deliver it,
234 * so mark it as pending and previously rejected
235 * for consistency with how xics.c works.
237 irq->status |= XICS_STATUS_MASKED_PENDING
238 | XICS_STATUS_REJECTED;
241 if (state & KVM_XICS_PRESENTED) {
242 irq->status |= XICS_STATUS_PRESENTED;
244 if (state & KVM_XICS_QUEUED) {
245 irq->status |= XICS_STATUS_QUEUED;
250 static void ics_synchronize_state(ICSState *ics)
252 ics_get_kvm_state(ics);
255 static int ics_set_kvm_state(ICSState *ics, int version_id)
257 uint64_t state;
258 struct kvm_device_attr attr = {
259 .flags = 0,
260 .group = KVM_DEV_XICS_GRP_SOURCES,
261 .addr = (uint64_t)(uintptr_t)&state,
263 int i;
265 for (i = 0; i < ics->nr_irqs; i++) {
266 ICSIRQState *irq = &ics->irqs[i];
267 int ret;
269 attr.attr = i + ics->offset;
271 state = irq->server;
272 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
273 << KVM_XICS_PRIORITY_SHIFT;
274 if (irq->priority != irq->saved_priority) {
275 assert(irq->priority == 0xff);
276 state |= KVM_XICS_MASKED;
279 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
280 state |= KVM_XICS_LEVEL_SENSITIVE;
281 if (irq->status & XICS_STATUS_ASSERTED) {
282 state |= KVM_XICS_PENDING;
284 } else {
285 if (irq->status & XICS_STATUS_MASKED_PENDING) {
286 state |= KVM_XICS_PENDING;
289 if (irq->status & XICS_STATUS_PRESENTED) {
290 state |= KVM_XICS_PRESENTED;
292 if (irq->status & XICS_STATUS_QUEUED) {
293 state |= KVM_XICS_QUEUED;
296 ret = ioctl(kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr);
297 if (ret != 0) {
298 error_report("Unable to restore KVM interrupt controller state"
299 " for IRQs %d: %s", i + ics->offset, strerror(errno));
300 return ret;
304 return 0;
307 static void ics_kvm_set_irq(void *opaque, int srcno, int val)
309 ICSState *ics = opaque;
310 struct kvm_irq_level args;
311 int rc;
313 args.irq = srcno + ics->offset;
314 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
315 if (!val) {
316 return;
318 args.level = KVM_INTERRUPT_SET;
319 } else {
320 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
322 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
323 if (rc < 0) {
324 perror("kvm_irq_line");
328 static void ics_kvm_reset(void *dev)
330 ICSState *ics = ICS_SIMPLE(dev);
331 int i;
332 uint8_t flags[ics->nr_irqs];
334 for (i = 0; i < ics->nr_irqs; i++) {
335 flags[i] = ics->irqs[i].flags;
338 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
340 for (i = 0; i < ics->nr_irqs; i++) {
341 ics->irqs[i].priority = 0xff;
342 ics->irqs[i].saved_priority = 0xff;
343 ics->irqs[i].flags = flags[i];
346 ics_set_kvm_state(ics, 1);
349 static void ics_kvm_realize(ICSState *ics, Error **errp)
351 if (!ics->nr_irqs) {
352 error_setg(errp, "Number of interrupts needs to be greater 0");
353 return;
355 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
356 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs);
358 qemu_register_reset(ics_kvm_reset, ics);
361 static void ics_kvm_class_init(ObjectClass *klass, void *data)
363 ICSStateClass *icsc = ICS_BASE_CLASS(klass);
365 icsc->realize = ics_kvm_realize;
366 icsc->pre_save = ics_get_kvm_state;
367 icsc->post_load = ics_set_kvm_state;
368 icsc->synchronize_state = ics_synchronize_state;
371 static const TypeInfo ics_kvm_info = {
372 .name = TYPE_ICS_KVM,
373 .parent = TYPE_ICS_SIMPLE,
374 .instance_size = sizeof(ICSState),
375 .class_init = ics_kvm_class_init,
379 * XICS-KVM
382 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr,
383 uint32_t token,
384 uint32_t nargs, target_ulong args,
385 uint32_t nret, target_ulong rets)
387 error_report("pseries: %s must never be called for in-kernel XICS",
388 __func__);
391 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp)
393 int rc;
394 struct kvm_create_device xics_create_device = {
395 .type = KVM_DEV_TYPE_XICS,
396 .flags = 0,
399 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
400 error_setg(errp,
401 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
402 goto fail;
405 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
406 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
407 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
408 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
410 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
411 if (rc < 0) {
412 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
413 goto fail;
416 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
417 if (rc < 0) {
418 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
419 goto fail;
422 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
423 if (rc < 0) {
424 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
425 goto fail;
428 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
429 if (rc < 0) {
430 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
431 goto fail;
434 /* Create the kernel ICP */
435 rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device);
436 if (rc < 0) {
437 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
438 goto fail;
441 kernel_xics_fd = xics_create_device.fd;
443 kvm_kernel_irqchip = true;
444 kvm_msi_via_irqfd_allowed = true;
445 kvm_gsi_direct_mapping = true;
447 return rc;
449 fail:
450 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
451 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
452 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
453 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
454 return -1;
457 static void xics_kvm_register_types(void)
459 type_register_static(&ics_kvm_info);
460 type_register_static(&icp_kvm_info);
463 type_init(xics_kvm_register_types)