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 *ss) 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 (!ss->cs) { 59 return; 60 } 61 62 ret = kvm_vcpu_ioctl(ss->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(ss->cs), strerror(errno)); 66 exit(1); 67 } 68 69 ss->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT; 70 ss->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT) 71 & KVM_REG_PPC_ICP_MFRR_MASK; 72 ss->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 *ss, 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 (!ss->cs) { 87 return 0; 88 } 89 90 state = ((uint64_t)ss->xirr << KVM_REG_PPC_ICP_XISR_SHIFT) 91 | ((uint64_t)ss->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) 92 | ((uint64_t)ss->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT); 93 94 ret = kvm_vcpu_ioctl(ss->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(ss->cs), 98 strerror(errno)); 99 return ret; 100 } 101 102 return 0; 103 } 104 105 static void icp_kvm_reset(DeviceState *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 *ss, 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 (ss->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 ss->cap_irq_xics_enabled = true; 147 } 148 149 static void icp_kvm_class_init(ObjectClass *klass, void *data) 150 { 151 DeviceClass *dc = DEVICE_CLASS(klass); 152 ICPStateClass *icpc = ICP_CLASS(klass); 153 154 dc->reset = icp_kvm_reset; 155 icpc->pre_save = icp_get_kvm_state; 156 icpc->post_load = icp_set_kvm_state; 157 icpc->cpu_setup = icp_kvm_cpu_setup; 158 } 159 160 static const TypeInfo icp_kvm_info = { 161 .name = TYPE_KVM_ICP, 162 .parent = TYPE_ICP, 163 .instance_size = sizeof(ICPState), 164 .class_init = icp_kvm_class_init, 165 .class_size = sizeof(ICPStateClass), 166 }; 167 168 /* 169 * ICS-KVM 170 */ 171 static void ics_get_kvm_state(ICSState *ics) 172 { 173 uint64_t state; 174 struct kvm_device_attr attr = { 175 .flags = 0, 176 .group = KVM_DEV_XICS_GRP_SOURCES, 177 .addr = (uint64_t)(uintptr_t)&state, 178 }; 179 int i; 180 181 for (i = 0; i < ics->nr_irqs; i++) { 182 ICSIRQState *irq = &ics->irqs[i]; 183 int ret; 184 185 attr.attr = i + ics->offset; 186 187 ret = ioctl(kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr); 188 if (ret != 0) { 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 if (state & KVM_XICS_PENDING) { 212 if (state & KVM_XICS_LEVEL_SENSITIVE) { 213 irq->status |= XICS_STATUS_ASSERTED; 214 } else { 215 /* 216 * A pending edge-triggered interrupt (or MSI) 217 * must have been rejected previously when we 218 * first detected it and tried to deliver it, 219 * so mark it as pending and previously rejected 220 * for consistency with how xics.c works. 221 */ 222 irq->status |= XICS_STATUS_MASKED_PENDING 223 | XICS_STATUS_REJECTED; 224 } 225 } 226 } 227 } 228 229 static int ics_set_kvm_state(ICSState *ics, int version_id) 230 { 231 uint64_t state; 232 struct kvm_device_attr attr = { 233 .flags = 0, 234 .group = KVM_DEV_XICS_GRP_SOURCES, 235 .addr = (uint64_t)(uintptr_t)&state, 236 }; 237 int i; 238 239 for (i = 0; i < ics->nr_irqs; i++) { 240 ICSIRQState *irq = &ics->irqs[i]; 241 int ret; 242 243 attr.attr = i + ics->offset; 244 245 state = irq->server; 246 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK) 247 << KVM_XICS_PRIORITY_SHIFT; 248 if (irq->priority != irq->saved_priority) { 249 assert(irq->priority == 0xff); 250 state |= KVM_XICS_MASKED; 251 } 252 253 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) { 254 state |= KVM_XICS_LEVEL_SENSITIVE; 255 if (irq->status & XICS_STATUS_ASSERTED) { 256 state |= KVM_XICS_PENDING; 257 } 258 } else { 259 if (irq->status & XICS_STATUS_MASKED_PENDING) { 260 state |= KVM_XICS_PENDING; 261 } 262 } 263 264 ret = ioctl(kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr); 265 if (ret != 0) { 266 error_report("Unable to restore KVM interrupt controller state" 267 " for IRQs %d: %s", i + ics->offset, strerror(errno)); 268 return ret; 269 } 270 } 271 272 return 0; 273 } 274 275 static void ics_kvm_set_irq(void *opaque, int srcno, int val) 276 { 277 ICSState *ics = opaque; 278 struct kvm_irq_level args; 279 int rc; 280 281 args.irq = srcno + ics->offset; 282 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) { 283 if (!val) { 284 return; 285 } 286 args.level = KVM_INTERRUPT_SET; 287 } else { 288 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; 289 } 290 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args); 291 if (rc < 0) { 292 perror("kvm_irq_line"); 293 } 294 } 295 296 static void ics_kvm_reset(DeviceState *dev) 297 { 298 ICSState *ics = ICS_SIMPLE(dev); 299 int i; 300 uint8_t flags[ics->nr_irqs]; 301 302 for (i = 0; i < ics->nr_irqs; i++) { 303 flags[i] = ics->irqs[i].flags; 304 } 305 306 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs); 307 308 for (i = 0; i < ics->nr_irqs; i++) { 309 ics->irqs[i].priority = 0xff; 310 ics->irqs[i].saved_priority = 0xff; 311 ics->irqs[i].flags = flags[i]; 312 } 313 314 ics_set_kvm_state(ics, 1); 315 } 316 317 static void ics_kvm_realize(DeviceState *dev, Error **errp) 318 { 319 ICSState *ics = ICS_SIMPLE(dev); 320 321 if (!ics->nr_irqs) { 322 error_setg(errp, "Number of interrupts needs to be greater 0"); 323 return; 324 } 325 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState)); 326 ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs); 327 } 328 329 static void ics_kvm_class_init(ObjectClass *klass, void *data) 330 { 331 DeviceClass *dc = DEVICE_CLASS(klass); 332 ICSStateClass *icsc = ICS_BASE_CLASS(klass); 333 334 icsc->realize = ics_kvm_realize; 335 dc->reset = ics_kvm_reset; 336 icsc->pre_save = ics_get_kvm_state; 337 icsc->post_load = ics_set_kvm_state; 338 } 339 340 static const TypeInfo ics_kvm_info = { 341 .name = TYPE_ICS_KVM, 342 .parent = TYPE_ICS_SIMPLE, 343 .instance_size = sizeof(ICSState), 344 .class_init = ics_kvm_class_init, 345 }; 346 347 /* 348 * XICS-KVM 349 */ 350 351 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr, 352 uint32_t token, 353 uint32_t nargs, target_ulong args, 354 uint32_t nret, target_ulong rets) 355 { 356 error_report("pseries: %s must never be called for in-kernel XICS", 357 __func__); 358 } 359 360 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp) 361 { 362 int rc; 363 struct kvm_create_device xics_create_device = { 364 .type = KVM_DEV_TYPE_XICS, 365 .flags = 0, 366 }; 367 368 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) { 369 error_setg(errp, 370 "KVM and IRQ_XICS capability must be present for in-kernel XICS"); 371 goto fail; 372 } 373 374 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy); 375 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy); 376 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy); 377 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy); 378 379 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive"); 380 if (rc < 0) { 381 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive"); 382 goto fail; 383 } 384 385 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive"); 386 if (rc < 0) { 387 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive"); 388 goto fail; 389 } 390 391 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on"); 392 if (rc < 0) { 393 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on"); 394 goto fail; 395 } 396 397 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off"); 398 if (rc < 0) { 399 error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off"); 400 goto fail; 401 } 402 403 /* Create the kernel ICP */ 404 rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device); 405 if (rc < 0) { 406 error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS"); 407 goto fail; 408 } 409 410 kernel_xics_fd = xics_create_device.fd; 411 412 kvm_kernel_irqchip = true; 413 kvm_msi_via_irqfd_allowed = true; 414 kvm_gsi_direct_mapping = true; 415 416 return rc; 417 418 fail: 419 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive"); 420 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive"); 421 kvmppc_define_rtas_kernel_token(0, "ibm,int-on"); 422 kvmppc_define_rtas_kernel_token(0, "ibm,int-off"); 423 return -1; 424 } 425 426 static void xics_kvm_register_types(void) 427 { 428 type_register_static(&ics_kvm_info); 429 type_register_static(&icp_kvm_info); 430 } 431 432 type_init(xics_kvm_register_types) 433