blob: 32d9bf0d02be2536da9bfe802ff8e5a64154bd21 [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{
25 return &gd->cyclic->cyclic_list;
26}
27
28struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
29 const char *name, void *ctx)
30{
31 struct cyclic_info *cyclic;
32
Rasmus Villemoes6b84b1d2022-10-28 13:50:51 +020033 if (!gd->cyclic) {
Stefan Roesec2c69712022-09-02 13:57:48 +020034 pr_debug("Cyclic IF not ready yet\n");
35 return NULL;
36 }
37
38 cyclic = calloc(1, sizeof(struct cyclic_info));
39 if (!cyclic) {
40 pr_debug("Memory allocation error\n");
41 return NULL;
42 }
43
44 /* Store values in struct */
45 cyclic->func = func;
46 cyclic->ctx = ctx;
47 cyclic->name = strdup(name);
48 cyclic->delay_us = delay_us;
49 cyclic->start_time_us = timer_get_us();
Rasmus Villemoes28968392022-10-28 13:50:53 +020050 hlist_add_head(&cyclic->list, &gd->cyclic->cyclic_list);
Stefan Roesec2c69712022-09-02 13:57:48 +020051
52 return cyclic;
53}
54
55int cyclic_unregister(struct cyclic_info *cyclic)
56{
Rasmus Villemoes28968392022-10-28 13:50:53 +020057 hlist_del(&cyclic->list);
Stefan Roesec2c69712022-09-02 13:57:48 +020058 free(cyclic);
59
60 return 0;
61}
62
63void cyclic_run(void)
64{
Rasmus Villemoes28968392022-10-28 13:50:53 +020065 struct cyclic_info *cyclic;
66 struct hlist_node *tmp;
Stefan Roesec2c69712022-09-02 13:57:48 +020067 uint64_t now, cpu_time;
68
69 /* Prevent recursion */
Rasmus Villemoesd7de5ef2022-10-28 13:50:50 +020070 if (gd->flags & GD_FLG_CYCLIC_RUNNING)
Stefan Roesec2c69712022-09-02 13:57:48 +020071 return;
72
Rasmus Villemoesd7de5ef2022-10-28 13:50:50 +020073 gd->flags |= GD_FLG_CYCLIC_RUNNING;
Rasmus Villemoes28968392022-10-28 13:50:53 +020074 hlist_for_each_entry_safe(cyclic, tmp, &gd->cyclic->cyclic_list, list) {
Stefan Roesec2c69712022-09-02 13:57:48 +020075 /*
76 * Check if this cyclic function needs to get called, e.g.
77 * do not call the cyclic func too often
78 */
79 now = timer_get_us();
80 if (time_after_eq64(now, cyclic->next_call)) {
81 /* Call cyclic function and account it's cpu-time */
82 cyclic->next_call = now + cyclic->delay_us;
83 cyclic->func(cyclic->ctx);
84 cyclic->run_cnt++;
85 cpu_time = timer_get_us() - now;
86 cyclic->cpu_time_us += cpu_time;
87
88 /* Check if cpu-time exceeds max allowed time */
Stefan Roeseddc8d362022-10-17 09:00:58 +020089 if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&
90 (!cyclic->already_warned)) {
91 pr_err("cyclic function %s took too long: %lldus vs %dus max\n",
Stefan Roesec2c69712022-09-02 13:57:48 +020092 cyclic->name, cpu_time,
93 CONFIG_CYCLIC_MAX_CPU_TIME_US);
94
Stefan Roeseddc8d362022-10-17 09:00:58 +020095 /*
96 * Don't disable this function, just warn once
97 * about this exceeding CPU time usage
98 */
99 cyclic->already_warned = true;
Stefan Roesec2c69712022-09-02 13:57:48 +0200100 }
101 }
102 }
Rasmus Villemoesd7de5ef2022-10-28 13:50:50 +0200103 gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
Stefan Roesec2c69712022-09-02 13:57:48 +0200104}
105
Stefan Roese881d4102022-09-02 14:10:45 +0200106void schedule(void)
107{
108 /* The HW watchdog is not integrated into the cyclic IF (yet) */
109 if (IS_ENABLED(CONFIG_HW_WATCHDOG))
110 hw_watchdog_reset();
111
112 /*
113 * schedule() might get called very early before the cyclic IF is
114 * ready. Make sure to only call cyclic_run() when it's initalized.
115 */
Rasmus Villemoes6b84b1d2022-10-28 13:50:51 +0200116 if (gd && gd->cyclic)
Stefan Roese881d4102022-09-02 14:10:45 +0200117 cyclic_run();
118}
119
Stefan Roesec2c69712022-09-02 13:57:48 +0200120int cyclic_uninit(void)
121{
Rasmus Villemoes28968392022-10-28 13:50:53 +0200122 struct cyclic_info *cyclic;
123 struct hlist_node *tmp;
Stefan Roesec2c69712022-09-02 13:57:48 +0200124
Rasmus Villemoes28968392022-10-28 13:50:53 +0200125 hlist_for_each_entry_safe(cyclic, tmp, &gd->cyclic->cyclic_list, list)
Stefan Roesec2c69712022-09-02 13:57:48 +0200126 cyclic_unregister(cyclic);
Stefan Roesec2c69712022-09-02 13:57:48 +0200127
128 return 0;
129}
130
131int cyclic_init(void)
132{
133 int size = sizeof(struct cyclic_drv);
134
135 gd->cyclic = (struct cyclic_drv *)malloc(size);
136 if (!gd->cyclic)
137 return -ENOMEM;
138
139 memset(gd->cyclic, '\0', size);
Rasmus Villemoes28968392022-10-28 13:50:53 +0200140 INIT_HLIST_HEAD(&gd->cyclic->cyclic_list);
Stefan Roesec2c69712022-09-02 13:57:48 +0200141
142 return 0;
143}