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