Linux电源管理-Runtime PM

前言

1.  什么是Runtime PM?
     Runtime PM (Runtime Power Management)翻译过来就是运行时电源管理。主要的作用是:  每个设备处理好自己的电源管理,在不需要工作时进入低功耗状态。也就是"各人自扫门前雪"。

2.  为什么需要Runtime PM?
    system suspend需要很长时间完成,其中还可能出现失败。比如freeze task的时候。而suspend设备速度相对system suspend快很对,而且还不需要freeze task。当设备不忙的时候就进入自己的低功耗模式,这样一来每个device(包括CPU) 都做好自己的事,整个系统就达到最大节省能源。这时候突然想起了一句话"只要人人都献出一片爱,世界将变成美好的人间"。

3.  为什么需要Runtime PM Framework?
    a.  改变设备的电源状态需要整个平台的支持。
    b.  当设备处于低功耗模式时,wakeup signal常常需要platform或者bus的支持。
    c.  设备驱动不知道什么时候去suspend设备的,也就是驱动是没法判断设备是否处于idle状态,通常需要依赖subsystem,比如bus。
    d.  pm相关的操作都需要顺序执行,这时候就免不了使用workqueue。
    e.  Runtime PM需要和system-wide suspend需要保持兼容。比如system-wide suspend的时候设备已经处于Runtime PM的SUSPENDED状态了,这时候应该怎么处理此设备?
    以上这些工作都需要Runtime PM framework的支持,这也都是Runtime PM framework应该操心的事情。

Subsystem and Driver Callbacks

为了实现设备的runtime pm,需要subsystem(PM domains, device types, classes and bus types)和driver提供下面三个回调函数:
struct dev_pm_ops {
        ....
	int (*runtime_suspend)(struct device *dev);
	int (*runtime_resume)(struct device *dev);
	int (*runtime_idle)(struct device *dev);
};
三个回调函数分别用于suspend device,resume device和idle device。通常Runtime PM framework会在合适的时机调用三个函数。

Device States

Runtime PM Framework使用rpm_status枚举类型表示device的状态
enum rpm_status {
	RPM_ACTIVE = 0,
	RPM_RESUMING,
	RPM_SUSPENDED,
	RPM_SUSPENDING,
};
RPM_ACTIVE:                设备处于正常工作状态,处于全速全进状态。runtime_resume的回调执行完毕。
RPM_RESUMING:          设备状态正在从suspend到active转换。 runtime_resume的回调正在执行。
RPM_SUSPENDED:       设备处于低功耗状态,不能处于IO操作。runtime_suspend的回调执行完毕。
RPM_SUSPENDING:     设备状态正从active到suspend状态转换。runtime_suspend回调正在执行。

是的,你没看错,的确是没有调用runtime_idle回调。那runtime_idle回调什么时候调用呢?  idle状态是suspend状态前的一个过渡而已,通常会在runtime_suspend调用之前调用一段时间runtime_idle回调。

Runtime PM请求类型

因为使设备进入suspend或者resume状态,有同步和异步的方式。通常在异步的时候会用到workqueue,这时候就会用到设备的请求类型。
enum rpm_request {
	RPM_REQ_NONE = 0,
	RPM_REQ_IDLE,
	RPM_REQ_SUSPEND,
	RPM_REQ_AUTOSUSPEND,
	RPM_REQ_RESUME,
};
PRM_REQ_NONE:                        Do nothing
PRM_REQ_IDLE:                           运行设备的runtime_idle回调。 
PRM_REQ_SUSPEND:                 运行设备的runtime_suspend回调。
RPM_REQ_AUTOSUSPEN:          在一段时间之后运行设备的runtime_suspend回调。
RPM_REQ_RESUME:                   运行设备的runtime_resume回调。

Runtime PM数据段

在每个device结构中都存在dev_pm_info的结构,此结构中通过CONFIG_PM_RUNTIME配置字段代码了Runtime PM的信息。
struct dev_pm_info {
        ....
	struct timer_list	suspend_timer;
	unsigned long		timer_expires;
	struct work_struct	work;
	wait_queue_head_t	wait_queue;
	atomic_t		usage_count;
	atomic_t		child_count;
	unsigned int		disable_depth:3;
	unsigned int		idle_notification:1;
	unsigned int		request_pending:1;
	unsigned int		deferred_resume:1;
	unsigned int		run_wake:1;
	unsigned int		runtime_auto:1;
	unsigned int		no_callbacks:1;
	unsigned int		irq_safe:1;
	unsigned int		use_autosuspend:1;
	unsigned int		timer_autosuspends:1;
	unsigned int		memalloc_noio:1;
	enum rpm_request	request;
	enum rpm_status		runtime_status;
	int			runtime_error;
	int			autosuspend_delay;
	unsigned long		last_busy;
	unsigned long		active_jiffies;
	unsigned long		suspended_jiffies;
	unsigned long		accounting_timestamp;

};
.suspend_timer:          休眠时候用到的定时器。
.timer_expires:            定时器的超时函数。
.work:                          用于workqueue中的工作项。
.wait_queue:               等待队列,用于异步pm操作时候使用。
.usage_count:             设备的引用计数,通过该字段判断是否有设备使用。
.child_count:               此设备的"active"子设备的个数。
.disable_depth:           用于禁止Runtime helper function。等于0代表使能,1代表禁止。
.idle_notification:         如果该值被设备,则调用runtime_idle回调函数。
.request_pending:       如果设备,代表工作队列有work请求。
.deferred_resume:      当设备正在执行-> runtime_suspend()的时候,如果->runtime_resume()将要运行,而等待挂起操作完成并不实际,就会设置该值;这里的意思是“一旦你挂起完成,我就开始恢复”。
.run_wake:                  如果设备能产生runtime wake-up events事件,就设置该值。
.runtime_auto:            如果设置,则表示用户空间已允许设备驱动程序通过/sys/devices/.../power/control接口在运行时对该设备进行电源管理。
.no_callbacks:            表明该设备不是有Runtime PM callbacks。
.irq_safe:                    表示->runtime_suspend()和->runtime_resume()回调函数将在持有自旋锁并禁止中断的情况下被调用。
.use_autosuspend:     表示该设备驱动支持延迟自动休眠功能。
.timer_autosuspends: 表明PM核心应该在定时器到期时尝试进行自动休眠(autosuspend),而不是一个常规的挂起(normal suspend)。
.request:                     runtime pm请求类型。
.runtime_status:          runtime pm设备状态。
.runtime_error:            如果该值设备,表明有错误。
.autosuspend_delay:  延迟时间,用于自动休眠。

 Runtime PM运行机制

上面了解了Runtime PM运行时相关的标志之后,可能对runtime已经有了大概的了解,接下来就详细说下runtime的运行机制。
1.  每个设备都维护一个usage_count变量,用于记录该设备的使用情况。当大于0的时候说明该设备在使用,当等于0的时候说明该设备没在使用。
2.  需要使用该设备的时候,设备驱动调用pm_runtime_get/pm_runtime_get_sync接口,增加变量usage_count的值;不再使用该设备的时候,调用pm_runtime_put/pm_runtime_put_sync接口,减小usage_count变量的值。
3.  每次调用get接口的时候,Runtime PM framework会判断该设备的状态。如果该不是active状态,则使用异步(ASYNC)或者同步(SYNC)方式调用runtime_resume回调函数,唤醒设备。
4.  每次调用put接口的时候,Runtime PM framewokr会判断设备的引用计数,如果为零,则使用异步(ASYNC)或者同步(SYNC)方式调用runtime_idle回调函数。
5.  为了防止频繁suspend,在suspend前面引入了idle状态。当设备处于idle状态之后,会在合适的时间调用suspend回调函数。通常都会通过runtime pm helper function启动一个timer,设置超时时间,在超时之后调用runtime_suspend回调函数。

Runtime PM回调约束

1.  回调是互斥的(例如:  对于同一个设备,禁止并行执行runtime_suspend和runtime_resume或者同一个设备runtime_suspend回调)。不过例外情况是:runtime_suspend()或runtime_resume()可以和runtime_idle()并行执行。
2.  runtime_idle()和runtime_suspend回调只能对"active"设备执行。
3.  runtime_idle和runtime_suspend回调只能对其引用计数(usage count)等于零,且器active children个数是零或者“power.ignore_children”标志被设置的设备执行。
4.  runtime_resume只能对挂起(suspend)的设备执行。
5.  如果runtime suspend()即将被执行,或者有一个挂起的请求执行,runtime idle()将不会在同一个设备上执行。
6.  如果runtime_suspend回调已经执行或者已经在pending状态,则取消该设备的runtime idle请求。
7.  如果runtime_resume回调已经执行,其他callbacks则将不被执行对于同一个设备。
8.  如果该设备下的任何一个子设备都处于idle,parent设备才可以idle。
9.  如果parent设备下任何一个设备处于active状态,parent设备必须active。
10. parent设备下任何一个设备处于idle,需要上报给parent用于记录。

Runtime Sys接口

关于runtime sys接口在文件:  /kernel/drivers/base/power/sysfs.c中描述。设备的runtime属性是在dpm_sysfs_add函数中增加的。
	if (pm_runtime_callbacks_present(dev)) {
		rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
		if (rc)
			goto err_out;
	}
runtime的属性如下:
static struct attribute *runtime_attrs[] = {
#ifdef CONFIG_PM_RUNTIME
#ifndef CONFIG_PM_ADVANCED_DEBUG
	&dev_attr_runtime_status.attr,
#endif
	&dev_attr_control.attr,
	&dev_attr_runtime_suspended_time.attr,
	&dev_attr_runtime_active_time.attr,
	&dev_attr_autosuspend_delay_ms.attr,
#endif /* CONFIG_PM_RUNTIME */
	NULL,
};
其中有五个属性。分别为control,  runtime_susupend_time,  runtime_active_time,  autosuspend_delay_ms,runtime_status属性。

/sys/devices/.../power/control
        on    -   调用pm_runtime_forbid接口,增加设备的引用计数,然后resume设备。
        auto -   调用pm_runtime_allow接口,减少设备的引用计数,如果设备的引用计数为0,则idle设备。

/sys/devices/.../power/runtime_status
      active - 设备的状态是正常工作状态。
      suspend- 设备的状态是低功耗模式。
      suspending-设备的状态正在从active->suspend转化。
      resuming-设备的状态正在从suspend->active转化。
      error-设备runtime出现错误,此时runtime_error的标志置位。
      unsupported-设备的runtime 没有使能,此时disable_depth标志置位。

/sys/devices/.../power/runtime_suspend_time
      设备在suspend状态的时间

/sys/devices/.../power/runtime_active_time
      设备在active状态的时间  

/sys/devices/.../power/autosuspend_delay_ms
     设备在idle状态多久之后suspend,设置延迟suspend的延迟时间。

Runtime API

因为Runtime API多达几十个以上,这里列举一些驱动中常用的API供大家参考。
  • pm_runtime_enable(使能设备的runtime pm)
void pm_runtime_enable(struct device *dev)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->power.lock, flags);

	if (dev->power.disable_depth > 0)
		dev->power.disable_depth--;
	else
		dev_warn(dev, "Unbalanced %s!\n", __func__);

	spin_unlock_irqrestore(&dev->power.lock, flags);
}
disable_depth在pm_runtime_init会被初始化为1,enabel函数就是将此值减去1而已。当然了在disable函数中会给该值加1。
  • pm_runtime_get/pm_runtime_put(异步请求增加/减少引用计数)
  • pm_runtime_get_sync/pm_runtime_put_sync(同步请求增加/减少引用计数)
  • pm_runtime_set_active/pm_runtime_set_suspended(设置设备的runtime运行状态)
  • pm_schedule_suspend(在指定时间之后suspend)
以上函数接口都比较简单,最终会调用到__pm_runtime_resume/__pm_runtime_suspend/__pm_runtime_idle接口中。
  • __pm_runtime_resume(resume设备)

__pm_runtime_idle/__pm_runtime_suspend函数不在这里分析了,大概流程和resume流程相似。

Runtime PM举例

写了一个简单的测试runtime测试例子,如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/pm_runtime.h>


static int runtime_pm_probe(struct platform_device *pdev)
{
	printk(KERN_EMERG "runtime_pm: runtime_pm_probe!\n");
	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);	
	return 0;
}

static int runtime_pm_remove(struct platform_device *pdev)
{
	printk(KERN_EMERG "runtime_pm: runtime_pm_remove!\n");
	pm_runtime_disable(&pdev->dev);
	return 0;	
}

static int runtime_pm_suspend(struct device *dev)
{
	printk(KERN_EMERG "runtime_pm: runtime_pm_suspend!\n");
	return 0;
}

static int runtime_pm_resume(struct device *dev)
{
	printk(KERN_EMERG "runtime_pm: runtime_pm_resume!\n");
	return 0;
}

static int runtime_pm_idle(struct device *dev)
{
	printk(KERN_EMERG "runtime_pm: runtime_pm_idle\n");
	return 0;
}


static const struct dev_pm_ops runtime_pm_ops = {
	SET_RUNTIME_PM_OPS(runtime_pm_suspend,
					runtime_pm_resume,
					runtime_pm_idle)
};

static void runtime_pm_release(struct device * dev)
{
}

static struct platform_device runtime_device = {
	.name		= "runtime_device",
	.id         = -1,
	.dev        = {
	        .release = runtime_pm_release,
	},
};

static struct platform_driver runtime_driver = {
	.probe		= runtime_pm_probe,
	.remove		= runtime_pm_remove,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "runtime_device",
		.pm	= &runtime_pm_ops,
	},
};

static int runtime_pm_init(void)
{	
	printk(KERN_EMERG "runtime_pm: runtime_pm_init\n");
    platform_device_register(&runtime_device);
	platform_driver_register(&runtime_driver);
	return 0;
}

static void runtime_pm_exit(void)
{
	printk(KERN_EMERG "runtime_pm: runtime_pm_exit\n");

	platform_driver_unregister(&runtime_driver);
	platform_device_unregister(&runtime_device);
}

module_init(runtime_pm_init);
module_exit(runtime_pm_exit);
MODULE_LICENSE("GPL");
如下是测试结果:
1.  查看当前设备的runtime状态
cat /sys/devices/platform/runtime_device/power/runtime_status
suspend
2. 查看设备的runtime_suspend时间
cat /sys/devices/platform/runtime_device/power/runtime_suspended_time
341028
3. 使设备处于active状态
echo on >  /sys/devices/platform/runtime_device/power/control
4. 使设备进入suspend状态
echo auto > /sys/devices/platform/runtime_device/power/control
5. 查看转换状态的打印
test:/ # dmesg | grep "runtime"                                     
[  451.432602] c7 runtime_pm: runtime_pm_resume!
[  509.842328] c5 runtime_pm: runtime_pm_idle
[  509.846430] c5 runtime_pm: runtime_pm_suspend!




发布了162 篇原创文章 · 获赞 93 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/longwang155069/article/details/53080667