blob: 3877daa88bbee4e17ea52fffd56709223cc6fd98 [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>
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -06007
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -06008/* Defaults */
Tim Wawrzynczak46f6fcf2020-05-29 14:29:53 -06009#define DEFAULT_RAW_UNIT "ma"
10
Tim Wawrzynczak03465f42020-06-03 16:27:30 -060011/* DPTF-specific UUIDs */
12#define DPTF_PASSIVE_POLICY_1_0_UUID "42A441D6-AE6A-462B-A84B-4A8CE79027D3"
13#define DPTF_CRITICAL_POLICY_UUID "97C68AE7-15FA-499c-B8C9-5DA81D606E0A"
14#define DPTF_ACTIVE_POLICY_UUID "3A95C389-E4B8-4629-A526-C52C88626BAE"
15
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060016enum {
17 ART_REVISION = 0,
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -060018 DEFAULT_PRIORITY = 100,
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -060019 DEFAULT_TRIP_POINT = 0xFFFFFFFFull,
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060020 DEFAULT_WEIGHT = 100,
21 DPTF_MAX_ART_THRESHOLDS = 10,
Tim Wawrzynczaka9d3e652020-07-27 13:51:04 -060022 FPS_REVISION = 0,
Tim Wawrzynczakbb5c2552020-05-29 14:46:19 -060023 PPCC_REVISION = 2,
24 RAPL_PL1_INDEX = 0,
25 RAPL_PL2_INDEX = 1,
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060026};
27
28/* Convert degrees C to 1/10 degree Kelvin for ACPI */
29static int to_acpi_temp(int deg_c)
30{
31 return deg_c * 10 + 2732;
32}
33
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -060034/* Converts ms to 1/10th second for ACPI */
35static int to_acpi_time(int ms)
36{
37 return ms / 100;
38}
39
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060040/* Writes out a 0-argument non-Serialized Method that returns an Integer */
41static void write_simple_return_method(const char *name, int value)
42{
43 acpigen_write_method(name, 0);
44 acpigen_write_return_integer(value);
45 acpigen_pop_len(); /* Method */
46}
47
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -060048/* Writes out 'count' ZEROs in a row */
49static void write_zeros(int count)
50{
51 for (; count; --count)
52 acpigen_write_integer(0);
53}
54
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060055/* Return the assigned namestring of any participant */
56static const char *namestring_of(enum dptf_participant participant)
57{
58 switch (participant) {
59 case DPTF_CPU:
60 return "TCPU";
61 case DPTF_CHARGER:
62 return "TCHG";
63 case DPTF_FAN:
64 return "TFN1";
65 case DPTF_TEMP_SENSOR_0:
66 return "TSR0";
67 case DPTF_TEMP_SENSOR_1:
68 return "TSR1";
69 case DPTF_TEMP_SENSOR_2:
70 return "TSR2";
71 case DPTF_TEMP_SENSOR_3:
72 return "TSR3";
73 default:
74 return "";
75 }
76}
77
78/* Helper to get Scope for participants underneath \_SB.DPTF */
79static const char *scope_of(enum dptf_participant participant)
80{
81 static char scope[16];
82
83 if (participant == DPTF_CPU)
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -060084 snprintf(scope, sizeof(scope), TCPU_SCOPE ".%s", namestring_of(participant));
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060085 else
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -060086 snprintf(scope, sizeof(scope), DPTF_DEVICE_PATH ".%s",
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -060087 namestring_of(participant));
88
89 return scope;
90}
91
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -060092/*
93 * Most of the DPTF participants are underneath the \_SB.DPTF scope, so we can just get away
94 * with using the simple namestring for references, but the TCPU has a different scope, so
95 * either an absolute or relative path must be used instead.
96 */
97static const char *path_of(enum dptf_participant participant)
98{
99 if (participant == DPTF_CPU)
100 return scope_of(participant);
101 else
102 return namestring_of(participant);
103}
104
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600105/* Write out scope of a participant */
106void dptf_write_scope(enum dptf_participant participant)
107{
108 acpigen_write_scope(scope_of(participant));
109}
110
111/*
112 * This table describes active cooling relationships between the system's fan and the
113 * temperature sensors that it can have an effect on. As ever-increasing temperature thresholds
114 * are crossed (_AC9.._AC0, low to high), the corresponding fan percentages listed in this table
115 * are used to increase the speed of the fan in order to speed up cooling.
116 */
117static void write_active_relationship_table(const struct dptf_active_policy *policies,
118 int max_count)
119{
120 char *pkg_count;
121 int i, j;
122
123 /* Nothing to do */
124 if (!max_count || policies[0].target == DPTF_NONE)
125 return;
126
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600127 acpigen_write_scope(DPTF_DEVICE_PATH);
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600128 acpigen_write_method("_ART", 0);
129
130 /* Return this package */
131 acpigen_emit_byte(RETURN_OP);
132
133 /* Keep track of items added to the package */
134 pkg_count = acpigen_write_package(1); /* The '1' here is for the revision */
135 acpigen_write_integer(ART_REVISION);
136
137 for (i = 0; i < max_count; ++i) {
138 /*
139 * These have to be filled out from AC0 down to AC9, filling in only as many
140 * as are used. As soon as one isn't filled in, we're done.
141 */
142 if (policies[i].target == DPTF_NONE)
143 break;
144
145 (*pkg_count)++;
146
147 /* Source, Target, Percent, Fan % for each of _AC0 ... _AC9 */
148 acpigen_write_package(13);
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600149 acpigen_emit_namestring(path_of(DPTF_FAN));
150 acpigen_emit_namestring(path_of(policies[i].target));
Tim Wawrzynczakc41f7f12020-05-29 13:56:37 -0600151 acpigen_write_integer(DEFAULT_IF_0(policies[i].weight, DEFAULT_WEIGHT));
152
153 /* Write out fan %; corresponds with target's _ACx methods */
154 for (j = 0; j < DPTF_MAX_ART_THRESHOLDS; ++j)
155 acpigen_write_integer(policies[i].thresholds[j].fan_pct);
156
157 acpigen_pop_len(); /* inner Package */
158 }
159
160 acpigen_pop_len(); /* outer Package */
161 acpigen_pop_len(); /* Method _ART */
162 acpigen_pop_len(); /* Scope */
163}
164
165/*
166 * _AC9 through _AC0 represent temperature thresholds, in increasing order, defined from _AC0
167 * down, that, when reached, DPTF will activate TFN1 in order to actively cool the temperature
168 * sensor(s). As increasing thresholds are reached, the fan is spun faster.
169 */
170static void write_active_cooling_methods(const struct dptf_active_policy *policies,
171 int max_count)
172{
173 char name[5];
174 int i, j;
175
176 /* Nothing to do */
177 if (!max_count || policies[0].target == DPTF_NONE)
178 return;
179
180 for (i = 0; i < max_count; ++i) {
181 if (policies[i].target == DPTF_NONE)
182 break;
183
184 dptf_write_scope(policies[i].target);
185
186 /* Write out as many of _AC0 through _AC9 that are applicable */
187 for (j = 0; j < DPTF_MAX_ACX; ++j) {
188 if (!policies[i].thresholds[j].temp)
189 break;
190
191 snprintf(name, sizeof(name), "_AC%1X", j);
192 write_simple_return_method(name, to_acpi_temp(
193 policies[i].thresholds[j].temp));
194 }
195
196 acpigen_pop_len(); /* Scope */
197 }
198}
199
200void dptf_write_active_policies(const struct dptf_active_policy *policies, int max_count)
201{
202 write_active_relationship_table(policies, max_count);
203 write_active_cooling_methods(policies, max_count);
204}
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -0600205
206/*
207 * This writes out the Thermal Relationship Table, which describes the thermal relationships
208 * between participants in a thermal zone. This information is used to passively cool (i.e.,
209 * throttle) the Source (source of heat), in order to indirectly cool the Target (temperature
210 * sensor).
211 */
212static void write_thermal_relationship_table(const struct dptf_passive_policy *policies,
213 int max_count)
214{
215 char *pkg_count;
216 int i;
217
218 /* Nothing to do */
219 if (!max_count || policies[0].source == DPTF_NONE)
220 return;
221
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600222 acpigen_write_scope(DPTF_DEVICE_PATH);
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -0600223
224 /*
225 * A _TRT Revision (TRTR) of 1 means that the 'Priority' field is an arbitrary priority
226 * value to be used for this specific relationship. The priority value determines the
227 * order in which various sources are used in a passive thermal action for a given
228 * target.
229 */
230 acpigen_write_name_integer("TRTR", 1);
231
232 /* Thermal Relationship Table */
233 acpigen_write_method("_TRT", 0);
234
235 /* Return this package */
236 acpigen_emit_byte(RETURN_OP);
237 pkg_count = acpigen_write_package(0);
238
239 for (i = 0; i < max_count; ++i) {
240 /* Stop writing the table once an entry is empty */
241 if (policies[i].source == DPTF_NONE)
242 break;
243
244 /* Keep track of outer package item count */
245 (*pkg_count)++;
246
247 acpigen_write_package(8);
248
249 /* Source, Target, Priority, Sampling Period */
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600250 acpigen_emit_namestring(path_of(policies[i].source));
251 acpigen_emit_namestring(path_of(policies[i].target));
Tim Wawrzynczak7eb11362020-05-29 14:10:53 -0600252 acpigen_write_integer(DEFAULT_IF_0(policies[i].priority, DEFAULT_PRIORITY));
253 acpigen_write_integer(to_acpi_time(policies[i].period));
254
255 /* Reserved */
256 write_zeros(4);
257
258 acpigen_pop_len(); /* Package */
259 }
260
261 acpigen_pop_len(); /* Package */
262 acpigen_pop_len(); /* Method */
263 acpigen_pop_len(); /* Scope */
264}
265
266/*
267 * When a temperature sensor measures above its the temperature returned in its _PSV Method,
268 * DPTF will begin throttling Sources in order to indirectly cool the sensor.
269 */
270static void write_all_PSV(const struct dptf_passive_policy *policies, int max_count)
271{
272 int i;
273
274 for (i = 0; i < max_count; ++i) {
275 if (policies[i].source == DPTF_NONE)
276 break;
277
278 dptf_write_scope(policies[i].target);
279 write_simple_return_method("_PSV", to_acpi_temp(policies[i].temp));
280 acpigen_pop_len(); /* Scope */
281 }
282}
283
284void dptf_write_passive_policies(const struct dptf_passive_policy *policies, int max_count)
285{
286 write_thermal_relationship_table(policies, max_count);
287 write_all_PSV(policies, max_count);
288}
Tim Wawrzynczak3a9cde92020-05-29 14:19:15 -0600289
290void dptf_write_critical_policies(const struct dptf_critical_policy *policies, int max_count)
291{
292 int i;
293
294 for (i = 0; i < max_count; ++i) {
295 if (policies[i].source == DPTF_NONE)
296 break;
297
298 dptf_write_scope(policies[i].source);
299
300 /* Choose _CRT or _HOT */
301 write_simple_return_method(policies[i].type == DPTF_CRITICAL_SHUTDOWN ?
302 "_CRT" : "_HOT", to_acpi_temp(policies[i].temp));
303
304 acpigen_pop_len(); /* Scope */
305 }
306}
Tim Wawrzynczak46f6fcf2020-05-29 14:29:53 -0600307
308void dptf_write_charger_perf(const struct dptf_charger_perf *states, int max_count)
309{
310 char *pkg_count;
311 int i;
312
313 if (!max_count || !states[0].control)
314 return;
315
316 dptf_write_scope(DPTF_CHARGER);
317
318 /* PPSS - Participant Performance Supported States */
319 acpigen_write_method("PPSS", 0);
320 acpigen_emit_byte(RETURN_OP);
321
322 pkg_count = acpigen_write_package(0);
323 for (i = 0; i < max_count; ++i) {
324 if (!states[i].control)
325 break;
326
327 (*pkg_count)++;
328
329 /*
330 * 0, 0, 0, 0, # Reserved
331 * Control, Raw Performance, Raw Unit, 0 # Reserved
332 */
333 acpigen_write_package(8);
334 write_zeros(4);
335 acpigen_write_integer(states[i].control);
336 acpigen_write_integer(states[i].raw_perf);
337 acpigen_write_string(DEFAULT_RAW_UNIT);
338 acpigen_write_integer(0);
339 acpigen_pop_len(); /* inner Package */
340 }
341
342 acpigen_pop_len(); /* outer Package */
343 acpigen_pop_len(); /* Method PPSS */
344 acpigen_pop_len(); /* Scope */
345}
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600346
347void dptf_write_fan_perf(const struct dptf_fan_perf *states, int max_count)
348{
349 char *pkg_count;
350 int i;
351
352 if (!max_count || !states[0].percent)
353 return;
354
355 dptf_write_scope(DPTF_FAN);
356
357 /* _FPS - Fan Performance States */
358 acpigen_write_name("_FPS");
Tim Wawrzynczaka9d3e652020-07-27 13:51:04 -0600359 pkg_count = acpigen_write_package(1); /* 1 for Revision */
360 acpigen_write_integer(FPS_REVISION); /* revision */
Tim Wawrzynczak2ad8ffe2020-05-29 14:39:02 -0600361
362 for (i = 0; i < max_count; ++i) {
363 /*
364 * Some _FPS tables do include a last entry where Percent is 0, but Power is
365 * called out, so this table is finished when both are zero.
366 */
367 if (!states[i].percent && !states[i].power)
368 break;
369
370 (*pkg_count)++;
371 acpigen_write_package(5);
372 acpigen_write_integer(states[i].percent);
373 acpigen_write_integer(DEFAULT_TRIP_POINT);
374 acpigen_write_integer(states[i].speed);
375 acpigen_write_integer(states[i].noise_level);
376 acpigen_write_integer(states[i].power);
377 acpigen_pop_len(); /* inner Package */
378 }
379
380 acpigen_pop_len(); /* Package */
381 acpigen_pop_len(); /* Scope */
382}
Tim Wawrzynczakbb5c2552020-05-29 14:46:19 -0600383
384void dptf_write_power_limits(const struct dptf_power_limits *limits)
385{
386 char *pkg_count;
387
388 /* Nothing to do */
389 if (!limits->pl1.min_power && !limits->pl2.min_power)
390 return;
391
392 dptf_write_scope(DPTF_CPU);
393 acpigen_write_method("PPCC", 0);
394
395 pkg_count = acpigen_write_package(1); /* 1 for the Revision */
396 acpigen_write_integer(PPCC_REVISION); /* revision */
397
398 if (limits->pl1.min_power) {
399 (*pkg_count)++;
400 acpigen_write_package(6);
401 acpigen_write_integer(RAPL_PL1_INDEX);
402 acpigen_write_integer(limits->pl1.min_power);
403 acpigen_write_integer(limits->pl1.max_power);
404 acpigen_write_integer(limits->pl1.time_window_min);
405 acpigen_write_integer(limits->pl1.time_window_max);
406 acpigen_write_integer(limits->pl1.granularity);
407 acpigen_pop_len(); /* inner Package */
408 }
409
410 if (limits->pl2.min_power) {
411 (*pkg_count)++;
412 acpigen_write_package(6);
413 acpigen_write_integer(RAPL_PL2_INDEX);
414 acpigen_write_integer(limits->pl2.min_power);
415 acpigen_write_integer(limits->pl2.max_power);
416 acpigen_write_integer(limits->pl2.time_window_min);
417 acpigen_write_integer(limits->pl2.time_window_max);
418 acpigen_write_integer(limits->pl2.granularity);
419 acpigen_pop_len(); /* inner Package */
420 }
421
422 acpigen_pop_len(); /* outer Package */
423 acpigen_pop_len(); /* Method */
424 acpigen_pop_len(); /* Scope */
425}
Tim Wawrzynczake4d8ebc2020-05-29 14:58:16 -0600426
427void dptf_write_STR(const char *str)
428{
429 if (!str)
430 return;
431
432 acpigen_write_name_string("_STR", str);
433}
434
435void dptf_write_fan_options(bool fine_grained, int step_size, bool low_speed_notify)
436{
437 acpigen_write_name("_FIF");
438 acpigen_write_package(4);
439
440 acpigen_write_integer(0); /* Revision */
441 acpigen_write_integer(fine_grained);
442 acpigen_write_integer(step_size);
443 acpigen_write_integer(low_speed_notify);
444 acpigen_pop_len(); /* Package */
445}
446
447void dptf_write_tsr_hysteresis(uint8_t hysteresis)
448{
449 if (!hysteresis)
450 return;
451
452 acpigen_write_name_integer("GTSH", hysteresis);
453}
Tim Wawrzynczak03465f42020-06-03 16:27:30 -0600454
455void dptf_write_enabled_policies(const struct dptf_active_policy *active_policies,
456 int active_count,
457 const struct dptf_passive_policy *passive_policies,
458 int passive_count,
459 const struct dptf_critical_policy *critical_policies,
460 int critical_count)
461{
462 bool is_active_used;
463 bool is_passive_used;
464 bool is_critical_used;
465 int pkg_count;
466
467 is_active_used = (active_count && active_policies[0].target != DPTF_NONE);
468 is_passive_used = (passive_count && passive_policies[0].target != DPTF_NONE);
469 is_critical_used = (critical_count && critical_policies[0].source != DPTF_NONE);
470 pkg_count = is_active_used + is_passive_used + is_critical_used;
471
472 if (!pkg_count)
473 return;
474
Tim Wawrzynczak5212ece62020-07-16 11:54:04 -0600475 acpigen_write_scope(DPTF_DEVICE_PATH);
Tim Wawrzynczak03465f42020-06-03 16:27:30 -0600476 acpigen_write_name("IDSP");
477 acpigen_write_package(pkg_count);
478
479 if (is_active_used)
480 acpigen_write_uuid(DPTF_ACTIVE_POLICY_UUID);
481
482 if (is_passive_used)
483 acpigen_write_uuid(DPTF_PASSIVE_POLICY_1_0_UUID);
484
485 if (is_critical_used)
486 acpigen_write_uuid(DPTF_CRITICAL_POLICY_UUID);
487
488 acpigen_pop_len(); /* Package */
489 acpigen_pop_len(); /* Scope */
490}