pseries_energy.c 8.6 KB

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