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> |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 22 | void hw_watchdog_reset(void); |
| 23 | |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 24 | struct hlist_head *cyclic_get_list(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 25 | { |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 26 | /* Silence "discards 'volatile' qualifier" warning. */ |
| 27 | return (struct hlist_head *)&gd->cyclic_list; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 30 | void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func, |
| 31 | uint64_t delay_us, const char *name) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 32 | { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 33 | memset(cyclic, 0, sizeof(*cyclic)); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 34 | |
| 35 | /* Store values in struct */ |
| 36 | cyclic->func = func; |
Rasmus Villemoes | d84e291 | 2024-05-21 10:46:50 +0200 | [diff] [blame] | 37 | cyclic->name = name; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 38 | cyclic->delay_us = delay_us; |
| 39 | cyclic->start_time_us = timer_get_us(); |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 40 | hlist_add_head(&cyclic->list, cyclic_get_list()); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 43 | void cyclic_unregister(struct cyclic_info *cyclic) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 44 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 45 | hlist_del(&cyclic->list); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Rasmus Villemoes | 3c27ef6 | 2024-10-03 23:27:56 +0200 | [diff] [blame] | 48 | static void cyclic_run(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 49 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 50 | struct cyclic_info *cyclic; |
| 51 | struct hlist_node *tmp; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 52 | uint64_t now, cpu_time; |
| 53 | |
| 54 | /* Prevent recursion */ |
Rasmus Villemoes | e2ff5fc | 2022-10-28 13:50:50 +0200 | [diff] [blame] | 55 | if (gd->flags & GD_FLG_CYCLIC_RUNNING) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 56 | return; |
| 57 | |
Rasmus Villemoes | e2ff5fc | 2022-10-28 13:50:50 +0200 | [diff] [blame] | 58 | gd->flags |= GD_FLG_CYCLIC_RUNNING; |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 59 | hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) { |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 60 | /* |
| 61 | * Check if this cyclic function needs to get called, e.g. |
| 62 | * do not call the cyclic func too often |
| 63 | */ |
| 64 | now = timer_get_us(); |
| 65 | if (time_after_eq64(now, cyclic->next_call)) { |
| 66 | /* Call cyclic function and account it's cpu-time */ |
| 67 | cyclic->next_call = now + cyclic->delay_us; |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 68 | cyclic->func(cyclic); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 69 | cyclic->run_cnt++; |
| 70 | cpu_time = timer_get_us() - now; |
| 71 | cyclic->cpu_time_us += cpu_time; |
| 72 | |
| 73 | /* Check if cpu-time exceeds max allowed time */ |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 74 | if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) && |
| 75 | (!cyclic->already_warned)) { |
| 76 | 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] | 77 | cyclic->name, cpu_time, |
| 78 | CONFIG_CYCLIC_MAX_CPU_TIME_US); |
| 79 | |
Stefan Roese | 75b0d96 | 2022-10-17 09:00:58 +0200 | [diff] [blame] | 80 | /* |
| 81 | * Don't disable this function, just warn once |
| 82 | * about this exceeding CPU time usage |
| 83 | */ |
| 84 | cyclic->already_warned = true; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
Rasmus Villemoes | e2ff5fc | 2022-10-28 13:50:50 +0200 | [diff] [blame] | 88 | gd->flags &= ~GD_FLG_CYCLIC_RUNNING; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 91 | void schedule(void) |
| 92 | { |
| 93 | /* The HW watchdog is not integrated into the cyclic IF (yet) */ |
| 94 | if (IS_ENABLED(CONFIG_HW_WATCHDOG)) |
| 95 | hw_watchdog_reset(); |
| 96 | |
| 97 | /* |
| 98 | * schedule() might get called very early before the cyclic IF is |
| 99 | * ready. Make sure to only call cyclic_run() when it's initalized. |
| 100 | */ |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 101 | if (gd) |
Stefan Roese | 9747363 | 2022-09-02 14:10:45 +0200 | [diff] [blame] | 102 | cyclic_run(); |
| 103 | } |
| 104 | |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 105 | int cyclic_unregister_all(void) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 106 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 107 | struct cyclic_info *cyclic; |
| 108 | struct hlist_node *tmp; |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 109 | |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 110 | hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 111 | cyclic_unregister(cyclic); |
Stefan Roese | 036f89a | 2022-09-02 13:57:48 +0200 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |