PM/Suspend: Introduce two new platform callbacks to avoid breakage

Commit 900af0d973856d6feb6fc088c2d0d3fde57707d3 (PM: Change suspend
code ordering) changed the ordering of suspend code in such a way
that the platform .prepare() callback is now executed after the
device drivers' late suspend callbacks have run. Unfortunately, this
turns out to break ARM platforms that need to talk via I2C to power
control devices during the .prepare() callback.

For this reason introduce two new platform suspend callbacks,
.prepare_late() and .wake(), that will be called just prior to
disabling non-boot CPUs and right after bringing them back on line,
respectively, and use them instead of .prepare() and .finish() for
ACPI suspend. Make the PM core execute the .prepare() and .finish()
platform suspend callbacks where they were executed previously (that
is, right after calling the regular suspend methods provided by
device drivers and right before executing their regular resume
methods, respectively).

It is not necessary to make analogous changes to the hibernation
code and data structures at the moment, because they are only used
by ACPI platforms.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Reported-by: Russell King <rmk+kernel@arm.linux.org.uk>
Acked-by: Len Brown <len.brown@intel.com>

+47 -21
+4 -4
drivers/acpi/sleep.c
··· 300 static struct platform_suspend_ops acpi_suspend_ops = { 301 .valid = acpi_suspend_state_valid, 302 .begin = acpi_suspend_begin, 303 - .prepare = acpi_pm_prepare, 304 .enter = acpi_suspend_enter, 305 - .finish = acpi_pm_finish, 306 .end = acpi_pm_end, 307 }; 308 ··· 328 static struct platform_suspend_ops acpi_suspend_ops_old = { 329 .valid = acpi_suspend_state_valid, 330 .begin = acpi_suspend_begin_old, 331 - .prepare = acpi_pm_disable_gpes, 332 .enter = acpi_suspend_enter, 333 - .finish = acpi_pm_finish, 334 .end = acpi_pm_end, 335 .recover = acpi_pm_finish, 336 };
··· 300 static struct platform_suspend_ops acpi_suspend_ops = { 301 .valid = acpi_suspend_state_valid, 302 .begin = acpi_suspend_begin, 303 + .prepare_late = acpi_pm_prepare, 304 .enter = acpi_suspend_enter, 305 + .wake = acpi_pm_finish, 306 .end = acpi_pm_end, 307 }; 308 ··· 328 static struct platform_suspend_ops acpi_suspend_ops_old = { 329 .valid = acpi_suspend_state_valid, 330 .begin = acpi_suspend_begin_old, 331 + .prepare_late = acpi_pm_disable_gpes, 332 .enter = acpi_suspend_enter, 333 + .wake = acpi_pm_finish, 334 .end = acpi_pm_end, 335 .recover = acpi_pm_finish, 336 };
+26 -10
include/linux/suspend.h
··· 58 * by @begin(). 59 * @prepare() is called right after devices have been suspended (ie. the 60 * appropriate .suspend() method has been executed for each device) and 61 - * before the nonboot CPUs are disabled (it is executed with IRQs enabled). 62 - * This callback is optional. It returns 0 on success or a negative 63 - * error code otherwise, in which case the system cannot enter the desired 64 - * sleep state (@enter() and @finish() will not be called in that case). 65 * 66 * @enter: Enter the system sleep state indicated by @begin() or represented by 67 * the argument if @begin() is not implemented. ··· 76 * error code otherwise, in which case the system cannot enter the desired 77 * sleep state. 78 * 79 - * @finish: Called when the system has just left a sleep state, right after 80 - * the nonboot CPUs have been enabled and before devices are resumed (it is 81 - * executed with IRQs enabled). 82 * This callback is optional, but should be implemented by the platforms 83 * that implement @prepare(). If implemented, it is always called after 84 - * @enter() (even if @enter() fails). 85 * 86 * @end: Called by the PM core right after resuming devices, to indicate to 87 * the platform that the system has returned to the working state or 88 * the transition to the sleep state has been aborted. 89 * This callback is optional, but should be implemented by the platforms 90 - * that implement @begin(), but platforms implementing @begin() should 91 - * also provide a @end() which cleans up transitions aborted before 92 * @enter(). 93 * 94 * @recover: Recover the platform from a suspend failure. ··· 107 int (*valid)(suspend_state_t state); 108 int (*begin)(suspend_state_t state); 109 int (*prepare)(void); 110 int (*enter)(suspend_state_t state); 111 void (*finish)(void); 112 void (*end)(void); 113 void (*recover)(void);
··· 58 * by @begin(). 59 * @prepare() is called right after devices have been suspended (ie. the 60 * appropriate .suspend() method has been executed for each device) and 61 + * before device drivers' late suspend callbacks are executed. It returns 62 + * 0 on success or a negative error code otherwise, in which case the 63 + * system cannot enter the desired sleep state (@prepare_late(), @enter(), 64 + * @wake(), and @finish() will not be called in that case). 65 + * 66 + * @prepare_late: Finish preparing the platform for entering the system sleep 67 + * state indicated by @begin(). 68 + * @prepare_late is called before disabling nonboot CPUs and after 69 + * device drivers' late suspend callbacks have been executed. It returns 70 + * 0 on success or a negative error code otherwise, in which case the 71 + * system cannot enter the desired sleep state (@enter() and @wake()). 72 * 73 * @enter: Enter the system sleep state indicated by @begin() or represented by 74 * the argument if @begin() is not implemented. ··· 69 * error code otherwise, in which case the system cannot enter the desired 70 * sleep state. 71 * 72 + * @wake: Called when the system has just left a sleep state, right after 73 + * the nonboot CPUs have been enabled and before device drivers' early 74 + * resume callbacks are executed. 75 + * This callback is optional, but should be implemented by the platforms 76 + * that implement @prepare_late(). If implemented, it is always called 77 + * after @enter(), even if @enter() fails. 78 + * 79 + * @finish: Finish wake-up of the platform. 80 + * @finish is called right prior to calling device drivers' regular suspend 81 + * callbacks. 82 * This callback is optional, but should be implemented by the platforms 83 * that implement @prepare(). If implemented, it is always called after 84 + * @enter() and @wake(), if implemented, even if any of them fails. 85 * 86 * @end: Called by the PM core right after resuming devices, to indicate to 87 * the platform that the system has returned to the working state or 88 * the transition to the sleep state has been aborted. 89 * This callback is optional, but should be implemented by the platforms 90 + * that implement @begin(). Accordingly, platforms implementing @begin() 91 + * should also provide a @end() which cleans up transitions aborted before 92 * @enter(). 93 * 94 * @recover: Recover the platform from a suspend failure. ··· 93 int (*valid)(suspend_state_t state); 94 int (*begin)(suspend_state_t state); 95 int (*prepare)(void); 96 + int (*prepare_late)(void); 97 int (*enter)(suspend_state_t state); 98 + void (*wake)(void); 99 void (*finish)(void); 100 void (*end)(void); 101 void (*recover)(void);
+17 -7
kernel/power/main.c
··· 291 292 device_pm_lock(); 293 294 error = device_power_down(PMSG_SUSPEND); 295 if (error) { 296 printk(KERN_ERR "PM: Some devices failed to power down\n"); 297 - goto Done; 298 } 299 300 - if (suspend_ops->prepare) { 301 - error = suspend_ops->prepare(); 302 if (error) 303 goto Power_up_devices; 304 } 305 306 if (suspend_test(TEST_PLATFORM)) 307 - goto Platfrom_finish; 308 309 error = disable_nonboot_cpus(); 310 if (error || suspend_test(TEST_CPUS)) ··· 332 Enable_cpus: 333 enable_nonboot_cpus(); 334 335 - Platfrom_finish: 336 - if (suspend_ops->finish) 337 - suspend_ops->finish(); 338 339 Power_up_devices: 340 device_power_up(PMSG_RESUME); 341 342 Done: 343 device_pm_unlock();
··· 291 292 device_pm_lock(); 293 294 + if (suspend_ops->prepare) { 295 + error = suspend_ops->prepare(); 296 + if (error) 297 + goto Done; 298 + } 299 + 300 error = device_power_down(PMSG_SUSPEND); 301 if (error) { 302 printk(KERN_ERR "PM: Some devices failed to power down\n"); 303 + goto Platfrom_finish; 304 } 305 306 + if (suspend_ops->prepare_late) { 307 + error = suspend_ops->prepare_late(); 308 if (error) 309 goto Power_up_devices; 310 } 311 312 if (suspend_test(TEST_PLATFORM)) 313 + goto Platform_wake; 314 315 error = disable_nonboot_cpus(); 316 if (error || suspend_test(TEST_CPUS)) ··· 326 Enable_cpus: 327 enable_nonboot_cpus(); 328 329 + Platform_wake: 330 + if (suspend_ops->wake) 331 + suspend_ops->wake(); 332 333 Power_up_devices: 334 device_power_up(PMSG_RESUME); 335 + 336 + Platfrom_finish: 337 + if (suspend_ops->finish) 338 + suspend_ops->finish(); 339 340 Done: 341 device_pm_unlock();