Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / power / cpupower / utils / helpers / misc.c
blob650b9a9a6584fe9b5547dc129e3682ee617e864d
1 // SPDX-License-Identifier: GPL-2.0
3 #include <stdio.h>
4 #include <errno.h>
5 #include <stdlib.h>
7 #include "helpers/helpers.h"
8 #include "helpers/sysfs.h"
10 #if defined(__i386__) || defined(__x86_64__)
12 #include "cpupower_intern.h"
14 #define MSR_AMD_HWCR 0xc0010015
16 int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active,
17 int *states)
19 struct cpupower_cpu_info cpu_info;
20 int ret;
21 unsigned long long val;
23 *support = *active = *states = 0;
25 ret = get_cpu_info(&cpu_info);
26 if (ret)
27 return ret;
29 if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_CBP) {
30 *support = 1;
32 /* AMD Family 0x17 does not utilize PCI D18F4 like prior
33 * families and has no fixed discrete boost states but
34 * has Hardware determined variable increments instead.
37 if (cpu_info.family == 0x17 || cpu_info.family == 0x18) {
38 if (!read_msr(cpu, MSR_AMD_HWCR, &val)) {
39 if (!(val & CPUPOWER_AMD_CPBDIS))
40 *active = 1;
42 } else {
43 ret = amd_pci_get_num_boost_states(active, states);
44 if (ret)
45 return ret;
47 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_INTEL_IDA)
48 *support = *active = 1;
49 return 0;
52 int cpupower_intel_get_perf_bias(unsigned int cpu)
54 char linebuf[MAX_LINE_LEN];
55 char path[SYSFS_PATH_MAX];
56 unsigned long val;
57 char *endp;
59 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
60 return -1;
62 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
64 if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)
65 return -1;
67 val = strtol(linebuf, &endp, 0);
68 if (endp == linebuf || errno == ERANGE)
69 return -1;
71 return val;
74 int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
76 char path[SYSFS_PATH_MAX];
77 char linebuf[3] = {};
79 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
80 return -1;
82 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
83 snprintf(linebuf, sizeof(linebuf), "%d", val);
85 if (cpupower_write_sysfs(path, linebuf, 3) <= 0)
86 return -1;
88 return 0;
91 #endif /* #if defined(__i386__) || defined(__x86_64__) */
93 /* get_cpustate
95 * Gather the information of all online CPUs into bitmask struct
97 void get_cpustate(void)
99 unsigned int cpu = 0;
101 bitmask_clearall(online_cpus);
102 bitmask_clearall(offline_cpus);
104 for (cpu = bitmask_first(cpus_chosen);
105 cpu <= bitmask_last(cpus_chosen); cpu++) {
107 if (cpupower_is_cpu_online(cpu) == 1)
108 bitmask_setbit(online_cpus, cpu);
109 else
110 bitmask_setbit(offline_cpus, cpu);
112 continue;
116 /* print_online_cpus
118 * Print the CPU numbers of all CPUs that are online currently
120 void print_online_cpus(void)
122 int str_len = 0;
123 char *online_cpus_str = NULL;
125 str_len = online_cpus->size * 5;
126 online_cpus_str = (void *)malloc(sizeof(char) * str_len);
128 if (!bitmask_isallclear(online_cpus)) {
129 bitmask_displaylist(online_cpus_str, str_len, online_cpus);
130 printf(_("Following CPUs are online:\n%s\n"), online_cpus_str);
134 /* print_offline_cpus
136 * Print the CPU numbers of all CPUs that are offline currently
138 void print_offline_cpus(void)
140 int str_len = 0;
141 char *offline_cpus_str = NULL;
143 str_len = offline_cpus->size * 5;
144 offline_cpus_str = (void *)malloc(sizeof(char) * str_len);
146 if (!bitmask_isallclear(offline_cpus)) {
147 bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);
148 printf(_("Following CPUs are offline:\n%s\n"), offline_cpus_str);
149 printf(_("cpupower set operation was not performed on them\n"));