blob: ec38fad6775f5bb98860ebb8b934bd10e0ad1344 [file] [log] [blame]
Stefan Roesec2c69712022-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>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Stefan Roese881d4102022-09-02 14:10:45 +020021void hw_watchdog_reset(void);
22
Rasmus Villemoes28968392022-10-28 13:50:53 +020023struct hlist_head *cyclic_get_list(void)
Stefan Roesec2c69712022-09-02 13:57:48 +020024{
Rasmus Villemoes50128ae2022-10-28 13:50:54 +020025 /* Silence "discards 'volatile' qualifier" warning. */
26 return (struct hlist_head *)&gd->cyclic_list;
Stefan Roesec2c69712022-09-02 13:57:48 +020027}
28
Rasmus Villemoes008c4b32024-05-21 10:46:52 +020029void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
30 uint64_t delay_us, const char *name)
Stefan Roesec2c69712022-09-02 13:57:48 +020031{
Rasmus Villemoes008c4b32024-05-21 10:46:52 +020032 memset(cyclic, 0, sizeof(*cyclic));
Stefan Roesec2c69712022-09-02 13:57:48 +020033
34 /* Store values in struct */
35 cyclic->func = func;
Rasmus Villemoes3a11ead2024-05-21 10:46:50 +020036 cyclic->name = name;
Stefan Roesec2c69712022-09-02 13:57:48 +020037 cyclic->delay_us = delay_us;
38 cyclic->start_time_us = timer_get_us();
Rasmus Villemoes50128ae2022-10-28 13:50:54 +020039 hlist_add_head(&cyclic->list, cyclic_get_list());
Stefan Roesec2c69712022-09-02 13:57:48 +020040}
41
Rasmus Villemoes008c4b32024-05-21 10:46:52 +020042void cyclic_unregister(struct cyclic_info *cyclic)
Stefan Roesec2c69712022-09-02 13:57:48 +020043{
Rasmus Villemoes28968392022-10-28 13:50:53 +020044 hlist_del(&cyclic->list);
Stefan Roesec2c69712022-09-02 13:57:48 +020045}
46
47void cyclic_run(void)
48{
Rasmus Villemoes28968392022-10-28 13:50:53 +020049 struct cyclic_info *cyclic;
50 struct hlist_node *tmp;
Stefan Roesec2c69712022-09-02 13:57:48 +020051 uint64_t now, cpu_time;
52
53 /* Prevent recursion */
Rasmus Villemoesd7de5ef2022-10-28 13:50:50 +020054 if (gd->flags & GD_FLG_CYCLIC_RUNNING)
Stefan Roesec2c69712022-09-02 13:57:48 +020055 return;
56
Rasmus Villemoesd7de5ef2022-10-28 13:50:50 +020057 gd->flags |= GD_FLG_CYCLIC_RUNNING;
Rasmus Villemoes50128ae2022-10-28 13:50:54 +020058 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
Stefan Roesec2c69712022-09-02 13:57:48 +020059 /*
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 Villemoes008c4b32024-05-21 10:46:52 +020067 cyclic->func(cyclic);
Stefan Roesec2c69712022-09-02 13:57:48 +020068 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 Roeseddc8d362022-10-17 09:00:58 +020073 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 Roesec2c69712022-09-02 13:57:48 +020076 cyclic->name, cpu_time,
77 CONFIG_CYCLIC_MAX_CPU_TIME_US);
78
Stefan Roeseddc8d362022-10-17 09:00:58 +020079 /*
80 * Don't disable this function, just warn once
81 * about this exceeding CPU time usage
82 */
83 cyclic->already_warned = true;
Stefan Roesec2c69712022-09-02 13:57:48 +020084 }
85 }
86 }
Rasmus Villemoesd7de5ef2022-10-28 13:50:50 +020087 gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
Stefan Roesec2c69712022-09-02 13:57:48 +020088}
89
Stefan Roese881d4102022-09-02 14:10:45 +020090void 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 Villemoes50128ae2022-10-28 13:50:54 +0200100 if (gd)
Stefan Roese881d4102022-09-02 14:10:45 +0200101 cyclic_run();
102}
103
Rasmus Villemoes50128ae2022-10-28 13:50:54 +0200104int cyclic_unregister_all(void)
Stefan Roesec2c69712022-09-02 13:57:48 +0200105{
Rasmus Villemoes28968392022-10-28 13:50:53 +0200106 struct cyclic_info *cyclic;
107 struct hlist_node *tmp;
Stefan Roesec2c69712022-09-02 13:57:48 +0200108
Rasmus Villemoes50128ae2022-10-28 13:50:54 +0200109 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list)
Stefan Roesec2c69712022-09-02 13:57:48 +0200110 cyclic_unregister(cyclic);
Stefan Roesec2c69712022-09-02 13:57:48 +0200111
112 return 0;
113}