xref: /openbmc/qemu/hw/s390x/s390-virtio-ccw.c (revision 93f7c4f0)
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 #include "qemu/config-file.h"
21 #include "s390-pci-bus.h"
22 
23 #define TYPE_S390_CCW_MACHINE               "s390-ccw-machine"
24 
25 void io_subsystem_reset(void)
26 {
27     DeviceState *css, *sclp, *flic;
28 
29     css = DEVICE(object_resolve_path_type("", "virtual-css-bridge", NULL));
30     if (css) {
31         qdev_reset_all(css);
32     }
33     sclp = DEVICE(object_resolve_path_type("",
34                   "s390-sclp-event-facility", NULL));
35     if (sclp) {
36         qdev_reset_all(sclp);
37     }
38     flic = DEVICE(object_resolve_path_type("", "s390-flic", NULL));
39     if (flic) {
40         qdev_reset_all(flic);
41     }
42 }
43 
44 static int virtio_ccw_hcall_notify(const uint64_t *args)
45 {
46     uint64_t subch_id = args[0];
47     uint64_t queue = args[1];
48     SubchDev *sch;
49     int cssid, ssid, schid, m;
50 
51     if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
52         return -EINVAL;
53     }
54     sch = css_find_subch(m, cssid, ssid, schid);
55     if (!sch || !css_subch_visible(sch)) {
56         return -EINVAL;
57     }
58     if (queue >= VIRTIO_PCI_QUEUE_MAX) {
59         return -EINVAL;
60     }
61     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
62     return 0;
63 
64 }
65 
66 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
67 {
68     uint64_t mem = args[0];
69 
70     if (mem < ram_size) {
71         /* Early printk */
72         return 0;
73     }
74     return -EINVAL;
75 }
76 
77 static void virtio_ccw_register_hcalls(void)
78 {
79     s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
80                                    virtio_ccw_hcall_notify);
81     /* Tolerate early printk. */
82     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
83                                    virtio_ccw_hcall_early_printk);
84 }
85 
86 static void ccw_init(MachineState *machine)
87 {
88     ram_addr_t my_ram_size = machine->ram_size;
89     MemoryRegion *sysmem = get_system_memory();
90     MemoryRegion *ram = g_new(MemoryRegion, 1);
91     sclpMemoryHotplugDev *mhd = init_sclp_memory_hotplug_dev();
92     uint8_t *storage_keys;
93     int ret;
94     VirtualCssBus *css_bus;
95     DeviceState *dev;
96     QemuOpts *opts = qemu_opts_find(qemu_find_opts("memory"), NULL);
97     ram_addr_t pad_size = 0;
98     ram_addr_t maxmem = qemu_opt_get_size(opts, "maxmem", my_ram_size);
99     ram_addr_t standby_mem_size = maxmem - my_ram_size;
100     uint64_t kvm_limit;
101 
102     /* The storage increment size is a multiple of 1M and is a power of 2.
103      * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer.
104      * The variable 'mhd->increment_size' is an exponent of 2 that can be
105      * used to calculate the size (in bytes) of an increment. */
106     mhd->increment_size = 20;
107     while ((my_ram_size >> mhd->increment_size) > MAX_STORAGE_INCREMENTS) {
108         mhd->increment_size++;
109     }
110     while ((standby_mem_size >> mhd->increment_size) > MAX_STORAGE_INCREMENTS) {
111         mhd->increment_size++;
112     }
113 
114     /* The core and standby memory areas need to be aligned with
115      * the increment size.  In effect, this can cause the
116      * user-specified memory size to be rounded down to align
117      * with the nearest increment boundary. */
118     standby_mem_size = standby_mem_size >> mhd->increment_size
119                                         << mhd->increment_size;
120     my_ram_size = my_ram_size >> mhd->increment_size
121                               << mhd->increment_size;
122 
123     /* let's propagate the changed ram size into the global variable. */
124     ram_size = my_ram_size;
125     machine->maxram_size = my_ram_size + standby_mem_size;
126 
127     ret = s390_set_memory_limit(machine->maxram_size, &kvm_limit);
128     if (ret == -E2BIG) {
129         hw_error("qemu: host supports a maximum of %" PRIu64 " GB",
130                  kvm_limit >> 30);
131     } else if (ret) {
132         hw_error("qemu: setting the guest size failed");
133     }
134 
135     /* get a BUS */
136     css_bus = virtual_css_bus_init();
137     s390_sclp_init();
138     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
139                       machine->initrd_filename, "s390-ccw.img", true);
140     s390_flic_init();
141 
142     dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
143     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
144                               OBJECT(dev), NULL);
145     qdev_init_nofail(dev);
146 
147     /* register hypercalls */
148     virtio_ccw_register_hcalls();
149 
150     /* allocate RAM for core */
151     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size, &error_abort);
152     vmstate_register_ram_global(ram);
153     memory_region_add_subregion(sysmem, 0, ram);
154 
155     /* If the size of ram is not on a MEM_SECTION_SIZE boundary,
156        calculate the pad size necessary to force this boundary. */
157     if (standby_mem_size) {
158         if (my_ram_size % MEM_SECTION_SIZE) {
159             pad_size = MEM_SECTION_SIZE - my_ram_size % MEM_SECTION_SIZE;
160         }
161         my_ram_size += standby_mem_size + pad_size;
162         mhd->pad_size = pad_size;
163         mhd->standby_mem_size = standby_mem_size;
164     }
165 
166     /* allocate storage keys */
167     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
168 
169     /* init CPUs */
170     s390_init_cpus(machine->cpu_model, storage_keys);
171 
172     if (kvm_enabled()) {
173         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
174     }
175     /*
176      * Create virtual css and set it as default so that non mcss-e
177      * enabled guests only see virtio devices.
178      */
179     ret = css_create_css_image(VIRTUAL_CSSID, true);
180     assert(ret == 0);
181 
182     /* Create VirtIO network adapters */
183     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
184 }
185 
186 static void ccw_machine_class_init(ObjectClass *oc, void *data)
187 {
188     MachineClass *mc = MACHINE_CLASS(oc);
189     NMIClass *nc = NMI_CLASS(oc);
190 
191     mc->name = "s390-ccw-virtio";
192     mc->alias = "s390-ccw";
193     mc->desc = "VirtIO-ccw based S390 machine";
194     mc->init = ccw_init;
195     mc->block_default_type = IF_VIRTIO;
196     mc->no_cdrom = 1;
197     mc->no_floppy = 1;
198     mc->no_serial = 1;
199     mc->no_parallel = 1;
200     mc->no_sdcard = 1;
201     mc->use_sclp = 1;
202     mc->max_cpus = 255;
203     nc->nmi_monitor_handler = s390_nmi;
204 }
205 
206 static const TypeInfo ccw_machine_info = {
207     .name          = TYPE_S390_CCW_MACHINE,
208     .parent        = TYPE_MACHINE,
209     .class_init    = ccw_machine_class_init,
210     .interfaces = (InterfaceInfo[]) {
211         { TYPE_NMI },
212         { }
213     },
214 };
215 
216 static void ccw_machine_register_types(void)
217 {
218     type_register_static(&ccw_machine_info);
219 }
220 
221 type_init(ccw_machine_register_types)
222