blob: a00db422e7a91ace8f28032ebdd0c845bc453158 [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>
20#include <asm/io.h>
21#include <asm/msr.h>
22#include <asm/mtrr.h>
23
Bin Meng068fb352015-01-22 11:29:39 +080024DECLARE_GLOBAL_DATA_PTR;
25
Simon Glass7bf5b9e2015-01-01 16:18:07 -070026/* Prepare to adjust MTRRs */
Simon Glass8fafd012018-10-01 12:22:37 -060027void mtrr_open(struct mtrr_state *state, bool do_caches)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070028{
Bin Meng80d29762015-01-22 11:29:41 +080029 if (!gd->arch.has_mtrr)
30 return;
31
Simon Glass8fafd012018-10-01 12:22:37 -060032 if (do_caches) {
33 state->enable_cache = dcache_status();
Simon Glass7bf5b9e2015-01-01 16:18:07 -070034
Simon Glass8fafd012018-10-01 12:22:37 -060035 if (state->enable_cache)
36 disable_caches();
37 }
Simon Glass7bf5b9e2015-01-01 16:18:07 -070038 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
39 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
40}
41
42/* Clean up after adjusting MTRRs, and enable them */
Simon Glass8fafd012018-10-01 12:22:37 -060043void mtrr_close(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 Glass7bf5b9e2015-01-01 16:18:07 -070048 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
Simon Glass8fafd012018-10-01 12:22:37 -060049 if (do_caches && state->enable_cache)
Simon Glass7bf5b9e2015-01-01 16:18:07 -070050 enable_caches();
51}
52
Simon Glass35520592019-09-25 08:56:45 -060053static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size)
54{
55 u64 mask;
56
57 wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type);
58 mask = ~(size - 1);
59 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
60 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID);
61}
62
Simon Glass7bf5b9e2015-01-01 16:18:07 -070063int mtrr_commit(bool do_caches)
64{
65 struct mtrr_request *req = gd->arch.mtrr_req;
66 struct mtrr_state state;
Simon Glass7bf5b9e2015-01-01 16:18:07 -070067 int i;
68
Simon Glass8fafd012018-10-01 12:22:37 -060069 debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
70 gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +080071 if (!gd->arch.has_mtrr)
72 return -ENOSYS;
73
Simon Glass8fafd012018-10-01 12:22:37 -060074 debug("open\n");
75 mtrr_open(&state, do_caches);
76 debug("open done\n");
Simon Glass35520592019-09-25 08:56:45 -060077 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
78 set_var_mtrr(i, req->type, req->start, req->size);
Simon Glass7bf5b9e2015-01-01 16:18:07 -070079
80 /* Clear the ones that are unused */
Simon Glass8fafd012018-10-01 12:22:37 -060081 debug("clear\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -070082 for (; i < MTRR_COUNT; i++)
83 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
Simon Glass8fafd012018-10-01 12:22:37 -060084 debug("close\n");
85 mtrr_close(&state, do_caches);
86 debug("mtrr done\n");
Simon Glass7bf5b9e2015-01-01 16:18:07 -070087
88 return 0;
89}
90
91int mtrr_add_request(int type, uint64_t start, uint64_t size)
92{
93 struct mtrr_request *req;
94 uint64_t mask;
95
Simon Glass8fafd012018-10-01 12:22:37 -060096 debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
Bin Meng80d29762015-01-22 11:29:41 +080097 if (!gd->arch.has_mtrr)
98 return -ENOSYS;
99
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700100 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
101 return -ENOSPC;
102 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
103 req->type = type;
104 req->start = start;
105 req->size = size;
106 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
107 req->type, req->start, req->size);
108 mask = ~(req->size - 1);
109 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
110 mask |= MTRR_PHYS_MASK_VALID;
111 debug(" %016llx %016llx\n", req->start | req->type, mask);
112
113 return 0;
114}
Simon Glass753297d2019-09-25 08:56:46 -0600115
116static int get_var_mtrr_count(void)
117{
118 return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
119}
120
121static int get_free_var_mtrr(void)
122{
123 struct msr_t maskm;
124 int vcnt;
125 int i;
126
127 vcnt = get_var_mtrr_count();
128
129 /* Identify the first var mtrr which is not valid */
130 for (i = 0; i < vcnt; i++) {
131 maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
132 if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
133 return i;
134 }
135
136 /* No free var mtrr */
137 return -ENOSPC;
138}
139
140int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
141{
142 int mtrr;
143
144 mtrr = get_free_var_mtrr();
145 if (mtrr < 0)
146 return mtrr;
147
148 set_var_mtrr(mtrr, type, start, size);
149 debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
150
151 return 0;
152}