xref: /openbmc/qemu/hw/ppc/pnv_bmc.c (revision 59a3a1c0)
1 /*
2  * QEMU PowerNV, BMC related functions
3  *
4  * Copyright (c) 2016-2017, IBM Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 2, as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "target/ppc/cpu.h"
21 #include "qemu/log.h"
22 #include "hw/ipmi/ipmi.h"
23 #include "hw/ppc/fdt.h"
24 
25 #include "hw/ppc/pnv.h"
26 
27 #include <libfdt.h>
28 
29 /* TODO: include definition in ipmi.h */
30 #define IPMI_SDR_FULL_TYPE 1
31 
32 /*
33  * OEM SEL Event data packet sent by BMC in response of a Read Event
34  * Message Buffer command
35  */
36 typedef struct OemSel {
37     /* SEL header */
38     uint8_t id[2];
39     uint8_t type;
40     uint8_t timestamp[4];
41     uint8_t manuf_id[3];
42 
43     /* OEM SEL data (6 bytes) follows */
44     uint8_t netfun;
45     uint8_t cmd;
46     uint8_t data[4];
47 } OemSel;
48 
49 #define SOFT_OFF        0x00
50 #define SOFT_REBOOT     0x01
51 
52 static void pnv_gen_oem_sel(IPMIBmc *bmc, uint8_t reboot)
53 {
54     /* IPMI SEL Event are 16 bytes long */
55     OemSel sel = {
56         .id        = { 0x55 , 0x55 },
57         .type      = 0xC0, /* OEM */
58         .manuf_id  = { 0x0, 0x0, 0x0 },
59         .timestamp = { 0x0, 0x0, 0x0, 0x0 },
60         .netfun    = 0x3A, /* IBM */
61         .cmd       = 0x04, /* AMI OEM SEL Power Notification */
62         .data      = { reboot, 0xFF, 0xFF, 0xFF },
63     };
64 
65     ipmi_bmc_gen_event(bmc, (uint8_t *) &sel, 0 /* do not log the event */);
66 }
67 
68 void pnv_bmc_powerdown(IPMIBmc *bmc)
69 {
70     pnv_gen_oem_sel(bmc, SOFT_OFF);
71 }
72 
73 void pnv_dt_bmc_sensors(IPMIBmc *bmc, void *fdt)
74 {
75     int offset;
76     int i;
77     const struct ipmi_sdr_compact *sdr;
78     uint16_t nextrec;
79 
80     offset = fdt_add_subnode(fdt, 0, "/bmc");
81     _FDT(offset);
82 
83     _FDT((fdt_setprop_string(fdt, offset, "name", "bmc")));
84     _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
85     _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
86 
87     offset = fdt_add_subnode(fdt, offset, "sensors");
88     _FDT(offset);
89 
90     _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
91     _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
92 
93     for (i = 0; !ipmi_bmc_sdr_find(bmc, i, &sdr, &nextrec); i++) {
94         int off;
95         char *name;
96 
97         if (sdr->header.rec_type != IPMI_SDR_COMPACT_TYPE &&
98             sdr->header.rec_type != IPMI_SDR_FULL_TYPE) {
99             continue;
100         }
101 
102         name = g_strdup_printf("sensor@%x", sdr->sensor_owner_number);
103         off = fdt_add_subnode(fdt, offset, name);
104         _FDT(off);
105         g_free(name);
106 
107         _FDT((fdt_setprop_cell(fdt, off, "reg", sdr->sensor_owner_number)));
108         _FDT((fdt_setprop_string(fdt, off, "name", "sensor")));
109         _FDT((fdt_setprop_string(fdt, off, "compatible", "ibm,ipmi-sensor")));
110         _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-reading-type",
111                                sdr->reading_type)));
112         _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-id",
113                                sdr->entity_id)));
114         _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-instance",
115                                sdr->entity_instance)));
116         _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-type",
117                                sdr->sensor_type)));
118     }
119 }
120