Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 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 | |
| 10 | #define LOG_CATEGORY LOGC_EVENT |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <event.h> |
| 14 | #include <event_internal.h> |
| 15 | #include <log.h> |
| 16 | #include <linker_lists.h> |
| 17 | #include <malloc.h> |
| 18 | #include <asm/global_data.h> |
| 19 | #include <linux/list.h> |
Ovidiu Panait | 145f200 | 2022-05-15 21:40:29 +0300 | [diff] [blame] | 20 | #include <relocate.h> |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | #if CONFIG_IS_ENABLED(EVENT_DEBUG) |
| 25 | const char *const type_name[] = { |
| 26 | "none", |
| 27 | "test", |
Simon Glass | 3ee337a | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 28 | |
| 29 | /* Events related to driver model */ |
Jaehoon Chung | 74cd2d8 | 2023-08-01 19:17:00 +0900 | [diff] [blame] | 30 | "dm_post_init_f", |
Chanho Park | 5865a00 | 2023-08-18 14:11:02 +0900 | [diff] [blame] | 31 | "dm_post_init_r", |
Simon Glass | 3ee337a | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 32 | "dm_pre_probe", |
| 33 | "dm_post_probe", |
| 34 | "dm_pre_remove", |
| 35 | "dm_post_remove", |
Simon Glass | f1c5191 | 2022-03-04 08:43:04 -0700 | [diff] [blame] | 36 | |
| 37 | /* init hooks */ |
| 38 | "misc_init_f", |
Simon Glass | fcebb7b | 2023-08-21 21:16:59 -0600 | [diff] [blame] | 39 | "fsp_init_r", |
Simon Glass | 1cedca1 | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 40 | "last_stage_init", |
Simon Glass | 4305fe7 | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 41 | |
Christian Taedcke | df36981 | 2023-07-20 09:27:24 +0200 | [diff] [blame] | 42 | /* Fpga load hook */ |
| 43 | "fpga_load", |
| 44 | |
Simon Glass | 4305fe7 | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 45 | /* fdt hooks */ |
| 46 | "ft_fixup", |
Sughosh Ganu | b780fa5 | 2022-10-21 18:16:01 +0530 | [diff] [blame] | 47 | |
| 48 | /* main loop events */ |
| 49 | "main_loop", |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size"); |
| 53 | #endif |
| 54 | |
Simon Glass | d6db894 | 2023-08-21 21:16:53 -0600 | [diff] [blame] | 55 | const char *event_type_name(enum event_t type) |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 56 | { |
| 57 | #if CONFIG_IS_ENABLED(EVENT_DEBUG) |
| 58 | return type_name[type]; |
| 59 | #else |
| 60 | return "(unknown)"; |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | static int notify_static(struct event *ev) |
| 65 | { |
| 66 | struct evspy_info *start = |
| 67 | ll_entry_start(struct evspy_info, evspy_info); |
| 68 | const int n_ents = ll_entry_count(struct evspy_info, evspy_info); |
| 69 | struct evspy_info *spy; |
| 70 | |
| 71 | for (spy = start; spy != start + n_ents; spy++) { |
| 72 | if (spy->type == ev->type) { |
| 73 | int ret; |
| 74 | |
| 75 | log_debug("Sending event %x/%s to spy '%s'\n", ev->type, |
| 76 | event_type_name(ev->type), event_spy_id(spy)); |
Simon Glass | 3bfbedd | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 77 | if (spy->flags & EVSPYF_SIMPLE) { |
| 78 | const struct evspy_info_simple *simple; |
| 79 | |
| 80 | simple = (struct evspy_info_simple *)spy; |
| 81 | ret = simple->func(); |
| 82 | } else { |
| 83 | ret = spy->func(NULL, ev); |
| 84 | } |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * TODO: Handle various return codes to |
| 88 | * |
| 89 | * - claim an event (no others will see it) |
| 90 | * - return an error from the event |
| 91 | */ |
| 92 | if (ret) |
| 93 | return log_msg_ret("spy", ret); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int notify_dynamic(struct event *ev) |
| 101 | { |
| 102 | struct event_state *state = gd_event_state(); |
| 103 | struct event_spy *spy, *next; |
| 104 | |
| 105 | list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) { |
| 106 | if (spy->type == ev->type) { |
| 107 | int ret; |
| 108 | |
| 109 | log_debug("Sending event %x/%s to spy '%s'\n", ev->type, |
| 110 | event_type_name(ev->type), spy->id); |
| 111 | ret = spy->func(spy->ctx, ev); |
| 112 | |
| 113 | /* |
| 114 | * TODO: Handle various return codes to |
| 115 | * |
| 116 | * - claim an event (no others will see it) |
| 117 | * - return an error from the event |
| 118 | */ |
| 119 | if (ret) |
| 120 | return log_msg_ret("spy", ret); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int event_notify(enum event_t type, void *data, int size) |
| 128 | { |
| 129 | struct event event; |
| 130 | int ret; |
| 131 | |
| 132 | event.type = type; |
| 133 | if (size > sizeof(event.data)) |
| 134 | return log_msg_ret("size", -E2BIG); |
| 135 | memcpy(&event.data, data, size); |
| 136 | |
| 137 | ret = notify_static(&event); |
| 138 | if (ret) |
Simon Glass | 1573cca | 2023-01-17 10:47:31 -0700 | [diff] [blame] | 139 | return log_msg_ret("sta", ret); |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 140 | |
| 141 | if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) { |
| 142 | ret = notify_dynamic(&event); |
| 143 | if (ret) |
| 144 | return log_msg_ret("dyn", ret); |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int event_notify_null(enum event_t type) |
| 151 | { |
| 152 | return event_notify(type, NULL, 0); |
| 153 | } |
| 154 | |
| 155 | void event_show_spy_list(void) |
| 156 | { |
| 157 | struct evspy_info *start = |
| 158 | ll_entry_start(struct evspy_info, evspy_info); |
| 159 | const int n_ents = ll_entry_count(struct evspy_info, evspy_info); |
| 160 | struct evspy_info *spy; |
| 161 | const int size = sizeof(ulong) * 2; |
| 162 | |
| 163 | printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID"); |
| 164 | for (spy = start; spy != start + n_ents; spy++) { |
| 165 | printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start), |
| 166 | spy->type, event_type_name(spy->type), size, spy->func, |
| 167 | event_spy_id(spy)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #if CONFIG_IS_ENABLED(EVENT_DYNAMIC) |
| 172 | static void spy_free(struct event_spy *spy) |
| 173 | { |
| 174 | list_del(&spy->sibling_node); |
| 175 | } |
| 176 | |
| 177 | int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx) |
| 178 | { |
| 179 | struct event_state *state = gd_event_state(); |
| 180 | struct event_spy *spy; |
| 181 | |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 182 | spy = malloc(sizeof(*spy)); |
| 183 | if (!spy) |
| 184 | return log_msg_ret("alloc", -ENOMEM); |
| 185 | |
| 186 | spy->id = id; |
| 187 | spy->type = type; |
| 188 | spy->func = func; |
| 189 | spy->ctx = ctx; |
| 190 | list_add_tail(&spy->sibling_node, &state->spy_head); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int event_uninit(void) |
| 196 | { |
| 197 | struct event_state *state = gd_event_state(); |
| 198 | struct event_spy *spy, *next; |
| 199 | |
| 200 | list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) |
| 201 | spy_free(spy); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int event_init(void) |
| 207 | { |
| 208 | struct event_state *state = gd_event_state(); |
| 209 | |
| 210 | INIT_LIST_HEAD(&state->spy_head); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | #endif /* EVENT_DYNAMIC */ |