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