1aeaef83dSCédric Le Goater /* 2aeaef83dSCédric Le Goater * QEMU PowerNV, BMC related functions 3aeaef83dSCédric Le Goater * 4aeaef83dSCédric Le Goater * Copyright (c) 2016-2017, IBM Corporation. 5aeaef83dSCédric Le Goater * 6aeaef83dSCédric Le Goater * This program is free software; you can redistribute it and/or modify 7aeaef83dSCédric Le Goater * it under the terms of the GNU General Public License, version 2, as 8aeaef83dSCédric Le Goater * published by the Free Software Foundation. 9aeaef83dSCédric Le Goater * 10aeaef83dSCédric Le Goater * This program is distributed in the hope that it will be useful, 11aeaef83dSCédric Le Goater * but WITHOUT ANY WARRANTY; without even the implied warranty of 12aeaef83dSCédric Le Goater * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13aeaef83dSCédric Le Goater * GNU General Public License for more details. 14aeaef83dSCédric Le Goater * 15aeaef83dSCédric Le Goater * You should have received a copy of the GNU General Public License 16aeaef83dSCédric Le Goater * along with this program; if not, see <http://www.gnu.org/licenses/>. 17aeaef83dSCédric Le Goater */ 18aeaef83dSCédric Le Goater 19aeaef83dSCédric Le Goater #include "qemu/osdep.h" 20e2392d43SCédric Le Goater #include "qemu-common.h" 21e2392d43SCédric Le Goater #include "qapi/error.h" 22aeaef83dSCédric Le Goater #include "target/ppc/cpu.h" 23aeaef83dSCédric Le Goater #include "qemu/log.h" 24aeaef83dSCédric Le Goater #include "hw/ipmi/ipmi.h" 25aeaef83dSCédric Le Goater #include "hw/ppc/fdt.h" 26aeaef83dSCédric Le Goater 27aeaef83dSCédric Le Goater #include "hw/ppc/pnv.h" 28aeaef83dSCédric Le Goater 29aeaef83dSCédric Le Goater #include <libfdt.h> 30aeaef83dSCédric Le Goater 31aeaef83dSCédric Le Goater /* TODO: include definition in ipmi.h */ 32aeaef83dSCédric Le Goater #define IPMI_SDR_FULL_TYPE 1 33aeaef83dSCédric Le Goater 34bce0b691SCédric Le Goater /* 35bce0b691SCédric Le Goater * OEM SEL Event data packet sent by BMC in response of a Read Event 36bce0b691SCédric Le Goater * Message Buffer command 37bce0b691SCédric Le Goater */ 38bce0b691SCédric Le Goater typedef struct OemSel { 39bce0b691SCédric Le Goater /* SEL header */ 40bce0b691SCédric Le Goater uint8_t id[2]; 41bce0b691SCédric Le Goater uint8_t type; 42bce0b691SCédric Le Goater uint8_t timestamp[4]; 43bce0b691SCédric Le Goater uint8_t manuf_id[3]; 44bce0b691SCédric Le Goater 45bce0b691SCédric Le Goater /* OEM SEL data (6 bytes) follows */ 46bce0b691SCédric Le Goater uint8_t netfun; 47bce0b691SCédric Le Goater uint8_t cmd; 48bce0b691SCédric Le Goater uint8_t data[4]; 49bce0b691SCédric Le Goater } OemSel; 50bce0b691SCédric Le Goater 51bce0b691SCédric Le Goater #define SOFT_OFF 0x00 52bce0b691SCédric Le Goater #define SOFT_REBOOT 0x01 53bce0b691SCédric Le Goater 54bce0b691SCédric Le Goater static void pnv_gen_oem_sel(IPMIBmc *bmc, uint8_t reboot) 55bce0b691SCédric Le Goater { 56bce0b691SCédric Le Goater /* IPMI SEL Event are 16 bytes long */ 57bce0b691SCédric Le Goater OemSel sel = { 58bce0b691SCédric Le Goater .id = { 0x55 , 0x55 }, 59bce0b691SCédric Le Goater .type = 0xC0, /* OEM */ 60bce0b691SCédric Le Goater .manuf_id = { 0x0, 0x0, 0x0 }, 61bce0b691SCédric Le Goater .timestamp = { 0x0, 0x0, 0x0, 0x0 }, 62bce0b691SCédric Le Goater .netfun = 0x3A, /* IBM */ 63bce0b691SCédric Le Goater .cmd = 0x04, /* AMI OEM SEL Power Notification */ 64bce0b691SCédric Le Goater .data = { reboot, 0xFF, 0xFF, 0xFF }, 65bce0b691SCédric Le Goater }; 66bce0b691SCédric Le Goater 67bce0b691SCédric Le Goater ipmi_bmc_gen_event(bmc, (uint8_t *) &sel, 0 /* do not log the event */); 68bce0b691SCédric Le Goater } 69bce0b691SCédric Le Goater 70bce0b691SCédric Le Goater void pnv_bmc_powerdown(IPMIBmc *bmc) 71bce0b691SCédric Le Goater { 72bce0b691SCédric Le Goater pnv_gen_oem_sel(bmc, SOFT_OFF); 73bce0b691SCédric Le Goater } 74bce0b691SCédric Le Goater 75b168a138SCédric Le Goater void pnv_dt_bmc_sensors(IPMIBmc *bmc, void *fdt) 76aeaef83dSCédric Le Goater { 77aeaef83dSCédric Le Goater int offset; 78aeaef83dSCédric Le Goater int i; 79aeaef83dSCédric Le Goater const struct ipmi_sdr_compact *sdr; 80aeaef83dSCédric Le Goater uint16_t nextrec; 81aeaef83dSCédric Le Goater 82f42b6f53SCédric Le Goater offset = fdt_add_subnode(fdt, 0, "bmc"); 83aeaef83dSCédric Le Goater _FDT(offset); 84aeaef83dSCédric Le Goater 85aeaef83dSCédric Le Goater _FDT((fdt_setprop_string(fdt, offset, "name", "bmc"))); 86aeaef83dSCédric Le Goater offset = fdt_add_subnode(fdt, offset, "sensors"); 87aeaef83dSCédric Le Goater _FDT(offset); 88aeaef83dSCédric Le Goater 89aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1))); 90aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0))); 91aeaef83dSCédric Le Goater 92aeaef83dSCédric Le Goater for (i = 0; !ipmi_bmc_sdr_find(bmc, i, &sdr, &nextrec); i++) { 93aeaef83dSCédric Le Goater int off; 94aeaef83dSCédric Le Goater char *name; 95aeaef83dSCédric Le Goater 96aeaef83dSCédric Le Goater if (sdr->header.rec_type != IPMI_SDR_COMPACT_TYPE && 97aeaef83dSCédric Le Goater sdr->header.rec_type != IPMI_SDR_FULL_TYPE) { 98aeaef83dSCédric Le Goater continue; 99aeaef83dSCédric Le Goater } 100aeaef83dSCédric Le Goater 101aeaef83dSCédric Le Goater name = g_strdup_printf("sensor@%x", sdr->sensor_owner_number); 102aeaef83dSCédric Le Goater off = fdt_add_subnode(fdt, offset, name); 103aeaef83dSCédric Le Goater _FDT(off); 104aeaef83dSCédric Le Goater g_free(name); 105aeaef83dSCédric Le Goater 106aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, off, "reg", sdr->sensor_owner_number))); 107aeaef83dSCédric Le Goater _FDT((fdt_setprop_string(fdt, off, "name", "sensor"))); 108aeaef83dSCédric Le Goater _FDT((fdt_setprop_string(fdt, off, "compatible", "ibm,ipmi-sensor"))); 109aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-reading-type", 110aeaef83dSCédric Le Goater sdr->reading_type))); 111aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-id", 112aeaef83dSCédric Le Goater sdr->entity_id))); 113aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-instance", 114aeaef83dSCédric Le Goater sdr->entity_instance))); 115aeaef83dSCédric Le Goater _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-type", 116aeaef83dSCédric Le Goater sdr->sensor_type))); 117aeaef83dSCédric Le Goater } 118aeaef83dSCédric Le Goater } 119ca661faeSCédric Le Goater 120ca661faeSCédric Le Goater /* 121ca661faeSCédric Le Goater * HIOMAP protocol handler 122ca661faeSCédric Le Goater */ 123ca661faeSCédric Le Goater #define HIOMAP_C_RESET 1 124ca661faeSCédric Le Goater #define HIOMAP_C_GET_INFO 2 125ca661faeSCédric Le Goater #define HIOMAP_C_GET_FLASH_INFO 3 126ca661faeSCédric Le Goater #define HIOMAP_C_CREATE_READ_WINDOW 4 127ca661faeSCédric Le Goater #define HIOMAP_C_CLOSE_WINDOW 5 128ca661faeSCédric Le Goater #define HIOMAP_C_CREATE_WRITE_WINDOW 6 129ca661faeSCédric Le Goater #define HIOMAP_C_MARK_DIRTY 7 130ca661faeSCédric Le Goater #define HIOMAP_C_FLUSH 8 131ca661faeSCédric Le Goater #define HIOMAP_C_ACK 9 132ca661faeSCédric Le Goater #define HIOMAP_C_ERASE 10 133ca661faeSCédric Le Goater #define HIOMAP_C_DEVICE_NAME 11 134ca661faeSCédric Le Goater #define HIOMAP_C_LOCK 12 135ca661faeSCédric Le Goater 136ca661faeSCédric Le Goater #define BLOCK_SHIFT 12 /* 4K */ 137ca661faeSCédric Le Goater 138ca661faeSCédric Le Goater static uint16_t bytes_to_blocks(uint32_t bytes) 139ca661faeSCédric Le Goater { 140ca661faeSCédric Le Goater return bytes >> BLOCK_SHIFT; 141ca661faeSCédric Le Goater } 142ca661faeSCédric Le Goater 143ca661faeSCédric Le Goater static void hiomap_cmd(IPMIBmcSim *ibs, uint8_t *cmd, unsigned int cmd_len, 144ca661faeSCédric Le Goater RspBuffer *rsp) 145ca661faeSCédric Le Goater { 146d8137bb7SGreg Kurz PnvPnor *pnor = PNV_PNOR(object_property_get_link(OBJECT(ibs), "pnor", 147d8137bb7SGreg Kurz &error_abort)); 148ca661faeSCédric Le Goater uint32_t pnor_size = pnor->size; 149ca661faeSCédric Le Goater uint32_t pnor_addr = PNOR_SPI_OFFSET; 150ca661faeSCédric Le Goater bool readonly = false; 151ca661faeSCédric Le Goater 152ca661faeSCédric Le Goater rsp_buffer_push(rsp, cmd[2]); 153ca661faeSCédric Le Goater rsp_buffer_push(rsp, cmd[3]); 154ca661faeSCédric Le Goater 155ca661faeSCédric Le Goater switch (cmd[2]) { 156ca661faeSCédric Le Goater case HIOMAP_C_MARK_DIRTY: 157ca661faeSCédric Le Goater case HIOMAP_C_FLUSH: 158ca661faeSCédric Le Goater case HIOMAP_C_ERASE: 159ca661faeSCédric Le Goater case HIOMAP_C_ACK: 160ca661faeSCédric Le Goater break; 161ca661faeSCédric Le Goater 162ca661faeSCédric Le Goater case HIOMAP_C_GET_INFO: 163ca661faeSCédric Le Goater rsp_buffer_push(rsp, 2); /* Version 2 */ 164ca661faeSCédric Le Goater rsp_buffer_push(rsp, BLOCK_SHIFT); /* block size */ 165ca661faeSCédric Le Goater rsp_buffer_push(rsp, 0); /* Timeout */ 166ca661faeSCédric Le Goater rsp_buffer_push(rsp, 0); /* Timeout */ 167ca661faeSCédric Le Goater break; 168ca661faeSCédric Le Goater 169ca661faeSCédric Le Goater case HIOMAP_C_GET_FLASH_INFO: 170ca661faeSCédric Le Goater rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) & 0xFF); 171ca661faeSCédric Le Goater rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) >> 8); 172ca661faeSCédric Le Goater rsp_buffer_push(rsp, 0x01); /* erase size */ 173ca661faeSCédric Le Goater rsp_buffer_push(rsp, 0x00); /* erase size */ 174ca661faeSCédric Le Goater break; 175ca661faeSCédric Le Goater 176ca661faeSCédric Le Goater case HIOMAP_C_CREATE_READ_WINDOW: 177ca661faeSCédric Le Goater readonly = true; 178ca661faeSCédric Le Goater /* Fall through */ 179ca661faeSCédric Le Goater 180ca661faeSCédric Le Goater case HIOMAP_C_CREATE_WRITE_WINDOW: 181ca661faeSCédric Le Goater memory_region_set_readonly(&pnor->mmio, readonly); 182ca661faeSCédric Le Goater memory_region_set_enabled(&pnor->mmio, true); 183ca661faeSCédric Le Goater 184ca661faeSCédric Le Goater rsp_buffer_push(rsp, bytes_to_blocks(pnor_addr) & 0xFF); 185ca661faeSCédric Le Goater rsp_buffer_push(rsp, bytes_to_blocks(pnor_addr) >> 8); 186ca661faeSCédric Le Goater rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) & 0xFF); 187ca661faeSCédric Le Goater rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) >> 8); 188ca661faeSCédric Le Goater rsp_buffer_push(rsp, 0x00); /* offset */ 189ca661faeSCédric Le Goater rsp_buffer_push(rsp, 0x00); /* offset */ 190ca661faeSCédric Le Goater break; 191ca661faeSCédric Le Goater 192ca661faeSCédric Le Goater case HIOMAP_C_CLOSE_WINDOW: 193ca661faeSCédric Le Goater memory_region_set_enabled(&pnor->mmio, false); 194ca661faeSCédric Le Goater break; 195ca661faeSCédric Le Goater 196ca661faeSCédric Le Goater case HIOMAP_C_DEVICE_NAME: 197ca661faeSCédric Le Goater case HIOMAP_C_RESET: 198ca661faeSCédric Le Goater case HIOMAP_C_LOCK: 199ca661faeSCédric Le Goater default: 200ca661faeSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "HIOMAP: unknow command %02X\n", cmd[2]); 201ca661faeSCédric Le Goater break; 202ca661faeSCédric Le Goater } 203ca661faeSCédric Le Goater } 204ca661faeSCédric Le Goater 205ca661faeSCédric Le Goater #define HIOMAP 0x5a 206ca661faeSCédric Le Goater 207ca661faeSCédric Le Goater static const IPMICmdHandler hiomap_cmds[] = { 208ca661faeSCédric Le Goater [HIOMAP] = { hiomap_cmd, 3 }, 209ca661faeSCédric Le Goater }; 210ca661faeSCédric Le Goater 211ca661faeSCédric Le Goater static const IPMINetfn hiomap_netfn = { 212ca661faeSCédric Le Goater .cmd_nums = ARRAY_SIZE(hiomap_cmds), 213ca661faeSCédric Le Goater .cmd_handlers = hiomap_cmds 214ca661faeSCédric Le Goater }; 215ca661faeSCédric Le Goater 21625f3170bSCédric Le Goater 21725f3170bSCédric Le Goater void pnv_bmc_set_pnor(IPMIBmc *bmc, PnvPnor *pnor) 21825f3170bSCédric Le Goater { 21925f3170bSCédric Le Goater object_ref(OBJECT(pnor)); 220d2623129SMarkus Armbruster object_property_add_const_link(OBJECT(bmc), "pnor", OBJECT(pnor)); 22125f3170bSCédric Le Goater 22225f3170bSCédric Le Goater /* Install the HIOMAP protocol handlers to access the PNOR */ 22325f3170bSCédric Le Goater ipmi_sim_register_netfn(IPMI_BMC_SIMULATOR(bmc), IPMI_NETFN_OEM, 22425f3170bSCédric Le Goater &hiomap_netfn); 22525f3170bSCédric Le Goater } 22625f3170bSCédric Le Goater 227e2392d43SCédric Le Goater /* 228e2392d43SCédric Le Goater * Instantiate the machine BMC. PowerNV uses the QEMU internal 229e2392d43SCédric Le Goater * simulator but it could also be external. 230e2392d43SCédric Le Goater */ 231d8137bb7SGreg Kurz IPMIBmc *pnv_bmc_create(PnvPnor *pnor) 232ca661faeSCédric Le Goater { 233e2392d43SCédric Le Goater Object *obj; 234e2392d43SCédric Le Goater 235e2392d43SCédric Le Goater obj = object_new(TYPE_IPMI_BMC_SIMULATOR); 236d8137bb7SGreg Kurz object_ref(OBJECT(pnor)); 237d2623129SMarkus Armbruster object_property_add_const_link(obj, "pnor", OBJECT(pnor)); 238*ce189ab2SMarkus Armbruster qdev_realize(DEVICE(obj), NULL, &error_fatal); 239e2392d43SCédric Le Goater 240e2392d43SCédric Le Goater /* Install the HIOMAP protocol handlers to access the PNOR */ 241e2392d43SCédric Le Goater ipmi_sim_register_netfn(IPMI_BMC_SIMULATOR(obj), IPMI_NETFN_OEM, 242e2392d43SCédric Le Goater &hiomap_netfn); 243e2392d43SCédric Le Goater 244e2392d43SCédric Le Goater return IPMI_BMC(obj); 245ca661faeSCédric Le Goater } 24625f3170bSCédric Le Goater 24725f3170bSCédric Le Goater typedef struct ForeachArgs { 24825f3170bSCédric Le Goater const char *name; 24925f3170bSCédric Le Goater Object *obj; 25025f3170bSCédric Le Goater } ForeachArgs; 25125f3170bSCédric Le Goater 25225f3170bSCédric Le Goater static int bmc_find(Object *child, void *opaque) 25325f3170bSCédric Le Goater { 25425f3170bSCédric Le Goater ForeachArgs *args = opaque; 25525f3170bSCédric Le Goater 25625f3170bSCédric Le Goater if (object_dynamic_cast(child, args->name)) { 25725f3170bSCédric Le Goater if (args->obj) { 25825f3170bSCédric Le Goater return 1; 25925f3170bSCédric Le Goater } 26025f3170bSCédric Le Goater args->obj = child; 26125f3170bSCédric Le Goater } 26225f3170bSCédric Le Goater return 0; 26325f3170bSCédric Le Goater } 26425f3170bSCédric Le Goater 26525f3170bSCédric Le Goater IPMIBmc *pnv_bmc_find(Error **errp) 26625f3170bSCédric Le Goater { 26725f3170bSCédric Le Goater ForeachArgs args = { TYPE_IPMI_BMC_SIMULATOR, NULL }; 26825f3170bSCédric Le Goater int ret; 26925f3170bSCédric Le Goater 27025f3170bSCédric Le Goater ret = object_child_foreach_recursive(object_get_root(), bmc_find, &args); 27125f3170bSCédric Le Goater if (ret) { 27225f3170bSCédric Le Goater error_setg(errp, "machine should have only one BMC device. " 27325f3170bSCédric Le Goater "Use '-nodefaults'"); 27425f3170bSCédric Le Goater return NULL; 27525f3170bSCédric Le Goater } 27625f3170bSCédric Le Goater 27725f3170bSCédric Le Goater return args.obj ? IPMI_BMC(args.obj) : NULL; 27825f3170bSCédric Le Goater } 279