Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 2 | /* |
Deepthi Dharwar | 962e7bd | 2014-01-14 16:26:02 +0530 | [diff] [blame] | 3 | * cpuidle-pseries - idle state cpuidle driver. |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 4 | * Adapted from drivers/idle/intel_idle.c and |
| 5 | * drivers/acpi/processor_idle.c |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/cpuidle.h> |
| 14 | #include <linux/cpu.h> |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 15 | #include <linux/notifier.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 16 | |
| 17 | #include <asm/paca.h> |
| 18 | #include <asm/reg.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 19 | #include <asm/machdep.h> |
| 20 | #include <asm/firmware.h> |
Preeti U Murthy | 3f67d96 | 2014-02-12 10:18:45 +0530 | [diff] [blame] | 21 | #include <asm/runlatch.h> |
Gautham R. Shenoy | e4a884c | 2020-04-07 14:17:39 +0530 | [diff] [blame] | 22 | #include <asm/idle.h> |
Deepthi Dharwar | 212bebb | 2013-08-22 15:23:52 +0530 | [diff] [blame] | 23 | #include <asm/plpar_wrappers.h> |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 24 | #include <asm/rtas.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 25 | |
Wei Yongjun | 92fe848 | 2020-07-14 22:24:24 +0800 | [diff] [blame] | 26 | static struct cpuidle_driver pseries_idle_driver = { |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 27 | .name = "pseries_idle", |
| 28 | .owner = THIS_MODULE, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Nicholas Piggin | 624e46d | 2017-06-14 23:02:40 +1000 | [diff] [blame] | 31 | static int max_idle_state __read_mostly; |
| 32 | static struct cpuidle_state *cpuidle_state_table __read_mostly; |
| 33 | static u64 snooze_timeout __read_mostly; |
| 34 | static bool snooze_timeout_en __read_mostly; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 35 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 36 | static __cpuidle |
| 37 | int snooze_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv, |
| 38 | int index) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 39 | { |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 40 | u64 snooze_exit_time; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 41 | |
Nicholas Piggin | 3fc5ee92 | 2017-06-14 23:02:39 +1000 | [diff] [blame] | 42 | set_thread_flag(TIF_POLLING_NRFLAG); |
| 43 | |
Gautham R. Shenoy | c401919 | 2020-04-07 14:17:40 +0530 | [diff] [blame] | 44 | pseries_idle_prolog(); |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 45 | raw_local_irq_enable(); |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 46 | snooze_exit_time = get_tb() + snooze_timeout; |
Aboorva Devarajan | 5ddcc03 | 2022-11-14 20:26:11 +0530 | [diff] [blame] | 47 | dev->poll_time_limit = false; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 48 | |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 49 | while (!need_resched()) { |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 50 | HMT_low(); |
| 51 | HMT_very_low(); |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 52 | if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { |
| 53 | /* |
| 54 | * Task has not woken up but we are exiting the polling |
| 55 | * loop anyway. Require a barrier after polling is |
| 56 | * cleared to order subsequent test of need_resched(). |
| 57 | */ |
Aboorva Devarajan | 5ddcc03 | 2022-11-14 20:26:11 +0530 | [diff] [blame] | 58 | dev->poll_time_limit = true; |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 59 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 60 | smp_mb(); |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 61 | break; |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 62 | } |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 65 | HMT_medium(); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 66 | clear_thread_flag(TIF_POLLING_NRFLAG); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 67 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 68 | raw_local_irq_disable(); |
Nicholas Piggin | ced54c0 | 2017-11-17 02:00:52 +1000 | [diff] [blame] | 69 | |
Gautham R. Shenoy | c401919 | 2020-04-07 14:17:40 +0530 | [diff] [blame] | 70 | pseries_idle_epilog(); |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 71 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 72 | return index; |
| 73 | } |
| 74 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 75 | static __cpuidle void check_and_cede_processor(void) |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 76 | { |
| 77 | /* |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 78 | * Ensure our interrupt state is properly tracked, |
| 79 | * also checks if no interrupt has occurred while we |
| 80 | * were soft-disabled |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 81 | */ |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 82 | if (prep_irq_for_idle()) { |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 83 | cede_processor(); |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 84 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 85 | /* Ensure that H_CEDE returns with IRQs on */ |
| 86 | if (WARN_ON(!(mfmsr() & MSR_EE))) |
| 87 | __hard_irq_enable(); |
| 88 | #endif |
| 89 | } |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 90 | } |
| 91 | |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 92 | /* |
| 93 | * XCEDE: Extended CEDE states discovered through the |
| 94 | * "ibm,get-systems-parameter" RTAS call with the token |
| 95 | * CEDE_LATENCY_TOKEN |
| 96 | */ |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 97 | |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 98 | /* |
| 99 | * Section 7.3.16 System Parameters Option of PAPR version 2.8.1 has a |
| 100 | * table with all the parameters to ibm,get-system-parameters. |
| 101 | * CEDE_LATENCY_TOKEN corresponds to the token value for Cede Latency |
| 102 | * Settings Information. |
| 103 | */ |
| 104 | #define CEDE_LATENCY_TOKEN 45 |
| 105 | |
| 106 | /* |
| 107 | * If the platform supports the cede latency settings information system |
| 108 | * parameter it must provide the following information in the NULL terminated |
| 109 | * parameter string: |
| 110 | * |
| 111 | * a. The first byte is the length āNā of each cede latency setting record minus |
| 112 | * one (zero indicates a length of 1 byte). |
| 113 | * |
| 114 | * b. For each supported cede latency setting a cede latency setting record |
| 115 | * consisting of the first āNā bytes as per the following table. |
| 116 | * |
| 117 | * ----------------------------- |
| 118 | * | Field | Field | |
| 119 | * | Name | Length | |
| 120 | * ----------------------------- |
| 121 | * | Cede Latency | 1 Byte | |
| 122 | * | Specifier Value | | |
| 123 | * ----------------------------- |
| 124 | * | Maximum wakeup | | |
| 125 | * | latency in | 8 Bytes | |
| 126 | * | tb-ticks | | |
| 127 | * ----------------------------- |
| 128 | * | Responsive to | | |
| 129 | * | external | 1 Byte | |
| 130 | * | interrupts | | |
| 131 | * ----------------------------- |
| 132 | * |
| 133 | * This version has cede latency record size = 10. |
| 134 | * |
| 135 | * The structure xcede_latency_payload represents a) and b) with |
| 136 | * xcede_latency_record representing the table in b). |
| 137 | * |
| 138 | * xcede_latency_parameter is what gets returned by |
| 139 | * ibm,get-systems-parameter RTAS call when made with |
| 140 | * CEDE_LATENCY_TOKEN. |
| 141 | * |
| 142 | * These structures are only used to represent the data obtained by the RTAS |
| 143 | * call. The data is in big-endian. |
| 144 | */ |
| 145 | struct xcede_latency_record { |
| 146 | u8 hint; |
| 147 | __be64 latency_ticks; |
| 148 | u8 wake_on_irqs; |
| 149 | } __packed; |
| 150 | |
| 151 | // Make space for 16 records, which "should be enough". |
| 152 | struct xcede_latency_payload { |
| 153 | u8 record_size; |
| 154 | struct xcede_latency_record records[16]; |
| 155 | } __packed; |
| 156 | |
| 157 | struct xcede_latency_parameter { |
| 158 | __be16 payload_size; |
| 159 | struct xcede_latency_payload payload; |
| 160 | u8 null_char; |
| 161 | } __packed; |
| 162 | |
| 163 | static unsigned int nr_xcede_records; |
| 164 | static struct xcede_latency_parameter xcede_latency_parameter __initdata; |
| 165 | |
| 166 | static int __init parse_cede_parameters(void) |
| 167 | { |
| 168 | struct xcede_latency_payload *payload; |
| 169 | u32 total_xcede_records_size; |
| 170 | u8 xcede_record_size; |
| 171 | u16 payload_size; |
| 172 | int ret, i; |
| 173 | |
| 174 | ret = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1, |
| 175 | NULL, CEDE_LATENCY_TOKEN, __pa(&xcede_latency_parameter), |
| 176 | sizeof(xcede_latency_parameter)); |
| 177 | if (ret) { |
| 178 | pr_err("xcede: Error parsing CEDE_LATENCY_TOKEN\n"); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | payload_size = be16_to_cpu(xcede_latency_parameter.payload_size); |
| 183 | payload = &xcede_latency_parameter.payload; |
| 184 | |
| 185 | xcede_record_size = payload->record_size + 1; |
| 186 | |
| 187 | if (xcede_record_size != sizeof(struct xcede_latency_record)) { |
| 188 | pr_err("xcede: Expected record-size %lu. Observed size %u.\n", |
| 189 | sizeof(struct xcede_latency_record), xcede_record_size); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | pr_info("xcede: xcede_record_size = %d\n", xcede_record_size); |
| 194 | |
| 195 | /* |
| 196 | * Since the payload_size includes the last NULL byte and the |
| 197 | * xcede_record_size, the remaining bytes correspond to array of all |
| 198 | * cede_latency settings. |
| 199 | */ |
| 200 | total_xcede_records_size = payload_size - 2; |
| 201 | nr_xcede_records = total_xcede_records_size / xcede_record_size; |
| 202 | |
| 203 | for (i = 0; i < nr_xcede_records; i++) { |
| 204 | struct xcede_latency_record *record = &payload->records[i]; |
| 205 | u64 latency_ticks = be64_to_cpu(record->latency_ticks); |
| 206 | u8 wake_on_irqs = record->wake_on_irqs; |
| 207 | u8 hint = record->hint; |
| 208 | |
| 209 | pr_info("xcede: Record %d : hint = %u, latency = 0x%llx tb ticks, Wake-on-irq = %u\n", |
| 210 | i, hint, latency_ticks, wake_on_irqs); |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | #define NR_DEDICATED_STATES 2 /* snooze, CEDE */ |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 217 | static u8 cede_latency_hint[NR_DEDICATED_STATES]; |
| 218 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 219 | static __cpuidle |
| 220 | int dedicated_cede_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv, |
| 221 | int index) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 222 | { |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 223 | u8 old_latency_hint; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 224 | |
Gautham R. Shenoy | c401919 | 2020-04-07 14:17:40 +0530 | [diff] [blame] | 225 | pseries_idle_prolog(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 226 | get_lppaca()->donate_dedicated_cpu = 1; |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 227 | old_latency_hint = get_lppaca()->cede_latency_hint; |
| 228 | get_lppaca()->cede_latency_hint = cede_latency_hint[index]; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 229 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 230 | HMT_medium(); |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 231 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 232 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 233 | raw_local_irq_disable(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 234 | get_lppaca()->donate_dedicated_cpu = 0; |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 235 | get_lppaca()->cede_latency_hint = old_latency_hint; |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 236 | |
Gautham R. Shenoy | c401919 | 2020-04-07 14:17:40 +0530 | [diff] [blame] | 237 | pseries_idle_epilog(); |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 238 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 239 | return index; |
| 240 | } |
| 241 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 242 | static __cpuidle |
| 243 | int shared_cede_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv, |
| 244 | int index) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 245 | { |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 246 | |
Gautham R. Shenoy | c401919 | 2020-04-07 14:17:40 +0530 | [diff] [blame] | 247 | pseries_idle_prolog(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * Yield the processor to the hypervisor. We return if |
| 251 | * an external interrupt occurs (which are driven prior |
| 252 | * to returning here) or if a prod occurs from another |
| 253 | * processor. When returning here, external interrupts |
| 254 | * are enabled. |
| 255 | */ |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 256 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 257 | |
Michael Ellerman | 8899074 | 2023-04-07 00:45:34 +1000 | [diff] [blame] | 258 | raw_local_irq_disable(); |
Gautham R. Shenoy | c401919 | 2020-04-07 14:17:40 +0530 | [diff] [blame] | 259 | pseries_idle_epilog(); |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 260 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 261 | return index; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * States for dedicated partition case. |
| 266 | */ |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 267 | static struct cpuidle_state dedicated_states[NR_DEDICATED_STATES] = { |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 268 | { /* Snooze */ |
| 269 | .name = "snooze", |
| 270 | .desc = "snooze", |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 271 | .exit_latency = 0, |
| 272 | .target_residency = 0, |
Aboorva Devarajan | 5ddcc03 | 2022-11-14 20:26:11 +0530 | [diff] [blame] | 273 | .enter = &snooze_loop, |
| 274 | .flags = CPUIDLE_FLAG_POLLING }, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 275 | { /* CEDE */ |
| 276 | .name = "CEDE", |
| 277 | .desc = "CEDE", |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 278 | .exit_latency = 10, |
| 279 | .target_residency = 100, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 280 | .enter = &dedicated_cede_loop }, |
| 281 | }; |
| 282 | |
| 283 | /* |
| 284 | * States for shared partition case. |
| 285 | */ |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 286 | static struct cpuidle_state shared_states[] = { |
Nicholas Piggin | f2ac428 | 2017-10-10 17:11:09 +1000 | [diff] [blame] | 287 | { /* Snooze */ |
| 288 | .name = "snooze", |
| 289 | .desc = "snooze", |
| 290 | .exit_latency = 0, |
| 291 | .target_residency = 0, |
Aboorva Devarajan | 5ddcc03 | 2022-11-14 20:26:11 +0530 | [diff] [blame] | 292 | .enter = &snooze_loop, |
| 293 | .flags = CPUIDLE_FLAG_POLLING }, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 294 | { /* Shared Cede */ |
| 295 | .name = "Shared Cede", |
| 296 | .desc = "Shared Cede", |
Nicholas Piggin | f2ac428 | 2017-10-10 17:11:09 +1000 | [diff] [blame] | 297 | .exit_latency = 10, |
| 298 | .target_residency = 100, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 299 | .enter = &shared_cede_loop }, |
| 300 | }; |
| 301 | |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 302 | static int pseries_cpuidle_cpu_online(unsigned int cpu) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 303 | { |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 304 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 305 | |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 306 | if (dev && cpuidle_get_driver()) { |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 307 | cpuidle_pause_and_lock(); |
| 308 | cpuidle_enable_device(dev); |
| 309 | cpuidle_resume_and_unlock(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 310 | } |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 311 | return 0; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 314 | static int pseries_cpuidle_cpu_dead(unsigned int cpu) |
| 315 | { |
| 316 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
| 317 | |
| 318 | if (dev && cpuidle_get_driver()) { |
| 319 | cpuidle_pause_and_lock(); |
| 320 | cpuidle_disable_device(dev); |
| 321 | cpuidle_resume_and_unlock(); |
| 322 | } |
| 323 | return 0; |
| 324 | } |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 325 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 326 | /* |
| 327 | * pseries_cpuidle_driver_init() |
| 328 | */ |
| 329 | static int pseries_cpuidle_driver_init(void) |
| 330 | { |
| 331 | int idle_state; |
| 332 | struct cpuidle_driver *drv = &pseries_idle_driver; |
| 333 | |
| 334 | drv->state_count = 0; |
| 335 | |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 336 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
| 337 | /* Is the state not enabled? */ |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 338 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 339 | continue; |
| 340 | |
| 341 | drv->states[drv->state_count] = /* structure copy */ |
| 342 | cpuidle_state_table[idle_state]; |
| 343 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 344 | drv->state_count += 1; |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 350 | static void __init fixup_cede0_latency(void) |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 351 | { |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 352 | struct xcede_latency_payload *payload; |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 353 | u64 min_xcede_latency_us = UINT_MAX; |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 354 | int i; |
| 355 | |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 356 | if (parse_cede_parameters()) |
| 357 | return; |
| 358 | |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 359 | pr_info("cpuidle: Skipping the %d Extended CEDE idle states\n", |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 360 | nr_xcede_records); |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 361 | |
| 362 | payload = &xcede_latency_parameter.payload; |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * The CEDE idle state maps to CEDE(0). While the hypervisor |
| 366 | * does not advertise CEDE(0) exit latency values, it does |
| 367 | * advertise the latency values of the extended CEDE states. |
| 368 | * We use the lowest advertised exit latency value as a proxy |
| 369 | * for the exit latency of CEDE(0). |
| 370 | */ |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 371 | for (i = 0; i < nr_xcede_records; i++) { |
| 372 | struct xcede_latency_record *record = &payload->records[i]; |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 373 | u8 hint = record->hint; |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 374 | u64 latency_tb = be64_to_cpu(record->latency_ticks); |
Gautham R. Shenoy | 1d3ee7d | 2020-09-03 14:57:27 +0530 | [diff] [blame] | 375 | u64 latency_us = DIV_ROUND_UP_ULL(tb_to_ns(latency_tb), NSEC_PER_USEC); |
| 376 | |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 377 | /* |
| 378 | * We expect the exit latency of an extended CEDE |
| 379 | * state to be non-zero, it to since it takes at least |
| 380 | * a few nanoseconds to wakeup the idle CPU and |
| 381 | * dispatch the virtual processor into the Linux |
| 382 | * Guest. |
| 383 | * |
| 384 | * So we consider only non-zero value for performing |
| 385 | * the fixup of CEDE(0) latency. |
| 386 | */ |
| 387 | if (latency_us == 0) { |
| 388 | pr_warn("cpuidle: Skipping xcede record %d [hint=%d]. Exit latency = 0us\n", |
| 389 | i, hint); |
| 390 | continue; |
| 391 | } |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 392 | |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 393 | if (latency_us < min_xcede_latency_us) |
| 394 | min_xcede_latency_us = latency_us; |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 395 | } |
| 396 | |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 397 | if (min_xcede_latency_us != UINT_MAX) { |
| 398 | dedicated_states[1].exit_latency = min_xcede_latency_us; |
| 399 | dedicated_states[1].target_residency = 10 * (min_xcede_latency_us); |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 400 | pr_info("cpuidle: Fixed up CEDE exit latency to %llu us\n", |
Gautham R. Shenoy | 71737a6 | 2021-07-19 12:03:19 +0530 | [diff] [blame] | 401 | min_xcede_latency_us); |
Gautham R. Shenoy | d947fb4 | 2020-07-30 11:02:57 +0530 | [diff] [blame] | 402 | } |
| 403 | |
Gautham R. Shenoy | 054e44b | 2020-07-30 11:02:56 +0530 | [diff] [blame] | 404 | } |
| 405 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 406 | /* |
| 407 | * pseries_idle_probe() |
| 408 | * Choose state table for shared versus dedicated partition |
| 409 | */ |
Nathan Chancellor | d04691d | 2021-08-03 14:15:47 -0700 | [diff] [blame] | 410 | static int __init pseries_idle_probe(void) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 411 | { |
| 412 | |
Deepthi Dharwar | e8bb3e0 | 2011-11-30 02:47:03 +0000 | [diff] [blame] | 413 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 414 | return -ENODEV; |
| 415 | |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 416 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
Russell Currey | eac030b | 2023-08-23 15:53:17 +1000 | [diff] [blame] | 417 | if (lppaca_shared_proc()) { |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 418 | cpuidle_state_table = shared_states; |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 419 | max_idle_state = ARRAY_SIZE(shared_states); |
| 420 | } else { |
Gautham R. Shenoy | 50741b7 | 2021-07-19 12:03:18 +0530 | [diff] [blame] | 421 | /* |
| 422 | * Use firmware provided latency values |
| 423 | * starting with POWER10 platforms. In the |
| 424 | * case that we are running on a POWER10 |
| 425 | * platform but in an earlier compat mode, we |
| 426 | * can still use the firmware provided values. |
| 427 | * |
| 428 | * However, on platforms prior to POWER10, we |
| 429 | * cannot rely on the accuracy of the firmware |
| 430 | * provided latency values. On such platforms, |
| 431 | * go with the conservative default estimate |
| 432 | * of 10us. |
| 433 | */ |
| 434 | if (cpu_has_feature(CPU_FTR_ARCH_31) || pvr_version_is(PVR_POWER10)) |
| 435 | fixup_cede0_latency(); |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 436 | cpuidle_state_table = dedicated_states; |
Gautham R. Shenoy | 3af0ada | 2020-07-30 11:02:55 +0530 | [diff] [blame] | 437 | max_idle_state = NR_DEDICATED_STATES; |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 438 | } |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 439 | } else |
| 440 | return -ENODEV; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 441 | |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 442 | if (max_idle_state > 1) { |
| 443 | snooze_timeout_en = true; |
| 444 | snooze_timeout = cpuidle_state_table[1].target_residency * |
| 445 | tb_ticks_per_usec; |
| 446 | } |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int __init pseries_processor_idle_init(void) |
| 451 | { |
| 452 | int retval; |
| 453 | |
| 454 | retval = pseries_idle_probe(); |
| 455 | if (retval) |
| 456 | return retval; |
| 457 | |
| 458 | pseries_cpuidle_driver_init(); |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 459 | retval = cpuidle_register(&pseries_idle_driver, NULL); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 460 | if (retval) { |
| 461 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); |
| 462 | return retval; |
| 463 | } |
| 464 | |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 465 | retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 466 | "cpuidle/pseries:online", |
| 467 | pseries_cpuidle_cpu_online, NULL); |
| 468 | WARN_ON(retval < 0); |
| 469 | retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD, |
| 470 | "cpuidle/pseries:DEAD", NULL, |
| 471 | pseries_cpuidle_cpu_dead); |
| 472 | WARN_ON(retval < 0); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 473 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Deepthi Dharwar | 12431c6 | 2014-01-14 16:26:18 +0530 | [diff] [blame] | 477 | device_initcall(pseries_processor_idle_init); |