Merge remote-tracking branch 'remotes/vivier2/tags/trivial-branch-for-6.0-pull-reques...
[qemu/ar7.git] / hw / ppc / pnv_bmc.c
blob67ebb16c4d5f49439afd68f139cabe5c6018fa75
1 /*
2 * QEMU PowerNV, BMC related functions
4 * Copyright (c) 2016-2017, IBM Corporation.
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.
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.
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/>.
19 #include "qemu/osdep.h"
20 #include "qemu-common.h"
21 #include "qapi/error.h"
22 #include "target/ppc/cpu.h"
23 #include "qemu/log.h"
24 #include "hw/ipmi/ipmi.h"
25 #include "hw/ppc/fdt.h"
27 #include "hw/ppc/pnv.h"
29 #include <libfdt.h>
31 /* TODO: include definition in ipmi.h */
32 #define IPMI_SDR_FULL_TYPE 1
35 * OEM SEL Event data packet sent by BMC in response of a Read Event
36 * Message Buffer command
38 typedef struct OemSel {
39 /* SEL header */
40 uint8_t id[2];
41 uint8_t type;
42 uint8_t timestamp[4];
43 uint8_t manuf_id[3];
45 /* OEM SEL data (6 bytes) follows */
46 uint8_t netfun;
47 uint8_t cmd;
48 uint8_t data[4];
49 } OemSel;
51 #define SOFT_OFF 0x00
52 #define SOFT_REBOOT 0x01
54 static void pnv_gen_oem_sel(IPMIBmc *bmc, uint8_t reboot)
56 /* IPMI SEL Event are 16 bytes long */
57 OemSel sel = {
58 .id = { 0x55 , 0x55 },
59 .type = 0xC0, /* OEM */
60 .manuf_id = { 0x0, 0x0, 0x0 },
61 .timestamp = { 0x0, 0x0, 0x0, 0x0 },
62 .netfun = 0x3A, /* IBM */
63 .cmd = 0x04, /* AMI OEM SEL Power Notification */
64 .data = { reboot, 0xFF, 0xFF, 0xFF },
67 ipmi_bmc_gen_event(bmc, (uint8_t *) &sel, 0 /* do not log the event */);
70 void pnv_bmc_powerdown(IPMIBmc *bmc)
72 pnv_gen_oem_sel(bmc, SOFT_OFF);
75 void pnv_dt_bmc_sensors(IPMIBmc *bmc, void *fdt)
77 int offset;
78 int i;
79 const struct ipmi_sdr_compact *sdr;
80 uint16_t nextrec;
82 offset = fdt_add_subnode(fdt, 0, "bmc");
83 _FDT(offset);
85 _FDT((fdt_setprop_string(fdt, offset, "name", "bmc")));
86 offset = fdt_add_subnode(fdt, offset, "sensors");
87 _FDT(offset);
89 _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
90 _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
92 for (i = 0; !ipmi_bmc_sdr_find(bmc, i, &sdr, &nextrec); i++) {
93 int off;
94 char *name;
96 if (sdr->header.rec_type != IPMI_SDR_COMPACT_TYPE &&
97 sdr->header.rec_type != IPMI_SDR_FULL_TYPE) {
98 continue;
101 name = g_strdup_printf("sensor@%x", sdr->sensor_owner_number);
102 off = fdt_add_subnode(fdt, offset, name);
103 _FDT(off);
104 g_free(name);
106 _FDT((fdt_setprop_cell(fdt, off, "reg", sdr->sensor_owner_number)));
107 _FDT((fdt_setprop_string(fdt, off, "name", "sensor")));
108 _FDT((fdt_setprop_string(fdt, off, "compatible", "ibm,ipmi-sensor")));
109 _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-reading-type",
110 sdr->reading_type)));
111 _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-id",
112 sdr->entity_id)));
113 _FDT((fdt_setprop_cell(fdt, off, "ipmi-entity-instance",
114 sdr->entity_instance)));
115 _FDT((fdt_setprop_cell(fdt, off, "ipmi-sensor-type",
116 sdr->sensor_type)));
121 * HIOMAP protocol handler
123 #define HIOMAP_C_RESET 1
124 #define HIOMAP_C_GET_INFO 2
125 #define HIOMAP_C_GET_FLASH_INFO 3
126 #define HIOMAP_C_CREATE_READ_WINDOW 4
127 #define HIOMAP_C_CLOSE_WINDOW 5
128 #define HIOMAP_C_CREATE_WRITE_WINDOW 6
129 #define HIOMAP_C_MARK_DIRTY 7
130 #define HIOMAP_C_FLUSH 8
131 #define HIOMAP_C_ACK 9
132 #define HIOMAP_C_ERASE 10
133 #define HIOMAP_C_DEVICE_NAME 11
134 #define HIOMAP_C_LOCK 12
136 #define BLOCK_SHIFT 12 /* 4K */
138 static uint16_t bytes_to_blocks(uint32_t bytes)
140 return bytes >> BLOCK_SHIFT;
143 static uint32_t blocks_to_bytes(uint16_t blocks)
145 return blocks << BLOCK_SHIFT;
148 static int hiomap_erase(PnvPnor *pnor, uint32_t offset, uint32_t size)
150 MemTxResult result;
151 int i;
153 for (i = 0; i < size / 4; i++) {
154 result = memory_region_dispatch_write(&pnor->mmio, offset + i * 4,
155 0xFFFFFFFF, MO_32,
156 MEMTXATTRS_UNSPECIFIED);
157 if (result != MEMTX_OK) {
158 return -1;
161 return 0;
164 static void hiomap_cmd(IPMIBmcSim *ibs, uint8_t *cmd, unsigned int cmd_len,
165 RspBuffer *rsp)
167 PnvPnor *pnor = PNV_PNOR(object_property_get_link(OBJECT(ibs), "pnor",
168 &error_abort));
169 uint32_t pnor_size = pnor->size;
170 uint32_t pnor_addr = PNOR_SPI_OFFSET;
171 bool readonly = false;
173 rsp_buffer_push(rsp, cmd[2]);
174 rsp_buffer_push(rsp, cmd[3]);
176 switch (cmd[2]) {
177 case HIOMAP_C_MARK_DIRTY:
178 case HIOMAP_C_FLUSH:
179 case HIOMAP_C_ACK:
180 break;
182 case HIOMAP_C_ERASE:
183 if (hiomap_erase(pnor, blocks_to_bytes(cmd[5] << 8 | cmd[4]),
184 blocks_to_bytes(cmd[7] << 8 | cmd[6]))) {
185 rsp_buffer_set_error(rsp, IPMI_CC_UNSPECIFIED);
187 break;
189 case HIOMAP_C_GET_INFO:
190 rsp_buffer_push(rsp, 2); /* Version 2 */
191 rsp_buffer_push(rsp, BLOCK_SHIFT); /* block size */
192 rsp_buffer_push(rsp, 0); /* Timeout */
193 rsp_buffer_push(rsp, 0); /* Timeout */
194 break;
196 case HIOMAP_C_GET_FLASH_INFO:
197 rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) & 0xFF);
198 rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) >> 8);
199 rsp_buffer_push(rsp, 0x01); /* erase size */
200 rsp_buffer_push(rsp, 0x00); /* erase size */
201 break;
203 case HIOMAP_C_CREATE_READ_WINDOW:
204 readonly = true;
205 /* Fall through */
207 case HIOMAP_C_CREATE_WRITE_WINDOW:
208 memory_region_set_readonly(&pnor->mmio, readonly);
209 memory_region_set_enabled(&pnor->mmio, true);
211 rsp_buffer_push(rsp, bytes_to_blocks(pnor_addr) & 0xFF);
212 rsp_buffer_push(rsp, bytes_to_blocks(pnor_addr) >> 8);
213 rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) & 0xFF);
214 rsp_buffer_push(rsp, bytes_to_blocks(pnor_size) >> 8);
215 rsp_buffer_push(rsp, 0x00); /* offset */
216 rsp_buffer_push(rsp, 0x00); /* offset */
217 break;
219 case HIOMAP_C_CLOSE_WINDOW:
220 memory_region_set_enabled(&pnor->mmio, false);
221 break;
223 case HIOMAP_C_DEVICE_NAME:
224 case HIOMAP_C_RESET:
225 case HIOMAP_C_LOCK:
226 default:
227 qemu_log_mask(LOG_GUEST_ERROR, "HIOMAP: unknow command %02X\n", cmd[2]);
228 break;
232 #define HIOMAP 0x5a
234 static const IPMICmdHandler hiomap_cmds[] = {
235 [HIOMAP] = { hiomap_cmd, 3 },
238 static const IPMINetfn hiomap_netfn = {
239 .cmd_nums = ARRAY_SIZE(hiomap_cmds),
240 .cmd_handlers = hiomap_cmds
244 void pnv_bmc_set_pnor(IPMIBmc *bmc, PnvPnor *pnor)
246 object_ref(OBJECT(pnor));
247 object_property_add_const_link(OBJECT(bmc), "pnor", OBJECT(pnor));
249 /* Install the HIOMAP protocol handlers to access the PNOR */
250 ipmi_sim_register_netfn(IPMI_BMC_SIMULATOR(bmc), IPMI_NETFN_OEM,
251 &hiomap_netfn);
255 * Instantiate the machine BMC. PowerNV uses the QEMU internal
256 * simulator but it could also be external.
258 IPMIBmc *pnv_bmc_create(PnvPnor *pnor)
260 Object *obj;
262 obj = object_new(TYPE_IPMI_BMC_SIMULATOR);
263 object_ref(OBJECT(pnor));
264 object_property_add_const_link(obj, "pnor", OBJECT(pnor));
265 qdev_realize(DEVICE(obj), NULL, &error_fatal);
267 /* Install the HIOMAP protocol handlers to access the PNOR */
268 ipmi_sim_register_netfn(IPMI_BMC_SIMULATOR(obj), IPMI_NETFN_OEM,
269 &hiomap_netfn);
271 return IPMI_BMC(obj);
274 typedef struct ForeachArgs {
275 const char *name;
276 Object *obj;
277 } ForeachArgs;
279 static int bmc_find(Object *child, void *opaque)
281 ForeachArgs *args = opaque;
283 if (object_dynamic_cast(child, args->name)) {
284 if (args->obj) {
285 return 1;
287 args->obj = child;
289 return 0;
292 IPMIBmc *pnv_bmc_find(Error **errp)
294 ForeachArgs args = { TYPE_IPMI_BMC_SIMULATOR, NULL };
295 int ret;
297 ret = object_child_foreach_recursive(object_get_root(), bmc_find, &args);
298 if (ret) {
299 error_setg(errp, "machine should have only one BMC device. "
300 "Use '-nodefaults'");
301 return NULL;
304 return args.obj ? IPMI_BMC(args.obj) : NULL;