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> |
Rasmus Villemoes | cde8c95 | 2024-10-03 23:27:51 +0200 | [diff] [blame] | 16 | #include <u-boot/schedule.h> // to be removed later |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 17 | |
| 18 | /** |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 19 | * struct cyclic_info - Information about cyclic execution function |
| 20 | * |
| 21 | * @func: Function to call periodically |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 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 |
| 28 | * @list: List node |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 29 | * @already_warned: Flag that we've warned about exceeding CPU time usage |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 30 | * |
| 31 | * When !CONFIG_CYCLIC, this struct is empty. |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 32 | */ |
| 33 | struct cyclic_info { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 34 | #if defined(CONFIG_CYCLIC) |
| 35 | void (*func)(struct cyclic_info *c); |
Rasmus Villemoes | d84e291 | 2024-05-21 10:46:50 +0200 | [diff] [blame] | 36 | const char *name; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 37 | 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 Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 42 | struct hlist_node list; |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 43 | bool already_warned; |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 44 | #endif |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /** Function type for cyclic functions */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 48 | typedef void (*cyclic_func_t)(struct cyclic_info *c); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 49 | |
Simon Glass | 0b38522 | 2024-07-31 08:44:08 -0600 | [diff] [blame] | 50 | #if CONFIG_IS_ENABLED(CYCLIC) |
| 51 | |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 52 | /** |
| 53 | * cyclic_register - Register a new cyclic function |
| 54 | * |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 55 | * @cyclic: Cyclic info structure |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 56 | * @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 Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 59 | * |
| 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 Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 63 | */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 64 | void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func, |
| 65 | uint64_t delay_us, const char *name); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * cyclic_unregister - Unregister a cyclic function |
| 69 | * |
| 70 | * @cyclic: Pointer to cyclic_struct of the function that shall be removed |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 71 | */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 72 | void cyclic_unregister(struct cyclic_info *cyclic); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 75 | * cyclic_unregister_all() - Clean up cyclic functions |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 76 | * |
| 77 | * This removes all cyclic functions |
| 78 | */ |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 79 | int cyclic_unregister_all(void); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 80 | |
| 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 Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 88 | struct hlist_head *cyclic_get_list(void); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 89 | |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 90 | #else |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 91 | |
| 92 | static inline void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func, |
| 93 | uint64_t delay_us, const char *name) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 94 | { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 97 | static inline void cyclic_unregister(struct cyclic_info *cyclic) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 98 | { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 101 | static inline int cyclic_unregister_all(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 102 | { |
| 103 | return 0; |
| 104 | } |
Simon Glass | 0b38522 | 2024-07-31 08:44:08 -0600 | [diff] [blame] | 105 | #endif /* CYCLIC */ |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 106 | |
| 107 | #endif |