blob: 67a431976c245e9ab82cb3cd897369bf9f49cda3 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Marc Jones1587dc82017-05-15 18:55:11 -06002
3#define __SIMPLE_DEVICE__
4
Marshall Dawsonb6172112017-09-13 17:47:31 -06005#include <assert.h>
Marc Jones1587dc82017-05-15 18:55:11 -06006#include <stdint.h>
Elyes HAOUAS20eaef02019-03-29 17:45:28 +01007#include <console/console.h>
Marshall Dawson94ee9372017-06-15 12:18:23 -06008#include <cpu/x86/msr.h>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +03009#include <cpu/x86/smm.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020010#include <cpu/amd/msr.h>
Marshall Dawson94ee9372017-06-15 12:18:23 -060011#include <cpu/amd/mtrr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060012#include <cbmem.h>
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060013#include <arch/bert_storage.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060014#include <soc/northbridge.h>
Marshall Dawson69486ca2019-05-02 12:03:45 -060015#include <soc/iomap.h>
Kyösti Mälkki2446c1e2020-07-09 07:13:37 +030016#include <amdblocks/biosram.h>
Marc Jones1587dc82017-05-15 18:55:11 -060017
Julius Wernercd49cce2019-03-05 16:53:33 -080018#if CONFIG(ACPI_BERT)
Marshall Dawsonf0de2422018-09-10 13:28:49 -060019 #if CONFIG_SMM_TSEG_SIZE == 0x0
20 #define BERT_REGION_MAX_SIZE 0x100000
21 #else
22 /* SMM_TSEG_SIZE must stay on a boundary appropriate for its granularity */
23 #define BERT_REGION_MAX_SIZE CONFIG_SMM_TSEG_SIZE
24 #endif
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060025#else
Marshall Dawsonf0de2422018-09-10 13:28:49 -060026 #define BERT_REGION_MAX_SIZE 0
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060027#endif
28
29void bert_reserved_region(void **start, size_t *size)
30{
Julius Wernercd49cce2019-03-05 16:53:33 -080031 if (CONFIG(ACPI_BERT))
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060032 *start = cbmem_top();
33 else
Felix Held1b457f82020-04-19 00:02:01 +020034 *start = NULL;
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060035 *size = BERT_REGION_MAX_SIZE;
36}
37
Arthur Heymans340e4b82019-10-23 17:25:58 +020038void *cbmem_top_chipset(void)
Marshall Dawson94ee9372017-06-15 12:18:23 -060039{
40 msr_t tom = rdmsr(TOP_MEM);
41
42 if (!tom.lo)
43 return 0;
Richard Spiegel8c614f22018-10-23 14:53:23 -070044
45 /* 8MB alignment to keep MTRR usage low */
46 return (void *)ALIGN_DOWN(restore_top_of_low_cacheable()
47 - CONFIG_SMM_TSEG_SIZE
48 - BERT_REGION_MAX_SIZE, 8*MiB);
Marshall Dawsonb6172112017-09-13 17:47:31 -060049}
50
51static uintptr_t smm_region_start(void)
52{
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060053 return (uintptr_t)cbmem_top() + BERT_REGION_MAX_SIZE;
Marshall Dawsonb6172112017-09-13 17:47:31 -060054}
55
56static size_t smm_region_size(void)
57{
58 return CONFIG_SMM_TSEG_SIZE;
59}
60
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070061/*
62 * For data stored in TSEG, ensure TValid is clear so R/W access can reach
63 * the DRAM when not in SMM.
64 */
65static void clear_tvalid(void)
66{
67 msr_t hwcr = rdmsr(HWCR_MSR);
Elyes HAOUAS400ce552018-10-12 10:54:30 +020068 msr_t mask = rdmsr(SMM_MASK_MSR);
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070069 int tvalid = !!(mask.lo & SMM_TSEG_VALID);
70
71 if (hwcr.lo & SMM_LOCK) {
72 if (!tvalid) /* not valid but locked means still accessible */
73 return;
74
75 printk(BIOS_ERR, "Error: can't clear TValid, already locked\n");
76 return;
77 }
78
79 mask.lo &= ~SMM_TSEG_VALID;
Elyes HAOUAS400ce552018-10-12 10:54:30 +020080 wrmsr(SMM_MASK_MSR, mask);
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070081}
82
Kyösti Mälkki4913d8a2019-08-05 12:49:09 +030083void smm_region(uintptr_t *start, size_t *size)
Marshall Dawsonb6172112017-09-13 17:47:31 -060084{
Kyösti Mälkki544369e2019-08-06 22:14:34 +030085 static int once;
Marshall Dawsonb6172112017-09-13 17:47:31 -060086
Kyösti Mälkki4913d8a2019-08-05 12:49:09 +030087 *start = smm_region_start();
88 *size = smm_region_size();
Marshall Dawsonb6172112017-09-13 17:47:31 -060089
Kyösti Mälkki544369e2019-08-06 22:14:34 +030090 if (!once) {
91 clear_tvalid();
92 once = 1;
93 }
Marshall Dawson94ee9372017-06-15 12:18:23 -060094}