2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-01-01 23:18:15 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:03 +00:00
|
|
|
#include <command.h>
|
2020-07-17 14:48:22 +00:00
|
|
|
#include <log.h>
|
2015-01-01 23:18:15 +00:00
|
|
|
#include <asm/msr.h>
|
2020-07-17 14:48:22 +00:00
|
|
|
#include <asm/mp.h>
|
2015-01-01 23:18:15 +00:00
|
|
|
#include <asm/mtrr.h>
|
|
|
|
|
2020-07-17 14:48:27 +00:00
|
|
|
static int do_mtrr_set(int cpu_select, uint reg, int argc, char *const argv[])
|
2015-01-01 23:18:15 +00:00
|
|
|
{
|
|
|
|
const char *typename = argv[0];
|
|
|
|
uint32_t start, size;
|
|
|
|
uint64_t base, mask;
|
2023-07-16 03:38:36 +00:00
|
|
|
int type = -1;
|
2015-01-01 23:18:15 +00:00
|
|
|
bool valid;
|
2020-07-17 14:48:27 +00:00
|
|
|
int ret;
|
2015-01-01 23:18:15 +00:00
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
return CMD_RET_USAGE;
|
2023-07-16 03:38:36 +00:00
|
|
|
type = mtrr_get_type_by_name(typename);
|
|
|
|
if (type < 0) {
|
2015-01-01 23:18:15 +00:00
|
|
|
printf("Invalid type name %s\n", typename);
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
2021-07-24 15:03:29 +00:00
|
|
|
start = hextoul(argv[1], NULL);
|
|
|
|
size = hextoul(argv[2], NULL);
|
2015-01-01 23:18:15 +00:00
|
|
|
|
|
|
|
base = start | type;
|
|
|
|
valid = native_read_msr(MTRR_PHYS_MASK_MSR(reg)) & MTRR_PHYS_MASK_VALID;
|
|
|
|
mask = ~((uint64_t)size - 1);
|
|
|
|
mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
|
|
|
|
if (valid)
|
|
|
|
mask |= MTRR_PHYS_MASK_VALID;
|
|
|
|
|
2020-07-17 14:48:27 +00:00
|
|
|
ret = mtrr_set(cpu_select, reg, base, mask);
|
|
|
|
if (ret)
|
|
|
|
return CMD_RET_FAILURE;
|
2015-01-01 23:18:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_mtrr(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2015-01-01 23:18:15 +00:00
|
|
|
{
|
2020-09-22 20:54:51 +00:00
|
|
|
int reg_count = mtrr_get_var_count();
|
2020-07-17 14:48:28 +00:00
|
|
|
int cmd;
|
2020-07-17 14:48:22 +00:00
|
|
|
int cpu_select;
|
2015-01-01 23:18:15 +00:00
|
|
|
uint reg;
|
2020-07-17 14:48:28 +00:00
|
|
|
int ret;
|
2015-01-01 23:18:15 +00:00
|
|
|
|
2020-07-17 14:48:22 +00:00
|
|
|
cpu_select = MP_SELECT_BSP;
|
2020-07-17 14:48:29 +00:00
|
|
|
if (argc >= 3 && !strcmp("-c", argv[1])) {
|
|
|
|
const char *cpustr;
|
|
|
|
|
|
|
|
cpustr = argv[2];
|
|
|
|
if (*cpustr == 'a')
|
|
|
|
cpu_select = MP_SELECT_ALL;
|
|
|
|
else
|
|
|
|
cpu_select = simple_strtol(cpustr, NULL, 16);
|
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
|
|
|
}
|
2020-07-17 14:48:28 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
cmd = argv[0] ? *argv[0] : 0;
|
|
|
|
if (argc < 1 || !cmd) {
|
|
|
|
cmd = 'l';
|
|
|
|
reg = 0;
|
2020-08-14 07:55:24 +00:00
|
|
|
}
|
|
|
|
if (cmd != 'l') {
|
2020-07-17 14:48:28 +00:00
|
|
|
if (argc < 2)
|
|
|
|
return CMD_RET_USAGE;
|
2021-07-24 15:03:29 +00:00
|
|
|
reg = hextoul(argv[1], NULL);
|
2020-09-22 20:54:51 +00:00
|
|
|
if (reg >= reg_count) {
|
2020-07-17 14:48:28 +00:00
|
|
|
printf("Invalid register number\n");
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cmd == 'l') {
|
2020-07-17 14:48:31 +00:00
|
|
|
bool first;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = mp_first_cpu(cpu_select);
|
|
|
|
if (i < 0) {
|
|
|
|
printf("Invalid CPU (err=%d)\n", i);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
first = true;
|
|
|
|
for (; i >= 0; i = mp_next_cpu(cpu_select, i)) {
|
|
|
|
if (!first)
|
|
|
|
printf("\n");
|
|
|
|
printf("CPU %d:\n", i);
|
2023-07-16 03:38:36 +00:00
|
|
|
ret = mtrr_list(reg_count, i);
|
2020-07-17 14:48:31 +00:00
|
|
|
if (ret) {
|
2023-05-04 22:54:54 +00:00
|
|
|
printf("Failed to read CPU %s (err=%d)\n",
|
|
|
|
i < MP_SELECT_ALL ? simple_itoa(i) : "",
|
2020-07-17 14:48:31 +00:00
|
|
|
ret);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
2020-07-17 14:48:28 +00:00
|
|
|
} else {
|
|
|
|
switch (cmd) {
|
|
|
|
case 'e':
|
|
|
|
ret = mtrr_set_valid(cpu_select, reg, true);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
ret = mtrr_set_valid(cpu_select, reg, false);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
ret = do_mtrr_set(cpu_select, reg, argc - 2, argv + 2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
printf("Operation failed (err=%d)\n", ret);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
2015-01-01 23:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
2020-07-17 14:48:29 +00:00
|
|
|
mtrr, 8, 1, do_mtrr,
|
2015-01-01 23:18:15 +00:00
|
|
|
"Use x86 memory type range registers (32-bit only)",
|
|
|
|
"[list] - list current registers\n"
|
|
|
|
"set <reg> <type> <start> <size> - set a register\n"
|
|
|
|
"\t<type> is Uncacheable, Combine, Through, Protect, Back\n"
|
|
|
|
"disable <reg> - disable a register\n"
|
2020-07-17 14:48:29 +00:00
|
|
|
"enable <reg> - enable a register\n"
|
|
|
|
"\n"
|
|
|
|
"Precede command with '-c <n>|all' to access a particular hex CPU, e.g.\n"
|
|
|
|
" mtrr -c all list; mtrr -c 2e list"
|
2015-01-01 23:18:15 +00:00
|
|
|
);
|