Understand Linux power management in one article (collection)

1 Introduction

All things operate according to the law of conservation of energy. Therefore, there is no perpetual motion machine in the world, and all movements require energy. People need energy to walk and think, cars need energy to run on the road, and electronic products need energy to operate. In addition, energy is not inexhaustible. Animals have evolved hibernation so that they can survive the cold winter and lack of food. When the car is parked for a long time, it must be turned off to save fuel. When electronic products are not in use, they should be turned off or on standby to save power.

The power consumption management of electronic products is a systematic project. It needs to use as few resources and power consumption as possible in various usage scenarios to complete the desired functions, while also having a long battery life and not getting hot. For electronic products such as mobile phones, in order to last, the battery power needs to be increased from the perspective of energy supply. However, the battery power increases without increasing the size. Therefore, people use various methods to increase energy density so that batteries can be made smaller and have greater power. However, as mobile phones support larger screens and richer functions, computing power units such as DSP and CNN are required for acceleration, which also requires more power consumption. On the one hand, high power consumption reduces battery life; on the other hand, it also brings a lot of heat, causing an electronic product with a structure, material, and volume like a mobile phone to become seriously hot and even hot to the touch.

In order to solve unnecessary power consumption, Linux provides a variety of power management methods. In order to solve the power consumption when the system is not working, Linux provides sleep (suspend to ram or disk), shutdown (power off), and reset (reboot). In order to solve unnecessary power consumption during runtime, Linux provides power management mechanisms such as runtime pm, cpu/device dvfs, cpu hotplug, cpu idle, clock gate, power gate, and reset. In order to solve the impact of runtime power management on performance, Linux provides the pm qos function and voting mechanism to balance performance and power consumption, which can reduce power consumption without affecting performance.

2. Framework

linux power management framework

Power consumption management is not only software logic, but also requires the support of hardware functions. Hardware design determines the lower limit of power consumption, thermal design determines the upper limit of power consumption, and software uses some mechanisms and strategies to reduce power consumption to the lower limit of hardware power consumption.

Low-power consumption mechanisms supported on the chip: power domain, reset, clock, system sleep/wake-up, low-power consumption of CPU, DDR self-refresh, etc. The system generally controls the voltage, current size and presence supplied to each device through the pmic chip. Peripherals also support low-power modes, or their power can be turned on and off.

Above the chip, power chip, and system peripherals is the management framework corresponding to each hardware low-power function:

  • Clock framework: Clock management framework (please refer to the article " Understanding the Linux clock subsystem in one article - Zhihu (zhihu.com) ") uniformly manages the clock resources of the system. These clock resources include pll/mux/div/gate, etc., which are very important for the clock. Resource operations are encapsulated and maintained as a clock tree, abstracting core logic such as frequency acquisition, frequency setting, and clock switching, and providing interfaces such as frequency adjustment, frequency query, enablement, and disablement for other driver sub-modules;
  • Regulator framework: Voltage management framework (please refer to the article " Understanding the Linux Regulator Subsystem in One Article - Zhihu (zhihu.com) ") to uniformly manage the power supply resources of the system. These power supply resources include pmic, corebuck, etc., which encapsulate the operation of power supply resources. They generally control the voltage and current adjustment or on/off of power supply resources through iic, spi, gpio, pwm, etc., and provide voltage and current adjustment for other drivers or frameworks. Switch and other interfaces;
  • Power domain framework: The power domain management framework (please refer to the article " Understand the Linux power domain framework in one article - Zhihu (zhihu.com) ") uniformly manages the power domain of the chip. Encapsulate the power domain switch operation in the chip, abstract the core switch processing logic, and provide switch and other interfaces for runtime pm and system sleep wake-up;
  • Reset framework: The reset management framework (please refer to the article " Understanding the linux reset framework in one article ") uniformly manages the reset of the chip. Encapsulate the reset and dereset register operations in the chip. Abstract the core reset/dereset processing logic and provide unified reset/dereset and other interfaces for other drivers;
  • Opp framework: The opp (Operating Performance Point) management framework (please refer to the article " Understanding linux device dvfs in one article - Zhihu (zhihu.com) ") uniformly manages the voltage and frequency combination that makes the CPU or Devices work normally. The kernel provides this Layer to provide some relatively fixed voltage and frequency combinations, thereby making frequency and voltage regulation simpler;
  • Runtime pm: The runtime pm management framework (please refer to the article " Understanding Linux runtime pm in one article - Zhihu (zhihu.com) ") uniformly manages on-chip devices and corresponding power domain hardware operations. It is encapsulated based on the switch interface provided by power domain and the corresponding clock, reset, and power management when the device is running. The device will specify the specific power domain in dts. The device driver implements clock and reset through runtime_suspend and runtime_resume. , electricity management, and then register to the runtime pm framework. runtime pm provides pm_runtime_get_xxx/pm_runtime_put_xxx class interfaces for the device. Before using the device, call the get interface first to let the device exit low power consumption. After using the device, call the put interface to let the device enter low power consumption. When all the devices are under power domain After all the devices have entered low power consumption, the power domain can be turned off. When a device under the power domain needs to work, the power domain must be turned on;
  • Device dvfs: device dvfs management framework (please refer to the article " Understanding linux device dvfs in one article - Zhihu (zhihu.com) ") uniformly manages the frequency and voltage adjustment of the device, using the interface provided by the opp framework, based on the settings given by the device driver Frequency modulation and voltage regulation are very similar to CPU dvfs;
  • Sleep: System sleep wake-up (please refer to the article " Understand Linux system sleep wake-up in one article - Zhihu (zhihu.com) ") can enter a state with very low power consumption when the system is not in use, and the chip power consumption will be very high. Low, DDR enters self-refresh and can recover quickly when the system is in use. During the sleep and wake-up process, the system suspend and resume functions provided by the device are called, and the suspend and resume functions provided by the power domain are called to close and open the cpuidle/cpuhotplug function;
  • Reboot/poweroff: System restart/shutdown. The two functions of restart and shutdown are closely related. You can simply understand that restarting means shutting down the system first and then turning it on again. System restart can reset the system after an abnormality occurs in the system or after a system upgrade. System shutdown is generally an operation in which the user no longer uses the machine, or the system detects that the user is no longer using the machine or will not use it for a foreseeable period of time, and the system is powered off. System restart and system shutdown are both implemented by the reboot system call, but the corresponding parameters are different. After the Service initiates restart and shutdown, the event will be handled by the init process. Init will save some data, stop all srvice, kill ordinary processes, and finally call the system call reboot to restart or shut down;
  • Cpu idle: The cpu idle management framework (please refer to the article " Understanding Linux cpu idle in one article - Zhihu (zhihu.com) ") will choose to enter the corresponding idle based on the idle status of the CPU in the current period and the estimate of the next idle time. Levels, different levels have different power consumption benefits and different entry and exit times. Generally, power consumption benefits are low, but corresponding to less exit time;
  • Cpu dvfs: The cpu dvfs management framework (please refer to the article " Understand Linux cpu dvfs in one article - Zhihu (zhihu.com) ") provides different governors to control the frequency and voltage adjustment of the CPU. These governors completely control the cpu dvfs It is handed over to the user service, which adjusts the frequency and voltage accordingly according to different scenarios, and some also adjusts the frequency and voltage dynamically according to the load;
  • Cpu hotplug: The cpu hotplug management framework (please refer to the article " Understanding Linux cpu hotplug in one article - Zhihu (zhihu.com) ") provides a mechanism for service: when the current idle cpu loads a lot, it will be directly unplugged by the service Remove some CPUs, and when the demand for CPU loading increases, the service will plug in the CPUs;
  • Pm qos: pm qos (quality of service) service quality (please refer to the article " Understanding Linux PM QOS in one article - Zhihu (zhihu.com) ") is to solve the problem that low power consumption may reduce performance. It reports to the driver or upper layer Service provides a set of interfaces for performance constraint requirements. During low-power management such as cpu dvfs and cpu idle, the performance constraint requirements will be checked. When it is found that low-power actions will affect performance, they will not be performed. This low-power operation;

The upper-layer service will have a dedicated power consumption management application to coordinate other services to enter sleep and wake up, and inform each service to implement corresponding power consumption strategies when entering different scenarios.

3. Framework of low-power management modules in the kernel

3.1 clock framework

For details, see the article " Understanding the Linux clock subsystem in one article - Zhihu (zhihu.com) "

clockframeworkframework

3.2 Regulator framework

For details, see the article " Understanding the Linux Regulator Subsystem in One Article - Zhihu (zhihu.com) "

regulator framework

3.3 Power domain framework

For details, see the article " Understanding the Linux Power Domain Framework in One Article - Zhihu (zhihu.com) "

power domain framework

3.4 Reset framework

For details, see the article " Understand the linux reset framework in one article "

reset framework

3.5 Up framework

The opp framework is relatively simple, so it will not be described in a separate article. The opp framework is also introduced in the article " Understanding linux device dvfs in one article - Zhihu (zhihu.com) ".

3.6 Runtime pm

For details, see the article " Understanding Linux runtime pm in one article - Zhihu (zhihu.com) "

rpm framework

3.7 Device dvfs

For details, see the article " Understanding linux device dvfs in one article - Zhihu (zhihu.com) "

linux device dvfs framework

3.8 System sleep wake-up

For details, please see the article " Understanding Linux system sleep and wake-up in one article - Zhihu (zhihu.com) "

System sleep wake-up framework

3.9 Cpu idle

For details, see the article " Understanding Linux CPU Idle in One Article - Zhihu (zhihu.com) "

cpuidle framework

3.10 Cpu dvfs

For details, see the article " Understanding Linux CPU Dvfs in One Article - Zhihu (zhihu.com) "

cpu dvfs framework

3.11 Cpu hotplug

For details, see the article " Understanding Linux CPU Hotplug in One Article - Zhihu (zhihu.com) "

cpu hotplug

3.12 Pm qos

For details, see the article " Understanding Linux PM QOS in One Article - Zhihu (zhihu.com) "

pm qos framework

(I usually share useful articles on Linux technology. Follow me and you can receive push notifications of related articles on a regular basis. I have the same name on Zhihu and WeChat: Hackers and Photographers)

Guess you like

Origin blog.csdn.net/u012294613/article/details/132452900