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