xref: /openbmc/qemu/hw/nubus/nubus-bus.c (revision c10a576c19665a617ce00cb93afd115142b6a7af)
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 bool nubus_check_address(BusState *bus, DeviceState *dev, Error **errp)
100 {
101     NubusDevice *nd = NUBUS_DEVICE(dev);
102     NubusBus *nubus = NUBUS_BUS(bus);
103 
104     if (nd->slot == -1) {
105         /* No slot specified, find first available free slot */
106         int s = ctz32(nubus->slot_available_mask);
107         if (s != 32) {
108             nd->slot = s;
109         } else {
110             error_setg(errp, "Cannot register nubus card, no free slot "
111                              "available");
112             return false;
113         }
114     } else {
115         /* Slot specified, make sure the slot is available */
116         if (!(nubus->slot_available_mask & BIT(nd->slot))) {
117             error_setg(errp, "Cannot register nubus card, slot %d is "
118                              "unavailable or already occupied", nd->slot);
119             return false;
120         }
121     }
122 
123     nubus->slot_available_mask &= ~BIT(nd->slot);
124     return true;
125 }
126 
127 static void nubus_class_init(ObjectClass *oc, void *data)
128 {
129     BusClass *bc = BUS_CLASS(oc);
130 
131     bc->realize = nubus_realize;
132     bc->check_address = nubus_check_address;
133 }
134 
135 static const TypeInfo nubus_bus_info = {
136     .name = TYPE_NUBUS_BUS,
137     .parent = TYPE_BUS,
138     .instance_size = sizeof(NubusBus),
139     .instance_init = nubus_init,
140     .class_init = nubus_class_init,
141 };
142 
143 static void nubus_register_types(void)
144 {
145     type_register_static(&nubus_bus_info);
146 }
147 
148 type_init(nubus_register_types)
149