2 * QEMU ISA IPMI KCS emulation
4 * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "hw/ipmi/ipmi.h"
26 #include "hw/isa/isa.h"
27 #include "hw/i386/pc.h"
29 #define IPMI_KCS_OBF_BIT 0
30 #define IPMI_KCS_IBF_BIT 1
31 #define IPMI_KCS_SMS_ATN_BIT 2
32 #define IPMI_KCS_CD_BIT 3
34 #define IPMI_KCS_OBF_MASK (1 << IPMI_KCS_OBF_BIT)
35 #define IPMI_KCS_GET_OBF(d) (((d) >> IPMI_KCS_OBF_BIT) & 0x1)
36 #define IPMI_KCS_SET_OBF(d, v) (d) = (((d) & ~IPMI_KCS_OBF_MASK) | \
37 (((v) & 1) << IPMI_KCS_OBF_BIT))
38 #define IPMI_KCS_IBF_MASK (1 << IPMI_KCS_IBF_BIT)
39 #define IPMI_KCS_GET_IBF(d) (((d) >> IPMI_KCS_IBF_BIT) & 0x1)
40 #define IPMI_KCS_SET_IBF(d, v) (d) = (((d) & ~IPMI_KCS_IBF_MASK) | \
41 (((v) & 1) << IPMI_KCS_IBF_BIT))
42 #define IPMI_KCS_SMS_ATN_MASK (1 << IPMI_KCS_SMS_ATN_BIT)
43 #define IPMI_KCS_GET_SMS_ATN(d) (((d) >> IPMI_KCS_SMS_ATN_BIT) & 0x1)
44 #define IPMI_KCS_SET_SMS_ATN(d, v) (d) = (((d) & ~IPMI_KCS_SMS_ATN_MASK) | \
45 (((v) & 1) << IPMI_KCS_SMS_ATN_BIT))
46 #define IPMI_KCS_CD_MASK (1 << IPMI_KCS_CD_BIT)
47 #define IPMI_KCS_GET_CD(d) (((d) >> IPMI_KCS_CD_BIT) & 0x1)
48 #define IPMI_KCS_SET_CD(d, v) (d) = (((d) & ~IPMI_KCS_CD_MASK) | \
49 (((v) & 1) << IPMI_KCS_CD_BIT))
51 #define IPMI_KCS_IDLE_STATE 0
52 #define IPMI_KCS_READ_STATE 1
53 #define IPMI_KCS_WRITE_STATE 2
54 #define IPMI_KCS_ERROR_STATE 3
56 #define IPMI_KCS_GET_STATE(d) (((d) >> 6) & 0x3)
57 #define IPMI_KCS_SET_STATE(d, v) ((d) = ((d) & ~0xc0) | (((v) & 0x3) << 6))
59 #define IPMI_KCS_ABORT_STATUS_CMD 0x60
60 #define IPMI_KCS_WRITE_START_CMD 0x61
61 #define IPMI_KCS_WRITE_END_CMD 0x62
62 #define IPMI_KCS_READ_CMD 0x68
64 #define IPMI_KCS_STATUS_NO_ERR 0x00
65 #define IPMI_KCS_STATUS_ABORTED_ERR 0x01
66 #define IPMI_KCS_STATUS_BAD_CC_ERR 0x02
67 #define IPMI_KCS_STATUS_LENGTH_ERR 0x06
69 typedef struct IPMIKCS
{
77 unsigned long io_length
;
85 uint8_t outmsg
[MAX_IPMI_MSG_SIZE
];
89 uint8_t inmsg
[MAX_IPMI_MSG_SIZE
];
96 int16_t data_in_reg
; /* -1 means not written */
100 * This is a response number that we send with the command to make
101 * sure that the response matches the command.
108 IPMI_KCS_SET_OBF(ik->status_reg, 1); \
109 if (ik->use_irq && ik->irqs_enabled && !ik->obf_irq_set) { \
110 ik->obf_irq_set = 1; \
111 if (!ik->atn_irq_set) { \
112 qemu_irq_raise(ik->irq); \
117 static void ipmi_kcs_signal(IPMIKCS
*ik
, IPMIInterface
*ii
)
119 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
122 while (ik
->do_wake
) {
124 iic
->handle_if_event(ii
);
128 static void ipmi_kcs_handle_event(IPMIInterface
*ii
)
130 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
131 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
133 if (ik
->cmd_reg
== IPMI_KCS_ABORT_STATUS_CMD
) {
134 if (IPMI_KCS_GET_STATE(ik
->status_reg
) != IPMI_KCS_ERROR_STATE
) {
135 ik
->waiting_rsp
++; /* Invalidate the message */
136 ik
->outmsg
[0] = IPMI_KCS_STATUS_ABORTED_ERR
;
139 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_ERROR_STATE
);
145 switch (IPMI_KCS_GET_STATE(ik
->status_reg
)) {
146 case IPMI_KCS_IDLE_STATE
:
147 if (ik
->cmd_reg
== IPMI_KCS_WRITE_START_CMD
) {
148 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_WRITE_STATE
);
156 case IPMI_KCS_READ_STATE
:
158 if (ik
->outpos
>= ik
->outlen
) {
159 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_IDLE_STATE
);
161 } else if (ik
->data_in_reg
== IPMI_KCS_READ_CMD
) {
162 ik
->data_out_reg
= ik
->outmsg
[ik
->outpos
];
166 ik
->outmsg
[0] = IPMI_KCS_STATUS_BAD_CC_ERR
;
169 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_ERROR_STATE
);
175 case IPMI_KCS_WRITE_STATE
:
176 if (ik
->data_in_reg
!= -1) {
178 * Don't worry about input overrun here, that will be
179 * handled in the BMC.
181 if (ik
->inlen
< sizeof(ik
->inmsg
)) {
182 ik
->inmsg
[ik
->inlen
] = ik
->data_in_reg
;
187 IPMIBmcClass
*bk
= IPMI_BMC_GET_CLASS(ik
->bmc
);
191 bk
->handle_command(ik
->bmc
, ik
->inmsg
, ik
->inlen
, sizeof(ik
->inmsg
),
194 } else if (ik
->cmd_reg
== IPMI_KCS_WRITE_END_CMD
) {
201 case IPMI_KCS_ERROR_STATE
:
202 if (ik
->data_in_reg
!= -1) {
203 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_READ_STATE
);
204 ik
->data_in_reg
= IPMI_KCS_READ_CMD
;
210 if (ik
->cmd_reg
!= -1) {
211 /* Got an invalid command */
212 ik
->outmsg
[0] = IPMI_KCS_STATUS_BAD_CC_ERR
;
215 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_ERROR_STATE
);
220 ik
->data_in_reg
= -1;
221 IPMI_KCS_SET_IBF(ik
->status_reg
, 0);
226 static void ipmi_kcs_handle_rsp(IPMIInterface
*ii
, uint8_t msg_id
,
227 unsigned char *rsp
, unsigned int rsp_len
)
229 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
230 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
232 if (ik
->waiting_rsp
== msg_id
) {
234 if (rsp_len
> sizeof(ik
->outmsg
)) {
235 ik
->outmsg
[0] = rsp
[0];
236 ik
->outmsg
[1] = rsp
[1];
237 ik
->outmsg
[2] = IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES
;
240 memcpy(ik
->outmsg
, rsp
, rsp_len
);
241 ik
->outlen
= rsp_len
;
243 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_READ_STATE
);
244 ik
->data_in_reg
= IPMI_KCS_READ_CMD
;
245 ipmi_kcs_signal(ik
, ii
);
250 static uint64_t ipmi_kcs_ioport_read(void *opaque
, hwaddr addr
, unsigned size
)
252 IPMIInterface
*ii
= opaque
;
253 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
254 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
259 ret
= ik
->data_out_reg
;
260 IPMI_KCS_SET_OBF(ik
->status_reg
, 0);
261 if (ik
->obf_irq_set
) {
263 if (!ik
->atn_irq_set
) {
264 qemu_irq_lower(ik
->irq
);
269 ret
= ik
->status_reg
;
270 if (ik
->atn_irq_set
) {
272 if (!ik
->obf_irq_set
) {
273 qemu_irq_lower(ik
->irq
);
281 static void ipmi_kcs_ioport_write(void *opaque
, hwaddr addr
, uint64_t val
,
284 IPMIInterface
*ii
= opaque
;
285 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
286 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
288 if (IPMI_KCS_GET_IBF(ik
->status_reg
)) {
294 ik
->data_in_reg
= val
;
301 IPMI_KCS_SET_IBF(ik
->status_reg
, 1);
302 ipmi_kcs_signal(ik
, ii
);
305 const MemoryRegionOps ipmi_kcs_io_ops
= {
306 .read
= ipmi_kcs_ioport_read
,
307 .write
= ipmi_kcs_ioport_write
,
309 .min_access_size
= 1,
310 .max_access_size
= 1,
312 .endianness
= DEVICE_LITTLE_ENDIAN
,
315 static void ipmi_kcs_set_atn(IPMIInterface
*ii
, int val
, int irq
)
317 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
318 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
320 IPMI_KCS_SET_SMS_ATN(ik
->status_reg
, val
);
322 if (irq
&& !ik
->atn_irq_set
&& ik
->use_irq
&& ik
->irqs_enabled
) {
324 if (!ik
->obf_irq_set
) {
325 qemu_irq_raise(ik
->irq
);
329 if (ik
->atn_irq_set
) {
331 if (!ik
->obf_irq_set
) {
332 qemu_irq_lower(ik
->irq
);
338 static void ipmi_kcs_set_irq_enable(IPMIInterface
*ii
, int val
)
340 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
341 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
343 ik
->irqs_enabled
= val
;
346 static void ipmi_kcs_init(IPMIInterface
*ii
, Error
**errp
)
348 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
349 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
352 memory_region_init_io(&ik
->io
, NULL
, &ipmi_kcs_io_ops
, ii
, "ipmi-kcs", 2);
355 static void ipmi_kcs_class_init(IPMIInterfaceClass
*iic
)
357 iic
->init
= ipmi_kcs_init
;
358 iic
->set_atn
= ipmi_kcs_set_atn
;
359 iic
->handle_rsp
= ipmi_kcs_handle_rsp
;
360 iic
->handle_if_event
= ipmi_kcs_handle_event
;
361 iic
->set_irq_enable
= ipmi_kcs_set_irq_enable
;
365 #define TYPE_ISA_IPMI_KCS "isa-ipmi-kcs"
366 #define ISA_IPMI_KCS(obj) OBJECT_CHECK(ISAIPMIKCSDevice, (obj), \
369 typedef struct ISAIPMIKCSDevice
{
376 static void ipmi_isa_realize(DeviceState
*dev
, Error
**errp
)
378 ISADevice
*isadev
= ISA_DEVICE(dev
);
379 ISAIPMIKCSDevice
*iik
= ISA_IPMI_KCS(dev
);
380 IPMIInterface
*ii
= IPMI_INTERFACE(dev
);
381 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
384 error_setg(errp
, "IPMI device requires a bmc attribute to be set");
388 iik
->kcs
.bmc
->intf
= ii
;
394 if (iik
->isairq
> 0) {
395 isa_init_irq(isadev
, &iik
->kcs
.irq
, iik
->isairq
);
396 iik
->kcs
.use_irq
= 1;
399 qdev_set_legacy_instance_id(dev
, iik
->kcs
.io_base
, iik
->kcs
.io_length
);
401 isa_register_ioport(isadev
, &iik
->kcs
.io
, iik
->kcs
.io_base
);
403 iik
->fwinfo
.interface_name
= "kcs";
404 iik
->fwinfo
.interface_type
= IPMI_SMBIOS_KCS
;
405 iik
->fwinfo
.ipmi_spec_major_revision
= 2;
406 iik
->fwinfo
.ipmi_spec_minor_revision
= 0;
407 iik
->fwinfo
.base_address
= iik
->kcs
.io_base
;
408 iik
->fwinfo
.i2c_slave_address
= iik
->kcs
.bmc
->slave_addr
;
409 iik
->fwinfo
.register_length
= iik
->kcs
.io_length
;
410 iik
->fwinfo
.register_spacing
= 1;
411 iik
->fwinfo
.memspace
= IPMI_MEMSPACE_IO
;
412 iik
->fwinfo
.irq_type
= IPMI_LEVEL_IRQ
;
413 iik
->fwinfo
.interrupt_number
= iik
->isairq
;
414 iik
->fwinfo
.acpi_parent
= "\\_SB.PCI0.ISA";
415 ipmi_add_fwinfo(&iik
->fwinfo
, errp
);
418 const VMStateDescription vmstate_ISAIPMIKCSDevice
= {
419 .name
= TYPE_IPMI_INTERFACE
,
421 .minimum_version_id
= 1,
422 .fields
= (VMStateField
[]) {
423 VMSTATE_BOOL(kcs
.obf_irq_set
, ISAIPMIKCSDevice
),
424 VMSTATE_BOOL(kcs
.atn_irq_set
, ISAIPMIKCSDevice
),
425 VMSTATE_BOOL(kcs
.use_irq
, ISAIPMIKCSDevice
),
426 VMSTATE_BOOL(kcs
.irqs_enabled
, ISAIPMIKCSDevice
),
427 VMSTATE_UINT32(kcs
.outpos
, ISAIPMIKCSDevice
),
428 VMSTATE_VBUFFER_UINT32(kcs
.outmsg
, ISAIPMIKCSDevice
, 1, NULL
, 0,
430 VMSTATE_VBUFFER_UINT32(kcs
.inmsg
, ISAIPMIKCSDevice
, 1, NULL
, 0,
432 VMSTATE_BOOL(kcs
.write_end
, ISAIPMIKCSDevice
),
433 VMSTATE_UINT8(kcs
.status_reg
, ISAIPMIKCSDevice
),
434 VMSTATE_UINT8(kcs
.data_out_reg
, ISAIPMIKCSDevice
),
435 VMSTATE_INT16(kcs
.data_in_reg
, ISAIPMIKCSDevice
),
436 VMSTATE_INT16(kcs
.cmd_reg
, ISAIPMIKCSDevice
),
437 VMSTATE_UINT8(kcs
.waiting_rsp
, ISAIPMIKCSDevice
),
438 VMSTATE_END_OF_LIST()
442 static void isa_ipmi_kcs_init(Object
*obj
)
444 ISAIPMIKCSDevice
*iik
= ISA_IPMI_KCS(obj
);
446 ipmi_bmc_find_and_link(obj
, (Object
**) &iik
->kcs
.bmc
);
448 vmstate_register(NULL
, 0, &vmstate_ISAIPMIKCSDevice
, iik
);
451 static void *isa_ipmi_kcs_get_backend_data(IPMIInterface
*ii
)
453 ISAIPMIKCSDevice
*iik
= ISA_IPMI_KCS(ii
);
458 static Property ipmi_isa_properties
[] = {
459 DEFINE_PROP_UINT32("ioport", ISAIPMIKCSDevice
, kcs
.io_base
, 0xca2),
460 DEFINE_PROP_INT32("irq", ISAIPMIKCSDevice
, isairq
, 5),
461 DEFINE_PROP_END_OF_LIST(),
464 static void isa_ipmi_kcs_class_init(ObjectClass
*oc
, void *data
)
466 DeviceClass
*dc
= DEVICE_CLASS(oc
);
467 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_CLASS(oc
);
469 dc
->realize
= ipmi_isa_realize
;
470 dc
->props
= ipmi_isa_properties
;
472 iic
->get_backend_data
= isa_ipmi_kcs_get_backend_data
;
473 ipmi_kcs_class_init(iic
);
476 static const TypeInfo isa_ipmi_kcs_info
= {
477 .name
= TYPE_ISA_IPMI_KCS
,
478 .parent
= TYPE_ISA_DEVICE
,
479 .instance_size
= sizeof(ISAIPMIKCSDevice
),
480 .instance_init
= isa_ipmi_kcs_init
,
481 .class_init
= isa_ipmi_kcs_class_init
,
482 .interfaces
= (InterfaceInfo
[]) {
483 { TYPE_IPMI_INTERFACE
},
488 static void ipmi_register_types(void)
490 type_register_static(&isa_ipmi_kcs_info
);
493 type_init(ipmi_register_types
)