powernv-cpufreq.c 8.7 KB

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