1 /* 2 * CPPC (Collaborative Processor Performance Control) driver for 3 * interfacing with the CPUfreq layer and governors. See 4 * cppc_acpi.c for CPPC specific methods. 5 * 6 * (C) Copyright 2014, 2015 Linaro Ltd. 7 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org> 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License 11 * as published by the Free Software Foundation; version 2 12 * of the License. 13 */ 14 15 #define pr_fmt(fmt) "CPPC Cpufreq:" fmt 16 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/delay.h> 20 #include <linux/cpu.h> 21 #include <linux/cpufreq.h> 22 #include <linux/dmi.h> 23 #include <linux/time.h> 24 #include <linux/vmalloc.h> 25 26 #include <asm/unaligned.h> 27 28 #include <acpi/cppc_acpi.h> 29 30 /* Minimum struct length needed for the DMI processor entry we want */ 31 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48 32 33 /* Offest in the DMI processor structure for the max frequency */ 34 #define DMI_PROCESSOR_MAX_SPEED 0x14 35 36 /* 37 * These structs contain information parsed from per CPU 38 * ACPI _CPC structures. 39 * e.g. For each CPU the highest, lowest supported 40 * performance capabilities, desired performance level 41 * requested etc. 42 */ 43 static struct cppc_cpudata **all_cpu_data; 44 45 /* Callback function used to retrieve the max frequency from DMI */ 46 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private) 47 { 48 const u8 *dmi_data = (const u8 *)dm; 49 u16 *mhz = (u16 *)private; 50 51 if (dm->type == DMI_ENTRY_PROCESSOR && 52 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) { 53 u16 val = (u16)get_unaligned((const u16 *) 54 (dmi_data + DMI_PROCESSOR_MAX_SPEED)); 55 *mhz = val > *mhz ? val : *mhz; 56 } 57 } 58 59 /* Look up the max frequency in DMI */ 60 static u64 cppc_get_dmi_max_khz(void) 61 { 62 u16 mhz = 0; 63 64 dmi_walk(cppc_find_dmi_mhz, &mhz); 65 66 /* 67 * Real stupid fallback value, just in case there is no 68 * actual value set. 69 */ 70 mhz = mhz ? mhz : 1; 71 72 return (1000 * mhz); 73 } 74 75 /* 76 * If CPPC lowest_freq and nominal_freq registers are exposed then we can 77 * use them to convert perf to freq and vice versa 78 * 79 * If the perf/freq point lies between Nominal and Lowest, we can treat 80 * (Low perf, Low freq) and (Nom Perf, Nom freq) as 2D co-ordinates of a line 81 * and extrapolate the rest 82 * For perf/freq > Nominal, we use the ratio perf:freq at Nominal for conversion 83 */ 84 static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu, 85 unsigned int perf) 86 { 87 static u64 max_khz; 88 struct cppc_perf_caps *caps = &cpu->perf_caps; 89 u64 mul, div; 90 91 if (caps->lowest_freq && caps->nominal_freq) { 92 if (perf >= caps->nominal_perf) { 93 mul = caps->nominal_freq; 94 div = caps->nominal_perf; 95 } else { 96 mul = caps->nominal_freq - caps->lowest_freq; 97 div = caps->nominal_perf - caps->lowest_perf; 98 } 99 } else { 100 if (!max_khz) 101 max_khz = cppc_get_dmi_max_khz(); 102 mul = max_khz; 103 div = cpu->perf_caps.highest_perf; 104 } 105 return (u64)perf * mul / div; 106 } 107 108 static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu, 109 unsigned int freq) 110 { 111 static u64 max_khz; 112 struct cppc_perf_caps *caps = &cpu->perf_caps; 113 u64 mul, div; 114 115 if (caps->lowest_freq && caps->nominal_freq) { 116 if (freq >= caps->nominal_freq) { 117 mul = caps->nominal_perf; 118 div = caps->nominal_freq; 119 } else { 120 mul = caps->lowest_perf; 121 div = caps->lowest_freq; 122 } 123 } else { 124 if (!max_khz) 125 max_khz = cppc_get_dmi_max_khz(); 126 mul = cpu->perf_caps.highest_perf; 127 div = max_khz; 128 } 129 130 return (u64)freq * mul / div; 131 } 132 133 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy, 134 unsigned int target_freq, 135 unsigned int relation) 136 { 137 struct cppc_cpudata *cpu; 138 struct cpufreq_freqs freqs; 139 u32 desired_perf; 140 int ret = 0; 141 142 cpu = all_cpu_data[policy->cpu]; 143 144 desired_perf = cppc_cpufreq_khz_to_perf(cpu, target_freq); 145 /* Return if it is exactly the same perf */ 146 if (desired_perf == cpu->perf_ctrls.desired_perf) 147 return ret; 148 149 cpu->perf_ctrls.desired_perf = desired_perf; 150 freqs.old = policy->cur; 151 freqs.new = target_freq; 152 153 cpufreq_freq_transition_begin(policy, &freqs); 154 ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls); 155 cpufreq_freq_transition_end(policy, &freqs, ret != 0); 156 157 if (ret) 158 pr_debug("Failed to set target on CPU:%d. ret:%d\n", 159 cpu->cpu, ret); 160 161 return ret; 162 } 163 164 static int cppc_verify_policy(struct cpufreq_policy *policy) 165 { 166 cpufreq_verify_within_cpu_limits(policy); 167 return 0; 168 } 169 170 static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy) 171 { 172 int cpu_num = policy->cpu; 173 struct cppc_cpudata *cpu = all_cpu_data[cpu_num]; 174 int ret; 175 176 cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf; 177 178 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls); 179 if (ret) 180 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", 181 cpu->perf_caps.lowest_perf, cpu_num, ret); 182 } 183 184 /* 185 * The PCC subspace describes the rate at which platform can accept commands 186 * on the shared PCC channel (including READs which do not count towards freq 187 * trasition requests), so ideally we need to use the PCC values as a fallback 188 * if we don't have a platform specific transition_delay_us 189 */ 190 #ifdef CONFIG_ARM64 191 #include <asm/cputype.h> 192 193 static unsigned int cppc_cpufreq_get_transition_delay_us(int cpu) 194 { 195 unsigned long implementor = read_cpuid_implementor(); 196 unsigned long part_num = read_cpuid_part_number(); 197 unsigned int delay_us = 0; 198 199 switch (implementor) { 200 case ARM_CPU_IMP_QCOM: 201 switch (part_num) { 202 case QCOM_CPU_PART_FALKOR_V1: 203 case QCOM_CPU_PART_FALKOR: 204 delay_us = 10000; 205 break; 206 default: 207 delay_us = cppc_get_transition_latency(cpu) / NSEC_PER_USEC; 208 break; 209 } 210 break; 211 default: 212 delay_us = cppc_get_transition_latency(cpu) / NSEC_PER_USEC; 213 break; 214 } 215 216 return delay_us; 217 } 218 219 #else 220 221 static unsigned int cppc_cpufreq_get_transition_delay_us(int cpu) 222 { 223 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC; 224 } 225 #endif 226 227 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) 228 { 229 struct cppc_cpudata *cpu; 230 unsigned int cpu_num = policy->cpu; 231 int ret = 0; 232 233 cpu = all_cpu_data[policy->cpu]; 234 235 cpu->cpu = cpu_num; 236 ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps); 237 238 if (ret) { 239 pr_debug("Err reading CPU%d perf capabilities. ret:%d\n", 240 cpu_num, ret); 241 return ret; 242 } 243 244 /* Convert the lowest and nominal freq from MHz to KHz */ 245 cpu->perf_caps.lowest_freq *= 1000; 246 cpu->perf_caps.nominal_freq *= 1000; 247 248 /* 249 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see 250 * Section 8.4.7.1.1.5 of ACPI 6.1 spec) 251 */ 252 policy->min = cppc_cpufreq_perf_to_khz(cpu, cpu->perf_caps.lowest_nonlinear_perf); 253 policy->max = cppc_cpufreq_perf_to_khz(cpu, cpu->perf_caps.highest_perf); 254 255 /* 256 * Set cpuinfo.min_freq to Lowest to make the full range of performance 257 * available if userspace wants to use any perf between lowest & lowest 258 * nonlinear perf 259 */ 260 policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu, cpu->perf_caps.lowest_perf); 261 policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu, cpu->perf_caps.highest_perf); 262 263 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu_num); 264 policy->shared_type = cpu->shared_type; 265 266 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) { 267 int i; 268 269 cpumask_copy(policy->cpus, cpu->shared_cpu_map); 270 271 for_each_cpu(i, policy->cpus) { 272 if (unlikely(i == policy->cpu)) 273 continue; 274 275 memcpy(&all_cpu_data[i]->perf_caps, &cpu->perf_caps, 276 sizeof(cpu->perf_caps)); 277 } 278 } else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) { 279 /* Support only SW_ANY for now. */ 280 pr_debug("Unsupported CPU co-ord type\n"); 281 return -EFAULT; 282 } 283 284 cpu->cur_policy = policy; 285 286 /* Set policy->cur to max now. The governors will adjust later. */ 287 policy->cur = cppc_cpufreq_perf_to_khz(cpu, 288 cpu->perf_caps.highest_perf); 289 cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf; 290 291 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls); 292 if (ret) 293 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", 294 cpu->perf_caps.highest_perf, cpu_num, ret); 295 296 return ret; 297 } 298 299 static struct cpufreq_driver cppc_cpufreq_driver = { 300 .flags = CPUFREQ_CONST_LOOPS, 301 .verify = cppc_verify_policy, 302 .target = cppc_cpufreq_set_target, 303 .init = cppc_cpufreq_cpu_init, 304 .stop_cpu = cppc_cpufreq_stop_cpu, 305 .name = "cppc_cpufreq", 306 }; 307 308 static int __init cppc_cpufreq_init(void) 309 { 310 int i, ret = 0; 311 struct cppc_cpudata *cpu; 312 313 if (acpi_disabled) 314 return -ENODEV; 315 316 all_cpu_data = kcalloc(num_possible_cpus(), sizeof(void *), 317 GFP_KERNEL); 318 if (!all_cpu_data) 319 return -ENOMEM; 320 321 for_each_possible_cpu(i) { 322 all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL); 323 if (!all_cpu_data[i]) 324 goto out; 325 326 cpu = all_cpu_data[i]; 327 if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL)) 328 goto out; 329 } 330 331 ret = acpi_get_psd_map(all_cpu_data); 332 if (ret) { 333 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n"); 334 goto out; 335 } 336 337 ret = cpufreq_register_driver(&cppc_cpufreq_driver); 338 if (ret) 339 goto out; 340 341 return ret; 342 343 out: 344 for_each_possible_cpu(i) { 345 cpu = all_cpu_data[i]; 346 if (!cpu) 347 break; 348 free_cpumask_var(cpu->shared_cpu_map); 349 kfree(cpu); 350 } 351 352 kfree(all_cpu_data); 353 return -ENODEV; 354 } 355 356 static void __exit cppc_cpufreq_exit(void) 357 { 358 struct cppc_cpudata *cpu; 359 int i; 360 361 cpufreq_unregister_driver(&cppc_cpufreq_driver); 362 363 for_each_possible_cpu(i) { 364 cpu = all_cpu_data[i]; 365 free_cpumask_var(cpu->shared_cpu_map); 366 kfree(cpu); 367 } 368 369 kfree(all_cpu_data); 370 } 371 372 module_exit(cppc_cpufreq_exit); 373 MODULE_AUTHOR("Ashwin Chaugule"); 374 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec"); 375 MODULE_LICENSE("GPL"); 376 377 late_initcall(cppc_cpufreq_init); 378 379 static const struct acpi_device_id cppc_acpi_ids[] = { 380 {ACPI_PROCESSOR_DEVICE_HID, }, 381 {} 382 }; 383 384 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids); 385