| 1 |
|
| 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 |
|
| 10 |
#define DEFAULT_RAW_UNIT "ma" |
| 11 |
|
| 12 |
|
| 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 |
|
| 30 |
static int to_acpi_temp(int deg_c) |
| 31 |
{ |
| 32 |
return deg_c * 10 + 2732; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
static int to_acpi_time(int ms) |
| 37 |
{ |
| 38 |
return ms / 100; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 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(); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
static void write_zeros(int count) |
| 51 |
{ |
| 52 |
for (; count; --count) |
| 53 |
acpigen_write_integer(0); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 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 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 117 |
void dptf_write_scope(enum dptf_participant participant) |
| 118 |
{ |
| 119 |
acpigen_write_scope(scope_of(participant)); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 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 |
|
| 142 |
acpigen_emit_byte(RETURN_OP); |
| 143 |
|
| 144 |
|
| 145 |
pkg_count = acpigen_write_package(1); |
| 146 |
acpigen_write_integer(ART_REVISION); |
| 147 |
|
| 148 |
for (i = 0; i < max_count; ++i) { |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if (policies[i].target == DPTF_NONE) |
| 154 |
break; |
| 155 |
|
| 156 |
(*pkg_count)++; |
| 157 |
|
| 158 |
|
| 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 |
|
| 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(); |
| 173 |
} |
| 174 |
|
| 175 |
acpigen_pop_len(); |
| 176 |
acpigen_pop_len(); |
| 177 |
acpigen_pop_len(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 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 |
|
| 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 |
|
| 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(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 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 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 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 |
|
| 235 |
if (!max_count || policies[0].source == DPTF_NONE) |
| 236 |
return; |
| 237 |
|
| 238 |
acpigen_write_scope(DPTF_DEVICE_PATH); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
acpigen_write_name_integer("TRTR", 1); |
| 247 |
|
| 248 |
|
| 249 |
acpigen_write_method("_TRT", 0); |
| 250 |
|
| 251 |
|
| 252 |
acpigen_emit_byte(RETURN_OP); |
| 253 |
pkg_count = acpigen_write_package(0); |
| 254 |
|
| 255 |
for (i = 0; i < max_count; ++i) { |
| 256 |
|
| 257 |
if (policies[i].source == DPTF_NONE) |
| 258 |
break; |
| 259 |
|
| 260 |
|
| 261 |
(*pkg_count)++; |
| 262 |
|
| 263 |
acpigen_write_package(8); |
| 264 |
|
| 265 |
|
| 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 |
|
| 272 |
write_zeros(4); |
| 273 |
|
| 274 |
acpigen_pop_len(); |
| 275 |
} |
| 276 |
|
| 277 |
acpigen_pop_len(); |
| 278 |
acpigen_pop_len(); |
| 279 |
acpigen_pop_len(); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 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(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 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 |
|
| 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 |
|
| 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(); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 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 |
|
| 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 |
|
| 347 |
|
| 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(); |
| 356 |
} |
| 357 |
|
| 358 |
acpigen_pop_len(); |
| 359 |
acpigen_pop_len(); |
| 360 |
acpigen_pop_len(); |
| 361 |
} |
| 362 |
|
| 363 |
int dptf_write_fan_perf_fps(uint8_t percent, uint16_t power, uint16_t speed, |
| 364 |
uint16_t noise_level) |
| 365 |
{ |
| 366 |
|
| 367 |
|
| 368 |
|
| 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(); |
| 380 |
|
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 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 |
|
| 396 |
acpigen_write_name("_FPS"); |
| 397 |
|
| 398 |
pkg_count = acpigen_write_package(1); |
| 399 |
acpigen_write_integer(FPS_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(); |
| 409 |
acpigen_pop_len(); |
| 410 |
} |
| 411 |
|
| 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 |
|
| 426 |
acpigen_write_name("_FPS"); |
| 427 |
|
| 428 |
pkg_count = acpigen_write_package(1); |
| 429 |
acpigen_write_integer(FPS_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(); |
| 439 |
acpigen_pop_len(); |
| 440 |
} |
| 441 |
|
| 442 |
void dptf_write_power_limits(const struct dptf_power_limits *limits) |
| 443 |
{ |
| 444 |
char *pkg_count; |
| 445 |
|
| 446 |
|
| 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); |
| 456 |
acpigen_write_integer(PPCC_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(); |
| 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(); |
| 480 |
} |
| 481 |
|
| 482 |
acpigen_pop_len(); |
| 483 |
acpigen_pop_len(); |
| 484 |
acpigen_pop_len(); |
| 485 |
} |
| 486 |
|
| 487 |
void dptf_write_STR(const char *str) |
| 488 |
{ |
| 489 |
if (!str) |
| 490 |
return; |
| 491 |
|
| 492 |
acpigen_write_name_unicode("_STR", str); |
| 493 |
} |
| 494 |
|
| 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); |
| 501 |
acpigen_write_integer(fine_grained); |
| 502 |
acpigen_write_integer(step_size); |
| 503 |
acpigen_write_integer(low_speed_notify); |
| 504 |
acpigen_pop_len(); |
| 505 |
} |
| 506 |
|
| 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 |
|
| 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(); |
| 549 |
acpigen_pop_len(); |
| 550 |
} |