Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * thermal.c - sysfs interface of thermal devices
4 *
5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6 *
7 * Highly based on original thermal_core.c
8 * Copyright (C) 2008 Intel Corp
9 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
10 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/container_of.h>
16#include <linux/sysfs.h>
17#include <linux/device.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/string.h>
21#include <linux/jiffies.h>
22
23#include "thermal_core.h"
24
25/* sys I/F for thermal zone */
26
27static ssize_t
28type_show(struct device *dev, struct device_attribute *attr, char *buf)
29{
30 struct thermal_zone_device *tz = to_thermal_zone(dev);
31
32 return sprintf(buf, "%s\n", tz->type);
33}
34
35static ssize_t
36temp_show(struct device *dev, struct device_attribute *attr, char *buf)
37{
38 struct thermal_zone_device *tz = to_thermal_zone(dev);
39 int temperature, ret;
40
41 ret = thermal_zone_get_temp(tz, &temperature);
42
43 if (ret)
44 return ret;
45
46 return sprintf(buf, "%d\n", temperature);
47}
48
49static ssize_t
50mode_show(struct device *dev, struct device_attribute *attr, char *buf)
51{
52 struct thermal_zone_device *tz = to_thermal_zone(dev);
53
54 guard(thermal_zone)(tz);
55
56 if (tz->mode == THERMAL_DEVICE_ENABLED)
57 return sprintf(buf, "enabled\n");
58
59 return sprintf(buf, "disabled\n");
60}
61
62static ssize_t
63mode_store(struct device *dev, struct device_attribute *attr,
64 const char *buf, size_t count)
65{
66 struct thermal_zone_device *tz = to_thermal_zone(dev);
67 int result;
68
69 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
70 result = thermal_zone_device_enable(tz);
71 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
72 result = thermal_zone_device_disable(tz);
73 else
74 result = -EINVAL;
75
76 if (result)
77 return result;
78
79 return count;
80}
81
82#define thermal_trip_of_attr(_ptr_, _attr_) \
83 ({ \
84 struct thermal_trip_desc *td; \
85 \
86 td = container_of(_ptr_, struct thermal_trip_desc, \
87 trip_attrs._attr_.attr); \
88 &td->trip; \
89 })
90
91static ssize_t
92trip_point_type_show(struct device *dev, struct device_attribute *attr,
93 char *buf)
94{
95 struct thermal_trip *trip = thermal_trip_of_attr(attr, type);
96
97 return sprintf(buf, "%s\n", thermal_trip_type_name(trip->type));
98}
99
100static ssize_t
101trip_point_temp_store(struct device *dev, struct device_attribute *attr,
102 const char *buf, size_t count)
103{
104 struct thermal_trip *trip = thermal_trip_of_attr(attr, temp);
105 struct thermal_zone_device *tz = to_thermal_zone(dev);
106 int temp;
107
108 if (kstrtoint(buf, 10, &temp))
109 return -EINVAL;
110
111 guard(thermal_zone)(tz);
112
113 if (temp == trip->temperature)
114 return count;
115
116 /* Arrange the condition to avoid integer overflows. */
117 if (temp != THERMAL_TEMP_INVALID &&
118 temp <= trip->hysteresis + THERMAL_TEMP_INVALID)
119 return -EINVAL;
120
121 if (tz->ops.set_trip_temp) {
122 int ret;
123
124 ret = tz->ops.set_trip_temp(tz, trip, temp);
125 if (ret)
126 return ret;
127 }
128
129 thermal_zone_set_trip_temp(tz, trip, temp);
130
131 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
132
133 return count;
134}
135
136static ssize_t
137trip_point_temp_show(struct device *dev, struct device_attribute *attr,
138 char *buf)
139{
140 struct thermal_trip *trip = thermal_trip_of_attr(attr, temp);
141
142 return sprintf(buf, "%d\n", READ_ONCE(trip->temperature));
143}
144
145static ssize_t
146trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t count)
148{
149 struct thermal_trip *trip = thermal_trip_of_attr(attr, hyst);
150 struct thermal_zone_device *tz = to_thermal_zone(dev);
151 int hyst;
152
153 if (kstrtoint(buf, 10, &hyst) || hyst < 0)
154 return -EINVAL;
155
156 guard(thermal_zone)(tz);
157
158 if (hyst == trip->hysteresis)
159 return count;
160
161 /*
162 * Allow the hysteresis to be updated when the temperature is invalid
163 * to allow user space to avoid having to adjust hysteresis after a
164 * valid temperature has been set, but in that case just change the
165 * value and do nothing else.
166 */
167 if (trip->temperature == THERMAL_TEMP_INVALID) {
168 WRITE_ONCE(trip->hysteresis, hyst);
169 return count;
170 }
171
172 if (trip->temperature - hyst <= THERMAL_TEMP_INVALID)
173 return -EINVAL;
174
175 thermal_zone_set_trip_hyst(tz, trip, hyst);
176
177 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
178
179 return count;
180}
181
182static ssize_t
183trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
184 char *buf)
185{
186 struct thermal_trip *trip = thermal_trip_of_attr(attr, hyst);
187
188 return sprintf(buf, "%d\n", READ_ONCE(trip->hysteresis));
189}
190
191static ssize_t
192policy_store(struct device *dev, struct device_attribute *attr,
193 const char *buf, size_t count)
194{
195 struct thermal_zone_device *tz = to_thermal_zone(dev);
196 char name[THERMAL_NAME_LENGTH];
197 int ret;
198
199 snprintf(name, sizeof(name), "%s", buf);
200
201 ret = thermal_zone_device_set_policy(tz, name);
202 if (!ret)
203 ret = count;
204
205 return ret;
206}
207
208static ssize_t
209policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
210{
211 struct thermal_zone_device *tz = to_thermal_zone(dev);
212
213 return sprintf(buf, "%s\n", tz->governor->name);
214}
215
216static ssize_t
217available_policies_show(struct device *dev, struct device_attribute *devattr,
218 char *buf)
219{
220 return thermal_build_list_of_policies(buf);
221}
222
223#if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
224static ssize_t
225emul_temp_store(struct device *dev, struct device_attribute *attr,
226 const char *buf, size_t count)
227{
228 struct thermal_zone_device *tz = to_thermal_zone(dev);
229 int temperature;
230
231 if (kstrtoint(buf, 10, &temperature))
232 return -EINVAL;
233
234 guard(thermal_zone)(tz);
235
236 if (tz->ops.set_emul_temp) {
237 int ret;
238
239 ret = tz->ops.set_emul_temp(tz, temperature);
240 if (ret)
241 return ret;
242 } else {
243 tz->emul_temperature = temperature;
244 }
245
246 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
247
248 return count;
249}
250static DEVICE_ATTR_WO(emul_temp);
251#endif
252
253static ssize_t
254sustainable_power_show(struct device *dev, struct device_attribute *devattr,
255 char *buf)
256{
257 struct thermal_zone_device *tz = to_thermal_zone(dev);
258
259 if (tz->tzp)
260 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
261 else
262 return -EIO;
263}
264
265static ssize_t
266sustainable_power_store(struct device *dev, struct device_attribute *devattr,
267 const char *buf, size_t count)
268{
269 struct thermal_zone_device *tz = to_thermal_zone(dev);
270 u32 sustainable_power;
271
272 if (!tz->tzp)
273 return -EIO;
274
275 if (kstrtou32(buf, 10, &sustainable_power))
276 return -EINVAL;
277
278 tz->tzp->sustainable_power = sustainable_power;
279
280 return count;
281}
282
283#define create_s32_tzp_attr(name) \
284 static ssize_t \
285 name##_show(struct device *dev, struct device_attribute *devattr, \
286 char *buf) \
287 { \
288 struct thermal_zone_device *tz = to_thermal_zone(dev); \
289 \
290 if (tz->tzp) \
291 return sprintf(buf, "%d\n", tz->tzp->name); \
292 else \
293 return -EIO; \
294 } \
295 \
296 static ssize_t \
297 name##_store(struct device *dev, struct device_attribute *devattr, \
298 const char *buf, size_t count) \
299 { \
300 struct thermal_zone_device *tz = to_thermal_zone(dev); \
301 s32 value; \
302 \
303 if (!tz->tzp) \
304 return -EIO; \
305 \
306 if (kstrtos32(buf, 10, &value)) \
307 return -EINVAL; \
308 \
309 tz->tzp->name = value; \
310 \
311 return count; \
312 } \
313 static DEVICE_ATTR_RW(name)
314
315create_s32_tzp_attr(k_po);
316create_s32_tzp_attr(k_pu);
317create_s32_tzp_attr(k_i);
318create_s32_tzp_attr(k_d);
319create_s32_tzp_attr(integral_cutoff);
320create_s32_tzp_attr(slope);
321create_s32_tzp_attr(offset);
322#undef create_s32_tzp_attr
323
324/*
325 * These are thermal zone device attributes that will always be present.
326 * All the attributes created for tzp (create_s32_tzp_attr) also are always
327 * present on the sysfs interface.
328 */
329static DEVICE_ATTR_RO(type);
330static DEVICE_ATTR_RO(temp);
331static DEVICE_ATTR_RW(policy);
332static DEVICE_ATTR_RO(available_policies);
333static DEVICE_ATTR_RW(sustainable_power);
334
335/* These thermal zone device attributes are created based on conditions */
336static DEVICE_ATTR_RW(mode);
337
338/* These attributes are unconditionally added to a thermal zone */
339static struct attribute *thermal_zone_dev_attrs[] = {
340 &dev_attr_type.attr,
341 &dev_attr_temp.attr,
342#if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
343 &dev_attr_emul_temp.attr,
344#endif
345 &dev_attr_policy.attr,
346 &dev_attr_available_policies.attr,
347 &dev_attr_sustainable_power.attr,
348 &dev_attr_k_po.attr,
349 &dev_attr_k_pu.attr,
350 &dev_attr_k_i.attr,
351 &dev_attr_k_d.attr,
352 &dev_attr_integral_cutoff.attr,
353 &dev_attr_slope.attr,
354 &dev_attr_offset.attr,
355 NULL,
356};
357
358static const struct attribute_group thermal_zone_attribute_group = {
359 .attrs = thermal_zone_dev_attrs,
360};
361
362static struct attribute *thermal_zone_mode_attrs[] = {
363 &dev_attr_mode.attr,
364 NULL,
365};
366
367static const struct attribute_group thermal_zone_mode_attribute_group = {
368 .attrs = thermal_zone_mode_attrs,
369};
370
371static const struct attribute_group *thermal_zone_attribute_groups[] = {
372 &thermal_zone_attribute_group,
373 &thermal_zone_mode_attribute_group,
374 /* This is not NULL terminated as we create the group dynamically */
375};
376
377/**
378 * create_trip_attrs() - create attributes for trip points
379 * @tz: the thermal zone device
380 *
381 * helper function to instantiate sysfs entries for every trip
382 * point and its properties of a struct thermal_zone_device.
383 *
384 * Return: 0 on success, the proper error value otherwise.
385 */
386static int create_trip_attrs(struct thermal_zone_device *tz)
387{
388 struct thermal_trip_desc *td;
389 struct attribute **attrs;
390 int i;
391
392 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
393 if (!attrs)
394 return -ENOMEM;
395
396 i = 0;
397 for_each_trip_desc(tz, td) {
398 struct thermal_trip_attrs *trip_attrs = &td->trip_attrs;
399
400 /* create trip type attribute */
401 snprintf(trip_attrs->type.name, THERMAL_NAME_LENGTH,
402 "trip_point_%d_type", i);
403
404 sysfs_attr_init(&trip_attrs->type.attr.attr);
405 trip_attrs->type.attr.attr.name = trip_attrs->type.name;
406 trip_attrs->type.attr.attr.mode = S_IRUGO;
407 trip_attrs->type.attr.show = trip_point_type_show;
408 attrs[i] = &trip_attrs->type.attr.attr;
409
410 /* create trip temp attribute */
411 snprintf(trip_attrs->temp.name, THERMAL_NAME_LENGTH,
412 "trip_point_%d_temp", i);
413
414 sysfs_attr_init(&trip_attrs->temp.attr.attr);
415 trip_attrs->temp.attr.attr.name = trip_attrs->temp.name;
416 trip_attrs->temp.attr.attr.mode = S_IRUGO;
417 trip_attrs->temp.attr.show = trip_point_temp_show;
418 if (td->trip.flags & THERMAL_TRIP_FLAG_RW_TEMP) {
419 trip_attrs->temp.attr.attr.mode |= S_IWUSR;
420 trip_attrs->temp.attr.store = trip_point_temp_store;
421 }
422 attrs[i + tz->num_trips] = &trip_attrs->temp.attr.attr;
423
424 snprintf(trip_attrs->hyst.name, THERMAL_NAME_LENGTH,
425 "trip_point_%d_hyst", i);
426
427 sysfs_attr_init(&trip_attrs->hyst.attr.attr);
428 trip_attrs->hyst.attr.attr.name = trip_attrs->hyst.name;
429 trip_attrs->hyst.attr.attr.mode = S_IRUGO;
430 trip_attrs->hyst.attr.show = trip_point_hyst_show;
431 if (td->trip.flags & THERMAL_TRIP_FLAG_RW_HYST) {
432 trip_attrs->hyst.attr.attr.mode |= S_IWUSR;
433 trip_attrs->hyst.attr.store = trip_point_hyst_store;
434 }
435 attrs[i + 2 * tz->num_trips] = &trip_attrs->hyst.attr.attr;
436 i++;
437 }
438 attrs[tz->num_trips * 3] = NULL;
439
440 tz->trips_attribute_group.attrs = attrs;
441
442 return 0;
443}
444
445/**
446 * destroy_trip_attrs() - destroy attributes for trip points
447 * @tz: the thermal zone device
448 *
449 * helper function to free resources allocated by create_trip_attrs()
450 */
451static void destroy_trip_attrs(struct thermal_zone_device *tz)
452{
453 if (tz)
454 kfree(tz->trips_attribute_group.attrs);
455}
456
457int thermal_zone_create_device_groups(struct thermal_zone_device *tz)
458{
459 const struct attribute_group **groups;
460 int i, size, result;
461
462 /* we need one extra for trips and the NULL to terminate the array */
463 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
464 /* This also takes care of API requirement to be NULL terminated */
465 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
466 if (!groups)
467 return -ENOMEM;
468
469 for (i = 0; i < size - 2; i++)
470 groups[i] = thermal_zone_attribute_groups[i];
471
472 if (tz->num_trips) {
473 result = create_trip_attrs(tz);
474 if (result) {
475 kfree(groups);
476
477 return result;
478 }
479
480 groups[size - 2] = &tz->trips_attribute_group;
481 }
482
483 tz->device.groups = groups;
484
485 return 0;
486}
487
488void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
489{
490 if (!tz)
491 return;
492
493 if (tz->num_trips)
494 destroy_trip_attrs(tz);
495
496 kfree(tz->device.groups);
497}
498
499/* sys I/F for cooling device */
500static ssize_t
501cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
502{
503 struct thermal_cooling_device *cdev = to_cooling_device(dev);
504
505 return sprintf(buf, "%s\n", cdev->type);
506}
507
508static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
509 char *buf)
510{
511 struct thermal_cooling_device *cdev = to_cooling_device(dev);
512
513 return sprintf(buf, "%ld\n", cdev->max_state);
514}
515
516static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
517 char *buf)
518{
519 struct thermal_cooling_device *cdev = to_cooling_device(dev);
520 unsigned long state;
521 int ret;
522
523 ret = cdev->ops->get_cur_state(cdev, &state);
524 if (ret)
525 return ret;
526 return sprintf(buf, "%ld\n", state);
527}
528
529static ssize_t
530cur_state_store(struct device *dev, struct device_attribute *attr,
531 const char *buf, size_t count)
532{
533 struct thermal_cooling_device *cdev = to_cooling_device(dev);
534 unsigned long state;
535 int result;
536
537 if (sscanf(buf, "%ld\n", &state) != 1)
538 return -EINVAL;
539
540 if ((long)state < 0)
541 return -EINVAL;
542
543 /* Requested state should be less than max_state + 1 */
544 if (state > cdev->max_state)
545 return -EINVAL;
546
547 guard(cooling_dev)(cdev);
548
549 result = cdev->ops->set_cur_state(cdev, state);
550 if (result)
551 return result;
552
553 thermal_cooling_device_stats_update(cdev, state);
554
555 return count;
556}
557
558static struct device_attribute
559dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
560static DEVICE_ATTR_RO(max_state);
561static DEVICE_ATTR_RW(cur_state);
562
563static struct attribute *cooling_device_attrs[] = {
564 &dev_attr_cdev_type.attr,
565 &dev_attr_max_state.attr,
566 &dev_attr_cur_state.attr,
567 NULL,
568};
569
570static const struct attribute_group cooling_device_attr_group = {
571 .attrs = cooling_device_attrs,
572};
573
574static const struct attribute_group *cooling_device_attr_groups[] = {
575 &cooling_device_attr_group,
576 NULL, /* Space allocated for cooling_device_stats_attr_group */
577 NULL,
578};
579
580#ifdef CONFIG_THERMAL_STATISTICS
581struct cooling_dev_stats {
582 spinlock_t lock;
583 unsigned int total_trans;
584 unsigned long state;
585 ktime_t last_time;
586 ktime_t *time_in_state;
587 unsigned int *trans_table;
588};
589
590static void update_time_in_state(struct cooling_dev_stats *stats)
591{
592 ktime_t now = ktime_get(), delta;
593
594 delta = ktime_sub(now, stats->last_time);
595 stats->time_in_state[stats->state] =
596 ktime_add(stats->time_in_state[stats->state], delta);
597 stats->last_time = now;
598}
599
600void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
601 unsigned long new_state)
602{
603 struct cooling_dev_stats *stats = cdev->stats;
604
605 lockdep_assert_held(&cdev->lock);
606
607 if (!stats)
608 return;
609
610 spin_lock(&stats->lock);
611
612 if (stats->state == new_state)
613 goto unlock;
614
615 update_time_in_state(stats);
616 stats->trans_table[stats->state * (cdev->max_state + 1) + new_state]++;
617 stats->state = new_state;
618 stats->total_trans++;
619
620unlock:
621 spin_unlock(&stats->lock);
622}
623
624static ssize_t total_trans_show(struct device *dev,
625 struct device_attribute *attr, char *buf)
626{
627 struct thermal_cooling_device *cdev = to_cooling_device(dev);
628 struct cooling_dev_stats *stats;
629 int ret;
630
631 guard(cooling_dev)(cdev);
632
633 stats = cdev->stats;
634 if (!stats)
635 return 0;
636
637 spin_lock(&stats->lock);
638 ret = sprintf(buf, "%u\n", stats->total_trans);
639 spin_unlock(&stats->lock);
640
641 return ret;
642}
643
644static ssize_t
645time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
646 char *buf)
647{
648 struct thermal_cooling_device *cdev = to_cooling_device(dev);
649 struct cooling_dev_stats *stats;
650 ssize_t len = 0;
651 int i;
652
653 guard(cooling_dev)(cdev);
654
655 stats = cdev->stats;
656 if (!stats)
657 return 0;
658
659 spin_lock(&stats->lock);
660
661 update_time_in_state(stats);
662
663 for (i = 0; i <= cdev->max_state; i++) {
664 len += sprintf(buf + len, "state%u\t%llu\n", i,
665 ktime_to_ms(stats->time_in_state[i]));
666 }
667 spin_unlock(&stats->lock);
668
669 return len;
670}
671
672static ssize_t
673reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
674 size_t count)
675{
676 struct thermal_cooling_device *cdev = to_cooling_device(dev);
677 struct cooling_dev_stats *stats;
678 int i, states;
679
680 guard(cooling_dev)(cdev);
681
682 stats = cdev->stats;
683 if (!stats)
684 return count;
685
686 states = cdev->max_state + 1;
687
688 spin_lock(&stats->lock);
689
690 stats->total_trans = 0;
691 stats->last_time = ktime_get();
692 memset(stats->trans_table, 0,
693 states * states * sizeof(*stats->trans_table));
694
695 for (i = 0; i < states; i++)
696 stats->time_in_state[i] = ktime_set(0, 0);
697
698 spin_unlock(&stats->lock);
699
700 return count;
701}
702
703static ssize_t trans_table_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705{
706 struct thermal_cooling_device *cdev = to_cooling_device(dev);
707 struct cooling_dev_stats *stats;
708 ssize_t len = 0;
709 int i, j;
710
711 guard(cooling_dev)(cdev);
712
713 stats = cdev->stats;
714 if (!stats)
715 return -ENODATA;
716
717 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
718 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
719 for (i = 0; i <= cdev->max_state; i++) {
720 if (len >= PAGE_SIZE)
721 break;
722 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
723 }
724 if (len >= PAGE_SIZE)
725 return PAGE_SIZE;
726
727 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
728
729 for (i = 0; i <= cdev->max_state; i++) {
730 if (len >= PAGE_SIZE)
731 break;
732
733 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
734
735 for (j = 0; j <= cdev->max_state; j++) {
736 if (len >= PAGE_SIZE)
737 break;
738 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
739 stats->trans_table[i * (cdev->max_state + 1) + j]);
740 }
741 if (len >= PAGE_SIZE)
742 break;
743 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
744 }
745
746 if (len >= PAGE_SIZE) {
747 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
748 len = -EFBIG;
749 }
750
751 return len;
752}
753
754static DEVICE_ATTR_RO(total_trans);
755static DEVICE_ATTR_RO(time_in_state_ms);
756static DEVICE_ATTR_WO(reset);
757static DEVICE_ATTR_RO(trans_table);
758
759static struct attribute *cooling_device_stats_attrs[] = {
760 &dev_attr_total_trans.attr,
761 &dev_attr_time_in_state_ms.attr,
762 &dev_attr_reset.attr,
763 &dev_attr_trans_table.attr,
764 NULL
765};
766
767static const struct attribute_group cooling_device_stats_attr_group = {
768 .attrs = cooling_device_stats_attrs,
769 .name = "stats"
770};
771
772static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
773{
774 const struct attribute_group *stats_attr_group = NULL;
775 struct cooling_dev_stats *stats;
776 /* Total number of states is highest state + 1 */
777 unsigned long states = cdev->max_state + 1;
778 int var;
779
780 var = sizeof(*stats);
781 var += sizeof(*stats->time_in_state) * states;
782 var += sizeof(*stats->trans_table) * states * states;
783
784 stats = kzalloc(var, GFP_KERNEL);
785 if (!stats)
786 goto out;
787
788 stats->time_in_state = (ktime_t *)(stats + 1);
789 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
790 cdev->stats = stats;
791 stats->last_time = ktime_get();
792
793 spin_lock_init(&stats->lock);
794
795 stats_attr_group = &cooling_device_stats_attr_group;
796
797out:
798 /* Fill the empty slot left in cooling_device_attr_groups */
799 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
800 cooling_device_attr_groups[var] = stats_attr_group;
801}
802
803static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
804{
805 kfree(cdev->stats);
806 cdev->stats = NULL;
807}
808
809#else
810
811static inline void
812cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
813static inline void
814cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
815
816#endif /* CONFIG_THERMAL_STATISTICS */
817
818void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
819{
820 cooling_device_stats_setup(cdev);
821 cdev->device.groups = cooling_device_attr_groups;
822}
823
824void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
825{
826 cooling_device_stats_destroy(cdev);
827}
828
829void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev)
830{
831 lockdep_assert_held(&cdev->lock);
832
833 cooling_device_stats_destroy(cdev);
834 cooling_device_stats_setup(cdev);
835}
836
837/* these helper will be used only at the time of bindig */
838ssize_t
839trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
840{
841 struct thermal_zone_device *tz = to_thermal_zone(dev);
842 struct thermal_instance *instance;
843
844 instance = container_of(attr, struct thermal_instance, attr);
845
846 return sprintf(buf, "%d\n", thermal_zone_trip_id(tz, instance->trip));
847}
848
849ssize_t
850weight_show(struct device *dev, struct device_attribute *attr, char *buf)
851{
852 struct thermal_instance *instance;
853
854 instance = container_of(attr, struct thermal_instance, weight_attr);
855
856 return sprintf(buf, "%d\n", instance->weight);
857}
858
859ssize_t weight_store(struct device *dev, struct device_attribute *attr,
860 const char *buf, size_t count)
861{
862 struct thermal_zone_device *tz = to_thermal_zone(dev);
863 struct thermal_instance *instance;
864 int ret, weight;
865
866 ret = kstrtoint(buf, 0, &weight);
867 if (ret)
868 return ret;
869
870 instance = container_of(attr, struct thermal_instance, weight_attr);
871
872 /* Don't race with governors using the 'weight' value */
873 guard(thermal_zone)(tz);
874
875 instance->weight = weight;
876
877 thermal_governor_update_tz(tz, THERMAL_INSTANCE_WEIGHT_CHANGED);
878
879 return count;
880}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * thermal.c - sysfs interface of thermal devices
4 *
5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6 *
7 * Highly based on original thermal_core.c
8 * Copyright (C) 2008 Intel Corp
9 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
10 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/sysfs.h>
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/jiffies.h>
21
22#include "thermal_core.h"
23
24/* sys I/F for thermal zone */
25
26static ssize_t
27type_show(struct device *dev, struct device_attribute *attr, char *buf)
28{
29 struct thermal_zone_device *tz = to_thermal_zone(dev);
30
31 return sprintf(buf, "%s\n", tz->type);
32}
33
34static ssize_t
35temp_show(struct device *dev, struct device_attribute *attr, char *buf)
36{
37 struct thermal_zone_device *tz = to_thermal_zone(dev);
38 int temperature, ret;
39
40 ret = thermal_zone_get_temp(tz, &temperature);
41
42 if (ret)
43 return ret;
44
45 return sprintf(buf, "%d\n", temperature);
46}
47
48static ssize_t
49mode_show(struct device *dev, struct device_attribute *attr, char *buf)
50{
51 struct thermal_zone_device *tz = to_thermal_zone(dev);
52 int enabled;
53
54 mutex_lock(&tz->lock);
55 enabled = thermal_zone_device_is_enabled(tz);
56 mutex_unlock(&tz->lock);
57
58 return sprintf(buf, "%s\n", enabled ? "enabled" : "disabled");
59}
60
61static ssize_t
62mode_store(struct device *dev, struct device_attribute *attr,
63 const char *buf, size_t count)
64{
65 struct thermal_zone_device *tz = to_thermal_zone(dev);
66 int result;
67
68 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
69 result = thermal_zone_device_enable(tz);
70 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
71 result = thermal_zone_device_disable(tz);
72 else
73 result = -EINVAL;
74
75 if (result)
76 return result;
77
78 return count;
79}
80
81static ssize_t
82trip_point_type_show(struct device *dev, struct device_attribute *attr,
83 char *buf)
84{
85 struct thermal_zone_device *tz = to_thermal_zone(dev);
86 enum thermal_trip_type type;
87 int trip, result;
88
89 if (!tz->ops->get_trip_type)
90 return -EPERM;
91
92 if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
93 return -EINVAL;
94
95 mutex_lock(&tz->lock);
96
97 if (device_is_registered(dev))
98 result = tz->ops->get_trip_type(tz, trip, &type);
99 else
100 result = -ENODEV;
101
102 mutex_unlock(&tz->lock);
103 if (result)
104 return result;
105
106 switch (type) {
107 case THERMAL_TRIP_CRITICAL:
108 return sprintf(buf, "critical\n");
109 case THERMAL_TRIP_HOT:
110 return sprintf(buf, "hot\n");
111 case THERMAL_TRIP_PASSIVE:
112 return sprintf(buf, "passive\n");
113 case THERMAL_TRIP_ACTIVE:
114 return sprintf(buf, "active\n");
115 default:
116 return sprintf(buf, "unknown\n");
117 }
118}
119
120static ssize_t
121trip_point_temp_store(struct device *dev, struct device_attribute *attr,
122 const char *buf, size_t count)
123{
124 struct thermal_zone_device *tz = to_thermal_zone(dev);
125 int trip, ret;
126 int temperature, hyst = 0;
127 enum thermal_trip_type type;
128
129 if (!tz->ops->set_trip_temp && !tz->trips)
130 return -EPERM;
131
132 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
133 return -EINVAL;
134
135 if (kstrtoint(buf, 10, &temperature))
136 return -EINVAL;
137
138 mutex_lock(&tz->lock);
139
140 if (!device_is_registered(dev)) {
141 ret = -ENODEV;
142 goto unlock;
143 }
144
145 if (tz->ops->set_trip_temp) {
146 ret = tz->ops->set_trip_temp(tz, trip, temperature);
147 if (ret)
148 goto unlock;
149 }
150
151 if (tz->trips)
152 tz->trips[trip].temperature = temperature;
153
154 if (tz->ops->get_trip_hyst) {
155 ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
156 if (ret)
157 goto unlock;
158 }
159
160 ret = tz->ops->get_trip_type(tz, trip, &type);
161 if (ret)
162 goto unlock;
163
164 thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
165
166 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
167
168unlock:
169 mutex_unlock(&tz->lock);
170
171 if (ret)
172 return ret;
173
174 return count;
175}
176
177static ssize_t
178trip_point_temp_show(struct device *dev, struct device_attribute *attr,
179 char *buf)
180{
181 struct thermal_zone_device *tz = to_thermal_zone(dev);
182 int trip, ret;
183 int temperature;
184
185 if (!tz->ops->get_trip_temp)
186 return -EPERM;
187
188 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
189 return -EINVAL;
190
191 mutex_lock(&tz->lock);
192
193 if (device_is_registered(dev))
194 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
195 else
196 ret = -ENODEV;
197
198 mutex_unlock(&tz->lock);
199
200 if (ret)
201 return ret;
202
203 return sprintf(buf, "%d\n", temperature);
204}
205
206static ssize_t
207trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
209{
210 struct thermal_zone_device *tz = to_thermal_zone(dev);
211 int trip, ret;
212 int temperature;
213
214 if (!tz->ops->set_trip_hyst)
215 return -EPERM;
216
217 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
218 return -EINVAL;
219
220 if (kstrtoint(buf, 10, &temperature))
221 return -EINVAL;
222
223 mutex_lock(&tz->lock);
224
225 if (!device_is_registered(dev)) {
226 ret = -ENODEV;
227 goto unlock;
228 }
229
230 /*
231 * We are not doing any check on the 'temperature' value
232 * here. The driver implementing 'set_trip_hyst' has to
233 * take care of this.
234 */
235 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
236
237 if (!ret)
238 __thermal_zone_set_trips(tz);
239
240unlock:
241 mutex_unlock(&tz->lock);
242
243 return ret ? ret : count;
244}
245
246static ssize_t
247trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
248 char *buf)
249{
250 struct thermal_zone_device *tz = to_thermal_zone(dev);
251 int trip, ret;
252 int temperature;
253
254 if (!tz->ops->get_trip_hyst)
255 return -EPERM;
256
257 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
258 return -EINVAL;
259
260 mutex_lock(&tz->lock);
261
262 if (device_is_registered(dev))
263 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
264 else
265 ret = -ENODEV;
266
267 mutex_unlock(&tz->lock);
268
269 return ret ? ret : sprintf(buf, "%d\n", temperature);
270}
271
272static ssize_t
273policy_store(struct device *dev, struct device_attribute *attr,
274 const char *buf, size_t count)
275{
276 struct thermal_zone_device *tz = to_thermal_zone(dev);
277 char name[THERMAL_NAME_LENGTH];
278 int ret;
279
280 snprintf(name, sizeof(name), "%s", buf);
281
282 ret = thermal_zone_device_set_policy(tz, name);
283 if (!ret)
284 ret = count;
285
286 return ret;
287}
288
289static ssize_t
290policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
291{
292 struct thermal_zone_device *tz = to_thermal_zone(dev);
293
294 return sprintf(buf, "%s\n", tz->governor->name);
295}
296
297static ssize_t
298available_policies_show(struct device *dev, struct device_attribute *devattr,
299 char *buf)
300{
301 return thermal_build_list_of_policies(buf);
302}
303
304#if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
305static ssize_t
306emul_temp_store(struct device *dev, struct device_attribute *attr,
307 const char *buf, size_t count)
308{
309 struct thermal_zone_device *tz = to_thermal_zone(dev);
310 int ret = 0;
311 int temperature;
312
313 if (kstrtoint(buf, 10, &temperature))
314 return -EINVAL;
315
316 mutex_lock(&tz->lock);
317
318 if (!device_is_registered(dev)) {
319 ret = -ENODEV;
320 goto unlock;
321 }
322
323 if (!tz->ops->set_emul_temp)
324 tz->emul_temperature = temperature;
325 else
326 ret = tz->ops->set_emul_temp(tz, temperature);
327
328 if (!ret)
329 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
330
331unlock:
332 mutex_unlock(&tz->lock);
333
334 return ret ? ret : count;
335}
336static DEVICE_ATTR_WO(emul_temp);
337#endif
338
339static ssize_t
340sustainable_power_show(struct device *dev, struct device_attribute *devattr,
341 char *buf)
342{
343 struct thermal_zone_device *tz = to_thermal_zone(dev);
344
345 if (tz->tzp)
346 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
347 else
348 return -EIO;
349}
350
351static ssize_t
352sustainable_power_store(struct device *dev, struct device_attribute *devattr,
353 const char *buf, size_t count)
354{
355 struct thermal_zone_device *tz = to_thermal_zone(dev);
356 u32 sustainable_power;
357
358 if (!tz->tzp)
359 return -EIO;
360
361 if (kstrtou32(buf, 10, &sustainable_power))
362 return -EINVAL;
363
364 tz->tzp->sustainable_power = sustainable_power;
365
366 return count;
367}
368
369#define create_s32_tzp_attr(name) \
370 static ssize_t \
371 name##_show(struct device *dev, struct device_attribute *devattr, \
372 char *buf) \
373 { \
374 struct thermal_zone_device *tz = to_thermal_zone(dev); \
375 \
376 if (tz->tzp) \
377 return sprintf(buf, "%d\n", tz->tzp->name); \
378 else \
379 return -EIO; \
380 } \
381 \
382 static ssize_t \
383 name##_store(struct device *dev, struct device_attribute *devattr, \
384 const char *buf, size_t count) \
385 { \
386 struct thermal_zone_device *tz = to_thermal_zone(dev); \
387 s32 value; \
388 \
389 if (!tz->tzp) \
390 return -EIO; \
391 \
392 if (kstrtos32(buf, 10, &value)) \
393 return -EINVAL; \
394 \
395 tz->tzp->name = value; \
396 \
397 return count; \
398 } \
399 static DEVICE_ATTR_RW(name)
400
401create_s32_tzp_attr(k_po);
402create_s32_tzp_attr(k_pu);
403create_s32_tzp_attr(k_i);
404create_s32_tzp_attr(k_d);
405create_s32_tzp_attr(integral_cutoff);
406create_s32_tzp_attr(slope);
407create_s32_tzp_attr(offset);
408#undef create_s32_tzp_attr
409
410/*
411 * These are thermal zone device attributes that will always be present.
412 * All the attributes created for tzp (create_s32_tzp_attr) also are always
413 * present on the sysfs interface.
414 */
415static DEVICE_ATTR_RO(type);
416static DEVICE_ATTR_RO(temp);
417static DEVICE_ATTR_RW(policy);
418static DEVICE_ATTR_RO(available_policies);
419static DEVICE_ATTR_RW(sustainable_power);
420
421/* These thermal zone device attributes are created based on conditions */
422static DEVICE_ATTR_RW(mode);
423
424/* These attributes are unconditionally added to a thermal zone */
425static struct attribute *thermal_zone_dev_attrs[] = {
426 &dev_attr_type.attr,
427 &dev_attr_temp.attr,
428#if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
429 &dev_attr_emul_temp.attr,
430#endif
431 &dev_attr_policy.attr,
432 &dev_attr_available_policies.attr,
433 &dev_attr_sustainable_power.attr,
434 &dev_attr_k_po.attr,
435 &dev_attr_k_pu.attr,
436 &dev_attr_k_i.attr,
437 &dev_attr_k_d.attr,
438 &dev_attr_integral_cutoff.attr,
439 &dev_attr_slope.attr,
440 &dev_attr_offset.attr,
441 NULL,
442};
443
444static const struct attribute_group thermal_zone_attribute_group = {
445 .attrs = thermal_zone_dev_attrs,
446};
447
448static struct attribute *thermal_zone_mode_attrs[] = {
449 &dev_attr_mode.attr,
450 NULL,
451};
452
453static const struct attribute_group thermal_zone_mode_attribute_group = {
454 .attrs = thermal_zone_mode_attrs,
455};
456
457static const struct attribute_group *thermal_zone_attribute_groups[] = {
458 &thermal_zone_attribute_group,
459 &thermal_zone_mode_attribute_group,
460 /* This is not NULL terminated as we create the group dynamically */
461};
462
463/**
464 * create_trip_attrs() - create attributes for trip points
465 * @tz: the thermal zone device
466 * @mask: Writeable trip point bitmap.
467 *
468 * helper function to instantiate sysfs entries for every trip
469 * point and its properties of a struct thermal_zone_device.
470 *
471 * Return: 0 on success, the proper error value otherwise.
472 */
473static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
474{
475 struct attribute **attrs;
476 int indx;
477
478 /* This function works only for zones with at least one trip */
479 if (tz->num_trips <= 0)
480 return -EINVAL;
481
482 tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
483 GFP_KERNEL);
484 if (!tz->trip_type_attrs)
485 return -ENOMEM;
486
487 tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
488 GFP_KERNEL);
489 if (!tz->trip_temp_attrs) {
490 kfree(tz->trip_type_attrs);
491 return -ENOMEM;
492 }
493
494 if (tz->ops->get_trip_hyst) {
495 tz->trip_hyst_attrs = kcalloc(tz->num_trips,
496 sizeof(*tz->trip_hyst_attrs),
497 GFP_KERNEL);
498 if (!tz->trip_hyst_attrs) {
499 kfree(tz->trip_type_attrs);
500 kfree(tz->trip_temp_attrs);
501 return -ENOMEM;
502 }
503 }
504
505 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
506 if (!attrs) {
507 kfree(tz->trip_type_attrs);
508 kfree(tz->trip_temp_attrs);
509 if (tz->ops->get_trip_hyst)
510 kfree(tz->trip_hyst_attrs);
511 return -ENOMEM;
512 }
513
514 for (indx = 0; indx < tz->num_trips; indx++) {
515 /* create trip type attribute */
516 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
517 "trip_point_%d_type", indx);
518
519 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
520 tz->trip_type_attrs[indx].attr.attr.name =
521 tz->trip_type_attrs[indx].name;
522 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
523 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
524 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
525
526 /* create trip temp attribute */
527 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
528 "trip_point_%d_temp", indx);
529
530 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
531 tz->trip_temp_attrs[indx].attr.attr.name =
532 tz->trip_temp_attrs[indx].name;
533 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
534 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
535 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
536 mask & (1 << indx)) {
537 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
538 tz->trip_temp_attrs[indx].attr.store =
539 trip_point_temp_store;
540 }
541 attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
542
543 /* create Optional trip hyst attribute */
544 if (!tz->ops->get_trip_hyst)
545 continue;
546 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
547 "trip_point_%d_hyst", indx);
548
549 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
550 tz->trip_hyst_attrs[indx].attr.attr.name =
551 tz->trip_hyst_attrs[indx].name;
552 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
553 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
554 if (tz->ops->set_trip_hyst) {
555 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
556 tz->trip_hyst_attrs[indx].attr.store =
557 trip_point_hyst_store;
558 }
559 attrs[indx + tz->num_trips * 2] =
560 &tz->trip_hyst_attrs[indx].attr.attr;
561 }
562 attrs[tz->num_trips * 3] = NULL;
563
564 tz->trips_attribute_group.attrs = attrs;
565
566 return 0;
567}
568
569/**
570 * destroy_trip_attrs() - destroy attributes for trip points
571 * @tz: the thermal zone device
572 *
573 * helper function to free resources allocated by create_trip_attrs()
574 */
575static void destroy_trip_attrs(struct thermal_zone_device *tz)
576{
577 if (!tz)
578 return;
579
580 kfree(tz->trip_type_attrs);
581 kfree(tz->trip_temp_attrs);
582 if (tz->ops->get_trip_hyst)
583 kfree(tz->trip_hyst_attrs);
584 kfree(tz->trips_attribute_group.attrs);
585}
586
587int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
588 int mask)
589{
590 const struct attribute_group **groups;
591 int i, size, result;
592
593 /* we need one extra for trips and the NULL to terminate the array */
594 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
595 /* This also takes care of API requirement to be NULL terminated */
596 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
597 if (!groups)
598 return -ENOMEM;
599
600 for (i = 0; i < size - 2; i++)
601 groups[i] = thermal_zone_attribute_groups[i];
602
603 if (tz->num_trips) {
604 result = create_trip_attrs(tz, mask);
605 if (result) {
606 kfree(groups);
607
608 return result;
609 }
610
611 groups[size - 2] = &tz->trips_attribute_group;
612 }
613
614 tz->device.groups = groups;
615
616 return 0;
617}
618
619void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
620{
621 if (!tz)
622 return;
623
624 if (tz->num_trips)
625 destroy_trip_attrs(tz);
626
627 kfree(tz->device.groups);
628}
629
630/* sys I/F for cooling device */
631static ssize_t
632cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
633{
634 struct thermal_cooling_device *cdev = to_cooling_device(dev);
635
636 return sprintf(buf, "%s\n", cdev->type);
637}
638
639static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
640 char *buf)
641{
642 struct thermal_cooling_device *cdev = to_cooling_device(dev);
643
644 return sprintf(buf, "%ld\n", cdev->max_state);
645}
646
647static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
648 char *buf)
649{
650 struct thermal_cooling_device *cdev = to_cooling_device(dev);
651 unsigned long state;
652 int ret;
653
654 ret = cdev->ops->get_cur_state(cdev, &state);
655 if (ret)
656 return ret;
657 return sprintf(buf, "%ld\n", state);
658}
659
660static ssize_t
661cur_state_store(struct device *dev, struct device_attribute *attr,
662 const char *buf, size_t count)
663{
664 struct thermal_cooling_device *cdev = to_cooling_device(dev);
665 unsigned long state;
666 int result;
667
668 if (sscanf(buf, "%ld\n", &state) != 1)
669 return -EINVAL;
670
671 if ((long)state < 0)
672 return -EINVAL;
673
674 /* Requested state should be less than max_state + 1 */
675 if (state > cdev->max_state)
676 return -EINVAL;
677
678 mutex_lock(&cdev->lock);
679
680 result = cdev->ops->set_cur_state(cdev, state);
681 if (!result)
682 thermal_cooling_device_stats_update(cdev, state);
683
684 mutex_unlock(&cdev->lock);
685 return result ? result : count;
686}
687
688static struct device_attribute
689dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
690static DEVICE_ATTR_RO(max_state);
691static DEVICE_ATTR_RW(cur_state);
692
693static struct attribute *cooling_device_attrs[] = {
694 &dev_attr_cdev_type.attr,
695 &dev_attr_max_state.attr,
696 &dev_attr_cur_state.attr,
697 NULL,
698};
699
700static const struct attribute_group cooling_device_attr_group = {
701 .attrs = cooling_device_attrs,
702};
703
704static const struct attribute_group *cooling_device_attr_groups[] = {
705 &cooling_device_attr_group,
706 NULL, /* Space allocated for cooling_device_stats_attr_group */
707 NULL,
708};
709
710#ifdef CONFIG_THERMAL_STATISTICS
711struct cooling_dev_stats {
712 spinlock_t lock;
713 unsigned int total_trans;
714 unsigned long state;
715 ktime_t last_time;
716 ktime_t *time_in_state;
717 unsigned int *trans_table;
718};
719
720static void update_time_in_state(struct cooling_dev_stats *stats)
721{
722 ktime_t now = ktime_get(), delta;
723
724 delta = ktime_sub(now, stats->last_time);
725 stats->time_in_state[stats->state] =
726 ktime_add(stats->time_in_state[stats->state], delta);
727 stats->last_time = now;
728}
729
730void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
731 unsigned long new_state)
732{
733 struct cooling_dev_stats *stats = cdev->stats;
734
735 if (!stats)
736 return;
737
738 spin_lock(&stats->lock);
739
740 if (stats->state == new_state)
741 goto unlock;
742
743 update_time_in_state(stats);
744 stats->trans_table[stats->state * (cdev->max_state + 1) + new_state]++;
745 stats->state = new_state;
746 stats->total_trans++;
747
748unlock:
749 spin_unlock(&stats->lock);
750}
751
752static ssize_t total_trans_show(struct device *dev,
753 struct device_attribute *attr, char *buf)
754{
755 struct thermal_cooling_device *cdev = to_cooling_device(dev);
756 struct cooling_dev_stats *stats = cdev->stats;
757 int ret;
758
759 spin_lock(&stats->lock);
760 ret = sprintf(buf, "%u\n", stats->total_trans);
761 spin_unlock(&stats->lock);
762
763 return ret;
764}
765
766static ssize_t
767time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
768 char *buf)
769{
770 struct thermal_cooling_device *cdev = to_cooling_device(dev);
771 struct cooling_dev_stats *stats = cdev->stats;
772 ssize_t len = 0;
773 int i;
774
775 spin_lock(&stats->lock);
776 update_time_in_state(stats);
777
778 for (i = 0; i <= cdev->max_state; i++) {
779 len += sprintf(buf + len, "state%u\t%llu\n", i,
780 ktime_to_ms(stats->time_in_state[i]));
781 }
782 spin_unlock(&stats->lock);
783
784 return len;
785}
786
787static ssize_t
788reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
789 size_t count)
790{
791 struct thermal_cooling_device *cdev = to_cooling_device(dev);
792 struct cooling_dev_stats *stats = cdev->stats;
793 int i, states = cdev->max_state + 1;
794
795 spin_lock(&stats->lock);
796
797 stats->total_trans = 0;
798 stats->last_time = ktime_get();
799 memset(stats->trans_table, 0,
800 states * states * sizeof(*stats->trans_table));
801
802 for (i = 0; i < states; i++)
803 stats->time_in_state[i] = ktime_set(0, 0);
804
805 spin_unlock(&stats->lock);
806
807 return count;
808}
809
810static ssize_t trans_table_show(struct device *dev,
811 struct device_attribute *attr, char *buf)
812{
813 struct thermal_cooling_device *cdev = to_cooling_device(dev);
814 struct cooling_dev_stats *stats = cdev->stats;
815 ssize_t len = 0;
816 int i, j;
817
818 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
819 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
820 for (i = 0; i <= cdev->max_state; i++) {
821 if (len >= PAGE_SIZE)
822 break;
823 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
824 }
825 if (len >= PAGE_SIZE)
826 return PAGE_SIZE;
827
828 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
829
830 for (i = 0; i <= cdev->max_state; i++) {
831 if (len >= PAGE_SIZE)
832 break;
833
834 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
835
836 for (j = 0; j <= cdev->max_state; j++) {
837 if (len >= PAGE_SIZE)
838 break;
839 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
840 stats->trans_table[i * (cdev->max_state + 1) + j]);
841 }
842 if (len >= PAGE_SIZE)
843 break;
844 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
845 }
846
847 if (len >= PAGE_SIZE) {
848 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
849 return -EFBIG;
850 }
851 return len;
852}
853
854static DEVICE_ATTR_RO(total_trans);
855static DEVICE_ATTR_RO(time_in_state_ms);
856static DEVICE_ATTR_WO(reset);
857static DEVICE_ATTR_RO(trans_table);
858
859static struct attribute *cooling_device_stats_attrs[] = {
860 &dev_attr_total_trans.attr,
861 &dev_attr_time_in_state_ms.attr,
862 &dev_attr_reset.attr,
863 &dev_attr_trans_table.attr,
864 NULL
865};
866
867static const struct attribute_group cooling_device_stats_attr_group = {
868 .attrs = cooling_device_stats_attrs,
869 .name = "stats"
870};
871
872static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
873{
874 const struct attribute_group *stats_attr_group = NULL;
875 struct cooling_dev_stats *stats;
876 /* Total number of states is highest state + 1 */
877 unsigned long states = cdev->max_state + 1;
878 int var;
879
880 var = sizeof(*stats);
881 var += sizeof(*stats->time_in_state) * states;
882 var += sizeof(*stats->trans_table) * states * states;
883
884 stats = kzalloc(var, GFP_KERNEL);
885 if (!stats)
886 goto out;
887
888 stats->time_in_state = (ktime_t *)(stats + 1);
889 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
890 cdev->stats = stats;
891 stats->last_time = ktime_get();
892
893 spin_lock_init(&stats->lock);
894
895 stats_attr_group = &cooling_device_stats_attr_group;
896
897out:
898 /* Fill the empty slot left in cooling_device_attr_groups */
899 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
900 cooling_device_attr_groups[var] = stats_attr_group;
901}
902
903static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
904{
905 kfree(cdev->stats);
906 cdev->stats = NULL;
907}
908
909#else
910
911static inline void
912cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
913static inline void
914cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
915
916#endif /* CONFIG_THERMAL_STATISTICS */
917
918void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
919{
920 cooling_device_stats_setup(cdev);
921 cdev->device.groups = cooling_device_attr_groups;
922}
923
924void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
925{
926 cooling_device_stats_destroy(cdev);
927}
928
929/* these helper will be used only at the time of bindig */
930ssize_t
931trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
932{
933 struct thermal_instance *instance;
934
935 instance =
936 container_of(attr, struct thermal_instance, attr);
937
938 return sprintf(buf, "%d\n", instance->trip);
939}
940
941ssize_t
942weight_show(struct device *dev, struct device_attribute *attr, char *buf)
943{
944 struct thermal_instance *instance;
945
946 instance = container_of(attr, struct thermal_instance, weight_attr);
947
948 return sprintf(buf, "%d\n", instance->weight);
949}
950
951ssize_t weight_store(struct device *dev, struct device_attribute *attr,
952 const char *buf, size_t count)
953{
954 struct thermal_instance *instance;
955 int ret, weight;
956
957 ret = kstrtoint(buf, 0, &weight);
958 if (ret)
959 return ret;
960
961 instance = container_of(attr, struct thermal_instance, weight_attr);
962 instance->weight = weight;
963
964 return count;
965}