1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 Google, Inc
11 static const char *const mtrr_type_name[MTRR_TYPE_COUNT] = {
21 static int do_mtrr_list(void)
25 printf("Reg Valid Write-type %-16s %-16s %-16s\n", "Base ||",
26 "Mask ||", "Size ||");
27 for (i = 0; i < MTRR_COUNT; i++) {
28 const char *type = "Invalid";
29 uint64_t base, mask, size;
32 base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
33 mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
34 size = ~mask & ((1ULL << CONFIG_CPU_ADDR_BITS) - 1);
35 size |= (1 << 12) - 1;
37 valid = mask & MTRR_PHYS_MASK_VALID;
38 type = mtrr_type_name[base & MTRR_BASE_TYPE_MASK];
39 printf("%d %-5s %-12s %016llx %016llx %016llx\n", i,
40 valid ? "Y" : "N", type, base & ~MTRR_BASE_TYPE_MASK,
41 mask & ~MTRR_PHYS_MASK_VALID, size);
47 static int do_mtrr_set(uint reg, int argc, char *const argv[])
49 const char *typename = argv[0];
50 struct mtrr_state state;
58 for (i = 0; i < MTRR_TYPE_COUNT; i++) {
59 if (*typename == *mtrr_type_name[i])
63 printf("Invalid type name %s\n", typename);
66 start = simple_strtoul(argv[1], NULL, 16);
67 size = simple_strtoul(argv[2], NULL, 16);
70 valid = native_read_msr(MTRR_PHYS_MASK_MSR(reg)) & MTRR_PHYS_MASK_VALID;
71 mask = ~((uint64_t)size - 1);
72 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
74 mask |= MTRR_PHYS_MASK_VALID;
76 printf("base=%llx, mask=%llx\n", base, mask);
77 mtrr_open(&state, true);
78 wrmsrl(MTRR_PHYS_BASE_MSR(reg), base);
79 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask);
80 mtrr_close(&state, true);
85 static int mtrr_set_valid(int reg, bool valid)
87 struct mtrr_state state;
90 mtrr_open(&state, true);
91 mask = native_read_msr(MTRR_PHYS_MASK_MSR(reg));
93 mask |= MTRR_PHYS_MASK_VALID;
95 mask &= ~MTRR_PHYS_MASK_VALID;
96 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask);
97 mtrr_close(&state, true);
102 static int do_mtrr(struct cmd_tbl *cmdtp, int flag, int argc,
109 if (argc < 2 || *cmd == 'l')
110 return do_mtrr_list();
114 return CMD_RET_USAGE;
115 reg = simple_strtoul(argv[0], NULL, 16);
116 if (reg >= MTRR_COUNT) {
117 printf("Invalid register number\n");
118 return CMD_RET_USAGE;
121 return mtrr_set_valid(reg, true);
122 else if (*cmd == 'd')
123 return mtrr_set_valid(reg, false);
124 else if (*cmd == 's')
125 return do_mtrr_set(reg, argc - 1, argv + 1);
127 return CMD_RET_USAGE;
134 "Use x86 memory type range registers (32-bit only)",
135 "[list] - list current registers\n"
136 "set <reg> <type> <start> <size> - set a register\n"
137 "\t<type> is Uncacheable, Combine, Through, Protect, Back\n"
138 "disable <reg> - disable a register\n"
139 "ensable <reg> - enable a register"