blob: 166aff380c64c8c247d36a938508f284c98cfaa0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7bf5b9e2015-01-01 16:18:07 -07002/*
3 * (C) Copyright 2014 Google, Inc
4 *
Simon Glass7bf5b9e2015-01-01 16:18:07 -07005 * Memory Type Range Regsters - these are used to tell the CPU whether
6 * memory is cacheable and if so the cache write mode to use.
7 *
8 * These can speed up booting. See the mtrr command.
9 *
10 * Reference: Intel Architecture Software Developer's Manual, Volume 3:
11 * System Programming
12 */
13
Simon Glass8fafd012018-10-01 12:22:37 -060014/*
15 * Note that any console output (e.g. debug()) in this file will likely fail
16 * since the MTRR registers are sometimes in flux.
17 */
18
Simon Glass7bf5b9e2015-01-01 16:18:07 -070019#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070020#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glass6b88e882020-09-22 12:45:27 -060022#include <sort.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070025#include <asm/io.h>
Simon Glass7403c262020-07-17 08:48:22 -060026#include <asm/mp.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070027#include <asm/msr.h>
28#include <asm/mtrr.h>
29
Bin Meng068fb352015-01-22 11:29:39 +080030DECLARE_GLOBAL_DATA_PTR;
31
Simon Glass7bf5b9e2015-01-01 16:18:07 -070032/* Prepare to adjust MTRRs */
Simon Glass8fafd012018-10-01 12:22:37 -060033void mtrr_open(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070034{
Bin Meng80d29762015-01-22 11:29:41 +080035 if (!gd->arch.has_mtrr)
36 return;
37
Simon Glass8fafd012018-10-01 12:22:37 -060038 if (do_caches) {
39 state->enable_cache = dcache_status();
Simon Glass7bf5b9e2015-01-01 16:18:07 -070040
Simon Glass8fafd012018-10-01 12:22:37 -060041 if (state->enable_cache)
42 disable_caches();
43 }
Simon Glass7bf5b9e2015-01-01 16:18:07 -070044 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
45 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
46}
47
48/* Clean up after adjusting MTRRs, and enable them */
Simon Glass8fafd012018-10-01 12:22:37 -060049void mtrr_close(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070050{
Bin Meng80d29762015-01-22 11:29:41 +080051 if (!gd->arch.has_mtrr)
52 return;
53
Simon Glass7bf5b9e2015-01-01 16:18:07 -070054 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
Simon Glass8fafd012018-10-01 12:22:37 -060055 if (do_caches && state->enable_cache)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070056 enable_caches();
57}
58
Simon Glass35520592019-09-25 08:56:45 -060059static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
60{
61 u64 mask;
62
63 wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
64 mask = ~(size - 1);
65 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
66 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
67}
68
Simon Glass7403c262020-07-17 08:48:22 -060069void mtrr_read_all(struct mtrr_info *info)
70{
Simon Glassfbf120c2020-09-22 14:54:51 -060071 int reg_count = mtrr_get_var_count();
Simon Glass7403c262020-07-17 08:48:22 -060072 int i;
73
Simon Glassfbf120c2020-09-22 14:54:51 -060074 for (i = 0; i < reg_count; i++) {
Simon Glass7403c262020-07-17 08:48:22 -060075 info->mtrr[i].base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
76 info->mtrr[i].mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
77 }
78}
79
Simon Glass00dc52f2020-07-17 08:48:25 -060080void mtrr_write_all(struct mtrr_info *info)
81{
Simon Glassfbf120c2020-09-22 14:54:51 -060082 int reg_count = mtrr_get_var_count();
Simon Glass00dc52f2020-07-17 08:48:25 -060083 struct mtrr_state state;
84 int i;
85
Simon Glassfbf120c2020-09-22 14:54:51 -060086 for (i = 0; i < reg_count; i++) {
Simon Glass00dc52f2020-07-17 08:48:25 -060087 mtrr_open(&state, true);
88 wrmsrl(MTRR_PHYS_BASE_MSR(i), info->mtrr[i].base);
89 wrmsrl(MTRR_PHYS_MASK_MSR(i), info->mtrr[i].mask);
90 mtrr_close(&state, true);
91 }
92}
93
94static void write_mtrrs(void *arg)
95{
96 struct mtrr_info *info = arg;
97
98 mtrr_write_all(info);
99}
100
101static void read_mtrrs(void *arg)
102{
103 struct mtrr_info *info = arg;
104
105 mtrr_read_all(info);
106}
107
108/**
109 * mtrr_copy_to_aps() - Copy the MTRRs from the boot CPU to other CPUs
110 *
111 * @return 0 on success, -ve on failure
112 */
113static int mtrr_copy_to_aps(void)
114{
115 struct mtrr_info info;
116 int ret;
117
118 ret = mp_run_on_cpus(MP_SELECT_BSP, read_mtrrs, &info);
119 if (ret == -ENXIO)
120 return 0;
121 else if (ret)
122 return log_msg_ret("bsp", ret);
123
124 ret = mp_run_on_cpus(MP_SELECT_APS, write_mtrrs, &info);
125 if (ret)
126 return log_msg_ret("bsp", ret);
127
128 return 0;
129}
130
Simon Glass6b88e882020-09-22 12:45:27 -0600131static int h_comp_mtrr(const void *p1, const void *p2)
132{
133 const struct mtrr_request *req1 = p1;
134 const struct mtrr_request *req2 = p2;
135
136 s64 diff = req1->start - req2->start;
137
138 return diff < 0 ? -1 : diff > 0 ? 1 : 0;
139}
140
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700141int mtrr_commit(bool do_caches)
142{
143 struct mtrr_request *req = gd->arch.mtrr_req;
144 struct mtrr_state state;
Simon Glass00dc52f2020-07-17 08:48:25 -0600145 int ret;
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700146 int i;
147
Simon Glass8fafd012018-10-01 12:22:37 -0600148 debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
149 gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800150 if (!gd->arch.has_mtrr)
151 return -ENOSYS;
152
Simon Glass8fafd012018-10-01 12:22:37 -0600153 debug("open\n");
154 mtrr_open(&state, do_caches);
155 debug("open done\n");
Simon Glass6b88e882020-09-22 12:45:27 -0600156 qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr);
Simon Glass35520592019-09-25 08:56:45 -0600157 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
158 set_var_mtrr(i, req->type, req->start, req->size);
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700159
160 /* Clear the ones that are unused */
Simon Glass8fafd012018-10-01 12:22:37 -0600161 debug("clear\n");
Bin Meng761d8eb2020-11-09 15:55:49 +0800162 for (; i < mtrr_get_var_count(); i++)
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700163 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
Simon Glass8fafd012018-10-01 12:22:37 -0600164 debug("close\n");
165 mtrr_close(&state, do_caches);
166 debug("mtrr done\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700167
Simon Glass00dc52f2020-07-17 08:48:25 -0600168 if (gd->flags & GD_FLG_RELOC) {
169 ret = mtrr_copy_to_aps();
170 if (ret)
171 return log_msg_ret("copy", ret);
172 }
173
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700174 return 0;
175}
176
177int mtrr_add_request(int type, uint64_t start, uint64_t size)
178{
179 struct mtrr_request *req;
180 uint64_t mask;
181
Simon Glass8fafd012018-10-01 12:22:37 -0600182 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800183 if (!gd->arch.has_mtrr)
184 return -ENOSYS;
185
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700186 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
187 return -ENOSPC;
188 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
189 req->type = type;
190 req->start = start;
191 req->size = size;
192 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
193 req->type, req->start, req->size);
194 mask = ~(req->size - 1);
195 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
196 mask |= MTRR_PHYS_MASK_VALID;
197 debug(" %016llx %016llx\n", req->start | req->type, mask);
198
199 return 0;
200}
Simon Glass753297d2019-09-25 08:56:46 -0600201
Simon Glassfbf120c2020-09-22 14:54:51 -0600202int mtrr_get_var_count(void)
Simon Glass753297d2019-09-25 08:56:46 -0600203{
204 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
205}
206
207static int get_free_var_mtrr(void)
208{
209 struct msr_t maskm;
210 int vcnt;
211 int i;
212
Simon Glassfbf120c2020-09-22 14:54:51 -0600213 vcnt = mtrr_get_var_count();
Simon Glass753297d2019-09-25 08:56:46 -0600214
215 /* Identify the first var mtrr which is not valid */
216 for (i = 0; i < vcnt; i++) {
217 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
218 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
219 return i;
220 }
221
222 /* No free var mtrr */
223 return -ENOSPC;
224}
225
226int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
227{
228 int mtrr;
229
230 mtrr = get_free_var_mtrr();
231 if (mtrr < 0)
232 return mtrr;
233
234 set_var_mtrr(mtrr, type, start, size);
235 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
236
237 return 0;
238}
Simon Glassd89e15f2020-07-17 08:48:26 -0600239
240/** enum mtrr_opcode - supported operations for mtrr_do_oper() */
241enum mtrr_opcode {
242 MTRR_OP_SET,
243 MTRR_OP_SET_VALID,
244};
245
246/**
247 * struct mtrr_oper - An MTRR operation to perform on a CPU
248 *
249 * @opcode: Indicates operation to perform
250 * @reg: MTRR reg number to select (0-7, -1 = all)
251 * @valid: Valid value to write for MTRR_OP_SET_VALID
252 * @base: Base value to write for MTRR_OP_SET
253 * @mask: Mask value to write for MTRR_OP_SET
254 */
255struct mtrr_oper {
256 enum mtrr_opcode opcode;
257 int reg;
258 bool valid;
259 u64 base;
260 u64 mask;
261};
262
263static void mtrr_do_oper(void *arg)
264{
265 struct mtrr_oper *oper = arg;
266 u64 mask;
267
268 switch (oper->opcode) {
269 case MTRR_OP_SET_VALID:
270 mask = native_read_msr(MTRR_PHYS_MASK_MSR(oper->reg));
271 if (oper->valid)
272 mask |= MTRR_PHYS_MASK_VALID;
273 else
274 mask &= ~MTRR_PHYS_MASK_VALID;
275 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), mask);
276 break;
277 case MTRR_OP_SET:
278 wrmsrl(MTRR_PHYS_BASE_MSR(oper->reg), oper->base);
279 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), oper->mask);
280 break;
281 }
282}
283
284static int mtrr_start_op(int cpu_select, struct mtrr_oper *oper)
285{
286 struct mtrr_state state;
287 int ret;
288
289 mtrr_open(&state, true);
290 ret = mp_run_on_cpus(cpu_select, mtrr_do_oper, oper);
291 mtrr_close(&state, true);
292 if (ret)
293 return log_msg_ret("run", ret);
294
295 return 0;
296}
297
298int mtrr_set_valid(int cpu_select, int reg, bool valid)
299{
300 struct mtrr_oper oper;
301
302 oper.opcode = MTRR_OP_SET_VALID;
303 oper.reg = reg;
304 oper.valid = valid;
305
306 return mtrr_start_op(cpu_select, &oper);
307}
308
309int mtrr_set(int cpu_select, int reg, u64 base, u64 mask)
310{
311 struct mtrr_oper oper;
312
313 oper.opcode = MTRR_OP_SET;
314 oper.reg = reg;
315 oper.base = base;
316 oper.mask = mask;
317
318 return mtrr_start_op(cpu_select, &oper);
319}