cpuidle-pseries.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cpuidle-pseries - idle state cpuidle driver.
  4. * Adapted from drivers/idle/intel_idle.c and
  5. * drivers/acpi/processor_idle.c
  6. *
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/cpuidle.h>
  13. #include <linux/cpu.h>
  14. #include <linux/notifier.h>
  15. #include <asm/paca.h>
  16. #include <asm/reg.h>
  17. #include <asm/machdep.h>
  18. #include <asm/firmware.h>
  19. #include <asm/runlatch.h>
  20. #include <asm/idle.h>
  21. #include <asm/plpar_wrappers.h>
  22. #include <asm/rtas.h>
  23. static struct cpuidle_driver pseries_idle_driver = {
  24. .name = "pseries_idle",
  25. .owner = THIS_MODULE,
  26. };
  27. static int max_idle_state __read_mostly;
  28. static struct cpuidle_state *cpuidle_state_table __read_mostly;
  29. static u64 snooze_timeout __read_mostly;
  30. static bool snooze_timeout_en __read_mostly;
  31. static int snooze_loop(struct cpuidle_device *dev,
  32. struct cpuidle_driver *drv,
  33. int index)
  34. {
  35. u64 snooze_exit_time;
  36. set_thread_flag(TIF_POLLING_NRFLAG);
  37. pseries_idle_prolog();
  38. local_irq_enable();
  39. snooze_exit_time = get_tb() + snooze_timeout;
  40. while (!need_resched()) {
  41. HMT_low();
  42. HMT_very_low();
  43. if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
  44. /*
  45. * Task has not woken up but we are exiting the polling
  46. * loop anyway. Require a barrier after polling is
  47. * cleared to order subsequent test of need_resched().
  48. */
  49. clear_thread_flag(TIF_POLLING_NRFLAG);
  50. smp_mb();
  51. break;
  52. }
  53. }
  54. HMT_medium();
  55. clear_thread_flag(TIF_POLLING_NRFLAG);
  56. local_irq_disable();
  57. pseries_idle_epilog();
  58. return index;
  59. }
  60. static void check_and_cede_processor(void)
  61. {
  62. /*
  63. * Ensure our interrupt state is properly tracked,
  64. * also checks if no interrupt has occurred while we
  65. * were soft-disabled
  66. */
  67. if (prep_irq_for_idle()) {
  68. cede_processor();
  69. #ifdef CONFIG_TRACE_IRQFLAGS
  70. /* Ensure that H_CEDE returns with IRQs on */
  71. if (WARN_ON(!(mfmsr() & MSR_EE)))
  72. __hard_irq_enable();
  73. #endif
  74. }
  75. }
  76. /*
  77. * XCEDE: Extended CEDE states discovered through the
  78. * "ibm,get-systems-parameter" RTAS call with the token
  79. * CEDE_LATENCY_TOKEN
  80. */
  81. /*
  82. * Section 7.3.16 System Parameters Option of PAPR version 2.8.1 has a
  83. * table with all the parameters to ibm,get-system-parameters.
  84. * CEDE_LATENCY_TOKEN corresponds to the token value for Cede Latency
  85. * Settings Information.
  86. */
  87. #define CEDE_LATENCY_TOKEN 45
  88. /*
  89. * If the platform supports the cede latency settings information system
  90. * parameter it must provide the following information in the NULL terminated
  91. * parameter string:
  92. *
  93. * a. The first byte is the length “N” of each cede latency setting record minus
  94. * one (zero indicates a length of 1 byte).
  95. *
  96. * b. For each supported cede latency setting a cede latency setting record
  97. * consisting of the first “N” bytes as per the following table.
  98. *
  99. * -----------------------------
  100. * | Field | Field |
  101. * | Name | Length |
  102. * -----------------------------
  103. * | Cede Latency | 1 Byte |
  104. * | Specifier Value | |
  105. * -----------------------------
  106. * | Maximum wakeup | |
  107. * | latency in | 8 Bytes |
  108. * | tb-ticks | |
  109. * -----------------------------
  110. * | Responsive to | |
  111. * | external | 1 Byte |
  112. * | interrupts | |
  113. * -----------------------------
  114. *
  115. * This version has cede latency record size = 10.
  116. *
  117. * The structure xcede_latency_payload represents a) and b) with
  118. * xcede_latency_record representing the table in b).
  119. *
  120. * xcede_latency_parameter is what gets returned by
  121. * ibm,get-systems-parameter RTAS call when made with
  122. * CEDE_LATENCY_TOKEN.
  123. *
  124. * These structures are only used to represent the data obtained by the RTAS
  125. * call. The data is in big-endian.
  126. */
  127. struct xcede_latency_record {
  128. u8 hint;
  129. __be64 latency_ticks;
  130. u8 wake_on_irqs;
  131. } __packed;
  132. // Make space for 16 records, which "should be enough".
  133. struct xcede_latency_payload {
  134. u8 record_size;
  135. struct xcede_latency_record records[16];
  136. } __packed;
  137. struct xcede_latency_parameter {
  138. __be16 payload_size;
  139. struct xcede_latency_payload payload;
  140. u8 null_char;
  141. } __packed;
  142. static unsigned int nr_xcede_records;
  143. static struct xcede_latency_parameter xcede_latency_parameter __initdata;
  144. static int __init parse_cede_parameters(void)
  145. {
  146. struct xcede_latency_payload *payload;
  147. u32 total_xcede_records_size;
  148. u8 xcede_record_size;
  149. u16 payload_size;
  150. int ret, i;
  151. ret = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  152. NULL, CEDE_LATENCY_TOKEN, __pa(&xcede_latency_parameter),
  153. sizeof(xcede_latency_parameter));
  154. if (ret) {
  155. pr_err("xcede: Error parsing CEDE_LATENCY_TOKEN\n");
  156. return ret;
  157. }
  158. payload_size = be16_to_cpu(xcede_latency_parameter.payload_size);
  159. payload = &xcede_latency_parameter.payload;
  160. xcede_record_size = payload->record_size + 1;
  161. if (xcede_record_size != sizeof(struct xcede_latency_record)) {
  162. pr_err("xcede: Expected record-size %lu. Observed size %u.\n",
  163. sizeof(struct xcede_latency_record), xcede_record_size);
  164. return -EINVAL;
  165. }
  166. pr_info("xcede: xcede_record_size = %d\n", xcede_record_size);
  167. /*
  168. * Since the payload_size includes the last NULL byte and the
  169. * xcede_record_size, the remaining bytes correspond to array of all
  170. * cede_latency settings.
  171. */
  172. total_xcede_records_size = payload_size - 2;
  173. nr_xcede_records = total_xcede_records_size / xcede_record_size;
  174. for (i = 0; i < nr_xcede_records; i++) {
  175. struct xcede_latency_record *record = &payload->records[i];
  176. u64 latency_ticks = be64_to_cpu(record->latency_ticks);
  177. u8 wake_on_irqs = record->wake_on_irqs;
  178. u8 hint = record->hint;
  179. pr_info("xcede: Record %d : hint = %u, latency = 0x%llx tb ticks, Wake-on-irq = %u\n",
  180. i, hint, latency_ticks, wake_on_irqs);
  181. }
  182. return 0;
  183. }
  184. #define NR_DEDICATED_STATES 2 /* snooze, CEDE */
  185. static u8 cede_latency_hint[NR_DEDICATED_STATES];
  186. static int dedicated_cede_loop(struct cpuidle_device *dev,
  187. struct cpuidle_driver *drv,
  188. int index)
  189. {
  190. u8 old_latency_hint;
  191. pseries_idle_prolog();
  192. get_lppaca()->donate_dedicated_cpu = 1;
  193. old_latency_hint = get_lppaca()->cede_latency_hint;
  194. get_lppaca()->cede_latency_hint = cede_latency_hint[index];
  195. HMT_medium();
  196. check_and_cede_processor();
  197. local_irq_disable();
  198. get_lppaca()->donate_dedicated_cpu = 0;
  199. get_lppaca()->cede_latency_hint = old_latency_hint;
  200. pseries_idle_epilog();
  201. return index;
  202. }
  203. static int shared_cede_loop(struct cpuidle_device *dev,
  204. struct cpuidle_driver *drv,
  205. int index)
  206. {
  207. pseries_idle_prolog();
  208. /*
  209. * Yield the processor to the hypervisor. We return if
  210. * an external interrupt occurs (which are driven prior
  211. * to returning here) or if a prod occurs from another
  212. * processor. When returning here, external interrupts
  213. * are enabled.
  214. */
  215. check_and_cede_processor();
  216. local_irq_disable();
  217. pseries_idle_epilog();
  218. return index;
  219. }
  220. /*
  221. * States for dedicated partition case.
  222. */
  223. static struct cpuidle_state dedicated_states[NR_DEDICATED_STATES] = {
  224. { /* Snooze */
  225. .name = "snooze",
  226. .desc = "snooze",
  227. .exit_latency = 0,
  228. .target_residency = 0,
  229. .enter = &snooze_loop },
  230. { /* CEDE */
  231. .name = "CEDE",
  232. .desc = "CEDE",
  233. .exit_latency = 10,
  234. .target_residency = 100,
  235. .enter = &dedicated_cede_loop },
  236. };
  237. /*
  238. * States for shared partition case.
  239. */
  240. static struct cpuidle_state shared_states[] = {
  241. { /* Snooze */
  242. .name = "snooze",
  243. .desc = "snooze",
  244. .exit_latency = 0,
  245. .target_residency = 0,
  246. .enter = &snooze_loop },
  247. { /* Shared Cede */
  248. .name = "Shared Cede",
  249. .desc = "Shared Cede",
  250. .exit_latency = 10,
  251. .target_residency = 100,
  252. .enter = &shared_cede_loop },
  253. };
  254. static int pseries_cpuidle_cpu_online(unsigned int cpu)
  255. {
  256. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  257. if (dev && cpuidle_get_driver()) {
  258. cpuidle_pause_and_lock();
  259. cpuidle_enable_device(dev);
  260. cpuidle_resume_and_unlock();
  261. }
  262. return 0;
  263. }
  264. static int pseries_cpuidle_cpu_dead(unsigned int cpu)
  265. {
  266. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  267. if (dev && cpuidle_get_driver()) {
  268. cpuidle_pause_and_lock();
  269. cpuidle_disable_device(dev);
  270. cpuidle_resume_and_unlock();
  271. }
  272. return 0;
  273. }
  274. /*
  275. * pseries_cpuidle_driver_init()
  276. */
  277. static int pseries_cpuidle_driver_init(void)
  278. {
  279. int idle_state;
  280. struct cpuidle_driver *drv = &pseries_idle_driver;
  281. drv->state_count = 0;
  282. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  283. /* Is the state not enabled? */
  284. if (cpuidle_state_table[idle_state].enter == NULL)
  285. continue;
  286. drv->states[drv->state_count] = /* structure copy */
  287. cpuidle_state_table[idle_state];
  288. drv->state_count += 1;
  289. }
  290. return 0;
  291. }
  292. static void __init fixup_cede0_latency(void)
  293. {
  294. struct xcede_latency_payload *payload;
  295. u64 min_latency_us;
  296. int i;
  297. min_latency_us = dedicated_states[1].exit_latency; // CEDE latency
  298. if (parse_cede_parameters())
  299. return;
  300. pr_info("cpuidle: Skipping the %d Extended CEDE idle states\n",
  301. nr_xcede_records);
  302. payload = &xcede_latency_parameter.payload;
  303. for (i = 0; i < nr_xcede_records; i++) {
  304. struct xcede_latency_record *record = &payload->records[i];
  305. u64 latency_tb = be64_to_cpu(record->latency_ticks);
  306. u64 latency_us = DIV_ROUND_UP_ULL(tb_to_ns(latency_tb), NSEC_PER_USEC);
  307. if (latency_us == 0)
  308. pr_warn("cpuidle: xcede record %d has an unrealistic latency of 0us.\n", i);
  309. if (latency_us < min_latency_us)
  310. min_latency_us = latency_us;
  311. }
  312. /*
  313. * By default, we assume that CEDE(0) has exit latency 10us,
  314. * since there is no way for us to query from the platform.
  315. *
  316. * However, if the wakeup latency of an Extended CEDE state is
  317. * smaller than 10us, then we can be sure that CEDE(0)
  318. * requires no more than that.
  319. *
  320. * Perform the fix-up.
  321. */
  322. if (min_latency_us < dedicated_states[1].exit_latency) {
  323. /*
  324. * We set a minimum of 1us wakeup latency for cede0 to
  325. * distinguish it from snooze
  326. */
  327. u64 cede0_latency = 1;
  328. if (min_latency_us > cede0_latency)
  329. cede0_latency = min_latency_us - 1;
  330. dedicated_states[1].exit_latency = cede0_latency;
  331. dedicated_states[1].target_residency = 10 * (cede0_latency);
  332. pr_info("cpuidle: Fixed up CEDE exit latency to %llu us\n",
  333. cede0_latency);
  334. }
  335. }
  336. /*
  337. * pseries_idle_probe()
  338. * Choose state table for shared versus dedicated partition
  339. */
  340. static int __init pseries_idle_probe(void)
  341. {
  342. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  343. return -ENODEV;
  344. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  345. /*
  346. * Use local_paca instead of get_lppaca() since
  347. * preemption is not disabled, and it is not required in
  348. * fact, since lppaca_ptr does not need to be the value
  349. * associated to the current CPU, it can be from any CPU.
  350. */
  351. if (lppaca_shared_proc(local_paca->lppaca_ptr)) {
  352. cpuidle_state_table = shared_states;
  353. max_idle_state = ARRAY_SIZE(shared_states);
  354. } else {
  355. /*
  356. * Use firmware provided latency values
  357. * starting with POWER10 platforms. In the
  358. * case that we are running on a POWER10
  359. * platform but in an earlier compat mode, we
  360. * can still use the firmware provided values.
  361. *
  362. * However, on platforms prior to POWER10, we
  363. * cannot rely on the accuracy of the firmware
  364. * provided latency values. On such platforms,
  365. * go with the conservative default estimate
  366. * of 10us.
  367. */
  368. if (cpu_has_feature(CPU_FTR_ARCH_31) || pvr_version_is(PVR_POWER10))
  369. fixup_cede0_latency();
  370. cpuidle_state_table = dedicated_states;
  371. max_idle_state = NR_DEDICATED_STATES;
  372. }
  373. } else
  374. return -ENODEV;
  375. if (max_idle_state > 1) {
  376. snooze_timeout_en = true;
  377. snooze_timeout = cpuidle_state_table[1].target_residency *
  378. tb_ticks_per_usec;
  379. }
  380. return 0;
  381. }
  382. static int __init pseries_processor_idle_init(void)
  383. {
  384. int retval;
  385. retval = pseries_idle_probe();
  386. if (retval)
  387. return retval;
  388. pseries_cpuidle_driver_init();
  389. retval = cpuidle_register(&pseries_idle_driver, NULL);
  390. if (retval) {
  391. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  392. return retval;
  393. }
  394. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  395. "cpuidle/pseries:online",
  396. pseries_cpuidle_cpu_online, NULL);
  397. WARN_ON(retval < 0);
  398. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  399. "cpuidle/pseries:DEAD", NULL,
  400. pseries_cpuidle_cpu_dead);
  401. WARN_ON(retval < 0);
  402. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  403. return 0;
  404. }
  405. device_initcall(pseries_processor_idle_init);