blob: d57fcface0123ced9d71469a35cc4f1bd714e2e6 [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>
Bin Menge41f0d22021-07-31 16:45:26 +080029#include <linux/log2.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070030
Bin Meng068fb352015-01-22 11:29:39 +080031DECLARE_GLOBAL_DATA_PTR;
32
Simon Glassfb842432023-07-15 21:38:36 -060033static const char *const mtrr_type_name[MTRR_TYPE_COUNT] = {
34 "Uncacheable",
35 "Combine",
36 "2",
37 "3",
38 "Through",
39 "Protect",
40 "Back",
41};
42
Simon Glass7bf5b9e2015-01-01 16:18:07 -070043/* Prepare to adjust MTRRs */
Simon Glass8fafd012018-10-01 12:22:37 -060044void mtrr_open(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070045{
Bin Meng80d29762015-01-22 11:29:41 +080046 if (!gd->arch.has_mtrr)
47 return;
48
Simon Glass8fafd012018-10-01 12:22:37 -060049 if (do_caches) {
50 state->enable_cache = dcache_status();
Simon Glass7bf5b9e2015-01-01 16:18:07 -070051
Simon Glass8fafd012018-10-01 12:22:37 -060052 if (state->enable_cache)
53 disable_caches();
54 }
Simon Glass7bf5b9e2015-01-01 16:18:07 -070055 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
56 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
57}
58
59/* Clean up after adjusting MTRRs, and enable them */
Simon Glass8fafd012018-10-01 12:22:37 -060060void mtrr_close(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070061{
Bin Meng80d29762015-01-22 11:29:41 +080062 if (!gd->arch.has_mtrr)
63 return;
64
Simon Glass7bf5b9e2015-01-01 16:18:07 -070065 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
Simon Glass8fafd012018-10-01 12:22:37 -060066 if (do_caches && state->enable_cache)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070067 enable_caches();
68}
69
Simon Glass35520592019-09-25 08:56:45 -060070static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
71{
72 u64 mask;
73
74 wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
75 mask = ~(size - 1);
76 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
77 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
78}
79
Simon Glass7403c262020-07-17 08:48:22 -060080void mtrr_read_all(struct mtrr_info *info)
81{
Simon Glassfbf120c2020-09-22 14:54:51 -060082 int reg_count = mtrr_get_var_count();
Simon Glass7403c262020-07-17 08:48:22 -060083 int i;
84
Simon Glassfbf120c2020-09-22 14:54:51 -060085 for (i = 0; i < reg_count; i++) {
Simon Glass7403c262020-07-17 08:48:22 -060086 info->mtrr[i].base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
87 info->mtrr[i].mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
88 }
89}
90
Simon Glass00dc52f2020-07-17 08:48:25 -060091void mtrr_write_all(struct mtrr_info *info)
92{
Simon Glassfbf120c2020-09-22 14:54:51 -060093 int reg_count = mtrr_get_var_count();
Simon Glass00dc52f2020-07-17 08:48:25 -060094 struct mtrr_state state;
95 int i;
96
Simon Glassfbf120c2020-09-22 14:54:51 -060097 for (i = 0; i < reg_count; i++) {
Simon Glass00dc52f2020-07-17 08:48:25 -060098 mtrr_open(&state, true);
99 wrmsrl(MTRR_PHYS_BASE_MSR(i), info->mtrr[i].base);
100 wrmsrl(MTRR_PHYS_MASK_MSR(i), info->mtrr[i].mask);
101 mtrr_close(&state, true);
102 }
103}
104
105static void write_mtrrs(void *arg)
106{
107 struct mtrr_info *info = arg;
108
109 mtrr_write_all(info);
110}
111
112static void read_mtrrs(void *arg)
113{
114 struct mtrr_info *info = arg;
115
116 mtrr_read_all(info);
117}
118
119/**
120 * mtrr_copy_to_aps() - Copy the MTRRs from the boot CPU to other CPUs
121 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100122 * Return: 0 on success, -ve on failure
Simon Glass00dc52f2020-07-17 08:48:25 -0600123 */
124static int mtrr_copy_to_aps(void)
125{
126 struct mtrr_info info;
127 int ret;
128
129 ret = mp_run_on_cpus(MP_SELECT_BSP, read_mtrrs, &info);
130 if (ret == -ENXIO)
131 return 0;
132 else if (ret)
133 return log_msg_ret("bsp", ret);
134
135 ret = mp_run_on_cpus(MP_SELECT_APS, write_mtrrs, &info);
136 if (ret)
137 return log_msg_ret("bsp", ret);
138
139 return 0;
140}
141
Simon Glass6b88e882020-09-22 12:45:27 -0600142static int h_comp_mtrr(const void *p1, const void *p2)
143{
144 const struct mtrr_request *req1 = p1;
145 const struct mtrr_request *req2 = p2;
146
147 s64 diff = req1->start - req2->start;
148
149 return diff < 0 ? -1 : diff > 0 ? 1 : 0;
150}
151
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700152int mtrr_commit(bool do_caches)
153{
154 struct mtrr_request *req = gd->arch.mtrr_req;
155 struct mtrr_state state;
Simon Glass00dc52f2020-07-17 08:48:25 -0600156 int ret;
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700157 int i;
158
Simon Glass8fafd012018-10-01 12:22:37 -0600159 debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
160 gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800161 if (!gd->arch.has_mtrr)
162 return -ENOSYS;
163
Simon Glass8fafd012018-10-01 12:22:37 -0600164 debug("open\n");
165 mtrr_open(&state, do_caches);
166 debug("open done\n");
Simon Glass6b88e882020-09-22 12:45:27 -0600167 qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr);
Simon Glass35520592019-09-25 08:56:45 -0600168 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
Bin Mengbe53d5e2021-07-31 16:45:25 +0800169 mtrr_set_next_var(req->type, req->start, req->size);
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700170
Simon Glass8fafd012018-10-01 12:22:37 -0600171 debug("close\n");
172 mtrr_close(&state, do_caches);
173 debug("mtrr done\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700174
Simon Glass00dc52f2020-07-17 08:48:25 -0600175 if (gd->flags & GD_FLG_RELOC) {
176 ret = mtrr_copy_to_aps();
177 if (ret)
178 return log_msg_ret("copy", ret);
179 }
180
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700181 return 0;
182}
183
184int mtrr_add_request(int type, uint64_t start, uint64_t size)
185{
186 struct mtrr_request *req;
187 uint64_t mask;
188
Simon Glass8fafd012018-10-01 12:22:37 -0600189 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800190 if (!gd->arch.has_mtrr)
191 return -ENOSYS;
192
Bin Menge41f0d22021-07-31 16:45:26 +0800193 if (!is_power_of_2(size))
194 return -EINVAL;
195
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700196 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
197 return -ENOSPC;
198 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
199 req->type = type;
200 req->start = start;
201 req->size = size;
202 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
203 req->type, req->start, req->size);
204 mask = ~(req->size - 1);
205 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
206 mask |= MTRR_PHYS_MASK_VALID;
207 debug(" %016llx %016llx\n", req->start | req->type, mask);
208
209 return 0;
210}
Simon Glass753297d2019-09-25 08:56:46 -0600211
Simon Glassfbf120c2020-09-22 14:54:51 -0600212int mtrr_get_var_count(void)
Simon Glass753297d2019-09-25 08:56:46 -0600213{
214 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
215}
216
217static int get_free_var_mtrr(void)
218{
219 struct msr_t maskm;
220 int vcnt;
221 int i;
222
Simon Glassfbf120c2020-09-22 14:54:51 -0600223 vcnt = mtrr_get_var_count();
Simon Glass753297d2019-09-25 08:56:46 -0600224
225 /* Identify the first var mtrr which is not valid */
226 for (i = 0; i < vcnt; i++) {
227 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
228 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
229 return i;
230 }
231
232 /* No free var mtrr */
233 return -ENOSPC;
234}
235
236int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
237{
238 int mtrr;
239
Bin Menge41f0d22021-07-31 16:45:26 +0800240 if (!is_power_of_2(size))
241 return -EINVAL;
242
Simon Glass753297d2019-09-25 08:56:46 -0600243 mtrr = get_free_var_mtrr();
244 if (mtrr < 0)
245 return mtrr;
246
247 set_var_mtrr(mtrr, type, start, size);
248 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
249
250 return 0;
251}
Simon Glassd89e15f2020-07-17 08:48:26 -0600252
253/** enum mtrr_opcode - supported operations for mtrr_do_oper() */
254enum mtrr_opcode {
255 MTRR_OP_SET,
256 MTRR_OP_SET_VALID,
257};
258
259/**
260 * struct mtrr_oper - An MTRR operation to perform on a CPU
261 *
262 * @opcode: Indicates operation to perform
263 * @reg: MTRR reg number to select (0-7, -1 = all)
264 * @valid: Valid value to write for MTRR_OP_SET_VALID
265 * @base: Base value to write for MTRR_OP_SET
266 * @mask: Mask value to write for MTRR_OP_SET
267 */
268struct mtrr_oper {
269 enum mtrr_opcode opcode;
270 int reg;
271 bool valid;
272 u64 base;
273 u64 mask;
274};
275
276static void mtrr_do_oper(void *arg)
277{
278 struct mtrr_oper *oper = arg;
279 u64 mask;
280
281 switch (oper->opcode) {
282 case MTRR_OP_SET_VALID:
283 mask = native_read_msr(MTRR_PHYS_MASK_MSR(oper->reg));
284 if (oper->valid)
285 mask |= MTRR_PHYS_MASK_VALID;
286 else
287 mask &= ~MTRR_PHYS_MASK_VALID;
288 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), mask);
289 break;
290 case MTRR_OP_SET:
291 wrmsrl(MTRR_PHYS_BASE_MSR(oper->reg), oper->base);
292 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), oper->mask);
293 break;
294 }
295}
296
297static int mtrr_start_op(int cpu_select, struct mtrr_oper *oper)
298{
299 struct mtrr_state state;
300 int ret;
301
302 mtrr_open(&state, true);
303 ret = mp_run_on_cpus(cpu_select, mtrr_do_oper, oper);
304 mtrr_close(&state, true);
305 if (ret)
306 return log_msg_ret("run", ret);
307
308 return 0;
309}
310
311int mtrr_set_valid(int cpu_select, int reg, bool valid)
312{
313 struct mtrr_oper oper;
314
315 oper.opcode = MTRR_OP_SET_VALID;
316 oper.reg = reg;
317 oper.valid = valid;
318
319 return mtrr_start_op(cpu_select, &oper);
320}
321
322int mtrr_set(int cpu_select, int reg, u64 base, u64 mask)
323{
324 struct mtrr_oper oper;
325
326 oper.opcode = MTRR_OP_SET;
327 oper.reg = reg;
328 oper.base = base;
329 oper.mask = mask;
330
331 return mtrr_start_op(cpu_select, &oper);
332}
Simon Glassfb842432023-07-15 21:38:36 -0600333
334static void read_mtrrs_(void *arg)
335{
336 struct mtrr_info *info = arg;
337
338 mtrr_read_all(info);
339}
340
341int mtrr_list(int reg_count, int cpu_select)
342{
343 struct mtrr_info info;
344 int ret;
345 int i;
346
347 printf("Reg Valid Write-type %-16s %-16s %-16s\n", "Base ||",
348 "Mask ||", "Size ||");
349 memset(&info, '\0', sizeof(info));
350 ret = mp_run_on_cpus(cpu_select, read_mtrrs_, &info);
351 if (ret)
352 return log_msg_ret("run", ret);
353 for (i = 0; i < reg_count; i++) {
354 const char *type = "Invalid";
355 u64 base, mask, size;
356 bool valid;
357
358 base = info.mtrr[i].base;
359 mask = info.mtrr[i].mask;
360 size = ~mask & ((1ULL << CONFIG_CPU_ADDR_BITS) - 1);
361 size |= (1 << 12) - 1;
362 size += 1;
363 valid = mask & MTRR_PHYS_MASK_VALID;
364 type = mtrr_type_name[base & MTRR_BASE_TYPE_MASK];
365 printf("%d %-5s %-12s %016llx %016llx %016llx\n", i,
366 valid ? "Y" : "N", type, base & ~MTRR_BASE_TYPE_MASK,
367 mask & ~MTRR_PHYS_MASK_VALID, size);
368 }
369
370 return 0;
371}
372
373int mtrr_get_type_by_name(const char *typename)
374{
375 int i;
376
377 for (i = 0; i < MTRR_TYPE_COUNT; i++) {
378 if (*typename == *mtrr_type_name[i])
379 return i;
380 }
381
382 return -EINVAL;
383};