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