pseries_energy.c 6.9 KB

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