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