hw/watchdog: Implement full i.MX watchdog support
[qemu/kevin.git] / hw / acpi / ghes.c
blobb363bc331d0468092814667630bf0822f4b01c0d
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;
209 * Check whether it will run out of the preallocated memory if adding a new
210 * generic error data entry
212 if ((data_length + ACPI_GHES_GESB_SIZE) > ACPI_GHES_MAX_RAW_DATA_LENGTH) {
213 error_report("Not enough memory to record new CPER!!!");
214 g_array_free(block, true);
215 return -1;
218 /* Build the new generic error status block header */
219 acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
220 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
222 /* Build this new generic error data entry header */
223 acpi_ghes_generic_error_data(block, uefi_cper_mem_sec,
224 ACPI_CPER_SEV_RECOVERABLE, 0, 0,
225 ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
227 /* Build the memory section CPER for above new generic error data entry */
228 acpi_ghes_build_append_mem_cper(block, error_physical_addr);
230 /* Write the generic error data entry into guest memory */
231 cpu_physical_memory_write(error_block_address, block->data, block->len);
233 g_array_free(block, true);
235 return 0;
239 * Build table for the hardware error fw_cfg blob.
240 * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
241 * See docs/specs/acpi_hest_ghes.rst for blobs format.
243 void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
245 int i, error_status_block_offset;
247 /* Build error_block_address */
248 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
249 build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
252 /* Build read_ack_register */
253 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
255 * Initialize the value of read_ack_register to 1, so GHES can be
256 * writeable after (re)boot.
257 * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
258 * (GHESv2 - Type 10)
260 build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
263 /* Generic Error Status Block offset in the hardware error fw_cfg blob */
264 error_status_block_offset = hardware_errors->len;
266 /* Reserve space for Error Status Data Block */
267 acpi_data_push(hardware_errors,
268 ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
270 /* Tell guest firmware to place hardware_errors blob into RAM */
271 bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
272 hardware_errors, sizeof(uint64_t), false);
274 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
276 * Tell firmware to patch error_block_address entries to point to
277 * corresponding "Generic Error Status Block"
279 bios_linker_loader_add_pointer(linker,
280 ACPI_GHES_ERRORS_FW_CFG_FILE, sizeof(uint64_t) * i,
281 sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
282 error_status_block_offset + i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
286 * tell firmware to write hardware_errors GPA into
287 * hardware_errors_addr fw_cfg, once the former has been initialized.
289 bios_linker_loader_write_pointer(linker, ACPI_GHES_DATA_ADDR_FW_CFG_FILE,
290 0, sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE, 0);
293 /* Build Generic Hardware Error Source version 2 (GHESv2) */
294 static void build_ghes_v2(GArray *table_data, int source_id, BIOSLinker *linker)
296 uint64_t address_offset;
298 * Type:
299 * Generic Hardware Error Source version 2(GHESv2 - Type 10)
301 build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
302 /* Source Id */
303 build_append_int_noprefix(table_data, source_id, 2);
304 /* Related Source Id */
305 build_append_int_noprefix(table_data, 0xffff, 2);
306 /* Flags */
307 build_append_int_noprefix(table_data, 0, 1);
308 /* Enabled */
309 build_append_int_noprefix(table_data, 1, 1);
311 /* Number of Records To Pre-allocate */
312 build_append_int_noprefix(table_data, 1, 4);
313 /* Max Sections Per Record */
314 build_append_int_noprefix(table_data, 1, 4);
315 /* Max Raw Data Length */
316 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
318 address_offset = table_data->len;
319 /* Error Status Address */
320 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
321 4 /* QWord access */, 0);
322 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
323 address_offset + GAS_ADDR_OFFSET, sizeof(uint64_t),
324 ACPI_GHES_ERRORS_FW_CFG_FILE, source_id * sizeof(uint64_t));
326 switch (source_id) {
327 case ACPI_HEST_SRC_ID_SEA:
329 * Notification Structure
330 * Now only enable ARMv8 SEA notification type
332 build_ghes_hw_error_notification(table_data, ACPI_GHES_NOTIFY_SEA);
333 break;
334 default:
335 error_report("Not support this error source");
336 abort();
339 /* Error Status Block Length */
340 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
343 * Read Ack Register
344 * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
345 * version 2 (GHESv2 - Type 10)
347 address_offset = table_data->len;
348 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
349 4 /* QWord access */, 0);
350 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
351 address_offset + GAS_ADDR_OFFSET,
352 sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
353 (ACPI_GHES_ERROR_SOURCE_COUNT + source_id) * sizeof(uint64_t));
356 * Read Ack Preserve field
357 * We only provide the first bit in Read Ack Register to OSPM to write
358 * while the other bits are preserved.
360 build_append_int_noprefix(table_data, ~0x1ULL, 8);
361 /* Read Ack Write */
362 build_append_int_noprefix(table_data, 0x1, 8);
365 /* Build Hardware Error Source Table */
366 void acpi_build_hest(GArray *table_data, BIOSLinker *linker)
368 uint64_t hest_start = table_data->len;
370 /* Hardware Error Source Table header*/
371 acpi_data_push(table_data, sizeof(AcpiTableHeader));
373 /* Error Source Count */
374 build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
376 build_ghes_v2(table_data, ACPI_HEST_SRC_ID_SEA, linker);
378 build_header(linker, table_data, (void *)(table_data->data + hest_start),
379 "HEST", table_data->len - hest_start, 1, NULL, NULL);
382 void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
383 GArray *hardware_error)
385 /* Create a read-only fw_cfg file for GHES */
386 fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
387 hardware_error->len);
389 /* Create a read-write fw_cfg file for Address */
390 fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
391 NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
394 int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
396 uint64_t error_block_addr, read_ack_register_addr, read_ack_register = 0;
397 uint64_t start_addr;
398 bool ret = -1;
399 AcpiGedState *acpi_ged_state;
400 AcpiGhesState *ags;
402 assert(source_id < ACPI_HEST_SRC_ID_RESERVED);
404 acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
405 NULL));
406 g_assert(acpi_ged_state);
407 ags = &acpi_ged_state->ghes_state;
409 start_addr = le64_to_cpu(ags->ghes_addr_le);
411 if (physical_address) {
413 if (source_id < ACPI_HEST_SRC_ID_RESERVED) {
414 start_addr += source_id * sizeof(uint64_t);
417 cpu_physical_memory_read(start_addr, &error_block_addr,
418 sizeof(error_block_addr));
420 error_block_addr = le64_to_cpu(error_block_addr);
422 read_ack_register_addr = start_addr +
423 ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
425 cpu_physical_memory_read(read_ack_register_addr,
426 &read_ack_register, sizeof(read_ack_register));
428 /* zero means OSPM does not acknowledge the error */
429 if (!read_ack_register) {
430 error_report("OSPM does not acknowledge previous error,"
431 " so can not record CPER for current error anymore");
432 } else if (error_block_addr) {
433 read_ack_register = cpu_to_le64(0);
435 * Clear the Read Ack Register, OSPM will write it to 1 when
436 * it acknowledges this error.
438 cpu_physical_memory_write(read_ack_register_addr,
439 &read_ack_register, sizeof(uint64_t));
441 ret = acpi_ghes_record_mem_error(error_block_addr,
442 physical_address);
443 } else
444 error_report("can not find Generic Error Status Block");
447 return ret;