4 * Copyright IBM, Corp. 2012
7 * Christian Borntraeger <borntraeger@de.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
10 * option) any later version. See the COPYING file in the top-level directory.
14 #include "sysemu/sysemu.h"
17 #include "hw/loader.h"
18 #include "hw/sysbus.h"
19 #include "hw/s390x/virtio-ccw.h"
20 #include "hw/s390x/css.h"
23 #define KERN_IMAGE_START 0x010000UL
24 #define KERN_PARM_AREA 0x010480UL
25 #define INITRD_START 0x800000UL
26 #define INITRD_PARM_START 0x010408UL
27 #define INITRD_PARM_SIZE 0x010410UL
28 #define PARMFILE_START 0x001000UL
29 #define ZIPL_IMAGE_START 0x009000UL
30 #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
32 #define TYPE_S390_IPL "s390-ipl"
33 #define S390_IPL(obj) \
34 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
36 #define S390_IPL_CLASS(klass) \
37 OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL)
38 #define S390_IPL_GET_CLASS(obj) \
39 OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL)
42 typedef struct S390IPLClass
{
44 SysBusDeviceClass parent_class
;
47 void (*parent_reset
) (SysBusDevice
*dev
);
50 typedef struct S390IPLState
{
52 SysBusDevice parent_obj
;
54 uint64_t bios_start_addr
;
56 IplParameterBlock iplb
;
70 static const VMStateDescription vmstate_iplb
= {
73 .minimum_version_id
= 0,
74 .fields
= (VMStateField
[]) {
75 VMSTATE_UINT8_ARRAY(reserved1
, IplParameterBlock
, 110),
76 VMSTATE_UINT16(devno
, IplParameterBlock
),
77 VMSTATE_UINT8_ARRAY(reserved2
, IplParameterBlock
, 88),
82 static const VMStateDescription vmstate_ipl
= {
85 .minimum_version_id
= 0,
86 .fields
= (VMStateField
[]) {
87 VMSTATE_UINT64(start_addr
, S390IPLState
),
88 VMSTATE_UINT64(bios_start_addr
, S390IPLState
),
89 VMSTATE_STRUCT(iplb
, S390IPLState
, 0, vmstate_iplb
, IplParameterBlock
),
90 VMSTATE_BOOL(iplb_valid
, S390IPLState
),
91 VMSTATE_UINT8(cssid
, S390IPLState
),
92 VMSTATE_UINT8(ssid
, S390IPLState
),
93 VMSTATE_UINT16(devno
, S390IPLState
),
98 static int s390_ipl_init(SysBusDevice
*dev
)
100 S390IPLState
*ipl
= S390_IPL(dev
);
101 uint64_t pentry
= KERN_IMAGE_START
;
108 * Always load the bios if it was enforced,
109 * even if an external kernel has been defined.
111 if (!ipl
->kernel
|| ipl
->enforce_bios
) {
112 if (bios_name
== NULL
) {
113 bios_name
= ipl
->firmware
;
116 bios_filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
117 if (bios_filename
== NULL
) {
118 hw_error("could not find stage1 bootloader\n");
121 bios_size
= load_elf(bios_filename
, NULL
, NULL
, &ipl
->bios_start_addr
,
122 NULL
, NULL
, 1, ELF_MACHINE
, 0);
124 bios_size
= load_image_targphys(bios_filename
, ZIPL_IMAGE_START
,
126 ipl
->bios_start_addr
= ZIPL_IMAGE_START
;
127 if (bios_size
> 4096) {
128 hw_error("stage1 bootloader is > 4k\n");
131 g_free(bios_filename
);
133 if (bios_size
== -1) {
134 hw_error("could not load bootloader '%s'\n", bios_name
);
137 /* default boot target is the bios */
138 ipl
->start_addr
= ipl
->bios_start_addr
;
142 kernel_size
= load_elf(ipl
->kernel
, NULL
, NULL
, &pentry
, NULL
,
143 NULL
, 1, ELF_MACHINE
, 0);
144 if (kernel_size
< 0) {
145 kernel_size
= load_image_targphys(ipl
->kernel
, 0, ram_size
);
147 if (kernel_size
< 0) {
148 fprintf(stderr
, "could not load kernel '%s'\n", ipl
->kernel
);
152 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
153 * kernel parameters here as well. Note: For old kernels (up to 3.2)
154 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
155 * loader) and it won't work. For this case we force it to 0x10000, too.
157 if (pentry
== KERN_IMAGE_START
|| pentry
== 0x800) {
158 ipl
->start_addr
= KERN_IMAGE_START
;
159 /* Overwrite parameters in the kernel image, which are "rom" */
160 strcpy(rom_ptr(KERN_PARM_AREA
), ipl
->cmdline
);
162 ipl
->start_addr
= pentry
;
166 ram_addr_t initrd_offset
;
169 initrd_offset
= INITRD_START
;
170 while (kernel_size
+ 0x100000 > initrd_offset
) {
171 initrd_offset
+= 0x100000;
173 initrd_size
= load_image_targphys(ipl
->initrd
, initrd_offset
,
174 ram_size
- initrd_offset
);
175 if (initrd_size
== -1) {
176 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
182 * we have to overwrite values in the kernel image,
185 stq_p(rom_ptr(INITRD_PARM_START
), initrd_offset
);
186 stq_p(rom_ptr(INITRD_PARM_SIZE
), initrd_size
);
192 static Property s390_ipl_properties
[] = {
193 DEFINE_PROP_STRING("kernel", S390IPLState
, kernel
),
194 DEFINE_PROP_STRING("initrd", S390IPLState
, initrd
),
195 DEFINE_PROP_STRING("cmdline", S390IPLState
, cmdline
),
196 DEFINE_PROP_STRING("firmware", S390IPLState
, firmware
),
197 DEFINE_PROP_BOOL("enforce_bios", S390IPLState
, enforce_bios
, false),
198 DEFINE_PROP_END_OF_LIST(),
202 * In addition to updating the iplstate, this function returns:
203 * - 0 if system was ipled with external kernel
204 * - -1 if no valid boot device was found
205 * - ccw id of the boot device otherwise
207 static uint64_t s390_update_iplstate(CPUS390XState
*env
, S390IPLState
*ipl
)
211 if (ipl
->iplb_valid
) {
214 ipl
->devno
= ipl
->iplb
.devno
;
222 dev_st
= get_boot_device(0);
224 VirtioCcwDevice
*ccw_dev
= (VirtioCcwDevice
*) object_dynamic_cast(
225 OBJECT(qdev_get_parent_bus(dev_st
)->parent
),
226 TYPE_VIRTIO_CCW_DEVICE
);
228 ipl
->cssid
= ccw_dev
->sch
->cssid
;
229 ipl
->ssid
= ccw_dev
->sch
->ssid
;
230 ipl
->devno
= ccw_dev
->sch
->devno
;
237 return ipl
->cssid
<< 24 | ipl
->ssid
<< 16 | ipl
->devno
;
240 int s390_ipl_update_diag308(IplParameterBlock
*iplb
)
244 ipl
= S390_IPL(object_resolve_path(TYPE_S390_IPL
, NULL
));
247 ipl
->iplb_valid
= true;
253 IplParameterBlock
*s390_ipl_get_iplb(void)
257 ipl
= S390_IPL(object_resolve_path(TYPE_S390_IPL
, NULL
));
258 if (!ipl
|| !ipl
->iplb_valid
) {
264 void s390_reipl_request(void)
268 ipl
= S390_IPL(object_resolve_path(TYPE_S390_IPL
, NULL
));
269 ipl
->reipl_requested
= true;
270 qemu_system_reset_request();
273 static void s390_ipl_reset(DeviceState
*dev
)
275 S390IPLState
*ipl
= S390_IPL(dev
);
276 S390CPU
*cpu
= S390_CPU(qemu_get_cpu(0));
277 CPUS390XState
*env
= &cpu
->env
;
279 env
->psw
.addr
= ipl
->start_addr
;
280 env
->psw
.mask
= IPL_PSW_MASK
;
282 if (!ipl
->reipl_requested
) {
283 ipl
->iplb_valid
= false;
285 ipl
->reipl_requested
= false;
287 if (!ipl
->kernel
|| ipl
->iplb_valid
) {
288 env
->psw
.addr
= ipl
->bios_start_addr
;
289 env
->regs
[7] = s390_update_iplstate(env
, ipl
);
292 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
295 static void s390_ipl_class_init(ObjectClass
*klass
, void *data
)
297 DeviceClass
*dc
= DEVICE_CLASS(klass
);
298 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
300 k
->init
= s390_ipl_init
;
301 dc
->props
= s390_ipl_properties
;
302 dc
->reset
= s390_ipl_reset
;
303 dc
->vmsd
= &vmstate_ipl
;
306 static const TypeInfo s390_ipl_info
= {
307 .class_init
= s390_ipl_class_init
,
308 .parent
= TYPE_SYS_BUS_DEVICE
,
310 .instance_size
= sizeof(S390IPLState
),
313 static void s390_ipl_register_types(void)
315 type_register_static(&s390_ipl_info
);
318 type_init(s390_ipl_register_types
)