2013-09-26 10:18:44 +04:00
|
|
|
/*
|
|
|
|
* QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
|
|
|
|
*
|
|
|
|
* PAPR Virtualized Interrupt System, aka ICS/ICP aka xics, in-kernel emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 David Gibson, IBM Corporation.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:16:58 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2016-01-19 23:51:44 +03:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "cpu.h"
|
2013-09-26 10:18:44 +04:00
|
|
|
#include "trace.h"
|
2014-09-17 14:21:29 +04:00
|
|
|
#include "sysemu/kvm.h"
|
2013-09-26 10:18:44 +04:00
|
|
|
#include "hw/ppc/spapr.h"
|
2019-05-13 11:42:45 +03:00
|
|
|
#include "hw/ppc/spapr_cpu_core.h"
|
2013-09-26 10:18:44 +04:00
|
|
|
#include "hw/ppc/xics.h"
|
2019-01-10 11:18:47 +03:00
|
|
|
#include "hw/ppc/xics_spapr.h"
|
2013-09-26 10:18:44 +04:00
|
|
|
#include "kvm_ppc.h"
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2017-02-27 17:29:22 +03:00
|
|
|
static int kernel_xics_fd = -1;
|
|
|
|
|
2017-05-17 17:38:20 +03:00
|
|
|
typedef struct KVMEnabledICP {
|
|
|
|
unsigned long vcpu_id;
|
|
|
|
QLIST_ENTRY(KVMEnabledICP) node;
|
|
|
|
} KVMEnabledICP;
|
|
|
|
|
|
|
|
static QLIST_HEAD(, KVMEnabledICP)
|
|
|
|
kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
|
|
|
|
|
2019-05-13 11:42:40 +03:00
|
|
|
static void kvm_disable_icps(void)
|
|
|
|
{
|
|
|
|
KVMEnabledICP *enabled_icp, *next;
|
|
|
|
|
|
|
|
QLIST_FOREACH_SAFE(enabled_icp, &kvm_enabled_icps, node, next) {
|
|
|
|
QLIST_REMOVE(enabled_icp, node);
|
|
|
|
g_free(enabled_icp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:18:44 +04:00
|
|
|
/*
|
|
|
|
* ICP-KVM
|
|
|
|
*/
|
2019-02-15 14:39:48 +03:00
|
|
|
void icp_get_kvm_state(ICPState *icp)
|
2013-09-26 10:18:44 +04:00
|
|
|
{
|
|
|
|
uint64_t state;
|
|
|
|
int ret;
|
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device is not in use */
|
|
|
|
if (kernel_xics_fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:18:44 +04:00
|
|
|
/* ICP for this CPU thread is not in use, exiting */
|
2017-02-27 17:29:33 +03:00
|
|
|
if (!icp->cs) {
|
2013-09-26 10:18:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:23:10 +03:00
|
|
|
ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
|
2013-09-26 10:18:44 +04:00
|
|
|
if (ret != 0) {
|
|
|
|
error_report("Unable to retrieve KVM interrupt controller state"
|
2017-02-27 17:29:33 +03:00
|
|
|
" for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
|
2013-09-26 10:18:44 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:29:33 +03:00
|
|
|
icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
|
|
|
|
icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
|
2013-09-26 10:18:44 +04:00
|
|
|
& KVM_REG_PPC_ICP_MFRR_MASK;
|
2017-02-27 17:29:33 +03:00
|
|
|
icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
|
2013-09-26 10:18:44 +04:00
|
|
|
& KVM_REG_PPC_ICP_PPRI_MASK;
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:42:39 +03:00
|
|
|
static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
|
|
|
|
{
|
|
|
|
icp_get_kvm_state(arg.host_ptr);
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:39:48 +03:00
|
|
|
void icp_synchronize_state(ICPState *icp)
|
2017-11-13 22:42:39 +03:00
|
|
|
{
|
|
|
|
if (icp->cs) {
|
|
|
|
run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:46:57 +03:00
|
|
|
int icp_set_kvm_state(ICPState *icp, Error **errp)
|
2013-09-26 10:18:44 +04:00
|
|
|
{
|
|
|
|
uint64_t state;
|
|
|
|
int ret;
|
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device is not in use */
|
|
|
|
if (kernel_xics_fd == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:18:44 +04:00
|
|
|
/* ICP for this CPU thread is not in use, exiting */
|
2017-02-27 17:29:33 +03:00
|
|
|
if (!icp->cs) {
|
2013-09-26 10:18:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:29:33 +03:00
|
|
|
state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
|
|
|
|
| ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
|
|
|
|
| ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
|
2013-09-26 10:18:44 +04:00
|
|
|
|
2018-06-11 19:23:10 +03:00
|
|
|
ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
|
2019-06-17 16:46:57 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"Unable to restore KVM interrupt controller state (0x%"
|
|
|
|
PRIx64 ") for CPU %ld", state,
|
|
|
|
kvm_arch_vcpu_id(icp->cs));
|
2013-09-26 10:18:44 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:40:00 +03:00
|
|
|
void icp_kvm_realize(DeviceState *dev, Error **errp)
|
2017-02-27 17:29:24 +03:00
|
|
|
{
|
2018-06-20 13:24:13 +03:00
|
|
|
ICPState *icp = ICP(dev);
|
|
|
|
CPUState *cs;
|
2017-05-17 17:38:20 +03:00
|
|
|
KVMEnabledICP *enabled_icp;
|
2018-06-20 13:24:13 +03:00
|
|
|
unsigned long vcpu_id;
|
2017-02-27 17:29:24 +03:00
|
|
|
int ret;
|
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device is not in use */
|
2017-02-27 17:29:24 +03:00
|
|
|
if (kernel_xics_fd == -1) {
|
2019-05-13 11:42:41 +03:00
|
|
|
return;
|
2017-02-27 17:29:24 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 13:24:13 +03:00
|
|
|
cs = icp->cs;
|
|
|
|
vcpu_id = kvm_arch_vcpu_id(cs);
|
|
|
|
|
2017-02-27 17:29:24 +03:00
|
|
|
/*
|
|
|
|
* If we are reusing a parked vCPU fd corresponding to the CPU
|
|
|
|
* which was hot-removed earlier we don't have to renable
|
|
|
|
* KVM_CAP_IRQ_XICS capability again.
|
|
|
|
*/
|
2017-05-17 17:38:20 +03:00
|
|
|
QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
|
|
|
|
if (enabled_icp->vcpu_id == vcpu_id) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-27 17:29:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:38:20 +03:00
|
|
|
ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
|
2017-02-27 17:29:24 +03:00
|
|
|
if (ret < 0) {
|
2019-11-26 19:46:28 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
error_setg(&local_err, "Unable to connect CPU%ld to kernel XICS: %s",
|
|
|
|
vcpu_id, strerror(errno));
|
|
|
|
if (errno == ENOSPC) {
|
|
|
|
error_append_hint(&local_err, "Try -smp maxcpus=N with N < %u\n",
|
|
|
|
MACHINE(qdev_get_machine())->smp.max_cpus);
|
|
|
|
}
|
|
|
|
error_propagate(errp, local_err);
|
2017-06-08 16:43:08 +03:00
|
|
|
return;
|
2017-02-27 17:29:24 +03:00
|
|
|
}
|
2017-05-17 17:38:20 +03:00
|
|
|
enabled_icp = g_malloc(sizeof(*enabled_icp));
|
|
|
|
enabled_icp->vcpu_id = vcpu_id;
|
|
|
|
QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
|
2017-02-27 17:29:24 +03:00
|
|
|
}
|
|
|
|
|
2013-09-26 10:18:44 +04:00
|
|
|
/*
|
|
|
|
* ICS-KVM
|
|
|
|
*/
|
2019-02-15 14:40:18 +03:00
|
|
|
void ics_get_kvm_state(ICSState *ics)
|
2013-09-26 10:18:44 +04:00
|
|
|
{
|
|
|
|
uint64_t state;
|
|
|
|
int i;
|
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device is not in use */
|
|
|
|
if (kernel_xics_fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:18:44 +04:00
|
|
|
for (i = 0; i < ics->nr_irqs; i++) {
|
|
|
|
ICSIRQState *irq = &ics->irqs[i];
|
|
|
|
|
2019-09-11 16:39:37 +03:00
|
|
|
if (ics_irq_free(ics, i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:23:10 +03:00
|
|
|
kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
|
2018-10-17 11:26:26 +03:00
|
|
|
i + ics->offset, &state, false, &error_fatal);
|
2013-09-26 10:18:44 +04:00
|
|
|
|
|
|
|
irq->server = state & KVM_XICS_DESTINATION_MASK;
|
|
|
|
irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
|
|
|
|
& KVM_XICS_PRIORITY_MASK;
|
|
|
|
/*
|
|
|
|
* To be consistent with the software emulation in xics.c, we
|
|
|
|
* split out the masked state + priority that we get from the
|
|
|
|
* kernel into 'current priority' (0xff if masked) and
|
|
|
|
* 'saved priority' (if masked, this is the priority the
|
|
|
|
* interrupt had before it was masked). Masking and unmasking
|
|
|
|
* are done with the ibm,int-off and ibm,int-on RTAS calls.
|
|
|
|
*/
|
|
|
|
if (state & KVM_XICS_MASKED) {
|
|
|
|
irq->priority = 0xff;
|
|
|
|
} else {
|
|
|
|
irq->priority = irq->saved_priority;
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
irq->status = 0;
|
2013-09-26 10:18:44 +04:00
|
|
|
if (state & KVM_XICS_PENDING) {
|
|
|
|
if (state & KVM_XICS_LEVEL_SENSITIVE) {
|
|
|
|
irq->status |= XICS_STATUS_ASSERTED;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* A pending edge-triggered interrupt (or MSI)
|
|
|
|
* must have been rejected previously when we
|
|
|
|
* first detected it and tried to deliver it,
|
|
|
|
* so mark it as pending and previously rejected
|
|
|
|
* for consistency with how xics.c works.
|
|
|
|
*/
|
|
|
|
irq->status |= XICS_STATUS_MASKED_PENDING
|
|
|
|
| XICS_STATUS_REJECTED;
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 09:32:03 +03:00
|
|
|
if (state & KVM_XICS_PRESENTED) {
|
|
|
|
irq->status |= XICS_STATUS_PRESENTED;
|
|
|
|
}
|
|
|
|
if (state & KVM_XICS_QUEUED) {
|
|
|
|
irq->status |= XICS_STATUS_QUEUED;
|
|
|
|
}
|
2013-09-26 10:18:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:40:18 +03:00
|
|
|
void ics_synchronize_state(ICSState *ics)
|
2017-11-13 22:42:39 +03:00
|
|
|
{
|
|
|
|
ics_get_kvm_state(ics);
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:46:57 +03:00
|
|
|
int ics_set_kvm_state_one(ICSState *ics, int srcno, Error **errp)
|
2013-09-26 10:18:44 +04:00
|
|
|
{
|
|
|
|
uint64_t state;
|
2019-02-19 20:18:03 +03:00
|
|
|
ICSIRQState *irq = &ics->irqs[srcno];
|
|
|
|
int ret;
|
2013-09-26 10:18:44 +04:00
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device is not in use */
|
|
|
|
if (kernel_xics_fd == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-19 20:18:03 +03:00
|
|
|
state = irq->server;
|
|
|
|
state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
|
|
|
|
<< KVM_XICS_PRIORITY_SHIFT;
|
|
|
|
if (irq->priority != irq->saved_priority) {
|
|
|
|
assert(irq->priority == 0xff);
|
2019-07-03 20:22:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (irq->priority == 0xff) {
|
2019-02-19 20:18:03 +03:00
|
|
|
state |= KVM_XICS_MASKED;
|
|
|
|
}
|
2013-09-26 10:18:44 +04:00
|
|
|
|
2019-02-19 20:18:03 +03:00
|
|
|
if (irq->flags & XICS_FLAGS_IRQ_LSI) {
|
|
|
|
state |= KVM_XICS_LEVEL_SENSITIVE;
|
|
|
|
if (irq->status & XICS_STATUS_ASSERTED) {
|
|
|
|
state |= KVM_XICS_PENDING;
|
2013-09-26 10:18:44 +04:00
|
|
|
}
|
2019-02-19 20:18:03 +03:00
|
|
|
} else {
|
|
|
|
if (irq->status & XICS_STATUS_MASKED_PENDING) {
|
|
|
|
state |= KVM_XICS_PENDING;
|
2017-04-27 09:32:03 +03:00
|
|
|
}
|
2019-02-19 20:18:03 +03:00
|
|
|
}
|
|
|
|
if (irq->status & XICS_STATUS_PRESENTED) {
|
|
|
|
state |= KVM_XICS_PRESENTED;
|
|
|
|
}
|
|
|
|
if (irq->status & XICS_STATUS_QUEUED) {
|
|
|
|
state |= KVM_XICS_QUEUED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
|
2019-06-17 16:46:57 +03:00
|
|
|
srcno + ics->offset, &state, true, errp);
|
|
|
|
if (ret < 0) {
|
2019-02-19 20:18:03 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:46:57 +03:00
|
|
|
int ics_set_kvm_state(ICSState *ics, Error **errp)
|
2019-02-19 20:18:03 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device is not in use */
|
|
|
|
if (kernel_xics_fd == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-19 20:18:03 +03:00
|
|
|
for (i = 0; i < ics->nr_irqs; i++) {
|
|
|
|
int ret;
|
2013-09-26 10:18:44 +04:00
|
|
|
|
2019-09-11 16:39:37 +03:00
|
|
|
if (ics_irq_free(ics, i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
error: Eliminate error_propagate() with Coccinelle, part 1
When all we do with an Error we receive into a local variable is
propagating to somewhere else, we can just as well receive it there
right away. Convert
if (!foo(..., &err)) {
...
error_propagate(errp, err);
...
return ...
}
to
if (!foo(..., errp)) {
...
...
return ...
}
where nothing else needs @err. Coccinelle script:
@rule1 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
binary operator op;
constant c1, c2;
symbol false;
@@
if (
(
- fun(args, &err, args2)
+ fun(args, errp, args2)
|
- !fun(args, &err, args2)
+ !fun(args, errp, args2)
|
- fun(args, &err, args2) op c1
+ fun(args, errp, args2) op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
)
}
@rule2 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
expression var;
binary operator op;
constant c1, c2;
symbol false;
@@
- var = fun(args, &err, args2);
+ var = fun(args, errp, args2);
... when != err
if (
(
var
|
!var
|
var op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
|
return var;
)
}
@depends on rule1 || rule2@
identifier err;
@@
- Error *err = NULL;
... when != err
Not exactly elegant, I'm afraid.
The "when != lbl:" is necessary to avoid transforming
if (fun(args, &err)) {
goto out
}
...
out:
error_propagate(errp, err);
even though other paths to label out still need the error_propagate().
For an actual example, see sclp_realize().
Without the "when strict", Coccinelle transforms vfio_msix_setup(),
incorrectly. I don't know what exactly "when strict" does, only that
it helps here.
The match of return is narrower than what I want, but I can't figure
out how to express "return where the operand doesn't use @err". For
an example where it's too narrow, see vfio_intx_enable().
Silently fails to convert hw/arm/armsse.c, because Coccinelle gets
confused by ARMSSE being used both as typedef and function-like macro
there. Converted manually.
Line breaks tidied up manually. One nested declaration of @local_err
deleted manually. Preexisting unwanted blank line dropped in
hw/riscv/sifive_e.c.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 19:06:02 +03:00
|
|
|
ret = ics_set_kvm_state_one(ics, i, errp);
|
2019-06-17 16:46:57 +03:00
|
|
|
if (ret < 0) {
|
2013-09-26 10:18:44 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:40:30 +03:00
|
|
|
void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
|
2013-09-26 10:18:44 +04:00
|
|
|
{
|
|
|
|
struct kvm_irq_level args;
|
|
|
|
int rc;
|
|
|
|
|
2019-05-13 11:42:41 +03:00
|
|
|
/* The KVM XICS device should be in use */
|
|
|
|
assert(kernel_xics_fd != -1);
|
|
|
|
|
2013-09-26 10:18:44 +04:00
|
|
|
args.irq = srcno + ics->offset;
|
2014-05-30 13:34:12 +04:00
|
|
|
if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
|
2013-09-26 10:18:44 +04:00
|
|
|
if (!val) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
args.level = KVM_INTERRUPT_SET;
|
|
|
|
} else {
|
|
|
|
args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
|
|
|
|
}
|
|
|
|
rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("kvm_irq_line");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 19:46:23 +03:00
|
|
|
int xics_kvm_connect(SpaprInterruptController *intc, uint32_t nr_servers,
|
|
|
|
Error **errp)
|
2013-09-26 10:18:44 +04:00
|
|
|
{
|
2019-09-26 16:23:51 +03:00
|
|
|
ICSState *ics = ICS_SPAPR(intc);
|
2017-02-27 17:29:11 +03:00
|
|
|
int rc;
|
2019-05-13 11:42:45 +03:00
|
|
|
CPUState *cs;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The KVM XICS device already in use. This is the case when
|
|
|
|
* rebooting under the XICS-only interrupt mode.
|
|
|
|
*/
|
|
|
|
if (kernel_xics_fd != -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-26 10:18:44 +04:00
|
|
|
|
|
|
|
if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
|
|
|
|
error_setg(errp,
|
|
|
|
"KVM and IRQ_XICS capability must be present for in-kernel XICS");
|
2019-06-17 16:46:46 +03:00
|
|
|
return -1;
|
2013-09-26 10:18:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 17:26:32 +04:00
|
|
|
rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
|
2013-09-26 10:18:44 +04:00
|
|
|
if (rc < 0) {
|
2019-06-17 16:46:52 +03:00
|
|
|
error_setg_errno(&local_err, -rc,
|
|
|
|
"kvmppc_define_rtas_kernel_token: ibm,set-xive");
|
2013-09-26 10:18:44 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-06-23 17:26:32 +04:00
|
|
|
rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
|
2013-09-26 10:18:44 +04:00
|
|
|
if (rc < 0) {
|
2019-06-17 16:46:52 +03:00
|
|
|
error_setg_errno(&local_err, -rc,
|
|
|
|
"kvmppc_define_rtas_kernel_token: ibm,get-xive");
|
2013-09-26 10:18:44 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-06-23 17:26:32 +04:00
|
|
|
rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
|
2013-09-26 10:18:44 +04:00
|
|
|
if (rc < 0) {
|
2019-06-17 16:46:52 +03:00
|
|
|
error_setg_errno(&local_err, -rc,
|
|
|
|
"kvmppc_define_rtas_kernel_token: ibm,int-on");
|
2013-09-26 10:18:44 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-06-23 17:26:32 +04:00
|
|
|
rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
|
2013-09-26 10:18:44 +04:00
|
|
|
if (rc < 0) {
|
2019-06-17 16:46:52 +03:00
|
|
|
error_setg_errno(&local_err, -rc,
|
|
|
|
"kvmppc_define_rtas_kernel_token: ibm,int-off");
|
2013-09-26 10:18:44 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:23:10 +03:00
|
|
|
/* Create the KVM XICS device */
|
|
|
|
rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
|
2013-09-26 10:18:44 +04:00
|
|
|
if (rc < 0) {
|
2019-06-17 16:46:52 +03:00
|
|
|
error_setg_errno(&local_err, -rc, "Error on KVM_CREATE_DEVICE for XICS");
|
2013-09-26 10:18:44 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-11-26 19:46:28 +03:00
|
|
|
/* Tell KVM about the # of VCPUs we may have (POWER9 and newer only) */
|
|
|
|
if (kvm_device_check_attr(rc, KVM_DEV_XICS_GRP_CTRL,
|
|
|
|
KVM_DEV_XICS_NR_SERVERS)) {
|
|
|
|
if (kvm_device_access(rc, KVM_DEV_XICS_GRP_CTRL,
|
|
|
|
KVM_DEV_XICS_NR_SERVERS, &nr_servers, true,
|
|
|
|
&local_err)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:23:10 +03:00
|
|
|
kernel_xics_fd = rc;
|
2013-09-26 10:18:47 +04:00
|
|
|
kvm_kernel_irqchip = true;
|
|
|
|
kvm_msi_via_irqfd_allowed = true;
|
|
|
|
kvm_gsi_direct_mapping = true;
|
|
|
|
|
2019-05-13 11:42:45 +03:00
|
|
|
/* Create the presenters */
|
|
|
|
CPU_FOREACH(cs) {
|
|
|
|
PowerPCCPU *cpu = POWERPC_CPU(cs);
|
|
|
|
|
|
|
|
icp_kvm_realize(DEVICE(spapr_cpu_state(cpu)->icp), &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the KVM sources */
|
2019-09-26 16:23:51 +03:00
|
|
|
ics_set_kvm_state(ics, &local_err);
|
2019-06-17 16:46:57 +03:00
|
|
|
if (local_err) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-13 11:42:45 +03:00
|
|
|
|
|
|
|
/* Connect the presenters to the initial VCPUs of the machine */
|
|
|
|
CPU_FOREACH(cs) {
|
|
|
|
PowerPCCPU *cpu = POWERPC_CPU(cs);
|
2019-06-17 16:46:57 +03:00
|
|
|
icp_set_kvm_state(spapr_cpu_state(cpu)->icp, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-13 11:42:45 +03:00
|
|
|
}
|
|
|
|
|
2018-06-11 19:23:10 +03:00
|
|
|
return 0;
|
2013-09-26 10:18:44 +04:00
|
|
|
|
|
|
|
fail:
|
2019-06-17 16:46:52 +03:00
|
|
|
error_propagate(errp, local_err);
|
2019-09-26 16:23:51 +03:00
|
|
|
xics_kvm_disconnect(intc);
|
2017-02-27 17:29:29 +03:00
|
|
|
return -1;
|
2013-09-26 10:18:44 +04:00
|
|
|
}
|
2019-05-13 11:42:40 +03:00
|
|
|
|
2019-09-26 16:23:51 +03:00
|
|
|
void xics_kvm_disconnect(SpaprInterruptController *intc)
|
2019-05-13 11:42:40 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Only on P9 using the XICS-on XIVE KVM device:
|
|
|
|
*
|
|
|
|
* When the KVM device fd is closed, the device is destroyed and
|
|
|
|
* removed from the list of devices of the VM. The VCPU presenters
|
|
|
|
* are also detached from the device.
|
|
|
|
*/
|
2019-06-17 16:47:03 +03:00
|
|
|
if (kernel_xics_fd != -1) {
|
|
|
|
close(kernel_xics_fd);
|
|
|
|
kernel_xics_fd = -1;
|
|
|
|
}
|
2019-05-13 11:42:40 +03:00
|
|
|
|
|
|
|
kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
|
|
|
|
kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
|
|
|
|
kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
|
|
|
|
kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
|
|
|
|
|
|
|
|
kvm_kernel_irqchip = false;
|
|
|
|
kvm_msi_via_irqfd_allowed = false;
|
|
|
|
kvm_gsi_direct_mapping = false;
|
|
|
|
|
|
|
|
/* Clear the presenter from the VCPUs */
|
|
|
|
kvm_disable_icps();
|
|
|
|
}
|
2019-06-13 19:45:05 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a heuristic to detect older KVMs on POWER9 hosts that don't
|
|
|
|
* support destruction of a KVM XICS device while the VM is running.
|
|
|
|
* Required to start a spapr machine with ic-mode=dual,kernel-irqchip=on.
|
|
|
|
*/
|
2020-11-20 20:46:43 +03:00
|
|
|
bool xics_kvm_has_broken_disconnect(void)
|
2019-06-13 19:45:05 +03:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
|
|
|
|
if (rc < 0) {
|
|
|
|
/*
|
|
|
|
* The error is ignored on purpose. The KVM XICS setup code
|
|
|
|
* will catch it again anyway. The goal here is to see if
|
|
|
|
* close() actually destroys the device or not.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(rc);
|
|
|
|
|
|
|
|
rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
|
|
|
|
if (rc >= 0) {
|
|
|
|
close(rc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return errno == EEXIST;
|
|
|
|
}
|