pseries_energy.c 8.4 KB

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