1 /* 2 * QEMU ISA IPMI BT emulation 3 * 4 * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 #include "qemu/log.h" 27 #include "qemu/module.h" 28 #include "qapi/error.h" 29 #include "hw/irq.h" 30 #include "hw/ipmi/ipmi_bt.h" 31 #include "hw/isa/isa.h" 32 #include "hw/qdev-properties.h" 33 #include "migration/vmstate.h" 34 #include "qom/object.h" 35 36 #define TYPE_ISA_IPMI_BT "isa-ipmi-bt" 37 typedef struct ISAIPMIBTDevice ISAIPMIBTDevice; 38 DECLARE_INSTANCE_CHECKER(ISAIPMIBTDevice, ISA_IPMI_BT, 39 TYPE_ISA_IPMI_BT) 40 41 struct ISAIPMIBTDevice { 42 ISADevice dev; 43 int32_t isairq; 44 qemu_irq irq; 45 IPMIBT bt; 46 uint32_t uuid; 47 }; 48 49 static void isa_ipmi_bt_get_fwinfo(struct IPMIInterface *ii, IPMIFwInfo *info) 50 { 51 ISAIPMIBTDevice *iib = ISA_IPMI_BT(ii); 52 53 ipmi_bt_get_fwinfo(&iib->bt, info); 54 info->interrupt_number = iib->isairq; 55 info->i2c_slave_address = iib->bt.bmc->slave_addr; 56 info->uuid = iib->uuid; 57 } 58 59 static void isa_ipmi_bt_raise_irq(IPMIBT *ib) 60 { 61 ISAIPMIBTDevice *iib = ib->opaque; 62 63 qemu_irq_raise(iib->irq); 64 } 65 66 static void isa_ipmi_bt_lower_irq(IPMIBT *ib) 67 { 68 ISAIPMIBTDevice *iib = ib->opaque; 69 70 qemu_irq_lower(iib->irq); 71 } 72 73 static void isa_ipmi_bt_realize(DeviceState *dev, Error **errp) 74 { 75 Error *err = NULL; 76 ISADevice *isadev = ISA_DEVICE(dev); 77 ISAIPMIBTDevice *iib = ISA_IPMI_BT(dev); 78 IPMIInterface *ii = IPMI_INTERFACE(dev); 79 IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii); 80 81 if (!iib->bt.bmc) { 82 error_setg(errp, "IPMI device requires a bmc attribute to be set"); 83 return; 84 } 85 86 iib->uuid = ipmi_next_uuid(); 87 88 iib->bt.bmc->intf = ii; 89 iib->bt.opaque = iib; 90 91 iic->init(ii, 0, &err); 92 if (err) { 93 error_propagate(errp, err); 94 return; 95 } 96 97 if (iib->isairq > 0) { 98 isa_init_irq(isadev, &iib->irq, iib->isairq); 99 iib->bt.use_irq = 1; 100 iib->bt.raise_irq = isa_ipmi_bt_raise_irq; 101 iib->bt.lower_irq = isa_ipmi_bt_lower_irq; 102 } 103 104 qdev_set_legacy_instance_id(dev, iib->bt.io_base, iib->bt.io_length); 105 106 isa_register_ioport(isadev, &iib->bt.io, iib->bt.io_base); 107 } 108 109 static const VMStateDescription vmstate_ISAIPMIBTDevice = { 110 .name = TYPE_IPMI_INTERFACE_PREFIX "isa-bt", 111 .version_id = 2, 112 .minimum_version_id = 2, 113 /* 114 * Version 1 had messed up the array transfer, it's not even usable 115 * because it used VMSTATE_VBUFFER_UINT32, but it did not transfer 116 * the buffer length, so random things would happen. 117 */ 118 .fields = (VMStateField[]) { 119 VMSTATE_STRUCT(bt, ISAIPMIBTDevice, 1, vmstate_IPMIBT, IPMIBT), 120 VMSTATE_END_OF_LIST() 121 } 122 }; 123 124 static void isa_ipmi_bt_init(Object *obj) 125 { 126 ISAIPMIBTDevice *iib = ISA_IPMI_BT(obj); 127 128 ipmi_bmc_find_and_link(obj, (Object **) &iib->bt.bmc); 129 130 vmstate_register(NULL, 0, &vmstate_ISAIPMIBTDevice, iib); 131 } 132 133 static void *isa_ipmi_bt_get_backend_data(IPMIInterface *ii) 134 { 135 ISAIPMIBTDevice *iib = ISA_IPMI_BT(ii); 136 137 return &iib->bt; 138 } 139 140 static Property ipmi_isa_properties[] = { 141 DEFINE_PROP_UINT32("ioport", ISAIPMIBTDevice, bt.io_base, 0xe4), 142 DEFINE_PROP_INT32("irq", ISAIPMIBTDevice, isairq, 5), 143 DEFINE_PROP_END_OF_LIST(), 144 }; 145 146 static void isa_ipmi_bt_class_init(ObjectClass *oc, void *data) 147 { 148 DeviceClass *dc = DEVICE_CLASS(oc); 149 IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc); 150 151 dc->realize = isa_ipmi_bt_realize; 152 device_class_set_props(dc, ipmi_isa_properties); 153 154 iic->get_backend_data = isa_ipmi_bt_get_backend_data; 155 ipmi_bt_class_init(iic); 156 iic->get_fwinfo = isa_ipmi_bt_get_fwinfo; 157 } 158 159 static const TypeInfo isa_ipmi_bt_info = { 160 .name = TYPE_ISA_IPMI_BT, 161 .parent = TYPE_ISA_DEVICE, 162 .instance_size = sizeof(ISAIPMIBTDevice), 163 .instance_init = isa_ipmi_bt_init, 164 .class_init = isa_ipmi_bt_class_init, 165 .interfaces = (InterfaceInfo[]) { 166 { TYPE_IPMI_INTERFACE }, 167 { } 168 } 169 }; 170 171 static void ipmi_register_types(void) 172 { 173 type_register_static(&isa_ipmi_bt_info); 174 } 175 176 type_init(ipmi_register_types) 177