spapr: Add ibm,processor-radix-AP-encodings to the device tree
[qemu/ar7.git] / hw / intc / xics_kvm.c
blob42e0e0ef84840758be5c281a31af6ecd0062aee1
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;
46 * ICP-KVM
48 static void icp_get_kvm_state(ICPState *icp)
50 uint64_t state;
51 struct kvm_one_reg reg = {
52 .id = KVM_REG_PPC_ICP_STATE,
53 .addr = (uintptr_t)&state,
55 int ret;
57 /* ICP for this CPU thread is not in use, exiting */
58 if (!icp->cs) {
59 return;
62 ret = kvm_vcpu_ioctl(icp->cs, KVM_GET_ONE_REG, &reg);
63 if (ret != 0) {
64 error_report("Unable to retrieve KVM interrupt controller state"
65 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
66 exit(1);
69 icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
70 icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
71 & KVM_REG_PPC_ICP_MFRR_MASK;
72 icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
73 & KVM_REG_PPC_ICP_PPRI_MASK;
76 static int icp_set_kvm_state(ICPState *icp, int version_id)
78 uint64_t state;
79 struct kvm_one_reg reg = {
80 .id = KVM_REG_PPC_ICP_STATE,
81 .addr = (uintptr_t)&state,
83 int ret;
85 /* ICP for this CPU thread is not in use, exiting */
86 if (!icp->cs) {
87 return 0;
90 state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
91 | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
92 | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
94 ret = kvm_vcpu_ioctl(icp->cs, KVM_SET_ONE_REG, &reg);
95 if (ret != 0) {
96 error_report("Unable to restore KVM interrupt controller state (0x%"
97 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs),
98 strerror(errno));
99 return ret;
102 return 0;
105 static void icp_kvm_reset(void *dev)
107 ICPState *icp = ICP(dev);
109 icp->xirr = 0;
110 icp->pending_priority = 0xff;
111 icp->mfrr = 0xff;
113 /* Make all outputs as deasserted only if the CPU thread is in use */
114 if (icp->output) {
115 qemu_set_irq(icp->output, 0);
118 icp_set_kvm_state(icp, 1);
121 static void icp_kvm_cpu_setup(ICPState *icp, PowerPCCPU *cpu)
123 CPUState *cs = CPU(cpu);
124 int ret;
126 if (kernel_xics_fd == -1) {
127 abort();
131 * If we are reusing a parked vCPU fd corresponding to the CPU
132 * which was hot-removed earlier we don't have to renable
133 * KVM_CAP_IRQ_XICS capability again.
135 if (icp->cap_irq_xics_enabled) {
136 return;
139 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd,
140 kvm_arch_vcpu_id(cs));
141 if (ret < 0) {
142 error_report("Unable to connect CPU%ld to kernel XICS: %s",
143 kvm_arch_vcpu_id(cs), strerror(errno));
144 exit(1);
146 icp->cap_irq_xics_enabled = true;
149 static void icp_kvm_realize(DeviceState *dev, Error **errp)
151 qemu_register_reset(icp_kvm_reset, dev);
154 static void icp_kvm_class_init(ObjectClass *klass, void *data)
156 DeviceClass *dc = DEVICE_CLASS(klass);
157 ICPStateClass *icpc = ICP_CLASS(klass);
159 dc->realize = icp_kvm_realize;
160 icpc->pre_save = icp_get_kvm_state;
161 icpc->post_load = icp_set_kvm_state;
162 icpc->cpu_setup = icp_kvm_cpu_setup;
165 static const TypeInfo icp_kvm_info = {
166 .name = TYPE_KVM_ICP,
167 .parent = TYPE_ICP,
168 .instance_size = sizeof(ICPState),
169 .class_init = icp_kvm_class_init,
170 .class_size = sizeof(ICPStateClass),
174 * ICS-KVM
176 static void ics_get_kvm_state(ICSState *ics)
178 uint64_t state;
179 struct kvm_device_attr attr = {
180 .flags = 0,
181 .group = KVM_DEV_XICS_GRP_SOURCES,
182 .addr = (uint64_t)(uintptr_t)&state,
184 int i;
186 for (i = 0; i < ics->nr_irqs; i++) {
187 ICSIRQState *irq = &ics->irqs[i];
188 int ret;
190 attr.attr = i + ics->offset;
192 ret = ioctl(kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr);
193 if (ret != 0) {
194 error_report("Unable to retrieve KVM interrupt controller state"
195 " for IRQ %d: %s", i + ics->offset, strerror(errno));
196 exit(1);
199 irq->server = state & KVM_XICS_DESTINATION_MASK;
200 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
201 & KVM_XICS_PRIORITY_MASK;
203 * To be consistent with the software emulation in xics.c, we
204 * split out the masked state + priority that we get from the
205 * kernel into 'current priority' (0xff if masked) and
206 * 'saved priority' (if masked, this is the priority the
207 * interrupt had before it was masked). Masking and unmasking
208 * are done with the ibm,int-off and ibm,int-on RTAS calls.
210 if (state & KVM_XICS_MASKED) {
211 irq->priority = 0xff;
212 } else {
213 irq->priority = irq->saved_priority;
216 if (state & KVM_XICS_PENDING) {
217 if (state & KVM_XICS_LEVEL_SENSITIVE) {
218 irq->status |= XICS_STATUS_ASSERTED;
219 } else {
221 * A pending edge-triggered interrupt (or MSI)
222 * must have been rejected previously when we
223 * first detected it and tried to deliver it,
224 * so mark it as pending and previously rejected
225 * for consistency with how xics.c works.
227 irq->status |= XICS_STATUS_MASKED_PENDING
228 | XICS_STATUS_REJECTED;
234 static int ics_set_kvm_state(ICSState *ics, int version_id)
236 uint64_t state;
237 struct kvm_device_attr attr = {
238 .flags = 0,
239 .group = KVM_DEV_XICS_GRP_SOURCES,
240 .addr = (uint64_t)(uintptr_t)&state,
242 int i;
244 for (i = 0; i < ics->nr_irqs; i++) {
245 ICSIRQState *irq = &ics->irqs[i];
246 int ret;
248 attr.attr = i + ics->offset;
250 state = irq->server;
251 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
252 << KVM_XICS_PRIORITY_SHIFT;
253 if (irq->priority != irq->saved_priority) {
254 assert(irq->priority == 0xff);
255 state |= KVM_XICS_MASKED;
258 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
259 state |= KVM_XICS_LEVEL_SENSITIVE;
260 if (irq->status & XICS_STATUS_ASSERTED) {
261 state |= KVM_XICS_PENDING;
263 } else {
264 if (irq->status & XICS_STATUS_MASKED_PENDING) {
265 state |= KVM_XICS_PENDING;
269 ret = ioctl(kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr);
270 if (ret != 0) {
271 error_report("Unable to restore KVM interrupt controller state"
272 " for IRQs %d: %s", i + ics->offset, strerror(errno));
273 return ret;
277 return 0;
280 static void ics_kvm_set_irq(void *opaque, int srcno, int val)
282 ICSState *ics = opaque;
283 struct kvm_irq_level args;
284 int rc;
286 args.irq = srcno + ics->offset;
287 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
288 if (!val) {
289 return;
291 args.level = KVM_INTERRUPT_SET;
292 } else {
293 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
295 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
296 if (rc < 0) {
297 perror("kvm_irq_line");
301 static void ics_kvm_reset(void *dev)
303 ICSState *ics = ICS_SIMPLE(dev);
304 int i;
305 uint8_t flags[ics->nr_irqs];
307 for (i = 0; i < ics->nr_irqs; i++) {
308 flags[i] = ics->irqs[i].flags;
311 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
313 for (i = 0; i < ics->nr_irqs; i++) {
314 ics->irqs[i].priority = 0xff;
315 ics->irqs[i].saved_priority = 0xff;
316 ics->irqs[i].flags = flags[i];
319 ics_set_kvm_state(ics, 1);
322 static void ics_kvm_realize(DeviceState *dev, Error **errp)
324 ICSState *ics = ICS_SIMPLE(dev);
326 if (!ics->nr_irqs) {
327 error_setg(errp, "Number of interrupts needs to be greater 0");
328 return;
330 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
331 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs);
333 qemu_register_reset(ics_kvm_reset, dev);
336 static void ics_kvm_class_init(ObjectClass *klass, void *data)
338 ICSStateClass *icsc = ICS_BASE_CLASS(klass);
340 icsc->realize = ics_kvm_realize;
341 icsc->pre_save = ics_get_kvm_state;
342 icsc->post_load = ics_set_kvm_state;
345 static const TypeInfo ics_kvm_info = {
346 .name = TYPE_ICS_KVM,
347 .parent = TYPE_ICS_SIMPLE,
348 .instance_size = sizeof(ICSState),
349 .class_init = ics_kvm_class_init,
353 * XICS-KVM
356 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr,
357 uint32_t token,
358 uint32_t nargs, target_ulong args,
359 uint32_t nret, target_ulong rets)
361 error_report("pseries: %s must never be called for in-kernel XICS",
362 __func__);
365 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp)
367 int rc;
368 struct kvm_create_device xics_create_device = {
369 .type = KVM_DEV_TYPE_XICS,
370 .flags = 0,
373 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
374 error_setg(errp,
375 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
376 goto fail;
379 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
380 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
381 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
382 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
384 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
385 if (rc < 0) {
386 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
387 goto fail;
390 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
391 if (rc < 0) {
392 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
393 goto fail;
396 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
397 if (rc < 0) {
398 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
399 goto fail;
402 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
403 if (rc < 0) {
404 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
405 goto fail;
408 /* Create the kernel ICP */
409 rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device);
410 if (rc < 0) {
411 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
412 goto fail;
415 kernel_xics_fd = xics_create_device.fd;
417 kvm_kernel_irqchip = true;
418 kvm_msi_via_irqfd_allowed = true;
419 kvm_gsi_direct_mapping = true;
421 return rc;
423 fail:
424 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
425 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
426 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
427 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
428 return -1;
431 static void xics_kvm_register_types(void)
433 type_register_static(&ics_kvm_info);
434 type_register_static(&icp_kvm_info);
437 type_init(xics_kvm_register_types)