xref: /openbmc/qemu/hw/ipack/ipack.c (revision 64552b6b)
1 /*
2  * QEMU IndustryPack emulation
3  *
4  * Copyright (C) 2012 Igalia, S.L.
5  * Author: Alberto Garcia <berto@igalia.com>
6  *
7  * This code is licensed under the GNU GPL v2 or (at your option) any
8  * later version.
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qemu/module.h"
14 #include "hw/ipack/ipack.h"
15 #include "hw/irq.h"
16 
17 IPackDevice *ipack_device_find(IPackBus *bus, int32_t slot)
18 {
19     BusChild *kid;
20 
21     QTAILQ_FOREACH(kid, &BUS(bus)->children, sibling) {
22         DeviceState *qdev = kid->child;
23         IPackDevice *ip = IPACK_DEVICE(qdev);
24         if (ip->slot == slot) {
25             return ip;
26         }
27     }
28     return NULL;
29 }
30 
31 void ipack_bus_new_inplace(IPackBus *bus, size_t bus_size,
32                            DeviceState *parent,
33                            const char *name, uint8_t n_slots,
34                            qemu_irq_handler handler)
35 {
36     qbus_create_inplace(bus, bus_size, TYPE_IPACK_BUS, parent, name);
37     bus->n_slots = n_slots;
38     bus->set_irq = handler;
39 }
40 
41 static void ipack_device_realize(DeviceState *dev, Error **errp)
42 {
43     IPackDevice *idev = IPACK_DEVICE(dev);
44     IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(dev));
45     IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
46 
47     if (idev->slot < 0) {
48         idev->slot = bus->free_slot;
49     }
50     if (idev->slot >= bus->n_slots) {
51         error_setg(errp, "Only %" PRIu8 " slots available.", bus->n_slots);
52         return;
53     }
54     bus->free_slot = idev->slot + 1;
55 
56     idev->irq = qemu_allocate_irqs(bus->set_irq, idev, 2);
57 
58     k->realize(dev, errp);
59 }
60 
61 static void ipack_device_unrealize(DeviceState *dev, Error **errp)
62 {
63     IPackDevice *idev = IPACK_DEVICE(dev);
64     IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
65     Error *err = NULL;
66 
67     if (k->unrealize) {
68         k->unrealize(dev, &err);
69         error_propagate(errp, err);
70         return;
71     }
72 
73     qemu_free_irqs(idev->irq, 2);
74 }
75 
76 static Property ipack_device_props[] = {
77     DEFINE_PROP_INT32("slot", IPackDevice, slot, -1),
78     DEFINE_PROP_END_OF_LIST()
79 };
80 
81 static void ipack_device_class_init(ObjectClass *klass, void *data)
82 {
83     DeviceClass *k = DEVICE_CLASS(klass);
84 
85     set_bit(DEVICE_CATEGORY_INPUT, k->categories);
86     k->bus_type = TYPE_IPACK_BUS;
87     k->realize = ipack_device_realize;
88     k->unrealize = ipack_device_unrealize;
89     k->props = ipack_device_props;
90 }
91 
92 const VMStateDescription vmstate_ipack_device = {
93     .name = "ipack_device",
94     .version_id = 1,
95     .minimum_version_id = 1,
96     .fields = (VMStateField[]) {
97         VMSTATE_INT32(slot, IPackDevice),
98         VMSTATE_END_OF_LIST()
99     }
100 };
101 
102 static const TypeInfo ipack_device_info = {
103     .name          = TYPE_IPACK_DEVICE,
104     .parent        = TYPE_DEVICE,
105     .instance_size = sizeof(IPackDevice),
106     .class_size    = sizeof(IPackDeviceClass),
107     .class_init    = ipack_device_class_init,
108     .abstract      = true,
109 };
110 
111 static const TypeInfo ipack_bus_info = {
112     .name = TYPE_IPACK_BUS,
113     .parent = TYPE_BUS,
114     .instance_size = sizeof(IPackBus),
115 };
116 
117 static void ipack_register_types(void)
118 {
119     type_register_static(&ipack_device_info);
120     type_register_static(&ipack_bus_info);
121 }
122 
123 type_init(ipack_register_types)
124