1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 Google, Inc
10 static const char *const mtrr_type_name[MTRR_TYPE_COUNT] = {
20 static int do_mtrr_list(void)
24 printf("Reg Valid Write-type %-16s %-16s %-16s\n", "Base ||",
25 "Mask ||", "Size ||");
26 for (i = 0; i < MTRR_COUNT; i++) {
27 const char *type = "Invalid";
28 uint64_t base, mask, size;
31 base = native_read_msr(MTRR_PHYS_BASE_MSR(i));
32 mask = native_read_msr(MTRR_PHYS_MASK_MSR(i));
33 size = ~mask & ((1ULL << CONFIG_CPU_ADDR_BITS) - 1);
34 size |= (1 << 12) - 1;
36 valid = mask & MTRR_PHYS_MASK_VALID;
37 type = mtrr_type_name[base & MTRR_BASE_TYPE_MASK];
38 printf("%d %-5s %-12s %016llx %016llx %016llx\n", i,
39 valid ? "Y" : "N", type, base & ~MTRR_BASE_TYPE_MASK,
40 mask & ~MTRR_PHYS_MASK_VALID, size);
46 static int do_mtrr_set(uint reg, int argc, char * const argv[])
48 const char *typename = argv[0];
49 struct mtrr_state state;
57 for (i = 0; i < MTRR_TYPE_COUNT; i++) {
58 if (*typename == *mtrr_type_name[i])
62 printf("Invalid type name %s\n", typename);
65 start = simple_strtoul(argv[1], NULL, 16);
66 size = simple_strtoul(argv[2], NULL, 16);
69 valid = native_read_msr(MTRR_PHYS_MASK_MSR(reg)) & MTRR_PHYS_MASK_VALID;
70 mask = ~((uint64_t)size - 1);
71 mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
73 mask |= MTRR_PHYS_MASK_VALID;
75 printf("base=%llx, mask=%llx\n", base, mask);
76 mtrr_open(&state, true);
77 wrmsrl(MTRR_PHYS_BASE_MSR(reg), base);
78 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask);
79 mtrr_close(&state, true);
84 static int mtrr_set_valid(int reg, bool valid)
86 struct mtrr_state state;
89 mtrr_open(&state, true);
90 mask = native_read_msr(MTRR_PHYS_MASK_MSR(reg));
92 mask |= MTRR_PHYS_MASK_VALID;
94 mask &= ~MTRR_PHYS_MASK_VALID;
95 wrmsrl(MTRR_PHYS_MASK_MSR(reg), mask);
96 mtrr_close(&state, true);
101 static int do_mtrr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
107 if (argc < 2 || *cmd == 'l')
108 return do_mtrr_list();
112 return CMD_RET_USAGE;
113 reg = simple_strtoul(argv[0], NULL, 16);
114 if (reg >= MTRR_COUNT) {
115 printf("Invalid register number\n");
116 return CMD_RET_USAGE;
119 return mtrr_set_valid(reg, true);
120 else if (*cmd == 'd')
121 return mtrr_set_valid(reg, false);
122 else if (*cmd == 's')
123 return do_mtrr_set(reg, argc - 1, argv + 1);
125 return CMD_RET_USAGE;
132 "Use x86 memory type range registers (32-bit only)",
133 "[list] - list current registers\n"
134 "set <reg> <type> <start> <size> - set a register\n"
135 "\t<type> is Uncacheable, Combine, Through, Protect, Back\n"
136 "disable <reg> - disable a register\n"
137 "ensable <reg> - enable a register"