xref: /qemu/hw/s390x/s390-virtio-ccw.c (revision 72ac97cd)
1 /*
2  * virtio ccw machine
3  *
4  * Copyright 2012 IBM Corp.
5  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or (at
8  * your option) any later version. See the COPYING file in the top-level
9  * directory.
10  */
11 
12 #include "hw/boards.h"
13 #include "exec/address-spaces.h"
14 #include "s390-virtio.h"
15 #include "hw/s390x/sclp.h"
16 #include "hw/s390x/s390_flic.h"
17 #include "ioinst.h"
18 #include "css.h"
19 #include "virtio-ccw.h"
20 
21 void io_subsystem_reset(void)
22 {
23     DeviceState *css, *sclp, *flic;
24 
25     css = DEVICE(object_resolve_path_type("", "virtual-css-bridge", NULL));
26     if (css) {
27         qdev_reset_all(css);
28     }
29     sclp = DEVICE(object_resolve_path_type("",
30                   "s390-sclp-event-facility", NULL));
31     if (sclp) {
32         qdev_reset_all(sclp);
33     }
34     flic = DEVICE(object_resolve_path_type("", "s390-flic", NULL));
35     if (flic) {
36         qdev_reset_all(flic);
37     }
38 }
39 
40 static int virtio_ccw_hcall_notify(const uint64_t *args)
41 {
42     uint64_t subch_id = args[0];
43     uint64_t queue = args[1];
44     SubchDev *sch;
45     int cssid, ssid, schid, m;
46 
47     if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
48         return -EINVAL;
49     }
50     sch = css_find_subch(m, cssid, ssid, schid);
51     if (!sch || !css_subch_visible(sch)) {
52         return -EINVAL;
53     }
54     if (queue >= VIRTIO_PCI_QUEUE_MAX) {
55         return -EINVAL;
56     }
57     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
58     return 0;
59 
60 }
61 
62 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
63 {
64     uint64_t mem = args[0];
65 
66     if (mem < ram_size) {
67         /* Early printk */
68         return 0;
69     }
70     return -EINVAL;
71 }
72 
73 static void virtio_ccw_register_hcalls(void)
74 {
75     s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
76                                    virtio_ccw_hcall_notify);
77     /* Tolerate early printk. */
78     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
79                                    virtio_ccw_hcall_early_printk);
80 }
81 
82 static void ccw_init(MachineState *machine)
83 {
84     ram_addr_t my_ram_size = machine->ram_size;
85     MemoryRegion *sysmem = get_system_memory();
86     MemoryRegion *ram = g_new(MemoryRegion, 1);
87     int shift = 0;
88     uint8_t *storage_keys;
89     int ret;
90     VirtualCssBus *css_bus;
91 
92     /* s390x ram size detection needs a 16bit multiplier + an increment. So
93        guests > 64GB can be specified in 2MB steps etc. */
94     while ((my_ram_size >> (20 + shift)) > 65535) {
95         shift++;
96     }
97     my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
98 
99     /* let's propagate the changed ram size into the global variable. */
100     ram_size = my_ram_size;
101 
102     /* get a BUS */
103     css_bus = virtual_css_bus_init();
104     s390_sclp_init();
105     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
106                       machine->initrd_filename, "s390-ccw.img");
107     s390_flic_init();
108 
109     /* register hypercalls */
110     virtio_ccw_register_hcalls();
111 
112     /* allocate RAM */
113     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size);
114     vmstate_register_ram_global(ram);
115     memory_region_add_subregion(sysmem, 0, ram);
116 
117     /* allocate storage keys */
118     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
119 
120     /* init CPUs */
121     s390_init_cpus(machine->cpu_model, storage_keys);
122 
123     if (kvm_enabled()) {
124         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
125     }
126     /*
127      * Create virtual css and set it as default so that non mcss-e
128      * enabled guests only see virtio devices.
129      */
130     ret = css_create_css_image(VIRTUAL_CSSID, true);
131     assert(ret == 0);
132 
133     /* Create VirtIO network adapters */
134     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
135 }
136 
137 static QEMUMachine ccw_machine = {
138     .name = "s390-ccw-virtio",
139     .alias = "s390-ccw",
140     .desc = "VirtIO-ccw based S390 machine",
141     .init = ccw_init,
142     .block_default_type = IF_VIRTIO,
143     .no_cdrom = 1,
144     .no_floppy = 1,
145     .no_serial = 1,
146     .no_parallel = 1,
147     .no_sdcard = 1,
148     .use_sclp = 1,
149     .max_cpus = 255,
150 };
151 
152 static void ccw_machine_init(void)
153 {
154     qemu_register_machine(&ccw_machine);
155 }
156 
157 machine_init(ccw_machine_init)
158