aboutsummaryrefslogtreecommitdiff
path: root/hw/ipmi/pci_ipmi_kcs.c
blob: 99f46152f48cd9aaee6fc46b90b5680da75dea0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * QEMU PCI IPMI KCS emulation
 *
 * Copyright (c) 2017 Corey Minyard, MontaVista Software, LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "qemu/osdep.h"
#include "migration/vmstate.h"
#include "qapi/error.h"
#include "hw/ipmi/ipmi_kcs.h"
#include "hw/pci/pci.h"

#define TYPE_PCI_IPMI_KCS "pci-ipmi-kcs"
#define PCI_IPMI_KCS(obj) OBJECT_CHECK(PCIIPMIKCSDevice, (obj), \
                                       TYPE_PCI_IPMI_KCS)

typedef struct PCIIPMIKCSDevice {
    PCIDevice dev;
    IPMIKCS kcs;
    bool irq_enabled;
    uint32_t uuid;
} PCIIPMIKCSDevice;

static void pci_ipmi_raise_irq(IPMIKCS *ik)
{
    PCIIPMIKCSDevice *pik = ik->opaque;

    pci_set_irq(&pik->dev, true);
}

static void pci_ipmi_lower_irq(IPMIKCS *ik)
{
    PCIIPMIKCSDevice *pik = ik->opaque;

    pci_set_irq(&pik->dev, false);
}

static void pci_ipmi_kcs_realize(PCIDevice *pd, Error **errp)
{
    Error *err = NULL;
    PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(pd);
    IPMIInterface *ii = IPMI_INTERFACE(pd);
    IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);

    if (!pik->kcs.bmc) {
        error_setg(errp, "IPMI device requires a bmc attribute to be set");
        return;
    }

    pik->uuid = ipmi_next_uuid();

    pik->kcs.bmc->intf = ii;
    pik->kcs.opaque = pik;

    pci_config_set_prog_interface(pd->config, 0x01); /* KCS */
    pci_config_set_interrupt_pin(pd->config, 0x01);
    pik->kcs.use_irq = 1;
    pik->kcs.raise_irq = pci_ipmi_raise_irq;
    pik->kcs.lower_irq = pci_ipmi_lower_irq;

    iic->init(ii, 8, &err);
    if (err) {
        error_propagate(errp, err);
        return;
    }
    pci_register_bar(pd, 0, PCI_BASE_ADDRESS_SPACE_IO, &pik->kcs.io);
}

const VMStateDescription vmstate_PCIIPMIKCSDevice = {
    .name = TYPE_IPMI_INTERFACE_PREFIX "pci-kcs",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_PCI_DEVICE(dev, PCIIPMIKCSDevice),
        VMSTATE_STRUCT(kcs, PCIIPMIKCSDevice, 1, vmstate_IPMIKCS, IPMIKCS),
        VMSTATE_END_OF_LIST()
    }
};

static void pci_ipmi_kcs_instance_init(Object *obj)
{
    PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(obj);

    ipmi_bmc_find_and_link(obj, (Object **) &pik->kcs.bmc);
}

static void *pci_ipmi_kcs_get_backend_data(IPMIInterface *ii)
{
    PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(ii);

    return &pik->kcs;
}

static void pci_ipmi_kcs_class_init(ObjectClass *oc, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(oc);
    PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
    IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc);

    pdc->vendor_id = PCI_VENDOR_ID_QEMU;
    pdc->device_id = PCI_DEVICE_ID_QEMU_IPMI;
    pdc->revision = 1;
    pdc->class_id = PCI_CLASS_SERIAL_IPMI;

    dc->vmsd = &vmstate_PCIIPMIKCSDevice;
    dc->desc = "PCI IPMI KCS";
    pdc->realize = pci_ipmi_kcs_realize;

    iic->get_backend_data = pci_ipmi_kcs_get_backend_data;
    ipmi_kcs_class_init(iic);
}

static const TypeInfo pci_ipmi_kcs_info = {
    .name          = TYPE_PCI_IPMI_KCS,
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIIPMIKCSDevice),
    .instance_init = pci_ipmi_kcs_instance_init,
    .class_init    = pci_ipmi_kcs_class_init,
    .interfaces = (InterfaceInfo[]) {
        { TYPE_IPMI_INTERFACE },
        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
        { }
    }
};

static void pci_ipmi_kcs_register_types(void)
{
    type_register_static(&pci_ipmi_kcs_info);
}

type_init(pci_ipmi_kcs_register_types)