xref: /openbmc/qemu/hw/ppc/pnv_bmc.c (revision 650d103d)
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 "sysemu/sysemu.h"
21 #include "target/ppc/cpu.h"
22 #include "qemu/log.h"
23 #include "hw/ipmi/ipmi.h"
24 #include "hw/ppc/fdt.h"
25 
26 #include "hw/ppc/pnv.h"
27 
28 #include <libfdt.h>
29 
30 /* TODO: include definition in ipmi.h */
31 #define IPMI_SDR_FULL_TYPE 1
32 
33 /*
34  * OEM SEL Event data packet sent by BMC in response of a Read Event
35  * Message Buffer command
36  */
37 typedef struct OemSel {
38     /* SEL header */
39     uint8_t id[2];
40     uint8_t type;
41     uint8_t timestamp[4];
42     uint8_t manuf_id[3];
43 
44     /* OEM SEL data (6 bytes) follows */
45     uint8_t netfun;
46     uint8_t cmd;
47     uint8_t data[4];
48 } OemSel;
49 
50 #define SOFT_OFF        0x00
51 #define SOFT_REBOOT     0x01
52 
53 static void pnv_gen_oem_sel(IPMIBmc *bmc, uint8_t reboot)
54 {
55     /* IPMI SEL Event are 16 bytes long */
56     OemSel sel = {
57         .id        = { 0x55 , 0x55 },
58         .type      = 0xC0, /* OEM */
59         .manuf_id  = { 0x0, 0x0, 0x0 },
60         .timestamp = { 0x0, 0x0, 0x0, 0x0 },
61         .netfun    = 0x3A, /* IBM */
62         .cmd       = 0x04, /* AMI OEM SEL Power Notification */
63         .data      = { reboot, 0xFF, 0xFF, 0xFF },
64     };
65 
66     ipmi_bmc_gen_event(bmc, (uint8_t *) &sel, 0 /* do not log the event */);
67 }
68 
69 void pnv_bmc_powerdown(IPMIBmc *bmc)
70 {
71     pnv_gen_oem_sel(bmc, SOFT_OFF);
72 }
73 
74 void pnv_dt_bmc_sensors(IPMIBmc *bmc, void *fdt)
75 {
76     int offset;
77     int i;
78     const struct ipmi_sdr_compact *sdr;
79     uint16_t nextrec;
80 
81     offset = fdt_add_subnode(fdt, 0, "/bmc");
82     _FDT(offset);
83 
84     _FDT((fdt_setprop_string(fdt, offset, "name", "bmc")));
85     _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
86     _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
87 
88     offset = fdt_add_subnode(fdt, offset, "sensors");
89     _FDT(offset);
90 
91     _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
92     _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
93 
94     for (i = 0; !ipmi_bmc_sdr_find(bmc, i, &sdr, &nextrec); i++) {
95         int off;
96         char *name;
97 
98         if (sdr->header.rec_type != IPMI_SDR_COMPACT_TYPE &&
99             sdr->header.rec_type != IPMI_SDR_FULL_TYPE) {
100             continue;
101         }
102 
103         name = g_strdup_printf("sensor@%x", sdr->sensor_owner_number);
104         off = fdt_add_subnode(fdt, offset, name);
105         _FDT(off);
106         g_free(name);
107 
108         _FDT((fdt_setprop_cell(fdt, off, "reg", sdr->sensor_owner_number)));
109         _FDT((fdt_setprop_string(fdt, off, "name", "sensor")));
110         _FDT((fdt_setprop_string(fdt, off, "compatible", "ibm,ipmi-sensor")));
111         _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-reading-type",
112                                sdr->reading_type)));
113         _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-id",
114                                sdr->entity_id)));
115         _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-instance",
116                                sdr->entity_instance)));
117         _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-type",
118                                sdr->sensor_type)));
119     }
120 }
121