blob: e8de616dcd51ffc7169e0a2c096bd4f7d17af8f5 [file] [log] [blame]
Stefan Roese036f89a2022-09-02 13:57:48 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
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
6 * tasks.
7 *
8 * Copyright (C) 2022 Stefan Roese <sr@denx.de>
9 */
10
11#ifndef __cyclic_h
12#define __cyclic_h
13
14#include <linux/list.h>
15#include <asm/types.h>
Rasmus Villemoescde8c952024-10-03 23:27:51 +020016#include <u-boot/schedule.h> // to be removed later
Stefan Roese036f89a2022-09-02 13:57:48 +020017
18/**
Stefan Roese036f89a2022-09-02 13:57:48 +020019 * struct cyclic_info - Information about cyclic execution function
20 *
21 * @func: Function to call periodically
Stefan Roese036f89a2022-09-02 13:57:48 +020022 * @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
28 * @list: List node
Stefan Roese75b0d962022-10-17 09:00:58 +020029 * @already_warned: Flag that we've warned about exceeding CPU time usage
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020030 *
31 * When !CONFIG_CYCLIC, this struct is empty.
Stefan Roese036f89a2022-09-02 13:57:48 +020032 */
33struct cyclic_info {
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020034#if defined(CONFIG_CYCLIC)
35 void (*func)(struct cyclic_info *c);
Rasmus Villemoesd84e2912024-05-21 10:46:50 +020036 const char *name;
Stefan Roese036f89a2022-09-02 13:57:48 +020037 uint64_t delay_us;
38 uint64_t start_time_us;
39 uint64_t cpu_time_us;
40 uint64_t run_cnt;
41 uint64_t next_call;
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020042 struct hlist_node list;
Stefan Roese75b0d962022-10-17 09:00:58 +020043 bool already_warned;
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020044#endif
Stefan Roese036f89a2022-09-02 13:57:48 +020045};
46
47/** Function type for cyclic functions */
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020048typedef void (*cyclic_func_t)(struct cyclic_info *c);
Stefan Roese036f89a2022-09-02 13:57:48 +020049
Simon Glass0b385222024-07-31 08:44:08 -060050#if CONFIG_IS_ENABLED(CYCLIC)
51
Stefan Roese036f89a2022-09-02 13:57:48 +020052/**
53 * cyclic_register - Register a new cyclic function
54 *
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020055 * @cyclic: Cyclic info structure
Stefan Roese036f89a2022-09-02 13:57:48 +020056 * @func: Function to call periodically
57 * @delay_us: Delay is us after which this function shall get executed
58 * @name: Cyclic function name/id
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020059 *
60 * The function @func will be called with @cyclic as its
61 * argument. @cyclic will usually be embedded in some device-specific
62 * structure, which the callback can retrieve using container_of().
Stefan Roese036f89a2022-09-02 13:57:48 +020063 */
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020064void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
65 uint64_t delay_us, const char *name);
Stefan Roese036f89a2022-09-02 13:57:48 +020066
67/**
68 * cyclic_unregister - Unregister a cyclic function
69 *
70 * @cyclic: Pointer to cyclic_struct of the function that shall be removed
Stefan Roese036f89a2022-09-02 13:57:48 +020071 */
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020072void cyclic_unregister(struct cyclic_info *cyclic);
Stefan Roese036f89a2022-09-02 13:57:48 +020073
74/**
Rasmus Villemoesc794e492022-10-28 13:50:54 +020075 * cyclic_unregister_all() - Clean up cyclic functions
Stefan Roese036f89a2022-09-02 13:57:48 +020076 *
77 * This removes all cyclic functions
78 */
Rasmus Villemoesc794e492022-10-28 13:50:54 +020079int cyclic_unregister_all(void);
Stefan Roese036f89a2022-09-02 13:57:48 +020080
81/**
82 * cyclic_get_list() - Get cyclic list pointer
83 *
84 * Return the cyclic list pointer
85 *
86 * @return: pointer to cyclic_list
87 */
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020088struct hlist_head *cyclic_get_list(void);
Stefan Roese036f89a2022-09-02 13:57:48 +020089
90/**
91 * cyclic_run() - Interate over all registered cyclic functions
92 *
93 * Interate over all registered cyclic functions and if the it's function
94 * needs to be executed, then call into these registered functions.
95 */
96void cyclic_run(void);
Stefan Roese97473632022-09-02 14:10:45 +020097
Stefan Roese036f89a2022-09-02 13:57:48 +020098#else
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020099
100static inline void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
101 uint64_t delay_us, const char *name)
Stefan Roese036f89a2022-09-02 13:57:48 +0200102{
Stefan Roese036f89a2022-09-02 13:57:48 +0200103}
104
Rasmus Villemoesea36ada2024-05-21 10:46:52 +0200105static inline void cyclic_unregister(struct cyclic_info *cyclic)
Stefan Roese036f89a2022-09-02 13:57:48 +0200106{
Stefan Roese036f89a2022-09-02 13:57:48 +0200107}
108
109static inline void cyclic_run(void)
110{
111}
112
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200113static inline int cyclic_unregister_all(void)
Stefan Roese036f89a2022-09-02 13:57:48 +0200114{
115 return 0;
116}
Simon Glass0b385222024-07-31 08:44:08 -0600117#endif /* CYCLIC */
Stefan Roese036f89a2022-09-02 13:57:48 +0200118
119#endif