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 | #include <cyclic.h> |
| 12 | #include <log.h> |
| 13 | #include <malloc.h> |
| 14 | #include <time.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <asm/global_data.h> |
Rasmus Villemoes | cde8c95 | 2024-10-03 23:27:51 +0200 | [diff] [blame] | 18 | #include <u-boot/schedule.h> |
Jerome Forissier | 2e24a35 | 2025-04-18 16:09:36 +0200 | [diff] [blame] | 19 | #include <uthread.h> |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 23 | void hw_watchdog_reset(void); |
| 24 | |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 25 | struct hlist_head *cyclic_get_list(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 26 | { |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 27 | /* Silence "discards 'volatile' qualifier" warning. */ |
| 28 | return (struct hlist_head *)&gd->cyclic_list; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Rasmus Villemoes | a26777b | 2025-05-07 12:58:19 +0200 | [diff] [blame] | 31 | static bool cyclic_is_registered(const struct cyclic_info *cyclic) |
| 32 | { |
| 33 | const struct cyclic_info *c; |
| 34 | |
| 35 | hlist_for_each_entry(c, cyclic_get_list(), list) { |
| 36 | if (c == cyclic) |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | } |
| 42 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 43 | void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func, |
| 44 | uint64_t delay_us, const char *name) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 45 | { |
Rasmus Villemoes | f7fc571 | 2025-05-07 12:58:20 +0200 | [diff] [blame] | 46 | cyclic_unregister(cyclic); |
| 47 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 48 | memset(cyclic, 0, sizeof(*cyclic)); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 49 | |
| 50 | /* Store values in struct */ |
| 51 | cyclic->func = func; |
Rasmus Villemoes | d84e291 | 2024-05-21 10:46:50 +0200 | [diff] [blame] | 52 | cyclic->name = name; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 53 | cyclic->delay_us = delay_us; |
Patrice Chotard | d25fe77 | 2025-01-14 14:28:13 +0100 | [diff] [blame] | 54 | cyclic->start_time_us = get_timer_us(0); |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 55 | hlist_add_head(&cyclic->list, cyclic_get_list()); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 58 | void cyclic_unregister(struct cyclic_info *cyclic) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 59 | { |
Rasmus Villemoes | a26777b | 2025-05-07 12:58:19 +0200 | [diff] [blame] | 60 | if (!cyclic_is_registered(cyclic)) |
| 61 | return; |
| 62 | |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 63 | hlist_del(&cyclic->list); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Rasmus Villemoes | 3c27ef6 | 2024-10-03 23:27:56 +0200 | [diff] [blame] | 66 | static void cyclic_run(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 67 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 68 | struct cyclic_info *cyclic; |
| 69 | struct hlist_node *tmp; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 70 | uint64_t now, cpu_time; |
| 71 | |
| 72 | /* Prevent recursion */ |
Rasmus Villemoes | e2ff5fc | 2022-10-28 13:50:50 +0200 | [diff] [blame] | 73 | if (gd->flags & GD_FLG_CYCLIC_RUNNING) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 74 | return; |
| 75 | |
Rasmus Villemoes | e2ff5fc | 2022-10-28 13:50:50 +0200 | [diff] [blame] | 76 | gd->flags |= GD_FLG_CYCLIC_RUNNING; |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 77 | hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Check if this cyclic function needs to get called, e.g. |
| 80 | * do not call the cyclic func too often |
| 81 | */ |
Patrice Chotard | d25fe77 | 2025-01-14 14:28:13 +0100 | [diff] [blame] | 82 | now = get_timer_us(0); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 83 | if (time_after_eq64(now, cyclic->next_call)) { |
| 84 | /* Call cyclic function and account it's cpu-time */ |
| 85 | cyclic->next_call = now + cyclic->delay_us; |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 86 | cyclic->func(cyclic); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 87 | cyclic->run_cnt++; |
Patrice Chotard | d25fe77 | 2025-01-14 14:28:13 +0100 | [diff] [blame] | 88 | cpu_time = get_timer_us(0) - now; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 89 | cyclic->cpu_time_us += cpu_time; |
| 90 | |
| 91 | /* Check if cpu-time exceeds max allowed time */ |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 92 | if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) && |
| 93 | (!cyclic->already_warned)) { |
| 94 | pr_err("cyclic function %s took too long: %lldus vs %dus max\n", |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 95 | cyclic->name, cpu_time, |
| 96 | CONFIG_CYCLIC_MAX_CPU_TIME_US); |
| 97 | |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Don't disable this function, just warn once |
| 100 | * about this exceeding CPU time usage |
| 101 | */ |
| 102 | cyclic->already_warned = true; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
Rasmus Villemoes | e2ff5fc | 2022-10-28 13:50:50 +0200 | [diff] [blame] | 106 | gd->flags &= ~GD_FLG_CYCLIC_RUNNING; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 109 | void schedule(void) |
| 110 | { |
| 111 | /* The HW watchdog is not integrated into the cyclic IF (yet) */ |
| 112 | if (IS_ENABLED(CONFIG_HW_WATCHDOG)) |
| 113 | hw_watchdog_reset(); |
| 114 | |
| 115 | /* |
| 116 | * schedule() might get called very early before the cyclic IF is |
| 117 | * ready. Make sure to only call cyclic_run() when it's initalized. |
| 118 | */ |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 119 | if (gd) |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 120 | cyclic_run(); |
Jerome Forissier | 2e24a35 | 2025-04-18 16:09:36 +0200 | [diff] [blame] | 121 | |
| 122 | uthread_schedule(); |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 125 | int cyclic_unregister_all(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 126 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 127 | struct cyclic_info *cyclic; |
| 128 | struct hlist_node *tmp; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 129 | |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 130 | hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 131 | cyclic_unregister(cyclic); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |