1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * A general-purpose cyclic execution infrastructure, to allow "small"
4 * (run-time wise) functions to be executed at a specified frequency.
5 * Things like LED blinking or watchdog triggering are examples for such
8 * Copyright (C) 2022 Stefan Roese <sr@denx.de>
14 #include <linux/list.h>
15 #include <asm/types.h>
18 * struct cyclic_info - Information about cyclic execution function
20 * @func: Function to call periodically
21 * @ctx: Context pointer to get passed to this function
22 * @name: Name of the cyclic function, e.g. shown in the commands
23 * @delay_ns: Delay is ns after which this function shall get executed
24 * @start_time_us: Start time in us, when this function started its execution
25 * @cpu_time_us: Total CPU time of this function
26 * @run_cnt: Counter of executions occurances
27 * @next_call: Next time in us, when the function shall be executed again
29 * @already_warned: Flag that we've warned about exceeding CPU time usage
32 void (*func)(void *ctx);
36 uint64_t start_time_us;
40 struct hlist_node list;
44 /** Function type for cyclic functions */
45 typedef void (*cyclic_func_t)(void *ctx);
47 #if defined(CONFIG_CYCLIC)
49 * cyclic_register - Register a new cyclic function
51 * @func: Function to call periodically
52 * @delay_us: Delay is us after which this function shall get executed
53 * @name: Cyclic function name/id
54 * @ctx: Context to pass to the function
55 * @return: pointer to cyclic_struct if OK, NULL on error
57 struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
58 const char *name, void *ctx);
61 * cyclic_unregister - Unregister a cyclic function
63 * @cyclic: Pointer to cyclic_struct of the function that shall be removed
64 * @return: 0 if OK, -ve on error
66 int cyclic_unregister(struct cyclic_info *cyclic);
69 * cyclic_unregister_all() - Clean up cyclic functions
71 * This removes all cyclic functions
73 int cyclic_unregister_all(void);
76 * cyclic_get_list() - Get cyclic list pointer
78 * Return the cyclic list pointer
80 * @return: pointer to cyclic_list
82 struct hlist_head *cyclic_get_list(void);
85 * cyclic_run() - Interate over all registered cyclic functions
87 * Interate over all registered cyclic functions and if the it's function
88 * needs to be executed, then call into these registered functions.
90 void cyclic_run(void);
93 * schedule() - Schedule all potentially waiting tasks
95 * Basically a wrapper for cyclic_run(), pontentially enhanced by some
96 * other parts, that need to get handled periodically.
100 static inline struct cyclic_info *cyclic_register(cyclic_func_t func,
108 static inline int cyclic_unregister(struct cyclic_info *cyclic)
113 static inline void cyclic_run(void)
117 static inline void schedule(void)
121 static inline int cyclic_unregister_all(void)