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 /* 46 * ICP-KVM 47 */ 48 static void icp_get_kvm_state(ICPState *icp) 49 { 50 uint64_t state; 51 struct kvm_one_reg reg = { 52 .id = KVM_REG_PPC_ICP_STATE, 53 .addr = (uintptr_t)&state, 54 }; 55 int ret; 56 57 /* ICP for this CPU thread is not in use, exiting */ 58 if (!icp->cs) { 59 return; 60 } 61 62 ret = kvm_vcpu_ioctl(icp->cs, KVM_GET_ONE_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); 67 } 68 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; 74 } 75 76 static int icp_set_kvm_state(ICPState *icp, int version_id) 77 { 78 uint64_t state; 79 struct kvm_one_reg reg = { 80 .id = KVM_REG_PPC_ICP_STATE, 81 .addr = (uintptr_t)&state, 82 }; 83 int ret; 84 85 /* ICP for this CPU thread is not in use, exiting */ 86 if (!icp->cs) { 87 return 0; 88 } 89 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); 93 94 ret = kvm_vcpu_ioctl(icp->cs, KVM_SET_ONE_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; 100 } 101 102 return 0; 103 } 104 105 static void icp_kvm_reset(void *dev) 106 { 107 ICPState *icp = ICP(dev); 108 109 icp->xirr = 0; 110 icp->pending_priority = 0xff; 111 icp->mfrr = 0xff; 112 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); 116 } 117 118 icp_set_kvm_state(icp, 1); 119 } 120 121 static void icp_kvm_cpu_setup(ICPState *icp, PowerPCCPU *cpu) 122 { 123 CPUState *cs = CPU(cpu); 124 int ret; 125 126 if (kernel_xics_fd == -1) { 127 abort(); 128 } 129 130 /* 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. 134 */ 135 if (icp->cap_irq_xics_enabled) { 136 return; 137 } 138 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); 145 } 146 icp->cap_irq_xics_enabled = true; 147 } 148 149 static void icp_kvm_realize(DeviceState *dev, Error **errp) 150 { 151 qemu_register_reset(icp_kvm_reset, dev); 152 } 153 154 static void icp_kvm_class_init(ObjectClass *klass, void *data) 155 { 156 DeviceClass *dc = DEVICE_CLASS(klass); 157 ICPStateClass *icpc = ICP_CLASS(klass); 158 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; 163 } 164 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), 171 }; 172 173 /* 174 * ICS-KVM 175 */ 176 static void ics_get_kvm_state(ICSState *ics) 177 { 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, 183 }; 184 int i; 185 186 for (i = 0; i < ics->nr_irqs; i++) { 187 ICSIRQState *irq = &ics->irqs[i]; 188 int ret; 189 190 attr.attr = i + ics->offset; 191 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); 197 } 198 199 irq->server = state & KVM_XICS_DESTINATION_MASK; 200 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT) 201 & KVM_XICS_PRIORITY_MASK; 202 /* 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. 209 */ 210 if (state & KVM_XICS_MASKED) { 211 irq->priority = 0xff; 212 } else { 213 irq->priority = irq->saved_priority; 214 } 215 216 irq->status = 0; 217 if (state & KVM_XICS_PENDING) { 218 if (state & KVM_XICS_LEVEL_SENSITIVE) { 219 irq->status |= XICS_STATUS_ASSERTED; 220 } else { 221 /* 222 * A pending edge-triggered interrupt (or MSI) 223 * must have been rejected previously when we 224 * first detected it and tried to deliver it, 225 * so mark it as pending and previously rejected 226 * for consistency with how xics.c works. 227 */ 228 irq->status |= XICS_STATUS_MASKED_PENDING 229 | XICS_STATUS_REJECTED; 230 } 231 } 232 if (state & KVM_XICS_PRESENTED) { 233 irq->status |= XICS_STATUS_PRESENTED; 234 } 235 if (state & KVM_XICS_QUEUED) { 236 irq->status |= XICS_STATUS_QUEUED; 237 } 238 } 239 } 240 241 static int ics_set_kvm_state(ICSState *ics, int version_id) 242 { 243 uint64_t state; 244 struct kvm_device_attr attr = { 245 .flags = 0, 246 .group = KVM_DEV_XICS_GRP_SOURCES, 247 .addr = (uint64_t)(uintptr_t)&state, 248 }; 249 int i; 250 251 for (i = 0; i < ics->nr_irqs; i++) { 252 ICSIRQState *irq = &ics->irqs[i]; 253 int ret; 254 255 attr.attr = i + ics->offset; 256 257 state = irq->server; 258 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK) 259 << KVM_XICS_PRIORITY_SHIFT; 260 if (irq->priority != irq->saved_priority) { 261 assert(irq->priority == 0xff); 262 state |= KVM_XICS_MASKED; 263 } 264 265 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) { 266 state |= KVM_XICS_LEVEL_SENSITIVE; 267 if (irq->status & XICS_STATUS_ASSERTED) { 268 state |= KVM_XICS_PENDING; 269 } 270 } else { 271 if (irq->status & XICS_STATUS_MASKED_PENDING) { 272 state |= KVM_XICS_PENDING; 273 } 274 } 275 if (irq->status & XICS_STATUS_PRESENTED) { 276 state |= KVM_XICS_PRESENTED; 277 } 278 if (irq->status & XICS_STATUS_QUEUED) { 279 state |= KVM_XICS_QUEUED; 280 } 281 282 ret = ioctl(kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr); 283 if (ret != 0) { 284 error_report("Unable to restore KVM interrupt controller state" 285 " for IRQs %d: %s", i + ics->offset, strerror(errno)); 286 return ret; 287 } 288 } 289 290 return 0; 291 } 292 293 static void ics_kvm_set_irq(void *opaque, int srcno, int val) 294 { 295 ICSState *ics = opaque; 296 struct kvm_irq_level args; 297 int rc; 298 299 args.irq = srcno + ics->offset; 300 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) { 301 if (!val) { 302 return; 303 } 304 args.level = KVM_INTERRUPT_SET; 305 } else { 306 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; 307 } 308 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args); 309 if (rc < 0) { 310 perror("kvm_irq_line"); 311 } 312 } 313 314 static void ics_kvm_reset(void *dev) 315 { 316 ICSState *ics = ICS_SIMPLE(dev); 317 int i; 318 uint8_t flags[ics->nr_irqs]; 319 320 for (i = 0; i < ics->nr_irqs; i++) { 321 flags[i] = ics->irqs[i].flags; 322 } 323 324 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs); 325 326 for (i = 0; i < ics->nr_irqs; i++) { 327 ics->irqs[i].priority = 0xff; 328 ics->irqs[i].saved_priority = 0xff; 329 ics->irqs[i].flags = flags[i]; 330 } 331 332 ics_set_kvm_state(ics, 1); 333 } 334 335 static void ics_kvm_realize(DeviceState *dev, Error **errp) 336 { 337 ICSState *ics = ICS_SIMPLE(dev); 338 339 if (!ics->nr_irqs) { 340 error_setg(errp, "Number of interrupts needs to be greater 0"); 341 return; 342 } 343 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState)); 344 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs); 345 346 qemu_register_reset(ics_kvm_reset, dev); 347 } 348 349 static void ics_kvm_class_init(ObjectClass *klass, void *data) 350 { 351 ICSStateClass *icsc = ICS_BASE_CLASS(klass); 352 353 icsc->realize = ics_kvm_realize; 354 icsc->pre_save = ics_get_kvm_state; 355 icsc->post_load = ics_set_kvm_state; 356 } 357 358 static const TypeInfo ics_kvm_info = { 359 .name = TYPE_ICS_KVM, 360 .parent = TYPE_ICS_SIMPLE, 361 .instance_size = sizeof(ICSState), 362 .class_init = ics_kvm_class_init, 363 }; 364 365 /* 366 * XICS-KVM 367 */ 368 369 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr, 370 uint32_t token, 371 uint32_t nargs, target_ulong args, 372 uint32_t nret, target_ulong rets) 373 { 374 error_report("pseries: %s must never be called for in-kernel XICS", 375 __func__); 376 } 377 378 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp) 379 { 380 int rc; 381 struct kvm_create_device xics_create_device = { 382 .type = KVM_DEV_TYPE_XICS, 383 .flags = 0, 384 }; 385 386 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) { 387 error_setg(errp, 388 "KVM and IRQ_XICS capability must be present for in-kernel XICS"); 389 goto fail; 390 } 391 392 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy); 393 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy); 394 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy); 395 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy); 396 397 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive"); 398 if (rc < 0) { 399 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive"); 400 goto fail; 401 } 402 403 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive"); 404 if (rc < 0) { 405 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive"); 406 goto fail; 407 } 408 409 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on"); 410 if (rc < 0) { 411 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on"); 412 goto fail; 413 } 414 415 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off"); 416 if (rc < 0) { 417 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off"); 418 goto fail; 419 } 420 421 /* Create the kernel ICP */ 422 rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device); 423 if (rc < 0) { 424 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS"); 425 goto fail; 426 } 427 428 kernel_xics_fd = xics_create_device.fd; 429 430 kvm_kernel_irqchip = true; 431 kvm_msi_via_irqfd_allowed = true; 432 kvm_gsi_direct_mapping = true; 433 434 return rc; 435 436 fail: 437 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive"); 438 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive"); 439 kvmppc_define_rtas_kernel_token(0, "ibm,int-on"); 440 kvmppc_define_rtas_kernel_token(0, "ibm,int-off"); 441 return -1; 442 } 443 444 static void xics_kvm_register_types(void) 445 { 446 type_register_static(&ics_kvm_info); 447 type_register_static(&icp_kvm_info); 448 } 449 450 type_init(xics_kvm_register_types) 451