xref: /aosp_15_r20/external/coreboot/src/acpi/acpigen_dptf.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpigen.h>
4 #include <acpi/acpigen_dptf.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 
9 /* Defaults */
10 #define DEFAULT_RAW_UNIT		"ma"
11 
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"
16 
17 enum {
18 	ART_REVISION			= 0,
19 	DEFAULT_PRIORITY		= 100,
20 	DEFAULT_TRIP_POINT		= 0xFFFFFFFFull,
21 	DEFAULT_WEIGHT			= 100,
22 	DPTF_MAX_ART_THRESHOLDS		= 10,
23 	FPS_REVISION			= 0,
24 	PPCC_REVISION			= 2,
25 	RAPL_PL1_INDEX			= 0,
26 	RAPL_PL2_INDEX			= 1,
27 };
28 
29 /* Convert degrees C to 1/10 degree Kelvin for ACPI */
to_acpi_temp(int deg_c)30 static int to_acpi_temp(int deg_c)
31 {
32 	return deg_c * 10 + 2732;
33 }
34 
35 /* Converts ms to 1/10th second for ACPI */
to_acpi_time(int ms)36 static int to_acpi_time(int ms)
37 {
38 	return ms / 100;
39 }
40 
41 /* Writes out a 0-argument non-Serialized Method that returns an Integer */
write_simple_return_method(const char * name,int value)42 static 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 
49 /* Writes out 'count' ZEROs in a row */
write_zeros(int count)50 static void write_zeros(int count)
51 {
52 	for (; count; --count)
53 		acpigen_write_integer(0);
54 }
55 
56 /* Return the assigned namestring of any participant */
namestring_of(enum dptf_participant participant)57 static 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";
66 	case DPTF_FAN_2:
67 		return "TFN2";
68 	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";
76 	case DPTF_TEMP_SENSOR_4:
77 		return "TSR4";
78 	case DPTF_TPCH:
79 		return "TPCH";
80 	case DPTF_POWER:
81 		return "TPWR";
82 	case DPTF_BATTERY:
83 		return "TBAT";
84 	default:
85 		return "";
86 	}
87 }
88 
89 /* Helper to get Scope for participants underneath \_SB.DPTF */
scope_of(enum dptf_participant participant)90 static const char *scope_of(enum dptf_participant participant)
91 {
92 	static char scope[16];
93 
94 	if (participant == DPTF_CPU)
95 		snprintf(scope, sizeof(scope), TCPU_SCOPE ".%s", namestring_of(participant));
96 	else
97 		snprintf(scope, sizeof(scope), DPTF_DEVICE_PATH ".%s",
98 			 namestring_of(participant));
99 
100 	return scope;
101 }
102 
103 /*
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  */
path_of(enum dptf_participant participant)108 static 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 
116 /* Write out scope of a participant */
dptf_write_scope(enum dptf_participant participant)117 void 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  */
write_active_relationship_table(const struct dptf_active_policy * policies,int max_count,bool dptf_multifan_support)128 static void write_active_relationship_table(const struct dptf_active_policy *policies,
129 					    int max_count, bool dptf_multifan_support)
130 {
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 
138 	acpigen_write_scope(DPTF_DEVICE_PATH);
139 	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);
160 		if (dptf_multifan_support)
161 			acpigen_emit_namestring(path_of(policies[i].source));
162 		else
163 			acpigen_emit_namestring(path_of(DPTF_FAN));
164 
165 		acpigen_emit_namestring(path_of(policies[i].target));
166 		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  */
write_active_cooling_methods(const struct dptf_active_policy * policies,int max_count)185 static 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 
dptf_write_active_policies(const struct dptf_active_policy * policies,int max_count,bool dptf_multifan_support)215 void dptf_write_active_policies(const struct dptf_active_policy *policies,
216 		int max_count, bool dptf_multifan_support)
217 {
218 	write_active_relationship_table(policies, max_count, dptf_multifan_support);
219 	write_active_cooling_methods(policies, max_count);
220 }
221 
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  */
write_thermal_relationship_table(const struct dptf_passive_policy * policies,int max_count)228 static 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 
238 	acpigen_write_scope(DPTF_DEVICE_PATH);
239 
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 */
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));
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  */
write_all_PSV(const struct dptf_passive_policy * policies,int max_count)286 static 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 
dptf_write_passive_policies(const struct dptf_passive_policy * policies,int max_count)300 void 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 }
305 
dptf_write_critical_policies(const struct dptf_critical_policy * policies,int max_count)306 void 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 }
323 
dptf_write_charger_perf(const struct dptf_charger_perf * states,int max_count)324 void 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 }
362 
dptf_write_fan_perf_fps(uint8_t percent,uint16_t power,uint16_t speed,uint16_t noise_level)363 int 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 
dptf_write_fan_perf(const struct dptf_fan_perf * states,int max_count,enum dptf_participant participant)384 void dptf_write_fan_perf(const struct dptf_fan_perf *states, int max_count,
385 						enum dptf_participant participant)
386 {
387 	char *pkg_count;
388 	int i;
389 
390 	if (!max_count || !states[0].percent)
391 		return;
392 
393 	dptf_write_scope(participant);
394 
395 	/* _FPS - Fan Performance States */
396 	acpigen_write_name("_FPS");
397 
398 	pkg_count = acpigen_write_package(1); /* 1 for Revision */
399 	acpigen_write_integer(FPS_REVISION); /* revision */
400 
401 	for (i = 0; i < max_count; ++i) {
402 		(*pkg_count)++;
403 		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 
dptf_write_multifan_perf(const struct dptf_multifan_perf states[DPTF_MAX_FAN_PARTICIPANTS][DPTF_MAX_FAN_PERF_STATES],int max_count,enum dptf_participant participant,int fan_num)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)
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;
436 	}
437 
438 	acpigen_pop_len(); /* Package */
439 	acpigen_pop_len(); /* Scope */
440 }
441 
dptf_write_power_limits(const struct dptf_power_limits * limits)442 void 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 
453 	acpigen_emit_byte(RETURN_OP);
454 
455 	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 }
486 
dptf_write_STR(const char * str)487 void dptf_write_STR(const char *str)
488 {
489 	if (!str)
490 		return;
491 
492 	acpigen_write_name_unicode("_STR", str);
493 }
494 
dptf_write_fan_options(bool fine_grained,int step_size,bool low_speed_notify)495 void 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 
dptf_write_tsr_hysteresis(uint8_t hysteresis)507 void dptf_write_tsr_hysteresis(uint8_t hysteresis)
508 {
509 	if (!hysteresis)
510 		return;
511 
512 	acpigen_write_name_integer("GTSH", hysteresis);
513 }
514 
dptf_write_enabled_policies(const struct dptf_active_policy * active_policies,int active_count,const struct dptf_passive_policy * passive_policies,int passive_count,const struct dptf_critical_policy * critical_policies,int critical_count)515 void 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 
535 	acpigen_write_scope(DPTF_DEVICE_PATH);
536 	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 }
551