Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ledtrig-cpu.c - LED trigger based on CPU activity
0004  *
0005  * This LED trigger will be registered for first 8 CPUs and named
0006  * as cpu0..cpu7. There's additional trigger called cpu that
0007  * is on when any CPU is active.
0008  *
0009  * If you want support for arbitrary number of CPUs, make it one trigger,
0010  * with additional sysfs file selecting which CPU to watch.
0011  *
0012  * It can be bound to any LED just like other triggers using either a
0013  * board file or via sysfs interface.
0014  *
0015  * An API named ledtrig_cpu is exported for any user, who want to add CPU
0016  * activity indication in their code.
0017  *
0018  * Copyright 2011 Linus Walleij <linus.walleij@linaro.org>
0019  * Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com>
0020  */
0021 
0022 #include <linux/kernel.h>
0023 #include <linux/init.h>
0024 #include <linux/slab.h>
0025 #include <linux/percpu.h>
0026 #include <linux/syscore_ops.h>
0027 #include <linux/rwsem.h>
0028 #include <linux/cpu.h>
0029 #include "../leds.h"
0030 
0031 #define MAX_NAME_LEN    8
0032 
0033 struct led_trigger_cpu {
0034     bool is_active;
0035     char name[MAX_NAME_LEN];
0036     struct led_trigger *_trig;
0037 };
0038 
0039 static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
0040 
0041 static struct led_trigger *trig_cpu_all;
0042 static atomic_t num_active_cpus = ATOMIC_INIT(0);
0043 
0044 /**
0045  * ledtrig_cpu - emit a CPU event as a trigger
0046  * @ledevt: CPU event to be emitted
0047  *
0048  * Emit a CPU event on a CPU core, which will trigger a
0049  * bound LED to turn on or turn off.
0050  */
0051 void ledtrig_cpu(enum cpu_led_event ledevt)
0052 {
0053     struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
0054     bool is_active = trig->is_active;
0055 
0056     /* Locate the correct CPU LED */
0057     switch (ledevt) {
0058     case CPU_LED_IDLE_END:
0059     case CPU_LED_START:
0060         /* Will turn the LED on, max brightness */
0061         is_active = true;
0062         break;
0063 
0064     case CPU_LED_IDLE_START:
0065     case CPU_LED_STOP:
0066     case CPU_LED_HALTED:
0067         /* Will turn the LED off */
0068         is_active = false;
0069         break;
0070 
0071     default:
0072         /* Will leave the LED as it is */
0073         break;
0074     }
0075 
0076     if (is_active != trig->is_active) {
0077         unsigned int active_cpus;
0078         unsigned int total_cpus;
0079 
0080         /* Update trigger state */
0081         trig->is_active = is_active;
0082         atomic_add(is_active ? 1 : -1, &num_active_cpus);
0083         active_cpus = atomic_read(&num_active_cpus);
0084         total_cpus = num_present_cpus();
0085 
0086         led_trigger_event(trig->_trig,
0087             is_active ? LED_FULL : LED_OFF);
0088 
0089 
0090         led_trigger_event(trig_cpu_all,
0091             DIV_ROUND_UP(LED_FULL * active_cpus, total_cpus));
0092 
0093     }
0094 }
0095 EXPORT_SYMBOL(ledtrig_cpu);
0096 
0097 static int ledtrig_cpu_syscore_suspend(void)
0098 {
0099     ledtrig_cpu(CPU_LED_STOP);
0100     return 0;
0101 }
0102 
0103 static void ledtrig_cpu_syscore_resume(void)
0104 {
0105     ledtrig_cpu(CPU_LED_START);
0106 }
0107 
0108 static void ledtrig_cpu_syscore_shutdown(void)
0109 {
0110     ledtrig_cpu(CPU_LED_HALTED);
0111 }
0112 
0113 static struct syscore_ops ledtrig_cpu_syscore_ops = {
0114     .shutdown   = ledtrig_cpu_syscore_shutdown,
0115     .suspend    = ledtrig_cpu_syscore_suspend,
0116     .resume     = ledtrig_cpu_syscore_resume,
0117 };
0118 
0119 static int ledtrig_online_cpu(unsigned int cpu)
0120 {
0121     ledtrig_cpu(CPU_LED_START);
0122     return 0;
0123 }
0124 
0125 static int ledtrig_prepare_down_cpu(unsigned int cpu)
0126 {
0127     ledtrig_cpu(CPU_LED_STOP);
0128     return 0;
0129 }
0130 
0131 static int __init ledtrig_cpu_init(void)
0132 {
0133     int cpu;
0134     int ret;
0135 
0136     /* Supports up to 9999 cpu cores */
0137     BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
0138 
0139     /*
0140      * Registering a trigger for all CPUs.
0141      */
0142     led_trigger_register_simple("cpu", &trig_cpu_all);
0143 
0144     /*
0145      * Registering CPU led trigger for each CPU core here
0146      * ignores CPU hotplug, but after this CPU hotplug works
0147      * fine with this trigger.
0148      */
0149     for_each_possible_cpu(cpu) {
0150         struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
0151 
0152         if (cpu >= 8)
0153             continue;
0154 
0155         snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
0156 
0157         led_trigger_register_simple(trig->name, &trig->_trig);
0158     }
0159 
0160     register_syscore_ops(&ledtrig_cpu_syscore_ops);
0161 
0162     ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting",
0163                 ledtrig_online_cpu, ledtrig_prepare_down_cpu);
0164     if (ret < 0)
0165         pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
0166                ret);
0167 
0168     pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
0169 
0170     return 0;
0171 }
0172 device_initcall(ledtrig_cpu_init);