xref: /openbmc/qemu/hw/acpi/ghes.c (revision 028f2361d0c2d28d6f918fe618f389228ac22b60)
1aa16508fSDongjiu Geng /*
2aa16508fSDongjiu Geng  * Support for generating APEI tables and recording CPER for Guests
3aa16508fSDongjiu Geng  *
4aa16508fSDongjiu Geng  * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO., LTD.
5aa16508fSDongjiu Geng  *
6aa16508fSDongjiu Geng  * Author: Dongjiu Geng <gengdongjiu@huawei.com>
7aa16508fSDongjiu Geng  *
8aa16508fSDongjiu Geng  * This program is free software; you can redistribute it and/or modify
9aa16508fSDongjiu Geng  * it under the terms of the GNU General Public License as published by
10aa16508fSDongjiu Geng  * the Free Software Foundation; either version 2 of the License, or
11aa16508fSDongjiu Geng  * (at your option) any later version.
12aa16508fSDongjiu Geng 
13aa16508fSDongjiu Geng  * This program is distributed in the hope that it will be useful,
14aa16508fSDongjiu Geng  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15aa16508fSDongjiu Geng  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16aa16508fSDongjiu Geng  * GNU General Public License for more details.
17aa16508fSDongjiu Geng 
18aa16508fSDongjiu Geng  * You should have received a copy of the GNU General Public License along
19aa16508fSDongjiu Geng  * with this program; if not, see <http://www.gnu.org/licenses/>.
20aa16508fSDongjiu Geng  */
21aa16508fSDongjiu Geng 
22aa16508fSDongjiu Geng #include "qemu/osdep.h"
23aa16508fSDongjiu Geng #include "qemu/units.h"
24aa16508fSDongjiu Geng #include "hw/acpi/ghes.h"
25aa16508fSDongjiu Geng #include "hw/acpi/aml-build.h"
26205cc75dSDongjiu Geng #include "qemu/error-report.h"
27a08a6462SDongjiu Geng #include "hw/acpi/generic_event_device.h"
28a08a6462SDongjiu Geng #include "hw/nvram/fw_cfg.h"
29558b9d86SDongjiu Geng #include "qemu/uuid.h"
30aa16508fSDongjiu Geng 
31aa16508fSDongjiu Geng #define ACPI_GHES_ERRORS_FW_CFG_FILE        "etc/hardware_errors"
32aa16508fSDongjiu Geng #define ACPI_GHES_DATA_ADDR_FW_CFG_FILE     "etc/hardware_errors_addr"
33aa16508fSDongjiu Geng 
34aa16508fSDongjiu Geng /* The max size in bytes for one error block */
35aa16508fSDongjiu Geng #define ACPI_GHES_MAX_RAW_DATA_LENGTH   (1 * KiB)
36aa16508fSDongjiu Geng 
37aa16508fSDongjiu Geng /* Now only support ARMv8 SEA notification type error source */
38aa16508fSDongjiu Geng #define ACPI_GHES_ERROR_SOURCE_COUNT        1
39aa16508fSDongjiu Geng 
40205cc75dSDongjiu Geng /* Generic Hardware Error Source version 2 */
41205cc75dSDongjiu Geng #define ACPI_GHES_SOURCE_GENERIC_ERROR_V2   10
42205cc75dSDongjiu Geng 
43205cc75dSDongjiu Geng /* Address offset in Generic Address Structure(GAS) */
44205cc75dSDongjiu Geng #define GAS_ADDR_OFFSET 4
45205cc75dSDongjiu Geng 
46205cc75dSDongjiu Geng /*
47558b9d86SDongjiu Geng  * The total size of Generic Error Data Entry
48558b9d86SDongjiu Geng  * ACPI 6.1/6.2: 18.3.2.7.1 Generic Error Data,
49558b9d86SDongjiu Geng  * Table 18-343 Generic Error Data Entry
50558b9d86SDongjiu Geng  */
51558b9d86SDongjiu Geng #define ACPI_GHES_DATA_LENGTH               72
52558b9d86SDongjiu Geng 
53558b9d86SDongjiu Geng /* The memory section CPER size, UEFI 2.6: N.2.5 Memory Error Section */
54558b9d86SDongjiu Geng #define ACPI_GHES_MEM_CPER_LENGTH           80
55558b9d86SDongjiu Geng 
56558b9d86SDongjiu Geng /* Masks for block_status flags */
57558b9d86SDongjiu Geng #define ACPI_GEBS_UNCORRECTABLE         1
58558b9d86SDongjiu Geng 
59558b9d86SDongjiu Geng /*
60558b9d86SDongjiu Geng  * Total size for Generic Error Status Block except Generic Error Data Entries
61558b9d86SDongjiu Geng  * ACPI 6.2: 18.3.2.7.1 Generic Error Data,
62558b9d86SDongjiu Geng  * Table 18-380 Generic Error Status Block
63558b9d86SDongjiu Geng  */
64558b9d86SDongjiu Geng #define ACPI_GHES_GESB_SIZE                 20
65558b9d86SDongjiu Geng 
66558b9d86SDongjiu Geng /*
67558b9d86SDongjiu Geng  * Values for error_severity field
68558b9d86SDongjiu Geng  */
69558b9d86SDongjiu Geng enum AcpiGenericErrorSeverity {
70558b9d86SDongjiu Geng     ACPI_CPER_SEV_RECOVERABLE = 0,
71558b9d86SDongjiu Geng     ACPI_CPER_SEV_FATAL = 1,
72558b9d86SDongjiu Geng     ACPI_CPER_SEV_CORRECTED = 2,
73558b9d86SDongjiu Geng     ACPI_CPER_SEV_NONE = 3,
74558b9d86SDongjiu Geng };
75558b9d86SDongjiu Geng 
76558b9d86SDongjiu Geng /*
77205cc75dSDongjiu Geng  * Hardware Error Notification
78205cc75dSDongjiu Geng  * ACPI 4.0: 17.3.2.7 Hardware Error Notification
79205cc75dSDongjiu Geng  * Composes dummy Hardware Error Notification descriptor of specified type
80205cc75dSDongjiu Geng  */
build_ghes_hw_error_notification(GArray * table,const uint8_t type)81205cc75dSDongjiu Geng static void build_ghes_hw_error_notification(GArray *table, const uint8_t type)
82205cc75dSDongjiu Geng {
83205cc75dSDongjiu Geng     /* Type */
84205cc75dSDongjiu Geng     build_append_int_noprefix(table, type, 1);
85205cc75dSDongjiu Geng     /*
86205cc75dSDongjiu Geng      * Length:
87205cc75dSDongjiu Geng      * Total length of the structure in bytes
88205cc75dSDongjiu Geng      */
89205cc75dSDongjiu Geng     build_append_int_noprefix(table, 28, 1);
90205cc75dSDongjiu Geng     /* Configuration Write Enable */
91205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 2);
92205cc75dSDongjiu Geng     /* Poll Interval */
93205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 4);
94205cc75dSDongjiu Geng     /* Vector */
95205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 4);
96205cc75dSDongjiu Geng     /* Switch To Polling Threshold Value */
97205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 4);
98205cc75dSDongjiu Geng     /* Switch To Polling Threshold Window */
99205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 4);
100205cc75dSDongjiu Geng     /* Error Threshold Value */
101205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 4);
102205cc75dSDongjiu Geng     /* Error Threshold Window */
103205cc75dSDongjiu Geng     build_append_int_noprefix(table, 0, 4);
104205cc75dSDongjiu Geng }
105205cc75dSDongjiu Geng 
106aa16508fSDongjiu Geng /*
107558b9d86SDongjiu Geng  * Generic Error Data Entry
108558b9d86SDongjiu Geng  * ACPI 6.1: 18.3.2.7.1 Generic Error Data
109558b9d86SDongjiu Geng  */
acpi_ghes_generic_error_data(GArray * table,const uint8_t * section_type,uint32_t error_severity,uint8_t validation_bits,uint8_t flags,uint32_t error_data_length,QemuUUID fru_id,uint64_t time_stamp)110558b9d86SDongjiu Geng static void acpi_ghes_generic_error_data(GArray *table,
111558b9d86SDongjiu Geng                 const uint8_t *section_type, uint32_t error_severity,
112558b9d86SDongjiu Geng                 uint8_t validation_bits, uint8_t flags,
113558b9d86SDongjiu Geng                 uint32_t error_data_length, QemuUUID fru_id,
114558b9d86SDongjiu Geng                 uint64_t time_stamp)
115558b9d86SDongjiu Geng {
116558b9d86SDongjiu Geng     const uint8_t fru_text[20] = {0};
117558b9d86SDongjiu Geng 
118558b9d86SDongjiu Geng     /* Section Type */
119558b9d86SDongjiu Geng     g_array_append_vals(table, section_type, 16);
120558b9d86SDongjiu Geng 
121558b9d86SDongjiu Geng     /* Error Severity */
122558b9d86SDongjiu Geng     build_append_int_noprefix(table, error_severity, 4);
123558b9d86SDongjiu Geng     /* Revision */
124558b9d86SDongjiu Geng     build_append_int_noprefix(table, 0x300, 2);
125558b9d86SDongjiu Geng     /* Validation Bits */
126558b9d86SDongjiu Geng     build_append_int_noprefix(table, validation_bits, 1);
127558b9d86SDongjiu Geng     /* Flags */
128558b9d86SDongjiu Geng     build_append_int_noprefix(table, flags, 1);
129558b9d86SDongjiu Geng     /* Error Data Length */
130558b9d86SDongjiu Geng     build_append_int_noprefix(table, error_data_length, 4);
131558b9d86SDongjiu Geng 
132558b9d86SDongjiu Geng     /* FRU Id */
133558b9d86SDongjiu Geng     g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data));
134558b9d86SDongjiu Geng 
135558b9d86SDongjiu Geng     /* FRU Text */
136558b9d86SDongjiu Geng     g_array_append_vals(table, fru_text, sizeof(fru_text));
137558b9d86SDongjiu Geng 
138558b9d86SDongjiu Geng     /* Timestamp */
139558b9d86SDongjiu Geng     build_append_int_noprefix(table, time_stamp, 8);
140558b9d86SDongjiu Geng }
141558b9d86SDongjiu Geng 
142558b9d86SDongjiu Geng /*
143558b9d86SDongjiu Geng  * Generic Error Status Block
144558b9d86SDongjiu Geng  * ACPI 6.1: 18.3.2.7.1 Generic Error Data
145558b9d86SDongjiu Geng  */
acpi_ghes_generic_error_status(GArray * table,uint32_t block_status,uint32_t raw_data_offset,uint32_t raw_data_length,uint32_t data_length,uint32_t error_severity)146558b9d86SDongjiu Geng static void acpi_ghes_generic_error_status(GArray *table, uint32_t block_status,
147558b9d86SDongjiu Geng                 uint32_t raw_data_offset, uint32_t raw_data_length,
148558b9d86SDongjiu Geng                 uint32_t data_length, uint32_t error_severity)
149558b9d86SDongjiu Geng {
150558b9d86SDongjiu Geng     /* Block Status */
151558b9d86SDongjiu Geng     build_append_int_noprefix(table, block_status, 4);
152558b9d86SDongjiu Geng     /* Raw Data Offset */
153558b9d86SDongjiu Geng     build_append_int_noprefix(table, raw_data_offset, 4);
154558b9d86SDongjiu Geng     /* Raw Data Length */
155558b9d86SDongjiu Geng     build_append_int_noprefix(table, raw_data_length, 4);
156558b9d86SDongjiu Geng     /* Data Length */
157558b9d86SDongjiu Geng     build_append_int_noprefix(table, data_length, 4);
158558b9d86SDongjiu Geng     /* Error Severity */
159558b9d86SDongjiu Geng     build_append_int_noprefix(table, error_severity, 4);
160558b9d86SDongjiu Geng }
161558b9d86SDongjiu Geng 
162558b9d86SDongjiu Geng /* UEFI 2.6: N.2.5 Memory Error Section */
acpi_ghes_build_append_mem_cper(GArray * table,uint64_t error_physical_addr)163558b9d86SDongjiu Geng static void acpi_ghes_build_append_mem_cper(GArray *table,
164558b9d86SDongjiu Geng                                             uint64_t error_physical_addr)
165558b9d86SDongjiu Geng {
166558b9d86SDongjiu Geng     /*
167558b9d86SDongjiu Geng      * Memory Error Record
168558b9d86SDongjiu Geng      */
169558b9d86SDongjiu Geng 
170558b9d86SDongjiu Geng     /* Validation Bits */
171558b9d86SDongjiu Geng     build_append_int_noprefix(table,
172558b9d86SDongjiu Geng                               (1ULL << 14) | /* Type Valid */
173558b9d86SDongjiu Geng                               (1ULL << 1) /* Physical Address Valid */,
174558b9d86SDongjiu Geng                               8);
175558b9d86SDongjiu Geng     /* Error Status */
176558b9d86SDongjiu Geng     build_append_int_noprefix(table, 0, 8);
177558b9d86SDongjiu Geng     /* Physical Address */
178558b9d86SDongjiu Geng     build_append_int_noprefix(table, error_physical_addr, 8);
179558b9d86SDongjiu Geng     /* Skip all the detailed information normally found in such a record */
180558b9d86SDongjiu Geng     build_append_int_noprefix(table, 0, 48);
181558b9d86SDongjiu Geng     /* Memory Error Type */
182558b9d86SDongjiu Geng     build_append_int_noprefix(table, 0 /* Unknown error */, 1);
183558b9d86SDongjiu Geng     /* Skip all the detailed information normally found in such a record */
184558b9d86SDongjiu Geng     build_append_int_noprefix(table, 0, 7);
185558b9d86SDongjiu Geng }
186558b9d86SDongjiu Geng 
acpi_ghes_record_mem_error(uint64_t error_block_address,uint64_t error_physical_addr)187558b9d86SDongjiu Geng static int acpi_ghes_record_mem_error(uint64_t error_block_address,
188558b9d86SDongjiu Geng                                       uint64_t error_physical_addr)
189558b9d86SDongjiu Geng {
190558b9d86SDongjiu Geng     GArray *block;
191558b9d86SDongjiu Geng 
192558b9d86SDongjiu Geng     /* Memory Error Section Type */
193558b9d86SDongjiu Geng     const uint8_t uefi_cper_mem_sec[] =
194558b9d86SDongjiu Geng           UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
195558b9d86SDongjiu Geng                   0xED, 0x7C, 0x83, 0xB1);
196558b9d86SDongjiu Geng 
197558b9d86SDongjiu Geng     /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data,
198558b9d86SDongjiu Geng      * Table 17-13 Generic Error Data Entry
199558b9d86SDongjiu Geng      */
200558b9d86SDongjiu Geng     QemuUUID fru_id = {};
201558b9d86SDongjiu Geng     uint32_t data_length;
202558b9d86SDongjiu Geng 
203558b9d86SDongjiu Geng     block = g_array_new(false, true /* clear */, 1);
204558b9d86SDongjiu Geng 
205558b9d86SDongjiu Geng     /* This is the length if adding a new generic error data entry*/
206558b9d86SDongjiu Geng     data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
207558b9d86SDongjiu Geng     /*
2084bf7c0cbSDongjiu Geng      * It should not run out of the preallocated memory if adding a new generic
2094bf7c0cbSDongjiu Geng      * error data entry
210558b9d86SDongjiu Geng      */
2114bf7c0cbSDongjiu Geng     assert((data_length + ACPI_GHES_GESB_SIZE) <=
2124bf7c0cbSDongjiu Geng             ACPI_GHES_MAX_RAW_DATA_LENGTH);
213558b9d86SDongjiu Geng 
214558b9d86SDongjiu Geng     /* Build the new generic error status block header */
215558b9d86SDongjiu Geng     acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
216558b9d86SDongjiu Geng         0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
217558b9d86SDongjiu Geng 
218558b9d86SDongjiu Geng     /* Build this new generic error data entry header */
219558b9d86SDongjiu Geng     acpi_ghes_generic_error_data(block, uefi_cper_mem_sec,
220558b9d86SDongjiu Geng         ACPI_CPER_SEV_RECOVERABLE, 0, 0,
221558b9d86SDongjiu Geng         ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
222558b9d86SDongjiu Geng 
223558b9d86SDongjiu Geng     /* Build the memory section CPER for above new generic error data entry */
224558b9d86SDongjiu Geng     acpi_ghes_build_append_mem_cper(block, error_physical_addr);
225558b9d86SDongjiu Geng 
226558b9d86SDongjiu Geng     /* Write the generic error data entry into guest memory */
227558b9d86SDongjiu Geng     cpu_physical_memory_write(error_block_address, block->data, block->len);
228558b9d86SDongjiu Geng 
229558b9d86SDongjiu Geng     g_array_free(block, true);
230558b9d86SDongjiu Geng 
231558b9d86SDongjiu Geng     return 0;
232558b9d86SDongjiu Geng }
233558b9d86SDongjiu Geng 
234558b9d86SDongjiu Geng /*
235aa16508fSDongjiu Geng  * Build table for the hardware error fw_cfg blob.
236aa16508fSDongjiu Geng  * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
237aa16508fSDongjiu Geng  * See docs/specs/acpi_hest_ghes.rst for blobs format.
238aa16508fSDongjiu Geng  */
build_ghes_error_table(GArray * hardware_errors,BIOSLinker * linker)239aa16508fSDongjiu Geng void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
240aa16508fSDongjiu Geng {
241aa16508fSDongjiu Geng     int i, error_status_block_offset;
242aa16508fSDongjiu Geng 
243aa16508fSDongjiu Geng     /* Build error_block_address */
244aa16508fSDongjiu Geng     for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
245aa16508fSDongjiu Geng         build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
246aa16508fSDongjiu Geng     }
247aa16508fSDongjiu Geng 
248aa16508fSDongjiu Geng     /* Build read_ack_register */
249aa16508fSDongjiu Geng     for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
250aa16508fSDongjiu Geng         /*
251aa16508fSDongjiu Geng          * Initialize the value of read_ack_register to 1, so GHES can be
252*9323e79fSPeter Maydell          * writable after (re)boot.
253aa16508fSDongjiu Geng          * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
254aa16508fSDongjiu Geng          * (GHESv2 - Type 10)
255aa16508fSDongjiu Geng          */
256aa16508fSDongjiu Geng         build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
257aa16508fSDongjiu Geng     }
258aa16508fSDongjiu Geng 
259aa16508fSDongjiu Geng     /* Generic Error Status Block offset in the hardware error fw_cfg blob */
260aa16508fSDongjiu Geng     error_status_block_offset = hardware_errors->len;
261aa16508fSDongjiu Geng 
262aa16508fSDongjiu Geng     /* Reserve space for Error Status Data Block */
263aa16508fSDongjiu Geng     acpi_data_push(hardware_errors,
264aa16508fSDongjiu Geng         ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
265aa16508fSDongjiu Geng 
266aa16508fSDongjiu Geng     /* Tell guest firmware to place hardware_errors blob into RAM */
267aa16508fSDongjiu Geng     bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
268aa16508fSDongjiu Geng                              hardware_errors, sizeof(uint64_t), false);
269aa16508fSDongjiu Geng 
270aa16508fSDongjiu Geng     for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
271aa16508fSDongjiu Geng         /*
272aa16508fSDongjiu Geng          * Tell firmware to patch error_block_address entries to point to
273aa16508fSDongjiu Geng          * corresponding "Generic Error Status Block"
274aa16508fSDongjiu Geng          */
275aa16508fSDongjiu Geng         bios_linker_loader_add_pointer(linker,
276aa16508fSDongjiu Geng             ACPI_GHES_ERRORS_FW_CFG_FILE, sizeof(uint64_t) * i,
277aa16508fSDongjiu Geng             sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
278aa16508fSDongjiu Geng             error_status_block_offset + i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
279aa16508fSDongjiu Geng     }
280aa16508fSDongjiu Geng 
281aa16508fSDongjiu Geng     /*
282aa16508fSDongjiu Geng      * tell firmware to write hardware_errors GPA into
283aa16508fSDongjiu Geng      * hardware_errors_addr fw_cfg, once the former has been initialized.
284aa16508fSDongjiu Geng      */
285aa16508fSDongjiu Geng     bios_linker_loader_write_pointer(linker, ACPI_GHES_DATA_ADDR_FW_CFG_FILE,
286aa16508fSDongjiu Geng         0, sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE, 0);
287aa16508fSDongjiu Geng }
288205cc75dSDongjiu Geng 
289205cc75dSDongjiu Geng /* Build Generic Hardware Error Source version 2 (GHESv2) */
build_ghes_v2(GArray * table_data,int source_id,BIOSLinker * linker)290205cc75dSDongjiu Geng static void build_ghes_v2(GArray *table_data, int source_id, BIOSLinker *linker)
291205cc75dSDongjiu Geng {
292205cc75dSDongjiu Geng     uint64_t address_offset;
293205cc75dSDongjiu Geng     /*
294205cc75dSDongjiu Geng      * Type:
295205cc75dSDongjiu Geng      * Generic Hardware Error Source version 2(GHESv2 - Type 10)
296205cc75dSDongjiu Geng      */
297205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
298205cc75dSDongjiu Geng     /* Source Id */
299205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, source_id, 2);
300205cc75dSDongjiu Geng     /* Related Source Id */
301205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, 0xffff, 2);
302205cc75dSDongjiu Geng     /* Flags */
303205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, 0, 1);
304205cc75dSDongjiu Geng     /* Enabled */
305205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, 1, 1);
306205cc75dSDongjiu Geng 
307205cc75dSDongjiu Geng     /* Number of Records To Pre-allocate */
308205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, 1, 4);
309205cc75dSDongjiu Geng     /* Max Sections Per Record */
310205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, 1, 4);
311205cc75dSDongjiu Geng     /* Max Raw Data Length */
312205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
313205cc75dSDongjiu Geng 
314205cc75dSDongjiu Geng     address_offset = table_data->len;
315205cc75dSDongjiu Geng     /* Error Status Address */
316205cc75dSDongjiu Geng     build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
317205cc75dSDongjiu Geng                      4 /* QWord access */, 0);
318205cc75dSDongjiu Geng     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
319205cc75dSDongjiu Geng         address_offset + GAS_ADDR_OFFSET, sizeof(uint64_t),
320205cc75dSDongjiu Geng         ACPI_GHES_ERRORS_FW_CFG_FILE, source_id * sizeof(uint64_t));
321205cc75dSDongjiu Geng 
322205cc75dSDongjiu Geng     switch (source_id) {
323205cc75dSDongjiu Geng     case ACPI_HEST_SRC_ID_SEA:
324205cc75dSDongjiu Geng         /*
325205cc75dSDongjiu Geng          * Notification Structure
326205cc75dSDongjiu Geng          * Now only enable ARMv8 SEA notification type
327205cc75dSDongjiu Geng          */
328205cc75dSDongjiu Geng         build_ghes_hw_error_notification(table_data, ACPI_GHES_NOTIFY_SEA);
329205cc75dSDongjiu Geng         break;
330205cc75dSDongjiu Geng     default:
331205cc75dSDongjiu Geng         error_report("Not support this error source");
332205cc75dSDongjiu Geng         abort();
333205cc75dSDongjiu Geng     }
334205cc75dSDongjiu Geng 
335205cc75dSDongjiu Geng     /* Error Status Block Length */
336205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
337205cc75dSDongjiu Geng 
338205cc75dSDongjiu Geng     /*
339205cc75dSDongjiu Geng      * Read Ack Register
340205cc75dSDongjiu Geng      * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
341205cc75dSDongjiu Geng      * version 2 (GHESv2 - Type 10)
342205cc75dSDongjiu Geng      */
343205cc75dSDongjiu Geng     address_offset = table_data->len;
344205cc75dSDongjiu Geng     build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
345205cc75dSDongjiu Geng                      4 /* QWord access */, 0);
346205cc75dSDongjiu Geng     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
347205cc75dSDongjiu Geng         address_offset + GAS_ADDR_OFFSET,
348205cc75dSDongjiu Geng         sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
349205cc75dSDongjiu Geng         (ACPI_GHES_ERROR_SOURCE_COUNT + source_id) * sizeof(uint64_t));
350205cc75dSDongjiu Geng 
351205cc75dSDongjiu Geng     /*
352205cc75dSDongjiu Geng      * Read Ack Preserve field
353205cc75dSDongjiu Geng      * We only provide the first bit in Read Ack Register to OSPM to write
354205cc75dSDongjiu Geng      * while the other bits are preserved.
355205cc75dSDongjiu Geng      */
356205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, ~0x1ULL, 8);
357205cc75dSDongjiu Geng     /* Read Ack Write */
358205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, 0x1, 8);
359205cc75dSDongjiu Geng }
360205cc75dSDongjiu Geng 
361205cc75dSDongjiu Geng /* Build Hardware Error Source Table */
acpi_build_hest(GArray * table_data,BIOSLinker * linker,const char * oem_id,const char * oem_table_id)362602b4582SMarian Postevca void acpi_build_hest(GArray *table_data, BIOSLinker *linker,
363602b4582SMarian Postevca                      const char *oem_id, const char *oem_table_id)
364205cc75dSDongjiu Geng {
36513229858SIgor Mammedov     AcpiTable table = { .sig = "HEST", .rev = 1,
36613229858SIgor Mammedov                         .oem_id = oem_id, .oem_table_id = oem_table_id };
367205cc75dSDongjiu Geng 
36813229858SIgor Mammedov     acpi_table_begin(&table, table_data);
369205cc75dSDongjiu Geng 
370205cc75dSDongjiu Geng     /* Error Source Count */
371205cc75dSDongjiu Geng     build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
372205cc75dSDongjiu Geng     build_ghes_v2(table_data, ACPI_HEST_SRC_ID_SEA, linker);
373205cc75dSDongjiu Geng 
37413229858SIgor Mammedov     acpi_table_end(linker, &table);
375205cc75dSDongjiu Geng }
376a08a6462SDongjiu Geng 
acpi_ghes_add_fw_cfg(AcpiGhesState * ags,FWCfgState * s,GArray * hardware_error)377a08a6462SDongjiu Geng void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
378a08a6462SDongjiu Geng                           GArray *hardware_error)
379a08a6462SDongjiu Geng {
380a08a6462SDongjiu Geng     /* Create a read-only fw_cfg file for GHES */
381a08a6462SDongjiu Geng     fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
382a08a6462SDongjiu Geng                     hardware_error->len);
383a08a6462SDongjiu Geng 
384a08a6462SDongjiu Geng     /* Create a read-write fw_cfg file for Address */
385a08a6462SDongjiu Geng     fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
386a08a6462SDongjiu Geng         NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
3871c81f573SPeter Maydell 
3881c81f573SPeter Maydell     ags->present = true;
389a08a6462SDongjiu Geng }
390558b9d86SDongjiu Geng 
acpi_ghes_record_errors(uint8_t source_id,uint64_t physical_address)391558b9d86SDongjiu Geng int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
392558b9d86SDongjiu Geng {
393558b9d86SDongjiu Geng     uint64_t error_block_addr, read_ack_register_addr, read_ack_register = 0;
394558b9d86SDongjiu Geng     uint64_t start_addr;
395558b9d86SDongjiu Geng     bool ret = -1;
396558b9d86SDongjiu Geng     AcpiGedState *acpi_ged_state;
397558b9d86SDongjiu Geng     AcpiGhesState *ags;
398558b9d86SDongjiu Geng 
399558b9d86SDongjiu Geng     assert(source_id < ACPI_HEST_SRC_ID_RESERVED);
400558b9d86SDongjiu Geng 
401558b9d86SDongjiu Geng     acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
402558b9d86SDongjiu Geng                                                        NULL));
403558b9d86SDongjiu Geng     g_assert(acpi_ged_state);
404558b9d86SDongjiu Geng     ags = &acpi_ged_state->ghes_state;
405558b9d86SDongjiu Geng 
406558b9d86SDongjiu Geng     start_addr = le64_to_cpu(ags->ghes_addr_le);
407558b9d86SDongjiu Geng 
408558b9d86SDongjiu Geng     if (physical_address) {
409558b9d86SDongjiu Geng 
410558b9d86SDongjiu Geng         if (source_id < ACPI_HEST_SRC_ID_RESERVED) {
411558b9d86SDongjiu Geng             start_addr += source_id * sizeof(uint64_t);
412558b9d86SDongjiu Geng         }
413558b9d86SDongjiu Geng 
414558b9d86SDongjiu Geng         cpu_physical_memory_read(start_addr, &error_block_addr,
415558b9d86SDongjiu Geng                                  sizeof(error_block_addr));
416558b9d86SDongjiu Geng 
417558b9d86SDongjiu Geng         error_block_addr = le64_to_cpu(error_block_addr);
418558b9d86SDongjiu Geng 
419558b9d86SDongjiu Geng         read_ack_register_addr = start_addr +
420558b9d86SDongjiu Geng             ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
421558b9d86SDongjiu Geng 
422558b9d86SDongjiu Geng         cpu_physical_memory_read(read_ack_register_addr,
423558b9d86SDongjiu Geng                                  &read_ack_register, sizeof(read_ack_register));
424558b9d86SDongjiu Geng 
425558b9d86SDongjiu Geng         /* zero means OSPM does not acknowledge the error */
426558b9d86SDongjiu Geng         if (!read_ack_register) {
427558b9d86SDongjiu Geng             error_report("OSPM does not acknowledge previous error,"
428558b9d86SDongjiu Geng                 " so can not record CPER for current error anymore");
429558b9d86SDongjiu Geng         } else if (error_block_addr) {
430558b9d86SDongjiu Geng             read_ack_register = cpu_to_le64(0);
431558b9d86SDongjiu Geng             /*
432558b9d86SDongjiu Geng              * Clear the Read Ack Register, OSPM will write it to 1 when
433558b9d86SDongjiu Geng              * it acknowledges this error.
434558b9d86SDongjiu Geng              */
435558b9d86SDongjiu Geng             cpu_physical_memory_write(read_ack_register_addr,
436558b9d86SDongjiu Geng                 &read_ack_register, sizeof(uint64_t));
437558b9d86SDongjiu Geng 
438558b9d86SDongjiu Geng             ret = acpi_ghes_record_mem_error(error_block_addr,
439558b9d86SDongjiu Geng                                              physical_address);
440558b9d86SDongjiu Geng         } else
441558b9d86SDongjiu Geng             error_report("can not find Generic Error Status Block");
442558b9d86SDongjiu Geng     }
443558b9d86SDongjiu Geng 
444558b9d86SDongjiu Geng     return ret;
445558b9d86SDongjiu Geng }
4461c81f573SPeter Maydell 
acpi_ghes_present(void)4471c81f573SPeter Maydell bool acpi_ghes_present(void)
4481c81f573SPeter Maydell {
4491c81f573SPeter Maydell     AcpiGedState *acpi_ged_state;
4501c81f573SPeter Maydell     AcpiGhesState *ags;
4511c81f573SPeter Maydell 
4521c81f573SPeter Maydell     acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
4531c81f573SPeter Maydell                                                        NULL));
4541c81f573SPeter Maydell 
4551c81f573SPeter Maydell     if (!acpi_ged_state) {
4561c81f573SPeter Maydell         return false;
4571c81f573SPeter Maydell     }
4581c81f573SPeter Maydell     ags = &acpi_ged_state->ghes_state;
4591c81f573SPeter Maydell     return ags->present;
4601c81f573SPeter Maydell }
461