GNU Linux-libre 4.19.264-gnu1
[releases.git] / tools / thermal / tmon / sysfs.c
1 /*
2  * sysfs.c sysfs ABI access functions for TMON program
3  *
4  * Copyright (C) 2013 Intel Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version
8  * 2 or later as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
16  *
17  */
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <dirent.h>
24 #include <libintl.h>
25 #include <limits.h>
26 #include <ctype.h>
27 #include <time.h>
28 #include <syslog.h>
29 #include <sys/time.h>
30 #include <errno.h>
31
32 #include "tmon.h"
33
34 struct tmon_platform_data ptdata;
35 const char *trip_type_name[] = {
36         "critical",
37         "hot",
38         "passive",
39         "active",
40 };
41
42 int sysfs_set_ulong(char *path, char *filename, unsigned long val)
43 {
44         FILE *fd;
45         int ret = -1;
46         char filepath[PATH_MAX + 2]; /* NUL and '/' */
47
48         snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
49
50         fd = fopen(filepath, "w");
51         if (!fd) {
52                 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
53                 return ret;
54         }
55         ret = fprintf(fd, "%lu", val);
56         fclose(fd);
57
58         return 0;
59 }
60
61 /* history of thermal data, used for control algo */
62 #define NR_THERMAL_RECORDS 3
63 struct thermal_data_record trec[NR_THERMAL_RECORDS];
64 int cur_thermal_record; /* index to the trec array */
65
66 static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong)
67 {
68         FILE *fd;
69         int ret = -1;
70         char filepath[PATH_MAX + 2]; /* NUL and '/' */
71
72         snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
73
74         fd = fopen(filepath, "r");
75         if (!fd) {
76                 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
77                 return ret;
78         }
79         ret = fscanf(fd, "%lu", p_ulong);
80         fclose(fd);
81
82         return 0;
83 }
84
85 static int sysfs_get_string(char *path, char *filename, char *str)
86 {
87         FILE *fd;
88         int ret = -1;
89         char filepath[PATH_MAX + 2]; /* NUL and '/' */
90
91         snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
92
93         fd = fopen(filepath, "r");
94         if (!fd) {
95                 syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath);
96                 return ret;
97         }
98         ret = fscanf(fd, "%256s", str);
99         fclose(fd);
100
101         return ret;
102 }
103
104 /* get states of the cooling device instance */
105 static int probe_cdev(struct cdev_info *cdi, char *path)
106 {
107         sysfs_get_string(path, "type", cdi->type);
108         sysfs_get_ulong(path, "max_state",  &cdi->max_state);
109         sysfs_get_ulong(path, "cur_state", &cdi->cur_state);
110
111         syslog(LOG_INFO, "%s: %s: type %s, max %lu, curr %lu inst %d\n",
112                 __func__, path,
113                 cdi->type, cdi->max_state, cdi->cur_state, cdi->instance);
114
115         return 0;
116 }
117
118 static int str_to_trip_type(char *name)
119 {
120         int i;
121
122         for (i = 0; i < NR_THERMAL_TRIP_TYPE; i++) {
123                 if (!strcmp(name, trip_type_name[i]))
124                         return i;
125         }
126
127         return -ENOENT;
128 }
129
130 /* scan and fill in trip point info for a thermal zone and trip point id */
131 static int get_trip_point_data(char *tz_path, int tzid, int tpid)
132 {
133         char filename[256];
134         char temp_str[256];
135         int trip_type;
136
137         if (tpid >= MAX_NR_TRIP)
138                 return -EINVAL;
139         /* check trip point type */
140         snprintf(filename, sizeof(filename), "trip_point_%d_type", tpid);
141         sysfs_get_string(tz_path, filename, temp_str);
142         trip_type = str_to_trip_type(temp_str);
143         if (trip_type < 0) {
144                 syslog(LOG_ERR, "%s:%s no matching type\n", __func__, temp_str);
145                 return -ENOENT;
146         }
147         ptdata.tzi[tzid].tp[tpid].type = trip_type;
148         syslog(LOG_INFO, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__, tzid,
149                 tpid, temp_str, trip_type);
150
151         /* TODO: check attribute */
152
153         return 0;
154 }
155
156 /* return instance id for file format such as trip_point_4_temp */
157 static int get_instance_id(char *name, int pos, int skip)
158 {
159         char *ch;
160         int i = 0;
161
162         ch = strtok(name, "_");
163         while (ch != NULL) {
164                 ++i;
165                 syslog(LOG_INFO, "%s:%s:%s:%d", __func__, name, ch, i);
166                 ch = strtok(NULL, "_");
167                 if (pos == i)
168                         return atol(ch + skip);
169         }
170
171         return -1;
172 }
173
174 /* Find trip point info of a thermal zone */
175 static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi,
176                         int tz_id)
177 {
178         int tp_id;
179         unsigned long temp_ulong;
180
181         if (strstr(d_name, "trip_point") &&
182                 strstr(d_name, "temp")) {
183                 /* check if trip point temp is non-zero
184                  * ignore 0/invalid trip points
185                  */
186                 sysfs_get_ulong(tz_name, d_name, &temp_ulong);
187                 if (temp_ulong < MAX_TEMP_KC) {
188                         tzi->nr_trip_pts++;
189                         /* found a valid trip point */
190                         tp_id = get_instance_id(d_name, 2, 0);
191                         syslog(LOG_DEBUG, "tzone %s trip %d temp %lu tpnode %s",
192                                 tz_name, tp_id, temp_ulong, d_name);
193                         if (tp_id < 0 || tp_id >= MAX_NR_TRIP) {
194                                 syslog(LOG_ERR, "Failed to find TP inst %s\n",
195                                         d_name);
196                                 return -1;
197                         }
198                         get_trip_point_data(tz_name, tz_id, tp_id);
199                         tzi->tp[tp_id].temp = temp_ulong;
200                 }
201         }
202
203         return 0;
204 }
205
206 /* check cooling devices for binding info. */
207 static int find_tzone_cdev(struct dirent *nl, char *tz_name,
208                         struct tz_info *tzi, int tz_id, int cid)
209 {
210         unsigned long trip_instance = 0;
211         char cdev_name_linked[256];
212         char cdev_name[PATH_MAX];
213         char cdev_trip_name[PATH_MAX];
214         int cdev_id;
215
216         if (nl->d_type == DT_LNK) {
217                 syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name,
218                         cid);
219                 tzi->nr_cdev++;
220                 if (tzi->nr_cdev > ptdata.nr_cooling_dev) {
221                         syslog(LOG_ERR, "Err: Too many cdev? %d\n",
222                                 tzi->nr_cdev);
223                         return -EINVAL;
224                 }
225                 /* find the link to real cooling device record binding */
226                 snprintf(cdev_name, sizeof(cdev_name) - 2, "%s/%s",
227                          tz_name, nl->d_name);
228                 memset(cdev_name_linked, 0, sizeof(cdev_name_linked));
229                 if (readlink(cdev_name, cdev_name_linked,
230                                 sizeof(cdev_name_linked) - 1) != -1) {
231                         cdev_id = get_instance_id(cdev_name_linked, 1,
232                                                 sizeof("device") - 1);
233                         syslog(LOG_DEBUG, "cdev %s linked to %s : %d\n",
234                                 cdev_name, cdev_name_linked, cdev_id);
235                         tzi->cdev_binding |= (1 << cdev_id);
236
237                         /* find the trip point in which the cdev is binded to
238                          * in this tzone
239                          */
240                         snprintf(cdev_trip_name, sizeof(cdev_trip_name) - 1,
241                                 "%s%s", nl->d_name, "_trip_point");
242                         sysfs_get_ulong(tz_name, cdev_trip_name,
243                                         &trip_instance);
244                         /* validate trip point range, e.g. trip could return -1
245                          * when passive is enabled
246                          */
247                         if (trip_instance > MAX_NR_TRIP)
248                                 trip_instance = 0;
249                         tzi->trip_binding[cdev_id] |= 1 << trip_instance;
250                         syslog(LOG_DEBUG, "cdev %s -> trip:%lu: 0x%lx %d\n",
251                                 cdev_name, trip_instance,
252                                 tzi->trip_binding[cdev_id],
253                                 cdev_id);
254
255
256                 }
257                 return 0;
258         }
259
260         return -ENODEV;
261 }
262
263
264
265 /*****************************************************************************
266  * Before calling scan_tzones, thermal sysfs must be probed to determine
267  * the number of thermal zones and cooling devices.
268  * We loop through each thermal zone and fill in tz_info struct, i.e.
269  * ptdata.tzi[]
270 root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0
271 /sys/class/thermal/thermal_zone0
272 |-- cdev0 -> ../cooling_device4
273 |-- cdev1 -> ../cooling_device3
274 |-- cdev10 -> ../cooling_device7
275 |-- cdev11 -> ../cooling_device6
276 |-- cdev12 -> ../cooling_device5
277 |-- cdev2 -> ../cooling_device2
278 |-- cdev3 -> ../cooling_device1
279 |-- cdev4 -> ../cooling_device0
280 |-- cdev5 -> ../cooling_device12
281 |-- cdev6 -> ../cooling_device11
282 |-- cdev7 -> ../cooling_device10
283 |-- cdev8 -> ../cooling_device9
284 |-- cdev9 -> ../cooling_device8
285 |-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00
286 |-- power
287 `-- subsystem -> ../../../../class/thermal
288 *****************************************************************************/
289 static int scan_tzones(void)
290 {
291         DIR *dir;
292         struct dirent **namelist;
293         char tz_name[256];
294         int i, j, n, k = 0;
295
296         if (!ptdata.nr_tz_sensor)
297                 return -1;
298
299         for (i = 0; i <= ptdata.max_tz_instance; i++) {
300                 memset(tz_name, 0, sizeof(tz_name));
301                 snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, i);
302
303                 dir = opendir(tz_name);
304                 if (!dir) {
305                         syslog(LOG_INFO, "Thermal zone %s skipped\n", tz_name);
306                         continue;
307                 }
308                 /* keep track of valid tzones */
309                 n = scandir(tz_name, &namelist, 0, alphasort);
310                 if (n < 0)
311                         syslog(LOG_ERR, "scandir failed in %s",  tz_name);
312                 else {
313                         sysfs_get_string(tz_name, "type", ptdata.tzi[k].type);
314                         ptdata.tzi[k].instance = i;
315                         /* detect trip points and cdev attached to this tzone */
316                         j = 0; /* index for cdev */
317                         ptdata.tzi[k].nr_cdev = 0;
318                         ptdata.tzi[k].nr_trip_pts = 0;
319                         while (n--) {
320                                 char *temp_str;
321
322                                 if (find_tzone_tp(tz_name, namelist[n]->d_name,
323                                                         &ptdata.tzi[k], k))
324                                         break;
325                                 temp_str = strstr(namelist[n]->d_name, "cdev");
326                                 if (!temp_str) {
327                                         free(namelist[n]);
328                                         continue;
329                                 }
330                                 if (!find_tzone_cdev(namelist[n], tz_name,
331                                                         &ptdata.tzi[k], i, j))
332                                         j++; /* increment cdev index */
333                                 free(namelist[n]);
334                         }
335                         free(namelist);
336                 }
337                 /*TODO: reverse trip points */
338                 closedir(dir);
339                 syslog(LOG_INFO, "TZ %d has %d cdev\n", i,
340                         ptdata.tzi[k].nr_cdev);
341                 k++;
342         }
343
344         return 0;
345 }
346
347 static int scan_cdevs(void)
348 {
349         DIR *dir;
350         struct dirent **namelist;
351         char cdev_name[256];
352         int i, n, k = 0;
353
354         if (!ptdata.nr_cooling_dev) {
355                 fprintf(stderr, "No cooling devices found\n");
356                 return 0;
357         }
358         for (i = 0; i <= ptdata.max_cdev_instance; i++) {
359                 memset(cdev_name, 0, sizeof(cdev_name));
360                 snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, i);
361
362                 dir = opendir(cdev_name);
363                 if (!dir) {
364                         syslog(LOG_INFO, "Cooling dev %s skipped\n", cdev_name);
365                         /* there is a gap in cooling device id, check again
366                          * for the same index.
367                          */
368                         continue;
369                 }
370
371                 n = scandir(cdev_name, &namelist, 0, alphasort);
372                 if (n < 0)
373                         syslog(LOG_ERR, "scandir failed in %s",  cdev_name);
374                 else {
375                         sysfs_get_string(cdev_name, "type", ptdata.cdi[k].type);
376                         ptdata.cdi[k].instance = i;
377                         if (strstr(ptdata.cdi[k].type, ctrl_cdev)) {
378                                 ptdata.cdi[k].flag |= CDEV_FLAG_IN_CONTROL;
379                                 syslog(LOG_DEBUG, "control cdev id %d\n", i);
380                         }
381                         while (n--)
382                                 free(namelist[n]);
383                         free(namelist);
384                 }
385                 closedir(dir);
386                 k++;
387         }
388         return 0;
389 }
390
391
392 int probe_thermal_sysfs(void)
393 {
394         DIR *dir;
395         struct dirent **namelist;
396         int n;
397
398         dir = opendir(THERMAL_SYSFS);
399         if (!dir) {
400                 fprintf(stderr, "\nNo thermal sysfs, exit\n");
401                 return -1;
402         }
403         n = scandir(THERMAL_SYSFS, &namelist, 0, alphasort);
404         if (n < 0)
405                 syslog(LOG_ERR, "scandir failed in thermal sysfs");
406         else {
407                 /* detect number of thermal zones and cooling devices */
408                 while (n--) {
409                         int inst;
410
411                         if (strstr(namelist[n]->d_name, CDEV)) {
412                                 inst = get_instance_id(namelist[n]->d_name, 1,
413                                                 sizeof("device") - 1);
414                                 /* keep track of the max cooling device since
415                                  * there may be gaps.
416                                  */
417                                 if (inst > ptdata.max_cdev_instance)
418                                         ptdata.max_cdev_instance = inst;
419
420                                 syslog(LOG_DEBUG, "found cdev: %s %d %d\n",
421                                         namelist[n]->d_name,
422                                         ptdata.nr_cooling_dev,
423                                         ptdata.max_cdev_instance);
424                                 ptdata.nr_cooling_dev++;
425                         } else if (strstr(namelist[n]->d_name, TZONE)) {
426                                 inst = get_instance_id(namelist[n]->d_name, 1,
427                                                 sizeof("zone") - 1);
428                                 if (inst > ptdata.max_tz_instance)
429                                         ptdata.max_tz_instance = inst;
430
431                                 syslog(LOG_DEBUG, "found tzone: %s %d %d\n",
432                                         namelist[n]->d_name,
433                                         ptdata.nr_tz_sensor,
434                                         ptdata.max_tz_instance);
435                                 ptdata.nr_tz_sensor++;
436                         }
437                         free(namelist[n]);
438                 }
439                 free(namelist);
440         }
441         syslog(LOG_INFO, "found %d tzone(s), %d cdev(s), target zone %d\n",
442                 ptdata.nr_tz_sensor, ptdata.nr_cooling_dev,
443                 target_thermal_zone);
444         closedir(dir);
445
446         if (!ptdata.nr_tz_sensor) {
447                 fprintf(stderr, "\nNo thermal zones found, exit\n\n");
448                 return -1;
449         }
450
451         ptdata.tzi = calloc(ptdata.max_tz_instance+1, sizeof(struct tz_info));
452         if (!ptdata.tzi) {
453                 fprintf(stderr, "Err: allocate tz_info\n");
454                 return -1;
455         }
456
457         /* we still show thermal zone information if there is no cdev */
458         if (ptdata.nr_cooling_dev) {
459                 ptdata.cdi = calloc(ptdata.max_cdev_instance + 1,
460                                 sizeof(struct cdev_info));
461                 if (!ptdata.cdi) {
462                         free(ptdata.tzi);
463                         fprintf(stderr, "Err: allocate cdev_info\n");
464                         return -1;
465                 }
466         }
467
468         /* now probe tzones */
469         if (scan_tzones())
470                 return -1;
471         if (scan_cdevs())
472                 return -1;
473         return 0;
474 }
475
476 /* convert sysfs zone instance to zone array index */
477 int zone_instance_to_index(int zone_inst)
478 {
479         int i;
480
481         for (i = 0; i < ptdata.nr_tz_sensor; i++)
482                 if (ptdata.tzi[i].instance == zone_inst)
483                         return i;
484         return -ENOENT;
485 }
486
487 /* read temperature of all thermal zones */
488 int update_thermal_data()
489 {
490         int i;
491         int next_thermal_record = cur_thermal_record + 1;
492         char tz_name[256];
493         static unsigned long samples;
494
495         if (!ptdata.nr_tz_sensor) {
496                 syslog(LOG_ERR, "No thermal zones found!\n");
497                 return -1;
498         }
499
500         /* circular buffer for keeping historic data */
501         if (next_thermal_record >= NR_THERMAL_RECORDS)
502                 next_thermal_record = 0;
503         gettimeofday(&trec[next_thermal_record].tv, NULL);
504         if (tmon_log) {
505                 fprintf(tmon_log, "%lu ", ++samples);
506                 fprintf(tmon_log, "%3.1f ", p_param.t_target);
507         }
508         for (i = 0; i < ptdata.nr_tz_sensor; i++) {
509                 memset(tz_name, 0, sizeof(tz_name));
510                 snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE,
511                         ptdata.tzi[i].instance);
512                 sysfs_get_ulong(tz_name, "temp",
513                                 &trec[next_thermal_record].temp[i]);
514                 if (tmon_log)
515                         fprintf(tmon_log, "%lu ",
516                                 trec[next_thermal_record].temp[i] / 1000);
517         }
518         cur_thermal_record = next_thermal_record;
519         for (i = 0; i < ptdata.nr_cooling_dev; i++) {
520                 char cdev_name[256];
521                 unsigned long val;
522
523                 snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV,
524                         ptdata.cdi[i].instance);
525                 probe_cdev(&ptdata.cdi[i], cdev_name);
526                 val = ptdata.cdi[i].cur_state;
527                 if (val > 1000000)
528                         val = 0;
529                 if (tmon_log)
530                         fprintf(tmon_log, "%lu ", val);
531         }
532
533         if (tmon_log) {
534                 fprintf(tmon_log, "\n");
535                 fflush(tmon_log);
536         }
537
538         return 0;
539 }
540
541 void set_ctrl_state(unsigned long state)
542 {
543         char ctrl_cdev_path[256];
544         int i;
545         unsigned long cdev_state;
546
547         if (no_control)
548                 return;
549         /* set all ctrl cdev to the same state */
550         for (i = 0; i < ptdata.nr_cooling_dev; i++) {
551                 if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
552                         if (ptdata.cdi[i].max_state < 10) {
553                                 strcpy(ctrl_cdev, "None.");
554                                 return;
555                         }
556                         /* scale to percentage of max_state */
557                         cdev_state = state * ptdata.cdi[i].max_state/100;
558                         syslog(LOG_DEBUG,
559                                 "ctrl cdev %d set state %lu scaled to %lu\n",
560                                 ptdata.cdi[i].instance, state, cdev_state);
561                         snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
562                                 CDEV, ptdata.cdi[i].instance);
563                         syslog(LOG_DEBUG, "ctrl cdev path %s", ctrl_cdev_path);
564                         sysfs_set_ulong(ctrl_cdev_path, "cur_state",
565                                         cdev_state);
566                 }
567         }
568 }
569
570 void get_ctrl_state(unsigned long *state)
571 {
572         char ctrl_cdev_path[256];
573         int ctrl_cdev_id = -1;
574         int i;
575
576         /* TODO: take average of all ctrl types. also consider change based on
577          * uevent. Take the first reading for now.
578          */
579         for (i = 0; i < ptdata.nr_cooling_dev; i++) {
580                 if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) {
581                         ctrl_cdev_id = ptdata.cdi[i].instance;
582                         syslog(LOG_INFO, "ctrl cdev %d get state\n",
583                                 ptdata.cdi[i].instance);
584                         break;
585                 }
586         }
587         if (ctrl_cdev_id == -1) {
588                 *state = 0;
589                 return;
590         }
591         snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS,
592                 CDEV, ctrl_cdev_id);
593         sysfs_get_ulong(ctrl_cdev_path, "cur_state", state);
594 }
595
596 void free_thermal_data(void)
597 {
598         free(ptdata.tzi);
599         free(ptdata.cdi);
600 }