Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / s390x / ipl.c
blob5ab743390876faa93114ad627c25923f52fd22d7
1 /*
2 * bootloader support
4 * Copyright IBM, Corp. 2012, 2020
6 * Authors:
7 * Christian Borntraeger <borntraeger@de.ibm.com>
8 * Janosch Frank <frankja@linux.ibm.com>
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.
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"
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)
50 static bool iplb_extended_needed(void *opaque)
52 S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
54 return ipl->iplbext_migration;
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()
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()
78 .subsections = (const VMStateDescription * const []) {
79 &vmstate_iplb_extended,
80 NULL
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()
100 static S390IPLState *get_ipl_device(void)
102 return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL));
105 static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
107 uint64_t dstaddr = *(uint64_t *) opaque;
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
112 return srcaddr + dstaddr;
115 static uint64_t get_max_kernel_cmdline_size(void)
117 uint64_t *size_ptr = rom_ptr(KERN_PARM_AREA_SIZE_ADDR, sizeof(*size_ptr));
119 if (size_ptr) {
120 uint64_t size;
122 size = be64_to_cpu(*size_ptr);
123 if (size) {
124 return size;
127 return LEGACY_KERN_PARM_AREA_SIZE;
130 static void s390_ipl_realize(DeviceState *dev, Error **errp)
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;
139 int bios_size;
140 char *bios_filename;
143 * Always load the bios if it was enforced,
144 * even if an external kernel has been defined.
146 if (!ipl->kernel || ipl->enforce_bios) {
147 uint64_t fwbase = (MIN(ms->ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
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;
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;
168 g_free(bios_filename);
170 if (bios_size == -1) {
171 error_setg(errp, "could not load bootloader '%s'", ipl->firmware);
172 return;
175 /* default boot target is the bios */
176 ipl->start_addr = ipl->bios_start_addr;
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;
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;
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.
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);
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();
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;
227 strcpy(parm_area, ipl->cmdline);
229 } else {
230 ipl->start_addr = pentry;
233 if (ipl->initrd) {
234 ram_addr_t initrd_offset;
235 int initrd_size;
236 uint64_t *romptr;
238 initrd_offset = INITRD_START;
239 while (kernel_size + 0x100000 > initrd_offset) {
240 initrd_offset += 0x100000;
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;
250 * we have to overwrite values in the kernel image,
251 * which are "rom"
253 romptr = rom_ptr(INITRD_PARM_START, 16);
254 if (romptr) {
255 stq_be_p(romptr, initrd_offset);
256 stq_be_p(romptr + 1, initrd_size);
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.
265 ipl->compat_start_addr = ipl->start_addr;
266 ipl->compat_bios_start_addr = ipl->bios_start_addr;
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.
275 qemu_register_reset(resettable_cold_reset_fn, dev);
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(),
290 static void s390_ipl_set_boot_menu(S390IPLState *ipl)
292 unsigned long splash_time = 0;
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");
299 return;
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;
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");
316 return;
319 if (!current_machine->boot_config.has_menu || !current_machine->boot_config.menu) {
320 return;
323 ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
325 if (current_machine->boot_config.has_splash_time) {
326 splash_time = current_machine->boot_config.splash_time;
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;
334 ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
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
343 static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
345 CcwDevice *ccw_dev = NULL;
346 int tmp_dt = CCW_DEVTYPE_NONE;
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);
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;
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;
387 if (devtype) {
388 *devtype = tmp_dt;
390 return ccw_dev;
393 static bool s390_gen_initial_iplb(S390IPLState *ipl)
395 DeviceState *dev_st;
396 CcwDevice *ccw_dev = NULL;
397 SCSIDevice *sd;
398 int devtype;
400 dev_st = get_boot_device(0);
401 if (dev_st) {
402 ccw_dev = s390_get_ccw_device(dev_st, &devtype);
406 * Currently allow IPL only from CCW devices.
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;
441 if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
442 ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
445 return true;
448 return false;
451 int s390_ipl_set_loadparm(uint8_t *loadparm)
453 MachineState *machine = MACHINE(qdev_get_machine());
454 char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
456 if (lp) {
457 int i;
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]];
464 if (i < 8) {
465 memset(loadparm + i, 0x40, 8 - i); /* fill with EBCDIC spaces */
468 g_free(lp);
469 return 0;
472 return -1;
475 static int load_netboot_image(Error **errp)
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;
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;
491 ram_ptr = memory_region_get_ram_ptr(mr);
492 if (!ram_ptr) {
493 error_setg(errp, "No RAM found");
494 goto unref_mr;
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;
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);
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;
514 if (img_size < 0) {
515 error_setg(errp, "Failed to load network bootloader");
518 g_free(netboot_filename);
520 unref_mr:
521 memory_region_unref(mr);
522 return img_size;
525 static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb,
526 int virtio_id)
528 uint8_t cssid;
529 uint8_t ssid;
530 uint16_t devno;
531 uint16_t schid;
532 SubchDev *sch = NULL;
534 if (iplb->pbt != S390_IPL_TYPE_CCW) {
535 return false;
538 devno = be16_to_cpu(iplb->ccw.devno);
539 ssid = iplb->ccw.ssid & 3;
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);
545 if (sch && sch->devno == devno) {
546 return sch->id.cu_model == virtio_id;
550 return false;
553 static bool is_virtio_net_device(IplParameterBlock *iplb)
555 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET);
558 static bool is_virtio_scsi_device(IplParameterBlock *iplb)
560 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI);
563 static void update_machine_ipl_properties(IplParameterBlock *iplb)
565 Object *machine = qdev_get_machine();
566 Error *err = NULL;
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;
574 for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) {
575 ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]];
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);
582 if (err) {
583 warn_report_err(err);
587 void s390_ipl_update_diag308(IplParameterBlock *iplb)
589 S390IPLState *ipl = get_ipl_device();
592 * The IPLB set and retrieved by subcodes 8/9 is completely
593 * separate from the one managed via subcodes 5/6.
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;
602 ipl->netboot = is_virtio_net_device(iplb);
603 update_machine_ipl_properties(iplb);
606 IplParameterBlock *s390_ipl_get_iplb_pv(void)
608 S390IPLState *ipl = get_ipl_device();
610 if (!ipl->iplb_valid_pv) {
611 return NULL;
613 return &ipl->iplb_pv;
616 IplParameterBlock *s390_ipl_get_iplb(void)
618 S390IPLState *ipl = get_ipl_device();
620 if (!ipl->iplb_valid) {
621 return NULL;
623 return &ipl->iplb;
626 void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
628 S390IPLState *ipl = get_ipl_device();
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;
636 ipl->reset_type = reset_type;
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);
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) {
649 * this is the original boot device's SCSI
650 * so restore IPL parameter info from it
652 ipl->iplb_valid = s390_gen_initial_iplb(ipl);
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);
663 /* as this is triggered by a CPU, make sure to exit the loop */
664 if (tcg_enabled()) {
665 cpu_loop_exit(cs);
669 void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
671 S390IPLState *ipl = get_ipl_device();
673 *cs = qemu_get_cpu(ipl->reset_cpu_index);
674 if (!*cs) {
675 /* use any CPU */
676 *cs = first_cpu;
678 *reset_type = ipl->reset_type;
681 void s390_ipl_clear_reset_request(void)
683 S390IPLState *ipl = get_ipl_device();
685 ipl->reset_type = S390_RESET_EXTERNAL;
686 /* use CPU 0 for full resets */
687 ipl->reset_cpu_index = 0;
690 static void s390_ipl_prepare_qipl(S390CPU *cpu)
692 S390IPLState *ipl = get_ipl_device();
693 uint8_t *addr;
694 uint64_t len = 4096;
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;
701 memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
702 cpu_physical_memory_unmap(addr, len, 1, len);
705 int s390_ipl_prepare_pv_header(Error **errp)
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;
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;
719 int s390_ipl_pv_unpack(void)
721 IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
722 IPLBlockPV *ipib_pv = &ipib->pv;
723 int i, rc = 0;
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;
733 return rc;
736 void s390_ipl_prepare_cpu(S390CPU *cpu)
738 S390IPLState *ipl = get_ipl_device();
740 cpu->env.psw.addr = ipl->start_addr;
741 cpu->env.psw.mask = IPL_PSW_MASK;
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);
749 if (ipl->netboot) {
750 load_netboot_image(&error_fatal);
751 ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr);
753 s390_ipl_set_boot_menu(ipl);
754 s390_ipl_prepare_qipl(cpu);
757 static void s390_ipl_reset(DeviceState *dev)
759 S390IPLState *ipl = S390_IPL(dev);
761 if (ipl->reset_type != S390_RESET_REIPL) {
762 ipl->iplb_valid = false;
763 memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
767 static void s390_ipl_class_init(ObjectClass *klass, void *data)
769 DeviceClass *dc = DEVICE_CLASS(klass);
771 dc->realize = s390_ipl_realize;
772 device_class_set_props(dc, s390_ipl_properties);
773 device_class_set_legacy_reset(dc, 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;
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),
787 static void s390_ipl_register_types(void)
789 type_register_static(&s390_ipl_info);
792 type_init(s390_ipl_register_types)