1 /* 2 * POWERNV cpufreq driver for the IBM POWER processors 3 * 4 * (C) Copyright IBM 2014 5 * 6 * Author: Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2, or (at your option) 11 * any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 */ 19 20 #define pr_fmt(fmt) "powernv-cpufreq: " fmt 21 22 #include <linux/kernel.h> 23 #include <linux/sysfs.h> 24 #include <linux/cpumask.h> 25 #include <linux/module.h> 26 #include <linux/cpufreq.h> 27 #include <linux/smp.h> 28 #include <linux/of.h> 29 #include <linux/reboot.h> 30 31 #include <asm/cputhreads.h> 32 #include <asm/firmware.h> 33 #include <asm/reg.h> 34 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */ 35 36 #define POWERNV_MAX_PSTATES 256 37 38 static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1]; 39 static bool rebooting; 40 41 /* 42 * Note: The set of pstates consists of contiguous integers, the 43 * smallest of which is indicated by powernv_pstate_info.min, the 44 * largest of which is indicated by powernv_pstate_info.max. 45 * 46 * The nominal pstate is the highest non-turbo pstate in this 47 * platform. This is indicated by powernv_pstate_info.nominal. 48 */ 49 static struct powernv_pstate_info { 50 int min; 51 int max; 52 int nominal; 53 int nr_pstates; 54 } powernv_pstate_info; 55 56 /* 57 * Initialize the freq table based on data obtained 58 * from the firmware passed via device-tree 59 */ 60 static int init_powernv_pstates(void) 61 { 62 struct device_node *power_mgt; 63 int i, pstate_min, pstate_max, pstate_nominal, nr_pstates = 0; 64 const __be32 *pstate_ids, *pstate_freqs; 65 u32 len_ids, len_freqs; 66 67 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt"); 68 if (!power_mgt) { 69 pr_warn("power-mgt node not found\n"); 70 return -ENODEV; 71 } 72 73 if (of_property_read_u32(power_mgt, "ibm,pstate-min", &pstate_min)) { 74 pr_warn("ibm,pstate-min node not found\n"); 75 return -ENODEV; 76 } 77 78 if (of_property_read_u32(power_mgt, "ibm,pstate-max", &pstate_max)) { 79 pr_warn("ibm,pstate-max node not found\n"); 80 return -ENODEV; 81 } 82 83 if (of_property_read_u32(power_mgt, "ibm,pstate-nominal", 84 &pstate_nominal)) { 85 pr_warn("ibm,pstate-nominal not found\n"); 86 return -ENODEV; 87 } 88 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min, 89 pstate_nominal, pstate_max); 90 91 pstate_ids = of_get_property(power_mgt, "ibm,pstate-ids", &len_ids); 92 if (!pstate_ids) { 93 pr_warn("ibm,pstate-ids not found\n"); 94 return -ENODEV; 95 } 96 97 pstate_freqs = of_get_property(power_mgt, "ibm,pstate-frequencies-mhz", 98 &len_freqs); 99 if (!pstate_freqs) { 100 pr_warn("ibm,pstate-frequencies-mhz not found\n"); 101 return -ENODEV; 102 } 103 104 if (len_ids != len_freqs) { 105 pr_warn("Entries in ibm,pstate-ids and " 106 "ibm,pstate-frequencies-mhz does not match\n"); 107 } 108 109 nr_pstates = min(len_ids, len_freqs) / sizeof(u32); 110 if (!nr_pstates) { 111 pr_warn("No PStates found\n"); 112 return -ENODEV; 113 } 114 115 pr_debug("NR PStates %d\n", nr_pstates); 116 for (i = 0; i < nr_pstates; i++) { 117 u32 id = be32_to_cpu(pstate_ids[i]); 118 u32 freq = be32_to_cpu(pstate_freqs[i]); 119 120 pr_debug("PState id %d freq %d MHz\n", id, freq); 121 powernv_freqs[i].frequency = freq * 1000; /* kHz */ 122 powernv_freqs[i].driver_data = id; 123 } 124 /* End of list marker entry */ 125 powernv_freqs[i].frequency = CPUFREQ_TABLE_END; 126 127 powernv_pstate_info.min = pstate_min; 128 powernv_pstate_info.max = pstate_max; 129 powernv_pstate_info.nominal = pstate_nominal; 130 powernv_pstate_info.nr_pstates = nr_pstates; 131 132 return 0; 133 } 134 135 /* Returns the CPU frequency corresponding to the pstate_id. */ 136 static unsigned int pstate_id_to_freq(int pstate_id) 137 { 138 int i; 139 140 i = powernv_pstate_info.max - pstate_id; 141 if (i >= powernv_pstate_info.nr_pstates || i < 0) { 142 pr_warn("PState id %d outside of PState table, " 143 "reporting nominal id %d instead\n", 144 pstate_id, powernv_pstate_info.nominal); 145 i = powernv_pstate_info.max - powernv_pstate_info.nominal; 146 } 147 148 return powernv_freqs[i].frequency; 149 } 150 151 /* 152 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by 153 * the firmware 154 */ 155 static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy, 156 char *buf) 157 { 158 return sprintf(buf, "%u\n", 159 pstate_id_to_freq(powernv_pstate_info.nominal)); 160 } 161 162 struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq = 163 __ATTR_RO(cpuinfo_nominal_freq); 164 165 static struct freq_attr *powernv_cpu_freq_attr[] = { 166 &cpufreq_freq_attr_scaling_available_freqs, 167 &cpufreq_freq_attr_cpuinfo_nominal_freq, 168 NULL, 169 }; 170 171 /* Helper routines */ 172 173 /* Access helpers to power mgt SPR */ 174 175 static inline unsigned long get_pmspr(unsigned long sprn) 176 { 177 switch (sprn) { 178 case SPRN_PMCR: 179 return mfspr(SPRN_PMCR); 180 181 case SPRN_PMICR: 182 return mfspr(SPRN_PMICR); 183 184 case SPRN_PMSR: 185 return mfspr(SPRN_PMSR); 186 } 187 BUG(); 188 } 189 190 static inline void set_pmspr(unsigned long sprn, unsigned long val) 191 { 192 switch (sprn) { 193 case SPRN_PMCR: 194 mtspr(SPRN_PMCR, val); 195 return; 196 197 case SPRN_PMICR: 198 mtspr(SPRN_PMICR, val); 199 return; 200 } 201 BUG(); 202 } 203 204 /* 205 * Use objects of this type to query/update 206 * pstates on a remote CPU via smp_call_function. 207 */ 208 struct powernv_smp_call_data { 209 unsigned int freq; 210 int pstate_id; 211 }; 212 213 /* 214 * powernv_read_cpu_freq: Reads the current frequency on this CPU. 215 * 216 * Called via smp_call_function. 217 * 218 * Note: The caller of the smp_call_function should pass an argument of 219 * the type 'struct powernv_smp_call_data *' along with this function. 220 * 221 * The current frequency on this CPU will be returned via 222 * ((struct powernv_smp_call_data *)arg)->freq; 223 */ 224 static void powernv_read_cpu_freq(void *arg) 225 { 226 unsigned long pmspr_val; 227 s8 local_pstate_id; 228 struct powernv_smp_call_data *freq_data = arg; 229 230 pmspr_val = get_pmspr(SPRN_PMSR); 231 232 /* 233 * The local pstate id corresponds bits 48..55 in the PMSR. 234 * Note: Watch out for the sign! 235 */ 236 local_pstate_id = (pmspr_val >> 48) & 0xFF; 237 freq_data->pstate_id = local_pstate_id; 238 freq_data->freq = pstate_id_to_freq(freq_data->pstate_id); 239 240 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n", 241 raw_smp_processor_id(), pmspr_val, freq_data->pstate_id, 242 freq_data->freq); 243 } 244 245 /* 246 * powernv_cpufreq_get: Returns the CPU frequency as reported by the 247 * firmware for CPU 'cpu'. This value is reported through the sysfs 248 * file cpuinfo_cur_freq. 249 */ 250 static unsigned int powernv_cpufreq_get(unsigned int cpu) 251 { 252 struct powernv_smp_call_data freq_data; 253 254 smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq, 255 &freq_data, 1); 256 257 return freq_data.freq; 258 } 259 260 /* 261 * set_pstate: Sets the pstate on this CPU. 262 * 263 * This is called via an smp_call_function. 264 * 265 * The caller must ensure that freq_data is of the type 266 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set 267 * on this CPU should be present in freq_data->pstate_id. 268 */ 269 static void set_pstate(void *freq_data) 270 { 271 unsigned long val; 272 unsigned long pstate_ul = 273 ((struct powernv_smp_call_data *) freq_data)->pstate_id; 274 275 val = get_pmspr(SPRN_PMCR); 276 val = val & 0x0000FFFFFFFFFFFFULL; 277 278 pstate_ul = pstate_ul & 0xFF; 279 280 /* Set both global(bits 56..63) and local(bits 48..55) PStates */ 281 val = val | (pstate_ul << 56) | (pstate_ul << 48); 282 283 pr_debug("Setting cpu %d pmcr to %016lX\n", 284 raw_smp_processor_id(), val); 285 set_pmspr(SPRN_PMCR, val); 286 } 287 288 /* 289 * get_nominal_index: Returns the index corresponding to the nominal 290 * pstate in the cpufreq table 291 */ 292 static inline unsigned int get_nominal_index(void) 293 { 294 return powernv_pstate_info.max - powernv_pstate_info.nominal; 295 } 296 297 /* 298 * powernv_cpufreq_target_index: Sets the frequency corresponding to 299 * the cpufreq table entry indexed by new_index on the cpus in the 300 * mask policy->cpus 301 */ 302 static int powernv_cpufreq_target_index(struct cpufreq_policy *policy, 303 unsigned int new_index) 304 { 305 struct powernv_smp_call_data freq_data; 306 307 if (unlikely(rebooting) && new_index != get_nominal_index()) 308 return 0; 309 310 freq_data.pstate_id = powernv_freqs[new_index].driver_data; 311 312 /* 313 * Use smp_call_function to send IPI and execute the 314 * mtspr on target CPU. We could do that without IPI 315 * if current CPU is within policy->cpus (core) 316 */ 317 smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1); 318 319 return 0; 320 } 321 322 static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy) 323 { 324 int base, i; 325 326 base = cpu_first_thread_sibling(policy->cpu); 327 328 for (i = 0; i < threads_per_core; i++) 329 cpumask_set_cpu(base + i, policy->cpus); 330 331 return cpufreq_table_validate_and_show(policy, powernv_freqs); 332 } 333 334 static int powernv_cpufreq_reboot_notifier(struct notifier_block *nb, 335 unsigned long action, void *unused) 336 { 337 int cpu; 338 struct cpufreq_policy cpu_policy; 339 340 rebooting = true; 341 for_each_online_cpu(cpu) { 342 cpufreq_get_policy(&cpu_policy, cpu); 343 powernv_cpufreq_target_index(&cpu_policy, get_nominal_index()); 344 } 345 346 return NOTIFY_DONE; 347 } 348 349 static struct notifier_block powernv_cpufreq_reboot_nb = { 350 .notifier_call = powernv_cpufreq_reboot_notifier, 351 }; 352 353 static void powernv_cpufreq_stop_cpu(struct cpufreq_policy *policy) 354 { 355 struct powernv_smp_call_data freq_data; 356 357 freq_data.pstate_id = powernv_pstate_info.min; 358 smp_call_function_single(policy->cpu, set_pstate, &freq_data, 1); 359 } 360 361 static struct cpufreq_driver powernv_cpufreq_driver = { 362 .name = "powernv-cpufreq", 363 .flags = CPUFREQ_CONST_LOOPS, 364 .init = powernv_cpufreq_cpu_init, 365 .verify = cpufreq_generic_frequency_table_verify, 366 .target_index = powernv_cpufreq_target_index, 367 .get = powernv_cpufreq_get, 368 .stop_cpu = powernv_cpufreq_stop_cpu, 369 .attr = powernv_cpu_freq_attr, 370 }; 371 372 static int __init powernv_cpufreq_init(void) 373 { 374 int rc = 0; 375 376 /* Don't probe on pseries (guest) platforms */ 377 if (!firmware_has_feature(FW_FEATURE_OPALv3)) 378 return -ENODEV; 379 380 /* Discover pstates from device tree and init */ 381 rc = init_powernv_pstates(); 382 if (rc) { 383 pr_info("powernv-cpufreq disabled. System does not support PState control\n"); 384 return rc; 385 } 386 387 register_reboot_notifier(&powernv_cpufreq_reboot_nb); 388 return cpufreq_register_driver(&powernv_cpufreq_driver); 389 } 390 module_init(powernv_cpufreq_init); 391 392 static void __exit powernv_cpufreq_exit(void) 393 { 394 unregister_reboot_notifier(&powernv_cpufreq_reboot_nb); 395 cpufreq_unregister_driver(&powernv_cpufreq_driver); 396 } 397 module_exit(powernv_cpufreq_exit); 398 399 MODULE_LICENSE("GPL"); 400 MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>"); 401