xref: /openbmc/qemu/hw/nubus/nubus-device.c (revision 2f95279a)
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     /* Super */
39     slot_offset = nd->slot * NUBUS_SUPER_SLOT_SIZE;
40 
41     name = g_strdup_printf("nubus-super-slot-%x", nd->slot);
42     memory_region_init(&nd->super_slot_mem, OBJECT(dev), name,
43                        NUBUS_SUPER_SLOT_SIZE);
44     memory_region_add_subregion(&nubus->super_slot_io, slot_offset,
45                                 &nd->super_slot_mem);
46     g_free(name);
47 
48     /* Normal */
49     slot_offset = nd->slot * NUBUS_SLOT_SIZE;
50 
51     name = g_strdup_printf("nubus-slot-%x", nd->slot);
52     memory_region_init(&nd->slot_mem, OBJECT(dev), name, NUBUS_SLOT_SIZE);
53     memory_region_add_subregion(&nubus->slot_io, slot_offset,
54                                 &nd->slot_mem);
55     g_free(name);
56 
57     /* Declaration ROM */
58     if (nd->romfile != NULL) {
59         path = qemu_find_file(QEMU_FILE_TYPE_BIOS, nd->romfile);
60         if (path == NULL) {
61             path = g_strdup(nd->romfile);
62         }
63 
64         size = get_image_size(path);
65         if (size < 0) {
66             error_setg(errp, "failed to find romfile \"%s\"", nd->romfile);
67             g_free(path);
68             return;
69         } else if (size == 0) {
70             error_setg(errp, "romfile \"%s\" is empty", nd->romfile);
71             g_free(path);
72             return;
73         } else if (size > NUBUS_DECL_ROM_MAX_SIZE) {
74             error_setg(errp, "romfile \"%s\" too large (maximum size 128K)",
75                        nd->romfile);
76             g_free(path);
77             return;
78         }
79 
80         name = g_strdup_printf("nubus-slot-%x-declaration-rom", nd->slot);
81 
82         /*
83          * Ensure ROM memory region is aligned to target page size regardless
84          * of the size of the Declaration ROM image
85          */
86         align_size = ROUND_UP(size, qemu_target_page_size());
87         memory_region_init_rom(&nd->decl_rom, OBJECT(dev), name, align_size,
88                                &error_abort);
89         rom_ptr = memory_region_get_ram_ptr(&nd->decl_rom);
90         ret = load_image_size(path, rom_ptr + (uintptr_t)(align_size - size),
91                               size);
92         g_free(path);
93         g_free(name);
94         if (ret < 0) {
95             error_setg(errp, "could not load romfile \"%s\"", nd->romfile);
96             return;
97         }
98         memory_region_add_subregion(&nd->slot_mem, NUBUS_SLOT_SIZE - align_size,
99                                     &nd->decl_rom);
100     }
101 }
102 
103 static Property nubus_device_properties[] = {
104     DEFINE_PROP_INT32("slot", NubusDevice, slot, -1),
105     DEFINE_PROP_STRING("romfile", NubusDevice, romfile),
106     DEFINE_PROP_END_OF_LIST()
107 };
108 
109 static void nubus_device_class_init(ObjectClass *oc, void *data)
110 {
111     DeviceClass *dc = DEVICE_CLASS(oc);
112 
113     dc->realize = nubus_device_realize;
114     dc->bus_type = TYPE_NUBUS_BUS;
115     device_class_set_props(dc, nubus_device_properties);
116 }
117 
118 static const TypeInfo nubus_device_type_info = {
119     .name = TYPE_NUBUS_DEVICE,
120     .parent = TYPE_DEVICE,
121     .abstract = true,
122     .instance_size = sizeof(NubusDevice),
123     .class_init = nubus_device_class_init,
124 };
125 
126 static void nubus_register_types(void)
127 {
128     type_register_static(&nubus_device_type_info);
129 }
130 
131 type_init(nubus_register_types)
132