1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35#define ACPI_GHES_MAX_RAW_DATA_LENGTH (1 * KiB)
36
37
38#define ACPI_GHES_ERROR_SOURCE_COUNT 1
39
40
41#define ACPI_GHES_SOURCE_GENERIC_ERROR_V2 10
42
43
44#define GAS_ADDR_OFFSET 4
45
46
47
48
49
50
51#define ACPI_GHES_DATA_LENGTH 72
52
53
54#define ACPI_GHES_MEM_CPER_LENGTH 80
55
56
57#define ACPI_GEBS_UNCORRECTABLE 1
58
59
60
61
62
63
64#define ACPI_GHES_GESB_SIZE 20
65
66
67
68
69enum 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,
74};
75
76
77
78
79
80
81static void build_ghes_hw_error_notification(GArray *table, const uint8_t type)
82{
83
84 build_append_int_noprefix(table, type, 1);
85
86
87
88
89 build_append_int_noprefix(table, 28, 1);
90
91 build_append_int_noprefix(table, 0, 2);
92
93 build_append_int_noprefix(table, 0, 4);
94
95 build_append_int_noprefix(table, 0, 4);
96
97 build_append_int_noprefix(table, 0, 4);
98
99 build_append_int_noprefix(table, 0, 4);
100
101 build_append_int_noprefix(table, 0, 4);
102
103 build_append_int_noprefix(table, 0, 4);
104}
105
106
107
108
109
110static 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)
115{
116 const uint8_t fru_text[20] = {0};
117
118
119 g_array_append_vals(table, section_type, 16);
120
121
122 build_append_int_noprefix(table, error_severity, 4);
123
124 build_append_int_noprefix(table, 0x300, 2);
125
126 build_append_int_noprefix(table, validation_bits, 1);
127
128 build_append_int_noprefix(table, flags, 1);
129
130 build_append_int_noprefix(table, error_data_length, 4);
131
132
133 g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data));
134
135
136 g_array_append_vals(table, fru_text, sizeof(fru_text));
137
138
139 build_append_int_noprefix(table, time_stamp, 8);
140}
141
142
143
144
145
146static 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)
149{
150
151 build_append_int_noprefix(table, block_status, 4);
152
153 build_append_int_noprefix(table, raw_data_offset, 4);
154
155 build_append_int_noprefix(table, raw_data_length, 4);
156
157 build_append_int_noprefix(table, data_length, 4);
158
159 build_append_int_noprefix(table, error_severity, 4);
160}
161
162
163static void acpi_ghes_build_append_mem_cper(GArray *table,
164 uint64_t error_physical_addr)
165{
166
167
168
169
170
171 build_append_int_noprefix(table,
172 (1ULL << 14) |
173 (1ULL << 1) ,
174 8);
175
176 build_append_int_noprefix(table, 0, 8);
177
178 build_append_int_noprefix(table, error_physical_addr, 8);
179
180 build_append_int_noprefix(table, 0, 48);
181
182 build_append_int_noprefix(table, 0 , 1);
183
184 build_append_int_noprefix(table, 0, 7);
185}
186
187static int acpi_ghes_record_mem_error(uint64_t error_block_address,
188 uint64_t error_physical_addr)
189{
190 GArray *block;
191
192
193 const uint8_t uefi_cper_mem_sec[] =
194 UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
195 0xED, 0x7C, 0x83, 0xB1);
196
197
198
199
200 QemuUUID fru_id = {};
201 uint32_t data_length;
202
203 block = g_array_new(false, true , 1);
204
205
206 data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
207
208
209
210
211 assert((data_length + ACPI_GHES_GESB_SIZE) <=
212 ACPI_GHES_MAX_RAW_DATA_LENGTH);
213
214
215 acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
216 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
217
218
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);
222
223
224 acpi_ghes_build_append_mem_cper(block, error_physical_addr);
225
226
227 cpu_physical_memory_write(error_block_address, block->data, block->len);
228
229 g_array_free(block, true);
230
231 return 0;
232}
233
234
235
236
237
238
239void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
240{
241 int i, error_status_block_offset;
242
243
244 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
245 build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
246 }
247
248
249 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
250
251
252
253
254
255
256 build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
257 }
258
259
260 error_status_block_offset = hardware_errors->len;
261
262
263 acpi_data_push(hardware_errors,
264 ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
265
266
267 bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
268 hardware_errors, sizeof(uint64_t), false);
269
270 for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
271
272
273
274
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);
279 }
280
281
282
283
284
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);
287}
288
289
290static void build_ghes_v2(GArray *table_data, int source_id, BIOSLinker *linker)
291{
292 uint64_t address_offset;
293
294
295
296
297 build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
298
299 build_append_int_noprefix(table_data, source_id, 2);
300
301 build_append_int_noprefix(table_data, 0xffff, 2);
302
303 build_append_int_noprefix(table_data, 0, 1);
304
305 build_append_int_noprefix(table_data, 1, 1);
306
307
308 build_append_int_noprefix(table_data, 1, 4);
309
310 build_append_int_noprefix(table_data, 1, 4);
311
312 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
313
314 address_offset = table_data->len;
315
316 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
317 4 , 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));
321
322 switch (source_id) {
323 case ACPI_HEST_SRC_ID_SEA:
324
325
326
327
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();
333 }
334
335
336 build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
337
338
339
340
341
342
343 address_offset = table_data->len;
344 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
345 4 , 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));
350
351
352
353
354
355
356 build_append_int_noprefix(table_data, ~0x1ULL, 8);
357
358 build_append_int_noprefix(table_data, 0x1, 8);
359}
360
361
362void acpi_build_hest(GArray *table_data, BIOSLinker *linker,
363 const char *oem_id, const char *oem_table_id)
364{
365 AcpiTable table = { .sig = "HEST", .rev = 1,
366 .oem_id = oem_id, .oem_table_id = oem_table_id };
367
368 acpi_table_begin(&table, table_data);
369
370
371 build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
372 build_ghes_v2(table_data, ACPI_HEST_SRC_ID_SEA, linker);
373
374 acpi_table_end(linker, &table);
375}
376
377void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
378 GArray *hardware_error)
379{
380
381 fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
382 hardware_error->len);
383
384
385 fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
386 NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
387
388 ags->present = true;
389}
390
391int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
392{
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;
398
399 assert(source_id < ACPI_HEST_SRC_ID_RESERVED);
400
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;
405
406 start_addr = le64_to_cpu(ags->ghes_addr_le);
407
408 if (physical_address) {
409
410 if (source_id < ACPI_HEST_SRC_ID_RESERVED) {
411 start_addr += source_id * sizeof(uint64_t);
412 }
413
414 cpu_physical_memory_read(start_addr, &error_block_addr,
415 sizeof(error_block_addr));
416
417 error_block_addr = le64_to_cpu(error_block_addr);
418
419 read_ack_register_addr = start_addr +
420 ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
421
422 cpu_physical_memory_read(read_ack_register_addr,
423 &read_ack_register, sizeof(read_ack_register));
424
425
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);
431
432
433
434
435 cpu_physical_memory_write(read_ack_register_addr,
436 &read_ack_register, sizeof(uint64_t));
437
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");
442 }
443
444 return ret;
445}
446
447bool acpi_ghes_present(void)
448{
449 AcpiGedState *acpi_ged_state;
450 AcpiGhesState *ags;
451
452 acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
453 NULL));
454
455 if (!acpi_ged_state) {
456 return false;
457 }
458 ags = &acpi_ged_state->ghes_state;
459 return ags->present;
460}
461