qemu/hw/s390x/s390-virtio-ccw.c
<<
>>
Prefs
   1/*
   2 * virtio ccw machine
   3 *
   4 * Copyright 2012, 2020 IBM Corp.
   5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
   6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
   7 *            Janosch Frank <frankja@linux.ibm.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2 or (at
  10 * your option) any later version. See the COPYING file in the top-level
  11 * directory.
  12 */
  13
  14#include "qemu/osdep.h"
  15#include "qapi/error.h"
  16#include "exec/ram_addr.h"
  17#include "hw/s390x/s390-virtio-hcall.h"
  18#include "hw/s390x/sclp.h"
  19#include "hw/s390x/s390_flic.h"
  20#include "hw/s390x/ioinst.h"
  21#include "hw/s390x/css.h"
  22#include "virtio-ccw.h"
  23#include "qemu/config-file.h"
  24#include "qemu/ctype.h"
  25#include "qemu/error-report.h"
  26#include "qemu/option.h"
  27#include "qemu/qemu-print.h"
  28#include "qemu/units.h"
  29#include "hw/s390x/s390-pci-bus.h"
  30#include "sysemu/reset.h"
  31#include "hw/s390x/storage-keys.h"
  32#include "hw/s390x/storage-attributes.h"
  33#include "hw/s390x/event-facility.h"
  34#include "ipl.h"
  35#include "hw/s390x/s390-virtio-ccw.h"
  36#include "hw/s390x/css-bridge.h"
  37#include "hw/s390x/ap-bridge.h"
  38#include "migration/register.h"
  39#include "cpu_models.h"
  40#include "hw/nmi.h"
  41#include "hw/qdev-properties.h"
  42#include "hw/s390x/tod.h"
  43#include "sysemu/sysemu.h"
  44#include "sysemu/cpus.h"
  45#include "hw/s390x/pv.h"
  46#include "migration/blocker.h"
  47#include "qapi/visitor.h"
  48
  49static Error *pv_mig_blocker;
  50
  51S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
  52{
  53    static MachineState *ms;
  54
  55    if (!ms) {
  56        ms = MACHINE(qdev_get_machine());
  57        g_assert(ms->possible_cpus);
  58    }
  59
  60    /* CPU address corresponds to the core_id and the index */
  61    if (cpu_addr >= ms->possible_cpus->len) {
  62        return NULL;
  63    }
  64    return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
  65}
  66
  67static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
  68                              Error **errp)
  69{
  70    S390CPU *cpu = S390_CPU(object_new(typename));
  71    S390CPU *ret = NULL;
  72
  73    if (!object_property_set_int(OBJECT(cpu), "core-id", core_id, errp)) {
  74        goto out;
  75    }
  76    if (!qdev_realize(DEVICE(cpu), NULL, errp)) {
  77        goto out;
  78    }
  79    ret = cpu;
  80
  81out:
  82    object_unref(OBJECT(cpu));
  83    return ret;
  84}
  85
  86static void s390_init_cpus(MachineState *machine)
  87{
  88    MachineClass *mc = MACHINE_GET_CLASS(machine);
  89    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
  90    int i;
  91
  92    if (machine->smp.threads > s390mc->max_threads) {
  93        error_report("S390 does not support more than %d threads.",
  94                     s390mc->max_threads);
  95        exit(1);
  96    }
  97
  98    /* initialize possible_cpus */
  99    mc->possible_cpu_arch_ids(machine);
 100
 101    for (i = 0; i < machine->smp.cpus; i++) {
 102        s390x_new_cpu(machine->cpu_type, i, &error_fatal);
 103    }
 104}
 105
 106static const char *const reset_dev_types[] = {
 107    TYPE_VIRTUAL_CSS_BRIDGE,
 108    "s390-sclp-event-facility",
 109    "s390-flic",
 110    "diag288",
 111    TYPE_S390_PCI_HOST_BRIDGE,
 112};
 113
 114static void subsystem_reset(void)
 115{
 116    DeviceState *dev;
 117    int i;
 118
 119    for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
 120        dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
 121        if (dev) {
 122            device_cold_reset(dev);
 123        }
 124    }
 125}
 126
 127static int virtio_ccw_hcall_notify(const uint64_t *args)
 128{
 129    uint64_t subch_id = args[0];
 130    uint64_t queue = args[1];
 131    SubchDev *sch;
 132    int cssid, ssid, schid, m;
 133
 134    if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
 135        return -EINVAL;
 136    }
 137    sch = css_find_subch(m, cssid, ssid, schid);
 138    if (!sch || !css_subch_visible(sch)) {
 139        return -EINVAL;
 140    }
 141    if (queue >= VIRTIO_QUEUE_MAX) {
 142        return -EINVAL;
 143    }
 144    virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
 145    return 0;
 146
 147}
 148
 149static int virtio_ccw_hcall_early_printk(const uint64_t *args)
 150{
 151    uint64_t mem = args[0];
 152    MachineState *ms = MACHINE(qdev_get_machine());
 153
 154    if (mem < ms->ram_size) {
 155        /* Early printk */
 156        return 0;
 157    }
 158    return -EINVAL;
 159}
 160
 161static void virtio_ccw_register_hcalls(void)
 162{
 163    s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
 164                                   virtio_ccw_hcall_notify);
 165    /* Tolerate early printk. */
 166    s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
 167                                   virtio_ccw_hcall_early_printk);
 168}
 169
 170static void s390_memory_init(MemoryRegion *ram)
 171{
 172    MemoryRegion *sysmem = get_system_memory();
 173
 174    /* allocate RAM for core */
 175    memory_region_add_subregion(sysmem, 0, ram);
 176
 177    /*
 178     * Configure the maximum page size. As no memory devices were created
 179     * yet, this is the page size of initial memory only.
 180     */
 181    s390_set_max_pagesize(qemu_maxrampagesize(), &error_fatal);
 182    /* Initialize storage key device */
 183    s390_skeys_init();
 184    /* Initialize storage attributes device */
 185    s390_stattrib_init();
 186}
 187
 188static void s390_init_ipl_dev(const char *kernel_filename,
 189                              const char *kernel_cmdline,
 190                              const char *initrd_filename, const char *firmware,
 191                              const char *netboot_fw, bool enforce_bios)
 192{
 193    Object *new = object_new(TYPE_S390_IPL);
 194    DeviceState *dev = DEVICE(new);
 195    char *netboot_fw_prop;
 196
 197    if (kernel_filename) {
 198        qdev_prop_set_string(dev, "kernel", kernel_filename);
 199    }
 200    if (initrd_filename) {
 201        qdev_prop_set_string(dev, "initrd", initrd_filename);
 202    }
 203    qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
 204    qdev_prop_set_string(dev, "firmware", firmware);
 205    qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
 206    netboot_fw_prop = object_property_get_str(new, "netboot_fw", &error_abort);
 207    if (!strlen(netboot_fw_prop)) {
 208        qdev_prop_set_string(dev, "netboot_fw", netboot_fw);
 209    }
 210    g_free(netboot_fw_prop);
 211    object_property_add_child(qdev_get_machine(), TYPE_S390_IPL,
 212                              new);
 213    object_unref(new);
 214    qdev_realize(dev, NULL, &error_fatal);
 215}
 216
 217static void s390_create_virtio_net(BusState *bus, const char *name)
 218{
 219    int i;
 220
 221    for (i = 0; i < nb_nics; i++) {
 222        NICInfo *nd = &nd_table[i];
 223        DeviceState *dev;
 224
 225        if (!nd->model) {
 226            nd->model = g_strdup("virtio");
 227        }
 228
 229        qemu_check_nic_model(nd, "virtio");
 230
 231        dev = qdev_new(name);
 232        qdev_set_nic_properties(dev, nd);
 233        qdev_realize_and_unref(dev, bus, &error_fatal);
 234    }
 235}
 236
 237static void s390_create_sclpconsole(const char *type, Chardev *chardev)
 238{
 239    DeviceState *dev;
 240
 241    dev = qdev_new(type);
 242    qdev_prop_set_chr(dev, "chardev", chardev);
 243    qdev_realize_and_unref(dev, sclp_get_event_facility_bus(), &error_fatal);
 244}
 245
 246static void ccw_init(MachineState *machine)
 247{
 248    int ret;
 249    VirtualCssBus *css_bus;
 250    DeviceState *dev;
 251
 252    s390_sclp_init();
 253    /* init memory + setup max page size. Required for the CPU model */
 254    s390_memory_init(machine->ram);
 255
 256    /* init CPUs (incl. CPU model) early so s390_has_feature() works */
 257    s390_init_cpus(machine);
 258
 259    /* Need CPU model to be determined before we can set up PV */
 260    s390_pv_init(machine->cgs, &error_fatal);
 261
 262    s390_flic_init();
 263
 264    /* init the SIGP facility */
 265    s390_init_sigp();
 266
 267    /* create AP bridge and bus(es) */
 268    s390_init_ap();
 269
 270    /* get a BUS */
 271    css_bus = virtual_css_bus_init();
 272    s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
 273                      machine->initrd_filename,
 274                      machine->firmware ?: "s390-ccw.img",
 275                      "s390-netboot.img", true);
 276
 277    dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE);
 278    object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
 279                              OBJECT(dev));
 280    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 281
 282    /* register hypercalls */
 283    virtio_ccw_register_hcalls();
 284
 285    s390_enable_css_support(s390_cpu_addr2state(0));
 286
 287    ret = css_create_css_image(VIRTUAL_CSSID, true);
 288
 289    assert(ret == 0);
 290    if (css_migration_enabled()) {
 291        css_register_vmstate();
 292    }
 293
 294    /* Create VirtIO network adapters */
 295    s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
 296
 297    /* init consoles */
 298    if (serial_hd(0)) {
 299        s390_create_sclpconsole("sclpconsole", serial_hd(0));
 300    }
 301    if (serial_hd(1)) {
 302        s390_create_sclpconsole("sclplmconsole", serial_hd(1));
 303    }
 304
 305    /* init the TOD clock */
 306    s390_init_tod();
 307}
 308
 309static void s390_cpu_plug(HotplugHandler *hotplug_dev,
 310                        DeviceState *dev, Error **errp)
 311{
 312    MachineState *ms = MACHINE(hotplug_dev);
 313    S390CPU *cpu = S390_CPU(dev);
 314
 315    g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu);
 316    ms->possible_cpus->cpus[cpu->env.core_id].cpu = OBJECT(dev);
 317
 318    if (dev->hotplugged) {
 319        raise_irq_cpu_hotplug();
 320    }
 321}
 322
 323static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg)
 324{
 325    S390CPU *cpu = S390_CPU(cs);
 326
 327    s390_ipl_prepare_cpu(cpu);
 328    s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
 329}
 330
 331static void s390_machine_unprotect(S390CcwMachineState *ms)
 332{
 333    if (!s390_pv_vm_try_disable_async()) {
 334        s390_pv_vm_disable();
 335    }
 336    ms->pv = false;
 337    migrate_del_blocker(pv_mig_blocker);
 338    error_free_or_abort(&pv_mig_blocker);
 339    ram_block_discard_disable(false);
 340}
 341
 342static int s390_machine_protect(S390CcwMachineState *ms)
 343{
 344    Error *local_err = NULL;
 345    int rc;
 346
 347   /*
 348    * Discarding of memory in RAM blocks does not work as expected with
 349    * protected VMs. Sharing and unsharing pages would be required. Disable
 350    * it for now, until until we have a solution to make at least Linux
 351    * guests either support it (e.g., virtio-balloon) or fail gracefully.
 352    */
 353    rc = ram_block_discard_disable(true);
 354    if (rc) {
 355        error_report("protected VMs: cannot disable RAM discard");
 356        return rc;
 357    }
 358
 359    error_setg(&pv_mig_blocker,
 360               "protected VMs are currently not migratable.");
 361    rc = migrate_add_blocker(pv_mig_blocker, &local_err);
 362    if (rc) {
 363        ram_block_discard_disable(false);
 364        error_report_err(local_err);
 365        error_free_or_abort(&pv_mig_blocker);
 366        return rc;
 367    }
 368
 369    /* Create SE VM */
 370    rc = s390_pv_vm_enable();
 371    if (rc) {
 372        ram_block_discard_disable(false);
 373        migrate_del_blocker(pv_mig_blocker);
 374        error_free_or_abort(&pv_mig_blocker);
 375        return rc;
 376    }
 377
 378    ms->pv = true;
 379
 380    /* Will return 0 if API is not available since it's not vital */
 381    rc = s390_pv_query_info();
 382    if (rc) {
 383        goto out_err;
 384    }
 385
 386    /* Set SE header and unpack */
 387    rc = s390_ipl_prepare_pv_header();
 388    if (rc) {
 389        goto out_err;
 390    }
 391
 392    /* Decrypt image */
 393    rc = s390_ipl_pv_unpack();
 394    if (rc) {
 395        goto out_err;
 396    }
 397
 398    /* Verify integrity */
 399    rc = s390_pv_verify();
 400    if (rc) {
 401        goto out_err;
 402    }
 403    return rc;
 404
 405out_err:
 406    s390_machine_unprotect(ms);
 407    return rc;
 408}
 409
 410static void s390_pv_prepare_reset(S390CcwMachineState *ms)
 411{
 412    CPUState *cs;
 413
 414    if (!s390_is_pv()) {
 415        return;
 416    }
 417    /* Unsharing requires all cpus to be stopped */
 418    CPU_FOREACH(cs) {
 419        s390_cpu_set_state(S390_CPU_STATE_STOPPED, S390_CPU(cs));
 420    }
 421    s390_pv_unshare();
 422    s390_pv_prep_reset();
 423}
 424
 425static void s390_machine_reset(MachineState *machine, ShutdownCause reason)
 426{
 427    S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
 428    enum s390_reset reset_type;
 429    CPUState *cs, *t;
 430    S390CPU *cpu;
 431
 432    /* get the reset parameters, reset them once done */
 433    s390_ipl_get_reset_request(&cs, &reset_type);
 434
 435    /* all CPUs are paused and synchronized at this point */
 436    s390_cmma_reset();
 437
 438    cpu = S390_CPU(cs);
 439
 440    switch (reset_type) {
 441    case S390_RESET_EXTERNAL:
 442    case S390_RESET_REIPL:
 443        if (s390_is_pv()) {
 444            s390_machine_unprotect(ms);
 445        }
 446
 447        qemu_devices_reset(reason);
 448        s390_crypto_reset();
 449
 450        /* configure and start the ipl CPU only */
 451        run_on_cpu(cs, s390_do_cpu_ipl, RUN_ON_CPU_NULL);
 452        break;
 453    case S390_RESET_MODIFIED_CLEAR:
 454        /*
 455         * Subsystem reset needs to be done before we unshare memory
 456         * and lose access to VIRTIO structures in guest memory.
 457         */
 458        subsystem_reset();
 459        s390_crypto_reset();
 460        s390_pv_prepare_reset(ms);
 461        CPU_FOREACH(t) {
 462            run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
 463        }
 464        run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
 465        break;
 466    case S390_RESET_LOAD_NORMAL:
 467        /*
 468         * Subsystem reset needs to be done before we unshare memory
 469         * and lose access to VIRTIO structures in guest memory.
 470         */
 471        subsystem_reset();
 472        s390_pv_prepare_reset(ms);
 473        CPU_FOREACH(t) {
 474            if (t == cs) {
 475                continue;
 476            }
 477            run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL);
 478        }
 479        run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL);
 480        run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
 481        break;
 482    case S390_RESET_PV: /* Subcode 10 */
 483        subsystem_reset();
 484        s390_crypto_reset();
 485
 486        CPU_FOREACH(t) {
 487            if (t == cs) {
 488                continue;
 489            }
 490            run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
 491        }
 492        run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL);
 493
 494        if (s390_machine_protect(ms)) {
 495            s390_pv_inject_reset_error(cs);
 496            /*
 497             * Continue after the diag308 so the guest knows something
 498             * went wrong.
 499             */
 500            s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
 501            return;
 502        }
 503
 504        run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
 505        break;
 506    default:
 507        g_assert_not_reached();
 508    }
 509
 510    CPU_FOREACH(t) {
 511        run_on_cpu(t, s390_do_cpu_set_diag318, RUN_ON_CPU_HOST_ULONG(0));
 512    }
 513    s390_ipl_clear_reset_request();
 514}
 515
 516static void s390_machine_device_plug(HotplugHandler *hotplug_dev,
 517                                     DeviceState *dev, Error **errp)
 518{
 519    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
 520        s390_cpu_plug(hotplug_dev, dev, errp);
 521    }
 522}
 523
 524static void s390_machine_device_unplug_request(HotplugHandler *hotplug_dev,
 525                                               DeviceState *dev, Error **errp)
 526{
 527    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
 528        error_setg(errp, "CPU hot unplug not supported on this machine");
 529        return;
 530    }
 531}
 532
 533static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms,
 534                                                     unsigned cpu_index)
 535{
 536    MachineClass *mc = MACHINE_GET_CLASS(ms);
 537    const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
 538
 539    assert(cpu_index < possible_cpus->len);
 540    return possible_cpus->cpus[cpu_index].props;
 541}
 542
 543static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms)
 544{
 545    int i;
 546    unsigned int max_cpus = ms->smp.max_cpus;
 547
 548    if (ms->possible_cpus) {
 549        g_assert(ms->possible_cpus && ms->possible_cpus->len == max_cpus);
 550        return ms->possible_cpus;
 551    }
 552
 553    ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
 554                                  sizeof(CPUArchId) * max_cpus);
 555    ms->possible_cpus->len = max_cpus;
 556    for (i = 0; i < ms->possible_cpus->len; i++) {
 557        ms->possible_cpus->cpus[i].type = ms->cpu_type;
 558        ms->possible_cpus->cpus[i].vcpus_count = 1;
 559        ms->possible_cpus->cpus[i].arch_id = i;
 560        ms->possible_cpus->cpus[i].props.has_core_id = true;
 561        ms->possible_cpus->cpus[i].props.core_id = i;
 562    }
 563
 564    return ms->possible_cpus;
 565}
 566
 567static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
 568                                                DeviceState *dev)
 569{
 570    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
 571        return HOTPLUG_HANDLER(machine);
 572    }
 573    return NULL;
 574}
 575
 576static void s390_nmi(NMIState *n, int cpu_index, Error **errp)
 577{
 578    CPUState *cs = qemu_get_cpu(cpu_index);
 579
 580    s390_cpu_restart(S390_CPU(cs));
 581}
 582
 583static ram_addr_t s390_fixup_ram_size(ram_addr_t sz)
 584{
 585    /* same logic as in sclp.c */
 586    int increment_size = 20;
 587    ram_addr_t newsz;
 588
 589    while ((sz >> increment_size) > MAX_STORAGE_INCREMENTS) {
 590        increment_size++;
 591    }
 592    newsz = sz >> increment_size << increment_size;
 593
 594    if (sz != newsz) {
 595        qemu_printf("Ram size %" PRIu64 "MB was fixed up to %" PRIu64
 596                    "MB to match machine restrictions. Consider updating "
 597                    "the guest definition.\n", (uint64_t) (sz / MiB),
 598                    (uint64_t) (newsz / MiB));
 599    }
 600    return newsz;
 601}
 602
 603static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
 604{
 605    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 606
 607    return ms->aes_key_wrap;
 608}
 609
 610static inline void machine_set_aes_key_wrap(Object *obj, bool value,
 611                                            Error **errp)
 612{
 613    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 614
 615    ms->aes_key_wrap = value;
 616}
 617
 618static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
 619{
 620    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 621
 622    return ms->dea_key_wrap;
 623}
 624
 625static inline void machine_set_dea_key_wrap(Object *obj, bool value,
 626                                            Error **errp)
 627{
 628    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 629
 630    ms->dea_key_wrap = value;
 631}
 632
 633static S390CcwMachineClass *current_mc;
 634
 635/*
 636 * Get the class of the s390-ccw-virtio machine that is currently in use.
 637 * Note: libvirt is using the "none" machine to probe for the features of the
 638 * host CPU, so in case this is called with the "none" machine, the function
 639 * returns the TYPE_S390_CCW_MACHINE base class. In this base class, all the
 640 * various "*_allowed" variables are enabled, so that the *_allowed() wrappers
 641 * below return the correct default value for the "none" machine.
 642 *
 643 * Attention! Do *not* add additional new wrappers for CPU features (e.g. like
 644 * the ri_allowed() wrapper) via this mechanism anymore. CPU features should
 645 * be handled via the CPU models, i.e. checking with cpu_model_allowed() during
 646 * CPU initialization and s390_has_feat() later should be sufficient.
 647 */
 648static S390CcwMachineClass *get_machine_class(void)
 649{
 650    if (unlikely(!current_mc)) {
 651        /*
 652        * No s390 ccw machine was instantiated, we are likely to
 653        * be called for the 'none' machine. The properties will
 654        * have their after-initialization values.
 655        */
 656        current_mc = S390_CCW_MACHINE_CLASS(
 657                     object_class_by_name(TYPE_S390_CCW_MACHINE));
 658    }
 659    return current_mc;
 660}
 661
 662bool ri_allowed(void)
 663{
 664    return get_machine_class()->ri_allowed;
 665}
 666
 667bool cpu_model_allowed(void)
 668{
 669    return get_machine_class()->cpu_model_allowed;
 670}
 671
 672bool hpage_1m_allowed(void)
 673{
 674    return get_machine_class()->hpage_1m_allowed;
 675}
 676
 677static void machine_get_loadparm(Object *obj, Visitor *v,
 678                                 const char *name, void *opaque,
 679                                 Error **errp)
 680{
 681    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 682    char *str = g_strndup((char *) ms->loadparm, sizeof(ms->loadparm));
 683
 684    visit_type_str(v, name, &str, errp);
 685    g_free(str);
 686}
 687
 688static void machine_set_loadparm(Object *obj, Visitor *v,
 689                                 const char *name, void *opaque,
 690                                 Error **errp)
 691{
 692    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 693    char *val;
 694    int i;
 695
 696    if (!visit_type_str(v, name, &val, errp)) {
 697        return;
 698    }
 699
 700    for (i = 0; i < sizeof(ms->loadparm) && val[i]; i++) {
 701        uint8_t c = qemu_toupper(val[i]); /* mimic HMC */
 702
 703        if (('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || (c == '.') ||
 704            (c == ' ')) {
 705            ms->loadparm[i] = c;
 706        } else {
 707            error_setg(errp, "LOADPARM: invalid character '%c' (ASCII 0x%02x)",
 708                       c, c);
 709            return;
 710        }
 711    }
 712
 713    for (; i < sizeof(ms->loadparm); i++) {
 714        ms->loadparm[i] = ' '; /* pad right with spaces */
 715    }
 716}
 717
 718static void ccw_machine_class_init(ObjectClass *oc, void *data)
 719{
 720    MachineClass *mc = MACHINE_CLASS(oc);
 721    NMIClass *nc = NMI_CLASS(oc);
 722    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
 723    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
 724
 725    s390mc->ri_allowed = true;
 726    s390mc->cpu_model_allowed = true;
 727    s390mc->css_migration_enabled = true;
 728    s390mc->hpage_1m_allowed = true;
 729    s390mc->max_threads = 1;
 730    mc->init = ccw_init;
 731    mc->reset = s390_machine_reset;
 732    mc->block_default_type = IF_VIRTIO;
 733    mc->no_cdrom = 1;
 734    mc->no_floppy = 1;
 735    mc->no_parallel = 1;
 736    mc->no_sdcard = 1;
 737    mc->max_cpus = S390_MAX_CPUS;
 738    mc->has_hotpluggable_cpus = true;
 739    assert(!mc->get_hotplug_handler);
 740    mc->get_hotplug_handler = s390_get_hotplug_handler;
 741    mc->cpu_index_to_instance_props = s390_cpu_index_to_props;
 742    mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids;
 743    /* it is overridden with 'host' cpu *in kvm_arch_init* */
 744    mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu");
 745    hc->plug = s390_machine_device_plug;
 746    hc->unplug_request = s390_machine_device_unplug_request;
 747    nc->nmi_monitor_handler = s390_nmi;
 748    mc->default_ram_id = "s390.ram";
 749
 750    object_class_property_add_bool(oc, "aes-key-wrap",
 751                                   machine_get_aes_key_wrap,
 752                                   machine_set_aes_key_wrap);
 753    object_class_property_set_description(oc, "aes-key-wrap",
 754            "enable/disable AES key wrapping using the CPACF wrapping key");
 755
 756    object_class_property_add_bool(oc, "dea-key-wrap",
 757                                   machine_get_dea_key_wrap,
 758                                   machine_set_dea_key_wrap);
 759    object_class_property_set_description(oc, "dea-key-wrap",
 760            "enable/disable DEA key wrapping using the CPACF wrapping key");
 761
 762    object_class_property_add(oc, "loadparm", "loadparm",
 763                              machine_get_loadparm, machine_set_loadparm,
 764                              NULL, NULL);
 765    object_class_property_set_description(oc, "loadparm",
 766            "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted"
 767            " to upper case) to pass to machine loader, boot manager,"
 768            " and guest kernel");
 769}
 770
 771static inline void s390_machine_initfn(Object *obj)
 772{
 773    S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
 774
 775    ms->aes_key_wrap = true;
 776    ms->dea_key_wrap = true;
 777}
 778
 779static const TypeInfo ccw_machine_info = {
 780    .name          = TYPE_S390_CCW_MACHINE,
 781    .parent        = TYPE_MACHINE,
 782    .abstract      = true,
 783    .instance_size = sizeof(S390CcwMachineState),
 784    .instance_init = s390_machine_initfn,
 785    .class_size = sizeof(S390CcwMachineClass),
 786    .class_init    = ccw_machine_class_init,
 787    .interfaces = (InterfaceInfo[]) {
 788        { TYPE_NMI },
 789        { TYPE_HOTPLUG_HANDLER},
 790        { }
 791    },
 792};
 793
 794bool css_migration_enabled(void)
 795{
 796    return get_machine_class()->css_migration_enabled;
 797}
 798
 799#define DEFINE_CCW_MACHINE(suffix, verstr, latest)                            \
 800    static void ccw_machine_##suffix##_class_init(ObjectClass *oc,            \
 801                                                  void *data)                 \
 802    {                                                                         \
 803        MachineClass *mc = MACHINE_CLASS(oc);                                 \
 804        ccw_machine_##suffix##_class_options(mc);                             \
 805        mc->desc = "Virtual s390x machine (version " verstr ")";              \
 806        if (latest) {                                                         \
 807            mc->alias = "s390-ccw-virtio";                                    \
 808            mc->is_default = true;                                            \
 809        }                                                                     \
 810    }                                                                         \
 811    static void ccw_machine_##suffix##_instance_init(Object *obj)             \
 812    {                                                                         \
 813        MachineState *machine = MACHINE(obj);                                 \
 814        current_mc = S390_CCW_MACHINE_CLASS(MACHINE_GET_CLASS(machine));          \
 815        ccw_machine_##suffix##_instance_options(machine);                     \
 816    }                                                                         \
 817    static const TypeInfo ccw_machine_##suffix##_info = {                     \
 818        .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr),                 \
 819        .parent = TYPE_S390_CCW_MACHINE,                                      \
 820        .class_init = ccw_machine_##suffix##_class_init,                      \
 821        .instance_init = ccw_machine_##suffix##_instance_init,                \
 822    };                                                                        \
 823    static void ccw_machine_register_##suffix(void)                           \
 824    {                                                                         \
 825        type_register_static(&ccw_machine_##suffix##_info);                   \
 826    }                                                                         \
 827    type_init(ccw_machine_register_##suffix)
 828
 829static void ccw_machine_8_0_instance_options(MachineState *machine)
 830{
 831}
 832
 833static void ccw_machine_8_0_class_options(MachineClass *mc)
 834{
 835}
 836DEFINE_CCW_MACHINE(8_0, "8.0", true);
 837
 838static void ccw_machine_7_2_instance_options(MachineState *machine)
 839{
 840    ccw_machine_8_0_instance_options(machine);
 841}
 842
 843static void ccw_machine_7_2_class_options(MachineClass *mc)
 844{
 845    ccw_machine_8_0_class_options(mc);
 846    compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len);
 847}
 848DEFINE_CCW_MACHINE(7_2, "7.2", false);
 849
 850static void ccw_machine_7_1_instance_options(MachineState *machine)
 851{
 852    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_1 };
 853
 854    ccw_machine_7_2_instance_options(machine);
 855    s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAIE);
 856    s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat);
 857}
 858
 859static void ccw_machine_7_1_class_options(MachineClass *mc)
 860{
 861    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
 862    static GlobalProperty compat[] = {
 863        { TYPE_S390_PCI_DEVICE, "interpret", "off", },
 864        { TYPE_S390_PCI_DEVICE, "forwarding-assist", "off", },
 865    };
 866
 867    ccw_machine_7_2_class_options(mc);
 868    compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len);
 869    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
 870    s390mc->max_threads = S390_MAX_CPUS;
 871}
 872DEFINE_CCW_MACHINE(7_1, "7.1", false);
 873
 874static void ccw_machine_7_0_instance_options(MachineState *machine)
 875{
 876    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_0 };
 877
 878    ccw_machine_7_1_instance_options(machine);
 879    s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat);
 880}
 881
 882static void ccw_machine_7_0_class_options(MachineClass *mc)
 883{
 884    ccw_machine_7_1_class_options(mc);
 885    compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len);
 886}
 887DEFINE_CCW_MACHINE(7_0, "7.0", false);
 888
 889static void ccw_machine_6_2_instance_options(MachineState *machine)
 890{
 891    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_2 };
 892
 893    ccw_machine_7_0_instance_options(machine);
 894    s390_set_qemu_cpu_model(0x3906, 14, 2, qemu_cpu_feat);
 895}
 896
 897static void ccw_machine_6_2_class_options(MachineClass *mc)
 898{
 899    ccw_machine_7_0_class_options(mc);
 900    compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len);
 901}
 902DEFINE_CCW_MACHINE(6_2, "6.2", false);
 903
 904static void ccw_machine_6_1_instance_options(MachineState *machine)
 905{
 906    ccw_machine_6_2_instance_options(machine);
 907    s390_cpudef_featoff_greater(16, 1, S390_FEAT_NNPA);
 908    s390_cpudef_featoff_greater(16, 1, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2);
 909    s390_cpudef_featoff_greater(16, 1, S390_FEAT_BEAR_ENH);
 910    s390_cpudef_featoff_greater(16, 1, S390_FEAT_RDP);
 911    s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAI);
 912}
 913
 914static void ccw_machine_6_1_class_options(MachineClass *mc)
 915{
 916    ccw_machine_6_2_class_options(mc);
 917    compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len);
 918    mc->smp_props.prefer_sockets = true;
 919}
 920DEFINE_CCW_MACHINE(6_1, "6.1", false);
 921
 922static void ccw_machine_6_0_instance_options(MachineState *machine)
 923{
 924    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_0 };
 925
 926    ccw_machine_6_1_instance_options(machine);
 927    s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
 928}
 929
 930static void ccw_machine_6_0_class_options(MachineClass *mc)
 931{
 932    ccw_machine_6_1_class_options(mc);
 933    compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len);
 934}
 935DEFINE_CCW_MACHINE(6_0, "6.0", false);
 936
 937static void ccw_machine_5_2_instance_options(MachineState *machine)
 938{
 939    ccw_machine_6_0_instance_options(machine);
 940}
 941
 942static void ccw_machine_5_2_class_options(MachineClass *mc)
 943{
 944    ccw_machine_6_0_class_options(mc);
 945    compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len);
 946}
 947DEFINE_CCW_MACHINE(5_2, "5.2", false);
 948
 949static void ccw_machine_5_1_instance_options(MachineState *machine)
 950{
 951    ccw_machine_5_2_instance_options(machine);
 952}
 953
 954static void ccw_machine_5_1_class_options(MachineClass *mc)
 955{
 956    ccw_machine_5_2_class_options(mc);
 957    compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len);
 958}
 959DEFINE_CCW_MACHINE(5_1, "5.1", false);
 960
 961static void ccw_machine_5_0_instance_options(MachineState *machine)
 962{
 963    ccw_machine_5_1_instance_options(machine);
 964}
 965
 966static void ccw_machine_5_0_class_options(MachineClass *mc)
 967{
 968    ccw_machine_5_1_class_options(mc);
 969    compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
 970}
 971DEFINE_CCW_MACHINE(5_0, "5.0", false);
 972
 973static void ccw_machine_4_2_instance_options(MachineState *machine)
 974{
 975    ccw_machine_5_0_instance_options(machine);
 976}
 977
 978static void ccw_machine_4_2_class_options(MachineClass *mc)
 979{
 980    ccw_machine_5_0_class_options(mc);
 981    mc->fixup_ram_size = s390_fixup_ram_size;
 982    compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
 983}
 984DEFINE_CCW_MACHINE(4_2, "4.2", false);
 985
 986static void ccw_machine_4_1_instance_options(MachineState *machine)
 987{
 988    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_1 };
 989    ccw_machine_4_2_instance_options(machine);
 990    s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
 991}
 992
 993static void ccw_machine_4_1_class_options(MachineClass *mc)
 994{
 995    ccw_machine_4_2_class_options(mc);
 996    compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
 997}
 998DEFINE_CCW_MACHINE(4_1, "4.1", false);
 999
1000static void ccw_machine_4_0_instance_options(MachineState *machine)
1001{
1002    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_0 };
1003    ccw_machine_4_1_instance_options(machine);
1004    s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
1005}
1006
1007static void ccw_machine_4_0_class_options(MachineClass *mc)
1008{
1009    ccw_machine_4_1_class_options(mc);
1010    compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
1011}
1012DEFINE_CCW_MACHINE(4_0, "4.0", false);
1013
1014static void ccw_machine_3_1_instance_options(MachineState *machine)
1015{
1016    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V3_1 };
1017    ccw_machine_4_0_instance_options(machine);
1018    s390_cpudef_featoff_greater(14, 1, S390_FEAT_MULTIPLE_EPOCH);
1019    s390_cpudef_group_featoff_greater(14, 1, S390_FEAT_GROUP_MULTIPLE_EPOCH_PTFF);
1020    s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
1021}
1022
1023static void ccw_machine_3_1_class_options(MachineClass *mc)
1024{
1025    ccw_machine_4_0_class_options(mc);
1026    compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
1027}
1028DEFINE_CCW_MACHINE(3_1, "3.1", false);
1029
1030static void ccw_machine_3_0_instance_options(MachineState *machine)
1031{
1032    ccw_machine_3_1_instance_options(machine);
1033}
1034
1035static void ccw_machine_3_0_class_options(MachineClass *mc)
1036{
1037    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1038
1039    s390mc->hpage_1m_allowed = false;
1040    ccw_machine_3_1_class_options(mc);
1041    compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
1042}
1043DEFINE_CCW_MACHINE(3_0, "3.0", false);
1044
1045static void ccw_machine_2_12_instance_options(MachineState *machine)
1046{
1047    ccw_machine_3_0_instance_options(machine);
1048    s390_cpudef_featoff_greater(11, 1, S390_FEAT_PPA15);
1049    s390_cpudef_featoff_greater(11, 1, S390_FEAT_BPB);
1050}
1051
1052static void ccw_machine_2_12_class_options(MachineClass *mc)
1053{
1054    ccw_machine_3_0_class_options(mc);
1055    compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
1056}
1057DEFINE_CCW_MACHINE(2_12, "2.12", false);
1058
1059static void ccw_machine_2_11_instance_options(MachineState *machine)
1060{
1061    static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V2_11 };
1062    ccw_machine_2_12_instance_options(machine);
1063
1064    /* before 2.12 we emulated the very first z900 */
1065    s390_set_qemu_cpu_model(0x2064, 7, 1, qemu_cpu_feat);
1066}
1067
1068static void ccw_machine_2_11_class_options(MachineClass *mc)
1069{
1070    static GlobalProperty compat[] = {
1071        { TYPE_SCLP_EVENT_FACILITY, "allow_all_mask_sizes", "off", },
1072    };
1073
1074    ccw_machine_2_12_class_options(mc);
1075    compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
1076    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1077}
1078DEFINE_CCW_MACHINE(2_11, "2.11", false);
1079
1080static void ccw_machine_2_10_instance_options(MachineState *machine)
1081{
1082    ccw_machine_2_11_instance_options(machine);
1083}
1084
1085static void ccw_machine_2_10_class_options(MachineClass *mc)
1086{
1087    ccw_machine_2_11_class_options(mc);
1088    compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
1089}
1090DEFINE_CCW_MACHINE(2_10, "2.10", false);
1091
1092static void ccw_machine_2_9_instance_options(MachineState *machine)
1093{
1094    ccw_machine_2_10_instance_options(machine);
1095    s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP);
1096    s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2);
1097    s390_cpudef_featoff_greater(12, 1, S390_FEAT_ZPCI);
1098    s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_INT_SUPPRESSION);
1099    s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_EVENT_NOTIFICATION);
1100}
1101
1102static void ccw_machine_2_9_class_options(MachineClass *mc)
1103{
1104    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1105    static GlobalProperty compat[] = {
1106        { TYPE_S390_STATTRIB, "migration-enabled", "off", },
1107    };
1108
1109    ccw_machine_2_10_class_options(mc);
1110    compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
1111    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1112    s390mc->css_migration_enabled = false;
1113}
1114DEFINE_CCW_MACHINE(2_9, "2.9", false);
1115
1116static void ccw_machine_2_8_instance_options(MachineState *machine)
1117{
1118    ccw_machine_2_9_instance_options(machine);
1119}
1120
1121static void ccw_machine_2_8_class_options(MachineClass *mc)
1122{
1123    static GlobalProperty compat[] = {
1124        { TYPE_S390_FLIC_COMMON, "adapter_routes_max_batch", "64", },
1125    };
1126
1127    ccw_machine_2_9_class_options(mc);
1128    compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
1129    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1130}
1131DEFINE_CCW_MACHINE(2_8, "2.8", false);
1132
1133static void ccw_machine_2_7_instance_options(MachineState *machine)
1134{
1135    ccw_machine_2_8_instance_options(machine);
1136}
1137
1138static void ccw_machine_2_7_class_options(MachineClass *mc)
1139{
1140    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1141
1142    s390mc->cpu_model_allowed = false;
1143    ccw_machine_2_8_class_options(mc);
1144    compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
1145}
1146DEFINE_CCW_MACHINE(2_7, "2.7", false);
1147
1148static void ccw_machine_2_6_instance_options(MachineState *machine)
1149{
1150    ccw_machine_2_7_instance_options(machine);
1151}
1152
1153static void ccw_machine_2_6_class_options(MachineClass *mc)
1154{
1155    S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1156    static GlobalProperty compat[] = {
1157        { TYPE_S390_IPL, "iplbext_migration", "off", },
1158         { TYPE_VIRTUAL_CSS_BRIDGE, "css_dev_path", "off", },
1159    };
1160
1161    s390mc->ri_allowed = false;
1162    ccw_machine_2_7_class_options(mc);
1163    compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
1164    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1165}
1166DEFINE_CCW_MACHINE(2_6, "2.6", false);
1167
1168static void ccw_machine_2_5_instance_options(MachineState *machine)
1169{
1170    ccw_machine_2_6_instance_options(machine);
1171}
1172
1173static void ccw_machine_2_5_class_options(MachineClass *mc)
1174{
1175    ccw_machine_2_6_class_options(mc);
1176    compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len);
1177}
1178DEFINE_CCW_MACHINE(2_5, "2.5", false);
1179
1180static void ccw_machine_2_4_instance_options(MachineState *machine)
1181{
1182    ccw_machine_2_5_instance_options(machine);
1183}
1184
1185static void ccw_machine_2_4_class_options(MachineClass *mc)
1186{
1187    static GlobalProperty compat[] = {
1188        { TYPE_S390_SKEYS, "migration-enabled", "off", },
1189        { "virtio-blk-ccw", "max_revision", "0", },
1190        { "virtio-balloon-ccw", "max_revision", "0", },
1191        { "virtio-serial-ccw", "max_revision", "0", },
1192        { "virtio-9p-ccw", "max_revision", "0", },
1193        { "virtio-rng-ccw", "max_revision", "0", },
1194        { "virtio-net-ccw", "max_revision", "0", },
1195        { "virtio-scsi-ccw", "max_revision", "0", },
1196        { "vhost-scsi-ccw", "max_revision", "0", },
1197    };
1198
1199    ccw_machine_2_5_class_options(mc);
1200    compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len);
1201    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1202}
1203DEFINE_CCW_MACHINE(2_4, "2.4", false);
1204
1205static void ccw_machine_register_types(void)
1206{
1207    type_register_static(&ccw_machine_info);
1208}
1209
1210type_init(ccw_machine_register_types)
1211