mb/google/skyrim: Enable Chrome EC
[coreboot.git] / src / arch / x86 / acpi_bert_storage.c
blob8559c06dfb588078af639764d63ee320e8be896b
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #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>
8 #include <acpi/acpi.h>
9 #include <arch/bert_storage.h>
10 #include <string.h>
11 #include <types.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
21 * manage the space.
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) {
44 *start = NULL;
45 *size = 0;
46 return;
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)
56 size_t alloc;
58 if (bert_region_broken)
59 return NULL;
60 if (bert_region_used + size > bert_region_size)
61 return NULL;
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
71 * with it.
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;
79 size_t struct_size;
81 if (!num || num > bert_entry_count(status))
82 return NULL;
84 ptr = (acpi_hest_generic_data_v300_t *)(status + 1);
85 while (--num) {
86 if (ptr->revision == HEST_GENERIC_ENTRY_V300)
87 struct_size = sizeof(acpi_hest_generic_data_v300_t);
88 else
89 struct_size = sizeof(acpi_hest_generic_data_t);
90 ptr = (acpi_hest_generic_data_v300_t *)(
91 (u8 *)ptr
92 + ptr->data_length
93 + struct_size);
95 return ptr;
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;
102 int entries;
104 if (!status)
105 return;
107 entries = bert_entry_count(status);
108 entry = acpi_hest_generic_data_nth(status, entries);
109 status->data_length += size;
110 if (entry)
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
121 * entries are added.
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));
129 if (!status) {
130 printk(BIOS_ERR, "New BERT error entry would exceed available region\n");
131 return NULL;
134 status->error_severity = ACPI_GENERROR_SEV_NONE;
135 return status;
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.
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");
163 return NULL;
166 entry = bert_allocate_storage(sizeof(*entry));
167 if (!entry) {
168 printk(BIOS_ERR, "New BERT error entry would exceed available region\n");
169 return NULL;
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);
180 return entry;
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");
195 return 0;
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);
201 if (!cl_data) {
202 printk(BIOS_ERR, "Crashlog entry (size %zu) would exceed available region\n",
203 cl_size);
204 return NULL;
207 revise_error_sizes(status, cl_size);
209 return cl_data;
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);
219 if (!entry)
220 return NULL;
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);
233 return entry;
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;
245 void *sect;
246 size_t sect_size;
248 sect_size = sizeof_error_section(guid);
249 if (!sect_size)
250 return NULL; /* Don't allocate structure if bad GUID passed */
252 if (sizeof(*entry) + sect_size > bert_storage_remaining())
253 return NULL;
255 entry = new_generic_error_entry(status);
256 if (!entry)
257 return NULL;
259 /* error section immediately follows the Generic Error Data Entry */
260 sect = bert_allocate_storage(sect_size);
261 if (!sect)
262 return NULL;
264 revise_error_sizes(status, sect_size);
266 guidcpy(&entry->section_type, guid);
267 return entry;
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
272 * caller.
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);
282 if (!entry)
283 return NULL;
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;
304 return entry;
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
310 * created.
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
314 * is updated.
316 * type is one of:
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)
334 size_t size;
335 cper_ia32x64_context_t *ctx;
336 static const char * const ctx_names[] = {
337 "Unclassified Data",
338 "MSR Registers",
339 "32-bit Mode Execution",
340 "64-bit Mode Execution",
341 "FXSAVE",
342 "32-bit Mode Debug",
343 "64-bit Mode Debug",
344 "Memory Mapped"
347 if (type > CPER_IA32X64_CTX_MEMMAPPED)
348 return NULL;
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",
352 ctx_names[type]);
353 return NULL;
356 size = cper_ia32x64_ctx_sz_bytype(type, num);
357 ctx = bert_allocate_storage(size);
358 if (!ctx) {
359 printk(BIOS_ERR, "New IA32X64 %s context entry would exceed available region\n",
360 ctx_names[type]);
361 return NULL;
364 revise_error_sizes(status, size);
366 ctx->type = type;
367 ctx->array_size = num;
368 cper_bump_ia32x64_ctx_count(x86err);
370 return ctx;
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
376 * added.
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
380 * updated.
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[] = {
389 "cache",
390 "TLB",
391 "bus",
392 "MS"
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)
402 return NULL;
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",
406 check_names[type]);
407 return NULL;
410 check = bert_allocate_storage(sizeof(*check));
411 if (!check) {
412 printk(BIOS_ERR, "New IA32X64 %s check entry would exceed available region\n",
413 check_names[type]);
414 return NULL;
417 revise_error_sizes(status, sizeof(*check));
419 guidcpy(&check->type, &check_guids[type]);
420 cper_bump_ia32x64_chk_count(x86err);
422 return check;
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
427 * caller.
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);
438 if (!entry)
439 return NULL;
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;
451 id = cpuid(1);
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;
458 return entry;
461 static const char * const generic_error_types[] = {
462 "PROCESSOR_GENERIC",
463 "PROCESSOR_SPECIFIC_X86",
464 "PROCESSOR_SPECIFIC_ARM",
465 "PLATFORM_MEMORY",
466 "PLATFORM_MEMORY2",
467 "PCIE",
468 "FW_ERROR_RECORD",
469 "PCI_PCIX_BUS",
470 "PCI_DEVICE",
471 "DMAR_GENERIC",
472 "DIRECTED_IO_DMAR",
473 "IOMMU_DMAR",
474 "UNRECOGNIZED"
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
508 * Section.
510 acpi_generic_error_status_t *bert_new_event(guid_t *guid)
512 size_t size;
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));
523 return NULL;
526 status = new_bert_status();
527 if (!status)
528 return NULL;
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 */
537 else
538 r = NULL;
540 if (r)
541 return status;
542 return NULL;
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;
551 int i;
552 msr_t *dest;
554 ctx = new_cper_ia32x64_ctx(status, x86err, CPER_IA32X64_CTX_MSR, num);
555 if (!ctx)
556 return NULL;
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);
566 return ctx;
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",
575 __func__);
576 *start = NULL;
577 *size = 0;
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;
593 return;
596 memset(bert_region_base, 0, bert_region_size);
599 RAMSTAGE_CBMEM_INIT_HOOK(bert_storage_setup)