xref: /qemu/hw/ipack/ipack.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
19c16fa79SAlberto Garcia /*
29c16fa79SAlberto Garcia  * QEMU IndustryPack emulation
39c16fa79SAlberto Garcia  *
49c16fa79SAlberto Garcia  * Copyright (C) 2012 Igalia, S.L.
5b996aed5SAlberto Garcia  * Author: Alberto Garcia <berto@igalia.com>
69c16fa79SAlberto Garcia  *
79c16fa79SAlberto Garcia  * This code is licensed under the GNU GPL v2 or (at your option) any
89c16fa79SAlberto Garcia  * later version.
99c16fa79SAlberto Garcia  */
109c16fa79SAlberto Garcia 
110430891cSPeter Maydell #include "qemu/osdep.h"
12da34e65cSMarkus Armbruster #include "qapi/error.h"
130b8fa32fSMarkus Armbruster #include "qemu/module.h"
141f9c4cfdSAndreas Färber #include "hw/ipack/ipack.h"
1564552b6bSMarkus Armbruster #include "hw/irq.h"
16a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
17d6454270SMarkus Armbruster #include "migration/vmstate.h"
189c16fa79SAlberto Garcia 
ipack_device_find(IPackBus * bus,int32_t slot)199c16fa79SAlberto Garcia IPackDevice *ipack_device_find(IPackBus *bus, int32_t slot)
209c16fa79SAlberto Garcia {
219c16fa79SAlberto Garcia     BusChild *kid;
229c16fa79SAlberto Garcia 
239c16fa79SAlberto Garcia     QTAILQ_FOREACH(kid, &BUS(bus)->children, sibling) {
249c16fa79SAlberto Garcia         DeviceState *qdev = kid->child;
259c16fa79SAlberto Garcia         IPackDevice *ip = IPACK_DEVICE(qdev);
269c16fa79SAlberto Garcia         if (ip->slot == slot) {
279c16fa79SAlberto Garcia             return ip;
289c16fa79SAlberto Garcia         }
299c16fa79SAlberto Garcia     }
309c16fa79SAlberto Garcia     return NULL;
319c16fa79SAlberto Garcia }
329c16fa79SAlberto Garcia 
ipack_bus_init(IPackBus * bus,size_t bus_size,DeviceState * parent,uint8_t n_slots,qemu_irq_handler handler)3343417c0cSPeter Maydell void ipack_bus_init(IPackBus *bus, size_t bus_size,
3477cbb28aSAndreas Färber                     DeviceState *parent,
3543417c0cSPeter Maydell                     uint8_t n_slots,
369c16fa79SAlberto Garcia                     qemu_irq_handler handler)
379c16fa79SAlberto Garcia {
38d637e1dcSPeter Maydell     qbus_init(bus, bus_size, TYPE_IPACK_BUS, parent, NULL);
399c16fa79SAlberto Garcia     bus->n_slots = n_slots;
409c16fa79SAlberto Garcia     bus->set_irq = handler;
419c16fa79SAlberto Garcia }
429c16fa79SAlberto Garcia 
ipack_device_realize(DeviceState * dev,Error ** errp)435c570902SAndreas Färber static void ipack_device_realize(DeviceState *dev, Error **errp)
449c16fa79SAlberto Garcia {
455c570902SAndreas Färber     IPackDevice *idev = IPACK_DEVICE(dev);
465c570902SAndreas Färber     IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(dev));
479c16fa79SAlberto Garcia     IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
489c16fa79SAlberto Garcia 
495c570902SAndreas Färber     if (idev->slot < 0) {
505c570902SAndreas Färber         idev->slot = bus->free_slot;
519c16fa79SAlberto Garcia     }
525c570902SAndreas Färber     if (idev->slot >= bus->n_slots) {
535c570902SAndreas Färber         error_setg(errp, "Only %" PRIu8 " slots available.", bus->n_slots);
545c570902SAndreas Färber         return;
559c16fa79SAlberto Garcia     }
565c570902SAndreas Färber     bus->free_slot = idev->slot + 1;
579c16fa79SAlberto Garcia 
58d50280afSPhilippe Mathieu-Daudé     qemu_init_irqs(idev->irq, ARRAY_SIZE(idev->irq), bus->set_irq, idev);
599c16fa79SAlberto Garcia 
605c570902SAndreas Färber     k->realize(dev, errp);
619c16fa79SAlberto Garcia }
629c16fa79SAlberto Garcia 
ipack_device_unrealize(DeviceState * dev)63b69c3c21SMarkus Armbruster static void ipack_device_unrealize(DeviceState *dev)
649c16fa79SAlberto Garcia {
659c16fa79SAlberto Garcia     IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
669c16fa79SAlberto Garcia 
675c570902SAndreas Färber     if (k->unrealize) {
68b69c3c21SMarkus Armbruster         k->unrealize(dev);
695c570902SAndreas Färber         return;
709c16fa79SAlberto Garcia     }
719c16fa79SAlberto Garcia }
729c16fa79SAlberto Garcia 
739811813eSRichard Henderson static const Property ipack_device_props[] = {
749c16fa79SAlberto Garcia     DEFINE_PROP_INT32("slot", IPackDevice, slot, -1),
759c16fa79SAlberto Garcia };
769c16fa79SAlberto Garcia 
ipack_device_class_init(ObjectClass * klass,const void * data)77*12d1a768SPhilippe Mathieu-Daudé static void ipack_device_class_init(ObjectClass *klass, const void *data)
789c16fa79SAlberto Garcia {
799c16fa79SAlberto Garcia     DeviceClass *k = DEVICE_CLASS(klass);
805c570902SAndreas Färber 
81125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_INPUT, k->categories);
829c16fa79SAlberto Garcia     k->bus_type = TYPE_IPACK_BUS;
835c570902SAndreas Färber     k->realize = ipack_device_realize;
845c570902SAndreas Färber     k->unrealize = ipack_device_unrealize;
854f67d30bSMarc-André Lureau     device_class_set_props(k, ipack_device_props);
869c16fa79SAlberto Garcia }
879c16fa79SAlberto Garcia 
889c16fa79SAlberto Garcia const VMStateDescription vmstate_ipack_device = {
899c16fa79SAlberto Garcia     .name = "ipack_device",
909c16fa79SAlberto Garcia     .version_id = 1,
919c16fa79SAlberto Garcia     .minimum_version_id = 1,
928913d05dSRichard Henderson     .fields = (const VMStateField[]) {
939c16fa79SAlberto Garcia         VMSTATE_INT32(slot, IPackDevice),
949c16fa79SAlberto Garcia         VMSTATE_END_OF_LIST()
959c16fa79SAlberto Garcia     }
969c16fa79SAlberto Garcia };
979c16fa79SAlberto Garcia 
989c16fa79SAlberto Garcia static const TypeInfo ipack_device_info = {
999c16fa79SAlberto Garcia     .name          = TYPE_IPACK_DEVICE,
1009c16fa79SAlberto Garcia     .parent        = TYPE_DEVICE,
1019c16fa79SAlberto Garcia     .instance_size = sizeof(IPackDevice),
1029c16fa79SAlberto Garcia     .class_size    = sizeof(IPackDeviceClass),
1039c16fa79SAlberto Garcia     .class_init    = ipack_device_class_init,
1049c16fa79SAlberto Garcia     .abstract      = true,
1059c16fa79SAlberto Garcia };
1069c16fa79SAlberto Garcia 
1079c16fa79SAlberto Garcia static const TypeInfo ipack_bus_info = {
1089c16fa79SAlberto Garcia     .name = TYPE_IPACK_BUS,
1099c16fa79SAlberto Garcia     .parent = TYPE_BUS,
1109c16fa79SAlberto Garcia     .instance_size = sizeof(IPackBus),
1119c16fa79SAlberto Garcia };
1129c16fa79SAlberto Garcia 
ipack_register_types(void)1139c16fa79SAlberto Garcia static void ipack_register_types(void)
1149c16fa79SAlberto Garcia {
1159c16fa79SAlberto Garcia     type_register_static(&ipack_device_info);
1169c16fa79SAlberto Garcia     type_register_static(&ipack_bus_info);
1179c16fa79SAlberto Garcia }
1189c16fa79SAlberto Garcia 
1199c16fa79SAlberto Garcia type_init(ipack_register_types)
120