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