blob: b07b67ee8af60b9eeae52f1fbf4f759d6c4fc980 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Marshall Dawson991467d2018-09-04 12:32:56 -06002
Marshall Dawson991467d2018-09-04 12:32:56 -06003#include <cbmem.h>
4#include <console/console.h>
5#include <cpu/x86/name.h>
6#include <cpu/x86/msr.h>
7#include <cpu/x86/lapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07008#include <acpi/acpi.h>
Marshall Dawson991467d2018-09-04 12:32:56 -06009#include <arch/bert_storage.h>
10#include <string.h>
11
12/* BERT region management: Allow the chipset to determine the specific
13 * location of the BERT region. We find that base and size, then manage
14 * the allocation of error information within it.
15 *
16 * Use simple static variables for managing the BERT region. This is a thin
17 * implementation; it is only created and consumed by coreboot, and only in
18 * a single stage, and we don't want its information to survive reboot or
19 * resume cycles. If the requirements change, consider using IMD to help
20 * manage the space.
21 */
22static int bert_region_broken;
23static void *bert_region_base;
24static size_t bert_region_size;
25static size_t bert_region_used;
26
27/* Calculate the remaining space in the BERT region. This knowledge may help
28 * the caller prioritize the information to store.
29 */
30size_t bert_storage_remaining(void)
31{
32 return bert_region_broken ? 0 : bert_region_size - bert_region_used;
33}
34
35int bert_errors_present(void)
36{
37 return bert_region_broken ? 0 : !!bert_region_used;
38}
39
40void bert_errors_region(void **start, size_t *size)
41{
42 if (bert_region_broken) {
43 *start = NULL;
44 *size = 0;
45 return;
46 }
47
48 /* No metadata, etc. with our region, so this is easy */
49 *start = bert_region_base;
50 *size = bert_region_used;
51}
52
53static void *bert_allocate_storage(size_t size)
54{
55 size_t alloc;
56
57 if (bert_region_broken)
58 return NULL;
59 if (bert_region_used + size > bert_region_size)
60 return NULL;
61
62 alloc = bert_region_used;
63 bert_region_used += size;
64
65 return (void *)((u8 *)bert_region_base + alloc);
66}
67
68/* Generic Error Status: Each Status represents a unique error event within
69 * the BERT errors region. Each event may have multiple errors associated
70 * with it.
71 */
72
73/* Find the nth (1-based) Generic Data Structure attached to an Error Status */
74static void *acpi_hest_generic_data_nth(
75 acpi_generic_error_status_t *status, int num)
76{
77 acpi_hest_generic_data_v300_t *ptr;
78 size_t struct_size;
79
80 if (!num || num > bert_entry_count(status))
81 return NULL;
82
83 ptr = (acpi_hest_generic_data_v300_t *)(status + 1);
84 while (--num) {
85 if (ptr->revision == HEST_GENERIC_ENTRY_V300)
86 struct_size = sizeof(acpi_hest_generic_data_v300_t);
87 else
88 struct_size = sizeof(acpi_hest_generic_data_t);
89 ptr = (acpi_hest_generic_data_v300_t *)(
90 (u8 *)ptr
91 + ptr->data_length
92 + struct_size);
93 }
94 return ptr;
95}
96
97/* Update data_length for this Error Status, and final Data Entry it contains */
98static void revise_error_sizes(acpi_generic_error_status_t *status, size_t size)
99{
100 acpi_hest_generic_data_v300_t *entry;
101 int entries;
102
103 if (!status)
104 return;
105
106 entries = bert_entry_count(status);
107 entry = acpi_hest_generic_data_nth(status, entries);
108 status->data_length += size;
Francois Toguo522e0db2021-01-21 09:55:19 -0800109 status->raw_data_length += size;
Marshall Dawson991467d2018-09-04 12:32:56 -0600110 if (entry)
111 entry->data_length += size;
112}
113
114/* Create space for a new BERT Generic Error Status Block, by finding the next
115 * available slot and moving the ending location. There is nothing to designate
116 * this as another Generic Error Status Block (e.g. no signature); only that it
117 * is within the BERT region.
118 *
119 * It is up to the caller to correctly fill the information, including status
120 * and error severity, and to update/maintain data offsets and lengths as
121 * entries are added.
122 */
123static acpi_generic_error_status_t *new_bert_status(void)
124{
125 acpi_generic_error_status_t *status;
126
127 status = bert_allocate_storage(sizeof(*status));
128
129 if (!status) {
130 printk(BIOS_ERR, "Error: New BERT error entry would exceed available region\n");
131 return NULL;
132 }
133
134 status->error_severity = ACPI_GENERROR_SEV_NONE;
135 return status;
136}
137
138/* Generic Error Data: Each Generic Error Status may contain zero or more
139 * Generic Error Data structures. The data structures describe particular
140 * error(s) associated with an event. The definition for the structure is
141 * found in the ACPI spec, however the data types and any accompanying data
142 * definitions are in the Common Platform Error Record appendix of the UEFI
143 * spec.
144 */
145
146/* Create space for a new BERT Generic Data Entry. Update the count and
147 * data length in the parent Generic Error Status Block. Version 0x300 of
148 * the structure is used, and the timestamp is filled and marked precise
149 * (i.e. assumed close enough for reporting).
150 *
151 * It is up to the caller to fill the Section Type field and add the Common
152 * Platform Error Record type data as appropriate. In addition, the caller
153 * should update the error severity, and may optionally add FRU information
154 * or override any existing information.
155 */
156static acpi_hest_generic_data_v300_t *new_generic_error_entry(
157 acpi_generic_error_status_t *status)
158{
159 acpi_hest_generic_data_v300_t *entry;
160
161 if (bert_entry_count(status) == GENERIC_ERR_STS_ENTRY_COUNT_MAX) {
162 printk(BIOS_ERR, "Error: New BERT error would exceed maximum entries\n");
163 return NULL;
164 }
165
166 entry = bert_allocate_storage(sizeof(*entry));
167 if (!entry) {
168 printk(BIOS_ERR, "Error: New BERT error entry would exceed available region\n");
169 return NULL;
170 }
171
172 entry->revision = HEST_GENERIC_ENTRY_V300;
173
174 entry->timestamp = cper_timestamp(CPER_TIMESTAMP_PRECISE);
175 entry->validation_bits |= ACPI_GENERROR_VALID_TIMESTAMP;
176
177 status->data_length += sizeof(*entry);
Francois Toguo522e0db2021-01-21 09:55:19 -0800178 status->raw_data_length += sizeof(*entry);
Marshall Dawson991467d2018-09-04 12:32:56 -0600179 bert_bump_entry_count(status);
180
181 return entry;
182}
183
184/* Find the size of a CPER error section w/o any add-ons */
185static size_t sizeof_error_section(guid_t *guid)
186{
187 if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
188 return sizeof(cper_proc_generic_error_section_t);
189 else if (!guidcmp(guid, &CPER_SEC_PROC_IA32X64_GUID))
190 return sizeof(cper_ia32x64_proc_error_section_t);
Francois Toguo522e0db2021-01-21 09:55:19 -0800191 else if (!guidcmp(guid, &CPER_SEC_FW_ERR_REC_REF_GUID))
192 return sizeof(cper_fw_err_rec_section_t);
Marshall Dawson991467d2018-09-04 12:32:56 -0600193 /* else if ... sizeof(structures not yet defined) */
194
195 printk(BIOS_ERR, "Error: Requested size of unrecognized CPER GUID\n");
196 return 0;
197}
198
Francois Toguo522e0db2021-01-21 09:55:19 -0800199void *new_cper_fw_error_crashlog(acpi_generic_error_status_t *status, size_t cl_size)
200{
201 void *cl_data = bert_allocate_storage(cl_size);
202 if (!cl_data) {
Benjamin Doron07dda332021-02-05 00:23:46 +0000203 printk(BIOS_ERR, "Error: Crashlog entry (size %zu) would exceed available region\n",
Francois Toguo522e0db2021-01-21 09:55:19 -0800204 cl_size);
205 return NULL;
206 }
207
208 revise_error_sizes(status, cl_size);
209
210 return cl_data;
211}
212
213/* Helper to append an ACPI Generic Error Data Entry per crashlog data */
214acpi_hest_generic_data_v300_t *bert_append_fw_err(acpi_generic_error_status_t *status)
215{
216 acpi_hest_generic_data_v300_t *entry;
217 cper_fw_err_rec_section_t *fw_err;
218
219 entry = bert_append_error_datasection(status, &CPER_SEC_FW_ERR_REC_REF_GUID);
220 if (!entry)
221 return NULL;
222
223 status->block_status |= GENERIC_ERR_STS_UNCORRECTABLE_VALID;
224 status->error_severity = ACPI_GENERROR_SEV_FATAL;
225 entry->error_severity = ACPI_GENERROR_SEV_FATAL;
226
227 fw_err = section_of_acpientry(fw_err, entry);
228
229 fw_err->record_type = CRASHLOG_RECORD_TYPE;
230 fw_err->revision = CRASHLOG_FW_ERR_REV;
231 fw_err->record_id = 0;
232 guidcpy(&fw_err->record_guid, &FW_ERR_RECORD_ID_CRASHLOG_GUID);
233
234 return entry;
235}
236
Marshall Dawson991467d2018-09-04 12:32:56 -0600237/* Append a new ACPI Generic Error Data Entry plus CPER Error Section to an
238 * existing ACPI Generic Error Status Block. The caller is responsible for
239 * the setting the status and entry severity, as well as populating all fields
240 * of the error section.
241 */
242acpi_hest_generic_data_v300_t *bert_append_error_datasection(
243 acpi_generic_error_status_t *status, guid_t *guid)
244{
245 acpi_hest_generic_data_v300_t *entry;
246 void *sect;
247 size_t sect_size;
248
249 sect_size = sizeof_error_section(guid);
250 if (!sect_size)
251 return NULL; /* Don't allocate structure if bad GUID passed */
252
253 if (sizeof(*entry) + sect_size > bert_storage_remaining())
254 return NULL;
255
256 entry = new_generic_error_entry(status);
257 if (!entry)
258 return NULL;
259
260 /* error section immediately follows the Generic Error Data Entry */
261 sect = bert_allocate_storage(sect_size);
262 if (!sect)
263 return NULL;
264
265 revise_error_sizes(status, sect_size);
266
267 guidcpy(&entry->section_type, guid);
268 return entry;
269}
270
271/* Helper to append an ACPI Generic Error Data Entry plus a CPER Processor
272 * Generic Error Section. As many fields are populated as possible for the
273 * caller.
274 */
275acpi_hest_generic_data_v300_t *bert_append_genproc(
276 acpi_generic_error_status_t *status)
277{
278 acpi_hest_generic_data_v300_t *entry;
279 cper_proc_generic_error_section_t *ges;
280
281 entry = bert_append_error_datasection(status,
282 &CPER_SEC_PROC_GENERIC_GUID);
283 if (!entry)
284 return NULL;
285
286 status->block_status |= GENERIC_ERR_STS_UNCORRECTABLE_VALID;
287 status->error_severity = ACPI_GENERROR_SEV_FATAL;
288
289 entry->error_severity = ACPI_GENERROR_SEV_FATAL;
290
291 ges = section_of_acpientry(ges, entry);
292
293 ges->proc_type = GENPROC_PROCTYPE_IA32X64;
294 ges->validation |= GENPROC_VALID_PROC_TYPE;
295
296 ges->cpu_version = cpuid_eax(1);
297 ges->validation |= GENPROC_VALID_CPU_VERSION;
298
299 fill_processor_name(ges->cpu_brand_string);
300 ges->validation |= GENPROC_VALID_CPU_BRAND;
301
302 ges->proc_id = lapicid();
303 ges->validation |= GENPROC_VALID_CPU_ID;
304
305 return entry;
306}
307
308/* Add a new IA32/X64 Processor Context Structure (Table 261), following any
309 * other contexts, to an existing Processor Error Section (Table 255). Contexts
310 * may only be added after the entire Processor Error Info array has been
311 * created.
312 *
313 * This function fills only the minimal amount of information required to parse
314 * or step through the contexts. The type is filled and PROC_CONTEXT_INFO_NUM
315 * is updated.
316 *
317 * type is one of:
318 * CPER_IA32X64_CTX_UNCL
319 * CPER_IA32X64_CTX_MSR
320 * CPER_IA32X64_CTX_32BIT_EX
321 * CPER_IA32X64_CTX_64BIT_EX
322 * CPER_IA32X64_CTX_FXSAVE
323 * CPER_IA32X64_CTX_32BIT_DBG
324 * CPER_IA32X64_CTX_64BIT_DBG
325 * CPER_IA32X64_CTX_MEMMAPPED
326 * num is the number of bytes eventually used to fill the context's register
327 * array, e.g. 4 MSRs * sizeof(msr_t)
328 *
329 * status and entry data_length values are updated.
330 */
331cper_ia32x64_context_t *new_cper_ia32x64_ctx(
332 acpi_generic_error_status_t *status,
333 cper_ia32x64_proc_error_section_t *x86err, int type, int num)
334{
335 size_t size;
336 cper_ia32x64_context_t *ctx;
337 static const char * const ctx_names[] = {
338 "Unclassified Data",
339 "MSR Registers",
340 "32-bit Mode Execution",
341 "64-bit Mode Execution",
Richard Spiegelc75f2d82018-09-14 08:27:50 -0700342 "FXSAVE",
343 "32-bit Mode Debug",
344 "64-bit Mode Debug",
Marshall Dawson991467d2018-09-04 12:32:56 -0600345 "Memory Mapped"
346 };
347
348 if (type > CPER_IA32X64_CTX_MEMMAPPED)
349 return NULL;
350
351 if (cper_ia32x64_proc_num_ctxs(x86err) == I32X64SEC_VALID_CTXNUM_MAX) {
352 printk(BIOS_ERR, "Error: New IA32X64 %s context entry would exceed max allowable contexts\n",
353 ctx_names[type]);
354 return NULL;
355 }
356
357 size = cper_ia32x64_ctx_sz_bytype(type, num);
358 ctx = bert_allocate_storage(size);
359 if (!ctx) {
360 printk(BIOS_ERR, "Error: New IA32X64 %s context entry would exceed available region\n",
361 ctx_names[type]);
362 return NULL;
363 }
364
365 revise_error_sizes(status, size);
366
367 ctx->type = type;
368 ctx->array_size = num;
369 cper_bump_ia32x64_ctx_count(x86err);
370
371 return ctx;
372}
373
374/* Add a new IA32/X64 Processor Error Information Structure (Table 256),
375 * following any other errors, to an existing Processor Error Section
376 * (Table 255). All error structures must be added before any contexts are
377 * added.
378 *
379 * This function fills only the minimal amount of information required to parse
380 * or step through the errors. The type is filled and PROC_ERR_INFO_NUM is
381 * updated.
382 */
383cper_ia32x64_proc_error_info_t *new_cper_ia32x64_check(
384 acpi_generic_error_status_t *status,
385 cper_ia32x64_proc_error_section_t *x86err,
386 enum cper_x86_check_type type)
387{
388 cper_ia32x64_proc_error_info_t *check;
389 static const char * const check_names[] = {
390 "cache",
391 "TLB",
392 "bus",
393 "MS"
394 };
395 const guid_t check_guids[] = {
396 X86_PROCESSOR_CACHE_CHK_ERROR_GUID,
397 X86_PROCESSOR_TLB_CHK_ERROR_GUID,
398 X86_PROCESSOR_BUS_CHK_ERROR_GUID,
399 X86_PROCESSOR_MS_CHK_ERROR_GUID
400 };
401
402 if (type > X86_PROCESSOR_CHK_MAX)
403 return NULL;
404
405 if (cper_ia32x64_proc_num_chks(x86err) == I32X64SEC_VALID_ERRNUM_MAX) {
406 printk(BIOS_ERR, "Error: New IA32X64 %s check entry would exceed max allowable errors\n",
407 check_names[type]);
408 return NULL;
409 }
410
411 check = bert_allocate_storage(sizeof(*check));
412 if (!check) {
413 printk(BIOS_ERR, "Error: New IA32X64 %s check entry would exceed available region\n",
414 check_names[type]);
415 return NULL;
416 }
417
418 revise_error_sizes(status, sizeof(*check));
419
420 guidcpy(&check->type, &check_guids[type]);
421 cper_bump_ia32x64_chk_count(x86err);
422
423 return check;
424}
425
426/* Helper to append an ACPI Generic Error Data Entry plus a CPER IA32/X64
427 * Processor Error Section. As many fields are populated as possible for the
428 * caller.
429 */
430acpi_hest_generic_data_v300_t *bert_append_ia32x64(
431 acpi_generic_error_status_t *status)
432{
433 acpi_hest_generic_data_v300_t *entry;
434 cper_ia32x64_proc_error_section_t *ipe;
435 struct cpuid_result id;
436
437 entry = bert_append_error_datasection(status,
438 &CPER_SEC_PROC_IA32X64_GUID);
439 if (!entry)
440 return NULL;
441
442 status->block_status |= GENERIC_ERR_STS_UNCORRECTABLE_VALID;
443 status->error_severity = ACPI_GENERROR_SEV_FATAL;
444
445 entry->error_severity = ACPI_GENERROR_SEV_FATAL;
446
447 ipe = section_of_acpientry(ipe, entry);
448
449 ipe->apicid = lapicid();
450 ipe->validation |= I32X64SEC_VALID_LAPIC;
451
452 id = cpuid(1);
453 ipe->cpuid[0] = id.eax;
454 ipe->cpuid[1] = id.ebx;
455 ipe->cpuid[2] = id.ecx;
456 ipe->cpuid[3] = id.edx;
457 ipe->validation |= I32X64SEC_VALID_CPUID;
458
459 return entry;
460}
461
462static const char * const generic_error_types[] = {
463 "PROCESSOR_GENERIC",
464 "PROCESSOR_SPECIFIC_X86",
465 "PROCESSOR_SPECIFIC_ARM",
466 "PLATFORM_MEMORY",
467 "PLATFORM_MEMORY2",
468 "PCIE",
469 "FW_ERROR_RECORD",
470 "PCI_PCIX_BUS",
471 "PCI_DEVICE",
472 "DMAR_GENERIC",
473 "DIRECTED_IO_DMAR",
474 "IOMMU_DMAR",
475 "UNRECOGNIZED"
476};
477
478static const char *generic_error_name(guid_t *guid)
479{
480 if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
481 return generic_error_types[0];
482 if (!guidcmp(guid, &CPER_SEC_PROC_IA32X64_GUID))
483 return generic_error_types[1];
484 if (!guidcmp(guid, &CPER_SEC_PROC_ARM_GUID))
485 return generic_error_types[2];
486 if (!guidcmp(guid, &CPER_SEC_PLATFORM_MEM_GUID))
487 return generic_error_types[3];
488 if (!guidcmp(guid, &CPER_SEC_PLATFORM_MEM2_GUID))
489 return generic_error_types[4];
490 if (!guidcmp(guid, &CPER_SEC_PCIE_GUID))
491 return generic_error_types[5];
492 if (!guidcmp(guid, &CPER_SEC_FW_ERR_REC_REF_GUID))
493 return generic_error_types[6];
494 if (!guidcmp(guid, &CPER_SEC_PCI_X_BUS_GUID))
495 return generic_error_types[7];
496 if (!guidcmp(guid, &CPER_SEC_PCI_DEV_GUID))
497 return generic_error_types[8];
498 if (!guidcmp(guid, &CPER_SEC_DMAR_GENERIC_GUID))
499 return generic_error_types[9];
500 if (!guidcmp(guid, &CPER_SEC_DMAR_VT_GUID))
501 return generic_error_types[10];
502 if (!guidcmp(guid, &CPER_SEC_DMAR_IOMMU_GUID))
503 return generic_error_types[11];
504 return generic_error_types[12];
505}
506
507/* Add a new event to the BERT region. An event consists of an ACPI Error
508 * Status Block, a Generic Error Data Entry, and an associated CPER Error
509 * Section.
510 */
511acpi_generic_error_status_t *bert_new_event(guid_t *guid)
512{
513 size_t size;
514 acpi_generic_error_status_t *status;
515 acpi_hest_generic_data_v300_t *entry, *r;
516
517 size = sizeof(*status);
518 size += sizeof(*entry);
519 size += sizeof_error_section(guid);
520
521 if (size > bert_storage_remaining()) {
522 printk(BIOS_ERR, "Error: Not enough BERT region space to add event for type %s\n",
523 generic_error_name(guid));
524 return NULL;
525 }
526
527 status = new_bert_status();
528 if (!status)
529 return NULL;
530
Francois Toguo522e0db2021-01-21 09:55:19 -0800531 status->raw_data_length = sizeof(*status);
532
Marshall Dawson991467d2018-09-04 12:32:56 -0600533 if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
534 r = bert_append_genproc(status);
535 else if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
536 r = bert_append_ia32x64(status);
Lijian Zhao94e49612021-02-06 12:18:12 +0800537 else if (!guidcmp(guid, &CPER_SEC_FW_ERR_REC_REF_GUID))
Francois Toguo522e0db2021-01-21 09:55:19 -0800538 r = bert_append_fw_err(status);
Marshall Dawson991467d2018-09-04 12:32:56 -0600539 /* else if other types not implemented */
540 else
541 r = NULL;
542
543 if (r)
544 return status;
545 return NULL;
546}
547
548/* Helper to add an MSR context to an existing IA32/X64-type error entry */
549cper_ia32x64_context_t *cper_new_ia32x64_context_msr(
550 acpi_generic_error_status_t *status,
551 cper_ia32x64_proc_error_section_t *x86err, u32 addr, int num)
552{
553 cper_ia32x64_context_t *ctx;
554 int i;
555 msr_t *dest;
556
557 ctx = new_cper_ia32x64_ctx(status, x86err, CPER_IA32X64_CTX_MSR, num);
558 if (!ctx)
559 return NULL;
560
561 /* already filled ctx->type = CPER_IA32X64_CTX_MSR; */
562 ctx->msr_addr = addr;
563 ctx->array_size = num * sizeof(msr_t);
564
565 dest = (msr_t *)((u8 *)(ctx + 1)); /* point to the Register Array */
566
567 for (i = 0 ; i < num ; i++)
568 *(dest + i) = rdmsr(addr + i);
569 return ctx;
570}
571
572/* The region must be in memory marked as reserved. If not implemented,
573 * skip generating the information in the region.
574 */
575__weak void bert_reserved_region(void **start, size_t *size)
576{
577 printk(BIOS_ERR, "Error: %s not implemented. BERT region generation disabled\n",
578 __func__);
579 *start = NULL;
580 *size = 0;
581}
582
583static void bert_storage_setup(int unused)
584{
585 /* Always start with a blank bert region. Make sure nothing is
586 * maintained across reboots or resumes.
587 */
588 bert_region_broken = 0;
589 bert_region_used = 0;
590
591 bert_reserved_region(&bert_region_base, &bert_region_size);
592
593 if (!bert_region_base || !bert_region_size) {
594 printk(BIOS_ERR, "Bug: Can't find/add BERT storage area\n");
595 bert_region_broken = 1;
596 return;
597 }
598
599 memset(bert_region_base, 0, bert_region_size);
600}
601
602RAMSTAGE_CBMEM_INIT_HOOK(bert_storage_setup)