1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
5 #include <cpu/x86/name.h>
6 #include <cpu/x86/msr.h>
7 #include <cpu/x86/lapic.h>
9 #include <arch/bert_storage.h>
13 /* BERT region management: Allow the chipset to determine the specific
14 * location of the BERT region. We find that base and size, then manage
15 * the allocation of error information within it.
17 * Use simple static variables for managing the BERT region. This is a thin
18 * implementation; it is only created and consumed by coreboot, and only in
19 * a single stage, and we don't want its information to survive reboot or
20 * resume cycles. If the requirements change, consider using IMD to help
23 static bool bert_region_broken
;
24 static void *bert_region_base
;
25 static size_t bert_region_size
;
26 static size_t bert_region_used
;
28 /* Calculate the remaining space in the BERT region. This knowledge may help
29 * the caller prioritize the information to store.
31 size_t bert_storage_remaining(void)
33 return bert_region_broken
? 0 : bert_region_size
- bert_region_used
;
36 bool bert_errors_present(void)
38 return !bert_region_broken
&& bert_region_used
;
41 void bert_errors_region(void **start
, size_t *size
)
43 if (bert_region_broken
) {
49 /* No metadata, etc. with our region, so this is easy */
50 *start
= bert_region_base
;
51 *size
= bert_region_used
;
54 static void *bert_allocate_storage(size_t size
)
58 if (bert_region_broken
)
60 if (bert_region_used
+ size
> bert_region_size
)
63 alloc
= bert_region_used
;
64 bert_region_used
+= size
;
66 return (void *)((u8
*)bert_region_base
+ alloc
);
69 /* Generic Error Status: Each Status represents a unique error event within
70 * the BERT errors region. Each event may have multiple errors associated
74 /* Find the nth (1-based) Generic Data Structure attached to an Error Status */
75 static void *acpi_hest_generic_data_nth(
76 acpi_generic_error_status_t
*status
, int num
)
78 acpi_hest_generic_data_v300_t
*ptr
;
81 if (!num
|| num
> bert_entry_count(status
))
84 ptr
= (acpi_hest_generic_data_v300_t
*)(status
+ 1);
86 if (ptr
->revision
== HEST_GENERIC_ENTRY_V300
)
87 struct_size
= sizeof(acpi_hest_generic_data_v300_t
);
89 struct_size
= sizeof(acpi_hest_generic_data_t
);
90 ptr
= (acpi_hest_generic_data_v300_t
*)(
98 /* Update data_length for this Error Status, and final Data Entry it contains */
99 static void revise_error_sizes(acpi_generic_error_status_t
*status
, size_t size
)
101 acpi_hest_generic_data_v300_t
*entry
;
107 entries
= bert_entry_count(status
);
108 entry
= acpi_hest_generic_data_nth(status
, entries
);
109 status
->data_length
+= size
;
111 entry
->data_length
+= size
;
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.
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
123 static acpi_generic_error_status_t
*new_bert_status(void)
125 acpi_generic_error_status_t
*status
;
127 status
= bert_allocate_storage(sizeof(*status
));
130 printk(BIOS_ERR
, "New BERT error entry would exceed available region\n");
134 status
->error_severity
= ACPI_GENERROR_SEV_NONE
;
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
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).
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.
156 static acpi_hest_generic_data_v300_t
*new_generic_error_entry(
157 acpi_generic_error_status_t
*status
)
159 acpi_hest_generic_data_v300_t
*entry
;
161 if (bert_entry_count(status
) == GENERIC_ERR_STS_ENTRY_COUNT_MAX
) {
162 printk(BIOS_ERR
, "New BERT error would exceed maximum entries\n");
166 entry
= bert_allocate_storage(sizeof(*entry
));
168 printk(BIOS_ERR
, "New BERT error entry would exceed available region\n");
172 entry
->revision
= HEST_GENERIC_ENTRY_V300
;
174 entry
->timestamp
= cper_timestamp(CPER_TIMESTAMP_PRECISE
);
175 entry
->validation_bits
|= ACPI_GENERROR_VALID_TIMESTAMP
;
177 status
->data_length
+= sizeof(*entry
);
178 bert_bump_entry_count(status
);
183 /* Find the size of a CPER error section w/o any add-ons */
184 static size_t sizeof_error_section(guid_t
*guid
)
186 if (!guidcmp(guid
, &CPER_SEC_PROC_GENERIC_GUID
))
187 return sizeof(cper_proc_generic_error_section_t
);
188 else if (!guidcmp(guid
, &CPER_SEC_PROC_IA32X64_GUID
))
189 return sizeof(cper_ia32x64_proc_error_section_t
);
190 else if (!guidcmp(guid
, &CPER_SEC_FW_ERR_REC_REF_GUID
))
191 return sizeof(cper_fw_err_rec_section_t
);
192 /* else if ... sizeof(structures not yet defined) */
194 printk(BIOS_ERR
, "Requested size of unrecognized CPER GUID\n");
198 void *new_cper_fw_error_crashlog(acpi_generic_error_status_t
*status
, size_t cl_size
)
200 void *cl_data
= bert_allocate_storage(cl_size
);
202 printk(BIOS_ERR
, "Crashlog entry (size %zu) would exceed available region\n",
207 revise_error_sizes(status
, cl_size
);
212 /* Helper to append an ACPI Generic Error Data Entry per crashlog data */
213 acpi_hest_generic_data_v300_t
*bert_append_fw_err(acpi_generic_error_status_t
*status
)
215 acpi_hest_generic_data_v300_t
*entry
;
216 cper_fw_err_rec_section_t
*fw_err
;
218 entry
= bert_append_error_datasection(status
, &CPER_SEC_FW_ERR_REC_REF_GUID
);
222 status
->block_status
|= GENERIC_ERR_STS_UNCORRECTABLE_VALID
;
223 status
->error_severity
= ACPI_GENERROR_SEV_FATAL
;
224 entry
->error_severity
= ACPI_GENERROR_SEV_FATAL
;
226 fw_err
= section_of_acpientry(fw_err
, entry
);
228 fw_err
->record_type
= CRASHLOG_RECORD_TYPE
;
229 fw_err
->revision
= CRASHLOG_FW_ERR_REV
;
230 fw_err
->record_id
= 0;
231 guidcpy(&fw_err
->record_guid
, &FW_ERR_RECORD_ID_CRASHLOG_GUID
);
236 /* Append a new ACPI Generic Error Data Entry plus CPER Error Section to an
237 * existing ACPI Generic Error Status Block. The caller is responsible for
238 * the setting the status and entry severity, as well as populating all fields
239 * of the error section.
241 acpi_hest_generic_data_v300_t
*bert_append_error_datasection(
242 acpi_generic_error_status_t
*status
, guid_t
*guid
)
244 acpi_hest_generic_data_v300_t
*entry
;
248 sect_size
= sizeof_error_section(guid
);
250 return NULL
; /* Don't allocate structure if bad GUID passed */
252 if (sizeof(*entry
) + sect_size
> bert_storage_remaining())
255 entry
= new_generic_error_entry(status
);
259 /* error section immediately follows the Generic Error Data Entry */
260 sect
= bert_allocate_storage(sect_size
);
264 revise_error_sizes(status
, sect_size
);
266 guidcpy(&entry
->section_type
, guid
);
270 /* Helper to append an ACPI Generic Error Data Entry plus a CPER Processor
271 * Generic Error Section. As many fields are populated as possible for the
274 acpi_hest_generic_data_v300_t
*bert_append_genproc(
275 acpi_generic_error_status_t
*status
)
277 acpi_hest_generic_data_v300_t
*entry
;
278 cper_proc_generic_error_section_t
*ges
;
280 entry
= bert_append_error_datasection(status
,
281 &CPER_SEC_PROC_GENERIC_GUID
);
285 status
->block_status
|= GENERIC_ERR_STS_UNCORRECTABLE_VALID
;
286 status
->error_severity
= ACPI_GENERROR_SEV_FATAL
;
288 entry
->error_severity
= ACPI_GENERROR_SEV_FATAL
;
290 ges
= section_of_acpientry(ges
, entry
);
292 ges
->proc_type
= GENPROC_PROCTYPE_IA32X64
;
293 ges
->validation
|= GENPROC_VALID_PROC_TYPE
;
295 ges
->cpu_version
= cpuid_eax(1);
296 ges
->validation
|= GENPROC_VALID_CPU_VERSION
;
298 fill_processor_name(ges
->cpu_brand_string
);
299 ges
->validation
|= GENPROC_VALID_CPU_BRAND
;
301 ges
->proc_id
= lapicid();
302 ges
->validation
|= GENPROC_VALID_CPU_ID
;
307 /* Add a new IA32/X64 Processor Context Structure (Table 261), following any
308 * other contexts, to an existing Processor Error Section (Table 255). Contexts
309 * may only be added after the entire Processor Error Info array has been
312 * This function fills only the minimal amount of information required to parse
313 * or step through the contexts. The type is filled and PROC_CONTEXT_INFO_NUM
317 * CPER_IA32X64_CTX_UNCL
318 * CPER_IA32X64_CTX_MSR
319 * CPER_IA32X64_CTX_32BIT_EX
320 * CPER_IA32X64_CTX_64BIT_EX
321 * CPER_IA32X64_CTX_FXSAVE
322 * CPER_IA32X64_CTX_32BIT_DBG
323 * CPER_IA32X64_CTX_64BIT_DBG
324 * CPER_IA32X64_CTX_MEMMAPPED
325 * num is the number of bytes eventually used to fill the context's register
326 * array, e.g. 4 MSRs * sizeof(msr_t)
328 * status and entry data_length values are updated.
330 cper_ia32x64_context_t
*new_cper_ia32x64_ctx(
331 acpi_generic_error_status_t
*status
,
332 cper_ia32x64_proc_error_section_t
*x86err
, int type
, int num
)
335 cper_ia32x64_context_t
*ctx
;
336 static const char * const ctx_names
[] = {
339 "32-bit Mode Execution",
340 "64-bit Mode Execution",
347 if (type
> CPER_IA32X64_CTX_MEMMAPPED
)
350 if (cper_ia32x64_proc_num_ctxs(x86err
) == I32X64SEC_VALID_CTXNUM_MAX
) {
351 printk(BIOS_ERR
, "New IA32X64 %s context entry would exceed max allowable contexts\n",
356 size
= cper_ia32x64_ctx_sz_bytype(type
, num
);
357 ctx
= bert_allocate_storage(size
);
359 printk(BIOS_ERR
, "New IA32X64 %s context entry would exceed available region\n",
364 revise_error_sizes(status
, size
);
367 ctx
->array_size
= num
;
368 cper_bump_ia32x64_ctx_count(x86err
);
373 /* Add a new IA32/X64 Processor Error Information Structure (Table 256),
374 * following any other errors, to an existing Processor Error Section
375 * (Table 255). All error structures must be added before any contexts are
378 * This function fills only the minimal amount of information required to parse
379 * or step through the errors. The type is filled and PROC_ERR_INFO_NUM is
382 cper_ia32x64_proc_error_info_t
*new_cper_ia32x64_check(
383 acpi_generic_error_status_t
*status
,
384 cper_ia32x64_proc_error_section_t
*x86err
,
385 enum cper_x86_check_type type
)
387 cper_ia32x64_proc_error_info_t
*check
;
388 static const char * const check_names
[] = {
394 const guid_t check_guids
[] = {
395 X86_PROCESSOR_CACHE_CHK_ERROR_GUID
,
396 X86_PROCESSOR_TLB_CHK_ERROR_GUID
,
397 X86_PROCESSOR_BUS_CHK_ERROR_GUID
,
398 X86_PROCESSOR_MS_CHK_ERROR_GUID
401 if (type
> X86_PROCESSOR_CHK_MAX
)
404 if (cper_ia32x64_proc_num_chks(x86err
) == I32X64SEC_VALID_ERRNUM_MAX
) {
405 printk(BIOS_ERR
, "New IA32X64 %s check entry would exceed max allowable errors\n",
410 check
= bert_allocate_storage(sizeof(*check
));
412 printk(BIOS_ERR
, "New IA32X64 %s check entry would exceed available region\n",
417 revise_error_sizes(status
, sizeof(*check
));
419 guidcpy(&check
->type
, &check_guids
[type
]);
420 cper_bump_ia32x64_chk_count(x86err
);
425 /* Helper to append an ACPI Generic Error Data Entry plus a CPER IA32/X64
426 * Processor Error Section. As many fields are populated as possible for the
429 acpi_hest_generic_data_v300_t
*bert_append_ia32x64(
430 acpi_generic_error_status_t
*status
)
432 acpi_hest_generic_data_v300_t
*entry
;
433 cper_ia32x64_proc_error_section_t
*ipe
;
434 struct cpuid_result id
;
436 entry
= bert_append_error_datasection(status
,
437 &CPER_SEC_PROC_IA32X64_GUID
);
441 status
->block_status
|= GENERIC_ERR_STS_UNCORRECTABLE_VALID
;
442 status
->error_severity
= ACPI_GENERROR_SEV_FATAL
;
444 entry
->error_severity
= ACPI_GENERROR_SEV_FATAL
;
446 ipe
= section_of_acpientry(ipe
, entry
);
448 ipe
->apicid
= lapicid();
449 ipe
->validation
|= I32X64SEC_VALID_LAPIC
;
452 ipe
->cpuid
[0] = id
.eax
;
453 ipe
->cpuid
[1] = id
.ebx
;
454 ipe
->cpuid
[2] = id
.ecx
;
455 ipe
->cpuid
[3] = id
.edx
;
456 ipe
->validation
|= I32X64SEC_VALID_CPUID
;
461 static const char * const generic_error_types
[] = {
463 "PROCESSOR_SPECIFIC_X86",
464 "PROCESSOR_SPECIFIC_ARM",
477 static const char *generic_error_name(guid_t
*guid
)
479 if (!guidcmp(guid
, &CPER_SEC_PROC_GENERIC_GUID
))
480 return generic_error_types
[0];
481 if (!guidcmp(guid
, &CPER_SEC_PROC_IA32X64_GUID
))
482 return generic_error_types
[1];
483 if (!guidcmp(guid
, &CPER_SEC_PROC_ARM_GUID
))
484 return generic_error_types
[2];
485 if (!guidcmp(guid
, &CPER_SEC_PLATFORM_MEM_GUID
))
486 return generic_error_types
[3];
487 if (!guidcmp(guid
, &CPER_SEC_PLATFORM_MEM2_GUID
))
488 return generic_error_types
[4];
489 if (!guidcmp(guid
, &CPER_SEC_PCIE_GUID
))
490 return generic_error_types
[5];
491 if (!guidcmp(guid
, &CPER_SEC_FW_ERR_REC_REF_GUID
))
492 return generic_error_types
[6];
493 if (!guidcmp(guid
, &CPER_SEC_PCI_X_BUS_GUID
))
494 return generic_error_types
[7];
495 if (!guidcmp(guid
, &CPER_SEC_PCI_DEV_GUID
))
496 return generic_error_types
[8];
497 if (!guidcmp(guid
, &CPER_SEC_DMAR_GENERIC_GUID
))
498 return generic_error_types
[9];
499 if (!guidcmp(guid
, &CPER_SEC_DMAR_VT_GUID
))
500 return generic_error_types
[10];
501 if (!guidcmp(guid
, &CPER_SEC_DMAR_IOMMU_GUID
))
502 return generic_error_types
[11];
503 return generic_error_types
[12];
506 /* Add a new event to the BERT region. An event consists of an ACPI Error
507 * Status Block, a Generic Error Data Entry, and an associated CPER Error
510 acpi_generic_error_status_t
*bert_new_event(guid_t
*guid
)
513 acpi_generic_error_status_t
*status
;
514 acpi_hest_generic_data_v300_t
*entry
, *r
;
516 size
= sizeof(*status
);
517 size
+= sizeof(*entry
);
518 size
+= sizeof_error_section(guid
);
520 if (size
> bert_storage_remaining()) {
521 printk(BIOS_ERR
, "Not enough BERT region space to add event for type %s\n",
522 generic_error_name(guid
));
526 status
= new_bert_status();
530 if (!guidcmp(guid
, &CPER_SEC_PROC_GENERIC_GUID
))
531 r
= bert_append_genproc(status
);
532 else if (!guidcmp(guid
, &CPER_SEC_PROC_GENERIC_GUID
))
533 r
= bert_append_ia32x64(status
);
534 else if (!guidcmp(guid
, &CPER_SEC_FW_ERR_REC_REF_GUID
))
535 r
= bert_append_fw_err(status
);
536 /* else if other types not implemented */
545 /* Helper to add an MSR context to an existing IA32/X64-type error entry */
546 cper_ia32x64_context_t
*cper_new_ia32x64_context_msr(
547 acpi_generic_error_status_t
*status
,
548 cper_ia32x64_proc_error_section_t
*x86err
, u32 addr
, int num
)
550 cper_ia32x64_context_t
*ctx
;
554 ctx
= new_cper_ia32x64_ctx(status
, x86err
, CPER_IA32X64_CTX_MSR
, num
);
558 /* already filled ctx->type = CPER_IA32X64_CTX_MSR; */
559 ctx
->msr_addr
= addr
;
560 ctx
->array_size
= num
* sizeof(msr_t
);
562 dest
= (msr_t
*)((u8
*)(ctx
+ 1)); /* point to the Register Array */
564 for (i
= 0 ; i
< num
; i
++)
565 *(dest
+ i
) = rdmsr(addr
+ i
);
569 /* The region must be in memory marked as reserved. If not implemented,
570 * skip generating the information in the region.
572 __weak
void bert_reserved_region(void **start
, size_t *size
)
574 printk(BIOS_ERR
, "%s not implemented. BERT region generation disabled\n",
580 static void bert_storage_setup(int unused
)
582 /* Always start with a blank bert region. Make sure nothing is
583 * maintained across reboots or resumes.
585 bert_region_broken
= false;
586 bert_region_used
= 0;
588 bert_reserved_region(&bert_region_base
, &bert_region_size
);
590 if (!bert_region_base
|| !bert_region_size
) {
591 printk(BIOS_ERR
, "Bug: Can't find/add BERT storage area\n");
592 bert_region_broken
= true;
596 memset(bert_region_base
, 0, bert_region_size
);
599 RAMSTAGE_CBMEM_INIT_HOOK(bert_storage_setup
)