blob: cd95b691d485ef218c4868bc2c89c64ce30df73d [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>
16
17/**
Stefan Roese036f89a2022-09-02 13:57:48 +020018 * struct cyclic_info - Information about cyclic execution function
19 *
20 * @func: Function to call periodically
Stefan Roese036f89a2022-09-02 13:57:48 +020021 * @name: Name of the cyclic function, e.g. shown in the commands
22 * @delay_ns: Delay is ns after which this function shall get executed
23 * @start_time_us: Start time in us, when this function started its execution
24 * @cpu_time_us: Total CPU time of this function
25 * @run_cnt: Counter of executions occurances
26 * @next_call: Next time in us, when the function shall be executed again
27 * @list: List node
Stefan Roese75b0d962022-10-17 09:00:58 +020028 * @already_warned: Flag that we've warned about exceeding CPU time usage
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020029 *
30 * When !CONFIG_CYCLIC, this struct is empty.
Stefan Roese036f89a2022-09-02 13:57:48 +020031 */
32struct cyclic_info {
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020033#if defined(CONFIG_CYCLIC)
34 void (*func)(struct cyclic_info *c);
Rasmus Villemoesd84e2912024-05-21 10:46:50 +020035 const char *name;
Stefan Roese036f89a2022-09-02 13:57:48 +020036 uint64_t delay_us;
37 uint64_t start_time_us;
38 uint64_t cpu_time_us;
39 uint64_t run_cnt;
40 uint64_t next_call;
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020041 struct hlist_node list;
Stefan Roese75b0d962022-10-17 09:00:58 +020042 bool already_warned;
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020043#endif
Stefan Roese036f89a2022-09-02 13:57:48 +020044};
45
46/** Function type for cyclic functions */
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020047typedef void (*cyclic_func_t)(struct cyclic_info *c);
Stefan Roese036f89a2022-09-02 13:57:48 +020048
Simon Glass0b385222024-07-31 08:44:08 -060049#if CONFIG_IS_ENABLED(CYCLIC)
50
Stefan Roese036f89a2022-09-02 13:57:48 +020051/**
52 * cyclic_register - Register a new cyclic function
53 *
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020054 * @cyclic: Cyclic info structure
Stefan Roese036f89a2022-09-02 13:57:48 +020055 * @func: Function to call periodically
56 * @delay_us: Delay is us after which this function shall get executed
57 * @name: Cyclic function name/id
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020058 *
59 * The function @func will be called with @cyclic as its
60 * argument. @cyclic will usually be embedded in some device-specific
61 * structure, which the callback can retrieve using container_of().
Stefan Roese036f89a2022-09-02 13:57:48 +020062 */
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020063void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
64 uint64_t delay_us, const char *name);
Stefan Roese036f89a2022-09-02 13:57:48 +020065
66/**
67 * cyclic_unregister - Unregister a cyclic function
68 *
69 * @cyclic: Pointer to cyclic_struct of the function that shall be removed
Stefan Roese036f89a2022-09-02 13:57:48 +020070 */
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020071void cyclic_unregister(struct cyclic_info *cyclic);
Stefan Roese036f89a2022-09-02 13:57:48 +020072
73/**
Rasmus Villemoesc794e492022-10-28 13:50:54 +020074 * cyclic_unregister_all() - Clean up cyclic functions
Stefan Roese036f89a2022-09-02 13:57:48 +020075 *
76 * This removes all cyclic functions
77 */
Rasmus Villemoesc794e492022-10-28 13:50:54 +020078int cyclic_unregister_all(void);
Stefan Roese036f89a2022-09-02 13:57:48 +020079
80/**
81 * cyclic_get_list() - Get cyclic list pointer
82 *
83 * Return the cyclic list pointer
84 *
85 * @return: pointer to cyclic_list
86 */
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020087struct hlist_head *cyclic_get_list(void);
Stefan Roese036f89a2022-09-02 13:57:48 +020088
89/**
90 * cyclic_run() - Interate over all registered cyclic functions
91 *
92 * Interate over all registered cyclic functions and if the it's function
93 * needs to be executed, then call into these registered functions.
94 */
95void cyclic_run(void);
Stefan Roese97473632022-09-02 14:10:45 +020096
97/**
98 * schedule() - Schedule all potentially waiting tasks
99 *
100 * Basically a wrapper for cyclic_run(), pontentially enhanced by some
101 * other parts, that need to get handled periodically.
102 */
103void schedule(void);
Stefan Roese036f89a2022-09-02 13:57:48 +0200104#else
Rasmus Villemoesea36ada2024-05-21 10:46:52 +0200105
106static inline void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
107 uint64_t delay_us, const char *name)
Stefan Roese036f89a2022-09-02 13:57:48 +0200108{
Stefan Roese036f89a2022-09-02 13:57:48 +0200109}
110
Rasmus Villemoesea36ada2024-05-21 10:46:52 +0200111static inline void cyclic_unregister(struct cyclic_info *cyclic)
Stefan Roese036f89a2022-09-02 13:57:48 +0200112{
Stefan Roese036f89a2022-09-02 13:57:48 +0200113}
114
115static inline void cyclic_run(void)
116{
117}
118
Stefan Roese97473632022-09-02 14:10:45 +0200119static inline void schedule(void)
120{
Stefan Roese036f89a2022-09-02 13:57:48 +0200121}
122
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200123static inline int cyclic_unregister_all(void)
Stefan Roese036f89a2022-09-02 13:57:48 +0200124{
125 return 0;
126}
Simon Glass0b385222024-07-31 08:44:08 -0600127#endif /* CYCLIC */
Stefan Roese036f89a2022-09-02 13:57:48 +0200128
129#endif