fw_cfg_mem: max access size and region size are the same for data register
[qemu.git] / hw / nvram / fw_cfg.c
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  */
24 #include "hw/hw.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/isa/isa.h"
27 #include "hw/nvram/fw_cfg.h"
28 #include "hw/sysbus.h"
29 #include "trace.h"
30 #include "qemu/error-report.h"
31 #include "qemu/config-file.h"
32
33 #define FW_CFG_SIZE 2
34 #define FW_CFG_NAME "fw_cfg"
35 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
36
37 #define TYPE_FW_CFG     "fw_cfg"
38 #define TYPE_FW_CFG_IO  "fw_cfg_io"
39 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
40
41 #define FW_CFG(obj)     OBJECT_CHECK(FWCfgState,    (obj), TYPE_FW_CFG)
42 #define FW_CFG_IO(obj)  OBJECT_CHECK(FWCfgIoState,  (obj), TYPE_FW_CFG_IO)
43 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
44
45 typedef struct FWCfgEntry {
46     uint32_t len;
47     uint8_t *data;
48     void *callback_opaque;
49     FWCfgCallback callback;
50     FWCfgReadCallback read_callback;
51 } FWCfgEntry;
52
53 struct FWCfgState {
54     /*< private >*/
55     SysBusDevice parent_obj;
56     /*< public >*/
57
58     FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
59     FWCfgFiles *files;
60     uint16_t cur_entry;
61     uint32_t cur_offset;
62     Notifier machine_ready;
63 };
64
65 struct FWCfgIoState {
66     /*< private >*/
67     FWCfgState parent_obj;
68     /*< public >*/
69
70     MemoryRegion comb_iomem;
71     uint32_t iobase;
72 };
73
74 struct FWCfgMemState {
75     /*< private >*/
76     FWCfgState parent_obj;
77     /*< public >*/
78
79     MemoryRegion ctl_iomem, data_iomem;
80 };
81
82 #define JPG_FILE 0
83 #define BMP_FILE 1
84
85 static char *read_splashfile(char *filename, gsize *file_sizep,
86                              int *file_typep)
87 {
88     GError *err = NULL;
89     gboolean res;
90     gchar *content;
91     int file_type;
92     unsigned int filehead;
93     int bmp_bpp;
94
95     res = g_file_get_contents(filename, &content, file_sizep, &err);
96     if (res == FALSE) {
97         error_report("failed to read splash file '%s'", filename);
98         g_error_free(err);
99         return NULL;
100     }
101
102     /* check file size */
103     if (*file_sizep < 30) {
104         goto error;
105     }
106
107     /* check magic ID */
108     filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
109     if (filehead == 0xd8ff) {
110         file_type = JPG_FILE;
111     } else if (filehead == 0x4d42) {
112         file_type = BMP_FILE;
113     } else {
114         goto error;
115     }
116
117     /* check BMP bpp */
118     if (file_type == BMP_FILE) {
119         bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
120         if (bmp_bpp != 24) {
121             goto error;
122         }
123     }
124
125     /* return values */
126     *file_typep = file_type;
127
128     return content;
129
130 error:
131     error_report("splash file '%s' format not recognized; must be JPEG "
132                  "or 24 bit BMP", filename);
133     g_free(content);
134     return NULL;
135 }
136
137 static void fw_cfg_bootsplash(FWCfgState *s)
138 {
139     int boot_splash_time = -1;
140     const char *boot_splash_filename = NULL;
141     char *p;
142     char *filename, *file_data;
143     gsize file_size;
144     int file_type;
145     const char *temp;
146
147     /* get user configuration */
148     QemuOptsList *plist = qemu_find_opts("boot-opts");
149     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
150     if (opts != NULL) {
151         temp = qemu_opt_get(opts, "splash");
152         if (temp != NULL) {
153             boot_splash_filename = temp;
154         }
155         temp = qemu_opt_get(opts, "splash-time");
156         if (temp != NULL) {
157             p = (char *)temp;
158             boot_splash_time = strtol(p, (char **)&p, 10);
159         }
160     }
161
162     /* insert splash time if user configurated */
163     if (boot_splash_time >= 0) {
164         /* validate the input */
165         if (boot_splash_time > 0xffff) {
166             error_report("splash time is big than 65535, force it to 65535.");
167             boot_splash_time = 0xffff;
168         }
169         /* use little endian format */
170         qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
171         qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
172         fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
173     }
174
175     /* insert splash file if user configurated */
176     if (boot_splash_filename != NULL) {
177         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
178         if (filename == NULL) {
179             error_report("failed to find file '%s'.", boot_splash_filename);
180             return;
181         }
182
183         /* loading file data */
184         file_data = read_splashfile(filename, &file_size, &file_type);
185         if (file_data == NULL) {
186             g_free(filename);
187             return;
188         }
189         if (boot_splash_filedata != NULL) {
190             g_free(boot_splash_filedata);
191         }
192         boot_splash_filedata = (uint8_t *)file_data;
193         boot_splash_filedata_size = file_size;
194
195         /* insert data */
196         if (file_type == JPG_FILE) {
197             fw_cfg_add_file(s, "bootsplash.jpg",
198                     boot_splash_filedata, boot_splash_filedata_size);
199         } else {
200             fw_cfg_add_file(s, "bootsplash.bmp",
201                     boot_splash_filedata, boot_splash_filedata_size);
202         }
203         g_free(filename);
204     }
205 }
206
207 static void fw_cfg_reboot(FWCfgState *s)
208 {
209     int reboot_timeout = -1;
210     char *p;
211     const char *temp;
212
213     /* get user configuration */
214     QemuOptsList *plist = qemu_find_opts("boot-opts");
215     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
216     if (opts != NULL) {
217         temp = qemu_opt_get(opts, "reboot-timeout");
218         if (temp != NULL) {
219             p = (char *)temp;
220             reboot_timeout = strtol(p, (char **)&p, 10);
221         }
222     }
223     /* validate the input */
224     if (reboot_timeout > 0xffff) {
225         error_report("reboot timeout is larger than 65535, force it to 65535.");
226         reboot_timeout = 0xffff;
227     }
228     fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
229 }
230
231 static void fw_cfg_write(FWCfgState *s, uint8_t value)
232 {
233     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
234     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
235
236     trace_fw_cfg_write(s, value);
237
238     if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
239         s->cur_offset < e->len) {
240         e->data[s->cur_offset++] = value;
241         if (s->cur_offset == e->len) {
242             e->callback(e->callback_opaque, e->data);
243             s->cur_offset = 0;
244         }
245     }
246 }
247
248 static int fw_cfg_select(FWCfgState *s, uint16_t key)
249 {
250     int ret;
251
252     s->cur_offset = 0;
253     if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
254         s->cur_entry = FW_CFG_INVALID;
255         ret = 0;
256     } else {
257         s->cur_entry = key;
258         ret = 1;
259     }
260
261     trace_fw_cfg_select(s, key, ret);
262     return ret;
263 }
264
265 static uint8_t fw_cfg_read(FWCfgState *s)
266 {
267     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
268     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
269     uint8_t ret;
270
271     if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
272         ret = 0;
273     else {
274         if (e->read_callback) {
275             e->read_callback(e->callback_opaque, s->cur_offset);
276         }
277         ret = e->data[s->cur_offset++];
278     }
279
280     trace_fw_cfg_read(s, ret);
281     return ret;
282 }
283
284 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
285                                      unsigned size)
286 {
287     return fw_cfg_read(opaque);
288 }
289
290 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
291                                   uint64_t value, unsigned size)
292 {
293     fw_cfg_write(opaque, (uint8_t)value);
294 }
295
296 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
297                                  uint64_t value, unsigned size)
298 {
299     fw_cfg_select(opaque, (uint16_t)value);
300 }
301
302 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
303                                  unsigned size, bool is_write)
304 {
305     return is_write && size == 2;
306 }
307
308 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
309                                  unsigned size)
310 {
311     return fw_cfg_read(opaque);
312 }
313
314 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
315                               uint64_t value, unsigned size)
316 {
317     switch (size) {
318     case 1:
319         fw_cfg_write(opaque, (uint8_t)value);
320         break;
321     case 2:
322         fw_cfg_select(opaque, (uint16_t)value);
323         break;
324     }
325 }
326
327 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
328                                   unsigned size, bool is_write)
329 {
330     return (size == 1) || (is_write && size == 2);
331 }
332
333 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
334     .write = fw_cfg_ctl_mem_write,
335     .endianness = DEVICE_NATIVE_ENDIAN,
336     .valid.accepts = fw_cfg_ctl_mem_valid,
337 };
338
339 static const MemoryRegionOps fw_cfg_data_mem_ops = {
340     .read = fw_cfg_data_mem_read,
341     .write = fw_cfg_data_mem_write,
342     .endianness = DEVICE_NATIVE_ENDIAN,
343     .valid = {
344         .min_access_size = 1,
345         .max_access_size = 1,
346     },
347 };
348
349 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
350     .read = fw_cfg_comb_read,
351     .write = fw_cfg_comb_write,
352     .endianness = DEVICE_LITTLE_ENDIAN,
353     .valid.accepts = fw_cfg_comb_valid,
354 };
355
356 static void fw_cfg_reset(DeviceState *d)
357 {
358     FWCfgState *s = FW_CFG(d);
359
360     fw_cfg_select(s, 0);
361 }
362
363 /* Save restore 32 bit int as uint16_t
364    This is a Big hack, but it is how the old state did it.
365    Or we broke compatibility in the state, or we can't use struct tm
366  */
367
368 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
369 {
370     uint32_t *v = pv;
371     *v = qemu_get_be16(f);
372     return 0;
373 }
374
375 static void put_unused(QEMUFile *f, void *pv, size_t size)
376 {
377     fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
378     fprintf(stderr, "This functions shouldn't be called.\n");
379 }
380
381 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
382     .name = "int32_as_uint16",
383     .get  = get_uint32_as_uint16,
384     .put  = put_unused,
385 };
386
387 #define VMSTATE_UINT16_HACK(_f, _s, _t)                                    \
388     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
389
390
391 static bool is_version_1(void *opaque, int version_id)
392 {
393     return version_id == 1;
394 }
395
396 static const VMStateDescription vmstate_fw_cfg = {
397     .name = "fw_cfg",
398     .version_id = 2,
399     .minimum_version_id = 1,
400     .fields = (VMStateField[]) {
401         VMSTATE_UINT16(cur_entry, FWCfgState),
402         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
403         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
404         VMSTATE_END_OF_LIST()
405     }
406 };
407
408 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
409                                            FWCfgReadCallback callback,
410                                            void *callback_opaque,
411                                            void *data, size_t len)
412 {
413     int arch = !!(key & FW_CFG_ARCH_LOCAL);
414
415     key &= FW_CFG_ENTRY_MASK;
416
417     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
418
419     s->entries[arch][key].data = data;
420     s->entries[arch][key].len = (uint32_t)len;
421     s->entries[arch][key].read_callback = callback;
422     s->entries[arch][key].callback_opaque = callback_opaque;
423 }
424
425 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
426                                               void *data, size_t len)
427 {
428     void *ptr;
429     int arch = !!(key & FW_CFG_ARCH_LOCAL);
430
431     key &= FW_CFG_ENTRY_MASK;
432
433     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
434
435     /* return the old data to the function caller, avoid memory leak */
436     ptr = s->entries[arch][key].data;
437     s->entries[arch][key].data = data;
438     s->entries[arch][key].len = len;
439     s->entries[arch][key].callback_opaque = NULL;
440     s->entries[arch][key].callback = NULL;
441
442     return ptr;
443 }
444
445 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
446 {
447     fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
448 }
449
450 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
451 {
452     size_t sz = strlen(value) + 1;
453
454     return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
455 }
456
457 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
458 {
459     uint16_t *copy;
460
461     copy = g_malloc(sizeof(value));
462     *copy = cpu_to_le16(value);
463     fw_cfg_add_bytes(s, key, copy, sizeof(value));
464 }
465
466 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
467 {
468     uint32_t *copy;
469
470     copy = g_malloc(sizeof(value));
471     *copy = cpu_to_le32(value);
472     fw_cfg_add_bytes(s, key, copy, sizeof(value));
473 }
474
475 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
476 {
477     uint64_t *copy;
478
479     copy = g_malloc(sizeof(value));
480     *copy = cpu_to_le64(value);
481     fw_cfg_add_bytes(s, key, copy, sizeof(value));
482 }
483
484 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
485                          void *callback_opaque, void *data, size_t len)
486 {
487     int arch = !!(key & FW_CFG_ARCH_LOCAL);
488
489     assert(key & FW_CFG_WRITE_CHANNEL);
490
491     key &= FW_CFG_ENTRY_MASK;
492
493     assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
494
495     s->entries[arch][key].data = data;
496     s->entries[arch][key].len = (uint32_t)len;
497     s->entries[arch][key].callback_opaque = callback_opaque;
498     s->entries[arch][key].callback = callback;
499 }
500
501 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
502                               FWCfgReadCallback callback, void *callback_opaque,
503                               void *data, size_t len)
504 {
505     int i, index;
506     size_t dsize;
507
508     if (!s->files) {
509         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
510         s->files = g_malloc0(dsize);
511         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
512     }
513
514     index = be32_to_cpu(s->files->count);
515     assert(index < FW_CFG_FILE_SLOTS);
516
517     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
518                                    callback, callback_opaque, data, len);
519
520     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
521             filename);
522     for (i = 0; i < index; i++) {
523         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
524             trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
525             return;
526         }
527     }
528
529     s->files->f[index].size   = cpu_to_be32(len);
530     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
531     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
532
533     s->files->count = cpu_to_be32(index+1);
534 }
535
536 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
537                      void *data, size_t len)
538 {
539     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
540 }
541
542 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
543                         void *data, size_t len)
544 {
545     int i, index;
546     void *ptr = NULL;
547
548     assert(s->files);
549
550     index = be32_to_cpu(s->files->count);
551     assert(index < FW_CFG_FILE_SLOTS);
552
553     for (i = 0; i < index; i++) {
554         if (strcmp(filename, s->files->f[i].name) == 0) {
555             ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
556                                            data, len);
557             s->files->f[i].size   = cpu_to_be32(len);
558             return ptr;
559         }
560     }
561     /* add new one */
562     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
563     return NULL;
564 }
565
566 static void fw_cfg_machine_reset(void *opaque)
567 {
568     void *ptr;
569     size_t len;
570     FWCfgState *s = opaque;
571     char *bootindex = get_boot_devices_list(&len, false);
572
573     ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
574     g_free(ptr);
575 }
576
577 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
578 {
579     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
580     qemu_register_reset(fw_cfg_machine_reset, s);
581 }
582
583
584
585 static void fw_cfg_init1(DeviceState *dev)
586 {
587     FWCfgState *s = FW_CFG(dev);
588
589     assert(!object_resolve_path(FW_CFG_PATH, NULL));
590
591     object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
592
593     qdev_init_nofail(dev);
594
595     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
596     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
597     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
598     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
599     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
600     fw_cfg_bootsplash(s);
601     fw_cfg_reboot(s);
602
603     s->machine_ready.notify = fw_cfg_machine_ready;
604     qemu_add_machine_init_done_notifier(&s->machine_ready);
605 }
606
607 FWCfgState *fw_cfg_init_io(uint32_t iobase)
608 {
609     DeviceState *dev;
610
611     dev = qdev_create(NULL, TYPE_FW_CFG_IO);
612     qdev_prop_set_uint32(dev, "iobase", iobase);
613     fw_cfg_init1(dev);
614
615     return FW_CFG(dev);
616 }
617
618 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
619 {
620     DeviceState *dev;
621     SysBusDevice *sbd;
622
623     dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
624     fw_cfg_init1(dev);
625
626     sbd = SYS_BUS_DEVICE(dev);
627     sysbus_mmio_map(sbd, 0, ctl_addr);
628     sysbus_mmio_map(sbd, 1, data_addr);
629
630     return FW_CFG(dev);
631 }
632
633
634 FWCfgState *fw_cfg_find(void)
635 {
636     return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
637 }
638
639 static void fw_cfg_class_init(ObjectClass *klass, void *data)
640 {
641     DeviceClass *dc = DEVICE_CLASS(klass);
642
643     dc->reset = fw_cfg_reset;
644     dc->vmsd = &vmstate_fw_cfg;
645 }
646
647 static const TypeInfo fw_cfg_info = {
648     .name          = TYPE_FW_CFG,
649     .parent        = TYPE_SYS_BUS_DEVICE,
650     .instance_size = sizeof(FWCfgState),
651     .class_init    = fw_cfg_class_init,
652 };
653
654
655 static Property fw_cfg_io_properties[] = {
656     DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
657     DEFINE_PROP_END_OF_LIST(),
658 };
659
660 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
661 {
662     FWCfgIoState *s = FW_CFG_IO(dev);
663     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
664
665     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
666                           FW_CFG(s), "fwcfg", FW_CFG_SIZE);
667     sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
668 }
669
670 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
671 {
672     DeviceClass *dc = DEVICE_CLASS(klass);
673
674     dc->realize = fw_cfg_io_realize;
675     dc->props = fw_cfg_io_properties;
676 }
677
678 static const TypeInfo fw_cfg_io_info = {
679     .name          = TYPE_FW_CFG_IO,
680     .parent        = TYPE_FW_CFG,
681     .instance_size = sizeof(FWCfgIoState),
682     .class_init    = fw_cfg_io_class_init,
683 };
684
685
686 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
687 {
688     FWCfgMemState *s = FW_CFG_MEM(dev);
689     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
690
691     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
692                           FW_CFG(s), "fwcfg.ctl", FW_CFG_SIZE);
693     sysbus_init_mmio(sbd, &s->ctl_iomem);
694
695     memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops,
696                           FW_CFG(s), "fwcfg.data",
697                           fw_cfg_data_mem_ops.valid.max_access_size);
698     sysbus_init_mmio(sbd, &s->data_iomem);
699 }
700
701 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
702 {
703     DeviceClass *dc = DEVICE_CLASS(klass);
704
705     dc->realize = fw_cfg_mem_realize;
706 }
707
708 static const TypeInfo fw_cfg_mem_info = {
709     .name          = TYPE_FW_CFG_MEM,
710     .parent        = TYPE_FW_CFG,
711     .instance_size = sizeof(FWCfgMemState),
712     .class_init    = fw_cfg_mem_class_init,
713 };
714
715
716 static void fw_cfg_register_types(void)
717 {
718     type_register_static(&fw_cfg_info);
719     type_register_static(&fw_cfg_io_info);
720     type_register_static(&fw_cfg_mem_info);
721 }
722
723 type_init(fw_cfg_register_types)
This page took 0.065966 seconds and 4 git commands to generate.