hw/elf_ops: Fix a typo
[qemu/ar7.git] / hw / acpi / ghes.c
bloba4dac6bf15ea8e3dbd49730ca7ea8441307ae0b7
1 /*
2 * Support for generating APEI tables and recording CPER for Guests
4 * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO., LTD.
6 * Author: Dongjiu Geng <gengdongjiu@huawei.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu/units.h"
24 #include "hw/acpi/ghes.h"
25 #include "hw/acpi/aml-build.h"
26 #include "qemu/error-report.h"
27 #include "hw/acpi/generic_event_device.h"
28 #include "hw/nvram/fw_cfg.h"
29 #include "qemu/uuid.h"
31 #define ACPI_GHES_ERRORS_FW_CFG_FILE "etc/hardware_errors"
32 #define ACPI_GHES_DATA_ADDR_FW_CFG_FILE "etc/hardware_errors_addr"
34 /* The max size in bytes for one error block */
35 #define ACPI_GHES_MAX_RAW_DATA_LENGTH (1 * KiB)
37 /* Now only support ARMv8 SEA notification type error source */
38 #define ACPI_GHES_ERROR_SOURCE_COUNT 1
40 /* Generic Hardware Error Source version 2 */
41 #define ACPI_GHES_SOURCE_GENERIC_ERROR_V2 10
43 /* Address offset in Generic Address Structure(GAS) */
44 #define GAS_ADDR_OFFSET 4
47 * The total size of Generic Error Data Entry
48 * ACPI 6.1/6.2: 18.3.2.7.1 Generic Error Data,
49 * Table 18-343 Generic Error Data Entry
51 #define ACPI_GHES_DATA_LENGTH 72
53 /* The memory section CPER size, UEFI 2.6: N.2.5 Memory Error Section */
54 #define ACPI_GHES_MEM_CPER_LENGTH 80
56 /* Masks for block_status flags */
57 #define ACPI_GEBS_UNCORRECTABLE 1
60 * Total size for Generic Error Status Block except Generic Error Data Entries
61 * ACPI 6.2: 18.3.2.7.1 Generic Error Data,
62 * Table 18-380 Generic Error Status Block
64 #define ACPI_GHES_GESB_SIZE 20
67 * Values for error_severity field
69 enum AcpiGenericErrorSeverity {
70 ACPI_CPER_SEV_RECOVERABLE = 0,
71 ACPI_CPER_SEV_FATAL = 1,
72 ACPI_CPER_SEV_CORRECTED = 2,
73 ACPI_CPER_SEV_NONE = 3,
77 * Hardware Error Notification
78 * ACPI 4.0: 17.3.2.7 Hardware Error Notification
79 * Composes dummy Hardware Error Notification descriptor of specified type
81 static void build_ghes_hw_error_notification(GArray *table, const uint8_t type)
83 /* Type */
84 build_append_int_noprefix(table, type, 1);
86 * Length:
87 * Total length of the structure in bytes
89 build_append_int_noprefix(table, 28, 1);
90 /* Configuration Write Enable */
91 build_append_int_noprefix(table, 0, 2);
92 /* Poll Interval */
93 build_append_int_noprefix(table, 0, 4);
94 /* Vector */
95 build_append_int_noprefix(table, 0, 4);
96 /* Switch To Polling Threshold Value */
97 build_append_int_noprefix(table, 0, 4);
98 /* Switch To Polling Threshold Window */
99 build_append_int_noprefix(table, 0, 4);
100 /* Error Threshold Value */
101 build_append_int_noprefix(table, 0, 4);
102 /* Error Threshold Window */
103 build_append_int_noprefix(table, 0, 4);
107 * Generic Error Data Entry
108 * ACPI 6.1: 18.3.2.7.1 Generic Error Data
110 static void acpi_ghes_generic_error_data(GArray *table,
111 const uint8_t *section_type, uint32_t error_severity,
112 uint8_t validation_bits, uint8_t flags,
113 uint32_t error_data_length, QemuUUID fru_id,
114 uint64_t time_stamp)
116 const uint8_t fru_text[20] = {0};
118 /* Section Type */
119 g_array_append_vals(table, section_type, 16);
121 /* Error Severity */
122 build_append_int_noprefix(table, error_severity, 4);
123 /* Revision */
124 build_append_int_noprefix(table, 0x300, 2);
125 /* Validation Bits */
126 build_append_int_noprefix(table, validation_bits, 1);
127 /* Flags */
128 build_append_int_noprefix(table, flags, 1);
129 /* Error Data Length */
130 build_append_int_noprefix(table, error_data_length, 4);
132 /* FRU Id */
133 g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data));
135 /* FRU Text */
136 g_array_append_vals(table, fru_text, sizeof(fru_text));
138 /* Timestamp */
139 build_append_int_noprefix(table, time_stamp, 8);
143 * Generic Error Status Block
144 * ACPI 6.1: 18.3.2.7.1 Generic Error Data
146 static void acpi_ghes_generic_error_status(GArray *table, uint32_t block_status,
147 uint32_t raw_data_offset, uint32_t raw_data_length,
148 uint32_t data_length, uint32_t error_severity)
150 /* Block Status */
151 build_append_int_noprefix(table, block_status, 4);
152 /* Raw Data Offset */
153 build_append_int_noprefix(table, raw_data_offset, 4);
154 /* Raw Data Length */
155 build_append_int_noprefix(table, raw_data_length, 4);
156 /* Data Length */
157 build_append_int_noprefix(table, data_length, 4);
158 /* Error Severity */
159 build_append_int_noprefix(table, error_severity, 4);
162 /* UEFI 2.6: N.2.5 Memory Error Section */
163 static void acpi_ghes_build_append_mem_cper(GArray *table,
164 uint64_t error_physical_addr)
167 * Memory Error Record
170 /* Validation Bits */
171 build_append_int_noprefix(table,
172 (1ULL << 14) | /* Type Valid */
173 (1ULL << 1) /* Physical Address Valid */,
175 /* Error Status */
176 build_append_int_noprefix(table, 0, 8);
177 /* Physical Address */
178 build_append_int_noprefix(table, error_physical_addr, 8);
179 /* Skip all the detailed information normally found in such a record */
180 build_append_int_noprefix(table, 0, 48);
181 /* Memory Error Type */
182 build_append_int_noprefix(table, 0 /* Unknown error */, 1);
183 /* Skip all the detailed information normally found in such a record */
184 build_append_int_noprefix(table, 0, 7);
187 static int acpi_ghes_record_mem_error(uint64_t error_block_address,
188 uint64_t error_physical_addr)
190 GArray *block;
192 /* Memory Error Section Type */
193 const uint8_t uefi_cper_mem_sec[] =
194 UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
195 0xED, 0x7C, 0x83, 0xB1);
197 /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data,
198 * Table 17-13 Generic Error Data Entry
200 QemuUUID fru_id = {};
201 uint32_t data_length;
203 block = g_array_new(false, true /* clear */, 1);
205 /* This is the length if adding a new generic error data entry*/
206 data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
208 * It should not run out of the preallocated memory if adding a new generic
209 * error data entry
211 assert((data_length + ACPI_GHES_GESB_SIZE) <=
212 ACPI_GHES_MAX_RAW_DATA_LENGTH);
214 /* Build the new generic error status block header */
215 acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
216 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
218 /* Build this new generic error data entry header */
219 acpi_ghes_generic_error_data(block, uefi_cper_mem_sec,
220 ACPI_CPER_SEV_RECOVERABLE, 0, 0,
221 ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
223 /* Build the memory section CPER for above new generic error data entry */
224 acpi_ghes_build_append_mem_cper(block, error_physical_addr);
226 /* Write the generic error data entry into guest memory */
227 cpu_physical_memory_write(error_block_address, block->data, block->len);
229 g_array_free(block, true);
231 return 0;
235 * Build table for the hardware error fw_cfg blob.
236 * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
237 * See docs/specs/acpi_hest_ghes.rst for blobs format.
239 void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
241 int i, error_status_block_offset;
243 /* Build error_block_address */
244 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
245 build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
248 /* Build read_ack_register */
249 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
251 * Initialize the value of read_ack_register to 1, so GHES can be
252 * writeable after (re)boot.
253 * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
254 * (GHESv2 - Type 10)
256 build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
259 /* Generic Error Status Block offset in the hardware error fw_cfg blob */
260 error_status_block_offset = hardware_errors->len;
262 /* Reserve space for Error Status Data Block */
263 acpi_data_push(hardware_errors,
264 ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
266 /* Tell guest firmware to place hardware_errors blob into RAM */
267 bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
268 hardware_errors, sizeof(uint64_t), false);
270 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
272 * Tell firmware to patch error_block_address entries to point to
273 * corresponding "Generic Error Status Block"
275 bios_linker_loader_add_pointer(linker,
276 ACPI_GHES_ERRORS_FW_CFG_FILE, sizeof(uint64_t) * i,
277 sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
278 error_status_block_offset + i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
282 * tell firmware to write hardware_errors GPA into
283 * hardware_errors_addr fw_cfg, once the former has been initialized.
285 bios_linker_loader_write_pointer(linker, ACPI_GHES_DATA_ADDR_FW_CFG_FILE,
286 0, sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE, 0);
289 /* Build Generic Hardware Error Source version 2 (GHESv2) */
290 static void build_ghes_v2(GArray *table_data, int source_id, BIOSLinker *linker)
292 uint64_t address_offset;
294 * Type:
295 * Generic Hardware Error Source version 2(GHESv2 - Type 10)
297 build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
298 /* Source Id */
299 build_append_int_noprefix(table_data, source_id, 2);
300 /* Related Source Id */
301 build_append_int_noprefix(table_data, 0xffff, 2);
302 /* Flags */
303 build_append_int_noprefix(table_data, 0, 1);
304 /* Enabled */
305 build_append_int_noprefix(table_data, 1, 1);
307 /* Number of Records To Pre-allocate */
308 build_append_int_noprefix(table_data, 1, 4);
309 /* Max Sections Per Record */
310 build_append_int_noprefix(table_data, 1, 4);
311 /* Max Raw Data Length */
312 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
314 address_offset = table_data->len;
315 /* Error Status Address */
316 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
317 4 /* QWord access */, 0);
318 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
319 address_offset + GAS_ADDR_OFFSET, sizeof(uint64_t),
320 ACPI_GHES_ERRORS_FW_CFG_FILE, source_id * sizeof(uint64_t));
322 switch (source_id) {
323 case ACPI_HEST_SRC_ID_SEA:
325 * Notification Structure
326 * Now only enable ARMv8 SEA notification type
328 build_ghes_hw_error_notification(table_data, ACPI_GHES_NOTIFY_SEA);
329 break;
330 default:
331 error_report("Not support this error source");
332 abort();
335 /* Error Status Block Length */
336 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
339 * Read Ack Register
340 * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
341 * version 2 (GHESv2 - Type 10)
343 address_offset = table_data->len;
344 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
345 4 /* QWord access */, 0);
346 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
347 address_offset + GAS_ADDR_OFFSET,
348 sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
349 (ACPI_GHES_ERROR_SOURCE_COUNT + source_id) * sizeof(uint64_t));
352 * Read Ack Preserve field
353 * We only provide the first bit in Read Ack Register to OSPM to write
354 * while the other bits are preserved.
356 build_append_int_noprefix(table_data, ~0x1ULL, 8);
357 /* Read Ack Write */
358 build_append_int_noprefix(table_data, 0x1, 8);
361 /* Build Hardware Error Source Table */
362 void acpi_build_hest(GArray *table_data, BIOSLinker *linker,
363 const char *oem_id, const char *oem_table_id)
365 uint64_t hest_start = table_data->len;
367 /* Hardware Error Source Table header*/
368 acpi_data_push(table_data, sizeof(AcpiTableHeader));
370 /* Error Source Count */
371 build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
373 build_ghes_v2(table_data, ACPI_HEST_SRC_ID_SEA, linker);
375 build_header(linker, table_data, (void *)(table_data->data + hest_start),
376 "HEST", table_data->len - hest_start, 1, oem_id, oem_table_id);
379 void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
380 GArray *hardware_error)
382 /* Create a read-only fw_cfg file for GHES */
383 fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
384 hardware_error->len);
386 /* Create a read-write fw_cfg file for Address */
387 fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
388 NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
391 int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
393 uint64_t error_block_addr, read_ack_register_addr, read_ack_register = 0;
394 uint64_t start_addr;
395 bool ret = -1;
396 AcpiGedState *acpi_ged_state;
397 AcpiGhesState *ags;
399 assert(source_id < ACPI_HEST_SRC_ID_RESERVED);
401 acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
402 NULL));
403 g_assert(acpi_ged_state);
404 ags = &acpi_ged_state->ghes_state;
406 start_addr = le64_to_cpu(ags->ghes_addr_le);
408 if (physical_address) {
410 if (source_id < ACPI_HEST_SRC_ID_RESERVED) {
411 start_addr += source_id * sizeof(uint64_t);
414 cpu_physical_memory_read(start_addr, &error_block_addr,
415 sizeof(error_block_addr));
417 error_block_addr = le64_to_cpu(error_block_addr);
419 read_ack_register_addr = start_addr +
420 ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
422 cpu_physical_memory_read(read_ack_register_addr,
423 &read_ack_register, sizeof(read_ack_register));
425 /* zero means OSPM does not acknowledge the error */
426 if (!read_ack_register) {
427 error_report("OSPM does not acknowledge previous error,"
428 " so can not record CPER for current error anymore");
429 } else if (error_block_addr) {
430 read_ack_register = cpu_to_le64(0);
432 * Clear the Read Ack Register, OSPM will write it to 1 when
433 * it acknowledges this error.
435 cpu_physical_memory_write(read_ack_register_addr,
436 &read_ack_register, sizeof(uint64_t));
438 ret = acpi_ghes_record_mem_error(error_block_addr,
439 physical_address);
440 } else
441 error_report("can not find Generic Error Status Block");
444 return ret;