Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 1 | /* 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 Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 18 | * struct cyclic_info - Information about cyclic execution function |
| 19 | * |
| 20 | * @func: Function to call periodically |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 21 | * @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 Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 28 | * @already_warned: Flag that we've warned about exceeding CPU time usage |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 29 | * |
| 30 | * When !CONFIG_CYCLIC, this struct is empty. |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 31 | */ |
| 32 | struct cyclic_info { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 33 | #if defined(CONFIG_CYCLIC) |
| 34 | void (*func)(struct cyclic_info *c); |
Rasmus Villemoes | d84e291 | 2024-05-21 10:46:50 +0200 | [diff] [blame] | 35 | const char *name; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 36 | 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 Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 41 | struct hlist_node list; |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 42 | bool already_warned; |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 43 | #endif |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /** Function type for cyclic functions */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 47 | typedef void (*cyclic_func_t)(struct cyclic_info *c); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 48 | |
| 49 | #if defined(CONFIG_CYCLIC) |
| 50 | /** |
| 51 | * cyclic_register - Register a new cyclic function |
| 52 | * |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 53 | * @cyclic: Cyclic info structure |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 54 | * @func: Function to call periodically |
| 55 | * @delay_us: Delay is us after which this function shall get executed |
| 56 | * @name: Cyclic function name/id |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 57 | * |
| 58 | * The function @func will be called with @cyclic as its |
| 59 | * argument. @cyclic will usually be embedded in some device-specific |
| 60 | * structure, which the callback can retrieve using container_of(). |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 61 | */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 62 | void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func, |
| 63 | uint64_t delay_us, const char *name); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * cyclic_unregister - Unregister a cyclic function |
| 67 | * |
| 68 | * @cyclic: Pointer to cyclic_struct of the function that shall be removed |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 69 | */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 70 | void cyclic_unregister(struct cyclic_info *cyclic); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 71 | |
| 72 | /** |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 73 | * cyclic_unregister_all() - Clean up cyclic functions |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 74 | * |
| 75 | * This removes all cyclic functions |
| 76 | */ |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 77 | int cyclic_unregister_all(void); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * cyclic_get_list() - Get cyclic list pointer |
| 81 | * |
| 82 | * Return the cyclic list pointer |
| 83 | * |
| 84 | * @return: pointer to cyclic_list |
| 85 | */ |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 86 | struct hlist_head *cyclic_get_list(void); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * cyclic_run() - Interate over all registered cyclic functions |
| 90 | * |
| 91 | * Interate over all registered cyclic functions and if the it's function |
| 92 | * needs to be executed, then call into these registered functions. |
| 93 | */ |
| 94 | void cyclic_run(void); |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * schedule() - Schedule all potentially waiting tasks |
| 98 | * |
| 99 | * Basically a wrapper for cyclic_run(), pontentially enhanced by some |
| 100 | * other parts, that need to get handled periodically. |
| 101 | */ |
| 102 | void schedule(void); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 103 | #else |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 104 | |
| 105 | static inline void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func, |
| 106 | uint64_t delay_us, const char *name) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 107 | { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 110 | static inline void cyclic_unregister(struct cyclic_info *cyclic) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 111 | { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static inline void cyclic_run(void) |
| 115 | { |
| 116 | } |
| 117 | |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 118 | static inline void schedule(void) |
| 119 | { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 122 | static inline int cyclic_unregister_all(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 123 | { |
| 124 | return 0; |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | #endif |