PM: Rework suspend and hibernation code for devices
Subsystems (8)
cover PM: Rework suspend and hibernation code for devices
Hi,
The following three patches are intended to start the redesign of the suspend and hibernation framework for devices.
The first patch defines the new framework. Specifically, it introduces the structure 'struct pm_ops' containing a set of hibernation and suspend callbacks to be defined by bus types, device types, device classes and, finally, by device drivers (the role of each callback is described in the comment in pm.h; the documentation will be updated later). It also modifies the code in drivers/base/power/main.c to use the new framework, if available, and fall back to the old one otherwise (no visible functional changes should be added).
The other two patches implement the new suspend and hibernation callbacks for the platform and PCI bus types.
Please review and tell me what you think.
Thanks,
Rafael
Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
RFC PATCH 1/3 PM: Introduce new top level suspend and hibernation callbacks
From: Rafael J. Wysocki <[email protected]>
Introduce 'struct pm_ops' representing a set of suspend and hibernation operations for bus types, device classes and device types.
Modify the PM core to use 'struct pm_ops' objects, if defined, instead of the ->suspend(), ->resume(), ->suspend_late(), ->resume_early() callbacks that will be considered as legacy and gradually phased out.
---
arch/x86/kernel/apm_32.c | 4 drivers/base/power/main.c | 453 +++++++++++++++++++++++++++++++++++----------- include/linux/device.h | 8 include/linux/pm.h | 241 +++++++++++++++++++++--- kernel/power/disk.c | 14 - kernel/power/main.c | 4 6 files changed, 583 insertions(+), 141 deletions(-)
include/linux/pm.h +213 -28
@@ -114,7 +114,9 @@ typedef struct pm_message {int event; } pm_message_t; -/* +/** +* struct pm_ops - device PM callbacks +* * Several driver power state transitions are externally visible, affecting * the state of pending I/O queues and (for drivers that touch hardware) * interrupts, wakeups, DMA, and other hardware state. There may also be
@@ -122,6 +124,213 @@ typedef struct pm_message {* to the rest of the driver stack (such as a driver that's ON gating off * clocks which are not in active use). * +* The externally visible transitions are handled with the help of the following +* callbacks included in this structure: +* +* @prepare: Prepare the device for the upcoming transition, but do NOT change +* its hardware state. Prevent new children of the device from being +* registered and prevent new calls to the probe method from being made +* after @prepare() returns. Also, if a concurrent child registration +* or a call to the probe method already in progress is detected by +* @prepare(), it may return -EAGAIN, so that the PM core can execute it +* once again (e.g. after suspending the newly registered child) to recover +* from the race condition. This method is executed for all kinds of +* suspend transitions and is immediately followed by one of the suspend +* callbacks: @suspend(), @freeze(), @poweroff(), or @quiesce(). +* +* @complete: Undo the changes made by @prepare(). This method is executed for +* all kinds of resume transitions, immediately following one of the resume +* callbacks: @resume(), @thaw(), @restore(), or @recover(). Also executed +* if a suspend callback (@suspend(), @freeze(), @poweroff(), @quiesce()) +* immediately following a successful @prepare() fails OR if a new child +* of the device has been registered during @prepare(). +* +* @suspend: Executed before putting the system into a sleep state in which the +* contents of main memory are preserved. Quiesce the device, put it into +* a low power state appropriate for the upcoming system state (such as +* PCI_D3hot), and enable wakeup events as appropriate. +* +* @resume: Executed after waking the system up from a sleep state in which the +* contents of main memory were preserved. Put the device into the +* appropriate state, according to the information saved in memory by the +* preceding @suspend(). The driver starts working again, responding to +* hardware events and software requests. The hardware may have gone +* through a power-off reset, or it may have maintained state from the +* previous suspend() which the driver may rely on while resuming. On most +* platforms, there are no restrictions on availability of resources like +* clocks during @resume(). +* +* @freeze: Hibernation-specific, executed before creating a hibernation image. +* Quiesce operations so that a consistent image can be created, but do NOT +* otherwise put the device into a low power device state and do NOT emit +* system wakeup events. Save in main memory the device settings to be +* used by @restore() during the subsequent resume from hibernation. +* +* @thaw: Hibernation-specific, executed after creating a hibernation image. +* Undo the changes made by the preceding @freeze(), so the device can be +* operated in the same way as immediately before the call to @freeze(). +* +* @poweroff: Hibernation-specific, executed after saving a hibernation image. +* Quiesce the device, put it into a low power state appropriate for the +* upcoming system state (such as PCI_D3hot), and enable wakeup events as +* appropriate. +* +* @quiesce: Hibernation-specific, executed after loading a hibernation image +* and before restoring the contents of main memory from it. Quiesce +* operations so that the contents of main memory can be restored from the +* image in a consistent way, but do NOT otherwise put the device into a +* low power state and do NOT emit system wakeup events. Do NOT save any +* device settings in main memory. +* +* @restore: Hibernation-specific, executed after restoring the contents of main +* memory from a hibernation image. Driver starts working again, +* responding to hardware events and software requests. Do NOT make ANY +* assumptions about the hardware state right prior to @restore(). On most +* platforms, there are no restrictions on availability of resources like +* clocks during @restore(). +* +* @recover: Hibernation-specific, executed after a failing creation of a +* hibernation image OR after a failing attempt to restore the contents of +* main memory from such an image. Undo the changes made by the preceding +* @freeze() or @quiesce(), so the device can be operated in the same way +* as immediately before the failing transition. +* +* struct pm_ops is also used for defining driver PM operations to be carried +* out with interrupts disabled. In this case, however, there is only one +* active CPU while the operations are being performed, so they are executed by +* the PM core in a more straightforward way. In particular, the failure of +* @prepare() carried out with interrupts disabled causes the entire PM +* transition to fail, regardless of the error code returned by it. +* +* All of the above callbacks, except for @complete(), return error codes. +* However, the error codes returned by resume operations, @resume(), @thaw(), +* @restore(), and @recover(), are only printed in the system logs, since the +* PM core cannot do anything else about them. +*/ + +struct pm_ops { +#ifdef CONFIG_PM_SLEEP +int (*prepare)(struct device *dev); +void (*complete)(struct device *dev); +#endif +#ifdef CONFIG_SUSPEND +int (*suspend)(struct device *dev); +int (*resume)(struct device *dev); +#endif +#ifdef CONFIG_HIBERNATION +int (*freeze)(struct device *dev); +int (*thaw)(struct device *dev); +int (*poweroff)(struct device *dev); +int (*quiesce)(struct device *dev); +int (*restore)(struct device *dev); +int (*recover)(struct device *dev); +#endif +}; + +/** +* PM_EVENT_ messages +* +* The following PM_EVENT_ messages are defined for the internal use of the PM +* core, in order to provide a mechanism allowing the high level suspend and +* hibernation code to convey the necessary information to the device PM core +* code: +* +* ON No transition. +* +* FREEZE System is going to hibernate, call ->prepare() and ->freeze() +* for all devices. +* +* SUSPEND System is going to suspend, call ->prepare() and ->suspend() +* for all devices. +* +* HIBERNATE Hibernation image has been saved, call ->prepare() and +* ->poweroff() for all devices. +* +* QUIESCE Contents of main memory are going to be restored from a (loaded) +* hibernation image, call ->prepare() and ->quiesce() for all +* devices. +* +* RESUME System is resuming, call ->resume() and ->complete() for all +* devices. +* +* THAW Hibernation image has been created, call ->thaw() and +* ->complete() for all devices. +* +* RESTORE Contents of main memory have been restored from a hibernation +* image, call ->restore() and ->complete() for all devices. +* +* RECOVER Creation of a hibernation image or restoration of the main +* memory contents from a hibernation image has failed, call +* ->recover() and ->complete() for all devices. +*/ + +#define PM_EVENT_ON 0x0000 +#define PM_EVENT_FREEZE 0x0001 +#define PM_EVENT_SUSPEND 0x0002 +#define PM_EVENT_HIBERNATE 0x0004 +#define PM_EVENT_QUIESCE 0x0008 +#define PM_EVENT_RESUME 0x0010 +#define PM_EVENT_THAW 0x0020 +#define PM_EVENT_RESTORE 0x0040 +#define PM_EVENT_RECOVER 0x0080 + +#define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE) + +#define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, }) +#define PMSG_QUIESCE ((struct pm_message){ .event = PM_EVENT_QUIESCE, }) +#define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, }) +#define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, }) +#define PMSG_RESUME ((struct pm_message){ .event = PM_EVENT_RESUME, }) +#define PMSG_THAW ((struct pm_message){ .event = PM_EVENT_THAW, }) +#define PMSG_RESTORE ((struct pm_message){ .event = PM_EVENT_RESTORE, }) +#define PMSG_RECOVER ((struct pm_message){ .event = PM_EVENT_RECOVER, }) +#define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, }) + +/** +* Device power management states +* +* These state labels are used internally by the PM core to indicate the current +* status of a device with respect to the PM core operations. +* +* DPM_ON Device is regarded as operational. Set this way +* initially and when ->resume(), ->thaw(), ->restore(), +* ->recover(), or ->complete() is about to be called. +* Also set when ->prepare() fails. +* +* DPM_SUSPENDING Device is currently being suspended. Set when +* ->prepare() is about to be called. +* +* DPM_OFF Device is regarded as suspended. Set when ->suspend(), +* ->freeze(), ->poweroff(), or ->quiesce() is about to be +* called. +*/ + +enum dpm_state { +DPM_ON , +DPM_SUSPENDING , +DPM_OFF , +}; + +struct dev_pm_info { +pm_message_t power_state; +unsigned can_wakeup:1 ; +unsigned should_wakeup:1 ; +enum dpm_state status:2 ;/* Owned by the PM core */ +#ifdef CONFIG_PM_SLEEP +struct list_head entry; +#endif +}; + +/* +* The PM_EVENT_ messages are also used by drivers implementing the legacy +* suspend framework, based on the ->suspend() and ->resume() callbacks common +* for suspend and hibernation transitions, according to the rules below. +*/ + +/* Necessary, because several drivers use PM_EVENT_PRETHAW */ +#define PM_EVENT_PRETHAW PM_EVENT_QUIESCE + +/* * One transition is triggered by resume(), after a suspend() call; the * message is implicit: *
@@ -166,35 +375,11 @@ typedef struct pm_message {* or from system low-power states such as standby or suspend-to-RAM. */ -#define PM_EVENT_ON 0 -#define PM_EVENT_FREEZE 1 -#define PM_EVENT_SUSPEND 2 -#define PM_EVENT_HIBERNATE 4 -#define PM_EVENT_PRETHAW 8 - -#define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE) - -#define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, }) -#define PMSG_PRETHAW ((struct pm_message){ .event = PM_EVENT_PRETHAW, }) -#define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, }) -#define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, }) -#define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, }) - -struct dev_pm_info { -pm_message_t power_state; -unsigned can_wakeup:1 ; -unsigned should_wakeup:1 ; -bool sleeping:1 ;/* Owned by the PM core */ -#ifdef CONFIG_PM_SLEEP -struct list_head entry; -#endif -}; +#ifdef CONFIG_PM_SLEEP +extern void device_power_up (pm_message_t state); +extern void device_resume (pm_message_t state);extern int device_power_down (pm_message_t state); -extern void device_power_up (void ); -extern void device_resume (void ); - -#ifdef CONFIG_PM_SLEEP extern int device_suspend (pm_message_t state);extern int device_prepare_suspend (pm_message_t state);
drivers/base/power/main.c +351 -102
635 lines not on this page · raw message
include/linux/device.h +8 -0
@@ -69,6 +69,9 @@ struct bus_type {int (*resume_early)(struct device *dev);int (*resume)(struct device *dev); +struct pm_ops *pm; +struct pm_ops *pm_noirq; +struct bus_type_private *p; };
@@ -202,6 +205,8 @@ struct class {int (*suspend)(struct device *dev,pm_message_t state);int (*resume)(struct device *dev); + +struct pm_ops *pm; };extern int __must_check class_register (struct class *class);
@@ -345,8 +350,11 @@ struct device_type {struct attribute_group **groups;int (*uevent)(struct device *dev,struct kobj_uevent_env *env);void (*release)(struct device *dev); +int (*suspend)(struct device *dev,pm_message_t state);int (*resume)(struct device *dev); + +struct pm_ops *pm; };/* interface for exporting device attributes */
kernel/power/disk.c +7 -7
@@ -224,7 +224,7 @@ static int create_image(int platform_mod/* NOTE: device_power_up() is just a resume() for devices * that suspended with irqs off ... no overall powerup. */ -device_power_up (); +device_power_up (in_suspend ?PMSG_RECOVER :PMSG_RESTORE ); Enable_irqs:local_irq_enable ();return error;
@@ -280,7 +280,7 @@ int hibernation_snapshot(int platform_mo Finish:platform_finish (platform_mode); Resume_devices: -device_resume (); +device_resume (in_suspend ?PMSG_RECOVER :PMSG_RESTORE ); Resume_console:resume_console (); Close:
@@ -301,7 +301,7 @@ static int resume_target_kernel(void)int error;local_irq_disable (); - error =device_power_down (PMSG_PRETHAW ); + error =device_power_down (PMSG_QUIESCE );if (error) {printk (KERN_ERR "PM: Some devices failed to power down, " "aborting resume\n" );
@@ -329,7 +329,7 @@ static int resume_target_kernel(void)swsusp_free ();restore_processor_state ();touch_softlockup_watchdog (); -device_power_up (); +device_power_up (PMSG_THAW ); Enable_irqs:local_irq_enable ();return error;
@@ -350,7 +350,7 @@ int hibernation_restore(int platform_modpm_prepare_console ();suspend_console (); - error =device_suspend (PMSG_PRETHAW ); + error =device_suspend (PMSG_QUIESCE );if (error) goto Finish;
@@ -362,7 +362,7 @@ int hibernation_restore(int platform_modenable_nonboot_cpus (); }platform_restore_cleanup (platform_mode); -device_resume (); +device_resume (PMSG_RECOVER ); Finish:resume_console ();pm_restore_console ();
@@ -419,7 +419,7 @@ int hibernation_platform_enter(void) Finish: hibernation_ops->finish (); Resume_devices: -device_resume (); +device_resume (PMSG_RESTORE ); Resume_console:resume_console (); Close:
kernel/power/main.c +2 -2
@@ -239,7 +239,7 @@ static int suspend_enter(suspend_state_tif (!suspend_test (TEST_CORE )) error = suspend_ops->enter (state); -device_power_up (); +device_power_up (PMSG_RESUME ); Done:arch_suspend_enable_irqs ();BUG_ON (irqs_disabled ());
@@ -291,7 +291,7 @@ int suspend_devices_and_enter(suspend_stif (suspend_ops->finish) suspend_ops->finish (); Resume_devices: -device_resume (); +device_resume (PMSG_RESUME ); Resume_console:resume_console (); Close:
arch/x86/kernel/apm_32.c +2 -2
@@ -1208,9 +1208,9 @@ static int suspend(int vetoable)if (err !=APM_SUCCESS )apm_error ("suspend" , err); err = (err ==APM_SUCCESS ) ?0 : -EIO ; -device_power_up (); +device_power_up (PMSG_RESUME );local_irq_enable (); -device_resume (); +device_resume (PMSG_RESUME );queue_event (APM_NORMAL_RESUME ,NULL ); out:spin_lock (&user_list_lock);
RFC PATCH 2/3 PM: New suspend and hibernation callbacks for platform bus type
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the platform bus type.
--- drivers/base/platform.c | 444 ++++++++++++++++++++++++++++++++++++++-- include/linux/platform_device.h | 2 2 files changed, 425 insertions(+), 21 deletions(-)
include/linux/platform_device.h +2 -0
@@ -53,6 +53,8 @@ struct platform_driver {int (*suspend_late)(struct platform_device *,pm_message_t state);int (*resume_early)(struct platform_device *);int (*resume)(struct platform_device *); +struct pm_ops *pm; +struct pm_ops *pm_noirq;struct device_driver driver; };
drivers/base/platform.c +423 -21
484 lines not on this page · raw message
RFC PATCH 2/3 PM: New suspend and hibernation callbacks for platform bus type
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the platform bus type.
--- drivers/base/platform.c | 444 ++++++++++++++++++++++++++++++++++++++-- include/linux/platform_device.h | 2 2 files changed, 425 insertions(+), 21 deletions(-)
include/linux/platform_device.h +2 -0
@@ -53,6 +53,8 @@ struct platform_driver {int (*suspend_late)(struct platform_device *,pm_message_t state);int (*resume_early)(struct platform_device *);int (*resume)(struct platform_device *); +struct pm_ops *pm; +struct pm_ops *pm_noirq;struct device_driver driver; };
drivers/base/platform.c +423 -21
484 lines not on this page · raw message
RFC PATCH 3/3 PM: New suspend and hibernation callbacks for PCI bus type
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)
drivers/pci/pci-driver.c +451 -46
@@ -271,45 +271,78 @@ static int pci_device_remove(struct devireturn 0 ; } -static int pci_device_suspend (struct device * dev,pm_message_t state) +static void pci_device_shutdown (struct device *dev) { -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver; -int i =0 ; - -if (drv && drv->suspend) { - i = drv->suspend (pci_dev, state); -suspend_report_result (drv->suspend, i); - }else { -pci_save_state (pci_dev); -/* -* mark its power state as "unknown", since we don't know if -* e.g. the BIOS will change its device state when we suspend. -*/ -if (pci_dev->current_state ==PCI_D0 ) - pci_dev->current_state =PCI_UNKNOWN ; - } -return i; +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->shutdown) + drv->shutdown (pci_dev); } -static int pci_device_suspend_late (struct device * dev,pm_message_t state) +#ifdef CONFIG_PM_SLEEP +static int pci_pm_prepare (struct device *dev) { -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver; -int i =0 ; +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; -if (drv && drv->suspend_late) { - i = drv->suspend_late (pci_dev, state); -suspend_report_result (drv->suspend_late, i); - } -return i; +if (drv && drv->pm && drv->pm->prepare) + error = drv->pm->prepare (dev); + +return error; +} + +static int pci_pm_prepare_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (drv && drv->pm_noirq && drv->pm_noirq->prepare) + error = drv->pm_noirq->prepare (dev); + +return error; +} + +static void pci_pm_complete (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->pm && drv->pm->complete) + drv->pm->complete (dev); +} + +static void pci_pm_complete_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->pm_noirq && drv->pm_noirq->complete) + drv->pm_noirq->complete (dev); }/* -* Default resume method for devices that have no driver provided resume, +* Default "suspend" method for devices that have no driver provided suspend, * or not even a driver at all. */ -static int pci_default_resume (struct pci_dev *pci_dev) +static void pci_default_pm_suspend (struct pci_dev *pci_dev) +{ +pci_save_state (pci_dev); +/* +* mark its power state as "unknown", since we don't know if +* e.g. the BIOS will change its device state when we suspend. +*/ +if (pci_dev->current_state ==PCI_D0 ) + pci_dev->current_state =PCI_UNKNOWN ; +} + +/* +* Default "resume" method for devices that have no driver provided resume, +* or not even a driver at all. +*/ +static int pci_default_pm_resume (struct pci_dev *pci_dev) {int retval =0 ;
@@ -324,40 +357,376 @@ static int pci_default_resume(struct pcireturn retval; } -static int pci_device_resume (struct device * dev) +#ifdef CONFIG_SUSPEND +static int pci_pm_suspend (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) { +pci_default_pm_suspend (pci_dev); +return 0 ; + } + +if (drv->pm) { +if (drv->pm->suspend) { + error = drv->pm->suspend (dev); +suspend_report_result (drv->pm->suspend, error); + }else { +pci_default_pm_suspend (pci_dev); + } + }else { +/* Legacy mechanism */ +if (drv->suspend) { + error = drv->suspend (pci_dev,PMSG_SUSPEND ); +suspend_report_result (drv->suspend, error); + }else { +pci_default_pm_suspend (pci_dev); + } + } + +return error; +} + +static int pci_pm_suspend_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->suspend) { + error = drv->pm_noirq->suspend (dev); +suspend_report_result (drv->pm_noirq->suspend, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_SUSPEND ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_resume (struct device *dev) { +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver;int error; -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver; -if (drv && drv->resume) +if (!drv) +return pci_default_pm_resume (pci_dev); + +if (drv->pm) { + error = drv->pm->resume ? + drv->pm->resume (dev) :pci_default_pm_resume (pci_dev); + }else { +/* Legacy mechanism */ + error = drv->resume ? + drv->resume (pci_dev) :pci_default_pm_resume (pci_dev); + } + +return error; +} + +static int pci_pm_resume_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +pci_fixup_device (pci_fixup_resume, pci_dev); + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->resume) + error = drv->pm_noirq->resume (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ + error = drv->resume_early (pci_dev); + } + +return error; +} +#endif /* CONFIG_SUSPEND */ + +#ifdef CONFIG_HIBERNATION +static int pci_pm_freeze (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) { +pci_default_pm_suspend (pci_dev); +return 0 ; + } + +if (drv->pm) { +if (drv->pm->freeze) { + error = drv->pm->freeze (dev); +suspend_report_result (drv->pm->freeze, error); + }else { +pci_default_pm_suspend (pci_dev); + } + }else { +/* Legacy mechanism */ +if (drv->suspend) { + error = drv->suspend (pci_dev,PMSG_FREEZE ); +suspend_report_result (drv->suspend, error); + }else { +pci_default_pm_suspend (pci_dev); + } + } + +return error; +} + +static int pci_pm_freeze_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->freeze) { + error = drv->pm_noirq->freeze (dev); +suspend_report_result (drv->pm_noirq->freeze, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_FREEZE ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_thaw (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->thaw) + error = drv->pm->thaw (dev); + }else if (drv->resume) { +/* Legacy mechanism */ error = drv->resume (pci_dev); -else - error =pci_default_resume (pci_dev); + } + +return error; +} + +static int pci_pm_thaw_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->thaw) + error = drv->pm_noirq->thaw (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ + error = drv->resume_early (pci_dev); + } + +return error; +} + +static int pci_pm_poweroff (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->poweroff) { + error = drv->pm->poweroff (dev); +suspend_report_result (drv->pm->poweroff, error); + } + }else if (drv->suspend) { +/* Legacy mechanism */ + error = drv->suspend (pci_dev,PMSG_HIBERNATE ); +suspend_report_result (drv->suspend, error); + } + +return error; +} + +static int pci_pm_poweroff_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->poweroff) { + error = drv->pm_noirq->poweroff (dev); +suspend_report_result (drv->pm_noirq->poweroff, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_HIBERNATE ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_quiesce (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->quiesce) { + error = drv->pm->quiesce (dev); +suspend_report_result (drv->pm->quiesce, error); + } + }else if (drv->suspend) { +/* Legacy mechanism */ + error = drv->suspend (pci_dev,PMSG_QUIESCE ); +suspend_report_result (drv->suspend, error); + } + +return error; +} + +static int pci_pm_quiesce_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->quiesce) { + error = drv->pm_noirq->quiesce (dev); +suspend_report_result (drv->pm_noirq->quiesce, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_QUIESCE ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_restore (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error; + +if (!drv) +return pci_default_pm_resume (pci_dev); + +if (drv->pm) { + error = drv->pm->restore ? + drv->pm->restore (dev) :pci_default_pm_resume (pci_dev); + }else { +/* Legacy mechanism */ + error = drv->resume ? + drv->resume (pci_dev) :pci_default_pm_resume (pci_dev); + } +return error; } -static int pci_device_resume_early (struct device * dev) +static int pci_pm_restore_noirq (struct device *dev) { +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver;int error =0 ; -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver;pci_fixup_device (pci_fixup_resume, pci_dev); -if (drv && drv->resume_early) +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->restore) + error = drv->pm_noirq->restore (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ error = drv->resume_early (pci_dev); + } +return error; } -static void pci_device_shutdown (struct device *dev) +static int pci_pm_recover (struct device *dev) {struct pci_dev *pci_dev =to_pci_dev (dev);struct pci_driver *drv = pci_dev->driver; +int error =0 ; -if (drv && drv->shutdown) - drv->shutdown (pci_dev); +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->recover) + error = drv->pm->recover (dev); + }else if (drv->resume) { +/* Legacy mechanism */ + error = drv->resume (pci_dev); + } + +return error; +} + +static int pci_pm_recover_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->recover) + error = drv->pm_noirq->recover (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ + error = drv->resume_early (pci_dev); + } + +return error; } +#endif /* CONFIG_HIBERNATION */ +#endif /* CONFIG_PM_SLEEP */ /** * __pci_register_driver - register a new pci driver
@@ -500,18 +869,54 @@ int pci_uevent(struct device *dev, struc }#endif +struct pm_ops pci_pm_ops = { +#ifdef CONFIG_PM_SLEEP + .prepare = pci_pm_prepare, + .complete = pci_pm_complete, +#endif +#ifdef CONFIG_SUSPEND + .suspend = pci_pm_suspend, + .resume = pci_pm_resume, +#endif +#ifdef CONFIG_HIBERNATION + .freeze = pci_pm_freeze, + .thaw = pci_pm_thaw, + .poweroff = pci_pm_poweroff, + .quiesce = pci_pm_quiesce, + .restore = pci_pm_restore, + .recover = pci_pm_recover, +#endif +}; + +struct pm_ops pci_pm_ops_noirq = { +#ifdef CONFIG_PM_SLEEP + .prepare = pci_pm_prepare_noirq, + .complete = pci_pm_complete_noirq, +#endif +#ifdef CONFIG_SUSPEND + .suspend = pci_pm_suspend_noirq, + .resume = pci_pm_resume_noirq, +#endif +#ifdef CONFIG_HIBERNATION + .freeze = pci_pm_freeze_noirq, + .thaw = pci_pm_thaw_noirq, + .poweroff = pci_pm_poweroff_noirq, + .quiesce = pci_pm_quiesce_noirq, + .restore = pci_pm_restore_noirq, + .recover = pci_pm_recover_noirq, +#endif +}; +struct bus_type pci_bus_type = { .name ="pci" , .match = pci_bus_match, .uevent = pci_uevent, .probe = pci_device_probe, .remove = pci_device_remove, - .suspend = pci_device_suspend, - .suspend_late = pci_device_suspend_late, - .resume_early = pci_device_resume_early, - .resume = pci_device_resume, .shutdown = pci_device_shutdown, .dev_attrs = pci_dev_attrs, + .pm = &pci_pm_ops, + .pm_noirq = &pci_pm_ops_noirq, };static int __init pci_driver_init (void )
include/linux/pci.h +2 -0
@@ -382,6 +382,8 @@ struct pci_driver {int (*resume) (struct pci_dev *dev);/* Device woken up */ void (*shutdown) (struct pci_dev *dev); +struct pm_ops *pm; +struct pm_ops *pm_noirq;struct pci_error_handlers *err_handler;struct device_driver driver;struct pci_dynids dynids;
RFC PATCH 1/3 PM: Introduce new top level suspend and hibernation callbacks
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Introduce 'struct pm_ops' representing a set of suspend and hibernation operations for bus types, device classes and device types.
Modify the PM core to use 'struct pm_ops' objects, if defined, instead of the ->suspend(), ->resume(), ->suspend_late(), ->resume_early() callbacks that will be considered as legacy and gradually phased out.
---
arch/x86/kernel/apm_32.c | 4 drivers/base/power/main.c | 453 +++++++++++++++++++++++++++++++++++----------- include/linux/device.h | 8 include/linux/pm.h | 241 +++++++++++++++++++++--- kernel/power/disk.c | 14 - kernel/power/main.c | 4 6 files changed, 583 insertions(+), 141 deletions(-)
include/linux/pm.h +213 -28
@@ -114,7 +114,9 @@ typedef struct pm_message {int event; } pm_message_t; -/* +/** +* struct pm_ops - device PM callbacks +* * Several driver power state transitions are externally visible, affecting * the state of pending I/O queues and (for drivers that touch hardware) * interrupts, wakeups, DMA, and other hardware state. There may also be
@@ -122,6 +124,213 @@ typedef struct pm_message {* to the rest of the driver stack (such as a driver that's ON gating off * clocks which are not in active use). * +* The externally visible transitions are handled with the help of the following +* callbacks included in this structure: +* +* @prepare: Prepare the device for the upcoming transition, but do NOT change +* its hardware state. Prevent new children of the device from being +* registered and prevent new calls to the probe method from being made +* after @prepare() returns. Also, if a concurrent child registration +* or a call to the probe method already in progress is detected by +* @prepare(), it may return -EAGAIN, so that the PM core can execute it +* once again (e.g. after suspending the newly registered child) to recover +* from the race condition. This method is executed for all kinds of +* suspend transitions and is immediately followed by one of the suspend +* callbacks: @suspend(), @freeze(), @poweroff(), or @quiesce(). +* +* @complete: Undo the changes made by @prepare(). This method is executed for +* all kinds of resume transitions, immediately following one of the resume +* callbacks: @resume(), @thaw(), @restore(), or @recover(). Also executed +* if a suspend callback (@suspend(), @freeze(), @poweroff(), @quiesce()) +* immediately following a successful @prepare() fails OR if a new child +* of the device has been registered during @prepare(). +* +* @suspend: Executed before putting the system into a sleep state in which the +* contents of main memory are preserved. Quiesce the device, put it into +* a low power state appropriate for the upcoming system state (such as +* PCI_D3hot), and enable wakeup events as appropriate. +* +* @resume: Executed after waking the system up from a sleep state in which the +* contents of main memory were preserved. Put the device into the +* appropriate state, according to the information saved in memory by the +* preceding @suspend(). The driver starts working again, responding to +* hardware events and software requests. The hardware may have gone +* through a power-off reset, or it may have maintained state from the +* previous suspend() which the driver may rely on while resuming. On most +* platforms, there are no restrictions on availability of resources like +* clocks during @resume(). +* +* @freeze: Hibernation-specific, executed before creating a hibernation image. +* Quiesce operations so that a consistent image can be created, but do NOT +* otherwise put the device into a low power device state and do NOT emit +* system wakeup events. Save in main memory the device settings to be +* used by @restore() during the subsequent resume from hibernation. +* +* @thaw: Hibernation-specific, executed after creating a hibernation image. +* Undo the changes made by the preceding @freeze(), so the device can be +* operated in the same way as immediately before the call to @freeze(). +* +* @poweroff: Hibernation-specific, executed after saving a hibernation image. +* Quiesce the device, put it into a low power state appropriate for the +* upcoming system state (such as PCI_D3hot), and enable wakeup events as +* appropriate. +* +* @quiesce: Hibernation-specific, executed after loading a hibernation image +* and before restoring the contents of main memory from it. Quiesce +* operations so that the contents of main memory can be restored from the +* image in a consistent way, but do NOT otherwise put the device into a +* low power state and do NOT emit system wakeup events. Do NOT save any +* device settings in main memory. +* +* @restore: Hibernation-specific, executed after restoring the contents of main +* memory from a hibernation image. Driver starts working again, +* responding to hardware events and software requests. Do NOT make ANY +* assumptions about the hardware state right prior to @restore(). On most +* platforms, there are no restrictions on availability of resources like +* clocks during @restore(). +* +* @recover: Hibernation-specific, executed after a failing creation of a +* hibernation image OR after a failing attempt to restore the contents of +* main memory from such an image. Undo the changes made by the preceding +* @freeze() or @quiesce(), so the device can be operated in the same way +* as immediately before the failing transition. +* +* struct pm_ops is also used for defining driver PM operations to be carried +* out with interrupts disabled. In this case, however, there is only one +* active CPU while the operations are being performed, so they are executed by +* the PM core in a more straightforward way. In particular, the failure of +* @prepare() carried out with interrupts disabled causes the entire PM +* transition to fail, regardless of the error code returned by it. +* +* All of the above callbacks, except for @complete(), return error codes. +* However, the error codes returned by resume operations, @resume(), @thaw(), +* @restore(), and @recover(), are only printed in the system logs, since the +* PM core cannot do anything else about them. +*/ + +struct pm_ops { +#ifdef CONFIG_PM_SLEEP +int (*prepare)(struct device *dev); +void (*complete)(struct device *dev); +#endif +#ifdef CONFIG_SUSPEND +int (*suspend)(struct device *dev); +int (*resume)(struct device *dev); +#endif +#ifdef CONFIG_HIBERNATION +int (*freeze)(struct device *dev); +int (*thaw)(struct device *dev); +int (*poweroff)(struct device *dev); +int (*quiesce)(struct device *dev); +int (*restore)(struct device *dev); +int (*recover)(struct device *dev); +#endif +}; + +/** +* PM_EVENT_ messages +* +* The following PM_EVENT_ messages are defined for the internal use of the PM +* core, in order to provide a mechanism allowing the high level suspend and +* hibernation code to convey the necessary information to the device PM core +* code: +* +* ON No transition. +* +* FREEZE System is going to hibernate, call ->prepare() and ->freeze() +* for all devices. +* +* SUSPEND System is going to suspend, call ->prepare() and ->suspend() +* for all devices. +* +* HIBERNATE Hibernation image has been saved, call ->prepare() and +* ->poweroff() for all devices. +* +* QUIESCE Contents of main memory are going to be restored from a (loaded) +* hibernation image, call ->prepare() and ->quiesce() for all +* devices. +* +* RESUME System is resuming, call ->resume() and ->complete() for all +* devices. +* +* THAW Hibernation image has been created, call ->thaw() and +* ->complete() for all devices. +* +* RESTORE Contents of main memory have been restored from a hibernation +* image, call ->restore() and ->complete() for all devices. +* +* RECOVER Creation of a hibernation image or restoration of the main +* memory contents from a hibernation image has failed, call +* ->recover() and ->complete() for all devices. +*/ + +#define PM_EVENT_ON 0x0000 +#define PM_EVENT_FREEZE 0x0001 +#define PM_EVENT_SUSPEND 0x0002 +#define PM_EVENT_HIBERNATE 0x0004 +#define PM_EVENT_QUIESCE 0x0008 +#define PM_EVENT_RESUME 0x0010 +#define PM_EVENT_THAW 0x0020 +#define PM_EVENT_RESTORE 0x0040 +#define PM_EVENT_RECOVER 0x0080 + +#define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE) + +#define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, }) +#define PMSG_QUIESCE ((struct pm_message){ .event = PM_EVENT_QUIESCE, }) +#define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, }) +#define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, }) +#define PMSG_RESUME ((struct pm_message){ .event = PM_EVENT_RESUME, }) +#define PMSG_THAW ((struct pm_message){ .event = PM_EVENT_THAW, }) +#define PMSG_RESTORE ((struct pm_message){ .event = PM_EVENT_RESTORE, }) +#define PMSG_RECOVER ((struct pm_message){ .event = PM_EVENT_RECOVER, }) +#define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, }) + +/** +* Device power management states +* +* These state labels are used internally by the PM core to indicate the current +* status of a device with respect to the PM core operations. +* +* DPM_ON Device is regarded as operational. Set this way +* initially and when ->resume(), ->thaw(), ->restore(), +* ->recover(), or ->complete() is about to be called. +* Also set when ->prepare() fails. +* +* DPM_SUSPENDING Device is currently being suspended. Set when +* ->prepare() is about to be called. +* +* DPM_OFF Device is regarded as suspended. Set when ->suspend(), +* ->freeze(), ->poweroff(), or ->quiesce() is about to be +* called. +*/ + +enum dpm_state { +DPM_ON , +DPM_SUSPENDING , +DPM_OFF , +}; + +struct dev_pm_info { +pm_message_t power_state; +unsigned can_wakeup:1 ; +unsigned should_wakeup:1 ; +enum dpm_state status:2 ;/* Owned by the PM core */ +#ifdef CONFIG_PM_SLEEP +struct list_head entry; +#endif +}; + +/* +* The PM_EVENT_ messages are also used by drivers implementing the legacy +* suspend framework, based on the ->suspend() and ->resume() callbacks common +* for suspend and hibernation transitions, according to the rules below. +*/ + +/* Necessary, because several drivers use PM_EVENT_PRETHAW */ +#define PM_EVENT_PRETHAW PM_EVENT_QUIESCE + +/* * One transition is triggered by resume(), after a suspend() call; the * message is implicit: *
@@ -166,35 +375,11 @@ typedef struct pm_message {* or from system low-power states such as standby or suspend-to-RAM. */ -#define PM_EVENT_ON 0 -#define PM_EVENT_FREEZE 1 -#define PM_EVENT_SUSPEND 2 -#define PM_EVENT_HIBERNATE 4 -#define PM_EVENT_PRETHAW 8 - -#define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE) - -#define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, }) -#define PMSG_PRETHAW ((struct pm_message){ .event = PM_EVENT_PRETHAW, }) -#define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, }) -#define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, }) -#define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, }) - -struct dev_pm_info { -pm_message_t power_state; -unsigned can_wakeup:1 ; -unsigned should_wakeup:1 ; -bool sleeping:1 ;/* Owned by the PM core */ -#ifdef CONFIG_PM_SLEEP -struct list_head entry; -#endif -}; +#ifdef CONFIG_PM_SLEEP +extern void device_power_up (pm_message_t state); +extern void device_resume (pm_message_t state);extern int device_power_down (pm_message_t state); -extern void device_power_up (void ); -extern void device_resume (void ); - -#ifdef CONFIG_PM_SLEEP extern int device_suspend (pm_message_t state);extern int device_prepare_suspend (pm_message_t state);
drivers/base/power/main.c +351 -102
635 lines not on this page · raw message
include/linux/device.h +8 -0
@@ -69,6 +69,9 @@ struct bus_type {int (*resume_early)(struct device *dev);int (*resume)(struct device *dev); +struct pm_ops *pm; +struct pm_ops *pm_noirq; +struct bus_type_private *p; };
@@ -202,6 +205,8 @@ struct class {int (*suspend)(struct device *dev,pm_message_t state);int (*resume)(struct device *dev); + +struct pm_ops *pm; };extern int __must_check class_register (struct class *class);
@@ -345,8 +350,11 @@ struct device_type {struct attribute_group **groups;int (*uevent)(struct device *dev,struct kobj_uevent_env *env);void (*release)(struct device *dev); +int (*suspend)(struct device *dev,pm_message_t state);int (*resume)(struct device *dev); + +struct pm_ops *pm; };/* interface for exporting device attributes */
kernel/power/disk.c +7 -7
@@ -224,7 +224,7 @@ static int create_image(int platform_mod/* NOTE: device_power_up() is just a resume() for devices * that suspended with irqs off ... no overall powerup. */ -device_power_up (); +device_power_up (in_suspend ?PMSG_RECOVER :PMSG_RESTORE ); Enable_irqs:local_irq_enable ();return error;
@@ -280,7 +280,7 @@ int hibernation_snapshot(int platform_mo Finish:platform_finish (platform_mode); Resume_devices: -device_resume (); +device_resume (in_suspend ?PMSG_RECOVER :PMSG_RESTORE ); Resume_console:resume_console (); Close:
@@ -301,7 +301,7 @@ static int resume_target_kernel(void)int error;local_irq_disable (); - error =device_power_down (PMSG_PRETHAW ); + error =device_power_down (PMSG_QUIESCE );if (error) {printk (KERN_ERR "PM: Some devices failed to power down, " "aborting resume\n" );
@@ -329,7 +329,7 @@ static int resume_target_kernel(void)swsusp_free ();restore_processor_state ();touch_softlockup_watchdog (); -device_power_up (); +device_power_up (PMSG_THAW ); Enable_irqs:local_irq_enable ();return error;
@@ -350,7 +350,7 @@ int hibernation_restore(int platform_modpm_prepare_console ();suspend_console (); - error =device_suspend (PMSG_PRETHAW ); + error =device_suspend (PMSG_QUIESCE );if (error) goto Finish;
@@ -362,7 +362,7 @@ int hibernation_restore(int platform_modenable_nonboot_cpus (); }platform_restore_cleanup (platform_mode); -device_resume (); +device_resume (PMSG_RECOVER ); Finish:resume_console ();pm_restore_console ();
@@ -419,7 +419,7 @@ int hibernation_platform_enter(void) Finish: hibernation_ops->finish (); Resume_devices: -device_resume (); +device_resume (PMSG_RESTORE ); Resume_console:resume_console (); Close:
kernel/power/main.c +2 -2
@@ -239,7 +239,7 @@ static int suspend_enter(suspend_state_tif (!suspend_test (TEST_CORE )) error = suspend_ops->enter (state); -device_power_up (); +device_power_up (PMSG_RESUME ); Done:arch_suspend_enable_irqs ();BUG_ON (irqs_disabled ());
@@ -291,7 +291,7 @@ int suspend_devices_and_enter(suspend_stif (suspend_ops->finish) suspend_ops->finish (); Resume_devices: -device_resume (); +device_resume (PMSG_RESUME ); Resume_console:resume_console (); Close:
arch/x86/kernel/apm_32.c +2 -2
@@ -1208,9 +1208,9 @@ static int suspend(int vetoable)if (err !=APM_SUCCESS )apm_error ("suspend" , err); err = (err ==APM_SUCCESS ) ?0 : -EIO ; -device_power_up (); +device_power_up (PMSG_RESUME );local_irq_enable (); -device_resume (); +device_resume (PMSG_RESUME );queue_event (APM_NORMAL_RESUME ,NULL ); out:spin_lock (&user_list_lock);
RFC PATCH 3/3 PM: New suspend and hibernation callbacks for PCI bus type
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)
drivers/pci/pci-driver.c +451 -46
@@ -271,45 +271,78 @@ static int pci_device_remove(struct devireturn 0 ; } -static int pci_device_suspend (struct device * dev,pm_message_t state) +static void pci_device_shutdown (struct device *dev) { -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver; -int i =0 ; - -if (drv && drv->suspend) { - i = drv->suspend (pci_dev, state); -suspend_report_result (drv->suspend, i); - }else { -pci_save_state (pci_dev); -/* -* mark its power state as "unknown", since we don't know if -* e.g. the BIOS will change its device state when we suspend. -*/ -if (pci_dev->current_state ==PCI_D0 ) - pci_dev->current_state =PCI_UNKNOWN ; - } -return i; +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->shutdown) + drv->shutdown (pci_dev); } -static int pci_device_suspend_late (struct device * dev,pm_message_t state) +#ifdef CONFIG_PM_SLEEP +static int pci_pm_prepare (struct device *dev) { -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver; -int i =0 ; +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; -if (drv && drv->suspend_late) { - i = drv->suspend_late (pci_dev, state); -suspend_report_result (drv->suspend_late, i); - } -return i; +if (drv && drv->pm && drv->pm->prepare) + error = drv->pm->prepare (dev); + +return error; +} + +static int pci_pm_prepare_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (drv && drv->pm_noirq && drv->pm_noirq->prepare) + error = drv->pm_noirq->prepare (dev); + +return error; +} + +static void pci_pm_complete (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->pm && drv->pm->complete) + drv->pm->complete (dev); +} + +static void pci_pm_complete_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->pm_noirq && drv->pm_noirq->complete) + drv->pm_noirq->complete (dev); }/* -* Default resume method for devices that have no driver provided resume, +* Default "suspend" method for devices that have no driver provided suspend, * or not even a driver at all. */ -static int pci_default_resume (struct pci_dev *pci_dev) +static void pci_default_pm_suspend (struct pci_dev *pci_dev) +{ +pci_save_state (pci_dev); +/* +* mark its power state as "unknown", since we don't know if +* e.g. the BIOS will change its device state when we suspend. +*/ +if (pci_dev->current_state ==PCI_D0 ) + pci_dev->current_state =PCI_UNKNOWN ; +} + +/* +* Default "resume" method for devices that have no driver provided resume, +* or not even a driver at all. +*/ +static int pci_default_pm_resume (struct pci_dev *pci_dev) {int retval =0 ;
@@ -324,40 +357,376 @@ static int pci_default_resume(struct pcireturn retval; } -static int pci_device_resume (struct device * dev) +#ifdef CONFIG_SUSPEND +static int pci_pm_suspend (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) { +pci_default_pm_suspend (pci_dev); +return 0 ; + } + +if (drv->pm) { +if (drv->pm->suspend) { + error = drv->pm->suspend (dev); +suspend_report_result (drv->pm->suspend, error); + }else { +pci_default_pm_suspend (pci_dev); + } + }else { +/* Legacy mechanism */ +if (drv->suspend) { + error = drv->suspend (pci_dev,PMSG_SUSPEND ); +suspend_report_result (drv->suspend, error); + }else { +pci_default_pm_suspend (pci_dev); + } + } + +return error; +} + +static int pci_pm_suspend_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->suspend) { + error = drv->pm_noirq->suspend (dev); +suspend_report_result (drv->pm_noirq->suspend, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_SUSPEND ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_resume (struct device *dev) { +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver;int error; -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver; -if (drv && drv->resume) +if (!drv) +return pci_default_pm_resume (pci_dev); + +if (drv->pm) { + error = drv->pm->resume ? + drv->pm->resume (dev) :pci_default_pm_resume (pci_dev); + }else { +/* Legacy mechanism */ + error = drv->resume ? + drv->resume (pci_dev) :pci_default_pm_resume (pci_dev); + } + +return error; +} + +static int pci_pm_resume_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +pci_fixup_device (pci_fixup_resume, pci_dev); + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->resume) + error = drv->pm_noirq->resume (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ + error = drv->resume_early (pci_dev); + } + +return error; +} +#endif /* CONFIG_SUSPEND */ + +#ifdef CONFIG_HIBERNATION +static int pci_pm_freeze (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) { +pci_default_pm_suspend (pci_dev); +return 0 ; + } + +if (drv->pm) { +if (drv->pm->freeze) { + error = drv->pm->freeze (dev); +suspend_report_result (drv->pm->freeze, error); + }else { +pci_default_pm_suspend (pci_dev); + } + }else { +/* Legacy mechanism */ +if (drv->suspend) { + error = drv->suspend (pci_dev,PMSG_FREEZE ); +suspend_report_result (drv->suspend, error); + }else { +pci_default_pm_suspend (pci_dev); + } + } + +return error; +} + +static int pci_pm_freeze_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->freeze) { + error = drv->pm_noirq->freeze (dev); +suspend_report_result (drv->pm_noirq->freeze, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_FREEZE ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_thaw (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->thaw) + error = drv->pm->thaw (dev); + }else if (drv->resume) { +/* Legacy mechanism */ error = drv->resume (pci_dev); -else - error =pci_default_resume (pci_dev); + } + +return error; +} + +static int pci_pm_thaw_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->thaw) + error = drv->pm_noirq->thaw (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ + error = drv->resume_early (pci_dev); + } + +return error; +} + +static int pci_pm_poweroff (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->poweroff) { + error = drv->pm->poweroff (dev); +suspend_report_result (drv->pm->poweroff, error); + } + }else if (drv->suspend) { +/* Legacy mechanism */ + error = drv->suspend (pci_dev,PMSG_HIBERNATE ); +suspend_report_result (drv->suspend, error); + } + +return error; +} + +static int pci_pm_poweroff_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->poweroff) { + error = drv->pm_noirq->poweroff (dev); +suspend_report_result (drv->pm_noirq->poweroff, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_HIBERNATE ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_quiesce (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->quiesce) { + error = drv->pm->quiesce (dev); +suspend_report_result (drv->pm->quiesce, error); + } + }else if (drv->suspend) { +/* Legacy mechanism */ + error = drv->suspend (pci_dev,PMSG_QUIESCE ); +suspend_report_result (drv->suspend, error); + } + +return error; +} + +static int pci_pm_quiesce_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->quiesce) { + error = drv->pm_noirq->quiesce (dev); +suspend_report_result (drv->pm_noirq->quiesce, error); + } + }else if (drv->suspend_late) { +/* Legacy mechanism */ + error = drv->suspend_late (pci_dev,PMSG_QUIESCE ); +suspend_report_result (drv->suspend_late, error); + } + +return error; +} + +static int pci_pm_restore (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error; + +if (!drv) +return pci_default_pm_resume (pci_dev); + +if (drv->pm) { + error = drv->pm->restore ? + drv->pm->restore (dev) :pci_default_pm_resume (pci_dev); + }else { +/* Legacy mechanism */ + error = drv->resume ? + drv->resume (pci_dev) :pci_default_pm_resume (pci_dev); + } +return error; } -static int pci_device_resume_early (struct device * dev) +static int pci_pm_restore_noirq (struct device *dev) { +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver;int error =0 ; -struct pci_dev * pci_dev =to_pci_dev (dev); -struct pci_driver * drv = pci_dev->driver;pci_fixup_device (pci_fixup_resume, pci_dev); -if (drv && drv->resume_early) +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->restore) + error = drv->pm_noirq->restore (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ error = drv->resume_early (pci_dev); + } +return error; } -static void pci_device_shutdown (struct device *dev) +static int pci_pm_recover (struct device *dev) {struct pci_dev *pci_dev =to_pci_dev (dev);struct pci_driver *drv = pci_dev->driver; +int error =0 ; -if (drv && drv->shutdown) - drv->shutdown (pci_dev); +if (!drv) +return 0 ; + +if (drv->pm) { +if (drv->pm->recover) + error = drv->pm->recover (dev); + }else if (drv->resume) { +/* Legacy mechanism */ + error = drv->resume (pci_dev); + } + +return error; +} + +static int pci_pm_recover_noirq (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; +int error =0 ; + +if (!drv) +return 0 ; + +if (drv->pm_noirq) { +if (drv->pm_noirq->recover) + error = drv->pm_noirq->recover (dev); + }else if (drv->resume_early) { +/* Legacy mechanism */ + error = drv->resume_early (pci_dev); + } + +return error; } +#endif /* CONFIG_HIBERNATION */ +#endif /* CONFIG_PM_SLEEP */ /** * __pci_register_driver - register a new pci driver
@@ -500,18 +869,54 @@ int pci_uevent(struct device *dev, struc }#endif +struct pm_ops pci_pm_ops = { +#ifdef CONFIG_PM_SLEEP + .prepare = pci_pm_prepare, + .complete = pci_pm_complete, +#endif +#ifdef CONFIG_SUSPEND + .suspend = pci_pm_suspend, + .resume = pci_pm_resume, +#endif +#ifdef CONFIG_HIBERNATION + .freeze = pci_pm_freeze, + .thaw = pci_pm_thaw, + .poweroff = pci_pm_poweroff, + .quiesce = pci_pm_quiesce, + .restore = pci_pm_restore, + .recover = pci_pm_recover, +#endif +}; + +struct pm_ops pci_pm_ops_noirq = { +#ifdef CONFIG_PM_SLEEP + .prepare = pci_pm_prepare_noirq, + .complete = pci_pm_complete_noirq, +#endif +#ifdef CONFIG_SUSPEND + .suspend = pci_pm_suspend_noirq, + .resume = pci_pm_resume_noirq, +#endif +#ifdef CONFIG_HIBERNATION + .freeze = pci_pm_freeze_noirq, + .thaw = pci_pm_thaw_noirq, + .poweroff = pci_pm_poweroff_noirq, + .quiesce = pci_pm_quiesce_noirq, + .restore = pci_pm_restore_noirq, + .recover = pci_pm_recover_noirq, +#endif +}; +struct bus_type pci_bus_type = { .name ="pci" , .match = pci_bus_match, .uevent = pci_uevent, .probe = pci_device_probe, .remove = pci_device_remove, - .suspend = pci_device_suspend, - .suspend_late = pci_device_suspend_late, - .resume_early = pci_device_resume_early, - .resume = pci_device_resume, .shutdown = pci_device_shutdown, .dev_attrs = pci_dev_attrs, + .pm = &pci_pm_ops, + .pm_noirq = &pci_pm_ops_noirq, };static int __init pci_driver_init (void )
include/linux/pci.h +2 -0
@@ -382,6 +382,8 @@ struct pci_driver {int (*resume) (struct pci_dev *dev);/* Device woken up */ void (*shutdown) (struct pci_dev *dev); +struct pm_ops *pm; +struct pm_ops *pm_noirq;struct pci_error_handlers *err_handler;struct device_driver driver;struct pci_dynids dynids;
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Rafael J. WysockiHi!
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Quoted 12 lines
+ * The externally visible transitions are handled with the help of the following
+ * callbacks included in this structure:
+ *
+ * @prepare: Prepare the device for the upcoming transition, but do NOT change
+ * its hardware state. Prevent new children of the device from being
+ * registered and prevent new calls to the probe method from being made
+ * after @prepare() returns. Also, if a concurrent child registration
+ * or a call to the probe method already in progress is detected by
+ * @prepare(), it may return-EAGAIN, so that the PM core can execute it
+ * once again (e.g. after suspending the newly registered child) to recover
+ * from the race condition. This method is executed for all kinds of
+ * suspend transitions and is immediately followed by one of the suspend
Quoted 8 lines
+ * callbacks: @suspend(), @freeze(), @poweroff(), or @quiesce().
+ *
+ * @complete: Undo the changes made by @prepare(). This method is executed for
+ * all kinds of resume transitions, immediately following one of the resume
+ * callbacks: @resume(), @thaw(), @restore(), or @recover(). Also executed
+ * if a suspend callback (@suspend(), @freeze(), @poweroff(), @quiesce())
+ * immediately following a successful @prepare() fails OR if a new child
+ * of the device has been registered during @prepare().
So... we do prepare() but it detects new child, so it returns -EAGAIN.
so we call complete() based on description above
...and then we call prepare() to suspend again?
Quoted 2 lines
+ * @suspend: Executed before putting the system into a sleep state in which the
+ * contents of main memory are preserved. Quiesce the device, put it into
content....is?
Quoted 2 lines
+ * @resume: Executed after waking the system up from a sleep state in which the
+ * contents of main memory were preserved. Put the device into the
content...is?
Quoted 4 lines
+ * @freeze: Hibernation-specific, executed before creating a hibernation image.
+ * Quiesce operations so that a consistent image can be created, but do NOT
+ * otherwise put the device into a low power device state and do NOT emit
+ * system wakeup events. Save in main memory the device settings to be
"Save device settings (used by @restore()) into main memory"?
Quoted 3 lines
+ * @quiesce: Hibernation-specific, executed after loading a hibernation image
+ * and before restoring the contents of main memory from it. Quiesce
+ * operations so that the contents of main memory can be restored from the
operations, so ?
Pavel
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Rafael J. WysockiHi!
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Quoted 12 lines
+ * The externally visible transitions are handled with the help of the following
+ * callbacks included in this structure:
+ *
+ * @prepare: Prepare the device for the upcoming transition, but do NOT change
+ * its hardware state. Prevent new children of the device from being
+ * registered and prevent new calls to the probe method from being made
+ * after @prepare() returns. Also, if a concurrent child registration
+ * or a call to the probe method already in progress is detected by
+ * @prepare(), it may return-EAGAIN, so that the PM core can execute it
+ * once again (e.g. after suspending the newly registered child) to recover
+ * from the race condition. This method is executed for all kinds of
+ * suspend transitions and is immediately followed by one of the suspend
Quoted 8 lines
+ * callbacks: @suspend(), @freeze(), @poweroff(), or @quiesce().
+ *
+ * @complete: Undo the changes made by @prepare(). This method is executed for
+ * all kinds of resume transitions, immediately following one of the resume
+ * callbacks: @resume(), @thaw(), @restore(), or @recover(). Also executed
+ * if a suspend callback (@suspend(), @freeze(), @poweroff(), @quiesce())
+ * immediately following a successful @prepare() fails OR if a new child
+ * of the device has been registered during @prepare().
So... we do prepare() but it detects new child, so it returns -EAGAIN.
so we call complete() based on description above
...and then we call prepare() to suspend again?
Quoted 2 lines
+ * @suspend: Executed before putting the system into a sleep state in which the
+ * contents of main memory are preserved. Quiesce the device, put it into
content....is?
Quoted 2 lines
+ * @resume: Executed after waking the system up from a sleep state in which the
+ * contents of main memory were preserved. Put the device into the
content...is?
Quoted 4 lines
+ * @freeze: Hibernation-specific, executed before creating a hibernation image.
+ * Quiesce operations so that a consistent image can be created, but do NOT
+ * otherwise put the device into a low power device state and do NOT emit
+ * system wakeup events. Save in main memory the device settings to be
"Save device settings (used by @restore()) into main memory"?
Quoted 3 lines
+ * @quiesce: Hibernation-specific, executed after loading a hibernation image
+ * and before restoring the contents of main memory from it. Quiesce
+ * operations so that the contents of main memory can be restored from the
operations, so ?
Pavel
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 2/3] PM: New suspend and hibernation callbacks for platform bus type
In reply to Rafael J. WysockiQuoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the platform bus type.
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK... but... this is getting quite big:
Quoted 2 lines
drivers/base/platform.c | 444 ++++++++++++++++++++++++++++++++++++++-- include/linux/platform_device.h | 2
...I hope cleanups in drivers are worth it...
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 3/3] PM: New suspend and hibernation callbacks for PCI bus type
In reply to Rafael J. WysockiQuoted 3 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
"bus", I guess.
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Quoted 4 lines
--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)
...but big, too :-(.
Pavel
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 3/3] PM: New suspend and hibernation callbacks for PCI bus type
In reply to Rafael J. WysockiQuoted 3 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
"bus", I guess.
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Quoted 4 lines
--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)
...but big, too :-(.
Pavel
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 2/3] PM: New suspend and hibernation callbacks for platform bus type
In reply to Rafael J. WysockiQuoted 6 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the platform bus type.
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK... but... this is getting quite big:
Quoted 2 lines
drivers/base/platform.c | 444 ++++++++++++++++++++++++++++++++++++++-- include/linux/platform_device.h | 2
...I hope cleanups in drivers are worth it...
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Pavel MachekQuoted 6 lines · Pavel Machek
Quoted 6 lines
+ * @complete: Undo the changes made by @prepare(). This method is executed for
+ * all kinds of resume transitions, immediately following one of the resume
+ * callbacks: @resume(), @thaw(), @restore(), or @recover(). Also executed
+ * if a suspend callback (@suspend(), @freeze(), @poweroff(), @quiesce())
+ * immediately following a successful @prepare() fails OR if a new child
+ * of the device has been registered during @prepare().
"during a successful call to @prepare()"?
Quoted 3 lines
So... we do
prepare()but it detects new child, so it returns-EAGAIN.
so we callcomplete()based on description above
...and then we callprepare()to suspend again?
You misunderstood (maybe the comment needs to be clarified as above).
If prepare() returns any error (including -EAGAIN) then complete() does not get called. If prepare() returns successfully but the PM core detects that a new child was added while prepare() was running, then we call complete(), suspend the child, and call prepare() again.
Quoted 4 lines
Quoted 2 lines
+ * @suspend: Executed before putting the system into a sleep state in which the
+ * contents of main memory are preserved. Quiesce the device, put it intocontent....is?
It's okay to use "contents" -- analogous to the table of contents in a book. It's one of those weird corner cases where either alternative is acceptable.
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
Hi!
Quoted 5 lines
Quoted 3 lines
So... we do
prepare()but it detects new child, so it returns-EAGAIN.
so we callcomplete()based on description above
...and then we callprepare()to suspend again?You misunderstood (maybe the comment needs to be clarified as above).
Yes, please :-).
Quoted 4 lines
If
prepare()returns any error (including-EAGAIN) thencomplete()does not get called. Ifprepare()returns successfully but the PM core detects that a new child was added whileprepare()was running, then we callcomplete(), suspend the child, and callprepare()again.
Ok.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
+ * @suspend: Executed before putting the system into a sleep state in which the
+ * contents of main memory are preserved. Quiesce the device, put it intocontent....is?
It's okay to use "contents" -- analogous to the table of contents in a book. It's one of those weird corner cases where either alternative is acceptable.
Ok -- I guess I should get that english course ;-).
Pavel
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Rafael J. WysockiQuoted 5 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Introduce 'struct
pm_ops' representing a set of suspend and hibernation operations for bus types, device classes and device types.
Ok, I must have missed the thread describing why we need to do this, so, why do we need to do this? What is this going to buy us in the end after everything is changed?
Quoted 7 lines
include/linux/pm.h +7
-0+struct pm_ops { +#ifdef CONFIG_PM_SLEEP +int (*prepare)(struct device *dev); +void (*complete)(struct device *dev); +#endif +#ifdef CONFIG_SUSPEND +int (*suspend)(struct device *dev);
include/linux/pm.h typedef struct pm_message { · line 227
Quoted 10 lines
include/linux/pm.h +10
-0+int (*resume)(struct device *dev); +#endif +#ifdef CONFIG_HIBERNATION +int (*freeze)(struct device *dev); +int (*thaw)(struct device *dev); +int (*poweroff)(struct device *dev); +int (*quiesce)(struct device *dev); +int (*restore)(struct device *dev); +int (*recover)(struct device *dev); +#endif
Don't ifdef stuff like this, it only causes ifdefs to be needed to the .c code as well for all places these structures are defined in drivers/busses, right?
thanks,
greg k-h
Re: [RFC][PATCH 3/3] PM: New suspend and hibernation callbacks for PCI bus type
In reply to Rafael J. WysockiQuoted 9 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)
Hm, that's a lot of code added. What additional functionality did we just get?
Are we going to have to create a noirq version for every driver and bus type now?
thanks,
greg k-h
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Pavel MachekQuoted 1 line · Pavel Machek
Hi!
Hi,
Quoted 18 lines
Quoted 5 lines
Quoted 3 lines
So... we do
prepare()but it detects new child, so it returns-EAGAIN.
so we callcomplete()based on description above
...and then we callprepare()to suspend again?You misunderstood (maybe the comment needs to be clarified as above).
Yes, please :-).
Quoted 4 lines
If
prepare()returns any error (including-EAGAIN) thencomplete()does not get called. Ifprepare()returns successfully but the PM core detects that a new child was added whileprepare()was running, then we callcomplete(), suspend the child, and callprepare()again.Ok.
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
+ * @suspend: Executed before putting the system into a sleep state in which the
+ * contents of main memory are preserved. Quiesce the device, put it into
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
content....is?
It's okay to use "contents" -- analogous to the table of contents in a book. It's one of those weird corner cases where either alternative is acceptable.
Ok -- I guess I should get that english course ;-).
Thanks for the comments.
In the meantime I had an IRC chat with Ben, who wanted a couple of quite substantial changes to be made to this patch.
First, Ben thinks that ->prepare() should be called in a separate loop for all devices, before any of them is suspended, so that drivers can assume the availability of the other devices during ->prepare() (for example, so that they can use GFP_KERNEL memory allocations). Accordingly, ->complete() would be called in a separate loop after calling ->resume() for all devices.
Next, he wants the number of noirq callbacks to be reduced.
Both of the above things make sense, so I'll rework the patch (or maybe even all three patches) to implement them and we'll see how they will look like.
Thanks,
Rafael
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
Quoted 8 lines · Rafael J. Wysocki
In the meantime I had an IRC chat with Ben, who wanted a couple of quite substantial changes to be made to this patch.
First, Ben thinks that ->prepare() should be called in a separate loop for all devices, before any of them is suspended, so that drivers can assume the availability of the other devices during ->prepare() (for example, so that they can use
GFP_KERNELmemory allocations). Accordingly, ->complete() would be called in a separate loop after calling ->resume() for all devices.
In order to make this work, you would have to prevent new children from being registered starting from the time just after prepare() returns, rather than from the time just before suspend() is called. Nothing is wrong with that, but it requires a redesign of the new flags. (It's worth mentioning that drivers will want to have a flag that gets set just _before_ prepare() -- just _after_ is too late.)
In the other direction, it ought to be okay to allow new children to be registered during resume(). There's no need to wait for complete().
Quoted 1 line
Next, he wants the number of noirq callbacks to be reduced.
I agree. For example, there isn't much point in having prepare() and complete() entries for noirq.
Quoted 2 lines
Both of the above things make sense, so I'll rework the patch (or maybe even all three patches) to implement them and we'll see how they will look like.
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
Hi Alan !
Quoted 6 lines · Alan Stern
In order to make this work, you would have to prevent new children from being registered starting from the time just after
prepare()returns, rather than from the time just beforesuspend()is called. Nothing is wrong with that, but it requires a redesign of the new flags. (It's worth mentioning that drivers will want to have a flag that gets set just _before_prepare()-- just _after_ is too late.)
I think that's the most logical semantic to expose to drivers. We've been needing prepare() for a lot of things in the past.
The main one is that you know user space is still up & running (not only it's not been frozen, but even in the absence of a freezer, you know storage hasn't been stopped yet, etc...). So if your driver has tight interaction with userspace that requires clean handling of suspend, it can be done at that point (X with DRI is an example that could make good use of that).
The whole GFP_KERNEL allocation is another, though we could make GFP_KERNEL become silently GFP_NOIO when entering the suspend loop, it's still useful for the driver to know that we are -about- to start a suspend operation, if any significant storage is needed (for backup for example, or if it needs to cache a firmware to ensure proper resume), it's the right time to do it.
Now, about the issue vs. device insertion, I believe it's solvable unless I missed something:
So it's the responsibility of bus drivers such as khubd to stop inserting new devices in the tree from prepare(). There we agree. The problem is about -enforcing- it in the core right ? That is, making device addition actually fail if a bogus bus driver tries to insert a device at the wrong time.
This does indeed have an issue of when precisely do we stick that flag (that tells the core to fail) since before calling the bus driver prepare() is too early and after is potentially too late.
Now, I think this is not really a problem if prepare() calls the drivers from top to bottom: In that case, we would set the flag after the bus driver returns from prepare(). That means that there is a small window where a bogus bus driver can still insert a new child, but I think that isn't an issue, specifically because we walk from parent to child:
If the sequence is:
1- prepare() returns
2- we set the "no new addition" flag
3- we start walking children
You see that the only place the where the "bad" bus driver can still attempt to insert without failing is between 1 and 2. However, that happens before we start prepare()'ing the children. So that means that anything that has successfully been added -will- have it's prepare() called, and thus becomes a normal part of the suspend/resume process. Any attempt at adding after 2 would fail, thus we are sure the children list is sane here (won't get new things right in between. Removal might still happen I suppose, we'll have to look into this closely).
Quoted 2 lines
In the other direction, it ought to be okay to allow new children to be registered during
resume(). There's no need to wait forcomplete().
There are pros and cons here. A lot of drivers may rely on request_firmware() and such, and will possibly deadlock or timeout if called during resume before storage is available (i've seen that happening with today scheme). It might be better to be safe here, but I agree, it's generally less of a concern.
Quoted 4 lines
Quoted 1 line
Next, he wants the number of noirq callbacks to be reduced.
I agree. For example, there isn't much point in having
prepare()andcomplete()entries for noirq.
Exactly. I think we really only need noirq variants of suspend and resume, and possibly freeze and thaw (I'm not 100% of the later as I haven't studied the problem of hibernate closely, but I can see a usage scenario that might make sense).
Quoted 2 lines
Quoted 2 lines
Both of the above things make sense, so I'll rework the patch (or maybe even all three patches) to implement them and we'll see how they will look like.
Cheers,
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Greg KHQuoted 10 lines · Greg KH
Quoted 5 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Introduce 'struct
pm_ops' representing a set of suspend and hibernation operations for bus types, device classes and device types.Ok, I must have missed the thread describing why we need to do this, so, why do we need to do this? What is this going to buy us in the end after everything is changed?
That was rather long thread where Linus flamed us for having everything in one "suspend" callback, selecting what to do by pm_message_t parameter. It was kind of hard to miss ;-).
Pavel
Quoted 22 lines
Quoted 7 lines
include/linux/pm.h +7
-0+struct pm_ops { +#ifdef CONFIG_PM_SLEEP +int (*prepare)(struct device *dev); +void (*complete)(struct device *dev); +#endif +#ifdef CONFIG_SUSPEND +int (*suspend)(struct device *dev);include/linux/pm.h typedef struct pm_message { · line 227
Quoted 10 lines
include/linux/pm.h +10
-0+int (*resume)(struct device *dev); +#endif +#ifdef CONFIG_HIBERNATION +int (*freeze)(struct device *dev); +int (*thaw)(struct device *dev); +int (*poweroff)(struct device *dev); +int (*quiesce)(struct device *dev); +int (*restore)(struct device *dev); +int (*recover)(struct device *dev); +#endif Don't ifdef stuff like this, it only causes ifdefs to be needed to the .c code as well for all places these structures are defined in
drivers/busses, right?
Signature · 3 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html pomozte zachranit klanovicky les: http://www.ujezdskystrom.info/
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Greg KHQuoted 10 lines · Greg KH
Quoted 5 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Introduce 'struct
pm_ops' representing a set of suspend and hibernation operations for bus types, device classes and device types.Ok, I must have missed the thread describing why we need to do this, so, why do we need to do this? What is this going to buy us in the end after everything is changed?
There were many threads related to that.
To summarize, the first purpose is to separate suspend (aka s2ram and standby) callbacks from hibernation callbacks in such a way that the new callbacks won't take arguments and the purpose of each of them will be clearly specified. This has been requested multiple times by many people, including Linus himself, and the reason is that within the current scheme if ->resume() is called, for example, it's difficult to say why it's been called (ie. is it a resume from RAM or from hibernation or a suspend/hibernation failure etc.?).
The second purpose is to make the suspend/hibernation callbacks more flexible so that device drivers can handle more than they can within the current scheme. For example, some drivers may need to prevent new children of the device from being registered before their ->suspend() callbacks are executed or they may want to carry out some operations requiring the availability of some other devices, not directly bound via the parent-child relationship, in order to prepare for the execution of ->suspend(), etc.
Ultimately, we'd like to stop using the freezing of tasks for suspend and therefore the drivers' suspend/hibernation code will have to take care of the handling of the user space during suspend/hibernation which would be difficult within the current scheme, without the ->prepare() and ->complete() callbacks.
Quoted 14 lines
Quoted 14 lines
include/linux/pm.h +14
-0+struct pm_ops { +#ifdef CONFIG_PM_SLEEP +int (*prepare)(struct device *dev); +void (*complete)(struct device *dev); +#endif +#ifdef CONFIG_SUSPEND +int (*suspend)(struct device *dev); +int (*resume)(struct device *dev); +#endif +#ifdef CONFIG_HIBERNATION +int (*freeze)(struct device *dev); +int (*thaw)(struct device *dev); +int (*poweroff)(struct device *dev); +int (*quiesce)(struct device *dev);
Quoted 8 lines
include/linux/pm.h typedef struct pm_message { · line 227
Quoted 3 lines
include/linux/pm.h +3
-0+int (*restore)(struct device *dev); +int (*recover)(struct device *dev); +#endif Don't ifdef stuff like this, it only causes ifdefs to be needed to the .c code as well for all places these structures are defined in
drivers/busses, right?
Sure, but that code won't be necessary otherwise too.
I'll remove the #ifdefs in the next version of the patch.
Thanks,
Rafael
Re: [RFC][PATCH 3/3] PM: New suspend and hibernation callbacks for PCI bus type
In reply to Greg KHQuoted 5 lines · Greg KH
Quoted 4 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)Hm, that's a lot of code added. What additional functionality did we just get?
The possibility to handle suspend/hibernation in a more flexible way, provided that the drivers define the new callbacks.
Quoted 2 lines
Are we going to have to create a noirq version for every driver and bus type now?
No, we aren't. The PCI and platform bus types already have the noirq callbacks implemented (they are called _late and _early, but that's the same essentially).
Thanks,
Rafael
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Benjamin HerrenschmidtQuoted 1 line · Benjamin Herrenschmidt
Hi Alan !
Hi!
Quoted 11 lines · Alan Stern
Now, about the issue vs. device insertion, I believe it's solvable unless I missed something:
So it's the responsibility of bus drivers such as khubd to stop inserting new devices in the tree from
prepare(). There we agree. The problem is about -enforcing- it in the core right ? That is, making device addition actually fail if a bogus bus driver tries to insert a device at the wrong time.
Quoted 6 lines
This does indeed have an issue of when precisely do we stick that flag (that tells the core to fail) since before calling the bus driver
prepare()is too early and after is potentially too late.Now, I think this is not really a problem if
prepare()calls the drivers from top to bottom:
That was in fact the original plan, before Rafael changed over to calling prepare() just prior to suspend().
Quoted 11 lines
In that case, we would set the flag after the bus
driver returns fromprepare(). That means that there is a small window where a bogus bus driver can still insert a new child, but I think that isn't an issue, specifically because we walk from parent to child:If the sequence is:
1-
prepare()returns
2- we set the "no new addition" flag
3- we start walking children
Quoted 8 lines
You see that the only place the where the "bad" bus driver can still attempt to insert without failing is between 1 and 2. However, that happens before we start prepare()'ing the children. So that means that anything that has successfully been added -will- have it's
prepare()called, and thus becomes a normal part of the suspend/resume process. Any attempt at adding after 2 would fail, thus we are sure the children list is sane here (won't get new things right in between. Removal might still happen I suppose, we'll have to look into this closely).
Your analysis is right. Removal isn't a problem; we don't care if drivers are prohibited from registering new children when they are unbound. In general we want to make it possible to unregister devices at any time during the suspend-resume procedure (except of course that a device can't be unregistered while one of its methods is running).
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_ prepare() is called.
This means there would have to be _two_ flags: one set before calling prepare() and one set afterward. Only the second would be checked by the PM core when deciding whether to allow new child registrations.
Quoted 8 lines
Quoted 2 lines
In the other direction, it ought to be okay to allow new children to be registered during
resume(). There's no need to wait forcomplete().There are pros and cons here. A lot of drivers may rely on
request_firmware()and such, and will possibly deadlock or timeout if called during resume before storage is available (i've seen that happening with today scheme). It might be better to be safe here, but I agree, it's generally less of a concern.
That's up to the individual driver. If it wants to wait until its complete method runs, then fine. But if it doesn't want to wait, the PM core shouldn't force new registrations to fail.
Alan Stern
Re: [RFC][PATCH 3/3] PM: New suspend and hibernation callbacks for PCI bus type
In reply to Rafael J. WysockiQuoted 12 lines · Rafael J. Wysocki
Quoted 11 lines · Greg KH
Quoted 9 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Implement new suspend and hibernation callbacks for the PCI busi type.
Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/pci/pci-driver.c | 497 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/pci.h | 2 2 files changed, 453 insertions(+), 46 deletions(-)
Quoted 5 lines
Quoted 2 lines
Hm, that's a lot of code added. What additional functionality did we just get?
The possibility to handle suspend/hibernation in a more flexible way, provided that the drivers define the new callbacks.
So, we're going to have to now convert all drivers, right? Nice, I can always use a bump up in the "number of patches submited" numbers :)
thanks,
greg k-h
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Rafael J. WysockiQuoted 30 lines · Rafael J. Wysocki
Quoted 10 lines · Greg KH
Quoted 5 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Introduce 'struct
pm_ops' representing a set of suspend and hibernation operations for bus types, device classes and device types.Ok, I must have missed the thread describing why we need to do this, so, why do we need to do this? What is this going to buy us in the end after everything is changed?
There were many threads related to that.
To summarize, the first purpose is to separate suspend (aka s2ram and standby) callbacks from hibernation callbacks in such a way that the new callbacks won't take arguments and the purpose of each of them will be clearly specified. This has been requested multiple times by many people, including Linus himself, and the reason is that within the current scheme if ->resume() is called, for example, it's difficult to say why it's been called (ie. is it a resume from RAM or from hibernation or a suspend/hibernation failure etc.?).
The second purpose is to make the suspend/hibernation callbacks more flexible so that device drivers can handle more than they can within the current scheme. For example, some drivers may need to prevent new children of the device from being registered before their ->suspend() callbacks are executed or they may want to carry out some operations requiring the availability of some other devices, not directly bound via the parent-child relationship, in order to prepare for the execution of ->suspend(), etc.
Quoted 5 lines
Ultimately, we'd like to stop using the freezing of tasks for suspend and therefore the drivers' suspend/hibernation code will have to take care of the handling of the user space during suspend/hibernation which would be difficult within the current scheme, without the ->prepare() and ->complete() callbacks.
Ok, thanks. You might want to include this in the patch itself (hint, hint, hint...)
greg k-h
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Alan SternQuoted 3 lines
That was in fact the original plan, before Rafael changed over to calling
prepare()just prior tosuspend().
Heh, ok :-)
Quoted 6 lines
Your analysis is right. Removal isn't a problem; we don't care if drivers are prohibited from registering new children when they are unbound. In general we want to make it possible to unregister devices at any time during the suspend-resume procedure (except of course that a device can't be unregistered while one of its methods is running).
Quoted 6 lines
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_
prepare()is called.
I don't totally agree here. Drivers could have their own flag set internally with appropriate locking. The problem with your approach is locking. Just setting a flag is mostly useless, -unless- there appropriate locking between setter and testers.
For example, the flags we talked about previously, the one set _after_ prepare that causes device_add() to fail, this one must have its setting done within the same mutex/lock as device_add() internally uses to protect the list. Or something along those lines.
Now, your idea of having the core set some other flag to tell drivers to stop doing addition wouldn't work here because the driver would -not- hold the core device list mutex between testing it and performing the addition.
As a general matter, the driver would need to internally, using it's own locking mechanisms, ensure that it will not do any further device_add, and that's not something the core can help with. In our case, that could be acheived by flushing the work queues and ensuring we don't queue another one, or using some driver internal semaphore to set a "stop adding" flag whatever...
However, I think this is mostly a non-issue, because the core _does_ provide something here that is useful for drivers who don't want to bother with the above: The failure return from device_add. If drivers don't want to do something akin to what I described, they can just be made to deal gracefully with the failure from device_add that would happen due to the core internal flag being set after the return from prepare().
So I don't think we need anything else.
Cheers,
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Alan SternQuoted 3 lines
That was in fact the original plan, before Rafael changed over to calling
prepare()just prior tosuspend().
Heh, ok :-)
Quoted 6 lines
Your analysis is right. Removal isn't a problem; we don't care if drivers are prohibited from registering new children when they are unbound. In general we want to make it possible to unregister devices at any time during the suspend-resume procedure (except of course that a device can't be unregistered while one of its methods is running).
Quoted 6 lines
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_
prepare()is called.
I don't totally agree here. Drivers could have their own flag set internally with appropriate locking. The problem with your approach is locking. Just setting a flag is mostly useless, -unless- there appropriate locking between setter and testers.
For example, the flags we talked about previously, the one set _after_ prepare that causes device_add() to fail, this one must have its setting done within the same mutex/lock as device_add() internally uses to protect the list. Or something along those lines.
Now, your idea of having the core set some other flag to tell drivers to stop doing addition wouldn't work here because the driver would -not- hold the core device list mutex between testing it and performing the addition.
As a general matter, the driver would need to internally, using it's own locking mechanisms, ensure that it will not do any further device_add, and that's not something the core can help with. In our case, that could be acheived by flushing the work queues and ensuring we don't queue another one, or using some driver internal semaphore to set a "stop adding" flag whatever...
However, I think this is mostly a non-issue, because the core _does_ provide something here that is useful for drivers who don't want to bother with the above: The failure return from device_add. If drivers don't want to do something akin to what I described, they can just be made to deal gracefully with the failure from device_add that would happen due to the core internal flag being set after the return from prepare().
So I don't think we need anything else.
Cheers,
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_
prepare()is called.I don't totally agree here. Drivers could have their own flag set internally with appropriate locking. The problem with your approach is locking. Just setting a flag is mostly useless, -unless- there appropriate locking between setter and testers.
I didn't mention locking because it seemed obvious that locking is needed. Of course a flag alone is mostly useless.
But we already _do_ have a lock (dev->sem) and we _don't_ have a flag. I was pointing out that some drivers will want to have a flag added. Maybe so many drivers will want it that the flag should be added to the PM structure, where it will be available for every device. If only a few drivers want this new flag, then yes, they can put it in their private data structures.
Quoted 7 lines
However, I think this is mostly a non-issue, because the core _does_ provide something here that is useful for drivers who don't want to bother with the above: The failure return from
device_add. If drivers don't want to do something akin to what I described, they can just be made to deal gracefully with the failure fromdevice_addthat would happen due to the core internal flag being set after the return fromprepare().
Relying on a registration failure to handle this sort of thing is _not_ a good idea. For example, how would the driver distinguish failure caused by impending suspend from other sorts of failure?
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Benjamin HerrenschmidtQuoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_
prepare()is called.I don't totally agree here. Drivers could have their own flag set internally with appropriate locking. The problem with your approach is locking. Just setting a flag is mostly useless, -unless- there appropriate locking between setter and testers.
I didn't mention locking because it seemed obvious that locking is needed. Of course a flag alone is mostly useless.
But we already _do_ have a lock (dev->sem) and we _don't_ have a flag. I was pointing out that some drivers will want to have a flag added. Maybe so many drivers will want it that the flag should be added to the PM structure, where it will be available for every device. If only a few drivers want this new flag, then yes, they can put it in their private data structures.
Quoted 7 lines
However, I think this is mostly a non-issue, because the core _does_ provide something here that is useful for drivers who don't want to bother with the above: The failure return from
device_add. If drivers don't want to do something akin to what I described, they can just be made to deal gracefully with the failure fromdevice_addthat would happen due to the core internal flag being set after the return fromprepare().
Relying on a registration failure to handle this sort of thing is _not_ a good idea. For example, how would the driver distinguish failure caused by impending suspend from other sorts of failure?
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Benjamin HerrenschmidtQuoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_
prepare()is called.I don't totally agree here. Drivers could have their own flag set internally with appropriate locking. The problem with your approach is locking. Just setting a flag is mostly useless, -unless- there appropriate locking between setter and testers.
Here's an example of what I mean.
One of the things we don't want to do is bind a new driver to a device after it has gone through the prepare() stage. Doing so would involve calling the driver's probe() routine, which is likely to want to register new children and who knows what else. The probe routine might even end up running after the device was suspended! Clearly this should be avoided.
But the user can force a binding to occur by writing the device's path to the driver's "bind" attribute in sysfs. This means that driver_bind() in drivers/base/bus.c will need to know whether or not the device has gone through the prepare() stage, which means the device structure will need to have a flag set before prepare() is called (more precisely, the flag must be set before dev->sem is released following the call to prepare).
Either that or else driver_bind() must always block whenever a system sleep is in progress. That would be easier -- but it's a lot like what the freezer would do. Which would you prefer?
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Benjamin HerrenschmidtQuoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines
An important point I tried to make in the earlier email is that drivers will want a simple way to know when it is illegal for them to register new children. For example, suppose the registration is done by a workqueue routine. The most reliable way for the driver to insure that the routine won't try to register new children improperly is to have the routine check a flag which gets set _before_
prepare()is called.I don't totally agree here. Drivers could have their own flag set internally with appropriate locking. The problem with your approach is locking. Just setting a flag is mostly useless, -unless- there appropriate locking between setter and testers.
Here's an example of what I mean.
One of the things we don't want to do is bind a new driver to a device after it has gone through the prepare() stage. Doing so would involve calling the driver's probe() routine, which is likely to want to register new children and who knows what else. The probe routine might even end up running after the device was suspended! Clearly this should be avoided.
But the user can force a binding to occur by writing the device's path to the driver's "bind" attribute in sysfs. This means that driver_bind() in drivers/base/bus.c will need to know whether or not the device has gone through the prepare() stage, which means the device structure will need to have a flag set before prepare() is called (more precisely, the flag must be set before dev->sem is released following the call to prepare).
Either that or else driver_bind() must always block whenever a system sleep is in progress. That would be easier -- but it's a lot like what the freezer would do. Which would you prefer?
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Alan SternQuoted 10 lines · Alan Stern
I didn't mention locking because it seemed obvious that locking is needed. Of course a flag alone is mostly useless.
But we already _do_ have a lock (
dev->sem) and we _don't_ have a flag. I was pointing out that some drivers will want to have a flag added. Maybe so many drivers will want it that the flag should be added to the PM structure, where it will be available for every device. If only a few drivers want this new flag, then yes, they can put it in their private data structures.
What I'm saying is that a flag set prior to prepare() would be almost impossible to get right in term of locking for the reasons I explained.
I think this should be under driver reponsibility, using it's own internal locking. Or else, drivers can rely on failure.
Quoted 11 lines
Quoted 7 lines
However, I think this is mostly a non-issue, because the core _does_ provide something here that is useful for drivers who don't want to bother with the above: The failure return from
device_add. If drivers don't want to do something akin to what I described, they can just be made to deal gracefully with the failure fromdevice_addthat would happen due to the core internal flag being set after the return fromprepare().Relying on a registration failure to handle this sort of thing is _not_ a good idea. For example, how would the driver distinguish failure caused by impending suspend from other sorts of failure?
By the precise error code of course. But as I said, I would expect most bus drivers to have their own proper implementation that does the right thing, it's not like it was hard and there aren't that many bus drivers. Relying on failure is a possibility for simple ones that don't want to care much, in which case, there is little need to differenciate the failure caused by suspend from other failures other than maybe printing some garbage in the log vs. not :-)
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Alan SternQuoted 10 lines · Alan Stern
I didn't mention locking because it seemed obvious that locking is needed. Of course a flag alone is mostly useless.
But we already _do_ have a lock (
dev->sem) and we _don't_ have a flag. I was pointing out that some drivers will want to have a flag added. Maybe so many drivers will want it that the flag should be added to the PM structure, where it will be available for every device. If only a few drivers want this new flag, then yes, they can put it in their private data structures.
What I'm saying is that a flag set prior to prepare() would be almost impossible to get right in term of locking for the reasons I explained.
I think this should be under driver reponsibility, using it's own internal locking. Or else, drivers can rely on failure.
Quoted 11 lines
Quoted 7 lines
However, I think this is mostly a non-issue, because the core _does_ provide something here that is useful for drivers who don't want to bother with the above: The failure return from
device_add. If drivers don't want to do something akin to what I described, they can just be made to deal gracefully with the failure fromdevice_addthat would happen due to the core internal flag being set after the return fromprepare().Relying on a registration failure to handle this sort of thing is _not_ a good idea. For example, how would the driver distinguish failure caused by impending suspend from other sorts of failure?
By the precise error code of course. But as I said, I would expect most bus drivers to have their own proper implementation that does the right thing, it's not like it was hard and there aren't that many bus drivers. Relying on failure is a possibility for simple ones that don't want to care much, in which case, there is little need to differenciate the failure caused by suspend from other failures other than maybe printing some garbage in the log vs. not :-)
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Alan SternQuoted 16 lines · Alan Stern
One of the things we don't want to do is bind a new driver to a device after it has gone through the
prepare()stage. Doing so would involve calling the driver'sprobe()routine, which is likely to want to register new children and who knows what else. The probe routine might even end up running after the device was suspended! Clearly this should be avoided.But the user can force a binding to occur by writing the device's path to the driver's "bind" attribute in sysfs. This means that
driver_bind()indrivers/base/bus.cwill need to know whether or not the device has gone through theprepare()stage, which means the device structure will need to have a flag set beforeprepare()is called (more precisely, the flag must be set beforedev->semis released following the call to prepare).
Quoted 3 lines
Either that or else
driver_bind()must always block whenever a system sleep is in progress. That would be easier -- but it's a lot like what the freezer would do. Which would you prefer?
I don't fully understand what you are saying here.
You say "bind a new driver to a device after it has gone through the prepare() stage"
So either there was already a driver bound to that device, which got a prepare() callback, and in which case bind() doesn't mean much. Or there was not, in which case there was no prepare() call involved.
I suppose if you prepare(), then unbind(), then something tries to bind()... But wouldn't the core flag set after prepare() be plenty enough to shield against that ?
I fail to see the race you are talking about here.
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Alan SternQuoted 16 lines · Alan Stern
One of the things we don't want to do is bind a new driver to a device after it has gone through the
prepare()stage. Doing so would involve calling the driver'sprobe()routine, which is likely to want to register new children and who knows what else. The probe routine might even end up running after the device was suspended! Clearly this should be avoided.But the user can force a binding to occur by writing the device's path to the driver's "bind" attribute in sysfs. This means that
driver_bind()indrivers/base/bus.cwill need to know whether or not the device has gone through theprepare()stage, which means the device structure will need to have a flag set beforeprepare()is called (more precisely, the flag must be set beforedev->semis released following the call to prepare).
Quoted 3 lines
Either that or else
driver_bind()must always block whenever a system sleep is in progress. That would be easier -- but it's a lot like what the freezer would do. Which would you prefer?
I don't fully understand what you are saying here.
You say "bind a new driver to a device after it has gone through the prepare() stage"
So either there was already a driver bound to that device, which got a prepare() callback, and in which case bind() doesn't mean much. Or there was not, in which case there was no prepare() call involved.
I suppose if you prepare(), then unbind(), then something tries to bind()... But wouldn't the core flag set after prepare() be plenty enough to shield against that ?
I fail to see the race you are talking about here.
Ben.
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
Quoted 22 lines · Benjamin Herrenschmidt
Quoted 16 lines · Alan Stern
One of the things we don't want to do is bind a new driver to a device after it has gone through the
prepare()stage. Doing so would involve calling the driver'sprobe()routine, which is likely to want to register new children and who knows what else. The probe routine might even end up running after the device was suspended! Clearly this should be avoided.But the user can force a binding to occur by writing the device's path to the driver's "bind" attribute in sysfs. This means that
driver_bind()indrivers/base/bus.cwill need to know whether or not the device has gone through theprepare()stage, which means the device structure will need to have a flag set beforeprepare()is called (more precisely, the flag must be set beforedev->semis released following the call to prepare).Quoted 3 lines
Either that or else
driver_bind()must always block whenever a system sleep is in progress. That would be easier -- but it's a lot like what the freezer would do. Which would you prefer?
Quoted 8 lines
I don't fully understand what you are saying here.
You say "bind a new driver to a device after it has gone through the
prepare()stage"So either there was already a driver bound to that device, which got a
prepare()callback, and in which casebind()doesn't mean much. Or there was not, in which case there was noprepare()call involved.
That's why I said "gone through the prepare() stage" instead of saying "after prepare() was called". Yes, if there's no driver bound then there's no method to call. But the PM core will still pass the device to a subroutine whose job is to call the corresponding method, if one exists.
Quoted 5 lines
I suppose if you
prepare(), thenunbind(), then something tries tobind()... But wouldn't the core flag set afterprepare()be plenty enough to shield against that ?I fail to see the race you are talking about here.
Let's say Task 0 is starting a sleep transition when Task 1 calls driver_bind():
Task 0 Task 1 ------ ------ down(&dev->sem) succeeds down(&dev->sem) blocks Sees that dev has no driver so there is no prepare method to call up(&dev->sem) down() succeeds Sees that dev->power.prepared flag isn't set Calls (drv->probe)(dev) Sets dev->power.prepared flag drv tries to register a child device and fails for no good reason
One way to fix this is to set dev->power.prepared before releasing dev->sem. Another way to fix it is to have driver_bind() block (as though it were frozen) whenever a system sleep is underway.
Which do you prefer?
Alan Stern
Re: [RFC][PATCH 1/3] PM: Introduce new top level suspend and hibernation callbacks
In reply to Benjamin HerrenschmidtQuoted 22 lines · Benjamin Herrenschmidt
Quoted 16 lines · Alan Stern
One of the things we don't want to do is bind a new driver to a device after it has gone through the
prepare()stage. Doing so would involve calling the driver'sprobe()routine, which is likely to want to register new children and who knows what else. The probe routine might even end up running after the device was suspended! Clearly this should be avoided.But the user can force a binding to occur by writing the device's path to the driver's "bind" attribute in sysfs. This means that
driver_bind()indrivers/base/bus.cwill need to know whether or not the device has gone through theprepare()stage, which means the device structure will need to have a flag set beforeprepare()is called (more precisely, the flag must be set beforedev->semis released following the call to prepare).Quoted 3 lines
Either that or else
driver_bind()must always block whenever a system sleep is in progress. That would be easier -- but it's a lot like what the freezer would do. Which would you prefer?
Quoted 8 lines
I don't fully understand what you are saying here.
You say "bind a new driver to a device after it has gone through the
prepare()stage"So either there was already a driver bound to that device, which got a
prepare()callback, and in which casebind()doesn't mean much. Or there was not, in which case there was noprepare()call involved.
That's why I said "gone through the prepare() stage" instead of saying "after prepare() was called". Yes, if there's no driver bound then there's no method to call. But the PM core will still pass the device to a subroutine whose job is to call the corresponding method, if one exists.
Quoted 5 lines
I suppose if you
prepare(), thenunbind(), then something tries tobind()... But wouldn't the core flag set afterprepare()be plenty enough to shield against that ?I fail to see the race you are talking about here.
Let's say Task 0 is starting a sleep transition when Task 1 calls driver_bind():
Task 0 Task 1 ------ ------ down(&dev->sem) succeeds down(&dev->sem) blocks Sees that dev has no driver so there is no prepare method to call up(&dev->sem) down() succeeds Sees that dev->power.prepared flag isn't set Calls (drv->probe)(dev) Sets dev->power.prepared flag drv tries to register a child device and fails for no good reason
One way to fix this is to set dev->power.prepared before releasing dev->sem. Another way to fix it is to have driver_bind() block (as though it were frozen) whenever a system sleep is underway.
Which do you prefer?
Alan Stern