]> git.proxmox.com Git - qemu.git/blame - hw/nvram/fw_cfg.c
fw_cfg: interface to trigger callback on read
[qemu.git] / hw / nvram / fw_cfg.c
CommitLineData
3cce6243
BS
1/*
2 * QEMU Firmware configuration device emulation
3 *
4 * Copyright (c) 2008 Gleb Natapov
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
83c9f4ca 24#include "hw/hw.h"
9c17d615 25#include "sysemu/sysemu.h"
0d09e41a
PB
26#include "hw/isa/isa.h"
27#include "hw/nvram/fw_cfg.h"
83c9f4ca 28#include "hw/sysbus.h"
f6e35343 29#include "trace.h"
1de7afc9
PB
30#include "qemu/error-report.h"
31#include "qemu/config-file.h"
3cce6243 32
3cce6243 33#define FW_CFG_SIZE 2
561e1827 34#define FW_CFG_DATA_SIZE 1
600c60b7
MT
35#define TYPE_FW_CFG "fw_cfg"
36#define FW_CFG_NAME "fw_cfg"
37#define FW_CFG_PATH "/machine/" FW_CFG_NAME
2ce92a11 38#define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
3cce6243 39
b96ae2da 40typedef struct FWCfgEntry {
ff06108b 41 uint32_t len;
3cce6243
BS
42 uint8_t *data;
43 void *callback_opaque;
44 FWCfgCallback callback;
d87072ce 45 FWCfgReadCallback read_callback;
3cce6243
BS
46} FWCfgEntry;
47
b96ae2da 48struct FWCfgState {
2ce92a11
HT
49 /*< private >*/
50 SysBusDevice parent_obj;
51 /*< public >*/
52
561e1827 53 MemoryRegion ctl_iomem, data_iomem, comb_iomem;
3a5c16fc 54 uint32_t ctl_iobase, data_iobase;
3cce6243 55 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
abe147e0 56 FWCfgFiles *files;
3cce6243 57 uint16_t cur_entry;
ff06108b 58 uint32_t cur_offset;
962630f2 59 Notifier machine_ready;
c2b5bda4 60};
3cce6243 61
3d3b8303 62#define JPG_FILE 0
63#define BMP_FILE 1
64
3d1bba20 65static char *read_splashfile(char *filename, gsize *file_sizep,
d09acb9b 66 int *file_typep)
3d3b8303 67{
9477c87e
PB
68 GError *err = NULL;
69 gboolean res;
70 gchar *content;
9f8863eb
MA
71 int file_type;
72 unsigned int filehead;
3d3b8303 73 int bmp_bpp;
74
d09acb9b 75 res = g_file_get_contents(filename, &content, file_sizep, &err);
9477c87e
PB
76 if (res == FALSE) {
77 error_report("failed to read splash file '%s'", filename);
78 g_error_free(err);
79 return NULL;
3d3b8303 80 }
9477c87e 81
3d3b8303 82 /* check file size */
9477c87e
PB
83 if (*file_sizep < 30) {
84 goto error;
3d3b8303 85 }
9477c87e 86
3d3b8303 87 /* check magic ID */
9477c87e
PB
88 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
89 if (filehead == 0xd8ff) {
3d3b8303 90 file_type = JPG_FILE;
9477c87e
PB
91 } else if (filehead == 0x4d42) {
92 file_type = BMP_FILE;
3d3b8303 93 } else {
9477c87e 94 goto error;
3d3b8303 95 }
9477c87e 96
3d3b8303 97 /* check BMP bpp */
98 if (file_type == BMP_FILE) {
9477c87e 99 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
3d3b8303 100 if (bmp_bpp != 24) {
9477c87e 101 goto error;
3d3b8303 102 }
103 }
9477c87e 104
3d3b8303 105 /* return values */
3d3b8303 106 *file_typep = file_type;
9477c87e
PB
107
108 return content;
109
110error:
111 error_report("splash file '%s' format not recognized; must be JPEG "
112 "or 24 bit BMP", filename);
113 g_free(content);
114 return NULL;
3d3b8303 115}
116
117static void fw_cfg_bootsplash(FWCfgState *s)
118{
119 int boot_splash_time = -1;
120 const char *boot_splash_filename = NULL;
121 char *p;
9477c87e 122 char *filename, *file_data;
3d1bba20 123 gsize file_size;
9f8863eb 124 int file_type;
3d3b8303 125 const char *temp;
126
127 /* get user configuration */
128 QemuOptsList *plist = qemu_find_opts("boot-opts");
129 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
130 if (opts != NULL) {
131 temp = qemu_opt_get(opts, "splash");
132 if (temp != NULL) {
133 boot_splash_filename = temp;
134 }
135 temp = qemu_opt_get(opts, "splash-time");
136 if (temp != NULL) {
137 p = (char *)temp;
138 boot_splash_time = strtol(p, (char **)&p, 10);
139 }
140 }
141
142 /* insert splash time if user configurated */
143 if (boot_splash_time >= 0) {
144 /* validate the input */
145 if (boot_splash_time > 0xffff) {
146 error_report("splash time is big than 65535, force it to 65535.");
147 boot_splash_time = 0xffff;
148 }
149 /* use little endian format */
150 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
151 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
152 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
153 }
154
155 /* insert splash file if user configurated */
156 if (boot_splash_filename != NULL) {
157 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
158 if (filename == NULL) {
159 error_report("failed to find file '%s'.", boot_splash_filename);
160 return;
161 }
9477c87e
PB
162
163 /* loading file data */
164 file_data = read_splashfile(filename, &file_size, &file_type);
165 if (file_data == NULL) {
7267c094 166 g_free(filename);
3d3b8303 167 return;
168 }
3d3b8303 169 if (boot_splash_filedata != NULL) {
7267c094 170 g_free(boot_splash_filedata);
3d3b8303 171 }
9477c87e 172 boot_splash_filedata = (uint8_t *)file_data;
3d3b8303 173 boot_splash_filedata_size = file_size;
9477c87e 174
3d3b8303 175 /* insert data */
176 if (file_type == JPG_FILE) {
177 fw_cfg_add_file(s, "bootsplash.jpg",
178 boot_splash_filedata, boot_splash_filedata_size);
179 } else {
180 fw_cfg_add_file(s, "bootsplash.bmp",
181 boot_splash_filedata, boot_splash_filedata_size);
182 }
7267c094 183 g_free(filename);
3d3b8303 184 }
185}
186
ac05f349
AK
187static void fw_cfg_reboot(FWCfgState *s)
188{
189 int reboot_timeout = -1;
190 char *p;
191 const char *temp;
192
193 /* get user configuration */
194 QemuOptsList *plist = qemu_find_opts("boot-opts");
195 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
196 if (opts != NULL) {
197 temp = qemu_opt_get(opts, "reboot-timeout");
198 if (temp != NULL) {
199 p = (char *)temp;
200 reboot_timeout = strtol(p, (char **)&p, 10);
201 }
202 }
203 /* validate the input */
204 if (reboot_timeout > 0xffff) {
205 error_report("reboot timeout is larger than 65535, force it to 65535.");
206 reboot_timeout = 0xffff;
207 }
208 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
209}
210
3cce6243
BS
211static void fw_cfg_write(FWCfgState *s, uint8_t value)
212{
213 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
214 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
215
f6e35343 216 trace_fw_cfg_write(s, value);
3cce6243 217
962d4b28
BS
218 if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
219 s->cur_offset < e->len) {
3cce6243
BS
220 e->data[s->cur_offset++] = value;
221 if (s->cur_offset == e->len) {
222 e->callback(e->callback_opaque, e->data);
223 s->cur_offset = 0;
224 }
225 }
226}
227
228static int fw_cfg_select(FWCfgState *s, uint16_t key)
229{
230 int ret;
231
232 s->cur_offset = 0;
233 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
234 s->cur_entry = FW_CFG_INVALID;
235 ret = 0;
236 } else {
237 s->cur_entry = key;
238 ret = 1;
239 }
240
f6e35343 241 trace_fw_cfg_select(s, key, ret);
3cce6243
BS
242 return ret;
243}
244
245static uint8_t fw_cfg_read(FWCfgState *s)
246{
247 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
248 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
249 uint8_t ret;
250
251 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
252 ret = 0;
d87072ce
MT
253 else {
254 if (e->read_callback) {
255 e->read_callback(e->callback_opaque, s->cur_offset);
256 }
3cce6243 257 ret = e->data[s->cur_offset++];
d87072ce 258 }
3cce6243 259
f6e35343 260 trace_fw_cfg_read(s, ret);
3cce6243
BS
261 return ret;
262}
263
a8170e5e 264static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
561e1827 265 unsigned size)
3cce6243
BS
266{
267 return fw_cfg_read(opaque);
268}
269
a8170e5e 270static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
561e1827 271 uint64_t value, unsigned size)
3cce6243 272{
7442511c 273 fw_cfg_write(opaque, (uint8_t)value);
3cce6243
BS
274}
275
a8170e5e 276static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
561e1827 277 uint64_t value, unsigned size)
3cce6243
BS
278{
279 fw_cfg_select(opaque, (uint16_t)value);
280}
281
a8170e5e 282static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
561e1827 283 unsigned size, bool is_write)
3cce6243 284{
561e1827 285 return is_write && size == 2;
3cce6243
BS
286}
287
a8170e5e 288static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
561e1827 289 unsigned size)
3cce6243 290{
561e1827 291 return fw_cfg_read(opaque);
3cce6243
BS
292}
293
a8170e5e 294static void fw_cfg_comb_write(void *opaque, hwaddr addr,
561e1827 295 uint64_t value, unsigned size)
3cce6243 296{
561e1827
AK
297 switch (size) {
298 case 1:
299 fw_cfg_write(opaque, (uint8_t)value);
300 break;
301 case 2:
302 fw_cfg_select(opaque, (uint16_t)value);
303 break;
304 }
3cce6243
BS
305}
306
a8170e5e 307static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
561e1827
AK
308 unsigned size, bool is_write)
309{
310 return (size == 1) || (is_write && size == 2);
311}
3cce6243 312
561e1827
AK
313static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
314 .write = fw_cfg_ctl_mem_write,
315 .endianness = DEVICE_NATIVE_ENDIAN,
316 .valid.accepts = fw_cfg_ctl_mem_valid,
3cce6243
BS
317};
318
561e1827
AK
319static const MemoryRegionOps fw_cfg_data_mem_ops = {
320 .read = fw_cfg_data_mem_read,
321 .write = fw_cfg_data_mem_write,
322 .endianness = DEVICE_NATIVE_ENDIAN,
323 .valid = {
324 .min_access_size = 1,
325 .max_access_size = 1,
326 },
3cce6243
BS
327};
328
561e1827
AK
329static const MemoryRegionOps fw_cfg_comb_mem_ops = {
330 .read = fw_cfg_comb_read,
331 .write = fw_cfg_comb_write,
6fdf98f2 332 .endianness = DEVICE_LITTLE_ENDIAN,
561e1827 333 .valid.accepts = fw_cfg_comb_valid,
3cce6243
BS
334};
335
3a5c16fc 336static void fw_cfg_reset(DeviceState *d)
3cce6243 337{
2ce92a11 338 FWCfgState *s = FW_CFG(d);
3cce6243
BS
339
340 fw_cfg_select(s, 0);
341}
342
ff06108b
JQ
343/* Save restore 32 bit int as uint16_t
344 This is a Big hack, but it is how the old state did it.
345 Or we broke compatibility in the state, or we can't use struct tm
346 */
347
348static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
349{
350 uint32_t *v = pv;
351 *v = qemu_get_be16(f);
352 return 0;
353}
354
355static void put_unused(QEMUFile *f, void *pv, size_t size)
356{
66c80e75 357 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
ff06108b
JQ
358 fprintf(stderr, "This functions shouldn't be called.\n");
359}
360
d05ac8fa 361static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
ff06108b
JQ
362 .name = "int32_as_uint16",
363 .get = get_uint32_as_uint16,
364 .put = put_unused,
365};
366
367#define VMSTATE_UINT16_HACK(_f, _s, _t) \
368 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
369
370
371static bool is_version_1(void *opaque, int version_id)
372{
373 return version_id == 1;
374}
375
7d2edd40
JQ
376static const VMStateDescription vmstate_fw_cfg = {
377 .name = "fw_cfg",
ff06108b 378 .version_id = 2,
7d2edd40
JQ
379 .minimum_version_id = 1,
380 .minimum_version_id_old = 1,
381 .fields = (VMStateField []) {
382 VMSTATE_UINT16(cur_entry, FWCfgState),
ff06108b
JQ
383 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
384 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
7d2edd40
JQ
385 VMSTATE_END_OF_LIST()
386 }
387};
3cce6243 388
d87072ce
MT
389static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
390 FWCfgReadCallback callback,
391 void *callback_opaque,
392 void *data, size_t len)
3cce6243 393{
3cce6243
BS
394 int arch = !!(key & FW_CFG_ARCH_LOCAL);
395
396 key &= FW_CFG_ENTRY_MASK;
397
089da572 398 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
3cce6243
BS
399
400 s->entries[arch][key].data = data;
089da572 401 s->entries[arch][key].len = (uint32_t)len;
d87072ce
MT
402 s->entries[arch][key].read_callback = callback;
403 s->entries[arch][key].callback_opaque = callback_opaque;
404}
405
406void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
407{
408 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
3cce6243
BS
409}
410
44687f75
MA
411void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
412{
413 size_t sz = strlen(value) + 1;
414
089da572 415 return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
44687f75
MA
416}
417
4cad3867 418void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
3cce6243
BS
419{
420 uint16_t *copy;
421
7267c094 422 copy = g_malloc(sizeof(value));
3cce6243 423 *copy = cpu_to_le16(value);
089da572 424 fw_cfg_add_bytes(s, key, copy, sizeof(value));
3cce6243
BS
425}
426
4cad3867 427void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
3cce6243
BS
428{
429 uint32_t *copy;
430
7267c094 431 copy = g_malloc(sizeof(value));
3cce6243 432 *copy = cpu_to_le32(value);
089da572 433 fw_cfg_add_bytes(s, key, copy, sizeof(value));
3cce6243
BS
434}
435
4cad3867 436void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
3cce6243
BS
437{
438 uint64_t *copy;
439
7267c094 440 copy = g_malloc(sizeof(value));
3cce6243 441 *copy = cpu_to_le64(value);
089da572 442 fw_cfg_add_bytes(s, key, copy, sizeof(value));
3cce6243
BS
443}
444
4cad3867 445void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
089da572 446 void *callback_opaque, void *data, size_t len)
3cce6243 447{
3cce6243
BS
448 int arch = !!(key & FW_CFG_ARCH_LOCAL);
449
4cad3867 450 assert(key & FW_CFG_WRITE_CHANNEL);
85df0de4 451
3cce6243
BS
452 key &= FW_CFG_ENTRY_MASK;
453
089da572 454 assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
3cce6243
BS
455
456 s->entries[arch][key].data = data;
089da572 457 s->entries[arch][key].len = (uint32_t)len;
3cce6243
BS
458 s->entries[arch][key].callback_opaque = callback_opaque;
459 s->entries[arch][key].callback = callback;
3cce6243
BS
460}
461
d87072ce
MT
462void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
463 FWCfgReadCallback callback, void *callback_opaque,
464 void *data, size_t len)
abe147e0 465{
de9352bc 466 int i, index;
089da572 467 size_t dsize;
abe147e0
GH
468
469 if (!s->files) {
089da572 470 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
7267c094 471 s->files = g_malloc0(dsize);
089da572 472 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
abe147e0
GH
473 }
474
475 index = be32_to_cpu(s->files->count);
4cad3867 476 assert(index < FW_CFG_FILE_SLOTS);
abe147e0 477
d87072ce
MT
478 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
479 callback, callback_opaque, data, len);
abe147e0 480
de1f34cb
GN
481 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
482 filename);
de9352bc
GH
483 for (i = 0; i < index; i++) {
484 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
f6e35343 485 trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
4cad3867 486 return;
de9352bc 487 }
abe147e0 488 }
de9352bc 489
abe147e0
GH
490 s->files->f[index].size = cpu_to_be32(len);
491 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
f6e35343 492 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
abe147e0
GH
493
494 s->files->count = cpu_to_be32(index+1);
abe147e0
GH
495}
496
d87072ce
MT
497void fw_cfg_add_file(FWCfgState *s, const char *filename,
498 void *data, size_t len)
499{
500 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
501}
502
9e8dd451 503static void fw_cfg_machine_ready(struct Notifier *n, void *data)
962630f2 504{
0e7a7592 505 size_t len;
962630f2
GN
506 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
507 char *bootindex = get_boot_devices_list(&len);
508
509 fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
510}
511
c2b5bda4 512FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
a8170e5e 513 hwaddr ctl_addr, hwaddr data_addr)
3cce6243 514{
3a5c16fc
BS
515 DeviceState *dev;
516 SysBusDevice *d;
3cce6243 517 FWCfgState *s;
3cce6243 518
2ce92a11 519 dev = qdev_create(NULL, TYPE_FW_CFG);
3a5c16fc
BS
520 qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
521 qdev_prop_set_uint32(dev, "data_iobase", data_port);
1356b98d 522 d = SYS_BUS_DEVICE(dev);
3a5c16fc 523
2ce92a11 524 s = FW_CFG(dev);
3cce6243 525
cac12210
MT
526 assert(!object_resolve_path(FW_CFG_PATH, NULL));
527
528 object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
10a584b2
HT
529
530 qdev_init_nofail(dev);
531
3cce6243 532 if (ctl_addr) {
3a5c16fc 533 sysbus_mmio_map(d, 0, ctl_addr);
3cce6243
BS
534 }
535 if (data_addr) {
3a5c16fc 536 sysbus_mmio_map(d, 1, data_addr);
3cce6243 537 }
089da572 538 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
084a197a 539 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
993fbfdb 540 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
905fdcb5 541 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
95387491 542 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
3d3b8303 543 fw_cfg_bootsplash(s);
ac05f349 544 fw_cfg_reboot(s);
962630f2
GN
545
546 s->machine_ready.notify = fw_cfg_machine_ready;
547 qemu_add_machine_init_done_notifier(&s->machine_ready);
548
3cce6243
BS
549 return s;
550}
3a5c16fc 551
56383955 552static void fw_cfg_initfn(Object *obj)
3a5c16fc 553{
56383955
HT
554 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
555 FWCfgState *s = FW_CFG(obj);
3a5c16fc 556
eedfac6f 557 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, s,
561e1827 558 "fwcfg.ctl", FW_CFG_SIZE);
56383955 559 sysbus_init_mmio(sbd, &s->ctl_iomem);
eedfac6f 560 memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops, s,
561e1827 561 "fwcfg.data", FW_CFG_DATA_SIZE);
56383955 562 sysbus_init_mmio(sbd, &s->data_iomem);
561e1827 563 /* In case ctl and data overlap: */
eedfac6f 564 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, s,
561e1827 565 "fwcfg", FW_CFG_SIZE);
56383955
HT
566}
567
568static void fw_cfg_realize(DeviceState *dev, Error **errp)
569{
570 FWCfgState *s = FW_CFG(dev);
571 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
572
561e1827
AK
573
574 if (s->ctl_iobase + 1 == s->data_iobase) {
56383955 575 sysbus_add_io(sbd, s->ctl_iobase, &s->comb_iomem);
561e1827
AK
576 } else {
577 if (s->ctl_iobase) {
56383955 578 sysbus_add_io(sbd, s->ctl_iobase, &s->ctl_iomem);
561e1827
AK
579 }
580 if (s->data_iobase) {
56383955 581 sysbus_add_io(sbd, s->data_iobase, &s->data_iomem);
561e1827 582 }
3a5c16fc 583 }
3a5c16fc
BS
584}
585
999e12bb
AL
586static Property fw_cfg_properties[] = {
587 DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
588 DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
589 DEFINE_PROP_END_OF_LIST(),
590};
591
600c60b7
MT
592FWCfgState *fw_cfg_find(void)
593{
2ce92a11 594 return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
600c60b7
MT
595}
596
999e12bb
AL
597static void fw_cfg_class_init(ObjectClass *klass, void *data)
598{
39bffca2 599 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 600
56383955 601 dc->realize = fw_cfg_realize;
39bffca2
AL
602 dc->no_user = 1;
603 dc->reset = fw_cfg_reset;
604 dc->vmsd = &vmstate_fw_cfg;
605 dc->props = fw_cfg_properties;
999e12bb
AL
606}
607
8c43a6f0 608static const TypeInfo fw_cfg_info = {
600c60b7 609 .name = TYPE_FW_CFG,
39bffca2
AL
610 .parent = TYPE_SYS_BUS_DEVICE,
611 .instance_size = sizeof(FWCfgState),
56383955 612 .instance_init = fw_cfg_initfn,
39bffca2 613 .class_init = fw_cfg_class_init,
3a5c16fc
BS
614};
615
83f7d43a 616static void fw_cfg_register_types(void)
3a5c16fc 617{
39bffca2 618 type_register_static(&fw_cfg_info);
3a5c16fc
BS
619}
620
83f7d43a 621type_init(fw_cfg_register_types)