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