1 /* 2 * QEMU S390x KVM floating interrupt controller (flic) 3 * 4 * Copyright 2014 IBM Corp. 5 * Author(s): Jens Freimann <jfrei@linux.vnet.ibm.com> 6 * Cornelia Huck <cornelia.huck@de.ibm.com> 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or (at 9 * your option) any later version. See the COPYING file in the top-level 10 * directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qemu-common.h" 15 #include "cpu.h" 16 #include <sys/ioctl.h> 17 #include "qemu/error-report.h" 18 #include "qapi/error.h" 19 #include "hw/sysbus.h" 20 #include "sysemu/kvm.h" 21 #include "hw/s390x/s390_flic.h" 22 #include "hw/s390x/adapter.h" 23 #include "hw/s390x/css.h" 24 #include "trace.h" 25 26 #define FLIC_SAVE_INITIAL_SIZE getpagesize() 27 #define FLIC_FAILED (-1UL) 28 #define FLIC_SAVEVM_VERSION 1 29 30 typedef struct KVMS390FLICState { 31 S390FLICState parent_obj; 32 33 uint32_t fd; 34 bool clear_io_supported; 35 } KVMS390FLICState; 36 37 DeviceState *s390_flic_kvm_create(void) 38 { 39 DeviceState *dev = NULL; 40 41 if (kvm_enabled()) { 42 dev = qdev_create(NULL, TYPE_KVM_S390_FLIC); 43 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC, 44 OBJECT(dev), NULL); 45 } 46 return dev; 47 } 48 49 /** 50 * flic_get_all_irqs - store all pending irqs in buffer 51 * @buf: pointer to buffer which is passed to kernel 52 * @len: length of buffer 53 * @flic: pointer to flic device state 54 * 55 * Returns: -ENOMEM if buffer is too small, 56 * -EINVAL if attr.group is invalid, 57 * -EFAULT if copying to userspace failed, 58 * on success return number of stored interrupts 59 */ 60 static int flic_get_all_irqs(KVMS390FLICState *flic, 61 void *buf, int len) 62 { 63 struct kvm_device_attr attr = { 64 .group = KVM_DEV_FLIC_GET_ALL_IRQS, 65 .addr = (uint64_t) buf, 66 .attr = len, 67 }; 68 int rc; 69 70 rc = ioctl(flic->fd, KVM_GET_DEVICE_ATTR, &attr); 71 72 return rc == -1 ? -errno : rc; 73 } 74 75 static void flic_enable_pfault(KVMS390FLICState *flic) 76 { 77 struct kvm_device_attr attr = { 78 .group = KVM_DEV_FLIC_APF_ENABLE, 79 }; 80 int rc; 81 82 rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 83 84 if (rc) { 85 fprintf(stderr, "flic: couldn't enable pfault\n"); 86 } 87 } 88 89 static void flic_disable_wait_pfault(KVMS390FLICState *flic) 90 { 91 struct kvm_device_attr attr = { 92 .group = KVM_DEV_FLIC_APF_DISABLE_WAIT, 93 }; 94 int rc; 95 96 rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 97 98 if (rc) { 99 fprintf(stderr, "flic: couldn't disable pfault\n"); 100 } 101 } 102 103 /** flic_enqueue_irqs - returns 0 on success 104 * @buf: pointer to buffer which is passed to kernel 105 * @len: length of buffer 106 * @flic: pointer to flic device state 107 * 108 * Returns: -EINVAL if attr.group is unknown 109 */ 110 static int flic_enqueue_irqs(void *buf, uint64_t len, 111 KVMS390FLICState *flic) 112 { 113 int rc; 114 struct kvm_device_attr attr = { 115 .group = KVM_DEV_FLIC_ENQUEUE, 116 .addr = (uint64_t) buf, 117 .attr = len, 118 }; 119 120 rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 121 122 return rc ? -errno : 0; 123 } 124 125 int kvm_s390_inject_flic(struct kvm_s390_irq *irq) 126 { 127 static KVMS390FLICState *flic; 128 129 if (unlikely(!flic)) { 130 flic = KVM_S390_FLIC(s390_get_flic()); 131 } 132 return flic_enqueue_irqs(irq, sizeof(*irq), flic); 133 } 134 135 static int kvm_s390_clear_io_flic(S390FLICState *fs, uint16_t subchannel_id, 136 uint16_t subchannel_nr) 137 { 138 KVMS390FLICState *flic = KVM_S390_FLIC(fs); 139 int rc; 140 uint32_t sid = subchannel_id << 16 | subchannel_nr; 141 struct kvm_device_attr attr = { 142 .group = KVM_DEV_FLIC_CLEAR_IO_IRQ, 143 .addr = (uint64_t) &sid, 144 .attr = sizeof(sid), 145 }; 146 if (unlikely(!flic->clear_io_supported)) { 147 return -ENOSYS; 148 } 149 rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 150 return rc ? -errno : 0; 151 } 152 153 static int kvm_s390_modify_ais_mode(S390FLICState *fs, uint8_t isc, 154 uint16_t mode) 155 { 156 KVMS390FLICState *flic = KVM_S390_FLIC(fs); 157 struct kvm_s390_ais_req req = { 158 .isc = isc, 159 .mode = mode, 160 }; 161 struct kvm_device_attr attr = { 162 .group = KVM_DEV_FLIC_AISM, 163 .addr = (uint64_t)&req, 164 }; 165 166 if (!fs->ais_supported) { 167 return -ENOSYS; 168 } 169 170 return ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr) ? -errno : 0; 171 } 172 173 /** 174 * __get_all_irqs - store all pending irqs in buffer 175 * @flic: pointer to flic device state 176 * @buf: pointer to pointer to a buffer 177 * @len: length of buffer 178 * 179 * Returns: return value of flic_get_all_irqs 180 * Note: Retry and increase buffer size until flic_get_all_irqs 181 * either returns a value >= 0 or a negative error code. 182 * -ENOMEM is an exception, which means the buffer is too small 183 * and we should try again. Other negative error codes can be 184 * -EFAULT and -EINVAL which we ignore at this point 185 */ 186 static int __get_all_irqs(KVMS390FLICState *flic, 187 void **buf, int len) 188 { 189 int r; 190 191 do { 192 /* returns -ENOMEM if buffer is too small and number 193 * of queued interrupts on success */ 194 r = flic_get_all_irqs(flic, *buf, len); 195 if (r >= 0) { 196 break; 197 } 198 len *= 2; 199 *buf = g_try_realloc(*buf, len); 200 if (!buf) { 201 return -ENOMEM; 202 } 203 } while (r == -ENOMEM && len <= KVM_S390_FLIC_MAX_BUFFER); 204 205 return r; 206 } 207 208 static int kvm_s390_register_io_adapter(S390FLICState *fs, uint32_t id, 209 uint8_t isc, bool swap, 210 bool is_maskable, uint8_t flags) 211 { 212 struct kvm_s390_io_adapter adapter = { 213 .id = id, 214 .isc = isc, 215 .maskable = is_maskable, 216 .swap = swap, 217 .flags = flags, 218 }; 219 KVMS390FLICState *flic = KVM_S390_FLIC(fs); 220 int r; 221 struct kvm_device_attr attr = { 222 .group = KVM_DEV_FLIC_ADAPTER_REGISTER, 223 .addr = (uint64_t)&adapter, 224 }; 225 226 if (!kvm_gsi_routing_enabled()) { 227 /* nothing to do */ 228 return 0; 229 } 230 231 r = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 232 233 return r ? -errno : 0; 234 } 235 236 static int kvm_s390_io_adapter_map(S390FLICState *fs, uint32_t id, 237 uint64_t map_addr, bool do_map) 238 { 239 struct kvm_s390_io_adapter_req req = { 240 .id = id, 241 .type = do_map ? KVM_S390_IO_ADAPTER_MAP : KVM_S390_IO_ADAPTER_UNMAP, 242 .addr = map_addr, 243 }; 244 struct kvm_device_attr attr = { 245 .group = KVM_DEV_FLIC_ADAPTER_MODIFY, 246 .addr = (uint64_t)&req, 247 }; 248 KVMS390FLICState *flic = KVM_S390_FLIC(fs); 249 int r; 250 251 if (!kvm_gsi_routing_enabled()) { 252 /* nothing to do */ 253 return 0; 254 } 255 256 r = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 257 return r ? -errno : 0; 258 } 259 260 static int kvm_s390_add_adapter_routes(S390FLICState *fs, 261 AdapterRoutes *routes) 262 { 263 int ret, i; 264 uint64_t ind_offset = routes->adapter.ind_offset; 265 266 for (i = 0; i < routes->num_routes; i++) { 267 ret = kvm_irqchip_add_adapter_route(kvm_state, &routes->adapter); 268 if (ret < 0) { 269 goto out_undo; 270 } 271 routes->gsi[i] = ret; 272 routes->adapter.ind_offset++; 273 } 274 kvm_irqchip_commit_routes(kvm_state); 275 276 /* Restore passed-in structure to original state. */ 277 routes->adapter.ind_offset = ind_offset; 278 return 0; 279 out_undo: 280 while (--i >= 0) { 281 kvm_irqchip_release_virq(kvm_state, routes->gsi[i]); 282 routes->gsi[i] = -1; 283 } 284 routes->adapter.ind_offset = ind_offset; 285 return ret; 286 } 287 288 static void kvm_s390_release_adapter_routes(S390FLICState *fs, 289 AdapterRoutes *routes) 290 { 291 int i; 292 293 for (i = 0; i < routes->num_routes; i++) { 294 if (routes->gsi[i] >= 0) { 295 kvm_irqchip_release_virq(kvm_state, routes->gsi[i]); 296 routes->gsi[i] = -1; 297 } 298 } 299 } 300 301 /** 302 * kvm_flic_save - Save pending floating interrupts 303 * @f: QEMUFile containing migration state 304 * @opaque: pointer to flic device state 305 * @size: ignored 306 * 307 * Note: Pass buf and len to kernel. Start with one page and 308 * increase until buffer is sufficient or maxium size is 309 * reached 310 */ 311 static int kvm_flic_save(QEMUFile *f, void *opaque, size_t size, 312 VMStateField *field, QJSON *vmdesc) 313 { 314 KVMS390FLICState *flic = opaque; 315 int len = FLIC_SAVE_INITIAL_SIZE; 316 void *buf; 317 int count; 318 int r = 0; 319 320 flic_disable_wait_pfault((struct KVMS390FLICState *) opaque); 321 322 buf = g_try_malloc0(len); 323 if (!buf) { 324 /* Storing FLIC_FAILED into the count field here will cause the 325 * target system to fail when attempting to load irqs from the 326 * migration state */ 327 error_report("flic: couldn't allocate memory"); 328 qemu_put_be64(f, FLIC_FAILED); 329 return -ENOMEM; 330 } 331 332 count = __get_all_irqs(flic, &buf, len); 333 if (count < 0) { 334 error_report("flic: couldn't retrieve irqs from kernel, rc %d", 335 count); 336 /* Storing FLIC_FAILED into the count field here will cause the 337 * target system to fail when attempting to load irqs from the 338 * migration state */ 339 qemu_put_be64(f, FLIC_FAILED); 340 r = count; 341 } else { 342 qemu_put_be64(f, count); 343 qemu_put_buffer(f, (uint8_t *) buf, 344 count * sizeof(struct kvm_s390_irq)); 345 } 346 g_free(buf); 347 348 return r; 349 } 350 351 /** 352 * kvm_flic_load - Load pending floating interrupts 353 * @f: QEMUFile containing migration state 354 * @opaque: pointer to flic device state 355 * @size: ignored 356 * 357 * Returns: value of flic_enqueue_irqs, -EINVAL on error 358 * Note: Do nothing when no interrupts where stored 359 * in QEMUFile 360 */ 361 static int kvm_flic_load(QEMUFile *f, void *opaque, size_t size, 362 VMStateField *field) 363 { 364 uint64_t len = 0; 365 uint64_t count = 0; 366 void *buf = NULL; 367 int r = 0; 368 369 flic_enable_pfault((struct KVMS390FLICState *) opaque); 370 371 count = qemu_get_be64(f); 372 len = count * sizeof(struct kvm_s390_irq); 373 if (count == FLIC_FAILED) { 374 r = -EINVAL; 375 goto out; 376 } 377 if (count == 0) { 378 r = 0; 379 goto out; 380 } 381 buf = g_try_malloc0(len); 382 if (!buf) { 383 r = -ENOMEM; 384 goto out; 385 } 386 387 if (qemu_get_buffer(f, (uint8_t *) buf, len) != len) { 388 r = -EINVAL; 389 goto out_free; 390 } 391 r = flic_enqueue_irqs(buf, len, (struct KVMS390FLICState *) opaque); 392 393 out_free: 394 g_free(buf); 395 out: 396 return r; 397 } 398 399 static const VMStateDescription kvm_s390_flic_vmstate = { 400 .name = "s390-flic", 401 .version_id = FLIC_SAVEVM_VERSION, 402 .minimum_version_id = FLIC_SAVEVM_VERSION, 403 .fields = (VMStateField[]) { 404 { 405 .name = "irqs", 406 .info = &(const VMStateInfo) { 407 .name = "irqs", 408 .get = kvm_flic_load, 409 .put = kvm_flic_save, 410 }, 411 .flags = VMS_SINGLE, 412 }, 413 VMSTATE_END_OF_LIST() 414 } 415 }; 416 417 typedef struct KVMS390FLICStateClass { 418 S390FLICStateClass parent_class; 419 DeviceRealize parent_realize; 420 } KVMS390FLICStateClass; 421 422 #define KVM_S390_FLIC_GET_CLASS(obj) \ 423 OBJECT_GET_CLASS(KVMS390FLICStateClass, (obj), TYPE_KVM_S390_FLIC) 424 425 #define KVM_S390_FLIC_CLASS(klass) \ 426 OBJECT_CLASS_CHECK(KVMS390FLICStateClass, (klass), TYPE_KVM_S390_FLIC) 427 428 static void kvm_s390_flic_realize(DeviceState *dev, Error **errp) 429 { 430 S390FLICState *fs = S390_FLIC_COMMON(dev); 431 KVMS390FLICState *flic_state = KVM_S390_FLIC(dev); 432 struct kvm_create_device cd = {0}; 433 struct kvm_device_attr test_attr = {0}; 434 int ret; 435 Error *errp_local = NULL; 436 437 KVM_S390_FLIC_GET_CLASS(dev)->parent_realize(dev, &errp_local); 438 if (errp_local) { 439 goto fail; 440 } 441 flic_state->fd = -1; 442 if (!kvm_check_extension(kvm_state, KVM_CAP_DEVICE_CTRL)) { 443 error_setg_errno(&errp_local, errno, "KVM is missing capability" 444 " KVM_CAP_DEVICE_CTRL"); 445 trace_flic_no_device_api(errno); 446 goto fail; 447 } 448 449 cd.type = KVM_DEV_TYPE_FLIC; 450 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd); 451 if (ret < 0) { 452 error_setg_errno(&errp_local, errno, "Creating the KVM device failed"); 453 trace_flic_create_device(errno); 454 goto fail; 455 } 456 flic_state->fd = cd.fd; 457 458 /* Check clear_io_irq support */ 459 test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ; 460 flic_state->clear_io_supported = !ioctl(flic_state->fd, 461 KVM_HAS_DEVICE_ATTR, test_attr); 462 463 fs->ais_supported = false; 464 return; 465 fail: 466 error_propagate(errp, errp_local); 467 } 468 469 static void kvm_s390_flic_reset(DeviceState *dev) 470 { 471 KVMS390FLICState *flic = KVM_S390_FLIC(dev); 472 S390FLICState *fs = S390_FLIC_COMMON(dev); 473 struct kvm_device_attr attr = { 474 .group = KVM_DEV_FLIC_CLEAR_IRQS, 475 }; 476 int rc = 0; 477 uint8_t isc; 478 479 if (flic->fd == -1) { 480 return; 481 } 482 483 flic_disable_wait_pfault(flic); 484 485 if (fs->ais_supported) { 486 for (isc = 0; isc <= MAX_ISC; isc++) { 487 rc = kvm_s390_modify_ais_mode(fs, isc, SIC_IRQ_MODE_ALL); 488 if (rc) { 489 error_report("Failed to reset ais mode for isc %d: %s", 490 isc, strerror(-rc)); 491 } 492 } 493 } 494 495 rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); 496 if (rc) { 497 trace_flic_reset_failed(errno); 498 } 499 500 flic_enable_pfault(flic); 501 } 502 503 static void kvm_s390_flic_class_init(ObjectClass *oc, void *data) 504 { 505 DeviceClass *dc = DEVICE_CLASS(oc); 506 S390FLICStateClass *fsc = S390_FLIC_COMMON_CLASS(oc); 507 508 KVM_S390_FLIC_CLASS(oc)->parent_realize = dc->realize; 509 dc->realize = kvm_s390_flic_realize; 510 dc->vmsd = &kvm_s390_flic_vmstate; 511 dc->reset = kvm_s390_flic_reset; 512 fsc->register_io_adapter = kvm_s390_register_io_adapter; 513 fsc->io_adapter_map = kvm_s390_io_adapter_map; 514 fsc->add_adapter_routes = kvm_s390_add_adapter_routes; 515 fsc->release_adapter_routes = kvm_s390_release_adapter_routes; 516 fsc->clear_io_irq = kvm_s390_clear_io_flic; 517 fsc->modify_ais_mode = kvm_s390_modify_ais_mode; 518 } 519 520 static const TypeInfo kvm_s390_flic_info = { 521 .name = TYPE_KVM_S390_FLIC, 522 .parent = TYPE_S390_FLIC_COMMON, 523 .instance_size = sizeof(KVMS390FLICState), 524 .class_size = sizeof(KVMS390FLICStateClass), 525 .class_init = kvm_s390_flic_class_init, 526 }; 527 528 static void kvm_s390_flic_register_types(void) 529 { 530 type_register_static(&kvm_s390_flic_info); 531 } 532 533 type_init(kvm_s390_flic_register_types) 534