xref: /openbmc/qemu/hw/nubus/nubus-bus.c (revision ce0e6a2c55b13c16de4a7985c0ca41835420430a)
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 /*
12  * References:
13  *   Nubus Specification (TI)
14  *     http://www.bitsavers.org/pdf/ti/nubus/2242825-0001_NuBus_Spec1983.pdf
15  *
16  *   Designing Cards and Drivers for the Macintosh Family (Apple)
17  */
18 
19 #include "qemu/osdep.h"
20 #include "hw/nubus/nubus.h"
21 #include "qapi/error.h"
22 #include "trace.h"
23 
24 
25 static NubusBus *nubus_find(void)
26 {
27     /* Returns NULL unless there is exactly one nubus device */
28     return NUBUS_BUS(object_resolve_path_type("", TYPE_NUBUS_BUS, NULL));
29 }
30 
31 static void nubus_slot_write(void *opaque, hwaddr addr, uint64_t val,
32                              unsigned int size)
33 {
34     /* read only */
35     trace_nubus_slot_write(addr, val, size);
36 }
37 
38 static uint64_t nubus_slot_read(void *opaque, hwaddr addr,
39                                 unsigned int size)
40 {
41     trace_nubus_slot_read(addr, size);
42     return 0;
43 }
44 
45 static const MemoryRegionOps nubus_slot_ops = {
46     .read  = nubus_slot_read,
47     .write = nubus_slot_write,
48     .endianness = DEVICE_BIG_ENDIAN,
49     .valid = {
50         .min_access_size = 1,
51         .max_access_size = 4,
52     },
53 };
54 
55 static void nubus_super_slot_write(void *opaque, hwaddr addr, uint64_t val,
56                                    unsigned int size)
57 {
58     /* read only */
59     trace_nubus_super_slot_write(addr, val, size);
60 }
61 
62 static uint64_t nubus_super_slot_read(void *opaque, hwaddr addr,
63                                       unsigned int size)
64 {
65     trace_nubus_super_slot_read(addr, size);
66     return 0;
67 }
68 
69 static const MemoryRegionOps nubus_super_slot_ops = {
70     .read  = nubus_super_slot_read,
71     .write = nubus_super_slot_write,
72     .endianness = DEVICE_BIG_ENDIAN,
73     .valid = {
74         .min_access_size = 1,
75         .max_access_size = 4,
76     },
77 };
78 
79 static void nubus_realize(BusState *bus, Error **errp)
80 {
81     if (!nubus_find()) {
82         error_setg(errp, "at most one %s device is permitted", TYPE_NUBUS_BUS);
83         return;
84     }
85 }
86 
87 static void nubus_init(Object *obj)
88 {
89     NubusBus *nubus = NUBUS_BUS(obj);
90 
91     memory_region_init_io(&nubus->super_slot_io, obj, &nubus_super_slot_ops,
92                           nubus, "nubus-super-slots",
93                           (NUBUS_SUPER_SLOT_NB + 1) * NUBUS_SUPER_SLOT_SIZE);
94 
95     memory_region_init_io(&nubus->slot_io, obj, &nubus_slot_ops,
96                           nubus, "nubus-slots",
97                           NUBUS_SLOT_NB * NUBUS_SLOT_SIZE);
98 
99     nubus->slot_available_mask = MAKE_64BIT_MASK(NUBUS_FIRST_SLOT,
100                                                  NUBUS_SLOT_NB);
101 }
102 
103 static char *nubus_get_dev_path(DeviceState *dev)
104 {
105     NubusDevice *nd = NUBUS_DEVICE(dev);
106     BusState *bus = qdev_get_parent_bus(dev);
107     char *p = qdev_get_dev_path(bus->parent);
108 
109     if (p) {
110         char *ret = g_strdup_printf("%s/%s/%02x", p, bus->name, nd->slot);
111         g_free(p);
112         return ret;
113     } else {
114         return g_strdup_printf("%s/%02x", bus->name, nd->slot);
115     }
116 }
117 
118 static bool nubus_check_address(BusState *bus, DeviceState *dev, Error **errp)
119 {
120     NubusDevice *nd = NUBUS_DEVICE(dev);
121     NubusBus *nubus = NUBUS_BUS(bus);
122 
123     if (nd->slot == -1) {
124         /* No slot specified, find first available free slot */
125         int s = ctz32(nubus->slot_available_mask);
126         if (s != 32) {
127             nd->slot = s;
128         } else {
129             error_setg(errp, "Cannot register nubus card, no free slot "
130                              "available");
131             return false;
132         }
133     } else {
134         /* Slot specified, make sure the slot is available */
135         if (!(nubus->slot_available_mask & BIT(nd->slot))) {
136             error_setg(errp, "Cannot register nubus card, slot %d is "
137                              "unavailable or already occupied", nd->slot);
138             return false;
139         }
140     }
141 
142     nubus->slot_available_mask &= ~BIT(nd->slot);
143     return true;
144 }
145 
146 static void nubus_class_init(ObjectClass *oc, void *data)
147 {
148     BusClass *bc = BUS_CLASS(oc);
149 
150     bc->realize = nubus_realize;
151     bc->check_address = nubus_check_address;
152     bc->get_dev_path = nubus_get_dev_path;
153 }
154 
155 static const TypeInfo nubus_bus_info = {
156     .name = TYPE_NUBUS_BUS,
157     .parent = TYPE_BUS,
158     .instance_size = sizeof(NubusBus),
159     .instance_init = nubus_init,
160     .class_init = nubus_class_init,
161 };
162 
163 static void nubus_register_types(void)
164 {
165     type_register_static(&nubus_bus_info);
166 }
167 
168 type_init(nubus_register_types)
169