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
;
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
++;
109 while ((standby_mem_size
>> mhd
->increment_size
) > MAX_STORAGE_INCREMENTS
) {
110 mhd
->increment_size
++;
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
;
122 /* let's propagate the changed ram size into the global variable. */
123 ram_size
= my_ram_size
;
126 css_bus
= virtual_css_bus_init();
128 s390_init_ipl_dev(machine
->kernel_filename
, machine
->kernel_cmdline
,
129 machine
->initrd_filename
, "s390-ccw.img");
132 dev
= qdev_create(NULL
, TYPE_S390_PCI_HOST_BRIDGE
);
133 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE
,
135 qdev_init_nofail(dev
);
137 /* register hypercalls */
138 virtio_ccw_register_hcalls();
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
);
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
;
151 my_ram_size
+= standby_mem_size
+ pad_size
;
152 mhd
->pad_size
= pad_size
;
153 mhd
->standby_mem_size
= standby_mem_size
;
156 /* allocate storage keys */
157 storage_keys
= g_malloc0(my_ram_size
/ TARGET_PAGE_SIZE
);
160 s390_init_cpus(machine
->cpu_model
, storage_keys
);
163 kvm_s390_enable_css_support(s390_cpu_addr2state(0));
166 * Create virtual css and set it as default so that non mcss-e
167 * enabled guests only see virtio devices.
169 ret
= css_create_css_image(VIRTUAL_CSSID
, true);
172 /* Create VirtIO network adapters */
173 s390_create_virtio_net(BUS(css_bus
), "virtio-net-ccw");
176 static void ccw_machine_class_init(ObjectClass
*oc
, void *data
)
178 MachineClass
*mc
= MACHINE_CLASS(oc
);
179 NMIClass
*nc
= NMI_CLASS(oc
);
181 mc
->name
= "s390-ccw-virtio";
182 mc
->alias
= "s390-ccw";
183 mc
->desc
= "VirtIO-ccw based S390 machine";
185 mc
->block_default_type
= IF_VIRTIO
;
193 nc
->nmi_monitor_handler
= s390_nmi
;
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
[]) {
206 static void ccw_machine_register_types(void)
208 type_register_static(&ccw_machine_info
);
211 type_init(ccw_machine_register_types
)