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