Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 1 | /* |
| 2 | * QEMU ISA IPMI KCS emulation |
| 3 | * |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 4 | * Copyright (c) 2015,2017 Corey Minyard, MontaVista Software, LLC |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 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 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 26 | #include "qemu/module.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 27 | #include "qapi/error.h" |
Markus Armbruster | 64552b6 | 2019-08-12 07:23:42 +0200 | [diff] [blame] | 28 | #include "hw/irq.h" |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 29 | #include "hw/ipmi/ipmi_kcs.h" |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 30 | #include "hw/isa/isa.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 31 | #include "hw/qdev-properties.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 32 | #include "migration/vmstate.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 33 | #include "qom/object.h" |
Igor Mammedov | 5876d9b | 2022-06-08 09:53:21 -0400 | [diff] [blame] | 34 | #include "hw/acpi/ipmi.h" |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 35 | |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 36 | #define TYPE_ISA_IPMI_KCS "isa-ipmi-kcs" |
Eduardo Habkost | 8063396 | 2020-09-16 14:25:19 -0400 | [diff] [blame] | 37 | OBJECT_DECLARE_SIMPLE_TYPE(ISAIPMIKCSDevice, ISA_IPMI_KCS) |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 38 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 39 | struct ISAIPMIKCSDevice { |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 40 | ISADevice dev; |
Peter Maydell | f401451 | 2016-01-22 15:09:21 +0000 | [diff] [blame] | 41 | int32_t isairq; |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 42 | qemu_irq irq; |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 43 | IPMIKCS kcs; |
Corey Minyard | 15139b8 | 2016-05-24 12:37:17 -0500 | [diff] [blame] | 44 | uint32_t uuid; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 45 | }; |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 46 | |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 47 | static void isa_ipmi_kcs_get_fwinfo(IPMIInterface *ii, IPMIFwInfo *info) |
Corey Minyard | 15139b8 | 2016-05-24 12:37:17 -0500 | [diff] [blame] | 48 | { |
| 49 | ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(ii); |
| 50 | |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 51 | ipmi_kcs_get_fwinfo(&iik->kcs, info); |
Corey Minyard | 15139b8 | 2016-05-24 12:37:17 -0500 | [diff] [blame] | 52 | info->interrupt_number = iik->isairq; |
| 53 | info->uuid = iik->uuid; |
| 54 | } |
| 55 | |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 56 | static void isa_ipmi_kcs_raise_irq(IPMIKCS *ik) |
Corey Minyard | 15139b8 | 2016-05-24 12:37:17 -0500 | [diff] [blame] | 57 | { |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 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); |
Corey Minyard | 15139b8 | 2016-05-24 12:37:17 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Thomas Huth | 6436db5 | 2023-10-20 16:55:54 +0200 | [diff] [blame] | 70 | static bool vmstate_kcs_before_version2(void *opaque, int version) |
| 71 | { |
| 72 | return version <= 1; |
| 73 | } |
| 74 | |
| 75 | static const VMStateDescription vmstate_ISAIPMIKCSDevice = { |
| 76 | .name = TYPE_IPMI_INTERFACE, |
| 77 | .version_id = 2, |
| 78 | .minimum_version_id = 1, |
Richard Henderson | 09c6ac6 | 2023-12-21 14:16:17 +1100 | [diff] [blame] | 79 | .fields = (const VMStateField[]) { |
Thomas Huth | 6436db5 | 2023-10-20 16:55:54 +0200 | [diff] [blame] | 80 | VMSTATE_VSTRUCT_TEST(kcs, ISAIPMIKCSDevice, vmstate_kcs_before_version2, |
| 81 | 0, vmstate_IPMIKCS, IPMIKCS, 1), |
| 82 | VMSTATE_VSTRUCT_V(kcs, ISAIPMIKCSDevice, 2, vmstate_IPMIKCS, |
| 83 | IPMIKCS, 2), |
| 84 | VMSTATE_END_OF_LIST() |
| 85 | } |
| 86 | }; |
| 87 | |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 88 | static void ipmi_isa_realize(DeviceState *dev, Error **errp) |
| 89 | { |
Markus Armbruster | f6166a4 | 2019-12-04 10:36:15 +0100 | [diff] [blame] | 90 | Error *err = NULL; |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 91 | ISADevice *isadev = ISA_DEVICE(dev); |
| 92 | ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(dev); |
| 93 | IPMIInterface *ii = IPMI_INTERFACE(dev); |
| 94 | IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii); |
| 95 | |
| 96 | if (!iik->kcs.bmc) { |
| 97 | error_setg(errp, "IPMI device requires a bmc attribute to be set"); |
| 98 | return; |
| 99 | } |
| 100 | |
Corey Minyard | 15139b8 | 2016-05-24 12:37:17 -0500 | [diff] [blame] | 101 | iik->uuid = ipmi_next_uuid(); |
| 102 | |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 103 | iik->kcs.bmc->intf = ii; |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 104 | iik->kcs.opaque = iik; |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 105 | |
Markus Armbruster | f6166a4 | 2019-12-04 10:36:15 +0100 | [diff] [blame] | 106 | iic->init(ii, 0, &err); |
| 107 | if (err) { |
| 108 | error_propagate(errp, err); |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 109 | return; |
Markus Armbruster | f6166a4 | 2019-12-04 10:36:15 +0100 | [diff] [blame] | 110 | } |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 111 | |
| 112 | if (iik->isairq > 0) { |
Bernhard Beschow | 215caca | 2022-03-01 23:00:37 +0100 | [diff] [blame] | 113 | iik->irq = isa_get_irq(isadev, iik->isairq); |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 114 | iik->kcs.use_irq = 1; |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 115 | iik->kcs.raise_irq = isa_ipmi_kcs_raise_irq; |
| 116 | iik->kcs.lower_irq = isa_ipmi_kcs_lower_irq; |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | qdev_set_legacy_instance_id(dev, iik->kcs.io_base, iik->kcs.io_length); |
| 120 | |
| 121 | isa_register_ioport(isadev, &iik->kcs.io, iik->kcs.io_base); |
Corey Minyard | bd66bcf | 2015-12-17 12:50:11 -0600 | [diff] [blame] | 122 | |
Corey Minyard | 7e57b82 | 2018-04-25 10:27:31 -0500 | [diff] [blame] | 123 | /* |
| 124 | * Version 1 had an incorrect name, it clashed with the BT |
| 125 | * IPMI device, so receive it, but transmit a different |
| 126 | * version. |
| 127 | */ |
Corey Minyard | bd66bcf | 2015-12-17 12:50:11 -0600 | [diff] [blame] | 128 | vmstate_register(NULL, 0, &vmstate_ISAIPMIKCSDevice, iik); |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 129 | } |
| 130 | |
Thomas Huth | 6436db5 | 2023-10-20 16:55:54 +0200 | [diff] [blame] | 131 | static void isa_ipmi_kcs_init(Object *obj) |
| 132 | { |
| 133 | ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(obj); |
| 134 | |
| 135 | ipmi_bmc_find_and_link(obj, (Object **) &iik->kcs.bmc); |
| 136 | } |
| 137 | |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 138 | static void *isa_ipmi_kcs_get_backend_data(IPMIInterface *ii) |
| 139 | { |
| 140 | ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(ii); |
| 141 | |
| 142 | return &iik->kcs; |
| 143 | } |
| 144 | |
| 145 | static 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 | |
| 151 | static void isa_ipmi_kcs_class_init(ObjectClass *oc, void *data) |
| 152 | { |
| 153 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 154 | IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc); |
Igor Mammedov | 5876d9b | 2022-06-08 09:53:21 -0400 | [diff] [blame] | 155 | AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(oc); |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 156 | |
| 157 | dc->realize = ipmi_isa_realize; |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 158 | device_class_set_props(dc, ipmi_isa_properties); |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 159 | |
| 160 | iic->get_backend_data = isa_ipmi_kcs_get_backend_data; |
| 161 | ipmi_kcs_class_init(iic); |
Corey Minyard | 0f310cd | 2017-12-06 07:34:24 -0600 | [diff] [blame] | 162 | iic->get_fwinfo = isa_ipmi_kcs_get_fwinfo; |
Igor Mammedov | 5876d9b | 2022-06-08 09:53:21 -0400 | [diff] [blame] | 163 | adevc->build_dev_aml = build_ipmi_dev_aml; |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 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 }, |
Igor Mammedov | 5876d9b | 2022-06-08 09:53:21 -0400 | [diff] [blame] | 174 | { TYPE_ACPI_DEV_AML_IF }, |
Corey Minyard | 0719029 | 2015-12-17 12:50:07 -0600 | [diff] [blame] | 175 | { } |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | static void ipmi_register_types(void) |
| 180 | { |
| 181 | type_register_static(&isa_ipmi_kcs_info); |
| 182 | } |
| 183 | |
| 184 | type_init(ipmi_register_types) |