xref: /openbmc/qemu/hw/s390x/s390-virtio-ccw.c (revision a06e4355)
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 
101     /* The storage increment size is a multiple of 1M and is a power of 2.
102      * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer.
103      * The variable 'mhd->increment_size' is an exponent of 2 that can be
104      * used to calculate the size (in bytes) of an increment. */
105     mhd->increment_size = 20;
106     while ((my_ram_size >> mhd->increment_size) > MAX_STORAGE_INCREMENTS) {
107         mhd->increment_size++;
108     }
109     while ((standby_mem_size >> mhd->increment_size) > MAX_STORAGE_INCREMENTS) {
110         mhd->increment_size++;
111     }
112 
113     /* The core and standby memory areas need to be aligned with
114      * the increment size.  In effect, this can cause the
115      * user-specified memory size to be rounded down to align
116      * with the nearest increment boundary. */
117     standby_mem_size = standby_mem_size >> mhd->increment_size
118                                         << mhd->increment_size;
119     my_ram_size = my_ram_size >> mhd->increment_size
120                               << mhd->increment_size;
121 
122     /* let's propagate the changed ram size into the global variable. */
123     ram_size = my_ram_size;
124 
125     /* get a BUS */
126     css_bus = virtual_css_bus_init();
127     s390_sclp_init();
128     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
129                       machine->initrd_filename, "s390-ccw.img");
130     s390_flic_init();
131 
132     dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
133     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
134                               OBJECT(dev), NULL);
135     qdev_init_nofail(dev);
136 
137     /* register hypercalls */
138     virtio_ccw_register_hcalls();
139 
140     /* allocate RAM for core */
141     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size, &error_abort);
142     vmstate_register_ram_global(ram);
143     memory_region_add_subregion(sysmem, 0, ram);
144 
145     /* If the size of ram is not on a MEM_SECTION_SIZE boundary,
146        calculate the pad size necessary to force this boundary. */
147     if (standby_mem_size) {
148         if (my_ram_size % MEM_SECTION_SIZE) {
149             pad_size = MEM_SECTION_SIZE - my_ram_size % MEM_SECTION_SIZE;
150         }
151         my_ram_size += standby_mem_size + pad_size;
152         mhd->pad_size = pad_size;
153         mhd->standby_mem_size = standby_mem_size;
154     }
155 
156     /* allocate storage keys */
157     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
158 
159     /* init CPUs */
160     s390_init_cpus(machine->cpu_model, storage_keys);
161 
162     if (kvm_enabled()) {
163         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
164     }
165     /*
166      * Create virtual css and set it as default so that non mcss-e
167      * enabled guests only see virtio devices.
168      */
169     ret = css_create_css_image(VIRTUAL_CSSID, true);
170     assert(ret == 0);
171 
172     /* Create VirtIO network adapters */
173     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
174 }
175 
176 static void ccw_machine_class_init(ObjectClass *oc, void *data)
177 {
178     MachineClass *mc = MACHINE_CLASS(oc);
179     NMIClass *nc = NMI_CLASS(oc);
180 
181     mc->name = "s390-ccw-virtio";
182     mc->alias = "s390-ccw";
183     mc->desc = "VirtIO-ccw based S390 machine";
184     mc->init = ccw_init;
185     mc->block_default_type = IF_VIRTIO;
186     mc->no_cdrom = 1;
187     mc->no_floppy = 1;
188     mc->no_serial = 1;
189     mc->no_parallel = 1;
190     mc->no_sdcard = 1;
191     mc->use_sclp = 1;
192     mc->max_cpus = 255;
193     nc->nmi_monitor_handler = s390_nmi;
194 }
195 
196 static const TypeInfo ccw_machine_info = {
197     .name          = TYPE_S390_CCW_MACHINE,
198     .parent        = TYPE_MACHINE,
199     .class_init    = ccw_machine_class_init,
200     .interfaces = (InterfaceInfo[]) {
201         { TYPE_NMI },
202         { }
203     },
204 };
205 
206 static void ccw_machine_register_types(void)
207 {
208     type_register_static(&ccw_machine_info);
209 }
210 
211 type_init(ccw_machine_register_types)
212