1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpigen.h>
4 #include <acpi/acpigen_dptf.h>
10 #define DEFAULT_RAW_UNIT "ma"
12 /* DPTF-specific UUIDs */
13 #define DPTF_PASSIVE_POLICY_1_0_UUID "42A441D6-AE6A-462B-A84B-4A8CE79027D3"
14 #define DPTF_CRITICAL_POLICY_UUID "97C68AE7-15FA-499c-B8C9-5DA81D606E0A"
15 #define DPTF_ACTIVE_POLICY_UUID "3A95C389-E4B8-4629-A526-C52C88626BAE"
19 DEFAULT_PRIORITY
= 100,
20 DEFAULT_TRIP_POINT
= 0xFFFFFFFFull
,
22 DPTF_MAX_ART_THRESHOLDS
= 10,
29 /* Convert degrees C to 1/10 degree Kelvin for ACPI */
30 static int to_acpi_temp(int deg_c
)
32 return deg_c
* 10 + 2732;
35 /* Converts ms to 1/10th second for ACPI */
36 static int to_acpi_time(int ms
)
41 /* Writes out a 0-argument non-Serialized Method that returns an Integer */
42 static void write_simple_return_method(const char *name
, int value
)
44 acpigen_write_method(name
, 0);
45 acpigen_write_return_integer(value
);
46 acpigen_pop_len(); /* Method */
49 /* Writes out 'count' ZEROs in a row */
50 static void write_zeros(int count
)
52 for (; count
; --count
)
53 acpigen_write_integer(0);
56 /* Return the assigned namestring of any participant */
57 static const char *namestring_of(enum dptf_participant participant
)
59 switch (participant
) {
68 case DPTF_TEMP_SENSOR_0
:
70 case DPTF_TEMP_SENSOR_1
:
72 case DPTF_TEMP_SENSOR_2
:
74 case DPTF_TEMP_SENSOR_3
:
76 case DPTF_TEMP_SENSOR_4
:
89 /* Helper to get Scope for participants underneath \_SB.DPTF */
90 static const char *scope_of(enum dptf_participant participant
)
92 static char scope
[16];
94 if (participant
== DPTF_CPU
)
95 snprintf(scope
, sizeof(scope
), TCPU_SCOPE
".%s", namestring_of(participant
));
97 snprintf(scope
, sizeof(scope
), DPTF_DEVICE_PATH
".%s",
98 namestring_of(participant
));
104 * Most of the DPTF participants are underneath the \_SB.DPTF scope, so we can just get away
105 * with using the simple namestring for references, but the TCPU has a different scope, so
106 * either an absolute or relative path must be used instead.
108 static const char *path_of(enum dptf_participant participant
)
110 if (participant
== DPTF_CPU
)
111 return scope_of(participant
);
113 return namestring_of(participant
);
116 /* Write out scope of a participant */
117 void dptf_write_scope(enum dptf_participant participant
)
119 acpigen_write_scope(scope_of(participant
));
123 * This table describes active cooling relationships between the system's fan and the
124 * temperature sensors that it can have an effect on. As ever-increasing temperature thresholds
125 * are crossed (_AC9.._AC0, low to high), the corresponding fan percentages listed in this table
126 * are used to increase the speed of the fan in order to speed up cooling.
128 static void write_active_relationship_table(const struct dptf_active_policy
*policies
,
129 int max_count
, bool dptf_multifan_support
)
135 if (!max_count
|| policies
[0].target
== DPTF_NONE
)
138 acpigen_write_scope(DPTF_DEVICE_PATH
);
139 acpigen_write_method("_ART", 0);
141 /* Return this package */
142 acpigen_emit_byte(RETURN_OP
);
144 /* Keep track of items added to the package */
145 pkg_count
= acpigen_write_package(1); /* The '1' here is for the revision */
146 acpigen_write_integer(ART_REVISION
);
148 for (i
= 0; i
< max_count
; ++i
) {
150 * These have to be filled out from AC0 down to AC9, filling in only as many
151 * as are used. As soon as one isn't filled in, we're done.
153 if (policies
[i
].target
== DPTF_NONE
)
158 /* Source, Target, Percent, Fan % for each of _AC0 ... _AC9 */
159 acpigen_write_package(13);
160 if (dptf_multifan_support
)
161 acpigen_emit_namestring(path_of(policies
[i
].source
));
163 acpigen_emit_namestring(path_of(DPTF_FAN
));
165 acpigen_emit_namestring(path_of(policies
[i
].target
));
166 acpigen_write_integer(DEFAULT_IF_0(policies
[i
].weight
, DEFAULT_WEIGHT
));
168 /* Write out fan %; corresponds with target's _ACx methods */
169 for (j
= 0; j
< DPTF_MAX_ART_THRESHOLDS
; ++j
)
170 acpigen_write_integer(policies
[i
].thresholds
[j
].fan_pct
);
172 acpigen_pop_len(); /* inner Package */
175 acpigen_pop_len(); /* outer Package */
176 acpigen_pop_len(); /* Method _ART */
177 acpigen_pop_len(); /* Scope */
181 * _AC9 through _AC0 represent temperature thresholds, in increasing order, defined from _AC0
182 * down, that, when reached, DPTF will activate TFN1 in order to actively cool the temperature
183 * sensor(s). As increasing thresholds are reached, the fan is spun faster.
185 static void write_active_cooling_methods(const struct dptf_active_policy
*policies
,
192 if (!max_count
|| policies
[0].target
== DPTF_NONE
)
195 for (i
= 0; i
< max_count
; ++i
) {
196 if (policies
[i
].target
== DPTF_NONE
)
199 dptf_write_scope(policies
[i
].target
);
201 /* Write out as many of _AC0 through _AC9 that are applicable */
202 for (j
= 0; j
< DPTF_MAX_ACX
; ++j
) {
203 if (!policies
[i
].thresholds
[j
].temp
)
206 snprintf(name
, sizeof(name
), "_AC%1X", j
);
207 write_simple_return_method(name
, to_acpi_temp(
208 policies
[i
].thresholds
[j
].temp
));
211 acpigen_pop_len(); /* Scope */
215 void dptf_write_active_policies(const struct dptf_active_policy
*policies
,
216 int max_count
, bool dptf_multifan_support
)
218 write_active_relationship_table(policies
, max_count
, dptf_multifan_support
);
219 write_active_cooling_methods(policies
, max_count
);
223 * This writes out the Thermal Relationship Table, which describes the thermal relationships
224 * between participants in a thermal zone. This information is used to passively cool (i.e.,
225 * throttle) the Source (source of heat), in order to indirectly cool the Target (temperature
228 static void write_thermal_relationship_table(const struct dptf_passive_policy
*policies
,
235 if (!max_count
|| policies
[0].source
== DPTF_NONE
)
238 acpigen_write_scope(DPTF_DEVICE_PATH
);
241 * A _TRT Revision (TRTR) of 1 means that the 'Priority' field is an arbitrary priority
242 * value to be used for this specific relationship. The priority value determines the
243 * order in which various sources are used in a passive thermal action for a given
246 acpigen_write_name_integer("TRTR", 1);
248 /* Thermal Relationship Table */
249 acpigen_write_method("_TRT", 0);
251 /* Return this package */
252 acpigen_emit_byte(RETURN_OP
);
253 pkg_count
= acpigen_write_package(0);
255 for (i
= 0; i
< max_count
; ++i
) {
256 /* Stop writing the table once an entry is empty */
257 if (policies
[i
].source
== DPTF_NONE
)
260 /* Keep track of outer package item count */
263 acpigen_write_package(8);
265 /* Source, Target, Priority, Sampling Period */
266 acpigen_emit_namestring(path_of(policies
[i
].source
));
267 acpigen_emit_namestring(path_of(policies
[i
].target
));
268 acpigen_write_integer(DEFAULT_IF_0(policies
[i
].priority
, DEFAULT_PRIORITY
));
269 acpigen_write_integer(to_acpi_time(policies
[i
].period
));
274 acpigen_pop_len(); /* Package */
277 acpigen_pop_len(); /* Package */
278 acpigen_pop_len(); /* Method */
279 acpigen_pop_len(); /* Scope */
283 * When a temperature sensor measures above its the temperature returned in its _PSV Method,
284 * DPTF will begin throttling Sources in order to indirectly cool the sensor.
286 static void write_all_PSV(const struct dptf_passive_policy
*policies
, int max_count
)
290 for (i
= 0; i
< max_count
; ++i
) {
291 if (policies
[i
].source
== DPTF_NONE
)
294 dptf_write_scope(policies
[i
].target
);
295 write_simple_return_method("_PSV", to_acpi_temp(policies
[i
].temp
));
296 acpigen_pop_len(); /* Scope */
300 void dptf_write_passive_policies(const struct dptf_passive_policy
*policies
, int max_count
)
302 write_thermal_relationship_table(policies
, max_count
);
303 write_all_PSV(policies
, max_count
);
306 void dptf_write_critical_policies(const struct dptf_critical_policy
*policies
, int max_count
)
310 for (i
= 0; i
< max_count
; ++i
) {
311 if (policies
[i
].source
== DPTF_NONE
)
314 dptf_write_scope(policies
[i
].source
);
316 /* Choose _CRT or _HOT */
317 write_simple_return_method(policies
[i
].type
== DPTF_CRITICAL_SHUTDOWN
?
318 "_CRT" : "_HOT", to_acpi_temp(policies
[i
].temp
));
320 acpigen_pop_len(); /* Scope */
324 void dptf_write_charger_perf(const struct dptf_charger_perf
*states
, int max_count
)
329 if (!max_count
|| !states
[0].control
)
332 dptf_write_scope(DPTF_CHARGER
);
334 /* PPSS - Participant Performance Supported States */
335 acpigen_write_method("PPSS", 0);
336 acpigen_emit_byte(RETURN_OP
);
338 pkg_count
= acpigen_write_package(0);
339 for (i
= 0; i
< max_count
; ++i
) {
340 if (!states
[i
].control
)
346 * 0, 0, 0, 0, # Reserved
347 * Control, Raw Performance, Raw Unit, 0 # Reserved
349 acpigen_write_package(8);
351 acpigen_write_integer(states
[i
].control
);
352 acpigen_write_integer(states
[i
].raw_perf
);
353 acpigen_write_string(DEFAULT_RAW_UNIT
);
354 acpigen_write_integer(0);
355 acpigen_pop_len(); /* inner Package */
358 acpigen_pop_len(); /* outer Package */
359 acpigen_pop_len(); /* Method PPSS */
360 acpigen_pop_len(); /* Scope */
363 int dptf_write_fan_perf_fps(uint8_t percent
, uint16_t power
, uint16_t speed
,
364 uint16_t noise_level
)
367 * Some _FPS tables do include a last entry where Percent is 0, but Power is
368 * called out, so this table is finished when both are zero.
370 if (!percent
&& !power
)
373 acpigen_write_package(5);
374 acpigen_write_integer(percent
);
375 acpigen_write_integer(DEFAULT_TRIP_POINT
);
376 acpigen_write_integer(speed
);
377 acpigen_write_integer(noise_level
);
378 acpigen_write_integer(power
);
379 acpigen_pop_len(); /* inner Package */
384 void dptf_write_fan_perf(const struct dptf_fan_perf
*states
, int max_count
,
385 enum dptf_participant participant
)
390 if (!max_count
|| !states
[0].percent
)
393 dptf_write_scope(participant
);
395 /* _FPS - Fan Performance States */
396 acpigen_write_name("_FPS");
398 pkg_count
= acpigen_write_package(1); /* 1 for Revision */
399 acpigen_write_integer(FPS_REVISION
); /* revision */
401 for (i
= 0; i
< max_count
; ++i
) {
403 if (dptf_write_fan_perf_fps(states
[i
].percent
, states
[i
].power
,
404 states
[i
].speed
, states
[i
].noise_level
))
408 acpigen_pop_len(); /* Package */
409 acpigen_pop_len(); /* Scope */
412 void dptf_write_multifan_perf(
413 const struct dptf_multifan_perf
414 states
[DPTF_MAX_FAN_PARTICIPANTS
][DPTF_MAX_FAN_PERF_STATES
],
415 int max_count
, enum dptf_participant participant
, int fan_num
)
420 if (!max_count
|| !states
[fan_num
][0].percent
)
423 dptf_write_scope(participant
);
425 /* _FPS - Fan Performance States */
426 acpigen_write_name("_FPS");
428 pkg_count
= acpigen_write_package(1); /* 1 for Revision */
429 acpigen_write_integer(FPS_REVISION
); /* revision */
431 for (i
= 0; i
< max_count
; ++i
) {
433 if (dptf_write_fan_perf_fps(states
[fan_num
][i
].percent
, states
[fan_num
][i
].power
,
434 states
[fan_num
][i
].speed
, states
[fan_num
][i
].noise_level
))
438 acpigen_pop_len(); /* Package */
439 acpigen_pop_len(); /* Scope */
442 void dptf_write_power_limits(const struct dptf_power_limits
*limits
)
447 if (!limits
->pl1
.min_power
&& !limits
->pl2
.min_power
)
450 dptf_write_scope(DPTF_CPU
);
451 acpigen_write_method("PPCC", 0);
453 acpigen_emit_byte(RETURN_OP
);
455 pkg_count
= acpigen_write_package(1); /* 1 for the Revision */
456 acpigen_write_integer(PPCC_REVISION
); /* revision */
458 if (limits
->pl1
.min_power
) {
460 acpigen_write_package(6);
461 acpigen_write_integer(RAPL_PL1_INDEX
);
462 acpigen_write_integer(limits
->pl1
.min_power
);
463 acpigen_write_integer(limits
->pl1
.max_power
);
464 acpigen_write_integer(limits
->pl1
.time_window_min
);
465 acpigen_write_integer(limits
->pl1
.time_window_max
);
466 acpigen_write_integer(limits
->pl1
.granularity
);
467 acpigen_pop_len(); /* inner Package */
470 if (limits
->pl2
.min_power
) {
472 acpigen_write_package(6);
473 acpigen_write_integer(RAPL_PL2_INDEX
);
474 acpigen_write_integer(limits
->pl2
.min_power
);
475 acpigen_write_integer(limits
->pl2
.max_power
);
476 acpigen_write_integer(limits
->pl2
.time_window_min
);
477 acpigen_write_integer(limits
->pl2
.time_window_max
);
478 acpigen_write_integer(limits
->pl2
.granularity
);
479 acpigen_pop_len(); /* inner Package */
482 acpigen_pop_len(); /* outer Package */
483 acpigen_pop_len(); /* Method */
484 acpigen_pop_len(); /* Scope */
487 void dptf_write_STR(const char *str
)
492 acpigen_write_name_unicode("_STR", str
);
495 void dptf_write_fan_options(bool fine_grained
, int step_size
, bool low_speed_notify
)
497 acpigen_write_name("_FIF");
498 acpigen_write_package(4);
500 acpigen_write_integer(0); /* Revision */
501 acpigen_write_integer(fine_grained
);
502 acpigen_write_integer(step_size
);
503 acpigen_write_integer(low_speed_notify
);
504 acpigen_pop_len(); /* Package */
507 void dptf_write_tsr_hysteresis(uint8_t hysteresis
)
512 acpigen_write_name_integer("GTSH", hysteresis
);
515 void dptf_write_enabled_policies(const struct dptf_active_policy
*active_policies
,
517 const struct dptf_passive_policy
*passive_policies
,
519 const struct dptf_critical_policy
*critical_policies
,
523 bool is_passive_used
;
524 bool is_critical_used
;
527 is_active_used
= (active_count
&& active_policies
[0].target
!= DPTF_NONE
);
528 is_passive_used
= (passive_count
&& passive_policies
[0].target
!= DPTF_NONE
);
529 is_critical_used
= (critical_count
&& critical_policies
[0].source
!= DPTF_NONE
);
530 pkg_count
= is_active_used
+ is_passive_used
+ is_critical_used
;
535 acpigen_write_scope(DPTF_DEVICE_PATH
);
536 acpigen_write_name("IDSP");
537 acpigen_write_package(pkg_count
);
540 acpigen_write_uuid(DPTF_ACTIVE_POLICY_UUID
);
543 acpigen_write_uuid(DPTF_PASSIVE_POLICY_1_0_UUID
);
545 if (is_critical_used
)
546 acpigen_write_uuid(DPTF_CRITICAL_POLICY_UUID
);
548 acpigen_pop_len(); /* Package */
549 acpigen_pop_len(); /* Scope */