hw/riscv: virt: Create a platform bus
[qemu/ar7.git] / hw / ipack / ipack.c
blobae20f36da68c61228afb27ed750645188ea4de41
1 /*
2 * QEMU IndustryPack emulation
4 * Copyright (C) 2012 Igalia, S.L.
5 * Author: Alberto Garcia <berto@igalia.com>
7 * This code is licensed under the GNU GPL v2 or (at your option) any
8 * later version.
9 */
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 "hw/qdev-properties.h"
17 #include "migration/vmstate.h"
19 IPackDevice *ipack_device_find(IPackBus *bus, int32_t slot)
21 BusChild *kid;
23 QTAILQ_FOREACH(kid, &BUS(bus)->children, sibling) {
24 DeviceState *qdev = kid->child;
25 IPackDevice *ip = IPACK_DEVICE(qdev);
26 if (ip->slot == slot) {
27 return ip;
30 return NULL;
33 void ipack_bus_init(IPackBus *bus, size_t bus_size,
34 DeviceState *parent,
35 uint8_t n_slots,
36 qemu_irq_handler handler)
38 qbus_init(bus, bus_size, TYPE_IPACK_BUS, parent, NULL);
39 bus->n_slots = n_slots;
40 bus->set_irq = handler;
43 static void ipack_device_realize(DeviceState *dev, Error **errp)
45 IPackDevice *idev = IPACK_DEVICE(dev);
46 IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(dev));
47 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
49 if (idev->slot < 0) {
50 idev->slot = bus->free_slot;
52 if (idev->slot >= bus->n_slots) {
53 error_setg(errp, "Only %" PRIu8 " slots available.", bus->n_slots);
54 return;
56 bus->free_slot = idev->slot + 1;
58 idev->irq = qemu_allocate_irqs(bus->set_irq, idev, 2);
60 k->realize(dev, errp);
63 static void ipack_device_unrealize(DeviceState *dev)
65 IPackDevice *idev = IPACK_DEVICE(dev);
66 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
68 if (k->unrealize) {
69 k->unrealize(dev);
70 return;
73 qemu_free_irqs(idev->irq, 2);
76 static Property ipack_device_props[] = {
77 DEFINE_PROP_INT32("slot", IPackDevice, slot, -1),
78 DEFINE_PROP_END_OF_LIST()
81 static void ipack_device_class_init(ObjectClass *klass, void *data)
83 DeviceClass *k = DEVICE_CLASS(klass);
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 device_class_set_props(k, ipack_device_props);
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()
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,
111 static const TypeInfo ipack_bus_info = {
112 .name = TYPE_IPACK_BUS,
113 .parent = TYPE_BUS,
114 .instance_size = sizeof(IPackBus),
117 static void ipack_register_types(void)
119 type_register_static(&ipack_device_info);
120 type_register_static(&ipack_bus_info);
123 type_init(ipack_register_types)