blob: 75662d9f6138bb0fa15a40601ea98ad7524d678c [file] [log] [blame]
Stefan Roese036f89a2022-09-02 13:57:48 +02001// 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 Villemoescde8c952024-10-03 23:27:51 +020018#include <u-boot/schedule.h>
Jerome Forissier2e24a352025-04-18 16:09:36 +020019#include <uthread.h>
Stefan Roese036f89a2022-09-02 13:57:48 +020020
21DECLARE_GLOBAL_DATA_PTR;
22
Stefan Roese97473632022-09-02 14:10:45 +020023void hw_watchdog_reset(void);
24
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020025struct hlist_head *cyclic_get_list(void)
Stefan Roese036f89a2022-09-02 13:57:48 +020026{
Rasmus Villemoesc794e492022-10-28 13:50:54 +020027 /* Silence "discards 'volatile' qualifier" warning. */
28 return (struct hlist_head *)&gd->cyclic_list;
Stefan Roese036f89a2022-09-02 13:57:48 +020029}
30
Rasmus Villemoesa26777b2025-05-07 12:58:19 +020031static 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 Villemoesea36ada2024-05-21 10:46:52 +020043void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
44 uint64_t delay_us, const char *name)
Stefan Roese036f89a2022-09-02 13:57:48 +020045{
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020046 memset(cyclic, 0, sizeof(*cyclic));
Stefan Roese036f89a2022-09-02 13:57:48 +020047
48 /* Store values in struct */
49 cyclic->func = func;
Rasmus Villemoesd84e2912024-05-21 10:46:50 +020050 cyclic->name = name;
Stefan Roese036f89a2022-09-02 13:57:48 +020051 cyclic->delay_us = delay_us;
Patrice Chotardd25fe772025-01-14 14:28:13 +010052 cyclic->start_time_us = get_timer_us(0);
Rasmus Villemoesc794e492022-10-28 13:50:54 +020053 hlist_add_head(&cyclic->list, cyclic_get_list());
Stefan Roese036f89a2022-09-02 13:57:48 +020054}
55
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020056void cyclic_unregister(struct cyclic_info *cyclic)
Stefan Roese036f89a2022-09-02 13:57:48 +020057{
Rasmus Villemoesa26777b2025-05-07 12:58:19 +020058 if (!cyclic_is_registered(cyclic))
59 return;
60
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020061 hlist_del(&cyclic->list);
Stefan Roese036f89a2022-09-02 13:57:48 +020062}
63
Rasmus Villemoes3c27ef62024-10-03 23:27:56 +020064static void cyclic_run(void)
Stefan Roese036f89a2022-09-02 13:57:48 +020065{
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020066 struct cyclic_info *cyclic;
67 struct hlist_node *tmp;
Stefan Roese036f89a2022-09-02 13:57:48 +020068 uint64_t now, cpu_time;
69
70 /* Prevent recursion */
Rasmus Villemoese2ff5fc2022-10-28 13:50:50 +020071 if (gd->flags & GD_FLG_CYCLIC_RUNNING)
Stefan Roese036f89a2022-09-02 13:57:48 +020072 return;
73
Rasmus Villemoese2ff5fc2022-10-28 13:50:50 +020074 gd->flags |= GD_FLG_CYCLIC_RUNNING;
Rasmus Villemoesc794e492022-10-28 13:50:54 +020075 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
Stefan Roese036f89a2022-09-02 13:57:48 +020076 /*
77 * Check if this cyclic function needs to get called, e.g.
78 * do not call the cyclic func too often
79 */
Patrice Chotardd25fe772025-01-14 14:28:13 +010080 now = get_timer_us(0);
Stefan Roese036f89a2022-09-02 13:57:48 +020081 if (time_after_eq64(now, cyclic->next_call)) {
82 /* Call cyclic function and account it's cpu-time */
83 cyclic->next_call = now + cyclic->delay_us;
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020084 cyclic->func(cyclic);
Stefan Roese036f89a2022-09-02 13:57:48 +020085 cyclic->run_cnt++;
Patrice Chotardd25fe772025-01-14 14:28:13 +010086 cpu_time = get_timer_us(0) - now;
Stefan Roese036f89a2022-09-02 13:57:48 +020087 cyclic->cpu_time_us += cpu_time;
88
89 /* Check if cpu-time exceeds max allowed time */
Stefan Roese75b0d962022-10-17 09:00:58 +020090 if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&
91 (!cyclic->already_warned)) {
92 pr_err("cyclic function %s took too long: %lldus vs %dus max\n",
Stefan Roese036f89a2022-09-02 13:57:48 +020093 cyclic->name, cpu_time,
94 CONFIG_CYCLIC_MAX_CPU_TIME_US);
95
Stefan Roese75b0d962022-10-17 09:00:58 +020096 /*
97 * Don't disable this function, just warn once
98 * about this exceeding CPU time usage
99 */
100 cyclic->already_warned = true;
Stefan Roese036f89a2022-09-02 13:57:48 +0200101 }
102 }
103 }
Rasmus Villemoese2ff5fc2022-10-28 13:50:50 +0200104 gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
Stefan Roese036f89a2022-09-02 13:57:48 +0200105}
106
Stefan Roese97473632022-09-02 14:10:45 +0200107void schedule(void)
108{
109 /* The HW watchdog is not integrated into the cyclic IF (yet) */
110 if (IS_ENABLED(CONFIG_HW_WATCHDOG))
111 hw_watchdog_reset();
112
113 /*
114 * schedule() might get called very early before the cyclic IF is
115 * ready. Make sure to only call cyclic_run() when it's initalized.
116 */
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200117 if (gd)
Stefan Roese97473632022-09-02 14:10:45 +0200118 cyclic_run();
Jerome Forissier2e24a352025-04-18 16:09:36 +0200119
120 uthread_schedule();
Stefan Roese97473632022-09-02 14:10:45 +0200121}
122
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200123int cyclic_unregister_all(void)
Stefan Roese036f89a2022-09-02 13:57:48 +0200124{
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +0200125 struct cyclic_info *cyclic;
126 struct hlist_node *tmp;
Stefan Roese036f89a2022-09-02 13:57:48 +0200127
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200128 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list)
Stefan Roese036f89a2022-09-02 13:57:48 +0200129 cyclic_unregister(cyclic);
Stefan Roese036f89a2022-09-02 13:57:48 +0200130
131 return 0;
132}