2013-06-13 04:32:51 +08:00
|
|
|
/*
|
|
|
|
* KVM in-kernel OpenPIC
|
|
|
|
*
|
|
|
|
* Copyright 2013 Freescale Semiconductor, Inc.
|
|
|
|
*
|
|
|
|
* 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-27 02:17:19 +08: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 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2013-06-13 04:32:51 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include "exec/address-spaces.h"
|
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "hw/ppc/openpic.h"
|
|
|
|
#include "hw/pci/msi.h"
|
|
|
|
#include "hw/sysbus.h"
|
|
|
|
#include "sysemu/kvm.h"
|
|
|
|
#include "qemu/log.h"
|
|
|
|
|
2014-05-22 23:12:23 +08:00
|
|
|
#define GCR_RESET 0x80000000
|
|
|
|
|
2013-06-17 03:30:40 +08:00
|
|
|
#define KVM_OPENPIC(obj) \
|
|
|
|
OBJECT_CHECK(KVMOpenPICState, (obj), TYPE_KVM_OPENPIC)
|
|
|
|
|
2013-06-13 04:32:51 +08:00
|
|
|
typedef struct KVMOpenPICState {
|
2013-06-17 03:30:40 +08:00
|
|
|
/*< private >*/
|
|
|
|
SysBusDevice parent_obj;
|
|
|
|
/*< public >*/
|
|
|
|
|
2013-06-13 04:32:51 +08:00
|
|
|
MemoryRegion mem;
|
|
|
|
MemoryListener mem_listener;
|
|
|
|
uint32_t fd;
|
|
|
|
uint32_t model;
|
2014-09-11 18:22:57 +08:00
|
|
|
hwaddr mapped;
|
2013-06-13 04:32:51 +08:00
|
|
|
} KVMOpenPICState;
|
|
|
|
|
|
|
|
static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level)
|
|
|
|
{
|
|
|
|
kvm_set_irq(kvm_state, n_IRQ, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
KVMOpenPICState *opp = opaque;
|
|
|
|
struct kvm_device_attr attr;
|
|
|
|
uint32_t val32 = val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
attr.group = KVM_DEV_MPIC_GRP_REGISTER;
|
|
|
|
attr.attr = addr;
|
|
|
|
attr.addr = (uint64_t)(unsigned long)&val32;
|
|
|
|
|
|
|
|
ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
|
|
|
|
if (ret < 0) {
|
|
|
|
qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
|
|
|
|
strerror(errno), attr.attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 23:12:23 +08:00
|
|
|
static void kvm_openpic_reset(DeviceState *d)
|
|
|
|
{
|
|
|
|
KVMOpenPICState *opp = KVM_OPENPIC(d);
|
|
|
|
|
|
|
|
/* Trigger the GCR.RESET bit to reset the PIC */
|
|
|
|
kvm_openpic_write(opp, 0x1020, GCR_RESET, sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
2013-06-13 04:32:51 +08:00
|
|
|
static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
KVMOpenPICState *opp = opaque;
|
|
|
|
struct kvm_device_attr attr;
|
|
|
|
uint32_t val = 0xdeadbeef;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
attr.group = KVM_DEV_MPIC_GRP_REGISTER;
|
|
|
|
attr.attr = addr;
|
|
|
|
attr.addr = (uint64_t)(unsigned long)&val;
|
|
|
|
|
|
|
|
ret = ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr);
|
|
|
|
if (ret < 0) {
|
|
|
|
qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
|
|
|
|
strerror(errno), attr.attr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps kvm_openpic_mem_ops = {
|
|
|
|
.write = kvm_openpic_write,
|
|
|
|
.read = kvm_openpic_read,
|
|
|
|
.endianness = DEVICE_BIG_ENDIAN,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_openpic_region_add(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
|
|
|
|
mem_listener);
|
|
|
|
struct kvm_device_attr attr;
|
|
|
|
uint64_t reg_base;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (section->address_space != &address_space_memory) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2014-04-02 17:41:58 +08:00
|
|
|
/* Ignore events on regions that are not us */
|
|
|
|
if (section->mr != &opp->mem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-11 18:22:57 +08:00
|
|
|
if (opp->mapped) {
|
|
|
|
/*
|
|
|
|
* We can only map the MPIC once. Since we are already mapped,
|
|
|
|
* the best we can do is ignore new maps.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 04:32:51 +08:00
|
|
|
reg_base = section->offset_within_address_space;
|
2014-09-11 18:22:57 +08:00
|
|
|
opp->mapped = reg_base;
|
2013-06-13 04:32:51 +08:00
|
|
|
|
|
|
|
attr.group = KVM_DEV_MPIC_GRP_MISC;
|
|
|
|
attr.attr = KVM_DEV_MPIC_BASE_ADDR;
|
|
|
|
attr.addr = (uint64_t)(unsigned long)®_base;
|
|
|
|
|
|
|
|
ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
|
|
|
|
strerror(errno), reg_base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_openpic_region_del(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
|
|
|
|
mem_listener);
|
|
|
|
struct kvm_device_attr attr;
|
|
|
|
uint64_t reg_base = 0;
|
|
|
|
int ret;
|
|
|
|
|
2014-04-02 17:41:58 +08:00
|
|
|
/* Ignore events on regions that are not us */
|
|
|
|
if (section->mr != &opp->mem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-11 18:22:57 +08:00
|
|
|
if (section->offset_within_address_space != opp->mapped) {
|
|
|
|
/*
|
|
|
|
* We can only map the MPIC once. This mapping was a secondary
|
|
|
|
* one that we couldn't fulfill. Ignore it.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
opp->mapped = 0;
|
|
|
|
|
2013-06-13 04:32:51 +08:00
|
|
|
attr.group = KVM_DEV_MPIC_GRP_MISC;
|
|
|
|
attr.attr = KVM_DEV_MPIC_BASE_ADDR;
|
|
|
|
attr.addr = (uint64_t)(unsigned long)®_base;
|
|
|
|
|
|
|
|
ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
|
|
|
|
strerror(errno), reg_base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-17 03:30:40 +08:00
|
|
|
static void kvm_openpic_init(Object *obj)
|
|
|
|
{
|
|
|
|
KVMOpenPICState *opp = KVM_OPENPIC(obj);
|
|
|
|
|
2013-06-07 09:25:08 +08:00
|
|
|
memory_region_init_io(&opp->mem, OBJECT(opp), &kvm_openpic_mem_ops, opp,
|
2013-06-17 03:30:40 +08:00
|
|
|
"kvm-openpic", 0x40000);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_openpic_realize(DeviceState *dev, Error **errp)
|
2013-06-13 04:32:51 +08:00
|
|
|
{
|
2013-06-17 03:30:40 +08:00
|
|
|
SysBusDevice *d = SYS_BUS_DEVICE(dev);
|
|
|
|
KVMOpenPICState *opp = KVM_OPENPIC(dev);
|
2013-06-13 04:32:51 +08:00
|
|
|
KVMState *s = kvm_state;
|
|
|
|
int kvm_openpic_model;
|
|
|
|
struct kvm_create_device cd = {0};
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
|
2013-06-17 03:30:40 +08:00
|
|
|
error_setg(errp, "Kernel is lacking Device Control API");
|
|
|
|
return;
|
2013-06-13 04:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (opp->model) {
|
|
|
|
case OPENPIC_MODEL_FSL_MPIC_20:
|
|
|
|
kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPENPIC_MODEL_FSL_MPIC_42:
|
|
|
|
kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-06-17 03:30:40 +08:00
|
|
|
error_setg(errp, "Unsupported OpenPIC model %" PRIu32, opp->model);
|
|
|
|
return;
|
2013-06-13 04:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cd.type = kvm_openpic_model;
|
|
|
|
ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd);
|
|
|
|
if (ret < 0) {
|
2013-06-17 03:30:40 +08:00
|
|
|
error_setg(errp, "Can't create device %d: %s",
|
|
|
|
cd.type, strerror(errno));
|
|
|
|
return;
|
2013-06-13 04:32:51 +08:00
|
|
|
}
|
|
|
|
opp->fd = cd.fd;
|
|
|
|
|
2013-06-17 03:30:40 +08:00
|
|
|
sysbus_init_mmio(d, &opp->mem);
|
|
|
|
qdev_init_gpio_in(dev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ);
|
2013-06-13 04:32:51 +08:00
|
|
|
|
|
|
|
opp->mem_listener.region_add = kvm_openpic_region_add;
|
2014-03-23 17:28:39 +08:00
|
|
|
opp->mem_listener.region_del = kvm_openpic_region_del;
|
2013-06-13 04:32:51 +08:00
|
|
|
memory_listener_register(&opp->mem_listener, &address_space_memory);
|
|
|
|
|
|
|
|
/* indicate pic capabilities */
|
2016-03-04 17:24:28 +08:00
|
|
|
msi_nonbroken = true;
|
2013-06-13 04:32:51 +08:00
|
|
|
kvm_kernel_irqchip = true;
|
|
|
|
kvm_async_interrupts_allowed = true;
|
|
|
|
|
|
|
|
/* set up irq routing */
|
|
|
|
kvm_init_irq_routing(kvm_state);
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_msi_via_irqfd_allowed = true;
|
|
|
|
kvm_gsi_routing_allowed = true;
|
|
|
|
|
|
|
|
kvm_irqchip_commit_routes(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs)
|
|
|
|
{
|
2013-06-17 03:30:40 +08:00
|
|
|
KVMOpenPICState *opp = KVM_OPENPIC(d);
|
2013-06-13 04:32:51 +08:00
|
|
|
|
2014-04-09 23:21:57 +08:00
|
|
|
return kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_MPIC, 0, opp->fd,
|
|
|
|
kvm_arch_vcpu_id(cs));
|
2013-06-13 04:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Property kvm_openpic_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("model", KVMOpenPICState, model,
|
|
|
|
OPENPIC_MODEL_FSL_MPIC_20),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2013-06-17 03:30:40 +08:00
|
|
|
static void kvm_openpic_class_init(ObjectClass *oc, void *data)
|
2013-06-13 04:32:51 +08:00
|
|
|
{
|
2013-06-17 03:30:40 +08:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2013-06-13 04:32:51 +08:00
|
|
|
|
2013-06-17 03:30:40 +08:00
|
|
|
dc->realize = kvm_openpic_realize;
|
2013-06-13 04:32:51 +08:00
|
|
|
dc->props = kvm_openpic_properties;
|
|
|
|
dc->reset = kvm_openpic_reset;
|
2015-09-27 00:22:12 +08:00
|
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
2013-06-13 04:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo kvm_openpic_info = {
|
2013-06-17 03:30:40 +08:00
|
|
|
.name = TYPE_KVM_OPENPIC,
|
2013-06-13 04:32:51 +08:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(KVMOpenPICState),
|
2013-06-17 03:30:40 +08:00
|
|
|
.instance_init = kvm_openpic_init,
|
2013-06-13 04:32:51 +08:00
|
|
|
.class_init = kvm_openpic_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_openpic_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&kvm_openpic_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(kvm_openpic_register_types)
|