Linux thermal framework

thermal作用

Linux Thermal 是 Linux 系统下温度控制相关的模块,主要用来控制系统运行过程中芯片产生的热量,使芯片温度和设备外壳温度维持在一个安全、舒适的范围。

那下面我们就来一起看看对于温度控制这样一个需求,Linux 内核是怎么实现的。

概念

  • thermal zone
    温度控制区域。

  • sensor
    获取温度。

  • trip points
    温度跳变点,或者是温度阈值。

  • cooling device
    Thermal Cooling Device 是可以降温设备的抽象,能降温的设备比如风扇,这些好理解,但是像CPU、GPU 这些 Cooling devices 怎么理解呢?
    其实降温可以从两方面来理解,一个是加快散热,另外一个就是降低产热量。风扇,散热片这些是用来加快散热,CPU、GPU 这些 Cooling devices 是通过降低产热来降温。

  • governor
    温度控制的策略。

    • step wise
      节流逻辑:使用趋势来节流。*如果热区域正在“升温”,这将通过一个步骤来限制所有与该区域及其特定跳变点相关的冷却设备。如果该区域正在“冷却”,它会将设备的性能提高一步。

    • user space
      给用户侧发送一个事件,由用户侧来处理

    • gov_bang_bang
      控制风扇的一种简单策略,当超过温度T0时开启风扇,当温度低于T1时关闭风扇,T0 > T1。

    • power_allocator
      使用PID控制算法来精细控制温度变化。

framework

软件分层

  • core层
    governor、cooling device、thermal zone等的核心接口。

  • governor层
    不同governor实现。

  • driver层
    不同硬件平台的驱动实现。
    thermal zone的注册在thermal_init中完成,这要比thermal driver早完成。也正是因为此,才可以在thermal driver中将thermal sensor和thermal zone绑定。这样每个thermal zone就有对应的thermal sensor操作函数,可以读取温度值。thermal_init是fs_initcall,而hisi_thermal_driver是module_init。
    thermal驱动主要获取内存映射、中断资源、时钟信息等,注册中断处理函数,并且添加thermal sensor到thermal zone。中断处理线程函数会更新thermal的温度,同时每个thermal zone都有work queue去轮询读取温度。

主要结构体的关系

运行框图

  • 初始化

  • 温度监控流程图

在platform的driver调用thermal_zone_device_register接口后会启动一个work queue,定期的获取温度来查看是否异常,温度分为critical和non-critical两种情况,critical的时候会处理关机和降温,non-critical会执行一些其他监控,最后会重新添加work再次监控。

文件节点

thermal_of_cooling_device_register
/sys/class/thermal/ folder as cooling_device[0-*]

thermal_zone_device_register
/sys/class/thermal folder as thermal_zone[0-*].

文件

文件名 作用
drivers/thermal/thermal_sysfs.c 文件节点
drivers/thermal/thermal_core.c governor: 注册、去注册、绑定
zone:配置监测周期、各trips处理(critical、non-critical)
power actor:功率系数,设置获取cooling设备的最大/最小功率
device:cooling/zones device,注册、去注册、绑定、去绑定等api接口
include/linux/thermal.h 对外接口头文件
posted @ 2022-03-14 16:51  zephyr~  阅读(847)  评论(0编辑  收藏  举报