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 if (state & KVM_XICS_PENDING) { 217 if (state & KVM_XICS_LEVEL_SENSITIVE) { 218 irq->status |= XICS_STATUS_ASSERTED; 219 } else { 220 /* 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. 226 */ 227 irq->status |= XICS_STATUS_MASKED_PENDING 228 | XICS_STATUS_REJECTED; 229 } 230 } 231 } 232 } 233 234 static int ics_set_kvm_state(ICSState *ics, int version_id) 235 { 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, 241 }; 242 int i; 243 244 for (i = 0; i < ics->nr_irqs; i++) { 245 ICSIRQState *irq = &ics->irqs[i]; 246 int ret; 247 248 attr.attr = i + ics->offset; 249 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; 256 } 257 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; 262 } 263 } else { 264 if (irq->status & XICS_STATUS_MASKED_PENDING) { 265 state |= KVM_XICS_PENDING; 266 } 267 } 268 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; 274 } 275 } 276 277 return 0; 278 } 279 280 static void ics_kvm_set_irq(void *opaque, int srcno, int val) 281 { 282 ICSState *ics = opaque; 283 struct kvm_irq_level args; 284 int rc; 285 286 args.irq = srcno + ics->offset; 287 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) { 288 if (!val) { 289 return; 290 } 291 args.level = KVM_INTERRUPT_SET; 292 } else { 293 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; 294 } 295 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args); 296 if (rc < 0) { 297 perror("kvm_irq_line"); 298 } 299 } 300 301 static void ics_kvm_reset(void *dev) 302 { 303 ICSState *ics = ICS_SIMPLE(dev); 304 int i; 305 uint8_t flags[ics->nr_irqs]; 306 307 for (i = 0; i < ics->nr_irqs; i++) { 308 flags[i] = ics->irqs[i].flags; 309 } 310 311 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs); 312 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]; 317 } 318 319 ics_set_kvm_state(ics, 1); 320 } 321 322 static void ics_kvm_realize(DeviceState *dev, Error **errp) 323 { 324 ICSState *ics = ICS_SIMPLE(dev); 325 326 if (!ics->nr_irqs) { 327 error_setg(errp, "Number of interrupts needs to be greater 0"); 328 return; 329 } 330 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState)); 331 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs); 332 333 qemu_register_reset(ics_kvm_reset, dev); 334 } 335 336 static void ics_kvm_class_init(ObjectClass *klass, void *data) 337 { 338 ICSStateClass *icsc = ICS_BASE_CLASS(klass); 339 340 icsc->realize = ics_kvm_realize; 341 icsc->pre_save = ics_get_kvm_state; 342 icsc->post_load = ics_set_kvm_state; 343 } 344 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, 350 }; 351 352 /* 353 * XICS-KVM 354 */ 355 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) 360 { 361 error_report("pseries: %s must never be called for in-kernel XICS", 362 __func__); 363 } 364 365 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp) 366 { 367 int rc; 368 struct kvm_create_device xics_create_device = { 369 .type = KVM_DEV_TYPE_XICS, 370 .flags = 0, 371 }; 372 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; 377 } 378 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); 383 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; 388 } 389 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; 394 } 395 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; 400 } 401 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; 406 } 407 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; 413 } 414 415 kernel_xics_fd = xics_create_device.fd; 416 417 kvm_kernel_irqchip = true; 418 kvm_msi_via_irqfd_allowed = true; 419 kvm_gsi_direct_mapping = true; 420 421 return rc; 422 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; 429 } 430 431 static void xics_kvm_register_types(void) 432 { 433 type_register_static(&ics_kvm_info); 434 type_register_static(&icp_kvm_info); 435 } 436 437 type_init(xics_kvm_register_types) 438