3220 views|0 replies

920

Posts

0

Resources
The OP
 

Linux power management detailed explanation [Copy link]

1. Overview

Although Linux can run on any PC with a processor of 386 or above, most people currently use new desktop PCs or laptops with various peripherals. As a result, power management (PM) is becoming increasingly important. On laptops, power management can save energy and extend battery life, while on desktop PCs it can reduce radiation, cool down, and extend the life of peripherals. Most operating systems today have built-in support for power management, such as Windows and Linux.

2. How to implement power management on a PC

To implement power management, there are two most important points: first, the device itself needs to support power saving functions, such as hard disks, which can be temporarily turned off by instructions; second, the operating system needs to support power management, so that the driver's power management function can be called to turn off the device after a period of idleness.

Two power management standards: APM and ACPI

The traditional APM (Advanced Power Management) is a power management standard based on BIOS. The latest version is 1.2. It provides the functions of CPU and device power management. However, since this power management method is mainly implemented by BIOS, there are some defects, such as excessive dependence on BIOS, incompatibility between new and old BIOS, and inability to determine whether the power management command is initiated by the user or by BIOS, and lack of support for some new hardware such as USB and 1394. In order to make up for the defects of APM, a new power management ACPI came into being. This is ACPI (Advanced Configuration and Power Interface). It mainly converts the main executor of power management from BIOS to operating system, which can provide greater flexibility and scalability.

The current PC motherboard generally supports both APM and ACPI standards.

3. Linux support for power management

kernel module

For the two different standards of APM and ACPI, the Linux kernel provides two different modules to implement power management functions, namely apm and acpi. It should be noted that apm and acpi are two conflicting modules. Users can only load one of them at the same time. If they find that one of them is already loaded when loading, they will automatically exit.

In the officially released kernel, APM is a more mature power management method, which can complete most of the functions that ACPI can complete under Windows. Since the functions of ACPI in the official kernel are relatively limited, it is still in the development version. Therefore, most current distributions, such as Red Hat, use apm as the power management method by default. However, it is worth noting that ACPI in Linux is actually maintained by a separate project team module, and the current kernel ACPI version is actually far behind the latest version. Since any new features are added to the stable version of Linux very cautiously, we may only be able to see the stable and fully functional version of ACPI after the release of Linux 2.6.x. However, we can also apply the latest ACPI patch to the kernel to obtain these functions.

Here is the homepage of ACPI: http://sf.net/projects/acpi/

The following introduction to power management focuses on APM.

User-mode Daemon

In order to make better use of the power management function in the Linux kernel, we also need the cooperation of the user-mode daemon program. For APM and ACPI, there are two different software, apmd and acpid. The functions they implement are similar, both allowing users to predefine certain policies, then track the power status and perform specific operations. There is also a tool apm in the apmd software package, which users can use to make the machine actively enter the standby and suspend states, and can also query the apm version number of the bios. When using acpi, the same function can be achieved by directly operating the proc file system.

4. The power management mechanism of the driver under Linux

Under Linux, there is no need to write the code corresponding to APM and ACPI for the driver separately. Linux, like Windows, provides a unified power management interface for the driver. As long as the driver implements these interfaces, it can implement the power management function. The operating system will notify the driver to complete these operations when it deems it appropriate.

The following five points need to be implemented to implement the device power management interface:

1. Use pm_register to register each instance of the device;

2. Call pm_access before operating the hardware (this will ensure that the device has been awakened and is in the ready state);

3. The user's own pm_callback function will be called when the system enters the suspend state (ACPI D1-D3) or resumes from the suspend state (ACPI D0);

4. Call the pm_dev_idle function when the device is not in use. This operation is optional and can enhance the monitoring ability of the device idle state;

5. When it is unloaded, use pm_unregister to cancel the registration of the device.

5. Programming APM

The following introduces the programming method of using the APM function in real mode and under Linux:

Since APM is provided by the bios, we can directly call the int 15 soft interrupt in real mode (such as DOS) to perform power management operations.

In real mode, the standby, suspend and poweroff functions of APM can be implemented by the following assembly language:

standby:
mov ax, 5307H
mov bx, 1
mov cx, 1
int 15H

suspend:
change to mov cx,2

poweroff:
change to mov cx,3



One thing to note is that the Linux kernel does not use the same method as the real mode to call the int 15H interrupt, but directly calls the bios protected mode interface. So if we modify the apm related code in the bios and do not handle the problem of the protected mode interface, the following situation may occur: everything is normal when using the apm function in real mode DOS, but a kernel general protection error occurs when calling the apm function in Linux.

In Linux, we can complete the same function by operating the apm_bios device.

The following code can implement the APM suspend function, which is equivalent to apm -s

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <asm/fcntl.h>
#include <linux/types.h>
#include <sys/types.h>
#include "apm.h"

int main()
{
int fd, res;
fd = open("/dev/apm_bios", O_RDWR);
if (fd < 0) {
printf("open /dev/apm_bios error!\n");
exit(-1);
}
sync();
res = ioctl(fd, APM_IOC_SUSPEND, NULL);
if (res != 0) {
printf("ioctl error!\n");
close(fd);
exit(-1);
}
close(fd);
return 0;
}



If we change SUSPEND in the above program to STANDBY, we can also achieve the function of apm -S.

The POWEROFF operation has its own unique process under Linux. Finally, according to the existence of apm or acpi in the kernel, the corresponding different processes are executed to turn off the power. Please refer to the Linux kernel source code. There is also a certain introduction in my article "Analysis of Linux Shutdown and Restart Process".


6. Frequently Asked Questions (FAQ)

1) My system cannot be suspended. What's going on?

Before suspending, the system will send messages to all drivers. If an arrogant driver returns an -EBUSY at this time, the attempt to suspend will be rejected by this driver. You can only try again after a while. If this driver always rejects (really arrogant, but it may have its own difficulties), you will never be able to suspend.

2) I press the system's POWEROFF switch, and the system will automatically shut down on the ATX motherboard. What is the processing flow?

A kernel thread is established in the kernel APM module to continuously monitor the system status. The user's shutdown action is intercepted and processed here. For detailed processes, please refer to my article "Analysis of Linux Shutdown and Restart Process".

3) Where is the document on power management in Linux?

The implementation of the Linux driver power management interface is defined in detail in pm.txt in the Linux/document.tion directory, and there are detailed examples. The implementation process of apm and acpi needs to refer to the implementation of the Linux source code.

7. Summary

The power management in Linux is a developing code. From the current trend, ACPI will eventually replace APM. Now using APM is a more mature and safe solution. If we write a driver now, we should strictly abide by the interface specified in pm.txt in the document, so that our driver has stronger adaptability and stability in power management.

This post is from Power technology
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list