input-linux: factor out input_linux_handle_mouse
[qemu/ar7.git] / hw / intc / xics_kvm.c
blobedbd62fd1baeaf142d36584eb2a8ac87f5dc1f08
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 typedef struct KVMXICSState {
44 XICSState parent_obj;
46 int kernel_xics_fd;
47 } KVMXICSState;
50 * ICP-KVM
52 static void icp_get_kvm_state(ICPState *ss)
54 uint64_t state;
55 struct kvm_one_reg reg = {
56 .id = KVM_REG_PPC_ICP_STATE,
57 .addr = (uintptr_t)&state,
59 int ret;
61 /* ICP for this CPU thread is not in use, exiting */
62 if (!ss->cs) {
63 return;
66 ret = kvm_vcpu_ioctl(ss->cs, KVM_GET_ONE_REG, &reg);
67 if (ret != 0) {
68 error_report("Unable to retrieve KVM interrupt controller state"
69 " for CPU %ld: %s", kvm_arch_vcpu_id(ss->cs), strerror(errno));
70 exit(1);
73 ss->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
74 ss->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
75 & KVM_REG_PPC_ICP_MFRR_MASK;
76 ss->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
77 & KVM_REG_PPC_ICP_PPRI_MASK;
80 static int icp_set_kvm_state(ICPState *ss, int version_id)
82 uint64_t state;
83 struct kvm_one_reg reg = {
84 .id = KVM_REG_PPC_ICP_STATE,
85 .addr = (uintptr_t)&state,
87 int ret;
89 /* ICP for this CPU thread is not in use, exiting */
90 if (!ss->cs) {
91 return 0;
94 state = ((uint64_t)ss->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
95 | ((uint64_t)ss->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
96 | ((uint64_t)ss->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
98 ret = kvm_vcpu_ioctl(ss->cs, KVM_SET_ONE_REG, &reg);
99 if (ret != 0) {
100 error_report("Unable to restore KVM interrupt controller state (0x%"
101 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(ss->cs),
102 strerror(errno));
103 return ret;
106 return 0;
109 static void icp_kvm_reset(DeviceState *dev)
111 ICPState *icp = ICP(dev);
113 icp->xirr = 0;
114 icp->pending_priority = 0xff;
115 icp->mfrr = 0xff;
117 /* Make all outputs as deasserted only if the CPU thread is in use */
118 if (icp->output) {
119 qemu_set_irq(icp->output, 0);
122 icp_set_kvm_state(icp, 1);
125 static void icp_kvm_class_init(ObjectClass *klass, void *data)
127 DeviceClass *dc = DEVICE_CLASS(klass);
128 ICPStateClass *icpc = ICP_CLASS(klass);
130 dc->reset = icp_kvm_reset;
131 icpc->pre_save = icp_get_kvm_state;
132 icpc->post_load = icp_set_kvm_state;
135 static const TypeInfo icp_kvm_info = {
136 .name = TYPE_KVM_ICP,
137 .parent = TYPE_ICP,
138 .instance_size = sizeof(ICPState),
139 .class_init = icp_kvm_class_init,
140 .class_size = sizeof(ICPStateClass),
144 * ICS-KVM
146 static void ics_get_kvm_state(ICSState *ics)
148 KVMXICSState *xicskvm = XICS_SPAPR_KVM(ics->xics);
149 uint64_t state;
150 struct kvm_device_attr attr = {
151 .flags = 0,
152 .group = KVM_DEV_XICS_GRP_SOURCES,
153 .addr = (uint64_t)(uintptr_t)&state,
155 int i;
157 for (i = 0; i < ics->nr_irqs; i++) {
158 ICSIRQState *irq = &ics->irqs[i];
159 int ret;
161 attr.attr = i + ics->offset;
163 ret = ioctl(xicskvm->kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr);
164 if (ret != 0) {
165 error_report("Unable to retrieve KVM interrupt controller state"
166 " for IRQ %d: %s", i + ics->offset, strerror(errno));
167 exit(1);
170 irq->server = state & KVM_XICS_DESTINATION_MASK;
171 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
172 & KVM_XICS_PRIORITY_MASK;
174 * To be consistent with the software emulation in xics.c, we
175 * split out the masked state + priority that we get from the
176 * kernel into 'current priority' (0xff if masked) and
177 * 'saved priority' (if masked, this is the priority the
178 * interrupt had before it was masked). Masking and unmasking
179 * are done with the ibm,int-off and ibm,int-on RTAS calls.
181 if (state & KVM_XICS_MASKED) {
182 irq->priority = 0xff;
183 } else {
184 irq->priority = irq->saved_priority;
187 if (state & KVM_XICS_PENDING) {
188 if (state & KVM_XICS_LEVEL_SENSITIVE) {
189 irq->status |= XICS_STATUS_ASSERTED;
190 } else {
192 * A pending edge-triggered interrupt (or MSI)
193 * must have been rejected previously when we
194 * first detected it and tried to deliver it,
195 * so mark it as pending and previously rejected
196 * for consistency with how xics.c works.
198 irq->status |= XICS_STATUS_MASKED_PENDING
199 | XICS_STATUS_REJECTED;
205 static int ics_set_kvm_state(ICSState *ics, int version_id)
207 KVMXICSState *xicskvm = XICS_SPAPR_KVM(ics->xics);
208 uint64_t state;
209 struct kvm_device_attr attr = {
210 .flags = 0,
211 .group = KVM_DEV_XICS_GRP_SOURCES,
212 .addr = (uint64_t)(uintptr_t)&state,
214 int i;
216 for (i = 0; i < ics->nr_irqs; i++) {
217 ICSIRQState *irq = &ics->irqs[i];
218 int ret;
220 attr.attr = i + ics->offset;
222 state = irq->server;
223 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
224 << KVM_XICS_PRIORITY_SHIFT;
225 if (irq->priority != irq->saved_priority) {
226 assert(irq->priority == 0xff);
227 state |= KVM_XICS_MASKED;
230 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
231 state |= KVM_XICS_LEVEL_SENSITIVE;
232 if (irq->status & XICS_STATUS_ASSERTED) {
233 state |= KVM_XICS_PENDING;
235 } else {
236 if (irq->status & XICS_STATUS_MASKED_PENDING) {
237 state |= KVM_XICS_PENDING;
241 ret = ioctl(xicskvm->kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr);
242 if (ret != 0) {
243 error_report("Unable to restore KVM interrupt controller state"
244 " for IRQs %d: %s", i + ics->offset, strerror(errno));
245 return ret;
249 return 0;
252 static void ics_kvm_set_irq(void *opaque, int srcno, int val)
254 ICSState *ics = opaque;
255 struct kvm_irq_level args;
256 int rc;
258 args.irq = srcno + ics->offset;
259 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
260 if (!val) {
261 return;
263 args.level = KVM_INTERRUPT_SET;
264 } else {
265 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
267 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
268 if (rc < 0) {
269 perror("kvm_irq_line");
273 static void ics_kvm_reset(DeviceState *dev)
275 ICSState *ics = ICS(dev);
276 int i;
277 uint8_t flags[ics->nr_irqs];
279 for (i = 0; i < ics->nr_irqs; i++) {
280 flags[i] = ics->irqs[i].flags;
283 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
285 for (i = 0; i < ics->nr_irqs; i++) {
286 ics->irqs[i].priority = 0xff;
287 ics->irqs[i].saved_priority = 0xff;
288 ics->irqs[i].flags = flags[i];
291 ics_set_kvm_state(ics, 1);
294 static void ics_kvm_realize(DeviceState *dev, Error **errp)
296 ICSState *ics = ICS(dev);
298 if (!ics->nr_irqs) {
299 error_setg(errp, "Number of interrupts needs to be greater 0");
300 return;
302 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
303 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs);
306 static void ics_kvm_class_init(ObjectClass *klass, void *data)
308 DeviceClass *dc = DEVICE_CLASS(klass);
309 ICSStateClass *icsc = ICS_CLASS(klass);
311 dc->realize = ics_kvm_realize;
312 dc->reset = ics_kvm_reset;
313 icsc->pre_save = ics_get_kvm_state;
314 icsc->post_load = ics_set_kvm_state;
317 static const TypeInfo ics_kvm_info = {
318 .name = TYPE_KVM_ICS,
319 .parent = TYPE_ICS,
320 .instance_size = sizeof(ICSState),
321 .class_init = ics_kvm_class_init,
325 * XICS-KVM
327 static void xics_kvm_cpu_setup(XICSState *xics, PowerPCCPU *cpu)
329 CPUState *cs;
330 ICPState *ss;
331 KVMXICSState *xicskvm = XICS_SPAPR_KVM(xics);
333 cs = CPU(cpu);
334 ss = &xics->ss[cs->cpu_index];
336 assert(cs->cpu_index < xics->nr_servers);
337 if (xicskvm->kernel_xics_fd == -1) {
338 abort();
342 * If we are reusing a parked vCPU fd corresponding to the CPU
343 * which was hot-removed earlier we don't have to renable
344 * KVM_CAP_IRQ_XICS capability again.
346 if (ss->cap_irq_xics_enabled) {
347 return;
350 if (xicskvm->kernel_xics_fd != -1) {
351 int ret;
353 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0,
354 xicskvm->kernel_xics_fd,
355 kvm_arch_vcpu_id(cs));
356 if (ret < 0) {
357 error_report("Unable to connect CPU%ld to kernel XICS: %s",
358 kvm_arch_vcpu_id(cs), strerror(errno));
359 exit(1);
361 ss->cap_irq_xics_enabled = true;
365 static void xics_kvm_set_nr_irqs(XICSState *xics, uint32_t nr_irqs,
366 Error **errp)
368 xics->nr_irqs = xics->ics->nr_irqs = nr_irqs;
371 static void xics_kvm_set_nr_servers(XICSState *xics, uint32_t nr_servers,
372 Error **errp)
374 int i;
376 xics->nr_servers = nr_servers;
378 xics->ss = g_malloc0(xics->nr_servers * sizeof(ICPState));
379 for (i = 0; i < xics->nr_servers; i++) {
380 char buffer[32];
381 object_initialize(&xics->ss[i], sizeof(xics->ss[i]), TYPE_KVM_ICP);
382 snprintf(buffer, sizeof(buffer), "icp[%d]", i);
383 object_property_add_child(OBJECT(xics), buffer, OBJECT(&xics->ss[i]),
384 errp);
388 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr,
389 uint32_t token,
390 uint32_t nargs, target_ulong args,
391 uint32_t nret, target_ulong rets)
393 error_report("pseries: %s must never be called for in-kernel XICS",
394 __func__);
397 static void xics_kvm_realize(DeviceState *dev, Error **errp)
399 KVMXICSState *xicskvm = XICS_SPAPR_KVM(dev);
400 XICSState *xics = XICS_COMMON(dev);
401 int i, rc;
402 Error *error = NULL;
403 struct kvm_create_device xics_create_device = {
404 .type = KVM_DEV_TYPE_XICS,
405 .flags = 0,
408 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
409 error_setg(errp,
410 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
411 goto fail;
414 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
415 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
416 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
417 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
419 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
420 if (rc < 0) {
421 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
422 goto fail;
425 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
426 if (rc < 0) {
427 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
428 goto fail;
431 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
432 if (rc < 0) {
433 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
434 goto fail;
437 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
438 if (rc < 0) {
439 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
440 goto fail;
443 /* Create the kernel ICP */
444 rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device);
445 if (rc < 0) {
446 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
447 goto fail;
450 xicskvm->kernel_xics_fd = xics_create_device.fd;
452 object_property_set_bool(OBJECT(xics->ics), true, "realized", &error);
453 if (error) {
454 error_propagate(errp, error);
455 goto fail;
458 assert(xics->nr_servers);
459 for (i = 0; i < xics->nr_servers; i++) {
460 object_property_set_bool(OBJECT(&xics->ss[i]), true, "realized",
461 &error);
462 if (error) {
463 error_propagate(errp, error);
464 goto fail;
468 kvm_kernel_irqchip = true;
469 kvm_msi_via_irqfd_allowed = true;
470 kvm_gsi_direct_mapping = true;
472 return;
474 fail:
475 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
476 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
477 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
478 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
481 static void xics_kvm_initfn(Object *obj)
483 XICSState *xics = XICS_COMMON(obj);
485 xics->ics = ICS(object_new(TYPE_KVM_ICS));
486 object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
487 xics->ics->xics = xics;
490 static void xics_kvm_class_init(ObjectClass *oc, void *data)
492 DeviceClass *dc = DEVICE_CLASS(oc);
493 XICSStateClass *xsc = XICS_COMMON_CLASS(oc);
495 dc->realize = xics_kvm_realize;
496 xsc->cpu_setup = xics_kvm_cpu_setup;
497 xsc->set_nr_irqs = xics_kvm_set_nr_irqs;
498 xsc->set_nr_servers = xics_kvm_set_nr_servers;
501 static const TypeInfo xics_spapr_kvm_info = {
502 .name = TYPE_XICS_SPAPR_KVM,
503 .parent = TYPE_XICS_COMMON,
504 .instance_size = sizeof(KVMXICSState),
505 .class_init = xics_kvm_class_init,
506 .instance_init = xics_kvm_initfn,
509 static void xics_kvm_register_types(void)
511 type_register_static(&xics_spapr_kvm_info);
512 type_register_static(&ics_kvm_info);
513 type_register_static(&icp_kvm_info);
516 type_init(xics_kvm_register_types)