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