wakeup_stats.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wakeup statistics in sysfs
  4. *
  5. * Copyright (c) 2019 Linux Foundation
  6. * Copyright (c) 2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  7. * Copyright (c) 2019 Google Inc.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/idr.h>
  11. #include <linux/init.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kobject.h>
  15. #include <linux/slab.h>
  16. #include <linux/timekeeping.h>
  17. #include "power.h"
  18. static struct class *wakeup_class;
  19. #define wakeup_attr(_name) \
  20. static ssize_t _name##_show(struct device *dev, \
  21. struct device_attribute *attr, char *buf) \
  22. { \
  23. struct wakeup_source *ws = dev_get_drvdata(dev); \
  24. \
  25. return sysfs_emit(buf, "%lu\n", ws->_name); \
  26. } \
  27. static DEVICE_ATTR_RO(_name)
  28. wakeup_attr(active_count);
  29. wakeup_attr(event_count);
  30. wakeup_attr(wakeup_count);
  31. wakeup_attr(expire_count);
  32. wakeup_attr(relax_count);
  33. static ssize_t active_time_ms_show(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct wakeup_source *ws = dev_get_drvdata(dev);
  37. ktime_t active_time =
  38. ws->active ? ktime_sub(ktime_get(), ws->last_time) : 0;
  39. return sysfs_emit(buf, "%lld\n", ktime_to_ms(active_time));
  40. }
  41. static DEVICE_ATTR_RO(active_time_ms);
  42. static ssize_t total_time_ms_show(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct wakeup_source *ws = dev_get_drvdata(dev);
  46. ktime_t active_time;
  47. ktime_t total_time = ws->total_time;
  48. if (ws->active) {
  49. active_time = ktime_sub(ktime_get(), ws->last_time);
  50. total_time = ktime_add(total_time, active_time);
  51. }
  52. return sysfs_emit(buf, "%lld\n", ktime_to_ms(total_time));
  53. }
  54. static DEVICE_ATTR_RO(total_time_ms);
  55. static ssize_t max_time_ms_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct wakeup_source *ws = dev_get_drvdata(dev);
  59. ktime_t active_time;
  60. ktime_t max_time = ws->max_time;
  61. if (ws->active) {
  62. active_time = ktime_sub(ktime_get(), ws->last_time);
  63. if (active_time > max_time)
  64. max_time = active_time;
  65. }
  66. return sysfs_emit(buf, "%lld\n", ktime_to_ms(max_time));
  67. }
  68. static DEVICE_ATTR_RO(max_time_ms);
  69. static ssize_t last_change_ms_show(struct device *dev,
  70. struct device_attribute *attr, char *buf)
  71. {
  72. struct wakeup_source *ws = dev_get_drvdata(dev);
  73. return sysfs_emit(buf, "%lld\n", ktime_to_ms(ws->last_time));
  74. }
  75. static DEVICE_ATTR_RO(last_change_ms);
  76. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  77. char *buf)
  78. {
  79. struct wakeup_source *ws = dev_get_drvdata(dev);
  80. return sysfs_emit(buf, "%s\n", ws->name);
  81. }
  82. static DEVICE_ATTR_RO(name);
  83. static ssize_t prevent_suspend_time_ms_show(struct device *dev,
  84. struct device_attribute *attr,
  85. char *buf)
  86. {
  87. struct wakeup_source *ws = dev_get_drvdata(dev);
  88. ktime_t prevent_sleep_time = ws->prevent_sleep_time;
  89. if (ws->active && ws->autosleep_enabled) {
  90. prevent_sleep_time = ktime_add(prevent_sleep_time,
  91. ktime_sub(ktime_get(), ws->start_prevent_time));
  92. }
  93. return sysfs_emit(buf, "%lld\n", ktime_to_ms(prevent_sleep_time));
  94. }
  95. static DEVICE_ATTR_RO(prevent_suspend_time_ms);
  96. static struct attribute *wakeup_source_attrs[] = {
  97. &dev_attr_name.attr,
  98. &dev_attr_active_count.attr,
  99. &dev_attr_event_count.attr,
  100. &dev_attr_wakeup_count.attr,
  101. &dev_attr_expire_count.attr,
  102. &dev_attr_relax_count.attr,
  103. &dev_attr_active_time_ms.attr,
  104. &dev_attr_total_time_ms.attr,
  105. &dev_attr_max_time_ms.attr,
  106. &dev_attr_last_change_ms.attr,
  107. &dev_attr_prevent_suspend_time_ms.attr,
  108. NULL,
  109. };
  110. ATTRIBUTE_GROUPS(wakeup_source);
  111. static void device_create_release(struct device *dev)
  112. {
  113. kfree(dev);
  114. }
  115. static struct device *wakeup_source_device_create(struct device *parent,
  116. struct wakeup_source *ws)
  117. {
  118. struct device *dev = NULL;
  119. int retval;
  120. dev = kzalloc_obj(*dev);
  121. if (!dev) {
  122. retval = -ENOMEM;
  123. goto error;
  124. }
  125. device_initialize(dev);
  126. dev->devt = MKDEV(0, 0);
  127. dev->class = wakeup_class;
  128. dev->parent = parent;
  129. dev->groups = wakeup_source_groups;
  130. dev->release = device_create_release;
  131. dev_set_drvdata(dev, ws);
  132. device_set_pm_not_required(dev);
  133. retval = dev_set_name(dev, "wakeup%d", ws->id);
  134. if (retval)
  135. goto error;
  136. retval = device_add(dev);
  137. if (retval)
  138. goto error;
  139. return dev;
  140. error:
  141. put_device(dev);
  142. return ERR_PTR(retval);
  143. }
  144. /**
  145. * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
  146. * @parent: Device given wakeup source is associated with (or NULL if virtual).
  147. * @ws: Wakeup source to be added in sysfs.
  148. */
  149. int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
  150. {
  151. struct device *dev;
  152. dev = wakeup_source_device_create(parent, ws);
  153. if (IS_ERR(dev))
  154. return PTR_ERR(dev);
  155. ws->dev = dev;
  156. return 0;
  157. }
  158. /**
  159. * pm_wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs
  160. * for a device if they're missing.
  161. * @parent: Device given wakeup source is associated with
  162. */
  163. int pm_wakeup_source_sysfs_add(struct device *parent)
  164. {
  165. if (!parent->power.wakeup || parent->power.wakeup->dev)
  166. return 0;
  167. return wakeup_source_sysfs_add(parent, parent->power.wakeup);
  168. }
  169. /**
  170. * wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs.
  171. * @ws: Wakeup source to be removed from sysfs.
  172. */
  173. void wakeup_source_sysfs_remove(struct wakeup_source *ws)
  174. {
  175. device_unregister(ws->dev);
  176. }
  177. static int __init wakeup_sources_sysfs_init(void)
  178. {
  179. wakeup_class = class_create("wakeup");
  180. return PTR_ERR_OR_ZERO(wakeup_class);
  181. }
  182. postcore_initcall(wakeup_sources_sysfs_init);