blob: c9b4e7d06ee4409a1413ec32f324369571ba07b1 [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 Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070023#include <asm/io.h>
Simon Glass7403c262020-07-17 08:48:22 -060024#include <asm/mp.h>
Simon Glass7bf5b9e2015-01-01 16:18:07 -070025#include <asm/msr.h>
26#include <asm/mtrr.h>
27
Bin Meng068fb352015-01-22 11:29:39 +080028DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass7bf5b9e2015-01-01 16:18:07 -070030/* Prepare to adjust MTRRs */
Simon Glass8fafd012018-10-01 12:22:37 -060031void mtrr_open(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070032{
Bin Meng80d29762015-01-22 11:29:41 +080033 if (!gd->arch.has_mtrr)
34 return;
35
Simon Glass8fafd012018-10-01 12:22:37 -060036 if (do_caches) {
37 state->enable_cache = dcache_status();
Simon Glass7bf5b9e2015-01-01 16:18:07 -070038
Simon Glass8fafd012018-10-01 12:22:37 -060039 if (state->enable_cache)
40 disable_caches();
41 }
Simon Glass7bf5b9e2015-01-01 16:18:07 -070042 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
43 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
44}
45
46/* Clean up after adjusting MTRRs, and enable them */
Simon Glass8fafd012018-10-01 12:22:37 -060047void mtrr_close(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070048{
Bin Meng80d29762015-01-22 11:29:41 +080049 if (!gd->arch.has_mtrr)
50 return;
51
Simon Glass7bf5b9e2015-01-01 16:18:07 -070052 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
Simon Glass8fafd012018-10-01 12:22:37 -060053 if (do_caches && state->enable_cache)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070054 enable_caches();
55}
56
Simon Glass35520592019-09-25 08:56:45 -060057static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
58{
59 u64 mask;
60
61 wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
62 mask = ~(size - 1);
63 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
64 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
65}
66
Simon Glass7403c262020-07-17 08:48:22 -060067void mtrr_read_all(struct mtrr_info *info)
68{
69 int i;
70
71 for (i = 0; i < MTRR_COUNT; i++) {
72 info->mtrr[i].base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
73 info->mtrr[i].mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
74 }
75}
76
Simon Glass7bf5b9e2015-01-01 16:18:07 -070077int mtrr_commit(bool do_caches)
78{
79 struct mtrr_request *req = gd->arch.mtrr_req;
80 struct mtrr_state state;
Simon Glass7bf5b9e2015-01-01 16:18:07 -070081 int i;
82
Simon Glass8fafd012018-10-01 12:22:37 -060083 debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
84 gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +080085 if (!gd->arch.has_mtrr)
86 return -ENOSYS;
87
Simon Glass8fafd012018-10-01 12:22:37 -060088 debug("open\n");
89 mtrr_open(&state, do_caches);
90 debug("open done\n");
Simon Glass35520592019-09-25 08:56:45 -060091 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
92 set_var_mtrr(i, req->type, req->start, req->size);
Simon Glass7bf5b9e2015-01-01 16:18:07 -070093
94 /* Clear the ones that are unused */
Simon Glass8fafd012018-10-01 12:22:37 -060095 debug("clear\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -070096 for (; i < MTRR_COUNT; i++)
97 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
Simon Glass8fafd012018-10-01 12:22:37 -060098 debug("close\n");
99 mtrr_close(&state, do_caches);
100 debug("mtrr done\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700101
102 return 0;
103}
104
105int mtrr_add_request(int type, uint64_t start, uint64_t size)
106{
107 struct mtrr_request *req;
108 uint64_t mask;
109
Simon Glass8fafd012018-10-01 12:22:37 -0600110 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +0800111 if (!gd->arch.has_mtrr)
112 return -ENOSYS;
113
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700114 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
115 return -ENOSPC;
116 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
117 req->type = type;
118 req->start = start;
119 req->size = size;
120 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
121 req->type, req->start, req->size);
122 mask = ~(req->size - 1);
123 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
124 mask |= MTRR_PHYS_MASK_VALID;
125 debug(" %016llx %016llx\n", req->start | req->type, mask);
126
127 return 0;
128}
Simon Glass753297d2019-09-25 08:56:46 -0600129
130static int get_var_mtrr_count(void)
131{
132 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
133}
134
135static int get_free_var_mtrr(void)
136{
137 struct msr_t maskm;
138 int vcnt;
139 int i;
140
141 vcnt = get_var_mtrr_count();
142
143 /* Identify the first var mtrr which is not valid */
144 for (i = 0; i < vcnt; i++) {
145 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
146 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
147 return i;
148 }
149
150 /* No free var mtrr */
151 return -ENOSPC;
152}
153
154int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
155{
156 int mtrr;
157
158 mtrr = get_free_var_mtrr();
159 if (mtrr < 0)
160 return mtrr;
161
162 set_var_mtrr(mtrr, type, start, size);
163 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
164
165 return 0;
166}