xref: /openbmc/qemu/hw/misc/empty_slot.c (revision f7160f32)
1 /*
2  * QEMU Empty Slot
3  *
4  * The empty_slot device emulates known to a bus but not connected devices.
5  *
6  * Copyright (c) 2010 Artyom Tarasenko
7  *
8  * This code is licensed under the GNU GPL v2 or (at your option) any later
9  * version.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "hw/sysbus.h"
14 #include "hw/qdev-properties.h"
15 #include "hw/misc/empty_slot.h"
16 #include "qapi/error.h"
17 #include "trace.h"
18 
19 #define TYPE_EMPTY_SLOT "empty_slot"
20 #define EMPTY_SLOT(obj) OBJECT_CHECK(EmptySlot, (obj), TYPE_EMPTY_SLOT)
21 
22 typedef struct EmptySlot {
23     SysBusDevice parent_obj;
24 
25     MemoryRegion iomem;
26     char *name;
27     uint64_t size;
28 } EmptySlot;
29 
30 static uint64_t empty_slot_read(void *opaque, hwaddr addr,
31                                 unsigned size)
32 {
33     EmptySlot *s = EMPTY_SLOT(opaque);
34 
35     trace_empty_slot_write(addr, size << 1, 0, size, s->name);
36 
37     return 0;
38 }
39 
40 static void empty_slot_write(void *opaque, hwaddr addr,
41                              uint64_t val, unsigned size)
42 {
43     EmptySlot *s = EMPTY_SLOT(opaque);
44 
45     trace_empty_slot_write(addr, size << 1, val, size, s->name);
46 }
47 
48 static const MemoryRegionOps empty_slot_ops = {
49     .read = empty_slot_read,
50     .write = empty_slot_write,
51     .endianness = DEVICE_NATIVE_ENDIAN,
52 };
53 
54 void empty_slot_init(const char *name, hwaddr addr, uint64_t slot_size)
55 {
56     if (slot_size > 0) {
57         /* Only empty slots larger than 0 byte need handling. */
58         DeviceState *dev;
59 
60         dev = qdev_new(TYPE_EMPTY_SLOT);
61 
62         qdev_prop_set_uint64(dev, "size", slot_size);
63         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
64 
65         sysbus_mmio_map_overlap(SYS_BUS_DEVICE(dev), 0, addr, -10000);
66     }
67 }
68 
69 static void empty_slot_realize(DeviceState *dev, Error **errp)
70 {
71     EmptySlot *s = EMPTY_SLOT(dev);
72 
73     if (s->name == NULL) {
74         s->name = g_strdup("empty-slot");
75     }
76     memory_region_init_io(&s->iomem, OBJECT(s), &empty_slot_ops, s,
77                           s->name, s->size);
78     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
79 }
80 
81 static Property empty_slot_properties[] = {
82     DEFINE_PROP_UINT64("size", EmptySlot, size, 0),
83     DEFINE_PROP_STRING("name", EmptySlot, name),
84     DEFINE_PROP_END_OF_LIST(),
85 };
86 
87 static void empty_slot_class_init(ObjectClass *klass, void *data)
88 {
89     DeviceClass *dc = DEVICE_CLASS(klass);
90 
91     dc->realize = empty_slot_realize;
92     device_class_set_props(dc, empty_slot_properties);
93     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
94 }
95 
96 static const TypeInfo empty_slot_info = {
97     .name          = TYPE_EMPTY_SLOT,
98     .parent        = TYPE_SYS_BUS_DEVICE,
99     .instance_size = sizeof(EmptySlot),
100     .class_init    = empty_slot_class_init,
101 };
102 
103 static void empty_slot_register_types(void)
104 {
105     type_register_static(&empty_slot_info);
106 }
107 
108 type_init(empty_slot_register_types)
109