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