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 int ret; 60 61 /* ICP for this CPU thread is not in use, exiting */ 62 if (!icp->cs) { 63 return; 64 } 65 66 ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state); 67 if (ret != 0) { 68 error_report("Unable to retrieve KVM interrupt controller state" 69 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno)); 70 exit(1); 71 } 72 73 icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT; 74 icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT) 75 & KVM_REG_PPC_ICP_MFRR_MASK; 76 icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT) 77 & KVM_REG_PPC_ICP_PPRI_MASK; 78 } 79 80 static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg) 81 { 82 icp_get_kvm_state(arg.host_ptr); 83 } 84 85 static void icp_synchronize_state(ICPState *icp) 86 { 87 if (icp->cs) { 88 run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp)); 89 } 90 } 91 92 static int icp_set_kvm_state(ICPState *icp, int version_id) 93 { 94 uint64_t state; 95 int ret; 96 97 /* ICP for this CPU thread is not in use, exiting */ 98 if (!icp->cs) { 99 return 0; 100 } 101 102 state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT) 103 | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) 104 | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT); 105 106 ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state); 107 if (ret != 0) { 108 error_report("Unable to restore KVM interrupt controller state (0x%" 109 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs), 110 strerror(errno)); 111 return ret; 112 } 113 114 return 0; 115 } 116 117 static void icp_kvm_reset(ICPState *icp) 118 { 119 icp_set_kvm_state(icp, 1); 120 } 121 122 static void icp_kvm_realize(ICPState *icp, Error **errp) 123 { 124 CPUState *cs = icp->cs; 125 KVMEnabledICP *enabled_icp; 126 unsigned long vcpu_id = kvm_arch_vcpu_id(cs); 127 int ret; 128 129 if (kernel_xics_fd == -1) { 130 abort(); 131 } 132 133 /* 134 * If we are reusing a parked vCPU fd corresponding to the CPU 135 * which was hot-removed earlier we don't have to renable 136 * KVM_CAP_IRQ_XICS capability again. 137 */ 138 QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) { 139 if (enabled_icp->vcpu_id == vcpu_id) { 140 return; 141 } 142 } 143 144 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id); 145 if (ret < 0) { 146 error_setg(errp, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id, 147 strerror(errno)); 148 return; 149 } 150 enabled_icp = g_malloc(sizeof(*enabled_icp)); 151 enabled_icp->vcpu_id = vcpu_id; 152 QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node); 153 } 154 155 static void icp_kvm_class_init(ObjectClass *klass, void *data) 156 { 157 ICPStateClass *icpc = ICP_CLASS(klass); 158 159 icpc->pre_save = icp_get_kvm_state; 160 icpc->post_load = icp_set_kvm_state; 161 icpc->realize = icp_kvm_realize; 162 icpc->reset = icp_kvm_reset; 163 icpc->synchronize_state = icp_synchronize_state; 164 } 165 166 static const TypeInfo icp_kvm_info = { 167 .name = TYPE_KVM_ICP, 168 .parent = TYPE_ICP, 169 .instance_size = sizeof(ICPState), 170 .class_init = icp_kvm_class_init, 171 .class_size = sizeof(ICPStateClass), 172 }; 173 174 /* 175 * ICS-KVM 176 */ 177 static void ics_get_kvm_state(ICSState *ics) 178 { 179 uint64_t state; 180 int i; 181 Error *local_err = NULL; 182 183 for (i = 0; i < ics->nr_irqs; i++) { 184 ICSIRQState *irq = &ics->irqs[i]; 185 186 kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES, 187 i + ics->offset, &state, false, &local_err); 188 if (local_err) { 189 error_report("Unable to retrieve KVM interrupt controller state" 190 " for IRQ %d: %s", i + ics->offset, strerror(errno)); 191 exit(1); 192 } 193 194 irq->server = state & KVM_XICS_DESTINATION_MASK; 195 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT) 196 & KVM_XICS_PRIORITY_MASK; 197 /* 198 * To be consistent with the software emulation in xics.c, we 199 * split out the masked state + priority that we get from the 200 * kernel into 'current priority' (0xff if masked) and 201 * 'saved priority' (if masked, this is the priority the 202 * interrupt had before it was masked). Masking and unmasking 203 * are done with the ibm,int-off and ibm,int-on RTAS calls. 204 */ 205 if (state & KVM_XICS_MASKED) { 206 irq->priority = 0xff; 207 } else { 208 irq->priority = irq->saved_priority; 209 } 210 211 irq->status = 0; 212 if (state & KVM_XICS_PENDING) { 213 if (state & KVM_XICS_LEVEL_SENSITIVE) { 214 irq->status |= XICS_STATUS_ASSERTED; 215 } else { 216 /* 217 * A pending edge-triggered interrupt (or MSI) 218 * must have been rejected previously when we 219 * first detected it and tried to deliver it, 220 * so mark it as pending and previously rejected 221 * for consistency with how xics.c works. 222 */ 223 irq->status |= XICS_STATUS_MASKED_PENDING 224 | XICS_STATUS_REJECTED; 225 } 226 } 227 if (state & KVM_XICS_PRESENTED) { 228 irq->status |= XICS_STATUS_PRESENTED; 229 } 230 if (state & KVM_XICS_QUEUED) { 231 irq->status |= XICS_STATUS_QUEUED; 232 } 233 } 234 } 235 236 static void ics_synchronize_state(ICSState *ics) 237 { 238 ics_get_kvm_state(ics); 239 } 240 241 static int ics_set_kvm_state(ICSState *ics, int version_id) 242 { 243 uint64_t state; 244 int i; 245 Error *local_err = NULL; 246 247 for (i = 0; i < ics->nr_irqs; i++) { 248 ICSIRQState *irq = &ics->irqs[i]; 249 int ret; 250 251 state = irq->server; 252 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK) 253 << KVM_XICS_PRIORITY_SHIFT; 254 if (irq->priority != irq->saved_priority) { 255 assert(irq->priority == 0xff); 256 state |= KVM_XICS_MASKED; 257 } 258 259 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) { 260 state |= KVM_XICS_LEVEL_SENSITIVE; 261 if (irq->status & XICS_STATUS_ASSERTED) { 262 state |= KVM_XICS_PENDING; 263 } 264 } else { 265 if (irq->status & XICS_STATUS_MASKED_PENDING) { 266 state |= KVM_XICS_PENDING; 267 } 268 } 269 if (irq->status & XICS_STATUS_PRESENTED) { 270 state |= KVM_XICS_PRESENTED; 271 } 272 if (irq->status & XICS_STATUS_QUEUED) { 273 state |= KVM_XICS_QUEUED; 274 } 275 276 kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES, 277 i + ics->offset, &state, true, &local_err); 278 if (local_err) { 279 error_report("Unable to restore KVM interrupt controller state" 280 " for IRQs %d: %s", i + ics->offset, strerror(errno)); 281 return ret; 282 } 283 } 284 285 return 0; 286 } 287 288 static void ics_kvm_set_irq(void *opaque, int srcno, int val) 289 { 290 ICSState *ics = opaque; 291 struct kvm_irq_level args; 292 int rc; 293 294 args.irq = srcno + ics->offset; 295 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) { 296 if (!val) { 297 return; 298 } 299 args.level = KVM_INTERRUPT_SET; 300 } else { 301 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; 302 } 303 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args); 304 if (rc < 0) { 305 perror("kvm_irq_line"); 306 } 307 } 308 309 static void ics_kvm_reset(void *dev) 310 { 311 ICSState *ics = ICS_SIMPLE(dev); 312 int i; 313 uint8_t flags[ics->nr_irqs]; 314 315 for (i = 0; i < ics->nr_irqs; i++) { 316 flags[i] = ics->irqs[i].flags; 317 } 318 319 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs); 320 321 for (i = 0; i < ics->nr_irqs; i++) { 322 ics->irqs[i].priority = 0xff; 323 ics->irqs[i].saved_priority = 0xff; 324 ics->irqs[i].flags = flags[i]; 325 } 326 327 ics_set_kvm_state(ics, 1); 328 } 329 330 static void ics_kvm_realize(ICSState *ics, Error **errp) 331 { 332 if (!ics->nr_irqs) { 333 error_setg(errp, "Number of interrupts needs to be greater 0"); 334 return; 335 } 336 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState)); 337 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs); 338 339 qemu_register_reset(ics_kvm_reset, ics); 340 } 341 342 static void ics_kvm_class_init(ObjectClass *klass, void *data) 343 { 344 ICSStateClass *icsc = ICS_BASE_CLASS(klass); 345 346 icsc->realize = ics_kvm_realize; 347 icsc->pre_save = ics_get_kvm_state; 348 icsc->post_load = ics_set_kvm_state; 349 icsc->synchronize_state = ics_synchronize_state; 350 } 351 352 static const TypeInfo ics_kvm_info = { 353 .name = TYPE_ICS_KVM, 354 .parent = TYPE_ICS_SIMPLE, 355 .instance_size = sizeof(ICSState), 356 .class_init = ics_kvm_class_init, 357 }; 358 359 /* 360 * XICS-KVM 361 */ 362 363 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr, 364 uint32_t token, 365 uint32_t nargs, target_ulong args, 366 uint32_t nret, target_ulong rets) 367 { 368 error_report("pseries: %s must never be called for in-kernel XICS", 369 __func__); 370 } 371 372 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp) 373 { 374 int rc; 375 376 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) { 377 error_setg(errp, 378 "KVM and IRQ_XICS capability must be present for in-kernel XICS"); 379 goto fail; 380 } 381 382 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy); 383 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy); 384 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy); 385 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy); 386 387 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive"); 388 if (rc < 0) { 389 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive"); 390 goto fail; 391 } 392 393 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive"); 394 if (rc < 0) { 395 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive"); 396 goto fail; 397 } 398 399 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on"); 400 if (rc < 0) { 401 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on"); 402 goto fail; 403 } 404 405 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off"); 406 if (rc < 0) { 407 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off"); 408 goto fail; 409 } 410 411 /* Create the KVM XICS device */ 412 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false); 413 if (rc < 0) { 414 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS"); 415 goto fail; 416 } 417 418 kernel_xics_fd = rc; 419 kvm_kernel_irqchip = true; 420 kvm_msi_via_irqfd_allowed = true; 421 kvm_gsi_direct_mapping = true; 422 423 return 0; 424 425 fail: 426 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive"); 427 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive"); 428 kvmppc_define_rtas_kernel_token(0, "ibm,int-on"); 429 kvmppc_define_rtas_kernel_token(0, "ibm,int-off"); 430 return -1; 431 } 432 433 static void xics_kvm_register_types(void) 434 { 435 type_register_static(&ics_kvm_info); 436 type_register_static(&icp_kvm_info); 437 } 438 439 type_init(xics_kvm_register_types) 440