xref: /openbmc/qemu/hw/misc/empty_slot.c (revision da278d58)
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 "trace.h"
17 
18 #define TYPE_EMPTY_SLOT "empty_slot"
19 #define EMPTY_SLOT(obj) OBJECT_CHECK(EmptySlot, (obj), TYPE_EMPTY_SLOT)
20 
21 typedef struct EmptySlot {
22     SysBusDevice parent_obj;
23 
24     MemoryRegion iomem;
25     char *name;
26     uint64_t size;
27 } EmptySlot;
28 
29 static uint64_t empty_slot_read(void *opaque, hwaddr addr,
30                                 unsigned size)
31 {
32     EmptySlot *s = EMPTY_SLOT(opaque);
33 
34     trace_empty_slot_write(addr, size << 1, 0, size, s->name);
35 
36     return 0;
37 }
38 
39 static void empty_slot_write(void *opaque, hwaddr addr,
40                              uint64_t val, unsigned size)
41 {
42     EmptySlot *s = EMPTY_SLOT(opaque);
43 
44     trace_empty_slot_write(addr, size << 1, val, size, s->name);
45 }
46 
47 static const MemoryRegionOps empty_slot_ops = {
48     .read = empty_slot_read,
49     .write = empty_slot_write,
50     .endianness = DEVICE_NATIVE_ENDIAN,
51 };
52 
53 void empty_slot_init(const char *name, hwaddr addr, uint64_t slot_size)
54 {
55     if (slot_size > 0) {
56         /* Only empty slots larger than 0 byte need handling. */
57         DeviceState *dev;
58 
59         dev = qdev_create(NULL, TYPE_EMPTY_SLOT);
60 
61         qdev_prop_set_uint64(dev, "size", slot_size);
62         qdev_init_nofail(dev);
63 
64         sysbus_mmio_map_overlap(SYS_BUS_DEVICE(dev), 0, addr, -10000);
65     }
66 }
67 
68 static void empty_slot_realize(DeviceState *dev, Error **errp)
69 {
70     EmptySlot *s = EMPTY_SLOT(dev);
71 
72     if (s->name == NULL) {
73         s->name = g_strdup("empty-slot");
74     }
75     memory_region_init_io(&s->iomem, OBJECT(s), &empty_slot_ops, s,
76                           s->name, s->size);
77     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
78 }
79 
80 static Property empty_slot_properties[] = {
81     DEFINE_PROP_UINT64("size", EmptySlot, size, 0),
82     DEFINE_PROP_STRING("name", EmptySlot, name),
83     DEFINE_PROP_END_OF_LIST(),
84 };
85 
86 static void empty_slot_class_init(ObjectClass *klass, void *data)
87 {
88     DeviceClass *dc = DEVICE_CLASS(klass);
89 
90     dc->realize = empty_slot_realize;
91     device_class_set_props(dc, empty_slot_properties);
92     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
93 }
94 
95 static const TypeInfo empty_slot_info = {
96     .name          = TYPE_EMPTY_SLOT,
97     .parent        = TYPE_SYS_BUS_DEVICE,
98     .instance_size = sizeof(EmptySlot),
99     .class_init    = empty_slot_class_init,
100 };
101 
102 static void empty_slot_register_types(void)
103 {
104     type_register_static(&empty_slot_info);
105 }
106 
107 type_init(empty_slot_register_types)
108