blob: b695f092f52aaace88995d57ce9d5a5ae18ca78e [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 Villemoesea36ada2024-05-21 10:46:52 +020031void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
32 uint64_t delay_us, const char *name)
Stefan Roese036f89a2022-09-02 13:57:48 +020033{
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020034 memset(cyclic, 0, sizeof(*cyclic));
Stefan Roese036f89a2022-09-02 13:57:48 +020035
36 /* Store values in struct */
37 cyclic->func = func;
Rasmus Villemoesd84e2912024-05-21 10:46:50 +020038 cyclic->name = name;
Stefan Roese036f89a2022-09-02 13:57:48 +020039 cyclic->delay_us = delay_us;
Patrice Chotardd25fe772025-01-14 14:28:13 +010040 cyclic->start_time_us = get_timer_us(0);
Rasmus Villemoesc794e492022-10-28 13:50:54 +020041 hlist_add_head(&cyclic->list, cyclic_get_list());
Stefan Roese036f89a2022-09-02 13:57:48 +020042}
43
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020044void cyclic_unregister(struct cyclic_info *cyclic)
Stefan Roese036f89a2022-09-02 13:57:48 +020045{
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020046 hlist_del(&cyclic->list);
Stefan Roese036f89a2022-09-02 13:57:48 +020047}
48
Rasmus Villemoes3c27ef62024-10-03 23:27:56 +020049static void cyclic_run(void)
Stefan Roese036f89a2022-09-02 13:57:48 +020050{
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020051 struct cyclic_info *cyclic;
52 struct hlist_node *tmp;
Stefan Roese036f89a2022-09-02 13:57:48 +020053 uint64_t now, cpu_time;
54
55 /* Prevent recursion */
Rasmus Villemoese2ff5fc2022-10-28 13:50:50 +020056 if (gd->flags & GD_FLG_CYCLIC_RUNNING)
Stefan Roese036f89a2022-09-02 13:57:48 +020057 return;
58
Rasmus Villemoese2ff5fc2022-10-28 13:50:50 +020059 gd->flags |= GD_FLG_CYCLIC_RUNNING;
Rasmus Villemoesc794e492022-10-28 13:50:54 +020060 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
Stefan Roese036f89a2022-09-02 13:57:48 +020061 /*
62 * Check if this cyclic function needs to get called, e.g.
63 * do not call the cyclic func too often
64 */
Patrice Chotardd25fe772025-01-14 14:28:13 +010065 now = get_timer_us(0);
Stefan Roese036f89a2022-09-02 13:57:48 +020066 if (time_after_eq64(now, cyclic->next_call)) {
67 /* Call cyclic function and account it's cpu-time */
68 cyclic->next_call = now + cyclic->delay_us;
Rasmus Villemoesea36ada2024-05-21 10:46:52 +020069 cyclic->func(cyclic);
Stefan Roese036f89a2022-09-02 13:57:48 +020070 cyclic->run_cnt++;
Patrice Chotardd25fe772025-01-14 14:28:13 +010071 cpu_time = get_timer_us(0) - now;
Stefan Roese036f89a2022-09-02 13:57:48 +020072 cyclic->cpu_time_us += cpu_time;
73
74 /* Check if cpu-time exceeds max allowed time */
Stefan Roese75b0d962022-10-17 09:00:58 +020075 if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&
76 (!cyclic->already_warned)) {
77 pr_err("cyclic function %s took too long: %lldus vs %dus max\n",
Stefan Roese036f89a2022-09-02 13:57:48 +020078 cyclic->name, cpu_time,
79 CONFIG_CYCLIC_MAX_CPU_TIME_US);
80
Stefan Roese75b0d962022-10-17 09:00:58 +020081 /*
82 * Don't disable this function, just warn once
83 * about this exceeding CPU time usage
84 */
85 cyclic->already_warned = true;
Stefan Roese036f89a2022-09-02 13:57:48 +020086 }
87 }
88 }
Rasmus Villemoese2ff5fc2022-10-28 13:50:50 +020089 gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
Stefan Roese036f89a2022-09-02 13:57:48 +020090}
91
Stefan Roese97473632022-09-02 14:10:45 +020092void schedule(void)
93{
94 /* The HW watchdog is not integrated into the cyclic IF (yet) */
95 if (IS_ENABLED(CONFIG_HW_WATCHDOG))
96 hw_watchdog_reset();
97
98 /*
99 * schedule() might get called very early before the cyclic IF is
100 * ready. Make sure to only call cyclic_run() when it's initalized.
101 */
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200102 if (gd)
Stefan Roese97473632022-09-02 14:10:45 +0200103 cyclic_run();
Jerome Forissier2e24a352025-04-18 16:09:36 +0200104
105 uthread_schedule();
Stefan Roese97473632022-09-02 14:10:45 +0200106}
107
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200108int cyclic_unregister_all(void)
Stefan Roese036f89a2022-09-02 13:57:48 +0200109{
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +0200110 struct cyclic_info *cyclic;
111 struct hlist_node *tmp;
Stefan Roese036f89a2022-09-02 13:57:48 +0200112
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200113 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list)
Stefan Roese036f89a2022-09-02 13:57:48 +0200114 cyclic_unregister(cyclic);
Stefan Roese036f89a2022-09-02 13:57:48 +0200115
116 return 0;
117}