blob: 15968abbe4789757a6882a245e3534ab9f77af16 [file] [log] [blame]
Marc Jones1587dc82017-05-15 18:55:11 -06001/*
2 * This file is part of the coreboot project.
3 *
Marshall Dawsonb6172112017-09-13 17:47:31 -06004 * Copyright (C) 2015 Intel Corp.
5 *
Marc Jones1587dc82017-05-15 18:55:11 -06006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#define __SIMPLE_DEVICE__
17
Marshall Dawsonb6172112017-09-13 17:47:31 -060018#include <assert.h>
Marc Jones1587dc82017-05-15 18:55:11 -060019#include <stdint.h>
20#include <arch/io.h>
Marshall Dawson94ee9372017-06-15 12:18:23 -060021#include <cpu/x86/msr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020022#include <cpu/amd/msr.h>
Marshall Dawson94ee9372017-06-15 12:18:23 -060023#include <cpu/amd/mtrr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060024#include <cbmem.h>
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070025#include <stage_cache.h>
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060026#include <arch/bert_storage.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060027#include <soc/northbridge.h>
Marshall Dawson22f54c52017-11-29 09:30:23 -070028#include <soc/southbridge.h>
Marc Jones1587dc82017-05-15 18:55:11 -060029
30void backup_top_of_low_cacheable(uintptr_t ramtop)
31{
Marshall Dawson22f54c52017-11-29 09:30:23 -070032 biosram_write32(BIOSRAM_CBMEM_TOP, ramtop);
Marc Jones1587dc82017-05-15 18:55:11 -060033}
34
35uintptr_t restore_top_of_low_cacheable(void)
36{
Marshall Dawson22f54c52017-11-29 09:30:23 -070037 return biosram_read32(BIOSRAM_CBMEM_TOP);
Marc Jones1587dc82017-05-15 18:55:11 -060038}
Marshall Dawson94ee9372017-06-15 12:18:23 -060039
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060040#if IS_ENABLED(CONFIG_ACPI_BERT)
Marshall Dawsonf0de2422018-09-10 13:28:49 -060041 #if CONFIG_SMM_TSEG_SIZE == 0x0
42 #define BERT_REGION_MAX_SIZE 0x100000
43 #else
44 /* SMM_TSEG_SIZE must stay on a boundary appropriate for its granularity */
45 #define BERT_REGION_MAX_SIZE CONFIG_SMM_TSEG_SIZE
46 #endif
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060047#else
Marshall Dawsonf0de2422018-09-10 13:28:49 -060048 #define BERT_REGION_MAX_SIZE 0
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060049#endif
50
51void bert_reserved_region(void **start, size_t *size)
52{
53 if (IS_ENABLED(CONFIG_ACPI_BERT))
54 *start = cbmem_top();
55 else
56 start = NULL;
57 *size = BERT_REGION_MAX_SIZE;
58}
59
Marshall Dawson94ee9372017-06-15 12:18:23 -060060void *cbmem_top(void)
61{
62 msr_t tom = rdmsr(TOP_MEM);
63
64 if (!tom.lo)
65 return 0;
Richard Spiegel8c614f22018-10-23 14:53:23 -070066
67 /* 8MB alignment to keep MTRR usage low */
68 return (void *)ALIGN_DOWN(restore_top_of_low_cacheable()
69 - CONFIG_SMM_TSEG_SIZE
70 - BERT_REGION_MAX_SIZE, 8*MiB);
Marshall Dawsonb6172112017-09-13 17:47:31 -060071}
72
73static uintptr_t smm_region_start(void)
74{
Marshall Dawson4b0f6fa2018-09-04 13:08:25 -060075 return (uintptr_t)cbmem_top() + BERT_REGION_MAX_SIZE;
Marshall Dawsonb6172112017-09-13 17:47:31 -060076}
77
78static size_t smm_region_size(void)
79{
80 return CONFIG_SMM_TSEG_SIZE;
81}
82
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070083void stage_cache_external_region(void **base, size_t *size)
84{
85 if (smm_subregion(SMM_SUBREGION_CACHE, base, size)) {
86 printk(BIOS_ERR, "ERROR: No cache SMM subregion.\n");
87 *base = NULL;
88 *size = 0;
89 }
90}
91
Marshall Dawsonb6172112017-09-13 17:47:31 -060092void smm_region_info(void **start, size_t *size)
93{
94 *start = (void *)smm_region_start();
95 *size = smm_region_size();
96}
97
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -070098/*
99 * For data stored in TSEG, ensure TValid is clear so R/W access can reach
100 * the DRAM when not in SMM.
101 */
102static void clear_tvalid(void)
103{
104 msr_t hwcr = rdmsr(HWCR_MSR);
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200105 msr_t mask = rdmsr(SMM_MASK_MSR);
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -0700106 int tvalid = !!(mask.lo & SMM_TSEG_VALID);
107
108 if (hwcr.lo & SMM_LOCK) {
109 if (!tvalid) /* not valid but locked means still accessible */
110 return;
111
112 printk(BIOS_ERR, "Error: can't clear TValid, already locked\n");
113 return;
114 }
115
116 mask.lo &= ~SMM_TSEG_VALID;
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200117 wrmsr(SMM_MASK_MSR, mask);
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -0700118}
119
Marshall Dawsonb6172112017-09-13 17:47:31 -0600120int smm_subregion(int sub, void **start, size_t *size)
121{
122 uintptr_t sub_base;
123 size_t sub_size;
124 const size_t cache_size = CONFIG_SMM_RESERVED_SIZE;
125
126 sub_base = smm_region_start();
127 sub_size = smm_region_size();
128
129 assert(sub_size > CONFIG_SMM_RESERVED_SIZE);
130
131 switch (sub) {
132 case SMM_SUBREGION_HANDLER:
133 /* Handler starts at the base of TSEG. */
134 sub_size -= cache_size;
135 break;
136 case SMM_SUBREGION_CACHE:
137 /* External cache is in the middle of TSEG. */
138 sub_base += sub_size - cache_size;
139 sub_size = cache_size;
Marshall Dawsonf3c57a7c2018-01-29 18:08:16 -0700140 clear_tvalid();
Marshall Dawsonb6172112017-09-13 17:47:31 -0600141 break;
142 default:
143 return -1;
144 }
145
146 *start = (void *)sub_base;
147 *size = sub_size;
148
149 return 0;
Marshall Dawson94ee9372017-06-15 12:18:23 -0600150}