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 "qemu/osdep.h" 13 #include "hw/boards.h" 14 #include "exec/address-spaces.h" 15 #include "s390-virtio.h" 16 #include "hw/s390x/sclp.h" 17 #include "hw/s390x/s390_flic.h" 18 #include "ioinst.h" 19 #include "css.h" 20 #include "virtio-ccw.h" 21 #include "qemu/config-file.h" 22 #include "s390-pci-bus.h" 23 #include "hw/s390x/storage-keys.h" 24 #include "hw/compat.h" 25 26 #define TYPE_S390_CCW_MACHINE "s390-ccw-machine" 27 28 #define S390_CCW_MACHINE(obj) \ 29 OBJECT_CHECK(S390CcwMachineState, (obj), TYPE_S390_CCW_MACHINE) 30 31 typedef struct S390CcwMachineState { 32 /*< private >*/ 33 MachineState parent_obj; 34 35 /*< public >*/ 36 bool aes_key_wrap; 37 bool dea_key_wrap; 38 } S390CcwMachineState; 39 40 static const char *const reset_dev_types[] = { 41 "virtual-css-bridge", 42 "s390-sclp-event-facility", 43 "s390-flic", 44 "diag288", 45 }; 46 47 void subsystem_reset(void) 48 { 49 DeviceState *dev; 50 int i; 51 52 for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) { 53 dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL)); 54 if (dev) { 55 qdev_reset_all(dev); 56 } 57 } 58 } 59 60 static int virtio_ccw_hcall_notify(const uint64_t *args) 61 { 62 uint64_t subch_id = args[0]; 63 uint64_t queue = args[1]; 64 SubchDev *sch; 65 int cssid, ssid, schid, m; 66 67 if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) { 68 return -EINVAL; 69 } 70 sch = css_find_subch(m, cssid, ssid, schid); 71 if (!sch || !css_subch_visible(sch)) { 72 return -EINVAL; 73 } 74 if (queue >= VIRTIO_CCW_QUEUE_MAX) { 75 return -EINVAL; 76 } 77 virtio_queue_notify(virtio_ccw_get_vdev(sch), queue); 78 return 0; 79 80 } 81 82 static int virtio_ccw_hcall_early_printk(const uint64_t *args) 83 { 84 uint64_t mem = args[0]; 85 86 if (mem < ram_size) { 87 /* Early printk */ 88 return 0; 89 } 90 return -EINVAL; 91 } 92 93 static void virtio_ccw_register_hcalls(void) 94 { 95 s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, 96 virtio_ccw_hcall_notify); 97 /* Tolerate early printk. */ 98 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY, 99 virtio_ccw_hcall_early_printk); 100 } 101 102 void s390_memory_init(ram_addr_t mem_size) 103 { 104 MemoryRegion *sysmem = get_system_memory(); 105 MemoryRegion *ram = g_new(MemoryRegion, 1); 106 107 /* allocate RAM for core */ 108 memory_region_allocate_system_memory(ram, NULL, "s390.ram", mem_size); 109 memory_region_add_subregion(sysmem, 0, ram); 110 111 /* Initialize storage key device */ 112 s390_skeys_init(); 113 } 114 115 static void ccw_init(MachineState *machine) 116 { 117 int ret; 118 VirtualCssBus *css_bus; 119 DeviceState *dev; 120 121 s390_sclp_init(); 122 s390_memory_init(machine->ram_size); 123 124 /* get a BUS */ 125 css_bus = virtual_css_bus_init(); 126 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline, 127 machine->initrd_filename, "s390-ccw.img", true); 128 s390_flic_init(); 129 130 dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE); 131 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE, 132 OBJECT(dev), NULL); 133 qdev_init_nofail(dev); 134 135 /* register hypercalls */ 136 virtio_ccw_register_hcalls(); 137 138 /* init CPUs */ 139 s390_init_cpus(machine->cpu_model); 140 141 if (kvm_enabled()) { 142 kvm_s390_enable_css_support(s390_cpu_addr2state(0)); 143 } 144 /* 145 * Create virtual css and set it as default so that non mcss-e 146 * enabled guests only see virtio devices. 147 */ 148 ret = css_create_css_image(VIRTUAL_CSSID, true); 149 assert(ret == 0); 150 151 /* Create VirtIO network adapters */ 152 s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw"); 153 154 /* Register savevm handler for guest TOD clock */ 155 register_savevm(NULL, "todclock", 0, 1, 156 gtod_save, gtod_load, kvm_state); 157 } 158 159 static void ccw_machine_class_init(ObjectClass *oc, void *data) 160 { 161 MachineClass *mc = MACHINE_CLASS(oc); 162 NMIClass *nc = NMI_CLASS(oc); 163 164 mc->init = ccw_init; 165 mc->reset = s390_machine_reset; 166 mc->block_default_type = IF_VIRTIO; 167 mc->no_cdrom = 1; 168 mc->no_floppy = 1; 169 mc->no_serial = 1; 170 mc->no_parallel = 1; 171 mc->no_sdcard = 1; 172 mc->use_sclp = 1; 173 mc->max_cpus = 255; 174 nc->nmi_monitor_handler = s390_nmi; 175 } 176 177 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp) 178 { 179 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 180 181 return ms->aes_key_wrap; 182 } 183 184 static inline void machine_set_aes_key_wrap(Object *obj, bool value, 185 Error **errp) 186 { 187 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 188 189 ms->aes_key_wrap = value; 190 } 191 192 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp) 193 { 194 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 195 196 return ms->dea_key_wrap; 197 } 198 199 static inline void machine_set_dea_key_wrap(Object *obj, bool value, 200 Error **errp) 201 { 202 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 203 204 ms->dea_key_wrap = value; 205 } 206 207 static inline void s390_machine_initfn(Object *obj) 208 { 209 object_property_add_bool(obj, "aes-key-wrap", 210 machine_get_aes_key_wrap, 211 machine_set_aes_key_wrap, NULL); 212 object_property_set_description(obj, "aes-key-wrap", 213 "enable/disable AES key wrapping using the CPACF wrapping key", 214 NULL); 215 object_property_set_bool(obj, true, "aes-key-wrap", NULL); 216 217 object_property_add_bool(obj, "dea-key-wrap", 218 machine_get_dea_key_wrap, 219 machine_set_dea_key_wrap, NULL); 220 object_property_set_description(obj, "dea-key-wrap", 221 "enable/disable DEA key wrapping using the CPACF wrapping key", 222 NULL); 223 object_property_set_bool(obj, true, "dea-key-wrap", NULL); 224 } 225 226 static const TypeInfo ccw_machine_info = { 227 .name = TYPE_S390_CCW_MACHINE, 228 .parent = TYPE_MACHINE, 229 .abstract = true, 230 .instance_size = sizeof(S390CcwMachineState), 231 .instance_init = s390_machine_initfn, 232 .class_init = ccw_machine_class_init, 233 .interfaces = (InterfaceInfo[]) { 234 { TYPE_NMI }, 235 { } 236 }, 237 }; 238 239 #define CCW_COMPAT_2_5 \ 240 HW_COMPAT_2_5 241 242 #define CCW_COMPAT_2_4 \ 243 CCW_COMPAT_2_5 \ 244 HW_COMPAT_2_4 \ 245 {\ 246 .driver = TYPE_S390_SKEYS,\ 247 .property = "migration-enabled",\ 248 .value = "off",\ 249 },{\ 250 .driver = "virtio-blk-ccw",\ 251 .property = "max_revision",\ 252 .value = "0",\ 253 },{\ 254 .driver = "virtio-balloon-ccw",\ 255 .property = "max_revision",\ 256 .value = "0",\ 257 },{\ 258 .driver = "virtio-serial-ccw",\ 259 .property = "max_revision",\ 260 .value = "0",\ 261 },{\ 262 .driver = "virtio-9p-ccw",\ 263 .property = "max_revision",\ 264 .value = "0",\ 265 },{\ 266 .driver = "virtio-rng-ccw",\ 267 .property = "max_revision",\ 268 .value = "0",\ 269 },{\ 270 .driver = "virtio-net-ccw",\ 271 .property = "max_revision",\ 272 .value = "0",\ 273 },{\ 274 .driver = "virtio-scsi-ccw",\ 275 .property = "max_revision",\ 276 .value = "0",\ 277 },{\ 278 .driver = "vhost-scsi-ccw",\ 279 .property = "max_revision",\ 280 .value = "0",\ 281 }, 282 283 static void ccw_machine_2_4_class_init(ObjectClass *oc, void *data) 284 { 285 MachineClass *mc = MACHINE_CLASS(oc); 286 static GlobalProperty compat_props[] = { 287 CCW_COMPAT_2_4 288 { /* end of list */ } 289 }; 290 291 mc->desc = "VirtIO-ccw based S390 machine v2.4"; 292 mc->compat_props = compat_props; 293 } 294 295 static const TypeInfo ccw_machine_2_4_info = { 296 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-2.4"), 297 .parent = TYPE_S390_CCW_MACHINE, 298 .class_init = ccw_machine_2_4_class_init, 299 }; 300 301 static void ccw_machine_2_5_class_init(ObjectClass *oc, void *data) 302 { 303 MachineClass *mc = MACHINE_CLASS(oc); 304 static GlobalProperty compat_props[] = { 305 CCW_COMPAT_2_5 306 { /* end of list */ } 307 }; 308 309 mc->desc = "VirtIO-ccw based S390 machine v2.5"; 310 mc->compat_props = compat_props; 311 } 312 313 static const TypeInfo ccw_machine_2_5_info = { 314 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-2.5"), 315 .parent = TYPE_S390_CCW_MACHINE, 316 .class_init = ccw_machine_2_5_class_init, 317 }; 318 319 static void ccw_machine_2_6_class_init(ObjectClass *oc, void *data) 320 { 321 MachineClass *mc = MACHINE_CLASS(oc); 322 323 mc->alias = "s390-ccw-virtio"; 324 mc->desc = "VirtIO-ccw based S390 machine v2.6"; 325 mc->is_default = 1; 326 } 327 328 static const TypeInfo ccw_machine_2_6_info = { 329 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-2.6"), 330 .parent = TYPE_S390_CCW_MACHINE, 331 .class_init = ccw_machine_2_6_class_init, 332 }; 333 334 static void ccw_machine_register_types(void) 335 { 336 type_register_static(&ccw_machine_info); 337 type_register_static(&ccw_machine_2_4_info); 338 type_register_static(&ccw_machine_2_5_info); 339 type_register_static(&ccw_machine_2_6_info); 340 } 341 342 type_init(ccw_machine_register_types) 343