halt为运行时如何让linux断电?

How to make linux power off when halt is run?

我已成功使用 pm_power_off 函数指针让我的自定义 Linux 开发板通过 i2c 调用其电源管理芯片(关闭电源)。

我也想用Linuxhalt命令来关闭电源。

我怎样才能做到这一点?

machine_halt 的 (ARM) 代码没有类似于 machine_power_offpm_power_off 的指针。

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,但如果可能的话,我想这样做 "properly"。

我是否遗漏了某些东西(可能在用户空间中)可能导致 halt 命令执行 "power off"?


更新:感谢您的回答和所有评论,它们帮助我了解了实际问题所在。

我的问题是:

我有一个输入边缘,可用于自定义电源管理单元。将其视为一个开始按钮,没有停止或重置功能。我完全控制了 PMU 代码(它是一个 ATMEGA 运行 作为 i2c 从设备)。

  1. 如果Linux内核是运行,我希望边缘被忽略。
  2. 如果CPU关机了,我想让edge开机CPU。
  3. 如果 Linux 内核停止,我希望边缘重置 CPU。

情况一很简单,不用做。

情况 2 很简单,在我的驱动程序中定义 pm_power_off 以向 PMU 发送 i2c 消息。幸运的是,当 pm_power_off 被调用时,i2c 子系统仍然处于工作状态。

情况 3 是问题 - 我正在寻找 pm_halt 来定义,向 PMU 发送 i2c 消息。

也许还有另一种方法,正如 0andriy 评论的那样?

内核中是否有地方可以用几赫兹的 i2c 消息不断刺激 PMU,除非机器已 halted?

答案位于:https://unix.stackexchange.com/a/42576/17288 为:

"* 现在 halt 足够聪明,可以在启用 ACPI 时自动调用 poweroff。事实上,它们现在在功能上是等价的。"

也许有一些方法可以提供或连接到 ACPI - 我将不得不阅读它。

您应该使用 poweroff 命令或 halt -p。根据 man 8 halt, halt command (with no arguments) doesn't guarantee to power off your machine. Reasons are described here:

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.

*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() 系统调用

在内核中,该系统调用已实现 here,而在 cmd = LINUX_REBOOT_CMD_POWER_OFF 上,它调用:
-> kernel_power_off()
-> machine_power_off()
-> pm_power_off()

在 Debian/Ubuntu 系统上(我不知道在其他 Linux 操作系统中是否可以这样做),如果您希望 halt 命令执行poweroff 你可以创建这个文件:

/etc/default/halt

写入该文件的内容:

# Default behaviour of shutdown -h / halt. Set to "halt" or "poweroff".
HALT=poweroff

正如评论所说,您现在可以设置 halt 应该如何工作。如果将其设置为暂停,它只会关闭计算机而不会关闭电源。如果您设置 poweroff 它将像 poweroff 命令一样在关闭所有后关闭计算机电源。