blob: 07ea89162dee2ea50a5a56e8a3deaf759966e394 [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 Glass1d91ba72019-11-14 12:57:37 -070019#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Simon Glass6b88e882020-09-22 12:45:27 -060021#include <sort.h>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070024#include <asm/io.h>
Simon Glass7403c262020-07-17 08:48:22 -060025#include <asm/mp.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070026#include <asm/msr.h>
27#include <asm/mtrr.h>
Bin Menge41f0d22021-07-31 16:45:26 +080028#include <linux/log2.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070029
Bin Meng068fb352015-01-22 11:29:39 +080030DECLARE_GLOBAL_DATA_PTR;
31
Simon Glassfb842432023-07-15 21:38:36 -060032static const char *const mtrr_type_name[MTRR_TYPE_COUNT] = {
33 "Uncacheable",
34 "Combine",
35 "2",
36 "3",
37 "Through",
38 "Protect",
39 "Back",
40};
41
Simon Glass7bf5b9e2015-01-01 16:18:07 -070042/* Prepare to adjust MTRRs */
Simon Glass8fafd012018-10-01 12:22:37 -060043void mtrr_open(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070044{
Bin Meng80d29762015-01-22 11:29:41 +080045 if (!gd->arch.has_mtrr)
46 return;
47
Simon Glass8fafd012018-10-01 12:22:37 -060048 if (do_caches) {
49 state->enable_cache = dcache_status();
Simon Glass7bf5b9e2015-01-01 16:18:07 -070050
Simon Glass8fafd012018-10-01 12:22:37 -060051 if (state->enable_cache)
52 disable_caches();
53 }
Simon Glass7bf5b9e2015-01-01 16:18:07 -070054 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
55 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
56}
57
58/* Clean up after adjusting MTRRs, and enable them */
Simon Glass8fafd012018-10-01 12:22:37 -060059void mtrr_close(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070060{
Bin Meng80d29762015-01-22 11:29:41 +080061 if (!gd->arch.has_mtrr)
62 return;
63
Simon Glass7bf5b9e2015-01-01 16:18:07 -070064 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
Simon Glass8fafd012018-10-01 12:22:37 -060065 if (do_caches && state->enable_cache)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070066 enable_caches();
67}
68
Simon Glass35520592019-09-25 08:56:45 -060069static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
70{
71 u64 mask;
72
73 wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
74 mask = ~(size - 1);
75 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
76 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
77}
78
Simon Glass7403c262020-07-17 08:48:22 -060079void mtrr_read_all(struct mtrr_info *info)
80{
Simon Glassfbf120c2020-09-22 14:54:51 -060081 int reg_count = mtrr_get_var_count();
Simon Glass7403c262020-07-17 08:48:22 -060082 int i;
83
Simon Glassfbf120c2020-09-22 14:54:51 -060084 for (i = 0; i < reg_count; i++) {
Simon Glass7403c262020-07-17 08:48:22 -060085 info->mtrr[i].base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
86 info->mtrr[i].mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
87 }
88}
89
Andy Shevchenko31731422024-10-05 22:11:58 +030090static void mtrr_write_all(struct mtrr_info *info)
Simon Glass00dc52f2020-07-17 08:48:25 -060091{
Simon Glassfbf120c2020-09-22 14:54:51 -060092 int reg_count = mtrr_get_var_count();
Simon Glass00dc52f2020-07-17 08:48:25 -060093 struct mtrr_state state;
94 int i;
95
Simon Glassfbf120c2020-09-22 14:54:51 -060096 for (i = 0; i < reg_count; i++) {
Simon Glass00dc52f2020-07-17 08:48:25 -060097 mtrr_open(&state, true);
98 wrmsrl(MTRR_PHYS_BASE_MSR(i), info->mtrr[i].base);
99 wrmsrl(MTRR_PHYS_MASK_MSR(i), info->mtrr[i].mask);
100 mtrr_close(&state, true);
101 }
102}
103
104static void write_mtrrs(void *arg)
105{
106 struct mtrr_info *info = arg;
107
108 mtrr_write_all(info);
109}
110
111static void read_mtrrs(void *arg)
112{
113 struct mtrr_info *info = arg;
114
115 mtrr_read_all(info);
116}
117
118/**
119 * mtrr_copy_to_aps() - Copy the MTRRs from the boot CPU to other CPUs
120 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100121 * Return: 0 on success, -ve on failure
Simon Glass00dc52f2020-07-17 08:48:25 -0600122 */
123static int mtrr_copy_to_aps(void)
124{
125 struct mtrr_info info;
126 int ret;
127
128 ret = mp_run_on_cpus(MP_SELECT_BSP, read_mtrrs, &info);
129 if (ret == -ENXIO)
130 return 0;
131 else if (ret)
132 return log_msg_ret("bsp", ret);
133
134 ret = mp_run_on_cpus(MP_SELECT_APS, write_mtrrs, &info);
135 if (ret)
136 return log_msg_ret("bsp", ret);
137
138 return 0;
139}
140
Simon Glass6b88e882020-09-22 12:45:27 -0600141static int h_comp_mtrr(const void *p1, const void *p2)
142{
143 const struct mtrr_request *req1 = p1;
144 const struct mtrr_request *req2 = p2;
145
146 s64 diff = req1->start - req2->start;
147
148 return diff < 0 ? -1 : diff > 0 ? 1 : 0;
149}
150
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700151int mtrr_commit(bool do_caches)
152{
153 struct mtrr_request *req = gd->arch.mtrr_req;
154 struct mtrr_state state;
Simon Glass00dc52f2020-07-17 08:48:25 -0600155 int ret;
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700156 int i;
157
Simon Glass8fafd012018-10-01 12:22:37 -0600158 debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
159 gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800160 if (!gd->arch.has_mtrr)
161 return -ENOSYS;
162
Simon Glass8fafd012018-10-01 12:22:37 -0600163 debug("open\n");
164 mtrr_open(&state, do_caches);
165 debug("open done\n");
Simon Glass6b88e882020-09-22 12:45:27 -0600166 qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr);
Simon Glass35520592019-09-25 08:56:45 -0600167 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
Simon Glass75324ab2023-07-31 14:01:08 +0800168 set_var_mtrr(i, req->type, req->start, req->size);
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700169
Simon Glass75324ab2023-07-31 14:01:08 +0800170 /* Clear the ones that are unused */
171 debug("clear\n");
172 for (; i < mtrr_get_var_count(); i++)
173 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
Simon Glass8fafd012018-10-01 12:22:37 -0600174 debug("close\n");
175 mtrr_close(&state, do_caches);
176 debug("mtrr done\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700177
Simon Glass00dc52f2020-07-17 08:48:25 -0600178 if (gd->flags & GD_FLG_RELOC) {
179 ret = mtrr_copy_to_aps();
180 if (ret)
181 return log_msg_ret("copy", ret);
182 }
183
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700184 return 0;
185}
186
187int mtrr_add_request(int type, uint64_t start, uint64_t size)
188{
189 struct mtrr_request *req;
190 uint64_t mask;
191
Simon Glass8fafd012018-10-01 12:22:37 -0600192 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800193 if (!gd->arch.has_mtrr)
194 return -ENOSYS;
195
Bin Menge41f0d22021-07-31 16:45:26 +0800196 if (!is_power_of_2(size))
197 return -EINVAL;
198
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700199 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
200 return -ENOSPC;
201 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
202 req->type = type;
203 req->start = start;
204 req->size = size;
205 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
206 req->type, req->start, req->size);
207 mask = ~(req->size - 1);
208 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
209 mask |= MTRR_PHYS_MASK_VALID;
210 debug(" %016llx %016llx\n", req->start | req->type, mask);
211
212 return 0;
213}
Simon Glass753297d2019-09-25 08:56:46 -0600214
Simon Glassfbf120c2020-09-22 14:54:51 -0600215int mtrr_get_var_count(void)
Simon Glass753297d2019-09-25 08:56:46 -0600216{
217 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
218}
219
220static int get_free_var_mtrr(void)
221{
222 struct msr_t maskm;
223 int vcnt;
224 int i;
225
Simon Glassfbf120c2020-09-22 14:54:51 -0600226 vcnt = mtrr_get_var_count();
Simon Glass753297d2019-09-25 08:56:46 -0600227
228 /* Identify the first var mtrr which is not valid */
229 for (i = 0; i < vcnt; i++) {
230 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
231 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
232 return i;
233 }
234
235 /* No free var mtrr */
236 return -ENOSPC;
237}
238
239int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
240{
241 int mtrr;
242
Bin Menge41f0d22021-07-31 16:45:26 +0800243 if (!is_power_of_2(size))
244 return -EINVAL;
245
Simon Glass753297d2019-09-25 08:56:46 -0600246 mtrr = get_free_var_mtrr();
247 if (mtrr < 0)
248 return mtrr;
249
250 set_var_mtrr(mtrr, type, start, size);
251 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
252
253 return 0;
254}
Simon Glassd89e15f2020-07-17 08:48:26 -0600255
256/** enum mtrr_opcode - supported operations for mtrr_do_oper() */
257enum mtrr_opcode {
258 MTRR_OP_SET,
259 MTRR_OP_SET_VALID,
260};
261
262/**
263 * struct mtrr_oper - An MTRR operation to perform on a CPU
264 *
265 * @opcode: Indicates operation to perform
266 * @reg: MTRR reg number to select (0-7, -1 = all)
267 * @valid: Valid value to write for MTRR_OP_SET_VALID
268 * @base: Base value to write for MTRR_OP_SET
269 * @mask: Mask value to write for MTRR_OP_SET
270 */
271struct mtrr_oper {
272 enum mtrr_opcode opcode;
273 int reg;
274 bool valid;
275 u64 base;
276 u64 mask;
277};
278
279static void mtrr_do_oper(void *arg)
280{
281 struct mtrr_oper *oper = arg;
282 u64 mask;
283
284 switch (oper->opcode) {
285 case MTRR_OP_SET_VALID:
286 mask = native_read_msr(MTRR_PHYS_MASK_MSR(oper->reg));
287 if (oper->valid)
288 mask |= MTRR_PHYS_MASK_VALID;
289 else
290 mask &= ~MTRR_PHYS_MASK_VALID;
291 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), mask);
292 break;
293 case MTRR_OP_SET:
294 wrmsrl(MTRR_PHYS_BASE_MSR(oper->reg), oper->base);
295 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), oper->mask);
296 break;
297 }
298}
299
300static int mtrr_start_op(int cpu_select, struct mtrr_oper *oper)
301{
302 struct mtrr_state state;
303 int ret;
304
305 mtrr_open(&state, true);
306 ret = mp_run_on_cpus(cpu_select, mtrr_do_oper, oper);
307 mtrr_close(&state, true);
308 if (ret)
309 return log_msg_ret("run", ret);
310
311 return 0;
312}
313
314int mtrr_set_valid(int cpu_select, int reg, bool valid)
315{
316 struct mtrr_oper oper;
317
318 oper.opcode = MTRR_OP_SET_VALID;
319 oper.reg = reg;
320 oper.valid = valid;
321
322 return mtrr_start_op(cpu_select, &oper);
323}
324
325int mtrr_set(int cpu_select, int reg, u64 base, u64 mask)
326{
327 struct mtrr_oper oper;
328
329 oper.opcode = MTRR_OP_SET;
330 oper.reg = reg;
331 oper.base = base;
332 oper.mask = mask;
333
334 return mtrr_start_op(cpu_select, &oper);
335}
Simon Glassfb842432023-07-15 21:38:36 -0600336
337static void read_mtrrs_(void *arg)
338{
339 struct mtrr_info *info = arg;
340
341 mtrr_read_all(info);
342}
343
344int mtrr_list(int reg_count, int cpu_select)
345{
346 struct mtrr_info info;
347 int ret;
348 int i;
349
350 printf("Reg Valid Write-type %-16s %-16s %-16s\n", "Base ||",
351 "Mask ||", "Size ||");
352 memset(&info, '\0', sizeof(info));
353 ret = mp_run_on_cpus(cpu_select, read_mtrrs_, &info);
354 if (ret)
355 return log_msg_ret("run", ret);
356 for (i = 0; i < reg_count; i++) {
357 const char *type = "Invalid";
358 u64 base, mask, size;
359 bool valid;
360
361 base = info.mtrr[i].base;
362 mask = info.mtrr[i].mask;
363 size = ~mask & ((1ULL << CONFIG_CPU_ADDR_BITS) - 1);
364 size |= (1 << 12) - 1;
365 size += 1;
366 valid = mask & MTRR_PHYS_MASK_VALID;
367 type = mtrr_type_name[base & MTRR_BASE_TYPE_MASK];
368 printf("%d %-5s %-12s %016llx %016llx %016llx\n", i,
369 valid ? "Y" : "N", type, base & ~MTRR_BASE_TYPE_MASK,
370 mask & ~MTRR_PHYS_MASK_VALID, size);
371 }
372
373 return 0;
374}
375
376int mtrr_get_type_by_name(const char *typename)
377{
378 int i;
379
380 for (i = 0; i < MTRR_TYPE_COUNT; i++) {
381 if (*typename == *mtrr_type_name[i])
382 return i;
383 }
384
385 return -EINVAL;
386};