blob: 5d36b3e0204d63cd9a15d2829cd9c96d48100fe2 [file] [log] [blame]
Simon Glass7bf5b9e2015-01-01 16:18:07 -07001/*
2 * (C) Copyright 2014 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 *
6 * Memory Type Range Regsters - these are used to tell the CPU whether
7 * memory is cacheable and if so the cache write mode to use.
8 *
9 * These can speed up booting. See the mtrr command.
10 *
11 * Reference: Intel Architecture Software Developer's Manual, Volume 3:
12 * System Programming
13 */
14
15#include <common.h>
16#include <asm/io.h>
17#include <asm/msr.h>
18#include <asm/mtrr.h>
19
Bin Meng068fb352015-01-22 11:29:39 +080020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass7bf5b9e2015-01-01 16:18:07 -070022/* Prepare to adjust MTRRs */
23void mtrr_open(struct mtrr_state *state)
24{
Bin Meng80d29762015-01-22 11:29:41 +080025 if (!gd->arch.has_mtrr)
26 return;
27
Simon Glass7bf5b9e2015-01-01 16:18:07 -070028 state->enable_cache = dcache_status();
29
30 if (state->enable_cache)
31 disable_caches();
32 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
33 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
34}
35
36/* Clean up after adjusting MTRRs, and enable them */
37void mtrr_close(struct mtrr_state *state)
38{
Bin Meng80d29762015-01-22 11:29:41 +080039 if (!gd->arch.has_mtrr)
40 return;
41
Simon Glass7bf5b9e2015-01-01 16:18:07 -070042 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
43 if (state->enable_cache)
44 enable_caches();
45}
46
47int mtrr_commit(bool do_caches)
48{
49 struct mtrr_request *req = gd->arch.mtrr_req;
50 struct mtrr_state state;
51 uint64_t mask;
52 int i;
53
Bin Meng80d29762015-01-22 11:29:41 +080054 if (!gd->arch.has_mtrr)
55 return -ENOSYS;
56
Simon Glass7bf5b9e2015-01-01 16:18:07 -070057 mtrr_open(&state);
58 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++) {
59 mask = ~(req->size - 1);
60 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
61 wrmsrl(MTRR_PHYS_BASE_MSR(i), req->start | req->type);
62 wrmsrl(MTRR_PHYS_MASK_MSR(i), mask | MTRR_PHYS_MASK_VALID);
63 }
64
65 /* Clear the ones that are unused */
66 for (; i < MTRR_COUNT; i++)
67 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
68 mtrr_close(&state);
69
70 return 0;
71}
72
73int mtrr_add_request(int type, uint64_t start, uint64_t size)
74{
75 struct mtrr_request *req;
76 uint64_t mask;
77
Bin Meng80d29762015-01-22 11:29:41 +080078 if (!gd->arch.has_mtrr)
79 return -ENOSYS;
80
Simon Glass7bf5b9e2015-01-01 16:18:07 -070081 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
82 return -ENOSPC;
83 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
84 req->type = type;
85 req->start = start;
86 req->size = size;
87 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
88 req->type, req->start, req->size);
89 mask = ~(req->size - 1);
90 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
91 mask |= MTRR_PHYS_MASK_VALID;
92 debug(" %016llx %016llx\n", req->start | req->type, mask);
93
94 return 0;
95}