cyclic.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include <cyclic.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <time.h>
  14. #include <linux/errno.h>
  15. #include <linux/list.h>
  16. #include <asm/global_data.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. void hw_watchdog_reset(void);
  19. struct hlist_head *cyclic_get_list(void)
  20. {
  21. /* Silence "discards 'volatile' qualifier" warning. */
  22. return (struct hlist_head *)&gd->cyclic_list;
  23. }
  24. struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
  25. const char *name, void *ctx)
  26. {
  27. struct cyclic_info *cyclic;
  28. cyclic = calloc(1, sizeof(struct cyclic_info));
  29. if (!cyclic) {
  30. pr_debug("Memory allocation error\n");
  31. return NULL;
  32. }
  33. /* Store values in struct */
  34. cyclic->func = func;
  35. cyclic->ctx = ctx;
  36. cyclic->name = strdup(name);
  37. cyclic->delay_us = delay_us;
  38. cyclic->start_time_us = timer_get_us();
  39. hlist_add_head(&cyclic->list, cyclic_get_list());
  40. return cyclic;
  41. }
  42. int cyclic_unregister(struct cyclic_info *cyclic)
  43. {
  44. hlist_del(&cyclic->list);
  45. free(cyclic);
  46. return 0;
  47. }
  48. void cyclic_run(void)
  49. {
  50. struct cyclic_info *cyclic;
  51. struct hlist_node *tmp;
  52. uint64_t now, cpu_time;
  53. /* Prevent recursion */
  54. if (gd->flags & GD_FLG_CYCLIC_RUNNING)
  55. return;
  56. gd->flags |= GD_FLG_CYCLIC_RUNNING;
  57. hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
  58. /*
  59. * Check if this cyclic function needs to get called, e.g.
  60. * do not call the cyclic func too often
  61. */
  62. now = timer_get_us();
  63. if (time_after_eq64(now, cyclic->next_call)) {
  64. /* Call cyclic function and account it's cpu-time */
  65. cyclic->next_call = now + cyclic->delay_us;
  66. cyclic->func(cyclic->ctx);
  67. cyclic->run_cnt++;
  68. cpu_time = timer_get_us() - now;
  69. cyclic->cpu_time_us += cpu_time;
  70. /* Check if cpu-time exceeds max allowed time */
  71. if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&
  72. (!cyclic->already_warned)) {
  73. pr_err("cyclic function %s took too long: %lldus vs %dus max\n",
  74. cyclic->name, cpu_time,
  75. CONFIG_CYCLIC_MAX_CPU_TIME_US);
  76. /*
  77. * Don't disable this function, just warn once
  78. * about this exceeding CPU time usage
  79. */
  80. cyclic->already_warned = true;
  81. }
  82. }
  83. }
  84. gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
  85. }
  86. void schedule(void)
  87. {
  88. /* The HW watchdog is not integrated into the cyclic IF (yet) */
  89. if (IS_ENABLED(CONFIG_HW_WATCHDOG))
  90. hw_watchdog_reset();
  91. /*
  92. * schedule() might get called very early before the cyclic IF is
  93. * ready. Make sure to only call cyclic_run() when it's initalized.
  94. */
  95. if (gd)
  96. cyclic_run();
  97. }
  98. int cyclic_unregister_all(void)
  99. {
  100. struct cyclic_info *cyclic;
  101. struct hlist_node *tmp;
  102. hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list)
  103. cyclic_unregister(cyclic);
  104. return 0;
  105. }