blob: ac8765f3cf2e17ece077f9463c9838c697793b9c [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{
25 state->enable_cache = dcache_status();
26
27 if (state->enable_cache)
28 disable_caches();
29 state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
30 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
31}
32
33/* Clean up after adjusting MTRRs, and enable them */
34void mtrr_close(struct mtrr_state *state)
35{
36 wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
37 if (state->enable_cache)
38 enable_caches();
39}
40
41int mtrr_commit(bool do_caches)
42{
43 struct mtrr_request *req = gd->arch.mtrr_req;
44 struct mtrr_state state;
45 uint64_t mask;
46 int i;
47
48 mtrr_open(&state);
49 for (i = 0; i < gd->arch.mtrr_req_count; i++, req++) {
50 mask = ~(req->size - 1);
51 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
52 wrmsrl(MTRR_PHYS_BASE_MSR(i), req->start | req->type);
53 wrmsrl(MTRR_PHYS_MASK_MSR(i), mask | MTRR_PHYS_MASK_VALID);
54 }
55
56 /* Clear the ones that are unused */
57 for (; i < MTRR_COUNT; i++)
58 wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
59 mtrr_close(&state);
60
61 return 0;
62}
63
64int mtrr_add_request(int type, uint64_t start, uint64_t size)
65{
66 struct mtrr_request *req;
67 uint64_t mask;
68
69 if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
70 return -ENOSPC;
71 req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
72 req->type = type;
73 req->start = start;
74 req->size = size;
75 debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
76 req->type, req->start, req->size);
77 mask = ~(req->size - 1);
78 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
79 mask |= MTRR_PHYS_MASK_VALID;
80 debug(" %016llx %016llx\n", req->start | req->type, mask);
81
82 return 0;
83}