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