| 1 | |
| 2 | |
| 3 | * drivers/base/power/domain.c - Common code related to device power domains.
|
| 4 | *
|
| 5 | * Copyright (C) 2011 Rafael J. Wysocki <[email protected]>, Renesas Electronics Corp.
|
| 6 |
|
| 7 | #define pr_fmt(fmt) "PM: " fmt
|
| 8 |
|
| 9 | #include <linux/delay.h>
|
| 10 | #include <linux/idr.h>
|
| 11 | #include <linux/kernel.h>
|
| 12 | #include <linux/io.h>
|
| 13 | #include <linux/platform_device.h>
|
| 14 | #include <linux/pm_opp.h>
|
| 15 | #include <linux/pm_runtime.h>
|
| 16 | #include <linux/pm_domain.h>
|
| 17 | #include <linux/pm_qos.h>
|
| 18 | #include <linux/pm_clock.h>
|
| 19 | #include <linux/slab.h>
|
| 20 | #include <linux/err.h>
|
| 21 | #include <linux/sched.h>
|
| 22 | #include <linux/suspend.h>
|
| 23 | #include <linux/export.h>
|
| 24 | #include <linux/cpu.h>
|
| 25 | #include <linux/debugfs.h>
|
| 26 |
|
| 27 |
|
| 28 | static DEFINE_IDA(genpd_ida);
|
| 29 |
|
| 30 |
|
| 31 | static const struct bus_type genpd_provider_bus_type = {
|
| 32 | .name = "genpd_provider",
|
| 33 | };
|
| 34 |
|
| 35 | #define GENPD_RETRY_MAX_MS 250
|
| 36 |
|
| 37 | #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
|
| 38 | ({ \
|
| 39 | type (*__routine)(struct device *__d); \
|
| 40 | type __ret = (type)0; \
|
| 41 | \
|
| 42 | __routine = genpd->dev_ops.callback; \
|
| 43 | if (__routine) { \
|
| 44 | __ret = __routine(dev); \
|
| 45 | } \
|
| 46 | __ret; \
|
| 47 | })
|
| 48 |
|
| 49 | static LIST_HEAD(gpd_list);
|
| 50 | static DEFINE_MUTEX(gpd_list_lock);
|
| 51 |
|
| 52 | struct genpd_lock_ops {
|
| 53 | void (*lock)(struct generic_pm_domain *genpd);
|
| 54 | void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
|
| 55 | int (*lock_interruptible)(struct generic_pm_domain *genpd);
|
| 56 | void (*unlock)(struct generic_pm_domain *genpd);
|
| 57 | };
|
| 58 |
|
| 59 | static void genpd_lock_mtx(struct generic_pm_domain *genpd)
|
| 60 | {
|
| 61 | mutex_lock(&genpd->mlock);
|
| 62 | }
|
| 63 |
|
| 64 | static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
|
| 65 | int depth)
|
| 66 | {
|
| 67 | mutex_lock_nested(&genpd->mlock, depth);
|
| 68 | }
|
| 69 |
|
| 70 | static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
|
| 71 | {
|
| 72 | return mutex_lock_interruptible(&genpd->mlock);
|
| 73 | }
|
| 74 |
|
| 75 | static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
|
| 76 | {
|
| 77 | return mutex_unlock(&genpd->mlock);
|
| 78 | }
|
| 79 |
|
| 80 | static const struct genpd_lock_ops genpd_mtx_ops = {
|
| 81 | .lock = genpd_lock_mtx,
|
| 82 | .lock_nested = genpd_lock_nested_mtx,
|
| 83 | .lock_interruptible = genpd_lock_interruptible_mtx,
|
| 84 | .unlock = genpd_unlock_mtx,
|
| 85 | };
|
| 86 |
|
| 87 | static void genpd_lock_spin(struct generic_pm_domain *genpd)
|
| 88 | __acquires(&genpd->slock)
|
| 89 | {
|
| 90 | unsigned long flags;
|
| 91 |
|
| 92 | spin_lock_irqsave(&genpd->slock, flags);
|
| 93 | genpd->lock_flags = flags;
|
| 94 | }
|
| 95 |
|
| 96 | static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
|
| 97 | int depth)
|
| 98 | __acquires(&genpd->slock)
|
| 99 | {
|
| 100 | unsigned long flags;
|
| 101 |
|
| 102 | spin_lock_irqsave_nested(&genpd->slock, flags, depth);
|
| 103 | genpd->lock_flags = flags;
|
| 104 | }
|
| 105 |
|
| 106 | static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
|
| 107 | __acquires(&genpd->slock)
|
| 108 | {
|
| 109 | unsigned long flags;
|
| 110 |
|
| 111 | spin_lock_irqsave(&genpd->slock, flags);
|
| 112 | genpd->lock_flags = flags;
|
| 113 | return 0;
|
| 114 | }
|
| 115 |
|
| 116 | static void genpd_unlock_spin(struct generic_pm_domain *genpd)
|
| 117 | __releases(&genpd->slock)
|
| 118 | {
|
| 119 | spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
|
| 120 | }
|
| 121 |
|
| 122 | static const struct genpd_lock_ops genpd_spin_ops = {
|
| 123 | .lock = genpd_lock_spin,
|
| 124 | .lock_nested = genpd_lock_nested_spin,
|
| 125 | .lock_interruptible = genpd_lock_interruptible_spin,
|
| 126 | .unlock = genpd_unlock_spin,
|
| 127 | };
|
| 128 |
|
| 129 | static void genpd_lock_raw_spin(struct generic_pm_domain *genpd)
|
| 130 | __acquires(&genpd->raw_slock)
|
| 131 | {
|
| 132 | unsigned long flags;
|
| 133 |
|
| 134 | raw_spin_lock_irqsave(&genpd->raw_slock, flags);
|
| 135 | genpd->raw_lock_flags = flags;
|
| 136 | }
|
| 137 |
|
| 138 | static void genpd_lock_nested_raw_spin(struct generic_pm_domain *genpd,
|
| 139 | int depth)
|
| 140 | __acquires(&genpd->raw_slock)
|
| 141 | {
|
| 142 | unsigned long flags;
|
| 143 |
|
| 144 | raw_spin_lock_irqsave_nested(&genpd->raw_slock, flags, depth);
|
| 145 | genpd->raw_lock_flags = flags;
|
| 146 | }
|
| 147 |
|
| 148 | static int genpd_lock_interruptible_raw_spin(struct generic_pm_domain *genpd)
|
| 149 | __acquires(&genpd->raw_slock)
|
| 150 | {
|
| 151 | unsigned long flags;
|
| 152 |
|
| 153 | raw_spin_lock_irqsave(&genpd->raw_slock, flags);
|
| 154 | genpd->raw_lock_flags = flags;
|
| 155 | return 0;
|
| 156 | }
|
| 157 |
|
| 158 | static void genpd_unlock_raw_spin(struct generic_pm_domain *genpd)
|
| 159 | __releases(&genpd->raw_slock)
|
| 160 | {
|
| 161 | raw_spin_unlock_irqrestore(&genpd->raw_slock, genpd->raw_lock_flags);
|
| 162 | }
|
| 163 |
|
| 164 | static const struct genpd_lock_ops genpd_raw_spin_ops = {
|
| 165 | .lock = genpd_lock_raw_spin,
|
| 166 | .lock_nested = genpd_lock_nested_raw_spin,
|
| 167 | .lock_interruptible = genpd_lock_interruptible_raw_spin,
|
| 168 | .unlock = genpd_unlock_raw_spin,
|
| 169 | };
|
| 170 |
|
| 171 | #define genpd_lock(p) p->lock_ops->lock(p)
|
| 172 | #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
|
| 173 | #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
|
| 174 | #define genpd_unlock(p) p->lock_ops->unlock(p)
|
| 175 |
|
| 176 | #define genpd_status_on(genpd) (genpd->status == GENPD_STATE_ON)
|
| 177 | #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
|
| 178 | #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
|
| 179 | #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
|
| 180 | #define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
|
| 181 | #define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
|
| 182 | #define genpd_is_opp_table_fw(genpd) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
|
| 183 | #define genpd_is_dev_name_fw(genpd) (genpd->flags & GENPD_FLAG_DEV_NAME_FW)
|
| 184 | #define genpd_is_no_sync_state(genpd) (genpd->flags & GENPD_FLAG_NO_SYNC_STATE)
|
| 185 | #define genpd_is_no_stay_on(genpd) (genpd->flags & GENPD_FLAG_NO_STAY_ON)
|
| 186 |
|
| 187 | static inline bool irq_safe_dev_in_sleep_domain(struct device *dev,
|
| 188 | const struct generic_pm_domain *genpd)
|
| 189 | {
|
| 190 | bool ret;
|
| 191 |
|
| 192 | ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
|
| 193 |
|
| 194 | |
| 195 | * Warn once if an IRQ safe device is attached to a domain, which
|
| 196 | * callbacks are allowed to sleep. This indicates a suboptimal
|
| 197 | * configuration for PM, but it doesn't matter for an always on domain.
|
| 198 |
|
| 199 | if (genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd))
|
| 200 | return ret;
|
| 201 |
|
| 202 | if (ret)
|
| 203 | dev_warn_once(dev, "PM domain %s will not be powered off\n",
|
| 204 | dev_name(&genpd->dev));
|
| 205 |
|
| 206 | return ret;
|
| 207 | }
|
| 208 |
|
| 209 | static int genpd_runtime_suspend(struct device *dev);
|
| 210 |
|
| 211 | |
| 212 | * Get the generic PM domain for a particular struct device.
|
| 213 | * This validates the struct device pointer, the PM domain pointer,
|
| 214 | * and checks that the PM domain pointer is a real generic PM domain.
|
| 215 | * Any failure results in NULL being returned.
|
| 216 |
|
| 217 | static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
|
| 218 | {
|
| 219 | if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
|
| 220 | return NULL;
|
| 221 |
|
| 222 |
|
| 223 | if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
|
| 224 | return pd_to_genpd(dev->pm_domain);
|
| 225 |
|
| 226 | return NULL;
|
| 227 | }
|
| 228 |
|
| 229 | |
| 230 | * This should only be used where we are certain that the pm_domain
|
| 231 | * attached to the device is a genpd domain.
|
| 232 |
|
| 233 | static struct generic_pm_domain *dev_to_genpd(struct device *dev)
|
| 234 | {
|
| 235 | if (IS_ERR_OR_NULL(dev->pm_domain))
|
| 236 | return ERR_PTR(-EINVAL);
|
| 237 |
|
| 238 | return pd_to_genpd(dev->pm_domain);
|
| 239 | }
|
| 240 |
|
| 241 | struct device *dev_to_genpd_dev(struct device *dev)
|
| 242 | {
|
| 243 | struct generic_pm_domain *genpd = dev_to_genpd(dev);
|
| 244 |
|
| 245 | if (IS_ERR(genpd))
|
| 246 | return ERR_CAST(genpd);
|
| 247 |
|
| 248 | return &genpd->dev;
|
| 249 | }
|
| 250 |
|
| 251 | static int genpd_stop_dev(const struct generic_pm_domain *genpd,
|
| 252 | struct device *dev)
|
| 253 | {
|
| 254 | return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
|
| 255 | }
|
| 256 |
|
| 257 | static int genpd_start_dev(const struct generic_pm_domain *genpd,
|
| 258 | struct device *dev)
|
| 259 | {
|
| 260 | return GENPD_DEV_CALLBACK(genpd, int, start, dev);
|
| 261 | }
|
| 262 |
|
| 263 | static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
|
| 264 | {
|
| 265 | bool ret = false;
|
| 266 |
|
| 267 | if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
|
| 268 | ret = !!atomic_dec_and_test(&genpd->sd_count);
|
| 269 |
|
| 270 | return ret;
|
| 271 | }
|
| 272 |
|
| 273 | static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
|
| 274 | {
|
| 275 | atomic_inc(&genpd->sd_count);
|
| 276 | smp_mb__after_atomic();
|
| 277 | }
|
| 278 |
|
| 279 | #ifdef CONFIG_DEBUG_FS
|
| 280 | static struct dentry *genpd_debugfs_dir;
|
| 281 |
|
| 282 | static void genpd_debug_add(struct generic_pm_domain *genpd);
|
| 283 |
|
| 284 | static void genpd_debug_remove(struct generic_pm_domain *genpd)
|
| 285 | {
|
| 286 | if (!genpd_debugfs_dir)
|
| 287 | return;
|
| 288 |
|
| 289 | debugfs_lookup_and_remove(dev_name(&genpd->dev), genpd_debugfs_dir);
|
| 290 | }
|
| 291 |
|
| 292 | static void genpd_update_accounting(struct generic_pm_domain *genpd)
|
| 293 | {
|
| 294 | u64 delta, now;
|
| 295 |
|
| 296 | now = ktime_get_mono_fast_ns();
|
| 297 | if (now <= genpd->accounting_time)
|
| 298 | return;
|
| 299 |
|
| 300 | delta = now - genpd->accounting_time;
|
| 301 |
|
| 302 | |
| 303 | * If genpd->status is active, it means we are just
|
| 304 | * out of off and so update the idle time and vice
|
| 305 | * versa.
|
| 306 |
|
| 307 | if (genpd->status == GENPD_STATE_ON)
|
| 308 | genpd->states[genpd->state_idx].idle_time += delta;
|
| 309 | else
|
| 310 | genpd->on_time += delta;
|
| 311 |
|
| 312 | genpd->accounting_time = now;
|
| 313 | }
|
| 314 |
|
| 315 | static void genpd_reflect_residency(struct generic_pm_domain *genpd)
|
| 316 | {
|
| 317 | struct genpd_governor_data *gd = genpd->gd;
|
| 318 | struct genpd_power_state *state, *next_state;
|
| 319 | unsigned int state_idx;
|
| 320 | s64 sleep_ns, target_ns;
|
| 321 |
|
| 322 | if (!gd || !gd->reflect_residency)
|
| 323 | return;
|
| 324 |
|
| 325 | sleep_ns = ktime_to_ns(ktime_sub(ktime_get(), gd->last_enter));
|
| 326 | state_idx = genpd->state_idx;
|
| 327 | state = &genpd->states[state_idx];
|
| 328 | target_ns = state->power_off_latency_ns + state->residency_ns;
|
| 329 |
|
| 330 | if (sleep_ns < target_ns) {
|
| 331 | state->above++;
|
| 332 | } else if (state_idx < (genpd->state_count -1)) {
|
| 333 | next_state = &genpd->states[state_idx + 1];
|
| 334 | target_ns = next_state->power_off_latency_ns +
|
| 335 | next_state->residency_ns;
|
| 336 |
|
| 337 | if (sleep_ns >= target_ns)
|
| 338 | state->below++;
|
| 339 | }
|
| 340 |
|
| 341 | gd->reflect_residency = false;
|
| 342 | }
|
| 343 | #else
|
| 344 | static inline void genpd_debug_add(struct generic_pm_domain *genpd) {}
|
| 345 | static inline void genpd_debug_remove(struct generic_pm_domain *genpd) {}
|
| 346 | static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
|
| 347 | static inline void genpd_reflect_residency(struct generic_pm_domain *genpd) {}
|
| 348 | #endif
|
| 349 |
|
| 350 | static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
|
| 351 | unsigned int state)
|
| 352 | {
|
| 353 | struct generic_pm_domain_data *pd_data;
|
| 354 | struct pm_domain_data *pdd;
|
| 355 | struct gpd_link *link;
|
| 356 |
|
| 357 |
|
| 358 | if (state == genpd->performance_state)
|
| 359 | return state;
|
| 360 |
|
| 361 |
|
| 362 | if (state > genpd->performance_state)
|
| 363 | return state;
|
| 364 |
|
| 365 |
|
| 366 | list_for_each_entry(pdd, &genpd->dev_list, list_node) {
|
| 367 | pd_data = to_gpd_data(pdd);
|
| 368 |
|
| 369 | if (pd_data->performance_state > state)
|
| 370 | state = pd_data->performance_state;
|
| 371 | }
|
| 372 |
|
| 373 | |
| 374 | * Traverse all sub-domains within the domain. This can be
|
| 375 | * done without any additional locking as the link->performance_state
|
| 376 | * field is protected by the parent genpd->lock, which is already taken.
|
| 377 | *
|
| 378 | * Also note that link->performance_state (subdomain's performance state
|
| 379 | * requirement to parent domain) is different from
|
| 380 | * link->child->performance_state (current performance state requirement
|
| 381 | * of the devices/sub-domains of the subdomain) and so can have a
|
| 382 | * different value.
|
| 383 | *
|
| 384 | * Note that we also take vote from powered-off sub-domains into account
|
| 385 | * as the same is done for devices right now.
|
| 386 |
|
| 387 | list_for_each_entry(link, &genpd->parent_links, parent_node) {
|
| 388 | if (link->performance_state > state)
|
| 389 | state = link->performance_state;
|
| 390 | }
|
| 391 |
|
| 392 | return state;
|
| 393 | }
|
| 394 |
|
| 395 | static int genpd_xlate_performance_state(struct generic_pm_domain *genpd,
|
| 396 | struct generic_pm_domain *parent,
|
| 397 | unsigned int pstate)
|
| 398 | {
|
| 399 | if (!parent->set_performance_state)
|
| 400 | return pstate;
|
| 401 |
|
| 402 | return dev_pm_opp_xlate_performance_state(genpd->opp_table,
|
| 403 | parent->opp_table,
|
| 404 | pstate);
|
| 405 | }
|
| 406 |
|
| 407 | static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
|
| 408 | unsigned int state, int depth);
|
| 409 |
|
| 410 | static void _genpd_rollback_parent_state(struct gpd_link *link, int depth)
|
| 411 | {
|
| 412 | struct generic_pm_domain *parent = link->parent;
|
| 413 | int parent_state;
|
| 414 |
|
| 415 | genpd_lock_nested(parent, depth + 1);
|
| 416 |
|
| 417 | parent_state = link->prev_performance_state;
|
| 418 | link->performance_state = parent_state;
|
| 419 |
|
| 420 | parent_state = _genpd_reeval_performance_state(parent, parent_state);
|
| 421 | if (_genpd_set_performance_state(parent, parent_state, depth + 1)) {
|
| 422 | pr_err("%s: Failed to roll back to %d performance state\n",
|
| 423 | parent->name, parent_state);
|
| 424 | }
|
| 425 |
|
| 426 | genpd_unlock(parent);
|
| 427 | }
|
| 428 |
|
| 429 | static int _genpd_set_parent_state(struct generic_pm_domain *genpd,
|
| 430 | struct gpd_link *link,
|
| 431 | unsigned int state, int depth)
|
| 432 | {
|
| 433 | struct generic_pm_domain *parent = link->parent;
|
| 434 | int parent_state, ret;
|
| 435 |
|
| 436 |
|
| 437 | ret = genpd_xlate_performance_state(genpd, parent, state);
|
| 438 | if (unlikely(ret < 0))
|
| 439 | return ret;
|
| 440 |
|
| 441 | parent_state = ret;
|
| 442 |
|
| 443 | genpd_lock_nested(parent, depth + 1);
|
| 444 |
|
| 445 | link->prev_performance_state = link->performance_state;
|
| 446 | link->performance_state = parent_state;
|
| 447 |
|
| 448 | parent_state = _genpd_reeval_performance_state(parent, parent_state);
|
| 449 | ret = _genpd_set_performance_state(parent, parent_state, depth + 1);
|
| 450 | if (ret)
|
| 451 | link->performance_state = link->prev_performance_state;
|
| 452 |
|
| 453 | genpd_unlock(parent);
|
| 454 |
|
| 455 | return ret;
|
| 456 | }
|
| 457 |
|
| 458 | static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
|
| 459 | unsigned int state, int depth)
|
| 460 | {
|
| 461 | struct gpd_link *link = NULL;
|
| 462 | int ret;
|
| 463 |
|
| 464 | if (state == genpd->performance_state)
|
| 465 | return 0;
|
| 466 |
|
| 467 |
|
| 468 | if (state > genpd->performance_state) {
|
| 469 | list_for_each_entry(link, &genpd->child_links, child_node) {
|
| 470 | ret = _genpd_set_parent_state(genpd, link, state, depth);
|
| 471 | if (ret)
|
| 472 | goto rollback_parents_up;
|
| 473 | }
|
| 474 | }
|
| 475 |
|
| 476 | if (genpd->set_performance_state) {
|
| 477 | ret = genpd->set_performance_state(genpd, state);
|
| 478 | if (ret) {
|
| 479 | if (link)
|
| 480 | goto rollback_parents_up;
|
| 481 | return ret;
|
| 482 | }
|
| 483 | }
|
| 484 |
|
| 485 |
|
| 486 | if (state < genpd->performance_state) {
|
| 487 | list_for_each_entry_reverse(link, &genpd->child_links, child_node) {
|
| 488 | ret = _genpd_set_parent_state(genpd, link, state, depth);
|
| 489 | if (ret)
|
| 490 | goto rollback_parents_down;
|
| 491 | }
|
| 492 | }
|
| 493 |
|
| 494 | genpd->performance_state = state;
|
| 495 | return 0;
|
| 496 |
|
| 497 | rollback_parents_up:
|
| 498 | list_for_each_entry_continue_reverse(link, &genpd->child_links, child_node)
|
| 499 | _genpd_rollback_parent_state(link, depth);
|
| 500 | return ret;
|
| 501 | rollback_parents_down:
|
| 502 | list_for_each_entry_continue(link, &genpd->child_links, child_node)
|
| 503 | _genpd_rollback_parent_state(link, depth);
|
| 504 | return ret;
|
| 505 | }
|
| 506 |
|
| 507 | static int genpd_set_performance_state(struct device *dev, unsigned int state)
|
| 508 | {
|
| 509 | struct generic_pm_domain *genpd = dev_to_genpd(dev);
|
| 510 | struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
|
| 511 | unsigned int prev_state;
|
| 512 | int ret;
|
| 513 |
|
| 514 | prev_state = gpd_data->performance_state;
|
| 515 | if (prev_state == state)
|
| 516 | return 0;
|
| 517 |
|
| 518 | gpd_data->performance_state = state;
|
| 519 | state = _genpd_reeval_performance_state(genpd, state);
|
| 520 |
|
| 521 | ret = _genpd_set_performance_state(genpd, state, 0);
|
| 522 | if (ret)
|
| 523 | gpd_data->performance_state = prev_state;
|
| 524 |
|
| 525 | return ret;
|
| 526 | }
|
| 527 |
|
| 528 | static int genpd_drop_performance_state(struct device *dev)
|
| 529 | {
|
| 530 | unsigned int prev_state = dev_gpd_data(dev)->performance_state;
|
| 531 |
|
| 532 | if (!genpd_set_performance_state(dev, 0))
|
| 533 | return prev_state;
|
| 534 |
|
| 535 | return 0;
|
| 536 | }
|
| 537 |
|
| 538 | static void genpd_restore_performance_state(struct device *dev,
|
| 539 | unsigned int state)
|
| 540 | {
|
| 541 | if (state)
|
| 542 | genpd_set_performance_state(dev, state);
|
| 543 | }
|
| 544 |
|
| 545 | static int genpd_dev_pm_set_performance_state(struct device *dev,
|
| 546 | unsigned int state)
|
| 547 | {
|
| 548 | struct generic_pm_domain *genpd = dev_to_genpd(dev);
|
| 549 | int ret = 0;
|
| 550 |
|
| 551 | genpd_lock(genpd);
|
| 552 | if (pm_runtime_suspended(dev)) {
|
| 553 | dev_gpd_data(dev)->rpm_pstate = state;
|
| 554 | } else {
|
| 555 | ret = genpd_set_performance_state(dev, state);
|
| 556 | if (!ret)
|
| 557 | dev_gpd_data(dev)->rpm_pstate = 0;
|
| 558 | }
|
| 559 | genpd_unlock(genpd);
|
| 560 |
|
| 561 | return ret;
|
| 562 | }
|
| 563 |
|
| 564 | |
| 565 | * dev_pm_genpd_set_performance_state- Set performance state of device's power
|
| 566 | * domain.
|
| 567 | *
|
| 568 | * @dev: Device for which the performance-state needs to be set.
|
| 569 | * @state: Target performance state of the device. This can be set as 0 when the
|
| 570 | * device doesn't have any performance state constraints left (And so
|
| 571 | * the device wouldn't participate anymore to find the target
|
| 572 | * performance state of the genpd).
|
| 573 | *
|
| 574 | * It is assumed that the users guarantee that the genpd wouldn't be detached
|
| 575 | * while this routine is getting called.
|
| 576 | *
|
| 577 | * Returns 0 on success and negative error values on failures.
|
| 578 |
|
| 579 | int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
|
| 580 | {
|
| 581 | struct generic_pm_domain *genpd;
|
| 582 |
|
| 583 | genpd = dev_to_genpd_safe(dev);
|
| 584 | if (!genpd)
|
| 585 | return -ENODEV;
|
| 586 |
|
| 587 | if (WARN_ON(!dev->power.subsys_data ||
|
| 588 | !dev->power.subsys_data->domain_data))
|
| 589 | return -EINVAL;
|
| 590 |
|
| 591 | return genpd_dev_pm_set_performance_state(dev, state);
|
| 592 | }
|
| 593 | EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
|
| 594 |
|
| 595 | |
| 596 | * dev_pm_genpd_set_next_wakeup - Notify PM framework of an impending wakeup.
|
| 597 | *
|
| 598 | * @dev: Device to handle
|
| 599 | * @next: impending interrupt/wakeup for the device
|
| 600 | *
|
| 601 | *
|
| 602 | * Allow devices to inform of the next wakeup. It's assumed that the users
|
| 603 | * guarantee that the genpd wouldn't be detached while this routine is getting
|
| 604 | * called. Additionally, it's also assumed that @dev isn't runtime suspended
|
| 605 | * (RPM_SUSPENDED)."
|
| 606 | * Although devices are expected to update the next_wakeup after the end of
|
| 607 | * their usecase as well, it is possible the devices themselves may not know
|
| 608 | * about that, so stale @next will be ignored when powering off the domain.
|
| 609 |
|
| 610 | void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
|
| 611 | {
|
| 612 | struct generic_pm_domain *genpd;
|
| 613 | struct gpd_timing_data *td;
|
| 614 |
|
| 615 | genpd = dev_to_genpd_safe(dev);
|
| 616 | if (!genpd)
|
| 617 | return;
|
| 618 |
|
| 619 | td = to_gpd_data(dev->power.subsys_data->domain_data)->td;
|
| 620 | if (td)
|
| 621 | td->next_wakeup = next;
|
| 622 | }
|
| 623 | EXPORT_SYMBOL_GPL(dev_pm_genpd_set_next_wakeup);
|
| 624 |
|
| 625 | |
| 626 | * dev_pm_genpd_get_next_hrtimer - Return the next_hrtimer for the genpd
|
| 627 | * @dev: A device that is attached to the genpd.
|
| 628 | *
|
| 629 | * This routine should typically be called for a device, at the point of when a
|
| 630 | * GENPD_NOTIFY_PRE_OFF notification has been sent for it.
|
| 631 | *
|
| 632 | * Returns the aggregated value of the genpd's next hrtimer or KTIME_MAX if no
|
| 633 | * valid value have been set.
|
| 634 |
|
| 635 | ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
|
| 636 | {
|
| 637 | struct generic_pm_domain *genpd;
|
| 638 |
|
| 639 | genpd = dev_to_genpd_safe(dev);
|
| 640 | if (!genpd)
|
| 641 | return KTIME_MAX;
|
| 642 |
|
| 643 | if (genpd->gd)
|
| 644 | return genpd->gd->next_hrtimer;
|
| 645 |
|
| 646 | return KTIME_MAX;
|
| 647 | }
|
| 648 | EXPORT_SYMBOL_GPL(dev_pm_genpd_get_next_hrtimer);
|
| 649 |
|
| 650 | |
| 651 | * dev_pm_genpd_synced_poweroff - Next power off should be synchronous
|
| 652 | *
|
| 653 | * @dev: A device that is attached to the genpd.
|
| 654 | *
|
| 655 | * Allows a consumer of the genpd to notify the provider that the next power off
|
| 656 | * should be synchronous.
|
| 657 | *
|
| 658 | * It is assumed that the users guarantee that the genpd wouldn't be detached
|
| 659 | * while this routine is getting called.
|
| 660 |
|
| 661 | void dev_pm_genpd_synced_poweroff(struct device *dev)
|
| 662 | {
|
| 663 | struct generic_pm_domain *genpd;
|
| 664 |
|
| 665 | genpd = dev_to_genpd_safe(dev);
|
| 666 | if (!genpd)
|
| 667 | return;
|
| 668 |
|
| 669 | genpd_lock(genpd);
|
| 670 | genpd->synced_poweroff = true;
|
| 671 | genpd_unlock(genpd);
|
| 672 | }
|
| 673 | EXPORT_SYMBOL_GPL(dev_pm_genpd_synced_poweroff);
|
| 674 |
|
| 675 | |
| 676 | * dev_pm_genpd_set_hwmode() - Set the HW mode for the device and its PM domain.
|
| 677 | *
|
| 678 | * @dev: Device for which the HW-mode should be changed.
|
| 679 | * @enable: Value to set or unset the HW-mode.
|
| 680 | *
|
| 681 | * Some PM domains can rely on HW signals to control the power for a device. To
|
| 682 | * allow a consumer driver to switch the behaviour for its device in runtime,
|
| 683 | * which may be beneficial from a latency or energy point of view, this function
|
| 684 | * may be called.
|
| 685 | *
|
| 686 | * It is assumed that the users guarantee that the genpd wouldn't be detached
|
| 687 | * while this routine is getting called.
|
| 688 | *
|
| 689 | * Return: Returns 0 on success and negative error values on failures.
|
| 690 |
|
| 691 | int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
|
| 692 | {
|
| 693 | struct generic_pm_domain *genpd;
|
| 694 | int ret = 0;
|
| 695 |
|
| 696 | genpd = dev_to_genpd_safe(dev);
|
| 697 | if (!genpd)
|
| 698 | return -ENODEV;
|
| 699 |
|
| 700 | if (!genpd->set_hwmode_dev)
|
| 701 | return -EOPNOTSUPP;
|
| 702 |
|
| 703 | genpd_lock(genpd);
|
| 704 |
|
| 705 | if (dev_gpd_data(dev)->hw_mode == enable)
|
| 706 | goto out;
|
| 707 |
|
| 708 | ret = genpd->set_hwmode_dev(genpd, dev, enable);
|
| 709 | if (!ret)
|
| 710 | dev_gpd_data(dev)->hw_mode = enable;
|
| 711 |
|
| 712 | out:
|
| 713 | genpd_unlock(genpd);
|
| 714 | return ret;
|
| 715 | }
|
| 716 | EXPORT_SYMBOL_GPL(dev_pm_genpd_set_hwmode);
|
| 717 |
|
| 718 | |
| 719 | * dev_pm_genpd_get_hwmode() - Get the HW mode setting for the device.
|
| 720 | *
|
| 721 | * @dev: Device for which the current HW-mode setting should be fetched.
|
| 722 | *
|
| 723 | * This helper function allows consumer drivers to fetch the current HW mode
|
| 724 | * setting of its the device.
|
| 725 | *
|
| 726 | * It is assumed that the users guarantee that the genpd wouldn't be detached
|
| 727 | * while this routine is getting called.
|
| 728 | *
|
| 729 | * Return: Returns the HW mode setting of device from SW cached hw_mode.
|
| 730 |
|
| 731 | bool dev_pm_genpd_get_hwmode(struct device *dev)
|
| 732 | {
|
| 733 | return dev_gpd_data(dev)->hw_mode;
|
| 734 | }
|
| 735 | EXPORT_SYMBOL_GPL(dev_pm_genpd_get_hwmode);
|
| 736 |
|
| 737 | |
| 738 | * dev_pm_genpd_rpm_always_on() - Control if the PM domain can be powered off.
|
| 739 | *
|
| 740 | * @dev: Device for which the PM domain may need to stay on for.
|
| 741 | * @on: Value to set or unset for the condition.
|
| 742 | *
|
| 743 | * For some usecases a consumer driver requires its device to remain power-on
|
| 744 | * from the PM domain perspective during runtime. This function allows the
|
| 745 | * behaviour to be dynamically controlled for a device attached to a genpd.
|
| 746 | *
|
| 747 | * It is assumed that the users guarantee that the genpd wouldn't be detached
|
| 748 | * while this routine is getting called.
|
| 749 | *
|
| 750 | * Return: Returns 0 on success and negative error values on failures.
|
| 751 |
|
| 752 | int dev_pm_genpd_rpm_always_on(struct device *dev, bool on)
|
| 753 | {
|
| 754 | struct generic_pm_domain *genpd;
|
| 755 |
|
| 756 | genpd = dev_to_genpd_safe(dev);
|
| 757 | if (!genpd)
|
| 758 | return -ENODEV;
|
| 759 |
|
| 760 | genpd_lock(genpd);
|
| 761 | dev_gpd_data(dev)->rpm_always_on = on;
|
| 762 | genpd_unlock(genpd);
|
| 763 |
|
| 764 | return 0;
|
| 765 | }
|
| 766 | EXPORT_SYMBOL_GPL(dev_pm_genpd_rpm_always_on);
|
| 767 |
|
| 768 | |
| 769 | * dev_pm_genpd_is_on() - Get device's current power domain status
|
| 770 | *
|
| 771 | * @dev: Device to get the current power status
|
| 772 | *
|
| 773 | * This function checks whether the generic power domain associated with the
|
| 774 | * given device is on or not by verifying if genpd_status_on equals
|
| 775 | * GENPD_STATE_ON.
|
| 776 | *
|
| 777 | * Note: this function returns the power status of the genpd at the time of the
|
| 778 | * call. The power status may change after due to activity from other devices
|
| 779 | * sharing the same genpd. Therefore, this information should not be relied for
|
| 780 | * long-term decisions about the device power state.
|
| 781 | *
|
| 782 | * Return: 'true' if the device's power domain is on, 'false' otherwise.
|
| 783 |
|
| 784 | bool dev_pm_genpd_is_on(struct device *dev)
|
| 785 | {
|
| 786 | struct generic_pm_domain *genpd;
|
| 787 | bool is_on;
|
| 788 |
|
| 789 | genpd = dev_to_genpd_safe(dev);
|
| 790 | if (!genpd)
|
| 791 | return false;
|
| 792 |
|
| 793 | genpd_lock(genpd);
|
| 794 | is_on = genpd_status_on(genpd);
|
| 795 | genpd_unlock(genpd);
|
| 796 |
|
| 797 | return is_on;
|
| 798 | }
|
| 799 | EXPORT_SYMBOL_GPL(dev_pm_genpd_is_on);
|
| 800 |
|
| 801 | |
| 802 | * pm_genpd_inc_rejected() - Adjust the rejected/usage counts for an idle-state.
|
| 803 | *
|
| 804 | * @genpd: The PM domain the idle-state belongs to.
|
| 805 | * @state_idx: The index of the idle-state that failed.
|
| 806 | *
|
| 807 | * In some special cases the ->power_off() callback is asynchronously powering
|
| 808 | * off the PM domain, leading to that it may return zero to indicate success,
|
| 809 | * even though the actual power-off could fail. To account for this correctly in
|
| 810 | * the rejected/usage counts for the idle-state statistics, users can call this
|
| 811 | * function to adjust the values.
|
| 812 | *
|
| 813 | * It is assumed that the users guarantee that the genpd doesn't get removed
|
| 814 | * while this routine is getting called.
|
| 815 |
|
| 816 | void pm_genpd_inc_rejected(struct generic_pm_domain *genpd,
|
| 817 | unsigned int state_idx)
|
| 818 | {
|
| 819 | genpd_lock(genpd);
|
| 820 | genpd->states[genpd->state_idx].rejected++;
|
| 821 | genpd->states[genpd->state_idx].usage--;
|
| 822 | genpd_unlock(genpd);
|
| 823 | }
|
| 824 | EXPORT_SYMBOL_GPL(pm_genpd_inc_rejected);
|
| 825 |
|
| 826 | static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
|
| 827 | {
|
| 828 | unsigned int state_idx = genpd->state_idx;
|
| 829 | ktime_t time_start;
|
| 830 | s64 elapsed_ns;
|
| 831 | int ret;
|
| 832 |
|
| 833 |
|
| 834 | ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
|
| 835 | GENPD_NOTIFY_PRE_ON,
|
| 836 | GENPD_NOTIFY_OFF, NULL);
|
| 837 | ret = notifier_to_errno(ret);
|
| 838 | if (ret)
|
| 839 | return ret;
|
| 840 |
|
| 841 | if (!genpd->power_on)
|
| 842 | goto out;
|
| 843 |
|
| 844 | timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
|
| 845 | if (!timed) {
|
| 846 | ret = genpd->power_on(genpd);
|
| 847 | if (ret)
|
| 848 | goto err;
|
| 849 |
|
| 850 | goto out;
|
| 851 | }
|
| 852 |
|
| 853 | time_start = ktime_get();
|
| 854 | ret = genpd->power_on(genpd);
|
| 855 | if (ret)
|
| 856 | goto err;
|
| 857 |
|
| 858 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
|
| 859 | if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
|
| 860 | goto out;
|
| 861 |
|
| 862 | genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
|
| 863 | genpd->gd->max_off_time_changed = true;
|
| 864 | pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
|
| 865 | dev_name(&genpd->dev), "on", elapsed_ns);
|
| 866 |
|
| 867 | out:
|
| 868 | raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
|
| 869 | genpd->synced_poweroff = false;
|
| 870 | return 0;
|
| 871 | err:
|
| 872 | raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
|
| 873 | NULL);
|
| 874 | return ret;
|
| 875 | }
|
| 876 |
|
| 877 | static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
|
| 878 | {
|
| 879 | unsigned int state_idx = genpd->state_idx;
|
| 880 | ktime_t time_start;
|
| 881 | s64 elapsed_ns;
|
| 882 | int ret;
|
| 883 |
|
| 884 |
|
| 885 | ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
|
| 886 | GENPD_NOTIFY_PRE_OFF,
|
| 887 | GENPD_NOTIFY_ON, NULL);
|
| 888 | ret = notifier_to_errno(ret);
|
| 889 | if (ret)
|
| 890 | return ret;
|
| 891 |
|
| 892 | if (!genpd->power_off)
|
| 893 | goto out;
|
| 894 |
|
| 895 | timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
|
| 896 | if (!timed) {
|
| 897 | ret = genpd->power_off(genpd);
|
| 898 | if (ret)
|
| 899 | goto busy;
|
| 900 |
|
| 901 | goto out;
|
| 902 | }
|
| 903 |
|
| 904 | time_start = ktime_get();
|
| 905 | ret = genpd->power_off(genpd);
|
| 906 | if (ret)
|
| 907 | goto busy;
|
| 908 |
|
| 909 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
|
| 910 | if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
|
| 911 | goto out;
|
| 912 |
|
| 913 | genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
|
| 914 | genpd->gd->max_off_time_changed = true;
|
| 915 | pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
|
| 916 | dev_name(&genpd->dev), "off", elapsed_ns);
|
| 917 |
|
| 918 | out:
|
| 919 | raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
|
| 920 | NULL);
|
| 921 | return 0;
|
| 922 | busy:
|
| 923 | raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
|
| 924 | return ret;
|
| 925 | }
|
| 926 |
|
| 927 | |
| 928 | * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
|
| 929 | * @genpd: PM domain to power off.
|
| 930 | *
|
| 931 | * Queue up the execution of genpd_power_off() unless it's already been done
|
| 932 | * before.
|
| 933 |
|
| 934 | static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
|
| 935 | {
|
| 936 | queue_work(pm_wq, &genpd->power_off_work);
|
| 937 | }
|
| 938 |
|
| 939 | |
| 940 | * genpd_power_off - Remove power from a given PM domain.
|
| 941 | * @genpd: PM domain to power down.
|
| 942 | * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
|
| 943 | * RPM status of the releated device is in an intermediate state, not yet turned
|
| 944 | * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
|
| 945 | * be RPM_SUSPENDED, while it tries to power off the PM domain.
|
| 946 | * @depth: nesting count for lockdep.
|
| 947 | *
|
| 948 | * If all of the @genpd's devices have been suspended and all of its subdomains
|
| 949 | * have been powered down, remove power from @genpd.
|
| 950 |
|
| 951 | static void genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
|
| 952 | unsigned int depth)
|
| 953 | {
|
| 954 | struct pm_domain_data *pdd;
|
| 955 | struct gpd_link *link;
|
| 956 | unsigned int not_suspended = 0;
|
| 957 |
|
| 958 | |
| 959 | * Do not try to power off the domain in the following situations:
|
| 960 | * The domain is already in the "power off" state.
|
| 961 | * System suspend is in progress.
|
| 962 | * The domain is configured as always on.
|
| 963 | * The domain was on at boot and still need to stay on.
|
| 964 | * The domain has a subdomain being powered on.
|
| 965 |
|
| 966 | if (!genpd_status_on(genpd) || genpd->prepared_count > 0 ||
|
| 967 | genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd) ||
|
| 968 | genpd->stay_on || atomic_read(&genpd->sd_count) > 0)
|
| 969 | return;
|
| 970 |
|
| 971 | |
| 972 | * The children must be in their deepest (powered-off) states to allow
|
| 973 | * the parent to be powered off. Note that, there's no need for
|
| 974 | * additional locking, as powering on a child, requires the parent's
|
| 975 | * lock to be acquired first.
|
| 976 |
|
| 977 | list_for_each_entry(link, &genpd->parent_links, parent_node) {
|
| 978 | struct generic_pm_domain *child = link->child;
|
| 979 | if (child->state_idx < child->state_count - 1)
|
| 980 | return;
|
| 981 | }
|
| 982 |
|
| 983 | list_for_each_entry(pdd, &genpd->dev_list, list_node) {
|
| 984 | |
| 985 | * Do not allow PM domain to be powered off, when an IRQ safe
|
| 986 | * device is part of a non-IRQ safe domain.
|
| 987 |
|
| 988 | if (!pm_runtime_suspended(pdd->dev) ||
|
| 989 | irq_safe_dev_in_sleep_domain(pdd->dev, genpd))
|
| 990 | not_suspended++;
|
| 991 |
|
| 992 |
|
| 993 | if (to_gpd_data(pdd)->rpm_always_on)
|
| 994 | return;
|
| 995 | }
|
| 996 |
|
| 997 | if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
|
| 998 | return;
|
| 999 |
|
| 1000 | if (genpd->gov && genpd->gov->power_down_ok) {
|
| 1001 | if (!genpd->gov->power_down_ok(&genpd->domain))
|
| 1002 | return;
|
| 1003 | }
|
| 1004 |
|
| 1005 |
|
| 1006 | if (!genpd->gov)
|
| 1007 | genpd->state_idx = 0;
|
| 1008 |
|
| 1009 |
|
| 1010 | if (atomic_read(&genpd->sd_count) > 0)
|
| 1011 | return;
|
| 1012 |
|
| 1013 | if (_genpd_power_off(genpd, true)) {
|
| 1014 | genpd->states[genpd->state_idx].rejected++;
|
| 1015 | return;
|
| 1016 | }
|
| 1017 |
|
| 1018 | genpd->status = GENPD_STATE_OFF;
|
| 1019 | genpd_update_accounting(genpd);
|
| 1020 | genpd->states[genpd->state_idx].usage++;
|
| 1021 |
|
| 1022 | list_for_each_entry(link, &genpd->child_links, child_node) {
|
| 1023 | genpd_sd_counter_dec(link->parent);
|
| 1024 | genpd_lock_nested(link->parent, depth + 1);
|
| 1025 | genpd_power_off(link->parent, false, depth + 1);
|
| 1026 | genpd_unlock(link->parent);
|
| 1027 | }
|
| 1028 | }
|
| 1029 |
|
| 1030 | |
| 1031 | * genpd_power_on - Restore power to a given PM domain and its parents.
|
| 1032 | * @genpd: PM domain to power up.
|
| 1033 | * @depth: nesting count for lockdep.
|
| 1034 | *
|
| 1035 | * Restore power to @genpd and all of its parents so that it is possible to
|
| 1036 | * resume a device belonging to it.
|
| 1037 |
|
| 1038 | static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
|
| 1039 | {
|
| 1040 | struct gpd_link *link;
|
| 1041 | int ret = 0;
|
| 1042 |
|
| 1043 | if (genpd_status_on(genpd))
|
| 1044 | return 0;
|
| 1045 |
|
| 1046 |
|
| 1047 | genpd_reflect_residency(genpd);
|
| 1048 |
|
| 1049 | |
| 1050 | * The list is guaranteed not to change while the loop below is being
|
| 1051 | * executed, unless one of the parents' .power_on() callbacks fiddles
|
| 1052 | * with it.
|
| 1053 |
|
| 1054 | list_for_each_entry(link, &genpd->child_links, child_node) {
|
| 1055 | struct generic_pm_domain *parent = link->parent;
|
| 1056 |
|
| 1057 | genpd_sd_counter_inc(parent);
|
| 1058 |
|
| 1059 | genpd_lock_nested(parent, depth + 1);
|
| 1060 | ret = genpd_power_on(parent, depth + 1);
|
| 1061 | genpd_unlock(parent);
|
| 1062 |
|
| 1063 | if (ret) {
|
| 1064 | genpd_sd_counter_dec(parent);
|
| 1065 | goto err;
|
| 1066 | }
|
| 1067 | }
|
| 1068 |
|
| 1069 | ret = _genpd_power_on(genpd, true);
|
| 1070 | if (ret)
|
| 1071 | goto err;
|
| 1072 |
|
| 1073 | genpd->status = GENPD_STATE_ON;
|
| 1074 | genpd_update_accounting(genpd);
|
| 1075 |
|
| 1076 | return 0;
|
| 1077 |
|
| 1078 | err:
|
| 1079 | list_for_each_entry_continue_reverse(link,
|
| 1080 | &genpd->child_links,
|
| 1081 | child_node) {
|
| 1082 | genpd_sd_counter_dec(link->parent);
|
| 1083 | genpd_lock_nested(link->parent, depth + 1);
|
| 1084 | genpd_power_off(link->parent, false, depth + 1);
|
| 1085 | genpd_unlock(link->parent);
|
| 1086 | }
|
| 1087 |
|
| 1088 | return ret;
|
| 1089 | }
|
| 1090 |
|
| 1091 | static int genpd_dev_pm_start(struct device *dev)
|
| 1092 | {
|
| 1093 | struct generic_pm_domain *genpd = dev_to_genpd(dev);
|
| 1094 |
|
| 1095 | return genpd_start_dev(genpd, dev);
|
| 1096 | }
|
| 1097 |
|
| 1098 | static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
|
| 1099 | unsigned long val, void *ptr)
|
| 1100 | {
|
| 1101 | struct generic_pm_domain_data *gpd_data;
|
| 1102 | struct device *dev;
|
| 1103 |
|
| 1104 | gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
|
| 1105 | dev = gpd_data->base.dev;
|
| 1106 |
|
| 1107 | for (;;) {
|
| 1108 | struct generic_pm_domain *genpd = ERR_PTR(-ENODATA);
|
| 1109 | struct pm_domain_data *pdd;
|
| 1110 | struct gpd_timing_data *td;
|
| 1111 |
|
| 1112 | spin_lock_irq(&dev->power.lock);
|
| 1113 |
|
| 1114 | pdd = dev->power.subsys_data ?
|
| 1115 | dev->power.subsys_data->domain_data : NULL;
|
| 1116 | if (pdd) {
|
| 1117 | td = to_gpd_data(pdd)->td;
|
| 1118 | if (td) {
|
| 1119 | td->constraint_changed = true;
|
| 1120 | genpd = dev_to_genpd(dev);
|
| 1121 | }
|
| 1122 | }
|
| 1123 |
|
| 1124 | spin_unlock_irq(&dev->power.lock);
|
| 1125 |
|
| 1126 | if (!IS_ERR(genpd)) {
|
| 1127 | genpd_lock(genpd);
|
| 1128 | genpd->gd->max_off_time_changed = true;
|
| 1129 | genpd_unlock(genpd);
|
| 1130 | }
|
| 1131 |
|
| 1132 | dev = dev->parent;
|
| 1133 | if (!dev || dev->power.ignore_children)
|
| 1134 | break;
|
| 1135 | }
|
| 1136 |
|
| 1137 | return NOTIFY_DONE;
|
| 1138 | }
|
| 1139 |
|
| 1140 | |
| 1141 | * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
|
| 1142 | * @work: Work structure used for scheduling the execution of this function.
|
| 1143 |
|
| 1144 | static void genpd_power_off_work_fn(struct work_struct *work)
|
| 1145 | {
|
| 1146 | struct generic_pm_domain *genpd;
|
| 1147 |
|
| 1148 | genpd = container_of(work, struct generic_pm_domain, power_off_work);
|
| 1149 |
|
| 1150 | genpd_lock(genpd);
|
| 1151 | genpd_power_off(genpd, false, 0);
|
| 1152 | genpd_unlock(genpd);
|
| 1153 | }
|
| 1154 |
|
| 1155 | |
| 1156 | * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
|
| 1157 | * @dev: Device to handle.
|
| 1158 |
|
| 1159 | static int __genpd_runtime_suspend(struct device *dev)
|
| 1160 | {
|
| 1161 | int (*cb)(struct device *__dev);
|
| 1162 |
|
| 1163 | if (dev->type && dev->type->pm)
|
| 1164 | cb = dev->type->pm->runtime_suspend;
|
| 1165 | else if (dev->class && dev->class->pm)
|
| 1166 | cb = dev->class->pm->runtime_suspend;
|
| 1167 | else if (dev->bus && dev->bus->pm)
|
| 1168 | cb = dev->bus->pm->runtime_suspend;
|
| 1169 | else
|
| 1170 | cb = NULL;
|
| 1171 |
|
| 1172 | if (!cb && dev->driver && dev->driver->pm)
|
| 1173 | cb = dev->driver->pm->runtime_suspend;
|
| 1174 |
|
| 1175 | return cb ? cb(dev) : 0;
|
| 1176 | }
|
| 1177 |
|
| 1178 | |
| 1179 | * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
|
| 1180 | * @dev: Device to handle.
|
| 1181 |
|
| 1182 | static int __genpd_runtime_resume(struct device *dev)
|
| 1183 | {
|
| 1184 | int (*cb)(struct device *__dev);
|
| 1185 |
|
| 1186 | if (dev->type && dev->type->pm)
|
| 1187 | cb = dev->type->pm->runtime_resume;
|
| 1188 | else if (dev->class && dev->class->pm)
|
| 1189 | cb = dev->class->pm->runtime_resume;
|
| 1190 | else if (dev->bus && dev->bus->pm)
|
| 1191 | cb = dev->bus->pm->runtime_resume;
|
| 1192 | else
|
| 1193 | cb = NULL;
|
| 1194 |
|
| 1195 | if (!cb && dev->driver && dev->driver->pm)
|
| 1196 | cb = dev->driver->pm->runtime_resume;
|
| 1197 |
|
| 1198 | return cb ? cb(dev) : 0;
|
| 1199 | }
|
| 1200 |
|
| 1201 | |
| 1202 | * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
|
| 1203 | * @dev: Device to suspend.
|
| 1204 | *
|
| 1205 | * Carry out a runtime suspend of a device under the assumption that its
|
| 1206 | * pm_domain field points to the domain member of an object of type
|
| 1207 | * struct generic_pm_domain representing a PM domain consisting of I/O devices.
|
| 1208 |
|
| 1209 | static int genpd_runtime_suspend(struct device *dev)
|
| 1210 | {
|
| 1211 | struct generic_pm_domain *genpd;
|
| 1212 | bool (*suspend_ok)(struct device *__dev);
|
| 1213 | struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
|
| 1214 | struct gpd_timing_data *td = gpd_data->td;
|
| 1215 | bool runtime_pm = pm_runtime_enabled(dev);
|
| 1216 | ktime_t time_start = 0;
|
| 1217 | s64 elapsed_ns;
|
| 1218 | int ret;
|
| 1219 |
|
| 1220 | dev_dbg(dev, "%s()\n", __func__);
|
| 1221 |
|
| 1222 | genpd = dev_to_genpd(dev);
|
| 1223 | if (IS_ERR(genpd))
|
| 1224 | return -EINVAL;
|
| 1225 |
|
| 1226 | |
| 1227 | * A runtime PM centric subsystem/driver may re-use the runtime PM
|
| 1228 | * callbacks for other purposes than runtime PM. In those scenarios
|
| 1229 | * runtime PM is disabled. Under these circumstances, we shall skip
|
| 1230 | * validating/measuring the PM QoS latency.
|
| 1231 |
|
| 1232 | suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
|
| 1233 | if (runtime_pm && suspend_ok && !suspend_ok(dev))
|
| 1234 | return -EBUSY;
|
| 1235 |
|
| 1236 |
|
| 1237 | if (td && runtime_pm)
|
| 1238 | time_start = ktime_get();
|
| 1239 |
|
| 1240 | ret = __genpd_runtime_suspend(dev);
|
| 1241 | if (ret)
|
| 1242 | return ret;
|
| 1243 |
|
| 1244 | ret = genpd_stop_dev(genpd, dev);
|
| 1245 | if (ret) {
|
| 1246 | __genpd_runtime_resume(dev);
|
| 1247 | return ret;
|
| 1248 | }
|
| 1249 |
|
| 1250 |
|
| 1251 | if (td && runtime_pm) {
|
| 1252 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
|
| 1253 | if (elapsed_ns > td->suspend_latency_ns) {
|
| 1254 | td->suspend_latency_ns = elapsed_ns;
|
| 1255 | dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
|
| 1256 | elapsed_ns);
|
| 1257 | genpd->gd->max_off_time_changed = true;
|
| 1258 | td->constraint_changed = true;
|
| 1259 | }
|
| 1260 | }
|
| 1261 |
|
| 1262 | |
| 1263 | * If power.irq_safe is set, this routine may be run with
|
| 1264 | * IRQs disabled, so suspend only if the PM domain also is irq_safe.
|
| 1265 |
|
| 1266 | if (irq_safe_dev_in_sleep_domain(dev, genpd))
|
| 1267 | return 0;
|
| 1268 |
|
| 1269 | genpd_lock(genpd);
|
| 1270 | genpd_power_off(genpd, true, 0);
|
| 1271 | gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
|
| 1272 | genpd_unlock(genpd);
|
| 1273 |
|
| 1274 | return 0;
|
| 1275 | }
|
| 1276 |
|
| 1277 | |
| 1278 | * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
|
| 1279 | * @dev: Device to resume.
|
| 1280 | *
|
| 1281 | * Carry out a runtime resume of a device under the assumption that its
|
| 1282 | * pm_domain field points to the domain member of an object of type
|
| 1283 | * struct generic_pm_domain representing a PM domain consisting of I/O devices.
|
| 1284 |
|
| 1285 | static int genpd_runtime_resume(struct device *dev)
|
| 1286 | {
|
| 1287 | struct generic_pm_domain *genpd;
|
| 1288 | struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
|
| 1289 | struct gpd_timing_data *td = gpd_data->td;
|
| 1290 | bool timed = td && pm_runtime_enabled(dev);
|
| 1291 | ktime_t time_start = 0;
|
| 1292 | s64 elapsed_ns;
|
| 1293 | int ret;
|
| 1294 |
|
| 1295 | dev_dbg(dev, "%s()\n", __func__);
|
| 1296 |
|
| 1297 | genpd = dev_to_genpd(dev);
|
| 1298 | if (IS_ERR(genpd))
|
| 1299 | return -EINVAL;
|
| 1300 |
|
| 1301 | |
| 1302 | * As we don't power off a non IRQ safe domain, which holds
|
| 1303 | * an IRQ safe device, we don't need to restore power to it.
|
| 1304 |
|
| 1305 | if (irq_safe_dev_in_sleep_domain(dev, genpd))
|
| 1306 | goto out;
|
| 1307 |
|
| 1308 | genpd_lock(genpd);
|
| 1309 | genpd_restore_performance_state(dev, gpd_data->rpm_pstate);
|
| 1310 | ret = genpd_power_on(genpd, 0);
|
| 1311 | genpd_unlock(genpd);
|
| 1312 |
|
| 1313 | if (ret)
|
| 1314 | return ret;
|
| 1315 |
|
| 1316 | out:
|
| 1317 |
|
| 1318 | if (timed)
|
| 1319 | time_start = ktime_get();
|
| 1320 |
|
| 1321 | ret = genpd_start_dev(genpd, dev);
|
| 1322 | if (ret)
|
| 1323 | goto err_poweroff;
|
| 1324 |
|
| 1325 | ret = __genpd_runtime_resume(dev);
|
| 1326 | if (ret)
|
| 1327 | goto err_stop;
|
| 1328 |
|
| 1329 |
|
| 1330 | if (timed) {
|
| 1331 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
|
| 1332 | if (elapsed_ns > td->resume_latency_ns) {
|
| 1333 | td->resume_latency_ns = elapsed_ns;
|
| 1334 | dev_dbg(dev, "resume latency exceeded, %lld ns\n",
|
| 1335 | elapsed_ns);
|
| 1336 | genpd->gd->max_off_time_changed = true;
|
| 1337 | td->constraint_changed = true;
|
| 1338 | }
|
| 1339 | }
|
| 1340 |
|
| 1341 | return 0;
|
| 1342 |
|
| 1343 | err_stop:
|
| 1344 | genpd_stop_dev(genpd, dev);
|
| 1345 | err_poweroff:
|
| 1346 | if (!pm_runtime_is_irq_safe(dev) || genpd_is_irq_safe(genpd)) {
|
| 1347 | genpd_lock(genpd);
|
| 1348 | genpd_power_off(genpd, true, 0);
|
| 1349 | gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
|
| 1350 | genpd_unlock(genpd);
|
| 1351 | }
|
| 1352 |
|
| 1353 | return ret;
|
| 1354 | }
|
| 1355 |
|
| 1356 | static bool pd_ignore_unused;
|
| 1357 | static int __init pd_ignore_unused_setup(char *__unused)
|
| 1358 | {
|
| 1359 | pd_ignore_unused = true;
|
| 1360 | return 1;
|
| 1361 | }
|
| 1362 | __setup("pd_ignore_unused", pd_ignore_unused_setup);
|
| 1363 |
|
| 1364 | |
| 1365 | * genpd_power_off_unused - Power off all PM domains with no devices in use.
|
| 1366 |
|
| 1367 | static int __init genpd_power_off_unused(void)
|
| 1368 | {
|
| 1369 | struct generic_pm_domain *genpd;
|
| 1370 |
|
| 1371 | if (pd_ignore_unused) {
|
| 1372 | pr_warn("genpd: Not disabling unused power domains\n");
|
| 1373 | return 0;
|
| 1374 | }
|
| 1375 |
|
| 1376 | pr_info("genpd: Disabling unused power domains\n");
|
| 1377 | mutex_lock(&gpd_list_lock);
|
| 1378 |
|
| 1379 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
|
| 1380 | genpd_queue_power_off_work(genpd);
|
| 1381 | }
|
| 1382 |
|
| 1383 | mutex_unlock(&gpd_list_lock);
|
| 1384 |
|
| 1385 | return 0;
|
| 1386 | }
|
| 1387 | late_initcall_sync(genpd_power_off_unused);
|
| 1388 |
|
| 1389 | #ifdef CONFIG_PM_SLEEP
|
| 1390 |
|
| 1391 | |
| 1392 | * genpd_sync_power_off - Synchronously power off a PM domain and its parents.
|
| 1393 | * @genpd: PM domain to power off, if possible.
|
| 1394 | * @use_lock: use the lock.
|
| 1395 | * @depth: nesting count for lockdep.
|
| 1396 | *
|
| 1397 | * Check if the given PM domain can be powered off (during system suspend or
|
| 1398 | * hibernation) and do that if so. Also, in that case propagate to its parents.
|
| 1399 | *
|
| 1400 | * This function is only called in "noirq" and "syscore" stages of system power
|
| 1401 | * transitions. The "noirq" callbacks may be executed asynchronously, thus in
|
| 1402 | * these cases the lock must be held.
|
| 1403 |
|
| 1404 | static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
|
| 1405 | unsigned int depth)
|
| 1406 | {
|
| 1407 | struct gpd_link *link;
|
| 1408 |
|
| 1409 | if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
|
| 1410 | return;
|
| 1411 |
|
| 1412 | if (genpd->suspended_count != genpd->device_count
|
| 1413 | || atomic_read(&genpd->sd_count) > 0)
|
| 1414 | return;
|
| 1415 |
|
| 1416 |
|
| 1417 | list_for_each_entry(link, &genpd->parent_links, parent_node) {
|
| 1418 | struct generic_pm_domain *child = link->child;
|
| 1419 | if (child->state_idx < child->state_count - 1)
|
| 1420 | return;
|
| 1421 | }
|
| 1422 |
|
| 1423 | if (genpd->gov && genpd->gov->system_power_down_ok) {
|
| 1424 | if (!genpd->gov->system_power_down_ok(&genpd->domain))
|
| 1425 | return;
|
| 1426 | } else {
|
| 1427 |
|
| 1428 | genpd->state_idx = genpd->state_count - 1;
|
| 1429 | }
|
| 1430 |
|
| 1431 | if (_genpd_power_off(genpd, false)) {
|
| 1432 | genpd->states[genpd->state_idx].rejected++;
|
| 1433 | return;
|
| 1434 | } else {
|
| 1435 | genpd->states[genpd->state_idx].usage++;
|
| 1436 |
|
| 1437 | |
| 1438 | * The ->system_power_down_ok() callback is currently used only
|
| 1439 | * for s2idle. Use it to know when to update the usage counter.
|
| 1440 |
|
| 1441 | if (genpd->gov && genpd->gov->system_power_down_ok)
|
| 1442 | genpd->states[genpd->state_idx].usage_s2idle++;
|
| 1443 | }
|
| 1444 |
|
| 1445 | genpd->status = GENPD_STATE_OFF;
|
| 1446 |
|
| 1447 | list_for_each_entry(link, &genpd->child_links, child_node) {
|
| 1448 | genpd_sd_counter_dec(link->parent);
|
| 1449 |
|
| 1450 | if (use_lock)
|
| 1451 | genpd_lock_nested(link->parent, depth + 1);
|
| 1452 |
|
| 1453 | genpd_sync_power_off(link->parent, use_lock, depth + 1);
|
| 1454 |
|
| 1455 | if (use_lock)
|
| 1456 | genpd_unlock(link->parent);
|
| 1457 | }
|
| 1458 | }
|
| 1459 |
|
| 1460 | |
| 1461 | * genpd_sync_power_on - Synchronously power on a PM domain and its parents.
|
| 1462 | * @genpd: PM domain to power on.
|
| 1463 | * @use_lock: use the lock.
|
| 1464 | * @depth: nesting count for lockdep.
|
| 1465 | *
|
| 1466 | * This function is only called in "noirq" and "syscore" stages of system power
|
| 1467 | * transitions. The "noirq" callbacks may be executed asynchronously, thus in
|
| 1468 | * these cases the lock must be held.
|
| 1469 |
|
| 1470 | static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
|
| 1471 | unsigned int depth)
|
| 1472 | {
|
| 1473 | struct gpd_link *link;
|
| 1474 |
|
| 1475 | if (genpd_status_on(genpd))
|
| 1476 | return;
|
| 1477 |
|
| 1478 | list_for_each_entry(link, &genpd->child_links, child_node) {
|
| 1479 | genpd_sd_counter_inc(link->parent);
|
| 1480 |
|
| 1481 | if (use_lock)
|
| 1482 | genpd_lock_nested(link->parent, depth + 1);
|
| 1483 |
|
| 1484 | genpd_sync_power_on(link->parent, use_lock, depth + 1);
|
| 1485 |
|
| 1486 | if (use_lock)
|
| 1487 | genpd_unlock(link->parent);
|
| 1488 | }
|
| 1489 |
|
| 1490 | _genpd_power_on(genpd, false);
|
| 1491 | genpd->status = GENPD_STATE_ON;
|
| 1492 | }
|
| 1493 |
|
| 1494 | |
| 1495 | * genpd_prepare - Start power transition of a device in a PM domain.
|
| 1496 | * @dev: Device to start the transition of.
|
| 1497 | *
|
| 1498 | * Start a power transition of a device (during a system-wide power transition)
|
| 1499 | * under the assumption that its pm_domain field points to the domain member of
|
| 1500 | * an object of type struct generic_pm_domain representing a PM domain
|
| 1501 | * consisting of I/O devices.
|
| 1502 |
|
| 1503 | static int genpd_prepare(struct device *dev)
|
| 1504 | {
|
| 1505 | struct generic_pm_domain *genpd;
|
| 1506 | int ret;
|
| 1507 |
|
| 1508 | dev_dbg(dev, "%s()\n", __func__);
|
| 1509 |
|
| 1510 | genpd = dev_to_genpd(dev);
|
| 1511 | if (IS_ERR(genpd))
|
| 1512 | return -EINVAL;
|
| 1513 |
|
| 1514 | genpd_lock(genpd);
|
| 1515 | genpd->prepared_count++;
|
| 1516 | genpd_unlock(genpd);
|
| 1517 |
|
| 1518 | ret = pm_generic_prepare(dev);
|
| 1519 | if (ret < 0) {
|
| 1520 | genpd_lock(genpd);
|
| 1521 |
|
| 1522 | genpd->prepared_count--;
|
| 1523 |
|
| 1524 | genpd_unlock(genpd);
|
| 1525 | }
|
| 1526 |
|
| 1527 |
|
| 1528 | return ret >= 0 ? 0 : ret;
|
| 1529 | }
|
| 1530 |
|
| 1531 | |
| 1532 | * genpd_finish_suspend - Completion of suspend or hibernation of device in an
|
| 1533 | * I/O pm domain.
|
| 1534 | * @dev: Device to suspend.
|
| 1535 | * @suspend_noirq: Generic suspend_noirq callback.
|
| 1536 | * @resume_noirq: Generic resume_noirq callback.
|
| 1537 | *
|
| 1538 | * Stop the device and remove power from the domain if all devices in it have
|
| 1539 | * been stopped.
|
| 1540 |
|
| 1541 | static int genpd_finish_suspend(struct device *dev,
|
| 1542 | int (*suspend_noirq)(struct device *dev),
|
| 1543 | int (*resume_noirq)(struct device *dev))
|
| 1544 | {
|
| 1545 | struct generic_pm_domain *genpd;
|
| 1546 | int ret = 0;
|
| 1547 |
|
| 1548 | genpd = dev_to_genpd(dev);
|
| 1549 | if (IS_ERR(genpd))
|
| 1550 | return -EINVAL;
|
| 1551 |
|
| 1552 | ret = suspend_noirq(dev);
|
| 1553 | if (ret)
|
| 1554 | return ret;
|
| 1555 |
|
| 1556 | if (device_awake_path(dev) && genpd_is_active_wakeup(genpd) &&
|
| 1557 | !device_out_band_wakeup(dev))
|
| 1558 | return 0;
|
| 1559 |
|
| 1560 | if (genpd->dev_ops.stop && genpd->dev_ops.start &&
|
| 1561 | !pm_runtime_status_suspended(dev)) {
|
| 1562 | ret = genpd_stop_dev(genpd, dev);
|
| 1563 | if (ret) {
|
| 1564 | resume_noirq(dev);
|
| 1565 | return ret;
|
| 1566 | }
|
| 1567 | }
|
| 1568 |
|
| 1569 | genpd_lock(genpd);
|
| 1570 | genpd->suspended_count++;
|
| 1571 | genpd_sync_power_off(genpd, true, 0);
|
| 1572 | genpd_unlock(genpd);
|
| 1573 |
|
| 1574 | return 0;
|
| 1575 | }
|
| 1576 |
|
| 1577 | |
| 1578 | * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
|
| 1579 | * @dev: Device to suspend.
|
| 1580 | *
|
| 1581 | * Stop the device and remove power from the domain if all devices in it have
|
| 1582 | * been stopped.
|
| 1583 |
|
| 1584 | static int genpd_suspend_noirq(struct device *dev)
|
| 1585 | {
|
| 1586 | dev_dbg(dev, "%s()\n", __func__);
|
| 1587 |
|
| 1588 | return genpd_finish_suspend(dev,
|
| 1589 | pm_generic_suspend_noirq,
|
| 1590 | pm_generic_resume_noirq);
|
| 1591 | }
|
| 1592 |
|
| 1593 | |
| 1594 | * genpd_finish_resume - Completion of resume of device in an I/O PM domain.
|
| 1595 | * @dev: Device to resume.
|
| 1596 | * @resume_noirq: Generic resume_noirq callback.
|
| 1597 | *
|
| 1598 | * Restore power to the device's PM domain, if necessary, and start the device.
|
| 1599 |
|
| 1600 | static int genpd_finish_resume(struct device *dev,
|
| 1601 | int (*resume_noirq)(struct device *dev))
|
| 1602 | {
|
| 1603 | struct generic_pm_domain *genpd;
|
| 1604 | int ret;
|
| 1605 |
|
| 1606 | dev_dbg(dev, "%s()\n", __func__);
|
| 1607 |
|
| 1608 | genpd = dev_to_genpd(dev);
|
| 1609 | if (IS_ERR(genpd))
|
| 1610 | return -EINVAL;
|
| 1611 |
|
| 1612 | if (device_awake_path(dev) && genpd_is_active_wakeup(genpd) &&
|
| 1613 | !device_out_band_wakeup(dev))
|
| 1614 | return resume_noirq(dev);
|
| 1615 |
|
| 1616 | genpd_lock(genpd);
|
| 1617 | genpd_sync_power_on(genpd, true, 0);
|
| 1618 | genpd->suspended_count--;
|
| 1619 | genpd_unlock(genpd);
|
| 1620 |
|
| 1621 | if (genpd->dev_ops.stop && genpd->dev_ops.start &&
|
| 1622 | !pm_runtime_status_suspended(dev)) {
|
| 1623 | ret = genpd_start_dev(genpd, dev);
|
| 1624 | if (ret)
|
| 1625 | return ret;
|
| 1626 | }
|
| 1627 |
|
| 1628 | return pm_generic_resume_noirq(dev);
|
| 1629 | }
|
| 1630 |
|
| 1631 | |
| 1632 | * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
|
| 1633 | * @dev: Device to resume.
|
| 1634 | *
|
| 1635 | * Restore power to the device's PM domain, if necessary, and start the device.
|
| 1636 |
|
| 1637 | static int genpd_resume_noirq(struct device *dev)
|
| 1638 | {
|
| 1639 | dev_dbg(dev, "%s()\n", __func__);
|
| 1640 |
|
| 1641 | return genpd_finish_resume(dev, pm_generic_resume_noirq);
|
| 1642 | }
|
| 1643 |
|
| 1644 | |
| 1645 | * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
|
| 1646 | * @dev: Device to freeze.
|
| 1647 | *
|
| 1648 | * Carry out a late freeze of a device under the assumption that its
|
| 1649 | * pm_domain field points to the domain member of an object of type
|
| 1650 | * struct generic_pm_domain representing a power domain consisting of I/O
|
| 1651 | * devices.
|
| 1652 |
|
| 1653 | static int genpd_freeze_noirq(struct device *dev)
|
| 1654 | {
|
| 1655 | dev_dbg(dev, "%s()\n", __func__);
|
| 1656 |
|
| 1657 | return genpd_finish_suspend(dev,
|
| 1658 | pm_generic_freeze_noirq,
|
| 1659 | pm_generic_thaw_noirq);
|
| 1660 | }
|
| 1661 |
|
| 1662 | |
| 1663 | * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
|
| 1664 | * @dev: Device to thaw.
|
| 1665 | *
|
| 1666 | * Start the device, unless power has been removed from the domain already
|
| 1667 | * before the system transition.
|
| 1668 |
|
| 1669 | static int genpd_thaw_noirq(struct device *dev)
|
| 1670 | {
|
| 1671 | dev_dbg(dev, "%s()\n", __func__);
|
| 1672 |
|
| 1673 | return genpd_finish_resume(dev, pm_generic_thaw_noirq);
|
| 1674 | }
|
| 1675 |
|
| 1676 | |
| 1677 | * genpd_poweroff_noirq - Completion of hibernation of device in an
|
| 1678 | * I/O PM domain.
|
| 1679 | * @dev: Device to poweroff.
|
| 1680 | *
|
| 1681 | * Stop the device and remove power from the domain if all devices in it have
|
| 1682 | * been stopped.
|
| 1683 |
|
| 1684 | static int genpd_poweroff_noirq(struct device *dev)
|
| 1685 | {
|
| 1686 | dev_dbg(dev, "%s()\n", __func__);
|
| 1687 |
|
| 1688 | return genpd_finish_suspend(dev,
|
| 1689 | pm_generic_poweroff_noirq,
|
| 1690 | pm_generic_restore_noirq);
|
| 1691 | }
|
| 1692 |
|
| 1693 | |
| 1694 | * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
|
| 1695 | * @dev: Device to resume.
|
| 1696 | *
|
| 1697 | * Make sure the domain will be in the same power state as before the
|
| 1698 | * hibernation the system is resuming from and start the device if necessary.
|
| 1699 |
|
| 1700 | static int genpd_restore_noirq(struct device *dev)
|
| 1701 | {
|
| 1702 | dev_dbg(dev, "%s()\n", __func__);
|
| 1703 |
|
| 1704 | return genpd_finish_resume(dev, pm_generic_restore_noirq);
|
| 1705 | }
|
| 1706 |
|
| 1707 | |
| 1708 | * genpd_complete - Complete power transition of a device in a power domain.
|
| 1709 | * @dev: Device to complete the transition of.
|
| 1710 | *
|
| 1711 | * Complete a power transition of a device (during a system-wide power
|
| 1712 | * transition) under the assumption that its pm_domain field points to the
|
| 1713 | * domain member of an object of type struct generic_pm_domain representing
|
| 1714 | * a power domain consisting of I/O devices.
|
| 1715 |
|
| 1716 | static void genpd_complete(struct device *dev)
|
| 1717 | {
|
| 1718 | struct generic_pm_domain *genpd;
|
| 1719 |
|
| 1720 | dev_dbg(dev, "%s()\n", __func__);
|
| 1721 |
|
| 1722 | genpd = dev_to_genpd(dev);
|
| 1723 | if (IS_ERR(genpd))
|
| 1724 | return;
|
| 1725 |
|
| 1726 | pm_generic_complete(dev);
|
| 1727 |
|
| 1728 | genpd_lock(genpd);
|
| 1729 |
|
| 1730 | genpd->prepared_count--;
|
| 1731 | if (!genpd->prepared_count)
|
| 1732 | genpd_queue_power_off_work(genpd);
|
| 1733 |
|
| 1734 | genpd_unlock(genpd);
|
| 1735 | }
|
| 1736 |
|
| 1737 | static void genpd_switch_state(struct device *dev, bool suspend)
|
| 1738 | {
|
| 1739 | struct generic_pm_domain *genpd;
|
| 1740 | bool use_lock;
|
| 1741 |
|
| 1742 | genpd = dev_to_genpd_safe(dev);
|
| 1743 | if (!genpd)
|
| 1744 | return;
|
| 1745 |
|
| 1746 | use_lock = genpd_is_irq_safe(genpd);
|
| 1747 |
|
| 1748 | if (use_lock)
|
| 1749 | genpd_lock(genpd);
|
| 1750 |
|
| 1751 | if (suspend) {
|
| 1752 | genpd->suspended_count++;
|
| 1753 | genpd_sync_power_off(genpd, use_lock, 0);
|
| 1754 | } else {
|
| 1755 | genpd_sync_power_on(genpd, use_lock, 0);
|
| 1756 | genpd->suspended_count--;
|
| 1757 | }
|
| 1758 |
|
| 1759 | if (use_lock)
|
| 1760 | genpd_unlock(genpd);
|
| 1761 | }
|
| 1762 |
|
| 1763 | |
| 1764 | * dev_pm_genpd_suspend - Synchronously try to suspend the genpd for @dev
|
| 1765 | * @dev: The device that is attached to the genpd, that can be suspended.
|
| 1766 | *
|
| 1767 | * This routine should typically be called for a device that needs to be
|
| 1768 | * suspended during the syscore suspend phase. It may also be called during
|
| 1769 | * suspend-to-idle to suspend a corresponding CPU device that is attached to a
|
| 1770 | * genpd.
|
| 1771 |
|
| 1772 | void dev_pm_genpd_suspend(struct device *dev)
|
| 1773 | {
|
| 1774 | genpd_switch_state(dev, true);
|
| 1775 | }
|
| 1776 | EXPORT_SYMBOL_GPL(dev_pm_genpd_suspend);
|
| 1777 |
|
| 1778 | |
| 1779 | * dev_pm_genpd_resume - Synchronously try to resume the genpd for @dev
|
| 1780 | * @dev: The device that is attached to the genpd, which needs to be resumed.
|
| 1781 | *
|
| 1782 | * This routine should typically be called for a device that needs to be resumed
|
| 1783 | * during the syscore resume phase. It may also be called during suspend-to-idle
|
| 1784 | * to resume a corresponding CPU device that is attached to a genpd.
|
| 1785 |
|
| 1786 | void dev_pm_genpd_resume(struct device *dev)
|
| 1787 | {
|
| 1788 | genpd_switch_state(dev, false);
|
| 1789 | }
|
| 1790 | EXPORT_SYMBOL_GPL(dev_pm_genpd_resume);
|
| 1791 |
|
| 1792 | #else
|
| 1793 |
|
| 1794 | #define genpd_prepare NULL
|
| 1795 | #define genpd_suspend_noirq NULL
|
| 1796 | #define genpd_resume_noirq NULL
|
| 1797 | #define genpd_freeze_noirq NULL
|
| 1798 | #define genpd_thaw_noirq NULL
|
| 1799 | #define genpd_poweroff_noirq NULL
|
| 1800 | #define genpd_restore_noirq NULL
|
| 1801 | #define genpd_complete NULL
|
| 1802 |
|
| 1803 | #endif
|
| 1804 |
|
| 1805 | static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
|
| 1806 | bool has_governor)
|
| 1807 | {
|
| 1808 | struct generic_pm_domain_data *gpd_data;
|
| 1809 | struct gpd_timing_data *td;
|
| 1810 | int ret;
|
| 1811 |
|
| 1812 | ret = dev_pm_get_subsys_data(dev);
|
| 1813 | if (ret)
|
| 1814 | return ERR_PTR(ret);
|
| 1815 |
|
| 1816 | gpd_data = kzalloc_obj(*gpd_data);
|
| 1817 | if (!gpd_data) {
|
| 1818 | ret = -ENOMEM;
|
| 1819 | goto err_put;
|
| 1820 | }
|
| 1821 |
|
| 1822 | gpd_data->base.dev = dev;
|
| 1823 | gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
|
| 1824 |
|
| 1825 |
|
| 1826 | if (has_governor) {
|
| 1827 | td = kzalloc_obj(*td);
|
| 1828 | if (!td) {
|
| 1829 | ret = -ENOMEM;
|
| 1830 | goto err_free;
|
| 1831 | }
|
| 1832 |
|
| 1833 | td->constraint_changed = true;
|
| 1834 | td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
|
| 1835 | td->next_wakeup = KTIME_MAX;
|
| 1836 | gpd_data->td = td;
|
| 1837 | }
|
| 1838 |
|
| 1839 | spin_lock_irq(&dev->power.lock);
|
| 1840 |
|
| 1841 | if (dev->power.subsys_data->domain_data)
|
| 1842 | ret = -EINVAL;
|
| 1843 | else
|
| 1844 | dev->power.subsys_data->domain_data = &gpd_data->base;
|
| 1845 |
|
| 1846 | spin_unlock_irq(&dev->power.lock);
|
| 1847 |
|
| 1848 | if (ret)
|
| 1849 | goto err_free;
|
| 1850 |
|
| 1851 | return gpd_data;
|
| 1852 |
|
| 1853 | err_free:
|
| 1854 | kfree(gpd_data->td);
|
| 1855 | kfree(gpd_data);
|
| 1856 | err_put:
|
| 1857 | dev_pm_put_subsys_data(dev);
|
| 1858 | return ERR_PTR(ret);
|
| 1859 | }
|
| 1860 |
|
| 1861 | static void genpd_free_dev_data(struct device *dev,
|
| 1862 | struct generic_pm_domain_data *gpd_data)
|
| 1863 | {
|
| 1864 | spin_lock_irq(&dev->power.lock);
|
| 1865 |
|
| 1866 | dev->power.subsys_data->domain_data = NULL;
|
| 1867 |
|
| 1868 | spin_unlock_irq(&dev->power.lock);
|
| 1869 |
|
| 1870 | dev_pm_opp_clear_config(gpd_data->opp_token);
|
| 1871 | kfree(gpd_data->td);
|
| 1872 | kfree(gpd_data);
|
| 1873 | dev_pm_put_subsys_data(dev);
|
| 1874 | }
|
| 1875 |
|
| 1876 | static void genpd_update_cpumask(struct generic_pm_domain *genpd,
|
| 1877 | int cpu, bool set, unsigned int depth)
|
| 1878 | {
|
| 1879 | struct gpd_link *link;
|
| 1880 |
|
| 1881 | if (!genpd_is_cpu_domain(genpd))
|
| 1882 | return;
|
| 1883 |
|
| 1884 | list_for_each_entry(link, &genpd->child_links, child_node) {
|
| 1885 | struct generic_pm_domain *parent = link->parent;
|
| 1886 |
|
| 1887 | genpd_lock_nested(parent, depth + 1);
|
| 1888 | genpd_update_cpumask(parent, cpu, set, depth + 1);
|
| 1889 | genpd_unlock(parent);
|
| 1890 | }
|
| 1891 |
|
| 1892 | if (set)
|
| 1893 | cpumask_set_cpu(cpu, genpd->cpus);
|
| 1894 | else
|
| 1895 | cpumask_clear_cpu(cpu, genpd->cpus);
|
| 1896 | }
|
| 1897 |
|
| 1898 | static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
|
| 1899 | {
|
| 1900 | if (cpu >= 0)
|
| 1901 | genpd_update_cpumask(genpd, cpu, true, 0);
|
| 1902 | }
|
| 1903 |
|
| 1904 | static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
|
| 1905 | {
|
| 1906 | if (cpu >= 0)
|
| 1907 | genpd_update_cpumask(genpd, cpu, false, 0);
|
| 1908 | }
|
| 1909 |
|
| 1910 | static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
|
| 1911 | {
|
| 1912 | int cpu;
|
| 1913 |
|
| 1914 | if (!genpd_is_cpu_domain(genpd))
|
| 1915 | return -1;
|
| 1916 |
|
| 1917 | for_each_possible_cpu(cpu) {
|
| 1918 | if (get_cpu_device(cpu) == dev)
|
| 1919 | return cpu;
|
| 1920 | }
|
| 1921 |
|
| 1922 | return -1;
|
| 1923 | }
|
| 1924 |
|
| 1925 | static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
|
| 1926 | struct device *base_dev)
|
| 1927 | {
|
| 1928 | struct genpd_governor_data *gd = genpd->gd;
|
| 1929 | struct generic_pm_domain_data *gpd_data;
|
| 1930 | int ret;
|
| 1931 |
|
| 1932 | dev_dbg(dev, "%s()\n", __func__);
|
| 1933 |
|
| 1934 | gpd_data = genpd_alloc_dev_data(dev, gd);
|
| 1935 | if (IS_ERR(gpd_data))
|
| 1936 | return PTR_ERR(gpd_data);
|
| 1937 |
|
| 1938 | gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
|
| 1939 |
|
| 1940 | gpd_data->hw_mode = genpd->get_hwmode_dev ? genpd->get_hwmode_dev(genpd, dev) : false;
|
| 1941 |
|
| 1942 | ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
|
| 1943 | if (ret)
|
| 1944 | goto out;
|
| 1945 |
|
| 1946 | genpd_lock(genpd);
|
| 1947 |
|
| 1948 | genpd_set_cpumask(genpd, gpd_data->cpu);
|
| 1949 |
|
| 1950 | genpd->device_count++;
|
| 1951 | if (gd)
|
| 1952 | gd->max_off_time_changed = true;
|
| 1953 |
|
| 1954 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
|
| 1955 |
|
| 1956 | genpd_unlock(genpd);
|
| 1957 | dev_pm_domain_set(dev, &genpd->domain);
|
| 1958 | out:
|
| 1959 | if (ret)
|
| 1960 | genpd_free_dev_data(dev, gpd_data);
|
| 1961 | else
|
| 1962 | dev_pm_qos_add_notifier(dev, &gpd_data->nb,
|
| 1963 | DEV_PM_QOS_RESUME_LATENCY);
|
| 1964 |
|
| 1965 | return ret;
|
| 1966 | }
|
| 1967 |
|
| 1968 | |
| 1969 | * pm_genpd_add_device - Add a device to an I/O PM domain.
|
| 1970 | * @genpd: PM domain to add the device to.
|
| 1971 | * @dev: Device to be added.
|
| 1972 |
|
| 1973 | int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
|
| 1974 | {
|
| 1975 | int ret;
|
| 1976 |
|
| 1977 | if (!genpd || !dev)
|
| 1978 | return -EINVAL;
|
| 1979 |
|
| 1980 | mutex_lock(&gpd_list_lock);
|
| 1981 | ret = genpd_add_device(genpd, dev, dev);
|
| 1982 | mutex_unlock(&gpd_list_lock);
|
| 1983 |
|
| 1984 | return ret;
|
| 1985 | }
|
| 1986 | EXPORT_SYMBOL_GPL(pm_genpd_add_device);
|
| 1987 |
|
| 1988 | static int genpd_remove_device(struct generic_pm_domain *genpd,
|
| 1989 | struct device *dev)
|
| 1990 | {
|
| 1991 | struct generic_pm_domain_data *gpd_data;
|
| 1992 | struct pm_domain_data *pdd;
|
| 1993 | int ret = 0;
|
| 1994 |
|
| 1995 | dev_dbg(dev, "%s()\n", __func__);
|
| 1996 |
|
| 1997 | pdd = dev->power.subsys_data->domain_data;
|
| 1998 | gpd_data = to_gpd_data(pdd);
|
| 1999 | dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
|
| 2000 | DEV_PM_QOS_RESUME_LATENCY);
|
| 2001 |
|
| 2002 | genpd_lock(genpd);
|
| 2003 |
|
| 2004 | if (genpd->prepared_count > 0) {
|
| 2005 | ret = -EAGAIN;
|
| 2006 | goto out;
|
| 2007 | }
|
| 2008 |
|
| 2009 | genpd->device_count--;
|
| 2010 | if (genpd->gd)
|
| 2011 | genpd->gd->max_off_time_changed = true;
|
| 2012 |
|
| 2013 | genpd_clear_cpumask(genpd, gpd_data->cpu);
|
| 2014 |
|
| 2015 | list_del_init(&pdd->list_node);
|
| 2016 |
|
| 2017 | genpd_unlock(genpd);
|
| 2018 |
|
| 2019 | dev_pm_domain_set(dev, NULL);
|
| 2020 |
|
| 2021 | if (genpd->detach_dev)
|
| 2022 | genpd->detach_dev(genpd, dev);
|
| 2023 |
|
| 2024 | genpd_free_dev_data(dev, gpd_data);
|
| 2025 |
|
| 2026 | return 0;
|
| 2027 |
|
| 2028 | out:
|
| 2029 | genpd_unlock(genpd);
|
| 2030 | dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);
|
| 2031 |
|
| 2032 | return ret;
|
| 2033 | }
|
| 2034 |
|
| 2035 | |
| 2036 | * pm_genpd_remove_device - Remove a device from an I/O PM domain.
|
| 2037 | * @dev: Device to be removed.
|
| 2038 |
|
| 2039 | int pm_genpd_remove_device(struct device *dev)
|
| 2040 | {
|
| 2041 | struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);
|
| 2042 |
|
| 2043 | if (!genpd)
|
| 2044 | return -EINVAL;
|
| 2045 |
|
| 2046 | return genpd_remove_device(genpd, dev);
|
| 2047 | }
|
| 2048 | EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
|
| 2049 |
|
| 2050 | |
| 2051 | * dev_pm_genpd_add_notifier - Add a genpd power on/off notifier for @dev
|
| 2052 | *
|
| 2053 | * @dev: Device that should be associated with the notifier
|
| 2054 | * @nb: The notifier block to register
|
| 2055 | *
|
| 2056 | * Users may call this function to add a genpd power on/off notifier for an
|
| 2057 | * attached @dev. Only one notifier per device is allowed. The notifier is
|
| 2058 | * sent when genpd is powering on/off the PM domain.
|
| 2059 | *
|
| 2060 | * It is assumed that the user guarantee that the genpd wouldn't be detached
|
| 2061 | * while this routine is getting called.
|
| 2062 | *
|
| 2063 | * Returns 0 on success and negative error values on failures.
|
| 2064 |
|
| 2065 | int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb)
|
| 2066 | {
|
| 2067 | struct generic_pm_domain *genpd;
|
| 2068 | struct generic_pm_domain_data *gpd_data;
|
| 2069 | int ret;
|
| 2070 |
|
| 2071 | genpd = dev_to_genpd_safe(dev);
|
| 2072 | if (!genpd)
|
| 2073 | return -ENODEV;
|
| 2074 |
|
| 2075 | if (WARN_ON(!dev->power.subsys_data ||
|
| 2076 | !dev->power.subsys_data->domain_data))
|
| 2077 | return -EINVAL;
|
| 2078 |
|
| 2079 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
|
| 2080 | if (gpd_data->power_nb)
|
| 2081 | return -EEXIST;
|
| 2082 |
|
| 2083 | genpd_lock(genpd);
|
| 2084 | ret = raw_notifier_chain_register(&genpd->power_notifiers, nb);
|
| 2085 | genpd_unlock(genpd);
|
| 2086 |
|
| 2087 | if (ret) {
|
| 2088 | dev_warn(dev, "failed to add notifier for PM domain %s\n",
|
| 2089 | dev_name(&genpd->dev));
|
| 2090 | return ret;
|
| 2091 | }
|
| 2092 |
|
| 2093 | gpd_data->power_nb = nb;
|
| 2094 | return 0;
|
| 2095 | }
|
| 2096 | EXPORT_SYMBOL_GPL(dev_pm_genpd_add_notifier);
|
| 2097 |
|
| 2098 | |
| 2099 | * dev_pm_genpd_remove_notifier - Remove a genpd power on/off notifier for @dev
|
| 2100 | *
|
| 2101 | * @dev: Device that is associated with the notifier
|
| 2102 | *
|
| 2103 | * Users may call this function to remove a genpd power on/off notifier for an
|
| 2104 | * attached @dev.
|
| 2105 | *
|
| 2106 | * It is assumed that the user guarantee that the genpd wouldn't be detached
|
| 2107 | * while this routine is getting called.
|
| 2108 | *
|
| 2109 | * Returns 0 on success and negative error values on failures.
|
| 2110 |
|
| 2111 | int dev_pm_genpd_remove_notifier(struct device *dev)
|
| 2112 | {
|
| 2113 | struct generic_pm_domain *genpd;
|
| 2114 | struct generic_pm_domain_data *gpd_data;
|
| 2115 | int ret;
|
| 2116 |
|
| 2117 | genpd = dev_to_genpd_safe(dev);
|
| 2118 | if (!genpd)
|
| 2119 | return -ENODEV;
|
| 2120 |
|
| 2121 | if (WARN_ON(!dev->power.subsys_data ||
|
| 2122 | !dev->power.subsys_data->domain_data))
|
| 2123 | return -EINVAL;
|
| 2124 |
|
| 2125 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
|
| 2126 | if (!gpd_data->power_nb)
|
| 2127 | return -ENODEV;
|
| 2128 |
|
| 2129 | genpd_lock(genpd);
|
| 2130 | ret = raw_notifier_chain_unregister(&genpd->power_notifiers,
|
| 2131 | gpd_data->power_nb);
|
| 2132 | genpd_unlock(genpd);
|
| 2133 |
|
| 2134 | if (ret) {
|
| 2135 | dev_warn(dev, "failed to remove notifier for PM domain %s\n",
|
| 2136 | dev_name(&genpd->dev));
|
| 2137 | return ret;
|
| 2138 | }
|
| 2139 |
|
| 2140 | gpd_data->power_nb = NULL;
|
| 2141 | return 0;
|
| 2142 | }
|
| 2143 | EXPORT_SYMBOL_GPL(dev_pm_genpd_remove_notifier);
|
| 2144 |
|
| 2145 | static int genpd_add_subdomain(struct generic_pm_domain *genpd,
|
| 2146 | struct generic_pm_domain *subdomain)
|
| 2147 | {
|
| 2148 | struct gpd_link *link, *itr;
|
| 2149 | int ret = 0;
|
| 2150 |
|
| 2151 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
|
| 2152 | || genpd == subdomain)
|
| 2153 | return -EINVAL;
|
| 2154 |
|
| 2155 | |
| 2156 | * If the domain can be powered on/off in an IRQ safe
|
| 2157 | * context, ensure that the subdomain can also be
|
| 2158 | * powered on/off in that context.
|
| 2159 |
|
| 2160 | if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
|
| 2161 | WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
|
| 2162 | dev_name(&genpd->dev), subdomain->name);
|
| 2163 | return -EINVAL;
|
| 2164 | }
|
| 2165 |
|
| 2166 | link = kzalloc_obj(*link);
|
| 2167 | if (!link)
|
| 2168 | return -ENOMEM;
|
| 2169 |
|
| 2170 | genpd_lock(subdomain);
|
| 2171 | genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
|
| 2172 |
|
| 2173 | if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
|
| 2174 | ret = -EINVAL;
|
| 2175 | goto out;
|
| 2176 | }
|
| 2177 |
|
| 2178 | list_for_each_entry(itr, &genpd->parent_links, parent_node) {
|
| 2179 | if (itr->child == subdomain && itr->parent == genpd) {
|
| 2180 | ret = -EINVAL;
|
| 2181 | goto out;
|
| 2182 | }
|
| 2183 | }
|
| 2184 |
|
| 2185 | link->parent = genpd;
|
| 2186 | list_add_tail(&link->parent_node, &genpd->parent_links);
|
| 2187 | link->child = subdomain;
|
| 2188 | list_add_tail(&link->child_node, &subdomain->child_links);
|
| 2189 | if (genpd_status_on(subdomain))
|
| 2190 | genpd_sd_counter_inc(genpd);
|
| 2191 |
|
| 2192 | out:
|
| 2193 | genpd_unlock(genpd);
|
| 2194 | genpd_unlock(subdomain);
|
| 2195 | if (ret)
|
| 2196 | kfree(link);
|
| 2197 | return ret;
|
| 2198 | }
|
| 2199 |
|
| 2200 | |
| 2201 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
|
| 2202 | * @genpd: Leader PM domain to add the subdomain to.
|
| 2203 | * @subdomain: Subdomain to be added.
|
| 2204 |
|
| 2205 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
|
| 2206 | struct generic_pm_domain *subdomain)
|
| 2207 | {
|
| 2208 | int ret;
|
| 2209 |
|
| 2210 | mutex_lock(&gpd_list_lock);
|
| 2211 | ret = genpd_add_subdomain(genpd, subdomain);
|
| 2212 | mutex_unlock(&gpd_list_lock);
|
| 2213 |
|
| 2214 | return ret;
|
| 2215 | }
|
| 2216 | EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
|
| 2217 |
|
| 2218 | |
| 2219 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
|
| 2220 | * @genpd: Leader PM domain to remove the subdomain from.
|
| 2221 | * @subdomain: Subdomain to be removed.
|
| 2222 |
|
| 2223 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
|
| 2224 | struct generic_pm_domain *subdomain)
|
| 2225 | {
|
| 2226 | struct gpd_link *l, *link;
|
| 2227 | int ret = -EINVAL;
|
| 2228 |
|
| 2229 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
|
| 2230 | return -EINVAL;
|
| 2231 |
|
| 2232 | genpd_lock(subdomain);
|
| 2233 | genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
|
| 2234 |
|
| 2235 | if (!list_empty(&subdomain->parent_links) || subdomain->device_count) {
|
| 2236 | pr_warn("%s: unable to remove subdomain %s\n",
|
| 2237 | dev_name(&genpd->dev), subdomain->name);
|
| 2238 | ret = -EBUSY;
|
| 2239 | goto out;
|
| 2240 | }
|
| 2241 |
|
| 2242 | list_for_each_entry_safe(link, l, &genpd->parent_links, parent_node) {
|
| 2243 | if (link->child != subdomain)
|
| 2244 | continue;
|
| 2245 |
|
| 2246 | list_del(&link->parent_node);
|
| 2247 | list_del(&link->child_node);
|
| 2248 | kfree(link);
|
| 2249 | if (genpd_status_on(subdomain))
|
| 2250 | genpd_sd_counter_dec(genpd);
|
| 2251 |
|
| 2252 | ret = 0;
|
| 2253 | break;
|
| 2254 | }
|
| 2255 |
|
| 2256 | out:
|
| 2257 | genpd_unlock(genpd);
|
| 2258 | genpd_unlock(subdomain);
|
| 2259 |
|
| 2260 | return ret;
|
| 2261 | }
|
| 2262 | EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
|
| 2263 |
|
| 2264 | static void genpd_free_default_power_state(struct genpd_power_state *states,
|
| 2265 | unsigned int state_count)
|
| 2266 | {
|
| 2267 | kfree(states);
|
| 2268 | }
|
| 2269 |
|
| 2270 | static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
|
| 2271 | {
|
| 2272 | struct genpd_power_state *state;
|
| 2273 |
|
| 2274 | state = kzalloc_obj(*state);
|
| 2275 | if (!state)
|
| 2276 | return -ENOMEM;
|
| 2277 |
|
| 2278 | genpd->states = state;
|
| 2279 | genpd->state_count = 1;
|
| 2280 | genpd->free_states = genpd_free_default_power_state;
|
| 2281 |
|
| 2282 | return 0;
|
| 2283 | }
|
| 2284 |
|
| 2285 | static void genpd_provider_release(struct device *dev)
|
| 2286 | {
|
| 2287 |
|
| 2288 | }
|
| 2289 |
|
| 2290 | static int genpd_alloc_data(struct generic_pm_domain *genpd)
|
| 2291 | {
|
| 2292 | struct genpd_governor_data *gd = NULL;
|
| 2293 | int ret;
|
| 2294 |
|
| 2295 | if (genpd_is_cpu_domain(genpd) &&
|
| 2296 | !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
|
| 2297 | return -ENOMEM;
|
| 2298 |
|
| 2299 | if (genpd->gov) {
|
| 2300 | gd = kzalloc_obj(*gd);
|
| 2301 | if (!gd) {
|
| 2302 | ret = -ENOMEM;
|
| 2303 | goto free;
|
| 2304 | }
|
| 2305 |
|
| 2306 | gd->max_off_time_ns = -1;
|
| 2307 | gd->max_off_time_changed = true;
|
| 2308 | gd->next_wakeup = KTIME_MAX;
|
| 2309 | gd->next_hrtimer = KTIME_MAX;
|
| 2310 | }
|
| 2311 |
|
| 2312 |
|
| 2313 | if (genpd->state_count == 0) {
|
| 2314 | ret = genpd_set_default_power_state(genpd);
|
| 2315 | if (ret)
|
| 2316 | goto free;
|
| 2317 | }
|
| 2318 |
|
| 2319 | genpd->gd = gd;
|
| 2320 | device_initialize(&genpd->dev);
|
| 2321 | genpd->dev.release = genpd_provider_release;
|
| 2322 | genpd->dev.bus = &genpd_provider_bus_type;
|
| 2323 |
|
| 2324 | if (!genpd_is_dev_name_fw(genpd)) {
|
| 2325 | dev_set_name(&genpd->dev, "%s", genpd->name);
|
| 2326 | } else {
|
| 2327 | ret = ida_alloc(&genpd_ida, GFP_KERNEL);
|
| 2328 | if (ret < 0)
|
| 2329 | goto put;
|
| 2330 |
|
| 2331 | genpd->device_id = ret;
|
| 2332 | dev_set_name(&genpd->dev, "%s_%u", genpd->name, genpd->device_id);
|
| 2333 | }
|
| 2334 |
|
| 2335 | return 0;
|
| 2336 | put:
|
| 2337 | put_device(&genpd->dev);
|
| 2338 | if (genpd->free_states == genpd_free_default_power_state) {
|
| 2339 | kfree(genpd->states);
|
| 2340 | genpd->states = NULL;
|
| 2341 | }
|
| 2342 | free:
|
| 2343 | if (genpd_is_cpu_domain(genpd))
|
| 2344 | free_cpumask_var(genpd->cpus);
|
| 2345 | kfree(gd);
|
| 2346 | return ret;
|
| 2347 | }
|
| 2348 |
|
| 2349 | static void genpd_free_data(struct generic_pm_domain *genpd)
|
| 2350 | {
|
| 2351 | put_device(&genpd->dev);
|
| 2352 | if (genpd->device_id != -ENXIO)
|
| 2353 | ida_free(&genpd_ida, genpd->device_id);
|
| 2354 | if (genpd_is_cpu_domain(genpd))
|
| 2355 | free_cpumask_var(genpd->cpus);
|
| 2356 | if (genpd->free_states)
|
| 2357 | genpd->free_states(genpd->states, genpd->state_count);
|
| 2358 | kfree(genpd->gd);
|
| 2359 | }
|
| 2360 |
|
| 2361 | static void genpd_lock_init(struct generic_pm_domain *genpd)
|
| 2362 | {
|
| 2363 | if (genpd_is_cpu_domain(genpd)) {
|
| 2364 | raw_spin_lock_init(&genpd->raw_slock);
|
| 2365 | genpd->lock_ops = &genpd_raw_spin_ops;
|
| 2366 | } else if (genpd_is_irq_safe(genpd)) {
|
| 2367 | spin_lock_init(&genpd->slock);
|
| 2368 | genpd->lock_ops = &genpd_spin_ops;
|
| 2369 | } else {
|
| 2370 | mutex_init(&genpd->mlock);
|
| 2371 | genpd->lock_ops = &genpd_mtx_ops;
|
| 2372 | }
|
| 2373 | }
|
| 2374 |
|
| 2375 | #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
|
| 2376 | static void genpd_set_stay_on(struct generic_pm_domain *genpd, bool is_off)
|
| 2377 | {
|
| 2378 | genpd->stay_on = !genpd_is_no_stay_on(genpd) && !is_off;
|
| 2379 | }
|
| 2380 | #else
|
| 2381 | static void genpd_set_stay_on(struct generic_pm_domain *genpd, bool is_off)
|
| 2382 | {
|
| 2383 | genpd->stay_on = false;
|
| 2384 | }
|
| 2385 | #endif
|
| 2386 |
|
| 2387 | |
| 2388 | * pm_genpd_init - Initialize a generic I/O PM domain object.
|
| 2389 | * @genpd: PM domain object to initialize.
|
| 2390 | * @gov: PM domain governor to associate with the domain (may be NULL).
|
| 2391 | * @is_off: Initial value of the domain's power_is_off field.
|
| 2392 | *
|
| 2393 | * Returns 0 on successful initialization, else a negative error code.
|
| 2394 |
|
| 2395 | int pm_genpd_init(struct generic_pm_domain *genpd,
|
| 2396 | struct dev_power_governor *gov, bool is_off)
|
| 2397 | {
|
| 2398 | int ret;
|
| 2399 |
|
| 2400 | if (IS_ERR_OR_NULL(genpd))
|
| 2401 | return -EINVAL;
|
| 2402 |
|
| 2403 | INIT_LIST_HEAD(&genpd->parent_links);
|
| 2404 | INIT_LIST_HEAD(&genpd->child_links);
|
| 2405 | INIT_LIST_HEAD(&genpd->dev_list);
|
| 2406 | RAW_INIT_NOTIFIER_HEAD(&genpd->power_notifiers);
|
| 2407 | genpd_lock_init(genpd);
|
| 2408 | genpd->gov = gov;
|
| 2409 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
|
| 2410 | atomic_set(&genpd->sd_count, 0);
|
| 2411 | genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
|
| 2412 | genpd_set_stay_on(genpd, is_off);
|
| 2413 | genpd->sync_state = GENPD_SYNC_STATE_OFF;
|
| 2414 | genpd->device_count = 0;
|
| 2415 | genpd->provider = NULL;
|
| 2416 | genpd->device_id = -ENXIO;
|
| 2417 | genpd->has_provider = false;
|
| 2418 | genpd->opp_table = NULL;
|
| 2419 | genpd->accounting_time = ktime_get_mono_fast_ns();
|
| 2420 | genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
|
| 2421 | genpd->domain.ops.runtime_resume = genpd_runtime_resume;
|
| 2422 | genpd->domain.ops.prepare = genpd_prepare;
|
| 2423 | genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
|
| 2424 | genpd->domain.ops.resume_noirq = genpd_resume_noirq;
|
| 2425 | genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
|
| 2426 | genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
|
| 2427 | genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
|
| 2428 | genpd->domain.ops.restore_noirq = genpd_restore_noirq;
|
| 2429 | genpd->domain.ops.complete = genpd_complete;
|
| 2430 | genpd->domain.start = genpd_dev_pm_start;
|
| 2431 | genpd->domain.set_performance_state = genpd_dev_pm_set_performance_state;
|
| 2432 |
|
| 2433 | if (genpd->flags & GENPD_FLAG_PM_CLK) {
|
| 2434 | genpd->dev_ops.stop = pm_clk_suspend;
|
| 2435 | genpd->dev_ops.start = pm_clk_resume;
|
| 2436 | }
|
| 2437 |
|
| 2438 |
|
| 2439 | if (gov == &pm_domain_always_on_gov)
|
| 2440 | genpd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;
|
| 2441 |
|
| 2442 |
|
| 2443 | if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
|
| 2444 | !genpd_status_on(genpd)) {
|
| 2445 | pr_err("always-on PM domain %s is not on\n", genpd->name);
|
| 2446 | return -EINVAL;
|
| 2447 | }
|
| 2448 |
|
| 2449 |
|
| 2450 | if (!gov && genpd->state_count > 1)
|
| 2451 | pr_warn("%s: no governor for states\n", genpd->name);
|
| 2452 |
|
| 2453 | ret = genpd_alloc_data(genpd);
|
| 2454 | if (ret)
|
| 2455 | return ret;
|
| 2456 |
|
| 2457 | mutex_lock(&gpd_list_lock);
|
| 2458 | list_add(&genpd->gpd_list_node, &gpd_list);
|
| 2459 | mutex_unlock(&gpd_list_lock);
|
| 2460 | genpd_debug_add(genpd);
|
| 2461 |
|
| 2462 | return 0;
|
| 2463 | }
|
| 2464 | EXPORT_SYMBOL_GPL(pm_genpd_init);
|
| 2465 |
|
| 2466 | static int genpd_remove(struct generic_pm_domain *genpd)
|
| 2467 | {
|
| 2468 | struct gpd_link *l, *link;
|
| 2469 |
|
| 2470 | if (IS_ERR_OR_NULL(genpd))
|
| 2471 | return -EINVAL;
|
| 2472 |
|
| 2473 | genpd_lock(genpd);
|
| 2474 |
|
| 2475 | if (genpd->has_provider) {
|
| 2476 | genpd_unlock(genpd);
|
| 2477 | pr_err("Provider present, unable to remove %s\n", dev_name(&genpd->dev));
|
| 2478 | return -EBUSY;
|
| 2479 | }
|
| 2480 |
|
| 2481 | if (!list_empty(&genpd->parent_links) || genpd->device_count) {
|
| 2482 | genpd_unlock(genpd);
|
| 2483 | pr_err("%s: unable to remove %s\n", __func__, dev_name(&genpd->dev));
|
| 2484 | return -EBUSY;
|
| 2485 | }
|
| 2486 |
|
| 2487 | list_for_each_entry_safe(link, l, &genpd->child_links, child_node) {
|
| 2488 | list_del(&link->parent_node);
|
| 2489 | list_del(&link->child_node);
|
| 2490 | kfree(link);
|
| 2491 | }
|
| 2492 |
|
| 2493 | list_del(&genpd->gpd_list_node);
|
| 2494 | genpd_unlock(genpd);
|
| 2495 | genpd_debug_remove(genpd);
|
| 2496 | cancel_work_sync(&genpd->power_off_work);
|
| 2497 | genpd_free_data(genpd);
|
| 2498 |
|
| 2499 | pr_debug("%s: removed %s\n", __func__, dev_name(&genpd->dev));
|
| 2500 |
|
| 2501 | return 0;
|
| 2502 | }
|
| 2503 |
|
| 2504 | |
| 2505 | * pm_genpd_remove - Remove a generic I/O PM domain
|
| 2506 | * @genpd: Pointer to PM domain that is to be removed.
|
| 2507 | *
|
| 2508 | * To remove the PM domain, this function:
|
| 2509 | * - Removes the PM domain as a subdomain to any parent domains,
|
| 2510 | * if it was added.
|
| 2511 | * - Removes the PM domain from the list of registered PM domains.
|
| 2512 | *
|
| 2513 | * The PM domain will only be removed, if the associated provider has
|
| 2514 | * been removed, it is not a parent to any other PM domain and has no
|
| 2515 | * devices associated with it.
|
| 2516 |
|
| 2517 | int pm_genpd_remove(struct generic_pm_domain *genpd)
|
| 2518 | {
|
| 2519 | int ret;
|
| 2520 |
|
| 2521 | mutex_lock(&gpd_list_lock);
|
| 2522 | ret = genpd_remove(genpd);
|
| 2523 | mutex_unlock(&gpd_list_lock);
|
| 2524 |
|
| 2525 | return ret;
|
| 2526 | }
|
| 2527 | EXPORT_SYMBOL_GPL(pm_genpd_remove);
|
| 2528 |
|
| 2529 | #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
|
| 2530 |
|
| 2531 | |
| 2532 | * Device Tree based PM domain providers.
|
| 2533 | *
|
| 2534 | * The code below implements generic device tree based PM domain providers that
|
| 2535 | * bind device tree nodes with generic PM domains registered in the system.
|
| 2536 | *
|
| 2537 | * Any driver that registers generic PM domains and needs to support binding of
|
| 2538 | * devices to these domains is supposed to register a PM domain provider, which
|
| 2539 | * maps a PM domain specifier retrieved from the device tree to a PM domain.
|
| 2540 | *
|
| 2541 | * Two simple mapping functions have been provided for convenience:
|
| 2542 | * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
|
| 2543 | * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
|
| 2544 | * index.
|
| 2545 |
|
| 2546 |
|
| 2547 | |
| 2548 | * struct of_genpd_provider - PM domain provider registration structure
|
| 2549 | * @link: Entry in global list of PM domain providers
|
| 2550 | * @node: Pointer to device tree node of PM domain provider
|
| 2551 | * @xlate: Provider-specific xlate callback mapping a set of specifier cells
|
| 2552 | * into a PM domain.
|
| 2553 | * @data: context pointer to be passed into @xlate callback
|
| 2554 |
|
| 2555 | struct of_genpd_provider {
|
| 2556 | struct list_head link;
|
| 2557 | struct device_node *node;
|
| 2558 | genpd_xlate_t xlate;
|
| 2559 | void *data;
|
| 2560 | };
|
| 2561 |
|
| 2562 |
|
| 2563 | static LIST_HEAD(of_genpd_providers);
|
| 2564 |
|
| 2565 | static DEFINE_MUTEX(of_genpd_mutex);
|
| 2566 |
|
| 2567 |
|
| 2568 | static struct device *genpd_provider_bus;
|
| 2569 |
|
| 2570 |
|
| 2571 | static bool genpd_bus_registered;
|
| 2572 |
|
| 2573 | |
| 2574 | * genpd_xlate_simple() - Xlate function for direct node-domain mapping
|
| 2575 | * @genpdspec: OF phandle args to map into a PM domain
|
| 2576 | * @data: xlate function private data - pointer to struct generic_pm_domain
|
| 2577 | *
|
| 2578 | * This is a generic xlate function that can be used to model PM domains that
|
| 2579 | * have their own device tree nodes. The private data of xlate function needs
|
| 2580 | * to be a valid pointer to struct generic_pm_domain.
|
| 2581 |
|
| 2582 | static struct generic_pm_domain *genpd_xlate_simple(
|
| 2583 | const struct of_phandle_args *genpdspec,
|
| 2584 | void *data)
|
| 2585 | {
|
| 2586 | return data;
|
| 2587 | }
|
| 2588 |
|
| 2589 | |
| 2590 | * genpd_xlate_onecell() - Xlate function using a single index.
|
| 2591 | * @genpdspec: OF phandle args to map into a PM domain
|
| 2592 | * @data: xlate function private data - pointer to struct genpd_onecell_data
|
| 2593 | *
|
| 2594 | * This is a generic xlate function that can be used to model simple PM domain
|
| 2595 | * controllers that have one device tree node and provide multiple PM domains.
|
| 2596 | * A single cell is used as an index into an array of PM domains specified in
|
| 2597 | * the genpd_onecell_data struct when registering the provider.
|
| 2598 |
|
| 2599 | static struct generic_pm_domain *genpd_xlate_onecell(
|
| 2600 | const struct of_phandle_args *genpdspec,
|
| 2601 | void *data)
|
| 2602 | {
|
| 2603 | struct genpd_onecell_data *genpd_data = data;
|
| 2604 | unsigned int idx = genpdspec->args[0];
|
| 2605 |
|
| 2606 | if (genpdspec->args_count != 1)
|
| 2607 | return ERR_PTR(-EINVAL);
|
| 2608 |
|
| 2609 | if (idx >= genpd_data->num_domains) {
|
| 2610 | pr_err("%s: invalid domain index %u\n", __func__, idx);
|
| 2611 | return ERR_PTR(-EINVAL);
|
| 2612 | }
|
| 2613 |
|
| 2614 | if (!genpd_data->domains[idx])
|
| 2615 | return ERR_PTR(-ENOENT);
|
| 2616 |
|
| 2617 | return genpd_data->domains[idx];
|
| 2618 | }
|
| 2619 |
|
| 2620 | |
| 2621 | * genpd_add_provider() - Register a PM domain provider for a node
|
| 2622 | * @np: Device node pointer associated with the PM domain provider.
|
| 2623 | * @xlate: Callback for decoding PM domain from phandle arguments.
|
| 2624 | * @data: Context pointer for @xlate callback.
|
| 2625 |
|
| 2626 | static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
|
| 2627 | void *data)
|
| 2628 | {
|
| 2629 | struct of_genpd_provider *cp;
|
| 2630 |
|
| 2631 | cp = kzalloc_obj(*cp);
|
| 2632 | if (!cp)
|
| 2633 | return -ENOMEM;
|
| 2634 |
|
| 2635 | cp->node = of_node_get(np);
|
| 2636 | cp->data = data;
|
| 2637 | cp->xlate = xlate;
|
| 2638 | fwnode_dev_initialized(of_fwnode_handle(np), true);
|
| 2639 |
|
| 2640 | mutex_lock(&of_genpd_mutex);
|
| 2641 | list_add(&cp->link, &of_genpd_providers);
|
| 2642 | mutex_unlock(&of_genpd_mutex);
|
| 2643 | pr_debug("Added domain provider from %pOF\n", np);
|
| 2644 |
|
| 2645 | return 0;
|
| 2646 | }
|
| 2647 |
|
| 2648 | static bool genpd_present(const struct generic_pm_domain *genpd)
|
| 2649 | {
|
| 2650 | bool ret = false;
|
| 2651 | const struct generic_pm_domain *gpd;
|
| 2652 |
|
| 2653 | mutex_lock(&gpd_list_lock);
|
| 2654 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
|
| 2655 | if (gpd == genpd) {
|
| 2656 | ret = true;
|
| 2657 | break;
|
| 2658 | }
|
| 2659 | }
|
| 2660 | mutex_unlock(&gpd_list_lock);
|
| 2661 |
|
| 2662 | return ret;
|
| 2663 | }
|
| 2664 |
|
| 2665 | static void genpd_sync_state(struct device *dev)
|
| 2666 | {
|
| 2667 | return of_genpd_sync_state(dev->of_node);
|
| 2668 | }
|
| 2669 |
|
| 2670 | |
| 2671 | * of_genpd_add_provider_simple() - Register a simple PM domain provider
|
| 2672 | * @np: Device node pointer associated with the PM domain provider.
|
| 2673 | * @genpd: Pointer to PM domain associated with the PM domain provider.
|
| 2674 |
|
| 2675 | int of_genpd_add_provider_simple(struct device_node *np,
|
| 2676 | struct generic_pm_domain *genpd)
|
| 2677 | {
|
| 2678 | struct fwnode_handle *fwnode;
|
| 2679 | struct device *dev;
|
| 2680 | int ret;
|
| 2681 |
|
| 2682 | if (!np || !genpd)
|
| 2683 | return -EINVAL;
|
| 2684 |
|
| 2685 | if (!genpd_bus_registered)
|
| 2686 | return -ENODEV;
|
| 2687 |
|
| 2688 | if (!genpd_present(genpd))
|
| 2689 | return -EINVAL;
|
| 2690 |
|
| 2691 | genpd->dev.parent = genpd_provider_bus;
|
| 2692 | genpd->dev.of_node = np;
|
| 2693 |
|
| 2694 | fwnode = of_fwnode_handle(np);
|
| 2695 | dev = get_dev_from_fwnode(fwnode);
|
| 2696 | if (!dev && !genpd_is_no_sync_state(genpd)) {
|
| 2697 | genpd->sync_state = GENPD_SYNC_STATE_SIMPLE;
|
| 2698 | device_set_node(&genpd->dev, fwnode);
|
| 2699 | } else {
|
| 2700 | dev_set_drv_sync_state(dev, genpd_sync_state);
|
| 2701 | }
|
| 2702 |
|
| 2703 | put_device(dev);
|
| 2704 |
|
| 2705 | ret = device_add(&genpd->dev);
|
| 2706 | if (ret)
|
| 2707 | return ret;
|
| 2708 |
|
| 2709 |
|
| 2710 | if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
|
| 2711 | ret = dev_pm_opp_of_add_table(&genpd->dev);
|
| 2712 | if (ret) {
|
| 2713 | dev_err_probe(&genpd->dev, ret, "Failed to add OPP table\n");
|
| 2714 | goto err_del;
|
| 2715 | }
|
| 2716 |
|
| 2717 | |
| 2718 | * Save table for faster processing while setting performance
|
| 2719 | * state.
|
| 2720 |
|
| 2721 | genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
|
| 2722 | WARN_ON(IS_ERR(genpd->opp_table));
|
| 2723 | }
|
| 2724 |
|
| 2725 | ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
|
| 2726 | if (ret)
|
| 2727 | goto err_opp;
|
| 2728 |
|
| 2729 | genpd->provider = fwnode;
|
| 2730 | genpd->has_provider = true;
|
| 2731 |
|
| 2732 | return 0;
|
| 2733 |
|
| 2734 | err_opp:
|
| 2735 | if (genpd->opp_table) {
|
| 2736 | dev_pm_opp_put_opp_table(genpd->opp_table);
|
| 2737 | dev_pm_opp_of_remove_table(&genpd->dev);
|
| 2738 | }
|
| 2739 | err_del:
|
| 2740 | device_del(&genpd->dev);
|
| 2741 | return ret;
|
| 2742 | }
|
| 2743 | EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
|
| 2744 |
|
| 2745 | |
| 2746 | * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
|
| 2747 | * @np: Device node pointer associated with the PM domain provider.
|
| 2748 | * @data: Pointer to the data associated with the PM domain provider.
|
| 2749 |
|
| 2750 | int of_genpd_add_provider_onecell(struct device_node *np,
|
| 2751 | struct genpd_onecell_data *data)
|
| 2752 | {
|
| 2753 | struct generic_pm_domain *genpd;
|
| 2754 | struct fwnode_handle *fwnode;
|
| 2755 | struct device *dev;
|
| 2756 | unsigned int i;
|
| 2757 | int ret = -EINVAL;
|
| 2758 | bool sync_state = false;
|
| 2759 |
|
| 2760 | if (!np || !data)
|
| 2761 | return -EINVAL;
|
| 2762 |
|
| 2763 | if (!genpd_bus_registered)
|
| 2764 | return -ENODEV;
|
| 2765 |
|
| 2766 | if (!data->xlate)
|
| 2767 | data->xlate = genpd_xlate_onecell;
|
| 2768 |
|
| 2769 | fwnode = of_fwnode_handle(np);
|
| 2770 | dev = get_dev_from_fwnode(fwnode);
|
| 2771 | if (!dev)
|
| 2772 | sync_state = true;
|
| 2773 | else
|
| 2774 | dev_set_drv_sync_state(dev, genpd_sync_state);
|
| 2775 |
|
| 2776 | put_device(dev);
|
| 2777 |
|
| 2778 | for (i = 0; i < data->num_domains; i++) {
|
| 2779 | genpd = data->domains[i];
|
| 2780 |
|
| 2781 | if (!genpd)
|
| 2782 | continue;
|
| 2783 | if (!genpd_present(genpd))
|
| 2784 | goto error;
|
| 2785 |
|
| 2786 | genpd->dev.parent = genpd_provider_bus;
|
| 2787 | genpd->dev.of_node = np;
|
| 2788 |
|
| 2789 | if (sync_state && !genpd_is_no_sync_state(genpd)) {
|
| 2790 | genpd->sync_state = GENPD_SYNC_STATE_ONECELL;
|
| 2791 | device_set_node(&genpd->dev, fwnode);
|
| 2792 | sync_state = false;
|
| 2793 | }
|
| 2794 |
|
| 2795 | ret = device_add(&genpd->dev);
|
| 2796 | if (ret)
|
| 2797 | goto error;
|
| 2798 |
|
| 2799 |
|
| 2800 | if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
|
| 2801 | ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
|
| 2802 | if (ret) {
|
| 2803 | dev_err_probe(&genpd->dev, ret,
|
| 2804 | "Failed to add OPP table for index %d\n", i);
|
| 2805 | device_del(&genpd->dev);
|
| 2806 | goto error;
|
| 2807 | }
|
| 2808 |
|
| 2809 | |
| 2810 | * Save table for faster processing while setting
|
| 2811 | * performance state.
|
| 2812 |
|
| 2813 | genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
|
| 2814 | WARN_ON(IS_ERR(genpd->opp_table));
|
| 2815 | }
|
| 2816 |
|
| 2817 | genpd->provider = fwnode;
|
| 2818 | genpd->has_provider = true;
|
| 2819 | }
|
| 2820 |
|
| 2821 | ret = genpd_add_provider(np, data->xlate, data);
|
| 2822 | if (ret < 0)
|
| 2823 | goto error;
|
| 2824 |
|
| 2825 | return 0;
|
| 2826 |
|
| 2827 | error:
|
| 2828 | while (i--) {
|
| 2829 | genpd = data->domains[i];
|
| 2830 |
|
| 2831 | if (!genpd)
|
| 2832 | continue;
|
| 2833 |
|
| 2834 | genpd->provider = NULL;
|
| 2835 | genpd->has_provider = false;
|
| 2836 |
|
| 2837 | if (genpd->opp_table) {
|
| 2838 | dev_pm_opp_put_opp_table(genpd->opp_table);
|
| 2839 | dev_pm_opp_of_remove_table(&genpd->dev);
|
| 2840 | }
|
| 2841 |
|
| 2842 | device_del(&genpd->dev);
|
| 2843 | }
|
| 2844 |
|
| 2845 | return ret;
|
| 2846 | }
|
| 2847 | EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
|
| 2848 |
|
| 2849 | |
| 2850 | * of_genpd_del_provider() - Remove a previously registered PM domain provider
|
| 2851 | * @np: Device node pointer associated with the PM domain provider
|
| 2852 |
|
| 2853 | void of_genpd_del_provider(struct device_node *np)
|
| 2854 | {
|
| 2855 | struct of_genpd_provider *cp, *tmp;
|
| 2856 | struct generic_pm_domain *gpd;
|
| 2857 |
|
| 2858 | mutex_lock(&gpd_list_lock);
|
| 2859 | mutex_lock(&of_genpd_mutex);
|
| 2860 | list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
|
| 2861 | if (cp->node == np) {
|
| 2862 | |
| 2863 | * For each PM domain associated with the
|
| 2864 | * provider, set the 'has_provider' to false
|
| 2865 | * so that the PM domain can be safely removed.
|
| 2866 |
|
| 2867 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
|
| 2868 | if (gpd->provider == of_fwnode_handle(np)) {
|
| 2869 | gpd->has_provider = false;
|
| 2870 |
|
| 2871 | if (gpd->opp_table) {
|
| 2872 | dev_pm_opp_put_opp_table(gpd->opp_table);
|
| 2873 | dev_pm_opp_of_remove_table(&gpd->dev);
|
| 2874 | }
|
| 2875 |
|
| 2876 | device_del(&gpd->dev);
|
| 2877 | }
|
| 2878 | }
|
| 2879 |
|
| 2880 | fwnode_dev_initialized(of_fwnode_handle(cp->node), false);
|
| 2881 | list_del(&cp->link);
|
| 2882 | of_node_put(cp->node);
|
| 2883 | kfree(cp);
|
| 2884 | break;
|
| 2885 | }
|
| 2886 | }
|
| 2887 | mutex_unlock(&of_genpd_mutex);
|
| 2888 | mutex_unlock(&gpd_list_lock);
|
| 2889 | }
|
| 2890 | EXPORT_SYMBOL_GPL(of_genpd_del_provider);
|
| 2891 |
|
| 2892 | |
| 2893 | * genpd_get_from_provider() - Look-up PM domain
|
| 2894 | * @genpdspec: OF phandle args to use for look-up
|
| 2895 | *
|
| 2896 | * Looks for a PM domain provider under the node specified by @genpdspec and if
|
| 2897 | * found, uses xlate function of the provider to map phandle args to a PM
|
| 2898 | * domain.
|
| 2899 | *
|
| 2900 | * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
|
| 2901 | * on failure.
|
| 2902 |
|
| 2903 | static struct generic_pm_domain *genpd_get_from_provider(
|
| 2904 | const struct of_phandle_args *genpdspec)
|
| 2905 | {
|
| 2906 | struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
|
| 2907 | struct of_genpd_provider *provider;
|
| 2908 |
|
| 2909 | if (!genpdspec)
|
| 2910 | return ERR_PTR(-EINVAL);
|
| 2911 |
|
| 2912 | mutex_lock(&of_genpd_mutex);
|
| 2913 |
|
| 2914 |
|
| 2915 | list_for_each_entry(provider, &of_genpd_providers, link) {
|
| 2916 | if (provider->node == genpdspec->np)
|
| 2917 | genpd = provider->xlate(genpdspec, provider->data);
|
| 2918 | if (!IS_ERR(genpd))
|
| 2919 | break;
|
| 2920 | }
|
| 2921 |
|
| 2922 | mutex_unlock(&of_genpd_mutex);
|
| 2923 |
|
| 2924 | return genpd;
|
| 2925 | }
|
| 2926 |
|
| 2927 | |
| 2928 | * of_genpd_add_child_ids() - Parse power-domains-child-ids property
|
| 2929 | * @np: Device node pointer associated with the PM domain provider.
|
| 2930 | * @data: Pointer to the onecell data associated with the PM domain provider.
|
| 2931 | *
|
| 2932 | * Parse the power-domains and power-domains-child-ids properties to establish
|
| 2933 | * parent-child relationships for PM domains. The power-domains property lists
|
| 2934 | * parent domains, and power-domains-child-ids lists which child domain IDs
|
| 2935 | * should be associated with each parent.
|
| 2936 | *
|
| 2937 | * Uses "all or nothing" semantics: either all relationships are established
|
| 2938 | * successfully, or none are (any partially-added relationships are unwound
|
| 2939 | * on error).
|
| 2940 | *
|
| 2941 | * Returns the number of parent-child relationships established on success,
|
| 2942 | * 0 if the properties don't exist, or a negative error code on failure.
|
| 2943 |
|
| 2944 | int of_genpd_add_child_ids(struct device_node *np,
|
| 2945 | struct genpd_onecell_data *data)
|
| 2946 | {
|
| 2947 | struct of_phandle_args parent_args;
|
| 2948 | struct generic_pm_domain *parent_genpd, *child_genpd;
|
| 2949 | struct generic_pm_domain **pairs;
|
| 2950 | u32 child_id;
|
| 2951 | int i, ret, count, child_count, added = 0;
|
| 2952 |
|
| 2953 |
|
| 2954 | count = of_count_phandle_with_args(np, "power-domains", "#power-domain-cells");
|
| 2955 | if (count <= 0)
|
| 2956 | return 0;
|
| 2957 |
|
| 2958 | child_count = of_property_count_u32_elems(np, "power-domains-child-ids");
|
| 2959 | if (child_count < 0)
|
| 2960 | return 0;
|
| 2961 | if (child_count != count)
|
| 2962 | return -EINVAL;
|
| 2963 |
|
| 2964 |
|
| 2965 | pairs = kmalloc_objs(*pairs, count * 2);
|
| 2966 | if (!pairs)
|
| 2967 | return -ENOMEM;
|
| 2968 |
|
| 2969 | for (i = 0; i < count; i++) {
|
| 2970 | ret = of_property_read_u32_index(np, "power-domains-child-ids",
|
| 2971 | i, &child_id);
|
| 2972 | if (ret)
|
| 2973 | goto err_unwind;
|
| 2974 |
|
| 2975 |
|
| 2976 | if (child_id >= data->num_domains) {
|
| 2977 | pr_err("Child ID %u out of bounds (max %u) for %pOF\n",
|
| 2978 | child_id, data->num_domains - 1, np);
|
| 2979 | ret = -EINVAL;
|
| 2980 | goto err_unwind;
|
| 2981 | }
|
| 2982 |
|
| 2983 |
|
| 2984 | child_genpd = data->domains[child_id];
|
| 2985 | if (!child_genpd) {
|
| 2986 | pr_err("Child domain %u is NULL for %pOF\n", child_id, np);
|
| 2987 | ret = -EINVAL;
|
| 2988 | goto err_unwind;
|
| 2989 | }
|
| 2990 |
|
| 2991 | ret = of_parse_phandle_with_args(np, "power-domains",
|
| 2992 | "#power-domain-cells", i,
|
| 2993 | &parent_args);
|
| 2994 | if (ret)
|
| 2995 | goto err_unwind;
|
| 2996 |
|
| 2997 |
|
| 2998 | parent_genpd = genpd_get_from_provider(&parent_args);
|
| 2999 | of_node_put(parent_args.np);
|
| 3000 | if (IS_ERR(parent_genpd)) {
|
| 3001 | pr_err("Failed to get parent domain for %pOF: %ld\n",
|
| 3002 | np, PTR_ERR(parent_genpd));
|
| 3003 | ret = PTR_ERR(parent_genpd);
|
| 3004 | goto err_unwind;
|
| 3005 | }
|
| 3006 |
|
| 3007 |
|
| 3008 | ret = pm_genpd_add_subdomain(parent_genpd, child_genpd);
|
| 3009 | if (ret) {
|
| 3010 | pr_err("Failed to add child domain %u to parent in %pOF: %d\n",
|
| 3011 | child_id, np, ret);
|
| 3012 | goto err_unwind;
|
| 3013 | }
|
| 3014 |
|
| 3015 |
|
| 3016 | pairs[2 * added] = parent_genpd;
|
| 3017 | pairs[2 * added + 1] = child_genpd;
|
| 3018 | added++;
|
| 3019 |
|
| 3020 | pr_debug("Added child domain %u (%s) to parent %s for %pOF\n",
|
| 3021 | child_id, child_genpd->name, parent_genpd->name, np);
|
| 3022 | }
|
| 3023 |
|
| 3024 | kfree(pairs);
|
| 3025 | return count;
|
| 3026 |
|
| 3027 | err_unwind:
|
| 3028 |
|
| 3029 | while (added-- > 0)
|
| 3030 | pm_genpd_remove_subdomain(pairs[2 * added], pairs[2 * added + 1]);
|
| 3031 | kfree(pairs);
|
| 3032 | return ret;
|
| 3033 | }
|
| 3034 | EXPORT_SYMBOL_GPL(of_genpd_add_child_ids);
|
| 3035 |
|
| 3036 | |
| 3037 | * of_genpd_remove_child_ids() - Remove parent-child PM domain relationships
|
| 3038 | * @np: Device node pointer associated with the PM domain provider.
|
| 3039 | * @data: Pointer to the onecell data associated with the PM domain provider.
|
| 3040 | *
|
| 3041 | * Reverses the effect of of_genpd_add_child_ids() by parsing the same
|
| 3042 | * power-domains and power-domains-child-ids properties and calling
|
| 3043 | * pm_genpd_remove_subdomain() for each established relationship.
|
| 3044 | *
|
| 3045 | * Returns 0 on success, -ENOENT if properties don't exist, or negative error
|
| 3046 | * code on failure.
|
| 3047 |
|
| 3048 | int of_genpd_remove_child_ids(struct device_node *np,
|
| 3049 | struct genpd_onecell_data *data)
|
| 3050 | {
|
| 3051 | struct of_phandle_args parent_args;
|
| 3052 | struct generic_pm_domain *parent_genpd, *child_genpd;
|
| 3053 | u32 child_id;
|
| 3054 | int i, ret, count, child_count;
|
| 3055 |
|
| 3056 |
|
| 3057 | count = of_count_phandle_with_args(np, "power-domains", "#power-domain-cells");
|
| 3058 | if (count <= 0)
|
| 3059 | return -ENOENT;
|
| 3060 |
|
| 3061 | child_count = of_property_count_u32_elems(np, "power-domains-child-ids");
|
| 3062 | if (child_count < 0)
|
| 3063 | return -ENOENT;
|
| 3064 | if (child_count != count)
|
| 3065 | return -EINVAL;
|
| 3066 |
|
| 3067 | for (i = 0; i < count; i++) {
|
| 3068 | if (of_property_read_u32_index(np, "power-domains-child-ids",
|
| 3069 | i, &child_id))
|
| 3070 | continue;
|
| 3071 |
|
| 3072 | if (child_id >= data->num_domains || !data->domains[child_id])
|
| 3073 | continue;
|
| 3074 |
|
| 3075 | ret = of_parse_phandle_with_args(np, "power-domains",
|
| 3076 | "#power-domain-cells", i,
|
| 3077 | &parent_args);
|
| 3078 | if (ret)
|
| 3079 | continue;
|
| 3080 |
|
| 3081 | parent_genpd = genpd_get_from_provider(&parent_args);
|
| 3082 | of_node_put(parent_args.np);
|
| 3083 | if (IS_ERR(parent_genpd))
|
| 3084 | continue;
|
| 3085 |
|
| 3086 | child_genpd = data->domains[child_id];
|
| 3087 | pm_genpd_remove_subdomain(parent_genpd, child_genpd);
|
| 3088 | }
|
| 3089 |
|
| 3090 | return 0;
|
| 3091 | }
|
| 3092 | EXPORT_SYMBOL_GPL(of_genpd_remove_child_ids);
|
| 3093 |
|
| 3094 | |
| 3095 | * of_genpd_add_device() - Add a device to an I/O PM domain
|
| 3096 | * @genpdspec: OF phandle args to use for look-up PM domain
|
| 3097 | * @dev: Device to be added.
|
| 3098 | *
|
| 3099 | * Looks-up an I/O PM domain based upon phandle args provided and adds
|
| 3100 | * the device to the PM domain. Returns a negative error code on failure.
|
| 3101 |
|
| 3102 | int of_genpd_add_device(const struct of_phandle_args *genpdspec, struct device *dev)
|
| 3103 | {
|
| 3104 | struct generic_pm_domain *genpd;
|
| 3105 | int ret;
|
| 3106 |
|
| 3107 | if (!dev)
|
| 3108 | return -EINVAL;
|
| 3109 |
|
| 3110 | mutex_lock(&gpd_list_lock);
|
| 3111 |
|
| 3112 | genpd = genpd_get_from_provider(genpdspec);
|
| 3113 | if (IS_ERR(genpd)) {
|
| 3114 | ret = PTR_ERR(genpd);
|
| 3115 | goto out;
|
| 3116 | }
|
| 3117 |
|
| 3118 | ret = genpd_add_device(genpd, dev, dev);
|
| 3119 |
|
| 3120 | out:
|
| 3121 | mutex_unlock(&gpd_list_lock);
|
| 3122 |
|
| 3123 | return ret;
|
| 3124 | }
|
| 3125 | EXPORT_SYMBOL_GPL(of_genpd_add_device);
|
| 3126 |
|
| 3127 | |
| 3128 | * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
|
| 3129 | * @parent_spec: OF phandle args to use for parent PM domain look-up
|
| 3130 | * @subdomain_spec: OF phandle args to use for subdomain look-up
|
| 3131 | *
|
| 3132 | * Looks-up a parent PM domain and subdomain based upon phandle args
|
| 3133 | * provided and adds the subdomain to the parent PM domain. Returns a
|
| 3134 | * negative error code on failure.
|
| 3135 |
|
| 3136 | int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
|
| 3137 | const struct of_phandle_args *subdomain_spec)
|
| 3138 | {
|
| 3139 | struct generic_pm_domain *parent, *subdomain;
|
| 3140 | int ret;
|
| 3141 |
|
| 3142 | mutex_lock(&gpd_list_lock);
|
| 3143 |
|
| 3144 | parent = genpd_get_from_provider(parent_spec);
|
| 3145 | if (IS_ERR(parent)) {
|
| 3146 | ret = PTR_ERR(parent);
|
| 3147 | goto out;
|
| 3148 | }
|
| 3149 |
|
| 3150 | subdomain = genpd_get_from_provider(subdomain_spec);
|
| 3151 | if (IS_ERR(subdomain)) {
|
| 3152 | ret = PTR_ERR(subdomain);
|
| 3153 | goto out;
|
| 3154 | }
|
| 3155 |
|
| 3156 | ret = genpd_add_subdomain(parent, subdomain);
|
| 3157 |
|
| 3158 | out:
|
| 3159 | mutex_unlock(&gpd_list_lock);
|
| 3160 |
|
| 3161 | return ret == -ENOENT ? -EPROBE_DEFER : ret;
|
| 3162 | }
|
| 3163 | EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
|
| 3164 |
|
| 3165 | |
| 3166 | * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
|
| 3167 | * @parent_spec: OF phandle args to use for parent PM domain look-up
|
| 3168 | * @subdomain_spec: OF phandle args to use for subdomain look-up
|
| 3169 | *
|
| 3170 | * Looks-up a parent PM domain and subdomain based upon phandle args
|
| 3171 | * provided and removes the subdomain from the parent PM domain. Returns a
|
| 3172 | * negative error code on failure.
|
| 3173 |
|
| 3174 | int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
|
| 3175 | const struct of_phandle_args *subdomain_spec)
|
| 3176 | {
|
| 3177 | struct generic_pm_domain *parent, *subdomain;
|
| 3178 | int ret;
|
| 3179 |
|
| 3180 | mutex_lock(&gpd_list_lock);
|
| 3181 |
|
| 3182 | parent = genpd_get_from_provider(parent_spec);
|
| 3183 | if (IS_ERR(parent)) {
|
| 3184 | ret = PTR_ERR(parent);
|
| 3185 | goto out;
|
| 3186 | }
|
| 3187 |
|
| 3188 | subdomain = genpd_get_from_provider(subdomain_spec);
|
| 3189 | if (IS_ERR(subdomain)) {
|
| 3190 | ret = PTR_ERR(subdomain);
|
| 3191 | goto out;
|
| 3192 | }
|
| 3193 |
|
| 3194 | ret = pm_genpd_remove_subdomain(parent, subdomain);
|
| 3195 |
|
| 3196 | out:
|
| 3197 | mutex_unlock(&gpd_list_lock);
|
| 3198 |
|
| 3199 | return ret;
|
| 3200 | }
|
| 3201 | EXPORT_SYMBOL_GPL(of_genpd_remove_subdomain);
|
| 3202 |
|
| 3203 | |
| 3204 | * of_genpd_remove_last - Remove the last PM domain registered for a provider
|
| 3205 | * @np: Pointer to device node associated with provider
|
| 3206 | *
|
| 3207 | * Find the last PM domain that was added by a particular provider and
|
| 3208 | * remove this PM domain from the list of PM domains. The provider is
|
| 3209 | * identified by the 'provider' device structure that is passed. The PM
|
| 3210 | * domain will only be removed, if the provider associated with domain
|
| 3211 | * has been removed.
|
| 3212 | *
|
| 3213 | * Returns a valid pointer to struct generic_pm_domain on success or
|
| 3214 | * ERR_PTR() on failure.
|
| 3215 |
|
| 3216 | struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
|
| 3217 | {
|
| 3218 | struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
|
| 3219 | int ret;
|
| 3220 |
|
| 3221 | if (IS_ERR_OR_NULL(np))
|
| 3222 | return ERR_PTR(-EINVAL);
|
| 3223 |
|
| 3224 | mutex_lock(&gpd_list_lock);
|
| 3225 | list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
|
| 3226 | if (gpd->provider == of_fwnode_handle(np)) {
|
| 3227 | ret = genpd_remove(gpd);
|
| 3228 | genpd = ret ? ERR_PTR(ret) : gpd;
|
| 3229 | break;
|
| 3230 | }
|
| 3231 | }
|
| 3232 | mutex_unlock(&gpd_list_lock);
|
| 3233 |
|
| 3234 | return genpd;
|
| 3235 | }
|
| 3236 | EXPORT_SYMBOL_GPL(of_genpd_remove_last);
|
| 3237 |
|
| 3238 | static void genpd_release_dev(struct device *dev)
|
| 3239 | {
|
| 3240 | of_node_put(dev->of_node);
|
| 3241 | kfree(dev);
|
| 3242 | }
|
| 3243 |
|
| 3244 | static const struct bus_type genpd_bus_type = {
|
| 3245 | .name = "genpd",
|
| 3246 | };
|
| 3247 |
|
| 3248 | |
| 3249 | * genpd_dev_pm_detach - Detach a device from its PM domain.
|
| 3250 | * @dev: Device to detach.
|
| 3251 | * @power_off: Currently not used
|
| 3252 | *
|
| 3253 | * Try to locate a corresponding generic PM domain, which the device was
|
| 3254 | * attached to previously. If such is found, the device is detached from it.
|
| 3255 |
|
| 3256 | static void genpd_dev_pm_detach(struct device *dev, bool power_off)
|
| 3257 | {
|
| 3258 | struct generic_pm_domain *pd;
|
| 3259 | bool is_virt_dev;
|
| 3260 | unsigned int i;
|
| 3261 | int ret = 0;
|
| 3262 |
|
| 3263 | pd = dev_to_genpd(dev);
|
| 3264 | if (IS_ERR(pd))
|
| 3265 | return;
|
| 3266 |
|
| 3267 | dev_dbg(dev, "removing from PM domain %s\n", pd->name);
|
| 3268 |
|
| 3269 |
|
| 3270 | is_virt_dev = dev->bus == &genpd_bus_type;
|
| 3271 |
|
| 3272 |
|
| 3273 | if (is_virt_dev)
|
| 3274 | pm_runtime_disable(dev);
|
| 3275 |
|
| 3276 |
|
| 3277 | if (dev_gpd_data(dev)->default_pstate) {
|
| 3278 | dev_pm_genpd_set_performance_state(dev, 0);
|
| 3279 | dev_gpd_data(dev)->default_pstate = 0;
|
| 3280 | }
|
| 3281 |
|
| 3282 | for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
|
| 3283 | ret = genpd_remove_device(pd, dev);
|
| 3284 | if (ret != -EAGAIN)
|
| 3285 | break;
|
| 3286 |
|
| 3287 | mdelay(i);
|
| 3288 | cond_resched();
|
| 3289 | }
|
| 3290 |
|
| 3291 | if (ret < 0) {
|
| 3292 | dev_err(dev, "failed to remove from PM domain %s: %d",
|
| 3293 | pd->name, ret);
|
| 3294 | return;
|
| 3295 | }
|
| 3296 |
|
| 3297 |
|
| 3298 | genpd_queue_power_off_work(pd);
|
| 3299 |
|
| 3300 |
|
| 3301 | if (is_virt_dev)
|
| 3302 | device_unregister(dev);
|
| 3303 | }
|
| 3304 |
|
| 3305 | static void genpd_dev_pm_sync(struct device *dev)
|
| 3306 | {
|
| 3307 | struct generic_pm_domain *pd;
|
| 3308 |
|
| 3309 | pd = dev_to_genpd(dev);
|
| 3310 | if (IS_ERR(pd))
|
| 3311 | return;
|
| 3312 |
|
| 3313 | genpd_queue_power_off_work(pd);
|
| 3314 | }
|
| 3315 |
|
| 3316 | static int genpd_set_required_opp_dev(struct device *dev,
|
| 3317 | struct device *base_dev)
|
| 3318 | {
|
| 3319 | struct dev_pm_opp_config config = {
|
| 3320 | .required_dev = dev,
|
| 3321 | };
|
| 3322 | int ret;
|
| 3323 |
|
| 3324 |
|
| 3325 | if (of_property_present(base_dev->of_node, "#power-domain-cells"))
|
| 3326 | return 0;
|
| 3327 |
|
| 3328 | if (!dev_pm_opp_of_has_required_opp(base_dev))
|
| 3329 | return 0;
|
| 3330 |
|
| 3331 | ret = dev_pm_opp_set_config(base_dev, &config);
|
| 3332 | if (ret < 0)
|
| 3333 | return ret;
|
| 3334 |
|
| 3335 | dev_gpd_data(dev)->opp_token = ret;
|
| 3336 | return 0;
|
| 3337 | }
|
| 3338 |
|
| 3339 | static int genpd_set_required_opp(struct device *dev, unsigned int index)
|
| 3340 | {
|
| 3341 | int ret, pstate;
|
| 3342 |
|
| 3343 |
|
| 3344 | pstate = of_get_required_opp_performance_state(dev->of_node, index);
|
| 3345 | if (pstate < 0 && pstate != -ENODEV && pstate != -EOPNOTSUPP) {
|
| 3346 | ret = pstate;
|
| 3347 | goto err;
|
| 3348 | } else if (pstate > 0) {
|
| 3349 | ret = dev_pm_genpd_set_performance_state(dev, pstate);
|
| 3350 | if (ret)
|
| 3351 | goto err;
|
| 3352 | dev_gpd_data(dev)->default_pstate = pstate;
|
| 3353 | }
|
| 3354 |
|
| 3355 | return 0;
|
| 3356 | err:
|
| 3357 | dev_err(dev, "failed to set required performance state for power-domain %s: %d\n",
|
| 3358 | dev_to_genpd(dev)->name, ret);
|
| 3359 | return ret;
|
| 3360 | }
|
| 3361 |
|
| 3362 | static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
|
| 3363 | unsigned int index, unsigned int num_domains,
|
| 3364 | bool power_on)
|
| 3365 | {
|
| 3366 | struct of_phandle_args pd_args;
|
| 3367 | struct generic_pm_domain *pd;
|
| 3368 | int ret;
|
| 3369 |
|
| 3370 | ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
|
| 3371 | "#power-domain-cells", index, &pd_args);
|
| 3372 | if (ret < 0)
|
| 3373 | return ret;
|
| 3374 |
|
| 3375 | mutex_lock(&gpd_list_lock);
|
| 3376 | pd = genpd_get_from_provider(&pd_args);
|
| 3377 | of_node_put(pd_args.np);
|
| 3378 | if (IS_ERR(pd)) {
|
| 3379 | mutex_unlock(&gpd_list_lock);
|
| 3380 | dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
|
| 3381 | __func__, PTR_ERR(pd));
|
| 3382 | return driver_deferred_probe_check_state(base_dev);
|
| 3383 | }
|
| 3384 |
|
| 3385 | dev_dbg(dev, "adding to PM domain %s\n", pd->name);
|
| 3386 |
|
| 3387 | ret = genpd_add_device(pd, dev, base_dev);
|
| 3388 | mutex_unlock(&gpd_list_lock);
|
| 3389 |
|
| 3390 | if (ret < 0)
|
| 3391 | return dev_err_probe(dev, ret, "failed to add to PM domain %s\n", pd->name);
|
| 3392 |
|
| 3393 | dev->pm_domain->detach = genpd_dev_pm_detach;
|
| 3394 | dev->pm_domain->sync = genpd_dev_pm_sync;
|
| 3395 |
|
| 3396 | |
| 3397 | * For a single PM domain the index of the required OPP must be zero, so
|
| 3398 | * let's try to assign a required dev in that case. In the multiple PM
|
| 3399 | * domains case, we need platform code to specify the index.
|
| 3400 |
|
| 3401 | if (num_domains == 1) {
|
| 3402 | ret = genpd_set_required_opp_dev(dev, base_dev);
|
| 3403 | if (ret)
|
| 3404 | goto err;
|
| 3405 | }
|
| 3406 |
|
| 3407 | ret = genpd_set_required_opp(dev, index);
|
| 3408 | if (ret)
|
| 3409 | goto err;
|
| 3410 |
|
| 3411 | if (power_on) {
|
| 3412 | genpd_lock(pd);
|
| 3413 | ret = genpd_power_on(pd, 0);
|
| 3414 | genpd_unlock(pd);
|
| 3415 | }
|
| 3416 |
|
| 3417 | if (ret) {
|
| 3418 |
|
| 3419 | if (dev_gpd_data(dev)->default_pstate) {
|
| 3420 | dev_pm_genpd_set_performance_state(dev, 0);
|
| 3421 | dev_gpd_data(dev)->default_pstate = 0;
|
| 3422 | }
|
| 3423 |
|
| 3424 | genpd_remove_device(pd, dev);
|
| 3425 | return -EPROBE_DEFER;
|
| 3426 | }
|
| 3427 |
|
| 3428 | return 1;
|
| 3429 |
|
| 3430 | err:
|
| 3431 | genpd_remove_device(pd, dev);
|
| 3432 | return ret;
|
| 3433 | }
|
| 3434 |
|
| 3435 | |
| 3436 | * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
|
| 3437 | * @dev: Device to attach.
|
| 3438 | *
|
| 3439 | * Parse device's OF node to find a PM domain specifier. If such is found,
|
| 3440 | * attaches the device to retrieved pm_domain ops.
|
| 3441 | *
|
| 3442 | * Returns 1 on successfully attached PM domain, 0 when the device don't need a
|
| 3443 | * PM domain or when multiple power-domains exists for it, else a negative error
|
| 3444 | * code. Note that if a power-domain exists for the device, but it cannot be
|
| 3445 | * found or turned on, then return -EPROBE_DEFER to ensure that the device is
|
| 3446 | * not probed and to re-try again later.
|
| 3447 |
|
| 3448 | int genpd_dev_pm_attach(struct device *dev)
|
| 3449 | {
|
| 3450 | if (!dev->of_node)
|
| 3451 | return 0;
|
| 3452 |
|
| 3453 | |
| 3454 | * Devices with multiple PM domains must be attached separately, as we
|
| 3455 | * can only attach one PM domain per device.
|
| 3456 |
|
| 3457 | if (of_count_phandle_with_args(dev->of_node, "power-domains",
|
| 3458 | "#power-domain-cells") != 1)
|
| 3459 | return 0;
|
| 3460 |
|
| 3461 | return __genpd_dev_pm_attach(dev, dev, 0, 1, true);
|
| 3462 | }
|
| 3463 | EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
|
| 3464 |
|
| 3465 | |
| 3466 | * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
|
| 3467 | * @dev: The device used to lookup the PM domain.
|
| 3468 | * @index: The index of the PM domain.
|
| 3469 | *
|
| 3470 | * Parse device's OF node to find a PM domain specifier at the provided @index.
|
| 3471 | * If such is found, creates a virtual device and attaches it to the retrieved
|
| 3472 | * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
|
| 3473 | * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
|
| 3474 | *
|
| 3475 | * Returns the created virtual device if successfully attached PM domain, NULL
|
| 3476 | * when the device don't need a PM domain, else an ERR_PTR() in case of
|
| 3477 | * failures. If a power-domain exists for the device, but cannot be found or
|
| 3478 | * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
|
| 3479 | * is not probed and to re-try again later.
|
| 3480 |
|
| 3481 | struct device *genpd_dev_pm_attach_by_id(struct device *dev,
|
| 3482 | unsigned int index)
|
| 3483 | {
|
| 3484 | struct device *virt_dev;
|
| 3485 | int num_domains;
|
| 3486 | int ret;
|
| 3487 |
|
| 3488 | if (!dev->of_node)
|
| 3489 | return NULL;
|
| 3490 |
|
| 3491 |
|
| 3492 | num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
|
| 3493 | "#power-domain-cells");
|
| 3494 | if (num_domains < 0 || index >= num_domains)
|
| 3495 | return NULL;
|
| 3496 |
|
| 3497 | if (!genpd_bus_registered)
|
| 3498 | return ERR_PTR(-ENODEV);
|
| 3499 |
|
| 3500 |
|
| 3501 | virt_dev = kzalloc_obj(*virt_dev);
|
| 3502 | if (!virt_dev)
|
| 3503 | return ERR_PTR(-ENOMEM);
|
| 3504 |
|
| 3505 | dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
|
| 3506 | virt_dev->bus = &genpd_bus_type;
|
| 3507 | virt_dev->release = genpd_release_dev;
|
| 3508 | virt_dev->of_node = of_node_get(dev->of_node);
|
| 3509 |
|
| 3510 | ret = device_register(virt_dev);
|
| 3511 | if (ret) {
|
| 3512 | put_device(virt_dev);
|
| 3513 | return ERR_PTR(ret);
|
| 3514 | }
|
| 3515 |
|
| 3516 |
|
| 3517 | ret = __genpd_dev_pm_attach(virt_dev, dev, index, num_domains, false);
|
| 3518 | if (ret < 1) {
|
| 3519 | device_unregister(virt_dev);
|
| 3520 | return ret ? ERR_PTR(ret) : NULL;
|
| 3521 | }
|
| 3522 |
|
| 3523 | pm_runtime_enable(virt_dev);
|
| 3524 | genpd_queue_power_off_work(dev_to_genpd(virt_dev));
|
| 3525 |
|
| 3526 | return virt_dev;
|
| 3527 | }
|
| 3528 | EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
|
| 3529 |
|
| 3530 | |
| 3531 | * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
|
| 3532 | * @dev: The device used to lookup the PM domain.
|
| 3533 | * @name: The name of the PM domain.
|
| 3534 | *
|
| 3535 | * Parse device's OF node to find a PM domain specifier using the
|
| 3536 | * power-domain-names DT property. For further description see
|
| 3537 | * genpd_dev_pm_attach_by_id().
|
| 3538 |
|
| 3539 | struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
|
| 3540 | {
|
| 3541 | int index;
|
| 3542 |
|
| 3543 | if (!dev->of_node)
|
| 3544 | return NULL;
|
| 3545 |
|
| 3546 | index = of_property_match_string(dev->of_node, "power-domain-names",
|
| 3547 | name);
|
| 3548 | if (index < 0)
|
| 3549 | return NULL;
|
| 3550 |
|
| 3551 | return genpd_dev_pm_attach_by_id(dev, index);
|
| 3552 | }
|
| 3553 |
|
| 3554 | static const struct of_device_id idle_state_match[] = {
|
| 3555 | { .compatible = "domain-idle-state", },
|
| 3556 | { }
|
| 3557 | };
|
| 3558 |
|
| 3559 | static int genpd_parse_state(struct genpd_power_state *genpd_state,
|
| 3560 | struct device_node *state_node)
|
| 3561 | {
|
| 3562 | int err;
|
| 3563 | u32 residency;
|
| 3564 | u32 entry_latency, exit_latency;
|
| 3565 |
|
| 3566 | err = of_property_read_u32(state_node, "entry-latency-us",
|
| 3567 | &entry_latency);
|
| 3568 | if (err) {
|
| 3569 | pr_debug(" * %pOF missing entry-latency-us property\n",
|
| 3570 | state_node);
|
| 3571 | return -EINVAL;
|
| 3572 | }
|
| 3573 |
|
| 3574 | err = of_property_read_u32(state_node, "exit-latency-us",
|
| 3575 | &exit_latency);
|
| 3576 | if (err) {
|
| 3577 | pr_debug(" * %pOF missing exit-latency-us property\n",
|
| 3578 | state_node);
|
| 3579 | return -EINVAL;
|
| 3580 | }
|
| 3581 |
|
| 3582 | err = of_property_read_u32(state_node, "min-residency-us", &residency);
|
| 3583 | if (!err)
|
| 3584 | genpd_state->residency_ns = 1000LL * residency;
|
| 3585 |
|
| 3586 | of_property_read_string(state_node, "idle-state-name", &genpd_state->name);
|
| 3587 |
|
| 3588 | genpd_state->power_on_latency_ns = 1000LL * exit_latency;
|
| 3589 | genpd_state->power_off_latency_ns = 1000LL * entry_latency;
|
| 3590 | genpd_state->fwnode = of_fwnode_handle(state_node);
|
| 3591 |
|
| 3592 | return 0;
|
| 3593 | }
|
| 3594 |
|
| 3595 | static int genpd_iterate_idle_states(struct device_node *dn,
|
| 3596 | struct genpd_power_state *states)
|
| 3597 | {
|
| 3598 | int ret;
|
| 3599 | struct of_phandle_iterator it;
|
| 3600 | struct device_node *np;
|
| 3601 | int i = 0;
|
| 3602 |
|
| 3603 | ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
|
| 3604 | if (ret <= 0)
|
| 3605 | return ret == -ENOENT ? 0 : ret;
|
| 3606 |
|
| 3607 |
|
| 3608 | of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
|
| 3609 | np = it.node;
|
| 3610 | if (!of_match_node(idle_state_match, np))
|
| 3611 | continue;
|
| 3612 |
|
| 3613 | if (!of_device_is_available(np))
|
| 3614 | continue;
|
| 3615 |
|
| 3616 | if (states) {
|
| 3617 | ret = genpd_parse_state(&states[i], np);
|
| 3618 | if (ret) {
|
| 3619 | pr_err("Parsing idle state node %pOF failed with err %d\n",
|
| 3620 | np, ret);
|
| 3621 | of_node_put(np);
|
| 3622 | return ret;
|
| 3623 | }
|
| 3624 | }
|
| 3625 | i++;
|
| 3626 | }
|
| 3627 |
|
| 3628 | return i;
|
| 3629 | }
|
| 3630 |
|
| 3631 | |
| 3632 | * of_genpd_parse_idle_states: Return array of idle states for the genpd.
|
| 3633 | *
|
| 3634 | * @dn: The genpd device node
|
| 3635 | * @states: The pointer to which the state array will be saved.
|
| 3636 | * @n: The count of elements in the array returned from this function.
|
| 3637 | *
|
| 3638 | * Returns the device states parsed from the OF node. The memory for the states
|
| 3639 | * is allocated by this function and is the responsibility of the caller to
|
| 3640 | * free the memory after use. If any or zero compatible domain idle states is
|
| 3641 | * found it returns 0 and in case of errors, a negative error code is returned.
|
| 3642 |
|
| 3643 | int of_genpd_parse_idle_states(struct device_node *dn,
|
| 3644 | struct genpd_power_state **states, int *n)
|
| 3645 | {
|
| 3646 | struct genpd_power_state *st;
|
| 3647 | int ret;
|
| 3648 |
|
| 3649 | ret = genpd_iterate_idle_states(dn, NULL);
|
| 3650 | if (ret < 0)
|
| 3651 | return ret;
|
| 3652 |
|
| 3653 | if (!ret) {
|
| 3654 | *states = NULL;
|
| 3655 | *n = 0;
|
| 3656 | return 0;
|
| 3657 | }
|
| 3658 |
|
| 3659 | st = kzalloc_objs(*st, ret);
|
| 3660 | if (!st)
|
| 3661 | return -ENOMEM;
|
| 3662 |
|
| 3663 | ret = genpd_iterate_idle_states(dn, st);
|
| 3664 | if (ret <= 0) {
|
| 3665 | kfree(st);
|
| 3666 | return ret < 0 ? ret : -EINVAL;
|
| 3667 | }
|
| 3668 |
|
| 3669 | *states = st;
|
| 3670 | *n = ret;
|
| 3671 |
|
| 3672 | return 0;
|
| 3673 | }
|
| 3674 | EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
|
| 3675 |
|
| 3676 | |
| 3677 | * of_genpd_sync_state() - A common sync_state function for genpd providers
|
| 3678 | * @np: The device node the genpd provider is associated with.
|
| 3679 | *
|
| 3680 | * The @np that corresponds to a genpd provider may provide one or multiple
|
| 3681 | * genpds. This function makes use @np to find the genpds that belongs to the
|
| 3682 | * provider. For each genpd we try a power-off.
|
| 3683 |
|
| 3684 | void of_genpd_sync_state(struct device_node *np)
|
| 3685 | {
|
| 3686 | struct generic_pm_domain *genpd;
|
| 3687 |
|
| 3688 | if (!np)
|
| 3689 | return;
|
| 3690 |
|
| 3691 | mutex_lock(&gpd_list_lock);
|
| 3692 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
|
| 3693 | if (genpd->provider == of_fwnode_handle(np)) {
|
| 3694 | genpd_lock(genpd);
|
| 3695 | genpd->stay_on = false;
|
| 3696 | genpd_power_off(genpd, false, 0);
|
| 3697 | genpd_unlock(genpd);
|
| 3698 | }
|
| 3699 | }
|
| 3700 | mutex_unlock(&gpd_list_lock);
|
| 3701 | }
|
| 3702 | EXPORT_SYMBOL_GPL(of_genpd_sync_state);
|
| 3703 |
|
| 3704 | static int genpd_provider_probe(struct device *dev)
|
| 3705 | {
|
| 3706 | return 0;
|
| 3707 | }
|
| 3708 |
|
| 3709 | static void genpd_provider_sync_state(struct device *dev)
|
| 3710 | {
|
| 3711 | struct generic_pm_domain *genpd = container_of(dev, struct generic_pm_domain, dev);
|
| 3712 |
|
| 3713 | switch (genpd->sync_state) {
|
| 3714 | case GENPD_SYNC_STATE_OFF:
|
| 3715 | break;
|
| 3716 |
|
| 3717 | case GENPD_SYNC_STATE_ONECELL:
|
| 3718 | of_genpd_sync_state(dev->of_node);
|
| 3719 | break;
|
| 3720 |
|
| 3721 | case GENPD_SYNC_STATE_SIMPLE:
|
| 3722 | genpd_lock(genpd);
|
| 3723 | genpd->stay_on = false;
|
| 3724 | genpd_power_off(genpd, false, 0);
|
| 3725 | genpd_unlock(genpd);
|
| 3726 | break;
|
| 3727 |
|
| 3728 | default:
|
| 3729 | break;
|
| 3730 | }
|
| 3731 | }
|
| 3732 |
|
| 3733 | static struct device_driver genpd_provider_drv = {
|
| 3734 | .name = "genpd_provider",
|
| 3735 | .bus = &genpd_provider_bus_type,
|
| 3736 | .probe = genpd_provider_probe,
|
| 3737 | .sync_state = genpd_provider_sync_state,
|
| 3738 | .suppress_bind_attrs = true,
|
| 3739 | };
|
| 3740 |
|
| 3741 | static int __init genpd_bus_init(void)
|
| 3742 | {
|
| 3743 | int ret;
|
| 3744 |
|
| 3745 | genpd_provider_bus = root_device_register("genpd_provider");
|
| 3746 | if (IS_ERR(genpd_provider_bus))
|
| 3747 | return PTR_ERR(genpd_provider_bus);
|
| 3748 |
|
| 3749 | ret = bus_register(&genpd_provider_bus_type);
|
| 3750 | if (ret)
|
| 3751 | goto err_dev;
|
| 3752 |
|
| 3753 | ret = bus_register(&genpd_bus_type);
|
| 3754 | if (ret)
|
| 3755 | goto err_prov_bus;
|
| 3756 |
|
| 3757 | ret = driver_register(&genpd_provider_drv);
|
| 3758 | if (ret)
|
| 3759 | goto err_bus;
|
| 3760 |
|
| 3761 | genpd_bus_registered = true;
|
| 3762 | return 0;
|
| 3763 |
|
| 3764 | err_bus:
|
| 3765 | bus_unregister(&genpd_bus_type);
|
| 3766 | err_prov_bus:
|
| 3767 | bus_unregister(&genpd_provider_bus_type);
|
| 3768 | err_dev:
|
| 3769 | root_device_unregister(genpd_provider_bus);
|
| 3770 | return ret;
|
| 3771 | }
|
| 3772 | core_initcall(genpd_bus_init);
|
| 3773 |
|
| 3774 | #endif
|
| 3775 |
|
| 3776 |
|
| 3777 |
|
| 3778 |
|
| 3779 | #ifdef CONFIG_DEBUG_FS
|
| 3780 | |
| 3781 | * TODO: This function is a slightly modified version of rtpm_status_show
|
| 3782 | * from sysfs.c, so generalize it.
|
| 3783 |
|
| 3784 | static void rtpm_status_str(struct seq_file *s, struct device *dev)
|
| 3785 | {
|
| 3786 | static const char * const status_lookup[] = {
|
| 3787 | [RPM_ACTIVE] = "active",
|
| 3788 | [RPM_RESUMING] = "resuming",
|
| 3789 | [RPM_SUSPENDED] = "suspended",
|
| 3790 | [RPM_SUSPENDING] = "suspending"
|
| 3791 | };
|
| 3792 | const char *p = "";
|
| 3793 |
|
| 3794 | if (dev->power.runtime_error)
|
| 3795 | p = "error";
|
| 3796 | else if (dev->power.disable_depth)
|
| 3797 | p = "unsupported";
|
| 3798 | else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
|
| 3799 | p = status_lookup[dev->power.runtime_status];
|
| 3800 | else
|
| 3801 | WARN_ON(1);
|
| 3802 |
|
| 3803 | seq_printf(s, "%-26s ", p);
|
| 3804 | }
|
| 3805 |
|
| 3806 | static void perf_status_str(struct seq_file *s, struct device *dev)
|
| 3807 | {
|
| 3808 | struct generic_pm_domain_data *gpd_data;
|
| 3809 |
|
| 3810 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
|
| 3811 |
|
| 3812 | seq_printf(s, "%-10u ", gpd_data->performance_state);
|
| 3813 | }
|
| 3814 |
|
| 3815 | static void mode_status_str(struct seq_file *s, struct device *dev)
|
| 3816 | {
|
| 3817 | struct generic_pm_domain_data *gpd_data;
|
| 3818 |
|
| 3819 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
|
| 3820 |
|
| 3821 | seq_printf(s, "%2s", gpd_data->hw_mode ? "HW" : "SW");
|
| 3822 | }
|
| 3823 |
|
| 3824 | static int genpd_summary_one(struct seq_file *s,
|
| 3825 | struct generic_pm_domain *genpd)
|
| 3826 | {
|
| 3827 | static const char * const status_lookup[] = {
|
| 3828 | [GENPD_STATE_ON] = "on",
|
| 3829 | [GENPD_STATE_OFF] = "off"
|
| 3830 | };
|
| 3831 | struct pm_domain_data *pm_data;
|
| 3832 | struct gpd_link *link;
|
| 3833 | char state[16];
|
| 3834 | int ret;
|
| 3835 |
|
| 3836 | ret = genpd_lock_interruptible(genpd);
|
| 3837 | if (ret)
|
| 3838 | return -ERESTARTSYS;
|
| 3839 |
|
| 3840 | if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
|
| 3841 | goto exit;
|
| 3842 | if (!genpd_status_on(genpd))
|
| 3843 | snprintf(state, sizeof(state), "%s-%u",
|
| 3844 | status_lookup[genpd->status], genpd->state_idx);
|
| 3845 | else
|
| 3846 | snprintf(state, sizeof(state), "%s",
|
| 3847 | status_lookup[genpd->status]);
|
| 3848 | seq_printf(s, "%-30s %-30s %u", dev_name(&genpd->dev), state, genpd->performance_state);
|
| 3849 |
|
| 3850 | |
| 3851 | * Modifications on the list require holding locks on both
|
| 3852 | * parent and child, so we are safe.
|
| 3853 | * Also the device name is immutable.
|
| 3854 |
|
| 3855 | list_for_each_entry(link, &genpd->parent_links, parent_node) {
|
| 3856 | if (list_is_first(&link->parent_node, &genpd->parent_links))
|
| 3857 | seq_printf(s, "\n%48s", " ");
|
| 3858 | seq_printf(s, "%s", link->child->name);
|
| 3859 | if (!list_is_last(&link->parent_node, &genpd->parent_links))
|
| 3860 | seq_puts(s, ", ");
|
| 3861 | }
|
| 3862 |
|
| 3863 | list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
|
| 3864 | seq_printf(s, "\n %-30s ", dev_name(pm_data->dev));
|
| 3865 | rtpm_status_str(s, pm_data->dev);
|
| 3866 | perf_status_str(s, pm_data->dev);
|
| 3867 | mode_status_str(s, pm_data->dev);
|
| 3868 | }
|
| 3869 |
|
| 3870 | seq_puts(s, "\n");
|
| 3871 | exit:
|
| 3872 | genpd_unlock(genpd);
|
| 3873 |
|
| 3874 | return 0;
|
| 3875 | }
|
| 3876 |
|
| 3877 | static int summary_show(struct seq_file *s, void *data)
|
| 3878 | {
|
| 3879 | struct generic_pm_domain *genpd;
|
| 3880 | int ret = 0;
|
| 3881 |
|
| 3882 | seq_puts(s, "domain status children performance\n");
|
| 3883 | seq_puts(s, " /device runtime status managed by\n");
|
| 3884 | seq_puts(s, "------------------------------------------------------------------------------\n");
|
| 3885 |
|
| 3886 | ret = mutex_lock_interruptible(&gpd_list_lock);
|
| 3887 | if (ret)
|
| 3888 | return -ERESTARTSYS;
|
| 3889 |
|
| 3890 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
|
| 3891 | ret = genpd_summary_one(s, genpd);
|
| 3892 | if (ret)
|
| 3893 | break;
|
| 3894 | }
|
| 3895 | mutex_unlock(&gpd_list_lock);
|
| 3896 |
|
| 3897 | return ret;
|
| 3898 | }
|
| 3899 |
|
| 3900 | static int status_show(struct seq_file *s, void *data)
|
| 3901 | {
|
| 3902 | static const char * const status_lookup[] = {
|
| 3903 | [GENPD_STATE_ON] = "on",
|
| 3904 | [GENPD_STATE_OFF] = "off"
|
| 3905 | };
|
| 3906 |
|
| 3907 | struct generic_pm_domain *genpd = s->private;
|
| 3908 | int ret = 0;
|
| 3909 |
|
| 3910 | ret = genpd_lock_interruptible(genpd);
|
| 3911 | if (ret)
|
| 3912 | return -ERESTARTSYS;
|
| 3913 |
|
| 3914 | if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
|
| 3915 | goto exit;
|
| 3916 |
|
| 3917 | if (genpd->status == GENPD_STATE_OFF)
|
| 3918 | seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
|
| 3919 | genpd->state_idx);
|
| 3920 | else
|
| 3921 | seq_printf(s, "%s\n", status_lookup[genpd->status]);
|
| 3922 | exit:
|
| 3923 | genpd_unlock(genpd);
|
| 3924 | return ret;
|
| 3925 | }
|
| 3926 |
|
| 3927 | static int sub_domains_show(struct seq_file *s, void *data)
|
| 3928 | {
|
| 3929 | struct generic_pm_domain *genpd = s->private;
|
| 3930 | struct gpd_link *link;
|
| 3931 | int ret = 0;
|
| 3932 |
|
| 3933 | ret = genpd_lock_interruptible(genpd);
|
| 3934 | if (ret)
|
| 3935 | return -ERESTARTSYS;
|
| 3936 |
|
| 3937 | list_for_each_entry(link, &genpd->parent_links, parent_node)
|
| 3938 | seq_printf(s, "%s\n", link->child->name);
|
| 3939 |
|
| 3940 | genpd_unlock(genpd);
|
| 3941 | return ret;
|
| 3942 | }
|
| 3943 |
|
| 3944 | static int idle_states_show(struct seq_file *s, void *data)
|
| 3945 | {
|
| 3946 | struct generic_pm_domain *genpd = s->private;
|
| 3947 | u64 now, delta, idle_time = 0;
|
| 3948 | unsigned int i;
|
| 3949 | int ret = 0;
|
| 3950 |
|
| 3951 | ret = genpd_lock_interruptible(genpd);
|
| 3952 | if (ret)
|
| 3953 | return -ERESTARTSYS;
|
| 3954 |
|
| 3955 | seq_puts(s, "State Time(ms) Usage Rejected Above Below S2idle\n");
|
| 3956 |
|
| 3957 | for (i = 0; i < genpd->state_count; i++) {
|
| 3958 | struct genpd_power_state *state = &genpd->states[i];
|
| 3959 | char state_name[7];
|
| 3960 |
|
| 3961 | idle_time += state->idle_time;
|
| 3962 |
|
| 3963 | if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
|
| 3964 | now = ktime_get_mono_fast_ns();
|
| 3965 | if (now > genpd->accounting_time) {
|
| 3966 | delta = now - genpd->accounting_time;
|
| 3967 | idle_time += delta;
|
| 3968 | }
|
| 3969 | }
|
| 3970 |
|
| 3971 | snprintf(state_name, ARRAY_SIZE(state_name), "S%-5d", i);
|
| 3972 | do_div(idle_time, NSEC_PER_MSEC);
|
| 3973 | seq_printf(s, "%-6s %-14llu %-10llu %-10llu %-10llu %-10llu %llu\n",
|
| 3974 | state_name, idle_time, state->usage, state->rejected,
|
| 3975 | state->above, state->below, state->usage_s2idle);
|
| 3976 | }
|
| 3977 |
|
| 3978 | genpd_unlock(genpd);
|
| 3979 | return ret;
|
| 3980 | }
|
| 3981 |
|
| 3982 | static int idle_states_desc_show(struct seq_file *s, void *data)
|
| 3983 | {
|
| 3984 | struct generic_pm_domain *genpd = s->private;
|
| 3985 | unsigned int i;
|
| 3986 | int ret = 0;
|
| 3987 |
|
| 3988 | ret = genpd_lock_interruptible(genpd);
|
| 3989 | if (ret)
|
| 3990 | return -ERESTARTSYS;
|
| 3991 |
|
| 3992 | seq_puts(s, "State Latency(us) Residency(us) Name\n");
|
| 3993 |
|
| 3994 | for (i = 0; i < genpd->state_count; i++) {
|
| 3995 | struct genpd_power_state *state = &genpd->states[i];
|
| 3996 | u64 latency, residency;
|
| 3997 | char state_name[7];
|
| 3998 |
|
| 3999 | latency = state->power_off_latency_ns +
|
| 4000 | state->power_on_latency_ns;
|
| 4001 | do_div(latency, NSEC_PER_USEC);
|
| 4002 |
|
| 4003 | residency = state->residency_ns;
|
| 4004 | do_div(residency, NSEC_PER_USEC);
|
| 4005 |
|
| 4006 | snprintf(state_name, ARRAY_SIZE(state_name), "S%-5d", i);
|
| 4007 | seq_printf(s, "%-6s %-12llu %-14llu %s\n",
|
| 4008 | state_name, latency, residency,
|
| 4009 | state->name ?: "N/A");
|
| 4010 | }
|
| 4011 |
|
| 4012 | genpd_unlock(genpd);
|
| 4013 | return ret;
|
| 4014 | }
|
| 4015 |
|
| 4016 | static int active_time_show(struct seq_file *s, void *data)
|
| 4017 | {
|
| 4018 | struct generic_pm_domain *genpd = s->private;
|
| 4019 | u64 now, on_time, delta = 0;
|
| 4020 | int ret = 0;
|
| 4021 |
|
| 4022 | ret = genpd_lock_interruptible(genpd);
|
| 4023 | if (ret)
|
| 4024 | return -ERESTARTSYS;
|
| 4025 |
|
| 4026 | if (genpd->status == GENPD_STATE_ON) {
|
| 4027 | now = ktime_get_mono_fast_ns();
|
| 4028 | if (now > genpd->accounting_time)
|
| 4029 | delta = now - genpd->accounting_time;
|
| 4030 | }
|
| 4031 |
|
| 4032 | on_time = genpd->on_time + delta;
|
| 4033 | do_div(on_time, NSEC_PER_MSEC);
|
| 4034 | seq_printf(s, "%llu ms\n", on_time);
|
| 4035 |
|
| 4036 | genpd_unlock(genpd);
|
| 4037 | return ret;
|
| 4038 | }
|
| 4039 |
|
| 4040 | static int total_idle_time_show(struct seq_file *s, void *data)
|
| 4041 | {
|
| 4042 | struct generic_pm_domain *genpd = s->private;
|
| 4043 | u64 now, delta, total = 0;
|
| 4044 | unsigned int i;
|
| 4045 | int ret = 0;
|
| 4046 |
|
| 4047 | ret = genpd_lock_interruptible(genpd);
|
| 4048 | if (ret)
|
| 4049 | return -ERESTARTSYS;
|
| 4050 |
|
| 4051 | for (i = 0; i < genpd->state_count; i++) {
|
| 4052 | total += genpd->states[i].idle_time;
|
| 4053 |
|
| 4054 | if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
|
| 4055 | now = ktime_get_mono_fast_ns();
|
| 4056 | if (now > genpd->accounting_time) {
|
| 4057 | delta = now - genpd->accounting_time;
|
| 4058 | total += delta;
|
| 4059 | }
|
| 4060 | }
|
| 4061 | }
|
| 4062 |
|
| 4063 | do_div(total, NSEC_PER_MSEC);
|
| 4064 | seq_printf(s, "%llu ms\n", total);
|
| 4065 |
|
| 4066 | genpd_unlock(genpd);
|
| 4067 | return ret;
|
| 4068 | }
|
| 4069 |
|
| 4070 |
|
| 4071 | static int devices_show(struct seq_file *s, void *data)
|
| 4072 | {
|
| 4073 | struct generic_pm_domain *genpd = s->private;
|
| 4074 | struct pm_domain_data *pm_data;
|
| 4075 | int ret = 0;
|
| 4076 |
|
| 4077 | ret = genpd_lock_interruptible(genpd);
|
| 4078 | if (ret)
|
| 4079 | return -ERESTARTSYS;
|
| 4080 |
|
| 4081 | list_for_each_entry(pm_data, &genpd->dev_list, list_node)
|
| 4082 | seq_printf(s, "%s\n", dev_name(pm_data->dev));
|
| 4083 |
|
| 4084 | genpd_unlock(genpd);
|
| 4085 | return ret;
|
| 4086 | }
|
| 4087 |
|
| 4088 | static int perf_state_show(struct seq_file *s, void *data)
|
| 4089 | {
|
| 4090 | struct generic_pm_domain *genpd = s->private;
|
| 4091 |
|
| 4092 | if (genpd_lock_interruptible(genpd))
|
| 4093 | return -ERESTARTSYS;
|
| 4094 |
|
| 4095 | seq_printf(s, "%u\n", genpd->performance_state);
|
| 4096 |
|
| 4097 | genpd_unlock(genpd);
|
| 4098 | return 0;
|
| 4099 | }
|
| 4100 |
|
| 4101 | DEFINE_SHOW_ATTRIBUTE(summary);
|
| 4102 | DEFINE_SHOW_ATTRIBUTE(status);
|
| 4103 | DEFINE_SHOW_ATTRIBUTE(sub_domains);
|
| 4104 | DEFINE_SHOW_ATTRIBUTE(idle_states);
|
| 4105 | DEFINE_SHOW_ATTRIBUTE(idle_states_desc);
|
| 4106 | DEFINE_SHOW_ATTRIBUTE(active_time);
|
| 4107 | DEFINE_SHOW_ATTRIBUTE(total_idle_time);
|
| 4108 | DEFINE_SHOW_ATTRIBUTE(devices);
|
| 4109 | DEFINE_SHOW_ATTRIBUTE(perf_state);
|
| 4110 |
|
| 4111 | static void genpd_debug_add(struct generic_pm_domain *genpd)
|
| 4112 | {
|
| 4113 | struct dentry *d;
|
| 4114 |
|
| 4115 | if (!genpd_debugfs_dir)
|
| 4116 | return;
|
| 4117 |
|
| 4118 | d = debugfs_create_dir(dev_name(&genpd->dev), genpd_debugfs_dir);
|
| 4119 |
|
| 4120 | debugfs_create_file("current_state", 0444,
|
| 4121 | d, genpd, &status_fops);
|
| 4122 | debugfs_create_file("sub_domains", 0444,
|
| 4123 | d, genpd, &sub_domains_fops);
|
| 4124 | debugfs_create_file("idle_states", 0444,
|
| 4125 | d, genpd, &idle_states_fops);
|
| 4126 | debugfs_create_file("idle_states_desc", 0444,
|
| 4127 | d, genpd, &idle_states_desc_fops);
|
| 4128 | debugfs_create_file("active_time", 0444,
|
| 4129 | d, genpd, &active_time_fops);
|
| 4130 | debugfs_create_file("total_idle_time", 0444,
|
| 4131 | d, genpd, &total_idle_time_fops);
|
| 4132 | debugfs_create_file("devices", 0444,
|
| 4133 | d, genpd, &devices_fops);
|
| 4134 | if (genpd->set_performance_state)
|
| 4135 | debugfs_create_file("perf_state", 0444,
|
| 4136 | d, genpd, &perf_state_fops);
|
| 4137 | }
|
| 4138 |
|
| 4139 | static int __init genpd_debug_init(void)
|
| 4140 | {
|
| 4141 | struct generic_pm_domain *genpd;
|
| 4142 |
|
| 4143 | genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
|
| 4144 |
|
| 4145 | debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
|
| 4146 | NULL, &summary_fops);
|
| 4147 |
|
| 4148 | list_for_each_entry(genpd, &gpd_list, gpd_list_node)
|
| 4149 | genpd_debug_add(genpd);
|
| 4150 |
|
| 4151 | return 0;
|
| 4152 | }
|
| 4153 | late_initcall(genpd_debug_init);
|
| 4154 |
|
| 4155 | static void __exit genpd_debug_exit(void)
|
| 4156 | {
|
| 4157 | debugfs_remove_recursive(genpd_debugfs_dir);
|
| 4158 | }
|
| 4159 | __exitcall(genpd_debug_exit);
|
| 4160 | #endif
|