1 // SPDX-License-Identifier: GPL-2.0
2 #if defined(__i386__) || defined(__x86_64__)
3 
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <stdint.h>
8 
9 #include "helpers/helpers.h"
10 
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
16 
17 /*
18  * read_msr
19  *
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
25  */
26 
read_msr(int cpu,unsigned int idx,unsigned long long * val)27 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
28 {
29 	int fd;
30 	char msr_file_name[64];
31 
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;
45 }
46 
47 /*
48  * write_msr
49  *
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
55  */
write_msr(int cpu,unsigned int idx,unsigned long long val)56 int write_msr(int cpu, unsigned int idx, unsigned long long val)
57 {
58 	int fd;
59 	char msr_file_name[64];
60 
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;
74 }
75 
msr_intel_get_perf_bias(unsigned int cpu)76 int msr_intel_get_perf_bias(unsigned int cpu)
77 {
78 	unsigned long long val;
79 	int ret;
80 
81 	if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
82 		return -1;
83 
84 	ret = read_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &val);
85 	if (ret)
86 		return ret;
87 	return val;
88 }
89 
msr_intel_set_perf_bias(unsigned int cpu,unsigned int val)90 int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
91 {
92 	int ret;
93 
94 	if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
95 		return -1;
96 
97 	ret = write_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, val);
98 	if (ret)
99 		return ret;
100 	return 0;
101 }
102 
msr_intel_get_turbo_ratio(unsigned int cpu)103 unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
104 {
105 	unsigned long long val;
106 	int ret;
107 
108 	if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO))
109 		return -1;
110 
111 	ret = read_msr(cpu, MSR_NEHALEM_TURBO_RATIO_LIMIT, &val);
112 	if (ret)
113 		return ret;
114 	return val;
115 }
116 #endif
117