Linux driver Power management Linux hibernation and wake-up (2)

Source: Internet
Author: User

In Linux, hibernation is mainly divided into three main steps: (1) freeze the user-state process and kernel-state tasks, (2) invoke the suspend callback function of the registered device, and (3) Hibernate the core device in order of registration and put the CPU into hibernation state. The freeze process is the kernel that sets the state of all processes in the process list to stop and saves the context for all processes. When these processes are thawed, they do not know that they have been frozen, but simply carry on. How do I get Linux into hibernation? The user can read and write the Sys file/sys/power/state is to implement the control system into hibernation. For example: # echo Standby >/sys/power/state command system goes into hibernation. You can also use # Cat/sys/power/state to get what kinds of hibernation the kernel supports.

The process of Linux Suspend. Paths to related files: linux_soruce/kernel/power/main.c linux_source/kernel/arch/xxx/mach-xxx/pm.c

LINUX_SOURCE/DRIVER/BASE/POWER/MAIN.C (1) Let's take a look at how Linux sleeps/wakes up in detail.

       the user reads and writes to the/sys/power/state to State_store () in Main.c, the user can write to the const char * const PM_ State[], such as "mem", "standby". Then State_store () calls Enter_state (), which first checks for some state parameters and then synchronizes the file system. (2) Prepare to freeze the process.

When entering Suspend_prepare (), it assigns suspend a virtual terminal to output information, then broadcasts a system to enter suspend notify, shuts down the user-State helper process, and then calls Suspend_ Freeze_processes () Freezes all processes, where the current state of all processes is saved, and perhaps some processes refuse to enter a frozen state, which causes the freeze to fail when such a process exists, and this function discards the frozen process and thaws all the processes just frozen.

(3) Put the peripheral into hibernation.

       now, all the processes (including workqueue/kthread) have stopped, and the kernel-state task is likely to hold some semaphores when it is stopped. So if this time in the peripheral to unlock this semaphore can be a deadlock, so in the peripheral suspend () function inside the Lock/unlock lock to be very careful, it is recommended to design in the suspend () do not wait for the lock.        finally calls Suspend_devices_and_enter () to hibernate all the peripherals, in this function, if the platform registers the Suspend_pos ( Typically defined and registered in the board-level definition), this will call Suspend_ops->begin () and then the Device_suspend ()->dpm_suspend () in DRIVER/BASE/POWER/MAIN.C will be called, they will call the driver's suspend () callback in turn to hibernate all the devices. When all the devices Hibernate, Suspend_ops->prepare () is called, and this function usually does some preparatory work to get the board into hibernation. Next Linux, the non-booting CPU in the Multicore CPU will be switched off, and the annotations see to avoid these other CPUs causing race condion, and then only one CPU is running later.       suspend_ops is a board-level power management operation that is typically registered in the file ARCH/XXX/MACH-XXX/PM.C. Next, Suspend_enter () is called, the function closes the arch IRQ, calls Device_power_down (), and it calls the Suspend_late () function, which is the function that the system actually goes into the last call to sleep, The final check is usually done in this function. If the check is fine, then hibernate all the system devices and buses, and call Suspend_pos->enter ()   to get the CPU into a power-saving state. At this point, it is dormant, and the execution of the code stops here.

(4) Resume.

If the system is interrupted during hibernation or other events wake up, the next code starts executing, and the order of wake-up is reversed in the order of hibernation, so the system device and bus will wake up first, enable the system to break, enable the non-booting CPU to stop during hibernation, and call suspend_ops-> Finish (), and the Suspend_devices_and_enter () function will also continue to wake each device to enable the virtual terminal. Finally call Suspend_ops->end (). Returning to the Enter_state () function, when Suspend_devices_and_enter () returns, the peripheral has woken up, but the processes and tasks are still frozen, and Suspend_finish () is called to unfreeze the processes and tasks. It also emits notify to indicate that the system has exited from the suspend state and wakes up the terminal. Here, all the sleep and wake are complete and the system continues to run.

Linux driver Power management Linux hibernation and wake-up (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.