blob: 34ddcb33c993245b72393a7711704802694ca1e4 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Marc Jones1587dc82017-05-15 18:55:11 -06003
4#define __SIMPLE_DEVICE__
5
Marshall Dawsonb6172112017-09-13 17:47:31 -06006#include <assert.h>
Marc Jones1587dc82017-05-15 18:55:11 -06007#include <stdint.h>
Elyes HAOUAS20eaef02019-03-29 17:45:28 +01008#include <console/console.h>
Marshall Dawson94ee9372017-06-15 12:18:23 -06009#include <cpu/x86/msr.h>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +030010#include <cpu/x86/smm.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020011#include <cpu/amd/msr.h>
Marshall Dawson94ee9372017-06-15 12:18:23 -060012#include <cpu/amd/mtrr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060013#include <cbmem.h>
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060014#include <arch/bert_storage.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060015#include <soc/northbridge.h>
Marshall Dawson69486ca2019-05-02 12:03:45 -060016#include <soc/iomap.h>
17#include <amdblocks/acpimmio.h>
Marc Jones1587dc82017-05-15 18:55:11 -060018
Julius Wernercd49cce2019-03-05 16:53:33 -080019#if CONFIG(ACPI_BERT)
Marshall Dawsonf0de2422018-09-10 13:28:49 -060020 #if CONFIG_SMM_TSEG_SIZE == 0x0
21 #define BERT_REGION_MAX_SIZE 0x100000
22 #else
23 /* SMM_TSEG_SIZE must stay on a boundary appropriate for its granularity */
24 #define BERT_REGION_MAX_SIZE CONFIG_SMM_TSEG_SIZE
25 #endif
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060026#else
Marshall Dawsonf0de2422018-09-10 13:28:49 -060027 #define BERT_REGION_MAX_SIZE 0
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060028#endif
29
30void bert_reserved_region(void **start, size_t *size)
31{
Julius Wernercd49cce2019-03-05 16:53:33 -080032 if (CONFIG(ACPI_BERT))
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060033 *start = cbmem_top();
34 else
Felix Held1b457f82020-04-19 00:02:01 +020035 *start = NULL;
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060036 *size = BERT_REGION_MAX_SIZE;
37}
38
Arthur Heymans340e4b82019-10-23 17:25:58 +020039void *cbmem_top_chipset(void)
Marshall Dawson94ee9372017-06-15 12:18:23 -060040{
41 msr_t tom = rdmsr(TOP_MEM);
42
43 if (!tom.lo)
44 return 0;
Richard Spiegel8c614f22018-10-23 14:53:23 -070045
46 /* 8MB alignment to keep MTRR usage low */
47 return (void *)ALIGN_DOWN(restore_top_of_low_cacheable()
48 - CONFIG_SMM_TSEG_SIZE
49 - BERT_REGION_MAX_SIZE, 8*MiB);
Marshall Dawsonb6172112017-09-13 17:47:31 -060050}
51
52static uintptr_t smm_region_start(void)
53{
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060054 return (uintptr_t)cbmem_top() + BERT_REGION_MAX_SIZE;
Marshall Dawsonb6172112017-09-13 17:47:31 -060055}
56
57static size_t smm_region_size(void)
58{
59 return CONFIG_SMM_TSEG_SIZE;
60}
61
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070062/*
63 * For data stored in TSEG, ensure TValid is clear so R/W access can reach
64 * the DRAM when not in SMM.
65 */
66static void clear_tvalid(void)
67{
68 msr_t hwcr = rdmsr(HWCR_MSR);
Elyes HAOUAS400ce552018-10-12 10:54:30 +020069 msr_t mask = rdmsr(SMM_MASK_MSR);
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070070 int tvalid = !!(mask.lo & SMM_TSEG_VALID);
71
72 if (hwcr.lo & SMM_LOCK) {
73 if (!tvalid) /* not valid but locked means still accessible */
74 return;
75
76 printk(BIOS_ERR, "Error: can't clear TValid, already locked\n");
77 return;
78 }
79
80 mask.lo &= ~SMM_TSEG_VALID;
Elyes HAOUAS400ce552018-10-12 10:54:30 +020081 wrmsr(SMM_MASK_MSR, mask);
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070082}
83
Kyösti Mälkki4913d8a2019-08-05 12:49:09 +030084void smm_region(uintptr_t *start, size_t *size)
Marshall Dawsonb6172112017-09-13 17:47:31 -060085{
Kyösti Mälkki544369e2019-08-06 22:14:34 +030086 static int once;
Marshall Dawsonb6172112017-09-13 17:47:31 -060087
Kyösti Mälkki4913d8a2019-08-05 12:49:09 +030088 *start = smm_region_start();
89 *size = smm_region_size();
Marshall Dawsonb6172112017-09-13 17:47:31 -060090
Kyösti Mälkki544369e2019-08-06 22:14:34 +030091 if (!once) {
92 clear_tvalid();
93 once = 1;
94 }
Marshall Dawson94ee9372017-06-15 12:18:23 -060095}