cpuidle-powernv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * cpuidle-powernv - idle state cpuidle driver.
  3. * Adapted from drivers/cpuidle/cpuidle-pseries
  4. *
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/cpuidle.h>
  11. #include <linux/cpu.h>
  12. #include <linux/notifier.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/of.h>
  15. #include <linux/slab.h>
  16. #include <asm/machdep.h>
  17. #include <asm/firmware.h>
  18. #include <asm/opal.h>
  19. #include <asm/runlatch.h>
  20. #include <asm/cpuidle.h>
  21. /*
  22. * Expose only those Hardware idle states via the cpuidle framework
  23. * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
  24. */
  25. #define POWERNV_THRESHOLD_LATENCY_NS 200000
  26. static struct cpuidle_driver powernv_idle_driver = {
  27. .name = "powernv_idle",
  28. .owner = THIS_MODULE,
  29. };
  30. static int max_idle_state;
  31. static struct cpuidle_state *cpuidle_state_table;
  32. struct stop_psscr_table {
  33. u64 val;
  34. u64 mask;
  35. };
  36. static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX];
  37. static u64 snooze_timeout;
  38. static bool snooze_timeout_en;
  39. static int snooze_loop(struct cpuidle_device *dev,
  40. struct cpuidle_driver *drv,
  41. int index)
  42. {
  43. u64 snooze_exit_time;
  44. local_irq_enable();
  45. set_thread_flag(TIF_POLLING_NRFLAG);
  46. snooze_exit_time = get_tb() + snooze_timeout;
  47. ppc64_runlatch_off();
  48. HMT_very_low();
  49. while (!need_resched()) {
  50. if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time)
  51. break;
  52. }
  53. HMT_medium();
  54. ppc64_runlatch_on();
  55. clear_thread_flag(TIF_POLLING_NRFLAG);
  56. smp_mb();
  57. return index;
  58. }
  59. static int nap_loop(struct cpuidle_device *dev,
  60. struct cpuidle_driver *drv,
  61. int index)
  62. {
  63. ppc64_runlatch_off();
  64. power7_idle();
  65. ppc64_runlatch_on();
  66. return index;
  67. }
  68. /* Register for fastsleep only in oneshot mode of broadcast */
  69. #ifdef CONFIG_TICK_ONESHOT
  70. static int fastsleep_loop(struct cpuidle_device *dev,
  71. struct cpuidle_driver *drv,
  72. int index)
  73. {
  74. unsigned long old_lpcr = mfspr(SPRN_LPCR);
  75. unsigned long new_lpcr;
  76. if (unlikely(system_state < SYSTEM_RUNNING))
  77. return index;
  78. new_lpcr = old_lpcr;
  79. /* Do not exit powersave upon decrementer as we've setup the timer
  80. * offload.
  81. */
  82. new_lpcr &= ~LPCR_PECE1;
  83. mtspr(SPRN_LPCR, new_lpcr);
  84. power7_sleep();
  85. mtspr(SPRN_LPCR, old_lpcr);
  86. return index;
  87. }
  88. #endif
  89. static int stop_loop(struct cpuidle_device *dev,
  90. struct cpuidle_driver *drv,
  91. int index)
  92. {
  93. ppc64_runlatch_off();
  94. power9_idle_stop(stop_psscr_table[index].val,
  95. stop_psscr_table[index].mask);
  96. ppc64_runlatch_on();
  97. return index;
  98. }
  99. /*
  100. * States for dedicated partition case.
  101. */
  102. static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
  103. { /* Snooze */
  104. .name = "snooze",
  105. .desc = "snooze",
  106. .exit_latency = 0,
  107. .target_residency = 0,
  108. .enter = snooze_loop },
  109. };
  110. static int powernv_cpuidle_cpu_online(unsigned int cpu)
  111. {
  112. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  113. if (dev && cpuidle_get_driver()) {
  114. cpuidle_pause_and_lock();
  115. cpuidle_enable_device(dev);
  116. cpuidle_resume_and_unlock();
  117. }
  118. return 0;
  119. }
  120. static int powernv_cpuidle_cpu_dead(unsigned int cpu)
  121. {
  122. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  123. if (dev && cpuidle_get_driver()) {
  124. cpuidle_pause_and_lock();
  125. cpuidle_disable_device(dev);
  126. cpuidle_resume_and_unlock();
  127. }
  128. return 0;
  129. }
  130. /*
  131. * powernv_cpuidle_driver_init()
  132. */
  133. static int powernv_cpuidle_driver_init(void)
  134. {
  135. int idle_state;
  136. struct cpuidle_driver *drv = &powernv_idle_driver;
  137. drv->state_count = 0;
  138. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  139. /* Is the state not enabled? */
  140. if (cpuidle_state_table[idle_state].enter == NULL)
  141. continue;
  142. drv->states[drv->state_count] = /* structure copy */
  143. cpuidle_state_table[idle_state];
  144. drv->state_count += 1;
  145. }
  146. /*
  147. * On the PowerNV platform cpu_present may be less than cpu_possible in
  148. * cases when firmware detects the CPU, but it is not available to the
  149. * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
  150. * run time and hence cpu_devices are not created for those CPUs by the
  151. * generic topology_init().
  152. *
  153. * drv->cpumask defaults to cpu_possible_mask in
  154. * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
  155. * cpu_devices are not created for CPUs in cpu_possible_mask that
  156. * cannot be hot-added later at run time.
  157. *
  158. * Trying cpuidle_register_device() on a CPU without a cpu_device is
  159. * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
  160. */
  161. drv->cpumask = (struct cpumask *)cpu_present_mask;
  162. return 0;
  163. }
  164. static inline void add_powernv_state(int index, const char *name,
  165. unsigned int flags,
  166. int (*idle_fn)(struct cpuidle_device *,
  167. struct cpuidle_driver *,
  168. int),
  169. unsigned int target_residency,
  170. unsigned int exit_latency,
  171. u64 psscr_val, u64 psscr_mask)
  172. {
  173. strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
  174. strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
  175. powernv_states[index].flags = flags;
  176. powernv_states[index].target_residency = target_residency;
  177. powernv_states[index].exit_latency = exit_latency;
  178. powernv_states[index].enter = idle_fn;
  179. stop_psscr_table[index].val = psscr_val;
  180. stop_psscr_table[index].mask = psscr_mask;
  181. }
  182. /*
  183. * Returns 0 if prop1_len == prop2_len. Else returns -1
  184. */
  185. static inline int validate_dt_prop_sizes(const char *prop1, int prop1_len,
  186. const char *prop2, int prop2_len)
  187. {
  188. if (prop1_len == prop2_len)
  189. return 0;
  190. pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
  191. prop1, prop2);
  192. return -1;
  193. }
  194. static int powernv_add_idle_states(void)
  195. {
  196. struct device_node *power_mgt;
  197. int nr_idle_states = 1; /* Snooze */
  198. int dt_idle_states, count;
  199. u32 latency_ns[CPUIDLE_STATE_MAX];
  200. u32 residency_ns[CPUIDLE_STATE_MAX];
  201. u32 flags[CPUIDLE_STATE_MAX];
  202. u64 psscr_val[CPUIDLE_STATE_MAX];
  203. u64 psscr_mask[CPUIDLE_STATE_MAX];
  204. const char *names[CPUIDLE_STATE_MAX];
  205. u32 has_stop_states = 0;
  206. int i, rc;
  207. /* Currently we have snooze statically defined */
  208. power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
  209. if (!power_mgt) {
  210. pr_warn("opal: PowerMgmt Node not found\n");
  211. goto out;
  212. }
  213. /* Read values of any property to determine the num of idle states */
  214. dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
  215. if (dt_idle_states < 0) {
  216. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  217. goto out;
  218. }
  219. count = of_property_count_u32_elems(power_mgt,
  220. "ibm,cpu-idle-state-latencies-ns");
  221. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states,
  222. "ibm,cpu-idle-state-latencies-ns",
  223. count) != 0)
  224. goto out;
  225. count = of_property_count_strings(power_mgt,
  226. "ibm,cpu-idle-state-names");
  227. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states,
  228. "ibm,cpu-idle-state-names",
  229. count) != 0)
  230. goto out;
  231. /*
  232. * Since snooze is used as first idle state, max idle states allowed is
  233. * CPUIDLE_STATE_MAX -1
  234. */
  235. if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
  236. pr_warn("cpuidle-powernv: discovered idle states more than allowed");
  237. dt_idle_states = CPUIDLE_STATE_MAX - 1;
  238. }
  239. if (of_property_read_u32_array(power_mgt,
  240. "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
  241. pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
  242. goto out;
  243. }
  244. if (of_property_read_u32_array(power_mgt,
  245. "ibm,cpu-idle-state-latencies-ns", latency_ns,
  246. dt_idle_states)) {
  247. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
  248. goto out;
  249. }
  250. if (of_property_read_string_array(power_mgt,
  251. "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
  252. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
  253. goto out;
  254. }
  255. /*
  256. * If the idle states use stop instruction, probe for psscr values
  257. * and psscr mask which are necessary to specify required stop level.
  258. */
  259. has_stop_states = (flags[0] &
  260. (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
  261. if (has_stop_states) {
  262. count = of_property_count_u64_elems(power_mgt,
  263. "ibm,cpu-idle-state-psscr");
  264. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
  265. dt_idle_states,
  266. "ibm,cpu-idle-state-psscr",
  267. count) != 0)
  268. goto out;
  269. count = of_property_count_u64_elems(power_mgt,
  270. "ibm,cpu-idle-state-psscr-mask");
  271. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
  272. dt_idle_states,
  273. "ibm,cpu-idle-state-psscr-mask",
  274. count) != 0)
  275. goto out;
  276. if (of_property_read_u64_array(power_mgt,
  277. "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
  278. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
  279. goto out;
  280. }
  281. if (of_property_read_u64_array(power_mgt,
  282. "ibm,cpu-idle-state-psscr-mask",
  283. psscr_mask, dt_idle_states)) {
  284. pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
  285. goto out;
  286. }
  287. }
  288. count = of_property_count_u32_elems(power_mgt,
  289. "ibm,cpu-idle-state-residency-ns");
  290. if (count < 0) {
  291. rc = count;
  292. } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
  293. dt_idle_states,
  294. "ibm,cpu-idle-state-residency-ns",
  295. count) != 0) {
  296. goto out;
  297. } else {
  298. rc = of_property_read_u32_array(power_mgt,
  299. "ibm,cpu-idle-state-residency-ns",
  300. residency_ns, dt_idle_states);
  301. }
  302. for (i = 0; i < dt_idle_states; i++) {
  303. unsigned int exit_latency, target_residency;
  304. /*
  305. * If an idle state has exit latency beyond
  306. * POWERNV_THRESHOLD_LATENCY_NS then don't use it
  307. * in cpu-idle.
  308. */
  309. if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
  310. continue;
  311. /*
  312. * Firmware passes residency and latency values in ns.
  313. * cpuidle expects it in us.
  314. */
  315. exit_latency = latency_ns[i] / 1000;
  316. if (!rc)
  317. target_residency = residency_ns[i] / 1000;
  318. else
  319. target_residency = 0;
  320. if (has_stop_states) {
  321. int err = validate_psscr_val_mask(&psscr_val[i],
  322. &psscr_mask[i],
  323. flags[i]);
  324. if (err) {
  325. report_invalid_psscr_val(psscr_val[i], err);
  326. continue;
  327. }
  328. }
  329. /*
  330. * For nap and fastsleep, use default target_residency
  331. * values if f/w does not expose it.
  332. */
  333. if (flags[i] & OPAL_PM_NAP_ENABLED) {
  334. if (!rc)
  335. target_residency = 100;
  336. /* Add NAP state */
  337. add_powernv_state(nr_idle_states, "Nap",
  338. CPUIDLE_FLAG_NONE, nap_loop,
  339. target_residency, exit_latency, 0, 0);
  340. } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) &&
  341. !(flags[i] & OPAL_PM_TIMEBASE_STOP)) {
  342. add_powernv_state(nr_idle_states, names[i],
  343. CPUIDLE_FLAG_NONE, stop_loop,
  344. target_residency, exit_latency,
  345. psscr_val[i], psscr_mask[i]);
  346. }
  347. /*
  348. * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
  349. * within this config dependency check.
  350. */
  351. #ifdef CONFIG_TICK_ONESHOT
  352. if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
  353. flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
  354. if (!rc)
  355. target_residency = 300000;
  356. /* Add FASTSLEEP state */
  357. add_powernv_state(nr_idle_states, "FastSleep",
  358. CPUIDLE_FLAG_TIMER_STOP,
  359. fastsleep_loop,
  360. target_residency, exit_latency, 0, 0);
  361. } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) &&
  362. (flags[i] & OPAL_PM_TIMEBASE_STOP)) {
  363. add_powernv_state(nr_idle_states, names[i],
  364. CPUIDLE_FLAG_TIMER_STOP, stop_loop,
  365. target_residency, exit_latency,
  366. psscr_val[i], psscr_mask[i]);
  367. }
  368. #endif
  369. nr_idle_states++;
  370. }
  371. out:
  372. return nr_idle_states;
  373. }
  374. /*
  375. * powernv_idle_probe()
  376. * Choose state table for shared versus dedicated partition
  377. */
  378. static int powernv_idle_probe(void)
  379. {
  380. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  381. return -ENODEV;
  382. if (firmware_has_feature(FW_FEATURE_OPAL)) {
  383. cpuidle_state_table = powernv_states;
  384. /* Device tree can indicate more idle states */
  385. max_idle_state = powernv_add_idle_states();
  386. if (max_idle_state > 1) {
  387. snooze_timeout_en = true;
  388. snooze_timeout = powernv_states[1].target_residency *
  389. tb_ticks_per_usec;
  390. }
  391. } else
  392. return -ENODEV;
  393. return 0;
  394. }
  395. static int __init powernv_processor_idle_init(void)
  396. {
  397. int retval;
  398. retval = powernv_idle_probe();
  399. if (retval)
  400. return retval;
  401. powernv_cpuidle_driver_init();
  402. retval = cpuidle_register(&powernv_idle_driver, NULL);
  403. if (retval) {
  404. printk(KERN_DEBUG "Registration of powernv driver failed.\n");
  405. return retval;
  406. }
  407. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  408. "cpuidle/powernv:online",
  409. powernv_cpuidle_cpu_online, NULL);
  410. WARN_ON(retval < 0);
  411. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  412. "cpuidle/powernv:dead", NULL,
  413. powernv_cpuidle_cpu_dead);
  414. WARN_ON(retval < 0);
  415. printk(KERN_DEBUG "powernv_idle_driver registered\n");
  416. return 0;
  417. }
  418. device_initcall(powernv_processor_idle_init);