pseries_energy.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #define MODULE_VERS "1.0"
  24. #define MODULE_NAME "pseries_energy"
  25. /* Driver flags */
  26. static int sysfs_entries;
  27. /* Helper routines */
  28. /*
  29. * Routine to detect firmware support for hcall
  30. * return 1 if H_BEST_ENERGY is supported
  31. * else return 0
  32. */
  33. static int check_for_h_best_energy(void)
  34. {
  35. struct device_node *rtas = NULL;
  36. const char *hypertas, *s;
  37. int length;
  38. int rc = 0;
  39. rtas = of_find_node_by_path("/rtas");
  40. if (!rtas)
  41. return 0;
  42. hypertas = of_get_property(rtas, "ibm,hypertas-functions", &length);
  43. if (!hypertas) {
  44. of_node_put(rtas);
  45. return 0;
  46. }
  47. /* hypertas will have list of strings with hcall names */
  48. for (s = hypertas; s < hypertas + length; s += strlen(s) + 1) {
  49. if (!strncmp("hcall-best-energy-1", s, 19)) {
  50. rc = 1; /* Found the string */
  51. break;
  52. }
  53. }
  54. of_node_put(rtas);
  55. return rc;
  56. }
  57. /* Helper Routines to convert between drc_index to cpu numbers */
  58. static u32 cpu_to_drc_index(int cpu)
  59. {
  60. struct device_node *dn = NULL;
  61. const int *indexes;
  62. int i;
  63. int rc = 1;
  64. u32 ret = 0;
  65. dn = of_find_node_by_path("/cpus");
  66. if (dn == NULL)
  67. goto err;
  68. indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
  69. if (indexes == NULL)
  70. goto err_of_node_put;
  71. /* Convert logical cpu number to core number */
  72. i = cpu_core_index_of_thread(cpu);
  73. /*
  74. * The first element indexes[0] is the number of drc_indexes
  75. * returned in the list. Hence i+1 will get the drc_index
  76. * corresponding to core number i.
  77. */
  78. WARN_ON(i > indexes[0]);
  79. ret = indexes[i + 1];
  80. rc = 0;
  81. err_of_node_put:
  82. of_node_put(dn);
  83. err:
  84. if (rc)
  85. printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
  86. return ret;
  87. }
  88. static int drc_index_to_cpu(u32 drc_index)
  89. {
  90. struct device_node *dn = NULL;
  91. const int *indexes;
  92. int i, cpu = 0;
  93. int rc = 1;
  94. dn = of_find_node_by_path("/cpus");
  95. if (dn == NULL)
  96. goto err;
  97. indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
  98. if (indexes == NULL)
  99. goto err_of_node_put;
  100. /*
  101. * First element in the array is the number of drc_indexes
  102. * returned. Search through the list to find the matching
  103. * drc_index and get the core number
  104. */
  105. for (i = 0; i < indexes[0]; i++) {
  106. if (indexes[i + 1] == drc_index)
  107. break;
  108. }
  109. /* Convert core number to logical cpu number */
  110. cpu = cpu_first_thread_of_core(i);
  111. rc = 0;
  112. err_of_node_put:
  113. of_node_put(dn);
  114. err:
  115. if (rc)
  116. printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
  117. return cpu;
  118. }
  119. /*
  120. * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
  121. * preferred logical cpus to activate or deactivate for optimized
  122. * energy consumption.
  123. */
  124. #define FLAGS_MODE1 0x004E200000080E01
  125. #define FLAGS_MODE2 0x004E200000080401
  126. #define FLAGS_ACTIVATE 0x100
  127. static ssize_t get_best_energy_list(char *page, int activate)
  128. {
  129. int rc, cnt, i, cpu;
  130. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  131. unsigned long flags = 0;
  132. u32 *buf_page;
  133. char *s = page;
  134. buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
  135. if (!buf_page)
  136. return -ENOMEM;
  137. flags = FLAGS_MODE1;
  138. if (activate)
  139. flags |= FLAGS_ACTIVATE;
  140. rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
  141. 0, 0, 0, 0, 0, 0);
  142. if (rc != H_SUCCESS) {
  143. free_page((unsigned long) buf_page);
  144. return -EINVAL;
  145. }
  146. cnt = retbuf[0];
  147. for (i = 0; i < cnt; i++) {
  148. cpu = drc_index_to_cpu(buf_page[2*i+1]);
  149. if ((cpu_online(cpu) && !activate) ||
  150. (!cpu_online(cpu) && activate))
  151. s += sprintf(s, "%d,", cpu);
  152. }
  153. if (s > page) { /* Something to show */
  154. s--; /* Suppress last comma */
  155. s += sprintf(s, "\n");
  156. }
  157. free_page((unsigned long) buf_page);
  158. return s-page;
  159. }
  160. static ssize_t get_best_energy_data(struct device *dev,
  161. char *page, int activate)
  162. {
  163. int rc;
  164. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  165. unsigned long flags = 0;
  166. flags = FLAGS_MODE2;
  167. if (activate)
  168. flags |= FLAGS_ACTIVATE;
  169. rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
  170. cpu_to_drc_index(dev->id),
  171. 0, 0, 0, 0, 0, 0, 0);
  172. if (rc != H_SUCCESS)
  173. return -EINVAL;
  174. return sprintf(page, "%lu\n", retbuf[1] >> 32);
  175. }
  176. /* Wrapper functions */
  177. static ssize_t cpu_activate_hint_list_show(struct device *dev,
  178. struct device_attribute *attr, char *page)
  179. {
  180. return get_best_energy_list(page, 1);
  181. }
  182. static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
  183. struct device_attribute *attr, char *page)
  184. {
  185. return get_best_energy_list(page, 0);
  186. }
  187. static ssize_t percpu_activate_hint_show(struct device *dev,
  188. struct device_attribute *attr, char *page)
  189. {
  190. return get_best_energy_data(dev, page, 1);
  191. }
  192. static ssize_t percpu_deactivate_hint_show(struct device *dev,
  193. struct device_attribute *attr, char *page)
  194. {
  195. return get_best_energy_data(dev, page, 0);
  196. }
  197. /*
  198. * Create sysfs interface:
  199. * /sys/devices/system/cpu/pseries_activate_hint_list
  200. * /sys/devices/system/cpu/pseries_deactivate_hint_list
  201. * Comma separated list of cpus to activate or deactivate
  202. * /sys/devices/system/cpu/cpuN/pseries_activate_hint
  203. * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
  204. * Per-cpu value of the hint
  205. */
  206. struct device_attribute attr_cpu_activate_hint_list =
  207. __ATTR(pseries_activate_hint_list, 0444,
  208. cpu_activate_hint_list_show, NULL);
  209. struct device_attribute attr_cpu_deactivate_hint_list =
  210. __ATTR(pseries_deactivate_hint_list, 0444,
  211. cpu_deactivate_hint_list_show, NULL);
  212. struct device_attribute attr_percpu_activate_hint =
  213. __ATTR(pseries_activate_hint, 0444,
  214. percpu_activate_hint_show, NULL);
  215. struct device_attribute attr_percpu_deactivate_hint =
  216. __ATTR(pseries_deactivate_hint, 0444,
  217. percpu_deactivate_hint_show, NULL);
  218. static int __init pseries_energy_init(void)
  219. {
  220. int cpu, err;
  221. struct device *cpu_dev;
  222. if (!check_for_h_best_energy()) {
  223. printk(KERN_INFO "Hypercall H_BEST_ENERGY not supported\n");
  224. return 0;
  225. }
  226. /* Create the sysfs files */
  227. err = device_create_file(cpu_subsys.dev_root,
  228. &attr_cpu_activate_hint_list);
  229. if (!err)
  230. err = device_create_file(cpu_subsys.dev_root,
  231. &attr_cpu_deactivate_hint_list);
  232. if (err)
  233. return err;
  234. for_each_possible_cpu(cpu) {
  235. cpu_dev = get_cpu_device(cpu);
  236. err = device_create_file(cpu_dev,
  237. &attr_percpu_activate_hint);
  238. if (err)
  239. break;
  240. err = device_create_file(cpu_dev,
  241. &attr_percpu_deactivate_hint);
  242. if (err)
  243. break;
  244. }
  245. if (err)
  246. return err;
  247. sysfs_entries = 1; /* Removed entries on cleanup */
  248. return 0;
  249. }
  250. static void __exit pseries_energy_cleanup(void)
  251. {
  252. int cpu;
  253. struct device *cpu_dev;
  254. if (!sysfs_entries)
  255. return;
  256. /* Remove the sysfs files */
  257. device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list);
  258. device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list);
  259. for_each_possible_cpu(cpu) {
  260. cpu_dev = get_cpu_device(cpu);
  261. sysfs_remove_file(&cpu_dev->kobj,
  262. &attr_percpu_activate_hint.attr);
  263. sysfs_remove_file(&cpu_dev->kobj,
  264. &attr_percpu_deactivate_hint.attr);
  265. }
  266. }
  267. module_init(pseries_energy_init);
  268. module_exit(pseries_energy_cleanup);
  269. MODULE_DESCRIPTION("Driver for pSeries platform energy management");
  270. MODULE_AUTHOR("Vaidyanathan Srinivasan");
  271. MODULE_LICENSE("GPL");