Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
| 4 | * |
| 5 | * From Coreboot file of the same name |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _ASM_MTRR_H |
| 9 | #define _ASM_MTRR_H |
| 10 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 11 | /* MTRR region types */ |
| 12 | #define MTRR_TYPE_UNCACHEABLE 0 |
| 13 | #define MTRR_TYPE_WRCOMB 1 |
| 14 | #define MTRR_TYPE_WRTHROUGH 4 |
| 15 | #define MTRR_TYPE_WRPROT 5 |
| 16 | #define MTRR_TYPE_WRBACK 6 |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 17 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 18 | #define MTRR_TYPE_COUNT 7 |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 19 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 20 | #define MTRR_CAP_MSR 0x0fe |
| 21 | #define MTRR_DEF_TYPE_MSR 0x2ff |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 22 | |
Bin Meng | c45a93b | 2015-07-06 16:31:30 +0800 | [diff] [blame] | 23 | #define MTRR_CAP_SMRR (1 << 11) |
| 24 | #define MTRR_CAP_WC (1 << 10) |
| 25 | #define MTRR_CAP_FIX (1 << 8) |
| 26 | #define MTRR_CAP_VCNT_MASK 0xff |
| 27 | |
Simon Glass | ca1c61e | 2019-09-25 08:11:46 -0600 | [diff] [blame] | 28 | #define MTRR_DEF_TYPE_MASK 0xff |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 29 | #define MTRR_DEF_TYPE_EN (1 << 11) |
| 30 | #define MTRR_DEF_TYPE_FIX_EN (1 << 10) |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 31 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 32 | #define MTRR_PHYS_BASE_MSR(reg) (0x200 + 2 * (reg)) |
| 33 | #define MTRR_PHYS_MASK_MSR(reg) (0x200 + 2 * (reg) + 1) |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 34 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 35 | #define MTRR_PHYS_MASK_VALID (1 << 11) |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 36 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 37 | #define MTRR_BASE_TYPE_MASK 0x7 |
| 38 | |
| 39 | /* Number of MTRRs supported */ |
| 40 | #define MTRR_COUNT 8 |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 41 | |
Simon Glass | a9a4426 | 2015-04-29 22:25:59 -0600 | [diff] [blame] | 42 | #define NUM_FIXED_MTRRS 11 |
| 43 | #define RANGES_PER_FIXED_MTRR 8 |
| 44 | #define NUM_FIXED_RANGES (NUM_FIXED_MTRRS * RANGES_PER_FIXED_MTRR) |
| 45 | |
Bin Meng | c45a93b | 2015-07-06 16:31:30 +0800 | [diff] [blame] | 46 | #define MTRR_FIX_64K_00000_MSR 0x250 |
| 47 | #define MTRR_FIX_16K_80000_MSR 0x258 |
| 48 | #define MTRR_FIX_16K_A0000_MSR 0x259 |
| 49 | #define MTRR_FIX_4K_C0000_MSR 0x268 |
| 50 | #define MTRR_FIX_4K_C8000_MSR 0x269 |
| 51 | #define MTRR_FIX_4K_D0000_MSR 0x26a |
| 52 | #define MTRR_FIX_4K_D8000_MSR 0x26b |
| 53 | #define MTRR_FIX_4K_E0000_MSR 0x26c |
| 54 | #define MTRR_FIX_4K_E8000_MSR 0x26d |
| 55 | #define MTRR_FIX_4K_F0000_MSR 0x26e |
| 56 | #define MTRR_FIX_4K_F8000_MSR 0x26f |
Simon Glass | 71ca938 | 2015-04-28 20:25:13 -0600 | [diff] [blame] | 57 | |
Bin Meng | 268ca83 | 2015-07-15 16:23:38 +0800 | [diff] [blame] | 58 | #define MTRR_FIX_TYPE(t) ((t << 24) | (t << 16) | (t << 8) | t) |
| 59 | |
Simon Glass | 559f1a8 | 2020-05-10 11:40:12 -0600 | [diff] [blame] | 60 | #if !defined(__ASSEMBLY__) |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 61 | |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Information about the previous MTRR state, set up by mtrr_open() |
| 64 | * |
| 65 | * @deftype: Previous value of MTRR_DEF_TYPE_MSR |
| 66 | * @enable_cache: true if cache was enabled |
| 67 | */ |
| 68 | struct mtrr_state { |
| 69 | uint64_t deftype; |
| 70 | bool enable_cache; |
| 71 | }; |
| 72 | |
| 73 | /** |
Simon Glass | 7403c26 | 2020-07-17 08:48:22 -0600 | [diff] [blame] | 74 | * struct mtrr - Information about a single MTRR |
| 75 | * |
| 76 | * @base: Base address and MTRR_BASE_TYPE_MASK |
| 77 | * @mask: Mask and MTRR_PHYS_MASK_VALID |
| 78 | */ |
| 79 | struct mtrr { |
| 80 | u64 base; |
| 81 | u64 mask; |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * struct mtrr_info - Information about all MTRRs |
| 86 | * |
| 87 | * @mtrr: Information about each mtrr |
| 88 | */ |
| 89 | struct mtrr_info { |
| 90 | struct mtrr mtrr[MTRR_COUNT]; |
| 91 | }; |
| 92 | |
| 93 | /** |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 94 | * mtrr_open() - Prepare to adjust MTRRs |
| 95 | * |
| 96 | * Use mtrr_open() passing in a structure - this function will init it. Then |
| 97 | * when done, pass the same structure to mtrr_close() to re-enable MTRRs and |
| 98 | * possibly the cache. |
| 99 | * |
| 100 | * @state: Empty structure to pass in to hold settings |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 101 | * @do_caches: true to disable caches before opening |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 102 | */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 103 | void mtrr_open(struct mtrr_state *state, bool do_caches); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * mtrr_open() - Clean up after adjusting MTRRs, and enable them |
| 107 | * |
| 108 | * This uses the structure containing information returned from mtrr_open(). |
| 109 | * |
| 110 | * @state: Structure from mtrr_open() |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 111 | * @state: true to restore cache state to that before mtrr_open() |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 112 | */ |
Simon Glass | 8fafd01 | 2018-10-01 12:22:37 -0600 | [diff] [blame] | 113 | void mtrr_close(struct mtrr_state *state, bool do_caches); |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * mtrr_add_request() - Add a new MTRR request |
| 117 | * |
| 118 | * This adds a request for a memory region to be set up in a particular way. |
| 119 | * |
| 120 | * @type: Requested type (MTRR_TYPE_) |
| 121 | * @start: Start address |
| 122 | * @size: Size |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 123 | * |
| 124 | * @return: 0 on success, non-zero on failure |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 125 | */ |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 126 | int mtrr_add_request(int type, uint64_t start, uint64_t size); |
| 127 | |
| 128 | /** |
| 129 | * mtrr_commit() - set up the MTRR registers based on current requests |
| 130 | * |
| 131 | * This sets up MTRRs for the available DRAM and the requests received so far. |
| 132 | * It must be called with caches disabled. |
| 133 | * |
| 134 | * @do_caches: true if caches are currently on |
Bin Meng | 80d2976 | 2015-01-22 11:29:41 +0800 | [diff] [blame] | 135 | * |
| 136 | * @return: 0 on success, non-zero on failure |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 137 | */ |
Simon Glass | 7bf5b9e | 2015-01-01 16:18:07 -0700 | [diff] [blame] | 138 | int mtrr_commit(bool do_caches); |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 139 | |
Simon Glass | 753297d | 2019-09-25 08:56:46 -0600 | [diff] [blame] | 140 | /** |
| 141 | * mtrr_set_next_var() - set up a variable MTRR |
| 142 | * |
| 143 | * This finds the first free variable MTRR and sets to the given area |
| 144 | * |
| 145 | * @type: Requested type (MTRR_TYPE_) |
| 146 | * @start: Start address |
| 147 | * @size: Size |
| 148 | * @return 0 on success, -ENOSPC if there are no more MTRRs |
| 149 | */ |
| 150 | int mtrr_set_next_var(uint type, uint64_t base, uint64_t size); |
| 151 | |
Simon Glass | 7403c26 | 2020-07-17 08:48:22 -0600 | [diff] [blame] | 152 | /** |
| 153 | * mtrr_read_all() - Save all the MTRRs |
| 154 | * |
| 155 | * This reads all MTRRs from the boot CPU into a struct so they can be loaded |
| 156 | * onto other CPUs |
| 157 | * |
| 158 | * @info: Place to put the MTRR info |
| 159 | */ |
| 160 | void mtrr_read_all(struct mtrr_info *info); |
| 161 | |
Simon Glass | d89e15f | 2020-07-17 08:48:26 -0600 | [diff] [blame] | 162 | /** |
| 163 | * mtrr_set_valid() - Set the valid flag for a selected MTRR and CPU(s) |
| 164 | * |
| 165 | * @cpu_select: Selected CPUs (either a CPU number or MP_SELECT_...) |
| 166 | * @reg: MTRR register to write (0-7) |
| 167 | * @valid: Valid flag to write |
| 168 | * @return 0 on success, -ve on error |
| 169 | */ |
| 170 | int mtrr_set_valid(int cpu_select, int reg, bool valid); |
| 171 | |
| 172 | /** |
| 173 | * mtrr_set() - Set the valid flag for a selected MTRR and CPU(s) |
| 174 | * |
| 175 | * @cpu_select: Selected CPUs (either a CPU number or MP_SELECT_...) |
| 176 | * @reg: MTRR register to write (0-7) |
| 177 | * @base: Base address and MTRR_BASE_TYPE_MASK |
| 178 | * @mask: Mask and MTRR_PHYS_MASK_VALID |
| 179 | * @return 0 on success, -ve on error |
| 180 | */ |
| 181 | int mtrr_set(int cpu_select, int reg, u64 base, u64 mask); |
| 182 | |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 183 | #endif |
| 184 | |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 185 | #if ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE - 1)) != 0) |
| 186 | # error "CONFIG_XIP_ROM_SIZE is not a power of 2" |
| 187 | #endif |
| 188 | |
| 189 | #if ((CONFIG_CACHE_ROM_SIZE & (CONFIG_CACHE_ROM_SIZE - 1)) != 0) |
| 190 | # error "CONFIG_CACHE_ROM_SIZE is not a power of 2" |
| 191 | #endif |
| 192 | |
| 193 | #define CACHE_ROM_BASE (((1 << 20) - (CONFIG_CACHE_ROM_SIZE >> 12)) << 12) |
| 194 | |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 195 | #endif |