blob: 30940065621dfb47cf601b8b97d99b5234e45892 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7bf5b9e2015-01-01 16:18:07 -07002/*
3 * (C) Copyright 2014 Google, Inc
4 *
Simon Glass7bf5b9e2015-01-01 16:18:07 -07005 * 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#include <common.h>
15#include <asm/io.h>
16#include <asm/msr.h>
17#include <asm/mtrr.h>
18
Bin Meng068fb352015-01-22 11:29:39 +080019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass7bf5b9e2015-01-01 16:18:07 -070021/* Prepare to adjust MTRRs */
22void mtrr_open(struct mtrr_state *state)
23{
Bin Meng80d29762015-01-22 11:29:41 +080024 if (!gd->arch.has_mtrr)
25 return;
26
Simon Glass7bf5b9e2015-01-01 16:18:07 -070027 state->enable_cache = dcache_status();
28
29 if (state->enable_cache)
30 disable_caches();
31 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
32 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
33}
34
35/* Clean up after adjusting MTRRs, and enable them */
36void mtrr_close(struct mtrr_state *state)
37{
Bin Meng80d29762015-01-22 11:29:41 +080038 if (!gd->arch.has_mtrr)
39 return;
40
Simon Glass7bf5b9e2015-01-01 16:18:07 -070041 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
42 if (state->enable_cache)
43 enable_caches();
44}
45
46int mtrr_commit(bool do_caches)
47{
48 struct mtrr_request *req = gd->arch.mtrr_req;
49 struct mtrr_state state;
50 uint64_t mask;
51 int i;
52
Bin Meng80d29762015-01-22 11:29:41 +080053 if (!gd->arch.has_mtrr)
54 return -ENOSYS;
55
Simon Glass7bf5b9e2015-01-01 16:18:07 -070056 mtrr_open(&state);
57 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++) {
58 mask = ~(req->size - 1);
59 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
60 wrmsrl(MTRR_PHYS_BASE_MSR(i), req->start | req->type);
61 wrmsrl(MTRR_PHYS_MASK_MSR(i), mask | MTRR_PHYS_MASK_VALID);
62 }
63
64 /* Clear the ones that are unused */
65 for (; i < MTRR_COUNT; i++)
66 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
67 mtrr_close(&state);
68
69 return 0;
70}
71
72int mtrr_add_request(int type, uint64_t start, uint64_t size)
73{
74 struct mtrr_request *req;
75 uint64_t mask;
76
Bin Meng80d29762015-01-22 11:29:41 +080077 if (!gd->arch.has_mtrr)
78 return -ENOSYS;
79
Simon Glass7bf5b9e2015-01-01 16:18:07 -070080 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
81 return -ENOSPC;
82 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
83 req->type = type;
84 req->start = start;
85 req->size = size;
86 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
87 req->type, req->start, req->size);
88 mask = ~(req->size - 1);
89 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
90 mask |= MTRR_PHYS_MASK_VALID;
91 debug(" %016llx %016llx\n", req->start | req->type, mask);
92
93 return 0;
94}