| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * drivers/base/power/main.c - Where the driver meets power management. | 
|  | 3 | * | 
|  | 4 | * Copyright (c) 2003 Patrick Mochel | 
|  | 5 | * Copyright (c) 2003 Open Source Development Lab | 
|  | 6 | * | 
|  | 7 | * This file is released under the GPLv2 | 
|  | 8 | * | 
|  | 9 | * | 
|  | 10 | * The driver model core calls device_pm_add() when a device is registered. | 
|  | 11 | * This will initialize the embedded device_pm_info object in the device | 
|  | 12 | * and add it to the list of power-controlled devices. sysfs entries for | 
|  | 13 | * controlling device power management will also be added. | 
|  | 14 | * | 
|  | 15 | * A separate list is used for keeping track of power info, because the power | 
|  | 16 | * domain dependencies may differ from the ancestral dependencies that the | 
|  | 17 | * subsystem list maintains. | 
|  | 18 | */ | 
|  | 19 |  | 
|  | 20 | #include <linux/device.h> | 
|  | 21 | #include <linux/export.h> | 
|  | 22 | #include <linux/mutex.h> | 
|  | 23 | #include <linux/pm.h> | 
|  | 24 | #include <linux/pm_runtime.h> | 
|  | 25 | #include <linux/pm-trace.h> | 
|  | 26 | #include <linux/pm_wakeirq.h> | 
|  | 27 | #include <linux/interrupt.h> | 
|  | 28 | #include <linux/sched.h> | 
|  | 29 | #include <linux/sched/debug.h> | 
|  | 30 | #include <linux/async.h> | 
|  | 31 | #include <linux/suspend.h> | 
|  | 32 | #include <trace/events/power.h> | 
|  | 33 | #include <linux/cpufreq.h> | 
|  | 34 | #include <linux/cpuidle.h> | 
|  | 35 | #include <linux/timer.h> | 
|  | 36 | #include <linux/wakeup_reason.h> | 
|  | 37 |  | 
|  | 38 | #include "../base.h" | 
|  | 39 | #include "power.h" | 
|  | 40 |  | 
|  | 41 | typedef int (*pm_callback_t)(struct device *); | 
|  | 42 |  | 
|  | 43 | /* | 
|  | 44 | * The entries in the dpm_list list are in a depth first order, simply | 
|  | 45 | * because children are guaranteed to be discovered after parents, and | 
|  | 46 | * are inserted at the back of the list on discovery. | 
|  | 47 | * | 
|  | 48 | * Since device_pm_add() may be called with a device lock held, | 
|  | 49 | * we must never try to acquire a device lock while holding | 
|  | 50 | * dpm_list_mutex. | 
|  | 51 | */ | 
|  | 52 |  | 
|  | 53 | LIST_HEAD(dpm_list); | 
|  | 54 | static LIST_HEAD(dpm_prepared_list); | 
|  | 55 | static LIST_HEAD(dpm_suspended_list); | 
|  | 56 | static LIST_HEAD(dpm_late_early_list); | 
|  | 57 | static LIST_HEAD(dpm_noirq_list); | 
|  | 58 |  | 
|  | 59 | struct suspend_stats suspend_stats; | 
|  | 60 | static DEFINE_MUTEX(dpm_list_mtx); | 
|  | 61 | static pm_message_t pm_transition; | 
|  | 62 |  | 
|  | 63 | static int async_error; | 
|  | 64 |  | 
|  | 65 | static const char *pm_verb(int event) | 
|  | 66 | { | 
|  | 67 | switch (event) { | 
|  | 68 | case PM_EVENT_SUSPEND: | 
|  | 69 | return "suspend"; | 
|  | 70 | case PM_EVENT_RESUME: | 
|  | 71 | return "resume"; | 
|  | 72 | case PM_EVENT_FREEZE: | 
|  | 73 | return "freeze"; | 
|  | 74 | case PM_EVENT_QUIESCE: | 
|  | 75 | return "quiesce"; | 
|  | 76 | case PM_EVENT_HIBERNATE: | 
|  | 77 | return "hibernate"; | 
|  | 78 | case PM_EVENT_THAW: | 
|  | 79 | return "thaw"; | 
|  | 80 | case PM_EVENT_RESTORE: | 
|  | 81 | return "restore"; | 
|  | 82 | case PM_EVENT_RECOVER: | 
|  | 83 | return "recover"; | 
|  | 84 | default: | 
|  | 85 | return "(unknown PM event)"; | 
|  | 86 | } | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | /** | 
|  | 90 | * device_pm_sleep_init - Initialize system suspend-related device fields. | 
|  | 91 | * @dev: Device object being initialized. | 
|  | 92 | */ | 
|  | 93 | void device_pm_sleep_init(struct device *dev) | 
|  | 94 | { | 
|  | 95 | dev->power.is_prepared = false; | 
|  | 96 | dev->power.is_suspended = false; | 
|  | 97 | dev->power.is_noirq_suspended = false; | 
|  | 98 | dev->power.is_late_suspended = false; | 
|  | 99 | init_completion(&dev->power.completion); | 
|  | 100 | complete_all(&dev->power.completion); | 
|  | 101 | dev->power.wakeup = NULL; | 
|  | 102 | INIT_LIST_HEAD(&dev->power.entry); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | /** | 
|  | 106 | * device_pm_lock - Lock the list of active devices used by the PM core. | 
|  | 107 | */ | 
|  | 108 | void device_pm_lock(void) | 
|  | 109 | { | 
|  | 110 | mutex_lock(&dpm_list_mtx); | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | /** | 
|  | 114 | * device_pm_unlock - Unlock the list of active devices used by the PM core. | 
|  | 115 | */ | 
|  | 116 | void device_pm_unlock(void) | 
|  | 117 | { | 
|  | 118 | mutex_unlock(&dpm_list_mtx); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /** | 
|  | 122 | * device_pm_add - Add a device to the PM core's list of active devices. | 
|  | 123 | * @dev: Device to add to the list. | 
|  | 124 | */ | 
|  | 125 | void device_pm_add(struct device *dev) | 
|  | 126 | { | 
|  | 127 | /* Skip PM setup/initialization. */ | 
|  | 128 | if (device_pm_not_required(dev)) | 
|  | 129 | return; | 
|  | 130 |  | 
|  | 131 | pr_debug("PM: Adding info for %s:%s\n", | 
|  | 132 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); | 
|  | 133 | device_pm_check_callbacks(dev); | 
|  | 134 | mutex_lock(&dpm_list_mtx); | 
|  | 135 | if (dev->parent && dev->parent->power.is_prepared) | 
|  | 136 | dev_warn(dev, "parent %s should not be sleeping\n", | 
|  | 137 | dev_name(dev->parent)); | 
|  | 138 | list_add_tail(&dev->power.entry, &dpm_list); | 
|  | 139 | dev->power.in_dpm_list = true; | 
|  | 140 | mutex_unlock(&dpm_list_mtx); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | /** | 
|  | 144 | * device_pm_remove - Remove a device from the PM core's list of active devices. | 
|  | 145 | * @dev: Device to be removed from the list. | 
|  | 146 | */ | 
|  | 147 | void device_pm_remove(struct device *dev) | 
|  | 148 | { | 
|  | 149 | if (device_pm_not_required(dev)) | 
|  | 150 | return; | 
|  | 151 |  | 
|  | 152 | pr_debug("PM: Removing info for %s:%s\n", | 
|  | 153 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); | 
|  | 154 | complete_all(&dev->power.completion); | 
|  | 155 | mutex_lock(&dpm_list_mtx); | 
|  | 156 | list_del_init(&dev->power.entry); | 
|  | 157 | dev->power.in_dpm_list = false; | 
|  | 158 | mutex_unlock(&dpm_list_mtx); | 
|  | 159 | device_wakeup_disable(dev); | 
|  | 160 | pm_runtime_remove(dev); | 
|  | 161 | device_pm_check_callbacks(dev); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | /** | 
|  | 165 | * device_pm_move_before - Move device in the PM core's list of active devices. | 
|  | 166 | * @deva: Device to move in dpm_list. | 
|  | 167 | * @devb: Device @deva should come before. | 
|  | 168 | */ | 
|  | 169 | void device_pm_move_before(struct device *deva, struct device *devb) | 
|  | 170 | { | 
|  | 171 | pr_debug("PM: Moving %s:%s before %s:%s\n", | 
|  | 172 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), | 
|  | 173 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); | 
|  | 174 | /* Delete deva from dpm_list and reinsert before devb. */ | 
|  | 175 | list_move_tail(&deva->power.entry, &devb->power.entry); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | /** | 
|  | 179 | * device_pm_move_after - Move device in the PM core's list of active devices. | 
|  | 180 | * @deva: Device to move in dpm_list. | 
|  | 181 | * @devb: Device @deva should come after. | 
|  | 182 | */ | 
|  | 183 | void device_pm_move_after(struct device *deva, struct device *devb) | 
|  | 184 | { | 
|  | 185 | pr_debug("PM: Moving %s:%s after %s:%s\n", | 
|  | 186 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), | 
|  | 187 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); | 
|  | 188 | /* Delete deva from dpm_list and reinsert after devb. */ | 
|  | 189 | list_move(&deva->power.entry, &devb->power.entry); | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | /** | 
|  | 193 | * device_pm_move_last - Move device to end of the PM core's list of devices. | 
|  | 194 | * @dev: Device to move in dpm_list. | 
|  | 195 | */ | 
|  | 196 | void device_pm_move_last(struct device *dev) | 
|  | 197 | { | 
|  | 198 | pr_debug("PM: Moving %s:%s to end of list\n", | 
|  | 199 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); | 
|  | 200 | list_move_tail(&dev->power.entry, &dpm_list); | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | static ktime_t initcall_debug_start(struct device *dev, void *cb) | 
|  | 204 | { | 
|  | 205 | if (!pm_print_times_enabled) | 
|  | 206 | return 0; | 
|  | 207 |  | 
|  | 208 | dev_info(dev, "calling %pF @ %i, parent: %s\n", cb, | 
|  | 209 | task_pid_nr(current), | 
|  | 210 | dev->parent ? dev_name(dev->parent) : "none"); | 
|  | 211 | return ktime_get(); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | static void initcall_debug_report(struct device *dev, ktime_t calltime, | 
|  | 215 | void *cb, int error) | 
|  | 216 | { | 
|  | 217 | ktime_t rettime; | 
|  | 218 | s64 nsecs; | 
|  | 219 |  | 
|  | 220 | if (!pm_print_times_enabled) | 
|  | 221 | return; | 
|  | 222 |  | 
|  | 223 | rettime = ktime_get(); | 
|  | 224 | nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime)); | 
|  | 225 |  | 
|  | 226 | dev_info(dev, "%pF returned %d after %Ld usecs\n", cb, error, | 
|  | 227 | (unsigned long long)nsecs >> 10); | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | /** | 
|  | 231 | * dpm_wait - Wait for a PM operation to complete. | 
|  | 232 | * @dev: Device to wait for. | 
|  | 233 | * @async: If unset, wait only if the device's power.async_suspend flag is set. | 
|  | 234 | */ | 
|  | 235 | static void dpm_wait(struct device *dev, bool async) | 
|  | 236 | { | 
|  | 237 | if (!dev) | 
|  | 238 | return; | 
|  | 239 |  | 
|  | 240 | if (async || (pm_async_enabled && dev->power.async_suspend)) | 
|  | 241 | wait_for_completion(&dev->power.completion); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | static int dpm_wait_fn(struct device *dev, void *async_ptr) | 
|  | 245 | { | 
|  | 246 | dpm_wait(dev, *((bool *)async_ptr)); | 
|  | 247 | return 0; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | static void dpm_wait_for_children(struct device *dev, bool async) | 
|  | 251 | { | 
|  | 252 | device_for_each_child(dev, &async, dpm_wait_fn); | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | static void dpm_wait_for_suppliers(struct device *dev, bool async) | 
|  | 256 | { | 
|  | 257 | struct device_link *link; | 
|  | 258 | int idx; | 
|  | 259 |  | 
|  | 260 | idx = device_links_read_lock(); | 
|  | 261 |  | 
|  | 262 | /* | 
|  | 263 | * If the supplier goes away right after we've checked the link to it, | 
|  | 264 | * we'll wait for its completion to change the state, but that's fine, | 
|  | 265 | * because the only things that will block as a result are the SRCU | 
|  | 266 | * callbacks freeing the link objects for the links in the list we're | 
|  | 267 | * walking. | 
|  | 268 | */ | 
|  | 269 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) | 
|  | 270 | if (READ_ONCE(link->status) != DL_STATE_DORMANT) | 
|  | 271 | dpm_wait(link->supplier, async); | 
|  | 272 |  | 
|  | 273 | device_links_read_unlock(idx); | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | static void dpm_wait_for_superior(struct device *dev, bool async) | 
|  | 277 | { | 
|  | 278 | dpm_wait(dev->parent, async); | 
|  | 279 | dpm_wait_for_suppliers(dev, async); | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | static void dpm_wait_for_consumers(struct device *dev, bool async) | 
|  | 283 | { | 
|  | 284 | struct device_link *link; | 
|  | 285 | int idx; | 
|  | 286 |  | 
|  | 287 | idx = device_links_read_lock(); | 
|  | 288 |  | 
|  | 289 | /* | 
|  | 290 | * The status of a device link can only be changed from "dormant" by a | 
|  | 291 | * probe, but that cannot happen during system suspend/resume.  In | 
|  | 292 | * theory it can change to "dormant" at that time, but then it is | 
|  | 293 | * reasonable to wait for the target device anyway (eg. if it goes | 
|  | 294 | * away, it's better to wait for it to go away completely and then | 
|  | 295 | * continue instead of trying to continue in parallel with its | 
|  | 296 | * unregistration). | 
|  | 297 | */ | 
|  | 298 | list_for_each_entry_rcu(link, &dev->links.consumers, s_node) | 
|  | 299 | if (READ_ONCE(link->status) != DL_STATE_DORMANT) | 
|  | 300 | dpm_wait(link->consumer, async); | 
|  | 301 |  | 
|  | 302 | device_links_read_unlock(idx); | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | static void dpm_wait_for_subordinate(struct device *dev, bool async) | 
|  | 306 | { | 
|  | 307 | dpm_wait_for_children(dev, async); | 
|  | 308 | dpm_wait_for_consumers(dev, async); | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | /** | 
|  | 312 | * pm_op - Return the PM operation appropriate for given PM event. | 
|  | 313 | * @ops: PM operations to choose from. | 
|  | 314 | * @state: PM transition of the system being carried out. | 
|  | 315 | */ | 
|  | 316 | static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state) | 
|  | 317 | { | 
|  | 318 | switch (state.event) { | 
|  | 319 | #ifdef CONFIG_SUSPEND | 
|  | 320 | case PM_EVENT_SUSPEND: | 
|  | 321 | return ops->suspend; | 
|  | 322 | case PM_EVENT_RESUME: | 
|  | 323 | return ops->resume; | 
|  | 324 | #endif /* CONFIG_SUSPEND */ | 
|  | 325 | #ifdef CONFIG_HIBERNATE_CALLBACKS | 
|  | 326 | case PM_EVENT_FREEZE: | 
|  | 327 | case PM_EVENT_QUIESCE: | 
|  | 328 | return ops->freeze; | 
|  | 329 | case PM_EVENT_HIBERNATE: | 
|  | 330 | return ops->poweroff; | 
|  | 331 | case PM_EVENT_THAW: | 
|  | 332 | case PM_EVENT_RECOVER: | 
|  | 333 | return ops->thaw; | 
|  | 334 | break; | 
|  | 335 | case PM_EVENT_RESTORE: | 
|  | 336 | return ops->restore; | 
|  | 337 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | return NULL; | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | /** | 
|  | 344 | * pm_late_early_op - Return the PM operation appropriate for given PM event. | 
|  | 345 | * @ops: PM operations to choose from. | 
|  | 346 | * @state: PM transition of the system being carried out. | 
|  | 347 | * | 
|  | 348 | * Runtime PM is disabled for @dev while this function is being executed. | 
|  | 349 | */ | 
|  | 350 | static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops, | 
|  | 351 | pm_message_t state) | 
|  | 352 | { | 
|  | 353 | switch (state.event) { | 
|  | 354 | #ifdef CONFIG_SUSPEND | 
|  | 355 | case PM_EVENT_SUSPEND: | 
|  | 356 | return ops->suspend_late; | 
|  | 357 | case PM_EVENT_RESUME: | 
|  | 358 | return ops->resume_early; | 
|  | 359 | #endif /* CONFIG_SUSPEND */ | 
|  | 360 | #ifdef CONFIG_HIBERNATE_CALLBACKS | 
|  | 361 | case PM_EVENT_FREEZE: | 
|  | 362 | case PM_EVENT_QUIESCE: | 
|  | 363 | return ops->freeze_late; | 
|  | 364 | case PM_EVENT_HIBERNATE: | 
|  | 365 | return ops->poweroff_late; | 
|  | 366 | case PM_EVENT_THAW: | 
|  | 367 | case PM_EVENT_RECOVER: | 
|  | 368 | return ops->thaw_early; | 
|  | 369 | case PM_EVENT_RESTORE: | 
|  | 370 | return ops->restore_early; | 
|  | 371 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | return NULL; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | /** | 
|  | 378 | * pm_noirq_op - Return the PM operation appropriate for given PM event. | 
|  | 379 | * @ops: PM operations to choose from. | 
|  | 380 | * @state: PM transition of the system being carried out. | 
|  | 381 | * | 
|  | 382 | * The driver of @dev will not receive interrupts while this function is being | 
|  | 383 | * executed. | 
|  | 384 | */ | 
|  | 385 | static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state) | 
|  | 386 | { | 
|  | 387 | switch (state.event) { | 
|  | 388 | #ifdef CONFIG_SUSPEND | 
|  | 389 | case PM_EVENT_SUSPEND: | 
|  | 390 | return ops->suspend_noirq; | 
|  | 391 | case PM_EVENT_RESUME: | 
|  | 392 | return ops->resume_noirq; | 
|  | 393 | #endif /* CONFIG_SUSPEND */ | 
|  | 394 | #ifdef CONFIG_HIBERNATE_CALLBACKS | 
|  | 395 | case PM_EVENT_FREEZE: | 
|  | 396 | case PM_EVENT_QUIESCE: | 
|  | 397 | return ops->freeze_noirq; | 
|  | 398 | case PM_EVENT_HIBERNATE: | 
|  | 399 | return ops->poweroff_noirq; | 
|  | 400 | case PM_EVENT_THAW: | 
|  | 401 | case PM_EVENT_RECOVER: | 
|  | 402 | return ops->thaw_noirq; | 
|  | 403 | case PM_EVENT_RESTORE: | 
|  | 404 | return ops->restore_noirq; | 
|  | 405 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | return NULL; | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info) | 
|  | 412 | { | 
|  | 413 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), | 
|  | 414 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? | 
|  | 415 | ", may wakeup" : ""); | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | static void pm_dev_err(struct device *dev, pm_message_t state, const char *info, | 
|  | 419 | int error) | 
|  | 420 | { | 
|  | 421 | printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n", | 
|  | 422 | dev_name(dev), pm_verb(state.event), info, error); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | static void dpm_show_time(ktime_t starttime, pm_message_t state, int error, | 
|  | 426 | const char *info) | 
|  | 427 | { | 
|  | 428 | ktime_t calltime; | 
|  | 429 | u64 usecs64; | 
|  | 430 | int usecs; | 
|  | 431 |  | 
|  | 432 | calltime = ktime_get(); | 
|  | 433 | usecs64 = ktime_to_ns(ktime_sub(calltime, starttime)); | 
|  | 434 | do_div(usecs64, NSEC_PER_USEC); | 
|  | 435 | usecs = usecs64; | 
|  | 436 | if (usecs == 0) | 
|  | 437 | usecs = 1; | 
|  | 438 |  | 
|  | 439 | pr_info("%s%s%s of devices %s after %ld.%03ld msecs\n", | 
|  | 440 | info ?: "", info ? " " : "", pm_verb(state.event), | 
|  | 441 | error ? "aborted" : "complete", | 
|  | 442 | usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC); | 
|  | 443 | } | 
|  | 444 |  | 
|  | 445 | static int dpm_run_callback(pm_callback_t cb, struct device *dev, | 
|  | 446 | pm_message_t state, const char *info) | 
|  | 447 | { | 
|  | 448 | ktime_t calltime; | 
|  | 449 | int error; | 
|  | 450 |  | 
|  | 451 | if (!cb) | 
|  | 452 | return 0; | 
|  | 453 |  | 
|  | 454 | calltime = initcall_debug_start(dev, cb); | 
|  | 455 |  | 
|  | 456 | pm_dev_dbg(dev, state, info); | 
|  | 457 | trace_device_pm_callback_start(dev, info, state.event); | 
|  | 458 | error = cb(dev); | 
|  | 459 | trace_device_pm_callback_end(dev, error); | 
|  | 460 | suspend_report_result(cb, error); | 
|  | 461 |  | 
|  | 462 | initcall_debug_report(dev, calltime, cb, error); | 
|  | 463 |  | 
|  | 464 | return error; | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | #ifdef CONFIG_DPM_WATCHDOG | 
|  | 468 | struct dpm_watchdog { | 
|  | 469 | struct device		*dev; | 
|  | 470 | struct task_struct	*tsk; | 
|  | 471 | struct timer_list	timer; | 
|  | 472 | }; | 
|  | 473 |  | 
|  | 474 | #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \ | 
|  | 475 | struct dpm_watchdog wd | 
|  | 476 |  | 
|  | 477 | /** | 
|  | 478 | * dpm_watchdog_handler - Driver suspend / resume watchdog handler. | 
|  | 479 | * @data: Watchdog object address. | 
|  | 480 | * | 
|  | 481 | * Called when a driver has timed out suspending or resuming. | 
|  | 482 | * There's not much we can do here to recover so panic() to | 
|  | 483 | * capture a crash-dump in pstore. | 
|  | 484 | */ | 
|  | 485 | static void dpm_watchdog_handler(struct timer_list *t) | 
|  | 486 | { | 
|  | 487 | struct dpm_watchdog *wd = from_timer(wd, t, timer); | 
|  | 488 |  | 
|  | 489 | dev_emerg(wd->dev, "**** DPM device timeout ****\n"); | 
|  | 490 | show_stack(wd->tsk, NULL); | 
|  | 491 | panic("%s %s: unrecoverable failure\n", | 
|  | 492 | dev_driver_string(wd->dev), dev_name(wd->dev)); | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | /** | 
|  | 496 | * dpm_watchdog_set - Enable pm watchdog for given device. | 
|  | 497 | * @wd: Watchdog. Must be allocated on the stack. | 
|  | 498 | * @dev: Device to handle. | 
|  | 499 | */ | 
|  | 500 | static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev) | 
|  | 501 | { | 
|  | 502 | struct timer_list *timer = &wd->timer; | 
|  | 503 |  | 
|  | 504 | wd->dev = dev; | 
|  | 505 | wd->tsk = current; | 
|  | 506 |  | 
|  | 507 | timer_setup_on_stack(timer, dpm_watchdog_handler, 0); | 
|  | 508 | /* use same timeout value for both suspend and resume */ | 
|  | 509 | timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT; | 
|  | 510 | add_timer(timer); | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | /** | 
|  | 514 | * dpm_watchdog_clear - Disable suspend/resume watchdog. | 
|  | 515 | * @wd: Watchdog to disable. | 
|  | 516 | */ | 
|  | 517 | static void dpm_watchdog_clear(struct dpm_watchdog *wd) | 
|  | 518 | { | 
|  | 519 | struct timer_list *timer = &wd->timer; | 
|  | 520 |  | 
|  | 521 | del_timer_sync(timer); | 
|  | 522 | destroy_timer_on_stack(timer); | 
|  | 523 | } | 
|  | 524 | #else | 
|  | 525 | #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) | 
|  | 526 | #define dpm_watchdog_set(x, y) | 
|  | 527 | #define dpm_watchdog_clear(x) | 
|  | 528 | #endif | 
|  | 529 |  | 
|  | 530 | /*------------------------- Resume routines -------------------------*/ | 
|  | 531 |  | 
|  | 532 | /** | 
|  | 533 | * dev_pm_skip_next_resume_phases - Skip next system resume phases for device. | 
|  | 534 | * @dev: Target device. | 
|  | 535 | * | 
|  | 536 | * Make the core skip the "early resume" and "resume" phases for @dev. | 
|  | 537 | * | 
|  | 538 | * This function can be called by middle-layer code during the "noirq" phase of | 
|  | 539 | * system resume if necessary, but not by device drivers. | 
|  | 540 | */ | 
|  | 541 | void dev_pm_skip_next_resume_phases(struct device *dev) | 
|  | 542 | { | 
|  | 543 | dev->power.is_late_suspended = false; | 
|  | 544 | dev->power.is_suspended = false; | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | /** | 
|  | 548 | * suspend_event - Return a "suspend" message for given "resume" one. | 
|  | 549 | * @resume_msg: PM message representing a system-wide resume transition. | 
|  | 550 | */ | 
|  | 551 | static pm_message_t suspend_event(pm_message_t resume_msg) | 
|  | 552 | { | 
|  | 553 | switch (resume_msg.event) { | 
|  | 554 | case PM_EVENT_RESUME: | 
|  | 555 | return PMSG_SUSPEND; | 
|  | 556 | case PM_EVENT_THAW: | 
|  | 557 | case PM_EVENT_RESTORE: | 
|  | 558 | return PMSG_FREEZE; | 
|  | 559 | case PM_EVENT_RECOVER: | 
|  | 560 | return PMSG_HIBERNATE; | 
|  | 561 | } | 
|  | 562 | return PMSG_ON; | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | /** | 
|  | 566 | * dev_pm_may_skip_resume - System-wide device resume optimization check. | 
|  | 567 | * @dev: Target device. | 
|  | 568 | * | 
|  | 569 | * Checks whether or not the device may be left in suspend after a system-wide | 
|  | 570 | * transition to the working state. | 
|  | 571 | */ | 
|  | 572 | bool dev_pm_may_skip_resume(struct device *dev) | 
|  | 573 | { | 
|  | 574 | return !dev->power.must_resume && pm_transition.event != PM_EVENT_RESTORE; | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | static pm_callback_t dpm_subsys_resume_noirq_cb(struct device *dev, | 
|  | 578 | pm_message_t state, | 
|  | 579 | const char **info_p) | 
|  | 580 | { | 
|  | 581 | pm_callback_t callback; | 
|  | 582 | const char *info; | 
|  | 583 |  | 
|  | 584 | if (dev->pm_domain) { | 
|  | 585 | info = "noirq power domain "; | 
|  | 586 | callback = pm_noirq_op(&dev->pm_domain->ops, state); | 
|  | 587 | } else if (dev->type && dev->type->pm) { | 
|  | 588 | info = "noirq type "; | 
|  | 589 | callback = pm_noirq_op(dev->type->pm, state); | 
|  | 590 | } else if (dev->class && dev->class->pm) { | 
|  | 591 | info = "noirq class "; | 
|  | 592 | callback = pm_noirq_op(dev->class->pm, state); | 
|  | 593 | } else if (dev->bus && dev->bus->pm) { | 
|  | 594 | info = "noirq bus "; | 
|  | 595 | callback = pm_noirq_op(dev->bus->pm, state); | 
|  | 596 | } else { | 
|  | 597 | return NULL; | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | if (info_p) | 
|  | 601 | *info_p = info; | 
|  | 602 |  | 
|  | 603 | return callback; | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 | static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev, | 
|  | 607 | pm_message_t state, | 
|  | 608 | const char **info_p); | 
|  | 609 |  | 
|  | 610 | static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev, | 
|  | 611 | pm_message_t state, | 
|  | 612 | const char **info_p); | 
|  | 613 |  | 
|  | 614 | /** | 
|  | 615 | * device_resume_noirq - Execute a "noirq resume" callback for given device. | 
|  | 616 | * @dev: Device to handle. | 
|  | 617 | * @state: PM transition of the system being carried out. | 
|  | 618 | * @async: If true, the device is being resumed asynchronously. | 
|  | 619 | * | 
|  | 620 | * The driver of @dev will not receive interrupts while this function is being | 
|  | 621 | * executed. | 
|  | 622 | */ | 
|  | 623 | static int device_resume_noirq(struct device *dev, pm_message_t state, bool async) | 
|  | 624 | { | 
|  | 625 | pm_callback_t callback; | 
|  | 626 | const char *info; | 
|  | 627 | bool skip_resume; | 
|  | 628 | int error = 0; | 
|  | 629 |  | 
|  | 630 | TRACE_DEVICE(dev); | 
|  | 631 | TRACE_RESUME(0); | 
|  | 632 |  | 
|  | 633 | if (dev->power.syscore || dev->power.direct_complete) | 
|  | 634 | goto Out; | 
|  | 635 |  | 
|  | 636 | if (!dev->power.is_noirq_suspended) | 
|  | 637 | goto Out; | 
|  | 638 |  | 
|  | 639 | dpm_wait_for_superior(dev, async); | 
|  | 640 |  | 
|  | 641 | skip_resume = dev_pm_may_skip_resume(dev); | 
|  | 642 |  | 
|  | 643 | callback = dpm_subsys_resume_noirq_cb(dev, state, &info); | 
|  | 644 | if (callback) | 
|  | 645 | goto Run; | 
|  | 646 |  | 
|  | 647 | if (skip_resume) | 
|  | 648 | goto Skip; | 
|  | 649 |  | 
|  | 650 | if (dev_pm_smart_suspend_and_suspended(dev)) { | 
|  | 651 | pm_message_t suspend_msg = suspend_event(state); | 
|  | 652 |  | 
|  | 653 | /* | 
|  | 654 | * If "freeze" callbacks have been skipped during a transition | 
|  | 655 | * related to hibernation, the subsequent "thaw" callbacks must | 
|  | 656 | * be skipped too or bad things may happen.  Otherwise, resume | 
|  | 657 | * callbacks are going to be run for the device, so its runtime | 
|  | 658 | * PM status must be changed to reflect the new state after the | 
|  | 659 | * transition under way. | 
|  | 660 | */ | 
|  | 661 | if (!dpm_subsys_suspend_late_cb(dev, suspend_msg, NULL) && | 
|  | 662 | !dpm_subsys_suspend_noirq_cb(dev, suspend_msg, NULL)) { | 
|  | 663 | if (state.event == PM_EVENT_THAW) { | 
|  | 664 | skip_resume = true; | 
|  | 665 | goto Skip; | 
|  | 666 | } else { | 
|  | 667 | pm_runtime_set_active(dev); | 
|  | 668 | } | 
|  | 669 | } | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | if (dev->driver && dev->driver->pm) { | 
|  | 673 | info = "noirq driver "; | 
|  | 674 | callback = pm_noirq_op(dev->driver->pm, state); | 
|  | 675 | } | 
|  | 676 |  | 
|  | 677 | Run: | 
|  | 678 | error = dpm_run_callback(callback, dev, state, info); | 
|  | 679 |  | 
|  | 680 | Skip: | 
|  | 681 | dev->power.is_noirq_suspended = false; | 
|  | 682 |  | 
|  | 683 | if (skip_resume) { | 
|  | 684 | /* | 
|  | 685 | * The device is going to be left in suspend, but it might not | 
|  | 686 | * have been in runtime suspend before the system suspended, so | 
|  | 687 | * its runtime PM status needs to be updated to avoid confusing | 
|  | 688 | * the runtime PM framework when runtime PM is enabled for the | 
|  | 689 | * device again. | 
|  | 690 | */ | 
|  | 691 | pm_runtime_set_suspended(dev); | 
|  | 692 | dev_pm_skip_next_resume_phases(dev); | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | Out: | 
|  | 696 | complete_all(&dev->power.completion); | 
|  | 697 | TRACE_RESUME(error); | 
|  | 698 | return error; | 
|  | 699 | } | 
|  | 700 |  | 
|  | 701 | static bool is_async(struct device *dev) | 
|  | 702 | { | 
|  | 703 | return dev->power.async_suspend && pm_async_enabled | 
|  | 704 | && !pm_trace_is_enabled(); | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | static void async_resume_noirq(void *data, async_cookie_t cookie) | 
|  | 708 | { | 
|  | 709 | struct device *dev = (struct device *)data; | 
|  | 710 | int error; | 
|  | 711 |  | 
|  | 712 | error = device_resume_noirq(dev, pm_transition, true); | 
|  | 713 | if (error) | 
|  | 714 | pm_dev_err(dev, pm_transition, " async", error); | 
|  | 715 |  | 
|  | 716 | put_device(dev); | 
|  | 717 | } | 
|  | 718 |  | 
|  | 719 | void dpm_noirq_resume_devices(pm_message_t state) | 
|  | 720 | { | 
|  | 721 | struct device *dev; | 
|  | 722 | ktime_t starttime = ktime_get(); | 
|  | 723 |  | 
|  | 724 | trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true); | 
|  | 725 | mutex_lock(&dpm_list_mtx); | 
|  | 726 | pm_transition = state; | 
|  | 727 |  | 
|  | 728 | /* | 
|  | 729 | * Advanced the async threads upfront, | 
|  | 730 | * in case the starting of async threads is | 
|  | 731 | * delayed by non-async resuming devices. | 
|  | 732 | */ | 
|  | 733 | list_for_each_entry(dev, &dpm_noirq_list, power.entry) { | 
|  | 734 | reinit_completion(&dev->power.completion); | 
|  | 735 | if (is_async(dev)) { | 
|  | 736 | get_device(dev); | 
|  | 737 | async_schedule(async_resume_noirq, dev); | 
|  | 738 | } | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | while (!list_empty(&dpm_noirq_list)) { | 
|  | 742 | dev = to_device(dpm_noirq_list.next); | 
|  | 743 | get_device(dev); | 
|  | 744 | list_move_tail(&dev->power.entry, &dpm_late_early_list); | 
|  | 745 | mutex_unlock(&dpm_list_mtx); | 
|  | 746 |  | 
|  | 747 | if (!is_async(dev)) { | 
|  | 748 | int error; | 
|  | 749 |  | 
|  | 750 | error = device_resume_noirq(dev, state, false); | 
|  | 751 | if (error) { | 
|  | 752 | suspend_stats.failed_resume_noirq++; | 
|  | 753 | dpm_save_failed_step(SUSPEND_RESUME_NOIRQ); | 
|  | 754 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 755 | pm_dev_err(dev, state, " noirq", error); | 
|  | 756 | } | 
|  | 757 | } | 
|  | 758 |  | 
|  | 759 | mutex_lock(&dpm_list_mtx); | 
|  | 760 | put_device(dev); | 
|  | 761 | } | 
|  | 762 | mutex_unlock(&dpm_list_mtx); | 
|  | 763 | async_synchronize_full(); | 
|  | 764 | dpm_show_time(starttime, state, 0, "noirq"); | 
|  | 765 | trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false); | 
|  | 766 | } | 
|  | 767 |  | 
|  | 768 | void dpm_noirq_end(void) | 
|  | 769 | { | 
|  | 770 | resume_device_irqs(); | 
|  | 771 | device_wakeup_disarm_wake_irqs(); | 
|  | 772 | cpuidle_resume(); | 
|  | 773 | } | 
|  | 774 |  | 
|  | 775 | /** | 
|  | 776 | * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices. | 
|  | 777 | * @state: PM transition of the system being carried out. | 
|  | 778 | * | 
|  | 779 | * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and | 
|  | 780 | * allow device drivers' interrupt handlers to be called. | 
|  | 781 | */ | 
|  | 782 | void dpm_resume_noirq(pm_message_t state) | 
|  | 783 | { | 
|  | 784 | dpm_noirq_resume_devices(state); | 
|  | 785 | dpm_noirq_end(); | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | static pm_callback_t dpm_subsys_resume_early_cb(struct device *dev, | 
|  | 789 | pm_message_t state, | 
|  | 790 | const char **info_p) | 
|  | 791 | { | 
|  | 792 | pm_callback_t callback; | 
|  | 793 | const char *info; | 
|  | 794 |  | 
|  | 795 | if (dev->pm_domain) { | 
|  | 796 | info = "early power domain "; | 
|  | 797 | callback = pm_late_early_op(&dev->pm_domain->ops, state); | 
|  | 798 | } else if (dev->type && dev->type->pm) { | 
|  | 799 | info = "early type "; | 
|  | 800 | callback = pm_late_early_op(dev->type->pm, state); | 
|  | 801 | } else if (dev->class && dev->class->pm) { | 
|  | 802 | info = "early class "; | 
|  | 803 | callback = pm_late_early_op(dev->class->pm, state); | 
|  | 804 | } else if (dev->bus && dev->bus->pm) { | 
|  | 805 | info = "early bus "; | 
|  | 806 | callback = pm_late_early_op(dev->bus->pm, state); | 
|  | 807 | } else { | 
|  | 808 | return NULL; | 
|  | 809 | } | 
|  | 810 |  | 
|  | 811 | if (info_p) | 
|  | 812 | *info_p = info; | 
|  | 813 |  | 
|  | 814 | return callback; | 
|  | 815 | } | 
|  | 816 |  | 
|  | 817 | /** | 
|  | 818 | * device_resume_early - Execute an "early resume" callback for given device. | 
|  | 819 | * @dev: Device to handle. | 
|  | 820 | * @state: PM transition of the system being carried out. | 
|  | 821 | * @async: If true, the device is being resumed asynchronously. | 
|  | 822 | * | 
|  | 823 | * Runtime PM is disabled for @dev while this function is being executed. | 
|  | 824 | */ | 
|  | 825 | static int device_resume_early(struct device *dev, pm_message_t state, bool async) | 
|  | 826 | { | 
|  | 827 | pm_callback_t callback; | 
|  | 828 | const char *info; | 
|  | 829 | int error = 0; | 
|  | 830 |  | 
|  | 831 | TRACE_DEVICE(dev); | 
|  | 832 | TRACE_RESUME(0); | 
|  | 833 |  | 
|  | 834 | if (dev->power.syscore || dev->power.direct_complete) | 
|  | 835 | goto Out; | 
|  | 836 |  | 
|  | 837 | if (!dev->power.is_late_suspended) | 
|  | 838 | goto Out; | 
|  | 839 |  | 
|  | 840 | dpm_wait_for_superior(dev, async); | 
|  | 841 |  | 
|  | 842 | callback = dpm_subsys_resume_early_cb(dev, state, &info); | 
|  | 843 |  | 
|  | 844 | if (!callback && dev->driver && dev->driver->pm) { | 
|  | 845 | info = "early driver "; | 
|  | 846 | callback = pm_late_early_op(dev->driver->pm, state); | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | error = dpm_run_callback(callback, dev, state, info); | 
|  | 850 | dev->power.is_late_suspended = false; | 
|  | 851 |  | 
|  | 852 | Out: | 
|  | 853 | TRACE_RESUME(error); | 
|  | 854 |  | 
|  | 855 | pm_runtime_enable(dev); | 
|  | 856 | complete_all(&dev->power.completion); | 
|  | 857 | return error; | 
|  | 858 | } | 
|  | 859 |  | 
|  | 860 | static void async_resume_early(void *data, async_cookie_t cookie) | 
|  | 861 | { | 
|  | 862 | struct device *dev = (struct device *)data; | 
|  | 863 | int error; | 
|  | 864 |  | 
|  | 865 | error = device_resume_early(dev, pm_transition, true); | 
|  | 866 | if (error) | 
|  | 867 | pm_dev_err(dev, pm_transition, " async", error); | 
|  | 868 |  | 
|  | 869 | put_device(dev); | 
|  | 870 | } | 
|  | 871 |  | 
|  | 872 | /** | 
|  | 873 | * dpm_resume_early - Execute "early resume" callbacks for all devices. | 
|  | 874 | * @state: PM transition of the system being carried out. | 
|  | 875 | */ | 
|  | 876 | void dpm_resume_early(pm_message_t state) | 
|  | 877 | { | 
|  | 878 | struct device *dev; | 
|  | 879 | ktime_t starttime = ktime_get(); | 
|  | 880 |  | 
|  | 881 | trace_suspend_resume(TPS("dpm_resume_early"), state.event, true); | 
|  | 882 | mutex_lock(&dpm_list_mtx); | 
|  | 883 | pm_transition = state; | 
|  | 884 |  | 
|  | 885 | /* | 
|  | 886 | * Advanced the async threads upfront, | 
|  | 887 | * in case the starting of async threads is | 
|  | 888 | * delayed by non-async resuming devices. | 
|  | 889 | */ | 
|  | 890 | list_for_each_entry(dev, &dpm_late_early_list, power.entry) { | 
|  | 891 | reinit_completion(&dev->power.completion); | 
|  | 892 | if (is_async(dev)) { | 
|  | 893 | get_device(dev); | 
|  | 894 | async_schedule(async_resume_early, dev); | 
|  | 895 | } | 
|  | 896 | } | 
|  | 897 |  | 
|  | 898 | while (!list_empty(&dpm_late_early_list)) { | 
|  | 899 | dev = to_device(dpm_late_early_list.next); | 
|  | 900 | get_device(dev); | 
|  | 901 | list_move_tail(&dev->power.entry, &dpm_suspended_list); | 
|  | 902 | mutex_unlock(&dpm_list_mtx); | 
|  | 903 |  | 
|  | 904 | if (!is_async(dev)) { | 
|  | 905 | int error; | 
|  | 906 |  | 
|  | 907 | error = device_resume_early(dev, state, false); | 
|  | 908 | if (error) { | 
|  | 909 | suspend_stats.failed_resume_early++; | 
|  | 910 | dpm_save_failed_step(SUSPEND_RESUME_EARLY); | 
|  | 911 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 912 | pm_dev_err(dev, state, " early", error); | 
|  | 913 | } | 
|  | 914 | } | 
|  | 915 | mutex_lock(&dpm_list_mtx); | 
|  | 916 | put_device(dev); | 
|  | 917 | } | 
|  | 918 | mutex_unlock(&dpm_list_mtx); | 
|  | 919 | async_synchronize_full(); | 
|  | 920 | dpm_show_time(starttime, state, 0, "early"); | 
|  | 921 | trace_suspend_resume(TPS("dpm_resume_early"), state.event, false); | 
|  | 922 | } | 
|  | 923 |  | 
|  | 924 | /** | 
|  | 925 | * dpm_resume_start - Execute "noirq" and "early" device callbacks. | 
|  | 926 | * @state: PM transition of the system being carried out. | 
|  | 927 | */ | 
|  | 928 | void dpm_resume_start(pm_message_t state) | 
|  | 929 | { | 
|  | 930 | dpm_resume_noirq(state); | 
|  | 931 | dpm_resume_early(state); | 
|  | 932 | } | 
|  | 933 | EXPORT_SYMBOL_GPL(dpm_resume_start); | 
|  | 934 |  | 
|  | 935 | /** | 
|  | 936 | * device_resume - Execute "resume" callbacks for given device. | 
|  | 937 | * @dev: Device to handle. | 
|  | 938 | * @state: PM transition of the system being carried out. | 
|  | 939 | * @async: If true, the device is being resumed asynchronously. | 
|  | 940 | */ | 
|  | 941 | static int device_resume(struct device *dev, pm_message_t state, bool async) | 
|  | 942 | { | 
|  | 943 | pm_callback_t callback = NULL; | 
|  | 944 | const char *info = NULL; | 
|  | 945 | int error = 0; | 
|  | 946 | DECLARE_DPM_WATCHDOG_ON_STACK(wd); | 
|  | 947 |  | 
|  | 948 | TRACE_DEVICE(dev); | 
|  | 949 | TRACE_RESUME(0); | 
|  | 950 |  | 
|  | 951 | if (dev->power.syscore) | 
|  | 952 | goto Complete; | 
|  | 953 |  | 
|  | 954 | if (dev->power.direct_complete) { | 
|  | 955 | /* Match the pm_runtime_disable() in __device_suspend(). */ | 
|  | 956 | pm_runtime_enable(dev); | 
|  | 957 | goto Complete; | 
|  | 958 | } | 
|  | 959 |  | 
|  | 960 | dpm_wait_for_superior(dev, async); | 
|  | 961 | dpm_watchdog_set(&wd, dev); | 
|  | 962 | device_lock(dev); | 
|  | 963 |  | 
|  | 964 | /* | 
|  | 965 | * This is a fib.  But we'll allow new children to be added below | 
|  | 966 | * a resumed device, even if the device hasn't been completed yet. | 
|  | 967 | */ | 
|  | 968 | dev->power.is_prepared = false; | 
|  | 969 |  | 
|  | 970 | if (!dev->power.is_suspended) | 
|  | 971 | goto Unlock; | 
|  | 972 |  | 
|  | 973 | if (dev->pm_domain) { | 
|  | 974 | info = "power domain "; | 
|  | 975 | callback = pm_op(&dev->pm_domain->ops, state); | 
|  | 976 | goto Driver; | 
|  | 977 | } | 
|  | 978 |  | 
|  | 979 | if (dev->type && dev->type->pm) { | 
|  | 980 | info = "type "; | 
|  | 981 | callback = pm_op(dev->type->pm, state); | 
|  | 982 | goto Driver; | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 | if (dev->class && dev->class->pm) { | 
|  | 986 | info = "class "; | 
|  | 987 | callback = pm_op(dev->class->pm, state); | 
|  | 988 | goto Driver; | 
|  | 989 | } | 
|  | 990 |  | 
|  | 991 | if (dev->bus) { | 
|  | 992 | if (dev->bus->pm) { | 
|  | 993 | info = "bus "; | 
|  | 994 | callback = pm_op(dev->bus->pm, state); | 
|  | 995 | } else if (dev->bus->resume) { | 
|  | 996 | info = "legacy bus "; | 
|  | 997 | callback = dev->bus->resume; | 
|  | 998 | goto End; | 
|  | 999 | } | 
|  | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | Driver: | 
|  | 1003 | if (!callback && dev->driver && dev->driver->pm) { | 
|  | 1004 | info = "driver "; | 
|  | 1005 | callback = pm_op(dev->driver->pm, state); | 
|  | 1006 | } | 
|  | 1007 |  | 
|  | 1008 | End: | 
|  | 1009 | error = dpm_run_callback(callback, dev, state, info); | 
|  | 1010 | dev->power.is_suspended = false; | 
|  | 1011 |  | 
|  | 1012 | Unlock: | 
|  | 1013 | device_unlock(dev); | 
|  | 1014 | dpm_watchdog_clear(&wd); | 
|  | 1015 |  | 
|  | 1016 | Complete: | 
|  | 1017 | complete_all(&dev->power.completion); | 
|  | 1018 |  | 
|  | 1019 | TRACE_RESUME(error); | 
|  | 1020 |  | 
|  | 1021 | return error; | 
|  | 1022 | } | 
|  | 1023 |  | 
|  | 1024 | static void async_resume(void *data, async_cookie_t cookie) | 
|  | 1025 | { | 
|  | 1026 | struct device *dev = (struct device *)data; | 
|  | 1027 | int error; | 
|  | 1028 |  | 
|  | 1029 | error = device_resume(dev, pm_transition, true); | 
|  | 1030 | if (error) | 
|  | 1031 | pm_dev_err(dev, pm_transition, " async", error); | 
|  | 1032 | put_device(dev); | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | /** | 
|  | 1036 | * dpm_resume - Execute "resume" callbacks for non-sysdev devices. | 
|  | 1037 | * @state: PM transition of the system being carried out. | 
|  | 1038 | * | 
|  | 1039 | * Execute the appropriate "resume" callback for all devices whose status | 
|  | 1040 | * indicates that they are suspended. | 
|  | 1041 | */ | 
|  | 1042 | void dpm_resume(pm_message_t state) | 
|  | 1043 | { | 
|  | 1044 | struct device *dev; | 
|  | 1045 | ktime_t starttime = ktime_get(); | 
|  | 1046 |  | 
|  | 1047 | trace_suspend_resume(TPS("dpm_resume"), state.event, true); | 
|  | 1048 | might_sleep(); | 
|  | 1049 |  | 
|  | 1050 | mutex_lock(&dpm_list_mtx); | 
|  | 1051 | pm_transition = state; | 
|  | 1052 | async_error = 0; | 
|  | 1053 |  | 
|  | 1054 | list_for_each_entry(dev, &dpm_suspended_list, power.entry) { | 
|  | 1055 | reinit_completion(&dev->power.completion); | 
|  | 1056 | if (is_async(dev)) { | 
|  | 1057 | get_device(dev); | 
|  | 1058 | async_schedule(async_resume, dev); | 
|  | 1059 | } | 
|  | 1060 | } | 
|  | 1061 |  | 
|  | 1062 | while (!list_empty(&dpm_suspended_list)) { | 
|  | 1063 | dev = to_device(dpm_suspended_list.next); | 
|  | 1064 | get_device(dev); | 
|  | 1065 | if (!is_async(dev)) { | 
|  | 1066 | int error; | 
|  | 1067 |  | 
|  | 1068 | mutex_unlock(&dpm_list_mtx); | 
|  | 1069 |  | 
|  | 1070 | error = device_resume(dev, state, false); | 
|  | 1071 | if (error) { | 
|  | 1072 | suspend_stats.failed_resume++; | 
|  | 1073 | dpm_save_failed_step(SUSPEND_RESUME); | 
|  | 1074 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1075 | pm_dev_err(dev, state, "", error); | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | mutex_lock(&dpm_list_mtx); | 
|  | 1079 | } | 
|  | 1080 | if (!list_empty(&dev->power.entry)) | 
|  | 1081 | list_move_tail(&dev->power.entry, &dpm_prepared_list); | 
|  | 1082 | put_device(dev); | 
|  | 1083 | } | 
|  | 1084 | mutex_unlock(&dpm_list_mtx); | 
|  | 1085 | async_synchronize_full(); | 
|  | 1086 | dpm_show_time(starttime, state, 0, NULL); | 
|  | 1087 |  | 
|  | 1088 | cpufreq_resume(); | 
|  | 1089 | trace_suspend_resume(TPS("dpm_resume"), state.event, false); | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 | /** | 
|  | 1093 | * device_complete - Complete a PM transition for given device. | 
|  | 1094 | * @dev: Device to handle. | 
|  | 1095 | * @state: PM transition of the system being carried out. | 
|  | 1096 | */ | 
|  | 1097 | static void device_complete(struct device *dev, pm_message_t state) | 
|  | 1098 | { | 
|  | 1099 | void (*callback)(struct device *) = NULL; | 
|  | 1100 | const char *info = NULL; | 
|  | 1101 |  | 
|  | 1102 | if (dev->power.syscore) | 
|  | 1103 | return; | 
|  | 1104 |  | 
|  | 1105 | device_lock(dev); | 
|  | 1106 |  | 
|  | 1107 | if (dev->pm_domain) { | 
|  | 1108 | info = "completing power domain "; | 
|  | 1109 | callback = dev->pm_domain->ops.complete; | 
|  | 1110 | } else if (dev->type && dev->type->pm) { | 
|  | 1111 | info = "completing type "; | 
|  | 1112 | callback = dev->type->pm->complete; | 
|  | 1113 | } else if (dev->class && dev->class->pm) { | 
|  | 1114 | info = "completing class "; | 
|  | 1115 | callback = dev->class->pm->complete; | 
|  | 1116 | } else if (dev->bus && dev->bus->pm) { | 
|  | 1117 | info = "completing bus "; | 
|  | 1118 | callback = dev->bus->pm->complete; | 
|  | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | if (!callback && dev->driver && dev->driver->pm) { | 
|  | 1122 | info = "completing driver "; | 
|  | 1123 | callback = dev->driver->pm->complete; | 
|  | 1124 | } | 
|  | 1125 |  | 
|  | 1126 | if (callback) { | 
|  | 1127 | pm_dev_dbg(dev, state, info); | 
|  | 1128 | callback(dev); | 
|  | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | device_unlock(dev); | 
|  | 1132 |  | 
|  | 1133 | pm_runtime_put(dev); | 
|  | 1134 | } | 
|  | 1135 |  | 
|  | 1136 | /** | 
|  | 1137 | * dpm_complete - Complete a PM transition for all non-sysdev devices. | 
|  | 1138 | * @state: PM transition of the system being carried out. | 
|  | 1139 | * | 
|  | 1140 | * Execute the ->complete() callbacks for all devices whose PM status is not | 
|  | 1141 | * DPM_ON (this allows new devices to be registered). | 
|  | 1142 | */ | 
|  | 1143 | void dpm_complete(pm_message_t state) | 
|  | 1144 | { | 
|  | 1145 | struct list_head list; | 
|  | 1146 |  | 
|  | 1147 | trace_suspend_resume(TPS("dpm_complete"), state.event, true); | 
|  | 1148 | might_sleep(); | 
|  | 1149 |  | 
|  | 1150 | INIT_LIST_HEAD(&list); | 
|  | 1151 | mutex_lock(&dpm_list_mtx); | 
|  | 1152 | while (!list_empty(&dpm_prepared_list)) { | 
|  | 1153 | struct device *dev = to_device(dpm_prepared_list.prev); | 
|  | 1154 |  | 
|  | 1155 | get_device(dev); | 
|  | 1156 | dev->power.is_prepared = false; | 
|  | 1157 | list_move(&dev->power.entry, &list); | 
|  | 1158 | mutex_unlock(&dpm_list_mtx); | 
|  | 1159 |  | 
|  | 1160 | trace_device_pm_callback_start(dev, "", state.event); | 
|  | 1161 | device_complete(dev, state); | 
|  | 1162 | trace_device_pm_callback_end(dev, 0); | 
|  | 1163 |  | 
|  | 1164 | mutex_lock(&dpm_list_mtx); | 
|  | 1165 | put_device(dev); | 
|  | 1166 | } | 
|  | 1167 | list_splice(&list, &dpm_list); | 
|  | 1168 | mutex_unlock(&dpm_list_mtx); | 
|  | 1169 |  | 
|  | 1170 | /* Allow device probing and trigger re-probing of deferred devices */ | 
|  | 1171 | device_unblock_probing(); | 
|  | 1172 | trace_suspend_resume(TPS("dpm_complete"), state.event, false); | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | /** | 
|  | 1176 | * dpm_resume_end - Execute "resume" callbacks and complete system transition. | 
|  | 1177 | * @state: PM transition of the system being carried out. | 
|  | 1178 | * | 
|  | 1179 | * Execute "resume" callbacks for all devices and complete the PM transition of | 
|  | 1180 | * the system. | 
|  | 1181 | */ | 
|  | 1182 | void dpm_resume_end(pm_message_t state) | 
|  | 1183 | { | 
|  | 1184 | dpm_resume(state); | 
|  | 1185 | dpm_complete(state); | 
|  | 1186 | } | 
|  | 1187 | EXPORT_SYMBOL_GPL(dpm_resume_end); | 
|  | 1188 |  | 
|  | 1189 |  | 
|  | 1190 | /*------------------------- Suspend routines -------------------------*/ | 
|  | 1191 |  | 
|  | 1192 | /** | 
|  | 1193 | * resume_event - Return a "resume" message for given "suspend" sleep state. | 
|  | 1194 | * @sleep_state: PM message representing a sleep state. | 
|  | 1195 | * | 
|  | 1196 | * Return a PM message representing the resume event corresponding to given | 
|  | 1197 | * sleep state. | 
|  | 1198 | */ | 
|  | 1199 | static pm_message_t resume_event(pm_message_t sleep_state) | 
|  | 1200 | { | 
|  | 1201 | switch (sleep_state.event) { | 
|  | 1202 | case PM_EVENT_SUSPEND: | 
|  | 1203 | return PMSG_RESUME; | 
|  | 1204 | case PM_EVENT_FREEZE: | 
|  | 1205 | case PM_EVENT_QUIESCE: | 
|  | 1206 | return PMSG_RECOVER; | 
|  | 1207 | case PM_EVENT_HIBERNATE: | 
|  | 1208 | return PMSG_RESTORE; | 
|  | 1209 | } | 
|  | 1210 | return PMSG_ON; | 
|  | 1211 | } | 
|  | 1212 |  | 
|  | 1213 | static void dpm_superior_set_must_resume(struct device *dev) | 
|  | 1214 | { | 
|  | 1215 | struct device_link *link; | 
|  | 1216 | int idx; | 
|  | 1217 |  | 
|  | 1218 | if (dev->parent) | 
|  | 1219 | dev->parent->power.must_resume = true; | 
|  | 1220 |  | 
|  | 1221 | idx = device_links_read_lock(); | 
|  | 1222 |  | 
|  | 1223 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) | 
|  | 1224 | link->supplier->power.must_resume = true; | 
|  | 1225 |  | 
|  | 1226 | device_links_read_unlock(idx); | 
|  | 1227 | } | 
|  | 1228 |  | 
|  | 1229 | static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev, | 
|  | 1230 | pm_message_t state, | 
|  | 1231 | const char **info_p) | 
|  | 1232 | { | 
|  | 1233 | pm_callback_t callback; | 
|  | 1234 | const char *info; | 
|  | 1235 |  | 
|  | 1236 | if (dev->pm_domain) { | 
|  | 1237 | info = "noirq power domain "; | 
|  | 1238 | callback = pm_noirq_op(&dev->pm_domain->ops, state); | 
|  | 1239 | } else if (dev->type && dev->type->pm) { | 
|  | 1240 | info = "noirq type "; | 
|  | 1241 | callback = pm_noirq_op(dev->type->pm, state); | 
|  | 1242 | } else if (dev->class && dev->class->pm) { | 
|  | 1243 | info = "noirq class "; | 
|  | 1244 | callback = pm_noirq_op(dev->class->pm, state); | 
|  | 1245 | } else if (dev->bus && dev->bus->pm) { | 
|  | 1246 | info = "noirq bus "; | 
|  | 1247 | callback = pm_noirq_op(dev->bus->pm, state); | 
|  | 1248 | } else { | 
|  | 1249 | return NULL; | 
|  | 1250 | } | 
|  | 1251 |  | 
|  | 1252 | if (info_p) | 
|  | 1253 | *info_p = info; | 
|  | 1254 |  | 
|  | 1255 | return callback; | 
|  | 1256 | } | 
|  | 1257 |  | 
|  | 1258 | static bool device_must_resume(struct device *dev, pm_message_t state, | 
|  | 1259 | bool no_subsys_suspend_noirq) | 
|  | 1260 | { | 
|  | 1261 | pm_message_t resume_msg = resume_event(state); | 
|  | 1262 |  | 
|  | 1263 | /* | 
|  | 1264 | * If all of the device driver's "noirq", "late" and "early" callbacks | 
|  | 1265 | * are invoked directly by the core, the decision to allow the device to | 
|  | 1266 | * stay in suspend can be based on its current runtime PM status and its | 
|  | 1267 | * wakeup settings. | 
|  | 1268 | */ | 
|  | 1269 | if (no_subsys_suspend_noirq && | 
|  | 1270 | !dpm_subsys_suspend_late_cb(dev, state, NULL) && | 
|  | 1271 | !dpm_subsys_resume_early_cb(dev, resume_msg, NULL) && | 
|  | 1272 | !dpm_subsys_resume_noirq_cb(dev, resume_msg, NULL)) | 
|  | 1273 | return !pm_runtime_status_suspended(dev) && | 
|  | 1274 | (resume_msg.event != PM_EVENT_RESUME || | 
|  | 1275 | (device_can_wakeup(dev) && !device_may_wakeup(dev))); | 
|  | 1276 |  | 
|  | 1277 | /* | 
|  | 1278 | * The only safe strategy here is to require that if the device may not | 
|  | 1279 | * be left in suspend, resume callbacks must be invoked for it. | 
|  | 1280 | */ | 
|  | 1281 | return !dev->power.may_skip_resume; | 
|  | 1282 | } | 
|  | 1283 |  | 
|  | 1284 | /** | 
|  | 1285 | * __device_suspend_noirq - Execute a "noirq suspend" callback for given device. | 
|  | 1286 | * @dev: Device to handle. | 
|  | 1287 | * @state: PM transition of the system being carried out. | 
|  | 1288 | * @async: If true, the device is being suspended asynchronously. | 
|  | 1289 | * | 
|  | 1290 | * The driver of @dev will not receive interrupts while this function is being | 
|  | 1291 | * executed. | 
|  | 1292 | */ | 
|  | 1293 | static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async) | 
|  | 1294 | { | 
|  | 1295 | pm_callback_t callback; | 
|  | 1296 | const char *info; | 
|  | 1297 | bool no_subsys_cb = false; | 
|  | 1298 | int error = 0; | 
|  | 1299 |  | 
|  | 1300 | TRACE_DEVICE(dev); | 
|  | 1301 | TRACE_SUSPEND(0); | 
|  | 1302 |  | 
|  | 1303 | dpm_wait_for_subordinate(dev, async); | 
|  | 1304 |  | 
|  | 1305 | if (async_error) | 
|  | 1306 | goto Complete; | 
|  | 1307 |  | 
|  | 1308 | if (pm_wakeup_pending()) { | 
|  | 1309 | async_error = -EBUSY; | 
|  | 1310 | goto Complete; | 
|  | 1311 | } | 
|  | 1312 |  | 
|  | 1313 | if (dev->power.syscore || dev->power.direct_complete) | 
|  | 1314 | goto Complete; | 
|  | 1315 |  | 
|  | 1316 | callback = dpm_subsys_suspend_noirq_cb(dev, state, &info); | 
|  | 1317 | if (callback) | 
|  | 1318 | goto Run; | 
|  | 1319 |  | 
|  | 1320 | no_subsys_cb = !dpm_subsys_suspend_late_cb(dev, state, NULL); | 
|  | 1321 |  | 
|  | 1322 | if (dev_pm_smart_suspend_and_suspended(dev) && no_subsys_cb) | 
|  | 1323 | goto Skip; | 
|  | 1324 |  | 
|  | 1325 | if (dev->driver && dev->driver->pm) { | 
|  | 1326 | info = "noirq driver "; | 
|  | 1327 | callback = pm_noirq_op(dev->driver->pm, state); | 
|  | 1328 | } | 
|  | 1329 |  | 
|  | 1330 | Run: | 
|  | 1331 | error = dpm_run_callback(callback, dev, state, info); | 
|  | 1332 | if (error) { | 
|  | 1333 | async_error = error; | 
|  | 1334 | goto Complete; | 
|  | 1335 | } | 
|  | 1336 |  | 
|  | 1337 | Skip: | 
|  | 1338 | dev->power.is_noirq_suspended = true; | 
|  | 1339 |  | 
|  | 1340 | if (dev_pm_test_driver_flags(dev, DPM_FLAG_LEAVE_SUSPENDED)) { | 
|  | 1341 | dev->power.must_resume = dev->power.must_resume || | 
|  | 1342 | atomic_read(&dev->power.usage_count) > 1 || | 
|  | 1343 | device_must_resume(dev, state, no_subsys_cb); | 
|  | 1344 | } else { | 
|  | 1345 | dev->power.must_resume = true; | 
|  | 1346 | } | 
|  | 1347 |  | 
|  | 1348 | if (dev->power.must_resume) | 
|  | 1349 | dpm_superior_set_must_resume(dev); | 
|  | 1350 |  | 
|  | 1351 | Complete: | 
|  | 1352 | complete_all(&dev->power.completion); | 
|  | 1353 | TRACE_SUSPEND(error); | 
|  | 1354 | return error; | 
|  | 1355 | } | 
|  | 1356 |  | 
|  | 1357 | static void async_suspend_noirq(void *data, async_cookie_t cookie) | 
|  | 1358 | { | 
|  | 1359 | struct device *dev = (struct device *)data; | 
|  | 1360 | int error; | 
|  | 1361 |  | 
|  | 1362 | error = __device_suspend_noirq(dev, pm_transition, true); | 
|  | 1363 | if (error) { | 
|  | 1364 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1365 | pm_dev_err(dev, pm_transition, " async", error); | 
|  | 1366 | } | 
|  | 1367 |  | 
|  | 1368 | put_device(dev); | 
|  | 1369 | } | 
|  | 1370 |  | 
|  | 1371 | static int device_suspend_noirq(struct device *dev) | 
|  | 1372 | { | 
|  | 1373 | reinit_completion(&dev->power.completion); | 
|  | 1374 |  | 
|  | 1375 | if (is_async(dev)) { | 
|  | 1376 | get_device(dev); | 
|  | 1377 | async_schedule(async_suspend_noirq, dev); | 
|  | 1378 | return 0; | 
|  | 1379 | } | 
|  | 1380 | return __device_suspend_noirq(dev, pm_transition, false); | 
|  | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | void dpm_noirq_begin(void) | 
|  | 1384 | { | 
|  | 1385 | cpuidle_pause(); | 
|  | 1386 | device_wakeup_arm_wake_irqs(); | 
|  | 1387 | suspend_device_irqs(); | 
|  | 1388 | } | 
|  | 1389 |  | 
|  | 1390 | int dpm_noirq_suspend_devices(pm_message_t state) | 
|  | 1391 | { | 
|  | 1392 | ktime_t starttime = ktime_get(); | 
|  | 1393 | int error = 0; | 
|  | 1394 |  | 
|  | 1395 | trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true); | 
|  | 1396 | mutex_lock(&dpm_list_mtx); | 
|  | 1397 | pm_transition = state; | 
|  | 1398 | async_error = 0; | 
|  | 1399 |  | 
|  | 1400 | while (!list_empty(&dpm_late_early_list)) { | 
|  | 1401 | struct device *dev = to_device(dpm_late_early_list.prev); | 
|  | 1402 |  | 
|  | 1403 | get_device(dev); | 
|  | 1404 | mutex_unlock(&dpm_list_mtx); | 
|  | 1405 |  | 
|  | 1406 | error = device_suspend_noirq(dev); | 
|  | 1407 |  | 
|  | 1408 | mutex_lock(&dpm_list_mtx); | 
|  | 1409 | if (error) { | 
|  | 1410 | pm_dev_err(dev, state, " noirq", error); | 
|  | 1411 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1412 | put_device(dev); | 
|  | 1413 | break; | 
|  | 1414 | } | 
|  | 1415 | if (!list_empty(&dev->power.entry)) | 
|  | 1416 | list_move(&dev->power.entry, &dpm_noirq_list); | 
|  | 1417 | put_device(dev); | 
|  | 1418 |  | 
|  | 1419 | if (async_error) | 
|  | 1420 | break; | 
|  | 1421 | } | 
|  | 1422 | mutex_unlock(&dpm_list_mtx); | 
|  | 1423 | async_synchronize_full(); | 
|  | 1424 | if (!error) | 
|  | 1425 | error = async_error; | 
|  | 1426 |  | 
|  | 1427 | if (error) { | 
|  | 1428 | suspend_stats.failed_suspend_noirq++; | 
|  | 1429 | dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ); | 
|  | 1430 | } | 
|  | 1431 | dpm_show_time(starttime, state, error, "noirq"); | 
|  | 1432 | trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false); | 
|  | 1433 | return error; | 
|  | 1434 | } | 
|  | 1435 |  | 
|  | 1436 | /** | 
|  | 1437 | * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices. | 
|  | 1438 | * @state: PM transition of the system being carried out. | 
|  | 1439 | * | 
|  | 1440 | * Prevent device drivers' interrupt handlers from being called and invoke | 
|  | 1441 | * "noirq" suspend callbacks for all non-sysdev devices. | 
|  | 1442 | */ | 
|  | 1443 | int dpm_suspend_noirq(pm_message_t state) | 
|  | 1444 | { | 
|  | 1445 | int ret; | 
|  | 1446 |  | 
|  | 1447 | dpm_noirq_begin(); | 
|  | 1448 | ret = dpm_noirq_suspend_devices(state); | 
|  | 1449 | if (ret) | 
|  | 1450 | dpm_resume_noirq(resume_event(state)); | 
|  | 1451 |  | 
|  | 1452 | return ret; | 
|  | 1453 | } | 
|  | 1454 |  | 
|  | 1455 | static void dpm_propagate_wakeup_to_parent(struct device *dev) | 
|  | 1456 | { | 
|  | 1457 | struct device *parent = dev->parent; | 
|  | 1458 |  | 
|  | 1459 | if (!parent) | 
|  | 1460 | return; | 
|  | 1461 |  | 
|  | 1462 | spin_lock_irq(&parent->power.lock); | 
|  | 1463 |  | 
|  | 1464 | if (dev->power.wakeup_path && !parent->power.ignore_children) | 
|  | 1465 | parent->power.wakeup_path = true; | 
|  | 1466 |  | 
|  | 1467 | spin_unlock_irq(&parent->power.lock); | 
|  | 1468 | } | 
|  | 1469 |  | 
|  | 1470 | static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev, | 
|  | 1471 | pm_message_t state, | 
|  | 1472 | const char **info_p) | 
|  | 1473 | { | 
|  | 1474 | pm_callback_t callback; | 
|  | 1475 | const char *info; | 
|  | 1476 |  | 
|  | 1477 | if (dev->pm_domain) { | 
|  | 1478 | info = "late power domain "; | 
|  | 1479 | callback = pm_late_early_op(&dev->pm_domain->ops, state); | 
|  | 1480 | } else if (dev->type && dev->type->pm) { | 
|  | 1481 | info = "late type "; | 
|  | 1482 | callback = pm_late_early_op(dev->type->pm, state); | 
|  | 1483 | } else if (dev->class && dev->class->pm) { | 
|  | 1484 | info = "late class "; | 
|  | 1485 | callback = pm_late_early_op(dev->class->pm, state); | 
|  | 1486 | } else if (dev->bus && dev->bus->pm) { | 
|  | 1487 | info = "late bus "; | 
|  | 1488 | callback = pm_late_early_op(dev->bus->pm, state); | 
|  | 1489 | } else { | 
|  | 1490 | return NULL; | 
|  | 1491 | } | 
|  | 1492 |  | 
|  | 1493 | if (info_p) | 
|  | 1494 | *info_p = info; | 
|  | 1495 |  | 
|  | 1496 | return callback; | 
|  | 1497 | } | 
|  | 1498 |  | 
|  | 1499 | /** | 
|  | 1500 | * __device_suspend_late - Execute a "late suspend" callback for given device. | 
|  | 1501 | * @dev: Device to handle. | 
|  | 1502 | * @state: PM transition of the system being carried out. | 
|  | 1503 | * @async: If true, the device is being suspended asynchronously. | 
|  | 1504 | * | 
|  | 1505 | * Runtime PM is disabled for @dev while this function is being executed. | 
|  | 1506 | */ | 
|  | 1507 | static int __device_suspend_late(struct device *dev, pm_message_t state, bool async) | 
|  | 1508 | { | 
|  | 1509 | pm_callback_t callback; | 
|  | 1510 | const char *info; | 
|  | 1511 | int error = 0; | 
|  | 1512 |  | 
|  | 1513 | TRACE_DEVICE(dev); | 
|  | 1514 | TRACE_SUSPEND(0); | 
|  | 1515 |  | 
|  | 1516 | __pm_runtime_disable(dev, false); | 
|  | 1517 |  | 
|  | 1518 | dpm_wait_for_subordinate(dev, async); | 
|  | 1519 |  | 
|  | 1520 | if (async_error) | 
|  | 1521 | goto Complete; | 
|  | 1522 |  | 
|  | 1523 | if (pm_wakeup_pending()) { | 
|  | 1524 | async_error = -EBUSY; | 
|  | 1525 | goto Complete; | 
|  | 1526 | } | 
|  | 1527 |  | 
|  | 1528 | if (dev->power.syscore || dev->power.direct_complete) | 
|  | 1529 | goto Complete; | 
|  | 1530 |  | 
|  | 1531 | callback = dpm_subsys_suspend_late_cb(dev, state, &info); | 
|  | 1532 | if (callback) | 
|  | 1533 | goto Run; | 
|  | 1534 |  | 
|  | 1535 | if (dev_pm_smart_suspend_and_suspended(dev) && | 
|  | 1536 | !dpm_subsys_suspend_noirq_cb(dev, state, NULL)) | 
|  | 1537 | goto Skip; | 
|  | 1538 |  | 
|  | 1539 | if (dev->driver && dev->driver->pm) { | 
|  | 1540 | info = "late driver "; | 
|  | 1541 | callback = pm_late_early_op(dev->driver->pm, state); | 
|  | 1542 | } | 
|  | 1543 |  | 
|  | 1544 | Run: | 
|  | 1545 | error = dpm_run_callback(callback, dev, state, info); | 
|  | 1546 | if (error) { | 
|  | 1547 | async_error = error; | 
|  | 1548 | goto Complete; | 
|  | 1549 | } | 
|  | 1550 | dpm_propagate_wakeup_to_parent(dev); | 
|  | 1551 |  | 
|  | 1552 | Skip: | 
|  | 1553 | dev->power.is_late_suspended = true; | 
|  | 1554 |  | 
|  | 1555 | Complete: | 
|  | 1556 | TRACE_SUSPEND(error); | 
|  | 1557 | complete_all(&dev->power.completion); | 
|  | 1558 | return error; | 
|  | 1559 | } | 
|  | 1560 |  | 
|  | 1561 | static void async_suspend_late(void *data, async_cookie_t cookie) | 
|  | 1562 | { | 
|  | 1563 | struct device *dev = (struct device *)data; | 
|  | 1564 | int error; | 
|  | 1565 |  | 
|  | 1566 | error = __device_suspend_late(dev, pm_transition, true); | 
|  | 1567 | if (error) { | 
|  | 1568 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1569 | pm_dev_err(dev, pm_transition, " async", error); | 
|  | 1570 | } | 
|  | 1571 | put_device(dev); | 
|  | 1572 | } | 
|  | 1573 |  | 
|  | 1574 | static int device_suspend_late(struct device *dev) | 
|  | 1575 | { | 
|  | 1576 | reinit_completion(&dev->power.completion); | 
|  | 1577 |  | 
|  | 1578 | if (is_async(dev)) { | 
|  | 1579 | get_device(dev); | 
|  | 1580 | async_schedule(async_suspend_late, dev); | 
|  | 1581 | return 0; | 
|  | 1582 | } | 
|  | 1583 |  | 
|  | 1584 | return __device_suspend_late(dev, pm_transition, false); | 
|  | 1585 | } | 
|  | 1586 |  | 
|  | 1587 | /** | 
|  | 1588 | * dpm_suspend_late - Execute "late suspend" callbacks for all devices. | 
|  | 1589 | * @state: PM transition of the system being carried out. | 
|  | 1590 | */ | 
|  | 1591 | int dpm_suspend_late(pm_message_t state) | 
|  | 1592 | { | 
|  | 1593 | ktime_t starttime = ktime_get(); | 
|  | 1594 | int error = 0; | 
|  | 1595 |  | 
|  | 1596 | trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true); | 
|  | 1597 | mutex_lock(&dpm_list_mtx); | 
|  | 1598 | pm_transition = state; | 
|  | 1599 | async_error = 0; | 
|  | 1600 |  | 
|  | 1601 | while (!list_empty(&dpm_suspended_list)) { | 
|  | 1602 | struct device *dev = to_device(dpm_suspended_list.prev); | 
|  | 1603 |  | 
|  | 1604 | get_device(dev); | 
|  | 1605 | mutex_unlock(&dpm_list_mtx); | 
|  | 1606 |  | 
|  | 1607 | error = device_suspend_late(dev); | 
|  | 1608 |  | 
|  | 1609 | mutex_lock(&dpm_list_mtx); | 
|  | 1610 | if (!list_empty(&dev->power.entry)) | 
|  | 1611 | list_move(&dev->power.entry, &dpm_late_early_list); | 
|  | 1612 |  | 
|  | 1613 | if (error) { | 
|  | 1614 | pm_dev_err(dev, state, " late", error); | 
|  | 1615 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1616 | put_device(dev); | 
|  | 1617 | break; | 
|  | 1618 | } | 
|  | 1619 | put_device(dev); | 
|  | 1620 |  | 
|  | 1621 | if (async_error) | 
|  | 1622 | break; | 
|  | 1623 | } | 
|  | 1624 | mutex_unlock(&dpm_list_mtx); | 
|  | 1625 | async_synchronize_full(); | 
|  | 1626 | if (!error) | 
|  | 1627 | error = async_error; | 
|  | 1628 | if (error) { | 
|  | 1629 | suspend_stats.failed_suspend_late++; | 
|  | 1630 | dpm_save_failed_step(SUSPEND_SUSPEND_LATE); | 
|  | 1631 | dpm_resume_early(resume_event(state)); | 
|  | 1632 | } | 
|  | 1633 | dpm_show_time(starttime, state, error, "late"); | 
|  | 1634 | trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false); | 
|  | 1635 | return error; | 
|  | 1636 | } | 
|  | 1637 |  | 
|  | 1638 | /** | 
|  | 1639 | * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks. | 
|  | 1640 | * @state: PM transition of the system being carried out. | 
|  | 1641 | */ | 
|  | 1642 | int dpm_suspend_end(pm_message_t state) | 
|  | 1643 | { | 
|  | 1644 | int error = dpm_suspend_late(state); | 
|  | 1645 | if (error) | 
|  | 1646 | return error; | 
|  | 1647 |  | 
|  | 1648 | error = dpm_suspend_noirq(state); | 
|  | 1649 | if (error) { | 
|  | 1650 | dpm_resume_early(resume_event(state)); | 
|  | 1651 | return error; | 
|  | 1652 | } | 
|  | 1653 |  | 
|  | 1654 | return 0; | 
|  | 1655 | } | 
|  | 1656 | EXPORT_SYMBOL_GPL(dpm_suspend_end); | 
|  | 1657 |  | 
|  | 1658 | /** | 
|  | 1659 | * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. | 
|  | 1660 | * @dev: Device to suspend. | 
|  | 1661 | * @state: PM transition of the system being carried out. | 
|  | 1662 | * @cb: Suspend callback to execute. | 
|  | 1663 | * @info: string description of caller. | 
|  | 1664 | */ | 
|  | 1665 | static int legacy_suspend(struct device *dev, pm_message_t state, | 
|  | 1666 | int (*cb)(struct device *dev, pm_message_t state), | 
|  | 1667 | const char *info) | 
|  | 1668 | { | 
|  | 1669 | int error; | 
|  | 1670 | ktime_t calltime; | 
|  | 1671 |  | 
|  | 1672 | calltime = initcall_debug_start(dev, cb); | 
|  | 1673 |  | 
|  | 1674 | trace_device_pm_callback_start(dev, info, state.event); | 
|  | 1675 | error = cb(dev, state); | 
|  | 1676 | trace_device_pm_callback_end(dev, error); | 
|  | 1677 | suspend_report_result(cb, error); | 
|  | 1678 |  | 
|  | 1679 | initcall_debug_report(dev, calltime, cb, error); | 
|  | 1680 |  | 
|  | 1681 | return error; | 
|  | 1682 | } | 
|  | 1683 |  | 
|  | 1684 | static void dpm_clear_superiors_direct_complete(struct device *dev) | 
|  | 1685 | { | 
|  | 1686 | struct device_link *link; | 
|  | 1687 | int idx; | 
|  | 1688 |  | 
|  | 1689 | if (dev->parent) { | 
|  | 1690 | spin_lock_irq(&dev->parent->power.lock); | 
|  | 1691 | dev->parent->power.direct_complete = false; | 
|  | 1692 | spin_unlock_irq(&dev->parent->power.lock); | 
|  | 1693 | } | 
|  | 1694 |  | 
|  | 1695 | idx = device_links_read_lock(); | 
|  | 1696 |  | 
|  | 1697 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) { | 
|  | 1698 | spin_lock_irq(&link->supplier->power.lock); | 
|  | 1699 | link->supplier->power.direct_complete = false; | 
|  | 1700 | spin_unlock_irq(&link->supplier->power.lock); | 
|  | 1701 | } | 
|  | 1702 |  | 
|  | 1703 | device_links_read_unlock(idx); | 
|  | 1704 | } | 
|  | 1705 |  | 
|  | 1706 | /** | 
|  | 1707 | * __device_suspend - Execute "suspend" callbacks for given device. | 
|  | 1708 | * @dev: Device to handle. | 
|  | 1709 | * @state: PM transition of the system being carried out. | 
|  | 1710 | * @async: If true, the device is being suspended asynchronously. | 
|  | 1711 | */ | 
|  | 1712 | static int __device_suspend(struct device *dev, pm_message_t state, bool async) | 
|  | 1713 | { | 
|  | 1714 | pm_callback_t callback = NULL; | 
|  | 1715 | const char *info = NULL; | 
|  | 1716 | int error = 0; | 
|  | 1717 | char suspend_abort[MAX_SUSPEND_ABORT_LEN]; | 
|  | 1718 | DECLARE_DPM_WATCHDOG_ON_STACK(wd); | 
|  | 1719 |  | 
|  | 1720 | TRACE_DEVICE(dev); | 
|  | 1721 | TRACE_SUSPEND(0); | 
|  | 1722 |  | 
|  | 1723 | dpm_wait_for_subordinate(dev, async); | 
|  | 1724 |  | 
|  | 1725 | if (async_error) { | 
|  | 1726 | dev->power.direct_complete = false; | 
|  | 1727 | goto Complete; | 
|  | 1728 | } | 
|  | 1729 |  | 
|  | 1730 | /* | 
|  | 1731 | * If a device configured to wake up the system from sleep states | 
|  | 1732 | * has been suspended at run time and there's a resume request pending | 
|  | 1733 | * for it, this is equivalent to the device signaling wakeup, so the | 
|  | 1734 | * system suspend operation should be aborted. | 
|  | 1735 | */ | 
|  | 1736 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) | 
|  | 1737 | pm_wakeup_event(dev, 0); | 
|  | 1738 |  | 
|  | 1739 | if (pm_wakeup_pending()) { | 
|  | 1740 | dev->power.direct_complete = false; | 
|  | 1741 | pm_get_active_wakeup_sources(suspend_abort, | 
|  | 1742 | MAX_SUSPEND_ABORT_LEN); | 
|  | 1743 | log_suspend_abort_reason(suspend_abort); | 
|  | 1744 | async_error = -EBUSY; | 
|  | 1745 | goto Complete; | 
|  | 1746 | } | 
|  | 1747 |  | 
|  | 1748 | if (dev->power.syscore) | 
|  | 1749 | goto Complete; | 
|  | 1750 |  | 
|  | 1751 | /* Avoid direct_complete to let wakeup_path propagate. */ | 
|  | 1752 | if (device_may_wakeup(dev) || dev->power.wakeup_path) | 
|  | 1753 | dev->power.direct_complete = false; | 
|  | 1754 |  | 
|  | 1755 | if (dev->power.direct_complete) { | 
|  | 1756 | if (pm_runtime_status_suspended(dev)) { | 
|  | 1757 | pm_runtime_disable(dev); | 
|  | 1758 | if (pm_runtime_status_suspended(dev)) | 
|  | 1759 | goto Complete; | 
|  | 1760 |  | 
|  | 1761 | pm_runtime_enable(dev); | 
|  | 1762 | } | 
|  | 1763 | dev->power.direct_complete = false; | 
|  | 1764 | } | 
|  | 1765 |  | 
|  | 1766 | dev->power.may_skip_resume = false; | 
|  | 1767 | dev->power.must_resume = false; | 
|  | 1768 |  | 
|  | 1769 | dpm_watchdog_set(&wd, dev); | 
|  | 1770 | device_lock(dev); | 
|  | 1771 |  | 
|  | 1772 | if (dev->pm_domain) { | 
|  | 1773 | info = "power domain "; | 
|  | 1774 | callback = pm_op(&dev->pm_domain->ops, state); | 
|  | 1775 | goto Run; | 
|  | 1776 | } | 
|  | 1777 |  | 
|  | 1778 | if (dev->type && dev->type->pm) { | 
|  | 1779 | info = "type "; | 
|  | 1780 | callback = pm_op(dev->type->pm, state); | 
|  | 1781 | goto Run; | 
|  | 1782 | } | 
|  | 1783 |  | 
|  | 1784 | if (dev->class && dev->class->pm) { | 
|  | 1785 | info = "class "; | 
|  | 1786 | callback = pm_op(dev->class->pm, state); | 
|  | 1787 | goto Run; | 
|  | 1788 | } | 
|  | 1789 |  | 
|  | 1790 | if (dev->bus) { | 
|  | 1791 | if (dev->bus->pm) { | 
|  | 1792 | info = "bus "; | 
|  | 1793 | callback = pm_op(dev->bus->pm, state); | 
|  | 1794 | } else if (dev->bus->suspend) { | 
|  | 1795 | pm_dev_dbg(dev, state, "legacy bus "); | 
|  | 1796 | error = legacy_suspend(dev, state, dev->bus->suspend, | 
|  | 1797 | "legacy bus "); | 
|  | 1798 | goto End; | 
|  | 1799 | } | 
|  | 1800 | } | 
|  | 1801 |  | 
|  | 1802 | Run: | 
|  | 1803 | if (!callback && dev->driver && dev->driver->pm) { | 
|  | 1804 | info = "driver "; | 
|  | 1805 | callback = pm_op(dev->driver->pm, state); | 
|  | 1806 | } | 
|  | 1807 |  | 
|  | 1808 | error = dpm_run_callback(callback, dev, state, info); | 
|  | 1809 |  | 
|  | 1810 | End: | 
|  | 1811 | if (!error) { | 
|  | 1812 | dev->power.is_suspended = true; | 
|  | 1813 | if (device_may_wakeup(dev)) | 
|  | 1814 | dev->power.wakeup_path = true; | 
|  | 1815 |  | 
|  | 1816 | dpm_propagate_wakeup_to_parent(dev); | 
|  | 1817 | dpm_clear_superiors_direct_complete(dev); | 
|  | 1818 | } | 
|  | 1819 |  | 
|  | 1820 | device_unlock(dev); | 
|  | 1821 | dpm_watchdog_clear(&wd); | 
|  | 1822 |  | 
|  | 1823 | Complete: | 
|  | 1824 | if (error) | 
|  | 1825 | async_error = error; | 
|  | 1826 |  | 
|  | 1827 | complete_all(&dev->power.completion); | 
|  | 1828 | TRACE_SUSPEND(error); | 
|  | 1829 | return error; | 
|  | 1830 | } | 
|  | 1831 |  | 
|  | 1832 | static void async_suspend(void *data, async_cookie_t cookie) | 
|  | 1833 | { | 
|  | 1834 | struct device *dev = (struct device *)data; | 
|  | 1835 | int error; | 
|  | 1836 |  | 
|  | 1837 | error = __device_suspend(dev, pm_transition, true); | 
|  | 1838 | if (error) { | 
|  | 1839 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1840 | pm_dev_err(dev, pm_transition, " async", error); | 
|  | 1841 | } | 
|  | 1842 |  | 
|  | 1843 | put_device(dev); | 
|  | 1844 | } | 
|  | 1845 |  | 
|  | 1846 | static int device_suspend(struct device *dev) | 
|  | 1847 | { | 
|  | 1848 | reinit_completion(&dev->power.completion); | 
|  | 1849 |  | 
|  | 1850 | if (is_async(dev)) { | 
|  | 1851 | get_device(dev); | 
|  | 1852 | async_schedule(async_suspend, dev); | 
|  | 1853 | return 0; | 
|  | 1854 | } | 
|  | 1855 |  | 
|  | 1856 | return __device_suspend(dev, pm_transition, false); | 
|  | 1857 | } | 
|  | 1858 |  | 
|  | 1859 | /** | 
|  | 1860 | * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. | 
|  | 1861 | * @state: PM transition of the system being carried out. | 
|  | 1862 | */ | 
|  | 1863 | int dpm_suspend(pm_message_t state) | 
|  | 1864 | { | 
|  | 1865 | ktime_t starttime = ktime_get(); | 
|  | 1866 | int error = 0; | 
|  | 1867 |  | 
|  | 1868 | trace_suspend_resume(TPS("dpm_suspend"), state.event, true); | 
|  | 1869 | might_sleep(); | 
|  | 1870 |  | 
|  | 1871 | cpufreq_suspend(); | 
|  | 1872 |  | 
|  | 1873 | mutex_lock(&dpm_list_mtx); | 
|  | 1874 | pm_transition = state; | 
|  | 1875 | async_error = 0; | 
|  | 1876 | while (!list_empty(&dpm_prepared_list)) { | 
|  | 1877 | struct device *dev = to_device(dpm_prepared_list.prev); | 
|  | 1878 |  | 
|  | 1879 | get_device(dev); | 
|  | 1880 | mutex_unlock(&dpm_list_mtx); | 
|  | 1881 |  | 
|  | 1882 | error = device_suspend(dev); | 
|  | 1883 |  | 
|  | 1884 | mutex_lock(&dpm_list_mtx); | 
|  | 1885 | if (error) { | 
|  | 1886 | pm_dev_err(dev, state, "", error); | 
|  | 1887 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 1888 | put_device(dev); | 
|  | 1889 | break; | 
|  | 1890 | } | 
|  | 1891 | if (!list_empty(&dev->power.entry)) | 
|  | 1892 | list_move(&dev->power.entry, &dpm_suspended_list); | 
|  | 1893 | put_device(dev); | 
|  | 1894 | if (async_error) | 
|  | 1895 | break; | 
|  | 1896 | } | 
|  | 1897 | mutex_unlock(&dpm_list_mtx); | 
|  | 1898 | async_synchronize_full(); | 
|  | 1899 | if (!error) | 
|  | 1900 | error = async_error; | 
|  | 1901 | if (error) { | 
|  | 1902 | suspend_stats.failed_suspend++; | 
|  | 1903 | dpm_save_failed_step(SUSPEND_SUSPEND); | 
|  | 1904 | } | 
|  | 1905 | dpm_show_time(starttime, state, error, NULL); | 
|  | 1906 | trace_suspend_resume(TPS("dpm_suspend"), state.event, false); | 
|  | 1907 | return error; | 
|  | 1908 | } | 
|  | 1909 |  | 
|  | 1910 | /** | 
|  | 1911 | * device_prepare - Prepare a device for system power transition. | 
|  | 1912 | * @dev: Device to handle. | 
|  | 1913 | * @state: PM transition of the system being carried out. | 
|  | 1914 | * | 
|  | 1915 | * Execute the ->prepare() callback(s) for given device.  No new children of the | 
|  | 1916 | * device may be registered after this function has returned. | 
|  | 1917 | */ | 
|  | 1918 | static int device_prepare(struct device *dev, pm_message_t state) | 
|  | 1919 | { | 
|  | 1920 | int (*callback)(struct device *) = NULL; | 
|  | 1921 | int ret = 0; | 
|  | 1922 |  | 
|  | 1923 | if (dev->power.syscore) | 
|  | 1924 | return 0; | 
|  | 1925 |  | 
|  | 1926 | WARN_ON(!pm_runtime_enabled(dev) && | 
|  | 1927 | dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND | | 
|  | 1928 | DPM_FLAG_LEAVE_SUSPENDED)); | 
|  | 1929 |  | 
|  | 1930 | /* | 
|  | 1931 | * If a device's parent goes into runtime suspend at the wrong time, | 
|  | 1932 | * it won't be possible to resume the device.  To prevent this we | 
|  | 1933 | * block runtime suspend here, during the prepare phase, and allow | 
|  | 1934 | * it again during the complete phase. | 
|  | 1935 | */ | 
|  | 1936 | pm_runtime_get_noresume(dev); | 
|  | 1937 |  | 
|  | 1938 | device_lock(dev); | 
|  | 1939 |  | 
|  | 1940 | dev->power.wakeup_path = false; | 
|  | 1941 |  | 
|  | 1942 | if (dev->power.no_pm_callbacks) | 
|  | 1943 | goto unlock; | 
|  | 1944 |  | 
|  | 1945 | if (dev->pm_domain) | 
|  | 1946 | callback = dev->pm_domain->ops.prepare; | 
|  | 1947 | else if (dev->type && dev->type->pm) | 
|  | 1948 | callback = dev->type->pm->prepare; | 
|  | 1949 | else if (dev->class && dev->class->pm) | 
|  | 1950 | callback = dev->class->pm->prepare; | 
|  | 1951 | else if (dev->bus && dev->bus->pm) | 
|  | 1952 | callback = dev->bus->pm->prepare; | 
|  | 1953 |  | 
|  | 1954 | if (!callback && dev->driver && dev->driver->pm) | 
|  | 1955 | callback = dev->driver->pm->prepare; | 
|  | 1956 |  | 
|  | 1957 | if (callback) | 
|  | 1958 | ret = callback(dev); | 
|  | 1959 |  | 
|  | 1960 | unlock: | 
|  | 1961 | device_unlock(dev); | 
|  | 1962 |  | 
|  | 1963 | if (ret < 0) { | 
|  | 1964 | suspend_report_result(callback, ret); | 
|  | 1965 | pm_runtime_put(dev); | 
|  | 1966 | return ret; | 
|  | 1967 | } | 
|  | 1968 | /* | 
|  | 1969 | * A positive return value from ->prepare() means "this device appears | 
|  | 1970 | * to be runtime-suspended and its state is fine, so if it really is | 
|  | 1971 | * runtime-suspended, you can leave it in that state provided that you | 
|  | 1972 | * will do the same thing with all of its descendants".  This only | 
|  | 1973 | * applies to suspend transitions, however. | 
|  | 1974 | */ | 
|  | 1975 | spin_lock_irq(&dev->power.lock); | 
|  | 1976 | dev->power.direct_complete = state.event == PM_EVENT_SUSPEND && | 
|  | 1977 | ((pm_runtime_suspended(dev) && ret > 0) || | 
|  | 1978 | dev->power.no_pm_callbacks) && | 
|  | 1979 | !dev_pm_test_driver_flags(dev, DPM_FLAG_NEVER_SKIP); | 
|  | 1980 | spin_unlock_irq(&dev->power.lock); | 
|  | 1981 | return 0; | 
|  | 1982 | } | 
|  | 1983 |  | 
|  | 1984 | /** | 
|  | 1985 | * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. | 
|  | 1986 | * @state: PM transition of the system being carried out. | 
|  | 1987 | * | 
|  | 1988 | * Execute the ->prepare() callback(s) for all devices. | 
|  | 1989 | */ | 
|  | 1990 | int dpm_prepare(pm_message_t state) | 
|  | 1991 | { | 
|  | 1992 | int error = 0; | 
|  | 1993 |  | 
|  | 1994 | trace_suspend_resume(TPS("dpm_prepare"), state.event, true); | 
|  | 1995 | might_sleep(); | 
|  | 1996 |  | 
|  | 1997 | /* | 
|  | 1998 | * Give a chance for the known devices to complete their probes, before | 
|  | 1999 | * disable probing of devices. This sync point is important at least | 
|  | 2000 | * at boot time + hibernation restore. | 
|  | 2001 | */ | 
|  | 2002 | wait_for_device_probe(); | 
|  | 2003 | /* | 
|  | 2004 | * It is unsafe if probing of devices will happen during suspend or | 
|  | 2005 | * hibernation and system behavior will be unpredictable in this case. | 
|  | 2006 | * So, let's prohibit device's probing here and defer their probes | 
|  | 2007 | * instead. The normal behavior will be restored in dpm_complete(). | 
|  | 2008 | */ | 
|  | 2009 | device_block_probing(); | 
|  | 2010 |  | 
|  | 2011 | mutex_lock(&dpm_list_mtx); | 
|  | 2012 | while (!list_empty(&dpm_list)) { | 
|  | 2013 | struct device *dev = to_device(dpm_list.next); | 
|  | 2014 |  | 
|  | 2015 | get_device(dev); | 
|  | 2016 | mutex_unlock(&dpm_list_mtx); | 
|  | 2017 |  | 
|  | 2018 | trace_device_pm_callback_start(dev, "", state.event); | 
|  | 2019 | error = device_prepare(dev, state); | 
|  | 2020 | trace_device_pm_callback_end(dev, error); | 
|  | 2021 |  | 
|  | 2022 | mutex_lock(&dpm_list_mtx); | 
|  | 2023 | if (error) { | 
|  | 2024 | if (error == -EAGAIN) { | 
|  | 2025 | put_device(dev); | 
|  | 2026 | error = 0; | 
|  | 2027 | continue; | 
|  | 2028 | } | 
|  | 2029 | printk(KERN_INFO "PM: Device %s not prepared " | 
|  | 2030 | "for power transition: code %d\n", | 
|  | 2031 | dev_name(dev), error); | 
|  | 2032 | dpm_save_failed_dev(dev_name(dev)); | 
|  | 2033 | put_device(dev); | 
|  | 2034 | break; | 
|  | 2035 | } | 
|  | 2036 | dev->power.is_prepared = true; | 
|  | 2037 | if (!list_empty(&dev->power.entry)) | 
|  | 2038 | list_move_tail(&dev->power.entry, &dpm_prepared_list); | 
|  | 2039 | put_device(dev); | 
|  | 2040 | } | 
|  | 2041 | mutex_unlock(&dpm_list_mtx); | 
|  | 2042 | trace_suspend_resume(TPS("dpm_prepare"), state.event, false); | 
|  | 2043 | return error; | 
|  | 2044 | } | 
|  | 2045 |  | 
|  | 2046 | /** | 
|  | 2047 | * dpm_suspend_start - Prepare devices for PM transition and suspend them. | 
|  | 2048 | * @state: PM transition of the system being carried out. | 
|  | 2049 | * | 
|  | 2050 | * Prepare all non-sysdev devices for system PM transition and execute "suspend" | 
|  | 2051 | * callbacks for them. | 
|  | 2052 | */ | 
|  | 2053 | int dpm_suspend_start(pm_message_t state) | 
|  | 2054 | { | 
|  | 2055 | int error; | 
|  | 2056 |  | 
|  | 2057 | error = dpm_prepare(state); | 
|  | 2058 | if (error) { | 
|  | 2059 | suspend_stats.failed_prepare++; | 
|  | 2060 | dpm_save_failed_step(SUSPEND_PREPARE); | 
|  | 2061 | } else | 
|  | 2062 | error = dpm_suspend(state); | 
|  | 2063 | return error; | 
|  | 2064 | } | 
|  | 2065 | EXPORT_SYMBOL_GPL(dpm_suspend_start); | 
|  | 2066 |  | 
|  | 2067 | void __suspend_report_result(const char *function, void *fn, int ret) | 
|  | 2068 | { | 
|  | 2069 | if (ret) | 
|  | 2070 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); | 
|  | 2071 | } | 
|  | 2072 | EXPORT_SYMBOL_GPL(__suspend_report_result); | 
|  | 2073 |  | 
|  | 2074 | /** | 
|  | 2075 | * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete. | 
|  | 2076 | * @dev: Device to wait for. | 
|  | 2077 | * @subordinate: Device that needs to wait for @dev. | 
|  | 2078 | */ | 
|  | 2079 | int device_pm_wait_for_dev(struct device *subordinate, struct device *dev) | 
|  | 2080 | { | 
|  | 2081 | dpm_wait(dev, subordinate->power.async_suspend); | 
|  | 2082 | return async_error; | 
|  | 2083 | } | 
|  | 2084 | EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); | 
|  | 2085 |  | 
|  | 2086 | /** | 
|  | 2087 | * dpm_for_each_dev - device iterator. | 
|  | 2088 | * @data: data for the callback. | 
|  | 2089 | * @fn: function to be called for each device. | 
|  | 2090 | * | 
|  | 2091 | * Iterate over devices in dpm_list, and call @fn for each device, | 
|  | 2092 | * passing it @data. | 
|  | 2093 | */ | 
|  | 2094 | void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *)) | 
|  | 2095 | { | 
|  | 2096 | struct device *dev; | 
|  | 2097 |  | 
|  | 2098 | if (!fn) | 
|  | 2099 | return; | 
|  | 2100 |  | 
|  | 2101 | device_pm_lock(); | 
|  | 2102 | list_for_each_entry(dev, &dpm_list, power.entry) | 
|  | 2103 | fn(dev, data); | 
|  | 2104 | device_pm_unlock(); | 
|  | 2105 | } | 
|  | 2106 | EXPORT_SYMBOL_GPL(dpm_for_each_dev); | 
|  | 2107 |  | 
|  | 2108 | static bool pm_ops_is_empty(const struct dev_pm_ops *ops) | 
|  | 2109 | { | 
|  | 2110 | if (!ops) | 
|  | 2111 | return true; | 
|  | 2112 |  | 
|  | 2113 | return !ops->prepare && | 
|  | 2114 | !ops->suspend && | 
|  | 2115 | !ops->suspend_late && | 
|  | 2116 | !ops->suspend_noirq && | 
|  | 2117 | !ops->resume_noirq && | 
|  | 2118 | !ops->resume_early && | 
|  | 2119 | !ops->resume && | 
|  | 2120 | !ops->complete; | 
|  | 2121 | } | 
|  | 2122 |  | 
|  | 2123 | void device_pm_check_callbacks(struct device *dev) | 
|  | 2124 | { | 
|  | 2125 | spin_lock_irq(&dev->power.lock); | 
|  | 2126 | dev->power.no_pm_callbacks = | 
|  | 2127 | (!dev->bus || (pm_ops_is_empty(dev->bus->pm) && | 
|  | 2128 | !dev->bus->suspend && !dev->bus->resume)) && | 
|  | 2129 | (!dev->class || pm_ops_is_empty(dev->class->pm)) && | 
|  | 2130 | (!dev->type || pm_ops_is_empty(dev->type->pm)) && | 
|  | 2131 | (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) && | 
|  | 2132 | (!dev->driver || (pm_ops_is_empty(dev->driver->pm) && | 
|  | 2133 | !dev->driver->suspend && !dev->driver->resume)); | 
|  | 2134 | spin_unlock_irq(&dev->power.lock); | 
|  | 2135 | } | 
|  | 2136 |  | 
|  | 2137 | bool dev_pm_smart_suspend_and_suspended(struct device *dev) | 
|  | 2138 | { | 
|  | 2139 | return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) && | 
|  | 2140 | pm_runtime_status_suspended(dev); | 
|  | 2141 | } |