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