summaryrefslogtreecommitdiff
path: root/src/arch/x86/acpi_bert_storage.c
blob: c5f98f7ea880c7b86b45e22d2c55b736c4db2a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* SPDX-License-Identifier: GPL-2.0-only */
/* This file is part of the coreboot project. */

#include <bootstate.h>
#include <cbmem.h>
#include <console/console.h>
#include <cpu/x86/name.h>
#include <cpu/x86/msr.h>
#include <cpu/x86/lapic.h>
#include <acpi/acpi.h>
#include <arch/bert_storage.h>
#include <string.h>

/* BERT region management:  Allow the chipset to determine the specific
 * location of the BERT region.  We find that base and size, then manage
 * the allocation of error information within it.
 *
 * Use simple static variables for managing the BERT region.  This is a thin
 * implementation; it is only created and consumed by coreboot, and only in
 * a single stage, and we don't want its information to survive reboot or
 * resume cycles.  If the requirements change, consider using IMD to help
 * manage the space.
 */
static int bert_region_broken;
static void *bert_region_base;
static size_t bert_region_size;
static size_t bert_region_used;

/* Calculate the remaining space in the BERT region.  This knowledge may help
 * the caller prioritize the information to store.
 */
size_t bert_storage_remaining(void)
{
	return bert_region_broken ? 0 : bert_region_size - bert_region_used;
}

int bert_errors_present(void)
{
	return bert_region_broken ? 0 : !!bert_region_used;
}

void bert_errors_region(void **start, size_t *size)
{
	if (bert_region_broken) {
		*start = NULL;
		*size = 0;
		return;
	}

	/* No metadata, etc. with our region, so this is easy */
	*start = bert_region_base;
	*size = bert_region_used;
}

static void *bert_allocate_storage(size_t size)
{
	size_t alloc;

	if (bert_region_broken)
		return NULL;
	if (bert_region_used + size > bert_region_size)
		return NULL;

	alloc = bert_region_used;
	bert_region_used += size;

	return (void *)((u8 *)bert_region_base + alloc);
}

/* Generic Error Status:  Each Status represents a unique error event within
 * the BERT errors region.  Each event may have multiple errors associated
 * with it.
 */

/* Find the nth (1-based) Generic Data Structure attached to an Error Status */
static void *acpi_hest_generic_data_nth(
		acpi_generic_error_status_t *status, int num)
{
	acpi_hest_generic_data_v300_t *ptr;
	size_t struct_size;

	if (!num || num > bert_entry_count(status))
		return NULL;

	ptr = (acpi_hest_generic_data_v300_t *)(status + 1);
	while (--num) {
		if (ptr->revision == HEST_GENERIC_ENTRY_V300)
			struct_size = sizeof(acpi_hest_generic_data_v300_t);
		else
			struct_size = sizeof(acpi_hest_generic_data_t);
		ptr = (acpi_hest_generic_data_v300_t *)(
				(u8 *)ptr
				+ ptr->data_length
				+ struct_size);
	}
	return ptr;
}

/* Update data_length for this Error Status, and final Data Entry it contains */
static void revise_error_sizes(acpi_generic_error_status_t *status, size_t size)
{
	acpi_hest_generic_data_v300_t *entry;
	int entries;

	if (!status)
		return;

	entries = bert_entry_count(status);
	entry = acpi_hest_generic_data_nth(status, entries);
	status->data_length += size;
	if (entry)
		entry->data_length += size;
}

/* Create space for a new BERT Generic Error Status Block, by finding the next
 * available slot and moving the ending location.  There is nothing to designate
 * this as another Generic Error Status Block (e.g. no signature); only that it
 * is within the BERT region.
 *
 * It is up to the caller to correctly fill the information, including status
 * and error severity, and to update/maintain data offsets and lengths as
 * entries are added.
 */
static acpi_generic_error_status_t *new_bert_status(void)
{
	acpi_generic_error_status_t *status;

	status = bert_allocate_storage(sizeof(*status));

	if (!status) {
		printk(BIOS_ERR, "Error: New BERT error entry would exceed available region\n");
		return NULL;
	}

	status->error_severity = ACPI_GENERROR_SEV_NONE;
	return status;
}

/* Generic Error Data:  Each Generic Error Status may contain zero or more
 * Generic Error Data structures.  The data structures describe particular
 * error(s) associated with an event.  The definition for the structure is
 * found in the ACPI spec, however the data types and any accompanying data
 * definitions are in the Common Platform Error Record appendix of the UEFI
 * spec.
 */

/* Create space for a new BERT Generic Data Entry.  Update the count and
 * data length in the parent Generic Error Status Block.  Version 0x300 of
 * the structure is used, and the timestamp is filled and marked precise
 * (i.e. assumed close enough for reporting).
 *
 * It is up to the caller to fill the Section Type field and add the Common
 * Platform Error Record type data as appropriate.  In addition, the caller
 * should update the error severity, and may optionally add FRU information
 * or override any existing information.
 */
static acpi_hest_generic_data_v300_t *new_generic_error_entry(
		acpi_generic_error_status_t *status)
{
	acpi_hest_generic_data_v300_t *entry;

	if (bert_entry_count(status) == GENERIC_ERR_STS_ENTRY_COUNT_MAX) {
		printk(BIOS_ERR, "Error: New BERT error would exceed maximum entries\n");
		return NULL;
	}

	entry = bert_allocate_storage(sizeof(*entry));
	if (!entry) {
		printk(BIOS_ERR, "Error: New BERT error entry would exceed available region\n");
		return NULL;
	}

	entry->revision = HEST_GENERIC_ENTRY_V300;

	entry->timestamp = cper_timestamp(CPER_TIMESTAMP_PRECISE);
	entry->validation_bits |= ACPI_GENERROR_VALID_TIMESTAMP;

	status->data_length += sizeof(*entry);
	bert_bump_entry_count(status);

	return entry;
}

/* Find the size of a CPER error section w/o any add-ons */
static size_t sizeof_error_section(guid_t *guid)
{
	if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
		return sizeof(cper_proc_generic_error_section_t);
	else if (!guidcmp(guid, &CPER_SEC_PROC_IA32X64_GUID))
		return sizeof(cper_ia32x64_proc_error_section_t);
	/* else if ... sizeof(structures not yet defined) */

	printk(BIOS_ERR, "Error: Requested size of unrecognized CPER GUID\n");
	return 0;
}

/* Append a new ACPI Generic Error Data Entry plus CPER Error Section to an
 * existing ACPI Generic Error Status Block.  The caller is responsible for
 * the setting the status and entry severity, as well as populating all fields
 * of the error section.
 */
acpi_hest_generic_data_v300_t *bert_append_error_datasection(
		acpi_generic_error_status_t *status, guid_t *guid)
{
	acpi_hest_generic_data_v300_t *entry;
	void *sect;
	size_t sect_size;

	sect_size = sizeof_error_section(guid);
	if (!sect_size)
		return NULL; /* Don't allocate structure if bad GUID passed */

	if (sizeof(*entry) + sect_size > bert_storage_remaining())
		return NULL;

	entry = new_generic_error_entry(status);
	if (!entry)
		return NULL;

	/* error section immediately follows the Generic Error Data Entry */
	sect = bert_allocate_storage(sect_size);
	if (!sect)
		return NULL;

	revise_error_sizes(status, sect_size);

	guidcpy(&entry->section_type, guid);
	return entry;
}

/* Helper to append an ACPI Generic Error Data Entry plus a CPER Processor
 * Generic Error Section.  As many fields are populated as possible for the
 * caller.
 */
acpi_hest_generic_data_v300_t *bert_append_genproc(
		acpi_generic_error_status_t *status)
{
	acpi_hest_generic_data_v300_t *entry;
	cper_proc_generic_error_section_t *ges;

	entry = bert_append_error_datasection(status,
					&CPER_SEC_PROC_GENERIC_GUID);
	if (!entry)
		return NULL;

	status->block_status |= GENERIC_ERR_STS_UNCORRECTABLE_VALID;
	status->error_severity = ACPI_GENERROR_SEV_FATAL;

	entry->error_severity = ACPI_GENERROR_SEV_FATAL;

	ges = section_of_acpientry(ges, entry);

	ges->proc_type = GENPROC_PROCTYPE_IA32X64;
	ges->validation |= GENPROC_VALID_PROC_TYPE;

	ges->cpu_version = cpuid_eax(1);
	ges->validation |= GENPROC_VALID_CPU_VERSION;

	fill_processor_name(ges->cpu_brand_string);
	ges->validation |= GENPROC_VALID_CPU_BRAND;

	ges->proc_id = lapicid();
	ges->validation |= GENPROC_VALID_CPU_ID;

	return entry;
}

/* Add a new IA32/X64 Processor Context Structure (Table 261), following any
 * other contexts, to an existing Processor Error Section (Table 255).  Contexts
 * may only be added after the entire Processor Error Info array has been
 * created.
 *
 * This function fills only the minimal amount of information required to parse
 * or step through the contexts.  The type is filled and PROC_CONTEXT_INFO_NUM
 * is updated.
 *
 * type is one of:
 *   CPER_IA32X64_CTX_UNCL
 *   CPER_IA32X64_CTX_MSR
 *   CPER_IA32X64_CTX_32BIT_EX
 *   CPER_IA32X64_CTX_64BIT_EX
 *   CPER_IA32X64_CTX_FXSAVE
 *   CPER_IA32X64_CTX_32BIT_DBG
 *   CPER_IA32X64_CTX_64BIT_DBG
 *   CPER_IA32X64_CTX_MEMMAPPED
 * num is the number of bytes eventually used to fill the context's register
 *   array, e.g. 4 MSRs * sizeof(msr_t)
 *
 * status and entry data_length values are updated.
 */
cper_ia32x64_context_t *new_cper_ia32x64_ctx(
		acpi_generic_error_status_t *status,
		cper_ia32x64_proc_error_section_t *x86err, int type, int num)
{
	size_t size;
	cper_ia32x64_context_t *ctx;
	static const char * const ctx_names[] = {
			"Unclassified Data",
			"MSR Registers",
			"32-bit Mode Execution",
			"64-bit Mode Execution",
			"FXSAVE",
			"32-bit Mode Debug",
			"64-bit Mode Debug",
			"Memory Mapped"
	};

	if (type > CPER_IA32X64_CTX_MEMMAPPED)
		return NULL;

	if (cper_ia32x64_proc_num_ctxs(x86err) == I32X64SEC_VALID_CTXNUM_MAX) {
		printk(BIOS_ERR, "Error: New IA32X64 %s context entry would exceed max allowable contexts\n",
				ctx_names[type]);
		return NULL;
	}

	size = cper_ia32x64_ctx_sz_bytype(type, num);
	ctx = bert_allocate_storage(size);
	if (!ctx) {
		printk(BIOS_ERR, "Error: New IA32X64 %s context entry would exceed available region\n",
				ctx_names[type]);
		return NULL;
	}

	revise_error_sizes(status, size);

	ctx->type = type;
	ctx->array_size = num;
	cper_bump_ia32x64_ctx_count(x86err);

	return ctx;
}

/* Add a new IA32/X64 Processor Error Information Structure (Table 256),
 * following any other errors, to an existing Processor Error Section
 * (Table 255).  All error structures must be added before any contexts are
 * added.
 *
 * This function fills only the minimal amount of information required to parse
 * or step through the errors.  The type is filled and PROC_ERR_INFO_NUM is
 * updated.
 */
cper_ia32x64_proc_error_info_t *new_cper_ia32x64_check(
		acpi_generic_error_status_t *status,
		cper_ia32x64_proc_error_section_t *x86err,
		enum cper_x86_check_type type)
{
	cper_ia32x64_proc_error_info_t *check;
	static const char * const check_names[] = {
			"cache",
			"TLB",
			"bus",
			"MS"
	};
	const guid_t check_guids[] = {
			X86_PROCESSOR_CACHE_CHK_ERROR_GUID,
			X86_PROCESSOR_TLB_CHK_ERROR_GUID,
			X86_PROCESSOR_BUS_CHK_ERROR_GUID,
			X86_PROCESSOR_MS_CHK_ERROR_GUID
	};

	if (type > X86_PROCESSOR_CHK_MAX)
		return NULL;

	if (cper_ia32x64_proc_num_chks(x86err) == I32X64SEC_VALID_ERRNUM_MAX) {
		printk(BIOS_ERR, "Error: New IA32X64 %s check entry would exceed max allowable errors\n",
				check_names[type]);
		return NULL;
	}

	check = bert_allocate_storage(sizeof(*check));
	if (!check) {
		printk(BIOS_ERR, "Error: New IA32X64 %s check entry would exceed available region\n",
				check_names[type]);
		return NULL;
	}

	revise_error_sizes(status, sizeof(*check));

	guidcpy(&check->type, &check_guids[type]);
	cper_bump_ia32x64_chk_count(x86err);

	return check;
}

/* Helper to append an ACPI Generic Error Data Entry plus a CPER IA32/X64
 * Processor Error Section.  As many fields are populated as possible for the
 * caller.
 */
acpi_hest_generic_data_v300_t *bert_append_ia32x64(
					acpi_generic_error_status_t *status)
{
	acpi_hest_generic_data_v300_t *entry;
	cper_ia32x64_proc_error_section_t *ipe;
	struct cpuid_result id;

	entry = bert_append_error_datasection(status,
					&CPER_SEC_PROC_IA32X64_GUID);
	if (!entry)
		return NULL;

	status->block_status |= GENERIC_ERR_STS_UNCORRECTABLE_VALID;
	status->error_severity = ACPI_GENERROR_SEV_FATAL;

	entry->error_severity = ACPI_GENERROR_SEV_FATAL;

	ipe = section_of_acpientry(ipe, entry);

	ipe->apicid = lapicid();
	ipe->validation |= I32X64SEC_VALID_LAPIC;

	id = cpuid(1);
	ipe->cpuid[0] = id.eax;
	ipe->cpuid[1] = id.ebx;
	ipe->cpuid[2] = id.ecx;
	ipe->cpuid[3] = id.edx;
	ipe->validation |= I32X64SEC_VALID_CPUID;

	return entry;
}

static const char * const generic_error_types[] = {
	"PROCESSOR_GENERIC",
	"PROCESSOR_SPECIFIC_X86",
	"PROCESSOR_SPECIFIC_ARM",
	"PLATFORM_MEMORY",
	"PLATFORM_MEMORY2",
	"PCIE",
	"FW_ERROR_RECORD",
	"PCI_PCIX_BUS",
	"PCI_DEVICE",
	"DMAR_GENERIC",
	"DIRECTED_IO_DMAR",
	"IOMMU_DMAR",
	"UNRECOGNIZED"
};

static const char *generic_error_name(guid_t *guid)
{
	if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
		return generic_error_types[0];
	if (!guidcmp(guid, &CPER_SEC_PROC_IA32X64_GUID))
		return generic_error_types[1];
	if (!guidcmp(guid, &CPER_SEC_PROC_ARM_GUID))
		return generic_error_types[2];
	if (!guidcmp(guid, &CPER_SEC_PLATFORM_MEM_GUID))
		return generic_error_types[3];
	if (!guidcmp(guid, &CPER_SEC_PLATFORM_MEM2_GUID))
		return generic_error_types[4];
	if (!guidcmp(guid, &CPER_SEC_PCIE_GUID))
		return generic_error_types[5];
	if (!guidcmp(guid, &CPER_SEC_FW_ERR_REC_REF_GUID))
		return generic_error_types[6];
	if (!guidcmp(guid, &CPER_SEC_PCI_X_BUS_GUID))
		return generic_error_types[7];
	if (!guidcmp(guid, &CPER_SEC_PCI_DEV_GUID))
		return generic_error_types[8];
	if (!guidcmp(guid, &CPER_SEC_DMAR_GENERIC_GUID))
		return generic_error_types[9];
	if (!guidcmp(guid, &CPER_SEC_DMAR_VT_GUID))
		return generic_error_types[10];
	if (!guidcmp(guid, &CPER_SEC_DMAR_IOMMU_GUID))
		return generic_error_types[11];
	return generic_error_types[12];
}

/* Add a new event to the BERT region.  An event consists of an ACPI Error
 * Status Block, a Generic Error Data Entry, and an associated CPER Error
 * Section.
 */
acpi_generic_error_status_t *bert_new_event(guid_t *guid)
{
	size_t size;
	acpi_generic_error_status_t *status;
	acpi_hest_generic_data_v300_t *entry, *r;

	size = sizeof(*status);
	size += sizeof(*entry);
	size += sizeof_error_section(guid);

	if (size > bert_storage_remaining()) {
		printk(BIOS_ERR, "Error: Not enough BERT region space to add event for type %s\n",
				generic_error_name(guid));
		return NULL;
	}

	status = new_bert_status();
	if (!status)
		return NULL;

	if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
		r = bert_append_genproc(status);
	else if (!guidcmp(guid, &CPER_SEC_PROC_GENERIC_GUID))
		r = bert_append_ia32x64(status);
	/* else if other types not implemented */
	else
		r = NULL;

	if (r)
		return status;
	return NULL;
}

/* Helper to add an MSR context to an existing IA32/X64-type error entry */
cper_ia32x64_context_t *cper_new_ia32x64_context_msr(
		acpi_generic_error_status_t *status,
		cper_ia32x64_proc_error_section_t *x86err, u32 addr, int num)
{
	cper_ia32x64_context_t *ctx;
	int i;
	msr_t *dest;

	ctx = new_cper_ia32x64_ctx(status, x86err, CPER_IA32X64_CTX_MSR, num);
	if (!ctx)
		return NULL;

	/* already filled ctx->type = CPER_IA32X64_CTX_MSR; */
	ctx->msr_addr = addr;
	ctx->array_size = num * sizeof(msr_t);

	dest = (msr_t *)((u8 *)(ctx + 1)); /* point to the Register Array */

	for (i = 0 ; i < num ; i++)
		*(dest + i) = rdmsr(addr + i);
	return ctx;
}

/* The region must be in memory marked as reserved.  If not implemented,
 * skip generating the information in the region.
 */
__weak void bert_reserved_region(void **start, size_t *size)
{
	printk(BIOS_ERR, "Error: %s not implemented.  BERT region generation disabled\n",
			__func__);
	*start = NULL;
	*size = 0;
}

static void bert_storage_setup(int unused)
{
	/* Always start with a blank bert region.  Make sure nothing is
	 * maintained across reboots or resumes.
	 */
	bert_region_broken = 0;
	bert_region_used = 0;

	bert_reserved_region(&bert_region_base, &bert_region_size);

	if (!bert_region_base || !bert_region_size) {
		printk(BIOS_ERR, "Bug: Can't find/add BERT storage area\n");
		bert_region_broken = 1;
		return;
	}

	memset(bert_region_base, 0, bert_region_size);
}

RAMSTAGE_CBMEM_INIT_HOOK(bert_storage_setup)