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