powernv-cpufreq.c 10 KB

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