当前位置:首页>Linux>写Linux驱动,必须掌握的Runtime PM机制

写Linux驱动,必须掌握的Runtime PM机制

  • 2026-11-02 22:20:41
写Linux驱动,必须掌握的Runtime PM机制

Linux驱动入门课程:

https://www.bilibili.com/cheese/play/ss635583370

Linux驱动进阶课程:

https://www.bilibili.com/cheese/play/ss254727759

01

简介

Runtime PM (运行时电源管理) 是 Linux 内核电源管理框架中非常高效且灵活的部分。Runtime PM 允许单个设备在系统运行过程中,根据其繁忙程度动态地进入低功耗状态,而不会干扰其他设备的正常工作。

Runtime PM 的基本逻辑是:当设备不再使用时,自动关掉它的电源或时钟;当有请求到来时,再即时唤醒它。

02

基本工作原理

为了实现 Runtime PM,内核维护了一个针对每个设备的引用计数(Usage Count):

  • 计数 > 0:设备处于活跃状态(Active),不能休眠。

  • 计数 = 0:设备处于空闲状态(Idle),可以尝试进入低功耗模式。

03

设备状态

Runtime PM 将设备状态简化为以下四种,并在它们之间切换:

  • RPM_ACTIVE:设备处于正常工作状态。

  • RPM_SUSPENDING:设备正在执行进入低功耗模式的过程。

  • RPM_SUSPENDED:设备已关闭或处于低功耗模式。

  • RPM_RESUMING:设备正在从低功耗模式恢复到活跃状态。

04

层级依赖

Runtime PM 具有级联效应:

  • 唤醒:当一个子设备(如 USB 鼠标)被唤醒时,内核会自动确保它的父设备(如 USB 控制器)也被唤醒。

  • 休眠:只有当所有的子设备都进入了 SUSPENDED 状态,父设备才被允许进入休眠。

05

重要结构体

struct dev_pm_ops {  int (*prepare)(struct device *dev);  void (*complete)(struct device *dev);  int (*suspend)(struct device *dev);  int (*resume)(struct device *dev);  int (*freeze)(struct device *dev);  int (*thaw)(struct device *dev);  int (*poweroff)(struct device *dev);  int (*restore)(struct device *dev);  int (*suspend_late)(struct device *dev);  int (*resume_early)(struct device *dev);  int (*freeze_late)(struct device *dev);  int (*thaw_early)(struct device *dev);  int (*poweroff_late)(struct device *dev);  int (*restore_early)(struct device *dev);  int (*suspend_noirq)(struct device *dev);  int (*resume_noirq)(struct device *dev);  int (*freeze_noirq)(struct device *dev);  int (*thaw_noirq)(struct device *dev);  int (*poweroff_noirq)(struct device *dev);  int (*restore_noirq)(struct device *dev);  //运行时电源管理  int (*runtime_suspend)(struct device *dev);  //设备空闲并满足延迟条件时调用  int (*runtime_resume)(struct device *dev);  //设备被重新访问,需要恢复工作时调用  int (*runtime_idle)(struct device *dev);  //当引用计数归零时,内核询问驱动:“现在是否方便进入低功耗?”(通常在此函数中决定是否启动延迟自动休眠)。};

设备电源管理操作函数集,包含系统级休眠,运行时电源管理,磁盘休眠等。在运行时电源管理中,我们主要关注 runtime_suspend, runtime_resume, runtime_idle 。

06

API函数

//使能设备的Runtime PM功能voidpm_runtime_enable(struct device *dev);//禁止设备的Runtime PM功能voidpm_runtime_disable(struct device *dev);//设置Runtime PM状态为"active"intpm_runtime_set_active(struct device *dev);//设置Runtime PM状态为"suspended"intpm_runtime_set_suspended(struct device *dev);//递增计数并同步唤醒设备。如果设备在睡觉,它会阻塞直到runtime_resume执行完毕。intpm_runtime_get_sync(struct device *dev);//它是pm_runtime_get_sync的安全包装版。如果唤醒失败,它内部会自动处理计数平衡,开发者无需手动清理。intpm_runtime_resume_and_get(struct device *dev);//递增计数,但异步触发唤醒。函数立即返回,不等待唤醒完成。intpm_runtime_get(struct device *dev);//计数减1。如果计数达到0,异步触发runtime_suspend。intpm_runtime_put(struct device *dev);//计数减1。如果计数达到0,触发runtime_suspend, 并同步等待执行完成intpm_runtime_put_sync(struct device *dev);//配合延迟机制使用。减1后不会立即休眠,而是等待设定的延迟时间。如果这段时间内没有新的get,再休眠。intpm_runtime_put_autosuspend(struct device *dev);//告知内核该设备支持延迟自动休眠voidpm_runtime_use_autosuspend(struct device *dev);//设置延迟时间(单位:毫秒)voidpm_runtime_set_autosuspend_delay(struct device *dev, int delay);//更新“最后一次忙碌”的时间戳。通常在put_autosuspend之前调用,确保延迟从当前时刻开始计算。voidpm_runtime_mark_last_busy(struct device *dev);//判断设备当前是否处于active状态boolpm_runtime_active(struct device *dev);//判断设备当前是否处于suspended状态boolpm_runtime_suspended(struct device *dev);//检查设备现在是否已经空闲,如果空闲的话可以考虑执行 Runtime Suspend。intpm_runtime_idle(struct device *dev);//引用计数减1,但不会触发设备进入idle状态检查。(仅仅只是减1)voidpm_runtime_put_noidle(struct device *dev);//仅当设备当前已经处于active状态,并且引用计数大于0时,才增加引用计数(usage count),否则什么也不做。intpm_runtime_get_if_in_use(struct device *dev);

07

驱动Demo

#include<linux/module.h>#include<linux/kernel.h>#include<linux/fs.h>#include<linux/platform_device.h>#include<linux/pm_runtime.h>#include<linux/slab.h>struct my_device_data {  void __iomem *regs;  int usage_count;};/* 1. 实现运行时休眠回调:关闭硬件时钟/电源 */staticintmy_runtime_suspend(struct device *dev){    pr_info("Runtime PM: Device entering low power state...\n");    // 这里执行具体的硬件操作,例如:    // clk_disable_unprepare(my_clk);    // regulator_disable(my_reg);    return 0;}/* 2. 实现运行时唤醒回调:开启硬件时钟/电源 */staticintmy_runtime_resume(struct device *dev){    pr_info("Runtime PM: Device waking up to active state...\n");    // 这里执行具体的硬件操作,例如:    // clk_prepare_enable(my_clk);    // regulator_enable(my_reg);    return 0;}/* 3. 定义电源管理操作结构体 */static const struct dev_pm_ops my_pm_ops = {    SET_RUNTIME_PM_OPS(my_runtime_suspend, my_runtime_resume, NULL)};/* 模拟硬件操作函数 */staticvoiddo_hardware_work(struct device *dev){    pr_info("Hardware: Doing some real work on registers...\n");}/* 模拟字符设备读操作 */staticssize_tmy_read(struct file *filp, char __user *buf, size_t count, loff_t *pos){    struct device *dev = filp->private_data;    int ret;    // 1. 尝试唤醒设备并增加引用计数    // 如果设备在休眠,这里会阻塞直到 my_runtime_resume 完成    ret = pm_runtime_resume_and_get(dev);    if (ret < 0) {        pr_err("Runtime PM: Failed to resume device\n");        return ret;    }    // 2. 设备已就绪,执行硬件操作    do_hardware_work(dev);    // 3. 释放计数    // 设备不会立即休眠,而是进入 autosuspend 计时    pm_runtime_put_autosuspend(dev);    return 0;}/* 驱动初始化 Probe */staticintmy_probe(struct platform_device *pdev){    struct device *dev = &pdev->dev;    pr_info("Runtime PM Demo: Probing device\n");    // 1. 设置 Autosuspend 延迟时间(如 2000ms)    pm_runtime_set_autosuspend_delay(dev, 2000);    pm_runtime_use_autosuspend(dev);    // 2. 初始化设备状态为 Active 并使能    pm_runtime_set_active(dev);    pm_runtime_enable(dev);    return 0;}/* 驱动卸载 Remove */staticintmy_remove(struct platform_device *pdev){    struct device *dev = &pdev->dev;    // 禁止 Runtime PM,如果此时处于休眠则会执行 resume    pm_runtime_disable(dev);    pm_runtime_set_suspended(dev);    return 0;}static struct platform_driver my_driver = {    .probe = my_probe,    .remove = my_remove,    .driver = {        .name = "my_pm_demo",        .pm = &my_pm_ops,    },};module_platform_driver(my_driver);MODULE_LICENSE("GPL");

END

往期推荐:

Linux通知链

驱动之间如何共享寄存器?Syscon 给出了答案

延迟函数别乱用,Linux 内核开发者必须掌握的延迟“潜规则”

Linux驱动之SPI子系统

一文搞懂SPI总线协议

最新文章

随机文章