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