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 | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 22 | #include <asm/cache.h> |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 23 | #include <asm/io.h> |
| 24 | #include <asm/msr.h> |
| 25 | #include <asm/mtrr.h> |
| 26 | |
Bin Meng | 068fb35 | 2015-01-22 11:29:39 +0800 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 29 | /* Prepare to adjust MTRRs */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 30 | void mtrr_open(struct mtrr_state *state, bool do_caches) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 31 | { |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 32 | if (!gd->arch.has_mtrr) |
| 33 | return; |
| 34 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 35 | if (do_caches) { |
| 36 | state->enable_cache = dcache_status(); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 37 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 38 | if (state->enable_cache) |
| 39 | disable_caches(); |
| 40 | } |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 41 | state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR); |
| 42 | wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN); |
| 43 | } |
| 44 | |
| 45 | /* Clean up after adjusting MTRRs, and enable them */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 46 | void mtrr_close(struct mtrr_state *state, bool do_caches) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 47 | { |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 48 | if (!gd->arch.has_mtrr) |
| 49 | return; |
| 50 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 51 | wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN); |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 52 | if (do_caches && state->enable_cache) |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 53 | enable_caches(); |
| 54 | } |
| 55 | |
Simon Glass | 3552059 | 2019-09-25 08:56:45 -0600 | [diff] [blame] | 56 | static void set_var_mtrr(uint reg, uint type, uint64_t start, uint64_t size) |
| 57 | { |
| 58 | u64 mask; |
| 59 | |
| 60 | wrmsrl(MTRR_PHYS_BASE_MSR(reg), start | type); |
| 61 | mask = ~(size - 1); |
| 62 | mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1; |
| 63 | wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask | MTRR_PHYS_MASK_VALID); |
| 64 | } |
| 65 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 66 | int mtrr_commit(bool do_caches) |
| 67 | { |
| 68 | struct mtrr_request *req = gd->arch.mtrr_req; |
| 69 | struct mtrr_state state; |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 70 | int i; |
| 71 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 72 | debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr, |
| 73 | gd->arch.mtrr_req_count); |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 74 | if (!gd->arch.has_mtrr) |
| 75 | return -ENOSYS; |
| 76 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 77 | debug("open\n"); |
| 78 | mtrr_open(&state, do_caches); |
| 79 | debug("open done\n"); |
Simon Glass | 3552059 | 2019-09-25 08:56:45 -0600 | [diff] [blame] | 80 | for (i = 0; i < gd->arch.mtrr_req_count; i++, req++) |
| 81 | set_var_mtrr(i, req->type, req->start, req->size); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 82 | |
| 83 | /* Clear the ones that are unused */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 84 | debug("clear\n"); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 85 | for (; i < MTRR_COUNT; i++) |
| 86 | wrmsrl(MTRR_PHYS_MASK_MSR(i), 0); |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 87 | debug("close\n"); |
| 88 | mtrr_close(&state, do_caches); |
| 89 | debug("mtrr done\n"); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int mtrr_add_request(int type, uint64_t start, uint64_t size) |
| 95 | { |
| 96 | struct mtrr_request *req; |
| 97 | uint64_t mask; |
| 98 | |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 99 | debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count); |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 100 | if (!gd->arch.has_mtrr) |
| 101 | return -ENOSYS; |
| 102 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 103 | if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS) |
| 104 | return -ENOSPC; |
| 105 | req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++]; |
| 106 | req->type = type; |
| 107 | req->start = start; |
| 108 | req->size = size; |
| 109 | debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1, |
| 110 | req->type, req->start, req->size); |
| 111 | mask = ~(req->size - 1); |
| 112 | mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1; |
| 113 | mask |= MTRR_PHYS_MASK_VALID; |
| 114 | debug(" %016llx %016llx\n", req->start | req->type, mask); |
| 115 | |
| 116 | return 0; |
| 117 | } |
Simon Glass | 753297d | 2019-09-25 08:56:46 -0600 | [diff] [blame] | 118 | |
| 119 | static int get_var_mtrr_count(void) |
| 120 | { |
| 121 | return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT; |
| 122 | } |
| 123 | |
| 124 | static int get_free_var_mtrr(void) |
| 125 | { |
| 126 | struct msr_t maskm; |
| 127 | int vcnt; |
| 128 | int i; |
| 129 | |
| 130 | vcnt = get_var_mtrr_count(); |
| 131 | |
| 132 | /* Identify the first var mtrr which is not valid */ |
| 133 | for (i = 0; i < vcnt; i++) { |
| 134 | maskm = msr_read(MTRR_PHYS_MASK_MSR(i)); |
| 135 | if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0) |
| 136 | return i; |
| 137 | } |
| 138 | |
| 139 | /* No free var mtrr */ |
| 140 | return -ENOSPC; |
| 141 | } |
| 142 | |
| 143 | int mtrr_set_next_var(uint type, uint64_t start, uint64_t size) |
| 144 | { |
| 145 | int mtrr; |
| 146 | |
| 147 | mtrr = get_free_var_mtrr(); |
| 148 | if (mtrr < 0) |
| 149 | return mtrr; |
| 150 | |
| 151 | set_var_mtrr(mtrr, type, start, size); |
| 152 | debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size); |
| 153 | |
| 154 | return 0; |
| 155 | } |