Prepare v2023.10
[platform/kernel/u-boot.git] / arch / x86 / cpu / mtrr.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014 Google, Inc
4  *
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
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
19 #include <common.h>
20 #include <cpu_func.h>
21 #include <log.h>
22 #include <sort.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
25 #include <asm/io.h>
26 #include <asm/mp.h>
27 #include <asm/msr.h>
28 #include <asm/mtrr.h>
29 #include <linux/log2.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
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
43 /* Prepare to adjust MTRRs */
44 void mtrr_open(struct mtrr_state *state, bool do_caches)
45 {
46         if (!gd->arch.has_mtrr)
47                 return;
48
49         if (do_caches) {
50                 state->enable_cache = dcache_status();
51
52                 if (state->enable_cache)
53                         disable_caches();
54         }
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 */
60 void mtrr_close(struct mtrr_state *state, bool do_caches)
61 {
62         if (!gd->arch.has_mtrr)
63                 return;
64
65         wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
66         if (do_caches && state->enable_cache)
67                 enable_caches();
68 }
69
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
80 void mtrr_read_all(struct mtrr_info *info)
81 {
82         int reg_count = mtrr_get_var_count();
83         int i;
84
85         for (i = 0; i < reg_count; i++) {
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
91 void mtrr_write_all(struct mtrr_info *info)
92 {
93         int reg_count = mtrr_get_var_count();
94         struct mtrr_state state;
95         int i;
96
97         for (i = 0; i < reg_count; i++) {
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  *
122  * Return: 0 on success, -ve on failure
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
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
152 int mtrr_commit(bool do_caches)
153 {
154         struct mtrr_request *req = gd->arch.mtrr_req;
155         struct mtrr_state state;
156         int ret;
157         int i;
158
159         debug("%s: enabled=%d, count=%d\n", __func__, gd->arch.has_mtrr,
160               gd->arch.mtrr_req_count);
161         if (!gd->arch.has_mtrr)
162                 return -ENOSYS;
163
164         debug("open\n");
165         mtrr_open(&state, do_caches);
166         debug("open done\n");
167         qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr);
168         for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
169                 set_var_mtrr(i, req->type, req->start, req->size);
170
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);
175         debug("close\n");
176         mtrr_close(&state, do_caches);
177         debug("mtrr done\n");
178
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
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
193         debug("%s: count=%d\n", __func__, gd->arch.mtrr_req_count);
194         if (!gd->arch.has_mtrr)
195                 return -ENOSYS;
196
197         if (!is_power_of_2(size))
198                 return -EINVAL;
199
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 }
215
216 int mtrr_get_var_count(void)
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
227         vcnt = mtrr_get_var_count();
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
244         if (!is_power_of_2(size))
245                 return -EINVAL;
246
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 }
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 }
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 };