停止运行时如何关闭linux电源? [英] How to make linux power off when halt is run?
问题描述
我已经成功使用pm_power_off
函数指针使我的自定义Linux板通过i2c调用其电源管理芯片(以关闭电源).
I have successfully used the pm_power_off
function pointer to make my custom Linux board call its power management chip over i2c (to turn the power off).
我也希望Linux halt
命令也能关闭电源.
I would like the Linux halt
command to switch the power off too.
我该如何实现?
machine_halt
的(ARM)代码没有类似于machine_power_off
的pm_power_off
的指针.
The (ARM) code for machine_halt
does not have an pointer analogous to machine_power_off
's pm_power_off
.
arch/arm/kernel/reboot.c:
arch/arm/kernel/reboot.c:
/*
* Halting simply requires that the secondary CPUs stop performing any
* activity (executing tasks, handling interrupts). smp_send_stop()
* achieves this.
*/
void machine_halt(void)
{
local_irq_disable();
smp_send_stop();
local_irq_disable();
while (1);
}
/*
* Power-off simply requires that the secondary CPUs stop performing any
* activity (executing tasks, handling interrupts). smp_send_stop()
* achieves this. When the system power is turned off, it will take all CPUs
* with it.
*/
void machine_power_off(void)
{
local_irq_disable();
smp_send_stop();
if (pm_power_off)
pm_power_off();
}
我显然可以只破解machine_halt
,但如果可能的话,我想适当地"做到这一点.
I could obviously just hack machine_halt
, but I would like to do this "properly", if possible.
我是否错过了某些内容(可能在用户空间中),可能导致halt
命令执行关闭电源"?
Have I missed something (perhaps in userspace) which could cause a halt
command to execute a "power off"?
更新:感谢您的回答和您的所有评论,他们帮助我意识到了实际的问题所在.
Update: Thanks for the answer and all of your comments, they have helped me realise what the actual issue is.
我的问题是:
我有一个输入边,可用于自定义电源管理单元.可以将其视为启动按钮,没有停止或重置功能.我完全可以控制PMU代码(这是一个ATMEGA作为i2c从设备运行).
I have an input edge, available to the custom power management unit. Think of it as a start button, with no stop or reset functionality. I have full control of the PMU code (it's an ATMEGA running as an i2c slave).
- 如果Linux内核正在运行,我希望忽略边缘.
- 如果CPU断电,我希望边缘能够打开CPU.
- 如果Linux内核停止运行,我希望边缘重新设置CPU.
案例1很简单,无所事事.
Case 1 is easy, nothing to do.
情况2很简单,请在驱动程序中定义pm_power_off
,以向PMU发送i2c消息.幸运的是,调用pm_power_off
时,i2c子系统仍处于工作状态.
Case 2 was easy, define pm_power_off
in my driver to send an i2c message to the PMU. Luckily the i2c subsystem is still in a working state when pm_power_off
is called.
问题3是问题-我一直在寻找pm_halt
进行定义,以向PMU发送i2c消息.
Case 3 is the problem - I was looking for a pm_halt
to define, to send an i2c message to the PMU.
也许还有另一种方式,例如0andriy评论?
Perhaps there is another way, as 0andriy comments?
内核中是否有地方以i2c消息以几赫兹的速率向PMU发出信号,除非是halt
计算机?
Is there a place in the kernel to keep prodding the PMU with i2c messages at a few Hz, unless the machine is halt
ed?
位于 https://unix.stackexchange.com/a/42576/17288 的答案为:
"**如果启用了ACPI,这些天的停顿足够聪明,可以自动关闭电源.实际上,它们现在在功能上是等效的."
"* These days halt is smart enough to automatically call poweroff if ACPI is enabled. In fact, they are functionally equivalent now."
也许有某种方式可以提供或吸引ACPI-我将不得不仔细阅读.
Perhaps there is some way of providing or hooking into ACPI - I will have to read up on it.
推荐答案
您应使用poweroff
命令或halt -p
.根据 man 8停止,halt
命令(不带参数)不保证关闭计算机电源. 这里:
You should use poweroff
command or halt -p
. As per man 8 halt, halt
command (with no arguments) doesn't guarantee to power off your machine. Reasons are described here:
在ACPI之前使用了
halt
(今天将为您关闭电源)*.它将停止系统,然后打印一条消息,提示现在可以关闭电源".那时有物理的开/关开关,而不是现代计算机的组合ACPI控制的电源按钮.
halt
was used before ACPI (which today will turn off the power for you)*. It would halt the system and then print a message to the effect of "it's ok to power off now". Back then there were physical on/off switches, rather than the combo ACPI controlled power button of modern computers.
*这几天halt
很聪明,如果启用了ACPI,则可以自动关闭电源.实际上,它们现在在功能上是等效的.
*These days halt
is smart enough to automatically call poweroff if ACPI is enabled. In fact, they are functionally equivalent now.
从halt
工具源代码中可以看到,它发出cmd = RB_POWER_OFF = LINUX_REBOOT_CMD_POWER_OFF> reboot()系统调用.
As you can see from halt
tool source code, it issues reboot() system call with cmd = RB_POWER_OFF = LINUX_REBOOT_CMD_POWER_OFF
.
在内核中,该系统调用是在此处实现的,并在cmd = LINUX_REBOOT_CMD_POWER_OFF
上调用:
-> kernel_power_off()
-> machine_power_off()
-> pm_power_off()
In kernel, that system call is implemented here, and on cmd = LINUX_REBOOT_CMD_POWER_OFF
, it calls:
-> kernel_power_off()
-> machine_power_off()
-> pm_power_off()
这篇关于停止运行时如何关闭linux电源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!