Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / power / cpupower / utils / helpers / msr.c
blobab9950748838a901345cd7acb19ab10816160c7d
1 // SPDX-License-Identifier: GPL-2.0
2 #if defined(__i386__) || defined(__x86_64__)
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <stdint.h>
9 #include "helpers/helpers.h"
11 /* Intel specific MSRs */
12 #define MSR_IA32_PERF_STATUS 0x198
13 #define MSR_IA32_MISC_ENABLES 0x1a0
14 #define MSR_IA32_ENERGY_PERF_BIAS 0x1b0
15 #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad
18 * read_msr
20 * Will return 0 on success and -1 on failure.
21 * Possible errno values could be:
22 * EFAULT -If the read/write did not fully complete
23 * EIO -If the CPU does not support MSRs
24 * ENXIO -If the CPU does not exist
27 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
29 int fd;
30 char msr_file_name[64];
32 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
33 fd = open(msr_file_name, O_RDONLY);
34 if (fd < 0)
35 return -1;
36 if (lseek(fd, idx, SEEK_CUR) == -1)
37 goto err;
38 if (read(fd, val, sizeof *val) != sizeof *val)
39 goto err;
40 close(fd);
41 return 0;
42 err:
43 close(fd);
44 return -1;
48 * write_msr
50 * Will return 0 on success and -1 on failure.
51 * Possible errno values could be:
52 * EFAULT -If the read/write did not fully complete
53 * EIO -If the CPU does not support MSRs
54 * ENXIO -If the CPU does not exist
56 int write_msr(int cpu, unsigned int idx, unsigned long long val)
58 int fd;
59 char msr_file_name[64];
61 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
62 fd = open(msr_file_name, O_WRONLY);
63 if (fd < 0)
64 return -1;
65 if (lseek(fd, idx, SEEK_CUR) == -1)
66 goto err;
67 if (write(fd, &val, sizeof val) != sizeof val)
68 goto err;
69 close(fd);
70 return 0;
71 err:
72 close(fd);
73 return -1;
76 int msr_intel_get_perf_bias(unsigned int cpu)
78 unsigned long long val;
79 int ret;
81 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
82 return -1;
84 ret = read_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &val);
85 if (ret)
86 return ret;
87 return val;
90 int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
92 int ret;
94 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
95 return -1;
97 ret = write_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, val);
98 if (ret)
99 return ret;
100 return 0;
103 unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
105 unsigned long long val;
106 int ret;
108 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO))
109 return -1;
111 ret = read_msr(cpu, MSR_NEHALEM_TURBO_RATIO_LIMIT, &val);
112 if (ret)
113 return ret;
114 return val;
116 #endif