runtime pm的实例分析

2024-05-15 15:28
文章标签 分析 实例 runtime pm

本文主要是介绍runtime pm的实例分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概念

运行时的PM与前文描述的系统级挂起到RAM时候的PM不太一样,它是针对单个设备,指系统在非睡眠状态的情况下,某个设备在空闲时可以进入运行时挂起状态,而在不是空闲时执行运行时恢复使得设备进入正常工作状态,如此,这个设备在运行时会省电

struct dev_pm_ops {
...
int (*runtime_suspend)(struct device *dev);
int (*runtime_resume)(struct device *dev);
int (*runtime_idle)(struct device *dev);
...
};

基本框架简化如下,就是驱动通过runtime pm core提供的接口,再调用到驱动本身的实现

 

spi_pump_messages

通过之前的文章简述spi内核框架,我们可以知道,这个文件drivers/spi/spi.c里的spi_pump_messages就是一次传输行为,总线空闲就会调用unprepare_transfer_hardware,总线忙就会调用prepare_transfer_hardware;soc根据这两个行为就可以去实现pm操作来省电

static void spi_pump_messages(struct kthread_work *work)
{struct spi_master *master =container_of(work, struct spi_master, pump_messages);unsigned long flags;bool was_busy = false;int ret;/* Lock queue and check for queue work */spin_lock_irqsave(&master->queue_lock, flags);if (list_empty(&master->queue) || !master->running) {if (!master->busy) {spin_unlock_irqrestore(&master->queue_lock, flags);return;}master->busy = false;spin_unlock_irqrestore(&master->queue_lock, flags);if (master->unprepare_transfer_hardware &&master->unprepare_transfer_hardware(master))dev_err(&master->dev,"failed to unprepare transfer hardware\n");return;}/* Make sure we are not already running a message */if (master->cur_msg) {spin_unlock_irqrestore(&master->queue_lock, flags);return;}/* Extract head of queue */master->cur_msg =list_entry(master->queue.next, struct spi_message, queue);list_del_init(&master->cur_msg->queue);if (master->busy)was_busy = true;elsemaster->busy = true;spin_unlock_irqrestore(&master->queue_lock, flags);if (!was_busy && master->prepare_transfer_hardware) {ret = master->prepare_transfer_hardware(master);if (ret) {dev_err(&master->dev,"failed to prepare transfer hardware\n");return;}}ret = master->transfer_one_message(master, master->cur_msg);if (ret) {dev_err(&master->dev,"failed to transfer one message from queue\n");return;}
}

unprepare_transfer_hardware

比如在drivers/spi/spi_qsd.c中,unprepare_transfer_hardware接口会去通过pm_runtime_put_autosuspend,去调用pm_dev_ops的runtime_suspend成员

pm_runtime_put_autosuspend__pm_runtime_suspendrpm_suspendcallback = RPM_GET_CALLBACK(dev, runtime_suspend);
static int msm_spi_unprepare_transfer_hardware(struct spi_master *master)
{struct msm_spi  *dd = spi_master_get_devdata(master);pm_runtime_mark_last_busy(dd->dev);pm_runtime_put_autosuspend(dd->dev);return 0;
}

prepare_transfer_hardware

比如在drivers/spi/spi_qsd.c中,prepare_transfer_hardware接口会去通过pm_runtime_get_sync,去调用pm_dev_ops的runtime_resume成员

pm_runtime_get_sync__pm_runtime_resumerpm_resumecallback = RPM_GET_CALLBACK(dev, runtime_resume);rpm_callback(callback, dev);
static int msm_spi_prepare_transfer_hardware(struct spi_master *master)
{struct msm_spi  *dd = spi_master_get_devdata(master);int resume_state = 0;resume_state = pm_runtime_get_sync(dd->dev);if (resume_state < 0)return resume_state;/** Counter-part of system-suspend when runtime-pm is not enabled.* This way, resume can be left empty and device will be put in* active mode only if client requests anything on the bus*/if (!pm_runtime_enabled(dd->dev))resume_state = msm_spi_pm_resume_runtime(dd->dev);if (resume_state < 0)return resume_state;if (dd->suspended)return -EBUSY;return 0;
}

runtime_suspend

runtime_suspend成员的实现如下:主要是去关闭spi控制器的时钟,达到省电的目的

static int msm_spi_pm_suspend_runtime(struct device *device)
{struct platform_device *pdev = to_platform_device(device);struct spi_master *master = platform_get_drvdata(pdev);struct msm_spi    *dd;unsigned long      flags;dev_err(device, "pm_runtime: suspending...\n");if (!master)goto suspend_exit;dd = spi_master_get_devdata(master);if (!dd)goto suspend_exit;if (dd->suspended)return 0;/** Make sure nothing is added to the queue while we're* suspending*/spin_lock_irqsave(&dd->queue_lock, flags);dd->suspended = 1;spin_unlock_irqrestore(&dd->queue_lock, flags);/* Wait for transactions to end, or time out */wait_event_interruptible(dd->continue_suspend,!dd->transfer_pending);if (dd->pdata && !dd->pdata->active_only)msm_spi_clk_path_unvote(dd);suspend_exit:return 0;
}

runtime_resume

runtime_resume成员的实现如下,主要是去开启spi控制的时钟

static int msm_spi_pm_resume_runtime(struct device *device)
{struct platform_device *pdev = to_platform_device(device);struct spi_master *master = platform_get_drvdata(pdev);struct msm_spi    *dd;int               ret = 0;dev_err(device, "pm_runtime: resuming...\n");if (!master)goto resume_exit;dd = spi_master_get_devdata(master);if (!dd)goto resume_exit;if (!dd->suspended)return 0;if (!dd->is_init_complete) {ret = init_resources(pdev);if (ret != 0)return ret;elsedd->is_init_complete = true;}msm_spi_clk_path_init(dd);if (!dd->pdata->active_only)msm_spi_clk_path_vote(dd);dd->suspended = 0;resume_exit:return 0;
}

dev_pm_ops

将实现赋值给dev_pm_ops结构里的runtime_suspend和runtime_resume成员

static const struct dev_pm_ops msm_spi_dev_pm_ops = {SET_SYSTEM_SLEEP_PM_OPS(msm_spi_suspend, msm_spi_resume)SET_RUNTIME_PM_OPS(msm_spi_pm_suspend_runtime,msm_spi_pm_resume_runtime, NULL)
};static struct platform_driver msm_spi_driver = {.driver         = {.name   = SPI_DRV_NAME,.owner  = THIS_MODULE,.pm             = &msm_spi_dev_pm_ops,.of_match_table = msm_spi_dt_match,},.remove         = msm_spi_remove,.probe          = msm_spi_probe,
};

pm_runtime_get/put

在/drivers/base/power/runtime.c中,设备驱动调用的pm_runtime_get/put 会先原子操作usage,然后判断是否调用rpm_idle/rpm_suspend/rpm_resume;他们会来执行设备驱动实现的对应操作;suspend_timer用定时器来延时调用rpm_suspend

void pm_runtime_init(struct device *dev)
{dev->power.runtime_status = RPM_SUSPENDED;dev->power.last_status = RPM_INVALID;dev->power.idle_notification = false;dev->power.disable_depth = 1;atomic_set(&dev->power.usage_count, 0);dev->power.runtime_error = 0;atomic_set(&dev->power.child_count, 0);pm_suspend_ignore_children(dev, false);dev->power.runtime_auto = true;dev->power.request_pending = false;dev->power.request = RPM_REQ_NONE;dev->power.deferred_resume = false;dev->power.needs_force_resume = 0;INIT_WORK(&dev->power.work, pm_runtime_work);dev->power.timer_expires = 0;hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);dev->power.suspend_timer.function = pm_suspend_timer_fn;init_waitqueue_head(&dev->power.wait_queue);
}static void pm_runtime_work(struct work_struct *work)
{struct device *dev = container_of(work, struct device, power.work);enum rpm_request req;spin_lock_irq(&dev->power.lock);if (!dev->power.request_pending)goto out;req = dev->power.request;dev->power.request = RPM_REQ_NONE;dev->power.request_pending = false;switch (req) {case RPM_REQ_NONE:break;case RPM_REQ_IDLE:rpm_idle(dev, RPM_NOWAIT);break;case RPM_REQ_SUSPEND:rpm_suspend(dev, RPM_NOWAIT);break;case RPM_REQ_AUTOSUSPEND:rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);break;case RPM_REQ_RESUME:rpm_resume(dev, RPM_NOWAIT);break;}out:spin_unlock_irq(&dev->power.lock);
}

这篇关于runtime pm的实例分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/992199

相关文章

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

MySQL的索引失效的原因实例及解决方案

《MySQL的索引失效的原因实例及解决方案》这篇文章主要讨论了MySQL索引失效的常见原因及其解决方案,它涵盖了数据类型不匹配、隐式转换、函数或表达式、范围查询、LIKE查询、OR条件、全表扫描、索引... 目录1. 数据类型不匹配2. 隐式转换3. 函数或表达式4. 范围查询之后的列5. like 查询6

python-nmap实现python利用nmap进行扫描分析

《python-nmap实现python利用nmap进行扫描分析》Nmap是一个非常用的网络/端口扫描工具,如果想将nmap集成进你的工具里,可以使用python-nmap这个python库,它提供了... 目录前言python-nmap的基本使用PortScanner扫描PortScannerAsync异

Oracle数据库执行计划的查看与分析技巧

《Oracle数据库执行计划的查看与分析技巧》在Oracle数据库中,执行计划能够帮助我们深入了解SQL语句在数据库内部的执行细节,进而优化查询性能、提升系统效率,执行计划是Oracle数据库优化器为... 目录一、什么是执行计划二、查看执行计划的方法(一)使用 EXPLAIN PLAN 命令(二)通过 S

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类