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