/arch/powerpc/platforms/pseries/pseries_energy.c

http://github.com/mirrors/linux · C · 362 lines · 256 code · 65 blank · 41 comment · 44 complexity · a029574f17e1d586a72fcc8c28062abb MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * POWER platform energy management driver
  4. * Copyright (C) 2010 IBM Corporation
  5. *
  6. * This pseries platform device driver provides access to
  7. * platform energy management capabilities.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/device.h>
  15. #include <linux/cpu.h>
  16. #include <linux/of.h>
  17. #include <asm/cputhreads.h>
  18. #include <asm/page.h>
  19. #include <asm/hvcall.h>
  20. #include <asm/firmware.h>
  21. #include <asm/prom.h>
  22. #define MODULE_VERS "1.0"
  23. #define MODULE_NAME "pseries_energy"
  24. /* Driver flags */
  25. static int sysfs_entries;
  26. /* Helper routines */
  27. /* Helper Routines to convert between drc_index to cpu numbers */
  28. static u32 cpu_to_drc_index(int cpu)
  29. {
  30. struct device_node *dn = NULL;
  31. struct property *info;
  32. int thread_index;
  33. int rc = 1;
  34. u32 ret = 0;
  35. dn = of_find_node_by_path("/cpus");
  36. if (dn == NULL)
  37. goto err;
  38. /* Convert logical cpu number to core number */
  39. thread_index = cpu_core_index_of_thread(cpu);
  40. info = of_find_property(dn, "ibm,drc-info", NULL);
  41. if (info) {
  42. struct of_drc_info drc;
  43. int j;
  44. u32 num_set_entries;
  45. const __be32 *value;
  46. value = of_prop_next_u32(info, NULL, &num_set_entries);
  47. if (!value)
  48. goto err_of_node_put;
  49. else
  50. value++;
  51. for (j = 0; j < num_set_entries; j++) {
  52. of_read_drc_info_cell(&info, &value, &drc);
  53. if (strncmp(drc.drc_type, "CPU", 3))
  54. goto err;
  55. if (thread_index < drc.last_drc_index)
  56. break;
  57. }
  58. ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
  59. } else {
  60. u32 nr_drc_indexes, thread_drc_index;
  61. /*
  62. * The first element of ibm,drc-indexes array is the
  63. * number of drc_indexes returned in the list. Hence
  64. * thread_index+1 will get the drc_index corresponding
  65. * to core number thread_index.
  66. */
  67. rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
  68. 0, &nr_drc_indexes);
  69. if (rc)
  70. goto err_of_node_put;
  71. WARN_ON_ONCE(thread_index > nr_drc_indexes);
  72. rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
  73. thread_index + 1,
  74. &thread_drc_index);
  75. if (rc)
  76. goto err_of_node_put;
  77. ret = thread_drc_index;
  78. }
  79. rc = 0;
  80. err_of_node_put:
  81. of_node_put(dn);
  82. err:
  83. if (rc)
  84. printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
  85. return ret;
  86. }
  87. static int drc_index_to_cpu(u32 drc_index)
  88. {
  89. struct device_node *dn = NULL;
  90. struct property *info;
  91. const int *indexes;
  92. int thread_index = 0, cpu = 0;
  93. int rc = 1;
  94. dn = of_find_node_by_path("/cpus");
  95. if (dn == NULL)
  96. goto err;
  97. info = of_find_property(dn, "ibm,drc-info", NULL);
  98. if (info) {
  99. struct of_drc_info drc;
  100. int j;
  101. u32 num_set_entries;
  102. const __be32 *value;
  103. value = of_prop_next_u32(info, NULL, &num_set_entries);
  104. if (!value)
  105. goto err_of_node_put;
  106. else
  107. value++;
  108. for (j = 0; j < num_set_entries; j++) {
  109. of_read_drc_info_cell(&info, &value, &drc);
  110. if (strncmp(drc.drc_type, "CPU", 3))
  111. goto err;
  112. if (drc_index > drc.last_drc_index) {
  113. cpu += drc.num_sequential_elems;
  114. continue;
  115. }
  116. cpu += ((drc_index - drc.drc_index_start) /
  117. drc.sequential_inc);
  118. thread_index = cpu_first_thread_of_core(cpu);
  119. rc = 0;
  120. break;
  121. }
  122. } else {
  123. unsigned long int i;
  124. indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
  125. if (indexes == NULL)
  126. goto err_of_node_put;
  127. /*
  128. * First element in the array is the number of drc_indexes
  129. * returned. Search through the list to find the matching
  130. * drc_index and get the core number
  131. */
  132. for (i = 0; i < indexes[0]; i++) {
  133. if (indexes[i + 1] == drc_index)
  134. break;
  135. }
  136. /* Convert core number to logical cpu number */
  137. thread_index = cpu_first_thread_of_core(i);
  138. rc = 0;
  139. }
  140. err_of_node_put:
  141. of_node_put(dn);
  142. err:
  143. if (rc)
  144. printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
  145. return thread_index;
  146. }
  147. /*
  148. * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
  149. * preferred logical cpus to activate or deactivate for optimized
  150. * energy consumption.
  151. */
  152. #define FLAGS_MODE1 0x004E200000080E01UL
  153. #define FLAGS_MODE2 0x004E200000080401UL
  154. #define FLAGS_ACTIVATE 0x100
  155. static ssize_t get_best_energy_list(char *page, int activate)
  156. {
  157. int rc, cnt, i, cpu;
  158. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  159. unsigned long flags = 0;
  160. u32 *buf_page;
  161. char *s = page;
  162. buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
  163. if (!buf_page)
  164. return -ENOMEM;
  165. flags = FLAGS_MODE1;
  166. if (activate)
  167. flags |= FLAGS_ACTIVATE;
  168. rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
  169. 0, 0, 0, 0, 0, 0);
  170. if (rc != H_SUCCESS) {
  171. free_page((unsigned long) buf_page);
  172. return -EINVAL;
  173. }
  174. cnt = retbuf[0];
  175. for (i = 0; i < cnt; i++) {
  176. cpu = drc_index_to_cpu(buf_page[2*i+1]);
  177. if ((cpu_online(cpu) && !activate) ||
  178. (!cpu_online(cpu) && activate))
  179. s += sprintf(s, "%d,", cpu);
  180. }
  181. if (s > page) { /* Something to show */
  182. s--; /* Suppress last comma */
  183. s += sprintf(s, "\n");
  184. }
  185. free_page((unsigned long) buf_page);
  186. return s-page;
  187. }
  188. static ssize_t get_best_energy_data(struct device *dev,
  189. char *page, int activate)
  190. {
  191. int rc;
  192. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  193. unsigned long flags = 0;
  194. flags = FLAGS_MODE2;
  195. if (activate)
  196. flags |= FLAGS_ACTIVATE;
  197. rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
  198. cpu_to_drc_index(dev->id),
  199. 0, 0, 0, 0, 0, 0, 0);
  200. if (rc != H_SUCCESS)
  201. return -EINVAL;
  202. return sprintf(page, "%lu\n", retbuf[1] >> 32);
  203. }
  204. /* Wrapper functions */
  205. static ssize_t cpu_activate_hint_list_show(struct device *dev,
  206. struct device_attribute *attr, char *page)
  207. {
  208. return get_best_energy_list(page, 1);
  209. }
  210. static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
  211. struct device_attribute *attr, char *page)
  212. {
  213. return get_best_energy_list(page, 0);
  214. }
  215. static ssize_t percpu_activate_hint_show(struct device *dev,
  216. struct device_attribute *attr, char *page)
  217. {
  218. return get_best_energy_data(dev, page, 1);
  219. }
  220. static ssize_t percpu_deactivate_hint_show(struct device *dev,
  221. struct device_attribute *attr, char *page)
  222. {
  223. return get_best_energy_data(dev, page, 0);
  224. }
  225. /*
  226. * Create sysfs interface:
  227. * /sys/devices/system/cpu/pseries_activate_hint_list
  228. * /sys/devices/system/cpu/pseries_deactivate_hint_list
  229. * Comma separated list of cpus to activate or deactivate
  230. * /sys/devices/system/cpu/cpuN/pseries_activate_hint
  231. * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
  232. * Per-cpu value of the hint
  233. */
  234. static struct device_attribute attr_cpu_activate_hint_list =
  235. __ATTR(pseries_activate_hint_list, 0444,
  236. cpu_activate_hint_list_show, NULL);
  237. static struct device_attribute attr_cpu_deactivate_hint_list =
  238. __ATTR(pseries_deactivate_hint_list, 0444,
  239. cpu_deactivate_hint_list_show, NULL);
  240. static struct device_attribute attr_percpu_activate_hint =
  241. __ATTR(pseries_activate_hint, 0444,
  242. percpu_activate_hint_show, NULL);
  243. static struct device_attribute attr_percpu_deactivate_hint =
  244. __ATTR(pseries_deactivate_hint, 0444,
  245. percpu_deactivate_hint_show, NULL);
  246. static int __init pseries_energy_init(void)
  247. {
  248. int cpu, err;
  249. struct device *cpu_dev;
  250. if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY))
  251. return 0; /* H_BEST_ENERGY hcall not supported */
  252. /* Create the sysfs files */
  253. err = device_create_file(cpu_subsys.dev_root,
  254. &attr_cpu_activate_hint_list);
  255. if (!err)
  256. err = device_create_file(cpu_subsys.dev_root,
  257. &attr_cpu_deactivate_hint_list);
  258. if (err)
  259. return err;
  260. for_each_possible_cpu(cpu) {
  261. cpu_dev = get_cpu_device(cpu);
  262. err = device_create_file(cpu_dev,
  263. &attr_percpu_activate_hint);
  264. if (err)
  265. break;
  266. err = device_create_file(cpu_dev,
  267. &attr_percpu_deactivate_hint);
  268. if (err)
  269. break;
  270. }
  271. if (err)
  272. return err;
  273. sysfs_entries = 1; /* Removed entries on cleanup */
  274. return 0;
  275. }
  276. static void __exit pseries_energy_cleanup(void)
  277. {
  278. int cpu;
  279. struct device *cpu_dev;
  280. if (!sysfs_entries)
  281. return;
  282. /* Remove the sysfs files */
  283. device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list);
  284. device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list);
  285. for_each_possible_cpu(cpu) {
  286. cpu_dev = get_cpu_device(cpu);
  287. sysfs_remove_file(&cpu_dev->kobj,
  288. &attr_percpu_activate_hint.attr);
  289. sysfs_remove_file(&cpu_dev->kobj,
  290. &attr_percpu_deactivate_hint.attr);
  291. }
  292. }
  293. module_init(pseries_energy_init);
  294. module_exit(pseries_energy_cleanup);
  295. MODULE_DESCRIPTION("Driver for pSeries platform energy management");
  296. MODULE_AUTHOR("Vaidyanathan Srinivasan");
  297. MODULE_LICENSE("GPL");