4 * Copyright 2012 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
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
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"
19 #include "virtio-ccw.h"
20 #include "qemu/config-file.h"
21 #include "s390-pci-bus.h"
23 #define TYPE_S390_CCW_MACHINE "s390-ccw-machine"
25 void io_subsystem_reset(void)
27 DeviceState
*css
, *sclp
, *flic
;
29 css
= DEVICE(object_resolve_path_type("", "virtual-css-bridge", NULL
));
33 sclp
= DEVICE(object_resolve_path_type("",
34 "s390-sclp-event-facility", NULL
));
38 flic
= DEVICE(object_resolve_path_type("", "s390-flic", NULL
));
44 static int virtio_ccw_hcall_notify(const uint64_t *args
)
46 uint64_t subch_id
= args
[0];
47 uint64_t queue
= args
[1];
49 int cssid
, ssid
, schid
, m
;
51 if (ioinst_disassemble_sch_ident(subch_id
, &m
, &cssid
, &ssid
, &schid
)) {
54 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
55 if (!sch
|| !css_subch_visible(sch
)) {
58 if (queue
>= VIRTIO_PCI_QUEUE_MAX
) {
61 virtio_queue_notify(virtio_ccw_get_vdev(sch
), queue
);
66 static int virtio_ccw_hcall_early_printk(const uint64_t *args
)
68 uint64_t mem
= args
[0];
77 static void virtio_ccw_register_hcalls(void)
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
);
86 static void ccw_init(MachineState
*machine
)
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
;
94 VirtualCssBus
*css_bus
;
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
;
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
++;
110 while ((standby_mem_size
>> mhd
->increment_size
) > MAX_STORAGE_INCREMENTS
) {
111 mhd
->increment_size
++;
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
;
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
;
127 ret
= s390_set_memory_limit(machine
->maxram_size
, &kvm_limit
);
129 hw_error("qemu: host supports a maximum of %" PRIu64
" GB",
132 hw_error("qemu: setting the guest size failed");
136 css_bus
= virtual_css_bus_init();
138 s390_init_ipl_dev(machine
->kernel_filename
, machine
->kernel_cmdline
,
139 machine
->initrd_filename
, "s390-ccw.img", true);
142 dev
= qdev_create(NULL
, TYPE_S390_PCI_HOST_BRIDGE
);
143 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE
,
145 qdev_init_nofail(dev
);
147 /* register hypercalls */
148 virtio_ccw_register_hcalls();
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
);
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
;
161 my_ram_size
+= standby_mem_size
+ pad_size
;
162 mhd
->pad_size
= pad_size
;
163 mhd
->standby_mem_size
= standby_mem_size
;
166 /* allocate storage keys */
167 storage_keys
= g_malloc0(my_ram_size
/ TARGET_PAGE_SIZE
);
170 s390_init_cpus(machine
->cpu_model
, storage_keys
);
173 kvm_s390_enable_css_support(s390_cpu_addr2state(0));
176 * Create virtual css and set it as default so that non mcss-e
177 * enabled guests only see virtio devices.
179 ret
= css_create_css_image(VIRTUAL_CSSID
, true);
182 /* Create VirtIO network adapters */
183 s390_create_virtio_net(BUS(css_bus
), "virtio-net-ccw");
186 static void ccw_machine_class_init(ObjectClass
*oc
, void *data
)
188 MachineClass
*mc
= MACHINE_CLASS(oc
);
189 NMIClass
*nc
= NMI_CLASS(oc
);
191 mc
->name
= "s390-ccw-virtio";
192 mc
->alias
= "s390-ccw";
193 mc
->desc
= "VirtIO-ccw based S390 machine";
195 mc
->block_default_type
= IF_VIRTIO
;
203 nc
->nmi_monitor_handler
= s390_nmi
;
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
[]) {
216 static void ccw_machine_register_types(void)
218 type_register_static(&ccw_machine_info
);
221 type_init(ccw_machine_register_types
)