xref: /openbmc/qemu/hw/ipmi/isa_ipmi_kcs.c (revision 8fa3b702)
1 /*
2  * QEMU ISA IPMI KCS emulation
3  *
4  * Copyright (c) 2015,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 
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_kcs.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_KCS "isa-ipmi-kcs"
37 typedef struct ISAIPMIKCSDevice ISAIPMIKCSDevice;
38 DECLARE_INSTANCE_CHECKER(ISAIPMIKCSDevice, ISA_IPMI_KCS,
39                          TYPE_ISA_IPMI_KCS)
40 
41 struct ISAIPMIKCSDevice {
42     ISADevice dev;
43     int32_t isairq;
44     qemu_irq irq;
45     IPMIKCS kcs;
46     uint32_t uuid;
47 };
48 
49 static void isa_ipmi_kcs_get_fwinfo(IPMIInterface *ii, IPMIFwInfo *info)
50 {
51     ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(ii);
52 
53     ipmi_kcs_get_fwinfo(&iik->kcs, info);
54     info->interrupt_number = iik->isairq;
55     info->uuid = iik->uuid;
56 }
57 
58 static void isa_ipmi_kcs_raise_irq(IPMIKCS *ik)
59 {
60     ISAIPMIKCSDevice *iik = ik->opaque;
61 
62     qemu_irq_raise(iik->irq);
63 }
64 
65 static void isa_ipmi_kcs_lower_irq(IPMIKCS *ik)
66 {
67     ISAIPMIKCSDevice *iik = ik->opaque;
68 
69     qemu_irq_lower(iik->irq);
70 }
71 
72 static void ipmi_isa_realize(DeviceState *dev, Error **errp)
73 {
74     Error *err = NULL;
75     ISADevice *isadev = ISA_DEVICE(dev);
76     ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(dev);
77     IPMIInterface *ii = IPMI_INTERFACE(dev);
78     IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
79 
80     if (!iik->kcs.bmc) {
81         error_setg(errp, "IPMI device requires a bmc attribute to be set");
82         return;
83     }
84 
85     iik->uuid = ipmi_next_uuid();
86 
87     iik->kcs.bmc->intf = ii;
88     iik->kcs.opaque = iik;
89 
90     iic->init(ii, 0, &err);
91     if (err) {
92         error_propagate(errp, err);
93         return;
94     }
95 
96     if (iik->isairq > 0) {
97         isa_init_irq(isadev, &iik->irq, iik->isairq);
98         iik->kcs.use_irq = 1;
99         iik->kcs.raise_irq = isa_ipmi_kcs_raise_irq;
100         iik->kcs.lower_irq = isa_ipmi_kcs_lower_irq;
101     }
102 
103     qdev_set_legacy_instance_id(dev, iik->kcs.io_base, iik->kcs.io_length);
104 
105     isa_register_ioport(isadev, &iik->kcs.io, iik->kcs.io_base);
106 }
107 
108 static bool vmstate_kcs_before_version2(void *opaque, int version)
109 {
110     return version <= 1;
111 }
112 
113 static const VMStateDescription vmstate_ISAIPMIKCSDevice = {
114     .name = TYPE_IPMI_INTERFACE,
115     .version_id = 2,
116     .minimum_version_id = 1,
117     .fields      = (VMStateField[]) {
118         VMSTATE_VSTRUCT_TEST(kcs, ISAIPMIKCSDevice, vmstate_kcs_before_version2,
119                              0, vmstate_IPMIKCS, IPMIKCS, 1),
120         VMSTATE_VSTRUCT_V(kcs, ISAIPMIKCSDevice, 2, vmstate_IPMIKCS,
121                           IPMIKCS, 2),
122         VMSTATE_END_OF_LIST()
123     }
124 };
125 
126 static void isa_ipmi_kcs_init(Object *obj)
127 {
128     ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(obj);
129 
130     ipmi_bmc_find_and_link(obj, (Object **) &iik->kcs.bmc);
131 
132     /*
133      * Version 1 had an incorrect name, it clashed with the BT
134      * IPMI device, so receive it, but transmit a different
135      * version.
136      */
137     vmstate_register(NULL, 0, &vmstate_ISAIPMIKCSDevice, iik);
138 }
139 
140 static void *isa_ipmi_kcs_get_backend_data(IPMIInterface *ii)
141 {
142     ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(ii);
143 
144     return &iik->kcs;
145 }
146 
147 static Property ipmi_isa_properties[] = {
148     DEFINE_PROP_UINT32("ioport", ISAIPMIKCSDevice, kcs.io_base,  0xca2),
149     DEFINE_PROP_INT32("irq",   ISAIPMIKCSDevice, isairq,  5),
150     DEFINE_PROP_END_OF_LIST(),
151 };
152 
153 static void isa_ipmi_kcs_class_init(ObjectClass *oc, void *data)
154 {
155     DeviceClass *dc = DEVICE_CLASS(oc);
156     IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc);
157 
158     dc->realize = ipmi_isa_realize;
159     device_class_set_props(dc, ipmi_isa_properties);
160 
161     iic->get_backend_data = isa_ipmi_kcs_get_backend_data;
162     ipmi_kcs_class_init(iic);
163     iic->get_fwinfo = isa_ipmi_kcs_get_fwinfo;
164 }
165 
166 static const TypeInfo isa_ipmi_kcs_info = {
167     .name          = TYPE_ISA_IPMI_KCS,
168     .parent        = TYPE_ISA_DEVICE,
169     .instance_size = sizeof(ISAIPMIKCSDevice),
170     .instance_init = isa_ipmi_kcs_init,
171     .class_init    = isa_ipmi_kcs_class_init,
172     .interfaces = (InterfaceInfo[]) {
173         { TYPE_IPMI_INTERFACE },
174         { }
175     }
176 };
177 
178 static void ipmi_register_types(void)
179 {
180     type_register_static(&isa_ipmi_kcs_info);
181 }
182 
183 type_init(ipmi_register_types)
184