blob: b28dcc6ec9bad4c6837373af752c666001f716bb [file] [log] [blame]
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <acpi/acpigen.h>
4#include <acpi/acpigen_dptf.h>
Elyes HAOUAS441e1912020-07-27 09:37:08 +02005#include <stdbool.h>
6#include <stdint.h>
Elyes Haouasbdd03c22024-05-27 11:20:07 +02007#include <stdio.h>
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -06008
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -06009/* Defaults */
Tim Wawrzynczak46f6fcf2020-05-29 14:29:53 -060010#define DEFAULT_RAW_UNIT "ma"
11
Tim Wawrzynczak03465f42020-06-03 16:27:30 -060012/* 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"
16
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060017enum {
18 ART_REVISION = 0,
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -060019 DEFAULT_PRIORITY = 100,
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -060020 DEFAULT_TRIP_POINT = 0xFFFFFFFFull,
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060021 DEFAULT_WEIGHT = 100,
22 DPTF_MAX_ART_THRESHOLDS = 10,
Tim Wawrzynczaka9d3e652020-07-27 13:51:04 -060023 FPS_REVISION = 0,
Tim Wawrzynczakbb5c2552020-05-29 14:46:19 -060024 PPCC_REVISION = 2,
25 RAPL_PL1_INDEX = 0,
26 RAPL_PL2_INDEX = 1,
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060027};
28
29/* Convert degrees C to 1/10 degree Kelvin for ACPI */
30static int to_acpi_temp(int deg_c)
31{
32 return deg_c * 10 + 2732;
33}
34
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -060035/* Converts ms to 1/10th second for ACPI */
36static int to_acpi_time(int ms)
37{
38 return ms / 100;
39}
40
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060041/* Writes out a 0-argument non-Serialized Method that returns an Integer */
42static void write_simple_return_method(const char *name, int value)
43{
44 acpigen_write_method(name, 0);
45 acpigen_write_return_integer(value);
46 acpigen_pop_len(); /* Method */
47}
48
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -060049/* Writes out 'count' ZEROs in a row */
50static void write_zeros(int count)
51{
52 for (; count; --count)
53 acpigen_write_integer(0);
54}
55
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060056/* Return the assigned namestring of any participant */
57static const char *namestring_of(enum dptf_participant participant)
58{
59 switch (participant) {
60 case DPTF_CPU:
61 return "TCPU";
62 case DPTF_CHARGER:
63 return "TCHG";
64 case DPTF_FAN:
65 return "TFN1";
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +053066 case DPTF_FAN_2:
67 return "TFN2";
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060068 case DPTF_TEMP_SENSOR_0:
69 return "TSR0";
70 case DPTF_TEMP_SENSOR_1:
71 return "TSR1";
72 case DPTF_TEMP_SENSOR_2:
73 return "TSR2";
74 case DPTF_TEMP_SENSOR_3:
75 return "TSR3";
Tim Wawrzynczak40713aa2021-11-24 09:18:44 -070076 case DPTF_TEMP_SENSOR_4:
77 return "TSR4";
Sumeet Pawnikare2e0a6b2021-09-24 18:39:50 +053078 case DPTF_TPCH:
79 return "TPCH";
Varshit B Pandyae7d3a1a2022-03-20 20:39:51 +053080 case DPTF_POWER:
81 return "TPWR";
Varshit B Pandya170a76c2022-04-02 15:11:36 +053082 case DPTF_BATTERY:
83 return "TBAT";
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060084 default:
85 return "";
86 }
87}
88
89/* Helper to get Scope for participants underneath \_SB.DPTF */
90static const char *scope_of(enum dptf_participant participant)
91{
92 static char scope[16];
93
94 if (participant == DPTF_CPU)
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -060095 snprintf(scope, sizeof(scope), TCPU_SCOPE ".%s", namestring_of(participant));
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060096 else
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -060097 snprintf(scope, sizeof(scope), DPTF_DEVICE_PATH ".%s",
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060098 namestring_of(participant));
99
100 return scope;
101}
102
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600103/*
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.
107 */
108static const char *path_of(enum dptf_participant participant)
109{
110 if (participant == DPTF_CPU)
111 return scope_of(participant);
112 else
113 return namestring_of(participant);
114}
115
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600116/* Write out scope of a participant */
117void dptf_write_scope(enum dptf_participant participant)
118{
119 acpigen_write_scope(scope_of(participant));
120}
121
122/*
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.
127 */
128static void write_active_relationship_table(const struct dptf_active_policy *policies,
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530129 int max_count, bool dptf_multifan_support)
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600130{
131 char *pkg_count;
132 int i, j;
133
134 /* Nothing to do */
135 if (!max_count || policies[0].target == DPTF_NONE)
136 return;
137
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600138 acpigen_write_scope(DPTF_DEVICE_PATH);
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600139 acpigen_write_method("_ART", 0);
140
141 /* Return this package */
142 acpigen_emit_byte(RETURN_OP);
143
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);
147
148 for (i = 0; i < max_count; ++i) {
149 /*
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.
152 */
153 if (policies[i].target == DPTF_NONE)
154 break;
155
156 (*pkg_count)++;
157
158 /* Source, Target, Percent, Fan % for each of _AC0 ... _AC9 */
159 acpigen_write_package(13);
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530160 if (dptf_multifan_support)
161 acpigen_emit_namestring(path_of(policies[i].source));
162 else
163 acpigen_emit_namestring(path_of(DPTF_FAN));
164
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600165 acpigen_emit_namestring(path_of(policies[i].target));
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600166 acpigen_write_integer(DEFAULT_IF_0(policies[i].weight, DEFAULT_WEIGHT));
167
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);
171
172 acpigen_pop_len(); /* inner Package */
173 }
174
175 acpigen_pop_len(); /* outer Package */
176 acpigen_pop_len(); /* Method _ART */
177 acpigen_pop_len(); /* Scope */
178}
179
180/*
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.
184 */
185static void write_active_cooling_methods(const struct dptf_active_policy *policies,
186 int max_count)
187{
188 char name[5];
189 int i, j;
190
191 /* Nothing to do */
192 if (!max_count || policies[0].target == DPTF_NONE)
193 return;
194
195 for (i = 0; i < max_count; ++i) {
196 if (policies[i].target == DPTF_NONE)
197 break;
198
199 dptf_write_scope(policies[i].target);
200
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)
204 break;
205
206 snprintf(name, sizeof(name), "_AC%1X", j);
207 write_simple_return_method(name, to_acpi_temp(
208 policies[i].thresholds[j].temp));
209 }
210
211 acpigen_pop_len(); /* Scope */
212 }
213}
214
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530215void dptf_write_active_policies(const struct dptf_active_policy *policies,
216 int max_count, bool dptf_multifan_support)
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600217{
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530218 write_active_relationship_table(policies, max_count, dptf_multifan_support);
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600219 write_active_cooling_methods(policies, max_count);
220}
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -0600221
222/*
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
226 * sensor).
227 */
228static void write_thermal_relationship_table(const struct dptf_passive_policy *policies,
229 int max_count)
230{
231 char *pkg_count;
232 int i;
233
234 /* Nothing to do */
235 if (!max_count || policies[0].source == DPTF_NONE)
236 return;
237
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600238 acpigen_write_scope(DPTF_DEVICE_PATH);
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -0600239
240 /*
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
244 * target.
245 */
246 acpigen_write_name_integer("TRTR", 1);
247
248 /* Thermal Relationship Table */
249 acpigen_write_method("_TRT", 0);
250
251 /* Return this package */
252 acpigen_emit_byte(RETURN_OP);
253 pkg_count = acpigen_write_package(0);
254
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)
258 break;
259
260 /* Keep track of outer package item count */
261 (*pkg_count)++;
262
263 acpigen_write_package(8);
264
265 /* Source, Target, Priority, Sampling Period */
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600266 acpigen_emit_namestring(path_of(policies[i].source));
267 acpigen_emit_namestring(path_of(policies[i].target));
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -0600268 acpigen_write_integer(DEFAULT_IF_0(policies[i].priority, DEFAULT_PRIORITY));
269 acpigen_write_integer(to_acpi_time(policies[i].period));
270
271 /* Reserved */
272 write_zeros(4);
273
274 acpigen_pop_len(); /* Package */
275 }
276
277 acpigen_pop_len(); /* Package */
278 acpigen_pop_len(); /* Method */
279 acpigen_pop_len(); /* Scope */
280}
281
282/*
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.
285 */
286static void write_all_PSV(const struct dptf_passive_policy *policies, int max_count)
287{
288 int i;
289
290 for (i = 0; i < max_count; ++i) {
291 if (policies[i].source == DPTF_NONE)
292 break;
293
294 dptf_write_scope(policies[i].target);
295 write_simple_return_method("_PSV", to_acpi_temp(policies[i].temp));
296 acpigen_pop_len(); /* Scope */
297 }
298}
299
300void dptf_write_passive_policies(const struct dptf_passive_policy *policies, int max_count)
301{
302 write_thermal_relationship_table(policies, max_count);
303 write_all_PSV(policies, max_count);
304}
Tim Wawrzynczak3a9cde92020-05-29 14:19:15 -0600305
306void dptf_write_critical_policies(const struct dptf_critical_policy *policies, int max_count)
307{
308 int i;
309
310 for (i = 0; i < max_count; ++i) {
311 if (policies[i].source == DPTF_NONE)
312 break;
313
314 dptf_write_scope(policies[i].source);
315
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));
319
320 acpigen_pop_len(); /* Scope */
321 }
322}
Tim Wawrzynczak46f6fcf2020-05-29 14:29:53 -0600323
324void dptf_write_charger_perf(const struct dptf_charger_perf *states, int max_count)
325{
326 char *pkg_count;
327 int i;
328
329 if (!max_count || !states[0].control)
330 return;
331
332 dptf_write_scope(DPTF_CHARGER);
333
334 /* PPSS - Participant Performance Supported States */
335 acpigen_write_method("PPSS", 0);
336 acpigen_emit_byte(RETURN_OP);
337
338 pkg_count = acpigen_write_package(0);
339 for (i = 0; i < max_count; ++i) {
340 if (!states[i].control)
341 break;
342
343 (*pkg_count)++;
344
345 /*
346 * 0, 0, 0, 0, # Reserved
347 * Control, Raw Performance, Raw Unit, 0 # Reserved
348 */
349 acpigen_write_package(8);
350 write_zeros(4);
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 */
356 }
357
358 acpigen_pop_len(); /* outer Package */
359 acpigen_pop_len(); /* Method PPSS */
360 acpigen_pop_len(); /* Scope */
361}
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600362
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530363int dptf_write_fan_perf_fps(uint8_t percent, uint16_t power, uint16_t speed,
364 uint16_t noise_level)
365{
366 /*
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.
369 */
370 if (!percent && !power)
371 return 1;
372
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 */
380
381 return 0;
382}
383
384void dptf_write_fan_perf(const struct dptf_fan_perf *states, int max_count,
385 enum dptf_participant participant)
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600386{
387 char *pkg_count;
388 int i;
389
390 if (!max_count || !states[0].percent)
391 return;
392
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530393 dptf_write_scope(participant);
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600394
395 /* _FPS - Fan Performance States */
396 acpigen_write_name("_FPS");
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530397
Tim Wawrzynczaka9d3e652020-07-27 13:51:04 -0600398 pkg_count = acpigen_write_package(1); /* 1 for Revision */
399 acpigen_write_integer(FPS_REVISION); /* revision */
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600400
401 for (i = 0; i < max_count; ++i) {
Sumeet Pawnikar672bd9b2022-06-08 17:43:36 +0530402 (*pkg_count)++;
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530403 if (dptf_write_fan_perf_fps(states[i].percent, states[i].power,
404 states[i].speed, states[i].noise_level))
405 break;
406 }
407
408 acpigen_pop_len(); /* Package */
409 acpigen_pop_len(); /* Scope */
410}
411
412void 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)
416{
417 char *pkg_count;
418 int i;
419
420 if (!max_count || !states[fan_num][0].percent)
421 return;
422
423 dptf_write_scope(participant);
424
425 /* _FPS - Fan Performance States */
426 acpigen_write_name("_FPS");
427
428 pkg_count = acpigen_write_package(1); /* 1 for Revision */
429 acpigen_write_integer(FPS_REVISION); /* revision */
430
431 for (i = 0; i < max_count; ++i) {
432 (*pkg_count)++;
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))
435 break;
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600436 }
437
438 acpigen_pop_len(); /* Package */
439 acpigen_pop_len(); /* Scope */
440}
Tim Wawrzynczakbb5c2552020-05-29 14:46:19 -0600441
442void dptf_write_power_limits(const struct dptf_power_limits *limits)
443{
444 char *pkg_count;
445
446 /* Nothing to do */
447 if (!limits->pl1.min_power && !limits->pl2.min_power)
448 return;
449
450 dptf_write_scope(DPTF_CPU);
451 acpigen_write_method("PPCC", 0);
452
CoolStare145c2f2023-02-09 22:44:09 -0800453 acpigen_emit_byte(RETURN_OP);
454
Tim Wawrzynczakbb5c2552020-05-29 14:46:19 -0600455 pkg_count = acpigen_write_package(1); /* 1 for the Revision */
456 acpigen_write_integer(PPCC_REVISION); /* revision */
457
458 if (limits->pl1.min_power) {
459 (*pkg_count)++;
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 */
468 }
469
470 if (limits->pl2.min_power) {
471 (*pkg_count)++;
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 */
480 }
481
482 acpigen_pop_len(); /* outer Package */
483 acpigen_pop_len(); /* Method */
484 acpigen_pop_len(); /* Scope */
485}
Tim Wawrzynczake4d8ebc2020-05-29 14:58:16 -0600486
487void dptf_write_STR(const char *str)
488{
489 if (!str)
490 return;
491
Cliff Huang95e4ffe2023-09-07 09:39:37 -0700492 acpigen_write_name_unicode("_STR", str);
Tim Wawrzynczake4d8ebc2020-05-29 14:58:16 -0600493}
494
495void dptf_write_fan_options(bool fine_grained, int step_size, bool low_speed_notify)
496{
497 acpigen_write_name("_FIF");
498 acpigen_write_package(4);
499
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 */
505}
506
507void dptf_write_tsr_hysteresis(uint8_t hysteresis)
508{
509 if (!hysteresis)
510 return;
511
512 acpigen_write_name_integer("GTSH", hysteresis);
513}
Tim Wawrzynczak03465f42020-06-03 16:27:30 -0600514
515void dptf_write_enabled_policies(const struct dptf_active_policy *active_policies,
516 int active_count,
517 const struct dptf_passive_policy *passive_policies,
518 int passive_count,
519 const struct dptf_critical_policy *critical_policies,
520 int critical_count)
521{
522 bool is_active_used;
523 bool is_passive_used;
524 bool is_critical_used;
525 int pkg_count;
526
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;
531
532 if (!pkg_count)
533 return;
534
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600535 acpigen_write_scope(DPTF_DEVICE_PATH);
Tim Wawrzynczak03465f42020-06-03 16:27:30 -0600536 acpigen_write_name("IDSP");
537 acpigen_write_package(pkg_count);
538
539 if (is_active_used)
540 acpigen_write_uuid(DPTF_ACTIVE_POLICY_UUID);
541
542 if (is_passive_used)
543 acpigen_write_uuid(DPTF_PASSIVE_POLICY_1_0_UUID);
544
545 if (is_critical_used)
546 acpigen_write_uuid(DPTF_CRITICAL_POLICY_UUID);
547
548 acpigen_pop_len(); /* Package */
549 acpigen_pop_len(); /* Scope */
550}