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