blob: 9c24ae984e907340dde770cd819128e91aff46aa [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
Tom Riniabb9a042024-05-18 20:20:43 -060019#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++)
Simon Glass75324ab2023-07-31 14:01:08 +0800169 set_var_mtrr(i, req->type, req->start, req->size);
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700170
Simon Glass75324ab2023-07-31 14:01:08 +0800171 /* Clear the ones that are unused */
172 debug("clear\n");
173 for (; i < mtrr_get_var_count(); i++)
174 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
Simon Glass8fafd012018-10-01 12:22:37 -0600175 debug("close\n");
176 mtrr_close(&state, do_caches);
177 debug("mtrr done\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700178
Simon Glass00dc52f2020-07-17 08:48:25 -0600179 if (gd->flags & GD_FLG_RELOC) {
180 ret = mtrr_copy_to_aps();
181 if (ret)
182 return log_msg_ret("copy", ret);
183 }
184
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700185 return 0;
186}
187
188int mtrr_add_request(int type, uint64_t start, uint64_t size)
189{
190 struct mtrr_request *req;
191 uint64_t mask;
192
Simon Glass8fafd012018-10-01 12:22:37 -0600193 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800194 if (!gd->arch.has_mtrr)
195 return -ENOSYS;
196
Bin Menge41f0d22021-07-31 16:45:26 +0800197 if (!is_power_of_2(size))
198 return -EINVAL;
199
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700200 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
201 return -ENOSPC;
202 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
203 req->type = type;
204 req->start = start;
205 req->size = size;
206 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
207 req->type, req->start, req->size);
208 mask = ~(req->size - 1);
209 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
210 mask |= MTRR_PHYS_MASK_VALID;
211 debug(" %016llx %016llx\n", req->start | req->type, mask);
212
213 return 0;
214}
Simon Glass753297d2019-09-25 08:56:46 -0600215
Simon Glassfbf120c2020-09-22 14:54:51 -0600216int mtrr_get_var_count(void)
Simon Glass753297d2019-09-25 08:56:46 -0600217{
218 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
219}
220
221static int get_free_var_mtrr(void)
222{
223 struct msr_t maskm;
224 int vcnt;
225 int i;
226
Simon Glassfbf120c2020-09-22 14:54:51 -0600227 vcnt = mtrr_get_var_count();
Simon Glass753297d2019-09-25 08:56:46 -0600228
229 /* Identify the first var mtrr which is not valid */
230 for (i = 0; i < vcnt; i++) {
231 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
232 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
233 return i;
234 }
235
236 /* No free var mtrr */
237 return -ENOSPC;
238}
239
240int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
241{
242 int mtrr;
243
Bin Menge41f0d22021-07-31 16:45:26 +0800244 if (!is_power_of_2(size))
245 return -EINVAL;
246
Simon Glass753297d2019-09-25 08:56:46 -0600247 mtrr = get_free_var_mtrr();
248 if (mtrr < 0)
249 return mtrr;
250
251 set_var_mtrr(mtrr, type, start, size);
252 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
253
254 return 0;
255}
Simon Glassd89e15f2020-07-17 08:48:26 -0600256
257/** enum mtrr_opcode - supported operations for mtrr_do_oper() */
258enum mtrr_opcode {
259 MTRR_OP_SET,
260 MTRR_OP_SET_VALID,
261};
262
263/**
264 * struct mtrr_oper - An MTRR operation to perform on a CPU
265 *
266 * @opcode: Indicates operation to perform
267 * @reg: MTRR reg number to select (0-7, -1 = all)
268 * @valid: Valid value to write for MTRR_OP_SET_VALID
269 * @base: Base value to write for MTRR_OP_SET
270 * @mask: Mask value to write for MTRR_OP_SET
271 */
272struct mtrr_oper {
273 enum mtrr_opcode opcode;
274 int reg;
275 bool valid;
276 u64 base;
277 u64 mask;
278};
279
280static void mtrr_do_oper(void *arg)
281{
282 struct mtrr_oper *oper = arg;
283 u64 mask;
284
285 switch (oper->opcode) {
286 case MTRR_OP_SET_VALID:
287 mask = native_read_msr(MTRR_PHYS_MASK_MSR(oper->reg));
288 if (oper->valid)
289 mask |= MTRR_PHYS_MASK_VALID;
290 else
291 mask &= ~MTRR_PHYS_MASK_VALID;
292 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), mask);
293 break;
294 case MTRR_OP_SET:
295 wrmsrl(MTRR_PHYS_BASE_MSR(oper->reg), oper->base);
296 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), oper->mask);
297 break;
298 }
299}
300
301static int mtrr_start_op(int cpu_select, struct mtrr_oper *oper)
302{
303 struct mtrr_state state;
304 int ret;
305
306 mtrr_open(&state, true);
307 ret = mp_run_on_cpus(cpu_select, mtrr_do_oper, oper);
308 mtrr_close(&state, true);
309 if (ret)
310 return log_msg_ret("run", ret);
311
312 return 0;
313}
314
315int mtrr_set_valid(int cpu_select, int reg, bool valid)
316{
317 struct mtrr_oper oper;
318
319 oper.opcode = MTRR_OP_SET_VALID;
320 oper.reg = reg;
321 oper.valid = valid;
322
323 return mtrr_start_op(cpu_select, &oper);
324}
325
326int mtrr_set(int cpu_select, int reg, u64 base, u64 mask)
327{
328 struct mtrr_oper oper;
329
330 oper.opcode = MTRR_OP_SET;
331 oper.reg = reg;
332 oper.base = base;
333 oper.mask = mask;
334
335 return mtrr_start_op(cpu_select, &oper);
336}
Simon Glassfb842432023-07-15 21:38:36 -0600337
338static void read_mtrrs_(void *arg)
339{
340 struct mtrr_info *info = arg;
341
342 mtrr_read_all(info);
343}
344
345int mtrr_list(int reg_count, int cpu_select)
346{
347 struct mtrr_info info;
348 int ret;
349 int i;
350
351 printf("Reg Valid Write-type %-16s %-16s %-16s\n", "Base ||",
352 "Mask ||", "Size ||");
353 memset(&info, '\0', sizeof(info));
354 ret = mp_run_on_cpus(cpu_select, read_mtrrs_, &info);
355 if (ret)
356 return log_msg_ret("run", ret);
357 for (i = 0; i < reg_count; i++) {
358 const char *type = "Invalid";
359 u64 base, mask, size;
360 bool valid;
361
362 base = info.mtrr[i].base;
363 mask = info.mtrr[i].mask;
364 size = ~mask & ((1ULL << CONFIG_CPU_ADDR_BITS) - 1);
365 size |= (1 << 12) - 1;
366 size += 1;
367 valid = mask & MTRR_PHYS_MASK_VALID;
368 type = mtrr_type_name[base & MTRR_BASE_TYPE_MASK];
369 printf("%d %-5s %-12s %016llx %016llx %016llx\n", i,
370 valid ? "Y" : "N", type, base & ~MTRR_BASE_TYPE_MASK,
371 mask & ~MTRR_PHYS_MASK_VALID, size);
372 }
373
374 return 0;
375}
376
377int mtrr_get_type_by_name(const char *typename)
378{
379 int i;
380
381 for (i = 0; i < MTRR_TYPE_COUNT; i++) {
382 if (*typename == *mtrr_type_name[i])
383 return i;
384 }
385
386 return -EINVAL;
387};