blob: b3c180bd1a62c14835f7e81d266f296ae98399fa [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>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Stefan Roese97473632022-09-02 14:10:45 +020021void hw_watchdog_reset(void);
22
Stefan Roese036f89a2022-09-02 13:57:48 +020023struct list_head *cyclic_get_list(void)
24{
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
33 if (!gd->cyclic->cyclic_ready) {
34 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();
50 list_add_tail(&cyclic->list, &gd->cyclic->cyclic_list);
51
52 return cyclic;
53}
54
55int cyclic_unregister(struct cyclic_info *cyclic)
56{
57 list_del(&cyclic->list);
58 free(cyclic);
59
60 return 0;
61}
62
63void cyclic_run(void)
64{
65 struct cyclic_info *cyclic, *tmp;
66 uint64_t now, cpu_time;
67
68 /* Prevent recursion */
69 if (gd->cyclic->cyclic_running)
70 return;
71
72 gd->cyclic->cyclic_running = true;
73 list_for_each_entry_safe(cyclic, tmp, &gd->cyclic->cyclic_list, list) {
74 /*
75 * Check if this cyclic function needs to get called, e.g.
76 * do not call the cyclic func too often
77 */
78 now = timer_get_us();
79 if (time_after_eq64(now, cyclic->next_call)) {
80 /* Call cyclic function and account it's cpu-time */
81 cyclic->next_call = now + cyclic->delay_us;
82 cyclic->func(cyclic->ctx);
83 cyclic->run_cnt++;
84 cpu_time = timer_get_us() - now;
85 cyclic->cpu_time_us += cpu_time;
86
87 /* Check if cpu-time exceeds max allowed time */
88 if (cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) {
89 pr_err("cyclic function %s took too long: %lldus vs %dus max, disabling\n",
90 cyclic->name, cpu_time,
91 CONFIG_CYCLIC_MAX_CPU_TIME_US);
92
93 /* Unregister this cyclic function */
94 cyclic_unregister(cyclic);
95 }
96 }
97 }
98 gd->cyclic->cyclic_running = false;
99}
100
Stefan Roese97473632022-09-02 14:10:45 +0200101void schedule(void)
102{
103 /* The HW watchdog is not integrated into the cyclic IF (yet) */
104 if (IS_ENABLED(CONFIG_HW_WATCHDOG))
105 hw_watchdog_reset();
106
107 /*
108 * schedule() might get called very early before the cyclic IF is
109 * ready. Make sure to only call cyclic_run() when it's initalized.
110 */
111 if (gd && gd->cyclic && gd->cyclic->cyclic_ready)
112 cyclic_run();
113}
114
Stefan Roese036f89a2022-09-02 13:57:48 +0200115int cyclic_uninit(void)
116{
117 struct cyclic_info *cyclic, *tmp;
118
119 list_for_each_entry_safe(cyclic, tmp, &gd->cyclic->cyclic_list, list)
120 cyclic_unregister(cyclic);
121 gd->cyclic->cyclic_ready = false;
122
123 return 0;
124}
125
126int cyclic_init(void)
127{
128 int size = sizeof(struct cyclic_drv);
129
130 gd->cyclic = (struct cyclic_drv *)malloc(size);
131 if (!gd->cyclic)
132 return -ENOMEM;
133
134 memset(gd->cyclic, '\0', size);
135 INIT_LIST_HEAD(&gd->cyclic->cyclic_list);
136 gd->cyclic->cyclic_ready = true;
137
138 return 0;
139}