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