1d85937e6SScott Wood /* 2d85937e6SScott Wood * KVM in-kernel OpenPIC 3d85937e6SScott Wood * 4d85937e6SScott Wood * Copyright 2013 Freescale Semiconductor, Inc. 5d85937e6SScott Wood * 6d85937e6SScott Wood * Permission is hereby granted, free of charge, to any person obtaining a copy 7d85937e6SScott Wood * of this software and associated documentation files (the "Software"), to deal 8d85937e6SScott Wood * in the Software without restriction, including without limitation the rights 9d85937e6SScott Wood * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10d85937e6SScott Wood * copies of the Software, and to permit persons to whom the Software is 11d85937e6SScott Wood * furnished to do so, subject to the following conditions: 12d85937e6SScott Wood * 13d85937e6SScott Wood * The above copyright notice and this permission notice shall be included in 14d85937e6SScott Wood * all copies or substantial portions of the Software. 15d85937e6SScott Wood * 16d85937e6SScott Wood * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17d85937e6SScott Wood * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18d85937e6SScott Wood * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19d85937e6SScott Wood * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20d85937e6SScott Wood * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21d85937e6SScott Wood * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22d85937e6SScott Wood * THE SOFTWARE. 23d85937e6SScott Wood */ 24d85937e6SScott Wood 25d85937e6SScott Wood #include <sys/ioctl.h> 26d85937e6SScott Wood #include "exec/address-spaces.h" 27d85937e6SScott Wood #include "hw/hw.h" 28d85937e6SScott Wood #include "hw/ppc/openpic.h" 29d85937e6SScott Wood #include "hw/pci/msi.h" 30d85937e6SScott Wood #include "hw/sysbus.h" 31d85937e6SScott Wood #include "sysemu/kvm.h" 32d85937e6SScott Wood #include "qemu/log.h" 33d85937e6SScott Wood 34*af354f19SAlexander Graf #define GCR_RESET 0x80000000 35*af354f19SAlexander Graf 36dd49c038SAndreas Färber #define KVM_OPENPIC(obj) \ 37dd49c038SAndreas Färber OBJECT_CHECK(KVMOpenPICState, (obj), TYPE_KVM_OPENPIC) 38dd49c038SAndreas Färber 39d85937e6SScott Wood typedef struct KVMOpenPICState { 40dd49c038SAndreas Färber /*< private >*/ 41dd49c038SAndreas Färber SysBusDevice parent_obj; 42dd49c038SAndreas Färber /*< public >*/ 43dd49c038SAndreas Färber 44d85937e6SScott Wood MemoryRegion mem; 45d85937e6SScott Wood MemoryListener mem_listener; 46d85937e6SScott Wood uint32_t fd; 47d85937e6SScott Wood uint32_t model; 48d85937e6SScott Wood } KVMOpenPICState; 49d85937e6SScott Wood 50d85937e6SScott Wood static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level) 51d85937e6SScott Wood { 52d85937e6SScott Wood kvm_set_irq(kvm_state, n_IRQ, level); 53d85937e6SScott Wood } 54d85937e6SScott Wood 55d85937e6SScott Wood static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val, 56d85937e6SScott Wood unsigned size) 57d85937e6SScott Wood { 58d85937e6SScott Wood KVMOpenPICState *opp = opaque; 59d85937e6SScott Wood struct kvm_device_attr attr; 60d85937e6SScott Wood uint32_t val32 = val; 61d85937e6SScott Wood int ret; 62d85937e6SScott Wood 63d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_REGISTER; 64d85937e6SScott Wood attr.attr = addr; 65d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)&val32; 66d85937e6SScott Wood 67d85937e6SScott Wood ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); 68d85937e6SScott Wood if (ret < 0) { 69d85937e6SScott Wood qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__, 70d85937e6SScott Wood strerror(errno), attr.attr); 71d85937e6SScott Wood } 72d85937e6SScott Wood } 73d85937e6SScott Wood 74*af354f19SAlexander Graf static void kvm_openpic_reset(DeviceState *d) 75*af354f19SAlexander Graf { 76*af354f19SAlexander Graf KVMOpenPICState *opp = KVM_OPENPIC(d); 77*af354f19SAlexander Graf 78*af354f19SAlexander Graf /* Trigger the GCR.RESET bit to reset the PIC */ 79*af354f19SAlexander Graf kvm_openpic_write(opp, 0x1020, GCR_RESET, sizeof(uint32_t)); 80*af354f19SAlexander Graf } 81*af354f19SAlexander Graf 82d85937e6SScott Wood static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size) 83d85937e6SScott Wood { 84d85937e6SScott Wood KVMOpenPICState *opp = opaque; 85d85937e6SScott Wood struct kvm_device_attr attr; 86d85937e6SScott Wood uint32_t val = 0xdeadbeef; 87d85937e6SScott Wood int ret; 88d85937e6SScott Wood 89d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_REGISTER; 90d85937e6SScott Wood attr.attr = addr; 91d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)&val; 92d85937e6SScott Wood 93d85937e6SScott Wood ret = ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr); 94d85937e6SScott Wood if (ret < 0) { 95d85937e6SScott Wood qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__, 96d85937e6SScott Wood strerror(errno), attr.attr); 97d85937e6SScott Wood return 0; 98d85937e6SScott Wood } 99d85937e6SScott Wood 100d85937e6SScott Wood return val; 101d85937e6SScott Wood } 102d85937e6SScott Wood 103d85937e6SScott Wood static const MemoryRegionOps kvm_openpic_mem_ops = { 104d85937e6SScott Wood .write = kvm_openpic_write, 105d85937e6SScott Wood .read = kvm_openpic_read, 106d85937e6SScott Wood .endianness = DEVICE_BIG_ENDIAN, 107d85937e6SScott Wood .impl = { 108d85937e6SScott Wood .min_access_size = 4, 109d85937e6SScott Wood .max_access_size = 4, 110d85937e6SScott Wood }, 111d85937e6SScott Wood }; 112d85937e6SScott Wood 113d85937e6SScott Wood static void kvm_openpic_region_add(MemoryListener *listener, 114d85937e6SScott Wood MemoryRegionSection *section) 115d85937e6SScott Wood { 116d85937e6SScott Wood KVMOpenPICState *opp = container_of(listener, KVMOpenPICState, 117d85937e6SScott Wood mem_listener); 118d85937e6SScott Wood struct kvm_device_attr attr; 119d85937e6SScott Wood uint64_t reg_base; 120d85937e6SScott Wood int ret; 121d85937e6SScott Wood 122d85937e6SScott Wood if (section->address_space != &address_space_memory) { 123d85937e6SScott Wood abort(); 124d85937e6SScott Wood } 125d85937e6SScott Wood 12687d8354dSAlexander Graf /* Ignore events on regions that are not us */ 12787d8354dSAlexander Graf if (section->mr != &opp->mem) { 12887d8354dSAlexander Graf return; 12987d8354dSAlexander Graf } 13087d8354dSAlexander Graf 131d85937e6SScott Wood reg_base = section->offset_within_address_space; 132d85937e6SScott Wood 133d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_MISC; 134d85937e6SScott Wood attr.attr = KVM_DEV_MPIC_BASE_ADDR; 135d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)®_base; 136d85937e6SScott Wood 137d85937e6SScott Wood ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); 138d85937e6SScott Wood if (ret < 0) { 139d85937e6SScott Wood fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__, 140d85937e6SScott Wood strerror(errno), reg_base); 141d85937e6SScott Wood } 142d85937e6SScott Wood } 143d85937e6SScott Wood 144d85937e6SScott Wood static void kvm_openpic_region_del(MemoryListener *listener, 145d85937e6SScott Wood MemoryRegionSection *section) 146d85937e6SScott Wood { 147d85937e6SScott Wood KVMOpenPICState *opp = container_of(listener, KVMOpenPICState, 148d85937e6SScott Wood mem_listener); 149d85937e6SScott Wood struct kvm_device_attr attr; 150d85937e6SScott Wood uint64_t reg_base = 0; 151d85937e6SScott Wood int ret; 152d85937e6SScott Wood 15387d8354dSAlexander Graf /* Ignore events on regions that are not us */ 15487d8354dSAlexander Graf if (section->mr != &opp->mem) { 15587d8354dSAlexander Graf return; 15687d8354dSAlexander Graf } 15787d8354dSAlexander Graf 158d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_MISC; 159d85937e6SScott Wood attr.attr = KVM_DEV_MPIC_BASE_ADDR; 160d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)®_base; 161d85937e6SScott Wood 162d85937e6SScott Wood ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); 163d85937e6SScott Wood if (ret < 0) { 164d85937e6SScott Wood fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__, 165d85937e6SScott Wood strerror(errno), reg_base); 166d85937e6SScott Wood } 167d85937e6SScott Wood } 168d85937e6SScott Wood 169dd49c038SAndreas Färber static void kvm_openpic_init(Object *obj) 170d85937e6SScott Wood { 171dd49c038SAndreas Färber KVMOpenPICState *opp = KVM_OPENPIC(obj); 172dd49c038SAndreas Färber 1731437c94bSPaolo Bonzini memory_region_init_io(&opp->mem, OBJECT(opp), &kvm_openpic_mem_ops, opp, 174dd49c038SAndreas Färber "kvm-openpic", 0x40000); 175dd49c038SAndreas Färber } 176dd49c038SAndreas Färber 177dd49c038SAndreas Färber static void kvm_openpic_realize(DeviceState *dev, Error **errp) 178dd49c038SAndreas Färber { 179dd49c038SAndreas Färber SysBusDevice *d = SYS_BUS_DEVICE(dev); 180dd49c038SAndreas Färber KVMOpenPICState *opp = KVM_OPENPIC(dev); 181d85937e6SScott Wood KVMState *s = kvm_state; 182d85937e6SScott Wood int kvm_openpic_model; 183d85937e6SScott Wood struct kvm_create_device cd = {0}; 184d85937e6SScott Wood int ret, i; 185d85937e6SScott Wood 186d85937e6SScott Wood if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) { 187dd49c038SAndreas Färber error_setg(errp, "Kernel is lacking Device Control API"); 188dd49c038SAndreas Färber return; 189d85937e6SScott Wood } 190d85937e6SScott Wood 191d85937e6SScott Wood switch (opp->model) { 192d85937e6SScott Wood case OPENPIC_MODEL_FSL_MPIC_20: 193d85937e6SScott Wood kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20; 194d85937e6SScott Wood break; 195d85937e6SScott Wood 196d85937e6SScott Wood case OPENPIC_MODEL_FSL_MPIC_42: 197d85937e6SScott Wood kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42; 198d85937e6SScott Wood break; 199d85937e6SScott Wood 200d85937e6SScott Wood default: 201dd49c038SAndreas Färber error_setg(errp, "Unsupported OpenPIC model %" PRIu32, opp->model); 202dd49c038SAndreas Färber return; 203d85937e6SScott Wood } 204d85937e6SScott Wood 205d85937e6SScott Wood cd.type = kvm_openpic_model; 206d85937e6SScott Wood ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd); 207d85937e6SScott Wood if (ret < 0) { 208dd49c038SAndreas Färber error_setg(errp, "Can't create device %d: %s", 209dd49c038SAndreas Färber cd.type, strerror(errno)); 210dd49c038SAndreas Färber return; 211d85937e6SScott Wood } 212d85937e6SScott Wood opp->fd = cd.fd; 213d85937e6SScott Wood 214dd49c038SAndreas Färber sysbus_init_mmio(d, &opp->mem); 215dd49c038SAndreas Färber qdev_init_gpio_in(dev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ); 216d85937e6SScott Wood 217d85937e6SScott Wood opp->mem_listener.region_add = kvm_openpic_region_add; 2186f1834a2SPrasad Joshi opp->mem_listener.region_del = kvm_openpic_region_del; 219d85937e6SScott Wood memory_listener_register(&opp->mem_listener, &address_space_memory); 220d85937e6SScott Wood 221d85937e6SScott Wood /* indicate pic capabilities */ 222d85937e6SScott Wood msi_supported = true; 223d85937e6SScott Wood kvm_kernel_irqchip = true; 224d85937e6SScott Wood kvm_async_interrupts_allowed = true; 225d85937e6SScott Wood 226d85937e6SScott Wood /* set up irq routing */ 227d85937e6SScott Wood kvm_init_irq_routing(kvm_state); 228d85937e6SScott Wood for (i = 0; i < 256; ++i) { 229d85937e6SScott Wood kvm_irqchip_add_irq_route(kvm_state, i, 0, i); 230d85937e6SScott Wood } 231d85937e6SScott Wood 232d85937e6SScott Wood kvm_irqfds_allowed = true; 233d85937e6SScott Wood kvm_msi_via_irqfd_allowed = true; 234d85937e6SScott Wood kvm_gsi_routing_allowed = true; 235d85937e6SScott Wood 236d85937e6SScott Wood kvm_irqchip_commit_routes(s); 237d85937e6SScott Wood } 238d85937e6SScott Wood 239d85937e6SScott Wood int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs) 240d85937e6SScott Wood { 241dd49c038SAndreas Färber KVMOpenPICState *opp = KVM_OPENPIC(d); 242d85937e6SScott Wood 24348add816SCornelia Huck return kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_MPIC, 0, opp->fd, 24448add816SCornelia Huck kvm_arch_vcpu_id(cs)); 245d85937e6SScott Wood } 246d85937e6SScott Wood 247d85937e6SScott Wood static Property kvm_openpic_properties[] = { 248d85937e6SScott Wood DEFINE_PROP_UINT32("model", KVMOpenPICState, model, 249d85937e6SScott Wood OPENPIC_MODEL_FSL_MPIC_20), 250d85937e6SScott Wood DEFINE_PROP_END_OF_LIST(), 251d85937e6SScott Wood }; 252d85937e6SScott Wood 253dd49c038SAndreas Färber static void kvm_openpic_class_init(ObjectClass *oc, void *data) 254d85937e6SScott Wood { 255dd49c038SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 256d85937e6SScott Wood 257dd49c038SAndreas Färber dc->realize = kvm_openpic_realize; 258d85937e6SScott Wood dc->props = kvm_openpic_properties; 259d85937e6SScott Wood dc->reset = kvm_openpic_reset; 260d85937e6SScott Wood } 261d85937e6SScott Wood 262d85937e6SScott Wood static const TypeInfo kvm_openpic_info = { 263dd49c038SAndreas Färber .name = TYPE_KVM_OPENPIC, 264d85937e6SScott Wood .parent = TYPE_SYS_BUS_DEVICE, 265d85937e6SScott Wood .instance_size = sizeof(KVMOpenPICState), 266dd49c038SAndreas Färber .instance_init = kvm_openpic_init, 267d85937e6SScott Wood .class_init = kvm_openpic_class_init, 268d85937e6SScott Wood }; 269d85937e6SScott Wood 270d85937e6SScott Wood static void kvm_openpic_register_types(void) 271d85937e6SScott Wood { 272d85937e6SScott Wood type_register_static(&kvm_openpic_info); 273d85937e6SScott Wood } 274d85937e6SScott Wood 275d85937e6SScott Wood type_init(kvm_openpic_register_types) 276