Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powernv: Pass PSSCR value and mask to power9_idle_stop

The power9_idle_stop method currently takes only the requested stop
level as a parameter and picks up the rest of the PSSCR bits from a
hand-coded macro. This is not a very flexible design, especially when
the firmware has the capability to communicate the psscr value and the
mask associated with a particular stop state via device tree.

This patch modifies the power9_idle_stop API to take as parameters the
PSSCR value and the PSSCR mask corresponding to the stop state that
needs to be set. These PSSCR value and mask are respectively obtained
by parsing the "ibm,cpu-idle-state-psscr" and
"ibm,cpu-idle-state-psscr-mask" fields from the device tree.

In addition to this, the patch adds support for handling stop states
for which ESL and EC bits in the PSSCR are zero. As per the
architecture, a wakeup from these stop states resumes execution from
the subsequent instruction as opposed to waking up at the System
Vector.

The older firmware sets only the Requested Level (RL) field in the
psscr and psscr-mask exposed in the device tree. For older firmware
where psscr-mask=0xf, this patch will set the default sane values that
the set for for remaining PSSCR fields (i.e PSLL, MTL, ESL, EC, and
TR). For the new firmware, the patch will validate that the invariants
required by the ISA for the psscr values are maintained by the
firmware.

This skiboot patch that exports fully populated PSSCR values and the
mask for all the stop states can be found here:
https://lists.ozlabs.org/pipermail/skiboot/2016-September/004869.html

[Optimize the number of instructions before entering STOP with
ESL=EC=0, validate the PSSCR values provided by the firimware
maintains the invariants required as per the ISA suggested by Balbir
Singh]

Acked-by: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Gautham R. Shenoy and committed by
Michael Ellerman
09206b60 9e9fc6f0

+241 -43
+44
arch/powerpc/include/asm/cpuidle.h
··· 10 10 #define PNV_CORE_IDLE_LOCK_BIT 0x100 11 11 #define PNV_CORE_IDLE_THREAD_BITS 0x0FF 12 12 13 + /* 14 + * ============================ NOTE ================================= 15 + * The older firmware populates only the RL field in the psscr_val and 16 + * sets the psscr_mask to 0xf. On such a firmware, the kernel sets the 17 + * remaining PSSCR fields to default values as follows: 18 + * 19 + * - ESL and EC bits are to 1. So wakeup from any stop state will be 20 + * at vector 0x100. 21 + * 22 + * - MTL and PSLL are set to the maximum allowed value as per the ISA, 23 + * i.e. 15. 24 + * 25 + * - The Transition Rate, TR is set to the Maximum value 3. 26 + */ 27 + #define PSSCR_HV_DEFAULT_VAL (PSSCR_ESL | PSSCR_EC | \ 28 + PSSCR_PSLL_MASK | PSSCR_TR_MASK | \ 29 + PSSCR_MTL_MASK) 30 + 31 + #define PSSCR_HV_DEFAULT_MASK (PSSCR_ESL | PSSCR_EC | \ 32 + PSSCR_PSLL_MASK | PSSCR_TR_MASK | \ 33 + PSSCR_MTL_MASK | PSSCR_RL_MASK) 34 + #define PSSCR_EC_SHIFT 20 35 + #define PSSCR_ESL_SHIFT 21 36 + #define GET_PSSCR_EC(x) (((x) & PSSCR_EC) >> PSSCR_EC_SHIFT) 37 + #define GET_PSSCR_ESL(x) (((x) & PSSCR_ESL) >> PSSCR_ESL_SHIFT) 38 + #define GET_PSSCR_RL(x) ((x) & PSSCR_RL_MASK) 39 + 40 + #define ERR_EC_ESL_MISMATCH -1 41 + #define ERR_DEEP_STATE_ESL_MISMATCH -2 42 + 13 43 #ifndef __ASSEMBLY__ 14 44 extern u32 pnv_fastsleep_workaround_at_entry[]; 15 45 extern u32 pnv_fastsleep_workaround_at_exit[]; 16 46 17 47 extern u64 pnv_first_deep_stop_state; 48 + 49 + int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags); 50 + static inline void report_invalid_psscr_val(u64 psscr_val, int err) 51 + { 52 + switch (err) { 53 + case ERR_EC_ESL_MISMATCH: 54 + pr_warn("Invalid psscr 0x%016llx : ESL,EC bits unequal", 55 + psscr_val); 56 + break; 57 + case ERR_DEEP_STATE_ESL_MISMATCH: 58 + pr_warn("Invalid psscr 0x%016llx : ESL cleared for deep stop-state", 59 + psscr_val); 60 + } 61 + } 18 62 #endif 19 63 20 64 #endif
+2 -1
arch/powerpc/include/asm/processor.h
··· 454 454 extern unsigned long power7_nap(int check_irq); 455 455 extern unsigned long power7_sleep(void); 456 456 extern unsigned long power7_winkle(void); 457 - extern unsigned long power9_idle_stop(unsigned long stop_level); 457 + extern unsigned long power9_idle_stop(unsigned long stop_psscr_val, 458 + unsigned long stop_psscr_mask); 458 459 459 460 extern void flush_instruction_cache(void); 460 461 extern void hard_reset_now(void);
+19 -11
arch/powerpc/kernel/idle_book3s.S
··· 40 40 #define _WORC GPR11 41 41 #define _PTCR GPR12 42 42 43 - #define PSSCR_HV_TEMPLATE PSSCR_ESL | PSSCR_EC | \ 44 - PSSCR_PSLL_MASK | PSSCR_TR_MASK | \ 45 - PSSCR_MTL_MASK 43 + #define PSSCR_EC_ESL_MASK_SHIFTED (PSSCR_EC | PSSCR_ESL) >> 16 46 44 47 45 .text 48 46 ··· 262 264 IDLE_STATE_ENTER_SEQ_NORET(PPC_WINKLE) 263 265 264 266 /* 265 - * r3 - requested stop state 267 + * r3 - PSSCR value corresponding to the requested stop state. 266 268 */ 267 269 power_enter_stop: 268 270 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE ··· 272 274 stb r4,HSTATE_HWTHREAD_STATE(r13) 273 275 #endif 274 276 /* 277 + * Check if we are executing the lite variant with ESL=EC=0 278 + */ 279 + andis. r4,r3,PSSCR_EC_ESL_MASK_SHIFTED 280 + clrldi r3,r3,60 /* r3 = Bits[60:63] = Requested Level (RL) */ 281 + bne 1f 282 + IDLE_STATE_ENTER_SEQ(PPC_STOP) 283 + li r3,0 /* Since we didn't lose state, return 0 */ 284 + b pnv_wakeup_noloss 285 + /* 275 286 * Check if the requested state is a deep idle state. 276 287 */ 277 - LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state) 288 + 1: LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state) 278 289 ld r4,ADDROFF(pnv_first_deep_stop_state)(r5) 279 290 cmpd r3,r4 280 291 bge 2f ··· 360 353 ld r3,ORIG_GPR3(r1); /* Restore original r3 */ \ 361 354 20: nop; 362 355 363 - 364 356 /* 365 - * r3 - requested stop state 357 + * r3 - The PSSCR value corresponding to the stop state. 358 + * r4 - The PSSCR mask corrresonding to the stop state. 366 359 */ 367 360 _GLOBAL(power9_idle_stop) 368 - LOAD_REG_IMMEDIATE(r4, PSSCR_HV_TEMPLATE) 369 - or r4,r4,r3 370 - mtspr SPRN_PSSCR, r4 371 - li r4, 1 361 + mfspr r5,SPRN_PSSCR 362 + andc r5,r5,r4 363 + or r3,r3,r5 364 + mtspr SPRN_PSSCR,r3 372 365 LOAD_REG_ADDR(r5,power_enter_stop) 366 + li r4,1 373 367 b pnv_powersave_common 374 368 /* No return */ 375 369 /*
+125 -13
arch/powerpc/platforms/powernv/idle.c
··· 237 237 show_fastsleep_workaround_applyonce, 238 238 store_fastsleep_workaround_applyonce); 239 239 240 + /* 241 + * The default stop state that will be used by ppc_md.power_save 242 + * function on platforms that support stop instruction. 243 + */ 244 + u64 pnv_default_stop_val; 245 + u64 pnv_default_stop_mask; 240 246 241 247 /* 242 248 * Used for ppc_md.power_save which needs a function with no parameters 243 249 */ 244 250 static void power9_idle(void) 245 251 { 246 - /* Requesting stop state 0 */ 247 - power9_idle_stop(0); 252 + power9_idle_stop(pnv_default_stop_val, pnv_default_stop_mask); 248 253 } 254 + 249 255 /* 250 256 * First deep stop state. Used to figure out when to save/restore 251 257 * hypervisor context. ··· 259 253 u64 pnv_first_deep_stop_state = MAX_STOP_STATE; 260 254 261 255 /* 262 - * Deepest stop idle state. Used when a cpu is offlined 256 + * psscr value and mask of the deepest stop idle state. 257 + * Used when a cpu is offlined. 263 258 */ 264 - u64 pnv_deepest_stop_state; 259 + u64 pnv_deepest_stop_psscr_val; 260 + u64 pnv_deepest_stop_psscr_mask; 265 261 266 262 /* 267 263 * Power ISA 3.0 idle initialization. ··· 300 292 * Bits 60:63 - Requested Level 301 293 * Used to specify which power-saving level must be entered on executing 302 294 * stop instruction 295 + */ 296 + 297 + int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags) 298 + { 299 + int err = 0; 300 + 301 + /* 302 + * psscr_mask == 0xf indicates an older firmware. 303 + * Set remaining fields of psscr to the default values. 304 + * See NOTE above definition of PSSCR_HV_DEFAULT_VAL 305 + */ 306 + if (*psscr_mask == 0xf) { 307 + *psscr_val = *psscr_val | PSSCR_HV_DEFAULT_VAL; 308 + *psscr_mask = PSSCR_HV_DEFAULT_MASK; 309 + return err; 310 + } 311 + 312 + /* 313 + * New firmware is expected to set the psscr_val bits correctly. 314 + * Validate that the following invariants are correctly maintained by 315 + * the new firmware. 316 + * - ESL bit value matches the EC bit value. 317 + * - ESL bit is set for all the deep stop states. 318 + */ 319 + if (GET_PSSCR_ESL(*psscr_val) != GET_PSSCR_EC(*psscr_val)) { 320 + err = ERR_EC_ESL_MISMATCH; 321 + } else if ((flags & OPAL_PM_LOSE_FULL_CONTEXT) && 322 + GET_PSSCR_ESL(*psscr_val) == 0) { 323 + err = ERR_DEEP_STATE_ESL_MISMATCH; 324 + } 325 + 326 + return err; 327 + } 328 + 329 + /* 330 + * pnv_arch300_idle_init: Initializes the default idle state, first 331 + * deep idle state and deepest idle state on 332 + * ISA 3.0 CPUs. 303 333 * 304 334 * @np: /ibm,opal/power-mgt device node 305 335 * @flags: cpu-idle-state-flags array ··· 348 302 int dt_idle_states) 349 303 { 350 304 u64 *psscr_val = NULL; 305 + u64 *psscr_mask = NULL; 306 + u32 *residency_ns = NULL; 307 + u64 max_residency_ns = 0; 351 308 int rc = 0, i; 309 + bool default_stop_found = false, deepest_stop_found = false; 352 310 353 - psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val), 354 - GFP_KERNEL); 355 - if (!psscr_val) { 311 + psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val), GFP_KERNEL); 312 + psscr_mask = kcalloc(dt_idle_states, sizeof(*psscr_mask), GFP_KERNEL); 313 + residency_ns = kcalloc(dt_idle_states, sizeof(*residency_ns), 314 + GFP_KERNEL); 315 + 316 + if (!psscr_val || !psscr_mask || !residency_ns) { 356 317 rc = -1; 357 318 goto out; 358 319 } 320 + 359 321 if (of_property_read_u64_array(np, 360 322 "ibm,cpu-idle-state-psscr", 361 323 psscr_val, dt_idle_states)) { 362 - pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n"); 324 + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n"); 325 + rc = -1; 326 + goto out; 327 + } 328 + 329 + if (of_property_read_u64_array(np, 330 + "ibm,cpu-idle-state-psscr-mask", 331 + psscr_mask, dt_idle_states)) { 332 + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr-mask in DT\n"); 333 + rc = -1; 334 + goto out; 335 + } 336 + 337 + if (of_property_read_u32_array(np, 338 + "ibm,cpu-idle-state-residency-ns", 339 + residency_ns, dt_idle_states)) { 340 + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-residency-ns in DT\n"); 363 341 rc = -1; 364 342 goto out; 365 343 } 366 344 367 345 /* 368 - * Set pnv_first_deep_stop_state and pnv_deepest_stop_state. 346 + * Set pnv_first_deep_stop_state, pnv_deepest_stop_psscr_{val,mask}, 347 + * and the pnv_default_stop_{val,mask}. 348 + * 369 349 * pnv_first_deep_stop_state should be set to the first stop 370 350 * level to cause hypervisor state loss. 371 - * pnv_deepest_stop_state should be set to the deepest stop 372 - * stop state. 351 + * 352 + * pnv_deepest_stop_{val,mask} should be set to values corresponding to 353 + * the deepest stop state. 354 + * 355 + * pnv_default_stop_{val,mask} should be set to values corresponding to 356 + * the shallowest (OPAL_PM_STOP_INST_FAST) loss-less stop state. 373 357 */ 374 358 pnv_first_deep_stop_state = MAX_STOP_STATE; 375 359 for (i = 0; i < dt_idle_states; i++) { 360 + int err; 376 361 u64 psscr_rl = psscr_val[i] & PSSCR_RL_MASK; 377 362 378 363 if ((flags[i] & OPAL_PM_LOSE_FULL_CONTEXT) && 379 364 (pnv_first_deep_stop_state > psscr_rl)) 380 365 pnv_first_deep_stop_state = psscr_rl; 381 366 382 - if (pnv_deepest_stop_state < psscr_rl) 383 - pnv_deepest_stop_state = psscr_rl; 367 + err = validate_psscr_val_mask(&psscr_val[i], &psscr_mask[i], 368 + flags[i]); 369 + if (err) { 370 + report_invalid_psscr_val(psscr_val[i], err); 371 + continue; 372 + } 373 + 374 + if (max_residency_ns < residency_ns[i]) { 375 + max_residency_ns = residency_ns[i]; 376 + pnv_deepest_stop_psscr_val = psscr_val[i]; 377 + pnv_deepest_stop_psscr_mask = psscr_mask[i]; 378 + deepest_stop_found = true; 379 + } 380 + 381 + if (!default_stop_found && 382 + (flags[i] & OPAL_PM_STOP_INST_FAST)) { 383 + pnv_default_stop_val = psscr_val[i]; 384 + pnv_default_stop_mask = psscr_mask[i]; 385 + default_stop_found = true; 386 + } 387 + } 388 + 389 + if (!default_stop_found) { 390 + pnv_default_stop_val = PSSCR_HV_DEFAULT_VAL; 391 + pnv_default_stop_mask = PSSCR_HV_DEFAULT_MASK; 392 + pr_warn("Setting default stop psscr val=0x%016llx,mask=0x%016llx\n", 393 + pnv_default_stop_val, pnv_default_stop_mask); 394 + } 395 + 396 + if (!deepest_stop_found) { 397 + pnv_deepest_stop_psscr_val = PSSCR_HV_DEFAULT_VAL; 398 + pnv_deepest_stop_psscr_mask = PSSCR_HV_DEFAULT_MASK; 399 + pr_warn("Setting default stop psscr val=0x%016llx,mask=0x%016llx\n", 400 + pnv_deepest_stop_psscr_val, 401 + pnv_deepest_stop_psscr_mask); 384 402 } 385 403 386 404 out: 387 405 kfree(psscr_val); 406 + kfree(psscr_mask); 407 + kfree(residency_ns); 388 408 return rc; 389 409 } 390 410
+2 -1
arch/powerpc/platforms/powernv/powernv.h
··· 18 18 #endif 19 19 20 20 extern u32 pnv_get_supported_cpuidle_states(void); 21 - extern u64 pnv_deepest_stop_state; 21 + extern u64 pnv_deepest_stop_psscr_val; 22 + extern u64 pnv_deepest_stop_psscr_mask; 22 23 23 24 extern void pnv_lpc_init(void); 24 25
+8 -6
arch/powerpc/platforms/powernv/smp.c
··· 182 182 183 183 ppc64_runlatch_off(); 184 184 185 - if (cpu_has_feature(CPU_FTR_ARCH_300)) 186 - srr1 = power9_idle_stop(pnv_deepest_stop_state); 187 - else if (idle_states & OPAL_PM_WINKLE_ENABLED) 185 + if (cpu_has_feature(CPU_FTR_ARCH_300)) { 186 + srr1 = power9_idle_stop(pnv_deepest_stop_psscr_val, 187 + pnv_deepest_stop_psscr_mask); 188 + } else if (idle_states & OPAL_PM_WINKLE_ENABLED) { 188 189 srr1 = power7_winkle(); 189 - else if ((idle_states & OPAL_PM_SLEEP_ENABLED) || 190 - (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) 190 + } else if ((idle_states & OPAL_PM_SLEEP_ENABLED) || 191 + (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) { 191 192 srr1 = power7_sleep(); 192 - else 193 + } else { 193 194 srr1 = power7_nap(1); 195 + } 194 196 195 197 ppc64_runlatch_on(); 196 198
+41 -11
drivers/cpuidle/cpuidle-powernv.c
··· 19 19 #include <asm/firmware.h> 20 20 #include <asm/opal.h> 21 21 #include <asm/runlatch.h> 22 + #include <asm/cpuidle.h> 22 23 23 24 /* 24 25 * Expose only those Hardware idle states via the cpuidle framework ··· 35 34 static int max_idle_state; 36 35 static struct cpuidle_state *cpuidle_state_table; 37 36 38 - static u64 stop_psscr_table[CPUIDLE_STATE_MAX]; 37 + struct stop_psscr_table { 38 + u64 val; 39 + u64 mask; 40 + }; 41 + 42 + static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX]; 39 43 40 44 static u64 snooze_timeout; 41 45 static bool snooze_timeout_en; ··· 112 106 int index) 113 107 { 114 108 ppc64_runlatch_off(); 115 - power9_idle_stop(stop_psscr_table[index]); 109 + power9_idle_stop(stop_psscr_table[index].val, 110 + stop_psscr_table[index].mask); 116 111 ppc64_runlatch_on(); 117 112 return index; 118 113 } ··· 185 178 int), 186 179 unsigned int target_residency, 187 180 unsigned int exit_latency, 188 - u64 psscr_val) 181 + u64 psscr_val, u64 psscr_mask) 189 182 { 190 183 strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN); 191 184 strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN); ··· 193 186 powernv_states[index].target_residency = target_residency; 194 187 powernv_states[index].exit_latency = exit_latency; 195 188 powernv_states[index].enter = idle_fn; 196 - stop_psscr_table[index] = psscr_val; 189 + stop_psscr_table[index].val = psscr_val; 190 + stop_psscr_table[index].mask = psscr_mask; 197 191 } 198 192 199 193 static int powernv_add_idle_states(void) ··· 206 198 u32 residency_ns[CPUIDLE_STATE_MAX]; 207 199 u32 flags[CPUIDLE_STATE_MAX]; 208 200 u64 psscr_val[CPUIDLE_STATE_MAX]; 201 + u64 psscr_mask[CPUIDLE_STATE_MAX]; 209 202 const char *names[CPUIDLE_STATE_MAX]; 203 + u32 has_stop_states = 0; 210 204 int i, rc; 211 205 212 206 /* Currently we have snooze statically defined */ ··· 255 245 256 246 /* 257 247 * If the idle states use stop instruction, probe for psscr values 258 - * which are necessary to specify required stop level. 248 + * and psscr mask which are necessary to specify required stop level. 259 249 */ 260 - if (flags[0] & (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP)) 250 + has_stop_states = (flags[0] & 251 + (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP)); 252 + if (has_stop_states) { 261 253 if (of_property_read_u64_array(power_mgt, 262 254 "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) { 263 - pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n"); 255 + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n"); 264 256 goto out; 265 257 } 258 + 259 + if (of_property_read_u64_array(power_mgt, 260 + "ibm,cpu-idle-state-psscr-mask", 261 + psscr_mask, dt_idle_states)) { 262 + pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n"); 263 + goto out; 264 + } 265 + } 266 266 267 267 rc = of_property_read_u32_array(power_mgt, 268 268 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states); ··· 296 276 else 297 277 target_residency = 0; 298 278 279 + if (has_stop_states) { 280 + int err = validate_psscr_val_mask(&psscr_val[i], 281 + &psscr_mask[i], 282 + flags[i]); 283 + if (err) { 284 + report_invalid_psscr_val(psscr_val[i], err); 285 + continue; 286 + } 287 + } 288 + 299 289 /* 300 290 * For nap and fastsleep, use default target_residency 301 291 * values if f/w does not expose it. ··· 316 286 /* Add NAP state */ 317 287 add_powernv_state(nr_idle_states, "Nap", 318 288 CPUIDLE_FLAG_NONE, nap_loop, 319 - target_residency, exit_latency, 0); 289 + target_residency, exit_latency, 0, 0); 320 290 } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) && 321 291 !(flags[i] & OPAL_PM_TIMEBASE_STOP)) { 322 292 add_powernv_state(nr_idle_states, names[i], 323 293 CPUIDLE_FLAG_NONE, stop_loop, 324 294 target_residency, exit_latency, 325 - psscr_val[i]); 295 + psscr_val[i], psscr_mask[i]); 326 296 } 327 297 328 298 /* ··· 338 308 add_powernv_state(nr_idle_states, "FastSleep", 339 309 CPUIDLE_FLAG_TIMER_STOP, 340 310 fastsleep_loop, 341 - target_residency, exit_latency, 0); 311 + target_residency, exit_latency, 0, 0); 342 312 } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) && 343 313 (flags[i] & OPAL_PM_TIMEBASE_STOP)) { 344 314 add_powernv_state(nr_idle_states, names[i], 345 315 CPUIDLE_FLAG_TIMER_STOP, stop_loop, 346 316 target_residency, exit_latency, 347 - psscr_val[i]); 317 + psscr_val[i], psscr_mask[i]); 348 318 } 349 319 #endif 350 320 nr_idle_states++;