1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 Google, Inc
5 * 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.
8 * These can speed up booting. See the mtrr command.
10 * Reference: Intel Architecture Software Developer's Manual, Volume 3:
15 * Note that any console output (e.g. debug()) in this file will likely fail
16 * since the MTRR registers are sometimes in flux.
23 #include <asm/cache.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 /* Prepare to adjust MTRRs */
32 void mtrr_open(struct mtrr_state *state, bool do_caches)
34 if (!gd->arch.has_mtrr)
38 state->enable_cache = dcache_status();
40 if (state->enable_cache)
43 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
44 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
47 /* Clean up after adjusting MTRRs, and enable them */
48 void mtrr_close(struct mtrr_state *state, bool do_caches)
50 if (!gd->arch.has_mtrr)
53 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
54 if (do_caches && state->enable_cache)
58 static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
62 wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
64 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
65 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
68 void mtrr_read_all(struct mtrr_info *info)
72 for (i = 0; i < MTRR_COUNT; i++) {
73 info->mtrr[i].base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
74 info->mtrr[i].mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
78 void mtrr_write_all(struct mtrr_info *info)
80 struct mtrr_state state;
83 for (i = 0; i < MTRR_COUNT; i++) {
84 mtrr_open(&state, true);
85 wrmsrl(MTRR_PHYS_BASE_MSR(i), info->mtrr[i].base);
86 wrmsrl(MTRR_PHYS_MASK_MSR(i), info->mtrr[i].mask);
87 mtrr_close(&state, true);
91 static void write_mtrrs(void *arg)
93 struct mtrr_info *info = arg;
98 static void read_mtrrs(void *arg)
100 struct mtrr_info *info = arg;
106 * mtrr_copy_to_aps() - Copy the MTRRs from the boot CPU to other CPUs
108 * @return 0 on success, -ve on failure
110 static int mtrr_copy_to_aps(void)
112 struct mtrr_info info;
115 ret = mp_run_on_cpus(MP_SELECT_BSP, read_mtrrs, &info);
119 return log_msg_ret("bsp", ret);
121 ret = mp_run_on_cpus(MP_SELECT_APS, write_mtrrs, &info);
123 return log_msg_ret("bsp", ret);
128 static int h_comp_mtrr(const void *p1, const void *p2)
130 const struct mtrr_request *req1 = p1;
131 const struct mtrr_request *req2 = p2;
133 s64 diff = req1->start - req2->start;
135 return diff < 0 ? -1 : diff > 0 ? 1 : 0;
138 int mtrr_commit(bool do_caches)
140 struct mtrr_request *req = gd->arch.mtrr_req;
141 struct mtrr_state state;
145 debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
146 gd->arch.mtrr_req_count);
147 if (!gd->arch.has_mtrr)
151 mtrr_open(&state, do_caches);
152 debug("open done\n");
153 qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr);
154 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
155 set_var_mtrr(i, req->type, req->start, req->size);
157 /* Clear the ones that are unused */
159 for (; i < MTRR_COUNT; i++)
160 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
162 mtrr_close(&state, do_caches);
163 debug("mtrr done\n");
165 if (gd->flags & GD_FLG_RELOC) {
166 ret = mtrr_copy_to_aps();
168 return log_msg_ret("copy", ret);
174 int mtrr_add_request(int type, uint64_t start, uint64_t size)
176 struct mtrr_request *req;
179 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
180 if (!gd->arch.has_mtrr)
183 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
185 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
189 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
190 req->type, req->start, req->size);
191 mask = ~(req->size - 1);
192 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
193 mask |= MTRR_PHYS_MASK_VALID;
194 debug(" %016llx %016llx\n", req->start | req->type, mask);
199 static int get_var_mtrr_count(void)
201 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
204 static int get_free_var_mtrr(void)
210 vcnt = get_var_mtrr_count();
212 /* Identify the first var mtrr which is not valid */
213 for (i = 0; i < vcnt; i++) {
214 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
215 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
219 /* No free var mtrr */
223 int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
227 mtrr = get_free_var_mtrr();
231 set_var_mtrr(mtrr, type, start, size);
232 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
237 /** enum mtrr_opcode - supported operations for mtrr_do_oper() */
244 * struct mtrr_oper - An MTRR operation to perform on a CPU
246 * @opcode: Indicates operation to perform
247 * @reg: MTRR reg number to select (0-7, -1 = all)
248 * @valid: Valid value to write for MTRR_OP_SET_VALID
249 * @base: Base value to write for MTRR_OP_SET
250 * @mask: Mask value to write for MTRR_OP_SET
253 enum mtrr_opcode opcode;
260 static void mtrr_do_oper(void *arg)
262 struct mtrr_oper *oper = arg;
265 switch (oper->opcode) {
266 case MTRR_OP_SET_VALID:
267 mask = native_read_msr(MTRR_PHYS_MASK_MSR(oper->reg));
269 mask |= MTRR_PHYS_MASK_VALID;
271 mask &= ~MTRR_PHYS_MASK_VALID;
272 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), mask);
275 wrmsrl(MTRR_PHYS_BASE_MSR(oper->reg), oper->base);
276 wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), oper->mask);
281 static int mtrr_start_op(int cpu_select, struct mtrr_oper *oper)
283 struct mtrr_state state;
286 mtrr_open(&state, true);
287 ret = mp_run_on_cpus(cpu_select, mtrr_do_oper, oper);
288 mtrr_close(&state, true);
290 return log_msg_ret("run", ret);
295 int mtrr_set_valid(int cpu_select, int reg, bool valid)
297 struct mtrr_oper oper;
299 oper.opcode = MTRR_OP_SET_VALID;
303 return mtrr_start_op(cpu_select, &oper);
306 int mtrr_set(int cpu_select, int reg, u64 base, u64 mask)
308 struct mtrr_oper oper;
310 oper.opcode = MTRR_OP_SET;
315 return mtrr_start_op(cpu_select, &oper);