Skip to content

Commit 2ff4b59

Browse files
kaushlenlenb
authored andcommitted
tools/power x86_energy_perf_policy: Add Android MSR device support
Add support for Android MSR device paths which use /dev/msrN format instead of the standard Linux /dev/cpu/N/msr format. The tool now probes both path formats at startup and uses the appropriate one. This enables x86_energy_perf_policy to work on Android systems where MSR devices follow a different naming convention while maintaining full compatibility with standard Linux systems. Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent d71cb40 commit 2ff4b59

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ unsigned int bdx_highest_ratio;
9595
#define PATH_TO_CPU "/sys/devices/system/cpu/"
9696
#define SYSFS_PATH_MAX 255
9797

98+
/* keep Default as a linux path */
99+
static int use_android_msr_path;
100+
98101
/*
99102
* maintain compatibility with original implementation, but don't document it:
100103
*/
@@ -678,16 +681,41 @@ void err_on_hypervisor(void)
678681
"not supported on this virtual machine");
679682
}
680683

684+
static void probe_msr_path_format(void)
685+
{
686+
struct stat sb;
687+
char test_path[32];
688+
689+
/* Test standard Linux path */
690+
sprintf(test_path, "/dev/cpu/%d/msr", base_cpu);
691+
if (stat(test_path, &sb) == 0) {
692+
use_android_msr_path = 0;
693+
return;
694+
}
695+
696+
/* Test Android-style path */
697+
sprintf(test_path, "/dev/msr%d", base_cpu);
698+
if (stat(test_path, &sb) == 0) {
699+
use_android_msr_path = 1;
700+
return;
701+
}
702+
703+
/* If neither exists, keep the default Linux format */
704+
use_android_msr_path = 0;
705+
}
706+
681707
int get_msr(int cpu, int offset, unsigned long long *msr)
682708
{
683709
int retval;
684710
char pathname[32];
685711
int fd;
686712

687-
sprintf(pathname, "/dev/cpu/%d/msr", cpu);
713+
sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", cpu);
688714
fd = open(pathname, O_RDONLY);
689715
if (fd < 0)
690-
err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
716+
err(-1, "%s open failed, try chown or chmod +r %s, or run as root",
717+
pathname, use_android_msr_path ? "/dev/msr*" : "/dev/cpu/*/msr");
718+
691719

692720
retval = pread(fd, msr, sizeof(*msr), offset);
693721
if (retval != sizeof(*msr)) {
@@ -708,10 +736,11 @@ int put_msr(int cpu, int offset, unsigned long long new_msr)
708736
int retval;
709737
int fd;
710738

711-
sprintf(pathname, "/dev/cpu/%d/msr", cpu);
739+
sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", cpu);
712740
fd = open(pathname, O_RDWR);
713741
if (fd < 0)
714-
err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
742+
err(-1, "%s open failed, try chown or chmod +r %s, or run as root",
743+
pathname, use_android_msr_path ? "/dev/msr*" : "/dev/cpu/*/msr");
715744

716745
retval = pwrite(fd, &new_msr, sizeof(new_msr), offset);
717746
if (retval != sizeof(new_msr))
@@ -1427,10 +1456,15 @@ void probe_dev_msr(void)
14271456
struct stat sb;
14281457
char pathname[32];
14291458

1430-
sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1431-
if (stat(pathname, &sb))
1432-
if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1433-
err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1459+
sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", base_cpu);
1460+
if (stat(pathname, &sb)) {
1461+
if (system("/sbin/modprobe msr > /dev/null 2>&1")) {
1462+
if (use_android_msr_path)
1463+
err(-5, "no /dev/msr0, Try \"# modprobe msr\" ");
1464+
else
1465+
err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1466+
}
1467+
}
14341468
}
14351469

14361470
static void get_cpuid_or_exit(unsigned int leaf,
@@ -1547,6 +1581,10 @@ void parse_cpuid(void)
15471581
int main(int argc, char **argv)
15481582
{
15491583
set_base_cpu();
1584+
1585+
/* probe MSR path */
1586+
probe_msr_path_format();
1587+
15501588
probe_dev_msr();
15511589
init_data_structures();
15521590

0 commit comments

Comments
 (0)