]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390x/ipl.c
qdev: provide qdev_reset_all_fn()
[mirror_qemu.git] / hw / s390x / ipl.c
CommitLineData
e674a49a
CB
1/*
2 * bootloader support
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Christian Borntraeger <borntraeger@de.ibm.com>
8 *
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.
11 *
12 */
13
14#include "sysemu/sysemu.h"
15#include "cpu.h"
16#include "elf.h"
17#include "hw/loader.h"
18#include "hw/sysbus.h"
ba1509c0
DD
19#include "hw/s390x/virtio-ccw.h"
20#include "hw/s390x/css.h"
df75a4e2 21#include "ipl.h"
e674a49a
CB
22
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
e674a49a
CB
29#define ZIPL_IMAGE_START 0x009000UL
30#define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
31
32#define TYPE_S390_IPL "s390-ipl"
33#define S390_IPL(obj) \
34 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
35#if 0
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)
40#endif
41
42typedef struct S390IPLClass {
43 /*< private >*/
44 SysBusDeviceClass parent_class;
45 /*< public >*/
46
47 void (*parent_reset) (SysBusDevice *dev);
48} S390IPLClass;
49
50typedef struct S390IPLState {
51 /*< private >*/
52 SysBusDevice parent_obj;
74ad2d22 53 uint64_t start_addr;
f0180f91
FZ
54 uint64_t bios_start_addr;
55 bool enforce_bios;
df75a4e2
FZ
56 IplParameterBlock iplb;
57 bool iplb_valid;
e91e972c 58 bool reipl_requested;
e674a49a 59
74ad2d22 60 /*< public >*/
e674a49a
CB
61 char *kernel;
62 char *initrd;
63 char *cmdline;
d0249ce5 64 char *firmware;
df75a4e2
FZ
65 uint8_t cssid;
66 uint8_t ssid;
67 uint16_t devno;
e674a49a
CB
68} S390IPLState;
69
2e13fbe4
FZ
70static const VMStateDescription vmstate_iplb = {
71 .name = "ipl/iplb",
72 .version_id = 0,
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),
78 VMSTATE_END_OF_LIST()
79 }
80};
81
82static const VMStateDescription vmstate_ipl = {
83 .name = "ipl",
84 .version_id = 0,
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),
94 VMSTATE_END_OF_LIST()
95 }
96};
e674a49a 97
feacc6c2
DH
98static S390IPLState *get_ipl_device(void)
99{
100 return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL));
101}
102
d884c86d
TH
103static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
104{
105 uint64_t dstaddr = *(uint64_t *) opaque;
106 /*
107 * Assuming that our s390-ccw.img was linked for starting at address 0,
108 * we can simply add the destination address for the final location
109 */
110 return srcaddr + dstaddr;
111}
112
e674a49a
CB
113static int s390_ipl_init(SysBusDevice *dev)
114{
115 S390IPLState *ipl = S390_IPL(dev);
7691993c 116 uint64_t pentry = KERN_IMAGE_START;
376827d4 117 int kernel_size;
e674a49a 118
f0180f91
FZ
119 int bios_size;
120 char *bios_filename;
e674a49a 121
f0180f91
FZ
122 /*
123 * Always load the bios if it was enforced,
124 * even if an external kernel has been defined.
125 */
126 if (!ipl->kernel || ipl->enforce_bios) {
d884c86d
TH
127 uint64_t fwbase = (MIN(ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
128
e674a49a 129 if (bios_name == NULL) {
d0249ce5 130 bios_name = ipl->firmware;
e674a49a
CB
131 }
132
133 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1f7de853
DD
134 if (bios_filename == NULL) {
135 hw_error("could not find stage1 bootloader\n");
136 }
137
d884c86d
TH
138 bios_size = load_elf(bios_filename, bios_translate_addr, &fwbase,
139 &ipl->bios_start_addr, NULL, NULL, 1,
99a4434e 140 EM_S390, 0);
d884c86d
TH
141 if (bios_size > 0) {
142 /* Adjust ELF start address to final location */
143 ipl->bios_start_addr += fwbase;
144 } else {
145 /* Try to load non-ELF file (e.g. s390-zipl.rom) */
33259956
AG
146 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
147 4096);
f0180f91 148 ipl->bios_start_addr = ZIPL_IMAGE_START;
33259956 149 }
e674a49a
CB
150 g_free(bios_filename);
151
376827d4 152 if (bios_size == -1) {
e674a49a
CB
153 hw_error("could not load bootloader '%s'\n", bios_name);
154 }
7691993c 155
f0180f91
FZ
156 /* default boot target is the bios */
157 ipl->start_addr = ipl->bios_start_addr;
e674a49a 158 }
7691993c 159
f0180f91
FZ
160 if (ipl->kernel) {
161 kernel_size = load_elf(ipl->kernel, NULL, NULL, &pentry, NULL,
99a4434e 162 NULL, 1, EM_S390, 0);
f0180f91
FZ
163 if (kernel_size < 0) {
164 kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
165 }
166 if (kernel_size < 0) {
167 fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel);
168 return -1;
e674a49a 169 }
f0180f91
FZ
170 /*
171 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
172 * kernel parameters here as well. Note: For old kernels (up to 3.2)
173 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
174 * loader) and it won't work. For this case we force it to 0x10000, too.
175 */
176 if (pentry == KERN_IMAGE_START || pentry == 0x800) {
177 ipl->start_addr = KERN_IMAGE_START;
178 /* Overwrite parameters in the kernel image, which are "rom" */
179 strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline);
180 } else {
181 ipl->start_addr = pentry;
e674a49a
CB
182 }
183
f0180f91
FZ
184 if (ipl->initrd) {
185 ram_addr_t initrd_offset;
186 int initrd_size;
187
188 initrd_offset = INITRD_START;
189 while (kernel_size + 0x100000 > initrd_offset) {
190 initrd_offset += 0x100000;
191 }
192 initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
193 ram_size - initrd_offset);
194 if (initrd_size == -1) {
195 fprintf(stderr, "qemu: could not load initrd '%s'\n",
196 ipl->initrd);
197 exit(1);
198 }
e674a49a 199
f0180f91
FZ
200 /*
201 * we have to overwrite values in the kernel image,
202 * which are "rom"
203 */
204 stq_p(rom_ptr(INITRD_PARM_START), initrd_offset);
205 stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size);
206 }
207 }
e674a49a
CB
208 return 0;
209}
210
211static Property s390_ipl_properties[] = {
212 DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
213 DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
214 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
d0249ce5 215 DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
f0180f91 216 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
e674a49a
CB
217 DEFINE_PROP_END_OF_LIST(),
218};
219
df75a4e2
FZ
220/*
221 * In addition to updating the iplstate, this function returns:
222 * - 0 if system was ipled with external kernel
223 * - -1 if no valid boot device was found
224 * - ccw id of the boot device otherwise
225 */
db3b2566 226static uint64_t s390_update_iplstate(S390IPLState *ipl)
df75a4e2
FZ
227{
228 DeviceState *dev_st;
229
230 if (ipl->iplb_valid) {
231 ipl->cssid = 0;
232 ipl->ssid = 0;
233 ipl->devno = ipl->iplb.devno;
234 goto out;
235 }
236
237 if (ipl->kernel) {
238 return 0;
239 }
240
241 dev_st = get_boot_device(0);
242 if (dev_st) {
243 VirtioCcwDevice *ccw_dev = (VirtioCcwDevice *) object_dynamic_cast(
244 OBJECT(qdev_get_parent_bus(dev_st)->parent),
245 TYPE_VIRTIO_CCW_DEVICE);
246 if (ccw_dev) {
247 ipl->cssid = ccw_dev->sch->cssid;
248 ipl->ssid = ccw_dev->sch->ssid;
249 ipl->devno = ccw_dev->sch->devno;
250 goto out;
251 }
252 }
253
254 return -1;
255out:
6efd2c2a 256 return (uint32_t) (ipl->cssid << 24 | ipl->ssid << 16 | ipl->devno);
df75a4e2
FZ
257}
258
feacc6c2 259void s390_ipl_update_diag308(IplParameterBlock *iplb)
df75a4e2 260{
feacc6c2 261 S390IPLState *ipl = get_ipl_device();
df75a4e2 262
feacc6c2
DH
263 ipl->iplb = *iplb;
264 ipl->iplb_valid = true;
df75a4e2
FZ
265}
266
267IplParameterBlock *s390_ipl_get_iplb(void)
268{
feacc6c2 269 S390IPLState *ipl = get_ipl_device();
df75a4e2 270
feacc6c2 271 if (!ipl->iplb_valid) {
df75a4e2
FZ
272 return NULL;
273 }
274 return &ipl->iplb;
275}
276
e91e972c
FZ
277void s390_reipl_request(void)
278{
feacc6c2 279 S390IPLState *ipl = get_ipl_device();
e91e972c 280
e91e972c
FZ
281 ipl->reipl_requested = true;
282 qemu_system_reset_request();
283}
284
db3b2566
DH
285void s390_ipl_prepare_cpu(S390CPU *cpu)
286{
287 S390IPLState *ipl = get_ipl_device();
288
289 cpu->env.psw.addr = ipl->start_addr;
290 cpu->env.psw.mask = IPL_PSW_MASK;
291
292 if (!ipl->kernel || ipl->iplb_valid) {
293 cpu->env.psw.addr = ipl->bios_start_addr;
294 cpu->env.regs[7] = s390_update_iplstate(ipl);
295 }
296}
297
e674a49a
CB
298static void s390_ipl_reset(DeviceState *dev)
299{
300 S390IPLState *ipl = S390_IPL(dev);
ba1509c0 301
e91e972c
FZ
302 if (!ipl->reipl_requested) {
303 ipl->iplb_valid = false;
304 }
305 ipl->reipl_requested = false;
e674a49a
CB
306}
307
308static void s390_ipl_class_init(ObjectClass *klass, void *data)
309{
310 DeviceClass *dc = DEVICE_CLASS(klass);
311 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
312
313 k->init = s390_ipl_init;
314 dc->props = s390_ipl_properties;
315 dc->reset = s390_ipl_reset;
2e13fbe4 316 dc->vmsd = &vmstate_ipl;
b4ab4572 317 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
e674a49a
CB
318}
319
49973ebc 320static const TypeInfo s390_ipl_info = {
e674a49a
CB
321 .class_init = s390_ipl_class_init,
322 .parent = TYPE_SYS_BUS_DEVICE,
323 .name = "s390-ipl",
324 .instance_size = sizeof(S390IPLState),
325};
326
327static void s390_ipl_register_types(void)
328{
329 type_register_static(&s390_ipl_info);
330}
331
332type_init(s390_ipl_register_types)