2 * QEMU ISA IPMI BT 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
24 #include "qemu/osdep.h"
26 #include "qapi/error.h"
28 #include "hw/ipmi/ipmi.h"
29 #include "hw/isa/isa.h"
31 /* Control register */
32 #define IPMI_BT_CLR_WR_BIT 0
33 #define IPMI_BT_CLR_RD_BIT 1
34 #define IPMI_BT_H2B_ATN_BIT 2
35 #define IPMI_BT_B2H_ATN_BIT 3
36 #define IPMI_BT_SMS_ATN_BIT 4
37 #define IPMI_BT_HBUSY_BIT 6
38 #define IPMI_BT_BBUSY_BIT 7
40 #define IPMI_BT_GET_CLR_WR(d) (((d) >> IPMI_BT_CLR_WR_BIT) & 0x1)
42 #define IPMI_BT_GET_CLR_RD(d) (((d) >> IPMI_BT_CLR_RD_BIT) & 0x1)
44 #define IPMI_BT_GET_H2B_ATN(d) (((d) >> IPMI_BT_H2B_ATN_BIT) & 0x1)
46 #define IPMI_BT_B2H_ATN_MASK (1 << IPMI_BT_B2H_ATN_BIT)
47 #define IPMI_BT_GET_B2H_ATN(d) (((d) >> IPMI_BT_B2H_ATN_BIT) & 0x1)
48 #define IPMI_BT_SET_B2H_ATN(d, v) ((d) = (((d) & ~IPMI_BT_B2H_ATN_MASK) | \
49 (!!(v) << IPMI_BT_B2H_ATN_BIT)))
51 #define IPMI_BT_SMS_ATN_MASK (1 << IPMI_BT_SMS_ATN_BIT)
52 #define IPMI_BT_GET_SMS_ATN(d) (((d) >> IPMI_BT_SMS_ATN_BIT) & 0x1)
53 #define IPMI_BT_SET_SMS_ATN(d, v) ((d) = (((d) & ~IPMI_BT_SMS_ATN_MASK) | \
54 (!!(v) << IPMI_BT_SMS_ATN_BIT)))
56 #define IPMI_BT_HBUSY_MASK (1 << IPMI_BT_HBUSY_BIT)
57 #define IPMI_BT_GET_HBUSY(d) (((d) >> IPMI_BT_HBUSY_BIT) & 0x1)
58 #define IPMI_BT_SET_HBUSY(d, v) ((d) = (((d) & ~IPMI_BT_HBUSY_MASK) | \
59 (!!(v) << IPMI_BT_HBUSY_BIT)))
61 #define IPMI_BT_BBUSY_MASK (1 << IPMI_BT_BBUSY_BIT)
62 #define IPMI_BT_SET_BBUSY(d, v) ((d) = (((d) & ~IPMI_BT_BBUSY_MASK) | \
63 (!!(v) << IPMI_BT_BBUSY_BIT)))
67 #define IPMI_BT_B2H_IRQ_EN_BIT 0
68 #define IPMI_BT_B2H_IRQ_BIT 1
70 #define IPMI_BT_B2H_IRQ_EN_MASK (1 << IPMI_BT_B2H_IRQ_EN_BIT)
71 #define IPMI_BT_GET_B2H_IRQ_EN(d) (((d) >> IPMI_BT_B2H_IRQ_EN_BIT) & 0x1)
72 #define IPMI_BT_SET_B2H_IRQ_EN(d, v) ((d) = (((d) & ~IPMI_BT_B2H_IRQ_EN_MASK) |\
73 (!!(v) << IPMI_BT_B2H_IRQ_EN_BIT)))
75 #define IPMI_BT_B2H_IRQ_MASK (1 << IPMI_BT_B2H_IRQ_BIT)
76 #define IPMI_BT_GET_B2H_IRQ(d) (((d) >> IPMI_BT_B2H_IRQ_BIT) & 0x1)
77 #define IPMI_BT_SET_B2H_IRQ(d, v) ((d) = (((d) & ~IPMI_BT_B2H_IRQ_MASK) | \
78 (!!(v) << IPMI_BT_B2H_IRQ_BIT)))
80 typedef struct IPMIBT
{
88 unsigned long io_length
;
96 uint8_t outmsg
[MAX_IPMI_MSG_SIZE
];
100 uint8_t inmsg
[MAX_IPMI_MSG_SIZE
];
107 * This is a response number that we send with the command to make
108 * sure that the response matches the command.
114 #define IPMI_CMD_GET_BT_INTF_CAP 0x36
116 static void ipmi_bt_handle_event(IPMIInterface
*ii
)
118 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
119 IPMIBT
*ib
= iic
->get_backend_data(ii
);
124 /* Note that overruns are handled by handle_command */
125 if (ib
->inmsg
[0] != (ib
->inlen
- 1)) {
126 /* Length mismatch, just ignore. */
127 IPMI_BT_SET_BBUSY(ib
->control_reg
, 1);
131 if ((ib
->inmsg
[1] == (IPMI_NETFN_APP
<< 2)) &&
132 (ib
->inmsg
[3] == IPMI_CMD_GET_BT_INTF_CAP
)) {
133 /* We handle this one ourselves. */
135 ib
->outmsg
[1] = ib
->inmsg
[1] | 0x04;
136 ib
->outmsg
[2] = ib
->inmsg
[2];
137 ib
->outmsg
[3] = ib
->inmsg
[3];
139 ib
->outmsg
[5] = 1; /* Only support 1 outstanding request. */
140 if (sizeof(ib
->inmsg
) > 0xff) { /* Input buffer size */
141 ib
->outmsg
[6] = 0xff;
143 ib
->outmsg
[6] = (unsigned char) sizeof(ib
->inmsg
);
145 if (sizeof(ib
->outmsg
) > 0xff) { /* Output buffer size */
146 ib
->outmsg
[7] = 0xff;
148 ib
->outmsg
[7] = (unsigned char) sizeof(ib
->outmsg
);
150 ib
->outmsg
[8] = 10; /* Max request to response time */
151 ib
->outmsg
[9] = 0; /* Don't recommend retries */
153 IPMI_BT_SET_BBUSY(ib
->control_reg
, 0);
154 IPMI_BT_SET_B2H_ATN(ib
->control_reg
, 1);
155 if (ib
->use_irq
&& ib
->irqs_enabled
&&
156 !IPMI_BT_GET_B2H_IRQ(ib
->mask_reg
) &&
157 IPMI_BT_GET_B2H_IRQ_EN(ib
->mask_reg
)) {
158 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 1);
159 qemu_irq_raise(ib
->irq
);
163 ib
->waiting_seq
= ib
->inmsg
[2];
164 ib
->inmsg
[2] = ib
->inmsg
[1];
166 IPMIBmcClass
*bk
= IPMI_BMC_GET_CLASS(ib
->bmc
);
167 bk
->handle_command(ib
->bmc
, ib
->inmsg
+ 2, ib
->inlen
- 2,
168 sizeof(ib
->inmsg
), ib
->waiting_rsp
);
174 static void ipmi_bt_handle_rsp(IPMIInterface
*ii
, uint8_t msg_id
,
175 unsigned char *rsp
, unsigned int rsp_len
)
177 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
178 IPMIBT
*ib
= iic
->get_backend_data(ii
);
180 if (ib
->waiting_rsp
== msg_id
) {
182 if (rsp_len
> (sizeof(ib
->outmsg
) - 2)) {
184 ib
->outmsg
[1] = rsp
[0];
185 ib
->outmsg
[2] = ib
->waiting_seq
;
186 ib
->outmsg
[3] = rsp
[1];
187 ib
->outmsg
[4] = IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES
;
190 ib
->outmsg
[0] = rsp_len
+ 1;
191 ib
->outmsg
[1] = rsp
[0];
192 ib
->outmsg
[2] = ib
->waiting_seq
;
193 memcpy(ib
->outmsg
+ 3, rsp
+ 1, rsp_len
- 1);
194 ib
->outlen
= rsp_len
+ 2;
196 IPMI_BT_SET_BBUSY(ib
->control_reg
, 0);
197 IPMI_BT_SET_B2H_ATN(ib
->control_reg
, 1);
198 if (ib
->use_irq
&& ib
->irqs_enabled
&&
199 !IPMI_BT_GET_B2H_IRQ(ib
->mask_reg
) &&
200 IPMI_BT_GET_B2H_IRQ_EN(ib
->mask_reg
)) {
201 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 1);
202 qemu_irq_raise(ib
->irq
);
208 static uint64_t ipmi_bt_ioport_read(void *opaque
, hwaddr addr
, unsigned size
)
210 IPMIInterface
*ii
= opaque
;
211 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
212 IPMIBT
*ib
= iic
->get_backend_data(ii
);
217 ret
= ib
->control_reg
;
220 if (ib
->outpos
< ib
->outlen
) {
221 ret
= ib
->outmsg
[ib
->outpos
];
223 if (ib
->outpos
== ib
->outlen
) {
238 static void ipmi_bt_signal(IPMIBT
*ib
, IPMIInterface
*ii
)
240 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
243 while (ib
->do_wake
) {
245 iic
->handle_if_event(ii
);
249 static void ipmi_bt_ioport_write(void *opaque
, hwaddr addr
, uint64_t val
,
252 IPMIInterface
*ii
= opaque
;
253 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
254 IPMIBT
*ib
= iic
->get_backend_data(ii
);
258 if (IPMI_BT_GET_CLR_WR(val
)) {
261 if (IPMI_BT_GET_CLR_RD(val
)) {
264 if (IPMI_BT_GET_B2H_ATN(val
)) {
265 IPMI_BT_SET_B2H_ATN(ib
->control_reg
, 0);
267 if (IPMI_BT_GET_SMS_ATN(val
)) {
268 IPMI_BT_SET_SMS_ATN(ib
->control_reg
, 0);
270 if (IPMI_BT_GET_HBUSY(val
)) {
272 IPMI_BT_SET_HBUSY(ib
->control_reg
,
273 !IPMI_BT_GET_HBUSY(ib
->control_reg
));
275 if (IPMI_BT_GET_H2B_ATN(val
)) {
276 IPMI_BT_SET_BBUSY(ib
->control_reg
, 1);
277 ipmi_bt_signal(ib
, ii
);
282 if (ib
->inlen
< sizeof(ib
->inmsg
)) {
283 ib
->inmsg
[ib
->inlen
] = val
;
289 if (IPMI_BT_GET_B2H_IRQ_EN(val
) !=
290 IPMI_BT_GET_B2H_IRQ_EN(ib
->mask_reg
)) {
291 if (IPMI_BT_GET_B2H_IRQ_EN(val
)) {
292 if (IPMI_BT_GET_B2H_ATN(ib
->control_reg
) ||
293 IPMI_BT_GET_SMS_ATN(ib
->control_reg
)) {
294 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 1);
295 qemu_irq_raise(ib
->irq
);
297 IPMI_BT_SET_B2H_IRQ_EN(ib
->mask_reg
, 1);
299 if (IPMI_BT_GET_B2H_IRQ(ib
->mask_reg
)) {
300 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 0);
301 qemu_irq_lower(ib
->irq
);
303 IPMI_BT_SET_B2H_IRQ_EN(ib
->mask_reg
, 0);
306 if (IPMI_BT_GET_B2H_IRQ(val
) && IPMI_BT_GET_B2H_IRQ(ib
->mask_reg
)) {
307 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 0);
308 qemu_irq_lower(ib
->irq
);
314 static const MemoryRegionOps ipmi_bt_io_ops
= {
315 .read
= ipmi_bt_ioport_read
,
316 .write
= ipmi_bt_ioport_write
,
318 .min_access_size
= 1,
319 .max_access_size
= 1,
321 .endianness
= DEVICE_LITTLE_ENDIAN
,
324 static void ipmi_bt_set_atn(IPMIInterface
*ii
, int val
, int irq
)
326 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
327 IPMIBT
*ib
= iic
->get_backend_data(ii
);
329 if (!!val
== IPMI_BT_GET_SMS_ATN(ib
->control_reg
)) {
333 IPMI_BT_SET_SMS_ATN(ib
->control_reg
, val
);
335 if (irq
&& ib
->use_irq
&& ib
->irqs_enabled
&&
336 !IPMI_BT_GET_B2H_ATN(ib
->control_reg
) &&
337 IPMI_BT_GET_B2H_IRQ_EN(ib
->mask_reg
)) {
338 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 1);
339 qemu_irq_raise(ib
->irq
);
342 if (!IPMI_BT_GET_B2H_ATN(ib
->control_reg
) &&
343 IPMI_BT_GET_B2H_IRQ(ib
->mask_reg
)) {
344 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 0);
345 qemu_irq_lower(ib
->irq
);
350 static void ipmi_bt_handle_reset(IPMIInterface
*ii
, bool is_cold
)
352 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
353 IPMIBT
*ib
= iic
->get_backend_data(ii
);
356 /* Disable the BT interrupt on reset */
357 if (IPMI_BT_GET_B2H_IRQ(ib
->mask_reg
)) {
358 IPMI_BT_SET_B2H_IRQ(ib
->mask_reg
, 0);
359 qemu_irq_lower(ib
->irq
);
361 IPMI_BT_SET_B2H_IRQ_EN(ib
->mask_reg
, 0);
365 static void ipmi_bt_set_irq_enable(IPMIInterface
*ii
, int val
)
367 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
368 IPMIBT
*ib
= iic
->get_backend_data(ii
);
370 ib
->irqs_enabled
= val
;
373 static void ipmi_bt_init(IPMIInterface
*ii
, Error
**errp
)
375 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
376 IPMIBT
*ib
= iic
->get_backend_data(ii
);
380 memory_region_init_io(&ib
->io
, NULL
, &ipmi_bt_io_ops
, ii
, "ipmi-bt", 3);
384 #define TYPE_ISA_IPMI_BT "isa-ipmi-bt"
385 #define ISA_IPMI_BT(obj) OBJECT_CHECK(ISAIPMIBTDevice, (obj), \
388 typedef struct ISAIPMIBTDevice
{
395 static void ipmi_bt_get_fwinfo(struct IPMIInterface
*ii
, IPMIFwInfo
*info
)
397 ISAIPMIBTDevice
*iib
= ISA_IPMI_BT(ii
);
399 info
->interface_name
= "bt";
400 info
->interface_type
= IPMI_SMBIOS_BT
;
401 info
->ipmi_spec_major_revision
= 2;
402 info
->ipmi_spec_minor_revision
= 0;
403 info
->base_address
= iib
->bt
.io_base
;
404 info
->register_length
= iib
->bt
.io_length
;
405 info
->register_spacing
= 1;
406 info
->memspace
= IPMI_MEMSPACE_IO
;
407 info
->irq_type
= IPMI_LEVEL_IRQ
;
408 info
->interrupt_number
= iib
->isairq
;
409 info
->i2c_slave_address
= iib
->bt
.bmc
->slave_addr
;
410 info
->uuid
= iib
->uuid
;
413 static void ipmi_bt_class_init(IPMIInterfaceClass
*iic
)
415 iic
->init
= ipmi_bt_init
;
416 iic
->set_atn
= ipmi_bt_set_atn
;
417 iic
->handle_rsp
= ipmi_bt_handle_rsp
;
418 iic
->handle_if_event
= ipmi_bt_handle_event
;
419 iic
->set_irq_enable
= ipmi_bt_set_irq_enable
;
420 iic
->reset
= ipmi_bt_handle_reset
;
421 iic
->get_fwinfo
= ipmi_bt_get_fwinfo
;
424 static void isa_ipmi_bt_realize(DeviceState
*dev
, Error
**errp
)
426 ISADevice
*isadev
= ISA_DEVICE(dev
);
427 ISAIPMIBTDevice
*iib
= ISA_IPMI_BT(dev
);
428 IPMIInterface
*ii
= IPMI_INTERFACE(dev
);
429 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
432 error_setg(errp
, "IPMI device requires a bmc attribute to be set");
436 iib
->uuid
= ipmi_next_uuid();
438 iib
->bt
.bmc
->intf
= ii
;
444 if (iib
->isairq
> 0) {
445 isa_init_irq(isadev
, &iib
->bt
.irq
, iib
->isairq
);
449 qdev_set_legacy_instance_id(dev
, iib
->bt
.io_base
, iib
->bt
.io_length
);
451 isa_register_ioport(isadev
, &iib
->bt
.io
, iib
->bt
.io_base
);
454 static int ipmi_bt_vmstate_post_load(void *opaque
, int version
)
458 /* Make sure all the values are sane. */
459 if (ib
->outpos
>= MAX_IPMI_MSG_SIZE
|| ib
->outlen
>= MAX_IPMI_MSG_SIZE
||
460 ib
->outpos
>= ib
->outlen
) {
461 qemu_log_mask(LOG_GUEST_ERROR
,
462 "ipmi:bt: vmstate transfer received bad out values: %d %d\n",
463 ib
->outpos
, ib
->outlen
);
468 if (ib
->inlen
>= MAX_IPMI_MSG_SIZE
) {
469 qemu_log_mask(LOG_GUEST_ERROR
,
470 "ipmi:bt: vmstate transfer received bad in value: %d\n",
478 const VMStateDescription vmstate_IPMIBT
= {
479 .name
= TYPE_IPMI_INTERFACE_PREFIX
"bt",
481 .minimum_version_id
= 1,
482 .post_load
= ipmi_bt_vmstate_post_load
,
483 .fields
= (VMStateField
[]) {
484 VMSTATE_BOOL(obf_irq_set
, IPMIBT
),
485 VMSTATE_BOOL(atn_irq_set
, IPMIBT
),
486 VMSTATE_BOOL(irqs_enabled
, IPMIBT
),
487 VMSTATE_UINT32(outpos
, IPMIBT
),
488 VMSTATE_UINT32(outlen
, IPMIBT
),
489 VMSTATE_UINT8_ARRAY(outmsg
, IPMIBT
, MAX_IPMI_MSG_SIZE
),
490 VMSTATE_UINT32(inlen
, IPMIBT
),
491 VMSTATE_UINT8_ARRAY(inmsg
, IPMIBT
, MAX_IPMI_MSG_SIZE
),
492 VMSTATE_UINT8(control_reg
, IPMIBT
),
493 VMSTATE_UINT8(mask_reg
, IPMIBT
),
494 VMSTATE_UINT8(waiting_rsp
, IPMIBT
),
495 VMSTATE_UINT8(waiting_seq
, IPMIBT
),
496 VMSTATE_END_OF_LIST()
500 static const VMStateDescription vmstate_ISAIPMIBTDevice
= {
501 .name
= TYPE_IPMI_INTERFACE_PREFIX
"isa-bt",
503 .minimum_version_id
= 2,
505 * Version 1 had messed up the array transfer, it's not even usable
506 * because it used VMSTATE_VBUFFER_UINT32, but it did not transfer
507 * the buffer length, so random things would happen.
509 .fields
= (VMStateField
[]) {
510 VMSTATE_STRUCT(bt
, ISAIPMIBTDevice
, 1, vmstate_IPMIBT
, IPMIBT
),
511 VMSTATE_END_OF_LIST()
515 static void isa_ipmi_bt_init(Object
*obj
)
517 ISAIPMIBTDevice
*iib
= ISA_IPMI_BT(obj
);
519 ipmi_bmc_find_and_link(obj
, (Object
**) &iib
->bt
.bmc
);
521 vmstate_register(NULL
, 0, &vmstate_ISAIPMIBTDevice
, iib
);
524 static void *isa_ipmi_bt_get_backend_data(IPMIInterface
*ii
)
526 ISAIPMIBTDevice
*iib
= ISA_IPMI_BT(ii
);
531 static Property ipmi_isa_properties
[] = {
532 DEFINE_PROP_UINT32("ioport", ISAIPMIBTDevice
, bt
.io_base
, 0xe4),
533 DEFINE_PROP_INT32("irq", ISAIPMIBTDevice
, isairq
, 5),
534 DEFINE_PROP_END_OF_LIST(),
537 static void isa_ipmi_bt_class_init(ObjectClass
*oc
, void *data
)
539 DeviceClass
*dc
= DEVICE_CLASS(oc
);
540 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_CLASS(oc
);
542 dc
->realize
= isa_ipmi_bt_realize
;
543 dc
->props
= ipmi_isa_properties
;
545 iic
->get_backend_data
= isa_ipmi_bt_get_backend_data
;
546 ipmi_bt_class_init(iic
);
549 static const TypeInfo isa_ipmi_bt_info
= {
550 .name
= TYPE_ISA_IPMI_BT
,
551 .parent
= TYPE_ISA_DEVICE
,
552 .instance_size
= sizeof(ISAIPMIBTDevice
),
553 .instance_init
= isa_ipmi_bt_init
,
554 .class_init
= isa_ipmi_bt_class_init
,
555 .interfaces
= (InterfaceInfo
[]) {
556 { TYPE_IPMI_INTERFACE
},
561 static void ipmi_register_types(void)
563 type_register_static(&isa_ipmi_bt_info
);
566 type_init(ipmi_register_types
)