event.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Events provide a general-purpose way to react to / subscribe to changes
  4. * within U-Boot
  5. *
  6. * Copyright 2021 Google LLC
  7. * Written by Simon Glass <sjg@chromium.org>
  8. */
  9. #define LOG_CATEGORY LOGC_EVENT
  10. #include <common.h>
  11. #include <event.h>
  12. #include <event_internal.h>
  13. #include <log.h>
  14. #include <linker_lists.h>
  15. #include <malloc.h>
  16. #include <asm/global_data.h>
  17. #include <linux/list.h>
  18. #include <relocate.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #if CONFIG_IS_ENABLED(EVENT_DEBUG)
  21. const char *const type_name[] = {
  22. "none",
  23. "test",
  24. /* Events related to driver model */
  25. "dm_post_init_f",
  26. "dm_post_init_r",
  27. "dm_pre_probe",
  28. "dm_post_probe",
  29. "dm_pre_remove",
  30. "dm_post_remove",
  31. /* init hooks */
  32. "misc_init_f",
  33. /* Fpga load hook */
  34. "fpga_load",
  35. /* fdt hooks */
  36. "ft_fixup",
  37. /* main loop events */
  38. "main_loop",
  39. };
  40. _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
  41. #endif
  42. static const char *event_type_name(enum event_t type)
  43. {
  44. #if CONFIG_IS_ENABLED(EVENT_DEBUG)
  45. return type_name[type];
  46. #else
  47. return "(unknown)";
  48. #endif
  49. }
  50. static int notify_static(struct event *ev)
  51. {
  52. struct evspy_info *start =
  53. ll_entry_start(struct evspy_info, evspy_info);
  54. const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
  55. struct evspy_info *spy;
  56. for (spy = start; spy != start + n_ents; spy++) {
  57. if (spy->type == ev->type) {
  58. int ret;
  59. log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
  60. event_type_name(ev->type), event_spy_id(spy));
  61. ret = spy->func(NULL, ev);
  62. /*
  63. * TODO: Handle various return codes to
  64. *
  65. * - claim an event (no others will see it)
  66. * - return an error from the event
  67. */
  68. if (ret)
  69. return log_msg_ret("spy", ret);
  70. }
  71. }
  72. return 0;
  73. }
  74. static int notify_dynamic(struct event *ev)
  75. {
  76. struct event_state *state = gd_event_state();
  77. struct event_spy *spy, *next;
  78. list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
  79. if (spy->type == ev->type) {
  80. int ret;
  81. log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
  82. event_type_name(ev->type), spy->id);
  83. ret = spy->func(spy->ctx, ev);
  84. /*
  85. * TODO: Handle various return codes to
  86. *
  87. * - claim an event (no others will see it)
  88. * - return an error from the event
  89. */
  90. if (ret)
  91. return log_msg_ret("spy", ret);
  92. }
  93. }
  94. return 0;
  95. }
  96. int event_notify(enum event_t type, void *data, int size)
  97. {
  98. struct event event;
  99. int ret;
  100. event.type = type;
  101. if (size > sizeof(event.data))
  102. return log_msg_ret("size", -E2BIG);
  103. memcpy(&event.data, data, size);
  104. ret = notify_static(&event);
  105. if (ret)
  106. return log_msg_ret("sta", ret);
  107. if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
  108. ret = notify_dynamic(&event);
  109. if (ret)
  110. return log_msg_ret("dyn", ret);
  111. }
  112. return 0;
  113. }
  114. int event_notify_null(enum event_t type)
  115. {
  116. return event_notify(type, NULL, 0);
  117. }
  118. void event_show_spy_list(void)
  119. {
  120. struct evspy_info *start =
  121. ll_entry_start(struct evspy_info, evspy_info);
  122. const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
  123. struct evspy_info *spy;
  124. const int size = sizeof(ulong) * 2;
  125. printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
  126. for (spy = start; spy != start + n_ents; spy++) {
  127. printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
  128. spy->type, event_type_name(spy->type), size, spy->func,
  129. event_spy_id(spy));
  130. }
  131. }
  132. #if IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)
  133. int event_manual_reloc(void)
  134. {
  135. struct evspy_info *spy, *end;
  136. spy = ll_entry_start(struct evspy_info, evspy_info);
  137. end = ll_entry_end(struct evspy_info, evspy_info);
  138. for (; spy < end; spy++)
  139. MANUAL_RELOC(spy->func);
  140. return 0;
  141. }
  142. #endif
  143. #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
  144. static void spy_free(struct event_spy *spy)
  145. {
  146. list_del(&spy->sibling_node);
  147. }
  148. int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
  149. {
  150. struct event_state *state = gd_event_state();
  151. struct event_spy *spy;
  152. spy = malloc(sizeof(*spy));
  153. if (!spy)
  154. return log_msg_ret("alloc", -ENOMEM);
  155. spy->id = id;
  156. spy->type = type;
  157. spy->func = func;
  158. spy->ctx = ctx;
  159. list_add_tail(&spy->sibling_node, &state->spy_head);
  160. return 0;
  161. }
  162. int event_uninit(void)
  163. {
  164. struct event_state *state = gd_event_state();
  165. struct event_spy *spy, *next;
  166. list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
  167. spy_free(spy);
  168. return 0;
  169. }
  170. int event_init(void)
  171. {
  172. struct event_state *state = gd_event_state();
  173. INIT_LIST_HEAD(&state->spy_head);
  174. return 0;
  175. }
  176. #endif /* EVENT_DYNAMIC */