蜗窝科技

linux thermal framework(5)_thermal core

huowj · · 电源管理子系统 · 阅读 4176 · 2025年4月存档

1. 介绍

本文从thermal framework core对内部实现做一个简单的分析

2. 提供给用户空间的接口

thermal framework通过sysfs和debugfs向上提供接口,接口分为两类,一类是thermal zone,一类是cooling device,sysfs的接口如下:

  1. “thermal"目录

    由于发热的设备以及降温的设备都是抽象出来的,并不是一个具体的物理设备,也没有compatible的driver和设备probe,因此linux使用class来描述这一类thermal设备,thermal class中包含了所有向thermal_core注册的thermal_zone和cooling_device的子目录,并按照id区分

    /sys/class/thermal/
    cooling_device0 cooling_device3 thermal_zone1 thermal_zone12 thermal_zone15 thermal_zone2 thermal_zone5 thermal_zone8 cooling_device1 cooling_device4 thermal_zone10 thermal_zone13 thermal_zone16 thermal_zone3 thermal_zone6 thermal_zone9 cooling_device2 thermal_zone0 thermal_zone11 thermal_zone14 thermal_zone17 thermal_zone4 thermal_zone7

  2. “thermal_zone"目录

    每个thermal zone目录都包含以下节点

    /sys/class/thermal/available_policies
    |– available_policies
    |– temp
    |– type
    |– power
    |– autosuspend_delay_ms
    |– control
    |– runtime_active_time
    |– runtime_status
    `– runtime_suspended_time
    |– policy
    |– subsystem -> ../../../../class/thermal
    |– sustainable_power
    |– k_po
    |– k_pu
    |– k_i
    |– k_d
    |– integral_cutoff
    |– slope
    |– offset

    1)available_policies,支持的所有governors的名字

    2)temp,现在的温度

    3)type,thermal zone的类型

    4)policy,当前使用的governor名字

    5)sustainable_power,这个thermal zone可以消散的可持续热量

    6)k_d, k_po, k_i, k_pu, 是PID算法的相关参数

    7)slope, 线性温度调节曲线的斜率

    8)offset, 线性温度调节曲线的偏移

  3. “cooling device"目录

    每个cooling device目录都包含以下节点

    /sys/class/thermal/cooling_device0
    |– cur_state
    |– max_state
    |– power
    |– autosuspend_delay_ms
    |– control
    |– runtime_active_time
    |– runtime_status
    -- runtime\_suspended\_time |-- stats |-- reset |-- time\_in\_state\_ms |-- total\_trans – trans_table
    |– subsystem -> ../../../../class/thermal
    |– type
    |– uevent

    1)cur_state,这个cooling device当前state

    2)max_state,这个cooling device支持的最大state

    4)stats-time_in_state_ms,在每个state持续的时间

    5)total_trans,状态切换的总次数

    6)trans_table,二位表格,表示从某一个状态转换到另一个状态的次数

    7)type,这个cooling device的类型

3. thermal core的初始化

thermal core是以postcore在系统初始化的时候注册进整个系统的,初始化的时候会运行thermal_init函数,我们来看看这个函数具体做了什么:

static int __init thermal_init(void)

{

	int result;

thermal_debug_init();



result = thermal_netlink_init();

if (result)

	goto error;



result = thermal_register_governors();

if (result)

	goto unregister_netlink;



thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);

if (!thermal_class) {

	result = -ENOMEM;

	goto unregister_governors;

}



thermal_class->name = "thermal";

thermal_class->dev_release = thermal_release;



result = class_register(thermal_class);

if (result) {

kfree(thermal_class);

thermal_class = NULL;

goto unregister_governors;

}

result = register_pm_notifier(&thermal_pm_nb);

if (result)

	pr_warn("Thermal: Can not register suspend notifier, return %d\n",

		result);



return 0;



unregister_governors:



thermal_unregister_governors();



unregister_netlink:



thermal_netlink_exit();



error:



mutex_destroy(&thermal_list_lock);



mutex_destroy(&thermal_governor_lock);



return result;



}



postcore_initcall(thermal_init);

1)thermal_debug_init来初始化debugfs
2)thermal_netlink_init,初始化thermal netlink,有thermal事件可以通知用户态进程
3)thermal_register_governors 会遍历所有governor_thermal_table的所有entry,即遍历所有静态定义的governors,调用thermal_register_governor函数将这个governor注册进thermal core中,具体流程是,将governor添加进governor list中,然后遍历所有的thermal zone,如果该governor和这个thermal zone的governor名字一致的话就将thermal zone的governor设置为这个governor
4)thermal_class linux通过class来作为一类设备的集合,thermal_class就是所有thermal zone和cooling device这类设备的集合,thermal_init会初始化thermal_class并将其注册进系统中
5)register_pm_notifier 将thermal的通知链加进pm_chain中

标签:thermal

继续讨论

新讨论请用 GitHub 账号在下方发言——评论会保存在本仓库的 Discussions 里,与站点同寿; 再下方的「历史评论」是原站的只读存档。