Power management of linux driver: linux sleep and wake-up (2)

Publisher:yunhuiLatest update time:2023-06-19 Source: elecfansKeywords:linux Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In Linux, hibernation is divided into three main steps: (1) Freeze user-mode processes and kernel-mode tasks; (2) Call the suspend callback function of registered devices; (3) Hibernate core devices and enable the CPU according to the registration order. Enter dormant state.       


Freezing a process means that the kernel sets the status of all processes in the process list to stopped and saves the context of all processes. When these processes are unfrozen, they are unaware that they have been frozen and simply continue execution. How to put Linux into hibernation? Users can control the system to enter hibernation by reading and writing the sys file /sys /power/state. For example: # echo standby > /sys/power/state commands the system to enter sleep mode. You can also use # cat /sys/power/state to get which sleep modes the kernel supports.


Linux Suspend process. 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) Next, let us take a detailed look at how Linux sleeps/wakes up.


Users' reading and writing of /sys/power/state will call state_store() in main.c. Users can write strings defined in const char * const pm_state[], such as "mem" and "standby". Then state_store() will call enter_state(), which will first check some state parameters and then synchronize the file system. (2) Prepare to freeze the process.

When entering suspend_prepare(), it allocates a virtual terminal to suspend to output information, then broadcasts a Notify that the system wants to enter suspend, closes the user-mode helper process, and then calls suspend_freeze_processes() at once to freeze all processes. The current status of all processes will be saved here. There may be some processes that refuse to enter the frozen state. When such processes exist, the freezing will fail. This function will give up the frozen process and unfreeze all the processes that were just frozen.


(3) Let the peripherals go to sleep.

Now, all processes (including workqueue/kthread) have stopped. The kernel mode task may hold some semaphores when stopped, so if you unlock the semaphore in the peripheral at this time, a deadlock may occur. , so be very careful when locking/unlocking in the peripheral's suspend() function. It is recommended not to wait for the lock in suspend() when designing.       


Finally, suspend_devices_and_enter() will be called to put all peripherals to sleep. In this function, if the platform registers suspend_pos (usually defined and registered in the board-level definition), suspend_ops->begin() will be called here, and then the driver device_suspend()->dpm_suspend() in /base/power/main.c will be called, and they will call the driver's suspend() callback in turn to sleep all devices. When all devices go to sleep, suspend_ops->prepare() will be called. This function usually does some preparation work to put the board into sleep. Next, in Linux, non-starting CPUs in multi-core CPUs will be turned off. As you can see from the comments, this is to prevent these other CPUs from causing race condion. From now on, only one CPU will be running. suspend_ops is a board-level power management operation, usually registered in the file arch/xxx/mach-xxx/pm.c. Next, suspend_enter() will be called. This function will close the arch irq and call device_power_down(). It will call the suspend_late() function. This function is the last function called when the system actually enters sleep. It is usually the last function in this function. inspection. If the check is OK, then sleep all system devices and buses, and call suspend_pos->enter() to put the CPU into a power saving state. At this time, it has gone to sleep, and the execution of the code stops here.


(4) Resume.

If the system is awakened by an interrupt or other event during sleep, the next code will begin to execute. The order of wake-up is reverse to the order of sleep, so the system devices and buses will wake up first, enable system interrupts, and enable sleep. When stopping the non-starting CPU, and calling suspend_ops->finish(), and in the suspend_devices_and_enter() function, it will continue to wake up each device and enable the virtual terminal. Finally call suspend_ops->end(). Return to the enter_state() function. When suspend_devices_and_enter() returns, the peripherals have woken up, but the processes and tasks are still frozen. Here, suspend_finish() will be called to unfreeze these processes and tasks, and a Notify will be issued to indicate this. The system has exited from the suspend state and woke up the terminal. At this point, all sleeping and waking up have been completed, and the system continues to run.


Keywords:linux Reference address:Power management of linux driver: linux sleep and wake-up (2)

Previous article:Analysis of the standard Linux sleep and wake-up mechanism of power management of Linux driver (1)
Next article:Regulator mechanism process of power management of Linux driver (1)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号