cyclic: Add basic support for cyclic function execution infrastruture

Add the basic infrastructure to periodically execute code, e.g. all
100ms. Examples for such functions might be LED blinking etc. The
functions that are hooked into this cyclic list should be small timewise
as otherwise the execution of the other code that relies on a high
frequent polling (e.g. UART rx char ready check) might be delayed too
much. This patch also adds the Kconfig option
CONFIG_CYCLIC_MAX_CPU_TIME_US, which configures the max allowed time
for such a cyclic function. If it's execution time exceeds this time,
this cyclic function will get removed from the cyclic list.

How is this cyclic functionality executed?
The following patch integrates the main function responsible for
calling all registered cyclic functions cyclic_run() into the
common WATCHDOG_RESET macro. This guarantees that cyclic_run() is
executed very often, which is necessary for the cyclic functions to
get scheduled and executed at their configured periods.

This cyclic infrastructure will be used by a board specific function on
the NIC23 MIPS Octeon board, which needs to check periodically, if a
PCIe FLR has occurred.

Signed-off-by: Stefan Roese <sr@denx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/MAINTAINERS b/MAINTAINERS
index 1ebcd36..4af527e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -795,6 +795,12 @@
 F:	arch/m68k/
 F:	doc/arch/m68k.rst
 
+CYCLIC
+M:	Stefan Roese <sr@denx.de>
+S:	Maintained
+F:	common/cyclic.c
+F:	include/cyclic.h
+
 DFU
 M:	Lukasz Majewski <lukma@denx.de>
 S:	Maintained
diff --git a/common/Kconfig b/common/Kconfig
index 2c3f7f4..3e44acd 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -545,6 +545,26 @@
 
 menu "Start-up hooks"
 
+config CYCLIC
+	bool "General-purpose cyclic execution mechanism"
+	help
+	  This enables a general-purpose cyclic execution infrastructure,
+	  to allow "small" (run-time wise) functions to be executed at
+	  a specified frequency. Things like LED blinking or watchdog
+	  triggering are examples for such tasks.
+
+if CYCLIC
+
+config CYCLIC_MAX_CPU_TIME_US
+	int "Sets the max allowed time for a cyclic function in us"
+	default 1000
+	help
+	  The max allowed time for a cyclic function in us. If a functions
+	  takes longer than this duration this function will get unregistered
+	  automatically.
+
+endif # CYCLIC
+
 config EVENT
 	bool "General-purpose event-handling mechanism"
 	default y if SANDBOX
diff --git a/common/Makefile b/common/Makefile
index 2ed8672..1d56c9f 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -84,6 +84,7 @@
 endif
 endif
 
+obj-$(CONFIG_CYCLIC) += cyclic.o
 obj-$(CONFIG_$(SPL_TPL_)EVENT) += event.o
 
 obj-$(CONFIG_$(SPL_TPL_)HASH) += hash.o
diff --git a/common/cyclic.c b/common/cyclic.c
new file mode 100644
index 0000000..cd5dcb1
--- /dev/null
+++ b/common/cyclic.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * A general-purpose cyclic execution infrastructure, to allow "small"
+ * (run-time wise) functions to be executed at a specified frequency.
+ * Things like LED blinking or watchdog triggering are examples for such
+ * tasks.
+ *
+ * Copyright (C) 2022 Stefan Roese <sr@denx.de>
+ */
+
+#include <cyclic.h>
+#include <log.h>
+#include <malloc.h>
+#include <time.h>
+#include <linux/errno.h>
+#include <linux/list.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+struct list_head *cyclic_get_list(void)
+{
+	return &gd->cyclic->cyclic_list;
+}
+
+struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
+				    const char *name, void *ctx)
+{
+	struct cyclic_info *cyclic;
+
+	if (!gd->cyclic->cyclic_ready) {
+		pr_debug("Cyclic IF not ready yet\n");
+		return NULL;
+	}
+
+	cyclic = calloc(1, sizeof(struct cyclic_info));
+	if (!cyclic) {
+		pr_debug("Memory allocation error\n");
+		return NULL;
+	}
+
+	/* Store values in struct */
+	cyclic->func = func;
+	cyclic->ctx = ctx;
+	cyclic->name = strdup(name);
+	cyclic->delay_us = delay_us;
+	cyclic->start_time_us = timer_get_us();
+	list_add_tail(&cyclic->list, &gd->cyclic->cyclic_list);
+
+	return cyclic;
+}
+
+int cyclic_unregister(struct cyclic_info *cyclic)
+{
+	list_del(&cyclic->list);
+	free(cyclic);
+
+	return 0;
+}
+
+void cyclic_run(void)
+{
+	struct cyclic_info *cyclic, *tmp;
+	uint64_t now, cpu_time;
+
+	/* Prevent recursion */
+	if (gd->cyclic->cyclic_running)
+		return;
+
+	gd->cyclic->cyclic_running = true;
+	list_for_each_entry_safe(cyclic, tmp, &gd->cyclic->cyclic_list, list) {
+		/*
+		 * Check if this cyclic function needs to get called, e.g.
+		 * do not call the cyclic func too often
+		 */
+		now = timer_get_us();
+		if (time_after_eq64(now, cyclic->next_call)) {
+			/* Call cyclic function and account it's cpu-time */
+			cyclic->next_call = now + cyclic->delay_us;
+			cyclic->func(cyclic->ctx);
+			cyclic->run_cnt++;
+			cpu_time = timer_get_us() - now;
+			cyclic->cpu_time_us += cpu_time;
+
+			/* Check if cpu-time exceeds max allowed time */
+			if (cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) {
+				pr_err("cyclic function %s took too long: %lldus vs %dus max, disabling\n",
+				       cyclic->name, cpu_time,
+				       CONFIG_CYCLIC_MAX_CPU_TIME_US);
+
+				/* Unregister this cyclic function */
+				cyclic_unregister(cyclic);
+			}
+		}
+	}
+	gd->cyclic->cyclic_running = false;
+}
+
+int cyclic_uninit(void)
+{
+	struct cyclic_info *cyclic, *tmp;
+
+	list_for_each_entry_safe(cyclic, tmp, &gd->cyclic->cyclic_list, list)
+		cyclic_unregister(cyclic);
+	gd->cyclic->cyclic_ready = false;
+
+	return 0;
+}
+
+int cyclic_init(void)
+{
+	int size = sizeof(struct cyclic_drv);
+
+	gd->cyclic = (struct cyclic_drv *)malloc(size);
+	if (!gd->cyclic)
+		return -ENOMEM;
+
+	memset(gd->cyclic, '\0', size);
+	INIT_LIST_HEAD(&gd->cyclic->cyclic_list);
+	gd->cyclic->cyclic_ready = true;
+
+	return 0;
+}
diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h
index 805a6fd..9006c76 100644
--- a/include/asm-generic/global_data.h
+++ b/include/asm-generic/global_data.h
@@ -20,6 +20,7 @@
  */
 
 #ifndef __ASSEMBLY__
+#include <cyclic.h>
 #include <event_internal.h>
 #include <fdtdec.h>
 #include <membuff.h>
@@ -474,6 +475,12 @@
 	 */
 	struct event_state event_state;
 #endif
+#ifdef CONFIG_CYCLIC
+	/**
+	 * @cyclic: cyclic driver data
+	 */
+	struct cyclic_drv *cyclic;
+#endif
 	/**
 	 * @dmtag_list: List of DM tags
 	 */
diff --git a/include/cyclic.h b/include/cyclic.h
new file mode 100644
index 0000000..2390223
--- /dev/null
+++ b/include/cyclic.h
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * A general-purpose cyclic execution infrastructure, to allow "small"
+ * (run-time wise) functions to be executed at a specified frequency.
+ * Things like LED blinking or watchdog triggering are examples for such
+ * tasks.
+ *
+ * Copyright (C) 2022 Stefan Roese <sr@denx.de>
+ */
+
+#ifndef __cyclic_h
+#define __cyclic_h
+
+#include <linux/list.h>
+#include <asm/types.h>
+
+/**
+ * struct cyclic_drv - Cyclic driver internal data
+ *
+ * @cyclic_list: Cylic list node
+ * @cyclic_ready: Flag if cyclic infrastructure is ready
+ * @cyclic_running: Flag if cyclic infrastructure is running
+ */
+struct cyclic_drv {
+	struct list_head cyclic_list;
+	bool cyclic_ready;
+	bool cyclic_running;
+};
+
+/**
+ * struct cyclic_info - Information about cyclic execution function
+ *
+ * @func: Function to call periodically
+ * @ctx: Context pointer to get passed to this function
+ * @name: Name of the cyclic function, e.g. shown in the commands
+ * @delay_ns: Delay is ns after which this function shall get executed
+ * @start_time_us: Start time in us, when this function started its execution
+ * @cpu_time_us: Total CPU time of this function
+ * @run_cnt: Counter of executions occurances
+ * @next_call: Next time in us, when the function shall be executed again
+ * @list: List node
+ */
+struct cyclic_info {
+	void (*func)(void *ctx);
+	void *ctx;
+	char *name;
+	uint64_t delay_us;
+	uint64_t start_time_us;
+	uint64_t cpu_time_us;
+	uint64_t run_cnt;
+	uint64_t next_call;
+	struct list_head list;
+};
+
+/** Function type for cyclic functions */
+typedef void (*cyclic_func_t)(void *ctx);
+
+#if defined(CONFIG_CYCLIC)
+/**
+ * cyclic_register - Register a new cyclic function
+ *
+ * @func: Function to call periodically
+ * @delay_us: Delay is us after which this function shall get executed
+ * @name: Cyclic function name/id
+ * @ctx: Context to pass to the function
+ * @return: pointer to cyclic_struct if OK, NULL on error
+ */
+struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
+				    const char *name, void *ctx);
+
+/**
+ * cyclic_unregister - Unregister a cyclic function
+ *
+ * @cyclic: Pointer to cyclic_struct of the function that shall be removed
+ * @return: 0 if OK, -ve on error
+ */
+int cyclic_unregister(struct cyclic_info *cyclic);
+
+/**
+ * cyclic_init() - Set up cyclic functions
+ *
+ * Init a list of cyclic functions, so that these can be added as needed
+ */
+int cyclic_init(void);
+
+/**
+ * cyclic_uninit() - Clean up cyclic functions
+ *
+ * This removes all cyclic functions
+ */
+int cyclic_uninit(void);
+
+/**
+ * cyclic_get_list() - Get cyclic list pointer
+ *
+ * Return the cyclic list pointer
+ *
+ * @return: pointer to cyclic_list
+ */
+struct list_head *cyclic_get_list(void);
+
+/**
+ * cyclic_run() - Interate over all registered cyclic functions
+ *
+ * Interate over all registered cyclic functions and if the it's function
+ * needs to be executed, then call into these registered functions.
+ */
+void cyclic_run(void);
+#else
+static inline struct cyclic_info *cyclic_register(cyclic_func_t func,
+						  uint64_t delay_us,
+						  const char *name,
+						  void *ctx)
+{
+	return NULL;
+}
+
+static inline int cyclic_unregister(struct cyclic_info *cyclic)
+{
+	return 0;
+}
+
+static inline void cyclic_run(void)
+{
+}
+
+static inline int cyclic_init(void)
+{
+	return 0;
+}
+
+static inline int cyclic_uninit(void)
+{
+	return 0;
+}
+#endif
+
+#endif