wakeup_stats.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. static ssize_t active_time_ms_show(struct device *dev,
  33. struct device_attribute *attr, char *buf)
  34. {
  35. struct wakeup_source *ws = dev_get_drvdata(dev);
  36. ktime_t active_time =
  37. ws->active ? ktime_sub(ktime_get(), ws->last_time) : 0;
  38. return sysfs_emit(buf, "%lld\n", ktime_to_ms(active_time));
  39. }
  40. static DEVICE_ATTR_RO(active_time_ms);
  41. static ssize_t total_time_ms_show(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct wakeup_source *ws = dev_get_drvdata(dev);
  45. ktime_t active_time;
  46. ktime_t total_time = ws->total_time;
  47. if (ws->active) {
  48. active_time = ktime_sub(ktime_get(), ws->last_time);
  49. total_time = ktime_add(total_time, active_time);
  50. }
  51. return sysfs_emit(buf, "%lld\n", ktime_to_ms(total_time));
  52. }
  53. static DEVICE_ATTR_RO(total_time_ms);
  54. static ssize_t max_time_ms_show(struct device *dev,
  55. struct device_attribute *attr, char *buf)
  56. {
  57. struct wakeup_source *ws = dev_get_drvdata(dev);
  58. ktime_t active_time;
  59. ktime_t max_time = ws->max_time;
  60. if (ws->active) {
  61. active_time = ktime_sub(ktime_get(), ws->last_time);
  62. if (active_time > max_time)
  63. max_time = active_time;
  64. }
  65. return sysfs_emit(buf, "%lld\n", ktime_to_ms(max_time));
  66. }
  67. static DEVICE_ATTR_RO(max_time_ms);
  68. static ssize_t last_change_ms_show(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct wakeup_source *ws = dev_get_drvdata(dev);
  72. return sysfs_emit(buf, "%lld\n", ktime_to_ms(ws->last_time));
  73. }
  74. static DEVICE_ATTR_RO(last_change_ms);
  75. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  76. char *buf)
  77. {
  78. struct wakeup_source *ws = dev_get_drvdata(dev);
  79. return sysfs_emit(buf, "%s\n", ws->name);
  80. }
  81. static DEVICE_ATTR_RO(name);
  82. static ssize_t prevent_suspend_time_ms_show(struct device *dev,
  83. struct device_attribute *attr,
  84. char *buf)
  85. {
  86. struct wakeup_source *ws = dev_get_drvdata(dev);
  87. ktime_t prevent_sleep_time = ws->prevent_sleep_time;
  88. if (ws->active && ws->autosleep_enabled) {
  89. prevent_sleep_time = ktime_add(prevent_sleep_time,
  90. ktime_sub(ktime_get(), ws->start_prevent_time));
  91. }
  92. return sysfs_emit(buf, "%lld\n", ktime_to_ms(prevent_sleep_time));
  93. }
  94. static DEVICE_ATTR_RO(prevent_suspend_time_ms);
  95. static struct attribute *wakeup_source_attrs[] = {
  96. &dev_attr_name.attr,
  97. &dev_attr_active_count.attr,
  98. &dev_attr_event_count.attr,
  99. &dev_attr_wakeup_count.attr,
  100. &dev_attr_expire_count.attr,
  101. &dev_attr_active_time_ms.attr,
  102. &dev_attr_total_time_ms.attr,
  103. &dev_attr_max_time_ms.attr,
  104. &dev_attr_last_change_ms.attr,
  105. &dev_attr_prevent_suspend_time_ms.attr,
  106. NULL,
  107. };
  108. ATTRIBUTE_GROUPS(wakeup_source);
  109. static void device_create_release(struct device *dev)
  110. {
  111. kfree(dev);
  112. }
  113. static struct device *wakeup_source_device_create(struct device *parent,
  114. struct wakeup_source *ws)
  115. {
  116. struct device *dev = NULL;
  117. int retval = -ENODEV;
  118. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  119. if (!dev) {
  120. retval = -ENOMEM;
  121. goto error;
  122. }
  123. device_initialize(dev);
  124. dev->devt = MKDEV(0, 0);
  125. dev->class = wakeup_class;
  126. dev->parent = parent;
  127. dev->groups = wakeup_source_groups;
  128. dev->release = device_create_release;
  129. dev_set_drvdata(dev, ws);
  130. device_set_pm_not_required(dev);
  131. retval = kobject_set_name(&dev->kobj, "wakeup%d", ws->id);
  132. if (retval)
  133. goto error;
  134. retval = device_add(dev);
  135. if (retval)
  136. goto error;
  137. return dev;
  138. error:
  139. put_device(dev);
  140. return ERR_PTR(retval);
  141. }
  142. /**
  143. * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
  144. * @parent: Device given wakeup source is associated with (or NULL if virtual).
  145. * @ws: Wakeup source to be added in sysfs.
  146. */
  147. int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
  148. {
  149. struct device *dev;
  150. dev = wakeup_source_device_create(parent, ws);
  151. if (IS_ERR(dev))
  152. return PTR_ERR(dev);
  153. ws->dev = dev;
  154. return 0;
  155. }
  156. /**
  157. * pm_wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs
  158. * for a device if they're missing.
  159. * @parent: Device given wakeup source is associated with
  160. */
  161. int pm_wakeup_source_sysfs_add(struct device *parent)
  162. {
  163. if (!parent->power.wakeup || parent->power.wakeup->dev)
  164. return 0;
  165. return wakeup_source_sysfs_add(parent, parent->power.wakeup);
  166. }
  167. /**
  168. * wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs.
  169. * @ws: Wakeup source to be removed from sysfs.
  170. */
  171. void wakeup_source_sysfs_remove(struct wakeup_source *ws)
  172. {
  173. device_unregister(ws->dev);
  174. }
  175. static int __init wakeup_sources_sysfs_init(void)
  176. {
  177. wakeup_class = class_create(THIS_MODULE, "wakeup");
  178. return PTR_ERR_OR_ZERO(wakeup_class);
  179. }
  180. postcore_initcall(wakeup_sources_sysfs_init);