blob: 2e995f5406169db2745e91442b55ae8d3bdee1eb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass98f139b2014-11-12 22:42:10 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 *
5 * From Coreboot file of the same name
Simon Glass98f139b2014-11-12 22:42:10 -07006 */
7
8#ifndef _ASM_MTRR_H
9#define _ASM_MTRR_H
10
Simon Glass7bf5b9e2015-01-01 16:18:07 -070011/* 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 Glass98f139b2014-11-12 22:42:10 -070017
Simon Glass7bf5b9e2015-01-01 16:18:07 -070018#define MTRR_TYPE_COUNT 7
Simon Glass98f139b2014-11-12 22:42:10 -070019
Simon Glass7bf5b9e2015-01-01 16:18:07 -070020#define MTRR_CAP_MSR 0x0fe
21#define MTRR_DEF_TYPE_MSR 0x2ff
Simon Glass98f139b2014-11-12 22:42:10 -070022
Bin Mengc45a93b2015-07-06 16:31:30 +080023#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 Glassca1c61e2019-09-25 08:11:46 -060028#define MTRR_DEF_TYPE_MASK 0xff
Simon Glass7bf5b9e2015-01-01 16:18:07 -070029#define MTRR_DEF_TYPE_EN (1 << 11)
30#define MTRR_DEF_TYPE_FIX_EN (1 << 10)
Simon Glass98f139b2014-11-12 22:42:10 -070031
Simon Glass7bf5b9e2015-01-01 16:18:07 -070032#define MTRR_PHYS_BASE_MSR(reg) (0x200 + 2 * (reg))
33#define MTRR_PHYS_MASK_MSR(reg) (0x200 + 2 * (reg) + 1)
Simon Glass98f139b2014-11-12 22:42:10 -070034
Simon Glass7bf5b9e2015-01-01 16:18:07 -070035#define MTRR_PHYS_MASK_VALID (1 << 11)
Simon Glass98f139b2014-11-12 22:42:10 -070036
Simon Glass7bf5b9e2015-01-01 16:18:07 -070037#define MTRR_BASE_TYPE_MASK 0x7
38
Simon Glassfbf120c2020-09-22 14:54:51 -060039/* Maximum number of MTRRs supported - see also mtrr_get_var_count() */
40#define MTRR_MAX_COUNT 10
Simon Glass98f139b2014-11-12 22:42:10 -070041
Simon Glassa9a44262015-04-29 22:25:59 -060042#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 Mengc45a93b2015-07-06 16:31:30 +080046#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 Glass71ca9382015-04-28 20:25:13 -060057
Bin Meng268ca832015-07-15 16:23:38 +080058#define MTRR_FIX_TYPE(t) ((t << 24) | (t << 16) | (t << 8) | t)
59
Simon Glass559f1a82020-05-10 11:40:12 -060060#if !defined(__ASSEMBLY__)
Simon Glass98f139b2014-11-12 22:42:10 -070061
Simon Glass7bf5b9e2015-01-01 16:18:07 -070062/**
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 */
68struct mtrr_state {
69 uint64_t deftype;
70 bool enable_cache;
71};
72
73/**
Simon Glass7403c262020-07-17 08:48:22 -060074 * 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 */
79struct 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 */
89struct mtrr_info {
Simon Glassfbf120c2020-09-22 14:54:51 -060090 struct mtrr mtrr[MTRR_MAX_COUNT];
Simon Glass7403c262020-07-17 08:48:22 -060091};
92
93/**
Simon Glass7bf5b9e2015-01-01 16:18:07 -070094 * 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 Glass8fafd012018-10-01 12:22:37 -0600101 * @do_caches: true to disable caches before opening
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700102 */
Simon Glass8fafd012018-10-01 12:22:37 -0600103void mtrr_open(struct mtrr_state *state, bool do_caches);
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700104
105/**
Wolfgang Wallner2282aab2021-03-23 10:39:09 +0100106 * mtrr_close() - Clean up after adjusting MTRRs, and enable them
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700107 *
108 * This uses the structure containing information returned from mtrr_open().
109 *
110 * @state: Structure from mtrr_open()
Simon Glass8fafd012018-10-01 12:22:37 -0600111 * @state: true to restore cache state to that before mtrr_open()
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700112 */
Simon Glass8fafd012018-10-01 12:22:37 -0600113void mtrr_close(struct mtrr_state *state, bool do_caches);
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700114
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
Bin Menge41f0d22021-07-31 16:45:26 +0800122 * @size: Size, must be power of 2
Bin Meng80d29762015-01-22 11:29:41 +0800123 *
124 * @return: 0 on success, non-zero on failure
Simon Glass98f139b2014-11-12 22:42:10 -0700125 */
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700126int 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 Meng80d29762015-01-22 11:29:41 +0800135 *
136 * @return: 0 on success, non-zero on failure
Simon Glass98f139b2014-11-12 22:42:10 -0700137 */
Simon Glass7bf5b9e2015-01-01 16:18:07 -0700138int mtrr_commit(bool do_caches);
Simon Glass98f139b2014-11-12 22:42:10 -0700139
Simon Glass753297d2019-09-25 08:56:46 -0600140/**
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
Bin Menge41f0d22021-07-31 16:45:26 +0800147 * @size: Size, must be power of 2
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100148 * Return: 0 on success, -EINVAL if size is not power of 2,
Bin Menge41f0d22021-07-31 16:45:26 +0800149 * -ENOSPC if there are no more MTRRs
Simon Glass753297d2019-09-25 08:56:46 -0600150 */
151int mtrr_set_next_var(uint type, uint64_t base, uint64_t size);
152
Simon Glass7403c262020-07-17 08:48:22 -0600153/**
154 * mtrr_read_all() - Save all the MTRRs
155 *
156 * This reads all MTRRs from the boot CPU into a struct so they can be loaded
157 * onto other CPUs
158 *
159 * @info: Place to put the MTRR info
160 */
161void mtrr_read_all(struct mtrr_info *info);
162
Simon Glassd89e15f2020-07-17 08:48:26 -0600163/**
164 * mtrr_set_valid() - Set the valid flag for a selected MTRR and CPU(s)
165 *
166 * @cpu_select: Selected CPUs (either a CPU number or MP_SELECT_...)
167 * @reg: MTRR register to write (0-7)
168 * @valid: Valid flag to write
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100169 * Return: 0 on success, -ve on error
Simon Glassd89e15f2020-07-17 08:48:26 -0600170 */
171int mtrr_set_valid(int cpu_select, int reg, bool valid);
172
173/**
Wolfgang Wallner2282aab2021-03-23 10:39:09 +0100174 * mtrr_set() - Set the base address and mask for a selected MTRR and CPU(s)
Simon Glassd89e15f2020-07-17 08:48:26 -0600175 *
176 * @cpu_select: Selected CPUs (either a CPU number or MP_SELECT_...)
177 * @reg: MTRR register to write (0-7)
178 * @base: Base address and MTRR_BASE_TYPE_MASK
179 * @mask: Mask and MTRR_PHYS_MASK_VALID
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100180 * Return: 0 on success, -ve on error
Simon Glassd89e15f2020-07-17 08:48:26 -0600181 */
182int mtrr_set(int cpu_select, int reg, u64 base, u64 mask);
183
Simon Glassfbf120c2020-09-22 14:54:51 -0600184/**
185 * mtrr_get_var_count() - Get the number of variable MTRRs
186 *
187 * Some CPUs have more than 8 MTRRs. This function returns the actual number
188 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100189 * Return: number of variable MTRRs
Simon Glassfbf120c2020-09-22 14:54:51 -0600190 */
191int mtrr_get_var_count(void);
192
Simon Glassfb842432023-07-15 21:38:36 -0600193/**
194 * mtrr_list() - List the MTRRs
195 *
196 * Shows a list of all the MTRRs including their values
197 *
198 * @reg_count: Number of registers to show. You can use mtrr_get_var_count() for
199 * this
200 * @cpu_select: CPU to use. Use MP_SELECT_BSP for the boot CPU
201 * Returns: 0 if OK, -ve if the CPU was not found
202 */
203int mtrr_list(int reg_count, int cpu_select);
204
205/**
206 * mtrr_get_type_by_name() - Get the type of an MTRR given its type name
207 *
208 * @typename: Name to check
209 * Returns: MTRR type (MTRR_TYPE_...) or -EINVAL if invalid
210 */
211int mtrr_get_type_by_name(const char *typename);
212
Simon Glass98f139b2014-11-12 22:42:10 -0700213#endif
214
Simon Glass98f139b2014-11-12 22:42:10 -0700215#if ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE - 1)) != 0)
216# error "CONFIG_XIP_ROM_SIZE is not a power of 2"
217#endif
218
219#if ((CONFIG_CACHE_ROM_SIZE & (CONFIG_CACHE_ROM_SIZE - 1)) != 0)
220# error "CONFIG_CACHE_ROM_SIZE is not a power of 2"
221#endif
222
223#define CACHE_ROM_BASE (((1 << 20) - (CONFIG_CACHE_ROM_SIZE >> 12)) << 12)
224
Simon Glass98f139b2014-11-12 22:42:10 -0700225#endif