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