powernv-cpufreq.c 11 KB

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