blob: 7dd6bf0040a42a160fe1ecc35183bb70448a44d7 [file] [log] [blame]
Corey Minyard07190292015-12-17 12:50:07 -06001/*
2 * QEMU ISA IPMI KCS emulation
3 *
Corey Minyard0f310cd2017-12-06 07:34:24 -06004 * Copyright (c) 2015,2017 Corey Minyard, MontaVista Software, LLC
Corey Minyard07190292015-12-17 12:50:07 -06005 *
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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell04308912016-01-26 18:17:30 +000025#include "qemu/osdep.h"
Corey Minyard7e57b822018-04-25 10:27:31 -050026#include "qemu/log.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020027#include "qemu/module.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Markus Armbruster64552b62019-08-12 07:23:42 +020029#include "hw/irq.h"
Corey Minyard0f310cd2017-12-06 07:34:24 -060030#include "hw/ipmi/ipmi_kcs.h"
Corey Minyard07190292015-12-17 12:50:07 -060031#include "hw/isa/isa.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020032#include "hw/qdev-properties.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020033#include "migration/vmstate.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040034#include "qom/object.h"
Corey Minyard07190292015-12-17 12:50:07 -060035
Corey Minyard07190292015-12-17 12:50:07 -060036#define TYPE_ISA_IPMI_KCS "isa-ipmi-kcs"
Eduardo Habkost80633962020-09-16 14:25:19 -040037OBJECT_DECLARE_SIMPLE_TYPE(ISAIPMIKCSDevice, ISA_IPMI_KCS)
Corey Minyard07190292015-12-17 12:50:07 -060038
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040039struct ISAIPMIKCSDevice {
Corey Minyard07190292015-12-17 12:50:07 -060040 ISADevice dev;
Peter Maydellf4014512016-01-22 15:09:21 +000041 int32_t isairq;
Corey Minyard0f310cd2017-12-06 07:34:24 -060042 qemu_irq irq;
Corey Minyard07190292015-12-17 12:50:07 -060043 IPMIKCS kcs;
Corey Minyard15139b82016-05-24 12:37:17 -050044 uint32_t uuid;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040045};
Corey Minyard07190292015-12-17 12:50:07 -060046
Corey Minyard0f310cd2017-12-06 07:34:24 -060047static void isa_ipmi_kcs_get_fwinfo(IPMIInterface *ii, IPMIFwInfo *info)
Corey Minyard15139b82016-05-24 12:37:17 -050048{
49 ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(ii);
50
Corey Minyard0f310cd2017-12-06 07:34:24 -060051 ipmi_kcs_get_fwinfo(&iik->kcs, info);
Corey Minyard15139b82016-05-24 12:37:17 -050052 info->interrupt_number = iik->isairq;
53 info->uuid = iik->uuid;
54}
55
Corey Minyard0f310cd2017-12-06 07:34:24 -060056static void isa_ipmi_kcs_raise_irq(IPMIKCS *ik)
Corey Minyard15139b82016-05-24 12:37:17 -050057{
Corey Minyard0f310cd2017-12-06 07:34:24 -060058 ISAIPMIKCSDevice *iik = ik->opaque;
59
60 qemu_irq_raise(iik->irq);
61}
62
63static void isa_ipmi_kcs_lower_irq(IPMIKCS *ik)
64{
65 ISAIPMIKCSDevice *iik = ik->opaque;
66
67 qemu_irq_lower(iik->irq);
Corey Minyard15139b82016-05-24 12:37:17 -050068}
69
Corey Minyard07190292015-12-17 12:50:07 -060070static void ipmi_isa_realize(DeviceState *dev, Error **errp)
71{
Markus Armbrusterf6166a42019-12-04 10:36:15 +010072 Error *err = NULL;
Corey Minyard07190292015-12-17 12:50:07 -060073 ISADevice *isadev = ISA_DEVICE(dev);
74 ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(dev);
75 IPMIInterface *ii = IPMI_INTERFACE(dev);
76 IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
77
78 if (!iik->kcs.bmc) {
79 error_setg(errp, "IPMI device requires a bmc attribute to be set");
80 return;
81 }
82
Corey Minyard15139b82016-05-24 12:37:17 -050083 iik->uuid = ipmi_next_uuid();
84
Corey Minyard07190292015-12-17 12:50:07 -060085 iik->kcs.bmc->intf = ii;
Corey Minyard0f310cd2017-12-06 07:34:24 -060086 iik->kcs.opaque = iik;
Corey Minyard07190292015-12-17 12:50:07 -060087
Markus Armbrusterf6166a42019-12-04 10:36:15 +010088 iic->init(ii, 0, &err);
89 if (err) {
90 error_propagate(errp, err);
Corey Minyard07190292015-12-17 12:50:07 -060091 return;
Markus Armbrusterf6166a42019-12-04 10:36:15 +010092 }
Corey Minyard07190292015-12-17 12:50:07 -060093
94 if (iik->isairq > 0) {
Corey Minyard0f310cd2017-12-06 07:34:24 -060095 isa_init_irq(isadev, &iik->irq, iik->isairq);
Corey Minyard07190292015-12-17 12:50:07 -060096 iik->kcs.use_irq = 1;
Corey Minyard0f310cd2017-12-06 07:34:24 -060097 iik->kcs.raise_irq = isa_ipmi_kcs_raise_irq;
98 iik->kcs.lower_irq = isa_ipmi_kcs_lower_irq;
Corey Minyard07190292015-12-17 12:50:07 -060099 }
100
101 qdev_set_legacy_instance_id(dev, iik->kcs.io_base, iik->kcs.io_length);
102
103 isa_register_ioport(isadev, &iik->kcs.io, iik->kcs.io_base);
104}
105
Corey Minyard7e57b822018-04-25 10:27:31 -0500106static bool vmstate_kcs_before_version2(void *opaque, int version)
107{
108 return version <= 1;
109}
110
Corey Minyard7e57b822018-04-25 10:27:31 -0500111static const VMStateDescription vmstate_ISAIPMIKCSDevice = {
Corey Minyardbd66bcf2015-12-17 12:50:11 -0600112 .name = TYPE_IPMI_INTERFACE,
Corey Minyard7e57b822018-04-25 10:27:31 -0500113 .version_id = 2,
Corey Minyardbd66bcf2015-12-17 12:50:11 -0600114 .minimum_version_id = 1,
115 .fields = (VMStateField[]) {
Corey Minyard7e57b822018-04-25 10:27:31 -0500116 VMSTATE_VSTRUCT_TEST(kcs, ISAIPMIKCSDevice, vmstate_kcs_before_version2,
117 0, vmstate_IPMIKCS, IPMIKCS, 1),
118 VMSTATE_VSTRUCT_V(kcs, ISAIPMIKCSDevice, 2, vmstate_IPMIKCS,
119 IPMIKCS, 2),
Corey Minyardbd66bcf2015-12-17 12:50:11 -0600120 VMSTATE_END_OF_LIST()
121 }
122};
123
Corey Minyard07190292015-12-17 12:50:07 -0600124static void isa_ipmi_kcs_init(Object *obj)
125{
126 ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(obj);
127
128 ipmi_bmc_find_and_link(obj, (Object **) &iik->kcs.bmc);
Corey Minyardbd66bcf2015-12-17 12:50:11 -0600129
Corey Minyard7e57b822018-04-25 10:27:31 -0500130 /*
131 * Version 1 had an incorrect name, it clashed with the BT
132 * IPMI device, so receive it, but transmit a different
133 * version.
134 */
Corey Minyardbd66bcf2015-12-17 12:50:11 -0600135 vmstate_register(NULL, 0, &vmstate_ISAIPMIKCSDevice, iik);
Corey Minyard07190292015-12-17 12:50:07 -0600136}
137
138static void *isa_ipmi_kcs_get_backend_data(IPMIInterface *ii)
139{
140 ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(ii);
141
142 return &iik->kcs;
143}
144
145static Property ipmi_isa_properties[] = {
146 DEFINE_PROP_UINT32("ioport", ISAIPMIKCSDevice, kcs.io_base, 0xca2),
147 DEFINE_PROP_INT32("irq", ISAIPMIKCSDevice, isairq, 5),
148 DEFINE_PROP_END_OF_LIST(),
149};
150
151static void isa_ipmi_kcs_class_init(ObjectClass *oc, void *data)
152{
153 DeviceClass *dc = DEVICE_CLASS(oc);
154 IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc);
155
156 dc->realize = ipmi_isa_realize;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400157 device_class_set_props(dc, ipmi_isa_properties);
Corey Minyard07190292015-12-17 12:50:07 -0600158
159 iic->get_backend_data = isa_ipmi_kcs_get_backend_data;
160 ipmi_kcs_class_init(iic);
Corey Minyard0f310cd2017-12-06 07:34:24 -0600161 iic->get_fwinfo = isa_ipmi_kcs_get_fwinfo;
Corey Minyard07190292015-12-17 12:50:07 -0600162}
163
164static const TypeInfo isa_ipmi_kcs_info = {
165 .name = TYPE_ISA_IPMI_KCS,
166 .parent = TYPE_ISA_DEVICE,
167 .instance_size = sizeof(ISAIPMIKCSDevice),
168 .instance_init = isa_ipmi_kcs_init,
169 .class_init = isa_ipmi_kcs_class_init,
170 .interfaces = (InterfaceInfo[]) {
171 { TYPE_IPMI_INTERFACE },
172 { }
173 }
174};
175
176static void ipmi_register_types(void)
177{
178 type_register_static(&isa_ipmi_kcs_info);
179}
180
181type_init(ipmi_register_types)