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