xref: /openbmc/qemu/hw/nubus/nubus-device.c (revision 154fd4d1)
1 /*
2  * QEMU Macintosh Nubus
3  *
4  * Copyright (c) 2013-2018 Laurent Vivier <laurent@vivier.eu>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qemu/datadir.h"
13 #include "exec/target_page.h"
14 #include "hw/irq.h"
15 #include "hw/loader.h"
16 #include "hw/nubus/nubus.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 
20 
21 void nubus_set_irq(NubusDevice *nd, int level)
22 {
23     NubusBus *nubus = NUBUS_BUS(qdev_get_parent_bus(DEVICE(nd)));
24 
25     qemu_set_irq(nubus->irqs[nd->slot], level);
26 }
27 
28 static void nubus_device_realize(DeviceState *dev, Error **errp)
29 {
30     NubusBus *nubus = NUBUS_BUS(qdev_get_parent_bus(dev));
31     NubusDevice *nd = NUBUS_DEVICE(dev);
32     char *name, *path;
33     hwaddr slot_offset;
34     int64_t size, align_size;
35     uint8_t *rom_ptr;
36     int ret;
37 
38     if (nd->slot < 0 || nd->slot >= NUBUS_SLOT_NB) {
39         error_setg(errp,
40                    "'slot' value %d out of range (must be between 0 and %d)",
41                    nd->slot, NUBUS_SLOT_NB - 1);
42         return;
43     }
44 
45     /* Super */
46     slot_offset = nd->slot * NUBUS_SUPER_SLOT_SIZE;
47 
48     name = g_strdup_printf("nubus-super-slot-%x", nd->slot);
49     memory_region_init(&nd->super_slot_mem, OBJECT(dev), name,
50                        NUBUS_SUPER_SLOT_SIZE);
51     memory_region_add_subregion(&nubus->super_slot_io, slot_offset,
52                                 &nd->super_slot_mem);
53     g_free(name);
54 
55     /* Normal */
56     slot_offset = nd->slot * NUBUS_SLOT_SIZE;
57 
58     name = g_strdup_printf("nubus-slot-%x", nd->slot);
59     memory_region_init(&nd->slot_mem, OBJECT(dev), name, NUBUS_SLOT_SIZE);
60     memory_region_add_subregion(&nubus->slot_io, slot_offset,
61                                 &nd->slot_mem);
62     g_free(name);
63 
64     /* Declaration ROM */
65     if (nd->romfile != NULL) {
66         path = qemu_find_file(QEMU_FILE_TYPE_BIOS, nd->romfile);
67         if (path == NULL) {
68             path = g_strdup(nd->romfile);
69         }
70 
71         size = get_image_size(path);
72         if (size < 0) {
73             error_setg(errp, "failed to find romfile \"%s\"", nd->romfile);
74             g_free(path);
75             return;
76         } else if (size == 0) {
77             error_setg(errp, "romfile \"%s\" is empty", nd->romfile);
78             g_free(path);
79             return;
80         } else if (size > NUBUS_DECL_ROM_MAX_SIZE) {
81             error_setg(errp, "romfile \"%s\" too large (maximum size 128K)",
82                        nd->romfile);
83             g_free(path);
84             return;
85         }
86 
87         name = g_strdup_printf("nubus-slot-%x-declaration-rom", nd->slot);
88 
89         /*
90          * Ensure ROM memory region is aligned to target page size regardless
91          * of the size of the Declaration ROM image
92          */
93         align_size = ROUND_UP(size, qemu_target_page_size());
94         memory_region_init_rom(&nd->decl_rom, OBJECT(dev), name, align_size,
95                                &error_abort);
96         rom_ptr = memory_region_get_ram_ptr(&nd->decl_rom);
97         ret = load_image_size(path, rom_ptr + (uintptr_t)(align_size - size),
98                               size);
99         g_free(path);
100         g_free(name);
101         if (ret < 0) {
102             error_setg(errp, "could not load romfile \"%s\"", nd->romfile);
103             return;
104         }
105         memory_region_add_subregion(&nd->slot_mem, NUBUS_SLOT_SIZE - align_size,
106                                     &nd->decl_rom);
107     }
108 }
109 
110 static Property nubus_device_properties[] = {
111     DEFINE_PROP_INT32("slot", NubusDevice, slot, -1),
112     DEFINE_PROP_STRING("romfile", NubusDevice, romfile),
113     DEFINE_PROP_END_OF_LIST()
114 };
115 
116 static void nubus_device_class_init(ObjectClass *oc, void *data)
117 {
118     DeviceClass *dc = DEVICE_CLASS(oc);
119 
120     dc->realize = nubus_device_realize;
121     dc->bus_type = TYPE_NUBUS_BUS;
122     device_class_set_props(dc, nubus_device_properties);
123 }
124 
125 static const TypeInfo nubus_device_type_info = {
126     .name = TYPE_NUBUS_DEVICE,
127     .parent = TYPE_DEVICE,
128     .abstract = true,
129     .instance_size = sizeof(NubusDevice),
130     .class_init = nubus_device_class_init,
131 };
132 
133 static void nubus_register_types(void)
134 {
135     type_register_static(&nubus_device_type_info);
136 }
137 
138 type_init(nubus_register_types)
139