xref: /openbmc/qemu/hw/acpi/ghes.c (revision 5eb07a4ff067053b4d6bf55d2d614b65b00a476b)
1 /*
2  * Support for generating APEI tables and recording CPER for Guests
3  *
4  * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO., LTD.
5  *
6  * Author: Dongjiu Geng <gengdongjiu@huawei.com>
7  *
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.
12 
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.
17 
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/>.
20  */
21 
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"
30 
31 #define ACPI_GHES_ERRORS_FW_CFG_FILE        "etc/hardware_errors"
32 #define ACPI_GHES_DATA_ADDR_FW_CFG_FILE     "etc/hardware_errors_addr"
33 
34 /* The max size in bytes for one error block */
35 #define ACPI_GHES_MAX_RAW_DATA_LENGTH   (1 * KiB)
36 
37 /* Generic Hardware Error Source version 2 */
38 #define ACPI_GHES_SOURCE_GENERIC_ERROR_V2   10
39 
40 /* Address offset in Generic Address Structure(GAS) */
41 #define GAS_ADDR_OFFSET 4
42 
43 /*
44  * The total size of Generic Error Data Entry
45  * ACPI 6.1/6.2: 18.3.2.7.1 Generic Error Data,
46  * Table 18-343 Generic Error Data Entry
47  */
48 #define ACPI_GHES_DATA_LENGTH               72
49 
50 /* The memory section CPER size, UEFI 2.6: N.2.5 Memory Error Section */
51 #define ACPI_GHES_MEM_CPER_LENGTH           80
52 
53 /* Masks for block_status flags */
54 #define ACPI_GEBS_UNCORRECTABLE         1
55 
56 /*
57  * Total size for Generic Error Status Block except Generic Error Data Entries
58  * ACPI 6.2: 18.3.2.7.1 Generic Error Data,
59  * Table 18-380 Generic Error Status Block
60  */
61 #define ACPI_GHES_GESB_SIZE                 20
62 
63 /*
64  * Values for error_severity field
65  */
66 enum AcpiGenericErrorSeverity {
67     ACPI_CPER_SEV_RECOVERABLE = 0,
68     ACPI_CPER_SEV_FATAL = 1,
69     ACPI_CPER_SEV_CORRECTED = 2,
70     ACPI_CPER_SEV_NONE = 3,
71 };
72 
73 /*
74  * Hardware Error Notification
75  * ACPI 4.0: 17.3.2.7 Hardware Error Notification
76  * Composes dummy Hardware Error Notification descriptor of specified type
77  */
78 static void build_ghes_hw_error_notification(GArray *table, const uint8_t type)
79 {
80     /* Type */
81     build_append_int_noprefix(table, type, 1);
82     /*
83      * Length:
84      * Total length of the structure in bytes
85      */
86     build_append_int_noprefix(table, 28, 1);
87     /* Configuration Write Enable */
88     build_append_int_noprefix(table, 0, 2);
89     /* Poll Interval */
90     build_append_int_noprefix(table, 0, 4);
91     /* Vector */
92     build_append_int_noprefix(table, 0, 4);
93     /* Switch To Polling Threshold Value */
94     build_append_int_noprefix(table, 0, 4);
95     /* Switch To Polling Threshold Window */
96     build_append_int_noprefix(table, 0, 4);
97     /* Error Threshold Value */
98     build_append_int_noprefix(table, 0, 4);
99     /* Error Threshold Window */
100     build_append_int_noprefix(table, 0, 4);
101 }
102 
103 /*
104  * Generic Error Data Entry
105  * ACPI 6.1: 18.3.2.7.1 Generic Error Data
106  */
107 static void acpi_ghes_generic_error_data(GArray *table,
108                 const uint8_t *section_type, uint32_t error_severity,
109                 uint8_t validation_bits, uint8_t flags,
110                 uint32_t error_data_length, QemuUUID fru_id,
111                 uint64_t time_stamp)
112 {
113     const uint8_t fru_text[20] = {0};
114 
115     /* Section Type */
116     g_array_append_vals(table, section_type, 16);
117 
118     /* Error Severity */
119     build_append_int_noprefix(table, error_severity, 4);
120     /* Revision */
121     build_append_int_noprefix(table, 0x300, 2);
122     /* Validation Bits */
123     build_append_int_noprefix(table, validation_bits, 1);
124     /* Flags */
125     build_append_int_noprefix(table, flags, 1);
126     /* Error Data Length */
127     build_append_int_noprefix(table, error_data_length, 4);
128 
129     /* FRU Id */
130     g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data));
131 
132     /* FRU Text */
133     g_array_append_vals(table, fru_text, sizeof(fru_text));
134 
135     /* Timestamp */
136     build_append_int_noprefix(table, time_stamp, 8);
137 }
138 
139 /*
140  * Generic Error Status Block
141  * ACPI 6.1: 18.3.2.7.1 Generic Error Data
142  */
143 static void acpi_ghes_generic_error_status(GArray *table, uint32_t block_status,
144                 uint32_t raw_data_offset, uint32_t raw_data_length,
145                 uint32_t data_length, uint32_t error_severity)
146 {
147     /* Block Status */
148     build_append_int_noprefix(table, block_status, 4);
149     /* Raw Data Offset */
150     build_append_int_noprefix(table, raw_data_offset, 4);
151     /* Raw Data Length */
152     build_append_int_noprefix(table, raw_data_length, 4);
153     /* Data Length */
154     build_append_int_noprefix(table, data_length, 4);
155     /* Error Severity */
156     build_append_int_noprefix(table, error_severity, 4);
157 }
158 
159 /* UEFI 2.6: N.2.5 Memory Error Section */
160 static void acpi_ghes_build_append_mem_cper(GArray *table,
161                                             uint64_t error_physical_addr)
162 {
163     /*
164      * Memory Error Record
165      */
166 
167     /* Validation Bits */
168     build_append_int_noprefix(table,
169                               (1ULL << 14) | /* Type Valid */
170                               (1ULL << 1) /* Physical Address Valid */,
171                               8);
172     /* Error Status */
173     build_append_int_noprefix(table, 0, 8);
174     /* Physical Address */
175     build_append_int_noprefix(table, error_physical_addr, 8);
176     /* Skip all the detailed information normally found in such a record */
177     build_append_int_noprefix(table, 0, 48);
178     /* Memory Error Type */
179     build_append_int_noprefix(table, 0 /* Unknown error */, 1);
180     /* Skip all the detailed information normally found in such a record */
181     build_append_int_noprefix(table, 0, 7);
182 }
183 
184 static int acpi_ghes_record_mem_error(uint64_t error_block_address,
185                                       uint64_t error_physical_addr)
186 {
187     GArray *block;
188 
189     /* Memory Error Section Type */
190     const uint8_t uefi_cper_mem_sec[] =
191           UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
192                   0xED, 0x7C, 0x83, 0xB1);
193 
194     /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data,
195      * Table 17-13 Generic Error Data Entry
196      */
197     QemuUUID fru_id = {};
198     uint32_t data_length;
199 
200     block = g_array_new(false, true /* clear */, 1);
201 
202     /* This is the length if adding a new generic error data entry*/
203     data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
204     /*
205      * It should not run out of the preallocated memory if adding a new generic
206      * error data entry
207      */
208     assert((data_length + ACPI_GHES_GESB_SIZE) <=
209             ACPI_GHES_MAX_RAW_DATA_LENGTH);
210 
211     /* Build the new generic error status block header */
212     acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
213         0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
214 
215     /* Build this new generic error data entry header */
216     acpi_ghes_generic_error_data(block, uefi_cper_mem_sec,
217         ACPI_CPER_SEV_RECOVERABLE, 0, 0,
218         ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
219 
220     /* Build the memory section CPER for above new generic error data entry */
221     acpi_ghes_build_append_mem_cper(block, error_physical_addr);
222 
223     /* Write the generic error data entry into guest memory */
224     cpu_physical_memory_write(error_block_address, block->data, block->len);
225 
226     g_array_free(block, true);
227 
228     return 0;
229 }
230 
231 /*
232  * Build table for the hardware error fw_cfg blob.
233  * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
234  * See docs/specs/acpi_hest_ghes.rst for blobs format.
235  */
236 static void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
237 {
238     int i, error_status_block_offset;
239 
240     /* Build error_block_address */
241     for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
242         build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
243     }
244 
245     /* Build read_ack_register */
246     for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
247         /*
248          * Initialize the value of read_ack_register to 1, so GHES can be
249          * writable after (re)boot.
250          * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
251          * (GHESv2 - Type 10)
252          */
253         build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
254     }
255 
256     /* Generic Error Status Block offset in the hardware error fw_cfg blob */
257     error_status_block_offset = hardware_errors->len;
258 
259     /* Reserve space for Error Status Data Block */
260     acpi_data_push(hardware_errors,
261         ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
262 
263     /* Tell guest firmware to place hardware_errors blob into RAM */
264     bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
265                              hardware_errors, sizeof(uint64_t), false);
266 
267     for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
268         /*
269          * Tell firmware to patch error_block_address entries to point to
270          * corresponding "Generic Error Status Block"
271          */
272         bios_linker_loader_add_pointer(linker,
273             ACPI_GHES_ERRORS_FW_CFG_FILE, sizeof(uint64_t) * i,
274             sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
275             error_status_block_offset + i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
276     }
277 
278     /*
279      * tell firmware to write hardware_errors GPA into
280      * hardware_errors_addr fw_cfg, once the former has been initialized.
281      */
282     bios_linker_loader_write_pointer(linker, ACPI_GHES_DATA_ADDR_FW_CFG_FILE,
283         0, sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE, 0);
284 }
285 
286 /* Build Generic Hardware Error Source version 2 (GHESv2) */
287 static void build_ghes_v2(GArray *table_data,
288                           BIOSLinker *linker,
289                           enum AcpiGhesNotifyType notify,
290                           uint16_t source_id)
291 {
292     uint64_t address_offset;
293 
294     /*
295      * Type:
296      * Generic Hardware Error Source version 2(GHESv2 - Type 10)
297      */
298     build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
299     /* Source Id */
300     build_append_int_noprefix(table_data, source_id, 2);
301     /* Related Source Id */
302     build_append_int_noprefix(table_data, 0xffff, 2);
303     /* Flags */
304     build_append_int_noprefix(table_data, 0, 1);
305     /* Enabled */
306     build_append_int_noprefix(table_data, 1, 1);
307 
308     /* Number of Records To Pre-allocate */
309     build_append_int_noprefix(table_data, 1, 4);
310     /* Max Sections Per Record */
311     build_append_int_noprefix(table_data, 1, 4);
312     /* Max Raw Data Length */
313     build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
314 
315     address_offset = table_data->len;
316     /* Error Status Address */
317     build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
318                      4 /* QWord access */, 0);
319     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
320         address_offset + GAS_ADDR_OFFSET, sizeof(uint64_t),
321         ACPI_GHES_ERRORS_FW_CFG_FILE, source_id * sizeof(uint64_t));
322 
323     /* Notification Structure */
324     build_ghes_hw_error_notification(table_data, notify);
325 
326     /* Error Status Block Length */
327     build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
328 
329     /*
330      * Read Ack Register
331      * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
332      * version 2 (GHESv2 - Type 10)
333      */
334     address_offset = table_data->len;
335     build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
336                      4 /* QWord access */, 0);
337     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
338         address_offset + GAS_ADDR_OFFSET,
339         sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
340         (ACPI_GHES_ERROR_SOURCE_COUNT + source_id) * sizeof(uint64_t));
341 
342     /*
343      * Read Ack Preserve field
344      * We only provide the first bit in Read Ack Register to OSPM to write
345      * while the other bits are preserved.
346      */
347     build_append_int_noprefix(table_data, ~0x1ULL, 8);
348     /* Read Ack Write */
349     build_append_int_noprefix(table_data, 0x1, 8);
350 }
351 
352 /* Build Hardware Error Source Table */
353 void acpi_build_hest(GArray *table_data, GArray *hardware_errors,
354                      BIOSLinker *linker,
355                      const char *oem_id, const char *oem_table_id)
356 {
357     AcpiTable table = { .sig = "HEST", .rev = 1,
358                         .oem_id = oem_id, .oem_table_id = oem_table_id };
359 
360     build_ghes_error_table(hardware_errors, linker);
361 
362     acpi_table_begin(&table, table_data);
363 
364     /* Error Source Count */
365     build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
366     build_ghes_v2(table_data, linker,
367                   ACPI_GHES_NOTIFY_SEA, ACPI_HEST_SRC_ID_SEA);
368 
369     acpi_table_end(linker, &table);
370 }
371 
372 void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
373                           GArray *hardware_error)
374 {
375     /* Create a read-only fw_cfg file for GHES */
376     fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
377                     hardware_error->len);
378 
379     /* Create a read-write fw_cfg file for Address */
380     fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
381         NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
382 
383     ags->present = true;
384 }
385 
386 int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
387 {
388     uint64_t error_block_addr, read_ack_register_addr, read_ack_register = 0;
389     uint64_t start_addr;
390     bool ret = -1;
391     AcpiGedState *acpi_ged_state;
392     AcpiGhesState *ags;
393 
394     assert(source_id < ACPI_GHES_ERROR_SOURCE_COUNT);
395 
396     acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
397                                                        NULL));
398     g_assert(acpi_ged_state);
399     ags = &acpi_ged_state->ghes_state;
400 
401     start_addr = le64_to_cpu(ags->ghes_addr_le);
402 
403     if (!physical_address) {
404         return -1;
405     }
406 
407     if (source_id < ACPI_GHES_ERROR_SOURCE_COUNT) {
408         start_addr += source_id * sizeof(uint64_t);
409     }
410 
411     cpu_physical_memory_read(start_addr, &error_block_addr,
412                              sizeof(error_block_addr));
413 
414     error_block_addr = le64_to_cpu(error_block_addr);
415 
416     read_ack_register_addr = start_addr +
417                              ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
418 
419     cpu_physical_memory_read(read_ack_register_addr,
420                              &read_ack_register, sizeof(read_ack_register));
421 
422     /* zero means OSPM does not acknowledge the error */
423     if (!read_ack_register) {
424         error_report("OSPM does not acknowledge previous error,"
425                      " so can not record CPER for current error anymore");
426     } else if (error_block_addr) {
427         read_ack_register = cpu_to_le64(0);
428         /*
429          * Clear the Read Ack Register, OSPM will write it to 1 when
430          * it acknowledges this error.
431          */
432         cpu_physical_memory_write(read_ack_register_addr,
433                                   &read_ack_register, sizeof(uint64_t));
434 
435         ret = acpi_ghes_record_mem_error(error_block_addr,
436                                          physical_address);
437     } else {
438         error_report("can not find Generic Error Status Block");
439     }
440 
441     return ret;
442 }
443 
444 bool acpi_ghes_present(void)
445 {
446     AcpiGedState *acpi_ged_state;
447     AcpiGhesState *ags;
448 
449     acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
450                                                        NULL));
451 
452     if (!acpi_ged_state) {
453         return false;
454     }
455     ags = &acpi_ged_state->ghes_state;
456     return ags->present;
457 }
458