cpuidle-pseries.c 12 KB

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