1 // 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 #include <u-boot/schedule.h>
19 #include <uthread.h>
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 void hw_watchdog_reset(void);
24 
cyclic_get_list(void)25 struct hlist_head *cyclic_get_list(void)
26 {
27 	/* Silence "discards 'volatile' qualifier" warning. */
28 	return (struct hlist_head *)&gd->cyclic_list;
29 }
30 
cyclic_is_registered(const struct cyclic_info * cyclic)31 static 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 
cyclic_register(struct cyclic_info * cyclic,cyclic_func_t func,uint64_t delay_us,const char * name)43 void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
44 		     uint64_t delay_us, const char *name)
45 {
46 	cyclic_unregister(cyclic);
47 
48 	memset(cyclic, 0, sizeof(*cyclic));
49 
50 	/* Store values in struct */
51 	cyclic->func = func;
52 	cyclic->name = name;
53 	cyclic->delay_us = delay_us;
54 	cyclic->start_time_us = get_timer_us(0);
55 	hlist_add_head(&cyclic->list, cyclic_get_list());
56 }
57 
cyclic_unregister(struct cyclic_info * cyclic)58 void cyclic_unregister(struct cyclic_info *cyclic)
59 {
60 	if (!cyclic_is_registered(cyclic))
61 		return;
62 
63 	hlist_del(&cyclic->list);
64 }
65 
cyclic_run(void)66 static void cyclic_run(void)
67 {
68 	struct cyclic_info *cyclic;
69 	struct hlist_node *tmp;
70 	uint64_t now, cpu_time;
71 
72 	/* Prevent recursion */
73 	if (gd->flags & GD_FLG_CYCLIC_RUNNING)
74 		return;
75 
76 	gd->flags |= GD_FLG_CYCLIC_RUNNING;
77 	hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
78 		/*
79 		 * Check if this cyclic function needs to get called, e.g.
80 		 * do not call the cyclic func too often
81 		 */
82 		now = get_timer_us(0);
83 		if (time_after_eq64(now, cyclic->next_call)) {
84 			/* Call cyclic function and account it's cpu-time */
85 			cyclic->next_call = now + cyclic->delay_us;
86 			cyclic->func(cyclic);
87 			cyclic->run_cnt++;
88 			cpu_time = get_timer_us(0) - now;
89 			cyclic->cpu_time_us += cpu_time;
90 
91 			/* Check if cpu-time exceeds max allowed time */
92 			if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&
93 			    (!cyclic->already_warned)) {
94 				pr_err("cyclic function %s took too long: %lldus vs %dus max\n",
95 				       cyclic->name, cpu_time,
96 				       CONFIG_CYCLIC_MAX_CPU_TIME_US);
97 
98 				/*
99 				 * Don't disable this function, just warn once
100 				 * about this exceeding CPU time usage
101 				 */
102 				cyclic->already_warned = true;
103 			}
104 		}
105 	}
106 	gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
107 }
108 
schedule(void)109 void schedule(void)
110 {
111 	/* The HW watchdog is not integrated into the cyclic IF (yet) */
112 	if (IS_ENABLED(CONFIG_HW_WATCHDOG))
113 		hw_watchdog_reset();
114 
115 	/*
116 	 * schedule() might get called very early before the cyclic IF is
117 	 * ready. Make sure to only call cyclic_run() when it's initalized.
118 	 */
119 	if (gd)
120 		cyclic_run();
121 
122 	uthread_schedule();
123 }
124 
cyclic_unregister_all(void)125 int cyclic_unregister_all(void)
126 {
127 	struct cyclic_info *cyclic;
128 	struct hlist_node *tmp;
129 
130 	hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list)
131 		cyclic_unregister(cyclic);
132 
133 	return 0;
134 }
135