Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 Google, Inc |
| 4 | * |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 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 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 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 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 19 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 20 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 21 | #include <log.h> |
Simon Glass | 6b88e88 | 2020-09-22 12:45:27 -0600 | [diff] [blame] | 22 | #include <sort.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 23 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 24 | #include <asm/global_data.h> |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 25 | #include <asm/io.h> |
Simon Glass | 7403c26 | 2020-07-17 08:48:22 -0600 | [diff] [blame] | 26 | #include <asm/mp.h> |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 27 | #include <asm/msr.h> |
| 28 | #include <asm/mtrr.h> |
| 29 | |
Bin Meng | 068fb35 | 2015-01-22 11:29:39 +0800 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 32 | /* Prepare to adjust MTRRs */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 33 | void mtrr_open(struct mtrr_state *state, bool do_caches) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 34 | { |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 35 | if (!gd->arch.has_mtrr) |
| 36 | return; |
| 37 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 38 | if (do_caches) { |
| 39 | state->enable_cache = dcache_status(); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 40 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 41 | if (state->enable_cache) |
| 42 | disable_caches(); |
| 43 | } |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 44 | state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR); |
| 45 | wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN); |
| 46 | } |
| 47 | |
| 48 | /* Clean up after adjusting MTRRs, and enable them */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 49 | void mtrr_close(struct mtrr_state *state, bool do_caches) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 50 | { |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 51 | if (!gd->arch.has_mtrr) |
| 52 | return; |
| 53 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 54 | wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN); |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 55 | if (do_caches && state->enable_cache) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 56 | enable_caches(); |
| 57 | } |
| 58 | |
Simon Glass | 3552059 | 2019-09-25 08:56:45 -0600 | [diff] [blame] | 59 | static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size) |
| 60 | { |
| 61 | u64 mask; |
| 62 | |
| 63 | wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type); |
| 64 | mask = ~(size - 1); |
| 65 | mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1; |
| 66 | wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID); |
| 67 | } |
| 68 | |
Simon Glass | 7403c26 | 2020-07-17 08:48:22 -0600 | [diff] [blame] | 69 | void mtrr_read_all(struct mtrr_info *info) |
| 70 | { |
Simon Glass | fbf120c | 2020-09-22 14:54:51 -0600 | [diff] [blame] | 71 | int reg_count = mtrr_get_var_count(); |
Simon Glass | 7403c26 | 2020-07-17 08:48:22 -0600 | [diff] [blame] | 72 | int i; |
| 73 | |
Simon Glass | fbf120c | 2020-09-22 14:54:51 -0600 | [diff] [blame] | 74 | for (i = 0; i < reg_count; i++) { |
Simon Glass | 7403c26 | 2020-07-17 08:48:22 -0600 | [diff] [blame] | 75 | info->mtrr[i].base = native_read_msr(MTRR_PHYS_BASE_MSR(i)); |
| 76 | info->mtrr[i].mask = native_read_msr(MTRR_PHYS_MASK_MSR(i)); |
| 77 | } |
| 78 | } |
| 79 | |
Simon Glass | 00dc52f | 2020-07-17 08:48:25 -0600 | [diff] [blame] | 80 | void mtrr_write_all(struct mtrr_info *info) |
| 81 | { |
Simon Glass | fbf120c | 2020-09-22 14:54:51 -0600 | [diff] [blame] | 82 | int reg_count = mtrr_get_var_count(); |
Simon Glass | 00dc52f | 2020-07-17 08:48:25 -0600 | [diff] [blame] | 83 | struct mtrr_state state; |
| 84 | int i; |
| 85 | |
Simon Glass | fbf120c | 2020-09-22 14:54:51 -0600 | [diff] [blame] | 86 | for (i = 0; i < reg_count; i++) { |
Simon Glass | 00dc52f | 2020-07-17 08:48:25 -0600 | [diff] [blame] | 87 | mtrr_open(&state, true); |
| 88 | wrmsrl(MTRR_PHYS_BASE_MSR(i), info->mtrr[i].base); |
| 89 | wrmsrl(MTRR_PHYS_MASK_MSR(i), info->mtrr[i].mask); |
| 90 | mtrr_close(&state, true); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void write_mtrrs(void *arg) |
| 95 | { |
| 96 | struct mtrr_info *info = arg; |
| 97 | |
| 98 | mtrr_write_all(info); |
| 99 | } |
| 100 | |
| 101 | static void read_mtrrs(void *arg) |
| 102 | { |
| 103 | struct mtrr_info *info = arg; |
| 104 | |
| 105 | mtrr_read_all(info); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * mtrr_copy_to_aps() - Copy the MTRRs from the boot CPU to other CPUs |
| 110 | * |
| 111 | * @return 0 on success, -ve on failure |
| 112 | */ |
| 113 | static int mtrr_copy_to_aps(void) |
| 114 | { |
| 115 | struct mtrr_info info; |
| 116 | int ret; |
| 117 | |
| 118 | ret = mp_run_on_cpus(MP_SELECT_BSP, read_mtrrs, &info); |
| 119 | if (ret == -ENXIO) |
| 120 | return 0; |
| 121 | else if (ret) |
| 122 | return log_msg_ret("bsp", ret); |
| 123 | |
| 124 | ret = mp_run_on_cpus(MP_SELECT_APS, write_mtrrs, &info); |
| 125 | if (ret) |
| 126 | return log_msg_ret("bsp", ret); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Simon Glass | 6b88e88 | 2020-09-22 12:45:27 -0600 | [diff] [blame] | 131 | static int h_comp_mtrr(const void *p1, const void *p2) |
| 132 | { |
| 133 | const struct mtrr_request *req1 = p1; |
| 134 | const struct mtrr_request *req2 = p2; |
| 135 | |
| 136 | s64 diff = req1->start - req2->start; |
| 137 | |
| 138 | return diff < 0 ? -1 : diff > 0 ? 1 : 0; |
| 139 | } |
| 140 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 141 | int mtrr_commit(bool do_caches) |
| 142 | { |
| 143 | struct mtrr_request *req = gd->arch.mtrr_req; |
| 144 | struct mtrr_state state; |
Simon Glass | 00dc52f | 2020-07-17 08:48:25 -0600 | [diff] [blame] | 145 | int ret; |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 146 | int i; |
| 147 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 148 | debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr, |
| 149 | gd->arch.mtrr_req_count); |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 150 | if (!gd->arch.has_mtrr) |
| 151 | return -ENOSYS; |
| 152 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 153 | debug("open\n"); |
| 154 | mtrr_open(&state, do_caches); |
| 155 | debug("open done\n"); |
Simon Glass | 6b88e88 | 2020-09-22 12:45:27 -0600 | [diff] [blame] | 156 | qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr); |
Simon Glass | 3552059 | 2019-09-25 08:56:45 -0600 | [diff] [blame] | 157 | for (i = 0; i < gd->arch.mtrr_req_count; i++, req++) |
| 158 | set_var_mtrr(i, req->type, req->start, req->size); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 159 | |
| 160 | /* Clear the ones that are unused */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 161 | debug("clear\n"); |
Bin Meng | 761d8eb | 2020-11-09 15:55:49 +0800 | [diff] [blame] | 162 | for (; i < mtrr_get_var_count(); i++) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 163 | wrmsrl(MTRR_PHYS_MASK_MSR(i), 0); |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 164 | debug("close\n"); |
| 165 | mtrr_close(&state, do_caches); |
| 166 | debug("mtrr done\n"); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 167 | |
Simon Glass | 00dc52f | 2020-07-17 08:48:25 -0600 | [diff] [blame] | 168 | if (gd->flags & GD_FLG_RELOC) { |
| 169 | ret = mtrr_copy_to_aps(); |
| 170 | if (ret) |
| 171 | return log_msg_ret("copy", ret); |
| 172 | } |
| 173 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int mtrr_add_request(int type, uint64_t start, uint64_t size) |
| 178 | { |
| 179 | struct mtrr_request *req; |
| 180 | uint64_t mask; |
| 181 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 182 | debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count); |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 183 | if (!gd->arch.has_mtrr) |
| 184 | return -ENOSYS; |
| 185 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 186 | if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS) |
| 187 | return -ENOSPC; |
| 188 | req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++]; |
| 189 | req->type = type; |
| 190 | req->start = start; |
| 191 | req->size = size; |
| 192 | debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1, |
| 193 | req->type, req->start, req->size); |
| 194 | mask = ~(req->size - 1); |
| 195 | mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1; |
| 196 | mask |= MTRR_PHYS_MASK_VALID; |
| 197 | debug(" %016llx %016llx\n", req->start | req->type, mask); |
| 198 | |
| 199 | return 0; |
| 200 | } |
Simon Glass | 753297d | 2019-09-25 08:56:46 -0600 | [diff] [blame] | 201 | |
Simon Glass | fbf120c | 2020-09-22 14:54:51 -0600 | [diff] [blame] | 202 | int mtrr_get_var_count(void) |
Simon Glass | 753297d | 2019-09-25 08:56:46 -0600 | [diff] [blame] | 203 | { |
| 204 | return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT; |
| 205 | } |
| 206 | |
| 207 | static int get_free_var_mtrr(void) |
| 208 | { |
| 209 | struct msr_t maskm; |
| 210 | int vcnt; |
| 211 | int i; |
| 212 | |
Simon Glass | fbf120c | 2020-09-22 14:54:51 -0600 | [diff] [blame] | 213 | vcnt = mtrr_get_var_count(); |
Simon Glass | 753297d | 2019-09-25 08:56:46 -0600 | [diff] [blame] | 214 | |
| 215 | /* Identify the first var mtrr which is not valid */ |
| 216 | for (i = 0; i < vcnt; i++) { |
| 217 | maskm = msr_read(MTRR_PHYS_MASK_MSR(i)); |
| 218 | if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0) |
| 219 | return i; |
| 220 | } |
| 221 | |
| 222 | /* No free var mtrr */ |
| 223 | return -ENOSPC; |
| 224 | } |
| 225 | |
| 226 | int mtrr_set_next_var(uint type, uint64_t start, uint64_t size) |
| 227 | { |
| 228 | int mtrr; |
| 229 | |
| 230 | mtrr = get_free_var_mtrr(); |
| 231 | if (mtrr < 0) |
| 232 | return mtrr; |
| 233 | |
| 234 | set_var_mtrr(mtrr, type, start, size); |
| 235 | debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size); |
| 236 | |
| 237 | return 0; |
| 238 | } |
Simon Glass | d89e15f | 2020-07-17 08:48:26 -0600 | [diff] [blame] | 239 | |
| 240 | /** enum mtrr_opcode - supported operations for mtrr_do_oper() */ |
| 241 | enum mtrr_opcode { |
| 242 | MTRR_OP_SET, |
| 243 | MTRR_OP_SET_VALID, |
| 244 | }; |
| 245 | |
| 246 | /** |
| 247 | * struct mtrr_oper - An MTRR operation to perform on a CPU |
| 248 | * |
| 249 | * @opcode: Indicates operation to perform |
| 250 | * @reg: MTRR reg number to select (0-7, -1 = all) |
| 251 | * @valid: Valid value to write for MTRR_OP_SET_VALID |
| 252 | * @base: Base value to write for MTRR_OP_SET |
| 253 | * @mask: Mask value to write for MTRR_OP_SET |
| 254 | */ |
| 255 | struct mtrr_oper { |
| 256 | enum mtrr_opcode opcode; |
| 257 | int reg; |
| 258 | bool valid; |
| 259 | u64 base; |
| 260 | u64 mask; |
| 261 | }; |
| 262 | |
| 263 | static void mtrr_do_oper(void *arg) |
| 264 | { |
| 265 | struct mtrr_oper *oper = arg; |
| 266 | u64 mask; |
| 267 | |
| 268 | switch (oper->opcode) { |
| 269 | case MTRR_OP_SET_VALID: |
| 270 | mask = native_read_msr(MTRR_PHYS_MASK_MSR(oper->reg)); |
| 271 | if (oper->valid) |
| 272 | mask |= MTRR_PHYS_MASK_VALID; |
| 273 | else |
| 274 | mask &= ~MTRR_PHYS_MASK_VALID; |
| 275 | wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), mask); |
| 276 | break; |
| 277 | case MTRR_OP_SET: |
| 278 | wrmsrl(MTRR_PHYS_BASE_MSR(oper->reg), oper->base); |
| 279 | wrmsrl(MTRR_PHYS_MASK_MSR(oper->reg), oper->mask); |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static int mtrr_start_op(int cpu_select, struct mtrr_oper *oper) |
| 285 | { |
| 286 | struct mtrr_state state; |
| 287 | int ret; |
| 288 | |
| 289 | mtrr_open(&state, true); |
| 290 | ret = mp_run_on_cpus(cpu_select, mtrr_do_oper, oper); |
| 291 | mtrr_close(&state, true); |
| 292 | if (ret) |
| 293 | return log_msg_ret("run", ret); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | int mtrr_set_valid(int cpu_select, int reg, bool valid) |
| 299 | { |
| 300 | struct mtrr_oper oper; |
| 301 | |
| 302 | oper.opcode = MTRR_OP_SET_VALID; |
| 303 | oper.reg = reg; |
| 304 | oper.valid = valid; |
| 305 | |
| 306 | return mtrr_start_op(cpu_select, &oper); |
| 307 | } |
| 308 | |
| 309 | int mtrr_set(int cpu_select, int reg, u64 base, u64 mask) |
| 310 | { |
| 311 | struct mtrr_oper oper; |
| 312 | |
| 313 | oper.opcode = MTRR_OP_SET; |
| 314 | oper.reg = reg; |
| 315 | oper.base = base; |
| 316 | oper.mask = mask; |
| 317 | |
| 318 | return mtrr_start_op(cpu_select, &oper); |
| 319 | } |