xref: /openbmc/qemu/hw/s390x/ipl.c (revision effd60c8)
1 /*
2  * bootloader support
3  *
4  * Copyright IBM, Corp. 2012, 2020
5  *
6  * Authors:
7  *  Christian Borntraeger <borntraeger@de.ibm.com>
8  *  Janosch Frank <frankja@linux.ibm.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
11  * option) any later version.  See the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qemu/datadir.h"
17 #include "qapi/error.h"
18 #include "sysemu/reset.h"
19 #include "sysemu/runstate.h"
20 #include "sysemu/tcg.h"
21 #include "elf.h"
22 #include "hw/loader.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/boards.h"
25 #include "hw/s390x/virtio-ccw.h"
26 #include "hw/s390x/vfio-ccw.h"
27 #include "hw/s390x/css.h"
28 #include "hw/s390x/ebcdic.h"
29 #include "target/s390x/kvm/pv.h"
30 #include "hw/scsi/scsi.h"
31 #include "hw/virtio/virtio-net.h"
32 #include "ipl.h"
33 #include "qemu/error-report.h"
34 #include "qemu/config-file.h"
35 #include "qemu/cutils.h"
36 #include "qemu/option.h"
37 #include "standard-headers/linux/virtio_ids.h"
38 
39 #define KERN_IMAGE_START                0x010000UL
40 #define LINUX_MAGIC_ADDR                0x010008UL
41 #define KERN_PARM_AREA_SIZE_ADDR        0x010430UL
42 #define KERN_PARM_AREA                  0x010480UL
43 #define LEGACY_KERN_PARM_AREA_SIZE      0x000380UL
44 #define INITRD_START                    0x800000UL
45 #define INITRD_PARM_START               0x010408UL
46 #define PARMFILE_START                  0x001000UL
47 #define ZIPL_IMAGE_START                0x009000UL
48 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
49 
50 static bool iplb_extended_needed(void *opaque)
51 {
52     S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
53 
54     return ipl->iplbext_migration;
55 }
56 
57 static const VMStateDescription vmstate_iplb_extended = {
58     .name = "ipl/iplb_extended",
59     .version_id = 0,
60     .minimum_version_id = 0,
61     .needed = iplb_extended_needed,
62     .fields = (const VMStateField[]) {
63         VMSTATE_UINT8_ARRAY(reserved_ext, IplParameterBlock, 4096 - 200),
64         VMSTATE_END_OF_LIST()
65     }
66 };
67 
68 static const VMStateDescription vmstate_iplb = {
69     .name = "ipl/iplb",
70     .version_id = 0,
71     .minimum_version_id = 0,
72     .fields = (const VMStateField[]) {
73         VMSTATE_UINT8_ARRAY(reserved1, IplParameterBlock, 110),
74         VMSTATE_UINT16(devno, IplParameterBlock),
75         VMSTATE_UINT8_ARRAY(reserved2, IplParameterBlock, 88),
76         VMSTATE_END_OF_LIST()
77     },
78     .subsections = (const VMStateDescription * const []) {
79         &vmstate_iplb_extended,
80         NULL
81     }
82 };
83 
84 static const VMStateDescription vmstate_ipl = {
85     .name = "ipl",
86     .version_id = 0,
87     .minimum_version_id = 0,
88     .fields = (const VMStateField[]) {
89         VMSTATE_UINT64(compat_start_addr, S390IPLState),
90         VMSTATE_UINT64(compat_bios_start_addr, S390IPLState),
91         VMSTATE_STRUCT(iplb, S390IPLState, 0, vmstate_iplb, IplParameterBlock),
92         VMSTATE_BOOL(iplb_valid, S390IPLState),
93         VMSTATE_UINT8(cssid, S390IPLState),
94         VMSTATE_UINT8(ssid, S390IPLState),
95         VMSTATE_UINT16(devno, S390IPLState),
96         VMSTATE_END_OF_LIST()
97      }
98 };
99 
100 static S390IPLState *get_ipl_device(void)
101 {
102     return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL));
103 }
104 
105 static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
106 {
107     uint64_t dstaddr = *(uint64_t *) opaque;
108     /*
109      * Assuming that our s390-ccw.img was linked for starting at address 0,
110      * we can simply add the destination address for the final location
111      */
112     return srcaddr + dstaddr;
113 }
114 
115 static uint64_t get_max_kernel_cmdline_size(void)
116 {
117     uint64_t *size_ptr = rom_ptr(KERN_PARM_AREA_SIZE_ADDR, sizeof(*size_ptr));
118 
119     if (size_ptr) {
120         uint64_t size;
121 
122         size = be64_to_cpu(*size_ptr);
123         if (size) {
124             return size;
125         }
126     }
127     return LEGACY_KERN_PARM_AREA_SIZE;
128 }
129 
130 static void s390_ipl_realize(DeviceState *dev, Error **errp)
131 {
132     MachineState *ms = MACHINE(qdev_get_machine());
133     S390IPLState *ipl = S390_IPL(dev);
134     uint32_t *ipl_psw;
135     uint64_t pentry;
136     char *magic;
137     int kernel_size;
138 
139     int bios_size;
140     char *bios_filename;
141 
142     /*
143      * Always load the bios if it was enforced,
144      * even if an external kernel has been defined.
145      */
146     if (!ipl->kernel || ipl->enforce_bios) {
147         uint64_t fwbase = (MIN(ms->ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
148 
149         bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->firmware);
150         if (bios_filename == NULL) {
151             error_setg(errp, "could not find stage1 bootloader");
152             return;
153         }
154 
155         bios_size = load_elf(bios_filename, NULL,
156                              bios_translate_addr, &fwbase,
157                              &ipl->bios_start_addr, NULL, NULL, NULL, 1,
158                              EM_S390, 0, 0);
159         if (bios_size > 0) {
160             /* Adjust ELF start address to final location */
161             ipl->bios_start_addr += fwbase;
162         } else {
163             /* Try to load non-ELF file */
164             bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
165                                             4096);
166             ipl->bios_start_addr = ZIPL_IMAGE_START;
167         }
168         g_free(bios_filename);
169 
170         if (bios_size == -1) {
171             error_setg(errp, "could not load bootloader '%s'", ipl->firmware);
172             return;
173         }
174 
175         /* default boot target is the bios */
176         ipl->start_addr = ipl->bios_start_addr;
177     }
178 
179     if (ipl->kernel) {
180         kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL,
181                                &pentry, NULL,
182                                NULL, NULL, 1, EM_S390, 0, 0);
183         if (kernel_size < 0) {
184             kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size);
185             if (kernel_size < 0) {
186                 error_setg(errp, "could not load kernel '%s'", ipl->kernel);
187                 return;
188             }
189             /* if this is Linux use KERN_IMAGE_START */
190             magic = rom_ptr(LINUX_MAGIC_ADDR, 6);
191             if (magic && !memcmp(magic, "S390EP", 6)) {
192                 pentry = KERN_IMAGE_START;
193             } else {
194                 /* if not Linux load the address of the (short) IPL PSW */
195                 ipl_psw = rom_ptr(4, 4);
196                 if (ipl_psw) {
197                     pentry = be32_to_cpu(*ipl_psw) & PSW_MASK_SHORT_ADDR;
198                 } else {
199                     error_setg(errp, "Could not get IPL PSW");
200                     return;
201                 }
202             }
203         }
204         /*
205          * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
206          * kernel parameters here as well. Note: For old kernels (up to 3.2)
207          * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
208          * loader) and it won't work. For this case we force it to 0x10000, too.
209          */
210         if (pentry == KERN_IMAGE_START || pentry == 0x800) {
211             size_t cmdline_size = strlen(ipl->cmdline) + 1;
212             char *parm_area = rom_ptr(KERN_PARM_AREA, cmdline_size);
213 
214             ipl->start_addr = KERN_IMAGE_START;
215             /* Overwrite parameters in the kernel image, which are "rom" */
216             if (parm_area) {
217                 uint64_t max_cmdline_size = get_max_kernel_cmdline_size();
218 
219                 if (cmdline_size > max_cmdline_size) {
220                     error_setg(errp,
221                                "kernel command line exceeds maximum size:"
222                                " %zu > %" PRIu64,
223                                cmdline_size, max_cmdline_size);
224                     return;
225                 }
226 
227                 strcpy(parm_area, ipl->cmdline);
228             }
229         } else {
230             ipl->start_addr = pentry;
231         }
232 
233         if (ipl->initrd) {
234             ram_addr_t initrd_offset;
235             int initrd_size;
236             uint64_t *romptr;
237 
238             initrd_offset = INITRD_START;
239             while (kernel_size + 0x100000 > initrd_offset) {
240                 initrd_offset += 0x100000;
241             }
242             initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
243                                               ms->ram_size - initrd_offset);
244             if (initrd_size == -1) {
245                 error_setg(errp, "could not load initrd '%s'", ipl->initrd);
246                 return;
247             }
248 
249             /*
250              * we have to overwrite values in the kernel image,
251              * which are "rom"
252              */
253             romptr = rom_ptr(INITRD_PARM_START, 16);
254             if (romptr) {
255                 stq_p(romptr, initrd_offset);
256                 stq_p(romptr + 1, initrd_size);
257             }
258         }
259     }
260     /*
261      * Don't ever use the migrated values, they could come from a different
262      * BIOS and therefore don't work. But still migrate the values, so
263      * QEMUs relying on it don't break.
264      */
265     ipl->compat_start_addr = ipl->start_addr;
266     ipl->compat_bios_start_addr = ipl->bios_start_addr;
267     /*
268      * Because this Device is not on any bus in the qbus tree (it is
269      * not a sysbus device and it's not on some other bus like a PCI
270      * bus) it will not be automatically reset by the 'reset the
271      * sysbus' hook registered by vl.c like most devices. So we must
272      * manually register a reset hook for it.
273      * TODO: there should be a better way to do this.
274      */
275     qemu_register_reset(resettable_cold_reset_fn, dev);
276 }
277 
278 static Property s390_ipl_properties[] = {
279     DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
280     DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
281     DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
282     DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
283     DEFINE_PROP_STRING("netboot_fw", S390IPLState, netboot_fw),
284     DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
285     DEFINE_PROP_BOOL("iplbext_migration", S390IPLState, iplbext_migration,
286                      true),
287     DEFINE_PROP_END_OF_LIST(),
288 };
289 
290 static void s390_ipl_set_boot_menu(S390IPLState *ipl)
291 {
292     unsigned long splash_time = 0;
293 
294     if (!get_boot_device(0)) {
295         if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) {
296             error_report("boot menu requires a bootindex to be specified for "
297                          "the IPL device");
298         }
299         return;
300     }
301 
302     switch (ipl->iplb.pbt) {
303     case S390_IPL_TYPE_CCW:
304         /* In the absence of -boot menu, use zipl parameters */
305         if (!current_machine->boot_config.has_menu) {
306             ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL;
307             return;
308         }
309         break;
310     case S390_IPL_TYPE_QEMU_SCSI:
311         break;
312     default:
313         if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) {
314             error_report("boot menu is not supported for this device type");
315         }
316         return;
317     }
318 
319     if (!current_machine->boot_config.has_menu || !current_machine->boot_config.menu) {
320         return;
321     }
322 
323     ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
324 
325     if (current_machine->boot_config.has_splash_time) {
326         splash_time = current_machine->boot_config.splash_time;
327     }
328     if (splash_time > 0xffffffff) {
329         error_report("splash-time is too large, forcing it to max value");
330         ipl->qipl.boot_menu_timeout = 0xffffffff;
331         return;
332     }
333 
334     ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
335 }
336 
337 #define CCW_DEVTYPE_NONE        0x00
338 #define CCW_DEVTYPE_VIRTIO      0x01
339 #define CCW_DEVTYPE_VIRTIO_NET  0x02
340 #define CCW_DEVTYPE_SCSI        0x03
341 #define CCW_DEVTYPE_VFIO        0x04
342 
343 static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
344 {
345     CcwDevice *ccw_dev = NULL;
346     int tmp_dt = CCW_DEVTYPE_NONE;
347 
348     if (dev_st) {
349         VirtIONet *virtio_net_dev = (VirtIONet *)
350             object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET);
351         VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *)
352             object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent),
353                                 TYPE_VIRTIO_CCW_DEVICE);
354         VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *)
355             object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW);
356 
357         if (virtio_ccw_dev) {
358             ccw_dev = CCW_DEVICE(virtio_ccw_dev);
359             if (virtio_net_dev) {
360                 tmp_dt = CCW_DEVTYPE_VIRTIO_NET;
361             } else {
362                 tmp_dt = CCW_DEVTYPE_VIRTIO;
363             }
364         } else if (vfio_ccw_dev) {
365             ccw_dev = CCW_DEVICE(vfio_ccw_dev);
366             tmp_dt = CCW_DEVTYPE_VFIO;
367         } else {
368             SCSIDevice *sd = (SCSIDevice *)
369                 object_dynamic_cast(OBJECT(dev_st),
370                                     TYPE_SCSI_DEVICE);
371             if (sd) {
372                 SCSIBus *sbus = scsi_bus_from_device(sd);
373                 VirtIODevice *vdev = (VirtIODevice *)
374                     object_dynamic_cast(OBJECT(sbus->qbus.parent),
375                                         TYPE_VIRTIO_DEVICE);
376                 if (vdev) {
377                     ccw_dev = (CcwDevice *)
378                         object_dynamic_cast(OBJECT(qdev_get_parent_bus(DEVICE(vdev))->parent),
379                                             TYPE_CCW_DEVICE);
380                     if (ccw_dev) {
381                         tmp_dt = CCW_DEVTYPE_SCSI;
382                     }
383                 }
384             }
385         }
386     }
387     if (devtype) {
388         *devtype = tmp_dt;
389     }
390     return ccw_dev;
391 }
392 
393 static bool s390_gen_initial_iplb(S390IPLState *ipl)
394 {
395     DeviceState *dev_st;
396     CcwDevice *ccw_dev = NULL;
397     SCSIDevice *sd;
398     int devtype;
399 
400     dev_st = get_boot_device(0);
401     if (dev_st) {
402         ccw_dev = s390_get_ccw_device(dev_st, &devtype);
403     }
404 
405     /*
406      * Currently allow IPL only from CCW devices.
407      */
408     if (ccw_dev) {
409         switch (devtype) {
410         case CCW_DEVTYPE_SCSI:
411             sd = SCSI_DEVICE(dev_st);
412             ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN);
413             ipl->iplb.blk0_len =
414                 cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN);
415             ipl->iplb.pbt = S390_IPL_TYPE_QEMU_SCSI;
416             ipl->iplb.scsi.lun = cpu_to_be32(sd->lun);
417             ipl->iplb.scsi.target = cpu_to_be16(sd->id);
418             ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
419             ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
420             ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
421             break;
422         case CCW_DEVTYPE_VFIO:
423             ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
424             ipl->iplb.pbt = S390_IPL_TYPE_CCW;
425             ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
426             ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
427             break;
428         case CCW_DEVTYPE_VIRTIO_NET:
429             ipl->netboot = true;
430             /* Fall through to CCW_DEVTYPE_VIRTIO case */
431         case CCW_DEVTYPE_VIRTIO:
432             ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
433             ipl->iplb.blk0_len =
434                 cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN);
435             ipl->iplb.pbt = S390_IPL_TYPE_CCW;
436             ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
437             ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
438             break;
439         }
440 
441         if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
442             ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
443         }
444 
445         return true;
446     }
447 
448     return false;
449 }
450 
451 int s390_ipl_set_loadparm(uint8_t *loadparm)
452 {
453     MachineState *machine = MACHINE(qdev_get_machine());
454     char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
455 
456     if (lp) {
457         int i;
458 
459         /* lp is an uppercase string without leading/embedded spaces */
460         for (i = 0; i < 8 && lp[i]; i++) {
461             loadparm[i] = ascii2ebcdic[(uint8_t) lp[i]];
462         }
463 
464         if (i < 8) {
465             memset(loadparm + i, 0x40, 8 - i); /* fill with EBCDIC spaces */
466         }
467 
468         g_free(lp);
469         return 0;
470     }
471 
472     return -1;
473 }
474 
475 static int load_netboot_image(Error **errp)
476 {
477     MachineState *ms = MACHINE(qdev_get_machine());
478     S390IPLState *ipl = get_ipl_device();
479     char *netboot_filename;
480     MemoryRegion *sysmem =  get_system_memory();
481     MemoryRegion *mr = NULL;
482     void *ram_ptr = NULL;
483     int img_size = -1;
484 
485     mr = memory_region_find(sysmem, 0, 1).mr;
486     if (!mr) {
487         error_setg(errp, "Failed to find memory region at address 0");
488         return -1;
489     }
490 
491     ram_ptr = memory_region_get_ram_ptr(mr);
492     if (!ram_ptr) {
493         error_setg(errp, "No RAM found");
494         goto unref_mr;
495     }
496 
497     netboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->netboot_fw);
498     if (netboot_filename == NULL) {
499         error_setg(errp, "Could not find network bootloader '%s'",
500                    ipl->netboot_fw);
501         goto unref_mr;
502     }
503 
504     img_size = load_elf_ram(netboot_filename, NULL, NULL, NULL,
505                             &ipl->start_addr,
506                             NULL, NULL, NULL, 1, EM_S390, 0, 0, NULL,
507                             false);
508 
509     if (img_size < 0) {
510         img_size = load_image_size(netboot_filename, ram_ptr, ms->ram_size);
511         ipl->start_addr = KERN_IMAGE_START;
512     }
513 
514     if (img_size < 0) {
515         error_setg(errp, "Failed to load network bootloader");
516     }
517 
518     g_free(netboot_filename);
519 
520 unref_mr:
521     memory_region_unref(mr);
522     return img_size;
523 }
524 
525 static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb,
526                                          int virtio_id)
527 {
528     uint8_t cssid;
529     uint8_t ssid;
530     uint16_t devno;
531     uint16_t schid;
532     SubchDev *sch = NULL;
533 
534     if (iplb->pbt != S390_IPL_TYPE_CCW) {
535         return false;
536     }
537 
538     devno = be16_to_cpu(iplb->ccw.devno);
539     ssid = iplb->ccw.ssid & 3;
540 
541     for (schid = 0; schid < MAX_SCHID; schid++) {
542         for (cssid = 0; cssid < MAX_CSSID; cssid++) {
543             sch = css_find_subch(1, cssid, ssid, schid);
544 
545             if (sch && sch->devno == devno) {
546                 return sch->id.cu_model == virtio_id;
547             }
548         }
549     }
550     return false;
551 }
552 
553 static bool is_virtio_net_device(IplParameterBlock *iplb)
554 {
555     return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET);
556 }
557 
558 static bool is_virtio_scsi_device(IplParameterBlock *iplb)
559 {
560     return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI);
561 }
562 
563 static void update_machine_ipl_properties(IplParameterBlock *iplb)
564 {
565     Object *machine = qdev_get_machine();
566     Error *err = NULL;
567 
568     /* Sync loadparm */
569     if (iplb->flags & DIAG308_FLAGS_LP_VALID) {
570         uint8_t *ebcdic_loadparm = iplb->loadparm;
571         char ascii_loadparm[9];
572         int i;
573 
574         for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) {
575             ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]];
576         }
577         ascii_loadparm[i] = 0;
578         object_property_set_str(machine, "loadparm", ascii_loadparm, &err);
579     } else {
580         object_property_set_str(machine, "loadparm", "", &err);
581     }
582     if (err) {
583         warn_report_err(err);
584     }
585 }
586 
587 void s390_ipl_update_diag308(IplParameterBlock *iplb)
588 {
589     S390IPLState *ipl = get_ipl_device();
590 
591     /*
592      * The IPLB set and retrieved by subcodes 8/9 is completely
593      * separate from the one managed via subcodes 5/6.
594      */
595     if (iplb->pbt == S390_IPL_TYPE_PV) {
596         ipl->iplb_pv = *iplb;
597         ipl->iplb_valid_pv = true;
598     } else {
599         ipl->iplb = *iplb;
600         ipl->iplb_valid = true;
601     }
602     ipl->netboot = is_virtio_net_device(iplb);
603     update_machine_ipl_properties(iplb);
604 }
605 
606 IplParameterBlock *s390_ipl_get_iplb_pv(void)
607 {
608     S390IPLState *ipl = get_ipl_device();
609 
610     if (!ipl->iplb_valid_pv) {
611         return NULL;
612     }
613     return &ipl->iplb_pv;
614 }
615 
616 IplParameterBlock *s390_ipl_get_iplb(void)
617 {
618     S390IPLState *ipl = get_ipl_device();
619 
620     if (!ipl->iplb_valid) {
621         return NULL;
622     }
623     return &ipl->iplb;
624 }
625 
626 void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
627 {
628     S390IPLState *ipl = get_ipl_device();
629 
630     if (reset_type == S390_RESET_EXTERNAL || reset_type == S390_RESET_REIPL) {
631         /* use CPU 0 for full resets */
632         ipl->reset_cpu_index = 0;
633     } else {
634         ipl->reset_cpu_index = cs->cpu_index;
635     }
636     ipl->reset_type = reset_type;
637 
638     if (reset_type == S390_RESET_REIPL &&
639         ipl->iplb_valid &&
640         !ipl->netboot &&
641         ipl->iplb.pbt == S390_IPL_TYPE_CCW &&
642         is_virtio_scsi_device(&ipl->iplb)) {
643         CcwDevice *ccw_dev = s390_get_ccw_device(get_boot_device(0), NULL);
644 
645         if (ccw_dev &&
646             cpu_to_be16(ccw_dev->sch->devno) == ipl->iplb.ccw.devno &&
647             (ccw_dev->sch->ssid & 3) == ipl->iplb.ccw.ssid) {
648             /*
649              * this is the original boot device's SCSI
650              * so restore IPL parameter info from it
651              */
652             ipl->iplb_valid = s390_gen_initial_iplb(ipl);
653         }
654     }
655     if (reset_type == S390_RESET_MODIFIED_CLEAR ||
656         reset_type == S390_RESET_LOAD_NORMAL ||
657         reset_type == S390_RESET_PV) {
658         /* ignore -no-reboot, send no event  */
659         qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
660     } else {
661         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
662     }
663     /* as this is triggered by a CPU, make sure to exit the loop */
664     if (tcg_enabled()) {
665         cpu_loop_exit(cs);
666     }
667 }
668 
669 void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
670 {
671     S390IPLState *ipl = get_ipl_device();
672 
673     *cs = qemu_get_cpu(ipl->reset_cpu_index);
674     if (!*cs) {
675         /* use any CPU */
676         *cs = first_cpu;
677     }
678     *reset_type = ipl->reset_type;
679 }
680 
681 void s390_ipl_clear_reset_request(void)
682 {
683     S390IPLState *ipl = get_ipl_device();
684 
685     ipl->reset_type = S390_RESET_EXTERNAL;
686     /* use CPU 0 for full resets */
687     ipl->reset_cpu_index = 0;
688 }
689 
690 static void s390_ipl_prepare_qipl(S390CPU *cpu)
691 {
692     S390IPLState *ipl = get_ipl_device();
693     uint8_t *addr;
694     uint64_t len = 4096;
695 
696     addr = cpu_physical_memory_map(cpu->env.psa, &len, true);
697     if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) {
698         error_report("Cannot set QEMU IPL parameters");
699         return;
700     }
701     memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
702     cpu_physical_memory_unmap(addr, len, 1, len);
703 }
704 
705 int s390_ipl_prepare_pv_header(Error **errp)
706 {
707     IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
708     IPLBlockPV *ipib_pv = &ipib->pv;
709     void *hdr = g_malloc(ipib_pv->pv_header_len);
710     int rc;
711 
712     cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
713                              ipib_pv->pv_header_len);
714     rc = s390_pv_set_sec_parms((uintptr_t)hdr, ipib_pv->pv_header_len, errp);
715     g_free(hdr);
716     return rc;
717 }
718 
719 int s390_ipl_pv_unpack(void)
720 {
721     IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
722     IPLBlockPV *ipib_pv = &ipib->pv;
723     int i, rc = 0;
724 
725     for (i = 0; i < ipib_pv->num_comp; i++) {
726         rc = s390_pv_unpack(ipib_pv->components[i].addr,
727                             TARGET_PAGE_ALIGN(ipib_pv->components[i].size),
728                             ipib_pv->components[i].tweak_pref);
729         if (rc) {
730             break;
731         }
732     }
733     return rc;
734 }
735 
736 void s390_ipl_prepare_cpu(S390CPU *cpu)
737 {
738     S390IPLState *ipl = get_ipl_device();
739 
740     cpu->env.psw.addr = ipl->start_addr;
741     cpu->env.psw.mask = IPL_PSW_MASK;
742 
743     if (!ipl->kernel || ipl->iplb_valid) {
744         cpu->env.psw.addr = ipl->bios_start_addr;
745         if (!ipl->iplb_valid) {
746             ipl->iplb_valid = s390_gen_initial_iplb(ipl);
747         }
748     }
749     if (ipl->netboot) {
750         load_netboot_image(&error_fatal);
751         ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr);
752     }
753     s390_ipl_set_boot_menu(ipl);
754     s390_ipl_prepare_qipl(cpu);
755 }
756 
757 static void s390_ipl_reset(DeviceState *dev)
758 {
759     S390IPLState *ipl = S390_IPL(dev);
760 
761     if (ipl->reset_type != S390_RESET_REIPL) {
762         ipl->iplb_valid = false;
763         memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
764     }
765 }
766 
767 static void s390_ipl_class_init(ObjectClass *klass, void *data)
768 {
769     DeviceClass *dc = DEVICE_CLASS(klass);
770 
771     dc->realize = s390_ipl_realize;
772     device_class_set_props(dc, s390_ipl_properties);
773     dc->reset = s390_ipl_reset;
774     dc->vmsd = &vmstate_ipl;
775     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
776     /* Reason: Loads the ROMs and thus can only be used one time - internally */
777     dc->user_creatable = false;
778 }
779 
780 static const TypeInfo s390_ipl_info = {
781     .class_init = s390_ipl_class_init,
782     .parent = TYPE_DEVICE,
783     .name  = TYPE_S390_IPL,
784     .instance_size  = sizeof(S390IPLState),
785 };
786 
787 static void s390_ipl_register_types(void)
788 {
789     type_register_static(&s390_ipl_info);
790 }
791 
792 type_init(s390_ipl_register_types)
793