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