idle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * PowerNV cpuidle code
  3. *
  4. * Copyright 2015 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/of.h>
  15. #include <linux/device.h>
  16. #include <linux/cpu.h>
  17. #include <asm/firmware.h>
  18. #include <asm/machdep.h>
  19. #include <asm/opal.h>
  20. #include <asm/cputhreads.h>
  21. #include <asm/cpuidle.h>
  22. #include <asm/code-patching.h>
  23. #include <asm/smp.h>
  24. #include "powernv.h"
  25. #include "subcore.h"
  26. /* Power ISA 3.0 allows for stop states 0x0 - 0xF */
  27. #define MAX_STOP_STATE 0xF
  28. static u32 supported_cpuidle_states;
  29. static int pnv_save_sprs_for_deep_states(void)
  30. {
  31. int cpu;
  32. int rc;
  33. /*
  34. * hid0, hid1, hid4, hid5, hmeer and lpcr values are symmetric across
  35. * all cpus at boot. Get these reg values of current cpu and use the
  36. * same across all cpus.
  37. */
  38. uint64_t lpcr_val = mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1;
  39. uint64_t hid0_val = mfspr(SPRN_HID0);
  40. uint64_t hid1_val = mfspr(SPRN_HID1);
  41. uint64_t hid4_val = mfspr(SPRN_HID4);
  42. uint64_t hid5_val = mfspr(SPRN_HID5);
  43. uint64_t hmeer_val = mfspr(SPRN_HMEER);
  44. for_each_possible_cpu(cpu) {
  45. uint64_t pir = get_hard_smp_processor_id(cpu);
  46. uint64_t hsprg0_val = (uint64_t)&paca[cpu];
  47. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  48. /*
  49. * HSPRG0 is used to store the cpu's pointer to paca.
  50. * Hence last 3 bits are guaranteed to be 0. Program
  51. * slw to restore HSPRG0 with 63rd bit set, so that
  52. * when a thread wakes up at 0x100 we can use this bit
  53. * to distinguish between fastsleep and deep winkle.
  54. * This is not necessary with stop/psscr since PLS
  55. * field of psscr indicates which state we are waking
  56. * up from.
  57. */
  58. hsprg0_val |= 1;
  59. }
  60. rc = opal_slw_set_reg(pir, SPRN_HSPRG0, hsprg0_val);
  61. if (rc != 0)
  62. return rc;
  63. rc = opal_slw_set_reg(pir, SPRN_LPCR, lpcr_val);
  64. if (rc != 0)
  65. return rc;
  66. /* HIDs are per core registers */
  67. if (cpu_thread_in_core(cpu) == 0) {
  68. rc = opal_slw_set_reg(pir, SPRN_HMEER, hmeer_val);
  69. if (rc != 0)
  70. return rc;
  71. rc = opal_slw_set_reg(pir, SPRN_HID0, hid0_val);
  72. if (rc != 0)
  73. return rc;
  74. rc = opal_slw_set_reg(pir, SPRN_HID1, hid1_val);
  75. if (rc != 0)
  76. return rc;
  77. rc = opal_slw_set_reg(pir, SPRN_HID4, hid4_val);
  78. if (rc != 0)
  79. return rc;
  80. rc = opal_slw_set_reg(pir, SPRN_HID5, hid5_val);
  81. if (rc != 0)
  82. return rc;
  83. }
  84. }
  85. return 0;
  86. }
  87. static void pnv_alloc_idle_core_states(void)
  88. {
  89. int i, j;
  90. int nr_cores = cpu_nr_cores();
  91. u32 *core_idle_state;
  92. /*
  93. * core_idle_state - First 8 bits track the idle state of each thread
  94. * of the core. The 8th bit is the lock bit. Initially all thread bits
  95. * are set. They are cleared when the thread enters deep idle state
  96. * like sleep and winkle. Initially the lock bit is cleared.
  97. * The lock bit has 2 purposes
  98. * a. While the first thread is restoring core state, it prevents
  99. * other threads in the core from switching to process context.
  100. * b. While the last thread in the core is saving the core state, it
  101. * prevents a different thread from waking up.
  102. */
  103. for (i = 0; i < nr_cores; i++) {
  104. int first_cpu = i * threads_per_core;
  105. int node = cpu_to_node(first_cpu);
  106. core_idle_state = kmalloc_node(sizeof(u32), GFP_KERNEL, node);
  107. *core_idle_state = PNV_CORE_IDLE_THREAD_BITS;
  108. for (j = 0; j < threads_per_core; j++) {
  109. int cpu = first_cpu + j;
  110. paca[cpu].core_idle_state_ptr = core_idle_state;
  111. paca[cpu].thread_idle_state = PNV_THREAD_RUNNING;
  112. paca[cpu].thread_mask = 1 << j;
  113. }
  114. }
  115. update_subcore_sibling_mask();
  116. if (supported_cpuidle_states & OPAL_PM_LOSE_FULL_CONTEXT)
  117. pnv_save_sprs_for_deep_states();
  118. }
  119. u32 pnv_get_supported_cpuidle_states(void)
  120. {
  121. return supported_cpuidle_states;
  122. }
  123. EXPORT_SYMBOL_GPL(pnv_get_supported_cpuidle_states);
  124. static void pnv_fastsleep_workaround_apply(void *info)
  125. {
  126. int rc;
  127. int *err = info;
  128. rc = opal_config_cpu_idle_state(OPAL_CONFIG_IDLE_FASTSLEEP,
  129. OPAL_CONFIG_IDLE_APPLY);
  130. if (rc)
  131. *err = 1;
  132. }
  133. /*
  134. * Used to store fastsleep workaround state
  135. * 0 - Workaround applied/undone at fastsleep entry/exit path (Default)
  136. * 1 - Workaround applied once, never undone.
  137. */
  138. static u8 fastsleep_workaround_applyonce;
  139. static ssize_t show_fastsleep_workaround_applyonce(struct device *dev,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. return sprintf(buf, "%u\n", fastsleep_workaround_applyonce);
  143. }
  144. static ssize_t store_fastsleep_workaround_applyonce(struct device *dev,
  145. struct device_attribute *attr, const char *buf,
  146. size_t count)
  147. {
  148. cpumask_t primary_thread_mask;
  149. int err;
  150. u8 val;
  151. if (kstrtou8(buf, 0, &val) || val != 1)
  152. return -EINVAL;
  153. if (fastsleep_workaround_applyonce == 1)
  154. return count;
  155. /*
  156. * fastsleep_workaround_applyonce = 1 implies
  157. * fastsleep workaround needs to be left in 'applied' state on all
  158. * the cores. Do this by-
  159. * 1. Patching out the call to 'undo' workaround in fastsleep exit path
  160. * 2. Sending ipi to all the cores which have at least one online thread
  161. * 3. Patching out the call to 'apply' workaround in fastsleep entry
  162. * path
  163. * There is no need to send ipi to cores which have all threads
  164. * offlined, as last thread of the core entering fastsleep or deeper
  165. * state would have applied workaround.
  166. */
  167. err = patch_instruction(
  168. (unsigned int *)pnv_fastsleep_workaround_at_exit,
  169. PPC_INST_NOP);
  170. if (err) {
  171. pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_exit");
  172. goto fail;
  173. }
  174. get_online_cpus();
  175. primary_thread_mask = cpu_online_cores_map();
  176. on_each_cpu_mask(&primary_thread_mask,
  177. pnv_fastsleep_workaround_apply,
  178. &err, 1);
  179. put_online_cpus();
  180. if (err) {
  181. pr_err("fastsleep_workaround_applyonce change failed while running pnv_fastsleep_workaround_apply");
  182. goto fail;
  183. }
  184. err = patch_instruction(
  185. (unsigned int *)pnv_fastsleep_workaround_at_entry,
  186. PPC_INST_NOP);
  187. if (err) {
  188. pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_entry");
  189. goto fail;
  190. }
  191. fastsleep_workaround_applyonce = 1;
  192. return count;
  193. fail:
  194. return -EIO;
  195. }
  196. static DEVICE_ATTR(fastsleep_workaround_applyonce, 0600,
  197. show_fastsleep_workaround_applyonce,
  198. store_fastsleep_workaround_applyonce);
  199. /*
  200. * Used for ppc_md.power_save which needs a function with no parameters
  201. */
  202. static void power9_idle(void)
  203. {
  204. /* Requesting stop state 0 */
  205. power9_idle_stop(0);
  206. }
  207. /*
  208. * First deep stop state. Used to figure out when to save/restore
  209. * hypervisor context.
  210. */
  211. u64 pnv_first_deep_stop_state = MAX_STOP_STATE;
  212. /*
  213. * Deepest stop idle state. Used when a cpu is offlined
  214. */
  215. u64 pnv_deepest_stop_state;
  216. /*
  217. * Power ISA 3.0 idle initialization.
  218. *
  219. * POWER ISA 3.0 defines a new SPR Processor stop Status and Control
  220. * Register (PSSCR) to control idle behavior.
  221. *
  222. * PSSCR layout:
  223. * ----------------------------------------------------------
  224. * | PLS | /// | SD | ESL | EC | PSLL | /// | TR | MTL | RL |
  225. * ----------------------------------------------------------
  226. * 0 4 41 42 43 44 48 54 56 60
  227. *
  228. * PSSCR key fields:
  229. * Bits 0:3 - Power-Saving Level Status (PLS). This field indicates the
  230. * lowest power-saving state the thread entered since stop instruction was
  231. * last executed.
  232. *
  233. * Bit 41 - Status Disable(SD)
  234. * 0 - Shows PLS entries
  235. * 1 - PLS entries are all 0
  236. *
  237. * Bit 42 - Enable State Loss
  238. * 0 - No state is lost irrespective of other fields
  239. * 1 - Allows state loss
  240. *
  241. * Bit 43 - Exit Criterion
  242. * 0 - Exit from power-save mode on any interrupt
  243. * 1 - Exit from power-save mode controlled by LPCR's PECE bits
  244. *
  245. * Bits 44:47 - Power-Saving Level Limit
  246. * This limits the power-saving level that can be entered into.
  247. *
  248. * Bits 60:63 - Requested Level
  249. * Used to specify which power-saving level must be entered on executing
  250. * stop instruction
  251. *
  252. * @np: /ibm,opal/power-mgt device node
  253. * @flags: cpu-idle-state-flags array
  254. * @dt_idle_states: Number of idle state entries
  255. * Returns 0 on success
  256. */
  257. static int __init pnv_arch300_idle_init(struct device_node *np, u32 *flags,
  258. int dt_idle_states)
  259. {
  260. u64 *psscr_val = NULL;
  261. int rc = 0, i;
  262. psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val),
  263. GFP_KERNEL);
  264. if (!psscr_val) {
  265. rc = -1;
  266. goto out;
  267. }
  268. if (of_property_read_u64_array(np,
  269. "ibm,cpu-idle-state-psscr",
  270. psscr_val, dt_idle_states)) {
  271. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n");
  272. rc = -1;
  273. goto out;
  274. }
  275. /*
  276. * Set pnv_first_deep_stop_state and pnv_deepest_stop_state.
  277. * pnv_first_deep_stop_state should be set to the first stop
  278. * level to cause hypervisor state loss.
  279. * pnv_deepest_stop_state should be set to the deepest stop
  280. * stop state.
  281. */
  282. pnv_first_deep_stop_state = MAX_STOP_STATE;
  283. for (i = 0; i < dt_idle_states; i++) {
  284. u64 psscr_rl = psscr_val[i] & PSSCR_RL_MASK;
  285. if ((flags[i] & OPAL_PM_LOSE_FULL_CONTEXT) &&
  286. (pnv_first_deep_stop_state > psscr_rl))
  287. pnv_first_deep_stop_state = psscr_rl;
  288. if (pnv_deepest_stop_state < psscr_rl)
  289. pnv_deepest_stop_state = psscr_rl;
  290. }
  291. out:
  292. kfree(psscr_val);
  293. return rc;
  294. }
  295. /*
  296. * Probe device tree for supported idle states
  297. */
  298. static void __init pnv_probe_idle_states(void)
  299. {
  300. struct device_node *np;
  301. int dt_idle_states;
  302. u32 *flags = NULL;
  303. int i;
  304. np = of_find_node_by_path("/ibm,opal/power-mgt");
  305. if (!np) {
  306. pr_warn("opal: PowerMgmt Node not found\n");
  307. goto out;
  308. }
  309. dt_idle_states = of_property_count_u32_elems(np,
  310. "ibm,cpu-idle-state-flags");
  311. if (dt_idle_states < 0) {
  312. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  313. goto out;
  314. }
  315. flags = kcalloc(dt_idle_states, sizeof(*flags), GFP_KERNEL);
  316. if (of_property_read_u32_array(np,
  317. "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
  318. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n");
  319. goto out;
  320. }
  321. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  322. if (pnv_arch300_idle_init(np, flags, dt_idle_states))
  323. goto out;
  324. }
  325. for (i = 0; i < dt_idle_states; i++)
  326. supported_cpuidle_states |= flags[i];
  327. out:
  328. kfree(flags);
  329. }
  330. static int __init pnv_init_idle_states(void)
  331. {
  332. supported_cpuidle_states = 0;
  333. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  334. goto out;
  335. pnv_probe_idle_states();
  336. if (!(supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
  337. patch_instruction(
  338. (unsigned int *)pnv_fastsleep_workaround_at_entry,
  339. PPC_INST_NOP);
  340. patch_instruction(
  341. (unsigned int *)pnv_fastsleep_workaround_at_exit,
  342. PPC_INST_NOP);
  343. } else {
  344. /*
  345. * OPAL_PM_SLEEP_ENABLED_ER1 is set. It indicates that
  346. * workaround is needed to use fastsleep. Provide sysfs
  347. * control to choose how this workaround has to be applied.
  348. */
  349. device_create_file(cpu_subsys.dev_root,
  350. &dev_attr_fastsleep_workaround_applyonce);
  351. }
  352. pnv_alloc_idle_core_states();
  353. if (supported_cpuidle_states & OPAL_PM_NAP_ENABLED)
  354. ppc_md.power_save = power7_idle;
  355. else if (supported_cpuidle_states & OPAL_PM_STOP_INST_FAST)
  356. ppc_md.power_save = power9_idle;
  357. out:
  358. return 0;
  359. }
  360. machine_subsys_initcall(powernv, pnv_init_idle_states);