1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <stdio.h> 4 #include <errno.h> 5 #include <stdlib.h> 6 7 #include "helpers/helpers.h" 8 #include "helpers/sysfs.h" 9 10 #if defined(__i386__) || defined(__x86_64__) 11 12 #include "cpupower_intern.h" 13 14 #define MSR_AMD_HWCR 0xc0010015 15 16 int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active, 17 int *states) 18 { 19 struct cpupower_cpu_info cpu_info; 20 int ret; 21 unsigned long long val; 22 23 *support = *active = *states = 0; 24 25 ret = get_cpu_info(&cpu_info); 26 if (ret) 27 return ret; 28 29 if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_CBP) { 30 *support = 1; 31 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. 35 */ 36 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; 41 } 42 } else { 43 ret = amd_pci_get_num_boost_states(active, states); 44 if (ret) 45 return ret; 46 } 47 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_INTEL_IDA) 48 *support = *active = 1; 49 return 0; 50 } 51 52 int cpupower_intel_get_perf_bias(unsigned int cpu) 53 { 54 char linebuf[MAX_LINE_LEN]; 55 char path[SYSFS_PATH_MAX]; 56 unsigned long val; 57 char *endp; 58 59 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) 60 return -1; 61 62 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu); 63 64 if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0) 65 return -1; 66 67 val = strtol(linebuf, &endp, 0); 68 if (endp == linebuf || errno == ERANGE) 69 return -1; 70 71 return val; 72 } 73 74 int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val) 75 { 76 char path[SYSFS_PATH_MAX]; 77 char linebuf[3] = {}; 78 79 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) 80 return -1; 81 82 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu); 83 snprintf(linebuf, sizeof(linebuf), "%d", val); 84 85 if (cpupower_write_sysfs(path, linebuf, 3) <= 0) 86 return -1; 87 88 return 0; 89 } 90 91 #endif /* #if defined(__i386__) || defined(__x86_64__) */ 92 93 /* get_cpustate 94 * 95 * Gather the information of all online CPUs into bitmask struct 96 */ 97 void get_cpustate(void) 98 { 99 unsigned int cpu = 0; 100 101 bitmask_clearall(online_cpus); 102 bitmask_clearall(offline_cpus); 103 104 for (cpu = bitmask_first(cpus_chosen); 105 cpu <= bitmask_last(cpus_chosen); cpu++) { 106 107 if (cpupower_is_cpu_online(cpu) == 1) 108 bitmask_setbit(online_cpus, cpu); 109 else 110 bitmask_setbit(offline_cpus, cpu); 111 112 continue; 113 } 114 } 115 116 /* print_online_cpus 117 * 118 * Print the CPU numbers of all CPUs that are online currently 119 */ 120 void print_online_cpus(void) 121 { 122 int str_len = 0; 123 char *online_cpus_str = NULL; 124 125 str_len = online_cpus->size * 5; 126 online_cpus_str = (void *)malloc(sizeof(char) * str_len); 127 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); 131 } 132 } 133 134 /* print_offline_cpus 135 * 136 * Print the CPU numbers of all CPUs that are offline currently 137 */ 138 void print_offline_cpus(void) 139 { 140 int str_len = 0; 141 char *offline_cpus_str = NULL; 142 143 str_len = offline_cpus->size * 5; 144 offline_cpus_str = (void *)malloc(sizeof(char) * str_len); 145 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")); 150 } 151 } 152