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