1 /* 2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 3 * 4 * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics, in-kernel emulation 5 * 6 * Copyright (c) 2013 David Gibson, IBM Corporation. 7 * 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: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 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. 25 * 26 */ 27 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" 40 41 #include <sys/ioctl.h> 42 43 static int kernel_xics_fd = -1; 44 45 typedef struct KVMEnabledICP { 46 unsigned long vcpu_id; 47 QLIST_ENTRY(KVMEnabledICP) node; 48 } KVMEnabledICP; 49 50 static QLIST_HEAD(, KVMEnabledICP) 51 kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps); 52 53 /* 54 * ICP-KVM 55 */ 56 static void icp_get_kvm_state(ICPState *icp) 57 { 58 uint64_t state; 59 struct kvm_one_reg reg = { 60 .id = KVM_REG_PPC_ICP_STATE, 61 .addr = (uintptr_t)&state, 62 }; 63 int ret; 64 65 /* ICP for this CPU thread is not in use, exiting */ 66 if (!icp->cs) { 67 return; 68 } 69 70 ret = kvm_vcpu_ioctl(icp->cs, KVM_GET_ONE_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); 75 } 76 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; 82 } 83 84 static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg) 85 { 86 icp_get_kvm_state(arg.host_ptr); 87 } 88 89 static void icp_synchronize_state(ICPState *icp) 90 { 91 if (icp->cs) { 92 run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp)); 93 } 94 } 95 96 static int icp_set_kvm_state(ICPState *icp, int version_id) 97 { 98 uint64_t state; 99 struct kvm_one_reg reg = { 100 .id = KVM_REG_PPC_ICP_STATE, 101 .addr = (uintptr_t)&state, 102 }; 103 int ret; 104 105 /* ICP for this CPU thread is not in use, exiting */ 106 if (!icp->cs) { 107 return 0; 108 } 109 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); 113 114 ret = kvm_vcpu_ioctl(icp->cs, KVM_SET_ONE_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; 120 } 121 122 return 0; 123 } 124 125 static void icp_kvm_reset(ICPState *icp) 126 { 127 icp_set_kvm_state(icp, 1); 128 } 129 130 static void icp_kvm_realize(ICPState *icp, Error **errp) 131 { 132 CPUState *cs = icp->cs; 133 KVMEnabledICP *enabled_icp; 134 unsigned long vcpu_id = kvm_arch_vcpu_id(cs); 135 int ret; 136 137 if (kernel_xics_fd == -1) { 138 abort(); 139 } 140 141 /* 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. 145 */ 146 QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) { 147 if (enabled_icp->vcpu_id == vcpu_id) { 148 return; 149 } 150 } 151 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; 157 } 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); 161 } 162 163 static void icp_kvm_class_init(ObjectClass *klass, void *data) 164 { 165 ICPStateClass *icpc = ICP_CLASS(klass); 166 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; 172 } 173 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), 180 }; 181 182 /* 183 * ICS-KVM 184 */ 185 static void ics_get_kvm_state(ICSState *ics) 186 { 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, 192 }; 193 int i; 194 195 for (i = 0; i < ics->nr_irqs; i++) { 196 ICSIRQState *irq = &ics->irqs[i]; 197 int ret; 198 199 attr.attr = i + ics->offset; 200 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); 206 } 207 208 irq->server = state & KVM_XICS_DESTINATION_MASK; 209 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT) 210 & KVM_XICS_PRIORITY_MASK; 211 /* 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. 218 */ 219 if (state & KVM_XICS_MASKED) { 220 irq->priority = 0xff; 221 } else { 222 irq->priority = irq->saved_priority; 223 } 224 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 { 230 /* 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. 236 */ 237 irq->status |= XICS_STATUS_MASKED_PENDING 238 | XICS_STATUS_REJECTED; 239 } 240 } 241 if (state & KVM_XICS_PRESENTED) { 242 irq->status |= XICS_STATUS_PRESENTED; 243 } 244 if (state & KVM_XICS_QUEUED) { 245 irq->status |= XICS_STATUS_QUEUED; 246 } 247 } 248 } 249 250 static void ics_synchronize_state(ICSState *ics) 251 { 252 ics_get_kvm_state(ics); 253 } 254 255 static int ics_set_kvm_state(ICSState *ics, int version_id) 256 { 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, 262 }; 263 int i; 264 265 for (i = 0; i < ics->nr_irqs; i++) { 266 ICSIRQState *irq = &ics->irqs[i]; 267 int ret; 268 269 attr.attr = i + ics->offset; 270 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; 277 } 278 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; 283 } 284 } else { 285 if (irq->status & XICS_STATUS_MASKED_PENDING) { 286 state |= KVM_XICS_PENDING; 287 } 288 } 289 if (irq->status & XICS_STATUS_PRESENTED) { 290 state |= KVM_XICS_PRESENTED; 291 } 292 if (irq->status & XICS_STATUS_QUEUED) { 293 state |= KVM_XICS_QUEUED; 294 } 295 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; 301 } 302 } 303 304 return 0; 305 } 306 307 static void ics_kvm_set_irq(void *opaque, int srcno, int val) 308 { 309 ICSState *ics = opaque; 310 struct kvm_irq_level args; 311 int rc; 312 313 args.irq = srcno + ics->offset; 314 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) { 315 if (!val) { 316 return; 317 } 318 args.level = KVM_INTERRUPT_SET; 319 } else { 320 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; 321 } 322 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args); 323 if (rc < 0) { 324 perror("kvm_irq_line"); 325 } 326 } 327 328 static void ics_kvm_reset(void *dev) 329 { 330 ICSState *ics = ICS_SIMPLE(dev); 331 int i; 332 uint8_t flags[ics->nr_irqs]; 333 334 for (i = 0; i < ics->nr_irqs; i++) { 335 flags[i] = ics->irqs[i].flags; 336 } 337 338 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs); 339 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]; 344 } 345 346 ics_set_kvm_state(ics, 1); 347 } 348 349 static void ics_kvm_realize(ICSState *ics, Error **errp) 350 { 351 if (!ics->nr_irqs) { 352 error_setg(errp, "Number of interrupts needs to be greater 0"); 353 return; 354 } 355 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState)); 356 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs); 357 358 qemu_register_reset(ics_kvm_reset, ics); 359 } 360 361 static void ics_kvm_class_init(ObjectClass *klass, void *data) 362 { 363 ICSStateClass *icsc = ICS_BASE_CLASS(klass); 364 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; 369 } 370 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, 376 }; 377 378 /* 379 * XICS-KVM 380 */ 381 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) 386 { 387 error_report("pseries: %s must never be called for in-kernel XICS", 388 __func__); 389 } 390 391 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp) 392 { 393 int rc; 394 struct kvm_create_device xics_create_device = { 395 .type = KVM_DEV_TYPE_XICS, 396 .flags = 0, 397 }; 398 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; 403 } 404 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); 409 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; 414 } 415 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; 420 } 421 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; 426 } 427 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; 432 } 433 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; 439 } 440 441 kernel_xics_fd = xics_create_device.fd; 442 443 kvm_kernel_irqchip = true; 444 kvm_msi_via_irqfd_allowed = true; 445 kvm_gsi_direct_mapping = true; 446 447 return rc; 448 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; 455 } 456 457 static void xics_kvm_register_types(void) 458 { 459 type_register_static(&ics_kvm_info); 460 type_register_static(&icp_kvm_info); 461 } 462 463 type_init(xics_kvm_register_types) 464