| 1 | /* |
| 2 | * QEMU PCI IPMI KCS emulation |
| 3 | * |
| 4 | * Copyright (c) 2017 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 | #include "qemu/osdep.h" |
| 25 | #include "migration/vmstate.h" |
| 26 | #include "qapi/error.h" |
| 27 | #include "hw/ipmi/ipmi_kcs.h" |
| 28 | #include "hw/pci/pci_device.h" |
| 29 | #include "qom/object.h" |
| 30 | |
| 31 | #define TYPE_PCI_IPMI_KCS "pci-ipmi-kcs" |
| 32 | OBJECT_DECLARE_SIMPLE_TYPE(PCIIPMIKCSDevice, PCI_IPMI_KCS) |
| 33 | |
| 34 | struct PCIIPMIKCSDevice { |
| 35 | PCIDevice dev; |
| 36 | IPMIKCS kcs; |
| 37 | bool irq_enabled; |
| 38 | uint32_t uuid; |
| 39 | }; |
| 40 | |
| 41 | static void pci_ipmi_kcs_get_fwinfo(struct IPMIInterface *ii, IPMIFwInfo *info) |
| 42 | { |
| 43 | PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(ii); |
| 44 | |
| 45 | ipmi_kcs_get_fwinfo(&pik->kcs, info); |
| 46 | info->irq_source = IPMI_PCI_IRQ; |
| 47 | info->interrupt_number = pci_intx(&pik->dev); |
| 48 | info->uuid = pik->uuid; |
| 49 | } |
| 50 | |
| 51 | static void pci_ipmi_raise_irq(IPMIKCS *ik) |
| 52 | { |
| 53 | PCIIPMIKCSDevice *pik = ik->opaque; |
| 54 | |
| 55 | pci_set_irq(&pik->dev, true); |
| 56 | } |
| 57 | |
| 58 | static void pci_ipmi_lower_irq(IPMIKCS *ik) |
| 59 | { |
| 60 | PCIIPMIKCSDevice *pik = ik->opaque; |
| 61 | |
| 62 | pci_set_irq(&pik->dev, false); |
| 63 | } |
| 64 | |
| 65 | static void pci_ipmi_kcs_realize(PCIDevice *pd, Error **errp) |
| 66 | { |
| 67 | Error *err = NULL; |
| 68 | PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(pd); |
| 69 | IPMIInterface *ii = IPMI_INTERFACE(pd); |
| 70 | IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii); |
| 71 | |
| 72 | if (!pik->kcs.bmc) { |
| 73 | error_setg(errp, "IPMI device requires a bmc attribute to be set"); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | pik->uuid = ipmi_next_uuid(); |
| 78 | |
| 79 | pik->kcs.bmc->intf = ii; |
| 80 | pik->kcs.opaque = pik; |
| 81 | |
| 82 | pci_config_set_prog_interface(pd->config, 0x01); /* KCS */ |
| 83 | pci_config_set_interrupt_pin(pd->config, 0x01); |
| 84 | pik->kcs.use_irq = 1; |
| 85 | pik->kcs.raise_irq = pci_ipmi_raise_irq; |
| 86 | pik->kcs.lower_irq = pci_ipmi_lower_irq; |
| 87 | |
| 88 | iic->init(ii, 8, &err); |
| 89 | if (err) { |
| 90 | error_propagate(errp, err); |
| 91 | return; |
| 92 | } |
| 93 | pci_register_bar(pd, 0, PCI_BASE_ADDRESS_SPACE_IO, &pik->kcs.io); |
| 94 | } |
| 95 | |
| 96 | const VMStateDescription vmstate_PCIIPMIKCSDevice = { |
| 97 | .name = TYPE_IPMI_INTERFACE_PREFIX "pci-kcs", |
| 98 | .version_id = 1, |
| 99 | .minimum_version_id = 1, |
| 100 | .fields = (const VMStateField[]) { |
| 101 | VMSTATE_PCI_DEVICE(dev, PCIIPMIKCSDevice), |
| 102 | VMSTATE_STRUCT(kcs, PCIIPMIKCSDevice, 1, vmstate_IPMIKCS, IPMIKCS), |
| 103 | VMSTATE_END_OF_LIST() |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | static void pci_ipmi_kcs_instance_init(Object *obj) |
| 108 | { |
| 109 | PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(obj); |
| 110 | |
| 111 | ipmi_bmc_find_and_link(obj, (Object **) &pik->kcs.bmc); |
| 112 | } |
| 113 | |
| 114 | static void *pci_ipmi_kcs_get_backend_data(IPMIInterface *ii) |
| 115 | { |
| 116 | PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(ii); |
| 117 | |
| 118 | return &pik->kcs; |
| 119 | } |
| 120 | |
| 121 | static void pci_ipmi_kcs_class_init(ObjectClass *oc, const void *data) |
| 122 | { |
| 123 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 124 | PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc); |
| 125 | IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc); |
| 126 | |
| 127 | pdc->vendor_id = PCI_VENDOR_ID_QEMU; |
| 128 | pdc->device_id = PCI_DEVICE_ID_QEMU_IPMI; |
| 129 | pdc->revision = 1; |
| 130 | pdc->class_id = PCI_CLASS_SERIAL_IPMI; |
| 131 | |
| 132 | dc->vmsd = &vmstate_PCIIPMIKCSDevice; |
| 133 | dc->desc = "PCI IPMI KCS"; |
| 134 | pdc->realize = pci_ipmi_kcs_realize; |
| 135 | |
| 136 | iic->get_backend_data = pci_ipmi_kcs_get_backend_data; |
| 137 | ipmi_kcs_class_init(iic); |
| 138 | iic->get_fwinfo = pci_ipmi_kcs_get_fwinfo; |
| 139 | } |
| 140 | |
| 141 | static const TypeInfo pci_ipmi_kcs_info = { |
| 142 | .name = TYPE_PCI_IPMI_KCS, |
| 143 | .parent = TYPE_PCI_DEVICE, |
| 144 | .instance_size = sizeof(PCIIPMIKCSDevice), |
| 145 | .instance_init = pci_ipmi_kcs_instance_init, |
| 146 | .class_init = pci_ipmi_kcs_class_init, |
| 147 | .interfaces = (const InterfaceInfo[]) { |
| 148 | { TYPE_IPMI_INTERFACE }, |
| 149 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, |
| 150 | { } |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | static void pci_ipmi_kcs_register_types(void) |
| 155 | { |
| 156 | type_register_static(&pci_ipmi_kcs_info); |
| 157 | } |
| 158 | |
| 159 | type_init(pci_ipmi_kcs_register_types) |