ledtrig-cpu.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ledtrig-cpu.c - LED trigger based on CPU activity
  4. *
  5. * This LED trigger will be registered for first 8 CPUs and named
  6. * as cpu0..cpu7. There's additional trigger called cpu that
  7. * is on when any CPU is active.
  8. *
  9. * If you want support for arbitrary number of CPUs, make it one trigger,
  10. * with additional sysfs file selecting which CPU to watch.
  11. *
  12. * It can be bound to any LED just like other triggers using either a
  13. * board file or via sysfs interface.
  14. *
  15. * An API named ledtrig_cpu is exported for any user, who want to add CPU
  16. * activity indication in their code.
  17. *
  18. * Copyright 2011 Linus Walleij <[email protected]>
  19. * Copyright 2011 - 2012 Bryan Wu <[email protected]>
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/percpu.h>
  25. #include <linux/syscore_ops.h>
  26. #include <linux/rwsem.h>
  27. #include <linux/cpu.h>
  28. #include "../leds.h"
  29. #define MAX_NAME_LEN 8
  30. struct led_trigger_cpu {
  31. bool is_active;
  32. char name[MAX_NAME_LEN];
  33. struct led_trigger *_trig;
  34. };
  35. static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
  36. static struct led_trigger *trig_cpu_all;
  37. static atomic_t num_active_cpus = ATOMIC_INIT(0);
  38. /**
  39. * ledtrig_cpu - emit a CPU event as a trigger
  40. * @ledevt: CPU event to be emitted
  41. *
  42. * Emit a CPU event on a CPU core, which will trigger a
  43. * bound LED to turn on or turn off.
  44. */
  45. void ledtrig_cpu(enum cpu_led_event ledevt)
  46. {
  47. struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
  48. bool is_active = trig->is_active;
  49. /* Locate the correct CPU LED */
  50. switch (ledevt) {
  51. case CPU_LED_IDLE_END:
  52. case CPU_LED_START:
  53. /* Will turn the LED on, max brightness */
  54. is_active = true;
  55. break;
  56. case CPU_LED_IDLE_START:
  57. case CPU_LED_STOP:
  58. case CPU_LED_HALTED:
  59. /* Will turn the LED off */
  60. is_active = false;
  61. break;
  62. default:
  63. /* Will leave the LED as it is */
  64. break;
  65. }
  66. if (is_active != trig->is_active) {
  67. unsigned int active_cpus;
  68. unsigned int total_cpus;
  69. /* Update trigger state */
  70. trig->is_active = is_active;
  71. atomic_add(is_active ? 1 : -1, &num_active_cpus);
  72. active_cpus = atomic_read(&num_active_cpus);
  73. total_cpus = num_present_cpus();
  74. led_trigger_event(trig->_trig,
  75. is_active ? LED_FULL : LED_OFF);
  76. led_trigger_event(trig_cpu_all,
  77. DIV_ROUND_UP(LED_FULL * active_cpus, total_cpus));
  78. }
  79. }
  80. EXPORT_SYMBOL(ledtrig_cpu);
  81. static int ledtrig_cpu_syscore_suspend(void)
  82. {
  83. ledtrig_cpu(CPU_LED_STOP);
  84. return 0;
  85. }
  86. static void ledtrig_cpu_syscore_resume(void)
  87. {
  88. ledtrig_cpu(CPU_LED_START);
  89. }
  90. static void ledtrig_cpu_syscore_shutdown(void)
  91. {
  92. ledtrig_cpu(CPU_LED_HALTED);
  93. }
  94. static struct syscore_ops ledtrig_cpu_syscore_ops = {
  95. .shutdown = ledtrig_cpu_syscore_shutdown,
  96. .suspend = ledtrig_cpu_syscore_suspend,
  97. .resume = ledtrig_cpu_syscore_resume,
  98. };
  99. static int ledtrig_online_cpu(unsigned int cpu)
  100. {
  101. ledtrig_cpu(CPU_LED_START);
  102. return 0;
  103. }
  104. static int ledtrig_prepare_down_cpu(unsigned int cpu)
  105. {
  106. ledtrig_cpu(CPU_LED_STOP);
  107. return 0;
  108. }
  109. static int __init ledtrig_cpu_init(void)
  110. {
  111. unsigned int cpu;
  112. int ret;
  113. /* Supports up to 9999 cpu cores */
  114. BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
  115. /*
  116. * Registering a trigger for all CPUs.
  117. */
  118. led_trigger_register_simple("cpu", &trig_cpu_all);
  119. /*
  120. * Registering CPU led trigger for each CPU core here
  121. * ignores CPU hotplug, but after this CPU hotplug works
  122. * fine with this trigger.
  123. */
  124. for_each_possible_cpu(cpu) {
  125. struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
  126. if (cpu >= 8)
  127. continue;
  128. snprintf(trig->name, MAX_NAME_LEN, "cpu%u", cpu);
  129. led_trigger_register_simple(trig->name, &trig->_trig);
  130. }
  131. register_syscore_ops(&ledtrig_cpu_syscore_ops);
  132. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting",
  133. ledtrig_online_cpu, ledtrig_prepare_down_cpu);
  134. if (ret < 0)
  135. pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
  136. ret);
  137. pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
  138. return 0;
  139. }
  140. device_initcall(ledtrig_cpu_init);