• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014 Google, Inc
4  *
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.
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 
14 /*
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 
19 #include <common.h>
20 #include <cpu_func.h>
21 #include <asm/io.h>
22 #include <asm/msr.h>
23 #include <asm/mtrr.h>
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 /* Prepare to adjust MTRRs */
mtrr_open(struct mtrr_state * state,bool do_caches)28 void mtrr_open(struct mtrr_state *state, bool do_caches)
29 {
30 	if (!gd->arch.has_mtrr)
31 		return;
32 
33 	if (do_caches) {
34 		state->enable_cache = dcache_status();
35 
36 		if (state->enable_cache)
37 			disable_caches();
38 	}
39 	state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
40 	wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
41 }
42 
43 /* Clean up after adjusting MTRRs, and enable them */
mtrr_close(struct mtrr_state * state,bool do_caches)44 void mtrr_close(struct mtrr_state *state, bool do_caches)
45 {
46 	if (!gd->arch.has_mtrr)
47 		return;
48 
49 	wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
50 	if (do_caches && state->enable_cache)
51 		enable_caches();
52 }
53 
set_var_mtrr(uint reg,uint type,uint64_t start,uint64_t size)54 static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
55 {
56 	u64 mask;
57 
58 	wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
59 	mask = ~(size - 1);
60 	mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
61 	wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
62 }
63 
mtrr_commit(bool do_caches)64 int mtrr_commit(bool do_caches)
65 {
66 	struct mtrr_request *req = gd->arch.mtrr_req;
67 	struct mtrr_state state;
68 	int i;
69 
70 	debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
71 	      gd->arch.mtrr_req_count);
72 	if (!gd->arch.has_mtrr)
73 		return -ENOSYS;
74 
75 	debug("open\n");
76 	mtrr_open(&state, do_caches);
77 	debug("open done\n");
78 	for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
79 		set_var_mtrr(i, req->type, req->start, req->size);
80 
81 	/* Clear the ones that are unused */
82 	debug("clear\n");
83 	for (; i < MTRR_COUNT; i++)
84 		wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
85 	debug("close\n");
86 	mtrr_close(&state, do_caches);
87 	debug("mtrr done\n");
88 
89 	return 0;
90 }
91 
mtrr_add_request(int type,uint64_t start,uint64_t size)92 int mtrr_add_request(int type, uint64_t start, uint64_t size)
93 {
94 	struct mtrr_request *req;
95 	uint64_t mask;
96 
97 	debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
98 	if (!gd->arch.has_mtrr)
99 		return -ENOSYS;
100 
101 	if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
102 		return -ENOSPC;
103 	req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
104 	req->type = type;
105 	req->start = start;
106 	req->size = size;
107 	debug("%d: type=%d, %08llx  %08llx\n", gd->arch.mtrr_req_count - 1,
108 	      req->type, req->start, req->size);
109 	mask = ~(req->size - 1);
110 	mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
111 	mask |= MTRR_PHYS_MASK_VALID;
112 	debug("   %016llx %016llx\n", req->start | req->type, mask);
113 
114 	return 0;
115 }
116 
get_var_mtrr_count(void)117 static int get_var_mtrr_count(void)
118 {
119 	return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
120 }
121 
get_free_var_mtrr(void)122 static int get_free_var_mtrr(void)
123 {
124 	struct msr_t maskm;
125 	int vcnt;
126 	int i;
127 
128 	vcnt = get_var_mtrr_count();
129 
130 	/* Identify the first var mtrr which is not valid */
131 	for (i = 0; i < vcnt; i++) {
132 		maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
133 		if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
134 			return i;
135 	}
136 
137 	/* No free var mtrr */
138 	return -ENOSPC;
139 }
140 
mtrr_set_next_var(uint type,uint64_t start,uint64_t size)141 int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
142 {
143 	int mtrr;
144 
145 	mtrr = get_free_var_mtrr();
146 	if (mtrr < 0)
147 		return mtrr;
148 
149 	set_var_mtrr(mtrr, type, start, size);
150 	debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
151 
152 	return 0;
153 }
154