Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 2 | /* |
| 3 | * ledtrig-cpu.c - LED trigger based on CPU activity |
| 4 | * |
| 5 | * This LED trigger will be registered for each possible CPU and named as |
| 6 | * cpu0, cpu1, cpu2, cpu3, etc. |
| 7 | * |
| 8 | * It can be bound to any LED just like other triggers using either a |
| 9 | * board file or via sysfs interface. |
| 10 | * |
| 11 | * An API named ledtrig_cpu is exported for any user, who want to add CPU |
| 12 | * activity indication in their code |
| 13 | * |
| 14 | * Copyright 2011 Linus Walleij <linus.walleij@linaro.org> |
| 15 | * Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com> |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 16 | */ |
| 17 | |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/percpu.h> |
| 22 | #include <linux/syscore_ops.h> |
| 23 | #include <linux/rwsem.h> |
Pawel Moll | fba14ae | 2014-01-22 08:32:02 -0800 | [diff] [blame] | 24 | #include <linux/cpu.h> |
Kim, Milo | f07fb52 | 2013-02-20 00:36:01 -0800 | [diff] [blame] | 25 | #include "../leds.h" |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 26 | |
| 27 | #define MAX_NAME_LEN 8 |
| 28 | |
| 29 | struct led_trigger_cpu { |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 30 | bool is_active; |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 31 | char name[MAX_NAME_LEN]; |
| 32 | struct led_trigger *_trig; |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig); |
| 36 | |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 37 | static struct led_trigger *trig_cpu_all; |
| 38 | static atomic_t num_active_cpus = ATOMIC_INIT(0); |
| 39 | |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 40 | /** |
| 41 | * ledtrig_cpu - emit a CPU event as a trigger |
| 42 | * @evt: CPU event to be emitted |
| 43 | * |
| 44 | * Emit a CPU event on a CPU core, which will trigger a |
Pavel Machek | e602fda | 2016-10-03 10:13:14 +0200 | [diff] [blame] | 45 | * bound LED to turn on or turn off. |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 46 | */ |
| 47 | void ledtrig_cpu(enum cpu_led_event ledevt) |
| 48 | { |
Christoph Lameter | 24c9301 | 2014-05-05 09:48:38 -0700 | [diff] [blame] | 49 | struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig); |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 50 | bool is_active = trig->is_active; |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 51 | |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 52 | /* Locate the correct CPU LED */ |
| 53 | switch (ledevt) { |
| 54 | case CPU_LED_IDLE_END: |
| 55 | case CPU_LED_START: |
| 56 | /* Will turn the LED on, max brightness */ |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 57 | is_active = true; |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 58 | break; |
| 59 | |
| 60 | case CPU_LED_IDLE_START: |
| 61 | case CPU_LED_STOP: |
| 62 | case CPU_LED_HALTED: |
| 63 | /* Will turn the LED off */ |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 64 | is_active = false; |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 65 | break; |
| 66 | |
| 67 | default: |
| 68 | /* Will leave the LED as it is */ |
| 69 | break; |
| 70 | } |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 71 | |
| 72 | if (is_active != trig->is_active) { |
| 73 | unsigned int active_cpus; |
| 74 | unsigned int total_cpus; |
| 75 | |
| 76 | /* Update trigger state */ |
| 77 | trig->is_active = is_active; |
| 78 | atomic_add(is_active ? 1 : -1, &num_active_cpus); |
| 79 | active_cpus = atomic_read(&num_active_cpus); |
| 80 | total_cpus = num_present_cpus(); |
| 81 | |
| 82 | led_trigger_event(trig->_trig, |
| 83 | is_active ? LED_FULL : LED_OFF); |
| 84 | |
| 85 | |
| 86 | led_trigger_event(trig_cpu_all, |
| 87 | DIV_ROUND_UP(LED_FULL * active_cpus, total_cpus)); |
| 88 | |
| 89 | } |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 90 | } |
| 91 | EXPORT_SYMBOL(ledtrig_cpu); |
| 92 | |
| 93 | static int ledtrig_cpu_syscore_suspend(void) |
| 94 | { |
| 95 | ledtrig_cpu(CPU_LED_STOP); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void ledtrig_cpu_syscore_resume(void) |
| 100 | { |
| 101 | ledtrig_cpu(CPU_LED_START); |
| 102 | } |
| 103 | |
| 104 | static void ledtrig_cpu_syscore_shutdown(void) |
| 105 | { |
| 106 | ledtrig_cpu(CPU_LED_HALTED); |
| 107 | } |
| 108 | |
| 109 | static struct syscore_ops ledtrig_cpu_syscore_ops = { |
| 110 | .shutdown = ledtrig_cpu_syscore_shutdown, |
| 111 | .suspend = ledtrig_cpu_syscore_suspend, |
| 112 | .resume = ledtrig_cpu_syscore_resume, |
| 113 | }; |
| 114 | |
Sebastian Andrzej Siewior | cd894f1 | 2016-07-20 17:24:55 +0200 | [diff] [blame] | 115 | static int ledtrig_online_cpu(unsigned int cpu) |
Pawel Moll | fba14ae | 2014-01-22 08:32:02 -0800 | [diff] [blame] | 116 | { |
Richard Cochran | 911a359 | 2016-07-13 17:16:45 +0000 | [diff] [blame] | 117 | ledtrig_cpu(CPU_LED_START); |
| 118 | return 0; |
Pawel Moll | fba14ae | 2014-01-22 08:32:02 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Sebastian Andrzej Siewior | cd894f1 | 2016-07-20 17:24:55 +0200 | [diff] [blame] | 121 | static int ledtrig_prepare_down_cpu(unsigned int cpu) |
Richard Cochran | 911a359 | 2016-07-13 17:16:45 +0000 | [diff] [blame] | 122 | { |
| 123 | ledtrig_cpu(CPU_LED_STOP); |
| 124 | return 0; |
| 125 | } |
Pawel Moll | fba14ae | 2014-01-22 08:32:02 -0800 | [diff] [blame] | 126 | |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 127 | static int __init ledtrig_cpu_init(void) |
| 128 | { |
| 129 | int cpu; |
Sebastian Andrzej Siewior | cd894f1 | 2016-07-20 17:24:55 +0200 | [diff] [blame] | 130 | int ret; |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 131 | |
| 132 | /* Supports up to 9999 cpu cores */ |
| 133 | BUILD_BUG_ON(CONFIG_NR_CPUS > 9999); |
| 134 | |
| 135 | /* |
Paulo Costa | 0b88b71 | 2017-02-09 21:07:35 -0200 | [diff] [blame] | 136 | * Registering a trigger for all CPUs. |
| 137 | */ |
| 138 | led_trigger_register_simple("cpu", &trig_cpu_all); |
| 139 | |
| 140 | /* |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 141 | * Registering CPU led trigger for each CPU core here |
| 142 | * ignores CPU hotplug, but after this CPU hotplug works |
| 143 | * fine with this trigger. |
| 144 | */ |
| 145 | for_each_possible_cpu(cpu) { |
| 146 | struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu); |
| 147 | |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 148 | snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu); |
| 149 | |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 150 | led_trigger_register_simple(trig->name, &trig->_trig); |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | register_syscore_ops(&ledtrig_cpu_syscore_ops); |
Richard Cochran | 911a359 | 2016-07-13 17:16:45 +0000 | [diff] [blame] | 154 | |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 155 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting", |
Sebastian Andrzej Siewior | cd894f1 | 2016-07-20 17:24:55 +0200 | [diff] [blame] | 156 | ledtrig_online_cpu, ledtrig_prepare_down_cpu); |
| 157 | if (ret < 0) |
| 158 | pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n", |
| 159 | ret); |
Bryan Wu | 8f88731 | 2011-06-25 18:33:50 +0800 | [diff] [blame] | 160 | |
| 161 | pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n"); |
| 162 | |
| 163 | return 0; |
| 164 | } |
Paul Gortmaker | 227a0ca | 2015-12-13 16:45:49 -0500 | [diff] [blame] | 165 | device_initcall(ledtrig_cpu_init); |