sysfs-api.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. ===================================
  2. Generic Thermal Sysfs driver How To
  3. ===================================
  4. Written by Sujith Thomas <[email protected]>, Zhang Rui <[email protected]>
  5. Updated: 2 January 2008
  6. Copyright (c) 2008 Intel Corporation
  7. 0. Introduction
  8. ===============
  9. The generic thermal sysfs provides a set of interfaces for thermal zone
  10. devices (sensors) and thermal cooling devices (fan, processor...) to register
  11. with the thermal management solution and to be a part of it.
  12. This how-to focuses on enabling new thermal zone and cooling devices to
  13. participate in thermal management.
  14. This solution is platform independent and any type of thermal zone devices
  15. and cooling devices should be able to make use of the infrastructure.
  16. The main task of the thermal sysfs driver is to expose thermal zone attributes
  17. as well as cooling device attributes to the user space.
  18. An intelligent thermal management application can make decisions based on
  19. inputs from thermal zone attributes (the current temperature and trip point
  20. temperature) and throttle appropriate devices.
  21. - `[0-*]` denotes any positive number starting from 0
  22. - `[1-*]` denotes any positive number starting from 1
  23. 1. thermal sysfs driver interface functions
  24. ===========================================
  25. 1.1 thermal zone device interface
  26. ---------------------------------
  27. ::
  28. struct thermal_zone_device
  29. *thermal_zone_device_register(char *type,
  30. int trips, int mask, void *devdata,
  31. struct thermal_zone_device_ops *ops,
  32. const struct thermal_zone_params *tzp,
  33. int passive_delay, int polling_delay))
  34. This interface function adds a new thermal zone device (sensor) to
  35. /sys/class/thermal folder as `thermal_zone[0-*]`. It tries to bind all the
  36. thermal cooling devices registered at the same time.
  37. type:
  38. the thermal zone type.
  39. trips:
  40. the total number of trip points this thermal zone supports.
  41. mask:
  42. Bit string: If 'n'th bit is set, then trip point 'n' is writable.
  43. devdata:
  44. device private data
  45. ops:
  46. thermal zone device call-backs.
  47. .bind:
  48. bind the thermal zone device with a thermal cooling device.
  49. .unbind:
  50. unbind the thermal zone device with a thermal cooling device.
  51. .get_temp:
  52. get the current temperature of the thermal zone.
  53. .set_trips:
  54. set the trip points window. Whenever the current temperature
  55. is updated, the trip points immediately below and above the
  56. current temperature are found.
  57. .get_mode:
  58. get the current mode (enabled/disabled) of the thermal zone.
  59. - "enabled" means the kernel thermal management is
  60. enabled.
  61. - "disabled" will prevent kernel thermal driver action
  62. upon trip points so that user applications can take
  63. charge of thermal management.
  64. .set_mode:
  65. set the mode (enabled/disabled) of the thermal zone.
  66. .get_trip_type:
  67. get the type of certain trip point.
  68. .get_trip_temp:
  69. get the temperature above which the certain trip point
  70. will be fired.
  71. .set_emul_temp:
  72. set the emulation temperature which helps in debugging
  73. different threshold temperature points.
  74. tzp:
  75. thermal zone platform parameters.
  76. passive_delay:
  77. number of milliseconds to wait between polls when
  78. performing passive cooling.
  79. polling_delay:
  80. number of milliseconds to wait between polls when checking
  81. whether trip points have been crossed (0 for interrupt driven systems).
  82. ::
  83. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  84. This interface function removes the thermal zone device.
  85. It deletes the corresponding entry from /sys/class/thermal folder and
  86. unbinds all the thermal cooling devices it uses.
  87. ::
  88. struct thermal_zone_device
  89. *thermal_zone_of_sensor_register(struct device *dev, int sensor_id,
  90. void *data,
  91. const struct thermal_zone_of_device_ops *ops)
  92. This interface adds a new sensor to a DT thermal zone.
  93. This function will search the list of thermal zones described in
  94. device tree and look for the zone that refer to the sensor device
  95. pointed by dev->of_node as temperature providers. For the zone
  96. pointing to the sensor node, the sensor will be added to the DT
  97. thermal zone device.
  98. The parameters for this interface are:
  99. dev:
  100. Device node of sensor containing valid node pointer in
  101. dev->of_node.
  102. sensor_id:
  103. a sensor identifier, in case the sensor IP has more
  104. than one sensors
  105. data:
  106. a private pointer (owned by the caller) that will be
  107. passed back, when a temperature reading is needed.
  108. ops:
  109. `struct thermal_zone_of_device_ops *`.
  110. ============== =======================================
  111. get_temp a pointer to a function that reads the
  112. sensor temperature. This is mandatory
  113. callback provided by sensor driver.
  114. set_trips a pointer to a function that sets a
  115. temperature window. When this window is
  116. left the driver must inform the thermal
  117. core via thermal_zone_device_update.
  118. get_trend a pointer to a function that reads the
  119. sensor temperature trend.
  120. set_emul_temp a pointer to a function that sets
  121. sensor emulated temperature.
  122. ============== =======================================
  123. The thermal zone temperature is provided by the get_temp() function
  124. pointer of thermal_zone_of_device_ops. When called, it will
  125. have the private pointer @data back.
  126. It returns error pointer if fails otherwise valid thermal zone device
  127. handle. Caller should check the return handle with IS_ERR() for finding
  128. whether success or not.
  129. ::
  130. void thermal_zone_of_sensor_unregister(struct device *dev,
  131. struct thermal_zone_device *tzd)
  132. This interface unregisters a sensor from a DT thermal zone which was
  133. successfully added by interface thermal_zone_of_sensor_register().
  134. This function removes the sensor callbacks and private data from the
  135. thermal zone device registered with thermal_zone_of_sensor_register()
  136. interface. It will also silent the zone by remove the .get_temp() and
  137. get_trend() thermal zone device callbacks.
  138. ::
  139. struct thermal_zone_device
  140. *devm_thermal_zone_of_sensor_register(struct device *dev,
  141. int sensor_id,
  142. void *data,
  143. const struct thermal_zone_of_device_ops *ops)
  144. This interface is resource managed version of
  145. thermal_zone_of_sensor_register().
  146. All details of thermal_zone_of_sensor_register() described in
  147. section 1.1.3 is applicable here.
  148. The benefit of using this interface to register sensor is that it
  149. is not require to explicitly call thermal_zone_of_sensor_unregister()
  150. in error path or during driver unbinding as this is done by driver
  151. resource manager.
  152. ::
  153. void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  154. struct thermal_zone_device *tzd)
  155. This interface is resource managed version of
  156. thermal_zone_of_sensor_unregister().
  157. All details of thermal_zone_of_sensor_unregister() described in
  158. section 1.1.4 is applicable here.
  159. Normally this function will not need to be called and the resource
  160. management code will ensure that the resource is freed.
  161. ::
  162. int thermal_zone_get_slope(struct thermal_zone_device *tz)
  163. This interface is used to read the slope attribute value
  164. for the thermal zone device, which might be useful for platform
  165. drivers for temperature calculations.
  166. ::
  167. int thermal_zone_get_offset(struct thermal_zone_device *tz)
  168. This interface is used to read the offset attribute value
  169. for the thermal zone device, which might be useful for platform
  170. drivers for temperature calculations.
  171. 1.2 thermal cooling device interface
  172. ------------------------------------
  173. ::
  174. struct thermal_cooling_device
  175. *thermal_cooling_device_register(char *name,
  176. void *devdata, struct thermal_cooling_device_ops *)
  177. This interface function adds a new thermal cooling device (fan/processor/...)
  178. to /sys/class/thermal/ folder as `cooling_device[0-*]`. It tries to bind itself
  179. to all the thermal zone devices registered at the same time.
  180. name:
  181. the cooling device name.
  182. devdata:
  183. device private data.
  184. ops:
  185. thermal cooling devices call-backs.
  186. .get_max_state:
  187. get the Maximum throttle state of the cooling device.
  188. .get_cur_state:
  189. get the Currently requested throttle state of the
  190. cooling device.
  191. .set_cur_state:
  192. set the Current throttle state of the cooling device.
  193. ::
  194. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  195. This interface function removes the thermal cooling device.
  196. It deletes the corresponding entry from /sys/class/thermal folder and
  197. unbinds itself from all the thermal zone devices using it.
  198. 1.3 interface for binding a thermal zone device with a thermal cooling device
  199. -----------------------------------------------------------------------------
  200. ::
  201. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  202. int trip, struct thermal_cooling_device *cdev,
  203. unsigned long upper, unsigned long lower, unsigned int weight);
  204. This interface function binds a thermal cooling device to a particular trip
  205. point of a thermal zone device.
  206. This function is usually called in the thermal zone device .bind callback.
  207. tz:
  208. the thermal zone device
  209. cdev:
  210. thermal cooling device
  211. trip:
  212. indicates which trip point in this thermal zone the cooling device
  213. is associated with.
  214. upper:
  215. the Maximum cooling state for this trip point.
  216. THERMAL_NO_LIMIT means no upper limit,
  217. and the cooling device can be in max_state.
  218. lower:
  219. the Minimum cooling state can be used for this trip point.
  220. THERMAL_NO_LIMIT means no lower limit,
  221. and the cooling device can be in cooling state 0.
  222. weight:
  223. the influence of this cooling device in this thermal
  224. zone. See 1.4.1 below for more information.
  225. ::
  226. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  227. int trip, struct thermal_cooling_device *cdev);
  228. This interface function unbinds a thermal cooling device from a particular
  229. trip point of a thermal zone device. This function is usually called in
  230. the thermal zone device .unbind callback.
  231. tz:
  232. the thermal zone device
  233. cdev:
  234. thermal cooling device
  235. trip:
  236. indicates which trip point in this thermal zone the cooling device
  237. is associated with.
  238. 1.4 Thermal Zone Parameters
  239. ---------------------------
  240. ::
  241. struct thermal_bind_params
  242. This structure defines the following parameters that are used to bind
  243. a zone with a cooling device for a particular trip point.
  244. .cdev:
  245. The cooling device pointer
  246. .weight:
  247. The 'influence' of a particular cooling device on this
  248. zone. This is relative to the rest of the cooling
  249. devices. For example, if all cooling devices have a
  250. weight of 1, then they all contribute the same. You can
  251. use percentages if you want, but it's not mandatory. A
  252. weight of 0 means that this cooling device doesn't
  253. contribute to the cooling of this zone unless all cooling
  254. devices have a weight of 0. If all weights are 0, then
  255. they all contribute the same.
  256. .trip_mask:
  257. This is a bit mask that gives the binding relation between
  258. this thermal zone and cdev, for a particular trip point.
  259. If nth bit is set, then the cdev and thermal zone are bound
  260. for trip point n.
  261. .binding_limits:
  262. This is an array of cooling state limits. Must have
  263. exactly 2 * thermal_zone.number_of_trip_points. It is an
  264. array consisting of tuples <lower-state upper-state> of
  265. state limits. Each trip will be associated with one state
  266. limit tuple when binding. A NULL pointer means
  267. <THERMAL_NO_LIMITS THERMAL_NO_LIMITS> on all trips.
  268. These limits are used when binding a cdev to a trip point.
  269. .match:
  270. This call back returns success(0) if the 'tz and cdev' need to
  271. be bound, as per platform data.
  272. ::
  273. struct thermal_zone_params
  274. This structure defines the platform level parameters for a thermal zone.
  275. This data, for each thermal zone should come from the platform layer.
  276. This is an optional feature where some platforms can choose not to
  277. provide this data.
  278. .governor_name:
  279. Name of the thermal governor used for this zone
  280. .no_hwmon:
  281. a boolean to indicate if the thermal to hwmon sysfs interface
  282. is required. when no_hwmon == false, a hwmon sysfs interface
  283. will be created. when no_hwmon == true, nothing will be done.
  284. In case the thermal_zone_params is NULL, the hwmon interface
  285. will be created (for backward compatibility).
  286. .num_tbps:
  287. Number of thermal_bind_params entries for this zone
  288. .tbp:
  289. thermal_bind_params entries
  290. 2. sysfs attributes structure
  291. =============================
  292. == ================
  293. RO read only value
  294. WO write only value
  295. RW read/write value
  296. == ================
  297. Thermal sysfs attributes will be represented under /sys/class/thermal.
  298. Hwmon sysfs I/F extension is also available under /sys/class/hwmon
  299. if hwmon is compiled in or built as a module.
  300. Thermal zone device sys I/F, created once it's registered::
  301. /sys/class/thermal/thermal_zone[0-*]:
  302. |---type: Type of the thermal zone
  303. |---temp: Current temperature
  304. |---mode: Working mode of the thermal zone
  305. |---policy: Thermal governor used for this zone
  306. |---available_policies: Available thermal governors for this zone
  307. |---trip_point_[0-*]_temp: Trip point temperature
  308. |---trip_point_[0-*]_type: Trip point type
  309. |---trip_point_[0-*]_hyst: Hysteresis value for this trip point
  310. |---emul_temp: Emulated temperature set node
  311. |---sustainable_power: Sustainable dissipatable power
  312. |---k_po: Proportional term during temperature overshoot
  313. |---k_pu: Proportional term during temperature undershoot
  314. |---k_i: PID's integral term in the power allocator gov
  315. |---k_d: PID's derivative term in the power allocator
  316. |---integral_cutoff: Offset above which errors are accumulated
  317. |---slope: Slope constant applied as linear extrapolation
  318. |---offset: Offset constant applied as linear extrapolation
  319. Thermal cooling device sys I/F, created once it's registered::
  320. /sys/class/thermal/cooling_device[0-*]:
  321. |---type: Type of the cooling device(processor/fan/...)
  322. |---max_state: Maximum cooling state of the cooling device
  323. |---cur_state: Current cooling state of the cooling device
  324. |---stats: Directory containing cooling device's statistics
  325. |---stats/reset: Writing any value resets the statistics
  326. |---stats/time_in_state_ms: Time (msec) spent in various cooling states
  327. |---stats/total_trans: Total number of times cooling state is changed
  328. |---stats/trans_table: Cooling state transition table
  329. Then next two dynamic attributes are created/removed in pairs. They represent
  330. the relationship between a thermal zone and its associated cooling device.
  331. They are created/removed for each successful execution of
  332. thermal_zone_bind_cooling_device/thermal_zone_unbind_cooling_device.
  333. ::
  334. /sys/class/thermal/thermal_zone[0-*]:
  335. |---cdev[0-*]: [0-*]th cooling device in current thermal zone
  336. |---cdev[0-*]_trip_point: Trip point that cdev[0-*] is associated with
  337. |---cdev[0-*]_weight: Influence of the cooling device in
  338. this thermal zone
  339. Besides the thermal zone device sysfs I/F and cooling device sysfs I/F,
  340. the generic thermal driver also creates a hwmon sysfs I/F for each _type_
  341. of thermal zone device. E.g. the generic thermal driver registers one hwmon
  342. class device and build the associated hwmon sysfs I/F for all the registered
  343. ACPI thermal zones.
  344. Please read Documentation/ABI/testing/sysfs-class-thermal for thermal
  345. zone and cooling device attribute details.
  346. ::
  347. /sys/class/hwmon/hwmon[0-*]:
  348. |---name: The type of the thermal zone devices
  349. |---temp[1-*]_input: The current temperature of thermal zone [1-*]
  350. |---temp[1-*]_critical: The critical trip point of thermal zone [1-*]
  351. Please read Documentation/hwmon/sysfs-interface.rst for additional information.
  352. 3. A simple implementation
  353. ==========================
  354. ACPI thermal zone may support multiple trip points like critical, hot,
  355. passive, active. If an ACPI thermal zone supports critical, passive,
  356. active[0] and active[1] at the same time, it may register itself as a
  357. thermal_zone_device (thermal_zone1) with 4 trip points in all.
  358. It has one processor and one fan, which are both registered as
  359. thermal_cooling_device. Both are considered to have the same
  360. effectiveness in cooling the thermal zone.
  361. If the processor is listed in _PSL method, and the fan is listed in _AL0
  362. method, the sys I/F structure will be built like this::
  363. /sys/class/thermal:
  364. |thermal_zone1:
  365. |---type: acpitz
  366. |---temp: 37000
  367. |---mode: enabled
  368. |---policy: step_wise
  369. |---available_policies: step_wise fair_share
  370. |---trip_point_0_temp: 100000
  371. |---trip_point_0_type: critical
  372. |---trip_point_1_temp: 80000
  373. |---trip_point_1_type: passive
  374. |---trip_point_2_temp: 70000
  375. |---trip_point_2_type: active0
  376. |---trip_point_3_temp: 60000
  377. |---trip_point_3_type: active1
  378. |---cdev0: --->/sys/class/thermal/cooling_device0
  379. |---cdev0_trip_point: 1 /* cdev0 can be used for passive */
  380. |---cdev0_weight: 1024
  381. |---cdev1: --->/sys/class/thermal/cooling_device3
  382. |---cdev1_trip_point: 2 /* cdev1 can be used for active[0]*/
  383. |---cdev1_weight: 1024
  384. |cooling_device0:
  385. |---type: Processor
  386. |---max_state: 8
  387. |---cur_state: 0
  388. |cooling_device3:
  389. |---type: Fan
  390. |---max_state: 2
  391. |---cur_state: 0
  392. /sys/class/hwmon:
  393. |hwmon0:
  394. |---name: acpitz
  395. |---temp1_input: 37000
  396. |---temp1_crit: 100000
  397. 4. Export Symbol APIs
  398. =====================
  399. 4.1. get_tz_trend
  400. -----------------
  401. This function returns the trend of a thermal zone, i.e the rate of change
  402. of temperature of the thermal zone. Ideally, the thermal sensor drivers
  403. are supposed to implement the callback. If they don't, the thermal
  404. framework calculated the trend by comparing the previous and the current
  405. temperature values.
  406. 4.2. get_thermal_instance
  407. -------------------------
  408. This function returns the thermal_instance corresponding to a given
  409. {thermal_zone, cooling_device, trip_point} combination. Returns NULL
  410. if such an instance does not exist.
  411. 4.3. thermal_cdev_update
  412. ------------------------
  413. This function serves as an arbitrator to set the state of a cooling
  414. device. It sets the cooling device to the deepest cooling state if
  415. possible.
  416. 5. thermal_emergency_poweroff
  417. =============================
  418. On an event of critical trip temperature crossing the thermal framework
  419. shuts down the system by calling hw_protection_shutdown(). The
  420. hw_protection_shutdown() first attempts to perform an orderly shutdown
  421. but accepts a delay after which it proceeds doing a forced power-off
  422. or as last resort an emergency_restart.
  423. The delay should be carefully profiled so as to give adequate time for
  424. orderly poweroff.
  425. If the delay is set to 0 emergency poweroff will not be supported. So a
  426. carefully profiled non-zero positive value is a must for emergency
  427. poweroff to be triggered.