1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/base/power/domain_governor.c - Governors for device PM domains. 4 * 5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. 6 */ 7 #include <linux/kernel.h> 8 #include <linux/pm_domain.h> 9 #include <linux/pm_qos.h> 10 #include <linux/hrtimer.h> 11 #include <linux/cpu.h> 12 #include <linux/cpuidle.h> 13 #include <linux/cpumask.h> 14 #include <linux/ktime.h> 15 16 #include "core.h" 17 18 static int dev_update_qos_constraint(struct device *dev, void *data) 19 { 20 s64 *constraint_ns_p = data; 21 s64 constraint_ns; 22 23 if (dev->power.subsys_data && dev->power.subsys_data->domain_data) { 24 struct gpd_timing_data *td = dev_gpd_data(dev)->td; 25 26 /* 27 * Only take suspend-time QoS constraints of devices into 28 * account, because constraints updated after the device has 29 * been suspended are not guaranteed to be taken into account 30 * anyway. In order for them to take effect, the device has to 31 * be resumed and suspended again. 32 */ 33 constraint_ns = td ? td->effective_constraint_ns : 34 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS; 35 } else { 36 /* 37 * The child is not in a domain and there's no info on its 38 * suspend/resume latencies, so assume them to be negligible and 39 * take its current PM QoS constraint (that's the only thing 40 * known at this point anyway). 41 */ 42 constraint_ns = dev_pm_qos_read_value(dev, DEV_PM_QOS_RESUME_LATENCY); 43 constraint_ns *= NSEC_PER_USEC; 44 } 45 46 if (constraint_ns < *constraint_ns_p) 47 *constraint_ns_p = constraint_ns; 48 49 return 0; 50 } 51 52 /** 53 * default_suspend_ok - Default PM domain governor routine to suspend devices. 54 * @dev: Device to check. 55 * 56 * Returns: true if OK to suspend, false if not OK to suspend 57 */ 58 static bool default_suspend_ok(struct device *dev) 59 { 60 struct gpd_timing_data *td = dev_gpd_data(dev)->td; 61 unsigned long flags; 62 s64 constraint_ns; 63 64 dev_dbg(dev, "%s()\n", __func__); 65 66 spin_lock_irqsave(&dev->power.lock, flags); 67 68 if (!td->constraint_changed) { 69 bool ret = td->cached_suspend_ok; 70 71 spin_unlock_irqrestore(&dev->power.lock, flags); 72 return ret; 73 } 74 td->constraint_changed = false; 75 td->cached_suspend_ok = false; 76 td->effective_constraint_ns = 0; 77 constraint_ns = __dev_pm_qos_resume_latency(dev); 78 79 spin_unlock_irqrestore(&dev->power.lock, flags); 80 81 if (constraint_ns == 0) 82 return false; 83 84 constraint_ns *= NSEC_PER_USEC; 85 /* 86 * We can walk the children without any additional locking, because 87 * they all have been suspended at this point and their 88 * effective_constraint_ns fields won't be modified in parallel with us. 89 */ 90 if (!dev->power.ignore_children) 91 device_for_each_child(dev, &constraint_ns, 92 dev_update_qos_constraint); 93 94 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) { 95 /* "No restriction", so the device is allowed to suspend. */ 96 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS; 97 td->cached_suspend_ok = true; 98 } else if (constraint_ns == 0) { 99 /* 100 * This triggers if one of the children that don't belong to a 101 * domain has a zero PM QoS constraint and it's better not to 102 * suspend then. effective_constraint_ns is zero already and 103 * cached_suspend_ok is false, so bail out. 104 */ 105 return false; 106 } else { 107 constraint_ns -= td->suspend_latency_ns + 108 td->resume_latency_ns; 109 /* 110 * effective_constraint_ns is zero already and cached_suspend_ok 111 * is false, so if the computed value is not positive, return 112 * right away. 113 */ 114 if (constraint_ns <= 0) 115 return false; 116 117 td->effective_constraint_ns = constraint_ns; 118 td->cached_suspend_ok = true; 119 } 120 121 /* 122 * The children have been suspended already, so we don't need to take 123 * their suspend latencies into account here. 124 */ 125 return td->cached_suspend_ok; 126 } 127 128 static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now) 129 { 130 ktime_t domain_wakeup = KTIME_MAX; 131 ktime_t next_wakeup; 132 struct pm_domain_data *pdd; 133 struct gpd_link *link; 134 135 if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY)) 136 return; 137 138 /* 139 * Devices that have a predictable wakeup pattern, may specify 140 * their next wakeup. Let's find the next wakeup from all the 141 * devices attached to this domain and from all the sub-domains. 142 * It is possible that component's a next wakeup may have become 143 * stale when we read that here. We will ignore to ensure the domain 144 * is able to enter its optimal idle state. 145 */ 146 list_for_each_entry(pdd, &genpd->dev_list, list_node) { 147 next_wakeup = to_gpd_data(pdd)->td->next_wakeup; 148 if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now)) 149 if (ktime_before(next_wakeup, domain_wakeup)) 150 domain_wakeup = next_wakeup; 151 } 152 153 list_for_each_entry(link, &genpd->parent_links, parent_node) { 154 struct genpd_governor_data *cgd = link->child->gd; 155 156 next_wakeup = cgd ? cgd->next_wakeup : KTIME_MAX; 157 if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now)) 158 if (ktime_before(next_wakeup, domain_wakeup)) 159 domain_wakeup = next_wakeup; 160 } 161 162 genpd->gd->next_wakeup = domain_wakeup; 163 } 164 165 static bool next_wakeup_allows_state(struct generic_pm_domain *genpd, 166 unsigned int state, ktime_t now) 167 { 168 ktime_t domain_wakeup = genpd->gd->next_wakeup; 169 s64 idle_time_ns, min_sleep_ns; 170 171 min_sleep_ns = genpd->states[state].power_off_latency_ns + 172 genpd->states[state].residency_ns; 173 174 idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now)); 175 176 return idle_time_ns >= min_sleep_ns; 177 } 178 179 static bool __default_power_down_ok(struct dev_pm_domain *pd, 180 unsigned int state) 181 { 182 struct generic_pm_domain *genpd = pd_to_genpd(pd); 183 struct gpd_link *link; 184 struct pm_domain_data *pdd; 185 s64 min_off_time_ns; 186 s64 off_on_time_ns; 187 188 off_on_time_ns = genpd->states[state].power_off_latency_ns + 189 genpd->states[state].power_on_latency_ns; 190 191 min_off_time_ns = -1; 192 /* 193 * Check if subdomains can be off for enough time. 194 * 195 * All subdomains have been powered off already at this point. 196 */ 197 list_for_each_entry(link, &genpd->parent_links, parent_node) { 198 struct genpd_governor_data *cgd = link->child->gd; 199 200 s64 sd_max_off_ns = cgd ? cgd->max_off_time_ns : -1; 201 202 if (sd_max_off_ns < 0) 203 continue; 204 205 /* 206 * Check if the subdomain is allowed to be off long enough for 207 * the current domain to turn off and on (that's how much time 208 * it will have to wait worst case). 209 */ 210 if (sd_max_off_ns <= off_on_time_ns) 211 return false; 212 213 if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0) 214 min_off_time_ns = sd_max_off_ns; 215 } 216 217 /* 218 * Check if the devices in the domain can be off enough time. 219 */ 220 list_for_each_entry(pdd, &genpd->dev_list, list_node) { 221 struct gpd_timing_data *td; 222 s64 constraint_ns; 223 224 /* 225 * Check if the device is allowed to be off long enough for the 226 * domain to turn off and on (that's how much time it will 227 * have to wait worst case). 228 */ 229 td = to_gpd_data(pdd)->td; 230 constraint_ns = td->effective_constraint_ns; 231 /* 232 * Zero means "no suspend at all" and this runs only when all 233 * devices in the domain are suspended, so it must be positive. 234 */ 235 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) 236 continue; 237 238 if (constraint_ns <= off_on_time_ns) 239 return false; 240 241 if (min_off_time_ns > constraint_ns || min_off_time_ns < 0) 242 min_off_time_ns = constraint_ns; 243 } 244 245 /* 246 * If the computed minimum device off time is negative, there are no 247 * latency constraints, so the domain can spend arbitrary time in the 248 * "off" state. 249 */ 250 if (min_off_time_ns < 0) 251 return true; 252 253 /* 254 * The difference between the computed minimum subdomain or device off 255 * time and the time needed to turn the domain on is the maximum 256 * theoretical time this domain can spend in the "off" state. 257 */ 258 genpd->gd->max_off_time_ns = min_off_time_ns - 259 genpd->states[state].power_on_latency_ns; 260 return true; 261 } 262 263 /** 264 * _default_power_down_ok - Default generic PM domain power off governor routine. 265 * @pd: PM domain to check. 266 * @now: current ktime. 267 * 268 * This routine must be executed under the PM domain's lock. 269 * 270 * Returns: true if OK to power down, false if not OK to power down 271 */ 272 static bool _default_power_down_ok(struct dev_pm_domain *pd, ktime_t now) 273 { 274 struct generic_pm_domain *genpd = pd_to_genpd(pd); 275 struct genpd_governor_data *gd = genpd->gd; 276 int state_idx = genpd->state_count - 1; 277 struct gpd_link *link; 278 279 /* 280 * Find the next wakeup from devices that can determine their own wakeup 281 * to find when the domain would wakeup and do it for every device down 282 * the hierarchy. It is not worth while to sleep if the state's residency 283 * cannot be met. 284 */ 285 update_domain_next_wakeup(genpd, now); 286 if ((genpd->flags & GENPD_FLAG_MIN_RESIDENCY) && (gd->next_wakeup != KTIME_MAX)) { 287 /* Let's find out the deepest domain idle state, the devices prefer */ 288 while (state_idx >= 0) { 289 if (next_wakeup_allows_state(genpd, state_idx, now)) { 290 gd->max_off_time_changed = true; 291 break; 292 } 293 state_idx--; 294 } 295 296 if (state_idx < 0) { 297 state_idx = 0; 298 gd->cached_power_down_ok = false; 299 goto done; 300 } 301 } 302 303 if (!gd->max_off_time_changed) { 304 genpd->state_idx = gd->cached_power_down_state_idx; 305 return gd->cached_power_down_ok; 306 } 307 308 /* 309 * We have to invalidate the cached results for the parents, so 310 * use the observation that default_power_down_ok() is not 311 * going to be called for any parent until this instance 312 * returns. 313 */ 314 list_for_each_entry(link, &genpd->child_links, child_node) { 315 struct genpd_governor_data *pgd = link->parent->gd; 316 317 if (pgd) 318 pgd->max_off_time_changed = true; 319 } 320 321 gd->max_off_time_ns = -1; 322 gd->max_off_time_changed = false; 323 gd->cached_power_down_ok = true; 324 325 /* 326 * Find a state to power down to, starting from the state 327 * determined by the next wakeup. 328 */ 329 while (!__default_power_down_ok(pd, state_idx)) { 330 if (state_idx == 0) { 331 gd->cached_power_down_ok = false; 332 break; 333 } 334 state_idx--; 335 } 336 337 done: 338 genpd->state_idx = state_idx; 339 gd->cached_power_down_state_idx = genpd->state_idx; 340 return gd->cached_power_down_ok; 341 } 342 343 static bool default_power_down_ok(struct dev_pm_domain *pd) 344 { 345 return _default_power_down_ok(pd, ktime_get()); 346 } 347 348 #ifdef CONFIG_CPU_IDLE 349 static bool cpu_power_down_ok(struct dev_pm_domain *pd) 350 { 351 struct generic_pm_domain *genpd = pd_to_genpd(pd); 352 struct cpuidle_device *dev; 353 ktime_t domain_wakeup, next_hrtimer; 354 ktime_t now = ktime_get(); 355 struct device *cpu_dev; 356 s64 cpu_constraint, global_constraint, wakeup_constraint; 357 s64 idle_duration_ns; 358 int cpu, i; 359 360 /* Validate dev PM QoS constraints. */ 361 if (!_default_power_down_ok(pd, now)) 362 return false; 363 364 if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN)) 365 return true; 366 367 wakeup_constraint = cpu_wakeup_latency_qos_limit(); 368 global_constraint = cpu_latency_qos_limit(); 369 if (global_constraint > wakeup_constraint) 370 global_constraint = wakeup_constraint; 371 372 /* 373 * Find the next wakeup for any of the online CPUs within the PM domain 374 * and its subdomains. Note, we only need the genpd->cpus, as it already 375 * contains a mask of all CPUs from subdomains. 376 */ 377 domain_wakeup = ktime_set(KTIME_SEC_MAX, 0); 378 for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) { 379 dev = per_cpu(cpuidle_devices, cpu); 380 if (dev) { 381 next_hrtimer = READ_ONCE(dev->next_hrtimer); 382 if (ktime_before(next_hrtimer, domain_wakeup)) 383 domain_wakeup = next_hrtimer; 384 } 385 386 cpu_dev = get_cpu_device(cpu); 387 if (cpu_dev) { 388 cpu_constraint = dev_pm_qos_raw_resume_latency(cpu_dev); 389 if (cpu_constraint < global_constraint) 390 global_constraint = cpu_constraint; 391 } 392 } 393 394 global_constraint *= NSEC_PER_USEC; 395 /* The minimum idle duration is from now - until the next wakeup. */ 396 idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now)); 397 if (idle_duration_ns <= 0) 398 return false; 399 400 /* Store the next domain_wakeup to allow consumers to use it. */ 401 genpd->gd->next_hrtimer = domain_wakeup; 402 403 /* 404 * Find the deepest idle state that has its residency value satisfied 405 * and by also taking into account the power off latency for the state. 406 * Start at the state picked by the dev PM QoS constraint validation. 407 */ 408 i = genpd->state_idx; 409 do { 410 if ((idle_duration_ns >= (genpd->states[i].residency_ns + 411 genpd->states[i].power_off_latency_ns)) && 412 (global_constraint >= (genpd->states[i].power_on_latency_ns + 413 genpd->states[i].power_off_latency_ns))) 414 break; 415 416 } while (--i >= 0); 417 418 if (i < 0) 419 return false; 420 421 if (cpus_peek_for_pending_ipi(genpd->cpus)) 422 return false; 423 424 genpd->state_idx = i; 425 genpd->gd->last_enter = now; 426 genpd->gd->reflect_residency = true; 427 return true; 428 } 429 430 /** 431 * check_device_qos_latency - Callback to check device QoS latency constraints 432 * @dev: Device to check 433 * @data: Pointer to s32 variable holding minimum latency found so far 434 * 435 * This callback checks if the device has a system-wide resume latency QoS 436 * constraint and updates the minimum latency if this device has a stricter 437 * constraint. 438 * 439 * The system-wide flag is checked first so that devices that have not opted 440 * in only incur a single lockless read. 441 * 442 * Returns: 0 to continue iteration. 443 */ 444 static int check_device_qos_latency(struct device *dev, void *data) 445 { 446 s32 *min_dev_latency = data; 447 s32 dev_latency; 448 449 if (!(dev_pm_qos_raw_flags(dev) & PM_QOS_FLAG_LATENCY_SYS)) 450 return 0; 451 452 dev_latency = dev_pm_qos_raw_resume_latency(dev); 453 if (dev_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT) { 454 if (dev_latency < *min_dev_latency) 455 *min_dev_latency = dev_latency; 456 } 457 458 return 0; 459 } 460 461 static bool cpu_system_power_down_ok(struct dev_pm_domain *pd) 462 { 463 s64 constraint_ns = cpu_wakeup_latency_qos_limit() * NSEC_PER_USEC; 464 struct generic_pm_domain *genpd = pd_to_genpd(pd); 465 int state_idx = genpd->state_count - 1; 466 s32 min_dev_latency = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT; 467 s64 min_dev_latency_ns; 468 469 if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN)) { 470 genpd->state_idx = state_idx; 471 return true; 472 } 473 474 genpd_for_each_child(genpd, check_device_qos_latency, 475 &min_dev_latency); 476 477 /* If device latency < CPU wakeup latency, use it instead */ 478 if (min_dev_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT) { 479 min_dev_latency_ns = (s64)min_dev_latency * NSEC_PER_USEC; 480 if (min_dev_latency_ns < constraint_ns) 481 constraint_ns = min_dev_latency_ns; 482 } 483 484 /* Find the deepest state for the latency constraint. */ 485 while (state_idx >= 0) { 486 s64 latency_ns = genpd->states[state_idx].power_off_latency_ns + 487 genpd->states[state_idx].power_on_latency_ns; 488 489 if (latency_ns <= constraint_ns) { 490 genpd->state_idx = state_idx; 491 return true; 492 } 493 state_idx--; 494 } 495 496 return false; 497 } 498 499 struct dev_power_governor pm_domain_cpu_gov = { 500 .suspend_ok = default_suspend_ok, 501 .power_down_ok = cpu_power_down_ok, 502 .system_power_down_ok = cpu_system_power_down_ok, 503 }; 504 #endif 505 506 struct dev_power_governor simple_qos_governor = { 507 .suspend_ok = default_suspend_ok, 508 .power_down_ok = default_power_down_ok, 509 }; 510 511 /* 512 * pm_domain_always_on_gov - A governor implementing an always-on policy 513 */ 514 struct dev_power_governor pm_domain_always_on_gov = { 515 .suspend_ok = default_suspend_ok, 516 }; 517